48# ifndef TPL_RAND_TREE_H
49# define TPL_RAND_TREE_H
53# include <type_traits>
159 template <
template <
typename>
class NodeType,
typename Key,
class Compare>
160 requires StrictWeakOrder<Compare, Key>
175 noexcept(nothrow_compare_copy_v<Compare, Key>)
177 if (
root == Node::NullPtr)
189 const Key & pk =
KEY(p);
194 if (result != Node::NullPtr)
201 else if (
cmp(rk, pk))
204 if (result != Node::NullPtr)
212 return Node::NullPtr;
216 noexcept(nothrow_compare_copy_v<Compare, Key>)
218 if (
root == Node::NullPtr)
229 const Key & pk =
KEY(p);
242 noexcept(nothrow_compare_copy_v<Compare, Key>)
244 if (
root == Node::NullPtr)
259 return searchInBinTree<Node, Compare>(
root,
KEY(p),
cmp);
262 const Key & pk =
KEY(p);
326 [[deprecated(
"it makes a Mersenne Twister in a tree that has none: "
327 "use random_source(); it goes in 8.0.0")]]
339 [[deprecated(
"the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
340 gsl_rng * gsl_rng_object() {
return r.gsl(); }
387 noexcept(
std::is_nothrow_default_constructible_v<Compare>
and
388 std::is_nothrow_copy_constructible_v<Compare>)
403 noexcept(std::is_nothrow_copy_constructible_v<Compare>)
435 noexcept(std::is_nothrow_copy_constructible_v<Compare>)
482 assert(p != Node::NullPtr);
483 assert((
LLINK(p) == Node::NullPtr)
and (
RLINK(p) == Node::NullPtr));
484 assert(
COUNT(p) == 1);
487 if (result == Node::NullPtr)
510 assert(p != Node::NullPtr);
511 assert((
LLINK(p) == Node::NullPtr)
and (
RLINK(p) == Node::NullPtr));
512 assert(
COUNT(p) == 1);
522 if (tl == Node::NullPtr)
525 if (tr == Node::NullPtr)
528 const size_t &
m =
COUNT(tl);
529 const size_t & n =
COUNT(tr);
542 noexcept(nothrow_compare_v<Compare, Key>)
544 if (
root == Node::NullPtr)
545 return Node::NullPtr;
552 if (ret_val != Node::NullPtr)
560 if (ret_val != Node::NullPtr)
585 Node *
remove(
const Key & key)
noexcept(nothrow_compare_v<Compare, Key>)
589 return ret_val != Node::NullPtr ? ret_val :
nullptr;
602 Node *
search(
const Key & key)
const noexcept(nothrow_compare_v<Compare, Key>)
605 return retVal == Node::NullPtr ? nullptr : retVal;
627 noexcept(nothrow_compare_copy_v<Compare, Key>)
629 assert(p != Node::NullPtr);
630 assert(
COUNT(p) == 1);
675 std::pair<long, Node *>
position(
const Key & key)
const
676 noexcept(nothrow_compare_copy_v<Compare, Key>)
678 std::pair<long, Node *> ret_val;
716 noexcept(nothrow_compare_copy_v<Compare, Key>)
718 std::pair<long, Node *>
r(-2,
nullptr);
773 noexcept(nothrow_compare_v<Compare, Key>)
794 noexcept(nothrow_compare_v<Compare, Key>)
822 if (t1 == Node::NullPtr)
825 if (t2 == Node::NullPtr)
828 Node *ret = Node::NullPtr;
829 const size_t m =
COUNT(t1);
830 const size_t n =
COUNT(t2);
837 if (ret != Node::NullPtr)
855 if (ret != Node::NullPtr)
873 if (t1 == Node::NullPtr)
876 if (t2 == Node::NullPtr)
879 Node *ret = Node::NullPtr;
880 const size_t &
m =
COUNT(t1);
881 const size_t & n =
COUNT(t2);
882 const size_t total =
m + n;
925 t.tree_root = Node::NullPtr;
945 t.tree_root = Node::NullPtr;
962 t.tree_root = Node::NullPtr;
1012 template <
typename Key,
class Compare = Aleph::less<Key>>
1013 requires StrictWeakOrder<Compare, Key>
1051 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.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
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(nothrow_compare_v< Cmp, Key >)
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())
Builds an empty random tree whose positions are those of GSL for seed: it draws them from a Mersenne ...
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove a key from the tree.
bool verify() const
Return true if this is a consistent randomized tree.
Node * insert_dup(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
const Compare & key_comp() const noexcept
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it f...
void split_key_dup(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key that could be in the tree.
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.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Search or insert a key.
Node * random_insert_dup(Node *root, Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Compare & key_comp() noexcept
return the comparison criteria
virtual ~Gen_Rand_Tree() noexcept
bool split_key(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key.
Compare & get_compare() noexcept
See key_comp().
Gen_Rand_Tree(Gen_Rand_Tree &&other) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Take the nodes, the generator and the comparison criterion of another tree, which is left empty,...
Random_Source & random_source() noexcept
The source of random numbers, which draws the positions of the nodes.
Gen_Rand_Tree(const Compare &cmp) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty random tree with the comparison criteria cmp, which draws its positions from a SplitM...
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.
void swap(Gen_Rand_Tree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap in constant time all the nodes of this with tree.
Node * remove_pos(const size_t i)
Remove the key at inorder position i
Gen_Rand_Tree(const Gen_Rand_Tree &)=delete
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.
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(nothrow_compare_copy_v< Compare, Key >)
Node * random_remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
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.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the tree, modulo 2^32 - 1: the positions are from then on those of GSL ...
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key.
Node * random_join(Node *t1, Node *t2, Node *&dup)
void split_pos(size_t pos, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2)
Split a extended binary tree according to a position.
Node * random_join_exclusive(Node *tl, Node *tr) noexcept
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node * remove_pos(Node *&root, const size_t pos) noexcept
static unsigned long generator_seed(const unsigned long seed) noexcept
Gen_Rand_Tree() noexcept(std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty random tree with a comparison criteria built by default, which draws its positions fr...
Node * random_search_or_insert(Node *&root, Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Node * tree_root
The type of node.
Gen_Rand_Tree & operator=(const Gen_Rand_Tree &)=delete
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.
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.
std::uint64_t uniform_int_ne(const std::uint64_t n) noexcept
An integer in [0, n), each with the same probability, without checking n.
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< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
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 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.
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.
Node * insert_root_xt(Node *&root, Node *p, Compare &cmp) noexcept
Insert a node p as root of an extended binary search 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 * insert_root(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p as root of an extended binary search tree.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Iterator on nodes of the tree.
Iterator(Gen_Rand_Tree &t)
Randomized binary search tree.
Randomized binary search tree.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Binary tree operations (split, join, rotate).