constant_iterator.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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_CONSTANT_ITERATOR_HPP
  11. #define BOOST_COMPUTE_ITERATOR_CONSTANT_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 constant_iterator<T>
  22. template<class T> class constant_iterator;
  23. namespace detail {
  24. // helper class which defines the iterator_facade super-class
  25. // type for constant_iterator<T>
  26. template<class T>
  27. class constant_iterator_base
  28. {
  29. public:
  30. typedef ::boost::iterator_facade<
  31. ::boost::compute::constant_iterator<T>,
  32. T,
  33. ::std::random_access_iterator_tag
  34. > type;
  35. };
  36. } // end detail namespace
  37. /// \class constant_iterator
  38. /// \brief An iterator with a constant value.
  39. ///
  40. /// The constant_iterator class provides an iterator which returns a constant
  41. /// value when dereferenced.
  42. ///
  43. /// For example, this could be used to implement the fill() algorithm in terms
  44. /// of the copy() algorithm by copying from a range of constant iterators:
  45. ///
  46. /// \snippet test/test_constant_iterator.cpp fill_with_copy
  47. ///
  48. /// \see make_constant_iterator()
  49. template<class T>
  50. class constant_iterator : public detail::constant_iterator_base<T>::type
  51. {
  52. public:
  53. typedef typename detail::constant_iterator_base<T>::type super_type;
  54. typedef typename super_type::reference reference;
  55. typedef typename super_type::difference_type difference_type;
  56. constant_iterator(const T &value, size_t index = 0)
  57. : m_value(value),
  58. m_index(index)
  59. {
  60. }
  61. constant_iterator(const constant_iterator<T> &other)
  62. : m_value(other.m_value),
  63. m_index(other.m_index)
  64. {
  65. }
  66. constant_iterator<T>& operator=(const constant_iterator<T> &other)
  67. {
  68. if(this != &other){
  69. m_value = other.m_value;
  70. m_index = other.m_index;
  71. }
  72. return *this;
  73. }
  74. ~constant_iterator()
  75. {
  76. }
  77. size_t get_index() const
  78. {
  79. return m_index;
  80. }
  81. /// \internal_
  82. template<class Expr>
  83. detail::meta_kernel_literal<T> operator[](const Expr &expr) const
  84. {
  85. (void) expr;
  86. return detail::meta_kernel::make_lit<T>(m_value);
  87. }
  88. private:
  89. friend class ::boost::iterator_core_access;
  90. /// \internal_
  91. reference dereference() const
  92. {
  93. return m_value;
  94. }
  95. /// \internal_
  96. bool equal(const constant_iterator<T> &other) const
  97. {
  98. return m_value == other.m_value && 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 constant_iterator<T> &other) const
  117. {
  118. return static_cast<difference_type>(other.m_index - m_index);
  119. }
  120. private:
  121. T m_value;
  122. size_t m_index;
  123. };
  124. /// Returns a new constant_iterator with \p value at \p index.
  125. ///
  126. /// \param value the constant value
  127. /// \param index the iterators index
  128. ///
  129. /// \return a \c constant_iterator with \p value
  130. template<class T>
  131. inline constant_iterator<T>
  132. make_constant_iterator(const T &value, size_t index = 0)
  133. {
  134. return constant_iterator<T>(value, index);
  135. }
  136. /// \internal_ (is_device_iterator specialization for constant_iterator)
  137. template<class T>
  138. struct is_device_iterator<constant_iterator<T> > : boost::true_type {};
  139. } // end compute namespace
  140. } // end boost namespace
  141. #endif // BOOST_COMPUTE_ITERATOR_CONSTANT_ITERATOR_HPP