|
ADS
Algorithms and Data Structures
|
The cuckoo hashing of Pagh and Rodler, beyond the book: each key \( k \) is in one of the two places \( h_1( k ) \) and \( h_2( k ) \) of a table of \( m \) places, so that a search and a removal look at two places, \( O( 1 ) \) time in the worst case. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/dictionary/RandomHashing.h"#include "ADS/memory/AllocatorAware.h"#include <algorithm>#include <bit>#include <cmath>#include <cstddef>#include <cstdint>#include <functional>#include <optional>#include <utility>#include <vector>#include "ADS/dictionary/impl/CuckooHashing.tpp"Go to the source code of this file.
Classes | |
| class | ADS::CuckooHashTable< K, V, H, A > |
| a DICTIONARY on the cuckoo hashing, with a stash More... | |
| struct | ADS::CuckooHashTable< K, V, H, A >::Places |
| the places and the stash, for SlotIterator More... | |
Namespaces | |
| namespace | ADS |
The cuckoo hashing of Pagh and Rodler, beyond the book: each key \( k \) is in one of the two places \( h_1( k ) \) and \( h_2( k ) \) of a table of \( m \) places, so that a search and a removal look at two places, \( O( 1 ) \) time in the worst case.
An insertion puts \( k \) in the first empty one of its two places, else in \( h_1( k ) \), evicting the key \( y \) there, which moves to its other place, possibly evicting another key, and so on; after \( \mathit{MaxLoop} \) evictions the insertion gives up and the table is rebuilt with two new functions (a rehash). In the cuckoo graph, whose vertices are the places and whose edges are the keys, the evictions follow a path from a place of \( k \), and an insertion fails only if the component of \( k \) has more than one cycle; for \( m \ge 2 c n \) two places are joined by a shortest path of length \( L \) with probability at most \( c^{-L} / m \) (as chapter 8 of Ferragina shows), a key is in the bucket of another with probability \( O( 1 / m ) \), and a cycle in the final graph has probability at most \( 1 / ( c - 1 ) \), at most \( 1 / 2 \) for \( m \ge 6 n ( 1 + \epsilon ) \): an insertion takes \( O( 1 ) \) amortized expected time. With one table and both functions on it the graph is a random graph of \( m \) vertices and \( n \) edges, whose components have at most one cycle with high probability while \( n / m < 1 / 2 \); the table doubles past the load maxLoad (by default 0.4) and halves below \( \mathit{maxLoad} / 4 \), the global rebuilding of chapter 8 of Ferragina.
A stash of \( s \) places (Kirsch, Mitzenmacher and Wieder) keeps the keys whose insertion fails, lowering the probability of a rehash from \( \Theta( 1 / n ) \) to \( \Theta( 1 / n^{s+1} ) \); a search also looks at the stash. No pair of functions separates keys whose hashes are equal, and more than \( s + 2 \) of them would make the rehashes go on forever: a rebuild draws at most 8 pairs of functions, and with the last one puts the keys still homeless in new places at the end of the stash, where the next homeless keys go too, with no rebuild, until a rebuild finds them a place. A search then scans the longer stash, but the table keeps its size, at most \( \max( m_0 , 2 n / \mathit{maxLoad} ) \) places for \( n \) keys. The two functions are simple tabulation [see TabulationHash in RandomHashing.h] of the hash of the key, which, as Pătrașcu and Thorup proved, keeps the failure probability of cuckoo hashing at \( O( 1 / n^{1/3} ) \), while the analysis of Pagh and Rodler asks for \( O( \log n ) \)-independent functions (Aumueller, Dietzfelbinger and Woelfel give explicit ones that suffice with a stash). Compared with the open addressing [see OpenHashTable.h], a search has no cluster to walk; the variants of today put several keys in a place (blocked cuckoo hashing, loads above 90 %) or use \( d > 2 \) functions (Fotakis et al.).
\( \mathit{MaxLoop} \) is the larger of \( 8 \lceil \log_2 m \rceil + 16 \), which suits the small tables, and the \( \lceil 3 \log_\gamma m \rceil \) of the analysis of Pagh and Rodler, with the slack \( \gamma = 1 / ( 2\, \mathit{maxLoad} ) \) of the load.
The places come from the allocator A, std::allocator by default, with the rules of the standard [see AllocatorAware.h].
The contracts [see Contract.h]: \( m \ge 2 \) and \( 0 < \mathit{maxLoad} < 1 / 2 \); insert() ensures that the key is there and remove() that it is not; the invariant is that each key is in one of its two places or in the stash and that the keys are counted right, checked on the key of the operation only past 1024 places, so that a long sequence of operations at the level 3 stays linear. The operations are defined in CuckooHashing.tpp.