1#ifndef DICE_HASH_DICEHASH_HPP
2#define DICE_HASH_DICEHASH_HPP
13#include "dice/hash/internal/DiceHashPolicies.hpp"
22#include <unordered_map>
23#include <unordered_set>
38 template<Policies::HashPolicy Policy,
typename T>
56 "The hash function is not defined for this type. You need to add an implementation yourself");
64 template<Policies::HashPolicy Policy>
75 template<
typename Container>
76 static std::size_t dice_hash_ordered_container(Container
const &container)
noexcept {
77 typename Policy::HashState hash_state(container.size());
78 std::size_t item_hash;
79 for (
const auto &item : container) {
81 hash_state.add(item_hash);
83 return hash_state.digest();
95 template<
typename Container>
96 static std::size_t dice_hash_unordered_container(Container
const &container)
noexcept {
98 for (
auto const &it : container) {
99 h = Policy::hash_invertible_combine({h,
dice_hash(it)});
112 template<
typename... TupleArgs, std::size_t... ids>
113 static std::size_t dice_hash_tuple(std::tuple<TupleArgs...>
const &tuple, std::index_sequence<ids...>
const &) {
114 return Policy::hash_combine({
dice_hash(std::get<ids>(tuple))...});
118 static constexpr bool is_fundamental = std::is_fundamental_v<T> || std::is_same_v<std::remove_cv_t<T>, std::byte>;
139 requires is_fundamental<std::decay_t<T>>
static std::size_t
dice_hash(T
const &fundamental)
noexcept {
140 return Policy::hash_fundamental(fundamental);
148 template<
typename CharT>
149 static std::size_t
dice_hash(std::basic_string<CharT>
const &str)
noexcept {
150 return Policy::hash_bytes(str.data(),
sizeof(CharT) * str.size());
158 template<
typename CharT>
159 static std::size_t
dice_hash(std::basic_string_view<CharT>
const &sv)
noexcept {
160 return Policy::hash_bytes(sv.data(),
sizeof(CharT) * sv.size());
171 return Policy::hash_fundamental(ptr);
181 static std::size_t
dice_hash(std::unique_ptr<T>
const &ptr)
noexcept {
192 static std::size_t
dice_hash(std::shared_ptr<T>
const &ptr)
noexcept {
203 template<
typename T, std::
size_t N>
204 static std::size_t
dice_hash(std::array<T, N>
const &arr)
noexcept {
205 if constexpr (is_fundamental<T>) {
206 return Policy::hash_bytes(arr.data(),
sizeof(T) * N);
208 return dice_hash_ordered_container(arr);
219 static std::size_t
dice_hash(std::vector<T>
const &vec)
noexcept {
220 if constexpr (is_fundamental<T>) {
221 static_assert(!std::is_same_v<std::decay_t<T>,
bool>,
222 "vector of booleans has a special implementation which results in errors!");
223 return Policy::hash_bytes(vec.data(),
sizeof(T) * vec.size());
225 return dice_hash_ordered_container(vec);
233 template<
typename T, std::
size_t Extent>
234 static std::size_t
dice_hash(std::span<T, Extent>
const &span)
noexcept {
235 if constexpr (is_fundamental<T>) {
236 return Policy::hash_bytes(span.data(), span.size_bytes());
238 return dice_hash_ordered_container(span);
248 template<
typename... TupleArgs>
249 static std::size_t
dice_hash(std::tuple<TupleArgs...>
const &tpl)
noexcept {
250 return dice_hash_tuple(tpl, std::make_index_sequence<
sizeof...(TupleArgs)>());
260 template<
typename T,
typename V>
261 static std::size_t
dice_hash(std::pair<T, V>
const &p)
noexcept {
270 static std::size_t
dice_hash(std::monostate
const &)
noexcept {
271 return Policy::ErrorValue;
283 template<
typename... VariantArgs>
284 static std::size_t
dice_hash(std::variant<VariantArgs...>
const &var)
noexcept {
286 return std::visit([]<
typename T>(T &&arg) {
return dice_hash(std::forward<T>(arg)); }, var);
287 }
catch (std::bad_variant_access
const &) {
288 return Policy::ErrorValue;
300 requires is_ordered_container_v<T>
static std::size_t
dice_hash(T
const &container)
noexcept {
301 return dice_hash_ordered_container(container);
312 requires is_unordered_container_v<T>
static std::size_t
dice_hash(T
const &container)
noexcept {
313 return dice_hash_unordered_container(container);
322 template<
typename T, Policies::HashPolicy Policy = Policies::Martinus>
329 using Policy::hash_combine;
336 using Policy::hash_invertible_combine;
352 [[nodiscard]]
static constexpr bool is_faulty(std::size_t to_check)
noexcept {
353 return to_check == Policy::ErrorValue;
357 template <
typename T>
358 using DiceHashMartinus = DiceHash<T, Policies::Martinus>;
359 template <
typename T>
360 using DiceHashxxh3 = DiceHash<T, Policies::xxh3>;
361 template <
typename T>
362 using DiceHashwyhash = DiceHash<T, Policies::wyhash>;
Home of custom type traits for the diceHash.
Class which contains all dice_hash functions.
Definition DiceHash.hpp:65
static std::size_t dice_hash(T *ptr) noexcept
Implementation for raw pointers.
Definition DiceHash.hpp:170
static std::size_t dice_hash(std::vector< T > const &vec) noexcept
Implementation for vectors.
Definition DiceHash.hpp:219
static std::size_t dice_hash(std::array< T, N > const &arr) noexcept
Implementation for std arrays.
Definition DiceHash.hpp:204
static std::size_t dice_hash(std::span< T, Extent > const &span) noexcept
Implementation for byte spans.
Definition DiceHash.hpp:234
static std::size_t dice_hash(T const &t) noexcept
Base case for dice_hash.
Definition DiceHash.hpp:129
static std::size_t dice_hash(std::shared_ptr< T > const &ptr) noexcept
implementation for shared pointers.
Definition DiceHash.hpp:192
static std::size_t dice_hash(std::monostate const &) noexcept
Overload for std::monostate.
Definition DiceHash.hpp:270
static std::size_t dice_hash(std::unique_ptr< T > const &ptr) noexcept
Implementation for unique pointers.
Definition DiceHash.hpp:181
static std::size_t dice_hash(std::variant< VariantArgs... > const &var) noexcept
Implementation for variant.
Definition DiceHash.hpp:284
static std::size_t dice_hash(std::pair< T, V > const &p) noexcept
Implementation for pairs.
Definition DiceHash.hpp:261
static std::size_t dice_hash(std::basic_string< CharT > const &str) noexcept
Implementation for string types.
Definition DiceHash.hpp:149
static std::size_t dice_hash(std::tuple< TupleArgs... > const &tpl) noexcept
Implementation for tuples.
Definition DiceHash.hpp:249
static std::size_t dice_hash(T const &container) noexcept
Implementation for ordered container.
Definition DiceHash.hpp:300
static std::size_t dice_hash(T const &container) noexcept
Implementation for unordered container.
Definition DiceHash.hpp:312
static std::size_t dice_hash(std::basic_string_view< CharT > const &sv) noexcept
Implementation for string view.
Definition DiceHash.hpp:159
static std::size_t dice_hash(T const &fundamental) noexcept
Implementation for fundamentals.
Definition DiceHash.hpp:139
Home of the DiceHash.
Definition Blake3.hpp:12
Wrapper class for the dice::hash::dice_hash function.
Definition DiceHash.hpp:323
static constexpr bool is_faulty(std::size_t to_check) noexcept
Function to check if a hash is equal to an error value.
Definition DiceHash.hpp:352
std::size_t operator()(T const &t) const noexcept
Overloaded operator to calculate a hash.
Definition DiceHash.hpp:343
Helper type.
Definition DiceHash.hpp:47
Helper struct for defining the hash for custom structs.
Definition DiceHash.hpp:39
static std::size_t dice_hash(T const &) noexcept
Default implementation of the dice_hash function.
Definition DiceHash.hpp:54