106template <
template <
typename>
class NodeType,
typename Key,
class Compare>
107 requires StrictWeakOrder<Compare, Key>
128 return path.size() == 1;
134 noexcept(nothrow_compare_v<Compare, Key>)
139 Node *candidate =
nullptr;
140 size_t candidate_pos = 0;
144 __builtin_prefetch(
LLINK(p));
145 __builtin_prefetch(
RLINK(p));
155 candidate_pos = path.size();
159 }
while (p != Node::NullPtr);
163 if (candidate !=
nullptr and not
static_cast<bool>(
cmp(
KEY(candidate), key))) [[unlikely]]
167 const size_t to_pop = path.size() > candidate_pos ? path.size() - candidate_pos : 0;
177 noexcept(nothrow_compare_v<Compare, Key>)
185 __builtin_prefetch(
LLINK(p));
186 __builtin_prefetch(
RLINK(p));
198 }
while (p != Node::NullPtr);
206 assert(
DIFF(p) == 2);
207 assert(
RLINK(p) != Node::NullPtr);
231 assert(
DIFF(p) == -2);
232 assert(
LLINK(p) != Node::NullPtr);
256 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
277 else if (
DIFF(
r) == -1)
293 [[gnu::always_inline]]
296 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
317 else if (
DIFF(
r) == -1)
342 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
354 if (
DIFF(pc) == -1 or
DIFF(pc) == 0)
363 assert(
DIFF(p) == -2 or
DIFF(p) == 2);
386 Node *pp = path.pop();
400 Node *gpp = path.pop();
401 if (
LLINK(gpp) == pp)
408 if (
DIFF(gpp) == -2 or
DIFF(gpp) == 2)
410 Node *ggpp = path.pop();
421 Node *&ref_to_stack_top = path.top();
428 while (
LLINK(succ) != Node::NullPtr)
435 ref_to_stack_top = succ;
444 LLINK(p) = Node::NullPtr;
446 if (
RLINK(p) == succ)
472 Node *pp = path.top(1);
473 Node *ppp = path.popn(3);
482 if (
DIFF(pp) == -2 or
DIFF(pp) == 2)
488 left_deficit =
LLINK(ppp) == pp;
499 const size_t sz = path.size();
500 for (
size_t i = 0; i < sz - 1; ++i)
501 ++
COUNT(path.top(i));
507 const size_t sz = path.size();
508 for (
size_t i = 0; i < sz - 1; ++i)
509 --
COUNT(path.top(i));
516 [[nodiscard]]
constexpr Compare &
key_comp() noexcept
541 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
542 std::is_nothrow_default_constructible_v<Compare>
and
543 std::is_nothrow_constructible_v<Compare, Compare &>)
558 noexcept(std::is_nothrow_default_constructible_v<Node>
and
559 std::is_nothrow_constructible_v<Compare, Compare &>)
596 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
597 bin_node_detail::nothrow_compare_taken_v<Compare>)
600 cmp(bin_node_detail::compare_to_take(tree.
cmp))
603 tree.root = Node::NullPtr;
617 std::swap(
cmp, tree.cmp);
618 std::swap(
root, tree.root);
636 [[nodiscard]]
size_t size() const noexcept
642 [[nodiscard]]
constexpr bool is_empty() const noexcept
644 return root == Node::NullPtr;
658 noexcept(nothrow_compare_v<Compare, Key>)
660 Node *result = searchInBinTree<Node, Compare>(
root, key,
cmp);
661 return result == Node::NullPtr ? nullptr : result;
675 if (
root == Node::NullPtr)
708 if (
root == Node::NullPtr)
739 if (
root == Node::NullPtr)
766 [[nodiscard]]
Node *
remove(
const Key &key)
noexcept(nothrow_compare_v<Compare, Key>)
768 if (
root == Node::NullPtr)
783 left_deficit =
LLINK(pp) == p;
784 if (
LLINK(p) == Node::NullPtr)
793 if (
RLINK(p) == Node::NullPtr)
838 std::pair<long, Node *>
position(
const Key &key)
const
839 noexcept(nothrow_compare_copy_v<Compare, Key>)
841 std::pair<long, Node *> ret_val;
859 noexcept(nothrow_compare_copy_v<Compare, Key>)
861 std::pair<long, Node *>
r(-2,
nullptr);
894 while (p != Node::NullPtr)
910 if (p == Node::NullPtr)
911 return Node::NullPtr;
913 if (
LLINK(p) == Node::NullPtr)
927 while (
LLINK(*pp) != Node::NullPtr)
939 Node **parent_ptr = stack.
pop();
940 Node *parent = *parent_ptr;
944 if (
DIFF(parent) == 2)
953 else if (
DIFF(parent) == 1)
966 if (p == Node::NullPtr)
967 return Node::NullPtr;
969 if (
RLINK(p) == Node::NullPtr)
982 while (
RLINK(*pp) != Node::NullPtr)
993 Node **parent_ptr = stack.
pop();
994 Node *parent = *parent_ptr;
998 if (
DIFF(parent) == -2)
1006 else if (
DIFF(parent) == -1)
1020 if (t1 == Node::NullPtr)
1022 if (t2 == Node::NullPtr)
1029 if (t1 != Node::NullPtr)
1038 if (t2 != Node::NullPtr)
1049 const size_t h2)
noexcept
1051 if (h1 <= h2 + 1
and h2 <= h1 + 1)
1056 DIFF(pivot) =
static_cast<signed char>(
static_cast<long>(h2) -
static_cast<long>(h1));
1080 while (curr_h > h2 + 1
and *pp != Node::NullPtr)
1095 const size_t left_h = (*pp != Node::NullPtr) ?
avl_height(*pp) : 0;
1096 DIFF(pivot) =
static_cast<signed char>(
static_cast<long>(h2) -
static_cast<long>(left_h));
1103 Node **parent_ptr = stack.
pop();
1104 Node *parent = *parent_ptr;
1109 DIFF(parent) =
static_cast<signed char>(new_right_h) -
static_cast<signed char>(new_left_h);
1111 if (
DIFF(parent) == 2)
1130 while (curr_h > h1 + 1
and *pp != Node::NullPtr)
1143 const size_t right_h = (*pp != Node::NullPtr) ?
avl_height(*pp) : 0;
1144 DIFF(pivot) =
static_cast<signed char>(
static_cast<long>(right_h) -
static_cast<long>(h1));
1150 Node **parent_ptr = stack.
pop();
1151 Node *parent = *parent_ptr;
1156 DIFF(parent) =
static_cast<signed char>(new_right_h) -
static_cast<signed char>(new_left_h);
1158 if (
DIFF(parent) == -2)
1176 noexcept(nothrow_compare_v<Compare, Key>)
1178 if (p == Node::NullPtr)
1180 t1 = t2 = Node::NullPtr;
1181 return Node::NullPtr;
1192 LLINK(p) = Node::NullPtr;
1193 const size_t left_t2_h = (left_t2 != Node::NullPtr) ?
avl_height(left_t2) : 0;
1197 RLINK(p) = Node::NullPtr;
1203 else if (
cmp(
KEY(p), key))
1210 RLINK(p) = Node::NullPtr;
1211 const size_t right_t1_h = (right_t1 != Node::NullPtr) ?
avl_height(right_t1) : 0;
1215 LLINK(p) = Node::NullPtr;
1239 noexcept(nothrow_compare_v<Compare, Key>)
1241 if (p == Node::NullPtr)
1243 t1 = t2 = Node::NullPtr;
1253 LLINK(p) = Node::NullPtr;
1254 const size_t left_t2_h = (left_t2 != Node::NullPtr) ?
avl_height(left_t2) : 0;
1258 RLINK(p) = Node::NullPtr;
1270 RLINK(p) = Node::NullPtr;
1271 const size_t right_t1_h = (right_t1 != Node::NullPtr) ?
avl_height(right_t1) : 0;
1275 LLINK(p) = Node::NullPtr;
1287 if (p == Node::NullPtr)
1289 t1 = t2 = Node::NullPtr;
1293 if (
const size_t left_count =
COUNT(
LLINK(p)); pos <= left_count)
1300 LLINK(p) = Node::NullPtr;
1301 const size_t left_t2_h = (left_t2 != Node::NullPtr) ?
avl_height(left_t2) : 0;
1305 RLINK(p) = Node::NullPtr;
1318 RLINK(p) = Node::NullPtr;
1319 const size_t right_t1_h = (right_t1 != Node::NullPtr) ?
avl_height(right_t1) : 0;
1323 LLINK(p) = Node::NullPtr;
1347 if (t.root == Node::NullPtr)
1350 if (
root == Node::NullPtr)
1353 t.root = Node::NullPtr;
1361 t.root = Node::NullPtr;
1375 if (right != Node::NullPtr)
1377 if (left != Node::NullPtr)
1404 if (
root == Node::NullPtr)
1408 Node *found =
nullptr;
1411 root = Node::NullPtr;
1419 if (right != Node::NullPtr)
1421 if (left != Node::NullPtr)
1429 (void) t1.insert(p);
1430 else if (
cmp(key,
KEY(p)))
1431 (
void) t2.insert(p);
1460 if (
root == Node::NullPtr)
1465 root = Node::NullPtr;
1473 if (right != Node::NullPtr)
1475 if (left != Node::NullPtr)
1483 (void) t2.insert(p);
1485 (
void) t1.insert_dup(p);
1510 if (
root == Node::NullPtr)
1516 root = Node::NullPtr;
1523 root = Node::NullPtr;
1531 root = Node::NullPtr;
1534 while (curr != Node::NullPtr or not stack.
is_empty())
1536 while (curr != Node::NullPtr)
1540 LLINK(curr) = Node::NullPtr;
1546 RLINK(curr) = Node::NullPtr;
1551 (void) t1.insert(curr);
1553 (
void) t2.insert(curr);
1574 using Base::operator =;
1589template <
typename Key,
class Compare = Aleph::less<Key>>
1608template <
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.
#define AH_ERROR(...)
Print an error message (always enabled).
Standard functor implementations and comparison objects.
AVL tree node with rank (subtree count).
#define DIFF(p)
Access the balance factor of node p.
Base iterator template for ranked binary search trees.
Functor encompassing basic operation for extended binary search trees.
long find_position(Node *r, const Key &key, Node *&p) noexcept(nothrow_compare_v< Cmp, Key >)
Find the inorder position of a key in an extended binary search tree.
AVL balanced binary search tree with rank (order statistics).
void restore_avl_after_deletion(bool left_deficit, Path &path) noexcept
Node * search_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
static Node * join_with_pivot(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
void update_counters_after_insertion(const Path &path) noexcept
static size_t avl_height(Node *p) noexcept
void restore_avl_after_insertion(Node *p, Path &path) noexcept
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Gen_Avl_Tree_Rk(Gen_Avl_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 * swapWithSuccessor(Node *p, Node *&pp, Path &path) noexcept
static Node * join_exclusive_rec(Node *t1, size_t h1, Node *t2, size_t h2) noexcept
void split_key_dup_rec(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Node * select(const size_t i) const
Return the i-th node in order sense.
void swap(Gen_Avl_Tree_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap in constant time all the items of this with the items of tree.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from tree the node containing key.
Gen_Avl_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.
Node head_node
The type of node.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
static Node * join_left(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
virtual ~Gen_Avl_Tree_Rk() noexcept=default
static Rotation_Type rotation_type(Node *p) noexcept
Gen_Avl_Tree_Rk(Compare cmf_fct) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria cmf_fct.
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
Node * split_key_rec(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node p without testing for key duplicity.
static Node * restore_avl(Node *p, Node *pp) noexcept
static Node * rotateLeft(Node *p) noexcept
size_t size() const noexcept
Return the number of nodes in the tree.
void split_pos_rec(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
Gen_Avl_Tree_Rk(const Gen_Avl_Tree_Rk &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
constexpr Compare & key_comp() noexcept
The key type.
static Node * extract_max(Node *&p) noexcept
Node * search_dup_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
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.
static Node * extract_min(Node *&p) noexcept
static Node * rotateRight(Node *p) noexcept
void join_exclusive(Gen_Avl_Tree_Rk &t) noexcept
Join this tree exclusively with another tree.
constexpr bool is_empty() const noexcept
Return true if tree is empty.
void update_counters_after_deletion(const Path &path) noexcept
static Node * doubleRotateLeft(Node *p) noexcept
constexpr Compare & get_compare() noexcept
See key_comp().
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
void split_key_dup(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
static Node * doubleRotateRight(Node *p) noexcept
bool verify() const
Return true if the tree is a valid AVL tree with correct counters and its keys are in order.
Node * split_key(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key.
Gen_Avl_Tree_Rk & operator=(const Gen_Avl_Tree_Rk &)=delete
Not copyable, for the same reason as the copy constructor.
void split_pos(const size_t pos, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by inorder position.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
constexpr Node * getRoot() const noexcept
Return a pointer to the tree's root.
static bool at_head(const Path &path) noexcept
true when path holds only the head node.
static Node * join_right(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
Stack of fixed capacity whose storage is inside the object.
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.
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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search 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.
long inorder_position(Node *r, const Key &key, Node *&p) noexcept(nothrow_compare_v< Cmp, Key >)
Compute the inorder position of a key.
Main namespace for Aleph-w library functions.
bool is_avl_rk(Node *p)
Verify if tree rooted at p is a valid AVL tree with correct counters.
and
Check uniqueness with explicit hash + equality functors.
@ CmpGreater
First argument is greater than second.
@ CmpLess
First argument is less than second.
@ CmpEqual
Arguments are equal.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Ranked AVL tree with nodes with a virtual destructor.
Ranked AVL tree with nodes without a virtual destructor.
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).