72#ifndef ALEPH_COMPACT_CUCKOO_FILTER_H
73#define ALEPH_COMPACT_CUCKOO_FILTER_H
98 static_assert(FingerprintBits > 0
and FingerprintBits <= 32,
"Fingerprint bits must be between 1 and 32");
99 static_assert(EntriesPerBucket > 0,
"Entries per bucket must be > 0");
113 for (
size_t i = 0; i < EntriesPerBucket * FingerprintBits; ++i)
119 const size_t bit_offset = idx * FingerprintBits;
121 for (
size_t i = 0; i < FingerprintBits; ++i)
129 const size_t bit_offset = idx * FingerprintBits;
130 for (
size_t i = 0; i < FingerprintBits; ++i)
141 for (
size_t i = 0; i < EntriesPerBucket; ++i)
152 for (
size_t i = 0; i < EntriesPerBucket; ++i)
160 for (
size_t i = 0; i < EntriesPerBucket; ++i)
176 std::uniform_int_distribution<size_t> dist(0, EntriesPerBucket - 1);
177 const size_t idx = dist(
rng);
205 static constexpr Fingerprint fp_mask = (FingerprintBits == 32) ? 0xFFFFFFFF : (1U << FingerprintBits) - 1;
242 :
std::mt19937(
std::random_device{}()))
249 constexpr double limit =
static_cast<double>(std::numeric_limits<size_t>::max() / 2 + 1);
252 <<
"Compact_Cuckoo_Filter: " <<
capacity <<
" items ask for more buckets than a size_t counts";
253 const auto needed_buckets =
static_cast<size_t>(needed);
280 :
buckets(std::move(other.buckets)),
num_items(std::exchange(other.num_items, 0)),
315 std::swap(
rng_, other.rng_);
316 std::swap(
stash_, other.stash_);
324 [[nodiscard]]
size_t size() const noexcept
333 [[nodiscard]]
bool empty() const noexcept
373 const size_t bits_per_bucket = FingerprintBits * EntriesPerBucket;
374 const size_t bytes_per_bucket = (bits_per_bucket + 7) / 8;
375 return buckets.size() * bytes_per_bucket;
409 std::uniform_int_distribution<int> coin(0, 1);
410 size_t curr_idx = coin(
rng_) ? i1 : i2;
414 size_t bucket_idx = 0;
419 size_t history_size = 0;
421 for (
size_t kick = 0; kick <
max_kicks; ++kick)
423 const size_t slot_idx =
buckets(curr_idx).swap_random_slot(curr_fp,
rng_);
424 history[history_size++] = {curr_idx, slot_idx, curr_fp};
441 while (history_size > 0)
443 const Kick_Record & rec = history[--history_size];
444 buckets(rec.bucket_idx).restore(rec.original_fp, rec.slot_idx);
526 for (
size_t i = 0; i <
buckets.size(); ++i)
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
General utility functions and helpers.
Space-efficient bit array implementation.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
Contiguous array of bits.
int read_bit(const size_t i) const
Read bit i.
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
Memory-optimized Cuckoo Filter with bit-packed fingerprints.
bool append(const T &item)
Alias for insert().
bool contains(const T &item) const
Test membership.
static constexpr double kTargetLoad
Victim stash_[stash_size]
bool is_empty() const noexcept
Synonym for empty() in traditional Aleph style.
Aleph::Array< Bucket > buckets
size_t size() const noexcept
Return the number of items currently in the filter.
void clear()
Clear the 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,...
bool remove(const T &item)
Remove an item from the filter.
bool insert(const T &item)
Insert an item into the filter.
double load_factor() const noexcept
Return the load factor of the filter (relative to capacity()).
Compact_Cuckoo_Filter(size_t capacity, const std::optional< std::uint32_t > seed=std::nullopt)
Construct a Compact Cuckoo Filter with a given capacity.
static constexpr size_t stash_size
size_t alt_index(const size_t i, Fingerprint fp) const
static constexpr size_t max_kicks
size_t memory_usage() const noexcept
Return approximate memory usage in bytes.
static constexpr Fingerprint fp_mask
bool empty() const noexcept
Return whether the filter contains no items.
Compact_Cuckoo_Filter & operator=(const Compact_Cuckoo_Filter &other)=default
Copy the buckets, the stash and the generator of another filter.
Compact_Cuckoo_Filter(const Compact_Cuckoo_Filter &other)=default
Copy the buckets, the stash and the generator of another filter.
size_t capacity() const noexcept
Return the total number of fingerprint slots (including stash).
Fingerprint get_fingerprint(const T &item) const
void swap(Compact_Cuckoo_Filter &other) noexcept
Exchange in O(1) the state of this filter and another.
size_t index_hash(const T &item) const noexcept
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
size_t next_power_of_2(size_t x)
Return the smallest power of 2 that is not less than x.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
size_t murmur3hash(const Key &key, std::uint32_t seed)
bool remove(Fingerprint fp)
Fingerprint get_slot(const size_t idx) const
size_t swap_random_slot(Fingerprint &fp, std::mt19937 &rng)
void restore(Fingerprint fp, size_t idx)
bool contains(const Fingerprint fp) const
bool insert(Fingerprint fp)
void set_slot(size_t idx, Fingerprint fp)
Victim entry: a fingerprint that was displaced during a failed insert.
Dynamic array container with automatic resizing.