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
|
#pragma once
#include <util/system/yassert.h>
#include <util/generic/algorithm.h>
// FIXME: check for Y_IF_DEBUG instead
#ifdef _DEBUG
template <class T>
struct TBoundCheck {
T* Data;
size_t Size;
TBoundCheck(T* d, size_t s) {
Data = d;
Size = s;
}
T& operator[](size_t i) const {
Y_ASSERT(i >= 0 && i < Size);
return Data[i];
}
};
#endif
template <class T>
class TArray2D {
private:
typedef T* PT;
T* Data;
T** PData;
size_t XSize;
size_t YSize;
private:
void Copy(const TArray2D& a) {
XSize = a.XSize;
YSize = a.YSize;
Create();
for (size_t i = 0; i < XSize * YSize; i++)
Data[i] = a.Data[i];
}
void Destroy() {
delete[] Data;
delete[] PData;
}
void Create() {
Data = new T[XSize * YSize];
PData = new PT[YSize];
for (size_t i = 0; i < YSize; i++)
PData[i] = Data + i * XSize;
}
public:
TArray2D(size_t xsize = 1, size_t ysize = 1) {
XSize = xsize;
YSize = ysize;
Create();
}
TArray2D(const TArray2D& a) {
Copy(a);
}
TArray2D& operator=(const TArray2D& a) {
Destroy();
Copy(a);
return *this;
}
~TArray2D() {
Destroy();
}
void SetSizes(size_t xsize, size_t ysize) {
if (XSize == xsize && YSize == ysize)
return;
Destroy();
XSize = xsize;
YSize = ysize;
Create();
}
void Clear() {
SetSizes(1, 1);
}
#ifdef _DEBUG
TBoundCheck<T> operator[](size_t i) const {
Y_ASSERT(i < YSize);
return TBoundCheck<T>(PData[i], XSize);
}
#else
T* operator[](size_t i) const {
Y_ASSERT(i < YSize);
return PData[i];
}
#endif
size_t GetXSize() const {
return XSize;
}
size_t GetYSize() const {
return YSize;
}
void FillZero() {
memset(Data, 0, sizeof(T) * XSize * YSize);
}
void FillEvery(const T& a) {
for (size_t i = 0; i < XSize * YSize; i++)
Data[i] = a;
}
void Swap(TArray2D& a) {
std::swap(Data, a.Data);
std::swap(PData, a.PData);
std::swap(XSize, a.XSize);
std::swap(YSize, a.YSize);
}
};
template <class T>
inline bool operator==(const TArray2D<T>& a, const TArray2D<T>& b) {
if (a.GetXSize() != b.GetXSize() || a.GetYSize() != b.GetYSize())
return false;
for (size_t y = 0; y < a.GetYSize(); ++y) {
for (size_t x = 0; x < a.GetXSize(); ++x)
if (a[y][x] != b[y][x])
return false;
}
return true;
}
template <class T>
inline bool operator!=(const TArray2D<T>& a, const TArray2D<T>& b) {
return !(a == b);
}
|