Skip to content

Commit

Permalink
Add a few more checks on the subspan range.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunney1 committed Jan 19, 2023
1 parent ae1eeb0 commit 846d9e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/axom/core/ArrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,23 @@ class ArrayView : public ArrayBase<T, DIM, ArrayView<T, DIM, SPACE>>
* \param [in] count The number of elements to include in the subspan, or -1
* to take all elements after offset (default).
*
* \return A subspan ArrayView that spans the indices [offset, offset + count),
* or [offset, num_elements) if count == -1.
* \return An ArrayView that spans the indices [offset, offset + count),
* or [offset, num_elements) if count < 0.
*
* \pre offset + count <= m_num_elements if count != -1
* \pre offset + count <= m_num_elements if count < 0
*/
template <int UDIM = DIM, typename Enable = typename std::enable_if<UDIM == 1>::type>
AXOM_HOST_DEVICE ArrayView subspan(IndexType offset, IndexType count = -1) const
{
assert(offset + count <= m_num_elements);
assert(offset >= 0 && offset < m_num_elements);
if(count >= 0)
{
assert(offset + count <= m_num_elements);
}

ArrayView slice = *this;
slice.m_data += offset;
if(count == -1)
if(count < 0)
{
slice.m_num_elements -= offset;
}
Expand Down
9 changes: 9 additions & 0 deletions src/axom/core/tests/core_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,12 +1937,21 @@ TEST(core_array, check_subspan_range)
{
int m = 10;
int n = 3;
int l = 5;
Array<int> arr(m);

ArrayView<int> arrv1(arr);
EXPECT_EQ(arrv1.size(), arr.size());

ArrayView<int> arrv2 = arrv1.subspan(n);
EXPECT_EQ(arrv2.size() + n, arrv1.size());
EXPECT_EQ(&arrv2[0], &arr[n]);
EXPECT_EQ(&arrv2[arrv2.size() - 1], &arr[arr.size() - 1]);

ArrayView<int> arrv3 = arrv1.subspan(n, l);
EXPECT_EQ(arrv3.size(), l);
EXPECT_EQ(&arrv3[0], &arr[n]);
EXPECT_EQ(&arrv3[arrv3.size() - 1], &arr[n + l - 1]);
}

} /* end namespace axom */

0 comments on commit 846d9e8

Please sign in to comment.