-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.h
172 lines (143 loc) · 3.69 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
#ifndef GRID_H
#define GRID_H
#include "Position.h"
#include <array>
#include <cassert>
#include <vector>
template <class T, int Radius>
using MooreNeighborhood = std::array<std::array<T, 2*Radius+1>, 2*Radius+1>;
template <class T>
class Grid
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef int size_type;
Grid(int rowCount, int columnCount)
: mRowCount(rowCount)
, mColumnCount(columnCount)
{
cells.resize(rowCount);
for (int i = 0; i < mRowCount; ++i) {
cells[i].resize(mColumnCount);
}
}
Grid(const Grid &other) = delete;
Grid(Grid&& other)
: mRowCount(other.mRowCount)
, mColumnCount(other.mColumnCount)
, cells(std::move(other.cells))
{
}
Grid& operator()(const Grid& other) = delete;
Grid& operator()(Grid&& other)
{
mRowCount = other.mRowCount;
mColumnCount = other.mColumnCount;
cells = std::move(other.cells);
}
size_type rowCount() const noexcept
{
return mRowCount;
}
size_type columnCount() const noexcept
{
return mColumnCount;
}
reference at(int rowIndex, int colIndex)
{
assert(rowIndex < mRowCount && colIndex < mColumnCount);
return cells[rowIndex][colIndex];
}
const_reference at(int rowIndex, int colIndex) const
{
assert(rowIndex < mRowCount && colIndex < mColumnCount);
return cells[rowIndex][colIndex];
}
reference at(Position p)
{
return at(p.row, p.col);
}
const_reference at(Position p) const
{
return at(p.row, p.col);
}
template <size_type Radius>
MooreNeighborhood<value_type, Radius> mooreNeighborhoodAt(int rowIndex, int colIndex)
{
assert(rowIndex < mRowCount && colIndex < mColumnCount);
if (__builtin_expect(needWraparound<Radius>(rowIndex, colIndex), 0)) {
return mooreNeighborhoodWraparoundedAt<Radius>(rowIndex, colIndex);
}
MooreNeighborhood<value_type, Radius> result;
for (int r = 0 ; r < (2*Radius+1); ++r) {
int row = rowIndex - (Radius - r);
for (int c = 0 ; c < (2*Radius+1); ++c) {
int col = colIndex - (Radius - c);
result[r][c] = at(row, col);
}
}
return result;
}
template <size_type Radius>
MooreNeighborhood<value_type, Radius> mooreNeighborhoodAt(Position p)
{
return mooreNeighborhoodAt<Radius>(p.row, p.col);
}
size_type size() const noexcept
{
return mRowCount * mColumnCount;
}
private:
template <size_type Radius>
MooreNeighborhood<value_type, Radius> mooreNeighborhoodWraparoundedAt(int rowIndex, int colIndex)
{
assert(rowIndex < mRowCount && colIndex < mColumnCount);
MooreNeighborhood<value_type, Radius> result;
for (int r = 0 ; r < (2*Radius+1); ++r) {
int row = rowIndex - (Radius - r);
row = wraparoundRow(row);
for (int c = 0 ; c < (2*Radius+1); ++c) {
int col = colIndex - (Radius - c);
col = wraparoundColumn(col);
result[r][c] = at(row, col);
}
}
return result;
}
constexpr bool needWraparound(int rowIndex, int colIndex, int radius) const noexcept
{
return (rowIndex < radius)
|| (colIndex < radius)
|| (rowIndex >= (mRowCount - radius))
|| (colIndex >= (mColumnCount - radius));
}
template <int Radius>
constexpr bool needWraparound(int rowIndex, int colIndex) const noexcept
{
return needWraparound(rowIndex, colIndex, Radius);
}
int wraparound(int index, int dimensionSize) const noexcept
{
if (index < 0) {
index += dimensionSize;
} else if (index >= dimensionSize) {
index -= dimensionSize;
}
return index;
}
int wraparoundRow(int rowIndex) const noexcept
{
return wraparound(rowIndex, mRowCount);
}
int wraparoundColumn(int colIndex) const noexcept
{
return wraparound(colIndex, mColumnCount);
}
private:
size_type mRowCount;
size_type mColumnCount;
std::vector<std::vector<value_type>> cells;
};
#endif // GRID_H