ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BloomFilter< T, H > Class Template Reference

a filter of Bloom of \( m \) bits and \( k \) hashes More...

#include <ADS/set/BloomFilter.h>

Public member functions

 BloomFilter (void)
 a filter of 64 bits and one hash, empty
 BloomFilter (std::size_t m, int k, H h=H())
 the empty filter of \( m \) bits and \( k \) hashes
void insert (const T &x)
 \( x \) into the filter, by setting its \( k \) bits
bool maybe (const T &x) const
 whether \( x \) may be in the set: false only if \( x \) was never inserted
double falsePositiveRate (void) const
 the expected false positives after the \( n \) insertions made, \( ( 1 - e^{-k n / m} )^k \)
std::size_t bits (void) const
 the bits \( m \)
int hashes (void) const
 the hashes \( k \)
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]

Static public member functions

static BloomFilter forItems (std::size_t n, double p)
 the filter of the best \( k \) for \( n \) items and the false positives \( p \): \( m = \lceil n \ln( 1 / p ) / ( \ln 2 )^2 \rceil \) bits and \( k = ( m / n ) \ln 2 \) rounded, at least 1
static constexpr std::uint64_t persistTag (void)
 the parameters of the layout, in a file

Static public attributes

static constexpr std::string_view persistName = "BloomFilter"
 the name of the structure in a file [see Persistence.h]

Detailed description

template<class T, class H = std::hash< T >>
class ADS::BloomFilter< T, H >

a filter of Bloom of \( m \) bits and \( k \) hashes

Constructor and destructor documentation

◆ BloomFilter() [1/2]

template<class T, class H = std::hash< T >>
ADS::BloomFilter< T, H >::BloomFilter ( void )
inline

a filter of 64 bits and one hash, empty

◆ BloomFilter() [2/2]

template<class T, class H = std::hash< T >>
ADS::BloomFilter< T, H >::BloomFilter ( std::size_t m,
int k,
H h = H() )

the empty filter of \( m \) bits and \( k \) hashes

Precondition
\( m \ge 1 \) and \( k \ge 1 \)

Member function documentation

◆ bits()

template<class T, class H = std::hash< T >>
std::size_t ADS::BloomFilter< T, H >::bits ( void ) const
nodiscard

the bits \( m \)

◆ falsePositiveRate()

template<class T, class H = std::hash< T >>
double ADS::BloomFilter< T, H >::falsePositiveRate ( void ) const
nodiscard

the expected false positives after the \( n \) insertions made, \( ( 1 - e^{-k n / m} )^k \)

◆ forItems()

template<class T, class H = std::hash< T >>
BloomFilter ADS::BloomFilter< T, H >::forItems ( std::size_t n,
double p )
static

the filter of the best \( k \) for \( n \) items and the false positives \( p \): \( m = \lceil n \ln( 1 / p ) / ( \ln 2 )^2 \rceil \) bits and \( k = ( m / n ) \ln 2 \) rounded, at least 1

Precondition
\( n \ge 1 \) and \( 0 < p < 1 \)

◆ hashes()

template<class T, class H = std::hash< T >>
int ADS::BloomFilter< T, H >::hashes ( void ) const
nodiscard

the hashes \( k \)

◆ insert()

template<class T, class H = std::hash< T >>
void ADS::BloomFilter< T, H >::insert ( const T & x)

\( x \) into the filter, by setting its \( k \) bits

In pseudocode (Bloom, with the two hashes of Kirsch and Mitzenmacher), \( B \) the bits and \( h_2 \) never 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x ) \cr 2 & \quad h_1 \gets \mathrm{splitmix64}( h( x ) );\ h_2 \gets ( \mathrm{splitmix64}( h_1 ) \vee 1 ) \bmod m;\ \textbf{if}\ h_2 = 0 \ \textbf{then}\ h_2 \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \ B[ ( h_1 + i h_2 ) \bmod m ] \gets 1 \end{array}} \]

Postcondition
maybe( x )

◆ maybe()

template<class T, class H = std::hash< T >>
bool ADS::BloomFilter< T, H >::maybe ( const T & x) const
nodiscard

whether \( x \) may be in the set: false only if \( x \) was never inserted

In pseudocode (Bloom), the bits of insert() read instead of set:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maybe}( x ) \cr 2 & \quad h_1 \gets \mathrm{splitmix64}( h( x ) );\ h_2 \gets ( \mathrm{splitmix64}( h_1 ) \vee 1 ) \bmod m;\ \textbf{if}\ h_2 = 0 \ \textbf{then}\ h_2 \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ B[ ( h_1 + i h_2 ) \bmod m ] = 0\ \textbf{then return false} \cr 5 & \quad \textbf{return true} \end{array}} \]

◆ persist()

template<class T, class H = std::hash< T >>
template<class A>
void ADS::BloomFilter< T, H >::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ persistTag()

template<class T, class H = std::hash< T >>
constexpr std::uint64_t ADS::BloomFilter< T, H >::persistTag ( void )
inlinestaticnodiscardconstexpr

the parameters of the layout, in a file

Member data documentation

◆ persistName

template<class T, class H = std::hash< T >>
std::string_view ADS::BloomFilter< T, H >::persistName = "BloomFilter"
staticconstexpr

the name of the structure in a file [see Persistence.h]


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