Blender V5.3
BLI_hash.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
63
64#include <bit>
65#include <memory>
66#include <string>
67#include <utility>
68
69#include "BLI_hash_fwd.hh"
70#include "BLI_string_ref.hh"
71
72namespace blender {
73
85template<typename T> struct DefaultHash {
86 constexpr uint64_t operator()(const T &value) const
87 {
88 if constexpr (std::is_enum_v<T>) {
89 /* For enums use the value as hash directly. */
90 return uint64_t(value);
91 }
92 else if constexpr (requires {
93 { value.hash() } -> std::convertible_to<uint64_t>;
94 })
95 {
96 /* When the type has a hash method directly. */
97 return value.hash();
98 }
99 else if constexpr (requires(const T &v) {
100 { hash_value(v) } -> std::convertible_to<uint64_t>;
101 })
102 {
103 /* When the type has an overload of the #hash_value which is standard popularized by boost.
104 * https://www.boost.org/doc/libs/latest/libs/container_hash/doc/html/hash.html
105 * This works e.g. for the pxr::SdfPath type. */
106 return hash_value(value);
107 }
108 else {
109 /* No hash function for the type has been found. */
111 }
112 }
113
114 template<typename U> constexpr uint64_t operator()(const U &value) const
115 {
116 /* Try calling the static `T::hash_as(value)` function with the given value. The returned hash
117 * should be "compatible" with `T::hash()`. Usually that means that if `value` is converted to
118 * `T` its hash does not change. */
119 /* If this results in a compiler error, no hash function for the heterogeneous lookup has been
120 * found. */
121 return T::hash_as(value);
122 }
123};
124
128template<typename T> struct DefaultHash<const T> {
129 constexpr uint64_t operator()(const T &value) const
130 {
131 return DefaultHash<T>{}(value);
132 }
133};
134
141template<typename T>
142 requires(std::is_integral_v<T> && !std::is_same_v<T, bool>)
144 constexpr uint64_t operator()(const T &value) const
145 {
146 return uint64_t(value);
147 }
148};
149
153template<> struct DefaultHash<float> {
154 constexpr uint64_t operator()(const float value) const
155 {
156 /* Make sure +0 and -0 hash to the same value. */
157 if (value == 0.0f) {
158 return 0;
159 }
160 /* Explicit `uint64_t` cast to suppress CPPCHECK warning. */
161 return uint64_t(std::bit_cast<uint32_t>(value));
162 }
163};
164
165template<> struct DefaultHash<double> {
166 constexpr uint64_t operator()(const double value) const
167 {
168 /* Make sure +0 and -0 hash to the same value. */
169 if (value == 0.0) {
170 return 0;
171 }
172 return std::bit_cast<uint64_t>(value);
173 }
174};
175
176template<> struct DefaultHash<bool> {
177 constexpr uint64_t operator()(bool value) const
178 {
179 return uint64_t((value != false) * 1298191);
180 }
181};
182
184{
185 uint64_t hash = 5381;
186 for (char c : str) {
187 hash = hash * 33 + c;
188 }
189 return hash;
190}
191
192template<> struct DefaultHash<std::string> {
197 constexpr uint64_t operator()(StringRef value) const
198 {
199 return hash_string(value);
200 }
201};
202
203template<> struct DefaultHash<StringRef> {
204 constexpr uint64_t operator()(StringRef value) const
205 {
206 return hash_string(value);
207 }
208};
209
210template<> struct DefaultHash<StringRefNull> {
211 constexpr uint64_t operator()(StringRef value) const
212 {
213 return hash_string(value);
214 }
215};
216
217template<> struct DefaultHash<std::string_view> {
218 constexpr uint64_t operator()(StringRef value) const
219 {
220 return hash_string(value);
221 }
222};
223
227template<typename T> struct DefaultHash<T *> {
228 constexpr uint64_t operator()(const T *value) const
229 {
230 uintptr_t ptr = uintptr_t(value);
231 uint64_t hash = uint64_t(ptr >> 4);
232 return hash;
233 }
234};
235
236namespace blenlib_detail {
237static constexpr std::array<uint64_t, 5> default_hash_factors = {
238 19349669, 83492791, 3632623, 8789800933, 7235126189};
239
240template<size_t... I, typename... Args>
241constexpr uint64_t get_default_hash_array(std::index_sequence<I...> /*indices*/,
242 const Args &...args)
243{
244 static_assert(sizeof...(Args) == sizeof...(I));
245 static_assert(sizeof...(Args) <= default_hash_factors.size());
246 return (0 ^ ... ^ (default_hash_factors[I] * DefaultHash<std::decay_t<Args>>{}(args)));
247}
248
249} // namespace blenlib_detail
250
251template<typename T, typename... Args>
252constexpr uint64_t get_default_hash(const T &v, const Args &...args)
253{
254 return DefaultHash<std::decay_t<T>>{}(v) ^
255 blenlib_detail::get_default_hash_array(std::make_index_sequence<sizeof...(Args)>(),
256 args...);
257}
258
260template<typename T> struct PointerHashes {
261 template<typename U> constexpr uint64_t operator()(const U &value) const
262 {
263 return get_default_hash(&*value);
264 }
265};
266
267template<typename T> struct DefaultHash<std::unique_ptr<T>> : public PointerHashes<T> {};
268template<typename T> struct DefaultHash<std::shared_ptr<T>> : public PointerHashes<T> {};
269
270template<typename T> struct DefaultHash<std::reference_wrapper<T>> {
271 constexpr uint64_t operator()(const std::reference_wrapper<T> &value) const
272 {
273 return get_default_hash(value.get());
274 }
275};
276
277template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> {
278 constexpr uint64_t operator()(const std::pair<T1, T2> &value) const
279 {
280 return get_default_hash(value.first, value.second);
281 }
282};
283
288template<typename Ret, typename... Args> struct DefaultHash<Ret (*)(Args...)> {
289 constexpr uint64_t operator()(Ret (*fn)(Args...)) const
290 {
291 return get_default_hash(reinterpret_cast<const void *>(fn));
292 }
293};
294
295} // namespace blender
#define BLI_assert_unreachable_static_t(T)
#define str(s)
#define T
constexpr uint64_t get_default_hash_array(std::index_sequence< I... >, const Args &...args)
Definition BLI_hash.hh:241
static constexpr std::array< uint64_t, 5 > default_hash_factors
Definition BLI_hash.hh:237
BMesh Iterators.
UserDef U
Definition blender.cc:58
unsigned __int64 uint64_t
Definition BKE_text.h:91
ATTR_WARN_UNUSED_RESULT const BMVert * v
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
constexpr uint64_t hash_string(StringRef str)
Definition BLI_hash.hh:183
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
#define I
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
constexpr uint64_t operator()(Ret(*fn)(Args...)) const
Definition BLI_hash.hh:289
constexpr uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:211
constexpr uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:204
constexpr uint64_t operator()(const T &value) const
Definition BLI_hash.hh:144
constexpr uint64_t operator()(const T *value) const
Definition BLI_hash.hh:228
constexpr uint64_t operator()(bool value) const
Definition BLI_hash.hh:177
constexpr uint64_t operator()(const T &value) const
Definition BLI_hash.hh:129
constexpr uint64_t operator()(const double value) const
Definition BLI_hash.hh:166
constexpr uint64_t operator()(const float value) const
Definition BLI_hash.hh:154
constexpr uint64_t operator()(const std::pair< T1, T2 > &value) const
Definition BLI_hash.hh:278
constexpr uint64_t operator()(const std::reference_wrapper< T > &value) const
Definition BLI_hash.hh:271
constexpr uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:197
constexpr uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:218
constexpr uint64_t operator()(const T &value) const
Definition BLI_hash.hh:86
constexpr uint64_t operator()(const U &value) const
Definition BLI_hash.hh:114
constexpr uint64_t operator()(const U &value) const
Definition BLI_hash.hh:261