1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP
17 #include <boost/config.hpp>
18 #include <boost/iterator/iterator_facade.hpp>
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>
27 // forward declaration for function_input_iterator<Function>
28 template<class Function> class function_input_iterator;
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
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
46 template<class Function>
47 struct function_input_iterator_expr
49 typedef typename ::boost::compute::result_of<Function()>::type result_type;
51 function_input_iterator_expr(const Function &function)
52 : m_function(function)
59 template<class Function>
60 inline meta_kernel& operator<<(meta_kernel &kernel,
61 const function_input_iterator_expr<Function> &expr)
63 return kernel << expr.m_function();
66 } // end detail namespace
68 /// \class function_input_iterator
69 /// \brief Iterator which returns the result of a function when dereferenced
73 /// \snippet test/test_function_input_iterator.cpp generate_42
75 /// \see make_function_input_iterator()
76 template<class Function>
77 class function_input_iterator :
78 public detail::function_input_iterator_base<Function>::type
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;
86 function_input_iterator(const Function &function, size_t index = 0)
87 : m_function(function),
92 function_input_iterator(const function_input_iterator<Function> &other)
93 : m_function(other.m_function),
94 m_index(other.m_index)
98 function_input_iterator<Function>&
99 operator=(const function_input_iterator<Function> &other)
102 m_function = other.m_function;
103 m_index = other.m_index;
109 ~function_input_iterator()
113 size_t get_index() const
119 detail::function_input_iterator_expr<Function>
120 operator[](const Expr &expr) const
124 return detail::function_input_iterator_expr<Function>(m_function);
128 friend class ::boost::iterator_core_access;
130 reference dereference() const
135 bool equal(const function_input_iterator<Function> &other) const
137 return m_function == other.m_function && m_index == other.m_index;
150 void advance(difference_type n)
152 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
156 distance_to(const function_input_iterator<Function> &other) const
158 return static_cast<difference_type>(other.m_index - m_index);
166 /// Returns a function_input_iterator with \p function.
168 /// \param function function to execute when dereferenced
169 /// \param index index of the iterator
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)
176 return function_input_iterator<Function>(function, index);
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 {};
183 } // end compute namespace
184 } // end boost namespace
186 #endif // BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP