169#ifndef __ADS_FlatHashTable
170 #define __ADS_FlatHashTable
197#include <type_traits>
203#if ADS_FLATHASH_BOOST
206 #include <boost/unordered/unordered_flat_map.hpp>
211#define ADS_FLAT_INLINE ADS_ALWAYS_INLINE
214#define ADS_FLAT_NOINLINE ADS_NOINLINE
229[[nodiscard]] [[gnu::always_inline]]
inline std::uint64_t
230mulx( std::uint64_t a , std::uint64_t b );
233[[nodiscard]]
constexpr std::uint64_t mulxPortable( std::uint64_t a ,
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 > >
254 using Holder = detail::AllocHolder<
255 std::conditional_t< ADS_FLATHASH_BOOST , std::allocator< char > , A > >;
289 requires detail::alwaysEqualAlloc< A >;
293 requires( ! detail::alwaysEqualAlloc< A > );
298 requires( ! detail::alwaysEqualAlloc< A > );
326 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
329 [[nodiscard]]
const V *
find(
const K & k )
const;
333 [[nodiscard]] V *
find(
const K & k );
359 bool insert(
const K & k ,
const V & v );
370 template<
class... Args >
371 std::pair< V * , bool >
try_emplace(
const K & k , Args &&... a );
374 template<
class... Args >
387 [[nodiscard]] std::size_t
size(
void )
const;
390 [[nodiscard]]
bool empty(
void )
const;
396 [[nodiscard]]
double load(
void )
const;
401 [[nodiscard]] std::size_t
deleted(
void )
const;
470#if ! ADS_FLATHASH_BOOST
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;
479 struct alignas( 16 )
Group
481 std::uint8_t c[ GROUP ];
486 using Mask = std::uint32_t;
487 static constexpr int STRIDE = 0;
490 static constexpr Mask FIRST = Mask( 1 ) << ( ( 1 << STRIDE ) - 1 );
499 std::pair< K , V > kv;
504 static constexpr std::size_t ALIGN =
alignof( Slot ) > 64 ?
alignof( Slot )
511 static constexpr std::size_t JOIN = 4096;
516 [[nodiscard]]
static std::size_t offset( std::size_t G );
523 [[nodiscard]]
static Slot * allocate( std::size_t G , Group *& c );
527 static void deallocate( Slot * s , Group * c , std::size_t G )
noexcept;
530 static constexpr bool STD = detail::isStdAllocator< A >;
533 struct alignas( ALIGN ) Line
535 unsigned char b[ ALIGN ];
539 using LineAlloc = detail::RebindAlloc< A , Line >;
542 [[nodiscard]]
static std::size_t lines( std::size_t b ) {
543 return( ( b + ALIGN - 1 ) / ALIGN );
548 Slot * obtain( std::size_t G , Group *& c );
552 void yield( Slot * s , Group * c , std::size_t G )
noexcept;
560 static constexpr bool AVALANCHING = detail::kAvalanching64< H , K >;
564 inline static Group s_none[ 3 ] = { {} , {} , { { 1 } } };
567 [[nodiscard]] std::uint64_t mix(
const K & k )
const;
570 [[nodiscard]]
static std::uint32_t tag( std::uint64_t x );
573 [[nodiscard]]
static std::uint8_t bit( std::uint64_t x );
576 [[nodiscard]] std::size_t home( std::uint64_t x )
const;
580 [[nodiscard]]
static Mask match(
const Group & c , std::uint32_t t );
583 [[nodiscard]]
static Mask empties(
const Group & c );
586 [[nodiscard]]
static Mask fulls(
const Group & c );
589 [[nodiscard]]
static std::size_t lowest( Mask m );
591#if ADS_FLAT_SIMD == 0
594 [[nodiscard]]
static std::uint64_t word(
const std::uint8_t * c );
598 [[nodiscard]]
static std::uint64_t zeros( std::uint64_t w );
601 [[nodiscard]]
static Mask gather( std::uint64_t z );
606 [[nodiscard]] std::uint8_t & ctrl( std::size_t i )
const;
609 [[nodiscard]] Slot * slot( std::size_t i )
const;
613 [[nodiscard]] Slot * locate( std::uint64_t x ,
const K & k )
const;
617 [[nodiscard]] Slot * scan( std::size_t g , Mask m ,
const K & k )
const;
621 using KeyArg = std::conditional_t< std::is_trivially_copyable_v< K > &&
622 (
sizeof( K ) <= 2 *
sizeof(
void * ) ) ,
632 std::size_t claim( std::uint64_t x );
635 template<
class KK ,
class VV >
636 bool put( KK && k , VV && v );
642 template<
bool Assign ,
class KK ,
class... Args >
643 std::pair< V * , bool > place( KK && k , Args &&... a );
648 template<
class KK ,
class... Args >
653 void drop( std::size_t i );
663 static void vacate( Group & c , std::size_t j );
668 void rebuild( std::size_t G , F && first );
671 [[nodiscard]]
static std::size_t groups( std::size_t n );
675 [[nodiscard]]
static constexpr std::size_t bound( std::size_t m );
679 void each( F && f )
const;
683 void release(
bool pairs =
true ) noexcept;
689 [[nodiscard]]
bool consistent( std::uint64_t x ) const;
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;
696 std::
size_t f_max = 0;
697 std::
size_t f_size = 0;
699 std::
size_t f_deleted = 0;
733 const auto & kv = f_b[ lowest( f_m ) ].kv;
734 return(
reference( kv.first , kv.second ) );
752 f_m &= ( f_m - 1 ) & fulls( *f_g );
753 if( ! f_m ) [[unlikely]]
777 return( f_b + lowest( f_m ) == o.f_b + lowest( o.f_m ) );
786 : f_g( g ) , f_b( b ) , f_m( m ) { }
789 void forward(
void );
791 const Group * f_g =
nullptr;
792 const Slot * f_b =
nullptr;
800 using Map = boost::unordered_flat_map<
801 K , V , H , Eq , detail::RebindAlloc< A , std::pair< const K , V > > >;
805 [[nodiscard]] std::size_t fullLoad(
void )
const;
832 return(
reference( f_i->first , f_i->second ) );
850 return( f_i == o.f_i );
857 explicit const_iterator(
typename Map::const_iterator i ) : f_i( i ) { }
859 typename Map::const_iterator f_i;
876#include "ADS/dictionary/impl/FlatHashTable.tpp"
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