-
Notifications
You must be signed in to change notification settings - Fork 21
/
trie.c
295 lines (253 loc) · 7.31 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
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
/* trie.c - simple implementation of a suffix tree
*
* Copyright (C) Navaneeth.K.N
*
* This is part of libvarnam. See LICENSE.txt for the license
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "util.h"
#include "trie.h"
#include "result-codes.h"
static struct trie *trie_new(const char *label, void *value)
{
struct trie *t = xmalloc(sizeof(struct trie));
if(t) {
strncpy(t->label, label, MAX_PATTERN_LENGTH);
t->root = 0;
t->value = value;
t->child = NULL;
t->next = NULL;
}
return t;
}
struct trie *trie_create()
{
struct trie *t;
t = trie_new( "", NULL );
assert( t );
t->root = 1;
return t;
}
/* inserts the supplied child at the end of children collection */
static void insert_child(struct trie *parent, struct trie *child)
{
assert(parent != NULL);
assert(child != NULL);
assert(child->next == NULL);
if(parent->child == NULL) {
parent->child = child;
}
else {
/* inserting child at the end of the list */
struct trie *t = parent->child;
while(t->next != NULL) {
t = t->next;
}
t->next = child;
}
}
static void rearrange_siblings(struct trie *parent, struct trie *new)
{
unsigned int first_item = 0;
struct trie *item = NULL, *prev = NULL, *next = NULL;
assert(parent != NULL);
assert(parent->child != NULL);
assert(new != NULL);
item = parent->child;
while(item != NULL)
{
next = item->next;
first_item = (strcmp(item->label, parent->child->label) == 0);
if(strcmp(item->label, new->label) != 0 && startswith(item->label, new->label))
{
/* making this item orphan and moving the orphaned node */
item->next = NULL;
if(first_item) {
/* this is the first item. So the only item refering this is the parent. */
/* cutting this reference and repointing the parent to current item's next */
parent->child = next;
}
else {
/* to make a non first item orphan, repoint the previous item to next item of this */
prev->next = next;
}
insert_child(new, item);
}
else {
prev = item;
}
item = next;
}
}
/* Search inside the immediate children of parent and returns */
/* the matching item. If no match found, parent will be returned. */
/**/
static struct trie *find_match(struct trie *parent, const char *lookup)
{
struct trie *item = NULL;
if(parent->child == NULL) {
return parent;
}
item = parent->child;
do {
if(strcmp(lookup, item->label) == 0) {
return item;
}
item = item->next;
} while(item != NULL);
return NULL;
}
/* iterate the tree begining from root and tries to find a node matches the label. */
/* matched item will be returned. If no match found, root will be returned. */
/* if the label already exist, NULL will be returned */
static struct trie *find_parent(struct trie *root, const char *label)
{
struct trie *item = NULL, *temp = NULL;
int charcount = 1;
char buffer[MAX_PATTERN_LENGTH];
const char *lbl;
item = root;
lbl = label;
/* iterating each character in the label and incrementally looking the children */
/* collection for the match */
/**/
while(*lbl != '\0')
{
substr(buffer, label, 1, charcount++);
temp = find_match(item, buffer);
if(temp) {
item = temp;
}
++lbl;
}
if(strcmp(item->label, label) == 0) {
/* this means item already exist */
return NULL;
}
return item;
}
/* add a new item to the children collection of parent. newly created node will be returned */
/* if parent is NULL, NULL will be returned */
/**/
struct trie *trie_add_child(struct trie *parent, const char *label, void *value)
{
struct trie *new = NULL, *p = NULL;
int should_rearrange = 0;
if(parent == NULL) return NULL;
/* Adding a new element into the trie
*
* (1) If parent has no children, add the new trie as a child and return.
* (2) when parent has children, iterate each character in the label and look for it in the children collection
* (3) if not found, add the new trie into the children list. else continue until we get a place to insert.
* (4) if item already exit without doing anything.
*/
p = find_parent(parent, label);
if(p == NULL) {
/* supplied label already exist. ignoring duplicates */
return parent;
}
if(p->child != NULL) {
/* this trie has another children. After inserting we may neeed to rearrange the siblings */
should_rearrange = 1;
}
new = trie_new(label, value);
if(new)
{
insert_child(p, new);
if(should_rearrange) {
rearrange_siblings(p, new);
}
}
return new;
}
static int iterate_trie_recursive(struct trie *t, itfunction function, unsigned int depth, void *userdata)
{
int rc;
while(t != NULL)
{
if(function(t, depth, userdata) != VARNAM_SUCCESS) {
return VARNAM_ERROR;
}
rc = iterate_trie_recursive(t->child, function, depth + 1, userdata);
if(rc != VARNAM_SUCCESS) {
return rc;
}
t = t->next;
}
return VARNAM_SUCCESS;
}
int trie_iterate(struct trie *t, itfunction function, void *userdata)
{
if(function == NULL) return VARNAM_ERROR;
return iterate_trie_recursive(t, function, 0, userdata);
}
unsigned int trie_free(struct trie *root, freefunction callback)
{
struct trie *current = NULL, *next = NULL;
unsigned int freecnt = 0;
current = root;
while(current != NULL)
{
freecnt += trie_free(current->child, callback) + 1;
next = current->next;
if( !current->root && callback != NULL ) {
callback( current->value );
}
xfree(current);
current = next;
}
return freecnt;
}
static unsigned int get_count(struct trie *t, unsigned int count)
{
while(t != NULL)
{
count = get_count(t->child, ++count);
t = t->next;
}
return count;
}
unsigned int trie_children_count(struct trie *root)
{
unsigned int count = get_count(root, 0);
if(count != 0)
count = count - 1; /* not considering root */
return count;
}
static struct trie *lookup(struct trie *root, const char *label)
{
struct trie *item = NULL, *temp = NULL;
int charcount = 1;
char buffer[MAX_PATTERN_LENGTH];
const char *lbl;
item = root;
lbl = label;
/* iterating each character in the label and incrementally looking the children */
/* collection for the match */
/**/
while(*lbl != '\0')
{
substr(buffer, label, 1, charcount++);
temp = find_match(item, buffer);
if(temp) {
item = temp;
}
++lbl;
}
if(strcmp(item->label, label) == 0) {
/* this means item already exist */
return item;
}
return NULL;
}
void *trie_lookup(struct trie *root, const char *label)
{
struct trie *item = NULL;
if(root == NULL) return NULL;
if((item = lookup(root, label)) != NULL) {
return item->value;
}
return NULL;
}