Imagine++
Coords.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 namespace Imagine {
10 
11 
16  template <int dim> class Coords : public FVector<int,dim> {
17  private:
18  typedef FVector<int,dim> Base;
19  public:
25  Coords() : Base() {}
32  Coords(const Base& v) : Base(v) {}
40  Coords(const int v[dim]): Base(v) {}
47  Coords(int v) : Base(v) {}
55  Coords(int x,int y) : Base(x,y) {}
64  Coords(int x,int y,int z) : Base(x,y,z) {}
71  size_t prod() const {
72  size_t p=1;
73  for (int i=0;i<dim;i++)
74  p*=(*this)[i];
75  return p;
76  }
77  };
78 
83  template <int dim> class CoordsIterator : public std::iterator<std::input_iterator_tag, Coords<dim> > {
84  protected:
92  bool _stop;
93  public:
100  CoordsIterator() : _stop(true) {}
107  CoordsIterator(const Coords<dim>& a, const Coords<dim>& b) : _a(a), _b(b), _pos(a), _stop(false) {}
116  _a = it._a;
117  _b = it._b;
118  _pos = it._pos;
119  _stop = it._stop;
120  return *this;
121  }
131  bool operator == (const CoordsIterator& it) const {
132  return (_stop ? it._stop : !it._stop && _pos == it._pos);
133  }
141  bool operator != (const CoordsIterator& it) const {
142  return (_stop ? !it._stop : it._stop || _pos != it._pos);
143  }
151  CoordsIterator& operator ++ () {
152  for (int i=0;i<dim;i++) {
153  if (_pos[i]!=_b[i]) {
154  _pos[i]++;
155  return *this;
156  }
157  _pos[i]=_a[i];
158  }
159  _stop = true;
160  return *this;
161  }
169  CoordsIterator operator ++(int) {
170  CoordsIterator tmp(*this);
171  ++(*this);
172  return tmp;
173  }
182  return _pos;
183  }
191  const Coords<dim>* operator -> () const {
192  return &_pos;
193  }
194  };
195 
196 
198 }
CoordsIterator(const Coords< dim > &a, const Coords< dim > &b)
Constructor.
Definition: Coords.h:107
Coordinates.
Definition: Coords.h:16
size_t prod() const
Product of coordinates.
Definition: Coords.h:71
Vector of fixed size.
Definition: FVector.h:17
Coords(int v)
Constructor with constant value.
Definition: Coords.h:47
const int & z() const
Read alias.
Definition: FVector.h:132
int operator*(const FVector &v) const
Scalar product.
Definition: FVector.h:259
Coords(int x, int y)
2D alias.
Definition: Coords.h:55
Coords< dim > _b
end
Definition: Coords.h:88
CoordsIterator()
Empty constructor.
Definition: Coords.h:100
Iterator on Coordinates.
Definition: Coords.h:83
const int & x() const
Read alias.
Definition: FVector.h:104
bool operator!=(const FArray &b) const
Inequality test.
Definition: FArray.h:241
Coords()
Empty constructor.
Definition: Coords.h:25
Coords< dim > _pos
Current position.
Definition: Coords.h:90
Coords< dim > _a
begin
Definition: Coords.h:86
FVector & operator=(const FArray< T2, dim > &b)
Assignment.
Definition: FVector.h:97
Coords(int x, int y, int z)
3D alias.
Definition: Coords.h:64
Coords(const Base &v)
Copy constructor Constructs from anit Coords.
Definition: Coords.h:32
Coords(const int v[dim])
Constructor from C array.
Definition: Coords.h:40
bool _stop
end reached?
Definition: Coords.h:92
bool operator==(const FArray &b) const
Equality test.
Definition: FArray.h:231
const int & y() const
Read alias.
Definition: FVector.h:118
Imagine++ namespace.
Definition: Array.h:7