ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Maps.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Maps.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
54/*--------------------------------------------------------------------------*/
55/*----------------------------- DEFINITIONS --------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#ifndef __ADS_interop_lemon_Maps
59 #define __ADS_interop_lemon_Maps
60/* self-identification: #endif constant */
61
62/*--------------------------------------------------------------------------*/
63/*------------------------------ INCLUDES ----------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#if ! __has_include( <lemon/core.h> )
67 #error "the interoperation with LEMON needs the headers of LEMON"
68#endif
69
70#include "ADS/Contract.h"
71#include "ADS/Modification.h"
72
73#include <lemon/core.h>
74
75#include <algorithm>
76#include <cstddef>
77#include <memory>
78#include <type_traits>
79#include <utility>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS::lemon_interop
86{
87
88/*--------------------------------------------------------------------------*/
89/*---------------------------- CLASS DenseMap ------------------------------*/
90/*--------------------------------------------------------------------------*/
91
94template< class GR , class T , class Item = typename GR::Node >
96{
97
98/*--------------------------------------------------------------------------*/
99 public:
100 using Key = Item;
101 using Value = T;
102 using Reference = T &;
103 using ConstReference = const T &;
104 using ReferenceMapTag = ::lemon::True;
105
108 explicit DenseMap( const GR & g , const T & t = T() )
109 : f_g( &g ) , f_n( std::size_t( g.maxId( Item() ) + 1 ) ) ,
110 f_v( new T[ f_n ] ) {
111 std::fill_n( f_v.get() , f_n , t );
112 }
113
115 [[nodiscard]] Reference operator[]( const Key & k ) {
116 return( f_v[ at( k ) ] );
117 }
118
119 [[nodiscard]] ConstReference operator[]( const Key & k ) const {
120 return( f_v[ at( k ) ] );
121 }
122
124 void set( const Key & k , const T & t ) { f_v[ at( k ) ] = t; }
125
127 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
128
129/*--------------------------------------------------------------------------*/
130 private:
132 [[nodiscard]] std::size_t at( const Key & k ) const {
133 const auto i = std::size_t( f_g->id( k ) );
134 ADS_PRE( i < f_n );
135 return( i );
136 }
137
138 const GR * f_g;
139 std::size_t f_n;
140 std::unique_ptr< T[] > f_v;
141
142 }; // end( class DenseMap )
143
144/*--------------------------------------------------------------------------*/
145/*---------------------------- CLASS IndexMap ------------------------------*/
146/*--------------------------------------------------------------------------*/
147
148namespace detail
149{
150
152template< class S >
153decltype( auto ) readAt( const S & s , std::size_t i )
154{
155 if constexpr( requires { s.get( i ); } )
156 return( s.get( i ) );
157 else
158 return( s[ i ] );
159 }
160
161} // end( namespace detail )
162
165template< class GR , class S , class Item = typename GR::Node >
167{
168
169/*--------------------------------------------------------------------------*/
170 public:
171 using Key = Item;
173 using Value = std::remove_cvref_t< decltype( detail::readAt(
174 std::declval< const S & >() , std::size_t() ) ) >;
175
178 IndexMap( const GR & g , S & s ) : f_g( &g ) , f_s( &s ) {
179 ADS_PRE( std::size_t( g.maxId( Item() ) + 1 ) <= s.size() );
180 }
181
183 [[nodiscard]] Value operator[]( const Key & k ) const {
184 return( detail::readAt( *f_s , at( k ) ) );
185 }
186
188 void set( const Key & k , const Value & v ) { f_s->set( at( k ) , v ); }
189
192 [[nodiscard]] Value prefix( const Key & k ) const
193 requires requires( const S & s ) { s.prefix( std::size_t() ); }
194 {
195 return( f_s->prefix( at( k ) + 1 ) );
196 }
197
199 [[nodiscard]] S & structure( void ) const { return( *f_s ); }
200
201/*--------------------------------------------------------------------------*/
202 private:
204 [[nodiscard]] std::size_t at( const Key & k ) const {
205 const auto i = std::size_t( f_g->id( k ) );
206 ADS_PRE( i < f_s->size() );
207 return( i );
208 }
209
210 const GR * f_g;
211 S * f_s;
212
213 }; // end( class IndexMap )
214
215/*--------------------------------------------------------------------------*/
216/*--------------------------- CLASS ObservedMap ----------------------------*/
217/*--------------------------------------------------------------------------*/
218
221template< class GR , class Map , class A = mod::Cost ,
222 class Policy = Dynamic<> >
224{
225
226/*--------------------------------------------------------------------------*/
227 public:
228 using Key = typename Map::Key;
229 using Value = typename Map::Value;
233 using Subject = typename Policy::template bind< modifications , ObservedMap >;
234
237 template< class... O >
238 ObservedMap( const GR & g , Map & m , O &... o )
239 : f_g( g ) , f_m( m ) , f_sub( o... ) { }
240
242 [[nodiscard]] Value operator[]( const Key & k ) const { return( f_m[ k ] ); }
243
245 void set( const Key & k , const Value & v ) {
246 if constexpr( Subject::template listened< Event > )
247 if( f_sub.template live< Event >() ) [[unlikely]] {
248 const Value old = f_m[ k ];
249 f_m.set( k , v );
250 f_sub.emit( Event{ f_g.id( k ) , old , v } );
251 return;
252 }
253 f_m.set( k , v );
254 }
255
257 [[nodiscard]] const Map & map( void ) const { return( f_m ); }
258
260 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
261
264 template< class O >
265 int attach( O & o )
266 requires( Subject::dynamic )
267 {
268 return( f_sub.attach( o ) );
269 }
270
273 void detach( int id )
274 requires( Subject::dynamic )
275 {
276 f_sub.detach( id );
277 }
278
279/*--------------------------------------------------------------------------*/
280 private:
281 const GR & f_g;
282 Map & f_m;
284
285 }; // end( class ObservedMap )
286
287/*--------------------------------------------------------------------------*/
288
289} // end( namespace ADS::lemon_interop )
290
291/*--------------------------------------------------------------------------*/
292
293#endif /* Maps.h included */
294
295/*--------------------------------------------------------------------------*/
296/*---------------------------- End File Maps.h -----------------------------*/
297/*--------------------------------------------------------------------------*/
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...
const T & ConstReference
a reference to a value read
Definition Maps.h:103
::lemon::True ReferenceMapTag
a ReferenceMap
Definition Maps.h:104
void set(const Key &k, const T &t)
the value of set to
Definition Maps.h:124
std::size_t size(void) const
the number of values, maxId( Item() ) + 1 at the construction
Definition Maps.h:127
T & Reference
a reference to a value
Definition Maps.h:102
T Value
the values
Definition Maps.h:101
ConstReference operator[](const Key &k) const
the value of , read
Definition Maps.h:119
DenseMap(const GR &g, const T &t=T())
the map of the items of , all ; must stay alive and keep its items
Definition Maps.h:108
Item Key
the items
Definition Maps.h:100
Reference operator[](const Key &k)
the value of
Definition Maps.h:115
S & structure(void) const
the structure
Definition Maps.h:199
Item Key
the items
Definition Maps.h:171
std::remove_cvref_t< decltype(detail::readAt( std::declval< const S & >(), std::size_t())) > Value
the values, those that the structure reads
Definition Maps.h:173
Value operator[](const Key &k) const
the value of
Definition Maps.h:183
void set(const Key &k, const Value &v)
the value of set to , by set() of the structure
Definition Maps.h:188
IndexMap(const GR &g, S &s)
the map of the items of on ; both must stay alive
Definition Maps.h:178
Value prefix(const Key &k) const
the aggregate of the values of the ids up to that of , by prefix() of the structure (e....
Definition Maps.h:192
ObservedMap(const GR &g, Map &m, O &... o)
the map of the items of , the static observers o...; both must stay alive
Definition Maps.h:238
Value operator[](const Key &k) const
the value of
Definition Maps.h:242
typename Policy::template bind< modifications, ObservedMap > Subject
the subject
Definition Maps.h:233
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition Maps.h:260
void set(const Key &k, const Value &v)
the value of set to , and Changed
Definition Maps.h:245
typename Map::Value Value
the values
Definition Maps.h:229
int attach(O &o)
attaches the observer and gives its number
Definition Maps.h:265
mod::Changed< A, int, Value > Event
a write
Definition Maps.h:230
typename Map::Key Key
the items
Definition Maps.h:228
void detach(int id)
detaches the observer number
Definition Maps.h:273
const Map & map(void) const
the map below
Definition Maps.h:257
mod::list< Event > modifications
the events
Definition Maps.h:231
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#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
the attribute A of the item of handle changed from from to to
Definition Modification.h:600