From 6eb53111bb3190b80efedede0bfbe07ead5c9749 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 23 Feb 2017 19:33:46 +0100 Subject: [PATCH] start implementing class Slice --- src/base_def.h | 5 ++++ src/multi_array.cc | 5 ++++ src/multi_array.h | 7 ++++- src/multi_array_operation.cc | 27 +++++++++--------- src/multi_array_operation.h | 9 +++--- src/slice.cc | 35 +++++++++++++++++++++++ src/slice.h | 54 ++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 src/slice.cc create mode 100644 src/slice.h diff --git a/src/base_def.h b/src/base_def.h index 590b89e..0ce3c95 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -80,6 +80,11 @@ namespace MultiArrayTools // multi_array_operation.h template class MultiArrayOperation; + + // slice.h + template + class Slice; + } #endif diff --git a/src/multi_array.cc b/src/multi_array.cc index 2088d9e..8fdcd68 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -69,5 +69,10 @@ namespace MultiArrayTools return mRange->end(); } + template + bool MultiArray::isSlice() const + { + return false; + } } diff --git a/src/multi_array.h b/src/multi_array.h index 1cef6e2..5ab0a9f 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -33,12 +33,17 @@ namespace MultiArrayTools size_t size() const; + virtual bool isSlice() const; + auto begin() -> decltype(Range().begin()); auto end() -> decltype(Range().end()); + + protected: + + std::shared_ptr mRange; private: bool mInit = false; - std::shared_ptr mRange; std::vector mCont; }; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index fc3845f..624807a 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -11,10 +11,12 @@ namespace MultiArrayTools MultiArrayOperationBase:: MultiArrayOperationBase(MultiArray& ma, const Name& nm) : mArrayRef(ma), + //mIndex(mArrayRef.begin()), mIibPtr(new IndexType(mArrayRef.begin())), mNm(nm) { mIibPtr->name(nm); + //mIndex.name(nm); } template @@ -43,7 +45,12 @@ namespace MultiArrayTools MultiArrayOperationBase::operator=(const MultiArrayOperationBase& in) { in.linkIndicesTo(mIibPtr); - for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){ + IndexType& iref = dynamic_cast(*mIibPtr); + if(mArrayRef.isSlice()){ + linkSlice(&in.index(), mIibPtr); + return *this; + } + for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){ // build in vectorization later get() = in.get(); } @@ -121,26 +128,16 @@ namespace MultiArrayTools template T& MultiArrayOperationBase::get() { + //return mArrayRef[mIndex]; return mArrayRef[*dynamic_cast(mIibPtr)]; } template const T& MultiArrayOperationBase::get() const { + //return mArrayRef[mIndex]; return mArrayRef[*dynamic_cast(mIibPtr)]; } - - template - T& MultiArrayOperationBase::get(IndefinitIndexBase* iibPtr) - { - return mArrayRef[*dynamic_cast(iibPtr)]; - } - - template - const T& MultiArrayOperationBase::get(IndefinitIndexBase* iibPtr) const - { - return mArrayRef[*dynamic_cast(iibPtr)]; - } /***************************** * MultiArrayOperation * @@ -197,7 +194,7 @@ namespace MultiArrayTools template static auto callOperation(const Operation& op, const Tuple& tp, const T& first, const MBases&... secs) - -> decltype(op(first.get(), std::get<0>(tp).get(), secs.get()...)) + -> decltype(op(first, std::get<0>(tp).get(), secs.get()...)) { return op(first, std::get<0>(tp).get(), secs.get()...); } @@ -239,6 +236,7 @@ namespace MultiArrayTools { mVal = OperationCall:: template callOperation(mOp, mSecs, + //OB::mArrayRef[OB::mIndex]); OB::mArrayRef[*dynamic_cast(OB::mIibPtr)]); return mVal; } @@ -248,6 +246,7 @@ namespace MultiArrayTools { mVal = OperationCall:: template callOperation(mOp, mSecs, + //OB::mArrayRef[OB::mIndex]); OB::mArrayRef[*dynamic_cast(OB::mIibPtr)]); return mVal; } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 66ffd0f..261c5f7 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -53,6 +53,7 @@ namespace MultiArrayTools virtual size_t argNum() const; + //IndexType& index() ; IndefinitIndexBase* index(); virtual void linkIndicesTo(IndefinitIndexBase* target) const; @@ -60,13 +61,11 @@ namespace MultiArrayTools virtual T& get(); virtual const T& get() const; - virtual T& get(IndefinitIndexBase* iibPtr); - virtual const T& get(IndefinitIndexBase* iibPtr) const; - protected: - + MultiArray& mArrayRef; - IndefinitIndexBase mutable* mIibPtr = nullptr; + //mutable IndexType mIndex; + IndefinitIndexBase* mIibPtr = nullptr; Name mNm; }; diff --git a/src/slice.cc b/src/slice.cc new file mode 100644 index 0000000..f8b3d46 --- /dev/null +++ b/src/slice.cc @@ -0,0 +1,35 @@ +// -*- C++ -*- + +#include "slice.h" + +namespace MultiArrayTools +{ + + template + Slice:: + Slice(const Range& range, const Index& slicePos) : + MA::mRange(new Range(range)), + mSlicePos(slicePos) {} + + template + Slice& Slice::setSlicePos(const Index& slicePos) + { + mSlicePos = slicePos; + mMAPtr->linkTo(&mSlicePos); + return *this; + } + + template + bool Slice::isSlice() const + { + return true; + } + + template + void Slice::setPtr(IndefinitIndexBase* MAPtr, + IndefinitIndexBase* ownPtr) + { + mMAPtr.reset(new typename MARange::IndexType(MAPtr)); + mOwnPtr.reset(new typename Range::IndexType(ownPtr)); + } +} diff --git a/src/slice.h b/src/slice.h new file mode 100644 index 0000000..0331118 --- /dev/null +++ b/src/slice.h @@ -0,0 +1,54 @@ +// -*- C++ -*- + +#ifndef __slice_h__ +#define __slice_h__ + +#include +#include "base_def.h" +#include "multi_array.h" + +namespace MultiArrayTools +{ + + // T = data type + // Range = range of slice + // 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 MultiArray // yes, Range is correct !!! + { + //MA::mCont has to be empty; only make use of the provided functions + public: + + typedef MultiArray MA; + + Slice(const Range& range, const Index& slicePos); + + T& operator[](const typename Range::IndexType& i) override; + const T& operator[](const typename Range::IndexType& i) const override; + + Slice& setSlicePos(const Index& slicePos); + + // link given Index to mMAPtr which is index of total array + auto begin() override -> decltype(Range().begin()); + auto end() override -> decltype(Range().end()); + + virtual bool isSlice() const override; + + void setPtr(IndefinitIndexBase* MAPtr, + IndefinitIndexBase* ownPtr); + + private: + + MultiArray& multiArrayRef; + IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range + IndefinitIndexBase* mOwnPtr; // idx ptr for own Range + Index mSlicePos; + }; + + +} + +#include "slice.cc" + +#endif