-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.h
68 lines (68 loc) · 1.84 KB
/
Map.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
#include "phoenix.h"
template<typename T>
class Map {
private:
unsigned int width = 0, height = 0;
T* data = nullptr;
public:
auto __Width__() const -> unsigned int {
return width;
}
void __Width__(unsigned int value) {
width = value;
}
auto __Height__() const -> unsigned int {
return height;
}
void __Height__(unsigned int value) {
height = value;
}
public:
Map() {
}
Map(const Map<T>& map) {
__Width__(map.width);
__Height__(map.height);
data = new T[width * height];
memcpy(this->data, map.data, width * height * sizeof(T));
}
Map(const T* data, int width, int height) {
__Width__(width);
__Height__(height);
this->data = new T[width * height];
memcpy(this->data, data, width * height * sizeof(T));
}
Map(const T& initData, int width, int height) {
__Width__(width);
__Height__(height);
this->data = new T[width * height];
T* p = this->data;
T* pEnd = this->data + __Width__() * __Height__();
while (p < pEnd) {
*p = initData;
p++;
}
}
~Map() {
if (data != nullptr) {
delete []data;
data = nullptr;
}
}
public:
void SetValueAt(const Point<unsigned int>& point, const T& value) {
SetValueAt(point.x, point.y, value);
}
void SetValueAt(unsigned int x, unsigned int y, const T& value) {
data[y * __Width__() + x] = value;
}
T GetValueAt(const Point<unsigned int>& point) const {
return GetValueAt(point.x, point.y);
}
T GetValueAt(unsigned int x, unsigned int y) const {
return data[y * __Width__() + x];
}
T operator[](const Point<unsigned int>& point) {
return data[point.y * __Width__() + point.x];
}
};