push_and_pop_dynamic.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12  ** Ifdefs
13  *****************************************************************************/
14 
15 #ifndef ECL_CONTAINERS_PUSH_AND_POP_DYNAMIC_HPP_
16 #define ECL_CONTAINERS_PUSH_AND_POP_DYNAMIC_HPP_
17 
18 /*****************************************************************************
19  ** Includes
20  *****************************************************************************/
21 
22 #include <ecl/config/macros.hpp>
23 #include "push_and_pop_fixed.hpp"
24 
25 /*****************************************************************************
26  ** Namespaces
27  *****************************************************************************/
28 
29 namespace ecl
30 {
31 
32 /*****************************************************************************
33 ** Forward Declarations
34 *****************************************************************************/
35 
36 namespace formatters {
37 
38 template <typename Type, size_t N> class PushAndPopFormatter;
39 
40 } // namespace formatters
41 
42 /*****************************************************************************
43  ** Interface
44  *****************************************************************************/
70 template<typename Type>
72 {
73 public:
74  typedef Type value_type;
75  typedef Type* iterator;
76  typedef const Type* const_iterator;
77  typedef Type& reference;
78  typedef const Type& const_reference;
79  typedef std::size_t size_type;
80  typedef std::ptrdiff_t difference_type;
81  typedef std::reverse_iterator<iterator> reverse_iterator;
82  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
90  PushAndPop() : size_fifo(0), leader(0), follower(0) {}
91 
92  PushAndPop( const unsigned int length ) ecl_assert_throw_decl(StandardException)
93  : size_fifo(length+1), leader(0), follower(0)
94  {
95  ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
96  data.resize( size_fifo );
97  }
98 
99  PushAndPop( const unsigned int length, const Type & d ) ecl_assert_throw_decl(StandardException) :
100  size_fifo(length+1),
101  leader(0),
102  follower(0)
103  {
104  ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
105  data.resize( size_fifo );
106  fill( d );
107  }
108  virtual ~PushAndPop()
109  {}
110 
111  /*********************
112  ** Iterators
113  **********************/
114  /* ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! ACHTUNG! */
115  /*
116  * These have not been updated to take care with the push/pop, they just point to the
117  * start and end of the underlying array!
118  */
125  iterator begin() ecl_assert_throw_decl(StandardException) {
126  return data.begin(); // underlying array will throw if no storage has been allocated
127  }
134  const_iterator begin() const ecl_assert_throw_decl(StandardException) {
135  return data.begin(); // underlying array will throw if no storage has been allocated
136  }
143  iterator end() ecl_assert_throw_decl(StandardException) {
144  return data.end(); // underlying array will throw if no storage has been allocated
145  }
152  const_iterator end() const ecl_assert_throw_decl(StandardException) {
153  return data.end(); // underlying array will throw if no storage has been allocated
154  }
161  reverse_iterator rbegin() ecl_assert_throw_decl(StandardException) {
162  return reverse_iterator(end());
163  }
170  const_reverse_iterator rbegin() const ecl_assert_throw_decl(StandardException) {
171  return const_reverse_iterator(end());
172  }
179  reverse_iterator rend() ecl_assert_throw_decl(StandardException) {
180  return reverse_iterator(begin());
181  }
188  const_reverse_iterator rend() const ecl_assert_throw_decl(StandardException) {
189  return const_reverse_iterator(begin());
190  }
191 
192 
193  /*********************
194  ** Accessors
195  **********************/
196  // Cannot do stencils easily as push and pop roll over - not guaranteed of being contiguous
197 
198  Type & operator[] (int idx)
199  {
200  return data[ ((follower+idx)%size_fifo) ];
201  }
202 
203  const Type & operator[] (int idx) const
204  {
205  return data[ ((follower+idx)%size_fifo) ];
206  }
207 
208  void operator() (const PushAndPop<Type,0> & otherOne )
209  {
210  leader = otherOne.leader;
211  follower = otherOne.follower;
212  for( int i=0; i<size_fifo; i++ )
213  {
214  data[i] = otherOne.data[i];
215  }
216  }
217 
224  void push_back( const Type & datum )
225  {
226  data[ leader++ ] = datum;
227  leader %= size_fifo;
228  if( leader == follower )
229  {
230  follower++;
231  follower %= size_fifo;
232  // it just rolls over
233  }
234  }
235 
236  Type pop_front()
237  {
238  ecl_assert_throw( (follower != leader), StandardException(LOC, OutOfRangeError, "PushAndPop follower==leader"));
239  Type value = data[follower++];
240  follower %= size_fifo;
241  return value;
242  }
243 
244  void fill( const Type & d )
245  {
246  for( unsigned int i=0; i<size_fifo; i++ ) data[i] = d;
247  }
248 
249  void resize( unsigned int length )
250  {
251  size_fifo = length+1;
252  ecl_assert_throw( (size_fifo>0), StandardException(LOC, OutOfRangeError, "SimpleFIFO start with zero size buffer"));
253  data.resize( size_fifo );
254  }
255 
263  unsigned int asize()
264  {
265  return size_fifo;
266  }
267 
268  unsigned int size() const
269  {
270  if( leader > follower ) return leader - follower;
271  else if( leader < follower ) return size_fifo-follower+leader;
272  return 0;
273  }
274 
275  void clear()
276  {
277  follower = 0;
278  leader = 0;
279  }
280 
281 //protected:
283  unsigned int size_fifo;
284  int leader;
285  int follower;
286 };
287 
288 } // namespace ecl
289 
290 #endif /* ECL_CONTAINERS_PUSH_AND_POP_DYNAMIC_HPP_ */
std::reverse_iterator< const_iterator > const_reverse_iterator
Pseudo formatter for integral type arrays.
iterator begin() ecl_assert_throw_decl(StandardException)
Embedded control libraries.
std::reverse_iterator< iterator > reverse_iterator
Surpport push and pack operation.
const_reverse_iterator rbegin() const ecl_assert_throw_decl(StandardException)
reverse_iterator rend() ecl_assert_throw_decl(StandardException)
#define LOC
#define ecl_assert_throw(expression, exception)
OutOfRangeError
A simple fifo implementation.
reverse_iterator rbegin() ecl_assert_throw_decl(StandardException)
iterator end() ecl_assert_throw_decl(StandardException)
const_reverse_iterator rend() const ecl_assert_throw_decl(StandardException)
formatters::PushAndPopFormatter< Type, DynamicStorage > Formatter
Formatter for this class.
#define ecl_assert_throw_decl(exception)
void push_back(const Type &datum)
Pushes an element onto the back of the container.
const_iterator begin() const ecl_assert_throw_decl(StandardException)
ecl::Array< Type, Size+1 > data
unsigned int asize()
The size allocated in memory for the fifo.
PushAndPop(const unsigned int length, const Type &d) ecl_assert_throw_decl(StandardException)
const_iterator end() const ecl_assert_throw_decl(StandardException)
PushAndPop(const unsigned int length) ecl_assert_throw_decl(StandardException)
#define ECL_PUBLIC


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:30