|
ADS
Algorithms and Data Structures
|
The minimal perfect hash function of ADS by recursive splitting, the RecSplit of Esposito, Mueller Graf and Vigna, beyond the book [see MinimalPerfectHash.h for the one by pilots, faster but about 3 bits per key]: a function that maps the \( n \) distinct keys of a static set \( S \) onto \( \{ 0 , \ldots , n - 1 \} \) with no collision in about 1.8 bits per key for the leaves of 8 keys and the buckets of 100, against the lower bound of \( \log_2 e \approx 1.4427 \) bits per key of any minimal perfect hash function. More...
#include "ADS/Contract.h"#include "ADS/dictionary/FastHash.h"#include "ADS/external/MappedFile.h"#include "ADS/succinct/BitVector.h"#include <algorithm>#include <array>#include <bit>#include <cmath>#include <cstddef>#include <cstdint>#include <memory>#include <stdexcept>#include <string_view>#include <type_traits>#include <utility>#include <vector>#include "ADS/dictionary/impl/RecSplit.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RecSplit< T, L, H > |
| a minimal perfect hash function of a static set of keys by recursive splitting, with leaves of at most L keys More... | |
Namespaces | |
| namespace | ADS |
The minimal perfect hash function of ADS by recursive splitting, the RecSplit of Esposito, Mueller Graf and Vigna, beyond the book [see MinimalPerfectHash.h for the one by pilots, faster but about 3 bits per key]: a function that maps the \( n \) distinct keys of a static set \( S \) onto \( \{ 0 , \ldots , n - 1 \} \) with no collision in about 1.8 bits per key for the leaves of 8 keys and the buckets of 100, against the lower bound of \( \log_2 e \approx 1.4427 \) bits per key of any minimal perfect hash function.
A key goes to the bucket \( \lfloor h_1 \lceil n / b \rceil / 2^{64} \rfloor \) of \( \lceil n / b \rceil \) buckets, \( b \) keys each on average, by its first hash \( h_1 \); the \( m \) keys of a bucket are then split recursively by their second hash \( h_2 \), so that each leaf of the splitting tree holds at most \( \ell \) keys (the template parameter L). A node of \( m \) keys with the seed \( x \) sends a key to the place \( \rho_x = \lfloor g_{x , d}( h_2 ) \, m / 2^{64} \rfloor \in [ 0 , m ) \), where \( g_{x , d} \) is a folded product salted by \( x \) and by the depth \( d \) of the node, and its children by the place:
The seed of a node is the first \( x \) that gives each part its size, with probability
\[ p = \frac{ m! }{ k_0! \cdots k_{s-1}! } \prod_{i} \Bigl( \frac{ k_i }{ m } \Bigr)^{ k_i } \]
for the parts of \( k_0 , \ldots , k_{s-1} \) keys, so that the seed is geometric of parameter \( p \) and the product of the probabilities of all the nodes of a tree of \( m \) keys is \( m! / m^m \) whatever the shape (the invariance of RecSplit). The seeds are written in preorder by a code of Golomb and Rice, whose parameter for a geometric source of parameter \( p \) is the optimal one of Kiely,
\[ r( p ) = \max\Bigl\{ 0 , \Bigl\lceil \log_2 \frac{ -\log_2 \varphi }{ \log_2 ( 1 - p ) } \Bigr\rceil \Bigr\} , \qquad \varphi = \frac{ 1 + \sqrt{ 5 } }{ 2 } , \]
a function of \( m \) alone, so that the code of a seed takes fewer than \( \log_2 ( 1 / p ) + 1.563 \) bits in expectation, and a tree of \( m \) keys with \( t \) nodes at most \( 1.563 t + m \log_2 e \) bits. The \( r( p ) \) low bits of all the seeds of a bucket come first, then all their unary parts; a table of the sizes \( m < 4096 \), computed once for each \( \ell \), gives \( r \), the low bits of a subtree and its number of nodes, so that a subtree is skipped by moving a pointer on the low bits and by a select on the unary parts. The numbers of keys before each bucket and the place of its code are two Elias-Fano sequences, the second one less \( \beta \) times the first, for the \( \beta \) bits per key of the codes, and both less \( i \) times their least gap, which leaves to their low bits only the deviations from the average; each keeps the place of every 64th 1 of its high bits, 64 bits per 64 buckets, so that a query costs two selects of one or two words each instead of the searches of a general select, the numbers of keys before the bucket and before the next one coming from the same select; the select in a word, there and in the unary parts, is the broadword select of selectInWord().
The search of a leaf uses the rotation fitting of Bez, Kurpicz, Lehmann and Sanders: the keys of the leaf go to two sets \( A \) and \( B \) by the highest bit of \( h_2 \); for each \( x \), the places of \( A \) and of \( B \) are two masks of \( m \) bits (\( x \) is discarded if a set has two keys in one place), and the leaf takes the seed \( x m + r \) for the first rotation \( r \) of the mask of \( B \) that fills the holes of the mask of \( A \). A key of \( B \) goes to the place \( ( \rho_x + r ) \bmod m \); each \( x \) tries \( m \) rotations, so that the leaf evaluates about \( m \) times fewer hashes for the same expected seed. The leaves of \( \ell \le 24 \) keys fit in a word, and the search grows as \( e^\ell \): \( 1 \le L \le 24 \) (a build at \( \ell = 16 \) is already slow).
An evaluation computes the two hashes and the bucket, reads the three values of the Elias-Fano sequences (three select) and descends the \( O( \log ( b / \ell ) ) \) levels of the tree, each one a read of the low bits, a scan of the unary part and, to skip the earlier siblings, a select in a word or a few; the build takes expected \( O( n \, e^\ell / ( \ell \sqrt{ \ell } ) ) \) time. For a key out of \( S \) the result is some place below \( n \).
The keys are hashed by h( k ), taken as it is if H declares is_avalanching [see FastHash.h] and mixed by hashWord() otherwise; the two hashes of 64 bits are two folded products of that hash XOR the seed, 128 bits per key. Two keys of a bucket with the same \( h_2 \), or a bucket of 4096 keys or more, make the build draw a new seed; two keys with the same hash of 64 bits cannot be separated.
Consensus-RecSplit of Lehmann, Sanders, Walzer and Ziegler is the space frontier of this family: it searches the seeds of consecutive nodes together, each one a continuation of the bits of the previous ones, and takes \( ( 1 + \varepsilon ) \, n \log_2 e \) bits in \( O( n / \varepsilon ) \) expected time.
The contracts [see Contract.h]: the keys distinct (an audit), fewer than \( 2^{32} \), \( 1 \le b \le 2000 \); after the build the keys are mapped onto \( \{ 0 , \ldots , n - 1 \} \), an audit. A build that draws 100 seeds with no success throws std::runtime_error. The operations are defined in RecSplit.tpp.