Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
array2d.h
Go to the documentation of this file.
1 #ifndef ARRAY2D_H
2 #define ARRAY2D_H
3 
4 #include <limits.h>
5 #include <memory.h>
6 #include <QtGlobal>
7 
8 namespace frechet { namespace data {
9 
18 template<class T>
19 class Array2D
20 {
21 protected:
22  T* d;
23 public:
25  const int n;
27  const int m;
28 
32  Array2D(int an=0, int am=0);
35  Array2D(const Array2D& that);
38  Array2D(Array2D&& that);
40  ~Array2D();
44  Array2D& operator= (const Array2D& that);
48  Array2D& operator= (Array2D&& that);
49 
54  T& at (int i, int j) { return d[offset(i,j)]; }
59  const T& at (int i, int j) const { return d[offset(i,j)]; }
60 
64  T& at (int offset) {
65  Q_ASSERT(offset>=0 && offset < n*m);
66  return d[offset];
67  }
68 
72  const T& at (int offset) const {
73  Q_ASSERT(offset>=0 && offset < n*m);
74  return d[offset];
75  }
76 
80  T& operator[] (int offset) { return at(offset); }
84  const T& operator[] (int offset) const { return at(offset); }
85 
95  class iterator {
96  public:
98  iterator() : parent(NULL), _offset(0) {}
102  iterator& operator= (const iterator& that) {
103  parent = that.parent;
104  _offset = that._offset;
105  return *this;
106  }
110  bool operator== (const iterator& that) const {
111  return (parent==that.parent) && (_offset==that._offset);
112  }
116  bool operator!= (const iterator& that) const {
117  return ! operator==(that);
118  }
120  bool valid() const { return valid(_offset); }
123  bool valid(int off) const { return (off >= 0) && (off < parent->n * parent->m); }
124 
126  int offset() const { return _offset; }
128  int i() const { return _offset / parent->m; }
130  int j() const { return _offset % parent->m; }
131 
133  T& operator* () { return parent->at(_offset); }
135  T* operator-> () { return & parent->at(_offset); }
136 
139  iterator& operator++ (/*pre-increment*/) { _offset++; return *this; }
142  iterator operator++ (int/*post-increment*/) { iterator it=*this; _offset++; return it; }
143 
146  iterator& operator-- (/*pre-increment*/) { _offset--; return *this; }
149  iterator operator-- (int/*post-increment*/) { iterator it=*this; _offset--; return it; }
150 
153  iterator& up() { _offset++; return *this; }
156  iterator& down() { _offset--; return *this; }
159  iterator& right() { _offset += parent->m; return *this; }
162  iterator& left() { _offset -= parent->m; return *this; }
163 
168  iterator& to(int i, int j) { to(i*parent->m + j); }
172  iterator& to(int off) { _offset = off; }
173 
176  if (i() > 0)
177  return &parent->at(_offset-parent->m);
178  else
179  return nullptr;
180  }
183  if (i()+1 < parent->n)
184  return &parent->at(_offset+parent->m);
185  else
186  return nullptr;
187  }
189  T* topNeighbor() {
190  if (j()+1 < parent->m)
191  return &parent->at(_offset+1);
192  else
193  return nullptr;
194  }
197  if (j() > 0)
198  return &parent->at(_offset-parent->m);
199  else
200  return nullptr;
201  }
202 
203  protected:
208  iterator(Array2D* aparent, int start) : parent(aparent), _offset(start) { }
212  int _offset;
213  };
215  iterator begin() { return iterator(this,0); }
217  iterator end() { return iterator(this,n*m); }
218 
219 public:
226  size_t offset(int i, int j) const {
227  Q_ASSERT(i>=0 && i < n && j>=0 && j < m);
228  return i*m+j;
229  }
236  void indices(int off, int* i, int* j) const {
237  Q_ASSERT(off>=0 && off<n*m);
238  *i = off/m;
239  *j = off%m;
240  }
247  static void copy(T* dst, T* src, int count);
248 };
249 
250 } } // namespace
251 
252 #include <array2d_impl.h>
253 
254 #endif // ARRAY2D_H
T * d
array data
Definition: array2d.h:22
T & operator[](int offset)
array accessor
Definition: array2d.h:80
int _offset
current offset
Definition: array2d.h:212
bool operator==(const iterator &that) const
equality comparator
Definition: array2d.h:110
iterator & left()
move index one column to the left (decrement)
Definition: array2d.h:162
static void copy(T *dst, T *src, int count)
copy a number of entries
Definition: array2d_impl.h:53
bool operator!=(const iterator &that) const
non-equality comparator
Definition: array2d.h:116
global definitions for all algorithms.
T & at(int offset)
array accessor
Definition: array2d.h:64
Array2D & operator=(const Array2D &that)
assignment operator
Definition: array2d_impl.h:7
iterator begin()
Definition: array2d.h:215
iterator & operator=(const iterator &that)
Definition: array2d.h:102
iterator & operator++()
pre-increment
Definition: array2d.h:139
iterator & to(int off)
move iterator to a given linear offset
Definition: array2d.h:172
Array2D * parent
reference to paran
Definition: array2d.h:210
iterator & up()
move index one row up (increment)
Definition: array2d.h:153
~Array2D()
destructor; releases data
Definition: array2d_impl.h:47
Array2D(int an=0, int am=0)
default constructor
Definition: array2d_impl.h:27
an Array2D iterator
Definition: array2d.h:95
A simple two-dimensional array of fixed size.
Definition: array2d.h:19
const T & at(int offset) const
array accessor
Definition: array2d.h:72
const int m
number of rows
Definition: array2d.h:27
iterator(Array2D *aparent, int start)
constructor with parent and start index
Definition: array2d.h:208
size_t offset(int i, int j) const
compute linear offset
Definition: array2d.h:226
iterator & operator--()
pre-decrement
Definition: array2d.h:146
const T & at(int i, int j) const
array accessor
Definition: array2d.h:59
const int n
number of columns
Definition: array2d.h:25
iterator & down()
move index one row down (decrement)
Definition: array2d.h:156
iterator & right()
move index one column to the right (increment)
Definition: array2d.h:159
bool valid(int off) const
Definition: array2d.h:123
T & at(int i, int j)
array accessor
Definition: array2d.h:54
void indices(int off, int *i, int *j) const
compute columnd and row from linear index
Definition: array2d.h:236
iterator & to(int i, int j)
move iterator to a given index
Definition: array2d.h:168
iterator()
default constructor
Definition: array2d.h:98