From b63d56e3158c396085be3db964594b18f11222b6 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 24 May 2017 19:01:02 +0200 Subject: [PATCH] ...continuing --- src/base_def.h | 3 + src/index_base.cc | 114 +++++++++++++++-------------------- src/index_base.h | 27 +++------ src/multi_array_operation.cc | 17 +++--- src/multi_array_operation.h | 17 ++---- src/multi_range.cc | 82 +++++++++---------------- src/multi_range.h | 23 +++---- src/range_base.cc | 7 ++- src/range_base.h | 22 +++++-- 9 files changed, 133 insertions(+), 179 deletions(-) diff --git a/src/base_def.h b/src/base_def.h index f502097..98ab87c 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -51,6 +51,9 @@ namespace MultiArrayTools // range_base.h class MultiRangeType; + // range_base.h + class IndefinitRangeBase; + // range_base.h template class RangeBase; diff --git a/src/index_base.cc b/src/index_base.cc index 1d6307f..d530340 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -7,37 +7,68 @@ namespace MultiArrayTools * IndefinitIndexBase * ************************/ - IndefinitIndexBase::~IndefinitIndexBase() - { - mMajor = nullptr; - } - size_t IndefinitIndexBase::pos() const { //assert(not virt()); return static_cast( mPos ); } - - void IndefinitIndexBase::setPos(size_t pos) + + bool IndefinitIndexBase::operator==(const IndefinitIndexBase& i) const + { + return rangeType() == in.rangeType() and pos() == in.pos(); + } + + bool IndefinitIndexBase::operator!=(const IndefinitIndexBase& i) const + { + return rangeType() != in.rangeType() or pos() != in.pos(); + } + + IndefinitIndexBase& IndefinitIndexBase::setPos(size_t pos, IndefinitIndexBase* ctrlPtr) { - //CHECK; - //assert(not virt()); mPos = pos; + for(auto mm: mMajor){ + if(mm.first == ctrlPtr){ + continue; + } + mm.first->setPos(mm.first->pos() % mm.second + mm.second * pos, this); + } + return *this; } - void IndefinitIndexBase::setPosRel(int relPos) + IndefinitIndexBase& IndefinitIndexBase::setPosRel(int relPos, IndefinitIndexBase* ctrlPtr) { mPos += relPos; + for(auto mm: mMajor){ + if(mm.first == ctrlPtr){ + continue; + } + mm.first->setPosRel(mm.second * relPos, this); + } + return *this; } - // MAJOR INDEX UPDATE !!!!! - void IndefinitIndexBase::toFirst() + + IndefinitIndexBase& IndefinitIndexBase::toFirst(IndefinitIndexBase* ctrlPtr) { mPos = 0; + for(auto mm: mMajor){ + if(mm.first == ctrlPtr){ + continue; + } + mm.first->setPos(mm.first->pos() % mm.second, this); + } + return *this; } - void IndefinitIndexBase::toLast() + IndefinitIndexBase& IndefinitIndexBase::toLast(IndefinitIndexBase* ctrlPtr) { mPos = max() - 1; + for(auto mm: mMajor){ + if(mm.first == ctrlPtr){ + continue; + } + mm.first->setPos(mm.first->pos() % mm.second + mm.second * mPos, this); + } + return *this; } int IndefinitIndexBase::outOfRange() const @@ -58,49 +89,15 @@ namespace MultiArrayTools return mPos == max(); } - bool IndefinitIndexBase::toNull() const + bool IndefinitIndexBase::master() const { - //assert(not virt()); - return true; + return mMajor.size() == 0; } - void IndefinitIndexBase::evalMajor() + IndefinitIndexBase& IndefinitIndexBase::subOrd(IndefinitIndexBase* major) { - //assert(not virt()); - if(not master()){ - //int start = mMajor->pos(); - mMajor->eval(); - //VCHECK(static_cast( mMajor->pos() ) - start); - } - } - - void IndefinitIndexBase::evalMajor(int num) - { - //assert(not virt()); - //CHECK; - if(not master()){ - //int start = mMajor->pos(); - mMajor->setPosRel( num * mMajorStep); - //VCHECK(static_cast( mMajor->pos() ) - start); - } - } - - bool IndefinitIndexBase::master() - { - //assert(not virt()); - return mMajor == nullptr; - } - - void IndefinitIndexBase::subOrd(IndefinitIndexBase* major) - { - //assert(not virt()); - mMajor = major; - mMajorStep = mMajor->giveSubStepSize(this); - } - - size_t IndefinitIndexBase::majorStep() const - { - return mMajorStep; + mMajor[major] = major->giveSubStepSize(this); + return *this; } /************** @@ -117,13 +114,6 @@ namespace MultiArrayTools return mRange->size(); } - template - bool IndexBase::toNull() const - { - //assert(not virt()); - return mRange == nullptr; - } - template void IndexBase::assignRange(RangeBase const* range) { @@ -132,10 +122,4 @@ namespace MultiArrayTools mRange = range; } } - - template - void IndexBase::eval() - { - setPos( evaluate(*dynamic_cast( this )) ); - } } diff --git a/src/index_base.h b/src/index_base.h index 4732999..bd9676f 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -18,7 +18,7 @@ namespace MultiArrayTools { public: DEFAULT_MEMBERS(IndefinitIndexBase); - virtual ~IndefinitIndexBase(); + virtual ~IndefinitIndexBase() = default; virtual IndefinitIndexBase& operator=(size_t pos) = 0; virtual IndefinitIndexBase& operator++() = 0; @@ -26,32 +26,26 @@ namespace MultiArrayTools virtual IndefinitIndexBase& operator+=(int n) = 0; virtual IndefinitIndexBase& operator-=(int n) = 0; - // Make this somehow better... !!! - //virtual bool operator==(const IndefinitIndexBase& i) = 0; - //virtual bool operator!=(const IndefinitIndexBase& i) = 0; + bool operator==(const IndefinitIndexBase& i) const; + bool operator!=(const IndefinitIndexBase& i) const; virtual size_t dim() const = 0; virtual size_t pos() const; virtual MultiRangeType rangeType() const = 0; - virtual void setPos(size_t pos); - virtual void setPosRel(int relPos); + virtual IndefinitIndexBase& setPos(size_t pos, IndefinitIndexBase* ctrlPtr = nullptr); + virtual IndefinitIndexBase& setPosRel(int relPos, IndefinitIndexBase* ctrlPtr = nullptr); - virtual IndefinitIndexBase& toFirst(); - virtual IndefinitIndexBase& toLast(); + virtual IndefinitIndexBase& toFirst(IndefinitIndexBase* ctrlPtr = nullptr); + virtual IndefinitIndexBase& toLast(IndefinitIndexBase* ctrlPtr = nullptr); virtual size_t max() const = 0; virtual int outOfRange() const; virtual bool atEdge() const; - virtual bool toNull() const; + virtual bool master() const; - virtual void eval() = 0; - virtual void evalMajor(); - virtual void evalMajor(int num); - virtual bool master(); - - virtual void subOrd(IndefinitIndexBase* major); + virtual IndefinitIndexBase& subOrd(IndefinitIndexBase* major); virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0; protected: @@ -72,11 +66,8 @@ namespace MultiArrayTools //virtual size_t pos() const override; // = mPos; implement !!! virtual size_t max() const override; - virtual bool toNull() const override; virtual void assignRange(RangeBase const* range); - - virtual void eval() override; virtual void copyPos(const Index& in) = 0; protected: diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 513295f..bb58cfa 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -291,7 +291,7 @@ namespace MultiArrayTools { return mNm; } - + template MultiArrayOperationRoot& MultiArrayOperationRoot::operator[](const IndexType& ind) { @@ -469,14 +469,15 @@ namespace MultiArrayTools mIndex = dynamic_cast( in.index() ); 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; - // !!!!! + IndexList il = in.getIndices(); + setInternalIndex(il); + in.setInternalIndex(il); MultiArray cnt(mRoot->range()); MultiArrayOperationRoot cnto(cnt, mRoot.name()); @@ -485,7 +486,6 @@ namespace MultiArrayTools get() += in.get(); cnto.get() += 1.; } - mRoot.freeIndex(); // CHECK whether T / size_t mixture works!! mRoot /= cnt(mRoot.name(), true); return *this; @@ -497,16 +497,17 @@ namespace MultiArrayTools operator=(const ConstMultiArrayOperationRoot& in) { mIndex = dynamic_cast( in.index() ); - mNm = in.name(); - mIndex.name(mNm); // to be sure... typename TotalInRange::IndexType endIndex = mIndex; ++endIndex.toLast(); std::cout << "map assignment: " << endIndex.pos() << " elements" << std::endl; - //!!!!!!! + IndexList il = in.getIndices(); + setInternalIndex(il); + in.setInternalIndex(il); + MultiArray cnt(mRoot->range()); MultiArrayOperationRoot cnto(cnt, mRoot.name()); for(mIndex.toFirst(), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){ diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index e5ddbda..0d7d7a3 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -13,7 +13,7 @@ namespace MultiArrayTools { - typedef std::vector > IndexList; + typedef std::map > IndexList; template class MultiArrayOperationBase @@ -29,12 +29,8 @@ namespace MultiArrayTools const IndefinitIndexBase& index() const = 0; virtual const T& get() const = 0; - - virtual IndexList getIndices() const = 0; - virtual void setInternalIndex(const IndexList& il) = 0; }; - - + template class MutableMultiArrayOperationBase : public MultiArrayOperationBase { @@ -143,7 +139,6 @@ namespace MultiArrayTools virtual const T& get() const override; const Name& name() const; - const MultiArrayBase& getCont() const { return mArrayRef; } template @@ -216,7 +211,6 @@ namespace MultiArrayTools virtual const T& get() const override; const Name& name() const; - const MultiArrayBase& getCont() const { return mArrayRef; } template @@ -250,6 +244,7 @@ namespace MultiArrayTools virtual const T& get() const override; virtual T& get() override; + // !!!! protected: IndexMapFunction mMF; @@ -297,9 +292,8 @@ namespace MultiArrayTools auto operator/(const MAOp2& sec) -> decltype(operator()(std::divides(), sec)); virtual size_t argNum() const override; - virtual const T& get() const override; - + protected: mutable T mVal; @@ -355,9 +349,8 @@ namespace MultiArrayTools auto operator/(const MAOp2& sec) -> decltype(operator()(std::divides(), sec)); virtual size_t argNum() const override; - virtual const T& get() const override; - + protected: mutable T mVal; diff --git a/src/multi_range.cc b/src/multi_range.cc index 72e63c5..6d996dc 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -77,17 +77,18 @@ namespace MultiArrayTools template struct SubIteration { + // use 'plus' as few as possible !! template static inline void plus(MultiIndex& index, int num) { auto& si = index.template getIndex(); - si.setPosRel(num); + si.setPosRel(num, &index); size_t oor = si.outOfRange(); - if(oor > 0){ + while(oor > 0){ SubIteration::pp(index); SubIteration::plus(index, -si.max()); } - else if(oor < 0){ + while(oor < 0){ SubIteration::mm(index); SubIteration::plus(index, si.max()); } @@ -97,13 +98,12 @@ namespace MultiArrayTools static inline void pp(MultiIndex& index) { auto& si = index.template getIndex(); - //IndefinitIndexBase& si = index.get(DIGIT); - if(si.pos() == si.max()-1){ - si.setPos(0); + if(si.pos() == si.atEdge()){ + si.setPos(0, &index); SubIteration::pp(index); } else { - si.setPosRel(1); + si.setPosRel(1, &index); } } @@ -111,13 +111,12 @@ namespace MultiArrayTools static inline void mm(MultiIndex& index) { auto& si = index.template getIndex(); - //IndefinitIndexBase& si = index.get(DIGIT); - if(si.pos() == si.max()-1){ - si.setPos(si.max()-1); + if(si.pos() == si.atEdge()){ + si.setPos(si.atEdge(), &index); SubIteration::mm(index); } else { - si.setPosRel(1); + si.setPosRel(1, &index); } } }; @@ -129,45 +128,21 @@ namespace MultiArrayTools static inline void plus(MultiIndex& index, int num) { auto& si = index.template getIndex<0>(); - //IndefinitIndexBase& si = index.get(0); - si.setPosRel(num); + si.setPosRel(num, &index); } template static inline void pp(MultiIndex& index) { auto& si = index.template getIndex<0>(); - //IndefinitIndexBase& si = index.get(0); - si.setPosRel(1); + si.setPosRel(1, &index); } template static inline void mm(MultiIndex& index) { auto& si = index.template getIndex<0>(); - //IndefinitIndexBase& si = index.get(0); - si.setPosRel(-1); - } - }; - - template - struct TupleNamer - { - template - static void nameTuple(IndexPack& iPack, Name& name) - { - std::get(iPack).name(name.get(N)); - TupleNamer::nameTuple(iPack, name); - } - }; - - template <> - struct TupleNamer<0> - { - template - static void nameTuple(IndexPack& iPack, Name& name) - { - std::get<0>(iPack).name(name.get(0)); + si.setPosRel(-1, &index); } }; @@ -415,6 +390,10 @@ namespace MultiArrayTools IIB::mPos = evaluate(*this); } + template + MultiIndex::MultiIndex(std::vector >& indexList) : + // !!!! + template MultiIndex& MultiIndex::operator++() { @@ -464,7 +443,7 @@ namespace MultiArrayTools } template - IndefinitIndexBase& MultiIndex::operator=(size_t pos) + MultiIndex& MultiIndex::operator=(size_t pos) { IIB::setPos( pos ); IndexPackSetter::setIndexPack(mIPack, pos); @@ -484,20 +463,6 @@ namespace MultiArrayTools return res; } - template - void MultiIndex::name(const Name& nm) - { - IIB::mName = nm.own(); - if(nm.size() >= sizeof...(Indices)){ - TupleNamer::nameTuple(mIPack, nm); - } - else { - Name nm2 = nm; - nm2.autoName(sizeof...(Indices)); - TupleNamer::nameTuple(mIPack, nm2); - } - } - template MultiIndex& MultiIndex::operator()(Indices&&... inds) { @@ -602,7 +567,7 @@ namespace MultiArrayTools return 0; } } - + template struct TuplePrinter { @@ -685,6 +650,15 @@ namespace MultiArrayTools IndexSetter::setEnd(is,mSpace); return ++MultiIndex(this, is); } + + template + std::shared_ptr MultiRange::indexInstance() const + { + std::tuple...> is; + IndexSetter::setBegin(is,mSpace); + std::shared_ptr sptr(new MultiIndex(this, is)); + return sptr; + } } template diff --git a/src/multi_range.h b/src/multi_range.h index f263ff3..3770262 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -46,8 +46,8 @@ namespace MultiArrayTools MultiIndex(RangeBase...> > const* range, const IndexPack& ipack); - - //virtual ~MultiIndex(); + + MultiIndex(std::vector >& indexList); virtual MultiIndex& operator++() override; virtual MultiIndex& operator--() override; @@ -57,15 +57,15 @@ namespace MultiArrayTools bool operator==(const MultiIndex& in); bool operator!=(const MultiIndex& in); - virtual IIB& operator=(size_t pos) override; + virtual MultiIndex& operator=(size_t pos) override; virtual MultiRangeType rangeType() const override; template - auto getIndex() ->; + auto getIndex() -> decltype(*std::get(mIPack))&; //typename std::tuple_element...> >::type& getIndex(); template - auto getIndex() const ->; + auto getIndex() const -> decltype(*std::get(mIPack))&; //typename std::tuple_element...> >::type const& getIndex() const; IndefinitIndexBase& get(size_t n); @@ -77,18 +77,9 @@ namespace MultiArrayTools MultiIndex& operator()(Indices&&... inds); MultiIndex& operator()(const Indices&... inds); - virtual void name(const Name& nm) override; - // dimension of MultiRange; includes ALL degrees of freedom virtual size_t dim() const override; - - virtual void copyPos(const MultiIndex...>& in) override; - virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override; - - //virtual void eval() override; - //virtual bool virt() const override { return false; } - //virtual void assignRange(RangeBase > const* range) override; }; /***************************** @@ -111,7 +102,7 @@ namespace MultiArrayTools MultiRange(const SpaceType& space); static const size_t dim = sizeof...(Ranges); - + template // !!! return ref to range, not the corresp. shared ptr auto getRange() -> ; //typename std::tuple_element...> >::type& getRange(); @@ -127,6 +118,8 @@ namespace MultiArrayTools virtual MultiIndex begin() const override; virtual MultiIndex end() const override; + + virtual std::shared_ptr indexInstance() const override; protected: SpaceType mSpace; diff --git a/src/range_base.cc b/src/range_base.cc index 0d2f236..b88d57a 100644 --- a/src/range_base.cc +++ b/src/range_base.cc @@ -112,6 +112,11 @@ namespace MultiArrayTools { return true; } - + + template + RangeBase* SubRangeBase::base() + { + return mBase; + } } diff --git a/src/range_base.h b/src/range_base.h index 6c63d13..6774a99 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -55,20 +55,30 @@ namespace MultiArrayTools std::vector* mMultiType; }; + class IndefinitRangeBase + { + public: + virtual ~IndefinitRangeBase() = default; + + virtual size_t size() const = 0; + virtual bool isSubRange() const = 0; + virtual MultiRangeType type() const = 0; + virtual std::shared_ptr indexInstance() const = 0; + + protected: + DEFAULT_MEMBERS(RangeBase); + }; + template - class RangeBase + class RangeBase : public IndefinitRangeBase { public: typedef Index IndexType; - virtual ~RangeBase() = default; - - virtual size_t size() const = 0; virtual Index begin() const = 0; virtual Index end() const = 0; virtual RangeBase* base(); - virtual bool isSubRange() const; - virtual MultiRangeType type() const = 0; + virtual bool isSubRange() const override; protected: DEFAULT_MEMBERS(RangeBase);