-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashutil.h
181 lines (148 loc) · 4.78 KB
/
hashutil.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef HASHUTIL_H_
#define HASHUTIL_H_
#include <climits>
#include <cstring>
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <sys/types.h>
#include "Hash_functions/BobJenkins.h"
#include "Hash_functions/wyhash.h"
#include "Hash_functions/xxhash64.h"
#include <random>
#include <vector>
#include <assert.h>
// #include "Hash_functions/woothash.h"
namespace hashing {
// See Martin Dietzfelbinger, "Universal hashing and k-wise independent random
// variables via integer arithmetic without primes".
class TwoIndependentMultiplyShift {
unsigned __int128 multiply_, add_;
public:
TwoIndependentMultiplyShift() {
::std::random_device random;
for (auto v : {&multiply_, &add_}) {
*v = random();
for (int i = 1; i <= 4; ++i) {
*v = *v << 32;
*v |= random();
}
}
}
/**
* @brief Construct a new Two Independent Multiply Shift object
* Disable the randomness for debugging.
*
* @param seed1 Garbage
* @param seed2 Garbage
*/
TwoIndependentMultiplyShift(unsigned __int128 seed1, unsigned __int128 seed2) {
std::cout << "hash function is pseudo random" << std::endl;
multiply_ = 0xaaaa'bbbb'cccc'dddd;
multiply_ <<= 64;
multiply_ |= 0xeeee'ffff'1111'0000;
add_ = 0xaaaa'aaaa'bbbb'bbbb;
add_ <<= 64;
add_ |= 0xcccc'cccc'dddd'dddd;
assert(multiply_ > 18446744073709551615ULL);
assert(add_ > 18446744073709551615ULL);
}
inline uint64_t operator()(uint64_t key) const {
return (add_ + multiply_ * static_cast<decltype(multiply_)>(key)) >> 64;
}
inline uint32_t hash32(uint64_t key) const {
return ((uint32_t)(add_ + multiply_ * static_cast<decltype(multiply_)>(key)));
}
auto get_name() const -> std::string {
return "TwoIndependentMultiplyShift";
}
};
class SimpleMixSplit {
public:
uint64_t seed;
SimpleMixSplit() {
::std::random_device random;
seed = random();
seed <<= 32;
seed |= random();
}
inline static uint64_t murmur64(uint64_t h) {
h ^= h >> 33;
h *= UINT64_C(0xff51afd7ed558ccd);
h ^= h >> 33;
h *= UINT64_C(0xc4ceb9fe1a85ec53);
h ^= h >> 33;
return h;
}
inline uint64_t operator()(uint64_t key) const {
return murmur64(key + seed);
}
};
class my_xxhash64 {
uint64_t seed;
public:
my_xxhash64() {
seed = random();
}
inline uint64_t operator()(uint64_t key) const {
return XXHash64::hash(&key, 8, seed);
}
auto get_name() const -> std::string {
return "xxhash64";
}
};
class my_wyhash64 {
uint64_t seed;
public:
my_wyhash64() {
seed = random();
}
inline uint64_t operator()(uint64_t key) const {
return wyhash64(key, seed);
}
auto get_name() const -> std::string {
return "wyhash64";
}
};
class my_BobHash {
uint64_t seed1, seed2;
public:
my_BobHash() {
seed1 = random();
seed2 = random();
}
inline uint64_t operator()(uint32_t s) const {
uint32_t out1 = seed1, out2 = seed2;
void BobHash(const void *buf, size_t length, uint32_t *idx1, uint32_t *idx2);
BobJenkins::BobHash((void *) &s, 4, &out1, &out2);
return ((uint64_t) out1 << 32ul) | ((uint64_t) out2);
// return BobJenkins::BobHash((void *) &s, 4, seed);
}
// inline uint64_t operator()(uint64_t s) const {
// return BobJenkins::BobHash((void *) &s, 8, seed);
// }
auto get_name() const -> std::string {
return "BobHash";
}
};
inline uint32_t hashint(uint32_t a) {
a = (a + 0x7ed55d16) + (a << 12);
a = (a ^ 0xc761c23c) ^ (a >> 19);
a = (a + 0x165667b1) + (a << 5);
a = (a + 0xd3a2646c) ^ (a << 9);
a = (a + 0xfd7046c5) + (a << 3);
a = (a ^ 0xb55a4f09) ^ (a >> 16);
return a;
}
inline uint32_t hashint(uint64_t a) {
a = (a + 0x7ed55d16) + (a << 12);
a = (a ^ 0xc761c23c) ^ (a >> 19);
a = (a + 0x165667b1) + (a << 5);
a = (a + 0xd3a2646c) ^ (a << 9);
a = (a + 0xfd7046c5) + (a << 3);
a = (a ^ 0xb55a4f09) ^ (a >> 16);
return a;
}
}// namespace hashing
#endif// CUCKOO_FILTER_HASHUTIL_H_