101# ifndef BLOOM_FILTER_H
102# define BLOOM_FILTER_H
135 template <
typename T>
179 [[nodiscard]]
static std::tuple<size_t, size_t>
estimate(
const size_t n,
const double p)
184 const double log2 = std::log(2);
189 constexpr double limit = 2.0 *
static_cast<double>(std::numeric_limits<size_t>::max() / 2 + 1);
190 const double bits = std::ceil(-(n * std::log(p)) / (
log2 *
log2));
192 <<
" ask for more bits than a size_t counts";
193 const auto m =
static_cast<size_t>(
bits);
194 const auto k =
static_cast<size_t>(std::ceil((1.0 *
m / n) *
log2));
195 return std::make_tuple(
m,
k);
224 [[nodiscard]]
size_t get_x() const noexcept
227 for (
size_t i = 0; i <
bits.
size(); ++i)
240 [[nodiscard]]
size_t size() const noexcept {
return get_n(); }
264 Hash_Fct hash_fn = dft_seed_hash_ptr_fct<T>,
265 const unsigned long seed =
static_cast<unsigned long>(std::time(
nullptr)))
275 for (
auto & s:
seeds)
290 unsigned long seed =
static_cast<unsigned long>(std::time(
nullptr)),
291 Hash_Fct hash_fn = dft_seed_hash_ptr_fct<T>)
446 for (
size_t i = 0; i <
bits.
size(); ++i)
464 const auto m =
static_cast<double>(
capacity());
465 const auto k =
static_cast<double>(
get_k());
466 if (
m == 0 or
k == 0)
469 const double p0 = 1.0 - (
static_cast<double>(x) /
m);
471 return std::numeric_limits<size_t>::max();
473 const double n = std::ceil(-(
m * std::log(p0)) /
k);
474 if (not std::isfinite(n) or n < 0)
475 return std::numeric_limits<size_t>::max();
477 return static_cast<size_t>(n);
499 <<
"Bloom filter have different hashes";
503 const auto x =
get_x();
518 <<
"Bloom filter have different hashes";
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.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Functional programming utilities for Aleph-w containers.
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.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
void swap(BitArray &array) noexcept
Bloom filter implementation.
friend Bloom_Filter operator|(const Bloom_Filter &f1, const Bloom_Filter &f2)
Return the union of two compatible Bloom filters.
~Bloom_Filter()=default
Destructor.
size_t size() const noexcept
Return the number of insertions.
auto & operator&=(const Bloom_Filter &f)
In-place intersection.
DynList< size_t > common_hashes(const T &i1, const T &i2) const
Return the intersection of hashes(i1) and hashes(i2).
Bloom_Filter(const size_t n, const double p, unsigned long seed=static_cast< unsigned long >(std::time(nullptr)), Hash_Fct hash_fn=dft_seed_hash_ptr_fct< T >)
Construct a Bloom filter using a desired capacity in insertions.
friend Bloom_Filter operator&(const Bloom_Filter &f1, const Bloom_Filter &f2)
Return the intersection of two compatible Bloom filters.
size_t expected_size() const noexcept
Convenience overload.
bool have_same_hashes(const Bloom_Filter &f) const noexcept
static std::tuple< size_t, size_t > estimate(const size_t n, const double p)
Estimate the parameters of a Bloom filter.
size_t expected_size(const size_t x) const noexcept
Estimate the number of inserted items from the number of bits set.
auto & append(const T &item)
Alias of insert().
std::vector< unsigned long > seeds
Bloom_Filter(const size_t dim, size_t num_hashes, Hash_Fct hash_fn=dft_seed_hash_ptr_fct< T >, const unsigned long seed=static_cast< unsigned long >(std::time(nullptr)))
Construct a Bloom filter with explicit parameters.
size_t get_n() const noexcept
Return the number of insertions performed on the filter.
DynList< size_t > set_bits() const
Return the indexes of bits set to 1.
size_t get_m() const noexcept
Return the number of bits of the filter.
auto & operator|=(const Bloom_Filter &f)
In-place union.
size_t(*)(const T &, unsigned long seed) Hash_Fct
bool contains(const T &item) const
Test membership.
DynList< size_t > hash_seeds() const
Return the internally generated per-hash seeds.
auto & insert(const T &item)
Insert an item.
Bloom_Filter(std::tuple< size_t, size_t > mk, Hash_Fct hash_fn, unsigned long seed)
Private delegating constructor to avoid calling estimate() twice.
Bloom_Filter(const Bloom_Filter &f)
Copy constructor.
Bloom_Filter(Bloom_Filter &&f) noexcept
Move constructor.
size_t capacity() const noexcept
Return the capacity in bits.
DynList< size_t > hashes(const T &item) const
Return the bit positions used by item.
void swap(Bloom_Filter &f) noexcept
Swap this with f in O(1).
size_t get_k() const noexcept
Return the number of hash functions used by the filter.
size_t get_x() const noexcept
Return the number of bits set to 1.
auto & operator=(const Bloom_Filter &f)
Copy assignment.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
unsigned long get() noexcept
The next number, as operator()().
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic set implementations based on hash tables.