1 | // Copyright 2009 (C) Dean Michael Berris <me@deanberris.com>
|
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See
|
---|
3 | // accompanying file LICENSE_1_0.txt or copy at
|
---|
4 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
5 | //
|
---|
6 |
|
---|
7 | #ifndef BOOST_FUNCTION_INPUT_ITERATOR
|
---|
8 | #define BOOST_FUNCTION_INPUT_ITERATOR
|
---|
9 |
|
---|
10 | #include <boost/iterator/iterator_facade.hpp>
|
---|
11 |
|
---|
12 | namespace boost {
|
---|
13 |
|
---|
14 | template <class Function, class Input>
|
---|
15 | class function_input_iterator
|
---|
16 | : public iterator_facade<
|
---|
17 | function_input_iterator<Function, Input>,
|
---|
18 | typename Function::result_type,
|
---|
19 | single_pass_traversal_tag,
|
---|
20 | typename Function::result_type const &
|
---|
21 | >
|
---|
22 | {
|
---|
23 | public:
|
---|
24 | function_input_iterator() {}
|
---|
25 | function_input_iterator(Function * f_, Input state_ = Input())
|
---|
26 | : f(f_), state(state_), value((*f)()) {}
|
---|
27 |
|
---|
28 | void increment() {
|
---|
29 | value = (*f)();
|
---|
30 | ++state;
|
---|
31 | }
|
---|
32 |
|
---|
33 | typename Function::result_type const &
|
---|
34 | dereference() const {
|
---|
35 | return value;
|
---|
36 | }
|
---|
37 |
|
---|
38 | bool equal(function_input_iterator const & other) const {
|
---|
39 | return f == other.f && state == other.state;
|
---|
40 | }
|
---|
41 |
|
---|
42 | private:
|
---|
43 | Function * f;
|
---|
44 | Input state;
|
---|
45 | typename Function::result_type value;
|
---|
46 | };
|
---|
47 |
|
---|
48 | template <class Function, class Input>
|
---|
49 | inline function_input_iterator<Function, Input>
|
---|
50 | make_function_input_iterator(Function & f, Input state) {
|
---|
51 | typedef function_input_iterator<Function, Input> result_t;
|
---|
52 | return result_t(&f, state);
|
---|
53 | }
|
---|
54 |
|
---|
55 | struct infinite {
|
---|
56 | infinite & operator++() { return *this; }
|
---|
57 | infinite & operator++(int) { return *this; }
|
---|
58 | bool operator==(infinite &) const { return false; };
|
---|
59 | bool operator==(infinite const &) const { return false; };
|
---|
60 | };
|
---|
61 | }
|
---|
62 |
|
---|
63 | #endif
|
---|
64 |
|
---|