mn_hash.hpp
Go to the documentation of this file.
1 
19 #ifndef __MINILIB_BASIC_HASH_H__
20 #define __MINILIB_BASIC_HASH_H__
21 
22 #include "mn_config.hpp"
23 
24 #include <stdint.h>
25 
26 #include "mn_def.hpp"
27 
28 
29 namespace mn {
31 
35  template<typename T>
37  return (result_type)t;
38  }
39 
40  namespace internal {
46  template<typename T>
49 
50  _hash = (_hash + 0x7ed55d16) + (_hash << 12);
51  _hash = (_hash ^ 0xc761c23c) ^ (_hash >> 19);
52  _hash = (_hash + 0x165667b1) + (_hash << 5);
53  _hash = (_hash + 0xd3a2646c) ^ (_hash << 9);
54  _hash = (_hash + 0xfd7046c5) + (_hash << 3);
55  _hash = (_hash ^ 0xb55a4f09) ^ (_hash >> 16);
56  return static_cast<result_type>(_hash);
57  }
58 
59  inline result_type rjenkins_hash_string(const char* strValue) {
60  unsigned long _iRet = 0;
61  while(*strValue) {
62  _iRet += rjenkins_hash<char>(*strValue);
63  ++strValue;
64  }
65  return static_cast<result_type>(_iRet);
66  }
67  }
71  template<typename T>
72  struct hash {
73  const result_type operator()(T& t) const noexcept {
74  return internal::rjenkins_hash(t);
75  }
76  };
77 
78  template<typename T>
79  struct hash<T*>{
80  result_type operator () (T* pPtr) const noexcept {
81  assert(pPtr);
82  auto _value = *pPtr;
83  return internal::rjenkins_hash(_value);
84  }
85  };
86 
87  template<>
88  struct hash<char*>{
89  const result_type operator()(const char* t) const noexcept {
91  }
92  };
93 
94  template<>
95  struct hash<const char*>{
96  const result_type operator()(const char* t) const noexcept {
98  }
99  };
100 
101  template<>
102  struct hash<int8_t>{
103  result_type operator () (int8_t n) const noexcept {
104  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
105  }
106  };
107 
108 
109  template<>
110  struct hash<uint8_t>{
111  result_type operator () (uint8_t n) const noexcept {
112  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
113  }
114  };
115 
116 
117  template<>
118  struct hash<int16_t>{
119  result_type operator () (int16_t n) const noexcept {
120  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
121  }
122  };
123 
124  template<>
125  struct hash<uint16_t>{
126  result_type operator () (uint16_t n) const noexcept {
127  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
128  }
129  };
130 
131  template<>
132  struct hash<int32_t>{
133  result_type operator () (int32_t n) const noexcept {
134  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
135  }
136  };
137 
138  template<>
139  struct hash<uint32_t>{
140  result_type operator () (uint32_t n) const noexcept {
141  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
142  }
143  };
144 
145 
146  template<>
147  struct hash<int64_t>{
148  result_type operator () (int64_t n) const noexcept {
149  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
150  }
151  };
152 
153 
154  template<>
155  struct hash<uint64_t>{
156  result_type operator () (const uint64_t n) const noexcept {
157  return static_cast<result_type>(n) * MN_THREAD_CONFIG_BASIC_HASHMUL_VAL;
158  }
159  };
160 
161  template <class T>
162  struct hash_function {
163  result_type operator () (T key, size_t maxValue) const noexcept {
164  return mn::hash<T>{}(key) % maxValue;
165  }
166  };
167 
168 
169  template <>
170  struct hash_function<const char*> {
171  result_type operator () (const char* key, size_t maxValue) const noexcept {
172  return mn::hash<const char*>{}(key) % maxValue;
173  }
174  };
175 }
176 
177 #endif // __MINILIB_BASIC_HASH_H__
#define MN_THREAD_CONFIG_BASIC_HASHMUL_VAL
Basic value for struct::hash as basic hash calculate.
Definition: mn_config.hpp:154
result_type rjenkins_hash_string(const char *strValue)
Definition: mn_hash.hpp:59
result_type rjenkins_hash(const T &t)
Implementation of hasher. Works for keys that can be converted to 32-bit integer with extract_int_key...
Definition: mn_hash.hpp:47
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
result_type extract_int_key_value(const T &t)
Default implementations, just casts to hash_value.
Definition: mn_hash.hpp:36
mn::size_t result_type
Definition: mn_hash.hpp:30
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
const result_type operator()(const char *t) const noexcept
Definition: mn_hash.hpp:89
Definition: mn_hash.hpp:95
const result_type operator()(const char *t) const noexcept
Definition: mn_hash.hpp:96
Definition: mn_hash.hpp:162
result_type operator()(T key, size_t maxValue) const noexcept
Definition: mn_hash.hpp:163
Default implementation of hasher.
Definition: mn_hash.hpp:72
const result_type operator()(T &t) const noexcept
Definition: mn_hash.hpp:73