Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of ArrayView returned by subspan #1001

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
gunney1 marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(arrv2.size() + n, arrv1.size());
gunney1 marked this conversation as resolved.
Show resolved Hide resolved
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 */