tree.h revision 127403
1127403Sdes/*	$NetBSD: tree.h,v 1.7 2004/01/24 21:59:47 dbj Exp $	*/
2127208Sdes/*	$OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $	*/
398679Sdes/*
498679Sdes * Copyright 2002 Niels Provos <provos@citi.umich.edu>
598679Sdes * All rights reserved.
698679Sdes *
798679Sdes * Redistribution and use in source and binary forms, with or without
898679Sdes * modification, are permitted provided that the following conditions
998679Sdes * are met:
1098679Sdes * 1. Redistributions of source code must retain the above copyright
1198679Sdes *    notice, this list of conditions and the following disclaimer.
1298679Sdes * 2. Redistributions in binary form must reproduce the above copyright
1398679Sdes *    notice, this list of conditions and the following disclaimer in the
1498679Sdes *    documentation and/or other materials provided with the distribution.
1598679Sdes *
1698679Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1798679Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1898679Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1998679Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2098679Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2198679Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2298679Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2398679Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2498679Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2598679Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2698679Sdes */
2798679Sdes
2898679Sdes#ifndef	_SYS_TREE_H_
2998679Sdes#define	_SYS_TREE_H_
3098679Sdes
3198679Sdes/*
3298679Sdes * This file defines data structures for different types of trees:
3398679Sdes * splay trees and red-black trees.
3498679Sdes *
3598679Sdes * A splay tree is a self-organizing data structure.  Every operation
3698679Sdes * on the tree causes a splay to happen.  The splay moves the requested
3798679Sdes * node to the root of the tree and partly rebalances it.
3898679Sdes *
3998679Sdes * This has the benefit that request locality causes faster lookups as
4098679Sdes * the requested nodes move to the top of the tree.  On the other hand,
4198679Sdes * every lookup causes memory writes.
4298679Sdes *
4398679Sdes * The Balance Theorem bounds the total access time for m operations
4498679Sdes * and n inserts on an initially empty tree as O((m + n)lg n).  The
4598679Sdes * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
4698679Sdes *
4798679Sdes * A red-black tree is a binary search tree with the node color as an
4898679Sdes * extra attribute.  It fulfills a set of conditions:
4998679Sdes *	- every search path from the root to a leaf consists of the
5098679Sdes *	  same number of black nodes,
5198679Sdes *	- each red node (except for the root) has a black parent,
5298679Sdes *	- each leaf node is black.
5398679Sdes *
5498679Sdes * Every operation on a red-black tree is bounded as O(lg n).
5598679Sdes * The maximum height of a red-black tree is 2lg (n+1).
5698679Sdes */
5798679Sdes
5898679Sdes#define SPLAY_HEAD(name, type)						\
5998679Sdesstruct name {								\
6098679Sdes	struct type *sph_root; /* root of the tree */			\
6198679Sdes}
6298679Sdes
6398679Sdes#define SPLAY_INITIALIZER(root)						\
6498679Sdes	{ NULL }
6598679Sdes
6698679Sdes#define SPLAY_INIT(root) do {						\
6798679Sdes	(root)->sph_root = NULL;					\
68127403Sdes} while (/*CONSTCOND*/ 0)
6998679Sdes
7098679Sdes#define SPLAY_ENTRY(type)						\
7198679Sdesstruct {								\
7298679Sdes	struct type *spe_left; /* left element */			\
7398679Sdes	struct type *spe_right; /* right element */			\
7498679Sdes}
7598679Sdes
7698679Sdes#define SPLAY_LEFT(elm, field)		(elm)->field.spe_left
7798679Sdes#define SPLAY_RIGHT(elm, field)		(elm)->field.spe_right
7898679Sdes#define SPLAY_ROOT(head)		(head)->sph_root
7998679Sdes#define SPLAY_EMPTY(head)		(SPLAY_ROOT(head) == NULL)
8098679Sdes
8198679Sdes/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
8298679Sdes#define SPLAY_ROTATE_RIGHT(head, tmp, field) do {			\
8398679Sdes	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field);	\
8498679Sdes	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
8598679Sdes	(head)->sph_root = tmp;						\
86127403Sdes} while (/*CONSTCOND*/ 0)
8798679Sdes
8898679Sdes#define SPLAY_ROTATE_LEFT(head, tmp, field) do {			\
8998679Sdes	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field);	\
9098679Sdes	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
9198679Sdes	(head)->sph_root = tmp;						\
92127403Sdes} while (/*CONSTCOND*/ 0)
9398679Sdes
9498679Sdes#define SPLAY_LINKLEFT(head, tmp, field) do {				\
9598679Sdes	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
9698679Sdes	tmp = (head)->sph_root;						\
9798679Sdes	(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);		\
98127403Sdes} while (/*CONSTCOND*/ 0)
9998679Sdes
10098679Sdes#define SPLAY_LINKRIGHT(head, tmp, field) do {				\
10198679Sdes	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
10298679Sdes	tmp = (head)->sph_root;						\
10398679Sdes	(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);	\
104127403Sdes} while (/*CONSTCOND*/ 0)
10598679Sdes
10698679Sdes#define SPLAY_ASSEMBLE(head, node, left, right, field) do {		\
10798679Sdes	SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field);	\
10898679Sdes	SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
10998679Sdes	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field);	\
11098679Sdes	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field);	\
111127403Sdes} while (/*CONSTCOND*/ 0)
11298679Sdes
11398679Sdes/* Generates prototypes and inline functions */
11498679Sdes
11598679Sdes#define SPLAY_PROTOTYPE(name, type, field, cmp)				\
11698679Sdesvoid name##_SPLAY(struct name *, struct type *);			\
11798679Sdesvoid name##_SPLAY_MINMAX(struct name *, int);				\
11898679Sdesstruct type *name##_SPLAY_INSERT(struct name *, struct type *);		\
11998679Sdesstruct type *name##_SPLAY_REMOVE(struct name *, struct type *);		\
12098679Sdes									\
12198679Sdes/* Finds the node with the same key as elm */				\
12298679Sdesstatic __inline struct type *						\
12398679Sdesname##_SPLAY_FIND(struct name *head, struct type *elm)			\
12498679Sdes{									\
12598679Sdes	if (SPLAY_EMPTY(head))						\
12698679Sdes		return(NULL);						\
12798679Sdes	name##_SPLAY(head, elm);					\
12898679Sdes	if ((cmp)(elm, (head)->sph_root) == 0)				\
12998679Sdes		return (head->sph_root);				\
13098679Sdes	return (NULL);							\
13198679Sdes}									\
13298679Sdes									\
13398679Sdesstatic __inline struct type *						\
13498679Sdesname##_SPLAY_NEXT(struct name *head, struct type *elm)			\
13598679Sdes{									\
13698679Sdes	name##_SPLAY(head, elm);					\
13798679Sdes	if (SPLAY_RIGHT(elm, field) != NULL) {				\
13898679Sdes		elm = SPLAY_RIGHT(elm, field);				\
13998679Sdes		while (SPLAY_LEFT(elm, field) != NULL) {		\
14098679Sdes			elm = SPLAY_LEFT(elm, field);			\
14198679Sdes		}							\
14298679Sdes	} else								\
14398679Sdes		elm = NULL;						\
14498679Sdes	return (elm);							\
14598679Sdes}									\
14698679Sdes									\
14798679Sdesstatic __inline struct type *						\
14898679Sdesname##_SPLAY_MIN_MAX(struct name *head, int val)			\
14998679Sdes{									\
15098679Sdes	name##_SPLAY_MINMAX(head, val);					\
15198679Sdes        return (SPLAY_ROOT(head));					\
15298679Sdes}
15398679Sdes
15498679Sdes/* Main splay operation.
15598679Sdes * Moves node close to the key of elm to top
15698679Sdes */
15798679Sdes#define SPLAY_GENERATE(name, type, field, cmp)				\
15898679Sdesstruct type *								\
15998679Sdesname##_SPLAY_INSERT(struct name *head, struct type *elm)		\
16098679Sdes{									\
16198679Sdes    if (SPLAY_EMPTY(head)) {						\
16298679Sdes	    SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL;	\
16398679Sdes    } else {								\
16498679Sdes	    int __comp;							\
16598679Sdes	    name##_SPLAY(head, elm);					\
16698679Sdes	    __comp = (cmp)(elm, (head)->sph_root);			\
16798679Sdes	    if(__comp < 0) {						\
16898679Sdes		    SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
16998679Sdes		    SPLAY_RIGHT(elm, field) = (head)->sph_root;		\
17098679Sdes		    SPLAY_LEFT((head)->sph_root, field) = NULL;		\
17198679Sdes	    } else if (__comp > 0) {					\
17298679Sdes		    SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
17398679Sdes		    SPLAY_LEFT(elm, field) = (head)->sph_root;		\
17498679Sdes		    SPLAY_RIGHT((head)->sph_root, field) = NULL;	\
17598679Sdes	    } else							\
17698679Sdes		    return ((head)->sph_root);				\
17798679Sdes    }									\
17898679Sdes    (head)->sph_root = (elm);						\
17998679Sdes    return (NULL);							\
18098679Sdes}									\
18198679Sdes									\
18298679Sdesstruct type *								\
18398679Sdesname##_SPLAY_REMOVE(struct name *head, struct type *elm)		\
18498679Sdes{									\
18598679Sdes	struct type *__tmp;						\
18698679Sdes	if (SPLAY_EMPTY(head))						\
18798679Sdes		return (NULL);						\
18898679Sdes	name##_SPLAY(head, elm);					\
18998679Sdes	if ((cmp)(elm, (head)->sph_root) == 0) {			\
19098679Sdes		if (SPLAY_LEFT((head)->sph_root, field) == NULL) {	\
19198679Sdes			(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
19298679Sdes		} else {						\
19398679Sdes			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
19498679Sdes			(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
19598679Sdes			name##_SPLAY(head, elm);			\
19698679Sdes			SPLAY_RIGHT((head)->sph_root, field) = __tmp;	\
19798679Sdes		}							\
19898679Sdes		return (elm);						\
19998679Sdes	}								\
20098679Sdes	return (NULL);							\
20198679Sdes}									\
20298679Sdes									\
20398679Sdesvoid									\
20498679Sdesname##_SPLAY(struct name *head, struct type *elm)			\
20598679Sdes{									\
20698679Sdes	struct type __node, *__left, *__right, *__tmp;			\
20798679Sdes	int __comp;							\
20898679Sdes\
20998679Sdes	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
21098679Sdes	__left = __right = &__node;					\
21198679Sdes\
212127403Sdes	while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) {		\
21398679Sdes		if (__comp < 0) {					\
21498679Sdes			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
21598679Sdes			if (__tmp == NULL)				\
21698679Sdes				break;					\
21798679Sdes			if ((cmp)(elm, __tmp) < 0){			\
21898679Sdes				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
21998679Sdes				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
22098679Sdes					break;				\
22198679Sdes			}						\
22298679Sdes			SPLAY_LINKLEFT(head, __right, field);		\
22398679Sdes		} else if (__comp > 0) {				\
22498679Sdes			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
22598679Sdes			if (__tmp == NULL)				\
22698679Sdes				break;					\
22798679Sdes			if ((cmp)(elm, __tmp) > 0){			\
22898679Sdes				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
22998679Sdes				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
23098679Sdes					break;				\
23198679Sdes			}						\
23298679Sdes			SPLAY_LINKRIGHT(head, __left, field);		\
23398679Sdes		}							\
23498679Sdes	}								\
23598679Sdes	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
23698679Sdes}									\
23798679Sdes									\
23898679Sdes/* Splay with either the minimum or the maximum element			\
23998679Sdes * Used to find minimum or maximum element in tree.			\
24098679Sdes */									\
24198679Sdesvoid name##_SPLAY_MINMAX(struct name *head, int __comp) \
24298679Sdes{									\
24398679Sdes	struct type __node, *__left, *__right, *__tmp;			\
24498679Sdes\
24598679Sdes	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
24698679Sdes	__left = __right = &__node;					\
24798679Sdes\
24898679Sdes	while (1) {							\
24998679Sdes		if (__comp < 0) {					\
25098679Sdes			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
25198679Sdes			if (__tmp == NULL)				\
25298679Sdes				break;					\
25398679Sdes			if (__comp < 0){				\
25498679Sdes				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
25598679Sdes				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
25698679Sdes					break;				\
25798679Sdes			}						\
25898679Sdes			SPLAY_LINKLEFT(head, __right, field);		\
25998679Sdes		} else if (__comp > 0) {				\
26098679Sdes			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
26198679Sdes			if (__tmp == NULL)				\
26298679Sdes				break;					\
26398679Sdes			if (__comp > 0) {				\
26498679Sdes				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
26598679Sdes				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
26698679Sdes					break;				\
26798679Sdes			}						\
26898679Sdes			SPLAY_LINKRIGHT(head, __left, field);		\
26998679Sdes		}							\
27098679Sdes	}								\
27198679Sdes	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
27298679Sdes}
27398679Sdes
27498679Sdes#define SPLAY_NEGINF	-1
27598679Sdes#define SPLAY_INF	1
27698679Sdes
27798679Sdes#define SPLAY_INSERT(name, x, y)	name##_SPLAY_INSERT(x, y)
27898679Sdes#define SPLAY_REMOVE(name, x, y)	name##_SPLAY_REMOVE(x, y)
27998679Sdes#define SPLAY_FIND(name, x, y)		name##_SPLAY_FIND(x, y)
28098679Sdes#define SPLAY_NEXT(name, x, y)		name##_SPLAY_NEXT(x, y)
28198679Sdes#define SPLAY_MIN(name, x)		(SPLAY_EMPTY(x) ? NULL	\
28298679Sdes					: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
28398679Sdes#define SPLAY_MAX(name, x)		(SPLAY_EMPTY(x) ? NULL	\
28498679Sdes					: name##_SPLAY_MIN_MAX(x, SPLAY_INF))
28598679Sdes
28698679Sdes#define SPLAY_FOREACH(x, name, head)					\
28798679Sdes	for ((x) = SPLAY_MIN(name, head);				\
28898679Sdes	     (x) != NULL;						\
28998679Sdes	     (x) = SPLAY_NEXT(name, head, x))
29098679Sdes
291127403Sdes/* Macros that define a red-black tree */
29298679Sdes#define RB_HEAD(name, type)						\
29398679Sdesstruct name {								\
29498679Sdes	struct type *rbh_root; /* root of the tree */			\
29598679Sdes}
29698679Sdes
29798679Sdes#define RB_INITIALIZER(root)						\
29898679Sdes	{ NULL }
29998679Sdes
30098679Sdes#define RB_INIT(root) do {						\
30198679Sdes	(root)->rbh_root = NULL;					\
302127403Sdes} while (/*CONSTCOND*/ 0)
30398679Sdes
30498679Sdes#define RB_BLACK	0
30598679Sdes#define RB_RED		1
30698679Sdes#define RB_ENTRY(type)							\
30798679Sdesstruct {								\
30898679Sdes	struct type *rbe_left;		/* left element */		\
30998679Sdes	struct type *rbe_right;		/* right element */		\
31098679Sdes	struct type *rbe_parent;	/* parent element */		\
31198679Sdes	int rbe_color;			/* node color */		\
31298679Sdes}
31398679Sdes
31498679Sdes#define RB_LEFT(elm, field)		(elm)->field.rbe_left
31598679Sdes#define RB_RIGHT(elm, field)		(elm)->field.rbe_right
31698679Sdes#define RB_PARENT(elm, field)		(elm)->field.rbe_parent
31798679Sdes#define RB_COLOR(elm, field)		(elm)->field.rbe_color
31898679Sdes#define RB_ROOT(head)			(head)->rbh_root
31998679Sdes#define RB_EMPTY(head)			(RB_ROOT(head) == NULL)
32098679Sdes
32198679Sdes#define RB_SET(elm, parent, field) do {					\
32298679Sdes	RB_PARENT(elm, field) = parent;					\
32398679Sdes	RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL;		\
32498679Sdes	RB_COLOR(elm, field) = RB_RED;					\
325127403Sdes} while (/*CONSTCOND*/ 0)
32698679Sdes
32798679Sdes#define RB_SET_BLACKRED(black, red, field) do {				\
32898679Sdes	RB_COLOR(black, field) = RB_BLACK;				\
32998679Sdes	RB_COLOR(red, field) = RB_RED;					\
330127403Sdes} while (/*CONSTCOND*/ 0)
33198679Sdes
33298679Sdes#ifndef RB_AUGMENT
33398679Sdes#define RB_AUGMENT(x)
33498679Sdes#endif
33598679Sdes
33698679Sdes#define RB_ROTATE_LEFT(head, elm, tmp, field) do {			\
33798679Sdes	(tmp) = RB_RIGHT(elm, field);					\
338127403Sdes	if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) {	\
33998679Sdes		RB_PARENT(RB_LEFT(tmp, field), field) = (elm);		\
34098679Sdes	}								\
34198679Sdes	RB_AUGMENT(elm);						\
342127403Sdes	if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {	\
34398679Sdes		if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))	\
34498679Sdes			RB_LEFT(RB_PARENT(elm, field), field) = (tmp);	\
34598679Sdes		else							\
34698679Sdes			RB_RIGHT(RB_PARENT(elm, field), field) = (tmp);	\
34798679Sdes	} else								\
34898679Sdes		(head)->rbh_root = (tmp);				\
34998679Sdes	RB_LEFT(tmp, field) = (elm);					\
35098679Sdes	RB_PARENT(elm, field) = (tmp);					\
35198679Sdes	RB_AUGMENT(tmp);						\
352127208Sdes	if ((RB_PARENT(tmp, field)))					\
353127208Sdes		RB_AUGMENT(RB_PARENT(tmp, field));			\
354127403Sdes} while (/*CONSTCOND*/ 0)
35598679Sdes
35698679Sdes#define RB_ROTATE_RIGHT(head, elm, tmp, field) do {			\
35798679Sdes	(tmp) = RB_LEFT(elm, field);					\
358127403Sdes	if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) {	\
35998679Sdes		RB_PARENT(RB_RIGHT(tmp, field), field) = (elm);		\
36098679Sdes	}								\
36198679Sdes	RB_AUGMENT(elm);						\
362127403Sdes	if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {	\
36398679Sdes		if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))	\
36498679Sdes			RB_LEFT(RB_PARENT(elm, field), field) = (tmp);	\
36598679Sdes		else							\
36698679Sdes			RB_RIGHT(RB_PARENT(elm, field), field) = (tmp);	\
36798679Sdes	} else								\
36898679Sdes		(head)->rbh_root = (tmp);				\
36998679Sdes	RB_RIGHT(tmp, field) = (elm);					\
37098679Sdes	RB_PARENT(elm, field) = (tmp);					\
37198679Sdes	RB_AUGMENT(tmp);						\
372127208Sdes	if ((RB_PARENT(tmp, field)))					\
373127208Sdes		RB_AUGMENT(RB_PARENT(tmp, field));			\
374127403Sdes} while (/*CONSTCOND*/ 0)
37598679Sdes
37698679Sdes/* Generates prototypes and inline functions */
37798679Sdes#define RB_PROTOTYPE(name, type, field, cmp)				\
37898679Sdesvoid name##_RB_INSERT_COLOR(struct name *, struct type *);	\
37998679Sdesvoid name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
38098679Sdesstruct type *name##_RB_REMOVE(struct name *, struct type *);		\
38198679Sdesstruct type *name##_RB_INSERT(struct name *, struct type *);		\
38298679Sdesstruct type *name##_RB_FIND(struct name *, struct type *);		\
38398679Sdesstruct type *name##_RB_NEXT(struct name *, struct type *);		\
38498679Sdesstruct type *name##_RB_MINMAX(struct name *, int);			\
38598679Sdes									\
38698679Sdes
38798679Sdes/* Main rb operation.
38898679Sdes * Moves node close to the key of elm to top
38998679Sdes */
39098679Sdes#define RB_GENERATE(name, type, field, cmp)				\
39198679Sdesvoid									\
39298679Sdesname##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
39398679Sdes{									\
39498679Sdes	struct type *parent, *gparent, *tmp;				\
395127403Sdes	while ((parent = RB_PARENT(elm, field)) != NULL &&		\
39698679Sdes	    RB_COLOR(parent, field) == RB_RED) {			\
39798679Sdes		gparent = RB_PARENT(parent, field);			\
39898679Sdes		if (parent == RB_LEFT(gparent, field)) {		\
39998679Sdes			tmp = RB_RIGHT(gparent, field);			\
40098679Sdes			if (tmp && RB_COLOR(tmp, field) == RB_RED) {	\
40198679Sdes				RB_COLOR(tmp, field) = RB_BLACK;	\
40298679Sdes				RB_SET_BLACKRED(parent, gparent, field);\
40398679Sdes				elm = gparent;				\
40498679Sdes				continue;				\
40598679Sdes			}						\
40698679Sdes			if (RB_RIGHT(parent, field) == elm) {		\
40798679Sdes				RB_ROTATE_LEFT(head, parent, tmp, field);\
40898679Sdes				tmp = parent;				\
40998679Sdes				parent = elm;				\
41098679Sdes				elm = tmp;				\
41198679Sdes			}						\
41298679Sdes			RB_SET_BLACKRED(parent, gparent, field);	\
41398679Sdes			RB_ROTATE_RIGHT(head, gparent, tmp, field);	\
41498679Sdes		} else {						\
41598679Sdes			tmp = RB_LEFT(gparent, field);			\
41698679Sdes			if (tmp && RB_COLOR(tmp, field) == RB_RED) {	\
41798679Sdes				RB_COLOR(tmp, field) = RB_BLACK;	\
41898679Sdes				RB_SET_BLACKRED(parent, gparent, field);\
41998679Sdes				elm = gparent;				\
42098679Sdes				continue;				\
42198679Sdes			}						\
42298679Sdes			if (RB_LEFT(parent, field) == elm) {		\
42398679Sdes				RB_ROTATE_RIGHT(head, parent, tmp, field);\
42498679Sdes				tmp = parent;				\
42598679Sdes				parent = elm;				\
42698679Sdes				elm = tmp;				\
42798679Sdes			}						\
42898679Sdes			RB_SET_BLACKRED(parent, gparent, field);	\
42998679Sdes			RB_ROTATE_LEFT(head, gparent, tmp, field);	\
43098679Sdes		}							\
43198679Sdes	}								\
43298679Sdes	RB_COLOR(head->rbh_root, field) = RB_BLACK;			\
43398679Sdes}									\
43498679Sdes									\
43598679Sdesvoid									\
43698679Sdesname##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
43798679Sdes{									\
43898679Sdes	struct type *tmp;						\
43998679Sdes	while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) &&	\
44098679Sdes	    elm != RB_ROOT(head)) {					\
44198679Sdes		if (RB_LEFT(parent, field) == elm) {			\
44298679Sdes			tmp = RB_RIGHT(parent, field);			\
44398679Sdes			if (RB_COLOR(tmp, field) == RB_RED) {		\
44498679Sdes				RB_SET_BLACKRED(tmp, parent, field);	\
44598679Sdes				RB_ROTATE_LEFT(head, parent, tmp, field);\
44698679Sdes				tmp = RB_RIGHT(parent, field);		\
44798679Sdes			}						\
44898679Sdes			if ((RB_LEFT(tmp, field) == NULL ||		\
44998679Sdes			    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
45098679Sdes			    (RB_RIGHT(tmp, field) == NULL ||		\
45198679Sdes			    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
45298679Sdes				RB_COLOR(tmp, field) = RB_RED;		\
45398679Sdes				elm = parent;				\
45498679Sdes				parent = RB_PARENT(elm, field);		\
45598679Sdes			} else {					\
45698679Sdes				if (RB_RIGHT(tmp, field) == NULL ||	\
45798679Sdes				    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
45898679Sdes					struct type *oleft;		\
459127403Sdes					if ((oleft = RB_LEFT(tmp, field)) \
460127403Sdes					    != NULL)			\
46198679Sdes						RB_COLOR(oleft, field) = RB_BLACK;\
46298679Sdes					RB_COLOR(tmp, field) = RB_RED;	\
46398679Sdes					RB_ROTATE_RIGHT(head, tmp, oleft, field);\
46498679Sdes					tmp = RB_RIGHT(parent, field);	\
46598679Sdes				}					\
46698679Sdes				RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
46798679Sdes				RB_COLOR(parent, field) = RB_BLACK;	\
46898679Sdes				if (RB_RIGHT(tmp, field))		\
46998679Sdes					RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
47098679Sdes				RB_ROTATE_LEFT(head, parent, tmp, field);\
47198679Sdes				elm = RB_ROOT(head);			\
47298679Sdes				break;					\
47398679Sdes			}						\
47498679Sdes		} else {						\
47598679Sdes			tmp = RB_LEFT(parent, field);			\
47698679Sdes			if (RB_COLOR(tmp, field) == RB_RED) {		\
47798679Sdes				RB_SET_BLACKRED(tmp, parent, field);	\
47898679Sdes				RB_ROTATE_RIGHT(head, parent, tmp, field);\
47998679Sdes				tmp = RB_LEFT(parent, field);		\
48098679Sdes			}						\
48198679Sdes			if ((RB_LEFT(tmp, field) == NULL ||		\
48298679Sdes			    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
48398679Sdes			    (RB_RIGHT(tmp, field) == NULL ||		\
48498679Sdes			    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
48598679Sdes				RB_COLOR(tmp, field) = RB_RED;		\
48698679Sdes				elm = parent;				\
48798679Sdes				parent = RB_PARENT(elm, field);		\
48898679Sdes			} else {					\
48998679Sdes				if (RB_LEFT(tmp, field) == NULL ||	\
49098679Sdes				    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
49198679Sdes					struct type *oright;		\
492127403Sdes					if ((oright = RB_RIGHT(tmp, field)) \
493127403Sdes					    != NULL)			\
49498679Sdes						RB_COLOR(oright, field) = RB_BLACK;\
49598679Sdes					RB_COLOR(tmp, field) = RB_RED;	\
49698679Sdes					RB_ROTATE_LEFT(head, tmp, oright, field);\
49798679Sdes					tmp = RB_LEFT(parent, field);	\
49898679Sdes				}					\
49998679Sdes				RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
50098679Sdes				RB_COLOR(parent, field) = RB_BLACK;	\
50198679Sdes				if (RB_LEFT(tmp, field))		\
50298679Sdes					RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
50398679Sdes				RB_ROTATE_RIGHT(head, parent, tmp, field);\
50498679Sdes				elm = RB_ROOT(head);			\
50598679Sdes				break;					\
50698679Sdes			}						\
50798679Sdes		}							\
50898679Sdes	}								\
50998679Sdes	if (elm)							\
51098679Sdes		RB_COLOR(elm, field) = RB_BLACK;			\
51198679Sdes}									\
51298679Sdes									\
51398679Sdesstruct type *								\
51498679Sdesname##_RB_REMOVE(struct name *head, struct type *elm)			\
51598679Sdes{									\
51698679Sdes	struct type *child, *parent, *old = elm;			\
51798679Sdes	int color;							\
51898679Sdes	if (RB_LEFT(elm, field) == NULL)				\
51998679Sdes		child = RB_RIGHT(elm, field);				\
52098679Sdes	else if (RB_RIGHT(elm, field) == NULL)				\
52198679Sdes		child = RB_LEFT(elm, field);				\
52298679Sdes	else {								\
52398679Sdes		struct type *left;					\
52498679Sdes		elm = RB_RIGHT(elm, field);				\
525127403Sdes		while ((left = RB_LEFT(elm, field)) != NULL)		\
52698679Sdes			elm = left;					\
52798679Sdes		child = RB_RIGHT(elm, field);				\
52898679Sdes		parent = RB_PARENT(elm, field);				\
52998679Sdes		color = RB_COLOR(elm, field);				\
53098679Sdes		if (child)						\
53198679Sdes			RB_PARENT(child, field) = parent;		\
53298679Sdes		if (parent) {						\
53398679Sdes			if (RB_LEFT(parent, field) == elm)		\
53498679Sdes				RB_LEFT(parent, field) = child;		\
53598679Sdes			else						\
53698679Sdes				RB_RIGHT(parent, field) = child;	\
53798679Sdes			RB_AUGMENT(parent);				\
53898679Sdes		} else							\
53998679Sdes			RB_ROOT(head) = child;				\
54098679Sdes		if (RB_PARENT(elm, field) == old)			\
54198679Sdes			parent = elm;					\
54298679Sdes		(elm)->field = (old)->field;				\
54398679Sdes		if (RB_PARENT(old, field)) {				\
54498679Sdes			if (RB_LEFT(RB_PARENT(old, field), field) == old)\
54598679Sdes				RB_LEFT(RB_PARENT(old, field), field) = elm;\
54698679Sdes			else						\
54798679Sdes				RB_RIGHT(RB_PARENT(old, field), field) = elm;\
54898679Sdes			RB_AUGMENT(RB_PARENT(old, field));		\
54998679Sdes		} else							\
55098679Sdes			RB_ROOT(head) = elm;				\
55198679Sdes		RB_PARENT(RB_LEFT(old, field), field) = elm;		\
55298679Sdes		if (RB_RIGHT(old, field))				\
55398679Sdes			RB_PARENT(RB_RIGHT(old, field), field) = elm;	\
55498679Sdes		if (parent) {						\
55598679Sdes			left = parent;					\
55698679Sdes			do {						\
55798679Sdes				RB_AUGMENT(left);			\
558127403Sdes			} while ((left = RB_PARENT(left, field)) != NULL); \
55998679Sdes		}							\
56098679Sdes		goto color;						\
56198679Sdes	}								\
56298679Sdes	parent = RB_PARENT(elm, field);					\
56398679Sdes	color = RB_COLOR(elm, field);					\
56498679Sdes	if (child)							\
56598679Sdes		RB_PARENT(child, field) = parent;			\
56698679Sdes	if (parent) {							\
56798679Sdes		if (RB_LEFT(parent, field) == elm)			\
56898679Sdes			RB_LEFT(parent, field) = child;			\
56998679Sdes		else							\
57098679Sdes			RB_RIGHT(parent, field) = child;		\
57198679Sdes		RB_AUGMENT(parent);					\
57298679Sdes	} else								\
57398679Sdes		RB_ROOT(head) = child;					\
57498679Sdescolor:									\
57598679Sdes	if (color == RB_BLACK)						\
57698679Sdes		name##_RB_REMOVE_COLOR(head, parent, child);		\
57798679Sdes	return (old);							\
57898679Sdes}									\
57998679Sdes									\
58098679Sdes/* Inserts a node into the RB tree */					\
58198679Sdesstruct type *								\
58298679Sdesname##_RB_INSERT(struct name *head, struct type *elm)			\
58398679Sdes{									\
58498679Sdes	struct type *tmp;						\
58598679Sdes	struct type *parent = NULL;					\
58698679Sdes	int comp = 0;							\
58798679Sdes	tmp = RB_ROOT(head);						\
58898679Sdes	while (tmp) {							\
58998679Sdes		parent = tmp;						\
59098679Sdes		comp = (cmp)(elm, parent);				\
59198679Sdes		if (comp < 0)						\
59298679Sdes			tmp = RB_LEFT(tmp, field);			\
59398679Sdes		else if (comp > 0)					\
59498679Sdes			tmp = RB_RIGHT(tmp, field);			\
59598679Sdes		else							\
59698679Sdes			return (tmp);					\
59798679Sdes	}								\
59898679Sdes	RB_SET(elm, parent, field);					\
59998679Sdes	if (parent != NULL) {						\
60098679Sdes		if (comp < 0)						\
60198679Sdes			RB_LEFT(parent, field) = elm;			\
60298679Sdes		else							\
60398679Sdes			RB_RIGHT(parent, field) = elm;			\
60498679Sdes		RB_AUGMENT(parent);					\
60598679Sdes	} else								\
60698679Sdes		RB_ROOT(head) = elm;					\
60798679Sdes	name##_RB_INSERT_COLOR(head, elm);				\
60898679Sdes	return (NULL);							\
60998679Sdes}									\
61098679Sdes									\
61198679Sdes/* Finds the node with the same key as elm */				\
61298679Sdesstruct type *								\
61398679Sdesname##_RB_FIND(struct name *head, struct type *elm)			\
61498679Sdes{									\
61598679Sdes	struct type *tmp = RB_ROOT(head);				\
61698679Sdes	int comp;							\
61798679Sdes	while (tmp) {							\
61898679Sdes		comp = cmp(elm, tmp);					\
61998679Sdes		if (comp < 0)						\
62098679Sdes			tmp = RB_LEFT(tmp, field);			\
62198679Sdes		else if (comp > 0)					\
62298679Sdes			tmp = RB_RIGHT(tmp, field);			\
62398679Sdes		else							\
62498679Sdes			return (tmp);					\
62598679Sdes	}								\
62698679Sdes	return (NULL);							\
62798679Sdes}									\
62898679Sdes									\
629127403Sdes/* ARGSUSED */								\
63098679Sdesstruct type *								\
63198679Sdesname##_RB_NEXT(struct name *head, struct type *elm)			\
63298679Sdes{									\
63398679Sdes	if (RB_RIGHT(elm, field)) {					\
63498679Sdes		elm = RB_RIGHT(elm, field);				\
63598679Sdes		while (RB_LEFT(elm, field))				\
63698679Sdes			elm = RB_LEFT(elm, field);			\
63798679Sdes	} else {							\
63898679Sdes		if (RB_PARENT(elm, field) &&				\
63998679Sdes		    (elm == RB_LEFT(RB_PARENT(elm, field), field)))	\
64098679Sdes			elm = RB_PARENT(elm, field);			\
64198679Sdes		else {							\
64298679Sdes			while (RB_PARENT(elm, field) &&			\
64398679Sdes			    (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
64498679Sdes				elm = RB_PARENT(elm, field);		\
64598679Sdes			elm = RB_PARENT(elm, field);			\
64698679Sdes		}							\
64798679Sdes	}								\
64898679Sdes	return (elm);							\
64998679Sdes}									\
65098679Sdes									\
65198679Sdesstruct type *								\
65298679Sdesname##_RB_MINMAX(struct name *head, int val)				\
65398679Sdes{									\
65498679Sdes	struct type *tmp = RB_ROOT(head);				\
65598679Sdes	struct type *parent = NULL;					\
65698679Sdes	while (tmp) {							\
65798679Sdes		parent = tmp;						\
65898679Sdes		if (val < 0)						\
65998679Sdes			tmp = RB_LEFT(tmp, field);			\
66098679Sdes		else							\
66198679Sdes			tmp = RB_RIGHT(tmp, field);			\
66298679Sdes	}								\
66398679Sdes	return (parent);						\
66498679Sdes}
66598679Sdes
66698679Sdes#define RB_NEGINF	-1
66798679Sdes#define RB_INF	1
66898679Sdes
66998679Sdes#define RB_INSERT(name, x, y)	name##_RB_INSERT(x, y)
67098679Sdes#define RB_REMOVE(name, x, y)	name##_RB_REMOVE(x, y)
67198679Sdes#define RB_FIND(name, x, y)	name##_RB_FIND(x, y)
67298679Sdes#define RB_NEXT(name, x, y)	name##_RB_NEXT(x, y)
67398679Sdes#define RB_MIN(name, x)		name##_RB_MINMAX(x, RB_NEGINF)
67498679Sdes#define RB_MAX(name, x)		name##_RB_MINMAX(x, RB_INF)
67598679Sdes
67698679Sdes#define RB_FOREACH(x, name, head)					\
67798679Sdes	for ((x) = RB_MIN(name, head);					\
67898679Sdes	     (x) != NULL;						\
67998679Sdes	     (x) = name##_RB_NEXT(head, x))
68098679Sdes
68198679Sdes#endif	/* _SYS_TREE_H_ */
682