aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/hash-inl.h
blob: 159748a09bcf9809f0fc0c9d6440f23369a211d5 (plain) (blame)
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
#ifndef HASH_INL_H_
#error "Direct inclusion of this file is not allowed, include hash.h"
// For the sake of sane code completion.
#include "hash.h"
#endif

#include <cmath>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

inline void HashCombine(size_t& h, size_t k)
{
    static_assert(sizeof(size_t) == 8, "size_t must be 64 bit.");

    const size_t m = 0xc6a4a7935bd1e995ULL;
    const int r = 47;

    k *= m;
    k ^= k >> r;
    k *= m;

    h ^= k;
    h *= m;
}

template <class T>
void HashCombine(size_t& h, const T& k)
{
    HashCombine(h, THash<T>()(k));
}

template <class T>
Y_FORCE_INLINE size_t NaNSafeHash(const T& value)
{
    return ::THash<T>()(value);
}

template <class T>
    requires std::is_floating_point_v<T>
Y_FORCE_INLINE size_t NaNSafeHash(const T& value)
{
    return std::isnan(value) ? 0 : ::THash<T>()(value);
}

////////////////////////////////////////////////////////////////////////////////

template <class TElement, class TUnderlying>
TRandomizedHash<TElement, TUnderlying>::TRandomizedHash()
    : Seed_(RandomNumber<size_t>())
{ }

template <class TElement, class TUnderlying>
size_t TRandomizedHash<TElement, TUnderlying>::operator ()(const TElement& element) const
{
    return Underlying_(element) + Seed_;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT