1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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_DISCARD_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_DISCARD_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 discard_iterator
28 class discard_iterator;
32 // helper class which defines the iterator_facade super-class
33 // type for discard_iterator
34 struct discard_iterator_base
36 typedef ::boost::iterator_facade<
37 ::boost::compute::discard_iterator,
39 ::std::random_access_iterator_tag,
44 template<class IndexExpr>
45 struct discard_iterator_index_expr
47 typedef void result_type;
49 discard_iterator_index_expr(const IndexExpr &expr)
57 template<class IndexExpr>
58 inline meta_kernel& operator<<(meta_kernel &kernel,
59 const discard_iterator_index_expr<IndexExpr> &expr)
66 } // end detail namespace
68 /// \class discard_iterator
69 /// \brief An iterator which discards all values written to it.
71 /// \see make_discard_iterator(), constant_iterator
72 class discard_iterator : public detail::discard_iterator_base::type
75 typedef detail::discard_iterator_base::type super_type;
76 typedef super_type::reference reference;
77 typedef super_type::difference_type difference_type;
79 discard_iterator(size_t index = 0)
84 discard_iterator(const discard_iterator &other)
85 : m_index(other.m_index)
89 discard_iterator& operator=(const discard_iterator &other)
92 m_index = other.m_index;
104 detail::discard_iterator_index_expr<Expr>
105 operator[](const Expr &expr) const
107 return detail::discard_iterator_index_expr<Expr>(expr);
111 friend class ::boost::iterator_core_access;
114 reference dereference() const
120 bool equal(const discard_iterator &other) const
122 return m_index == other.m_index;
138 void advance(difference_type n)
140 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
144 difference_type distance_to(const discard_iterator &other) const
146 return static_cast<difference_type>(other.m_index - m_index);
153 /// Returns a new discard_iterator with \p index.
155 /// \param index the index of the iterator
157 /// \return a \c discard_iterator at \p index
158 inline discard_iterator make_discard_iterator(size_t index = 0)
160 return discard_iterator(index);
163 /// internal_ (is_device_iterator specialization for discard_iterator)
165 struct is_device_iterator<discard_iterator> : boost::true_type {};
167 } // end compute namespace
168 } // end boost namespace
170 #endif // BOOST_COMPUTE_ITERATOR_DISCARD_ITERATOR_HPP