ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ObservableArray.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File ObservableArray.h -------------------------*/
3/*--------------------------------------------------------------------------*/
40/*--------------------------------------------------------------------------*/
41/*----------------------------- DEFINITIONS --------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#ifndef __ADS_ObservableArray
45 #define __ADS_ObservableArray
46/* self-identification: #endif constant */
47
48/*--------------------------------------------------------------------------*/
49/*------------------------------ INCLUDES ----------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#include "ADS/Contract.h"
53#include "ADS/Modification.h"
54#include "ADS/Portable.h"
55
56#include <cstddef>
57#include <initializer_list>
58#include <iterator>
59#include <utility>
60#include <vector>
61
62/*--------------------------------------------------------------------------*/
63/*--------------------------- NAMESPACE ------------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66namespace ADS
67{
68
69/*--------------------------------------------------------------------------*/
70/*------------------------- CLASS ObservableArray --------------------------*/
71/*--------------------------------------------------------------------------*/
72
74template< class T , class Policy = NoObservers >
76{
77
78/*--------------------------------------------------------------------------*/
79 public:
80 using value_type = T;
81 using size_type = std::size_t;
83 using const_iterator = typename std::vector< T >::const_iterator;
84
95 using Subject =
96 typename Policy::template bind< modifications , ObservableArray >;
97
100 template< class... O >
101 explicit ObservableArray( std::size_t n = 0 , const T & v = T() , O &... o )
102 : f_a( n , v ) , f_sub( o... ) { }
103
106 template< class... O >
107 ObservableArray( std::initializer_list< T > il , O &... o )
108 : f_a( il ) , f_sub( o... ) { }
109
110/*-------------------------------- THE READS -------------------------------*/
111
113 [[nodiscard]] std::size_t size( void ) const { return( f_a.size() ); }
114
116 [[nodiscard]] bool empty( void ) const { return( f_a.empty() ); }
117
120 [[nodiscard]] const T & operator[]( std::size_t i ) const {
121 ADS_PRE( i < f_a.size() );
122 return( f_a[ i ] );
123 }
124
126 [[nodiscard]] const T * data( void ) const { return( f_a.data() ); }
127
129 [[nodiscard]] const_iterator begin( void ) const { return( f_a.begin() ); }
130
132 [[nodiscard]] const_iterator end( void ) const { return( f_a.end() ); }
133
135 [[nodiscard]] const std::vector< T > & values( void ) const {
136 return( f_a );
137 }
138
139/*------------------------------- THE CHANGES ------------------------------*/
140
156 void set( std::size_t i , const T & v ) {
157 ADS_PRE( i < f_a.size() );
158 if constexpr( Subject::template listened< Assign > )
159 if( f_sub.template live< Assign >() ) [[unlikely]] {
160 if constexpr( Subject::dynamic )
161 setCold( i , v );
162 else
163 setTold( i , v );
164 return;
165 }
166 f_a[ i ] = v;
167 }
168
172 void add( std::size_t i , const T & d ) {
173 ADS_PRE( i < f_a.size() );
174 if constexpr( Subject::template listened< Assign > )
175 if( f_sub.template live< Assign >() ) [[unlikely]] {
176 if constexpr( Subject::dynamic )
177 setCold( i , f_a[ i ] + d );
178 else
179 setTold( i , f_a[ i ] + d );
180 return;
181 }
182 f_a[ i ] += d;
183 }
184
186 void push_back( const T & v ) {
187 if constexpr( Subject::template listened< Insert > )
188 if( f_sub.template live< Insert >() ) [[unlikely]] {
189 if constexpr( Subject::dynamic )
190 pushCold( v );
191 else
192 pushTold( v );
193 return;
194 }
195 f_a.push_back( v );
196 }
197
200 void pop_back( void ) {
201 ADS_PRE( ! f_a.empty() );
202 if constexpr( Subject::template listened< Erase > )
203 if( f_sub.template live< Erase >() ) [[unlikely]] {
204 if constexpr( Subject::dynamic )
205 popCold();
206 else
207 popTold();
208 return;
209 }
210 f_a.pop_back();
211 }
212
214 void clear( void );
215
218 template< std::input_iterator I >
219 void assign( I first , I last );
220
223 void apply( const Assign & e ) { set( e.h , e.to ); }
226 void apply( const Insert & e ) {
227 ADS_PRE( e.h == f_a.size() );
228 push_back( e.value );
229 }
230
232 void apply( const Erase & e ) {
233 ADS_PRE( e.h + 1 == f_a.size() );
234 pop_back();
235 }
236
237/*----------------------------- THE OBSERVERS ------------------------------*/
238
240 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
242 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
243
246 template< class O >
247 int attach( O & o )
248 requires( Subject::dynamic )
249 {
250 return( f_sub.attach( o ) );
251 }
252
255 void detach( int id )
256 requires( Subject::dynamic )
257 {
258 f_sub.detach( id );
259 }
260
261/*--------------------------------------------------------------------------*/
262 private:
264 void setTold( std::size_t i , const T & v );
266 ADS_COLD_PATH void setCold( std::size_t i ,
267 const T & v ) { setTold( i , v ); }
268
270 void pushTold( const T & v );
272 ADS_COLD_PATH void pushCold( const T & v ) { pushTold( v ); }
273
275 void popTold( void );
277 ADS_COLD_PATH void popCold( void ) { popTold(); }
278
279 std::vector< T > f_a;
281
282 }; // end( class ObservableArray )
283
284/*--------------------------------------------------------------------------*/
285
286} // end( namespace ADS )
287
288/*--------------------------------------------------------------------------*/
289/*-------------------------- IMPLEMENTATION --------------------------------*/
290/*--------------------------------------------------------------------------*/
291
292#include "ADS/sequence/impl/ObservableArray.tpp"
293
294/*--------------------------------------------------------------------------*/
295
296#endif /* ObservableArray.h included */
297
298/*--------------------------------------------------------------------------*/
299/*----------------------- End File ObservableArray.h -----------------------*/
300/*--------------------------------------------------------------------------*/
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: ...
T value_type
the values
Definition ObservableArray.h:80
const_iterator begin(void) const
the first value
Definition ObservableArray.h:129
void pop_back(void)
the last value removed, and Erased before
Definition ObservableArray.h:200
void clear(void)
all the values removed, and Cleared
std::size_t size_type
the indices
Definition ObservableArray.h:81
int attach(O &o)
attaches the observer and gives its number
Definition ObservableArray.h:247
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition ObservableArray.h:240
typename std::vector< T >::const_iterator const_iterator
the reads, which never change the array
Definition ObservableArray.h:83
ObservableArray(std::size_t n=0, const T &v=T(), O &... o)
the array of values v, the static observers o...; no event
Definition ObservableArray.h:101
void apply(const Assign &e)
the change of the event , told as such (for UndoLog)
Definition ObservableArray.h:223
typename Policy::template bind< modifications, ObservableArray > Subject
the subject
Definition ObservableArray.h:95
bool empty(void) const
whether there is no value
Definition ObservableArray.h:116
const T & operator[](std::size_t i) const
the value of the index
Definition ObservableArray.h:120
ObservableArray(std::initializer_list< T > il, O &... o)
the array of the values of il, the static observers o...; no event
Definition ObservableArray.h:107
void push_back(const T &v)
appended, and Inserted
Definition ObservableArray.h:186
mod::list< Assign, Insert, Erase, mod::Cleared, mod::Rebuilt > modifications
the events
Definition ObservableArray.h:92
void apply(const Insert &e)
the insertion of
Definition ObservableArray.h:226
void detach(int id)
detaches the observer number
Definition ObservableArray.h:255
void add(std::size_t i, const T &d)
the value of the index raised by (+=), and Assigned
Definition ObservableArray.h:172
mod::Assigned< std::size_t, T > Assign
a value written: the index, the value before and after
Definition ObservableArray.h:86
mod::Inserted< std::size_t, std::size_t, T > Insert
a value appended: its index (the handle and the key) and the value
Definition ObservableArray.h:88
void set(std::size_t i, const T &v)
the value of the index set to , and Assigned
Definition ObservableArray.h:156
const T * data(void) const
the values, in a contiguous array
Definition ObservableArray.h:126
void apply(const Erase &e)
the erasure of
Definition ObservableArray.h:232
std::size_t size(void) const
the number of values
Definition ObservableArray.h:113
const std::vector< T > & values(void) const
the vector of the values
Definition ObservableArray.h:135
const Subject & subject(void) const
the subject
Definition ObservableArray.h:242
mod::Erased< std::size_t, std::size_t, T > Erase
the last value removed: its index and the value
Definition ObservableArray.h:90
const_iterator end(void) const
after the last value
Definition ObservableArray.h:132
void assign(I first, I last)
the values of in place of all those of the array, and Rebuilt
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
#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
Definition ADT.h:96
V to
the value after
Definition Modification.h:608
H h
< the value
Definition Modification.h:606
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
H h
< the value
Definition Modification.h:574
the item of handle , key and value inserted
Definition Modification.h:536
V value
the value
Definition Modification.h:544
H h
< the value
Definition Modification.h:542
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482