-
Notifications
You must be signed in to change notification settings - Fork 38
/
range.hpp
231 lines (169 loc) · 5.41 KB
/
range.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
#ifndef UTIL_LANG_RANGE_HPP
#define UTIL_LANG_RANGE_HPP
#include <cmath>
#include <iterator>
#include <type_traits>
namespace util { namespace lang {
namespace detail {
template <typename T>
struct range_iter_base : std::iterator<std::input_iterator_tag, T> {
range_iter_base(T current) : current(current) { }
T operator *() const { return current; }
T const* operator ->() const { return ¤t; }
range_iter_base& operator ++() {
++current;
return *this;
}
range_iter_base operator ++(int) {
auto copy = *this;
++*this;
return copy;
}
bool operator ==(range_iter_base const& other) const {
return current == other.current;
}
bool operator !=(range_iter_base const& other) const {
return not (*this == other);
}
protected:
T current;
};
} // namespace detail
template <typename T>
struct step_range_proxy {
struct iterator : detail::range_iter_base<T> {
iterator(T current, T step)
: detail::range_iter_base<T>(current), step_(step) { }
using detail::range_iter_base<T>::current;
iterator& operator ++() {
current += step_;
return *this;
}
iterator operator ++(int) {
auto copy = *this;
++*this;
return copy;
}
// Loses commutativity. Iterator-based ranges are simply broken. :-(
bool operator ==(iterator const& other) const {
return step_ > 0 ? current >= other.current
: current < other.current;
}
bool operator !=(iterator const& other) const {
return not (*this == other);
}
T step_;
};
step_range_proxy(T begin, T end, T step)
: begin_(begin, step), end_(end, step) { }
iterator begin() const { return begin_; }
iterator end() const { return end_; }
std::size_t size() const {
if (*end_ >= *begin_) {
// Increasing and empty range
if (begin_.step_ < T{0}) return 0;
} else {
// Decreasing range
if (begin_.step_ > T{0}) return 0;
}
return std::ceil(std::abs(static_cast<double>(*end_ - *begin_) / begin_.step_));
}
private:
iterator begin_;
iterator end_;
};
template <typename T>
struct range_proxy {
struct iterator : detail::range_iter_base<T> {
iterator(T current) : detail::range_iter_base<T>(current) { }
};
range_proxy(T begin, T end) : begin_(begin), end_(end) { }
step_range_proxy<T> step(T step) {
return {*begin_, *end_, step};
}
iterator begin() const { return begin_; }
iterator end() const { return end_; }
std::size_t size() const { return *end_ - *begin_; }
private:
iterator begin_;
iterator end_;
};
template <typename T>
struct step_inf_range_proxy {
struct iterator : detail::range_iter_base<T> {
iterator(T current = T(), T step = T())
: detail::range_iter_base<T>(current), step(step) { }
using detail::range_iter_base<T>::current;
iterator& operator ++() {
current += step;
return *this;
}
iterator operator ++(int) {
auto copy = *this;
++*this;
return copy;
}
bool operator ==(iterator const&) const { return false; }
bool operator !=(iterator const&) const { return true; }
private:
T step;
};
step_inf_range_proxy(T begin, T step) : begin_(begin, step) { }
iterator begin() const { return begin_; }
iterator end() const { return iterator(); }
private:
iterator begin_;
};
template <typename T>
struct infinite_range_proxy {
struct iterator : detail::range_iter_base<T> {
iterator(T current = T()) : detail::range_iter_base<T>(current) { }
bool operator ==(iterator const&) const { return false; }
bool operator !=(iterator const&) const { return true; }
};
infinite_range_proxy(T begin) : begin_(begin) { }
step_inf_range_proxy<T> step(T step) {
return {*begin_, step};
}
iterator begin() const { return begin_; }
iterator end() const { return iterator(); }
private:
iterator begin_;
};
template <typename T, typename U>
auto range(T begin, U end) -> range_proxy<typename std::common_type<T, U>::type> {
using C = typename std::common_type<T, U>::type;
return {static_cast<C>(begin), static_cast<C>(end)};
}
template <typename T>
infinite_range_proxy<T> range(T begin) {
return {begin};
}
namespace traits {
template <typename C>
struct has_size {
template <typename T>
static auto check(T*) ->
typename std::is_integral<
decltype(std::declval<T const>().size())>::type;
template <typename>
static auto check(...) -> std::false_type;
using type = decltype(check<C>(0));
static constexpr bool value = type::value;
};
} // namespace traits
template <typename C, typename = typename std::enable_if<traits::has_size<C>::value>>
auto indices(C const& cont) -> range_proxy<decltype(cont.size())> {
return {0, cont.size()};
}
template <typename T, std::size_t N>
range_proxy<std::size_t> indices(T (&)[N]) {
return {0, N};
}
template <typename T>
range_proxy<typename std::initializer_list<T>::size_type>
indices(std::initializer_list<T>&& cont) {
return {0, cont.size()};
}
} } // namespace util::lang
#endif // ndef UTIL_LANG_RANGE_HPP