summary refs log tree commit diff stats
path: root/src/map.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/map.h')
-rw-r--r--src/map.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/map.h b/src/map.h new file mode 100644 index 0000000..329553c --- /dev/null +++ b/src/map.h
@@ -0,0 +1,127 @@
1#ifndef MAP_H_3AB00D12
2#define MAP_H_3AB00D12
3
4#include <vector>
5#include <algorithm>
6
7template <typename T>
8class Map {
9public:
10
11 Map(
12 int left,
13 int top,
14 int width,
15 int height) :
16 left_(left),
17 top_(top),
18 width_(width),
19 height_(height),
20 data_(width_*height_)
21 {
22 }
23
24 inline int getLeft() const
25 {
26 return left_;
27 }
28
29 inline int getRight() const
30 {
31 return left_ + width_;
32 }
33
34 inline int getTop() const
35 {
36 return top_;
37 }
38
39 inline int getBottom() const
40 {
41 return top_ + height_;
42 }
43
44 inline int getWidth() const
45 {
46 return width_;
47 }
48
49 inline int getHeight() const
50 {
51 return height_;
52 }
53
54 inline int getTrueX(int x) const
55 {
56 return (x - left_);
57 }
58
59 inline int getTrueY(int y) const
60 {
61 return (y - top_);
62 }
63
64 inline bool inBounds(int x, int y) const
65 {
66 return (x >= left_) &&
67 (x < left_ + width_) &&
68 (y >= top_) &&
69 (y < top_ + height_);
70 }
71
72 inline const T& at(int x, int y) const
73 {
74 return data_.at((x - left_) + width_ * (y - top_));
75 }
76
77 inline T& at(int x, int y)
78 {
79 return const_cast<T&>(static_cast<const Map&>(*this).at(x, y));
80 }
81
82 inline const std::vector<T>& data() const
83 {
84 return data_;
85 }
86
87 inline std::vector<T>& data()
88 {
89 return data_;
90 }
91
92 void resize(int newLeft, int newTop, int newWidth, int newHeight)
93 {
94 std::vector<T> newData(newWidth * newHeight);
95
96 int winTop = std::max(top_, newTop);
97 int winBottom = std::min(top_ + height_, newTop + newHeight);
98 int winLeft = std::max(left_, newLeft);
99 int winRight = std::min(left_ + width_, newLeft + newWidth);
100
101 for (int y = winTop; y < winBottom; y++)
102 {
103 std::move(
104 std::next(std::begin(data_), (winLeft - left_) + width_ * (y - top_)),
105 std::next(std::begin(data_), (winRight - left_) + width_ * (y - top_)),
106 std::next(std::begin(newData),
107 (winLeft - newLeft) + newWidth * (y - newTop)));
108 }
109
110 left_ = newLeft;
111 top_ = newTop;
112 width_ = newWidth;
113 height_ = newHeight;
114 data_.swap(newData);
115 }
116
117private:
118
119 int left_;
120 int top_;
121 int width_;
122 int height_;
123
124 std::vector<T> data_;
125};
126
127#endif /* end of include guard: MAP_H_3AB00D12 */