ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RibbonFilter.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File RibbonFilter.h --------------------------*/
3/*--------------------------------------------------------------------------*/
138/*--------------------------------------------------------------------------*/
139/*----------------------------- DEFINITIONS --------------------------------*/
140/*--------------------------------------------------------------------------*/
141
142#ifndef __ADS_RibbonFilter
143 #define __ADS_RibbonFilter
144/* self-identification: #endif constant */
145
146/*--------------------------------------------------------------------------*/
147/*------------------------------ INCLUDES ----------------------------------*/
148/*--------------------------------------------------------------------------*/
149
150#include "ADS/Contract.h"
151#include "ADS/Portable.h"
152#include "ADS/SimdInline.h"
155#include "ADS/memory/Prefetch.h"
156
157#include <algorithm>
158#include <bit>
159#include <cmath>
160#include <cstddef>
161#include <cstdint>
162#include <limits>
163#include <stdexcept>
164#include <string_view>
165#include <type_traits>
166#include <vector>
167
169#ifndef ADS_FILTER_INLINE
170 #define ADS_FILTER_INLINE ADS_ALWAYS_INLINE
171#endif
172
173/*--------------------------------------------------------------------------*/
174/*--------------------------- NAMESPACE ------------------------------------*/
175/*--------------------------------------------------------------------------*/
176
177namespace ADS
178{
179
180/*--------------------------------------------------------------------------*/
181/*--------------------------- CLASS RibbonFilter ---------------------------*/
182/*--------------------------------------------------------------------------*/
183
186template< class T , class F = std::uint8_t , class H = DefaultHash< T > >
188{
189
190 static_assert( std::is_unsigned_v< F > && ( sizeof( F ) <= 4 ) ,
191 "the results have at most 32 bits" );
192
193/*--------------------------------------------------------------------------*/
194 public:
196 RibbonFilter( void ) : RibbonFilter( std::vector< T >() ) { }
197
230 explicit RibbonFilter( const std::vector< T > & keys ,
231 std::uint64_t seed = 1 ,
232 H h = H() );
233
251 [[nodiscard]] bool maybe( const T & x ) const;
252
254 [[nodiscard]] std::size_t size( void ) const;
255
257 [[nodiscard]] bool empty( void ) const;
258
260 [[nodiscard]] std::size_t bits( void ) const;
261
263 [[nodiscard]] std::size_t rows( void ) const;
264
266 [[nodiscard]] std::size_t trials( void ) const;
267
268/*----------------------------- THE PERSISTENCE ----------------------------*/
269
271 static constexpr std::string_view persistName = "RibbonFilter";
272
277 static constexpr std::uint32_t persistVersion = 2;
278
280 static constexpr std::uint32_t persistOldestVersion = 1;
281
283 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
284 return( detail::persistMix( { sizeof( T ) , sizeof( F ) } ) );
285 }
286
289 template< class A >
290 void persist( A & a );
291
292/*--------------------------------------------------------------------------*/
293 private:
294 static constexpr int kR = 8 * sizeof( F );
295 static constexpr std::size_t kMaxTrials = 1000;
296 static constexpr std::size_t kAhead = 16;
297
299 struct alignas( 64 ) Line
300 {
301 std::uint64_t w[ 8 ];
302 using persist_unit = std::uint64_t;
303 };
304
306 [[nodiscard]] std::uint64_t base( const T & x ) const;
307
309 [[nodiscard]] std::uint64_t mixed( std::uint64_t b ) const;
310
312 [[nodiscard]] std::size_t start( std::uint64_t h ) const;
313
315 [[nodiscard]] static std::uint64_t coefficients( std::uint64_t h );
316
318 [[nodiscard]] F fingerprint( std::uint64_t h ) const;
319
324 [[nodiscard]] static std::uint32_t
325 parities( const std::uint64_t * u , std::uint64_t lo , std::uint64_t hi );
326
328 bool build( const std::vector< T > & keys );
329
332 void solve( const std::vector< std::uint64_t > & C ,
333 const std::vector< F > & R );
334
336 std::vector< Line , MappableAllocator< Line > > f_Z;
337 std::size_t f_m = 0;
338 std::uint64_t f_seed;
339 std::size_t f_n = 0;
340 std::size_t f_trials = 0;
341 H f_h;
342 bool f_old = false;
344 static constexpr std::uint64_t kProductBit = std::uint64_t( 1 ) << 63;
345
346 }; // end( class RibbonFilter )
347
348/*--------------------------------------------------------------------------*/
349
350} // end( namespace ADS )
351
352/*--------------------------------------------------------------------------*/
353/*-------------------------- IMPLEMENTATION --------------------------------*/
354/*--------------------------------------------------------------------------*/
355
356#include "ADS/set/impl/RibbonFilter.tpp"
357
358/*--------------------------------------------------------------------------*/
359
360#endif /* RibbonFilter.h included */
361
362/*--------------------------------------------------------------------------*/
363/*------------------------- End File RibbonFilter.h ------------------------*/
364/*--------------------------------------------------------------------------*/
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 prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
static constexpr std::uint32_t persistOldestVersion
the oldest version read: 1, whose filters keep their fingerprints
Definition RibbonFilter.h:280
std::size_t bits(void) const
the bits of the solution, padding included
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 ,...
std::size_t size(void) const
the keys of the build, with their repetitions
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 RibbonFilter.h:277
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition RibbonFilter.h:283
std::size_t trials(void) const
the seeds drawn by the build
std::size_t rows(void) const
the rows m of the solution
bool empty(void) const
whether the filter has no key
RibbonFilter(void)
the filter of no key
Definition RibbonFilter.h:196
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition RibbonFilter.h:271
RibbonFilter(const std::vector< T > &keys, std::uint64_t seed=1, H h=H())
the filter of the keys, which may repeat
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
Definition ADT.h:96