-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.cpp
107 lines (98 loc) · 2.53 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
#include "bloom_filter.h"
bloom_filter& bloom_filter::operator&=(const bloom_filter& filter)
{
/* intersection */
if (
(salt_count == filter.salt_count) &&
(table_size == filter.table_size) &&
(random_seed == filter.random_seed)
)
{
for (std::size_t i = 0; i < (table_size / char_size); i++)
{
hash_table[i] &= filter.hash_table[i];
}
}
return *this;
}
bloom_filter& bloom_filter::operator|=(const bloom_filter& filter)
{
/* union */
if (
(salt_count == filter.salt_count) &&
(table_size == filter.table_size) &&
(random_seed == filter.random_seed)
)
{
for (std::size_t i = 0; i < (table_size / char_size); i++)
{
hash_table[i] |= filter.hash_table[i];
}
}
return *this;
}
bloom_filter& bloom_filter::operator^=(const bloom_filter& filter)
{
/* difference */
if (
(salt_count == filter.salt_count) &&
(table_size == filter.table_size) &&
(random_seed == filter.random_seed)
)
{
for (std::size_t i = 0; i < (table_size / char_size); i++)
{
hash_table[i] ^= filter.hash_table[i];
}
}
return *this;
}
std::string longIntToString(long int imgId) {
std::stringstream oss;
oss << imgId;
return oss.str();
}
void bloom_filter::generate_unique_salt()
{
srand(static_cast<unsigned int>(random_seed));
const std::size_t MAX_LENGTH = 5;
while(salt.size() < salt_count)
{
std::string current_salt = std::string(MAX_LENGTH,0x0);
for(std::string::iterator it = current_salt.begin(); it != current_salt.end(); ++it)
{
(*it) = static_cast<char>((256.0 * rand()) / RAND_MAX);
}
bool duplicate_found = false;
for(std::vector<std::string>::iterator it = salt.begin(); it != salt.end(); ++it)
{
if (current_salt == (*it))
{
duplicate_found = true;
break;
}
}
if (!duplicate_found)
{
salt.push_back(current_salt);
}
}
}
bloom_filter operator & (const bloom_filter& a, const bloom_filter& b)
{
bloom_filter result = a;
result &= b;
return result;
}
bloom_filter operator | (const bloom_filter& a, const bloom_filter& b)
{
bloom_filter result = a;
result |= b;
return result;
}
bloom_filter operator ^ (const bloom_filter& a, const bloom_filter& b)
{
bloom_filter result = a;
result ^= b;
return result;
}