107template <
template <
typename>
class NodeType,
typename Key,
class Compare>
151 while (p != Node::NullPtr);
179 const Key &
pk =
KEY(p);
191 while (p != Node::NullPtr);
199 assert(p != Node::NullPtr);
222 assert(p != Node::NullPtr);
311 while (
LLINK(succ) != Node::NullPtr)
327 LLINK(p) = Node::NullPtr;
329 if (
RLINK(p) == succ)
450 for (
size_t i = 0; i < sz - 1; ++i)
458 for (
size_t i = 0; i < sz - 1; ++i)
477 std::swap(
root, tree.root);
478 std::swap(
cmp, tree.cmp);
483 rb_stack(Node::MaxHeight),
cmp(std::move(tree.cmp))
486 tree.root = Node::NullPtr;
494 tree.root = Node::NullPtr;
495 cmp = std::move(tree.cmp);
516 assert(p !=
nullptr and p != Node::NullPtr);
519 if (
root == Node::NullPtr)
542 assert(p !=
nullptr and p != Node::NullPtr);
545 if (
root == Node::NullPtr)
568 assert(p !=
nullptr and p != Node::NullPtr);
571 if (
root == Node::NullPtr)
591 if (
root == Node::NullPtr)
607 if (
LLINK(q) == Node::NullPtr)
616 if (
RLINK(q) == Node::NullPtr)
657 std::pair<long, Node*>
position(
const Key & key)
const noexcept
659 std::pair<long, Node*>
ret_val;
674 std::pair<long, Node*>
r(-2,
nullptr);
688 while (p != Node::NullPtr)
780 if (p == Node::NullPtr)
781 return Node::NullPtr;
783 if (
LLINK(p) == Node::NullPtr)
796 if (
LLINK(p) == Node::NullPtr)
814 if (s != Node::NullPtr)
819 p = rotate_left_simple(p);
826 if (s != Node::NullPtr)
849 RLINK(p) = rotate_right_simple(s);
853 p = rotate_left_simple(p);
856 if (
RLINK(p) != Node::NullPtr)
875 return ExtractHelper::extract(p,
dummy);
881 if (p == Node::NullPtr)
882 return Node::NullPtr;
884 if (
RLINK(p) == Node::NullPtr)
896 if (
RLINK(p) == Node::NullPtr)
912 if (s != Node::NullPtr)
916 p = rotate_right_simple(p);
922 if (s != Node::NullPtr)
942 LLINK(p) = rotate_left_simple(s);
945 p = rotate_right_simple(p);
948 if (
LLINK(p) != Node::NullPtr)
967 return ExtractHelper::extract(p,
dummy);
999 if (
t1 == Node::NullPtr)
1070 if (
t2 == Node::NullPtr)
1135 if (
t1 == Node::NullPtr)
1137 if (
t2 == Node::NullPtr)
1145 if (
t1 != Node::NullPtr)
1153 if (
t2 != Node::NullPtr)
1166 if (p == Node::NullPtr)
1168 t1 =
t2 = Node::NullPtr;
1169 return Node::NullPtr;
1178 LLINK(p) = Node::NullPtr;
1183 RLINK(p) = Node::NullPtr;
1188 if (
t2 != Node::NullPtr)
1191 else if (
cmp(
KEY(p), key))
1196 RLINK(p) = Node::NullPtr;
1201 LLINK(p) = Node::NullPtr;
1206 if (
t1 != Node::NullPtr)
1217 if (
t1 != Node::NullPtr)
1219 if (
t2 != Node::NullPtr)
1230 if (p == Node::NullPtr)
1232 t1 =
t2 = Node::NullPtr;
1241 LLINK(p) = Node::NullPtr;
1246 RLINK(p) = Node::NullPtr;
1251 if (
t2 != Node::NullPtr)
1259 RLINK(p) = Node::NullPtr;
1264 LLINK(p) = Node::NullPtr;
1269 if (
t1 != Node::NullPtr)
1277 if (p == Node::NullPtr)
1279 t1 =
t2 = Node::NullPtr;
1290 LLINK(p) = Node::NullPtr;
1295 RLINK(p) = Node::NullPtr;
1300 if (
t2 != Node::NullPtr)
1308 RLINK(p) = Node::NullPtr;
1313 LLINK(p) = Node::NullPtr;
1318 if (
t1 != Node::NullPtr)
1335 if (t.root == Node::NullPtr)
1338 if (
root == Node::NullPtr)
1341 t.root = Node::NullPtr;
1349 t.root = Node::NullPtr;
1361 if (right != Node::NullPtr)
1363 if (left != Node::NullPtr)
1384 if (
root == Node::NullPtr)
1388 Node * found =
nullptr;
1391 root = Node::NullPtr;
1399 if (right != Node::NullPtr)
1401 if (left != Node::NullPtr)
1408 else if (
cmp(key,
KEY(p)))
1432 if (
root == Node::NullPtr)
1437 root = Node::NullPtr;
1445 if (right != Node::NullPtr)
1447 if (left != Node::NullPtr)
1474 if (
root == Node::NullPtr)
1480 root = Node::NullPtr;
1487 root = Node::NullPtr;
1495 root = Node::NullPtr;
1500 while (curr != Node::NullPtr)
1504 LLINK(curr) = Node::NullPtr;
1535 using Base::operator=;
1548template <
typename Key,
class Compare = Aleph::less<Key>>
1565template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts for constraining comparison functors.
Exception handling system with formatted messages for Aleph-w.
Standard functor implementations and comparison objects.
Base iterator template for ranked binary search trees.
size_t size() const noexcept
Return the number of elements stored in the stack.
T popn(const int &n) noexcept
Perform in constant time n pops.
bool is_empty() const noexcept
Return true if stack is empty.
T pop() noexcept
Pop by moving the top of stack.
T & top() noexcept
Return a modifiable reference to stack's top.
void empty() noexcept
Empty the stack.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
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.
void update_counters_after_insertion() noexcept
static Node * join_left_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
FixedStack< Node * > rb_stack
Node * search_and_stack_rb(const Key &key, signed char &cmp_result) noexcept
Search for key and stack ancestors (optimistic duplicate detection)
size_t size() const noexcept
Gen_Rb_Tree_Rk(Compare cmp_arg=Compare())
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.
static Node * join_right_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
void fix_red_condition(Node *p)
static Node * extract_min_rb(Node *&p) noexcept
static Node * rotate_right_simple(Node *p) noexcept
static constexpr signed char CmpEqual
virtual ~Gen_Rb_Tree_Rk()=default
static void fix_red_violation(Node *&subtree_root) noexcept
Node * insert_dup(Node *p)
void find_succ_and_swap(Node *p, Node *&pp)
std::pair< long, Node * > find_position(const Key &key) const noexcept
Find the inorder position of a key in the tree.
void swap(Gen_Rb_Tree_Rk &tree) 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
Node * search(const Key &key)
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result)
Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
void fix_black_condition(Node *p)
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
Gen_Rb_Tree_Rk & operator=(Gen_Rb_Tree_Rk &&tree) noexcept
Node * search_or_insert(Node *p)
Compare & key_comp() noexcept
void update_counters_after_deletion() noexcept
static Node * rotate_left_simple(Node *p) noexcept
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
void split_key_dup_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) 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 * split_key_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
Gen_Rb_Tree_Rk(Gen_Rb_Tree_Rk &&tree) noexcept
Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Compare & get_compare() noexcept
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
static Node * extract_max_rb(Node *&p) noexcept
static size_t black_height(Node *p) noexcept
Strict weak ordering constraint for BST comparators.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
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.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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).