77#ifndef ALEPH_CUCKOO_FILTER_H
78#define ALEPH_CUCKOO_FILTER_H
107template <
typename T,
size_t Fingerpr
intBits = 8,
size_t EntriesPerBucket = 4>
class Cuckoo_Filter
109 static_assert(FingerprintBits > 0
and FingerprintBits <= 32,
"Fingerprint bits must be between 1 and 32");
110 static_assert(EntriesPerBucket > 0,
"Entries per bucket must be > 0");
120 std::fill(
slots,
slots + EntriesPerBucket, 0);
125 std::fill(
slots,
slots + EntriesPerBucket, 0);
130 for (
size_t i = 0; i < EntriesPerBucket; ++i)
141 for (
size_t i = 0; i < EntriesPerBucket; ++i)
149 for (
size_t i = 0; i < EntriesPerBucket; ++i)
165 std::uniform_int_distribution<size_t> dist(0, EntriesPerBucket - 1);
166 const size_t idx = dist(
rng);
167 std::swap(fp,
slots[idx]);
192 static constexpr Fingerprint fp_mask = (FingerprintBits == 32) ? 0xFFFFFFFF : (1U << FingerprintBits) - 1;
228 std::optional<std::uint32_t>
seed = std::nullopt)
230 :
std::mt19937(
std::random_device{}()))
237 constexpr double limit =
static_cast<double>(std::numeric_limits<size_t>::max() / 2 + 1);
240 <<
"Cuckoo_Filter: " <<
capacity <<
" items ask for more buckets than a size_t counts";
241 const auto needed_buckets =
static_cast<size_t>(needed);
304 std::swap(
rng_, other.rng_);
305 std::swap(
stash_, other.stash_);
313 [[nodiscard]]
size_t size() const noexcept
322 [[nodiscard]]
bool empty() const noexcept
380 std::uniform_int_distribution<int> coin(0, 1);
381 size_t curr_idx = coin(
rng_) ? i1 : i2;
385 size_t bucket_idx = 0;
390 size_t history_size = 0;
392 for (
size_t kick = 0; kick <
max_kicks; ++kick)
394 const size_t slot_idx =
buckets(curr_idx).swap_random_slot(curr_fp,
rng_);
395 history[history_size++] = {curr_idx, slot_idx, curr_fp};
412 while (history_size > 0)
414 const Kick_Record & rec = history[--history_size];
415 buckets(rec.bucket_idx).restore(rec.original_fp, rec.slot_idx);
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.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
Industrial-grade Cuckoo Filter implementation.
bool contains(const T &item) const
Test membership.
static constexpr Fingerprint fp_mask
Cuckoo_Filter(size_t capacity, std::optional< std::uint32_t > seed=std::nullopt)
Construct a Cuckoo Filter with a given capacity.
double load_factor() const noexcept
Return the load factor of the filter (relative to capacity()).
void clear()
Clear the filter.
Cuckoo_Filter(Cuckoo_Filter &&other)
Take the items of another filter, which is left as an empty filter of one bucket.
bool empty() const noexcept
Return whether the filter contains no items.
bool is_empty() const noexcept
Synonym for empty() in traditional Aleph style.
Fingerprint get_fingerprint(const T &item) const
bool remove(const T &item)
Remove an item from the filter.
size_t size() const noexcept
Return the number of items currently in the filter.
Cuckoo_Filter(const Cuckoo_Filter &other)=default
Copy the buckets, the stash and the generator of another filter.
size_t alt_index(size_t i, Fingerprint fp) const
Aleph::Array< Bucket > buckets
Cuckoo_Filter & operator=(const Cuckoo_Filter &other)=default
Copy the buckets, the stash and the generator of another filter.
static constexpr size_t stash_size
size_t index_hash(const T &item) const noexcept
static constexpr size_t max_kicks
static constexpr double kTargetLoad
Victim stash_[stash_size]
size_t capacity() const noexcept
Return the total number of fingerprint slots (including stash).
bool insert(const T &item)
Insert an item into the filter.
void swap(Cuckoo_Filter &other) noexcept
Exchange in O(1) the state of this filter and another.
bool append(const T &item)
Alias for insert().
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)
size_t swap_random_slot(Fingerprint &fp, std::mt19937 &rng)
Fingerprint slots[EntriesPerBucket]
bool insert(Fingerprint fp)
void restore(Fingerprint fp, size_t idx)
bool contains(Fingerprint fp) const
Victim entry: a fingerprint displaced during a failed kick chain.
Dynamic array container with automatic resizing.