ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PerfectHashing.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File PerfectHashing.h --------------------------*/
3/*--------------------------------------------------------------------------*/
102/*--------------------------------------------------------------------------*/
103/*----------------------------- DEFINITIONS --------------------------------*/
104/*--------------------------------------------------------------------------*/
105
106#ifndef __ADS_PerfectHashing
107 #define __ADS_PerfectHashing
108/* self-identification: #endif constant */
109
110/*--------------------------------------------------------------------------*/
111/*------------------------------ INCLUDES ----------------------------------*/
112/*--------------------------------------------------------------------------*/
113
114#include "ADS/Contract.h"
115#include "ADS/Iterators.h"
119
120#include <algorithm>
121#include <cmath>
122#include <concepts>
123#include <cstddef>
124#include <cstdint>
125#include <optional>
126#include <stdexcept>
127#include <string_view>
128#include <vector>
129
130/*--------------------------------------------------------------------------*/
131/*--------------------------- NAMESPACE ------------------------------------*/
132/*--------------------------------------------------------------------------*/
133
134namespace ADS
135{
136
137/*--------------------------------------------------------------------------*/
138/*------------------------ CLASS PerfectHashTable --------------------------*/
139/*--------------------------------------------------------------------------*/
140
143{
144
145/*--------------------------------------------------------------------------*/
146 public:
148 [[nodiscard]] static PerfectHashTable persistEmpty( void ) {
149 return( PerfectHashTable( std::vector< std::uint64_t >() ) );
150 }
151
152 using Key = std::uint64_t;
153
182 explicit PerfectHashTable( const std::vector< std::uint64_t > & S ,
183 std::uint64_t seed = 1 );
184
198 [[nodiscard]] bool contains( std::uint64_t k ) const;
199
201 [[nodiscard]] std::size_t size( void ) const;
202
204 [[nodiscard]] std::size_t space( void ) const;
205
207 [[nodiscard]] std::size_t trials( void ) const;
208
209/*--------------------------------------------------------------------------*/
210 private:
211 static constexpr std::uint64_t kEmpty = ~std::uint64_t( 0 );
212 static constexpr std::size_t kMaxTrials = 1000;
213
214 struct Bucket
215 {
216 std::size_t offset = 0;
217 std::size_t size = 0;
219
221 template< class A >
222 void persist( A & a ) {
223 a( offset , size , h );
224 }
225 };
226
227 public:
228/*----------------------------- THE PERSISTENCE ----------------------------*/
229
231 static constexpr std::string_view persistName = "PerfectHashTable";
232
235 template< class A >
236 void persist( A & a );
237
238/*------------------------- THE STANDARD INTERFACE -------------------------*/
239
240 // a constant view of the keys, in the order of the places, bidirectional
241
243 struct Places
244 {
246 static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
248 static bool occupied( const auto & o , std::size_t i ) {
249 return( o.f_T[ i ] != kEmpty );
250 }
251
252 static const std::uint64_t & read( const auto & o , std::size_t i ) {
253 return( o.f_T[ i ] );
254 }
255 };
256
258 using value_type = std::uint64_t;
260 using size_type = std::size_t;
262 using difference_type = std::ptrdiff_t;
264 using reference = const std::uint64_t &;
266 using const_reference = const std::uint64_t &;
271
273 [[nodiscard]] const_iterator begin( void ) const {
274 return( const_iterator( this , 0 ) );
275 }
276
277 [[nodiscard]] const_iterator end( void ) const {
278 return( const_iterator( this , f_T.size() ) );
279 }
280
281 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
283 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
284
285 private:
286 CarterWegmanHash f_h;
287 std::vector< Bucket > f_B;
288 std::vector< std::uint64_t > f_T;
289 std::size_t f_n = 0;
290 std::size_t f_trials = 0;
291
292 }; // end( class PerfectHashTable )
293
294/*--------------------------------------------------------------------------*/
295/*-------------------- CLASS OrderedMinimalPerfectHash ---------------------*/
296/*--------------------------------------------------------------------------*/
297
300template< class K >
301 requires( std::integral< K > ||
302 std::convertible_to< const K & , std::string_view > )
304{
305
306/*--------------------------------------------------------------------------*/
307 public:
310 : OrderedMinimalPerfectHash( std::vector< K >() ) { }
311
312 using Key = K;
313
336 explicit OrderedMinimalPerfectHash( const std::vector< K > & keys ,
337 int r = 2 ,
338 double c = 0 , std::uint64_t seed = 1 );
339
344 [[nodiscard]] std::size_t operator()( const K & k ) const;
345
347 [[nodiscard]] std::size_t size( void ) const;
348
350 [[nodiscard]] std::size_t vertices( void ) const;
351
353 [[nodiscard]] std::size_t trials( void ) const;
354
385 [[nodiscard]] static std::optional< std::vector< std::uint32_t > >
386 labelAcyclicGraph( std::size_t m , const std::vector< std::size_t > & E ,
387 const std::vector< std::size_t > & h , std::size_t n );
388
423 [[nodiscard]] static std::optional< std::vector< std::uint32_t > >
424 labelByPeeling( std::size_t m , const std::vector< std::size_t > & E , int r ,
425 const std::vector< std::size_t > & h , std::size_t n );
426
427/*----------------------------- THE PERSISTENCE ----------------------------*/
428
430 static constexpr std::string_view persistName = "OrderedMinimalPerfectHash";
431
433 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
434 return( detail::persistMix( { sizeof( K ) } ) );
435 }
436
439 template< class A >
440 void persist( A & a );
441
442/*--------------------------------------------------------------------------*/
443 private:
444 static constexpr std::size_t kMaxTrials = 1000;
445
448 [[nodiscard]] std::uint64_t fingerprint( const K & k , int i ) const;
449
451 [[nodiscard]] std::size_t vertex( const K & k , int i ) const;
452
453 int f_r;
454 std::size_t f_n = 0;
455 std::size_t f_m = 0;
456 std::size_t f_block = 0;
457 std::size_t f_trials = 0;
458 std::vector< std::uint64_t > f_seed;
459 std::vector< CarterWegmanHash > f_h;
460 std::vector< std::uint32_t > f_g;
461 }; // end( class OrderedMinimalPerfectHash )
462
463/*--------------------------------------------------------------------------*/
464
465} // end( namespace ADS )
466
467/*--------------------------------------------------------------------------*/
468/*-------------------------- IMPLEMENTATION --------------------------------*/
469/*--------------------------------------------------------------------------*/
470
471#include "ADS/dictionary/impl/PerfectHashing.tpp"
472
473/*--------------------------------------------------------------------------*/
474
475#endif /* PerfectHashing.h included */
476
477/*--------------------------------------------------------------------------*/
478/*----------------------- End File PerfectHashing.h ------------------------*/
479/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
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:
Universal hashing, beyond the book [see HashFunctions.h for the fixed functions of section 7....
, of the class of Carter and Wegman
Definition UniversalHashing.h:165
static std::optional< std::vector< std::uint32_t > > labelAcyclicGraph(std::size_t m, const std::vector< std::size_t > &E, const std::vector< std::size_t > &h, std::size_t n)
the labeling of an acyclic graph: on the vertices such that on each edge , each tree labeled from ...
std::size_t trials(void) const
the graphs drawn by the build
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition PerfectHashing.h:430
std::size_t size(void) const
the number of keys
K Key
the type of the keys
Definition PerfectHashing.h:312
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition PerfectHashing.h:433
std::size_t operator()(const K &k) const
the value of , , in ; anything in it for a key out of
std::size_t vertices(void) const
the number of vertices, i.e., of values of
OrderedMinimalPerfectHash(const std::vector< K > &keys, int r=2, double c=0, std::uint64_t seed=1)
the function of the distinct keys, on or 3 functions and vertices; stands for 3 if and for 1....
static std::optional< std::vector< std::uint32_t > > labelByPeeling(std::size_t m, const std::vector< std::size_t > &E, int r, const std::vector< std::size_t > &h, std::size_t n)
on the vertices such that the of the vertices of each hyperedge add up to , by the reverse order ...
OrderedMinimalPerfectHash(void)
the function of no key
Definition PerfectHashing.h:309
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::uint64_t value_type
the type of the items, for the standard library
Definition PerfectHashing.h:258
bool contains(std::uint64_t k) const
whether is in , any , in time in the worst case: the key in the place of the table compared with
std::size_t space(void) const
the places of the second level,
std::size_t trials(void) const
the functions drawn by the build, of both levels
std::uint64_t Key
the type of the keys
Definition PerfectHashing.h:152
const std::uint64_t & reference
a reference to an item
Definition PerfectHashing.h:264
SlotIterator< PerfectHashTable, Places > const_iterator
the iterator on the items, constant
Definition PerfectHashing.h:268
const_iterator iterator
the iterator on the items
Definition PerfectHashing.h:270
const_iterator cbegin(void) const
the constant iterator on the first item
Definition PerfectHashing.h:281
std::size_t size_type
the type of the sizes
Definition PerfectHashing.h:260
const_iterator cend(void) const
the constant iterator past the last item
Definition PerfectHashing.h:283
const std::uint64_t & const_reference
a constant reference to an item
Definition PerfectHashing.h:266
const_iterator end(void) const
the iterator past the last item
Definition PerfectHashing.h:277
PerfectHashTable(const std::vector< std::uint64_t > &S, std::uint64_t seed=1)
the table of the keys of
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition PerfectHashing.h:262
static PerfectHashTable persistEmpty(void)
the table of no key, to be read from a file [see Persistence.h]
Definition PerfectHashing.h:148
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition PerfectHashing.h:231
std::size_t size(void) const
the number of keys
const_iterator begin(void) const
the iterator on the first item
Definition PerfectHashing.h:273
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
Definition ADT.h:96
the places of the second level, for SlotIterator
Definition PerfectHashing.h:244
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key
Definition PerfectHashing.h:248
static const std::uint64_t & read(const auto &o, std::size_t i)
the key of the place
Definition PerfectHashing.h:252
static std::size_t size(const auto &o)
the number of places
Definition PerfectHashing.h:246