| 1 |
/**************************************************************************** |
| 2 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 6 |
Free Software Foundation. |
| 7 |
|
| 8 |
This program is designed to work with certain software (including |
| 9 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 10 |
as designated in a particular file or component or in included license |
| 11 |
documentation. The authors of MySQL hereby grant you an additional |
| 12 |
permission to link the program and your derivative works with the |
| 13 |
separately licensed software that they have either included with |
| 14 |
the program or referenced in the documentation. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 19 |
for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License along with |
| 22 |
this program; if not, write to the Free Software Foundation, Inc., |
| 23 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
|
| 25 |
*****************************************************************************/ |
| 26 |
/** Red-Black tree implementation |
| 27 |
|
| 28 |
(c) 2007 Oracle/Innobase Oy |
| 29 |
|
| 30 |
Created 2007-03-20 Sunny Bains |
| 31 |
***********************************************************************/ |
| 32 |
|
| 33 |
#include "ut0rbt.h" |
| 34 |
#include "univ.i" |
| 35 |
#include "ut0new.h" |
| 36 |
|
| 37 |
/** Definition of a red-black tree |
| 38 |
============================== |
| 39 |
|
| 40 |
A red-black tree is a binary search tree which has the following |
| 41 |
red-black properties: |
| 42 |
|
| 43 |
1. Every node is either red or black. |
| 44 |
2. Every leaf (NULL - in our case tree->nil) is black. |
| 45 |
3. If a node is red, then both its children are black. |
| 46 |
4. Every simple path from a node to a descendant leaf contains the |
| 47 |
same number of black nodes. |
| 48 |
|
| 49 |
from (3) above, the implication is that on any path from the root |
| 50 |
to a leaf, red nodes must not be adjacent. |
| 51 |
|
| 52 |
However, any number of black nodes may appear in a sequence. |
| 53 |
*/ |
| 54 |
|
| 55 |
#if defined(IB_RBT_TESTING) |
| 56 |
#warning "Testing enabled!" |
| 57 |
#endif |
| 58 |
|
| 59 |
inline static ib_rbt_node_t *ROOT(const ib_rbt_t *t) { return t->root->left; } |
| 60 |
inline static size_t SIZEOF_NODE(const ib_rbt_t *t) { |
| 61 |
return sizeof(ib_rbt_node_t) + t->sizeof_value - 1; |
| 62 |
} |
| 63 |
|
| 64 |
#if defined UNIV_DEBUG || defined IB_RBT_TESTING |
| 65 |
/** Verify that the keys are in order. |
| 66 |
@param[in] tree tree to verify |
| 67 |
@return true of OK. false if not ordered */ |
| 68 |
static bool rbt_check_ordering(const ib_rbt_t *tree) { |
| 69 |
const ib_rbt_node_t *node; |
| 70 |
const ib_rbt_node_t *prev = nullptr; |
| 71 |
|
| 72 |
/* Iterate over all the nodes, comparing each node with the prev */ |
| 73 |
for (node = rbt_first(tree); node; node = rbt_next(tree, prev)) { |
| 74 |
if (prev) { |
| 75 |
int result; |
| 76 |
|
| 77 |
if (tree->cmp_arg) { |
| 78 |
result = |
| 79 |
tree->compare_with_arg(tree->cmp_arg, prev->value, node->value); |
| 80 |
} else { |
| 81 |
result = tree->compare(prev->value, node->value); |
| 82 |
} |
| 83 |
|
| 84 |
if (result >= 0) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
prev = node; |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** Check that every path from the root to the leaves has the same count. |
| 96 |
Count is expressed in the number of black nodes. |
| 97 |
@return 0 on failure else black height of the subtree */ |
| 98 |
static ulint rbt_count_black_nodes( |
| 99 |
const ib_rbt_t *tree, /*!< in: tree to verify */ |
| 100 |
const ib_rbt_node_t *node) /*!< in: start of sub-tree */ |
| 101 |
{ |
| 102 |
ulint result; |
| 103 |
|
| 104 |
if (node != tree->nil) { |
| 105 |
ulint left_height = rbt_count_black_nodes(tree, node->left); |
| 106 |
|
| 107 |
ulint right_height = rbt_count_black_nodes(tree, node->right); |
| 108 |
|
| 109 |
if (left_height == 0 || right_height == 0 || left_height != right_height) { |
| 110 |
result = 0; |
| 111 |
} else if (node->color == IB_RBT_RED) { |
| 112 |
/* Case 3 */ |
| 113 |
if (node->left->color != IB_RBT_BLACK || |
| 114 |
node->right->color != IB_RBT_BLACK) { |
| 115 |
result = 0; |
| 116 |
} else { |
| 117 |
result = left_height; |
| 118 |
} |
| 119 |
/* Check if it's anything other than RED or BLACK. */ |
| 120 |
} else if (node->color != IB_RBT_BLACK) { |
| 121 |
result = 0; |
| 122 |
} else { |
| 123 |
result = right_height + 1; |
| 124 |
} |
| 125 |
} else { |
| 126 |
result = 1; |
| 127 |
} |
| 128 |
|
| 129 |
return (result); |
| 130 |
} |
| 131 |
#endif /* UNIV_DEBUG || IB_RBT_TESTING */ |
| 132 |
|
| 133 |
/** Turn the node's right child's left sub-tree into node's right sub-tree. |
| 134 |
This will also make node's right child it's parent. */ |
| 135 |
static void rbt_rotate_left( |
| 136 |
const ib_rbt_node_t *nil, /*!< in: nil node of the tree */ |
| 137 |
ib_rbt_node_t *node) /*!< in: node to rotate */ |
| 138 |
{ |
| 139 |
ib_rbt_node_t *right = node->right; |
| 140 |
|
| 141 |
node->right = right->left; |
| 142 |
|
| 143 |
if (right->left != nil) { |
| 144 |
right->left->parent = node; |
| 145 |
} |
| 146 |
|
| 147 |
/* Right's new parent was node's parent. */ |
| 148 |
right->parent = node->parent; |
| 149 |
|
| 150 |
/* Since root's parent is tree->nil and root->parent->left points |
| 151 |
back to root, we can avoid the check. */ |
| 152 |
if (node == node->parent->left) { |
| 153 |
/* Node was on the left of its parent. */ |
| 154 |
node->parent->left = right; |
| 155 |
} else { |
| 156 |
/* Node must have been on the right. */ |
| 157 |
node->parent->right = right; |
| 158 |
} |
| 159 |
|
| 160 |
/* Finally, put node on right's left. */ |
| 161 |
right->left = node; |
| 162 |
node->parent = right; |
| 163 |
} |
| 164 |
|
| 165 |
/** Turn the node's left child's right sub-tree into node's left sub-tree. |
| 166 |
This also make node's left child it's parent. */ |
| 167 |
static void rbt_rotate_right( |
| 168 |
const ib_rbt_node_t *nil, /*!< in: nil node of tree */ |
| 169 |
ib_rbt_node_t *node) /*!< in: node to rotate */ |
| 170 |
{ |
| 171 |
ib_rbt_node_t *left = node->left; |
| 172 |
|
| 173 |
node->left = left->right; |
| 174 |
|
| 175 |
if (left->right != nil) { |
| 176 |
left->right->parent = node; |
| 177 |
} |
| 178 |
|
| 179 |
/* Left's new parent was node's parent. */ |
| 180 |
left->parent = node->parent; |
| 181 |
|
| 182 |
/* Since root's parent is tree->nil and root->parent->left points |
| 183 |
back to root, we can avoid the check. */ |
| 184 |
if (node == node->parent->right) { |
| 185 |
/* Node was on the left of its parent. */ |
| 186 |
node->parent->right = left; |
| 187 |
} else { |
| 188 |
/* Node must have been on the left. */ |
| 189 |
node->parent->left = left; |
| 190 |
} |
| 191 |
|
| 192 |
/* Finally, put node on left's right. */ |
| 193 |
left->right = node; |
| 194 |
node->parent = left; |
| 195 |
} |
| 196 |
|
| 197 |
/** Append a node to the tree. */ |
| 198 |
static ib_rbt_node_t *rbt_tree_add_child(const ib_rbt_t *tree, |
| 199 |
ib_rbt_bound_t *parent, |
| 200 |
ib_rbt_node_t *node) { |
| 201 |
/* Cast away the const. */ |
| 202 |
ib_rbt_node_t *last = (ib_rbt_node_t *)parent->last; |
| 203 |
|
| 204 |
if (last == tree->root || parent->result < 0) { |
| 205 |
last->left = node; |
| 206 |
} else { |
| 207 |
/* FIXME: We don't handle duplicates (yet)! */ |
| 208 |
ut_a(parent->result != 0); |
| 209 |
|
| 210 |
last->right = node; |
| 211 |
} |
| 212 |
|
| 213 |
node->parent = last; |
| 214 |
|
| 215 |
return (node); |
| 216 |
} |
| 217 |
|
| 218 |
/** Generic binary tree insert */ |
| 219 |
static ib_rbt_node_t *rbt_tree_insert(ib_rbt_t *tree, const void *key, |
| 220 |
ib_rbt_node_t *node) { |
| 221 |
ib_rbt_bound_t parent; |
| 222 |
ib_rbt_node_t *current = ROOT(tree); |
| 223 |
|
| 224 |
parent.result = 0; |
| 225 |
parent.last = tree->root; |
| 226 |
|
| 227 |
/* Regular binary search. */ |
| 228 |
while (current != tree->nil) { |
| 229 |
parent.last = current; |
| 230 |
|
| 231 |
if (tree->cmp_arg) { |
| 232 |
parent.result = |
| 233 |
tree->compare_with_arg(tree->cmp_arg, key, current->value); |
| 234 |
} else { |
| 235 |
parent.result = tree->compare(key, current->value); |
| 236 |
} |
| 237 |
|
| 238 |
if (parent.result < 0) { |
| 239 |
current = current->left; |
| 240 |
} else { |
| 241 |
current = current->right; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
ut_a(current == tree->nil); |
| 246 |
|
| 247 |
rbt_tree_add_child(tree, &parent, node); |
| 248 |
|
| 249 |
return (node); |
| 250 |
} |
| 251 |
|
| 252 |
/** Balance a tree after inserting a node. */ |
| 253 |
static void rbt_balance_tree( |
| 254 |
const ib_rbt_t *tree, /*!< in: tree to balance */ |
| 255 |
ib_rbt_node_t *node) /*!< in: node that was inserted */ |
| 256 |
{ |
| 257 |
const ib_rbt_node_t *nil = tree->nil; |
| 258 |
ib_rbt_node_t *parent = node->parent; |
| 259 |
|
| 260 |
/* Restore the red-black property. */ |
| 261 |
node->color = IB_RBT_RED; |
| 262 |
|
| 263 |
while (node != ROOT(tree) && parent->color == IB_RBT_RED) { |
| 264 |
ib_rbt_node_t *grand_parent = parent->parent; |
| 265 |
|
| 266 |
if (parent == grand_parent->left) { |
| 267 |
ib_rbt_node_t *uncle = grand_parent->right; |
| 268 |
|
| 269 |
if (uncle->color == IB_RBT_RED) { |
| 270 |
/* Case 1 - change the colors. */ |
| 271 |
uncle->color = IB_RBT_BLACK; |
| 272 |
parent->color = IB_RBT_BLACK; |
| 273 |
grand_parent->color = IB_RBT_RED; |
| 274 |
|
| 275 |
/* Move node up the tree. */ |
| 276 |
node = grand_parent; |
| 277 |
|
| 278 |
} else { |
| 279 |
if (node == parent->right) { |
| 280 |
/* Right is a black node and node is |
| 281 |
to the right, case 2 - move node |
| 282 |
up and rotate. */ |
| 283 |
node = parent; |
| 284 |
rbt_rotate_left(nil, node); |
| 285 |
} |
| 286 |
|
| 287 |
grand_parent = node->parent->parent; |
| 288 |
|
| 289 |
/* Case 3. */ |
| 290 |
node->parent->color = IB_RBT_BLACK; |
| 291 |
grand_parent->color = IB_RBT_RED; |
| 292 |
|
| 293 |
rbt_rotate_right(nil, grand_parent); |
| 294 |
} |
| 295 |
|
| 296 |
} else { |
| 297 |
ib_rbt_node_t *uncle = grand_parent->left; |
| 298 |
|
| 299 |
if (uncle->color == IB_RBT_RED) { |
| 300 |
/* Case 1 - change the colors. */ |
| 301 |
uncle->color = IB_RBT_BLACK; |
| 302 |
parent->color = IB_RBT_BLACK; |
| 303 |
grand_parent->color = IB_RBT_RED; |
| 304 |
|
| 305 |
/* Move node up the tree. */ |
| 306 |
node = grand_parent; |
| 307 |
|
| 308 |
} else { |
| 309 |
if (node == parent->left) { |
| 310 |
/* Left is a black node and node is to |
| 311 |
the right, case 2 - move node up and |
| 312 |
rotate. */ |
| 313 |
node = parent; |
| 314 |
rbt_rotate_right(nil, node); |
| 315 |
} |
| 316 |
|
| 317 |
grand_parent = node->parent->parent; |
| 318 |
|
| 319 |
/* Case 3. */ |
| 320 |
node->parent->color = IB_RBT_BLACK; |
| 321 |
grand_parent->color = IB_RBT_RED; |
| 322 |
|
| 323 |
rbt_rotate_left(nil, grand_parent); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
parent = node->parent; |
| 328 |
} |
| 329 |
|
| 330 |
/* Color the root black. */ |
| 331 |
ROOT(tree)->color = IB_RBT_BLACK; |
| 332 |
} |
| 333 |
|
| 334 |
/** Find the given node's successor. |
| 335 |
@return successor node or NULL if no successor */ |
| 336 |
static ib_rbt_node_t *rbt_find_successor( |
| 337 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 338 |
const ib_rbt_node_t *current) /*!< in: this is declared const |
| 339 |
because it can be called via |
| 340 |
rbt_next() */ |
| 341 |
{ |
| 342 |
const ib_rbt_node_t *nil = tree->nil; |
| 343 |
ib_rbt_node_t *next = current->right; |
| 344 |
|
| 345 |
/* Is there a sub-tree to the right that we can follow. */ |
| 346 |
if (next != nil) { |
| 347 |
/* Follow the left most links of the current right child. */ |
| 348 |
while (next->left != nil) { |
| 349 |
next = next->left; |
| 350 |
} |
| 351 |
|
| 352 |
} else { /* We will have to go up the tree to find the successor. */ |
| 353 |
ib_rbt_node_t *parent = current->parent; |
| 354 |
|
| 355 |
/* Cast away the const. */ |
| 356 |
next = (ib_rbt_node_t *)current; |
| 357 |
|
| 358 |
while (parent != tree->root && next == parent->right) { |
| 359 |
next = parent; |
| 360 |
parent = next->parent; |
| 361 |
} |
| 362 |
|
| 363 |
next = (parent == tree->root) ? nullptr : parent; |
| 364 |
} |
| 365 |
|
| 366 |
return (next); |
| 367 |
} |
| 368 |
|
| 369 |
/** Find the given node's precedecessor. |
| 370 |
@return predecessor node or NULL if no predecessor */ |
| 371 |
static ib_rbt_node_t *rbt_find_predecessor( |
| 372 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 373 |
const ib_rbt_node_t *current) /*!< in: this is declared const |
| 374 |
because it can be called via |
| 375 |
rbt_prev() */ |
| 376 |
{ |
| 377 |
const ib_rbt_node_t *nil = tree->nil; |
| 378 |
ib_rbt_node_t *prev = current->left; |
| 379 |
|
| 380 |
/* Is there a sub-tree to the left that we can follow. */ |
| 381 |
if (prev != nil) { |
| 382 |
/* Follow the right most links of the current left child. */ |
| 383 |
while (prev->right != nil) { |
| 384 |
prev = prev->right; |
| 385 |
} |
| 386 |
|
| 387 |
} else { /* We will have to go up the tree to find the precedecessor. */ |
| 388 |
ib_rbt_node_t *parent = current->parent; |
| 389 |
|
| 390 |
/* Cast away the const. */ |
| 391 |
prev = (ib_rbt_node_t *)current; |
| 392 |
|
| 393 |
while (parent != tree->root && prev == parent->left) { |
| 394 |
prev = parent; |
| 395 |
parent = prev->parent; |
| 396 |
} |
| 397 |
|
| 398 |
prev = (parent == tree->root) ? nullptr : parent; |
| 399 |
} |
| 400 |
|
| 401 |
return (prev); |
| 402 |
} |
| 403 |
|
| 404 |
/** Replace node with child. After applying transformations eject becomes |
| 405 |
an orphan. */ |
| 406 |
static void rbt_eject_node(ib_rbt_node_t *eject, /*!< in: node to eject */ |
| 407 |
ib_rbt_node_t *node) /*!< in: node to replace with */ |
| 408 |
{ |
| 409 |
/* Update the to be ejected node's parent's child pointers. */ |
| 410 |
if (eject->parent->left == eject) { |
| 411 |
eject->parent->left = node; |
| 412 |
} else if (eject->parent->right == eject) { |
| 413 |
eject->parent->right = node; |
| 414 |
} else { |
| 415 |
ut_error; |
| 416 |
} |
| 417 |
/* eject is now an orphan but otherwise its pointers |
| 418 |
and color are left intact. */ |
| 419 |
|
| 420 |
node->parent = eject->parent; |
| 421 |
} |
| 422 |
|
| 423 |
/** Replace a node with another node. */ |
| 424 |
static void rbt_replace_node( |
| 425 |
ib_rbt_node_t *replace, /*!< in: node to replace */ |
| 426 |
ib_rbt_node_t *node) /*!< in: node to replace with */ |
| 427 |
{ |
| 428 |
ib_rbt_color_t color = node->color; |
| 429 |
|
| 430 |
/* Update the node pointers. */ |
| 431 |
node->left = replace->left; |
| 432 |
node->right = replace->right; |
| 433 |
|
| 434 |
/* Update the child node pointers. */ |
| 435 |
node->left->parent = node; |
| 436 |
node->right->parent = node; |
| 437 |
|
| 438 |
/* Make the parent of replace point to node. */ |
| 439 |
rbt_eject_node(replace, node); |
| 440 |
|
| 441 |
/* Swap the colors. */ |
| 442 |
node->color = replace->color; |
| 443 |
replace->color = color; |
| 444 |
} |
| 445 |
|
| 446 |
/** Detach node from the tree replacing it with one of it's children. |
| 447 |
@return the child node that now occupies the position of the detached node */ |
| 448 |
static ib_rbt_node_t *rbt_detach_node( |
| 449 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 450 |
ib_rbt_node_t *node) /*!< in: node to detach */ |
| 451 |
{ |
| 452 |
ib_rbt_node_t *child; |
| 453 |
const ib_rbt_node_t *nil = tree->nil; |
| 454 |
|
| 455 |
if (node->left != nil && node->right != nil) { |
| 456 |
/* Case where the node to be deleted has two children. */ |
| 457 |
ib_rbt_node_t *successor = rbt_find_successor(tree, node); |
| 458 |
|
| 459 |
ut_a(successor != nil); |
| 460 |
ut_a(successor->parent != nil); |
| 461 |
ut_a(successor->left == nil); |
| 462 |
|
| 463 |
child = successor->right; |
| 464 |
|
| 465 |
/* Remove the successor node and replace with its child. */ |
| 466 |
rbt_eject_node(successor, child); |
| 467 |
|
| 468 |
/* Replace the node to delete with its successor node. */ |
| 469 |
rbt_replace_node(node, successor); |
| 470 |
} else { |
| 471 |
ut_a(node->left == nil || node->right == nil); |
| 472 |
|
| 473 |
child = (node->left != nil) ? node->left : node->right; |
| 474 |
|
| 475 |
/* Replace the node to delete with one of it's children. */ |
| 476 |
rbt_eject_node(node, child); |
| 477 |
} |
| 478 |
|
| 479 |
/* Reset the node links. */ |
| 480 |
node->parent = node->right = node->left = tree->nil; |
| 481 |
|
| 482 |
return (child); |
| 483 |
} |
| 484 |
|
| 485 |
/** Rebalance the right sub-tree after deletion. |
| 486 |
@return node to rebalance if more rebalancing required else NULL */ |
| 487 |
static ib_rbt_node_t *rbt_balance_right( |
| 488 |
const ib_rbt_node_t *nil, /*!< in: rb tree nil node */ |
| 489 |
ib_rbt_node_t *parent, /*!< in: parent node */ |
| 490 |
ib_rbt_node_t *sibling) /*!< in: sibling node */ |
| 491 |
{ |
| 492 |
ib_rbt_node_t *node = nullptr; |
| 493 |
|
| 494 |
ut_a(sibling != nil); |
| 495 |
|
| 496 |
/* Case 3. */ |
| 497 |
if (sibling->color == IB_RBT_RED) { |
| 498 |
parent->color = IB_RBT_RED; |
| 499 |
sibling->color = IB_RBT_BLACK; |
| 500 |
|
| 501 |
rbt_rotate_left(nil, parent); |
| 502 |
|
| 503 |
sibling = parent->right; |
| 504 |
|
| 505 |
ut_a(sibling != nil); |
| 506 |
} |
| 507 |
|
| 508 |
/* Since this will violate case 3 because of the change above. */ |
| 509 |
if (sibling->left->color == IB_RBT_BLACK && |
| 510 |
sibling->right->color == IB_RBT_BLACK) { |
| 511 |
node = parent; /* Parent needs to be rebalanced too. */ |
| 512 |
sibling->color = IB_RBT_RED; |
| 513 |
|
| 514 |
} else { |
| 515 |
if (sibling->right->color == IB_RBT_BLACK) { |
| 516 |
ut_a(sibling->left->color == IB_RBT_RED); |
| 517 |
|
| 518 |
sibling->color = IB_RBT_RED; |
| 519 |
sibling->left->color = IB_RBT_BLACK; |
| 520 |
|
| 521 |
rbt_rotate_right(nil, sibling); |
| 522 |
|
| 523 |
sibling = parent->right; |
| 524 |
ut_a(sibling != nil); |
| 525 |
} |
| 526 |
|
| 527 |
sibling->color = parent->color; |
| 528 |
sibling->right->color = IB_RBT_BLACK; |
| 529 |
|
| 530 |
parent->color = IB_RBT_BLACK; |
| 531 |
|
| 532 |
rbt_rotate_left(nil, parent); |
| 533 |
} |
| 534 |
|
| 535 |
return (node); |
| 536 |
} |
| 537 |
|
| 538 |
/** Rebalance the left sub-tree after deletion. |
| 539 |
@return node to rebalance if more rebalancing required else NULL */ |
| 540 |
static ib_rbt_node_t *rbt_balance_left( |
| 541 |
const ib_rbt_node_t *nil, /*!< in: rb tree nil node */ |
| 542 |
ib_rbt_node_t *parent, /*!< in: parent node */ |
| 543 |
ib_rbt_node_t *sibling) /*!< in: sibling node */ |
| 544 |
{ |
| 545 |
ib_rbt_node_t *node = nullptr; |
| 546 |
|
| 547 |
ut_a(sibling != nil); |
| 548 |
|
| 549 |
/* Case 3. */ |
| 550 |
if (sibling->color == IB_RBT_RED) { |
| 551 |
parent->color = IB_RBT_RED; |
| 552 |
sibling->color = IB_RBT_BLACK; |
| 553 |
|
| 554 |
rbt_rotate_right(nil, parent); |
| 555 |
sibling = parent->left; |
| 556 |
|
| 557 |
ut_a(sibling != nil); |
| 558 |
} |
| 559 |
|
| 560 |
/* Since this will violate case 3 because of the change above. */ |
| 561 |
if (sibling->right->color == IB_RBT_BLACK && |
| 562 |
sibling->left->color == IB_RBT_BLACK) { |
| 563 |
node = parent; /* Parent needs to be rebalanced too. */ |
| 564 |
sibling->color = IB_RBT_RED; |
| 565 |
|
| 566 |
} else { |
| 567 |
if (sibling->left->color == IB_RBT_BLACK) { |
| 568 |
ut_a(sibling->right->color == IB_RBT_RED); |
| 569 |
|
| 570 |
sibling->color = IB_RBT_RED; |
| 571 |
sibling->right->color = IB_RBT_BLACK; |
| 572 |
|
| 573 |
rbt_rotate_left(nil, sibling); |
| 574 |
|
| 575 |
sibling = parent->left; |
| 576 |
|
| 577 |
ut_a(sibling != nil); |
| 578 |
} |
| 579 |
|
| 580 |
sibling->color = parent->color; |
| 581 |
sibling->left->color = IB_RBT_BLACK; |
| 582 |
|
| 583 |
parent->color = IB_RBT_BLACK; |
| 584 |
|
| 585 |
rbt_rotate_right(nil, parent); |
| 586 |
} |
| 587 |
|
| 588 |
return (node); |
| 589 |
} |
| 590 |
|
| 591 |
/** Delete the node and rebalance the tree if necessary */ |
| 592 |
static void rbt_remove_node_and_rebalance( |
| 593 |
ib_rbt_t *tree, /*!< in: rb tree */ |
| 594 |
ib_rbt_node_t *node) /*!< in: node to remove */ |
| 595 |
{ |
| 596 |
/* Detach node and get the node that will be used |
| 597 |
as rebalance start. */ |
| 598 |
ib_rbt_node_t *child = rbt_detach_node(tree, node); |
| 599 |
|
| 600 |
if (node->color == IB_RBT_BLACK) { |
| 601 |
ib_rbt_node_t *last = child; |
| 602 |
|
| 603 |
ROOT(tree)->color = IB_RBT_RED; |
| 604 |
|
| 605 |
while (child && child->color == IB_RBT_BLACK) { |
| 606 |
ib_rbt_node_t *parent = child->parent; |
| 607 |
|
| 608 |
/* Did the deletion cause an imbalance in the |
| 609 |
parents left sub-tree. */ |
| 610 |
if (parent->left == child) { |
| 611 |
child = rbt_balance_right(tree->nil, parent, parent->right); |
| 612 |
|
| 613 |
} else if (parent->right == child) { |
| 614 |
child = rbt_balance_left(tree->nil, parent, parent->left); |
| 615 |
|
| 616 |
} else { |
| 617 |
ut_error; |
| 618 |
} |
| 619 |
|
| 620 |
if (child) { |
| 621 |
last = child; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
ut_a(last); |
| 626 |
|
| 627 |
last->color = IB_RBT_BLACK; |
| 628 |
ROOT(tree)->color = IB_RBT_BLACK; |
| 629 |
} |
| 630 |
|
| 631 |
/* Note that we have removed a node from the tree. */ |
| 632 |
--tree->n_nodes; |
| 633 |
} |
| 634 |
|
| 635 |
/** Recursively free the nodes. */ |
| 636 |
static void rbt_free_node(ib_rbt_node_t *node, /*!< in: node to free */ |
| 637 |
ib_rbt_node_t *nil) /*!< in: rb tree nil node */ |
| 638 |
{ |
| 639 |
if (node != nil) { |
| 640 |
rbt_free_node(node->left, nil); |
| 641 |
rbt_free_node(node->right, nil); |
| 642 |
|
| 643 |
ut::free(node); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
/** Free all the nodes and free the tree. */ |
| 648 |
void rbt_free(ib_rbt_t *tree) /*!< in: rb tree to free */ |
| 649 |
{ |
| 650 |
rbt_free_node(tree->root, tree->nil); |
| 651 |
ut::free(tree->nil); |
| 652 |
ut::free(tree); |
| 653 |
} |
| 654 |
|
| 655 |
/** Create an instance of a red black tree, whose comparison function takes |
| 656 |
an argument |
| 657 |
@return an empty rb tree */ |
| 658 |
ib_rbt_t *rbt_create_arg_cmp( |
| 659 |
size_t sizeof_value, /*!< in: sizeof data item */ |
| 660 |
ib_rbt_arg_compare compare, /*!< in: fn to compare items */ |
| 661 |
void *cmp_arg) /*!< in: compare fn arg */ |
| 662 |
{ |
| 663 |
ib_rbt_t *tree; |
| 664 |
|
| 665 |
ut_a(cmp_arg); |
| 666 |
|
| 667 |
tree = rbt_create(sizeof_value, nullptr); |
| 668 |
tree->cmp_arg = cmp_arg; |
| 669 |
tree->compare_with_arg = compare; |
| 670 |
|
| 671 |
return (tree); |
| 672 |
} |
| 673 |
|
| 674 |
/** Create an instance of a red black tree. |
| 675 |
@return an empty rb tree */ |
| 676 |
ib_rbt_t *rbt_create(size_t sizeof_value, /*!< in: sizeof data item */ |
| 677 |
ib_rbt_compare compare) /*!< in: fn to compare items */ |
| 678 |
{ |
| 679 |
ib_rbt_t *tree; |
| 680 |
ib_rbt_node_t *node; |
| 681 |
|
| 682 |
tree = |
| 683 |
(ib_rbt_t *)ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*tree)); |
| 684 |
|
| 685 |
tree->sizeof_value = sizeof_value; |
| 686 |
|
| 687 |
/* Create the sentinel (NIL) node. */ |
| 688 |
node = tree->nil = (ib_rbt_node_t *)ut::zalloc_withkey( |
| 689 |
UT_NEW_THIS_FILE_PSI_KEY, sizeof(*node)); |
| 690 |
|
| 691 |
node->color = IB_RBT_BLACK; |
| 692 |
node->parent = node->left = node->right = node; |
| 693 |
|
| 694 |
/* Create the "fake" root, the real root node will be the |
| 695 |
left child of this node. */ |
| 696 |
node = tree->root = (ib_rbt_node_t *)ut::zalloc_withkey( |
| 697 |
UT_NEW_THIS_FILE_PSI_KEY, sizeof(*node)); |
| 698 |
|
| 699 |
node->color = IB_RBT_BLACK; |
| 700 |
node->parent = node->left = node->right = tree->nil; |
| 701 |
|
| 702 |
tree->compare = compare; |
| 703 |
|
| 704 |
return (tree); |
| 705 |
} |
| 706 |
|
| 707 |
/** Generic insert of a value in the rb tree. |
| 708 |
@return inserted node */ |
| 709 |
const ib_rbt_node_t *rbt_insert( |
| 710 |
ib_rbt_t *tree, /*!< in: rb tree */ |
| 711 |
const void *key, /*!< in: key for ordering */ |
| 712 |
const void *value) /*!< in: value of key, this value |
| 713 |
is copied to the node */ |
| 714 |
{ |
| 715 |
ib_rbt_node_t *node; |
| 716 |
|
| 717 |
/* Create the node that will hold the value data. */ |
| 718 |
node = (ib_rbt_node_t *)ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 719 |
SIZEOF_NODE(tree)); |
| 720 |
|
| 721 |
memcpy(node->value, value, tree->sizeof_value); |
| 722 |
node->parent = node->left = node->right = tree->nil; |
| 723 |
|
| 724 |
/* Insert in the tree in the usual way. */ |
| 725 |
rbt_tree_insert(tree, key, node); |
| 726 |
rbt_balance_tree(tree, node); |
| 727 |
|
| 728 |
++tree->n_nodes; |
| 729 |
|
| 730 |
return (node); |
| 731 |
} |
| 732 |
|
| 733 |
/** Add a new node to the tree, useful for data that is pre-sorted. |
| 734 |
@return appended node */ |
| 735 |
const ib_rbt_node_t *rbt_add_node(ib_rbt_t *tree, /*!< in: rb tree */ |
| 736 |
ib_rbt_bound_t *parent, /*!< in: bounds */ |
| 737 |
const void *value) /*!< in: this value is |
| 738 |
copied to the node */ |
| 739 |
{ |
| 740 |
ib_rbt_node_t *node; |
| 741 |
|
| 742 |
/* Create the node that will hold the value data */ |
| 743 |
node = (ib_rbt_node_t *)ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 744 |
SIZEOF_NODE(tree)); |
| 745 |
|
| 746 |
memcpy(node->value, value, tree->sizeof_value); |
| 747 |
node->parent = node->left = node->right = tree->nil; |
| 748 |
|
| 749 |
/* If tree is empty */ |
| 750 |
if (parent->last == nullptr) { |
| 751 |
parent->last = tree->root; |
| 752 |
} |
| 753 |
|
| 754 |
/* Append the node, the hope here is that the caller knows |
| 755 |
what s/he is doing. */ |
| 756 |
rbt_tree_add_child(tree, parent, node); |
| 757 |
rbt_balance_tree(tree, node); |
| 758 |
|
| 759 |
++tree->n_nodes; |
| 760 |
|
| 761 |
#if (defined IB_RBT_TESTING) |
| 762 |
ut_a(rbt_validate(tree)); |
| 763 |
#endif |
| 764 |
return (node); |
| 765 |
} |
| 766 |
|
| 767 |
/** Find a matching node in the rb tree. |
| 768 |
@return NULL if not found else the node where key was found */ |
| 769 |
static const ib_rbt_node_t *rbt_lookup( |
| 770 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 771 |
const void *key) /*!< in: key to use for search */ |
| 772 |
{ |
| 773 |
const ib_rbt_node_t *current = ROOT(tree); |
| 774 |
|
| 775 |
/* Regular binary search. */ |
| 776 |
while (current != tree->nil) { |
| 777 |
int result; |
| 778 |
|
| 779 |
if (tree->cmp_arg) { |
| 780 |
result = tree->compare_with_arg(tree->cmp_arg, key, current->value); |
| 781 |
} else { |
| 782 |
result = tree->compare(key, current->value); |
| 783 |
} |
| 784 |
|
| 785 |
if (result < 0) { |
| 786 |
current = current->left; |
| 787 |
} else if (result > 0) { |
| 788 |
current = current->right; |
| 789 |
} else { |
| 790 |
break; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
return (current != tree->nil ? current : nullptr); |
| 795 |
} |
| 796 |
|
| 797 |
/** Delete a node identified by key. |
| 798 |
@return true if success false if not found */ |
| 799 |
bool rbt_delete(ib_rbt_t *tree, /*!< in: rb tree */ |
| 800 |
const void *key) /*!< in: key to delete */ |
| 801 |
{ |
| 802 |
bool deleted = false; |
| 803 |
ib_rbt_node_t *node = (ib_rbt_node_t *)rbt_lookup(tree, key); |
| 804 |
|
| 805 |
if (node) { |
| 806 |
rbt_remove_node_and_rebalance(tree, node); |
| 807 |
|
| 808 |
ut::free(node); |
| 809 |
deleted = true; |
| 810 |
} |
| 811 |
|
| 812 |
return deleted; |
| 813 |
} |
| 814 |
|
| 815 |
/** Remove a node from the rb tree, the node is not free'd, that is the |
| 816 |
callers responsibility. |
| 817 |
@return deleted node but without the const */ |
| 818 |
ib_rbt_node_t *rbt_remove_node( |
| 819 |
ib_rbt_t *tree, /*!< in: rb tree */ |
| 820 |
const ib_rbt_node_t *const_node) /*!< in: node to delete, this |
| 821 |
is a fudge and declared const |
| 822 |
because the caller can access |
| 823 |
only const nodes */ |
| 824 |
{ |
| 825 |
/* Cast away the const. */ |
| 826 |
rbt_remove_node_and_rebalance(tree, (ib_rbt_node_t *)const_node); |
| 827 |
|
| 828 |
/* This is to make it easier to do something like this: |
| 829 |
ut::free(rbt_remove_node(node)); |
| 830 |
*/ |
| 831 |
|
| 832 |
return ((ib_rbt_node_t *)const_node); |
| 833 |
} |
| 834 |
|
| 835 |
/** Find the node that has the greatest key that is <= key. |
| 836 |
@return value of result */ |
| 837 |
int rbt_search(const ib_rbt_t *tree, /*!< in: rb tree */ |
| 838 |
ib_rbt_bound_t *parent, /*!< in: search bounds */ |
| 839 |
const void *key) /*!< in: key to search */ |
| 840 |
{ |
| 841 |
ib_rbt_node_t *current = ROOT(tree); |
| 842 |
|
| 843 |
/* Every thing is greater than the NULL root. */ |
| 844 |
parent->result = 1; |
| 845 |
parent->last = nullptr; |
| 846 |
|
| 847 |
while (current != tree->nil) { |
| 848 |
parent->last = current; |
| 849 |
|
| 850 |
if (tree->cmp_arg) { |
| 851 |
parent->result = |
| 852 |
tree->compare_with_arg(tree->cmp_arg, key, current->value); |
| 853 |
} else { |
| 854 |
parent->result = tree->compare(key, current->value); |
| 855 |
} |
| 856 |
|
| 857 |
if (parent->result > 0) { |
| 858 |
current = current->right; |
| 859 |
} else if (parent->result < 0) { |
| 860 |
current = current->left; |
| 861 |
} else { |
| 862 |
break; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
return (parent->result); |
| 867 |
} |
| 868 |
|
| 869 |
/** Find the node that has the greatest key that is <= key. But use the |
| 870 |
supplied comparison function. |
| 871 |
@return value of result */ |
| 872 |
int rbt_search_cmp(const ib_rbt_t *tree, /*!< in: rb tree */ |
| 873 |
ib_rbt_bound_t *parent, /*!< in: search bounds */ |
| 874 |
const void *key, /*!< in: key to search */ |
| 875 |
ib_rbt_compare compare, /*!< in: fn to compare items */ |
| 876 |
ib_rbt_arg_compare arg_compare) /*!< in: fn to compare items |
| 877 |
with argument */ |
| 878 |
{ |
| 879 |
ib_rbt_node_t *current = ROOT(tree); |
| 880 |
|
| 881 |
/* Every thing is greater than the NULL root. */ |
| 882 |
parent->result = 1; |
| 883 |
parent->last = nullptr; |
| 884 |
|
| 885 |
while (current != tree->nil) { |
| 886 |
parent->last = current; |
| 887 |
|
| 888 |
if (arg_compare) { |
| 889 |
ut_ad(tree->cmp_arg); |
| 890 |
parent->result = arg_compare(tree->cmp_arg, key, current->value); |
| 891 |
} else { |
| 892 |
parent->result = compare(key, current->value); |
| 893 |
} |
| 894 |
|
| 895 |
if (parent->result > 0) { |
| 896 |
current = current->right; |
| 897 |
} else if (parent->result < 0) { |
| 898 |
current = current->left; |
| 899 |
} else { |
| 900 |
break; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
return (parent->result); |
| 905 |
} |
| 906 |
|
| 907 |
/** Return the left most node in the tree. */ |
| 908 |
const ib_rbt_node_t *rbt_first( |
| 909 |
/* out leftmost node or NULL */ |
| 910 |
const ib_rbt_t *tree) /* in: rb tree */ |
| 911 |
{ |
| 912 |
ib_rbt_node_t *first = nullptr; |
| 913 |
ib_rbt_node_t *current = ROOT(tree); |
| 914 |
|
| 915 |
while (current != tree->nil) { |
| 916 |
first = current; |
| 917 |
current = current->left; |
| 918 |
} |
| 919 |
|
| 920 |
return (first); |
| 921 |
} |
| 922 |
|
| 923 |
/** Return the right most node in the tree. |
| 924 |
@return the rightmost node or NULL */ |
| 925 |
const ib_rbt_node_t *rbt_last(const ib_rbt_t *tree) /*!< in: rb tree */ |
| 926 |
{ |
| 927 |
ib_rbt_node_t *last = nullptr; |
| 928 |
ib_rbt_node_t *current = ROOT(tree); |
| 929 |
|
| 930 |
while (current != tree->nil) { |
| 931 |
last = current; |
| 932 |
current = current->right; |
| 933 |
} |
| 934 |
|
| 935 |
return (last); |
| 936 |
} |
| 937 |
|
| 938 |
/** Return the next node. |
| 939 |
@return node next from current */ |
| 940 |
const ib_rbt_node_t *rbt_next( |
| 941 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 942 |
const ib_rbt_node_t *current) /*!< in: current node */ |
| 943 |
{ |
| 944 |
return (current ? rbt_find_successor(tree, current) : nullptr); |
| 945 |
} |
| 946 |
|
| 947 |
/** Return the previous node. |
| 948 |
@return node prev from current */ |
| 949 |
const ib_rbt_node_t *rbt_prev( |
| 950 |
const ib_rbt_t *tree, /*!< in: rb tree */ |
| 951 |
const ib_rbt_node_t *current) /*!< in: current node */ |
| 952 |
{ |
| 953 |
return (current ? rbt_find_predecessor(tree, current) : nullptr); |
| 954 |
} |
| 955 |
|
| 956 |
/** Merge the node from dst into src. Return the number of nodes merged. |
| 957 |
@return no. of recs merged */ |
| 958 |
ulint rbt_merge_uniq(ib_rbt_t *dst, /*!< in: dst rb tree */ |
| 959 |
const ib_rbt_t *src) /*!< in: src rb tree */ |
| 960 |
{ |
| 961 |
ib_rbt_bound_t parent; |
| 962 |
ulint n_merged = 0; |
| 963 |
const ib_rbt_node_t *src_node = rbt_first(src); |
| 964 |
|
| 965 |
if (rbt_empty(src) || dst == src) { |
| 966 |
return (0); |
| 967 |
} |
| 968 |
|
| 969 |
for (/* No op */; src_node; src_node = rbt_next(src, src_node)) { |
| 970 |
if (rbt_search(dst, &parent, src_node->value) != 0) { |
| 971 |
rbt_add_node(dst, &parent, src_node->value); |
| 972 |
++n_merged; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
return (n_merged); |
| 977 |
} |
| 978 |
|
| 979 |
#if defined UNIV_DEBUG || defined IB_RBT_TESTING |
| 980 |
/** Check that every path from the root to the leaves has the same count and |
| 981 |
the tree nodes are in order. |
| 982 |
@return true if OK false otherwise */ |
| 983 |
bool rbt_validate(const ib_rbt_t *tree) /*!< in: RB tree to validate */ |
| 984 |
{ |
| 985 |
if (rbt_count_black_nodes(tree, ROOT(tree)) > 0) { |
| 986 |
return (rbt_check_ordering(tree)); |
| 987 |
} |
| 988 |
|
| 989 |
return false; |
| 990 |
} |
| 991 |
#endif /* UNIV_DEBUG || IB_RBT_TESTING */ |