ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BloomVariants.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File BloomVariants.h --------------------------*/
3/*--------------------------------------------------------------------------*/
83/*--------------------------------------------------------------------------*/
84/*----------------------------- DEFINITIONS --------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87#ifndef __ADS_BloomVariants
88 #define __ADS_BloomVariants
89/* self-identification: #endif constant */
90
91/*--------------------------------------------------------------------------*/
92/*------------------------------ INCLUDES ----------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95#include "ADS/Contract.h"
98#include "ADS/set/BloomFilter.h"
99
100#include <algorithm>
101#include <cmath>
102#include <cstddef>
103#include <cstdint>
104#include <functional>
105#include <limits>
106#include <string_view>
107#include <unordered_set>
108#include <utility>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
122[[nodiscard]] inline double bloomFalsePositive( double m , double n , int r );
123
142[[nodiscard]] inline int bloomBestHashes( double m , double n );
143
147[[nodiscard]] inline double bloomLowerBound( double n , double epsilon );
148
153[[nodiscard]] inline double bloomBits( double n , double epsilon );
154
171[[nodiscard]] inline std::pair< double , double >
172compressedBloom( double bitsPerKey , int r );
173
196template< class T , class H = std::hash< T > >
197[[nodiscard]] std::pair< std::vector< T > , std::vector< T > >
198bloomIntersection( const std::vector< T > & A , const std::vector< T > & B ,
199 std::size_t mA );
200
201/*--------------------------------------------------------------------------*/
202/*----------------------- CLASS SpectralBloomFilter ------------------------*/
203/*--------------------------------------------------------------------------*/
204
207template< class T , class C = std::uint32_t , class H = std::hash< T > >
209{
210
211/*--------------------------------------------------------------------------*/
212 public:
215
218 SpectralBloomFilter( std::size_t m , int r , H h = H() );
219
234 void insert( const T & x , C f = 1 );
235
249 void remove( const T & x , C f = 1 );
250
262 [[nodiscard]] C count( const T & x ) const;
263
266 [[nodiscard]] bool maybe( const T & x ) const;
267
269 [[nodiscard]] std::size_t stuck( void ) const;
270
272 [[nodiscard]] std::size_t counters( void ) const;
273
275 [[nodiscard]] int hashes( void ) const;
276
277/*----------------------------- THE PERSISTENCE ----------------------------*/
278
280 static constexpr std::string_view persistName = "SpectralBloomFilter";
281
283 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
284 return( detail::persistMix( { sizeof( T ) , sizeof( C ) } ) );
285 }
286
289 template< class A >
290 void persist( A & a );
291
292/*--------------------------------------------------------------------------*/
293 private:
295 [[nodiscard]] std::vector< std::size_t > places( const T & x ) const;
296
297 std::vector< C > f_C;
298 int f_r;
299 H f_h;
300
301 }; // end( class SpectralBloomFilter )
302
303/*--------------------------------------------------------------------------*/
304/*---------------------- CLASS PartitionedBloomFilter ----------------------*/
305/*--------------------------------------------------------------------------*/
306
309template< class T , class H = std::hash< T > >
311{
312
313/*--------------------------------------------------------------------------*/
314 public:
317
320 PartitionedBloomFilter( std::size_t m , int r , H h = H() );
321
334 void insert( const T & x );
335
338 [[nodiscard]] bool maybe( const T & x ) const;
339
341 [[nodiscard]] std::size_t ones( void ) const;
342
344 [[nodiscard]] std::size_t bits( void ) const;
345
346/*----------------------------- THE PERSISTENCE ----------------------------*/
347
349 static constexpr std::string_view persistName = "PartitionedBloomFilter";
350
352 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
353 return( detail::persistMix( { sizeof( T ) } ) );
354 }
355
358 template< class A >
359 void persist( A & a );
360
361/*--------------------------------------------------------------------------*/
362 private:
365 [[nodiscard]] detail::BloomProbe probe( const T & x ) const;
366
367 std::vector< bool > f_B;
368 std::size_t f_w;
369 int f_r;
370 H f_h;
371
372 }; // end( class PartitionedBloomFilter )
373
374/*--------------------------------------------------------------------------*/
375
376} // end( namespace ADS )
377
378/*--------------------------------------------------------------------------*/
379/*-------------------------- IMPLEMENTATION --------------------------------*/
380/*--------------------------------------------------------------------------*/
381
382#include "ADS/set/impl/BloomVariants.tpp"
383
384/*--------------------------------------------------------------------------*/
385
386#endif /* BloomVariants.h included */
387
388/*--------------------------------------------------------------------------*/
389/*------------------------ End File BloomVariants.h ------------------------*/
390/*--------------------------------------------------------------------------*/
The filter of Bloom, beyond the book: a set that may answer yes for an item it does not hold,...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
Hashing with random tables, beyond the book:
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition BloomVariants.h:349
PartitionedBloomFilter(std::size_t m, int r, H h=H())
the empty filter of about bits in parts
bool maybe(const T &x) const
whether may be in the set, its bit of each part read: false only if was never inserted
std::size_t bits(void) const
the bits, times those of a part
std::size_t ones(void) const
the bits set
PartitionedBloomFilter(void)
a filter of 64 bits and one part, empty
Definition BloomVariants.h:316
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
void insert(const T &x)
into the filter, one bit in each part
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition BloomVariants.h:352
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition BloomVariants.h:283
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
int hashes(void) const
the hashes
std::size_t stuck(void) const
the counters at their largest value, which no longer move
void remove(const T &x, C f=1)
subtracted from the multiplicity of ; a stuck counter stays
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition BloomVariants.h:280
void insert(const T &x, C f=1)
added to the multiplicity of
C count(const T &x) const
the least counter of (the minimum selection), never below its multiplicity
SpectralBloomFilter(void)
a filter of 64 counters and one hash, empty
Definition BloomVariants.h:214
SpectralBloomFilter(std::size_t m, int r, H h=H())
the empty filter of counters and hashes
bool maybe(const T &x) const
whether may be there, count( x ) : false only if is not there
std::size_t counters(void) const
the counters
double bloomFalsePositive(double m, double n, int r)
the probability of a false positive of a filter of bits and hashes after insertions,...
double bloomLowerBound(double n, double epsilon)
, the bits that any filter of keys with false positives needs
std::pair< std::vector< T >, std::vector< T > > bloomIntersection(const std::vector< T > &A, const std::vector< T > &B, std::size_t mA)
the intersection of and by a filter of of bits: the keys of that the filter accepts,...
double bloomBits(double n, double epsilon)
, the bits of a filter of Bloom with the best number of hashes and false positives
int bloomBestHashes(double m, double n)
the number of hashes, the floor or the ceiling of , of the least false positives; past the largest in...
std::pair< double, double > compressedBloom(double bitsPerKey, int r)
the false positives and the bits per key after an arithmetic coding, times the entropy of a bit,...
Definition ADT.h:96