-
Notifications
You must be signed in to change notification settings - Fork 264
/
map.c
362 lines (307 loc) · 6.92 KB
/
map.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
/* Crit-bit tree based map which supports lookups based on unique
* prefixes as well as ordered iteration.
*
* Based on public domain code from Rusty Russel, Adam Langley
* and D. J. Bernstein.
*
* Further information about the data structure can be found at:
* http://cr.yp.to/critbit.html
* http://github.com/agl/critbit
* http://ccodearchive.net/info/strmap.html
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include "map.h"
typedef struct Node Node;
struct Map { /* struct holding either an item with value v and key u.s or an internal node */
union {
Node *n;
const char *s;
} u;
void *v; /* value stored in the map, if non NULL u.s holds the corresponding key */
};
struct Node {
Map child[2]; /* These point to strings or nodes. */
size_t byte_num; /* The byte number where first bit differs. */
uint8_t bit_num; /* The bit where these children differ. */
};
/* Closest key to this in a non-empty map. */
static Map *closest(Map *n, const char *key)
{
size_t len = strlen(key);
const uint8_t *bytes = (const uint8_t *)key;
/* Anything with NULL value is an internal node. */
while (!n->v) {
uint8_t direction = 0;
if (n->u.n->byte_num < len) {
uint8_t c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
}
n = &n->u.n->child[direction];
}
return n;
}
void *map_get(const Map *map, const char *key)
{
/* Not empty map? */
if (map->u.n) {
Map *n = closest((Map *)map, key);
if (strcmp(key, n->u.s) == 0)
return n->v;
}
return NULL;
}
void *map_closest(const Map *map, const char *prefix)
{
errno = 0;
void *v = map_get(map, prefix);
if (v)
return v;
const Map *m = map_prefix(map, prefix);
if (map_empty(m))
errno = ENOENT;
return m->v;
}
bool map_contains(const Map *map, const char *prefix)
{
return !map_empty(map_prefix(map, prefix));
}
bool map_put(Map *map, const char *k, const void *value)
{
size_t len = strlen(k);
const uint8_t *bytes = (const uint8_t *)k;
Map *n;
Node *newn;
size_t byte_num;
uint8_t bit_num, new_dir;
char *key;
if (!value) {
errno = EINVAL;
return false;
}
if (!(key = strdup(k))) {
errno = ENOMEM;
return false;
}
/* Empty map? */
if (!map->u.n) {
map->u.s = key;
map->v = (void *)value;
return true;
}
/* Find closest existing key. */
n = closest(map, key);
/* Find where they differ. */
for (byte_num = 0; n->u.s[byte_num] == key[byte_num]; byte_num++) {
if (key[byte_num] == '\0') {
/* All identical! */
free(key);
errno = EEXIST;
return false;
}
}
/* Find which bit differs */
uint8_t diff = (uint8_t)n->u.s[byte_num] ^ bytes[byte_num];
/* TODO: bit_num = 31 - __builtin_clz(diff); ? */
for (bit_num = 0; diff >>= 1; bit_num++);
/* Which direction do we go at this bit? */
new_dir = ((bytes[byte_num]) >> bit_num) & 1;
/* Allocate new node. */
newn = malloc(sizeof(*newn));
if (!newn) {
free(key);
errno = ENOMEM;
return false;
}
newn->byte_num = byte_num;
newn->bit_num = bit_num;
newn->child[new_dir].v = (void *)value;
newn->child[new_dir].u.s = key;
/* Find where to insert: not closest, but first which differs! */
n = map;
while (!n->v) {
uint8_t direction = 0;
if (n->u.n->byte_num > byte_num)
break;
/* Subtle: bit numbers are "backwards" for comparison */
if (n->u.n->byte_num == byte_num && n->u.n->bit_num < bit_num)
break;
if (n->u.n->byte_num < len) {
uint8_t c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
}
n = &n->u.n->child[direction];
}
newn->child[!new_dir] = *n;
n->u.n = newn;
n->v = NULL;
return true;
}
void *map_delete(Map *map, const char *key)
{
size_t len = strlen(key);
const uint8_t *bytes = (const uint8_t *)key;
Map *parent = NULL, *n;
void *value = NULL;
uint8_t direction;
/* Empty map? */
if (!map->u.n) {
errno = ENOENT;
return NULL;
}
/* Find closest, but keep track of parent. */
n = map;
/* Anything with NULL value is a node. */
while (!n->v) {
uint8_t c = 0;
parent = n;
if (n->u.n->byte_num < len) {
c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
} else {
direction = 0;
}
n = &n->u.n->child[direction];
}
/* Did we find it? */
if (strcmp(key, n->u.s)) {
errno = ENOENT;
return NULL;
}
free((char*)n->u.s);
value = n->v;
if (!parent) {
/* We deleted last node. */
map->u.n = NULL;
} else {
Node *old = parent->u.n;
/* Raise other node to parent. */
*parent = old->child[!direction];
free(old);
}
return value;
}
static bool iterate(Map n, bool (*handle)(const char *, void *, void *), const void *data)
{
if (n.v)
return handle(n.u.s, n.v, (void *)data);
return iterate(n.u.n->child[0], handle, data)
&& iterate(n.u.n->child[1], handle, data);
}
void map_iterate(const Map *map, bool (*handle)(const char *, void *, void *), const void *data)
{
/* Empty map? */
if (!map->u.n)
return;
iterate(*map, handle, data);
}
typedef struct {
const char *key;
void *value;
} KeyValue;
static bool first(const char *key, void *value, void *data)
{
KeyValue *kv = data;
kv->key = key;
kv->value = value;
return false;
}
void *map_first(const Map *map, const char **key)
{
KeyValue kv = { 0 };
map_iterate(map, first, &kv);
if (key && kv.key)
*key = kv.key;
return kv.value;
}
const Map *map_prefix(const Map *map, const char *prefix)
{
const Map *n, *top;
size_t len = strlen(prefix);
const uint8_t *bytes = (const uint8_t *)prefix;
/* Empty map -> return empty map. */
if (!map->u.n)
return map;
top = n = map;
/* We walk to find the top, but keep going to check prefix matches. */
while (!n->v) {
uint8_t c = 0, direction;
if (n->u.n->byte_num < len)
c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
n = &n->u.n->child[direction];
if (c)
top = n;
}
if (strncmp(n->u.s, prefix, len)) {
/* Convenient return for prefixes which do not appear in map. */
static const Map empty_map;
return &empty_map;
}
return top;
}
static void clear(Map n)
{
if (!n.v) {
clear(n.u.n->child[0]);
clear(n.u.n->child[1]);
free(n.u.n);
} else {
free((char*)n.u.s);
}
}
void map_clear(Map *map)
{
if (map->u.n)
clear(*map);
map->u.n = NULL;
map->v = NULL;
}
static bool copy(Map *dest, Map n)
{
if (!n.v) {
return copy(dest, n.u.n->child[0]) &&
copy(dest, n.u.n->child[1]);
} else {
if (!map_put(dest, n.u.s, n.v) && map_get(dest, n.u.s) != n.v) {
map_delete(dest, n.u.s);
return map_put(dest, n.u.s, n.v);
}
return true;
}
}
bool map_copy(Map *dest, Map *src)
{
if (!src || !src->u.n)
return true;
return copy(dest, *src);
}
bool map_empty(const Map *map)
{
return map->u.n == NULL;
}
Map *map_new(void)
{
return calloc(1, sizeof(Map));
}
void map_free(Map *map)
{
if (!map)
return;
map_clear(map);
free(map);
}
static bool free_elem(const char *key, void *value, void *data)
{
free(value);
return true;
}
void map_free_full(Map *map)
{
if (!map)
return;
map_iterate(map, free_elem, NULL);
map_free(map);
}