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_ZIP_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_ZIP_ITERATOR_HPP
17 #include <boost/config.hpp>
18 #include <boost/fusion/algorithm/iteration/for_each.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
20 #include <boost/mpl/back_inserter.hpp>
21 #include <boost/mpl/transform.hpp>
22 #include <boost/mpl/vector.hpp>
23 #include <boost/preprocessor/repetition.hpp>
24 #include <boost/tuple/tuple.hpp>
25 #include <boost/tuple/tuple_comparison.hpp>
27 #include <boost/compute/config.hpp>
28 #include <boost/compute/functional.hpp>
29 #include <boost/compute/detail/meta_kernel.hpp>
30 #include <boost/compute/detail/mpl_vector_to_tuple.hpp>
31 #include <boost/compute/types/tuple.hpp>
32 #include <boost/compute/type_traits/is_device_iterator.hpp>
33 #include <boost/compute/type_traits/type_name.hpp>
38 // forward declaration for zip_iterator
39 template<class IteratorTuple>
44 namespace mpl = boost::mpl;
46 // meta-function returning the value_type for an iterator
47 template<class Iterator>
48 struct make_iterator_value_type
50 typedef typename std::iterator_traits<Iterator>::value_type type;
53 // meta-function returning the value_type for a zip_iterator
54 template<class IteratorTuple>
55 struct make_zip_iterator_value_type
58 detail::mpl_vector_to_tuple<
59 typename mpl::transform<
61 make_iterator_value_type<mpl::_1>,
62 mpl::back_inserter<mpl::vector<> >
67 // helper class which defines the iterator_facade super-class
68 // type for zip_iterator
69 template<class IteratorTuple>
70 class zip_iterator_base
73 typedef ::boost::iterator_facade<
74 ::boost::compute::zip_iterator<IteratorTuple>,
75 typename make_zip_iterator_value_type<IteratorTuple>::type,
76 ::std::random_access_iterator_tag,
77 typename make_zip_iterator_value_type<IteratorTuple>::type
81 template<class IteratorTuple, class IndexExpr>
82 struct zip_iterator_index_expr
85 make_zip_iterator_value_type<IteratorTuple>::type
88 zip_iterator_index_expr(const IteratorTuple &iterators,
89 const IndexExpr &index_expr)
90 : m_iterators(iterators),
91 m_index_expr(index_expr)
95 IteratorTuple m_iterators;
96 IndexExpr m_index_expr;
100 #define BOOST_COMPUTE_PRINT_ELEM(z, n, unused) \
101 BOOST_PP_EXPR_IF(n, << ", ") \
102 << boost::get<n>(expr.m_iterators)[expr.m_index_expr]
105 #define BOOST_COMPUTE_PRINT_ZIP_IDX(z, n, unused) \
106 template<BOOST_PP_ENUM_PARAMS(n, class Iterator), class IndexExpr> \
107 inline meta_kernel& operator<<( \
108 meta_kernel &kernel, \
109 const zip_iterator_index_expr< \
110 boost::tuple<BOOST_PP_ENUM_PARAMS(n, Iterator)>, \
115 boost::tuple<BOOST_PP_ENUM_PARAMS(n, Iterator)> \
118 make_zip_iterator_value_type<tuple_type>::type \
120 kernel.inject_type<value_type>(); \
122 << "(" << type_name<value_type>() << ")" \
124 BOOST_PP_REPEAT(n, BOOST_COMPUTE_PRINT_ELEM, ~) \
128 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_PRINT_ZIP_IDX, ~)
130 #undef BOOST_COMPUTE_PRINT_ZIP_IDX
131 #undef BOOST_COMPUTE_PRINT_ELEM
133 struct iterator_advancer
135 iterator_advancer(size_t n)
140 template<class Iterator>
141 void operator()(Iterator &i) const
143 std::advance(i, m_distance);
149 template<class Iterator>
150 void increment_iterator(Iterator &i)
155 template<class Iterator>
156 void decrement_iterator(Iterator &i)
161 } // end detail namespace
163 /// \class zip_iterator
164 /// \brief A zip iterator adaptor.
166 /// The zip_iterator class combines values from multiple input iterators. When
167 /// dereferenced it returns a tuple containing each value at the current
168 /// position in each input range.
170 /// \see make_zip_iterator()
171 template<class IteratorTuple>
172 class zip_iterator : public detail::zip_iterator_base<IteratorTuple>::type
176 detail::zip_iterator_base<IteratorTuple>::type
178 typedef typename super_type::value_type value_type;
179 typedef typename super_type::reference reference;
180 typedef typename super_type::difference_type difference_type;
181 typedef IteratorTuple iterator_tuple;
183 zip_iterator(IteratorTuple iterators)
184 : m_iterators(iterators)
188 zip_iterator(const zip_iterator<IteratorTuple> &other)
189 : m_iterators(other.m_iterators)
193 zip_iterator<IteratorTuple>&
194 operator=(const zip_iterator<IteratorTuple> &other)
197 super_type::operator=(other);
199 m_iterators = other.m_iterators;
209 const IteratorTuple& get_iterator_tuple() const
214 template<class IndexExpression>
215 detail::zip_iterator_index_expr<IteratorTuple, IndexExpression>
216 operator[](const IndexExpression &expr) const
218 return detail::zip_iterator_index_expr<IteratorTuple,
219 IndexExpression>(m_iterators,
224 friend class ::boost::iterator_core_access;
226 reference dereference() const
231 bool equal(const zip_iterator<IteratorTuple> &other) const
233 return m_iterators == other.m_iterators;
238 boost::fusion::for_each(m_iterators, detail::increment_iterator);
243 boost::fusion::for_each(m_iterators, detail::decrement_iterator);
246 void advance(difference_type n)
248 boost::fusion::for_each(m_iterators, detail::iterator_advancer(n));
251 difference_type distance_to(const zip_iterator<IteratorTuple> &other) const
253 return std::distance(boost::get<0>(m_iterators),
254 boost::get<0>(other.m_iterators));
258 IteratorTuple m_iterators;
261 /// Creates a zip_iterator for \p iterators.
263 /// \param iterators a tuple of input iterators to zip together
265 /// \return a \c zip_iterator for \p iterators
267 /// For example, to zip together iterators from three vectors (\c a, \c b, and
270 /// auto zipped = boost::compute::make_zip_iterator(
271 /// boost::make_tuple(a.begin(), b.begin(), c.begin())
274 template<class IteratorTuple>
275 inline zip_iterator<IteratorTuple>
276 make_zip_iterator(IteratorTuple iterators)
278 return zip_iterator<IteratorTuple>(iterators);
281 /// \internal_ (is_device_iterator specialization for zip_iterator)
282 template<class IteratorTuple>
283 struct is_device_iterator<zip_iterator<IteratorTuple> > : boost::true_type {};
287 // get<N>() specialization for zip_iterator
289 #define BOOST_COMPUTE_ZIP_GET_N(z, n, unused) \
290 template<size_t N, class IteratorTuple, class IndexExpr, \
291 BOOST_PP_ENUM_PARAMS(n, class T)> \
292 inline meta_kernel& \
293 operator<<(meta_kernel &kernel, \
296 zip_iterator_index_expr<IteratorTuple, IndexExpr>, \
297 boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> \
300 typedef typename boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> Tuple; \
301 typedef typename boost::tuples::element<N, Tuple>::type T; \
302 BOOST_STATIC_ASSERT(N < size_t(boost::tuples::length<Tuple>::value)); \
303 kernel.inject_type<T>(); \
305 << boost::get<N>(expr.m_arg.m_iterators)[expr.m_arg.m_index_expr]; \
308 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_ZIP_GET_N, ~)
310 #undef BOOST_COMPUTE_ZIP_GET_N
312 } // end detail namespace
313 } // end compute namespace
314 } // end boost namespace
316 #endif // BOOST_COMPUTE_ITERATOR_ZIP_ITERATOR_HPP