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>.
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.
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>
25 /* To differentiate us from strings. */
27 /* The bit where these children differ. */
29 /* The byte number where first bit differs (-1 == empty string node). */
31 /* These point to strings or nodes. */
32 struct strset child[2];
35 /* Closest member to this in a non-empty set. */
36 static const char *closest(struct strset n, const char *member)
38 size_t len = strlen(member);
39 const u8 *bytes = (const u8 *)member;
41 /* Anything with first byte 0 is a node. */
45 /* Special node which represents the empty string. */
46 if (unlikely(n.u.n->byte_num == (size_t)-1)) {
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;
55 n = n.u.n->child[direction];
60 char *strset_test(const struct strset *set, const char *member)
67 str = closest(*set, member);
68 if (streq(member, str))
73 static bool set_string(struct strset *set,
74 struct strset *n, const char *member)
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)) {
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];
92 bool strset_set(struct strset *set, const char *member)
94 size_t len = strlen(member);
95 const u8 *bytes = (const u8 *)member;
104 return set_string(set, set, member);
107 /* Find closest existing member. */
108 str = closest(*set, member);
110 /* Find where they differ. */
111 for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
112 if (member[byte_num] == '\0') {
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);
123 /* Which direction do we go at this bit? */
124 new_dir = ((bytes[byte_num]) >> bit_num) & 1;
126 /* Allocate new node. */
127 newn = malloc(sizeof(*newn));
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))) {
140 /* Find where to insert: not closest, but first which differs! */
142 while (!np->u.s[0]) {
145 /* Special node which represents the empty string will
147 if (np->u.n->byte_num > byte_num)
149 /* Subtle: bit numbers are "backwards" for comparison */
150 if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num)
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;
157 np = &np->u.n->child[direction];
160 newn->child[!new_dir]= *np;
165 char *strset_clear(struct strset *set, const char *member)
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. */
177 /* Find closest, but keep track of parent. */
179 /* Anything with first byte 0 is a node. */
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;
190 /* Sew empty string back so remaining logic works */
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;
202 n = &n->u.n->child[direction];
205 /* Did we find it? */
206 if (!streq(member, n->u.s))
212 /* We deleted last node. */
215 struct node *old = parent->u.n;
216 /* Raise other node to parent. */
217 *parent = old->child[!direction];
224 static bool iterate(struct strset n,
225 bool (*handle)(const char *, void *), void *data)
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);
232 return iterate(n.u.n->child[0], handle, data)
233 || iterate(n.u.n->child[1], handle, data);
236 void strset_iterate_(const struct strset *set,
237 bool (*handle)(const char *, void *), void *data)
243 iterate(*set, handle, data);
246 const struct strset *strset_prefix(const struct strset *set, const char *prefix)
248 const struct strset *n, *top;
249 size_t len = strlen(prefix);
250 const u8 *bytes = (const u8 *)prefix;
252 /* Empty set -> return empty set. */
258 /* We walk to find the top, but keep going to check prefix matches. */
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];
268 if (n->u.n->byte_num < len)
269 c = bytes[n->u.n->byte_num];
271 direction = (c >> n->u.n->bit_num) & 1;
272 n = &n->u.n->child[direction];
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;
286 static void destroy(struct strset n)
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]);
297 void strset_destroy(struct strset *set)