|
ADS
Algorithms and Data Structures
|
The exact search of a pattern \( P \) of \( m \) bytes in a text \( T \) of \( n \) bytes, beyond the book: the engineered search of ADS, the one that the benchmark of the searches compares with memmem() of the C library, std::search() with its three searchers and the searchers of Boost.Algorithm, while kmp() [see StringMatching.h] stays the algorithm of the textbook. More...
#include "ADS/Contract.h"#include "ADS/SimdInline.h"#include "ADS/memory/Prefetch.h"#include "ADS/string/StringMatching.h"#include <cstddef>#include <array>#include <cstdint>#include <iterator>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/string/impl/StringSearch.tpp"Go to the source code of this file.
Classes | |
| class | ADS::StringSearcher |
| a pattern prepared for the search of its occurrences in any text More... | |
Namespaces | |
| namespace | ADS |
Macros | |
| #define | ADS_SEARCH_SIMD ADS_SIMD |
| the vector comparisons of the filter: 1 by the vectors of Highway, 32 bytes at a time, at the instructions of the compiler [see SimdInline.h], 0 none (memchr() and bytes); by default ADS_SIMD [see Simd.h], and the same in all the translation units of a program | |
Functions | |
| std::size_t | ADS::findFirst (std::string_view T, std::string_view P, std::size_t from=0) |
| the first occurrence of \( P \) in \( T \) at or after from, npos if none, by a StringSearcher used once | |
| std::vector< std::size_t > | ADS::findAll (std::string_view T, std::string_view P) |
| all the occurrences of \( P \) in \( T \), overlapping ones included, by a StringSearcher used once | |
The exact search of a pattern \( P \) of \( m \) bytes in a text \( T \) of \( n \) bytes, beyond the book: the engineered search of ADS, the one that the benchmark of the searches compares with memmem() of the C library, std::search() with its three searchers and the searchers of Boost.Algorithm, while kmp() [see StringMatching.h] stays the algorithm of the textbook.
The occurrences are the places
\[ \mathrm{Occ}( P , T ) = \{ s \in [ 0 , n - m ] : T[ s .. s + m - 1 ] = P \} . \]
StringSearcher prepares \( P \) once, as the searchers of the standard library do, and then finds its occurrences in any text:
\[ \mathit{shift}[ x ] = \min\bigl\{ 255 , m - q + 1 , \min\{ m - q - i : 0 \le i < m - q , \; h( P[ i .. i + q - 1 ] ) = x \} \bigr\} , \]
0 for the hash of the last q-gram of \( P \); the window at \( s \) moves to \( s + \mathit{shift}[ h( T[ s + m - q .. s + m - 1 ] ) ] \), and a window of shift 0 is compared with \( P \) and moves by the shift that the last q-gram of \( P \) has among the others. A shift never passes an occurrence, since it is at most the distance to the last place where the q-gram at the end of the window occurs in \( P \). On random texts a window moves by about \( m - q + 1 \), i.e., \( O( n / m ) \) windows of one load of 8 bytes; the text 1024 bytes ahead is prefetched, and the four quarters of a text of 16 KB or more are searched at once, their windows in registers, so that the chains of loads of the four overlap (the occurrences of a quarter wait for the end of the ones before it). When 64 steps have moved the first window by less than \( 16 m \) (a text of few distinct q-grams), the filter of the probes takes over;Preparing \( P \) takes \( O( 256 + m ) \) time (the last place of each byte gives the probes) and 4 KB for the shifts; find() gives the first occurrence at or after from (npos if none), findAll() all of them, overlapping ones included, in increasing order, count() their number, and operator() the range of the first occurrence in \( [ \mathit{first} , \mathit{last} ) \), the interface of the searchers of std::search(). The empty pattern occurs at every place \( 0 , \ldots , n \). The functions findFirst() and findAll() of the namespace prepare \( P \) and search once.
The contracts [see Contract.h]: an occurrence found is one, and findAll() gives those of naiveMatching() [see StringMatching.h] (an audit, \( O( n m ) \) time). The functions are defined in StringSearch.tpp.