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