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)
44 n = closest((struct strmap *)map, member);
45 if (streq(member, n->u.s))
52 bool strmap_add_(struct strmap *map, const char *member, const void *value)
54 size_t len = strlen(member);
55 const u8 *bytes = (const u8 *)member;
66 map->v = (void *)value;
70 /* Find closest existing member. */
71 n = closest(map, member);
73 /* Find where they differ. */
74 for (byte_num = 0; n->u.s[byte_num] == member[byte_num]; byte_num++) {
75 if (member[byte_num] == '\0') {
82 /* Find which bit differs (if we had ilog8, we'd use it) */
83 bit_num = ilog32_nz((u8)n->u.s[byte_num] ^ bytes[byte_num]) - 1;
84 assert(bit_num < CHAR_BIT);
86 /* Which direction do we go at this bit? */
87 new_dir = ((bytes[byte_num]) >> bit_num) & 1;
89 /* Allocate new node. */
90 newn = malloc(sizeof(*newn));
95 newn->byte_num = byte_num;
96 newn->bit_num = bit_num;
97 newn->child[new_dir].v = (void *)value;
98 newn->child[new_dir].u.s = member;
100 /* Find where to insert: not closest, but first which differs! */
105 if (n->u.n->byte_num > byte_num)
107 /* Subtle: bit numbers are "backwards" for comparison */
108 if (n->u.n->byte_num == byte_num && n->u.n->bit_num < bit_num)
111 if (n->u.n->byte_num < len) {
112 u8 c = bytes[n->u.n->byte_num];
113 direction = (c >> n->u.n->bit_num) & 1;
115 n = &n->u.n->child[direction];
118 newn->child[!new_dir] = *n;
124 char *strmap_del_(struct strmap *map, const char *member, void **valuep)
126 size_t len = strlen(member);
127 const u8 *bytes = (const u8 *)member;
128 struct strmap *parent = NULL, *n;
129 const char *ret = NULL;
130 u8 direction = 0; /* prevent bogus gcc warning. */
138 /* Find closest, but keep track of parent. */
140 /* Anything with NULL value is a node. */
145 if (n->u.n->byte_num < len) {
146 c = bytes[n->u.n->byte_num];
147 direction = (c >> n->u.n->bit_num) & 1;
150 n = &n->u.n->child[direction];
153 /* Did we find it? */
154 if (!streq(member, n->u.s)) {
164 /* We deleted last node. */
167 struct node *old = parent->u.n;
168 /* Raise other node to parent. */
169 *parent = old->child[!direction];
176 static bool iterate(struct strmap n,
177 bool (*handle)(const char *, void *, void *),
181 return handle(n.u.s, n.v, (void *)data);
183 return iterate(n.u.n->child[0], handle, data)
184 && iterate(n.u.n->child[1], handle, data);
187 void strmap_iterate_(const struct strmap *map,
188 bool (*handle)(const char *, void *, void *),
195 iterate(*map, handle, data);
198 const struct strmap *strmap_prefix_(const struct strmap *map,
201 const struct strmap *n, *top;
202 size_t len = strlen(prefix);
203 const u8 *bytes = (const u8 *)prefix;
205 /* Empty map -> return empty map. */
211 /* We walk to find the top, but keep going to check prefix matches. */
215 if (n->u.n->byte_num < len)
216 c = bytes[n->u.n->byte_num];
218 direction = (c >> n->u.n->bit_num) & 1;
219 n = &n->u.n->child[direction];
224 if (!strstarts(n->u.s, prefix)) {
225 /* Convenient return for prefixes which do not appear in map. */
226 static const struct strmap empty_map;
233 static void clear(struct strmap n)
236 clear(n.u.n->child[0]);
237 clear(n.u.n->child[1]);
242 void strmap_clear_(struct strmap *map)