45# ifndef TPL_HTDRBTREERK_H
46# define TPL_HTDRBTREERK_H
92template <
class Key,
class Compare = Aleph::less<Key>>
93 requires StrictWeakOrder<Compare, Key>
122 bool less(
const Key & k1,
const Key & k2)
const
123 noexcept(nothrow_compare_v<Compare, Key>)
124 {
return cmp(k1, k2); }
126 bool equals(
const Key & k1,
const Key & k2)
const
127 noexcept(nothrow_compare_v<Compare, Key>)
128 {
return not
static_cast<bool>(
cmp(k1, k2))
and not
static_cast<bool>(
cmp(k2, k1)); }
208 assert(
LLINK(fffp) == ffp ||
RLINK(fffp) == ffp);
258 noexcept(nothrow_compare_v<Compare, Key>)
263 assert(
COUNT(q) == 1);
265 const Key & qk =
KEY(q);
268 Node* insertPath[128];
269 size_t insertPathLen = 0;
277 insertPath[insertPathLen++] = p;
281 const Key & pk =
KEY(p);
283 if (
equals(qk, pk)) [[unlikely]]
314 insertPath[insertPathLen++] = p;
318 const Key & pk =
KEY(p);
327 for (
size_t i = insertPathLen; i > 0; --i)
329 Node* node = insertPath[i - 1];
342 noexcept(nothrow_compare_v<Compare, Key>)
347 assert(
COUNT(q) == 1);
349 const Key & qk =
KEY(q);
351 Node* insertPath[128];
352 size_t insertPathLen = 0;
360 insertPath[insertPathLen++] = p;
364 const Key & pk =
KEY(p);
394 insertPath[insertPathLen++] = p;
397 const Key & pk =
KEY(p);
405 for (
size_t i = insertPathLen; i > 0; --i)
407 Node* node = insertPath[i - 1];
421 noexcept(nothrow_compare_v<Compare, Key>)
432 const Key & pk =
KEY(p);
459 Node *& refToSearchPath = path.top();
460 assert(refToSearchPath == p);
473 refToSearchPath = succ;
484 if (
RLINK(p) == succ)
512 assert(!path.is_empty());
514 Node *& ffp = path.top();
541 assert(!path.is_empty());
543 Node *ffp = path.top();
562 assert(!path.is_empty());
564 Node *ffp = path.top();
567 if (
LLINK(sp) == snp)
616 assert(path.top() == q);
618 Node *fq = path.top(1);
739 [[nodiscard]]
constexpr const Compare &
get_compare() const noexcept {
return cmp; }
755 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
756 std::is_nothrow_default_constructible_v<Compare>
and
757 std::is_nothrow_constructible_v<Compare, Compare &>)
772 noexcept(std::is_nothrow_default_constructible_v<Node>
and
773 std::is_nothrow_constructible_v<Compare, Compare &>)
794 std::swap(
cmp, tree.cmp);
795 std::swap(
root, tree.root);
816 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
817 bin_node_detail::nothrow_compare_taken_v<Compare>)
825 cmp(bin_node_detail::compare_to_take(tree.
cmp))
840 noexcept(std::is_nothrow_swappable_v<Compare>)
905 const Key & pk =
KEY(p);
907 if (found !=
nullptr) [[unlikely]]
946 Node *
search(
const Key & key)
const noexcept(nothrow_compare_v<Compare, Key>)
951 const Key & pk =
KEY(p);
968 Node *
remove(
const Key & key)
noexcept(nothrow_compare_v<Compare, Key>)
1015 noexcept(nothrow_compare_copy_v<Compare, Key>)
1017 std::pair<long, Node*> ret_val;
1035 noexcept(nothrow_compare_copy_v<Compare, Key>)
1037 std::pair<long, Node*>
r;
1087 if (t1.
size() < pos)
1139template <
class Key,
class Compare = Aleph::less<Key>>
1140 requires StrictWeakOrder<Compare, Key>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for extended binary search trees.
Hybrid RB tree with rank and virtual destructor.
Hybrid top-down/bottom-up red-black tree with rank support.
const Compare & key_comp() const noexcept
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
constexpr const Compare & get_compare() const noexcept
static void colorParentAndSibling(Node *fp, Node *sp) noexcept
static Node * getSibling(Node *p, Node *fp) noexcept
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Node * searchAndBuildPath(const Key &key, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
HtdRbTreeRk & operator=(HtdRbTreeRk &&tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps the nodes and the comparison criteria with tree.
void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path &path) noexcept
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
static void flipColors(Node *p) noexcept
static Node * rotate_to_left_rk(Node *p, Node *fp) noexcept
Left rotation with count update.
virtual ~HtdRbTreeRk()=default
HtdRbTreeRk() 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.
void balanceDownAndColor(Node *p, Node *&fp, Node *&sp, Path &path) noexcept
bool equals(const Key &k1, const Key &k2) const noexcept(nothrow_compare_v< Compare, Key >)
static bool verifyCountsRec(Node *p) noexcept
Compare & key_comp() noexcept
static void updateCountRec(Node *p) noexcept
Recursively update counts for entire subtree - O(n)
Node *& getRoot() noexcept
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position where key would be inserted.
size_t size() const noexcept
O(1) size using root count.
void split_pos(size_t pos, HtdRbTreeRk &t1, HtdRbTreeRk &t2)
Split tree at position.
Node * select(size_t i) const
Select the i-th smallest element (0-indexed).
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Insert with stack tracking for count updates.
HtdRbTreeRk & operator=(const HtdRbTreeRk &)=delete
Node * getRoot() const noexcept
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
HtdRbTreeRk(Compare __cmp) 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.
bool less(const Key &k1, const Key &k2) const noexcept(nothrow_compare_v< Compare, Key >)
void findSuccAndSwap(Node *p, Node *&fp, Path &path) noexcept
void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path &path) noexcept
Compare & get_compare() noexcept
HtdRbTreeRk(HtdRbTreeRk &&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.
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key.
constexpr bool is_empty() const noexcept
void swap(HtdRbTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
HtdRbTreeRk(const HtdRbTreeRk &)=delete
bool verify() const
Verify red-black and rank invariants.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
static Node * rotate_to_right_rk(Node *p, Node *fp) noexcept
Right rotation with count update.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert allowing duplicate keys.
static void colorSiblingAsRed(Node *sp) noexcept
Node * remove_pos(size_t i)
Remove element at position i.
void removeAndFixBlackCondition(Node *q, Path &path) noexcept
Remove node and fix red-black violations.
Stack of fixed capacity whose storage is inside the object.
Extended Red-Black node with subtree counter.
static RbNodeRk *const NullPtr
Minimal std::expected-style result type for C++20.
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.
Red-Black tree node with rank (subtree count).
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Iterator(const HtdRbTreeRk &t) noexcept
Iterator(HtdRbTreeRk &t) noexcept
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Basic binary tree node definitions.
Binary tree operations (split, join, rotate).