Skip to content
Snippets Groups Projects

Add SubArray class

Merged Stéphane Del Pino requested to merge feature/item-array into develop
2 files
+ 9
76
Compare changes
  • Side-by-side
  • Inline
Files
2
  • 31f12337
    Change SubArray implementation · 31f12337
    Stéphane Del Pino authored
    It does not store the underlying Array anymore.
    - this is quite dangerous but it increases performances by a factor 2+
    - all copy constructors have been removed as well to reduce the risk
    of bad manipulation (ie: using a SubArray on a destroyed Array)
+ 8
34
@@ -16,11 +16,8 @@ class [[nodiscard]] SubArray
using index_type = size_t;
private:
// underlying array
Array<DataType> m_array;
DataType* m_sub_values;
size_t m_size;
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>>;
@@ -47,32 +44,15 @@ class [[nodiscard]] SubArray
this->size(), PUGS_LAMBDA(index_type i) { m_sub_values[i] = data; });
}
template <typename DataType2>
PUGS_INLINE SubArray& operator=(const SubArray<DataType2>& sub_array) noexcept
{
// ensures that DataType is the same as source DataType2
static_assert(std::is_same<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>(),
"Cannot assign SubArray of different type");
// ensures that const is not lost through copy
static_assert(((std::is_const<DataType2>() and std::is_const<DataType>()) or not std::is_const<DataType2>()),
"Cannot assign SubArray of const to SubArray of non-const");
m_array = sub_array.m_array;
m_size = sub_array.m_size;
m_sub_values = sub_array.m_sub_values;
return *this;
}
PUGS_INLINE
SubArray& operator=(const SubArray&) = default;
SubArray& operator=(const SubArray&) = delete;
PUGS_INLINE
SubArray& operator=(SubArray&&) = default;
SubArray& operator=(SubArray&&) = delete;
PUGS_INLINE
explicit SubArray(Array<DataType> array, size_t begin, size_t size)
: m_array{array}, m_sub_values{&array[0] + begin}, m_size{size}
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");
}
@@ -81,16 +61,10 @@ class [[nodiscard]] SubArray
SubArray() = delete;
PUGS_INLINE
SubArray(const SubArray&) = default;
template <typename DataType2>
PUGS_INLINE SubArray(const SubArray<DataType2>& sub_array) noexcept
{
this->operator=(sub_array);
}
SubArray(const SubArray&) = delete;
PUGS_INLINE
SubArray(SubArray &&) = default;
SubArray(SubArray &&) = delete;
PUGS_INLINE
~SubArray() = default;
Loading