ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HashFunctions.h File Reference

The hash functions of section 7.1. More...

#include "ADS/Contract.h"
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <string_view>
#include "ADS/dictionary/impl/HashFunctions.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

unsigned ADS::bin_bit (std::string_view k, std::size_t j)
 the bit of place \( j \) of \( \mathrm{bin}( k ) \), counted from the left; 0 past the end
std::uint64_t ADS::int_bin (std::string_view k)
 \( \mathrm{int}( \mathrm{bin}( k ) ) \) of a key of at most 8 characters
std::uint64_t ADS::hash_extraction (std::string_view k, int p, std::size_t s)
 the extraction: the \( p \) bits of \( \mathrm{bin}( k ) \) from the place \( s \) on, counted from the left, as an integer in \( [ 0 , 2^p ) \)
std::uint64_t ADS::hash_xor (std::string_view k, int p)
 the XOR of the blocks of \( p \) bits of \( \mathrm{bin}( k ) \), the last one padded with zeros on the right
std::uint64_t ADS::hash_division (std::string_view k, std::uint64_t m, std::uint64_t base=256, std::uint64_t seed=0)
 the division \( \mathrm{int}( \mathrm{bin}( k ) ) \bmod m \) by the rule of Horner in the given base, each character being a digit, from the leading digit seed (the \( h = 5 \), \( h = 3 h + c \) is base 3 and seed 5); a character is any byte, up to 255 also in a base below 256
std::uint64_t ADS::hash_multiplication (std::uint64_t i, std::uint64_t m, long double C=(std::sqrt(5.0L) - 1)/2)
 the multiplication in floating point, the formula of the book, \( \lfloor m ( C i - \lfloor C i \rfloor ) \rfloor \): exact only while \( C i \) keeps enough digits after the point, i.e., for \( i \) well below the \( 2^{64} \) of the mantissa of a long double
std::uint64_t ADS::hash_fibonacci (std::uint64_t i, int p)
 the multiplication with integers for \( m = 2^p \), \( w = 64 \): the \( p \) most significant bits of the 64 least significant ones of \( i s \), with \( s = \lfloor C 2^{64} \rfloor \); it is \( \lfloor m \, \mathrm{frac}( i s / 2^{64} ) \rfloor \), which differs from the exact \( \lfloor m \, \mathrm{frac}( C i ) \rfloor \) by about \( m i / 2^{64} \)
std::uint64_t ADS::hash_midsquare (std::uint64_t i, int p)
 the middle square: the \( p \) bits in the middle of the 64 least significant ones of \( i^2 \)

Detailed description

The hash functions of section 7.1.

A hash function \( H : U \rightarrow \{ 0 , \ldots , m - 1 \} \) maps a key to a place of a vector of size \( m \); it has the simple uniformity if

\[ Q( i ) = \sum_{k : H( k ) = i} P( k ) = \frac{1}{m} \qquad \forall i \in \{ 0 , \ldots , m - 1 \} \]

where \( P( k ) \) is the probability that the key \( k \) is in the dictionary. A key is read as the string \( \mathrm{bin}( k ) \) of its bits, the bytes of a string one after the other from the first, the most significant bit of each first, and \( \mathrm{int}( b ) \) is the integer of the bit string \( b \). The functions are

  • the extraction, \( H( k ) = \mathrm{int}( b ) \) with \( b \) a block of \( p \) bits of \( \mathrm{bin}( k ) \) [see hash_extraction()];
  • the XOR, \( H( k ) = \mathrm{int}( \bigoplus_i b_i ) \) of the blocks of \( p \) bits of \( \mathrm{bin}( k ) \), the last one padded with zeros [see hash_xor()];
  • the division, \( H( k ) = \mathrm{int}( \mathrm{bin}( k ) ) \bmod m \), computed with the rule of Horner and a mod at each character so that nothing overflows [see hash_division()];
  • the multiplication, \( H( k ) = \lfloor m ( C i - \lfloor C i \rfloor ) \rfloor \) with \( i = \mathrm{int}( \mathrm{bin}( k ) ) \) and \( C = ( \sqrt{5} - 1 ) / 2 \), the conjugate of the golden ratio [see hash_multiplication()], and its version with integers for \( m = 2^p \): with \( s = \lfloor C 2^w \rfloor \) and \( i s = r_1 2^w + r_0 \), \( H( k ) \) is made of the \( p \) most significant bits of \( r_0 \) [see hash_fibonacci()];
  • the middle square, the \( p \) bits in the middle of \( i^2 \) [see hash_midsquare()].

The contracts [see Contract.h]: the number \( p \) of bits in its range, a key of at most 8 characters for int_bin(), and for hash_division() a size \( m > 0 \) and a base \( > 0 \) such that the rule of Horner does not overflow; each result is below the size it is meant for. The functions are defined in HashFunctions.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 7.1, https://algoritmicamente.it/materiali/libro/capitoli/cap07.pdf
D. E. Knuth, The Art of Computer Programming, vol. 3, Sorting and Searching, 2nd ed., Addison-Wesley, 1998, section 6.4
R. Morris, Scatter storage techniques, Communications of the ACM 11(1), 38-44, 1968, doi:10.1145/362851.362882
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa