Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket > Class Template Reference

Memory-optimized Cuckoo Filter with bit-packed fingerprints. More...

#include <compact-cuckoo-filter.H>

Collaboration diagram for Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >:
[legend]

Classes

struct  Bucket
 
struct  Victim
 Victim entry: a fingerprint that was displaced during a failed insert. More...
 

Public Member Functions

 Compact_Cuckoo_Filter (size_t capacity, const std::optional< std::uint32_t > seed=std::nullopt)
 Construct a Compact Cuckoo Filter with a given capacity.
 
 Compact_Cuckoo_Filter (const Compact_Cuckoo_Filter &other)=default
 Copy the buckets, the stash and the generator of another filter.
 
 Compact_Cuckoo_Filter (Compact_Cuckoo_Filter &&other) noexcept
 Take the items of another filter, which is left as an empty filter of the same capacity, with no buckets until it inserts.
 
Compact_Cuckoo_Filter & operator= (const Compact_Cuckoo_Filter &other)=default
 Copy the buckets, the stash and the generator of another filter.
 
Compact_Cuckoo_Filter & operator= (Compact_Cuckoo_Filter &&other) noexcept
 Exchange this filter with another, which is left with the previous state of this one.
 
void swap (Compact_Cuckoo_Filter &other) noexcept
 Exchange in O(1) the state of this filter and another.
 
size_t size () const noexcept
 Return the number of items currently in the filter.
 
bool empty () const noexcept
 Return whether the filter contains no items.
 
bool is_empty () const noexcept
 Synonym for empty() in traditional Aleph style.
 
size_t capacity () const noexcept
 Return the total number of fingerprint slots (including stash).
 
double load_factor () const noexcept
 Return the load factor of the filter (relative to capacity()).
 
size_t memory_usage () const noexcept
 Return approximate memory usage in bytes.
 
bool insert (const T &item)
 Insert an item into the filter.
 
bool append (const T &item)
 Alias for insert().
 
bool contains (const T &item) const
 Test membership.
 
bool remove (const T &item)
 Remove an item from the filter.
 
void clear ()
 Clear the filter.
 

Private Types

using Fingerprint = uint32_t
 

Private Member Functions

Fingerprint get_fingerprint (const T &item) const
 
size_t index_hash (const T &item) const noexcept
 
size_t alt_index (const size_t i, Fingerprint fp) const
 

Private Attributes

Aleph::Array< Bucket > buckets
 
size_t num_items = 0
 
size_t num_buckets = 0
 
size_t bucket_mask = 0
 
std::mt19937 rng_
 
Victim stash_ [stash_size] = {}
 
size_t stash_count_ = 0
 

Static Private Attributes

static constexpr size_t max_kicks = 500
 
static constexpr size_t stash_size = 4
 
static constexpr double kTargetLoad = 0.9
 
static constexpr Fingerprint fp_mask = (FingerprintBits == 32) ? 0xFFFFFFFF : (1U << FingerprintBits) - 1
 

Detailed Description

template<typename T, size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
class Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >

Memory-optimized Cuckoo Filter with bit-packed fingerprints.

Note
Not thread-safe. Concurrent reads (contains / size / etc.) are safe; any concurrent write (insert / remove / clear) requires external synchronisation.

Definition at line 96 of file compact-cuckoo-filter.H.

Member Typedef Documentation

◆ Fingerprint

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
using Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::Fingerprint = uint32_t
private

Definition at line 101 of file compact-cuckoo-filter.H.

Constructor & Destructor Documentation

◆ Compact_Cuckoo_Filter() [1/3]

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::Compact_Cuckoo_Filter ( size_t  capacity,
const std::optional< std::uint32_t >  seed = std::nullopt 
)
inlineexplicit

Construct a Compact Cuckoo Filter with a given capacity.

Parameters
capacityMinimum number of items to support.
seedOptional RNG seed for deterministic kick sequences. Pass a fixed value in tests to eliminate flakiness. Defaults to a non-deterministic device seed.
Exceptions
std::bad_allocif memory allocation fails.
std::overflow_errorif the capacity asks for more buckets than a size_t counts, or than their array holds.
Note
O(capacity / EntriesPerBucket).

Definition at line 240 of file compact-cuckoo-filter.H.

References ah_overflow_error_if, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::bucket_mask, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::buckets, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::capacity(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::kTargetLoad, Aleph::next_power_of_2(), and Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_buckets.

◆ Compact_Cuckoo_Filter() [2/3]

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::Compact_Cuckoo_Filter ( const Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket > &  other)
default

Copy the buckets, the stash and the generator of another filter.

Parameters
[in]otherfilter to copy.
Exceptions
std::bad_allocif the buckets cannot be allocated.

◆ Compact_Cuckoo_Filter() [3/3]

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::Compact_Cuckoo_Filter ( Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket > &&  other)
inlinenoexcept

Take the items of another filter, which is left as an empty filter of the same capacity, with no buckets until it inserts.

The implicit move took the buckets but copied their count and their mask: the moved-from filter then indexed buckets that it no longer had. Now its first insertion allocates them, and it draws its kicks from a copy of its generator.

Parameters
[in,out]otherfilter to move from.
Note
O(1).

Definition at line 279 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_, and Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_size.

Member Function Documentation

◆ alt_index()

◆ append()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
bool Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::append ( const T &  item)
inline

Alias for insert().

Parameters
itemItem to insert.
Returns
true if insertion succeeded, false if filter is full.
Exceptions
none.

Definition at line 455 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::insert().

◆ capacity()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
size_t Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::capacity ( ) const
inlinenoexcept

◆ clear()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
void Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::clear ( )
inline

◆ contains()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
bool Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::contains ( const T &  item) const
inline

◆ empty()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
bool Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::empty ( ) const
inlinenoexcept

Return whether the filter contains no items.

Returns
true if the filter has no items.
Exceptions
none.

Definition at line 333 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_items.

Referenced by Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::is_empty().

◆ get_fingerprint()

◆ index_hash()

◆ insert()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
bool Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::insert ( const T &  item)
inline

Insert an item into the filter.

Parameters
itemItem to insert.
Returns
true if insertion succeeded, false if filter is full.
Exceptions
std::bad_allocif the filter was moved from and its buckets cannot be allocated.
Note
Amortised O(1); the first insertion into a moved-from filter allocates its buckets, in O(capacity / EntriesPerBucket).

Definition at line 387 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::alt_index(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::buckets, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::get_fingerprint(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::index_hash(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::insert(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::max_kicks, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_buckets, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_items, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::rng_, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_, Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_count_, and Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_size.

Referenced by Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::append(), example_basic_usage(), example_deletion(), example_duplicates(), example_embedded_systems(), example_packet_deduplication(), example_performance(), Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::insert(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ is_empty()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
bool Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::is_empty ( ) const
inlinenoexcept

Synonym for empty() in traditional Aleph style.

Returns
true if the filter has no items.
Exceptions
none.

Definition at line 342 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::empty().

Referenced by TEST(), and TEST().

◆ load_factor()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
double Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::load_factor ( ) const
inlinenoexcept

Return the load factor of the filter (relative to capacity()).

Returns
Ratio of stored items to total slots.
Exceptions
none.

Definition at line 360 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::capacity(), and Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_items.

Referenced by example_basic_usage(), TEST(), and TEST().

◆ memory_usage()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
size_t Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::memory_usage ( ) const
inlinenoexcept

Return approximate memory usage in bytes.

Returns
Byte count used by the bucket array; 0 in a moved-from filter, until it inserts.
Exceptions
none.

Definition at line 370 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::buckets.

Referenced by example_basic_usage(), example_custom_fingerprints(), example_embedded_systems(), example_memory_optimization(), example_packet_deduplication(), example_performance(), TEST(), TEST(), and TEST().

◆ operator=() [1/2]

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
Compact_Cuckoo_Filter & Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::operator= ( Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket > &&  other)
inlinenoexcept

Exchange this filter with another, which is left with the previous state of this one.

Parameters
[in,out]otherfilter to move from.
Returns
this filter.
Note
Complexity: O(1).

Definition at line 300 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::swap().

◆ operator=() [2/2]

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
Compact_Cuckoo_Filter & Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::operator= ( const Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket > &  other)
default

Copy the buckets, the stash and the generator of another filter.

Parameters
[in]otherfilter to copy.
Returns
this filter.
Exceptions
std::bad_allocif the buckets cannot be allocated.

◆ remove()

◆ size()

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
size_t Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::size ( ) const
inlinenoexcept

Return the number of items currently in the filter.

Returns
Current item count.
Exceptions
none.

Definition at line 324 of file compact-cuckoo-filter.H.

References Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::num_items.

Referenced by example_basic_usage(), example_deletion(), example_duplicates(), example_packet_deduplication(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ swap()

Member Data Documentation

◆ bucket_mask

◆ buckets

◆ fp_mask

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
constexpr Fingerprint Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::fp_mask = (FingerprintBits == 32) ? 0xFFFFFFFF : (1U << FingerprintBits) - 1
staticconstexprprivate

◆ kTargetLoad

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
constexpr double Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::kTargetLoad = 0.9
staticconstexprprivate

◆ max_kicks

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
constexpr size_t Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::max_kicks = 500
staticconstexprprivate

◆ num_buckets

◆ num_items

◆ rng_

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
std::mt19937 Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::rng_
mutableprivate

◆ stash_

◆ stash_count_

◆ stash_size

template<typename T , size_t FingerprintBits = 8, size_t EntriesPerBucket = 4>
constexpr size_t Aleph::Compact_Cuckoo_Filter< T, FingerprintBits, EntriesPerBucket >::stash_size = 4
staticconstexprprivate

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