ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DenseHashMap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File DenseHashMap.h --------------------------*/
3/*--------------------------------------------------------------------------*/
120/*--------------------------------------------------------------------------*/
121/*----------------------------- DEFINITIONS --------------------------------*/
122/*--------------------------------------------------------------------------*/
123
124#ifndef __ADS_DenseHashMap
125 #define __ADS_DenseHashMap
126/* self-identification: #endif constant */
127
128/*--------------------------------------------------------------------------*/
129/*------------------------------ INCLUDES ----------------------------------*/
130/*--------------------------------------------------------------------------*/
131
132#include "ADS/Abi.h"
133#include "ADS/Contract.h"
134#include "ADS/Modification.h"
138
139#include <bit>
140#include <cstddef>
141#include <cstdint>
142#include <cstring>
143#include <functional>
144#include <memory>
145#include <new>
146#include <optional>
147#include <tuple>
148#include <type_traits>
149#include <utility>
150#include <vector>
151
152/*--------------------------------------------------------------------------*/
153/*--------------------------- NAMESPACE ------------------------------------*/
154/*--------------------------------------------------------------------------*/
155
156namespace ADS
157{
158
160
161/*--------------------------------------------------------------------------*/
162/*--------------------------- CLASS DenseHashMap ---------------------------*/
163/*--------------------------------------------------------------------------*/
164
167template< class K , class V , class H = DefaultHash< K > ,
168 class Eq = std::equal_to< K > ,
169 class A = std::allocator< std::pair< const K , V > > ,
170 class Policy = NoObservers >
172{
173
174/*--------------------------------------------------------------------------*/
175 public:
176 using Key = K;
177 using Value = V;
178
185
191 using Subject = typename Policy::template bind< modifications ,
192 DenseHashMap >;
193
196 template< class... O >
197 explicit DenseHashMap( std::size_t n = 0 , H h = H() , Eq eq = Eq() ,
198 const A & alloc = A() , O &... o );
199
203
205 DenseHashMap( const DenseHashMap & o , const A & alloc );
206
208 DenseHashMap( DenseHashMap && o ) noexcept;
209
212 DenseHashMap( DenseHashMap && o , const A & alloc );
213
216 requires detail::alwaysEqualAlloc< A >;
217
220 requires( ! detail::alwaysEqualAlloc< A > && Subject::none )
221 {
222 return( detail::copyAssign( *this , o ) );
223 }
224
228 requires( ! detail::alwaysEqualAlloc< A > && Subject::none )
229 {
230 return( detail::moveAssign( *this , std::move( o ) ) );
231 }
232
234 ~DenseHashMap() = default;
235
236/*--------------------------------------------------------------------------*/
237
259 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
260
262 [[nodiscard]] const V * find( const K & k ) const;
263
266 [[nodiscard]] V * find( const K & k );
267
289 bool insert( const K & k , const V & v );
290
292 bool insert( K && k , V && v );
293
300 template< class... Args >
301 std::pair< V * , bool > try_emplace( const K & k , Args &&... a );
302
304 template< class... Args >
305 std::pair< V * , bool > try_emplace( K && k , Args &&... a );
306
327 bool remove( const K & k );
328
330 [[nodiscard]] bool contains( const K & k ) const;
331
333 [[nodiscard]] std::size_t size( void ) const;
334
336 [[nodiscard]] bool empty( void ) const;
337
340 [[nodiscard]] std::size_t capacity( void ) const;
341
343 [[nodiscard]] double load( void ) const;
344
348 [[nodiscard]] std::size_t deleted( void ) const;
349
351 void reserve( std::size_t n );
352
354 void clear( void );
355
357 template< class F >
358 void forEach( F && f ) const;
359
361 [[nodiscard]] const std::pair< K , V > * data( void ) const;
362
366 void swap( DenseHashMap & o ) noexcept( detail::swapNoexcept< A > );
367
368/*--------------------------- THE OBSERVERS --------------------------------*/
369
371 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
373 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
374
377 template< class O >
378 int attach( O & o )
379 requires( Subject::dynamic )
380 {
381 return( f_sub.attach( o ) );
382 }
383
386 void detach( int id )
387 requires( Subject::dynamic )
388 {
389 f_sub.detach( id );
390 }
391
392/*------------------------- THE STANDARD INTERFACE -------------------------*/
393
394 // a constant view of the pairs, in the order of the vector, random
395 // access; an insertion of a new key or a removal invalidates them
396
398 using value_type = std::pair< K , V >;
400 using size_type = std::size_t;
402 using difference_type = std::ptrdiff_t;
404 using reference = const std::pair< K , V > &;
406 using const_reference = const std::pair< K , V > &;
409 typename detail::AllocVector< A , std::pair< K , V > >::const_iterator;
412
414 using allocator_type = A;
416 [[nodiscard]] allocator_type get_allocator( void ) const {
417 return( allocator_type( f_kv.get_allocator() ) );
418 }
419
421 [[nodiscard]] const_iterator begin( void ) const { return( f_kv.cbegin() ); }
423 [[nodiscard]] const_iterator end( void ) const { return( f_kv.cend() ); }
425 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
427 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
428
429/*--------------------------------------------------------------------------*/
430 private:
431 static constexpr std::uint8_t EMPTY = 0;
432 static constexpr std::size_t SLOTS = 12;
433 static constexpr std::size_t OVER = 15;
434 static constexpr std::uint32_t PLACES = 0xFFF;
435
438 struct alignas( 64 ) Group
439 {
440 std::uint8_t c[ 16 ];
441 std::uint32_t at[ SLOTS ];
442 };
443
446 using Mask = std::uint32_t;
447
450 static constexpr bool AVALANCHING = detail::kAvalanching64< H , K >;
451
454 inline static Group s_none[ 2 ] = {};
455
457 [[nodiscard]] std::uint64_t mix( const K & k ) const;
458
460 [[nodiscard]] static std::uint8_t bit( std::uint64_t x );
461
463 [[nodiscard]] std::size_t home( std::uint64_t x ) const;
464
467 [[nodiscard]] static Mask match( const Group & c , std::uint32_t t );
468
470 [[nodiscard]] static Mask empties( const Group & c );
471
474 static void vacate( Group & c , std::size_t j );
475
478 [[nodiscard]] std::size_t locate( std::uint64_t x , const K & k ) const;
479
481 using KeyArg = std::conditional_t< std::is_trivially_copyable_v< K > &&
482 ( sizeof( K ) <= 2 * sizeof( void * ) ) ,
483 K , const K & >;
484
488 ADS_FLAT_NOINLINE std::pair< std::size_t , std::size_t >
489 further( std::uint64_t x , std::size_t g , KeyArg k ) const;
490
494 std::size_t claim( std::uint64_t x );
495
498 std::uint32_t & holder( std::uint64_t x , std::size_t p );
499
501 template< class KK , class VV >
502 bool put( KK && k , VV && v );
503
508 template< bool Assign , class KK , class... Args >
509 std::pair< V * , bool > place( KK && k , Args &&... a );
510
512 bool removePlain( const K & k );
513
516 template< class KK , class VV >
517 bool putTold( KK && k , VV && v );
518 template< class KK , class... Args >
519 std::pair< V * , bool > emplaceTold( KK && k ,
520 Args &&... a );
521 bool removeTold( const K & k );
522
524 template< class KK , class VV >
525 ADS_COLD_PATH bool putCold( KK && k , VV && v ) {
526 return( putTold( std::forward< KK >( k ) , std::forward< VV >( v ) ) );
527 }
528 template< class KK , class... Args >
529 ADS_COLD_PATH std::pair< V * , bool > emplaceCold( KK && k , Args &&... a ) {
530 return(
531 emplaceTold( std::forward< KK >( k ) , std::forward< Args >( a )... ) );
532 }
533 ADS_COLD_PATH bool removeCold( const K & k ) { return( removeTold( k ) ); }
534
536 void tellCleared( void ) noexcept;
537 void tellRebuilt( void ) noexcept;
538
541 void unlink( Group & g , std::size_t j , std::size_t p );
542
546 void rebuild( std::size_t G );
547
549 ADS_FLAT_NOINLINE void grow( void );
550
552 [[nodiscard]] static std::size_t groups( std::size_t n );
553
556 [[nodiscard]] static constexpr std::size_t bound( std::size_t m );
557
561 [[nodiscard]] bool consistent( std::uint64_t x ) const;
562
564 struct Free
565 {
567 void operator()( Group * g ) const noexcept;
568 };
569
571 static constexpr bool STD = detail::isStdAllocator< A >;
572
574 using Pairs = detail::AllocVector< A , std::pair< K , V > >;
575
577 using Groups = std::conditional_t< STD , std::unique_ptr< Group[] , Free > ,
578 detail::AllocVector< A , Group > >;
579
581 [[nodiscard]] static Groups noGroups( const Pairs & kv ) {
582 if constexpr( STD )
583 return( Groups() );
584 else
585 return( Groups( kv.get_allocator() ) );
586 }
587
590 [[nodiscard]] static Groups newGroups( std::size_t G , const Pairs & kv ) {
591 if constexpr( STD )
592 return( Groups( static_cast< Group * >( ::operator new(
593 G * sizeof( Group ) , std::align_val_t( alignof( Group ) ) ) ) ) );
594 else {
595 Groups g( kv.get_allocator() );
596 g.resize( G );
597 return( g );
598 }
599 }
600
602 [[nodiscard]] static Group * first( Groups & g ) {
603 if constexpr( STD )
604 return( g.get() );
605 else
606 return( g.data() );
607 }
608
610 void copyGroups( const DenseHashMap & o );
611
612 Pairs f_kv;
613 Groups f_g = noGroups( f_kv );
614 Group * f_ctrl = s_none;
615 std::size_t f_mask = 0;
616 std::size_t f_cap = 0;
617 int f_shift = 63;
618 std::size_t f_max = 0;
619 std::size_t f_deleted = 0;
620 H f_h;
621 Eq f_eq;
623
624 }; // end( class DenseHashMap )
625
626/*--------------------------------------------------------------------------*/
627
629
630} // end( namespace ADS )
631
632/*--------------------------------------------------------------------------*/
633/*-------------------------- IMPLEMENTATION --------------------------------*/
634/*--------------------------------------------------------------------------*/
635
636#include "ADS/dictionary/impl/DenseHashMap.tpp"
637
638/*--------------------------------------------------------------------------*/
639
640#endif /* DenseHashMap.h included */
641
642/*--------------------------------------------------------------------------*/
643/*------------------------ End File DenseHashMap.h -------------------------*/
644/*--------------------------------------------------------------------------*/
The name of the configuration of the layouts, an inline namespace: the headers whose classes change t...
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
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 hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of AD...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
const_iterator begin(void) const
the iterator on the first item
Definition DenseHashMap.h:421
void clear(void)
removes all the pairs, keeping the places
DenseHashMap(const DenseHashMap &o)
a copy of other, on the allocator that select_on_container_copy_construction() gives
K Key
the type of the keys
Definition DenseHashMap.h:176
DenseHashMap(const DenseHashMap &o, const A &alloc)
a copy of other, on alloc
mod::Assigned< K, V > Assigned
a change of the value of a key
Definition DenseHashMap.h:184
void swap(DenseHashMap &o) noexcept(detail::swapNoexcept< A >)
exchanges the contents with other in , and the allocators if they propagate on a swap
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
void detach(int id)
detaches the observer number
Definition DenseHashMap.h:386
std::pair< V *, bool > try_emplace(const K &k, Args &&... a)
the value of the key , built from a if was not there, and true in that case; a key already there kee...
bool remove(const K &k)
removes the key , if it is there, moving the last pair into its position; true if it was
DenseHashMap(DenseHashMap &&o) noexcept
the pairs of o, which is left empty, with no places
~DenseHashMap()=default
frees the pairs and the groups
const Subject & subject(void) const
the subject
Definition DenseHashMap.h:373
DenseHashMap(std::size_t n=0, H h=H(), Eq eq=Eq(), const A &alloc=A(), O &... o)
the empty map, with room for pairs before it grows, its pairs and groups from alloc,...
V * find(const K &k)
the value of the key , to change in place, nil if it is not there
V Value
the type of the values
Definition DenseHashMap.h:177
bool contains(const K &k) const
true if the key is there
const V * find(const K &k) const
the value of the key , nil if it is not there
void reserve(std::size_t n)
room for pairs before the map grows
mod::Erased< K, K, V > Erased
an erasure of a key, told before the removal
Definition DenseHashMap.h:182
DenseHashMap & operator=(DenseHashMap &&o)
the move assignment: the memory of o if its allocator propagates or is equal, else its pairs moved on...
Definition DenseHashMap.h:227
mod::list< Inserted, Erased, Assigned, mod::Cleared, mod::Rebuilt > modifications
the events: a key inserted or erased with its value, its value changed, the map emptied or rebuilt
Definition DenseHashMap.h:188
std::size_t capacity(void) const
the number of places of the index, 12 times 0 or a power of 2 at least 2
const_iterator cend(void) const
the constant iterator past the last item
Definition DenseHashMap.h:427
std::pair< K, V > value_type
the type of the items, for the standard library
Definition DenseHashMap.h:398
std::size_t size_type
the type of the sizes
Definition DenseHashMap.h:400
typename Policy::template bind< modifications, DenseHashMap > Subject
the subject
Definition DenseHashMap.h:191
bool insert(K &&k, V &&v)
as insert( k , v ), moving the key and the value
const std::pair< K, V > & const_reference
a constant reference to an item
Definition DenseHashMap.h:406
const std::pair< K, V > & reference
a reference to an item
Definition DenseHashMap.h:404
void forEach(F &&f) const
f( k , v ) on the pairs, in the order of the vector
double load(void) const
the load factor of the index, 0 with no places
const_iterator iterator
the iterator on the items
Definition DenseHashMap.h:411
std::pair< V *, bool > try_emplace(K &&k, Args &&... a)
as try_emplace( k , a ), moving the key
DenseHashMap & operator=(DenseHashMap o) noexcept
the assignment, by copy and swap, when the allocators are all equal
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition DenseHashMap.h:402
DenseHashMap(DenseHashMap &&o, const A &alloc)
the pairs of o, which is left empty, on alloc: moved one by one if the allocators are not equal
std::size_t size(void) const
the number of pairs
bool empty(void) const
true if there are no pairs
mod::Inserted< K, K, V > Inserted
an insertion of a key, the key its handle
Definition DenseHashMap.h:180
int attach(O &o)
attaches the observer and gives its number
Definition DenseHashMap.h:378
std::size_t deleted(void) const
the places of the index emptied by remove() in groups that some inspection went past,...
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition DenseHashMap.h:371
typename detail::AllocVector< A, std::pair< K, V > >::const_iterator const_iterator
the iterator on the items, constant
Definition DenseHashMap.h:408
bool insert(const K &k, const V &v)
associates the value to the key ; true if was not there
const std::pair< K, V > * data(void) const
the pairs, in the order of the vector
const_iterator cbegin(void) const
the constant iterator on the first item
Definition DenseHashMap.h:425
DenseHashMap & operator=(const DenseHashMap &o)
the copy assignment, with the allocator of o if it propagates
Definition DenseHashMap.h:219
A allocator_type
the type of the allocator
Definition DenseHashMap.h:414
const_iterator end(void) const
the iterator past the last item
Definition DenseHashMap.h:423
allocator_type get_allocator(void) const
the allocator of the pairs and of the groups
Definition DenseHashMap.h:416
a monoid with the inverse G::inverse( a ), i.e., combine( inverse( a) , a ) and combine( a ,...
Definition Algebra.h:130
#define ADS_ABI_BEGIN
opens the inline namespace of the configuration
Definition Abi.h:106
#define ADS_ABI_END
closes the inline namespace of the configuration
Definition Abi.h:111
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#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
#define ADS_FLAT_NOINLINE
a function never inlined, the rare path out of a loop
Definition FlatHashTable.h:214
Definition ADT.h:96
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482