Cachemere
Modular Caching Library for C++
counting_bloom_filter.h
1 #ifndef CACHEMERE_COUNTINGBLOOMFILTER_H
2 #define CACHEMERE_COUNTINGBLOOMFILTER_H
3 
4 #include <functional>
5 #include <vector>
6 
7 #include <absl/hash/hash.h>
8 
9 #include "hash_mixer.h"
10 
11 namespace cachemere::policy::detail {
12 
18 template<typename ItemHash> class CountingBloomFilter
19 {
20 public:
30 
33  template<typename ItemKey> void add(const ItemKey& item);
34 
36  void clear();
37 
42  void decay();
43 
51  template<typename ItemKey> [[nodiscard]] uint32_t estimate(const ItemKey& item) const;
52 
55  [[nodiscard]] uint32_t cardinality() const noexcept;
56 
59  [[nodiscard]] size_t memory_used() const noexcept;
60 
67  [[nodiscard]] double saturation() const noexcept;
68 
69 private:
70  uint32_t m_cardinality;
71  std::vector<uint32_t> m_filter;
72  uint32_t m_nb_hashes;
73  uint32_t m_nb_nonzero = 0;
74 };
75 
76 } // namespace cachemere::policy::detail
77 
78 #include "counting_bloom_filter.hpp"
79 
80 #endif
cachemere::policy::detail::CountingBloomFilter::add
void add(const ItemKey &item)
Increment the count for a given item by one.
Definition: counting_bloom_filter.hpp:17
cachemere::policy::detail::CountingBloomFilter
Space-efficient probabilistic data structure to estimate the number of times an item was inserted in ...
Definition: counting_bloom_filter.h:18
cachemere::policy::detail::CountingBloomFilter::memory_used
size_t memory_used() const noexcept
Get an estimate of the memory consumption of the filter.
Definition: counting_bloom_filter.hpp:86
cachemere::policy::detail::CountingBloomFilter::clear
void clear()
Clear the filter while keeping the allocated memory.
Definition: counting_bloom_filter.hpp:48
cachemere::policy::detail::CountingBloomFilter::decay
void decay()
Divide counter values by two.
Definition: counting_bloom_filter.hpp:54
cachemere::policy::detail::CountingBloomFilter::saturation
double saturation() const noexcept
Get the saturation of the filter.
Definition: counting_bloom_filter.hpp:96
cachemere::policy::detail::CountingBloomFilter::estimate
uint32_t estimate(const ItemKey &item) const
Get the counter estimate for a given item.
Definition: counting_bloom_filter.hpp:64
cachemere::policy::detail::CountingBloomFilter::cardinality
uint32_t cardinality() const noexcept
Get the cardinality of the filter.
Definition: counting_bloom_filter.hpp:81
cachemere::policy::detail::CountingBloomFilter::CountingBloomFilter
CountingBloomFilter(uint32_t cardinality)
Constructor.
Definition: counting_bloom_filter.hpp:10