2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This file defines data structures for different types of trees:
31 * splay trees and red-black trees.
33 * A splay tree is a self-organizing data structure. Every operation
34 * on the tree causes a splay to happen. The splay moves the requested
35 * node to the root of the tree and partly rebalances it.
37 * This has the benefit that request locality causes faster lookups as
38 * the requested nodes move to the top of the tree. On the other hand,
39 * every lookup causes memory writes.
41 * The Balance Theorem bounds the total access time for m operations
42 * and n inserts on an initially empty tree as O((m + n)lg n). The
43 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
45 * A red-black tree is a binary search tree with the node color as an
46 * extra attribute. It fulfills a set of conditions:
47 * - every search path from the root to a leaf consists of the
48 * same number of black nodes,
49 * - each red node (except for the root) has a black parent,
50 * - each leaf node is black.
52 * Every operation on a red-black tree is bounded as O(lg n).
53 * The maximum height of a red-black tree is 2lg (n+1).
56 #define SPLAY_HEAD(name, type) \
58 struct type *sph_root; /* root of the tree */ \
61 #define SPLAY_INITIALIZER(root) \
64 #define SPLAY_INIT(root) do { \
65 (root)->sph_root = NULL; \
68 #define SPLAY_ENTRY(type) \
70 struct type *spe_left; /* left element */ \
71 struct type *spe_right; /* right element */ \
74 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
75 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
76 #define SPLAY_ROOT(head) (head)->sph_root
77 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
79 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
80 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
81 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
82 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
83 (head)->sph_root = tmp; \
86 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
87 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
88 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
89 (head)->sph_root = tmp; \
92 #define SPLAY_LINKLEFT(head, tmp, field) do { \
93 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
94 tmp = (head)->sph_root; \
95 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
98 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
99 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
100 tmp = (head)->sph_root; \
101 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
104 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
105 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
106 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
107 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
108 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
111 /* Generates prototypes and inline functions */
113 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
114 void name##_SPLAY(struct name *, struct type *); \
115 void name##_SPLAY_MINMAX(struct name *, int); \
117 static __inline void \
118 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
120 if (SPLAY_EMPTY(head)) { \
121 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
124 name##_SPLAY(head, elm); \
125 __comp = (cmp)(elm, (head)->sph_root); \
127 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
128 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
129 SPLAY_LEFT((head)->sph_root, field) = NULL; \
130 } else if (__comp > 0) { \
131 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
132 SPLAY_LEFT(elm, field) = (head)->sph_root; \
133 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
137 (head)->sph_root = (elm); \
140 static __inline void \
141 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
143 struct type *__tmp; \
144 if (SPLAY_EMPTY(head)) \
146 name##_SPLAY(head, elm); \
147 if ((cmp)(elm, (head)->sph_root) == 0) { \
148 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
149 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
151 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
152 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
153 name##_SPLAY(head, elm); \
154 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
159 /* Finds the node with the same key as elm */ \
160 static __inline struct type * \
161 name##_SPLAY_FIND(struct name *head, struct type *elm) \
163 if (SPLAY_EMPTY(head)) \
165 name##_SPLAY(head, elm); \
166 if ((cmp)(elm, (head)->sph_root) == 0) \
167 return (head->sph_root); \
171 static __inline struct type * \
172 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
174 name##_SPLAY(head, elm); \
175 if (SPLAY_RIGHT(elm, field) != NULL) { \
176 elm = SPLAY_RIGHT(elm, field); \
177 while (SPLAY_LEFT(elm, field) != NULL) { \
178 elm = SPLAY_LEFT(elm, field); \
185 static __inline struct type * \
186 name##_SPLAY_MIN_MAX(struct name *head, int val) \
188 name##_SPLAY_MINMAX(head, val); \
189 return (SPLAY_ROOT(head)); \
192 /* Main splay operation.
193 * Moves node close to the key of elm to top
195 #define SPLAY_GENERATE(name, type, field, cmp) \
196 void name##_SPLAY(struct name *head, struct type *elm) \
198 struct type __node, *__left, *__right, *__tmp; \
201 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
202 __left = __right = &__node; \
204 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
206 __tmp = SPLAY_LEFT((head)->sph_root, field); \
209 if ((cmp)(elm, __tmp) < 0){ \
210 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
211 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
214 SPLAY_LINKLEFT(head, __right, field); \
215 } else if (__comp > 0) { \
216 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
219 if ((cmp)(elm, __tmp) > 0){ \
220 SPLAY_ROTATE_LEFT(head, __tmp, field); \
221 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
224 SPLAY_LINKRIGHT(head, __left, field); \
227 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
230 /* Splay with either the minimum or the maximum element \
231 * Used to find minimum or maximum element in tree. \
233 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
235 struct type __node, *__left, *__right, *__tmp; \
237 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
238 __left = __right = &__node; \
242 __tmp = SPLAY_LEFT((head)->sph_root, field); \
246 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
247 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
250 SPLAY_LINKLEFT(head, __right, field); \
251 } else if (__comp > 0) { \
252 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
256 SPLAY_ROTATE_LEFT(head, __tmp, field); \
257 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
260 SPLAY_LINKRIGHT(head, __left, field); \
263 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
266 #define SPLAY_NEGINF -1
269 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
270 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
271 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
272 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
273 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
274 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
275 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
276 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
278 #define SPLAY_FOREACH(x, name, head) \
279 for ((x) = SPLAY_MIN(name, head); \
281 (x) = SPLAY_NEXT(name, head, x))
283 /* Macros that define a red-back tree */
284 #define RB_HEAD(name, type) \
286 struct type *rbh_root; /* root of the tree */ \
289 #define RB_INITIALIZER(root) \
292 #define RB_INIT(root) do { \
293 (root)->rbh_root = NULL; \
298 #define RB_ENTRY(type) \
300 struct type *rbe_left; /* left element */ \
301 struct type *rbe_right; /* right element */ \
302 struct type *rbe_parent; /* parent element */ \
303 int rbe_color; /* node color */ \
306 #define RB_LEFT(elm, field) (elm)->field.rbe_left
307 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
308 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
309 #define RB_COLOR(elm, field) (elm)->field.rbe_color
310 #define RB_ROOT(head) (head)->rbh_root
311 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
313 #define RB_SET(elm, parent, field) do { \
314 RB_PARENT(elm, field) = parent; \
315 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
316 RB_COLOR(elm, field) = RB_RED; \
319 #define RB_SET_BLACKRED(black, red, field) do { \
320 RB_COLOR(black, field) = RB_BLACK; \
321 RB_COLOR(red, field) = RB_RED; \
325 #define RB_AUGMENT(x)
328 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
329 (tmp) = RB_RIGHT(elm, field); \
330 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
331 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
334 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
335 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
336 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
338 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
339 RB_AUGMENT(RB_PARENT(elm, field)); \
341 (head)->rbh_root = (tmp); \
342 RB_LEFT(tmp, field) = (elm); \
343 RB_PARENT(elm, field) = (tmp); \
347 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
348 (tmp) = RB_LEFT(elm, field); \
349 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
350 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
353 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
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); \
358 RB_AUGMENT(RB_PARENT(elm, field)); \
360 (head)->rbh_root = (tmp); \
361 RB_RIGHT(tmp, field) = (elm); \
362 RB_PARENT(elm, field) = (tmp); \
366 /* Generates prototypes and inline functions */
367 #define RB_PROTOTYPE(name, type, field, cmp) \
368 void name##_RB_INSERT_COLOR(struct name *, struct type *); \
369 void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
370 void name##_RB_REMOVE(struct name *, struct type *); \
371 struct type *name##_RB_INSERT(struct name *, struct type *); \
372 struct type *name##_RB_FIND(struct name *, struct type *); \
373 struct type *name##_RB_NEXT(struct name *, struct type *); \
374 struct type *name##_RB_MINMAX(struct name *, int); \
377 /* Main rb operation.
378 * Moves node close to the key of elm to top
380 #define RB_GENERATE(name, type, field, cmp) \
382 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
384 struct type *parent, *gparent, *tmp; \
385 while ((parent = RB_PARENT(elm, field)) && \
386 RB_COLOR(parent, field) == RB_RED) { \
387 gparent = RB_PARENT(parent, field); \
388 if (parent == RB_LEFT(gparent, field)) { \
389 tmp = RB_RIGHT(gparent, field); \
390 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
391 RB_COLOR(tmp, field) = RB_BLACK; \
392 RB_SET_BLACKRED(parent, gparent, field);\
396 if (RB_RIGHT(parent, field) == elm) { \
397 RB_ROTATE_LEFT(head, parent, tmp, field);\
402 RB_SET_BLACKRED(parent, gparent, field); \
403 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
405 tmp = RB_LEFT(gparent, field); \
406 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
407 RB_COLOR(tmp, field) = RB_BLACK; \
408 RB_SET_BLACKRED(parent, gparent, field);\
412 if (RB_LEFT(parent, field) == elm) { \
413 RB_ROTATE_RIGHT(head, parent, tmp, field);\
418 RB_SET_BLACKRED(parent, gparent, field); \
419 RB_ROTATE_LEFT(head, gparent, tmp, field); \
422 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
426 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
429 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
430 elm != RB_ROOT(head)) { \
431 if (RB_LEFT(parent, field) == elm) { \
432 tmp = RB_RIGHT(parent, field); \
433 if (RB_COLOR(tmp, field) == RB_RED) { \
434 RB_SET_BLACKRED(tmp, parent, field); \
435 RB_ROTATE_LEFT(head, parent, tmp, field);\
436 tmp = RB_RIGHT(parent, field); \
438 if ((RB_LEFT(tmp, field) == NULL || \
439 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
440 (RB_RIGHT(tmp, field) == NULL || \
441 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
442 RB_COLOR(tmp, field) = RB_RED; \
444 parent = RB_PARENT(elm, field); \
446 if (RB_RIGHT(tmp, field) == NULL || \
447 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
448 struct type *oleft; \
449 if ((oleft = RB_LEFT(tmp, field)))\
450 RB_COLOR(oleft, field) = RB_BLACK;\
451 RB_COLOR(tmp, field) = RB_RED; \
452 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
453 tmp = RB_RIGHT(parent, field); \
455 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
456 RB_COLOR(parent, field) = RB_BLACK; \
457 if (RB_RIGHT(tmp, field)) \
458 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
459 RB_ROTATE_LEFT(head, parent, tmp, field);\
460 elm = RB_ROOT(head); \
464 tmp = RB_LEFT(parent, field); \
465 if (RB_COLOR(tmp, field) == RB_RED) { \
466 RB_SET_BLACKRED(tmp, parent, field); \
467 RB_ROTATE_RIGHT(head, parent, tmp, field);\
468 tmp = RB_LEFT(parent, field); \
470 if ((RB_LEFT(tmp, field) == NULL || \
471 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
472 (RB_RIGHT(tmp, field) == NULL || \
473 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
474 RB_COLOR(tmp, field) = RB_RED; \
476 parent = RB_PARENT(elm, field); \
478 if (RB_LEFT(tmp, field) == NULL || \
479 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
480 struct type *oright; \
481 if ((oright = RB_RIGHT(tmp, field)))\
482 RB_COLOR(oright, field) = RB_BLACK;\
483 RB_COLOR(tmp, field) = RB_RED; \
484 RB_ROTATE_LEFT(head, tmp, oright, field);\
485 tmp = RB_LEFT(parent, field); \
487 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
488 RB_COLOR(parent, field) = RB_BLACK; \
489 if (RB_LEFT(tmp, field)) \
490 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
491 RB_ROTATE_RIGHT(head, parent, tmp, field);\
492 elm = RB_ROOT(head); \
498 RB_COLOR(elm, field) = RB_BLACK; \
502 name##_RB_REMOVE(struct name *head, struct type *elm) \
504 struct type *child, *parent; \
506 if (RB_LEFT(elm, field) == NULL) \
507 child = RB_RIGHT(elm, field); \
508 else if (RB_RIGHT(elm, field) == NULL) \
509 child = RB_LEFT(elm, field); \
511 struct type *old = elm, *left; \
512 elm = RB_RIGHT(elm, field); \
513 while ((left = RB_LEFT(elm, field))) \
515 child = RB_RIGHT(elm, field); \
516 parent = RB_PARENT(elm, field); \
517 color = RB_COLOR(elm, field); \
519 RB_PARENT(child, field) = parent; \
521 if (RB_LEFT(parent, field) == elm) \
522 RB_LEFT(parent, field) = child; \
524 RB_RIGHT(parent, field) = child; \
525 RB_AUGMENT(parent); \
527 RB_ROOT(head) = child; \
528 if (RB_PARENT(elm, field) == old) \
530 (elm)->field = (old)->field; \
531 if (RB_PARENT(old, field)) { \
532 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
533 RB_LEFT(RB_PARENT(old, field), field) = elm;\
535 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
536 RB_AUGMENT(RB_PARENT(old, field)); \
538 RB_ROOT(head) = elm; \
539 RB_PARENT(RB_LEFT(old, field), field) = elm; \
540 if (RB_RIGHT(old, field)) \
541 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
546 } while ((left = RB_PARENT(left, field))); \
550 parent = RB_PARENT(elm, field); \
551 color = RB_COLOR(elm, field); \
553 RB_PARENT(child, field) = parent; \
555 if (RB_LEFT(parent, field) == elm) \
556 RB_LEFT(parent, field) = child; \
558 RB_RIGHT(parent, field) = child; \
559 RB_AUGMENT(parent); \
561 RB_ROOT(head) = child; \
563 if (color == RB_BLACK) \
564 name##_RB_REMOVE_COLOR(head, parent, child); \
567 /* Inserts a node into the RB tree */ \
569 name##_RB_INSERT(struct name *head, struct type *elm) \
572 struct type *parent = NULL; \
574 tmp = RB_ROOT(head); \
577 comp = (cmp)(elm, parent); \
579 tmp = RB_LEFT(tmp, field); \
581 tmp = RB_RIGHT(tmp, field); \
585 RB_SET(elm, parent, field); \
586 if (parent != NULL) { \
588 RB_LEFT(parent, field) = elm; \
590 RB_RIGHT(parent, field) = elm; \
591 RB_AUGMENT(parent); \
593 RB_ROOT(head) = elm; \
594 name##_RB_INSERT_COLOR(head, elm); \
598 /* Finds the node with the same key as elm */ \
600 name##_RB_FIND(struct name *head, struct type *elm) \
602 struct type *tmp = RB_ROOT(head); \
605 comp = cmp(elm, tmp); \
607 tmp = RB_LEFT(tmp, field); \
609 tmp = RB_RIGHT(tmp, field); \
617 name##_RB_NEXT(struct name *head, struct type *elm) \
619 if (RB_RIGHT(elm, field)) { \
620 elm = RB_RIGHT(elm, field); \
621 while (RB_LEFT(elm, field)) \
622 elm = RB_LEFT(elm, field); \
624 if (RB_PARENT(elm, field) && \
625 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
626 elm = RB_PARENT(elm, field); \
628 while (RB_PARENT(elm, field) && \
629 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
630 elm = RB_PARENT(elm, field); \
631 elm = RB_PARENT(elm, field); \
638 name##_RB_MINMAX(struct name *head, int val) \
640 struct type *tmp = RB_ROOT(head); \
641 struct type *parent = NULL; \
645 tmp = RB_LEFT(tmp, field); \
647 tmp = RB_RIGHT(tmp, field); \
655 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
656 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
657 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
658 #define RB_NEXT(name, x, y) name##_RB_NEXT(x, y)
659 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
660 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
662 #define RB_FOREACH(x, name, head) \
663 for ((x) = RB_MIN(name, head); \
665 (x) = name##_RB_NEXT(head, x))
667 #endif /* _SYS_TREE_H_ */