IBR-DTNSuite  0.10
BloomFilter.h
Go to the documentation of this file.
1 /*
2  * BloomFilter.h
3  *
4  * This bloom filter implementation is based on
5  * Open Bloom Filter (http://www.partow.net) written by Arash Partow.
6  *
7  * Copyright (C) 2011 IBR, TU Braunschweig
8  *
9  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  */
24 
25 #include <cstddef>
26 #include <algorithm>
27 #include <cmath>
28 #include <limits>
29 #include <string>
30 #include <vector>
31 #include <list>
32 
33 #ifndef BLOOMFILTER_H_
34 #define BLOOMFILTER_H_
35 
36 namespace ibrcommon
37 {
38  typedef unsigned int bloom_type;
39  typedef unsigned char cell_type;
40 
42  {
43  public:
44  virtual ~HashProvider() = 0;
45 
50  virtual size_t count() const = 0;
51 
52  virtual void clear() = 0;
53  virtual const std::list<bloom_type> hash(const unsigned char* begin, std::size_t remaining_length) const = 0;
54  };
55 
57  {
58  public:
59  DefaultHashProvider(size_t salt_count);
60  virtual ~DefaultHashProvider();
61 
62  bool operator==(const DefaultHashProvider &provider) const;
63  size_t count() const;
64  void clear();
65 
66  const std::list<bloom_type> hash(const unsigned char* begin, std::size_t remaining_length) const;
67 
68  private:
69  void add(bloom_type hash);
70  void generate_salt();
71  bloom_type hash_ap(const unsigned char* begin, std::size_t remaining_length, bloom_type hash) const;
72 
73  std::vector<bloom_type> _salt;
74  std::size_t salt_count_;
75  };
76 
78  {
79  protected:
80  static const std::size_t bits_per_char = 0x08; // 8 bits in 1 char(unsigned)
81  static const unsigned char bit_mask[bits_per_char];
82 
83  public:
84  BloomFilter(std::size_t table_size = 8192, std::size_t salt_count = 2);
85  BloomFilter(const BloomFilter& filter);
86  virtual ~BloomFilter();
87 
88  void load(const cell_type* data, size_t len);
89 
90  BloomFilter& operator=(const BloomFilter& filter);
91 
92  bool operator!() const;
93  void clear();
94 
95  void insert(const unsigned char* key_begin, const std::size_t length);
96 
97  template<typename T>
98  void insert(const T& t)
99  {
100  insert(reinterpret_cast<const unsigned char*>(&t),sizeof(T));
101  }
102 
103  void insert(const std::string& key);
104 
105  void insert(const char* data, const std::size_t& length);
106 
107  template<typename InputIterator>
108  void insert(const InputIterator begin, const InputIterator end)
109  {
110  InputIterator it = begin;
111  while(it != end)
112  {
113  insert(*(it++));
114  }
115  }
116 
117  virtual bool contains(const unsigned char* key_begin, const std::size_t length) const;
118 
119  template<typename T>
120  bool contains(const T& t) const
121  {
122  return contains(reinterpret_cast<const unsigned char*>(&t),static_cast<std::size_t>(sizeof(T)));
123  }
124 
125  bool contains(const std::string& key) const;
126 
127  bool contains(const char* data, const std::size_t& length) const;
128 
129  template<typename InputIterator>
130  InputIterator contains_all(const InputIterator begin, const InputIterator end) const
131  {
132  InputIterator it = begin;
133  while(it != end)
134  {
135  if (!contains(*it))
136  {
137  return it;
138  }
139  ++it;
140  }
141  return end;
142  }
143 
144  template<typename InputIterator>
145  InputIterator contains_none(const InputIterator begin, const InputIterator end) const
146  {
147  InputIterator it = begin;
148  while(it != end)
149  {
150  if (contains(*it))
151  {
152  return it;
153  }
154  ++it;
155  }
156  return end;
157  }
158 
159  virtual std::size_t size() const;
160 
161  BloomFilter& operator &= (const BloomFilter& filter);
162  BloomFilter& operator |= (const BloomFilter& filter);
163  BloomFilter& operator ^= (const BloomFilter& filter);
164 
165  const cell_type* table() const;
166 
167  double getAllocation() const;
168 
169  protected:
170  virtual void compute_indices(const bloom_type& hash, std::size_t& bit_index, std::size_t& bit) const;
172 
173  unsigned char* bit_table_;
174  std::size_t table_size_;
175 
176  unsigned int _itemcount;
177  std::size_t salt_count_;
178  };
179 }
180 
181 
182 #endif /* BLOOMFILTER_H_ */