44# ifndef AHO_CORASICK_H
45# define AHO_CORASICK_H
48# include <string_view>
139 <<
"Aho_Corasick::add_pattern(): pattern cannot be empty";
142 for (
const unsigned char c:
pattern)
157 nodes_[state].output.append(
id);
183 for (
size_t c = 0; c < 256; ++c)
186 const auto child =
static_cast<size_t>(
next_state);
188 nodes_[child].out_link = -1;
192 while (head < queue.
size())
194 const size_t state = queue[head++];
196 for (
size_t c = 0; c < 256; ++c)
202 const auto child =
static_cast<size_t>(
next_state);
237 <<
"Aho_Corasick::search(): call build() before search()";
242 for (
size_t i = 0; i <
text.size(); ++i)
244 const unsigned char c =
static_cast<unsigned char>(
text[i]);
247 state =
static_cast<size_t>(
nodes_[state].fail);
258 for (
size_t k = 0;
k <
output.size(); ++
k)
285 <<
"Aho_Corasick::contains_any(): call build() before search()";
288 for (
size_t i = 0; i <
text.size(); ++i)
290 const unsigned char c =
static_cast<unsigned char>(
text[i]);
293 state =
static_cast<size_t>(
nodes_[state].fail);
303 if (
nodes_[state].out_link != -1)
339 <<
"Aho_Corasick::pattern(): id out of range";
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Aho-Corasick multi-pattern automaton.
const std::string & pattern(const size_t id) const
Return the pattern text by id.
bool contains_any(const std::string_view text) const
Return true if at least one pattern appears in the text.
void clear()
Clear all patterns and reset to an empty automaton.
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.
Aho_Corasick()
Build an empty automaton (root only).
void build()
Build failure links and transition completion.
size_t add_pattern(std::string pattern)
Add one pattern to the automaton.
Array< Match > search(const std::string_view text) const
Search all patterns in a text.
Array< std::string > patterns_
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void next()
Advance all underlying iterators (bounds-checked).
One match occurrence in the searched text.
bool operator==(const Match &other) const noexcept
Equality operator for Match.
size_t pattern_id
Insertion-order ID of the pattern.
size_t position
0-based start index of the match.
std::array< int, 256 > next
Dynamic array container with automatic resizing.