Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Rand_Tree< NodeType, Key, Compare > Class Template Reference

Randomized binary search tree with rank support. More...

#include <tpl_rand_tree.H>

Inheritance diagram for Aleph::Gen_Rand_Tree< NodeType, Key, Compare >:
[legend]
Collaboration diagram for Aleph::Gen_Rand_Tree< NodeType, Key, Compare >:
[legend]

Classes

struct  Iterator
 Iterator on nodes of the tree. More...
 

Public Types

using Node = NodeType< Key >
 

Public Member Functions

 Gen_Rand_Tree (const Gen_Rand_Tree &)=delete
 
Gen_Rand_Tree & operator= (const Gen_Rand_Tree &)=delete
 
Compare & key_comp () noexcept
 return the comparison criteria
 
const Compare & key_comp () const noexcept
 
Compare & get_compare () noexcept
 See key_comp().
 
const Compare & get_compare () const noexcept
 
Random_Source & random_source () noexcept
 The source of random numbers, which draws the positions of the nodes.
 
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.
 
Gsl_Compatible_Rng & rng ()
 The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it from then on.
 
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 for the seed.
 
 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 Twister, in dynamic memory.
 
 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 from a SplitMix64 with a seed of its own.
 
 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 SplitMix64 with a seed of its own.
 
void swap (Gen_Rand_Tree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swap in constant time all the nodes of this with tree.
 
 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, and draws from then on from a SplitMix64 with a seed of its own.
 
Gen_Rand_Tree & operator= (Gen_Rand_Tree &&other) noexcept(noexcept(swap(other)))
 Exchange this tree with another, which is left with the previous nodes, generator and comparison criterion of this one.
 
virtual ~Gen_Rand_Tree () noexcept
 
Node * insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Insert a node in the tree.
 
Node * insert_dup (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Insert a node in the tree.
 
Node * remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
 Remove a key from the tree.
 
Node * search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
 Search a key.
 
Node * search_or_insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Search or insert a key.
 
bool verify () const
 Return true if this is a consistent randomized tree.
 
Node *& getRoot () noexcept
 Return the tree's root.
 
Node * select (const size_t i) const
 Return the i-th node in inorder sense.
 
size_t size () const noexcept
 Return the number of nodes that have the tree.
 
bool is_empty () const noexcept
 Return true if the tree is empty.
 
std::pair< long, Node * > position (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
 Compute the inorder position of a key.
 
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.
 
Node * remove_pos (const size_t i)
 Remove the key at inorder position i
 
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.
 
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.
 
void split_pos (size_t pos, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2)
 Split a extended binary tree according to a position.
 
void join (Gen_Rand_Tree &t, Gen_Rand_Tree &dup) noexcept
 Join this with t filtering the duplicated keys.
 
void join_dup (Gen_Rand_Tree &t) noexcept
 Join this with t independently of the presence of duplicated keys.
 
void join_exclusive (Gen_Rand_Tree &t) noexcept
 Join exclusive of this with t
 

Private Member Functions

Node * random_insert (Node *root, Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 
Node * random_insert_dup (Node *root, Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 
Node * random_search_or_insert (Node *&root, Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 
Node * random_join_exclusive (Node *tl, Node *tr) noexcept
 
Node * random_remove (Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
 
Node * remove_pos (Node *&root, const size_t pos) noexcept
 
Node * random_join (Node *t1, Node *t2, Node *&dup)
 
Node * random_join (Node *t1, Node *t2)
 

Static Private Member Functions

static unsigned long generator_seed (const unsigned long seed) noexcept
 

Private Attributes

Node * tree_root
 The type of node.
 
Random_Source r
 
Compare cmp
 

Detailed Description

template<template< typename > class NodeType, typename Key, class Compare>
requires StrictWeakOrder<Compare, Key>
class Aleph::Gen_Rand_Tree< NodeType, Key, Compare >

Randomized binary search tree with rank support.

This class implements a randomized binary search tree where random decisions during insertion and deletion ensure that the tree has the same expected properties as if it were built from a random permutation of keys, regardless of actual insertion order.

Key Insight:
At each insertion, a random "raffle" determines whether the new node becomes the root (and existing nodes are split around it) or is inserted recursively into a subtree. This ensures that every node has an equal probability of being at any position, producing expected O(log n) height.
Ranked Operations:
Unlike basic BSTs, randomized trees maintain subtree sizes (COUNT), enabling O(log n) access by rank (inorder position):
  • select(i): Get the i-th smallest element
  • position(key): Get the rank of a key
  • remove_pos(i): Remove the i-th element

This allows treating the tree like a sorted dynamic array with O(log n) indexed access, insertion, and deletion.

Split and Join:
Randomized trees excel at split/join operations, which take O(log(max(n,m))) time. This is dramatically better than other balanced trees that require O(n log m) for joining two trees of sizes n and m.
Template Parameters
NodeTypeNode template (RandNode or RandNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
Complexity:
  • Search: O(log n) expected
  • Insert: O(log n) expected
  • Delete: O(log n) expected
  • Select (by rank): O(log n) expected
  • Position (get rank): O(log n) expected
  • Split: O(log n) expected
  • Join: O(log(max(n,m))) expected - much better than other trees
  • Space: O(n) + O(1) per node for COUNT
Random Number Generator:
The positions come from a Random_Source. A tree that is given no seed draws them from a SplitMix64 inside it, with a seed of its own: it is built and moved without allocating. With a seed, that of a constructor or of set_seed(), it draws them from a Gsl_Compatible_Rng, the Mersenne Twister (mt19937) with the numbers of the one of GSL, which it used before: for a seed, the trees are the same. random_source() gives the source, which can be asked for a SplitMix64 with a seed.
Example:
// Insert nodes
// Ranked access (treat as sorted array)
auto smallest = tree.select(0); // Get 0th element (8)
auto median = tree.select(tree.size() / 2); // Get median
// Find position/rank of a key
size_t pos = tree.position(42); // Returns 2 (0-indexed)
// Remove by position
auto removed = tree.remove_pos(1); // Remove 2nd smallest
delete removed;
// Efficient split at key
Rand_Tree<int> left, right;
tree.split_key(50, left, right); // All <= 50 go to left, > 50 to right
// Efficient join (O(log(max(n,m))))
tree.join(left, right);
NodeType< Key > Node
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.
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 join(Gen_Rand_Tree &t, Gen_Rand_Tree &dup) noexcept
Join this with t filtering the duplicated keys.
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
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Definition ahUtils.H:85
Randomized binary search tree.
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetRandTree.
All operations maintain COUNT values for ranked operations.
Best choice when frequent split/join operations are needed.
See also
Rand_Tree Convenient typedef for Rand_Tree<Key, Compare>.
Treap Alternative randomized tree (often faster by constant factor).
TreapRk Treap with rank support.
DynSetRandTree High-level wrapper with automatic memory management.

Definition at line 161 of file tpl_rand_tree.H.

Member Typedef Documentation

◆ Node

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Node = NodeType<Key>

Definition at line 164 of file tpl_rand_tree.H.

Constructor & Destructor Documentation

◆ Gen_Rand_Tree() [1/5]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree ( const Gen_Rand_Tree< NodeType, Key, Compare > &  )
delete

◆ Gen_Rand_Tree() [2/5]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree ( const unsigned int  seed,
const Compare &  __cmp = Compare() 
)
inline

Builds an empty random tree whose positions are those of GSL for seed: it draws them from a Mersenne Twister, in dynamic memory.

Parameters
[in]seedThe seed, taken modulo 2^32 - 1, as it was for GSL.
[in]__cmpThe comparison criteria, which the tree copies.
Exceptions
std::bad_allocIf there is not enough memory for the Mersenne Twister, or what the copy of __cmp throws.

Definition at line 366 of file tpl_rand_tree.H.

◆ Gen_Rand_Tree() [3/5]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree ( )
inlinenoexcept

Builds an empty random tree with a comparison criteria built by default, which draws its positions from a SplitMix64 with a seed of its own.

It does not allocate.

It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.

Exceptions
whatthe default construction or the copy of the comparison throws; nothing when they are noexcept.

Definition at line 386 of file tpl_rand_tree.H.

◆ Gen_Rand_Tree() [4/5]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree ( const Compare &  cmp)
inlinenoexcept

Builds an empty random tree with the comparison criteria cmp, which draws its positions from a SplitMix64 with a seed of its own.

It does not allocate.

Parameters
[in]cmpthe comparison criteria, which the tree copies.
Exceptions
whatthe copy of cmp throws; nothing when it is noexcept.

Definition at line 402 of file tpl_rand_tree.H.

◆ Gen_Rand_Tree() [5/5]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree ( Gen_Rand_Tree< NodeType, Key, Compare > &&  other)
inlinenoexcept

Take the nodes, the generator and the comparison criterion of another tree, which is left empty, and draws from then on from a SplitMix64 with a seed of its own.

It does not allocate.

Parameters
[in,out]otherthe tree to move from.
Exceptions
whatthe copy of the comparison criterion throws; then other is unchanged. Nothing when that copy is noexcept.

Definition at line 434 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r, Aleph::Random_Source::swap(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ ~Gen_Rand_Tree()

template<template< typename > class NodeType, typename Key , class Compare >
virtual Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::~Gen_Rand_Tree ( )
inlinevirtualnoexcept

Definition at line 462 of file tpl_rand_tree.H.

Member Function Documentation

◆ find_position()

template<template< typename > class NodeType, typename Key , class Compare >
std::pair< long, Node * > Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::find_position ( const Key &  key) const
inlinenoexcept

Find the inorder position of a key in the tree.

find_position(key) determines the inorder position that has or had key in the tree. Themethod return a tuple with a position and a node pointer.

If key is found then its inorder position is returned along with a pointer to the node containing it.

Otherwise, the tuple returns the position that would have key if this was in the tree and the parent node that the key should be. At this regard, there are three cases:

  1. if key is lesser than the minimum key of tree, then first is -1 and the node is one with the smallest key.
  2. If key is greater than the maximum key in the tree, then first is the number of keys and the node is one with the maximum key in the tree.
  3. For any other case, first is the inorder position that would have key if this was in the tree and second is the node whose key is immediately greater than key.
Parameters
[in]keyto be searched
Returns
a pair with the inorder position and related node
Exceptions
anythingthat the comparison or its copy throws; the search does not modify the tree, so it is left as it was.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 715 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::find_position(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r, and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

Referenced by Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::find_position().

◆ generator_seed()

template<template< typename > class NodeType, typename Key , class Compare >
static unsigned long Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::generator_seed ( const unsigned long  seed)
inlinestaticprivatenoexcept

◆ get_compare() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::get_compare ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 307 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp.

◆ get_compare() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Compare & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::get_compare ( )
inlinenoexcept

◆ getRoot()

template<template< typename > class NodeType, typename Key , class Compare >
Node *& Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::getRoot ( )
inlinenoexcept

◆ insert()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::insert ( Node *  p)
inlinenoexcept

Insert a node in the tree.

Parameters
[in]ppointer to the node to insert
Returns
if p->get_key() is not in the tree, then the pointer p is returned (it was inserted); otherwise, nullptr is returned
Exceptions
anythingthat the comparison or its copy throws; the tree changes only after the last comparison, so its links and nodes are left as they were, without p; not so the random generator, which the insertion may have used.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 480 of file tpl_rand_tree.H.

References Aleph::and, Aleph::COUNT(), LLINK, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_insert(), RLINK, and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

Referenced by main(), TEST(), and write_rand().

◆ insert_dup()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::insert_dup ( Node *  p)
inlinenoexcept

Insert a node in the tree.

This method does not fail. It always inserts.

Parameters
[in]ppointer to the node to insert
Returns
the p pointer
Exceptions
anythingthat the comparison or its copy throws; the tree changes only after the last comparison, so its links and nodes are left as they were, without p; not so the random generator, which the insertion may have used.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 508 of file tpl_rand_tree.H.

References Aleph::and, Aleph::COUNT(), LLINK, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_insert_dup(), RLINK, and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ is_empty()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::is_empty ( ) const
inlinenoexcept

◆ join()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::join ( Gen_Rand_Tree< NodeType, Key, Compare > &  t,
Gen_Rand_Tree< NodeType, Key, Compare > &  dup 
)
inlinenoexcept

Join this with t filtering the duplicated keys.

join(t, dup) produces a random tree result of join of this and t. The resulting tree is stored in this.

Nodes containing duplicated keys are inserted in the randomized tree dup. The nodes could belong to any of two trees

Parameters
[in]trandomized tree to join with this
[out]duprandomized tree where nodes containing duplicated keys are inserted
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()).
Note
It stays noexcept because it moves the nodes one by one: a comparison that threw in the middle would leave nodes out of every tree.

Definition at line 922 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ join_dup()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::join_dup ( Gen_Rand_Tree< NodeType, Key, Compare > &  t)
inlinenoexcept

Join this with t independently of the presence of duplicated keys.

join(t) produces a random tree result of join of this and t. The resulting tree is stored in this.

Parameters
[in]trandomized tree to join with this keys are inserted
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()).
Note
It stays noexcept because it moves the nodes one by one: a comparison that threw in the middle would leave nodes out of every tree.

Definition at line 942 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ join_exclusive()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::join_exclusive ( Gen_Rand_Tree< NodeType, Key, Compare > &  t)
inlinenoexcept

Join exclusive of this with t

Exclusive means that all the keys of this are lesser than all the keys of t. This knowledge allows a more efficient way for joining that when the keys ranks are overlapped. However, use very carefully because the algorithm does not perform any check and the result would be incorrect.

Parameters
[in]trandomized tree to exclusively join with this keys are inserted

Definition at line 959 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join_exclusive(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ key_comp() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::key_comp ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 301 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp.

◆ key_comp() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Compare & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::key_comp ( )
inlinenoexcept

return the comparison criteria

Definition at line 298 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp.

Referenced by Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::get_compare().

◆ operator=() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Gen_Rand_Tree & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::operator= ( const Gen_Rand_Tree< NodeType, Key, Compare > &  )
delete

◆ operator=() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Gen_Rand_Tree & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::operator= ( Gen_Rand_Tree< NodeType, Key, Compare > &&  other)
inlinenoexcept

Exchange this tree with another, which is left with the previous nodes, generator and comparison criterion of this one.

The previous assignment took the generator of other, which then drew from a null one, and freed the nodes of this tree, which the tree does not own: they go now to other, as in swap().

Parameters
[in,out]otherthe tree to move from.
Returns
a reference to this tree.
Exceptions
whatswapping the comparison criteria throws; then nothing is exchanged.

Definition at line 456 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::swap().

◆ random_insert()

◆ random_insert_dup()

◆ random_join() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join ( Node *  t1,
Node *  t2 
)
inlineprivate

◆ random_join() [2/2]

◆ random_join_exclusive()

◆ random_remove()

◆ random_search_or_insert()

◆ random_source() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
const Random_Source & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_source ( ) const
inlinenoexcept

The source of random numbers, to look at it: its engine, or whether it is equal to another one.

Definition at line 315 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r.

◆ random_source() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Random_Source & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_source ( )
inlinenoexcept

The source of random numbers, which draws the positions of the nodes.

Definition at line 311 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r.

Referenced by TEST().

◆ remove()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::remove ( const Key &  key)
inlinenoexcept

Remove a key from the tree.

Parameters
[in]keyto remove
Returns
a valid pointer to the removed node if key was found in the tree, nullptr otherwise
Exceptions
anythingthat the comparison throws; the tree changes only after the last comparison, so it is left as it was.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 585 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_remove(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ remove_pos() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::remove_pos ( const size_t  i)
inline

Remove the key at inorder position i

Parameters
[in]iinorder position to be removed
Returns
a valid pointer to the removed node
Exceptions
out_of_rangeif i is greater or equal than the number of nodes of tree

Definition at line 751 of file tpl_rand_tree.H.

References ah_out_of_range_error_if, Aleph::COUNT(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::remove_pos(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ remove_pos() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::remove_pos ( Node *&  root,
const size_t  pos 
)
inlineprivatenoexcept

◆ rng()

template<template< typename > class NodeType, typename Key , class Compare >
Gsl_Compatible_Rng & Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::rng ( )
inline

The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it from then on.

Deprecated:
It makes a generator of some 2500 bytes in a tree that has none; use random_source(). It goes in 8.0.0.
Returns
The Mersenne Twister: Random_Source::mersenne_twister().
Exceptions
std::bad_allocIf the tree has no Mersenne Twister and there is not enough memory for it.

Definition at line 328 of file tpl_rand_tree.H.

References Aleph::Random_Source::mersenne_twister(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r.

Referenced by TEST(), and TEST().

◆ search()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::search ( const Key &  key) const
inlinenoexcept

Search a key.

Parameters
[in]keyto be searched
Returns
a pointer to the containing key if this was found; otherwise nullptr
Exceptions
anythingthat the comparison throws; the search does not modify the tree, so it is left as it was.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 602 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp, and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

Referenced by main(), and write_rand().

◆ search_or_insert()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::search_or_insert ( Node *  p)
inlinenoexcept

Search or insert a key.

search_or_insert(p) searches in the tree the key KEY(p). If this key is found, then a pointer to the node containing it is returned. Otherwise, p is inserted.

Parameters
[in]pnode containing a key to be searched and eventually inserted
Returns
if the key contained in p is found, then a pointer to the containing key in the tree is returned. Otherwise, p is inserted and returned
Exceptions
anythingthat the comparison or its copy throws; the tree changes only after the last comparison, so its links and nodes are left as they were, without p; not so the random generator, which the insertion may have used.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 626 of file tpl_rand_tree.H.

References Aleph::COUNT(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_search_or_insert(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ select()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::select ( const size_t  i) const
inline

Return the i-th node in inorder sense.

Parameters
[in]iinorder position to be located
Returns
a valid pointer to the i-th node inorder sense
Exceptions
out_of_rangeif i is greater or equal that the number of nodes

Definition at line 651 of file tpl_rand_tree.H.

References Aleph::select(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

Referenced by Moved_From_Case< Rand_Tree< int > >::use().

◆ set_seed()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::set_seed ( const unsigned long  seed)
inline

Seeds the Mersenne Twister of the tree, modulo 2^32 - 1: the positions are from then on those of GSL for the seed.

A tree that was given no seed makes its Mersenne Twister here.

Parameters
[in]seedThe seed.
Exceptions
std::bad_allocIf the tree has no Mersenne Twister and there is not enough memory for it; then it is unchanged.

Definition at line 352 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::generator_seed(), Aleph::Random_Source::Mersenne_Twister, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r, Aleph::Random_Source::seed(), and seed.

Referenced by TEST(), and TEST().

◆ size()

template<template< typename > class NodeType, typename Key , class Compare >
size_t Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::size ( ) const
inlinenoexcept

◆ split_key()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::split_key ( const Key &  key,
Gen_Rand_Tree< NodeType, Key, Compare > &  t1,
Gen_Rand_Tree< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split the tree according to a key.

It splits with the comparison criteria of the tree.

Parameters
[in]keyfor splitting
[out]t1tree with keys lesser than key
[out]t2tree with keys greater than key
Returns
true if tree was split; that is if key is not in the tree. Otherwise, if key is in the tree, false is returned
Exceptions
anythingthat the comparison throws; every comparison comes before the first change, so the three trees are left as they were.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 772 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp, Aleph::split_key_rec_xt(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ split_key_dup()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::split_key_dup ( const Key &  key,
Gen_Rand_Tree< NodeType, Key, Compare > &  t1,
Gen_Rand_Tree< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split the tree according to a key that could be in the tree.

split_dup(key, t1, t2) splits the tree according to key in two trees t1 which contains the key lesser than key and t2 which contains the keys greater or equal than key. It splits with the comparison criteria of the tree.

Parameters
[in]keyfor splitting
[out]t1resulting tree with the keys lesser than key
[out]t2resulting tree with the keys greater or equal than key
Exceptions
anythingthat the comparison throws; every comparison comes before the first change, so the three trees are left as they were.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 793 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp, Aleph::split_key_dup_rec_xt(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ split_pos()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::split_pos ( size_t  pos,
Gen_Rand_Tree< NodeType, Key, Compare > &  t1,
Gen_Rand_Tree< NodeType, Key, Compare > &  t2 
)
inline

Split a extended binary tree according to a position.

split_pos(pos, t1, t2) splits the tree in two trees t1 and t2 according to a position pos. t1 contains the keys from 0 to pos - 1 inorder sense and t2 the keys from pos to n -

  1. After completion this becames empty.

It is not noexcept: a position past the end throws, and inside noexcept that would end the program.

Parameters
[in]posposition for splitting, at most size()
[out]t1tree where the rank of keys \([0, pos)\) will be put
[out]t2tree where the rank of keys \([pos, n)\) will be put
Exceptions
std::out_of_rangeif pos > size(); then nothing changes

Definition at line 814 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::getRoot(), Aleph::split_pos_rec(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

◆ swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::swap ( Gen_Rand_Tree< NodeType, Key, Compare > &  tree)
inlinenoexcept

Swap in constant time all the nodes of this with tree.

Parameters
[in,out]treethe tree to exchange with.
Exceptions
whatswapping the comparison criteria throws. They are exchanged first, so that, if that swap throws, nothing else is exchanged, and both trees are left as they were if it left its arguments as they were.

Definition at line 416 of file tpl_rand_tree.H.

References Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::r, Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::swap(), Aleph::Random_Source::swap(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root.

Referenced by Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::operator=(), and Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::swap().

◆ verify()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::verify ( ) const
inline

Member Data Documentation

◆ cmp

◆ r

◆ tree_root

template<template< typename > class NodeType, typename Key , class Compare >
Node* Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::tree_root
private

The documentation for this class was generated from the following file: