-
Notifications
You must be signed in to change notification settings - Fork 4
/
range.cpp
50 lines (42 loc) · 1.28 KB
/
range.cpp
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
// Python-like simple range in C++14
#include <iostream>
#include <type_traits>
template <typename T>
class Range {
private:
T first, last, iter, step;
public:
Range(T first, T last, T step = 1)
: first(first), last(last), iter(first), step(step) {}
// Iterable functions
const Range& begin() const noexcept { return *this; }
const Range& end() const noexcept { return *this; }
const Range& cbegin() const noexcept { return begin(); }
const Range& cend() const noexcept { return end(); }
// Iterator functions
bool operator!=(const Range&) const noexcept { return iter < last; }
Range& operator++() noexcept {
iter += step;
return *this;
}
Range operator++(int) noexcept {
Range cpy{*this};
this->operator++();
return cpy;
}
T operator*() const noexcept { return iter; }
};
// Convenience function for automatic type deduction
template <typename T>
auto make_range(T fist, T last, T step = 1) {
return Range<T>{fist, last, step};
}
int main() {
for (auto i : make_range(10, 20, 2)) {
std::cout << i << std::endl;
}
auto it = make_range(-0.5, 0.5, 0.1);
std::cout << *(it++) << std::endl;
std::cout << *(++it) << std::endl;
std::cout << *it << std::endl;
}