-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bloom_Filter.cpp
112 lines (91 loc) · 2.07 KB
/
Bloom_Filter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
// Simple Bloom Filter implementation with
// k = 2(hash functions) and m=5(bloom filter's size)
class BloomFilter
{
private:
int SIZE;
int bloom[5];
public:
BloomFilter()
{
SIZE = 5;
for (int i = 0; i < SIZE; i++)
bloom[i] = 0;
}
int hash1(int data)
{
return (data % SIZE);
}
int hash2(int data)
{
return (((2 * data) + 3) % SIZE);
}
void insertElements(int data)
{
int index1 = hash1(data);
int index2 = hash2(data);
bloom[index1] = 1;
bloom[index2] = 1;
}
bool checkElement(int data)
{
int index1 = hash1(data);
int index2 = hash2(data);
if ((bloom[index1] == 0) || (bloom[index2] == 0))
return true;
else
return false;
}
void displayFilter()
{
cout << "\nBloom Filter formed is : " << endl;
cout << "INDEX BIT" << endl;
for (int i = 0; i < SIZE; i++)
cout << i << " ---> " << bloom[i] << endl;
}
};
int main()
{
BloomFilter b;
int choice, data;
while (1)
{
cout << "\n1.Insert \t 2.Check Element \t 3.Display Bloom Filter \t 4.EXIT" << endl;
cout << "Enter choice : ";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "\nEnter element : ";
cin >> data;
b.insertElements(data);
break;
}
case 2:
{
cout << "\nEnter element : ";
cin >> data;
bool result = b.checkElement(data);
if (result == true)
cout << "Element is not present" << endl;
else
cout << "Element may be present (FALSE POSITIVE)" << endl;
break;
}
case 3:
{
b.displayFilter();
break;
}
case 4:
exit(0);
break;
default:
cout << "\nINVALID CHOICE\n";
}
}
return 0;
}