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_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
0028 template<class T> class constant_iterator;
0029
0030 namespace detail {
0031
0032
0033
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 }
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
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
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
0111 reference dereference() const
0112 {
0113 return m_value;
0114 }
0115
0116
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
0123 void increment()
0124 {
0125 m_index++;
0126 }
0127
0128
0129 void decrement()
0130 {
0131 m_index--;
0132 }
0133
0134
0135 void advance(difference_type n)
0136 {
0137 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
0138 }
0139
0140
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
0152
0153
0154
0155
0156
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
0165 template<class T>
0166 struct is_device_iterator<constant_iterator<T> > : boost::true_type {};
0167
0168 }
0169 }
0170
0171 #endif