Plasma Engine  2.0
Loading...
Searching...
No Matches
HashingMurmur_inl.h
1
2namespace plInternal
3{
4 constexpr plUInt32 MURMUR_M = 0x5bd1e995;
5 constexpr plUInt32 MURMUR_R = 24;
6
7 template <size_t N, size_t Loop>
9 {
10 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const
11 {
12 return CompileTimeMurmurHash<N, Loop - 4>()(CompileTimeMurmurHash<N, 4>()(uiHash, str, i), str, i + 4);
13 }
14 };
15
16 template <size_t N>
18 {
19 static constexpr PL_ALWAYS_INLINE plUInt32 helper(plUInt32 k) { return (k ^ (k >> MURMUR_R)) * MURMUR_M; }
20
21 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const
22 {
23 // In C++11 constexpr local variables are not allowed. Need to express the following without "plUInt32 k"
24 // (this restriction is lifted in C++14's generalized constexpr)
25 // plUInt32 k = ((str[i + 0]) | ((str[i + 1]) << 8) | ((str[i + 2]) << 16) | ((str[i + 3]) << 24));
26 // k *= MURMUR_M;
27 // k ^= (k >> MURMUR_R);
28 // k *= MURMUR_M;
29 // return (hash * MURMUR_M) ^ k;
30
31 return (uiHash * MURMUR_M) ^ helper(((str[i + 0]) | ((str[i + 1]) << 8) | ((str[i + 2]) << 16) | ((str[i + 3]) << 24)) * MURMUR_M);
32 }
33 };
34
35 template <size_t N>
37 {
38 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const
39 {
40 return (uiHash ^ (str[i + 2] << 16) ^ (str[i + 1] << 8) ^ (str[i + 0])) * MURMUR_M;
41 }
42 };
43
44 template <size_t N>
46 {
47 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const
48 {
49 return (uiHash ^ (str[i + 1] << 8) ^ (str[i])) * MURMUR_M;
50 }
51 };
52
53 template <size_t N>
55 {
56 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const { return (uiHash ^ (str[i])) * MURMUR_M; }
57 };
58
59 template <size_t N>
61 {
62 constexpr PL_ALWAYS_INLINE plUInt32 operator()(plUInt32 uiHash, const char (&str)[N], size_t i) const { return uiHash; }
63 };
64
65 constexpr plUInt32 rightShift_and_xorWithPrevSelf(plUInt32 h, plUInt32 uiShift)
66 {
67 return h ^ (h >> uiShift);
68 }
69} // namespace plInternal
70
71template <size_t N>
72constexpr PL_ALWAYS_INLINE plUInt32 plHashingUtils::MurmurHash32String(const char (&str)[N], plUInt32 uiSeed)
73{
74 // In C++11 constexpr local variables are not allowed. Need to express the following without "plUInt32 h"
75 // (this restriction is lifted in C++14's generalized constexpr)
76 // const plUInt32 uiStrlen = (plUInt32)(N - 1);
77 // plUInt32 h = plInternal::CompileTimeMurmurHash<N - 1>(uiSeed ^ uiStrlen, str, 0);
78 // h ^= h >> 13;
79 // h *= plInternal::MURMUR_M;
80 // h ^= h >> 15;
81 // return h;
82
83 return plInternal::rightShift_and_xorWithPrevSelf(
84 plInternal::rightShift_and_xorWithPrevSelf(plInternal::CompileTimeMurmurHash<N, N - 1>()(uiSeed ^ static_cast<plUInt32>(N - 1), str, 0), 13) *
85 plInternal::MURMUR_M,
86 15);
87}
88
89PL_ALWAYS_INLINE plUInt32 plHashingUtils::MurmurHash32String(plStringView sStr, plUInt32 uiSeed)
90{
91 return MurmurHash32(sStr.GetStartPointer(), sStr.GetElementCount(), uiSeed);
92}
static plUInt32 MurmurHash32(const void *pKey, size_t uiSizeInByte, plUInt32 uiSeed=0)
Calculates the 32bit murmur hash of the given key.
Definition HashingUtils.cpp:6
static constexpr plUInt32 MurmurHash32String(const char(&str)[N], plUInt32 uiSeed=0)
Calculates the 32bit murmur hash of a string constant at compile time. Encoding does not matter here.
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
plUInt32 GetElementCount() const
Returns the number of bytes from the start position up to its end.
Definition StringView.h:93
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
Definition HashingMurmur_inl.h:9