Blender V4.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
64#include <memory>
65#include <string>
66#include <utility>
67
68#include "BLI_string_ref.hh"
69#include "BLI_utildefines.h"
70
71namespace blender {
72
83template<typename T> struct DefaultHash {
84 uint64_t operator()(const T &value) const
85 {
86 if constexpr (std::is_enum_v<T>) {
87 /* For enums use the value as hash directly. */
88 return uint64_t(value);
89 }
90 else {
91 /* Try to call the `hash()` function on the value. */
92 /* If this results in a compiler error, no hash function for the type has been found. */
93 return value.hash();
94 }
95 }
96
97 template<typename U> uint64_t operator()(const U &value) const
98 {
99 /* Try calling the static `T::hash_as(value)` function with the given value. The returned hash
100 * should be "compatible" with `T::hash()`. Usually that means that if `value` is converted to
101 * `T` its hash does not change. */
102 /* If this results in a compiler error, no hash function for the heterogeneous lookup has been
103 * found. */
104 return T::hash_as(value);
105 }
106};
107
111template<typename T> struct DefaultHash<const T> {
112 uint64_t operator()(const T &value) const
113 {
114 return DefaultHash<T>{}(value);
115 }
116};
117
118#define TRIVIAL_DEFAULT_INT_HASH(TYPE) \
119 template<> struct DefaultHash<TYPE> { \
120 uint64_t operator()(TYPE value) const \
121 { \
122 return uint64_t(value); \
123 } \
124 }
125
140
144template<> struct DefaultHash<float> {
145 uint64_t operator()(float value) const
146 {
147 /* Explicit `uint64_t` cast to suppress CPPCHECK warning. */
148 return uint64_t(*reinterpret_cast<uint32_t *>(&value));
149 }
150};
151
152template<> struct DefaultHash<double> {
153 uint64_t operator()(double value) const
154 {
155 return *reinterpret_cast<uint64_t *>(&value);
156 }
157};
158
159template<> struct DefaultHash<bool> {
160 uint64_t operator()(bool value) const
161 {
162 return uint64_t((value != false) * 1298191);
163 }
164};
165
167{
168 uint64_t hash = 5381;
169 for (char c : str) {
170 hash = hash * 33 + c;
171 }
172 return hash;
173}
174
175template<> struct DefaultHash<std::string> {
181 {
182 return hash_string(value);
183 }
184};
185
186template<> struct DefaultHash<StringRef> {
188 {
189 return hash_string(value);
190 }
191};
192
193template<> struct DefaultHash<StringRefNull> {
195 {
196 return hash_string(value);
197 }
198};
199
200template<> struct DefaultHash<std::string_view> {
202 {
203 return hash_string(value);
204 }
205};
206
210template<typename T> struct DefaultHash<T *> {
211 uint64_t operator()(const T *value) const
212 {
213 uintptr_t ptr = uintptr_t(value);
214 uint64_t hash = uint64_t(ptr >> 4);
215 return hash;
216 }
217};
218
219template<typename T> uint64_t get_default_hash(const T &v)
220{
222}
223
224template<typename T1, typename T2> uint64_t get_default_hash(const T1 &v1, const T2 &v2)
225{
226 const uint64_t h1 = get_default_hash(v1);
227 const uint64_t h2 = get_default_hash(v2);
228 return h1 ^ (h2 * 19349669);
229}
230
231template<typename T1, typename T2, typename T3>
232uint64_t get_default_hash(const T1 &v1, const T2 &v2, const T3 &v3)
233{
234 const uint64_t h1 = get_default_hash(v1);
235 const uint64_t h2 = get_default_hash(v2);
236 const uint64_t h3 = get_default_hash(v3);
237 return h1 ^ (h2 * 19349669) ^ (h3 * 83492791);
238}
239
240template<typename T1, typename T2, typename T3, typename T4>
241uint64_t get_default_hash(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
242{
243 const uint64_t h1 = get_default_hash(v1);
244 const uint64_t h2 = get_default_hash(v2);
245 const uint64_t h3 = get_default_hash(v3);
246 const uint64_t h4 = get_default_hash(v4);
247 return h1 ^ (h2 * 19349669) ^ (h3 * 83492791) ^ (h4 * 3632623);
248}
249
251template<typename T> struct PointerHashes {
252 template<typename U> uint64_t operator()(const U &value) const
253 {
254 return get_default_hash(&*value);
255 }
256};
257
258template<typename T> struct DefaultHash<std::unique_ptr<T>> : public PointerHashes<T> {};
259template<typename T> struct DefaultHash<std::shared_ptr<T>> : public PointerHashes<T> {};
260
261template<typename T> struct DefaultHash<std::reference_wrapper<T>> {
262 uint64_t operator()(const std::reference_wrapper<T> &value) const
263 {
264 return get_default_hash(value.get());
265 }
266};
267
268template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> {
269 uint64_t operator()(const std::pair<T1, T2> &value) const
270 {
271 return get_default_hash(value.first, value.second);
272 }
273};
274
275} // namespace blender
#define TRIVIAL_DEFAULT_INT_HASH(TYPE)
Definition BLI_hash.hh:118
typedef double(DMatrix)[4][4]
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int U
Definition btGjkEpa3.h:78
draw_view in_light_buf[] float
#define str(s)
#define T2
Definition md5.cpp:19
#define T3
Definition md5.cpp:20
#define T4
Definition md5.cpp:21
#define T1
Definition md5.cpp:18
uint64_t hash_string(StringRef str)
Definition BLI_hash.hh:166
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
#define hash
Definition noise.c:154
signed short int16_t
Definition stdint.h:76
unsigned short uint16_t
Definition stdint.h:79
_W64 unsigned int uintptr_t
Definition stdint.h:119
unsigned int uint32_t
Definition stdint.h:80
__int64 int64_t
Definition stdint.h:89
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90
signed char int8_t
Definition stdint.h:75
uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:194
uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:187
uint64_t operator()(const T *value) const
Definition BLI_hash.hh:211
uint64_t operator()(bool value) const
Definition BLI_hash.hh:160
uint64_t operator()(const T &value) const
Definition BLI_hash.hh:112
uint64_t operator()(double value) const
Definition BLI_hash.hh:153
uint64_t operator()(float value) const
Definition BLI_hash.hh:145
uint64_t operator()(const std::pair< T1, T2 > &value) const
Definition BLI_hash.hh:269
uint64_t operator()(const std::reference_wrapper< T > &value) const
Definition BLI_hash.hh:262
uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:180
uint64_t operator()(StringRef value) const
Definition BLI_hash.hh:201
uint64_t operator()(const T &value) const
Definition BLI_hash.hh:84
uint64_t operator()(const U &value) const
Definition BLI_hash.hh:97
uint64_t operator()(const U &value) const
Definition BLI_hash.hh:252
PointerRNA * ptr
Definition wm_files.cc:4126