-
Notifications
You must be signed in to change notification settings - Fork 23
/
bloom.h
337 lines (296 loc) · 9.82 KB
/
bloom.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef BLOOM_FILTER_BLOOM_FILTER_H_
#define BLOOM_FILTER_BLOOM_FILTER_H_
#include <algorithm>
#include <assert.h>
#include <sstream>
#include <climits>
#include "hashutil.h"
using namespace std;
using namespace hashing;
namespace bloomfilter {
// status returned by a Bloom filter operation
enum Status {
Ok = 0,
NotFound = 1,
NotEnoughSpace = 2,
NotSupported = 3,
};
inline uint32_t reduce(uint32_t hash, uint32_t n) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return (uint32_t)(((uint64_t)hash * n) >> 32);
}
/**
* Given a value "word", produces an integer in [0,p) without division.
* The function is as fair as possible in the sense that if you iterate
* through all possible values of "word", then you will generate all
* possible outputs as uniformly as possible.
*/
static inline uint32_t fastrange32(uint32_t word, uint32_t p) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return (uint32_t)(((uint64_t)word * (uint64_t)p) >> 32);
}
#if defined(_MSC_VER) && defined (_WIN64)
#include <intrin.h>// should be part of all recent Visual Studio
#pragma intrinsic(_umul128)
#endif // defined(_MSC_VER) && defined (_WIN64)
/**
* Given a value "word", produces an integer in [0,p) without division.
* The function is as fair as possible in the sense that if you iterate
* through all possible values of "word", then you will generate all
* possible outputs as uniformly as possible.
*/
static inline uint64_t fastrange64(uint64_t word, uint64_t p) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
#ifdef __SIZEOF_INT128__ // then we know we have a 128-bit int
return (uint64_t)(((__uint128_t)word * (__uint128_t)p) >> 64);
#elif defined(_MSC_VER) && defined(_WIN64)
// supported in Visual Studio 2005 and better
uint64_t highProduct;
_umul128(word, p, &highProduct); // ignore output
return highProduct;
unsigned __int64 _umul128(
unsigned __int64 Multiplier,
unsigned __int64 Multiplicand,
unsigned __int64 *HighProduct
);
#else
return word % p; // fallback
#endif // __SIZEOF_INT128__
}
#ifndef UINT32_MAX
#define UINT32_MAX (0xffffffff)
#endif // UINT32_MAX
/**
* Given a value "word", produces an integer in [0,p) without division.
* The function is as fair as possible in the sense that if you iterate
* through all possible values of "word", then you will generate all
* possible outputs as uniformly as possible.
*/
static inline size_t fastrangesize(uint64_t word, size_t p) {
#if (SIZE_MAX == UINT32_MAX)
return (size_t)fastrange32(word, p);
#else // assume 64-bit
return (size_t)fastrange64(word, p);
#endif // SIZE_MAX == UINT32_MAX
}
static inline size_t getBestK(size_t bitsPerItem) {
return max(1, (int)round((double)bitsPerItem * log(2)));
}
inline uint64_t getBit(uint32_t index) { return 1L << (index & 63); }
template <typename ItemType, size_t bits_per_item, bool branchless,
typename HashFamily = SimpleMixSplit,
int k = (int)((double)bits_per_item * 0.693147180559945 + 0.5)>
class BloomFilter {
public:
uint64_t *data;
size_t size;
size_t arrayLength;
size_t bitCount;
int kk;
HashFamily hasher;
double BitsPerItem() const { return k; }
explicit BloomFilter(const size_t n) : hasher() {
this->size = 0;
this->kk = getBestK(bits_per_item);
this->bitCount = n * bits_per_item;
this->arrayLength = (bitCount + 63) / 64;
data = new uint64_t[arrayLength];
std::fill_n(data, arrayLength, 0);
}
~BloomFilter() { delete[] data; }
// Add an item to the filter.
Status Add(const ItemType &item);
// Add multiple items to the filter.
Status AddAll(const vector<ItemType>& data, const size_t start,
const size_t end) {
return AddAll(data.data(),start,end);
}
Status AddAll(const ItemType* data, const size_t start,
const size_t end);
// Report if the item is inserted, with false positive rate.
Status Contain(const ItemType &item) const;
/* methods for providing stats */
// summary infomation
std::string Info() const;
// number of current inserted items;
size_t Size() const { return size; }
// size of the filter in bytes.
size_t SizeInBytes() const { return arrayLength * 8; }
};
template <typename ItemType, size_t bits_per_item, bool branchless,
typename HashFamily, int k>
Status BloomFilter<ItemType, bits_per_item, branchless, HashFamily, k>::Add(
const ItemType &key) {
uint64_t hash = hasher(key);
uint64_t a = (hash >> 32) | (hash << 32);
uint64_t b = hash;
for (int i = 0; i < k; i++) {
// int index = reduce(a, this->bitCount);
// data[index >> 6] |= getBit(index);
// reworked to avoid overflows
// use the fact that reduce is not very sensitive to lower bits of a
data[fastrangesize(a, this->arrayLength)] |= getBit(a);
a += b;
}
return Ok;
}
const int blockShift = 15;
const int blockLen = 1 << blockShift;
void applyBlock(uint32_t *tmp, int block, int len, uint64_t *data) {
for (int i = 0; i < len; i++) {
uint32_t index = tmp[(block << blockShift) + i];
data[index >> 6] |= getBit(index);
}
}
template <typename ItemType, size_t bits_per_item, bool branchless,
typename HashFamily, int k>
Status BloomFilter<ItemType, bits_per_item, branchless, HashFamily, k>::AddAll(
const ItemType* keys, const size_t start, const size_t end) {
// we have that AddAll assumes that arrayLength << 6 is a
// 32-bit integer
if(arrayLength > 0x3ffffff) {
for(size_t i = start; i < end; i++) {
Add(keys[i]);
}
return Ok;
}
int blocks = 1 + arrayLength / blockLen;
uint32_t *tmp = new uint32_t[blocks * blockLen];
int *tmpLen = new int[blocks]();
for (size_t i = start; i < end; i++) {
uint64_t key = keys[i];
uint64_t hash = hasher(key);
uint64_t a = (hash >> 32) | (hash << 32);
uint64_t b = hash;
for (int j = 0; j < k; j++) {
int index = fastrangesize(a, this->arrayLength);
int block = index >> blockShift;
int len = tmpLen[block];
tmp[(block << blockShift) + len] = (index << 6) + (a & 63);
tmpLen[block] = len + 1;
if (len + 1 == blockLen) {
applyBlock(tmp, block, len + 1, data);
tmpLen[block] = 0;
}
a += b;
}
}
for (int block = 0; block < blocks; block++) {
applyBlock(tmp, block, tmpLen[block], data);
}
delete[] tmp;
delete[] tmpLen;
return Ok;
}
char bittest64(const uint64_t *t, uint64_t bit) {
return (*t & (1L << (bit & 63))) != 0;
}
template <typename ItemType, size_t bits_per_item, bool branchless,
typename HashFamily, int k>
Status BloomFilter<ItemType, bits_per_item, branchless, HashFamily, k>::Contain(
const ItemType &key) const {
uint64_t hash = hasher(key);
uint64_t a = (hash >> 32) | (hash << 32);
uint64_t b = hash;
if (branchless && k >= 3) {
int b0 = data[fastrangesize(a, this->arrayLength)] >> (a & 63);
a += b;
int b1 = data[fastrangesize(a, this->arrayLength)] >> (a & 63);
a += b;
int b2 = data[fastrangesize(a, this->arrayLength)] >> (a & 63);
if ((b0 & b1 & b2 & 1) == 0) {
return NotFound;
}
for (int i = 3; i < k; i++) {
a += b;
if (((data[fastrangesize(a, this->arrayLength)] >> (a & 63)) & 1) == 0) {
return NotFound;
}
}
return Ok;
}
for (int i = 0; i < k; i++) {
if ((data[fastrangesize(a, this->arrayLength)] & getBit(a)) == 0) {
return NotFound;
}
a += b;
}
return Ok;
}
template <typename ItemType, size_t bits_per_item, bool branchless,
typename HashFamily, int k>
std::string
BloomFilter<ItemType, bits_per_item, branchless, HashFamily, k>::Info() const {
std::stringstream ss;
ss << "BloomFilter Status:\n"
<< "\t\tKeys stored: " << Size() << "\n";
if (Size() > 0) {
ss << "\t\tk: " << BitsPerItem() << "\n";
} else {
ss << "\t\tk: N/A\n";
}
return ss.str();
}
/***************
* Simple block filter (naive implementation)
***************/
template <size_t blocksize, int k,
typename HashFamily = ::hashing::SimpleMixSplit>
class SimpleBlockFilter {
private:
const size_t arrayLength;
uint64_t* data;
HashFamily hasher_;
public:
// Consumes at most (1 << log_heap_space) bytes on the heap:
explicit SimpleBlockFilter(const int bits);
~SimpleBlockFilter() noexcept;
void Add(const uint64_t key) noexcept;
bool Find(const uint64_t key) const noexcept;
uint64_t SizeInBytes() const {
return arrayLength * 8;
}
};
template <size_t blocksize, int k, typename HashFamily>
SimpleBlockFilter<blocksize, k, HashFamily>::SimpleBlockFilter(
const int capacity)
: arrayLength((capacity * 10) / 64 + 8),
hasher_() {
data = new uint64_t[arrayLength]();
}
template <size_t blocksize, int k, typename HashFamily>
SimpleBlockFilter<blocksize, k, HashFamily>::~SimpleBlockFilter() noexcept {
delete[] data;
data = nullptr;
}
static inline uint64_t rotl64(uint64_t n, unsigned int c) {
// assumes width is a power of 2
const unsigned int mask = (CHAR_BIT * sizeof(n) - 1);
c &= mask;
return (n << c) | (n >> ((-c) & mask));
}
template <size_t blocksize, int k, typename HashFamily>
inline void
SimpleBlockFilter<blocksize, k, HashFamily>::Add(const uint64_t key) noexcept {
const auto hash = hasher_(key);
const uint32_t idx = reduce(hash, arrayLength);
uint64_t *bucket = data + idx;
uint64_t m1 = 1L << hash;
uint64_t m2 = 1L << (hash >> 8);
uint64_t m = m1 | m2;
*bucket |= m;
}
template <size_t blocksize, int k, typename HashFamily>
inline bool
SimpleBlockFilter<blocksize, k, HashFamily>::Find(const uint64_t key) const
noexcept {
const auto hash = hasher_(key);
const uint32_t idx = reduce(hash, arrayLength);
uint64_t *bucket = data + idx;
uint64_t m1 = 1L << hash;
uint64_t m2 = 1L << (hash >> 8);
uint64_t m = m1 | m2;
return !((m & *bucket) - m);
}
} // namespace bloomfilter
#endif // BLOOM_FILTER_BLOOM_FILTER_H_