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
27template <class T>
29 friend class CacheMemoryTestPeer;
30
31 public:
32 BloomFilter(size_t length, size_t hashcount)
33 : m_Bitmap(), m_nLength(length), m_nHashCount(hashcount) {}
34 virtual ~BloomFilter() {};
35
36 void add(const T& data) {
37 add(&data, sizeof(T));
38 }
39
40 void add(const T* data, size_t length) {
41 uint64_t baseHash[2];
42 MurmurHash3_x64_128(data, length, 0, baseHash);
43
44 for (size_t i = 0; i < m_nHashCount; ++i) {
45 uint64_t n = (baseHash[0] + (i * baseHash[1])) % m_nLength;
46 m_Bitmap.set(n);
47 }
48 }
49
50 bool contains(const T& data) {
51 return contains(&data, sizeof(T));
52 }
53
54 bool contains(const T* data, size_t length) {
55 uint64_t baseHash[2];
56 MurmurHash3_x64_128(data, length, 0, baseHash);
57
58 for (size_t i = 0; i < m_nHashCount; ++i) {
59 uint64_t n = (baseHash[0] + (i * baseHash[1])) % m_nLength;
60 if (!m_Bitmap.test(n)) {
61 return false;
62 }
63 }
64
65 return true;
66 }
67
68 void clear() {
69 for (size_t i = 0; i < m_nLength; ++i) {
70 m_Bitmap.clear(i);
71 }
72 }
73
74 private:
75 ExtensibleBitmap m_Bitmap;
76 size_t m_nLength;
77 size_t m_nHashCount;
78};
79
80extern template class BloomFilter<void*>; // IWYU pragma: keep
81extern template class BloomFilter<int8_t>; // IWYU pragma: keep
82extern template class BloomFilter<int16_t>; // IWYU pragma: keep
83extern template class BloomFilter<int32_t>; // IWYU pragma: keep
84extern template class BloomFilter<int64_t>; // IWYU pragma: keep
85extern template class BloomFilter<uint8_t>; // IWYU pragma: keep
86extern template class BloomFilter<uint16_t>; // IWYU pragma: keep
87extern template class BloomFilter<uint32_t>; // IWYU pragma: keep
88extern template class BloomFilter<uint64_t>; // IWYU pragma: keep
89
90#endif
bool test(size_t n) const
void clear(size_t n)
void set(size_t n)