-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.h
312 lines (240 loc) · 6.05 KB
/
Grid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef _GRID_H
#define _GRID_H
#include "Location.h"
#include "Neighbours.h"
#include "choose.h"
#include <vector>
class GridSize
{
protected:
int _rows;
int _cols;
public:
int locationToIndex(int i, int j) const
{
return i * _cols + j;
}
Location indexToLocation(int index) const
{
int row = index / _cols;
int col = index - row * _cols;
return Location(row, col);
}
};
template<typename C> class Grid;
template<typename C, bool IsConst>
class GridIter
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef C value_type;
typedef std::ptrdiff_t difference_type;
typedef typename choose<IsConst, const C&, C&>::type reference;
typedef typename choose<IsConst, const C*, C*>::type pointer;
typedef typename choose<IsConst, const Grid<C>*, Grid<C>*>::type gridptr;
GridIter(gridptr grid, int row, int col) :
_grid(grid),
_location(row, col)
{ }
bool operator!= (const GridIter& other) const
{
return !(_location == other._location);
}
reference operator* ();
pointer operator-> ();
const GridIter& operator++ ()
{
++_location.col;
if (_location.col >= _grid->getCols())
{
_location.col = 0;
++_location.row;
}
return *this;
}
const Location& getLocation() const
{
return _location;
}
private:
gridptr _grid;
Location _location;
};
/*!
* \class Grid
* \brief Storage and access for a 2D grid of arbitrary elements
*/
template<typename C>
class Grid : public GridSize
{
public:
typedef std::vector<Location> LocationList;
typedef GridIter<C, false> iterator;
typedef GridIter<C, true> const_iterator;
protected:
std::vector<std::vector<C> > _cells;
public:
Grid(int rows = 0, int cols = 0);
~Grid();
void allocCells(int rows, int cols);
void resetCells(const C & value);
int getRows() const;
int getCols() const;
C& getCell(int i, int j);
C& getCell(const Location& loc);
const C& getCell(int i, int j) const;
const C& getCell(const Location& loc) const;
void setCell(const Location& loc, C c);
bool empty() const;
bool isValidCoordinate(int i, int j) const;
// define begin() in order to use it as an iterator in range for loops
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end();
Neighbours getNeighbours(int i, int j) const;
Neighbours getNeighbours(const Location &loc) const
{
return getNeighbours(loc.row, loc.col);
}
std::string toString() const;
bool fromStrings(const std::vector<std::string>& rows);
void fail(const char* message) const;
};
//--- inline method declarations ----------------------------------------------
template<typename C, bool IsFalse>
inline typename GridIter<C, IsFalse>::reference GridIter<C, IsFalse>::operator*()
{
return _grid->getCell(_location);
}
template<typename C, bool IsFalse>
inline typename GridIter<C, IsFalse>::pointer GridIter<C, IsFalse>::operator->()
{
return &_grid->getCell(_location);
}
template<typename C>
Grid<C>::Grid(int rows, int cols)
{
allocCells(rows, cols);
}
template<typename C>
Grid<C>::~Grid()
{
}
template<typename C>
void Grid<C>::allocCells(int rows, int cols)
{
_cells.clear();
_cells.resize(rows);
_rows = rows;
_cols = cols;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
C cell = C();
_cells[i].push_back(cell);
}
}
}
template<typename C>
void Grid<C>::resetCells(const C & value)
{
for (int i = 0; i < _rows; i++)
{
for (int j = 0; j < _cols; j++)
{
_cells[i][j] = value;
}
}
}
template<typename C>
inline bool Grid<C>::empty() const
{
return _rows == 0 && _cols == 0;
}
template<typename C>
inline bool Grid<C>::isValidCoordinate(int i, int j) const
{
return i >= 0 && i < _rows && j >= 0 && j < _cols;
}
template<typename C>
inline int Grid<C>::getCols() const
{
return _cols;
}
template<typename C>
inline int Grid<C>::getRows() const
{
return _rows;
}
template<typename C>
inline void Grid<C>::setCell(const Location& loc, C c)
{
_cells[loc.row][loc.col] = c;
}
template<typename C>
Neighbours Grid<C>::getNeighbours(int i, int j) const
{
Neighbours res;
for (int di = -1; di <= 1; di++)
{
for (int dj = -1; dj <= 1; dj++)
{
if (di == 0 && dj == 0)
continue; // I'm not my neighbour
if (!this->isValidCoordinate(i + di, j + dj))
continue; // outside grid
res.addLocation(Location(i + di, j + dj));
}
}
return res;
}
template<typename C>
inline const C& Grid<C>::getCell(int i, int j) const
{
if (!isValidCoordinate(i, j))
throw("kalle");
// fail("Grid: tried to get cell from invalid coordinate");
return _cells[i][j];
}
template<typename C>
inline C& Grid<C>::getCell(int i, int j)
{
if (!isValidCoordinate(i, j))
throw("kalle");
// fail("Grid: tried to get cell from invalid coordinate");
return _cells[i][j];
}
template<typename C>
inline C& Grid<C>::getCell(const Location& location)
{
return this->getCell(location.row, location.col);
}
template<typename C>
inline const C& Grid<C>::getCell(const Location& location) const
{
return this->getCell(location.row, location.col);
}
// define begin() in order to use it as an iterator in range for loops
template<typename C>
inline typename Grid<C>::const_iterator Grid<C>::begin() const
{
return const_iterator(this, 0, 0);
}
template<typename C>
inline typename Grid<C>::const_iterator Grid<C>::end() const
{
return const_iterator(this, _rows, 0);
}
template<typename C>
inline typename Grid<C>::iterator Grid<C>::begin()
{
return iterator(this, 0, 0);
}
template<typename C>
inline typename Grid<C>::iterator Grid<C>::end()
{
return iterator(this, _rows, 0);
}
#endif // _GRID_H_