-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid2D.hpp
214 lines (181 loc) · 7.05 KB
/
Grid2D.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
// Two dimensional grid class
#include <algorithm> // std::min()
#include <cassert> // assert()
#include <functional> // std::function
#include <iostream> // std::ostream
#include <limits> // std::numeric_limits<T>
#include <vector> // std::vector<T>
namespace utils
{
struct row_major {
static size_t index(const size_t i, const size_t j, const size_t rows, const size_t cols) {
return i*cols + j;
}
};
struct column_major {
static size_t index(const size_t i, const size_t j, const size_t rows, const size_t cols) {
return j*rows + i;
}
};
template<class T, class Ordering>
class Grid2D
{
public:
// This is the default constructor. Call it without any
// arguments and it will return a 0x0 matrix (initialized
// with 0)
// Example
// utils::Grid2D<double, utils::row_major> g;
//
// Example 2
// using Grid_t = utils::Grid2D<double, utils::row_major>
// Grid_t g;
Grid2D()
: rows(0)
, cols(0)
, data_(std::vector<T>(0))
, blockSize(0)
{}
// This is the normal constructor. Gets you a rowsxcols matrix
// with optinal default value value
// Example
// utils::Grid2D<double, utils::row_major> square(3, 3, 5.0);
Grid2D(const size_t rows, const size_t cols, T value=0, const size_t blockSize = 0)
: rows(rows)
, cols(cols)
, data_(std::vector<T>(rows*cols, value))
, blockSize(0)
{}
const size_t getRows() const { return rows; }
const size_t getCols() const { return cols; }
const T* data() const { return data_.data(); }
T* data() { return data_.data(); }
const size_t getBlockSize() const { return blockSize; }
// PRE only works for row_major
// TODO write a version for column_major
void setBlockSize(const size_t size) {
// assert rows and cols beeing multiple of the block size
assert(rows % size == 0);
assert(cols % size == 0);
assert(size > 1 && size <= std::min(rows, cols));
blockSize = size;
}
// PRE only works for row_major
// TODO write a version for column_major
void getBlock(const size_t I, const size_t J, utils::Grid2D<T, Ordering>& block) {
// assert a useful block size
assert(blockSize > 1);
assert(blockSize <= std::min(rows, cols));
assert(rows % blockSize == 0 && cols % blockSize == 0);
// assert i/j within bounds
assert(I < rows/blockSize && J < cols/blockSize);
// assert block beeing of size blockSize x blockSize
assert(block.getCols() == blockSize && block.getRows() == blockSize);
size_t k = 0;
for(size_t i = 0; i < blockSize; ++i) {
for(size_t j = 0; j < blockSize; ++j) {
block.data_[k++] = data_[(I*blockSize+i)*cols + J*blockSize+j];
}
}
assert(k == blockSize * blockSize);
}
void setBlock(const size_t I, const size_t J, const utils::Grid2D<T, Ordering>& block) {
// assert a useful blockSize factor
assert(blockSize > 1);
assert(blockSize <= std::min(rows, cols));
assert(rows % blockSize == 0 && cols % blockSize == 0);
// assert i/j within bounds
assert(I < rows/blockSize && J < cols/blockSize);
// assert block beeing of size blockSize x blockSize
assert(block.getCols() == blockSize && block.getRows() == blockSize);
size_t k = 0;
for(size_t i = 0; i < blockSize; ++i) {
for(size_t j = 0; j < blockSize; ++j) {
data_[(I*blockSize+i)*cols + J*blockSize+j] = block.data_[k++];
}
}
}
void fillRandom(std::function<T()> generator) {
std::generate(data_.begin(), data_.end(), generator);
}
// This is the copy constructor.
// Example
// utils::Grid2D<int, utils::row_major> g(3, 3, 0);
// utils::Grid2D<int, utils::row_major> copy(g);
Grid2D(const Grid2D<T, Ordering>& g)
: rows(g.rows)
, cols(g.cols)
, data_(g.data_)
, blockSize(g.blockSize)
{}
// This is the assignment operator
// Example
// utils::Grid2D<double, utils::row_major> g(3, 4, 5.0);
// utils::Grid2D<int, utils::row_major> g2;
// g2 = g;
Grid2D& operator=(Grid2D<T, Ordering> g) {
using std::swap;
swap(*this, g);
return *this;
}
// This is for writing to location (i,j)
T& operator()(const size_t i, const size_t j) {
assert(i < rows);
assert(j < cols);
return data_[Ordering::index(i, j, rows, cols)];
}
// This is for reading from location (i,j)
const T& operator()(const size_t i, const size_t j) const {
assert(i < rows);
assert(j < cols);
return data_[Ordering::index(i, j, rows, cols)];
}
// This is for wirting to location (idx)
T& operator()(const size_t idx) {
assert(idx < data_.size());
return data_[idx];
}
// This is for reading from location (idx)
const T& operator()(const size_t idx) const {
assert(idx < data_.size());
return data_[idx];
}
template<class U, class O2>
friend void swap(utils::Grid2D<U, O2>& left, utils::Grid2D<U, O2>& right);
private:
size_t rows;
size_t cols;
std::vector<T> data_;
size_t blockSize;
};
template<class T, class Ordering>
std::ostream& operator<<(std::ostream& out, const utils::Grid2D<T, Ordering>& g)
{
if(g.getBlockSize() > 1) {
out << "Block size: " << g.getBlockSize() << "\n";
}
for(size_t i = 0; i < g.getRows(); ++i) {
if(g.getBlockSize() > 1 && i > 0 && i % g.getBlockSize() == 0) {
for(size_t k = 0; k < g.getCols() + g.getCols() / g.getBlockSize() - 1; ++k) {
out << "-\t";
}
out << "\n";
}
for(size_t j = 0; j < g.getCols(); ++j) {
if(g.getBlockSize() > 1 && j > 0 && j % g.getBlockSize() == 0) {
out << "|\t";
}
out << g(i,j) << "\t";
}
out << "\n";
}
return out;
}
template<class T, class Ordering>
void swap(utils::Grid2D<T, Ordering>& left, utils::Grid2D<T, Ordering>& right) {
std::swap(left.rows, right.rows);
std::swap(left.cols, right.cols);
std::swap(left.data_, right.data_);
std::swap(left.blockSize, right.blockSize);
}
} // end of namespace utils