File: phmap_utils.h

package info (click to toggle)
spades 3.15.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 96,896 kB
  • sloc: cpp: 850,748; ansic: 156,813; python: 23,134; perl: 4,547; sh: 2,352; makefile: 1,273; java: 890; pascal: 875; xml: 19
file content (310 lines) | stat: -rw-r--r-- 8,408 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#if !defined(phmap_utils_h_guard_)
#define phmap_utils_h_guard_

// ---------------------------------------------------------------------------
// Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com
//
//       minimal header providing phmap::hash_combine
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ---------------------------------------------------------------------------

#include <cstdint>
#include <functional>
#include "phmap_bits.h"

namespace phmap
{

// ---------------------------------------------------------------
// ---------------------------------------------------------------
template<int n> 
struct phmap_mix
{
    inline size_t operator()(size_t) const;
};

template<>
struct phmap_mix<4>
{
    inline size_t operator()(size_t a) const
    {
        static constexpr uint64_t kmul = 0xcc9e2d51UL;
        // static constexpr uint64_t kmul = 0x3B9ACB93UL; // [greg] my own random prime
        uint64_t l = a * kmul;
        return static_cast<size_t>(l ^ (l >> 32));
    }
};

#if defined(PHMAP_HAS_UMUL128)
    template<>
    struct phmap_mix<8>
    {
        // Very fast mixing (similar to Abseil)
        inline size_t operator()(size_t a) const
        {
            static constexpr uint64_t k = 0xde5fb9d2630458e9ULL;
            // static constexpr uint64_t k = 0x7C9D0BF0567102A5ULL; // [greg] my own random prime
            uint64_t h;
            uint64_t l = umul128(a, k, &h);
            return static_cast<size_t>(h + l);
        }
    };
#else
    template<>
    struct phmap_mix<8>
    {
        inline size_t operator()(size_t a) const
        {
            a = (~a) + (a << 21); // a = (a << 21) - a - 1;
            a = a ^ (a >> 24);
            a = (a + (a << 3)) + (a << 8); // a * 265
            a = a ^ (a >> 14);
            a = (a + (a << 2)) + (a << 4); // a * 21
            a = a ^ (a >> 28);
            a = a + (a << 31);
            return static_cast<size_t>(a);
        }
    };
#endif

// --------------------------------------------
template<int n> 
struct fold_if_needed
{
    inline size_t operator()(uint64_t) const;
};

template<>
struct fold_if_needed<4>
{
    inline size_t operator()(uint64_t a) const
    {
        return static_cast<size_t>(a ^ (a >> 32));
    }
};

template<>
struct fold_if_needed<8>
{
    inline size_t operator()(uint64_t a) const
    {
        return static_cast<size_t>(a);
    }
};

// ---------------------------------------------------------------
// see if class T has a hash_value() friend method
// ---------------------------------------------------------------
template<typename T>
struct has_hash_value
{
private:
    typedef std::true_type yes;
    typedef std::false_type no;

    template<typename U> static auto test(int) -> decltype(U::hash_value(std::declval<U&>()) == 1, yes());

    template<typename> static no test(...);

public:
    static constexpr bool value = std::is_same<decltype(test<T>(0)), yes>::value;
};
 
// ---------------------------------------------------------------
//               phmap::Hash
// ---------------------------------------------------------------
template <class T>
struct Hash
{
    template <class U, typename std::enable_if<has_hash_value<U>::value, int>::type = 0>
    size_t _hash(const T& val) const
    {
        return U::hash_value(val);
    }
 
    template <class U, typename std::enable_if<!has_hash_value<U>::value, int>::type = 0>
    size_t _hash(const T& val) const
    {
        return std::hash<T>()(val);
    }
 
    inline size_t operator()(const T& val) const
    {
        return _hash<T>(val);
    }
};

template <class T>
struct Hash<T *>
{
    inline size_t operator()(const T *val) const noexcept
    {
        return static_cast<size_t>(reinterpret_cast<const uintptr_t>(val)); 
    }
};

template<class ArgumentType, class ResultType>
struct phmap_unary_function
{
    typedef ArgumentType argument_type;
    typedef ResultType result_type;
};

template <>
struct Hash<bool> : public phmap_unary_function<bool, size_t>
{
    inline size_t operator()(bool val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<char> : public phmap_unary_function<char, size_t>
{
    inline size_t operator()(char val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<signed char> : public phmap_unary_function<signed char, size_t>
{
    inline size_t operator()(signed char val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<unsigned char> : public phmap_unary_function<unsigned char, size_t>
{
    inline size_t operator()(unsigned char val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<wchar_t> : public phmap_unary_function<wchar_t, size_t>
{
    inline size_t operator()(wchar_t val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<int16_t> : public phmap_unary_function<int16_t, size_t>
{
    inline size_t operator()(int16_t val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<uint16_t> : public phmap_unary_function<uint16_t, size_t>
{
    inline size_t operator()(uint16_t val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<int32_t> : public phmap_unary_function<int32_t, size_t>
{
    inline size_t operator()(int32_t val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<uint32_t> : public phmap_unary_function<uint32_t, size_t>
{
    inline size_t operator()(uint32_t val) const noexcept
    { return static_cast<size_t>(val); }
};

template <>
struct Hash<int64_t> : public phmap_unary_function<int64_t, size_t>
{
    inline size_t operator()(int64_t val) const noexcept
    { return fold_if_needed<sizeof(size_t)>()(static_cast<uint64_t>(val)); }
};

template <>
struct Hash<uint64_t> : public phmap_unary_function<uint64_t, size_t>
{
    inline size_t operator()(uint64_t val) const noexcept
    { return fold_if_needed<sizeof(size_t)>()(val); }
};

template <>
struct Hash<float> : public phmap_unary_function<float, size_t>
{
    inline size_t operator()(float val) const noexcept
    {
        // -0.0 and 0.0 should return same hash
        uint32_t *as_int = reinterpret_cast<uint32_t *>(&val);
        return (val == 0) ? static_cast<size_t>(0) : 
                            static_cast<size_t>(*as_int);
    }
};

template <>
struct Hash<double> : public phmap_unary_function<double, size_t>
{
    inline size_t operator()(double val) const noexcept
    {
        // -0.0 and 0.0 should return same hash
        uint64_t *as_int = reinterpret_cast<uint64_t *>(&val);
        return (val == 0) ? static_cast<size_t>(0) : 
                            fold_if_needed<sizeof(size_t)>()(*as_int);
    }
};

template <class H, int sz> struct Combiner
{
    H operator()(H seed, size_t value);
};

template <class H> struct Combiner<H, 4>
{
    H operator()(H seed, size_t value)
    {
        return seed ^ (value + 0x9e3779b9 + (seed << 6) + (seed >> 2));
    }
};

template <class H> struct Combiner<H, 8>
{
    H operator()(H seed, size_t value)
    {
        return seed ^ (value + size_t(0xc6a4a7935bd1e995) + (seed << 6) + (seed >> 2));
    }
};


// -----------------------------------------------------------------------------
template <typename H>
class HashStateBase {
public:
    template <typename T, typename... Ts>
    static H combine(H state, const T& value, const Ts&... values);

    static H combine(H state) { return state; }
};

template <typename H>
template <typename T, typename... Ts>
H HashStateBase<H>::combine(H seed, const T& v, const Ts&... vs)
{
    return HashStateBase<H>::combine(Combiner<H, sizeof(H)>()(
                                         seed, phmap::Hash<T>()(v)),  vs...);
}

using HashState = HashStateBase<size_t>;

}  // namespace phmap


#endif // phmap_utils_h_guard_