the search of many patterns at once, by a SIMD filter or by the automaton of Aho and Corasick
More...
#include <ADS/string/MultiSearch.h>
|
| | MultiSearcher (const std::vector< std::string > &P) |
| | the patterns \( P \), numbered \( 0 , 1 , \ldots \) in their order, and the filter that suits them
|
| 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 start and number
|
| std::size_t | count (std::string_view T) const |
| | the number of the occurrences of the patterns in \( T \), by the search of findAll()
|
| std::size_t | automatonFrom (std::string_view T) const |
| | the place of \( T \) from which the automaton takes over the search of findAll() and count(), std::string_view::npos if the filter searches all of \( T \) (or there are fewer than two patterns)
|
| std::size_t | size (void) const |
| | the number of patterns
|
| bool | filtered (void) const |
| | whether the filter of shuffles searches (else the shift-or filter)
|
|
| static constexpr std::size_t | kFilter = 64 |
| | the most patterns of the filter of shuffles, beyond which the shift-or filter searches
|
the search of many patterns at once, by a SIMD filter or by the automaton of Aho and Corasick
◆ MultiSearcher()
| ADS::MultiSearcher::MultiSearcher |
( |
const std::vector< std::string > & | P | ) |
|
|
explicit |
the patterns \( P \), numbered \( 0 , 1 , \ldots \) in their order, and the filter that suits them
In pseudocode (the filters of FDR in Hyperscan, beyond the book), the patterns sorted and cut into 8 buckets of consecutive ones:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{MultiSearcher}( P ) \cr
2 & \quad \text{sort } P \text{ and put it into 8 buckets of }
\text{consecutive patterns} \cr
3 & \quad \textbf{if}\ k > \mathit{kFilter}\ \textbf{then} \cr
4 & \qquad L \gets \min\{ 8 , \ell \} \cr
5 & \qquad \textbf{foreach}\ \text{pattern } p \text{ of the bucket } b
\text{ and } r < L\ \textbf{do} \cr
6 & \qquad\quad \text{clear the bit } b \text{ of the byte } L - 1 - r
\text{ of } \mathit{sh}[ x ] \text{ for each super character } x
\text{ that agrees with } p \text{ at } r \cr
7 & \qquad \textbf{if}\ \ell \ge 8\ \textbf{then}\ \text{build the masks }
\text{of stride 2, kept if a window passes with probability below }
2^{-8} \cr
8 & \qquad \text{put the prefixes of } L \text{ bytes of the patterns in }
\text{a chained hash table} \cr
9 & \quad \textbf{else} \cr
10 & \qquad \textbf{foreach}\ \text{pattern } p \text{ of the bucket } b
\text{ and } r < \min\{ 8 , \ell \}\ \textbf{do} \cr
11 & \qquad\quad \text{set the bit } b \text{ of } \mathit{lo}_r[ p[ r ]
\bmod 16 ] \text{ and of } \mathit{hi}_r[ \lfloor p[ r ] / 16
\rfloor ] \cr
12 & \qquad q \gets \text{the fewest bytes whose filter lets a place }
\text{pass with probability below } 2^{-10} \cr
13 & \quad \text{keep the first 8 bytes of each pattern for the checks;}
\text{ build the automaton of } P
\end{array}}
\]
- Precondition
- no pattern is empty
◆ automatonFrom()
| std::size_t ADS::MultiSearcher::automatonFrom |
( |
std::string_view | T | ) |
const |
|
nodiscard |
the place of \( T \) from which the automaton takes over the search of findAll() and count(), std::string_view::npos if the filter searches all of \( T \) (or there are fewer than two patterns)
◆ count()
| std::size_t ADS::MultiSearcher::count |
( |
std::string_view | T | ) |
const |
|
nodiscard |
the number of the occurrences of the patterns in \( T \), by the search of findAll()
◆ filtered()
| bool ADS::MultiSearcher::filtered |
( |
void | | ) |
const |
|
inlinenodiscard |
whether the filter of shuffles searches (else the shift-or filter)
◆ findAll()
| std::vector< std::pair< std::size_t, std::size_t > > ADS::MultiSearcher::findAll |
( |
std::string_view | T | ) |
const |
|
nodiscard |
the occurrences \( ( \mathit{start} , \mathit{number} ) \) of the patterns in \( T \), by increasing start and number
In pseudocode (the filters of FDR in Hyperscan, beyond the book), the candidates of a filter checked against the patterns of their buckets, and the automaton of Aho and Corasick taking over when the bytes compared are too many, so that the search is linear in \( n \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{findAll}( T ) \cr
2 & \quad \textbf{if}\ k = 1\ \textbf{then return}\ \text{the }
\text{occurrences of } P_0 \text{ by } \mathrm{StringSearcher} \cr
3 & \quad R \gets \emptyset;\ \mathit{bytes} \gets 0 \cr
4 & \quad \textbf{foreach}\ \text{block of places } i \text{ of } T
\ \textbf{do} \cr
5 & \qquad \textbf{foreach}\ i \text{ and bucket } b \text{ that pass }
\text{the filter } ( C_i \text{ or } \mathit{St}_{i + L - 1} )
\ \textbf{do} \cr
6 & \qquad\quad \textbf{foreach}\ \text{pattern } P_j \text{ of } b
\ \textbf{with}\ T[ i .. i + |P_j| - 1 ] = P_j\ \textbf{do}\ R \gets R
\cup \{ ( i , j ) \} \cr
7 & \qquad\quad \mathit{bytes} \gets \mathit{bytes} + \text{the }
\text{bytes compared with the patterns} \cr
8 & \qquad \textbf{if}\ \mathit{bytes} > 16 i + 4096\ \textbf{then}
\cr
9 & \qquad\quad R \gets R \cup \text{the occurrences that the automaton }
\text{finds from } i \text{ on};\ \textbf{break} \cr
10 & \quad \textbf{return}\ R \text{ sorted}
\end{array}}
\]
- Postcondition
- the occurrences of naiveMatching() for each pattern (audit)
◆ size()
| std::size_t ADS::MultiSearcher::size |
( |
void | | ) |
const |
|
inlinenodiscard |
◆ kFilter
| std::size_t ADS::MultiSearcher::kFilter = 64 |
|
staticconstexpr |
the most patterns of the filter of shuffles, beyond which the shift-or filter searches
The documentation for this class was generated from the following file: