function_input_iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  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
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP
  11. #define BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP
  12. #include <cstddef>
  13. #include <iterator>
  14. #include <boost/config.hpp>
  15. #include <boost/iterator/iterator_facade.hpp>
  16. #include <boost/compute/detail/meta_kernel.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. #include <boost/compute/type_traits/result_of.hpp>
  19. namespace boost {
  20. namespace compute {
  21. // forward declaration for function_input_iterator<Function>
  22. template<class Function> class function_input_iterator;
  23. namespace detail {
  24. // helper class which defines the iterator_facade super-class
  25. // type for function_input_iterator<Function>
  26. template<class Function>
  27. class function_input_iterator_base
  28. {
  29. public:
  30. typedef ::boost::iterator_facade<
  31. ::boost::compute::function_input_iterator<Function>,
  32. typename ::boost::compute::result_of<Function()>::type,
  33. ::std::random_access_iterator_tag,
  34. typename ::boost::compute::result_of<Function()>::type
  35. > type;
  36. };
  37. template<class Function>
  38. struct function_input_iterator_expr
  39. {
  40. typedef typename ::boost::compute::result_of<Function()>::type result_type;
  41. function_input_iterator_expr(const Function &function)
  42. : m_function(function)
  43. {
  44. }
  45. const Function m_function;
  46. };
  47. template<class Function>
  48. inline meta_kernel& operator<<(meta_kernel &kernel,
  49. const function_input_iterator_expr<Function> &expr)
  50. {
  51. return kernel << expr.m_function();
  52. }
  53. } // end detail namespace
  54. /// \class function_input_iterator
  55. /// \brief Iterator which returns the result of a function when dereferenced
  56. ///
  57. /// For example:
  58. ///
  59. /// \snippet test/test_function_input_iterator.cpp generate_42
  60. ///
  61. /// \see make_function_input_iterator()
  62. template<class Function>
  63. class function_input_iterator :
  64. public detail::function_input_iterator_base<Function>::type
  65. {
  66. public:
  67. typedef typename detail::function_input_iterator_base<Function>::type super_type;
  68. typedef typename super_type::reference reference;
  69. typedef typename super_type::difference_type difference_type;
  70. typedef Function function;
  71. function_input_iterator(const Function &function, size_t index = 0)
  72. : m_function(function),
  73. m_index(index)
  74. {
  75. }
  76. function_input_iterator(const function_input_iterator<Function> &other)
  77. : m_function(other.m_function),
  78. m_index(other.m_index)
  79. {
  80. }
  81. function_input_iterator<Function>&
  82. operator=(const function_input_iterator<Function> &other)
  83. {
  84. if(this != &other){
  85. m_function = other.m_function;
  86. m_index = other.m_index;
  87. }
  88. return *this;
  89. }
  90. ~function_input_iterator()
  91. {
  92. }
  93. size_t get_index() const
  94. {
  95. return m_index;
  96. }
  97. template<class Expr>
  98. detail::function_input_iterator_expr<Function>
  99. operator[](const Expr &expr) const
  100. {
  101. (void) expr;
  102. return detail::function_input_iterator_expr<Function>(m_function);
  103. }
  104. private:
  105. friend class ::boost::iterator_core_access;
  106. reference dereference() const
  107. {
  108. return reference();
  109. }
  110. bool equal(const function_input_iterator<Function> &other) const
  111. {
  112. return m_function == other.m_function && m_index == other.m_index;
  113. }
  114. void increment()
  115. {
  116. m_index++;
  117. }
  118. void decrement()
  119. {
  120. m_index--;
  121. }
  122. void advance(difference_type n)
  123. {
  124. m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
  125. }
  126. difference_type
  127. distance_to(const function_input_iterator<Function> &other) const
  128. {
  129. return static_cast<difference_type>(other.m_index - m_index);
  130. }
  131. private:
  132. Function m_function;
  133. size_t m_index;
  134. };
  135. /// Returns a function_input_iterator with \p function.
  136. ///
  137. /// \param function function to execute when dereferenced
  138. /// \param index index of the iterator
  139. ///
  140. /// \return a \c function_input_iterator with \p function
  141. template<class Function>
  142. inline function_input_iterator<Function>
  143. make_function_input_iterator(const Function &function, size_t index = 0)
  144. {
  145. return function_input_iterator<Function>(function, index);
  146. }
  147. /// \internal_ (is_device_iterator specialization for function_input_iterator)
  148. template<class Function>
  149. struct is_device_iterator<function_input_iterator<Function> > : boost::true_type {};
  150. } // end compute namespace
  151. } // end boost namespace
  152. #endif // BOOST_COMPUTE_ITERATOR_FUNCTION_INPUT_ITERATOR_HPP