1 #ifndef SIPLASPLAS_UTILITY_HASH_HPP 2 #define SIPLASPLAS_UTILITY_HASH_HPP 5 #include "function_traits.hpp" 7 #include <unordered_map> 8 #include <unordered_set> 46 constexpr std::size_t
hash(
const T& value);
65 template<
typename T,
typename U,
typename... Args>
66 constexpr std::size_t
hash(
const T& first,
const U& second,
const Args&... tail);
80 std::size_t
raw_hash(
const T& value);
85 template<
typename T, cpp::FunctionKind Kind = cpp::function_kind<T>()>
89 template<
typename U,
typename =
void>
90 class HashDispatchForValueTypes
93 static constexpr std::size_t apply(
const U& value)
95 return std::hash<T>{}(value);
100 class HashDispatchForValueTypes<U, typename
std::enable_if<std::is_enum<U>::value>::type>
103 static constexpr std::size_t apply(
const U& value)
105 return std::hash<T>{}(
static_cast<typename std::underlying_type<U>::type
>(value));
109 static constexpr std::size_t apply(
const T& value)
111 return HashDispatchForValueTypes<T>::apply(value);
116 class HashDispatch<T,
cpp::FunctionKind::FREE_FUNCTION>
119 static constexpr std::size_t apply(
const T& value)
126 class HashDispatch<T,
cpp::FunctionKind::MEMBER_FUNCTION>
129 static constexpr std::size_t apply(
const T& value)
136 class HashDispatch<T,
cpp::FunctionKind::CONST_MEMBER_FUNCTION>
139 static constexpr std::size_t apply(
const T& value)
146 class HashDispatch<T,
cpp::FunctionKind::FUNCTOR>
149 static constexpr std::size_t apply(
const T& value)
151 return std::is_empty<T>::value ?
154 ::
cpp::
hash(&value, &T::operator())
176 return seed ^ (
hash(value) + 0x9e3779b9 + (seed<<6) + (seed>>2));
180 constexpr std::size_t
hash(
const T& value)
182 return HashDispatch<T>::apply(value);
185 template<
typename T,
typename U,
typename... Args>
186 constexpr std::size_t
hash(
const T& first,
const U& second,
const Args&... tail)
190 hash(second, tail...)
194 template<
typename T,
typename U>
195 constexpr std::size_t
hash(
const std::pair<T, U>& pair)
197 return hash(pair.first, pair.second);
200 template<
typename... Ts>
201 constexpr std::size_t
hash(
const std::tuple<Ts...>& tuple)
203 return cpp::tuple_call(
205 [](
const Ts&... args)
207 return hash(args...);
226 constexpr std::size_t operator()(
const T& value)
const 239 template<
typename Key,
typename Value>
240 using HashTable = std::unordered_map<Key, Value, Hash<Key>>;
248 template<
typename Key>
249 using HashSet = std::unordered_set<Key, Hash<Key>>;
254 std::aligned_storage_t<sizeof(T), alignof(T)> rawStorage;
255 std::memcpy(&rawStorage, &value,
sizeof(T));
256 std::size_t
hash = 0;
258 for(std::size_t i = 0; i <
sizeof(T); ++i)
260 hash =
hash_combine(hash, reinterpret_cast<const char*>(&rawStorage)[i]);
280 constexpr std::size_t operator()(
const T& value)
const 288 #endif // SIPLASPLAS_UTILITY_HASH_HPP constexpr std::size_t hash_combine(std::size_t seed, const T &value)
Implements a hash combination function of a given hash value and a value of type T.
Definition: hash.hpp:174
std::unordered_set< Key, Hash< Key >> HashSet
std::set alias using cpp::Hash as hash.
Definition: hash.hpp:249
A functor that implements a bytewise hash function for values of type T.
Definition: hash.hpp:277
Definition: canary_allocator.hpp:7
Definition: variant.hpp:500
A functor that implements a hash function for values of type T.
Definition: hash.hpp:223
std::unordered_map< Key, Value, Hash< Key >> HashTable
std::unordered_map alias using cpp::Hash as hash.
Definition: hash.hpp:240
std::size_t raw_hash(const T &value)
Returns a bytewise hash of a given value.
Definition: hash.hpp:252
constexpr std::size_t hash(const T &first, const U &second, const Args &...tail)
Returns the comination of hashes of a set of values.
Definition: hash.hpp:186