triez 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ /*
2
+ * This file is part of hat-trie.
3
+ *
4
+ * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
5
+ *
6
+ *
7
+ * This is an implementation of the 'cache-conscious' hash tables described in,
8
+ *
9
+ * Askitis, N., & Zobel, J. (2005). Cache-conscious collision resolution in
10
+ * string hash tables. String Processing and Information Retrieval (pp.
11
+ * 91–102). Springer.
12
+ *
13
+ * Briefly, the idea is, as opposed to separate chaining with linked lists, to
14
+ * store keys contiguously in one big array, thereby improving the caching
15
+ * behavior, and reducing space requirments.
16
+ *
17
+ */
18
+
19
+ #ifndef HATTRIE_AHTABLE_H
20
+ #define HATTRIE_AHTABLE_H
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ #include <stdlib.h>
27
+ #include <stdbool.h>
28
+ #include "pstdint.h"
29
+ #include "common.h"
30
+
31
+ typedef unsigned char* slot_t;
32
+
33
+ typedef struct ahtable_t_
34
+ {
35
+ /* these fields are reserved for hattrie to fiddle with */
36
+ uint8_t flag;
37
+ unsigned char c0;
38
+ unsigned char c1;
39
+
40
+ size_t n; // number of slots
41
+ size_t m; // numbur of key/value pairs stored
42
+ size_t max_m; // number of stored keys before we resize
43
+
44
+ size_t* slot_sizes;
45
+ slot_t* slots;
46
+ } ahtable_t;
47
+
48
+ extern const double ahtable_max_load_factor;
49
+ extern const size_t ahtable_initial_size;
50
+
51
+ ahtable_t* ahtable_create (void); // Create an empty hash table.
52
+ ahtable_t* ahtable_create_n (size_t n); // Create an empty hash table, with
53
+ // n slots reserved.
54
+
55
+ void ahtable_free (ahtable_t*); // Free all memory used by a table.
56
+ void ahtable_clear (ahtable_t*); // Remove all entries.
57
+ size_t ahtable_size (const ahtable_t*); // Number of stored keys.
58
+
59
+
60
+ /** Find the given key in the table, inserting it if it does not exist, and
61
+ * returning a pointer to it's key.
62
+ *
63
+ * This pointer is not guaranteed to be valid after additional calls to
64
+ * ahtable_get, ahtable_del, ahtable_clear, or other functions that modifies the
65
+ * table.
66
+ */
67
+ value_t* ahtable_get (ahtable_t*, const char* key, size_t len);
68
+
69
+
70
+ /** Find a given key in the table, returning a NULL pointer if it does not
71
+ * exist. */
72
+ value_t* ahtable_tryget (ahtable_t*, const char* key, size_t len);
73
+
74
+
75
+ int ahtable_del(ahtable_t*, const char* key, size_t len);
76
+
77
+
78
+ typedef struct ahtable_iter_t_ ahtable_iter_t;
79
+
80
+ ahtable_iter_t* ahtable_iter_begin (const ahtable_t*, bool sorted);
81
+ void ahtable_iter_next (ahtable_iter_t*);
82
+ bool ahtable_iter_finished (ahtable_iter_t*);
83
+ void ahtable_iter_free (ahtable_iter_t*);
84
+ const char* ahtable_iter_key (ahtable_iter_t*, size_t* len);
85
+ value_t* ahtable_iter_val (ahtable_iter_t*);
86
+
87
+
88
+ #ifdef __cplusplus
89
+ }
90
+ #endif
91
+
92
+ #endif
93
+
@@ -0,0 +1,709 @@
1
+ /*
2
+ * This file is part of hat-trie.
3
+ *
4
+ * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
5
+ *
6
+ */
7
+
8
+ #include "hat-trie.h"
9
+ #include "ahtable.h"
10
+ #include "misc.h"
11
+ #include "pstdint.h"
12
+ #include <assert.h>
13
+ #include <string.h>
14
+
15
+ #define HT_UNUSED(x) x=x
16
+
17
+ /* maximum number of keys that may be stored in a bucket before it is burst */
18
+ static const size_t MAX_BUCKET_SIZE = 16384;
19
+ #define NODE_MAXCHAR 0xff // 0x7f for 7-bit ASCII
20
+ #define NODE_CHILDS (NODE_MAXCHAR+1)
21
+
22
+ static const uint8_t NODE_TYPE_TRIE = 0x1;
23
+ static const uint8_t NODE_TYPE_PURE_BUCKET = 0x2;
24
+ static const uint8_t NODE_TYPE_HYBRID_BUCKET = 0x4;
25
+ static const uint8_t NODE_HAS_VAL = 0x8;
26
+
27
+
28
+ struct trie_node_t_;
29
+
30
+ /* Node's may be trie nodes or buckets. This union allows us to keep
31
+ * non-specific pointer. */
32
+ typedef union node_ptr_
33
+ {
34
+ ahtable_t* b;
35
+ struct trie_node_t_* t;
36
+ uint8_t* flag;
37
+ } node_ptr;
38
+
39
+
40
+ typedef struct trie_node_t_
41
+ {
42
+ uint8_t flag;
43
+
44
+ /* the value for the key that is consumed on a trie node */
45
+ value_t val;
46
+
47
+ /* Map a character to either a trie_node_t or a ahtable_t. The first byte
48
+ * must be examined to determine which. */
49
+ node_ptr xs[NODE_CHILDS];
50
+
51
+ } trie_node_t;
52
+
53
+ struct hattrie_t_
54
+ {
55
+ node_ptr root; // root node
56
+ size_t m; // number of stored keys
57
+ };
58
+
59
+ /* Create a new trie node with all pointer pointing to the given child (which
60
+ * can be NULL). */
61
+ static trie_node_t* alloc_trie_node(hattrie_t* T, node_ptr child)
62
+ {
63
+ trie_node_t* node = malloc_or_die(sizeof(trie_node_t));
64
+ node->flag = NODE_TYPE_TRIE;
65
+ node->val = 0;
66
+
67
+ /* pass T to allow custom allocator for trie. */
68
+ HT_UNUSED(T); /* unused now */
69
+
70
+ size_t i;
71
+ for (i = 0; i < NODE_CHILDS; ++i) node->xs[i] = child;
72
+ return node;
73
+ }
74
+
75
+ /* iterate trie nodes until string is consumed or bucket is found */
76
+ static node_ptr hattrie_consume(node_ptr *p, const char **k, size_t *l, unsigned brk)
77
+ {
78
+ node_ptr node = p->t->xs[(unsigned char) **k];
79
+ while (*node.flag & NODE_TYPE_TRIE && *l > brk) {
80
+ ++*k;
81
+ --*l;
82
+ *p = node;
83
+ node = node.t->xs[(unsigned char) **k];
84
+ }
85
+
86
+ /* copy and writeback variables if it's faster */
87
+
88
+ assert(*p->flag & NODE_TYPE_TRIE);
89
+ return node;
90
+ }
91
+
92
+ /* use node value and return pointer to it */
93
+ static inline value_t* hattrie_useval(hattrie_t *T, node_ptr n)
94
+ {
95
+ if (!(n.t->flag & NODE_HAS_VAL)) {
96
+ n.t->flag |= NODE_HAS_VAL;
97
+ ++T->m;
98
+ }
99
+ return &n.t->val;
100
+ }
101
+
102
+ /* clear node value if exists */
103
+ static inline int hattrie_clrval(hattrie_t *T, node_ptr n)
104
+ {
105
+ if (n.t->flag & NODE_HAS_VAL) {
106
+ n.t->flag &= ~NODE_HAS_VAL;
107
+ n.t->val = 0;
108
+ --T->m;
109
+ return 0;
110
+ }
111
+ return -1;
112
+ }
113
+
114
+ /* find node in trie */
115
+ static node_ptr hattrie_find(hattrie_t* T, const char **key, size_t *len)
116
+ {
117
+ node_ptr parent = T->root;
118
+ assert(*parent.flag & NODE_TYPE_TRIE);
119
+
120
+ if (*len == 0) return parent;
121
+
122
+ node_ptr node = hattrie_consume(&parent, key, len, 1);
123
+
124
+ /* if the trie node consumes value, use it */
125
+ if (*node.flag & NODE_TYPE_TRIE) {
126
+ if (!(node.t->flag & NODE_HAS_VAL)) {
127
+ node.flag = NULL;
128
+ }
129
+ return node;
130
+ }
131
+
132
+ /* pure bucket holds only key suffixes, skip current char */
133
+ if (*node.flag & NODE_TYPE_PURE_BUCKET) {
134
+ *key += 1;
135
+ *len -= 1;
136
+ }
137
+
138
+ /* do not scan bucket, it's not needed for this operation */
139
+ return node;
140
+ }
141
+
142
+ hattrie_t* hattrie_create()
143
+ {
144
+ hattrie_t* T = malloc_or_die(sizeof(hattrie_t));
145
+ T->m = 0;
146
+
147
+ node_ptr node;
148
+ node.b = ahtable_create();
149
+ node.b->flag = NODE_TYPE_HYBRID_BUCKET;
150
+ node.b->c0 = 0x00;
151
+ node.b->c1 = NODE_MAXCHAR;
152
+ T->root.t = alloc_trie_node(T, node);
153
+
154
+ return T;
155
+ }
156
+
157
+
158
+ static void hattrie_free_node(node_ptr node)
159
+ {
160
+ if (*node.flag & NODE_TYPE_TRIE) {
161
+ size_t i;
162
+ for (i = 0; i < NODE_CHILDS; ++i) {
163
+ if (i > 0 && node.t->xs[i].t == node.t->xs[i - 1].t) continue;
164
+
165
+ /* XXX: recursion might not be the best choice here. It is possible
166
+ * to build a very deep trie. */
167
+ if (node.t->xs[i].t) hattrie_free_node(node.t->xs[i]);
168
+ }
169
+ free(node.t);
170
+ }
171
+ else {
172
+ ahtable_free(node.b);
173
+ }
174
+ }
175
+
176
+
177
+ void hattrie_free(hattrie_t* T)
178
+ {
179
+ hattrie_free_node(T->root);
180
+ free(T);
181
+ }
182
+
183
+
184
+ size_t hattrie_size(hattrie_t* T)
185
+ {
186
+ return T->m;
187
+ }
188
+
189
+
190
+ /* Perform one split operation on the given node with the given parent.
191
+ */
192
+ static void hattrie_split(hattrie_t* T, node_ptr parent, node_ptr node)
193
+ {
194
+ /* only buckets may be split */
195
+ assert(*node.flag & NODE_TYPE_PURE_BUCKET ||
196
+ *node.flag & NODE_TYPE_HYBRID_BUCKET);
197
+
198
+ assert(*parent.flag & NODE_TYPE_TRIE);
199
+
200
+ if (*node.flag & NODE_TYPE_PURE_BUCKET) {
201
+ /* turn the pure bucket into a hybrid bucket */
202
+ parent.t->xs[node.b->c0].t = alloc_trie_node(T, node);
203
+
204
+ /* if the bucket had an empty key, move it to the new trie node */
205
+ value_t* val = ahtable_tryget(node.b, NULL, 0);
206
+ if (val) {
207
+ parent.t->xs[node.b->c0].t->val = *val;
208
+ parent.t->xs[node.b->c0].t->flag |= NODE_HAS_VAL;
209
+ *val = 0;
210
+ ahtable_del(node.b, NULL, 0);
211
+ }
212
+
213
+ node.b->c0 = 0x00;
214
+ node.b->c1 = NODE_MAXCHAR;
215
+ node.b->flag = NODE_TYPE_HYBRID_BUCKET;
216
+
217
+ return;
218
+ }
219
+
220
+ /* This is a hybrid bucket. Perform a proper split. */
221
+
222
+ /* count the number of occourances of every leading character */
223
+ unsigned int cs[NODE_CHILDS]; // occurance count for leading chars
224
+ memset(cs, 0, NODE_CHILDS * sizeof(unsigned int));
225
+ size_t len;
226
+ const char* key;
227
+
228
+ ahtable_iter_t* i = ahtable_iter_begin(node.b, false);
229
+ while (!ahtable_iter_finished(i)) {
230
+ key = ahtable_iter_key(i, &len);
231
+ assert(len > 0);
232
+ cs[(unsigned char) key[0]] += 1;
233
+ ahtable_iter_next(i);
234
+ }
235
+ ahtable_iter_free(i);
236
+
237
+ /* choose a split point */
238
+ unsigned int left_m, right_m, all_m;
239
+ unsigned char j = node.b->c0;
240
+ all_m = ahtable_size(node.b);
241
+ left_m = cs[j];
242
+ right_m = all_m - left_m;
243
+ int d;
244
+
245
+ while (j + 1 < node.b->c1) {
246
+ d = abs((int) (left_m + cs[j + 1]) - (int) (right_m - cs[j + 1]));
247
+ if (d <= abs(left_m - right_m) && left_m + cs[j + 1] < all_m) {
248
+ j += 1;
249
+ left_m += cs[j];
250
+ right_m -= cs[j];
251
+ }
252
+ else break;
253
+ }
254
+
255
+ /* now split into two node cooresponding to ranges [0, j] and
256
+ * [j + 1, NODE_MAXCHAR], respectively. */
257
+
258
+
259
+ /* create new left and right nodes */
260
+
261
+ /* TODO: Add a special case if either node is a hybrid bucket containing all
262
+ * the keys. In such a case, do not build a new table, just use the old one.
263
+ * */
264
+ size_t num_slots;
265
+
266
+
267
+ for (num_slots = ahtable_initial_size;
268
+ (double) left_m > ahtable_max_load_factor * (double) num_slots;
269
+ num_slots *= 2);
270
+
271
+ node_ptr left, right;
272
+ left.b = ahtable_create_n(num_slots);
273
+ left.b->c0 = node.b->c0;
274
+ left.b->c1 = j;
275
+ left.b->flag = left.b->c0 == left.b->c1 ?
276
+ NODE_TYPE_PURE_BUCKET : NODE_TYPE_HYBRID_BUCKET;
277
+
278
+
279
+ for (num_slots = ahtable_initial_size;
280
+ (double) right_m > ahtable_max_load_factor * (double) num_slots;
281
+ num_slots *= 2);
282
+
283
+ right.b = ahtable_create_n(num_slots);
284
+ right.b->c0 = j + 1;
285
+ right.b->c1 = node.b->c1;
286
+ right.b->flag = right.b->c0 == right.b->c1 ?
287
+ NODE_TYPE_PURE_BUCKET : NODE_TYPE_HYBRID_BUCKET;
288
+
289
+
290
+ /* update the parent's pointer */
291
+
292
+ unsigned int c;
293
+ for (c = node.b->c0; c <= j; ++c) parent.t->xs[c] = left;
294
+ for (; c <= node.b->c1; ++c) parent.t->xs[c] = right;
295
+
296
+
297
+
298
+ /* distribute keys to the new left or right node */
299
+ value_t* u;
300
+ value_t* v;
301
+ i = ahtable_iter_begin(node.b, false);
302
+ while (!ahtable_iter_finished(i)) {
303
+ key = ahtable_iter_key(i, &len);
304
+ u = ahtable_iter_val(i);
305
+ assert(len > 0);
306
+
307
+ /* left */
308
+ if ((unsigned char) key[0] <= j) {
309
+ if (*left.flag & NODE_TYPE_PURE_BUCKET) {
310
+ v = ahtable_get(left.b, key + 1, len - 1);
311
+ }
312
+ else {
313
+ v = ahtable_get(left.b, key, len);
314
+ }
315
+ *v = *u;
316
+ }
317
+
318
+ /* right */
319
+ else {
320
+ if (*right.flag & NODE_TYPE_PURE_BUCKET) {
321
+ v = ahtable_get(right.b, key + 1, len - 1);
322
+ }
323
+ else {
324
+ v = ahtable_get(right.b, key, len);
325
+ }
326
+ *v = *u;
327
+ }
328
+
329
+ ahtable_iter_next(i);
330
+ }
331
+
332
+ ahtable_iter_free(i);
333
+ ahtable_free(node.b);
334
+ }
335
+
336
+ value_t* hattrie_get(hattrie_t* T, const char* key, size_t len)
337
+ {
338
+ node_ptr parent = T->root;
339
+ assert(*parent.flag & NODE_TYPE_TRIE);
340
+
341
+ if (len == 0) return &parent.t->val;
342
+
343
+ /* consume all trie nodes, now parent must be trie and child anything */
344
+ node_ptr node = hattrie_consume(&parent, &key, &len, 0);
345
+ assert(*parent.flag & NODE_TYPE_TRIE);
346
+
347
+ /* if the key has been consumed on a trie node, use its value */
348
+ if (len == 0) {
349
+ if (*node.flag & NODE_TYPE_TRIE) {
350
+ return hattrie_useval(T, node);
351
+ }
352
+ else if (*node.flag & NODE_TYPE_HYBRID_BUCKET) {
353
+ return hattrie_useval(T, parent);
354
+ }
355
+ }
356
+
357
+
358
+ /* preemptively split the bucket if it is full */
359
+ while (ahtable_size(node.b) >= MAX_BUCKET_SIZE) {
360
+ hattrie_split(T, parent, node);
361
+
362
+ /* after the split, the node pointer is invalidated, so we search from
363
+ * the parent again. */
364
+ node = hattrie_consume(&parent, &key, &len, 0);
365
+
366
+ /* if the key has been consumed on a trie node, use its value */
367
+ if (len == 0) {
368
+ if (*node.flag & NODE_TYPE_TRIE) {
369
+ return hattrie_useval(T, node);
370
+ }
371
+ else if (*node.flag & NODE_TYPE_HYBRID_BUCKET) {
372
+ return hattrie_useval(T, parent);
373
+ }
374
+ }
375
+ }
376
+
377
+ assert(*node.flag & NODE_TYPE_PURE_BUCKET || *node.flag & NODE_TYPE_HYBRID_BUCKET);
378
+
379
+ assert(len > 0);
380
+ size_t m_old = node.b->m;
381
+ value_t* val;
382
+ if (*node.flag & NODE_TYPE_PURE_BUCKET) {
383
+ val = ahtable_get(node.b, key + 1, len - 1);
384
+ }
385
+ else {
386
+ val = ahtable_get(node.b, key, len);
387
+ }
388
+ T->m += (node.b->m - m_old);
389
+
390
+ return val;
391
+ }
392
+
393
+
394
+ value_t* hattrie_tryget(hattrie_t* T, const char* key, size_t len)
395
+ {
396
+ /* find node for given key */
397
+ node_ptr node = hattrie_find(T, &key, &len);
398
+ if (node.flag == NULL) {
399
+ return NULL;
400
+ }
401
+
402
+ /* if the trie node consumes value, use it */
403
+ if (*node.flag & NODE_TYPE_TRIE) {
404
+ return &node.t->val;
405
+ }
406
+
407
+ return ahtable_tryget(node.b, key, len);
408
+ }
409
+
410
+
411
+ int hattrie_del(hattrie_t* T, const char* key, size_t len)
412
+ {
413
+ node_ptr parent = T->root;
414
+ assert(*parent.flag & NODE_TYPE_TRIE);
415
+
416
+ /* find node for deletion */
417
+ node_ptr node = hattrie_find(T, &key, &len);
418
+ if (node.flag == NULL) {
419
+ return -1;
420
+ }
421
+
422
+ /* if consumed on a trie node, clear the value */
423
+ if (*node.flag & NODE_TYPE_TRIE) {
424
+ return hattrie_clrval(T, node);
425
+ }
426
+
427
+ /* remove from bucket */
428
+ size_t m_old = ahtable_size(node.b);
429
+ int ret = ahtable_del(node.b, key, len);
430
+ T->m -= (m_old - ahtable_size(node.b));
431
+
432
+ /* merge empty buckets */
433
+ /*! \todo */
434
+
435
+ return ret;
436
+ }
437
+
438
+
439
+ /* plan for iteration:
440
+ * This is tricky, as we have no parent pointers currently, and I would like to
441
+ * avoid adding them. That means maintaining a stack
442
+ *
443
+ */
444
+
445
+ typedef struct hattrie_node_stack_t_
446
+ {
447
+ unsigned char c;
448
+ size_t level;
449
+
450
+ node_ptr node;
451
+ struct hattrie_node_stack_t_* next;
452
+
453
+ } hattrie_node_stack_t;
454
+
455
+
456
+ struct hattrie_iter_t_
457
+ {
458
+ char* key;
459
+ size_t keysize; // space reserved for the key
460
+ size_t level;
461
+
462
+ /* keep track of keys stored in trie nodes */
463
+ bool has_nil_key;
464
+ value_t nil_val;
465
+
466
+ const hattrie_t* T;
467
+ bool sorted;
468
+ ahtable_iter_t* i;
469
+ hattrie_node_stack_t* stack;
470
+
471
+ // subtree inside a table
472
+ // store remaining prefix for filtering nodes not matching it
473
+ char* prefix;
474
+ size_t prefix_len;
475
+ };
476
+
477
+
478
+ static void hattrie_iter_pushchar(hattrie_iter_t* i, size_t level, char c)
479
+ {
480
+ if (i->keysize < level) {
481
+ i->keysize *= 2;
482
+ i->key = realloc_or_die(i->key, i->keysize * sizeof(char));
483
+ }
484
+
485
+ if (level > 0) {
486
+ i->key[level - 1] = c;
487
+ }
488
+
489
+ i->level = level;
490
+ }
491
+
492
+
493
+ static void hattrie_iter_nextnode(hattrie_iter_t* i)
494
+ {
495
+ if (i->stack == NULL) return;
496
+
497
+ /* pop the stack */
498
+ node_ptr node;
499
+ hattrie_node_stack_t* next;
500
+ unsigned char c;
501
+ size_t level;
502
+
503
+ node = i->stack->node;
504
+ next = i->stack->next;
505
+ c = i->stack->c;
506
+ level = i->stack->level;
507
+
508
+ free(i->stack);
509
+ i->stack = next;
510
+
511
+ if (*node.flag & NODE_TYPE_TRIE) {
512
+ hattrie_iter_pushchar(i, level, c);
513
+
514
+ if(node.t->flag & NODE_HAS_VAL) {
515
+ i->has_nil_key = true;
516
+ i->nil_val = node.t->val;
517
+ }
518
+
519
+ /* push all child nodes from right to left */
520
+ int j;
521
+ for (j = NODE_MAXCHAR; j >= 0; --j) {
522
+
523
+ /* skip repeated pointers to hybrid bucket */
524
+ if (j < NODE_MAXCHAR && node.t->xs[j].t == node.t->xs[j + 1].t) continue;
525
+
526
+ // push stack
527
+ next = i->stack;
528
+ i->stack = malloc_or_die(sizeof(hattrie_node_stack_t));
529
+ i->stack->node = node.t->xs[j];
530
+ i->stack->next = next;
531
+ i->stack->level = level + 1;
532
+ i->stack->c = (unsigned char) j;
533
+ }
534
+ }
535
+ else {
536
+ if (*node.flag & NODE_TYPE_PURE_BUCKET) {
537
+ hattrie_iter_pushchar(i, level, c);
538
+ }
539
+ else if (level) {
540
+ i->level = level - 1;
541
+ }
542
+ i->i = ahtable_iter_begin(node.b, i->sorted);
543
+
544
+ }
545
+ }
546
+
547
+
548
+ // TODO pick a better name
549
+ static void hattrie_iter_step(hattrie_iter_t* i)
550
+ {
551
+ while (((i->i == NULL || ahtable_iter_finished(i->i)) && !i->has_nil_key) &&
552
+ i->stack != NULL ) {
553
+
554
+ ahtable_iter_free(i->i);
555
+ i->i = NULL;
556
+ hattrie_iter_nextnode(i);
557
+ }
558
+
559
+ if (i->i != NULL && ahtable_iter_finished(i->i)) {
560
+ ahtable_iter_free(i->i);
561
+ i->i = NULL;
562
+ }
563
+ }
564
+
565
+ static bool hattrie_iter_prefix_not_match(hattrie_iter_t* i)
566
+ {
567
+ if (hattrie_iter_finished(i)) {
568
+ return false; // can not advance the iter
569
+ }
570
+ if (i->level >= i->prefix_len) {
571
+ return memcmp(i->key, i->prefix, i->prefix_len);
572
+ } else if (i->has_nil_key) {
573
+ return true; // subkey too short
574
+ }
575
+
576
+ size_t sublen;
577
+ const char* subkey;
578
+ subkey = ahtable_iter_key(i->i, &sublen);
579
+ if (i->level + sublen < i->prefix_len) {
580
+ return true; // subkey too short
581
+ }
582
+ return memcmp(i->key, i->prefix, i->level) ||
583
+ memcmp(subkey, i->prefix + i->level, (i->prefix_len - i->level));
584
+ }
585
+
586
+
587
+ hattrie_iter_t* hattrie_iter_begin(const hattrie_t* T, bool sorted)
588
+ {
589
+ return hattrie_iter_with_prefix(T, sorted, NULL, 0);
590
+ }
591
+
592
+
593
+ hattrie_iter_t* hattrie_iter_with_prefix(const hattrie_t* T, bool sorted, const char* prefix, size_t prefix_len)
594
+ {
595
+ node_ptr node = hattrie_find((hattrie_t*)T, &prefix, &prefix_len);
596
+
597
+ hattrie_iter_t* i = malloc_or_die(sizeof(hattrie_iter_t));
598
+ i->T = T;
599
+ i->sorted = sorted;
600
+ i->i = NULL;
601
+ i->keysize = 16;
602
+ i->key = malloc_or_die(i->keysize * sizeof(char));
603
+ i->level = 0;
604
+ i->has_nil_key = false;
605
+ i->nil_val = 0;
606
+
607
+ i->prefix_len = prefix_len;
608
+ if (prefix_len) {
609
+ i->prefix = (char*)malloc_or_die(prefix_len);
610
+ memcpy(i->prefix, prefix, prefix_len);
611
+ } else {
612
+ i->prefix = NULL;
613
+ }
614
+
615
+ i->stack = malloc_or_die(sizeof(hattrie_node_stack_t));
616
+ i->stack->next = NULL;
617
+ i->stack->node = node;
618
+ i->stack->c = '\0';
619
+ i->stack->level = 0;
620
+
621
+ hattrie_iter_step(i);
622
+ if (i->prefix_len && hattrie_iter_prefix_not_match(i)) {
623
+ hattrie_iter_next(i);
624
+ }
625
+
626
+ return i;
627
+ }
628
+
629
+
630
+ void hattrie_iter_next(hattrie_iter_t* i)
631
+ {
632
+ do {
633
+ if (hattrie_iter_finished(i)) return;
634
+
635
+ if (i->i != NULL && !ahtable_iter_finished(i->i)) {
636
+ ahtable_iter_next(i->i);
637
+ }
638
+ else if (i->has_nil_key) {
639
+ i->has_nil_key = false;
640
+ i->nil_val = 0;
641
+ hattrie_iter_nextnode(i);
642
+ }
643
+
644
+ hattrie_iter_step(i);
645
+ } while (i->prefix_len && hattrie_iter_prefix_not_match(i));
646
+ }
647
+
648
+
649
+ bool hattrie_iter_finished(hattrie_iter_t* i)
650
+ {
651
+ return i->stack == NULL && i->i == NULL && !i->has_nil_key;
652
+ }
653
+
654
+
655
+ void hattrie_iter_free(hattrie_iter_t* i)
656
+ {
657
+ if (i == NULL) return;
658
+ if (i->i) ahtable_iter_free(i->i);
659
+
660
+ hattrie_node_stack_t* next;
661
+ while (i->stack) {
662
+ next = i->stack->next;
663
+ free(i->stack);
664
+ i->stack = next;
665
+ }
666
+
667
+ if (i->prefix_len) {
668
+ free(i->prefix);
669
+ }
670
+
671
+ free(i->key);
672
+ free(i);
673
+ }
674
+
675
+
676
+ const char* hattrie_iter_key(hattrie_iter_t* i, size_t* len)
677
+ {
678
+ if (hattrie_iter_finished(i)) return NULL;
679
+
680
+ size_t sublen;
681
+ const char* subkey;
682
+
683
+ if (i->has_nil_key) {
684
+ subkey = NULL;
685
+ sublen = 0;
686
+ }
687
+ else subkey = ahtable_iter_key(i->i, &sublen);
688
+
689
+ if (i->keysize < i->level + sublen + 1) {
690
+ while (i->keysize < i->level + sublen + 1) i->keysize *= 2;
691
+ i->key = realloc_or_die(i->key, i->keysize * sizeof(char));
692
+ }
693
+
694
+ memcpy(i->key + i->level, subkey, sublen);
695
+ i->key[i->level + sublen] = '\0';
696
+
697
+ *len = i->level + sublen - i->prefix_len;
698
+ return i->key + i->prefix_len;
699
+ }
700
+
701
+
702
+ value_t* hattrie_iter_val(hattrie_iter_t* i)
703
+ {
704
+ if (i->has_nil_key) return &i->nil_val;
705
+
706
+ if (hattrie_iter_finished(i)) return NULL;
707
+
708
+ return ahtable_iter_val(i->i);
709
+ }