iblt.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) 2014 Gavin Andresen
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_IBLT_HPP
47 #define PSYNC_IBLT_HPP
48 
49 #include <ndn-cxx/name.hpp>
50 
51 #include <inttypes.h>
52 #include <set>
53 #include <vector>
54 #include <string>
55 
56 namespace psync {
57 
59 {
60 public:
61  int32_t count;
62  uint32_t keySum;
63  uint32_t keyCheck;
64 
65  bool
66  isPure() const;
67 
68  bool
69  isEmpty() const;
70 };
71 
72 extern const size_t N_HASH;
73 extern const size_t N_HASHCHECK;
74 
80 class IBLT
81 {
82 public:
83  class Error : public std::runtime_error
84  {
85  public:
86  using std::runtime_error::runtime_error;
87  };
88 
94  explicit IBLT(size_t expectedNumEntries);
95 
102  void
103  initialize(const ndn::name::Component& ibltName);
104 
105  void
106  insert(uint32_t key);
107 
108  void
109  erase(uint32_t key);
110 
122  bool
123  listEntries(std::set<uint32_t>& positive, std::set<uint32_t>& negative) const;
124 
125  IBLT
126  operator-(const IBLT& other) const;
127 
128  std::vector<HashTableEntry>
129  getHashTable() const
130  {
131  return m_hashTable;
132  }
133 
145  void
146  appendToName(ndn::Name& name) const;
147 
157  std::vector<uint32_t>
158  extractValueFromName(const ndn::name::Component& ibltName) const;
159 
160 private:
161  void
162  update(int plusOrMinus, uint32_t key);
163 
164 private:
165  std::vector<HashTableEntry> m_hashTable;
166  static const int INSERT = 1;
167  static const int ERASE = -1;
168 };
169 
170 bool
171 operator==(const IBLT& iblt1, const IBLT& iblt2);
172 
173 bool
174 operator!=(const IBLT& iblt1, const IBLT& iblt2);
175 
176 std::ostream&
177 operator<<(std::ostream& out, const IBLT& iblt);
178 
179 } // namespace psync
180 
181 #endif // PSYNC_IBLT_HPP
uint32_t keyCheck
Definition: iblt.hpp:63
std::vector< HashTableEntry > getHashTable() const
Definition: iblt.hpp:129
const size_t N_HASH
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:80
const size_t N_HASHCHECK
bool operator==(const BloomFilter &bf1, const BloomFilter &bf2)
uint32_t keySum
Definition: iblt.hpp:62
bool operator!=(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:209
bool isEmpty() const
Definition: iblt.cpp:73
bool isPure() const
Definition: iblt.cpp:62
std::ostream & operator<<(std::ostream &out, const BloomFilter &bf)