Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Aho_Corasick Class Reference

Aho-Corasick multi-pattern automaton. More...

#include <Aho_Corasick.H>

Collaboration diagram for Aleph::Aho_Corasick:
[legend]

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
 

Detailed Description

Aho-Corasick multi-pattern automaton.

The automaton operates on bytes (unsigned char alphabet size 256). All matches (including overlaps) are reported.

Note
A growing 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.
Examples
aho_corasick_example.cc.

Definition at line 69 of file Aho_Corasick.H.

Constructor & Destructor Documentation

◆ Aho_Corasick() [1/3]

Aleph::Aho_Corasick::Aho_Corasick ( )
inline

Build an empty automaton (root only).

Definition at line 118 of file Aho_Corasick.H.

References nodes_.

◆ Aho_Corasick() [2/3]

Aleph::Aho_Corasick::Aho_Corasick ( const Aho_Corasick &  other)
default

Copy the patterns and the automaton of another one.

Parameters
[in]otherautomaton to copy.
Exceptions
std::bad_allocif the arrays cannot be allocated.

◆ Aho_Corasick() [3/3]

Aleph::Aho_Corasick::Aho_Corasick ( Aho_Corasick &&  other)
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.

Parameters
[in,out]otherautomaton to move from.
Exceptions
std::bad_allocif the root left to other cannot be allocated; other is then unchanged.
Note
Complexity: O(1), but it allocates the empty automaton left to other.

Definition at line 142 of file Aho_Corasick.H.

References swap().

Member Function Documentation

◆ add_pattern()

size_t Aleph::Aho_Corasick::add_pattern ( std::string  pattern)
inline

Add one pattern to the automaton.

Parameters
[in]patternPattern to insert (must be non-empty).
Returns
Pattern id to be used in match reporting.
Precondition
pattern is non-empty.
Exceptions
std::invalid_argumentIf pattern is empty.
Note
Calling this invalidates the previously built automaton; call build() again before search().
Examples
aho_corasick_example.cc.

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().

◆ build()

void Aleph::Aho_Corasick::build ( )
inline

Build failure links and transition completion.

Finalizes the automaton structure. Must be called after all add_pattern() calls and before invoking search().

Precondition
add_pattern() has been called for all desired patterns and no further modifications (no additional add_pattern() calls) have occurred since.
Complexity
O(sum(pattern lengths) * alphabet) with fixed alphabet 256.
Examples
aho_corasick_example.cc.

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().

◆ clear()

void Aleph::Aho_Corasick::clear ( )
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().

◆ contains_any()

bool Aleph::Aho_Corasick::contains_any ( const std::string_view  text) const
inline

Return true if at least one pattern appears in the text.

Parameters
[in]textText to scan.
Returns
true if any match is found, false otherwise.
Precondition
build() has been called since the last modification.
Exceptions
std::runtime_errorIf 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().

◆ create_node()

size_t Aleph::Aho_Corasick::create_node ( )
inlineprivate

Definition at line 110 of file Aho_Corasick.H.

References nodes_.

Referenced by add_pattern().

◆ is_built()

bool Aleph::Aho_Corasick::is_built ( ) const
inlinenoexcept

Return whether build() has been executed since last insertion.

Returns
true if the automaton is built, false otherwise.

Definition at line 384 of file Aho_Corasick.H.

References built_.

Referenced by Moved_From_Case< Aho_Corasick >::is_empty(), and TEST().

◆ operator=() [1/2]

Aho_Corasick & Aleph::Aho_Corasick::operator= ( Aho_Corasick &&  other)
inlinenoexcept

Exchange this automaton with another, which is left with the previous patterns of this one.

Parameters
[in,out]otherautomaton to move from.
Returns
this automaton.
Note
Complexity: O(1).

Definition at line 160 of file Aho_Corasick.H.

References swap().

◆ operator=() [2/2]

Aho_Corasick & Aleph::Aho_Corasick::operator= ( const Aho_Corasick &  other)
default

Copy the patterns and the automaton of another one.

Parameters
[in]otherautomaton to copy.
Returns
this automaton.
Exceptions
std::bad_allocif the arrays cannot be allocated.

◆ pattern()

const std::string & Aleph::Aho_Corasick::pattern ( const size_t  id) const
inline

Return the pattern text by id.

Parameters
[in]idPattern id.
Returns
Pattern string.
Precondition
id is a valid pattern id returned from add_pattern.
Exceptions
std::out_of_rangeIf id >= pattern_count().
Examples
aho_corasick_example.cc.

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().

◆ pattern_count()

size_t Aleph::Aho_Corasick::pattern_count ( ) const
inlinenoexcept

Return the number of inserted patterns.

Returns
Number of 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()

Array< Match > Aleph::Aho_Corasick::search ( const std::string_view  text) const
inline

Search all patterns in a text.

Parameters
[in]textText to scan.
Returns
Matches sorted by end-scan order.
Precondition
build() has been called since the last modification.
Exceptions
std::runtime_errorIf build() has not been called.
Complexity
O(text.size() + number_of_matches).
Examples
aho_corasick_example.cc.

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().

◆ swap()

void Aleph::Aho_Corasick::swap ( Aho_Corasick &  other)
inlinenoexcept

Exchange in O(1) the patterns and the automaton of this one and another.

Parameters
[in,out]otherautomaton 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=().

Member Data Documentation

◆ built_

bool Aleph::Aho_Corasick::built_ = false
private

Definition at line 108 of file Aho_Corasick.H.

Referenced by add_pattern(), build(), clear(), contains_any(), is_built(), search(), and swap().

◆ nodes_

Array<Node> Aleph::Aho_Corasick::nodes_
private

◆ patterns_

Array<std::string> Aleph::Aho_Corasick::patterns_
private

Definition at line 107 of file Aho_Corasick.H.

Referenced by add_pattern(), clear(), pattern(), pattern_count(), search(), and swap().


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