From defa568c5bf565af4aff19b0664a1017f29a7774 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 10 Dec 2017 18:41:53 +0100 Subject: [PATCH] start removing virtualities in Index*; intr VIWB for flexible use instead; Missing reg this: Multi/ContainerIndex, PackNum, OpClasses (NOT COMPILABLE) --- src/index_base.h | 191 +++++++++++++++++++++--------------- src/multi_array_operation.h | 33 ++++--- src/pack_num.h | 63 ++++++------ src/range_base.h | 2 +- src/single_range.h | 80 ++++++++------- 5 files changed, 207 insertions(+), 162 deletions(-) diff --git a/src/index_base.h b/src/index_base.h index 0cce418..7d15ae3 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -10,7 +10,6 @@ #include "base_def.h" #include "range_base.h" - namespace MultiArrayTools { size_t indexId() @@ -25,69 +24,115 @@ namespace MultiArrayTools MULTI = 1, CONT = 2 }; - - class IndexBase + + class VirtualIndexWrapperBase { public: - //DEFAULT_MEMBERS(IndexBase); - - IndexBase() { mId = indexId(); } - IndexBase(IndexBase&& in) = default; - IndexBase& operator=(IndexBase&& in) = default; + DEFAULT_MEMBERS(VirtualIndexWrapperBase); - IndexBase(const std::shared_ptr& range, size_t pos); - virtual ~IndexBase() = default; - virtual IndexType type() const = 0; - - virtual IndexBase& operator=(size_t pos) = 0; - virtual IndexBase& operator++() = 0; - virtual IndexBase& operator--() = 0; - - virtual int pp(std::shared_ptr& idxPtr) = 0; - virtual int mm(std::shared_ptr& idxPtr) = 0; - - 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 size_t pos() const = 0; + virtual size_t max() const = 0; + virtual std::shared_ptr rangePtr() const = 0; + virtual VirtualIndexWrapperBase getPtr(size_t n) const = 0; + virtual intptr_t getPtrNum() const = 0; + }; - virtual bool last() const = 0; - virtual bool first() const = 0; + typedef VirtualIndexWrapperBase VIWB; - //virtual bool locked() const; - //virtual IndexBase& lock(std::shared_ptr& idx); + template + std::shared_ptr > make_viwb(std::shared_ptr idxPtr) + { + return std::make_shared >(idxPtr); + } - virtual std::shared_ptr rangePtr() const; - virtual std::shared_ptr getPtr(size_t n) const = 0; - - virtual size_t getStepSize(size_t n) const = 0; + template + std::shared_ptr > make_viwb(const I& idxPtr) + { + return make_viwb( std::make_shared(idxPtr) ); + } + + template + class IndexWrapper + { + public: - virtual operator size_t() const; + DEFAULT_MEMBERS(IndexWrapper); - virtual std::string id() const { return std::to_string( mId ); } + IndexWrapper(std::shared_ptr& idxPtr); + + virtual IndexType type() const override { return mIdxPtr->type(); } + virtual size_t dim() const override { return mIdxPtr->dim(); } + virtual size_t pos() const override { return mIdxPtr->pos(); } + virtual size_t max() const override { return mIdxPtr->max(); } + virtual std::shared_ptr rangePtr() const override { return mIdxPtr->rangePtr(); } + virtual std::shared_ptr getPtr(size_t n) const override { return mIdxPtr->getv(n); } + virtual intptr_t getPtrNum() const override { return static_cast( mIdxPtr.get() ); }; + + private: + std::shared_ptr mIdxPtr; + }; + + template + class IndexInterface + { + public: + //DEFAULT_MEMBERS(IndexInterface); + + I* THIS() { return static_cast(this); } + + ~IndexInterface() = default; + + IndexType type() const { return I::S_type(THIS()); } + + IndexInterface& operator=(size_t pos) { return I::S_ass_op(THIS(), pos); } + IndexInterface& operator++() { return I::S_pp_op(THIS()); } + IndexInterface& operator--() { return I::S_mm_op(THIS()); } + + int pp(std::shared_ptr& idxPtr) { return I::S_pp(THIS()); } + int mm(std::shared_ptr& idxPtr) { return I::S_mm(THIS()); } + + bool operator==(const IndexInterface& in) const; + bool operator!=(const IndexInterface& in) const; + + size_t dim() const { return I::S_dim(THIS()); } + size_t pos() const; + size_t max() const; + + bool last() const { return I::S_last(THIS()); } + bool first() const { return I::S_first(THIS()); } + + std::shared_ptr rangePtr() const; + + template + auto getPtr() const -> decltype(I::S_get(THIS())) { return I::S_get(THIS()); } + + std::shared_ptr getVPtr(size_t n) const { return I::S_getVPtr(THIS(),n); } + + size_t getStepSize(size_t n) const { return I::S_getStepSize(THIS(),n); } + + operator size_t() const; + + std::string id() const { return I::S_id(THIS()); } + + MetaType meta() const { return I::S_meta(THIS()); } + IndexInterface& at(const MetaType& meta) { return I::S_at(THIS(), meta); } + protected: + IndexInterface() { mId = indexId(); } + IndexInterface(IndexInterface&& in) = default; + IndexInterface& operator=(IndexInterface&& in) = default; + + IndexInterface(const std::shared_ptr& range, size_t pos); + std::shared_ptr mRangePtr; size_t mPos; size_t mId; - //bool mLocked = false; - }; - - template - class IndexInterface : public IndexBase - { - public: - - DEFAULT_MEMBERS(IndexInterface); - IndexInterface(const std::shared_ptr& rangePtr, size_t pos); - virtual MetaType meta() const = 0; - virtual IndexInterface& at(const MetaType& meta) = 0; }; - + } /* ========================= * @@ -97,66 +142,52 @@ namespace MultiArrayTools namespace MultiArrayTools { /***************** - * IndexBase * + * IndexInterface * *****************/ - IndexBase::IndexBase(const std::shared_ptr& range, + template + IndexInterface::IndexInterface(const std::shared_ptr& range, size_t pos) : mRangePtr(range), mPos(pos) { mId = indexId(); } - - bool IndexBase::operator==(const IndexBase& in) const + + template + bool IndexInterface::operator==(const IndexInterface& in) const { return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get(); } - - bool IndexBase::operator!=(const IndexBase& in) const + + template + bool IndexInterface::operator!=(const IndexInterface& in) const { return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get(); } - - size_t IndexBase::pos() const + + template + size_t IndexInterface::pos() const { return mPos; } - size_t IndexBase::max() const + template + size_t IndexInterface::max() const { return mRangePtr->size(); } - /* - bool IndexBase::locked() const - { - return mLocked; - } - - IndexBase& IndexBase::lock(std::shared_ptr& idx) - { - mLocked = (idx.get() == this); - return *this; - } - */ - std::shared_ptr IndexBase::rangePtr() const + template + std::shared_ptr IndexInterface::rangePtr() const { return mRangePtr; } - - IndexBase::operator size_t() const + + template + IndexInterface::operator size_t() const { return pos(); } - - /********************** - * IndexInterface * - **********************/ - - template - IndexInterface::IndexInterface(const std::shared_ptr& rangePtr, size_t pos) : - IndexBase(rangePtr, pos) {} - } #endif diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 90537b7..9daf13e 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -32,14 +32,14 @@ namespace MultiArrayTools * */ - void seekIndexInst(std::shared_ptr i, std::vector >& ivec); + void seekIndexInst(std::shared_ptr i, std::vector >& ivec); // typedef std::pair BTSS; - BTSS getBlockType(std::shared_ptr i, - std::shared_ptr j, + BTSS getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first, size_t higherStepSize = 1); template @@ -50,12 +50,12 @@ namespace MultiArrayTools size_t getBTNum(const std::vector& mp, BlockType bt); - void minimizeAppearanceOfType(std::map, std::vector >& mp, + void minimizeAppearanceOfType(std::map, std::vector >& mp, BlockType bt); template - std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OpClass& second); + std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, + const OpClass& second); /* template class OperationBase @@ -133,7 +133,7 @@ namespace MultiArrayTools MBlock& get(); const Block& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex) const; const OperationMaster& block() const; protected: @@ -163,7 +163,7 @@ namespace MultiArrayTools const Block& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex) const; const ConstOperationRoot& block() const; protected: @@ -194,7 +194,7 @@ namespace MultiArrayTools const MBlock& get() const; MBlock& get(); - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex) const; const OperationRoot& block() const; protected: @@ -219,7 +219,7 @@ namespace MultiArrayTools const BlockResult& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex) const; const Operation& block() const; protected: @@ -239,7 +239,7 @@ namespace MultiArrayTools const BlockResult& get() const; - std::vector block(const std::shared_ptr blockIndex) const; + std::vector block(const std::shared_ptr blockIndex) const; const Contraction& block() const; protected: @@ -262,7 +262,7 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } - void seekIndexInst(std::shared_ptr i, std::vector >& ivec) + void seekIndexInst(std::shared_ptr i, std::vector >& ivec) { for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ auto ii = i->getPtr(inum); @@ -274,8 +274,8 @@ namespace MultiArrayTools } } - BTSS getBlockType(std::shared_ptr i, - std::shared_ptr j, + BTSS getBlockType(std::shared_ptr i, + std::shared_ptr j, bool first, size_t higherStepSize) { // returning BlockType and step size is redundant (change in the future) @@ -440,11 +440,12 @@ namespace MultiArrayTools (*mIndex) = *index; auto blockIndex = seekBlockIndex( mIndex, second); - + intptr_t blockIndexNum = blockIndex->getPtrNum(); + block(blockIndex); second.block(blockIndex); - for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) ){ + for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){ get() = mSecond.get(); } } diff --git a/src/pack_num.h b/src/pack_num.h index 9fa0fb7..8fb37a4 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -21,16 +21,16 @@ namespace MultiArrayHelper struct PackNum { template - static IndexBase& getIndex(IndexType& in, size_t n) + static std::shared_ptr getIndex(const IndexType& in, size_t n) { if(n == N){ - return in.template get(); + return make_viwb( in.template get() ); } else { return PackNum::getIndex(in, n); } } - + /* template static const IndexBase& getIndex(const IndexType& in, size_t n) { @@ -40,19 +40,19 @@ namespace MultiArrayHelper else { return PackNum::getIndex(in, n); } - } + }*/ template - static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) { if(n == N){ - return in.template getPtr(); + return make_viwb( in.template getPtr() ); } else { return PackNum::getIndexPtr(in, n); } } - + /* template static void lock(std::tuple...>& ip, std::shared_ptr toLock) @@ -60,7 +60,7 @@ namespace MultiArrayHelper std::get(ip)->lock(toLock); PackNum::lock(ip, toLock); } - + */ template static void initBlockSizes(std::array& bs, std::tuple...>& ip) @@ -85,18 +85,18 @@ namespace MultiArrayHelper template static inline int pp(std::tuple...>& ip, std::array& bs, - std::shared_ptr idxPtr) + intptr_t idxPtrNum) { auto& siPtr = std::get(ip); //VCHECK(siPtr.id()); - if(siPtr.get() == idxPtr.get()){ - return PackNum::pp(ip, bs, idxPtr); + if(static_cast(siPtr.get()) == idxPtrNum){ + return PackNum::pp(ip, bs, idxPtrNum; } else { int tmp = siPtr->pp(idxPtr); if(siPtr->pos() == siPtr->max()){ (*siPtr) = 0; - return PackNum::pp(ip, bs, idxPtr) - siPtr->max() + 1; + return PackNum::pp(ip, bs, idxPtrNum) - siPtr->max() + 1; } else { return tmp * std::get(bs); @@ -117,22 +117,23 @@ namespace MultiArrayHelper } } + // !!!! template static inline int mm(std::tuple...>& ip, std::array& bs, - std::shared_ptr idxPtr) + intptr_t idxPtrNum) { auto& siPtr = std::get(ip); - if(siPtr.get() == idxPtr.get()){ - return std::get(bs) + PackNum::mm(ip, bs, idxPtr); + if(static_cast(siPtr.get()) == idxPtrNum){ + return std::get(bs) + PackNum::mm(ip, bs, idxPtrNum); } else { if(siPtr->first()){ (*siPtr) = siPtr->max() - 1; - return PackNum::mm(ip, bs, idxPtr) - siPtr->max() + 1; + return PackNum::mm(ip, bs, idxPtrNum) - siPtr->max() + 1; } else { - return siPtr->mm(idxPtr); + return siPtr->mm(idxPtrNum); } } } @@ -257,31 +258,31 @@ namespace MultiArrayHelper template<> struct PackNum<0> { - template - static IndexBase& getIndex(MultiIndex& in, size_t n) + template + static std::shared_ptr getIndex(const IndexType& in, size_t n) { - return in.template get<0>(); + return make_viwb( in.template get<0>() ); } - + /* template static const IndexBase& getIndex(const MultiIndex& in, size_t n) { return in.template get<0>(); } - + */ template static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) { - return in.template getPtr<0>(); + return make_viwb( in.template get<0>() ); } - + /* template static void lock(std::tuple...>& ip, std::shared_ptr toLock) { std::get<0>(ip)->lock(toLock); } - + */ template static void initBlockSizes(std::array& bs, std::tuple...>& ip) @@ -299,14 +300,14 @@ namespace MultiArrayHelper template static inline int pp(std::tuple...>& ip, std::array& bs, - std::shared_ptr idxPtr) + intptr_t idxPtrNum) { auto& siPtr = std::get<0>(ip); - if(siPtr.get() == idxPtr.get()){ + if(static_cast(siPtr.get()) == idxPtrNum){ return std::get<0>(bs); } else { - int tmp = siPtr->pp(idxPtr); + int tmp = siPtr->pp(idxPtrNum); return tmp * std::get<1>(bs); } } @@ -321,15 +322,15 @@ namespace MultiArrayHelper template static inline int mm(std::tuple...>& ip, std::array& bs, - std::shared_ptr idxPtr) + intptr_t idxPtrNum) { auto& siPtr = std::get<0>(ip); - if(siPtr.get() == idxPtr.get()){ + if(static_cast(siPtr.get()) == idxPtrNum){ return std::get<0>(bs); //return 1; } else { - return siPtr->mm(idxPtr); + return siPtr->mm(idxPtrNum); } } diff --git a/src/range_base.h b/src/range_base.h index d4c0437..2c564a6 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -48,7 +48,7 @@ namespace MultiArrayTools virtual size_t size() const = 0; virtual size_t dim() const = 0; - virtual std::shared_ptr index() const = 0; + virtual std::shared_ptr index() const = 0; bool operator==(const RangeBase& in) const; bool operator!=(const RangeBase& in) const; diff --git a/src/single_range.h b/src/single_range.h index 33d0ffa..90f0193 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -15,11 +15,11 @@ namespace MultiArrayTools { template - class SingleIndex : public IndexInterface + class SingleIndex : public IndexInterface,U> { public: - typedef IndexBase IB; + typedef IndexBase,U> IB; typedef U MetaType; typedef SingleRange RangeType; @@ -27,26 +27,30 @@ namespace MultiArrayTools SingleIndex(const std::shared_ptr >& range); - virtual IndexType type() const override; + static IndexType S_type(SingleIndex* i) const; - virtual SingleIndex& operator=(size_t pos) override; - virtual SingleIndex& operator++() override; - virtual SingleIndex& operator--() override; + static SingleIndex& S_ass_op(SingleIndex* i, size_t pos); + static SingleIndex& S_pp_op(SingleIndex* i); + static SingleIndex& S_mm_op(SingleIndex* i); - virtual int pp(std::shared_ptr& idxPtr) override; - virtual int mm(std::shared_ptr& idxPtr) override; + static int S_pp(SingleIndex* i, std::shared_ptr& idxPtr); + static int S_mm(SingleIndex* i, std::shared_ptr& idxPtr); - virtual U meta() const override; - virtual SingleIndex& at(const U& metaPos) override; + static U S_meta(SingleIndex* i) const; + static SingleIndex& S_at(SingleIndex* i, const U& metaPos); - virtual size_t dim() const override; // = 1 - virtual bool last() const override; - virtual bool first() const override; + static size_t S_dim(SingleIndex* i) const; // = 1 + static bool S_last(SingleIndex* i) const; + static bool S_first(SingleIndex* i) const; - virtual std::shared_ptr getPtr(size_t n) const override; - virtual size_t getStepSize(size_t n) const override; + template + static void S_getPtr(SingleIndex* i) const; + + std::shared_ptr getVPtr(size_t n) const; - virtual std::string id() const override { return std::string("sin") + std::to_string(IB::mId); } + static size_t S_getStepSize(SingleIndex* i, size_t n) const; + + static std::string S_id(SingleIndex* i) const { return std::string("sin") + std::to_string(IB::mId); } }; template @@ -77,7 +81,7 @@ namespace MultiArrayTools virtual IndexType begin() const override; virtual IndexType end() const override; - virtual std::shared_ptr index() const override; + virtual std::shared_ptr index() const override; friend SingleRangeFactory; @@ -108,85 +112,92 @@ namespace MultiArrayTools IndexInterface(range, 0) {} template - IndexType SingleIndex::type() const + IndexType SingleIndex::S_type() const { return IndexType::SINGLE; } template - SingleIndex& SingleIndex::operator=(size_t pos) + SingleIndex& SingleIndex::S_ass_op(size_t pos) { IB::mPos = pos; return *this; } template - SingleIndex& SingleIndex::operator++() + SingleIndex& SingleIndex::S_pp_op() { ++IB::mPos; return *this; } template - SingleIndex& SingleIndex::operator--() + SingleIndex& SingleIndex::S_mm_op() { --IB::mPos; return *this; } template - int SingleIndex::pp(std::shared_ptr& idxPtr) + int SingleIndex::S_pp(std::shared_ptr& idxPtr) { ++(*this); return 1; } template - int SingleIndex::mm(std::shared_ptr& idxPtr) + int SingleIndex::S_mm(std::shared_ptr& idxPtr) { --(*this); return 1; } template - U SingleIndex::meta() const + U SingleIndex::S_meta() const { return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); } template - SingleIndex& SingleIndex::at(const U& metaPos) + SingleIndex& SingleIndex::S_at(const U& metaPos) { operator=( std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ) ); return *this; } template - size_t SingleIndex::dim() const + size_t SingleIndex::S_dim() const { return 1; } template - bool SingleIndex::last() const + bool SingleIndex::S_last() const { return IB::mPos == IB::mRangePtr->size() - 1; } template - bool SingleIndex::first() const + bool SingleIndex::S_first() const { return IB::mPos == 0; } template - std::shared_ptr SingleIndex::getPtr(size_t n) const + template + void SingleIndex::S_getPtr() const { - return std::shared_ptr(); + return; } + template + std::shared_ptr SingleIndex::getVPtr(size_t n) const + { + return std::shared_ptr(); + } + template - size_t SingleIndex::getStepSize(size_t n) const + size_t SingleIndex::S_getStepSize(size_t n) const { return 1; } @@ -267,11 +278,12 @@ namespace MultiArrayTools // put this in the interface class !!! template - std::shared_ptr SingleRange::index() const + std::shared_ptr SingleRange::index() const { - return std::make_shared > + return std::make_shared + ( std::make_shared > ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); + ( std::shared_ptr( RB::mThis ) ) ) ); } }