KADATH
index.hpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef __INDEX_HPP_
21 #define __INDEX_HPP_
22 
23 
24 #include "headcpp.hpp"
25 #include "dim_array.hpp"
26 
27 namespace Kadath {
28 
29 class Tensor;
30 
38 class Index : public Memory_mapped {
39 public:
41  using Data_type = Memory_mapped_array<int>;
42 
43 protected:
50 
51 
52 public:
57  explicit Index (const Dim_array& dim) : sizes{dim}, coord{dim.get_ndim()}
58  {for(unsigned i{0u};i<dim.get_ndim();i++) coord[i]=0;}
63  Index (const Tensor& so) ;
64 
66  void swap(Index & so) {sizes.swap(so.sizes); coord.swap(so.coord);}
67 
72  int& set(int i) {assert(i>=0); assert(i<get_ndim()); return coord[i] ;}
77  int operator() (int i) const {assert(i>=0); assert(i<get_ndim()); return coord[i]; }
81  int get_ndim() const {return sizes.get_ndim() ;} ;
85  Dim_array const& get_sizes() const {return sizes ;} ;
86 
88  void set_start()
89  {
90  for (int i=0 ; i<get_ndim() ; i++) coord[i] = 0 ;
91  }
99  bool inc (int increm, int var=0) {
100  int const ndimm1{get_ndim()-1};
101  if((var >=0) && (var<=ndimm1)) {
102  coord[var] += increm ;
103  div_t division {div(coord[var],sizes(var))};
104  for (int i=var ; i<ndimm1 && division.quot>0; i++) {
105  division = div(coord[i], sizes(i)) ;
106  coord[i] = division.rem ;
107  coord[i+1] += division.quot ;
108  }
109  return (coord[ndimm1] < sizes(ndimm1));
110  }
111  else return false;
112  }
114  bool inc1(int var) {
115  int const ndimm1{get_ndim()-1};
116  int i{var};
117  while(i<= ndimm1 && coord[i]==(sizes(i)-1))
118  {
119  coord[i] = 0;
120  i++;
121  }
122  if(i>=(ndimm1+1)) { return false;}
123  else {
124  coord[i]++;
125  return true;
126  }
127  }
128 
130  bool inc() {return inc1(0);}
131 
132 
134  bool inc_vanilla (int increm, int var) {
135  bool res = ((var >=0) && (var<get_ndim())) ? true : false ;
136  if (res) {
137  coord[var] += increm ;
138  for (int i=var ; i<get_ndim()-1 ; i++) {
139  div_t division = div(coord[i], sizes(i)) ;
140  coord[i] = division.rem ;
141  coord[i+1] += division.quot ;
142  }
143  res = (coord[get_ndim()-1] >= sizes(get_ndim()-1)) ? false : true ;
144  }
145  return res ;
146  }
147 
152  void operator=(const Index& so) {assert(sizes==so.sizes);for(int i=0 ;i<get_ndim();i++)coord[i] = so.coord[i];}
153 
155  bool operator== (const Index& xx) const {
156  bool res = (get_ndim()==xx.get_ndim()) ;
157  if (res)
158  for (int i=0 ; i<get_ndim() && res; i++)
159  res = (xx.coord[i] == coord[i]);
160  return res ;
161  }
162 
163  template <class> friend class Array ;
165  friend ostream& operator<< (ostream&, const Index&) ;
166 } ;
167 }
168 #endif
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
void swap(Dim_array &so)
Sylvain's stuff.
Definition: dim_array.hpp:70
Class that gives the position inside a multi-dimensional Array.
Definition: index.hpp:38
int operator()(int i) const
Read/write of the position in a given dimension.
Definition: index.hpp:77
void swap(Index &so)
Sylvain's stuff.
Definition: index.hpp:66
Memory_mapped_array< int > Data_type
Type of data.
Definition: index.hpp:41
Dim_array sizes
Sizes of the associated Array.
Definition: index.hpp:48
int & set(int i)
Read/write of the position in a given dimension.
Definition: index.hpp:72
void set_start()
Sets the position to zero in all dimensions.
Definition: index.hpp:88
bool inc()
Increment on the first dimension.
Definition: index.hpp:130
bool inc_vanilla(int increm, int var)
General increment.
Definition: index.hpp:134
void operator=(const Index &so)
Assignment operator.
Definition: index.hpp:152
friend ostream & operator<<(ostream &, const Index &)
Operator<<.
Definition: index.cpp:36
bool inc(int increm, int var=0)
Increments the position of the Index.
Definition: index.hpp:99
Data_type coord
Value of each index.
Definition: index.hpp:49
Dim_array const & get_sizes() const
Returns all the dimensions.
Definition: index.hpp:85
int get_ndim() const
Returns the number of dimensions.
Definition: index.hpp:81
bool inc1(int var)
Increment on one dimension.
Definition: index.hpp:114
Index(const Dim_array &dim)
Standard constructor.
Definition: index.hpp:57
bool operator==(const Index &xx) const
Comparison operator.
Definition: index.hpp:155
Tensor handling.
Definition: tensor.hpp:149