kernel debug: priv can be NULL early on
[minix.git] / include / sys / tree.h
blob1243ef1cf4ce6960433cc9716b1587a71a763676
1 /* $NetBSD: tree.h,v 1.16 2008/03/21 13:07:15 ad Exp $ */
2 /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
3 /*
4 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _SYS_TREE_H_
29 #define _SYS_TREE_H_
32 * This file defines data structures for different types of trees:
33 * splay trees and red-black trees.
35 * A splay tree is a self-organizing data structure. Every operation
36 * on the tree causes a splay to happen. The splay moves the requested
37 * node to the root of the tree and partly rebalances it.
39 * This has the benefit that request locality causes faster lookups as
40 * the requested nodes move to the top of the tree. On the other hand,
41 * every lookup causes memory writes.
43 * The Balance Theorem bounds the total access time for m operations
44 * and n inserts on an initially empty tree as O((m + n)lg n). The
45 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
47 * A red-black tree is a binary search tree with the node color as an
48 * extra attribute. It fulfills a set of conditions:
49 * - every search path from the root to a leaf consists of the
50 * same number of black nodes,
51 * - each red node (except for the root) has a black parent,
52 * - each leaf node is black.
54 * Every operation on a red-black tree is bounded as O(lg n).
55 * The maximum height of a red-black tree is 2lg (n+1).
58 #define SPLAY_HEAD(name, type) \
59 struct name { \
60 struct type *sph_root; /* root of the tree */ \
63 #define SPLAY_INITIALIZER(root) \
64 { NULL }
66 #define SPLAY_INIT(root) do { \
67 (root)->sph_root = NULL; \
68 } while (/*CONSTCOND*/ 0)
70 #define SPLAY_ENTRY(type) \
71 struct { \
72 struct type *spe_left; /* left element */ \
73 struct type *spe_right; /* right element */ \
76 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
77 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
78 #define SPLAY_ROOT(head) (head)->sph_root
79 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
81 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
82 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
83 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
84 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
85 (head)->sph_root = tmp; \
86 } while (/*CONSTCOND*/ 0)
88 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
89 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
90 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
91 (head)->sph_root = tmp; \
92 } while (/*CONSTCOND*/ 0)
94 #define SPLAY_LINKLEFT(head, tmp, field) do { \
95 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
96 tmp = (head)->sph_root; \
97 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
98 } while (/*CONSTCOND*/ 0)
100 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
101 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
102 tmp = (head)->sph_root; \
103 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
104 } while (/*CONSTCOND*/ 0)
106 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
107 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
108 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
109 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
110 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
111 } while (/*CONSTCOND*/ 0)
113 /* Generates prototypes and inline functions */
115 void name##_SPLAY_MINMAX(struct name *, int); \
116 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
117 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
119 /* Finds the node with the same key as elm */ \
120 static __inline struct type * \
121 name##_SPLAY_FIND(struct name *head, struct type *elm) \
123 if (SPLAY_EMPTY(head)) \
124 return(NULL); \
125 name##_SPLAY(head, elm); \
126 if ((cmp)(elm, (head)->sph_root) == 0) \
127 return (head->sph_root); \
128 return (NULL); \
131 static __inline struct type * \
132 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
134 name##_SPLAY(head, elm); \
135 if (SPLAY_RIGHT(elm, field) != NULL) { \
136 elm = SPLAY_RIGHT(elm, field); \
137 while (SPLAY_LEFT(elm, field) != NULL) { \
138 elm = SPLAY_LEFT(elm, field); \
140 } else \
141 elm = NULL; \
142 return (elm); \
145 static __inline struct type * \
146 name##_SPLAY_MIN_MAX(struct name *head, int val) \
148 name##_SPLAY_MINMAX(head, val); \
149 return (SPLAY_ROOT(head)); \
152 /* Main splay operation.
153 * Moves node close to the key of elm to top
155 #define SPLAY_GENERATE(name, type, field, cmp) \
156 struct type * \
157 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
159 if (SPLAY_EMPTY(head)) { \
160 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
161 } else { \
162 int __comp; \
163 name##_SPLAY(head, elm); \
164 __comp = (cmp)(elm, (head)->sph_root); \
165 if(__comp < 0) { \
166 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
167 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
168 SPLAY_LEFT((head)->sph_root, field) = NULL; \
169 } else if (__comp > 0) { \
170 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
171 SPLAY_LEFT(elm, field) = (head)->sph_root; \
172 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
173 } else \
174 return ((head)->sph_root); \
176 (head)->sph_root = (elm); \
177 return (NULL); \
180 struct type * \
181 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
183 struct type *__tmp; \
184 if (SPLAY_EMPTY(head)) \
185 return (NULL); \
186 name##_SPLAY(head, elm); \
187 if ((cmp)(elm, (head)->sph_root) == 0) { \
188 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
189 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
190 } else { \
191 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
192 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
193 name##_SPLAY(head, elm); \
194 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
196 return (elm); \
198 return (NULL); \
201 void \
202 name##_SPLAY(struct name *head, struct type *elm) \
204 struct type __node, *__left, *__right, *__tmp; \
205 int __comp; \
207 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
208 __left = __right = &__node; \
210 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
211 if (__comp < 0) { \
212 __tmp = SPLAY_LEFT((head)->sph_root, field); \
213 if (__tmp == NULL) \
214 break; \
215 if ((cmp)(elm, __tmp) < 0){ \
216 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
217 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
218 break; \
220 SPLAY_LINKLEFT(head, __right, field); \
221 } else if (__comp > 0) { \
222 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
223 if (__tmp == NULL) \
224 break; \
225 if ((cmp)(elm, __tmp) > 0){ \
226 SPLAY_ROTATE_LEFT(head, __tmp, field); \
227 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
228 break; \
230 SPLAY_LINKRIGHT(head, __left, field); \
233 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
236 /* Splay with either the minimum or the maximum element \
237 * Used to find minimum or maximum element in tree. \
238 */ \
239 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
241 struct type __node, *__left, *__right, *__tmp; \
243 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
244 __left = __right = &__node; \
246 while (1) { \
247 if (__comp < 0) { \
248 __tmp = SPLAY_LEFT((head)->sph_root, field); \
249 if (__tmp == NULL) \
250 break; \
251 if (__comp < 0){ \
252 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
253 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
254 break; \
256 SPLAY_LINKLEFT(head, __right, field); \
257 } else if (__comp > 0) { \
258 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
259 if (__tmp == NULL) \
260 break; \
261 if (__comp > 0) { \
262 SPLAY_ROTATE_LEFT(head, __tmp, field); \
263 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
264 break; \
266 SPLAY_LINKRIGHT(head, __left, field); \
269 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
272 #define SPLAY_NEGINF -1
273 #define SPLAY_INF 1
275 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
276 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
277 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
278 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
279 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
280 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
281 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
282 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
284 #define SPLAY_FOREACH(x, name, head) \
285 for ((x) = SPLAY_MIN(name, head); \
286 (x) != NULL; \
287 (x) = SPLAY_NEXT(name, head, x))
289 /* Macros that define a red-black tree */
290 #define RB_HEAD(name, type) \
291 struct name { \
292 struct type *rbh_root; /* root of the tree */ \
295 #define RB_INITIALIZER(root) \
296 { NULL }
298 #define RB_INIT(root) do { \
299 (root)->rbh_root = NULL; \
300 } while (/*CONSTCOND*/ 0)
302 #define RB_BLACK 0
303 #define RB_RED 1
304 #define RB_ENTRY(type) \
305 struct { \
306 struct type *rbe_left; /* left element */ \
307 struct type *rbe_right; /* right element */ \
308 struct type *rbe_parent; /* parent element */ \
309 int rbe_color; /* node color */ \
312 #define RB_LEFT(elm, field) (elm)->field.rbe_left
313 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
314 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
315 #define RB_COLOR(elm, field) (elm)->field.rbe_color
316 #define RB_ROOT(head) (head)->rbh_root
317 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
319 #define RB_SET(elm, parent, field) do { \
320 RB_PARENT(elm, field) = parent; \
321 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
322 RB_COLOR(elm, field) = RB_RED; \
323 } while (/*CONSTCOND*/ 0)
325 #define RB_SET_BLACKRED(black, red, field) do { \
326 RB_COLOR(black, field) = RB_BLACK; \
327 RB_COLOR(red, field) = RB_RED; \
328 } while (/*CONSTCOND*/ 0)
330 #ifndef RB_AUGMENT
331 #define RB_AUGMENT(x) (void)(x)
332 #endif
334 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
335 (tmp) = RB_RIGHT(elm, field); \
336 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
337 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
339 RB_AUGMENT(elm); \
340 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
341 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
342 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
343 else \
344 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
345 } else \
346 (head)->rbh_root = (tmp); \
347 RB_LEFT(tmp, field) = (elm); \
348 RB_PARENT(elm, field) = (tmp); \
349 RB_AUGMENT(tmp); \
350 if ((RB_PARENT(tmp, field))) \
351 RB_AUGMENT(RB_PARENT(tmp, field)); \
352 } while (/*CONSTCOND*/ 0)
354 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
355 (tmp) = RB_LEFT(elm, field); \
356 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
357 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
359 RB_AUGMENT(elm); \
360 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
361 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
362 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
363 else \
364 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
365 } else \
366 (head)->rbh_root = (tmp); \
367 RB_RIGHT(tmp, field) = (elm); \
368 RB_PARENT(elm, field) = (tmp); \
369 RB_AUGMENT(tmp); \
370 if ((RB_PARENT(tmp, field))) \
371 RB_AUGMENT(RB_PARENT(tmp, field)); \
372 } while (/*CONSTCOND*/ 0)
374 /* Generates prototypes and inline functions */
375 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
376 attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
377 attr struct type *name##_RB_INSERT(struct name *, struct type *); \
378 attr struct type *name##_RB_FIND(struct name *, struct type *); \
379 attr struct type *name##_RB_NFIND(struct name *, struct type *); \
380 attr struct type *name##_RB_NEXT(struct type *); \
381 attr struct type *name##_RB_PREV(struct type *); \
382 attr struct type *name##_RB_MINMAX(struct name *, int); \
385 /* Main rb operation.
386 * Moves node close to the key of elm to top
388 #define RB_GENERATE(name, type, field, cmp) \
389 RB_GENERATE_INTERNAL(name, type, field, cmp,)
390 #define RB_GENERATE_STATIC(name, type, field, cmp) \
391 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
392 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
393 attr void \
394 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
396 struct type *parent, *gparent, *tmp; \
397 while ((parent = RB_PARENT(elm, field)) != NULL && \
398 RB_COLOR(parent, field) == RB_RED) { \
399 gparent = RB_PARENT(parent, field); \
400 if (parent == RB_LEFT(gparent, field)) { \
401 tmp = RB_RIGHT(gparent, field); \
402 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
403 RB_COLOR(tmp, field) = RB_BLACK; \
404 RB_SET_BLACKRED(parent, gparent, field);\
405 elm = gparent; \
406 continue; \
408 if (RB_RIGHT(parent, field) == elm) { \
409 RB_ROTATE_LEFT(head, parent, tmp, field);\
410 tmp = parent; \
411 parent = elm; \
412 elm = tmp; \
414 RB_SET_BLACKRED(parent, gparent, field); \
415 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
416 } else { \
417 tmp = RB_LEFT(gparent, field); \
418 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
419 RB_COLOR(tmp, field) = RB_BLACK; \
420 RB_SET_BLACKRED(parent, gparent, field);\
421 elm = gparent; \
422 continue; \
424 if (RB_LEFT(parent, field) == elm) { \
425 RB_ROTATE_RIGHT(head, parent, tmp, field);\
426 tmp = parent; \
427 parent = elm; \
428 elm = tmp; \
430 RB_SET_BLACKRED(parent, gparent, field); \
431 RB_ROTATE_LEFT(head, gparent, tmp, field); \
434 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
437 attr void \
438 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
440 struct type *tmp; \
441 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
442 elm != RB_ROOT(head)) { \
443 if (RB_LEFT(parent, field) == elm) { \
444 tmp = RB_RIGHT(parent, field); \
445 if (RB_COLOR(tmp, field) == RB_RED) { \
446 RB_SET_BLACKRED(tmp, parent, field); \
447 RB_ROTATE_LEFT(head, parent, tmp, field);\
448 tmp = RB_RIGHT(parent, field); \
450 if ((RB_LEFT(tmp, field) == NULL || \
451 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
452 (RB_RIGHT(tmp, field) == NULL || \
453 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
454 RB_COLOR(tmp, field) = RB_RED; \
455 elm = parent; \
456 parent = RB_PARENT(elm, field); \
457 } else { \
458 if (RB_RIGHT(tmp, field) == NULL || \
459 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
460 struct type *oleft; \
461 if ((oleft = RB_LEFT(tmp, field)) \
462 != NULL) \
463 RB_COLOR(oleft, field) = RB_BLACK;\
464 RB_COLOR(tmp, field) = RB_RED; \
465 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
466 tmp = RB_RIGHT(parent, field); \
468 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
469 RB_COLOR(parent, field) = RB_BLACK; \
470 if (RB_RIGHT(tmp, field)) \
471 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
472 RB_ROTATE_LEFT(head, parent, tmp, field);\
473 elm = RB_ROOT(head); \
474 break; \
476 } else { \
477 tmp = RB_LEFT(parent, field); \
478 if (RB_COLOR(tmp, field) == RB_RED) { \
479 RB_SET_BLACKRED(tmp, parent, field); \
480 RB_ROTATE_RIGHT(head, parent, tmp, field);\
481 tmp = RB_LEFT(parent, field); \
483 if ((RB_LEFT(tmp, field) == NULL || \
484 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
485 (RB_RIGHT(tmp, field) == NULL || \
486 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
487 RB_COLOR(tmp, field) = RB_RED; \
488 elm = parent; \
489 parent = RB_PARENT(elm, field); \
490 } else { \
491 if (RB_LEFT(tmp, field) == NULL || \
492 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
493 struct type *oright; \
494 if ((oright = RB_RIGHT(tmp, field)) \
495 != NULL) \
496 RB_COLOR(oright, field) = RB_BLACK;\
497 RB_COLOR(tmp, field) = RB_RED; \
498 RB_ROTATE_LEFT(head, tmp, oright, field);\
499 tmp = RB_LEFT(parent, field); \
501 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
502 RB_COLOR(parent, field) = RB_BLACK; \
503 if (RB_LEFT(tmp, field)) \
504 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
505 RB_ROTATE_RIGHT(head, parent, tmp, field);\
506 elm = RB_ROOT(head); \
507 break; \
511 if (elm) \
512 RB_COLOR(elm, field) = RB_BLACK; \
515 attr struct type * \
516 name##_RB_REMOVE(struct name *head, struct type *elm) \
518 struct type *child, *parent, *old = elm; \
519 int color; \
520 if (RB_LEFT(elm, field) == NULL) \
521 child = RB_RIGHT(elm, field); \
522 else if (RB_RIGHT(elm, field) == NULL) \
523 child = RB_LEFT(elm, field); \
524 else { \
525 struct type *left; \
526 elm = RB_RIGHT(elm, field); \
527 while ((left = RB_LEFT(elm, field)) != NULL) \
528 elm = left; \
529 child = RB_RIGHT(elm, field); \
530 parent = RB_PARENT(elm, field); \
531 color = RB_COLOR(elm, field); \
532 if (child) \
533 RB_PARENT(child, field) = parent; \
534 if (parent) { \
535 if (RB_LEFT(parent, field) == elm) \
536 RB_LEFT(parent, field) = child; \
537 else \
538 RB_RIGHT(parent, field) = child; \
539 RB_AUGMENT(parent); \
540 } else \
541 RB_ROOT(head) = child; \
542 if (RB_PARENT(elm, field) == old) \
543 parent = elm; \
544 (elm)->field = (old)->field; \
545 if (RB_PARENT(old, field)) { \
546 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
547 RB_LEFT(RB_PARENT(old, field), field) = elm;\
548 else \
549 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
550 RB_AUGMENT(RB_PARENT(old, field)); \
551 } else \
552 RB_ROOT(head) = elm; \
553 RB_PARENT(RB_LEFT(old, field), field) = elm; \
554 if (RB_RIGHT(old, field)) \
555 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
556 if (parent) { \
557 left = parent; \
558 do { \
559 RB_AUGMENT(left); \
560 } while ((left = RB_PARENT(left, field)) != NULL); \
562 goto color; \
564 parent = RB_PARENT(elm, field); \
565 color = RB_COLOR(elm, field); \
566 if (child) \
567 RB_PARENT(child, field) = parent; \
568 if (parent) { \
569 if (RB_LEFT(parent, field) == elm) \
570 RB_LEFT(parent, field) = child; \
571 else \
572 RB_RIGHT(parent, field) = child; \
573 RB_AUGMENT(parent); \
574 } else \
575 RB_ROOT(head) = child; \
576 color: \
577 if (color == RB_BLACK) \
578 name##_RB_REMOVE_COLOR(head, parent, child); \
579 return (old); \
582 /* Inserts a node into the RB tree */ \
583 attr struct type * \
584 name##_RB_INSERT(struct name *head, struct type *elm) \
586 struct type *tmp; \
587 struct type *parent = NULL; \
588 int comp = 0; \
589 tmp = RB_ROOT(head); \
590 while (tmp) { \
591 parent = tmp; \
592 comp = (cmp)(elm, parent); \
593 if (comp < 0) \
594 tmp = RB_LEFT(tmp, field); \
595 else if (comp > 0) \
596 tmp = RB_RIGHT(tmp, field); \
597 else \
598 return (tmp); \
600 RB_SET(elm, parent, field); \
601 if (parent != NULL) { \
602 if (comp < 0) \
603 RB_LEFT(parent, field) = elm; \
604 else \
605 RB_RIGHT(parent, field) = elm; \
606 RB_AUGMENT(parent); \
607 } else \
608 RB_ROOT(head) = elm; \
609 name##_RB_INSERT_COLOR(head, elm); \
610 return (NULL); \
613 /* Finds the node with the same key as elm */ \
614 attr struct type * \
615 name##_RB_FIND(struct name *head, struct type *elm) \
617 struct type *tmp = RB_ROOT(head); \
618 int comp; \
619 while (tmp) { \
620 comp = cmp(elm, tmp); \
621 if (comp < 0) \
622 tmp = RB_LEFT(tmp, field); \
623 else if (comp > 0) \
624 tmp = RB_RIGHT(tmp, field); \
625 else \
626 return (tmp); \
628 return (NULL); \
631 /* Finds the first node greater than or equal to the search key */ \
632 attr struct type * \
633 name##_RB_NFIND(struct name *head, struct type *elm) \
635 struct type *tmp = RB_ROOT(head); \
636 struct type *res = NULL; \
637 int comp; \
638 while (tmp) { \
639 comp = cmp(elm, tmp); \
640 if (comp < 0) { \
641 res = tmp; \
642 tmp = RB_LEFT(tmp, field); \
644 else if (comp > 0) \
645 tmp = RB_RIGHT(tmp, field); \
646 else \
647 return (tmp); \
649 return (res); \
652 /* ARGSUSED */ \
653 attr struct type * \
654 name##_RB_NEXT(struct type *elm) \
656 if (RB_RIGHT(elm, field)) { \
657 elm = RB_RIGHT(elm, field); \
658 while (RB_LEFT(elm, field)) \
659 elm = RB_LEFT(elm, field); \
660 } else { \
661 if (RB_PARENT(elm, field) && \
662 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
663 elm = RB_PARENT(elm, field); \
664 else { \
665 while (RB_PARENT(elm, field) && \
666 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
667 elm = RB_PARENT(elm, field); \
668 elm = RB_PARENT(elm, field); \
671 return (elm); \
674 /* ARGSUSED */ \
675 attr struct type * \
676 name##_RB_PREV(struct type *elm) \
678 if (RB_LEFT(elm, field)) { \
679 elm = RB_LEFT(elm, field); \
680 while (RB_RIGHT(elm, field)) \
681 elm = RB_RIGHT(elm, field); \
682 } else { \
683 if (RB_PARENT(elm, field) && \
684 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
685 elm = RB_PARENT(elm, field); \
686 else { \
687 while (RB_PARENT(elm, field) && \
688 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
689 elm = RB_PARENT(elm, field); \
690 elm = RB_PARENT(elm, field); \
693 return (elm); \
696 attr struct type * \
697 name##_RB_MINMAX(struct name *head, int val) \
699 struct type *tmp = RB_ROOT(head); \
700 struct type *parent = NULL; \
701 while (tmp) { \
702 parent = tmp; \
703 if (val < 0) \
704 tmp = RB_LEFT(tmp, field); \
705 else \
706 tmp = RB_RIGHT(tmp, field); \
708 return (parent); \
711 #define RB_NEGINF -1
712 #define RB_INF 1
714 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
715 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
716 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
717 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
718 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
719 #define RB_PREV(name, x, y) name##_RB_PREV(y)
720 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
721 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
723 #define RB_FOREACH(x, name, head) \
724 for ((x) = RB_MIN(name, head); \
725 (x) != NULL; \
726 (x) = name##_RB_NEXT(x))
728 #define RB_FOREACH_REVERSE(x, name, head) \
729 for ((x) = RB_MAX(name, head); \
730 (x) != NULL; \
731 (x) = name##_RB_PREV(x))
733 #endif /* _SYS_TREE_H_ */