30#ifndef LIBSBX_UTILITY_HASH_HPP_
31#define LIBSBX_UTILITY_HASH_HPP_
45namespace sbx::utility {
50template<
typename Type>
51concept hashable =
requires(
const Type& instance) {
52 { std::hash<Type>{}(instance) } -> std::same_as<std::size_t>;
58inline constexpr auto hash_combine([[maybe_unused]] std::size_t& seed) ->
void { }
70template<hashable Type, hashable... Rest>
71inline constexpr auto hash_combine(std::size_t& seed,
const Type& value, Rest... rest) ->
void {
72 auto hasher = std::hash<Type>{};
73 seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
82template<
typename Type>
83concept 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>;
90template<std::
unsigned_
integral Type>
101 inline static constexpr auto basis = std::uint32_t{0x811c9dc5};
103 inline static constexpr auto prime = std::uint32_t{0x01000193};
114 inline static constexpr auto basis = std::uint64_t{0xcbf29ce484222325};
116 inline static constexpr auto prime = std::uint64_t{0x00000100000001B3};
126template<
character Char, std::
unsigned_
integral Hash = std::u
int64_t,
typename HashTraits = fnv1a_traits<Hash>>
128 using char_type = Char;
129 using size_type = std::size_t;
130 using hash_type = Hash;
131 using hash_traits = HashTraits;
141 inline constexpr auto operator()(std::basic_string_view<Char>
string)
const noexcept -> hash_type {
142 auto hash = hash_traits::basis;
145 hash ^=
static_cast<hash_type
>(
character);
146 hash *= hash_traits::prime;
153template<std::
unsigned_
integral Hash = std::u
int64_t>
156 using hash_type = Hash;
158 inline constexpr auto operator()(std::span<const std::uint8_t> buffer)
const noexcept -> hash_type {
160 auto hash = hash_type{5381};
162 for (
auto byte : buffer) {
163 hash = ((hash << 5) + hash) + static_cast<std::int32_t>(
byte);
169 template<
typename Type>
170 requires (std::is_trivially_copyable_v<Type>)
171 inline constexpr auto operator()(
const Type& value)
const noexcept -> hash_type {
172 return operator()({
reinterpret_cast<std::uint8_t*
>(std::addressof(value)),
sizeof(Type)});
A concept that represents a character type.
Definition: hash.hpp:83
A concept that represents a type that can be hashed.
Definition: hash.hpp:51
constexpr auto hash_combine(std::size_t &seed) -> void
Combines multiple hashes into a single hash.
Definition: hash.hpp:58
Functor that implements the fnv1a hash algorithm.
Definition: hash.hpp:127
constexpr auto operator()(std::basic_string_view< Char > string) const noexcept -> hash_type
Hashes the given string.
Definition: hash.hpp:141
Traits for the fnv1a hash function.
Definition: hash.hpp:91