-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter_range.h
103 lines (88 loc) · 2.56 KB
/
iter_range.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
/*
Copyright 2017 Krzysztof Karbowiak
*/
#ifndef ITER_RANGE_H__DDK
#define ITER_RANGE_H__DDK
#include <algorithm>
namespace iter
{
namespace detail
{
template<typename C>
class ranger
{
public:
class iterator
{
public:
iterator(C start, C stop, C step)
: mValue(start)
, mStop(stop)
, mStep(step)
{
}
auto operator!=(iterator const & other) const -> bool
{
return (mValue != other.mValue);
}
auto operator*() const -> C
{
return mValue;
}
auto operator++() -> iterator &
{
mValue = (mStop + mStep >= mStop)
? std::min(static_cast<C>(mValue + mStep), mStop)
: std::max(static_cast<C>(mValue + mStep), mStop);
return *this;
}
private:
C mValue;
C const mStop;
C const mStep;
};
public:
ranger(C start, C stop, C step)
: mStart(start)
, mStop(stop)
, mStep(step)
{
}
auto begin() const -> iterator
{
return iterator(mStart, mStop, mStep);
}
auto end() const -> iterator
{
return iterator(mStop, mStop, mStep);
}
private:
C const mStart;
C const mStop;
C const mStep;
};
}
}
namespace iter
{
////////////////////////////////////////////////////////////////////////////////
template<typename C, typename C1>
inline auto range(C start, C stop, C1 step) -> detail::ranger<C>
{
return detail::ranger<C>(start, stop, step);
}
////////////////////////////////////////////////////////////////////////////////
template<typename C>
inline auto range(C start, C stop) -> detail::ranger<C>
{
return range(start, stop, static_cast<C>(1));
}
////////////////////////////////////////////////////////////////////////////////
template<typename C>
inline auto range(C stop) -> detail::ranger<C>
{
return range(static_cast<C>(0), stop);
}
////////////////////////////////////////////////////////////////////////////////
}
#endif /* ITER_RANGE_H__DDK */