ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BloomFilter.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File BloomFilter.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
37/*--------------------------------------------------------------------------*/
38/*----------------------------- DEFINITIONS --------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#ifndef __ADS_BloomFilter
42 #define __ADS_BloomFilter
43/* self-identification: #endif constant */
44
45/*--------------------------------------------------------------------------*/
46/*------------------------------ INCLUDES ----------------------------------*/
47/*--------------------------------------------------------------------------*/
48
49#include "ADS/Contract.h"
52
53#include <cmath>
54#include <cstddef>
55#include <cstdint>
56#include <functional>
57#include <string_view>
58#include <vector>
59
60/*--------------------------------------------------------------------------*/
61/*--------------------------- NAMESPACE ------------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64namespace ADS
65{
66
67/*--------------------------------------------------------------------------*/
68/*--------------------------- CLASS BloomFilter ----------------------------*/
69/*--------------------------------------------------------------------------*/
70
72template< class T , class H = std::hash< T > >
74{
75
76/*--------------------------------------------------------------------------*/
77 public:
79 BloomFilter( void ) : BloomFilter( 64 , 1 ) { }
80
83 BloomFilter( std::size_t m , int k , H h = H() );
84
90 static BloomFilter forItems( std::size_t n , double p );
91
107 void insert( const T & x );
108
125 [[nodiscard]] bool maybe( const T & x ) const;
126
129 [[nodiscard]] double falsePositiveRate( void ) const;
130
132 [[nodiscard]] std::size_t bits( void ) const;
133
135 [[nodiscard]] int hashes( void ) const;
136
137/*----------------------------- THE PERSISTENCE ----------------------------*/
138
140 static constexpr std::string_view persistName = "BloomFilter";
141
143 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
144 return( detail::persistMix( { sizeof( T ) } ) );
145 }
146
149 template< class A >
150 void persist( A & a );
151
152/*--------------------------------------------------------------------------*/
153 private:
154 std::vector< std::uint64_t > f_B;
155 std::size_t f_m;
156 int f_k;
157 std::size_t f_n = 0;
158 H f_h;
159
160 }; // end( class BloomFilter )
161
162/*--------------------------------------------------------------------------*/
163
164} // end( namespace ADS )
165
166/*--------------------------------------------------------------------------*/
167/*-------------------------- IMPLEMENTATION --------------------------------*/
168/*--------------------------------------------------------------------------*/
169
170#include "ADS/set/impl/BloomFilter.tpp"
171
172/*--------------------------------------------------------------------------*/
173
174#endif /* BloomFilter.h included */
175
176/*--------------------------------------------------------------------------*/
177/*------------------------- End File BloomFilter.h -------------------------*/
178/*--------------------------------------------------------------------------*/
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::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition BloomFilter.h:143
int hashes(void) const
the hashes
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition BloomFilter.h:140
void insert(const T &x)
into the filter, by setting its bits
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::size_t bits(void) const
the bits
static BloomFilter forItems(std::size_t n, double p)
the filter of the best for items and the false positives : bits and rounded, at least 1
BloomFilter(void)
a filter of 64 bits and one hash, empty
Definition BloomFilter.h:79
double falsePositiveRate(void) const
the expected false positives after the insertions made,
bool maybe(const T &x) const
whether may be in the set: false only if was never inserted
BloomFilter(std::size_t m, int k, H h=H())
the empty filter of bits and hashes
Definition ADT.h:96