-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBloomFilter.hpp
98 lines (81 loc) · 1.96 KB
/
BloomFilter.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
#ifndef __BLOOM_FILTER__
#define __BLOOM_FILTER__
#include <vector>
#include <cmath>
#include <functional>
template<class T>
class BloomFilter {
private:
std::vector<bool> array;
unsigned int size;
unsigned int nElements;
unsigned int hashFunction1(T s);
unsigned int hashFunction2(T s);
public:
BloomFilter(unsigned int size);
~BloomFilter();
unsigned int getSize();
float getFalseRate();
bool contains(T value);
void insert(T value);
void resize(unsigned int size);
void clear(unsigned int size);
//It would be nice to have a function to resize the filter to obtain the falseRate that we want
//unsigned int getFalseRate(float rate);
};
//Code
template<class T>
BloomFilter<T>::BloomFilter(unsigned int size) {
nElements = 0;
array.reserve(size);
array.assign(size,false);
this->size = size;
}
template<class T>
BloomFilter<T>::~BloomFilter() {
}
template<class T>
unsigned int BloomFilter<T>::getSize(){
return size;
}
template<class T>
float BloomFilter<T>::getFalseRate(){
//k = 2; m = size; n = nElements;
return std::pow(1-std::exp(-2 * ((float)nElements)/((float)size)),2);
}
template<class T>
bool BloomFilter<T>::contains(T value){
return array[hashFunction1(value)] && array[hashFunction2(value)];
}
template<class T>
void BloomFilter<T>::insert(T value){
array[hashFunction1(value)] = true;
array[hashFunction2(value)] = true;
nElements++;
}
//djb2
template<class T>
unsigned int BloomFilter<T>::hashFunction1(T value){
unsigned long hash = 5381;
for (auto c : value) {
hash = (hash << 5) + hash + c; /* hash * 33 + c */
}
return hash % size;
}
//STL Hash
template<class T>
unsigned int BloomFilter<T>::hashFunction2(T value){
return std::hash<T>{}(value) % size;
}
template<class T>
void BloomFilter<T>::resize(unsigned int size){
array.reserve(size);
array.assign(size,false);
this->size = size;
}
template<class T>
void BloomFilter<T>::clear(unsigned int size){
array.clear();
nElements = 0;
}
#endif