aboutsummaryrefslogtreecommitdiffstats
path: root/util/digest/murmur.h
blob: 8d44d3e0df917d5a081b47d0ca39c4ba72eb4bb4 (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
#pragma once

#include <util/system/defaults.h>
#include <util/generic/array_ref.h>

/*
 * murmur2 from http://murmurhash.googlepages.com/
 *
 */
namespace NMurmurPrivate {
    Y_PURE_FUNCTION ui32 MurmurHash32(const void* key, size_t len, ui32 seed) noexcept;

    Y_PURE_FUNCTION ui64 MurmurHash64(const void* key, size_t len, ui64 seed) noexcept;
 
    template <unsigned N>
    struct TMurHelper;

#define DEF_MUR(t)                                                                         \
    template <>                                                                            \
    struct TMurHelper<t> {                                                                 \
        static inline ui##t MurmurHash(const void* buf, size_t len, ui##t init) noexcept { \
            return MurmurHash##t(buf, len, init);                                          \
        }                                                                                  \
    };

    DEF_MUR(32)
    DEF_MUR(64)

#undef DEF_MUR
}

template <class T>
static inline T MurmurHash(const void* buf, size_t len, T init) noexcept {
    return (T)NMurmurPrivate::TMurHelper<8 * sizeof(T)>::MurmurHash(buf, len, init);
}

template <class T>
static inline T MurmurHash(const void* buf, size_t len) noexcept {
    return MurmurHash<T>(buf, len, (T)0);
}

//non-inline version
size_t MurmurHashSizeT(const char* buf, size_t len) noexcept;

template <typename TOut = size_t>
struct TMurmurHash {
    TOut operator()(const void* buf, size_t len) const noexcept {
        return MurmurHash<TOut>(buf, len);
    }

    template <typename ElementType>
    TOut operator()(const TArrayRef<ElementType>& data) const noexcept {
        return operator()(data.data(), data.size() * sizeof(ElementType));
    }
};