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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#pragma once
#include "debug.hpp"
namespace mm::iter {
template<typename T, std::size_t Rows, std::size_t Cols, class IterType, class Grid>
class vector_iterator;
template<typename T, std::size_t Rows, std::size_t Cols, class Grid>
class basic_iterator;
template<typename T, std::size_t N, class Grid>
class diag_iterator;
}
template<typename T, std::size_t Rows, std::size_t Cols, class IterType, class Grid>
class mm::iter::vector_iterator
{
public:
template<typename U, std::size_t R, std::size_t C, class G>
friend class mm::iter::basic_iterator;
template<typename U, std::size_t N, class G>
friend class mm::iter::diag_iterator;
vector_iterator(Grid& _M, std::size_t pos, std::size_t i = 0)
: M(_M), position(pos), index(i) {}
#ifdef MM_IMPLICIT_CONVERSION_ITERATOR
operator T&()
{
npdebug("Calling +")
return *(*this);
}
#endif
IterType operator++()
{
IterType it = cpy();
++index;
return it;
}
IterType operator--()
{
IterType it = cpy();
--index;
return it;
}
IterType& operator++(int)
{
++index;
return ref();
}
IterType& operator--(int)
{
--index;
return ref();
}
bool operator==(const IterType& other) const
{
return index == other.index;
}
bool operator!=(const IterType& other) const
{
return index != other.index;
}
bool ok() const
{
return index < size();
}
virtual std::size_t size() const = 0;
virtual T& operator*() = 0;
virtual T& operator[](std::size_t) = 0;
virtual T& operator[](std::size_t) const = 0;
IterType begin()
{
return IterType(M, position, 0);
}
virtual IterType end() = 0;
protected:
Grid& M; // grid mapping
const std::size_t position; // fixed index, negative too for diagonal iterator
std::size_t index; // variable index
virtual IterType& ref() = 0;
virtual IterType cpy() = 0;
};
/*
* Scalar product
*/
template<typename T,
std::size_t R1, std::size_t C1,
std::size_t R2, std::size_t C2,
class IterType1, class IterType2,
class Grid1, class Grid2>
typename std::remove_const<T>::type operator*(const mm::iter::vector_iterator<T, R1, C1, IterType1, Grid1>& v,
const mm::iter::vector_iterator<T, R2, C2, IterType2, Grid2>& w)
{
typename std::remove_const<T>::type out(0);
const std::size_t N = std::min(v.size(), w.size());
for(unsigned i = 0; i < N; ++i)
out += v[i] * w[i];
return out;
}
template<typename T, std::size_t Rows, std::size_t Cols, class Grid>
class mm::iter::basic_iterator : public mm::iter::vector_iterator<T, Rows, Cols, mm::iter::basic_iterator<T, Rows, Cols, Grid>, Grid>
{
bool direction;
virtual mm::iter::basic_iterator<T, Rows, Cols, Grid>& ref() override
{
return *this;
}
virtual mm::iter::basic_iterator<T, Rows, Cols, Grid> cpy() override
{
return *this;
}
public:
basic_iterator(Grid& A, std::size_t pos, std::size_t _index = 0, bool dir = true)
: mm::iter::vector_iterator<T, Rows, Cols, mm::iter::basic_iterator<T, Rows, Cols, Grid>, Grid>
(A, pos, _index), direction(dir)
{
//npdebug("Position: ", pos, ", Rows: ", Rows, " Cols: ", Cols, ", Direction: ", dir)
if (direction)
assert(pos < Rows);
else
assert(pos < Cols);
}
virtual std::size_t size() const
{
return (direction) ? Cols : Rows;
}
virtual T& operator*() override
{
return (direction) ?
this->M.data[this->position * Cols + this->index] :
this->M.data[this->index * Cols + this->position];
}
virtual T& operator[](std::size_t i) override
{
return (direction) ?
this->M.data[this->position * Cols + i] :
this->M.data[i * Cols + this->position];
}
virtual T& operator[](std::size_t i) const override
{
return (direction) ?
this->M.data[this->position * Cols + i] :
this->M.data[i * Cols + this->position];
}
virtual mm::iter::basic_iterator<T, Rows, Cols, Grid> end()
{
return mm::iter::basic_iterator<T, Rows, Cols, Grid>(this->M, this->position,
(direction) ? Cols : Rows);
}
};
template<typename T, std::size_t N, class Grid>
class mm::iter::diag_iterator : public mm::iter::vector_iterator<T, N, N, mm::iter::diag_iterator<T, N, Grid>, Grid>
{
bool sign;
virtual mm::iter::diag_iterator<T, N, Grid>& ref() override
{
return *this;
}
virtual mm::iter::diag_iterator<T, N, Grid> cpy() override
{
return *this;
}
public:
diag_iterator(Grid& A, signed long int pos, std::size_t _index = 0)
: mm::iter::vector_iterator<T, N, N, mm::iter::diag_iterator<T, N, Grid>, Grid>
(A, static_cast<std::size_t>(labs(pos)), _index), sign(pos >= 0)
{
assert(this->position < N);
}
virtual std::size_t size() const
{
return N - this->position;
}
virtual T& operator*() override
{
return (sign) ?
this->M.data[(this->index - this->position) * N + this->index] :
this->M.data[this->index * N + (this->index + this->position)];
}
virtual T& operator[](std::size_t i) override
{
return (sign) ?
this->M.data[(i - this->position) * N + i] :
this->M.data[i * N + (i + this->position)];
}
virtual T& operator[](std::size_t i) const override
{
return (sign) ?
this->M.data[(i - this->position) * N + i] :
this->M.data[i * N + (i + this->position)];
}
virtual mm::iter::diag_iterator<T, N, Grid> end()
{
return mm::iter::diag_iterator<T, N, Grid>(this->M, this->position, N);
}
};
|