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