-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.h
More file actions
94 lines (79 loc) · 2.19 KB
/
iterator.h
File metadata and controls
94 lines (79 loc) · 2.19 KB
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
#pragma once
#include <iterator>
#include <utility>
namespace iterator
{
// the class copies some ideas from boost function_input_iterator
// but you can specify a stop value here
template<class F, class S>
class ifunction : public std::iterator<std::input_iterator_tag, decltype(std::declval<F>()())>
{
public:
using function_type = F;
using state_type = S;
using value_type = typename ifunction<F, S>::value_type;
ifunction(function_type& f, const state_type& s, const value_type& v)
: function(&f)
, state(s)
, value(v)
{
}
const value_type& operator*() const
{
return value;
}
const value_type* operator->() const
{
return &**this;
}
ifunction& operator++()
{
value = (*function)();
++state;
return *this;
}
ifunction operator++(int)
{
ifunction result(*this);
++*this;
return result;
}
bool operator==(const ifunction& right) const
{
return function == right.function && (state == right.state || value == right.value);
}
bool operator!=(const ifunction& right) const
{
return !(*this == right);
}
private:
function_type* function;
state_type state;
value_type value;
};
struct infinite
{
infinite& operator++()
{
return *this;
}
infinite& operator++(int)
{
return *this;
}
bool operator==(const infinite&) const
{
return false;
}
};
template<class F, class S = infinite>
inline auto ifunction_begin(F& f, const S& s = S())
{
return ifunction<F, S>(f, s, f());
}
template<class F, class S = infinite, class V = typename ifunction<F, S>::value_type>
inline auto ifunction_end(F& f, const S& s = S(), const V& v = V())
{
return ifunction<F, S>(f, s, v);
}
}