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

Extended Treap with rank support for O(log n) indexed access. More...

#include <tpl_treapRk.H>

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

Classes

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

Public Types

using Node = NodeType< Key >
 

Public Member Functions

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 of GSL for the seed.
 
Random_Source & random_source () noexcept
 The source of random numbers, which gives the priorities.
 
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 treap, which a treap that was given no seed makes here, and draws from it from then on.
 
Compare & key_comp () noexcept
 return the comparison criteria
 
Compare & get_compare () noexcept
 See key_comp().
 
 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 Twister, in dynamic memory.
 
 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 SplitMix64 with a seed of its own.
 
 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 SplitMix64 with a seed of its own.
 
 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, and draws from then on from a SplitMix64 with a seed of its own: it can take nodes again.
 
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.
 
Node *& getRoot () noexcept
 Return the tree's root.
 
Node * getRoot () const noexcept
 
Node * search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
 Search a key in a treap.
 
Node * insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Insert a node in a treap.
 
Node * insert_dup (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Insert a node in the tree.
 
Node * search_or_insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Search or insert a key.
 
bool verify () const
 Return true if the treap is consistent.
 
Node * remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
 Remove a key from the tree.
 
Node * remove (const size_t beg, const size_t end)
 Remove from the treap all the keys between inorder position beg and end.
 
Node * remove_pos (const size_t pos)
 Remove the node at the inorder position pos.
 
Node * select (const size_t i) const
 Return the i-th node in order sense.
 
size_t size () const noexcept
 Return the number of nodes contained in the tree.
 
bool is_empty () const noexcept
 Return true if tree is empty.
 
std::pair< int, Node * > position (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
 Compute the inorder position of a 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.
 
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 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.
 
void split_pos (size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2)
 Split the tree at the inorder position pos.
 
void join (Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept
 Join this with t filtering the duplicated keys.
 
void join_dup (Gen_Treap_Rk &t) noexcept
 Join this with t independently of the presence of duplicated keys.
 
void join_exclusive (Gen_Treap_Rk &t) noexcept
 Join exclusive of this with t
 

Private Member Functions

bool insert (Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 
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 >)
 
Node * remove (Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
 
void join_dup (Node *&t1, Node *t2) noexcept
 
void join (Node *&t1, Node *t2, Node *&dup) noexcept
 

Static Private Member Functions

static unsigned long generator_seed (const unsigned long seed) noexcept
 
static Node * join_exclusive (Node *t1, Node *t2) noexcept
 
static Node * remove_pos (Node *&root, const size_t pos) noexcept
 

Private Attributes

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

Detailed Description

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

Extended Treap with rank support for O(log n) indexed access.

TreapRk is a Treap (tree + heap) that extends the basic treap with subtree size counters (COUNT), enabling O(log n) access by rank (inorder position). This allows treating the tree like a sorted dynamic array with efficient indexed operations.

Treap Properties (from base Treap):
  • Keys follow BST ordering: left < parent < right
  • Priorities follow max-heap ordering: parent priority > child priorities
  • Priorities are randomly assigned at insertion time
  • Expected O(log n) height due to randomization
Rank Extension:
Each node maintains COUNT = size of subtree rooted at that node. This enables:
  • select(i): Get the i-th smallest element, O(log n)
  • position(key): Get the rank of a key, O(log n)
  • remove_pos(i): Remove element at position i, O(log n)
  • split_pos(i): Split tree at position i, O(log n)
Comparison with Other Trees:
  • TreapRk vs Treap: TreapRk adds rank support (O(1) space per node)
  • TreapRk vs Rand_Tree: Similar functionality, but TreapRk is typically faster by a constant factor (bottom-up vs top-down balancing)
  • TreapRk vs Splay_Tree_Rk: TreapRk has expected (not amortized) complexity; Splay_Tree_Rk adapts to access patterns but lacks guarantees
Template Parameters
NodeTypeNode template (typically Treap_Rk_Node).
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
  • Remove by position: O(log n) expected
  • Split: O(log n) expected
  • Join: O(log n) expected
  • Space: O(n) + O(1) per node for priority and COUNT
Random Number Generator:
The priorities come from a Random_Source. A treap 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 priorities, and so the trees, are the same. random_source() gives the source, which can be asked for a SplitMix64 with a seed.
Example:
TreapRk<int> tree;
// Insert nodes
tree.insert(new TreapRk<int>::Node(42));
tree.insert(new TreapRk<int>::Node(17));
tree.insert(new TreapRk<int>::Node(99));
tree.insert(new TreapRk<int>::Node(8));
// 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 rank of 42
// Remove by position
auto removed = tree.remove_pos(1); // Remove 2nd smallest
delete removed;
// Split at position
TreapRk<int> left, right;
tree.split_pos(2, left, right); // Split after 2nd element
// Join trees
tree.join(left, right);
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
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetTreapRk.
All operations maintain COUNT values for ranked operations.
Faster than Rand_Tree by a constant factor.
See also
TreapRk Convenient typedef for TreapRk<Key, Compare>.
Treap Basic treap without rank support (slightly faster, less space).
Rand_Tree Alternative randomized tree with rank support.
Splay_Tree_Rk Self-adjusting alternative with rank support.
DynSetTreapRk High-level wrapper with automatic memory management.

Definition at line 189 of file tpl_treapRk.H.

Member Typedef Documentation

◆ Node

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

Definition at line 193 of file tpl_treapRk.H.

Constructor & Destructor Documentation

◆ Gen_Treap_Rk() [1/4]

template<template< class > class NodeType, class Key , class Compare >
Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::Gen_Treap_Rk ( const unsigned long  seed,
Compare  __cmp = Compare() 
)
inline

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

Parameters
[in]seedThe seed, taken through an unsigned int, modulo 2^32 - 1, as it was for GSL.
[in]__cmpThe comparison criteria, which the treap copies.
Exceptions
std::bad_allocIf there is not enough memory for the Mersenne Twister, or what the default construction of the key of the header node or the copy of __cmp throws.

Definition at line 263 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::head_ptr, Aleph::Min_Priority, and Aleph::PRIO().

◆ Gen_Treap_Rk() [2/4]

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

Builds an empty treap with a comparison criteria built by default, which draws its priorities 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 treap that inherits these, and what the construction of the comparison threw would end the program there.

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

Definition at line 284 of file tpl_treapRk.H.

◆ Gen_Treap_Rk() [3/4]

template<template< class > class NodeType, class Key , class Compare >
Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::Gen_Treap_Rk ( Compare  __cmp)
inlinenoexcept

Builds an empty treap with the comparison criteria __cmp, which draws its priorities from a SplitMix64 with a seed of its own.

It does not allocate.

Parameters
[in]__cmpThe comparison criteria, which the treap copies.
Exceptions
whatthe default construction of the key of the header node or the copy of __cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one.

Definition at line 304 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::head_ptr, Aleph::Min_Priority, and Aleph::PRIO().

◆ Gen_Treap_Rk() [4/4]

template<template< class > class NodeType, class Key , class Compare >
Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::Gen_Treap_Rk ( Gen_Treap_Rk< NodeType, Key, Compare > &&  tree)
inlinenoexcept

Takes the nodes, the comparison criteria and the generator of tree, which keeps no nodes, and draws from then on from a SplitMix64 with a seed of its own: it can take nodes again.

It does not allocate.

It builds no key, if it can: the key of the header node is built by default when that cannot throw; else it is that of the header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).

Parameters
[in,out]treeThe treap to move. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from.
Exceptions
whatthe copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of the header node throws, with a key whose move may throw too. Then tree keeps its nodes, but not its generator: it draws from a SplitMix64 with a seed of its own. Nothing when they are noexcept.

Definition at line 334 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::head_ptr, Aleph::Min_Priority, Aleph::PRIO(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

Member Function Documentation

◆ find_position()

template<template< class > class NodeType, class Key , class Compare >
std::pair< int, Node * > Aleph::Gen_Treap_Rk< 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 the tuple returns the position that would have key if this was in the tree and the parent node that the key would had. 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 inmediataly greater than key.
Parameters
[in]keyto be searched
Returns
a pair with the inorder position and 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 777 of file tpl_treapRk.H.

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

Referenced by Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::find_position(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::Iterator::reset_to_key().

◆ generator_seed()

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

Definition at line 205 of file tpl_treapRk.H.

References Aleph::Gsl_Compatible_Rng::max(), and seed.

◆ get_compare()

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

◆ getRoot() [1/2]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::getRoot ( ) const
inlinenoexcept

◆ getRoot() [2/2]

◆ insert() [1/2]

◆ insert() [2/2]

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

Insert a node in a treap.

insert(p) inserts the node p in this treap.

Parameters
[in]ppointer to the node to be inserted
Returns
if p->get_key() is not in the tree, then p is inserted and this node pointer is returned. Otherwise, it is returned nullptr.
Exceptions
anythingthat the comparison 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: the priority of p was drawn before the first comparison.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 513 of file tpl_treapRk.H.

References Aleph::Random_Source::get(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::insert(), Aleph::PRIO(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::r, and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ insert_dup() [1/2]

◆ insert_dup() [2/2]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< 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 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: the priority of p was drawn before the first comparison.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 535 of file tpl_treapRk.H.

References Aleph::Random_Source::get(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::insert_dup(), Aleph::PRIO(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::r, and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ is_empty()

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

Return true if tree is empty.

Definition at line 722 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ join() [1/2]

template<template< class > class NodeType, class Key , class Compare >
void Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join ( Gen_Treap_Rk< NodeType, Key, Compare > &  t,
Gen_Treap_Rk< 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]tramdomized tree to join with this
[out]dupramdomized 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 898 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ join() [2/2]

◆ join_dup() [1/2]

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

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

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

Parameters
[in]tramdomized 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 918 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join_dup(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ join_dup() [2/2]

template<template< class > class NodeType, class Key , class Compare >
void Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join_dup ( Node *&  t1,
Node *  t2 
)
inlineprivatenoexcept

◆ join_exclusive() [1/2]

template<template< class > class NodeType, class Key , class Compare >
void Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join_exclusive ( Gen_Treap_Rk< 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 knowlege allows a more effcient 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]tramdomized tree to exclusively join with this keys are inserted

Definition at line 935 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join_exclusive(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ join_exclusive() [2/2]

◆ key_comp()

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

return the comparison criteria

Definition at line 248 of file tpl_treapRk.H.

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

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

◆ random_source() [1/2]

template<template< class > class NodeType, class Key , class Compare >
const Random_Source & Aleph::Gen_Treap_Rk< 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 232 of file tpl_treapRk.H.

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

◆ random_source() [2/2]

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

The source of random numbers, which gives the priorities.

Definition at line 228 of file tpl_treapRk.H.

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

Referenced by TEST(), and TEST().

◆ remove() [1/3]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< 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 638 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::remove(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ remove() [2/3]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::remove ( const size_t  beg,
const size_t  end 
)
inline

Remove from the treap all the keys between inorder position beg and end.

Parameters
[in]begbeggining inorder position
[in]endending inorder position
Returns
a pointer to a tree root containing all the removed nodes
Exceptions
range_errorif the range is invalid

Definition at line 657 of file tpl_treapRk.H.

References ah_range_error_if, Aleph::COUNT(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::join_exclusive(), Aleph::split_pos_rec(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ remove() [3/3]

◆ remove_pos() [1/2]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::remove_pos ( const size_t  pos)
inline

Remove the node at the inorder position pos.

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

Definition at line 699 of file tpl_treapRk.H.

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

◆ remove_pos() [2/2]

template<template< class > class NodeType, class Key , class Compare >
static Node * Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::remove_pos ( Node *&  root,
const size_t  pos 
)
inlinestaticprivatenoexcept

◆ rng()

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

The Mersenne Twister of the treap, which a treap 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 treap 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 treap has no Mersenne Twister and there is not enough memory for it.

Definition at line 245 of file tpl_treapRk.H.

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

◆ search()

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

Search a key in a treap.

Parameters
[in]keyto be searched
Returns
a pointer to the node containing key if this is found; otherwise, nullptr is returned
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 377 of file tpl_treapRk.H.

References Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::cmp, Aleph::searchInBinTree(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

Referenced by main().

◆ search_or_insert() [1/2]

◆ search_or_insert() [2/2]

template<template< class > class NodeType, class Key , class Compare >
Node * Aleph::Gen_Treap_Rk< 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 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: the priority of p was drawn before the first comparison.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 562 of file tpl_treapRk.H.

References Aleph::Random_Source::get(), Aleph::PRIO(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::r, Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::search_or_insert(), and Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root.

◆ select()

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

Return the i-th node in order sense.

Parameters
[in]iinorder position of node to be selected
Returns
a pointer to the i-th node inorder sense
Exceptions
out_of_rangeif pos is greater or equal than the number of nodes of tree

Definition at line 713 of file tpl_treapRk.H.

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

Referenced by main().

◆ set_seed()

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

Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those of GSL for the seed.

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

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

Definition at line 222 of file tpl_treapRk.H.

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

Referenced by TEST().

◆ size()

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

Return the number of nodes contained in the tree.

Definition at line 719 of file tpl_treapRk.H.

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

Referenced by main().

◆ split_key()

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

Split the tree according to a key.

It splits with the comparison criteria of the tree, which links the nodes of t1 and t2 in its order: their criteria must order as it does, or their searches may not find the keys that they hold.

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 804 of file tpl_treapRk.H.

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

◆ split_key_dup()

template<template< class > class NodeType, class Key , class Compare >
void Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::split_key_dup ( const Key &  key,
Gen_Treap_Rk< NodeType, Key, Compare > &  t1,
Gen_Treap_Rk< 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, which links the nodes of t1 and t2 in its order: their criteria must order as it does, or their searches may not find the keys that they hold.

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 828 of file tpl_treapRk.H.

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

◆ split_pos()

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

Split the tree at the inorder position pos.

Parameters
[in]posinorder position where to split
[out]t1tree where the rank of keys \([0, pos)\) will be put
[out]t2tree where the rank of keys \([pos, n]\) will be put

Definition at line 840 of file tpl_treapRk.H.

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

◆ swap()

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

Swaps in constant time the nodes and the comparison criteria of this and tree.

Parameters
[in,out]treethe tree to swap with.
Exceptions
whatthe swap of the comparison criteria throws, which goes first: then the nodes stay where they were.

Definition at line 353 of file tpl_treapRk.H.

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

◆ verify()

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

Member Data Documentation

◆ cmp

◆ head

template<template< class > class NodeType, class Key , class Compare >
Node Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::head
private

The type of node.

Definition at line 197 of file tpl_treapRk.H.

◆ head_ptr

template<template< class > class NodeType, class Key , class Compare >
Node* Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::head_ptr
private

◆ r

◆ tree_root

template<template< class > class NodeType, class Key , class Compare >
Node*& Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::tree_root
private

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