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_CONSTANT_ITERATOR_HPP
0012 #define BOOST_COMPUTE_ITERATOR_CONSTANT_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 constant_iterator<T>
0028 template<class T> class constant_iterator;
0029 
0030 namespace detail {
0031 
0032 // helper class which defines the iterator_facade super-class
0033 // type for constant_iterator<T>
0034 template<class T>
0035 class constant_iterator_base
0036 {
0037 public:
0038     typedef ::boost::iterator_facade<
0039         ::boost::compute::constant_iterator<T>,
0040         T,
0041         ::std::random_access_iterator_tag
0042     > type;
0043 };
0044 
0045 } // end detail namespace
0046 
0047 /// \class constant_iterator
0048 /// \brief An iterator with a constant value.
0049 ///
0050 /// The constant_iterator class provides an iterator which returns a constant
0051 /// value when dereferenced.
0052 ///
0053 /// For example, this could be used to implement the fill() algorithm in terms
0054 /// of the copy() algorithm by copying from a range of constant iterators:
0055 ///
0056 /// \snippet test/test_constant_iterator.cpp fill_with_copy
0057 ///
0058 /// \see make_constant_iterator()
0059 template<class T>
0060 class constant_iterator : public detail::constant_iterator_base<T>::type
0061 {
0062 public:
0063     typedef typename detail::constant_iterator_base<T>::type super_type;
0064     typedef typename super_type::reference reference;
0065     typedef typename super_type::difference_type difference_type;
0066 
0067     constant_iterator(const T &value, size_t index = 0)
0068         : m_value(value),
0069           m_index(index)
0070     {
0071     }
0072 
0073     constant_iterator(const constant_iterator<T> &other)
0074         : m_value(other.m_value),
0075           m_index(other.m_index)
0076     {
0077     }
0078 
0079     constant_iterator<T>& operator=(const constant_iterator<T> &other)
0080     {
0081         if(this != &other){
0082             m_value = other.m_value;
0083             m_index = other.m_index;
0084         }
0085 
0086         return *this;
0087     }
0088 
0089     ~constant_iterator()
0090     {
0091     }
0092 
0093     size_t get_index() const
0094     {
0095         return m_index;
0096     }
0097 
0098     /// \internal_
0099     template<class Expr>
0100     detail::meta_kernel_literal<T> operator[](const Expr &expr) const
0101     {
0102         (void) expr;
0103 
0104         return detail::meta_kernel::make_lit<T>(m_value);
0105     }
0106 
0107 private:
0108     friend class ::boost::iterator_core_access;
0109 
0110     /// \internal_
0111     reference dereference() const
0112     {
0113         return m_value;
0114     }
0115 
0116     /// \internal_
0117     bool equal(const constant_iterator<T> &other) const
0118     {
0119         return m_value == other.m_value && m_index == other.m_index;
0120     }
0121 
0122     /// \internal_
0123     void increment()
0124     {
0125         m_index++;
0126     }
0127 
0128     /// \internal_
0129     void decrement()
0130     {
0131         m_index--;
0132     }
0133 
0134     /// \internal_
0135     void advance(difference_type n)
0136     {
0137         m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
0138     }
0139 
0140     /// \internal_
0141     difference_type distance_to(const constant_iterator<T> &other) const
0142     {
0143         return static_cast<difference_type>(other.m_index - m_index);
0144     }
0145 
0146 private:
0147     T m_value;
0148     size_t m_index;
0149 };
0150 
0151 /// Returns a new constant_iterator with \p value at \p index.
0152 ///
0153 /// \param value the constant value
0154 /// \param index the iterators index
0155 ///
0156 /// \return a \c constant_iterator with \p value
0157 template<class T>
0158 inline constant_iterator<T>
0159 make_constant_iterator(const T &value, size_t index = 0)
0160 {
0161     return constant_iterator<T>(value, index);
0162 }
0163 
0164 /// \internal_ (is_device_iterator specialization for constant_iterator)
0165 template<class T>
0166 struct is_device_iterator<constant_iterator<T> > : boost::true_type {};
0167 
0168 } // end compute namespace
0169 } // end boost namespace
0170 
0171 #endif // BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP