|
ADS
Algorithms and Data Structures
|
The hash functions of section 7.1. More...
Files | |
| file | ADS/dictionary/HashFunctions.h |
| The hash functions of section 7.1. | |
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 \) | |
The hash functions of section 7.1.
The definitions, the invariants and the costs of the area are in Theory of dictionaries.
| 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::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
In pseudocode (section 7.1), \( \beta \) the base and \( c_1 , \ldots , c_\ell \) the characters of \( k \); taking the remainder at each step gives the same result, since \( ( x \bmod m ) \beta + c \equiv x \beta + c \pmod{m} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{division}( k , m , \beta , \mathit{seed} )\quad \triangleright\ k = c_1 c_2 \cdots c_\ell \text{ in base } \beta \cr 2 & \quad b \gets \mathit{seed} \bmod m \cr 3 & \quad \textbf{foreach}\ \text{digit } c_j \text{ of } k \text{, from } c_1\ \textbf{do}\ b \gets ( \beta b + c_j ) \bmod m\quad \triangleright\ \text{Horner, reduced at each step} \cr 4 & \quad \textbf{return}\ b \end{array}} \]
| 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 ) \)
In pseudocode (section 7.1):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extraction}( k , p , s )\quad \triangleright\ \text{the } p \text{ bits of } k \text{ from the } s \text{-th} \cr 2 & \quad \textbf{return}\ \text{the number written in binary by } \mathrm{bin}( k )[ s .. s + p - 1 ] \text{, the first bit the highest} \end{array}} \]
| 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} \)
In pseudocode (section 7.1, the version of Knuth, section 6.4 of TAOCP):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fibonacci}( i , p ) \cr 2 & \quad s \gets \lfloor C 2^{64} \rfloor\quad \triangleright \ \text{the conjugate of the golden ratio, scaled} \cr 3 & \quad r_0 \gets i s \bmod 2^{64} \cr 4 & \quad \textbf{return}\ \lfloor r_0 / 2^{64 - p} \rfloor \end{array}} \]
| 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 \)
In pseudocode (section 7.1):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{midsquare}( i , p ) \cr 2 & \quad q \gets i^2 \bmod 2^{64}\quad \triangleright\ \text{the } p \text{ bits about the middle of } q \cr 3 & \quad \textbf{return}\ \lfloor q / 2^{32 - \lfloor p / 2 \rfloor} \rfloor \bmod 2^p \end{array}} \]
| 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_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
In pseudocode (section 7.1), \( \ell \) the length of \( \mathrm{bin}( k ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{xor}( k , p ) \cr 2 & \quad \textbf{return}\ \bigoplus_{s = 0 , p , 2 p , \ldots ,\ s < \ell} \mathrm{extraction}( k , p , s )\quad \triangleright \ \text{the blocks of } p \text{ bits} \end{array}} \]
| std::uint64_t ADS::int_bin | ( | std::string_view | k | ) |
\( \mathrm{int}( \mathrm{bin}( k ) ) \) of a key of at most 8 characters