-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilter.cpp
More file actions
66 lines (57 loc) · 1.37 KB
/
BloomFilter.cpp
File metadata and controls
66 lines (57 loc) · 1.37 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
60
61
62
63
64
65
66
#include "BloomFilter.h"
#include "IntegerHashes.h"
#include "StringHashes.h"
#include <cstdint>
#include <string>
BloomFilter::BloomFilter(int k, int m, std::string strfn, std::string intfn){
this->k = k;
this->m = m;
// initialize bit array
this->bits = new uint64_t[m]();
// initialize intfns array
intfns = new IntegerHash*[k];
if(intfn=="division"){
for(int i = 0; i<k; i++){
intfns[i] = new DivisionHash(i,m);
}
} else if (intfn=="reciprocal"){
for(int i =0; i<k;i++){
intfns[i] = new ReciprocalHash(i, m);
}
} else {
for(int i =0; i<k;i++){
intfns[i] = new SquareRootHash(i, m);
}
}
// initialize strfn
if(strfn=="pearson"){
this->strfn = new PearsonHash();
} else {
this->strfn = new JenkinsHash();
}
}
BloomFilter::~BloomFilter(){
delete [] bits;
delete strfn;
for(int i=0; i<k; i++){
delete intfns[i];
}
delete [] intfns;
}
void BloomFilter::insert(const std::string& value){
uint64_t key;
for(int i = 0; i < k; i++){
key = intfns[i] -> hash(strfn -> hash(value));
bits[key/64] |= (uint64_t(1) << (key % 64));
}
}
bool BloomFilter::lookup(const std::string& value) const{
for(int i=0; i<k; i++){
uint64_t key;
key = intfns[i] -> hash(strfn -> hash(value));
if((bits[key/64] & (uint64_t(1) << (key % 64))) == 0){
return false;
}
}
return true;
}