110#ifndef __ADS_Observable
111 #define __ADS_Observable
130#include <type_traits>
213[[nodiscard]]
constexpr EdgeInserted< W >
221[[nodiscard]]
constexpr WeightChanged< W >
249 static constexpr unsigned EDGES = 28u;
250 static constexpr unsigned ALL = 31u;
257 return( 1u << e.index() );
292[[nodiscard]] std::vector< GraphEvent< W > >
297template<
class O ,
class W >
299 std::span< const GraphEvent< W > > s )
301 { o.mask() } -> std::convertible_to< unsigned >;
318 static std::vector< GraphEvent< W > >
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 );
345 requires requires( O & o , std::span< const GraphEvent< W > > s ) {
348 static void many( O & o , std::span<
const GraphEvent< W > > s ) {
353 static std::uint64_t mask(
const O & o ) {
354 return( std::uint64_t(
unsigned( o.mask() ) ) );
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 ) );
365 requires( mod::with_policy< O > && mod::observes< O , mod::Rebuilt > )
366 static void rebuilt( O & o )
noexcept {
367 mod::Direct::rebuilt( o );
380template< Graph G ,
class Policy = NoObservers >
391 using Subject =
typename Policy::template bind< modifications , G >;
397 template<
class... O >
406 [[nodiscard]]
const G &
graph(
void )
const {
return( f_g ); }
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 >() );
420 [[nodiscard]]
unsigned mask(
void )
const {
421 return(
unsigned( f_sub.mask() ) );
426 return( (
mask() & k ) != 0 );
430 [[nodiscard]] std::size_t
built(
void )
const {
return( f_sub.built() ); }
437 [[nodiscard]]
const G &
observed(
void )
const {
return( f_g ); }
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 ) );
458 f_g.forEachAdj( u , std::forward< F >( f ) );
463 f_g.forEachEdge( u , std::forward< F >( f ) );
466 [[nodiscard]] std::optional< Weight >
weight(
int u ,
int v )
const {
467 return( f_g.weight( u , v ) );
476 const int u = f_g.insertNode();
477 if constexpr( Subject::template listened< NodeInserted > )
478 if( f_sub.template live< NodeInserted >() ) [[unlikely]]
489 if constexpr( Subject::template listened< EdgeInserted< Weight > > ||
490 Subject::template listened< WeightChanged< Weight > > )
493 if constexpr( Subject::dynamic )
494 insertEdgeCold( u , v , w );
496 insertEdgeTold( u , v , w );
499 f_g.insertEdge( u , v , w );
507 if constexpr( Subject::template listened< EdgeDeleted< Weight > > )
509 if constexpr( Subject::dynamic )
510 deleteEdgeCold( u , v );
512 deleteEdgeTold( u , v );
515 f_g.deleteEdge( u , v );
549 ADS_PRE( f_g.hasNode( u ) && f_g.hasNode( v ) );
550 if constexpr( Subject::template listened< WeightChanged< Weight > > )
552 if constexpr( Subject::dynamic )
553 return( setWeightCold( u , v , w ) );
555 return( setWeightTold( u , v , w ) );
557 if constexpr(
requires { f_g.setWeight( u , v , w ); } )
558 return( f_g.setWeight( u , v , w ) );
560 if( ! f_g.weight( u , v ) )
562 f_g.insertEdge( u , v , w );
591 requires( ! Subject::none )
593 return( f_sub.buffered() );
599 requires( ! Subject::none );
607 requires( ! Subject::none );
616 requires( Subject::dynamic )
619 return( f_sub.attach( o ) );
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 ) );
632 requires( Subject::dynamic )
641 [[nodiscard]]
int depth(
void )
const {
return( f_sub.depth() ); }
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 ) );
654 void tell(
const E & e ) {
660 void insertEdgeTold(
int u ,
int v ,
const Weight & w );
663 insertEdgeTold( u , v , w );
667 void deleteEdgeTold(
int u ,
int v );
670 int v ) { deleteEdgeTold( u , v ); }
673 bool setWeightTold(
int u ,
int v ,
const Weight & w );
676 return( setWeightTold( u , v , w ) );
691template<
Graph G ,
class... O >
695 detail::GraphNotify< typename G::Weight > >... > ,
706#include "ADS/graph/impl/Observable.tpp"
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
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