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
|
#ifndef MAP_H_3AB00D12
#define MAP_H_3AB00D12
#include <vector>
#include <algorithm>
template <typename T>
class Map {
public:
Map(
int left,
int top,
int width,
int height) :
left_(left),
top_(top),
width_(width),
height_(height),
data_(width_*height_)
{
}
inline int getLeft() const
{
return left_;
}
inline int getRight() const
{
return left_ + width_;
}
inline int getTop() const
{
return top_;
}
inline int getBottom() const
{
return top_ + height_;
}
inline int getWidth() const
{
return width_;
}
inline int getHeight() const
{
return height_;
}
inline int getTrueX(int x) const
{
return (x - left_);
}
inline int getTrueY(int y) const
{
return (y - top_);
}
inline bool inBounds(int x, int y) const
{
return (x >= left_) &&
(x < left_ + width_) &&
(y >= top_) &&
(y < top_ + height_);
}
inline const T& at(int x, int y) const
{
return data_.at((x - left_) + width_ * (y - top_));
}
inline T& at(int x, int y)
{
return const_cast<T&>(static_cast<const Map&>(*this).at(x, y));
}
inline const std::vector<T>& data() const
{
return data_;
}
inline std::vector<T>& data()
{
return data_;
}
void resize(int newLeft, int newTop, int newWidth, int newHeight)
{
std::vector<T> newData(newWidth * newHeight);
int winTop = std::max(top_, newTop);
int winBottom = std::min(top_ + height_, newTop + newHeight);
int winLeft = std::max(left_, newLeft);
int winRight = std::min(left_ + width_, newLeft + newWidth);
for (int y = winTop; y < winBottom; y++)
{
std::move(
std::next(std::begin(data_), (winLeft - left_) + width_ * (y - top_)),
std::next(std::begin(data_), (winRight - left_) + width_ * (y - top_)),
std::next(std::begin(newData),
(winLeft - newLeft) + newWidth * (y - newTop)));
}
left_ = newLeft;
top_ = newTop;
width_ = newWidth;
height_ = newHeight;
data_.swap(newData);
}
private:
int left_;
int top_;
int width_;
int height_;
std::vector<T> data_;
};
#endif /* end of include guard: MAP_H_3AB00D12 */
|