blob: 58309d5aa9d7ad3d8d24973820c21bc4e7988f7f [file] [log] [blame]
jianfeng594baf12012-06-19 19:53:15 +08001/* $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$ */
4
5/*-
6 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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.
28 */
29
30#ifndef _SYS_TREE_H_
31#define _SYS_TREE_H_
32
33#include <sys/cdefs.h>
34
35/*
36 * This file defines data structures for different types of trees:
37 * splay trees and red-black trees.
38 *
39 * A splay tree is a self-organizing data structure. Every operation
40 * on the tree causes a splay to happen. The splay moves the requested
41 * node to the root of the tree and partly rebalances it.
42 *
43 * This has the benefit that request locality causes faster lookups as
44 * the requested nodes move to the top of the tree. On the other hand,
45 * every lookup causes memory writes.
46 *
47 * The Balance Theorem bounds the total access time for m operations
48 * and n inserts on an initially empty tree as O((m + n)lg n). The
49 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
50 *
51 * A red-black tree is a binary search tree with the node color as an
52 * extra attribute. It fulfills a set of conditions:
53 * - every search path from the root to a leaf consists of the
54 * same number of black nodes,
55 * - each red node (except for the root) has a black parent,
56 * - each leaf node is black.
57 *
58 * Every operation on a red-black tree is bounded as O(lg n).
59 * The maximum height of a red-black tree is 2lg (n+1).
60 */
61
62#define SPLAY_HEAD(name, type) \
63struct name { \
64 struct type *sph_root; /* root of the tree */ \
65}
66
67#define SPLAY_INITIALIZER(root) \
68 { NULL }
69
70#define SPLAY_INIT(root) do { \
71 (root)->sph_root = NULL; \
72} while (/*CONSTCOND*/ 0)
73
74#define SPLAY_ENTRY(type) \
75struct { \
76 struct type *spe_left; /* left element */ \
77 struct type *spe_right; /* right element */ \
78}
79
80#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
81#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
82#define SPLAY_ROOT(head) (head)->sph_root
83#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
84
85/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
86#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
87 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
88 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
89 (head)->sph_root = tmp; \
90} while (/*CONSTCOND*/ 0)
91
92#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
93 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
94 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
95 (head)->sph_root = tmp; \
96} while (/*CONSTCOND*/ 0)
97
98#define SPLAY_LINKLEFT(head, tmp, field) do { \
99 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
100 tmp = (head)->sph_root; \
101 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
102} while (/*CONSTCOND*/ 0)
103
104#define SPLAY_LINKRIGHT(head, tmp, field) do { \
105 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
106 tmp = (head)->sph_root; \
107 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
108} while (/*CONSTCOND*/ 0)
109
110#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
111 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
112 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
113 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
114 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
115} while (/*CONSTCOND*/ 0)
116
117/* Generates prototypes and inline functions */
118
119#define SPLAY_PROTOTYPE(name, type, field, cmp) \
120void name##_SPLAY(struct name *, struct type *); \
121void name##_SPLAY_MINMAX(struct name *, int); \
122struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
123struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
124 \
125/* Finds the node with the same key as elm */ \
126static __inline struct type * \
127name##_SPLAY_FIND(struct name *head, struct type *elm) \
128{ \
129 if (SPLAY_EMPTY(head)) \
130 return NULL; \
131 name##_SPLAY(head, elm); \
132 if ((cmp)(elm, (head)->sph_root) == 0) \
133 return head->sph_root; \
134 return NULL; \
135} \
136 \
137static __inline struct type * \
138name##_SPLAY_NEXT(struct name *head, struct type *elm) \
139{ \
140 name##_SPLAY(head, elm); \
141 if (SPLAY_RIGHT(elm, field) != NULL) { \
142 elm = SPLAY_RIGHT(elm, field); \
143 while (SPLAY_LEFT(elm, field) != NULL) { \
144 elm = SPLAY_LEFT(elm, field); \
145 } \
146 } else \
147 elm = NULL; \
148 return elm; \
149} \
150 \
151static __inline struct type * \
152name##_SPLAY_MIN_MAX(struct name *head, int val) \
153{ \
154 name##_SPLAY_MINMAX(head, val); \
155 return SPLAY_ROOT(head); \
156}
157
158/* Main splay operation.
159 * Moves node close to the key of elm to top
160 */
161#define SPLAY_GENERATE(name, type, field, cmp) \
162struct type * \
163name##_SPLAY_INSERT(struct name *head, struct type *elm) \
164{ \
165 if (SPLAY_EMPTY(head)) { \
166 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
167 } else { \
168 int __comp; \
169 name##_SPLAY(head, elm); \
170 __comp = (cmp)(elm, (head)->sph_root); \
171 if(__comp < 0) { \
172 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
173 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
174 SPLAY_LEFT((head)->sph_root, field) = NULL; \
175 } else if (__comp > 0) { \
176 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
177 SPLAY_LEFT(elm, field) = (head)->sph_root; \
178 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
179 } else \
180 return (head)->sph_root; \
181 } \
182 (head)->sph_root = (elm); \
183 return NULL; \
184} \
185 \
186struct type * \
187name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
188{ \
189 struct type *__tmp; \
190 if (SPLAY_EMPTY(head)) \
191 return NULL; \
192 name##_SPLAY(head, elm); \
193 if ((cmp)(elm, (head)->sph_root) == 0) { \
194 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
195 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
196 } else { \
197 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
198 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
199 name##_SPLAY(head, elm); \
200 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
201 } \
202 return elm; \
203 } \
204 return NULL; \
205} \
206 \
207void \
208name##_SPLAY(struct name *head, struct type *elm) \
209{ \
210 struct type __node, *__left, *__right, *__tmp; \
211 int __comp; \
212\
213 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
214 __left = __right = &__node; \
215\
216 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
217 if (__comp < 0) { \
218 __tmp = SPLAY_LEFT((head)->sph_root, field); \
219 if (__tmp == NULL) \
220 break; \
221 if ((cmp)(elm, __tmp) < 0){ \
222 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
223 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
224 break; \
225 } \
226 SPLAY_LINKLEFT(head, __right, field); \
227 } else if (__comp > 0) { \
228 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
229 if (__tmp == NULL) \
230 break; \
231 if ((cmp)(elm, __tmp) > 0){ \
232 SPLAY_ROTATE_LEFT(head, __tmp, field); \
233 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
234 break; \
235 } \
236 SPLAY_LINKRIGHT(head, __left, field); \
237 } \
238 } \
239 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
240} \
241 \
242/* Splay with either the minimum or the maximum element \
243 * Used to find minimum or maximum element in tree. \
244 */ \
245void name##_SPLAY_MINMAX(struct name *head, int __comp) \
246{ \
247 struct type __node, *__left, *__right, *__tmp; \
248\
249 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
250 __left = __right = &__node; \
251\
252 while (1) { \
253 if (__comp < 0) { \
254 __tmp = SPLAY_LEFT((head)->sph_root, field); \
255 if (__tmp == NULL) \
256 break; \
257 if (__comp < 0){ \
258 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
259 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
260 break; \
261 } \
262 SPLAY_LINKLEFT(head, __right, field); \
263 } else if (__comp > 0) { \
264 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
265 if (__tmp == NULL) \
266 break; \
267 if (__comp > 0) { \
268 SPLAY_ROTATE_LEFT(head, __tmp, field); \
269 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
270 break; \
271 } \
272 SPLAY_LINKRIGHT(head, __left, field); \
273 } \
274 } \
275 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
276}
277
278#define SPLAY_NEGINF -1
279#define SPLAY_INF 1
280
281#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
282#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
283#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
284#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
285#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
286 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
287#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
288 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
289
290#define SPLAY_FOREACH(x, name, head) \
291 for ((x) = SPLAY_MIN(name, head); \
292 (x) != NULL; \
293 (x) = SPLAY_NEXT(name, head, x))
294
295/* Macros that define a red-black tree */
296#define RB_HEAD(name, type) \
297struct name { \
298 struct type *rbh_root; /* root of the tree */ \
299}
300
301#define RB_INITIALIZER(root) \
302 { NULL }
303
304#define RB_INIT(root) do { \
305 (root)->rbh_root = NULL; \
306} while (/*CONSTCOND*/ 0)
307
308#define RB_BLACK 0
309#define RB_RED 1
310#define RB_ENTRY(type) \
311struct { \
312 struct type *rbe_left; /* left element */ \
313 struct type *rbe_right; /* right element */ \
314 struct type *rbe_parent; /* parent element */ \
315 int rbe_color; /* node color */ \
316}
317
318#define RB_LEFT(elm, field) (elm)->field.rbe_left
319#define RB_RIGHT(elm, field) (elm)->field.rbe_right
320#define RB_PARENT(elm, field) (elm)->field.rbe_parent
321#define RB_COLOR(elm, field) (elm)->field.rbe_color
322#define RB_ROOT(head) (head)->rbh_root
323#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
324
325#define RB_SET(elm, parent, field) do { \
326 RB_PARENT(elm, field) = parent; \
327 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
328 RB_COLOR(elm, field) = RB_RED; \
329} while (/*CONSTCOND*/ 0)
330
331#define RB_SET_BLACKRED(black, red, field) do { \
332 RB_COLOR(black, field) = RB_BLACK; \
333 RB_COLOR(red, field) = RB_RED; \
334} while (/*CONSTCOND*/ 0)
335
336#ifndef RB_AUGMENT
337#define RB_AUGMENT(x) do {} while (0)
338#endif
339
340#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
341 (tmp) = RB_RIGHT(elm, field); \
342 RB_RIGHT(elm, field) = RB_LEFT(tmp, field); \
343 if (RB_RIGHT(elm, field) != NULL) { \
344 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
345 } \
346 RB_AUGMENT(elm); \
347 RB_PARENT(tmp, field) = RB_PARENT(elm, field); \
348 if (RB_PARENT(tmp, field) != NULL) { \
349 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
350 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
351 else \
352 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
353 } else \
354 (head)->rbh_root = (tmp); \
355 RB_LEFT(tmp, field) = (elm); \
356 RB_PARENT(elm, field) = (tmp); \
357 RB_AUGMENT(tmp); \
358 if ((RB_PARENT(tmp, field))) \
359 RB_AUGMENT(RB_PARENT(tmp, field)); \
360} while (/*CONSTCOND*/ 0)
361
362#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
363 (tmp) = RB_LEFT(elm, field); \
364 RB_LEFT(elm, field) = RB_RIGHT(tmp, field); \
365 if (RB_LEFT(elm, field) != NULL) { \
366 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
367 } \
368 RB_AUGMENT(elm); \
369 RB_PARENT(tmp, field) = RB_PARENT(elm, field); \
370 if (RB_PARENT(tmp, field) != NULL) { \
371 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
372 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
373 else \
374 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
375 } else \
376 (head)->rbh_root = (tmp); \
377 RB_RIGHT(tmp, field) = (elm); \
378 RB_PARENT(elm, field) = (tmp); \
379 RB_AUGMENT(tmp); \
380 if ((RB_PARENT(tmp, field))) \
381 RB_AUGMENT(RB_PARENT(tmp, field)); \
382} while (/*CONSTCOND*/ 0)
383
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) \
390attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
391attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
392attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
393attr struct type *name##_RB_INSERT(struct name *, struct type *); \
394attr struct type *name##_RB_FIND(struct name *, struct type *); \
395attr struct type *name##_RB_NFIND(struct name *, struct type *); \
396attr struct type *name##_RB_NEXT(struct type *); \
397attr struct type *name##_RB_PREV(struct type *); \
398attr struct type *name##_RB_MINMAX(struct name *, int); \
399 \
400
401/* Main rb operation.
402 * Moves node close to the key of elm to top
403 */
404#define RB_GENERATE(name, type, field, cmp) \
405 RB_GENERATE_INTERNAL(name, type, field, cmp,)
406#define RB_GENERATE_STATIC(name, type, field, cmp) \
407 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
408#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
409attr void \
410name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
411{ \
412 struct type *parent, *gparent, *tmp; \
413 while ((parent = RB_PARENT(elm, field)) != NULL && \
414 RB_COLOR(parent, field) == RB_RED) { \
415 gparent = RB_PARENT(parent, field); \
416 if (parent == RB_LEFT(gparent, field)) { \
417 tmp = RB_RIGHT(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; \
423 } \
424 if (RB_RIGHT(parent, field) == elm) { \
425 RB_ROTATE_LEFT(head, parent, tmp, field);\
426 tmp = parent; \
427 parent = elm; \
428 elm = tmp; \
429 } \
430 RB_SET_BLACKRED(parent, gparent, field); \
431 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
432 } else { \
433 tmp = RB_LEFT(gparent, field); \
434 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
435 RB_COLOR(tmp, field) = RB_BLACK; \
436 RB_SET_BLACKRED(parent, gparent, field);\
437 elm = gparent; \
438 continue; \
439 } \
440 if (RB_LEFT(parent, field) == elm) { \
441 RB_ROTATE_RIGHT(head, parent, tmp, field);\
442 tmp = parent; \
443 parent = elm; \
444 elm = tmp; \
445 } \
446 RB_SET_BLACKRED(parent, gparent, field); \
447 RB_ROTATE_LEFT(head, gparent, tmp, field); \
448 } \
449 } \
450 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
451} \
452 \
453attr void \
454name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
455{ \
456 struct type *tmp; \
457 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
458 elm != RB_ROOT(head)) { \
459 if (RB_LEFT(parent, field) == elm) { \
460 tmp = RB_RIGHT(parent, field); \
461 if (RB_COLOR(tmp, field) == RB_RED) { \
462 RB_SET_BLACKRED(tmp, parent, field); \
463 RB_ROTATE_LEFT(head, parent, tmp, field);\
464 tmp = RB_RIGHT(parent, field); \
465 } \
466 if ((RB_LEFT(tmp, field) == NULL || \
467 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
468 (RB_RIGHT(tmp, field) == NULL || \
469 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
470 RB_COLOR(tmp, field) = RB_RED; \
471 elm = parent; \
472 parent = RB_PARENT(elm, field); \
473 } else { \
474 if (RB_RIGHT(tmp, field) == NULL || \
475 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
476 struct type *oleft; \
477 oleft = RB_LEFT(tmp, field); \
478 if (oleft != NULL) \
479 RB_COLOR(oleft, field) = RB_BLACK;\
480 RB_COLOR(tmp, field) = RB_RED; \
481 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
482 tmp = RB_RIGHT(parent, field); \
483 } \
484 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
485 RB_COLOR(parent, field) = RB_BLACK; \
486 if (RB_RIGHT(tmp, field)) \
487 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
488 RB_ROTATE_LEFT(head, parent, tmp, field);\
489 elm = RB_ROOT(head); \
490 break; \
491 } \
492 } else { \
493 tmp = RB_LEFT(parent, field); \
494 if (RB_COLOR(tmp, field) == RB_RED) { \
495 RB_SET_BLACKRED(tmp, parent, field); \
496 RB_ROTATE_RIGHT(head, parent, tmp, field);\
497 tmp = RB_LEFT(parent, field); \
498 } \
499 if ((RB_LEFT(tmp, field) == NULL || \
500 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
501 (RB_RIGHT(tmp, field) == NULL || \
502 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
503 RB_COLOR(tmp, field) = RB_RED; \
504 elm = parent; \
505 parent = RB_PARENT(elm, field); \
506 } else { \
507 if (RB_LEFT(tmp, field) == NULL || \
508 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
509 struct type *oright; \
510 oright = RB_RIGHT(tmp, field); \
511 if (oright != NULL) \
512 RB_COLOR(oright, field) = RB_BLACK;\
513 RB_COLOR(tmp, field) = RB_RED; \
514 RB_ROTATE_LEFT(head, tmp, oright, field);\
515 tmp = RB_LEFT(parent, field); \
516 } \
517 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
518 RB_COLOR(parent, field) = RB_BLACK; \
519 if (RB_LEFT(tmp, field)) \
520 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
521 RB_ROTATE_RIGHT(head, parent, tmp, field);\
522 elm = RB_ROOT(head); \
523 break; \
524 } \
525 } \
526 } \
527 if (elm) \
528 RB_COLOR(elm, field) = RB_BLACK; \
529} \
530 \
531attr struct type * \
532name##_RB_REMOVE(struct name *head, struct type *elm) \
533{ \
534 struct type *child, *parent, *old = elm; \
535 int color; \
536 if (RB_LEFT(elm, field) == NULL) \
537 child = RB_RIGHT(elm, field); \
538 else if (RB_RIGHT(elm, field) == NULL) \
539 child = RB_LEFT(elm, field); \
540 else { \
541 struct type *left; \
542 elm = RB_RIGHT(elm, field); \
543 while ((left = RB_LEFT(elm, field)) != NULL) \
544 elm = left; \
545 child = RB_RIGHT(elm, field); \
546 parent = RB_PARENT(elm, field); \
547 color = RB_COLOR(elm, field); \
548 if (child) \
549 RB_PARENT(child, field) = parent; \
550 if (parent) { \
551 if (RB_LEFT(parent, field) == elm) \
552 RB_LEFT(parent, field) = child; \
553 else \
554 RB_RIGHT(parent, field) = child; \
555 RB_AUGMENT(parent); \
556 } else \
557 RB_ROOT(head) = child; \
558 if (RB_PARENT(elm, field) == old) \
559 parent = elm; \
560 (elm)->field = (old)->field; \
561 if (RB_PARENT(old, field)) { \
562 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
563 RB_LEFT(RB_PARENT(old, field), field) = elm;\
564 else \
565 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
566 RB_AUGMENT(RB_PARENT(old, field)); \
567 } else \
568 RB_ROOT(head) = elm; \
569 RB_PARENT(RB_LEFT(old, field), field) = elm; \
570 if (RB_RIGHT(old, field)) \
571 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
572 if (parent) { \
573 left = parent; \
574 do { \
575 RB_AUGMENT(left); \
576 } while ((left = RB_PARENT(left, field)) != NULL); \
577 } \
578 goto color; \
579 } \
580 parent = RB_PARENT(elm, field); \
581 color = RB_COLOR(elm, field); \
582 if (child) \
583 RB_PARENT(child, field) = parent; \
584 if (parent) { \
585 if (RB_LEFT(parent, field) == elm) \
586 RB_LEFT(parent, field) = child; \
587 else \
588 RB_RIGHT(parent, field) = child; \
589 RB_AUGMENT(parent); \
590 } else \
591 RB_ROOT(head) = child; \
592color: \
593 if (color == RB_BLACK) \
594 name##_RB_REMOVE_COLOR(head, parent, child); \
595 return old; \
596} \
597 \
598/* Inserts a node into the RB tree */ \
599attr struct type * \
600name##_RB_INSERT(struct name *head, struct type *elm) \
601{ \
602 struct type *tmp; \
603 struct type *parent = NULL; \
604 int comp = 0; \
605 tmp = RB_ROOT(head); \
606 while (tmp) { \
607 parent = tmp; \
608 comp = (cmp)(elm, parent); \
609 if (comp < 0) \
610 tmp = RB_LEFT(tmp, field); \
611 else if (comp > 0) \
612 tmp = RB_RIGHT(tmp, field); \
613 else \
614 return tmp; \
615 } \
616 RB_SET(elm, parent, field); \
617 if (parent != NULL) { \
618 if (comp < 0) \
619 RB_LEFT(parent, field) = elm; \
620 else \
621 RB_RIGHT(parent, field) = elm; \
622 RB_AUGMENT(parent); \
623 } else \
624 RB_ROOT(head) = elm; \
625 name##_RB_INSERT_COLOR(head, elm); \
626 return NULL; \
627} \
628 \
629/* Finds the node with the same key as elm */ \
630attr struct type * \
631name##_RB_FIND(struct name *head, struct type *elm) \
632{ \
633 struct type *tmp = RB_ROOT(head); \
634 int comp; \
635 while (tmp) { \
636 comp = cmp(elm, tmp); \
637 if (comp < 0) \
638 tmp = RB_LEFT(tmp, field); \
639 else if (comp > 0) \
640 tmp = RB_RIGHT(tmp, field); \
641 else \
642 return tmp; \
643 } \
644 return NULL; \
645} \
646 \
647/* Finds the first node greater than or equal to the search key */ \
648attr struct type * \
649name##_RB_NFIND(struct name *head, struct type *elm) \
650{ \
651 struct type *tmp = RB_ROOT(head); \
652 struct type *res = NULL; \
653 int comp; \
654 while (tmp) { \
655 comp = cmp(elm, tmp); \
656 if (comp < 0) { \
657 res = tmp; \
658 tmp = RB_LEFT(tmp, field); \
659 } \
660 else if (comp > 0) \
661 tmp = RB_RIGHT(tmp, field); \
662 else \
663 return tmp; \
664 } \
665 return res; \
666} \
667 \
668/* ARGSUSED */ \
669attr struct type * \
670name##_RB_NEXT(struct type *elm) \
671{ \
672 if (RB_RIGHT(elm, field)) { \
673 elm = RB_RIGHT(elm, field); \
674 while (RB_LEFT(elm, field)) \
675 elm = RB_LEFT(elm, field); \
676 } else { \
677 if (RB_PARENT(elm, field) && \
678 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
679 elm = RB_PARENT(elm, field); \
680 else { \
681 while (RB_PARENT(elm, field) && \
682 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
683 elm = RB_PARENT(elm, field); \
684 elm = RB_PARENT(elm, field); \
685 } \
686 } \
687 return elm; \
688} \
689 \
690/* ARGSUSED */ \
691attr struct type * \
692name##_RB_PREV(struct type *elm) \
693{ \
694 if (RB_LEFT(elm, field)) { \
695 elm = RB_LEFT(elm, field); \
696 while (RB_RIGHT(elm, field)) \
697 elm = RB_RIGHT(elm, field); \
698 } else { \
699 if (RB_PARENT(elm, field) && \
700 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
701 elm = RB_PARENT(elm, field); \
702 else { \
703 while (RB_PARENT(elm, field) && \
704 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
705 elm = RB_PARENT(elm, field); \
706 elm = RB_PARENT(elm, field); \
707 } \
708 } \
709 return elm; \
710} \
711 \
712attr struct type * \
713name##_RB_MINMAX(struct name *head, int val) \
714{ \
715 struct type *tmp = RB_ROOT(head); \
716 struct type *parent = NULL; \
717 while (tmp) { \
718 parent = tmp; \
719 if (val < 0) \
720 tmp = RB_LEFT(tmp, field); \
721 else \
722 tmp = RB_RIGHT(tmp, field); \
723 } \
724 return parent; \
725}
726
727#define RB_NEGINF -1
728#define RB_INF 1
729
730#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
731#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
732#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
733#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
734#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
735#define RB_PREV(name, x, y) name##_RB_PREV(y)
736#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
737#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
738
739#define RB_FOREACH(x, name, head) \
740 for ((x) = RB_MIN(name, head); \
741 (x) != NULL; \
742 (x) = name##_RB_NEXT(x))
743
744#define RB_FOREACH_FROM(x, name, y) \
745 for ((x) = (y); \
746 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
747 (x) = (y))
748
749#define RB_FOREACH_SAFE(x, name, head, y) \
750 for ((x) = RB_MIN(name, head); \
751 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
752 (x) = (y))
753
754#define RB_FOREACH_REVERSE(x, name, head) \
755 for ((x) = RB_MAX(name, head); \
756 (x) != NULL; \
757 (x) = name##_RB_PREV(x))
758
759#define RB_FOREACH_REVERSE_FROM(x, name, y) \
760 for ((x) = (y); \
761 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
762 (x) = (y))
763
764#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
765 for ((x) = RB_MAX(name, head); \
766 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
767 (x) = (y))
768
769#endif /* _SYS_TREE_H_ */