AGX Dynamics  2.35.0.1
HashFunction.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_HASHFUNCTIONS_H
18 #define AGX_HASHFUNCTIONS_H
19 
20 #include <stddef.h>
21 #include <cstring>
22 
23 #include <agx/agx.h>
24 #include <agx/Integer.h>
25 #include <agx/String.h>
26 #include <agx/hash.h>
27 
28 namespace agx
29 {
30 
32  // See hash function declarations in agx/hash.h
34 
35 
36 
37 
38  // Implementation of basic hash functions
39 
40 
42  {
43  return (id1 < id2) ? (((UInt64)id1 << 32) | (UInt64)id2) : (((UInt64)id2 << 32) | (UInt64)id1);
44  }
45 
47  {
48  return (UInt32)key == id || (UInt32)(key >> 32) == id;
49  }
50 
52  {
53  id1 = (UInt32)key;
54  id2 = (UInt32)(key >> 32);
55  }
56 
57  template <typename T>
58  UInt32 HashFn<T>::operator()(const T& key) const
59  {
60  return key.hash();
61  }
62 
63 
64  template <typename T>
66  {
67  HashFn<T> hashFn;
68  return hashFn(key);
69  }
70 
72  {
73  return h1 ^ (h2 + 0x9e3779b9 + (h1<<6) + (h1>>2));
74  }
75 
76  template <typename T1, typename T2>
77  AGX_FORCE_INLINE bool hashKeyEqual(const T1& key1, const T2& key2)
78  {
79  return key1 == key2;
80  }
81 
82 
83  // Pair hashing
84  template<typename T1, typename T2>
85  struct HashFn< std::pair<T1, T2> >
86  {
87  AGX_FORCE_INLINE UInt32 operator()(const std::pair<T1,T2>& key) const
88  {
89  return hash(hash(key.first), hash(key.second));
90  }
91  };
92 
93 
94 
95  // Integer hash functions
96  template <>
97  struct HashFn<UInt32>
98  {
100  {
101  /* http://www.concentric.net/~Ttwang/tech/inthash.htm */
102  key = ~key + (key << 15); // key = (key << 15) - key - 1;
103  key = key ^ (key >> 12);
104  key = key + (key << 2);
105  key = key ^ (key >> 4);
106  key = key * 2057; // key = (key + (key << 3)) + (key << 11);
107  key = key ^ (key >> 16);
108  return key;
109  }
110  };
111 
112  template <>
113  struct HashFn<Int32>
114  {
116  {
117  return hash((UInt32)key);
118  }
119 
120  };
121 
122  template <>
123  struct HashFn<UInt64>
124  {
126  {
127  /* http://www.concentric.net/~Ttwang/tech/inthash.htm */
128  key = (~key) + (key << 18); // key = (key << 18) - key - 1;
129  key = key ^ (key >> 31);
130  key = key * 21; // key = (key + (key << 2)) + (key << 4);
131  key = key ^ (key >> 11);
132  key = key + (key << 6);
133  key = key ^ (key >> 22);
134  return (UInt32)key;
135  }
136  };
137 
138  template <>
139  struct HashFn<Int64>
140  {
142  {
143  return hash((UInt64)key);
144  }
145 
146  };
147 
148 
149 #ifdef __APPLE__
150  template <>
151  struct HashFn<size_t>
152  {
153  AGX_FORCE_INLINE UInt32 operator()(size_t key) const
154  {
155  #if AGX_64BIT_ARCHITECTURE
156  return hash((UInt64)(key));
157  #else
158  return hash((UInt32)(key));
159  #endif
160  }
161 
162  };
163 #endif
164 
165  // Pointer hashing
166 
167  template <typename T>
168  struct HashFn<T *>
169  {
170  typedef const T *PtrT;
171 
173  {
174  #if AGX_64BIT_ARCHITECTURE
175  return hash((UInt64)(key));
176  #else
177  return hash((UInt32)(key));
178  #endif
179  }
180  };
181 
182  // String hashing
183 
184  // Hash functions
185 
186  template <typename T>
187  AGX_FORCE_INLINE UInt32 stringHash(const T& key, UInt32 startValue = 0)
188  {
189  UInt32 hashValue = startValue;
190 
191  for (UInt32 i = 0; i < key.length(); i++)
192  hashValue = 37 * hashValue + (UInt32)key[i];
193 
194  return hashValue;
195  }
196 
197 
198  template<>
199  struct HashFn<std::string>
200  {
201  AGX_FORCE_INLINE UInt32 operator()(const std::string& key) const
202  {
203  return agx::stringHash(key);
204  }
205  };
206 
207 
208  template<>
209  struct HashFn<agx::String>
210  {
212  {
213  return agx::stringHash(key);
214  }
215  };
216 
217 
218  template<>
219  struct HashFn<const char *>
220  {
221  AGX_FORCE_INLINE UInt32 operator()(const char *key) const
222  {
223  UInt32 hashValue = 0;
224 
225  while (*key)
226  hashValue = 37 * hashValue + (UInt32)(*key++);
227 
228  return hashValue;
229  }
230  };
231 
232 
233  AGX_FORCE_INLINE bool hashKeyEqual(char *key1, char *key2)
234  {
235  return strcmp(key1, key2) == 0;
236  }
237 
238  AGX_FORCE_INLINE bool hashKeyEqual(const char *key1, const char *key2)
239  {
240  return strcmp(key1, key2) == 0;
241  }
242 
243 
244 }
245 
246 #endif /* _AGX_HASHFUNCTIONS_H_ */
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
UInt32 stringHash(const T &key, UInt32 startValue=0)
Definition: HashFunction.h:187
int32_t Int32
Definition: Integer.h:45
AGXCORE_EXPORT void splitHashKey(agx::UInt64 key, agx::UInt32 &id1, agx::UInt32 &id2)
Definition: HashFunction.h:51
uint32_t UInt32
Definition: Integer.h:40
uint64_t UInt64
Definition: Integer.h:41
AGXCORE_EXPORT UInt32 hash(const T &key)
Definition: HashFunction.h:65
int64_t Int64
Definition: Integer.h:46
AGXCORE_EXPORT agx::UInt64 buildHashKey(agx::UInt32 id1, agx::UInt32 id2)
Definition: HashFunction.h:41
bool hashKeyEqual(const T1 &key1, const T2 &key2)
Definition: HashFunction.h:77
AGXCORE_EXPORT bool hashKeyContains(agx::UInt64 key, agx::UInt32 id)
Definition: HashFunction.h:46
UInt32 operator()(Int32 key) const
Definition: HashFunction.h:115
UInt32 operator()(Int64 key) const
Definition: HashFunction.h:141
UInt32 operator()(const PtrT key) const
Definition: HashFunction.h:172
UInt32 operator()(UInt32 key) const
Definition: HashFunction.h:99
UInt32 operator()(UInt64 key) const
Definition: HashFunction.h:125
UInt32 operator()(const agx::String &key) const
Definition: HashFunction.h:211
UInt32 operator()(const char *key) const
Definition: HashFunction.h:221
UInt32 operator()(const std::pair< T1, T2 > &key) const
Definition: HashFunction.h:87
UInt32 operator()(const std::string &key) const
Definition: HashFunction.h:201
UInt32 operator()(const T &key) const
Definition: HashFunction.h:58