C++ Bloom Filter Library  release
Functions
bloom_filter_example01.cpp File Reference
#include <iostream>
#include <string>
#include "bloom_filter.hpp"
Include dependency graph for bloom_filter_example01.cpp:

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 32 of file bloom_filter_example01.cpp.

References bloom_parameters::compute_optimal_parameters(), bloom_filter::contains(), bloom_parameters::false_positive_probability, bloom_filter::insert(), bloom_parameters::projected_element_count, and bloom_parameters::random_seed.

33 {
34  bloom_parameters parameters;
35 
36  // How many elements roughly do we expect to insert?
37  parameters.projected_element_count = 1000;
38 
39  // Maximum tolerable false positive probability? (0,1)
40  parameters.false_positive_probability = 0.0001; // 1 in 10000
41 
42  // Simple randomizer (optional)
43  parameters.random_seed = 0xA5A5A5A5;
44 
45  if (!parameters)
46  {
47  std::cout << "Error - Invalid set of bloom filter parameters!" << std::endl;
48  return 1;
49  }
50 
51  parameters.compute_optimal_parameters();
52 
53  //Instantiate Bloom Filter
54  bloom_filter filter(parameters);
55 
56  std::string str_list[] = { "AbC", "iJk", "XYZ" };
57 
58  // Insert into Bloom Filter
59  {
60  // Insert some strings
61  for (std::size_t i = 0; i < (sizeof(str_list) / sizeof(std::string)); ++i)
62  {
63  filter.insert(str_list[i]);
64  }
65 
66  // Insert some numbers
67  for (std::size_t i = 0; i < 100; ++i)
68  {
69  filter.insert(i);
70  }
71  }
72 
73  // Query Bloom Filter
74  {
75  // Query the existence of strings
76  for (std::size_t i = 0; i < (sizeof(str_list) / sizeof(std::string)); ++i)
77  {
78  if (filter.contains(str_list[i]))
79  {
80  std::cout << "BF contains: " << str_list[i] << std::endl;
81  }
82  }
83 
84  // Query the existence of numbers
85  for (std::size_t i = 0; i < 100; ++i)
86  {
87  if (filter.contains(i))
88  {
89  std::cout << "BF contains: " << i << std::endl;
90  }
91  }
92 
93  std::string invalid_str_list[] = { "AbCX", "iJkX", "XYZX" };
94 
95  // Query the existence of invalid strings
96  for (std::size_t i = 0; i < (sizeof(invalid_str_list) / sizeof(std::string)); ++i)
97  {
98  if (filter.contains(invalid_str_list[i]))
99  {
100  std::cout << "BF falsely contains: " << invalid_str_list[i] << std::endl;
101  }
102  }
103 
104  // Query the existence of invalid numbers
105  for (int i = -1; i > -100; --i)
106  {
107  if (filter.contains(i))
108  {
109  std::cout << "BF falsely contains: " << i << std::endl;
110  }
111  }
112  }
113 
114  return 0;
115 }
virtual bool compute_optimal_parameters()
unsigned long long int random_seed
double false_positive_probability
unsigned long long int projected_element_count
Here is the call graph for this function: