summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/hash.h
blob: 7b8b9e8aafc76d3f0e7e251259821f8be8515c02 (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
#pragma once

#include <util/generic/hash.h>

#include <util/random/random.h>

namespace NYT {

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

//! Finalization function that makes each bit of the output depend on each bit of the input.
//! Needed to achieve unbiased distribution for bit-sensitive application like HLL,
//! as opposed to raw collision minimization.
//! This is also SplitMix64 PRNG.
//! Cf. |http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html|, |boost::random::splitmix64|.
size_t SplitMix(size_t value);

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

//! Updates #h with #k.
//! Cf. |boost::hash_combine|.
void HashCombine(size_t& h, size_t k);

//! Updates #h with the hash of #k.
//! Cf. |boost::hash_combine|.
template <class T>
void HashCombine(size_t& h, const T& k);

//! Computes the hash of #value handling NaN values gracefully
//! (returning the same constant for all NaNs).
//! If |T| is not a floating-point type, #NaNSafeHash is equivalent to #THash.
template <class T>
size_t NaNSafeHash(const T& value);

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

//! Provides a hasher that randomizes the results of another one.
//! \note In case seed value is 0, the hash is just the underlying hash.
template <class TElement, class TUnderlying = ::THash<TElement>>
class TRandomizedHash
{
public:
    TRandomizedHash();
    explicit TRandomizedHash(size_t seed);

    template <class THeterogenousElement>
    size_t operator()(const THeterogenousElement& element) const;

private:
    size_t Seed_;
    TUnderlying Underlying_;
};

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

} // namespace NYT

#define HASH_INL_H_
#include "hash-inl.h"
#undef HASH_INL_H_