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

the automaton of Aho and Corasick of a set of patterns More...

#include <ADS/string/AhoCorasick.h>

Public member functions

 AhoCorasick (void)
 no pattern
 AhoCorasick (const std::vector< std::string > &P)
 the automaton of the patterns \( P \), numbered \( 0 , 1 , \ldots \) in their order, built
std::size_t add (std::string_view P)
 adds the pattern \( P \) to the trie, the automaton to be built again; its number
void build (void)
 the failures, the transitions and the output links, in \( O( 256 M ) \)
std::vector< std::pair< std::size_t, std::size_t > > findAll (std::string_view T) const
 the occurrences \( ( \mathit{start} , \mathit{number} ) \) of the patterns in \( T \), by increasing end, the longer pattern first for one end
std::size_t count (std::string_view T) const
 the number of the occurrences of the patterns in \( T \), by the scan of findAll()
template<class F>
void forEach (std::string_view T, F &&report) const
 each occurrence \( ( \mathit{start} , \mathit{number} ) \) of the patterns in \( T \) given to report(), by increasing end, the longer pattern first for one end
std::size_t size (void) const
 the number of patterns
std::size_t states (void) const
 the number of states, the nodes of the trie

Detailed description

the automaton of Aho and Corasick of a set of patterns

Constructor and destructor documentation

◆ AhoCorasick() [1/2]

ADS::AhoCorasick::AhoCorasick ( void )

no pattern

◆ AhoCorasick() [2/2]

ADS::AhoCorasick::AhoCorasick ( const std::vector< std::string > & P)
explicit

the automaton of the patterns \( P \), numbered \( 0 , 1 , \ldots \) in their order, built

Precondition
no pattern is empty

Member function documentation

◆ add()

std::size_t ADS::AhoCorasick::add ( std::string_view P)

adds the pattern \( P \) to the trie, the automaton to be built again; its number

In pseudocode (Aho and Corasick, beyond the book), the trie rebuilt from the patterns first if the automaton was built:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{add}( P ) \cr 2 & \quad u \gets \varepsilon\quad \triangleright\ \text{the root} \cr 3 & \quad \textbf{foreach}\ c \in P\ \textbf{do} \cr 4 & \qquad \textbf{if}\ u c \text{ is not in the trie }\ \textbf{then} \ \text{add the state } u c \text{ as a child of } u \cr 5 & \qquad u \gets u c \cr 6 & \quad \text{append the number } k \text{ of } P \text{ to the } \text{patterns of } u;\ k \gets k + 1 \cr 7 & \quad \textbf{return}\ k - 1 \end{array}} \]

Precondition
\( P \) is not empty

◆ build()

void ADS::AhoCorasick::build ( void )

the failures, the transitions and the output links, in \( O( 256 M ) \)

In pseudocode (Aho and Corasick, beyond the book), the states in breadth first order, so that the failure of a state and its transitions are complete before those of its children are computed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}() \cr 2 & \quad Q \gets \text{an empty queue} \cr 3 & \quad \textbf{foreach}\ c\ \textbf{do} \cr 4 & \qquad \textbf{if}\ c \text{ is in the trie}\ \textbf{then} \ \mathit{fail}( c ) \gets \varepsilon;\ \mathrm{enqueue}( Q , c ) \cr 5 & \qquad \textbf{else}\ \delta( \varepsilon , c ) \gets \varepsilon \cr 6 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 7 & \qquad u \gets \mathrm{dequeue}( Q );\ f \gets \mathit{fail}( u ) \cr 8 & \qquad \mathit{out}( u ) \gets f \text{ if } f \text{ is a pattern,} \text{ else } \mathit{out}( f ) \cr 9 & \qquad \textbf{foreach}\ c\ \textbf{do} \cr 10 & \qquad\quad \textbf{if}\ u c \text{ is in the trie}\ \textbf{then} \ \mathit{fail}( u c ) \gets \delta( f , c );\ \mathrm{enqueue}( Q , u c ) \cr 11 & \qquad\quad \textbf{else}\ \delta( u , c ) \gets \delta( f , c ) \end{array}} \]

◆ count()

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

the number of the occurrences of the patterns in \( T \), by the scan of findAll()

Precondition
built

◆ findAll()

std::vector< std::pair< std::size_t, std::size_t > > ADS::AhoCorasick::findAll ( std::string_view T) const
nodiscard

the occurrences \( ( \mathit{start} , \mathit{number} ) \) of the patterns in \( T \), by increasing end, the longer pattern first for one end

In pseudocode (Aho and Corasick, beyond the book), one transition per byte and one step of the output links per occurrence:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findAll}( T ) \cr 2 & \quad s \gets \varepsilon;\ R \gets \emptyset \cr 3 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad s \gets \delta( s , T[ j ] ) \cr 5 & \qquad x \gets s \text{ if } s \text{ is a pattern, else } \mathit{out}( s ) \cr 6 & \qquad \textbf{while}\ x \ne \varepsilon\ \textbf{do} \cr 7 & \qquad\quad \textbf{foreach}\ \text{pattern } P_i \text{ of } x \ \textbf{do}\ R \gets R \cup \{ ( j + 1 - |P_i| , i ) \} \cr 8 & \qquad\quad x \gets \mathit{out}( x ) \cr 9 & \quad \textbf{return}\ R \end{array}} \]

Precondition
built
Postcondition
the occurrences of naiveMatching() for each pattern (audit)

◆ forEach()

template<class F>
void ADS::AhoCorasick::forEach ( std::string_view T,
F && report ) const
inline

each occurrence \( ( \mathit{start} , \mathit{number} ) \) of the patterns in \( T \) given to report(), by increasing end, the longer pattern first for one end

Precondition
built

◆ size()

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

the number of patterns

◆ states()

std::size_t ADS::AhoCorasick::states ( void ) const
inlinenodiscard

the number of states, the nodes of the trie


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