]> git.ozlabs.org Git - ccan/blob - ccan/strset/strset.c
strset: set errno on strset_add failures.
[ccan] / ccan / strset / strset.c
1 /* This code is based on the public domain code at
2  * http://github.com/agl/critbit writtem by Adam Langley
3  * <agl@imperialviolet.org>.
4  *
5  * Here are the main implementation differences:
6  * (1) We don't strdup the string on insert; we use the pointer we're given.
7  * (2) We use a straight bit number rather than a mask; it's simpler.
8  * (3) We don't use the bottom bit of the pointer, but instead use a leading
9  *     zero to distinguish nodes from strings.
10  * (4) The empty string (which would look like a node) is handled
11  *     using a special "empty node".
12  * (5) Delete returns the string, so you can free it if you want to.
13  * (6) Unions instead of void *, bool instead of int.
14  */
15 #include <ccan/strset/strset.h>
16 #include <ccan/short_types/short_types.h>
17 #include <ccan/likely/likely.h>
18 #include <ccan/str/str.h>
19 #include <ccan/ilog/ilog.h>
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <errno.h>
23
24 struct node {
25         /* To differentiate us from strings. */
26         char nul_byte;
27         /* The bit where these children differ. */
28         u8 bit_num;
29         /* The byte number where first bit differs (-1 == empty string node). */
30         size_t byte_num;
31         /* These point to strings or nodes. */
32         struct strset child[2];
33 };
34
35 /* Closest member to this in a non-empty set. */
36 static const char *closest(struct strset n, const char *member)
37 {
38         size_t len = strlen(member);
39         const u8 *bytes = (const u8 *)member;
40
41         /* Anything with first byte 0 is a node. */
42         while (!n.u.s[0]) {
43                 u8 direction = 0;
44
45                 /* Special node which represents the empty string. */
46                 if (unlikely(n.u.n->byte_num == (size_t)-1)) {
47                         n = n.u.n->child[0];
48                         break;
49                 }
50
51                 if (n.u.n->byte_num < len) {
52                         u8 c = bytes[n.u.n->byte_num];
53                         direction = (c >> n.u.n->bit_num) & 1;
54                 }
55                 n = n.u.n->child[direction];
56         }
57         return n.u.s;
58 }
59
60 char *strset_test(const struct strset *set, const char *member)
61 {
62         const char *str;
63
64         /* Empty set? */
65         if (!set->u.n)
66                 return NULL;
67         str = closest(*set, member);
68         if (streq(member, str))
69                 return (char *)str;
70         return NULL;
71 }
72
73 static bool set_string(struct strset *set,
74                        struct strset *n, const char *member)
75 {
76         /* Substitute magic empty node if this is the empty string */
77         if (unlikely(!member[0])) {
78                 n->u.n = malloc(sizeof(*n->u.n));
79                 if (unlikely(!n->u.n)) {
80                         errno = ENOMEM;
81                         return false;
82                 }
83                 n->u.n->nul_byte = '\0';
84                 n->u.n->byte_num = (size_t)-1;
85                 /* Attach the string to child[0] */
86                 n = &n->u.n->child[0];
87         }
88         n->u.s = member;
89         return true;
90 }
91
92 bool strset_set(struct strset *set, const char *member)
93 {
94         size_t len = strlen(member);
95         const u8 *bytes = (const u8 *)member;
96         struct strset *np;
97         const char *str;
98         struct node *newn;
99         size_t byte_num;
100         u8 bit_num, new_dir;
101
102         /* Empty set? */
103         if (!set->u.n) {
104                 return set_string(set, set, member);
105         }
106
107         /* Find closest existing member. */
108         str = closest(*set, member);
109
110         /* Find where they differ. */
111         for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
112                 if (member[byte_num] == '\0') {
113                         /* All identical! */
114                         errno = EEXIST;
115                         return false;
116                 }
117         }
118
119         /* Find which bit differs (if we had ilog8, we'd use it) */
120         bit_num = ilog32_nz((u8)str[byte_num] ^ bytes[byte_num]) - 1;
121         assert(bit_num < CHAR_BIT);
122
123         /* Which direction do we go at this bit? */
124         new_dir = ((bytes[byte_num]) >> bit_num) & 1;
125
126         /* Allocate new node. */
127         newn = malloc(sizeof(*newn));
128         if (!newn) {
129                 errno = ENOMEM;
130                 return false;
131         }
132         newn->nul_byte = '\0';
133         newn->byte_num = byte_num;
134         newn->bit_num = bit_num;
135         if (unlikely(!set_string(set, &newn->child[new_dir], member))) {
136                 free(newn);
137                 return false;
138         }
139
140         /* Find where to insert: not closest, but first which differs! */
141         np = set;
142         while (!np->u.s[0]) {
143                 u8 direction = 0;
144
145                 /* Special node which represents the empty string will
146                  * break here too! */
147                 if (np->u.n->byte_num > byte_num)
148                         break;
149                 /* Subtle: bit numbers are "backwards" for comparison */
150                 if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num)
151                         break;
152
153                 if (np->u.n->byte_num < len) {
154                         u8 c = bytes[np->u.n->byte_num];
155                         direction = (c >> np->u.n->bit_num) & 1;
156                 }
157                 np = &np->u.n->child[direction];
158         }
159
160         newn->child[!new_dir]= *np;
161         np->u.n = newn;
162         return true;
163 }
164
165 char *strset_clear(struct strset *set, const char *member)
166 {
167         size_t len = strlen(member);
168         const u8 *bytes = (const u8 *)member;
169         struct strset *parent = NULL, *n;
170         const char *ret = NULL;
171         u8 direction = 0; /* prevent bogus gcc warning. */
172
173         /* Empty set? */
174         if (!set->u.n)
175                 return NULL;
176
177         /* Find closest, but keep track of parent. */
178         n = set;
179         /* Anything with first byte 0 is a node. */
180         while (!n->u.s[0]) {
181                 u8 c = 0;
182
183                 /* Special node which represents the empty string. */
184                 if (unlikely(n->u.n->byte_num == (size_t)-1)) {
185                         const char *empty_str = n->u.n->child[0].u.s;
186
187                         if (member[0])
188                                 return NULL;
189
190                         /* Sew empty string back so remaining logic works */
191                         free(n->u.n);
192                         n->u.s = empty_str;
193                         break;
194                 }
195
196                 parent = n;
197                 if (n->u.n->byte_num < len) {
198                         c = bytes[n->u.n->byte_num];
199                         direction = (c >> n->u.n->bit_num) & 1;
200                 } else
201                         direction = 0;
202                 n = &n->u.n->child[direction];
203         }
204
205         /* Did we find it? */
206         if (!streq(member, n->u.s))
207                 return NULL;
208
209         ret = n->u.s;
210
211         if (!parent) {
212                 /* We deleted last node. */
213                 set->u.n = NULL;
214         } else {
215                 struct node *old = parent->u.n;
216                 /* Raise other node to parent. */
217                 *parent = old->child[!direction];
218                 free(old);
219         }
220
221         return (char *)ret;
222 }
223
224 static bool iterate(struct strset n,
225                     bool (*handle)(const char *, void *), void *data)
226 {
227         if (n.u.s[0])
228                 return handle(n.u.s, data);
229         if (unlikely(n.u.n->byte_num == (size_t)-1))
230                 return handle(n.u.n->child[0].u.s, data);
231
232         return iterate(n.u.n->child[0], handle, data)
233                 || iterate(n.u.n->child[1], handle, data);
234 }
235
236 void strset_iterate_(const struct strset *set,
237                      bool (*handle)(const char *, void *), void *data)
238 {
239         /* Empty set? */
240         if (!set->u.n)
241                 return;
242
243         iterate(*set, handle, data);
244 }
245
246 const struct strset *strset_prefix(const struct strset *set, const char *prefix)
247 {
248         const struct strset *n, *top;
249         size_t len = strlen(prefix);
250         const u8 *bytes = (const u8 *)prefix;
251
252         /* Empty set -> return empty set. */
253         if (!set->u.n)
254                 return set;
255
256         top = n = set;
257
258         /* We walk to find the top, but keep going to check prefix matches. */
259         while (!n->u.s[0]) {
260                 u8 c = 0, direction;
261
262                 /* Special node which represents the empty string. */
263                 if (unlikely(n->u.n->byte_num == (size_t)-1)) {
264                         n = &n->u.n->child[0];
265                         break;
266                 }
267
268                 if (n->u.n->byte_num < len)
269                         c = bytes[n->u.n->byte_num];
270
271                 direction = (c >> n->u.n->bit_num) & 1;
272                 n = &n->u.n->child[direction];
273                 if (c)
274                         top = n;
275         }
276
277         if (!strstarts(n->u.s, prefix)) {
278                 /* Convenient return for prefixes which do not appear in set. */
279                 static const struct strset empty_set;
280                 return &empty_set;
281         }
282
283         return top;
284 }
285
286 static void destroy(struct strset n)
287 {
288         if (!n.u.s[0]) {
289                 if (likely(n.u.n->byte_num != (size_t)-1)) {
290                         destroy(n.u.n->child[0]);
291                         destroy(n.u.n->child[1]);
292                 }
293                 free(n.u.n);
294         }
295 }
296
297 void strset_destroy(struct strset *set)
298 {
299         if (set->u.n)
300                 destroy(*set);
301         set->u.n = NULL;
302 }