nfx-hashing 0.1.2
Modern C++20 header-only hashing library with hardware acceleration
Loading...
Searching...
No Matches
nfx::hashing::Hasher< HashType, Seed > Struct Template Referencefinal

General-purpose STL-compatible hash functor supporting multiple types. More...

#include <nfx/hashing/Hasher.h>

Public Types

using is_transparent = void
 Enables transparent lookup in STL containers.

Public Member Functions

HashType operator() (std::string_view key) const noexcept
 Hashes a std::string_view using CRC32-C algorithm.
HashType operator() (const std::string &key) const noexcept
 Hashes a std::string using CRC32-C algorithm.
HashType operator() (const char *key) const noexcept
 Hashes a C-style string using CRC32-C algorithm.
template<typename TKey>
std::enable_if_t< std::is_integral_v< TKey >, HashType > operator() (const TKey &key) const noexcept
 Hashes an integral type using multiplicative hashing.
template<typename T>
std::enable_if_t< std::is_floating_point_v< T >, HashType > operator() (T value) const noexcept
 Hashes a floating-point value with normalization.
template<typename T>
std::enable_if_t< std::is_pointer_v< T > &&!std::is_same_v< T, const char * > &&!std::is_same_v< T, char * >, HashType > operator() (T ptr) const noexcept
 Hashes a pointer by its address.
template<typename TKey>
std::enable_if_t< std::is_enum_v< TKey >, HashType > operator() (const TKey &key) const noexcept
 Hashes an enum by its underlying integral value.
template<typename T, size_t N>
HashType operator() (const std::array< T, N > &arr) const noexcept
 Hashes a std::array by combining hashes of all elements.
template<typename T>
HashType operator() (const std::optional< T > &opt) const noexcept
 Hashes a std::optional - nullopt has distinct hash from any value.
template<typename T1, typename T2>
HashType operator() (const std::pair< T1, T2 > &p) const noexcept
 Hashes a std::pair by combining hashes of both elements.
template<typename T, std::size_t Extent = std::dynamic_extent>
HashType operator() (std::span< T, Extent > sp) const noexcept
 Hashes a std::span by combining hashes of all elements in the view.
template<typename... Ts>
HashType operator() (const std::tuple< Ts... > &t) const noexcept
 Hashes a std::tuple by combining hashes of all elements.
template<typename... Ts>
HashType operator() (const std::variant< Ts... > &var) const noexcept
 Hashes a std::variant by combining index and active alternative's hash.
template<typename T>
HashType operator() (const std::vector< T > &vec) const noexcept
 Hashes a std::vector by combining size and hashes of all elements.
template<typename TKey>
std::enable_if_t<!std::is_same_v< std::decay_t< TKey >, std::string_view > &&!std::is_same_v< std::decay_t< TKey >, std::string > &&!std::is_same_v< std::decay_t< TKey >, const char * > &&!std::is_integral_v< TKey > &&!std::is_floating_point_v< TKey > &&!std::is_pointer_v< TKey > &&!std::is_enum_v< TKey > &&!is_std_array< TKey >::value &&!is_std_optional< TKey >::value &&!is_std_pair< TKey >::value &&!is_std_span< TKey >::value &&!is_std_tuple< TKey >::value &&!is_std_variant< TKey >::value &&!is_std_vector< TKey >::value, HashType > operator() (const TKey &key) const noexcept
 Hashes custom types using std::hash fallback.

Detailed Description

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
struct nfx::hashing::Hasher< HashType, Seed >

General-purpose STL-compatible hash functor supporting multiple types.

Template Parameters
HashTypeHash value type - must be uint32_t or uint64_t (default: uint32_t)
SeedInitial seed value for hash calculation (default: FNV_OFFSET_BASIS_32 for 32-bit, FNV_OFFSET_BASIS_64 for 64-bit)

This functor provides a unified hashing interface compatible with STL containers like std::unordered_map and std::unordered_set. It supports transparent lookup via the is_transparent type alias, allowing heterogeneous key comparisons.

Supported Types:

  • Strings: std::string, std::string_view, const char* → CRC32-C with SSE4.2 hardware acceleration (requires -march=native/-msse4.2 or /arch:AVX)
  • Integers: All integral types → uses multiplicative hashing (Knuth/Wang)
  • Pointers: Generic pointers → hashes the address as uintptr_t
  • Floating-point: float, double → normalizes special values (+0/-0, NaN) and hashes bit representation
  • Enums: Converts to underlying integral type and hashes
  • Pairs: std::pair<T1, T2> → hashes both elements and combines them
  • Tuples: std::tuple<Ts...> → hashes all elements and combines them
  • Arrays: std::array<T, N> → hashes all elements and combines them
  • Vectors: std::vector<T> → hashes size and all elements
  • Spans: std::span<T> → hashes all elements in the contiguous view
  • Optionals: std::optional<T> → hashes nullopt state or contained value
  • Variants: std::variant<Ts...> → hashes index and active alternative
  • Custom types: Falls back to std::hash<T> for unknown types

Usage Example:

// 32-bit hash with default seed
std::unordered_map<std::string, int, nfx::hashing::Hasher<>> map32;
// 64-bit hash with default seed (automatically uses FNV_OFFSET_BASIS_64)
std::unordered_map<std::string, int, nfx::hashing::Hasher<uint64_t>> map64;
// Transparent lookup (find with string_view in string-keyed map)
std::unordered_map<std::string, int, nfx::hashing::Hasher<>, std::equal_to<>> transparentMap;
std::string_view key = "lookup";
auto it = transparentMap.find(key); // No temporary string allocation

Definition at line 168 of file Hasher.h.

Member Typedef Documentation

◆ is_transparent

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
using nfx::hashing::Hasher< HashType, Seed >::is_transparent = void

Enables transparent lookup in STL containers.

Allows heterogeneous lookup without temporary object creation.

Definition at line 178 of file Hasher.h.

Member Function Documentation

◆ operator()() [1/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const char * key) const
inlinenodiscardnoexcept

Hashes a C-style string using CRC32-C algorithm.

Parameters
keyNull-terminated string to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [2/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T, size_t N>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::array< T, N > & arr) const
inlinenodiscardnoexcept

Hashes a std::array by combining hashes of all elements.

Template Parameters
TElement type
NArray size
Parameters
arrArray to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [3/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::optional< T > & opt) const
inlinenodiscardnoexcept

Hashes a std::optional - nullopt has distinct hash from any value.

Template Parameters
TContained type
Parameters
optOptional to hash
Returns
Hash value (distinct for nullopt vs any contained value)
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [4/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T1, typename T2>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::pair< T1, T2 > & p) const
inlinenodiscardnoexcept

Hashes a std::pair by combining hashes of both elements.

Template Parameters
T1Type of first element
T2Type of second element
Parameters
pPair to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [5/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::string & key) const
inlinenodiscardnoexcept

Hashes a std::string using CRC32-C algorithm.

Parameters
keyString to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [6/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename... Ts>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::tuple< Ts... > & t) const
inlinenodiscardnoexcept

Hashes a std::tuple by combining hashes of all elements.

Template Parameters
TsTypes of tuple elements
Parameters
tTuple to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [7/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename... Ts>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::variant< Ts... > & var) const
inlinenodiscardnoexcept

Hashes a std::variant by combining index and active alternative's hash.

Template Parameters
TsAlternative types
Parameters
varVariant to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [8/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( const std::vector< T > & vec) const
inlinenodiscardnoexcept

Hashes a std::vector by combining size and hashes of all elements.

Template Parameters
TElement type
Parameters
vecVector to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [9/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename TKey>
std::enable_if_t<!std::is_same_v< std::decay_t< TKey >, std::string_view > &&!std::is_same_v< std::decay_t< TKey >, std::string > &&!std::is_same_v< std::decay_t< TKey >, const char * > &&!std::is_integral_v< TKey > &&!std::is_floating_point_v< TKey > &&!std::is_pointer_v< TKey > &&!std::is_enum_v< TKey > &&!is_std_array< TKey >::value &&!is_std_optional< TKey >::value &&!is_std_pair< TKey >::value &&!is_std_span< TKey >::value &&!is_std_tuple< TKey >::value &&!is_std_variant< TKey >::value &&!is_std_vector< TKey >::value, HashType > nfx::hashing::Hasher< HashType, Seed >::operator() ( const TKey & key) const
inlinenodiscardnoexcept

Hashes custom types using std::hash fallback.

Template Parameters
TKeyCustom type
Parameters
keyCustom object to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Warning
Hash quality depends on std::hash<TKey> implementation quality. Some STL implementations have poor std::hash (e.g., identity function for pointers). For critical custom types, consider specializing Hasher<> or providing a custom hash function.

◆ operator()() [10/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename TKey>
std::enable_if_t< std::is_enum_v< TKey >, HashType > nfx::hashing::Hasher< HashType, Seed >::operator() ( const TKey & key) const
inlinenodiscardnoexcept

Hashes an enum by its underlying integral value.

Template Parameters
TKeyEnum type
Parameters
keyEnum value to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [11/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename TKey>
std::enable_if_t< std::is_integral_v< TKey >, HashType > nfx::hashing::Hasher< HashType, Seed >::operator() ( const TKey & key) const
inlinenodiscardnoexcept

Hashes an integral type using multiplicative hashing.

Template Parameters
TKeyIntegral type
Parameters
keyInteger value to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [12/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T, std::size_t Extent = std::dynamic_extent>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( std::span< T, Extent > sp) const
inlinenodiscardnoexcept

Hashes a std::span by combining hashes of all elements in the view.

Template Parameters
TElement type
ExtentStatic extent (std::dynamic_extent for dynamic spans)
Parameters
spSpan to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [13/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
HashType nfx::hashing::Hasher< HashType, Seed >::operator() ( std::string_view key) const
inlinenodiscardnoexcept

Hashes a std::string_view using CRC32-C algorithm.

Parameters
keyString view to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [14/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T>
std::enable_if_t< std::is_pointer_v< T > &&!std::is_same_v< T, const char * > &&!std::is_same_v< T, char * >, HashType > nfx::hashing::Hasher< HashType, Seed >::operator() ( T ptr) const
inlinenodiscardnoexcept

Hashes a pointer by its address.

Template Parameters
TPointer type
Parameters
ptrPointer to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

◆ operator()() [15/15]

template<Hash32or64 HashType = uint32_t, HashType Seed = ( sizeof( HashType ) == 4 ? constants::FNV_OFFSET_BASIS_32 : constants::FNV_OFFSET_BASIS_64 )>
template<typename T>
std::enable_if_t< std::is_floating_point_v< T >, HashType > nfx::hashing::Hasher< HashType, Seed >::operator() ( T value) const
inlinenodiscardnoexcept

Hashes a floating-point value with normalization.

Template Parameters
TFloating-point type
Parameters
valueFloating-point value to hash
Returns
Hash value
Note
This function is marked [[nodiscard]] - the return value should not be ignored

The documentation for this struct was generated from the following file: