53# include <gsl/gsl_rng.h>
179 template <
template <
class>
class NodeType,
class Key,
class Compare>
238 std::swap(
cmp, tree.cmp);
239 std::swap(
r, tree.r);
263 if (
root == Node::NullPtr)
269 const Key &
pk =
KEY(p);
301 if (
root == Node::NullPtr)
304 const Key &
pk =
KEY(p);
345 if (
root == Node::NullPtr)
348 const Key &
pk =
KEY(p);
381 assert(p != Node::NullPtr);
397 assert(p != Node::NullPtr);
418 assert(p != Node::NullPtr);
432 if (
t1 == Node::NullPtr)
435 if (
t2 == Node::NullPtr)
451 if (
root == Node::NullPtr)
452 return Node::NullPtr;
458 else if (
cmp(
rk, key))
469 return Node::NullPtr;
578 std::pair<int, Node*>
position(
const Key & key)
const noexcept
616 std::pair<int, Node*>
r(-2,
nullptr);
668 if (
t2 == Node::NullPtr)
680 if (
t2 == Node::NullPtr)
715 t.tree_root = Node::NullPtr;
729 t.tree_root = Node::NullPtr;
746 t.tree_root = Node::NullPtr;
781 return curr !=
nullptr;
1075 template <
typename Key,
class Compare = Aleph::less<Key>>
1121 template <
typename Key,
class Compare = Aleph::less<Key>>
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_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
SentinelCtor
Tag type for sentinel node construction.
Standard functor implementations and comparison objects.
void put_itor_at_the_end(Itor &it) noexcept
Iterator on nodes of the tree.
bool operator==(const Iterator &itor) const noexcept
Return true if this is equal to itor
bool has_curr() const noexcept
Return true if iterator has current node.
static constexpr int Pos_Not_Updated
void update_curr() const noexcept
Node * del()
Remove the current node and move the iterator one position forward.
void reset_to_key(const Key &key) noexcept
Put the iterator to a node according to specific key.
Node * get_curr() const noexcept
void reset_first() noexcept
Reset the iterator to the first position.
Iterator & operator=(const Iterator &itor) noexcept
Iterator(const Gen_Treap_Rk &__tree, const size_t pos) noexcept
Initialize an iterator starting from the iorder 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
Node * get_curr_ne() const noexcept
Return the current node.
void prev()
Move the iterator one position backward.
void update_pos() const noexcept
Iterator(const Gen_Treap_Rk &__tree) noexcept
Initialize an iterator on __tree
void reset_last() noexcept
Reset the iterator to the last position.
void reset_to_pos(const size_t pos) noexcept
Put the current to the position pos.
bool verify(Gen_Treap_Rk *r) const noexcept
void next()
Move the iterator one position forward.
bool is_container_empty() const noexcept
static constexpr int Pos_Not_Current
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
size_t get_current_position() const
return the position of current node
bool pos_updated() const noexcept
void end() noexcept
Put the iterator in the end state.
bool is_updated() const noexcept
Extended Treap with rank support for O(log n) indexed access.
Gen_Treap_Rk(Compare __cmp=Compare())
Gen_Treap_Rk(const unsigned long seed, Compare __cmp=Compare())
Initialize a treap with random seed and comparison criteria __cmp
Node head
The type of node.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
void set_seed(const unsigned long seed) noexcept
Set the random number generator seed.
bool split_key(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
Split the tree according to a key.
Node *& getRoot() noexcept
Return the tree's root.
static Node * remove_pos(Node *&root, const size_t pos) noexcept
bool verify() const
Return true if the treap is consistent.
Compare & get_compare() noexcept
Node * insert_dup(Node *&root, Node *p) noexcept
Node * select(const size_t i) const
Return the i-th node in order sense.
Node * insert(Node *p) noexcept
Insert a node in a treap.
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
void join_dup(Node *&t1, Node *t2) noexcept
Node * insert_dup(Node *p) noexcept
Insert a node in the tree.
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 * search_or_insert(Node *&root, Node *p) noexcept
Node * search(const Key &key) const noexcept
Search a key in a treap.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
void join(Node *&t1, Node *t2, Node *&dup) noexcept
void init(unsigned int seed)
Node * remove_pos(const size_t pos)
Remove the node at the inorder position pos.
std::pair< int, Node * > find_position(const Key &key) const noexcept
Find the inorder position of a key in the tree.
Node * remove(Node *&root, const Key &key) noexcept
Node * getRoot() const noexcept
Compare & key_comp() noexcept
return the comparison criteria
void swap(Gen_Treap_Rk &tree) noexcept
Swap in constant time all the nodes of this with tree
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.
bool is_empty() const noexcept
Return true if tree is empty.
bool insert(Node *&root, Node *p) noexcept
void split_key_dup(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
Split the tree according to a key that could be in the tree.
TreapRkNode_Data() noexcept
unsigned long & getPriority() noexcept
unsigned long & getCount() noexcept
TreapRkNode_Data(SentinelCtor) noexcept
__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)
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
void split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Split a extended binary tree according to a position.
std::pair< int, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary tree.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
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.
bool split_key_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept
Split an extended binary search tree according to a key.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
void split_key_dup_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept
Split an extended binary search tree according to a key which can be in the tree.
Main namespace for Aleph-w library functions.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
const long Max_Priority
Maximum priority value (used for sentinel/leaves)
const long Min_Priority
Minimum priority value.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.