1#ifndef DICE_HASH_DICEHASH_HPP
2#define DICE_HASH_DICEHASH_HPP
13#include "dice/hash/internal/DiceHashPolicies.hpp"
21#include <unordered_map>
22#include <unordered_set>
37 template<Policies::HashPolicy Policy,
typename T>
55 "The hash function is not defined for this type. You need to add an implementation yourself");
63 template<Policies::HashPolicy Policy>
74 template<
typename Container>
75 static std::size_t dice_hash_ordered_container(Container
const &container)
noexcept {
76 typename Policy::HashState hash_state(container.size());
77 std::size_t item_hash;
78 for (
const auto &item : container) {
80 hash_state.add(item_hash);
82 return hash_state.digest();
94 template<
typename Container>
95 static std::size_t dice_hash_unordered_container(Container
const &container)
noexcept {
97 for (
auto const &it : container) {
98 h = Policy::hash_invertible_combine({h,
dice_hash(it)});
111 template<
typename... TupleArgs, std::size_t... ids>
112 static std::size_t dice_hash_tuple(std::tuple<TupleArgs...>
const &tuple, std::index_sequence<ids...>
const &) {
113 return Policy::hash_combine({
dice_hash(std::get<ids>(tuple))...});
135 requires std::is_fundamental_v<std::decay_t<T>>
static std::size_t
dice_hash(T
const &fundamental)
noexcept {
136 return Policy::hash_fundamental(fundamental);
144 template<
typename CharT>
145 static std::size_t
dice_hash(std::basic_string<CharT>
const &str)
noexcept {
146 return Policy::hash_bytes(str.data(),
sizeof(CharT) * str.size());
154 template<
typename CharT>
155 static std::size_t
dice_hash(std::basic_string_view<CharT>
const &sv)
noexcept {
156 return Policy::hash_bytes(sv.data(),
sizeof(CharT) * sv.size());
167 return Policy::hash_fundamental(ptr);
177 static std::size_t
dice_hash(std::unique_ptr<T>
const &ptr)
noexcept {
188 static std::size_t
dice_hash(std::shared_ptr<T>
const &ptr)
noexcept {
199 template<
typename T, std::
size_t N>
200 static std::size_t
dice_hash(std::array<T, N>
const &arr)
noexcept {
201 if constexpr (std::is_fundamental_v<T>) {
202 return Policy::hash_bytes(arr.data(),
sizeof(T) * N);
204 return dice_hash_ordered_container(arr);
215 static std::size_t
dice_hash(std::vector<T>
const &vec)
noexcept {
216 if constexpr (std::is_fundamental_v<T>) {
217 static_assert(!std::is_same_v<std::decay_t<T>,
bool>,
218 "vector of booleans has a special implementation which results into errors!");
219 return Policy::hash_bytes(vec.data(),
sizeof(T) * vec.size());
221 return dice_hash_ordered_container(vec);
231 template<
typename... TupleArgs>
232 static std::size_t
dice_hash(std::tuple<TupleArgs...>
const &tpl)
noexcept {
233 return dice_hash_tuple(tpl, std::make_index_sequence<
sizeof...(TupleArgs)>());
243 template<
typename T,
typename V>
244 static std::size_t
dice_hash(std::pair<T, V>
const &p)
noexcept {
253 static std::size_t
dice_hash(std::monostate
const &)
noexcept {
254 return Policy::ErrorValue;
266 template<
typename... VariantArgs>
267 static std::size_t
dice_hash(std::variant<VariantArgs...>
const &var)
noexcept {
269 return std::visit([]<
typename T>(T &&arg) {
return dice_hash(std::forward<T>(arg)); }, var);
270 }
catch (std::bad_variant_access
const &) {
271 return Policy::ErrorValue;
284 return dice_hash_ordered_container(
container);
296 return dice_hash_unordered_container(
container);
305 template<
typename T, Policies::HashPolicy Policy = Policies::Martinus>
312 using Policy::hash_combine;
319 using Policy::hash_invertible_combine;
336 return to_check == Policy::ErrorValue;
340 template <
typename T>
342 template <
typename T>
344 template <
typename T>
Home of custom type traits for the diceHash.
Class which contains all dice_hash functions.
Definition DiceHash.hpp:64
static std::size_t dice_hash(T *ptr) noexcept
Implementation for raw pointers.
Definition DiceHash.hpp:166
static std::size_t dice_hash(std::vector< T > const &vec) noexcept
Implementation for vectors.
Definition DiceHash.hpp:215
static std::size_t dice_hash(std::array< T, N > const &arr) noexcept
Implementation for std arrays.
Definition DiceHash.hpp:200
static std::size_t dice_hash(T const &t) noexcept
Base case for dice_hash.
Definition DiceHash.hpp:125
static std::size_t dice_hash(std::shared_ptr< T > const &ptr) noexcept
implementation for shared pointers.
Definition DiceHash.hpp:188
static std::size_t dice_hash(T const &fundamental) noexcept
Implementation for fundamentals.
Definition DiceHash.hpp:135
static std::size_t dice_hash(std::monostate const &) noexcept
Overload for std::monostate.
Definition DiceHash.hpp:253
static std::size_t dice_hash(std::unique_ptr< T > const &ptr) noexcept
Implementation for unique pointers.
Definition DiceHash.hpp:177
static std::size_t dice_hash(std::variant< VariantArgs... > const &var) noexcept
Implementation for variant.
Definition DiceHash.hpp:267
static std::size_t dice_hash(std::pair< T, V > const &p) noexcept
Implementation for pairs.
Definition DiceHash.hpp:244
static std::size_t dice_hash(std::basic_string< CharT > const &str) noexcept
Implementation for string types.
Definition DiceHash.hpp:145
static std::size_t dice_hash(std::tuple< TupleArgs... > const &tpl) noexcept
Implementation for tuples.
Definition DiceHash.hpp:232
static std::size_t dice_hash(T const &container) noexcept
Implementation for ordered container.
Definition DiceHash.hpp:283
static std::size_t dice_hash(T const &container) noexcept
Implementation for unordered container.
Definition DiceHash.hpp:295
static std::size_t dice_hash(std::basic_string_view< CharT > const &sv) noexcept
Implementation for string view.
Definition DiceHash.hpp:155
Home of the DiceHash.
Definition DiceHash.hpp:30
constexpr bool is_ordered_container_v
Helper definition.
Definition Container_trait.hpp:87
Wrapper class for the dice::hash::dice_hash function.
Definition DiceHash.hpp:306
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:335
std::size_t operator()(T const &t) const noexcept
Overloaded operator to calculate a hash.
Definition DiceHash.hpp:326
Helper type.
Definition DiceHash.hpp:46
Helper struct for defining the hash for custom structs.
Definition DiceHash.hpp:38
static std::size_t dice_hash(T const &) noexcept
Default implementation of the dice_hash function.
Definition DiceHash.hpp:53