-
Notifications
You must be signed in to change notification settings - Fork 4
/
critbit.c
375 lines (338 loc) · 11.5 KB
/
critbit.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright (c) 2012, Enno Rehling <enno@eressea.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include "critbit.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* see http://cr.yp.to/critbit.html */
struct critbit_node {
void * child[2];
size_t byte;
unsigned int mask;
};
#define EXTERNAL_NODE 0
#define INTERNAL_NODE 1
static int decode_pointer(void ** ptr)
{
ptrdiff_t numvalue = (char*)*ptr - (char*)0;
if (numvalue & 1) {
*ptr = (void*)(numvalue - 1);
return EXTERNAL_NODE;
}
return INTERNAL_NODE;
}
static void * make_external_node(const void * key, size_t keylen)
{
char * data = (char *)malloc(sizeof(size_t) + keylen);
#ifndef NDEBUG
ptrdiff_t numvalue = (char *)data - (char*)0;
assert((numvalue & 1) == 0);
#endif
assert(keylen > 0);
if (!data) abort();
memcpy(data, &keylen, sizeof(size_t));
memcpy(data + sizeof(size_t), key, keylen);
return (void*)(data + 1);
}
static void from_external_node(void * ptr, void **key, size_t *keylen)
{
unsigned char * bytes = (unsigned char *)ptr;
#ifndef NDEBUG
ptrdiff_t numvalue = bytes - (unsigned char*)0;
assert(numvalue && (numvalue & 1) == 0);
assert(key && keylen);
#endif
memcpy(keylen, bytes, sizeof(size_t));
*key = bytes + sizeof(size_t);
}
static struct critbit_node * make_internal_node(void)
{
struct critbit_node *node = (struct critbit_node *)malloc(sizeof(struct critbit_node));
return node;
}
static void cb_free_node(void * ptr)
{
if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * node = (struct critbit_node *)ptr;
cb_free_node(node->child[0]);
cb_free_node(node->child[1]);
free(node);
}
else {
free(ptr);
}
}
void cb_clear(critbit_tree * cb)
{
if (cb->root) {
cb_free_node(cb->root);
cb->root = 0;
}
}
static int cb_less(const struct critbit_node * a, const struct critbit_node * b)
{
return a->byte < b->byte || (a->byte == b->byte && a->mask < b->mask);
}
int cb_insert(critbit_tree * cb, const void * key, size_t keylen)
{
assert(cb);
assert(key);
if (!cb->root) {
cb->root = make_external_node(key, keylen);
return CB_SUCCESS;
}
else {
void ** iter = &cb->root;
struct critbit_node * prev = NULL;
for (;;) {
void * ptr = *iter;
if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * node = (struct critbit_node *)ptr;
unsigned char * bytes = (unsigned char *)key;
int branch = (keylen <= node->byte) ? 0 : ((1 + ((bytes[node->byte] | node->mask) & 0xFF)) >> 8);
iter = &node->child[branch];
prev = node;
}
else {
unsigned char *iptr, *bytes = (unsigned char *)key, *ikey = bytes;
void * vptr;
unsigned int mask, byte = 0;
int branch;
size_t len;
struct critbit_node * node;
from_external_node(ptr, &vptr, &len);
for (iptr = vptr; byte < keylen && byte < len && *ikey == *iptr;) {
++ikey;
++iptr;
++byte;
}
if (byte == keylen && byte == len) {
return CB_EXISTS; /* duplicate entry */
}
node = make_internal_node();
node->byte = byte;
mask = *ikey ^ *iptr; /* these are all the bits that differ */
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4; /* now, every bit up to the MSB is set to 1 */
mask = (mask&~(mask >> 1)) ^ 0xFF;
node->mask = (unsigned char)mask;
/* find the right place to insert, iff prev's crit-bit is later in the string than new crit-bit */
if (prev && cb_less(node, prev)) {
for (iter = &cb->root;;) {
ptr = *iter;
if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * next = (struct critbit_node *)ptr;
if (cb_less(next, node)) {
branch = ((1 + ((bytes[next->byte] | next->mask) & 0xFF)) >> 8);
iter = &next->child[branch];
}
else {
break;
}
}
else {
assert(!"should never get here");
}
}
}
branch = ((1 + ((*ikey | node->mask) & 0xFF)) >> 8);
node->child[branch] = make_external_node(key, keylen);
node->child[1 - branch] = *iter;
*iter = (void *)node;
return CB_SUCCESS;
}
}
}
}
static void * cb_find_top_i(const critbit_tree * cb, const void * key, size_t keylen)
{
void *ptr, *top = NULL;
assert(keylen==0 || key);
if (!cb->root) {
return 0;
}
for (ptr = cb->root, top = cb->root;;) {
void * last = ptr;
if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * node = (struct critbit_node *)ptr;
int branch;
if (keylen <= node->byte) {
break;
}
else {
unsigned char * bytes = (unsigned char *)key;
top = last;
branch = (1 + ((bytes[node->byte] | node->mask) & 0xFF)) >> 8;
ptr = node->child[branch];
}
}
else {
/* we reached an external node before exhausting the key length */
top = last;
break;
}
}
return top;
}
static int cb_find_prefix_i(void * ptr, const void * key, size_t keylen, void ** results, int numresults, int * offset, int next)
{
assert(next <= numresults);
if (next == numresults) {
return next;
}
else if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * node = (struct critbit_node *)ptr;
next = cb_find_prefix_i(node->child[0], key, keylen, results, numresults, offset, next);
if (next < numresults) {
next = cb_find_prefix_i(node->child[1], key, keylen, results, numresults, offset, next);
}
}
else {
/* reached an external node */
char * str;
void * vptr;
size_t len;
from_external_node(ptr, &vptr, &len);
str = vptr;
if (len >= keylen && memcmp(key, str, keylen) == 0) {
if (*offset>0) {
--*offset;
}
else {
results[next++] = str;
}
}
}
return next;
}
int cb_find_prefix(const critbit_tree * cb, const void * key, size_t keylen, void ** results, int numresults, int offset)
{
if (numresults > 0) {
void *top = cb_find_top_i(cb, key, keylen);
if (top) {
/* recursively add all children except the ones from [0-offset) of top to the results */
return cb_find_prefix_i(top, key, keylen, results, numresults, &offset, 0);
}
}
return 0;
}
static int cb_foreach_i(void * ptr, const void * key, size_t keylen, int(*match_cb)(void * match, const void * key, size_t keylen, void *), void *data)
{
int result = 0;
if (decode_pointer(&ptr) == INTERNAL_NODE) {
struct critbit_node * node = (struct critbit_node *)ptr;
result = cb_foreach_i(node->child[0], key, keylen, match_cb, data);
if (!result) {
result = cb_foreach_i(node->child[1], key, keylen, match_cb, data);
}
}
else {
/* reached an external node */
void * match;
size_t len;
from_external_node(ptr, &match, &len);
if (len >= keylen && memcmp(key, match, keylen) == 0) {
return match_cb(match, key, keylen, data);
}
}
return result;
}
int cb_foreach(critbit_tree * cb, const void * key, size_t keylen, int(*match_cb)(void * match, const void * key, size_t keylen, void *), void *data)
{
void *top = cb_find_top_i(cb, key, keylen);
if (top) {
/* recursively add all children except the ones from [0-offset) of top to the results */
return cb_foreach_i(top, key, keylen, match_cb, data);
}
return 0;
}
const void * cb_find(critbit_tree * cb, const void * key, size_t keylen)
{
void * str;
size_t len;
unsigned char * bytes = (unsigned char *)key;
void * ptr;
assert(cb);
assert(key);
if (!cb->root) return 0;
for (ptr = cb->root; decode_pointer(&ptr) == INTERNAL_NODE;) {
struct critbit_node * node = (struct critbit_node *)ptr;
int branch = (keylen <= node->byte) ? 0 : ((1 + ((bytes[node->byte] | node->mask) & 0xFF)) >> 8);
ptr = node->child[branch];
}
from_external_node(ptr, &str, &len);
if (len >= keylen && memcmp(key, str, keylen) == 0) {
return str;
}
return 0;
}
int cb_erase(critbit_tree * cb, const void * key, size_t keylen)
{
void **iter = NULL;
void *ptr = cb->root;
struct critbit_node *parent = NULL;
unsigned char * bytes = (unsigned char *)key;
int branch = 0;
if (!cb->root) return CB_NOTFOUND;
for (;;) {
int type;
type = decode_pointer(&ptr);
if (type == INTERNAL_NODE) {
iter = parent ? &parent->child[branch] : &cb->root;
parent = (struct critbit_node*)ptr;
branch = (keylen <= parent->byte) ? 0 : ((1 + ((bytes[parent->byte] | parent->mask) & 0xFF)) >> 8);
ptr = parent->child[branch];
}
else {
void * str;
size_t len;
from_external_node(ptr, &str, &len);
if (len == keylen && memcmp(key, str, len) == 0) {
free(ptr);
if (iter) {
*iter = parent->child[1 - branch];
free(parent);
}
else {
cb->root = 0;
}
return CB_SUCCESS;
}
return CB_NOTFOUND;
}
}
}
size_t cb_new_kv(const char *key, size_t keylen, const void * value, size_t len, void * out)
{
char * dst = (char*)out;
if (dst != key) {
memmove(dst, key, keylen);
}
dst[keylen] = 0;
memmove(dst + keylen + 1, value, len);
return len + keylen + 1;
}
void cb_get_kv(const void *kv, void * value, size_t len)
{
const char * key = (const char *)kv;
size_t keylen = strlen(key) + 1;
memmove(value, key + keylen, len);
}
void cb_get_kv_ex(void *kv, void ** value)
{
char * key = (char *)kv;
size_t keylen = strlen(key) + 1;
*value = key + keylen;
}