diff --git a/src/index_base.cc b/src/index_base.cc index ac3e792..1d6307f 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -9,10 +9,7 @@ namespace MultiArrayTools IndefinitIndexBase::~IndefinitIndexBase() { - //freeLinked(); - mLinked = nullptr; mMajor = nullptr; - mSoftLinked = nullptr; } size_t IndefinitIndexBase::pos() const @@ -21,42 +18,26 @@ namespace MultiArrayTools return static_cast( mPos ); } - const std::string& IndefinitIndexBase::name() const - { - //assert(not virt()); - return mName; - } - - void IndefinitIndexBase::name(const std::string& str) - { - //assert(not virt()); - mName = str; - } - - void IndefinitIndexBase::name(const Name& nm) - { - //assert(not virt()); - mName = nm.own(); - } - void IndefinitIndexBase::setPos(size_t pos) { //CHECK; //assert(not virt()); mPos = pos; - if(linked()){ - mLinked->setPos(mPos); - mLinked->evalMajor(); - } } void IndefinitIndexBase::setPosRel(int relPos) { mPos += relPos; - if(linked()){ - mLinked->setPosRel(relPos); - mLinked->evalMajor(relPos); - } + } + // MAJOR INDEX UPDATE !!!!! + void IndefinitIndexBase::toFirst() + { + mPos = 0; + } + + void IndefinitIndexBase::toLast() + { + mPos = max() - 1; } int IndefinitIndexBase::outOfRange() const @@ -72,6 +53,11 @@ namespace MultiArrayTools } } + bool atEdge() const + { + return mPos == max(); + } + bool IndefinitIndexBase::toNull() const { //assert(not virt()); diff --git a/src/index_base.h b/src/index_base.h index 5037f14..4732999 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -13,7 +13,7 @@ namespace MultiArrayTools { - + class IndefinitIndexBase { public: @@ -33,18 +33,17 @@ namespace MultiArrayTools virtual size_t dim() const = 0; virtual size_t pos() const; - virtual const std::string& name() const; - virtual void name(const std::string& str); - virtual void name(const Name& nm); - virtual MultiRangeType rangeType() const = 0; virtual void setPos(size_t pos); virtual void setPosRel(int relPos); + virtual IndefinitIndexBase& toFirst(); + virtual IndefinitIndexBase& toLast(); + virtual size_t max() const = 0; virtual int outOfRange() const; - + virtual bool atEdge() const; virtual bool toNull() const; virtual void eval() = 0; @@ -54,18 +53,11 @@ namespace MultiArrayTools virtual void subOrd(IndefinitIndexBase* major); virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0; - - virtual size_t majorStep() const; - - //virtual bool virt() const { return true; } protected: - std::string mName; int mPos; - size_t mMajorStep; - - IndefinitIndexBase* mMajor = nullptr; + std::map mMajor; }; template diff --git a/src/multi_array.h b/src/multi_array.h index 713ea5e..0c01757 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -10,6 +10,7 @@ #include #include "base_def.h" +#include "multi_range.h" #include "multi_array_operation.h" #include "manipulator.h" #include "name.h" @@ -17,11 +18,15 @@ namespace MultiArrayTools { - template + template + using TopRange = MultiRange; + + template class MultiArrayBase { public: + typedef TopRange TopRangeType typedef T value_type; class const_iterator : public std::iterator @@ -31,7 +36,7 @@ namespace MultiArrayTools DEFAULT_MEMBERS(const_iterator); const_iterator(const MultiArrayBase& ma); - const_iterator(const MultiArrayBase& ma, const typename Range::IndexType& index); + const_iterator(const MultiArrayBase& ma, const typename TopRangeType::IndexType& index); virtual ~const_iterator() = default; // Requirements: @@ -62,23 +67,23 @@ namespace MultiArrayTools // Multi Array specific: - const typename Range::IndexType& index() const; - typename Range::IndexType& index(); + const typename TopRangeType::IndexType& index() const; + typename TopRangeType::IndexType& index(); protected: MultiArrayBase const* mMAPtr = nullptr; - typename Range::IndexType mIndex; + typename TopRangeType::IndexType mIndex; }; DEFAULT_MEMBERS(MultiArrayBase); - MultiArrayBase(const Range& range); + MultiArrayBase(const TopRangeType& range); virtual ~MultiArrayBase() = default; // only relevant for slices... has no effect for usual multiarrays virtual void link(IndefinitIndexBase* iibPtr) const; - virtual const T& operator[](const typename Range::IndexType& i) const = 0; + virtual const T& operator[](const typename TopRangeType::IndexType& i) const = 0; virtual size_t size() const; virtual bool isSlice() const = 0; @@ -89,31 +94,33 @@ namespace MultiArrayTools virtual auto beginIndex() const -> decltype(Range().begin()); virtual auto endIndex() const -> decltype(Range().end()); - virtual const Range& range() const; + virtual const TopRangeType& range() const; virtual bool isConst() const; template - ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; + ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; template - ConstMultiArrayOperationRoot operator()(const NameType& name, bool master) const; + ConstMultiArrayOperationRoot operator()(const NameType& name, bool master) const; virtual bool isInit() const; protected: bool mInit = false; - std::shared_ptr mRange; + std::shared_ptr mRange; }; - template - class MutableMultiArrayBase : public MultiArrayBase + template + class MutableMultiArrayBase : public MultiArrayBase { public: - typedef typename MultiArrayBase::const_iterator const_iterator; - typedef MultiArrayBase MAB; + typedef typename MultiArrayBase::const_iterator const_iterator; + typedef MultiArrayBase MAB; + typedef typename MAB::TopRangeType TopRangeType; + typedef typename TopRangeType::IndexType IndexType; class iterator : public std::iterator, public std::iterator @@ -123,7 +130,7 @@ namespace MultiArrayTools DEFAULT_MEMBERS(iterator); iterator(MutableMultiArrayBase& ma); - iterator(MutableMultiArrayBase& ma, const typename Range::IndexType& index); + iterator(MutableMultiArrayBase& ma, const IndexType& index); virtual ~iterator() = default; // Requirements: @@ -157,19 +164,19 @@ namespace MultiArrayTools // Multi Array specific: - const typename Range::IndexType& index() const; - typename Range::IndexType& index(); + const IndexType& index() const; + IndexType& index(); protected: MutableMultiArrayBase* mMAPtr = nullptr; - typename Range::IndexType mIndex; + IndexType mIndex; }; DEFAULT_MEMBERS(MutableMultiArrayBase); - MutableMultiArrayBase(const Range& range); + MutableMultiArrayBase(const TopRangeType& range); - virtual T& operator[](const typename Range::IndexType& i) = 0; + virtual T& operator[](const IndexType& i) = 0; virtual iterator begin(); virtual iterator end(); @@ -177,38 +184,40 @@ namespace MultiArrayTools virtual bool isConst() const override; template - ConstMultiArrayOperationRoot operator()(bool x, const NameTypes&... str) const + ConstMultiArrayOperationRoot operator()(bool x, const NameTypes&... str) const { return MAB::operator()(str...); } template - ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; + ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; template - ConstMultiArrayOperationRoot operator()(const NameType& name, bool master) const; + ConstMultiArrayOperationRoot operator()(const NameType& name, bool master) const; template - MultiArrayOperationRoot operator()(const NameTypes&... str); + MultiArrayOperationRoot operator()(const NameTypes&... str); template - MultiArrayOperationRoot operator()(const NameType& name, bool master); + MultiArrayOperationRoot operator()(const NameType& name, bool master); }; - template - class MultiArray : public MutableMultiArrayBase + template + class MultiArray : public MutableMultiArrayBase { public: - typedef MultiArrayBase MAB; - typedef typename MultiArrayBase::const_iterator const_iterator; - typedef typename MutableMultiArrayBase::iterator iterator; + typedef MultiArrayBase MAB; + typedef typename MultiArrayBase::const_iterator const_iterator; + typedef typename MutableMultiArrayBase::iterator iterator; + typedef typename MAB::TopRangeType TopRangeType; + typedef typename TopRangeType::IndexType IndexType; DEFAULT_MEMBERS(MultiArray); - MultiArray(const Range& range); - MultiArray(const Range& range, const std::vector& vec); - MultiArray(const Range& range, std::vector&& vec); + MultiArray(const TopRangeType& range); + MultiArray(const TopRangeType& range, const std::vector& vec); + MultiArray(const TopRangeType& range, std::vector&& vec); template MultiArray(const MultiArray,Range3> in); @@ -218,8 +227,8 @@ namespace MultiArrayTools template MultiArray& operator=(const MultiArray,Range3> in); - T& operator[](const typename Range::IndexType& i) override; - const T& operator[](const typename Range::IndexType& i) const override; + T& operator[](const IndexType& i) override; + const T& operator[](const IndexType& i) const override; virtual bool isConst() const override; virtual bool isSlice() const override; @@ -227,8 +236,8 @@ namespace MultiArrayTools const T* data() const; // virtual void manipulate(ManipulatorBase& mb, - // const typename Range::IndexType& manBegin, - // const typename Range::IndexType& manEnd); + // const typename Ranges...::IndexType& manBegin, + // const typename Ranges...::IndexType& manEnd); template friend class MultiArray; @@ -237,18 +246,20 @@ namespace MultiArrayTools std::vector mCont; }; - template - class FunctionalMultiArray : public MultiArrayBase + template + class FunctionalMultiArray : public MultiArrayBase { public: - typedef MultiArrayBase MAB; - typedef typename MultiArrayBase::const_iterator const_iterator; - + typedef MultiArrayBase MAB; + typedef typename MultiArrayBase::const_iterator const_iterator; + typedef typename MAB::TopRangeType TopRangeType; + typedef typename TopRangeType::IndexType IndexType; + DEFAULT_MEMBERS(FunctionalMultiArray); - //FunctionalMultiArray(const Range& range); - FunctionalMultiArray(const Range& range, const Function& func); + //FunctionalMultiArray(const Ranges...& range); + FunctionalMultiArray(const TopRangeType& range, const Function& func); - virtual const T& operator[](const typename Range::IndexType& i) const override; + virtual const T& operator[](const IndexType& i) const override; virtual bool isConst() const override; virtual bool isSlice() const override; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 34010ea..513295f 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -5,45 +5,30 @@ namespace MultiArrayTools { - /********************************* - * MultiArrayOperationRoot * - *********************************/ - - template - MultiArrayOperationBase::~MultiArrayOperationBase() - { - //delete mIibPtr; - } - - template - const IndefinitIndexBase& MultiArrayOperationBase::index() const - { - return *mIibPtr; - } - - template - void MultiArrayOperationBase::freeIndex() const - { - mIibPtr->freeLinked(); - } - + /********************************* * MultiArrayOperationBase * *********************************/ - + + // purely virtual at the moment + + /********************************* + * MultiArrayOperationRoot * + *********************************/ + template void MultiArrayOperationRoot::performAssignment(const MultiArrayOperationBase& in) { //#error "WRITE MAOR INTRINSIC CONTRACT FUNCTION" //CHECK; - + IndexList il = in.getIndices(); + setInternalIndex(il); + in.setInternalIndex(il); //CHECK; - IndexType& iref = dynamic_cast(*MAOB::mIibPtr); - //CHECK; - const size_t endPos = mArrayRef.endIndex().pos(); + const size_t endPos = mIndex.max(); std::cout << "assignment: " << endPos << " elements" << std::endl; // assignment loop - for(iref = mArrayRef.beginIndex().pos(); iref != mArrayRef.endIndex(); ++iref){ + for(mIndex.toFirst(); not mIndex.atEdge(); ++mIndex){ //std::cout << get() << " / " << in.get() << std::endl; //std::cout << iref.pos() << '\r' << std::flush; get() = in.get(); @@ -51,7 +36,6 @@ namespace MultiArrayTools //assert(not std::isnan( get() )); } //CHECK; - } template @@ -60,37 +44,20 @@ namespace MultiArrayTools const Name& nm) : MutableMultiArrayOperationBase(), mArrayRef(ma), - mIndex(mArrayRef.beginIndex()), - mNm(nm) - { - //CHECK; - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(nm); - //CHECK; - //mIndex.name(nm); - } + mNm(nm) {} template MultiArrayOperationRoot:: MultiArrayOperationRoot(const MultiArrayOperationRoot& in) : MutableMultiArrayOperationBase(), mArrayRef(in.mArrayRef), - mIndex(mArrayRef.beginIndex()), - mNm(in.mNm) - { - //CHECK; - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(mNm); - //CHECK; - //mIndex.name(nm); - } + mNm(in.mNm) {} template MultiArrayOperationRoot& MultiArrayOperationRoot::operator=(const MultiArrayOperationRoot& in) { performAssignment(in); - return *this; } @@ -309,20 +276,14 @@ namespace MultiArrayTools T& MultiArrayOperationRoot::get() { //CHECK; - //return mArrayRef[mIndex]; - //assert(MAOB::mIibPtr == &mIndex); - //VCHECK(mArrayRef[*dynamic_cast(MAOB::mIibPtr)]); - return mArrayRef[*dynamic_cast(MAOB::mIibPtr)]; + return mArrayRef[mIndex]; } template const T& MultiArrayOperationRoot::get() const { //CHECK; - //return mArrayRef[mIndex]; - //assert(MAOB::mIibPtr == &mIndex); - //VCHECK(mArrayRef[*dynamic_cast(MAOB::mIibPtr)]); - return mArrayRef[*dynamic_cast(MAOB::mIibPtr)]; + return mArrayRef[mIndex]; } template @@ -345,14 +306,6 @@ namespace MultiArrayTools return *this; } - template - void MultiArrayOperationRoot::freeIndex() const - { - mIndex = mArrayRef.beginIndex(); - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(mNm); - } - /************************************** * ConstMultiArrayOperationBase * **************************************/ @@ -364,39 +317,21 @@ namespace MultiArrayTools const Name& nm) : MultiArrayOperationBase(), mArrayRef(ma), - mIndex(mArrayRef.beginIndex()), - mNm(nm) - { - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(nm); - //mIndex.name(nm); - } + mNm(nm) {} template ConstMultiArrayOperationRoot:: ConstMultiArrayOperationRoot(const MultiArrayOperationRoot& in) : MultiArrayOperationBase(), mArrayRef(in.getCont()), - mIndex(mArrayRef.beginIndex()), - mNm(in.name()) - { - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(mNm); - //mIndex.name(nm); - } + mNm(in.name()) {} template ConstMultiArrayOperationRoot:: ConstMultiArrayOperationRoot(const ConstMultiArrayOperationRoot& in) : MultiArrayOperationBase(), mArrayRef(in.mArrayRef), - mIndex(mArrayRef.beginIndex()), - mNm(in.mNm) - { - MAOB::mIibPtr = &mIndex; - MAOB::mIibPtr->name(mNm); - //mIndex.name(nm); - } + mNm(in.mNm) {} template template @@ -498,8 +433,7 @@ namespace MultiArrayTools const T& ConstMultiArrayOperationRoot::get() const { //CHECK; - //assert(MAOB::mIibPtr == &mIndex); - return mArrayRef[*dynamic_cast(MAOB::mIibPtr)]; + return mArrayRef[mIndex]; } template @@ -525,16 +459,7 @@ namespace MultiArrayTools const IndexMapFunction& mf) : MutableMultiArrayOperationBase(), mMF(mf), - mRoot(root) - //mIndex(mArrayRef.beginIndex()), - //mNm(nm) - { - //CHECK; - MAOB::mIibPtr = &mIndex; - //CHECK; - //MAOB::mIibPtr->name(nm); - //mIndex.name(nm); - } + mRoot(root) {} template MultiArrayOperationMap& @@ -543,7 +468,6 @@ namespace MultiArrayTools { mIndex = dynamic_cast( in.index() ); - MAOB::mIibPtr = &mIndex; mNm = in.name(); mIndex.name(mNm); // to be sure... @@ -557,7 +481,7 @@ namespace MultiArrayTools MultiArray cnt(mRoot->range()); MultiArrayOperationRoot cnto(cnt, mRoot.name()); - for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){ + for(mIndex.toFirst(), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){ get() += in.get(); cnto.get() += 1.; } @@ -574,19 +498,18 @@ namespace MultiArrayTools { mIndex = dynamic_cast( in.index() ); - MAOB::mIibPtr = &mIndex; mNm = in.name(); mIndex.name(mNm); // to be sure... typename TotalInRange::IndexType endIndex = mIndex; - endIndex.setPos( mIndex.max() ); + ++endIndex.toLast(); std::cout << "map assignment: " << endIndex.pos() << " elements" << std::endl; //!!!!!!! MultiArray cnt(mRoot->range()); MultiArrayOperationRoot cnto(cnt, mRoot.name()); - for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){ + for(mIndex.toFirst(), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){ get() += in.get(); cnto.get() += 1.; } @@ -765,8 +688,8 @@ namespace MultiArrayTools mBeginIndex(runIndex), mEndIndex(runIndex), mRunIndex(runIndex) { - mBeginIndex.setPos(0); - mEndIndex.setPos(mRunIndex.max()); + mBeginIndex.toFirst(); + ++mEndIndex.toLast(); // DON'T link here !! //linkIndicesTo(&mRunIndex); } @@ -817,7 +740,7 @@ namespace MultiArrayTools const std::string& indexName, const MAOps2&... mao) const { - typename Range2::IndexType* ind = dynamic_cast( getLinked(indexName) ); + // return MultiArrayContraction, MAOps2...>(cop, *ind, *this, mao...); } @@ -865,7 +788,7 @@ namespace MultiArrayTools { //CHECK; mOp.reset(); - for(mRunIndex.copyPos( mBeginIndex ); mRunIndex.pos() != mEndIndex.pos(); ++mRunIndex){ + for(mRunIndex = mBeginIndex ; mRunIndex != mEndIndex; ++mRunIndex){ OperationCall:: template callOperation(mOp, mArgs); } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 5cc2317..e5ddbda 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -13,6 +13,8 @@ namespace MultiArrayTools { + typedef std::vector > IndexList; + template class MultiArrayOperationBase { @@ -21,18 +23,15 @@ namespace MultiArrayTools typedef T value_type; MultiArrayOperationBase() /*{ CHECK; }*/ = default; - //MultiArrayOperationBase(const MultiArrayOperationBase& in) = default; virtual ~MultiArrayOperationBase(); virtual size_t argNum() const = 0; - const IndefinitIndexBase& index() const; + const IndefinitIndexBase& index() const = 0; virtual const T& get() const = 0; - - - protected: - mutable IndefinitIndexBase* mIibPtr = nullptr; + virtual IndexList getIndices() const = 0; + virtual void setInternalIndex(const IndexList& il) = 0; }; @@ -42,7 +41,6 @@ namespace MultiArrayTools public: MutableMultiArrayOperationBase() /*{ CHECK; }*/ = default; - //MutableMultiArrayOperationBase(const MutableMultiArrayOperationBase& in) = default; virtual T& get() = 0; }; @@ -157,7 +155,7 @@ namespace MultiArrayTools protected: void performAssignment(const MultiArrayOperationBase& in); - + MutableMultiArrayBase& mArrayRef; mutable IndexType mIndex; Name mNm; @@ -316,13 +314,14 @@ namespace MultiArrayTools typedef MultiArrayOperationBase MAOB; typedef std::tuple OBT; - + typedef typename Range::IndexType RunIndexType; + MultiArrayContraction(const ContractOperation& cop, - const typename Range::IndexType& runIndex, + const RunIndexType& runIndex, const MAOps&... mao); MultiArrayContraction(const ContractOperation& cop, - const typename Range::IndexType& runIndex, + const RunIndexType& runIndex, size_t begin, size_t end, const MAOps&... mao); @@ -364,9 +363,9 @@ namespace MultiArrayTools mutable T mVal; ContractOperation mOp; OBT mArgs; // include first arg also here !!! - typename Range::IndexType mBeginIndex; - typename Range::IndexType mEndIndex; - mutable typename Range::IndexType mRunIndex; + RunIndexType mBeginIndex; + RunIndexType mEndIndex; + mutable RunIndexType mRunIndex; }; }