1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP
18 #include <boost/config.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
27 // forward declaration for constant_iterator<T>
28 template<class T> class constant_iterator;
32 // helper class which defines the iterator_facade super-class
33 // type for constant_iterator<T>
35 class constant_iterator_base
38 typedef ::boost::iterator_facade<
39 ::boost::compute::constant_iterator<T>,
41 ::std::random_access_iterator_tag
45 } // end detail namespace
47 /// \class constant_iterator
48 /// \brief An iterator with a constant value.
50 /// The constant_iterator class provides an iterator which returns a constant
51 /// value when dereferenced.
53 /// For example, this could be used to implement the fill() algorithm in terms
54 /// of the copy() algorithm by copying from a range of constant iterators:
56 /// \snippet test/test_constant_iterator.cpp fill_with_copy
58 /// \see make_constant_iterator()
60 class constant_iterator : public detail::constant_iterator_base<T>::type
63 typedef typename detail::constant_iterator_base<T>::type super_type;
64 typedef typename super_type::reference reference;
65 typedef typename super_type::difference_type difference_type;
67 constant_iterator(const T &value, size_t index = 0)
73 constant_iterator(const constant_iterator<T> &other)
74 : m_value(other.m_value),
75 m_index(other.m_index)
79 constant_iterator<T>& operator=(const constant_iterator<T> &other)
82 m_value = other.m_value;
83 m_index = other.m_index;
93 size_t get_index() const
100 detail::meta_kernel_literal<T> operator[](const Expr &expr) const
104 return detail::meta_kernel::make_lit<T>(m_value);
108 friend class ::boost::iterator_core_access;
111 reference dereference() const
117 bool equal(const constant_iterator<T> &other) const
119 return m_value == other.m_value && m_index == other.m_index;
135 void advance(difference_type n)
137 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
141 difference_type distance_to(const constant_iterator<T> &other) const
143 return static_cast<difference_type>(other.m_index - m_index);
151 /// Returns a new constant_iterator with \p value at \p index.
153 /// \param value the constant value
154 /// \param index the iterators index
156 /// \return a \c constant_iterator with \p value
158 inline constant_iterator<T>
159 make_constant_iterator(const T &value, size_t index = 0)
161 return constant_iterator<T>(value, index);
164 /// \internal_ (is_device_iterator specialization for constant_iterator)
166 struct is_device_iterator<constant_iterator<T> > : boost::true_type {};
168 } // end compute namespace
169 } // end boost namespace
171 #endif // BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP