1 /* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */
2 /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
3 /* $DragonFly: src/sys/sys/tree.h,v 1.5 2006/03/28 22:17:05 dillon Exp $ */
5 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * This file defines data structures for different types of trees:
34 * splay trees and red-black trees.
36 * A splay tree is a self-organizing data structure. Every operation
37 * on the tree causes a splay to happen. The splay moves the requested
38 * node to the root of the tree and partly rebalances it.
40 * This has the benefit that request locality causes faster lookups as
41 * the requested nodes move to the top of the tree. On the other hand,
42 * every lookup causes memory writes.
44 * The Balance Theorem bounds the total access time for m operations
45 * and n inserts on an initially empty tree as O((m + n)lg n). The
46 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
48 * A red-black tree is a binary search tree with the node color as an
49 * extra attribute. It fulfills a set of conditions:
50 * - every search path from the root to a leaf consists of the
51 * same number of black nodes,
52 * - each red node (except for the root) has a black parent,
53 * - each leaf node is black.
55 * Every operation on a red-black tree is bounded as O(lg n).
56 * The maximum height of a red-black tree is 2lg (n+1).
59 #define SPLAY_HEAD(name, type) \
61 struct type *sph_root; /* root of the tree */ \
64 #define SPLAY_INITIALIZER(root) \
67 #define SPLAY_INIT(root) do { \
68 (root)->sph_root = NULL; \
69 } while (/*CONSTCOND*/ 0)
71 #define SPLAY_ENTRY(type) \
73 struct type *spe_left; /* left element */ \
74 struct type *spe_right; /* right element */ \
77 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
78 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
79 #define SPLAY_ROOT(head) (head)->sph_root
80 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
82 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
83 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
84 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
85 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
86 (head)->sph_root = tmp; \
87 } while (/*CONSTCOND*/ 0)
89 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
90 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
91 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
92 (head)->sph_root = tmp; \
93 } while (/*CONSTCOND*/ 0)
95 #define SPLAY_LINKLEFT(head, tmp, field) do { \
96 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
97 tmp = (head)->sph_root; \
98 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
99 } while (/*CONSTCOND*/ 0)
101 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
102 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
103 tmp = (head)->sph_root; \
104 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
105 } while (/*CONSTCOND*/ 0)
107 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
108 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
109 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
110 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
111 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
112 } while (/*CONSTCOND*/ 0)
114 /* Generates prototypes and inline functions */
116 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
117 void name##_SPLAY(struct name *, struct type *); \
118 void name##_SPLAY_MINMAX(struct name *, int); \
119 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
120 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
122 /* Finds the node with the same key as elm */ \
123 static __inline struct type * \
124 name##_SPLAY_FIND(struct name *head, struct type *elm) \
126 if (SPLAY_EMPTY(head)) \
128 name##_SPLAY(head, elm); \
129 if ((cmp)(elm, (head)->sph_root) == 0) \
130 return (head->sph_root); \
134 static __inline struct type * \
135 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
137 name##_SPLAY(head, elm); \
138 if (SPLAY_RIGHT(elm, field) != NULL) { \
139 elm = SPLAY_RIGHT(elm, field); \
140 while (SPLAY_LEFT(elm, field) != NULL) { \
141 elm = SPLAY_LEFT(elm, field); \
148 static __inline struct type * \
149 name##_SPLAY_MIN_MAX(struct name *head, int val) \
151 name##_SPLAY_MINMAX(head, val); \
152 return (SPLAY_ROOT(head)); \
155 /* Main splay operation.
156 * Moves node close to the key of elm to top
158 #define SPLAY_GENERATE(name, type, field, cmp) \
160 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
162 if (SPLAY_EMPTY(head)) { \
163 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
166 name##_SPLAY(head, elm); \
167 __comp = (cmp)(elm, (head)->sph_root); \
169 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
170 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
171 SPLAY_LEFT((head)->sph_root, field) = NULL; \
172 } else if (__comp > 0) { \
173 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
174 SPLAY_LEFT(elm, field) = (head)->sph_root; \
175 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
177 return ((head)->sph_root); \
179 (head)->sph_root = (elm); \
184 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
186 struct type *__tmp; \
187 if (SPLAY_EMPTY(head)) \
189 name##_SPLAY(head, elm); \
190 if ((cmp)(elm, (head)->sph_root) == 0) { \
191 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
192 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
194 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
195 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
196 name##_SPLAY(head, elm); \
197 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
205 name##_SPLAY(struct name *head, struct type *elm) \
207 struct type __node, *__left, *__right, *__tmp; \
210 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
211 __left = __right = &__node; \
213 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
215 __tmp = SPLAY_LEFT((head)->sph_root, field); \
218 if ((cmp)(elm, __tmp) < 0){ \
219 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
220 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
223 SPLAY_LINKLEFT(head, __right, field); \
224 } else if (__comp > 0) { \
225 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
228 if ((cmp)(elm, __tmp) > 0){ \
229 SPLAY_ROTATE_LEFT(head, __tmp, field); \
230 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
233 SPLAY_LINKRIGHT(head, __left, field); \
236 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
239 /* Splay with either the minimum or the maximum element \
240 * Used to find minimum or maximum element in tree. \
242 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
244 struct type __node, *__left, *__right, *__tmp; \
246 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
247 __left = __right = &__node; \
251 __tmp = SPLAY_LEFT((head)->sph_root, field); \
255 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
256 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
259 SPLAY_LINKLEFT(head, __right, field); \
260 } else if (__comp > 0) { \
261 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
265 SPLAY_ROTATE_LEFT(head, __tmp, field); \
266 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
269 SPLAY_LINKRIGHT(head, __left, field); \
272 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
275 #define SPLAY_NEGINF -1
278 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
279 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
280 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
281 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
282 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
283 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
284 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
285 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
287 #define SPLAY_FOREACH(x, name, head) \
288 for ((x) = SPLAY_MIN(name, head); \
290 (x) = SPLAY_NEXT(name, head, x))
292 /* Macros that define a red-black tree */
294 #define RB_SCAN_INFO(name, type) \
295 struct name##_scan_info { \
296 struct name##_scan_info *link; \
300 #define RB_HEAD(name, type) \
302 struct type *rbh_root; /* root of the tree */ \
303 struct name##_scan_info *rbh_inprog; /* scans in progress */ \
306 #define RB_INITIALIZER(root) \
309 #define RB_INIT(root) do { \
310 (root)->rbh_root = NULL; \
311 (root)->rbh_inprog = NULL; \
312 } while (/*CONSTCOND*/ 0)
316 #define RB_ENTRY(type) \
318 struct type *rbe_left; /* left element */ \
319 struct type *rbe_right; /* right element */ \
320 struct type *rbe_parent; /* parent element */ \
321 int rbe_color; /* node color */ \
324 #define RB_LEFT(elm, field) (elm)->field.rbe_left
325 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
326 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
327 #define RB_COLOR(elm, field) (elm)->field.rbe_color
328 #define RB_ROOT(head) (head)->rbh_root
329 #define RB_INPROG(head) (head)->rbh_inprog
330 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
332 #define RB_SET(elm, parent, field) do { \
333 RB_PARENT(elm, field) = parent; \
334 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
335 RB_COLOR(elm, field) = RB_RED; \
336 } while (/*CONSTCOND*/ 0)
338 #define RB_SET_BLACKRED(black, red, field) do { \
339 RB_COLOR(black, field) = RB_BLACK; \
340 RB_COLOR(red, field) = RB_RED; \
341 } while (/*CONSTCOND*/ 0)
344 #define RB_AUGMENT(x)
347 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
348 (tmp) = RB_RIGHT(elm, field); \
349 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
350 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
353 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
354 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
355 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
357 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
359 (head)->rbh_root = (tmp); \
360 RB_LEFT(tmp, field) = (elm); \
361 RB_PARENT(elm, field) = (tmp); \
363 if ((RB_PARENT(tmp, field))) \
364 RB_AUGMENT(RB_PARENT(tmp, field)); \
365 } while (/*CONSTCOND*/ 0)
367 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
368 (tmp) = RB_LEFT(elm, field); \
369 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
370 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
373 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
374 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
375 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
377 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
379 (head)->rbh_root = (tmp); \
380 RB_RIGHT(tmp, field) = (elm); \
381 RB_PARENT(elm, field) = (tmp); \
383 if ((RB_PARENT(tmp, field))) \
384 RB_AUGMENT(RB_PARENT(tmp, field)); \
385 } while (/*CONSTCOND*/ 0)
387 /* Generates prototypes and inline functions */
388 #define RB_PROTOTYPE(name, type, field, cmp) \
389 struct type *name##_RB_REMOVE(struct name *, struct type *); \
390 struct type *name##_RB_INSERT(struct name *, struct type *); \
391 struct type *name##_RB_FIND(struct name *, struct type *); \
392 int name##_RB_SCAN(struct name *, int (*)(struct type *, void *), \
393 int (*)(struct type *, void *), void *); \
394 struct type *name##_RB_NEXT(struct type *); \
395 struct type *name##_RB_PREV(struct type *); \
396 struct type *name##_RB_MINMAX(struct name *, int); \
397 RB_SCAN_INFO(name, type) \
400 * A version which supplies a fast lookup routine for an exact match
401 * on a numeric field.
403 #define RB_PROTOTYPE2(name, type, field, cmp, datatype) \
404 RB_PROTOTYPE(name, type, field, cmp); \
405 struct type *name##_RB_LOOKUP(struct name *, datatype) \
408 * A version which supplies a fast lookup routine for a numeric
409 * field which resides within a ranged object, either using (begin,end),
410 * or using (begin,size).
412 #define RB_PROTOTYPE3(name, type, field, cmp, datatype) \
413 RB_PROTOTYPE2(name, type, field, cmp, datatype); \
414 struct type *name##_RB_RLOOKUP(struct name *, datatype) \
416 #define RB_PROTOTYPE4(name, type, field, cmp, datatype) \
417 RB_PROTOTYPE2(name, type, field, cmp, datatype); \
418 struct type *name##_RB_RLOOKUP(struct name *, datatype) \
420 /* Main rb operation.
421 * Moves node close to the key of elm to top
423 #define RB_GENERATE(name, type, field, cmp) \
425 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
427 struct type *parent, *gparent, *tmp; \
428 while ((parent = RB_PARENT(elm, field)) != NULL && \
429 RB_COLOR(parent, field) == RB_RED) { \
430 gparent = RB_PARENT(parent, field); \
431 if (parent == RB_LEFT(gparent, field)) { \
432 tmp = RB_RIGHT(gparent, field); \
433 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
434 RB_COLOR(tmp, field) = RB_BLACK; \
435 RB_SET_BLACKRED(parent, gparent, field);\
439 if (RB_RIGHT(parent, field) == elm) { \
440 RB_ROTATE_LEFT(head, parent, tmp, field);\
445 RB_SET_BLACKRED(parent, gparent, field); \
446 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
448 tmp = RB_LEFT(gparent, field); \
449 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
450 RB_COLOR(tmp, field) = RB_BLACK; \
451 RB_SET_BLACKRED(parent, gparent, field);\
455 if (RB_LEFT(parent, field) == elm) { \
456 RB_ROTATE_RIGHT(head, parent, tmp, field);\
461 RB_SET_BLACKRED(parent, gparent, field); \
462 RB_ROTATE_LEFT(head, gparent, tmp, field); \
465 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
469 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \
473 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
474 elm != RB_ROOT(head)) { \
475 if (RB_LEFT(parent, field) == elm) { \
476 tmp = RB_RIGHT(parent, field); \
477 if (RB_COLOR(tmp, field) == RB_RED) { \
478 RB_SET_BLACKRED(tmp, parent, field); \
479 RB_ROTATE_LEFT(head, parent, tmp, field);\
480 tmp = RB_RIGHT(parent, field); \
482 if ((RB_LEFT(tmp, field) == NULL || \
483 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
484 (RB_RIGHT(tmp, field) == NULL || \
485 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
486 RB_COLOR(tmp, field) = RB_RED; \
488 parent = RB_PARENT(elm, field); \
490 if (RB_RIGHT(tmp, field) == NULL || \
491 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
492 struct type *oleft; \
493 if ((oleft = RB_LEFT(tmp, field)) \
495 RB_COLOR(oleft, field) = RB_BLACK;\
496 RB_COLOR(tmp, field) = RB_RED; \
497 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
498 tmp = RB_RIGHT(parent, field); \
500 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
501 RB_COLOR(parent, field) = RB_BLACK; \
502 if (RB_RIGHT(tmp, field)) \
503 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
504 RB_ROTATE_LEFT(head, parent, tmp, field);\
505 elm = RB_ROOT(head); \
509 tmp = RB_LEFT(parent, field); \
510 if (RB_COLOR(tmp, field) == RB_RED) { \
511 RB_SET_BLACKRED(tmp, parent, field); \
512 RB_ROTATE_RIGHT(head, parent, tmp, field);\
513 tmp = RB_LEFT(parent, field); \
515 if ((RB_LEFT(tmp, field) == NULL || \
516 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
517 (RB_RIGHT(tmp, field) == NULL || \
518 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
519 RB_COLOR(tmp, field) = RB_RED; \
521 parent = RB_PARENT(elm, field); \
523 if (RB_LEFT(tmp, field) == NULL || \
524 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
525 struct type *oright; \
526 if ((oright = RB_RIGHT(tmp, field)) \
528 RB_COLOR(oright, field) = RB_BLACK;\
529 RB_COLOR(tmp, field) = RB_RED; \
530 RB_ROTATE_LEFT(head, tmp, oright, field);\
531 tmp = RB_LEFT(parent, field); \
533 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
534 RB_COLOR(parent, field) = RB_BLACK; \
535 if (RB_LEFT(tmp, field)) \
536 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
537 RB_ROTATE_RIGHT(head, parent, tmp, field);\
538 elm = RB_ROOT(head); \
544 RB_COLOR(elm, field) = RB_BLACK; \
548 name##_RB_REMOVE(struct name *head, struct type *elm) \
550 struct type *child, *parent, *old; \
551 struct name##_scan_info *inprog; \
554 for (inprog = RB_INPROG(head); inprog; inprog = inprog->link) { \
555 if (inprog->node == elm) \
556 inprog->node = RB_NEXT(name, head, elm); \
560 if (RB_LEFT(elm, field) == NULL) \
561 child = RB_RIGHT(elm, field); \
562 else if (RB_RIGHT(elm, field) == NULL) \
563 child = RB_LEFT(elm, field); \
566 elm = RB_RIGHT(elm, field); \
567 while ((left = RB_LEFT(elm, field)) != NULL) \
569 child = RB_RIGHT(elm, field); \
570 parent = RB_PARENT(elm, field); \
571 color = RB_COLOR(elm, field); \
573 RB_PARENT(child, field) = parent; \
575 if (RB_LEFT(parent, field) == elm) \
576 RB_LEFT(parent, field) = child; \
578 RB_RIGHT(parent, field) = child; \
579 RB_AUGMENT(parent); \
581 RB_ROOT(head) = child; \
582 if (RB_PARENT(elm, field) == old) \
584 (elm)->field = (old)->field; \
585 if (RB_PARENT(old, field)) { \
586 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
587 RB_LEFT(RB_PARENT(old, field), field) = elm;\
589 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
590 RB_AUGMENT(RB_PARENT(old, field)); \
592 RB_ROOT(head) = elm; \
593 RB_PARENT(RB_LEFT(old, field), field) = elm; \
594 if (RB_RIGHT(old, field)) \
595 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
600 } while ((left = RB_PARENT(left, field)) != NULL); \
604 parent = RB_PARENT(elm, field); \
605 color = RB_COLOR(elm, field); \
607 RB_PARENT(child, field) = parent; \
609 if (RB_LEFT(parent, field) == elm) \
610 RB_LEFT(parent, field) = child; \
612 RB_RIGHT(parent, field) = child; \
613 RB_AUGMENT(parent); \
615 RB_ROOT(head) = child; \
617 if (color == RB_BLACK) \
618 name##_RB_REMOVE_COLOR(head, parent, child); \
622 /* Inserts a node into the RB tree */ \
624 name##_RB_INSERT(struct name *head, struct type *elm) \
627 struct type *parent = NULL; \
629 tmp = RB_ROOT(head); \
632 comp = (cmp)(elm, parent); \
634 tmp = RB_LEFT(tmp, field); \
636 tmp = RB_RIGHT(tmp, field); \
640 RB_SET(elm, parent, field); \
641 if (parent != NULL) { \
643 RB_LEFT(parent, field) = elm; \
645 RB_RIGHT(parent, field) = elm; \
646 RB_AUGMENT(parent); \
648 RB_ROOT(head) = elm; \
649 name##_RB_INSERT_COLOR(head, elm); \
653 /* Finds the node with the same key as elm */ \
655 name##_RB_FIND(struct name *head, struct type *elm) \
657 struct type *tmp = RB_ROOT(head); \
660 comp = cmp(elm, tmp); \
662 tmp = RB_LEFT(tmp, field); \
664 tmp = RB_RIGHT(tmp, field); \
672 * Issue a callback for all matching items. The scan function must \
673 * return < 0 for items below the desired range, 0 for items within \
674 * the range, and > 0 for items beyond the range. Any item may be \
675 * deleted while the scan is in progress. \
678 name##_SCANCMP_ALL(struct type *type, void *data) \
684 name##_RB_SCAN(struct name *head, \
685 int (*scancmp)(struct type *, void *), \
686 int (*callback)(struct type *, void *), \
689 struct name##_scan_info info; \
690 struct name##_scan_info **infopp; \
696 if (scancmp == NULL) \
697 scancmp = name##_SCANCMP_ALL; \
700 * Locate the first element. \
702 tmp = RB_ROOT(head); \
705 comp = scancmp(tmp, data); \
707 tmp = RB_RIGHT(tmp, field); \
708 } else if (comp > 0) { \
709 tmp = RB_LEFT(tmp, field); \
712 if (RB_LEFT(tmp, field) == NULL) \
714 tmp = RB_LEFT(tmp, field); \
719 info.node = RB_NEXT(name, head, best); \
720 info.link = RB_INPROG(head); \
721 RB_INPROG(head) = &info; \
722 while ((comp = callback(best, data)) >= 0) { \
725 if (best == NULL || scancmp(best, data) != 0) \
727 info.node = RB_NEXT(name, head, best); \
729 if (comp < 0) /* error or termination */ \
731 infopp = &RB_INPROG(head); \
732 while (*infopp != &info) \
733 infopp = &(*infopp)->link; \
734 *infopp = info.link; \
741 name##_RB_NEXT(struct type *elm) \
743 if (RB_RIGHT(elm, field)) { \
744 elm = RB_RIGHT(elm, field); \
745 while (RB_LEFT(elm, field)) \
746 elm = RB_LEFT(elm, field); \
748 if (RB_PARENT(elm, field) && \
749 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
750 elm = RB_PARENT(elm, field); \
752 while (RB_PARENT(elm, field) && \
753 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
754 elm = RB_PARENT(elm, field); \
755 elm = RB_PARENT(elm, field); \
763 name##_RB_PREV(struct type *elm) \
765 if (RB_LEFT(elm, field)) { \
766 elm = RB_LEFT(elm, field); \
767 while (RB_RIGHT(elm, field)) \
768 elm = RB_RIGHT(elm, field); \
770 if (RB_PARENT(elm, field) && \
771 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
772 elm = RB_PARENT(elm, field); \
774 while (RB_PARENT(elm, field) && \
775 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
776 elm = RB_PARENT(elm, field); \
777 elm = RB_PARENT(elm, field); \
784 name##_RB_MINMAX(struct name *head, int val) \
786 struct type *tmp = RB_ROOT(head); \
787 struct type *parent = NULL; \
791 tmp = RB_LEFT(tmp, field); \
793 tmp = RB_RIGHT(tmp, field); \
799 * This extended version implements a fast LOOKUP function given
800 * a numeric data type.
802 * The element whos index/offset field is exactly the specified value
803 * will be returned, or NULL.
805 #define RB_GENERATE2(name, type, field, cmp, datatype, indexfield) \
806 RB_GENERATE(name, type, field, cmp) \
809 name##_RB_LOOKUP(struct name *head, datatype value) \
813 tmp = RB_ROOT(head); \
815 if (value > tmp->indexfield) \
816 tmp = RB_RIGHT(tmp, field); \
817 else if (value < tmp->indexfield) \
818 tmp = RB_LEFT(tmp, field); \
826 * This extended version implements a fast ranged-based LOOKUP function
827 * given a numeric data type, for data types with a beginning and end
828 * (end is inclusive).
830 * The element whos range contains the specified value is returned, or NULL
832 #define RB_GENERATE3(name, type, field, cmp, datatype, begfield, endfield) \
833 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \
836 name##_RB_RLOOKUP(struct name *head, datatype value) \
840 tmp = RB_ROOT(head); \
842 if (value >= tmp->begfield && value <= tmp->endfield) \
844 if (value > tmp->begfield) \
845 tmp = RB_RIGHT(tmp, field); \
847 tmp = RB_LEFT(tmp, field); \
853 * This extended version implements a fast ranged-based LOOKUP function
854 * given a numeric data type, for data types with a beginning and size.
856 * WARNING: The full range of the data type is not supported due to a
857 * boundary condition at the end, where (beginning + size) might overflow.
859 * The element whos range contains the specified value is returned, or NULL
861 #define RB_GENERATE4(name, type, field, cmp, datatype, begfield, sizefield) \
862 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \
865 name##_RB_RLOOKUP(struct name *head, datatype value) \
869 tmp = RB_ROOT(head); \
871 if (value >= tmp->begfield && \
872 value < tmp->begfield + tmp->sizefield) { \
875 if (value > tmp->begfield) \
876 tmp = RB_RIGHT(tmp, field); \
878 tmp = RB_LEFT(tmp, field); \
887 #define RB_INSERT(name, root, elm) name##_RB_INSERT(root, elm)
888 #define RB_REMOVE(name, root, elm) name##_RB_REMOVE(root, elm)
889 #define RB_FIND(name, root, elm) name##_RB_FIND(root, elm)
890 #define RB_LOOKUP(name, root, value) name##_RB_LOOKUP(root, value)
891 #define RB_RLOOKUP(name, root, value) name##_RB_RLOOKUP(root, value)
892 #define RB_SCAN(name, root, cmp, callback, data) \
893 name##_RB_SCAN(root, cmp, callback, data)
894 #define RB_NEXT(name, root, elm) name##_RB_NEXT(elm)
895 #define RB_PREV(name, root, elm) name##_RB_PREV(elm)
896 #define RB_MIN(name, root) name##_RB_MINMAX(root, RB_NEGINF)
897 #define RB_MAX(name, root) name##_RB_MINMAX(root, RB_INF)
899 #define RB_FOREACH(x, name, head) \
900 for ((x) = RB_MIN(name, head); \
902 (x) = name##_RB_NEXT(x))
904 #endif /* _SYS_TREE_H_ */