tyler-trie 0.2.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
+ /*
3
+ * trie-private.h - Private utilities for trie implementation
4
+ * Created: 2007-08-25
5
+ * Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
6
+ */
7
+
8
+ #ifndef __TRIE_PRIVATE_H
9
+ #define __TRIE_PRIVATE_H
10
+
11
+ #include <datrie/typedefs.h>
12
+
13
+ /**
14
+ * @file trie-private.h
15
+ * @brief Private utilities for trie implementation
16
+ */
17
+
18
+ /**
19
+ * @brief Minimum value macro
20
+ */
21
+ #define MIN_VAL(a,b) ((a)<(b)?(a):(b))
22
+ /**
23
+ * @brief Maximum value macro
24
+ */
25
+ #define MAX_VAL(a,b) ((a)>(b)?(a):(b))
26
+
27
+ #endif /* __TRIE_PRIVATE_H */
28
+
29
+ /*
30
+ vi:ts=4:ai:expandtab
31
+ */
data/ext/trie/trie.c CHANGED
@@ -1,393 +1,298 @@
1
1
  #include "ruby.h"
2
- #include <datrie/sb-trie.h>
2
+ #include "trie.h"
3
3
  #include <stdlib.h>
4
4
  #include <stdio.h>
5
5
  #include <string.h>
6
6
 
7
7
  VALUE cTrie, cTrieNode;
8
8
 
9
- static TrieChar* stringToTrieChar(VALUE string) {
10
- StringValue(string);
11
- return (TrieChar*) RSTRING(string)->ptr;
9
+ static VALUE rb_trie_alloc(VALUE klass) {
10
+ VALUE obj;
11
+ obj = Data_Wrap_Struct(klass, 0, trie_free, trie_new());
12
+ return obj;
12
13
  }
13
14
 
14
- static void trie_free(SBTrie *sb_trie) {
15
- if(sb_trie)
16
- sb_trie_close(sb_trie);
17
- }
18
-
19
- static VALUE trie_alloc(VALUE klass) {
20
- SBTrie *sb_trie;
21
- VALUE obj;
22
-
23
- obj = Data_Wrap_Struct(klass, 0, trie_free, sb_trie);
24
- rb_iv_set(obj, "@open", Qfalse);
25
-
26
- return obj;
27
- }
28
-
29
- static VALUE trie_initialize(VALUE self, VALUE path) {
30
- SBTrie *sb_trie;
31
-
32
- char *cpath = RSTRING(path)->ptr;
33
- char *full_path = (char*)malloc(strlen(cpath) + 10);
34
- sprintf(full_path, "%s/trie.sbm", cpath);
35
-
36
- FILE *file;
37
-
38
- file = fopen (full_path, "r");
39
- if (!file) {
40
- file = fopen (full_path, "w+");
41
- fprintf(file,"[00,FF]\n");
42
- }
43
- fclose(file);
44
- free (full_path);
45
-
46
- // replace the pretend SBTrie created in alloc with a real one
47
- RDATA(self)->data = sb_trie_open(cpath, "trie",
48
- TRIE_IO_READ | TRIE_IO_WRITE | TRIE_IO_CREATE);
49
-
50
- rb_iv_set(self, "@open", Qtrue);
51
- rb_iv_set(self, "@path", path);
52
- return self;
53
- }
54
-
55
- static VALUE trie_close(VALUE self) {
56
- SBTrie *sb_trie;
57
- Data_Get_Struct(self, SBTrie, sb_trie);
15
+ static VALUE rb_trie_has_key(VALUE self, VALUE key) {
16
+ Trie *trie;
17
+ Data_Get_Struct(self, Trie, trie);
58
18
 
59
- rb_iv_set(self, "@open", Qfalse);
60
-
61
- return self;
62
- }
63
-
64
- static VALUE trie_has_key(VALUE self, VALUE key) {
65
- SBTrie *sb_trie;
66
- Data_Get_Struct(self, SBTrie, sb_trie);
67
-
68
- const TrieChar *sb_key = (const TrieChar *)RSTRING(key)->ptr;
69
-
70
- if(sb_trie_retrieve(sb_trie, sb_key, NULL))
71
- return Qtrue;
19
+ if(trie_retrieve(trie, (TrieChar*)RSTRING(key)->ptr, NULL))
20
+ return Qtrue;
72
21
  else
73
- return Qnil;
22
+ return Qnil;
74
23
  }
75
24
 
76
- static VALUE trie_get(VALUE self, VALUE key) {
77
- SBTrie *sb_trie;
78
- TrieData trie_data;
79
- Data_Get_Struct(self, SBTrie, sb_trie);
80
-
81
- const TrieChar *sb_key = stringToTrieChar(key);
25
+ static VALUE rb_trie_get(VALUE self, VALUE key) {
26
+ Trie *trie;
27
+ Data_Get_Struct(self, Trie, trie);
82
28
 
83
- if(sb_trie_retrieve(sb_trie, sb_key, &trie_data)) {
84
- return INT2FIX(trie_data);
85
- } else
86
- return Qnil;
29
+ TrieData data;
30
+ if(trie_retrieve(trie, (TrieChar*)RSTRING(key)->ptr, &data))
31
+ return INT2FIX(data);
32
+ else
33
+ return Qnil;
87
34
  }
88
35
 
89
- static VALUE trie_add(VALUE self, VALUE args) {
90
- SBTrie *sb_trie;
91
- Data_Get_Struct(self, SBTrie, sb_trie);
36
+ static VALUE rb_trie_add(VALUE self, VALUE args) {
37
+ Trie *trie;
38
+ Data_Get_Struct(self, Trie, trie);
92
39
 
93
- long size = RARRAY(args)->len;
40
+ int size = RARRAY(args)->len;
94
41
  if(size < 1 || size > 2)
95
- return Qnil;
42
+ return Qnil;
96
43
 
97
44
  VALUE key;
98
45
  key = RARRAY(args)->ptr[0];
99
46
  TrieData value = size == 2 ? NUM2INT(RARRAY(args)->ptr[1]) : TRIE_DATA_ERROR;
100
47
 
101
- const TrieChar *sb_key = stringToTrieChar(key);
102
-
103
- if(sb_trie_store(sb_trie, sb_key, value))
104
- return Qtrue;
48
+ if(trie_store(trie, (TrieChar*)RSTRING(key)->ptr, value))
49
+ return Qtrue;
105
50
  else
106
- return Qnil;
51
+ return Qnil;
107
52
  }
108
53
 
109
- static VALUE trie_delete(VALUE self, VALUE key) {
110
- SBTrie *sb_trie;
111
- Data_Get_Struct(self, SBTrie, sb_trie);
112
-
113
- const TrieChar *sb_key = stringToTrieChar(key);
54
+ static VALUE rb_trie_delete(VALUE self, VALUE key) {
55
+ Trie *trie;
56
+ Data_Get_Struct(self, Trie, trie);
114
57
 
115
- if(sb_trie_delete(sb_trie, sb_key))
116
- return Qtrue;
58
+ if(trie_delete(trie, (TrieChar*)RSTRING(key)->ptr))
59
+ return Qtrue;
117
60
  else
118
- return Qnil;
61
+ return Qnil;
119
62
  }
120
63
 
121
- static VALUE trie_save(VALUE self) {
122
- SBTrie *sb_trie;
123
- Data_Get_Struct(self, SBTrie, sb_trie);
124
-
125
- if(sb_trie_save(sb_trie) == -1)
126
- return Qnil;
127
- else
128
- return Qtrue;
64
+ char* append_char(char* existing, int size, char c) {
65
+ char *new = (char*) malloc(size + 2);
66
+ memcpy(new, existing, size);
67
+ new[size] = c;
68
+ new[size + 1] = 0;
69
+ return new;
129
70
  }
130
71
 
131
- static VALUE walk_all_paths(VALUE children, SBTrieState *state, char *prefix) {
132
- int c;
133
- for(c = 1; c < TRIE_CHAR_MAX; c++) {
134
- if(sb_trie_state_is_walkable(state,c)) {
135
- SBTrieState *next_state = sb_trie_state_clone(state);
136
- sb_trie_state_walk(next_state, (TrieChar)c);
137
-
138
- char *word = (char*)malloc(strlen(prefix) + 2);
139
- strcat(strcpy(word, prefix), (char*)&c);
140
-
141
- if(sb_trie_state_is_terminal(next_state))
142
- rb_ary_push(children, rb_str_new2(word));
143
-
144
- walk_all_paths(children, next_state, word);
145
-
146
- sb_trie_state_free(next_state);
147
- }
72
+ static VALUE walk_all_paths(Trie *trie, VALUE children, TrieState *state, char *prefix, int prefix_size) {
73
+ int c;
74
+ for(c = 1; c < 256; c++) {
75
+ if(trie_state_is_walkable(state,c)) {
76
+ TrieState *next_state = trie_state_clone(state);
77
+ trie_state_walk(next_state, c);
78
+
79
+ prefix[prefix_size] = c;
80
+ prefix[prefix_size + 1] = 0;
81
+
82
+ if(trie_state_is_terminal(next_state)) {
83
+ char *word = (char*) malloc(prefix_size + 2);
84
+ memcpy(word, prefix, prefix_size + 2);
85
+ rb_ary_push(children, rb_str_new2(word));
86
+ }
87
+
88
+ walk_all_paths(trie, children, next_state, prefix, prefix_size + 1);
89
+
90
+ prefix[prefix_size] = 0;
91
+ trie_state_free(next_state);
92
+ }
148
93
  }
149
94
  }
150
95
 
151
- static VALUE trie_children(VALUE self, VALUE prefix) {
96
+ static VALUE rb_trie_children(VALUE self, VALUE prefix) {
152
97
  if(NIL_P(prefix))
153
- return rb_ary_new();
98
+ return rb_ary_new();
154
99
 
155
- SBTrie *sb_trie;
156
- Data_Get_Struct(self, SBTrie, sb_trie);
100
+ Trie *trie;
101
+ Data_Get_Struct(self, Trie, trie);
157
102
 
158
- const TrieChar *sb_prefix = stringToTrieChar(prefix);
159
-
103
+ int prefix_size = RSTRING(prefix)->len;
104
+ TrieState *state = trie_root(trie);
160
105
  VALUE children = rb_ary_new();
161
-
162
- SBTrieState *state = sb_trie_root(sb_trie);
106
+ TrieChar *char_prefix = (TrieChar*)RSTRING(prefix)->ptr;
163
107
 
164
- TrieChar *iterator = (TrieChar*)sb_prefix;
165
- while(*iterator != '\0') {
166
- if(!sb_trie_state_is_walkable(state, *iterator))
167
- return rb_ary_new();
168
- sb_trie_state_walk(state, *iterator);
169
- iterator++;
108
+ const TrieChar *iterator = char_prefix;
109
+ while(*iterator != 0) {
110
+ if(!trie_state_is_walkable(state, *iterator))
111
+ return children;
112
+ trie_state_walk(state, *iterator);
113
+ iterator++;
170
114
  }
171
115
 
172
- if(sb_trie_state_is_terminal(state))
173
- rb_ary_push(children, prefix);
116
+ if(trie_state_is_terminal(state))
117
+ rb_ary_push(children, prefix);
118
+
119
+ char prefix_buffer[1024];
120
+ memcpy(prefix_buffer, char_prefix, prefix_size);
121
+ prefix_buffer[prefix_size] = 0;
174
122
 
175
- walk_all_paths(children, state, (char*)sb_prefix);
123
+ walk_all_paths(trie, children, state, prefix_buffer, prefix_size);
176
124
 
177
- sb_trie_state_free(state);
125
+ trie_state_free(state);
178
126
  return children;
179
127
  }
180
- static VALUE walk_all_paths_with_values(VALUE children, SBTrieState *state, char *prefix) {
181
- int c;
182
- for(c = 1; c < TRIE_CHAR_MAX; c++) {
183
- if(sb_trie_state_is_walkable(state,c)) {
184
- SBTrieState *next_state = sb_trie_state_clone(state);
185
- sb_trie_state_walk(next_state, (TrieChar)c);
186
128
 
187
- char *word = (char*)malloc(strlen(prefix) + 2);
188
- strcat(strcpy(word, prefix), (char*)&c);
189
129
 
190
- if(sb_trie_state_is_terminal(next_state)) {
191
- SBTrieState *end_state = sb_trie_state_clone(next_state);
192
- sb_trie_state_walk(end_state, '\0');
130
+ static VALUE walk_all_paths_with_values(Trie *trie, VALUE children, TrieState *state, char *prefix, int prefix_size) {
131
+ int c;
132
+ for(c = 1; c < 256; c++) {
133
+ if(trie_state_is_walkable(state,c)) {
134
+ TrieState *next_state = trie_state_clone(state);
135
+ trie_state_walk(next_state, c);
193
136
 
194
- VALUE tuple = rb_ary_new();
195
- rb_ary_push(tuple, rb_str_new2(word));
196
- TrieData trie_data = sb_trie_state_get_data(end_state);
197
- rb_ary_push(tuple, INT2FIX(trie_data));
198
- rb_ary_push(children, tuple);
137
+ prefix[prefix_size] = c;
138
+ prefix[prefix_size + 1] = 0;
199
139
 
200
- sb_trie_state_free(end_state);
201
- }
140
+ if(trie_state_is_terminal(next_state)) {
141
+ TrieState *end_state = trie_state_clone(next_state);
142
+ trie_state_walk(end_state, '\0');
143
+
144
+ char *word = (char*) malloc(prefix_size + 2);
145
+ memcpy(word, prefix, prefix_size + 2);
202
146
 
203
- walk_all_paths_with_values(children, next_state, word);
147
+ VALUE tuple = rb_ary_new();
148
+ rb_ary_push(tuple, rb_str_new2(word));
204
149
 
205
- sb_trie_state_free(next_state);
206
- }
150
+ TrieData trie_data = trie_state_get_data(end_state);
151
+ rb_ary_push(tuple, INT2FIX(trie_data));
152
+ rb_ary_push(children, tuple);
153
+
154
+ trie_state_free(end_state);
155
+ }
156
+
157
+ walk_all_paths_with_values(trie, children, next_state, prefix, prefix_size + 1);
158
+
159
+ prefix[prefix_size] = 0;
160
+ trie_state_free(next_state);
161
+ }
207
162
  }
208
163
  }
209
164
 
210
- static VALUE trie_children_with_values(VALUE self, VALUE prefix) {
165
+
166
+
167
+
168
+ static VALUE rb_trie_children_with_values(VALUE self, VALUE prefix) {
211
169
  if(NIL_P(prefix))
212
- return rb_ary_new();
170
+ return rb_ary_new();
213
171
 
214
- SBTrie *sb_trie;
215
- Data_Get_Struct(self, SBTrie, sb_trie);
172
+ Trie *trie;
173
+ Data_Get_Struct(self, Trie, trie);
216
174
 
217
- const TrieChar *sb_prefix = stringToTrieChar(prefix);
175
+ int prefix_size = RSTRING(prefix)->len;
176
+ TrieChar *char_prefix = (TrieChar*)RSTRING(prefix)->ptr;
218
177
 
219
178
  VALUE children = rb_ary_new();
220
179
 
221
- SBTrieState *state = sb_trie_root(sb_trie);
180
+ TrieState *state = trie_root(trie);
222
181
 
223
- TrieChar *iterator = (TrieChar*)sb_prefix;
224
- while(*iterator != '\0') {
225
- if(!sb_trie_state_is_walkable(state, *iterator))
226
- return rb_ary_new();
227
- sb_trie_state_walk(state, *iterator);
228
- iterator++;
229
- }
230
-
231
- if(sb_trie_state_is_terminal(state)) {
232
- SBTrieState *end_state = sb_trie_state_clone(state);
233
- sb_trie_state_walk(end_state, '\0');
234
-
235
- VALUE tuple = rb_ary_new();
236
- rb_ary_push(tuple, prefix);
237
- TrieData trie_data = sb_trie_state_get_data(end_state);
238
- rb_ary_push(tuple, INT2FIX(trie_data));
239
- rb_ary_push(children, tuple);
240
-
241
- sb_trie_state_free(end_state);
182
+ const TrieChar *iterator = char_prefix;
183
+ while(*iterator != 0) {
184
+ if(!trie_state_is_walkable(state, *iterator))
185
+ return rb_ary_new();
186
+ trie_state_walk(state, *iterator);
187
+ iterator++;
242
188
  }
243
189
 
244
- walk_all_paths_with_values(children, state, (char*)sb_prefix);
245
-
246
- sb_trie_state_free(state);
247
- return children;
248
- }
249
-
250
- static VALUE trie_walk_to_terminal(VALUE self, VALUE args) {
251
- SBTrie *sb_trie;
252
- Data_Get_Struct(self, SBTrie, sb_trie);
190
+ if(trie_state_is_terminal(state)) {
191
+ TrieState *end_state = trie_state_clone(state);
192
+ trie_state_walk(end_state, '\0');
253
193
 
254
- long size = RARRAY(args)->len;
255
- if(size < 1 || size > 2)
256
- return Qnil;
194
+ VALUE tuple = rb_ary_new();
195
+ rb_ary_push(tuple, prefix);
196
+ TrieData trie_data = trie_state_get_data(end_state);
197
+ rb_ary_push(tuple, INT2FIX(trie_data));
198
+ rb_ary_push(children, tuple);
257
199
 
258
- VALUE rb_path;
259
- rb_path = RARRAY(args)->ptr[0];
260
- short include_value = size == 2 ? RTEST(RARRAY(args)->ptr[1]) : 0;
261
-
262
- SBTrieState *state = sb_trie_root(sb_trie);
263
-
264
- char *path = RSTRING(rb_path)->ptr;
265
-
266
- TrieChar *iterator = (TrieChar*)path;
267
- while(*iterator != '\0') {
268
- if(sb_trie_state_is_terminal(state)) {
269
- int word_length = (int)iterator - (int)path;
270
- char *word = (char*)malloc(word_length + 1);
271
- strncpy(word, path, word_length);
272
- word[word_length] = '\0';
273
- VALUE rb_word = rb_str_new2((const char*)word);
274
-
275
- if(include_value) {
276
- sb_trie_state_walk(state, (TrieChar)'\0');
277
-
278
- VALUE return_ary = rb_ary_new();
279
- rb_ary_push(return_ary, rb_word);
280
- TrieData trie_data = sb_trie_state_get_data(state);
281
- rb_ary_push(return_ary, INT2FIX(trie_data));
282
- return return_ary;
283
- } else
284
- return rb_word;
285
- }
286
-
287
- if(!sb_trie_state_is_walkable(state, *iterator))
288
- return Qnil;
289
-
290
- sb_trie_state_walk(state, *iterator);
291
- iterator++;
200
+ trie_state_free(end_state);
292
201
  }
293
202
 
294
- sb_trie_state_free(state);
203
+ char prefix_buffer[1024];
204
+ memcpy(prefix_buffer, char_prefix, prefix_size);
205
+ prefix_buffer[prefix_size] = 0;
295
206
 
296
- return Qnil;
297
- }
298
-
299
- static VALUE trie_get_path(VALUE self) {
300
- return rb_iv_get(self, "@path");
301
- }
207
+ walk_all_paths_with_values(trie, children, state, prefix_buffer, prefix_size);
302
208
 
303
- static void trie_node_free(SBTrieState *state) {
304
- if(state)
305
- sb_trie_state_free(state);
209
+ trie_state_free(state);
210
+ return children;
306
211
  }
307
212
 
308
- static VALUE trie_node_alloc(VALUE klass) {
309
- SBTrieState *state;
213
+ static VALUE rb_trie_node_alloc(VALUE klass) {
310
214
  VALUE obj;
311
-
312
- obj = Data_Wrap_Struct(klass, 0, trie_node_free, state);
313
-
215
+ obj = Data_Wrap_Struct(klass, 0, trie_state_free, NULL);
314
216
  return obj;
315
217
  }
316
218
 
317
- static VALUE trie_root(VALUE self) {
318
- SBTrie *sb_trie;
319
- Data_Get_Struct(self, SBTrie, sb_trie);
219
+ static VALUE rb_trie_root(VALUE self) {
220
+ Trie *trie;
221
+ Data_Get_Struct(self, Trie, trie);
320
222
 
321
- VALUE trie_node = trie_node_alloc(cTrieNode);
223
+ VALUE trie_node = rb_trie_node_alloc(cTrieNode);
322
224
 
323
- // replace the pretend SBTrieState created in TrieNode's alloc with a real one
324
- RDATA(trie_node)->data = sb_trie_root(sb_trie);
225
+ TrieState *state = trie_root(trie);
226
+ RDATA(trie_node)->data = state;
325
227
 
326
228
  rb_iv_set(trie_node, "@state", Qnil);
327
229
  rb_iv_set(trie_node, "@full_state", rb_str_new2(""));
328
230
  return trie_node;
329
231
  }
330
232
 
331
- static VALUE trie_node_get_state(VALUE self) {
233
+ static VALUE rb_trie_node_get_state(VALUE self) {
332
234
  return rb_iv_get(self, "@state");
333
235
  }
334
- static VALUE trie_node_get_full_state(VALUE self) {
236
+ static VALUE rb_trie_node_get_full_state(VALUE self) {
335
237
  return rb_iv_get(self, "@full_state");
336
238
  }
337
239
 
338
- static VALUE trie_node_walk_bang(VALUE self, VALUE rchar) {
339
- SBTrieState *state;
340
- Data_Get_Struct(self, SBTrieState, state);
240
+ static VALUE rb_trie_node_walk_bang(VALUE self, VALUE rchar) {
241
+ TrieState *state;
242
+ Data_Get_Struct(self, TrieState, state);
341
243
 
342
244
  if(RSTRING(rchar)->len != 1)
343
- return Qnil;
245
+ return Qnil;
344
246
 
345
- char ch = RSTRING(rchar)->ptr[0];
346
- int result = sb_trie_state_walk(state, (TrieChar)ch);
247
+ Bool result = trie_state_walk(state, *RSTRING(rchar)->ptr);
347
248
 
348
249
  if(result) {
349
- rb_iv_set(self, "@state", rchar);
350
- VALUE full_state = rb_iv_get(self, "@full_state");
351
- rb_str_append(full_state, rchar);
352
- rb_iv_set(self, "@full_state", full_state);
353
- return self;
250
+ rb_iv_set(self, "@state", rchar);
251
+ VALUE full_state = rb_iv_get(self, "@full_state");
252
+ rb_str_append(full_state, rchar);
253
+ rb_iv_set(self, "@full_state", full_state);
254
+ return self;
354
255
  } else
355
- return Qnil;
256
+ return Qnil;
356
257
  }
357
258
 
358
- static VALUE trie_node_value(VALUE self) {
359
- SBTrieState *state, *dup;
360
- Data_Get_Struct(self, SBTrieState, state);
259
+ static VALUE rb_trie_node_value(VALUE self) {
260
+ TrieState *state;
261
+ TrieState *dup;
262
+ Data_Get_Struct(self, TrieState, state);
361
263
 
362
- dup = sb_trie_state_clone(state);
264
+ dup = trie_state_clone(state);
363
265
 
364
- sb_trie_state_walk(dup, (TrieChar)'\0');
365
- TrieData trie_data = sb_trie_state_get_data(dup);
366
- sb_trie_state_free(dup);
266
+ trie_state_walk(dup, 0);
267
+ TrieData trie_data = trie_state_get_data(dup);
268
+ trie_state_free(dup);
367
269
 
368
270
  return TRIE_DATA_ERROR == trie_data ? Qnil : INT2FIX(trie_data);
369
271
  }
370
272
 
371
- static VALUE trie_node_terminal(VALUE self) {
372
- SBTrieState *state;
373
- Data_Get_Struct(self, SBTrieState, state);
273
+ static VALUE rb_trie_node_terminal(VALUE self) {
274
+ TrieState *state;
275
+ Data_Get_Struct(self, TrieState, state);
374
276
 
375
- return sb_trie_state_is_terminal(state) ? Qtrue : Qnil;
277
+ return trie_state_is_terminal(state) ? Qtrue : Qnil;
376
278
  }
377
279
 
378
- static VALUE trie_node_leaf(VALUE self) {
379
- SBTrieState *state;
380
- Data_Get_Struct(self, SBTrieState, state);
280
+ static VALUE rb_trie_node_leaf(VALUE self) {
281
+ TrieState *state;
282
+ Data_Get_Struct(self, TrieState, state);
381
283
 
382
- return sb_trie_state_is_leaf(state) ? Qtrue : Qnil;
284
+ return trie_state_is_leaf(state) ? Qtrue : Qnil;
383
285
  }
384
286
 
385
- static VALUE trie_node_clone(VALUE self) {
386
- SBTrieState *state;
387
- Data_Get_Struct(self, SBTrieState, state);
287
+ static VALUE rb_trie_node_clone(VALUE self) {
288
+ TrieState *state;
289
+ Data_Get_Struct(self, TrieState, state);
388
290
 
389
- VALUE new_node = trie_node_alloc(cTrieNode);
390
- RDATA(new_node)->data = sb_trie_state_clone(state);
291
+ VALUE new_node = rb_trie_node_alloc(cTrieNode);
292
+
293
+ TrieState *new_state = trie_state_clone(state);
294
+
295
+ RDATA(new_node)->data = new_state;
391
296
 
392
297
  rb_iv_set(new_node, "@state", rb_iv_get(self, "@state"));
393
298
  rb_iv_set(new_node, "@full_state", rb_iv_get(self, "@full_state"));
@@ -398,27 +303,25 @@ static VALUE trie_node_clone(VALUE self) {
398
303
 
399
304
  void Init_trie() {
400
305
  cTrie = rb_define_class("Trie", rb_cObject);
401
- rb_define_alloc_func(cTrie, trie_alloc);
402
- rb_define_method(cTrie, "initialize", trie_initialize, 1);
403
- rb_define_method(cTrie, "path", trie_get_path, 0);
404
- rb_define_method(cTrie, "has_key?", trie_has_key, 1);
405
- rb_define_method(cTrie, "get", trie_get, 1);
406
- rb_define_method(cTrie, "add", trie_add, -2);
407
- rb_define_method(cTrie, "delete", trie_delete, 1);
408
- rb_define_method(cTrie, "close", trie_close, 0);
409
- rb_define_method(cTrie, "children", trie_children, 1);
410
- rb_define_method(cTrie, "children_with_values", trie_children_with_values, 1);
411
- rb_define_method(cTrie, "walk_to_terminal", trie_walk_to_terminal, -2);
412
- rb_define_method(cTrie, "root", trie_root, 0);
413
- rb_define_method(cTrie, "save", trie_save, 0);
306
+ rb_define_alloc_func(cTrie, rb_trie_alloc);
307
+ //rb_define_method(cTrie, "initialize", rb_trie_initialize, -2);
308
+ //rb_define_method(cTrie, "path", rb_trie_get_path, 0);
309
+ rb_define_method(cTrie, "has_key?", rb_trie_has_key, 1);
310
+ rb_define_method(cTrie, "get", rb_trie_get, 1);
311
+ rb_define_method(cTrie, "add", rb_trie_add, -2);
312
+ rb_define_method(cTrie, "delete", rb_trie_delete, 1);
313
+ rb_define_method(cTrie, "children", rb_trie_children, 1);
314
+ rb_define_method(cTrie, "children_with_values", rb_trie_children_with_values, 1);
315
+ rb_define_method(cTrie, "root", rb_trie_root, 0);
316
+ //rb_define_method(cTrie, "save", rb_trie_save, 0);
414
317
 
415
318
  cTrieNode = rb_define_class("TrieNode", rb_cObject);
416
- rb_define_alloc_func(cTrieNode, trie_node_alloc);
417
- rb_define_method(cTrieNode, "state", trie_node_get_state, 0);
418
- rb_define_method(cTrieNode, "full_state", trie_node_get_full_state, 0);
419
- rb_define_method(cTrieNode, "walk!", trie_node_walk_bang, 1);
420
- rb_define_method(cTrieNode, "value", trie_node_value, 0);
421
- rb_define_method(cTrieNode, "terminal?", trie_node_terminal, 0);
422
- rb_define_method(cTrieNode, "leaf?", trie_node_leaf, 0);
423
- rb_define_method(cTrieNode, "clone", trie_node_clone, 0);
319
+ rb_define_alloc_func(cTrieNode, rb_trie_node_alloc);
320
+ rb_define_method(cTrieNode, "state", rb_trie_node_get_state, 0);
321
+ rb_define_method(cTrieNode, "full_state", rb_trie_node_get_full_state, 0);
322
+ rb_define_method(cTrieNode, "walk!", rb_trie_node_walk_bang, 1);
323
+ rb_define_method(cTrieNode, "value", rb_trie_node_value, 0);
324
+ rb_define_method(cTrieNode, "terminal?", rb_trie_node_terminal, 0);
325
+ rb_define_method(cTrieNode, "leaf?", rb_trie_node_leaf, 0);
326
+ rb_define_method(cTrieNode, "clone", rb_trie_node_clone, 0);
424
327
  }