Blender  V2.93
BLI_hash.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
76 #include <functional>
77 #include <memory>
78 #include <string>
79 #include <utility>
80 
81 #include "BLI_math_base.h"
82 #include "BLI_string_ref.hh"
83 #include "BLI_utildefines.h"
84 
85 namespace blender {
86 
94 template<typename T> struct DefaultHash {
95  uint64_t operator()(const T &value) const
96  {
97  if constexpr (std::is_enum_v<T>) {
98  return (uint64_t)value;
99  }
100  else {
101  return value.hash();
102  }
103  }
104 };
105 
109 template<typename T> struct DefaultHash<const T> {
110  uint64_t operator()(const T &value) const
111  {
112  return DefaultHash<T>{}(value);
113  }
114 };
115 
116 #define TRIVIAL_DEFAULT_INT_HASH(TYPE) \
117  template<> struct DefaultHash<TYPE> { \
118  uint64_t operator()(TYPE value) const \
119  { \
120  return static_cast<uint64_t>(value); \
121  } \
122  }
123 
138 
142 template<> struct DefaultHash<float> {
143  uint64_t operator()(float value) const
144  {
145  return *reinterpret_cast<uint32_t *>(&value);
146  }
147 };
148 
149 template<> struct DefaultHash<bool> {
150  uint64_t operator()(bool value) const
151  {
152  return static_cast<uint64_t>((value != false) * 1298191);
153  }
154 };
155 
157 {
158  uint64_t hash = 5381;
159  for (char c : str) {
160  hash = hash * 33 + c;
161  }
162  return hash;
163 }
164 
165 template<> struct DefaultHash<std::string> {
171  {
172  return hash_string(value);
173  }
174 };
175 
176 template<> struct DefaultHash<StringRef> {
178  {
179  return hash_string(value);
180  }
181 };
182 
183 template<> struct DefaultHash<StringRefNull> {
185  {
186  return hash_string(value);
187  }
188 };
189 
190 template<> struct DefaultHash<std::string_view> {
192  {
193  return hash_string(value);
194  }
195 };
196 
200 template<typename T> struct DefaultHash<T *> {
201  uint64_t operator()(const T *value) const
202  {
203  uintptr_t ptr = reinterpret_cast<uintptr_t>(value);
204  uint64_t hash = static_cast<uint64_t>(ptr >> 4);
205  return hash;
206  }
207 };
208 
209 template<typename T> uint64_t get_default_hash(const T &v)
210 {
211  return DefaultHash<T>{}(v);
212 }
213 
214 template<typename T1, typename T2> uint64_t get_default_hash_2(const T1 &v1, const T2 &v2)
215 {
216  const uint64_t h1 = get_default_hash(v1);
217  const uint64_t h2 = get_default_hash(v2);
218  return h1 ^ (h2 * 19349669);
219 }
220 
221 template<typename T1, typename T2, typename T3>
222 uint64_t get_default_hash_3(const T1 &v1, const T2 &v2, const T3 &v3)
223 {
224  const uint64_t h1 = get_default_hash(v1);
225  const uint64_t h2 = get_default_hash(v2);
226  const uint64_t h3 = get_default_hash(v3);
227  return h1 ^ (h2 * 19349669) ^ (h3 * 83492791);
228 }
229 
230 template<typename T> struct DefaultHash<std::unique_ptr<T>> {
231  uint64_t operator()(const std::unique_ptr<T> &value) const
232  {
233  return get_default_hash(value.get());
234  }
235 };
236 
237 template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> {
238  uint64_t operator()(const std::pair<T1, T2> &value) const
239  {
240  return get_default_hash_2(value.first, value.second);
241  }
242 };
243 
244 } // namespace blender
typedef float(TangentPoint)[2]
_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
#define str(s)
#define T
static unsigned c
Definition: RandGen.cpp:97
TRIVIAL_DEFAULT_INT_HASH(int8_t)
uint64_t hash_string(StringRef str)
Definition: BLI_hash.hh:156
uint64_t get_default_hash(const T &v)
Definition: BLI_hash.hh:209
uint64_t get_default_hash_2(const T1 &v1, const T2 &v2)
Definition: BLI_hash.hh:214
uint64_t get_default_hash_3(const T1 &v1, const T2 &v2, const T3 &v3)
Definition: BLI_hash.hh:222
#define hash
Definition: noise.c:169
signed short int16_t
Definition: stdint.h:79
unsigned short uint16_t
Definition: stdint.h:82
_W64 unsigned int uintptr_t
Definition: stdint.h:122
unsigned int uint32_t
Definition: stdint.h:83
__int64 int64_t
Definition: stdint.h:92
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
unsigned __int64 uint64_t
Definition: stdint.h:93
signed char int8_t
Definition: stdint.h:78
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:184
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:177
uint64_t operator()(const T *value) const
Definition: BLI_hash.hh:201
uint64_t operator()(bool value) const
Definition: BLI_hash.hh:150
uint64_t operator()(const T &value) const
Definition: BLI_hash.hh:110
uint64_t operator()(float value) const
Definition: BLI_hash.hh:143
uint64_t operator()(const std::pair< T1, T2 > &value) const
Definition: BLI_hash.hh:238
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:170
uint64_t operator()(StringRef value) const
Definition: BLI_hash.hh:191
uint64_t operator()(const std::unique_ptr< T > &value) const
Definition: BLI_hash.hh:231
uint64_t operator()(const T &value) const
Definition: BLI_hash.hh:95
#define T2
Definition: util_md5.cpp:36
#define T3
Definition: util_md5.cpp:37
#define T1
Definition: util_md5.cpp:35
PointerRNA * ptr
Definition: wm_files.c:3157