118#ifndef QUOTIENT_FILTER_H
119#define QUOTIENT_FILTER_H
159 static constexpr uint64_t
OCC_BIT = 1ULL << 0;
166 return (1ULL <<
r_) - 1;
169 [[nodiscard]]
bool get_occ(
size_t i)
const noexcept
174 [[nodiscard]]
bool get_cont(
size_t i)
const noexcept
179 [[nodiscard]]
bool get_shft(
size_t i)
const noexcept
268 void set_occ(
const size_t i,
const bool v)
noexcept
276 void set_cont(
const size_t i,
const bool v)
noexcept
284 void set_shft(
const size_t i,
const bool v)
noexcept
292 [[nodiscard]] uint64_t
get_rem(
const size_t i)
const noexcept
297 void set_rem(
const size_t i,
const uint64_t rem)
noexcept
304 void set_element(
const size_t i,
const bool cont,
const bool shft,
const uint64_t rem)
noexcept
317 [[nodiscard]]
size_t incr(
const size_t i)
const noexcept
322 [[nodiscard]]
size_t decr(
const size_t i)
const noexcept
327 [[nodiscard]] std::pair<size_t, uint64_t>
fingerprint(
const T &item)
const
331 const uint64_t fr = (
static_cast<uint64_t
>(
h) >>
q_) &
rem_mask();
376 const size_t prev =
decr(empty);
377 const bool occ_at_empty =
get_occ(empty);
386 [[nodiscard]]
bool holds(
const size_t fq,
const uint64_t fr)
const noexcept
394 const uint64_t cur =
get_rem(s);
450 const uint64_t cur =
get_rem(pos);
468 const bool at_run_start = (pos == s);
511 size_t nxt =
incr(curr);
520 const bool occ_at_curr =
get_occ(curr);
552 ah_domain_error_if(q < 1 or q > 32) <<
"Quotient_Filter: q must be in [1, 32] (got " <<
static_cast<int>(
q) <<
")";
553 ah_domain_error_if(r < 1 or r > 60) <<
"Quotient_Filter: r must be in [1, 60] (got " <<
static_cast<int>(
r) <<
")";
554 ah_domain_error_if(
q +
r > 64) <<
"Quotient_Filter: q + r must be <= 64 (got " <<
static_cast<int>(
q +
r) <<
")";
617 ah_domain_error_if(expected_n == 0) <<
"Quotient_Filter::from_capacity: expected_n must be > 0";
619 <<
"Quotient_Filter::from_capacity: fp_rate must be in (0, 1) (got " << fp_rate <<
")";
621 const auto [q_v, r_v] =
estimate(expected_n, fp_rate);
645 [[nodiscard]]
bool contains(
const T &item)
const noexcept
648 return holds(fq, fr);
677 const bool was_run_start = not
get_cont(pos);
678 size_t nxt =
incr(pos);
681 if (was_run_start
and not nxt_is_cont)
692 [[nodiscard]]
size_t size() const noexcept
716 [[nodiscard]] uint8_t
q() const noexcept
722 [[nodiscard]] uint8_t
r() const noexcept
730 return std::ldexp(1.0, -
static_cast<int>(
r_));
734 [[nodiscard]] uint32_t
seed() const noexcept
786 if (not
holds(fq, fr))
790 <<
"Quotient_Filter::merge: " << missing <<
" elements of the other filter do not fit in the " << room
791 <<
" free slots of this one";
812 [[nodiscard]]
static std::pair<uint8_t, uint8_t>
estimate(
size_t n,
double fp_rate)
816 <<
"Quotient_Filter::estimate: fp_rate must be in (0, 1) (got " << fp_rate <<
")";
821 const double slots =
static_cast<double>(n) / 0.75;
822 const int qv =
static_cast<int>(std::max(1.0, std::ceil(std::log2(slots))));
823 const int rv = -std::ilogb(fp_rate);
825 ah_domain_error_if(rv > 60) <<
"Quotient_Filter::estimate: a rate of " << fp_rate <<
" asks for " << rv
826 <<
" remainder bits, and a filter has 60 at most";
827 ah_domain_error_if(qv > 32) <<
"Quotient_Filter::estimate: " << n <<
" elements ask for 2^" << qv
828 <<
" slots, and a filter has 2^32 at most";
829 ah_domain_error_if(qv + rv > 64) <<
"Quotient_Filter::estimate: " << n <<
" elements at a rate of " << fp_rate
830 <<
" ask for " << qv + rv <<
" bits of the hash, which has 64";
831 return {
static_cast<uint8_t
>(qv),
static_cast<uint8_t
>(rv)};
837 std::swap(
q_, other.q_);
838 std::swap(
r_, other.r_);
841 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.
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
Quotient filter for probabilistic set membership with deletion.
bool get_occ(size_t i) const noexcept
static constexpr uint64_t CONT_BIT
bool holds(const size_t fq, const uint64_t fr) const noexcept
Quotient_Filter(Quotient_Filter &&other)
Take the elements of another filter, which is left as an empty filter of two slots (q = 1),...
void shift_elements_left(size_t pos, size_t quot, bool promote) noexcept
size_t decr(const size_t i) const noexcept
size_t incr(const size_t i) const noexcept
Quotient_Filter & insert(const T &item)
Insert an element.
static Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
Construct from desired capacity and false positive rate.
double load_factor() const noexcept
Current load factor.
Quotient_Filter(const Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
bool has_element(const size_t i) const noexcept
size_t memory_usage() const noexcept
Memory usage in bytes.
uint8_t q() const noexcept
Quotient bits.
bool get_shft(size_t i) const noexcept
bool is_empty() const noexcept
True if empty.
void set_rem(const size_t i, const uint64_t rem) noexcept
double false_positive_rate() const noexcept
Theoretical FP probability ~ 2^{-r}.
Quotient_Filter & operator=(Quotient_Filter &&other) noexcept
Exchange this filter with another, which is left with the previous state of this one.
void set_shft(const size_t i, const bool v) noexcept
Insert_Result do_insert(size_t fq, uint64_t fr)
Quotient_Filter & operator=(const Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
size_t capacity() const noexcept
Number of slots (2^q).
uint8_t r() const noexcept
Remainder bits.
uint64_t get_rem(const size_t i) const noexcept
void set_occ(const size_t i, const bool v) noexcept
void merge(const Quotient_Filter &other)
Merge another filter (same q, r, seed).
size_t find_first_unused(size_t start) const noexcept
uint64_t rem_mask() const noexcept
bool is_run_start(const size_t i) const noexcept
size_t size() const noexcept
Number of elements.
bool contains(const T &item) const noexcept
Test membership.
bool get_cont(size_t i) const noexcept
void for_each_fingerprint(Op op) const
void set_element(const size_t i, const bool cont, const bool shft, const uint64_t rem) noexcept
std::pair< size_t, uint64_t > fingerprint(const T &item) const
static constexpr uint64_t SHFT_BIT
static constexpr uint64_t OCC_BIT
static constexpr uint64_t META_MASK
uint32_t seed() const noexcept
Hash seed.
bool slot_empty(size_t i) const noexcept
Quotient_Filter(uint8_t q, uint8_t r, uint32_t seed=0x5F3759DF)
Construct a quotient filter.
void shift_elements_right(size_t pos, size_t empty) 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.
size_t find_run_start(size_t fq) const noexcept
void set_cont(const size_t i, const bool v) noexcept
bool remove(const T &item) noexcept
Remove an element.
void clear() noexcept
Remove all elements.
void clear_element(const size_t i) noexcept
void swap(Quotient_Filter &other) noexcept
Swap in O(1).
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)
Dynamic array container with automatic resizing.