31#ifndef LIBSBX_UTILITY_HASH_HPP_
32#define LIBSBX_UTILITY_HASH_HPP_
46namespace sbx::utility {
51template<
typename Type>
52concept hashable =
requires(
const Type& instance) {
53 { std::hash<Type>{}(instance) } -> std::same_as<std::size_t>;
59inline constexpr auto hash_combine([[maybe_unused]] std::size_t& seed) ->
void { }
71template<hashable Type, hashable... Rest>
72inline constexpr auto hash_combine(std::size_t& seed,
const Type& value, Rest... rest) ->
void {
73 auto hasher = std::hash<Type>{};
74 seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
83template<
typename Type>
84concept 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>;
91template<std::
unsigned_
integral Type>
102 inline static constexpr auto basis = std::uint32_t{0x811c9dc5};
104 inline static constexpr auto prime = std::uint32_t{0x01000193};
115 inline static constexpr auto basis = std::uint64_t{0xcbf29ce484222325};
117 inline static constexpr auto prime = std::uint64_t{0x00000100000001B3};
127template<
character Char, std::
unsigned_
integral Hash = std::u
int64_t,
typename HashTraits = fnv1a_traits<Hash>>
129 using char_type = Char;
130 using size_type = std::size_t;
131 using hash_type = Hash;
132 using hash_traits = HashTraits;
142 inline constexpr auto operator()(std::basic_string_view<Char>
string)
const noexcept -> hash_type {
143 auto hash = hash_traits::basis;
146 hash ^=
static_cast<hash_type
>(
character);
147 hash *= hash_traits::prime;
154template<std::
unsigned_
integral Hash = std::u
int64_t>
157 using hash_type = Hash;
159 inline constexpr auto operator()(std::span<const std::uint8_t> buffer)
const noexcept -> hash_type {
161 auto hash = hash_type{5381};
163 for (
auto byte : buffer) {
164 hash = ((hash << 5) + hash) + static_cast<std::int32_t>(
byte);
170 template<
typename Type>
171 requires (std::is_trivially_copyable_v<Type>)
172 inline constexpr auto operator()(
const Type& value)
const noexcept -> hash_type {
173 return operator()({
reinterpret_cast<std::uint8_t*
>(std::addressof(value)),
sizeof(Type)});
A concept that represents a character type.
Definition: hash.hpp:84
A concept that represents a type that can be hashed.
Definition: hash.hpp:52
constexpr auto hash_combine(std::size_t &seed) -> void
Combines multiple hashes into a single hash.
Definition: hash.hpp:59
Functor that implements the fnv1a hash algorithm.
Definition: hash.hpp:128
constexpr auto operator()(std::basic_string_view< Char > string) const noexcept -> hash_type
Hashes the given string.
Definition: hash.hpp:142
Traits for the fnv1a hash function.
Definition: hash.hpp:92