Skip to content

Commit

Permalink
Merge pull request #1110 from LLNL/bugfix/yang39/array-resize
Browse files Browse the repository at this point in the history
Fix instantiation of Array::resize
  • Loading branch information
gunney1 committed Jun 6, 2023
2 parents f21098d + d0da453 commit 31ae2e4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The Axom project release numbers follow [Semantic Versioning](http://semver.org/
and triangles before using the geometry.
- Improves import logic for `lua` dependency
- Improves import logic for `mfem` dependency in device builds when `mfem` is configured with `caliper`
- Fixes ambiguity when calling `Array::resize(size, value)` for `Array<bool>`

### Deprecated
- Integer types in `src/axom/core/Types.hpp` are deprecated because `c++11` supports their equivalents.
Expand Down
20 changes: 10 additions & 10 deletions src/axom/core/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,26 +716,26 @@ class Array : public ArrayBase<T, DIM, Array<T, DIM, SPACE>>
"constructible. Use Array<T>::reserve() and emplace_back()"
"instead.");
const StackArray<IndexType, DIM> dims {static_cast<IndexType>(args)...};
resize(dims, true);
resizeImpl(dims, true);
}

/// \overload
template <typename... Args, typename Enable = std::enable_if_t<sizeof...(Args) == DIM>>
void resize(ArrayOptions::Uninitialized, Args... args)
{
const StackArray<IndexType, DIM> dims {static_cast<IndexType>(args)...};
resize(dims, false);
resizeImpl(dims, false);
}

template <int Dims = DIM, typename Enable = std::enable_if_t<Dims == 1>>
void resize(IndexType size, const T& value)
{
resize({size}, true, &value);
resizeImpl({size}, true, &value);
}

void resize(const StackArray<IndexType, DIM>& size, const T& value)
{
resize(size, true, &value);
resizeImpl(size, true, &value);
}

/*!
Expand Down Expand Up @@ -818,9 +818,9 @@ class Array : public ArrayBase<T, DIM, Array<T, DIM, SPACE>>
* \param [in] value pointer to the value to fill new elements in the array
* with. If null, will default-construct elements in place.
*/
void resize(const StackArray<IndexType, DIM>& dims,
bool construct_with_values,
const T* value = nullptr);
void resizeImpl(const StackArray<IndexType, DIM>& dims,
bool construct_with_values,
const T* value = nullptr);

/*!
* \brief Make space for a subsequent insertion into the array.
Expand Down Expand Up @@ -1295,9 +1295,9 @@ inline void Array<T, DIM, SPACE>::emplace_back(Args&&... args)

//------------------------------------------------------------------------------
template <typename T, int DIM, MemorySpace SPACE>
inline void Array<T, DIM, SPACE>::resize(const StackArray<IndexType, DIM>& dims,
bool construct_with_values,
const T* value)
inline void Array<T, DIM, SPACE>::resizeImpl(const StackArray<IndexType, DIM>& dims,
bool construct_with_values,
const T* value)
{
assert(detail::allNonNegative(dims.m_data));
const auto new_num_elements = detail::packProduct(dims.m_data);
Expand Down
49 changes: 49 additions & 0 deletions src/axom/core/tests/core_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,4 +2006,53 @@ TEST(core_array, check_subspan_range)
EXPECT_EQ(&arrv3[arrv3.size() - 1], &arr[n + l - 1]);
}

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

template <typename DataType>
void test_resize_with_stackarray(DataType value)
{
const int I_DIMS = 3;
const int J_DIMS = 5;
const int K_DIMS = 7;
Array<DataType, 2> arr2;

StackArray<axom::IndexType, 2> dims2 = {I_DIMS, J_DIMS};
arr2.resize(dims2, value);
EXPECT_EQ(arr2.size(), I_DIMS * J_DIMS);
EXPECT_EQ(arr2.shape()[0], I_DIMS);
EXPECT_EQ(arr2.shape()[1], J_DIMS);
for(int i = 0; i < I_DIMS; i++)
{
for(int j = 0; j < J_DIMS; j++)
{
EXPECT_EQ(arr2[i][j], value);
}
}

Array<DataType, 3> arr3;

StackArray<axom::IndexType, 3> dims3 = {I_DIMS, J_DIMS, K_DIMS};
arr3.resize(dims3, value);
EXPECT_EQ(arr3.size(), I_DIMS * J_DIMS * K_DIMS);
EXPECT_EQ(arr3.shape()[0], I_DIMS);
EXPECT_EQ(arr3.shape()[1], J_DIMS);
EXPECT_EQ(arr3.shape()[2], K_DIMS);
for(int i = 0; i < I_DIMS; i++)
{
for(int j = 0; j < J_DIMS; j++)
{
for(int k = 0; k < K_DIMS; k++)
{
EXPECT_EQ(arr3[i][j][k], value);
}
}
}
}

TEST(core_array, resize_stackarray)
{
test_resize_with_stackarray<bool>(false);
test_resize_with_stackarray<int>(-1);
}

} /* end namespace axom */

0 comments on commit 31ae2e4

Please sign in to comment.