extents.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_UTILITY_EXTENTS_HPP
  11. #define BOOST_COMPUTE_UTILITY_EXTENTS_HPP
  12. #include <functional>
  13. #include <numeric>
  14. #include <boost/compute/config.hpp>
  15. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  16. #include <initializer_list>
  17. #endif
  18. #include <boost/array.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// The extents class contains an array of n-dimensional extents.
  22. ///
  23. /// \see dim()
  24. template<size_t N>
  25. class extents
  26. {
  27. public:
  28. typedef size_t size_type;
  29. static const size_type static_size = N;
  30. typedef boost::array<size_t, N> array_type;
  31. typedef typename array_type::iterator iterator;
  32. typedef typename array_type::const_iterator const_iterator;
  33. /// Creates an extents object with each component set to zero.
  34. ///
  35. /// For example:
  36. /// \code
  37. /// extents<3> exts(); // (0, 0, 0)
  38. /// \endcode
  39. extents()
  40. {
  41. m_extents.fill(0);
  42. }
  43. /// Creates an extents object with each component set to \p value.
  44. ///
  45. /// For example:
  46. /// \code
  47. /// extents<3> exts(1); // (1, 1, 1)
  48. /// \endcode
  49. explicit extents(size_t value)
  50. {
  51. m_extents.fill(value);
  52. }
  53. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  54. /// Creates an extents object with \p values.
  55. extents(std::initializer_list<size_t> values)
  56. {
  57. BOOST_ASSERT(values.size() == N);
  58. std::copy(values.begin(), values.end(), m_extents.begin());
  59. }
  60. #endif // BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  61. /// Returns the size (i.e. dimensionality) of the extents array.
  62. size_type size() const
  63. {
  64. return N;
  65. }
  66. /// Returns the linear size of the extents. This is equivalent to the
  67. /// product of each extent in each dimension.
  68. size_type linear() const
  69. {
  70. return std::accumulate(
  71. m_extents.begin(), m_extents.end(), 1, std::multiplies<size_type>()
  72. );
  73. }
  74. /// Returns a pointer to the extents data array.
  75. ///
  76. /// This is useful for passing the extents data to OpenCL APIs which
  77. /// expect an array of \c size_t.
  78. size_t* data()
  79. {
  80. return m_extents.data();
  81. }
  82. /// \overload
  83. const size_t* data() const
  84. {
  85. return m_extents.data();
  86. }
  87. iterator begin()
  88. {
  89. return m_extents.begin();
  90. }
  91. const_iterator begin() const
  92. {
  93. return m_extents.begin();
  94. }
  95. const_iterator cbegin() const
  96. {
  97. return m_extents.cbegin();
  98. }
  99. iterator end()
  100. {
  101. return m_extents.end();
  102. }
  103. const_iterator end() const
  104. {
  105. return m_extents.end();
  106. }
  107. const_iterator cend() const
  108. {
  109. return m_extents.cend();
  110. }
  111. /// Returns a reference to the extent at \p index.
  112. size_t& operator[](size_t index)
  113. {
  114. return m_extents[index];
  115. }
  116. /// \overload
  117. const size_t& operator[](size_t index) const
  118. {
  119. return m_extents[index];
  120. }
  121. /// Returns \c true if the extents in \c *this are the same as \p other.
  122. bool operator==(const extents &other) const
  123. {
  124. return m_extents == other.m_extents;
  125. }
  126. /// Returns \c true if the extents in \c *this are not the same as \p other.
  127. bool operator!=(const extents &other) const
  128. {
  129. return m_extents != other.m_extents;
  130. }
  131. private:
  132. array_type m_extents;
  133. };
  134. } // end compute namespace
  135. } // end boost namespace
  136. #endif // BOOST_COMPUTE_UTILITY_EXTENTS_HPP