1 /*	$NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $	*/
2 /*	$OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $	*/
3 /* $FreeBSD: head/sys/sys/tree.h 347360 2019-05-08 18:47:00Z trasz $ */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef	_SYS_TREE_H_
33 #define	_SYS_TREE_H_
34 
35 #include <sys/cdefs.h>
36 
37 /*
38  * This file defines data structures for different types of trees:
39  * splay trees and red-black trees.
40  *
41  * A splay tree is a self-organizing data structure.  Every operation
42  * on the tree causes a splay to happen.  The splay moves the requested
43  * node to the root of the tree and partly rebalances it.
44  *
45  * This has the benefit that request locality causes faster lookups as
46  * the requested nodes move to the top of the tree.  On the other hand,
47  * every lookup causes memory writes.
48  *
49  * The Balance Theorem bounds the total access time for m operations
50  * and n inserts on an initially empty tree as O((m + n)lg n).  The
51  * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
52  *
53  * A red-black tree is a binary search tree with the node color as an
54  * extra attribute.  It fulfills a set of conditions:
55  *	- every search path from the root to a leaf consists of the
56  *	  same number of black nodes,
57  *	- each red node (except for the root) has a black parent,
58  *	- each leaf node is black.
59  *
60  * Every operation on a red-black tree is bounded as O(lg n).
61  * The maximum height of a red-black tree is 2lg (n+1).
62  */
63 
64 #define SPLAY_HEAD(name, type)						\
65 struct name {								\
66 	struct type *sph_root; /* root of the tree */			\
67 }
68 
69 #define SPLAY_INITIALIZER(root)						\
70 	{ NULL }
71 
72 #define SPLAY_INIT(root) do {						\
73 	(root)->sph_root = NULL;					\
74 } while (/*CONSTCOND*/ 0)
75 
76 #define SPLAY_ENTRY(type)						\
77 struct {								\
78 	struct type *spe_left; /* left element */			\
79 	struct type *spe_right; /* right element */			\
80 }
81 
82 #define SPLAY_LEFT(elm, field)		(elm)->field.spe_left
83 #define SPLAY_RIGHT(elm, field)		(elm)->field.spe_right
84 #define SPLAY_ROOT(head)		(head)->sph_root
85 #define SPLAY_EMPTY(head)		(SPLAY_ROOT(head) == NULL)
86 
87 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
88 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do {			\
89 	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field);	\
90 	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
91 	(head)->sph_root = tmp;						\
92 } while (/*CONSTCOND*/ 0)
93 
94 #define SPLAY_ROTATE_LEFT(head, tmp, field) do {			\
95 	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field);	\
96 	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
97 	(head)->sph_root = tmp;						\
98 } while (/*CONSTCOND*/ 0)
99 
100 #define SPLAY_LINKLEFT(head, tmp, field) do {				\
101 	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
102 	tmp = (head)->sph_root;						\
103 	(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);		\
104 } while (/*CONSTCOND*/ 0)
105 
106 #define SPLAY_LINKRIGHT(head, tmp, field) do {				\
107 	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
108 	tmp = (head)->sph_root;						\
109 	(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);	\
110 } while (/*CONSTCOND*/ 0)
111 
112 #define SPLAY_ASSEMBLE(head, node, left, right, field) do {		\
113 	SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field);	\
114 	SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
115 	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field);	\
116 	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field);	\
117 } while (/*CONSTCOND*/ 0)
118 
119 /* Generates prototypes and inline functions */
120 
121 #define SPLAY_PROTOTYPE(name, type, field, cmp)				\
122 void name##_SPLAY(struct name *, struct type *);			\
123 void name##_SPLAY_MINMAX(struct name *, int);				\
124 struct type *name##_SPLAY_INSERT(struct name *, struct type *);		\
125 struct type *name##_SPLAY_REMOVE(struct name *, struct type *);		\
126 									\
127 /* Finds the node with the same key as elm */				\
128 static __unused __inline struct type *					\
129 name##_SPLAY_FIND(struct name *head, struct type *elm)			\
130 {									\
131 	if (SPLAY_EMPTY(head))						\
132 		return(NULL);						\
133 	name##_SPLAY(head, elm);					\
134 	if ((cmp)(elm, (head)->sph_root) == 0)				\
135 		return (head->sph_root);				\
136 	return (NULL);							\
137 }									\
138 									\
139 static __unused __inline struct type *					\
140 name##_SPLAY_NEXT(struct name *head, struct type *elm)			\
141 {									\
142 	name##_SPLAY(head, elm);					\
143 	if (SPLAY_RIGHT(elm, field) != NULL) {				\
144 		elm = SPLAY_RIGHT(elm, field);				\
145 		while (SPLAY_LEFT(elm, field) != NULL) {		\
146 			elm = SPLAY_LEFT(elm, field);			\
147 		}							\
148 	} else								\
149 		elm = NULL;						\
150 	return (elm);							\
151 }									\
152 									\
153 static __unused __inline struct type *					\
154 name##_SPLAY_MIN_MAX(struct name *head, int val)			\
155 {									\
156 	name##_SPLAY_MINMAX(head, val);					\
157         return (SPLAY_ROOT(head));					\
158 }
159 
160 /* Main splay operation.
161  * Moves node close to the key of elm to top
162  */
163 #define SPLAY_GENERATE(name, type, field, cmp)				\
164 struct type *								\
165 name##_SPLAY_INSERT(struct name *head, struct type *elm)		\
166 {									\
167     if (SPLAY_EMPTY(head)) {						\
168 	    SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL;	\
169     } else {								\
170 	    int __comp;							\
171 	    name##_SPLAY(head, elm);					\
172 	    __comp = (cmp)(elm, (head)->sph_root);			\
173 	    if(__comp < 0) {						\
174 		    SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
175 		    SPLAY_RIGHT(elm, field) = (head)->sph_root;		\
176 		    SPLAY_LEFT((head)->sph_root, field) = NULL;		\
177 	    } else if (__comp > 0) {					\
178 		    SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
179 		    SPLAY_LEFT(elm, field) = (head)->sph_root;		\
180 		    SPLAY_RIGHT((head)->sph_root, field) = NULL;	\
181 	    } else							\
182 		    return ((head)->sph_root);				\
183     }									\
184     (head)->sph_root = (elm);						\
185     return (NULL);							\
186 }									\
187 									\
188 struct type *								\
189 name##_SPLAY_REMOVE(struct name *head, struct type *elm)		\
190 {									\
191 	struct type *__tmp;						\
192 	if (SPLAY_EMPTY(head))						\
193 		return (NULL);						\
194 	name##_SPLAY(head, elm);					\
195 	if ((cmp)(elm, (head)->sph_root) == 0) {			\
196 		if (SPLAY_LEFT((head)->sph_root, field) == NULL) {	\
197 			(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
198 		} else {						\
199 			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
200 			(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
201 			name##_SPLAY(head, elm);			\
202 			SPLAY_RIGHT((head)->sph_root, field) = __tmp;	\
203 		}							\
204 		return (elm);						\
205 	}								\
206 	return (NULL);							\
207 }									\
208 									\
209 void									\
210 name##_SPLAY(struct name *head, struct type *elm)			\
211 {									\
212 	struct type __node, *__left, *__right, *__tmp;			\
213 	int __comp;							\
214 \
215 	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
216 	__left = __right = &__node;					\
217 \
218 	while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) {		\
219 		if (__comp < 0) {					\
220 			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
221 			if (__tmp == NULL)				\
222 				break;					\
223 			if ((cmp)(elm, __tmp) < 0){			\
224 				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
225 				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
226 					break;				\
227 			}						\
228 			SPLAY_LINKLEFT(head, __right, field);		\
229 		} else if (__comp > 0) {				\
230 			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
231 			if (__tmp == NULL)				\
232 				break;					\
233 			if ((cmp)(elm, __tmp) > 0){			\
234 				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
235 				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
236 					break;				\
237 			}						\
238 			SPLAY_LINKRIGHT(head, __left, field);		\
239 		}							\
240 	}								\
241 	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
242 }									\
243 									\
244 /* Splay with either the minimum or the maximum element			\
245  * Used to find minimum or maximum element in tree.			\
246  */									\
247 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
248 {									\
249 	struct type __node, *__left, *__right, *__tmp;			\
250 \
251 	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
252 	__left = __right = &__node;					\
253 \
254 	while (1) {							\
255 		if (__comp < 0) {					\
256 			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
257 			if (__tmp == NULL)				\
258 				break;					\
259 			if (__comp < 0){				\
260 				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
261 				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
262 					break;				\
263 			}						\
264 			SPLAY_LINKLEFT(head, __right, field);		\
265 		} else if (__comp > 0) {				\
266 			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
267 			if (__tmp == NULL)				\
268 				break;					\
269 			if (__comp > 0) {				\
270 				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
271 				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
272 					break;				\
273 			}						\
274 			SPLAY_LINKRIGHT(head, __left, field);		\
275 		}							\
276 	}								\
277 	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
278 }
279 
280 #define SPLAY_NEGINF	-1
281 #define SPLAY_INF	1
282 
283 #define SPLAY_INSERT(name, x, y)	name##_SPLAY_INSERT(x, y)
284 #define SPLAY_REMOVE(name, x, y)	name##_SPLAY_REMOVE(x, y)
285 #define SPLAY_FIND(name, x, y)		name##_SPLAY_FIND(x, y)
286 #define SPLAY_NEXT(name, x, y)		name##_SPLAY_NEXT(x, y)
287 #define SPLAY_MIN(name, x)		(SPLAY_EMPTY(x) ? NULL	\
288 					: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
289 #define SPLAY_MAX(name, x)		(SPLAY_EMPTY(x) ? NULL	\
290 					: name##_SPLAY_MIN_MAX(x, SPLAY_INF))
291 
292 #define SPLAY_FOREACH(x, name, head)					\
293 	for ((x) = SPLAY_MIN(name, head);				\
294 	     (x) != NULL;						\
295 	     (x) = SPLAY_NEXT(name, head, x))
296 
297 /* Macros that define a red-black tree */
298 #define RB_HEAD(name, type)						\
299 struct name {								\
300 	struct type *rbh_root; /* root of the tree */			\
301 }
302 
303 #define RB_INITIALIZER(root)						\
304 	{ NULL }
305 
306 #define RB_INIT(root) do {						\
307 	(root)->rbh_root = NULL;					\
308 } while (/*CONSTCOND*/ 0)
309 
310 #define RB_BLACK	0
311 #define RB_RED		1
312 #define RB_ENTRY(type)							\
313 struct {								\
314 	struct type *rbe_left;		/* left element */		\
315 	struct type *rbe_right;		/* right element */		\
316 	struct type *rbe_parent;	/* parent element */		\
317 	int rbe_color;			/* node color */		\
318 }
319 
320 #define RB_LEFT(elm, field)		(elm)->field.rbe_left
321 #define RB_RIGHT(elm, field)		(elm)->field.rbe_right
322 #define RB_PARENT(elm, field)		(elm)->field.rbe_parent
323 #define RB_COLOR(elm, field)		(elm)->field.rbe_color
324 #define RB_ISRED(elm, field)		((elm) != NULL && RB_COLOR(elm, field) == RB_RED)
325 #define RB_ROOT(head)			(head)->rbh_root
326 #define RB_EMPTY(head)			(RB_ROOT(head) == NULL)
327 
328 #define RB_SET_PARENT(dst, src, field) do {				\
329 	RB_PARENT(dst, field) = src;					\
330 } while (/*CONSTCOND*/ 0)
331 
332 #define RB_SET(elm, parent, field) do {					\
333 	RB_SET_PARENT(elm, parent, field);				\
334 	RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL;		\
335 	RB_COLOR(elm, field) = RB_RED;					\
336 } while (/*CONSTCOND*/ 0)
337 
338 #define RB_SET_BLACKRED(black, red, field) do {				\
339 	RB_COLOR(black, field) = RB_BLACK;				\
340 	RB_COLOR(red, field) = RB_RED;					\
341 } while (/*CONSTCOND*/ 0)
342 
343 /*
344  * Something to be invoked in a loop at the root of every modified subtree,
345  * from the bottom up to the root, to update augmented node data.
346  */
347 #ifndef RB_AUGMENT
348 #define RB_AUGMENT(x)	break
349 #endif
350 
351 #define RB_SWAP_CHILD(head, out, in, field) do {			\
352 	if (RB_PARENT(out, field) == NULL)				\
353 		RB_ROOT(head) = (in);					\
354 	else if ((out) == RB_LEFT(RB_PARENT(out, field), field))	\
355 		RB_LEFT(RB_PARENT(out, field), field) = (in);		\
356 	else								\
357 		RB_RIGHT(RB_PARENT(out, field), field) = (in);		\
358 } while (/*CONSTCOND*/ 0)
359 
360 #define RB_ROTATE_LEFT(head, elm, tmp, field) do {			\
361 	(tmp) = RB_RIGHT(elm, field);					\
362 	if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) {	\
363 		RB_SET_PARENT(RB_RIGHT(elm, field), elm, field);	\
364 	}								\
365 	RB_SET_PARENT(tmp, RB_PARENT(elm, field), field);		\
366 	RB_SWAP_CHILD(head, elm, tmp, field);				\
367 	RB_LEFT(tmp, field) = (elm);					\
368 	RB_SET_PARENT(elm, tmp, field);					\
369 	RB_AUGMENT(elm);						\
370 } while (/*CONSTCOND*/ 0)
371 
372 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do {			\
373 	(tmp) = RB_LEFT(elm, field);					\
374 	if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) {	\
375 		RB_SET_PARENT(RB_LEFT(elm, field), elm, field);		\
376 	}								\
377 	RB_SET_PARENT(tmp, RB_PARENT(elm, field), field);		\
378 	RB_SWAP_CHILD(head, elm, tmp, field);				\
379 	RB_RIGHT(tmp, field) = (elm);					\
380 	RB_SET_PARENT(elm, tmp, field);					\
381 	RB_AUGMENT(elm);						\
382 } while (/*CONSTCOND*/ 0)
383 
384 /*
385  * The RB_PARENT_ROTATE_LEFT() and RB_PARENT_ROTATE_RIGHT() rotations are
386  * specialized versions of RB_ROTATE_LEFT() and RB_ROTATE_RIGHT() which may be
387  * used if the parent node exists and the direction of the child element is
388  * known.
389  */
390 
391 #define RB_PARENT_ROTATE_LEFT(parent, left, tmp, field) do {		\
392 	(tmp) = RB_RIGHT(left, field);					\
393 	if ((RB_RIGHT(left, field) = RB_LEFT(tmp, field)) != NULL) {	\
394 		RB_SET_PARENT(RB_RIGHT(left, field), left, field);	\
395 	}								\
396 	RB_SET_PARENT(tmp, parent, field);				\
397 	RB_LEFT(parent, field) = (tmp);					\
398 	RB_LEFT(tmp, field) = (left);					\
399 	RB_SET_PARENT(left, tmp, field);				\
400 	RB_AUGMENT(left);						\
401 } while (/*CONSTCOND*/ 0)
402 
403 #define RB_PARENT_ROTATE_RIGHT(parent, right, tmp, field) do {		\
404 	(tmp) = RB_LEFT(right, field);					\
405 	if ((RB_LEFT(right, field) = RB_RIGHT(tmp, field)) != NULL) {	\
406 		RB_SET_PARENT(RB_LEFT(right, field), right, field);	\
407 	}								\
408 	RB_SET_PARENT(tmp, parent, field);				\
409 	RB_RIGHT(parent, field) = (tmp);				\
410 	RB_RIGHT(tmp, field) = (right);					\
411 	RB_SET_PARENT(right, tmp, field);				\
412 	RB_AUGMENT(right);						\
413 } while (/*CONSTCOND*/ 0)
414 
415 /*
416  * The RB_RED_ROTATE_LEFT() and RB_RED_ROTATE_RIGHT() rotations are specialized
417  * versions of RB_ROTATE_LEFT() and RB_ROTATE_RIGHT() which may be used if we
418  * rotate an element with a red child which has a black sibling.  Such a red
419  * node must have at least two child nodes so that the following red-black tree
420  * invariant is fulfilled:
421  *
422  *  Every path from a given node to any of its descendant NULL nodes goes
423  *  through the same number of black nodes.
424  *
425  *  elm (could be the root)
426  *    /      \
427  * BLACK   RED (left or right child)
428  *          /   \
429  *       BLACK  BLACK
430  */
431 
432 #define RB_RED_ROTATE_LEFT(head, elm, tmp, field) do {			\
433 	(tmp) = RB_RIGHT(elm, field);					\
434 	RB_RIGHT(elm, field) = RB_LEFT(tmp, field);			\
435 	RB_SET_PARENT(RB_RIGHT(elm, field), elm, field);		\
436 	RB_SET_PARENT(tmp, RB_PARENT(elm, field), field);		\
437 	RB_SWAP_CHILD(head, elm, tmp, field);				\
438 	RB_LEFT(tmp, field) = (elm);					\
439 	RB_SET_PARENT(elm, tmp, field);					\
440 	RB_AUGMENT(elm);						\
441 } while (/*CONSTCOND*/ 0)
442 
443 #define RB_RED_ROTATE_RIGHT(head, elm, tmp, field) do {			\
444 	(tmp) = RB_LEFT(elm, field);					\
445 	RB_LEFT(elm, field) = RB_RIGHT(tmp, field);			\
446 	RB_SET_PARENT(RB_LEFT(elm, field), elm, field);			\
447 	RB_SET_PARENT(tmp, RB_PARENT(elm, field), field);		\
448 	RB_SWAP_CHILD(head, elm, tmp, field);				\
449 	RB_RIGHT(tmp, field) = (elm);					\
450 	RB_SET_PARENT(elm, tmp, field);					\
451 	RB_AUGMENT(elm);						\
452 } while (/*CONSTCOND*/ 0)
453 
454 /* Generates prototypes and inline functions */
455 #define	RB_PROTOTYPE(name, type, field, cmp)				\
456 	RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
457 #define	RB_PROTOTYPE_STATIC(name, type, field, cmp)			\
458 	RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
459 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr)		\
460 	RB_PROTOTYPE_INSERT_COLOR(name, type, attr);			\
461 	RB_PROTOTYPE_REMOVE_COLOR(name, type, attr);			\
462 	RB_PROTOTYPE_INSERT(name, type, attr);				\
463 	RB_PROTOTYPE_REMOVE(name, type, attr);				\
464 	RB_PROTOTYPE_FIND(name, type, attr);				\
465 	RB_PROTOTYPE_NFIND(name, type, attr);				\
466 	RB_PROTOTYPE_NEXT(name, type, attr);				\
467 	RB_PROTOTYPE_PREV(name, type, attr);				\
468 	RB_PROTOTYPE_MINMAX(name, type, attr);				\
469 	RB_PROTOTYPE_REINSERT(name, type, attr);
470 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr)			\
471 	attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
472 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr)			\
473 	attr void name##_RB_REMOVE_COLOR(struct name *, struct type *)
474 #define RB_PROTOTYPE_REMOVE(name, type, attr)				\
475 	attr struct type *name##_RB_REMOVE(struct name *, struct type *)
476 #define RB_PROTOTYPE_INSERT(name, type, attr)				\
477 	attr struct type *name##_RB_INSERT(struct name *, struct type *)
478 #define RB_PROTOTYPE_FIND(name, type, attr)				\
479 	attr struct type *name##_RB_FIND(struct name *, struct type *)
480 #define RB_PROTOTYPE_NFIND(name, type, attr)				\
481 	attr struct type *name##_RB_NFIND(struct name *, struct type *)
482 #define RB_PROTOTYPE_NEXT(name, type, attr)				\
483 	attr struct type *name##_RB_NEXT(struct type *)
484 #define RB_PROTOTYPE_PREV(name, type, attr)				\
485 	attr struct type *name##_RB_PREV(struct type *)
486 #define RB_PROTOTYPE_MINMAX(name, type, attr)				\
487 	attr struct type *name##_RB_MINMAX(struct name *, int)
488 #define RB_PROTOTYPE_REINSERT(name, type, attr)			\
489 	attr struct type *name##_RB_REINSERT(struct name *, struct type *)
490 
491 /* Main rb operation.
492  * Moves node close to the key of elm to top
493  */
494 #define	RB_GENERATE(name, type, field, cmp)				\
495 	RB_GENERATE_INTERNAL(name, type, field, cmp,)
496 #define	RB_GENERATE_STATIC(name, type, field, cmp)			\
497 	RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
498 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr)		\
499 	RB_GENERATE_INSERT_COLOR(name, type, field, attr)		\
500 	RB_GENERATE_REMOVE_COLOR(name, type, field, attr)		\
501 	RB_GENERATE_INSERT(name, type, field, cmp, attr)		\
502 	RB_GENERATE_REMOVE(name, type, field, attr)			\
503 	RB_GENERATE_FIND(name, type, field, cmp, attr)			\
504 	RB_GENERATE_NFIND(name, type, field, cmp, attr)			\
505 	RB_GENERATE_NEXT(name, type, field, attr)			\
506 	RB_GENERATE_PREV(name, type, field, attr)			\
507 	RB_GENERATE_MINMAX(name, type, field, attr)			\
508 	RB_GENERATE_REINSERT(name, type, field, cmp, attr)
509 
510 
511 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr)		\
512 attr void								\
513 name##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
514 {									\
515 	struct type *parent, *gparent, *tmp;				\
516 	while (RB_ISRED((parent = RB_PARENT(elm, field)), field)) {	\
517 		gparent = RB_PARENT(parent, field);			\
518 		if (parent == RB_LEFT(gparent, field)) {		\
519 			tmp = RB_RIGHT(gparent, field);			\
520 			if (RB_ISRED(tmp, field)) {			\
521 				RB_COLOR(tmp, field) = RB_BLACK;	\
522 				RB_SET_BLACKRED(parent, gparent, field);\
523 				elm = gparent;				\
524 				continue;				\
525 			}						\
526 			if (RB_RIGHT(parent, field) == elm) {		\
527 				RB_PARENT_ROTATE_LEFT(gparent, parent,	\
528 				    tmp, field);			\
529 				tmp = parent;				\
530 				parent = elm;				\
531 				elm = tmp;				\
532 			}						\
533 			RB_SET_BLACKRED(parent, gparent, field);	\
534 			RB_ROTATE_RIGHT(head, gparent, tmp, field);	\
535 		} else {						\
536 			tmp = RB_LEFT(gparent, field);			\
537 			if (RB_ISRED(tmp, field)) {			\
538 				RB_COLOR(tmp, field) = RB_BLACK;	\
539 				RB_SET_BLACKRED(parent, gparent, field);\
540 				elm = gparent;				\
541 				continue;				\
542 			}						\
543 			if (RB_LEFT(parent, field) == elm) {		\
544 				RB_PARENT_ROTATE_RIGHT(gparent, parent,	\
545 				    tmp, field);			\
546 				tmp = parent;				\
547 				parent = elm;				\
548 				elm = tmp;				\
549 			}						\
550 			RB_SET_BLACKRED(parent, gparent, field);	\
551 			RB_ROTATE_LEFT(head, gparent, tmp, field);	\
552 		}							\
553 	}								\
554 	RB_COLOR(head->rbh_root, field) = RB_BLACK;			\
555 }
556 
557 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr)		\
558 attr void								\
559 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent)		\
560 {									\
561 	struct type *elm, *tmp;						\
562 	elm = NULL;							\
563 	do {								\
564 		if (RB_LEFT(parent, field) == elm) {			\
565 			tmp = RB_RIGHT(parent, field);			\
566 			if (RB_COLOR(tmp, field) == RB_RED) {		\
567 				RB_SET_BLACKRED(tmp, parent, field);	\
568 				RB_RED_ROTATE_LEFT(head, parent, tmp, field); \
569 				tmp = RB_RIGHT(parent, field);		\
570 			}						\
571 			if (RB_ISRED(RB_RIGHT(tmp, field), field))	\
572 				RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
573 			else if (RB_ISRED(RB_LEFT(tmp, field), field)) { \
574 				struct type *oleft;			\
575 				RB_PARENT_ROTATE_RIGHT(parent, tmp,	\
576 				    oleft, field);			\
577 				RB_COLOR(oleft, field) = RB_BLACK;	\
578 				tmp = oleft;				\
579 			} else {					\
580 				RB_COLOR(tmp, field) = RB_RED;		\
581 				elm = parent;				\
582 				parent = RB_PARENT(elm, field);		\
583 				continue;				\
584 			}						\
585 			RB_COLOR(tmp, field) = RB_COLOR(parent, field);	\
586 			RB_COLOR(parent, field) = RB_BLACK;		\
587 			RB_ROTATE_LEFT(head, parent, tmp, field);	\
588 			elm = RB_ROOT(head);				\
589 			break;						\
590 		} else {						\
591 			tmp = RB_LEFT(parent, field);			\
592 			if (RB_COLOR(tmp, field) == RB_RED) {		\
593 				RB_SET_BLACKRED(tmp, parent, field);	\
594 				RB_RED_ROTATE_RIGHT(head, parent, tmp, field); \
595 				tmp = RB_LEFT(parent, field);		\
596 			}						\
597 			if (RB_ISRED(RB_LEFT(tmp, field), field))	\
598 				RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
599 			else if (RB_ISRED(RB_RIGHT(tmp, field), field)) { \
600 				struct type *oright;			\
601 				RB_PARENT_ROTATE_LEFT(parent, tmp,	\
602 				    oright, field);			\
603 				RB_COLOR(oright, field) = RB_BLACK;	\
604 				tmp = oright;				\
605 			} else {					\
606 				RB_COLOR(tmp, field) = RB_RED;		\
607 				elm = parent;				\
608 				parent = RB_PARENT(elm, field);		\
609 				continue;				\
610 			}						\
611 			RB_COLOR(tmp, field) = RB_COLOR(parent, field);	\
612 			RB_COLOR(parent, field) = RB_BLACK;		\
613 			RB_ROTATE_RIGHT(head, parent, tmp, field);	\
614 			elm = RB_ROOT(head);				\
615 			break;						\
616 		}							\
617 	} while (RB_COLOR(elm, field) == RB_BLACK && parent != NULL);	\
618 	RB_COLOR(elm, field) = RB_BLACK;				\
619 }
620 
621 #define RB_GENERATE_REMOVE(name, type, field, attr)			\
622 attr struct type *							\
623 name##_RB_REMOVE(struct name *head, struct type *elm)			\
624 {									\
625 	struct type *child, *old, *parent, *right;			\
626 	int color;							\
627 									\
628 	old = elm;							\
629 	parent = RB_PARENT(elm, field);					\
630 	right = RB_RIGHT(elm, field);					\
631 	color = RB_COLOR(elm, field);					\
632 	if (RB_LEFT(elm, field) == NULL)				\
633 		elm = child = right;					\
634 	else if (right == NULL)						\
635 		elm = child = RB_LEFT(elm, field);			\
636 	else {								\
637 		if ((child = RB_LEFT(right, field)) == NULL) {		\
638 			child = RB_RIGHT(right, field);			\
639 			RB_RIGHT(old, field) = child;			\
640 			parent = elm = right;				\
641 		} else {						\
642 			do						\
643 				elm = child;				\
644 			while ((child = RB_LEFT(elm, field)) != NULL);	\
645 			child = RB_RIGHT(elm, field);			\
646 			parent = RB_PARENT(elm, field);			\
647 			RB_LEFT(parent, field) = child;			\
648 			RB_SET_PARENT(RB_RIGHT(old, field), elm, field); \
649 		}							\
650 		RB_SET_PARENT(RB_LEFT(old, field), elm, field);		\
651 		color = RB_COLOR(elm, field);				\
652 		elm->field = old->field;				\
653 	}								\
654 	RB_SWAP_CHILD(head, old, elm, field);				\
655 	if (child != NULL) {						\
656 		RB_SET_PARENT(child, parent, field);			\
657 		RB_COLOR(child, field) = RB_BLACK;			\
658 	} else if (color != RB_RED && parent != NULL)			\
659 		name##_RB_REMOVE_COLOR(head, parent);			\
660 	while (parent != NULL) {					\
661 		RB_AUGMENT(parent);					\
662 		parent = RB_PARENT(parent, field);			\
663 	}								\
664 	return (old);							\
665 }
666 
667 #define RB_GENERATE_INSERT(name, type, field, cmp, attr)		\
668 /* Inserts a node into the RB tree */					\
669 attr struct type *							\
670 name##_RB_INSERT(struct name *head, struct type *elm)			\
671 {									\
672 	struct type *tmp;						\
673 	struct type *parent = NULL;					\
674 	int comp = 0;							\
675 	tmp = RB_ROOT(head);						\
676 	while (tmp) {							\
677 		parent = tmp;						\
678 		comp = (cmp)(elm, parent);				\
679 		if (comp < 0)						\
680 			tmp = RB_LEFT(tmp, field);			\
681 		else if (comp > 0)					\
682 			tmp = RB_RIGHT(tmp, field);			\
683 		else							\
684 			return (tmp);					\
685 	}								\
686 	RB_SET(elm, parent, field);					\
687 	if (parent != NULL) {						\
688 		if (comp < 0)						\
689 			RB_LEFT(parent, field) = elm;			\
690 		else							\
691 			RB_RIGHT(parent, field) = elm;			\
692 	} else								\
693 		RB_ROOT(head) = elm;					\
694 	name##_RB_INSERT_COLOR(head, elm);				\
695 	while (elm != NULL) {						\
696 		RB_AUGMENT(elm);					\
697 		elm = RB_PARENT(elm, field);				\
698 	}								\
699 	return (NULL);							\
700 }
701 
702 #define RB_GENERATE_FIND(name, type, field, cmp, attr)			\
703 /* Finds the node with the same key as elm */				\
704 attr struct type *							\
705 name##_RB_FIND(struct name *head, struct type *elm)			\
706 {									\
707 	struct type *tmp = RB_ROOT(head);				\
708 	int comp;							\
709 	while (tmp) {							\
710 		comp = cmp(elm, tmp);					\
711 		if (comp < 0)						\
712 			tmp = RB_LEFT(tmp, field);			\
713 		else if (comp > 0)					\
714 			tmp = RB_RIGHT(tmp, field);			\
715 		else							\
716 			return (tmp);					\
717 	}								\
718 	return (NULL);							\
719 }
720 
721 #define RB_GENERATE_NFIND(name, type, field, cmp, attr)			\
722 /* Finds the first node greater than or equal to the search key */	\
723 attr struct type *							\
724 name##_RB_NFIND(struct name *head, struct type *elm)			\
725 {									\
726 	struct type *tmp = RB_ROOT(head);				\
727 	struct type *res = NULL;					\
728 	int comp;							\
729 	while (tmp) {							\
730 		comp = cmp(elm, tmp);					\
731 		if (comp < 0) {						\
732 			res = tmp;					\
733 			tmp = RB_LEFT(tmp, field);			\
734 		}							\
735 		else if (comp > 0)					\
736 			tmp = RB_RIGHT(tmp, field);			\
737 		else							\
738 			return (tmp);					\
739 	}								\
740 	return (res);							\
741 }
742 
743 #define RB_GENERATE_NEXT(name, type, field, attr)			\
744 /* ARGSUSED */								\
745 attr struct type *							\
746 name##_RB_NEXT(struct type *elm)					\
747 {									\
748 	if (RB_RIGHT(elm, field)) {					\
749 		elm = RB_RIGHT(elm, field);				\
750 		while (RB_LEFT(elm, field))				\
751 			elm = RB_LEFT(elm, field);			\
752 	} else {							\
753 		if (RB_PARENT(elm, field) &&				\
754 		    (elm == RB_LEFT(RB_PARENT(elm, field), field)))	\
755 			elm = RB_PARENT(elm, field);			\
756 		else {							\
757 			while (RB_PARENT(elm, field) &&			\
758 			    (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
759 				elm = RB_PARENT(elm, field);		\
760 			elm = RB_PARENT(elm, field);			\
761 		}							\
762 	}								\
763 	return (elm);							\
764 }
765 
766 #define RB_GENERATE_PREV(name, type, field, attr)			\
767 /* ARGSUSED */								\
768 attr struct type *							\
769 name##_RB_PREV(struct type *elm)					\
770 {									\
771 	if (RB_LEFT(elm, field)) {					\
772 		elm = RB_LEFT(elm, field);				\
773 		while (RB_RIGHT(elm, field))				\
774 			elm = RB_RIGHT(elm, field);			\
775 	} else {							\
776 		if (RB_PARENT(elm, field) &&				\
777 		    (elm == RB_RIGHT(RB_PARENT(elm, field), field)))	\
778 			elm = RB_PARENT(elm, field);			\
779 		else {							\
780 			while (RB_PARENT(elm, field) &&			\
781 			    (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
782 				elm = RB_PARENT(elm, field);		\
783 			elm = RB_PARENT(elm, field);			\
784 		}							\
785 	}								\
786 	return (elm);							\
787 }
788 
789 #define RB_GENERATE_MINMAX(name, type, field, attr)			\
790 attr struct type *							\
791 name##_RB_MINMAX(struct name *head, int val)				\
792 {									\
793 	struct type *tmp = RB_ROOT(head);				\
794 	struct type *parent = NULL;					\
795 	while (tmp) {							\
796 		parent = tmp;						\
797 		if (val < 0)						\
798 			tmp = RB_LEFT(tmp, field);			\
799 		else							\
800 			tmp = RB_RIGHT(tmp, field);			\
801 	}								\
802 	return (parent);						\
803 }
804 
805 #define	RB_GENERATE_REINSERT(name, type, field, cmp, attr)		\
806 attr struct type *							\
807 name##_RB_REINSERT(struct name *head, struct type *elm)			\
808 {									\
809 	struct type *cmpelm;						\
810 	if (((cmpelm = RB_PREV(name, head, elm)) != NULL &&		\
811 	    cmp(cmpelm, elm) >= 0) ||					\
812 	    ((cmpelm = RB_NEXT(name, head, elm)) != NULL &&		\
813 	    cmp(elm, cmpelm) >= 0)) {					\
814 		/* XXXLAS: Remove/insert is heavy handed. */		\
815 		RB_REMOVE(name, head, elm);				\
816 		return (RB_INSERT(name, head, elm));			\
817 	}								\
818 	return (NULL);							\
819 }									\
820 
821 #define RB_NEGINF	-1
822 #define RB_INF	1
823 
824 #define RB_INSERT(name, x, y)	name##_RB_INSERT(x, y)
825 #define RB_REMOVE(name, x, y)	name##_RB_REMOVE(x, y)
826 #define RB_FIND(name, x, y)	name##_RB_FIND(x, y)
827 #define RB_NFIND(name, x, y)	name##_RB_NFIND(x, y)
828 #define RB_NEXT(name, x, y)	name##_RB_NEXT(y)
829 #define RB_PREV(name, x, y)	name##_RB_PREV(y)
830 #define RB_MIN(name, x)		name##_RB_MINMAX(x, RB_NEGINF)
831 #define RB_MAX(name, x)		name##_RB_MINMAX(x, RB_INF)
832 #define RB_REINSERT(name, x, y)	name##_RB_REINSERT(x, y)
833 
834 #define RB_FOREACH(x, name, head)					\
835 	for ((x) = RB_MIN(name, head);					\
836 	     (x) != NULL;						\
837 	     (x) = name##_RB_NEXT(x))
838 
839 #define RB_FOREACH_FROM(x, name, y)					\
840 	for ((x) = (y);							\
841 	    ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);	\
842 	     (x) = (y))
843 
844 #define RB_FOREACH_SAFE(x, name, head, y)				\
845 	for ((x) = RB_MIN(name, head);					\
846 	    ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);	\
847 	     (x) = (y))
848 
849 #define RB_FOREACH_REVERSE(x, name, head)				\
850 	for ((x) = RB_MAX(name, head);					\
851 	     (x) != NULL;						\
852 	     (x) = name##_RB_PREV(x))
853 
854 #define RB_FOREACH_REVERSE_FROM(x, name, y)				\
855 	for ((x) = (y);							\
856 	    ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);	\
857 	     (x) = (y))
858 
859 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y)			\
860 	for ((x) = RB_MAX(name, head);					\
861 	    ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);	\
862 	     (x) = (y))
863 
864 #endif	/* _SYS_TREE_H_ */
865