ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryFuseFilter.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File BinaryFuseFilter.h ------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_BinaryFuseFilter
76 #define __ADS_BinaryFuseFilter
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#include "ADS/Contract.h"
86
87#include <algorithm>
88#include <array>
89#include <bit>
90#include <cmath>
91#include <cstddef>
92#include <cstdint>
93#include <limits>
94#include <memory>
95#include <stdexcept>
96#include <string_view>
97#include <type_traits>
98#include <vector>
99
100/*--------------------------------------------------------------------------*/
101/*--------------------------- NAMESPACE ------------------------------------*/
102/*--------------------------------------------------------------------------*/
103
104namespace ADS
105{
106
107/*--------------------------------------------------------------------------*/
108/*------------------------- CLASS BinaryFuseFilter -------------------------*/
109/*--------------------------------------------------------------------------*/
110
113template< class T , class F = std::uint8_t , class H = DefaultHash< T > >
114 requires( std::is_unsigned_v< F > && ( sizeof( F ) <= 4 ) )
116{
117
118/*--------------------------------------------------------------------------*/
119 public:
121 BinaryFuseFilter( void ) : BinaryFuseFilter( std::vector< T >() ) { }
122
161 explicit BinaryFuseFilter( const std::vector< T > & keys ,
162 std::uint64_t seed = 1 , H h = H() );
163
177 [[nodiscard]] bool maybe( const T & x ) const;
178
180 [[nodiscard]] std::size_t size( void ) const;
181
183 [[nodiscard]] bool empty( void ) const;
184
186 [[nodiscard]] std::size_t bits( void ) const;
187
189 [[nodiscard]] std::size_t trials( void ) const;
190
191/*----------------------------- THE PERSISTENCE ----------------------------*/
192
194 static constexpr std::string_view persistName = "BinaryFuseFilter";
195
200 static constexpr std::uint32_t persistVersion = 2;
201
203 static constexpr std::uint32_t persistOldestVersion = 1;
204
206 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
207 return( detail::persistMix( { sizeof( T ) , sizeof( F ) } ) );
208 }
209
212 template< class A >
213 void persist( A & a );
214
215/*--------------------------------------------------------------------------*/
216 private:
217 static constexpr std::size_t kMaxTrials = 1000;
218
220 [[nodiscard]] std::uint64_t base( const T & x ) const;
221
223 [[nodiscard]] std::uint64_t mixed( std::uint64_t b ) const;
224
226 [[nodiscard]] F fingerprint( std::uint64_t h ) const;
227
229 [[nodiscard]] std::uint32_t first( std::uint64_t h ) const;
230
233 [[nodiscard]] std::array< std::uint32_t , 3 > places( std::uint64_t h ) const;
234
236 void layout( std::size_t n );
237
240 bool build( const std::vector< std::uint64_t > & B );
241
242 std::vector< F , MappableAllocator< F > > f_F;
243 std::uint64_t f_seed;
244 std::uint32_t f_L = 0;
245 std::uint32_t f_CL = 0;
246 std::size_t f_n = 0;
247 std::size_t f_trials = 0;
248 H f_h;
249 bool f_old = false;
251 static constexpr std::uint64_t kProductBit = std::uint64_t( 1 ) << 63;
252
253 }; // end( class BinaryFuseFilter )
254
255/*--------------------------------------------------------------------------*/
256
257} // end( namespace ADS )
258
259/*--------------------------------------------------------------------------*/
260/*-------------------------- IMPLEMENTATION --------------------------------*/
261/*--------------------------------------------------------------------------*/
262
263#include "ADS/set/impl/BinaryFuseFilter.tpp"
264
265/*--------------------------------------------------------------------------*/
266
267#endif /* BinaryFuseFilter.h included */
268
269/*--------------------------------------------------------------------------*/
270/*----------------------- End File BinaryFuseFilter.h ----------------------*/
271/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition BinaryFuseFilter.h:194
bool empty(void) const
whether the filter has no key
std::size_t bits(void) const
the bits of the fingerprints
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition BinaryFuseFilter.h:206
static constexpr std::uint32_t persistOldestVersion
the oldest version read: 1, whose filters keep their fingerprints
Definition BinaryFuseFilter.h:203
BinaryFuseFilter(void)
the filter of no key
Definition BinaryFuseFilter.h:121
std::size_t trials(void) const
the seeds drawn by the build
static constexpr std::uint32_t persistVersion
the version of the layout in a file: 2, whose bit 63 of the trials says that the fingerprints are by ...
Definition BinaryFuseFilter.h:200
std::size_t size(void) const
the keys of the build, the repeated ones once each
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
BinaryFuseFilter(const std::vector< T > &keys, std::uint64_t seed=1, H h=H())
the filter of the keys, which may repeat
bool maybe(const T &x) const
whether may be in the set: true for every key of the set, and for any other with probability about ,...
Definition ADT.h:96