AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore::AhoCorasick< CharT > Class Template Reference

Multi-pattern substring matcher. More...

#include "AhoCorasick.h"

Classes

struct  Node
 

Public Types

using StringType = std::basic_string< CharT >
 
using StringViewType = std::basic_string_view< CharT >
 

Public Member Functions

 AhoCorasick ()
 
void Insert (StringViewType pattern)
 
void Build ()
 
bool ContainsAny (StringViewType text) const
 
bool Empty () const
 
void Clear ()
 

Private Attributes

std::vector< Node_nodes
 

Detailed Description

template<typename CharT>
class Acore::AhoCorasick< CharT >

Multi-pattern substring matcher.

Insert all patterns, call Build() once, then query in O(text length) regardless of pattern count. Insertion after Build() is not supported (it would invalidate failure links — call Build() again if you must).

Matching is exact on the CharT alphabet — pre-lowercase patterns and inputs in the caller if you need case-insensitive matching.

Usage: Acore::AhoCorasick<wchar_t> a; a.Insert(L"foo"); a.Insert(L"bar"); a.Build(); bool hit = a.ContainsAny(L"the bar is open"); // -> true

Member Typedef Documentation

◆ StringType

template<typename CharT >
using Acore::AhoCorasick< CharT >::StringType = std::basic_string<CharT>

◆ StringViewType

template<typename CharT >
using Acore::AhoCorasick< CharT >::StringViewType = std::basic_string_view<CharT>

Constructor & Destructor Documentation

◆ AhoCorasick()

template<typename CharT >
Acore::AhoCorasick< CharT >::AhoCorasick ( )
inline
56{ _nodes.emplace_back(); }
std::vector< Node > _nodes
Definition AhoCorasick.h:152

References Acore::AhoCorasick< CharT >::_nodes.

Member Function Documentation

◆ Build()

template<typename CharT >
void Acore::AhoCorasick< CharT >::Build ( )
inline
82 {
83 std::queue<uint32> bfs;
84 for (auto const& kv : _nodes[0].next)
85 {
86 _nodes[kv.second].fail = 0;
87 bfs.push(kv.second);
88 }
89
90 while (!bfs.empty())
91 {
92 uint32 u = bfs.front();
93 bfs.pop();
94
95 for (auto const& kv : _nodes[u].next)
96 {
97 CharT c = kv.first;
98 uint32 v = kv.second;
99
100 uint32 f = _nodes[u].fail;
101 while (f != 0 && _nodes[f].next.find(c) == _nodes[f].next.end())
102 f = _nodes[f].fail;
103
104 auto it = _nodes[f].next.find(c);
105 _nodes[v].fail = (it != _nodes[f].next.end() && it->second != v) ? it->second : 0;
106
107 if (_nodes[_nodes[v].fail].output)
108 _nodes[v].output = true;
109
110 bfs.push(v);
111 }
112 }
113 }
std::uint32_t uint32
Definition Define.h:107

References Acore::AhoCorasick< CharT >::_nodes.

◆ Clear()

template<typename CharT >
void Acore::AhoCorasick< CharT >::Clear ( )
inline
139 {
140 _nodes.clear();
141 _nodes.emplace_back();
142 }

References Acore::AhoCorasick< CharT >::_nodes.

◆ ContainsAny()

template<typename CharT >
bool Acore::AhoCorasick< CharT >::ContainsAny ( StringViewType  text) const
inline
116 {
117 if (_nodes.size() <= 1)
118 return false;
119
120 uint32 cur = 0;
121 for (CharT c : text)
122 {
123 while (cur != 0 && _nodes[cur].next.find(c) == _nodes[cur].next.end())
124 cur = _nodes[cur].fail;
125
126 auto it = _nodes[cur].next.find(c);
127 if (it != _nodes[cur].next.end())
128 cur = it->second;
129
130 if (_nodes[cur].output)
131 return true;
132 }
133 return false;
134 }

References Acore::AhoCorasick< CharT >::_nodes.

◆ Empty()

template<typename CharT >
bool Acore::AhoCorasick< CharT >::Empty ( ) const
inline
136{ return _nodes.size() <= 1; }

References Acore::AhoCorasick< CharT >::_nodes.

◆ Insert()

template<typename CharT >
void Acore::AhoCorasick< CharT >::Insert ( StringViewType  pattern)
inline
59 {
60 if (pattern.empty())
61 return;
62
63 uint32 cur = 0;
64 for (CharT c : pattern)
65 {
66 auto it = _nodes[cur].next.find(c);
67 if (it != _nodes[cur].next.end())
68 {
69 cur = it->second;
70 continue;
71 }
72
73 uint32 idx = static_cast<uint32>(_nodes.size());
74 _nodes.emplace_back();
75 _nodes[cur].next.emplace(c, idx);
76 cur = idx;
77 }
78 _nodes[cur].output = true;
79 }

References Acore::AhoCorasick< CharT >::_nodes.

Member Data Documentation

◆ _nodes


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