Blender  V3.3
BLI_hash.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
62 #include <functional>
63 #include <memory>
64 #include <string>
65 #include <utility>
66 
67 #include "BLI_math_base.h"
68 #include "BLI_string_ref.hh"
69 #include "BLI_utildefines.h"
70 
71 namespace blender {
72 
83 template<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 
111 template<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 static_cast<uint64_t>(value); \
123  } \
124  }
125 
140 
144 template<> struct DefaultHash<float> {
145  uint64_t operator()(float value) const
146  {
147  return *reinterpret_cast<uint32_t *>(&value);
148  }
149 };
150 
151 template<> struct DefaultHash<double> {
152  uint64_t operator()(double value) const
153  {
154  return *reinterpret_cast<uint64_t *>(&value);
155  }
156 };
157 
158 template<> struct DefaultHash<bool> {
159  uint64_t operator()(bool value) const
160  {
161  return static_cast<uint64_t>((value != false) * 1298191);
162  }
163 };
164 
166 {
167  uint64_t hash = 5381;
168  for (char c : str) {
169  hash = hash * 33 + c;
170  }
171  return hash;
172 }
173 
174 template<> struct DefaultHash<std::string> {
180  {
181  return hash_string(value);
182  }
183 };
184 
185 template<> struct DefaultHash<StringRef> {
187  {
188  return hash_string(value);
189  }
190 };
191 
192 template<> struct DefaultHash<StringRefNull> {
194  {
195  return hash_string(value);
196  }
197 };
198 
199 template<> struct DefaultHash<std::string_view> {
201  {
202  return hash_string(value);
203  }
204 };
205 
209 template<typename T> struct DefaultHash<T *> {
210  uint64_t operator()(const T *value) const
211  {
212  uintptr_t ptr = reinterpret_cast<uintptr_t>(value);
213  uint64_t hash = static_cast<uint64_t>(ptr >> 4);
214  return hash;
215  }
216 };
217 
218 template<typename T> uint64_t get_default_hash(const T &v)
219 {
220  return DefaultHash<T>{}(v);
221 }
222 
223 template<typename T1, typename T2> uint64_t get_default_hash_2(const T1 &v1, const T2 &v2)
224 {
225  const uint64_t h1 = get_default_hash(v1);
226  const uint64_t h2 = get_default_hash(v2);
227  return h1 ^ (h2 * 19349669);
228 }
229 
230 template<typename T1, typename T2, typename T3>
231 uint64_t get_default_hash_3(const T1 &v1, const T2 &v2, const T3 &v3)
232 {
233  const uint64_t h1 = get_default_hash(v1);
234  const uint64_t h2 = get_default_hash(v2);
235  const uint64_t h3 = get_default_hash(v3);
236  return h1 ^ (h2 * 19349669) ^ (h3 * 83492791);
237 }
238 
239 template<typename T1, typename T2, typename T3, typename T4>
240 uint64_t get_default_hash_4(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
241 {
242  const uint64_t h1 = get_default_hash(v1);
243  const uint64_t h2 = get_default_hash(v2);
244  const uint64_t h3 = get_default_hash(v3);
245  const uint64_t h4 = get_default_hash(v4);
246  return h1 ^ (h2 * 19349669) ^ (h3 * 83492791) ^ (h4 * 3632623);
247 }
248 
249 template<typename T> struct DefaultHash<std::unique_ptr<T>> {
250  uint64_t operator()(const std::unique_ptr<T> &value) const
251  {
252  return get_default_hash(value.get());
253  }
254 };
255 
256 template<typename T> struct DefaultHash<std::shared_ptr<T>> {
257  uint64_t operator()(const std::shared_ptr<T> &value) const
258  {
259  return get_default_hash(value.get());
260  }
261 };
262 
263 template<typename T> struct DefaultHash<std::reference_wrapper<T>> {
264  uint64_t operator()(const std::reference_wrapper<T> &value) const
265  {
266  return get_default_hash(value.get());
267  }
268 };
269 
270 template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> {
271  uint64_t operator()(const std::pair<T1, T2> &value) const
272  {
273  return get_default_hash_2(value.first, value.second);
274  }
275 };
276 
277 } // namespace blender
typedef float(TangentPoint)[2]
typedef double(DMatrix)[4][4]
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int U
Definition: btGjkEpa3.h:78
#define str(s)
#define T
#define T2
Definition: md5.cpp:18
#define T3
Definition: md5.cpp:19
#define T4
Definition: md5.cpp:20
#define T1
Definition: md5.cpp:17
static unsigned c
Definition: RandGen.cpp:83
uint64_t get_default_hash_4(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Definition: BLI_hash.hh:240
TRIVIAL_DEFAULT_INT_HASH(int8_t)
uint64_t hash_string(StringRef str)
Definition: BLI_hash.hh:165
uint64_t get_default_hash(const T &v)
Definition: BLI_hash.hh:218
uint64_t get_default_hash_2(const T1 &v1, const T2 &v2)
Definition: BLI_hash.hh:223
uint64_t get_default_hash_3(const T1 &v1, const T2 &v2, const T3 &v3)
Definition: BLI_hash.hh:231
#define hash
Definition: noise.c:153
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:193
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:186
uint64_t operator()(const T *value) const
Definition: BLI_hash.hh:210
uint64_t operator()(bool value) const
Definition: BLI_hash.hh:159
uint64_t operator()(const T &value) const
Definition: BLI_hash.hh:112
uint64_t operator()(double value) const
Definition: BLI_hash.hh:152
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:271
uint64_t operator()(const std::reference_wrapper< T > &value) const
Definition: BLI_hash.hh:264
uint64_t operator()(const std::shared_ptr< T > &value) const
Definition: BLI_hash.hh:257
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:179
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:200
uint64_t operator()(const std::unique_ptr< T > &value) const
Definition: BLI_hash.hh:250
uint64_t operator()(const T &value) const
Definition: BLI_hash.hh:84
uint64_t operator()(const U &value) const
Definition: BLI_hash.hh:97
PointerRNA * ptr
Definition: wm_files.c:3480