Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::CountingBloomFilter< kLog2Slots > Class Template Reference

A counting Bloom filter of 2^kLog2Slots 8-bit saturating counters with two probes per key, which supports remove. More...

#include "donner/base/BloomFilter.h"

Public Member Functions

constexpr CountingBloomFilter () noexcept=default
 Creates an empty filter.
constexpr bool insert (BloomHash hash) noexcept
 Adds one occurrence of a key.
constexpr void remove (BloomHash hash) noexcept
 Removes one occurrence of a key.
constexpr bool mayContain (BloomHash hash) const noexcept
 Returns false only if hash has no occurrence left since construction or the last clear.
constexpr void clear () noexcept
 Removes every key and resets saturated counters.

Static Public Member Functions

static double expectedFalsePositiveRate (std::size_t keys) noexcept
 Approximates the false-positive rate with keys distinct keys present, assuming no counters remain saturated after removals.

Static Public Attributes

static constexpr std::size_t kSlots = Probes::kSlots
 Number of counters.
static constexpr std::uint8_t kSaturated = 255
 Counter value that is never decremented.

Detailed Description

template<int kLog2Slots>
class donner::CountingBloomFilter< kLog2Slots >

A counting Bloom filter of 2^kLog2Slots 8-bit saturating counters with two probes per key, which supports remove.

Invariant: after any sequence of operations in which every remove(h) follows a matching insert(h), mayContain(h) is true for every h inserted more times than removed. A counter that reaches 255 stays at 255: insert does not wrap it and remove never decrements it, so removal cannot create a false negative, at the cost of a false positive on that counter until clear. Sizing guidance matches BloomFilter, counting the keys present: with 4,096 counters (kLog2Slots = 12, 4 KiB), 100 keys give about 0.23 percent false positives.

Storage is inline (2^kLog2Slots bytes) and every operation except clear takes constant time and never allocates. There is no internal synchronization: concurrent mayContain calls are safe while no thread modifies the filter, and any modification needs exclusive access.

Template Parameters
kLog2SlotsBase-2 logarithm of the counter count, 1 to 16.

Member Function Documentation

◆ expectedFalsePositiveRate()

template<int kLog2Slots>
double donner::CountingBloomFilter< kLog2Slots >::expectedFalsePositiveRate ( std::size_t keys)
inlinestaticnoexcept

Approximates the false-positive rate with keys distinct keys present, assuming no counters remain saturated after removals.

Parameters
keysNumber of keys present.

◆ insert()

template<int kLog2Slots>
bool donner::CountingBloomFilter< kLog2Slots >::insert ( BloomHash hash)
inlineconstexprnoexcept

Adds one occurrence of a key.

Parameters
hashThe key's hash.
Returns
True if either probed counter is saturated after this insertion.

◆ mayContain()

template<int kLog2Slots>
bool donner::CountingBloomFilter< kLog2Slots >::mayContain ( BloomHash hash) const
inlinenodiscardconstexprnoexcept

Returns false only if hash has no occurrence left since construction or the last clear.

Parameters
hashThe key's hash.

◆ remove()

template<int kLog2Slots>
void donner::CountingBloomFilter< kLog2Slots >::remove ( BloomHash hash)
inlineconstexprnoexcept

Removes one occurrence of a key.

The key must have been inserted more times than removed since construction or the last clear. Removing a key without a matching insertion can cause false negatives for other keys.

Parameters
hashThe key's hash.

The documentation for this class was generated from the following file: