ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
UniversalHashing.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File UniversalHashing.h -------------------------*/
3/*--------------------------------------------------------------------------*/
119/*--------------------------------------------------------------------------*/
120/*----------------------------- DEFINITIONS --------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123#ifndef __ADS_UniversalHashing
124 #define __ADS_UniversalHashing
125/* self-identification: #endif constant */
126
127/*--------------------------------------------------------------------------*/
128/*------------------------------ INCLUDES ----------------------------------*/
129/*--------------------------------------------------------------------------*/
130
131#include "ADS/Contract.h"
132#include "ADS/Iterators.h"
133#include "ADS/Portable.h"
138
139#include <algorithm>
140#include <bit>
141#include <cstddef>
142#include <cstdint>
143#include <functional>
144#include <iterator>
145#include <optional>
146#include <span>
147#include <string_view>
148#include <utility>
149#include <vector>
150
151/*--------------------------------------------------------------------------*/
152/*--------------------------- NAMESPACE ------------------------------------*/
153/*--------------------------------------------------------------------------*/
154
155namespace ADS
156{
157
158/*--------------------------------------------------------------------------*/
159/*------------------------ CLASS CarterWegmanHash --------------------------*/
160/*--------------------------------------------------------------------------*/
161
165{
166 public:
170
174 explicit CarterWegmanHash( std::uint64_t m , std::uint64_t seed = 1 );
175
179 CarterWegmanHash( std::uint64_t p , std::uint64_t a , std::uint64_t b ,
180 std::uint64_t m );
181
186 [[nodiscard]] std::uint64_t operator()( std::uint64_t k ) const;
187
189 [[nodiscard]] std::uint64_t p( void ) const { return( f_p ); }
190
192 [[nodiscard]] std::uint64_t a( void ) const { return( f_a ); }
193
195 [[nodiscard]] std::uint64_t b( void ) const { return( f_b ); }
196
198 [[nodiscard]] std::uint64_t m( void ) const { return( f_m ); }
199
202 template< class A >
203 void persist( A & a ) {
204 a( f_p , f_a , f_b , f_m );
205 a.require( ( f_p >= 2 ) && ( f_p < ( std::uint64_t( 1 ) << 63 ) ) &&
206 ( f_a >= 1 ) && ( f_a < f_p ) && ( f_b < f_p ) && ( f_m >= 1 ) ,
207 "the parameters of a CarterWegmanHash" );
208 }
209
210 private:
212 std::uint64_t f_p , f_a , f_b , f_m;
213 };
214
215/*--------------------------------------------------------------------------*/
216/*------------------------- CLASS DotProductHash ---------------------------*/
217/*--------------------------------------------------------------------------*/
218
222{
223 public:
226 DotProductHash( std::uint64_t m , int r , std::uint64_t seed = 1 );
227
231 DotProductHash( std::uint64_t m , std::vector< std::uint64_t > a );
232
248 [[nodiscard]] std::uint64_t operator()( std::uint64_t k ) const;
249
252 [[nodiscard]] std::uint64_t universe( void ) const;
253
254 private:
255 std::uint64_t f_m;
256 std::vector< std::uint64_t > f_a;
257 };
258
259/*--------------------------------------------------------------------------*/
260/*------------------------ CLASS MultiplyShiftHash -------------------------*/
261/*--------------------------------------------------------------------------*/
262
266{
267 public:
270 MultiplyShiftHash( int h , int l , std::uint64_t seed = 1 );
271
274 [[nodiscard]] static MultiplyShiftHash ofMultiplier( int h , int l ,
275 std::uint64_t a );
276
280 [[nodiscard]] std::uint64_t operator()( std::uint64_t k ) const;
281
282 private:
283 int f_h , f_l;
284 std::uint64_t f_a;
285 std::uint64_t f_mask;
286 };
287
288/*--------------------------------------------------------------------------*/
289/*---------------------------- CLASS StringHash ----------------------------*/
290/*--------------------------------------------------------------------------*/
291
295{
296 public:
298 explicit StringHash( std::uint64_t seed = 1 );
299
315 [[nodiscard]] std::uint64_t operator()( std::string_view s ) const;
316
317 private:
318 std::uint64_t f_x;
319 };
320
321/*--------------------------------------------------------------------------*/
322/*----------------------- CLASS MultiplicativeHash -------------------------*/
323/*--------------------------------------------------------------------------*/
324
329template< class K = std::uint64_t , class Code = std::hash< K > >
331{
332 public:
334 explicit MultiplicativeHash( std::uint64_t seed = 1 , Code code = Code() );
335
338 [[nodiscard]] static MultiplicativeHash ofMultiplier( std::uint64_t z ,
339 Code code = Code() );
340
342 [[nodiscard]] std::uint64_t operator()( const K & k ) const;
343
359 [[nodiscard]] std::size_t place( const K & k , std::size_t m ) const;
360
362 [[nodiscard]] std::uint64_t multiplier( void ) const { return( f_z ); }
363
364 private:
365 std::uint64_t f_z;
366 ADS_NO_UNIQUE_ADDRESS Code f_code;
367 };
368
369/*--------------------------------------------------------------------------*/
370/*-------------------------- CLASS CompoundHash ----------------------------*/
371/*--------------------------------------------------------------------------*/
372
381{
382 public:
385 explicit CompoundHash( std::size_t r , std::uint64_t seed = 1 );
386
401 [[nodiscard]] std::uint64_t
402 operator()( std::span< const std::uint64_t > x ) const;
403
405 [[nodiscard]] std::size_t parts( void ) const { return( f_Z.size() ); }
406
407 private:
408 std::vector< std::uint64_t > f_Z;
409 std::uint64_t f_lo , f_hi;
410 };
411
412/*--------------------------------------------------------------------------*/
413/*-------------------------- CLASS SequenceHash ----------------------------*/
414/*--------------------------------------------------------------------------*/
415
422{
423 public:
426 explicit SequenceHash( std::uint64_t seed = 1 );
427
447 [[nodiscard]] std::uint64_t
448 operator()( std::span< const std::uint64_t > x ) const;
449
454 [[nodiscard]] std::uint64_t operator()( std::string_view s ) const;
455
456 private:
457 std::uint64_t f_z;
458 std::uint64_t f_a;
459 };
460
461/*--------------------------------------------------------------------------*/
462/*------------------------- CLASS DLeftHashTable ---------------------------*/
463/*--------------------------------------------------------------------------*/
464
467template< class K , class V , class H = std::hash< K > >
469{
470
471/*--------------------------------------------------------------------------*/
472 public:
473 using Key = K;
474 using Value = V;
475
479 DLeftHashTable( std::size_t m , int d , std::uint64_t seed = 1 , H h = H() );
480
482 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
483
501 void insert( const K & k , const V & v );
502
505 void remove( const K & k );
506
508 [[nodiscard]] bool contains( const K & k ) const;
509
511 [[nodiscard]] std::size_t size( void ) const;
512
514 [[nodiscard]] std::size_t maxLoad( void ) const;
515
517 [[nodiscard]] int tables( void ) const;
518
519/*--------------------------------------------------------------------------*/
520 private:
521 using Bucket = std::vector< std::pair< K , V > >;
522
523 public:
524/*------------------------- THE STANDARD INTERFACE -------------------------*/
525
526 // a constant view of the pairs ( key , value ), table after table and
527 // list after list, forward; an insertion or a removal invalidates it
528
531 {
532 public:
536 using value_type = std::pair< K , V >;
538 using difference_type = std::ptrdiff_t;
540 using iterator_concept = std::forward_iterator_tag;
542 using iterator_category = std::input_iterator_tag;
543
545 const_iterator( void ) = default;
546
548 const_iterator( const std::vector< Bucket > * T , std::size_t b )
549 : f_T( T ) , f_b( b ) {
550 forward();
551 }
552
554 reference operator*( void ) const {
555 const auto & e = ( *f_T )[ f_b ][ f_i ];
556 return( reference( e.first , e.second ) );
557 }
558
560 ++f_i;
561 forward();
562 return( *this );
563 }
564
566 auto t = *this;
567 ++*this;
568 return( t );
569 }
570
572 bool operator==( const const_iterator & o ) const {
573 return( ( f_b == o.f_b ) && ( f_i == o.f_i ) );
574 }
575
576 private:
578 void forward( void ) {
579 while( ( f_b < f_T->size() ) && ( f_i >= ( *f_T )[ f_b ].size() ) ) {
580 ++f_b;
581 f_i = 0;
582 }
583 }
584
585 const std::vector< Bucket > * f_T = nullptr;
586 std::size_t f_b = 0;
587 std::size_t f_i = 0;
588 };
589
591 using value_type = std::pair< K , V >;
593 using key_type = K;
595 using mapped_type = V;
597 using size_type = std::size_t;
599 using difference_type = std::ptrdiff_t;
606
608 [[nodiscard]] const_iterator begin( void ) const {
609 return( const_iterator( &f_T , 0 ) );
610 }
611
612 [[nodiscard]] const_iterator end( void ) const {
613 return( const_iterator( &f_T , f_T.size() ) );
614 }
615
616 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
618 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
619
620 private:
622 [[nodiscard]] std::size_t list( const K & k , int i ) const;
623
625 [[nodiscard]] std::optional< std::pair< std::size_t , std::size_t > >
626 find( const K & k ) const;
627
630 [[nodiscard]] bool placed( const K & k ) const;
631
633 std::vector< Bucket > f_T;
634 std::size_t f_w;
635 std::vector< TabulationHash > f_g;
636 std::size_t f_n = 0;
637 H f_h;
638 }; // end( class DLeftHashTable )
639
640/*--------------------------------------------------------------------------*/
641
642} // end( namespace ADS )
643
644/*--------------------------------------------------------------------------*/
645/*-------------------------- IMPLEMENTATION --------------------------------*/
646/*--------------------------------------------------------------------------*/
647
648#include "ADS/dictionary/impl/UniversalHashing.tpp"
649
650/*--------------------------------------------------------------------------*/
651
652#endif /* UniversalHashing.h included */
653
654/*--------------------------------------------------------------------------*/
655/*---------------------- End File UniversalHashing.h -----------------------*/
656/*--------------------------------------------------------------------------*/
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....
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Hashing with random tables, beyond the book:
Random numbers as Knuth treats them (chapter 3 of The Art of Computer Programming),...
CarterWegmanHash(std::uint64_t m, std::uint64_t seed=1)
a function drawn from with , from the seed
std::uint64_t b(void) const
the addend
Definition UniversalHashing.h:195
std::uint64_t a(void) const
the multiplier
Definition UniversalHashing.h:192
std::uint64_t p(void) const
the prime
Definition UniversalHashing.h:189
std::uint64_t m(void) const
the size of the range
Definition UniversalHashing.h:198
CarterWegmanHash(void)
the function into one place drawn from the seed 1, to be read from a file [see Persistence....
Definition UniversalHashing.h:169
std::uint64_t operator()(std::uint64_t k) const
, the product computed with no overflow [see mulMod()]
void persist(A &a)
the parameters written to, or read from, the archive [see Persistence.h]
Definition UniversalHashing.h:203
CarterWegmanHash(std::uint64_t p, std::uint64_t a, std::uint64_t b, std::uint64_t m)
the function of the given parameters
std::uint64_t operator()(std::span< const std::uint64_t > x) const
, in time
std::size_t parts(void) const
the number of parts
Definition UniversalHashing.h:405
CompoundHash(std::size_t r, std::uint64_t seed=1)
a function of tuples of words, its multipliers from the seed
the iterator on the pairs, table after table and list after list
Definition UniversalHashing.h:531
bool operator==(const const_iterator &o) const
equal on the same place of the same list
Definition UniversalHashing.h:572
const_iterator & operator++(void)
to the next pair
Definition UniversalHashing.h:559
const_iterator operator++(int)
to the next pair, returning the iterator before the step
Definition UniversalHashing.h:565
reference operator*(void) const
the pair
Definition UniversalHashing.h:554
PairRef< const K &, const V & > reference
a reference to an item
Definition UniversalHashing.h:534
std::input_iterator_tag iterator_category
the category of the iterator
Definition UniversalHashing.h:542
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition UniversalHashing.h:538
std::forward_iterator_tag iterator_concept
the concept of the iterator, for C++20
Definition UniversalHashing.h:540
const_iterator(const std::vector< Bucket > *T, std::size_t b)
on the first pair from the list of the lists
Definition UniversalHashing.h:548
std::pair< K, V > value_type
the type of the items, for the standard library
Definition UniversalHashing.h:536
const_iterator(void)=default
the end of no table
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, from its lists
K key_type
the type of the keys
Definition UniversalHashing.h:593
void remove(const K &k)
removes the key , if it is there, from its list
DLeftHashTable(std::size_t m, int d, std::uint64_t seed=1, H h=H())
tables of lists each,
std::size_t size(void) const
the number of keys
std::pair< K, V > value_type
the type of the items, for the standard library
Definition UniversalHashing.h:591
V mapped_type
the type of the values
Definition UniversalHashing.h:595
reference const_reference
a constant reference to an item
Definition UniversalHashing.h:603
std::size_t size_type
the type of the sizes
Definition UniversalHashing.h:597
const_iterator begin(void) const
the iterator on the first item
Definition UniversalHashing.h:608
V Value
the type of the values
Definition UniversalHashing.h:474
K Key
the type of the keys
Definition UniversalHashing.h:473
std::size_t maxLoad(void) const
the length of the longest list
const_iterator cbegin(void) const
the constant iterator on the first item
Definition UniversalHashing.h:616
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition UniversalHashing.h:599
PairRef< const K &, const V & > reference
a reference to an item
Definition UniversalHashing.h:601
const_iterator end(void) const
the iterator past the last item
Definition UniversalHashing.h:612
const_iterator cend(void) const
the constant iterator past the last item
Definition UniversalHashing.h:618
bool contains(const K &k) const
true if the key is there
const_iterator iterator
the iterator on the items
Definition UniversalHashing.h:605
void insert(const K &k, const V &v)
associates the value to the key
int tables(void) const
the number of tables
DotProductHash(std::uint64_t m, std::vector< std::uint64_t > a)
the function of the digits
std::uint64_t universe(void) const
the size of the universe, or 0 if it exceeds
std::uint64_t operator()(std::uint64_t k) const
DotProductHash(std::uint64_t m, int r, std::uint64_t seed=1)
a function with random digits , from the seed
std::uint64_t operator()(const K &k) const
the product , whose high bits are the hash
static MultiplicativeHash ofMultiplier(std::uint64_t z, Code code=Code())
the function of the multiplier
std::size_t place(const K &k, std::size_t m) const
the place of in a table of places, the high bits of [see PlaceHash in TableSize....
std::uint64_t multiplier(void) const
the multiplier
Definition UniversalHashing.h:362
MultiplicativeHash(std::uint64_t seed=1, Code code=Code())
a function with a random odd of 64 bits, from the seed
std::uint64_t operator()(std::uint64_t k) const
, a product and a shift
MultiplyShiftHash(int h, int l, std::uint64_t seed=1)
a function with a random odd , from the seed
static MultiplyShiftHash ofMultiplier(int h, int l, std::uint64_t a)
the function of the multiplier
std::uint64_t operator()(std::span< const std::uint64_t > x) const
of an array of words, each reduced first to 60 bits by the multiplicative hashing [see Multiplicative...
SequenceHash(std::uint64_t seed=1)
a function with a random point and a random odd multiplier for the reduction of the words,...
std::uint64_t operator()(std::string_view s) const
of a string, its bytes taken as they are: two distinct strings, the longer of bytes,...
StringHash(std::uint64_t seed=1)
a function with a random point , from the seed
std::uint64_t operator()(std::string_view s) const
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96