Skip to content
Snippets Groups Projects
Commit 8e2b603e authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Remove useless/obsolete SubArray class

parent 7c9ea1b2
No related branches found
No related tags found
1 merge request!89Add missing compatibility check when affecting lists to R^d or R^dxd
...@@ -10,8 +10,6 @@ ...@@ -10,8 +10,6 @@
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/Table.hpp> #include <utils/Table.hpp>
#include <utils/SubArray.hpp>
#include <memory> #include <memory>
template <typename DataType, typename ItemOfItem, typename ConnectivityPtr = std::shared_ptr<const IConnectivity>> template <typename DataType, typename ItemOfItem, typename ConnectivityPtr = std::shared_ptr<const IConnectivity>>
......
#ifndef SUB_ARRAY_HPP
#define SUB_ARRAY_HPP
#include <utils/Array.hpp>
#include <utils/PugsAssert.hpp>
#include <utils/PugsMacros.hpp>
#include <utils/PugsUtils.hpp>
#include <algorithm>
template <typename DataType>
class [[nodiscard]] SubArray
{
public:
using data_type = DataType;
using index_type = size_t;
private:
PUGS_RESTRICT DataType* const m_sub_values;
const size_t m_size;
// Allows const version to access our data
friend SubArray<std::add_const_t<DataType>>;
public:
PUGS_INLINE size_t size() const noexcept
{
return m_size;
}
PUGS_INLINE DataType& operator[](index_type i) const noexcept(NO_ASSERT)
{
Assert(i < m_size);
return m_sub_values[i];
}
PUGS_INLINE
void fill(const DataType& data) const
{
static_assert(not std::is_const<DataType>(), "Cannot modify SubArray of const");
// could consider to use std::fill
parallel_for(
this->size(), PUGS_LAMBDA(index_type i) { m_sub_values[i] = data; });
}
PUGS_INLINE
SubArray& operator=(const SubArray&) = delete;
PUGS_INLINE
SubArray& operator=(SubArray&&) = delete;
PUGS_INLINE
explicit SubArray(const Array<DataType>& array, size_t begin, size_t size)
: m_sub_values{&array[0] + begin}, m_size{size}
{
Assert(begin + size <= array.size(), "SubView is not contained in the source Array");
}
PUGS_INLINE
explicit SubArray(DataType* const raw_array, size_t begin, size_t size)
: m_sub_values{raw_array + begin}, m_size{size}
{}
PUGS_INLINE
SubArray() = delete;
PUGS_INLINE
SubArray(const SubArray&) = delete;
PUGS_INLINE
SubArray(SubArray &&) = delete;
PUGS_INLINE
~SubArray() = default;
};
#endif // SUB_ARRAY_HPP
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <utils/PugsAssert.hpp>
#include <utils/SubArray.hpp>
#include <utils/Types.hpp>
// Instantiate to ensure full coverage is performed
template class SubArray<int>;
// clazy:excludeall=non-pod-global-static
TEST_CASE("SubArray", "[utils]")
{
Array<int> a(10);
REQUIRE(a.size() == 10);
SECTION("shared values")
{
SubArray sub_a{a, 0, 10};
for (size_t i = 0; i < sub_a.size(); ++i) {
sub_a[i] = 2 * i;
}
REQUIRE(((a[0] == 0) and (a[1] == 2) and (a[2] == 4) and (a[3] == 6) and (a[4] == 8) and (a[5] == 10) and
(a[6] == 12) and (a[7] == 14) and (a[8] == 16) and (a[9] == 18)));
for (size_t i = 0; i < a.size(); ++i) {
a[i] = (i + 1) * (2 * i + 1);
}
REQUIRE(((sub_a[0] == 1) and (sub_a[1] == 6) and (sub_a[2] == 15) and (sub_a[3] == 28) and (sub_a[4] == 45) and
(sub_a[5] == 66) and (sub_a[6] == 91) and (sub_a[7] == 120) and (sub_a[8] == 153) and (sub_a[9] == 190)));
}
SECTION("sub array")
{
a.fill(0);
SubArray sub_a{a, 5, 5};
for (size_t i = 0; i < sub_a.size(); ++i) {
sub_a[i] = i + 1;
}
REQUIRE(((a[0] == 0) and (a[1] == 0) and (a[2] == 0) and (a[3] == 0) and (a[4] == 0) and (a[5] == 1) and
(a[6] == 2) and (a[7] == 3) and (a[8] == 4) and (a[9] == 5)));
for (size_t i = 0; i < a.size(); ++i) {
a[i] = (i + 1) * (2 * i + 1);
}
REQUIRE(((sub_a[0] == 66) and (sub_a[1] == 91) and (sub_a[2] == 120) and (sub_a[3] == 153) and (sub_a[4] == 190)));
}
#ifndef NDEBUG
SECTION("errors")
{
a.fill(0);
SubArray<int> sub_a{a, 5, 5};
for (size_t i = 0; i < sub_a.size(); ++i) {
sub_a[i] = i + 1;
}
REQUIRE_THROWS_AS(sub_a[5], AssertError);
}
#endif // NDEBUG
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment