ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CuckooFilter.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File CuckooFilter.h --------------------------*/
3/*--------------------------------------------------------------------------*/
107/*--------------------------------------------------------------------------*/
108/*----------------------------- DEFINITIONS --------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111#ifndef __ADS_CuckooFilter
112 #define __ADS_CuckooFilter
113/* self-identification: #endif constant */
114
115/*--------------------------------------------------------------------------*/
116/*------------------------------ INCLUDES ----------------------------------*/
117/*--------------------------------------------------------------------------*/
118
119#include "ADS/Contract.h"
120#include "ADS/Portable.h"
123
124#include <algorithm>
125#include <bit>
126#include <cstddef>
127#include <cstdint>
128#include <string_view>
129#include <type_traits>
130#include <vector>
131
133#ifndef ADS_FILTER_INLINE
134 #define ADS_FILTER_INLINE ADS_ALWAYS_INLINE
135#endif
136
137/*--------------------------------------------------------------------------*/
138/*--------------------------- NAMESPACE ------------------------------------*/
139/*--------------------------------------------------------------------------*/
140
141namespace ADS
142{
143
144/*--------------------------------------------------------------------------*/
145/*--------------------------- CLASS CuckooFilter ---------------------------*/
146/*--------------------------------------------------------------------------*/
147
150template< class T , class F = std::uint8_t , class H = DefaultHash< T > >
152{
153
154 static_assert( std::is_same_v< F , std::uint8_t > ||
155 std::is_same_v< F , std::uint16_t > ,
156 "the fingerprints have 8 or 16 bits" );
157
158/*--------------------------------------------------------------------------*/
159 public:
161 CuckooFilter( void ) : CuckooFilter( 1 ) { }
162
167 explicit CuckooFilter( std::size_t n , H h = H() );
168
172 bool insert( const T & x );
173
177 bool remove( const T & x );
178
181 [[nodiscard]] bool maybe( const T & x ) const;
182
207 bool insertHash( std::uint64_t h );
208
230 bool removeHash( std::uint64_t h );
231
245 [[nodiscard]] bool maybeHash( std::uint64_t h ) const;
246
248 void clear( void );
249
251 [[nodiscard]] std::size_t size( void ) const;
252
254 [[nodiscard]] bool empty( void ) const;
255
257 [[nodiscard]] std::size_t capacity( void ) const;
258
260 [[nodiscard]] double load( void ) const;
261
263 [[nodiscard]] std::size_t bits( void ) const;
264
265/*----------------------------- THE PERSISTENCE ----------------------------*/
266
268 static constexpr std::string_view persistName = "CuckooFilter";
269
271 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
272 return( detail::persistMix( { sizeof( T ) , sizeof( F ) } ) );
273 }
274
277 template< class A >
278 void persist( A & a );
279
280/*--------------------------------------------------------------------------*/
281 private:
283 using W =
284 std::conditional_t< sizeof( F ) == 1 , std::uint32_t , std::uint64_t >;
285
286 static constexpr int kLane = 8 * sizeof( F );
287 static constexpr W kOnes = W( ~W( 0 ) ) / W( F( ~F( 0 ) ) );
288 static constexpr W kLow = kOnes * W( F( ~F( 0 ) ) >> 1 );
289 static constexpr int kMaxKicks = 500;
290
292 [[nodiscard]] static constexpr W zeros( W w ) {
293 return( W( ~( ( ( w & kLow ) + kLow ) | w | kLow ) ) );
294 }
295
297 [[nodiscard]] std::uint64_t hash( const T & x ) const;
298
300 [[nodiscard]] static F tag( std::uint64_t h );
301
303 [[nodiscard]] std::size_t bucket( std::uint64_t h ) const;
304
306 [[nodiscard]] std::size_t other( std::size_t b , F t ) const;
307
309 bool put( std::size_t b , F t );
310
313 void place( std::size_t b , F t );
314
316 std::uint64_t random( void );
317
318 std::vector< W , MappableAllocator< W > > f_T;
319 std::size_t f_mask = 0;
320 std::size_t f_n = 0;
321 std::size_t f_vb = 0;
322 F f_vt = 0;
323 std::uint64_t f_rng = 0x9E3779B97F4A7C15ULL;
324 H f_h;
325
326 }; // end( class CuckooFilter )
327
328/*--------------------------------------------------------------------------*/
329
330} // end( namespace ADS )
331
332/*--------------------------------------------------------------------------*/
333/*-------------------------- IMPLEMENTATION --------------------------------*/
334/*--------------------------------------------------------------------------*/
335
336#include "ADS/set/impl/CuckooFilter.tpp"
337
338/*--------------------------------------------------------------------------*/
339
340#endif /* CuckooFilter.h included */
341
342/*--------------------------------------------------------------------------*/
343/*------------------------- End File CuckooFilter.h ------------------------*/
344/*--------------------------------------------------------------------------*/
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: ...
std::size_t bits(void) const
the bits of the buckets
CuckooFilter(void)
a filter for one key, empty
Definition CuckooFilter.h:161
CuckooFilter(std::size_t n, H h=H())
the empty filter for keys, at the load 0.94 when they are in, in at least one bucket: buckets,...
void clear(void)
removes all the keys
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
double load(void) const
size() / capacity(), the load
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition CuckooFilter.h:271
std::size_t size(void) const
the keys in, with their repetitions
bool empty(void) const
whether no key is in
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition CuckooFilter.h:268
std::size_t capacity(void) const
the places, 4 per bucket
bool insertHash(std::uint64_t h)
the hash of 64 bits, which must be random, into the filter, false (and nothing changed) if the filte...
bool removeHash(std::uint64_t h)
one insertion of the hash removed, false if it is not there
bool insert(const T &x)
into the filter by insertHash() of its hash, false (and nothing changed) if the filter is full
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
bool remove(const T &x)
one insertion of removed by removeHash() of its hash, false if is not there
bool maybeHash(std::uint64_t h) const
whether the hash may be in the set: false only if it is not
Definition ADT.h:96