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

a pattern prepared for the search of its occurrences in any text More...

#include <ADS/string/StringSearch.h>

Public member functions

 StringSearcher (void)
 the empty pattern
 StringSearcher (std::string_view P)
 the pattern \( P \) prepared, its probes and the shifts of its q-grams, in \( O( 256 + m ) \)
std::size_t find (std::string_view T, std::size_t from=0) const
 the first occurrence of \( P \) at or after from in \( T \), npos if none
std::vector< std::size_t > findAll (std::string_view T) const
 all the occurrences of \( P \) in \( T \), in increasing order: the search of find(), which goes on after each occurrence
std::size_t count (std::string_view T) const
 the number of occurrences of \( P \) in \( T \), by the search of findAll()
template<std::contiguous_iterator I>
requires ( sizeof( std::iter_value_t< I > ) == 1 )
std::pair< I, I > operator() (I first, I last) const
 the range of the first occurrence of \( P \) in \( [ \mathit{first} , \mathit{last} ) \), or \( ( \mathit{last} , \mathit{last} ) \): the searcher of std::search()
std::string_view pattern (void) const
 the pattern \( P \)

Static public attributes

static constexpr std::size_t npos = std::string_view::npos
 no place: the result of a search that finds no occurrence
static constexpr std::size_t kSkip = 64
 the length from which a pattern is searched by the shifts of its q-grams instead of the filter of the probes

Detailed description

a pattern prepared for the search of its occurrences in any text

Constructor and destructor documentation

◆ StringSearcher() [1/2]

ADS::StringSearcher::StringSearcher ( void )
inline

the empty pattern

◆ StringSearcher() [2/2]

ADS::StringSearcher::StringSearcher ( std::string_view P)
explicit

the pattern \( P \) prepared, its probes and the shifts of its q-grams, in \( O( 256 + m ) \)

In pseudocode (the probes of Faro and Kulekci and the q-grams of Lecroq, beyond the book), \( r( c ) \) the rank of the byte \( c \) by its frequency in text and \( d \) the number of distinct bytes of \( P \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{StringSearcher}( P ) \cr 2 & \quad \textbf{if}\ m < 2\ \textbf{then return} \cr 3 & \quad \textbf{foreach}\ c\ \textbf{do}\ \mathit{last}[ c ] \gets \text{the last place of } c \text{ in } P \cr 4 & \quad k \gets 4 \text{ if } m \ge 4 \text{ and } ( d < 8 \le m \text{ or } d < m ) \text{, else } 2 \cr 5 & \quad ( b_i , o_i )_{i = 1}^{k} \gets \text{the } k \text{ distinct } \text{bytes of } P \text{ of least } r( c ) \text{, at } o_i = \mathit{last}[ b_i ] \cr 6 & \quad \text{complete them with other places of } P \text{ of least } \text{rank, if } d < k \cr 7 & \quad \textbf{if}\ m < \mathit{kSkip}\ \textbf{then return} \cr 8 & \quad q \gets \min\{ q \ge 2 : d^q \ge 16 m \text{ or } q = 8 \} \cr 9 & \quad \textbf{foreach}\ x \in [ 0 , 4096 )\ \textbf{do} \ \mathit{shift}[ x ] \gets \min\{ m - q + 1 , 255 \} \cr 10 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - q - 1\ \textbf{do} \ \mathit{shift}[ h( P[ i .. i + q - 1 ] ) ] \gets \min\{ m - q - i , 255 \} \cr 11 & \quad x \gets h( P[ m - q .. m - 1 ] );\ \mathit{shift}_0 \gets \mathit{shift}[ x ];\ \mathit{shift}[ x ] \gets 0 \end{array}} \]

Member function documentation

◆ count()

std::size_t ADS::StringSearcher::count ( std::string_view T) const
nodiscard

the number of occurrences of \( P \) in \( T \), by the search of findAll()

◆ find()

std::size_t ADS::StringSearcher::find ( std::string_view T,
std::size_t from = 0 ) const
nodiscard

the first occurrence of \( P \) at or after from in \( T \), npos if none

In pseudocode (beyond the book), the windows that the filter or the shifts let through compared with \( P \), and the Two-Way algorithm [see twoWay() of StringMatching.h] taking over when the bytes compared exceed \( 16 ( s - \mathit{from} ) + 4096 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( T , \mathit{from} ) \cr 2 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ \mathit{from} \cr 3 & \quad \textbf{if}\ m = 1\ \textbf{then return}\ \mathrm{memchr}( T , P[ 0 ] , \mathit{from} ) \cr 4 & \quad s \gets \mathit{from};\ \mathit{work} \gets 0 \cr 5 & \quad \textbf{while}\ s \le n - m\ \textbf{do} \cr 6 & \qquad \textbf{if}\ m \ge \mathit{kSkip}\ \textbf{and}\ \text{the } \text{shifts move fast enough}\ \textbf{then} \cr 7 & \qquad\quad \delta \gets \mathit{shift}[ h( T[ s + m - q .. s + m - 1 ] ) ] \cr 8 & \qquad\quad \textbf{if}\ \delta > 0\ \textbf{then}\ s \gets s + \delta;\ \textbf{continue} \cr 9 & \qquad \textbf{else}\ s \gets \text{the next start with } T[ s + o_i ] = b_i \text{ for every } i\quad \triangleright\ \text{the probes} \cr 10 & \qquad \textbf{if}\ T[ s .. s + m - 1 ] = P\ \textbf{then return} \ s \cr 11 & \qquad \mathit{work} \gets \mathit{work} + m \cr 12 & \qquad \textbf{if}\ \mathit{work} > 16 ( s - \mathit{from} ) + 4096\ \textbf{then return}\ \mathrm{twoWay}( T , s + 1 ) \cr 13 & \qquad s \gets s + \mathit{shift}_0 \text{ (with the shifts), or } s + 1 \cr 14 & \quad \textbf{return}\ \mathit{npos} \end{array}} \]

Postcondition
npos, or an occurrence at or after from

◆ findAll()

std::vector< std::size_t > ADS::StringSearcher::findAll ( std::string_view T) const
nodiscard

all the occurrences of \( P \) in \( T \), in increasing order: the search of find(), which goes on after each occurrence

Postcondition
the occurrences of naiveMatching( T , P ) (audit)

◆ operator()()

template<std::contiguous_iterator I>
requires ( sizeof( std::iter_value_t< I > ) == 1 )
std::pair< I, I > ADS::StringSearcher::operator() ( I first,
I last ) const
inlinenodiscard

the range of the first occurrence of \( P \) in \( [ \mathit{first} , \mathit{last} ) \), or \( ( \mathit{last} , \mathit{last} ) \): the searcher of std::search()

◆ pattern()

std::string_view ADS::StringSearcher::pattern ( void ) const
inlinenodiscard

the pattern \( P \)

Member data documentation

◆ kSkip

std::size_t ADS::StringSearcher::kSkip = 64
staticconstexpr

the length from which a pattern is searched by the shifts of its q-grams instead of the filter of the probes

◆ npos

std::size_t ADS::StringSearcher::npos = std::string_view::npos
staticconstexpr

no place: the result of a search that finds no occurrence


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