Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:02

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
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 // forward declaration for counting_iterator<T>
0028 template<class T> class counting_iterator;
0029 
0030 namespace detail {
0031 
0032 // helper class which defines the iterator_facade super-class
0033 // type for counting_iterator<T>
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 } // end detail namespace
0068 
0069 /// \class counting_iterator
0070 /// \brief The counting_iterator class implements a counting iterator.
0071 ///
0072 /// A counting iterator returns an internal value (initialized with \p init)
0073 /// which is incremented each time the iterator is incremented.
0074 ///
0075 /// For example, this could be used to implement the iota() algorithm in terms
0076 /// of the copy() algorithm by copying from a range of counting iterators:
0077 ///
0078 /// \snippet test/test_counting_iterator.cpp iota_with_copy
0079 ///
0080 /// \see make_counting_iterator()
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 /// Returns a new counting_iterator starting at \p init.
0162 ///
0163 /// \param init the initial value
0164 ///
0165 /// \return a counting_iterator with \p init.
0166 ///
0167 /// For example, to create a counting iterator which returns unsigned integers
0168 /// and increments from one:
0169 /// \code
0170 /// auto iter = make_counting_iterator<uint_>(1);
0171 /// \endcode
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 /// \internal_ (is_device_iterator specialization for counting_iterator)
0179 template<class T>
0180 struct is_device_iterator<counting_iterator<T> > : boost::true_type {};
0181 
0182 } // end compute namespace
0183 } // end boost namespace
0184 
0185 #endif // BOOST_COMPUTE_ITERATOR_COUNTING_ITERATOR_HPP