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

The search by prefix in a sorted dictionary of \( n \) strings, beyond the book. More...

Files

file  ADS/string/PrefixSearch.h
 The search by prefix in a sorted dictionary of \( n \) strings, beyond the book: the strings prefixed by \( P \) are consecutive, between the lexicographic position of \( P \) (the number of strings smaller than \( P \)) and that of \( P\# \), \( \# \) being a character larger than all the others, so that two lexicographic searches give their range.

Classes

class  ADS::BinnedSearch
 the search by interpolation of a sorted array of integers by bins More...
class  ADS::BinnedStringSearch
 the search by interpolation of a sorted array of strings by bins More...

Functions

std::size_t ADS::lexicographicPosition (const std::vector< std::string > &A, std::string_view Q)
 the number of strings of the sorted \( A \) smaller than \( Q \)
std::size_t ADS::prefixEnd (const std::vector< std::string > &A, std::string_view P)
 the number of strings of the sorted \( A \) smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by \( P \)
std::pair< std::size_t, std::size_t > ADS::prefixRange (const std::vector< std::string > &A, std::string_view P)
 the range \( [ l , r ) \) of the strings of the sorted \( A \) prefixed by \( P \)
std::size_t ADS::commonPrefixLength (std::string_view a, std::string_view b)
 the length of the longest common prefix of \( a \) and \( b \), by comparing them from their start

Detailed description

The search by prefix in a sorted dictionary of \( n \) strings, beyond the book.

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

Function documentation

◆ commonPrefixLength()

std::size_t ADS::commonPrefixLength ( std::string_view a,
std::string_view b )
inlinenodiscard

the length of the longest common prefix of \( a \) and \( b \), by comparing them from their start

◆ lexicographicPosition()

std::size_t ADS::lexicographicPosition ( const std::vector< std::string > & A,
std::string_view Q )
inlinenodiscard

the number of strings of the sorted \( A \) smaller than \( Q \)

In pseudocode (the binary search of chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book), \( [ \mathit{lo} , \mathit{hi} ) \) the places that may still hold the answer:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lexicographicPosition}( A , Q ) \cr 2 & \quad \mathit{lo} \gets 0;\ \mathit{hi} \gets n \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad c \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ A[ c ] < Q\ \textbf{then}\ \mathit{lo} \gets c + 1\ \textbf{else}\ \mathit{hi} \gets c \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]

Precondition
\( A \) is sorted (audit)
Postcondition
\( A[ r - 1 ] < Q \le A[ r ] \)

◆ prefixEnd()

std::size_t ADS::prefixEnd ( const std::vector< std::string > & A,
std::string_view P )
inlinenodiscard

the number of strings of the sorted \( A \) smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by \( P \)

In pseudocode (chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book), the binary search of lexicographicPosition() with another test:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prefixEnd}( A , P ) \cr 2 & \quad \mathit{lo} \gets 0;\ \mathit{hi} \gets n \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad c \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ A[ c ] < P \text{ or } P \text{ is a prefix of } A[ c ]\ \textbf{then}\ \mathit{lo} \gets c + 1\ \textbf{else} \ \mathit{hi} \gets c \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]

Precondition
\( A \) is sorted (audit)
Postcondition
the strings before \( r \) are smaller than \( P\# \), those from \( r \) on are not

◆ prefixRange()

std::pair< std::size_t, std::size_t > ADS::prefixRange ( const std::vector< std::string > & A,
std::string_view P )
inlinenodiscard

the range \( [ l , r ) \) of the strings of the sorted \( A \) prefixed by \( P \)

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

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prefixRange}( A , P ) \cr 2 & \quad \textbf{return}\ ( \mathrm{lexicographicPosition}( A , P ) , \mathrm{prefixEnd}( A , P ) ) \end{array}} \]

Precondition
\( A \) is sorted (audit)