File indexing completed on 2025-01-18 09:30:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
0012 #define BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
0013
0014 #include <string>
0015 #include <cstddef>
0016 #include <iterator>
0017
0018 #include <boost/config.hpp>
0019 #include <boost/iterator/iterator_facade.hpp>
0020
0021 #include <boost/compute/detail/meta_kernel.hpp>
0022 #include <boost/compute/type_traits/is_device_iterator.hpp>
0023
0024 namespace boost {
0025 namespace compute {
0026
0027
0028 template<class T> class counting_iterator;
0029
0030 namespace detail {
0031
0032
0033
0034 template<class T>
0035 class counting_iterator_base
0036 {
0037 public:
0038 typedef ::boost::iterator_facade<
0039 ::boost::compute::counting_iterator<T>,
0040 T,
0041 ::std::random_access_iterator_tag
0042 > type;
0043 };
0044
0045 template<class T, class IndexExpr>
0046 struct counting_iterator_index_expr
0047 {
0048 typedef T result_type;
0049
0050 counting_iterator_index_expr(const T init, const IndexExpr &expr)
0051 : m_init(init),
0052 m_expr(expr)
0053 {
0054 }
0055
0056 const T m_init;
0057 const IndexExpr m_expr;
0058 };
0059
0060 template<class T, class IndexExpr>
0061 inline meta_kernel& operator<<(meta_kernel &kernel,
0062 const counting_iterator_index_expr<T, IndexExpr> &expr)
0063 {
0064 return kernel << '(' << expr.m_init << '+' << expr.m_expr << ')';
0065 }
0066
0067 }
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 template<class T>
0082 class counting_iterator : public detail::counting_iterator_base<T>::type
0083 {
0084 public:
0085 typedef typename detail::counting_iterator_base<T>::type super_type;
0086 typedef typename super_type::reference reference;
0087 typedef typename super_type::difference_type difference_type;
0088
0089 counting_iterator(const T &init)
0090 : m_init(init)
0091 {
0092 }
0093
0094 counting_iterator(const counting_iterator<T> &other)
0095 : m_init(other.m_init)
0096 {
0097 }
0098
0099 counting_iterator<T>& operator=(const counting_iterator<T> &other)
0100 {
0101 if(this != &other){
0102 m_init = other.m_init;
0103 }
0104
0105 return *this;
0106 }
0107
0108 ~counting_iterator()
0109 {
0110 }
0111
0112 size_t get_index() const
0113 {
0114 return 0;
0115 }
0116
0117 template<class Expr>
0118 detail::counting_iterator_index_expr<T, Expr>
0119 operator[](const Expr &expr) const
0120 {
0121 return detail::counting_iterator_index_expr<T, Expr>(m_init, expr);
0122 }
0123
0124 private:
0125 friend class ::boost::iterator_core_access;
0126
0127 reference dereference() const
0128 {
0129 return m_init;
0130 }
0131
0132 bool equal(const counting_iterator<T> &other) const
0133 {
0134 return m_init == other.m_init;
0135 }
0136
0137 void increment()
0138 {
0139 m_init++;
0140 }
0141
0142 void decrement()
0143 {
0144 m_init--;
0145 }
0146
0147 void advance(difference_type n)
0148 {
0149 m_init += static_cast<T>(n);
0150 }
0151
0152 difference_type distance_to(const counting_iterator<T> &other) const
0153 {
0154 return difference_type(other.m_init) - difference_type(m_init);
0155 }
0156
0157 private:
0158 T m_init;
0159 };
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 template<class T>
0173 inline counting_iterator<T> make_counting_iterator(const T &init)
0174 {
0175 return counting_iterator<T>(init);
0176 }
0177
0178
0179 template<class T>
0180 struct is_device_iterator<counting_iterator<T> > : boost::true_type {};
0181
0182 }
0183 }
0184
0185 #endif