dice-hash
Hash function for stl types and container
Loading...
Searching...
No Matches
DiceHash.hpp
Go to the documentation of this file.
1#ifndef DICE_HASH_DICEHASH_HPP
2#define DICE_HASH_DICEHASH_HPP
3
13#include "dice/hash/internal/DiceHashPolicies.hpp"
14#include <cstring>
15#include <map>
16#include <memory>
17#include <set>
18#include <string>
19#include <string_view>
20#include <tuple>
21#include <unordered_map>
22#include <unordered_set>
23#include <utility>
24#include <variant>
25#include <vector>
26
30namespace dice::hash {
31
37 template<Policies::HashPolicy Policy, typename T>
45 template<typename>
46 struct AlwaysFalse : std::false_type {};
47
53 static std::size_t dice_hash(T const &) noexcept {
54 static_assert(AlwaysFalse<T>::value,
55 "The hash function is not defined for this type. You need to add an implementation yourself");
56 return 0;
57 }
58 };
59
63 template<Policies::HashPolicy Policy>
65 private:
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) {
79 item_hash = dice_hash(item);
80 hash_state.add(item_hash);
81 }
82 return hash_state.digest();
83 }
84
94 template<typename Container>
95 static std::size_t dice_hash_unordered_container(Container const &container) noexcept {
96 std::size_t h{};
97 for (auto const &it : container) {
98 h = Policy::hash_invertible_combine({h, dice_hash(it)});
99 }
100 return h;
101 }
102
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))...});
114 }
115
116 public:
124 template<typename T>
125 static std::size_t dice_hash(T const &t) noexcept {
127 }
128
134 template<typename T>
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);
137 }
138
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());
147 }
148
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());
157 }
158
165 template<typename T>
166 static std::size_t dice_hash(T *ptr) noexcept {
167 return Policy::hash_fundamental(ptr);
168 }
169
176 template<typename T>
177 static std::size_t dice_hash(std::unique_ptr<T> const &ptr) noexcept {
178 return dice_hash(ptr.get());
179 }
180
187 template<typename T>
188 static std::size_t dice_hash(std::shared_ptr<T> const &ptr) noexcept {
189 return dice_hash(ptr.get());
190 }
191
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);
203 } else {
204 return dice_hash_ordered_container(arr);
205 }
206 }
207
214 template<typename T>
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());
220 } else {
221 return dice_hash_ordered_container(vec);
222 }
223 }
224
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)>());
234 }
235
243 template<typename T, typename V>
244 static std::size_t dice_hash(std::pair<T, V> const &p) noexcept {
245 return Policy::hash_combine({dice_hash(p.first), dice_hash(p.second)});
246 }
247
253 static std::size_t dice_hash(std::monostate const &) noexcept {
254 return Policy::ErrorValue;
255 }
256
266 template<typename... VariantArgs>
267 static std::size_t dice_hash(std::variant<VariantArgs...> const &var) noexcept {
268 try {
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;
272 }
273 }
274
282 template<typename T>
283 requires is_ordered_container_v<T> static std::size_t dice_hash(T const &container) noexcept {
284 return dice_hash_ordered_container(container);
285 }
286
294 template<typename T>
295 requires is_unordered_container_v<T> static std::size_t dice_hash(T const &container) noexcept {
296 return dice_hash_unordered_container(container);
297 }
298 };
299
305 template<typename T, Policies::HashPolicy Policy = Policies::Martinus>
306 struct DiceHash : private Policy {
312 using Policy::hash_combine;
313
319 using Policy::hash_invertible_combine;
320
326 std::size_t operator()(T const &t) const noexcept {
328 }
329
335 [[nodiscard]] static constexpr bool is_faulty(std::size_t to_check) noexcept {
336 return to_check == Policy::ErrorValue;
337 }
338 };
339
340 template <typename T>
341 using DiceHashMartinus = DiceHash<T, Policies::Martinus>;
342 template <typename T>
343 using DiceHashxxh3 = DiceHash<T, Policies::xxh3>;
344 template <typename T>
345 using DiceHashwyhash = DiceHash<T, Policies::wyhash>;
346}// namespace dice::hash
347#endif//DICE_HASH_DICEHASH_HPP
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