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

Quotient filter for probabilistic set membership with deletion. More...

#include <quotient-filter.H>

Collaboration diagram for Aleph::Quotient_Filter< T >:
[legend]

Public Member Functions

 Quotient_Filter (uint8_t q, uint8_t r, uint32_t seed=0x5F3759DF)
 Construct a quotient filter.
 
 Quotient_Filter (const Quotient_Filter &other)=default
 Copy the parameters and the slots of another filter.
 
 Quotient_Filter (Quotient_Filter &&other)
 Take the elements of another filter, which is left as an empty filter of two slots (q = 1), with its remainder bits and seed.
 
Quotient_Filter & operator= (const Quotient_Filter &other)=default
 Copy the parameters and the slots of another filter.
 
Quotient_Filter & operator= (Quotient_Filter &&other) noexcept
 Exchange this filter with another, which is left with the previous state of this one.
 
Quotient_Filter & insert (const T &item)
 Insert an element.
 
bool contains (const T &item) const noexcept
 Test membership.
 
bool remove (const T &item) noexcept
 Remove an element.
 
size_t size () const noexcept
 Number of elements.
 
size_t capacity () const noexcept
 Number of slots (2^q).
 
double load_factor () const noexcept
 Current load factor.
 
bool is_empty () const noexcept
 True if empty.
 
uint8_t q () const noexcept
 Quotient bits.
 
uint8_t r () const noexcept
 Remainder bits.
 
double false_positive_rate () const noexcept
 Theoretical FP probability ~ 2^{-r}.
 
uint32_t seed () const noexcept
 Hash seed.
 
size_t memory_usage () const noexcept
 Memory usage in bytes.
 
void clear () noexcept
 Remove all elements.
 
void merge (const Quotient_Filter &other)
 Merge another filter (same q, r, seed).
 
void swap (Quotient_Filter &other) noexcept
 Swap in O(1).
 

Static Public Member Functions

static Quotient_Filter from_capacity (size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
 Construct from desired capacity and false positive rate.
 
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.
 

Private Types

enum class  Insert_Result { inserted , duplicate , full }
 

Private Member Functions

uint64_t rem_mask () const noexcept
 
bool get_occ (size_t i) const noexcept
 
bool get_cont (size_t i) const noexcept
 
bool get_shft (size_t i) const noexcept
 
bool slot_empty (size_t i) const noexcept
 
bool has_element (const size_t i) const noexcept
 
bool is_run_start (const size_t i) const noexcept
 
void set_occ (const size_t i, const bool v) noexcept
 
void set_cont (const size_t i, const bool v) noexcept
 
void set_shft (const size_t i, const bool v) noexcept
 
uint64_t get_rem (const size_t i) const noexcept
 
void set_rem (const size_t i, const uint64_t rem) noexcept
 
void set_element (const size_t i, const bool cont, const bool shft, const uint64_t rem) noexcept
 
void clear_element (const size_t i) noexcept
 
size_t incr (const size_t i) const noexcept
 
size_t decr (const size_t i) const noexcept
 
std::pair< size_t, uint64_t > fingerprint (const T &item) const
 
size_t find_run_start (size_t fq) const noexcept
 
size_t find_first_unused (size_t start) const noexcept
 
void shift_elements_right (size_t pos, size_t empty) noexcept
 
bool holds (const size_t fq, const uint64_t fr) const noexcept
 
template<class Op >
void for_each_fingerprint (Op op) const
 
Insert_Result do_insert (size_t fq, uint64_t fr)
 
void shift_elements_left (size_t pos, size_t quot, bool promote) noexcept
 

Private Attributes

uint8_t q_
 
uint8_t r_
 
size_t num_slots_
 
size_t num_elems_
 
uint32_t seed_
 
Array< uint64_t > slots_
 

Static Private Attributes

static constexpr uint64_t OCC_BIT = 1ULL << 0
 
static constexpr uint64_t CONT_BIT = 1ULL << 1
 
static constexpr uint64_t SHFT_BIT = 1ULL << 2
 
static constexpr uint64_t META_MASK = 0x7ULL
 

Detailed Description

template<typename T>
class Aleph::Quotient_Filter< T >

Quotient filter for probabilistic set membership with deletion.

Template Parameters
TElement type. Must be hashable via murmur3hash(T, seed).
Note
A growing std::vector copies these objects instead of moving them: their move constructor is not noexcept, because it leaves the source empty and usable, which allocates, and std::vector moves only what cannot throw. DynList holds them without relocating them, so it never copies them; Array and DynArray cannot hold them, as they build their items by default and these objects have no default constructor.

Definition at line 144 of file quotient-filter.H.

Member Enumeration Documentation

◆ Insert_Result

template<typename T >
enum class Aleph::Quotient_Filter::Insert_Result
strongprivate
Enumerator
inserted 
duplicate 
full 

Definition at line 422 of file quotient-filter.H.

Constructor & Destructor Documentation

◆ Quotient_Filter() [1/3]

template<typename T >
Aleph::Quotient_Filter< T >::Quotient_Filter ( uint8_t  q,
uint8_t  r,
uint32_t  seed = 0x5F3759DF 
)
inline

Construct a quotient filter.

Parameters
[in]qNumber of quotient bits (2^q slots). Must be in [1, 32].
[in]rRemainder bits per slot (FP rate ~ 2^{-r}). Must be in [1, 60].
[in]seedHash seed (default: 0x5F3759DF).
Exceptions
std::domain_errorif q or r are out of range, or q + r > 64.

Definition at line 549 of file quotient-filter.H.

References ah_domain_error_if, Aleph::Quotient_Filter< T >::num_slots_, Aleph::Quotient_Filter< T >::q(), Aleph::Quotient_Filter< T >::r(), and Aleph::Quotient_Filter< T >::slots_.

◆ Quotient_Filter() [2/3]

template<typename T >
Aleph::Quotient_Filter< T >::Quotient_Filter ( const Quotient_Filter< T > &  other)
default

Copy the parameters and the slots of another filter.

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

◆ Quotient_Filter() [3/3]

template<typename T >
Aleph::Quotient_Filter< T >::Quotient_Filter ( Quotient_Filter< T > &&  other)
inline

Take the elements of another filter, which is left as an empty filter of two slots (q = 1), with its remainder bits and seed.

The implicit move took the slots but copied their count: the moved-from filter then read slots that it no longer had, through checked accesses in functions that are noexcept. With two slots, every operation of other works as in any filter, with no test for a filter without slots.

Parameters
[in,out]otherfilter to move from.
Exceptions
std::bad_allocif the slots left to other cannot be allocated; other is then unchanged.
Note
O(1): it allocates the two slots left to other.

Definition at line 580 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::swap().

Member Function Documentation

◆ capacity()

template<typename T >
size_t Aleph::Quotient_Filter< T >::capacity ( ) const
inlinenoexcept

Number of slots (2^q).

O(1).

Definition at line 698 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_slots_.

Referenced by demo_basic(), and TEST().

◆ clear()

template<typename T >
void Aleph::Quotient_Filter< T >::clear ( )
inlinenoexcept

Remove all elements.

O(2^q).

Definition at line 746 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_elems_, Aleph::Quotient_Filter< T >::num_slots_, and Aleph::Quotient_Filter< T >::slots_.

Referenced by TEST().

◆ clear_element()

template<typename T >
void Aleph::Quotient_Filter< T >::clear_element ( const size_t  i)
inlineprivatenoexcept

◆ contains()

template<typename T >
bool Aleph::Quotient_Filter< T >::contains ( const T &  item) const
inlinenoexcept

Test membership.

May return false positives (~2^{-r}), never false negatives.

Definition at line 645 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::fingerprint(), and Aleph::Quotient_Filter< T >::holds().

Referenced by demo_basic(), demo_fp(), demo_merge(), example_performance(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ decr()

template<typename T >
size_t Aleph::Quotient_Filter< T >::decr ( const size_t  i) const
inlineprivatenoexcept

◆ do_insert()

◆ estimate()

template<typename T >
static std::pair< uint8_t, uint8_t > Aleph::Quotient_Filter< T >::estimate ( size_t  n,
double  fp_rate 
)
inlinestatic

The quotient and the remainder bits of a filter for n elements and a target FP rate.

The slots are the least power of two that holds n elements at a load of 75 % or less, and the remainder bits are the fewest that give the rate.

Parameters
[in]nExpected number of elements.
[in]fp_rateTarget false positive probability.
Returns
The pair (q, r), as the constructor takes them.
Exceptions
std::domain_errorif n == 0, if fp_rate is not in (0, 1), or if no filter has the two: the rate asks for more than 60 remainder bits, the elements for more than 2^32 slots, or the two together for more than the 64 bits of the hash.

Definition at line 812 of file quotient-filter.H.

References ah_domain_error_if, and Aleph::and.

Referenced by Aleph::Quotient_Filter< T >::from_capacity(), and TEST().

◆ false_positive_rate()

template<typename T >
double Aleph::Quotient_Filter< T >::false_positive_rate ( ) const
inlinenoexcept

Theoretical FP probability ~ 2^{-r}.

Definition at line 728 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::r_.

Referenced by demo_basic(), demo_fp(), TEST(), and TEST().

◆ find_first_unused()

template<typename T >
size_t Aleph::Quotient_Filter< T >::find_first_unused ( size_t  start) const
inlineprivatenoexcept

◆ find_run_start()

◆ fingerprint()

◆ for_each_fingerprint()

◆ from_capacity()

template<typename T >
static Quotient_Filter Aleph::Quotient_Filter< T >::from_capacity ( size_t  expected_n,
double  fp_rate,
uint32_t  seed = 0x5F3759DF 
)
inlinestatic

Construct from desired capacity and false positive rate.

The filter has the quotient and the remainder bits that estimate() gives: slots for expected_n elements at a load of 75 % or less.

Parameters
[in]expected_nExpected number of elements.
[in]fp_rateTarget false positive probability.
[in]seedHash seed.
Exceptions
std::domain_errorif expected_n == 0, if fp_rate is not in (0, 1), or if no filter has the two, as estimate() says.

Definition at line 615 of file quotient-filter.H.

References ah_domain_error_if, Aleph::and, Aleph::Quotient_Filter< T >::estimate(), and Aleph::Quotient_Filter< T >::seed().

Referenced by demo_cache(), and TEST().

◆ get_cont()

◆ get_occ()

◆ get_rem()

◆ get_shft()

template<typename T >
bool Aleph::Quotient_Filter< T >::get_shft ( size_t  i) const
inlineprivatenoexcept

◆ has_element()

◆ holds()

◆ incr()

◆ insert()

template<typename T >
Quotient_Filter & Aleph::Quotient_Filter< T >::insert ( const T &  item)
inline

Insert an element.

After insertion, contains(item) is guaranteed true. Duplicates are silently ignored, in a full filter too.

Exceptions
std::overflow_errorif the filter is full and does not hold the element.

Definition at line 633 of file quotient-filter.H.

References ah_overflow_error_if, Aleph::Quotient_Filter< T >::do_insert(), Aleph::Quotient_Filter< T >::fingerprint(), Aleph::Quotient_Filter< T >::full, Aleph::Quotient_Filter< T >::num_elems_, and Aleph::Quotient_Filter< T >::num_slots_.

Referenced by demo_basic(), demo_fp(), demo_merge(), example_performance(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ is_empty()

template<typename T >
bool Aleph::Quotient_Filter< T >::is_empty ( ) const
inlinenoexcept

True if empty.

O(1).

Definition at line 710 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_elems_.

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

◆ is_run_start()

template<typename T >
bool Aleph::Quotient_Filter< T >::is_run_start ( const size_t  i) const
inlineprivatenoexcept

◆ load_factor()

template<typename T >
double Aleph::Quotient_Filter< T >::load_factor ( ) const
inlinenoexcept

Current load factor.

O(1).

Definition at line 704 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_elems_, and Aleph::Quotient_Filter< T >::num_slots_.

Referenced by TEST().

◆ memory_usage()

template<typename T >
size_t Aleph::Quotient_Filter< T >::memory_usage ( ) const
inlinenoexcept

Memory usage in bytes.

Definition at line 740 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_slots_.

Referenced by demo_basic(), example_memory_optimization(), example_performance(), TEST(), and TEST().

◆ merge()

template<typename T >
void Aleph::Quotient_Filter< T >::merge ( const Quotient_Filter< T > &  other)
inline

Merge another filter (same q, r, seed).

The filter is left with its elements and those of other, each one once: an element that the two hold takes no slot.

It takes O(2^q) on average at the loads that a filter is made for. Each element costs the length of its cluster, so the time grows up to O(4^q) as the filters fill. When the sizes of the two filters say that the elements of other may not fit, they are first looked for in this filter, which is a second pass over other.

Parameters
[in]otherfilter whose elements are taken; it is not changed.
Exceptions
std::domain_erroron parameter mismatch.
std::overflow_errorif the elements of other that this filter does not hold are more than its free slots. The filter is then left as it was.

Definition at line 770 of file quotient-filter.H.

References ah_domain_error_if, ah_overflow_error_if, Aleph::Quotient_Filter< T >::do_insert(), Aleph::Quotient_Filter< T >::for_each_fingerprint(), Aleph::Quotient_Filter< T >::holds(), Aleph::Quotient_Filter< T >::num_elems_, Aleph::Quotient_Filter< T >::num_slots_, Aleph::Quotient_Filter< T >::q_, Aleph::Quotient_Filter< T >::r_, and Aleph::Quotient_Filter< T >::seed_.

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

◆ operator=() [1/2]

template<typename T >
Quotient_Filter & Aleph::Quotient_Filter< T >::operator= ( const Quotient_Filter< T > &  other)
default

Copy the parameters and the slots of another filter.

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

◆ operator=() [2/2]

template<typename T >
Quotient_Filter & Aleph::Quotient_Filter< T >::operator= ( Quotient_Filter< T > &&  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
O(1).

Definition at line 598 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::swap().

◆ q()

template<typename T >
uint8_t Aleph::Quotient_Filter< T >::q ( ) const
inlinenoexcept

Quotient bits.

Definition at line 716 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::q_.

Referenced by Aleph::Quotient_Filter< T >::Quotient_Filter(), TEST(), and TEST().

◆ r()

template<typename T >
uint8_t Aleph::Quotient_Filter< T >::r ( ) const
inlinenoexcept

Remainder bits.

Definition at line 722 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::r_.

Referenced by Aleph::Quotient_Filter< T >::Quotient_Filter(), demo_basic(), TEST(), and TEST().

◆ rem_mask()

◆ remove()

◆ seed()

template<typename T >
uint32_t Aleph::Quotient_Filter< T >::seed ( ) const
inlinenoexcept

Hash seed.

Definition at line 734 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::seed_.

Referenced by Aleph::Quotient_Filter< T >::from_capacity().

◆ set_cont()

template<typename T >
void Aleph::Quotient_Filter< T >::set_cont ( const size_t  i,
const bool  v 
)
inlineprivatenoexcept

◆ set_element()

template<typename T >
void Aleph::Quotient_Filter< T >::set_element ( const size_t  i,
const bool  cont,
const bool  shft,
const uint64_t  rem 
)
inlineprivatenoexcept

◆ set_occ()

◆ set_rem()

template<typename T >
void Aleph::Quotient_Filter< T >::set_rem ( const size_t  i,
const uint64_t  rem 
)
inlineprivatenoexcept

◆ set_shft()

template<typename T >
void Aleph::Quotient_Filter< T >::set_shft ( const size_t  i,
const bool  v 
)
inlineprivatenoexcept

◆ shift_elements_left()

◆ shift_elements_right()

template<typename T >
void Aleph::Quotient_Filter< T >::shift_elements_right ( size_t  pos,
size_t  empty 
)
inlineprivatenoexcept

◆ size()

template<typename T >
size_t Aleph::Quotient_Filter< T >::size ( ) const
inlinenoexcept

Number of elements.

O(1).

Definition at line 692 of file quotient-filter.H.

References Aleph::Quotient_Filter< T >::num_elems_.

Referenced by demo_basic(), demo_merge(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ slot_empty()

template<typename T >
bool Aleph::Quotient_Filter< T >::slot_empty ( size_t  i) const
inlineprivatenoexcept

◆ swap()

Member Data Documentation

◆ CONT_BIT

template<typename T >
constexpr uint64_t Aleph::Quotient_Filter< T >::CONT_BIT = 1ULL << 1
staticconstexprprivate

◆ META_MASK

template<typename T >
constexpr uint64_t Aleph::Quotient_Filter< T >::META_MASK = 0x7ULL
staticconstexprprivate

◆ num_elems_

◆ num_slots_

◆ OCC_BIT

◆ q_

◆ r_

◆ seed_

◆ SHFT_BIT

template<typename T >
constexpr uint64_t Aleph::Quotient_Filter< T >::SHFT_BIT = 1ULL << 2
staticconstexprprivate

◆ slots_


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