123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #ifndef BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
- #define BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP
- #include <string>
- #include <cstddef>
- #include <iterator>
- #include <boost/config.hpp>
- #include <boost/iterator/iterator_facade.hpp>
- #include <boost/compute/detail/meta_kernel.hpp>
- #include <boost/compute/type_traits/is_device_iterator.hpp>
- namespace boost {
- namespace compute {
- template<class T> class counting_iterator;
- namespace detail {
- template<class T>
- class counting_iterator_base
- {
- public:
- typedef ::boost::iterator_facade<
- ::boost::compute::counting_iterator<T>,
- T,
- ::std::random_access_iterator_tag
- > type;
- };
- template<class T, class IndexExpr>
- struct counting_iterator_index_expr
- {
- typedef T result_type;
- counting_iterator_index_expr(const T init, const IndexExpr &expr)
- : m_init(init),
- m_expr(expr)
- {
- }
- const T m_init;
- const IndexExpr m_expr;
- };
- template<class T, class IndexExpr>
- inline meta_kernel& operator<<(meta_kernel &kernel,
- const counting_iterator_index_expr<T, IndexExpr> &expr)
- {
- return kernel << '(' << expr.m_init << '+' << expr.m_expr << ')';
- }
- }
- template<class T>
- class counting_iterator : public detail::counting_iterator_base<T>::type
- {
- public:
- typedef typename detail::counting_iterator_base<T>::type super_type;
- typedef typename super_type::reference reference;
- typedef typename super_type::difference_type difference_type;
- counting_iterator(const T &init)
- : m_init(init)
- {
- }
- counting_iterator(const counting_iterator<T> &other)
- : m_init(other.m_init)
- {
- }
- counting_iterator<T>& operator=(const counting_iterator<T> &other)
- {
- if(this != &other){
- m_init = other.m_init;
- }
- return *this;
- }
- ~counting_iterator()
- {
- }
- size_t get_index() const
- {
- return 0;
- }
- template<class Expr>
- detail::counting_iterator_index_expr<T, Expr>
- operator[](const Expr &expr) const
- {
- return detail::counting_iterator_index_expr<T, Expr>(m_init, expr);
- }
- private:
- friend class ::boost::iterator_core_access;
- reference dereference() const
- {
- return m_init;
- }
- bool equal(const counting_iterator<T> &other) const
- {
- return m_init == other.m_init;
- }
- void increment()
- {
- m_init++;
- }
- void decrement()
- {
- m_init--;
- }
- void advance(difference_type n)
- {
- m_init += static_cast<T>(n);
- }
- difference_type distance_to(const counting_iterator<T> &other) const
- {
- return difference_type(other.m_init) - difference_type(m_init);
- }
- private:
- T m_init;
- };
- template<class T>
- inline counting_iterator<T> make_counting_iterator(const T &init)
- {
- return counting_iterator<T>(init);
- }
- template<class T>
- struct is_device_iterator<counting_iterator<T> > : boost::true_type {};
- }
- }
- #endif
|