diff --git a/src/slice.cc b/src/slice.cc index 026cc0f..b63dc08 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -8,8 +8,8 @@ namespace MultiArrayTools template Slice:: Slice(const Range& range, - MutableMultiArrayBase& multiArrayRef, const Name& ownNm, // for correct linkage + MutableMultiArrayBase& multiArrayRef, const typename MARange::IndexType& MAIdx, // for desired slice position const Name& MANm) : MutableMultiArrayBase(range), @@ -23,6 +23,23 @@ namespace MultiArrayTools mMAIdx.linkTo(&mOwnIdx); } + template + Slice:: + Slice(const Range& range, + const Name& ownNm, + MultiArrayOperationRoot& mor, + const typename MARange::IndexType& MAIdx) : + MutableMultiArrayBase(range), + mOwnName(ownNm), mMAName(mor.name()), mMultiArrayPtr(&(*mor)), + mMAIdx(MAIdx) + { + MAB::mInit = true; + mOwnIdx = MAB::mRange->begin(); + mMAIdx.name(mMAName); + mOwnIdx.name(ownNm); + mMAIdx.linkTo(&mOwnIdx); + } + template bool Slice::isSlice() const { @@ -110,11 +127,11 @@ namespace MultiArrayTools template ConstSlice:: ConstSlice(const Range& range, - const MultiArrayBase& multiArrayRef, const Name& ownNm, // for correct linkage + const MultiArrayBase& multiArrayRef, const typename MARange::IndexType& MAIdx, // for desired slice position const Name& MANm) : - MutableMultiArrayBase(range), + MultiArrayBase(range), mMultiArrayPtr(&multiArrayRef), mMAIdx(MAIdx) { @@ -125,6 +142,22 @@ namespace MultiArrayTools mMAIdx.linkTo(&mOwnIdx); } + template + ConstSlice:: + ConstSlice(const Range& range, + const Name& ownNm, + const ConstMultiArrayOperationRoot& mor, + const typename MARange::IndexType& MAIdx) : + MultiArrayBase(range), + mMultiArrayPtr(&(*mor)), + mMAIdx(MAIdx) + { + MAB::mInit = true; + mOwnIdx = MAB::mRange->begin(); + mMAIdx.name(mor.name()); + mOwnIdx.name(ownNm); + mMAIdx.linkTo(&mOwnIdx); + } template ConstSlice:: @@ -192,14 +225,4 @@ namespace MultiArrayTools //mOwnIdx = i.pos(); return (*mMultiArrayPtr)[ mMAIdx ]; } - - - template - T& ConstSlice::operator[](const typename Range::IndexType& i) - { - assert(0); - mOwnIdx.copyPos(i); - //mOwnIdx = i.pos(); - return x; - } } diff --git a/src/slice.h b/src/slice.h index b1ccac7..027b945 100644 --- a/src/slice.h +++ b/src/slice.h @@ -22,10 +22,16 @@ namespace MultiArrayTools typedef MultiArrayBase MAB; Slice(const Range& range, - MutableMultiArrayBase& multiArrayRef, const Name& ownNm, + MutableMultiArrayBase& multiArrayRef, const typename MARange::IndexType& MAIdx, const Name& MANm); + + Slice(const Range& range, + const Name& ownNm, + MultiArrayOperationRoot& mor, + const typename MARange::IndexType& MAIdx); + virtual T& operator[](const typename Range::IndexType& i) override; virtual const T& operator[](const typename Range::IndexType& i) const override; @@ -71,13 +77,18 @@ namespace MultiArrayTools //ConstSlice(const Range& range); ConstSlice(const Range& range, - const MultiArrayBase& multiArrayRef, const Name& ownNm, + const MultiArrayBase& multiArrayRef, const typename MARange::IndexType& MAIdx, const Name& MANm); + + ConstSlice(const Range& range, + const Name& ownNm, + const ConstMultiArrayOperationRoot& mor, + const typename MARange::IndexType& MAIdx); + ConstSlice(const Slice& slice); - virtual T& operator[](const typename Range::IndexType& i) override; virtual const T& operator[](const typename Range::IndexType& i) const override; //Slice& setSlicePos(const Index& slicePos); diff --git a/src/unit_test.cc b/src/unit_test.cc index 5f37bed..2db98ec 100644 --- a/src/unit_test.cc +++ b/src/unit_test.cc @@ -413,7 +413,7 @@ namespace { auto i2 = i.template getIndex<1>(); auto i3 = i.template getIndex<2>(); - Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"), + Slice2d3dAny sl(ra, MAT::Name("master", "alpha", "gamma"), ma, i(i1 = 0, i2 = 2, i3 = 0), MAT::Name("master", "alpha","beta","gamma")); @@ -421,7 +421,7 @@ namespace { //sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; EXPECT_EQ(sl.size(), 6); - Slice2d3dAny sl2(rb, ma, MAT::Name("master", "alpha", "beta"), + Slice2d3dAny sl2(rb, MAT::Name("master", "alpha", "beta"), ma, i(i1 = 0, i2 = 0, i3 = 1), MAT::Name("master", "alpha","beta","gamma")); @@ -436,7 +436,7 @@ namespace { auto i1 = i.template getIndex<0>(); auto i2 = i.template getIndex<1>(); auto i3 = i.template getIndex<2>(); - Slice2d3dAny sl(ra, ma, MAT::Name("master", "alpha", "gamma"), + Slice2d3dAny sl(ra, MAT::Name("master", "alpha", "gamma"), ma, 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)];