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_COUNTING_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
18 #include <boost/config.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
27 // forward declaration for counting_iterator<T>
28 template<class T> class counting_iterator;
32 // helper class which defines the iterator_facade super-class
33 // type for counting_iterator<T>
35 class counting_iterator_base
38 typedef ::boost::iterator_facade<
39 ::boost::compute::counting_iterator<T>,
41 ::std::random_access_iterator_tag
45 template<class T, class IndexExpr>
46 struct counting_iterator_index_expr
48 typedef T result_type;
50 counting_iterator_index_expr(const T init, const IndexExpr &expr)
57 const IndexExpr m_expr;
60 template<class T, class IndexExpr>
61 inline meta_kernel& operator<<(meta_kernel &kernel,
62 const counting_iterator_index_expr<T, IndexExpr> &expr)
64 return kernel << '(' << expr.m_init << '+' << expr.m_expr << ')';
67 } // end detail namespace
69 /// \class counting_iterator
70 /// \brief The counting_iterator class implements a counting iterator.
72 /// A counting iterator returns an internal value (initialized with \p init)
73 /// which is incremented each time the iterator is incremented.
75 /// For example, this could be used to implement the iota() algorithm in terms
76 /// of the copy() algorithm by copying from a range of counting iterators:
78 /// \snippet test/test_counting_iterator.cpp iota_with_copy
80 /// \see make_counting_iterator()
82 class counting_iterator : public detail::counting_iterator_base<T>::type
85 typedef typename detail::counting_iterator_base<T>::type super_type;
86 typedef typename super_type::reference reference;
87 typedef typename super_type::difference_type difference_type;
89 counting_iterator(const T &init)
94 counting_iterator(const counting_iterator<T> &other)
95 : m_init(other.m_init)
99 counting_iterator<T>& operator=(const counting_iterator<T> &other)
102 m_init = other.m_init;
112 size_t get_index() const
118 detail::counting_iterator_index_expr<T, Expr>
119 operator[](const Expr &expr) const
121 return detail::counting_iterator_index_expr<T, Expr>(m_init, expr);
125 friend class ::boost::iterator_core_access;
127 reference dereference() const
132 bool equal(const counting_iterator<T> &other) const
134 return m_init == other.m_init;
147 void advance(difference_type n)
149 m_init += static_cast<T>(n);
152 difference_type distance_to(const counting_iterator<T> &other) const
154 return difference_type(other.m_init) - difference_type(m_init);
161 /// Returns a new counting_iterator starting at \p init.
163 /// \param init the initial value
165 /// \return a counting_iterator with \p init.
167 /// For example, to create a counting iterator which returns unsigned integers
168 /// and increments from one:
170 /// auto iter = make_counting_iterator<uint_>(1);
173 inline counting_iterator<T> make_counting_iterator(const T &init)
175 return counting_iterator<T>(init);
178 /// \internal_ (is_device_iterator specialization for counting_iterator)
180 struct is_device_iterator<counting_iterator<T> > : boost::true_type {};
182 } // end compute namespace
183 } // end boost namespace
185 #endif // BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP