diff --git a/src/base_def.h b/src/base_def.h index efd7184..321b288 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -84,6 +84,10 @@ namespace MultiArrayTools // multi_array.h template class MultiArrayBase; + + // multi_array.h + template + class MutableMultiArrayBase; // multi_array.h template diff --git a/src/multi_array.cc b/src/multi_array.cc index a045264..b1f2e36 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -39,25 +39,11 @@ namespace MultiArrayTools } template - template - MultiArrayOperationRoot MultiArrayBase::operator()(const NameTypes&... str) + bool MultiArrayBase::isConst() const { - return MultiArrayOperationRoot(*this, Name("master", str...)); + return true; } - - template - template - MultiArrayOperationRoot MultiArrayBase::operator()(const NameType& name, bool master) - { - //CHECK; - if(master){ - return MultiArrayOperationRoot(*this, name); - } - else { - return operator()(name); - } - } - + template template ConstMultiArrayOperationRoot MultiArrayBase::operator()(const NameTypes&... str) const @@ -83,6 +69,40 @@ namespace MultiArrayTools { return mInit; } + + /****************************** + * MutableMultiArrayBase * + ******************************/ + + template + MutableMultiArrayBase::MutableMultiArrayBase(const Range& range) : MultiArrayBase(range) {} + + template + bool MutableMultiArrayBase::isConst() const + { + return false; + } + + template + template + MultiArrayOperationRoot MutableMultiArrayBase::operator()(const NameTypes&... str) + { + return MultiArrayOperationRoot(*this, Name("master", str...)); + } + + template + template + MultiArrayOperationRoot MutableMultiArrayBase::operator()(const NameType& name, bool master) + { + //CHECK; + if(master){ + return MultiArrayOperationRoot(*this, name); + } + else { + return operator()(name); + } + } + /******************* * MultiArray * @@ -90,7 +110,7 @@ namespace MultiArrayTools template MultiArray::MultiArray(const Range& range) : - MultiArrayBase(range), + MutableMultiArrayBase(range), mCont(MAB::mRange->size()) { MAB::mInit = true; @@ -98,7 +118,7 @@ namespace MultiArrayTools template MultiArray::MultiArray(const Range& range, const std::vector& vec) : - MultiArrayBase(range), + MutableMultiArrayBase(range), mCont(vec) { MAB::mInit = true; @@ -109,7 +129,7 @@ namespace MultiArrayTools template MultiArray::MultiArray(const Range& range, std::vector&& vec) : - MultiArrayBase(range), + MutableMultiArrayBase(range), mCont(vec) { MAB::mInit = true; @@ -121,7 +141,7 @@ namespace MultiArrayTools template template MultiArray::MultiArray(const MultiArray,Range3> in) : - MultiArrayBase(merge(in.range(), in[ in.begin() ].range())) + MutableMultiArrayBase(merge(in.range(), in[ in.begin() ].range())) // assert that Range2 has always same extension { MAB::mInit = true; diff --git a/src/multi_array.h b/src/multi_array.h index 32eb447..62f5fa5 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -27,7 +27,6 @@ namespace MultiArrayTools virtual ~MultiArrayBase() = default; - virtual T& operator[](const typename Range::IndexType& i) = 0; virtual const T& operator[](const typename Range::IndexType& i) const = 0; virtual size_t size() const; @@ -38,14 +37,8 @@ namespace MultiArrayTools virtual const Range& range() const; - virtual bool isConst() const = 0; + virtual bool isConst() const; - template - MultiArrayOperationRoot operator()(const NameTypes&... str); - - template - MultiArrayOperationRoot operator()(const NameType& name, bool master); - template ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; @@ -59,9 +52,31 @@ namespace MultiArrayTools std::shared_ptr mRange; }; + + template + class MutableMultiArrayBase : public MultiArrayBase + { + public: + + // iterator ( containing idx of Range ) + + DEFAULT_MEMBERS(MutableMultiArrayBase); + MutableMultiArrayBase(const Range& range); + + virtual T& operator[](const typename Range::IndexType& i) = 0; + + virtual bool isConst() const override; + + template + MultiArrayOperationRoot operator()(const NameTypes&... str); + + template + MultiArrayOperationRoot operator()(const NameType& name, bool master); + + }; template - class MultiArray : public MultiArrayBase + class MultiArray : public MutableMultiArrayBase { public: diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index a969fab..2bc20ff 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -75,7 +75,7 @@ namespace MultiArrayTools template MultiArrayOperationRoot:: - MultiArrayOperationRoot(MultiArrayBase& ma, + MultiArrayOperationRoot(MutableMultiArrayBase& ma, const Name& nm) : MutableMultiArrayOperationBase(), mArrayRef(ma), diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index ff74c66..64c7ecc 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -51,7 +51,7 @@ namespace MultiArrayTools typedef typename Range::IndexType IndexType; //typedef decltype(MultiArray().begin()) IndexType; - MultiArrayOperationRoot(MultiArrayBase& ma, const Name& nm); + MultiArrayOperationRoot(MutableMultiArrayBase& ma, const Name& nm); MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); MultiArrayOperationRoot& operator=(MultiArrayOperationRoot& in); @@ -138,7 +138,7 @@ namespace MultiArrayTools template const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot& in); - MultiArrayBase& mArrayRef; + MutableMultiArrayBase& mArrayRef; mutable IndexType mIndex; Name mNm; }; diff --git a/src/slice.cc b/src/slice.cc index 9d07fc0..026cc0f 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -7,8 +7,21 @@ namespace MultiArrayTools template Slice:: - Slice(const Range& range) : - MultiArrayBase(range) {} + Slice(const Range& range, + MutableMultiArrayBase& multiArrayRef, + const Name& ownNm, // for correct linkage + const typename MARange::IndexType& MAIdx, // for desired slice position + const Name& MANm) : + MutableMultiArrayBase(range), + mOwnName(ownNm), mMAName(MANm), mMultiArrayPtr(&multiArrayRef), + mMAIdx(MAIdx) + { + MAB::mInit = true; + mOwnIdx = MAB::mRange->begin(); + mMAIdx.name(MANm); + mOwnIdx.name(ownNm); + mMAIdx.linkTo(&mOwnIdx); + } template bool Slice::isSlice() const @@ -16,8 +29,9 @@ namespace MultiArrayTools return true; } + /* template - void Slice::set(MultiArrayBase& multiArrayRef, + void Slice::set(MutableMultiArrayBase& multiArrayRef, const Name& ownNm, // for correct linkage const typename MARange::IndexType& MAIdx, // for desired slice position const Name& MANm) // for correct linkage) @@ -32,7 +46,9 @@ namespace MultiArrayTools mOwnIdx.name(ownNm); mMAIdx.linkTo(&mOwnIdx); } + */ + /* template void Slice::setConst(const MultiArrayBase& multiArrayRef, const Name& ownNm, // for correct linkage @@ -49,7 +65,7 @@ namespace MultiArrayTools mOwnIdx.name(ownNm); mMAIdx.linkTo(&mOwnIdx); } - + */ template auto Slice::begin() const -> decltype(Range().begin()) @@ -84,12 +100,32 @@ namespace MultiArrayTools { return false; } - + + /* template ConstSlice:: ConstSlice(const Range& range) : MultiArrayBase(range) {} + */ + template + ConstSlice:: + ConstSlice(const Range& range, + const MultiArrayBase& multiArrayRef, + const Name& ownNm, // for correct linkage + const typename MARange::IndexType& MAIdx, // for desired slice position + const Name& MANm) : + MutableMultiArrayBase(range), + mMultiArrayPtr(&multiArrayRef), + mMAIdx(MAIdx) + { + MAB::mInit = true; + mOwnIdx = MAB::mRange->begin(); + mMAIdx.name(MANm); + mOwnIdx.name(ownNm); + mMAIdx.linkTo(&mOwnIdx); + } + template ConstSlice:: ConstSlice(const Slice& slice) : diff --git a/src/slice.h b/src/slice.h index 7020ef3..b1ccac7 100644 --- a/src/slice.h +++ b/src/slice.h @@ -15,14 +15,18 @@ namespace MultiArrayTools // MARange = original range of multi array of which this is the slice // Index = index which determines the slice position (remnant of MARange w.o. Range) template - class Slice : public MultiArrayBase // yes, 'Range' is correct !!! + class Slice : public MutableMultiArrayBase // yes, 'Range' is correct !!! { public: typedef MultiArrayBase MAB; - Slice(const Range& range); - + Slice(const Range& range, + MutableMultiArrayBase& multiArrayRef, + const Name& ownNm, + const typename MARange::IndexType& MAIdx, + const Name& MANm); + virtual T& operator[](const typename Range::IndexType& i) override; virtual const T& operator[](const typename Range::IndexType& i) const override; @@ -34,28 +38,28 @@ namespace MultiArrayTools virtual bool isSlice() const override; virtual bool isConst() const override; - - void set(MultiArrayBase& multiArrayRef, - const Name& ownNm, - const typename MARange::IndexType& MAIdx, - const Name& MANm); + /* + void set(); + */ + + /* void setConst(const MultiArrayBase& multiArrayRef, const Name& ownNm, const typename MARange::IndexType& MAIdx, const Name& MANm); - + */ template friend class ConstSlice; private: - MultiArrayBase const* mConstMultiArrayPtr = nullptr; - MultiArrayBase* mMultiArrayPtr = nullptr; - mutable typename Range::IndexType mOwnIdx; - mutable typename MARange::IndexType mMAIdx; Name mOwnName; Name mMAName; + MutableMultiArrayBase* mMultiArrayPtr = nullptr; + mutable typename Range::IndexType mOwnIdx; + mutable typename MARange::IndexType mMAIdx; + }; template @@ -65,7 +69,12 @@ namespace MultiArrayTools typedef MultiArrayBase MAB; - ConstSlice(const Range& range); + //ConstSlice(const Range& range); + ConstSlice(const Range& range, + const MultiArrayBase& multiArrayRef, + const Name& ownNm, + const typename MARange::IndexType& MAIdx, + const Name& MANm); ConstSlice(const Slice& slice); virtual T& operator[](const typename Range::IndexType& i) override; diff --git a/src/unit_test.cc b/src/unit_test.cc index fb26b96..5f37bed 100644 --- a/src/unit_test.cc +++ b/src/unit_test.cc @@ -413,12 +413,20 @@ namespace { auto i2 = i.template getIndex<1>(); auto i3 = i.template getIndex<2>(); - Slice2d3dAny sl(ra); - sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; + Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"), + i(i1 = 0, i2 = 2, i3 = 0), + MAT::Name("master", "alpha","beta","gamma")); + + //Slice2d3dAny sl(ra); + //sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; EXPECT_EQ(sl.size(), 6); - Slice2d3dAny sl2(rb); - sl2("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)]; + Slice2d3dAny sl2(rb, ma, MAT::Name("master", "alpha", "beta"), + i(i1 = 0, i2 = 0, i3 = 1), + MAT::Name("master", "alpha","beta","gamma")); + + //Slice2d3dAny sl2(rb); + //sl2("alpha","beta") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 0, i3 = 1)]; EXPECT_EQ(sl2.size(), 12); } @@ -428,8 +436,10 @@ namespace { auto i1 = i.template getIndex<0>(); auto i2 = i.template getIndex<1>(); auto i3 = i.template getIndex<2>(); - Slice2d3dAny sl(ra); - sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; + Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"), + i(i1 = 0, i2 = 2, i3 = 0), + MAT::Name("master", "alpha","beta","gamma")); + //sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; auto j = sl.begin(); auto j1 = j.template getIndex<0>();