ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Observers.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Observers.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_interop_lemon_Observers
76 #define __ADS_interop_lemon_Observers
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#if ! __has_include( <lemon/core.h> )
84 #error "the interoperation with LEMON needs the headers of LEMON"
85#endif
86
87#include "ADS/Contract.h"
88#include "ADS/Modification.h"
89#include "ADS/graph/Graph.h"
91
92#include <lemon/bits/alteration_notifier.h>
93#include <lemon/bits/traits.h>
94#include <lemon/core.h>
95#include <lemon/list_graph.h>
96
97#include <cstddef>
98#include <cstdint>
99#include <type_traits>
100#include <unordered_map>
101#include <vector>
102
103/*--------------------------------------------------------------------------*/
104/*--------------------------- NAMESPACE ------------------------------------*/
105/*--------------------------------------------------------------------------*/
106
107namespace ADS::lemon_interop
108{
109
110/*--------------------------------------------------------------------------*/
111/*---------------------------- CLASS LemonTap ------------------------------*/
112/*--------------------------------------------------------------------------*/
113
115struct Ends
116{
117 int u;
118 int v;
120 bool operator==( const Ends & ) const = default;
121 };
122
124namespace detail
125{
126
128template< class M >
129struct ValueOf
130{
131 using type = typename M::Value;
132 };
133
136template< class GR , class Item , class CostMap >
137struct TapEvents
138{
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;
147 };
148
149} // end( namespace detail )
151
154template< class GR , class Item = typename GR::Arc , class CostMap = void ,
155 class Policy = Dynamic<> >
157 : public ::lemon::ItemSetTraits< GR , Item >::ItemNotifier::ObserverBase
158{
159
160 using Notifier = typename ::lemon::ItemSetTraits< GR , Item >::ItemNotifier;
161 using Base = typename Notifier::ObserverBase;
162 using T = detail::TapEvents< GR , Item , CostMap >;
163
164 static constexpr bool withCost = ! std::is_void_v< CostMap >;
165
166/*--------------------------------------------------------------------------*/
167 public:
168 using Key = typename T::Key;
169 using Insert = typename T::Ins;
170 using Erase = typename T::Era;
173 mod::Rebuilt >;
175 using Subject = typename Policy::template bind< modifications , LemonTap >;
176
179 template< class... O >
180 explicit LemonTap( const GR & g , O &... o )
181 requires( ! withCost )
182 : f_g( g ) , f_c( nullptr ) , f_sub( o... ) {
183 Base::attach( g.notifier( Item() ) );
184 }
185
189 template< class C , class... O >
190 LemonTap( const GR & g , const C & c , O &... 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() ) );
194 }
195
198 [[nodiscard]] bool spoiled( void ) const { return( f_spoiled ); }
199
201 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
203 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
204
207 template< class O >
208 int attach( O & o )
209 requires( Subject::dynamic )
210 {
211 return( f_sub.attach( o ) );
212 }
213
216 void detach( int id )
217 requires( Subject::dynamic )
218 {
219 f_sub.detach( id );
220 }
221
222/*--------------------------------------------------------------------------*/
223 protected:
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 { } );
229 }
230
232 void add( const std::vector< Item > & X ) override {
233 if constexpr( Subject::template listened< Insert > )
234 if( f_sub.template live< Insert >() ) {
235 auto b = ADS::batch( *this );
236 for( const Item & x : X )
237 add( x );
238 }
239 }
240
242 void erase( const Item & x ) noexcept override {
243 if constexpr( Subject::template listened< Erase > )
244 if( f_sub.template live< Erase >() )
245 try {
246 f_sub.emit( erased( x ) );
247 }
248 catch( ... ) {
249 f_spoiled = true;
250 }
251 }
252
254 void erase( const std::vector< Item > & X ) noexcept override {
255 if constexpr( Subject::template listened< Erase > )
256 if( f_sub.template live< Erase >() )
257 try {
258 auto b = ADS::batch( *this );
259 for( const Item & x : X )
260 f_sub.emit( erased( x ) );
261 }
262 catch( ... ) {
263 f_spoiled = true;
264 }
265 }
266
268 void build( void ) override {
269 if( f_sub.template live< mod::Rebuilt >() )
270 f_sub.emit( mod::Rebuilt{} );
271 }
272
274 void clear( void ) noexcept override {
275 if( f_sub.template live< mod::Cleared >() )
276 try {
277 f_sub.emit( mod::Cleared{} );
278 }
279 catch( ... ) {
280 f_spoiled = true;
281 }
282 }
283
284/*--------------------------------------------------------------------------*/
285 private:
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 ) ) } );
290 else
291 return( f_g.id( x ) );
292 }
293
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 ] } );
298 else
299 return( Erase{ f_g.id( x ) , key( x ) } );
300 }
301
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 ] } );
306 else
307 return( Insert{ f_g.id( x ) , key( x ) } );
308 }
309
311 using CM = std::conditional_t< withCost , CostMap , int >;
312
313 const GR & f_g;
314 const CM * f_c;
315 bool f_spoiled = false;
317
318 }; // end( class LemonTap )
319
320/*--------------------------------------------------------------------------*/
321/*--------------------------- CLASS LemonMirror ----------------------------*/
322/*--------------------------------------------------------------------------*/
323
326template< Graph G >
328{
329
330/*--------------------------------------------------------------------------*/
331 public:
332 using Weight = typename G::Weight;
333 using LG = ::lemon::ListDigraph;
334 using WeightMap = LG::ArcMap< Weight >;
335
337 struct Kinds
338 {
339 bool nodes = false;
340 bool arcs = false;
341 bool weights = false;
342 bool down = false;
344 [[nodiscard]] bool any( void ) const { return( nodes || arcs || weights ); }
345 };
346
349 explicit LemonMirror( const G & g );
350
352 LemonMirror( const LemonMirror & ) = delete;
354 LemonMirror & operator=( const LemonMirror & ) = delete;
355
357 void on( const NodeInserted & e );
359 void on( const NodeDeleted & e ) noexcept;
361 void on( const EdgeInserted< Weight > & e );
363 void on( const EdgeDeleted< Weight > & e ) noexcept;
365 void on( const WeightChanged< Weight > & e );
366
368 [[nodiscard]] const LG & digraph( void ) const { return( f_lg ); }
369
371 [[nodiscard]] const WeightMap & weights( void ) const { return( f_w ); }
372
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 ) );
378 }
379
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 );
385 }
386
388 [[nodiscard]] const Kinds & kinds( void ) const { return( f_kinds ); }
389
391 [[nodiscard]] bool stale( void ) const { return( f_kinds.any() ); }
392
394 void clearKinds( void ) { f_kinds = Kinds{}; }
395
396/*--------------------------------------------------------------------------*/
397 private:
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 ) );
402 }
403
405 void put( int u , int v , const Weight & w );
406
408 void drop( int u , int v ) noexcept;
409
410 bool f_directed;
411 LG f_lg;
412 WeightMap f_w;
413 std::vector< LG::Node > f_node;
414 std::unordered_map< std::uint64_t , LG::Arc > f_arc;
415 Kinds f_kinds;
416
417 }; // end( class LemonMirror )
418
419template< Graph G >
421 : f_directed( g.directed() ) , f_w( f_lg )
422{
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 )
426 if( g.hasNode( u ) )
427 f_node[ std::size_t( u ) ] = f_lg.addNode();
428 for( int u : g.V() )
429 g.forEachEdge( u , [ & ]( int v , const Weight & w ) {
430 if( f_directed || ( u < v ) )
431 put( u , v , w );
432 } );
433 }
434
435template< Graph G >
437{
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;
442 }
443
444template< Graph G >
445void LemonMirror< G >::on( const NodeDeleted & e ) noexcept
446{
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;
450 }
451
452template< Graph G >
454{
455 put( e.u , e.v , e.w );
456 f_kinds.arcs = true;
457 }
458
459template< Graph G >
461{
462 drop( e.u , e.v );
463 if( ! f_directed )
464 drop( e.v , e.u );
465 f_kinds.arcs = true;
466 f_kinds.down = true;
467 }
468
469template< Graph G >
471{
472 f_w.set( f_arc.at( key( e.u , e.v ) ) , e.to );
473 if( ! f_directed )
474 f_w.set( f_arc.at( key( e.v , e.u ) ) , e.to );
475 f_kinds.weights = true;
476 if( e.to < e.from )
477 f_kinds.down = true;
478 }
479
480template< Graph G >
481void LemonMirror< G >::put( int u , int v , const Weight & w )
482{
483 const LG::Arc a =
484 f_lg.addArc( f_node[ std::size_t( u ) ] , f_node[ std::size_t( v ) ] );
485 f_w.set( a , w );
486 f_arc[ key( u , v ) ] = a;
487 if( ! f_directed ) {
488 const LG::Arc b =
489 f_lg.addArc( f_node[ std::size_t( v ) ] , f_node[ std::size_t( u ) ] );
490 f_w.set( b , w );
491 f_arc[ key( v , u ) ] = b;
492 }
493 }
494
495template< Graph G >
496void LemonMirror< G >::drop( int u , int v ) noexcept
497{
498 const auto it = f_arc.find( key( u , v ) );
499 if( it != f_arc.end() ) {
500 f_lg.erase( it->second );
501 f_arc.erase( it );
502 }
503 }
504
505/*--------------------------------------------------------------------------*/
506
507} // end( namespace ADS::lemon_interop )
508
509/*--------------------------------------------------------------------------*/
510
511#endif /* Observers.h included */
512
513/*--------------------------------------------------------------------------*/
514/*-------------------------- End File Observers.h --------------------------*/
515/*--------------------------------------------------------------------------*/
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
Definition Digraphs.h:89
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