123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #ifndef BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP
- #define BOOST_COMPUTE_ITERATOR_CONSTANT_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 constant_iterator;
- namespace detail {
- template<class T>
- class constant_iterator_base
- {
- public:
- typedef ::boost::iterator_facade<
- ::boost::compute::constant_iterator<T>,
- T,
- ::std::random_access_iterator_tag
- > type;
- };
- }
- template<class T>
- class constant_iterator : public detail::constant_iterator_base<T>::type
- {
- public:
- typedef typename detail::constant_iterator_base<T>::type super_type;
- typedef typename super_type::reference reference;
- typedef typename super_type::difference_type difference_type;
- constant_iterator(const T &value, size_t index = 0)
- : m_value(value),
- m_index(index)
- {
- }
- constant_iterator(const constant_iterator<T> &other)
- : m_value(other.m_value),
- m_index(other.m_index)
- {
- }
- constant_iterator<T>& operator=(const constant_iterator<T> &other)
- {
- if(this != &other){
- m_value = other.m_value;
- m_index = other.m_index;
- }
- return *this;
- }
- ~constant_iterator()
- {
- }
- size_t get_index() const
- {
- return m_index;
- }
-
- template<class Expr>
- detail::meta_kernel_literal<T> operator[](const Expr &expr) const
- {
- (void) expr;
- return detail::meta_kernel::make_lit<T>(m_value);
- }
- private:
- friend class ::boost::iterator_core_access;
-
- reference dereference() const
- {
- return m_value;
- }
-
- bool equal(const constant_iterator<T> &other) const
- {
- return m_value == other.m_value && m_index == other.m_index;
- }
-
- void increment()
- {
- m_index++;
- }
-
- void decrement()
- {
- m_index--;
- }
-
- void advance(difference_type n)
- {
- m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
- }
-
- difference_type distance_to(const constant_iterator<T> &other) const
- {
- return static_cast<difference_type>(other.m_index - m_index);
- }
- private:
- T m_value;
- size_t m_index;
- };
- template<class T>
- inline constant_iterator<T>
- make_constant_iterator(const T &value, size_t index = 0)
- {
- return constant_iterator<T>(value, index);
- }
- template<class T>
- struct is_device_iterator<constant_iterator<T> > : boost::true_type {};
- }
- }
- #endif
|