Skip to content

Commit

Permalink
Merge pull request #1001 from LLNL/bugfix/gunney/array-view-subspan-r…
Browse files Browse the repository at this point in the history
…ange

Reduce size of ArrayView returned by `subspan`
  • Loading branch information
gunney1 authored Jan 19, 2023
2 parents 20bfdf9 + 846d9e8 commit 1f86e24
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The Axom project release numbers follow [Semantic Versioning](http://semver.org/
- Updates uberenv submodule to HEAD of main on 28Dec2022
- Updates blt submodule to HEAD of develop on 28Dec2022
- Adds `vcpkg` ports for `RAJA`, `Umpire` with optional `OpenMP` feature for automated Windows build
- Reduce size of `ArrayView::subspan` to prevent accessing invalid memory.

## [Version 0.7.0] - Release date 2022-08-30

Expand Down
19 changes: 14 additions & 5 deletions src/axom/core/ArrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,27 @@ 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 [offsets, offsets + count),
* or [offsets, 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;
}
else
{
slice.m_num_elements = count;
}
Expand Down
23 changes: 23 additions & 0 deletions src/axom/core/tests/core_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1931,4 +1931,27 @@ TEST(core_array, checkVariadicCtors)
Array<int, 3> arr12(s, s, s);
}

//------------------------------------------------------------------------------

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 1f86e24

Please sign in to comment.