bloom-filter.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The University of Memphis
4  *
5  * This file is part of PSync.
6  * See AUTHORS.md for complete list of PSync authors and contributors.
7  *
8  * PSync is free software: you can redistribute it and/or modify it under the terms
9  * of the GNU Lesser General Public License as published by the Free Software Foundation,
10  * either version 3 of the License, or (at your option) any later version.
11  *
12  * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  * PURPOSE. See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License along with
17  * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18  *
19 
20  * This file incorporates work covered by the following copyright and
21  * permission notice:
22 
23  * The MIT License (MIT)
24 
25  * Copyright (c) 2000 Arash Partow
26 
27  * Permission is hereby granted, free of charge, to any person obtaining a copy
28  * of this software and associated documentation files (the "Software"), to deal
29  * in the Software without restriction, including without limitation the rights
30  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31  * copies of the Software, and to permit persons to whom the Software is
32  * furnished to do so, subject to the following conditions:
33 
34  * The above copyright notice and this permission notice shall be included in all
35  * copies or substantial portions of the Software.
36 
37  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43  * SOFTWARE.
44 */
45 
46 #ifndef PSYNC_BLOOM_FILTER_HPP
47 #define PSYNC_BLOOM_FILTER_HPP
48 
49 #include <ndn-cxx/name.hpp>
50 
51 #include <string>
52 #include <vector>
53 #include <cmath>
54 #include <cstdlib>
55 
56 namespace psync {
57 
59 {
61  : number_of_hashes(0),
62  table_size(0)
63  {}
64 
65  unsigned int number_of_hashes;
66  unsigned int table_size;
67 };
68 
70 {
71 public:
73 
74  bool
75  compute_optimal_parameters();
76 
77  bool operator!() const
78  {
79  return (minimum_size > maximum_size) ||
80  (minimum_number_of_hashes > maximum_number_of_hashes) ||
81  (minimum_number_of_hashes < 1) ||
82  (0 == maximum_number_of_hashes) ||
83  (0 == projected_element_count) ||
84  (false_positive_probability < 0.0) ||
85  (std::numeric_limits<double>::infinity() == std::abs(false_positive_probability)) ||
86  (0 == random_seed) ||
87  (0xFFFFFFFFFFFFFFFFULL == random_seed);
88  }
89 
90  unsigned int minimum_size;
91  unsigned int maximum_size;
96  unsigned long long int random_seed;
98 };
99 
101 {
102 protected:
103  typedef uint32_t bloom_type;
104  typedef uint8_t cell_type;
105  typedef std::vector <cell_type>::iterator Iterator;
106 
107 public:
108  class Error : public std::runtime_error
109  {
110  public:
111  using std::runtime_error::runtime_error;
112  };
113 
114  BloomFilter();
115 
116  explicit BloomFilter(const BloomParameters& p);
117 
118  BloomFilter(unsigned int projected_element_count,
119  double false_positive_probability);
120 
121  BloomFilter(unsigned int projected_element_count,
122  double false_positive_probability,
123  const ndn::name::Component& bfName);
124 
126  getParameters(unsigned int projected_element_count,
127  double false_positive_probability);
128 
137  void
138  appendToName(ndn::Name& name) const;
139 
140  void
141  clear();
142 
143  void
144  insert(const std::string& key);
145 
146  bool
147  contains(const std::string& key) const;
148 
149  std::vector<cell_type>
150  table() const;
151 
152 private:
153  void
154  generate_unique_salt();
155 
156  void
157  compute_indices(const bloom_type& hash,
158  std::size_t& bit_index, std::size_t& bit) const;
159 
160 private:
161  std::vector <bloom_type> salt_;
162  std::vector <cell_type> bit_table_;
163  unsigned int salt_count_;
164  unsigned int table_size_; // 8 * raw_table_size;
165  unsigned int raw_table_size_;
166  unsigned int projected_element_count_;
167  unsigned int inserted_element_count_;
168  unsigned long long int random_seed_;
169  double desired_false_positive_probability_;
170 };
171 
172 bool
173 operator==(const BloomFilter& bf1, const BloomFilter& bf2);
174 
175 std::ostream&
176 operator<<(std::ostream& out, const BloomFilter& bf);
177 
178 } // namespace psync
179 
180 #endif // PSYNC_BLOOM_FILTER_HPP
unsigned long long int random_seed
unsigned int minimum_size
unsigned int minimum_number_of_hashes
bool operator==(const BloomFilter &bf1, const BloomFilter &bf2)
unsigned int projected_element_count
unsigned int maximum_number_of_hashes
std::ostream & operator<<(std::ostream &out, const BloomFilter &bf)
std::vector< cell_type >::iterator Iterator
unsigned int maximum_size
bool operator!() const
optimal_parameters_t optimal_parameters