C++ Bloom Filter Library  release
bloom_filter_example01.cpp
Go to the documentation of this file.
1 /*
2  *********************************************************************
3  * *
4  * Open Bloom Filter *
5  * *
6  * Description: Basic Bloom Filter Usage *
7  * Author: Arash Partow - 2000 *
8  * URL: http://www.partow.net *
9  * URL: http://www.partow.net/programming/hashfunctions/index.html *
10  * *
11  * Copyright notice: *
12  * Free use of the Open Bloom Filter Library is permitted under the *
13  * guidelines and in accordance with the MIT License. *
14  * http://www.opensource.org/licenses/MIT *
15  * *
16  *********************************************************************
17 */
18 
19 
20 /*
21  Description: This example demonstrates basic usage of the Bloom filter.
22  Initially some values are inserted then they are subsequently
23  queried, noting any false positives or errors.
24 */
25 
26 
27 #include <iostream>
28 #include <string>
29 
30 #include "bloom_filter.hpp"
31 
32 int main()
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
void insert(const unsigned char *key_begin, const std::size_t &length)
double false_positive_probability
int main()
virtual bool contains(const unsigned char *key_begin, const std::size_t length) const
unsigned long long int projected_element_count