Blame include/sys/tree.h

Kylie McClain b61f59
/*	$NetBSD: tree.h,v 1.20 2013/09/14 13:20:45 joerg Exp $	*/
Kylie McClain b61f59
/*	$OpenBSD: tree.h,v 1.13 2011/07/09 00:19:45 pirofti Exp $	*/
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
Kylie McClain b61f59
 * All rights reserved.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * Redistribution and use in source and binary forms, with or without
Kylie McClain b61f59
 * modification, are permitted provided that the following conditions
Kylie McClain b61f59
 * are met:
Kylie McClain b61f59
 * 1. Redistributions of source code must retain the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer.
Kylie McClain b61f59
 * 2. Redistributions in binary form must reproduce the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer in the
Kylie McClain b61f59
 *    documentation and/or other materials provided with the distribution.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Kylie McClain b61f59
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Kylie McClain b61f59
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Kylie McClain b61f59
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Kylie McClain b61f59
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Kylie McClain b61f59
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Kylie McClain b61f59
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Kylie McClain b61f59
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kylie McClain b61f59
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Kylie McClain b61f59
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
#ifndef	_SYS_TREE_H_
Kylie McClain b61f59
#define	_SYS_TREE_H_
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * This file defines data structures for different types of trees:
Kylie McClain b61f59
 * splay trees and red-black trees.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A splay tree is a self-organizing data structure.  Every operation
Kylie McClain b61f59
 * on the tree causes a splay to happen.  The splay moves the requested
Kylie McClain b61f59
 * node to the root of the tree and partly rebalances it.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * This has the benefit that request locality causes faster lookups as
Kylie McClain b61f59
 * the requested nodes move to the top of the tree.  On the other hand,
Kylie McClain b61f59
 * every lookup causes memory writes.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * The Balance Theorem bounds the total access time for m operations
Kylie McClain b61f59
 * and n inserts on an initially empty tree as O((m + n)lg n).  The
Kylie McClain b61f59
 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A red-black tree is a binary search tree with the node color as an
Kylie McClain b61f59
 * extra attribute.  It fulfills a set of conditions:
Kylie McClain b61f59
 *	- every search path from the root to a leaf consists of the
Kylie McClain b61f59
 *	  same number of black nodes,
Kylie McClain b61f59
 *	- each red node (except for the root) has a black parent,
Kylie McClain b61f59
 *	- each leaf node is black.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * Every operation on a red-black tree is bounded as O(lg n).
Kylie McClain b61f59
 * The maximum height of a red-black tree is 2lg (n+1).
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_HEAD(name, type)						\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *sph_root; /* root of the tree */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_INITIALIZER(root)						\
Kylie McClain b61f59
	{ NULL }
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_INIT(root) do {						\
Kylie McClain b61f59
	(root)->sph_root = NULL;					\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *spe_left; /* left element */			\
Kylie McClain b61f59
	struct type *spe_right; /* right element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_LEFT(elm, field)		(elm)->field.spe_left
Kylie McClain b61f59
#define SPLAY_RIGHT(elm, field)		(elm)->field.spe_right
Kylie McClain b61f59
#define SPLAY_ROOT(head)		(head)->sph_root
Kylie McClain b61f59
#define SPLAY_EMPTY(head)		(SPLAY_ROOT(head) == NULL)
Kylie McClain b61f59
Kylie McClain b61f59
/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
Kylie McClain b61f59
#define SPLAY_ROTATE_RIGHT(head, tmp, field) do {			\
Kylie McClain b61f59
	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field);	\
Kylie McClain b61f59
	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
Kylie McClain b61f59
	(head)->sph_root = tmp;						\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_ROTATE_LEFT(head, tmp, field) do {			\
Kylie McClain b61f59
	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field);	\
Kylie McClain b61f59
	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
Kylie McClain b61f59
	(head)->sph_root = tmp;						\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_LINKLEFT(head, tmp, field) do {				\
Kylie McClain b61f59
	SPLAY_LEFT(tmp, field) = (head)->sph_root;			\
Kylie McClain b61f59
	tmp = (head)->sph_root;						\
Kylie McClain b61f59
	(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);		\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_LINKRIGHT(head, tmp, field) do {				\
Kylie McClain b61f59
	SPLAY_RIGHT(tmp, field) = (head)->sph_root;			\
Kylie McClain b61f59
	tmp = (head)->sph_root;						\
Kylie McClain b61f59
	(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);	\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_ASSEMBLE(head, node, left, right, field) do {		\
Kylie McClain b61f59
	SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field);	\
Kylie McClain b61f59
	SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
Kylie McClain b61f59
	SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field);	\
Kylie McClain b61f59
	SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field);	\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
/* Generates prototypes and inline functions */
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_PROTOTYPE(name, type, field, cmp)				\
Kylie McClain b61f59
void name##_SPLAY(struct name *, struct type *);			\
Kylie McClain b61f59
void name##_SPLAY_MINMAX(struct name *, int);				\
Kylie McClain b61f59
struct type *name##_SPLAY_INSERT(struct name *, struct type *);		\
Kylie McClain b61f59
struct type *name##_SPLAY_REMOVE(struct name *, struct type *);		\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* Finds the node with the same key as elm */				\
Kylie McClain b61f59
static __inline struct type *						\
Kylie McClain b61f59
name##_SPLAY_FIND(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	if (SPLAY_EMPTY(head))						\
Kylie McClain b61f59
		return(NULL);						\
Kylie McClain b61f59
	name##_SPLAY(head, elm);					\
Kylie McClain b61f59
	if ((cmp)(elm, (head)->sph_root) == 0)				\
Kylie McClain b61f59
		return (head->sph_root);				\
Kylie McClain b61f59
	return (NULL);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
static __inline __unused struct type *					\
Kylie McClain b61f59
name##_SPLAY_NEXT(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	name##_SPLAY(head, elm);					\
Kylie McClain b61f59
	if (SPLAY_RIGHT(elm, field) != NULL) {				\
Kylie McClain b61f59
		elm = SPLAY_RIGHT(elm, field);				\
Kylie McClain b61f59
		while (SPLAY_LEFT(elm, field) != NULL) {		\
Kylie McClain b61f59
			elm = SPLAY_LEFT(elm, field);			\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	} else								\
Kylie McClain b61f59
		elm = NULL;						\
Kylie McClain b61f59
	return (elm);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
static __unused __inline struct type *					\
Kylie McClain b61f59
name##_SPLAY_MIN_MAX(struct name *head, int val)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	name##_SPLAY_MINMAX(head, val);					\
Kylie McClain b61f59
        return (SPLAY_ROOT(head));					\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/* Main splay operation.
Kylie McClain b61f59
 * Moves node close to the key of elm to top
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define SPLAY_GENERATE(name, type, field, cmp)				\
Kylie McClain b61f59
struct type *								\
Kylie McClain b61f59
name##_SPLAY_INSERT(struct name *head, struct type *elm)		\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
    if (SPLAY_EMPTY(head)) {						\
Kylie McClain b61f59
	    SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL;	\
Kylie McClain b61f59
    } else {								\
Kylie McClain b61f59
	    int __comp;							\
Kylie McClain b61f59
	    name##_SPLAY(head, elm);					\
Kylie McClain b61f59
	    __comp = (cmp)(elm, (head)->sph_root);			\
Kylie McClain b61f59
	    if(__comp < 0) {						\
Kylie McClain b61f59
		    SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
Kylie McClain b61f59
		    SPLAY_RIGHT(elm, field) = (head)->sph_root;		\
Kylie McClain b61f59
		    SPLAY_LEFT((head)->sph_root, field) = NULL;		\
Kylie McClain b61f59
	    } else if (__comp > 0) {					\
Kylie McClain b61f59
		    SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
Kylie McClain b61f59
		    SPLAY_LEFT(elm, field) = (head)->sph_root;		\
Kylie McClain b61f59
		    SPLAY_RIGHT((head)->sph_root, field) = NULL;	\
Kylie McClain b61f59
	    } else							\
Kylie McClain b61f59
		    return ((head)->sph_root);				\
Kylie McClain b61f59
    }									\
Kylie McClain b61f59
    (head)->sph_root = (elm);						\
Kylie McClain b61f59
    return (NULL);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
struct type *								\
Kylie McClain b61f59
name##_SPLAY_REMOVE(struct name *head, struct type *elm)		\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *__tmp;						\
Kylie McClain b61f59
	if (SPLAY_EMPTY(head))						\
Kylie McClain b61f59
		return (NULL);						\
Kylie McClain b61f59
	name##_SPLAY(head, elm);					\
Kylie McClain b61f59
	if ((cmp)(elm, (head)->sph_root) == 0) {			\
Kylie McClain b61f59
		if (SPLAY_LEFT((head)->sph_root, field) == NULL) {	\
Kylie McClain b61f59
			(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
Kylie McClain b61f59
		} else {						\
Kylie McClain b61f59
			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
Kylie McClain b61f59
			(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
Kylie McClain b61f59
			name##_SPLAY(head, elm);			\
Kylie McClain b61f59
			SPLAY_RIGHT((head)->sph_root, field) = __tmp;	\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
		return (elm);						\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (NULL);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
void									\
Kylie McClain b61f59
name##_SPLAY(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type __node, *__left, *__right, *__tmp;			\
Kylie McClain b61f59
	int __comp;							\
Kylie McClain b61f59
\
Kylie McClain b61f59
	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
Kylie McClain b61f59
	__left = __right = &__node;					\
Kylie McClain b61f59
\
Kylie McClain b61f59
	while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) {		\
Kylie McClain b61f59
		if (__comp < 0) {					\
Kylie McClain b61f59
			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
Kylie McClain b61f59
			if (__tmp == NULL)				\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			if ((cmp)(elm, __tmp) < 0){			\
Kylie McClain b61f59
				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
Kylie McClain b61f59
				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
Kylie McClain b61f59
					break;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			SPLAY_LINKLEFT(head, __right, field);		\
Kylie McClain b61f59
		} else if (__comp > 0) {				\
Kylie McClain b61f59
			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
Kylie McClain b61f59
			if (__tmp == NULL)				\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			if ((cmp)(elm, __tmp) > 0){			\
Kylie McClain b61f59
				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
Kylie McClain b61f59
				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
Kylie McClain b61f59
					break;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			SPLAY_LINKRIGHT(head, __left, field);		\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* Splay with either the minimum or the maximum element			\
Kylie McClain b61f59
 * Used to find minimum or maximum element in tree.			\
Kylie McClain b61f59
 */									\
Kylie McClain b61f59
void name##_SPLAY_MINMAX(struct name *head, int __comp) \
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type __node, *__left, *__right, *__tmp;			\
Kylie McClain b61f59
\
Kylie McClain b61f59
	SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
Kylie McClain b61f59
	__left = __right = &__node;					\
Kylie McClain b61f59
\
Kylie McClain b61f59
	while (1) {							\
Kylie McClain b61f59
		if (__comp < 0) {					\
Kylie McClain b61f59
			__tmp = SPLAY_LEFT((head)->sph_root, field);	\
Kylie McClain b61f59
			if (__tmp == NULL)				\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			if (__comp < 0){				\
Kylie McClain b61f59
				SPLAY_ROTATE_RIGHT(head, __tmp, field);	\
Kylie McClain b61f59
				if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
Kylie McClain b61f59
					break;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			SPLAY_LINKLEFT(head, __right, field);		\
Kylie McClain b61f59
		} else if (__comp > 0) {				\
Kylie McClain b61f59
			__tmp = SPLAY_RIGHT((head)->sph_root, field);	\
Kylie McClain b61f59
			if (__tmp == NULL)				\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			if (__comp > 0) {				\
Kylie McClain b61f59
				SPLAY_ROTATE_LEFT(head, __tmp, field);	\
Kylie McClain b61f59
				if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
Kylie McClain b61f59
					break;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			SPLAY_LINKRIGHT(head, __left, field);		\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	SPLAY_ASSEMBLE(head, &__node, __left, __right, field);		\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_NEGINF	-1
Kylie McClain b61f59
#define SPLAY_INF	1
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_INSERT(name, x, y)	name##_SPLAY_INSERT(x, y)
Kylie McClain b61f59
#define SPLAY_REMOVE(name, x, y)	name##_SPLAY_REMOVE(x, y)
Kylie McClain b61f59
#define SPLAY_FIND(name, x, y)		name##_SPLAY_FIND(x, y)
Kylie McClain b61f59
#define SPLAY_NEXT(name, x, y)		name##_SPLAY_NEXT(x, y)
Kylie McClain b61f59
#define SPLAY_MIN(name, x)		(SPLAY_EMPTY(x) ? NULL	\
Kylie McClain b61f59
					: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
Kylie McClain b61f59
#define SPLAY_MAX(name, x)		(SPLAY_EMPTY(x) ? NULL	\
Kylie McClain b61f59
					: name##_SPLAY_MIN_MAX(x, SPLAY_INF))
Kylie McClain b61f59
Kylie McClain b61f59
#define SPLAY_FOREACH(x, name, head)					\
Kylie McClain b61f59
	for ((x) = SPLAY_MIN(name, head);				\
Kylie McClain b61f59
	     (x) != NULL;						\
Kylie McClain b61f59
	     (x) = SPLAY_NEXT(name, head, x))
Kylie McClain b61f59
Kylie McClain b61f59
/* Macros that define a red-black tree */
Kylie McClain b61f59
#define RB_HEAD(name, type)						\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *rbh_root; /* root of the tree */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_INITIALIZER(root)						\
Kylie McClain b61f59
	{ NULL }
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_INIT(root) do {						\
Kylie McClain b61f59
	(root)->rbh_root = NULL;					\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_BLACK	0
Kylie McClain b61f59
#define RB_RED		1
Kylie McClain b61f59
#define RB_ENTRY(type)							\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *rbe_left;		/* left element */		\
Kylie McClain b61f59
	struct type *rbe_right;		/* right element */		\
Kylie McClain b61f59
	struct type *rbe_parent;	/* parent element */		\
Kylie McClain b61f59
	int rbe_color;			/* node color */		\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_LEFT(elm, field)		(elm)->field.rbe_left
Kylie McClain b61f59
#define RB_RIGHT(elm, field)		(elm)->field.rbe_right
Kylie McClain b61f59
#define RB_PARENT(elm, field)		(elm)->field.rbe_parent
Kylie McClain b61f59
#define RB_COLOR(elm, field)		(elm)->field.rbe_color
Kylie McClain b61f59
#define RB_ROOT(head)			(head)->rbh_root
Kylie McClain b61f59
#define RB_EMPTY(head)			(RB_ROOT(head) == NULL)
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_SET(elm, parent, field) do {					\
Kylie McClain b61f59
	RB_PARENT(elm, field) = parent;					\
Kylie McClain b61f59
	RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL;		\
Kylie McClain b61f59
	RB_COLOR(elm, field) = RB_RED;					\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_SET_BLACKRED(black, red, field) do {				\
Kylie McClain b61f59
	RB_COLOR(black, field) = RB_BLACK;				\
Kylie McClain b61f59
	RB_COLOR(red, field) = RB_RED;					\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#ifndef RB_AUGMENT
Kylie McClain b61f59
#define RB_AUGMENT(x)	do {} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_ROTATE_LEFT(head, elm, tmp, field) do {			\
Kylie McClain b61f59
	(tmp) = RB_RIGHT(elm, field);					\
Kylie McClain b61f59
	if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) {	\
Kylie McClain b61f59
		RB_PARENT(RB_LEFT(tmp, field), field) = (elm);		\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	RB_AUGMENT(elm);						\
Kylie McClain b61f59
	if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {	\
Kylie McClain b61f59
		if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))	\
Kylie McClain b61f59
			RB_LEFT(RB_PARENT(elm, field), field) = (tmp);	\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			RB_RIGHT(RB_PARENT(elm, field), field) = (tmp);	\
Kylie McClain b61f59
	} else								\
Kylie McClain b61f59
		(head)->rbh_root = (tmp);				\
Kylie McClain b61f59
	RB_LEFT(tmp, field) = (elm);					\
Kylie McClain b61f59
	RB_PARENT(elm, field) = (tmp);					\
Kylie McClain b61f59
	RB_AUGMENT(tmp);						\
Kylie McClain b61f59
	if ((RB_PARENT(tmp, field)))					\
Kylie McClain b61f59
		RB_AUGMENT(RB_PARENT(tmp, field));			\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_ROTATE_RIGHT(head, elm, tmp, field) do {			\
Kylie McClain b61f59
	(tmp) = RB_LEFT(elm, field);					\
Kylie McClain b61f59
	if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) {	\
Kylie McClain b61f59
		RB_PARENT(RB_RIGHT(tmp, field), field) = (elm);		\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	RB_AUGMENT(elm);						\
Kylie McClain b61f59
	if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) {	\
Kylie McClain b61f59
		if ((elm) == RB_LEFT(RB_PARENT(elm, field), field))	\
Kylie McClain b61f59
			RB_LEFT(RB_PARENT(elm, field), field) = (tmp);	\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			RB_RIGHT(RB_PARENT(elm, field), field) = (tmp);	\
Kylie McClain b61f59
	} else								\
Kylie McClain b61f59
		(head)->rbh_root = (tmp);				\
Kylie McClain b61f59
	RB_RIGHT(tmp, field) = (elm);					\
Kylie McClain b61f59
	RB_PARENT(elm, field) = (tmp);					\
Kylie McClain b61f59
	RB_AUGMENT(tmp);						\
Kylie McClain b61f59
	if ((RB_PARENT(tmp, field)))					\
Kylie McClain b61f59
		RB_AUGMENT(RB_PARENT(tmp, field));			\
Kylie McClain b61f59
} while (/*CONSTCOND*/ 0)
Kylie McClain b61f59
Kylie McClain b61f59
/* Generates prototypes and inline functions */
Kylie McClain b61f59
#define RB_PROTOTYPE(name, type, field, cmp)				\
Kylie McClain b61f59
	RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
Kylie McClain b61f59
#define	RB_PROTOTYPE_STATIC(name, type, field, cmp)			\
Kylie McClain b61f59
	RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
Kylie McClain b61f59
#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr)		\
Kylie McClain b61f59
attr void name##_RB_INSERT_COLOR(struct name *, struct type *);		\
Kylie McClain b61f59
attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
Kylie McClain b61f59
attr struct type *name##_RB_REMOVE(struct name *, struct type *);	\
Kylie McClain b61f59
attr struct type *name##_RB_INSERT(struct name *, struct type *);	\
Kylie McClain b61f59
attr struct type *name##_RB_FIND(struct name *, struct type *);		\
Kylie McClain b61f59
attr struct type *name##_RB_NFIND(struct name *, struct type *);	\
Kylie McClain b61f59
attr struct type *name##_RB_NEXT(struct type *);			\
Kylie McClain b61f59
attr struct type *name##_RB_PREV(struct type *);			\
Kylie McClain b61f59
attr struct type *name##_RB_MINMAX(struct name *, int);			\
Kylie McClain b61f59
									\
Kylie McClain b61f59
Kylie McClain b61f59
/* Main rb operation.
Kylie McClain b61f59
 * Moves node close to the key of elm to top
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	RB_GENERATE(name, type, field, cmp)				\
Kylie McClain b61f59
	RB_GENERATE_INTERNAL(name, type, field, cmp,)
Kylie McClain b61f59
#define	RB_GENERATE_STATIC(name, type, field, cmp)			\
Kylie McClain b61f59
	RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
Kylie McClain b61f59
#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr)		\
Kylie McClain b61f59
attr void								\
Kylie McClain b61f59
name##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *parent, *gparent, *tmp;				\
Kylie McClain b61f59
	while ((parent = RB_PARENT(elm, field)) != NULL &&		\
Kylie McClain b61f59
	    RB_COLOR(parent, field) == RB_RED) {			\
Kylie McClain b61f59
		gparent = RB_PARENT(parent, field);			\
Kylie McClain b61f59
		if (parent == RB_LEFT(gparent, field)) {		\
Kylie McClain b61f59
			tmp = RB_RIGHT(gparent, field);			\
Kylie McClain b61f59
			if (tmp && RB_COLOR(tmp, field) == RB_RED) {	\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_BLACK;	\
Kylie McClain b61f59
				RB_SET_BLACKRED(parent, gparent, field);\
Kylie McClain b61f59
				elm = gparent;				\
Kylie McClain b61f59
				continue;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			if (RB_RIGHT(parent, field) == elm) {		\
Kylie McClain b61f59
				RB_ROTATE_LEFT(head, parent, tmp, field);\
Kylie McClain b61f59
				tmp = parent;				\
Kylie McClain b61f59
				parent = elm;				\
Kylie McClain b61f59
				elm = tmp;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			RB_SET_BLACKRED(parent, gparent, field);	\
Kylie McClain b61f59
			RB_ROTATE_RIGHT(head, gparent, tmp, field);	\
Kylie McClain b61f59
		} else {						\
Kylie McClain b61f59
			tmp = RB_LEFT(gparent, field);			\
Kylie McClain b61f59
			if (tmp && RB_COLOR(tmp, field) == RB_RED) {	\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_BLACK;	\
Kylie McClain b61f59
				RB_SET_BLACKRED(parent, gparent, field);\
Kylie McClain b61f59
				elm = gparent;				\
Kylie McClain b61f59
				continue;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			if (RB_LEFT(parent, field) == elm) {		\
Kylie McClain b61f59
				RB_ROTATE_RIGHT(head, parent, tmp, field);\
Kylie McClain b61f59
				tmp = parent;				\
Kylie McClain b61f59
				parent = elm;				\
Kylie McClain b61f59
				elm = tmp;				\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			RB_SET_BLACKRED(parent, gparent, field);	\
Kylie McClain b61f59
			RB_ROTATE_LEFT(head, gparent, tmp, field);	\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	RB_COLOR(head->rbh_root, field) = RB_BLACK;			\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
attr void								\
Kylie McClain b61f59
name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *tmp;						\
Kylie McClain b61f59
	while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) &&	\
Kylie McClain b61f59
	    elm != RB_ROOT(head)) {					\
Kylie McClain b61f59
		if (RB_LEFT(parent, field) == elm) {			\
Kylie McClain b61f59
			tmp = RB_RIGHT(parent, field);			\
Kylie McClain b61f59
			if (RB_COLOR(tmp, field) == RB_RED) {		\
Kylie McClain b61f59
				RB_SET_BLACKRED(tmp, parent, field);	\
Kylie McClain b61f59
				RB_ROTATE_LEFT(head, parent, tmp, field);\
Kylie McClain b61f59
				tmp = RB_RIGHT(parent, field);		\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			if ((RB_LEFT(tmp, field) == NULL ||		\
Kylie McClain b61f59
			    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
Kylie McClain b61f59
			    (RB_RIGHT(tmp, field) == NULL ||		\
Kylie McClain b61f59
			    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_RED;		\
Kylie McClain b61f59
				elm = parent;				\
Kylie McClain b61f59
				parent = RB_PARENT(elm, field);		\
Kylie McClain b61f59
			} else {					\
Kylie McClain b61f59
				if (RB_RIGHT(tmp, field) == NULL ||	\
Kylie McClain b61f59
				    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
Kylie McClain b61f59
					struct type *oleft;		\
Kylie McClain b61f59
					if ((oleft = RB_LEFT(tmp, field)) \
Kylie McClain b61f59
					    != NULL)			\
Kylie McClain b61f59
						RB_COLOR(oleft, field) = RB_BLACK;\
Kylie McClain b61f59
					RB_COLOR(tmp, field) = RB_RED;	\
Kylie McClain b61f59
					RB_ROTATE_RIGHT(head, tmp, oleft, field);\
Kylie McClain b61f59
					tmp = RB_RIGHT(parent, field);	\
Kylie McClain b61f59
				}					\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
Kylie McClain b61f59
				RB_COLOR(parent, field) = RB_BLACK;	\
Kylie McClain b61f59
				if (RB_RIGHT(tmp, field))		\
Kylie McClain b61f59
					RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
Kylie McClain b61f59
				RB_ROTATE_LEFT(head, parent, tmp, field);\
Kylie McClain b61f59
				elm = RB_ROOT(head);			\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
		} else {						\
Kylie McClain b61f59
			tmp = RB_LEFT(parent, field);			\
Kylie McClain b61f59
			if (RB_COLOR(tmp, field) == RB_RED) {		\
Kylie McClain b61f59
				RB_SET_BLACKRED(tmp, parent, field);	\
Kylie McClain b61f59
				RB_ROTATE_RIGHT(head, parent, tmp, field);\
Kylie McClain b61f59
				tmp = RB_LEFT(parent, field);		\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
			if ((RB_LEFT(tmp, field) == NULL ||		\
Kylie McClain b61f59
			    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
Kylie McClain b61f59
			    (RB_RIGHT(tmp, field) == NULL ||		\
Kylie McClain b61f59
			    RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_RED;		\
Kylie McClain b61f59
				elm = parent;				\
Kylie McClain b61f59
				parent = RB_PARENT(elm, field);		\
Kylie McClain b61f59
			} else {					\
Kylie McClain b61f59
				if (RB_LEFT(tmp, field) == NULL ||	\
Kylie McClain b61f59
				    RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
Kylie McClain b61f59
					struct type *oright;		\
Kylie McClain b61f59
					if ((oright = RB_RIGHT(tmp, field)) \
Kylie McClain b61f59
					    != NULL)			\
Kylie McClain b61f59
						RB_COLOR(oright, field) = RB_BLACK;\
Kylie McClain b61f59
					RB_COLOR(tmp, field) = RB_RED;	\
Kylie McClain b61f59
					RB_ROTATE_LEFT(head, tmp, oright, field);\
Kylie McClain b61f59
					tmp = RB_LEFT(parent, field);	\
Kylie McClain b61f59
				}					\
Kylie McClain b61f59
				RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
Kylie McClain b61f59
				RB_COLOR(parent, field) = RB_BLACK;	\
Kylie McClain b61f59
				if (RB_LEFT(tmp, field))		\
Kylie McClain b61f59
					RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
Kylie McClain b61f59
				RB_ROTATE_RIGHT(head, parent, tmp, field);\
Kylie McClain b61f59
				elm = RB_ROOT(head);			\
Kylie McClain b61f59
				break;					\
Kylie McClain b61f59
			}						\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	if (elm)							\
Kylie McClain b61f59
		RB_COLOR(elm, field) = RB_BLACK;			\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_REMOVE(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *child, *parent, *old = elm;			\
Kylie McClain b61f59
	int color;							\
Kylie McClain b61f59
	if (RB_LEFT(elm, field) == NULL)				\
Kylie McClain b61f59
		child = RB_RIGHT(elm, field);				\
Kylie McClain b61f59
	else if (RB_RIGHT(elm, field) == NULL)				\
Kylie McClain b61f59
		child = RB_LEFT(elm, field);				\
Kylie McClain b61f59
	else {								\
Kylie McClain b61f59
		struct type *left;					\
Kylie McClain b61f59
		elm = RB_RIGHT(elm, field);				\
Kylie McClain b61f59
		while ((left = RB_LEFT(elm, field)) != NULL)		\
Kylie McClain b61f59
			elm = left;					\
Kylie McClain b61f59
		child = RB_RIGHT(elm, field);				\
Kylie McClain b61f59
		parent = RB_PARENT(elm, field);				\
Kylie McClain b61f59
		color = RB_COLOR(elm, field);				\
Kylie McClain b61f59
		if (child)						\
Kylie McClain b61f59
			RB_PARENT(child, field) = parent;		\
Kylie McClain b61f59
		if (parent) {						\
Kylie McClain b61f59
			if (RB_LEFT(parent, field) == elm)		\
Kylie McClain b61f59
				RB_LEFT(parent, field) = child;		\
Kylie McClain b61f59
			else						\
Kylie McClain b61f59
				RB_RIGHT(parent, field) = child;	\
Kylie McClain b61f59
			RB_AUGMENT(parent);				\
Kylie McClain b61f59
		} else							\
Kylie McClain b61f59
			RB_ROOT(head) = child;				\
Kylie McClain b61f59
		if (RB_PARENT(elm, field) == old)			\
Kylie McClain b61f59
			parent = elm;					\
Kylie McClain b61f59
		(elm)->field = (old)->field;				\
Kylie McClain b61f59
		if (RB_PARENT(old, field)) {				\
Kylie McClain b61f59
			if (RB_LEFT(RB_PARENT(old, field), field) == old)\
Kylie McClain b61f59
				RB_LEFT(RB_PARENT(old, field), field) = elm;\
Kylie McClain b61f59
			else						\
Kylie McClain b61f59
				RB_RIGHT(RB_PARENT(old, field), field) = elm;\
Kylie McClain b61f59
			RB_AUGMENT(RB_PARENT(old, field));		\
Kylie McClain b61f59
		} else							\
Kylie McClain b61f59
			RB_ROOT(head) = elm;				\
Kylie McClain b61f59
		RB_PARENT(RB_LEFT(old, field), field) = elm;		\
Kylie McClain b61f59
		if (RB_RIGHT(old, field))				\
Kylie McClain b61f59
			RB_PARENT(RB_RIGHT(old, field), field) = elm;	\
Kylie McClain b61f59
		if (parent) {						\
Kylie McClain b61f59
			left = parent;					\
Kylie McClain b61f59
			do {						\
Kylie McClain b61f59
				RB_AUGMENT(left);			\
Kylie McClain b61f59
			} while ((left = RB_PARENT(left, field)) != NULL); \
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
		goto color;						\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	parent = RB_PARENT(elm, field);					\
Kylie McClain b61f59
	color = RB_COLOR(elm, field);					\
Kylie McClain b61f59
	if (child)							\
Kylie McClain b61f59
		RB_PARENT(child, field) = parent;			\
Kylie McClain b61f59
	if (parent) {							\
Kylie McClain b61f59
		if (RB_LEFT(parent, field) == elm)			\
Kylie McClain b61f59
			RB_LEFT(parent, field) = child;			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			RB_RIGHT(parent, field) = child;		\
Kylie McClain b61f59
		RB_AUGMENT(parent);					\
Kylie McClain b61f59
	} else								\
Kylie McClain b61f59
		RB_ROOT(head) = child;					\
Kylie McClain b61f59
color:									\
Kylie McClain b61f59
	if (color == RB_BLACK)						\
Kylie McClain b61f59
		name##_RB_REMOVE_COLOR(head, parent, child);		\
Kylie McClain b61f59
	return (old);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* Inserts a node into the RB tree */					\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_INSERT(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *tmp;						\
Kylie McClain b61f59
	struct type *parent = NULL;					\
Kylie McClain b61f59
	int comp = 0;							\
Kylie McClain b61f59
	tmp = RB_ROOT(head);						\
Kylie McClain b61f59
	while (tmp) {							\
Kylie McClain b61f59
		parent = tmp;						\
Kylie McClain b61f59
		comp = (cmp)(elm, parent);				\
Kylie McClain b61f59
		if (comp < 0)						\
Kylie McClain b61f59
			tmp = RB_LEFT(tmp, field);			\
Kylie McClain b61f59
		else if (comp > 0)					\
Kylie McClain b61f59
			tmp = RB_RIGHT(tmp, field);			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			return (tmp);					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	RB_SET(elm, parent, field);					\
Kylie McClain b61f59
	if (parent != NULL) {						\
Kylie McClain b61f59
		if (comp < 0)						\
Kylie McClain b61f59
			RB_LEFT(parent, field) = elm;			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			RB_RIGHT(parent, field) = elm;			\
Kylie McClain b61f59
		RB_AUGMENT(parent);					\
Kylie McClain b61f59
	} else								\
Kylie McClain b61f59
		RB_ROOT(head) = elm;					\
Kylie McClain b61f59
	name##_RB_INSERT_COLOR(head, elm);				\
Kylie McClain b61f59
	return (NULL);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* Finds the node with the same key as elm */				\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_FIND(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *tmp = RB_ROOT(head);				\
Kylie McClain b61f59
	int comp;							\
Kylie McClain b61f59
	while (tmp) {							\
Kylie McClain b61f59
		comp = cmp(elm, tmp);					\
Kylie McClain b61f59
		if (comp < 0)						\
Kylie McClain b61f59
			tmp = RB_LEFT(tmp, field);			\
Kylie McClain b61f59
		else if (comp > 0)					\
Kylie McClain b61f59
			tmp = RB_RIGHT(tmp, field);			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			return (tmp);					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (NULL);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* Finds the first node greater than or equal to the search key */	\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_NFIND(struct name *head, struct type *elm)			\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *tmp = RB_ROOT(head);				\
Kylie McClain b61f59
	struct type *res = NULL;					\
Kylie McClain b61f59
	int comp;							\
Kylie McClain b61f59
	while (tmp) {							\
Kylie McClain b61f59
		comp = cmp(elm, tmp);					\
Kylie McClain b61f59
		if (comp < 0) {						\
Kylie McClain b61f59
			res = tmp;					\
Kylie McClain b61f59
			tmp = RB_LEFT(tmp, field);			\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
		else if (comp > 0)					\
Kylie McClain b61f59
			tmp = RB_RIGHT(tmp, field);			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			return (tmp);					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (res);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* ARGSUSED */								\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_NEXT(struct type *elm)					\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	if (RB_RIGHT(elm, field)) {					\
Kylie McClain b61f59
		elm = RB_RIGHT(elm, field);				\
Kylie McClain b61f59
		while (RB_LEFT(elm, field))				\
Kylie McClain b61f59
			elm = RB_LEFT(elm, field);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		if (RB_PARENT(elm, field) &&				\
Kylie McClain b61f59
		    (elm == RB_LEFT(RB_PARENT(elm, field), field)))	\
Kylie McClain b61f59
			elm = RB_PARENT(elm, field);			\
Kylie McClain b61f59
		else {							\
Kylie McClain b61f59
			while (RB_PARENT(elm, field) &&			\
Kylie McClain b61f59
			    (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
Kylie McClain b61f59
				elm = RB_PARENT(elm, field);		\
Kylie McClain b61f59
			elm = RB_PARENT(elm, field);			\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (elm);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
/* ARGSUSED */								\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_PREV(struct type *elm)					\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	if (RB_LEFT(elm, field)) {					\
Kylie McClain b61f59
		elm = RB_LEFT(elm, field);				\
Kylie McClain b61f59
		while (RB_RIGHT(elm, field))				\
Kylie McClain b61f59
			elm = RB_RIGHT(elm, field);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		if (RB_PARENT(elm, field) &&				\
Kylie McClain b61f59
		    (elm == RB_RIGHT(RB_PARENT(elm, field), field)))	\
Kylie McClain b61f59
			elm = RB_PARENT(elm, field);			\
Kylie McClain b61f59
		else {							\
Kylie McClain b61f59
			while (RB_PARENT(elm, field) &&			\
Kylie McClain b61f59
			    (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
Kylie McClain b61f59
				elm = RB_PARENT(elm, field);		\
Kylie McClain b61f59
			elm = RB_PARENT(elm, field);			\
Kylie McClain b61f59
		}							\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (elm);							\
Kylie McClain b61f59
}									\
Kylie McClain b61f59
									\
Kylie McClain b61f59
attr struct type *							\
Kylie McClain b61f59
name##_RB_MINMAX(struct name *head, int val)				\
Kylie McClain b61f59
{									\
Kylie McClain b61f59
	struct type *tmp = RB_ROOT(head);				\
Kylie McClain b61f59
	struct type *parent = NULL;					\
Kylie McClain b61f59
	while (tmp) {							\
Kylie McClain b61f59
		parent = tmp;						\
Kylie McClain b61f59
		if (val < 0)						\
Kylie McClain b61f59
			tmp = RB_LEFT(tmp, field);			\
Kylie McClain b61f59
		else							\
Kylie McClain b61f59
			tmp = RB_RIGHT(tmp, field);			\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	return (parent);						\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_NEGINF	-1
Kylie McClain b61f59
#define RB_INF	1
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_INSERT(name, x, y)	name##_RB_INSERT(x, y)
Kylie McClain b61f59
#define RB_REMOVE(name, x, y)	name##_RB_REMOVE(x, y)
Kylie McClain b61f59
#define RB_FIND(name, x, y)	name##_RB_FIND(x, y)
Kylie McClain b61f59
#define RB_NFIND(name, x, y)	name##_RB_NFIND(x, y)
Kylie McClain b61f59
#define RB_NEXT(name, x, y)	name##_RB_NEXT(y)
Kylie McClain b61f59
#define RB_PREV(name, x, y)	name##_RB_PREV(y)
Kylie McClain b61f59
#define RB_MIN(name, x)		name##_RB_MINMAX(x, RB_NEGINF)
Kylie McClain b61f59
#define RB_MAX(name, x)		name##_RB_MINMAX(x, RB_INF)
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH(x, name, head)					\
Kylie McClain b61f59
	for ((x) = RB_MIN(name, head);					\
Kylie McClain b61f59
	     (x) != NULL;						\
Kylie McClain b61f59
	     (x) = name##_RB_NEXT(x))
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH_FROM(x, name, y)					\
Kylie McClain b61f59
	for ((x) = (y);							\
Kylie McClain b61f59
	    ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);	\
Kylie McClain b61f59
	     (x) = (y))
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH_SAFE(x, name, head, y)				\
Kylie McClain b61f59
	for ((x) = RB_MIN(name, head);					\
Kylie McClain b61f59
	    ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL);	\
Kylie McClain b61f59
	     (x) = (y))
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH_REVERSE(x, name, head)				\
Kylie McClain b61f59
	for ((x) = RB_MAX(name, head);					\
Kylie McClain b61f59
	     (x) != NULL;						\
Kylie McClain b61f59
	     (x) = name##_RB_PREV(x))
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH_REVERSE_FROM(x, name, y)				\
Kylie McClain b61f59
	for ((x) = (y);							\
Kylie McClain b61f59
	    ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);	\
Kylie McClain b61f59
	     (x) = (y))
Kylie McClain b61f59
Kylie McClain b61f59
#define RB_FOREACH_REVERSE_SAFE(x, name, head, y)			\
Kylie McClain b61f59
	for ((x) = RB_MAX(name, head);					\
Kylie McClain b61f59
	    ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL);	\
Kylie McClain b61f59
	     (x) = (y))
Kylie McClain b61f59
Kylie McClain b61f59
#endif	/* _SYS_TREE_H_ */