54# include <type_traits>
187 template <
template <
class>
class NodeType,
class Key,
class Compare>
207 return static_cast<unsigned int>(
seed)
243 [[deprecated(
"it makes a Mersenne Twister in a treap that has none: "
244 "use random_source(); it goes in 8.0.0")]]
285 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
286 std::is_nothrow_default_constructible_v<Compare>
and
287 std::is_nothrow_constructible_v<Compare, Compare &>)
305 noexcept(std::is_nothrow_default_constructible_v<Node>
and
306 std::is_nothrow_constructible_v<Compare, Compare &>)
335 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
336 bin_node_detail::nothrow_compare_taken_v<Compare>)
337 :
head(bin_node_detail::header_from(tree.
head)),
339 cmp(bin_node_detail::compare_to_take(tree.
cmp))
343 tree.tree_root = Node::NullPtr;
357 std::swap(
cmp, tree.cmp);
377 Node *
search(
const Key & key)
const noexcept(nothrow_compare_v<Compare, Key>)
380 return ret_val == Node::NullPtr ? nullptr : ret_val;
390 if (
root == Node::NullPtr)
396 const Key & pk =
KEY(p);
409 else if (
cmp(rk, pk))
427 noexcept(nothrow_compare_v<Compare, Key>)
429 if (
root == Node::NullPtr)
432 const Key & pk =
KEY(p);
473 if (
root == Node::NullPtr)
476 const Key & pk =
KEY(p);
515 assert(p != Node::NullPtr);
537 assert(p != Node::NullPtr);
564 assert(p != Node::NullPtr);
582 if (t1 == Node::NullPtr)
585 if (t2 == Node::NullPtr)
601 if (
root == Node::NullPtr)
602 return Node::NullPtr;
608 else if (
cmp(rk, key))
618 if (ret_val == Node::NullPtr)
619 return Node::NullPtr;
638 Node *
remove(
const Key & key)
noexcept(nothrow_compare_v<Compare, Key>)
641 if (ret_val == Node::NullPtr)
661 Node * before_beg, * after_end, * aux;
736 std::pair<int, Node*>
position(
const Key & key)
const
737 noexcept(nothrow_compare_copy_v<Compare, Key>)
739 std::pair<int, Node*> ret_val;
778 noexcept(nothrow_compare_copy_v<Compare, Key>)
780 std::pair<int, Node*>
r(-2,
nullptr);
782 r.first = BinTreeXt_Operation <Node, Compare>(
cmp) .
805 noexcept(nothrow_compare_v<Compare, Key>)
829 noexcept(nothrow_compare_v<Compare, Key>)
849 if (t2 == Node::NullPtr)
861 if (t2 == Node::NullPtr)
867 const bool inserted =
insert(t1, t2);
901 t.tree_root = Node::NullPtr;
921 t.tree_root = Node::NullPtr;
938 t.tree_root = Node::NullPtr;
974 return curr !=
nullptr;
987 assert(
curr !=
nullptr);
1000 constexpr auto max_pos =
1001 static_cast<size_t>(std::numeric_limits<int>::max());
1278 noexcept(nothrow_compare_v<Compare, Key>)
1287 return curr == itor.curr;
1303 return not (*
this == itor);
1358 template <
typename Key,
class Compare = Aleph::less<Key>>
1359 requires StrictWeakOrder<Compare, Key>
1406 template <
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_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
SentinelCtor
Tag type for sentinel node construction.
Standard functor implementations and comparison objects.
void put_itor_at_the_end(Itor &it) noexcept
Functor encompassing basic operation for extended binary search trees.
Iterator on nodes of the tree.
static constexpr int Pos_Not_Updated
void update_curr() const noexcept
Node * del()
Remove the current node and move the iterator one position forward.
Node * get_curr() const noexcept
Return the current node.
void reset_first() noexcept
Reset the iterator to the first position.
Iterator & operator=(const Iterator &itor) noexcept
static constexpr int Pos_Out_Of_Range
Iterator(const Gen_Treap_Rk &__tree, const size_t pos) noexcept
Initialize an iterator starting from the inorder position pos.
bool curr_updated() const noexcept
bool verify(const Iterator &it) const noexcept
static constexpr int Pos_Empty_Container
bool operator!=(const Iterator &itor) const
Return true if this is not equal to itor
bool operator==(const Iterator &itor) const noexcept(nothrow_compare_v< Compare, Key >)
Return true if this and itor are on the same position.
Node * get_curr_ne() const noexcept
Return the current node.
void update_pos() const noexcept(nothrow_compare_v< Compare, Key >)
void prev()
Move the iterator one position backward.
bool has_curr() const noexcept(nothrow_compare_v< Compare, Key >)
Return true if the iterator has a current node.
size_t get_current_position() const
return the position of current node
Iterator(const Gen_Treap_Rk &__tree) noexcept
Initialize an iterator on __tree
void reset_last() noexcept
Reset the iterator to the last position.
size_t get_pos() const
See get_current_position().
void reset_to_pos(const size_t pos) noexcept
Put the iterator on the inorder position pos.
bool verify(Gen_Treap_Rk *r) const noexcept
void reset_to_key(const Key &key) noexcept(noexcept(tree_ptr->find_position(key)))
Put the iterator on the node of a key.
void next()
Move the iterator one position forward.
bool is_container_empty() const noexcept
static constexpr int Pos_Not_Current
static int saturated_pos(const size_t pos) noexcept
void reset_to_node(Node *node) noexcept
Put the current node to a specific node.
Iterator(const Iterator &itor) noexcept
Iterator(const Gen_Treap_Rk &__tree, Node *__curr) noexcept
Initialize an iterator starting from node __curr
bool pos_updated() const noexcept
void end() noexcept
Put the iterator in the end state.
bool is_updated() const noexcept
void next_ne() noexcept
Move the iterator one position forward, without the check of next().
Extended Treap with rank support for O(log n) indexed access.
Gen_Treap_Rk(const unsigned long seed, Compare __cmp=Compare())
Builds an empty treap whose priorities are those of GSL for seed: it draws them from a Mersenne Twist...
Node head
The type of node.
Compare & key_comp() noexcept
return the comparison criteria
Node *& getRoot() noexcept
Return the tree's root.
void split_key_dup(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key that could be in the tree.
static Node * remove_pos(Node *&root, const size_t pos) noexcept
const Random_Source & random_source() const noexcept
The source of random numbers, to look at it: its engine, or whether it is equal to another one.
bool verify() const
Return true if the treap is consistent.
Compare & get_compare() noexcept
See key_comp().
Node * select(const size_t i) const
Return the i-th node in order sense.
void join_exclusive(Gen_Treap_Rk &t) noexcept
Join exclusive of this with t
size_t size() const noexcept
Return the number of nodes contained in the tree.
static Node * join_exclusive(Node *t1, Node *t2) noexcept
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
bool insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
void join_dup(Node *&t1, Node *t2) noexcept
Node * search_or_insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Node * insert_dup(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
void split_pos(size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2)
Split the tree at the inorder position pos.
Node * remove(const size_t beg, const size_t end)
Remove from the treap all the keys between inorder position beg and end.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in a treap.
void join(Node *&t1, Node *t2, Node *&dup) noexcept
Node * remove_pos(const size_t pos)
Remove the node at the inorder position pos.
Gen_Treap_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 treap with a comparison criteria built by default, which draws its priorities from a ...
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
Gen_Treap_Rk(Gen_Treap_Rk &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes, the comparison criteria and the generator of tree, which keeps no nodes,...
Node * getRoot() const noexcept
bool split_key(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
void join_dup(Gen_Treap_Rk &t) noexcept
Join this with t independently of the presence of duplicated keys.
void join(Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept
Join this with t filtering the duplicated keys.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key in a treap.
void swap(Gen_Treap_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the treap, which a treap that was given no seed makes here,...
bool is_empty() const noexcept
Return true if tree is empty.
Node * remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
std::pair< int, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find the inorder position of a key in the tree.
Gen_Treap_Rk(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty treap with the comparison criteria __cmp, which draws its priorities from a SplitMix6...
static unsigned long generator_seed(const unsigned long seed) noexcept
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in the tree.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
The source of random numbers of a randomized structure, which draws from one of two generators.
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
unsigned long get() noexcept
The next number, as operator()().
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
TreapRkNode_Data() noexcept
unsigned long & getPriority() noexcept
unsigned long & getCount() noexcept
TreapRkNode_Data(SentinelCtor) noexcept
Strict weak ordering constraint for BST comparators.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
std::pair< int, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
long inorder_position_of_node(Node *r, Node *p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The inorder position of the node p, in a tree that may have keys equivalent to that of p.
bool split_key_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split an extended binary search tree according to a key.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
void split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Split a extended binary tree according to a position.
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a 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.
void split_key_dup_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split an extended binary search tree according to a key which can be in the tree.
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 * 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 * select_ne(Node *r, const size_t pos) noexcept
Iterative selection of a node according to inorder position without exception.
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
constexpr bool nothrow_compare_v
Whether Compare compares two keys of type Key without throwing, as the operations of the trees call i...
Main namespace for Aleph-w library functions.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
and
Check uniqueness with explicit hash + equality functors.
const long Max_Priority
Maximum priority value (used for sentinel/leaves)
const long Min_Priority
Minimum priority value.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Binary tree operations (split, join, rotate).
Treap node definition with BST key and heap priority.