TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
bloom_filter.cpp
Go to the documentation of this file.
1
24
25
#include <cassert>
26
#include <functional>
27
#include <initializer_list>
28
#include <string>
29
#include <vector>
30
#include <iostream>
31
36
namespace
data_structures
{
40
class
Bitset
{
41
private
:
42
std::vector<std::size_t>
data
;
43
static
const
std::size_t
blockSize
=
44
sizeof
(std::size_t);
46
public
:
47
explicit
Bitset
(std::size_t);
48
std::size_t
size
();
49
void
add
(std::size_t);
50
bool
contains
(std::size_t);
51
};
52
57
std::size_t
Bitset::size
() {
return
data
.size(); }
58
63
Bitset::Bitset
(std::size_t initSize) :
data
(initSize) {}
64
71
void
Bitset::add
(std::size_t x) {
72
std::size_t blockIndex = x /
blockSize
;
73
if
(blockIndex >=
data
.size()) {
74
data
.resize(blockIndex + 1);
75
}
76
data
[blockIndex] |= 1 << (x %
blockSize
);
77
}
78
86
bool
Bitset::contains
(std::size_t x) {
87
std::size_t blockIndex = x /
blockSize
;
88
if
(blockIndex >=
data
.size()) {
89
return
false
;
90
}
91
return
data
[blockIndex] & (1 << (x %
blockSize
));
92
}
93
98
template
<
typename
T>
99
class
BloomFilter
{
100
private
:
101
Bitset
set
;
102
std::vector<std::function<std::size_t(T)>>
103
hashFunks
;
104
105
public
:
106
BloomFilter
(std::size_t,
107
std::initializer_list<std::function<std::size_t(T)>>);
108
void
add
(T);
109
bool
contains
(T);
110
};
111
120
template
<
typename
T>
121
BloomFilter<T>::BloomFilter
(
122
std::size_t size,
123
std::initializer_list<std::function<std::size_t(T)>> funks)
124
:
set
(size),
hashFunks
(funks) {}
125
133
template
<
typename
T>
134
void
BloomFilter<T>::add
(T x) {
135
for
(std::size_t i = 0; i <
hashFunks
.size(); i++) {
136
set
.
add
(
hashFunks
[i](x) % (
sizeof
(std::size_t) *
set
.
size
()));
137
}
138
}
139
148
template
<
typename
T>
149
bool
BloomFilter<T>::contains
(T x) {
150
for
(std::size_t i = 0; i <
hashFunks
.size(); i++) {
151
if
(
set
.
contains
(
hashFunks
[i](x) %
152
(
sizeof
(std::size_t) *
set
.
size
())) ==
false
) {
153
return
false
;
154
}
155
}
156
return
true
;
157
}
158
166
static
std::size_t
hashDJB2
(std::string
const
& s) {
167
std::size_t hash = 5381;
168
for
(
char
c : s) {
169
hash = ((hash << 5) + hash) + c;
170
}
171
return
hash;
172
}
173
182
static
std::size_t
hashStr
(std::string
const
& s) {
183
std::size_t hash = 37;
184
std::size_t primeNum1 = 54059;
185
std::size_t primeNum2 = 76963;
186
for
(
char
c : s) {
187
hash = (hash * primeNum1) ^ (c * primeNum2);
188
}
189
return
hash;
190
}
191
199
std::size_t
hashInt_1
(
int
x) {
200
x = ((x >> 16) ^ x) * 0x45d9f3b;
201
x = ((x >> 16) ^ x) * 0x45d9f3b;
202
x = (x >> 16) ^ x;
203
return
x;
204
}
205
213
std::size_t
hashInt_2
(
int
x) {
214
auto
y =
static_cast<
std::size_t
>
(x);
215
y = (y ^ (y >> 30)) *
static_cast<
std::size_t
>
(0xbf58476d1ce4e5b9);
216
y = (y ^ (y >> 27)) *
static_cast<
std::size_t
>
(0x94d049bb133111eb);
217
y = y ^ (y >> 31);
218
return
y;
219
}
220
}
// namespace data_structures
221
226
static
void
test_bloom_filter_string
() {
227
data_structures::BloomFilter<std::string>
filter(
228
10, {
data_structures::hashDJB2
,
data_structures::hashStr
});
229
std::vector<std::string> toCheck{
"hello"
,
"world"
,
"!"
};
230
std::vector<std::string> toFalse{
"false"
,
"world2"
,
"!!!"
};
231
for
(
const
auto
& x : toCheck) {
232
filter.add(x);
233
}
234
for
(
const
auto
& x : toFalse) {
235
assert(filter.contains(x) ==
false
);
236
}
237
for
(
const
auto
& x : toCheck) {
238
assert(filter.contains(x));
239
}
240
}
241
246
static
void
test_bloom_filter_int
() {
247
data_structures::BloomFilter<int>
filter(
248
20, {
data_structures::hashInt_1
,
data_structures::hashInt_2
});
249
std::vector<int> toCheck{100, 200, 300, 50};
250
std::vector<int> toFalse{1, 2, 3, 4, 5, 6, 7, 8};
251
for
(
int
x : toCheck) {
252
filter.add(x);
253
}
254
for
(
int
x : toFalse) {
255
assert(filter.contains(x) ==
false
);
256
}
257
for
(
int
x : toCheck) {
258
assert(filter.contains(x));
259
}
260
}
261
267
static
void
test_bitset
() {
268
data_structures::Bitset
set(2);
269
std::vector<std::size_t> toCheck{0, 1, 5, 8, 63, 64, 67, 127};
270
for
(
auto
x : toCheck) {
271
set.
add
(x);
272
assert(set.
contains
(x));
273
}
274
assert(set.
contains
(128) ==
false
);
275
assert(set.
contains
(256) ==
false
);
276
}
277
282
int
main
() {
283
// run self-test implementations
284
285
test_bitset
();
// run test for bitset, because bloom filter is depending on it
286
test_bloom_filter_string
();
287
test_bloom_filter_int
();
288
289
std::cout <<
"All tests have successfully passed!\n"
;
290
return
0;
291
}
test_bloom_filter_int
static void test_bloom_filter_int()
Test for bloom filter with int as generic type.
Definition
bloom_filter.cpp:246
test_bitset
static void test_bitset()
Test for bitset.
Definition
bloom_filter.cpp:267
test_bloom_filter_string
static void test_bloom_filter_string()
Test for bloom filter with string as generic type.
Definition
bloom_filter.cpp:226
data_structures::Bitset
Simple bitset implementation for bloom filter.
Definition
bloom_filter.cpp:40
data_structures::Bitset::Bitset
Bitset(std::size_t)
BitSet class constructor.
Definition
bloom_filter.cpp:63
data_structures::Bitset::add
void add(std::size_t)
Turn bit on position x to 1s.
Definition
bloom_filter.cpp:71
data_structures::Bitset::size
std::size_t size()
Utility function to return the size of the inner array.
Definition
bloom_filter.cpp:57
data_structures::Bitset::contains
bool contains(std::size_t)
Doest bitset contains element x.
Definition
bloom_filter.cpp:86
data_structures::Bitset::blockSize
static const std::size_t blockSize
Definition
bloom_filter.cpp:43
data_structures::Bitset::data
std::vector< std::size_t > data
short info of this variable
Definition
bloom_filter.cpp:42
data_structures::BloomFilter
Bloom filter template class.
Definition
bloom_filter.cpp:99
data_structures::BloomFilter::contains
bool contains(T)
Check element function for Bloom filter.
Definition
bloom_filter.cpp:149
data_structures::BloomFilter::hashFunks
std::vector< std::function< std::size_t(T)> > hashFunks
hash functions for T type
Definition
bloom_filter.cpp:103
data_structures::BloomFilter::add
void add(T)
Add function for Bloom filter.
Definition
bloom_filter.cpp:134
data_structures::BloomFilter::BloomFilter
BloomFilter(std::size_t, std::initializer_list< std::function< std::size_t(T)> >)
Constructor for Bloom filter.
Definition
bloom_filter.cpp:121
data_structures::BloomFilter::set
Bitset set
inner bitset for elements
Definition
bloom_filter.cpp:101
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures
for IO operations
data_structures::hashDJB2
static std::size_t hashDJB2(std::string const &s)
Function djb2 to get hash for the given string.
Definition
bloom_filter.cpp:166
data_structures::hashStr
static std::size_t hashStr(std::string const &s)
Hash function, to get hash for the given string.
Definition
bloom_filter.cpp:182
data_structures::hashInt_2
std::size_t hashInt_2(int x)
Hash function for test
Definition
bloom_filter.cpp:213
data_structures::hashInt_1
std::size_t hashInt_1(int x)
Hash function for test
Definition
bloom_filter.cpp:199
data_structures
bloom_filter.cpp
Generated by
1.18.0