GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Hash.hpp
Go to the documentation of this file.
1 #ifndef RADIX_UTIL_HASH_HPP
2 #define RADIX_UTIL_HASH_HPP
3 
4 #include <cstdint>
5 
6 namespace radix {
7 
8 namespace impl {
9 
10 // MurmurHash2, by Austin Appleby, https://sites.google.com/site/murmurhash/
11 #if 0
12 constexpr unsigned int MurmurHash2(const void *key, int len, unsigned int seed) {
13  static_assert(sizeof(int) == 4, "int type isn't 4 bytes long");
14  const unsigned int m = 0x5bd1e995;
15  const int r = 24;
16  unsigned int h = seed ^ len;
17  const unsigned char *data = (const unsigned char*)key;
18  while (len >= 4) {
19  unsigned int k = *(unsigned int *)data;
20  k *= m;
21  k ^= k >> r;
22  k *= m;
23  h *= m;
24  h ^= k;
25  data += 4;
26  len -= 4;
27  }
28  switch (len) {
29  case 3: h ^= data[2] << 16;
30  case 2: h ^= data[1] << 8;
31  case 1: h ^= data[0];
32  h *= m;
33  };
34  h ^= h >> 13;
35  h *= m;
36  h ^= h >> 15;
37  return h;
38 }
39 #endif
40 constexpr unsigned int MurmurHashNeutral2(const char *key, int len, unsigned int seed) {
41  static_assert(sizeof(int) == 4, "int type isn't 4 bytes long");
42  const unsigned int m = 0x5bd1e995;
43  const int r = 24;
44  unsigned int h = seed ^ len;
45  const char *data = key;
46  while (len >= 4) {
47  unsigned int k = data[0];
48  k |= data[1] << 8;
49  k |= data[2] << 16;
50  k |= data[3] << 24;
51  k *= m;
52  k ^= k >> r;
53  k *= m;
54  h *= m;
55  h ^= k;
56  data += 4;
57  len -= 4;
58  }
59  switch (len) {
60  case 3: h ^= data[2] << 16;
61  case 2: h ^= data[1] << 8;
62  case 1: h ^= data[0];
63  h *= m;
64  };
65  h ^= h >> 13;
66  h *= m;
67  h ^= h >> 15;
68  return h;
69 }
70 
71 }
72 
73 /*constexpr uint32_t Hash32(const void *key, int len) {
74  return impl::MurmurHashNeutral2(key, len, 0);
75 }*/
76 
77 constexpr int conststrlen(const char *str) {
78  return *str ? 1 + conststrlen(str + 1) : 0;
79 }
80 
81 constexpr uint32_t Hash32(const char *str) {
82  return impl::MurmurHashNeutral2(str, conststrlen(str), 0);
83 }
84 
85 } /* namespace radix */
86 
87 #endif /* RADIX_UTIL_HASH_HPP */
Definition: GameController.hpp:7
constexpr unsigned int MurmurHashNeutral2(const char *key, int len, unsigned int seed)
Definition: Hash.hpp:40
constexpr int conststrlen(const char *str)
Definition: Hash.hpp:77
constexpr uint32_t Hash32(const char *str)
Definition: Hash.hpp:81