-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloom_Filter.cpp
More file actions
59 lines (47 loc) · 1.38 KB
/
Bloom_Filter.cpp
File metadata and controls
59 lines (47 loc) · 1.38 KB
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
#include <bits/stdc++.h>
using namespace std;
class BloomFilter {
private:
vector<unsigned long long> points;
int size;
std::hash<string> h;
int hashFunction(const string &value, int multiplier) const {
size_t baseHash = h(value);
return int((baseHash * multiplier) % size);
}
void mark(int h) {
h %= size;
points[h >> 6] |= (1ULL << (h & 63));
}
bool isMarked(int h) const {
h %= size;
return (points[h >> 6] & (1ULL << (h & 63))) != 0;
}
public:
BloomFilter(int s) {
size = s;
points.resize((size + 63) >> 6, 0);
}
void add(const string& value) {
mark(hashFunction(value, 29));
mark(hashFunction(value, 19));
mark(hashFunction(value, 17));
}
bool exists(const string& value) const {
return isMarked(hashFunction(value, 29)) &&
isMarked(hashFunction(value, 19)) &&
isMarked(hashFunction(value, 17));
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
BloomFilter bf(1000);
bf.add("141432");
bf.add("2354364");
cout << boolalpha;
cout << bf.exists("141432") << endl; // true
cout << bf.exists("723") << endl; // false (maybe true in rare case of collision)
cout << bf.exists("1432") << endl; // false (but bloom filter allows false positives)
return 0;
}