forked from tobez/validns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbtree.c
392 lines (329 loc) · 8.56 KB
/
cbtree.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* djb's critbit with associated data storage.
*
* Based on https://github.com/agl/critbit, which is in public domain.
* Changes:
* - data storage added
* - cweb removed
* - functions renamed and data types cleaned to my liking
*/
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "cbtree.h"
struct node {
void *child[2];
uint32_t byte;
uint8_t otherbits;
};
/* our own memory management */
struct pool
{
struct pool *next;
size_t pool_size;
size_t free_index;
char mem[0];
};
static struct pool *internal = NULL;
static struct pool *external = NULL;
static int new_pool(struct pool **root, size_t size)
{
struct pool *pool;
size = (size + sizeof(void *) - 1) / sizeof(void *);
size *= sizeof(void *);
pool = malloc(size + sizeof(struct pool));
if (!pool) {
return 1;
}
pool->next = *root;
pool->free_index = 0;
pool->pool_size = size;
*root = pool;
return 0;
}
static void *alloc(struct pool **root, size_t size)
{
void *ret;
size = (size + sizeof(void *) - 1) / sizeof(void *);
size *= sizeof(void *);
if (!*root)
if (new_pool(root, size > 256000 ? size : 256000) != 0)
return NULL;
if ((*root)->pool_size - (*root)->free_index < size)
if (new_pool(root, size > 256000 ? size : 256000) != 0)
return NULL;
ret = (*root)->mem + (*root)->free_index;
(*root)->free_index += size;
return ret;
}
/* main code */
intptr_t*
cbtree_find(struct cbtree *t, const char *u)
{
const uint8_t *ubytes = (void *)u;
const size_t ulen = strlen(u);
uint8_t *p = t->root;
/* Test for empty tree */
if (!p) return NULL;
/* Walk tree for best member */
while (1 & (intptr_t) p) {
struct node *q = (void *)(p - 1);
/* Calculate direction */
int direction;
uint8_t c = 0;
if (q->byte < ulen)
c = ubytes[q->byte];
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
/* The leaves contain "[data ptr][string]" */
if (strcmp(u, (const char *)(p + sizeof(intptr_t))) == 0)
return (intptr_t *)p;
return NULL;
}
intptr_t*
cbtree_insert(struct cbtree *t, const char *u)
{
const uint8_t *const ubytes = (void *) u;
const size_t ulen = strlen(u);
uint8_t *p = t->root;
/* Deal with inserting into an empty tree */
if (!p) {
char *x = alloc(&external, ulen + 1 + sizeof(intptr_t));
if (!x)
return NULL;
*((intptr_t *)x) = 0;
memcpy(x + sizeof(intptr_t), u, ulen + 1);
t->root = x;
return (intptr_t *)x;
}
/* Walk tree for best member */
while (1 & (intptr_t) p) {
struct node *q = (void *)(p - 1);
/* Calculate direction */
int direction;
uint8_t c = 0;
if (q->byte < ulen)
c = ubytes[q->byte];
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
/* Find the critical bit */
/* 1: Find differing byte */
uint32_t newbyte;
uint32_t newotherbits;
for (newbyte = 0; newbyte < ulen; ++newbyte) {
if (p[sizeof(intptr_t) + newbyte] != ubytes[newbyte]) {
newotherbits = p[sizeof(intptr_t) + newbyte] ^ ubytes[newbyte];
goto different_byte_found;
}
}
if (p[sizeof(intptr_t) + newbyte] != 0) {
newotherbits = p[sizeof(intptr_t) + newbyte];
goto different_byte_found;
}
return (intptr_t *)p;
different_byte_found:
/* 2: Find differing bit */
newotherbits |= newotherbits >> 1;
newotherbits |= newotherbits >> 2;
newotherbits |= newotherbits >> 4;
newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255;
uint8_t c = p[sizeof(intptr_t) + newbyte];
int newdirection = (1 + (newotherbits | c)) >> 8;
/* Insert new string */
/* 1: Allocate new node structure */
struct node *newnode;
newnode = alloc(&internal, sizeof(struct node));
if (!newnode)
return NULL;
char *x = alloc(&external, ulen + 1 + sizeof(intptr_t));
if (!x)
return NULL;
*((intptr_t *)x) = 0;
memcpy(x + sizeof(intptr_t), ubytes, ulen + 1);
newnode->byte = newbyte;
newnode->otherbits = newotherbits;
newnode->child[1 - newdirection] = x;
/* 2: Insert new node */
void **wherep = &t->root;
for (;;) {
uint8_t *p = *wherep;
if (!(1 & (intptr_t) p)) break;
struct node *q = (void *) (p - 1);
if (q->byte > newbyte) break;
if (q->byte == newbyte && q->otherbits > newotherbits) break;
uint8_t c = 0;
if (q->byte < ulen) c = ubytes[q->byte];
const int direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
}
newnode->child[newdirection] = *wherep;
*wherep = (void *) (1 + (char *) newnode);
return (intptr_t *)x;
}
intptr_t
cbtree_delete(struct cbtree *t, const char *u)
{
const uint8_t *ubytes = (void *) u;
const size_t ulen = strlen(u);
uint8_t *p = t->root;
void **wherep = &t->root;
void **whereq = 0;
struct node *q = 0;
int direction = 0;
intptr_t ret;
/* Deal with deleting from an empty tree */
if (!p) return 0;
/* Walk the tree for the best match */
while (1 & (intptr_t) p) {
whereq = wherep;
q = (void *) (p - 1);
uint8_t c = 0;
if (q->byte < ulen) c = ubytes[q->byte];
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
p = *wherep;
}
/* Check the best match */
if (0 != strcmp(u, (const char *)(p + sizeof(intptr_t))))
return 0;
ret = *((intptr_t *)p);
/* Remove the element and/or node */
if (!whereq) {
t->root = 0;
return ret;
}
*whereq = q->child[1 - direction];
// free(q);
return ret;
}
static void
traverse(void *top) {
uint8_t *p = top;
if (1 & (intptr_t) p) {
struct node *q = (void *) (p - 1);
traverse(q->child[0]);
traverse(q->child[1]);
// free(q);
} else {
// free(p);
}
}
void
cbtree_clear(struct cbtree *t)
{
if (t->root) traverse(t->root);
t->root = NULL;
}
static int
allprefixed_traverse(uint8_t *top, int (*handle)(const char *, intptr_t *, void *), void *arg)
{
int direction;
/* Deal with an internal node */
if (1 & (intptr_t) top) {
struct node *q = (void *) (top - 1);
for (direction = 0; direction < 2; ++direction)
switch(allprefixed_traverse(q->child[direction], handle, arg)) {
case 1: break;
case 0: return 0;
default: return -1;
}
return 1;
}
/* Deal with an external node */
return handle((const char *)(top + sizeof(intptr_t)), (intptr_t *)top, arg);
}
int
cbtree_allprefixed(struct cbtree *t, const char *prefix,
int (*handle)(const char *, intptr_t *, void *),
void *arg)
{
const uint8_t *ubytes = (void *) prefix;
const size_t ulen = strlen(prefix);
uint8_t *p = t->root;
uint8_t *top = p;
int i;
if (!p) return 1; /* S = $\emptyset$ */
/* Walk tree, maintaining top pointer */
while (1 & (intptr_t) p) {
struct node *q = (void *) (p - 1);
uint8_t c = 0;
if (q->byte < ulen) c = ubytes[q->byte];
const int direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
if (q->byte < ulen) top = p;
}
/* Check prefix */
for (i = 0; i < ulen; ++i) {
if (p[i+sizeof(intptr_t)] != ubytes[i]) return 1;
}
return allprefixed_traverse(top, handle, arg);
}
static const char *byte_to_binary(int x)
{
static char b[9];
int z;
b[0] = '\0';
for (z = 128; z > 0; z >>= 1)
strcat(b, ((x & z) == z) ? "1" : "0");
return b;
}
static void
traverse_dump(void *top, int level, int byte)
{
uint8_t *p = top;
int i;
for (i = 0; i < level; i++) printf(" ");
if (1 & (intptr_t) p) {
struct node *q = (void *) (p - 1);
printf("[byte(%d),otherbits(%s)]\n", q->byte, byte_to_binary(q->otherbits));
traverse_dump(q->child[0], level + 1, q->byte);
traverse_dump(q->child[1], level + 1, q->byte);
} else {
const size_t ulen = strlen((char *)(p + sizeof(intptr_t)));
int c = byte < ulen ? p[sizeof(intptr_t) + byte] : 0;
printf("\"%s\" (%s)\n", p + sizeof(intptr_t), byte_to_binary(c));
}
}
void
cbtree_dump(struct cbtree *t)
{
if (t->root)
traverse_dump(t->root, 0, 0);
printf("\n");
}
char*
cbtree_next(struct cbtree *t, const char *u, intptr_t *data)
{
const uint8_t *ubytes = (void *) u;
const size_t ulen = strlen(u);
uint8_t *p = t->root;
uint8_t *branch = NULL;
if (!p) return NULL;
/* Walk tree, maintaining top pointer */
while (1 & (intptr_t) p) {
struct node *q = (void *) (p - 1);
uint8_t c = 0;
if (q->byte < ulen) c = ubytes[q->byte];
const int direction = (1 + (q->otherbits | c)) >> 8;
if (direction == 0)
branch = q->child[1];
p = q->child[direction];
}
/* check whether what we found is what we are looking for already */
if (strcmp((char *)(p + sizeof(intptr_t)), u) > 0) {
if (data) *data = *((intptr_t *)p);
return (char *)(p + sizeof(intptr_t));
}
if (!branch) return NULL;
/* select the lowest value on the branch */
p = branch;
while (1 & (intptr_t) p) {
struct node *q = (void *) (p - 1);
p = q->child[0];
}
if (data) *data = *((intptr_t *)p);
return (char *)(p + sizeof(intptr_t));
}