Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Hash functions

Collection of high-performance hash functions for various data types. More...

Files

file  bloom-filter.H
 Probabilistic set membership with Bloom filters.
 
file  compact-cuckoo-filter.H
 Memory-optimized Cuckoo filter with bit-packed fingerprints.
 
file  compact-quotient-filter.H
 Memory-optimized quotient filter with bit-packed slots.
 
file  cuckoo-filter.H
 Probabilistic set membership with Cuckoo filters.
 
file  hash-dry.H
 Common hash table utilities and base classes.
 
file  hashDry.H
 Common operations for open addressing hash tables (CRTP mixin).
 
file  hashPars.H
 Hash table parameters.
 
file  primes.H
 Prime number utilities for hash tables and mathematical operations.
 
file  quotient-filter.H
 Quotient filter: a cache-friendly probabilistic set with deletion.
 
file  tpl_dynLhash.H
 Dynamic hash table mapping keys to records with separate chaining.
 
file  tpl_dynMapOhash.H
 Dynamic map with open hashing.
 
file  tpl_dynSetHash.H
 Dynamic set implementations based on hash tables.
 
file  tpl_hash.H
 Unified hash table interface.
 
file  tpl_lhash.H
 Linear hashing with dynamic bucket expansion.
 
file  tpl_linHash.H
 Linear hashing with chaining.
 
file  tpl_odhash.H
 Open addressing hash table with double hashing.
 
file  tpl_olhash.H
 Open addressing hash table with linear probing.
 

Classes

class  OhashCommon< HashTbl, Key >
 CRTP mixin providing common operations for open addressing hash tables. More...
 
class  OhashCommon< HashTbl, Key >::Iterator
 Bidirectional iterator for traversing hash table entries. More...
 
struct  OhashCommon< HashTbl, Key >::Stats
 Statistics about hash table performance. More...
 
class  HashStats< HashTbl >
 CRTP mixin providing statistics and configuration for chained hash tables. More...
 
struct  HashStats< HashTbl >::Stats
 Statistics about chain length distribution. More...
 
struct  Aleph::MapOpenHash< Key, Data, Cmp, HashTable >
 Open addressing hash map for key-value pairs. More...
 
struct  Aleph::MapOLhash< Key, Data, Cmp >
 Open addressing hash map using linear probing. More...
 
struct  Aleph::MapODhash< Key, Data, Cmp >
 Open addressing hash map using double hashing. More...
 
class  Aleph::DynHashTable< Key, HashTable, Cmp >
 Self-adjusting dynamic hash table. More...
 
class  Aleph::DynHashTable< Key, HashTable, Cmp >::Iterator
 
struct  Aleph::DynSetLhash< Key, Cmp >
 
struct  Aleph::DynSetLinHash< Key, Cmp >
 
class  Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >
 
class  Aleph::GenLhashTable< Key, BucketType, Cmp >
 Generic hash table with collision resolution by separate chaining. More...
 
class  Aleph::GenLhashTable< Key, BucketType, Cmp >::Iterator
 Iterator over a GenLhashTable hash table. More...
 
struct  Aleph::LhashBucketVtl< Key >
 Bucket with virtual destructor for a hash table with collision resolution by separate chaining. More...
 
struct  Aleph::LhashTable< Key, Cmp >
 Generic hash table with collision resolution by separate chaining and buckets without virtual destructor. More...
 
struct  Aleph::LhashTableVtl< Key, Cmp >
 Generic hash table with collision resolution by separate chaining and buckets with virtual destructor. More...
 
class  Aleph::LinHashBucket< Key >
 Bucket without virtual destructor for a hash table with collision resolution by separate chaining. More...
 
class  Aleph::LinHashBucketVtl< Key >
 Bucket with virtual destructor for a hash table with collision resolution by separate chaining. More...
 
class  Aleph::GenLinearHashTable< Key, BucketType, Cmp >
 Generic linear hash table. More...
 
class  Aleph::GenLinearHashTable< Key, BucketType, Cmp >::Iterator
 
class  Aleph::ODhashTable< Key, Cmp >
 Open addressing hash table with double hashing collision resolution. More...
 
struct  Aleph::ODhashTable< Key, Cmp >::Bucket
 
class  Aleph::OLhashTable< Key, Cmp >
 Open addressing hash table with linear probing collision resolution. More...
 
struct  Aleph::OLhashTable< Key, Cmp >::Bucket
 
class  Aleph::DynLhashTable< Key, Record, Cmp >
 Dynamic hash table mapping keys to records with separate chaining. More...
 

Typedefs

template<typename Key , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynSetHash = DynHashTable< Key, LhashTable, Cmp >
 
template<typename Key , typename Data , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynMapLinHash = DynMapHashTable< Key, Data, LinearHashTable, Cmp >
 
template<typename Key , typename Data , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynMapHash = DynMapHashTable< Key, Data, LhashTable, Cmp >
 
template<typename Key >
using Aleph::LhashBucket = Dnode< Key >
 Bucket without virtual destructor for a hash table with collision resolution by separate chaining.
 
template<typename Key , class Cmp = Aleph::equal_to<Key>>
using Aleph::LinearHashTable = GenLinearHashTable< Key, LinHashBucket, Cmp >
 Linear hash table with buckets without virtual destructor.
 
template<typename Key , class Cmp = Aleph::equal_to<Key>>
using Aleph::LinearHashTableVtl = GenLinearHashTable< Key, LinHashBucketVtl, Cmp >
 Linear hash table with virtual destructor in its buckets.
 

Functions

void Aleph::init_jsw () noexcept
 Initializes the randomized lookup table used by jsw_hash().
 
void Aleph::init_jsw (std::uint32_t seed) noexcept
 Initializes the randomized lookup table used by jsw_hash().
 
bool Aleph::is_jsw_initialized () noexcept
 Checks if the jsw_hash() lookup table has been initialized.
 
size_t Aleph::add_hash (const void *key, const size_t len) noexcept
 Additive hash.
 
size_t Aleph::xor_hash (const void *key, const size_t len) noexcept
 XOR hash.
 
size_t Aleph::rot_hash (const void *key, const size_t len) noexcept
 Rotating hash.
 
size_t Aleph::djb_hash (const void *key, const size_t len) noexcept
 Bernstein's hash (DJB hash)
 
size_t Aleph::sax_hash (const void *key, const size_t len) noexcept
 Shift-Add-XOR hash (SAX hash)
 
size_t Aleph::fnv_hash (const void *key, const size_t len) noexcept
 FNV-1a hash.
 
size_t Aleph::oat_hash (const void *key, const size_t len) noexcept
 One-at-a-Time hash (OAT hash)
 
size_t Aleph::jsw_hash (const void *key, size_t len) noexcept
 JSW hash (Julienne Walker)
 
size_t Aleph::elf_hash (const void *key, const size_t len) noexcept
 ELF hash.
 
size_t Aleph::jen_hash (const void *key, size_t len, unsigned initval) noexcept
 Jenkins hash (lookup3)
 
size_t Aleph::SuperFastHash (const void *key, size_t len, std::uint32_t seed=0) noexcept
 Paul Hsieh super fast hash function.
 
size_t Aleph::xxhash64_hash (const void *key, size_t len, std::uint64_t seed=Default_Hash_Seed) noexcept
 xxHash64 from the xxHash family.
 
size_t Aleph::wyhash_hash (const void *key, size_t len, std::uint64_t seed=Default_Hash_Seed) noexcept
 wyhash non-cryptographic hash.
 
size_t Aleph::siphash24_hash (const void *key, size_t len, std::uint64_t key0=0x0706050403020100ULL, std::uint64_t key1=0x0f0e0d0c0b0a0908ULL) noexcept
 SipHash-2-4 keyed hash.
 
template<typename Key >
requires std::is_trivially_copyable_v<Key>
size_t Aleph::jsw_hash (const Key &key) noexcept
 JSW hash for arbitrary key types.
 
size_t Aleph::jsw_hash (const std::string &key) noexcept
 JSW hash for std::string.
 

Detailed Description

Collection of high-performance hash functions for various data types.

Aleph-w provides a wide range of hash functions, from classical ones (like FNV, DJB, ELF) to modern ones (like Murmur3, xxHash64, wyhash, SipHash).

The default policy is defined by:

Key highlights:

Typedef Documentation

◆ DynMapHash

template<typename Key , typename Data , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynMapHash = typedef DynMapHashTable<Key, Data, LhashTable, Cmp>

Definition at line 739 of file tpl_dynSetHash.H.

◆ DynMapLinHash

template<typename Key , typename Data , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynMapLinHash = typedef DynMapHashTable<Key, Data, LinearHashTable, Cmp>

Definition at line 731 of file tpl_dynSetHash.H.

◆ DynSetHash

template<typename Key , class Cmp = Aleph::equal_to<Key>>
using Aleph::DynSetHash = typedef DynHashTable<Key, LhashTable, Cmp>

Definition at line 473 of file tpl_dynSetHash.H.

◆ LhashBucket

Bucket without virtual destructor for a hash table with collision resolution by separate chaining.

Parameters
Keyhash lookup key type
See also
LhashTable

Definition at line 802 of file tpl_lhash.H.

◆ LinearHashTable

template<typename Key , class Cmp = Aleph::equal_to<Key>>
using Aleph::LinearHashTable = typedef GenLinearHashTable<Key, LinHashBucket, Cmp>

Linear hash table with buckets without virtual destructor.

Basically, a linear hash table is a hash table with collision resolution by separate chaining but with the difference that the table size increases dynamically to ensure that the load factor, typically called \(\alpha\), is bounded between two lower and upper values, respectively.

Internally, the table uses a dynamic array of type DynArray. This leads to memory savings for table entries that have not been written.

Parameters
Keythe key type by which the table is indexed.
Cmpcomparison class between keys.
See also
DynArray LinearHashTableVtl

Definition at line 676 of file tpl_linHash.H.

◆ LinearHashTableVtl

Linear hash table with virtual destructor in its buckets.

Basically, a linear hash table is a hash table with collision resolution by separate chaining but with the difference that the table size increases dynamically to ensure that the load factor, typically called \(\alpha\), is bounded between two lower and upper values, respectively.

Internally, the table uses a dynamic array of type DynArray. This leads to memory savings for table entries that have not been written.

Parameters
Keythe key type by which the table is indexed.
Cmpcomparison class between keys.
See also
DynArray LinearHashTable

Definition at line 698 of file tpl_linHash.H.

Function Documentation

◆ add_hash()

size_t Aleph::add_hash ( const void key,
const size_t  len 
)
inlinenoexcept

Additive hash.

"Probably the simplest algorithm for hashing a sequence of integral values (such as a string), is to add all of the characters together and then force the range into something suitable for lookup with the remainder of division. I will give an example of this algorithm only because books commonly suggest it in their rush to get past the topic of hash functions on their way to collision resolution methods. This algorithm is very bad"

Author
Julienne Walker

Definition at line 101 of file hash-fct.H.

References h.

Referenced by Aleph::add_hash(), Aleph::add_hash(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ djb_hash()

size_t Aleph::djb_hash ( const void key,
const size_t  len 
)
inlinenoexcept

Bernstein's hash (DJB hash)

"The DJB hash is a simple and effective hash function that works well on many types of data. It was first published by Daniel J. Bernstein on comp.lang.c"

Author
Daniel J. Bernstein

Definition at line 163 of file hash-fct.H.

References h.

Referenced by Aleph::djb_hash(), Aleph::djb_hash(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ elf_hash()

size_t Aleph::elf_hash ( const void key,
const size_t  len 
)
inlinenoexcept

ELF hash.

"The ELF hash function has been around for a while, and it is believed to be one of the better algorithms out there. In my experience, this is true, though ELF hash does not perform sufficiently better than most of the other algorithms presented in this tutorial to justify its slightly more complicated implementation. It should be on your list of first functions to test in a new lookup implementation"

Author
Julienne Walker

Definition at line 279 of file hash-fct.H.

References Aleph::divide_and_conquer_partition_dp(), and h.

Referenced by Aleph::elf_hash(), Aleph::elf_hash(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ fnv_hash()

size_t Aleph::fnv_hash ( const void key,
const size_t  len 
)
inlinenoexcept

FNV-1a hash.

"The FNV-1a hash is a high-performance hash function that is designed to be fast while maintaining a low collision rate. It is widely used in many applications"

Author
Fowler, Noll, and Vo

Definition at line 203 of file hash-fct.H.

References h.

Referenced by Aleph::fnv_hash(), Aleph::fnv_hash(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ init_jsw() [1/2]

void Aleph::init_jsw ( )
noexcept

Initializes the randomized lookup table used by jsw_hash().

This overload uses the fixed Default_Hash_Seed so results are deterministic and reproducible across runs.

Definition at line 219 of file hash-fct.C.

References Aleph::Default_Hash_Seed, Aleph::divide_and_conquer_partition_dp(), Aleph::init, Aleph::jsw_fill_table(), Aleph::jsw_init_flag, and Aleph::jsw_mtx.

Referenced by HashTestEnvironment::SetUp(), HashValidation::SetUpTestSuite(), and TEST().

◆ init_jsw() [2/2]

void Aleph::init_jsw ( std::uint32_t  seed)
noexcept

Initializes the randomized lookup table used by jsw_hash().

This overload is deterministic and is intended for reproducible tests and benchmarks.

Parameters
seedSeed used to populate the internal lookup table.

Definition at line 238 of file hash-fct.C.

References Aleph::jsw_fill_table(), Aleph::jsw_mtx, and seed.

◆ is_jsw_initialized()

bool Aleph::is_jsw_initialized ( )
noexcept

Checks if the jsw_hash() lookup table has been initialized.

Returns
true if initialized, false otherwise.

Definition at line 211 of file hash-fct.C.

References Aleph::init.

Referenced by TEST().

◆ jen_hash()

size_t Aleph::jen_hash ( const void key,
size_t  len,
unsigned  initval 
)
noexcept

Jenkins hash (lookup3)

"Bob Jenkins' lookup3.c, is the result of many years of research and testing. It is considered one of the best general-purpose hash functions available"

Author
Bob Jenkins

Definition at line 325 of file hash-fct.C.

References Aleph::divide_and_conquer_partition_dp(), jen_final, jen_mix, and k.

Referenced by Aleph::jen_hash(), Aleph::jen_hash(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ jsw_hash() [1/3]

template<typename Key >
requires std::is_trivially_copyable_v<Key>
size_t Aleph::jsw_hash ( const Key &  key)
inlinenoexcept

JSW hash for arbitrary key types.

Definition at line 568 of file hash-fct.H.

References Aleph::jsw_hash().

◆ jsw_hash() [2/3]

size_t Aleph::jsw_hash ( const std::string &  key)
inlinenoexcept

JSW hash for std::string.

Definition at line 796 of file hash-fct.H.

References Aleph::jsw_hash().

◆ jsw_hash() [3/3]

size_t Aleph::jsw_hash ( const void key,
size_t  len 
)
noexcept

JSW hash (Julienne Walker)

"This is a hash of my own devising that combines a rotating hash with a table of randomly generated numbers. The algorithm walks through each byte of the input, and uses it as an index into a table of random integers generated by a good random number generator. The internal state is rotated to mix it up a bit, then XORed with the random number from the table. The result is a uniform distribution if the random numbers are uniform. The size of the table should match the values in a byte. For example, if a byte is eight bits then the table would hold 256 random numbers"

Note
If init_jsw() has not been called, it will be lazily initialized with Default_Hash_Seed (deterministic).
Parameters
keyPointer to the data to hash
lenLength of the data in bytes
Returns
Hash value
Author
Julienne Walker

Definition at line 247 of file hash-fct.C.

References Aleph::Default_Hash_Seed, Aleph::divide_and_conquer_partition_dp(), h, Aleph::init, Aleph::jsw_fill_table(), Aleph::jsw_init_flag, Aleph::jsw_mtx, and Aleph::tab.

Referenced by Aleph::jsw_hash(), Aleph::jsw_hash(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ oat_hash()

size_t Aleph::oat_hash ( const void key,
const size_t  len 
)
inlinenoexcept

One-at-a-Time hash (OAT hash)

"The One-at-a-Time hash is a simple and effective hash function designed by Bob Jenkins. It is used in many applications, including the Perl interpreter"

Author
Bob Jenkins

Definition at line 223 of file hash-fct.H.

References h.

Referenced by Aleph::oat_hash(), Aleph::oat_hash(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ rot_hash()

size_t Aleph::rot_hash ( const void key,
const size_t  len 
)
inlinenoexcept

Rotating hash.

"The rotating hash is identical to the XOR hash except that it incorporates a shift and the addition of the next byte into the loop instead of just XORing it. Many people mistakenly call this hash the DJB hash"

Author
Julienne Walker

Definition at line 143 of file hash-fct.H.

References h.

Referenced by Aleph::rot_hash(), Aleph::rot_hash(), TEST_F(), TEST_F(), and TEST_F().

◆ sax_hash()

size_t Aleph::sax_hash ( const void key,
const size_t  len 
)
inlinenoexcept

Shift-Add-XOR hash (SAX hash)

"The SAX hash is a simple hash function that works well on many types of data. It is similar to the rotating hash except that it uses addition instead of XOR to mix the bits"

Author
Julienne Walker

Definition at line 183 of file hash-fct.H.

References h.

Referenced by Aleph::sax_hash(), Aleph::sax_hash(), TEST(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ siphash24_hash()

size_t Aleph::siphash24_hash ( const void key,
size_t  len,
std::uint64_t  key0 = 0x0706050403020100ULL,
std::uint64_t  key1 = 0x0f0e0d0c0b0a0908ULL 
)
noexcept

SipHash-2-4 keyed hash.

This function implements SipHash-2-4, a keyed PRF-style hash. When called with secret, per-process keys it provides protection against hash-flooding attacks from adversarial inputs.

Warning
The default key values (key0 = 0x0706050403020100, key1 = 0x0f0e0d0c0b0a0908) are public constants. Overloads that use these defaults are deterministic and do not provide adversarial resistance. For hash-flooding protection, generate secret keys at process startup (e.g., via std::random_device) and pass them explicitly on every call.
Parameters
keyPointer to the input bytes.
lenLength of the input in bytes.
key0First 64-bit secret key word.
key1Second 64-bit secret key word.
Returns
Hash value truncated to size_t.

Definition at line 766 of file hash-fct.C.

References Aleph::divide_and_conquer_partition_dp(), m, Aleph::read_le64(), and ROTL64.

Referenced by Aleph::siphash24_hash(), Aleph::siphash24_hash(), Aleph::siphash24_hash(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ SuperFastHash()

size_t Aleph::SuperFastHash ( const void key,
size_t  len,
std::uint32_t  seed = 0 
)
inlinenoexcept

Paul Hsieh super fast hash function.

Parameters
keyPointer to the data to hash.
lenLength of the data in bytes.
seedOptional seed to randomize the hash.
Author
Paul Hsieh

Definition at line 408 of file hash-fct.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::get16bits(), and seed.

Referenced by Aleph::dft_hash_fct(), Aleph::dft_hash_fct(), Aleph::Aleph_Hash< float >::operator()(), Aleph::Aleph_Hash< float >::operator()(), Aleph::snd_hash_fct(), Aleph::SuperFastHash(), Aleph::SuperFastHash(), Aleph::SuperFastHash(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ wyhash_hash()

size_t Aleph::wyhash_hash ( const void key,
size_t  len,
std::uint64_t  seed = Default_Hash_Seed 
)
noexcept

wyhash non-cryptographic hash.

This function implements a wyhash-style 64-bit hash suitable for hash tables and other latency-sensitive data structures.

Parameters
keyPointer to the input bytes.
lenLength of the input in bytes.
seedSeed used to randomize the hash stream.
Returns
Hash value truncated to size_t.

Definition at line 694 of file hash-fct.C.

References Aleph::divide_and_conquer_partition_dp(), Aleph::read_le32(), Aleph::read_le64(), seed, and Aleph::wyhash_mix().

Referenced by Aleph::dft_hash_fct(), Aleph::dft_hash_fct(), Aleph::dft_hash_fct(), Aleph::dft_hash_fct(), Aleph::Aleph_Hash< T >::operator()(), Aleph::Aleph_Hash< T >::operator()(), Aleph::Aleph_Hash< double >::operator()(), Aleph::Aleph_Hash< double >::operator()(), Aleph::snd_hash_fct(), Aleph::snd_hash_fct(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), Aleph::wyhash_hash(), Aleph::wyhash_hash(), and Aleph::wyhash_hash().

◆ xor_hash()

size_t Aleph::xor_hash ( const void key,
const size_t  len 
)
inlinenoexcept

XOR hash.

"The XOR hash is another algorithm commonly suggested by textbooks. Instead of adding together the bytes of an object as the additive hash does, the XOR hash repeatedly folds the bytes together to produce a seemingly random hash value"

Author
Julienne Walker

Definition at line 122 of file hash-fct.H.

References h.

Referenced by TEST(), TEST_F(), TEST_F(), TEST_F(), Aleph::xor_hash(), and Aleph::xor_hash().

◆ xxhash64_hash()

size_t Aleph::xxhash64_hash ( const void key,
size_t  len,
std::uint64_t  seed = Default_Hash_Seed 
)
noexcept

xxHash64 from the xxHash family.

This function implements the 64-bit xxHash variant and is intended as a modern high-throughput non-cryptographic hash for in-memory tables, sketches and filters.

Parameters
keyPointer to the input bytes.
lenLength of the input in bytes.
seedSeed used to randomize the hash stream.
Returns
Hash value truncated to size_t.

Definition at line 611 of file hash-fct.C.

References Aleph::divide_and_conquer_partition_dp(), Aleph::read_le32(), Aleph::read_le64(), ROTL64, seed, Aleph::xxh64_merge_round(), and Aleph::xxh64_round().

Referenced by TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), Aleph::xxhash64_hash(), Aleph::xxhash64_hash(), and Aleph::xxhash64_hash().