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