1 /* This code is based on ccan/strset.c. */
2 #include <ccan/strmap/strmap.h>
3 #include <ccan/short_types/short_types.h>
4 #include <ccan/str/str.h>
5 #include <ccan/ilog/ilog.h>
11 /* These point to strings or nodes. */
12 struct strmap child[2];
13 /* The byte number where first bit differs. */
15 /* The bit where these children differ. */
19 /* Closest member to this in a non-empty map. */
20 static struct strmap *closest(struct strmap *n, const char *member)
22 size_t len = strlen(member);
23 const u8 *bytes = (const u8 *)member;
25 /* Anything with NULL value is a node. */
29 if (n->u.n->byte_num < len) {
30 u8 c = bytes[n->u.n->byte_num];
31 direction = (c >> n->u.n->bit_num) & 1;
33 n = &n->u.n->child[direction];
38 void *strmap_get_(const struct strmap *map, const char *member)
45 n = closest((struct strmap *)map, member);
46 if (streq(member, n->u.s))
51 bool strmap_add_(struct strmap *map, const char *member, const void *value)
53 size_t len = strlen(member);
54 const u8 *bytes = (const u8 *)member;
65 map->v = (void *)value;
69 /* Find closest existing member. */
70 n = closest(map, member);
72 /* Find where they differ. */
73 for (byte_num = 0; n->u.s[byte_num] == member[byte_num]; byte_num++) {
74 if (member[byte_num] == '\0') {
81 /* Find which bit differs (if we had ilog8, we'd use it) */
82 bit_num = ilog32_nz((u8)n->u.s[byte_num] ^ bytes[byte_num]) - 1;
83 assert(bit_num < CHAR_BIT);
85 /* Which direction do we go at this bit? */
86 new_dir = ((bytes[byte_num]) >> bit_num) & 1;
88 /* Allocate new node. */
89 newn = malloc(sizeof(*newn));
94 newn->byte_num = byte_num;
95 newn->bit_num = bit_num;
96 newn->child[new_dir].v = (void *)value;
97 newn->child[new_dir].u.s = member;
99 /* Find where to insert: not closest, but first which differs! */
104 if (n->u.n->byte_num > byte_num)
106 /* Subtle: bit numbers are "backwards" for comparison */
107 if (n->u.n->byte_num == byte_num && n->u.n->bit_num < bit_num)
110 if (n->u.n->byte_num < len) {
111 u8 c = bytes[n->u.n->byte_num];
112 direction = (c >> n->u.n->bit_num) & 1;
114 n = &n->u.n->child[direction];
117 newn->child[!new_dir] = *n;
123 char *strmap_del_(struct strmap *map, const char *member, void **valuep)
125 size_t len = strlen(member);
126 const u8 *bytes = (const u8 *)member;
127 struct strmap *parent = NULL, *n;
128 const char *ret = NULL;
129 u8 direction = 0; /* prevent bogus gcc warning. */
135 /* Find closest, but keep track of parent. */
137 /* Anything with NULL value is a node. */
142 if (n->u.n->byte_num < len) {
143 c = bytes[n->u.n->byte_num];
144 direction = (c >> n->u.n->bit_num) & 1;
147 n = &n->u.n->child[direction];
150 /* Did we find it? */
151 if (!streq(member, n->u.s))
159 /* We deleted last node. */
162 struct node *old = parent->u.n;
163 /* Raise other node to parent. */
164 *parent = old->child[!direction];
171 static bool iterate(struct strmap n,
172 bool (*handle)(const char *, void *, void *), void *data)
175 return handle(n.u.s, n.v, data);
177 return iterate(n.u.n->child[0], handle, data)
178 || iterate(n.u.n->child[1], handle, data);
181 void strmap_iterate_(const struct strmap *map,
182 bool (*handle)(const char *, void *, void *), void *data)
188 iterate(*map, handle, data);
191 const struct strmap *strmap_prefix_(const struct strmap *map,
194 const struct strmap *n, *top;
195 size_t len = strlen(prefix);
196 const u8 *bytes = (const u8 *)prefix;
198 /* Empty map -> return empty map. */
204 /* We walk to find the top, but keep going to check prefix matches. */
208 if (n->u.n->byte_num < len)
209 c = bytes[n->u.n->byte_num];
211 direction = (c >> n->u.n->bit_num) & 1;
212 n = &n->u.n->child[direction];
217 if (!strstarts(n->u.s, prefix)) {
218 /* Convenient return for prefixes which do not appear in map. */
219 static const struct strmap empty_map;
226 static void clear(struct strmap n)
229 clear(n.u.n->child[0]);
230 clear(n.u.n->child[1]);
235 void strmap_clear_(struct strmap *map)