119#ifndef __ADS_PointerTable
120 #define __ADS_PointerTable
140#include <type_traits>
145#if defined( ADS_WITH_BOOST ) && ! defined( ADS_POINTERTABLE_BOOST )
146 #define ADS_POINTERTABLE_BOOST 1
148#ifndef ADS_POINTERTABLE_BOOST
149 #define ADS_POINTERTABLE_BOOST 0
151#if ADS_POINTERTABLE_BOOST
154 #include <boost/unordered/unordered_flat_set.hpp>
168 template<
class P ,
class K >
170 return( p->state == key );
190template<
class L ,
class P = NoObservers >
208 requires requires {
typename P::places; }
211 using type =
typename P::places;
217inline constexpr bool kZeroedAllocator =
requires {
typename A::is_zeroed; };
222struct NoInitAllocator : B
226 NoInitAllocator(
const B & b ) noexcept : B( b ) { }
231 using other = NoInitAllocator<
232 typename std::allocator_traits< B >::template rebind_alloc< U > >;
236 void construct( U * )
noexcept { }
238 template<
class U ,
class... Args >
239 void construct( U * p , Args &&... a ) {
240 std::allocator_traits< B >::construct(
static_cast< B &
>( *
this ) , p ,
241 std::forward< Args >( a )... );
248template<
class A ,
class P >
249concept BoostPointerPlaces =
251 std::is_same_v< typename PlacesOf< P >::type , WidePlaces >;
262 class A = std::allocator< std::byte > ,
class Policy =
NoObservers >
266 using L =
typename detail::PlacesOf< Policy >::type;
269 static constexpr bool kIndex = ! std::is_same_v< L , WidePlaces >;
285 template<
class... O >
288 const A & alloc = A() , O &... o )
291 f_eq( std::move( eq ) ) , f_sub( o... ) {
292 f_mask = f_S.size() - 1;
293 f_bits = std::countr_zero( f_S.size() );
297 template<
class... O >
303 f_eq( std::move( eq ) ) , f_L( places ) , f_sub( o... ) {
304 ADS_PRE( f_L.store !=
nullptr );
305 f_mask = f_S.size() - 1;
306 f_bits = std::countr_zero( f_S.size() );
332 [[nodiscard]]
static constexpr std::size_t
slotsFor( std::size_t n ) {
334 while( n * 10 > m * 7 )
362 template<
class K ,
class Make >
369 [[nodiscard]] T *
find(
const K & key , std::uint64_t hash )
const;
375 [[nodiscard]] std::size_t
size(
void )
const;
381 [[nodiscard]] std::size_t
bytes(
void )
const;
398 requires( Subject::dynamic )
400 return( f_sub.attach( o ) );
406 requires( Subject::dynamic )
421 static std::size_t
size(
const auto & o ) {
return( o.f_S.size() ); }
423 static bool occupied(
const auto & o , std::size_t i ) {
424 return( o.full( o.f_S[ i ] ) );
427 static auto read(
const auto & o , std::size_t i ) {
428 return( o.object( o.f_S[ i ] ) );
459 template<
class K ,
class Make >
460 std::pair< T * , bool > findOrInsertPlain(
const K & key ,
465 template<
class K ,
class Make >
466 std::pair< T * , bool > insertTold(
const K & key , std::uint64_t hash ,
470 template<
class K ,
class Make >
472 insertCold(
const K & key , std::uint64_t hash , Make & make ) {
473 return( insertTold( key , hash , make ) );
477 void tellClearedAudited(
void );
480 void tellCleared(
void )
noexcept;
481 void tellRebuilt(
void )
noexcept;
487 std::uint64_t hash = 0;
493 using Slot = std::conditional_t< kIndex , std::uint64_t , WideSlot >;
497 using SA = std::conditional_t<
498 detail::kZeroedAllocator< A > ,
499 detail::NoInitAllocator<
500 typename std::allocator_traits< A >::template rebind_alloc< Slot > > ,
501 typename std::allocator_traits< A >::template rebind_alloc< Slot > >;
505 [[nodiscard]]
static std::uint64_t bits( std::uint64_t hash ) {
506 if constexpr( kIndex )
507 return( hash >> 32 );
513 [[nodiscard]]
static std::uint64_t bitsOf(
const Slot & s ) {
514 if constexpr( kIndex )
521 [[nodiscard]]
static bool full(
const Slot & s ) {
522 if constexpr( kIndex )
525 return( s.p !=
nullptr );
529 [[nodiscard]] T * object(
const Slot & s )
const {
530 if constexpr( kIndex )
531 return( f_L.store->at( std::size_t( std::uint32_t( s ) ) - 1 ) );
537 [[nodiscard]] Slot slot( std::uint64_t hash , T * p )
const;
540 [[nodiscard]] std::size_t place( std::uint64_t b )
const;
543 void reset( std::size_t m );
546 void move(
const std::vector< Slot , SA > & old );
554 [[nodiscard]]
bool reached(
const K & key , std::uint64_t hash )
const;
556 std::vector< Slot , SA > f_S;
557 std::size_t f_mask = 0;
559 std::size_t f_size = 0;
570#if ADS_POINTERTABLE_BOOST
574template<
class T ,
class Eq ,
class A ,
class Policy >
575 requires detail::BoostPointerPlaces< A , Policy >
583 using Inserted = mod::Inserted< T * , std::uint64_t >;
586 using modifications = mod::list< Inserted , mod::Cleared , mod::Rebuilt >;
593 template<
class... O >
595 const A & alloc = A() , O &... o );
617 [[nodiscard]]
static constexpr std::size_t
slotsFor( std::size_t n ) {
619 while( n * 10 > m * 7 )
627 template<
class K ,
class Make >
628 std::pair< T * , bool >
find_or_insert(
const K & key , std::uint64_t hash ,
633 [[nodiscard]] T *
find(
const K & key , std::uint64_t hash )
const;
636 void prefetch( std::uint64_t hash )
const;
639 [[nodiscard]] std::size_t
size(
void )
const;
642 [[nodiscard]] std::size_t
capacity(
void )
const;
645 [[nodiscard]] std::size_t
bytes(
void )
const;
656 [[nodiscard]]
const Subject &
subject(
void )
const {
return( f_sub ); }
662 requires( Subject::dynamic )
664 return( f_sub.attach( o ) );
670 requires( Subject::dynamic )
704 template<
class K ,
class Make >
705 std::pair< T * , bool > findOrInsertPlain(
const K & key ,
710 template<
class K ,
class Make >
711 std::pair< T * , bool > insertTold(
const K & key , std::uint64_t hash ,
715 template<
class K ,
class Make >
717 insertCold(
const K & key , std::uint64_t hash , Make & make ) {
718 return( insertTold( key , hash , make ) );
722 void tellClearedAudited(
void );
725 void tellCleared(
void )
noexcept;
726 void tellRebuilt(
void )
noexcept;
731 std::uint64_t hash = 0;
737 template<
class K ,
class Make >
750 template<
class K ,
class Make >
751 explicit Entry(
const Probe< K , Make > & q );
757 using is_transparent = void;
759 std::size_t operator()(
const Entry & e )
const {
760 return( std::size_t( e.hash ) );
763 template<
class K ,
class Make >
764 std::size_t operator()(
const Probe< K , Make > & q )
const {
765 return( std::size_t( q.hash ) );
773 using is_transparent = void;
775 explicit Equal( Eq e ) : eq( std::move( e ) ) { }
778 bool operator()(
const Entry & a ,
const Entry & b )
const {
779 return( a.p == b.p );
782 template<
class K ,
class Make >
783 bool operator()(
const Probe< K , Make > & q ,
const Entry & e )
const {
784 return( ( e.hash == q.hash ) && eq( e.p , q.key ) );
787 template<
class K ,
class Make >
788 bool operator()(
const Entry & e ,
const Probe< K , Make > & q )
const {
789 return( ( *
this )( q , e ) );
794 typename std::allocator_traits< A >::template rebind_alloc< Entry >;
796 using Set = boost::unordered_flat_set< Entry , Hash , Equal , SA >;
808template<
class T ,
class Eq ,
class A ,
class Policy >
809 requires detail::BoostPointerPlaces< A , Policy >
810class PointerTable< T , Eq , A , Policy >::const_iterator
817 using iterator_concept = std::forward_iterator_tag;
819 using iterator_category = std::input_iterator_tag;
825 reference operator*(
void )
const {
return( f_i->p ); }
841 bool operator==(
const const_iterator & o )
const {
return( f_i == o.f_i ); }
847 explicit const_iterator(
typename Set::const_iterator i ) : f_i( i ) { }
849 typename Set::const_iterator f_i;
861#include "ADS/dictionary/impl/PointerTable.tpp"
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...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
#define ADS_POINTERTABLE_BOOST
Definition PointerTable.h:149
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 ...
Hashing with random tables, beyond the book:
a hash table of pointers to objects found by their keys, on the places of ADS (the inspection above),...
Definition PointerTable.h:264
std::size_t size_type
the type of the sizes
Definition PointerTable.h:435
void prefetch(std::uint64_t hash) const
brings in the cache the first place of hash
std::size_t size(void) const
the number of objects
static constexpr std::size_t slotsFor(std::size_t n)
the places that objects need with no growth: the least power of 2, at least 16, above ,...
Definition PointerTable.h:332
mod::list< Inserted, mod::Cleared, mod::Rebuilt > modifications
the events: an object inserted, the table emptied or rebuilt
Definition PointerTable.h:278
void clear(void)
no object, the places kept; the objects are not touched
const_iterator cbegin(void) const
the constant iterator on the first item
Definition PointerTable.h:452
PointerTable(PointerTable &&o) noexcept
the table of o, which is left empty, with no places until an insertion
const_iterator iterator
the iterator on the items
Definition PointerTable.h:445
T * find(const K &key, std::uint64_t hash) const
the object of the key, nil if there is none: the inspection of find_or_insert() up to the first empty...
PointerTable & operator=(const PointerTable &)=default
a copy of the places, the objects shared
typename Policy::template bind< modifications, PointerTable > Subject
the subject
Definition PointerTable.h:280
PointerTable & operator=(PointerTable &&o) noexcept
the table of o, which is left empty, as the constructor above
void forEach(F &&f) const
f( p ) on each object, in the order of the places
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition PointerTable.h:437
void detach(int id)
detaches the observer number
Definition PointerTable.h:405
PointerTable(std::size_t capacity=16, Eq eq=Eq(), const A &alloc=A(), O &... o)
a table of at least capacity places, a power of 2, at least 16, the static observers o....
Definition PointerTable.h:287
PointerTable(std::size_t capacity, Eq eq, const A &alloc, L places, O &... o)
the same on the store of places, with IndexPlaces
Definition PointerTable.h:299
const_iterator begin(void) const
the iterator on the first item
T * reference
a reference to an item
Definition PointerTable.h:439
T * value_type
the type of the items, for the standard library
Definition PointerTable.h:433
const Subject & subject(void) const
the subject
Definition PointerTable.h:392
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition PointerTable.h:390
T * const_reference
a constant reference to an item
Definition PointerTable.h:441
void reserve(std::size_t n)
room for objects with no growth, i.e., at least slotsFor( n ) places
std::size_t capacity(void) const
the number of places
mod::Inserted< T *, std::uint64_t > Inserted
an insertion of an object, its pointer the handle and its hash the key
Definition PointerTable.h:275
std::pair< T *, bool > find_or_insert(const K &key, std::uint64_t hash, Make &&make)
the object of the key, whose hash is hash, and false; if there is none, the object that make() return...
const_iterator end(void) const
the iterator past the last item
PointerTable(const PointerTable &)=default
a copy of the places, the objects shared
std::size_t bytes(void) const
the memory of the places, in bytes
const_iterator cend(void) const
the constant iterator past the last item
Definition PointerTable.h:454
SlotIterator< PointerTable, Places > const_iterator
the iterator on the items, constant
Definition PointerTable.h:443
int attach(O &o)
attaches the observer and gives its number
Definition PointerTable.h:397
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
constexpr detail::Prefetch prefetch
a hint that is looked up next: s.prefetch( k ), else prefetch( s , k ) found by ADL,...
Definition Capabilities.h:451
constexpr detail::Reserve reserve
room for items in : s.reserve( n ), else reserve( s , n ) found by ADL, else nothing (the builders)
Definition Capabilities.h:444
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#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
the places of 8 bytes of PointerTable: the high 32 bits of the hash and the index plus 1 of the objec...
Definition PointerTable.h:184
const S * store
the store of the objects
Definition PointerTable.h:185
nobody listens: the subject is empty and every test is false at compile time, so that the structure c...
Definition Modification.h:1958
the places of the table, for SlotIterator
Definition PointerTable.h:419
static bool occupied(const auto &o, std::size_t i)
whether the place holds an object
Definition PointerTable.h:423
static std::size_t size(const auto &o)
the number of places
Definition PointerTable.h:421
static auto read(const auto &o, std::size_t i)
the pointer of the place
Definition PointerTable.h:427
the default equality of PointerTable: p->state == key
Definition PointerTable.h:166
bool operator()(const P *p, const K &key) const
whether the state of the object is key
Definition PointerTable.h:169
the places of 16 bytes of PointerTable, the default: the whole hash and the pointer to the object
Definition PointerTable.h:177
the subject policy P [see Modification.h] of a PointerTable on the places L, WidePlaces or IndexPlace...
Definition PointerTable.h:192
L places
the places
Definition PointerTable.h:193
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