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>
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;
83  typedef typename Array::reference 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;
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  {
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  {
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  **********************/
207  {
208  return containers::BoundedListInitialiser<value_type,iterator>(value, begin, size());
209  }
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  **********************/
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  {
436  }
437  if ( b_iter+i >= array.end() )
438  {
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  {
460  }
461  if ( b_iter+i >= array.end() )
462  {
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:
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_ */
Fixed size container with a few bells and whistles.
const Type * const_iterator
Embedded control libraries.
reference back() ecl_assert_throw_decl(StandardException)
const_iterator end() const ecl_assert_throw_decl(StandardException)
#define LOC
std::reverse_iterator< iterator > reverse_iterator
Uses the array&#39;s reverse iterator type.
formatters::StencilFormatter< value_type, Array > Formatter
const_reference at(size_type i) const
Array::iterator iterator
Uses the array&#39;s iterator type.
std::reverse_iterator< const_iterator > const_reverse_iterator
Uses the array&#39;s constant reverse iterator type.
Stencil(Array &underlying_array, const unsigned int &start_index=0, const unsigned int &n=0) ecl_assert_throw_decl(StandardException)
Initialises with a reference to the underlying container and boundary constraints.
reference front() ecl_assert_throw_decl(StandardException)
void resettle(const unsigned int &start_index, const unsigned int &n) ecl_assert_throw_decl(StandardException)
Resettle the stencil on a different range over the same underlying array.
Array::reference reference
Uses the array&#39;s element reference type.
#define ecl_assert_throw(expression, exception)
std::size_t size_type
Uses the array&#39;s type used to denote the length of the array.
virtual ~Stencil()
const_reference operator[](size_type i) const ecl_assert_throw_decl(StandardException)
OutOfRangeError
const Type & const_reference
std::ptrdiff_t difference_type
Stencil< Array > & operator=(const Stencil< Array > &s) ecl_assert_throw_decl(StandardException)
This either resettles this stencil or copies across to it (depending on the rhs stencil).
Array::const_reference const_reference
Uses the array&#39;s element const reference type.
iterator end() ecl_assert_throw_decl(StandardException)
const_reference back() const ecl_assert_throw_decl(StandardException)
Convenience initialiser with bounds checking for fixed size containers.
Definition: initialiser.hpp:49
const_reverse_iterator rbegin() const ecl_assert_throw_decl(StandardException)
#define ecl_assert_throw_decl(exception)
Text formatters for streaming stencil classes.
Stencil< Array > stencil(const unsigned int &start_index, const unsigned int &n) const ecl_assert_throw_decl(StandardException)
Generate a sub-stencil.
A safe windowing class that opens onto array-like containers.
#define ecl_compile_time_concept_check(Model)
Array::value_type value_type
Uses the array&#39;s element type.
OutputStream & operator<<(OutputStream &ostream, Format< std::string > &formatter) ecl_assert_throw_decl(StandardException)
iterator begin() ecl_assert_throw_decl(StandardException)
reference at(size_type i)
Pseudo formatter for stencils.
const_iterator begin() const ecl_assert_throw_decl(StandardException)
const_reference front() const ecl_assert_throw_decl(StandardException)
reverse_iterator rend() ecl_assert_throw_decl(StandardException)
reference operator[](size_type i) ecl_assert_throw_decl(StandardException)
Stencil(Array &underlying_array, iterator begin_iter, iterator end_iter) ecl_assert_throw_decl(StandardException)
Initialises with a reference to the underlying container and boundary constraints.
Array::const_iterator const_iterator
Uses the array&#39;s constant iterator type.
const_reverse_iterator rend() const ecl_assert_throw_decl(StandardException)
#define ECL_PUBLIC
size_type size() const
reverse_iterator rbegin() ecl_assert_throw_decl(StandardException)


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