|
ADS
Algorithms and Data Structures
|
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7.1 and UniversalHashing.h for the universal classes]: hashes of 64 bits for the tables, the filters and the perfect hash functions, made of the folded product, the product of two words on 128 bits whose two halves are XORed [see foldedProduct()]. More...
#include "ADS/Abi.h"#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/Wide.h"#include "ADS/Simd.h"#include "ADS/memory/Prefetch.h"#include <array>#include <bit>#include <concepts>#include <cstddef>#include <cstdint>#include <cstring>#include <functional>#include <string>#include <string_view>#include <type_traits>#include <utility>#include "ADS/dictionary/impl/FastHash.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::Product128 |
| the product \( a b \) on 128 bits, its low and its high half More... | |
| struct | ADS::FastHash< std::string_view > |
| the hash of a string, by its bytes; any std::string_view, std::string and C string with the same characters hash the same More... | |
| struct | ADS::FastHash< std::string > |
| the hash of a std::string, that of its characters More... | |
| struct | ADS::FastHash< const char * > |
| the hash of a C string, that of its characters More... | |
Namespaces | |
| namespace | ADS |
Typedefs | |
| template<class K> | |
| using | ADS::DefaultHash = typename detail::DefaultHashOf< K >::type |
| the hash of the engineered tables and filters of ADS: FastHash< K > for the integers, the enumerations, the pointers, the floating point numbers and the strings, std::hash< K > for any other K | |
Functions | |
| constexpr Product128 | ADS::product128 (std::uint64_t a, std::uint64_t b) |
| the product \( a b \) on 128 bits | |
| constexpr std::uint64_t | ADS::foldedProduct (std::uint64_t a, std::uint64_t b) |
| the folded product: the low half of \( a b \) on 128 bits XOR its high half, \( ( a b \bmod 2^{64} ) \oplus \lfloor a b / 2^{64} \rfloor \) | |
| constexpr std::uint64_t | ADS::hashWord (std::uint64_t x, std::uint64_t seed=0) |
| the hash of the word \( x \) under the seed, the folded product of \( x \) and of the seed XOR a constant | |
| std::uint64_t | ADS::hashBytes (const void *p, std::size_t n, std::uint64_t seed=0) |
| the hash of the \( n \) bytes from \( p \) under the seed | |
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7.1 and UniversalHashing.h for the universal classes]: hashes of 64 bits for the tables, the filters and the perfect hash functions, made of the folded product, the product of two words on 128 bits whose two halves are XORed [see foldedProduct()].
Each bit of the folded product depends on every bit of its factors that the low half of the product does not lose, and a 64-bit processor computes it by one multiplication, so that a word costs one product and a string one product per 16 bytes.
Every bit of a hash depends on every bit of the key, the high half of a product carrying the bits that its low half loses, which is what a hash table needs; a single product does not give the full avalanche that the statistical batteries ask (one bit of a key flips each bit of the hash with probability near \( 1 / 2 \), not exactly \( 1 / 2 \)), as for the hashes of the short strings of Abseil. The functions are not cryptographic: a string chosen to make a factor of a product 0 loses the bits of the other one, and anyone who knows the seed can make collisions; a table that faces adversarial keys must use a seed drawn at random. A word is read in the byte order of the machine, so the hashes differ between little and big endian machines.
The contracts [see Contract.h]: a string of \( n \) bytes from a pointer that is not nil when \( n > 0 \). The functions are defined in FastHash.tpp.