From c13c51bd5180e838f89c21473a84677a1c17f513 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 25 Jul 2017 17:46:59 +0200 Subject: [PATCH] simplify index/range framework -> no native major index system (planned to implement separate class) --- src/index_base.cc | 128 ++++++++++----------------------------------- src/index_base.h | 69 +++++++++--------------- src/multi_range.cc | 128 ++++++++++++++++++++++++++++++--------------- src/multi_range.h | 72 +++++++++---------------- src/range_base.cc | 112 --------------------------------------- src/range_base.h | 84 +++++------------------------ 6 files changed, 175 insertions(+), 418 deletions(-) diff --git a/src/index_base.cc b/src/index_base.cc index cb72e46..39c641e 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -3,114 +3,40 @@ namespace MultiArrayTools { - /************************ - * IndefinitIndexBase * - ************************/ + /***************** + * IndexBase * + *****************/ + + IndexBase::IndexBase(const std::shared_ptr& range, + size_t pos) : mRangePtr(range), + mPos(pos) {} - size_t IndefinitIndexBase::pos() const + bool IndexBase::operator==(const IndexBase& in) const { - //assert(not virt()); - return static_cast( mPos ); + return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get(); + } + + bool IndexBase::operator!=(const IndexBase& in) const + { + return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get(); + } + + size_t IndexBase::pos() const + { + return mPos; } - bool IndefinitIndexBase::operator==(const IndefinitIndexBase& in) const + operator IndexBase::size_t() const { - return rangeType() == in.rangeType() and pos() == in.pos(); + return mPos; } - bool IndefinitIndexBase::operator!=(const IndefinitIndexBase& in) const - { - return rangeType() != in.rangeType() or pos() != in.pos(); - } - - IndefinitIndexBase& IndefinitIndexBase::setPos(size_t pos, IndefinitIndexBase* ctrlPtr) - { - 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; - } + /********************** + * IndexInterface * + **********************/ - 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; - } - - 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; - } + template + IndexInterface::IndexInterface(const RangeBase& range, size_t pos) : + IndexBase(range, pos) {} - 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 - { - if(mPos < 0){ - return mPos; - } - else if(mPos >= static_cast( max() ) ){ - return mPos - max() + 1; - } - else { - return 0; - } - } - - bool IndefinitIndexBase::atEdge() const - { - return static_cast( mPos ) == max(); - } - - bool IndefinitIndexBase::master() const - { - return mMajor.size() == 0; - } - - IndefinitIndexBase& IndefinitIndexBase::subOrd(IndefinitIndexBase* major) - { - mMajor[major] = major->giveSubStepSize(this); - return *this; - } - - /************** - * IndexBase * - **************/ - - template - IndexBase::IndexBase(std::shared_ptr >& rangePtr) : mRangePtr(rangePtr) {} - - template - size_t IndexBase::max() const - { - //assert(not virt()); - return mRange->size(); - } } diff --git a/src/index_base.h b/src/index_base.h index fcd4c30..eff765e 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -14,67 +14,48 @@ namespace MultiArrayTools { - class IndefinitIndexBase + class IndexBase { public: - DEFAULT_MEMBERS(IndefinitIndexBase); - virtual ~IndefinitIndexBase() = default; + DEFAULT_MEMBERS(IndexBase); + IndexBase(const RangeBase& range, size_t pos); + virtual ~IndexBase() = default; - virtual IndefinitIndexBase& operator=(size_t pos) = 0; - virtual IndefinitIndexBase& operator++() = 0; - virtual IndefinitIndexBase& operator--() = 0; - virtual IndefinitIndexBase& operator+=(int n) = 0; - virtual IndefinitIndexBase& operator-=(int n) = 0; + virtual IndexBase& operator=(size_t pos) = 0; + virtual IndexBase& operator++() = 0; + virtual IndexBase& operator--() = 0; + virtual IndexBase& operator+=(int n) = 0; + virtual IndexBase& operator-=(int n) = 0; - bool operator==(const IndefinitIndexBase& in) const; - bool operator!=(const IndefinitIndexBase& in) const; + bool operator==(const IndexBase& in) const; + bool operator!=(const IndexBase& in) const; virtual size_t dim() const = 0; virtual size_t pos() const; + virtual size_t max() const; + + virtual bool last() const = 0; + virtual bool first() const = 0; - virtual MultiRangeType rangeType() const = 0; - - virtual IndefinitIndexBase& setPos(size_t pos, IndefinitIndexBase* ctrlPtr = nullptr); - virtual IndefinitIndexBase& setPosRel(int relPos, IndefinitIndexBase* ctrlPtr = nullptr); - - 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 master() const; - - virtual IndefinitIndexBase& subOrd(IndefinitIndexBase* major); - virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) = 0; + virtual operator size_t() const; protected: - int mPos; - std::map mMajor; + std::shared_ptr mRangePtr; + size_t mPos; }; - - template - class IndexBase : public IndefinitIndexBase + + template + class IndexInterface : public IndexBase { public: - typedef IndefinitIndexBase IIB; + IndexInterface(const RangeBase& range, size_t pos); - DEFAULT_MEMBERS(IndexBase); - IndexBase(std::shared_ptr >& rangePtr); - - //virtual size_t pos() const override; // = mPos; implement !!! - virtual size_t max() const override; - virtual void copyPos(const Index& in) = 0; - - protected: - - // translate index into position - virtual size_t evaluate(const Index& in) const = 0; - std::shared_ptr > mRangePtr; + MetaType& meta() = 0; + const MetaType& meta() const = 0; }; - + } #include "index_base.cc" diff --git a/src/multi_range.cc b/src/multi_range.cc index 4e310d5..65c63a6 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -362,62 +362,106 @@ namespace MultiArrayTools }; + template + struct SubIndexConstruct + { + template + void construct(std::tuple...>& ip, + const MRange& range) + { + typedef decltype(range.template get()) SubIndexType; + typedef decltype(std::get(ip).get()) wTypeFromIndexPack; + + static_assert(is_same::value, + "inconsiśtent types"); + + std::get(ip).swap( std::make_shared ( range.template get() ) ); + SubIndexConstruct::construct(ip, range); + } + + template + void copy(std::tuple...>& ip, + const MultiIndex& ind) + { + typedef decltype(ind.template get()) SubIndexType; + std::get(ip).swap( std::make_shared( ind.template get() ) ); + SubIndexConstruct::copy(ip, ind); + } + }; + + template <> + struct SubIndexConstruct<0> + { + template + void construct(std::tuple...>& ip, + const MRange& range) + { + typedef decltype(range.template get<0>()) SubIndexType; + typedef decltype(std::get<0>(ip).get()) wTypeFromIndexPack; + + static_assert(std::is_same::value, + "inconsiśtent types"); + + std::get<0>(ip).swap( std::make_shared ( range.template get<0>() ) ); + } + + template + void copy(std::tuple...>& ip, + const MultiIndex& ind) + { + typedef decltype(ind.template get<0>()) SubIndexType; + std::get<0>(ip).swap( std::make_shared( ind.template get<0>() ) ); + } + + }; + + template + struct PosGetter + { + template + size_t makePos(const std::tuple...>& iPtrTup) + { + return std::get(iPtrTup)->pos() + + PosGetter::makePos(iPtrTup) * std::get(iPtrTup)->max(); + } + }; + + template <> + struct PosGetter<0> + { + template + size_t makePos(const std::tuple...>& iPtrTup) + { + return std::get<0>(iPtrTup)->pos(); + } + }; + } template MultiIndex::MultiIndex(const MultiIndex& in) : - IndexBase >(in), - mIPack(in.mIPack) + IndexInterface >(in) { - // THAT's the point: - IndexSubOrder::subOrd(mIPack, this); - IIB::mPos = evaluate(*this); + SubIndexConstruct::copy(mIPack, in); + mPos = PosGetter::makePos(mIPack); } - + template MultiIndex& MultiIndex::operator=(const MultiIndex& in) { - IndexBase >::operator=(in); - mIPack = in.mIPack; - - // THAT's the point: - IndexSubOrder::subOrd(mIPack, this); - IIB::mPos = evaluate(*this); + IndexI::operator=(in); + SubIndexConstruct::copy(mIPack, in); + mPos = PosGetter::makePos(mIPack); return *this; } template - MultiIndex::MultiIndex(RangeBase > const* range) : - IndexBase >(range), - mIPack() + template + MultiIndex::MultiIndex(const std::shared_ptr& range) : + IndexInterface >(range, 0) { - operator=(IB::mRange->begin()); - IIB::mPos = evaluate(*this); - } - - template - MultiIndex::MultiIndex(RangeBase > const* range, - Indices&&... inds) : IndexBase >(range), - mIPack(std::make_tuple(inds...)) - { - IndexSubOrder::subOrd(mIPack, this); - IIB::mPos = evaluate(*this); - } - - template - MultiIndex::MultiIndex(RangeBase > const* range, - const IndexPack& ipack) : IndexBase >(range), - mIPack(ipack) - { - IndexSubOrder::subOrd(mIPack, this); - IIB::mPos = evaluate(*this); - } - - template - MultiIndex::MultiIndex(std::vector >& indexList) - { - mIPack = IndexPackSetter::setFromPointerList(mIPack, indexList); - IndexSubOrder::subOrd(mIPack, this); + SubIndexConstruct::construct(mIPack, *range); + mPos = PosGetter::makePos(mIPack); } template diff --git a/src/multi_range.h b/src/multi_range.h index b877d3f..f746123 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -15,63 +15,41 @@ namespace MultiArrayTools { template - class MultiIndex : public IndexBase > + class MultiIndex : public IndexInterface > { public: + typedef std::tuple...> IndexPack; - typedef IndefinitIndexBase IIB; - typedef IndexBase...> > IB; - typedef std::tuple MetaType; + typedef std::tuple MetaType; + typedef IndexInterface IndexI; - protected: - virtual bool linkLower(IndefinitIndexBase* toLink); - virtual size_t evaluate(const MultiIndex& in) const override; - - IndexPack mIPack; - public: - + MultiIndex() = default; // NO DEFAULT HERE !!! // ( have to subord sub-indices (mMajor) correctly, and not only copy their mMajor pointer to 'in' // which is not major any more in copies!! ) MultiIndex(const MultiIndex& in); MultiIndex& operator=(const MultiIndex& in); - - MultiIndex(RangeBase...> > const* range); - - MultiIndex(RangeBase...> > const* range, - Indices&&... inds); - - MultiIndex(RangeBase...> > const* range, - const IndexPack& ipack); - MultiIndex(std::vector >& indexList); + template + MultiIndex(const std::shared_ptr& range); virtual MultiIndex& operator++() override; virtual MultiIndex& operator--() override; virtual MultiIndex& operator+=(int n) override; virtual MultiIndex& operator-=(int n) override; - - bool operator==(const MultiIndex& in); - bool operator!=(const MultiIndex& in); virtual MultiIndex& operator=(size_t pos) override; - virtual MultiRangeType rangeType() const override; template - auto getIndex() -> decltype(*std::get(mIPack))&; - //typename std::tuple_element...> >::type& getIndex(); - - template - auto getIndex() const -> decltype(*std::get(mIPack))&; + auto get() const -> decltype(*std::get(mIPack))&; //typename std::tuple_element...> >::type const& getIndex() const; - IndefinitIndexBase& get(size_t n); - const IndefinitIndexBase& get(size_t n) const; - - MetaType getMetaPos() const; + const IndexBase& get(size_t n) const; + + virtual MetaType meta() const override; MultiIndex& atMeta(const MetaType& metaPos); MultiIndex& operator()(Indices&&... inds); @@ -79,7 +57,11 @@ namespace MultiArrayTools // dimension of MultiRange; includes ALL degrees of freedom virtual size_t dim() const override; - virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override; + + protected: + + IndexPack mIPack; + }; /***************************** @@ -88,33 +70,29 @@ namespace MultiArrayTools template - class MultiRange : public RangeBase > + class MultiRange : public RangeInterface > { public: typedef std::tuple...> SpaceType; - typedef typename RangeBase >::IndexType + typedef typename RangeInterface >::IndexType IndexType; 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(); template // !!! - auto getRange() const ->; + auto get() const ->; //typename std::tuple_element...> >::type const& getRange() const; - + + size_t dim() const override; size_t size() const override; + const SpaceType& space() const; - virtual MultiRangeType type() const override; - - virtual MultiIndex begin() const override; - virtual MultiIndex end() const override; + virtual typename IndexType begin() const override; + virtual typename IndexType end() const override; - virtual std::shared_ptr indexInstance() const override; + virtual std::shared_ptr index() const override; protected: diff --git a/src/range_base.cc b/src/range_base.cc index cbeddff..a8d3660 100644 --- a/src/range_base.cc +++ b/src/range_base.cc @@ -3,121 +3,9 @@ namespace MultiArrayTools { - /********************* - * MultiRangeType * - *********************/ - - MultiRangeType::MultiRangeType(const RangeType& type) : mType(type), mMultiType(nullptr) {} - - MultiRangeType::MultiRangeType(const std::vector& multiType) : - mType(RangeType::NIL), - mMultiType(new std::vector(multiType)) {} - - MultiRangeType::~MultiRangeType() - { - delete mMultiType; - } - - - MultiRangeType& MultiRangeType::operator=(const RangeType& type) - { - setType(type); - return *this; - } - - MultiRangeType& MultiRangeType::operator=(const std::vector& multiType) - { - setMultiType(multiType); - return *this; - } - - MultiRangeType& MultiRangeType::operator[](size_t num) - { - return mMultiType->at(num); - } - - const MultiRangeType& MultiRangeType::operator[](size_t num) const - { - return mMultiType->at(num); - } - - bool MultiRangeType::multi() const - { - return mMultiType != nullptr; - } - - bool MultiRangeType::operator==(const MultiRangeType& in) const - { - if(multi() xor in.multi()){ - return false; - } - if(multi()){ - return *mMultiType == *in.mMultiType; - } - else { - return mType == in.mType; - } - } - - bool MultiRangeType::operator!=(const MultiRangeType& in) const - { - if(multi() xor in.multi()){ - return true; - } - if(multi()){ - return *mMultiType != *in.mMultiType; - } - else { - return mType != in.mType; - } - } - - void MultiRangeType::setType(RangeType type) - { - mType = type; - if(mMultiType != nullptr){ - delete mMultiType; - } - mMultiType = nullptr; - } - - void MultiRangeType::setMultiType(const std::vector& multiType) - { - mMultiType = new std::vector( multiType ); - mType = RangeType::NIL; - } - - /****************** * RangeBase * ******************/ - template - bool RangeBase::isSubRange() const - { - return false; - } - - template - RangeBase* RangeBase::base() - { - return nullptr; - } - - /********************* - * SubRangeBase * - *********************/ - - template - bool SubRangeBase::isSubRange() const - { - return true; - } - - template - RangeBase* SubRangeBase::base() - { - return mBase; - } } diff --git a/src/range_base.h b/src/range_base.h index 459c4dc..1f2ea3e 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -25,92 +25,32 @@ namespace MultiArrayTools DISTANCE = 8 }; - class MultiRangeType + class RangeBase { public: - DEFAULT_MEMBERS(MultiRangeType); - - MultiRangeType(const RangeType& type); - MultiRangeType(const std::vector& multiType); - - ~MultiRangeType(); - - MultiRangeType& operator=(const RangeType& type); - MultiRangeType& operator=(const std::vector& multiType); - - MultiRangeType& operator[](size_t num); - const MultiRangeType& operator[](size_t num) const; - - bool multi() const; - - bool operator==(const MultiRangeType& in) const; - bool operator!=(const MultiRangeType& in) const; - - private: - void setType(RangeType type); - void setMultiType(const std::vector& multiType); - - RangeType mType; - std::vector* mMultiType; - }; - - class IndefinitRangeBase - { - public: - - virtual ~IndefinitRangeBase() = default; + virtual ~RangeBase() = 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: - // only constructable via Factory + virtual size_t dim() const = 0; + + virtual std::shared_ptr index() const = 0; + + protected: - IndefinitRangeBase() = default; - IndefinitRangeBase(const IndefinitRangeBase& in) = delete; - IndefinitRangeBase& operator=(const IndefinitRangeBase& in) = delete; }; - + template - class RangeBase : public IndefinitRangeBase + class RangeInterface : public RangeBase { public: + typedef Index IndexType; - virtual IndexType begin() const = 0; - virtual IndexType end() const = 0; - virtual RangeBase* base(); - virtual bool isSubRange() const override; - - virtual std::shared_ptr indexInstance() const override; - - protected: - RangeBase() = default; - RangeBase(const RangeBase& in) = delete; - RangeBase& operator=(const RangeBase& in) = delete; + virtual Index begin() = 0; + virtual Index end() = 0; }; - - //template - //auto cross(const Range& r1, const Range& r2) -> /**/; - //template - //auto cross(const Range1& r1, const Range2& r2) -> /**/; - - template - class SubRangeBase : public RangeBase - { - public: - virtual bool isSubRange() const override; - virtual RangeBase* base() override; - protected: - DEFAULT_MEMBERS(SubRangeBase); - RangeBase* mBase; - std::vector mOccupation; - }; - } #include "range_base.cc"