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
|
#pragma once
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) {}
operator T&()
{
return *(*this);
}
IterType operator++()
{
IterType it = *this;
++index;
return it;
}
IterType operator--()
{
IterType it = *this;
--index;
return it;
}
IterType& operator++(int)
{
++index;
return *this;
}
IterType& operator--(int)
{
--index;
return *this;
}
bool operator==(const IterType& other) const
{
return index == other.index;
}
bool operator!=(const IterType& other) const
{
return index != other.index;
}
virtual bool ok() const = 0;
virtual T& operator*() = 0;
virtual T& operator[](std::size_t) = 0;
IterType begin()
{
return IterType(M, position, 0);
}
virtual IterType end() = 0;
/*
* Scalar product
*/
template<std::size_t P>
T operator*(const mm::iter::vector_iterator<T, Rows, P, IterType, Grid>& v)
{
T out(0);
for (unsigned k(0); k < Rows; ++k)
out += (*this)[k] * v[k];
return out;
}
template<std::size_t P>
T operator*(const mm::iter::vector_iterator<T, P, Cols, IterType, Grid>& v)
{
T out(0);
for (unsigned k(0); k < Cols; ++k)
out += (*this)[k] * v[k];
return out;
}
protected:
Grid& M; // grid mapping
const std::size_t position; // fixed index, negative too for diagonal iterator
std::size_t index; // variable index
};
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;
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)
{
if (direction)
assert(pos < Rows);
else
assert(pos < Cols);
}
virtual bool ok() const override
{
return (direction) ? this->index < Cols : this->index < 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 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;
public:
diag_iterator(Grid& A, signed long 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>(abs(pos)), _index), sign(pos >= 0)
{
assert(this->position < N);
}
virtual bool ok() const
{
return this->index < N;
}
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 mm::iter::diag_iterator<T, N, Grid> end()
{
return mm::iter::diag_iterator<T, N, Grid>(this->M, this->position, N);
}
};
|