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