summaryrefslogtreecommitdiffstats
path: root/ccan/ccan/strset/strset.c
blob: 06b0d7a76c35fe119353310888b4dbf1828d949b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* This code is based on the public domain code at
 * http://github.com/agl/critbit writtem by Adam Langley
 * <agl@imperialviolet.org>.
 *
 * Here are the main implementation differences:
 * (1) We don't strdup the string on insert; we use the pointer we're given.
 * (2) We use a straight bit number rather than a mask; it's simpler.
 * (3) We don't use the bottom bit of the pointer, but instead use a leading
 *     zero to distinguish nodes from strings.
 * (4) The empty string (which would look like a node) is handled
 *     using a special "empty node".
 * (5) Delete returns the string, so you can free it if you want to.
 * (6) Unions instead of void *, bool instead of int.
 */
#include <ccan/strset/strset.h>
#include <ccan/short_types/short_types.h>
#include <ccan/likely/likely.h>
#include <ccan/str/str.h>
#include <ccan/ilog/ilog.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>

struct node {
	/* To differentiate us from strings. */
	char nul_byte;
	/* The bit where these children differ. */
	u8 bit_num;
	/* The byte number where first bit differs (-1 == empty string node). */
	size_t byte_num;
	/* These point to strings or nodes. */
	struct strset child[2];
};

/* Closest member to this in a non-empty set. */
static const char *closest(struct strset n, const char *member)
{
	size_t len = strlen(member);
	const u8 *bytes = (const u8 *)member;

	/* Anything with first byte 0 is a node. */
	while (!n.u.s[0]) {
		u8 direction = 0;

		/* Special node which represents the empty string. */
		if (unlikely(n.u.n->byte_num == (size_t)-1)) {
			n = n.u.n->child[0];
			break;
		}

		if (n.u.n->byte_num < len) {
			u8 c = bytes[n.u.n->byte_num];
			direction = (c >> n.u.n->bit_num) & 1;
		}
		n = n.u.n->child[direction];
	}
	return n.u.s;
}

char *strset_get(const struct strset *set, const char *member)
{
	const char *str;

	/* Non-empty set? */
	if (set->u.n) {
		str = closest(*set, member);
		if (streq(member, str))
			return (char *)str;
	}
	errno = ENOENT;
	return NULL;
}

static bool set_string(struct strset *set,
		       struct strset *n, const char *member)
{
	/* Substitute magic empty node if this is the empty string */
	if (unlikely(!member[0])) {
		n->u.n = malloc(sizeof(*n->u.n));
		if (unlikely(!n->u.n)) {
			errno = ENOMEM;
			return false;
		}
		n->u.n->nul_byte = '\0';
		n->u.n->byte_num = (size_t)-1;
		/* Attach the string to child[0] */
		n = &n->u.n->child[0];
	}
	n->u.s = member;
	return true;
}

bool strset_add(struct strset *set, const char *member)
{
	size_t len = strlen(member);
	const u8 *bytes = (const u8 *)member;
	struct strset *np;
	const char *str;
	struct node *newn;
	size_t byte_num;
	u8 bit_num, new_dir;

	/* Empty set? */
	if (!set->u.n) {
		return set_string(set, set, member);
	}

	/* Find closest existing member. */
	str = closest(*set, member);

	/* Find where they differ. */
	for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
		if (member[byte_num] == '\0') {
			/* All identical! */
			errno = EEXIST;
			return false;
		}
	}

	/* Find which bit differs (if we had ilog8, we'd use it) */
	bit_num = ilog32_nz((u8)str[byte_num] ^ bytes[byte_num]) - 1;
	assert(bit_num < CHAR_BIT);

	/* Which direction do we go at this bit? */
	new_dir = ((bytes[byte_num]) >> bit_num) & 1;

	/* Allocate new node. */
	newn = malloc(sizeof(*newn));
	if (!newn) {
		errno = ENOMEM;
		return false;
	}
	newn->nul_byte = '\0';
	newn->byte_num = byte_num;
	newn->bit_num = bit_num;
	if (unlikely(!set_string(set, &newn->child[new_dir], member))) {
		free(newn);
		return false;
	}

	/* Find where to insert: not closest, but first which differs! */
	np = set;
	while (!np->u.s[0]) {
		u8 direction = 0;

		/* Special node which represents the empty string will
		 * break here too! */
		if (np->u.n->byte_num > byte_num)
			break;
		/* Subtle: bit numbers are "backwards" for comparison */
		if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num)
			break;

		if (np->u.n->byte_num < len) {
			u8 c = bytes[np->u.n->byte_num];
			direction = (c >> np->u.n->bit_num) & 1;
		}
		np = &np->u.n->child[direction];
	}

	newn->child[!new_dir]= *np;
	np->u.n = newn;
	return true;
}

char *strset_del(struct strset *set, const char *member)
{
	size_t len = strlen(member);
	const u8 *bytes = (const u8 *)member;
	struct strset *parent = NULL, *n;
	const char *ret = NULL;
	u8 direction = 0; /* prevent bogus gcc warning. */

	/* Empty set? */
	if (!set->u.n) {
		errno = ENOENT;
		return NULL;
	}

	/* Find closest, but keep track of parent. */
	n = set;
	/* Anything with first byte 0 is a node. */
	while (!n->u.s[0]) {
		u8 c = 0;

		/* Special node which represents the empty string. */
		if (unlikely(n->u.n->byte_num == (size_t)-1)) {
			const char *empty_str = n->u.n->child[0].u.s;

			if (member[0]) {
				errno = ENOENT;
				return NULL;
			}

			/* Sew empty string back so remaining logic works */
			free(n->u.n);
			n->u.s = empty_str;
			break;
		}

		parent = n;
		if (n->u.n->byte_num < len) {
			c = bytes[n->u.n->byte_num];
			direction = (c >> n->u.n->bit_num) & 1;
		} else
			direction = 0;
		n = &n->u.n->child[direction];
	}

	/* Did we find it? */
	if (!streq(member, n->u.s)) {
		errno = ENOENT;
		return NULL;
	}

	ret = n->u.s;

	if (!parent) {
		/* We deleted last node. */
		set->u.n = NULL;
	} else {
		struct node *old = parent->u.n;
		/* Raise other node to parent. */
		*parent = old->child[!direction];
		free(old);
	}

	return (char *)ret;
}

static bool iterate(struct strset n,
		    bool (*handle)(const char *, void *), const void *data)
{
	if (n.u.s[0])
		return handle(n.u.s, (void *)data);
	if (unlikely(n.u.n->byte_num == (size_t)-1))
		return handle(n.u.n->child[0].u.s, (void *)data);

	return iterate(n.u.n->child[0], handle, data)
		&& iterate(n.u.n->child[1], handle, data);
}

void strset_iterate_(const struct strset *set,
		     bool (*handle)(const char *, void *), const void *data)
{
	/* Empty set? */
	if (!set->u.n)
		return;

	iterate(*set, handle, data);
}

const struct strset *strset_prefix(const struct strset *set, const char *prefix)
{
	const struct strset *n, *top;
	size_t len = strlen(prefix);
	const u8 *bytes = (const u8 *)prefix;

	/* Empty set -> return empty set. */
	if (!set->u.n)
		return set;

	top = n = set;

	/* We walk to find the top, but keep going to check prefix matches. */
	while (!n->u.s[0]) {
		u8 c = 0, direction;

		/* Special node which represents the empty string. */
		if (unlikely(n->u.n->byte_num == (size_t)-1)) {
			n = &n->u.n->child[0];
			break;
		}

		if (n->u.n->byte_num < len)
			c = bytes[n->u.n->byte_num];

		direction = (c >> n->u.n->bit_num) & 1;
		n = &n->u.n->child[direction];
		if (c)
			top = n;
	}

	if (!strstarts(n->u.s, prefix)) {
		/* Convenient return for prefixes which do not appear in set. */
		static const struct strset empty_set;
		return &empty_set;
	}

	return top;
}

static void clear(struct strset n)
{
	if (!n.u.s[0]) {
		if (likely(n.u.n->byte_num != (size_t)-1)) {
			clear(n.u.n->child[0]);
			clear(n.u.n->child[1]);
		}
		free(n.u.n);
	}
}

void strset_clear(struct strset *set)
{
	if (set->u.n)
		clear(*set);
	set->u.n = NULL;
}