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>
10 /* These point to strings or nodes. */
11 struct strmap child[2];
12 /* The byte number where first bit differs. */
14 /* The bit where these children differ. */
18 /* Closest member to this in a non-empty map. */
19 static struct strmap *closest(struct strmap *n, const char *member)
21 size_t len = strlen(member);
22 const u8 *bytes = (const u8 *)member;
24 /* Anything with NULL value is a node. */
28 if (n->u.n->byte_num < len) {
29 u8 c = bytes[n->u.n->byte_num];
30 direction = (c >> n->u.n->bit_num) & 1;
32 n = &n->u.n->child[direction];
37 void *strmap_get_(const struct strmap *map, const char *member)
44 n = closest((struct strmap *)map, member);
45 if (streq(member, n->u.s))
50 bool strmap_add_(struct strmap *map, const char *member, const void *value)
52 size_t len = strlen(member);
53 const u8 *bytes = (const u8 *)member;
64 map->v = (void *)value;
68 /* Find closest existing member. */
69 n = closest(map, member);
71 /* Find where they differ. */
72 for (byte_num = 0; n->u.s[byte_num] == member[byte_num]; byte_num++) {
73 if (member[byte_num] == '\0') {
79 /* Find which bit differs (if we had ilog8, we'd use it) */
80 bit_num = ilog32_nz((u8)n->u.s[byte_num] ^ bytes[byte_num]) - 1;
81 assert(bit_num < CHAR_BIT);
83 /* Which direction do we go at this bit? */
84 new_dir = ((bytes[byte_num]) >> bit_num) & 1;
86 /* Allocate new node. */
87 newn = malloc(sizeof(*newn));
92 newn->byte_num = byte_num;
93 newn->bit_num = bit_num;
94 newn->child[new_dir].v = (void *)value;
95 newn->child[new_dir].u.s = member;
97 /* Find where to insert: not closest, but first which differs! */
102 if (n->u.n->byte_num > byte_num)
104 /* Subtle: bit numbers are "backwards" for comparison */
105 if (n->u.n->byte_num == byte_num && n->u.n->bit_num < bit_num)
108 if (n->u.n->byte_num < len) {
109 u8 c = bytes[n->u.n->byte_num];
110 direction = (c >> n->u.n->bit_num) & 1;
112 n = &n->u.n->child[direction];
115 newn->child[!new_dir] = *n;
121 char *strmap_del_(struct strmap *map, const char *member, void **valuep)
123 size_t len = strlen(member);
124 const u8 *bytes = (const u8 *)member;
125 struct strmap *parent = NULL, *n;
126 const char *ret = NULL;
127 u8 direction = 0; /* prevent bogus gcc warning. */
133 /* Find closest, but keep track of parent. */
135 /* Anything with NULL value is a node. */
140 if (n->u.n->byte_num < len) {
141 c = bytes[n->u.n->byte_num];
142 direction = (c >> n->u.n->bit_num) & 1;
145 n = &n->u.n->child[direction];
148 /* Did we find it? */
149 if (!streq(member, n->u.s))
157 /* We deleted last node. */
160 struct node *old = parent->u.n;
161 /* Raise other node to parent. */
162 *parent = old->child[!direction];
169 static bool iterate(struct strmap n,
170 bool (*handle)(const char *, void *, void *), void *data)
173 return handle(n.u.s, n.v, data);
175 return iterate(n.u.n->child[0], handle, data)
176 || iterate(n.u.n->child[1], handle, data);
179 void strmap_iterate_(const struct strmap *map,
180 bool (*handle)(const char *, void *, void *), void *data)
186 iterate(*map, handle, data);
189 const struct strmap *strmap_prefix_(const struct strmap *map,
192 const struct strmap *n, *top;
193 size_t len = strlen(prefix);
194 const u8 *bytes = (const u8 *)prefix;
196 /* Empty map -> return empty map. */
202 /* We walk to find the top, but keep going to check prefix matches. */
206 if (n->u.n->byte_num < len)
207 c = bytes[n->u.n->byte_num];
209 direction = (c >> n->u.n->bit_num) & 1;
210 n = &n->u.n->child[direction];
215 if (!strstarts(n->u.s, prefix)) {
216 /* Convenient return for prefixes which do not appear in map. */
217 static const struct strmap empty_map;
224 static void clear(struct strmap n)
227 clear(n.u.n->child[0]);
228 clear(n.u.n->child[1]);
233 void strmap_clear_(struct strmap *map)