forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrienode.c
200 lines (148 loc) · 4.09 KB
/
trienode.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
/*
This is part of pyahocorasick Python module.
Trie implementation
Author : Wojciech Muła, wojciech_mula@poczta.onet.pl
WWW : http://0x80.pl
License : BSD-3-Clause (see LICENSE)
*/
#include "trienode.h"
static TrieNode*
trienode_new(const char eow) {
TrieNode* node = (TrieNode*)memory_alloc(sizeof(TrieNode));
if (node) {
node->output.integer = 0;
node->output.object = NULL;
node->fail = NULL;
node->n = 0;
node->eow = eow;
node->next = NULL;
}
return node;
}
static void
trienode_free(TrieNode* node) {
ASSERT(node);
if (node->n > 0) {
memory_free(node->next);
}
memory_free(node);
}
static TrieNode* PURE
trienode_get_next(TrieNode* node, const TRIE_LETTER_TYPE letter) {
unsigned i;
Pair* next;
ASSERT(node);
next = (Pair*)node->next;
for (i=0; i < node->n; i++)
if (next[i].letter == letter) {
return next[i].child;
}
return NULL;
}
static TristateResult
trienode_unset_next_pointer(TrieNode* node, TrieNode* child) {
unsigned i;
unsigned index;
Pair* next;
ASSERT(node);
for (i=0; i < node->n; i++) {
if (node->next[i].child == child) {
index = i;
goto found;
}
}
return FALSE;
found:
if (node->n == 1) {
// there is just one node
node->n = 0;
memory_free(node->next);
node->next = NULL;
return TRUE;
}
// there are more nodes, reallocation is needed
next = (Pair*)memory_alloc((node->n - 1) * sizeof(Pair));
if (UNLIKELY(next == NULL)) {
return MEMORY_ERROR;
}
for (i=0; i < index; i++) {
next[i] = node->next[i];
}
for (i=index + 1; i < node->n; i++) {
next[i - 1] = node->next[i];
}
memory_free(node->next);
node->next = next;
node->n -= 1;
return TRUE;
}
static TrieNode* PURE
trienode_get_ith_unsafe(TrieNode* node, size_t index) {
ASSERT(node);
return node->next[index].child;
}
static TRIE_LETTER_TYPE PURE
trieletter_get_ith_unsafe(TrieNode* node, size_t index) {
ASSERT(node);
return node->next[index].letter;
}
static TrieNode*
trienode_set_next(TrieNode* node, const TRIE_LETTER_TYPE letter, TrieNode* child) {
int n;
void* next;
ASSERT(node);
ASSERT(child);
ASSERT(trienode_get_next(node, letter) == NULL);
n = node->n;
next = (TrieNode**)memory_realloc(node->next, (n + 1) * (sizeof(Pair)));
if (next) {
node->next = next;
node->next[n].letter = letter;
node->next[n].child = child;
node->n += 1;
return child;
}
else
return NULL;
}
#ifdef DEBUG_LAYOUT
void trienode_dump_layout() {
#define field_size(TYPE, name) sizeof(((TYPE*)NULL)->name)
#define field_ofs(TYPE, name) offsetof(TYPE, name)
#define field_dump(TYPE, name) printf("- %-12s: %d %d\n", #name, field_size(TYPE, name), field_ofs(TYPE, name));
printf("TrieNode (size=%lu):\n", sizeof(TrieNode));
field_dump(TrieNode, output);
field_dump(TrieNode, fail);
field_dump(TrieNode, n);
field_dump(TrieNode, eow);
field_dump(TrieNode, next);
printf("Pair (size=%lu):\n", sizeof(Pair));
field_dump(Pair, letter);
field_dump(Pair, child);
#undef field_dump
#undef field_size
#undef field_ofs
}
#endif
UNUSED static void
trienode_dump_to_file(TrieNode* node, FILE* f) {
unsigned i;
ASSERT(node != NULL);
ASSERT(f != NULL);
if (node->n == 0)
fprintf(f, "leaf ");
fprintf(f, "node %p\n", node);
if (node->eow)
fprintf(f, "- eow [%p]\n", node->output.object);
fprintf(f, "- fail: %p\n", node->fail);
if (node->n > 0) {
if (node->next == NULL) {
fprintf(f, "- %d next: %p\n", node->n, node->next);
} else {
fprintf(f, "- %d next: [(%d; %p)", node->n, node->next[0].letter, node->next[0].child);
for (i=1; i < node->n; i++)
fprintf(f, ", (%d; %p)", node->next[i].letter, node->next[i].child);
fprintf(f, "]\n");
}
}
}