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-2022, 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_DETAIL_BLOOM_FILTER_HPP
47 #define PSYNC_DETAIL_BLOOM_FILTER_HPP
48 
49 #include <ndn-cxx/name.hpp>
50 #include <ndn-cxx/util/string-helper.hpp>
51 
52 #include <string>
53 #include <vector>
54 
55 namespace psync::detail {
56 
57 class bloom_parameters;
58 
60 {
61 public:
62  class Error : public std::runtime_error
63  {
64  public:
65  using std::runtime_error::runtime_error;
66  };
67 
68  BloomFilter() = default;
69 
70  BloomFilter(unsigned int projected_element_count,
71  double false_positive_probability);
72 
73  BloomFilter(unsigned int projected_element_count,
74  double false_positive_probability,
75  const ndn::name::Component& bfName);
76 
85  void
86  appendToName(ndn::Name& name) const;
87 
88  void
89  clear();
90 
91  void
92  insert(const ndn::Name& key);
93 
94  bool
95  contains(const ndn::Name& key) const;
96 
97 private:
98  typedef uint32_t bloom_type;
99  typedef uint8_t cell_type;
100 
101  explicit
102  BloomFilter(const bloom_parameters& p);
103 
104  void
105  compute_indices(const bloom_type& hash, std::size_t& bit_index, std::size_t& bit) const;
106 
107  void
108  generate_unique_salt();
109 
110 private: // non-member operators
111  // NOTE: the following "hidden friend" operators are available via
112  // argument-dependent lookup only and must be defined inline.
113 
114  friend bool
115  operator==(const BloomFilter& lhs, const BloomFilter& rhs)
116  {
117  return lhs.bit_table_ == rhs.bit_table_;
118  }
119 
120  friend bool
121  operator!=(const BloomFilter& lhs, const BloomFilter& rhs)
122  {
123  return lhs.bit_table_ != rhs.bit_table_;
124  }
125 
126  friend std::ostream&
127  operator<<(std::ostream& os, const BloomFilter& bf)
128  {
129  ndn::printHex(os, bf.bit_table_, false);
130  return os;
131  }
132 
133 private:
134  std::vector<bloom_type> salt_;
135  std::vector<cell_type> bit_table_;
136  unsigned int salt_count_ = 0;
137  unsigned int table_size_ = 0;
138  unsigned int projected_element_count_ = 0;
139  unsigned int inserted_element_count_ = 0;
140  unsigned long long int random_seed_ = 0;
141  double desired_false_positive_probability_ = 0.0;
142 };
143 
144 } // namespace psync::detail
145 
146 #endif // PSYNC_DETAIL_BLOOM_FILTER_HPP
void insert(const ndn::Name &key)
friend bool operator!=(const BloomFilter &lhs, const BloomFilter &rhs)
void appendToName(ndn::Name &name) const
Append our bloom filter to the given name.
bool contains(const ndn::Name &key) const
friend std::ostream & operator<<(std::ostream &os, const BloomFilter &bf)
friend bool operator==(const BloomFilter &lhs, const BloomFilter &rhs)