ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PrefixFilter.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File PrefixFilter.h --------------------------*/
3/*--------------------------------------------------------------------------*/
161/*--------------------------------------------------------------------------*/
162/*----------------------------- DEFINITIONS --------------------------------*/
163/*--------------------------------------------------------------------------*/
164
165#ifndef __ADS_PrefixFilter
166 #define __ADS_PrefixFilter
167/* self-identification: #endif constant */
168
169/*--------------------------------------------------------------------------*/
170/*------------------------------ INCLUDES ----------------------------------*/
171/*--------------------------------------------------------------------------*/
172
173#include "ADS/Contract.h"
174#include "ADS/Portable.h"
175#include "ADS/SimdInline.h"
180
181#include <algorithm>
182#include <bit>
183#include <cmath>
184#include <cstddef>
185#include <cstdint>
186#include <cstring>
187#include <string_view>
188#include <vector>
189
191#ifndef ADS_FILTER_INLINE
192 #define ADS_FILTER_INLINE ADS_ALWAYS_INLINE
193#endif
194
196#ifndef ADS_FILTER_NOINLINE
197 #define ADS_FILTER_NOINLINE ADS_NOINLINE
198#endif
199
200/*--------------------------------------------------------------------------*/
201/*--------------------------- NAMESPACE ------------------------------------*/
202/*--------------------------------------------------------------------------*/
203
204namespace ADS
205{
206
207/*--------------------------------------------------------------------------*/
208/*--------------------------- CLASS PrefixFilter ---------------------------*/
209/*--------------------------------------------------------------------------*/
210
213template< class T , class H = DefaultHash< T > >
215{
216
217/*--------------------------------------------------------------------------*/
218 public:
220 PrefixFilter( void ) : PrefixFilter( 1 ) { }
221
225 explicit PrefixFilter( std::size_t n , H h = H() );
226
230 bool insert( const T & x );
231
234 [[nodiscard]] bool maybe( const T & x ) const;
235
260 bool insertHash( std::uint64_t h );
261
289 [[nodiscard]] bool maybeHash( std::uint64_t h ) const;
290
292 void clear( void );
293
295 [[nodiscard]] std::size_t size( void ) const;
296
298 [[nodiscard]] bool empty( void ) const;
299
301 [[nodiscard]] std::size_t capacity( void ) const;
302
304 [[nodiscard]] double load( void ) const;
305
307 [[nodiscard]] std::size_t bits( void ) const;
308
310 [[nodiscard]] std::size_t bins( void ) const;
311
313 [[nodiscard]] std::size_t spareSize( void ) const;
314
317 [[nodiscard]] double expectedSpareSize( void ) const;
318
321 [[nodiscard]] double falsePositiveRate( void ) const;
322
323/*----------------------------- THE PERSISTENCE ----------------------------*/
324
326 static constexpr std::string_view persistName = "PrefixFilter";
327
329 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
330 return( detail::persistMix( { sizeof( T ) } ) );
331 }
332
335 template< class A >
336 void persist( A & a );
337
338/*--------------------------------------------------------------------------*/
339 private:
343 struct alignas( 32 ) Bin
344 {
346 unsigned char b[ 32 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0xFC };
347 using persist_unit = unsigned char;
348 };
349
350 static constexpr unsigned kK = 25;
351 static constexpr unsigned kQ = 25;
352 static constexpr std::uint32_t kS = kQ << 8;
353 static constexpr unsigned kBody = 7;
354 static constexpr std::uint64_t kHead = ( std::uint64_t( 1 ) << 50 ) - 1;
355 static constexpr std::uint64_t kTop = std::uint64_t( 63 ) << 50;
356 static constexpr double kAlpha = 0.95;
357 static constexpr double kSpareBits = 12;
358
360 [[nodiscard]] static std::uint64_t header( const Bin & B );
361
364 static void setHeader( Bin & B , std::uint64_t w );
365
369 [[nodiscard]] static unsigned select0( std::uint64_t w , unsigned j );
370
372 static void run( std::uint64_t w , unsigned q , unsigned & s , unsigned & e );
373
376 [[nodiscard]] static std::uint32_t equal( const Bin & B , unsigned r );
377
380 [[nodiscard]] static std::uint32_t below( const Bin & B , unsigned r );
381
383 [[nodiscard]] static std::uint32_t runMask( std::uint64_t w , unsigned q );
384
387 [[nodiscard]] static bool find( const Bin & B , std::uint64_t w , unsigned q ,
388 unsigned r );
389
392 [[nodiscard]] static std::uint32_t maximum( const Bin & B , std::uint64_t w );
393
396 static void put( Bin & B , std::uint64_t w , unsigned q , unsigned r ,
397 unsigned c );
398
400 [[nodiscard]] static std::size_t binsFor( std::size_t n );
401
404 [[nodiscard]] static double spareMean( std::size_t n , std::size_t m );
405
407 [[nodiscard]] std::uint64_t hash( const T & x ) const;
408
410 [[nodiscard]] std::size_t bin( std::uint64_t h ) const;
411
413 [[nodiscard]] static std::uint32_t mini( std::uint64_t h );
414
417 [[nodiscard]] bool spare( std::size_t i , std::uint32_t x ) const;
418
421 [[nodiscard]] static std::uint64_t spareHash( std::size_t i ,
422 std::uint32_t x );
423
424 std::vector< Bin , MappableAllocator< Bin > > f_B;
425 std::size_t f_cap;
426 std::size_t f_n = 0;
427 std::size_t f_spareN = 0;
428 BlockedBloomFilter< std::uint64_t > f_spare;
429 H f_h;
430
431 }; // end( class PrefixFilter )
432
433/*--------------------------------------------------------------------------*/
434
435} // end( namespace ADS )
436
437/*--------------------------------------------------------------------------*/
438/*-------------------------- IMPLEMENTATION --------------------------------*/
439/*--------------------------------------------------------------------------*/
440
441#include "ADS/set/impl/PrefixFilter.tpp"
442
443/*--------------------------------------------------------------------------*/
444
445#endif /* PrefixFilter.h included */
446
447/*--------------------------------------------------------------------------*/
448/*------------------------- End File PrefixFilter.h ------------------------*/
449/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
The blocked filter of Bloom, beyond the book: the engineered filter of ADS [see BloomFilter....
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....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
bool maybeHash(std::uint64_t h) const
whether the hash may be in the set: false only if it is not
PrefixFilter(void)
a filter for one key, empty
Definition PrefixFilter.h:220
std::size_t bins(void) const
the bins, of 256 bits each
std::size_t spareSize(void) const
the fingerprints sent to the spare
bool empty(void) const
whether no key is in
double expectedSpareSize(void) const
the expected fingerprints sent to the spare when capacity() keys are in,
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition PrefixFilter.h:329
double falsePositiveRate(void) const
the expected false positives at the current load, the sum of the analysis of PrefixFilter....
bool insert(const T &x)
into the filter by insertHash() of its hash, false (and nothing changed) if keys are in
std::size_t size(void) const
the keys in, with their repetitions
double load(void) const
size() / ( 25 bins() ), at most about 0.95
std::size_t bits(void) const
the bits of the bins and of the spare
PrefixFilter(std::size_t n, H h=H())
the empty filter for keys, its bins at the load 0.95 when they are in, at least one bin
bool maybe(const T &x) const
whether may be in the set, by maybeHash() of its hash: false only if is not in the set
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
bool insertHash(std::uint64_t h)
the hash of 64 bits, which must be random, into the filter, false (and nothing changed) if keys are...
void clear(void)
removes all the keys
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition PrefixFilter.h:326
std::size_t capacity(void) const
the keys that fit, the of the constructor
Definition ADT.h:96