kblib 0.2.3
General utilities library for modern C++
hash.h File Reference

Provides generic facilities for hashing data, and aliases for standard unordered containers using the provided hash objects. More...

#include "iterators.h"
#include "tdecl.h"
#include "traits.h"
#include "variant.h"
#include <climits>
#include <cstdint>
#include <numeric>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <optional>
#include <string_view>
#include <variant>
Include dependency graph for hash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  kblib::fnv::fnv_prime< std::uint32_t >
 
struct  kblib::fnv::fnv_prime< std::uint64_t >
 
struct  kblib::fnv::fnv_prime< UInt >
 The prime to use for the FNVa hash algorithm, as a type trait. More...
 
struct  kblib::fnv::fnv_offset< std::uint32_t >
 
struct  kblib::fnv::fnv_offset< std::uint64_t >
 
struct  kblib::fnv::fnv_offset< UInt >
 The starting value for the FNVa hash algorithm, as a type trait. More...
 
struct  kblib::padding_bits< T >
 
struct  kblib::FNV_hash< Key, HashInt, typename >
 The primary template has to exist, but not be constructible, in order to be compatible with std::hash. More...
 
struct  kblib::is_hashable< Key, typename >
 
struct  kblib::is_hashable< Key, void_if_t< std::is_constructible< FNV_hash< Key > >::value > >
 
struct  kblib::FNV_hash< bool, HashInt, void >
 Hasher for bool. More...
 
struct  kblib::FNV_hash< char, HashInt, void >
 Hasher for char. More...
 
struct  kblib::FNV_hash< signed char, HashInt, void >
 Hasher for signed char. More...
 
struct  kblib::FNV_hash< unsigned char, HashInt, void >
 Hasher for unsigned char. More...
 
struct  kblib::FNV_hash< T, HashInt, void_if_t< std::is_empty< T >::value > >
 An empty type is treated as if it were a single null byte. More...
 
struct  kblib::is_trivially_hashable< T >
 
struct  kblib::FNV_hash< T, HashInt, void_if_t< std::is_integral< T >::value and is_trivially_hashable_v< T > > >
 Hasher for any integral type without padding type not explicitly mentioned above. More...
 
struct  kblib::FNV_hash< T, HashInt, void_if_t< std::is_pointer< T >::value > >
 Hasher for any pointer type. More...
 
struct  kblib::FNV_hash< T, HashInt, void_if_t<(std::is_base_of< std::forward_iterator_tag, typename std::iterator_traits< T >::iterator_category >::value and not std::is_pointer< T >::value and not is_trivially_hashable_v< T > and std::is_pointer< typename fakestd::invoke_result< decltype(&T::operator->), T >::type >::value)> >
 Hasher for any forward iterator type. More...
 
struct  kblib::FNV_hash< Container, HashInt, void_if_t<(is_contiguous_v< Container > and is_trivially_hashable_v< typename Container::value_type >)> >
 Container hasher, for contiguously-stored trivial elements. More...
 
struct  kblib::FNV_hash< T, HashInt, void_if_t< not is_contiguous_v< T > and not std::is_integral< T >::value and not std::is_pointer< T >::value and is_trivially_hashable_v< T > > >
 Hasher for any non-integral trivially copyable type that has no padding. More...
 
struct  kblib::FNV_hash< Container, HashInt, void_if_t< value_detected< Container >::value and is_hashable_v< value_detected_t< Container > > and not hash_detected< Container >::value and is_iterable< Container >::value and not(is_contiguous< Container >::value and is_trivially_hashable_v< typename Container::value_type >) and not is_iterator_v< Container > > >
 Container hasher, for non-trivial elements (or non-contiguous storage) More...
 
struct  kblib::FNV_hash< std::optional< T >, HashInt, void >
 
struct  kblib::FNV_hash< void, HashInt, void >
 Transparent hasher for any hashable type. More...
 

Namespaces

namespace  kblib
 The main namespace in which all entities from kblib are defined.
 
namespace  kblib::fnv
 
namespace  kblib::literals
 
namespace  kblib::asserts
 
namespace  detail_hash
 
namespace  kblib::detail_hash
 

Typedefs

template<typename Key >
using kblib::FNV32_hash = FNV_hash< Key, std::uint32_t >
 
template<typename Key , typename Value >
using kblib::hash_map = std::unordered_map< Key, Value, FNV_hash<>, std::equal_to<> >
 
template<typename Key , typename Value >
using kblib::hash_multimap = std::unordered_multimap< Key, Value, FNV_hash<>, std::equal_to<> >
 
template<typename T , typename HashInt >
using kblib::hash_set = std::unordered_set< T, FNV_hash<>, std::equal_to<> >
 
template<typename T , typename HashInt >
using kblib::hash_multiset = std::unordered_set< T, FNV_hash<>, std::equal_to<> >
 

Functions

template<typename Integral , typename CharT >
constexpr auto kblib::to_bytes_le (Integral ival, CharT(&dest)[sizeof(Integral)]) noexcept -> void
 
template<typename Integral , typename CharT >
constexpr auto kblib::to_bytes_be (Integral ival, CharT(&dest)[sizeof(Integral)]) noexcept -> void
 
template<typename Integral , typename CharT >
constexpr auto kblib::to_bytes (Integral val, CharT(&dest)[sizeof(Integral)]) noexcept -> void
 
template<typename HashInt , typename Span >
constexpr auto kblib::FNVa (Span &&s, HashInt hval=fnv::fnv_offset< HashInt >::value) noexcept -> HashInt
 A templatized generic FNVa hash function. More...
 
template<typename HashInt , typename CharT , std::size_t N>
constexpr auto kblib::FNVa_a (const CharT(&s)[N], HashInt hval=fnv::fnv_offset< HashInt >::value) noexcept -> HashInt
 A templatized FNVa hash function, for raw character arrays, such as string literals. More...
 
constexpr auto kblib::FNV32a (std::string_view s, std::uint32_t hval=fnv::fnv_offset< std::uint32_t >::value) noexcept -> std::uint32_t
 A standard FNV32a hash function, for string_views. More...
 
template<typename HashInt >
constexpr auto kblib::FNVa_s (const char *begin, std::size_t length, HashInt hval=fnv::fnv_offset< HashInt >::value) noexcept -> HashInt
 
template<std::size_t N>
constexpr auto kblib::FNV32a_a (const char(&s)[N], std::uint32_t hval=fnv::fnv_offset< std::uint32_t >::value) noexcept -> std::uint32_t
 A standard FNV32a hash function, for raw character arrays, such as string literals. More...
 
constexpr auto kblib::FNV32a_s (const char *begin, std::size_t length, uint32_t hval=fnv::fnv_offset< std::uint32_t >::value) noexcept -> std::uint32_t
 
constexpr auto kblib::literals::operator""_fnv32 (const char *str, std::size_t length) noexcept -> std::uint32_t
 A literal suffix that produces the FNV32a hash of a string literal. More...
 
constexpr auto kblib::literals::operator""_fnv64 (const char *str, std::size_t length) noexcept -> std::uint64_t
 A literal suffix that produces the FNV64a hash of a string literal. More...
 
constexpr auto kblib::literals::operator""_fnv32 (unsigned long long val) -> std::uint32_t
 
constexpr auto kblib::literals::operator""_fnv64 (unsigned long long val) -> std::uint64_t
 
template<typename Tuple , typename HashInt , std::size_t I>
constexpr auto kblib::detail_hash::hash_tuple_impl (const Tuple &tuple, HashInt offset, std::index_sequence< I >) noexcept -> HashInt
 Hash each element of a tuple. This overload is for tuples of a single type, or as the base case for the other overload. More...
 
template<typename Tuple , typename HashInt , std::size_t I, std::size_t I2, std::size_t... Is>
constexpr auto kblib::detail_hash::hash_tuple_impl (const Tuple &tuple, HashInt offset, std::index_sequence< I, I2, Is... >) noexcept -> HashInt
 Hash each element of a tuple. This overload is for tuples of at least 2 elements. More...
 
template<typename Tuple , std::size_t... Is>
constexpr auto kblib::detail_hash::all_hashable_impl (std::index_sequence< Is... >) -> bool
 
template<typename Tuple , typename std::enable_if<(std::tuple_size< Tuple >::value > 0u), int >::type = 0>
constexpr auto kblib::detail_hash::all_hashable () -> bool
 

Variables

template<typename T >
constexpr int kblib::padding_bits_v
 Get the number of padding bits in an integral type. More...
 
template<>
constexpr int kblib::padding_bits_v< void > = 0
 
template<typename Key >
KBLIB_CONSTANT_V kblib::is_hashable_v = is_hashable<Key>::value
 
template<typename T >
KBLIB_CONSTANT_V kblib::is_trivially_hashable_v
 
template<typename Container >
KBLIB_CONSTANT_V kblib::asserts::is_trivial_container
 

Detailed Description

Provides generic facilities for hashing data, and aliases for standard unordered containers using the provided hash objects.

Author
killerbee
Date
2019-2021

Definition in file hash.h.