-
Notifications
You must be signed in to change notification settings - Fork 0
/
critbit.c
executable file
·257 lines (223 loc) · 5.76 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
/*
* This code is taken from Dan Bernstein's qhasm and implements a binary
* crit-bit (alsa known as PATRICIA) tree for |NUL| terminated strings. Crit-bit
* trees are underused and it's this author's hope that a good example will aid
* their adoption.
*/
#define _POSIX_C_SOURCE 200112
#define uint8 uint8_t
#define uint32 uint32_t
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
typedef struct {
void *child[2];
uint32 byte;
uint8 otherbits;
} critbit0_node;
typedef struct {
void *root;
} critbit0_tree;
int
critbit0_contains (critbit0_tree * t, const char *u, void *opq)
{
const uint8 *ubytes = (void *) u;
const size_t ulen = strlen (u);
uint8 *p = t->root;
if (!p)
return 0;
while (1 & (intptr_t) p) {
critbit0_node *q = (void *) (p - 1);
uint8 c = 0;
if (q->byte < ulen)
c = ubytes[q->byte];
const int direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
if (0 == strcmp (u, (const char *) p)) {
memcpy(opq, p + ulen + 1, sizeof(void*));
return 1;
}
return 0;
}
int
critbit0_insert (critbit0_tree * t, const char *u, void *opq)
{
const uint8 *const ubytes = (void *) u;
const size_t ulen = strlen (u);
uint8 *p = t->root;
if (!p) {
char *x;
if (posix_memalign ((void **) &x, sizeof (void *), ulen + 1 + sizeof (void*)))
return 0 ;
memcpy (x, u, ulen + 1);
memcpy (x + ulen + 1, &opq, sizeof(void*));
t->root = x;
return 2;
}
while (1 & (intptr_t) p) {
critbit0_node *q = (void *) (p - 1);
uint8 c = 0;
if (q->byte < ulen)
c = ubytes[q->byte];
const int direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
uint32 newbyte;
uint32 newotherbits;
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;
uint8 c = p[newbyte];
int newdirection = (1 + (newotherbits | c)) >> 8;
critbit0_node *newnode;
if (posix_memalign
((void **) &newnode, sizeof (void *), sizeof (critbit0_node)))
return 0;
char *x;
if (posix_memalign ((void **) &x, sizeof (void *), ulen + 1 + sizeof(void*))) {
free (newnode);
return 0;
}
memcpy (x, ubytes, ulen + 1);
memcpy (x + ulen + 1, &opq, sizeof(void*));
newnode->byte = newbyte;
newnode->otherbits = newotherbits;
newnode->child[1 - newdirection] = x;
void **wherep = &t->root;
for (;;) {
uint8 *p = *wherep;
if (!(1 & (intptr_t) p))
break;
critbit0_node *q = (void *) (p - 1);
if (q->byte > newbyte)
break;
if (q->byte == newbyte && q->otherbits > newotherbits)
break;
uint8 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 2;
}
int
critbit0_delete (critbit0_tree * t, const char *u, void *opq)
{
const uint8 *ubytes = (void *) u;
const size_t ulen = strlen (u);
uint8 *p = t->root;
void **wherep = &t->root;
void **whereq = 0;
critbit0_node *q = 0;
int direction = 0;
if (!p)
return 0;
while (1 & (intptr_t) p) {
whereq = wherep;
q = (void *) (p - 1);
uint8 c = 0;
if (q->byte < ulen)
c = ubytes[q->byte];
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
p = *wherep;
}
if (0 != strcmp (u, (const char *) p))
return 0;
memcpy(opq, p + ulen + 1, sizeof(void*));
free (p);
if (!whereq) {
t->root = 0;
return 1;
}
*whereq = q->child[1 - direction];
free (q);
return 1;
}
static void
traverse (void *top)
{
uint8 *p = top;
if (1 & (intptr_t) p) {
critbit0_node *q = (void *) (p - 1);
traverse (q->child[0]);
traverse (q->child[1]);
free (q);
} else {
free (p);
}
}
void
critbit0_clear (critbit0_tree * t)
{
if (t->root)
traverse (t->root);
t->root = NULL;
}
static int
allprefixed_traverse (uint8 * top,
int (*handle) (const char *, void *), void *arg)
{
if (1 & (intptr_t) top) {
int direction;
critbit0_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;
}
return handle ((const char *) top, arg);
}
int
critbit0_allprefixed (critbit0_tree * t, const char *prefix,
int (*handle) (const char *, void *), void *arg)
{
const uint8 *ubytes = (void *) prefix;
const size_t ulen = strlen (prefix);
uint8 *p = t->root;
uint8 *top = p;
if (!p)
return 1;
while (1 & (intptr_t) p) {
critbit0_node *q = (void *) (p - 1);
uint8 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;
}
size_t i;
for (i = 0; i < ulen; ++i) {
if (p[i] != ubytes[i])
return 1;
}
return allprefixed_traverse (top, handle, arg);
}