3es  0.7
3esvectorhash.h
1 /*
2 License from the NVIDIA source on which this code is based:
3 Copyright (c) 2009-2011, NVIDIA Corporation
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of NVIDIA Corporation nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef _3ESVECTORHASH_H
30 #define _3ESVECTORHASH_H
31 
32 #include <cmath>
33 #include <cinttypes>
34 #include <unordered_map>
35 
37 #define VHASH_MAGIC (0x9e3779b9u)
38 
39 // By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net.
40 #define VHASH_JENKINS_MIX(a, b, c) \
41  a -= b; a -= c; a ^= (c>>13); \
42  b -= c; b -= a; b ^= (a<<8); \
43  c -= a; c -= b; c ^= (b>>13); \
44  a -= b; a -= c; a ^= (c>>12); \
45  b -= c; b -= a; b ^= (a<<16); \
46  c -= a; c -= b; c ^= (b>>5); \
47  a -= b; a -= c; a ^= (c>>3); \
48  b -= c; b -= a; b ^= (a<<10); \
49  c -= a; c -= b; c ^= (b>>15);
50 
51 namespace tes
52 {
57  namespace vhash
58  {
63  inline uint32_t hashBits(uint32_t a, uint32_t b = VHASH_MAGIC, uint32_t c = 0)
64  {
65  c += VHASH_MAGIC;
66  VHASH_JENKINS_MIX(a, b, c);
67  return c;
68  }
69 
70 
78  inline uint32_t hashBits(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e = 0, uint32_t f = 0)
79  {
80  c += VHASH_MAGIC;
81  VHASH_JENKINS_MIX(a, b, c);
82  a += d;
83  b += e;
84  c += f;
85  VHASH_JENKINS_MIX(a, b, c);
86  return c;
87  }
88 
89 
94  inline uint32_t hash(float x, float y, float z)
95  {
96  return hashBits(*(const uint32_t *)&x, *(const uint32_t *)&y, *(const uint32_t *)&z);
97  }
98 
99 
105  inline uint32_t hash(float x, float y, float z, float w)
106  {
107  return hashBits(*(const uint32_t *)&x, *(const uint32_t *)&y, *(const uint32_t *)&z, *(const uint32_t *)&w);
108  }
109  }
110 
111 
114  template <class T>
116  {
117  public:
121  inline size_t operator()(const T &p) const
122  {
123  return vhash::hash(p.x, p.y, p.z);
124  }
125  };
126 }
127 
128 #endif // _3ESVECTORHASH_H
uint32_t hashBits(uint32_t a, uint32_t b=VHASH_MAGIC, uint32_t c=0)
Generate a hash for 2 to 3 components.
Definition: 3esvectorhash.h:63
Hash structure for use with standard library maps.
Definition: 3esvectorhash.h:115
Definition: 3esbounds.h:13
uint32_t hash(float x, float y, float z)
Generate a hash code for a 3-component vertex.
Definition: 3esvectorhash.h:94
size_t operator()(const T &p) const
Operator to convert the vector p to its hash code.
Definition: 3esvectorhash.h:121