Skip to content

Commit

Permalink
renamed coloring functions
Browse files Browse the repository at this point in the history
  • Loading branch information
caspernorrbin committed Dec 18, 2024
1 parent 3705c1e commit 505c7f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/hotspot/share/utilities/rbTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class RBTree {
bool is_black() const { return _color == BLACK; }
bool is_red() const { return _color == RED; }

void color_black() { _color = BLACK; }
void color_red() { _color = RED; }
void set_black() { _color = BLACK; }
void set_red() { _color = RED; }

RBNode(const K& k, const V& v)
: _parent(nullptr), _left(nullptr), _right(nullptr),
Expand Down Expand Up @@ -229,7 +229,7 @@ class RBTree {
}

// Finds the value associated with the key
V* find(K& key) {
V* find(const K& key) {
RBNode* node = find_node(_root, key);
if (node == nullptr) {
return nullptr;
Expand Down
40 changes: 19 additions & 21 deletions src/hotspot/share/utilities/rbTree.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::fix_insert_violations(RBNode* n
RBNode* grandparent = parent->_parent;
if (grandparent == nullptr) { // Parent is the tree root
assert(parent == _root, "parent must be root");
parent->color_black(); // Color parent black to eliminate the red-violation
parent->set_black(); // Color parent black to eliminate the red-violation
return;
}

Expand All @@ -253,8 +253,8 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::fix_insert_violations(RBNode* n
}

// Swap parent and grandparent colors to eliminate the red-violation
parent->color_black();
grandparent->color_red();
parent->set_black();
grandparent->set_red();

if (_root == grandparent) {
_root = parent;
Expand All @@ -265,9 +265,9 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::fix_insert_violations(RBNode* n

// Parent and uncle are both red
// Paint both black, paint grandparent red to not create a black-violation
parent->color_black();
uncle->color_black();
grandparent->color_red();
parent->set_black();
uncle->set_black();
grandparent->set_red();

// Move up two levels to check for new potential red-violation
node = grandparent;
Expand All @@ -288,8 +288,8 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_black_leaf(RBNode* node)
assert(is_black(sibling->_left), "nephew must be black");
assert(is_black(sibling->_right), "nephew must be black");
// Swap parent and sibling colors
parent->color_red();
sibling->color_black();
parent->set_red();
sibling->set_black();

// Rotate parent down and sibling up
if (node->is_left_child()) {
Expand Down Expand Up @@ -320,8 +320,8 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_black_leaf(RBNode* node)
distant_nephew = sibling;
sibling = close_nephew;

distant_nephew->color_red();
sibling->color_black();
distant_nephew->set_red();
sibling->set_black();
}

// Distant nephew red
Expand All @@ -337,29 +337,27 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_black_leaf(RBNode* node)

// Swap parent and sibling colors
if (parent->is_black()) {
sibling->color_black();
sibling->set_black();
} else {
sibling->color_red();
}
parent->color_black();
if (sibling->is_black()) {
sibling->set_red();
}
parent->set_black();

// Color distant nephew black to restore black balance
distant_nephew->color_black();
distant_nephew->set_black();
return;
}

if (is_red(parent)) { // parent red, sibling and nephews black
// Swap parent and sibling colors to restore black balance
sibling->color_red();
parent->color_black();
sibling->set_red();
parent->set_black();
return;
}

// Parent, sibling, and both nephews black
// Color sibling red and move up one level
sibling->color_red();
sibling->set_red();
node = parent;
parent = node->_parent;
}
Expand All @@ -376,7 +374,7 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_from_tree(RBNode* node)
assert(right == nullptr, "right must be nullptr");
assert(is_black(node), "node must be black");
assert(is_red(left), "child must be red");
left->color_black();
left->set_black();
left->_parent = parent;
if (parent == nullptr) {
assert(node == _root, "node must be root");
Expand All @@ -390,7 +388,7 @@ inline void RBTree<K, V, COMPARATOR, ALLOCATOR>::remove_from_tree(RBNode* node)
assert(left == nullptr, "left must be nullptr");
assert(is_black(node), "node must be black");
assert(is_red(right), "child must be red");
right->color_black();
right->set_black();
right->_parent = parent;
if (parent == nullptr) {
assert(node == _root, "node must be root");
Expand Down

0 comments on commit 505c7f8

Please sign in to comment.