-
Notifications
You must be signed in to change notification settings - Fork 6
/
grid.hpp
249 lines (222 loc) · 6.44 KB
/
grid.hpp
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
//
// grid.hpp
// Simpleton Engine
//
// Created by Indi Kernick on 21/4/18.
// Copyright © 2018 Indi Kernick. All rights reserved.
//
#ifndef engine_grid_grid_hpp
#define engine_grid_grid_hpp
#include <array>
#include <vector>
#include "pos.hpp"
#include <utility>
#include "../Utils/numeric iterators.hpp"
namespace Grid {
namespace detail {
template <typename Derived, typename Tile>
class GridBase {
private:
Derived &that() {
return *static_cast<Derived *>(this);
}
const Derived &that() const {
return *static_cast<const Derived *>(this);
}
void checkRange(const Pos pos) const {
if (that().outOfRange(pos)) {
throw std::range_error("Position out of range");
}
}
void checkRange(const size_t index) const {
if (that().outOfRange(index)) {
throw std::range_error("Index out of range");
}
}
public:
auto begin() {
return that().mTiles.begin();
}
auto begin() const {
return that().mTiles.begin();
}
auto cbegin() const {
return that().mTiles.begin();
}
auto end() {
return that().mTiles.end();
}
auto end() const {
return that().mTiles.end();
}
auto cend() const {
return that().mTiles.end();
}
auto hori() const {
return Utils::range(that().size().x);
}
auto vert() const {
return Utils::range(that().size().y);
}
auto horiRev() const {
return Utils::rangeRev(that().size().x);
}
auto vertRev() const {
return Utils::rangeRev(that().size().y);
}
Tile &operator()(const Coord x, const Coord y) {
return const_cast<Tile &>(std::as_const(*this).operator()(x, y));
}
const Tile &operator()(const Coord x, const Coord y) const {
return that().mTiles[that().toIndex({x, y})];
}
Tile &operator[](const Pos pos) {
return const_cast<Tile &>(std::as_const(*this).operator[](pos));
}
const Tile &operator[](const Pos pos) const {
return that().mTiles[that().toIndex(pos)];
}
Tile &at(const Pos pos) {
return const_cast<Tile &>(std::as_const(*this).at(pos));
}
const Tile &at(const Pos pos) const {
checkRange(pos);
return that().mTiles[that().toIndex(pos)];
}
Tile &operator[](const size_t index) {
return const_cast<Tile &>(std::as_const(*this).operator[](index));
}
const Tile &operator[](const size_t index) const {
assert(index < that().mTiles.size());
return that().mTiles[index];
}
Tile &at(const size_t index) {
return const_cast<Tile &>(std::as_const(*this).at(index));
}
const Tile &at(const size_t index) const {
checkRange(index);
return that().mTiles[index];
}
};
}
constexpr Coord runtime = 0;
template <typename Tile_, Coord Width_ = runtime, Coord Height_ = runtime>
class Grid final : public detail::GridBase<Grid<Tile_, Width_, Height_>, Tile_> {
public:
friend detail::GridBase<Grid, Tile_>;
using Tile = Tile_;
static constexpr Coord Width = Width_;
static constexpr Coord Height = Height_;
using Tiles = std::array<Tile, Width * Height>;
static_assert(Width > 0);
static_assert(Height > 0);
Grid() = default;
// So that dynamic and static grids have compatible interfaces
Grid(const Coord width, const Coord height) {
assert(width == Width);
assert(height == Height);
}
explicit Grid(const Pos size)
: Grid{size.x, size.y} {}
Grid(const Pos size, const Tile &tile)
: Grid{size} {
mTiles.fill(tile);
}
explicit Grid(const Tile &tile) {
mTiles.fill(tile);
}
void fill(const Tile &tile) {
mTiles.fill(tile);
}
static constexpr Pos size() {
return {Width, Height};
}
static constexpr Coord width() {
return Width;
}
static constexpr Coord height() {
return Height;
}
static constexpr size_t area() {
return static_cast<size_t>(Width * Height);
}
static bool outOfRange(const Pos pos) {
return pos.x < 0 || pos.y < 0 || pos.x >= Width || pos.y >= Height;
}
static bool outOfRange(const size_t index) {
return index >= Width * Height;
}
static size_t toIndex(const Pos pos) {
assert(!outOfRange(pos));
return pos.y * Width + pos.x;
}
static Pos toPos(const size_t index) {
assert(!outOfRange(index));
const Coord cindex = static_cast<Coord>(index);
return {cindex % Width, cindex / Width};
}
private:
Tiles mTiles;
};
template <typename Tile_>
class Grid<Tile_, runtime, runtime> final : public detail::GridBase<Grid<Tile_, runtime, runtime>, Tile_> {
public:
friend detail::GridBase<Grid, Tile_>;
using Tile = Tile_;
using Tiles = std::vector<Tile>;
Grid()
: mSize(0, 0) {}
Grid(const Coord width, const Coord height, const Tile &tile = {})
: mTiles{static_cast<size_t>(width * height), tile},
mSize{width, height} {
assert(width > 0);
assert(height > 0);
}
explicit Grid(const Pos size, const Tile &tile = {})
: Grid{size.x, size.y, tile} {}
void clear() {
mTiles.clear();
mSize = {0, 0};
}
void resize(const Pos size, const Tile &tile = {}) {
assert(size.x > 0);
assert(size.y > 0);
mTiles.resize(static_cast<size_t>(size.x * size.y), tile);
mSize = size;
}
void fill(const Tile &tile) {
std::fill(mTiles.begin(), mTiles.end(), tile);
}
Pos size() const {
return mSize;
}
Coord width() const {
return mSize.x;
}
Coord height() const {
return mSize.y;
}
size_t area() const {
return mTiles.size();
}
bool outOfRange(const Pos pos) const {
return pos.x < 0 || pos.y < 0 || pos.x >= mSize.x || pos.y >= mSize.y;
}
bool outOfRange(const size_t index) const {
return index >= mTiles.size();
}
size_t toIndex(const Pos pos) const {
assert(!outOfRange(pos));
return pos.y * mSize.x + pos.x;
}
Pos toPos(const size_t index) const {
assert(!outOfRange(index));
const Coord cindex = static_cast<Coord>(index);
return {cindex % mSize.x, cindex / mSize.x};
}
private:
Tiles mTiles;
Pos mSize;
};
}
#endif