-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBloomFilter.cpp
92 lines (64 loc) · 3.03 KB
/
BloomFilter.cpp
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
#include "BloomFilter.h"
#include "hash.h"
#define K 16
#define BYTE_IN_BITS 8
BloomFilter::BloomFilter(int size) {
this->BloomFilterSize = size;
this->BloomFilterArray = new char[this->BloomFilterSize];
memset(this->BloomFilterArray,'\0', BloomFilterSize); /* Clear the array and set all bits to 0 */
}
BloomFilter::BloomFilter(int size, char* bloomArray){
this->BloomFilterSize = size;
this->BloomFilterArray = new char[size];
memset(this->BloomFilterArray,'\0', size); /* Clear the array and set all bits to 0 */
/* bitwise or the 2 arrays */
for (int i = 0; i < size; i++)
this->BloomFilterArray[i] = (char)(bloomArray[i] | this->BloomFilterArray[i]);
}
void BloomFilter::BloomFilterMerge(char* bloomArray){
/* bitwise or the 2 arrays */
for (int i = 0; i < this->BloomFilterSize ; i++)
this->BloomFilterArray[i] = (char)(bloomArray[i] | this->BloomFilterArray[i]);
}
BloomFilter::~BloomFilter(){
delete BloomFilterArray;
}
int BloomFilter::BloomFilterInsert(string str){
int pos; /* position in the array [BloomFilter] */
int offset; /* find the bit to change in that position in [BloomFilter] */
unsigned long hashed[K]; /* An array with K hashed values */
hashedValues(str, K ,hashed);
for (int i = 0 ; i < K ; i++){
/* hashed values mod M = the number of bits in the array that we need to change */
hashed[i] = hashed[i] % (BloomFilterSize * BYTE_IN_BITS); /* mod the number of bits in the array */
}
/* Insert in BloomFilter */
for (int i = 0 ; i < K ; i++){
pos = hashed[i] / BYTE_IN_BITS; /* find the right byte */
offset = pos % BYTE_IN_BITS; /* find the right bit */
BloomFilterArray[pos] = (BloomFilterArray[pos] | (1 << offset));
}
return 1;
}
/* Return 1 if answer is "YES" or "MAYBE" and 0 if the answer is "NO" => It's not in the array */
int BloomFilter::BloomFilterSearch(string str){
int pos; /* position in the array [BloomFilter] */
int offset; /* find the bit to change in that position in [BloomFilter] */
unsigned long hashed[K]; /* An array with K hashed values */
hashedValues(str, K ,hashed);
for (int i = 0 ; i < K ; i++){
/* hashed values mod M = the number of bits in the array that we need to change */
hashed[i] = hashed[i] % (BloomFilterSize * BYTE_IN_BITS); /* mod the number of bits in the array */
}
/* Insert in BloomFilter */
for (int i = 0 ; i < K ; i++){
pos = hashed[i] / BYTE_IN_BITS; /* find the right byte */
offset = pos % BYTE_IN_BITS; /* find the right bit */
/* Check if bit is already 1 */
char c = (BloomFilterArray[pos] >> offset) & 1;
if (c != 1){ /* If bit is already set to 1 then go to the next iteration */
return 0;
}
}
return 1;
}