-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathHashFunctions.hpp
109 lines (94 loc) · 2.3 KB
/
HashFunctions.hpp
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
#ifndef HASH
#define HASH
#include "Base.hpp"
static uint32_t size_ = 32;
uint64_t mix(uint64_t h) {
h ^= (h) >> 23;
(h) *= 0x2127599bf4325c37ULL;
(h) ^= (h) >> 47;
return h;
}
uint32_t jenkinHash(const char *c) {
uint32_t hash = 0;
while(*c) {
hash += *c;
hash += (hash << 10);
hash ^= (hash >> 6);
++c;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
uint32_t murmur_32(const char* key, size_t len, uint32_t seed)
{
uint32_t h = seed;
if (len > 3) {
size_t i = len >> 2;
do {
uint32_t k;
memcpy(&k, key, sizeof(uint32_t));
key += sizeof(uint32_t);
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
h ^= k;
h = (h << 13) | (h >> 19);
h = h * 5 + 0xe6546b64;
} while (--i);
}
if (len & 3) {
size_t i = len & 3;
uint32_t k = 0;
do {
k <<= 8;
k |= key[i - 1];
} while (--i);
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
h ^= k;
}
h ^= len;
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
{
const uint64_t m = 0x880355f21e6d1965ULL;
const uint64_t *pos = (const uint64_t *)buf;
const uint64_t *end = pos + (len / 8);
const unsigned char *pos2;
uint64_t h = seed ^ (len * m);
uint64_t v;
while (pos != end) {
v = *pos++;
h ^= mix(v);
h *= m;
}
pos2 = (const unsigned char*)pos;
v = 0;
switch (len & 7) {
case 7: v ^= (uint64_t)pos2[6] << 48;
case 6: v ^= (uint64_t)pos2[5] << 40;
case 5: v ^= (uint64_t)pos2[4] << 32;
case 4: v ^= (uint64_t)pos2[3] << 24;
case 3: v ^= (uint64_t)pos2[2] << 16;
case 2: v ^= (uint64_t)pos2[1] << 8;
case 1: v ^= (uint64_t)pos2[0];
h ^= mix(v);
h *= m;
}
return mix(h);
}
uint32_t fasthash32(const void *buf, size_t len, uint32_t seed) {
uint64_t Hsh = fasthash64(buf, len, seed);
Hsh -= (Hsh << 32);
return Hsh;
}
#endif