RavEngine
Loading...
Searching...
No Matches
phmap_utils.h
1#if !defined(phmap_utils_h_guard_)
2#define phmap_utils_h_guard_
3
4// ---------------------------------------------------------------------------
5// Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com
6//
7// minimal header providing phmap::HashState
8//
9// use as: phmap::HashState().combine(0, _first_name, _last_name, _age);
10//
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14//
15// https://www.apache.org/licenses/LICENSE-2.0
16//
17// Unless required by applicable law or agreed to in writing, software
18// distributed under the License is distributed on an "AS IS" BASIS,
19// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20// See the License for the specific language governing permissions and
21// limitations under the License.
22// ---------------------------------------------------------------------------
23
24#ifdef _MSC_VER
25 #pragma warning(push)
26 #pragma warning(disable : 4514) // unreferenced inline function has been removed
27 #pragma warning(disable : 4710) // function not inlined
28 #pragma warning(disable : 4711) // selected for automatic inline expansion
29#endif
30
31#include <cstdint>
32#include <functional>
33#include <tuple>
34#include "phmap_bits.h"
35
36// ---------------------------------------------------------------
37// Absl forward declaration requires global scope.
38// ---------------------------------------------------------------
39#if defined(PHMAP_USE_ABSL_HASH) && !defined(phmap_fwd_decl_h_guard_) && !defined(ABSL_HASH_HASH_H_)
40 namespace absl { template <class T> struct Hash; };
41#endif
42
43namespace phmap
44{
45
46// ---------------------------------------------------------------
47// ---------------------------------------------------------------
48template<int n>
50{
51 inline size_t operator()(size_t) const;
52};
53
54template<>
55struct phmap_mix<4>
56{
57 inline size_t operator()(size_t a) const
58 {
59 static constexpr uint64_t kmul = 0xcc9e2d51UL;
60 // static constexpr uint64_t kmul = 0x3B9ACB93UL; // [greg] my own random prime
61 uint64_t l = a * kmul;
62 return static_cast<size_t>(l ^ (l >> 32));
63 }
64};
65
66#if defined(PHMAP_HAS_UMUL128)
67 template<>
68 struct phmap_mix<8>
69 {
70 // Very fast mixing (similar to Abseil)
71 inline size_t operator()(size_t a) const
72 {
73 static constexpr uint64_t k = 0xde5fb9d2630458e9ULL;
74 // static constexpr uint64_t k = 0x7C9D0BF0567102A5ULL; // [greg] my own random prime
75 uint64_t h;
76 uint64_t l = umul128(a, k, &h);
77 return static_cast<size_t>(h + l);
78 }
79 };
80#else
81 template<>
82 struct phmap_mix<8>
83 {
84 inline size_t operator()(size_t a) const
85 {
86 a = (~a) + (a << 21); // a = (a << 21) - a - 1;
87 a = a ^ (a >> 24);
88 a = (a + (a << 3)) + (a << 8); // a * 265
89 a = a ^ (a >> 14);
90 a = (a + (a << 2)) + (a << 4); // a * 21
91 a = a ^ (a >> 28);
92 a = a + (a << 31);
93 return static_cast<size_t>(a);
94 }
95 };
96#endif
97
98// --------------------------------------------
99template<int n>
101{
102 inline size_t operator()(uint64_t) const;
103};
104
105template<>
107{
108 inline size_t operator()(uint64_t a) const
109 {
110 return static_cast<size_t>(a ^ (a >> 32));
111 }
112};
113
114template<>
116{
117 inline size_t operator()(uint64_t a) const
118 {
119 return static_cast<size_t>(a);
120 }
121};
122
123// ---------------------------------------------------------------
124// see if class T has a hash_value() friend method
125// ---------------------------------------------------------------
126template<typename T>
128{
129private:
130 typedef std::true_type yes;
131 typedef std::false_type no;
132
133 template<typename U> static auto test(int) -> decltype(hash_value(std::declval<const U&>()) == 1, yes());
134
135 template<typename> static no test(...);
136
137public:
138 static constexpr bool value = std::is_same<decltype(test<T>(0)), yes>::value;
139};
140
141#if defined(PHMAP_USE_ABSL_HASH) && !defined(phmap_fwd_decl_h_guard_)
142 template <class T> using Hash = ::absl::Hash<T>;
143#elif !defined(PHMAP_USE_ABSL_HASH)
144// ---------------------------------------------------------------
145// phmap::Hash
146// ---------------------------------------------------------------
147template <class T>
148struct Hash
149{
150 template <class U, typename std::enable_if<has_hash_value<U>::value, int>::type = 0>
151 size_t _hash(const T& val) const
152 {
153 return hash_value(val);
154 }
155
156 template <class U, typename std::enable_if<!has_hash_value<U>::value, int>::type = 0>
157 size_t _hash(const T& val) const
158 {
159 return std::hash<T>()(val);
160 }
161
162 inline size_t operator()(const T& val) const
163 {
164 return _hash<T>(val);
165 }
166};
167
168template <class T>
169struct Hash<T *>
170{
171 inline size_t operator()(const T *val) const noexcept
172 {
173 return static_cast<size_t>(reinterpret_cast<const uintptr_t>(val));
174 }
175};
176
177template<class ArgumentType, class ResultType>
179{
180 typedef ArgumentType argument_type;
181 typedef ResultType result_type;
182};
183
184template <>
185struct Hash<bool> : public phmap_unary_function<bool, size_t>
186{
187 inline size_t operator()(bool val) const noexcept
188 { return static_cast<size_t>(val); }
189};
190
191template <>
192struct Hash<char> : public phmap_unary_function<char, size_t>
193{
194 inline size_t operator()(char val) const noexcept
195 { return static_cast<size_t>(val); }
196};
197
198template <>
199struct Hash<signed char> : public phmap_unary_function<signed char, size_t>
200{
201 inline size_t operator()(signed char val) const noexcept
202 { return static_cast<size_t>(val); }
203};
204
205template <>
206struct Hash<unsigned char> : public phmap_unary_function<unsigned char, size_t>
207{
208 inline size_t operator()(unsigned char val) const noexcept
209 { return static_cast<size_t>(val); }
210};
211
212template <>
213struct Hash<wchar_t> : public phmap_unary_function<wchar_t, size_t>
214{
215 inline size_t operator()(wchar_t val) const noexcept
216 { return static_cast<size_t>(val); }
217};
218
219template <>
220struct Hash<int16_t> : public phmap_unary_function<int16_t, size_t>
221{
222 inline size_t operator()(int16_t val) const noexcept
223 { return static_cast<size_t>(val); }
224};
225
226template <>
227struct Hash<uint16_t> : public phmap_unary_function<uint16_t, size_t>
228{
229 inline size_t operator()(uint16_t val) const noexcept
230 { return static_cast<size_t>(val); }
231};
232
233template <>
234struct Hash<int32_t> : public phmap_unary_function<int32_t, size_t>
235{
236 inline size_t operator()(int32_t val) const noexcept
237 { return static_cast<size_t>(val); }
238};
239
240template <>
241struct Hash<uint32_t> : public phmap_unary_function<uint32_t, size_t>
242{
243 inline size_t operator()(uint32_t val) const noexcept
244 { return static_cast<size_t>(val); }
245};
246
247template <>
248struct Hash<int64_t> : public phmap_unary_function<int64_t, size_t>
249{
250 inline size_t operator()(int64_t val) const noexcept
251 { return fold_if_needed<sizeof(size_t)>()(static_cast<uint64_t>(val)); }
252};
253
254template <>
255struct Hash<uint64_t> : public phmap_unary_function<uint64_t, size_t>
256{
257 inline size_t operator()(uint64_t val) const noexcept
258 { return fold_if_needed<sizeof(size_t)>()(val); }
259};
260
261template <>
262struct Hash<float> : public phmap_unary_function<float, size_t>
263{
264 inline size_t operator()(float val) const noexcept
265 {
266 // -0.0 and 0.0 should return same hash
267 uint32_t *as_int = reinterpret_cast<uint32_t *>(&val);
268 return (val == 0) ? static_cast<size_t>(0) :
269 static_cast<size_t>(*as_int);
270 }
271};
272
273template <>
274struct Hash<double> : public phmap_unary_function<double, size_t>
275{
276 inline size_t operator()(double val) const noexcept
277 {
278 // -0.0 and 0.0 should return same hash
279 uint64_t *as_int = reinterpret_cast<uint64_t *>(&val);
280 return (val == 0) ? static_cast<size_t>(0) :
282 }
283};
284
285#endif
286
287template <class H, int sz> struct Combiner
288{
289 H operator()(H seed, size_t value);
290};
291
292template <class H> struct Combiner<H, 4>
293{
294 H operator()(H seed, size_t value)
295 {
296 return seed ^ (value + 0x9e3779b9 + (seed << 6) + (seed >> 2));
297 }
298};
299
300template <class H> struct Combiner<H, 8>
301{
302 H operator()(H seed, size_t value)
303 {
304 return seed ^ (value + size_t(0xc6a4a7935bd1e995) + (seed << 6) + (seed >> 2));
305 }
306};
307
308// define HashState to combine member hashes... see example below
309// -----------------------------------------------------------------------------
310template <typename H>
312public:
313 template <typename T, typename... Ts>
314 static H combine(H state, const T& value, const Ts&... values);
315
316 static H combine(H state) { return state; }
317};
318
319template <typename H>
320template <typename T, typename... Ts>
321H HashStateBase<H>::combine(H seed, const T& v, const Ts&... vs)
322{
323 return HashStateBase<H>::combine(Combiner<H, sizeof(H)>()(
324 seed, phmap::Hash<T>()(v)),
325 vs...);
326}
327
328using HashState = HashStateBase<size_t>;
329
330// -----------------------------------------------------------------------------
331
332#if !defined(PHMAP_USE_ABSL_HASH)
333
334// define Hash for std::pair
335// -------------------------
336template<class T1, class T2>
337struct Hash<std::pair<T1, T2>> {
338 size_t operator()(std::pair<T1, T2> const& p) const noexcept {
339 return phmap::HashState().combine(phmap::Hash<T1>()(p.first), p.second);
340 }
341};
342
343// define Hash for std::tuple
344// --------------------------
345template<class... T>
346struct Hash<std::tuple<T...>> {
347 size_t operator()(std::tuple<T...> const& t) const noexcept {
348 return _hash_helper(t);
349 }
350
351private:
352 template<size_t I = 0, class ...P>
353 typename std::enable_if<I == sizeof...(P), size_t>::type
354 _hash_helper(const std::tuple<P...> &) const noexcept { return 0; }
355
356 template<size_t I = 0, class ...P>
357 typename std::enable_if<I < sizeof...(P), size_t>::type
358 _hash_helper(const std::tuple<P...> &t) const noexcept {
359 const auto &el = std::get<I>(t);
360 using el_type = typename std::remove_cv<typename std::remove_reference<decltype(el)>::type>::type;
362 phmap::Hash<el_type>()(el), _hash_helper<I + 1>(t));
363 }
364};
365
366
367#endif
368
369
370} // namespace phmap
371
372#ifdef _MSC_VER
373 #pragma warning(pop)
374#endif
375
376#endif // phmap_utils_h_guard_
Definition phmap_utils.h:311
Definition phmap_utils.h:288
Definition phmap_utils.h:149
Definition phmap_utils.h:101
Definition phmap_utils.h:128
Definition phmap_utils.h:50
Definition phmap_utils.h:179