stencil/stencil.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11  ** Ifdefs
12  *****************************************************************************/
13 
14 #ifndef ECL_CONTAINERS_STENCIL_STENCIL_HPP_
15 #define ECL_CONTAINERS_STENCIL_STENCIL_HPP_
16 
17 /*****************************************************************************
18  ** Includes
19  *****************************************************************************/
20 
21 #include <cstddef> // size_t
22 #include <ecl/config/macros.hpp>
23 #include <ecl/exceptions/standard_exception.hpp>
24 #include <ecl/concepts/containers.hpp>
25 #include <ecl/concepts/streams.hpp>
26 #include "../initialiser.hpp"
27 #include "formatters.hpp"
28 
29 /*****************************************************************************
30  ** Namespaces
31  *****************************************************************************/
32 
33 namespace ecl
34 {
35 
36 /*****************************************************************************
37  ** Interface
38  *****************************************************************************/
71 template <typename Array>
72 class ECL_PUBLIC Stencil
73 {
74 public:
75  /*********************
76  ** Typedefs
77  **********************/
78  typedef typename Array::value_type value_type;
79  // Looks like I'm now needing to rely on the underlying storage iterator definitions
80  // Naturally, this means I can't do stencils of c arrays, but hey, thats dangerous anyway and we want to avoid that.
81  typedef typename Array::iterator iterator;
82  typedef typename Array::const_iterator const_iterator;
83  typedef typename Array::reference reference;
84  typedef typename Array::const_reference const_reference;
85  // typedef typename Array::value_type* iterator; /**< @brief Uses the array's iterator type. **/
86  // typedef const typename Array::value_type* const_iterator; /**< @brief Uses the array's constant iterator type. **/
87  // typedef typename Array::value_type& reference; /**< @brief Uses the array's element reference type. **/
88  // typedef const typename Array::value_type& const_reference; /**< @brief Uses the array's element const reference type. **/
89  typedef std::size_t size_type;
90  typedef std::ptrdiff_t difference_type;
91  typedef std::reverse_iterator<iterator> reverse_iterator;
92  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
93  typedef formatters::StencilFormatter<value_type,Array> Formatter;
94 
95  /*********************
96  ** C&D's
97  **********************/
113  Stencil(Array& underlying_array, iterator begin_iter, iterator end_iter) ecl_assert_throw_decl(StandardException) :
114  array(underlying_array),
115  b_iter(begin_iter),
116  e_iter(end_iter)
117  {
118  ecl_compile_time_concept_check(ContainerConcept<Array>);
119  ecl_assert_throw((b_iter >= array.begin()) && (b_iter <= array.end()), StandardException(LOC, OutOfRangeError, "Begin iterator out of range."));
120  ecl_assert_throw((e_iter >= array.begin()) && (e_iter <= array.end()), StandardException(LOC, OutOfRangeError, "End iterator out of range."));
121  }
122 
138  Stencil(Array& underlying_array, const unsigned int& start_index = 0, const unsigned int &n = 0) ecl_assert_throw_decl(StandardException) :
139  array(underlying_array),
140  b_iter(array.begin()+start_index),
141  e_iter(array.begin()+start_index+n)
142  {
143  ecl_compile_time_concept_check(ContainerConcept<Array>);
144  ecl_assert_throw((b_iter >= array.begin()) && (b_iter <= array.end()), StandardException(LOC, OutOfRangeError, "Begin iterator out of range."));
145  ecl_assert_throw((e_iter >= array.begin()) && (e_iter <= array.end()), StandardException(LOC, OutOfRangeError, "End iterator out of range."));
146  }
147 
148  virtual ~Stencil()
149  {};
150 
163  Stencil<Array> stencil(const unsigned int& start_index, const unsigned int& n) const ecl_assert_throw_decl(StandardException)
164  {
165  ecl_assert_throw( b_iter+start_index < array.end(), StandardException(LOC, OutOfRangeError, "Start index provided is larger than the underlying stencil size."));
166  ecl_assert_throw( b_iter+start_index+n <= array.end(), StandardException(LOC, OutOfRangeError, "Finish index provided is larger than the underlying stencil size."));
167  return Stencil<Array>(array,b_iter+start_index,b_iter+start_index+n);
168  }
169 
181  void resettle(const unsigned int& start_index, const unsigned int& n) ecl_assert_throw_decl(StandardException)
182  {
183 
184  b_iter = array.begin()+start_index;
185  e_iter = array.begin()+start_index+n;
186  ecl_assert_throw((b_iter >= array.begin()) && (b_iter <= array.end()), StandardException(LOC, OutOfRangeError, "Begin iterator out of range."));
187  ecl_assert_throw((e_iter >= array.begin()) && (e_iter <= array.end()), StandardException(LOC, OutOfRangeError, "End iterator out of range."));
188  }
189 
190  /*********************
191  ** Assignment
192  **********************/
206  containers::BoundedListInitialiser<value_type,value_type*> operator<< (const value_type &value)
207  {
208  return containers::BoundedListInitialiser<value_type,iterator>(value, begin, size());
209  }
240  Stencil<Array>& operator=(const Stencil<Array> &s) ecl_assert_throw_decl(StandardException)
241  {
242  if ( &array == &(s.array) )
243  { // resettle
244  b_iter = s.b_iter;
245  e_iter = s.e_iter;
246  }
247  else
248  { // copy
249  ecl_assert_throw( s.size() <= size(), StandardException(LOC, OutOfRangeError, "Stencil to be copied is larger than this stencil's size."));
250  for ( unsigned int i = 0; i < s.size(); ++i )
251  {
252  *(b_iter+i) = *(s.b_iter+i);
253  }
254  }
255  return *this;
256  }
257 
258  /*********************
259  ** Iterators
260  **********************/
266  iterator begin() ecl_assert_throw_decl(StandardException)
267  {
268  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
269  return b_iter;
270  }
276  const_iterator begin() const ecl_assert_throw_decl(StandardException)
277  {
278  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
279  return b_iter;
280  }
286  iterator end() ecl_assert_throw_decl(StandardException)
287  {
288  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
289  return e_iter;
290  }
296  const_iterator end() const ecl_assert_throw_decl(StandardException)
297  {
298  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
299  return e_iter;
300  }
306  reverse_iterator rbegin() ecl_assert_throw_decl(StandardException)
307  {
308  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
309  return reverse_iterator(end());
310  }
316  const_reverse_iterator rbegin() const ecl_assert_throw_decl(StandardException)
317  {
318  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
319  return const_reverse_iterator(end());
320  }
326  reverse_iterator rend() ecl_assert_throw_decl(StandardException)
327  {
328  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
329  return reverse_iterator(begin());
330  }
336  const_reverse_iterator rend() const ecl_assert_throw_decl(StandardException)
337  {
338  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
339  return const_reverse_iterator(begin());
340  }
341 
342  /*********************
343  ** Front/Back
344  **********************/
350  reference front() ecl_assert_throw_decl(StandardException)
351  {
352  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
353  return *b_iter;
354  }
360  const_reference front() const ecl_assert_throw_decl(StandardException)
361  {
362  ecl_assert_throw( b_iter <= array.end(), StandardException(LOC,OutOfRangeError));
363  return *b_iter;
364  }
370  reference back() ecl_assert_throw_decl(StandardException)
371  {
372  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
373  return *(e_iter-1);
374  }
380  const_reference back() const ecl_assert_throw_decl(StandardException)
381  {
382  ecl_assert_throw( e_iter <= array.end(), StandardException(LOC,OutOfRangeError));
383  return *(e_iter-1);
384  }
385 
386  /*********************
387  ** Accessors
388  **********************/
398  reference operator[](size_type i) ecl_assert_throw_decl(StandardException)
399  {
400  ecl_assert_throw( b_iter+i >= array.begin(), StandardException(LOC,OutOfRangeError));
401  ecl_assert_throw( b_iter+i <= array.end(), StandardException(LOC,OutOfRangeError));
402  return *(b_iter+i);
403  }
415  const_reference operator[](size_type i) const ecl_assert_throw_decl(StandardException)
416  {
417  ecl_assert_throw( b_iter+i >= array.begin(), StandardException(LOC,OutOfRangeError));
418  ecl_assert_throw( b_iter+i <= array.end(), StandardException(LOC,OutOfRangeError));
419  return *(b_iter+i);
420  }
431  reference at(size_type i) throw(StandardException)
432  {
433  if ( b_iter+i <= array.begin() )
434  {
435  throw StandardException(LOC,OutOfRangeError);
436  }
437  if ( b_iter+i >= array.end() )
438  {
439  throw StandardException(LOC,OutOfRangeError);
440  }
441  return *(b_iter+i);
442  }
455  const_reference at(size_type i) const throw(StandardException)
456  {
457  if ( b_iter+i <= array.begin() )
458  {
459  throw StandardException(LOC,OutOfRangeError);
460  }
461  if ( b_iter+i >= array.end() )
462  {
463  throw StandardException(LOC,OutOfRangeError);
464  }
465  return *(b_iter+i);
466  }
467 
468  /*********************
469  ** Utilities
470  **********************/
476  size_type size() const
477  { return e_iter-b_iter;}
478 
479  /*********************
480  ** Streaming
481  **********************/
497  template <typename OutputStream, typename ArrayType>
498  friend OutputStream& operator<<(OutputStream &ostream , const Stencil<ArrayType> &stencil);
499 
500 private:
501  Array& array;
502  iterator b_iter, e_iter;
503 };
504 
505 /*****************************************************************************
506  ** Implementation [Streaming]
507  *****************************************************************************/
508 
509 template<typename OutputStream, typename ArrayType>
510  OutputStream& operator<<(OutputStream &ostream, const Stencil<ArrayType> &stencil)
511  {
512 
514 
515  ostream << "[ ";
516  for (std::size_t i = 0; i < stencil.size(); ++i)
517  {
518  ostream << stencil[i] << " ";
519  }
520  ostream << "]";
521  ostream.flush();
522 
523  return ostream;
524  }
525 
526 } // namespace ecl
527 
528 #endif /* ECL_CONTAINERS_STENCIL_STENCIL_HPP_ */
const Type * const_iterator
Defines validating functionality for the streams concept.
#define LOC
Stringify the line of code you are at.
OutputStream & operator<<(OutputStream &ostream, const Void void_object)
Output stream operator for Void objects.
Definition: void.hpp:76
Type * iterator
#define ecl_assert_throw(expression, exception)
Debug mode throw with a logical condition check.
#define ECL_PUBLIC
OutOfRangeError
const Type & const_reference
ecl_geometry_PUBLIC int size(const Trajectory2D &trajectory)
#define ecl_assert_throw_decl(exception)
Assure throw exception declaration.
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
MatrixFormatter< Derived, Scalar > Formatter
Definition: plugin.hpp:28
Type & reference
Type value_type


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:38