ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OpenHashTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File OpenHashTable.h --------------------------*/
3/*--------------------------------------------------------------------------*/
121/*--------------------------------------------------------------------------*/
122/*----------------------------- DEFINITIONS --------------------------------*/
123/*--------------------------------------------------------------------------*/
124
125#ifndef __ADS_OpenHashTable
126 #define __ADS_OpenHashTable
127/* self-identification: #endif constant */
128
129/*--------------------------------------------------------------------------*/
130/*------------------------------ INCLUDES ----------------------------------*/
131/*--------------------------------------------------------------------------*/
132
133#include "ADS/Contract.h"
134#include "ADS/Iterators.h"
137
138#include <algorithm>
139#include <atomic>
140#include <concepts>
141#include <cstddef>
142#include <functional>
143#include <numeric>
144#include <optional>
145#include <random>
146#include <stdexcept>
147#include <utility>
148#include <vector>
149
150/*--------------------------------------------------------------------------*/
151/*--------------------------- NAMESPACE ------------------------------------*/
152/*--------------------------------------------------------------------------*/
153
154namespace ADS
155{
156
157/*--------------------------------------------------------------------------*/
158/*----------------------------- CLASS Probing ------------------------------*/
159/*--------------------------------------------------------------------------*/
160
162class Probing
163{
164
165/*--------------------------------------------------------------------------*/
166 public:
175
177 using Step = std::function< std::size_t( std::size_t ) >;
178
180 static Probing linear( long delta = 1 );
181
184 static Probing quadratic( long c1 , long c2 );
185
187 static Probing double_hashing( void );
188
191 static Probing double_hashing( Step g );
192
195 static Probing triangular( void );
196
200 static Probing pseudo_random( unsigned long seed = 1 );
201
203 [[nodiscard]] Kind kind( void ) const;
204
209 [[nodiscard]] std::size_t at( std::size_t h , std::size_t i ,
210 std::size_t m ) const;
211
215 [[nodiscard]] static std::size_t step( std::size_t h , std::size_t m );
216
221 [[nodiscard]] std::size_t stride( std::size_t h , std::size_t m ) const;
222
224 [[nodiscard]] bool works( std::size_t m ) const;
225
228 void check( std::size_t m ) const;
229
230/*--------------------------------------------------------------------------*/
231 private:
234 Probing( Kind k , long c1 , long c2 );
235
237 static std::size_t mod( long c , std::size_t m );
238
241 const std::vector< std::size_t > & permutation( std::size_t m ) const;
242
243 Kind f_kind;
244 long f_c1;
245 long f_c2;
246 Step f_g;
247 unsigned long f_seed = 0;
248 mutable std::vector< std::size_t > f_p;
249 mutable std::size_t f_m = 0;
250
251 }; // end( class Probing )
252
253/*--------------------------------------------------------------------------*/
254/*-------------------------- CLASS OpenHashTable ---------------------------*/
255/*--------------------------------------------------------------------------*/
256
258template< class K , class V , class H = std::hash< K > >
260{
261
262/*--------------------------------------------------------------------------*/
263 public:
264 using Key = K;
265 using Value = V;
266
268 enum class Slot {
272 };
273
277 OpenHashTable( std::size_t m , Probing probe , bool restructure = false ,
278 H h = H() );
279
280/*--------------------------------------------------------------------------*/
281
309 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
310
332 void insert( const K & k , const V & v );
333
338 void remove( const K & k );
339
341 [[nodiscard]] bool contains( const K & k ) const;
342
347 [[nodiscard]] const K * findKey( const K & k ) const;
348
350 [[nodiscard]] std::size_t size( void ) const;
351
353 [[nodiscard]] bool empty( void ) const;
354
356 [[nodiscard]] std::size_t capacity( void ) const;
357
359 [[nodiscard]] double load( void ) const;
360
363 template< class F >
364 void forEach( F && f ) const;
365
368 [[nodiscard]] Slot slot( std::size_t i ) const;
369
372 [[nodiscard]] const K & keyAt( std::size_t i ) const;
373
375 [[nodiscard]] std::size_t probes( void ) const;
376
378 [[nodiscard]] std::size_t restructures( void ) const;
379
381 [[nodiscard]] std::size_t moved( void ) const;
382
383/*------------------------- THE STANDARD INTERFACE -------------------------*/
384
385 // a constant view of the pairs ( key , value ), in the order of the
386 // places, bidirectional; it skips the empty and the deleted places, and a
387 // rebuild after an insertion or a removal invalidates it
388
390 struct Places
391 {
393 static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
395 static bool occupied( const auto & o , std::size_t i ) {
396 return( o.f_T[ i ].slot == Slot::FULL );
397 }
398
399 static auto read( const auto & o , std::size_t i ) {
400 return(
401 PairRef< const K & , const V & >( o.f_T[ i ].key , o.f_T[ i ].value ) );
402 }
403 };
404
406 using value_type = std::pair< K , V >;
408 using key_type = K;
410 using mapped_type = V;
412 using size_type = std::size_t;
414 using difference_type = std::ptrdiff_t;
423
425 [[nodiscard]] const_iterator begin( void ) const {
426 return( const_iterator( this , 0 ) );
427 }
428
429 [[nodiscard]] const_iterator end( void ) const {
430 return( const_iterator( this , Places::size( *this ) ) );
431 }
432
433 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
435 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
436
437/*--------------------------------------------------------------------------*/
438 private:
439 struct Cell
440 {
441 Slot slot = Slot::NIL;
442 K key{};
443 V value{};
444 };
445
447 [[nodiscard]] bool is( std::size_t i , const K & k ) const;
448
453 std::size_t scan( const K & k , bool insert ) const;
454
456 [[nodiscard]] std::size_t hashOf( const K & k ) const;
457
460 [[nodiscard]] std::size_t where( const K & k ) const;
461
466 [[nodiscard]] bool consistent( const K & k ) const;
467
469 void rebuild( std::size_t m );
470
471 std::vector< Cell > f_T;
472 Probing f_probe;
473 std::size_t f_m0;
474 std::size_t f_linear = 0;
475 bool f_restructure;
476 H f_h;
477 std::size_t f_size = 0;
478 std::size_t f_deleted = 0;
481 struct Probes
482 {
483 std::atomic< std::size_t > n{ 0 };
485 Probes( void ) = default;
487 Probes( const Probes & o ) : n( o.n.load( std::memory_order_relaxed ) ) { }
489 Probes & operator=( const Probes & o ) {
490 n.store( o.n.load( std::memory_order_relaxed ) ,
491 std::memory_order_relaxed );
492 return( *this );
493 }
494 };
495 mutable Probes f_probes;
496 std::size_t f_rebuilds = 0;
497 std::size_t f_moved = 0;
498
499 }; // end( class OpenHashTable )
500
501/*--------------------------------------------------------------------------*/
502
503} // end( namespace ADS )
504
505/*--------------------------------------------------------------------------*/
506/*-------------------------- IMPLEMENTATION --------------------------------*/
507/*--------------------------------------------------------------------------*/
508
509#include "ADS/dictionary/impl/OpenHashTable.tpp"
510
511/*--------------------------------------------------------------------------*/
512
513#endif /* OpenHashTable.h included */
514
515/*--------------------------------------------------------------------------*/
516/*------------------------ End File OpenHashTable.h ------------------------*/
517/*--------------------------------------------------------------------------*/
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...
Random numbers as Knuth treats them (chapter 3 of The Art of Computer Programming),...
The sizes of the hash tables that restructure themselves (section 7.4): a size that is a power of two...
SlotIterator< OpenHashTable, Places > const_iterator
the iterator on the items, constant
Definition OpenHashTable.h:420
PairRef< const K &, const V & > const_reference
a constant reference to an item
Definition OpenHashTable.h:418
void remove(const K &k)
removes the key , if it is there, leaving a deleted place, by scan( k , false ) of lookup(); the tabl...
bool contains(const K &k) const
true if the key is there
Slot
the state of a place
Definition OpenHashTable.h:268
@ DELETED
emptied by remove(): an inspection goes past it
Definition OpenHashTable.h:271
@ FULL
it holds a pair
Definition OpenHashTable.h:270
@ NIL
never used: it stops an inspection
Definition OpenHashTable.h:269
std::size_t moved(void) const
the number of pairs moved by the restructurings so far
std::size_t restructures(void) const
the number of restructurings so far
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
const_iterator cend(void) const
the constant iterator past the last item
Definition OpenHashTable.h:435
const_iterator begin(void) const
the iterator on the first item
Definition OpenHashTable.h:425
bool empty(void) const
true if there are no keys
void insert(const K &k, const V &v)
associates the value to the key ; throws std::overflow_error if the inspection finds no place
Slot slot(std::size_t i) const
the state of the place
const_iterator end(void) const
the iterator past the last item
Definition OpenHashTable.h:429
V Value
the type of the values
Definition OpenHashTable.h:265
void forEach(F &&f) const
the foreach of the book: f( k , v ) on the pairs, place after place, time
const_iterator iterator
the iterator on the items
Definition OpenHashTable.h:422
std::size_t capacity(void) const
the size of the vector
PairRef< const K &, const V & > reference
a reference to an item
Definition OpenHashTable.h:416
const K & keyAt(std::size_t i) const
the key in the place
K Key
the type of the keys
Definition OpenHashTable.h:264
OpenHashTable(std::size_t m, Probing probe, bool restructure=false, H h=H())
the empty table of size m with the given inspection, which restructures itself or not
const_iterator cbegin(void) const
the constant iterator on the first item
Definition OpenHashTable.h:433
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition OpenHashTable.h:414
double load(void) const
the load factor
const K * findKey(const K &k) const
the address of the key , nil if absent, in expected time: the find( x ) of a USet of Morin,...
V mapped_type
the type of the values
Definition OpenHashTable.h:410
std::size_t size_type
the type of the sizes
Definition OpenHashTable.h:412
std::size_t probes(void) const
the places examined by the last operation
K key_type
the type of the keys
Definition OpenHashTable.h:408
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 OpenHashTable.h:406
a sequence of inspection of an OpenHashTable
Definition OpenHashTable.h:163
static Probing pseudo_random(unsigned long seed=1)
, a random permutation of with , drawn from the seed for each
static Probing double_hashing(void)
Kind
the kind of the inspection
Definition OpenHashTable.h:168
@ RANDOM
Definition OpenHashTable.h:173
@ TRIANGULAR
Definition OpenHashTable.h:172
@ QUADRATIC
Definition OpenHashTable.h:170
@ LINEAR
Definition OpenHashTable.h:169
@ DOUBLE
Definition OpenHashTable.h:171
static Probing double_hashing(Step g)
, the step from
Kind kind(void) const
the kind of the inspection
static Probing quadratic(long c1, long c2)
, prime
static std::size_t step(std::size_t h, std::size_t m)
the step of the double hashing, coprime with when is prime or a power of two
void check(std::size_t m) const
checks that the sequence works with
static Probing linear(long delta=1)
bool works(std::size_t m) const
whether the sequence can work with a table of size
std::function< std::size_t(std::size_t) > Step
the second function of the double hashing, of the hash
Definition OpenHashTable.h:177
static Probing triangular(void)
, a power of two
std::size_t stride(std::size_t h, std::size_t m) const
the constant difference of the linear inspection and of the double hashing, so that a scan adds it i...
std::size_t at(std::size_t h, std::size_t i, std::size_t m) const
the place in a table of size , from
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
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
constexpr int NIL
no node
Definition Visit.h:128
Definition ADT.h:96
the places of the table, for SlotIterator
Definition OpenHashTable.h:391
static bool occupied(const auto &o, std::size_t i)
whether the place is full
Definition OpenHashTable.h:395
static auto read(const auto &o, std::size_t i)
the pair of the place
Definition OpenHashTable.h:399
static std::size_t size(const auto &o)
the number of places
Definition OpenHashTable.h:393