Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
bloom_filter.cpp File Reference

Bloom Filter generic implementation in C++ More...

#include <cassert>
#include <functional>
#include <initializer_list>
#include <string>
#include <vector>
#include <iostream>
Include dependency graph for bloom_filter.cpp:

Classes

class  data_structures::Bitset
 Simple bitset implementation for bloom filter. More...
 
class  data_structures::BloomFilter< T >
 Bloom filter template class. More...
 

Namespaces

namespace  data_structures
 for IO operations
 

Functions

static std::size_t data_structures::hashDJB2 (std::string const &s)
 Function djb2 to get hash for the given string.
 
static std::size_t data_structures::hashStr (std::string const &s)
 Hash function, to get hash for the given string.
 
std::size_t data_structures::hashInt_1 (int x)
 Hash function for test
 
std::size_t data_structures::hashInt_2 (int x)
 Hash function for test
 
static void test_bloom_filter_string ()
 Test for bloom filter with string as generic type.
 
static void test_bloom_filter_int ()
 Test for bloom filter with int as generic type.
 
static void test_bitset ()
 Test for bitset.
 
int main ()
 Main function.
 

Detailed Description

Bloom Filter generic implementation in C++

A Bloom filter is a space-efficient probabilistic data structure, a query returns either "possibly in set" or "definitely not in set".

More generally, fewer than 10 bits per element are required for a 1% false positive probability, independent of the size or number of elements in the set.

It helps us to not make an "expensive operations", like disk IO - we can use bloom filter to check incoming request, and with a good probability get an answer of bloom filter, that we don't need to make our "expensive operation"

Very good use case example

Basic bloom filter doesn't support deleting of elements, so we don't need to implement deletion in bloom filter and bitset in our case.

Author
DanArmor

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
282 {
283 // run self-test implementations
284
285 test_bitset(); // run test for bitset, because bloom filter is depending on it
288
289 std::cout << "All tests have successfully passed!\n";
290 return 0;
291}
static void test_bloom_filter_int()
Test for bloom filter with int as generic type.
Definition bloom_filter.cpp:246
static void test_bitset()
Test for bitset.
Definition bloom_filter.cpp:267
static void test_bloom_filter_string()
Test for bloom filter with string as generic type.
Definition bloom_filter.cpp:226
Here is the call graph for this function:

◆ test_bitset()

static void test_bitset ( )
static

Test for bitset.

Returns
void
267 {
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}
Simple bitset implementation for bloom filter.
Definition bloom_filter.cpp:40
Here is the call graph for this function:

◆ test_bloom_filter_int()

static void test_bloom_filter_int ( )
static

Test for bloom filter with int as generic type.

Returns
void
246 {
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}
Bloom filter template class.
Definition bloom_filter.cpp:99
std::size_t hashInt_2(int x)
Hash function for test
Definition bloom_filter.cpp:213
std::size_t hashInt_1(int x)
Hash function for test
Definition bloom_filter.cpp:199
Here is the call graph for this function:

◆ test_bloom_filter_string()

static void test_bloom_filter_string ( )
static

Test for bloom filter with string as generic type.

Returns
void
226 {
229 std::vector<std::string> toCheck{"hello", "world", "!"};
230 std::vector<std::string> toFalse{"false", "world2", "!!!"};
231 for (auto& x : toCheck) {
232 filter.add(x);
233 }
234 for (auto& x : toFalse) {
235 assert(filter.contains(x) == false);
236 }
237 for (auto& x : toCheck) {
238 assert(filter.contains(x));
239 }
240}
static std::size_t hashDJB2(std::string const &s)
Function djb2 to get hash for the given string.
Definition bloom_filter.cpp:166
static std::size_t hashStr(std::string const &s)
Hash function, to get hash for the given string.
Definition bloom_filter.cpp:182
Here is the call graph for this function: