TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
HashTrait.h
Go to the documentation of this file.
1 
12 #ifndef G3D_HashTrait_h
13 #define G3D_HashTrait_h
14 
15 #include "G3D/platform.h"
16 #include "G3D/Crypto.h"
17 #include "G3D/g3dmath.h"
18 #include "G3D/uint128.h"
19 #include <typeinfo>
20 
21 #include <stdint.h>
22 #undef get16bits
23 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
24  || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
25 #define get16bits(d) (*((const uint16_t *) (d)))
26 #endif
27 
28 #if !defined (get16bits)
29 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
30  +(uint32_t)(((const uint8_t *)(d))[0]) )
31 #endif
32 namespace G3D {
35 inline uint32_t superFastHash (const void* _data, size_t numBytes) {
36  const char* data = (const char*)_data;
37  uint32_t hash = (uint32_t)numBytes;
38  uint32_t tmp;
39  int rem;
40 
41  if ((numBytes <= 0) || (data == NULL)) {
42  return 0;
43  }
44 
45  rem = numBytes & 3;
46  numBytes >>= 2;
47 
48  /* Main loop */
49  for (;numBytes > 0; --numBytes) {
50  hash += get16bits (data);
51  tmp = (get16bits (data+2) << 11) ^ hash;
52  hash = (hash << 16) ^ tmp;
53  data += 2*sizeof (uint16_t);
54  hash += hash >> 11;
55  }
56 
57  /* Handle end cases */
58  switch (rem) {
59  case 3: hash += get16bits (data);
60  hash ^= hash << 16;
61  hash ^= data[sizeof (uint16_t)] << 18;
62  hash += hash >> 11;
63  break;
64  case 2: hash += get16bits (data);
65  hash ^= hash << 11;
66  hash += hash >> 17;
67  break;
68  case 1: hash += *data;
69  hash ^= hash << 10;
70  hash += hash >> 1;
71  }
72 
73  /* Force "avalanching" of final 127 bits */
74  hash ^= hash << 3;
75  hash += hash >> 5;
76  hash ^= hash << 4;
77  hash += hash >> 17;
78  hash ^= hash << 25;
79  hash += hash >> 6;
80 
81  return hash;
82 }
83 
84 
91  key = (~key) + (key << 18); // key = (key << 18) - key - 1;
92  key = key ^ (key >> 31);
93  key = key * 21; // key = (key + (key << 2)) + (key << 4);
94  key = key ^ (key >> 11);
95  key = key + (key << 6);
96  return uint32_t(key) ^ uint32_t(key >> 22);
97 }
98 
99 }
100 #undef get16bits
101 
105 template <typename T> struct HashTrait{};
106 
107 template <typename T> struct HashTrait<T*> {
108  static size_t hashCode(const void* k) { return reinterpret_cast<size_t>(k) >> 1; }
109 };
110 
112 template <> struct HashTrait <std::type_info* const> {
113  static size_t hashCode(const std::type_info* const t) {
114 # ifdef _MSC_VER
115  return t->hash_code();
116 # else
117  return reinterpret_cast<size_t>(t) >> 1;
118 # endif
119  }
120 };
121 
122 template <> struct HashTrait <G3D::int16> {
123  static size_t hashCode(G3D::int16 k) { return static_cast<size_t>(k); }
124 };
125 
126 template <> struct HashTrait <G3D::uint16> {
127  static size_t hashCode(G3D::uint16 k) { return static_cast<size_t>(k); }
128 };
129 
130 template <> struct HashTrait <G3D::int32> {
131  static size_t hashCode(G3D::int32 k) { return static_cast<size_t>(k); }
132 };
133 
134 template <> struct HashTrait <G3D::uint32> {
135  static size_t hashCode(G3D::uint32 k) { return static_cast<size_t>(k); }
136 };
137 
138 #ifdef G3D_OSX
139 template <> struct HashTrait <long unsigned int> {
140  static size_t hashCode(G3D::uint32 k) { return static_cast<size_t>(k); }
141 };
142 #endif
143 
144 
145 template <> struct HashTrait <G3D::uint64> {
146  static size_t hashCode(G3D::uint64 k) { return static_cast<size_t>(k) ^ static_cast<size_t>(k >> 32); }
147 };
148 
149 template <> struct HashTrait <G3D::int64> {
151 };
152 
153 
154 template <> struct HashTrait <std::string> {
155  static size_t hashCode(const std::string& k) {
156  return G3D::superFastHash(k.c_str(), k.size());
157  //return static_cast<size_t>(G3D::Crypto::crc32(k.c_str(), k.size()));
158  }
159 };
160 
161 template <> struct HashTrait<G3D::uint128> {
162  static size_t hashCode(G3D::uint128 key) {
163  return G3D::superFastHash(&key, sizeof(key));
164  //return HashTrait<G3D::uint64>::hashCode(key.hi) ^ HashTrait<G3D::uint64>::hashCode(key.lo);
165 
166 #if 0 // Really slow under gcc
167  // Use the FNV-1 hash (http://isthe.com/chongo/tech/comp/fnv/#FNV-1).
168  static const G3D::uint128 FNV_PRIME_128(1 << 24, 0x159);
169  static const G3D::uint128 FNV_OFFSET_128(0xCF470AAC6CB293D2ULL, 0xF52F88BF32307F8FULL);
170 
171  G3D::uint128 hash = FNV_OFFSET_128;
172  G3D::uint128 mask(0, 0xFF);
173  for (int i = 0; i < 16; ++i) {
174  hash *= FNV_PRIME_128;
175  hash ^= (mask & key);
176  key >>= 8;
177  }
178 
179  G3D::uint64 foldedHash = hash.hi ^ hash.lo;
180  return static_cast<size_t>((foldedHash >> 32) ^ (foldedHash & 0xFFFFFFFF));
181 #endif
182  }
183 };
184 
185 #endif
static size_t hashCode(G3D::uint32 k)
Definition: HashTrait.h:135
static size_t hashCode(G3D::uint16 k)
Definition: HashTrait.h:127
int16_t int16
Definition: g3dmath.h:165
size_t hashCode() const
Definition: Vector2unorm16.h:76
#define hash
Definition: private_namespace.h:186
static size_t hashCode(const std::string &k)
Definition: HashTrait.h:155
int64_t int64
Definition: Define.h:145
static size_t hashCode(G3D::int64 k)
Definition: HashTrait.h:150
static size_t hashCode(G3D::uint128 key)
Definition: HashTrait.h:162
G3D::uint64 hi
Definition: uint128.h:23
uint32_t superFastHash(const void *_data, size_t numBytes)
A hash function that is faster than CRC32 for arbitrary long strings http://www.azillionmonkeys.com/qed/hash.html by Paul Hsieh.
Definition: HashTrait.h:35
static size_t hashCode(G3D::uint64 k)
Definition: HashTrait.h:146
Definition: HashTrait.h:105
Definition: AABox.h:25
STL namespace.
arena_t NULL
Definition: jemalloc_internal.h:624
uint64_t uint64
Definition: g3dmath.h:170
uint16_t uint16
Definition: g3dmath.h:166
unsigned int uint32_t
Definition: stdint.h:80
static size_t hashCode(G3D::int16 k)
Definition: HashTrait.h:123
uint32_t wangHash6432Shift(int64 key)
Definition: HashTrait.h:90
Definition: uint128.h:20
static size_t hashCode(const void *k)
Definition: HashTrait.h:108
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
#define get16bits(d)
Definition: HashTrait.h:29
uint16_t uint16
Definition: Define.h:151
int64_t int64
Definition: g3dmath.h:169
int32_t int32
Definition: g3dmath.h:167
G3D::uint64 lo
Definition: uint128.h:24
int16_t int16
Definition: Define.h:147
uint32_t uint32
Definition: g3dmath.h:168
#define const
Definition: zconf.h:217
static size_t hashCode(G3D::int32 k)
Definition: HashTrait.h:131
static size_t hashCode(const std::type_info *const t)
Definition: HashTrait.h:113
unsigned short uint16_t
Definition: stdint.h:79