Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 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_FUNCTION_INPUT_ITERATOR_HPP
0012 #define BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP
0013 
0014 #include <cstddef>
0015 #include <iterator>
0016 
0017 #include <boost/config.hpp>
0018 #include <boost/iterator/iterator_facade.hpp>
0019 
0020 #include <boost/compute/detail/meta_kernel.hpp>
0021 #include <boost/compute/type_traits/is_device_iterator.hpp>
0022 #include <boost/compute/type_traits/result_of.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 // forward declaration for function_input_iterator<Function>
0028 template<class Function> class function_input_iterator;
0029 
0030 namespace detail {
0031 
0032 // helper class which defines the iterator_facade super-class
0033 // type for function_input_iterator<Function>
0034 template<class Function>
0035 class function_input_iterator_base
0036 {
0037 public:
0038     typedef ::boost::iterator_facade<
0039         ::boost::compute::function_input_iterator<Function>,
0040         typename ::boost::compute::result_of<Function()>::type,
0041         ::std::random_access_iterator_tag,
0042         typename ::boost::compute::result_of<Function()>::type
0043     > type;
0044 };
0045 
0046 template<class Function>
0047 struct function_input_iterator_expr
0048 {
0049     typedef typename ::boost::compute::result_of<Function()>::type result_type;
0050 
0051     function_input_iterator_expr(const Function &function)
0052         : m_function(function)
0053     {
0054     }
0055 
0056     const Function m_function;
0057 };
0058 
0059 template<class Function>
0060 inline meta_kernel& operator<<(meta_kernel &kernel,
0061                                const function_input_iterator_expr<Function> &expr)
0062 {
0063     return kernel << expr.m_function();
0064 }
0065 
0066 } // end detail namespace
0067 
0068 /// \class function_input_iterator
0069 /// \brief Iterator which returns the result of a function when dereferenced
0070 ///
0071 /// For example:
0072 ///
0073 /// \snippet test/test_function_input_iterator.cpp generate_42
0074 ///
0075 /// \see make_function_input_iterator()
0076 template<class Function>
0077 class function_input_iterator :
0078     public detail::function_input_iterator_base<Function>::type
0079 {
0080 public:
0081     typedef typename detail::function_input_iterator_base<Function>::type super_type;
0082     typedef typename super_type::reference reference;
0083     typedef typename super_type::difference_type difference_type;
0084     typedef Function function;
0085 
0086     function_input_iterator(const Function &function, size_t index = 0)
0087         : m_function(function),
0088           m_index(index)
0089     {
0090     }
0091 
0092     function_input_iterator(const function_input_iterator<Function> &other)
0093         : m_function(other.m_function),
0094           m_index(other.m_index)
0095     {
0096     }
0097 
0098     function_input_iterator<Function>&
0099     operator=(const function_input_iterator<Function> &other)
0100     {
0101         if(this != &other){
0102             m_function = other.m_function;
0103             m_index = other.m_index;
0104         }
0105 
0106         return *this;
0107     }
0108 
0109     ~function_input_iterator()
0110     {
0111     }
0112 
0113     size_t get_index() const
0114     {
0115         return m_index;
0116     }
0117 
0118     template<class Expr>
0119     detail::function_input_iterator_expr<Function>
0120     operator[](const Expr &expr) const
0121     {
0122         (void) expr;
0123 
0124         return detail::function_input_iterator_expr<Function>(m_function);
0125     }
0126 
0127 private:
0128     friend class ::boost::iterator_core_access;
0129 
0130     reference dereference() const
0131     {
0132         return reference();
0133     }
0134 
0135     bool equal(const function_input_iterator<Function> &other) const
0136     {
0137         return m_function == other.m_function && m_index == other.m_index;
0138     }
0139 
0140     void increment()
0141     {
0142         m_index++;
0143     }
0144 
0145     void decrement()
0146     {
0147         m_index--;
0148     }
0149 
0150     void advance(difference_type n)
0151     {
0152         m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
0153     }
0154 
0155     difference_type
0156     distance_to(const function_input_iterator<Function> &other) const
0157     {
0158         return static_cast<difference_type>(other.m_index - m_index);
0159     }
0160 
0161 private:
0162     Function m_function;
0163     size_t m_index;
0164 };
0165 
0166 /// Returns a function_input_iterator with \p function.
0167 ///
0168 /// \param function function to execute when dereferenced
0169 /// \param index index of the iterator
0170 ///
0171 /// \return a \c function_input_iterator with \p function
0172 template<class Function>
0173 inline function_input_iterator<Function>
0174 make_function_input_iterator(const Function &function, size_t index = 0)
0175 {
0176     return function_input_iterator<Function>(function, index);
0177 }
0178 
0179 /// \internal_ (is_device_iterator specialization for function_input_iterator)
0180 template<class Function>
0181 struct is_device_iterator<function_input_iterator<Function> > : boost::true_type {};
0182 
0183 } // end compute namespace
0184 } // end boost namespace
0185 
0186 #endif // BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP