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 $ */
4 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _SYS_SPINLOCK_H_
32 #include <sys/spinlock.h>
35 void rb_spin_lock(struct spinlock
*spin
);
36 void rb_spin_unlock(struct spinlock
*spin
);
39 * This file defines data structures for different types of trees:
40 * splay trees and red-black trees.
42 * A splay tree is a self-organizing data structure. Every operation
43 * on the tree causes a splay to happen. The splay moves the requested
44 * node to the root of the tree and partly rebalances it.
46 * This has the benefit that request locality causes faster lookups as
47 * the requested nodes move to the top of the tree. On the other hand,
48 * every lookup causes memory writes.
50 * The Balance Theorem bounds the total access time for m operations
51 * and n inserts on an initially empty tree as O((m + n)lg n). The
52 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
54 * A red-black tree is a binary search tree with the node color as an
55 * extra attribute. It fulfills a set of conditions:
56 * - every search path from the root to a leaf consists of the
57 * same number of black nodes,
58 * - each red node (except for the root) has a black parent,
59 * - each leaf node is black.
61 * Every operation on a red-black tree is bounded as O(lg n).
62 * The maximum height of a red-black tree is 2lg (n+1).
65 #define SPLAY_HEAD(name, type) \
67 struct type *sph_root; /* root of the tree */ \
70 #define SPLAY_INITIALIZER(root) \
73 #define SPLAY_INIT(root) do { \
74 (root)->sph_root = NULL; \
75 } while (/*CONSTCOND*/ 0)
77 #define SPLAY_ENTRY(type) \
79 struct type *spe_left; /* left element */ \
80 struct type *spe_right; /* right element */ \
83 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
84 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
85 #define SPLAY_ROOT(head) (head)->sph_root
86 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
88 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
89 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
90 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
91 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
92 (head)->sph_root = tmp; \
93 } while (/*CONSTCOND*/ 0)
95 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
96 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
97 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
98 (head)->sph_root = tmp; \
99 } while (/*CONSTCOND*/ 0)
101 #define SPLAY_LINKLEFT(head, tmp, field) do { \
102 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
103 tmp = (head)->sph_root; \
104 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
105 } while (/*CONSTCOND*/ 0)
107 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
108 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
109 tmp = (head)->sph_root; \
110 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
111 } while (/*CONSTCOND*/ 0)
113 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
114 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
115 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
116 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
117 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
118 } while (/*CONSTCOND*/ 0)
120 /* Generates prototypes and inline functions */
122 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
123 void name##_SPLAY(struct name *, struct type *); \
124 void name##_SPLAY_MINMAX(struct name *, int); \
125 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
126 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
128 /* Finds the node with the same key as elm */ \
129 static __inline struct type * \
130 name##_SPLAY_FIND(struct name *head, struct type *elm) \
132 if (SPLAY_EMPTY(head)) \
134 name##_SPLAY(head, elm); \
135 if ((cmp)(elm, (head)->sph_root) == 0) \
136 return (head->sph_root); \
140 static __inline struct type * \
141 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
143 name##_SPLAY(head, elm); \
144 if (SPLAY_RIGHT(elm, field) != NULL) { \
145 elm = SPLAY_RIGHT(elm, field); \
146 while (SPLAY_LEFT(elm, field) != NULL) { \
147 elm = SPLAY_LEFT(elm, field); \
154 static __inline struct type * \
155 name##_SPLAY_MIN_MAX(struct name *head, int val) \
157 name##_SPLAY_MINMAX(head, val); \
158 return (SPLAY_ROOT(head)); \
161 /* Main splay operation.
162 * Moves node close to the key of elm to top
164 #define SPLAY_GENERATE(name, type, field, cmp) \
166 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
168 if (SPLAY_EMPTY(head)) { \
169 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
172 name##_SPLAY(head, elm); \
173 __comp = (cmp)(elm, (head)->sph_root); \
175 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
176 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
177 SPLAY_LEFT((head)->sph_root, field) = NULL; \
178 } else if (__comp > 0) { \
179 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
180 SPLAY_LEFT(elm, field) = (head)->sph_root; \
181 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
183 return ((head)->sph_root); \
185 (head)->sph_root = (elm); \
190 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
192 struct type *__tmp; \
193 if (SPLAY_EMPTY(head)) \
195 name##_SPLAY(head, elm); \
196 if ((cmp)(elm, (head)->sph_root) == 0) { \
197 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
198 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
200 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
201 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
202 name##_SPLAY(head, elm); \
203 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
211 name##_SPLAY(struct name *head, struct type *elm) \
213 struct type __node, *__left, *__right, *__tmp; \
216 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
217 __left = __right = &__node; \
219 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
221 __tmp = SPLAY_LEFT((head)->sph_root, field); \
224 if ((cmp)(elm, __tmp) < 0){ \
225 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
226 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
229 SPLAY_LINKLEFT(head, __right, field); \
230 } else if (__comp > 0) { \
231 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
234 if ((cmp)(elm, __tmp) > 0){ \
235 SPLAY_ROTATE_LEFT(head, __tmp, field); \
236 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
239 SPLAY_LINKRIGHT(head, __left, field); \
242 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
245 /* Splay with either the minimum or the maximum element \
246 * Used to find minimum or maximum element in tree. \
248 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
250 struct type __node, *__left, *__right, *__tmp; \
252 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
253 __left = __right = &__node; \
257 __tmp = SPLAY_LEFT((head)->sph_root, field); \
261 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
262 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
265 SPLAY_LINKLEFT(head, __right, field); \
266 } else if (__comp > 0) { \
267 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
271 SPLAY_ROTATE_LEFT(head, __tmp, field); \
272 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
275 SPLAY_LINKRIGHT(head, __left, field); \
278 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
281 #define SPLAY_NEGINF -1
284 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
285 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
286 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
287 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
288 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
289 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
290 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
291 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
293 #define SPLAY_FOREACH(x, name, head) \
294 for ((x) = SPLAY_MIN(name, head); \
296 (x) = SPLAY_NEXT(name, head, x))
299 * Macros that define a red-black tree
302 #define RB_SCAN_INFO(name, type) \
303 struct name##_scan_info { \
304 struct name##_scan_info *link; \
308 #define RB_HEAD(name, type) \
310 struct type *rbh_root; /* root of the tree */ \
311 struct name##_scan_info *rbh_inprog; /* scans in progress */ \
312 struct spinlock rbh_spin; \
315 #define RB_INITIALIZER(root) \
316 { NULL, NULL, SPINLOCK_INITIALIZER(root.spin, "root.spin") }
318 #define RB_INIT(root) do { \
319 (root)->rbh_root = NULL; \
320 (root)->rbh_inprog = NULL; \
321 } while (/*CONSTCOND*/ 0)
324 #define RB_SCAN_LOCK(spin) rb_spin_lock(spin)
325 #define RB_SCAN_UNLOCK(spin) rb_spin_unlock(spin)
327 #define RB_SCAN_LOCK(spin)
328 #define RB_SCAN_UNLOCK(spin)
333 #define RB_ENTRY(type) \
335 struct type *rbe_left; /* left element */ \
336 struct type *rbe_right; /* right element */ \
337 struct type *rbe_parent; /* parent element */ \
338 int rbe_color; /* node color */ \
341 #define RB_LEFT(elm, field) (elm)->field.rbe_left
342 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
343 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
344 #define RB_COLOR(elm, field) (elm)->field.rbe_color
345 #define RB_ROOT(head) (head)->rbh_root
346 #define RB_INPROG(head) (head)->rbh_inprog
347 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
349 #define RB_SET(elm, parent, field) do { \
350 RB_PARENT(elm, field) = parent; \
351 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
352 RB_COLOR(elm, field) = RB_RED; \
353 } while (/*CONSTCOND*/ 0)
355 #define RB_SET_BLACKRED(black, red, field) do { \
356 RB_COLOR(black, field) = RB_BLACK; \
357 RB_COLOR(red, field) = RB_RED; \
358 } while (/*CONSTCOND*/ 0)
361 #define RB_AUGMENT(x) do {} while (0)
364 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
365 (tmp) = RB_RIGHT(elm, field); \
366 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
367 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
370 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
371 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
372 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
374 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
376 (head)->rbh_root = (tmp); \
377 RB_LEFT(tmp, field) = (elm); \
378 RB_PARENT(elm, field) = (tmp); \
380 if ((RB_PARENT(tmp, field))) \
381 RB_AUGMENT(RB_PARENT(tmp, field)); \
382 } while (/*CONSTCOND*/ 0)
384 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
385 (tmp) = RB_LEFT(elm, field); \
386 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
387 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
390 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
391 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
392 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
394 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
396 (head)->rbh_root = (tmp); \
397 RB_RIGHT(tmp, field) = (elm); \
398 RB_PARENT(elm, field) = (tmp); \
400 if ((RB_PARENT(tmp, field))) \
401 RB_AUGMENT(RB_PARENT(tmp, field)); \
402 } while (/*CONSTCOND*/ 0)
404 /* Generates prototypes and inline functions */
405 #define RB_PROTOTYPE(name, type, field, cmp) \
406 _RB_PROTOTYPE(name, type, field, cmp,)
407 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
408 _RB_PROTOTYPE(name, type, field, cmp, __unused static)
410 #define _RB_PROTOTYPE(name, type, field, cmp, STORQUAL) \
411 STORQUAL void name##_RB_INSERT_COLOR(struct name *, struct type *); \
412 STORQUAL void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
413 STORQUAL struct type *name##_RB_REMOVE(struct name *, struct type *); \
414 STORQUAL struct type *name##_RB_INSERT(struct name *, struct type *); \
415 STORQUAL struct type *name##_RB_FIND(struct name *, struct type *); \
416 STORQUAL int name##_RB_SCAN(struct name *, int (*)(struct type *, void *),\
417 int (*)(struct type *, void *), void *); \
418 STORQUAL struct type *name##_RB_NEXT(struct type *); \
419 STORQUAL struct type *name##_RB_PREV(struct type *); \
420 STORQUAL struct type *name##_RB_MINMAX(struct name *, int); \
421 RB_SCAN_INFO(name, type) \
424 * A version which supplies a fast lookup routine for an exact match
425 * on a numeric field.
427 #define RB_PROTOTYPE2(name, type, field, cmp, datatype) \
428 RB_PROTOTYPE(name, type, field, cmp); \
429 struct type *name##_RB_LOOKUP(struct name *, datatype) \
432 * A version which supplies a fast lookup routine for a numeric
433 * field which resides within a ranged object, either using (begin,end),
434 * or using (begin,size).
436 #define RB_PROTOTYPE3(name, type, field, cmp, datatype) \
437 RB_PROTOTYPE2(name, type, field, cmp, datatype); \
438 struct type *name##_RB_RLOOKUP(struct name *, datatype) \
440 #define RB_PROTOTYPE4(name, type, field, cmp, datatype) \
441 RB_PROTOTYPE2(name, type, field, cmp, datatype); \
442 struct type *name##_RB_RLOOKUP(struct name *, datatype) \
444 #define RB_PROTOTYPEX(name, ext, type, field, cmp, datatype) \
445 RB_PROTOTYPE(name, type, field, cmp); \
446 struct type *name##_RB_LOOKUP_##ext (struct name *, datatype) \
448 /* Main rb operation.
449 * Moves node close to the key of elm to top
451 #define RB_GENERATE(name, type, field, cmp) \
452 _RB_GENERATE(name, type, field, cmp,)
454 #define RB_GENERATE_STATIC(name, type, field, cmp) \
455 _RB_GENERATE(name, type, field, cmp, __unused static)
457 #define _RB_GENERATE(name, type, field, cmp, STORQUAL) \
459 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
461 struct type *parent, *gparent, *tmp; \
462 while ((parent = RB_PARENT(elm, field)) != NULL && \
463 RB_COLOR(parent, field) == RB_RED) { \
464 gparent = RB_PARENT(parent, field); \
465 if (parent == RB_LEFT(gparent, field)) { \
466 tmp = RB_RIGHT(gparent, field); \
467 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
468 RB_COLOR(tmp, field) = RB_BLACK; \
469 RB_SET_BLACKRED(parent, gparent, field);\
473 if (RB_RIGHT(parent, field) == elm) { \
474 RB_ROTATE_LEFT(head, parent, tmp, field);\
479 RB_SET_BLACKRED(parent, gparent, field); \
480 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
482 tmp = RB_LEFT(gparent, field); \
483 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
484 RB_COLOR(tmp, field) = RB_BLACK; \
485 RB_SET_BLACKRED(parent, gparent, field);\
489 if (RB_LEFT(parent, field) == elm) { \
490 RB_ROTATE_RIGHT(head, parent, tmp, field);\
495 RB_SET_BLACKRED(parent, gparent, field); \
496 RB_ROTATE_LEFT(head, gparent, tmp, field); \
499 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
503 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \
507 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
508 elm != RB_ROOT(head)) { \
509 if (RB_LEFT(parent, field) == elm) { \
510 tmp = RB_RIGHT(parent, field); \
511 if (RB_COLOR(tmp, field) == RB_RED) { \
512 RB_SET_BLACKRED(tmp, parent, field); \
513 RB_ROTATE_LEFT(head, parent, tmp, field);\
514 tmp = RB_RIGHT(parent, field); \
516 if ((RB_LEFT(tmp, field) == NULL || \
517 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
518 (RB_RIGHT(tmp, field) == NULL || \
519 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
520 RB_COLOR(tmp, field) = RB_RED; \
522 parent = RB_PARENT(elm, field); \
524 if (RB_RIGHT(tmp, field) == NULL || \
525 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
526 struct type *oleft; \
527 if ((oleft = RB_LEFT(tmp, field)) \
529 RB_COLOR(oleft, field) = RB_BLACK;\
530 RB_COLOR(tmp, field) = RB_RED; \
531 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
532 tmp = RB_RIGHT(parent, field); \
534 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
535 RB_COLOR(parent, field) = RB_BLACK; \
536 if (RB_RIGHT(tmp, field)) \
537 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
538 RB_ROTATE_LEFT(head, parent, tmp, field);\
539 elm = RB_ROOT(head); \
543 tmp = RB_LEFT(parent, field); \
544 if (RB_COLOR(tmp, field) == RB_RED) { \
545 RB_SET_BLACKRED(tmp, parent, field); \
546 RB_ROTATE_RIGHT(head, parent, tmp, field);\
547 tmp = RB_LEFT(parent, field); \
549 if ((RB_LEFT(tmp, field) == NULL || \
550 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
551 (RB_RIGHT(tmp, field) == NULL || \
552 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
553 RB_COLOR(tmp, field) = RB_RED; \
555 parent = RB_PARENT(elm, field); \
557 if (RB_LEFT(tmp, field) == NULL || \
558 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
559 struct type *oright; \
560 if ((oright = RB_RIGHT(tmp, field)) \
562 RB_COLOR(oright, field) = RB_BLACK;\
563 RB_COLOR(tmp, field) = RB_RED; \
564 RB_ROTATE_LEFT(head, tmp, oright, field);\
565 tmp = RB_LEFT(parent, field); \
567 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
568 RB_COLOR(parent, field) = RB_BLACK; \
569 if (RB_LEFT(tmp, field)) \
570 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
571 RB_ROTATE_RIGHT(head, parent, tmp, field);\
572 elm = RB_ROOT(head); \
578 RB_COLOR(elm, field) = RB_BLACK; \
581 STORQUAL struct type * \
582 name##_RB_REMOVE(struct name *head, struct type *elm) \
584 struct type *child, *parent, *old; \
585 struct name##_scan_info *inprog; \
588 for (inprog = RB_INPROG(head); inprog; inprog = inprog->link) { \
589 if (inprog->node == elm) \
590 inprog->node = RB_NEXT(name, head, elm); \
594 if (RB_LEFT(elm, field) == NULL) \
595 child = RB_RIGHT(elm, field); \
596 else if (RB_RIGHT(elm, field) == NULL) \
597 child = RB_LEFT(elm, field); \
600 elm = RB_RIGHT(elm, field); \
601 while ((left = RB_LEFT(elm, field)) != NULL) \
603 child = RB_RIGHT(elm, field); \
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; \
616 if (RB_PARENT(elm, field) == old) \
618 (elm)->field = (old)->field; \
619 if (RB_PARENT(old, field)) { \
620 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
621 RB_LEFT(RB_PARENT(old, field), field) = elm;\
623 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
624 RB_AUGMENT(RB_PARENT(old, field)); \
626 RB_ROOT(head) = elm; \
627 RB_PARENT(RB_LEFT(old, field), field) = elm; \
628 if (RB_RIGHT(old, field)) \
629 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
634 } while ((left = RB_PARENT(left, field)) != NULL); \
638 parent = RB_PARENT(elm, field); \
639 color = RB_COLOR(elm, field); \
641 RB_PARENT(child, field) = parent; \
643 if (RB_LEFT(parent, field) == elm) \
644 RB_LEFT(parent, field) = child; \
646 RB_RIGHT(parent, field) = child; \
647 RB_AUGMENT(parent); \
649 RB_ROOT(head) = child; \
651 if (color == RB_BLACK) \
652 name##_RB_REMOVE_COLOR(head, parent, child); \
656 /* Inserts a node into the RB tree */ \
657 STORQUAL struct type * \
658 name##_RB_INSERT(struct name *head, struct type *elm) \
661 struct type *parent = NULL; \
663 tmp = RB_ROOT(head); \
666 comp = (cmp)(elm, parent); \
668 tmp = RB_LEFT(tmp, field); \
670 tmp = RB_RIGHT(tmp, field); \
674 RB_SET(elm, parent, field); \
675 if (parent != NULL) { \
677 RB_LEFT(parent, field) = elm; \
679 RB_RIGHT(parent, field) = elm; \
680 RB_AUGMENT(parent); \
682 RB_ROOT(head) = elm; \
683 name##_RB_INSERT_COLOR(head, elm); \
687 /* Finds the node with the same key as elm */ \
688 STORQUAL struct type * \
689 name##_RB_FIND(struct name *head, struct type *elm) \
691 struct type *tmp = RB_ROOT(head); \
694 comp = cmp(elm, tmp); \
696 tmp = RB_LEFT(tmp, field); \
698 tmp = RB_RIGHT(tmp, field); \
706 * Issue a callback for all matching items. The scan function must \
707 * return < 0 for items below the desired range, 0 for items within \
708 * the range, and > 0 for items beyond the range. Any item may be \
709 * deleted while the scan is in progress. \
712 name##_SCANCMP_ALL(struct type *type __unused, void *data __unused) \
717 static __inline void \
718 name##_scan_info_link(struct name##_scan_info *scan, struct name *head) \
720 RB_SCAN_LOCK(&head->rbh_spin); \
721 scan->link = RB_INPROG(head); \
722 RB_INPROG(head) = scan; \
723 RB_SCAN_UNLOCK(&head->rbh_spin); \
726 static __inline void \
727 name##_scan_info_done(struct name##_scan_info *scan, struct name *head) \
729 struct name##_scan_info **infopp; \
731 RB_SCAN_LOCK(&head->rbh_spin); \
732 infopp = &RB_INPROG(head); \
733 while (*infopp != scan) \
734 infopp = &(*infopp)->link; \
735 *infopp = scan->link; \
736 RB_SCAN_UNLOCK(&head->rbh_spin); \
740 name##_RB_SCAN(struct name *head, \
741 int (*scancmp)(struct type *, void *), \
742 int (*callback)(struct type *, void *), \
745 struct name##_scan_info info; \
751 if (scancmp == NULL) \
752 scancmp = name##_SCANCMP_ALL; \
755 * Locate the first element. \
757 tmp = RB_ROOT(head); \
760 comp = scancmp(tmp, data); \
762 tmp = RB_RIGHT(tmp, field); \
763 } else if (comp > 0) { \
764 tmp = RB_LEFT(tmp, field); \
767 if (RB_LEFT(tmp, field) == NULL) \
769 tmp = RB_LEFT(tmp, field); \
774 info.node = RB_NEXT(name, head, best); \
775 name##_scan_info_link(&info, head); \
776 while ((comp = callback(best, data)) >= 0) { \
779 if (best == NULL || scancmp(best, data) != 0) \
781 info.node = RB_NEXT(name, head, best); \
783 name##_scan_info_done(&info, head); \
784 if (comp < 0) /* error or termination */ \
791 STORQUAL struct type * \
792 name##_RB_NEXT(struct type *elm) \
794 if (RB_RIGHT(elm, field)) { \
795 elm = RB_RIGHT(elm, field); \
796 while (RB_LEFT(elm, field)) \
797 elm = RB_LEFT(elm, field); \
799 if (RB_PARENT(elm, field) && \
800 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
801 elm = RB_PARENT(elm, field); \
803 while (RB_PARENT(elm, field) && \
804 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
805 elm = RB_PARENT(elm, field); \
806 elm = RB_PARENT(elm, field); \
813 STORQUAL struct type * \
814 name##_RB_PREV(struct type *elm) \
816 if (RB_LEFT(elm, field)) { \
817 elm = RB_LEFT(elm, field); \
818 while (RB_RIGHT(elm, field)) \
819 elm = RB_RIGHT(elm, field); \
821 if (RB_PARENT(elm, field) && \
822 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
823 elm = RB_PARENT(elm, field); \
825 while (RB_PARENT(elm, field) && \
826 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
827 elm = RB_PARENT(elm, field); \
828 elm = RB_PARENT(elm, field); \
834 STORQUAL struct type * \
835 name##_RB_MINMAX(struct name *head, int val) \
837 struct type *tmp = RB_ROOT(head); \
838 struct type *parent = NULL; \
842 tmp = RB_LEFT(tmp, field); \
844 tmp = RB_RIGHT(tmp, field); \
850 * This extended version implements a fast LOOKUP function given
851 * a numeric data type.
853 * The element whos index/offset field is exactly the specified value
854 * will be returned, or NULL.
856 #define RB_GENERATE2(name, type, field, cmp, datatype, indexfield) \
857 RB_GENERATE(name, type, field, cmp) \
860 name##_RB_LOOKUP(struct name *head, datatype value) \
864 tmp = RB_ROOT(head); \
866 if (value > tmp->indexfield) \
867 tmp = RB_RIGHT(tmp, field); \
868 else if (value < tmp->indexfield) \
869 tmp = RB_LEFT(tmp, field); \
877 * This extended version implements a fast ranged-based LOOKUP function
878 * given a numeric data type, for data types with a beginning and end
879 * (end is inclusive).
881 * The element whos range contains the specified value is returned, or NULL
883 #define RB_GENERATE3(name, type, field, cmp, datatype, begfield, endfield) \
884 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \
887 name##_RB_RLOOKUP(struct name *head, datatype value) \
891 tmp = RB_ROOT(head); \
893 if (value >= tmp->begfield && value <= tmp->endfield) \
895 if (value > tmp->begfield) \
896 tmp = RB_RIGHT(tmp, field); \
898 tmp = RB_LEFT(tmp, field); \
904 * This extended version implements a fast ranged-based LOOKUP function
905 * given a numeric data type, for data types with a beginning and size.
907 * WARNING: The full range of the data type is not supported due to a
908 * boundary condition at the end, where (beginning + size) might overflow.
910 * The element whos range contains the specified value is returned, or NULL
912 #define RB_GENERATE4(name, type, field, cmp, datatype, begfield, sizefield) \
913 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \
916 name##_RB_RLOOKUP(struct name *head, datatype value) \
920 tmp = RB_ROOT(head); \
922 if (value >= tmp->begfield && \
923 value < tmp->begfield + tmp->sizefield) { \
926 if (value > tmp->begfield) \
927 tmp = RB_RIGHT(tmp, field); \
929 tmp = RB_LEFT(tmp, field); \
935 * This generates a custom lookup function for a red-black tree.
936 * Note that the macro may be used with a storage qualifier.
939 #define RB_GENERATE_XLOOKUP(name, ext, type, field, xcmp, datatype) \
940 _RB_GENERATE_XLOOKUP(name, ext, type, field, xcmp, datatype,)
941 #define RB_GENERATE_XLOOKUP_STATIC(name, ext, type, field, xcmp, datatype) \
942 _RB_GENERATE_XLOOKUP(name, ext, type, field, xcmp, datatype, __unused static)
944 #define _RB_GENERATE_XLOOKUP(name, ext, type, field, xcmp, datatype, STORQUAL)\
946 STORQUAL struct type * \
947 name##_RB_LOOKUP_##ext (struct name *head, datatype value) \
952 tmp = RB_ROOT(head); \
954 r = xcmp(value, tmp); \
958 tmp = RB_RIGHT(tmp, field); \
960 tmp = RB_LEFT(tmp, field); \
969 #define RB_INSERT(name, root, elm) name##_RB_INSERT(root, elm)
970 #define RB_REMOVE(name, root, elm) name##_RB_REMOVE(root, elm)
971 #define RB_FIND(name, root, elm) name##_RB_FIND(root, elm)
972 #define RB_LOOKUP(name, root, value) name##_RB_LOOKUP(root, value)
973 #define RB_RLOOKUP(name, root, value) name##_RB_RLOOKUP(root, value)
974 #define RB_SCAN(name, root, cmp, callback, data) \
975 name##_RB_SCAN(root, cmp, callback, data)
976 #define RB_FIRST(name, root) name##_RB_MINMAX(root, RB_NEGINF)
977 #define RB_NEXT(name, root, elm) name##_RB_NEXT(elm)
978 #define RB_PREV(name, root, elm) name##_RB_PREV(elm)
979 #define RB_MIN(name, root) name##_RB_MINMAX(root, RB_NEGINF)
980 #define RB_MAX(name, root) name##_RB_MINMAX(root, RB_INF)
982 #define RB_FOREACH(x, name, head) \
983 for ((x) = RB_MIN(name, head); \
985 (x) = name##_RB_NEXT(x))
987 #define RB_FOREACH_FROM(x, name, y) \
989 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
992 #define RB_FOREACH_SAFE(x, name, head, y) \
993 for ((x) = RB_MIN(name, head); \
994 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
997 #define RB_FOREACH_REVERSE(x, name, head) \
998 for ((x) = RB_MAX(name, head); \
1000 (x) = name##_RB_PREV(x))
1002 #define RB_FOREACH_REVERSE_FROM(x, name, y) \
1004 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
1007 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
1008 for ((x) = RB_MAX(name, head); \
1009 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
1012 #endif /* _SYS_TREE_H_ */