ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ChainedHashTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File ChainedHashTable.h -------------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_ChainedHashTable
60 #define __ADS_ChainedHashTable
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Contract.h"
68#include "ADS/Portable.h"
70#include "ADS/sequence/List.h"
71
72#include <concepts>
73#include <cstddef>
74#include <functional>
75#include <iterator>
76#include <optional>
77#include <utility>
78#include <vector>
79
80/*--------------------------------------------------------------------------*/
81/*--------------------------- NAMESPACE ------------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84namespace ADS
85{
86
87/*--------------------------------------------------------------------------*/
88/*------------------------- CLASS ChainedHashTable -------------------------*/
89/*--------------------------------------------------------------------------*/
90
97
99template< class K , class V , Chain C = Chain::FRONT ,
100 class H = std::hash< K > >
101 requires( C != Chain::SORTED || std::totally_ordered< K > )
103{
104
105/*--------------------------------------------------------------------------*/
106 public:
107 using Key = K;
108 using Value = V;
109
113 explicit ChainedHashTable( std::size_t m , bool restructure = false ,
114 H h = H() );
115
125 ChainedHashTable( std::size_t m , double g , double s , H h = H() );
126
127/*--------------------------------------------------------------------------*/
128
146 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
147
168 void insert( const K & k , const V & v );
169
174 void remove( const K & k );
175
177 [[nodiscard]] bool contains( const K & k ) const;
178
183 [[nodiscard]] const K * findKey( const K & k ) const;
184
186 [[nodiscard]] std::size_t size( void ) const;
187
189 [[nodiscard]] bool empty( void ) const;
190
192 [[nodiscard]] std::size_t capacity( void ) const;
193
195 [[nodiscard]] double load( void ) const;
196
199 template< class F >
200 void forEach( F && f ) const;
201
204 [[nodiscard]] std::vector< K > chain( std::size_t i ) const;
205
208 [[nodiscard]] std::size_t probes( void ) const;
209
211 [[nodiscard]] std::size_t restructures( void ) const;
212
214 [[nodiscard]] std::size_t moved( void ) const;
215
216/*--------------------------------------------------------------------------*/
217 private:
218 using Pair = std::pair< K , V >;
219 using Bucket = List< Pair , false >;
220 using Pos = typename Bucket::Pos;
221
222 public:
223/*------------------------- THE STANDARD INTERFACE -------------------------*/
224
225 // a constant view of the pairs ( key , value ), list by list, forward;
226 // with Chain::MOVE_TO_FRONT even a lookup moves a pair, and with the
227 // restructuring an insertion or a removal rebuilds the table, both of
228 // which invalidate it
229
232 {
233 using Lists = std::vector< Bucket >;
234
235 public:
237 using iterator_category = std::forward_iterator_tag;
239 using value_type = Pair;
241 using difference_type = std::ptrdiff_t;
243 using reference = const Pair &;
245 using pointer = const Pair *;
246
248 const_iterator( void ) = default;
249
251 const_iterator( const Lists * T , std::size_t b ) : f_T( T ) , f_b( b ) {
252 if( f_b < f_T->size() )
253 f_c = ( *f_T )[ f_b ].begin();
254 settle();
255 }
256
258 reference operator*( void ) const { return( *f_c ); }
259
261 pointer operator->( void ) const { return( &*f_c ); }
262
266 ++f_c;
267 settle();
268 return( *this );
269 }
270
273 auto t = *this;
274 ++*this;
275 return( t );
276 }
277
279 bool operator==( const const_iterator & o ) const {
280 return(
281 ( f_b == o.f_b ) &&
282 ( ( f_T == nullptr ) || ( f_b >= f_T->size() ) || ( f_c == o.f_c ) ) );
283 }
284
285 private:
286 // past the empty lists; out of line, since the optimizer of MSVC does
287 // not end on its loop inlined into a copy of the table
288 ADS_NOINLINE void settle( void ) {
289 while( ( f_b < f_T->size() ) && ( f_c == ( *f_T )[ f_b ].end() ) )
290 if( ++f_b < f_T->size() )
291 f_c = ( *f_T )[ f_b ].begin();
292 }
293
294 const Lists * f_T = nullptr;
295 std::size_t f_b = 0;
296 typename Bucket::const_iterator f_c;
297 };
298
300 using value_type = Pair;
302 using size_type = std::size_t;
304 using difference_type = std::ptrdiff_t;
306 using reference = const Pair &;
308 using const_reference = const Pair &;
311
313 [[nodiscard]] const_iterator begin( void ) const {
314 return( const_iterator( &f_T , 0 ) );
315 }
316
317 [[nodiscard]] const_iterator end( void ) const {
318 return( const_iterator( &f_T , f_T.size() ) );
319 }
320
321 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
323 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
324
325 private:
328 [[nodiscard]] std::size_t place( const K & k ) const;
329
332 Pos find( const Bucket & L , const K & k ) const;
333
335 [[nodiscard]] bool present( const K & k ) const;
336
340 [[nodiscard]] bool consistent( const K & k ) const;
341
344 [[nodiscard]] bool listOk( std::size_t i , const K & k ,
345 std::size_t & n ) const;
346
348 void rebuild( std::size_t m );
349
350 mutable std::vector< Bucket > f_T;
351 std::size_t f_m0;
352 bool f_restructure;
353 double f_grow = 2;
354 double f_shrink = 0.5;
355 H f_h;
356 std::size_t f_size = 0;
357 mutable std::size_t f_probes = 0;
358 std::size_t f_rebuilds = 0;
359 std::size_t f_moved = 0;
360
361 }; // end( class ChainedHashTable )
362
363/*--------------------------------------------------------------------------*/
364
365} // end( namespace ADS )
366
367/*--------------------------------------------------------------------------*/
368/*-------------------------- IMPLEMENTATION --------------------------------*/
369/*--------------------------------------------------------------------------*/
370
371#include "ADS/dictionary/impl/ChainedHashTable.tpp"
372
373/*--------------------------------------------------------------------------*/
374
375#endif /* ChainedHashTable.h included */
376
377/*--------------------------------------------------------------------------*/
378/*---------------------- End File ChainedHashTable.h -----------------------*/
379/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The sizes of the hash tables that restructure themselves (section 7.4): a size that is a power of two...
the iterator on the pairs, list by list
Definition ChainedHashTable.h:232
bool operator==(const const_iterator &o) const
equal on the same pair, or both at the end
Definition ChainedHashTable.h:279
pointer operator->(void) const
the address of the pair
Definition ChainedHashTable.h:261
const_iterator & operator++(void)
to the next pair, on the next list that is not empty after the last pair of a list
Definition ChainedHashTable.h:265
std::forward_iterator_tag iterator_category
the category of the iterator
Definition ChainedHashTable.h:237
const_iterator(void)=default
the end of no table
const_iterator operator++(int)
to the next pair, returning the iterator before the step
Definition ChainedHashTable.h:272
const Pair & reference
a reference to an item
Definition ChainedHashTable.h:243
const Pair * pointer
a pointer to an item
Definition ChainedHashTable.h:245
Pair value_type
the type of the items, for the standard library
Definition ChainedHashTable.h:239
reference operator*(void) const
the pair
Definition ChainedHashTable.h:258
const_iterator(const Lists *T, std::size_t b)
on the first pair from the list , or at the end
Definition ChainedHashTable.h:251
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition ChainedHashTable.h:241
double load(void) const
the load factor
bool empty(void) const
true if there are no keys
const Pair & const_reference
a constant reference to an item
Definition ChainedHashTable.h:308
std::size_t restructures(void) const
the number of restructurings so far
void insert(const K &k, const V &v)
associates the value to the key
std::vector< K > chain(std::size_t i) const
the keys of the list , from its head
bool contains(const K &k) const
true if the key is there
K Key
the type of the keys
Definition ChainedHashTable.h:107
const_iterator cbegin(void) const
the constant iterator on the first item
Definition ChainedHashTable.h:321
const_iterator end(void) const
the iterator past the last item
Definition ChainedHashTable.h:317
std::size_t size_type
the type of the sizes
Definition ChainedHashTable.h:302
std::size_t size(void) const
the number of keys
const Pair & reference
a reference to an item
Definition ChainedHashTable.h:306
std::size_t moved(void) const
the number of pairs moved by the restructurings so far
const_iterator begin(void) const
the iterator on the first item
Definition ChainedHashTable.h:313
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,...
const_iterator iterator
the iterator on the items
Definition ChainedHashTable.h:310
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition ChainedHashTable.h:304
V Value
the type of the values
Definition ChainedHashTable.h:108
std::size_t probes(void) const
the cells of the list examined by the last operation, plus one for the nil that ends an unsuccessful ...
const_iterator cend(void) const
the constant iterator past the last item
Definition ChainedHashTable.h:323
ChainedHashTable(std::size_t m, bool restructure=false, H h=H())
the empty table of lists, which restructures itself or not, at the loads 2 and
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
std::size_t capacity(void) const
the number of lists
void remove(const K &k)
removes the key , if it is there, from its list; the table, if it restructures itself,...
Pair value_type
the type of the items, for the standard library
Definition ChainedHashTable.h:300
void forEach(F &&f) const
the foreach of the book: f( k , v ) on the pairs, list after list, time
ChainedHashTable(std::size_t m, double g, double s, H h=H())
the empty table of lists, which doubles when its load exceeds and halves, down to ,...
a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled
Definition List.h:118
Cell * Pos
Definition List.h:132
#define ADS_NOINLINE
a function never inlined
Definition Portable.h:99
Chain
how a ChainedHashTable keeps its lists
Definition ChainedHashTable.h:92
@ MOVE_TO_FRONT
also the key found by lookup() goes to the head
Definition ChainedHashTable.h:94
@ SORTED
the keys of a list in increasing order
Definition ChainedHashTable.h:95
@ FRONT
a new key at the head, nothing moves
Definition ChainedHashTable.h:93
Definition ADT.h:96