|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Aho-Corasick multi-pattern automaton. More...
#include <Aho_Corasick.H>
Classes | |
| struct | Match |
| One match occurrence in the searched text. More... | |
| struct | Node |
Public Member Functions | |
| Aho_Corasick () | |
| Build an empty automaton (root only). | |
| Aho_Corasick (const Aho_Corasick &other)=default | |
| Copy the patterns and the automaton of another one. | |
| Aho_Corasick (Aho_Corasick &&other) | |
| Take the patterns and the automaton of another one, which is left empty, with the root only, as built by default. | |
| Aho_Corasick & | operator= (const Aho_Corasick &other)=default |
| Copy the patterns and the automaton of another one. | |
| Aho_Corasick & | operator= (Aho_Corasick &&other) noexcept |
| Exchange this automaton with another, which is left with the previous patterns of this one. | |
| void | swap (Aho_Corasick &other) noexcept |
| Exchange in O(1) the patterns and the automaton of this one and another. | |
| void | clear () |
| Clear all patterns and reset to an empty automaton. | |
| size_t | add_pattern (std::string pattern) |
| Add one pattern to the automaton. | |
| void | build () |
| Build failure links and transition completion. | |
| Array< Match > | search (const std::string_view text) const |
| Search all patterns in a text. | |
| bool | contains_any (const std::string_view text) const |
| Return true if at least one pattern appears in the text. | |
| size_t | pattern_count () const noexcept |
| Return the number of inserted patterns. | |
| bool | is_built () const noexcept |
Return whether build() has been executed since last insertion. | |
| const std::string & | pattern (const size_t id) const |
| Return the pattern text by id. | |
Private Member Functions | |
| size_t | create_node () |
Private Attributes | |
| Array< Node > | nodes_ |
| Array< std::string > | patterns_ |
| bool | built_ = false |
Aho-Corasick multi-pattern automaton.
The automaton operates on bytes (unsigned char alphabet size 256). All matches (including overlaps) are reported.
std::vector copies these objects instead of moving them: their move constructor is not noexcept, because it leaves the source empty and usable, which allocates, and std::vector moves only what cannot throw. The containers of Aleph do not copy them to grow: Array moves its items, and DynList and DynArray never relocate them. Definition at line 69 of file Aho_Corasick.H.
|
inline |
Build an empty automaton (root only).
Definition at line 118 of file Aho_Corasick.H.
References nodes_.
|
default |
Copy the patterns and the automaton of another one.
| [in] | other | automaton to copy. |
| std::bad_alloc | if the arrays cannot be allocated. |
|
inline |
Take the patterns and the automaton of another one, which is left empty, with the root only, as built by default.
The implicit move took the nodes, the root among them, but copied the built flag: the moved-from automaton then said that it was built and read a root that it no longer had.
| [in,out] | other | automaton to move from. |
| std::bad_alloc | if the root left to other cannot be allocated; other is then unchanged. |
other. Definition at line 142 of file Aho_Corasick.H.
References swap().
|
inline |
Add one pattern to the automaton.
| [in] | pattern | Pattern to insert (must be non-empty). |
| std::invalid_argument | If pattern is empty. |
Definition at line 197 of file Aho_Corasick.H.
References ah_invalid_argument_if, Aleph::Array< T >::append(), built_, create_node(), nodes_, pattern(), patterns_, and Aleph::Array< T >::size().
Referenced by main(), Moved_From_Case< Aho_Corasick >::make(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inline |
Build failure links and transition completion.
Finalizes the automaton structure. Must be called after all add_pattern() calls and before invoking search().
add_pattern() has been called for all desired patterns and no further modifications (no additional add_pattern() calls) have occurred since.Definition at line 236 of file Aho_Corasick.H.
References Aleph::and, Aleph::Array< T >::append(), built_, Aleph::Array< T >::is_empty(), Aleph::next(), nodes_, and Aleph::Array< T >::size().
Referenced by main(), Moved_From_Case< Aho_Corasick >::make(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inline |
Clear all patterns and reset to an empty automaton.
Definition at line 178 of file Aho_Corasick.H.
References built_, Aleph::Array< T >::empty(), nodes_, and patterns_.
Referenced by TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inline |
Return true if at least one pattern appears in the text.
| [in] | text | Text to scan. |
| std::runtime_error | If build() has not been called. |
Definition at line 343 of file Aho_Corasick.H.
References ah_runtime_error_unless, Aleph::and, built_, Aleph::next(), nodes_, and output.
Referenced by TEST(), TEST(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inlineprivate |
|
inlinenoexcept |
Return whether build() has been executed since last insertion.
Definition at line 384 of file Aho_Corasick.H.
References built_.
Referenced by Moved_From_Case< Aho_Corasick >::is_empty(), and TEST().
|
inlinenoexcept |
Exchange this automaton with another, which is left with the previous patterns of this one.
| [in,out] | other | automaton to move from. |
Definition at line 160 of file Aho_Corasick.H.
References swap().
|
default |
Copy the patterns and the automaton of another one.
| [in] | other | automaton to copy. |
| std::bad_alloc | if the arrays cannot be allocated. |
|
inline |
Return the pattern text by id.
| [in] | id | Pattern id. |
| std::out_of_range | If id >= pattern_count(). |
Definition at line 397 of file Aho_Corasick.H.
References ah_out_of_range_error_if, patterns_, and Aleph::Array< T >::size().
Referenced by add_pattern(), main(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inlinenoexcept |
Return the number of inserted patterns.
Definition at line 375 of file Aho_Corasick.H.
References patterns_, and Aleph::Array< T >::size().
Referenced by Moved_From_Case< Aho_Corasick >::is_empty(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
Search all patterns in a text.
| [in] | text | Text to scan. |
| std::runtime_error | If build() has not been called. |
Definition at line 295 of file Aho_Corasick.H.
References ah_runtime_error_unless, Aleph::and, Aleph::Array< T >::append(), built_, k, Aleph::next(), nodes_, output, patterns_, and Aleph::Array< T >::size().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Aho_Corasick >::use().
|
inlinenoexcept |
Exchange in O(1) the patterns and the automaton of this one and another.
| [in,out] | other | automaton to exchange with. |
Definition at line 170 of file Aho_Corasick.H.
References built_, nodes_, patterns_, and Aleph::Array< T >::swap().
Referenced by Aho_Corasick(), and operator=().
|
private |
Definition at line 108 of file Aho_Corasick.H.
Referenced by add_pattern(), build(), clear(), contains_any(), is_built(), search(), and swap().
Definition at line 106 of file Aho_Corasick.H.
Referenced by Aho_Corasick(), add_pattern(), build(), clear(), contains_any(), create_node(), search(), and swap().
|
private |
Definition at line 107 of file Aho_Corasick.H.
Referenced by add_pattern(), clear(), pattern(), pattern_count(), search(), and swap().