forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.c
154 lines (119 loc) · 2.61 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
/*
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('\0', 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(letter, false);
if (child)
trienode_set_next(node, letter, child);
else
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 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++) {
TrieNode* child = node->next[i];
ASSERT(child);
if (trie_traverse_aux(child, 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*);
}