|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef SHARE_UTILITIES_RBTREE_INLINE_HPP |
| 26 | +#define SHARE_UTILITIES_RBTREE_INLINE_HPP |
| 27 | + |
| 28 | +#include "utilities/debug.hpp" |
| 29 | +#include "utilities/globalDefinitions.hpp" |
| 30 | +#include "utilities/powerOfTwo.hpp" |
| 31 | +#include "utilities/rbTree.hpp" |
| 32 | + |
| 33 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 34 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::replace_child( |
| 35 | + RBNode* old_child, RBNode* new_child) { |
| 36 | + if (_left == old_child) { |
| 37 | + _left = new_child; |
| 38 | + } else if (_right == old_child) { |
| 39 | + _right = new_child; |
| 40 | + } else { |
| 41 | + ShouldNotReachHere(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 46 | +inline typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 47 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::rotate_left() { |
| 48 | + // This node down, right child up |
| 49 | + RBNode* old_right = _right; |
| 50 | + |
| 51 | + _right = old_right->_left; |
| 52 | + if (_right != nullptr) { |
| 53 | + _right->set_parent(this); |
| 54 | + } |
| 55 | + |
| 56 | + old_right->set_parent(parent()); |
| 57 | + if (parent() != nullptr) { |
| 58 | + parent()->replace_child(this, old_right); |
| 59 | + } |
| 60 | + |
| 61 | + old_right->_left = this; |
| 62 | + set_parent(old_right); |
| 63 | + |
| 64 | + return old_right; |
| 65 | +} |
| 66 | + |
| 67 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 68 | +inline typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 69 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::rotate_right() { |
| 70 | + // This node down, left child up |
| 71 | + RBNode* old_left = _left; |
| 72 | + |
| 73 | + _left = old_left->_right; |
| 74 | + if (_left != nullptr) { |
| 75 | + _left->set_parent(this); |
| 76 | + } |
| 77 | + |
| 78 | + old_left->set_parent(parent()); |
| 79 | + if (parent() != nullptr) { |
| 80 | + parent()->replace_child(this, old_left); |
| 81 | + } |
| 82 | + |
| 83 | + old_left->_right = this; |
| 84 | + set_parent(old_left); |
| 85 | + |
| 86 | + return old_left; |
| 87 | +} |
| 88 | + |
| 89 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 90 | +inline typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 91 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::prev() { |
| 92 | + RBNode* node = this; |
| 93 | + if (_left != nullptr) { // right subtree exists |
| 94 | + node = _left; |
| 95 | + while (node->_right != nullptr) { |
| 96 | + node = node->_right; |
| 97 | + } |
| 98 | + return node; |
| 99 | + } |
| 100 | + |
| 101 | + while (node != nullptr && node->is_left_child()) { |
| 102 | + node = node->parent(); |
| 103 | + } |
| 104 | + return node->parent(); |
| 105 | +} |
| 106 | + |
| 107 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 108 | +inline typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 109 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::next() { |
| 110 | + RBNode* node = this; |
| 111 | + if (_right != nullptr) { // right subtree exists |
| 112 | + node = _right; |
| 113 | + while (node->_left != nullptr) { |
| 114 | + node = node->_left; |
| 115 | + } |
| 116 | + return node; |
| 117 | + } |
| 118 | + |
| 119 | + while (node != nullptr && node->is_right_child()) { |
| 120 | + node = node->parent(); |
| 121 | + } |
| 122 | + return node->parent(); |
| 123 | +} |
| 124 | + |
| 125 | +#ifdef ASSERT |
| 126 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 127 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode::verify( |
| 128 | + size_t& num_nodes, size_t& black_nodes_until_leaf, size_t& shortest_leaf_path, size_t& longest_leaf_path, |
| 129 | + size_t& tree_depth, bool expect_visited) { |
| 130 | + assert(expect_visited != _visited, "node already visited"); |
| 131 | + _visited = !_visited; |
| 132 | + |
| 133 | + size_t num_black_nodes_left = 0; |
| 134 | + size_t shortest_leaf_path_left = 0; |
| 135 | + size_t longest_leaf_path_left = 0; |
| 136 | + size_t tree_depth_left = 0; |
| 137 | + |
| 138 | + if (_left != nullptr) { |
| 139 | + if (_right == nullptr) { |
| 140 | + assert(is_black() && _left->is_red(), "if one child it must be red and node black"); |
| 141 | + } |
| 142 | + assert(COMPARATOR::cmp(_left->key(), _key) < 0, "left node must be less than parent"); |
| 143 | + assert(is_black() || _left->is_black(), "2 red nodes in a row"); |
| 144 | + assert(_left->parent() == this, "pointer mismatch"); |
| 145 | + _left->verify(num_nodes, num_black_nodes_left, shortest_leaf_path_left, |
| 146 | + longest_leaf_path_left, tree_depth_left, expect_visited); |
| 147 | + } |
| 148 | + |
| 149 | + size_t num_black_nodes_right = 0; |
| 150 | + size_t shortest_leaf_path_right = 0; |
| 151 | + size_t longest_leaf_path_right = 0; |
| 152 | + size_t tree_depth_right = 0; |
| 153 | + |
| 154 | + if (_right != nullptr) { |
| 155 | + if (_left == nullptr) { |
| 156 | + assert(is_black() && _right->is_red(), "if one child it must be red and node black"); |
| 157 | + } |
| 158 | + assert(COMPARATOR::cmp(_right->key(), _key) > 0, "right node must be greater than parent"); |
| 159 | + assert(is_black() || _left->is_black(), "2 red nodes in a row"); |
| 160 | + assert(_right->parent() == this, "pointer mismatch"); |
| 161 | + _right->verify(num_nodes, num_black_nodes_right, shortest_leaf_path_right, |
| 162 | + longest_leaf_path_right, tree_depth_right, expect_visited); |
| 163 | + } |
| 164 | + |
| 165 | + shortest_leaf_path = MAX2(longest_leaf_path_left, longest_leaf_path_right); |
| 166 | + longest_leaf_path = MAX2(longest_leaf_path_left, longest_leaf_path_right); |
| 167 | + |
| 168 | + assert(shortest_leaf_path <= longest_leaf_path && longest_leaf_path <= shortest_leaf_path * 2, |
| 169 | + "tree imbalanced, shortest path: %zu longest: %zu", shortest_leaf_path, longest_leaf_path); |
| 170 | + assert(num_black_nodes_left == num_black_nodes_right, |
| 171 | + "number of black nodes in left/right subtree should match"); |
| 172 | + |
| 173 | + num_nodes++; |
| 174 | + tree_depth = 1 + MAX2(tree_depth_left, tree_depth_right); |
| 175 | + |
| 176 | + shortest_leaf_path++; |
| 177 | + longest_leaf_path++; |
| 178 | + |
| 179 | + black_nodes_until_leaf = num_black_nodes_left; |
| 180 | + if (is_black()) { |
| 181 | + black_nodes_until_leaf++; |
| 182 | + } |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +#endif // ASSERT |
| 187 | + |
| 188 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 189 | +inline const typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 190 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::find_node(const K& key) const { |
| 191 | + RBNode* curr = _root; |
| 192 | + while (curr != nullptr) { |
| 193 | + const int key_cmp_k = COMPARATOR::cmp(key, curr->key()); |
| 194 | + |
| 195 | + if (key_cmp_k == 0) { |
| 196 | + return curr; |
| 197 | + } else if (key_cmp_k < 0) { |
| 198 | + curr = curr->_left; |
| 199 | + } else { |
| 200 | + curr = curr->_right; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return nullptr; |
| 205 | +} |
| 206 | + |
| 207 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 208 | +inline typename RBTree<K, V, COMPARATOR, ALLOCATOR>::RBNode* |
| 209 | +RBTree<K, V, COMPARATOR, ALLOCATOR>::insert_node(const K& key, const V& val) { |
| 210 | + RBNode* curr = _root; |
| 211 | + if (curr == nullptr) { // Tree is empty |
| 212 | + _root = allocate_node(key, val); |
| 213 | + return _root; |
| 214 | + } |
| 215 | + |
| 216 | + RBNode* parent = nullptr; |
| 217 | + while (curr != nullptr) { |
| 218 | + const int key_cmp_k = COMPARATOR::cmp(key, curr->key()); |
| 219 | + |
| 220 | + if (key_cmp_k == 0) { |
| 221 | + curr->_value = val; |
| 222 | + return curr; |
| 223 | + } |
| 224 | + |
| 225 | + parent = curr; |
| 226 | + if (key_cmp_k < 0) { |
| 227 | + curr = curr->_left; |
| 228 | + } else { |
| 229 | + curr = curr->_right; |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + // Create and insert new node |
| 234 | + RBNode* node = allocate_node(key, val); |
| 235 | + node->set_parent(parent); |
| 236 | + |
| 237 | + const int key_cmp_k = COMPARATOR::cmp(key, parent->key()); |
| 238 | + if (key_cmp_k < 0) { |
| 239 | + parent->_left = node; |
| 240 | + } else { |
| 241 | + parent->_right = node; |
| 242 | + } |
| 243 | + |
| 244 | + return node; |
| 245 | +} |
| 246 | + |
| 247 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 248 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::fix_insert_violations(RBNode* node) { |
| 249 | + if (node->is_black()) { // node's value was updated |
| 250 | + return; // Tree is already correct |
| 251 | + } |
| 252 | + |
| 253 | + RBNode* parent = node->parent(); |
| 254 | + while (parent != nullptr && parent->is_red()) { |
| 255 | + // Node and parent are both red, creating a red-violation |
| 256 | + |
| 257 | + RBNode* grandparent = parent->parent(); |
| 258 | + if (grandparent == nullptr) { // Parent is the tree root |
| 259 | + assert(parent == _root, "parent must be root"); |
| 260 | + parent->set_black(); // Color parent black to eliminate the red-violation |
| 261 | + return; |
| 262 | + } |
| 263 | + |
| 264 | + RBNode* uncle = parent->is_left_child() ? grandparent->_right : grandparent->_left; |
| 265 | + if (is_black(uncle)) { // Parent is red, uncle is black |
| 266 | + // Rotate the parent to the position of the grandparent |
| 267 | + if (parent->is_left_child()) { |
| 268 | + if (node->is_right_child()) { // Node is an "inner" node |
| 269 | + // Rotate and swap node and parent to make it an "outer" node |
| 270 | + parent->rotate_left(); |
| 271 | + parent = node; |
| 272 | + } |
| 273 | + grandparent->rotate_right(); // Rotate the parent to the position of the grandparent |
| 274 | + } else if (parent->is_right_child()) { |
| 275 | + if (node->is_left_child()) { // Node is an "inner" node |
| 276 | + // Rotate and swap node and parent to make it an "outer" node |
| 277 | + parent->rotate_right(); |
| 278 | + parent = node; |
| 279 | + } |
| 280 | + grandparent->rotate_left(); // Rotate the parent to the position of the grandparent |
| 281 | + } |
| 282 | + |
| 283 | + // Swap parent and grandparent colors to eliminate the red-violation |
| 284 | + parent->set_black(); |
| 285 | + grandparent->set_red(); |
| 286 | + |
| 287 | + if (_root == grandparent) { |
| 288 | + _root = parent; |
| 289 | + } |
| 290 | + |
| 291 | + return; |
| 292 | + } |
| 293 | + |
| 294 | + // Parent and uncle are both red |
| 295 | + // Paint both black, paint grandparent red to not create a black-violation |
| 296 | + parent->set_black(); |
| 297 | + uncle->set_black(); |
| 298 | + grandparent->set_red(); |
| 299 | + |
| 300 | + // Move up two levels to check for new potential red-violation |
| 301 | + node = grandparent; |
| 302 | + parent = grandparent->parent(); |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 307 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_black_leaf(RBNode* node) { |
| 308 | + // Black node removed, balancing needed |
| 309 | + RBNode* parent = node->parent(); |
| 310 | + while (parent != nullptr) { |
| 311 | + // Sibling must exist. If it did not, node would need to be red to not break |
| 312 | + // tree properties, and could be trivially removed before reaching here |
| 313 | + RBNode* sibling = node->is_left_child() ? parent->_right : parent->_left; |
| 314 | + if (is_red(sibling)) { // Sibling red, parent and nephews must be black |
| 315 | + assert(is_black(parent), "parent must be black"); |
| 316 | + assert(is_black(sibling->_left), "nephew must be black"); |
| 317 | + assert(is_black(sibling->_right), "nephew must be black"); |
| 318 | + // Swap parent and sibling colors |
| 319 | + parent->set_red(); |
| 320 | + sibling->set_black(); |
| 321 | + |
| 322 | + // Rotate parent down and sibling up |
| 323 | + if (node->is_left_child()) { |
| 324 | + parent->rotate_left(); |
| 325 | + sibling = parent->_right; |
| 326 | + } else { |
| 327 | + parent->rotate_right(); |
| 328 | + sibling = parent->_left; |
| 329 | + } |
| 330 | + |
| 331 | + if (_root == parent) { |
| 332 | + _root = parent->parent(); |
| 333 | + } |
| 334 | + // Further balancing needed |
| 335 | + } |
| 336 | + |
| 337 | + RBNode* close_nephew = node->is_left_child() ? sibling->_left : sibling->_right; |
| 338 | + RBNode* distant_nephew = node->is_left_child() ? sibling->_right : sibling->_left; |
| 339 | + if (is_red(distant_nephew) || is_red(close_nephew)) { |
| 340 | + if (is_black(distant_nephew)) { // close red, distant black |
| 341 | + // Rotate sibling down and inner nephew up |
| 342 | + if (node->is_left_child()) { |
| 343 | + sibling->rotate_right(); |
| 344 | + } else { |
| 345 | + sibling->rotate_left(); |
| 346 | + } |
| 347 | + |
| 348 | + distant_nephew = sibling; |
| 349 | + sibling = close_nephew; |
| 350 | + |
| 351 | + distant_nephew->set_red(); |
| 352 | + sibling->set_black(); |
| 353 | + } |
| 354 | + |
| 355 | + // Distant nephew red |
| 356 | + // Rotate parent down and sibling up |
| 357 | + if (node->is_left_child()) { |
| 358 | + parent->rotate_left(); |
| 359 | + } else { |
| 360 | + parent->rotate_right(); |
| 361 | + } |
| 362 | + if (_root == parent) { |
| 363 | + _root = sibling; |
| 364 | + } |
| 365 | + |
| 366 | + // Swap parent and sibling colors |
| 367 | + if (parent->is_black()) { |
| 368 | + sibling->set_black(); |
| 369 | + } else { |
| 370 | + sibling->set_red(); |
| 371 | + } |
| 372 | + parent->set_black(); |
| 373 | + |
| 374 | + // Color distant nephew black to restore black balance |
| 375 | + distant_nephew->set_black(); |
| 376 | + return; |
| 377 | + } |
| 378 | + |
| 379 | + if (is_red(parent)) { // parent red, sibling and nephews black |
| 380 | + // Swap parent and sibling colors to restore black balance |
| 381 | + sibling->set_red(); |
| 382 | + parent->set_black(); |
| 383 | + return; |
| 384 | + } |
| 385 | + |
| 386 | + // Parent, sibling, and both nephews black |
| 387 | + // Color sibling red and move up one level |
| 388 | + sibling->set_red(); |
| 389 | + node = parent; |
| 390 | + parent = node->parent(); |
| 391 | + } |
| 392 | +} |
| 393 | + |
| 394 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 395 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_from_tree(RBNode* node) { |
| 396 | + RBNode* parent = node->parent(); |
| 397 | + RBNode* left = node->_left; |
| 398 | + RBNode* right = node->_right; |
| 399 | + if (left != nullptr) { // node has a left only-child |
| 400 | + // node must be black, and child red, otherwise a black-violation would |
| 401 | + // exist Remove node and color the child black. |
| 402 | + assert(right == nullptr, "right must be nullptr"); |
| 403 | + assert(is_black(node), "node must be black"); |
| 404 | + assert(is_red(left), "child must be red"); |
| 405 | + left->set_black(); |
| 406 | + left->set_parent(parent); |
| 407 | + if (parent == nullptr) { |
| 408 | + assert(node == _root, "node must be root"); |
| 409 | + _root = left; |
| 410 | + } else { |
| 411 | + parent->replace_child(node, left); |
| 412 | + } |
| 413 | + } else if (right != nullptr) { // node has a right only-child |
| 414 | + // node must be black, and child red, otherwise a black-violation would |
| 415 | + // exist Remove node and color the child black. |
| 416 | + assert(left == nullptr, "left must be nullptr"); |
| 417 | + assert(is_black(node), "node must be black"); |
| 418 | + assert(is_red(right), "child must be red"); |
| 419 | + right->set_black(); |
| 420 | + right->set_parent(parent); |
| 421 | + if (parent == nullptr) { |
| 422 | + assert(node == _root, "node must be root"); |
| 423 | + _root = right; |
| 424 | + } else { |
| 425 | + parent->replace_child(node, right); |
| 426 | + } |
| 427 | + } else { // node has no children |
| 428 | + if (node == _root) { // Tree empty |
| 429 | + _root = nullptr; |
| 430 | + } else { |
| 431 | + if (is_black(node)) { |
| 432 | + // Removed node is black, creating a black imbalance |
| 433 | + remove_black_leaf(node); |
| 434 | + } |
| 435 | + parent->replace_child(node, nullptr); |
| 436 | + } |
| 437 | + } |
| 438 | +} |
| 439 | + |
| 440 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 441 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove(RBNode* node) { |
| 442 | + assert(node != nullptr, "must be"); |
| 443 | + |
| 444 | + if (node->_left != nullptr && node->_right != nullptr) { // node has two children |
| 445 | + // Swap place with the in-order successor and delete there instead |
| 446 | + RBNode* curr = node->_right; |
| 447 | + while (curr->_left != nullptr) { |
| 448 | + curr = curr->_left; |
| 449 | + } |
| 450 | + |
| 451 | + if (_root == node) _root = curr; |
| 452 | + |
| 453 | + swap(curr->_left, node->_left); |
| 454 | + swap(curr->_parent, node->_parent); // Swaps parent and color |
| 455 | + |
| 456 | + // If node is curr's parent, parent and right pointers become invalid |
| 457 | + if (node->_right == curr) { |
| 458 | + node->_right = curr->_right; |
| 459 | + node->set_parent(curr); |
| 460 | + curr->_right = node; |
| 461 | + } else { |
| 462 | + swap(curr->_right, node->_right); |
| 463 | + node->parent()->replace_child(curr, node); |
| 464 | + curr->_right->set_parent(curr); |
| 465 | + } |
| 466 | + |
| 467 | + if (curr->parent() != nullptr) curr->parent()->replace_child(node, curr); |
| 468 | + curr->_left->set_parent(curr); |
| 469 | + |
| 470 | + |
| 471 | + if (node->_left != nullptr) node->_left->set_parent(node); |
| 472 | + if (node->_right != nullptr) node->_right->set_parent(node); |
| 473 | + } |
| 474 | + |
| 475 | + remove_from_tree(node); |
| 476 | + free_node(node); |
| 477 | +} |
| 478 | + |
| 479 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 480 | +template <typename F> |
| 481 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::visit_in_order(F f) const { |
| 482 | + RBNode* to_visit[64]; |
| 483 | + int stack_idx = 0; |
| 484 | + RBNode* head = _root; |
| 485 | + while (stack_idx > 0 || head != nullptr) { |
| 486 | + while (head != nullptr) { |
| 487 | + to_visit[stack_idx++] = head; |
| 488 | + head = head->_left; |
| 489 | + } |
| 490 | + head = to_visit[--stack_idx]; |
| 491 | + f(head); |
| 492 | + head = head->_right; |
| 493 | + } |
| 494 | +} |
| 495 | + |
| 496 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 497 | +template <typename F> |
| 498 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::visit_range_in_order(const K& from, const K& to, F f) { |
| 499 | + assert(COMPARATOR::cmp(from, to) <= 0, "from must be less or equal to to"); |
| 500 | + RBNode* curr = closest_geq(from); |
| 501 | + if (curr == nullptr) return; |
| 502 | + RBNode* end = closest_geq(to); |
| 503 | + |
| 504 | + while (curr != nullptr && curr != end) { |
| 505 | + f(curr); |
| 506 | + curr = curr->next(); |
| 507 | + } |
| 508 | +} |
| 509 | + |
| 510 | +#ifdef ASSERT |
| 511 | +template <typename K, typename V, typename COMPARATOR, typename ALLOCATOR> |
| 512 | +inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::verify_self() { |
| 513 | + if (_root == nullptr) { |
| 514 | + assert(_num_nodes == 0, "rbtree has nodes but no root"); |
| 515 | + return; |
| 516 | + } |
| 517 | + |
| 518 | + assert(_root->parent() == nullptr, "root of rbtree has a parent"); |
| 519 | + |
| 520 | + size_t num_nodes = 0; |
| 521 | + size_t black_depth = 0; |
| 522 | + size_t tree_depth = 0; |
| 523 | + size_t shortest_leaf_path = 0; |
| 524 | + size_t longest_leaf_path = 0; |
| 525 | + _expected_visited = !_expected_visited; |
| 526 | + |
| 527 | + _root->verify(num_nodes, black_depth, shortest_leaf_path, longest_leaf_path, tree_depth, _expected_visited); |
| 528 | + |
| 529 | + const unsigned int maximum_depth = log2i(size() + 1) * 2; |
| 530 | + |
| 531 | + assert(shortest_leaf_path <= longest_leaf_path && longest_leaf_path <= shortest_leaf_path * 2, |
| 532 | + "tree imbalanced, shortest path: %zu longest: %zu", |
| 533 | + shortest_leaf_path, longest_leaf_path); |
| 534 | + assert(tree_depth <= maximum_depth, "rbtree is too deep"); |
| 535 | + assert(size() == num_nodes, |
| 536 | + "unexpected number of nodes in rbtree. expected: %zu" |
| 537 | + ", actual: %zu", size(), num_nodes); |
| 538 | +} |
| 539 | +#endif // ASSERT |
| 540 | + |
| 541 | +#endif // SHARE_UTILITIES_RBTREE_INLINE_HPP |
0 commit comments