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