Walls And Holes  1
array2d_private.h
Go to the documentation of this file.
1 #ifndef ARRAY2D_PRIVATE_H
2 #define ARRAY2D_PRIVATE_H
3 
4 #include <QPoint>
5 #include <QPair>
6 
7 #include "array2d.h"
8 
9 
10 template< typename Type > class Array2D;
11 
12 /*
13  * This header contains some helper classes and methods for Array2D that are
14  * not necessary for users of Array2D to know.
15  *
16  * The purpose is to reduce clutter in "array2d.h" and in the global namespace.
17  * */
18 
19 namespace Array2D_Private {
20 
21 
30 
32  {
33  ++mY;
34  if (mY == mHeight) {
35  mY = 0;
36  ++mX;
37  }
38 
39  return *this;
40  }
41 
42  bool operator ==(const Array2DItrTemplate &other) const
43  {
44  return mX == other.mX && mY == other.mY && mWidth == other.mWidth && mHeight == other.mHeight;
45  }
46 
47  bool operator !=(const Array2DItrTemplate &other) const
48  {
49  return !(*this == other);
50  }
51 
52 protected:
53  int x() const { return mX; }
54  int y() const { return mY; }
55 
56  int width() const { return mWidth; }
57  int height() const { return mHeight; }
58 
59  Array2DItrTemplate(int w, int h, int x, int y)
60  : mWidth(w), mHeight(h)
61  , mX(x), mY(y) {}
62 
63 private:
64 
65  int mWidth;
66  int mHeight;
67  int mX;
68  int mY;
69 };
70 
71 
76 public:
77  struct Itr;
78 
79  template< typename T >
81  : mWidth(arr.width()), mHeight(arr.height())
82  {}
83 
84 
85  Itr begin() const { return Itr::start(mWidth, mHeight); }
86  Itr end() const { return Itr::end(mWidth, mHeight); }
87 
88 
89  struct Itr : public Array2DItrTemplate {
90  static Itr start(int w, int h) { return Itr(w, h, 0, 0); }
91  static Itr end(int w, int h) { return Itr(w, h, w, 0); }
92 
93  QPoint operator *() const
94  {
95  return QPoint(x(), y());
96  }
97 
98  private:
99  Itr(int w, int h, int x, int y)
100  : Array2DItrTemplate(w, h, x, y) {}
101  };
102 
103 
104 protected:
105  int mWidth;
106  int mHeight;
107 };
108 
113 template< typename Type >
115 public:
116  struct Itr;
117 
119  : mArray(arr)
120  {}
121 
122 
123  Itr begin() const { return Itr::start(mArray); }
124  Itr end() const { return Itr::end(mArray); }
125 
126 
127  struct Itr : public Array2DItrTemplate {
128  static Itr start(const Array2D<Type> &arr) { return Itr(arr, 0, 0); }
129  static Itr end(const Array2D<Type> &arr) { return Itr(arr, arr.width(), 0); }
130 
131  QPair<QPoint, const Type &> operator *() const
132  {
133  QPoint pt = QPoint(x(), y());
134  return QPair<QPoint, const Type &> (pt, mArray(pt));
135  }
136 
137  private:
138  Itr(const Array2D<Type> arr, int x, int y)
139  : Array2DItrTemplate(arr.width(), arr.height(), x, y)
140  , mArray(arr)
141  {}
142 
143  const Array2D<Type> &mArray;
144  };
145 
146 
147 protected:
149 };
150 
151 
152 
153 }
154 
155 
156 #endif // ARRAY2D_PRIVATE_H
static Itr end(int w, int h)
Definition: array2d_private.h:91
bool operator==(const Array2DItrTemplate &other) const
Definition: array2d_private.h:42
static Itr start(const Array2D< Type > &arr)
Definition: array2d_private.h:128
int width() const
Definition: array2d_private.h:56
int width() const
Definition: array2d.h:81
Array2DPointWrapper(const Array2D< T > &arr)
Definition: array2d_private.h:80
A lightweight class to allow looping through all the data in an Array2D, outputting QPairs of (positi...
Definition: array2d_private.h:114
Array2DPointAndConstDataWrapper(const Array2D< Type > &arr)
Definition: array2d_private.h:118
static Itr end(const Array2D< Type > &arr)
Definition: array2d_private.h:129
Itr begin() const
Definition: array2d_private.h:123
Array2DItrTemplate(int w, int h, int x, int y)
Definition: array2d_private.h:59
int x() const
Definition: array2d_private.h:53
Itr end() const
Definition: array2d_private.h:86
Definition: array2d_private.h:19
Definition: array2d_private.h:89
const Array2D< Type > & mArray
Definition: array2d_private.h:148
bool operator!=(const Array2DItrTemplate &other) const
Definition: array2d_private.h:47
QPoint operator*() const
Definition: array2d_private.h:93
QPair< QPoint, const Type & > operator*() const
Definition: array2d_private.h:131
The Array2D class is basically a 2D implementation of QVector.
Definition: array2d.h:35
Itr begin() const
Definition: array2d_private.h:85
An almost-implemented iterator object that iterates through all positions in a rectangle.
Definition: array2d_private.h:29
int y() const
Definition: array2d_private.h:54
Array2DItrTemplate & operator++()
Definition: array2d_private.h:31
int mWidth
Definition: array2d_private.h:105
Itr end() const
Definition: array2d_private.h:124
static Itr start(int w, int h)
Definition: array2d_private.h:90
int height() const
Definition: array2d_private.h:57
int mHeight
Definition: array2d_private.h:106
A lightweight class to allow looping through all the valid indices in Array2D.
Definition: array2d_private.h:75