66#ifndef COMPACT_QUOTIENT_FILTER_H
67#define COMPACT_QUOTIENT_FILTER_H
125 return (1ULL <<
r_) - 1;
128 [[nodiscard]]
bool get_bit_at(
const size_t slot,
const size_t bit_idx)
const noexcept
133 void set_bit_at(
const size_t slot,
const size_t bit_idx,
const bool v)
noexcept
138 [[nodiscard]]
bool get_occ(
const size_t i)
const noexcept
143 [[nodiscard]]
bool get_cont(
const size_t i)
const noexcept
148 [[nodiscard]]
bool get_shft(
const size_t i)
const noexcept
172 void set_occ(
const size_t i,
const bool v)
noexcept
177 void set_cont(
const size_t i,
const bool v)
noexcept
182 void set_shft(
const size_t i,
const bool v)
noexcept
187 [[nodiscard]] uint64_t
get_rem(
const size_t i)
const noexcept
191 for (
size_t b = 0; b <
r_; ++b)
197 void set_rem(
const size_t i,
const uint64_t rem)
noexcept
200 for (
size_t b = 0; b <
r_; ++b)
204 void set_element(
const size_t i,
const bool cont,
const bool shft,
const uint64_t rem)
noexcept
222 [[nodiscard]]
size_t incr(
const size_t i)
const noexcept
227 [[nodiscard]]
size_t decr(
const size_t i)
const noexcept
232 [[nodiscard]] std::pair<size_t, uint64_t>
fingerprint(
const T &item)
const
236 const uint64_t fr = (
static_cast<uint64_t
>(
h) >>
q_) &
rem_mask();
272 const size_t prev =
decr(empty);
273 const bool occ_at_empty =
get_occ(empty);
275 const bool prev_cont =
get_cont(prev);
276 const uint64_t prev_rem =
get_rem(prev);
286 [[nodiscard]]
bool holds(
const size_t fq,
const uint64_t fr)
const noexcept
294 const uint64_t cur =
get_rem(s);
363 const bool at_run_start = (pos == s);
402 size_t nxt =
incr(curr);
411 const bool occ_at_curr =
get_occ(curr);
413 const bool nxt_cont =
get_cont(nxt)
and not promote;
414 const uint64_t nxt_rem =
get_rem(nxt);
418 set_element(curr, nxt_cont, nxt_cont or quot != curr, nxt_rem);
439 ah_domain_error_if(q < 1 or q > 32) <<
"Compact_Quotient_Filter: q must be in [1, 32] (got " <<
static_cast<int>(
q)
441 ah_domain_error_if(r < 1 or r > 60) <<
"Compact_Quotient_Filter: r must be in [1, 60] (got " <<
static_cast<int>(
r)
443 ah_domain_error_if(
q +
r > 64) <<
"Compact_Quotient_Filter: q + r must be <= 64 (got " <<
static_cast<int>(
q +
r)
509 ah_domain_error_if(expected_n == 0) <<
"Compact_Quotient_Filter::from_capacity: expected_n must be > 0";
511 <<
"Compact_Quotient_Filter::from_capacity: fp_rate must be in (0, 1) (got " << fp_rate <<
")";
513 const auto [q_v, r_v] =
estimate(expected_n, fp_rate);
529 <<
"Compact_Quotient_Filter::insert: filter is full (" <<
num_elems_ <<
"/" <<
num_slots_ <<
")";
537 [[nodiscard]]
bool contains(
const T &item)
const noexcept
540 return holds(fq, fr);
568 const bool was_run_start = not
get_cont(pos);
569 const size_t nxt =
incr(pos);
572 if (was_run_start
and not nxt_is_cont)
583 [[nodiscard]]
size_t size() const noexcept
607 [[nodiscard]] uint8_t
q() const noexcept
613 [[nodiscard]] uint8_t
r() const noexcept
621 return std::ldexp(1.0, -
static_cast<int>(
r_));
625 [[nodiscard]] uint32_t
seed() const noexcept
640 for (
size_t i = 0; i < total_bits; ++i)
678 if (not
holds(fq, fr))
682 <<
"Compact_Quotient_Filter::merge: " << missing <<
" elements of the other filter do not fit in the "
683 << room <<
" free slots of this one";
704 [[nodiscard]]
static std::pair<uint8_t, uint8_t>
estimate(
size_t n,
double fp_rate)
708 <<
"Compact_Quotient_Filter::estimate: fp_rate must be in (0, 1) (got " << fp_rate <<
")";
713 const double slots =
static_cast<double>(n) / 0.75;
714 const int qv =
static_cast<int>(std::max(1.0, std::ceil(std::log2(slots))));
715 const int rv = -std::ilogb(fp_rate);
717 ah_domain_error_if(rv > 60) <<
"Compact_Quotient_Filter::estimate: a rate of " << fp_rate <<
" asks for " << rv
718 <<
" remainder bits, and a filter has 60 at most";
719 ah_domain_error_if(qv > 32) <<
"Compact_Quotient_Filter::estimate: " << n <<
" elements ask for 2^" << qv
720 <<
" slots, and a filter has 2^32 at most";
721 ah_domain_error_if(qv + rv > 64) <<
"Compact_Quotient_Filter::estimate: " << n <<
" elements at a rate of "
722 << fp_rate <<
" ask for " << qv + rv <<
" bits of the hash, which has 64";
723 return {
static_cast<uint8_t
>(qv),
static_cast<uint8_t
>(rv)};
729 std::swap(
q_, other.q_);
730 std::swap(
r_, other.r_);
733 std::swap(
seed_, other.seed_);
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Space-efficient bit array implementation.
Contiguous array of bits.
void fast_write(const size_t i, const unsigned int value)
Write bit i without checking i.
int fast_read(const size_t i) const noexcept
Read bit i without any check.
void reserve(const size_t dim)
Reserve memory in advance for the bit array dim dimension.
void swap(BitArray &array) noexcept
Memory-optimized quotient filter with bit-packed slots.
Compact_Quotient_Filter & operator=(Compact_Quotient_Filter &&other) noexcept
Exchange this filter with another, which is left with the previous state of this one.
Compact_Quotient_Filter(const Compact_Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
bool is_run_start(const size_t i) const noexcept
size_t find_run_start(const size_t fq) const noexcept
void set_occ(const size_t i, const bool v) noexcept
bool get_occ(const size_t i) const noexcept
size_t bits_per_slot() const noexcept
double load_factor() const noexcept
Current load factor.
size_t find_first_unused(const size_t start) const noexcept
Insert_Result do_insert(const size_t fq, const uint64_t fr)
void set_cont(const size_t i, const bool v) noexcept
size_t memory_usage() const noexcept
Memory usage in bytes.
void swap(Compact_Quotient_Filter &other) noexcept
Swap in O(1).
uint32_t seed() const noexcept
Hash seed.
Compact_Quotient_Filter(Compact_Quotient_Filter &&other)
Take the elements of another filter, which is left as an empty filter of two slots (q = 1),...
Compact_Quotient_Filter(uint8_t q, uint8_t r, uint32_t seed=0x5F3759DF)
Construct a compact quotient filter.
void clear_element(const size_t i) noexcept
static std::pair< uint8_t, uint8_t > estimate(size_t n, double fp_rate)
The quotient and the remainder bits of a filter for n elements and a target FP rate.
uint8_t q() const noexcept
Quotient bits.
size_t capacity() const noexcept
Number of slots (2^q).
void shift_elements_left(const size_t pos, size_t quot, bool promote) noexcept
void set_bit_at(const size_t slot, const size_t bit_idx, const bool v) noexcept
void for_each_fingerprint(Op op) const
static Compact_Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
Construct from desired capacity and false positive rate.
bool slot_empty(const size_t i) const noexcept
bool is_empty() const noexcept
True if empty.
size_t decr(const size_t i) const noexcept
uint64_t rem_mask() const noexcept
size_t incr(const size_t i) const noexcept
void clear() noexcept
Remove all elements.
bool holds(const size_t fq, const uint64_t fr) const noexcept
Compact_Quotient_Filter & insert(const T &item)
Insert an element.
bool get_cont(const size_t i) const noexcept
size_t size() const noexcept
Number of elements.
bool get_bit_at(const size_t slot, const size_t bit_idx) const noexcept
void merge(const Compact_Quotient_Filter &other)
Merge another filter (same q, r, seed).
bool remove(const T &item) noexcept
Remove an element.
void set_shft(const size_t i, const bool v) noexcept
uint8_t r() const noexcept
Remainder bits.
std::pair< size_t, uint64_t > fingerprint(const T &item) const
size_t slot_offset(const size_t i) const noexcept
void shift_elements_right(const size_t pos, size_t empty) noexcept
bool contains(const T &item) const noexcept
Test membership.
void set_rem(const size_t i, const uint64_t rem) noexcept
uint64_t get_rem(const size_t i) const noexcept
Compact_Quotient_Filter & operator=(const Compact_Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
void set_element(const size_t i, const bool cont, const bool shft, const uint64_t rem) noexcept
bool get_shft(const size_t i) const noexcept
bool has_element(const size_t i) const noexcept
double false_positive_rate() const noexcept
Theoretical FP probability ~ 2^{-r}.
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
size_t murmur3hash(const Key &key, std::uint32_t seed)