ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::HashedTreap< K, V > Class Template Reference

a DICTIONARY on a treap whose priorities are the hashes of the addresses of the nodes More...

#include <ADS/tree/HashedTreap.h>

Classes

struct  Shape
 the shape of the tree, for PathIterator More...

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using value_type = std::pair< K , V >
 the pairs ( key , value ) by value, which own their copies
using key_type = K
 the type of the keys
using mapped_type = V
 the type of the values
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances
using reference = PairRef< const K & , const V & >
 references, by value
using const_reference = reference
 the same
using const_iterator = PathIterator< HashedTreap , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

 HashedTreap (std::uint64_t seed=1)
 the empty treap, the tables of its hash drawn from the seed
std::optional< V > lookup (const K &k) const
 the value of \( k \), or nothing, in \( O( \log n ) \) expected time
void insert (const K &k, const V &v)
 associates \( v \) to \( k \), in \( O( \log n ) \) expected time, by the insertion of Treap::insert() with \( p( u ) = h( \mathrm{address}( u ) ) \)
void remove (const K &k)
 removes \( k \), if it is there, in \( O( \log n ) \) expected time, by the removal of Treap::remove() with \( p( u ) = h( \mathrm{address}( u ) ) \)
bool contains (const K &k) const
 true if \( k \) is there
std::size_t size (void) const
 the number \( n \) of keys
int height (void) const
 the height, \( -1 \) if empty, in \( O( n ) \)
std::optional< std::pair< K, V > > lowerBound (const K &k) const
 the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin)
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
const K * lowerBoundKey (const K &k) const
 the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
const_iterator begin (void) const
 the iterator on the smallest key
const_iterator end (void) const
 the iterator past the largest key
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Detailed description

template<std::totally_ordered K, class V>
class ADS::HashedTreap< K, V >

a DICTIONARY on a treap whose priorities are the hashes of the addresses of the nodes

Member typedef documentation

◆ const_iterator

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::const_iterator = PathIterator< HashedTreap , Shape >

the constant iterator by increasing key, along the path from the root

◆ const_reference

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::mapped_type = V

the type of the values

◆ reference

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::reference = PairRef< const K & , const V & >

references, by value

◆ size_type

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V>
using ADS::HashedTreap< K, V >::value_type = std::pair< K , V >

the pairs ( key , value ) by value, which own their copies

Constructor and destructor documentation

◆ HashedTreap()

template<std::totally_ordered K, class V>
ADS::HashedTreap< K, V >::HashedTreap ( std::uint64_t seed = 1)
inlineexplicit

the empty treap, the tables of its hash drawn from the seed

Member function documentation

◆ begin()

template<std::totally_ordered K, class V>
const_iterator ADS::HashedTreap< K, V >::begin ( void ) const
inlinenodiscard

the iterator on the smallest key

◆ cbegin()

template<std::totally_ordered K, class V>
const_iterator ADS::HashedTreap< K, V >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered K, class V>
const_iterator ADS::HashedTreap< K, V >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<std::totally_ordered K, class V>
bool ADS::HashedTreap< K, V >::contains ( const K & k) const
nodiscard

true if \( k \) is there

◆ end()

template<std::totally_ordered K, class V>
const_iterator ADS::HashedTreap< K, V >::end ( void ) const
inlinenodiscard

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::HashedTreap< K, V >::findKey ( const K & k) const
nodiscard

the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ forEach()

template<std::totally_ordered K, class V>
template<class F>
void ADS::HashedTreap< K, V >::forEach ( F && f) const

f( k , v ) on the pairs by increasing keys

◆ height()

template<std::totally_ordered K, class V>
int ADS::HashedTreap< K, V >::height ( void ) const
nodiscard

the height, \( -1 \) if empty, in \( O( n ) \)

◆ insert()

template<std::totally_ordered K, class V>
void ADS::HashedTreap< K, V >::insert ( const K & k,
const V & v )

associates \( v \) to \( k \), in \( O( \log n ) \) expected time, by the insertion of Treap::insert() with \( p( u ) = h( \mathrm{address}( u ) ) \)

In pseudocode (Seidel and Aragon, beyond the book), the recursion returning 0 if \( k \) was there, 1 if the new node may still go up and 2 once it has stopped, so that no hash is computed above the first parent of larger priority:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( t , k , v ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then}\ t \gets \text{a new node of } ( k , v );\ \textbf{return}\ 1 \cr 3 & \quad \textbf{if}\ k = t.\mathit{key}\ \textbf{then} \ t.\mathit{value} \gets v;\ \textbf{return}\ 0 \cr 4 & \quad c \gets t.\mathit{left}\ \textbf{if}\ k < t.\mathit{key} \text{, else } t.\mathit{right};\ r \gets \mathrm{insert}( c , k , v ) \cr 5 & \quad \textbf{if}\ r \ne 1\ \textbf{then return}\ r \cr 6 & \quad \textbf{if}\ h( \mathrm{address}( c ) ) \le h( \mathrm{address}( t ) )\ \textbf{then return}\ 2 \cr 7 & \quad \mathrm{rotateRight}( t )\ \textbf{if}\ c = t.\mathit{left} \text{, else } \mathrm{rotateLeft}( t );\ \textbf{return}\ 1 \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ lookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::HashedTreap< K, V >::lookup ( const K & k) const
nodiscard

the value of \( k \), or nothing, in \( O( \log n ) \) expected time

◆ lowerBound()

template<std::totally_ordered K, class V>
std::optional< std::pair< K, V > > ADS::HashedTreap< K, V >::lowerBound ( const K & k) const
nodiscard

the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin)

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::HashedTreap< K, V >::lowerBoundKey ( const K & k) const
nodiscard

the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed

◆ remove()

template<std::totally_ordered K, class V>
void ADS::HashedTreap< K, V >::remove ( const K & k)

removes \( k \), if it is there, in \( O( \log n ) \) expected time, by the removal of Treap::remove() with \( p( u ) = h( \mathrm{address}( u ) ) \)

In pseudocode (Seidel and Aragon, beyond the book), the key rotated down, the child of larger hash going up, until it has one child at most, which takes its place:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( t , k ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \ \textbf{false} \cr 3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then return} \ \mathrm{remove}( t.\mathit{left} , k ) \cr 4 & \quad \textbf{if}\ t.\mathit{key} < k\ \textbf{then return} \ \mathrm{remove}( t.\mathit{right} , k ) \cr 5 & \quad \textbf{if}\ t \text{ has one child at most} \ \textbf{then}\ t \gets \text{that child, or } \textbf{nil};\ \textbf{return}\ \textbf{true} \cr 6 & \quad \textbf{if}\ h( \mathrm{address}( t.\mathit{left} ) ) > h( \mathrm{address}( t.\mathit{right} ) )\ \textbf{then} \ \mathrm{rotateRight}( t );\ \textbf{return}\ \mathrm{remove}( t.\mathit{right} , k ) \cr 7 & \quad \mathrm{rotateLeft}( t );\ \textbf{return} \ \mathrm{remove}( t.\mathit{left} , k ) \end{array}} \]

Postcondition
\( k \) is not there

◆ size()

template<std::totally_ordered K, class V>
std::size_t ADS::HashedTreap< K, V >::size ( void ) const
inlinenodiscard

the number \( n \) of keys


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