-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaho_corasick.hpp
352 lines (309 loc) · 8.77 KB
/
aho_corasick.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef AHO_CORASICK_HPP
#define AHO_CORASICK_HPP
#include <vector>
#include <memory>
#include <algorithm>
#include <map>
#include <list>
#include <deque>
#include <assert.h>
/* Algoritm for simultaneous
* pattern matching in O(n + m),
* where n - text length, m - patterns length.
* A.V.Aho and M.Corasick. Efficient string matching.
* CACM, 18(6):333-340, June 1975.
*/
template <typename T>
class AhoCorasick
{
public:
typedef typename T::value_type value_type;
typedef T pattern_type;
/* User-space class to call match().
*/
class Matcher
{
Matcher(AhoCorasick& automation);
public:
Matcher(const Matcher&) = default;
~Matcher() = default;
Matcher& operator=(const Matcher&) = default;
/* find all matches in [begin, end)
* f - function(pattern_type, Iterator)
* that is called on every match.
* begin - iterator to begin
* end - iterator to end
*/
template <typename F, typename Iterator>
void match(F f, Iterator begin, const Iterator& end);
private:
AhoCorasick& m_automation;
friend class AhoCorasick<T>;
}; // class AhoCorasick::Matcher
private:
class State
{
public:
typedef typename std::map<value_type, std::unique_ptr<State> > container;
typedef typename container::iterator iterator;
public:
State();
State(value_type label, const State* parent);
State(const State&) = delete;
~State() = default;
State& operator=(const State&) = delete;
bool isRoot() const;
value_type label() const;
const State* parent() const;
State* next(value_type next_label);
template <typename F, typename Iterator>
bool applyPatterns(F& f, const Iterator& i) const;
private:
State* addNext(value_type label);
void setFailTransition(State* state);
void addPattern(const pattern_type* pattern);
value_type m_label;
const State* m_parent;
container m_next;
State* m_failTransition;
std::vector<const pattern_type*> m_patterns;
friend class AhoCorasick<T>;
}; // class AhoCorasick::State
public:
AhoCorasick();
AhoCorasick(const AhoCorasick&) = delete;
~AhoCorasick() = default;
AhoCorasick& operator=(const AhoCorasick&) = delete;
/* add pattern to match against.
*/
AhoCorasick& addPattern(const pattern_type& pattern);
/* get matcher that has match().
*/
Matcher matcher();
private:
template <typename Iterator>
State* updateChain(const Iterator& current,
const Iterator& end, State* state);
template <typename Iterator>
State* findState(const Iterator& current,
const Iterator& end, State* state);
void prepare();
size_t prepareRecurcive(State* state,
size_t lvl, size_t max_lvl);
void prepareState(State* state);
void failSuffix(const State* state,
std::deque<value_type>& suffix);
template <typename F, typename Iterator>
void match(F& f, Iterator begin, const Iterator& end);
private:
std::list<pattern_type> m_patterns;
State m_root;
bool m_prepaired;
}; // class AhoCorasick
template <typename T>
AhoCorasick<T>::Matcher::Matcher(AhoCorasick& automation)
: m_automation(automation)
{
m_automation.prepare();
}
template <typename T>
template <typename F, typename Iterator>
void AhoCorasick<T>::Matcher::match(F f, Iterator begin, const Iterator& end)
{
m_automation.match(f, begin, end);
}
template <typename T>
AhoCorasick<T>::State::State()
: m_parent(nullptr)
, m_failTransition(this)
{}
template <typename T>
AhoCorasick<T>::State::State(value_type label, const State* parent)
: m_label(label)
, m_parent(parent)
, m_failTransition(nullptr)
{}
template <typename T>
bool AhoCorasick<T>::State::isRoot() const
{
return m_parent == nullptr;
}
template <typename T>
typename AhoCorasick<T>::value_type
AhoCorasick<T>::State::label() const
{
return m_label;
}
template <typename T>
const typename AhoCorasick<T>::State*
AhoCorasick<T>::State::parent() const
{
return m_parent;
}
template <typename T>
typename AhoCorasick<T>::State*
AhoCorasick<T>::State::next(value_type next_label)
{
auto state = m_next.find(next_label);
if (state != std::end(m_next))
{
return state->second.get();
}
else
{
if (isRoot())
return this;
return m_failTransition->next(next_label);
}
}
template <typename T>
template <typename F, typename Iterator>
bool AhoCorasick<T>::State::applyPatterns(F& f, const Iterator& i) const
{
if (isRoot())
return false;
for (auto pattern : m_patterns)
if (f(*pattern, std::prev(i, pattern->size() - 1)))
return true;
return false;
}
template <typename T>
typename AhoCorasick<T>::State*
AhoCorasick<T>::State::addNext(AhoCorasick<T>::value_type label)
{
return m_next.emplace(label,
std::unique_ptr<State>(new State{label, this}))
.first->second.get();
}
template <typename T>
void AhoCorasick<T>::State::setFailTransition(State* state)
{
m_failTransition = state;
}
template <typename T>
void AhoCorasick<T>::State::addPattern(const AhoCorasick<T>::pattern_type* pattern)
{
m_patterns.push_back(pattern);
}
template <typename T>
AhoCorasick<T>::AhoCorasick()
: m_prepaired(false)
{}
template <typename T>
AhoCorasick<T>& AhoCorasick<T>::addPattern(const AhoCorasick<T>::pattern_type& pattern)
{
auto state = updateChain(std::begin(pattern), std::end(pattern), &m_root);
if (state->isRoot())
return *this;
state->addPattern(&(*m_patterns.emplace(std::end(m_patterns), pattern)));
m_prepaired = false;
return *this;
}
template <typename T>
typename AhoCorasick<T>::Matcher AhoCorasick<T>::matcher()
{
return Matcher(*this);
}
template <typename T>
template <typename Iterator>
typename AhoCorasick<T>::State*
AhoCorasick<T>::updateChain(const Iterator& current,
const Iterator& end, State* state)
{
if (current == end)
return state;
auto next = state->m_next.find(*current);
if (next == std::end(state->m_next))
return updateChain(std::next(current), end, state->addNext(*current));
else
return updateChain(std::next(current), end, next->second.get());
}
template <typename T>
template <typename Iterator>
typename AhoCorasick<T>::State*
AhoCorasick<T>::findState(const Iterator& current,
const Iterator& end, State* state)
{
if (current == end)
return state;
auto next = state->m_next.find(*current);
if (next != std::end(state->m_next))
return findState(std::next(current), end, next->second.get());
else
return nullptr;
}
template <typename T>
void AhoCorasick<T>::prepare()
{
if (m_prepaired)
return;
size_t max_lvl = 1;
while(prepareRecurcive(&m_root, 0, max_lvl) > 0)
++max_lvl;
m_prepaired = true;
}
template <typename T>
size_t AhoCorasick<T>::prepareRecurcive(State* state,
size_t lvl, size_t max_lvl)
{
if (lvl == max_lvl)
{
prepareState(state);
return 1;
}
size_t prepared = 0;
for (auto& next : state->m_next)
prepared += prepareRecurcive(next.second.get(), lvl + 1, max_lvl);
return prepared;
}
template <typename T>
void AhoCorasick<T>::prepareState(State* state)
{
if (state->isRoot())
return;
if (state->parent()->isRoot())
{
state->setFailTransition(&m_root);
return;
}
std::deque<value_type> suffix;
failSuffix(state, suffix);
while (!suffix.empty())
{
State* transition = findState(std::begin(suffix),
std::end(suffix), &m_root);
if (transition != nullptr)
{
state->setFailTransition(transition);
for (auto pattern : transition->m_patterns)
state->addPattern(pattern);
break;
}
suffix.pop_front();
}
if (suffix.empty())
state->setFailTransition(&m_root);
}
template <typename T>
void AhoCorasick<T>::failSuffix(const State* state,
std::deque<value_type>& suffix)
{
if (state->parent()->isRoot())
return;
suffix.emplace_front(state->label());
failSuffix(state->parent(), suffix);
}
template <typename T>
template <typename F, typename Iterator>
void AhoCorasick<T>::match(F& f, Iterator begin, const Iterator& end)
{
assert(m_prepaired);
State* state = &m_root;
for (; begin != end; ++begin)
{
state = state->next(*begin);
if (state->applyPatterns(f, begin))
break;
}
}
#endif // AHO_CORASICK_HPP