|
| | 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
|
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
template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , 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 \)
template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
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
template<class K, class V, class H = std::hash< K >, class A = std::allocator< std::pair< const K , V > >>
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