44# ifndef AHO_CORASICK_H
45# define AHO_CORASICK_H
48# include <string_view>
172 nodes_.swap(other.nodes_);
174 std::swap(
built_, other.built_);
200 <<
"Aho_Corasick::add_pattern(): pattern cannot be empty";
203 for (
const unsigned char c:
pattern)
205 int next_state =
nodes_[state].next[c];
206 if (next_state == -1)
209 nodes_[state].next[c] =
static_cast<int>(new_state);
210 next_state =
static_cast<int>(new_state);
213 state =
static_cast<size_t>(next_state);
218 nodes_[state].output.append(
id);
244 for (
size_t c = 0; c < 256; ++c)
245 if (
const int next_state =
nodes_[0].
next[c]; next_state != -1)
247 const auto child =
static_cast<size_t>(next_state);
249 nodes_[child].out_link = -1;
253 while (head < queue.
size())
255 const size_t state = queue[head++];
257 for (
size_t c = 0; c < 256; ++c)
259 const int next_state =
nodes_[state].next[c];
260 if (next_state == -1)
263 const auto child =
static_cast<size_t>(next_state);
264 auto fail_to =
static_cast<size_t>(
nodes_[state].fail);
267 fail_to =
static_cast<size_t>(
nodes_[fail_to].fail);
269 if (
const int candidate =
nodes_[fail_to].
next[c]; candidate != -1)
270 fail_to =
static_cast<size_t>(candidate);
272 nodes_[child].fail =
static_cast<int>(fail_to);
275 nodes_[fail_to].out_link :
static_cast<int>(fail_to);
298 <<
"Aho_Corasick::search(): call build() before search()";
303 for (
size_t i = 0; i < text.size(); ++i)
305 const unsigned char c =
static_cast<unsigned char>(text[i]);
308 state =
static_cast<size_t>(
nodes_[state].fail);
310 if (
const int next_state =
nodes_[state].
next[c]; next_state != -1)
311 state =
static_cast<size_t>(next_state);
315 size_t out_state = state;
319 for (
size_t k = 0;
k <
output.size(); ++
k)
325 const int next_out =
nodes_[out_state].out_link;
328 out_state =
static_cast<size_t>(next_out);
346 <<
"Aho_Corasick::contains_any(): call build() before search()";
349 for (
size_t i = 0; i < text.size(); ++i)
351 const unsigned char c =
static_cast<unsigned char>(text[i]);
354 state =
static_cast<size_t>(
nodes_[state].fail);
356 if (
const int next_state =
nodes_[state].
next[c]; next_state != -1)
357 state =
static_cast<size_t>(next_state);
364 if (
nodes_[state].out_link != -1)
397 [[nodiscard]]
const std::string &
pattern(
const size_t id)
const
400 <<
"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.
void swap(Aho_Corasick &other) noexcept
Exchange in O(1) the patterns and the automaton of this one and another.
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.
Aho_Corasick & operator=(const Aho_Corasick &other)=default
Copy the patterns and the automaton of another one.
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.
Aho_Corasick(Aho_Corasick &&other)
Take the patterns and the automaton of another one, which is left empty, with the root only,...
Array< Match > search(const std::string_view text) const
Search all patterns in a text.
Aho_Corasick(const Aho_Corasick &other)=default
Copy the patterns and the automaton of another one.
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.
void swap(Array &s) noexcept
Swap this with s
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.
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.