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

a DICTIONARY on the cuckoo hashing, with a stash More...

#include <ADS/dictionary/CuckooHashing.h>

Classes

struct  Places
 the places and the stash, for SlotIterator 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 type of the items, for the standard library
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 between two iterators
using reference = PairRef< const K & , const V & >
 a reference to an item
using const_reference = PairRef< const K & , const V & >
 a constant reference to an item
using const_iterator = SlotIterator< CuckooHashTable , Places >
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items
using allocator_type = A
 the type of the allocator

Public member functions

 CuckooHashTable (std::size_t m=16, double maxLoad=0.4, std::size_t s=0, std::uint64_t seed=1, H h=H(), const A &alloc=A())
 the empty table of \( m \) places and a stash of \( s \) places, from alloc
std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing, from its two places \( h_1( k ) \) and \( h_2( k ) \) and the stash, \( O( 1 ) \) in the worst case
void insert (const K &k, const V &v)
 associates the value \( v \) to the key \( k \), in \( O( 1 ) \) amortized expected time
void remove (const K &k)
 removes the key \( k \), if it is there, emptying its place; the table is halved below a load of \( \mathit{maxLoad} / 4 \)
bool contains (const K &k) const
 true if the key \( k \) is there
std::size_t size (void) const
 the number of keys
std::size_t capacity (void) const
 the number \( m \) of places, the stash excluded
std::size_t rehashes (void) const
 the rebuilds with new functions after a failed insertion
std::size_t evictions (void) const
 the evictions made by the insertions
std::size_t stashed (void) const
 the keys in the stash
allocator_type get_allocator (void) const
 the allocator of the places
const_iterator begin (void) const
 the iterator on the first item
const_iterator end (void) const
 the iterator past the last item
const_iterator cbegin (void) const
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item

Detailed description

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
class ADS::CuckooHashTable< K, V, H, A >

a DICTIONARY on the cuckoo hashing, with a stash

Member typedef documentation

◆ allocator_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::allocator_type = A

the type of the allocator

◆ const_iterator

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::const_iterator = SlotIterator< CuckooHashTable , Places >

the iterator on the items, constant

◆ const_reference

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::const_reference = PairRef< const K & , const V & >

a constant reference to an item

◆ difference_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::iterator = const_iterator

the iterator on the items

◆ Key

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::Key = K

the type of the keys

◆ key_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::key_type = K

the type of the keys

◆ mapped_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::mapped_type = V

the type of the values

◆ reference

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::reference = PairRef< const K & , const V & >

a reference to an item

◆ size_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::size_type = std::size_t

the type of the sizes

◆ Value

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::Value = V

the type of the values

◆ value_type

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::CuckooHashTable< K, V, H, A >::value_type = std::pair< K , V >

the type of the items, for the standard library

Constructor and destructor documentation

◆ CuckooHashTable()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::CuckooHashTable< K, V, H, A >::CuckooHashTable ( std::size_t m = 16,
double maxLoad = 0.4,
std::size_t s = 0,
std::uint64_t seed = 1,
H h = H(),
const A & alloc = A() )
explicit

the empty table of \( m \) places and a stash of \( s \) places, from alloc

Precondition
\( m \ge 2 \) and \( 0 < \mathit{maxLoad} < 1 / 2 \)

Member function documentation

◆ begin()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::CuckooHashTable< K, V, H, A >::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ capacity()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::CuckooHashTable< K, V, H, A >::capacity ( void ) const
nodiscard

the number \( m \) of places, the stash excluded

◆ cbegin()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::CuckooHashTable< K, V, H, A >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::CuckooHashTable< K, V, H, A >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::CuckooHashTable< K, V, H, A >::contains ( const K & k) const
nodiscard

true if the key \( k \) is there

◆ end()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::CuckooHashTable< K, V, H, A >::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ evictions()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::CuckooHashTable< K, V, H, A >::evictions ( void ) const
nodiscard

the evictions made by the insertions

◆ get_allocator()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
allocator_type ADS::CuckooHashTable< K, V, H, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the places

◆ insert()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
void ADS::CuckooHashTable< K, V, H, A >::insert ( const K & k,
const V & v )

associates the value \( v \) to the key \( k \), in \( O( 1 ) \) amortized expected time

In pseudocode (Pagh and Rodler, chapter 8 of Ferragina), \( T \) the table, \( c \) the key in hand and \( \mathit{maxLoop} = \max\{ 8 \lceil \log_2 m \rceil + 16 , \lceil 3 \log_\gamma m \rceil \} \), the bound of Pagh and Rodler with the slack \( \gamma = 1 / ( 2\, \mathit{maxLoad} ) \) of the load:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad \textbf{if}\ k \text{ is there }\ \textbf{then}\ \text{give it } \text{the value } v;\ \textbf{return} \cr 3 & \quad \textbf{if}\ n + 1 > \mathit{maxLoad} \cdot m\ \textbf{then} \ \text{rebuild on } 2 m \text{ places} \cr 4 & \quad c \gets ( k , v ) \cr 5 & \quad \textbf{if}\ T[ h_1( k ) ] \text{ or } T[ h_2( k ) ] \text{ is } \text{empty}\ \textbf{then}\ \text{put } c \text{ there}; \ \textbf{return} \cr 6 & \quad p \gets h_1( k ) \cr 7 & \quad \textbf{for}\ t \gets 1\ \textbf{to}\ \mathit{maxLoop} \ \textbf{do} \cr 8 & \qquad \text{swap } c \text{ and } T[ p ]\quad \triangleright \ c \text{ is the key evicted} \cr 9 & \qquad p \gets \text{the other place of } c \cr 10 & \qquad \textbf{if}\ T[ p ] \text{ is empty }\ \textbf{then}\ T[ p ] \gets c;\ \textbf{return} \cr 11 & \quad \textbf{if}\ \text{the stash has an empty place } \ \textbf{then}\ \text{put } c \text{ there} \cr 12 & \quad \textbf{else if}\ \text{a rebuild has lengthened the stash} \ \textbf{then}\ \text{put } c \text{ in a new place at its end} \cr 13 & \quad \textbf{else}\ \text{rebuild with new functions and } c \text{, at most 8 times, the keys still homeless after the} \text{ 8th} \cr 14 & \qquad \text{in new places at the end of the stash} \end{array}} \]

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

◆ lookup()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::optional< V > ADS::CuckooHashTable< K, V, H, A >::lookup ( const K & k) const
nodiscard

the value of the key \( k \), or nothing, from its two places \( h_1( k ) \) and \( h_2( k ) \) and the stash, \( O( 1 ) \) in the worst case

◆ rehashes()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::CuckooHashTable< K, V, H, A >::rehashes ( void ) const
nodiscard

the rebuilds with new functions after a failed insertion

◆ remove()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
void ADS::CuckooHashTable< K, V, H, A >::remove ( const K & k)

removes the key \( k \), if it is there, emptying its place; the table is halved below a load of \( \mathit{maxLoad} / 4 \)

Postcondition
\( k \) is not there

◆ size()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::CuckooHashTable< K, V, H, A >::size ( void ) const
nodiscard

the number of keys

◆ stashed()

template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::CuckooHashTable< K, V, H, A >::stashed ( void ) const
nodiscard

the keys in the stash


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