1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | #ifndef PSFOUNDATION_PSHASH_H |
31 | #define PSFOUNDATION_PSHASH_H |
32 | |
33 | #include "Ps.h" |
34 | #include "PsBasicTemplates.h" |
35 | |
36 | #if PX_VC |
37 | #pragma warning(push) |
38 | #pragma warning(disable : 4302) |
39 | #endif |
40 | |
41 | #if PX_LINUX |
42 | #include "foundation/PxSimpleTypes.h" |
43 | #endif |
44 | |
45 | /*! |
46 | Central definition of hash functions |
47 | */ |
48 | |
49 | namespace physx |
50 | { |
51 | namespace shdfnd |
52 | { |
53 | // Hash functions |
54 | |
55 | // Thomas Wang's 32 bit mix |
56 | // http://www.cris.com/~Ttwang/tech/inthash.htm |
57 | PX_FORCE_INLINE uint32_t hash(const uint32_t key) |
58 | { |
59 | uint32_t k = key; |
60 | k += ~(k << 15); |
61 | k ^= (k >> 10); |
62 | k += (k << 3); |
63 | k ^= (k >> 6); |
64 | k += ~(k << 11); |
65 | k ^= (k >> 16); |
66 | return uint32_t(k); |
67 | } |
68 | |
69 | // Thomas Wang's 64 bit mix |
70 | // http://www.cris.com/~Ttwang/tech/inthash.htm |
71 | PX_FORCE_INLINE uint32_t hash(const uint64_t key) |
72 | { |
73 | uint64_t k = key; |
74 | k += ~(k << 32); |
75 | k ^= (k >> 22); |
76 | k += ~(k << 13); |
77 | k ^= (k >> 8); |
78 | k += (k << 3); |
79 | k ^= (k >> 15); |
80 | k += ~(k << 27); |
81 | k ^= (k >> 31); |
82 | return uint32_t(UINT32_MAX & k); |
83 | } |
84 | |
85 | // Hash function for pointers |
86 | template <typename T> |
87 | PX_INLINE uint32_t hash(T* ptr) |
88 | { |
89 | #if PX_P64_FAMILY |
90 | return hash(key: uint64_t(ptr)); |
91 | #else |
92 | return hash(uint32_t(UINT32_MAX & size_t(ptr))); |
93 | #endif |
94 | } |
95 | |
96 | // Hash function for pairs |
97 | template <typename F, typename S> |
98 | PX_INLINE uint32_t hash(const Pair<F, S>& p) |
99 | { |
100 | uint32_t seed = 0x876543; |
101 | uint32_t m = 1000007; |
102 | return hash(p.second) ^ (m * (hash(p.first) ^ (m * seed))); |
103 | } |
104 | |
105 | template <typename T> |
106 | PX_FORCE_INLINE uint32_t hash(const T key) |
107 | { |
108 | if (sizeof(T) == 4) |
109 | return hash(key: uint32_t(key)); |
110 | return hash(key: uint64_t(key)); |
111 | } |
112 | |
113 | // hash object for hash map template parameter |
114 | template <class Key> |
115 | struct Hash |
116 | { |
117 | uint32_t operator()(const Key& k) const |
118 | { |
119 | return hash(k); |
120 | } |
121 | bool equal(const Key& k0, const Key& k1) const |
122 | { |
123 | return k0 == k1; |
124 | } |
125 | }; |
126 | |
127 | // specialization for strings |
128 | template <> |
129 | struct Hash<const char*> |
130 | { |
131 | public: |
132 | uint32_t operator()(const char* _string) const |
133 | { |
134 | // "DJB" string hash |
135 | const uint8_t* string = reinterpret_cast<const uint8_t*>(_string); |
136 | uint32_t h = 5381; |
137 | for(const uint8_t* ptr = string; *ptr; ptr++) |
138 | h = ((h << 5) + h) ^ uint32_t(*ptr); |
139 | return h; |
140 | } |
141 | bool equal(const char* string0, const char* string1) const |
142 | { |
143 | return !strcmp(s1: string0, s2: string1); |
144 | } |
145 | }; |
146 | |
147 | } // namespace shdfnd |
148 | } // namespace physx |
149 | |
150 | #if PX_VC |
151 | #pragma warning(pop) |
152 | #endif |
153 | |
154 | #endif // #ifndef PSFOUNDATION_PSHASH_H |
155 | |