48# ifndef TPL_RAND_TREE_H
49# define TPL_RAND_TREE_H
53# include <gsl/gsl_rng.h>
150 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
163 if (
root == Node::NullPtr)
175 const Key &
pk =
KEY(p);
180 if (result != Node::NullPtr)
190 if (result != Node::NullPtr)
198 return Node::NullPtr;
203 if (
root == Node::NullPtr)
214 const Key &
pk =
KEY(p);
228 if (
root == Node::NullPtr)
246 const Key &
pk =
KEY(p);
274 void init(
const unsigned long seed)
324 std::swap(
r, tree.r);
325 std::swap(
cmp, tree.cmp);
332 other.tree_root = Node::NullPtr;
352 other.tree_root = Node::NullPtr;
374 assert(p != Node::NullPtr);
379 if (result == Node::NullPtr)
394 assert(p != Node::NullPtr);
404 if (
tl == Node::NullPtr)
407 if (
tr == Node::NullPtr)
425 if (
root == Node::NullPtr)
426 return Node::NullPtr;
495 assert(p != Node::NullPtr);
537 std::pair<long, Node *>
position(
const Key & key)
const noexcept
539 std::pair<long, Node *>
ret_val;
574 std::pair<long, Node *>
r(-2,
nullptr);
663 if (
t1 == Node::NullPtr)
666 if (
t2 == Node::NullPtr)
678 if (
ret != Node::NullPtr)
696 if (
ret != Node::NullPtr)
714 if (
t1 == Node::NullPtr)
717 if (
t2 == Node::NullPtr)
723 const size_t total = m + n;
761 t.tree_root = Node::NullPtr;
775 t.tree_root = Node::NullPtr;
792 t.tree_root = Node::NullPtr;
841 template <
typename Key,
class Compare = Aleph::less<Key>>
878 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_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Standard functor implementations and comparison objects.
General utility functions and helpers.
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for extended binary search trees.
Node * insert_dup_root(Node *&root, Node *p) noexcept
Insert a node as root of an extended binary search tree.
Randomized binary search tree with rank support.
bool is_empty() const noexcept
Return true if the tree is empty.
Gen_Rand_Tree(const unsigned int seed, const Compare &__cmp=Compare())
Initialize a random tree with random seed and comparison criteria __cmp
bool verify() const
Return true if this is a consistent randomized tree.
const Compare & key_comp() const noexcept
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
Compare & key_comp() noexcept
return the comparison criteria
Node * insert_dup(Node *p) noexcept
Insert a node in the tree.
virtual ~Gen_Rand_Tree() noexcept
Node * random_search_or_insert(Node *&root, Node *p) noexcept
void split_pos(size_t pos, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split a extended binary tree according to a position.
std::pair< long, Node * > find_position(const Key &key) const noexcept
Find the inorder position of a key in the tree.
void set_seed(const unsigned long seed) noexcept
Set the random number generator seed.
Compare & get_compare() noexcept
void init(const unsigned long seed)
bool split_key(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split the tree according to a key.
Node * insert(Node *p) noexcept
Insert a node in the tree.
void split_key_dup(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split the tree according to a key that could be in the tree.
void join_exclusive(Gen_Rand_Tree &t) noexcept
Join exclusive of this with t
size_t size() const noexcept
Return the number of nodes that have the tree.
Node * select(const size_t i) const
Return the i-th node in inorder sense.
Node * remove_pos(const size_t i)
Remove the key at inorder position i
Gen_Rand_Tree(const Gen_Rand_Tree &)=delete
Gen_Rand_Tree(Gen_Rand_Tree &&other) noexcept
Move constructor - transfers ownership of tree and RNG.
const Compare & get_compare() const noexcept
void join_dup(Gen_Rand_Tree &t) noexcept
Join this with t independently of the presence of duplicated keys.
Node * random_insert(Node *root, Node *p) noexcept
gsl_rng * gsl_rng_object() noexcept
Get a pointer to gsl random number generator.
Node * random_remove(Node *&root, const Key &key) noexcept
Node * random_join(Node *t1, Node *t2)
Node *& getRoot() noexcept
Return the tree's root.
void join(Gen_Rand_Tree &t, Gen_Rand_Tree &dup) noexcept
Join this with t filtering the duplicated keys.
Node * random_insert_dup(Node *root, Node *p) noexcept
Node * random_join(Node *t1, Node *t2, Node *&dup)
Gen_Rand_Tree(const Compare &cmp=Compare()) noexcept
Initialize a random tree with random seed from system time and comparison criteria cmp
Node * random_join_exclusive(Node *tl, Node *tr) noexcept
Node * search(const Key &key) const noexcept
Search a key.
void swap(Gen_Rand_Tree &tree) noexcept
Swap in constant time all the nodes of this with tree
Node * remove_pos(Node *&root, const size_t pos) noexcept
Node * tree_root
The type of node.
Gen_Rand_Tree & operator=(const Gen_Rand_Tree &)=delete
__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< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
Node * insert_dup_root_xt(Node *&root, Node *p, Compare &cmp) noexcept
Insert a node as root of an extended binary search tree.
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.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
Node * insert_root(Node *&root, Node *p) noexcept
Insert a node p as root of an extended binary search tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Node * insert_root_xt(Node *&root, Node *p, Compare &cmp) noexcept
Insert a node p as root of an extended binary search tree.
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.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Iterator on nodes of the tree.
Iterator(Gen_Rand_Tree &t)
Randomized binary search tree.
Randomized binary search tree.
Binary tree operations (split, join, rotate).