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.
37 #include "ext4_config.h"
40 * This file defines data structures for different types of trees:
41 * splay trees and red-black trees.
43 * A splay tree is a self-organizing data structure. Every operation
44 * on the tree causes a splay to happen. The splay moves the requested
45 * node to the root of the tree and partly rebalances it.
47 * This has the benefit that request locality causes faster lookups as
48 * the requested nodes move to the top of the tree. On the other hand,
49 * every lookup causes memory writes.
51 * The Balance Theorem bounds the total access time for m operations
52 * and n inserts on an initially empty tree as O((m + n)lg n). The
53 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
55 * A red-black tree is a binary search tree with the node color as an
56 * extra attribute. It fulfills a set of conditions:
57 * - every search path from the root to a leaf consists of the
58 * same number of black nodes,
59 * - each red node (except for the root) has a black parent,
60 * - each leaf node is black.
62 * Every operation on a red-black tree is bounded as O(lg n).
63 * The maximum height of a red-black tree is 2lg (n+1).
66 #define SPLAY_HEAD(name, type) \
68 struct type *sph_root; /* root of the tree */ \
71 #define SPLAY_INITIALIZER(root) \
74 #define SPLAY_INIT(root) do { \
75 (root)->sph_root = NULL; \
76 } while (/*CONSTCOND*/ 0)
78 #define SPLAY_ENTRY(type) \
80 struct type *spe_left; /* left element */ \
81 struct type *spe_right; /* right element */ \
84 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
85 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
86 #define SPLAY_ROOT(head) (head)->sph_root
87 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
89 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
90 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
91 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
92 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
93 (head)->sph_root = tmp; \
94 } while (/*CONSTCOND*/ 0)
96 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
97 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
98 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
99 (head)->sph_root = tmp; \
100 } while (/*CONSTCOND*/ 0)
102 #define SPLAY_LINKLEFT(head, tmp, field) do { \
103 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
104 tmp = (head)->sph_root; \
105 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
106 } while (/*CONSTCOND*/ 0)
108 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
109 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
110 tmp = (head)->sph_root; \
111 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
112 } while (/*CONSTCOND*/ 0)
114 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
115 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
116 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
117 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
118 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
119 } while (/*CONSTCOND*/ 0)
121 /* Generates prototypes and inline functions */
123 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
124 void name##_SPLAY(struct name *, struct type *); \
125 void name##_SPLAY_MINMAX(struct name *, int); \
126 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
127 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
129 /* Finds the node with the same key as elm */ \
130 static __inline struct type * \
131 name##_SPLAY_FIND(struct name *head, struct type *elm) \
133 if (SPLAY_EMPTY(head)) \
135 name##_SPLAY(head, elm); \
136 if ((cmp)(elm, (head)->sph_root) == 0) \
137 return (head->sph_root); \
141 static __inline struct type * \
142 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
144 name##_SPLAY(head, elm); \
145 if (SPLAY_RIGHT(elm, field) != NULL) { \
146 elm = SPLAY_RIGHT(elm, field); \
147 while (SPLAY_LEFT(elm, field) != NULL) { \
148 elm = SPLAY_LEFT(elm, field); \
155 static __inline struct type * \
156 name##_SPLAY_MIN_MAX(struct name *head, int val) \
158 name##_SPLAY_MINMAX(head, val); \
159 return (SPLAY_ROOT(head)); \
162 /* Main splay operation.
163 * Moves node close to the key of elm to top
165 #define SPLAY_GENERATE(name, type, field, cmp) \
167 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
169 if (SPLAY_EMPTY(head)) { \
170 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
173 name##_SPLAY(head, elm); \
174 __comp = (cmp)(elm, (head)->sph_root); \
176 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
177 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
178 SPLAY_LEFT((head)->sph_root, field) = NULL; \
179 } else if (__comp > 0) { \
180 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
181 SPLAY_LEFT(elm, field) = (head)->sph_root; \
182 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
184 return ((head)->sph_root); \
186 (head)->sph_root = (elm); \
191 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
193 struct type *__tmp; \
194 if (SPLAY_EMPTY(head)) \
196 name##_SPLAY(head, elm); \
197 if ((cmp)(elm, (head)->sph_root) == 0) { \
198 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
199 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
201 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
202 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
203 name##_SPLAY(head, elm); \
204 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
212 name##_SPLAY(struct name *head, struct type *elm) \
214 struct type __node, *__left, *__right, *__tmp; \
217 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
218 __left = __right = &__node; \
220 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
222 __tmp = SPLAY_LEFT((head)->sph_root, field); \
225 if ((cmp)(elm, __tmp) < 0){ \
226 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
227 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
230 SPLAY_LINKLEFT(head, __right, field); \
231 } else if (__comp > 0) { \
232 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
235 if ((cmp)(elm, __tmp) > 0){ \
236 SPLAY_ROTATE_LEFT(head, __tmp, field); \
237 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
240 SPLAY_LINKRIGHT(head, __left, field); \
243 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
246 /* Splay with either the minimum or the maximum element \
247 * Used to find minimum or maximum element in tree. \
249 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
251 struct type __node, *__left, *__right, *__tmp; \
253 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
254 __left = __right = &__node; \
258 __tmp = SPLAY_LEFT((head)->sph_root, field); \
262 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
263 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
266 SPLAY_LINKLEFT(head, __right, field); \
267 } else if (__comp > 0) { \
268 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
272 SPLAY_ROTATE_LEFT(head, __tmp, field); \
273 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
276 SPLAY_LINKRIGHT(head, __left, field); \
279 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
282 #define SPLAY_NEGINF -1
285 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
286 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
287 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
288 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
289 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
290 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
291 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
292 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
294 #define SPLAY_FOREACH(x, name, head) \
295 for ((x) = SPLAY_MIN(name, head); \
297 (x) = SPLAY_NEXT(name, head, x))
299 /* Macros that define a red-black tree */
300 #define RB_HEAD(name, type) \
302 struct type *rbh_root; /* root of the tree */ \
305 #define RB_INITIALIZER(root) \
308 #define RB_INIT(root) do { \
309 (root)->rbh_root = NULL; \
310 } while (/*CONSTCOND*/ 0)
314 #define RB_ENTRY(type) \
316 struct type *rbe_left; /* left element */ \
317 struct type *rbe_right; /* right element */ \
318 struct type *rbe_parent; /* parent element */ \
319 int rbe_color; /* node color */ \
322 #define RB_LEFT(elm, field) (elm)->field.rbe_left
323 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
324 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
325 #define RB_COLOR(elm, field) (elm)->field.rbe_color
326 #define RB_ROOT(head) (head)->rbh_root
327 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
329 #define RB_SET(elm, parent, field) do { \
330 RB_PARENT(elm, field) = parent; \
331 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
332 RB_COLOR(elm, field) = RB_RED; \
333 } while (/*CONSTCOND*/ 0)
335 #define RB_SET_BLACKRED(black, red, field) do { \
336 RB_COLOR(black, field) = RB_BLACK; \
337 RB_COLOR(red, field) = RB_RED; \
338 } while (/*CONSTCOND*/ 0)
341 #define RB_AUGMENT(x) do {} while (0)
344 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
345 (tmp) = RB_RIGHT(elm, field); \
346 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
347 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
350 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
351 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
352 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
354 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
356 (head)->rbh_root = (tmp); \
357 RB_LEFT(tmp, field) = (elm); \
358 RB_PARENT(elm, field) = (tmp); \
360 if ((RB_PARENT(tmp, field))) \
361 RB_AUGMENT(RB_PARENT(tmp, field)); \
362 } while (/*CONSTCOND*/ 0)
364 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
365 (tmp) = RB_LEFT(elm, field); \
366 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
367 RB_PARENT(RB_RIGHT(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_RIGHT(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 /* Generates prototypes and inline functions */
385 #define RB_PROTOTYPE(name, type, field, cmp) \
386 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
387 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
388 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
389 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
390 RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
391 RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
392 RB_PROTOTYPE_INSERT(name, type, attr); \
393 RB_PROTOTYPE_REMOVE(name, type, attr); \
394 RB_PROTOTYPE_FIND(name, type, attr); \
395 RB_PROTOTYPE_NFIND(name, type, attr); \
396 RB_PROTOTYPE_NEXT(name, type, attr); \
397 RB_PROTOTYPE_PREV(name, type, attr); \
398 RB_PROTOTYPE_MINMAX(name, type, attr);
399 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
400 attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
401 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
402 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *)
403 #define RB_PROTOTYPE_REMOVE(name, type, attr) \
404 attr struct type *name##_RB_REMOVE(struct name *, struct type *)
405 #define RB_PROTOTYPE_INSERT(name, type, attr) \
406 attr struct type *name##_RB_INSERT(struct name *, struct type *)
407 #define RB_PROTOTYPE_FIND(name, type, attr) \
408 attr struct type *name##_RB_FIND(struct name *, struct type *)
409 #define RB_PROTOTYPE_NFIND(name, type, attr) \
410 attr struct type *name##_RB_NFIND(struct name *, struct type *)
411 #define RB_PROTOTYPE_NEXT(name, type, attr) \
412 attr struct type *name##_RB_NEXT(struct type *)
413 #define RB_PROTOTYPE_PREV(name, type, attr) \
414 attr struct type *name##_RB_PREV(struct type *)
415 #define RB_PROTOTYPE_MINMAX(name, type, attr) \
416 attr struct type *name##_RB_MINMAX(struct name *, int)
418 /* Main rb operation.
419 * Moves node close to the key of elm to top
421 #define RB_GENERATE(name, type, field, cmp) \
422 RB_GENERATE_INTERNAL(name, type, field, cmp,)
423 #define RB_GENERATE_STATIC(name, type, field, cmp) \
424 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
425 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
426 RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
427 RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
428 RB_GENERATE_INSERT(name, type, field, cmp, attr) \
429 RB_GENERATE_REMOVE(name, type, field, attr) \
430 RB_GENERATE_FIND(name, type, field, cmp, attr) \
431 RB_GENERATE_NFIND(name, type, field, cmp, attr) \
432 RB_GENERATE_NEXT(name, type, field, attr) \
433 RB_GENERATE_PREV(name, type, field, attr) \
434 RB_GENERATE_MINMAX(name, type, field, attr)
436 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
438 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
440 struct type *parent, *gparent, *tmp; \
441 while ((parent = RB_PARENT(elm, field)) != NULL && \
442 RB_COLOR(parent, field) == RB_RED) { \
443 gparent = RB_PARENT(parent, field); \
444 if (parent == RB_LEFT(gparent, field)) { \
445 tmp = RB_RIGHT(gparent, field); \
446 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
447 RB_COLOR(tmp, field) = RB_BLACK; \
448 RB_SET_BLACKRED(parent, gparent, field);\
452 if (RB_RIGHT(parent, field) == elm) { \
453 RB_ROTATE_LEFT(head, parent, tmp, field);\
458 RB_SET_BLACKRED(parent, gparent, field); \
459 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
461 tmp = RB_LEFT(gparent, field); \
462 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
463 RB_COLOR(tmp, field) = RB_BLACK; \
464 RB_SET_BLACKRED(parent, gparent, field);\
468 if (RB_LEFT(parent, field) == elm) { \
469 RB_ROTATE_RIGHT(head, parent, tmp, field);\
474 RB_SET_BLACKRED(parent, gparent, field); \
475 RB_ROTATE_LEFT(head, gparent, tmp, field); \
478 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
481 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
483 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
486 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
487 elm != RB_ROOT(head)) { \
488 if (RB_LEFT(parent, field) == elm) { \
489 tmp = RB_RIGHT(parent, field); \
490 if (RB_COLOR(tmp, field) == RB_RED) { \
491 RB_SET_BLACKRED(tmp, parent, field); \
492 RB_ROTATE_LEFT(head, parent, tmp, field);\
493 tmp = RB_RIGHT(parent, field); \
495 if ((RB_LEFT(tmp, field) == NULL || \
496 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
497 (RB_RIGHT(tmp, field) == NULL || \
498 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
499 RB_COLOR(tmp, field) = RB_RED; \
501 parent = RB_PARENT(elm, field); \
503 if (RB_RIGHT(tmp, field) == NULL || \
504 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
505 struct type *oleft; \
506 if ((oleft = RB_LEFT(tmp, field)) \
508 RB_COLOR(oleft, field) = RB_BLACK;\
509 RB_COLOR(tmp, field) = RB_RED; \
510 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
511 tmp = RB_RIGHT(parent, field); \
513 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
514 RB_COLOR(parent, field) = RB_BLACK; \
515 if (RB_RIGHT(tmp, field)) \
516 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
517 RB_ROTATE_LEFT(head, parent, tmp, field);\
518 elm = RB_ROOT(head); \
522 tmp = RB_LEFT(parent, field); \
523 if (RB_COLOR(tmp, field) == RB_RED) { \
524 RB_SET_BLACKRED(tmp, parent, field); \
525 RB_ROTATE_RIGHT(head, parent, tmp, field);\
526 tmp = RB_LEFT(parent, field); \
528 if ((RB_LEFT(tmp, field) == NULL || \
529 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
530 (RB_RIGHT(tmp, field) == NULL || \
531 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
532 RB_COLOR(tmp, field) = RB_RED; \
534 parent = RB_PARENT(elm, field); \
536 if (RB_LEFT(tmp, field) == NULL || \
537 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
538 struct type *oright; \
539 if ((oright = RB_RIGHT(tmp, field)) \
541 RB_COLOR(oright, field) = RB_BLACK;\
542 RB_COLOR(tmp, field) = RB_RED; \
543 RB_ROTATE_LEFT(head, tmp, oright, field);\
544 tmp = RB_LEFT(parent, field); \
546 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
547 RB_COLOR(parent, field) = RB_BLACK; \
548 if (RB_LEFT(tmp, field)) \
549 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
550 RB_ROTATE_RIGHT(head, parent, tmp, field);\
551 elm = RB_ROOT(head); \
557 RB_COLOR(elm, field) = RB_BLACK; \
560 #define RB_GENERATE_REMOVE(name, type, field, attr) \
562 name##_RB_REMOVE(struct name *head, struct type *elm) \
564 struct type *child, *parent, *old = elm; \
566 if (RB_LEFT(elm, field) == NULL) \
567 child = RB_RIGHT(elm, field); \
568 else if (RB_RIGHT(elm, field) == NULL) \
569 child = RB_LEFT(elm, field); \
572 elm = RB_RIGHT(elm, field); \
573 while ((left = RB_LEFT(elm, field)) != NULL) \
575 child = RB_RIGHT(elm, field); \
576 parent = RB_PARENT(elm, field); \
577 color = RB_COLOR(elm, field); \
579 RB_PARENT(child, field) = parent; \
581 if (RB_LEFT(parent, field) == elm) \
582 RB_LEFT(parent, field) = child; \
584 RB_RIGHT(parent, field) = child; \
585 RB_AUGMENT(parent); \
587 RB_ROOT(head) = child; \
588 if (RB_PARENT(elm, field) == old) \
590 (elm)->field = (old)->field; \
591 if (RB_PARENT(old, field)) { \
592 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
593 RB_LEFT(RB_PARENT(old, field), field) = elm;\
595 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
596 RB_AUGMENT(RB_PARENT(old, field)); \
598 RB_ROOT(head) = elm; \
599 RB_PARENT(RB_LEFT(old, field), field) = elm; \
600 if (RB_RIGHT(old, field)) \
601 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
606 } while ((left = RB_PARENT(left, field)) != NULL); \
610 parent = RB_PARENT(elm, field); \
611 color = RB_COLOR(elm, field); \
613 RB_PARENT(child, field) = parent; \
615 if (RB_LEFT(parent, field) == elm) \
616 RB_LEFT(parent, field) = child; \
618 RB_RIGHT(parent, field) = child; \
619 RB_AUGMENT(parent); \
621 RB_ROOT(head) = child; \
623 if (color == RB_BLACK) \
624 name##_RB_REMOVE_COLOR(head, parent, child); \
628 #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
629 /* Inserts a node into the RB tree */ \
631 name##_RB_INSERT(struct name *head, struct type *elm) \
634 struct type *parent = NULL; \
636 tmp = RB_ROOT(head); \
639 comp = (cmp)(elm, parent); \
641 tmp = RB_LEFT(tmp, field); \
643 tmp = RB_RIGHT(tmp, field); \
647 RB_SET(elm, parent, field); \
648 if (parent != NULL) { \
650 RB_LEFT(parent, field) = elm; \
652 RB_RIGHT(parent, field) = elm; \
653 RB_AUGMENT(parent); \
655 RB_ROOT(head) = elm; \
656 name##_RB_INSERT_COLOR(head, elm); \
660 #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
661 /* Finds the node with the same key as elm */ \
663 name##_RB_FIND(struct name *head, struct type *elm) \
665 struct type *tmp = RB_ROOT(head); \
668 comp = cmp(elm, tmp); \
670 tmp = RB_LEFT(tmp, field); \
672 tmp = RB_RIGHT(tmp, field); \
679 #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
680 /* Finds the first node greater than or equal to the search key */ \
682 name##_RB_NFIND(struct name *head, struct type *elm) \
684 struct type *tmp = RB_ROOT(head); \
685 struct type *res = NULL; \
688 comp = cmp(elm, tmp); \
691 tmp = RB_LEFT(tmp, field); \
694 tmp = RB_RIGHT(tmp, field); \
701 #define RB_GENERATE_NEXT(name, type, field, attr) \
704 name##_RB_NEXT(struct type *elm) \
706 if (RB_RIGHT(elm, field)) { \
707 elm = RB_RIGHT(elm, field); \
708 while (RB_LEFT(elm, field)) \
709 elm = RB_LEFT(elm, field); \
711 if (RB_PARENT(elm, field) && \
712 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
713 elm = RB_PARENT(elm, field); \
715 while (RB_PARENT(elm, field) && \
716 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
717 elm = RB_PARENT(elm, field); \
718 elm = RB_PARENT(elm, field); \
724 #define RB_GENERATE_PREV(name, type, field, attr) \
727 name##_RB_PREV(struct type *elm) \
729 if (RB_LEFT(elm, field)) { \
730 elm = RB_LEFT(elm, field); \
731 while (RB_RIGHT(elm, field)) \
732 elm = RB_RIGHT(elm, field); \
734 if (RB_PARENT(elm, field) && \
735 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
736 elm = RB_PARENT(elm, field); \
738 while (RB_PARENT(elm, field) && \
739 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
740 elm = RB_PARENT(elm, field); \
741 elm = RB_PARENT(elm, field); \
747 #define RB_GENERATE_MINMAX(name, type, field, attr) \
749 name##_RB_MINMAX(struct name *head, int val) \
751 struct type *tmp = RB_ROOT(head); \
752 struct type *parent = NULL; \
756 tmp = RB_LEFT(tmp, field); \
758 tmp = RB_RIGHT(tmp, field); \
766 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
767 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
768 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
769 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
770 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
771 #define RB_PREV(name, x, y) name##_RB_PREV(y)
772 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
773 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
775 #define RB_FOREACH(x, name, head) \
776 for ((x) = RB_MIN(name, head); \
778 (x) = name##_RB_NEXT(x))
780 #define RB_FOREACH_FROM(x, name, y) \
782 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
785 #define RB_FOREACH_SAFE(x, name, head, y) \
786 for ((x) = RB_MIN(name, head); \
787 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
790 #define RB_FOREACH_REVERSE(x, name, head) \
791 for ((x) = RB_MAX(name, head); \
793 (x) = name##_RB_PREV(x))
795 #define RB_FOREACH_REVERSE_FROM(x, name, y) \
797 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
800 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
801 for ((x) = RB_MAX(name, head); \
802 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
809 #endif /* _SYS_TREE_H_ */