]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //---------------------------------------------------------------------------// |
2 | // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com> | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0 | |
5 | // See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt | |
7 | // | |
8 | // See http://boostorg.github.com/compute for more information. | |
9 | //---------------------------------------------------------------------------// | |
10 | ||
11 | #ifndef BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP | |
12 | #define BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP | |
13 | ||
14 | #include <cstddef> | |
15 | #include <iterator> | |
16 | ||
17 | #include <boost/config.hpp> | |
18 | #include <boost/iterator/iterator_facade.hpp> | |
19 | ||
20 | #include <boost/compute/detail/meta_kernel.hpp> | |
21 | #include <boost/compute/type_traits/is_device_iterator.hpp> | |
22 | #include <boost/compute/type_traits/result_of.hpp> | |
23 | ||
24 | namespace boost { | |
25 | namespace compute { | |
26 | ||
27 | // forward declaration for function_input_iterator<Function> | |
28 | template<class Function> class function_input_iterator; | |
29 | ||
30 | namespace detail { | |
31 | ||
32 | // helper class which defines the iterator_facade super-class | |
33 | // type for function_input_iterator<Function> | |
34 | template<class Function> | |
35 | class function_input_iterator_base | |
36 | { | |
37 | public: | |
38 | typedef ::boost::iterator_facade< | |
39 | ::boost::compute::function_input_iterator<Function>, | |
40 | typename ::boost::compute::result_of<Function()>::type, | |
41 | ::std::random_access_iterator_tag, | |
42 | typename ::boost::compute::result_of<Function()>::type | |
43 | > type; | |
44 | }; | |
45 | ||
46 | template<class Function> | |
47 | struct function_input_iterator_expr | |
48 | { | |
49 | typedef typename ::boost::compute::result_of<Function()>::type result_type; | |
50 | ||
51 | function_input_iterator_expr(const Function &function) | |
52 | : m_function(function) | |
53 | { | |
54 | } | |
55 | ||
56 | Function m_function; | |
57 | }; | |
58 | ||
59 | template<class Function> | |
60 | inline meta_kernel& operator<<(meta_kernel &kernel, | |
61 | const function_input_iterator_expr<Function> &expr) | |
62 | { | |
63 | return kernel << expr.m_function(); | |
64 | } | |
65 | ||
66 | } // end detail namespace | |
67 | ||
68 | /// \class function_input_iterator | |
69 | /// \brief Iterator which returns the result of a function when dereferenced | |
70 | /// | |
71 | /// For example: | |
72 | /// | |
73 | /// \snippet test/test_function_input_iterator.cpp generate_42 | |
74 | /// | |
75 | /// \see make_function_input_iterator() | |
76 | template<class Function> | |
77 | class function_input_iterator : | |
78 | public detail::function_input_iterator_base<Function>::type | |
79 | { | |
80 | public: | |
81 | typedef typename detail::function_input_iterator_base<Function>::type super_type; | |
82 | typedef typename super_type::reference reference; | |
83 | typedef typename super_type::difference_type difference_type; | |
84 | typedef Function function; | |
85 | ||
86 | function_input_iterator(const Function &function, size_t index = 0) | |
87 | : m_function(function), | |
88 | m_index(index) | |
89 | { | |
90 | } | |
91 | ||
92 | function_input_iterator(const function_input_iterator<Function> &other) | |
93 | : m_function(other.m_function), | |
94 | m_index(other.m_index) | |
95 | { | |
96 | } | |
97 | ||
98 | function_input_iterator<Function>& | |
99 | operator=(const function_input_iterator<Function> &other) | |
100 | { | |
101 | if(this != &other){ | |
102 | m_function = other.m_function; | |
103 | m_index = other.m_index; | |
104 | } | |
105 | ||
106 | return *this; | |
107 | } | |
108 | ||
109 | ~function_input_iterator() | |
110 | { | |
111 | } | |
112 | ||
113 | size_t get_index() const | |
114 | { | |
115 | return m_index; | |
116 | } | |
117 | ||
118 | template<class Expr> | |
119 | detail::function_input_iterator_expr<Function> | |
120 | operator[](const Expr &expr) const | |
121 | { | |
122 | (void) expr; | |
123 | ||
124 | return detail::function_input_iterator_expr<Function>(m_function); | |
125 | } | |
126 | ||
127 | private: | |
128 | friend class ::boost::iterator_core_access; | |
129 | ||
130 | reference dereference() const | |
131 | { | |
132 | return reference(); | |
133 | } | |
134 | ||
135 | bool equal(const function_input_iterator<Function> &other) const | |
136 | { | |
137 | return m_function == other.m_function && m_index == other.m_index; | |
138 | } | |
139 | ||
140 | void increment() | |
141 | { | |
142 | m_index++; | |
143 | } | |
144 | ||
145 | void decrement() | |
146 | { | |
147 | m_index--; | |
148 | } | |
149 | ||
150 | void advance(difference_type n) | |
151 | { | |
152 | m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n); | |
153 | } | |
154 | ||
155 | difference_type | |
156 | distance_to(const function_input_iterator<Function> &other) const | |
157 | { | |
158 | return static_cast<difference_type>(other.m_index - m_index); | |
159 | } | |
160 | ||
161 | private: | |
162 | Function m_function; | |
163 | size_t m_index; | |
164 | }; | |
165 | ||
166 | /// Returns a function_input_iterator with \p function. | |
167 | /// | |
168 | /// \param function function to execute when dereferenced | |
169 | /// \param index index of the iterator | |
170 | /// | |
171 | /// \return a \c function_input_iterator with \p function | |
172 | template<class Function> | |
173 | inline function_input_iterator<Function> | |
174 | make_function_input_iterator(const Function &function, size_t index = 0) | |
175 | { | |
176 | return function_input_iterator<Function>(function, index); | |
177 | } | |
178 | ||
179 | /// \internal_ (is_device_iterator specialization for function_input_iterator) | |
180 | template<class Function> | |
181 | struct is_device_iterator<function_input_iterator<Function> > : boost::true_type {}; | |
182 | ||
183 | } // end compute namespace | |
184 | } // end boost namespace | |
185 | ||
186 | #endif // BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP |