forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.c
231 lines (180 loc) · 5.17 KB
/
trie.c
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
/*
This is part of pyahocorasick Python module.
Trie implementation
Author : Wojciech Muła, [email protected]
WWW : http://0x80.pl
License : BSD-3-Clause (see LICENSE)
*/
#include "trie.h"
static TrieNode*
trie_add_word(Automaton* automaton, const TRIE_LETTER_TYPE* word, const size_t wordlen, bool* new_word) {
TrieNode* node;
TrieNode* child;
unsigned i;
if (automaton->kind == EMPTY) {
ASSERT(automaton->root == NULL);
automaton->root = trienode_new(false);
if (automaton->root == NULL)
return NULL;
}
node = automaton->root;
for (i=0; i < wordlen; i++) {
const TRIE_LETTER_TYPE letter = word[i];
child = trienode_get_next(node, letter);
if (child == NULL) {
child = trienode_new(false);
if (LIKELY(child != NULL)) {
if (UNLIKELY(trienode_set_next(node, letter, child) == NULL)) {
memory_free(child);
return NULL;
}
} else {
// Note: in case of memory error, the already allocate nodes
// are still reachable from the root and will be free
// upon automaton destruction.
return NULL;
}
}
node = child;
}
if (node->eow == false) {
node->eow = true;
*new_word = true;
automaton->count += 1;
}
else
*new_word = false;
automaton->kind = TRIE;
return node;
}
static PyObject*
trie_remove_word(Automaton* automaton, const TRIE_LETTER_TYPE* word, const size_t wordlen) {
PyObject* object;
TrieNode* node;
TrieNode* tmp;
TrieNode* last_multiway;
unsigned last_multiway_index;
unsigned i;
if (automaton->root == NULL) {
return NULL;
}
node = automaton->root;
last_multiway = node;
last_multiway_index = 0;
for (i=0; i < wordlen; i++) {
const TRIE_LETTER_TYPE letter = word[i];
node = trienode_get_next(node, letter);
if (node == NULL) {
return NULL;
}
// Save the last node along path which has more children
// or is a terminating node.
if (node->n > 1 || (node->n == 1 && node->eow)) {
last_multiway = node;
last_multiway_index = i + 1;
}
}
if (node->eow != true) {
return NULL;
}
object = node->output.object;
if (trienode_is_leaf(node)) {
// Remove a linear list that starts at the last_multiway node
// and ends at the last [found] one.
// 1. Unlink the tail from the trie
node = trienode_get_next(last_multiway, word[last_multiway_index]);
ASSERT(node != NULL);
if (UNLIKELY(trienode_unset_next_pointer(last_multiway, node) == MEMORY_ERROR)) {
PyErr_NoMemory();
return NULL;
}
// 2. Free the tail (reference to value from the last element was already saved)
for (i = last_multiway_index + 1; i < wordlen; i++) {
tmp = trienode_get_next(node, word[i]);
ASSERT(tmp->n <= 1);
trienode_free(node);
node = tmp;
}
trienode_free(node);
} else {
// just unmark the terminating node
node->eow = false;
}
automaton->kind = TRIE;
return object;
}
static TrieNode* PURE
trie_find(TrieNode* root, const TRIE_LETTER_TYPE* word, const size_t wordlen) {
TrieNode* node;
size_t i;
node = root;
if (node != NULL) {
for (i=0; i < wordlen; i++) {
node = trienode_get_next(node, word[i]);
if (node == NULL)
return NULL;
}
}
return node;
}
static int PURE
trie_longest(TrieNode* root, const TRIE_LETTER_TYPE* word, const size_t wordlen) {
TrieNode* node;
int len = 0;
size_t i;
node = root;
for (i=0; i < wordlen; i++) {
node = trienode_get_next(node, word[i]);
if (node == NULL)
break;
else
len += 1;
}
return len;
}
static TrieNode* PURE
ahocorasick_next(TrieNode* node, TrieNode* root, const TRIE_LETTER_TYPE letter) {
TrieNode* next = node;
TrieNode* tmp;
while (next) {
tmp = trienode_get_next(next, letter);
if (tmp)
// found link
return tmp;
else
// or go back through fail edges
next = next->fail;
}
// or return root node
return root;
}
static int
trie_traverse_aux(
TrieNode* node,
const int depth,
trie_traverse_callback callback,
void *extra
) {
unsigned i;
if (callback(node, depth, extra) == 0)
return 0;
for (i=0; i < node->n; i++) {
if (trie_traverse_aux(trienode_get_ith_unsafe(node, i), depth + 1, callback, extra) == 0)
return 0;
}
return 1;
}
static void
trie_traverse(
TrieNode* root,
trie_traverse_callback callback,
void *extra
) {
ASSERT(root);
ASSERT(callback);
trie_traverse_aux(root, 0, callback, extra);
}
size_t PURE
trienode_get_size(const TrieNode* node) {
return sizeof(TrieNode) + node->n * sizeof(TrieNode*);
}