Skip to content

Commit bd8d81b

Browse files
v0.6
1 parent 091e26f commit bd8d81b

File tree

3 files changed

+90
-30
lines changed

3 files changed

+90
-30
lines changed

Lesson03/Activity02/Activity2.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#include <openssl/md5.h>
5+
6+
class BloomFilter
7+
{
8+
int nHashes;
9+
std::vector<bool> bits;
10+
11+
static constexpr int hashSize = 128 / 8;
12+
13+
unsigned char hashValue[hashSize];
14+
15+
public:
16+
BloomFilter(int size, int hashes) : bits(size), nHashes(hashes)
17+
{
18+
if (nHashes > hashSize)
19+
{
20+
throw("Number of hash functions too high");
21+
}
22+
23+
if (size > 255)
24+
{
25+
throw("Size of bloom filter can't be >255");
26+
}
27+
}
28+
29+
void hash(const std::string &key)
30+
{
31+
MD5(reinterpret_cast<const unsigned char *>(key.data()), key.length(), hashValue);
32+
}
33+
34+
void add(const std::string &key)
35+
{
36+
hash(key);
37+
for (auto it = &hashValue[0]; it < &hashValue[nHashes]; it++)
38+
{
39+
bits[*it] = true;
40+
}
41+
std::cout << key << " added in bloom filter." << std::endl;
42+
}
43+
44+
bool mayContain(const std::string &key)
45+
{
46+
hash(key);
47+
for (auto it = &hashValue[0]; it < &hashValue[nHashes]; it++)
48+
{
49+
if (!bits[*it])
50+
{
51+
std::cout << key << " email can by used." << std::endl;
52+
return false;
53+
}
54+
}
55+
56+
std::cout << key << " email is used by someone else." << std::endl;
57+
return true;
58+
}
59+
};
60+
61+
int main()
62+
{
63+
BloomFilter bloom(10, 15);
64+
65+
bloom.add("[email protected]");
66+
bloom.add("[email protected]");
67+
68+
bloom.mayContain("abc");
69+
bloom.mayContain("[email protected]");
70+
bloom.mayContain("xyz");
71+
72+
bloom.add("[email protected]");
73+
bloom.add("[email protected]");
74+
75+
bloom.mayContain("abcd");
76+
bloom.mayContain("[email protected]");
77+
}

Lesson03/Exercise01/1.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class hash_map
3838

3939
int main()
4040
{
41-
hash_map map(100); // Let’s go with 100 as its easier for calculation.
41+
hash_map map(7);
4242

4343
auto print = [&](int value) {
4444
if (map.find(value))
@@ -48,22 +48,14 @@ int main()
4848
std::cout << std::endl;
4949
};
5050

51-
map.insert(3135); // Inserts at position 35
52-
map.insert(3); // Inserts at position 3
53-
map.insert(34); // Inserts at position 34
51+
map.insert(2);
52+
map.insert(25);
53+
map.insert(290);
5454

55-
print(35);
56-
print(34);
57-
print(10);
58-
59-
map.insert(55);
60-
map.erase(155);
61-
print(55);
62-
map.erase(55);
63-
print(55);
55+
print(25);
56+
print(100);
6457

6558
map.insert(100);
66-
map.insert(0);
67-
print(0);
6859
print(100);
60+
map.erase(25);
6961
}

Lesson03/Exercise02/2.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class hash_map
4444

4545
int main()
4646
{
47-
hash_map map(100); // Let’s go with 100 as its easier for calculation.
47+
hash_map map(7);
4848

4949
auto print = [&](int value) {
5050
if (map.find(value))
@@ -54,22 +54,13 @@ int main()
5454
std::cout << std::endl;
5555
};
5656

57-
map.insert(3135); // Inserts at position 35
58-
map.insert(3); // Inserts at position 3
59-
map.insert(34); // Inserts at position 34
60-
61-
print(35);
62-
print(34);
63-
print(10);
57+
map.insert(2);
58+
map.insert(25);
59+
map.insert(290);
6460

61+
map.insert(100);
6562
map.insert(55);
66-
map.erase(155);
67-
print(55);
68-
map.erase(55);
69-
print(55);
7063

71-
map.insert(100);
72-
map.insert(0);
73-
print(0);
7464
print(100);
65+
map.erase(2);
7566
}

0 commit comments

Comments
 (0)