-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
181 lines (146 loc) · 3.17 KB
/
matrix.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
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
172
173
174
175
176
177
178
179
180
181
#pragma once
#include <array>
#include <map>
namespace hw06 {
namespace details {
template<typename T, std::size_t... I>
auto toTuple(const T& array, std::index_sequence<I...>)
{
return std::make_tuple(array[I]...);
}
template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
auto toTuple(const std::array<T, N>& array)
{
return toTuple(array, Indices{});
}
}
template<typename T, T Default, size_t Dimension = 2>
class Matrix
{
public:
using Index = std::array<size_t, Dimension>;
using Data = std::map<Index, T>;
template<size_t N, typename F = void>
class Proxy
{
public:
Proxy(const std::array<size_t, N>& index, Data& data)
: m_index{index}
, m_data{data}
{
}
auto operator[](size_t i)
{
std::array<size_t, N + 1> index;
std::copy(std::begin(m_index), std::end(m_index), std::begin(index));
index[N] = i;
return Proxy<N + 1>{index, m_data};
}
private:
std::array<size_t, N> m_index;
Data& m_data;
};
template<typename F>
class Proxy<Dimension, F>
{
public:
Proxy(const Index& index, Data& data)
: m_index{index}
, m_data{data}
{
}
auto& operator=(const T& value)
{
if (value == Default) {
m_data.erase(m_index);
return m_default;
}
m_data[m_index] = value;
return m_data[m_index];
}
operator const T&() const
{
auto i = m_data.find(m_index);
if (i != m_data.end())
return i->second;
return m_default;
}
private:
Index m_index;
Data& m_data;
T m_default{Default};
};
using DataIterator = typename Data::iterator;
struct iterator : std::iterator<std::forward_iterator_tag, DataIterator>
{
iterator() = default;
explicit iterator(DataIterator it)
: m_dataIterator(it)
{
}
auto& operator++()
{
++m_dataIterator;
return *this;
}
auto operator++(int)
{
auto temp = *this;
++m_dataIterator;
return temp;
}
auto operator*()
{
return std::tuple_cat(details::toTuple(m_dataIterator->first), std::make_tuple(m_dataIterator->second));
}
bool operator==(const iterator& other) const
{
return m_dataIterator == other.m_dataIterator;
}
bool operator!=(const iterator& other) const
{
return !(*this == other);
}
private:
DataIterator m_dataIterator{};
};
iterator begin()
{
return iterator(m_data.begin());
}
iterator end()
{
return iterator(m_data.end());
}
Matrix() = default;
Matrix(const Matrix& other)
: m_data{other.m_data}
{
}
Matrix(Matrix&& other) noexcept
{
m_data = std::move(other.m_data);
}
Matrix& operator=(const Matrix& other)
{
m_data = other.m_data;
return *this;
}
Matrix& operator=(Matrix&& other) noexcept
{
m_data = std::move(other.m_data);
return *this;
}
size_t size() const
{
return m_data.size();
}
auto operator[](size_t i)
{
const std::array<size_t, 1> index{i};
return Proxy<1>{index, m_data};
}
private:
Data m_data;
};
} // namespace