• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COUNTING_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
13 
14 #include <string>
15 #include <cstddef>
16 #include <iterator>
17 
18 #include <boost/config.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
20 
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
23 
24 namespace boost {
25 namespace compute {
26 
27 // forward declaration for counting_iterator<T>
28 template<class T> class counting_iterator;
29 
30 namespace detail {
31 
32 // helper class which defines the iterator_facade super-class
33 // type for counting_iterator<T>
34 template<class T>
35 class counting_iterator_base
36 {
37 public:
38     typedef ::boost::iterator_facade<
39         ::boost::compute::counting_iterator<T>,
40         T,
41         ::std::random_access_iterator_tag
42     > type;
43 };
44 
45 template<class T, class IndexExpr>
46 struct counting_iterator_index_expr
47 {
48     typedef T result_type;
49 
counting_iterator_index_exprboost::compute::detail::counting_iterator_index_expr50     counting_iterator_index_expr(const T init, const IndexExpr &expr)
51         : m_init(init),
52           m_expr(expr)
53     {
54     }
55 
56     const T m_init;
57     const IndexExpr m_expr;
58 };
59 
60 template<class T, class IndexExpr>
operator <<(meta_kernel & kernel,const counting_iterator_index_expr<T,IndexExpr> & expr)61 inline meta_kernel& operator<<(meta_kernel &kernel,
62                                const counting_iterator_index_expr<T, IndexExpr> &expr)
63 {
64     return kernel << '(' << expr.m_init << '+' << expr.m_expr << ')';
65 }
66 
67 } // end detail namespace
68 
69 /// \class counting_iterator
70 /// \brief The counting_iterator class implements a counting iterator.
71 ///
72 /// A counting iterator returns an internal value (initialized with \p init)
73 /// which is incremented each time the iterator is incremented.
74 ///
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:
77 ///
78 /// \snippet test/test_counting_iterator.cpp iota_with_copy
79 ///
80 /// \see make_counting_iterator()
81 template<class T>
82 class counting_iterator : public detail::counting_iterator_base<T>::type
83 {
84 public:
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;
88 
counting_iterator(const T & init)89     counting_iterator(const T &init)
90         : m_init(init)
91     {
92     }
93 
counting_iterator(const counting_iterator<T> & other)94     counting_iterator(const counting_iterator<T> &other)
95         : m_init(other.m_init)
96     {
97     }
98 
operator =(const counting_iterator<T> & other)99     counting_iterator<T>& operator=(const counting_iterator<T> &other)
100     {
101         if(this != &other){
102             m_init = other.m_init;
103         }
104 
105         return *this;
106     }
107 
~counting_iterator()108     ~counting_iterator()
109     {
110     }
111 
get_index() const112     size_t get_index() const
113     {
114         return 0;
115     }
116 
117     template<class Expr>
118     detail::counting_iterator_index_expr<T, Expr>
operator [](const Expr & expr) const119     operator[](const Expr &expr) const
120     {
121         return detail::counting_iterator_index_expr<T, Expr>(m_init, expr);
122     }
123 
124 private:
125     friend class ::boost::iterator_core_access;
126 
dereference() const127     reference dereference() const
128     {
129         return m_init;
130     }
131 
equal(const counting_iterator<T> & other) const132     bool equal(const counting_iterator<T> &other) const
133     {
134         return m_init == other.m_init;
135     }
136 
increment()137     void increment()
138     {
139         m_init++;
140     }
141 
decrement()142     void decrement()
143     {
144         m_init--;
145     }
146 
advance(difference_type n)147     void advance(difference_type n)
148     {
149         m_init += static_cast<T>(n);
150     }
151 
distance_to(const counting_iterator<T> & other) const152     difference_type distance_to(const counting_iterator<T> &other) const
153     {
154         return difference_type(other.m_init) - difference_type(m_init);
155     }
156 
157 private:
158     T m_init;
159 };
160 
161 /// Returns a new counting_iterator starting at \p init.
162 ///
163 /// \param init the initial value
164 ///
165 /// \return a counting_iterator with \p init.
166 ///
167 /// For example, to create a counting iterator which returns unsigned integers
168 /// and increments from one:
169 /// \code
170 /// auto iter = make_counting_iterator<uint_>(1);
171 /// \endcode
172 template<class T>
make_counting_iterator(const T & init)173 inline counting_iterator<T> make_counting_iterator(const T &init)
174 {
175     return counting_iterator<T>(init);
176 }
177 
178 /// \internal_ (is_device_iterator specialization for counting_iterator)
179 template<class T>
180 struct is_device_iterator<counting_iterator<T> > : boost::true_type {};
181 
182 } // end compute namespace
183 } // end boost namespace
184 
185 #endif // BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
186