Skip to main content

BloomFilter

Bloom filter for probabilistic set membership. Implements a space-efficient probabilistic set data structure. Supports insert and membership test operations.

Parameters

  • N/A Uses internal bit array (not template parameter)
Filter capacity must be >= 1 False positive rate must be in (0, 1) :::note Memory usage: approximately 1.2 bytes per expected element at 1% FPR ::: :::note Uses FNV-1a hash with double hashing for k independent hash functions :::

Public Methods


BloomFilter

inline
Construct a Bloom filter with specified capacity and false positive rate.

Parameters

  • expected_elements Maximum number of elements expected to be inserted
  • false_positive_rate Desired false positive rate (0.0 to 1.0)

Exceptions

  • std::invalid_argument If capacity < 1
  • std::invalid_argument If false_positive_rate <= 0.0 or >= 1.0
Filter is initialized with all bits set to 0

Example:


add

inline
Insert an element into the filter.

Parameters

  • item String to insert
Element will return true for subsequent has() calls :::note May set additional bits (affects other elements’ FPR) ::: :::note Thread-safe: caller must ensure no concurrent access :::

has

const
Check if element may be in the set.

Parameters

  • item String to check

Returns

true if element possibly exists, false if definitely not exists

Parameters

  • true Element may be in set (or false positive)
  • false Element is definitely not in set
:::note False positives are possible (returns true when element wasn’t added) ::: :::note False negatives are impossible (returns false only for non-members) :::

Example:


clear

inline
Reset all bits to zero.

Private Attributes


bits_

Bit array storage.

k_

Number of hash functions.

m_

Number of bits in filter.

Private Methods


hash_fn

const
Compute hash at index i using double hashing technique. Uses FNV-1a as base hash, then applies secondary hash for generating k independent hash functions via h(i) = h1 + i*h2

Parameters

  • s String to hash
  • seed Secondary hash seed

Returns

Hash value modulo m_