-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.hpp
71 lines (60 loc) · 1.59 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
#pragma once
#include <iterator>
#include <algorithm>
template<class V> struct default_step
{
void operator()(V& value) const { value++; }
};
template<int v> struct int_step
{
template<class V>
void operator()(V& value) const { value+=v; }
};
template<class V> struct step
{
V s;
void operator()(V& value) const { value+=s; }
step(V step_) : s(step_) {}
};
template<class V, typename Step = default_step<V>, typename Compare = std::less<V>> class range
{
V left;
V right;
Compare comp;
Step next;
public:
struct Sentinel
{
V value;
};
class Iterator : public std::iterator<std::forward_iterator_tag, V, V>
{
V value;
Compare comp;
Step next;
public:
Iterator(V val, Compare c, Step n) : value(val), comp(c), next(n) {}
Iterator(const Iterator& i) : value(i.value), comp(i.comp), next(i.next) {}
V operator*() { return value; }
Iterator operator++() { next(value); return *this; }
Iterator operator++(int) { Iterator i = *this; next(value); return i; }
bool operator!=(const Sentinel& i) const
{
return comp(value, i.value);
}
bool operator==(const Sentinel& i) const
{
return !((*this) != i);
}
};
range(const range& r) : left(r.left, r.right) {}
range(V l, V r, Step s = {}, Compare c = {}) : left(l), right(r), next(s), comp(c) {}
Iterator begin() const
{
return Iterator(left, comp, next);
}
Sentinel end() const
{
return Sentinel{ right };
}
};