-
Notifications
You must be signed in to change notification settings - Fork 2
/
trie.c
129 lines (114 loc) · 3.32 KB
/
trie.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
#include "trie.h"
#include <stdlib.h>
#include <string.h>
struct trie_find_result {
int limit;
const struct trie_node *node;
};
static void trie_node_init(struct trie_node *tn)
{
memset(tn, 0, sizeof(*tn));
}
static void trie_find_recurse(const struct trie_node *cur,
const unsigned char *k,
int limit, struct trie_find_result *r)
{
unsigned i;
/* If we are out of key and at a terminal node,
* we have a match. Is it better than the current best? */
if (!*k && cur->terminal) {
if (limit > r->limit) {
r->limit = limit;
r->node = cur;
}
return;
}
/* Try an exact match of this element. */
if (*k && cur->children[*k])
trie_find_recurse(cur->children[*k], k+1, limit, r);
/* If we have no limit to spare, or if we are already as bad as than
* something we have already found, there's no point in continuing. */
if (!limit || limit <= r->limit)
return;
/* Try deleting this element. */
if (*k)
trie_find_recurse(cur, k+1, limit-1, r);
/* Try substitution or addition. */
for (i = 0; i < 256; i++) {
if (!cur->children[i])
continue;
/* Substitution. Avoid substituting for ourselves,
* as we will already have tried that with exact match above. */
if (*k && i != *k)
trie_find_recurse(cur->children[i], k+1, limit-1, r);
/* Addition. */
trie_find_recurse(cur->children[i], k, limit-1, r);
}
}
void trie_init(struct trie *t)
{
trie_node_init(&t->root);
}
int trie_insert(struct trie *t, const char *ks, void *value)
{
struct trie_node *cur = &t->root;
const unsigned char *k = (const unsigned char *)ks;
for (; *k; k++) {
if (!cur->children[*k]) {
cur->children[*k] = malloc(sizeof(*cur->children[*k]));
if (!cur->children[*k]) return -1;
trie_node_init(cur->children[*k]);
}
cur = cur->children[*k];
}
cur->terminal = 1;
cur->value = value;
return 0;
}
static int trie_erase_recurse(struct trie_node *cur, const unsigned char *k,
void **value)
{
unsigned i;
/* If we're at the end... */
if (!*k) {
/* And we're a terminal, then we are actually erasing something */
if (cur->terminal) {
*value = cur->value;
cur->terminal = 0;
}
/* Report back whether we can be erased */
for (i = 0; i < 256; i++)
if (cur->children[i])
return 0;
return 1;
}
/* Otherwise, if we can continue, do so. */
if (cur->children[*k]) {
/* If our child can't be erased, neither can we. */
if (!trie_erase_recurse(cur->children[*k], k+1, value))
return 0;
/* Otherwise, erase the child... */
free(cur->children[*k]);
cur->children[*k] = NULL;
/* ... and fall through to the missing case */
}
/* see if we can be erased */
if (cur->terminal) return 0;
for (i = 0; i < 256; i++)
if (cur->children[i])
return 0;
return 1;
}
void *trie_erase(struct trie *t, const char *k)
{
void *value = NULL;
/* ignore return value; we never erase the root node */
trie_erase_recurse(&t->root, (const unsigned char *)k, &value);
return value;
}
void *trie_find(const struct trie *t, const char *k, int limit)
{
struct trie_find_result r = { -1, NULL };
trie_find_recurse(&t->root, (const unsigned char *)k, limit, &r);
return r.node ? r.node->value : NULL;
}