diff --git a/src/anonymous_range.h b/src/anonymous_range.h index 5b87367..cbf6e82 100644 --- a/src/anonymous_range.h +++ b/src/anonymous_range.h @@ -44,7 +44,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 AnonymousRangeFactory; @@ -155,11 +155,12 @@ namespace MultiArrayTools } // put this in the interface class !!! - std::shared_ptr AnonymousRange::index() const + std::shared_ptr AnonymousRange::index() const { - return std::make_shared - ( std::dynamic_pointer_cast - ( std::shared_ptr( RB::mThis ) ) ); + return std::make_shared + (std::make_shared + ( std::dynamic_pointer_cast + ( std::shared_ptr( RB::mThis ) ) ) ); } } diff --git a/src/base_def.h b/src/base_def.h index ca81680..c84f2d5 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -64,10 +64,15 @@ namespace MultiArrayTools class RangeInterface; // index_base.h - class IndexBase; - + class VirtualIndexWrapperBase; + typedef VirtualIndexWrapperBase VIWB; + // index_base.h - template + template + class IndexWrapper; + + // index_base.h + template class IndexInterface; // single_range.h diff --git a/src/container_range.h b/src/container_range.h index 902ce65..5efa01e 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -17,64 +17,144 @@ namespace MultiArrayTools { template - class ContainerIndex : public IndexInterface > + class ContainerIndex : public IndexInterface, + std::tuple > { public: - typedef IndexBase IB; + typedef IndexInterface, + std::tuple > IB; typedef std::tuple MetaType; typedef std::tuple...> IndexPack; - typedef IndexInterface > IndexI; typedef ContainerRange RangeType; - protected: + private: + + friend IB; + bool mExternControl = false; IndexPack mIPack; std::array mBlockSizes; public: ContainerIndex() = delete; + + template + ContainerIndex(const std::shared_ptr& range); + + template + auto get() const -> decltype( *std::get( mIPack ) )&; + + ContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true + ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices + ContainerIndex& operator()(); // -> sync; just to shorten the code + + private: //ContainerIndex(const ContainerIndex& in); //ContainerIndex& operator=(const ContainerIndex& in); - template - ContainerIndex(const std::shared_ptr& range); - - virtual IndexType type() const override; + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - virtual ContainerIndex& operator++() override; - virtual ContainerIndex& operator--() override; - virtual ContainerIndex& operator=(size_t pos) override; - - virtual int pp(std::shared_ptr& idxPtr) override; - virtual int mm(std::shared_ptr& idxPtr) override; + static IndexType S_type(ContainerIndex* i) { return IndexType::CONT; } - virtual MetaType meta() const override; - virtual ContainerIndex& at(const MetaType& metaPos) override; - - virtual bool first() const override; - virtual bool last() const override; + static ContainerIndex& S_pp_op(ContainerIndex* i) + { + if(i->mExternControl){ + i->mPos = PackNum::makePos(i->mIPack); + } + PackNum::pp( i->mIPack ); + ++i->mPos; + return *i; + } - virtual size_t dim() const override; + static ContainerIndex& S_mm_op(ContainerIndex* i) + { + if(i->mExternControl){ + i->mPos = PackNum::makePos(i->mIPack); + } + PackNum::mm( i->mIPack ); + --i->mPos; + return *i; - ContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true + } + + static ContainerIndex& S_ass_op(ContainerIndex* i, size_t pos) + { + i->mPos = pos; + PackNum::setIndexPack(i->mIPack, pos); + return *i; + } + + static int S_pp(ContainerIndex* i, intptr_t idxPtrNum) + { + int tmp = PackNum::pp(i->mIPack, i->mBlockSizes, idxPtrNum); + i->mPos += tmp; + return tmp; + } + + static int S_mm(ContainerIndex* i, intptr_t idxPtrNum) + { + int tmp = PackNum::mm(i->mIPack, i->mBlockSizes, idxPtrNum); + i->mPos -= tmp; + return tmp; + } + + static MetaType S_meta(ContainerIndex* i) + { + MetaType metaTuple; + PackNum::getMetaPos(metaTuple, i->mIPack); + return metaTuple; + } + + static ContainerIndex& S_at(ContainerIndex* i, const MetaType& metaPos) + { + PackNum::setMeta(i->mIPack, metaPos); + i->mPos = PackNum::makePos(i->mIPack); + return *i; + } + + static size_t S_dim(ContainerIndex* i) + { + return sizeof...(Indices); + } + + static bool S_first(ContainerIndex* i) + { + return i->pos() == 0; + } + + static bool S_last(ContainerIndex* i) + { + return i->pos() == i->mMax - 1; + } template - auto get() const -> decltype( *std::get( mIPack ) )&; + static auto S_getPtr(ContainerIndex* i) -> decltype( std::get( mIPack ) )& + { + return std::get( i->mIPack ); + } - template - auto getPtr() const -> decltype( std::get( mIPack ) )&; + static std::shared_ptr S_getVPtr(ContainerIndex* i, size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + ContainerIndex const* t = i; + return PackNum::getIndexPtr(*t, n); + } - virtual std::shared_ptr getPtr(size_t n) const override; - virtual size_t getStepSize(size_t n) const override; + static size_t S_getStepSize(ContainerIndex* i, size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return i->mBlockSizes[n+1]; + } - ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices - - ContainerIndex& operator()(); // -> sync; just to shorten the code - - std::shared_ptr range() const; - virtual std::string id() const override { return std::string("con") + std::to_string(IB::mId); } + static std::string S_id(ContainerIndex* i) { return std::string("con") + std::to_string(IB::mId); } }; @@ -131,7 +211,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 ContainerRangeFactory; @@ -157,7 +237,7 @@ namespace MultiArrayTools template template ContainerIndex::ContainerIndex(const std::shared_ptr& range) : - IndexInterface >(range, 0) + IndexInterface,std::tuple >(range, 0) { PackNum::construct(mIPack, *range); IB::mPos = PackNum::makePos(mIPack); @@ -165,81 +245,6 @@ namespace MultiArrayTools PackNum::initBlockSizes(mBlockSizes, mIPack); } - template - IndexType ContainerIndex::type() const - { - return IndexType::CONT; - } - - template - ContainerIndex& ContainerIndex::operator++() - { - if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); - } - PackNum::pp( mIPack ); - ++IB::mPos; - return *this; - } - - template - ContainerIndex& ContainerIndex::operator--() - { - if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); - } - PackNum::mm( mIPack ); - --IB::mPos; - return *this; - - } - - template - ContainerIndex& ContainerIndex::operator=(size_t pos) - { - IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); - return *this; - } - - template - int ContainerIndex::pp(std::shared_ptr& idxPtr) - { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); - IB::mPos += tmp; - return tmp; - } - - template - int ContainerIndex::mm(std::shared_ptr& idxPtr) - { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); - IB::mPos -= tmp; - return tmp; - } - - template - typename ContainerIndex::MetaType ContainerIndex::meta() const - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); - return metaTuple; - } - - template - ContainerIndex& ContainerIndex::at(const MetaType& metaPos) - { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - - template - size_t ContainerIndex::dim() const - { - return sizeof...(Indices); - } - template ContainerIndex& ContainerIndex::sync() { @@ -259,47 +264,6 @@ namespace MultiArrayTools return *std::get( mIPack ); } - template - template - auto ContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& - { - return std::get( mIPack ); - } - - template - std::shared_ptr ContainerIndex::getPtr(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - ContainerIndex const* t = this; - return PackNum::getIndexPtr(*t, n); - } - - - template - size_t ContainerIndex::getStepSize(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } - - template - bool ContainerIndex::first() const - { - return IB::pos() == 0; - } - - template - bool ContainerIndex::last() const - { - return IB::pos() == IB::mRangePtr->size() - 1; - } - template ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) { @@ -314,12 +278,6 @@ namespace MultiArrayTools return sync(); } - template - std::shared_ptr::RangeType> ContainerIndex::range() const - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } - /***************************** * ContainerRangeFactory * *****************************/ @@ -408,11 +366,12 @@ namespace MultiArrayTools } template - std::shared_ptr ContainerRange::index() const + std::shared_ptr ContainerRange::index() const { - return std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); + return std::make_shared + ( std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ) ); } } // end namespace MultiArrayTools diff --git a/src/index_base.h b/src/index_base.h index 7d15ae3..53ecdb0 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -35,12 +35,11 @@ namespace MultiArrayTools 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 std::shared_ptr getPtr(size_t n) const = 0; virtual intptr_t getPtrNum() const = 0; + virtual size_t getStepSize(size_t n) const = 0; }; - typedef VirtualIndexWrapperBase VIWB; - template std::shared_ptr > make_viwb(std::shared_ptr idxPtr) { @@ -68,7 +67,8 @@ namespace MultiArrayTools 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() ); }; + virtual intptr_t getPtrNum() const override { return static_cast( mIdxPtr.get() ); } + virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); } private: std::shared_ptr mIdxPtr; @@ -78,9 +78,12 @@ namespace MultiArrayTools class IndexInterface { public: + typedef typename I::RangeType RangeType; + //DEFAULT_MEMBERS(IndexInterface); I* THIS() { return static_cast(this); } + I const* THIS() const { return static_cast(this); } ~IndexInterface() = default; @@ -103,7 +106,8 @@ namespace MultiArrayTools bool last() const { return I::S_last(THIS()); } bool first() const { return I::S_first(THIS()); } - std::shared_ptr rangePtr() const; + std::shared_ptr vrange() const { return mRangePtr; } + std::shared_ptr range() const { return std::dynamic_pointer_cast(mRangePtr); } template auto getPtr() const -> decltype(I::S_get(THIS())) { return I::S_get(THIS()); } @@ -118,9 +122,12 @@ namespace MultiArrayTools MetaType meta() const { return I::S_meta(THIS()); } IndexInterface& at(const MetaType& meta) { return I::S_at(THIS(), meta); } - - protected: + + private: + + friend I; + IndexInterface() { mId = indexId(); } IndexInterface(IndexInterface&& in) = default; IndexInterface& operator=(IndexInterface&& in) = default; @@ -130,7 +137,7 @@ namespace MultiArrayTools std::shared_ptr mRangePtr; size_t mPos; size_t mId; - + size_t mMax; }; } @@ -141,14 +148,15 @@ namespace MultiArrayTools namespace MultiArrayTools { - /***************** + /********************** * IndexInterface * - *****************/ + **********************/ template IndexInterface::IndexInterface(const std::shared_ptr& range, size_t pos) : mRangePtr(range), - mPos(pos) + mPos(pos), + mMax(mRangePtr->size()) { mId = indexId(); } @@ -174,13 +182,7 @@ namespace MultiArrayTools template size_t IndexInterface::max() const { - return mRangePtr->size(); - } - - template - std::shared_ptr IndexInterface::rangePtr() const - { - return mRangePtr; + return mMax; } template diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 9daf13e..93122a1 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -56,36 +56,7 @@ namespace MultiArrayTools template std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, const OpClass& second); - /* - template - class OperationBase - { - public: - typedef T value_type; - OperationBase() = default; - virtual ~OperationBase() = default; - - // init block, return resulting type (BLOCK, VALUE, SPLIT) - virtual std::vector block(const std::shared_ptr blockIndex) const = 0; - virtual const OperationBase& block() const = 0; // update block - - //virtual size_t argNum() const = 0; - virtual const Block& get() const = 0; - }; - - template - class MutableOperationBase : public OperationBase - { - public: - typedef T value_type; - - MutableOperationBase() = default; - - virtual MBlock& get() = 0; - - }; - */ template class OperationTemplate { @@ -333,7 +304,7 @@ namespace MultiArrayTools return out; } - void minimizeAppearanceOfType(std::map, std::vector >& mp, + void minimizeAppearanceOfType(std::map, std::vector >& mp, BlockType bt) { size_t minNum = getBTNum( mp.begin()->second, bt ); @@ -357,12 +328,12 @@ namespace MultiArrayTools } template - std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OpClass& second) + std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, + const OpClass& second) { - std::vector > ivec; + std::vector > ivec; seekIndexInst(ownIdx, ivec); - std::map, std::vector > mp; + std::map, std::vector > mp; for(auto& xx: ivec){ mp[xx] = second.block(xx); @@ -465,7 +436,7 @@ namespace MultiArrayTools } template - std::vector OperationMaster::block(const std::shared_ptr blockIndex) const + std::vector OperationMaster::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); @@ -501,7 +472,7 @@ namespace MultiArrayTools } template - std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const + std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); @@ -551,7 +522,7 @@ namespace MultiArrayTools } template - std::vector OperationRoot::block(const std::shared_ptr blockIndex) const + std::vector OperationRoot::block(const std::shared_ptr blockIndex) const { std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); @@ -582,7 +553,7 @@ namespace MultiArrayTools } template - std::vector Operation::block(const std::shared_ptr blockIndex) const + std::vector Operation::block(const std::shared_ptr blockIndex) const { std::vector btv; PackNum::makeBlockTypeVec(btv, mOps, blockIndex); @@ -617,7 +588,7 @@ namespace MultiArrayTools } template - std::vector Contraction::block(const std::shared_ptr blockIndex) const + std::vector Contraction::block(const std::shared_ptr blockIndex) const { return mOp.block(blockIndex); } diff --git a/src/multi_range.h b/src/multi_range.h index ecfe670..6ecf09d 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -15,24 +15,32 @@ namespace MultiArrayTools { + namespace + { + using namespace MultiArrayHelper; + } template - class MultiIndex : public IndexInterface > + class MultiIndex : public IndexInterface, + std::tuple > { public: - typedef IndexBase IB; + typedef IndexInterface, + std::tuple > IB; typedef std::tuple...> IndexPack; typedef std::tuple MetaType; - typedef IndexInterface IndexI; typedef MultiRange RangeType; - protected: + private: + IndexPack mIPack; std::array mBlockSizes; - + public: + MultiIndex() = delete; + // NO DEFAULT HERE !!! // ( have to assign sub-indices (ptr!) correctly ) //MultiIndex(const MultiIndex& in); @@ -42,50 +50,119 @@ namespace MultiArrayTools template MultiIndex(const std::shared_ptr& range); - virtual IndexType type() const override; - - virtual MultiIndex& operator++() override; - virtual MultiIndex& operator--() override; - virtual MultiIndex& operator=(size_t pos) override; - - virtual int pp(std::shared_ptr& idxPtr) override; - virtual int mm(std::shared_ptr& idxPtr) override; - template MultiIndex& up(); template MultiIndex& down(); - + template auto get() const -> decltype( *std::get( mIPack ) )&; - template - auto getPtr() const -> decltype( std::get( mIPack ) )&; - - const IndexBase& get(size_t n) const; - virtual std::shared_ptr getPtr(size_t n) const override; - virtual size_t getStepSize(size_t n) const override; - - virtual MetaType meta() const override; - virtual MultiIndex& at(const MetaType& metaPos) override; - - virtual bool first() const override; - virtual bool last() const override; - - virtual size_t dim() const override; - - std::shared_ptr range() const; - - //virtual MultiIndex& lock(std::shared_ptr& idx) override; - // raplace instances (in contrast to its analogon in ContainerIndex // MultiIndices CANNOT be influences be its subindices, so there is // NO foreign/external controll) // Do NOT share index instances between two or more MultiIndex instances MultiIndex& operator()(std::shared_ptr&... indices); - virtual std::string id() const override { return std::string("mul") + std::to_string(IB::mId); } + private: + + friend IB; + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== + + static IndexType S_type(MultiIndex* i) { return IndexType::MULTI; } + + static MultiIndex& S_ass_op(MultiIndex* i, size_t pos) + { + i->mPos = pos; + PackNum::setIndexPack(i->mIPack, pos); + return *i; + } + + static MultiIndex& S_pp_op(MultiIndex* i) + { + PackNum::pp( i->mIPack ); + ++i->mPos; + return *i; + } + + static MultiIndex& S_mm_op(MultiIndex* i) + { + PackNum::mm( i->mIPack ); + --i->mPos; + return *i; + } + + static int S_pp(MultiIndex* i, intptr_t idxPtrNum) + { + int tmp = PackNum::pp(i->mIPack, i->mBlockSizes, idxPtrNum); + i->mPos += tmp; + return tmp; + } + + static int S_mm(MultiIndex* i, intptr_t idxPtrNum) + { + int tmp = PackNum::mm(i->mIPack, i->mBlockSizes, idxPtrNum); + i->mPos -= tmp; + return tmp; + } + + static MetaType S_meta(MultiIndex* i) + { + MetaType metaTuple; + PackNum::getMetaPos(metaTuple, i->mIPack); + return metaTuple; + } + + static MultiIndex& S_at(MultiIndex* i, const MetaType& metaPos) + { + PackNum::setMeta(i->mIPack, metaPos); + i->mPos = PackNum::makePos(i->mIPack); + return *i; + } + + static size_t S_dim(MultiIndex* i) + { + return sizeof...(Indices); + } + + static bool S_first(MultiIndex* i) + { + return i->mPos == 0; + } + + static bool S_last(MultiIndex* i) + { + return i->mPos == i->mMax - 1; + } + + template + static auto S_getPtr(MultiIndex* i) -> decltype( std::get( mIPack ) )& + { + return std::get(i->mIPack); + } + + //const IndexBase& get(size_t n); + static std::shared_ptr S_getVPtr(MultiIndex* i, size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + MultiIndex const* t = i; + return PackNum::getIndexPtr(*t, n); + } + + static size_t S_getStepSize(MultiIndex* i, size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return i->mBlockSizes[n+1]; + } + + static std::string S_id(MultiIndex* i) { return std::string("mul") + std::to_string(IB::mId); } }; /************************* @@ -146,7 +223,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 MultiRangeFactory; @@ -199,7 +276,7 @@ namespace MultiArrayTools template template MultiIndex::MultiIndex(const std::shared_ptr& range) : - IndexInterface >(range, 0) + IndexInterface,std::tuple >(range, 0) { PackNum::construct(mIPack, *range); IB::mPos = PackNum::makePos(mIPack); @@ -207,52 +284,6 @@ namespace MultiArrayTools PackNum::initBlockSizes(mBlockSizes, mIPack); // has one more element! } - template - IndexType MultiIndex::type() const - { - return IndexType::MULTI; - } - - template - MultiIndex& MultiIndex::operator++() - { - PackNum::pp( mIPack ); - ++IB::mPos; - return *this; - } - - template - MultiIndex& MultiIndex::operator--() - { - PackNum::mm( mIPack ); - --IB::mPos; - return *this; - } - - template - MultiIndex& MultiIndex::operator=(size_t pos) - { - IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); - return *this; - } - - template - int MultiIndex::pp(std::shared_ptr& idxPtr) - { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); - IB::mPos += tmp; - return tmp; - } - - template - int MultiIndex::mm(std::shared_ptr& idxPtr) - { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); - IB::mPos -= tmp; - return tmp; - } - template template MultiIndex& MultiIndex::up() @@ -273,12 +304,6 @@ namespace MultiArrayTools return *this; } - template - size_t MultiIndex::dim() const - { - return sizeof...(Indices); - } - template template auto MultiIndex::get() const -> decltype( *std::get( mIPack ) )& @@ -286,87 +311,6 @@ namespace MultiArrayTools return *std::get(mIPack); } - template - template - auto MultiIndex::getPtr() const -> decltype( std::get( mIPack ) )& - { - return std::get(mIPack); - } - - template - const IndexBase& MultiIndex::get(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - MultiIndex const* t = this; - return PackNum::getIndex(*t, n); - } - - template - std::shared_ptr MultiIndex::getPtr(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - MultiIndex const* t = this; - return PackNum::getIndexPtr(*t, n); - } - - template - size_t MultiIndex::getStepSize(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } - - template - typename MultiIndex::MetaType MultiIndex::meta() const - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); - return metaTuple; - } - - template - MultiIndex& MultiIndex::at(const MultiIndex::MetaType& metaPos) - { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - - template - bool MultiIndex::first() const - { - return IB::mPos == 0; - } - - template - bool MultiIndex::last() const - { - return IB::mPos == IB::mRangePtr->size() - 1; - } - - template - std::shared_ptr::RangeType> MultiIndex::range() const - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } - /* - template - MultiIndex& MultiIndex::lock(std::shared_ptr& idx) - { - IB::mLocked = (idx.get() == this); - PackNum::lock(mIPack, idx); - return *this; - }*/ - template MultiIndex& MultiIndex::operator()(std::shared_ptr&... indices) { @@ -467,11 +411,12 @@ namespace MultiArrayTools } template - std::shared_ptr MultiRange::index() const + std::shared_ptr MultiRange::index() const { - return std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); + return std::make_shared + ( std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ) ); } } diff --git a/src/pack_num.h b/src/pack_num.h index 8fb37a4..bf9065e 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -90,10 +90,10 @@ namespace MultiArrayHelper auto& siPtr = std::get(ip); //VCHECK(siPtr.id()); if(static_cast(siPtr.get()) == idxPtrNum){ - return PackNum::pp(ip, bs, idxPtrNum; + return PackNum::pp(ip, bs, idxPtrNum); } else { - int tmp = siPtr->pp(idxPtr); + int tmp = siPtr->pp(idxPtrNum); if(siPtr->pos() == siPtr->max()){ (*siPtr) = 0; return PackNum::pp(ip, bs, idxPtrNum) - siPtr->max() + 1; @@ -233,7 +233,7 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr idxPtr) + std::shared_ptr idxPtr) { auto subvec = std::move( std::get(ops).block(idxPtr) ); btv.insert(btv.end(), subvec.begin(), subvec.end() ); @@ -271,7 +271,7 @@ namespace MultiArrayHelper } */ template - static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) { return make_viwb( in.template get<0>() ); } @@ -423,7 +423,7 @@ namespace MultiArrayHelper template static void makeBlockTypeVec(std::vector >& btv, const std::tuple& ops, - std::shared_ptr idxPtr) + std::shared_ptr idxPtr) { auto subvec = std::move( std::get<0>(ops).block(idxPtr) ); btv.insert(btv.end(), subvec.begin(), subvec.end() ); diff --git a/src/single_range.h b/src/single_range.h index 90f0193..785427b 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -19,7 +19,7 @@ namespace MultiArrayTools { public: - typedef IndexBase,U> IB; + typedef IndexInterface,U> IB; typedef U MetaType; typedef SingleRange RangeType; @@ -27,30 +27,87 @@ namespace MultiArrayTools SingleIndex(const std::shared_ptr >& range); - static IndexType S_type(SingleIndex* i) const; - - static SingleIndex& S_ass_op(SingleIndex* i, size_t pos); - static SingleIndex& S_pp_op(SingleIndex* i); - static SingleIndex& S_mm_op(SingleIndex* i); + private: - static int S_pp(SingleIndex* i, std::shared_ptr& idxPtr); - static int S_mm(SingleIndex* i, std::shared_ptr& idxPtr); + friend IB; + + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - static U S_meta(SingleIndex* i) const; - static SingleIndex& S_at(SingleIndex* i, const U& metaPos); + static IndexType S_type(SingleIndex* i) + { + return IndexType::SINGLE; + } - static size_t S_dim(SingleIndex* i) const; // = 1 - static bool S_last(SingleIndex* i) const; - static bool S_first(SingleIndex* i) const; + static SingleIndex& S_ass_op(SingleIndex* i, size_t pos) + { + i->mPos = pos; + return *i; + } + + static SingleIndex& S_pp_op(SingleIndex* i) + { + ++i->mPos; + return *i; + } + + static SingleIndex& S_mm_op(SingleIndex* i) + { + --i->mPos; + return *i; + } + + static int S_pp(SingleIndex* i, intptr_t idxPtrNum) + { + ++(*i); + return 1; + } + + static int S_mm(SingleIndex* i, intptr_t idxPtrNum) + { + --(*i); + return 1; + } + + static U S_meta(SingleIndex* i) + { + return std::dynamic_pointer_cast const>( i->mRangePtr )->get( i->pos() ); + } + + static SingleIndex& S_at(SingleIndex* i, const U& metaPos) + { + operator=( std::dynamic_pointer_cast const>( i->mRangePtr )->getMeta( metaPos ) ); + return *i; + } + + static size_t S_dim(SingleIndex* i) // = 1 + { + return 1; + } + + static bool S_last(SingleIndex* i) + { + return i->mPos == i->mMax - 1; + } + + static bool S_first(SingleIndex* i) + { + return i->mPos == 0; + } template - static void S_getPtr(SingleIndex* i) const; + static void S_getPtr(SingleIndex* i) {} - std::shared_ptr getVPtr(size_t n) const; + static std::shared_ptr S_getVPtr(SingleIndex* i, size_t n) + { + return std::shared_ptr(); + } - static size_t S_getStepSize(SingleIndex* i, size_t n) const; + static size_t S_getStepSize(SingleIndex* i, size_t n) + { + return 1; + } - static std::string S_id(SingleIndex* i) const { return std::string("sin") + std::to_string(IB::mId); } + static std::string S_id(SingleIndex* i) { return std::string("sin") + std::to_string(IB::mId); } }; template @@ -109,98 +166,7 @@ namespace MultiArrayTools template SingleIndex::SingleIndex(const std::shared_ptr >& range) : - IndexInterface(range, 0) {} - - template - IndexType SingleIndex::S_type() const - { - return IndexType::SINGLE; - } - - template - SingleIndex& SingleIndex::S_ass_op(size_t pos) - { - IB::mPos = pos; - return *this; - } - - template - SingleIndex& SingleIndex::S_pp_op() - { - ++IB::mPos; - return *this; - } - - template - SingleIndex& SingleIndex::S_mm_op() - { - --IB::mPos; - return *this; - } - - template - int SingleIndex::S_pp(std::shared_ptr& idxPtr) - { - ++(*this); - return 1; - } - - template - int SingleIndex::S_mm(std::shared_ptr& idxPtr) - { - --(*this); - return 1; - } - - template - U SingleIndex::S_meta() const - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); - } - - template - SingleIndex& SingleIndex::S_at(const U& metaPos) - { - operator=( std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ) ); - return *this; - } - - template - size_t SingleIndex::S_dim() const - { - return 1; - } - - template - bool SingleIndex::S_last() const - { - return IB::mPos == IB::mRangePtr->size() - 1; - } - - template - bool SingleIndex::S_first() const - { - return IB::mPos == 0; - } - - template - template - void SingleIndex::S_getPtr() const - { - return; - } - - template - std::shared_ptr SingleIndex::getVPtr(size_t n) const - { - return std::shared_ptr(); - } - - template - size_t SingleIndex::S_getStepSize(size_t n) const - { - return 1; - } + IndexInterface,U>(range, 0) {} /******************** * SingleRange * @@ -282,8 +248,8 @@ namespace MultiArrayTools { return std::make_shared ( std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ) ); + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ) ); } }