75#ifndef __ADS_interop_lemon_Observers
76 #define __ADS_interop_lemon_Observers
83#if ! __has_include( <lemon/core.h> )
84 #error "the interoperation with LEMON needs the headers of LEMON"
92#include <lemon/bits/alteration_notifier.h>
93#include <lemon/bits/traits.h>
94#include <lemon/core.h>
95#include <lemon/list_graph.h>
100#include <unordered_map>
131 using type =
typename M::Value;
136template<
class GR ,
class Item ,
class CostMap >
139 static constexpr bool arcs = std::is_same_v< Item , typename GR::Arc >;
140 static constexpr bool cost = arcs && ! std::is_void_v< CostMap >;
141 using Key = std::conditional_t< arcs , Ends , int >;
143 using V =
typename std::conditional_t< cost , ValueOf< CostMap > ,
144 std::type_identity< void > >::type;
154template<
class GR ,
class Item =
typename GR::Arc ,
class CostMap = void ,
155 class Policy = Dynamic<> >
157 :
public ::lemon::ItemSetTraits< GR , Item >::ItemNotifier::ObserverBase
160 using Notifier = typename ::lemon::ItemSetTraits< GR , Item >::ItemNotifier;
161 using Base =
typename Notifier::ObserverBase;
162 using T = detail::TapEvents< GR , Item , CostMap >;
164 static constexpr bool withCost = ! std::is_void_v< CostMap >;
168 using Key =
typename T::Key;
175 using Subject =
typename Policy::template bind< modifications , LemonTap >;
179 template<
class... O >
181 requires( ! withCost )
182 : f_g( g ) , f_c( nullptr ) , f_sub( o... ) {
183 Base::attach( g.notifier( Item() ) );
189 template<
class C ,
class... O >
191 requires( withCost && std::is_same_v< C , CostMap > )
192 : f_g( g ) , f_c( &c ) , f_sub( o... ) {
193 Base::attach( g.notifier( Item() ) );
198 [[nodiscard]]
bool spoiled(
void )
const {
return( f_spoiled ); }
209 requires( Subject::dynamic )
211 return( f_sub.attach( o ) );
217 requires( Subject::dynamic )
225 void add(
const Item & x )
override {
226 if constexpr( Subject::template listened< Insert > )
227 if( f_sub.template live< Insert >() )
228 f_sub.emit( inserted( x ) , [](
void )
noexcept { } );
232 void add(
const std::vector< Item > & X )
override {
233 if constexpr( Subject::template listened< Insert > )
234 if( f_sub.template live< Insert >() ) {
236 for(
const Item & x : X )
242 void erase(
const Item & x )
noexcept override {
243 if constexpr( Subject::template listened< Erase > )
244 if( f_sub.template live< Erase >() )
246 f_sub.emit( erased( x ) );
254 void erase(
const std::vector< Item > & X )
noexcept override {
255 if constexpr( Subject::template listened< Erase > )
256 if( f_sub.template live< Erase >() )
259 for(
const Item & x : X )
260 f_sub.emit( erased( x ) );
269 if( f_sub.template live< mod::Rebuilt >() )
274 void clear(
void )
noexcept override {
275 if( f_sub.template live< mod::Cleared >() )
287 [[nodiscard]]
Key key(
const Item & x )
const {
288 if constexpr( T::arcs )
289 return(
Key{ f_g.id( f_g.source( x ) ) , f_g.id( f_g.target( x ) ) } );
291 return( f_g.id( x ) );
295 [[nodiscard]]
Erase erased(
const Item & x )
const {
296 if constexpr( withCost && T::arcs )
297 return(
Erase{ f_g.id( x ) , key( x ) , ( *f_c )[ x ] } );
299 return(
Erase{ f_g.id( x ) , key( x ) } );
303 [[nodiscard]]
Insert inserted(
const Item & x )
const {
304 if constexpr( withCost && T::arcs )
305 return(
Insert{ f_g.id( x ) , key( x ) , ( *f_c )[ x ] } );
307 return(
Insert{ f_g.id( x ) , key( x ) } );
311 using CM = std::conditional_t< withCost , CostMap , int >;
315 bool f_spoiled =
false;
333 using LG = ::lemon::ListDigraph;
368 [[nodiscard]]
const LG &
digraph(
void )
const {
return( f_lg ); }
374 [[nodiscard]] LG::Node
node(
int u )
const {
375 return( ( u >= 0 ) && ( std::size_t( u ) < f_node.size() )
376 ? f_node[ std::size_t( u ) ]
377 : LG::Node( ::lemon::INVALID ) );
382 [[nodiscard]] LG::Arc
arc(
int u ,
int v )
const {
383 const auto it = f_arc.find( key( u , v ) );
384 return( it == f_arc.end() ? LG::Arc( ::lemon::INVALID ) : it->second );
388 [[nodiscard]]
const Kinds &
kinds(
void )
const {
return( f_kinds ); }
391 [[nodiscard]]
bool stale(
void )
const {
return( f_kinds.any() ); }
399 static std::uint64_t key(
int u ,
int v ) {
400 return( ( std::uint64_t( std::uint32_t( u ) ) << 32 ) |
401 std::uint32_t( v ) );
405 void put(
int u ,
int v ,
const Weight & w );
408 void drop(
int u ,
int v )
noexcept;
413 std::vector< LG::Node > f_node;
414 std::unordered_map< std::uint64_t , LG::Arc > f_arc;
421 : f_directed( g.directed() ) , f_w( f_lg )
423 const int n = g.bound();
424 f_node.assign( std::size_t( n ) , LG::Node( ::lemon::INVALID ) );
425 for(
int u = 0 ; u < n ; ++u )
427 f_node[ std::size_t( u ) ] = f_lg.addNode();
429 g.forEachEdge( u , [ & ](
int v ,
const Weight & w ) {
430 if( f_directed || ( u < v ) )
438 if( f_node.size() <= std::size_t( e.
u ) )
439 f_node.resize( std::size_t( e.
u ) + 1 , LG::Node( ::lemon::INVALID ) );
440 f_node[ std::size_t( e.
u ) ] = f_lg.addNode();
441 f_kinds.nodes =
true;
447 f_lg.erase( f_node[ std::size_t( e.u ) ] );
448 f_node[ std::size_t( e.u ) ] = ::lemon::INVALID;
449 f_kinds.nodes =
true;
455 put( e.
u , e.
v , e.
w );
472 f_w.set( f_arc.at( key( e.
u , e.
v ) ) , e.
to );
474 f_w.set( f_arc.at( key( e.
v , e.
u ) ) , e.
to );
475 f_kinds.weights =
true;
481void LemonMirror< G >::put(
int u ,
int v ,
const Weight & w )
484 f_lg.addArc( f_node[ std::size_t( u ) ] , f_node[ std::size_t( v ) ] );
486 f_arc[ key( u , v ) ] = a;
489 f_lg.addArc( f_node[ std::size_t( v ) ] , f_node[ std::size_t( u ) ] );
491 f_arc[ key( v , u ) ] = b;
496void LemonMirror< G >::drop(
int u ,
int v )
noexcept
498 const auto it = f_arc.find( key( u , v ) );
499 if( it != f_arc.end() ) {
500 f_lg.erase( it->second );
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...
A GRAPH that tells its changes, beyond the book: the wrapper Observable< G , O... > routes the change...
LemonMirror(const LemonMirror &)=delete
not copied: the maps of LEMON are bound to their digraph
LG::Arc arc(int u, int v) const
the arc of LEMON of the arc of ADS, INVALID if none
Definition Observers.h:382
::lemon::ListDigraph LG
the digraph of LEMON
Definition Observers.h:333
LG::Node node(int u) const
the node of LEMON of the name of ADS, INVALID if none
Definition Observers.h:374
bool stale(void) const
whether something changed since the last clearKinds()
Definition Observers.h:391
void on(const NodeInserted &e)
the node inserted
Definition Observers.h:436
const Kinds & kinds(void) const
the kinds of the events since the last clearKinds()
Definition Observers.h:388
const WeightMap & weights(void) const
the weights of its arcs
Definition Observers.h:371
typename G::Weight Weight
the type of the weights
Definition Observers.h:332
void clearKinds(void)
the kinds forgotten, e.g., after an algorithm of LEMON ran again
Definition Observers.h:394
LemonMirror & operator=(const LemonMirror &)=delete
not assigned
LemonMirror(const G &g)
the mirror of as it is now, in expected time
Definition Observers.h:420
const LG & digraph(void) const
the digraph of LEMON
Definition Observers.h:368
LG::ArcMap< Weight > WeightMap
its weights
Definition Observers.h:334
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition Observers.h:201
LemonTap(const GR &g, const C &c, O &... o)
the tap of the arcs of with the costs , the static observers o...; it must be constructed after [se...
Definition Observers.h:190
const Subject & subject(void) const
the subject
Definition Observers.h:203
void build(void) override
Rebuilt after the items were built anew
Definition Observers.h:268
void clear(void) noexcept override
Cleared before all the items leave
Definition Observers.h:274
typename Policy::template bind< modifications, LemonTap > Subject
the subject
Definition Observers.h:175
void detach(int id)
detaches the observer number
Definition Observers.h:216
void erase(const Item &x) noexcept override
Erased before the removal of , with its cost
Definition Observers.h:242
bool spoiled(void) const
whether an observer of ADS threw within erase() or clear(), which LEMON forbids: its state may no lon...
Definition Observers.h:198
mod::list< Insert, Erase, mod::Cleared, mod::Rebuilt > modifications
the events
Definition Observers.h:172
typename T::Key Key
the key of an item: its ends, or its id
Definition Observers.h:168
LemonTap(const GR &g, O &... o)
the tap of the items of , the static observers o...; must outlive it
Definition Observers.h:180
void add(const Item &x) override
Inserted after the insertion of
Definition Observers.h:225
void add(const std::vector< Item > &X) override
the insertions of , in a batch
Definition Observers.h:232
void erase(const std::vector< Item > &X) noexcept override
the erasures of , in a batch
Definition Observers.h:254
typename T::Ins Insert
an insertion
Definition Observers.h:169
int attach(O &o)
attaches the observer and gives its number
Definition Observers.h:208
typename T::Era Erase
an erasure
Definition Observers.h:170
The GRAPH of chapter 9 and its realizations.
Batch< S > batch(S &s)
a batch on the structure , open while it lives: its events are collected, and the outermost batch sen...
Definition Modification.h:2150
#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
an edge deleted, the weight that it had
Definition Observable.h:180
an edge , or with , inserted with the weight
Definition Observable.h:168
int u
the tail
Definition Observable.h:170
int v
the head
Definition Observable.h:171
W w
the weight
Definition Observable.h:172
a node deleted, after the deletion of its edges
Definition Observable.h:157
a node inserted
Definition Observable.h:148
int u
the node
Definition Observable.h:150
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
int u
the tail
Definition Observable.h:194
the ends of an arc, the key of its events
Definition Observers.h:116
int u
the tail
Definition Observers.h:117
bool operator==(const Ends &) const =default
whether the two pairs are the same
int v
the head
Definition Observers.h:118
what the events since the last clearKinds() changed
Definition Observers.h:338
bool nodes
a node inserted or deleted
Definition Observers.h:339
bool down
a weight fell, or an arc left
Definition Observers.h:342
bool weights
a weight changed
Definition Observers.h:341
bool any(void) const
whether anything changed
Definition Observers.h:344
bool arcs
an arc inserted or deleted
Definition Observers.h:340
the structure emptied
Definition Modification.h:699
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
anything may have changed: forget and recompute
Definition Modification.h:708
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482