tyler-trie 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/VERSION.yml +4 -0
  2. data/ext/libdatrie/AUTHORS +1 -0
  3. data/ext/libdatrie/COPYING +510 -0
  4. data/ext/libdatrie/ChangeLog +410 -0
  5. data/ext/libdatrie/INSTALL +236 -0
  6. data/ext/libdatrie/Makefile.am +5 -0
  7. data/ext/libdatrie/Makefile.in +661 -0
  8. data/ext/libdatrie/NEWS +27 -0
  9. data/ext/libdatrie/README +32 -0
  10. data/ext/libdatrie/aclocal.m4 +7431 -0
  11. data/ext/libdatrie/config.guess +1516 -0
  12. data/ext/libdatrie/config.h.in +74 -0
  13. data/ext/libdatrie/config.sub +1626 -0
  14. data/ext/libdatrie/configure +22008 -0
  15. data/ext/libdatrie/configure.ac +71 -0
  16. data/ext/libdatrie/datrie.pc.in +11 -0
  17. data/ext/libdatrie/datrie/Makefile.am +35 -0
  18. data/ext/libdatrie/datrie/Makefile.in +522 -0
  19. data/ext/libdatrie/datrie/alpha-map.c +170 -0
  20. data/ext/libdatrie/datrie/alpha-map.h +36 -0
  21. data/ext/libdatrie/datrie/darray.c +674 -0
  22. data/ext/libdatrie/datrie/darray.h +229 -0
  23. data/ext/libdatrie/datrie/fileutils.c +151 -0
  24. data/ext/libdatrie/datrie/fileutils.h +36 -0
  25. data/ext/libdatrie/datrie/libdatrie.def +31 -0
  26. data/ext/libdatrie/datrie/sb-trie.c +331 -0
  27. data/ext/libdatrie/datrie/sb-trie.h +279 -0
  28. data/ext/libdatrie/datrie/tail.c +344 -0
  29. data/ext/libdatrie/datrie/tail.h +200 -0
  30. data/ext/libdatrie/datrie/trie-private.h +31 -0
  31. data/ext/libdatrie/datrie/trie.c +413 -0
  32. data/ext/libdatrie/datrie/trie.h +270 -0
  33. data/ext/libdatrie/datrie/triedefs.h +63 -0
  34. data/ext/libdatrie/datrie/typedefs.h +113 -0
  35. data/ext/libdatrie/depcomp +530 -0
  36. data/ext/libdatrie/doc/Doxyfile.in +244 -0
  37. data/ext/libdatrie/doc/Makefile.am +29 -0
  38. data/ext/libdatrie/doc/Makefile.in +352 -0
  39. data/ext/libdatrie/install-sh +323 -0
  40. data/ext/libdatrie/ltmain.sh +6938 -0
  41. data/ext/libdatrie/man/Makefile.am +4 -0
  42. data/ext/libdatrie/man/Makefile.in +381 -0
  43. data/ext/libdatrie/man/trietool.1 +107 -0
  44. data/ext/libdatrie/missing +360 -0
  45. data/ext/libdatrie/tools/Makefile.am +7 -0
  46. data/ext/libdatrie/tools/Makefile.in +460 -0
  47. data/ext/libdatrie/tools/trietool.c +308 -0
  48. data/ext/trie/extconf.rb +12 -0
  49. data/ext/trie/trie.c +174 -0
  50. data/lib/trie.rb +1 -0
  51. data/spec/test-trie/README +1 -0
  52. data/spec/trie_spec.rb +79 -0
  53. metadata +139 -0
@@ -0,0 +1,331 @@
1
+ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
+ /*
3
+ * sb-trie.c - Single-byte domain trie front end
4
+ * Created: 2006-08-19
5
+ * Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
6
+ */
7
+
8
+ #include <string.h>
9
+ #include <stdlib.h>
10
+
11
+ #include "sb-trie.h"
12
+ #include "trie.h"
13
+ #include "alpha-map.h"
14
+
15
+ /*------------------------*
16
+ * INTERNAL FUNCTIONS *
17
+ *------------------------*/
18
+ static TrieChar * sb_map_char_to_alphabet_str (const AlphaMap *alpha_map,
19
+ const SBChar *str);
20
+
21
+ static SBChar * sb_map_alphabet_to_char_str (const AlphaMap *alpha_map,
22
+ const TrieChar *str);
23
+
24
+ /* ==================== BEGIN IMPLEMENTATION PART ==================== */
25
+
26
+ /*----------------------------------------*
27
+ * INTERNAL FUNCTIONS IMPLEMENTATIONS *
28
+ *----------------------------------------*/
29
+
30
+ static TrieChar *
31
+ sb_map_char_to_alphabet_str (const AlphaMap *alpha_map, const SBChar *str)
32
+ {
33
+ TrieChar *alphabet_str, *p;
34
+
35
+ alphabet_str = (TrieChar *) malloc (strlen (str) + 1);
36
+ for (p = alphabet_str; *str; p++, str++)
37
+ *p = alpha_map_char_to_alphabet (alpha_map, *str);
38
+ *p = '\0';
39
+
40
+ return alphabet_str;
41
+ }
42
+
43
+ static SBChar *
44
+ sb_map_alphabet_to_char_str (const AlphaMap *alpha_map, const TrieChar *str)
45
+ {
46
+ SBChar *sb_str, *p;
47
+
48
+ sb_str = (SBChar *) malloc (strlen ((const char *)str) + 1);
49
+ for (p = sb_str; *str; p++, str++)
50
+ *p = (SBChar) alpha_map_alphabet_to_char (alpha_map, *str);
51
+ *p = '\0';
52
+
53
+ return sb_str;
54
+ }
55
+
56
+ /*------------------------------*
57
+ * PRIVATE DATA DEFINITONS *
58
+ *------------------------------*/
59
+ struct _SBTrie {
60
+ Trie *trie;
61
+ AlphaMap *alpha_map;
62
+ };
63
+
64
+ struct _SBTrieState {
65
+ SBTrie *sb_trie;
66
+ TrieState *trie_state;
67
+ };
68
+
69
+ /*-----------------------------*
70
+ * METHODS IMPLEMENTAIONS *
71
+ *-----------------------------*/
72
+
73
+ /*-----------------------*
74
+ * GENERAL FUNCTIONS *
75
+ *-----------------------*/
76
+
77
+ SBTrie *
78
+ sb_trie_open (const char *path, const char *name, TrieIOMode mode)
79
+ {
80
+ SBTrie *sb_trie;
81
+
82
+ sb_trie = (SBTrie *) malloc (sizeof (SBTrie));
83
+ if (!sb_trie)
84
+ return NULL;
85
+
86
+ sb_trie->trie = trie_open (path, name, mode);
87
+ if (!sb_trie->trie)
88
+ goto exit1;
89
+
90
+ sb_trie->alpha_map = alpha_map_open (path, name, ".sbm");
91
+ if (!sb_trie->alpha_map)
92
+ goto exit2;
93
+
94
+ return sb_trie;
95
+
96
+ exit2:
97
+ trie_close (sb_trie->trie);
98
+ exit1:
99
+ free (sb_trie);
100
+ return NULL;
101
+ }
102
+
103
+ int
104
+ sb_trie_close (SBTrie *sb_trie)
105
+ {
106
+ if (!sb_trie)
107
+ return -1;
108
+
109
+ alpha_map_free (sb_trie->alpha_map);
110
+ return trie_close (sb_trie->trie);
111
+ }
112
+
113
+ int
114
+ sb_trie_save (SBTrie *sb_trie)
115
+ {
116
+ if (!sb_trie)
117
+ return -1;
118
+
119
+ return trie_save (sb_trie->trie);
120
+ }
121
+
122
+
123
+ /*------------------------------*
124
+ * GENERAL QUERY OPERATIONS *
125
+ *------------------------------*/
126
+
127
+ Bool
128
+ sb_trie_retrieve (SBTrie *sb_trie, const SBChar *key, TrieData *o_data)
129
+ {
130
+ TrieChar *trie_key;
131
+ Bool ret;
132
+
133
+ if (!sb_trie)
134
+ return FALSE;
135
+
136
+ trie_key = sb_map_char_to_alphabet_str (sb_trie->alpha_map, key);
137
+ ret = trie_retrieve (sb_trie->trie, trie_key, o_data);
138
+ free (trie_key);
139
+
140
+ return ret;
141
+ }
142
+
143
+ Bool
144
+ sb_trie_store (SBTrie *sb_trie, const SBChar *key, TrieData data)
145
+ {
146
+ TrieChar *trie_key;
147
+ Bool ret;
148
+
149
+ if (!sb_trie)
150
+ return FALSE;
151
+
152
+ trie_key = sb_map_char_to_alphabet_str (sb_trie->alpha_map, key);
153
+ ret = trie_store (sb_trie->trie, trie_key, data);
154
+ free (trie_key);
155
+
156
+ return ret;
157
+ }
158
+
159
+ Bool
160
+ sb_trie_delete (SBTrie *sb_trie, const SBChar *key)
161
+ {
162
+ TrieChar *trie_key;
163
+ Bool ret;
164
+
165
+ if (!sb_trie)
166
+ return FALSE;
167
+
168
+ trie_key = sb_map_char_to_alphabet_str (sb_trie->alpha_map, key);
169
+ ret = trie_delete (sb_trie->trie, trie_key);
170
+ free (trie_key);
171
+
172
+ return ret;
173
+ }
174
+
175
+ typedef struct {
176
+ SBTrie *sb_trie;
177
+ SBTrieEnumFunc enum_func;
178
+ void *user_data;
179
+ } _SBTrieEnumData;
180
+
181
+ static Bool
182
+ sb_trie_enum_func (const TrieChar *key, TrieData key_data, void *user_data)
183
+ {
184
+ _SBTrieEnumData *enum_data;
185
+ SBChar *sb_key;
186
+ Bool ret;
187
+
188
+ enum_data = (_SBTrieEnumData *) user_data;
189
+
190
+ sb_key = sb_map_alphabet_to_char_str (enum_data->sb_trie->alpha_map, key);
191
+ ret = (*enum_data->enum_func) (sb_key, key_data, enum_data->user_data);
192
+ free (sb_key);
193
+
194
+ return ret;
195
+ }
196
+
197
+ Bool
198
+ sb_trie_enumerate (SBTrie *sb_trie,
199
+ SBTrieEnumFunc enum_func,
200
+ void *user_data)
201
+ {
202
+ _SBTrieEnumData enum_data;
203
+
204
+ if (!sb_trie)
205
+ return FALSE;
206
+
207
+ enum_data.sb_trie = sb_trie;
208
+ enum_data.enum_func = enum_func;
209
+ enum_data.user_data = user_data;
210
+
211
+ return trie_enumerate (sb_trie->trie, sb_trie_enum_func, &enum_data);
212
+ }
213
+
214
+
215
+ /*-------------------------------*
216
+ * STEPWISE QUERY OPERATIONS *
217
+ *-------------------------------*/
218
+
219
+ SBTrieState *
220
+ sb_trie_root (SBTrie *sb_trie)
221
+ {
222
+ SBTrieState *sb_state;
223
+
224
+ if (!sb_trie)
225
+ return NULL;
226
+
227
+ sb_state = (SBTrieState *) malloc (sizeof (SBTrieState));
228
+ if (!sb_state)
229
+ return NULL;
230
+
231
+ sb_state->sb_trie = sb_trie;
232
+ sb_state->trie_state = trie_root (sb_trie->trie);
233
+
234
+ return sb_state;
235
+ }
236
+
237
+
238
+ /*----------------*
239
+ * TRIE STATE *
240
+ *----------------*/
241
+
242
+ SBTrieState *
243
+ sb_trie_state_clone (const SBTrieState *s)
244
+ {
245
+ SBTrieState *new_state;
246
+
247
+ if (!s)
248
+ return NULL;
249
+
250
+ new_state = (SBTrieState *) malloc (sizeof (SBTrieState));
251
+ if (!new_state)
252
+ return NULL;
253
+
254
+ new_state->sb_trie = s->sb_trie;
255
+ new_state->trie_state = trie_state_clone (s->trie_state);
256
+
257
+ return new_state;
258
+ }
259
+
260
+ void
261
+ sb_trie_state_free (SBTrieState *s)
262
+ {
263
+ if (!s)
264
+ return;
265
+
266
+ trie_state_free (s->trie_state);
267
+ free (s);
268
+ }
269
+
270
+ void
271
+ sb_trie_state_rewind (SBTrieState *s)
272
+ {
273
+ if (!s)
274
+ return;
275
+
276
+ trie_state_rewind (s->trie_state);
277
+ }
278
+
279
+ Bool
280
+ sb_trie_state_walk (SBTrieState *s, SBChar c)
281
+ {
282
+ if (!s)
283
+ return FALSE;
284
+
285
+ return trie_state_walk (s->trie_state,
286
+ alpha_map_char_to_alphabet (s->sb_trie->alpha_map,
287
+ (UniChar) c));
288
+ }
289
+
290
+ Bool
291
+ sb_trie_state_is_walkable (const SBTrieState *s, SBChar c)
292
+ {
293
+ if (!s)
294
+ return FALSE;
295
+
296
+ return trie_state_is_walkable (
297
+ s->trie_state,
298
+ alpha_map_char_to_alphabet (s->sb_trie->alpha_map, (UniChar) c)
299
+ );
300
+ }
301
+
302
+ Bool
303
+ sb_trie_state_is_terminal (const SBTrieState *s)
304
+ {
305
+ if (!s)
306
+ return FALSE;
307
+
308
+ return trie_state_is_terminal (s->trie_state);
309
+ }
310
+
311
+ Bool
312
+ sb_trie_state_is_leaf (const SBTrieState *s)
313
+ {
314
+ if (!s)
315
+ return FALSE;
316
+
317
+ return trie_state_is_leaf (s->trie_state);
318
+ }
319
+
320
+ TrieData
321
+ sb_trie_state_get_data (const SBTrieState *s)
322
+ {
323
+ if (!s)
324
+ return TRIE_DATA_ERROR;
325
+
326
+ return trie_state_get_data (s->trie_state);
327
+ }
328
+
329
+ /*
330
+ vi:ts=4:ai:expandtab
331
+ */
@@ -0,0 +1,279 @@
1
+ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
+ /*
3
+ * sb-trie.h - Single-byte domain trie front end
4
+ * Created: 2006-08-19
5
+ * Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
6
+ */
7
+
8
+ #ifndef __SB_TRIE_H
9
+ #define __SB_TRIE_H
10
+
11
+ #include <datrie/triedefs.h>
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ /**
18
+ * @file sb-trie.h
19
+ * @brief Trie data type and functions
20
+ */
21
+
22
+ /**
23
+ * @brief Single-byte domain trie data type
24
+ */
25
+ typedef struct _SBTrie SBTrie;
26
+
27
+ /**
28
+ * @brief Single-byte character type
29
+ */
30
+ typedef unsigned char SBChar;
31
+
32
+ /**
33
+ * @brief Single-byte domain trie enumeration function
34
+ *
35
+ * @param key : the key of the entry
36
+ * @param data : the data of the entry
37
+ *
38
+ * @return TRUE to continue enumeration, FALSE to stop
39
+ */
40
+ typedef Bool (*SBTrieEnumFunc) (const SBChar *key,
41
+ TrieData key_data,
42
+ void *user_data);
43
+
44
+ /**
45
+ * @brief Single-byte domain trie walking state
46
+ */
47
+ typedef struct _SBTrieState SBTrieState;
48
+
49
+ /*-----------------------*
50
+ * GENERAL FUNCTIONS *
51
+ *-----------------------*/
52
+
53
+ /**
54
+ * @brief Open a single-byte domain trie
55
+ *
56
+ * @param path : the path that stores the trie files
57
+ * @param name : the name of the trie (not actual file name)
58
+ * @param mode : openning mode, read or write
59
+ *
60
+ * @return a pointer to the openned trie, NULL on failure
61
+ *
62
+ * Open a trie of given name. Note that @a name here does not mean the
63
+ * actual file name. Rather, the file name will be inferred by the name.
64
+ */
65
+ SBTrie * sb_trie_open (const char *path, const char *name, TrieIOMode mode);
66
+
67
+ /**
68
+ * @brief Close a single-byte domain trie
69
+ *
70
+ * @param sb_trie: the single-byte domain trie
71
+ *
72
+ * @return 0 on success, non-zero on failure
73
+ *
74
+ * Close the given trie. If @a trie was openned for writing, all pending
75
+ * changes will be saved to file.
76
+ */
77
+ int sb_trie_close (SBTrie *sb_trie);
78
+
79
+ /**
80
+ * @brief Save a single-byte domain trie
81
+ *
82
+ * @param sb_trie: the single-byte domain trie
83
+ *
84
+ * @return 0 on success, non-zero on failure
85
+ *
86
+ * If @a trie was openned for writing, save all pending data to file.
87
+ */
88
+ int sb_trie_save (SBTrie *sb_trie);
89
+
90
+
91
+ /*------------------------------*
92
+ * GENERAL QUERY OPERATIONS *
93
+ *------------------------------*/
94
+
95
+ /**
96
+ * @brief Retrieve an entry from single-byte domain trie
97
+ *
98
+ * @param sb_trie : the single-byte domain trie
99
+ * @param key : the key for the entry to retrieve
100
+ * @param o_data : the storage for storing the entry data on return
101
+ *
102
+ * @return boolean value indicating the existence of the entry.
103
+ *
104
+ * Retrieve an entry for the given @a key from @a trie. On return,
105
+ * if @a key is found and @a o_val is not NULL, @a *o_val is set
106
+ * to the data associated to @a key.
107
+ */
108
+ Bool sb_trie_retrieve (SBTrie *sb_trie,
109
+ const SBChar *key,
110
+ TrieData *o_data);
111
+
112
+ /**
113
+ * @brief Store a value for an entry to single-byte domain trie
114
+ *
115
+ * @param sb_trie : the single-byte domain trie
116
+ * @param key : the key for the entry to retrieve
117
+ * @param data : the data associated to the entry
118
+ *
119
+ * @return boolean value indicating the success of the process
120
+ *
121
+ * Store a data @a data for the given @a key in @a trie. If @a key does not
122
+ * exist in @a trie, it will be appended.
123
+ */
124
+ Bool sb_trie_store (SBTrie *sb_trie, const SBChar *key, TrieData data);
125
+
126
+ /**
127
+ * @brief Delete an entry from single-byte domain trie
128
+ *
129
+ * @param sb_trie : the single-byte domain trie
130
+ * @param key : the key for the entry to delete
131
+ *
132
+ * @return boolean value indicating whether the key exists and is removed
133
+ *
134
+ * Delete an entry for the given @a key from @a trie.
135
+ */
136
+ Bool sb_trie_delete (SBTrie *sb_trie, const SBChar *key);
137
+
138
+ /**
139
+ * @brief Enumerate entries in single-byte domain trie
140
+ *
141
+ * @param sb_trie : the single-byte domain trie
142
+ * @param enum_func : the callback function to be called on each key
143
+ * @param user_data : user-supplied data to send as an argument to @a enum_func
144
+ *
145
+ * @return boolean value indicating whether all the keys are visited
146
+ *
147
+ * Enumerate all entries in trie. For each entry, the user-supplied
148
+ * @a enum_func callback function is called, with the entry key and data.
149
+ * Returning FALSE from such callback will stop enumeration and return FALSE.
150
+ */
151
+ Bool sb_trie_enumerate (SBTrie *sb_trie,
152
+ SBTrieEnumFunc enum_func,
153
+ void *user_data);
154
+
155
+
156
+ /*-------------------------------*
157
+ * STEPWISE QUERY OPERATIONS *
158
+ *-------------------------------*/
159
+
160
+ /**
161
+ * @brief Get root state of a single-byte domain trie
162
+ *
163
+ * @param sb_trie : the single-byte domain trie
164
+ *
165
+ * @return the root state of the trie
166
+ *
167
+ * Get root state of @a trie, for stepwise walking.
168
+ *
169
+ * The returned state is allocated and must be freed with trie_state_free()
170
+ */
171
+ SBTrieState * sb_trie_root (SBTrie *sb_trie);
172
+
173
+
174
+ /*----------------*
175
+ * TRIE STATE *
176
+ *----------------*/
177
+
178
+ /**
179
+ * @brief Clone a single-byte domain trie state
180
+ *
181
+ * @param s : the state to clone
182
+ *
183
+ * @return an duplicated instance of @a s
184
+ *
185
+ * Make a copy of trie state.
186
+ *
187
+ * The returned state is allocated and must be freed with trie_state_free()
188
+ */
189
+ SBTrieState * sb_trie_state_clone (const SBTrieState *s);
190
+
191
+ /**
192
+ * @brief Free a single-byte trie state
193
+ *
194
+ * @param s : the state to free
195
+ *
196
+ * Free the trie state.
197
+ */
198
+ void sb_trie_state_free (SBTrieState *s);
199
+
200
+ /**
201
+ * @brief Rewind a single-byte trie state
202
+ *
203
+ * @param s : the state to rewind
204
+ *
205
+ * Put the state at root.
206
+ */
207
+ void sb_trie_state_rewind (SBTrieState *s);
208
+
209
+ /**
210
+ * @brief Walk the single-byte domain trie from the state
211
+ *
212
+ * @param s : current state
213
+ * @param c : key character for walking
214
+ *
215
+ * @return boolean value indicating the success of the walk
216
+ *
217
+ * Walk the trie stepwise, using a given character @a c.
218
+ * On return, the state @a s is updated to the new state if successfully walked.
219
+ */
220
+ Bool sb_trie_state_walk (SBTrieState *s, SBChar c);
221
+
222
+ /**
223
+ * @brief Test walkability of character from state
224
+ *
225
+ * @param s : the state to check
226
+ * @param c : the input character
227
+ *
228
+ * @return boolean indicating walkability
229
+ *
230
+ * Test if there is a transition from state @a s with input character @a c.
231
+ */
232
+ Bool sb_trie_state_is_walkable (const SBTrieState *s, SBChar c);
233
+
234
+ /**
235
+ * @brief Check for terminal state
236
+ *
237
+ * @param s : the state to check
238
+ *
239
+ * @return boolean value indicating whether it is a terminal state
240
+ *
241
+ * Check if the given state is a terminal state. A leaf state is a trie state
242
+ * that terminates a key, and stores a value associated with the key.
243
+ */
244
+ Bool sb_trie_state_is_terminal (const SBTrieState *s);
245
+
246
+ /**
247
+ * @brief Check for leaf state
248
+ *
249
+ * @param s : the state to check
250
+ *
251
+ * @return boolean value indicating whether it is a leaf state
252
+ *
253
+ * Check if the given state is a leaf state. A leaf state is a terminal state
254
+ * that has no other branch.
255
+ */
256
+ Bool sb_trie_state_is_leaf (const SBTrieState *s);
257
+
258
+ /**
259
+ * @brief Get data from leaf state
260
+ *
261
+ * @param s : the leaf state
262
+ *
263
+ * @return the data associated with the leaf state @a s,
264
+ * or TRIE_DATA_ERROR if @a s is not a leaf state
265
+ *
266
+ * Get value from a leaf state of trie. Getting the value from leaf state
267
+ * will result in TRIE_DATA_ERROR.
268
+ */
269
+ TrieData sb_trie_state_get_data (const SBTrieState *s);
270
+
271
+ #ifdef __cplusplus
272
+ }
273
+ #endif
274
+
275
+ #endif /* __SB_TRIE_H */
276
+
277
+ /*
278
+ vi:ts=4:ai:expandtab
279
+ */