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