ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SuffixSorting.h

The engineered construction of the suffix array and of its LCP array, beyond the book. More...

Files

file  ADS/string/SuffixSorting.h
 The engineered construction of the suffix array and of its LCP array, beyond the book: the ones that the benchmark of the suffix arrays compares with libsais and libdivsufsort, while the constructions of SuffixArray.h stay those of the textbooks, on integers of the size of a pointer.

Functions

template<class I = std::uint32_t>
requires ( std::same_as< I , std::uint32_t > || std::same_as< I , std::uint64_t > )
std::vector< I > ADS::suffixArray (std::string_view T)
 the suffix array of the bytes of \( T \), by SA-IS on integers I (32 or 64 bits, unsigned)
template<class I>
requires ( std::same_as< I , std::uint32_t > || std::same_as< I , std::uint64_t > )
std::vector< I > ADS::suffixArray (const std::vector< I > &T, std::size_t K)
 the suffix array of an integer text, by SA-IS
template<class I>
std::vector< I > ADS::plcpArray (std::string_view T, const std::vector< I > &SA)
 the permuted LCP array: \( \mathit{PLCP}[ i ] \) is the lcp of the suffix \( i \) and the one before it in \( \mathit{SA} \) (0 for the first)
template<class I>
std::vector< I > ADS::lcpArrayPhi (std::string_view T, const std::vector< I > &SA)
 the LCP array by the permuted LCP array, \( n - 1 \) entries (none if \( n < 2 \))

Detailed description

The engineered construction of the suffix array and of its LCP array, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of strings.

Function documentation

◆ lcpArrayPhi()

template<class I>
std::vector< I > ADS::lcpArrayPhi ( std::string_view T,
const std::vector< I > & SA )
nodiscard

the LCP array by the permuted LCP array, \( n - 1 \) entries (none if \( n < 2 \))

In pseudocode (the algorithm of Kärkkäinen, Manzini and Puglisi, beyond the book), the reads of \( \mathit{PLCP} \) falling in its copy of \( n \) bytes when \( n \ge 2^{18} \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcpArrayPhi}( T , \mathit{SA} ) \cr 2 & \quad \mathit{PLCP} \gets \mathrm{plcpArray}( T , \mathit{SA} ) \cr 3 & \quad \textbf{for}\ r \gets 0\ \textbf{to}\ n - 2\ \textbf{do} \ \mathit{LCP}[ r ] \gets \mathit{PLCP}[ \mathit{SA}[ r + 1 ] ] \cr 4 & \quad \textbf{return}\ \mathit{LCP} \end{array}} \]

Precondition
\( \mathit{SA} \) is a suffix array of \( T \) (an audit)
Postcondition
\( \mathit{LCP}[ r ] \) is the lcp of the suffixes \( \mathit{SA}[ r ] \) and \( \mathit{SA}[ r + 1 ] \) (an audit)

◆ plcpArray()

template<class I>
std::vector< I > ADS::plcpArray ( std::string_view T,
const std::vector< I > & SA )
nodiscard

the permuted LCP array: \( \mathit{PLCP}[ i ] \) is the lcp of the suffix \( i \) and the one before it in \( \mathit{SA} \) (0 for the first)

In pseudocode (the algorithm of Kärkkäinen, Manzini and Puglisi, beyond the book), \( \Phi \) and \( \mathit{PLCP} \) in one array, since \( \Phi[ i ] \) is read just before \( \mathit{PLCP}[ i ] \) is written:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{plcpArray}( T , \mathit{SA} ) \cr 2 & \quad \Phi[ \mathit{SA}[ 0 ] ] \gets n \cr 3 & \quad \textbf{for}\ r \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \ \Phi[ \mathit{SA}[ r ] ] \gets \mathit{SA}[ r - 1 ] \cr 4 & \quad h \gets 0 \cr 5 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 6 & \qquad j \gets \Phi[ i ] \cr 7 & \qquad \textbf{while}\ h < n - \max\{ i , j \}\ \textbf{and}\ T[ i + h ] = T[ j + h ]\ \textbf{do}\ h \gets h + 1 \cr 8 & \qquad \mathit{PLCP}[ i ] \gets h;\ \textbf{if}\ h > 0\ \textbf{then} \ h \gets h - 1 \cr 9 & \quad \textbf{return}\ \mathit{PLCP} \end{array}} \]

Precondition
\( \mathit{SA} \) is a suffix array of \( T \) (an audit)

◆ suffixArray() [1/2]

template<class I>
requires ( std::same_as< I , std::uint32_t > || std::same_as< I , std::uint64_t > )
std::vector< I > ADS::suffixArray ( const std::vector< I > & T,
std::size_t K )
nodiscard

the suffix array of an integer text, by SA-IS

It is the algorithm of suffixArray() above without the direct sort and the comparisons of the text, which need bytes: the LMS-substrings are sorted by the partial induction, and the ties left by the doubling or by the recursion.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArray}( T , K ) \cr 2 & \quad \textbf{return}\ \text{the lines 2, 3 and 7 to 13 of } \mathrm{suffixArray}( T ) \text{, on symbols in } [ 0 , K ) \end{array}} \]

Precondition
every symbol of \( T \) is less than \( K \), and \( |T| < 2^{31} - 1 \) if I has 32 bits
Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArray() [2/2]

template<class I = std::uint32_t>
requires ( std::same_as< I , std::uint32_t > || std::same_as< I , std::uint64_t > )
std::vector< I > ADS::suffixArray ( std::string_view T)
nodiscard

the suffix array of the bytes of \( T \), by SA-IS on integers I (32 or 64 bits, unsigned)

In pseudocode (SA-IS of Nong, Zhang and Chan as engineered here, beyond the book), \( m \) the number of LMS places:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArray}( T ) \cr 2 & \quad \text{the types } t_i \text{ by the carries of } G + ( G \vee E ) \text{, 64 places a word, and the counts of the kinds} \cr 3 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ ( n - 1 , \ldots , 0 ) \quad \triangleright\ \text{or one induction, if some } t_i = \text{S} \cr 4 & \quad \textbf{if}\ \text{a sample of the windows has few ties } \textbf{and}\ \text{the LMS-suffixes sort by their windows} \ \textbf{then} \cr 5 & \qquad \text{the LMS-suffixes are sorted, by a radix sort on the } \text{windows and insertion} \cr 6 & \quad \textbf{else} \cr 7 & \qquad \text{sort the LMS-substrings by the partial induction, and } \text{name them} \cr 8 & \qquad \textbf{if}\ \text{two names are equal}\ \textbf{then} \cr 9 & \qquad\quad \text{sort the groups of equal names by the text that } \text{follows, then by the names that follow} \cr 10 & \qquad\quad \textbf{if}\ \text{some group is still tied} \ \textbf{then}\ \text{sort by the doubling of Larsson and Sadakane,} \cr & \qquad\qquad \text{or by } \mathrm{suffixArray}( \text{the string of } \text{the names} ) \cr 11 & \quad \text{move the sorted LMS-suffixes to the ends of their } \text{buckets} \cr 12 & \quad \text{induce the L-suffixes from left to right and the } \text{S-suffixes from right to left} \cr 13 & \quad \textbf{return}\ \mathit{SA} \end{array}} \]

Precondition
\( |T| < 2^{31} - 1 \) if I has 32 bits
Postcondition
a suffix array of \( T \) (an audit)