-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.c
227 lines (196 loc) · 5 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
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include "trie.h"
#include "key.h"
#include "util.h"
gpointer _trie_remove (Trie *t, trie_key_t key, gconstpointer bucket_key);
Trie *_trie_retrieve_trie (Trie *t, trie_key_t key);
TrieBucket *_trie_retrieve_bucket (Trie *t, trie_key_t key);
TrieBucket *_trie_make_bucket (Trie *t, trie_key_t key);
gboolean _node_collect (Trie *t, gpointer plist)
{
GList *l = *((GList**) plist);
GList *bucket_items = g_hash_table_get_values(trie_node_bucket(t));
/* ( (key1 item1 item2 ...) (key2 item1 item2 ...) ...) */
l = g_list_prepend(l, g_list_prepend(bucket_items, TO_SP(trie_node_key(t))));
*((GList**) plist) = l;
return FALSE;
}
void trie_bucket_destroy(TrieBucket *tb);
gboolean _trie_node_destroy (Trie *t, gpointer UNUSED)
{
trie_bucket_destroy(((NodeData*)t->data)->items);
g_free(t->data);
return FALSE;
}
void trie_destroy (Trie *t)
{
g_node_traverse (t, G_IN_ORDER, G_TRAVERSE_ALL,
-1,/*depth. unlimited */
_trie_node_destroy,NULL);
g_node_destroy(t);
}
/* returns the children of the Trie in the form
((key (bucket_list)) ...)
where each key is actually a key part
*/
GList *trie_children (Trie *t)
{
GList *l = NULL;
g_node_traverse(t, G_IN_ORDER, G_TRAVERSE_ALL, -1, _node_collect, &l);
return l;
}
void trie_bucket_insert (TrieBucket *tb, gpointer key, gpointer object)
{
g_hash_table_insert(tb, key, object);
}
TrieBucket *new_trie_bucket ()
{
return (TrieBucket*) g_hash_table_new(g_direct_hash, g_direct_equal);
}
gpointer trie_bucket_lookup (TrieBucket *tb, gpointer key)
{
return g_hash_table_lookup(tb, key);
}
gpointer trie_bucket_remove (TrieBucket *tb, gpointer key)
{
gpointer res = g_hash_table_lookup(tb, key);
g_hash_table_remove(tb, key);
return res;
}
Trie *new_trie0 (bucket_id_t key, TrieBucket *items)
{
NodeData *nd = malloc(sizeof(NodeData));
nd->key = key;
if (!items)
nd->items = new_trie_bucket();
else
nd->items = items;
return (Trie*) g_node_new((gpointer) nd);
}
Trie *new_trie()
{
Trie *res = new_trie0(0, NULL);
return res;
}
GList *trie_bucket_as_list (TrieBucket *tb)
{
if (tb)
{
return g_hash_table_get_values((GHashTable*)tb);
}
else
{
return NULL;
}
}
gpointer trie_retrieve (Trie *t, trie_key_t key, gconstpointer bucket_key)
{
TrieBucket *tb = trie_retrieve_bucket(t, key);
if (tb == NULL)
{
return NULL;
}
return trie_bucket_lookup(tb, (gpointer)bucket_key);
}
void trie_insert (Trie *t, trie_key_t key, gconstpointer bucket_key, gpointer object)
{
TrieBucket *tb = _trie_make_bucket(t, key);
trie_bucket_insert(tb, (gpointer)bucket_key, object);
}
gpointer trie_remove (Trie *t, trie_key_t key, gconstpointer bucket_key)
{
return _trie_remove(t, key, bucket_key);
}
gpointer _trie_remove (Trie *t, trie_key_t key, gconstpointer bucket_key)
{
TrieBucket *tb = _trie_retrieve_bucket(t, key);
return trie_bucket_remove(tb, (gpointer)bucket_key);
}
GList *trie_retrieve_bucket_l (Trie *t, trie_key_t key)
{
return trie_bucket_as_list(trie_retrieve_bucket(t, key));
}
TrieBucket *trie_retrieve_bucket (Trie *t, trie_key_t key)
{
if (key == NULL)
return trie_node_bucket(t);
return _trie_retrieve_bucket(t, key);
}
TrieBucket *_trie_retrieve_bucket (Trie *t, trie_key_t key)
{
Trie *my_trie = trie_retrieve_trie(t, key);
if (my_trie)
return trie_node_bucket(my_trie);
return NULL;
}
Trie *trie_retrieve_trie (Trie *t, trie_key_t key)
{
if (key == NULL)
{
return t;
}
return _trie_retrieve_trie(t, key);
}
Trie *_trie_retrieve_trie (Trie *t, trie_key_t key)
{
if (!t)
{
return NULL;
}
if (key_is_empty(key))
{
return t;
}
Trie *child = g_node_first_child(t);
KL(key,i)
{
while ((child != NULL)
&&(key_ref(key,i) != trie_node_key(child)))
{
child = child->next;
}
if (child == NULL)
{
// the key doesn't correspond to any node
return NULL;
}
t = g_node_first_child(t);
child = g_node_first_child(t);
}
return t;
}
TrieBucket *trie_make_bucket (Trie *t, trie_key_t key)
{
return _trie_make_bucket(t, key);
}
TrieBucket *_trie_make_bucket (Trie *t, trie_key_t key)
{
if (!t)
{
return NULL;
}
Trie *child = g_node_first_child(t);
KL(key,i)
{
while ((child != NULL)
&&(key_ref(key,i) != trie_node_key(child)))
{
child = child->next;
}
if (child == NULL)
{
// the key doesn't correspond to any node
Trie *nt = new_trie0(key_ref(key,i), NULL);
g_node_prepend(t, nt);
}
t = g_node_first_child(t);
child = g_node_first_child(t);
}
return trie_node_bucket(t);
}
void trie_bucket_destroy(TrieBucket *tb)
{
g_hash_table_destroy(tb);
}