ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FlatHashTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File FlatHashTable.h --------------------------*/
3/*--------------------------------------------------------------------------*/
165/*--------------------------------------------------------------------------*/
166/*----------------------------- DEFINITIONS --------------------------------*/
167/*--------------------------------------------------------------------------*/
168
169#ifndef __ADS_FlatHashTable
170 #define __ADS_FlatHashTable
171/* self-identification: #endif constant */
172
173/*--------------------------------------------------------------------------*/
174/*------------------------------ INCLUDES ----------------------------------*/
175/*--------------------------------------------------------------------------*/
176
177#include "ADS/Abi.h"
178#include "ADS/Contract.h"
179#include "ADS/Iterators.h"
180#include "ADS/SimdInline.h"
181#include "ADS/Portable.h"
182#include "ADS/Wide.h"
185#include "ADS/memory/Prefetch.h"
186
187#include <array>
188#include <bit>
189#include <cstddef>
190#include <cstdint>
191#include <cstring>
192#include <functional>
193#include <memory>
194#include <new>
195#include <optional>
196#include <tuple>
197#include <type_traits>
198#include <utility>
199#include <vector>
200
201
202// the pairs of boost::unordered_flat_map, with ADS_FLATHASH_BOOST [see Abi.h]
203#if ADS_FLATHASH_BOOST
204 #include <iterator>
205
206 #include <boost/unordered/unordered_flat_map.hpp>
207#endif
208
209
211#define ADS_FLAT_INLINE ADS_ALWAYS_INLINE
212
214#define ADS_FLAT_NOINLINE ADS_NOINLINE
215
216/*--------------------------------------------------------------------------*/
217/*--------------------------- NAMESPACE ------------------------------------*/
218/*--------------------------------------------------------------------------*/
219
220namespace ADS
221{
222
223namespace detail
224{
226
229[[nodiscard]] [[gnu::always_inline]] inline std::uint64_t
230mulx( std::uint64_t a , std::uint64_t b );
231
233[[nodiscard]] constexpr std::uint64_t mulxPortable( std::uint64_t a ,
234 std::uint64_t b );
235
237} // end( namespace detail )
238
240
241/*--------------------------------------------------------------------------*/
242/*-------------------------- CLASS FlatHashTable ---------------------------*/
243/*--------------------------------------------------------------------------*/
244
246template< class K , class V , class H = DefaultHash< K > ,
247 class Eq = std::equal_to< K > ,
248 class A = std::allocator< std::pair< const K , V > > >
250 : private detail::AllocHolder<
251 std::conditional_t< ADS_FLATHASH_BOOST , std::allocator< char > , A > >
252{
254 using Holder = detail::AllocHolder<
255 std::conditional_t< ADS_FLATHASH_BOOST , std::allocator< char > , A > >;
256
257/*--------------------------------------------------------------------------*/
258 public:
259 using Key = K;
260 using Value = V;
261
263 explicit FlatHashTable( std::size_t n = 0 , H h = H() , Eq eq = Eq() );
264
267 FlatHashTable( std::size_t n , H h , Eq eq , const A & alloc );
268
270 explicit FlatHashTable( const A & alloc )
271 : FlatHashTable( 0 , H() , Eq() , alloc ) { }
272
276
278 FlatHashTable( const FlatHashTable & o , const A & alloc );
279
281 FlatHashTable( FlatHashTable && o ) noexcept;
282
285 FlatHashTable( FlatHashTable && o , const A & alloc );
286
289 requires detail::alwaysEqualAlloc< A >;
290
293 requires( ! detail::alwaysEqualAlloc< A > );
294
298 requires( ! detail::alwaysEqualAlloc< A > );
299
302
303/*--------------------------------------------------------------------------*/
304
326 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
327
329 [[nodiscard]] const V * find( const K & k ) const;
330
333 [[nodiscard]] V * find( const K & k );
334
359 bool insert( const K & k , const V & v );
360
362 bool insert( K && k , V && v );
363
370 template< class... Args >
371 std::pair< V * , bool > try_emplace( const K & k , Args &&... a );
372
374 template< class... Args >
375 std::pair< V * , bool > try_emplace( K && k , Args &&... a );
376
381 bool remove( const K & k );
382
384 [[nodiscard]] bool contains( const K & k ) const;
385
387 [[nodiscard]] std::size_t size( void ) const;
388
390 [[nodiscard]] bool empty( void ) const;
391
393 [[nodiscard]] std::size_t capacity( void ) const;
394
396 [[nodiscard]] double load( void ) const;
397
401 [[nodiscard]] std::size_t deleted( void ) const;
402
404 void reserve( std::size_t n );
405
407 void clear( void );
408
410 template< class F >
411 void forEach( F && f ) const;
412
415 template< class F >
416 void forEach( F && f );
417
421 void swap( FlatHashTable & o ) noexcept( detail::swapNoexcept< A > );
422
423/*------------------------- THE STANDARD INTERFACE -------------------------*/
424
425 // a constant view of the pairs ( key , value ), in the order of the
426 // places, bidirectional (forward with Boost); a removal invalidates only
427 // the iterators on the removed pair, an insertion that rebuilds the table
428 // all of them
429
431 using value_type = std::pair< K , V >;
433 using key_type = K;
435 using mapped_type = V;
437 using size_type = std::size_t;
439 using difference_type = std::ptrdiff_t;
444
446 class const_iterator;
447
450
452 using allocator_type = A;
453
455 [[nodiscard]] allocator_type get_allocator( void ) const;
456
458 [[nodiscard]] const_iterator begin( void ) const;
460 [[nodiscard]] const_iterator end( void ) const;
462 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
464 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
465
469
470#if ! ADS_FLATHASH_BOOST
471/*--------------------------------------------------------------------------*/
472 private:
473 static constexpr std::uint8_t EMPTY = 0;
474 static constexpr std::size_t GROUP = 16;
475 static constexpr std::size_t SLOTS = 15;
476 static constexpr std::size_t OVER = 15;
477
479 struct alignas( 16 ) Group
480 {
481 std::uint8_t c[ GROUP ];
482 };
483
486 using Mask = std::uint32_t;
487 static constexpr int STRIDE = 0;
488
490 static constexpr Mask FIRST = Mask( 1 ) << ( ( 1 << STRIDE ) - 1 );
491
493 union Slot
494 {
496 Slot( void ) { }
498 ~Slot() { }
499 std::pair< K , V > kv;
500 };
501
504 static constexpr std::size_t ALIGN = alignof( Slot ) > 64 ? alignof( Slot )
505 : 64;
506
511 static constexpr std::size_t JOIN = 4096;
512
516 [[nodiscard]] static std::size_t offset( std::size_t G );
517
523 [[nodiscard]] static Slot * allocate( std::size_t G , Group *& c );
524
527 static void deallocate( Slot * s , Group * c , std::size_t G ) noexcept;
528
530 static constexpr bool STD = detail::isStdAllocator< A >;
531
533 struct alignas( ALIGN ) Line
534 {
535 unsigned char b[ ALIGN ];
536 };
537
539 using LineAlloc = detail::RebindAlloc< A , Line >;
540
542 [[nodiscard]] static std::size_t lines( std::size_t b ) {
543 return( ( b + ALIGN - 1 ) / ALIGN );
544 }
545
548 Slot * obtain( std::size_t G , Group *& c );
549
552 void yield( Slot * s , Group * c , std::size_t G ) noexcept;
553
556 void copy( const FlatHashTable & o );
557
560 static constexpr bool AVALANCHING = detail::kAvalanching64< H , K >;
561
564 inline static Group s_none[ 3 ] = { {} , {} , { { 1 } } };
565
567 [[nodiscard]] std::uint64_t mix( const K & k ) const;
568
570 [[nodiscard]] static std::uint32_t tag( std::uint64_t x );
571
573 [[nodiscard]] static std::uint8_t bit( std::uint64_t x );
574
576 [[nodiscard]] std::size_t home( std::uint64_t x ) const;
577
580 [[nodiscard]] static Mask match( const Group & c , std::uint32_t t );
581
583 [[nodiscard]] static Mask empties( const Group & c );
584
586 [[nodiscard]] static Mask fulls( const Group & c );
587
589 [[nodiscard]] static std::size_t lowest( Mask m );
590
591#if ADS_FLAT_SIMD == 0
594 [[nodiscard]] static std::uint64_t word( const std::uint8_t * c );
595
598 [[nodiscard]] static std::uint64_t zeros( std::uint64_t w );
599
601 [[nodiscard]] static Mask gather( std::uint64_t z );
602#endif
603
606 [[nodiscard]] std::uint8_t & ctrl( std::size_t i ) const;
607
609 [[nodiscard]] Slot * slot( std::size_t i ) const;
610
613 [[nodiscard]] Slot * locate( std::uint64_t x , const K & k ) const;
614
617 [[nodiscard]] Slot * scan( std::size_t g , Mask m , const K & k ) const;
618
621 using KeyArg = std::conditional_t< std::is_trivially_copyable_v< K > &&
622 ( sizeof( K ) <= 2 * sizeof( void * ) ) ,
623 K , const K & >;
624
627 ADS_FLAT_NOINLINE Slot * further( std::uint64_t x , std::size_t g ,
628 KeyArg k ) const;
629
632 std::size_t claim( std::uint64_t x );
633
635 template< class KK , class VV >
636 bool put( KK && k , VV && v );
637
642 template< bool Assign , class KK , class... Args >
643 std::pair< V * , bool > place( KK && k , Args &&... a );
644
648 template< class KK , class... Args >
649 ADS_FLAT_NOINLINE std::size_t grow( std::uint64_t x , KK && k ,
650 Args &&... a );
651
653 void drop( std::size_t i );
654
659 ADS_FLAT_NOINLINE bool recount( void );
660
663 static void vacate( Group & c , std::size_t j );
664
667 template< class F >
668 void rebuild( std::size_t G , F && first );
669
671 [[nodiscard]] static std::size_t groups( std::size_t n );
672
675 [[nodiscard]] static constexpr std::size_t bound( std::size_t m );
676
678 template< class F >
679 void each( F && f ) const;
680
683 void release( bool pairs = true ) noexcept;
684
689 [[nodiscard]] bool consistent( std::uint64_t x ) const;
690
691 Group * f_ctrl = s_none;
692 Slot * f_slot = nullptr;
693 std::size_t f_cap = 0;
694 std::size_t f_mask = 0;
695 int f_shift = 63;
696 std::size_t f_max = 0;
697 std::size_t f_size = 0;
699 std::size_t f_deleted = 0;
700 H f_h;
701 Eq f_eq;
702
703/*--------------------------------------------------------------------------*/
704 public:
715 {
716 public:
718 using value_type = std::pair< K , V >;
722 using difference_type = std::ptrdiff_t;
724 using iterator_concept = std::bidirectional_iterator_tag;
726 using iterator_category = std::input_iterator_tag;
727
729 const_iterator( void ) = default;
730
732 reference operator*( void ) const {
733 const auto & kv = f_b[ lowest( f_m ) ].kv;
734 return( reference( kv.first , kv.second ) );
735 }
736
752 f_m &= ( f_m - 1 ) & fulls( *f_g );
753 if( ! f_m ) [[unlikely]]
754 forward();
755 return( *this );
756 }
757
760
763 auto t = *this;
764 ++*this;
765 return( t );
766 }
767
770 auto t = *this;
771 --*this;
772 return( t );
773 }
774
776 bool operator==( const const_iterator & o ) const {
777 return( f_b + lowest( f_m ) == o.f_b + lowest( o.f_m ) );
778 }
779
780 private:
781 friend class FlatHashTable;
782
785 const_iterator( const Group * g , const Slot * b , Mask m )
786 : f_g( g ) , f_b( b ) , f_m( m ) { }
787
789 void forward( void );
790
791 const Group * f_g = nullptr;
792 const Slot * f_b = nullptr;
793 Mask f_m = FIRST;
794
795 }; // end( class const_iterator )
796#else
797/*--------------------------------------------------------------------------*/
798 private:
800 using Map = boost::unordered_flat_map<
801 K , V , H , Eq , detail::RebindAlloc< A , std::pair< const K , V > > >;
802
805 [[nodiscard]] std::size_t fullLoad( void ) const;
806
807 Map f_map;
808
809/*--------------------------------------------------------------------------*/
810 public:
813 class const_iterator
814 {
815 public:
817 using value_type = std::pair< K , V >;
821 using difference_type = std::ptrdiff_t;
823 using iterator_concept = std::forward_iterator_tag;
825 using iterator_category = std::input_iterator_tag;
826
828 const_iterator( void ) = default;
829
831 reference operator*( void ) const {
832 return( reference( f_i->first , f_i->second ) );
833 }
834
836 const_iterator & operator++( void ) {
837 ++f_i;
838 return( *this );
839 }
840
843 auto t = *this;
844 ++f_i;
845 return( t );
846 }
847
849 bool operator==( const const_iterator & o ) const {
850 return( f_i == o.f_i );
851 }
852
853 private:
854 friend class FlatHashTable;
855
857 explicit const_iterator( typename Map::const_iterator i ) : f_i( i ) { }
858
859 typename Map::const_iterator f_i;
860
861 }; // end( class const_iterator )
862#endif
863
864 }; // end( class FlatHashTable )
865
866/*--------------------------------------------------------------------------*/
867
869
870} // end( namespace ADS )
871
872/*--------------------------------------------------------------------------*/
873/*-------------------------- IMPLEMENTATION --------------------------------*/
874/*--------------------------------------------------------------------------*/
875
876#include "ADS/dictionary/impl/FlatHashTable.tpp"
877
878/*--------------------------------------------------------------------------*/
879
880#endif /* FlatHashTable.h included */
881
882/*--------------------------------------------------------------------------*/
883/*------------------------ End File FlatHashTable.h ------------------------*/
884/*--------------------------------------------------------------------------*/
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....
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
The arithmetic of two words that C++20 does not spell, in one form for every compiler: the product of...
the iterator on the full places, by its group, the slot of the first place of the group and the mask ...
Definition FlatHashTable.h:715
const_iterator & operator++(void)
to the next full place
Definition FlatHashTable.h:751
friend class FlatHashTable
Definition FlatHashTable.h:781
PairRef< const K &, const V & > reference
a reference to an item
Definition FlatHashTable.h:720
const_iterator(void)=default
an iterator on no table
std::pair< K, V > value_type
the type of the items, for the standard library
Definition FlatHashTable.h:718
reference operator*(void) const
the pair of the place
Definition FlatHashTable.h:732
const_iterator & operator--(void)
to the previous full place
const_iterator operator--(int)
to the previous full place, returning the iterator before the step
Definition FlatHashTable.h:769
std::bidirectional_iterator_tag iterator_concept
the concept of the iterator, for C++20
Definition FlatHashTable.h:724
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition FlatHashTable.h:722
std::input_iterator_tag iterator_category
the category of the iterator
Definition FlatHashTable.h:726
const_iterator operator++(int)
to the next full place, returning the iterator before the step
Definition FlatHashTable.h:762
bool operator==(const const_iterator &o) const
equal on the same place
Definition FlatHashTable.h:776
V mapped_type
the type of the values
Definition FlatHashTable.h:435
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
const_iterator iterator
the iterator on the items
Definition FlatHashTable.h:449
FlatHashTable(const A &alloc)
the empty table, its places from alloc
Definition FlatHashTable.h:270
K Key
the type of the keys
Definition FlatHashTable.h:259
allocator_type get_allocator(void) const
the allocator of the places
std::size_t deleted(void) const
the places emptied by remove() in groups that some inspection went past, which count as full until th...
V Value
the type of the values
Definition FlatHashTable.h:260
void clear(void)
removes all the pairs, keeping the places
void swap(FlatHashTable &o) noexcept(detail::swapNoexcept< A >)
exchanges the contents with other in , and the allocators if they propagate on a swap
std::size_t size_type
the type of the sizes
Definition FlatHashTable.h:437
void forEach(F &&f)
f( k , v ) on the pairs, place after place, to change in place
std::size_t capacity(void) const
the number of places, 15 times 0 or a power of 2 at least 2
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition FlatHashTable.h:439
std::size_t size(void) const
the number of pairs
FlatHashTable & operator=(FlatHashTable o) noexcept
the assignment, by copy and swap, when the allocators are all equal
FlatHashTable(std::size_t n=0, H h=H(), Eq eq=Eq())
the empty table, with room for pairs before it grows
FlatHashTable(std::size_t n, H h, Eq eq, const A &alloc)
the empty table, with room for pairs before it grows, its places from alloc
void forEach(F &&f) const
f( k , v ) on the pairs, place after place
const_iterator cbegin(void) const
the constant iterator on the first item
Definition FlatHashTable.h:462
FlatHashTable & operator=(FlatHashTable &&o)
the move assignment: the places of o if its allocator propagates or is equal, else its pairs moved on...
A allocator_type
the type of the allocator
Definition FlatHashTable.h:452
std::pair< V *, bool > try_emplace(K &&k, Args &&... a)
as try_emplace( k , a ), moving the key
bool remove(const K &k)
removes the key , if it is there, emptying its place, with no deleted mark, and adding 1 to the bound...
bool insert(K &&k, V &&v)
as insert( k , v ), moving the key and the value
const_iterator erase(const_iterator it)
removes the pair of it, and returns the iterator on the next one
void reserve(std::size_t n)
room for pairs before the table grows
~FlatHashTable()
frees the memory of the structure
V * find(const K &k)
the value of the key , to change in place, nil if it is not there
FlatHashTable & operator=(const FlatHashTable &o)
the copy assignment, with the allocator of o if it propagates
PairRef< const K &, const V & > const_reference
a constant reference to an item
Definition FlatHashTable.h:443
std::pair< K, V > value_type
the type of the items, for the standard library
Definition FlatHashTable.h:431
FlatHashTable(const FlatHashTable &o, const A &alloc)
a copy of other, on alloc
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...
const_iterator end(void) const
the iterator past the last item
FlatHashTable(FlatHashTable &&o, const A &alloc)
the pairs of o, on alloc: its places if the allocators are equal, else the pairs moved one by one to ...
bool contains(const K &k) const
true if the key is there
double load(void) const
the load factor , 0 with no places
FlatHashTable(FlatHashTable &&o) noexcept
the pairs of o, which is left empty, with no places
bool insert(const K &k, const V &v)
associates the value to the key ; true if was not there
bool empty(void) const
true if there are no pairs
const_iterator begin(void) const
the iterator on the first item
const V * find(const K &k) const
the value of the key , nil if it is not there
FlatHashTable(const FlatHashTable &o)
a copy of other, on the allocator that select_on_container_copy_construction() gives
K key_type
the type of the keys
Definition FlatHashTable.h:433
PairRef< const K &, const V & > reference
a reference to an item
Definition FlatHashTable.h:441
const_iterator cend(void) const
the constant iterator past the last item
Definition FlatHashTable.h:464
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
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_FLAT_NOINLINE
a function never inlined, the rare path out of a loop
Definition FlatHashTable.h:214
Definition ADT.h:96