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 $ */
6 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #define __unused __attribute__ ((__unused__))
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))
298 /* Macros that define a red-black tree */
299 #define RB_HEAD(name, type) \
301 struct type *rbh_root; /* root of the tree */ \
304 #define RB_INITIALIZER(root) \
307 #define RB_INIT(root) do { \
308 (root)->rbh_root = NULL; \
309 } while (/*CONSTCOND*/ 0)
313 #define RB_ENTRY(type) \
315 struct type *rbe_left; /* left element */ \
316 struct type *rbe_right; /* right element */ \
317 struct type *rbe_parent; /* parent element */ \
318 int rbe_color; /* node color */ \
321 #define RB_LEFT(elm, field) (elm)->field.rbe_left
322 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
323 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
324 #define RB_COLOR(elm, field) (elm)->field.rbe_color
325 #define RB_ROOT(head) (head)->rbh_root
326 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
328 #define RB_SET(elm, parent, field) do { \
329 RB_PARENT(elm, field) = parent; \
330 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
331 RB_COLOR(elm, field) = RB_RED; \
332 } while (/*CONSTCOND*/ 0)
334 #define RB_SET_BLACKRED(black, red, field) do { \
335 RB_COLOR(black, field) = RB_BLACK; \
336 RB_COLOR(red, field) = RB_RED; \
337 } while (/*CONSTCOND*/ 0)
340 #define RB_AUGMENT(x) do {} while (0)
343 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
344 (tmp) = RB_RIGHT(elm, field); \
345 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
346 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
349 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
350 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
351 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
353 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
355 (head)->rbh_root = (tmp); \
356 RB_LEFT(tmp, field) = (elm); \
357 RB_PARENT(elm, field) = (tmp); \
359 if ((RB_PARENT(tmp, field))) \
360 RB_AUGMENT(RB_PARENT(tmp, field)); \
361 } while (/*CONSTCOND*/ 0)
363 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
364 (tmp) = RB_LEFT(elm, field); \
365 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
366 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
369 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
370 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
371 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
373 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
375 (head)->rbh_root = (tmp); \
376 RB_RIGHT(tmp, field) = (elm); \
377 RB_PARENT(elm, field) = (tmp); \
379 if ((RB_PARENT(tmp, field))) \
380 RB_AUGMENT(RB_PARENT(tmp, field)); \
381 } while (/*CONSTCOND*/ 0)
383 /* Generates prototypes and inline functions */
384 #define RB_PROTOTYPE(name, type, field, cmp) \
385 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
386 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
387 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
388 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
389 RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
390 RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
391 RB_PROTOTYPE_INSERT(name, type, attr); \
392 RB_PROTOTYPE_REMOVE(name, type, attr); \
393 RB_PROTOTYPE_FIND(name, type, attr); \
394 RB_PROTOTYPE_NFIND(name, type, attr); \
395 RB_PROTOTYPE_NEXT(name, type, attr); \
396 RB_PROTOTYPE_PREV(name, type, attr); \
397 RB_PROTOTYPE_MINMAX(name, type, attr);
398 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
399 attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
400 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
401 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *)
402 #define RB_PROTOTYPE_REMOVE(name, type, attr) \
403 attr struct type *name##_RB_REMOVE(struct name *, struct type *)
404 #define RB_PROTOTYPE_INSERT(name, type, attr) \
405 attr struct type *name##_RB_INSERT(struct name *, struct type *)
406 #define RB_PROTOTYPE_FIND(name, type, attr) \
407 attr struct type *name##_RB_FIND(struct name *, struct type *)
408 #define RB_PROTOTYPE_NFIND(name, type, attr) \
409 attr struct type *name##_RB_NFIND(struct name *, struct type *)
410 #define RB_PROTOTYPE_NEXT(name, type, attr) \
411 attr struct type *name##_RB_NEXT(struct type *)
412 #define RB_PROTOTYPE_PREV(name, type, attr) \
413 attr struct type *name##_RB_PREV(struct type *)
414 #define RB_PROTOTYPE_MINMAX(name, type, attr) \
415 attr struct type *name##_RB_MINMAX(struct name *, int)
417 /* Main rb operation.
418 * Moves node close to the key of elm to top
420 #define RB_GENERATE(name, type, field, cmp) \
421 RB_GENERATE_INTERNAL(name, type, field, cmp,)
422 #define RB_GENERATE_STATIC(name, type, field, cmp) \
423 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
424 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
425 RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
426 RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
427 RB_GENERATE_INSERT(name, type, field, cmp, attr) \
428 RB_GENERATE_REMOVE(name, type, field, attr) \
429 RB_GENERATE_FIND(name, type, field, cmp, attr) \
430 RB_GENERATE_NFIND(name, type, field, cmp, attr) \
431 RB_GENERATE_NEXT(name, type, field, attr) \
432 RB_GENERATE_PREV(name, type, field, attr) \
433 RB_GENERATE_MINMAX(name, type, field, attr)
435 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
437 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
439 struct type *parent, *gparent, *tmp; \
440 while ((parent = RB_PARENT(elm, field)) != NULL && \
441 RB_COLOR(parent, field) == RB_RED) { \
442 gparent = RB_PARENT(parent, field); \
443 if (parent == RB_LEFT(gparent, field)) { \
444 tmp = RB_RIGHT(gparent, field); \
445 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
446 RB_COLOR(tmp, field) = RB_BLACK; \
447 RB_SET_BLACKRED(parent, gparent, field);\
451 if (RB_RIGHT(parent, field) == elm) { \
452 RB_ROTATE_LEFT(head, parent, tmp, field);\
457 RB_SET_BLACKRED(parent, gparent, field); \
458 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
460 tmp = RB_LEFT(gparent, field); \
461 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
462 RB_COLOR(tmp, field) = RB_BLACK; \
463 RB_SET_BLACKRED(parent, gparent, field);\
467 if (RB_LEFT(parent, field) == elm) { \
468 RB_ROTATE_RIGHT(head, parent, tmp, field);\
473 RB_SET_BLACKRED(parent, gparent, field); \
474 RB_ROTATE_LEFT(head, gparent, tmp, field); \
477 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
480 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
482 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
485 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
486 elm != RB_ROOT(head)) { \
487 if (RB_LEFT(parent, field) == elm) { \
488 tmp = RB_RIGHT(parent, field); \
489 if (RB_COLOR(tmp, field) == RB_RED) { \
490 RB_SET_BLACKRED(tmp, parent, field); \
491 RB_ROTATE_LEFT(head, parent, tmp, field);\
492 tmp = RB_RIGHT(parent, field); \
494 if ((RB_LEFT(tmp, field) == NULL || \
495 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
496 (RB_RIGHT(tmp, field) == NULL || \
497 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
498 RB_COLOR(tmp, field) = RB_RED; \
500 parent = RB_PARENT(elm, field); \
502 if (RB_RIGHT(tmp, field) == NULL || \
503 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
504 struct type *oleft; \
505 if ((oleft = RB_LEFT(tmp, field)) \
507 RB_COLOR(oleft, field) = RB_BLACK;\
508 RB_COLOR(tmp, field) = RB_RED; \
509 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
510 tmp = RB_RIGHT(parent, field); \
512 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
513 RB_COLOR(parent, field) = RB_BLACK; \
514 if (RB_RIGHT(tmp, field)) \
515 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
516 RB_ROTATE_LEFT(head, parent, tmp, field);\
517 elm = RB_ROOT(head); \
521 tmp = RB_LEFT(parent, field); \
522 if (RB_COLOR(tmp, field) == RB_RED) { \
523 RB_SET_BLACKRED(tmp, parent, field); \
524 RB_ROTATE_RIGHT(head, parent, tmp, field);\
525 tmp = RB_LEFT(parent, field); \
527 if ((RB_LEFT(tmp, field) == NULL || \
528 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
529 (RB_RIGHT(tmp, field) == NULL || \
530 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
531 RB_COLOR(tmp, field) = RB_RED; \
533 parent = RB_PARENT(elm, field); \
535 if (RB_LEFT(tmp, field) == NULL || \
536 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
537 struct type *oright; \
538 if ((oright = RB_RIGHT(tmp, field)) \
540 RB_COLOR(oright, field) = RB_BLACK;\
541 RB_COLOR(tmp, field) = RB_RED; \
542 RB_ROTATE_LEFT(head, tmp, oright, field);\
543 tmp = RB_LEFT(parent, field); \
545 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
546 RB_COLOR(parent, field) = RB_BLACK; \
547 if (RB_LEFT(tmp, field)) \
548 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
549 RB_ROTATE_RIGHT(head, parent, tmp, field);\
550 elm = RB_ROOT(head); \
556 RB_COLOR(elm, field) = RB_BLACK; \
559 #define RB_GENERATE_REMOVE(name, type, field, attr) \
561 name##_RB_REMOVE(struct name *head, struct type *elm) \
563 struct type *child, *parent, *old = elm; \
565 if (RB_LEFT(elm, field) == NULL) \
566 child = RB_RIGHT(elm, field); \
567 else if (RB_RIGHT(elm, field) == NULL) \
568 child = RB_LEFT(elm, field); \
571 elm = RB_RIGHT(elm, field); \
572 while ((left = RB_LEFT(elm, field)) != NULL) \
574 child = RB_RIGHT(elm, field); \
575 parent = RB_PARENT(elm, field); \
576 color = RB_COLOR(elm, field); \
578 RB_PARENT(child, field) = parent; \
580 if (RB_LEFT(parent, field) == elm) \
581 RB_LEFT(parent, field) = child; \
583 RB_RIGHT(parent, field) = child; \
584 RB_AUGMENT(parent); \
586 RB_ROOT(head) = child; \
587 if (RB_PARENT(elm, field) == old) \
589 (elm)->field = (old)->field; \
590 if (RB_PARENT(old, field)) { \
591 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
592 RB_LEFT(RB_PARENT(old, field), field) = elm;\
594 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
595 RB_AUGMENT(RB_PARENT(old, field)); \
597 RB_ROOT(head) = elm; \
598 RB_PARENT(RB_LEFT(old, field), field) = elm; \
599 if (RB_RIGHT(old, field)) \
600 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
605 } while ((left = RB_PARENT(left, field)) != NULL); \
609 parent = RB_PARENT(elm, field); \
610 color = RB_COLOR(elm, field); \
612 RB_PARENT(child, field) = parent; \
614 if (RB_LEFT(parent, field) == elm) \
615 RB_LEFT(parent, field) = child; \
617 RB_RIGHT(parent, field) = child; \
618 RB_AUGMENT(parent); \
620 RB_ROOT(head) = child; \
622 if (color == RB_BLACK) \
623 name##_RB_REMOVE_COLOR(head, parent, child); \
627 #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
628 /* Inserts a node into the RB tree */ \
630 name##_RB_INSERT(struct name *head, struct type *elm) \
633 struct type *parent = NULL; \
635 tmp = RB_ROOT(head); \
638 comp = (cmp)(elm, parent); \
640 tmp = RB_LEFT(tmp, field); \
642 tmp = RB_RIGHT(tmp, field); \
646 RB_SET(elm, parent, field); \
647 if (parent != NULL) { \
649 RB_LEFT(parent, field) = elm; \
651 RB_RIGHT(parent, field) = elm; \
652 RB_AUGMENT(parent); \
654 RB_ROOT(head) = elm; \
655 name##_RB_INSERT_COLOR(head, elm); \
659 #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
660 /* Finds the node with the same key as elm */ \
662 name##_RB_FIND(struct name *head, struct type *elm) \
664 struct type *tmp = RB_ROOT(head); \
667 comp = cmp(elm, tmp); \
669 tmp = RB_LEFT(tmp, field); \
671 tmp = RB_RIGHT(tmp, field); \
678 #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
679 /* Finds the first node greater than or equal to the search key */ \
681 name##_RB_NFIND(struct name *head, struct type *elm) \
683 struct type *tmp = RB_ROOT(head); \
684 struct type *res = NULL; \
687 comp = cmp(elm, tmp); \
690 tmp = RB_LEFT(tmp, field); \
693 tmp = RB_RIGHT(tmp, field); \
700 #define RB_GENERATE_NEXT(name, type, field, attr) \
703 name##_RB_NEXT(struct type *elm) \
705 if (RB_RIGHT(elm, field)) { \
706 elm = RB_RIGHT(elm, field); \
707 while (RB_LEFT(elm, field)) \
708 elm = RB_LEFT(elm, field); \
710 if (RB_PARENT(elm, field) && \
711 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
712 elm = RB_PARENT(elm, field); \
714 while (RB_PARENT(elm, field) && \
715 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
716 elm = RB_PARENT(elm, field); \
717 elm = RB_PARENT(elm, field); \
723 #define RB_GENERATE_PREV(name, type, field, attr) \
726 name##_RB_PREV(struct type *elm) \
728 if (RB_LEFT(elm, field)) { \
729 elm = RB_LEFT(elm, field); \
730 while (RB_RIGHT(elm, field)) \
731 elm = RB_RIGHT(elm, field); \
733 if (RB_PARENT(elm, field) && \
734 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
735 elm = RB_PARENT(elm, field); \
737 while (RB_PARENT(elm, field) && \
738 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
739 elm = RB_PARENT(elm, field); \
740 elm = RB_PARENT(elm, field); \
746 #define RB_GENERATE_MINMAX(name, type, field, attr) \
748 name##_RB_MINMAX(struct name *head, int val) \
750 struct type *tmp = RB_ROOT(head); \
751 struct type *parent = NULL; \
755 tmp = RB_LEFT(tmp, field); \
757 tmp = RB_RIGHT(tmp, field); \
765 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
766 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
767 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
768 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
769 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
770 #define RB_PREV(name, x, y) name##_RB_PREV(y)
771 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
772 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
774 #define RB_FOREACH(x, name, head) \
775 for ((x) = RB_MIN(name, head); \
777 (x) = name##_RB_NEXT(x))
779 #define RB_FOREACH_FROM(x, name, y) \
781 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
784 #define RB_FOREACH_SAFE(x, name, head, y) \
785 for ((x) = RB_MIN(name, head); \
786 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
789 #define RB_FOREACH_REVERSE(x, name, head) \
790 for ((x) = RB_MAX(name, head); \
792 (x) = name##_RB_PREV(x))
794 #define RB_FOREACH_REVERSE_FROM(x, name, y) \
796 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
799 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
800 for ((x) = RB_MAX(name, head); \
801 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
804 #endif /* _SYS_TREE_H_ */