48# ifndef TPL_SPLAY_TREE_RK_H
49# define TPL_SPLAY_TREE_RK_H
51# include <type_traits>
118template <
template <
class>
class NodeType,
typename Key,
class Compare>
119 requires StrictWeakOrder<Compare, Key>
134 assert(
root != Node::NullPtr);
136 Node * head_ptr = &header;
147 if (
const Key & tk =
KEY(t);
cmp(key, tk))
150 if (
LLINK(t) == Node::NullPtr) [[unlikely]]
156 if (
LLINK(t) == Node::NullPtr) [[unlikely]]
165 else if (
cmp(tk, key))
168 if (
RLINK(t) == Node::NullPtr) [[unlikely]]
174 if (
RLINK(t) == Node::NullPtr) [[unlikely]]
192 COUNT(t) = l_size + r_size + 1;
240 void splay(
const Key & key)
noexcept
272 noexcept(std::is_nothrow_constructible_v<Compare, Compare &>)
306 noexcept(bin_node_detail::nothrow_compare_taken_v<Compare>)
307 :
root(
Node::NullPtr),
cmp(bin_node_detail::compare_to_take(tree.
cmp))
310 tree.root = Node::NullPtr;
325 std::swap(
root, tree.root);
355 const Key & pk =
KEY(p);
387 assert(p != Node::NullPtr);
388 assert(
COUNT(p) == 1);
389 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
392 if (
root == Node::NullPtr)
395 const Key & key =
KEY(p);
397 const signed char cmp_result =
splay_impl(key);
415 assert(p != Node::NullPtr);
416 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
419 if (
root == Node::NullPtr)
438 if (
root == Node::NullPtr)
441 const signed char cmp_result =
splay_impl(key);
458 if (
root == Node::NullPtr)
461 const Key & key =
KEY(p);
462 const signed char cmp_result =
splay_impl(key);
482 if (
r == Node::NullPtr ||
RLINK(
r) == Node::NullPtr)
486 Node * head_ptr = &header;
493 while (
RLINK(t) != Node::NullPtr)
499 if (
RLINK(t) == Node::NullPtr)
512 COUNT(t) = l_size + 1;
541 if (
root == Node::NullPtr)
544 const signed char cmp_result =
splay_impl(key);
592 return root == Node::NullPtr;
611 if (
root == Node::NullPtr)
612 return {-1,
nullptr};
614 const signed char cmp_result =
splay_impl(key);
618 return {-1,
nullptr};
653 if (
root == Node::NullPtr)
654 return {-1,
nullptr};
656 const signed char cmp_result =
splay_impl(key);
707 <<
"Iterator is at end";
716 <<
"Iterator is at end";
735 curr = Node::NullPtr;
741 while (
LLINK(
r) != Node::NullPtr)
748 Node *successor = Node::NullPtr;
751 while (
curr != Node::NullPtr)
768template <
typename Key,
class Compare = Aleph::less<Key>>
769 requires StrictWeakOrder<Compare, Key>
777template <
typename Key,
class Compare = Aleph::less<Key>>
778 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_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Inorder iterator over the extended splay tree.
GenTdSplayTreeRk * tree_ptr
size_t get_pos() const noexcept
Node * get_curr_ne() const noexcept
static Node * find_min(Node *r) noexcept
void reset_first() noexcept
Iterator(GenTdSplayTreeRk &tree) noexcept
bool has_curr() const noexcept
static Node * inorder_successor(Node *root, const Key &key) noexcept
Top-down splay tree with rank support.
GenTdSplayTreeRk(GenTdSplayTreeRk &&tree) noexcept(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)
Returns the inorder (sorted) position of key.
Node * splay_max(Node *r) noexcept
Splay the maximum element to the root.
void splay(const Key &key) noexcept
Search key within the tree and splay that node; if key is not found, the last node of the search is s...
Compare & get_compare()
See key_comp().
static constexpr signed char CmpGreater
std::pair< long, Node * > find_position(const Key &key)
Returns the inorder (sorted) position of key.
GenTdSplayTreeRk() noexcept(std::is_nothrow_default_constructible_v< Compare >)
Builds an empty tree with a comparison criteria built by default.
void swap(GenTdSplayTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Exchange in O(1) the nodes and the comparison criteria of this tree and another.
Node * insert(Node *p)
Inserts a node in a top down splay tree.
GenTdSplayTreeRk(const GenTdSplayTreeRk &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
Node * remove(const Key &key)
Remove a key from a top-down splay tree.
Node * search_or_insert(Node *p)
Searches the key of p and, if it is not in the tree, inserts p.
Compare & key_comp()
Returns a reference to the comparison functor.
bool is_empty() const
Returns true if the tree is empty.
signed char splay_impl(const Key &key) noexcept
Node * search(const Key &key)
Searches a key in a top down splay tree.
GenTdSplayTreeRk & operator=(const GenTdSplayTreeRk &)=delete
Not copyable, for the same reason as the copy constructor.
GenTdSplayTreeRk(Compare __cmp) noexcept(std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria __cmp.
static constexpr signed char CmpEqual
Key key_type
The key type stored in the node.
Node * select(const size_t &i)
Returns the node whose inorder position in the extended tree is i.
Node * insert_dup(Node *p)
Inserts a node in a top down splay tree, even if its key is already in the tree.
virtual ~GenTdSplayTreeRk()
Destructor.
static constexpr signed char CmpLess
size_t size() const
Returns the number of nodes stored in the tree.
Node *& getRoot()
Get the top-down splay tree's root.
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 * rotate_to_left_xt(Node *p) noexcept
Rotate to left the extended binary tree with root p.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Extended binary node with subtree count.