|
ADS
Algorithms and Data Structures
|
Universal hashing, beyond the book [see HashFunctions.h for the fixed functions of section 7.1]: a class \( H \) of functions \( U \rightarrow \{ 0 , \ldots , m - 1 \} \) is universal if two distinct keys collide under at most \( | H | / m \) of its functions, and \( c \)-universal if under at most \( c | H | / m \); a function drawn at random from it gives every set of keys the expected lists of length \( 1 + \alpha \) of the simple uniform hashing, \( \alpha = n / m \), and the longest of them is \( O( \log n / \log \log n ) \) in expectation when \( m = \Theta( n ) \), whatever the keys, while for every fixed function there are \( n \) keys that fall in one list. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/Portable.h"#include "ADS/dictionary/FastHash.h"#include "ADS/dictionary/RandomHashing.h"#include "ADS/numeric/NumberTheory.h"#include "ADS/numeric/RandomNumbers.h"#include <algorithm>#include <bit>#include <cstddef>#include <cstdint>#include <functional>#include <iterator>#include <optional>#include <span>#include <string_view>#include <utility>#include <vector>#include "ADS/dictionary/impl/UniversalHashing.tpp"Go to the source code of this file.
Classes | |
| class | ADS::CarterWegmanHash |
| \( h( k ) = ( ( a k + b ) \bmod p ) \bmod m \), of the class \( H_{p,m} \) of Carter and Wegman More... | |
| class | ADS::DotProductHash |
| \( h( k ) = \sum_i a_i k_i \bmod m \), the \( k_i \) being the digits of \( k \) in base \( m \) More... | |
| class | ADS::MultiplyShiftHash |
| \( h( k ) = ( a k \bmod 2^h ) \operatorname{div} 2^{h - l} \), with \( a \) odd More... | |
| class | ADS::StringHash |
| \( h( s ) = \sum_i ( s_i + 1 ) x^i \bmod ( 2^{61} - 1 ) \), at a random point \( x \) More... | |
| class | ADS::MultiplicativeHash< K, Code > |
| the multiplicative hashing of section 5.1.1 of Morin's book, \( \mathrm{hash}( x ) = ( z x \bmod 2^w ) \operatorname{div} 2^{w-d} \) on a table of \( 2^d \) places, \( z \) a random odd word and \( x \) the hash code Code( k ) of the key More... | |
| class | ADS::CompoundHash |
| the hash code of an object of \( r \) parts of section 5.3.2 of Morin's book, \( h( x_0 , \ldots , x_{r-1} ) = ( ( z \sum_i z_i x_i ) \bmod
2^{2w} ) \operatorname{div} 2^w \) with \( w = 64 \), the \( z_i \) random words and \( z \) a random odd integer of \( 2 w \) bits: two distinct tuples collide with probability at most \( 3 / 2^w \) (Theorem 5.3 of Morin's book), where the XOR or the sum of the parts make large sets of tuples collide (exercises 5.7 to 5.9) More... | |
| class | ADS::SequenceHash |
| the hash code of the arrays and of the strings of section 5.3.3 of Morin's book, a polynomial over the prime field of \( p = 2^{61} - 1 \) with an end marker, \( h( x_0 , \ldots , x_{r-1} ) = ( \sum_i x_i z^i + ( p - 1 ) z^r )
\bmod p \) More... | |
| class | ADS::DLeftHashTable< K, V, H > |
| a DICTIONARY on \( d \) tables of lists, a key in the shortest of its \( d \) lists More... | |
| class | ADS::DLeftHashTable< K, V, H >::const_iterator |
| the iterator on the pairs, table after table and list after list More... | |
Namespaces | |
| namespace | ADS |
Universal hashing, beyond the book [see HashFunctions.h for the fixed functions of section 7.1]: a class \( H \) of functions \( U \rightarrow \{ 0 , \ldots , m - 1 \} \) is universal if two distinct keys collide under at most \( | H | / m \) of its functions, and \( c \)-universal if under at most \( c | H | / m \); a function drawn at random from it gives every set of keys the expected lists of length \( 1 + \alpha \) of the simple uniform hashing, \( \alpha = n / m \), and the longest of them is \( O( \log n / \log \log n ) \) in expectation when \( m = \Theta( n ) \), whatever the keys, while for every fixed function there are \( n \) keys that fall in one list.
The classes are
Against the code of Morin's book: the words are of 64 bits, where the book has \( w = 32 \), and the prime of SequenceHash is \( 2^{61} - 1 \), whose products are reduced by shifts and masks [see mulMod61()], where the book uses \( 2^{32} - 5 \) and a division; the collision bounds are \( 2^{32} \) times smaller for the same work per word.
DLeftHashTable is the d-left hashing of Vöcking on top of them: \( d \) tables of \( m / d \) lists, each with a function of its own, a key going to the shortest of its \( d \) lists and to the leftmost one on a tie; a search looks at the \( d \) lists. The longest list is then \( \ln \ln n / ( d \ln \phi_d ) + O( 1 ) \) with high probability for \( n = m \) keys, \( \phi_d \) being the ratio of the generalized Fibonacci numbers, against \( \Theta( \log n / \log \log n ) \) with one function: the power of two choices of Azar, Broder, Karlin and Upfal, an exponential gain for one more probe, which lets each list be a bucket of fixed size \( O( \log \log n ) \) instead of a linked list. Its functions are simple tabulation [see TabulationHash in RandomHashing.h], for which Dahlgaard, Knudsen, Rotenberg and Thorup proved a longest list of \( O( \log \log n ) \) with high probability.
The contracts [see Contract.h]: a key below the universe of its class (\( p \) for CarterWegmanHash, \( m^r \) for DotProductHash, \( 2^h \) for MultiplyShiftHash), the parameters of a class in its range (a prime \( p \) or \( m \) is the caller's duty, not checked), each hash below its \( m \); insert() of DLeftHashTable ensures that the key is there and remove() that it is not, and its invariant is that each key is in one of its \( d \) lists and is counted once, checked on the key of the operation only past 1024 keys, so that a long sequence of operations at the level 3 stays linear. The functions are defined in UniversalHashing.tpp.