discard_iterator.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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_DISCARD_ITERATOR_HPP
  11. #define BOOST_COMPUTE_ITERATOR_DISCARD_ITERATOR_HPP
  12. #include <string>
  13. #include <cstddef>
  14. #include <iterator>
  15. #include <boost/config.hpp>
  16. #include <boost/iterator/iterator_facade.hpp>
  17. #include <boost/compute/detail/meta_kernel.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. // forward declaration for discard_iterator
  22. class discard_iterator;
  23. namespace detail {
  24. // helper class which defines the iterator_facade super-class
  25. // type for discard_iterator
  26. struct discard_iterator_base
  27. {
  28. typedef ::boost::iterator_facade<
  29. ::boost::compute::discard_iterator,
  30. void,
  31. ::std::random_access_iterator_tag,
  32. void *
  33. > type;
  34. };
  35. template<class IndexExpr>
  36. struct discard_iterator_index_expr
  37. {
  38. typedef void result_type;
  39. discard_iterator_index_expr(const IndexExpr &expr)
  40. : m_expr(expr)
  41. {
  42. }
  43. IndexExpr m_expr;
  44. };
  45. template<class IndexExpr>
  46. inline meta_kernel& operator<<(meta_kernel &kernel,
  47. const discard_iterator_index_expr<IndexExpr> &expr)
  48. {
  49. (void) expr;
  50. return kernel;
  51. }
  52. } // end detail namespace
  53. /// \class discard_iterator
  54. /// \brief An iterator which discards all values written to it.
  55. ///
  56. /// \see make_discard_iterator(), constant_iterator
  57. class discard_iterator : public detail::discard_iterator_base::type
  58. {
  59. public:
  60. typedef detail::discard_iterator_base::type super_type;
  61. typedef super_type::reference reference;
  62. typedef super_type::difference_type difference_type;
  63. discard_iterator(size_t index = 0)
  64. : m_index(index)
  65. {
  66. }
  67. discard_iterator(const discard_iterator &other)
  68. : m_index(other.m_index)
  69. {
  70. }
  71. discard_iterator& operator=(const discard_iterator &other)
  72. {
  73. if(this != &other){
  74. m_index = other.m_index;
  75. }
  76. return *this;
  77. }
  78. ~discard_iterator()
  79. {
  80. }
  81. /// \internal_
  82. template<class Expr>
  83. detail::discard_iterator_index_expr<Expr>
  84. operator[](const Expr &expr) const
  85. {
  86. return detail::discard_iterator_index_expr<Expr>(expr);
  87. }
  88. private:
  89. friend class ::boost::iterator_core_access;
  90. /// \internal_
  91. reference dereference() const
  92. {
  93. return 0;
  94. }
  95. /// \internal_
  96. bool equal(const discard_iterator &other) const
  97. {
  98. return m_index == other.m_index;
  99. }
  100. /// \internal_
  101. void increment()
  102. {
  103. m_index++;
  104. }
  105. /// \internal_
  106. void decrement()
  107. {
  108. m_index--;
  109. }
  110. /// \internal_
  111. void advance(difference_type n)
  112. {
  113. m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
  114. }
  115. /// \internal_
  116. difference_type distance_to(const discard_iterator &other) const
  117. {
  118. return static_cast<difference_type>(other.m_index - m_index);
  119. }
  120. private:
  121. size_t m_index;
  122. };
  123. /// Returns a new discard_iterator with \p index.
  124. ///
  125. /// \param index the index of the iterator
  126. ///
  127. /// \return a \c discard_iterator at \p index
  128. inline discard_iterator make_discard_iterator(size_t index = 0)
  129. {
  130. return discard_iterator(index);
  131. }
  132. /// internal_ (is_device_iterator specialization for discard_iterator)
  133. template<>
  134. struct is_device_iterator<discard_iterator> : boost::true_type {};
  135. } // end compute namespace
  136. } // end boost namespace
  137. #endif // BOOST_COMPUTE_ITERATOR_DISCARD_ITERATOR_HPP