From 9d618135d0d5693caec710acde86dafc70e08a1f Mon Sep 17 00:00:00 2001 From: Kris Thielemans Date: Thu, 24 Aug 2023 11:38:44 +0100 Subject: [PATCH] Array: clean-up - make sure it works with other STIR classes, not just floats etc - re-instate VectorWithOffset constructors that take both start and end pointers for backwards compatibility --- src/include/stir/Array.inl | 3 ++- src/include/stir/VectorWithOffset.h | 24 ++++++++++++++++++++---- src/include/stir/VectorWithOffset.inl | 21 ++++++--------------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/include/stir/Array.inl b/src/include/stir/Array.inl index e9cc22fd1f..b30b7b02d8 100644 --- a/src/include/stir/Array.inl +++ b/src/include/stir/Array.inl @@ -100,7 +100,8 @@ Array::Array(const IndexRange& range) //grow(range); this->_allocated_full_data_ptr = new elemT[range.size_all()]; // info("Array constructor range " + std::to_string(reinterpret_cast(this->_allocated_full_data_ptr)) + " of size " + std::to_string(range.size_all())); - std::fill(this->_allocated_full_data_ptr, this->_allocated_full_data_ptr + range.size_all(), elemT(0)); + // set elements to zero + std::for_each(this->_allocated_full_data_ptr, this->_allocated_full_data_ptr + range.size_all(), [](elemT& e) { assign(e, 0); }); this->init(range, this->_allocated_full_data_ptr, false); } diff --git a/src/include/stir/VectorWithOffset.h b/src/include/stir/VectorWithOffset.h index 53bb22190c..87d34e9c68 100644 --- a/src/include/stir/VectorWithOffset.h +++ b/src/include/stir/VectorWithOffset.h @@ -136,14 +136,30 @@ class VectorWithOffset inline VectorWithOffset(const int min_index, const int max_index); //! Construct a VectorWithOffset of given length pointing to existing data - inline explicit + inline VectorWithOffset(const int hsz, - T * const data_ptr); - + T * const data_ptr, + T * const end_of_data_ptr); + + //! Construct a VectorWithOffset of given length pointing to existing data + inline + VectorWithOffset(const int hsz, + T * const data_ptr) + : VectorWithOffset(hsz, data_ptr, data_ptr + hsz) + {} + + //! Construct a VectorWithOffset with offset \c min_index point to existing data + inline + VectorWithOffset(const int min_index, const int max_index, + T * const data_ptr, + T * const end_of_data_ptr); + //! Construct a VectorWithOffset with offset \c min_index point to existing data inline VectorWithOffset(const int min_index, const int max_index, - T * const data_ptr); + T * const data_ptr) + : VectorWithOffset(min_index, max_index, data_ptr, data_ptr + (max_index - min_index + 1)) + {} //! copy constructor inline VectorWithOffset(const VectorWithOffset &il) ; diff --git a/src/include/stir/VectorWithOffset.inl b/src/include/stir/VectorWithOffset.inl index 82fb6b5964..15fe1d6c0b 100644 --- a/src/include/stir/VectorWithOffset.inl +++ b/src/include/stir/VectorWithOffset.inl @@ -293,28 +293,30 @@ VectorWithOffset::VectorWithOffset(const int min_index, const int max_index) template VectorWithOffset:: -VectorWithOffset(const int sz, T * const data_ptr) +VectorWithOffset(const int sz, + T * const data_ptr, T * const end_of_data_ptr) : length(static_cast(sz)), start(0), pointer_access(false), _owns_memory_for_data(false) { this->begin_allocated_memory = data_ptr; - this->end_allocated_memory = data_ptr + sz; + this->end_allocated_memory = end_of_data_ptr; this->num = this->begin_allocated_memory - this->start; this->check_state(); } template VectorWithOffset:: -VectorWithOffset(const int min_index, const int max_index, T * const data_ptr) +VectorWithOffset(const int min_index, const int max_index, + T * const data_ptr, T * const end_of_data_ptr) : length(static_cast(max_index - min_index) + 1), start(min_index), pointer_access(false), _owns_memory_for_data(false) { this->begin_allocated_memory = data_ptr; - this->end_allocated_memory = data_ptr + (max_index - min_index + 1); + this->end_allocated_memory = end_of_data_ptr; this->num = this->begin_allocated_memory - this->start; this->check_state(); } @@ -335,17 +337,6 @@ VectorWithOffset::~VectorWithOffset() _destruct_and_deallocate(); } -#if 0 -template -VectorWithOffset& -VectorWithOffset:: -operator=(VectorWithOffset other) -{ - swap(*this, other); - return *this; -} -#endif - template void VectorWithOffset::set_offset(const int min_index)