ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PointerTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File PointerTable.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
115/*--------------------------------------------------------------------------*/
116/*----------------------------- DEFINITIONS --------------------------------*/
117/*--------------------------------------------------------------------------*/
118
119#ifndef __ADS_PointerTable
120 #define __ADS_PointerTable
121/* self-identification: #endif constant */
122
123/*--------------------------------------------------------------------------*/
124/*------------------------------ INCLUDES ----------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127#include "ADS/Contract.h"
128#include "ADS/Iterators.h"
129#include "ADS/Modification.h"
130#include "ADS/Portable.h"
132#include "ADS/memory/Prefetch.h"
133
134#include <algorithm>
135#include <bit>
136#include <cstddef>
137#include <cstdint>
138#include <memory>
139#include <stdexcept>
140#include <type_traits>
141#include <utility>
142#include <vector>
143
144// the places of boost::unordered_flat_set, with ADS_WITH_BOOST
145#if defined( ADS_WITH_BOOST ) && ! defined( ADS_POINTERTABLE_BOOST )
146 #define ADS_POINTERTABLE_BOOST 1
147#endif
148#ifndef ADS_POINTERTABLE_BOOST
149 #define ADS_POINTERTABLE_BOOST 0
150#endif
151#if ADS_POINTERTABLE_BOOST
152 #include <iterator>
153
154 #include <boost/unordered/unordered_flat_set.hpp>
155#endif
156
157/*--------------------------------------------------------------------------*/
158/*--------------------------- NAMESPACE ------------------------------------*/
159/*--------------------------------------------------------------------------*/
160
161namespace ADS
162{
163
166{
168 template< class P , class K >
169 bool operator()( const P * p , const K & key ) const {
170 return( p->state == key );
171 }
172 };
173
177{ };
178
182template< class S >
184{
185 const S * store = nullptr;
186 };
187
190template< class L , class P = NoObservers >
191struct WithPlaces : P
192{
193 using places = L;
194 };
195
196namespace detail
197{
198
201template< class P >
202struct PlacesOf
203{
204 using type = WidePlaces;
205 };
206
207template< class P >
208 requires requires { typename P::places; }
209struct PlacesOf< P >
210{
211 using type = typename P::places;
212 };
213
216template< class A >
217inline constexpr bool kZeroedAllocator = requires { typename A::is_zeroed; };
218
221template< class B >
222struct NoInitAllocator : B
223{
224 using B::B;
226 NoInitAllocator( const B & b ) noexcept : B( b ) { }
228 template< class U >
229 struct rebind
230 {
231 using other = NoInitAllocator<
232 typename std::allocator_traits< B >::template rebind_alloc< U > >;
233 };
235 template< class 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 )... );
242 }
243 };
244
248template< class A , class P >
249concept BoostPointerPlaces =
250 ( ADS_POINTERTABLE_BOOST != 0 ) && ( ! kZeroedAllocator< A > ) &&
251 std::is_same_v< typename PlacesOf< P >::type , WidePlaces >;
252
253} // end( namespace detail )
254
255/*--------------------------------------------------------------------------*/
256/*-------------------------- CLASS PointerTable ----------------------------*/
257/*--------------------------------------------------------------------------*/
258
261template< class T , class Eq = StateEqual ,
262 class A = std::allocator< std::byte > , class Policy = NoObservers >
264{
266 using L = typename detail::PlacesOf< Policy >::type;
267
269 static constexpr bool kIndex = ! std::is_same_v< L , WidePlaces >;
270
271/*--------------------------------------------------------------------------*/
272 public:
276
280 using Subject = typename Policy::template bind< modifications ,
281 PointerTable >;
282
285 template< class... O >
286 requires( ! kIndex )
287 explicit PointerTable( std::size_t capacity = 16 , Eq eq = Eq() ,
288 const A & alloc = A() , O &... o )
289 : f_S( std::bit_ceil( capacity < 16 ? std::size_t( 16 ) : capacity ) ,
290 SA( alloc ) ) ,
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() );
294 }
295
297 template< class... O >
298 requires( kIndex )
299 PointerTable( std::size_t capacity , Eq eq , const A & alloc , L places ,
300 O &... o )
301 : f_S( std::bit_ceil( capacity < 16 ? std::size_t( 16 ) : capacity ) ,
302 SA( alloc ) ) ,
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() );
307 }
308
310 PointerTable( const PointerTable & ) = default;
311
313 PointerTable & operator=( const PointerTable & ) = default;
314
317 PointerTable( PointerTable && o ) noexcept;
318
321
323 void clear( void );
324
327 void reserve( std::size_t n );
328
332 [[nodiscard]] static constexpr std::size_t slotsFor( std::size_t n ) {
333 std::size_t m = 16;
334 while( n * 10 > m * 7 )
335 m *= 2;
336 return( m );
337 }
338
362 template< class K , class Make >
363 std::pair< T * , bool > find_or_insert( const K & key , std::uint64_t hash ,
364 Make && make );
365
368 template< class K >
369 [[nodiscard]] T * find( const K & key , std::uint64_t hash ) const;
370
372 void prefetch( std::uint64_t hash ) const;
373
375 [[nodiscard]] std::size_t size( void ) const;
376
378 [[nodiscard]] std::size_t capacity( void ) const;
379
381 [[nodiscard]] std::size_t bytes( void ) const;
382
384 template< class F >
385 void forEach( F && f ) const;
386
387/*--------------------------- THE OBSERVERS --------------------------------*/
388
390 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
392 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
393
396 template< class O >
397 int attach( O & o )
398 requires( Subject::dynamic )
399 {
400 return( f_sub.attach( o ) );
401 }
402
405 void detach( int id )
406 requires( Subject::dynamic )
407 {
408 f_sub.detach( id );
409 }
410
411/*------------------------- THE STANDARD INTERFACE -------------------------*/
412
413 // a view of the pointers to the objects, in the order of the places,
414 // bidirectional; the key of an object must not change through it, and an
415 // insertion that grows the table invalidates it
416
418 struct Places
419 {
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 ] ) );
425 }
426
427 static auto read( const auto & o , std::size_t i ) {
428 return( o.object( o.f_S[ i ] ) );
429 }
430 };
431
433 using value_type = T *;
435 using size_type = std::size_t;
437 using difference_type = std::ptrdiff_t;
439 using reference = T *;
441 using const_reference = T *;
446
448 [[nodiscard]] const_iterator begin( void ) const;
450 [[nodiscard]] const_iterator end( void ) const;
452 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
454 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
455
456/*--------------------------------------------------------------------------*/
457 private:
459 template< class K , class Make >
460 std::pair< T * , bool > findOrInsertPlain( const K & key ,
461 std::uint64_t hash ,
462 Make && make );
463
465 template< class K , class Make >
466 std::pair< T * , bool > insertTold( const K & key , std::uint64_t hash ,
467 Make & make );
468
470 template< class K , class Make >
471 ADS_COLD_PATH std::pair< T * , bool >
472 insertCold( const K & key , std::uint64_t hash , Make & make ) {
473 return( insertTold( key , hash , make ) );
474 }
475
477 void tellClearedAudited( void );
478
480 void tellCleared( void ) noexcept;
481 void tellRebuilt( void ) noexcept;
482
485 struct WideSlot
486 {
487 std::uint64_t hash = 0;
488 T * p = nullptr;
489 };
490
493 using Slot = std::conditional_t< kIndex , std::uint64_t , WideSlot >;
494
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 > >;
502
505 [[nodiscard]] static std::uint64_t bits( std::uint64_t hash ) {
506 if constexpr( kIndex )
507 return( hash >> 32 );
508 else
509 return( hash );
510 }
511
513 [[nodiscard]] static std::uint64_t bitsOf( const Slot & s ) {
514 if constexpr( kIndex )
515 return( s >> 32 );
516 else
517 return( s.hash );
518 }
519
521 [[nodiscard]] static bool full( const Slot & s ) {
522 if constexpr( kIndex )
523 return( s != 0 );
524 else
525 return( s.p != nullptr );
526 }
527
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 ) );
532 else
533 return( s.p );
534 }
535
537 [[nodiscard]] Slot slot( std::uint64_t hash , T * p ) const;
538
540 [[nodiscard]] std::size_t place( std::uint64_t b ) const;
541
543 void reset( std::size_t m );
544
546 void move( const std::vector< Slot , SA > & old );
547
549 void grow( void );
550
553 template< class K >
554 [[nodiscard]] bool reached( const K & key , std::uint64_t hash ) const;
555
556 std::vector< Slot , SA > f_S;
557 std::size_t f_mask = 0;
558 int f_bits = 0;
559 std::size_t f_size = 0;
560 Eq f_eq;
563
564 }; // end( class PointerTable )
565
566/*--------------------------------------------------------------------------*/
567/*---------------------- CLASS PointerTable OF BOOST -----------------------*/
568/*--------------------------------------------------------------------------*/
569
570#if ADS_POINTERTABLE_BOOST
574template< class T , class Eq , class A , class Policy >
575 requires detail::BoostPointerPlaces< A , Policy >
576class PointerTable< T , Eq , A , Policy >
577{
578
579/*--------------------------------------------------------------------------*/
580 public:
583 using Inserted = mod::Inserted< T * , std::uint64_t >;
584
586 using modifications = mod::list< Inserted , mod::Cleared , mod::Rebuilt >;
588 using Subject = typename Policy::template bind< modifications ,
589 PointerTable >;
590
593 template< class... O >
594 explicit PointerTable( std::size_t capacity = 16 , Eq eq = Eq() ,
595 const A & alloc = A() , O &... o );
596
598 PointerTable( const PointerTable & ) = default;
599
601 PointerTable & operator=( const PointerTable & ) = default;
602
604 PointerTable( PointerTable && o ) noexcept;
605
607 PointerTable & operator=( PointerTable && o ) noexcept;
608
610 void clear( void );
611
613 void reserve( std::size_t n );
614
617 [[nodiscard]] static constexpr std::size_t slotsFor( std::size_t n ) {
618 std::size_t m = 16;
619 while( n * 10 > m * 7 )
620 m *= 2;
621 return( m );
622 }
623
627 template< class K , class Make >
628 std::pair< T * , bool > find_or_insert( const K & key , std::uint64_t hash ,
629 Make && make );
630
632 template< class K >
633 [[nodiscard]] T * find( const K & key , std::uint64_t hash ) const;
634
636 void prefetch( std::uint64_t hash ) const;
637
639 [[nodiscard]] std::size_t size( void ) const;
640
642 [[nodiscard]] std::size_t capacity( void ) const;
643
645 [[nodiscard]] std::size_t bytes( void ) const;
646
648 template< class F >
649 void forEach( F && f ) const;
650
651/*--------------------------- THE OBSERVERS --------------------------------*/
652
654 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
656 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
657
660 template< class O >
661 int attach( O & o )
662 requires( Subject::dynamic )
663 {
664 return( f_sub.attach( o ) );
665 }
666
669 void detach( int id )
670 requires( Subject::dynamic )
671 {
672 f_sub.detach( id );
673 }
674
675/*------------------------- THE STANDARD INTERFACE -------------------------*/
676
678 using value_type = T *;
680 using size_type = std::size_t;
682 using difference_type = std::ptrdiff_t;
684 using reference = T *;
686 using const_reference = T *;
688 class const_iterator;
690 using iterator = const_iterator;
691
693 [[nodiscard]] const_iterator begin( void ) const;
695 [[nodiscard]] const_iterator end( void ) const;
697 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
699 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
700
701/*--------------------------------------------------------------------------*/
702 private:
704 template< class K , class Make >
705 std::pair< T * , bool > findOrInsertPlain( const K & key ,
706 std::uint64_t hash ,
707 Make && make );
708
710 template< class K , class Make >
711 std::pair< T * , bool > insertTold( const K & key , std::uint64_t hash ,
712 Make & make );
713
715 template< class K , class Make >
716 ADS_COLD_PATH std::pair< T * , bool >
717 insertCold( const K & key , std::uint64_t hash , Make & make ) {
718 return( insertTold( key , hash , make ) );
719 }
720
722 void tellClearedAudited( void );
723
725 void tellCleared( void ) noexcept;
726 void tellRebuilt( void ) noexcept;
727
729 struct Slot
730 {
731 std::uint64_t hash = 0;
732 T * p = nullptr;
733 };
734
737 template< class K , class Make >
738 struct Probe
739 {
740 const K & key;
741 std::uint64_t hash;
742 Make * make;
743 };
744
747 struct Entry : Slot
748 {
750 template< class K , class Make >
751 explicit Entry( const Probe< K , Make > & q );
752 };
753
755 struct Hash
756 {
757 using is_transparent = void;
759 std::size_t operator()( const Entry & e ) const {
760 return( std::size_t( e.hash ) );
761 }
763 template< class K , class Make >
764 std::size_t operator()( const Probe< K , Make > & q ) const {
765 return( std::size_t( q.hash ) );
766 }
767 };
768
771 struct Equal
772 {
773 using is_transparent = void;
775 explicit Equal( Eq e ) : eq( std::move( e ) ) { }
776 Eq eq;
778 bool operator()( const Entry & a , const Entry & b ) const {
779 return( a.p == b.p );
780 }
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 ) );
785 }
787 template< class K , class Make >
788 bool operator()( const Entry & e , const Probe< K , Make > & q ) const {
789 return( ( *this )( q , e ) );
790 }
791 };
792
793 using SA =
794 typename std::allocator_traits< A >::template rebind_alloc< Entry >;
796 using Set = boost::unordered_flat_set< Entry , Hash , Equal , SA >;
797
798 Set f_set;
800
801 }; // end( class PointerTable )
802
803/*--------------------------------------------------------------------------*/
804/*-------------------- CLASS PointerTable::const_iterator ------------------*/
805/*--------------------------------------------------------------------------*/
806
808template< class T , class Eq , class A , class Policy >
809 requires detail::BoostPointerPlaces< A , Policy >
810class PointerTable< T , Eq , A , Policy >::const_iterator
811{
812 public:
813 using value_type = T *;
814 using reference = T *;
815 using difference_type = std::ptrdiff_t;
817 using iterator_concept = std::forward_iterator_tag;
819 using iterator_category = std::input_iterator_tag;
820
822 const_iterator( void ) = default;
823
825 reference operator*( void ) const { return( f_i->p ); }
826
828 const_iterator & operator++( void ) {
829 ++f_i;
830 return( *this );
831 }
832
834 const_iterator operator++( int ) {
835 auto t = *this;
836 ++f_i;
837 return( t );
838 }
839
841 bool operator==( const const_iterator & o ) const { return( f_i == o.f_i ); }
842
843 private:
844 friend class PointerTable;
845
847 explicit const_iterator( typename Set::const_iterator i ) : f_i( i ) { }
848
849 typename Set::const_iterator f_i;
850 }; // end( class PointerTable::const_iterator )
851#endif
852
853/*--------------------------------------------------------------------------*/
854
855} // end( namespace ADS )
856
857/*--------------------------------------------------------------------------*/
858/*-------------------------- IMPLEMENTATION --------------------------------*/
859/*--------------------------------------------------------------------------*/
860
861#include "ADS/dictionary/impl/PointerTable.tpp"
862
863/*--------------------------------------------------------------------------*/
864
865#endif /* PointerTable.h included */
866
867/*--------------------------------------------------------------------------*/
868/*------------------------ End File PointerTable.h -------------------------*/
869/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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