The Pedigree Project  0.1
BloomFilter.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_BLOOMFILTER_H
21 #define KERNEL_UTILITIES_BLOOMFILTER_H
22 
23 #include "pedigree/kernel/processor/types.h"
24 #include "pedigree/kernel/utilities/ExtensibleBitmap.h"
25 #include "pedigree/kernel/utilities/smhasher/MurmurHash3.h"
26 
27 template <class T>
29 {
30  public:
31  BloomFilter(size_t length, size_t hashcount)
32  : m_Bitmap(), m_nLength(length), m_nHashCount(hashcount)
33  {
34  }
35  virtual ~BloomFilter(){};
36 
37  void add(const T &data)
38  {
39  add(&data, sizeof(T));
40  }
41 
42  void add(const T *data, size_t length)
43  {
44  uint64_t baseHash[2];
45  MurmurHash3_x64_128(data, length, 0, baseHash);
46 
47  for (size_t i = 0; i < m_nHashCount; ++i)
48  {
49  uint64_t n = (baseHash[0] + (i * baseHash[1])) % m_nLength;
50  m_Bitmap.set(n);
51  }
52  }
53 
54  bool contains(const T &data)
55  {
56  return contains(&data, sizeof(T));
57  }
58 
59  bool contains(const T *data, size_t length)
60  {
61  uint64_t baseHash[2];
62  MurmurHash3_x64_128(data, length, 0, baseHash);
63 
64  for (size_t i = 0; i < m_nHashCount; ++i)
65  {
66  uint64_t n = (baseHash[0] + (i * baseHash[1])) % m_nLength;
67  if (!m_Bitmap.test(n))
68  {
69  return false;
70  }
71  }
72 
73  return true;
74  }
75 
76  void clear()
77  {
78  for (size_t i = 0; i < m_nLength; ++i)
79  {
80  m_Bitmap.clear(i);
81  }
82  }
83 
84  private:
85  ExtensibleBitmap m_Bitmap;
86  size_t m_nLength;
87  size_t m_nHashCount;
88 };
89 
90 extern template class BloomFilter<void *>; // IWYU pragma: keep
91 extern template class BloomFilter<int8_t>; // IWYU pragma: keep
92 extern template class BloomFilter<int16_t>; // IWYU pragma: keep
93 extern template class BloomFilter<int32_t>; // IWYU pragma: keep
94 extern template class BloomFilter<int64_t>; // IWYU pragma: keep
95 extern template class BloomFilter<uint8_t>; // IWYU pragma: keep
96 extern template class BloomFilter<uint16_t>; // IWYU pragma: keep
97 extern template class BloomFilter<uint32_t>; // IWYU pragma: keep
98 extern template class BloomFilter<uint64_t>; // IWYU pragma: keep
99 
100 #endif
bool test(size_t n) const
void set(size_t n)
void clear(size_t n)