Skip to content

Commit d035cf9

Browse files
committed
Merge pull request #429 from kylelutz/extents-begin-end
Add begin() and end() methods to extents<N>
2 parents 4215101 + a71a4e9 commit d035cf9

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

include/boost/compute/utility/extents.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class extents
3535
public:
3636
typedef size_t size_type;
3737
static const size_type static_size = N;
38+
typedef boost::array<size_t, N> array_type;
39+
typedef typename array_type::iterator iterator;
40+
typedef typename array_type::const_iterator const_iterator;
3841

3942
/// Creates an extents object with each component set to zero.
4043
///
@@ -99,6 +102,36 @@ class extents
99102
return m_extents.data();
100103
}
101104

105+
iterator begin()
106+
{
107+
return m_extents.begin();
108+
}
109+
110+
const_iterator begin() const
111+
{
112+
return m_extents.begin();
113+
}
114+
115+
const_iterator cbegin() const
116+
{
117+
return m_extents.cbegin();
118+
}
119+
120+
iterator end()
121+
{
122+
return m_extents.end();
123+
}
124+
125+
const_iterator end() const
126+
{
127+
return m_extents.end();
128+
}
129+
130+
const_iterator cend() const
131+
{
132+
return m_extents.cend();
133+
}
134+
102135
/// Returns a reference to the extent at \p index.
103136
size_t& operator[](size_t index)
104137
{
@@ -124,7 +157,7 @@ class extents
124157
}
125158

126159
private:
127-
boost::array<size_t, N> m_extents;
160+
array_type m_extents;
128161
};
129162

130163
} // end compute namespace

test/test_extents.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#define BOOST_TEST_MODULE TestExtents
1212
#include <boost/test/unit_test.hpp>
1313

14+
#include <algorithm>
15+
#include <vector>
16+
1417
#include <boost/compute/utility/dim.hpp>
1518
#include <boost/compute/utility/extents.hpp>
1619

@@ -79,4 +82,15 @@ BOOST_AUTO_TEST_CASE(empty_dim)
7982
BOOST_CHECK(compute::dim<3>() == compute::dim(0, 0, 0));
8083
}
8184

85+
BOOST_AUTO_TEST_CASE(copy_to_vector)
86+
{
87+
compute::extents<3> exts = compute::dim(4, 5, 6);
88+
89+
std::vector<size_t> vec(3);
90+
std::copy(exts.begin(), exts.end(), vec.begin());
91+
BOOST_CHECK_EQUAL(vec[0], 4);
92+
BOOST_CHECK_EQUAL(vec[1], 5);
93+
BOOST_CHECK_EQUAL(vec[2], 6);
94+
}
95+
8296
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)