AGX Dynamics 2.41.3.0
Loading...
Searching...
No Matches
HashFunction.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
28namespace agx
29{
30
32 // See hash function declarations in agx/hash.h
34
35
36
37
38 // Implementation of basic hash functions
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 <>
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 return hash((UInt64)(key));
156 }
157
158 };
159#endif
160
161 // Pointer hashing
162
163 template <typename T>
164 struct HashFn<T *>
165 {
166 typedef const T *PtrT;
167
169 {
170 return hash((UInt64)(key));
171 }
172 };
173
174 // String hashing
175
176 // Hash functions
177
178 template <typename T>
179 AGX_FORCE_INLINE UInt32 stringHash(const T& key, UInt32 startValue = 0)
180 {
181 UInt32 hashValue = startValue;
182
183 for (UInt32 i = 0; i < key.length(); i++)
184 hashValue = 37 * hashValue + (UInt32)key[i];
185
186 return hashValue;
187 }
188
189
190 template<>
191 struct HashFn<std::string>
192 {
193 AGX_FORCE_INLINE UInt32 operator()(const std::string& key) const
194 {
195 return agx::stringHash(key);
196 }
197 };
198
199
200 template<>
202 {
204 {
205 return agx::stringHash(key);
206 }
207 };
208
209
210 template<>
211 struct HashFn<const char *>
212 {
213 AGX_FORCE_INLINE UInt32 operator()(const char *key) const
214 {
215 UInt32 hashValue = 0;
216
217 while (*key)
218 hashValue = 37 * hashValue + (UInt32)(*key++);
219
220 return hashValue;
221 }
222 };
223
224
225 AGX_FORCE_INLINE bool hashKeyEqual(char *key1, char *key2)
226 {
227 return strcmp(key1, key2) == 0;
228 }
229
230 AGX_FORCE_INLINE bool hashKeyEqual(const char *key1, const char *key2)
231 {
232 return strcmp(key1, key2) == 0;
233 }
234
235
236}
237
238#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 hash(const T &key)
Definition: HashFunction.h:65
UInt32 stringHash(const T &key, UInt32 startValue=0)
Definition: HashFunction.h:179
int32_t Int32
Definition: Integer.h:37
AGXCORE_EXPORT void splitHashKey(agx::UInt64 key, agx::UInt32 &id1, agx::UInt32 &id2)
Definition: HashFunction.h:51
uint32_t UInt32
Definition: Integer.h:32
uint64_t UInt64
Definition: Integer.h:33
int64_t Int64
Definition: Integer.h:38
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
STL namespace.
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:168
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:203
UInt32 operator()(const char *key) const
Definition: HashFunction.h:213
UInt32 operator()(const std::pair< T1, T2 > &key) const
Definition: HashFunction.h:87
UInt32 operator()(const std::string &key) const
Definition: HashFunction.h:193
UInt32 operator()(const T &key) const
Definition: HashFunction.h:58