/src/igraph/src/core/trie.c
Line | Count | Source |
1 | | /* |
2 | | igraph library. |
3 | | Copyright (C) 2003-2012 Gabor Csardi <csardi.gabor@gmail.com> |
4 | | 334 Harvard street, Cambridge, MA 02139 USA |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 2 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | | 02110-1301 USA |
20 | | |
21 | | */ |
22 | | |
23 | | #include "igraph_types.h" |
24 | | #include "igraph_memory.h" |
25 | | |
26 | | #include "core/trie.h" |
27 | | #include "internal/hacks.h" /* strdup */ |
28 | | |
29 | | #include <assert.h> |
30 | | #include <string.h> |
31 | | |
32 | | |
33 | | /* |
34 | | * igraph_trie_t is a data structures that stores an ordered list of strings. |
35 | | * It allows an efficient lookup of the index of a string. It has the capability |
36 | | * to also store the list of strings directly for reverse lookup of strings |
37 | | * by index. |
38 | | */ |
39 | | |
40 | | /* Allocates memory for a trie node. */ |
41 | 298k | static igraph_error_t igraph_i_trie_init_node(igraph_trie_node_t *t) { |
42 | 298k | IGRAPH_STRVECTOR_INIT_FINALLY(&t->strs, 0); |
43 | 298k | IGRAPH_VECTOR_PTR_INIT_FINALLY(&t->children, 0); |
44 | 298k | IGRAPH_VECTOR_INT_INIT_FINALLY(&t->values, 0); |
45 | 298k | IGRAPH_FINALLY_CLEAN(3); |
46 | 298k | return IGRAPH_SUCCESS; |
47 | 298k | } |
48 | | |
49 | | static void igraph_i_trie_destroy_node(igraph_trie_node_t *t); |
50 | | |
51 | | /** |
52 | | * \ingroup igraphtrie |
53 | | * \brief Creates a trie. |
54 | | * |
55 | | * \param t An uninitialized trie. |
56 | | * \param storekeys Specifies whether keys are stored for reverse lookup. |
57 | | * \return Error code: Errors by \ref igraph_strvector_init(), |
58 | | * \ref igraph_vector_ptr_init() and \ref igraph_vector_init() might be returned. |
59 | | */ |
60 | | |
61 | 298k | igraph_error_t igraph_trie_init(igraph_trie_t *t, igraph_bool_t storekeys) { |
62 | 298k | t->maxvalue = -1; |
63 | 298k | t->storekeys = storekeys; |
64 | 298k | IGRAPH_CHECK(igraph_i_trie_init_node(&t->node)); |
65 | 298k | IGRAPH_FINALLY(igraph_i_trie_destroy_node, &t->node); |
66 | 298k | if (storekeys) { |
67 | 79.0k | IGRAPH_CHECK(igraph_strvector_init(&t->keys, 0)); |
68 | 79.0k | } |
69 | | |
70 | 298k | IGRAPH_FINALLY_CLEAN(1); |
71 | 298k | return IGRAPH_SUCCESS; |
72 | 298k | } |
73 | | |
74 | 1.86M | static void igraph_i_trie_destroy_node_helper(igraph_trie_node_t *t, igraph_bool_t sfree) { |
75 | 1.86M | igraph_strvector_destroy(&t->strs); |
76 | 1.86M | igraph_int_t children_size = igraph_vector_ptr_size(&t->children); |
77 | 16.4M | for (igraph_int_t i = 0; i < children_size; i++) { |
78 | 14.5M | igraph_trie_node_t *child = VECTOR(t->children)[i]; |
79 | 14.5M | if (child != NULL) { |
80 | 1.56M | igraph_i_trie_destroy_node_helper(child, true); |
81 | 1.56M | } |
82 | 14.5M | } |
83 | 1.86M | igraph_vector_ptr_destroy(&t->children); |
84 | 1.86M | igraph_vector_int_destroy(&t->values); |
85 | 1.86M | if (sfree) { |
86 | 1.56M | IGRAPH_FREE(t); |
87 | 1.56M | } |
88 | 1.86M | } |
89 | | |
90 | | /* Deallocates a trie node. */ |
91 | 298k | static void igraph_i_trie_destroy_node(igraph_trie_node_t *t) { |
92 | 298k | igraph_i_trie_destroy_node_helper(t, false); |
93 | 298k | } |
94 | | |
95 | | /** |
96 | | * \ingroup igraphtrie |
97 | | * \brief Destroys a trie (frees allocated memory). |
98 | | * |
99 | | * \param t The trie. |
100 | | */ |
101 | | |
102 | 298k | void igraph_trie_destroy(igraph_trie_t *t) { |
103 | 298k | if (t->storekeys) { |
104 | 79.0k | igraph_strvector_destroy(&t->keys); |
105 | 79.0k | } |
106 | 298k | igraph_i_trie_destroy_node(&t->node); |
107 | 298k | } |
108 | | |
109 | | |
110 | | /* Computes the location (index) of the first difference between 'str' and 'key' */ |
111 | 845M | static size_t igraph_i_strdiff(const char *str, const char *key) { |
112 | 845M | size_t diff = 0; |
113 | 1.87G | while (key[diff] != '\0' && str[diff] != '\0' && str[diff] == key[diff]) { |
114 | 1.02G | diff++; |
115 | 1.02G | } |
116 | 845M | return diff; |
117 | 845M | } |
118 | | |
119 | | /** |
120 | | * \ingroup igraphtrie |
121 | | * \brief Search/insert in a trie (not to be called directly). |
122 | | * |
123 | | * \return Error code, usually \c IGRAPH_ENOMEM. |
124 | | */ |
125 | | |
126 | | static igraph_error_t igraph_i_trie_get_node( |
127 | | igraph_trie_node_t *t, const char *key, igraph_int_t newvalue, |
128 | | igraph_int_t *id |
129 | 203M | ) { |
130 | 203M | assert(key != NULL); |
131 | | |
132 | | /* If newvalue is negative, we don't add the node if nonexistent, only check |
133 | | * for its existence */ |
134 | 203M | igraph_bool_t add = (newvalue >= 0); |
135 | | |
136 | 203M | igraph_int_t strs_size = igraph_strvector_size(&t->strs); |
137 | 858M | for (igraph_int_t i = 0; i < strs_size; i++) { |
138 | 845M | size_t diff; |
139 | 845M | const char *str = igraph_strvector_get(&t->strs, i); |
140 | 845M | diff = igraph_i_strdiff(str, key); |
141 | | |
142 | 845M | if (diff == 0) { |
143 | | |
144 | | /* ------------------------------------ */ |
145 | | /* No match, next */ |
146 | | |
147 | 654M | } else if (str[diff] == '\0' && key[diff] == '\0') { |
148 | | |
149 | | /* ------------------------------------ */ |
150 | | /* They are exactly the same */ |
151 | 57.5M | if (VECTOR(t->values)[i] != -1) { |
152 | 57.5M | *id = VECTOR(t->values)[i]; |
153 | 57.5M | return IGRAPH_SUCCESS; |
154 | 57.5M | } else { |
155 | 15.3k | VECTOR(t->values)[i] = newvalue; |
156 | 15.3k | *id = newvalue; |
157 | 15.3k | return IGRAPH_SUCCESS; |
158 | 15.3k | } |
159 | | |
160 | 133M | } else if (str[diff] == '\0') { |
161 | | |
162 | | /* ------------------------------------ */ |
163 | | /* str is prefix of key, follow its link if there is one */ |
164 | 132M | igraph_trie_node_t *node = VECTOR(t->children)[i]; |
165 | 132M | if (node != NULL) { |
166 | 131M | return igraph_i_trie_get_node(node, key + diff, newvalue, id); |
167 | 131M | } else if (add) { |
168 | 1.40M | igraph_trie_node_t *new_node = IGRAPH_CALLOC(1, igraph_trie_node_t); |
169 | 1.40M | IGRAPH_CHECK_OOM(new_node, "Cannot add to trie."); |
170 | 1.40M | IGRAPH_FINALLY(igraph_free, new_node); |
171 | | |
172 | 1.40M | IGRAPH_STRVECTOR_INIT_FINALLY(&new_node->strs, 1); |
173 | 1.40M | IGRAPH_VECTOR_PTR_INIT_FINALLY(&new_node->children, 1); |
174 | 1.40M | IGRAPH_VECTOR_INT_INIT_FINALLY(&new_node->values, 1); |
175 | 1.40M | IGRAPH_CHECK(igraph_strvector_set(&new_node->strs, 0, key + diff)); |
176 | 1.40M | IGRAPH_FINALLY_CLEAN(4); |
177 | | |
178 | 1.40M | VECTOR(new_node->children)[0] = 0; |
179 | 1.40M | VECTOR(new_node->values)[0] = newvalue; |
180 | | |
181 | 1.40M | VECTOR(t->children)[i] = new_node; |
182 | | |
183 | 1.40M | *id = newvalue; |
184 | 1.40M | return IGRAPH_SUCCESS; |
185 | 1.40M | } else { |
186 | 3.91k | *id = -1; |
187 | 3.91k | return IGRAPH_SUCCESS; |
188 | 3.91k | } |
189 | | |
190 | 132M | } else if (key[diff] == '\0' && add) { |
191 | | |
192 | | /* ------------------------------------ */ |
193 | | /* key is prefix of str, the node has to be cut */ |
194 | 33.2k | char *str2; |
195 | | |
196 | 33.2k | igraph_trie_node_t *node = IGRAPH_CALLOC(1, igraph_trie_node_t); |
197 | 33.2k | IGRAPH_CHECK_OOM(node, "Cannot add to trie."); |
198 | 33.2k | IGRAPH_FINALLY(igraph_free, node); |
199 | | |
200 | 33.2k | IGRAPH_STRVECTOR_INIT_FINALLY(&node->strs, 1); |
201 | 33.2k | IGRAPH_VECTOR_PTR_INIT_FINALLY(&node->children, 1); |
202 | 33.2k | IGRAPH_VECTOR_INT_INIT_FINALLY(&node->values, 1); |
203 | 33.2k | IGRAPH_CHECK(igraph_strvector_set(&node->strs, 0, str + diff)); |
204 | | |
205 | 33.2k | VECTOR(node->children)[0] = VECTOR(t->children)[i]; |
206 | 33.2k | VECTOR(node->values)[0] = VECTOR(t->values)[i]; |
207 | | |
208 | 33.2k | str2 = strdup(str); |
209 | 33.2k | IGRAPH_CHECK_OOM(str2, "Cannot add to trie."); |
210 | 33.2k | IGRAPH_FINALLY(igraph_free, str2); |
211 | 33.2k | str2[diff] = '\0'; |
212 | | |
213 | 33.2k | IGRAPH_CHECK(igraph_strvector_set(&t->strs, i, str2)); |
214 | | |
215 | 33.2k | IGRAPH_FREE(str2); |
216 | 33.2k | IGRAPH_FINALLY_CLEAN(5); |
217 | | |
218 | 33.2k | VECTOR(t->values)[i] = newvalue; |
219 | 33.2k | VECTOR(t->children)[i] = node; |
220 | | |
221 | 33.2k | *id = newvalue; |
222 | 33.2k | return IGRAPH_SUCCESS; |
223 | | |
224 | 138k | } else if (add) { |
225 | | |
226 | | /* ------------------------------------ */ |
227 | | /* the first diff characters match */ |
228 | 126k | char *str2; |
229 | | |
230 | 126k | igraph_trie_node_t *node = IGRAPH_CALLOC(1, igraph_trie_node_t); |
231 | 126k | IGRAPH_CHECK_OOM(node, "Cannot add to trie."); |
232 | 126k | IGRAPH_FINALLY(igraph_free, node); |
233 | | |
234 | 126k | IGRAPH_STRVECTOR_INIT_FINALLY(&node->strs, 2); |
235 | 126k | IGRAPH_VECTOR_PTR_INIT_FINALLY(&node->children, 2); |
236 | 126k | IGRAPH_VECTOR_INT_INIT_FINALLY(&node->values, 2); |
237 | 126k | IGRAPH_CHECK(igraph_strvector_set(&node->strs, 0, str + diff)); |
238 | 126k | IGRAPH_CHECK(igraph_strvector_set(&node->strs, 1, key + diff)); |
239 | 126k | VECTOR(node->children)[0] = VECTOR(t->children)[i]; |
240 | 126k | VECTOR(node->children)[1] = 0; |
241 | 126k | VECTOR(node->values)[0] = VECTOR(t->values)[i]; |
242 | 126k | VECTOR(node->values)[1] = newvalue; |
243 | | |
244 | 126k | str2 = strdup(str); |
245 | 126k | IGRAPH_CHECK_OOM(str2, "Cannot add to trie."); |
246 | | |
247 | 126k | str2[diff] = '\0'; |
248 | 126k | IGRAPH_FINALLY(igraph_free, str2); |
249 | | |
250 | 126k | IGRAPH_CHECK(igraph_strvector_set(&t->strs, i, str2)); |
251 | | |
252 | 126k | IGRAPH_FREE(str2); |
253 | 126k | IGRAPH_FINALLY_CLEAN(5); |
254 | | |
255 | 126k | VECTOR(t->values)[i] = -1; |
256 | 126k | VECTOR(t->children)[i] = node; |
257 | | |
258 | 126k | *id = newvalue; |
259 | 126k | return IGRAPH_SUCCESS; |
260 | 126k | } else { |
261 | | |
262 | | /* ------------------------------------------------- */ |
263 | | /* No match, but we requested not to add the new key */ |
264 | 12.2k | *id = -1; |
265 | 12.2k | return IGRAPH_SUCCESS; |
266 | 12.2k | } |
267 | 845M | } |
268 | | |
269 | | /* ------------------------------------ */ |
270 | | /* Nothing matches */ |
271 | | |
272 | 12.8M | if (add) { |
273 | | /* Memory saving at the cost of performance may be possible by using the pattern |
274 | | * CHECK(reserve(vec, size(vec) + 1)); |
275 | | * push_back(vec, value); |
276 | | * This was the original pattern used before igraph 0.10. */ |
277 | 12.8M | IGRAPH_CHECK(igraph_strvector_push_back(&t->strs, key)); |
278 | 12.8M | IGRAPH_CHECK(igraph_vector_ptr_push_back(&t->children, NULL)); |
279 | 12.8M | IGRAPH_CHECK(igraph_vector_int_push_back(&t->values, newvalue)); |
280 | 12.8M | *id = newvalue; |
281 | 12.8M | } else { |
282 | 49.0k | *id = -1; |
283 | 49.0k | } |
284 | | |
285 | 12.8M | return IGRAPH_SUCCESS; |
286 | 12.8M | } |
287 | | |
288 | | /** |
289 | | * \ingroup igraphtrie |
290 | | * \brief Search/insert a null-terminated string in a trie. |
291 | | * |
292 | | * \param t The trie. |
293 | | * \param key The string to search for. If not found, it will be inserted. |
294 | | * \param id The index of the string is stored here. |
295 | | * \return Error code, usually \c IGRAPH_ENOMEM. |
296 | | */ |
297 | | |
298 | 67.6M | igraph_error_t igraph_trie_get(igraph_trie_t *t, const char *key, igraph_int_t *id) { |
299 | 67.6M | assert(key != NULL); |
300 | | |
301 | 67.6M | if (*key == '\0') { |
302 | 11 | IGRAPH_ERROR("Keys in a trie cannot be empty.", IGRAPH_EINVAL); |
303 | 11 | } |
304 | | |
305 | 67.6M | if (!t->storekeys) { |
306 | 45.1M | IGRAPH_CHECK(igraph_i_trie_get_node(&t->node, key, t->maxvalue + 1, id)); |
307 | 45.1M | if (*id > t->maxvalue) { |
308 | 10.7M | t->maxvalue = *id; |
309 | 10.7M | } |
310 | 45.1M | } else { |
311 | 22.5M | igraph_error_t ret; |
312 | | |
313 | 22.5M | IGRAPH_FINALLY_ENTER(); |
314 | | /* Add it to the string vector first, we can undo this later */ |
315 | 22.5M | ret = igraph_strvector_push_back(&t->keys, key); |
316 | 22.5M | if (ret != IGRAPH_SUCCESS) { |
317 | 0 | IGRAPH_FINALLY_EXIT(); |
318 | 0 | IGRAPH_ERROR("Cannot get element from trie.", ret); |
319 | 0 | } |
320 | 22.5M | ret = igraph_i_trie_get_node(&t->node, key, t->maxvalue + 1, id); |
321 | 22.5M | if (ret != IGRAPH_SUCCESS) { |
322 | 0 | igraph_strvector_resize(&t->keys, igraph_strvector_size(&t->keys) - 1); /* shrinks, error safe */ |
323 | 0 | IGRAPH_FINALLY_EXIT(); |
324 | 0 | IGRAPH_ERROR("Cannot get element from trie.", ret); |
325 | 0 | } |
326 | | |
327 | | /* everything is fine */ |
328 | 22.5M | if (*id > t->maxvalue) { |
329 | 3.62M | t->maxvalue = *id; |
330 | 18.8M | } else { |
331 | 18.8M | igraph_strvector_resize(&t->keys, igraph_strvector_size(&t->keys) - 1); /* shrinks, error safe */ |
332 | 18.8M | } |
333 | 22.5M | IGRAPH_FINALLY_EXIT(); |
334 | 22.5M | } |
335 | | |
336 | 67.6M | return IGRAPH_SUCCESS; |
337 | 67.6M | } |
338 | | |
339 | | /** |
340 | | * \ingroup igraphtrie |
341 | | * \brief Search/insert a string of given length in a trie. |
342 | | * |
343 | | * This function is identical to \ref igraph_trie_get(), except that |
344 | | * it takes a string of a given length as input instead of a null-terminated |
345 | | * string. |
346 | | * |
347 | | * \param t The trie. |
348 | | * \param key The string to search for. If not found, it will be inserted. |
349 | | * \param length The length of \p key. |
350 | | * \param id The index of the string is stored here. |
351 | | * \return Error code, usually \c IGRAPH_ENOMEM. |
352 | | */ |
353 | | |
354 | | igraph_error_t igraph_trie_get_len( |
355 | | igraph_trie_t *t, const char *key, |
356 | | igraph_int_t length, |
357 | 12.9M | igraph_int_t *id) { |
358 | | |
359 | 12.9M | char *tmp = strndup(key, length); |
360 | 12.9M | IGRAPH_CHECK_OOM(tmp, "Cannot get from trie."); |
361 | 12.9M | IGRAPH_FINALLY(igraph_free, tmp); |
362 | 12.9M | IGRAPH_CHECK(igraph_trie_get(t, tmp, id)); |
363 | 12.9M | IGRAPH_FREE(tmp); |
364 | 12.9M | IGRAPH_FINALLY_CLEAN(1); |
365 | | |
366 | 12.9M | return IGRAPH_SUCCESS; |
367 | 12.9M | } |
368 | | |
369 | | /** |
370 | | * \ingroup igraphtrie |
371 | | * \brief Search in a trie. |
372 | | * |
373 | | * This variant does not add \p key to the trie if it does not exist. |
374 | | * In this case, a negative \p id is returned. |
375 | | * |
376 | | * \param t The trie. |
377 | | * \param key The string to search for. |
378 | | * \param id If \p key is found, its index is stored here. Otherwise, |
379 | | * a negative value is returned. |
380 | | * \return Error code. |
381 | | */ |
382 | | |
383 | 4.40M | igraph_error_t igraph_trie_check(igraph_trie_t *t, const char *key, igraph_int_t *id) { |
384 | 4.40M | IGRAPH_CHECK(igraph_i_trie_get_node(&t->node, key, -1, id)); |
385 | 4.40M | return IGRAPH_SUCCESS; |
386 | 4.40M | } |
387 | | |
388 | | /** |
389 | | * \ingroup igraphtrie |
390 | | * \brief Gets an element of a trie based on its index. |
391 | | * |
392 | | * \param t The trie. |
393 | | * \param idx The index of the string. It is not checked that it is within range. |
394 | | * \return The string with the given index. If the trie does not store the keys for |
395 | | * reverse lookup, \c NULL is returned. |
396 | | */ |
397 | | |
398 | 0 | const char* igraph_trie_idx(igraph_trie_t *t, igraph_int_t idx) { |
399 | 0 | if (! t->storekeys) { |
400 | 0 | return NULL; |
401 | 0 | } |
402 | 0 | return igraph_strvector_get(&t->keys, idx); |
403 | 0 | } |
404 | | |
405 | | /** |
406 | | * \ingroup igraphtrie |
407 | | * \brief Returns the size of a trie. |
408 | | * |
409 | | * \param t The trie. |
410 | | * \return The size of the trie, i.e. one larger than the maximum index. |
411 | | */ |
412 | | |
413 | 19.6M | igraph_int_t igraph_trie_size(igraph_trie_t *t) { |
414 | 19.6M | return t->maxvalue + 1; |
415 | 19.6M | } |
416 | | |
417 | | /* Hmmm, very dirty.... */ |
418 | | |
419 | | /** |
420 | | * \ingroup igraphtrie |
421 | | * \brief Retrieves all the keys from the trie. |
422 | | * |
423 | | * </para><para> |
424 | | * Note that the returned pointer is a \em borrowed reference into the internal |
425 | | * string vector of the trie. Do \em not modify it and do \em not use it after |
426 | | * the trie was destroyed. |
427 | | * |
428 | | * \param t The trie. |
429 | | * \return The borrowed reference. |
430 | | */ |
431 | | |
432 | 25.7k | const igraph_strvector_t* igraph_i_trie_borrow_keys(igraph_trie_t *t) { |
433 | 25.7k | return &t->keys; |
434 | 25.7k | } |