added const overload for NDArray::view() (#351)
Build on RHEL9 / build (push) Successful in 2m29s
Build on RHEL8 / build (push) Successful in 3m3s
Run tests using data on local RHEL8 / build (push) Successful in 3m59s
Build on local RHEL8 / build (push) Successful in 2m45s

- Added missing const T overload for NDArray::view()
- Tests
This commit is contained in:
Erik Fröjdh authored and GitHub committed 2026-08-19 16:21:37 +02:00
1 parent af25aea684
commit fef57c13c9
4 files changed
+26 -10

No files matched your search

+7 -6
View File
@@ -506,12 +506,13 @@ class NDArray : public ArrayExpr<NDArray<T, Ndim>, Ndim> {
return *this;
}
/**
* @brief Create a view of the NDArray.
*
* @return NDView<T, Ndim>
*/
NDView<T, Ndim> view() const { return NDView<T, Ndim>{data_, shape_}; }
/** @brief Create a mutable view of the NDArray. */
NDView<T, Ndim> view() { return NDView<T, Ndim>{data_, shape_}; }
/** @brief Create a read-only view of a const NDArray. */
NDView<const T, Ndim> view() const {
return NDView<const T, Ndim>{data_, shape_};
}
private:
/**
+2 -2
View File
@@ -45,7 +45,7 @@ template <typename SUM_TYPE = double> class Pedestal {
NDArray<SUM_TYPE, 2> mean() { return m_mean; }
const NDView<SUM_TYPE, 2> view() const { return m_mean.view(); }
NDView<const SUM_TYPE, 2> view() const { return m_mean.view(); }
SUM_TYPE mean(const uint32_t row, const uint32_t col) const {
return m_mean(row, col);
@@ -221,4 +221,4 @@ template <typename SUM_TYPE = double> class Pedestal {
m_mean(row, col) = m_sum(row, col) / m_samples;
}
};
} // namespace aare
} // namespace aare
+2 -2
View File
@@ -42,7 +42,7 @@ template <typename T, typename StorageType> class PixelHistogramImpl {
// Zero-copy view of the underlying [rows x cols x n_bins] storage.
// Lifetime is tied to *this. Use for low-level merge/stitching paths;
// prefer values() for the public API where you want an owned copy.
NDView<StorageType, 3> view() const;
NDView<const StorageType, 3> view() const;
NDArray<T, 1> bin_centers() const;
NDArray<T, 1> bin_edges() const;
};
@@ -122,7 +122,7 @@ NDArray<StorageType, 3> PixelHistogramImpl<T, StorageType>::values() const {
}
template <typename T, typename StorageType>
NDView<StorageType, 3> PixelHistogramImpl<T, StorageType>::view() const {
NDView<const StorageType, 3> PixelHistogramImpl<T, StorageType>::view() const {
return m_values.view();
}
+15
View File
@@ -4,6 +4,7 @@
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
#include <numeric>
#include <type_traits>
using aare::NDArray;
using aare::NDView;
@@ -71,6 +72,20 @@ TEST_CASE("Accessing a const object") {
REQUIRE(img.shape(2) == 5);
}
TEST_CASE("A const NDArray returns a read-only view") {
NDArray<int, 2> mutable_array({2, 3}, 1);
const auto &const_array = mutable_array;
static_assert(
std::is_same_v<decltype(mutable_array.view()), NDView<int, 2>>);
static_assert(
std::is_same_v<decltype(const_array.view()), NDView<const int, 2>>);
auto view = const_array.view();
static_assert(std::is_same_v<decltype(view.data()), const int *>);
REQUIRE(view(1, 2) == 1);
}
TEST_CASE("Indexing of a 2D image") {
std::array<ssize_t, 2> shape{{3, 7}};
NDArray<long> img(shape, 5);