-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockMap.h
182 lines (154 loc) · 3.29 KB
/
BlockMap.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
#ifndef BLOCKMAP_H
#define BLOCKMAP_H
#include "Position.h"
#include <cassert>
#include <cmath>
#include <vector>
#include <iostream>
template <class CellType>
class Block
{
public:
Block(int rows, int columns)
: mPlantCells(new CellType[rows*columns])
, mRows(rows)
, mColumns(columns)
{
//std::cout << mRows << "x" << mColumns << std::endl;
}
Block(Block& other) = delete;
Block(Block&& other)
: mPlantCells(other.mPlantCells)
, mRows(other.mRows)
, mColumns(other.mColumns)
{
other.mPlantCells = nullptr;
}
Block& operator=(Block& other) = delete;
Block& operator=(Block&& other)
{
mPlantCells = other.mPlantCells;
mRows = other.mRows;
mColumns = other.mColumns;
other.mPlantCells = nullptr;
}
~Block()
{
delete[] mPlantCells;
}
CellType& cell(int row, int col)
{
assert(row < mRows && col < mColumns);
return mPlantCells[row*mColumns + col];
}
const CellType& cell(int row, int col) const
{
assert(row < mRows && col < mColumns);
return mPlantCells[row*mColumns + col];
}
CellType& cell(Position p)
{
return cell(p.row, p.col);
}
const CellType& cell(Position p) const
{
return cell(p.row, p.col);
}
int rows() const
{
return mRows;
}
int columns() const
{
return mColumns;
}
private:
CellType* mPlantCells;
int mRows;
int mColumns;
};
template <class CellType, int BLOCK_ROWS, int BLOCK_COLUMNS>
class BlockMap
{
public:
BlockMap(int columns, int rows)
: mRows(std::ceil((double)rows / BLOCK_ROWS))
, mColumns(std::ceil((double)columns / BLOCK_COLUMNS))
{
blocks.resize(mRows);
for (int i = 0; i < mRows; ++i) {
for (int j = 0; j < mColumns; ++j) {
int blockRows;
int blockColumns;
if ((i + 1) * BLOCK_ROWS <= rows) {
blockRows = BLOCK_ROWS;
} else {
blockRows = rows - i*BLOCK_ROWS;
}
if ((j + 1) * BLOCK_COLUMNS <= columns) {
blockColumns = BLOCK_COLUMNS;
} else {
blockColumns = columns - j*BLOCK_COLUMNS;
}
blocks[i].push_back(Block<CellType>(blockRows, blockColumns));
}
}
}
CellType& cell(int row, int col)
{
int blockRow = row / BLOCK_ROWS;
int blockCol = col / BLOCK_COLUMNS;
int cellRow = row - blockRow * BLOCK_ROWS;
int cellCol = col - blockCol * BLOCK_COLUMNS;
return blocks[blockRow][blockCol].cell(cellRow, cellCol);
}
const CellType& cell(int row, int col) const
{
int blockRow = row / BLOCK_ROWS;
int blockCol = col / BLOCK_COLUMNS;
int cellRow = row - blockRow * BLOCK_ROWS;
int cellCol = col - blockCol * BLOCK_COLUMNS;
return blocks[blockRow][blockCol].cell(cellRow, cellCol);
}
CellType& cell(Position p)
{
return cell(p.row, p.col);
}
const CellType& cell(Position p) const
{
return cell(p.row, p.col);
}
int rows() const
{
return mRows;
}
int columns() const
{
return mColumns;
}
Block<CellType>& block(int row, int col)
{
assert(row < mRows && col < mColumns);
return blocks[row][col];
}
const Block<CellType>& block(int row, int col) const
{
assert(row < mRows && col < mColumns);
return blocks[row][col];
}
Block<CellType>& block(Position p)
{
return block(p.row, p.col);
}
const Block<CellType>& block(Position p) const
{
return block(p.row, p.col);
}
private:
std::vector<std::vector<Block<CellType>>> blocks;
int width;
int height;
int mRows;
int mColumns;
};
#endif // BLOCKMAP_H