-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcritbit.c
311 lines (262 loc) · 6.23 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
/*
* critbit89 - A crit-bit tree implementation for strings in C89
* Written by Jonas Gehring <[email protected]>
*/
/*
* The code makes the assumption that malloc returns pointers aligned at at
* least a two-byte boundary. Since the C standard requires that malloc return
* pointers that can store any type, there are no commonly-used toolchains for
* which this assumption is false.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "critbit.h"
#ifdef _MSC_VER /* MSVC */
typedef unsigned __int8 uint8_t;
typedef unsigned __int32 uint32_t;
#ifdef _WIN64
typedef signed __int64 intptr_t;
#else
typedef _W64 signed int intptr_t;
#endif
#else /* Not MSVC */
#include <stdint.h>
#endif
typedef struct {
void *child[2];
uint32_t byte;
uint8_t otherbits;
} cb_node_t;
/* Standard memory allocation functions */
static void *malloc_std(size_t size, void *baton) {
(void)baton; /* Prevent compiler warnings */
return malloc(size);
}
static void free_std(void *ptr, void *baton) {
(void)baton; /* Prevent compiler warnings */
free(ptr);
}
/* Static helper functions */
static void cbt_traverse_delete(cb_tree_t *tree, void *top)
{
uint8_t *p = top;
if (1 & (intptr_t)p) {
cb_node_t *q = (void *)(p - 1);
cbt_traverse_delete(tree, q->child[0]);
cbt_traverse_delete(tree, q->child[1]);
tree->free(q, tree->baton);
} else {
tree->free(p, tree->baton);
}
}
static int cbt_traverse_prefixed(uint8_t *top,
int (*callback)(const char *, void *), void *baton)
{
if (1 & (intptr_t)top) {
cb_node_t *q = (void *)(top - 1);
int ret = 0;
ret = cbt_traverse_prefixed(q->child[0], callback, baton);
if (ret != 0) {
return ret;
}
ret = cbt_traverse_prefixed(q->child[1], callback, baton);
if (ret != 0) {
return ret;
}
return 0;
}
return (callback)((const char *)top, baton);
}
/*! Creates a new, empty critbit tree */
cb_tree_t cb_tree_make()
{
cb_tree_t tree;
tree.root = NULL;
tree.malloc = &malloc_std;
tree.free = &free_std;
tree.baton = NULL;
return tree;
}
/*! Returns non-zero if tree contains str */
int cb_tree_contains(cb_tree_t *tree, const char *str)
{
const uint8_t *ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = tree->root;
if (p == NULL) {
return 0;
}
while (1 & (intptr_t)p) {
cb_node_t *q = (void *)(p - 1);
uint8_t c = 0;
int direction;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
return (strcmp(str, (const char *)p) == 0);
}
/*! Inserts str into tree, returns 0 on success */
int cb_tree_insert(cb_tree_t *tree, const char *str)
{
const uint8_t *const ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = tree->root;
uint8_t c, *x;
uint32_t newbyte;
uint32_t newotherbits;
int direction, newdirection;
cb_node_t *newnode;
void **wherep;
if (p == NULL) {
x = tree->malloc(ulen + 1, tree->baton);
if (x == NULL) {
return ENOMEM;
}
memcpy(x, str, ulen + 1);
tree->root = x;
return 0;
}
while (1 & (intptr_t)p) {
cb_node_t *q = (void *)(p - 1);
c = 0;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
for (newbyte = 0; newbyte < ulen; ++newbyte) {
if (p[newbyte] != ubytes[newbyte]) {
newotherbits = p[newbyte] ^ ubytes[newbyte];
goto different_byte_found;
}
}
if (p[newbyte] != 0) {
newotherbits = p[newbyte];
goto different_byte_found;
}
return 1;
different_byte_found:
newotherbits |= newotherbits >> 1;
newotherbits |= newotherbits >> 2;
newotherbits |= newotherbits >> 4;
newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255;
c = p[newbyte];
newdirection = (1 + (newotherbits | c)) >> 8;
newnode = tree->malloc(sizeof(cb_node_t), tree->baton);
if (newnode == NULL) {
return ENOMEM;
}
x = tree->malloc(ulen + 1, tree->baton);
if (x == NULL) {
tree->free(newnode, tree->baton);
return ENOMEM;
}
memcpy(x, ubytes, ulen + 1);
newnode->byte = newbyte;
newnode->otherbits = newotherbits;
newnode->child[1 - newdirection] = x;
/* Insert into tree */
wherep = &tree->root;
for (;;) {
cb_node_t *q;
p = *wherep;
if (!(1 & (intptr_t)p)) {
break;
}
q = (void *)(p - 1);
if (q->byte > newbyte) {
break;
}
if (q->byte == newbyte && q->otherbits > newotherbits) {
break;
}
c = 0;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
}
newnode->child[newdirection] = *wherep;
*wherep = (void *)(1 + (char *)newnode);
return 0;
}
/*! Deletes str from the tree, returns 0 on success */
int cb_tree_delete(cb_tree_t *tree, const char *str)
{
const uint8_t *ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = tree->root;
void **wherep = 0, **whereq = 0;
cb_node_t *q = 0;
int direction = 0;
if (tree->root == NULL) {
return 1;
}
wherep = &tree->root;
while (1 & (intptr_t)p) {
uint8_t c = 0;
whereq = wherep;
q = (void *)(p - 1);
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
p = *wherep;
}
if (strcmp(str, (const char *)p) != 0) {
return 1;
}
tree->free(p, tree->baton);
if (!whereq) {
tree->root = NULL;
return 0;
}
*whereq = q->child[1 - direction];
tree->free(q, tree->baton);
return 0;
}
/*! Clears the given tree */
void cb_tree_clear(cb_tree_t *tree)
{
if (tree->root) {
cbt_traverse_delete(tree, tree->root);
}
tree->root = NULL;
}
/*! Calls callback for all strings in tree with the given prefix */
int cb_tree_walk_prefixed(cb_tree_t *tree, const char *prefix,
int (*callback)(const char *, void *), void *baton)
{
const uint8_t *ubytes = (void *)prefix;
const size_t ulen = strlen(prefix);
uint8_t *p = tree->root;
uint8_t *top = p;
if (p == NULL) {
return 0;
}
while (1 & (intptr_t)p) {
cb_node_t *q = (void *)(p - 1);
uint8_t c = 0;
int direction;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
if (q->byte < ulen) {
top = p;
}
}
if (strlen((const char *)p) < ulen || memcmp(p, prefix, ulen) != 0) {
/* No strings match */
return 0;
}
return cbt_traverse_prefixed(top, callback, baton);
}