1 /* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */ 2 /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ 3 /* $DragonFly: src/sys/sys/tree.h,v 1.6 2007/04/19 19:06:01 dillon Exp $ */ 4 /* 5 * Copyright 2002 Niels Provos <provos@citi.umich.edu> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef _SYS_TREE_H_ 30 #define _SYS_TREE_H_ 31 32 /* 33 * This file defines data structures for different types of trees: 34 * splay trees and red-black trees. 35 * 36 * A splay tree is a self-organizing data structure. Every operation 37 * on the tree causes a splay to happen. The splay moves the requested 38 * node to the root of the tree and partly rebalances it. 39 * 40 * This has the benefit that request locality causes faster lookups as 41 * the requested nodes move to the top of the tree. On the other hand, 42 * every lookup causes memory writes. 43 * 44 * The Balance Theorem bounds the total access time for m operations 45 * and n inserts on an initially empty tree as O((m + n)lg n). The 46 * amortized cost for a sequence of m accesses to a splay tree is O(lg n); 47 * 48 * A red-black tree is a binary search tree with the node color as an 49 * extra attribute. It fulfills a set of conditions: 50 * - every search path from the root to a leaf consists of the 51 * same number of black nodes, 52 * - each red node (except for the root) has a black parent, 53 * - each leaf node is black. 54 * 55 * Every operation on a red-black tree is bounded as O(lg n). 56 * The maximum height of a red-black tree is 2lg (n+1). 57 */ 58 59 #define SPLAY_HEAD(name, type) \ 60 struct name { \ 61 struct type *sph_root; /* root of the tree */ \ 62 } 63 64 #define SPLAY_INITIALIZER(root) \ 65 { NULL } 66 67 #define SPLAY_INIT(root) do { \ 68 (root)->sph_root = NULL; \ 69 } while (/*CONSTCOND*/ 0) 70 71 #define SPLAY_ENTRY(type) \ 72 struct { \ 73 struct type *spe_left; /* left element */ \ 74 struct type *spe_right; /* right element */ \ 75 } 76 77 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left 78 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right 79 #define SPLAY_ROOT(head) (head)->sph_root 80 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) 81 82 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ 83 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ 84 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ 85 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ 86 (head)->sph_root = tmp; \ 87 } while (/*CONSTCOND*/ 0) 88 89 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ 90 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ 91 SPLAY_LEFT(tmp, field) = (head)->sph_root; \ 92 (head)->sph_root = tmp; \ 93 } while (/*CONSTCOND*/ 0) 94 95 #define SPLAY_LINKLEFT(head, tmp, field) do { \ 96 SPLAY_LEFT(tmp, field) = (head)->sph_root; \ 97 tmp = (head)->sph_root; \ 98 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ 99 } while (/*CONSTCOND*/ 0) 100 101 #define SPLAY_LINKRIGHT(head, tmp, field) do { \ 102 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ 103 tmp = (head)->sph_root; \ 104 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ 105 } while (/*CONSTCOND*/ 0) 106 107 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ 108 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ 109 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ 110 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ 111 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ 112 } while (/*CONSTCOND*/ 0) 113 114 /* Generates prototypes and inline functions */ 115 116 #define SPLAY_PROTOTYPE(name, type, field, cmp) \ 117 void name##_SPLAY(struct name *, struct type *); \ 118 void name##_SPLAY_MINMAX(struct name *, int); \ 119 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ 120 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ 121 \ 122 /* Finds the node with the same key as elm */ \ 123 static __inline struct type * \ 124 name##_SPLAY_FIND(struct name *head, struct type *elm) \ 125 { \ 126 if (SPLAY_EMPTY(head)) \ 127 return(NULL); \ 128 name##_SPLAY(head, elm); \ 129 if ((cmp)(elm, (head)->sph_root) == 0) \ 130 return (head->sph_root); \ 131 return (NULL); \ 132 } \ 133 \ 134 static __inline struct type * \ 135 name##_SPLAY_NEXT(struct name *head, struct type *elm) \ 136 { \ 137 name##_SPLAY(head, elm); \ 138 if (SPLAY_RIGHT(elm, field) != NULL) { \ 139 elm = SPLAY_RIGHT(elm, field); \ 140 while (SPLAY_LEFT(elm, field) != NULL) { \ 141 elm = SPLAY_LEFT(elm, field); \ 142 } \ 143 } else \ 144 elm = NULL; \ 145 return (elm); \ 146 } \ 147 \ 148 static __inline struct type * \ 149 name##_SPLAY_MIN_MAX(struct name *head, int val) \ 150 { \ 151 name##_SPLAY_MINMAX(head, val); \ 152 return (SPLAY_ROOT(head)); \ 153 } 154 155 /* Main splay operation. 156 * Moves node close to the key of elm to top 157 */ 158 #define SPLAY_GENERATE(name, type, field, cmp) \ 159 struct type * \ 160 name##_SPLAY_INSERT(struct name *head, struct type *elm) \ 161 { \ 162 if (SPLAY_EMPTY(head)) { \ 163 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ 164 } else { \ 165 int __comp; \ 166 name##_SPLAY(head, elm); \ 167 __comp = (cmp)(elm, (head)->sph_root); \ 168 if(__comp < 0) { \ 169 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ 170 SPLAY_RIGHT(elm, field) = (head)->sph_root; \ 171 SPLAY_LEFT((head)->sph_root, field) = NULL; \ 172 } else if (__comp > 0) { \ 173 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ 174 SPLAY_LEFT(elm, field) = (head)->sph_root; \ 175 SPLAY_RIGHT((head)->sph_root, field) = NULL; \ 176 } else \ 177 return ((head)->sph_root); \ 178 } \ 179 (head)->sph_root = (elm); \ 180 return (NULL); \ 181 } \ 182 \ 183 struct type * \ 184 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ 185 { \ 186 struct type *__tmp; \ 187 if (SPLAY_EMPTY(head)) \ 188 return (NULL); \ 189 name##_SPLAY(head, elm); \ 190 if ((cmp)(elm, (head)->sph_root) == 0) { \ 191 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ 192 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ 193 } else { \ 194 __tmp = SPLAY_RIGHT((head)->sph_root, field); \ 195 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ 196 name##_SPLAY(head, elm); \ 197 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ 198 } \ 199 return (elm); \ 200 } \ 201 return (NULL); \ 202 } \ 203 \ 204 void \ 205 name##_SPLAY(struct name *head, struct type *elm) \ 206 { \ 207 struct type __node, *__left, *__right, *__tmp; \ 208 int __comp; \ 209 \ 210 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ 211 __left = __right = &__node; \ 212 \ 213 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \ 214 if (__comp < 0) { \ 215 __tmp = SPLAY_LEFT((head)->sph_root, field); \ 216 if (__tmp == NULL) \ 217 break; \ 218 if ((cmp)(elm, __tmp) < 0){ \ 219 SPLAY_ROTATE_RIGHT(head, __tmp, field); \ 220 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ 221 break; \ 222 } \ 223 SPLAY_LINKLEFT(head, __right, field); \ 224 } else if (__comp > 0) { \ 225 __tmp = SPLAY_RIGHT((head)->sph_root, field); \ 226 if (__tmp == NULL) \ 227 break; \ 228 if ((cmp)(elm, __tmp) > 0){ \ 229 SPLAY_ROTATE_LEFT(head, __tmp, field); \ 230 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ 231 break; \ 232 } \ 233 SPLAY_LINKRIGHT(head, __left, field); \ 234 } \ 235 } \ 236 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ 237 } \ 238 \ 239 /* Splay with either the minimum or the maximum element \ 240 * Used to find minimum or maximum element in tree. \ 241 */ \ 242 void name##_SPLAY_MINMAX(struct name *head, int __comp) \ 243 { \ 244 struct type __node, *__left, *__right, *__tmp; \ 245 \ 246 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ 247 __left = __right = &__node; \ 248 \ 249 while (1) { \ 250 if (__comp < 0) { \ 251 __tmp = SPLAY_LEFT((head)->sph_root, field); \ 252 if (__tmp == NULL) \ 253 break; \ 254 if (__comp < 0){ \ 255 SPLAY_ROTATE_RIGHT(head, __tmp, field); \ 256 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ 257 break; \ 258 } \ 259 SPLAY_LINKLEFT(head, __right, field); \ 260 } else if (__comp > 0) { \ 261 __tmp = SPLAY_RIGHT((head)->sph_root, field); \ 262 if (__tmp == NULL) \ 263 break; \ 264 if (__comp > 0) { \ 265 SPLAY_ROTATE_LEFT(head, __tmp, field); \ 266 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ 267 break; \ 268 } \ 269 SPLAY_LINKRIGHT(head, __left, field); \ 270 } \ 271 } \ 272 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ 273 } 274 275 #define SPLAY_NEGINF -1 276 #define SPLAY_INF 1 277 278 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) 279 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) 280 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) 281 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) 282 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ 283 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) 284 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ 285 : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) 286 287 #define SPLAY_FOREACH(x, name, head) \ 288 for ((x) = SPLAY_MIN(name, head); \ 289 (x) != NULL; \ 290 (x) = SPLAY_NEXT(name, head, x)) 291 292 /* Macros that define a red-black tree */ 293 294 #define RB_SCAN_INFO(name, type) \ 295 struct name##_scan_info { \ 296 struct name##_scan_info *link; \ 297 struct type *node; \ 298 } 299 300 #define RB_HEAD(name, type) \ 301 struct name { \ 302 struct type *rbh_root; /* root of the tree */ \ 303 struct name##_scan_info *rbh_inprog; /* scans in progress */ \ 304 } 305 306 #define RB_INITIALIZER(root) \ 307 { NULL, NULL } 308 309 #define RB_INIT(root) do { \ 310 (root)->rbh_root = NULL; \ 311 (root)->rbh_inprog = NULL; \ 312 } while (/*CONSTCOND*/ 0) 313 314 #define RB_BLACK 0 315 #define RB_RED 1 316 #define RB_ENTRY(type) \ 317 struct { \ 318 struct type *rbe_left; /* left element */ \ 319 struct type *rbe_right; /* right element */ \ 320 struct type *rbe_parent; /* parent element */ \ 321 int rbe_color; /* node color */ \ 322 } 323 324 #define RB_LEFT(elm, field) (elm)->field.rbe_left 325 #define RB_RIGHT(elm, field) (elm)->field.rbe_right 326 #define RB_PARENT(elm, field) (elm)->field.rbe_parent 327 #define RB_COLOR(elm, field) (elm)->field.rbe_color 328 #define RB_ROOT(head) (head)->rbh_root 329 #define RB_INPROG(head) (head)->rbh_inprog 330 #define RB_EMPTY(head) (RB_ROOT(head) == NULL) 331 332 #define RB_SET(elm, parent, field) do { \ 333 RB_PARENT(elm, field) = parent; \ 334 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ 335 RB_COLOR(elm, field) = RB_RED; \ 336 } while (/*CONSTCOND*/ 0) 337 338 #define RB_SET_BLACKRED(black, red, field) do { \ 339 RB_COLOR(black, field) = RB_BLACK; \ 340 RB_COLOR(red, field) = RB_RED; \ 341 } while (/*CONSTCOND*/ 0) 342 343 #ifndef RB_AUGMENT 344 #define RB_AUGMENT(x) 345 #endif 346 347 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ 348 (tmp) = RB_RIGHT(elm, field); \ 349 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ 350 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ 351 } \ 352 RB_AUGMENT(elm); \ 353 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ 354 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ 355 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ 356 else \ 357 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ 358 } else \ 359 (head)->rbh_root = (tmp); \ 360 RB_LEFT(tmp, field) = (elm); \ 361 RB_PARENT(elm, field) = (tmp); \ 362 RB_AUGMENT(tmp); \ 363 if ((RB_PARENT(tmp, field))) \ 364 RB_AUGMENT(RB_PARENT(tmp, field)); \ 365 } while (/*CONSTCOND*/ 0) 366 367 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ 368 (tmp) = RB_LEFT(elm, field); \ 369 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ 370 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ 371 } \ 372 RB_AUGMENT(elm); \ 373 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ 374 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ 375 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ 376 else \ 377 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ 378 } else \ 379 (head)->rbh_root = (tmp); \ 380 RB_RIGHT(tmp, field) = (elm); \ 381 RB_PARENT(elm, field) = (tmp); \ 382 RB_AUGMENT(tmp); \ 383 if ((RB_PARENT(tmp, field))) \ 384 RB_AUGMENT(RB_PARENT(tmp, field)); \ 385 } while (/*CONSTCOND*/ 0) 386 387 /* Generates prototypes and inline functions */ 388 #define RB_PROTOTYPE(name, type, field, cmp) \ 389 struct type *name##_RB_REMOVE(struct name *, struct type *); \ 390 struct type *name##_RB_INSERT(struct name *, struct type *); \ 391 struct type *name##_RB_FIND(struct name *, struct type *); \ 392 int name##_RB_SCAN(struct name *, int (*)(struct type *, void *), \ 393 int (*)(struct type *, void *), void *); \ 394 struct type *name##_RB_NEXT(struct type *); \ 395 struct type *name##_RB_PREV(struct type *); \ 396 struct type *name##_RB_MINMAX(struct name *, int); \ 397 RB_SCAN_INFO(name, type) \ 398 399 /* 400 * A version which supplies a fast lookup routine for an exact match 401 * on a numeric field. 402 */ 403 #define RB_PROTOTYPE2(name, type, field, cmp, datatype) \ 404 RB_PROTOTYPE(name, type, field, cmp); \ 405 struct type *name##_RB_LOOKUP(struct name *, datatype) \ 406 407 /* 408 * A version which supplies a fast lookup routine for a numeric 409 * field which resides within a ranged object, either using (begin,end), 410 * or using (begin,size). 411 */ 412 #define RB_PROTOTYPE3(name, type, field, cmp, datatype) \ 413 RB_PROTOTYPE2(name, type, field, cmp, datatype); \ 414 struct type *name##_RB_RLOOKUP(struct name *, datatype) \ 415 416 #define RB_PROTOTYPE4(name, type, field, cmp, datatype) \ 417 RB_PROTOTYPE2(name, type, field, cmp, datatype); \ 418 struct type *name##_RB_RLOOKUP(struct name *, datatype) \ 419 420 #define RB_PROTOTYPEX(name, type, field, cmp, rcmp, datatype) \ 421 RB_PROTOTYPE2(name, type, field, cmp, datatype); \ 422 struct type *name##_RB_RLOOKUP(struct name *, datatype) \ 423 424 /* Main rb operation. 425 * Moves node close to the key of elm to top 426 */ 427 #define RB_GENERATE(name, type, field, cmp) \ 428 static void \ 429 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ 430 { \ 431 struct type *parent, *gparent, *tmp; \ 432 while ((parent = RB_PARENT(elm, field)) != NULL && \ 433 RB_COLOR(parent, field) == RB_RED) { \ 434 gparent = RB_PARENT(parent, field); \ 435 if (parent == RB_LEFT(gparent, field)) { \ 436 tmp = RB_RIGHT(gparent, field); \ 437 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ 438 RB_COLOR(tmp, field) = RB_BLACK; \ 439 RB_SET_BLACKRED(parent, gparent, field);\ 440 elm = gparent; \ 441 continue; \ 442 } \ 443 if (RB_RIGHT(parent, field) == elm) { \ 444 RB_ROTATE_LEFT(head, parent, tmp, field);\ 445 tmp = parent; \ 446 parent = elm; \ 447 elm = tmp; \ 448 } \ 449 RB_SET_BLACKRED(parent, gparent, field); \ 450 RB_ROTATE_RIGHT(head, gparent, tmp, field); \ 451 } else { \ 452 tmp = RB_LEFT(gparent, field); \ 453 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ 454 RB_COLOR(tmp, field) = RB_BLACK; \ 455 RB_SET_BLACKRED(parent, gparent, field);\ 456 elm = gparent; \ 457 continue; \ 458 } \ 459 if (RB_LEFT(parent, field) == elm) { \ 460 RB_ROTATE_RIGHT(head, parent, tmp, field);\ 461 tmp = parent; \ 462 parent = elm; \ 463 elm = tmp; \ 464 } \ 465 RB_SET_BLACKRED(parent, gparent, field); \ 466 RB_ROTATE_LEFT(head, gparent, tmp, field); \ 467 } \ 468 } \ 469 RB_COLOR(head->rbh_root, field) = RB_BLACK; \ 470 } \ 471 \ 472 static void \ 473 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \ 474 struct type *elm) \ 475 { \ 476 struct type *tmp; \ 477 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ 478 elm != RB_ROOT(head)) { \ 479 if (RB_LEFT(parent, field) == elm) { \ 480 tmp = RB_RIGHT(parent, field); \ 481 if (RB_COLOR(tmp, field) == RB_RED) { \ 482 RB_SET_BLACKRED(tmp, parent, field); \ 483 RB_ROTATE_LEFT(head, parent, tmp, field);\ 484 tmp = RB_RIGHT(parent, field); \ 485 } \ 486 if ((RB_LEFT(tmp, field) == NULL || \ 487 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ 488 (RB_RIGHT(tmp, field) == NULL || \ 489 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ 490 RB_COLOR(tmp, field) = RB_RED; \ 491 elm = parent; \ 492 parent = RB_PARENT(elm, field); \ 493 } else { \ 494 if (RB_RIGHT(tmp, field) == NULL || \ 495 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ 496 struct type *oleft; \ 497 if ((oleft = RB_LEFT(tmp, field)) \ 498 != NULL) \ 499 RB_COLOR(oleft, field) = RB_BLACK;\ 500 RB_COLOR(tmp, field) = RB_RED; \ 501 RB_ROTATE_RIGHT(head, tmp, oleft, field);\ 502 tmp = RB_RIGHT(parent, field); \ 503 } \ 504 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ 505 RB_COLOR(parent, field) = RB_BLACK; \ 506 if (RB_RIGHT(tmp, field)) \ 507 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ 508 RB_ROTATE_LEFT(head, parent, tmp, field);\ 509 elm = RB_ROOT(head); \ 510 break; \ 511 } \ 512 } else { \ 513 tmp = RB_LEFT(parent, field); \ 514 if (RB_COLOR(tmp, field) == RB_RED) { \ 515 RB_SET_BLACKRED(tmp, parent, field); \ 516 RB_ROTATE_RIGHT(head, parent, tmp, field);\ 517 tmp = RB_LEFT(parent, field); \ 518 } \ 519 if ((RB_LEFT(tmp, field) == NULL || \ 520 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ 521 (RB_RIGHT(tmp, field) == NULL || \ 522 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ 523 RB_COLOR(tmp, field) = RB_RED; \ 524 elm = parent; \ 525 parent = RB_PARENT(elm, field); \ 526 } else { \ 527 if (RB_LEFT(tmp, field) == NULL || \ 528 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ 529 struct type *oright; \ 530 if ((oright = RB_RIGHT(tmp, field)) \ 531 != NULL) \ 532 RB_COLOR(oright, field) = RB_BLACK;\ 533 RB_COLOR(tmp, field) = RB_RED; \ 534 RB_ROTATE_LEFT(head, tmp, oright, field);\ 535 tmp = RB_LEFT(parent, field); \ 536 } \ 537 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ 538 RB_COLOR(parent, field) = RB_BLACK; \ 539 if (RB_LEFT(tmp, field)) \ 540 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ 541 RB_ROTATE_RIGHT(head, parent, tmp, field);\ 542 elm = RB_ROOT(head); \ 543 break; \ 544 } \ 545 } \ 546 } \ 547 if (elm) \ 548 RB_COLOR(elm, field) = RB_BLACK; \ 549 } \ 550 \ 551 struct type * \ 552 name##_RB_REMOVE(struct name *head, struct type *elm) \ 553 { \ 554 struct type *child, *parent, *old; \ 555 struct name##_scan_info *inprog; \ 556 int color; \ 557 \ 558 for (inprog = RB_INPROG(head); inprog; inprog = inprog->link) { \ 559 if (inprog->node == elm) \ 560 inprog->node = RB_NEXT(name, head, elm); \ 561 } \ 562 \ 563 old = elm; \ 564 if (RB_LEFT(elm, field) == NULL) \ 565 child = RB_RIGHT(elm, field); \ 566 else if (RB_RIGHT(elm, field) == NULL) \ 567 child = RB_LEFT(elm, field); \ 568 else { \ 569 struct type *left; \ 570 elm = RB_RIGHT(elm, field); \ 571 while ((left = RB_LEFT(elm, field)) != NULL) \ 572 elm = left; \ 573 child = RB_RIGHT(elm, field); \ 574 parent = RB_PARENT(elm, field); \ 575 color = RB_COLOR(elm, field); \ 576 if (child) \ 577 RB_PARENT(child, field) = parent; \ 578 if (parent) { \ 579 if (RB_LEFT(parent, field) == elm) \ 580 RB_LEFT(parent, field) = child; \ 581 else \ 582 RB_RIGHT(parent, field) = child; \ 583 RB_AUGMENT(parent); \ 584 } else \ 585 RB_ROOT(head) = child; \ 586 if (RB_PARENT(elm, field) == old) \ 587 parent = elm; \ 588 (elm)->field = (old)->field; \ 589 if (RB_PARENT(old, field)) { \ 590 if (RB_LEFT(RB_PARENT(old, field), field) == old)\ 591 RB_LEFT(RB_PARENT(old, field), field) = elm;\ 592 else \ 593 RB_RIGHT(RB_PARENT(old, field), field) = elm;\ 594 RB_AUGMENT(RB_PARENT(old, field)); \ 595 } else \ 596 RB_ROOT(head) = elm; \ 597 RB_PARENT(RB_LEFT(old, field), field) = elm; \ 598 if (RB_RIGHT(old, field)) \ 599 RB_PARENT(RB_RIGHT(old, field), field) = elm; \ 600 if (parent) { \ 601 left = parent; \ 602 do { \ 603 RB_AUGMENT(left); \ 604 } while ((left = RB_PARENT(left, field)) != NULL); \ 605 } \ 606 goto color; \ 607 } \ 608 parent = RB_PARENT(elm, field); \ 609 color = RB_COLOR(elm, field); \ 610 if (child) \ 611 RB_PARENT(child, field) = parent; \ 612 if (parent) { \ 613 if (RB_LEFT(parent, field) == elm) \ 614 RB_LEFT(parent, field) = child; \ 615 else \ 616 RB_RIGHT(parent, field) = child; \ 617 RB_AUGMENT(parent); \ 618 } else \ 619 RB_ROOT(head) = child; \ 620 color: \ 621 if (color == RB_BLACK) \ 622 name##_RB_REMOVE_COLOR(head, parent, child); \ 623 return (old); \ 624 } \ 625 \ 626 /* Inserts a node into the RB tree */ \ 627 struct type * \ 628 name##_RB_INSERT(struct name *head, struct type *elm) \ 629 { \ 630 struct type *tmp; \ 631 struct type *parent = NULL; \ 632 int comp = 0; \ 633 tmp = RB_ROOT(head); \ 634 while (tmp) { \ 635 parent = tmp; \ 636 comp = (cmp)(elm, parent); \ 637 if (comp < 0) \ 638 tmp = RB_LEFT(tmp, field); \ 639 else if (comp > 0) \ 640 tmp = RB_RIGHT(tmp, field); \ 641 else \ 642 return(tmp); \ 643 } \ 644 RB_SET(elm, parent, field); \ 645 if (parent != NULL) { \ 646 if (comp < 0) \ 647 RB_LEFT(parent, field) = elm; \ 648 else \ 649 RB_RIGHT(parent, field) = elm; \ 650 RB_AUGMENT(parent); \ 651 } else \ 652 RB_ROOT(head) = elm; \ 653 name##_RB_INSERT_COLOR(head, elm); \ 654 return (NULL); \ 655 } \ 656 \ 657 /* Finds the node with the same key as elm */ \ 658 struct type * \ 659 name##_RB_FIND(struct name *head, struct type *elm) \ 660 { \ 661 struct type *tmp = RB_ROOT(head); \ 662 int comp; \ 663 while (tmp) { \ 664 comp = cmp(elm, tmp); \ 665 if (comp < 0) \ 666 tmp = RB_LEFT(tmp, field); \ 667 else if (comp > 0) \ 668 tmp = RB_RIGHT(tmp, field); \ 669 else \ 670 return (tmp); \ 671 } \ 672 return (NULL); \ 673 } \ 674 \ 675 /* \ 676 * Issue a callback for all matching items. The scan function must \ 677 * return < 0 for items below the desired range, 0 for items within \ 678 * the range, and > 0 for items beyond the range. Any item may be \ 679 * deleted while the scan is in progress. \ 680 */ \ 681 static int \ 682 name##_SCANCMP_ALL(struct type *type, void *data) \ 683 { \ 684 return(0); \ 685 } \ 686 \ 687 int \ 688 name##_RB_SCAN(struct name *head, \ 689 int (*scancmp)(struct type *, void *), \ 690 int (*callback)(struct type *, void *), \ 691 void *data) \ 692 { \ 693 struct name##_scan_info info; \ 694 struct name##_scan_info **infopp; \ 695 struct type *best; \ 696 struct type *tmp; \ 697 int count; \ 698 int comp; \ 699 \ 700 if (scancmp == NULL) \ 701 scancmp = name##_SCANCMP_ALL; \ 702 \ 703 /* \ 704 * Locate the first element. \ 705 */ \ 706 tmp = RB_ROOT(head); \ 707 best = NULL; \ 708 while (tmp) { \ 709 comp = scancmp(tmp, data); \ 710 if (comp < 0) { \ 711 tmp = RB_RIGHT(tmp, field); \ 712 } else if (comp > 0) { \ 713 tmp = RB_LEFT(tmp, field); \ 714 } else { \ 715 best = tmp; \ 716 if (RB_LEFT(tmp, field) == NULL) \ 717 break; \ 718 tmp = RB_LEFT(tmp, field); \ 719 } \ 720 } \ 721 count = 0; \ 722 if (best) { \ 723 info.node = RB_NEXT(name, head, best); \ 724 info.link = RB_INPROG(head); \ 725 RB_INPROG(head) = &info; \ 726 while ((comp = callback(best, data)) >= 0) { \ 727 count += comp; \ 728 best = info.node; \ 729 if (best == NULL || scancmp(best, data) != 0) \ 730 break; \ 731 info.node = RB_NEXT(name, head, best); \ 732 } \ 733 if (comp < 0) /* error or termination */ \ 734 count = comp; \ 735 infopp = &RB_INPROG(head); \ 736 while (*infopp != &info) \ 737 infopp = &(*infopp)->link; \ 738 *infopp = info.link; \ 739 } \ 740 return(count); \ 741 } \ 742 \ 743 /* ARGSUSED */ \ 744 struct type * \ 745 name##_RB_NEXT(struct type *elm) \ 746 { \ 747 if (RB_RIGHT(elm, field)) { \ 748 elm = RB_RIGHT(elm, field); \ 749 while (RB_LEFT(elm, field)) \ 750 elm = RB_LEFT(elm, field); \ 751 } else { \ 752 if (RB_PARENT(elm, field) && \ 753 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ 754 elm = RB_PARENT(elm, field); \ 755 else { \ 756 while (RB_PARENT(elm, field) && \ 757 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ 758 elm = RB_PARENT(elm, field); \ 759 elm = RB_PARENT(elm, field); \ 760 } \ 761 } \ 762 return (elm); \ 763 } \ 764 \ 765 /* ARGSUSED */ \ 766 struct type * \ 767 name##_RB_PREV(struct type *elm) \ 768 { \ 769 if (RB_LEFT(elm, field)) { \ 770 elm = RB_LEFT(elm, field); \ 771 while (RB_RIGHT(elm, field)) \ 772 elm = RB_RIGHT(elm, field); \ 773 } else { \ 774 if (RB_PARENT(elm, field) && \ 775 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ 776 elm = RB_PARENT(elm, field); \ 777 else { \ 778 while (RB_PARENT(elm, field) && \ 779 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ 780 elm = RB_PARENT(elm, field); \ 781 elm = RB_PARENT(elm, field); \ 782 } \ 783 } \ 784 return (elm); \ 785 } \ 786 \ 787 struct type * \ 788 name##_RB_MINMAX(struct name *head, int val) \ 789 { \ 790 struct type *tmp = RB_ROOT(head); \ 791 struct type *parent = NULL; \ 792 while (tmp) { \ 793 parent = tmp; \ 794 if (val < 0) \ 795 tmp = RB_LEFT(tmp, field); \ 796 else \ 797 tmp = RB_RIGHT(tmp, field); \ 798 } \ 799 return (parent); \ 800 } 801 802 /* 803 * This extended version implements a fast LOOKUP function given 804 * a numeric data type. 805 * 806 * The element whos index/offset field is exactly the specified value 807 * will be returned, or NULL. 808 */ 809 #define RB_GENERATE2(name, type, field, cmp, datatype, indexfield) \ 810 RB_GENERATE(name, type, field, cmp) \ 811 \ 812 struct type * \ 813 name##_RB_LOOKUP(struct name *head, datatype value) \ 814 { \ 815 struct type *tmp; \ 816 \ 817 tmp = RB_ROOT(head); \ 818 while (tmp) { \ 819 if (value > tmp->indexfield) \ 820 tmp = RB_RIGHT(tmp, field); \ 821 else if (value < tmp->indexfield) \ 822 tmp = RB_LEFT(tmp, field); \ 823 else \ 824 return(tmp); \ 825 } \ 826 return(NULL); \ 827 } \ 828 829 /* 830 * This extended version implements a fast ranged-based LOOKUP function 831 * given a numeric data type, for data types with a beginning and end 832 * (end is inclusive). 833 * 834 * The element whos range contains the specified value is returned, or NULL 835 */ 836 #define RB_GENERATE3(name, type, field, cmp, datatype, begfield, endfield) \ 837 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \ 838 \ 839 struct type * \ 840 name##_RB_RLOOKUP(struct name *head, datatype value) \ 841 { \ 842 struct type *tmp; \ 843 \ 844 tmp = RB_ROOT(head); \ 845 while (tmp) { \ 846 if (value >= tmp->begfield && value <= tmp->endfield) \ 847 return(tmp); \ 848 if (value > tmp->begfield) \ 849 tmp = RB_RIGHT(tmp, field); \ 850 else \ 851 tmp = RB_LEFT(tmp, field); \ 852 } \ 853 return(NULL); \ 854 } \ 855 856 /* 857 * This extended version implements a fast ranged-based LOOKUP function 858 * given a numeric data type, for data types with a beginning and size. 859 * 860 * WARNING: The full range of the data type is not supported due to a 861 * boundary condition at the end, where (beginning + size) might overflow. 862 * 863 * The element whos range contains the specified value is returned, or NULL 864 */ 865 #define RB_GENERATE4(name, type, field, cmp, datatype, begfield, sizefield) \ 866 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \ 867 \ 868 struct type * \ 869 name##_RB_RLOOKUP(struct name *head, datatype value) \ 870 { \ 871 struct type *tmp; \ 872 \ 873 tmp = RB_ROOT(head); \ 874 while (tmp) { \ 875 if (value >= tmp->begfield && \ 876 value < tmp->begfield + tmp->sizefield) { \ 877 return(tmp); \ 878 } \ 879 if (value > tmp->begfield) \ 880 tmp = RB_RIGHT(tmp, field); \ 881 else \ 882 tmp = RB_LEFT(tmp, field); \ 883 } \ 884 return(NULL); \ 885 } \ 886 887 /* 888 * The 'X' version adds a generic ranged function using a callback instead 889 * of fixed functions. 890 */ 891 #define RB_GENERATEX(name, type, field, cmp, rcmp, datatype, begfield) \ 892 RB_GENERATE2(name, type, field, cmp, datatype, begfield) \ 893 \ 894 struct type * \ 895 name##_RB_RLOOKUP(struct name *head, datatype value) \ 896 { \ 897 struct type *tmp; \ 898 int r; \ 899 \ 900 tmp = RB_ROOT(head); \ 901 while (tmp) { \ 902 r = rcmp(value, tmp); \ 903 if (r == 0) \ 904 return(tmp); \ 905 if (r > 0) \ 906 tmp = RB_RIGHT(tmp, field); \ 907 else \ 908 tmp = RB_LEFT(tmp, field); \ 909 } \ 910 return(NULL); \ 911 } \ 912 913 914 #define RB_NEGINF -1 915 #define RB_INF 1 916 917 #define RB_INSERT(name, root, elm) name##_RB_INSERT(root, elm) 918 #define RB_REMOVE(name, root, elm) name##_RB_REMOVE(root, elm) 919 #define RB_FIND(name, root, elm) name##_RB_FIND(root, elm) 920 #define RB_LOOKUP(name, root, value) name##_RB_LOOKUP(root, value) 921 #define RB_RLOOKUP(name, root, value) name##_RB_RLOOKUP(root, value) 922 #define RB_SCAN(name, root, cmp, callback, data) \ 923 name##_RB_SCAN(root, cmp, callback, data) 924 #define RB_NEXT(name, root, elm) name##_RB_NEXT(elm) 925 #define RB_PREV(name, root, elm) name##_RB_PREV(elm) 926 #define RB_MIN(name, root) name##_RB_MINMAX(root, RB_NEGINF) 927 #define RB_MAX(name, root) name##_RB_MINMAX(root, RB_INF) 928 929 #define RB_FOREACH(x, name, head) \ 930 for ((x) = RB_MIN(name, head); \ 931 (x) != NULL; \ 932 (x) = name##_RB_NEXT(x)) 933 934 #endif /* _SYS_TREE_H_ */ 935