1 /* $OpenBSD: tree.h,v 1.30 2020/10/10 18:03:41 otto Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Local modifications:
35 * - __inline -> inline
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; \
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; \
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; \
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); \
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); \
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); \
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))) { \
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; \
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; \
334 #define RB_SET_BLACKRED(black, red, field) do { \
335 RB_COLOR(black, field) = RB_BLACK; \
336 RB_COLOR(red, field) = RB_RED; \
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))) { \
346 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
349 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
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)); \
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))) { \
366 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
369 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
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)); \
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, __attribute__((__unused__)) static)
388 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
389 attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
390 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
391 attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
392 attr struct type *name##_RB_INSERT(struct name *, struct type *); \
393 attr struct type *name##_RB_FIND(struct name *, struct type *); \
394 attr struct type *name##_RB_NFIND(struct name *, struct type *); \
395 attr struct type *name##_RB_NEXT(struct type *); \
396 attr struct type *name##_RB_PREV(struct type *); \
397 attr struct type *name##_RB_MINMAX(struct name *, int); \
400 /* Main rb operation.
401 * Moves node close to the key of elm to top
403 #define RB_GENERATE(name, type, field, cmp) \
404 RB_GENERATE_INTERNAL(name, type, field, cmp,)
405 #define RB_GENERATE_STATIC(name, type, field, cmp) \
406 RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
407 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
409 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
411 struct type *parent, *gparent, *tmp; \
412 while ((parent = RB_PARENT(elm, field)) && \
413 RB_COLOR(parent, field) == RB_RED) { \
414 gparent = RB_PARENT(parent, field); \
415 if (parent == RB_LEFT(gparent, field)) { \
416 tmp = RB_RIGHT(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_RIGHT(parent, field) == elm) { \
424 RB_ROTATE_LEFT(head, parent, tmp, field);\
429 RB_SET_BLACKRED(parent, gparent, field); \
430 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
432 tmp = RB_LEFT(gparent, field); \
433 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
434 RB_COLOR(tmp, field) = RB_BLACK; \
435 RB_SET_BLACKRED(parent, gparent, field);\
439 if (RB_LEFT(parent, field) == elm) { \
440 RB_ROTATE_RIGHT(head, parent, tmp, field);\
445 RB_SET_BLACKRED(parent, gparent, field); \
446 RB_ROTATE_LEFT(head, gparent, tmp, field); \
449 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
453 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
456 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
457 elm != RB_ROOT(head)) { \
458 if (RB_LEFT(parent, field) == elm) { \
459 tmp = RB_RIGHT(parent, field); \
460 if (RB_COLOR(tmp, field) == RB_RED) { \
461 RB_SET_BLACKRED(tmp, parent, field); \
462 RB_ROTATE_LEFT(head, parent, tmp, field);\
463 tmp = RB_RIGHT(parent, field); \
465 if ((RB_LEFT(tmp, field) == NULL || \
466 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
467 (RB_RIGHT(tmp, field) == NULL || \
468 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
469 RB_COLOR(tmp, field) = RB_RED; \
471 parent = RB_PARENT(elm, field); \
473 if (RB_RIGHT(tmp, field) == NULL || \
474 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
475 struct type *oleft; \
476 if ((oleft = RB_LEFT(tmp, field)))\
477 RB_COLOR(oleft, field) = RB_BLACK;\
478 RB_COLOR(tmp, field) = RB_RED; \
479 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
480 tmp = RB_RIGHT(parent, field); \
482 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
483 RB_COLOR(parent, field) = RB_BLACK; \
484 if (RB_RIGHT(tmp, field)) \
485 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
486 RB_ROTATE_LEFT(head, parent, tmp, field);\
487 elm = RB_ROOT(head); \
491 tmp = RB_LEFT(parent, field); \
492 if (RB_COLOR(tmp, field) == RB_RED) { \
493 RB_SET_BLACKRED(tmp, parent, field); \
494 RB_ROTATE_RIGHT(head, parent, tmp, field);\
495 tmp = RB_LEFT(parent, field); \
497 if ((RB_LEFT(tmp, field) == NULL || \
498 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
499 (RB_RIGHT(tmp, field) == NULL || \
500 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
501 RB_COLOR(tmp, field) = RB_RED; \
503 parent = RB_PARENT(elm, field); \
505 if (RB_LEFT(tmp, field) == NULL || \
506 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
507 struct type *oright; \
508 if ((oright = RB_RIGHT(tmp, field)))\
509 RB_COLOR(oright, field) = RB_BLACK;\
510 RB_COLOR(tmp, field) = RB_RED; \
511 RB_ROTATE_LEFT(head, tmp, oright, field);\
512 tmp = RB_LEFT(parent, field); \
514 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
515 RB_COLOR(parent, field) = RB_BLACK; \
516 if (RB_LEFT(tmp, field)) \
517 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
518 RB_ROTATE_RIGHT(head, parent, tmp, field);\
519 elm = RB_ROOT(head); \
525 RB_COLOR(elm, field) = RB_BLACK; \
529 name##_RB_REMOVE(struct name *head, struct type *elm) \
531 struct type *child, *parent, *old = elm; \
533 if (RB_LEFT(elm, field) == NULL) \
534 child = RB_RIGHT(elm, field); \
535 else if (RB_RIGHT(elm, field) == NULL) \
536 child = RB_LEFT(elm, field); \
539 elm = RB_RIGHT(elm, field); \
540 while ((left = RB_LEFT(elm, field))) \
542 child = RB_RIGHT(elm, field); \
543 parent = RB_PARENT(elm, field); \
544 color = RB_COLOR(elm, field); \
546 RB_PARENT(child, field) = parent; \
548 if (RB_LEFT(parent, field) == elm) \
549 RB_LEFT(parent, field) = child; \
551 RB_RIGHT(parent, field) = child; \
552 RB_AUGMENT(parent); \
554 RB_ROOT(head) = child; \
555 if (RB_PARENT(elm, field) == old) \
557 (elm)->field = (old)->field; \
558 if (RB_PARENT(old, field)) { \
559 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
560 RB_LEFT(RB_PARENT(old, field), field) = elm;\
562 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
563 RB_AUGMENT(RB_PARENT(old, field)); \
565 RB_ROOT(head) = elm; \
566 RB_PARENT(RB_LEFT(old, field), field) = elm; \
567 if (RB_RIGHT(old, field)) \
568 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
573 } while ((left = RB_PARENT(left, field))); \
577 parent = RB_PARENT(elm, field); \
578 color = RB_COLOR(elm, field); \
580 RB_PARENT(child, field) = parent; \
582 if (RB_LEFT(parent, field) == elm) \
583 RB_LEFT(parent, field) = child; \
585 RB_RIGHT(parent, field) = child; \
586 RB_AUGMENT(parent); \
588 RB_ROOT(head) = child; \
590 if (color == RB_BLACK) \
591 name##_RB_REMOVE_COLOR(head, parent, child); \
595 /* Inserts a node into the RB tree */ \
597 name##_RB_INSERT(struct name *head, struct type *elm) \
600 struct type *parent = NULL; \
602 tmp = RB_ROOT(head); \
605 comp = (cmp)(elm, parent); \
607 tmp = RB_LEFT(tmp, field); \
609 tmp = RB_RIGHT(tmp, field); \
613 RB_SET(elm, parent, field); \
614 if (parent != NULL) { \
616 RB_LEFT(parent, field) = elm; \
618 RB_RIGHT(parent, field) = elm; \
619 RB_AUGMENT(parent); \
621 RB_ROOT(head) = elm; \
622 name##_RB_INSERT_COLOR(head, elm); \
626 /* Finds the node with the same key as elm */ \
628 name##_RB_FIND(struct name *head, struct type *elm) \
630 struct type *tmp = RB_ROOT(head); \
633 comp = cmp(elm, tmp); \
635 tmp = RB_LEFT(tmp, field); \
637 tmp = RB_RIGHT(tmp, field); \
644 /* Finds the first node greater than or equal to the search key */ \
646 name##_RB_NFIND(struct name *head, struct type *elm) \
648 struct type *tmp = RB_ROOT(head); \
649 struct type *res = NULL; \
652 comp = cmp(elm, tmp); \
655 tmp = RB_LEFT(tmp, field); \
658 tmp = RB_RIGHT(tmp, field); \
667 name##_RB_NEXT(struct type *elm) \
669 if (RB_RIGHT(elm, field)) { \
670 elm = RB_RIGHT(elm, field); \
671 while (RB_LEFT(elm, field)) \
672 elm = RB_LEFT(elm, field); \
674 if (RB_PARENT(elm, field) && \
675 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
676 elm = RB_PARENT(elm, field); \
678 while (RB_PARENT(elm, field) && \
679 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
680 elm = RB_PARENT(elm, field); \
681 elm = RB_PARENT(elm, field); \
689 name##_RB_PREV(struct type *elm) \
691 if (RB_LEFT(elm, field)) { \
692 elm = RB_LEFT(elm, field); \
693 while (RB_RIGHT(elm, field)) \
694 elm = RB_RIGHT(elm, field); \
696 if (RB_PARENT(elm, field) && \
697 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
698 elm = RB_PARENT(elm, field); \
700 while (RB_PARENT(elm, field) && \
701 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
702 elm = RB_PARENT(elm, field); \
703 elm = RB_PARENT(elm, field); \
710 name##_RB_MINMAX(struct name *head, int val) \
712 struct type *tmp = RB_ROOT(head); \
713 struct type *parent = NULL; \
717 tmp = RB_LEFT(tmp, field); \
719 tmp = RB_RIGHT(tmp, field); \
727 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
728 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
729 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
730 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
731 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
732 #define RB_PREV(name, x, y) name##_RB_PREV(y)
733 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
734 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
736 #define RB_FOREACH(x, name, head) \
737 for ((x) = RB_MIN(name, head); \
739 (x) = name##_RB_NEXT(x))
741 #define RB_FOREACH_SAFE(x, name, head, y) \
742 for ((x) = RB_MIN(name, head); \
743 ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
746 #define RB_FOREACH_REVERSE(x, name, head) \
747 for ((x) = RB_MAX(name, head); \
749 (x) = name##_RB_PREV(x))
751 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
752 for ((x) = RB_MAX(name, head); \
753 ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
758 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
760 * Permission to use, copy, modify, and distribute this software for any
761 * purpose with or without fee is hereby granted, provided that the above
762 * copyright notice and this permission notice appear in all copies.
764 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
765 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
766 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
767 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
768 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
769 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
770 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
774 int (*t_compare)(const void *, const void *);
775 void (*t_augment)(void *);
776 unsigned int t_offset; /* offset of rb_entry in type */
780 struct rb_entry *rbt_root;
784 struct rb_entry *rbt_parent;
785 struct rb_entry *rbt_left;
786 struct rb_entry *rbt_right;
787 unsigned int rbt_color;
790 #define RBT_HEAD(_name, _type) \
792 struct rb_tree rbh_root; \
795 #define RBT_ENTRY(_type) struct rb_entry
798 _rb_init(struct rb_tree *rbt)
800 rbt->rbt_root = NULL;
804 _rb_empty(struct rb_tree *rbt)
806 return (rbt->rbt_root == NULL);
809 void *_rb_insert(const struct rb_type *, struct rb_tree *, void *);
810 void *_rb_remove(const struct rb_type *, struct rb_tree *, void *);
811 void *_rb_find(const struct rb_type *, struct rb_tree *, const void *);
812 void *_rb_nfind(const struct rb_type *, struct rb_tree *, const void *);
813 void *_rb_root(const struct rb_type *, struct rb_tree *);
814 void *_rb_min(const struct rb_type *, struct rb_tree *);
815 void *_rb_max(const struct rb_type *, struct rb_tree *);
816 void *_rb_next(const struct rb_type *, void *);
817 void *_rb_prev(const struct rb_type *, void *);
818 void *_rb_left(const struct rb_type *, void *);
819 void *_rb_right(const struct rb_type *, void *);
820 void *_rb_parent(const struct rb_type *, void *);
821 void _rb_set_left(const struct rb_type *, void *, void *);
822 void _rb_set_right(const struct rb_type *, void *, void *);
823 void _rb_set_parent(const struct rb_type *, void *, void *);
824 void _rb_poison(const struct rb_type *, void *, unsigned long);
825 int _rb_check(const struct rb_type *, void *, unsigned long);
827 #define RBT_INITIALIZER(_head) { { NULL } }
829 #define RBT_PROTOTYPE(_name, _type, _field, _cmp) \
830 extern const struct rb_type *const _name##_RBT_TYPE; \
833 _name##_RBT_INIT(struct _name *head) \
835 _rb_init(&head->rbh_root); \
838 static inline struct _type * \
839 _name##_RBT_INSERT(struct _name *head, struct _type *elm) \
841 return _rb_insert(_name##_RBT_TYPE, &head->rbh_root, elm); \
844 static inline struct _type * \
845 _name##_RBT_REMOVE(struct _name *head, struct _type *elm) \
847 return _rb_remove(_name##_RBT_TYPE, &head->rbh_root, elm); \
850 static inline struct _type * \
851 _name##_RBT_FIND(struct _name *head, const struct _type *key) \
853 return _rb_find(_name##_RBT_TYPE, &head->rbh_root, key); \
856 static inline struct _type * \
857 _name##_RBT_NFIND(struct _name *head, const struct _type *key) \
859 return _rb_nfind(_name##_RBT_TYPE, &head->rbh_root, key); \
862 static inline struct _type * \
863 _name##_RBT_ROOT(struct _name *head) \
865 return _rb_root(_name##_RBT_TYPE, &head->rbh_root); \
869 _name##_RBT_EMPTY(struct _name *head) \
871 return _rb_empty(&head->rbh_root); \
874 static inline struct _type * \
875 _name##_RBT_MIN(struct _name *head) \
877 return _rb_min(_name##_RBT_TYPE, &head->rbh_root); \
880 static inline struct _type * \
881 _name##_RBT_MAX(struct _name *head) \
883 return _rb_max(_name##_RBT_TYPE, &head->rbh_root); \
886 static inline struct _type * \
887 _name##_RBT_NEXT(struct _type *elm) \
889 return _rb_next(_name##_RBT_TYPE, elm); \
892 static inline struct _type * \
893 _name##_RBT_PREV(struct _type *elm) \
895 return _rb_prev(_name##_RBT_TYPE, elm); \
898 static inline struct _type * \
899 _name##_RBT_LEFT(struct _type *elm) \
901 return _rb_left(_name##_RBT_TYPE, elm); \
904 static inline struct _type * \
905 _name##_RBT_RIGHT(struct _type *elm) \
907 return _rb_right(_name##_RBT_TYPE, elm); \
910 static inline struct _type * \
911 _name##_RBT_PARENT(struct _type *elm) \
913 return _rb_parent(_name##_RBT_TYPE, elm); \
917 _name##_RBT_SET_LEFT(struct _type *elm, struct _type *left) \
919 _rb_set_left(_name##_RBT_TYPE, elm, left); \
923 _name##_RBT_SET_RIGHT(struct _type *elm, struct _type *right) \
925 _rb_set_right(_name##_RBT_TYPE, elm, right); \
929 _name##_RBT_SET_PARENT(struct _type *elm, struct _type *parent) \
931 _rb_set_parent(_name##_RBT_TYPE, elm, parent); \
935 _name##_RBT_POISON(struct _type *elm, unsigned long poison) \
937 _rb_poison(_name##_RBT_TYPE, elm, poison); \
941 _name##_RBT_CHECK(struct _type *elm, unsigned long poison) \
943 return _rb_check(_name##_RBT_TYPE, elm, poison); \
946 #define RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
948 _name##_RBT_COMPARE(const void *lptr, const void *rptr) \
950 const struct _type *l = lptr, *r = rptr; \
953 static const struct rb_type _name##_RBT_INFO = { \
954 _name##_RBT_COMPARE, \
956 offsetof(struct _type, _field), \
958 const struct rb_type *const _name##_RBT_TYPE = &_name##_RBT_INFO
960 #define RBT_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
962 _name##_RBT_AUGMENT(void *ptr) \
964 struct _type *p = ptr; \
967 RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RBT_AUGMENT)
969 #define RBT_GENERATE(_name, _type, _field, _cmp) \
970 RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
972 #define RBT_INIT(_name, _head) _name##_RBT_INIT(_head)
973 #define RBT_INSERT(_name, _head, _elm) _name##_RBT_INSERT(_head, _elm)
974 #define RBT_REMOVE(_name, _head, _elm) _name##_RBT_REMOVE(_head, _elm)
975 #define RBT_FIND(_name, _head, _key) _name##_RBT_FIND(_head, _key)
976 #define RBT_NFIND(_name, _head, _key) _name##_RBT_NFIND(_head, _key)
977 #define RBT_ROOT(_name, _head) _name##_RBT_ROOT(_head)
978 #define RBT_EMPTY(_name, _head) _name##_RBT_EMPTY(_head)
979 #define RBT_MIN(_name, _head) _name##_RBT_MIN(_head)
980 #define RBT_MAX(_name, _head) _name##_RBT_MAX(_head)
981 #define RBT_NEXT(_name, _elm) _name##_RBT_NEXT(_elm)
982 #define RBT_PREV(_name, _elm) _name##_RBT_PREV(_elm)
983 #define RBT_LEFT(_name, _elm) _name##_RBT_LEFT(_elm)
984 #define RBT_RIGHT(_name, _elm) _name##_RBT_RIGHT(_elm)
985 #define RBT_PARENT(_name, _elm) _name##_RBT_PARENT(_elm)
986 #define RBT_SET_LEFT(_name, _elm, _l) _name##_RBT_SET_LEFT(_elm, _l)
987 #define RBT_SET_RIGHT(_name, _elm, _r) _name##_RBT_SET_RIGHT(_elm, _r)
988 #define RBT_SET_PARENT(_name, _elm, _p) _name##_RBT_SET_PARENT(_elm, _p)
989 #define RBT_POISON(_name, _elm, _p) _name##_RBT_POISON(_elm, _p)
990 #define RBT_CHECK(_name, _elm, _p) _name##_RBT_CHECK(_elm, _p)
992 #define RBT_FOREACH(_e, _name, _head) \
993 for ((_e) = RBT_MIN(_name, (_head)); \
995 (_e) = RBT_NEXT(_name, (_e)))
997 #define RBT_FOREACH_SAFE(_e, _name, _head, _n) \
998 for ((_e) = RBT_MIN(_name, (_head)); \
999 (_e) != NULL && ((_n) = RBT_NEXT(_name, (_e)), 1); \
1002 #define RBT_FOREACH_REVERSE(_e, _name, _head) \
1003 for ((_e) = RBT_MAX(_name, (_head)); \
1005 (_e) = RBT_PREV(_name, (_e)))
1007 #define RBT_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
1008 for ((_e) = RBT_MAX(_name, (_head)); \
1009 (_e) != NULL && ((_n) = RBT_PREV(_name, (_e)), 1); \
1012 #endif /* _SYS_TREE_H_ */