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 /* $FreeBSD: head/sys/sys/tree.h 347360 2019-05-08 18:47:00Z trasz $ */
6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef __PERMIT_DEPRECATED_SYS_TREE_H
36 #warning "sys/tree.h is deprecated and will be removed in the future."
39 #include <sys/cdefs.h>
42 * This file defines data structures for different types of trees:
43 * splay trees and red-black trees.
45 * A splay tree is a self-organizing data structure. Every operation
46 * on the tree causes a splay to happen. The splay moves the requested
47 * node to the root of the tree and partly rebalances it.
49 * This has the benefit that request locality causes faster lookups as
50 * the requested nodes move to the top of the tree. On the other hand,
51 * every lookup causes memory writes.
53 * The Balance Theorem bounds the total access time for m operations
54 * and n inserts on an initially empty tree as O((m + n)lg n). The
55 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
57 * A red-black tree is a binary search tree with the node color as an
58 * extra attribute. It fulfills a set of conditions:
59 * - every search path from the root to a leaf consists of the
60 * same number of black nodes,
61 * - each red node (except for the root) has a black parent,
62 * - each leaf node is black.
64 * Every operation on a red-black tree is bounded as O(lg n).
65 * The maximum height of a red-black tree is 2lg (n+1).
68 #define SPLAY_HEAD(name, type) \
70 struct type *sph_root; /* root of the tree */ \
73 #define SPLAY_INITIALIZER(root) \
76 #define SPLAY_INIT(root) do { \
77 (root)->sph_root = NULL; \
78 } while (/*CONSTCOND*/ 0)
80 #define SPLAY_ENTRY(type) \
82 struct type *spe_left; /* left element */ \
83 struct type *spe_right; /* right element */ \
86 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
87 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
88 #define SPLAY_ROOT(head) (head)->sph_root
89 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
91 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
92 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
93 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
94 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
95 (head)->sph_root = tmp; \
96 } while (/*CONSTCOND*/ 0)
98 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
99 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
100 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
101 (head)->sph_root = tmp; \
102 } while (/*CONSTCOND*/ 0)
104 #define SPLAY_LINKLEFT(head, tmp, field) do { \
105 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
106 tmp = (head)->sph_root; \
107 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
108 } while (/*CONSTCOND*/ 0)
110 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
111 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
112 tmp = (head)->sph_root; \
113 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
114 } while (/*CONSTCOND*/ 0)
116 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
117 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
118 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
119 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
120 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
121 } while (/*CONSTCOND*/ 0)
123 /* Generates prototypes and inline functions */
125 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
126 void name##_SPLAY(struct name *, struct type *); \
127 void name##_SPLAY_MINMAX(struct name *, int); \
128 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
129 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
131 /* Finds the node with the same key as elm */ \
132 static __unused __inline struct type * \
133 name##_SPLAY_FIND(struct name *head, struct type *elm) \
135 if (SPLAY_EMPTY(head)) \
137 name##_SPLAY(head, elm); \
138 if ((cmp)(elm, (head)->sph_root) == 0) \
139 return (head->sph_root); \
143 static __unused __inline struct type * \
144 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
146 name##_SPLAY(head, elm); \
147 if (SPLAY_RIGHT(elm, field) != NULL) { \
148 elm = SPLAY_RIGHT(elm, field); \
149 while (SPLAY_LEFT(elm, field) != NULL) { \
150 elm = SPLAY_LEFT(elm, field); \
157 static __unused __inline struct type * \
158 name##_SPLAY_MIN_MAX(struct name *head, int val) \
160 name##_SPLAY_MINMAX(head, val); \
161 return (SPLAY_ROOT(head)); \
164 /* Main splay operation.
165 * Moves node close to the key of elm to top
167 #define SPLAY_GENERATE(name, type, field, cmp) \
169 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
171 if (SPLAY_EMPTY(head)) { \
172 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
175 name##_SPLAY(head, elm); \
176 __comp = (cmp)(elm, (head)->sph_root); \
178 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
179 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
180 SPLAY_LEFT((head)->sph_root, field) = NULL; \
181 } else if (__comp > 0) { \
182 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
183 SPLAY_LEFT(elm, field) = (head)->sph_root; \
184 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
186 return ((head)->sph_root); \
188 (head)->sph_root = (elm); \
193 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
195 struct type *__tmp; \
196 if (SPLAY_EMPTY(head)) \
198 name##_SPLAY(head, elm); \
199 if ((cmp)(elm, (head)->sph_root) == 0) { \
200 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
201 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
203 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
204 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
205 name##_SPLAY(head, elm); \
206 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
214 name##_SPLAY(struct name *head, struct type *elm) \
216 struct type __node, *__left, *__right, *__tmp; \
219 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
220 __left = __right = &__node; \
222 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
224 __tmp = SPLAY_LEFT((head)->sph_root, field); \
227 if ((cmp)(elm, __tmp) < 0){ \
228 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
229 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
232 SPLAY_LINKLEFT(head, __right, field); \
233 } else if (__comp > 0) { \
234 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
237 if ((cmp)(elm, __tmp) > 0){ \
238 SPLAY_ROTATE_LEFT(head, __tmp, field); \
239 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
242 SPLAY_LINKRIGHT(head, __left, field); \
245 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
248 /* Splay with either the minimum or the maximum element \
249 * Used to find minimum or maximum element in tree. \
251 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
253 struct type __node, *__left, *__right, *__tmp; \
255 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
256 __left = __right = &__node; \
260 __tmp = SPLAY_LEFT((head)->sph_root, field); \
264 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
265 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
268 SPLAY_LINKLEFT(head, __right, field); \
269 } else if (__comp > 0) { \
270 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
274 SPLAY_ROTATE_LEFT(head, __tmp, field); \
275 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
278 SPLAY_LINKRIGHT(head, __left, field); \
281 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
284 #define SPLAY_NEGINF -1
287 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
288 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
289 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
290 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
291 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
292 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
293 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
294 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
296 #define SPLAY_FOREACH(x, name, head) \
297 for ((x) = SPLAY_MIN(name, head); \
299 (x) = SPLAY_NEXT(name, head, x))
301 /* Macros that define a red-black tree */
302 #define RB_HEAD(name, type) \
304 struct type *rbh_root; /* root of the tree */ \
307 #define RB_INITIALIZER(root) \
310 #define RB_INIT(root) do { \
311 (root)->rbh_root = NULL; \
312 } while (/*CONSTCOND*/ 0)
316 #define RB_ENTRY(type) \
318 struct type *rbe_left; /* left element */ \
319 struct type *rbe_right; /* right element */ \
320 struct type *rbe_parent; /* parent element */ \
321 int rbe_color; /* node color */ \
324 #define RB_LEFT(elm, field) (elm)->field.rbe_left
325 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
326 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
327 #define RB_COLOR(elm, field) (elm)->field.rbe_color
328 #define RB_ISRED(elm, field) ((elm) != NULL && RB_COLOR(elm, field) == RB_RED)
329 #define RB_ROOT(head) (head)->rbh_root
330 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
332 #define RB_SET_PARENT(dst, src, field) do { \
333 RB_PARENT(dst, field) = src; \
334 } while (/*CONSTCOND*/ 0)
336 #define RB_SET(elm, parent, field) do { \
337 RB_SET_PARENT(elm, parent, field); \
338 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
339 RB_COLOR(elm, field) = RB_RED; \
340 } while (/*CONSTCOND*/ 0)
342 #define RB_SET_BLACKRED(black, red, field) do { \
343 RB_COLOR(black, field) = RB_BLACK; \
344 RB_COLOR(red, field) = RB_RED; \
345 } while (/*CONSTCOND*/ 0)
348 * Something to be invoked in a loop at the root of every modified subtree,
349 * from the bottom up to the root, to update augmented node data.
352 #define RB_AUGMENT(x) break
355 #define RB_SWAP_CHILD(head, out, in, field) do { \
356 if (RB_PARENT(out, field) == NULL) \
357 RB_ROOT(head) = (in); \
358 else if ((out) == RB_LEFT(RB_PARENT(out, field), field)) \
359 RB_LEFT(RB_PARENT(out, field), field) = (in); \
361 RB_RIGHT(RB_PARENT(out, field), field) = (in); \
362 } while (/*CONSTCOND*/ 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_SET_PARENT(RB_RIGHT(elm, field), elm, field); \
369 RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
370 RB_SWAP_CHILD(head, elm, tmp, field); \
371 RB_LEFT(tmp, field) = (elm); \
372 RB_SET_PARENT(elm, tmp, field); \
374 } while (/*CONSTCOND*/ 0)
376 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
377 (tmp) = RB_LEFT(elm, field); \
378 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
379 RB_SET_PARENT(RB_LEFT(elm, field), elm, field); \
381 RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
382 RB_SWAP_CHILD(head, elm, tmp, field); \
383 RB_RIGHT(tmp, field) = (elm); \
384 RB_SET_PARENT(elm, tmp, field); \
386 } while (/*CONSTCOND*/ 0)
389 * The RB_PARENT_ROTATE_LEFT() and RB_PARENT_ROTATE_RIGHT() rotations are
390 * specialized versions of RB_ROTATE_LEFT() and RB_ROTATE_RIGHT() which may be
391 * used if the parent node exists and the direction of the child element is
395 #define RB_PARENT_ROTATE_LEFT(parent, left, tmp, field) do { \
396 (tmp) = RB_RIGHT(left, field); \
397 if ((RB_RIGHT(left, field) = RB_LEFT(tmp, field)) != NULL) { \
398 RB_SET_PARENT(RB_RIGHT(left, field), left, field); \
400 RB_SET_PARENT(tmp, parent, field); \
401 RB_LEFT(parent, field) = (tmp); \
402 RB_LEFT(tmp, field) = (left); \
403 RB_SET_PARENT(left, tmp, field); \
405 } while (/*CONSTCOND*/ 0)
407 #define RB_PARENT_ROTATE_RIGHT(parent, right, tmp, field) do { \
408 (tmp) = RB_LEFT(right, field); \
409 if ((RB_LEFT(right, field) = RB_RIGHT(tmp, field)) != NULL) { \
410 RB_SET_PARENT(RB_LEFT(right, field), right, field); \
412 RB_SET_PARENT(tmp, parent, field); \
413 RB_RIGHT(parent, field) = (tmp); \
414 RB_RIGHT(tmp, field) = (right); \
415 RB_SET_PARENT(right, tmp, field); \
417 } while (/*CONSTCOND*/ 0)
420 * The RB_RED_ROTATE_LEFT() and RB_RED_ROTATE_RIGHT() rotations are specialized
421 * versions of RB_ROTATE_LEFT() and RB_ROTATE_RIGHT() which may be used if we
422 * rotate an element with a red child which has a black sibling. Such a red
423 * node must have at least two child nodes so that the following red-black tree
424 * invariant is fulfilled:
426 * Every path from a given node to any of its descendant NULL nodes goes
427 * through the same number of black nodes.
429 * elm (could be the root)
431 * BLACK RED (left or right child)
436 #define RB_RED_ROTATE_LEFT(head, elm, tmp, field) do { \
437 (tmp) = RB_RIGHT(elm, field); \
438 RB_RIGHT(elm, field) = RB_LEFT(tmp, field); \
439 RB_SET_PARENT(RB_RIGHT(elm, field), elm, field); \
440 RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
441 RB_SWAP_CHILD(head, elm, tmp, field); \
442 RB_LEFT(tmp, field) = (elm); \
443 RB_SET_PARENT(elm, tmp, field); \
445 } while (/*CONSTCOND*/ 0)
447 #define RB_RED_ROTATE_RIGHT(head, elm, tmp, field) do { \
448 (tmp) = RB_LEFT(elm, field); \
449 RB_LEFT(elm, field) = RB_RIGHT(tmp, field); \
450 RB_SET_PARENT(RB_LEFT(elm, field), elm, field); \
451 RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
452 RB_SWAP_CHILD(head, elm, tmp, field); \
453 RB_RIGHT(tmp, field) = (elm); \
454 RB_SET_PARENT(elm, tmp, field); \
456 } while (/*CONSTCOND*/ 0)
458 /* Generates prototypes and inline functions */
459 #define RB_PROTOTYPE(name, type, field, cmp) \
460 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
461 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
462 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
463 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
464 RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
465 RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
466 RB_PROTOTYPE_INSERT(name, type, attr); \
467 RB_PROTOTYPE_REMOVE(name, type, attr); \
468 RB_PROTOTYPE_FIND(name, type, attr); \
469 RB_PROTOTYPE_NFIND(name, type, attr); \
470 RB_PROTOTYPE_NEXT(name, type, attr); \
471 RB_PROTOTYPE_PREV(name, type, attr); \
472 RB_PROTOTYPE_MINMAX(name, type, attr); \
473 RB_PROTOTYPE_REINSERT(name, type, attr);
474 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
475 attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
476 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
477 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *)
478 #define RB_PROTOTYPE_REMOVE(name, type, attr) \
479 attr struct type *name##_RB_REMOVE(struct name *, struct type *)
480 #define RB_PROTOTYPE_INSERT(name, type, attr) \
481 attr struct type *name##_RB_INSERT(struct name *, struct type *)
482 #define RB_PROTOTYPE_FIND(name, type, attr) \
483 attr struct type *name##_RB_FIND(struct name *, struct type *)
484 #define RB_PROTOTYPE_NFIND(name, type, attr) \
485 attr struct type *name##_RB_NFIND(struct name *, struct type *)
486 #define RB_PROTOTYPE_NEXT(name, type, attr) \
487 attr struct type *name##_RB_NEXT(struct type *)
488 #define RB_PROTOTYPE_PREV(name, type, attr) \
489 attr struct type *name##_RB_PREV(struct type *)
490 #define RB_PROTOTYPE_MINMAX(name, type, attr) \
491 attr struct type *name##_RB_MINMAX(struct name *, int)
492 #define RB_PROTOTYPE_REINSERT(name, type, attr) \
493 attr struct type *name##_RB_REINSERT(struct name *, struct type *)
495 /* Main rb operation.
496 * Moves node close to the key of elm to top
498 #define RB_GENERATE(name, type, field, cmp) \
499 RB_GENERATE_INTERNAL(name, type, field, cmp,)
500 #define RB_GENERATE_STATIC(name, type, field, cmp) \
501 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
502 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
503 RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
504 RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
505 RB_GENERATE_INSERT(name, type, field, cmp, attr) \
506 RB_GENERATE_REMOVE(name, type, field, attr) \
507 RB_GENERATE_FIND(name, type, field, cmp, attr) \
508 RB_GENERATE_NFIND(name, type, field, cmp, attr) \
509 RB_GENERATE_NEXT(name, type, field, attr) \
510 RB_GENERATE_PREV(name, type, field, attr) \
511 RB_GENERATE_MINMAX(name, type, field, attr) \
512 RB_GENERATE_REINSERT(name, type, field, cmp, attr)
515 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
517 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
519 struct type *parent, *gparent, *tmp; \
520 while (RB_ISRED((parent = RB_PARENT(elm, field)), field)) { \
521 gparent = RB_PARENT(parent, field); \
522 if (parent == RB_LEFT(gparent, field)) { \
523 tmp = RB_RIGHT(gparent, field); \
524 if (RB_ISRED(tmp, field)) { \
525 RB_COLOR(tmp, field) = RB_BLACK; \
526 RB_SET_BLACKRED(parent, gparent, field);\
530 if (RB_RIGHT(parent, field) == elm) { \
531 RB_PARENT_ROTATE_LEFT(gparent, parent, \
537 RB_SET_BLACKRED(parent, gparent, field); \
538 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
540 tmp = RB_LEFT(gparent, field); \
541 if (RB_ISRED(tmp, field)) { \
542 RB_COLOR(tmp, field) = RB_BLACK; \
543 RB_SET_BLACKRED(parent, gparent, field);\
547 if (RB_LEFT(parent, field) == elm) { \
548 RB_PARENT_ROTATE_RIGHT(gparent, parent, \
554 RB_SET_BLACKRED(parent, gparent, field); \
555 RB_ROTATE_LEFT(head, gparent, tmp, field); \
558 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
561 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
563 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent) \
565 struct type *elm, *tmp; \
568 if (RB_LEFT(parent, field) == elm) { \
569 tmp = RB_RIGHT(parent, field); \
570 if (RB_COLOR(tmp, field) == RB_RED) { \
571 RB_SET_BLACKRED(tmp, parent, field); \
572 RB_RED_ROTATE_LEFT(head, parent, tmp, field); \
573 tmp = RB_RIGHT(parent, field); \
575 if (RB_ISRED(RB_RIGHT(tmp, field), field)) \
576 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
577 else if (RB_ISRED(RB_LEFT(tmp, field), field)) { \
578 struct type *oleft; \
579 RB_PARENT_ROTATE_RIGHT(parent, tmp, \
581 RB_COLOR(oleft, field) = RB_BLACK; \
584 RB_COLOR(tmp, field) = RB_RED; \
586 parent = RB_PARENT(elm, field); \
589 RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
590 RB_COLOR(parent, field) = RB_BLACK; \
591 RB_ROTATE_LEFT(head, parent, tmp, field); \
592 elm = RB_ROOT(head); \
595 tmp = RB_LEFT(parent, field); \
596 if (RB_COLOR(tmp, field) == RB_RED) { \
597 RB_SET_BLACKRED(tmp, parent, field); \
598 RB_RED_ROTATE_RIGHT(head, parent, tmp, field); \
599 tmp = RB_LEFT(parent, field); \
601 if (RB_ISRED(RB_LEFT(tmp, field), field)) \
602 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
603 else if (RB_ISRED(RB_RIGHT(tmp, field), field)) { \
604 struct type *oright; \
605 RB_PARENT_ROTATE_LEFT(parent, tmp, \
607 RB_COLOR(oright, field) = RB_BLACK; \
610 RB_COLOR(tmp, field) = RB_RED; \
612 parent = RB_PARENT(elm, field); \
615 RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
616 RB_COLOR(parent, field) = RB_BLACK; \
617 RB_ROTATE_RIGHT(head, parent, tmp, field); \
618 elm = RB_ROOT(head); \
621 } while (RB_COLOR(elm, field) == RB_BLACK && parent != NULL); \
622 RB_COLOR(elm, field) = RB_BLACK; \
625 #define RB_GENERATE_REMOVE(name, type, field, attr) \
627 name##_RB_REMOVE(struct name *head, struct type *elm) \
629 struct type *child, *old, *parent, *right; \
633 parent = RB_PARENT(elm, field); \
634 right = RB_RIGHT(elm, field); \
635 color = RB_COLOR(elm, field); \
636 if (RB_LEFT(elm, field) == NULL) \
637 elm = child = right; \
638 else if (right == NULL) \
639 elm = child = RB_LEFT(elm, field); \
641 if ((child = RB_LEFT(right, field)) == NULL) { \
642 child = RB_RIGHT(right, field); \
643 RB_RIGHT(old, field) = child; \
644 parent = elm = right; \
648 while ((child = RB_LEFT(elm, field)) != NULL); \
649 child = RB_RIGHT(elm, field); \
650 parent = RB_PARENT(elm, field); \
651 RB_LEFT(parent, field) = child; \
652 RB_SET_PARENT(RB_RIGHT(old, field), elm, field); \
654 RB_SET_PARENT(RB_LEFT(old, field), elm, field); \
655 color = RB_COLOR(elm, field); \
656 elm->field = old->field; \
658 RB_SWAP_CHILD(head, old, elm, field); \
659 if (child != NULL) { \
660 RB_SET_PARENT(child, parent, field); \
661 RB_COLOR(child, field) = RB_BLACK; \
662 } else if (color != RB_RED && parent != NULL) \
663 name##_RB_REMOVE_COLOR(head, parent); \
664 while (parent != NULL) { \
665 RB_AUGMENT(parent); \
666 parent = RB_PARENT(parent, field); \
671 #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
672 /* Inserts a node into the RB tree */ \
674 name##_RB_INSERT(struct name *head, struct type *elm) \
677 struct type *parent = NULL; \
679 tmp = RB_ROOT(head); \
682 comp = (cmp)(elm, parent); \
684 tmp = RB_LEFT(tmp, field); \
686 tmp = RB_RIGHT(tmp, field); \
690 RB_SET(elm, parent, field); \
691 if (parent != NULL) { \
693 RB_LEFT(parent, field) = elm; \
695 RB_RIGHT(parent, field) = elm; \
697 RB_ROOT(head) = elm; \
698 name##_RB_INSERT_COLOR(head, elm); \
699 while (elm != NULL) { \
701 elm = RB_PARENT(elm, field); \
706 #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
707 /* Finds the node with the same key as elm */ \
709 name##_RB_FIND(struct name *head, struct type *elm) \
711 struct type *tmp = RB_ROOT(head); \
714 comp = cmp(elm, tmp); \
716 tmp = RB_LEFT(tmp, field); \
718 tmp = RB_RIGHT(tmp, field); \
725 #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
726 /* Finds the first node greater than or equal to the search key */ \
728 name##_RB_NFIND(struct name *head, struct type *elm) \
730 struct type *tmp = RB_ROOT(head); \
731 struct type *res = NULL; \
734 comp = cmp(elm, tmp); \
737 tmp = RB_LEFT(tmp, field); \
740 tmp = RB_RIGHT(tmp, field); \
747 #define RB_GENERATE_NEXT(name, type, field, attr) \
750 name##_RB_NEXT(struct type *elm) \
752 if (RB_RIGHT(elm, field)) { \
753 elm = RB_RIGHT(elm, field); \
754 while (RB_LEFT(elm, field)) \
755 elm = RB_LEFT(elm, field); \
757 if (RB_PARENT(elm, field) && \
758 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
759 elm = RB_PARENT(elm, field); \
761 while (RB_PARENT(elm, field) && \
762 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
763 elm = RB_PARENT(elm, field); \
764 elm = RB_PARENT(elm, field); \
770 #define RB_GENERATE_PREV(name, type, field, attr) \
773 name##_RB_PREV(struct type *elm) \
775 if (RB_LEFT(elm, field)) { \
776 elm = RB_LEFT(elm, field); \
777 while (RB_RIGHT(elm, field)) \
778 elm = RB_RIGHT(elm, field); \
780 if (RB_PARENT(elm, field) && \
781 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
782 elm = RB_PARENT(elm, field); \
784 while (RB_PARENT(elm, field) && \
785 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
786 elm = RB_PARENT(elm, field); \
787 elm = RB_PARENT(elm, field); \
793 #define RB_GENERATE_MINMAX(name, type, field, attr) \
795 name##_RB_MINMAX(struct name *head, int val) \
797 struct type *tmp = RB_ROOT(head); \
798 struct type *parent = NULL; \
802 tmp = RB_LEFT(tmp, field); \
804 tmp = RB_RIGHT(tmp, field); \
809 #define RB_GENERATE_REINSERT(name, type, field, cmp, attr) \
811 name##_RB_REINSERT(struct name *head, struct type *elm) \
813 struct type *cmpelm; \
814 if (((cmpelm = RB_PREV(name, head, elm)) != NULL && \
815 cmp(cmpelm, elm) >= 0) || \
816 ((cmpelm = RB_NEXT(name, head, elm)) != NULL && \
817 cmp(elm, cmpelm) >= 0)) { \
818 /* XXXLAS: Remove/insert is heavy handed. */ \
819 RB_REMOVE(name, head, elm); \
820 return (RB_INSERT(name, head, elm)); \
828 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
829 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
830 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
831 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
832 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
833 #define RB_PREV(name, x, y) name##_RB_PREV(y)
834 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
835 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
836 #define RB_REINSERT(name, x, y) name##_RB_REINSERT(x, y)
838 #define RB_FOREACH(x, name, head) \
839 for ((x) = RB_MIN(name, head); \
841 (x) = name##_RB_NEXT(x))
843 #define RB_FOREACH_FROM(x, name, y) \
845 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
848 #define RB_FOREACH_SAFE(x, name, head, y) \
849 for ((x) = RB_MIN(name, head); \
850 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
853 #define RB_FOREACH_REVERSE(x, name, head) \
854 for ((x) = RB_MAX(name, head); \
856 (x) = name##_RB_PREV(x))
858 #define RB_FOREACH_REVERSE_FROM(x, name, y) \
860 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
863 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
864 for ((x) = RB_MAX(name, head); \
865 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
868 #endif /* _SYS_TREE_H_ */