From 854e1a0533bc54110ffc7158bf9af3ab92f34d50 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 6 Dec 2022 19:34:42 +0100 Subject: [PATCH] slices --- src/include/array/array.cc.h | 2 +- src/include/array/array.h | 2 +- src/include/array/array_base.cc.h | 9 ++++- src/include/array/array_base.h | 1 - src/include/array/marray.cc.h | 6 --- src/include/array/marray.h | 1 - src/include/array/slice.cc.h | 64 +++++++++++++++++++++++++++++++ src/include/array/slice.h | 53 +++++++++++++++++++++++++ src/include/ranges/yrange.h | 3 +- src/lib/ranges/yrange.cc | 12 ++++++ src/tests/marray_unit_test.cc | 1 - 11 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 src/include/array/slice.cc.h create mode 100644 src/include/array/slice.h diff --git a/src/include/array/array.cc.h b/src/include/array/array.cc.h index 1da0edb..dd01178 100644 --- a/src/include/array/array.cc.h +++ b/src/include/array/array.cc.h @@ -1,6 +1,6 @@ -//#include "access.cc.h" #include "array_base.cc.h" #include "marray.cc.h" #include "aindex.cc.h" +#include "slice.cc.h" //#include "functional_array.cc.h" diff --git a/src/include/array/array.h b/src/include/array/array.h index 53aa197..b8d0a21 100644 --- a/src/include/array/array.h +++ b/src/include/array/array.h @@ -1,8 +1,8 @@ -//#include "access.h" #include "array_base.h" #include "marray.h" #include "aindex.h" +#include "slice.h" //#include "functional_array.h" #include "array.cc.h" diff --git a/src/include/array/array_base.cc.h b/src/include/array/array_base.cc.h index 666460f..48cc44d 100644 --- a/src/include/array/array_base.cc.h +++ b/src/include/array/array_base.cc.h @@ -33,6 +33,13 @@ namespace CNORXZ return *ai; } + template + template + Sptr> CArrayBase::sl(const IndexInterface& i) const + { + + } + template SizeT CArrayBase::size() const { @@ -87,7 +94,7 @@ namespace CNORXZ template T& ArrayBase::at(const IndexInterface& i) { - CXZ_ASSERT(i.pos() < this->pmax(), "index out of range"); + CXZ_ASSERT(i.les() < this->size(), "index out of range"); // check further compatibility of index/range format!!! auto ai = this->begin() + i.lex(); return *ai; diff --git a/src/include/array/array_base.h b/src/include/array/array_base.h index 24a9510..7a4a9c2 100644 --- a/src/include/array/array_base.h +++ b/src/include/array/array_base.h @@ -39,7 +39,6 @@ namespace CNORXZ Sptr> sl(const IndexInterface& i) const; virtual const T* data() const = 0; - virtual SizeT pmax() const = 0; // max allocated postion of data() (exclusive!) virtual SizeT size() const; virtual RangePtr range() const; diff --git a/src/include/array/marray.cc.h b/src/include/array/marray.cc.h index ad60d53..6925645 100644 --- a/src/include/array/marray.cc.h +++ b/src/include/array/marray.cc.h @@ -25,12 +25,6 @@ namespace CNORXZ ArrayBase(range), mCont(vec) {} - template - SizeT MArray::pmax() const - { - return mCont.size(); - } - template const T* MArray::data() const { diff --git a/src/include/array/marray.h b/src/include/array/marray.h index 67be01d..118f80b 100644 --- a/src/include/array/marray.h +++ b/src/include/array/marray.h @@ -22,7 +22,6 @@ namespace CNORXZ MArray(const RangePtr& range, const Vector& vec); MArray(const RangePtr& range, Vector&& vec); - virtual SizeT pmax() const override; virtual const T* data() const override; virtual T* data() override; virtual const_iterator cbegin() const override; diff --git a/src/include/array/slice.cc.h b/src/include/array/slice.cc.h new file mode 100644 index 0000000..667414a --- /dev/null +++ b/src/include/array/slice.cc.h @@ -0,0 +1,64 @@ + +#ifndef __cxz_slice_cc_h__ +#define __cxz_slice_cc_h__ + +#include "slice.h" + +namespace CNORXZ +{ + /************** + * CSlice * + **************/ + + template + CSlice::CSlice(const RangePtr& range, const CArrayBase* parent, + const Vector& blockSizes, SizeT off) : + CArrayBase(range), + mCParent(parent), + mBlockSizes(blockSizes), + mOff(off) + {} + + template + const T* CSlice::data() const + { + return mCParent->data() + mOff; + } + + template + typename CSlice::const_iterator CSlice::cbegin() const + { + return const_iterator(YIndex(AB::mRange, mBlockSizes, 0)); + } + + template + typename CSlice::const_iterator CSlice::cend() const + { + return const_iterator(YIndex(AB::mRange, mBlockSizes, AB::mRange->size())); + } + + template + bool CSlice::isView() const + { + return true; + } + + /************* + * Slice * + *************/ + + template + Slice::Slice(const RangePtr& range, ArrayBase* parent, + const Vector& blockSizes, SizeT off) : + CSlice(range, parent, blockSizes, off), + mParent(parent) + {} + + template + T* Slice::data() + { + return mParent->data(); + } +} + +#endif diff --git a/src/include/array/slice.h b/src/include/array/slice.h new file mode 100644 index 0000000..42da2ef --- /dev/null +++ b/src/include/array/slice.h @@ -0,0 +1,53 @@ + +#ifndef __cxz_slice_h__ +#define __cxz_slice_h__ + +#include "base/base.h" +#include "array_base.h" + +namespace CNORXZ +{ + template + class CSlice : public CArrayBase + { + public: + typedef CArrayBase AB; + typedef typename AB::const_iterator const_iterator; + + protected: + const CArrayBase* mCParent = nullptr; + Vector mBlockSizes; + SizeT mOff = 0; + + public: + DEFAULT_MEMBERS(CSlice); + CSlice(const RangePtr& range, const CArrayBase* parent, + const Vector& blockSizes, SizeT off); + + virtual const T* data() const override; + virtual const_iterator cbegin() const override; + virtual const_iterator cend() const override; + virtual bool isView() const override final; + }; + + template + class Slice : public virtual ArrayBase, + public virtual CSlice + { + public: + typedef CArrayBase AB; + typedef typename AB::const_iterator const_iterator; + + private: + ArrayBase* mParent = nullptr; + + public: + DEFAULT_MEMBERS(Slice); + Slice(const RangePtr& range, ArrayBase* parent, + const Vector& blockSizes, SizeT off); + + virtual T* data() override; + }; +} + +#endif diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index ebbcd08..9709246 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -26,7 +26,8 @@ namespace CNORXZ YIndex(const Vector& is); YIndex(const Vector& bs, const Vector& is); - YIndex(const RangePtr& range, SizeT lexpos); + YIndex(const RangePtr& range, SizeT lexpos = 0); + YIndex(const RangePtr& range, const Vector& bs, SizeT lexpos = 0); YIndex& operator=(SizeT lexpos); YIndex& operator++(); diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index b40adf4..1c736ab 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -184,6 +184,18 @@ namespace CNORXZ *this = lexpos; } + YIndex::YIndex(const RangePtr& range, const Vector& bs, SizeT lexpos) : + IndexInterface(0), + mRange(rangeCast(range)), + mIs(mkIndices()), + mBlockSizes(bs), + mLexBlockSizes(mkLexBlockSizes()), + mPMax(mkPMax()), + mLMax(mkLMax()) + { + *this = lexpos; + } + YIndex& YIndex::operator=(SizeT lexpos) { if(lexpos >= lmax().val()){ diff --git a/src/tests/marray_unit_test.cc b/src/tests/marray_unit_test.cc index 9ceac8e..ab85ecf 100644 --- a/src/tests/marray_unit_test.cc +++ b/src/tests/marray_unit_test.cc @@ -69,7 +69,6 @@ namespace const MArray a(mCR1*mUR1, Numbers::get(0,size)); EXPECT_EQ(a.range()->dim(), 2u); EXPECT_EQ(a.size(), size); - EXPECT_EQ(a.pmax(), size); EXPECT_EQ(a.range()->sub(0), mCR1); EXPECT_EQ(a.range()->sub(1), mUR1); auto cr1x = std::dynamic_pointer_cast(mCR1);