-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (141 loc) · 4.04 KB
/
Copy pathmain.cpp
File metadata and controls
171 lines (141 loc) · 4.04 KB
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
#include <iostream>
#include <vector>
#include <map>
#include <assert.h>
//#define ENABLE_2D_MATRIX
//#define ENABLE_3D_MATRIX
template<typename T, T DefVal = -1, size_t Dimension = 2>
class Matrix
{
public:
Matrix() = default;
~Matrix() { }
auto size() { return m_data.size(); }
auto begin() { return m_data.begin(); }
auto end() { return m_data.end(); }
//рекурсивный Proxy класс
template<class ContainerType, size_t SubDimension>
struct Proxy
{
private:
ContainerType *m_container;
std::vector<int> m_index;
public:
Proxy(ContainerType *a, std::vector<int> index) : m_container(a), m_index(index)
{ }
auto operator[](int i)
{
m_index.push_back(i);
return Proxy<ContainerType, SubDimension - 1>(m_container, m_index);
}
};
//конец рекурсии Proxy-класса
template<class ContainerType>
struct Proxy<ContainerType, 0>
{
private:
ContainerType *m_container;
std::vector<int> m_index;
public:
Proxy(ContainerType *a, std::vector<int> index) : m_container(a), m_index(index)
{ }
auto& operator = (const T& other)
{
//do not store default values
if (other == DefVal)
{
m_container->m_data.erase(m_index);
return const_cast<T&>(other);
}
return m_container->m_data[m_index] = other;
}
operator T()
{
if (m_container->m_data.find(m_index) != m_container->m_data.end())
return m_container->m_data[m_index];
else
return DefVal;
}
};
auto operator[](int i)
{
std::vector<int> index;
index.push_back(i);
return Proxy<Matrix<T, DefVal, Dimension>, Dimension - 1 >(this, index);
}
private:
std::map<std::vector<int>, T> m_data;
};
int main(int, char *[])
{
#ifdef ENABLE_2D_MATRIX
Matrix<int, -1, 2> matrix2d;
assert(matrix2d.size() == 0); // все ячейки свободны
auto a2d = matrix2d[2][4];
assert(a2d == -1);
assert(matrix2d.size() == 0);
matrix2d[100][100] = 314;
assert(matrix2d[100][100] == 314);
assert(matrix2d.size() == 1);
// выведется одна строка
// 100100314
for(auto c : matrix2d)
{
for(auto v : c.first)
std::cout << v;
std::cout << c.second << std::endl;
}
#endif
#ifdef ENABLE_3D_MATRIX
Matrix<int, -1, 3> matrix3d;
assert(matrix3d.size() == 0); // все ячейки свободны
auto a3d = matrix3d[0][0][0];
assert(a3d == -1);
assert(matrix3d.size() == 0);
matrix3d[100][100][100] = 314;
assert(matrix3d[100][100][100] == 314);
assert(matrix3d.size() == 1);
// выведется одна строка
// 100100100314
for(auto c : matrix3d)
{
for(auto v : c.first)
std::cout << v;
std::cout << c.second << std::endl;
}
#endif
//fill matrix
Matrix<int, 0, 2> matrix;
for(int i = 0, j = 0; i <= 9; ++i, ++j)
{
matrix[i][i] = i;
matrix[9 - i][i] = 9 - i;
}
//print matrix fragment
for(int i = 1; i <= 8; ++i)
{
for(int j = 1; j <= 8; ++j)
{
std::cout << matrix[i][j] << (j < 8 ? " " : "");
}
std::cout << std::endl;
}
assert(matrix.size() == 18);
std::cout << matrix.size() << std::endl;
for(auto c : matrix)
{
for(auto v : c.first)
std::cout << v;
std::cout << c.second << std::endl;
}
((matrix[100][100] = 314) = 0) = 217;
assert(matrix[100][100] == 217);
assert(matrix.size() == 19);
//std::cout << matrix(100, 100) << std::endl; //print 217
//check removing of element
matrix[100][100] = 314;
assert(matrix.size() == 19);
matrix[100][100] = 0;
assert(matrix.size() == 18);
return 0;
}