-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloomFilters.cpp
73 lines (72 loc) · 1.94 KB
/
bloomFilters.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
#include <iostream>
#include <string>
using namespace std;
#define ll long long
int hash1(string s, int arrSize)
{
ll h = 0;
for (int i = 0; i < s.size(); i++)
{
h += (int)s[i];
h = h % arrSize;
}
return h;
}
int hash2(string s, int arrSize)
{
ll h = 4;
for (int i = 0; i < s.size(); i++)
{
h *= ((int)s[i] + 1) + 42432;
h = h % arrSize;
}
return h;
}
bool lookup(bool *bitarr, int arrSize, string s)
{
int a = hash1(s, arrSize);
int b = hash2(s, arrSize);
if (bitarr[a] && bitarr[b])
return true;
return false;
}
void insert(bool *bitarr, int arrSize, string s)
{
if (lookup(bitarr, arrSize, s))
{
cout << s << " is probably already present\n";
}
else
{
int a = hash1(s, arrSize);
int b = hash2(s, arrSize);
bitarr[a] = true;
bitarr[b] = true;
cout << s << " is inserted\n";
}
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
bool bitarr[100] = {false};
int arrSize{100};
string sarray[33] = {"abound", "abounds", "abundance",
"abundant", "accessible", "bloom",
"blossom", "bolster", "bonny",
"bonus", "bonuses", "coherent",
"cohesive", "colorful", "comely",
"comfort", "gems", "generosity",
"generous", "generously", "genial",
"bluff", "cheater", "hate",
"war", "humanity", "racism",
"hurt", "nuke", "gloomy",
"facebook", "geeksforgeeks", "twitter"};
for (int i = 0; i < 33; i++)
{
insert(bitarr, arrSize, sarray[i]);
}
lookup(bitarr, arrSize, "abound") ? cout << "Present\n" : cout << "Not Present\n";
return 0;
}