|
ADS
Algorithms and Data Structures
|
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 | |
the automaton of Aho and Corasick of a set of patterns
| ADS::AhoCorasick::AhoCorasick | ( | void | ) |
no pattern
|
explicit |
the automaton of the patterns \( P \), numbered \( 0 , 1 , \ldots \) in their order, built
| 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}} \]
| 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}} \]
|
nodiscard |
the number of the occurrences of the patterns in \( T \), by the scan of findAll()
|
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}} \]
|
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
|
inlinenodiscard |
the number of patterns
|
inlinenodiscard |
the number of states, the nodes of the trie