KrisLibrary  1.0.0
utils/indexing.h
1 #ifndef INDEXING_H
2 #define INDEXING_H
3 
4 #include <vector>
5 #include <list>
6 #include <set>
7 #include "IntPair.h"
8 #include "IntTriple.h"
9 #include "IntTuple.h"
10 
13 int IncrementIndex(std::vector<int>& i,const std::vector<int>& imin,const std::vector<int>& imax);
16 int IncrementIndex(std::vector<int>& i,const std::vector<int>& imax);
17 
20 template <class T,class IterT,class MapT>
21 struct MapIterator : public IterT
22 {
24 
25  explicit MapIterator(MapT& _mapping):mapping(_mapping) {}
26  explicit MapIterator(IterT& _iter,MapT& _mapping):IterT(_iter),mapping(_mapping) {}
27  inline T operator*() { return mapping(IterT::operator *()); }
28  inline bool operator == (const MyT& rhs) const { return &mapping==&rhs.mapping && IterT::operator==(rhs); }
29  inline bool operator != (const MyT& rhs) const { return !operator==(rhs); }
30  inline bool operator < (const MyT& rhs) const {
31  if(&mapping!=&rhs.mapping) return true;
32  return IterT::operator < (rhs);
33  }
34 
35  MapT& mapping;
36 };
37 
46 {
47  RangeIndices();
48  RangeIndices(const RangeIndices& rhs);
49  explicit RangeIndices(int max); //range from 0 to max-1
50  explicit RangeIndices(int min,int max); //range from min to max-1
51  explicit RangeIndices(int min,int max,int stride); //range from min to max-1, skipping stride
52  void enumerate(std::vector<int>& indices);
53  int operator [] (int i) const { return start+stride*i; }
54  bool contains(int index) const;
55  int indexToElement(int index) const;
56  bool operator == (const RangeIndices& range) const;
57  inline bool operator != (const RangeIndices& range) const { return !operator==(range); }
58 
59  struct iterator
60  {
61  iterator();
62  explicit iterator(const RangeIndices* range);
63  explicit iterator(const RangeIndices*, int invalid);
64  iterator(const iterator& rhs);
65  inline int operator*() const { return index; }
66  inline int getElement() const { return i; }
67  void setElement(int i);
68  iterator& operator ++ ();
69  iterator& operator -- ();
70  iterator& operator += (int skip);
71  iterator& operator -= (int skip);
72  bool isInvalid() const;
73  bool operator == (const iterator& rhs) const;
74  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
75  bool operator < (const iterator& rhs) const;
76 
77  //parameters
78  const RangeIndices *range;
79  //temp
80  int i,index;
81  };
82 
83  iterator begin() const { return iterator(this); }
84  iterator end() const { return iterator(this,-1); }
85 
86  int start,size,stride;
87 };
88 
89 /*
90 //Note: Use IntTuple instead of this
91 struct Indices : public std::vector<int>
92 {
93  typedef std::vector<int> ParentT;
94 
95  inline Indices() {}
96  inline Indices(const ParentT& rhs) :ParentT(rhs) {}
97  inline Indices(int element) :ParentT(1,element) {}
98  inline Indices(int e1,int e2) :ParentT(2)
99  { operator[](0)=e1; operator[](1)=e2; }
100  inline Indices(int e1,int e2,int e3) :ParentT(3)
101  { operator[](0)=e1; operator[](1)=e2; operator[](2)=e3; }
102  inline Indices(const std::list<int>& rhs):ParentT(rhs.size())
103  { std::copy(rhs.begin(),rhs.end(),begin()); }
104  inline Indices(const std::set<int>& rhs):ParentT(rhs.size())
105  { std::copy(rhs.begin(),rhs.end(),begin()); }
106  inline Indices(const RangeIndices& indices) { indices.enumerate(*this); }
107 };
108 */
109 
117 {
118  Range2Indices();
119  Range2Indices(const Range2Indices& rhs);
120  Range2Indices(const RangeIndices& irange,const RangeIndices& jrange);
121  Range2Indices(int imax,int jmax); //range [0,imax)x[0,jmax)
122  Range2Indices(int imin,int imax,int jmin,int jmax); //range [imin,imax)x[jmin,jmax)
123  //range [imin,imax)x[jmin,jmax) with strides istride,jstride
124  Range2Indices(int imin,int imax,int istride,int jmin,int jmax,int jstride);
125  void enumerate(std::vector<IntPair>& indices);
126  inline IntPair operator () (int i,int j) const { return IntPair(irange[i],jrange[j]); }
127  inline IntPair operator () (const IntPair& t) const { return IntPair(irange[t.a],jrange[t.b]); }
128  inline bool contains(int iindex,int jindex) const { return irange.contains(iindex) && jrange.contains(jindex); }
129  inline bool contains(const IntPair& t) const { return contains(t.a,t.b); }
130  inline IntPair indexToElement(int iindex,int jindex) const { return IntPair(irange.indexToElement(iindex),jrange.indexToElement(jindex)); }
131 
132  struct iterator
133  {
134  iterator();
135  iterator(const RangeIndices& irange,const RangeIndices& jrange);
136  iterator(const RangeIndices& irange,const RangeIndices& jrange,int invalid);
137  iterator(const iterator& rhs);
138  inline IntPair operator*() { return IntPair(*i,*j); }
139  inline int getElement() const { return element; }
140  void setElement(int k);
141  void setElement(int i,int j);
142  void setElement(const IntPair& t) { setElement(t.a,t.b); }
143  iterator& operator ++ ();
144  iterator& operator -- ();
145  iterator& operator += (int skip);
146  iterator& operator -= (int skip);
147  inline int getFirst() const { return *i; }
148  inline int getSecond() const { return *j; }
149  inline int getFirstElement() const { return i.getElement(); }
150  inline int getSecondElement() const { return j.getElement(); }
151  inline void incFirst(int skip=1) { i+=skip; }
152  inline void incSecond(int skip=1) { j+=skip; }
153  inline bool isInvalid() const { return i.isInvalid() || j.isInvalid(); }
154  inline bool operator == (const iterator& rhs) const { return i==rhs.i && j==rhs.j; }
155  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
156  bool operator < (const iterator& rhs) const;
157 
158  //temp
160  int element; //the absolute index
161  };
162 
163  iterator begin() const { return iterator(irange,jrange); }
164  iterator end() const { return iterator(irange,jrange,-1); }
165 
166  RangeIndices irange,jrange;
167 };
168 
176 {
177  Range3Indices();
178  Range3Indices(const Range3Indices& rhs);
179  Range3Indices(const RangeIndices& irange,const RangeIndices& jrange,const RangeIndices& krange);
180  Range3Indices(int imax,int jmax,int kmax); //range [0,imax)x[0,jmax)x[0,kmax)
181  Range3Indices(int imin,int imax,int jmin,int jmax,int kmin,int kmax); //range [imin,imax)x[jmin,jmax)x[kmin,kmax)
182  void enumerate(std::vector<IntTriple>& indices);
183  inline IntTriple operator () (int i,int j,int k) const
184  { return IntTriple(irange[i],jrange[j],krange[k]); }
185  inline IntTriple operator () (const IntTriple& t) const
186  { return IntTriple(irange[t.a],jrange[t.b],krange[t.c]); }
187  inline bool contains(int iindex,int jindex,int kindex) const { return irange.contains(iindex) && jrange.contains(jindex) && krange.contains(kindex); }
188  inline bool contains(const IntTriple& t) const
189  { return contains(t.a,t.b,t.c); }
190  inline IntTriple indexToElement(int iindex,int jindex,int kindex) const {
191  return IntTriple(irange.indexToElement(iindex),
192  jrange.indexToElement(jindex),
193  krange.indexToElement(kindex)); }
194 
195  struct iterator
196  {
197  iterator();
198  iterator(const RangeIndices& irange,const RangeIndices& jrange,const RangeIndices& krange);
199  iterator(const RangeIndices& irange,const RangeIndices& jrange,const RangeIndices& krange,int invalid);
200  iterator(const iterator& rhs);
201  inline IntTriple operator*() { return IntTriple(*i,*j,*k); }
202  inline int getElement() const { return element; }
203  void setElement(int m);
204  void setElement(int i,int j,int k);
205  void setElement(const IntTriple& t) { setElement(t.a,t.b,t.c); }
206  iterator& operator ++ ();
207  iterator& operator -- ();
208  iterator& operator += (int skip);
209  iterator& operator -= (int skip);
210  inline int getFirst() const { return *i; }
211  inline int getSecond() const { return *j; }
212  inline int getThird() const { return *k; }
213  inline int getFirstElement() const { return i.getElement(); }
214  inline int getSecondElement() const { return j.getElement(); }
215  inline int getThirdElement() const { return k.getElement(); }
216  inline void incFirst(int skip=1) { i+=skip; }
217  inline void incSecond(int skip=1) { j+=skip; }
218  inline void incThird(int skip=1) { k+=skip; }
219  inline bool isInvalid() const { return i.isInvalid() || j.isInvalid() || k.isInvalid(); }
220  inline bool operator == (const iterator& rhs) const { return i==rhs.i && j==rhs.j && k==rhs.k; }
221  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
222  bool operator < (const iterator& rhs) const;
223 
224  //temp
226  int element; //the absolute index
227  };
228 
229  iterator begin() const { return iterator(irange,jrange,krange); }
230  iterator end() const { return iterator(irange,jrange,krange,-1); }
231 
232  RangeIndices irange,jrange,krange;
233 };
234 
242 {
243  Stripe2Indices();
244  Stripe2Indices(const Stripe2Indices& rhs);
245  Stripe2Indices(int isize,int jsize,int base=0,int istride=1,int jstride=1);
246  Stripe2Indices(const Range2Indices& rhs); //assigns a row-major ordering
247  Stripe2Indices(int isize,int jsize,const Range2Indices& subRange); //assigns a row-major ordering
248  Stripe2Indices(const RangeIndices& irange,const RangeIndices& jrange);
249  void enumerate(std::vector<int>& indices);
250  inline int operator () (int i,int j) const { return base+i*istride+j*jstride; }
251  inline int operator () (const IntPair& t) const { return operator()(t.a,t.b); }
252  bool contains(int index) const;
253  IntPair indexToElement(int index) const;
254  void setRange(const Stripe2Indices& stripe,const Range2Indices& inds);
255  bool operator == (const Stripe2Indices& stripe) const;
256  inline bool operator != (const Stripe2Indices& stripe) const { return !operator==(stripe); }
257 
258  struct iterator
259  {
260  explicit iterator(const Stripe2Indices*);
261  explicit iterator(const Stripe2Indices*,int invalid);
262  iterator(const iterator& i);
263  inline int operator*() const { return index; }
264  inline IntPair getElement() const { return IntPair(i,j); }
265  iterator& operator ++();
266  iterator& operator --();
267  iterator& operator +=(int skip);
268  iterator& operator -=(int skip);
269  void incFirst(int skip=1);
270  void incSecond(int skip=1);
271  bool isInvalid() const;
272  bool operator == (const iterator& rhs) const;
273  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
274  bool operator < (const iterator& rhs) const;
275 
276  const Stripe2Indices* stripe;
277  int i,j;
278  int index,stripeIndex;
279  };
280 
281  iterator begin() const { return iterator(this); }
282  iterator end() const { return iterator(this,-1); }
283 
284  int base;
285  int isize,jsize;
286  int istride,jstride;
287 };
288 
296 {
297  Stripe3Indices();
298  Stripe3Indices(const Stripe3Indices& rhs);
299  Stripe3Indices(int isize,int jsize,int ksize,int base=0,int istride=1,int jstride=1,int kstride=1);
300  explicit Stripe3Indices(const Range3Indices& rhs); //assigns a row-major ordering
301  Stripe3Indices(int isize,int jsize,int ksize,const Range3Indices& subRange); //assigns a row-major ordering
302  Stripe3Indices(const RangeIndices& irange,const RangeIndices& jrange,const RangeIndices& krange);
303  void enumerate(std::vector<int>& indices);
304  inline int operator () (int i,int j,int k) const { return base+i*istride+j*jstride+k*kstride; }
305  inline int operator () (const IntTriple& t) const { return operator()(t.a,t.b,t.c); }
306  bool contains(int index) const;
307  IntTriple indexToElement(int index) const;
308  void setRange(const Stripe3Indices& stripe,const Range3Indices& inds);
309  bool operator == (const Stripe3Indices& stripe) const;
310  inline bool operator != (const Stripe3Indices& stripe) const { return !operator==(stripe); }
311 
312  struct iterator
313  {
314  explicit iterator(const Stripe3Indices*);
315  explicit iterator(const Stripe3Indices*,int invalid);
316  iterator(const iterator& i);
317  inline int operator*() const { return index; }
318  inline IntTriple getElement() const { return IntTriple(i,j,k); }
319  iterator& operator ++();
320  iterator& operator --();
321  iterator& operator +=(int skip);
322  iterator& operator -=(int skip);
323  void incFirst(int skip=1);
324  void incSecond(int skip=1);
325  void incThird(int skip=1);
326  bool isInvalid() const;
327  bool operator == (const iterator& rhs) const;
328  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
329  bool operator < (const iterator& rhs) const;
330 
331  const Stripe3Indices* stripe;
332  int i,j,k;
333  int index,firstIndex,secondIndex;
334  };
335 
336  iterator begin() const { return iterator(this); }
337  iterator end() const { return iterator(this,-1); }
338 
339  int base;
340  int isize,jsize,ksize;
341  int istride,jstride,kstride;
342 };
343 
351 {
352  void enumerate(std::vector<IntPair>& indices);
353  inline IntPair operator () (int i,int j) const { return IntPair(iindices[i],jindices[j]); }
354  inline IntPair operator () (const IntPair& t) const { return operator() (t.a,t.b); }
355  inline bool contains(int iindex,int jindex) const { return iindices.contains(iindex) && jindices.contains(jindex); }
356 
358  {
360  iterator(const Grid2Indices* grid);
361  iterator(const Grid2Indices* grid,int invalid);
362  iterator(const Grid2Indices* grid,const Range2Indices& range);
363  iterator(const Grid2Indices* grid,const Range2Indices& range,int invalid);
364  inline IntPair operator*() {
365  IntPair i=ParentT::operator *();
366  return (*grid)(i);
367  }
368  inline int getFirst() const { return grid->iindices[ParentT::getFirst()]; }
369  inline int getSecond() const { return grid->jindices[ParentT::getSecond()]; }
370  inline bool operator == (const iterator& rhs) const { return grid==rhs.grid && ParentT::operator==(rhs); }
371  inline bool operator != (const iterator& rhs) const { return !operator==(rhs); }
372  inline bool operator < (const iterator& rhs) const { return ParentT::operator < (rhs); }
373 
374  const Grid2Indices* grid;
375  Range2Indices range;
377  };
378 
379  iterator begin() const { return iterator(this); }
380  iterator end() const { return iterator(this,-1); }
381  iterator beginRange(const Range2Indices& r) const { return iterator(this,r); }
382  iterator endRange(const Range2Indices& r) const { return iterator(this,r,-1); }
383 
384  IntTuple iindices,jindices;
385 };
386 
387 #endif
A lightweight integer 3-tuple class.
Definition: IntTriple.h:9
A regular range of indices ik = start+k*stride, for k=0 to size-1.
Definition: utils/indexing.h:45
Definition: utils/indexing.h:132
A 2D lattice of irregular ranges.
Definition: utils/indexing.h:350
Definition: utils/indexing.h:357
A lightweight integer 2-tuple class.
Definition: IntPair.h:9
A 3D lattice of regular ranges.
Definition: utils/indexing.h:175
An integer tuple class.
Definition: IntTuple.h:14
Definition: utils/indexing.h:21
Definition: utils/indexing.h:312
Definition: utils/indexing.h:258
A 2D lattice of regular ranges.
Definition: utils/indexing.h:116
Definition: utils/indexing.h:195
Definition: utils/indexing.h:59
A 2D lattice indexing into a linear sequence, in a striping pattern.
Definition: utils/indexing.h:241
A 3D lattice indexing into a linear sequence, in a striping pattern.
Definition: utils/indexing.h:295