-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.cpp
151 lines (121 loc) · 3.09 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Name: Anurag Kulkarni
Div: SY IC B
Batch: B1
Roll no.: 21
Gr No.: 11910287
*/
#include <iostream>
#include <cmath>
#define MAX 100
using namespace std;
int hash1(string name){
int hash = 0;
for(int i=0; i<name.size(); i++){
//cout << name[i] << endl;
hash = (hash + (int)name[i]) % MAX;
}
//cout << "Final Hash 1: " << hash << endl;
return hash;
}
int hash2(string name){
int hash = 3, hk;
for(int i=0; i<name.size(); i++){
//cout << name[i] << endl;
hk = 23 - ((int)name[i] % 23);
hash = (hash + hk) % MAX;
}
//cout << "Final Hash 2: " << hash << endl;
return hash;
}
int hash3(string name){
int hash = 23, hk;
for(int i=0; i<name.size(); i++){
hk = 71 * ((int)name[i] % 71);
hash = (hash + hk) % MAX;
}
//cout << "Final Hash 3: " << hash << endl;
return hash;
}
int hash4(string name){
int hash = 3;
int p = 7;
for (int i = 0; i < name.size(); i++) {
hash += hash * 7 + name[i] * pow(p, i);
hash = hash % MAX;
}
//cout << "Final Hash 4: " << hash << endl;
return hash;
}
void display(bool set[]){
cout << "\nWelcome to display!" << endl;
for(int i=0; i<MAX; i++){
cout << i << "\t" << set[i] << endl;
}
}
bool lookup(bool set[], string name){
int one = hash1(name);
int two = hash2(name);
int three = hash3(name);
int four = hash4(name);
if(set[one] && set[two] && set[three] && set[four]){
return 1;
} else {
return 0;
}
}
void search(bool set[]){
cout << "\nWelcome to search!" << endl;
string name;
cout << "\nEnter your name: ";
cin >> name;
if(lookup(set, name)){
cout << "\nUsername " << name << " Found in the DataSet!" << endl;
} else {
cout << "\nUsername " << name << " not present in the DataSet!" << endl;
}
}
void insert(bool set[]){
cout << "\nWelcome to insert!" << endl;
string name;
cout << "\nEnter your name: ";
cin >> name;
cout << "Hello, " << name << endl;
if(lookup(set, name)){
cout << "\nUsername already present!" << endl;
} else {
int one = hash1(name);
int two = hash2(name);
int three = hash3(name);
int four = hash4(name);
set[one] = 1;
set[two] = 1;
set[three] = 1;
set[four] = 1;
cout << "\nUsername " << name << " Inserted!" << endl;
}
}
int main()
{
cout << "Welcome to Bloom Filter Program!" << endl;
bool dataSet[MAX] = {0};
int choice;
do{
cout << "\n1.Search\n2.Insert\n3.Display\n4.Exit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice){
case 1:
search(dataSet);
break;
case 2:
insert(dataSet);
break;
case 3:
display(dataSet);
break;
}
} while(choice!=4);
cout << "\nExiting Program!" << endl;
return 0;
}