KADATH
array_iterator.hpp
1 //
2 // Created by sauliac on 20/03/2020.
3 //
4 
5 #ifndef __ARRAY_ITERATOR_HPP_
6 #define __ARRAY_ITERATOR_HPP_
7 
8 #include <vector>
9 
10 #include "headcpp.hpp"
11 #include "dim_array.hpp"
12 
13 namespace Kadath {
14 
18  class Array_iterator : public Memory_mapped
19  {
20  protected:
27  Memory_mapped_array<int> steps;
28  int position;
29 
30  public:
32  explicit Array_iterator(int ndim, int tndim) : sizes{ndim}, steps(ndim + 1), position{0}
33  {steps.front()=1; for(int i=0;i<ndim;i++) {steps[i+1] = steps[i]*tndim; sizes.set(i) = tndim;}}
38  explicit Array_iterator (const Dim_array& dim) : sizes{dim}, steps(dim.get_ndim() + 1), position{0}
39  {steps.front()=1; for(int i=0;i<dim.get_ndim();i++) steps[i+1] = steps[i]*sizes(i);}
40 
41  //Copy constructor and assignment operator, move constructor and assignment as well as the destructor are the
42  // default ones generated by the compiler.
46  int get_ndim() const {return sizes.get_ndim() ;} ;
48  int get_position() const {return position;}
49 
51  bool operator==(Array_iterator const & xx) const {return (position == xx.position && get_ndim() == xx.get_ndim());}
53  Array_iterator & set_value(int _position) { position = _position; return *this;}
59  Array_iterator & set(Array_iterator const &_so) {position = _so.position; return *this;}
61  bool check_value();
63  void set_start() { position = 0;}
67  Dim_array const& get_sizes() const {return sizes ;} ;
75  bool inc (int increm, int var=0) { position += increm * steps[var]; return check_value();}
81  bool inc1(int var) { position += steps[var]; return check_value();}
86  bool inc() {position++; return check_value(); }
87  template <class> friend class Array ;
88  };
89 
91  if(position >= steps.back()) {
92  position = steps.back() - 1;
93  return false;
94  }
95  else return true;
96  }
97 
98 }
99 
100 #endif //__ARRAY_ITERATOR_HPP_
Version of the Index class optimized for incremental access to Array components.
bool check_value()
Sylvain's stuff.
Dim_array const & get_sizes() const
Returns all the dimensions.
Array_iterator(const Dim_array &dim)
Standard constructor.
int get_ndim() const
Returns the number of dimensions.
void set_start()
Sets the iterator at the start.
bool inc(int increm, int var=0)
Increments the position of the Array_iterator.
int get_position() const
Accessor for the value member.
Array_iterator & set(Array_iterator const &_so)
Set the position of the iterator at the same as the one of the source.
Memory_mapped_array< int > steps
The incremental index step with respect to each dimensions.
int position
Corresponding value for 1D indexing .
bool inc()
Optimized unit increment.
Array_iterator(int ndim, int tndim)
Constructor.
Dim_array sizes
Sizes of the associated Array.
bool operator==(Array_iterator const &xx) const
Comparison operator.
bool inc1(int var)
Optimized unit increment with respect to the passed index number.
Array_iterator & set_value(int _position)
Set position.
Template class for arrays.
Definition: array.hpp:86
Class for storing the dimensions of an array.
Definition: dim_array.hpp:34
int get_ndim() const
Returns the number of dimensions.
Definition: dim_array.hpp:63
int & set(int i)
Read/write of the size of a given dimension.
Definition: dim_array.hpp:54