-
Notifications
You must be signed in to change notification settings - Fork 6
/
stride_iterator.hpp
376 lines (329 loc) · 10.8 KB
/
stride_iterator.hpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* @file stride_iterator.hpp
* @brief
* @date 2020-03-17
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_ITERATOR_STRIDE_ITERATOR_HPP
#define KERBAL_ITERATOR_STRIDE_ITERATOR_HPP
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/iterator/iterator.hpp>
#include <kerbal/iterator/iterator_traits.hpp>
#include <kerbal/operators/addable.hpp>
#include <kerbal/operators/dereferenceable.hpp>
#include <kerbal/operators/incr_decr.hpp>
#include <kerbal/operators/subtractable.hpp>
namespace kerbal
{
namespace iterator
{
template <typename Iter>
class stride_iterator;
namespace detail
{
template <typename Iter, typename Tag>
class stride_iterator_base;
template <typename Iter>
class stride_iterator_base<Iter, std::input_iterator_tag> :
public kerbal::operators::dereferenceable<
kerbal::iterator::stride_iterator<Iter>,
typename kerbal::iterator::iterator_traits<Iter>::pointer
>, // it->
public kerbal::operators::incrementable<
kerbal::iterator::stride_iterator<Iter>
> // it++
{
private:
typedef kerbal::iterator::stride_iterator<Iter> stride_iterator;
typedef kerbal::iterator::iterator_traits<Iter> base_iterator_traits;
protected:
typedef std::input_iterator_tag iterator_category;
typedef typename base_iterator_traits::value_type value_type;
typedef typename base_iterator_traits::difference_type difference_type;
typedef typename base_iterator_traits::pointer pointer;
typedef typename base_iterator_traits::reference reference;
protected:
difference_type stride;
difference_type out;
Iter current;
Iter end;
public:
KERBAL_CONSTEXPR
explicit stride_iterator_base(
const Iter & current, const Iter & end,
const difference_type & stride, const difference_type & out = 0
) :
stride(stride), out(out), current(current), end(end)
{
}
KERBAL_CONSTEXPR
explicit stride_iterator_base(
const Iter &, const Iter & current, const Iter & end,
const difference_type & stride, const difference_type & out = 0
) :
stride(stride), out(out), current(current), end(end)
{
}
KERBAL_CONSTEXPR14
reference operator*() const
{
return *current;
}
protected:
KERBAL_CONSTEXPR14
void advance(difference_type times = 1)
{
difference_type strides = times * this->stride;
if (this->out <= -strides || this->out > 0) {
this->out += strides;
} else { // -stride < out <= 0
difference_type offset = strides;
if (this->out != 0) {
offset = strides + this->out;
this->out = 0;
}
difference_type move_actual(kerbal::iterator::advance_at_most(this->current, offset, this->end));
this->out += offset - move_actual;
}
}
public:
KERBAL_CONSTEXPR14
stride_iterator & operator++()
{
this->advance();
return static_cast<stride_iterator &>(*this);
}
KERBAL_CONSTEXPR
friend
bool operator==(const stride_iterator & lhs, const stride_iterator & rhs)
{
return
static_cast<bool>(lhs.current == rhs.current) &&
static_cast<bool>(lhs.out == rhs.out);
}
KERBAL_CONSTEXPR
friend
bool operator!=(const stride_iterator & lhs, const stride_iterator & rhs)
{
return
static_cast<bool>(lhs.current != rhs.current) ||
static_cast<bool>(lhs.out != rhs.out);
}
};
template <typename Iter>
class stride_iterator_base<Iter, std::bidirectional_iterator_tag> :
public stride_iterator_base<Iter, std::input_iterator_tag>,
public kerbal::operators::decrementable<
kerbal::iterator::stride_iterator<Iter>
> // it--
{
private:
typedef stride_iterator_base<Iter, std::input_iterator_tag> super;
typedef kerbal::iterator::stride_iterator<Iter> stride_iterator;
typedef kerbal::iterator::iterator_traits<Iter> base_iterator_traits;
protected:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename base_iterator_traits::value_type value_type;
typedef typename base_iterator_traits::difference_type difference_type;
typedef typename base_iterator_traits::pointer pointer;
typedef typename base_iterator_traits::reference reference;
protected:
Iter begin;
public:
KERBAL_CONSTEXPR
explicit stride_iterator_base(
const Iter & begin, const Iter & current, const Iter & end,
const difference_type & stride, const difference_type & out = 0) :
super(current, end, stride, out), begin(begin)
{
}
protected:
KERBAL_CONSTEXPR14
void retreat(difference_type times = 1)
{
difference_type strides = times * this->stride;
if (this->out >= strides || this->out < 0) {
this->out -= strides;
} else { // 0 <= out < stride
difference_type offset = strides;
if (this->out != 0) {
offset = strides - this->out;
this->out = 0;
}
difference_type move_actual(kerbal::iterator::retreat_at_most(this->current, offset, this->begin));
this->out -= offset - move_actual;
}
}
public:
KERBAL_CONSTEXPR14
stride_iterator & operator--()
{
this->retreat();
return static_cast<stride_iterator &>(*this);
}
};
template <typename Iter>
class stride_iterator_base<Iter, std::random_access_iterator_tag> :
public stride_iterator_base<Iter, std::bidirectional_iterator_tag>,
public kerbal::operators::addable<
kerbal::iterator::stride_iterator<Iter>,
typename kerbal::iterator::iterator_traits<Iter>::difference_type
>, // it + N
public kerbal::operators::addable_left<
kerbal::iterator::stride_iterator<Iter>,
typename kerbal::iterator::iterator_traits<Iter>::difference_type
>, // N + it
public kerbal::operators::subtractable<
kerbal::iterator::stride_iterator<Iter>,
typename kerbal::iterator::iterator_traits<Iter>::difference_type
> // it - N
{
private:
typedef stride_iterator_base<Iter, std::bidirectional_iterator_tag> super;
typedef kerbal::iterator::stride_iterator<Iter> stride_iterator;
typedef kerbal::iterator::iterator_traits<Iter> base_iterator_traits;
protected:
typedef std::random_access_iterator_tag iterator_category;
typedef typename base_iterator_traits::value_type value_type;
typedef typename base_iterator_traits::difference_type difference_type;
typedef typename base_iterator_traits::pointer pointer;
typedef typename base_iterator_traits::reference reference;
public:
KERBAL_CONSTEXPR
explicit stride_iterator_base(
const Iter & begin, const Iter & current, const Iter & end,
const difference_type & stride, const difference_type & out = 0
) :
super(begin, current, end, stride, out)
{
}
/*
* @precondition lhs.stride == rhs.stride
*/
KERBAL_CONSTEXPR14
friend
difference_type
operator-(const stride_iterator & lhs, const stride_iterator & rhs)
{
difference_type dist(kerbal::iterator::distance(rhs.current, lhs.current) - rhs.out + lhs.out);
dist /= lhs.stride;
return dist;
}
KERBAL_CONSTEXPR14
stride_iterator & operator+=(const difference_type & delta)
{
this->advance(delta);
return static_cast<stride_iterator &>(*this);
}
KERBAL_CONSTEXPR14
stride_iterator & operator-=(const difference_type & delta)
{
this->retreat(delta);
return static_cast<stride_iterator &>(*this);
}
KERBAL_CONSTEXPR14
reference operator[](const difference_type & dist) const
{
return *(static_cast<const stride_iterator &>(*this) + dist);
}
KERBAL_CONSTEXPR14
friend
bool operator<(const stride_iterator & lhs, const stride_iterator & rhs)
{
if (lhs.current < rhs.current) {
return true;
} else if (lhs.current > rhs.current) {
return false;
}
return lhs.out < rhs.out;
}
KERBAL_CONSTEXPR14
friend
bool operator<=(const stride_iterator & lhs, const stride_iterator & rhs)
{
if (lhs.current < rhs.current) {
return true;
} else if (lhs.current > rhs.current) {
return false;
}
return lhs.out <= rhs.out;
}
KERBAL_CONSTEXPR14
friend
bool operator>(const stride_iterator & lhs, const stride_iterator & rhs)
{
if (lhs.current < rhs.current) {
return false;
} else if (lhs.current > rhs.current) {
return true;
}
return lhs.out > rhs.out;
}
KERBAL_CONSTEXPR14
friend
bool operator>=(const stride_iterator & lhs, const stride_iterator & rhs)
{
if (lhs.current < rhs.current) {
return false;
} else if (lhs.current > rhs.current) {
return true;
}
return lhs.out >= rhs.out;
}
};
} // namespace detail
template <typename Iter>
class stride_iterator :
public kerbal::iterator::detail::stride_iterator_base<
Iter,
typename kerbal::iterator::iterator_traits<Iter>::iterator_category
>
{
private:
typedef kerbal::iterator::detail::stride_iterator_base<
Iter,
typename kerbal::iterator::iterator_traits<Iter>::iterator_category
> super;
public:
typedef typename super::iterator_category iterator_category;
typedef typename super::value_type value_type;
typedef typename super::difference_type difference_type;
typedef typename super::pointer pointer;
typedef typename super::reference reference;
public:
KERBAL_CONSTEXPR
explicit stride_iterator(
const Iter & begin, const Iter & current, const Iter & end,
const difference_type & stride, const difference_type & out = 0
) :
super(begin, current, end, stride, out)
{
}
};
# if __cplusplus >= 201703L
template <typename Iter>
stride_iterator(
Iter, Iter, Iter,
const typename kerbal::iterator::iterator_traits<Iter>::difference_type &,
const typename kerbal::iterator::iterator_traits<Iter>::difference_type & = 0
)->
stride_iterator<Iter>;
# endif
template <typename Iter>
KERBAL_CONSTEXPR
stride_iterator<Iter>
make_stride_iterator(
Iter begin, Iter current, Iter end,
const typename kerbal::iterator::iterator_traits<Iter>::difference_type & stride,
const typename kerbal::iterator::iterator_traits<Iter>::difference_type & out = 0
)
{
return stride_iterator<Iter>(begin, current, end, stride, out);
}
} // namespace iterator
} // namespace kerbal
#endif // KERBAL_ITERATOR_STRIDE_ITERATOR_HPP