107template <
template <
typename>
class NodeType,
typename Key,
class Compare>
108 requires StrictWeakOrder<Compare, Key>
135 signed char & cmp_result,
Path & path)
138 Node *candidate =
nullptr;
139 size_t candidate_pos = 0;
153 candidate_pos = path.
size();
158 while (p != Node::NullPtr);
162 if (candidate !=
nullptr and not
static_cast<bool>(
cmp(
KEY(candidate), key))) [[unlikely]]
166 const size_t to_pop =
167 path.
size() > candidate_pos ?
168 path.
size() - candidate_pos : 0;
178 signed char & cmp_result,
Path & path)
186 const Key & pk =
KEY(p);
198 while (p != Node::NullPtr);
206 assert(p != Node::NullPtr);
207 assert(pp != Node::NullPtr);
229 assert(p != Node::NullPtr);
230 assert(pp != Node::NullPtr);
310 Node *& ref_path_top = path.
top();
316 while (
LLINK(succ) != Node::NullPtr)
332 LLINK(p) = Node::NullPtr;
334 if (
RLINK(p) == succ)
419 if (
LLINK(sp) == snp)
454 const size_t sz = path.size();
455 for (
size_t i = 0; i < sz - 1; ++i)
456 ++
COUNT(path.top(i));
462 const size_t sz = path.size();
463 for (
size_t i = 0; i < sz - 1; ++i)
464 --
COUNT(path.top(i));
489 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
490 std::is_nothrow_default_constructible_v<Compare>
and
491 std::is_nothrow_constructible_v<Compare, Compare &>)
506 noexcept(std::is_nothrow_default_constructible_v<Node>
and
507 std::is_nothrow_constructible_v<Compare, Compare &>)
523 std::swap(
cmp, tree.cmp);
524 std::swap(
root, tree.root);
545 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
546 bin_node_detail::nothrow_compare_taken_v<Compare>)
549 cmp(bin_node_detail::compare_to_take(tree.
cmp))
552 tree.root = Node::NullPtr;
563 noexcept(std::is_nothrow_move_assignable_v<Compare>)
568 cmp = std::move(tree.cmp);
570 tree.root = Node::NullPtr;
579 Node * retVal = Aleph::searchInBinTree<Node, Compare>(
root, key,
cmp);
580 return retVal == Node::NullPtr ? nullptr : retVal;
585 [[nodiscard]]
bool is_empty() const noexcept {
return root == Node::NullPtr; }
591 assert(p !=
nullptr and p != Node::NullPtr);
594 if (
root == Node::NullPtr)
615 assert(p !=
nullptr and p != Node::NullPtr);
618 if (
root == Node::NullPtr)
639 assert(p !=
nullptr and p != Node::NullPtr);
642 if (
root == Node::NullPtr)
663 if (
root == Node::NullPtr)
677 if (
LLINK(q) == Node::NullPtr)
686 if (
RLINK(q) == Node::NullPtr)
730 std::pair<long, Node*>
position(
const Key & key)
const
731 noexcept(nothrow_compare_copy_v<Compare, Key>)
733 std::pair<long, Node*> ret_val;
751 noexcept(nothrow_compare_copy_v<Compare, Key>)
753 std::pair<long, Node*>
r(-2,
nullptr);
767 while (p != Node::NullPtr)
803 if (subtree_root == Node::NullPtr)
807 if (
LLINK(subtree_root) != Node::NullPtr
and
831 if (
RLINK(subtree_root) != Node::NullPtr
and
859 if (p == Node::NullPtr)
860 return Node::NullPtr;
862 if (
LLINK(p) == Node::NullPtr)
873 static Node * extract(
Node *& p,
bool & height_decreased)
noexcept
875 if (
LLINK(p) == Node::NullPtr)
884 bool child_decreased =
false;
885 Node * ret = extract(
LLINK(p), child_decreased);
893 if (s != Node::NullPtr)
898 p = rotate_left_simple(p);
905 if (s != Node::NullPtr)
915 height_decreased =
false;
918 height_decreased =
true;
928 RLINK(p) = rotate_right_simple(s);
932 p = rotate_left_simple(p);
933 COLOR(p) = old_color;
935 if (
RLINK(p) != Node::NullPtr)
937 height_decreased =
false;
947 height_decreased =
false;
954 return ExtractHelper::extract(p, dummy);
960 if (p == Node::NullPtr)
961 return Node::NullPtr;
963 if (
RLINK(p) == Node::NullPtr)
973 static Node * extract(
Node *& p,
bool & height_decreased)
noexcept
975 if (
RLINK(p) == Node::NullPtr)
984 bool child_decreased =
false;
985 Node * ret = extract(
RLINK(p), child_decreased);
991 if (s != Node::NullPtr)
995 p = rotate_right_simple(p);
1001 if (s != Node::NullPtr)
1010 height_decreased =
false;
1013 height_decreased =
true;
1021 LLINK(p) = rotate_left_simple(s);
1024 p = rotate_right_simple(p);
1025 COLOR(p) = old_color;
1027 if (
LLINK(p) != Node::NullPtr)
1029 height_decreased =
false;
1039 height_decreased =
false;
1046 return ExtractHelper::extract(p, dummy);
1053 Node * t2,
size_t bh2)
noexcept
1076 Node * t2,
size_t bh2)
noexcept
1078 if (t1 == Node::NullPtr)
1080 LLINK(pivot) = Node::NullPtr;
1088 size_t curr_bh = bh1;
1117 RLINK(t1) = right_result;
1147 Node * t2,
size_t bh2)
noexcept
1149 if (t2 == Node::NullPtr)
1152 RLINK(pivot) = Node::NullPtr;
1158 size_t curr_bh = bh2;
1182 LLINK(t2) = left_result;
1212 Node * t2,
size_t bh2)
noexcept
1214 if (t1 == Node::NullPtr)
1216 if (t2 == Node::NullPtr)
1224 if (t1 != Node::NullPtr)
1232 if (t2 != Node::NullPtr)
1245 noexcept(nothrow_compare_v<Compare, Key>)
1247 if (p == Node::NullPtr)
1249 t1 = t2 = Node::NullPtr;
1250 return Node::NullPtr;
1259 LLINK(p) = Node::NullPtr;
1260 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ?
black_height(left_t2) : 0;
1264 RLINK(p) = Node::NullPtr;
1269 if (t2 != Node::NullPtr)
1272 else if (
cmp(
KEY(p), key))
1277 RLINK(p) = Node::NullPtr;
1278 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ?
black_height(right_t1) : 0;
1282 LLINK(p) = Node::NullPtr;
1287 if (t1 != Node::NullPtr)
1298 if (t1 != Node::NullPtr)
1300 if (t2 != Node::NullPtr)
1310 noexcept(nothrow_compare_v<Compare, Key>)
1312 if (p == Node::NullPtr)
1314 t1 = t2 = Node::NullPtr;
1323 LLINK(p) = Node::NullPtr;
1324 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ?
black_height(left_t2) : 0;
1328 RLINK(p) = Node::NullPtr;
1333 if (t2 != Node::NullPtr)
1341 RLINK(p) = Node::NullPtr;
1342 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ?
black_height(right_t1) : 0;
1346 LLINK(p) = Node::NullPtr;
1351 if (t1 != Node::NullPtr)
1359 if (p == Node::NullPtr)
1361 t1 = t2 = Node::NullPtr;
1367 if (pos <= left_count)
1372 LLINK(p) = Node::NullPtr;
1373 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ?
black_height(left_t2) : 0;
1377 RLINK(p) = Node::NullPtr;
1382 if (t2 != Node::NullPtr)
1390 RLINK(p) = Node::NullPtr;
1391 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ?
black_height(right_t1) : 0;
1395 LLINK(p) = Node::NullPtr;
1400 if (t1 != Node::NullPtr)
1422 if (t.root == Node::NullPtr)
1425 if (
root == Node::NullPtr)
1428 t.root = Node::NullPtr;
1436 t.root = Node::NullPtr;
1448 if (right != Node::NullPtr)
1450 if (left != Node::NullPtr)
1476 if (
root == Node::NullPtr)
1480 Node * found =
nullptr;
1483 root = Node::NullPtr;
1491 if (right != Node::NullPtr)
1493 if (left != Node::NullPtr)
1500 else if (
cmp(key,
KEY(p)))
1529 if (
root == Node::NullPtr)
1534 root = Node::NullPtr;
1542 if (right != Node::NullPtr)
1544 if (left != Node::NullPtr)
1576 if (
root == Node::NullPtr)
1582 root = Node::NullPtr;
1589 root = Node::NullPtr;
1597 root = Node::NullPtr;
1600 while (curr != Node::NullPtr or not stack.
is_empty())
1602 while (curr != Node::NullPtr)
1606 LLINK(curr) = Node::NullPtr;
1637 using Base::operator=;
1650template <
typename Key,
class Compare = Aleph::less<Key>>
1667template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
Standard functor implementations and comparison objects.
Base iterator template for ranked binary search trees.
Functor encompassing basic operation for extended binary search trees.
Iterator on nodes of the tree.
Red-black tree with rank support (select/position operations).
static Node * join_exclusive_rec_rb(Node *t1, size_t bh1, Node *t2, size_t bh2) noexcept
Node * split_key(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key.
static Node * join_left_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result, Path &path)
size_t size() const noexcept
void fix_red_condition(Node *p, Path &path)
void swap(Gen_Rb_Tree_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
void join_exclusive(Gen_Rb_Tree_Rk &t) noexcept
Join this tree exclusively with another tree.
Node * select(const size_t i) const
Return the i-th node in order sense.
void update_counters_after_insertion(const Path &path) noexcept
static Node * join_right_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
void find_succ_and_swap(Node *p, Node *&pp, Path &path)
static Node * extract_min_rb(Node *&p) noexcept
static Node * rotate_right_simple(Node *p) noexcept
void fix_black_condition(Node *p, Path &path)
static constexpr signed char CmpEqual
virtual ~Gen_Rb_Tree_Rk()=default
static void fix_red_violation(Node *&subtree_root) noexcept
void split_key_dup_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Node * insert_dup(Node *p)
void update_counters_after_deletion(const Path &path) noexcept
bool is_empty() const noexcept
static Node * join_with_pivot_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Gen_Rb_Tree_Rk & operator=(Gen_Rb_Tree_Rk &&tree) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty.
Node * search(const Key &key)
Gen_Rb_Tree_Rk() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
Gen_Rb_Tree_Rk(Gen_Rb_Tree_Rk &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
Node * search_and_stack_rb(const Key &key, signed char &cmp_result, Path &path)
Search for key and stack ancestors (optimistic duplicate detection).
Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
Gen_Rb_Tree_Rk(Compare cmp_arg) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria cmp_arg.
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
void split_pos(size_t pos, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by inorder position.
static constexpr signed char CmpGreater
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find the inorder position of a key in the tree.
Node * search_or_insert(Node *p)
Compare & key_comp() noexcept
static Node * rotate_left_simple(Node *p) noexcept
Node * split_key_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
void split_key_dup(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Node *& getRoot() noexcept
static constexpr signed char CmpLess
Node * remove(const Key &key)
void split_pos_rec_rb(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Compare & get_compare() noexcept
static Node * extract_max_rb(Node *&p) noexcept
static size_t black_height(Node *p) noexcept
Stack of fixed capacity whose storage is inside the object.
size_t size() const noexcept
Return the number of items.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
T popn(const size_t n) noexcept
Perform n pops in constant time.
bool is_empty() const noexcept
Return true if the stack is empty.
Strict weak ordering constraint for BST comparators.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Compute the inorder position of a key.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Main namespace for Aleph-w library functions.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Red-Black tree node with rank (subtree count).
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Red-Black binary search tree with virtual destructor in its nodes and with subtree counters for selec...
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Binary tree operations (split, join, rotate).