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