Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:02

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2014 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_DISCARD_ITERATOR_HPP
0012 #define BOOST_COMPUTE_ITERATOR_DISCARD_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 discard_iterator
0028 class discard_iterator;
0029 
0030 namespace detail {
0031 
0032 // helper class which defines the iterator_facade super-class
0033 // type for discard_iterator
0034 struct discard_iterator_base
0035 {
0036     typedef ::boost::iterator_facade<
0037         ::boost::compute::discard_iterator,
0038         void,
0039         ::std::random_access_iterator_tag,
0040         void *
0041     > type;
0042 };
0043 
0044 template<class IndexExpr>
0045 struct discard_iterator_index_expr
0046 {
0047     typedef void result_type;
0048 
0049     discard_iterator_index_expr(const IndexExpr &expr)
0050         : m_expr(expr)
0051     {
0052     }
0053 
0054     IndexExpr m_expr;
0055 };
0056 
0057 template<class IndexExpr>
0058 inline meta_kernel& operator<<(meta_kernel &kernel,
0059                                const discard_iterator_index_expr<IndexExpr> &expr)
0060 {
0061     (void) expr;
0062 
0063     return kernel;
0064 }
0065 
0066 } // end detail namespace
0067 
0068 /// \class discard_iterator
0069 /// \brief An iterator which discards all values written to it.
0070 ///
0071 /// \see make_discard_iterator(), constant_iterator
0072 class discard_iterator : public detail::discard_iterator_base::type
0073 {
0074 public:
0075     typedef detail::discard_iterator_base::type super_type;
0076     typedef super_type::reference reference;
0077     typedef super_type::difference_type difference_type;
0078 
0079     discard_iterator(size_t index = 0)
0080         : m_index(index)
0081     {
0082     }
0083 
0084     discard_iterator(const discard_iterator &other)
0085         : m_index(other.m_index)
0086     {
0087     }
0088 
0089     discard_iterator& operator=(const discard_iterator &other)
0090     {
0091         if(this != &other){
0092             m_index = other.m_index;
0093         }
0094 
0095         return *this;
0096     }
0097 
0098     ~discard_iterator()
0099     {
0100     }
0101 
0102     /// \internal_
0103     template<class Expr>
0104     detail::discard_iterator_index_expr<Expr>
0105     operator[](const Expr &expr) const
0106     {
0107         return detail::discard_iterator_index_expr<Expr>(expr);
0108     }
0109 
0110 private:
0111     friend class ::boost::iterator_core_access;
0112 
0113     /// \internal_
0114     reference dereference() const
0115     {
0116         return 0;
0117     }
0118 
0119     /// \internal_
0120     bool equal(const discard_iterator &other) const
0121     {
0122         return m_index == other.m_index;
0123     }
0124 
0125     /// \internal_
0126     void increment()
0127     {
0128         m_index++;
0129     }
0130 
0131     /// \internal_
0132     void decrement()
0133     {
0134         m_index--;
0135     }
0136 
0137     /// \internal_
0138     void advance(difference_type n)
0139     {
0140         m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
0141     }
0142 
0143     /// \internal_
0144     difference_type distance_to(const discard_iterator &other) const
0145     {
0146         return static_cast<difference_type>(other.m_index - m_index);
0147     }
0148 
0149 private:
0150     size_t m_index;
0151 };
0152 
0153 /// Returns a new discard_iterator with \p index.
0154 ///
0155 /// \param index the index of the iterator
0156 ///
0157 /// \return a \c discard_iterator at \p index
0158 inline discard_iterator make_discard_iterator(size_t index = 0)
0159 {
0160     return discard_iterator(index);
0161 }
0162 
0163 /// internal_ (is_device_iterator specialization for discard_iterator)
0164 template<>
0165 struct is_device_iterator<discard_iterator> : boost::true_type {};
0166 
0167 } // end compute namespace
0168 } // end boost namespace
0169 
0170 #endif // BOOST_COMPUTE_ITERATOR_DISCARD_ITERATOR_HPP