Cachemere
Modular Caching Library for C++
bloom_filter.hpp
1 #include <cassert>
2 
3 #include "bloom_filter_math.h"
4 
5 namespace cachemere::policy::detail {
6 
7 template<typename ItemHash>
8 BloomFilter<ItemHash>::BloomFilter(uint32_t cardinality)
9  : m_cardinality{cardinality},
10  m_filter{optimal_filter_size(cardinality), false},
11  m_nb_hashes{optimal_nb_of_hash_functions(cardinality, m_filter.size())}
12 {
13 }
14 
15 template<typename ItemHash> template<typename ItemKey> void BloomFilter<ItemHash>::add(const ItemKey& item)
16 {
17  HashMixer<ItemKey, ItemHash> mixer{item, m_filter.size()};
18 
19  for (size_t i = 0; i < m_nb_hashes; ++i) {
20  const size_t filter_idx = mixer();
21  assert(filter_idx < m_filter.size());
22 
23  m_filter.set(filter_idx);
24  }
25 }
26 
27 template<typename ItemHash> void BloomFilter<ItemHash>::clear()
28 {
29  m_filter.reset();
30 }
31 
32 template<typename ItemHash> template<typename ItemKey> bool BloomFilter<ItemHash>::maybe_contains(const ItemKey& item) const
33 {
34  HashMixer<ItemKey, ItemHash> mixer{item, m_filter.size()};
35 
36  for (size_t i = 0; i < m_nb_hashes; ++i) {
37  const size_t filter_idx = mixer();
38  assert(filter_idx < m_filter.size());
39 
40  if (!m_filter.test(filter_idx)) {
41  return false;
42  }
43  }
44 
45  return true;
46 }
47 
48 template<typename ItemHash> size_t BloomFilter<ItemHash>::memory_used() const noexcept
49 {
50  return m_filter.num_blocks() * sizeof(BitsetBlock) + sizeof(m_nb_hashes);
51 }
52 
53 template<typename ItemHash> double BloomFilter<ItemHash>::saturation() const noexcept
54 {
55  assert(m_filter.size() > 0);
56  return static_cast<double>(m_filter.count()) / static_cast<double>(m_filter.size());
57 }
58 
59 } // namespace cachemere::policy::detail
cachemere::policy::detail::BloomFilter
Probabilistic data structure for representing sets in a space-efficient format.
Definition: bloom_filter.h:29
cachemere::policy::detail::HashMixer
Functor used for generating a uniform sequence of numbers in a given value range for a given key.
Definition: hash_mixer.h:13
cachemere::policy::detail::BloomFilter::BloomFilter
BloomFilter(uint32_t cardinality)
Constructor.
Definition: bloom_filter.hpp:8