ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Observable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File Observable.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
106/*--------------------------------------------------------------------------*/
107/*----------------------------- DEFINITIONS --------------------------------*/
108/*--------------------------------------------------------------------------*/
109
110#ifndef __ADS_Observable
111 #define __ADS_Observable
112/* self-identification: #endif constant */
113
114/*--------------------------------------------------------------------------*/
115/*------------------------------ INCLUDES ----------------------------------*/
116/*--------------------------------------------------------------------------*/
117
118#include "ADS/Capabilities.h"
119#include "ADS/Contract.h"
120#include "ADS/Modification.h"
121#include "ADS/Portable.h"
122#include "ADS/graph/Graph.h"
123
124#include <concepts>
125#include <cstddef>
126#include <cstdint>
127#include <optional>
128#include <span>
129#include <tuple>
130#include <type_traits>
131#include <utility>
132#include <variant>
133#include <vector>
134
135/*--------------------------------------------------------------------------*/
136/*--------------------------- NAMESPACE ------------------------------------*/
137/*--------------------------------------------------------------------------*/
138
139namespace ADS
140{
141
142/*--------------------------------------------------------------------------*/
143/*-------------------------------- EVENTS ----------------------------------*/
144/*--------------------------------------------------------------------------*/
145
148{
149 static constexpr unsigned effects = mod::effect::grows;
150 int u;
152 bool operator==( const NodeInserted & ) const = default;
153 };
154
157{
158 static constexpr unsigned effects = mod::effect::shrinks;
159 int u;
161 bool operator==( const NodeDeleted & ) const = default;
162 };
163
166template< class W >
168{
169 static constexpr unsigned effects = mod::effect::grows;
170 int u;
171 int v;
172 W w;
174 bool operator==( const EdgeInserted & ) const = default;
175 };
176
178template< class W >
180{
181 static constexpr unsigned effects = mod::effect::shrinks;
182 int u;
183 int v;
184 W w;
186 bool operator==( const EdgeDeleted & ) const = default;
187 };
188
190template< class W >
192{
193 static constexpr unsigned effects = mod::effect::up | mod::effect::down;
194 int u;
195 int v;
197 W to;
199 bool operator==( const WeightChanged & ) const = default;
200 };
201
203template< class W >
204[[nodiscard]] constexpr EdgeDeleted< W >
206{
207 return( EdgeDeleted< W >{ e.u , e.v , e.w } );
208 }
209
212template< class W >
213[[nodiscard]] constexpr EdgeInserted< W >
215{
216 return( EdgeInserted< W >{ e.u , e.v , e.w } );
217 }
218
220template< class W >
221[[nodiscard]] constexpr WeightChanged< W >
223{
224 return( WeightChanged< W >{ e.u , e.v , e.to , e.from } );
225 }
226
228template< class W >
233
236template< class W >
237using GraphEvent = std::variant< NodeInserted , NodeDeleted ,
240
243{
244 static constexpr unsigned NODE_INSERT = 1u;
245 static constexpr unsigned NODE_DELETE = 2u;
246 static constexpr unsigned EDGE_INSERT = 4u;
247 static constexpr unsigned EDGE_DELETE = 8u;
248 static constexpr unsigned WEIGHT = 16u;
249 static constexpr unsigned EDGES = 28u;
250 static constexpr unsigned ALL = 31u;
251 };
252
254template< class W >
255[[nodiscard]] constexpr unsigned kindOf( const GraphEvent< W > & e )
256{
257 return( 1u << e.index() );
258 }
259
262template< class W >
263[[nodiscard]] std::pair< int , int > endsOf( const GraphEvent< W > & e );
264
291template< class W >
292[[nodiscard]] std::vector< GraphEvent< W > >
293coalesce( std::span< const GraphEvent< W > > E );
294
297template< class O , class W >
298concept GraphObserver = requires( O & o , const GraphEvent< W > & e ,
299 std::span< const GraphEvent< W > > s )
300{
301 { o.mask() } -> std::convertible_to< unsigned >;
302 o.notify( e );
303 o.notify( s );
304 };
305
306/*--------------------------------------------------------------------------*/
307/*---------------------------- THE COALESCING ------------------------------*/
308/*--------------------------------------------------------------------------*/
309
310namespace mod
311{
312
314template< class W >
316{
318 static std::vector< GraphEvent< W > >
319 coalesce( std::span< const GraphEvent< W > > B ) {
320 return( ADS::coalesce< W >( B ) );
321 }
322 };
323
324} // end( namespace mod )
325
327namespace detail
328{
329
332template< class W >
333struct GraphNotify
334{
336 template< class O , class E >
337 requires( mod::contains_v< E , GraphModifications< W > > &&
338 requires( O & o , const GraphEvent< W > & e ) { o.notify( e ); } )
339 static void one( O & o , const E & e ) {
340 const GraphEvent< W > x( e );
341 o.notify( x );
342 }
344 template< class O >
345 requires requires( O & o , std::span< const GraphEvent< W > > s ) {
346 o.notify( s );
347 }
348 static void many( O & o , std::span< const GraphEvent< W > > s ) {
349 o.notify( s );
350 }
352 template< class O >
353 static std::uint64_t mask( const O & o ) {
354 return( std::uint64_t( unsigned( o.mask() ) ) );
355 }
358 template< class O >
359 requires( mod::with_policy< O > && mod::observes< O , mod::Rebuilt > )
360 static bool early( O & o , std::size_t k ) {
361 return( mod::Direct::early( o , k ) );
362 }
364 template< class O >
365 requires( mod::with_policy< O > && mod::observes< O , mod::Rebuilt > )
366 static void rebuilt( O & o ) noexcept {
367 mod::Direct::rebuilt( o );
368 }
369 };
370
371} // end( namespace detail )
373
374/*--------------------------------------------------------------------------*/
375/*-------------------------- CLASS ObservedGraph ---------------------------*/
376/*--------------------------------------------------------------------------*/
377
380template< Graph G , class Policy = NoObservers >
382{
383
384/*--------------------------------------------------------------------------*/
385 public:
386 using Weight = typename G::Weight;
391 using Subject = typename Policy::template bind< modifications , G >;
394
397 template< class... O >
398 explicit ObservedGraph( G & g , O &... o ) : f_g( g ) , f_sub( o... ) { }
399
401 ObservedGraph( const ObservedGraph & ) = delete;
403 ObservedGraph & operator=( const ObservedGraph & ) = delete;
404
406 [[nodiscard]] const G & graph( void ) const { return( f_g ); }
407
411 template< forwarding_query_tag T >
412 [[nodiscard]] static constexpr auto query( T ) noexcept {
413 if constexpr( std::same_as< T , event_set_t > )
414 return( std::type_identity< modifications >() );
415 else
416 return( query_v< G , T > );
417 }
418
420 [[nodiscard]] unsigned mask( void ) const {
421 return( unsigned( f_sub.mask() ) );
422 }
423
425 [[nodiscard]] bool listening( unsigned k = GraphEvents::ALL ) const {
426 return( ( mask() & k ) != 0 );
427 }
428
430 [[nodiscard]] std::size_t built( void ) const { return( f_sub.built() ); }
431
433 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
435 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
437 [[nodiscard]] const G & observed( void ) const { return( f_g ); }
438
439/*---------------------------- THE GRAPH -----------------------------------*/
440
442 [[nodiscard]] bool directed( void ) const { return( f_g.directed() ); }
444 [[nodiscard]] int n( void ) const { return( f_g.n() ); }
446 [[nodiscard]] int bound( void ) const { return( f_g.bound() ); }
448 [[nodiscard]] bool hasNode( int u ) const { return( f_g.hasNode( u ) ); }
450 [[nodiscard]] std::vector< int > V( void ) const { return( f_g.V() ); }
452 [[nodiscard]] std::vector< int > adj( int u ) const {
453 return( f_g.adj( u ) );
454 }
455
456 template< class F >
457 void forEachAdj( int u , F && f ) const {
458 f_g.forEachAdj( u , std::forward< F >( f ) );
459 }
460
461 template< class F >
462 void forEachEdge( int u , F && f ) const {
463 f_g.forEachEdge( u , std::forward< F >( f ) );
464 }
465
466 [[nodiscard]] std::optional< Weight > weight( int u , int v ) const {
467 return( f_g.weight( u , v ) );
468 }
469
470/*---------------------------- THE CHANGES ---------------------------------*/
471
473 int insertNode( void )
474 requires DynamicGraph< G >
475 {
476 const int u = f_g.insertNode();
477 if constexpr( Subject::template listened< NodeInserted > )
478 if( f_sub.template live< NodeInserted >() ) [[unlikely]]
479 tell( NodeInserted{ u } );
480 return( u );
481 }
482
486 void insertEdge( int u , int v , const Weight & w = Weight() )
487 requires DynamicGraph< G >
488 {
489 if constexpr( Subject::template listened< EdgeInserted< Weight > > ||
490 Subject::template listened< WeightChanged< Weight > > )
491 if( f_sub.template live< EdgeInserted< Weight > >() ||
492 f_sub.template live< WeightChanged< Weight > >() ) [[unlikely]] {
493 if constexpr( Subject::dynamic )
494 insertEdgeCold( u , v , w );
495 else
496 insertEdgeTold( u , v , w );
497 return;
498 }
499 f_g.insertEdge( u , v , w );
500 }
501
504 void deleteEdge( int u , int v )
505 requires DynamicGraph< G >
506 {
507 if constexpr( Subject::template listened< EdgeDeleted< Weight > > )
508 if( f_sub.template live< EdgeDeleted< Weight > >() ) [[unlikely]] {
509 if constexpr( Subject::dynamic )
510 deleteEdgeCold( u , v );
511 else
512 deleteEdgeTold( u , v );
513 return;
514 }
515 f_g.deleteEdge( u , v );
516 }
517
521 void deleteNode( int u )
522 requires DynamicGraph< G >;
523
548 bool setWeight( int u , int v , const Weight & w ) {
549 ADS_PRE( f_g.hasNode( u ) && f_g.hasNode( v ) );
550 if constexpr( Subject::template listened< WeightChanged< Weight > > )
551 if( f_sub.template live< WeightChanged< Weight > >() ) [[unlikely]] {
552 if constexpr( Subject::dynamic )
553 return( setWeightCold( u , v , w ) );
554 else
555 return( setWeightTold( u , v , w ) );
556 }
557 if constexpr( requires { f_g.setWeight( u , v , w ); } )
558 return( f_g.setWeight( u , v , w ) );
559 else {
560 if( ! f_g.weight( u , v ) )
561 return( false );
562 f_g.insertEdge( u , v , w ); // only the weight changes
563 return( true );
564 }
565 }
566
567/*---------------------------- THE UNDO ------------------------------------*/
568
571 requires DynamicGraph< G >
572 {
573 insertEdge( e.u , e.v , e.w );
574 }
575
576 void apply( const EdgeDeleted< Weight > & e )
577 requires DynamicGraph< G >
578 {
579 deleteEdge( e.u , e.v );
580 }
581
582 void apply( const WeightChanged< Weight > & e ) {
583 setWeight( e.u , e.v , e.to );
584 }
585
590 [[nodiscard]] std::size_t checkpoint( void ) const
591 requires( ! Subject::none )
592 {
593 return( f_sub.buffered() );
594 }
595
598 [[nodiscard]] bool revertible( std::size_t t ) const
599 requires( ! Subject::none );
600
606 void rollback( std::size_t t )
607 requires( ! Subject::none );
608
609/*--------------------------- THE OBSERVERS --------------------------------*/
610
614 template< class D >
615 int attach( D & o )
616 requires( Subject::dynamic )
617 {
619 return( f_sub.attach( o ) );
620 else {
621 static_assert( GraphObserver< D , Weight > ,
622 "ADS: neither an observer of the events of the graph nor "
623 "one with mask() and notify()" );
624 return( f_sub.template attachVia< detail::GraphNotify< Weight > >( o ) );
625 }
626 }
627
631 void detach( int id )
632 requires( Subject::dynamic )
633 {
634 f_sub.detach( id );
635 }
636
638 [[nodiscard]] Batch batch( void ) { return( Batch( *this ) ); }
639
641 [[nodiscard]] int depth( void ) const { return( f_sub.depth() ); }
642
643/*--------------------------------------------------------------------------*/
644 private:
646 [[nodiscard]] std::pair< int , int > key( int u , int v ) const {
647 if( ! f_g.directed() && ( v < u ) )
648 return( std::pair< int , int >( v , u ) );
649 return( std::pair< int , int >( u , v ) );
650 }
651
653 template< class E >
654 void tell( const E & e ) {
655 f_sub.emit( e );
656 f_sub.audit( f_g );
657 }
658
660 void insertEdgeTold( int u , int v , const Weight & w );
662 ADS_COLD_PATH void insertEdgeCold( int u , int v , const Weight & w ) {
663 insertEdgeTold( u , v , w );
664 }
665
667 void deleteEdgeTold( int u , int v );
669 ADS_COLD_PATH void deleteEdgeCold( int u ,
670 int v ) { deleteEdgeTold( u , v ); }
671
673 bool setWeightTold( int u , int v , const Weight & w );
675 ADS_COLD_PATH bool setWeightCold( int u , int v , const Weight & w ) {
676 return( setWeightTold( u , v , w ) );
677 }
678
679 G & f_g;
681
682 }; // end( class ObservedGraph )
683
684/*--------------------------------------------------------------------------*/
685/*--------------------------- CLASS Observable -----------------------------*/
686/*--------------------------------------------------------------------------*/
687
691template< Graph G , class... O >
693 G ,
694 Both< Static< mod::Via< O ,
695 detail::GraphNotify< typename G::Weight > >... > ,
696 Dynamic<> > >;
697
698/*--------------------------------------------------------------------------*/
699
700} // end( namespace ADS )
701
702/*--------------------------------------------------------------------------*/
703/*-------------------------- IMPLEMENTATION --------------------------------*/
704/*--------------------------------------------------------------------------*/
705
706#include "ADS/graph/impl/Observable.tpp"
707
708/*--------------------------------------------------------------------------*/
709
710#endif /* Observable.h included */
711
712/*--------------------------------------------------------------------------*/
713/*------------------------- End File Observable.h --------------------------*/
714/*--------------------------------------------------------------------------*/
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
the graph , whose changes are sent through the subject policy Policy [see Modification....
Definition Observable.h:382
void forEachEdge(int u, F &&f) const
f( v , w ) for each edge of weight
Definition Observable.h:462
void deleteNode(int u)
EdgeDeleted for each edge of , deleteNode() of the graph, and NodeDeleted
std::optional< Weight > weight(int u, int v) const
the weight of , nothing if there is no such edge
Definition Observable.h:466
bool directed(void) const
whether the graph is oriented
Definition Observable.h:442
ObservedGraph(const ObservedGraph &)=delete
not copied
void detach(int id)
detaches the observer number
Definition Observable.h:631
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition Observable.h:433
bool hasNode(int u) const
whether is a node
Definition Observable.h:448
unsigned mask(void) const
the union of the masks of the observers
Definition Observable.h:420
static constexpr auto query(T) noexcept
the answer to the forwarded query of the tag T [see Capabilities.h]: that of the graph,...
Definition Observable.h:412
const G & observed(void) const
what the audits of the observers read: the graph
Definition Observable.h:437
std::vector< int > V(void) const
the nodes
Definition Observable.h:450
bool listening(unsigned k=GraphEvents::ALL) const
whether anyone listens to one of the kinds
Definition Observable.h:425
int bound(void) const
a bound of the numbers of the nodes
Definition Observable.h:446
typename Policy::template bind< modifications, G > Subject
the subject
Definition Observable.h:391
std::vector< int > adj(int u) const
the nodes adjacent to
Definition Observable.h:452
Batch batch(void)
opens a channel: the events are collected until it is closed
Definition Observable.h:638
bool revertible(std::size_t t) const
whether the changes since the checkpoint can be undone: all on edges, and every kind listened to,...
int attach(D &o)
attaches the observer , of either form, its mask read now, and gives its number
Definition Observable.h:615
int insertNode(void)
insertNode() of the graph, and NodeInserted
Definition Observable.h:473
std::size_t built(void) const
the events built so far, sent or collected (none when no one listens)
Definition Observable.h:430
const Subject & subject(void) const
the subject
Definition Observable.h:435
void rollback(std::size_t t)
back to the checkpoint of the batches open: the changes collected since undone by their inverses,...
ObservedGraph(G &g, O &... o)
the wrapper of , the static observers o...; must stay alive, and change only through the wrapper
Definition Observable.h:398
void apply(const EdgeInserted< Weight > &e)
the insertion of the edge of , for UndoLog
Definition Observable.h:570
const G & graph(void) const
the graph
Definition Observable.h:406
void apply(const WeightChanged< Weight > &e)
the change of the weight of , for UndoLog
Definition Observable.h:582
void apply(const EdgeDeleted< Weight > &e)
the deletion of the edge of , for UndoLog
Definition Observable.h:576
void forEachAdj(int u, F &&f) const
f( v ) for each node adjacent to
Definition Observable.h:457
int depth(void) const
the depth of the channels open, if none
Definition Observable.h:641
std::size_t checkpoint(void) const
a checkpoint of the batches open, the number of their events so far: with rollback() it makes the wra...
Definition Observable.h:590
ObservedGraph & operator=(const ObservedGraph &)=delete
not assigned
void insertEdge(int u, int v, const Weight &w=Weight())
insertEdge() of the graph, and EdgeInserted, or WeightChanged if the edge was there
Definition Observable.h:486
GraphModifications< Weight > modifications
the events
Definition Observable.h:389
int n(void) const
the number of nodes
Definition Observable.h:444
GraphEvent< Weight > Event
the type of the events
Definition Observable.h:387
ADS::Batch< ObservedGraph > Batch
the channel of a batch, open while it lives [see batch()]
Definition Observable.h:393
bool setWeight(int u, int v, const Weight &w)
the weight of the edge set to , and WeightChanged; false, and nothing, if there is no such edge
Definition Observable.h:548
typename G::Weight Weight
the type of the weights
Definition Observable.h:386
void deleteEdge(int u, int v)
deleteEdge() of the graph, and EdgeDeleted if the edge was there
Definition Observable.h:504
a GRAPH with the insertions and the deletions of its specification
Definition Graph.h:103
an observer of the events of weights W: the kinds that it wants, and the two ways of getting them
Definition Observable.h:298
GRAPH (section 9.2), as far as the visits need it: the nodes, the adjacent nodes of a node,...
Definition Graph.h:90
an observer of at least one of the events of the list L
Definition Modification.h:877
The GRAPH of chapter 9 and its realizations.
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
#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
std::variant< NodeInserted, NodeDeleted, EdgeInserted< W >, EdgeDeleted< W >, WeightChanged< W > > GraphEvent
an event, whose kind is its index in the variant, the variant of GraphModifications< W >
Definition Observable.h:237
constexpr EdgeDeleted< W > inverse(const EdgeInserted< W > &e)
the inverse of an insertion of an edge, its deletion
Definition Observable.h:205
std::vector< GraphEvent< W > > coalesce(std::span< const GraphEvent< W > > E)
the net effect of the events , i.e., the nodes inserted, the edges in the order of their first event ...
std::pair< int, int > endsOf(const GraphEvent< W > &e)
the ends of the edge of , for an event on a node
constexpr unsigned kindOf(const GraphEvent< W > &e)
the bit of the kind of
Definition Observable.h:255
ObservedGraph< G, Both< Static< mod::Via< O, detail::GraphNotify< typename G::Weight > >... >, Dynamic<> > > Observable
the graph , whose changes are sent to the static observers O... of the first form [see GraphObserver]...
Definition Observable.h:692
mod::list< NodeInserted, NodeDeleted, EdgeInserted< W >, EdgeDeleted< W >, WeightChanged< W > > GraphModifications
the events of a graph of weights W, in the order of their kinds
Definition Observable.h:229
Definition Capabilities.h:104
Definition ADT.h:96
the static observers of St, then those attached at run time
Definition Modification.h:1991
observers attached at run time, the events those of the structure (or the list L, which must be them)
Definition Modification.h:1980
an edge deleted, the weight that it had
Definition Observable.h:180
bool operator==(const EdgeDeleted &) const =default
whether the two events are the same
static constexpr unsigned effects
it shrinks
Definition Observable.h:181
int u
the tail
Definition Observable.h:182
int v
the head
Definition Observable.h:183
W w
the weight it had
Definition Observable.h:184
an edge , or with , inserted with the weight
Definition Observable.h:168
int u
the tail
Definition Observable.h:170
static constexpr unsigned effects
it grows
Definition Observable.h:169
int v
the head
Definition Observable.h:171
W w
the weight
Definition Observable.h:172
bool operator==(const EdgeInserted &) const =default
whether the two events are the same
the kinds of event as bits of a mask
Definition Observable.h:243
static constexpr unsigned NODE_INSERT
NodeInserted
Definition Observable.h:244
static constexpr unsigned EDGE_INSERT
EdgeInserted
Definition Observable.h:246
static constexpr unsigned EDGES
the three on edges
Definition Observable.h:249
static constexpr unsigned WEIGHT
WeightChanged
Definition Observable.h:248
static constexpr unsigned NODE_DELETE
NodeDeleted
Definition Observable.h:245
static constexpr unsigned EDGE_DELETE
EdgeDeleted
Definition Observable.h:247
static constexpr unsigned ALL
all of them
Definition Observable.h:250
a node deleted, after the deletion of its edges
Definition Observable.h:157
bool operator==(const NodeDeleted &) const =default
whether the two events are the same
int u
the node
Definition Observable.h:159
static constexpr unsigned effects
it shrinks
Definition Observable.h:158
a node inserted
Definition Observable.h:148
bool operator==(const NodeInserted &) const =default
whether the two events are the same
int u
the node
Definition Observable.h:150
static constexpr unsigned effects
it grows
Definition Observable.h:149
the static observers O... (or mod::Via< O , A >), references called directly in the order of the argu...
Definition Modification.h:1968
the weight of an edge changed from from to to
Definition Observable.h:192
W to
the weight after
Definition Observable.h:197
W from
the weight before
Definition Observable.h:196
int v
the head
Definition Observable.h:195
static constexpr unsigned effects
Definition Observable.h:193
bool operator==(const WeightChanged &) const =default
whether the two events are the same
int u
the tail
Definition Observable.h:194
the observer O called through the adapter A, an argument of Static
Definition Modification.h:954
static std::vector< GraphEvent< W > > coalesce(std::span< const GraphEvent< W > > B)
coalesce( B )
Definition Observable.h:319
the net effect of the events of a batch of the structures whose events are the list L: by default the...
Definition Modification.h:968
static constexpr unsigned grows
the structure may grow
Definition Modification.h:364
static constexpr unsigned down
a value may decrease
Definition Modification.h:367
static constexpr unsigned shrinks
it may shrink
Definition Modification.h:365
static constexpr unsigned up
a value may increase
Definition Modification.h:366
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482