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.1 2004/08/19 20:38:33 joerg 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 */
293 #define RB_HEAD(name, type) \
295 struct type *rbh_root; /* root of the tree */ \
298 #define RB_INITIALIZER(root) \
301 #define RB_INIT(root) do { \
302 (root)->rbh_root = NULL; \
303 } while (/*CONSTCOND*/ 0)
307 #define RB_ENTRY(type) \
309 struct type *rbe_left; /* left element */ \
310 struct type *rbe_right; /* right element */ \
311 struct type *rbe_parent; /* parent element */ \
312 int rbe_color; /* node color */ \
315 #define RB_LEFT(elm, field) (elm)->field.rbe_left
316 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
317 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
318 #define RB_COLOR(elm, field) (elm)->field.rbe_color
319 #define RB_ROOT(head) (head)->rbh_root
320 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
322 #define RB_SET(elm, parent, field) do { \
323 RB_PARENT(elm, field) = parent; \
324 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
325 RB_COLOR(elm, field) = RB_RED; \
326 } while (/*CONSTCOND*/ 0)
328 #define RB_SET_BLACKRED(black, red, field) do { \
329 RB_COLOR(black, field) = RB_BLACK; \
330 RB_COLOR(red, field) = RB_RED; \
331 } while (/*CONSTCOND*/ 0)
334 #define RB_AUGMENT(x)
337 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
338 (tmp) = RB_RIGHT(elm, field); \
339 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
340 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
343 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
344 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
345 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
347 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
349 (head)->rbh_root = (tmp); \
350 RB_LEFT(tmp, field) = (elm); \
351 RB_PARENT(elm, field) = (tmp); \
353 if ((RB_PARENT(tmp, field))) \
354 RB_AUGMENT(RB_PARENT(tmp, field)); \
355 } while (/*CONSTCOND*/ 0)
357 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
358 (tmp) = RB_LEFT(elm, field); \
359 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
360 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
363 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
364 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
365 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
367 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
369 (head)->rbh_root = (tmp); \
370 RB_RIGHT(tmp, field) = (elm); \
371 RB_PARENT(elm, field) = (tmp); \
373 if ((RB_PARENT(tmp, field))) \
374 RB_AUGMENT(RB_PARENT(tmp, field)); \
375 } while (/*CONSTCOND*/ 0)
377 /* Generates prototypes and inline functions */
378 #define RB_PROTOTYPE(name, type, field, cmp) \
379 void name##_RB_INSERT_COLOR(struct name *, struct type *); \
380 void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
381 struct type *name##_RB_REMOVE(struct name *, struct type *); \
382 struct type *name##_RB_INSERT(struct name *, struct type *); \
383 struct type *name##_RB_FIND(struct name *, struct type *); \
384 struct type *name##_RB_NEXT(struct type *); \
385 struct type *name##_RB_MINMAX(struct name *, int); \
388 /* Main rb operation.
389 * Moves node close to the key of elm to top
391 #define RB_GENERATE(name, type, field, cmp) \
393 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
395 struct type *parent, *gparent, *tmp; \
396 while ((parent = RB_PARENT(elm, field)) != NULL && \
397 RB_COLOR(parent, field) == RB_RED) { \
398 gparent = RB_PARENT(parent, field); \
399 if (parent == RB_LEFT(gparent, field)) { \
400 tmp = RB_RIGHT(gparent, field); \
401 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
402 RB_COLOR(tmp, field) = RB_BLACK; \
403 RB_SET_BLACKRED(parent, gparent, field);\
407 if (RB_RIGHT(parent, field) == elm) { \
408 RB_ROTATE_LEFT(head, parent, tmp, field);\
413 RB_SET_BLACKRED(parent, gparent, field); \
414 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
416 tmp = RB_LEFT(gparent, field); \
417 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
418 RB_COLOR(tmp, field) = RB_BLACK; \
419 RB_SET_BLACKRED(parent, gparent, field);\
423 if (RB_LEFT(parent, field) == elm) { \
424 RB_ROTATE_RIGHT(head, parent, tmp, field);\
429 RB_SET_BLACKRED(parent, gparent, field); \
430 RB_ROTATE_LEFT(head, gparent, tmp, field); \
433 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
437 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
440 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
441 elm != RB_ROOT(head)) { \
442 if (RB_LEFT(parent, field) == elm) { \
443 tmp = RB_RIGHT(parent, field); \
444 if (RB_COLOR(tmp, field) == RB_RED) { \
445 RB_SET_BLACKRED(tmp, parent, field); \
446 RB_ROTATE_LEFT(head, parent, tmp, field);\
447 tmp = RB_RIGHT(parent, field); \
449 if ((RB_LEFT(tmp, field) == NULL || \
450 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
451 (RB_RIGHT(tmp, field) == NULL || \
452 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
453 RB_COLOR(tmp, field) = RB_RED; \
455 parent = RB_PARENT(elm, field); \
457 if (RB_RIGHT(tmp, field) == NULL || \
458 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
459 struct type *oleft; \
460 if ((oleft = RB_LEFT(tmp, field)) \
462 RB_COLOR(oleft, field) = RB_BLACK;\
463 RB_COLOR(tmp, field) = RB_RED; \
464 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
465 tmp = RB_RIGHT(parent, field); \
467 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
468 RB_COLOR(parent, field) = RB_BLACK; \
469 if (RB_RIGHT(tmp, field)) \
470 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
471 RB_ROTATE_LEFT(head, parent, tmp, field);\
472 elm = RB_ROOT(head); \
476 tmp = RB_LEFT(parent, field); \
477 if (RB_COLOR(tmp, field) == RB_RED) { \
478 RB_SET_BLACKRED(tmp, parent, field); \
479 RB_ROTATE_RIGHT(head, parent, tmp, field);\
480 tmp = RB_LEFT(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_LEFT(tmp, field) == NULL || \
491 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
492 struct type *oright; \
493 if ((oright = RB_RIGHT(tmp, field)) \
495 RB_COLOR(oright, field) = RB_BLACK;\
496 RB_COLOR(tmp, field) = RB_RED; \
497 RB_ROTATE_LEFT(head, tmp, oright, field);\
498 tmp = RB_LEFT(parent, field); \
500 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
501 RB_COLOR(parent, field) = RB_BLACK; \
502 if (RB_LEFT(tmp, field)) \
503 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
504 RB_ROTATE_RIGHT(head, parent, tmp, field);\
505 elm = RB_ROOT(head); \
511 RB_COLOR(elm, field) = RB_BLACK; \
515 name##_RB_REMOVE(struct name *head, struct type *elm) \
517 struct type *child, *parent, *old = elm; \
519 if (RB_LEFT(elm, field) == NULL) \
520 child = RB_RIGHT(elm, field); \
521 else if (RB_RIGHT(elm, field) == NULL) \
522 child = RB_LEFT(elm, field); \
525 elm = RB_RIGHT(elm, field); \
526 while ((left = RB_LEFT(elm, field)) != NULL) \
528 child = RB_RIGHT(elm, field); \
529 parent = RB_PARENT(elm, field); \
530 color = RB_COLOR(elm, field); \
532 RB_PARENT(child, field) = parent; \
534 if (RB_LEFT(parent, field) == elm) \
535 RB_LEFT(parent, field) = child; \
537 RB_RIGHT(parent, field) = child; \
538 RB_AUGMENT(parent); \
540 RB_ROOT(head) = child; \
541 if (RB_PARENT(elm, field) == old) \
543 (elm)->field = (old)->field; \
544 if (RB_PARENT(old, field)) { \
545 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
546 RB_LEFT(RB_PARENT(old, field), field) = elm;\
548 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
549 RB_AUGMENT(RB_PARENT(old, field)); \
551 RB_ROOT(head) = elm; \
552 RB_PARENT(RB_LEFT(old, field), field) = elm; \
553 if (RB_RIGHT(old, field)) \
554 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
559 } while ((left = RB_PARENT(left, field)) != NULL); \
563 parent = RB_PARENT(elm, field); \
564 color = RB_COLOR(elm, field); \
566 RB_PARENT(child, field) = parent; \
568 if (RB_LEFT(parent, field) == elm) \
569 RB_LEFT(parent, field) = child; \
571 RB_RIGHT(parent, field) = child; \
572 RB_AUGMENT(parent); \
574 RB_ROOT(head) = child; \
576 if (color == RB_BLACK) \
577 name##_RB_REMOVE_COLOR(head, parent, child); \
581 /* Inserts a node into the RB tree */ \
583 name##_RB_INSERT(struct name *head, struct type *elm) \
586 struct type *parent = NULL; \
588 tmp = RB_ROOT(head); \
591 comp = (cmp)(elm, parent); \
593 tmp = RB_LEFT(tmp, field); \
595 tmp = RB_RIGHT(tmp, field); \
599 RB_SET(elm, parent, field); \
600 if (parent != NULL) { \
602 RB_LEFT(parent, field) = elm; \
604 RB_RIGHT(parent, field) = elm; \
605 RB_AUGMENT(parent); \
607 RB_ROOT(head) = elm; \
608 name##_RB_INSERT_COLOR(head, elm); \
612 /* Finds the node with the same key as elm */ \
614 name##_RB_FIND(struct name *head, struct type *elm) \
616 struct type *tmp = RB_ROOT(head); \
619 comp = cmp(elm, tmp); \
621 tmp = RB_LEFT(tmp, field); \
623 tmp = RB_RIGHT(tmp, field); \
632 name##_RB_NEXT(struct type *elm) \
634 if (RB_RIGHT(elm, field)) { \
635 elm = RB_RIGHT(elm, field); \
636 while (RB_LEFT(elm, field)) \
637 elm = RB_LEFT(elm, field); \
639 if (RB_PARENT(elm, field) && \
640 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
641 elm = RB_PARENT(elm, field); \
643 while (RB_PARENT(elm, field) && \
644 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
645 elm = RB_PARENT(elm, field); \
646 elm = RB_PARENT(elm, field); \
653 name##_RB_MINMAX(struct name *head, int val) \
655 struct type *tmp = RB_ROOT(head); \
656 struct type *parent = NULL; \
660 tmp = RB_LEFT(tmp, field); \
662 tmp = RB_RIGHT(tmp, field); \
670 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
671 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
672 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
673 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
674 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
675 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
677 #define RB_FOREACH(x, name, head) \
678 for ((x) = RB_MIN(name, head); \
680 (x) = name##_RB_NEXT(x))
682 #endif /* _SYS_TREE_H_ */