ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MinimalPerfectHash.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File MinimalPerfectHash.h -----------------------*/
3/*--------------------------------------------------------------------------*/
84/*--------------------------------------------------------------------------*/
85/*----------------------------- DEFINITIONS --------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#ifndef __ADS_MinimalPerfectHash
89 #define __ADS_MinimalPerfectHash
90/* self-identification: #endif constant */
91
92/*--------------------------------------------------------------------------*/
93/*------------------------------ INCLUDES ----------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96#include "ADS/Contract.h"
99
100#include <algorithm>
101#include <bit>
102#include <cmath>
103#include <cstddef>
104#include <cstdint>
105#include <memory>
106#include <stdexcept>
107#include <string_view>
108#include <type_traits>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
118/*--------------------------------------------------------------------------*/
119/*------------------------ CLASS MinimalPerfectHash ------------------------*/
120/*--------------------------------------------------------------------------*/
121
123template< class T , class H = DefaultHash< T > >
125{
126
127/*--------------------------------------------------------------------------*/
128 public:
130 MinimalPerfectHash( void ) : MinimalPerfectHash( std::vector< T >() ) { }
131
132 using Key = T;
133
160 explicit MinimalPerfectHash( const std::vector< T > & keys ,
161 double lambda = 5 ,
162 double alpha = 0.99 , std::uint64_t seed = 1 ,
163 H h = H() );
164
181 [[nodiscard]] std::size_t operator()( const T & k ) const;
182
184 [[nodiscard]] std::size_t size( void ) const;
185
187 [[nodiscard]] bool empty( void ) const;
188
190 [[nodiscard]] std::size_t bits( void ) const;
191
193 [[nodiscard]] std::size_t buckets( void ) const;
194
196 [[nodiscard]] std::size_t trials( void ) const;
197
198/*----------------------------- THE PERSISTENCE ----------------------------*/
199
201 static constexpr std::string_view persistName = "MinimalPerfectHash";
202
204 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
205 return( detail::persistMix( { sizeof( T ) } ) );
206 }
207
210 template< class A >
211 void persist( A & a );
212
213/*--------------------------------------------------------------------------*/
214 private:
215 static constexpr std::size_t kMaxTrials = 100;
216 static constexpr std::uint64_t kMaxPilot = std::uint64_t( 1 ) << 24;
217
219 [[nodiscard]] std::uint64_t base( const T & k ) const;
220
222 [[nodiscard]] std::size_t bucket( std::uint64_t h ) const;
223
225 [[nodiscard]] std::size_t place( std::uint64_t h2 , std::uint64_t k ) const;
226
228 [[nodiscard]] std::uint64_t pilot( std::size_t b ) const;
229
232 bool build( const std::vector< std::uint64_t > & B );
233
234 std::uint64_t f_seed;
235 std::size_t f_n = 0;
236 std::size_t f_m = 0;
237 std::size_t f_dense = 0;
238 std::size_t f_sparse = 0;
239 int f_w = 0;
241 std::vector< std::uint64_t , MappableAllocator< std::uint64_t > > f_P;
242 std::vector< std::uint32_t > f_free;
243 std::size_t f_trials = 0;
244 H f_h;
245
246 }; // end( class MinimalPerfectHash )
247
248/*--------------------------------------------------------------------------*/
249
250} // end( namespace ADS )
251
252/*--------------------------------------------------------------------------*/
253/*-------------------------- IMPLEMENTATION --------------------------------*/
254/*--------------------------------------------------------------------------*/
255
256#include "ADS/dictionary/impl/MinimalPerfectHash.tpp"
257
258/*--------------------------------------------------------------------------*/
259
260#endif /* MinimalPerfectHash.h included */
261
262/*--------------------------------------------------------------------------*/
263/*---------------------- End File MinimalPerfectHash.h ---------------------*/
264/*--------------------------------------------------------------------------*/
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....
bool empty(void) const
true if there are no keys
std::size_t operator()(const T &k) const
the value of , in ; some value in it for a key out of
std::size_t size(void) const
the number of keys
MinimalPerfectHash(void)
the function of no key
Definition MinimalPerfectHash.h:130
std::size_t trials(void) const
the seeds drawn by the build
T Key
the type of the keys
Definition MinimalPerfectHash.h:132
std::size_t bits(void) const
the bits of the function: the pilots, the free places and the sizes
static constexpr std::uint64_t persistTag(void)
the parameters of the layout, in a file
Definition MinimalPerfectHash.h:204
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 MinimalPerfectHash.h:201
MinimalPerfectHash(const std::vector< T > &keys, double lambda=5, double alpha=0.99, std::uint64_t seed=1, H h=H())
the function of the distinct keys, with keys per bucket on average and the load of the table
std::size_t buckets(void) const
the number of buckets
constexpr int lambda(Word x)
the place of the leading 1 of ,
Definition ADT.h:96