30#ifndef LIBSBX_UTILITY_HASH_HPP_
31#define LIBSBX_UTILITY_HASH_HPP_
44namespace sbx::utility {
49template<
typename Type>
50concept hashable =
requires(
const Type& instance) {
51 { std::hash<Type>{}(instance) } -> std::same_as<std::size_t>;
57inline constexpr auto hash_combine([[maybe_unused]] std::size_t& seed) ->
void { }
69template<hashable Type, hashable... Rest>
70inline constexpr auto hash_combine(std::size_t& seed,
const Type& value, Rest... rest) ->
void {
71 auto hasher = std::hash<Type>{};
72 seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
81template<
typename Type>
82concept character = std::same_as<Type, char> || std::same_as<Type, wchar_t> || std::same_as<Type, char8_t> || std::same_as<Type, char16_t> || std::same_as<Type, char32_t>;
89template<std::
unsigned_
integral Type>
100 inline static constexpr auto basis = std::uint32_t{0x811c9dc5};
102 inline static constexpr auto prime = std::uint32_t{0x01000193};
113 inline static constexpr auto basis = std::uint64_t{0xcbf29ce484222325};
115 inline static constexpr auto prime = std::uint64_t{0x00000100000001B3};
125template<
character Char, std::
unsigned_
integral Hash = std::u
int64_t,
typename HashTraits = fnv1a_traits<Hash>>
127 using char_type = Char;
128 using size_type = std::size_t;
129 using hash_type = Hash;
130 using hash_traits = HashTraits;
140 inline constexpr auto operator()(std::basic_string_view<Char>
string)
const noexcept -> hash_type {
141 auto hash = hash_traits::basis;
144 hash ^=
static_cast<hash_type
>(
character);
145 hash *= hash_traits::prime;
A concept that represents a character type.
Definition: hash.hpp:82
A concept that represents a type that can be hashed.
Definition: hash.hpp:50
constexpr auto hash_combine(std::size_t &seed) -> void
Combines multiple hashes into a single hash.
Definition: hash.hpp:57
Functor that implements the fnv1a hash algorithm.
Definition: hash.hpp:126
constexpr auto operator()(std::basic_string_view< Char > string) const noexcept -> hash_type
Hashes the given string.
Definition: hash.hpp:140
Traits for the fnv1a hash function.
Definition: hash.hpp:90