ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SuffixArray Class Reference

the suffix array of a text with its \( \mathit{LCP} \), \( \mathit{Llcp} \) and \( \mathit{Rlcp} \) arrays and the range minima of \( \mathit{LCP} \) More...

#include <ADS/string/SuffixArray.h>

Public types

using value_type = std::size_t
 a place of the text
using size_type = std::size_t
 the type of the ranks
using difference_type = std::ptrdiff_t
 a difference of ranks
using const_reference = const std::size_t &
 an entry of SA
using reference = const_reference
 an entry of SA
using const_iterator = std::vector< std::size_t >::const_iterator
 a constant iterator on the entries of SA
using iterator = const_iterator
 on the entries of SA

Public member functions

 SuffixArray (void)=default
 the index of the empty text
 SuffixArray (std::string_view T)
 the index of the bytes of \( T \), built by suffixArraySais() and lcpArray()
 SuffixArray (Symbols T, std::size_t K)
 the index of an integer text, built as the one of the bytes above
std::pair< std::size_t, std::size_t > range (const Symbols &P, long *comparisons=nullptr) const
 the range \( [ \mathit{first} , \mathit{last} ) \) of \( \mathit{SA} \) of the suffixes prefixed by \( P \), by two binary searches; the comparisons of symbols go to *comparisons, if given
std::pair< std::size_t, std::size_t > range (std::string_view P, long *comparisons=nullptr) const
 the range of range() above, of the bytes of \( P \)
std::pair< std::size_t, std::size_t > rangeLcp (const Symbols &P, long *comparisons=nullptr) const
 the range of range() by the search of Manber and Myers, \( O( p + \log n ) \) comparisons of symbols
std::pair< std::size_t, std::size_t > rangeLcp (std::string_view P, long *comparisons=nullptr) const
 the range of rangeLcp() above, of the bytes of \( P \)
std::size_t count (std::string_view P) const
 the number of occurrences of \( P \), the size of the range of rangeLcp()
std::vector< std::size_t > locate (std::string_view P) const
 the places of the occurrences of \( P \), in the order of \( \mathit{SA} \): the entries of the range of rangeLcp()
std::vector< std::size_t > locate (const Symbols &P) const
 the places of locate() above, of an integer pattern
std::size_t lce (std::size_t i, std::size_t j) const
 the length of the longest common prefix of the suffixes \( i \) and \( j \), \( O( 1 ) \)
std::size_t rank (std::size_t i) const
 the rank of the suffix \( i \) in \( \mathit{SA} \)
std::size_t operator[] (std::size_t r) const
 the place \( \mathit{SA}[ r ] \) of the suffix of rank \( r \)
std::size_t size (void) const
 the length \( n \) of the text
const Symbols & text (void) const
 the text \( T \)
const std::vector< std::size_t > & lcp (void) const
 the array \( \mathit{LCP} \), \( n - 1 \) entries
const_iterator begin (void) const
 the first entry of \( \mathit{SA} \)
const_iterator end (void) const
 one past the last entry of \( \mathit{SA} \)
const_iterator cbegin (void) const
 the first entry of \( \mathit{SA} \)
const_iterator cend (void) const
 one past the last entry of \( \mathit{SA} \)

Detailed description

the suffix array of a text with its \( \mathit{LCP} \), \( \mathit{Llcp} \) and \( \mathit{Rlcp} \) arrays and the range minima of \( \mathit{LCP} \)

Member typedef documentation

◆ const_iterator

using ADS::SuffixArray::const_iterator = std::vector< std::size_t >::const_iterator

a constant iterator on the entries of SA

◆ const_reference

using ADS::SuffixArray::const_reference = const std::size_t &

an entry of SA

◆ difference_type

using ADS::SuffixArray::difference_type = std::ptrdiff_t

a difference of ranks

◆ iterator

on the entries of SA

◆ reference

◆ size_type

using ADS::SuffixArray::size_type = std::size_t

the type of the ranks

◆ value_type

using ADS::SuffixArray::value_type = std::size_t

a place of the text

Constructor and destructor documentation

◆ SuffixArray() [1/3]

ADS::SuffixArray::SuffixArray ( void )
default

the index of the empty text

◆ SuffixArray() [2/3]

ADS::SuffixArray::SuffixArray ( std::string_view T)
explicit

the index of the bytes of \( T \), built by suffixArraySais() and lcpArray()

In pseudocode (the search of Manber and Myers, beyond the book), the arrays \( \mathit{Llcp} \) and \( \mathit{Rlcp} \) filled by the call \( \mathrm{build}( -1 , n ) \), which gives the lcp of the suffixes at the ends of the range \( ( L , R ) \) of a binary search:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( L , R ) \cr 2 & \quad \textbf{if}\ R - L = 1\ \textbf{then return}\ \mathit{LCP}[ L ] \text{ if } L \ge 0 \text{ and } R < n \text{, and 0 otherwise} \cr 3 & \quad M \gets \lfloor ( L + R ) / 2 \rfloor \cr 4 & \quad \mathit{Llcp}[ M ] \gets \mathrm{build}( L , M ); \ \mathit{Rlcp}[ M ] \gets \mathrm{build}( M , R ) \cr 5 & \quad \textbf{return}\ \min\{ \mathit{Llcp}[ M ] , \mathit{Rlcp}[ M ] \} \end{array}} \]

◆ SuffixArray() [3/3]

ADS::SuffixArray::SuffixArray ( Symbols T,
std::size_t K )

the index of an integer text, built as the one of the bytes above

Precondition
every symbol of \( T \) is less than \( K \)

Member function documentation

◆ begin()

const_iterator ADS::SuffixArray::begin ( void ) const
inlinenodiscard

the first entry of \( \mathit{SA} \)

◆ cbegin()

const_iterator ADS::SuffixArray::cbegin ( void ) const
inlinenodiscard

the first entry of \( \mathit{SA} \)

◆ cend()

const_iterator ADS::SuffixArray::cend ( void ) const
inlinenodiscard

one past the last entry of \( \mathit{SA} \)

◆ count()

std::size_t ADS::SuffixArray::count ( std::string_view P) const
nodiscard

the number of occurrences of \( P \), the size of the range of rangeLcp()

◆ end()

const_iterator ADS::SuffixArray::end ( void ) const
inlinenodiscard

one past the last entry of \( \mathit{SA} \)

◆ lce()

std::size_t ADS::SuffixArray::lce ( std::size_t i,
std::size_t j ) const
nodiscard

the length of the longest common prefix of the suffixes \( i \) and \( j \), \( O( 1 ) \)

In pseudocode (beyond the book), by a query of range minimum on \( \mathit{LCP} \), \( R \) the inverse of \( \mathit{SA} \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lce}( i , j ) \cr 2 & \quad \textbf{if}\ i = n\ \textbf{or}\ j = n\ \textbf{then return} \ 0 \cr 3 & \quad \textbf{if}\ i = j\ \textbf{then return}\ n - i \cr 4 & \quad a \gets \min\{ R[ i ] , R[ j ] \};\ b \gets \max\{ R[ i ] , R[ j ] \} \cr 5 & \quad \textbf{return}\ \min_{a \le r < b} \mathit{LCP}[ r ] \end{array}} \]

Precondition
\( i \le n \) and \( j \le n \), \( n \) the value of size()

◆ lcp()

const std::vector< std::size_t > & ADS::SuffixArray::lcp ( void ) const
inlinenodiscard

the array \( \mathit{LCP} \), \( n - 1 \) entries

◆ locate() [1/2]

std::vector< std::size_t > ADS::SuffixArray::locate ( const Symbols & P) const
nodiscard

the places of locate() above, of an integer pattern

◆ locate() [2/2]

std::vector< std::size_t > ADS::SuffixArray::locate ( std::string_view P) const
nodiscard

the places of the occurrences of \( P \), in the order of \( \mathit{SA} \): the entries of the range of rangeLcp()

◆ operator[]()

std::size_t ADS::SuffixArray::operator[] ( std::size_t r) const
nodiscard

the place \( \mathit{SA}[ r ] \) of the suffix of rank \( r \)

Precondition
\( r < n \)

◆ range() [1/2]

std::pair< std::size_t, std::size_t > ADS::SuffixArray::range ( const Symbols & P,
long * comparisons = nullptr ) const
nodiscard

the range \( [ \mathit{first} , \mathit{last} ) \) of \( \mathit{SA} \) of the suffixes prefixed by \( P \), by two binary searches; the comparisons of symbols go to *comparisons, if given

In pseudocode (chapter 10, Searching Strings by Substring, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{range}( P ) \cr 2 & \quad \textbf{return}\ ( \mathrm{bound}( P , \textbf{false} ) , \mathrm{bound}( P , \textbf{true} ) ) \cr 3 & \textbf{function}\ \mathrm{bound}( P , \mathit{upper} ) \cr 4 & \quad L \gets 0;\ R \gets n \cr 5 & \quad \textbf{while}\ L < R\ \textbf{do} \cr 6 & \qquad M \gets \lfloor ( L + R ) / 2 \rfloor \cr 7 & \qquad \textbf{if}\ P \text{, followed by a symbol larger than all } \text{if } \mathit{upper} \text{, is greater than the suffix } \mathit{SA}[ M ]\ \textbf{then} \cr 8 & \qquad\quad L \gets M + 1 \cr 9 & \qquad \textbf{else}\ R \gets M \cr 10 & \quad \textbf{return}\ L \end{array}} \]

◆ range() [2/2]

std::pair< std::size_t, std::size_t > ADS::SuffixArray::range ( std::string_view P,
long * comparisons = nullptr ) const
nodiscard

the range of range() above, of the bytes of \( P \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{range}( P ) \cr 2 & \quad \textbf{return}\ \mathrm{range}( \mathrm{toSymbols}( P ) ) \end{array}} \]

◆ rangeLcp() [1/2]

std::pair< std::size_t, std::size_t > ADS::SuffixArray::rangeLcp ( const Symbols & P,
long * comparisons = nullptr ) const
nodiscard

the range of range() by the search of Manber and Myers, \( O( p + \log n ) \) comparisons of symbols

In pseudocode (the search of Manber and Myers, beyond the book), the virtual ranks \( -1 \) and \( n \) at the ends, \( P \) greater than the suffix of rank \( L \) and sharing \( l \) symbols with it, not greater than the suffix of rank \( R \) and sharing \( r \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{boundLcp}( P , \mathit{upper} ) \cr 2 & \quad L \gets -1;\ R \gets n;\ l \gets 0;\ r \gets 0 \cr 3 & \quad \textbf{while}\ R - L > 1\ \textbf{do} \cr 4 & \qquad M \gets \lfloor ( L + R ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ l \ge r\ \textbf{then} \cr 6 & \qquad\quad \textbf{if}\ \mathit{Llcp}[ M ] > l\ \textbf{then}\ ( h , g ) \gets ( l , \textbf{true} ) \cr 7 & \qquad\quad \textbf{else if}\ \mathit{Llcp}[ M ] < l\ \textbf{then} \ ( h , g ) \gets ( \mathit{Llcp}[ M ] , \textbf{false} ) \cr 8 & \qquad\quad \textbf{else}\ ( h , g ) \gets \text{the lcp of } P \text{ and the suffix } \mathit{SA}[ M ] \text{, compared from } l \text{,} \cr & \qquad\qquad \text{and whether } P \text{ is greater} \cr 9 & \qquad \textbf{else}\ \text{the same with } r \text{, } \mathit{Rlcp}[ M ] \text{ and the two outcomes swapped} \cr 10 & \qquad \textbf{if}\ g\ \textbf{then}\ L \gets M;\ l \gets h \ \textbf{else}\ R \gets M;\ r \gets h \cr 11 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the range of range( P ) (an audit)

◆ rangeLcp() [2/2]

std::pair< std::size_t, std::size_t > ADS::SuffixArray::rangeLcp ( std::string_view P,
long * comparisons = nullptr ) const
nodiscard

the range of rangeLcp() above, of the bytes of \( P \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rangeLcp}( P ) \cr 2 & \quad \textbf{return}\ \mathrm{rangeLcp}( \mathrm{toSymbols}( P ) ) \end{array}} \]

Postcondition
the range of range( P ) (an audit)

◆ rank()

std::size_t ADS::SuffixArray::rank ( std::size_t i) const
nodiscard

the rank of the suffix \( i \) in \( \mathit{SA} \)

Precondition
\( i < n \)

◆ size()

std::size_t ADS::SuffixArray::size ( void ) const
inlinenodiscard

the length \( n \) of the text

◆ text()

const Symbols & ADS::SuffixArray::text ( void ) const
inlinenodiscard

the text \( T \)


The documentation for this class was generated from the following file: