-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringtrie.c
407 lines (327 loc) · 8.58 KB
/
stringtrie.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stringtrie.h>
#include "common.h"
/*
* The array lengths of eight bytes come from the 6-bit crunched representation
* of characters in the trie: 2**6 = 64 = 8 * 8
*/
struct st_lsb {
struct st_root *lsb[8];
};
/*
* Character map for crunching characters. value+32 is the proper ASCII
* representation (i.e. 63 => 63 + 32 = 95 = '_').
*/
static const char crunch_char[256] = {
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
0, 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,
63, 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,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63
};
/*
* Character map for decrunching characters. value+32 is the proper ASCII
* representation, but we'll also make uppercase into lowercase characters
* to make them nicer to look at.
*/
static const char decrunch_char[64] = {
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, 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, 91, 92, 93, 94, 95
};
static char crunch(const char in)
{
return crunch_char[(const unsigned char)in];
}
static char decrunch(const unsigned char in)
{
assert(in < sizeof(decrunch_char));
return decrunch_char[in];
}
void st_init(struct st_root * const root)
{
assert(root);
memset(root, 0, sizeof(*root));
}
static unsigned char get_char_lsb(const char c)
{
return crunch(c) & 0x07;
}
static unsigned char get_char_msb(const char c)
{
return (crunch(c) >> 3) & 0x7;
}
static void st_destroy_root(struct st_root *root,
const enum st_free_data do_free_data)
{
struct st_lsb *lsb;
struct st_root *next;
if (do_free_data)
free(root->data);
root->data = NULL;
for (size_t i = 0; i < ARRAY_SIZE(root->msb); i++) {
lsb = root->msb[i];
if (!lsb)
continue;
root->msb[i] = NULL;
for (size_t j = 0; j < ARRAY_SIZE(lsb->lsb); j++) {
next = lsb->lsb[j];
if (!next)
continue;
lsb->lsb[j] = NULL;
st_destroy_root(next, do_free_data);
free(next);
}
free(lsb);
}
}
void st_destroy(struct st_root * const root,
const enum st_free_data do_free_data)
{
assert(root);
assert(do_free_data == ST_DO_FREE_DATA || do_free_data == ST_DONT_FREE_DATA);
st_destroy_root(root, do_free_data);
}
int st_add_string(struct st_root * const root, const char *string, void *data)
{
assert(root);
unsigned char c_lsb, c_msb;
struct st_lsb *lsb;
struct st_root *node;
if (!string || !string[0])
return -1;
node = root;
for (const char *c = string; *c; c++) {
c_lsb = get_char_lsb(*c);
c_msb = get_char_msb(*c);
if (!node->msb[c_msb]) {
node->msb[c_msb] = malloc(sizeof(*node->msb[c_msb]));
memset(node->msb[c_msb], 0, sizeof(*node->msb[c_msb]));
}
lsb = node->msb[c_msb];
if (!lsb->lsb[c_lsb]) {
lsb->lsb[c_lsb] = malloc(sizeof(*lsb->lsb[c_lsb]));
memset(lsb->lsb[c_lsb], 0, sizeof(*lsb->lsb[c_lsb]));
}
node = lsb->lsb[c_lsb];
}
node->data = data;
return 0;
}
/*
* If there is only one node with a data pointer if we traverse all child nodes
* from root, function get_the_only_child() will return the data pointer for
* that node. Otherwise, it will return NULL. This is useful for implementing
* matching the shortest unique string for a set of strings.
*/
static const struct st_root *get_the_only_child(
const struct st_root * const root)
{
struct st_root *node = NULL;
struct st_lsb *lsb = NULL;
for (size_t i = 0; i < ARRAY_SIZE(root->msb); i++) {
if (!root->msb[i])
continue;
if (lsb)
return NULL;
lsb = root->msb[i];
}
if (root->data) {
if (lsb)
return NULL;
return root;
}
for (size_t i = 0; i < ARRAY_SIZE(lsb->lsb); i++) {
if (!lsb->lsb[i])
continue;
if (node)
return NULL;
node = lsb->lsb[i];
}
/*
* This should never fail because it's impossible to have an msb
* without an lsb. Remember: the data pointer is in the msb.
*/
assert(node);
return get_the_only_child(node);
}
static const struct st_root *find_node(const struct st_root * const root,
const char * const string, int exact_match_only)
{
unsigned char c_lsb, c_msb;
struct st_lsb *lsb;
const struct st_root *node = root;
for (const char *c = string; *c; c++) {
c_lsb = get_char_lsb(*c);
c_msb = get_char_msb(*c);
lsb = node->msb[c_msb];
if (!lsb)
return NULL;
node = lsb->lsb[c_lsb];
if (!node)
return NULL;
}
if (node->data || exact_match_only)
return node;
else
return get_the_only_child(node);
}
void *st_lookup_string(const struct st_root * const root,
const char * const string)
{
const struct st_root *node;
if (!root || !string || !*string)
return NULL;
node = find_node(root, string, 0);
if (!node)
return NULL;
return node->data;
}
void *st_lookup_exact(const struct st_root * const root,
const char * const string)
{
const struct st_root *node;
if (!root || !string || !*string)
return NULL;
node = find_node(root, string, 1);
if (!node)
return NULL;
return node->data;
}
static int is_msb_free(const struct st_root *root)
{
for (size_t i = 0; i < ARRAY_SIZE(root->msb); i++) {
if (root->msb[i])
return 0;
}
return 1;
}
static int is_lsb_free(const struct st_lsb *lsb)
{
for (size_t i = 0; i < ARRAY_SIZE(lsb->lsb); i++) {
if (lsb->lsb[i])
return 0;
}
return 1;
}
void *st_rm_string(struct st_root * const root, const char * const string)
{
struct pair {
struct st_root *root;
struct st_lsb *lsb;
};
unsigned char c_lsb, c_msb;
struct st_lsb *lsb;
struct st_root *node = root;
if (!root || !string || !string[0])
return NULL;
const size_t str_len = strlen(string);
struct pair path[str_len];
for (size_t i = 0; string[i]; i++) {
c_lsb = get_char_lsb(string[i]);
c_msb = get_char_msb(string[i]);
lsb = node->msb[c_msb];
if (!lsb)
return NULL;
path[i].root = node;
node = lsb->lsb[c_lsb];
if (!node)
return NULL;
path[i].lsb = lsb;
}
void * const data = node->data;
node->data = NULL;
if (!is_msb_free(node))
return data;
for (size_t i = str_len - 1; i > 0; i--) {
lsb = path[i].lsb;
if (lsb->lsb[get_char_lsb(string[i])]->data)
return data;
free(lsb->lsb[get_char_lsb(string[i])]);
lsb->lsb[get_char_lsb(string[i])] = NULL;
if (!is_lsb_free(lsb))
return data;
node = path[i].root;
free(node->msb[get_char_msb(string[i])]);
node->msb[get_char_msb(string[i])] = NULL;
if (!is_msb_free(node))
return data;
}
return data;
}
int st_is_empty(const struct st_root * const root)
{
if (!is_msb_free(root))
return 0;
return 1;
}
static char get_char_from_msb_lsb(unsigned char msb, unsigned char lsb)
{
return decrunch((msb << 3) | lsb);
}
void __st_foreach_data(struct st_root *root,
void (*func)(void *data, const char *string, void *hints),
void *hints, char *buf, size_t idx, size_t len)
{
struct st_lsb *lsb;
struct st_root *next;
if (idx >= len)
return;
if (root->data) {
buf[idx] = '\0';
func(root->data, buf, hints);
}
for (size_t i = 0; i < ARRAY_SIZE(root->msb); i++) {
lsb = root->msb[i];
if (!lsb)
continue;
for (size_t j = 0; j < ARRAY_SIZE(lsb->lsb); j++) {
next = lsb->lsb[j];
if (!next)
continue;
buf[idx] = get_char_from_msb_lsb(i, j);
__st_foreach_data(next, func, hints, buf, idx + 1, len);
}
}
}
void st_foreach_data(struct st_root *root,
void (*func)(void *data, const char *string, void *hints),
void *hints)
{
char buf[64];
return __st_foreach_data(root, func, hints, buf, 0, ARRAY_SIZE(buf));
}