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>
24 /* To differentiate us from strings. */
26 /* The bit where these children differ. */
28 /* The byte number where first bit differs (-1 == empty string node). */
30 /* These point to strings or nodes. */
31 struct strset child[2];
34 /* Closest member to this in a non-empty set. */
35 static const char *closest(struct strset n, const char *member)
37 size_t len = strlen(member);
38 const u8 *bytes = (const u8 *)member;
40 /* Anything with first byte 0 is a node. */
44 /* Special node which represents the empty string. */
45 if (unlikely(n.u.n->byte_num == (size_t)-1)) {
50 if (n.u.n->byte_num < len) {
51 u8 c = bytes[n.u.n->byte_num];
52 direction = (c >> n.u.n->bit_num) & 1;
54 n = n.u.n->child[direction];
59 char *strset_test(const struct strset *set, const char *member)
66 str = closest(*set, member);
67 if (streq(member, str))
72 static bool set_string(struct strset *set,
73 struct strset *n, const char *member)
75 /* Substitute magic empty node if this is the empty string */
76 if (unlikely(!member[0])) {
77 n->u.n = malloc(sizeof(*n->u.n));
78 if (unlikely(!n->u.n))
80 n->u.n->nul_byte = '\0';
81 n->u.n->byte_num = (size_t)-1;
82 /* Attach the string to child[0] */
83 n = &n->u.n->child[0];
89 bool strset_set(struct strset *set, const char *member)
91 size_t len = strlen(member);
92 const u8 *bytes = (const u8 *)member;
101 return set_string(set, set, member);
104 /* Find closest existing member. */
105 str = closest(*set, member);
107 /* Find where they differ. */
108 for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
109 if (member[byte_num] == '\0') {
115 /* Find which bit differs (if we had ilog8, we'd use it) */
116 bit_num = ilog32_nz((u8)str[byte_num] ^ bytes[byte_num]) - 1;
117 assert(bit_num < CHAR_BIT);
119 /* Which direction do we go at this bit? */
120 new_dir = ((bytes[byte_num]) >> bit_num) & 1;
122 /* Allocate new node. */
123 newn = malloc(sizeof(*newn));
128 newn->nul_byte = '\0';
129 newn->byte_num = byte_num;
130 newn->bit_num = bit_num;
131 if (unlikely(!set_string(set, &newn->child[new_dir], member))) {
136 /* Find where to insert: not closest, but first which differs! */
138 while (!np->u.s[0]) {
141 /* Special node which represents the empty string will
143 if (np->u.n->byte_num > byte_num)
145 /* Subtle: bit numbers are "backwards" for comparison */
146 if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num)
149 if (np->u.n->byte_num < len) {
150 u8 c = bytes[np->u.n->byte_num];
151 direction = (c >> np->u.n->bit_num) & 1;
153 np = &np->u.n->child[direction];
156 newn->child[!new_dir]= *np;
161 char *strset_clear(struct strset *set, const char *member)
163 size_t len = strlen(member);
164 const u8 *bytes = (const u8 *)member;
165 struct strset *parent = NULL, *n;
166 const char *ret = NULL;
167 u8 direction = 0; /* prevent bogus gcc warning. */
173 /* Find closest, but keep track of parent. */
175 /* Anything with first byte 0 is a node. */
179 /* Special node which represents the empty string. */
180 if (unlikely(n->u.n->byte_num == (size_t)-1)) {
181 const char *empty_str = n->u.n->child[0].u.s;
186 /* Sew empty string back so remaining logic works */
193 if (n->u.n->byte_num < len) {
194 c = bytes[n->u.n->byte_num];
195 direction = (c >> n->u.n->bit_num) & 1;
198 n = &n->u.n->child[direction];
201 /* Did we find it? */
202 if (!streq(member, n->u.s))
208 /* We deleted last node. */
211 struct node *old = parent->u.n;
212 /* Raise other node to parent. */
213 *parent = old->child[!direction];
220 static bool iterate(struct strset n,
221 bool (*handle)(const char *, void *), void *data)
224 return handle(n.u.s, data);
225 if (unlikely(n.u.n->byte_num == (size_t)-1))
226 return handle(n.u.n->child[0].u.s, data);
228 return iterate(n.u.n->child[0], handle, data)
229 || iterate(n.u.n->child[1], handle, data);
232 void strset_iterate_(const struct strset *set,
233 bool (*handle)(const char *, void *), void *data)
239 iterate(*set, handle, data);
242 const struct strset *strset_prefix(const struct strset *set, const char *prefix)
244 const struct strset *n, *top;
245 size_t len = strlen(prefix);
246 const u8 *bytes = (const u8 *)prefix;
248 /* Empty set -> return empty set. */
254 /* We walk to find the top, but keep going to check prefix matches. */
258 /* Special node which represents the empty string. */
259 if (unlikely(n->u.n->byte_num == (size_t)-1)) {
260 n = &n->u.n->child[0];
264 if (n->u.n->byte_num < len)
265 c = bytes[n->u.n->byte_num];
267 direction = (c >> n->u.n->bit_num) & 1;
268 n = &n->u.n->child[direction];
273 if (!strstarts(n->u.s, prefix)) {
274 /* Convenient return for prefixes which do not appear in set. */
275 static const struct strset empty_set;
282 static void destroy(struct strset n)
285 if (likely(n.u.n->byte_num != (size_t)-1)) {
286 destroy(n.u.n->child[0]);
287 destroy(n.u.n->child[1]);
293 void strset_destroy(struct strset *set)