1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
/*
* Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
* Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
* Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/*
* Abstract:
* Implementation of quick map, a binary tree where the caller always
* provides all necessary storage.
*
*/
/*****************************************************************************
*
* Map
*
* Map is an associative array. By providing a key, the caller can retrieve
* an object from the map. All objects in the map have an associated key,
* as specified by the caller when the object was inserted into the map.
* In addition to random access, the caller can traverse the map much like
* a linked list, either forwards from the first object or backwards from
* the last object. The objects in the map are always traversed in
* order since the nodes are stored sorted.
*
* This implementation of Map uses a red black tree verified against
* Cormen-Leiserson-Rivest text, McGraw-Hill Edition, fourteenth
* printing, 1994.
*
*****************************************************************************/
#include <util/cl_qmap.h>
#include <string.h>
static inline void __cl_primitive_insert(cl_list_item_t *const p_list_item,
cl_list_item_t *const p_new_item)
{
/* CL_ASSERT that a non-null pointer is provided. */
assert(p_list_item);
/* CL_ASSERT that a non-null pointer is provided. */
assert(p_new_item);
p_new_item->p_next = p_list_item;
p_new_item->p_prev = p_list_item->p_prev;
p_list_item->p_prev = p_new_item;
p_new_item->p_prev->p_next = p_new_item;
}
static inline void __cl_primitive_remove(cl_list_item_t *const p_list_item)
{
/* CL_ASSERT that a non-null pointer is provided. */
assert(p_list_item);
/* set the back pointer */
p_list_item->p_next->p_prev = p_list_item->p_prev;
/* set the next pointer */
p_list_item->p_prev->p_next = p_list_item->p_next;
/* if we're debugging, spruce up the pointers to help find bugs */
#if defined( _DEBUG_ )
if (p_list_item != p_list_item->p_next) {
p_list_item->p_next = NULL;
p_list_item->p_prev = NULL;
}
#endif /* defined( _DEBUG_ ) */
}
/******************************************************************************
IMPLEMENTATION OF QUICK MAP
******************************************************************************/
/*
* Get the root.
*/
static inline cl_map_item_t *__cl_map_root(const cl_qmap_t * const p_map)
{
assert(p_map);
return (p_map->root.p_left);
}
/*
* Returns whether a given item is on the left of its parent.
*/
static bool __cl_map_is_left_child(const cl_map_item_t * const p_item)
{
assert(p_item);
assert(p_item->p_up);
assert(p_item->p_up != p_item);
return (p_item->p_up->p_left == p_item);
}
/*
* Retrieve the pointer to the parent's pointer to an item.
*/
static cl_map_item_t **__cl_map_get_parent_ptr_to_item(cl_map_item_t *
const p_item)
{
assert(p_item);
assert(p_item->p_up);
assert(p_item->p_up != p_item);
if (__cl_map_is_left_child(p_item))
return (&p_item->p_up->p_left);
assert(p_item->p_up->p_right == p_item);
return (&p_item->p_up->p_right);
}
/*
* Rotate a node to the left. This rotation affects the least number of links
* between nodes and brings the level of C up by one while increasing the depth
* of A one. Note that the links to/from W, X, Y, and Z are not affected.
*
* R R
* | |
* A C
* / \ / \
* W C A Z
* / \ / \
* B Z W B
* / \ / \
* X Y X Y
*/
static void __cl_map_rot_left(cl_qmap_t * const p_map,
cl_map_item_t * const p_item)
{
cl_map_item_t **pp_root;
assert(p_map);
assert(p_item);
assert(p_item->p_right != &p_map->nil);
pp_root = __cl_map_get_parent_ptr_to_item(p_item);
/* Point R to C instead of A. */
*pp_root = p_item->p_right;
/* Set C's parent to R. */
(*pp_root)->p_up = p_item->p_up;
/* Set A's right to B */
p_item->p_right = (*pp_root)->p_left;
/*
* Set B's parent to A. We trap for B being NIL since the
* caller may depend on NIL not changing.
*/
if ((*pp_root)->p_left != &p_map->nil)
(*pp_root)->p_left->p_up = p_item;
/* Set C's left to A. */
(*pp_root)->p_left = p_item;
/* Set A's parent to C. */
p_item->p_up = *pp_root;
}
/*
* Rotate a node to the right. This rotation affects the least number of links
* between nodes and brings the level of A up by one while increasing the depth
* of C one. Note that the links to/from W, X, Y, and Z are not affected.
*
* R R
* | |
* C A
* / \ / \
* A Z W C
* / \ / \
* W B B Z
* / \ / \
* X Y X Y
*/
static void __cl_map_rot_right(cl_qmap_t * const p_map,
cl_map_item_t * const p_item)
{
cl_map_item_t **pp_root;
assert(p_map);
assert(p_item);
assert(p_item->p_left != &p_map->nil);
/* Point R to A instead of C. */
pp_root = __cl_map_get_parent_ptr_to_item(p_item);
(*pp_root) = p_item->p_left;
/* Set A's parent to R. */
(*pp_root)->p_up = p_item->p_up;
/* Set C's left to B */
p_item->p_left = (*pp_root)->p_right;
/*
* Set B's parent to C. We trap for B being NIL since the
* caller may depend on NIL not changing.
*/
if ((*pp_root)->p_right != &p_map->nil)
(*pp_root)->p_right->p_up = p_item;
/* Set A's right to C. */
(*pp_root)->p_right = p_item;
/* Set C's parent to A. */
p_item->p_up = *pp_root;
}
void cl_qmap_init(cl_qmap_t * const p_map)
{
assert(p_map);
memset(p_map, 0, sizeof(cl_qmap_t));
/* special setup for the root node */
p_map->root.p_up = &p_map->root;
p_map->root.p_left = &p_map->nil;
p_map->root.p_right = &p_map->nil;
p_map->root.color = CL_MAP_BLACK;
/* Setup the node used as terminator for all leaves. */
p_map->nil.p_up = &p_map->nil;
p_map->nil.p_left = &p_map->nil;
p_map->nil.p_right = &p_map->nil;
p_map->nil.color = CL_MAP_BLACK;
cl_qmap_remove_all(p_map);
}
cl_map_item_t *cl_qmap_get(const cl_qmap_t * const p_map,
const uint64_t key)
{
cl_map_item_t *p_item;
assert(p_map);
p_item = __cl_map_root(p_map);
while (p_item != &p_map->nil) {
if (key == p_item->key)
break; /* just right */
if (key < p_item->key)
p_item = p_item->p_left; /* too small */
else
p_item = p_item->p_right; /* too big */
}
return (p_item);
}
cl_map_item_t *cl_qmap_get_next(const cl_qmap_t * const p_map,
const uint64_t key)
{
cl_map_item_t *p_item;
cl_map_item_t *p_item_found;
assert(p_map);
p_item = __cl_map_root(p_map);
p_item_found = (cl_map_item_t *) & p_map->nil;
while (p_item != &p_map->nil) {
if (key < p_item->key) {
p_item_found = p_item;
p_item = p_item->p_left;
} else {
p_item = p_item->p_right;
}
}
return (p_item_found);
}
void cl_qmap_apply_func(const cl_qmap_t * const p_map,
cl_pfn_qmap_apply_t pfn_func,
const void *const context)
{
cl_map_item_t *p_map_item;
/* Note that context can have any arbitrary value. */
assert(p_map);
assert(pfn_func);
p_map_item = cl_qmap_head(p_map);
while (p_map_item != cl_qmap_end(p_map)) {
pfn_func(p_map_item, (void *)context);
p_map_item = cl_qmap_next(p_map_item);
}
}
/*
* Balance a tree starting at a given item back to the root.
*/
static void __cl_map_ins_bal(cl_qmap_t * const p_map,
cl_map_item_t * p_item)
{
cl_map_item_t *p_grand_uncle;
assert(p_map);
assert(p_item);
assert(p_item != &p_map->root);
while (p_item->p_up->color == CL_MAP_RED) {
if (__cl_map_is_left_child(p_item->p_up)) {
p_grand_uncle = p_item->p_up->p_up->p_right;
assert(p_grand_uncle);
if (p_grand_uncle->color == CL_MAP_RED) {
p_grand_uncle->color = CL_MAP_BLACK;
p_item->p_up->color = CL_MAP_BLACK;
p_item->p_up->p_up->color = CL_MAP_RED;
p_item = p_item->p_up->p_up;
continue;
}
if (!__cl_map_is_left_child(p_item)) {
p_item = p_item->p_up;
__cl_map_rot_left(p_map, p_item);
}
p_item->p_up->color = CL_MAP_BLACK;
p_item->p_up->p_up->color = CL_MAP_RED;
__cl_map_rot_right(p_map, p_item->p_up->p_up);
} else {
p_grand_uncle = p_item->p_up->p_up->p_left;
assert(p_grand_uncle);
if (p_grand_uncle->color == CL_MAP_RED) {
p_grand_uncle->color = CL_MAP_BLACK;
p_item->p_up->color = CL_MAP_BLACK;
p_item->p_up->p_up->color = CL_MAP_RED;
p_item = p_item->p_up->p_up;
continue;
}
if (__cl_map_is_left_child(p_item)) {
p_item = p_item->p_up;
__cl_map_rot_right(p_map, p_item);
}
p_item->p_up->color = CL_MAP_BLACK;
p_item->p_up->p_up->color = CL_MAP_RED;
__cl_map_rot_left(p_map, p_item->p_up->p_up);
}
}
}
cl_map_item_t *cl_qmap_insert(cl_qmap_t * const p_map,
const uint64_t key,
cl_map_item_t * const p_item)
{
cl_map_item_t *p_insert_at, *p_comp_item;
assert(p_map);
assert(p_item);
assert(p_map->root.p_up == &p_map->root);
assert(p_map->root.color != CL_MAP_RED);
assert(p_map->nil.color != CL_MAP_RED);
p_item->p_left = &p_map->nil;
p_item->p_right = &p_map->nil;
p_item->key = key;
p_item->color = CL_MAP_RED;
/* Find the insertion location. */
p_insert_at = &p_map->root;
p_comp_item = __cl_map_root(p_map);
while (p_comp_item != &p_map->nil) {
p_insert_at = p_comp_item;
if (key == p_insert_at->key)
return (p_insert_at);
/* Traverse the tree until the correct insertion point is found. */
if (key < p_insert_at->key)
p_comp_item = p_insert_at->p_left;
else
p_comp_item = p_insert_at->p_right;
}
assert(p_insert_at != &p_map->nil);
assert(p_comp_item == &p_map->nil);
/* Insert the item. */
if (p_insert_at == &p_map->root) {
p_insert_at->p_left = p_item;
/*
* Primitive insert places the new item in front of
* the existing item.
*/
__cl_primitive_insert(&p_map->nil.pool_item.list_item,
&p_item->pool_item.list_item);
} else if (key < p_insert_at->key) {
p_insert_at->p_left = p_item;
/*
* Primitive insert places the new item in front of
* the existing item.
*/
__cl_primitive_insert(&p_insert_at->pool_item.list_item,
&p_item->pool_item.list_item);
} else {
p_insert_at->p_right = p_item;
/*
* Primitive insert places the new item in front of
* the existing item.
*/
__cl_primitive_insert(p_insert_at->pool_item.list_item.p_next,
&p_item->pool_item.list_item);
}
/* Increase the count. */
p_map->count++;
p_item->p_up = p_insert_at;
/*
* We have added depth to this section of the tree.
* Rebalance as necessary as we retrace our path through the tree
* and update colors.
*/
__cl_map_ins_bal(p_map, p_item);
__cl_map_root(p_map)->color = CL_MAP_BLACK;
/*
* Note that it is not necessary to re-color the nil node black because all
* red color assignments are made via the p_up pointer, and nil is never
* set as the value of a p_up pointer.
*/
#ifdef _DEBUG_
/* Set the pointer to the map in the map item for consistency checking. */
p_item->p_map = p_map;
#endif
return (p_item);
}
static void __cl_map_del_bal(cl_qmap_t * const p_map,
cl_map_item_t * p_item)
{
cl_map_item_t *p_uncle;
while ((p_item->color != CL_MAP_RED) && (p_item->p_up != &p_map->root)) {
if (__cl_map_is_left_child(p_item)) {
p_uncle = p_item->p_up->p_right;
if (p_uncle->color == CL_MAP_RED) {
p_uncle->color = CL_MAP_BLACK;
p_item->p_up->color = CL_MAP_RED;
__cl_map_rot_left(p_map, p_item->p_up);
p_uncle = p_item->p_up->p_right;
}
if (p_uncle->p_right->color != CL_MAP_RED) {
if (p_uncle->p_left->color != CL_MAP_RED) {
p_uncle->color = CL_MAP_RED;
p_item = p_item->p_up;
continue;
}
p_uncle->p_left->color = CL_MAP_BLACK;
p_uncle->color = CL_MAP_RED;
__cl_map_rot_right(p_map, p_uncle);
p_uncle = p_item->p_up->p_right;
}
p_uncle->color = p_item->p_up->color;
p_item->p_up->color = CL_MAP_BLACK;
p_uncle->p_right->color = CL_MAP_BLACK;
__cl_map_rot_left(p_map, p_item->p_up);
break;
} else {
p_uncle = p_item->p_up->p_left;
if (p_uncle->color == CL_MAP_RED) {
p_uncle->color = CL_MAP_BLACK;
p_item->p_up->color = CL_MAP_RED;
__cl_map_rot_right(p_map, p_item->p_up);
p_uncle = p_item->p_up->p_left;
}
if (p_uncle->p_left->color != CL_MAP_RED) {
if (p_uncle->p_right->color != CL_MAP_RED) {
p_uncle->color = CL_MAP_RED;
p_item = p_item->p_up;
continue;
}
p_uncle->p_right->color = CL_MAP_BLACK;
p_uncle->color = CL_MAP_RED;
__cl_map_rot_left(p_map, p_uncle);
p_uncle = p_item->p_up->p_left;
}
p_uncle->color = p_item->p_up->color;
p_item->p_up->color = CL_MAP_BLACK;
p_uncle->p_left->color = CL_MAP_BLACK;
__cl_map_rot_right(p_map, p_item->p_up);
break;
}
}
p_item->color = CL_MAP_BLACK;
}
void cl_qmap_remove_item(cl_qmap_t * const p_map,
cl_map_item_t * const p_item)
{
cl_map_item_t *p_child, *p_del_item;
assert(p_map);
assert(p_item);
if (p_item == cl_qmap_end(p_map))
return;
if ((p_item->p_right == &p_map->nil) || (p_item->p_left == &p_map->nil)) {
/* The item being removed has children on at most on side. */
p_del_item = p_item;
} else {
/*
* The item being removed has children on both side.
* We select the item that will replace it. After removing
* the substitute item and rebalancing, the tree will have the
* correct topology. Exchanging the substitute for the item
* will finalize the removal.
*/
p_del_item = cl_qmap_next(p_item);
assert(p_del_item != &p_map->nil);
}
/* Remove the item from the list. */
__cl_primitive_remove(&p_item->pool_item.list_item);
/* Decrement the item count. */
p_map->count--;
/* Get the pointer to the new root's child, if any. */
if (p_del_item->p_left != &p_map->nil)
p_child = p_del_item->p_left;
else
p_child = p_del_item->p_right;
/*
* This assignment may modify the parent pointer of the nil node.
* This is inconsequential.
*/
p_child->p_up = p_del_item->p_up;
(*__cl_map_get_parent_ptr_to_item(p_del_item)) = p_child;
if (p_del_item->color != CL_MAP_RED)
__cl_map_del_bal(p_map, p_child);
/*
* Note that the splicing done below does not need to occur before
* the tree is balanced, since the actual topology changes are made by the
* preceding code. The topology is preserved by the color assignment made
* below (reader should be reminded that p_del_item == p_item in some cases).
*/
if (p_del_item != p_item) {
/*
* Finalize the removal of the specified item by exchanging it with
* the substitute which we removed above.
*/
p_del_item->p_up = p_item->p_up;
p_del_item->p_left = p_item->p_left;
p_del_item->p_right = p_item->p_right;
(*__cl_map_get_parent_ptr_to_item(p_item)) = p_del_item;
p_item->p_right->p_up = p_del_item;
p_item->p_left->p_up = p_del_item;
p_del_item->color = p_item->color;
}
assert(p_map->nil.color != CL_MAP_RED);
#ifdef _DEBUG_
/* Clear the pointer to the map since the item has been removed. */
p_item->p_map = NULL;
#endif
}
cl_map_item_t *cl_qmap_remove(cl_qmap_t * const p_map, const uint64_t key)
{
cl_map_item_t *p_item;
assert(p_map);
/* Seek the node with the specified key */
p_item = cl_qmap_get(p_map, key);
cl_qmap_remove_item(p_map, p_item);
return (p_item);
}
void cl_qmap_merge(cl_qmap_t * const p_dest_map,
cl_qmap_t * const p_src_map)
{
cl_map_item_t *p_item, *p_item2, *p_next;
assert(p_dest_map);
assert(p_src_map);
p_item = cl_qmap_head(p_src_map);
while (p_item != cl_qmap_end(p_src_map)) {
p_next = cl_qmap_next(p_item);
/* Remove the item from its current map. */
cl_qmap_remove_item(p_src_map, p_item);
/* Insert the item into the destination map. */
p_item2 =
cl_qmap_insert(p_dest_map, cl_qmap_key(p_item), p_item);
/* Check that the item was successfully inserted. */
if (p_item2 != p_item) {
/* Put the item in back in the source map. */
p_item2 =
cl_qmap_insert(p_src_map, cl_qmap_key(p_item),
p_item);
assert(p_item2 == p_item);
}
p_item = p_next;
}
}
static void __cl_qmap_delta_move(cl_qmap_t * const p_dest,
cl_qmap_t * const p_src,
cl_map_item_t ** const pp_item)
{
cl_map_item_t __attribute__((__unused__)) *p_temp;
cl_map_item_t *p_next;
/*
* Get the next item so that we can ensure that pp_item points to
* a valid item upon return from the function.
*/
p_next = cl_qmap_next(*pp_item);
/* Move the old item from its current map the the old map. */
cl_qmap_remove_item(p_src, *pp_item);
p_temp = cl_qmap_insert(p_dest, cl_qmap_key(*pp_item), *pp_item);
/* We should never have duplicates. */
assert(p_temp == *pp_item);
/* Point pp_item to a valid item in the source map. */
(*pp_item) = p_next;
}
void cl_qmap_delta(cl_qmap_t * const p_map1,
cl_qmap_t * const p_map2,
cl_qmap_t * const p_new, cl_qmap_t * const p_old)
{
cl_map_item_t *p_item1, *p_item2;
uint64_t key1, key2;
assert(p_map1);
assert(p_map2);
assert(p_new);
assert(p_old);
assert(cl_is_qmap_empty(p_new));
assert(cl_is_qmap_empty(p_old));
p_item1 = cl_qmap_head(p_map1);
p_item2 = cl_qmap_head(p_map2);
while (p_item1 != cl_qmap_end(p_map1) && p_item2 != cl_qmap_end(p_map2)) {
key1 = cl_qmap_key(p_item1);
key2 = cl_qmap_key(p_item2);
if (key1 < key2) {
/* We found an old item. */
__cl_qmap_delta_move(p_old, p_map1, &p_item1);
} else if (key1 > key2) {
/* We found a new item. */
__cl_qmap_delta_move(p_new, p_map2, &p_item2);
} else {
/* Move both forward since they have the same key. */
p_item1 = cl_qmap_next(p_item1);
p_item2 = cl_qmap_next(p_item2);
}
}
/* Process the remainder if the end of either source map was reached. */
while (p_item2 != cl_qmap_end(p_map2))
__cl_qmap_delta_move(p_new, p_map2, &p_item2);
while (p_item1 != cl_qmap_end(p_map1))
__cl_qmap_delta_move(p_old, p_map1, &p_item1);
}
|