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