This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrie.c
235 lines (179 loc) · 5.27 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
#include <stdlib.h> /* for malloc, free */
#include <string.h> /* for memcmp, memmove */
#include "ruby.h"
// typdefs!
typedef enum { false = 0, true} bool;
typedef struct node {
char character;
VALUE value;
struct node * first_child;
struct node * next_sibling;
} trie_node;
static VALUE rb_cTrie;
// =========================
// = function declarations =
// =========================
//trie implementation
static trie_node * trie_node_for_key(trie_node * root, char * key, bool create_missing_nodes);
static trie_node * trie_sibling_for_char(trie_node * node, char ch);
static trie_node * trie_add_sibling_for_char(trie_node * node, char ch);
static trie_node * trie_new_node_with_char(char ch);
static trie_node * trie_new_node();
static void trie_traverse(trie_node * trie, void (*lambda_func)(void *));
static void free_trie(trie_node * trie);
// ========================
// = function definitions =
// ========================
// instance methods
static VALUE rb_trie_get_key(VALUE self, VALUE key) {
trie_node * root;
trie_node * node;
char * key_cstring;
//Check_Type(key, T_STRING);
key_cstring = StringValuePtr(key);
Data_Get_Struct(self, trie_node, root);
node = trie_node_for_key(root, key_cstring, false);
if (node == NULL) return Qnil;
return node->value;
}
static VALUE rb_trie_set_key_to_value(VALUE self, VALUE key, VALUE value) {
trie_node * root;
trie_node * node;
char * key_cstring;
//Check_Type(key, T_STRING);
key_cstring = StringValuePtr(key);
Data_Get_Struct(self, trie_node, root);
node = trie_node_for_key(root, key_cstring, true);
node->value = value;
return Qnil;
}
static VALUE rb_trie_undef_key(VALUE self, VALUE key) {
trie_node * root, * node, * prev, * next;
VALUE return_value;
char * key_cstring;
int steps;
int i;
//Check_Type(key, T_STRING);
key_cstring = StringValuePtr(key);
Data_Get_Struct(self, trie_node, root);
next = root;
node = NULL;
prev = NULL;
steps = strlen(key_cstring);
for (i = 0; i < steps; i++) {
if (next == NULL) return Qnil;
while(next->character != key_cstring[i]) {
if (next == NULL) return Qnil;
next = next->next_sibling;
}
prev = node;
node = next;
next = node->first_child;
}
return_value = node->value;
node->value = Qnil;
if (node->first_child == NULL) { //node has no children. we can delete it.
if (prev == NULL) {
//printf("should delete root");
} else if (prev->first_child == node) {
prev->first_child = node->next_sibling;
free(node);
} else if (prev->next_sibling == node) {
prev->next_sibling = node->next_sibling;
free(node);
}
}
return return_value;
}
// garbage collection and allocation
static void trie_mark_value(void * t) {
rb_gc_mark( ((trie_node *)t)->value );
}
static void rb_trie_mark(trie_node * t) {
trie_traverse(t, trie_mark_value);
}
static void rb_trie_free(trie_node * t) {
free_trie(t);
}
static VALUE rb_trie_allocate (VALUE klass) {
trie_node * t = trie_new_node();
return Data_Wrap_Struct(klass, rb_trie_mark, rb_trie_free, t);
}
// extension init
void Init_trie() {
rb_cTrie = rb_define_class("Trie", rb_cObject);
rb_define_alloc_func (rb_cTrie, rb_trie_allocate);
int arg_count = 0;
//rb_define_method(rb_cTrie, "inspect", rb_trie_inspect, arg_count);
arg_count = 1;
rb_define_method(rb_cTrie, "[]", rb_trie_get_key, arg_count);
rb_define_method(rb_cTrie, "delete", rb_trie_undef_key, arg_count);
arg_count = 2;
rb_define_method(rb_cTrie, "[]=", rb_trie_set_key_to_value, arg_count);
}
// =======================
// = trie implementation =
// =======================
static trie_node * trie_node_for_key(trie_node * root, char * key, bool create_missing_nodes) {
int steps, i;
trie_node * next, * node;
steps = strlen(key);
next = root;
for (i = 0; i < steps; i++) {
if (next == NULL) {
if (create_missing_nodes) {
node->first_child = trie_new_node();
next = node->first_child;
}
else return NULL;
}
node = trie_sibling_for_char(next, key[i]);
if (node == NULL) {
if (create_missing_nodes) {
node = trie_add_sibling_for_char(next, key[i]);
}
else return NULL;
}
next = node->first_child;
}
return node;
}
static trie_node * trie_sibling_for_char(trie_node * node, char ch) {
while(true) {
if (node == NULL) return NULL;
if (node->character == ch) return node;
node = node->next_sibling;
}
return node;
}
static trie_node * trie_add_sibling_for_char(trie_node * node, char ch) {
trie_node * current_next;
current_next = node->next_sibling;
node->next_sibling = trie_new_node_with_char(ch);
node->next_sibling->next_sibling = current_next;
return node->next_sibling;
}
static trie_node * trie_new_node_with_char(char ch) {
trie_node * trie;
trie = malloc(sizeof(trie_node));
trie->character = ch;
trie->value = Qnil;
trie->first_child = NULL;
trie->next_sibling = NULL;
return trie;
}
static trie_node * trie_new_node() {
return trie_new_node_with_char('s'); //insert most common starting letter here.
}
static void trie_traverse(trie_node * trie, void (* lambda_func)(void *)) {
if (trie->next_sibling != NULL) {
trie_traverse(trie->next_sibling, lambda_func);
}
if (trie->first_child != NULL) {
trie_traverse(trie->first_child, lambda_func);
}
lambda_func(trie);
}
static void free_trie(trie_node * trie) {
trie_traverse(trie, free);
}