ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Capabilities.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File Capabilities.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_Capabilities
76 #define __ADS_Capabilities
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#include "ADS/memory/Prefetch.h"
85
86#include <algorithm>
87#include <concepts>
88#include <cstddef>
89#include <cstdint>
90#include <ranges>
91#include <span>
92#include <type_traits>
93#include <utility>
94#include <vector>
95
96/*--------------------------------------------------------------------------*/
97/*--------------------------- NAMESPACE ------------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100namespace ADS
101{
102
103namespace mod
104{
105
107template< class... E >
108struct list;
109
110} // end( namespace mod )
111
112/*--------------------------------------------------------------------------*/
113/*--------------------------- THE ASSOCIATED TYPES -------------------------*/
114/*--------------------------------------------------------------------------*/
115
117namespace detail
118{
119
120template< class S >
121constexpr auto key_of( void )
122{
123 if constexpr( requires { typename S::Key; } )
124 return( std::type_identity< typename S::Key >() );
125 else if constexpr( requires { typename S::key_type; } )
126 return( std::type_identity< typename S::key_type >() );
127 else if constexpr( requires { typename S::value_type; } )
128 return( std::type_identity< typename S::value_type >() );
129 else
130 return( std::type_identity< std::size_t >() );
131 }
132
133template< class S >
134struct key_type_of
135{
136 using type = typename decltype( key_of< S >() )::type;
137 };
138
139template< class S >
140struct modifications_of
141{
142 using type = mod::list<>;
143 };
144
145template< class S >
146 requires requires { typename S::modifications; }
147struct modifications_of< S >
148{
149 using type = typename S::modifications;
150 };
151
152template< class Q >
153struct priority_of
154{
155 using type = queue_item_t< Q >;
156 };
157
158template< class Q >
159 requires(
160 requires { typename Q::Key; } && ! requires { typename Q::Priority; } )
161struct priority_of< Q >
162{
163 using type = typename Q::Key;
164 };
165
166template< class Q >
167 requires requires { typename Q::Priority; }
168struct priority_of< Q >
169{
170 using type = typename Q::Priority;
171 };
172
173} // end( namespace detail )
175
178template< class S >
179using key_type_t = typename detail::key_type_of< S >::type;
180
184template< class S >
185using modifications_of = typename detail::modifications_of< S >::type;
186
189template< class Q >
190using priority_t = typename detail::priority_of< Q >::type;
191
192/*--------------------------------------------------------------------------*/
193/*--------------------------- THE SEMANTIC OPT-INS -------------------------*/
194/*--------------------------------------------------------------------------*/
195
202template< class S >
203inline constexpr bool enable_stable_references = false;
204
215template< class T >
216inline constexpr bool nonmonotone_values = false;
217
223template< class S >
224constexpr unsigned monotone_of( void )
225{
226 if constexpr( requires { S::monotone; } )
227 return( unsigned( S::monotone ) );
228 else
229 return( ~0u );
230 }
231
233template< class S >
234concept declares_monotone = requires {
235 { S::monotone } -> std::convertible_to< unsigned >;
236 };
237
238/*--------------------------------------------------------------------------*/
239/*------------------------------ THE QUERIES -------------------------------*/
240/*--------------------------------------------------------------------------*/
241
245{ };
246
248template< class T >
250 std::derived_from< T , forwarding_query > && std::is_empty_v< T >;
251
256{
258 template< class S >
259 static constexpr bool fallback( void ) {
261 }
262
263 static constexpr bool meet( bool a , bool b ) { return( a && b ); }
264 };
265
270{
272 template< class S >
273 static constexpr bool fallback( void ) {
274 return( false );
275 }
276
277 static constexpr bool meet( bool a , bool b ) { return( a && b ); }
278 };
279
282
287{
289 template< class S >
290 static constexpr complexity fallback( void ) {
291 return( complexity::linear );
292 }
293
294 static constexpr complexity meet( complexity a , complexity b ) {
295 return( std::max( a , b ) );
296 }
297 };
298
303{
305 template< class S >
306 static constexpr auto fallback( void ) {
307 return( std::type_identity< modifications_of< S > >() );
308 }
309 };
310
315inline constexpr event_set_t event_set{};
316
318namespace detail
319{
320
321template< class S , class T >
322constexpr auto query_of( void )
323{
324 if constexpr( requires { S::query( T() ); } )
325 return( S::query( T() ) );
326 else
327 return( T::template fallback< S >() );
328 }
329
330} // end( namespace detail )
332
335template< class S , forwarding_query_tag T >
336inline constexpr auto query_v =
337 detail::query_of< std::remove_cvref_t< S > , T >();
338
340template< class T >
342 T::meet( T::template fallback< int >() , T::template fallback< int >() );
343 };
344
346template< class A , class B , meetable_query T >
347inline constexpr auto query_meet_v =
349
351namespace detail
352{
353
354struct Query
355{
356 template< class S , forwarding_query_tag T >
357 constexpr auto operator()( const S & , T ) const noexcept {
358 return( query_v< S , T > );
359 }
360 };
361
362} // end( namespace detail )
364
367inline constexpr detail::Query query{};
368
369/*--------------------------------------------------------------------------*/
370/*------------------------ THE CUSTOMIZATION POINTS ------------------------*/
371/*--------------------------------------------------------------------------*/
372
374namespace detail
375{
376
377void reserve( void ) = delete; // the free ones, only by ADL
378void prefetch( void ) = delete;
379void insert_bulk( void ) = delete;
380
381struct Reserve
382{
383 template< class S >
384 constexpr void operator()( S & s , std::size_t n ) const {
385 if constexpr( requires { s.reserve( n ); } )
386 s.reserve( n );
387 else if constexpr( requires { reserve( s , n ); } )
388 reserve( s , n );
389 }
390 };
391
392struct Prefetch
393{
395 void operator()( const void * p ) const noexcept { prefetchLine( p ); }
397 void operator()( const void * p , Locality l ) const noexcept {
398 prefetchLine( p , l );
399 }
400 template< class S , class K >
401 requires( ! std::is_same_v< K , Locality > )
402 constexpr void operator()( const S & s , const K & k ) const {
403 if constexpr( requires { s.prefetch( k ); } )
404 s.prefetch( k );
405 else if constexpr( requires { prefetch( s , k ); } )
406 prefetch( s , k );
407 }
408 };
409
410template< class S , class R >
411concept member_bulk =
412 requires( S & s , R && r )
413{ s.insert_bulk( std::forward< R >( r ) ); };
414template< class S , class R >
415concept free_bulk =
416 requires( S & s , R && r )
417{ insert_bulk( s , std::forward< R >( r ) ); };
418template< class S , class R >
419concept one_by_one =
420 std::ranges::input_range< R > &&
421 requires( S & s , std::ranges::range_reference_t< R > x )
422{ s.insert( x ); };
423
424struct InsertBulk
425{
426 template< class S , class R >
427 requires( member_bulk< S , R > || free_bulk< S , R > || one_by_one< S , R > )
428 constexpr void operator()( S & s , R && r ) const {
429 if constexpr( member_bulk< S , R > )
430 s.insert_bulk( std::forward< R >( r ) );
431 else if constexpr( free_bulk< S , R > )
432 insert_bulk( s , std::forward< R >( r ) );
433 else
434 for( auto && x : r )
435 s.insert( x );
436 }
437 };
438
439} // end( namespace detail )
441
444inline constexpr detail::Reserve reserve{};
445
451inline constexpr detail::Prefetch prefetch{};
452
456inline constexpr detail::InsertBulk insert_bulk{};
457
458/*--------------------------------------------------------------------------*/
459/*------------------------ THE CAPABILITIES OF SETS ------------------------*/
460/*--------------------------------------------------------------------------*/
461
464template< class S , class R = std::span< const key_type_t< S > > >
465concept bulk_insertable = requires( S & s , R r )
466{ s.insert_bulk( r ); };
467
470template< class S , class R = std::span< const key_type_t< S > > >
471concept bulk_erasable = requires( S & s , R r )
472{ s.erase_bulk( r ); };
473
477template< class S >
478concept splittable =
479 requires( S && s , const key_type_t< S > & k )
480{ std::move( s ).split( k ); };
481
486template< class S >
487concept joinable = requires( S && l , S && r )
488{
489 { S::join2( std::move( l ) , std::move( r ) ) } -> std::same_as< S >;
490 };
491
494template< class S , class K = key_type_t< S > >
495concept rankable = requires( const S & s , const K & k )
496{
497 { s.rank( k ) } -> std::convertible_to< std::size_t >;
498 };
499
503template< class S , class G = void >
504concept selectable =
505 ( std::is_void_v< G > &&
506 requires( const S & s , std::size_t i )
507{ s.select( i ); } ) ||
508 ( ! std::is_void_v< G > &&
509 requires( const S & s , bool ( *p )( const typename G::Value & ) )
510{
511 s.augSelect( p );
512 } );
513
516template< class S , class K = key_type_t< S > >
518 requires( const S & s , const K & k )
519{ s.prefetch( k ); };
520
522template< class S >
523concept reservable = requires( S & s , std::size_t n )
524{ s.reserve( n ); };
525
529template< class S , class G >
530concept augmented_by = std::same_as< typename S::Augmented , G > &&
531 requires( const S & s , const key_type_t< S > & k )
532{
533 {
534 s.augRange( k , k )
535 } -> std::convertible_to< typename G::Value >;
536 };
537
541template< class S , class A >
542concept range_actionable = requires(
543 S & s , const key_type_t< S > & k , const A & a )
544{ s.applyRange( k , k , a ); };
545
546/*--------------------------------------------------------------------------*/
547/*----------------------- THE CAPABILITIES OF QUEUES -----------------------*/
548/*--------------------------------------------------------------------------*/
549
554
577template< class Q >
579{
580 if constexpr( requires { Q::decrease_key; } )
581 return( Q::decrease_key ); // the opt-in wins
582 else if constexpr( requires( Q & q , queue_handle_t< Q > h ,
583 const priority_t< Q > & p ) {
584 ADS::queue_decrease( q , h , p );
585 } )
587 else if constexpr( PriorityQueue< Q > )
588 return( decrease_key_kind::lazy );
589 else
590 return( decrease_key_kind::none );
591 }
592
596template< class Q >
597concept addressable =
599 requires( Q & q , typename Q::Handle h , const typename Q::Priority & p )
600{
601 q.increase( h , p );
602 q.erase( h );
603 };
604
608template< class Q >
609concept observes_values = requires( Q & q , int id )
610{ q.update( id ); };
611
616template< class Q >
617concept batch_extract = requires( Q & q , const priority_t< Q > & theta ,
618 std::vector< queue_item_t< Q > > & out )
619{
620 q.extract( theta , out );
621 };
622
625template< class Q >
627 requires( Q & q , const priority_t< Q > & d )
628{ q.shift_all( d ); };
629
633template< class Q >
636 requires( Q & q , const priority_t< Q > & k )
637{ q.rebase( k ); };
638
640namespace detail
641{
642
643template< class Q >
644struct same_key
645{
646 const priority_t< Q > & operator()( const queue_item_t< Q > & ) const;
647 };
648
649} // end( namespace detail )
651
655template< class Q , class F = detail::same_key< Q > >
656concept rekeyable = requires( Q & q , F f )
657{ q.rekey( f ); };
658
659/*--------------------------------------------------------------------------*/
660/*-------------------- THE CAPABILITIES OF NODE TABLES ---------------------*/
661/*--------------------------------------------------------------------------*/
662
665template< class T >
666concept erasable =
667 requires( T & t , const key_type_t< T > & k )
668{ t.erase( k ); };
669
673template< class T >
674concept epoch_clearable = requires( T & t , const T & ct )
675{
676 typename T::Clear::Stamp;
677 t.clear();
678 { ct.epoch() } -> std::convertible_to< std::uint64_t >;
679 };
680
681/*--------------------------------------------------------------------------*/
682/*------------------- THE CAPABILITIES OF UNDO AND STATE -------------------*/
683/*--------------------------------------------------------------------------*/
684
687template< class S >
688concept undoable = requires( S & s )
689{ s.rollback( s.checkpoint() ); };
690
693template< class S >
694concept snapshotable = requires( const S & s )
695{ s.snapshot(); };
696
697/*--------------------------------------------------------------------------*/
698/*----------------------- THE CAPABILITIES OF GRAPHS -----------------------*/
699/*--------------------------------------------------------------------------*/
700
702namespace detail
703{
704
705struct any_arc
706{
707 template< class... A >
708 void operator()( A &&... ) const;
709 };
710
711} // end( namespace detail )
713
716template< class G >
717concept in_arcs =
718 requires( const G & g , int v , detail::any_arc f )
719{ g.forEachIn( v , f ); };
720
723template< class G >
724concept stable_edge_ids = requires( const G & g , int u )
725{
726 { g.arcId( u , u ) } -> std::convertible_to< std::size_t >;
727 };
728
731template< class G >
732concept batch_updatable = requires( G & g )
733{ g.batch(); };
734
735} // end( namespace ADS )
736
737/*--------------------------------------------------------------------------*/
738
739#endif /* Capabilities.h included */
740
741/*--------------------------------------------------------------------------*/
742/*------------------------ End File Capabilities.h -------------------------*/
743/*--------------------------------------------------------------------------*/
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The PRIORITYQUEUE of section 10.1, with decreasing priority, in two concepts.
the PRIORITYQUEUE of section 10.1 with handles and decreasing priority: the types Item,...
Definition PriorityQueue.h:115
the PRIORITYQUEUE of section 10.1 without handles: the type of the items (Item or value_type),...
Definition PriorityQueue.h:102
an AddressablePriorityQueue with increase( h , p ) and erase( h ) on its handles as well (the open li...
Definition Capabilities.h:597
augRange( lo , hi ), the values of the augmentation G of the keys in combined, kept by the structure...
Definition Capabilities.h:530
extract( theta , out ), every item of priority at most moved to out at once (the stepping algorithms...
Definition Capabilities.h:617
batch(), a group of changes told as one (the observers of a batch of changes)
Definition Capabilities.h:732
erase_bulk( r ) of the range of keys as one operation (the event BulkErased)
Definition Capabilities.h:471
insert_bulk( r ) of the range of items as one operation (the generic insertion of a range,...
Definition Capabilities.h:465
whether S declares the only direction in which its values move
Definition Capabilities.h:234
clear() in by epochs: a policy of clear(), T::Clear, that stamps the places, and epoch() (the node t...
Definition Capabilities.h:674
erase( k ) of a key (the node tables that reuse their places within an episode)
Definition Capabilities.h:666
a tag of a forwarded query
Definition Capabilities.h:249
forEachIn( v , f ), the arcs entering (the observers that repair a tree from the heads of the arcs t...
Definition Capabilities.h:717
S::join2( l , r ), the tree of the keys of and of when those of are smaller (the union of Blelloch...
Definition Capabilities.h:487
whether the query T composes two answers
Definition Capabilities.h:341
a queue whose extractions never decrease, with rebase(), which lets the next episode start below the ...
Definition Capabilities.h:634
update( id ), the key of the item read again from the table the queue observes (the queue of the ope...
Definition Capabilities.h:609
prefetch( k ), a hint that (a key or its hash) is looked up next (a batch of lookups by group prefet...
Definition Capabilities.h:517
applyRange( lo , hi , a ), the action applied to every value of the keys in in by a lazy tag (the ...
Definition Capabilities.h:542
rank( k ), the number of keys smaller than (the order statistics); its time is the query rank_comple...
Definition Capabilities.h:495
rekey( f ), the key of each item recomputed by and the order rebuilt in (the change of the key func...
Definition Capabilities.h:656
reserve( n ), room for items (the builders)
Definition Capabilities.h:523
select( i ), the key of rank (the order statistics); with an augmentation G, augSelect( p ),...
Definition Capabilities.h:504
shift_all( delta ), every key moved by in (D* Lite, Generalized Adaptive A*)
Definition Capabilities.h:626
snapshot(), a view of the present state in which later changes do not touch (the audits,...
Definition Capabilities.h:694
std::move( s ).split( k ), the keys before , its value and the keys after it (the set operations of B...
Definition Capabilities.h:478
arcId( u , v ), an id of the arc that does not change while the arc is in the graph (the observers th...
Definition Capabilities.h:724
checkpoint() and rollback( t ) back to a checkpoint (a speculative change, a batch that exits by an e...
Definition Capabilities.h:688
constexpr detail::InsertBulk insert_bulk
the items of the range inserted in : s.insert_bulk( r ), else insert_bulk( s , r ) found by ADL,...
Definition Capabilities.h:456
typename detail::modifications_of< S >::type modifications_of
the events of S [see Modification.h], S::modifications if it declares them, the empty list otherwise:...
Definition Capabilities.h:185
constexpr decrease_key_kind decrease_key_of(void)
how the queue Q lowers a priority: Q::decrease_key if it declares it (e.g., a queue whose decrease() ...
Definition Capabilities.h:578
decrease_key_kind
how a priority queue lowers the priority of an item: it cannot (none), by a new insertion and the sta...
Definition Capabilities.h:553
constexpr unsigned monotone_of(void)
the directions in which the values of S may move, as the bits up and down of mod::effect: S::monotone...
Definition Capabilities.h:224
typename detail::key_type_of< S >::type key_type_t
the keys of S, which its lookups take: S::Key, else S::key_type, else S::value_type,...
Definition Capabilities.h:179
typename detail::priority_of< Q >::type priority_t
the priorities of the queue Q: Q::Priority, else Q::Key (the first key of a queue of two,...
Definition Capabilities.h:190
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
complexity
a class of time, from the cheapest
Definition Capabilities.h:281
constexpr detail::Query query
the answer of to the query of the tag , query_v< S , T >
Definition Capabilities.h:367
constexpr bool enable_stable_references
whether the items of S keep their addresses while they are in it, so that a handle may be a pointer a...
Definition Capabilities.h:203
constexpr bool nonmonotone_values
whether the values of the nodes of the table T may grow as well as fall, so that its concurrent field...
Definition Capabilities.h:216
constexpr auto query_meet_v
the answer of the composition of A and B to the query T
Definition Capabilities.h:347
constexpr auto query_v
the answer of S to the query T, at compile time: the static S::query( T() ) if it has one,...
Definition Capabilities.h:336
constexpr stable_references_t stable_references
the tags of the queries
Definition Capabilities.h:312
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
@ lazy
Definition Capabilities.h:553
@ none
Definition Capabilities.h:553
@ native
Definition Capabilities.h:553
@ constant
Definition Capabilities.h:281
@ logarithmic
Definition Capabilities.h:281
@ linear
Definition Capabilities.h:281
Growth linear(double a, double beta)
the order of ,
GrB_Type type(void)
the type of GraphBLAS of the numbers T: GrB_BOOL, the integers of 8 to 64 bits with or without sign,...
Definition GraphBLAS.h:128
constexpr detail::QueueDecrease queue_decrease
the priority of the item of the handle of lowered to , i.e., the item moved toward the minimum: q....
Definition QueueCustomization.h:295
typename detail::queue_handle< Q >::type queue_handle_t
the handles of the queue Q, which its insertions return: Q::Handle, else Q::handle_type (the mutable ...
Definition QueueCustomization.h:250
typename detail::queue_item< Q >::type queue_item_t
the items of the queue Q: Q::Item, else Q::value_type
Definition QueueCustomization.h:93
Definition Capabilities.h:104
Definition ADT.h:96
constexpr rank_complexity_t rank_complexity
Definition Capabilities.h:314
constexpr event_set_t event_set
Definition Capabilities.h:315
constexpr thread_safe_reads_t thread_safe_reads
Definition Capabilities.h:313
the query of the events of a structure, as std::type_identity of a mod::list; by default modification...
Definition Capabilities.h:303
static constexpr auto fallback(void)
the answer of a structure that does not give one
Definition Capabilities.h:306
the base of the tags of the queries that a wrapper forwards to what it wraps
Definition Capabilities.h:245
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482
the query of the time of rank(), so that an rank is not taken for an one; linear by default,...
Definition Capabilities.h:287
static constexpr complexity fallback(void)
the answer of a structure that does not give one
Definition Capabilities.h:290
static constexpr complexity meet(complexity a, complexity b)
the answer of the composition of two structures
Definition Capabilities.h:294
the query of whether the items keep their addresses; by default enable_stable_references< S >,...
Definition Capabilities.h:256
static constexpr bool meet(bool a, bool b)
the answer of the composition of two structures
Definition Capabilities.h:263
static constexpr bool fallback(void)
the answer of a structure that does not give one
Definition Capabilities.h:259
the query of whether reads may run with other reads and with writes from other threads; false by defa...
Definition Capabilities.h:270
static constexpr bool meet(bool a, bool b)
the answer of the composition of two structures
Definition Capabilities.h:277
static constexpr bool fallback(void)
the answer of a structure that does not give one
Definition Capabilities.h:273