File: radix-tree-simple.c

package info (click to toggle)
lvm2 2.03.31-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,920 kB
  • sloc: ansic: 180,675; sh: 42,231; python: 6,554; makefile: 2,079; cpp: 1,258; ruby: 66; awk: 20
file content (299 lines) | stat: -rw-r--r-- 5,697 bytes parent folder | download
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
// Copyright (C) 2018 Red Hat, Inc. All rights reserved.
//
// This file is part of LVM2.
//
// This copyrighted material is made available to anyone wishing to use,
// modify, copy, or redistribute it subject to the terms and conditions
// of the GNU Lesser General Public License v.2.1.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "radix-tree.h"

#include "base/memory/container_of.h"
#include "base/memory/zalloc.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

//----------------------------------------------------------------
// This implementation is based around nested binary trees.  Very
// simple (and hopefully correct).

struct node {
	struct node *left;
	struct node *right;

	uint8_t key;
	struct node *center;

	bool has_value;
	union radix_value value;
};

struct radix_tree {
	radix_value_dtr dtr;
	void *dtr_context;
	unsigned nr_entries;

	struct node *root;
};

struct radix_tree *radix_tree_create(radix_value_dtr dtr, void *dtr_context)
{
	struct radix_tree *rt = zalloc(sizeof(*rt));

	if (rt) {
		rt->dtr = dtr;
		rt->dtr_context = dtr_context;
	}

	return rt;
}

// Returns the number of entries in the tree
static unsigned _destroy_tree(struct node *n, radix_value_dtr dtr, void *context)
{
	unsigned r;

	if (!n)
		return 0;

	r = _destroy_tree(n->left, dtr, context);
	r += _destroy_tree(n->right, dtr, context);
	r += _destroy_tree(n->center, dtr, context);

	if (n->has_value) {
		if (dtr)
			dtr(context, n->value);
		r++;
	}

	free(n);

	return r;
}

void radix_tree_destroy(struct radix_tree *rt)
{
	_destroy_tree(rt->root, rt->dtr, rt->dtr_context);
	free(rt);
}

static unsigned _count(struct node *n)
{
	unsigned r;

	if (!n)
		return 0;

	r = _count(n->left);
	r += _count(n->right);
	r += _count(n->center);

	if (n->has_value)
		r++;

	return r;
}

unsigned radix_tree_size(struct radix_tree *rt)
{
	return _count(rt->root);
}

static struct node **_lookup(struct node **pn, const uint8_t *kb, const uint8_t *ke)
{
	struct node *n = *pn;

	if (!n || (kb == ke))
		return pn;

	if (*kb < n->key)
		return _lookup(&n->left, kb, ke);

	else if (*kb > n->key)
		return _lookup(&n->right, kb, ke);

	else
		return _lookup(&n->center, kb + 1, ke);
}

static bool _insert(struct node **pn, const uint8_t *kb, const uint8_t *ke, union radix_value v)
{
	struct node *n = *pn;

	if (!n) {
		n = zalloc(sizeof(*n));
		if (!n)
			return false;

		n->key = *kb;
		*pn = n;
	}

	if (kb == ke) {
		n->has_value = true;
		n->value = v;
		return true;
	}

	if (*kb < n->key)
		return _insert(&n->left, kb, ke, v);

	else if (*kb > n->key)
		return _insert(&n->right, kb, ke, v);

	else
		return _insert(&n->center, kb + 1, ke, v);
}

bool radix_tree_insert(struct radix_tree *rt, const void *key, size_t keylen,
		       union radix_value v)
{
	const uint8_t *kb = key;
	const uint8_t *ke = kb + keylen;

	if (!_insert(&rt->root, kb, ke, v))
		return false;

	rt->nr_entries++;
	return true;
}

bool radix_tree_remove(struct radix_tree *rt, const void *key, size_t keylen)
{
	const uint8_t *kb = key;
	const uint8_t *ke = kb + keylen;
	struct node **pn = _lookup(&rt->root, kb, ke);
	struct node *n = *pn;

	if (!n || !n->has_value)
		return false;

	rt->nr_entries--;

	if (rt->dtr)
	    rt->dtr(rt->dtr_context, n->value);

	if (n->left || n->center || n->right) {
	    n->has_value = false;
	    return true;

	}

	// FIXME: delete parent if this was the last entry
	free(n);
	*pn = NULL;

	return true;
}

unsigned radix_tree_remove_prefix(struct radix_tree *rt, const void *prefix, size_t prefix_len)
{
	const uint8_t *kb = prefix;
	const uint8_t *ke = kb + prefix_len;
	struct node **pn;
	unsigned count = 0;

	pn = _lookup(&rt->root, kb, ke);

	if (*pn) {
		count = _destroy_tree(*pn, rt->dtr, rt->dtr_context);
		*pn = NULL;
	}

	return count;
}

bool radix_tree_lookup(struct radix_tree *rt, const void *key, size_t keylen,
		       union radix_value *result)
{
	const uint8_t *kb = key;
	const uint8_t *ke = kb + keylen;
	struct node **pn = _lookup(&rt->root, kb, ke);
	struct node *n = *pn;

	if (n && n->has_value) {
		*result = n->value;
		return true;
	}

	return false;
}

static void _iterate(struct node *n, struct radix_tree_iterator *it)
{
	if (!n)
		return;

	_iterate(n->left, it);

	if (n->has_value)
		// FIXME: fill out the key
		it->visit(it, NULL, 0, n->value);

	_iterate(n->center, it);
	_iterate(n->right, it);
}

void radix_tree_iterate(struct radix_tree *rt, const void *key, size_t keylen,
                        struct radix_tree_iterator *it)
{
	const uint8_t *kb = key;
	const uint8_t *ke = kb + keylen;

	if (kb == ke)
		_iterate(rt->root, it);

	else {
		struct node **pn = _lookup(&rt->root, kb, ke);
		struct node *n = *pn;

		if (n) {
			if (n->has_value)
				it->visit(it, NULL, 0, n->value);
			_iterate(n->center, it);
		}
	}
}

bool radix_tree_is_well_formed(struct radix_tree *rt)
{
	return true;
}

static void _dump(FILE *out, struct node *n, unsigned indent)
{
	unsigned i;

	if (!n)
		return;

	_dump(out, n->left, indent + 1);

	for (i = 0; i < 2 * indent; i++)
		fprintf(out, " ");

	if (n->has_value) {
		fprintf(out, "value: %lu\n", (unsigned long) n->value.n);
	} else {
		fprintf(out, "key: '%c' [0x%02x] %u\n",
			isprint(n->key) ? n->key : ' ', n->key, indent);
	}

	_dump(out, n->center, indent + 1);
	_dump(out, n->right, indent + 1);
}

void radix_tree_dump(struct radix_tree *rt, FILE *out)
{
	_dump(out, rt->root, 0);
}

//----------------------------------------------------------------