From fef57c13c906f00ff3d46004649f74aebe857059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Wed, 19 Aug 2026 16:21:37 +0200 Subject: [PATCH] added const overload for NDArray::view() (#351) - Added missing const T overload for NDArray::view() - Tests --- include/aare/NDArray.hpp | 13 +++++++------ include/aare/Pedestal.hpp | 4 ++-- include/aare/hist/PixelHistogramImpl.hpp | 4 ++-- src/NDArray.test.cpp | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/aare/NDArray.hpp b/include/aare/NDArray.hpp index 079915d6..43964028 100644 --- a/include/aare/NDArray.hpp +++ b/include/aare/NDArray.hpp @@ -506,12 +506,13 @@ class NDArray : public ArrayExpr, Ndim> { return *this; } - /** - * @brief Create a view of the NDArray. - * - * @return NDView - */ - NDView view() const { return NDView{data_, shape_}; } + /** @brief Create a mutable view of the NDArray. */ + NDView view() { return NDView{data_, shape_}; } + + /** @brief Create a read-only view of a const NDArray. */ + NDView view() const { + return NDView{data_, shape_}; + } private: /** diff --git a/include/aare/Pedestal.hpp b/include/aare/Pedestal.hpp index 6ea6a6fb..48bb6568 100644 --- a/include/aare/Pedestal.hpp +++ b/include/aare/Pedestal.hpp @@ -45,7 +45,7 @@ template class Pedestal { NDArray mean() { return m_mean; } - const NDView view() const { return m_mean.view(); } + NDView 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 class Pedestal { m_mean(row, col) = m_sum(row, col) / m_samples; } }; -} // namespace aare \ No newline at end of file +} // namespace aare diff --git a/include/aare/hist/PixelHistogramImpl.hpp b/include/aare/hist/PixelHistogramImpl.hpp index 715537f6..30b468b9 100644 --- a/include/aare/hist/PixelHistogramImpl.hpp +++ b/include/aare/hist/PixelHistogramImpl.hpp @@ -42,7 +42,7 @@ template 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 view() const; + NDView view() const; NDArray bin_centers() const; NDArray bin_edges() const; }; @@ -122,7 +122,7 @@ NDArray PixelHistogramImpl::values() const { } template -NDView PixelHistogramImpl::view() const { +NDView PixelHistogramImpl::view() const { return m_values.view(); } diff --git a/src/NDArray.test.cpp b/src/NDArray.test.cpp index e2609d27..3a3cafc0 100644 --- a/src/NDArray.test.cpp +++ b/src/NDArray.test.cpp @@ -4,6 +4,7 @@ #include #include #include +#include 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 mutable_array({2, 3}, 1); + const auto &const_array = mutable_array; + + static_assert( + std::is_same_v>); + static_assert( + std::is_same_v>); + + auto view = const_array.view(); + static_assert(std::is_same_v); + REQUIRE(view(1, 2) == 1); +} + TEST_CASE("Indexing of a 2D image") { std::array shape{{3, 7}}; NDArray img(shape, 5);