• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __AC_KBTREE_H
2 #define __AC_KBTREE_H
3 
4 /*-
5  * Copyright 1997-1999, 2001, John-Mark Gurney.
6  *           2008-2009, Attractive Chaos <attractor@live.co.uk>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 
35 #if defined(__GNUC__) || defined(__clang__)
36 #pragma GCC diagnostic push
37 //#pragma GCC diagnostic ignored "-Wall"
38 #endif
39 
40 typedef struct {
41 	uint32_t is_internal:1, n:31;
42 } kbnode_t;
43 
44 #define	__KB_KEY(type, x)	((type*)((char*)x + 4))
45 #define __KB_PTR(btr, x)	((kbnode_t**)((char*)x + btr->off_ptr))
46 
47 #define __KB_TREE_T(name)						\
48 	typedef struct {							\
49 		kbnode_t *root;							\
50 		int	off_key, off_ptr, ilen, elen;		\
51 		int	n, t;								\
52 		int	n_keys, n_nodes;					\
53 	} kbtree_##name##_t;
54 
55 #define __KB_INIT(name, key_t)											\
56 	kbtree_##name##_t *kb_init_##name(int size)							\
57 	{																	\
58 		kbtree_##name##_t *b;											\
59 		b = (kbtree_##name##_t*)calloc(1, sizeof(kbtree_##name##_t));	\
60 		b->t = ((size - 4 - sizeof(void*)) / (sizeof(void*) + sizeof(key_t)) + 1) >> 1; \
61 		if (b->t < 2) {													\
62 			free(b); return 0;											\
63 		}																\
64 		b->n = 2 * b->t - 1;											\
65 		b->off_ptr = 4 + b->n * sizeof(key_t);							\
66 		b->ilen = (4 + sizeof(void*) + b->n * (sizeof(void*) + sizeof(key_t)) + 3) >> 2 << 2; \
67 		b->elen = (b->off_ptr + 3) >> 2 << 2;							\
68 		b->root = (kbnode_t*)calloc(1, b->ilen);						\
69 		++b->n_nodes;													\
70 		return b;														\
71 	}
72 
73 #define __kb_destroy(b) do {											\
74 		int i, max = 8;													\
75 		kbnode_t *x, **top, **stack = 0;								\
76 		if (b) {														\
77 			top = stack = (kbnode_t**)calloc(max, sizeof(kbnode_t*));	\
78 			*top++ = (b)->root;											\
79 			while (top != stack) {										\
80 				x = *--top;												\
81 				if (x->is_internal == 0) { free(x); continue; }			\
82 				for (i = 0; i <= x->n; ++i)								\
83 					if (__KB_PTR(b, x)[i]) {							\
84 						if (top - stack == max) {						\
85 							max <<= 1;									\
86 							stack = (kbnode_t**)realloc(stack, max * sizeof(kbnode_t*)); \
87 							top = stack + (max>>1);						\
88 						}												\
89 						*top++ = __KB_PTR(b, x)[i];						\
90 					}													\
91 				free(x);												\
92 			}															\
93 		}																\
94 		free(b); free(stack);											\
95 	} while (0)
96 
97 #define __kb_get_first(key_t, b, ret) do {	\
98 		kbnode_t *__x = (b)->root;			\
99 		while (__KB_PTR(b, __x)[0] != 0)	\
100 			__x = __KB_PTR(b, __x)[0];		\
101 		(ret) = __KB_KEY(key_t, __x)[0];	\
102 	} while (0)
103 
104 #define __KB_GET_AUX0(name, key_t, __cmp)								\
105 	static inline int __kb_get_aux_##name(const kbnode_t * __restrict x, const key_t * __restrict k, int *r) \
106 	{																	\
107 		int tr, *rr, begin, end, n = x->n >> 1;							\
108 		if (x->n == 0) return -1;										\
109 		if (__cmp(*k, __KB_KEY(key_t, x)[n]) < 0) {						\
110 			begin = 0; end = n;											\
111 		} else { begin = n; end = x->n - 1; }							\
112 		rr = r? r : &tr;												\
113 		n = end;														\
114 		while (n >= begin && (*rr = __cmp(*k, __KB_KEY(key_t, x)[n])) < 0) --n; \
115 		return n;														\
116 	}
117 
118 #define __KB_GET_AUX1(name, key_t, __cmp)								\
119 	static inline int __kb_getp_aux_##name(const kbnode_t * __restrict x, const key_t * __restrict k, int *r) \
120 	{																	\
121 		int tr, *rr, begin = 0, end = x->n;								\
122 		if (x->n == 0) return -1;										\
123 		rr = r? r : &tr;												\
124 		while (begin < end) {											\
125 			int mid = (begin + end) >> 1;								\
126 			if (__cmp(__KB_KEY(key_t, x)[mid], *k) < 0) begin = mid + 1; \
127 			else end = mid;												\
128 		}																\
129 		if (begin == x->n) { *rr = 1; return x->n - 1; }				\
130 		if ((*rr = __cmp(*k, __KB_KEY(key_t, x)[begin])) < 0) --begin;	\
131 		return begin;													\
132 	}
133 
134 #define __KB_GET(name, key_t)											\
135 	static key_t *kb_getp_##name(kbtree_##name##_t *b, const key_t * __restrict k) \
136 	{																	\
137 		int i, r = 0;													\
138 		kbnode_t *x = b->root;											\
139 		while (x) {														\
140 			i = __kb_getp_aux_##name(x, k, &r);							\
141 			if (i >= 0 && r == 0) return &__KB_KEY(key_t, x)[i];		\
142 			if (x->is_internal == 0) return 0;							\
143 			x = __KB_PTR(b, x)[i + 1];									\
144 		}																\
145 		return 0;														\
146 	}																	\
147 	static inline key_t *kb_get_##name(kbtree_##name##_t *b, const key_t k) \
148 	{																	\
149 		return kb_getp_##name(b, &k);									\
150 	}
151 
152 #define __KB_INTERVAL(name, key_t)										\
153 	static void kb_intervalp_##name(kbtree_##name##_t *b, const key_t * __restrict k, key_t **lower, key_t **upper)	\
154 	{																	\
155 		int i, r = 0;													\
156 		kbnode_t *x = b->root;											\
157 		*lower = *upper = 0;											\
158 		while (x) {														\
159 			i = __kb_getp_aux_##name(x, k, &r);							\
160 			if (i >= 0 && r == 0) {										\
161 				*lower = *upper = &__KB_KEY(key_t, x)[i];				\
162 				return;													\
163 			}															\
164 			if (i >= 0) *lower = &__KB_KEY(key_t, x)[i];				\
165 			if (i < x->n - 1) *upper = &__KB_KEY(key_t, x)[i + 1];		\
166 			if (x->is_internal == 0) return;							\
167 			x = __KB_PTR(b, x)[i + 1];									\
168 		}																\
169 	}																	\
170 	static inline void kb_interval_##name(kbtree_##name##_t *b, const key_t k, key_t **lower, key_t **upper) \
171 	{																	\
172 		kb_intervalp_##name(b, &k, lower, upper);						\
173 	}
174 
175 #define __KB_PUT(name, key_t, __cmp)									\
176 	/* x must be an internal node */									\
177 	static void __kb_split_##name(kbtree_##name##_t *b, kbnode_t *x, int i, kbnode_t *y) \
178 	{																	\
179 		kbnode_t *z;													\
180 		z = (kbnode_t*)calloc(1, y->is_internal? b->ilen : b->elen);	\
181 		++b->n_nodes;													\
182 		z->is_internal = y->is_internal;								\
183 		z->n = b->t - 1;												\
184 		memcpy(__KB_KEY(key_t, z), __KB_KEY(key_t, y) + b->t, sizeof(key_t) * (b->t - 1)); \
185 		if (y->is_internal) memcpy(__KB_PTR(b, z), __KB_PTR(b, y) + b->t, sizeof(void*) * b->t); \
186 		y->n = b->t - 1;												\
187 		memmove(__KB_PTR(b, x) + i + 2, __KB_PTR(b, x) + i + 1, sizeof(void*) * (x->n - i)); \
188 		__KB_PTR(b, x)[i + 1] = z;										\
189 		memmove(__KB_KEY(key_t, x) + i + 1, __KB_KEY(key_t, x) + i, sizeof(key_t) * (x->n - i)); \
190 		__KB_KEY(key_t, x)[i] = __KB_KEY(key_t, y)[b->t - 1];			\
191 		++x->n;															\
192 	}																	\
193 	static void __kb_putp_aux_##name(kbtree_##name##_t *b, kbnode_t *x, const key_t * __restrict k) \
194 	{																	\
195 		int i = x->n - 1;												\
196 		if (x->is_internal == 0) {										\
197 			i = __kb_getp_aux_##name(x, k, 0);							\
198 			if (i != x->n - 1)											\
199 				memmove(__KB_KEY(key_t, x) + i + 2, __KB_KEY(key_t, x) + i + 1, (x->n - i - 1) * sizeof(key_t)); \
200 			__KB_KEY(key_t, x)[i + 1] = *k;								\
201 			++x->n;														\
202 		} else {														\
203 			i = __kb_getp_aux_##name(x, k, 0) + 1;						\
204 			if (__KB_PTR(b, x)[i]->n == 2 * b->t - 1) {					\
205 				__kb_split_##name(b, x, i, __KB_PTR(b, x)[i]);			\
206 				if (__cmp(*k, __KB_KEY(key_t, x)[i]) > 0) ++i;			\
207 			}															\
208 			__kb_putp_aux_##name(b, __KB_PTR(b, x)[i], k);				\
209 		}																\
210 	}																	\
211 	static void kb_putp_##name(kbtree_##name##_t *b, const key_t * __restrict k) \
212 	{																	\
213 		kbnode_t *r, *s;												\
214 		++b->n_keys;													\
215 		r = b->root;													\
216 		if (r->n == 2 * b->t - 1) {										\
217 			++b->n_nodes;												\
218 			s = (kbnode_t*)calloc(1, b->ilen);							\
219 			b->root = s; s->is_internal = 1; s->n = 0;					\
220 			__KB_PTR(b, s)[0] = r;										\
221 			__kb_split_##name(b, s, 0, r);								\
222 			r = s;														\
223 		}																\
224 		__kb_putp_aux_##name(b, r, k);									\
225 	}																	\
226 	static inline void kb_put_##name(kbtree_##name##_t *b, const key_t k) \
227 	{																	\
228 		kb_putp_##name(b, &k);											\
229 	}
230 
231 
232 #define __KB_DEL(name, key_t)											\
233 	static key_t __kb_delp_aux_##name(kbtree_##name##_t *b, kbnode_t *x, const key_t * __restrict k, int s) \
234 	{																	\
235 		int yn, zn, i, r = 0;											\
236 		kbnode_t *xp, *y, *z;											\
237 		key_t kp;														\
238 		if (x == 0) return *k;											\
239 		if (s) { /* s can only be 0, 1 or 2 */							\
240 			r = x->is_internal == 0? 0 : s == 1? 1 : -1;				\
241 			i = s == 1? x->n - 1 : -1;									\
242 		} else i = __kb_getp_aux_##name(x, k, &r);						\
243 		if (x->is_internal == 0) {										\
244 			if (s == 2) ++i;											\
245 			kp = __KB_KEY(key_t, x)[i];									\
246 			memmove(__KB_KEY(key_t, x) + i, __KB_KEY(key_t, x) + i + 1, (x->n - i - 1) * sizeof(key_t)); \
247 			--x->n;														\
248 			return kp;													\
249 		}																\
250 		if (r == 0) {													\
251 			if ((yn = __KB_PTR(b, x)[i]->n) >= b->t) {					\
252 				xp = __KB_PTR(b, x)[i];									\
253 				kp = __KB_KEY(key_t, x)[i];								\
254 				__KB_KEY(key_t, x)[i] = __kb_delp_aux_##name(b, xp, 0, 1); \
255 				return kp;												\
256 			} else if ((zn = __KB_PTR(b, x)[i + 1]->n) >= b->t) {		\
257 				xp = __KB_PTR(b, x)[i + 1];								\
258 				kp = __KB_KEY(key_t, x)[i];								\
259 				__KB_KEY(key_t, x)[i] = __kb_delp_aux_##name(b, xp, 0, 2); \
260 				return kp;												\
261 			} else if (yn == b->t - 1 && zn == b->t - 1) {				\
262 				y = __KB_PTR(b, x)[i]; z = __KB_PTR(b, x)[i + 1];		\
263 				__KB_KEY(key_t, y)[y->n++] = *k;						\
264 				memmove(__KB_KEY(key_t, y) + y->n, __KB_KEY(key_t, z), z->n * sizeof(key_t)); \
265 				if (y->is_internal) memmove(__KB_PTR(b, y) + y->n, __KB_PTR(b, z), (z->n + 1) * sizeof(void*)); \
266 				y->n += z->n;											\
267 				memmove(__KB_KEY(key_t, x) + i, __KB_KEY(key_t, x) + i + 1, (x->n - i - 1) * sizeof(key_t)); \
268 				memmove(__KB_PTR(b, x) + i + 1, __KB_PTR(b, x) + i + 2, (x->n - i - 1) * sizeof(void*)); \
269 				--x->n;													\
270 				free(z);												\
271 				return __kb_delp_aux_##name(b, y, k, s);				\
272 			}															\
273 		}																\
274 		++i;															\
275 		if ((xp = __KB_PTR(b, x)[i])->n == b->t - 1) {					\
276 			if (i > 0 && (y = __KB_PTR(b, x)[i - 1])->n >= b->t) {		\
277 				memmove(__KB_KEY(key_t, xp) + 1, __KB_KEY(key_t, xp), xp->n * sizeof(key_t)); \
278 				if (xp->is_internal) memmove(__KB_PTR(b, xp) + 1, __KB_PTR(b, xp), (xp->n + 1) * sizeof(void*)); \
279 				__KB_KEY(key_t, xp)[0] = __KB_KEY(key_t, x)[i - 1];		\
280 				__KB_KEY(key_t, x)[i - 1] = __KB_KEY(key_t, y)[y->n - 1]; \
281 				if (xp->is_internal) __KB_PTR(b, xp)[0] = __KB_PTR(b, y)[y->n]; \
282 				--y->n; ++xp->n;										\
283 			} else if (i < x->n && (y = __KB_PTR(b, x)[i + 1])->n >= b->t) { \
284 				__KB_KEY(key_t, xp)[xp->n++] = __KB_KEY(key_t, x)[i];	\
285 				__KB_KEY(key_t, x)[i] = __KB_KEY(key_t, y)[0];			\
286 				if (xp->is_internal) __KB_PTR(b, xp)[xp->n] = __KB_PTR(b, y)[0]; \
287 				--y->n;													\
288 				memmove(__KB_KEY(key_t, y), __KB_KEY(key_t, y) + 1, y->n * sizeof(key_t)); \
289 				if (y->is_internal) memmove(__KB_PTR(b, y), __KB_PTR(b, y) + 1, (y->n + 1) * sizeof(void*)); \
290 			} else if (i > 0 && (y = __KB_PTR(b, x)[i - 1])->n == b->t - 1) { \
291 				__KB_KEY(key_t, y)[y->n++] = __KB_KEY(key_t, x)[i - 1];	\
292 				memmove(__KB_KEY(key_t, y) + y->n, __KB_KEY(key_t, xp), xp->n * sizeof(key_t));	\
293 				if (y->is_internal) memmove(__KB_PTR(b, y) + y->n, __KB_PTR(b, xp), (xp->n + 1) * sizeof(void*)); \
294 				y->n += xp->n;											\
295 				memmove(__KB_KEY(key_t, x) + i - 1, __KB_KEY(key_t, x) + i, (x->n - i) * sizeof(key_t)); \
296 				memmove(__KB_PTR(b, x) + i, __KB_PTR(b, x) + i + 1, (x->n - i) * sizeof(void*)); \
297 				--x->n;													\
298 				free(xp);												\
299 				xp = y;													\
300 			} else if (i < x->n && (y = __KB_PTR(b, x)[i + 1])->n == b->t - 1) { \
301 				__KB_KEY(key_t, xp)[xp->n++] = __KB_KEY(key_t, x)[i];	\
302 				memmove(__KB_KEY(key_t, xp) + xp->n, __KB_KEY(key_t, y), y->n * sizeof(key_t));	\
303 				if (xp->is_internal) memmove(__KB_PTR(b, xp) + xp->n, __KB_PTR(b, y), (y->n + 1) * sizeof(void*)); \
304 				xp->n += y->n;											\
305 				memmove(__KB_KEY(key_t, x) + i, __KB_KEY(key_t, x) + i + 1, (x->n - i - 1) * sizeof(key_t)); \
306 				memmove(__KB_PTR(b, x) + i + 1, __KB_PTR(b, x) + i + 2, (x->n - i - 1) * sizeof(void*)); \
307 				--x->n;													\
308 				free(y);												\
309 			}															\
310 		}																\
311 		return __kb_delp_aux_##name(b, xp, k, s);						\
312 	}																	\
313 	static key_t kb_delp_##name(kbtree_##name##_t *b, const key_t * __restrict k) \
314 	{																	\
315 		kbnode_t *x;													\
316 		key_t ret;														\
317 		ret = __kb_delp_aux_##name(b, b->root, k, 0);					\
318 		--b->n_keys;													\
319 		if (b->root->n == 0 && b->root->is_internal) {					\
320 			--b->n_nodes;												\
321 			x = b->root;												\
322 			b->root = __KB_PTR(b, x)[0];								\
323 			free(x);													\
324 		}																\
325 		return ret;														\
326 	}																	\
327 	static inline key_t kb_del_##name(kbtree_##name##_t *b, const key_t k) \
328 	{																	\
329 		return kb_delp_##name(b, &k);									\
330 	}
331 
332 typedef struct {
333 	kbnode_t *x;
334 	int i;
335 } __kbstack_t;
336 
337 #define __kb_traverse(key_t, b, __func) do {							\
338 		int __kmax = 8;													\
339 		__kbstack_t *__kstack, *__kp;									\
340 		__kp = __kstack = (__kbstack_t*)calloc(__kmax, sizeof(__kbstack_t)); \
341 		__kp->x = (b)->root; __kp->i = 0;								\
342 		for (;;) {														\
343 			while (__kp->x && __kp->i <= __kp->x->n) {					\
344 				if (__kp - __kstack == __kmax - 1) {					\
345 					__kmax <<= 1;										\
346 					__kstack = (__kbstack_t*)realloc(__kstack, __kmax * sizeof(__kbstack_t)); \
347 					__kp = __kstack + (__kmax>>1) - 1;					\
348 				}														\
349 				(__kp+1)->i = 0; (__kp+1)->x = __kp->x->is_internal? __KB_PTR(b, __kp->x)[__kp->i] : 0; \
350 				++__kp;													\
351 			}															\
352 			--__kp;														\
353 			if (__kp >= __kstack) {										\
354 				if (__kp->x && __kp->i < __kp->x->n) __func(&__KB_KEY(key_t, __kp->x)[__kp->i]); \
355 				++__kp->i;												\
356 			} else break;												\
357 		}																\
358 		free(__kstack);													\
359 	} while (0)
360 
361 #define KBTREE_INIT(name, key_t, __cmp)			\
362 	__KB_TREE_T(name)							\
363 	__KB_INIT(name, key_t)						\
364 	__KB_GET_AUX1(name, key_t, __cmp)			\
365 	__KB_GET(name, key_t)						\
366 	__KB_INTERVAL(name, key_t)					\
367 	__KB_PUT(name, key_t, __cmp)				\
368 	__KB_DEL(name, key_t)
369 
370 #define KB_DEFAULT_SIZE 512
371 
372 #define kbtree_t(name) kbtree_##name##_t
373 #define kb_init(name, s) kb_init_##name(s)
374 #define kb_destroy(name, b) __kb_destroy(b)
375 #define kb_get(name, b, k) kb_get_##name(b, k)
376 #define kb_put(name, b, k) kb_put_##name(b, k)
377 #define kb_del(name, b, k) kb_del_##name(b, k)
378 #define kb_interval(name, b, k, l, u) kb_interval_##name(b, k, l, u)
379 #define kb_getp(name, b, k) kb_getp_##name(b, k)
380 #define kb_putp(name, b, k) kb_putp_##name(b, k)
381 #define kb_delp(name, b, k) kb_delp_##name(b, k)
382 #define kb_intervalp(name, b, k, l, u) kb_intervalp_##name(b, k, l, u)
383 
384 #define kb_size(b) ((b)->n_keys)
385 
386 #define kb_generic_cmp(a, b) (((b) < (a)) - ((a) < (b)))
387 #define kb_str_cmp(a, b) strcmp(a, b)
388 
389 #if defined(__GNUC__) || defined(__clang__)
390 #pragma GCC diagnostic pop
391 #endif
392 
393 #endif
394