diff --git a/src/container_range.h b/src/container_range.h index 550e698..bdbfcd5 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -30,19 +30,16 @@ namespace MultiArrayTools private: - friend IB; - bool mExternControl = false; IndexPack mIPack; std::array mBlockSizes; public: + ContainerIndex() = delete; template ContainerIndex(const std::shared_ptr& range); - - ContainerIndex& operator=(size_t pos) { IB::operator=(pos) ; return *this; } template auto get() const -> decltype( *std::get( mIPack ) )&; @@ -54,130 +51,36 @@ namespace MultiArrayTools ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices ContainerIndex& operator()(); // -> sync; just to shorten the code - std::shared_ptr range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - - private: - - //ContainerIndex(const ContainerIndex& in); - //ContainerIndex& operator=(const ContainerIndex& in); - // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - static IndexType S_type(ContainerIndex const* i) { return IndexType::CONT; } + IndexType type(); - static ContainerIndex& S_pp_op(ContainerIndex* i) - { - if(i->mExternControl){ - i->mPos = PackNum::makePos(i->mIPack); - } - PackNum::pp( i->mIPack ); - ++i->mPos; - return *i; - } + ContainerIndex& operator++(); + ContainerIndex& operator--(); - 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& operator=(size_t pos); - } - - 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, std::intptr_t idxPtrNum) - { - int tmp = PackNum::pp(i->mIPack, i->mBlockSizes, idxPtrNum); - i->mPos += tmp; - return tmp; - } - - static int S_mm(ContainerIndex* i, std::intptr_t idxPtrNum) - { - int tmp = PackNum::mm(i->mIPack, i->mBlockSizes, idxPtrNum); - i->mPos -= tmp; - return tmp; - } + int pp(std::intptr_t idxPtrNum); + int mm(std::intptr_t idxPtrNum); - static MetaType S_meta(ContainerIndex const* i) - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, i->mIPack); - return metaTuple; - } + MetaType meta(); + ContainerIndex& at(const MetaType& metaPos); - static ContainerIndex& S_at(ContainerIndex* i, const MetaType& metaPos) - { - PackNum::setMeta(i->mIPack, metaPos); - i->mPos = PackNum::makePos(i->mIPack); - return *i; - } + size_t dim(); + bool first(); + bool last(); - static size_t S_dim(ContainerIndex const* i) - { - return sizeof...(Indices); - } - - static bool S_first(ContainerIndex const* i) - { - return i->pos() == 0; - } - - static bool S_last(ContainerIndex const* i) - { - return i->pos() == i->mMax - 1; - } - - static std::shared_ptr S_range(ContainerIndex const* i) - { - return std::dynamic_pointer_cast( i->mRangePtr ); - } + std::shared_ptr range(); template - static auto S_getPtr(ContainerIndex const* i) -> decltype( std::get( mIPack ) )& - { - return std::get( i->mIPack ); - } - - static std::shared_ptr S_getVPtr(ContainerIndex const* i, size_t n) - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - ContainerIndex const* t = i; - return PackNum::getIndexPtr(*t, n); - } + auto getPtr() -> decltype( std::get( mIPack ) )&; - static size_t S_getStepSize(ContainerIndex const* i, size_t n) - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return i->mBlockSizes[n+1]; - } + std::shared_ptr getVPtr(size_t n); + size_t getStepSize(size_t n); - static std::string S_id(ContainerIndex const* i) { return std::string("con") + std::to_string(i->mId); } + std::string id(); + void print(size_t offset); - static void S_print(ContainerIndex const* i, size_t offset) - { - if(offset == 0){ - std::cout << " === " << std::endl; - } - for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } - std::cout << S_id(i) << "[" << reinterpret_cast(i) << "]" - << "(" << i->mRangePtr << "): " << S_meta(i) << std::endl; - PackNum::printIndex(i->mIPack, offset+1); - } }; @@ -310,6 +213,144 @@ namespace MultiArrayTools return sync(); } + template + IndexType ContainerIndex::type() { 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::intptr_t idxPtrNum) + { + int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); + IB::mPos += tmp; + return tmp; + } + + template + int ContainerIndex::mm(std::intptr_t idxPtrNum) + { + int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); + IB::mPos -= tmp; + return tmp; + } + + template + typename ContainerIndex::MetaType ContainerIndex::meta() + { + 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() + { + return sizeof...(Indices); + } + + template + bool ContainerIndex::first() + { + return IB::pos() == 0; + } + + template + bool ContainerIndex::last() + { + return IB::pos() == IB::mMax - 1; + } + + template + std::shared_ptr::RangeType> + ContainerIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + template + auto ContainerIndex::getPtr() -> decltype( std::get( mIPack ) )& + { + return std::get( mIPack ); + } + + template + std::shared_ptr ContainerIndex::getVPtr(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + ContainerIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } + + template + size_t ContainerIndex::getStepSize(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + std::string ContainerIndex::id() + { + return std::string("con") + std::to_string(IB::mId); + } + + template + void ContainerIndex::print(size_t offset) + { + if(offset == 0){ + std::cout << " === " << std::endl; + } + for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } + std::cout << id() << "[" << reinterpret_cast(this) << "]" + << "(" << IB::mRangePtr << "): " << meta() << std::endl; + PackNum::printIndex(mIPack, offset+1); + } + + /***************************** * ContainerRangeFactory * *****************************/ diff --git a/src/index_base.h b/src/index_base.h index 1046185..b3c66b4 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -85,29 +85,29 @@ namespace MultiArrayTools //DEFAULT_MEMBERS(IndexInterface); - I* THIS() { return static_cast(this); } - I const* THIS() const { return static_cast(this); } + I& THIS() { return static_cast(*this); } + I const& THIS() const { return static_cast(*this); } ~IndexInterface() = default; - IndexType type() const { return I::S_type(THIS()); } + IndexType type() const { return THIS().type(); } - I& operator=(size_t pos) { return I::S_ass_op(THIS(), pos); } - I& operator++() { return I::S_pp_op(THIS()); } - I& operator--() { return I::S_mm_op(THIS()); } + I& operator=(size_t pos) { return THIS() = pos; } + I& operator++() { return THIS()++; } + I& operator--() { return THIS()--;} - int pp(std::intptr_t idxPtrNum) { return I::S_pp(THIS(), idxPtrNum); } - int mm(std::intptr_t idxPtrNum) { return I::S_mm(THIS(), idxPtrNum); } + int pp(std::intptr_t idxPtrNum) { return THIS().pp(idxPtrNum); } + int mm(std::intptr_t idxPtrNum) { return THIS().mm(idxPtrNum); } bool operator==(const IndexInterface& in) const; bool operator!=(const IndexInterface& in) const; - size_t dim() const { return I::S_dim(THIS()); } + size_t dim() const { return THIS().dim(); } size_t pos() const; size_t max() const; - bool last() const { return I::S_last(THIS()); } - bool first() const { return I::S_first(THIS()); } + bool last() const { return THIS().last(); } + bool first() const { return THIS().first(); } std::shared_ptr vrange() const { return mRangePtr; } @@ -117,18 +117,18 @@ namespace MultiArrayTools auto getPtr() const -> decltype(I::template S_get(THIS())) { return I::template S_get(THIS()); } */ - std::shared_ptr getVPtr(size_t n) const { return I::S_getVPtr(THIS(),n); } + std::shared_ptr getVPtr(size_t n) const { return THIS().getVPtr(n); } - size_t getStepSize(size_t n) const { return I::S_getStepSize(THIS(),n); } + size_t getStepSize(size_t n) const { return THIS().getStepSize(n); } operator size_t() const; - std::string id() const { return I::S_id(THIS()); } + std::string id() const { return THIS().id(); } - MetaType meta() const { return I::S_meta(THIS()); } - I& at(const MetaType& meta) { return I::S_at(THIS(), meta); } + MetaType meta() const { return THIS().meta(); } + I& at(const MetaType& meta) { return THIS().at(meta); } - void print(size_t offset = 0) const { I::S_print(THIS(), offset); } + void print(size_t offset = 0) const { THIS().print(offset); } private: diff --git a/src/multi_range.h b/src/multi_range.h index 85056ef..eb537ee 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -40,8 +40,6 @@ namespace MultiArrayTools public: MultiIndex() = delete; - - MultiIndex& operator=(size_t pos) { IB::operator=(pos) ; return *this; } // NO DEFAULT HERE !!! // ( have to assign sub-indices (ptr!) correctly ) @@ -71,120 +69,117 @@ namespace MultiArrayTools MultiIndex& operator()(std::shared_ptr&... indices); std::shared_ptr range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - - private: - friend IB; // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - static IndexType S_type(MultiIndex const* i) { return IndexType::MULTI; } + IndexType type() { return IndexType::MULTI; } - static MultiIndex& S_ass_op(MultiIndex* i, size_t pos) + MultiIndex& operator=(size_t pos) { - i->mPos = pos; - PackNum::setIndexPack(i->mIPack, pos); - return *i; + IB::mPos = pos; + PackNum::setIndexPack(mIPack, pos); + return *this; } - static MultiIndex& S_pp_op(MultiIndex* i) + MultiIndex& operator++() { - PackNum::pp( i->mIPack ); - ++i->mPos; - return *i; + PackNum::pp( mIPack ); + ++IB::mPos; + return *this; } - static MultiIndex& S_mm_op(MultiIndex* i) + MultiIndex& operator--() { - PackNum::mm( i->mIPack ); - --i->mPos; - return *i; + PackNum::mm( mIPack ); + --IB::mPos; + return *this; } - static int S_pp(MultiIndex* i, std::intptr_t idxPtrNum) + int pp(std::intptr_t idxPtrNum) { - int tmp = PackNum::pp(i->mIPack, i->mBlockSizes, idxPtrNum); - i->mPos += tmp; + int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); + IB::mPos += tmp; return tmp; } - static int S_mm(MultiIndex* i, std::intptr_t idxPtrNum) + int mm(std::intptr_t idxPtrNum) { - int tmp = PackNum::mm(i->mIPack, i->mBlockSizes, idxPtrNum); - i->mPos -= tmp; + int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); + IB::mPos -= tmp; return tmp; } - static MetaType S_meta(MultiIndex const* i) + MetaType meta() { MetaType metaTuple; - PackNum::getMetaPos(metaTuple, i->mIPack); + PackNum::getMetaPos(metaTuple, mIPack); return metaTuple; } - static MultiIndex& S_at(MultiIndex* i, const MetaType& metaPos) + MultiIndex& at(const MetaType& metaPos) { - PackNum::setMeta(i->mIPack, metaPos); - i->mPos = PackNum::makePos(i->mIPack); - return *i; + PackNum::setMeta(mIPack, metaPos); + IB::mPos = PackNum::makePos(mIPack); + return *this; } - static size_t S_dim(MultiIndex const* i) + size_t dim() { return sizeof...(Indices); } - static bool S_first(MultiIndex const* i) + bool first() { - return i->mPos == 0; + return IB::mPos == 0; } - static bool S_last(MultiIndex const* i) + bool last() { - return i->mPos == i->mMax - 1; + return IB::mPos == IB::mMax - 1; } - static std::shared_ptr S_range(MultiIndex const* i) + std::shared_ptr range() { - return std::dynamic_pointer_cast( i->mRangePtr ); + return std::dynamic_pointer_cast( IB::mRangePtr ); } template - static auto S_getPtr(MultiIndex const* i) -> decltype( std::get( mIPack ) )& + auto getPtr() -> decltype( std::get( mIPack ) )& { - return std::get(i->mIPack); + return std::get(mIPack); } //const IndexBase& get(size_t n); - static std::shared_ptr S_getVPtr(MultiIndex const* i, size_t n) + std::shared_ptr getVPtr(size_t n) { if(n >= sizeof...(Indices)){ assert(0); // throw !! } - MultiIndex const* t = i; + MultiIndex const* t = this; return PackNum::getIndexPtr(*t, n); } - static size_t S_getStepSize(MultiIndex const* i, size_t n) + size_t getStepSize(size_t n) { if(n >= sizeof...(Indices)){ assert(0); // throw !! } - return i->mBlockSizes[n+1]; + return mBlockSizes[n+1]; } - static std::string S_id(MultiIndex const* i) { return std::string("mul") + std::to_string(i->mId); } + std::string id() { return std::string("mul") + std::to_string(IB::mId); } - static void S_print(MultiIndex const* i, size_t offset) + void print(size_t offset) { if(offset == 0){ std::cout << " === " << std::endl; } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } - std::cout << S_id(i) << "[" << reinterpret_cast(i) - << "]" << "(" << i->mRangePtr << "): " << S_meta(i) << std::endl; - PackNum::printIndex(i->mIPack, offset+1); + std::cout << id() << "[" << reinterpret_cast(this) + << "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl; + PackNum::printIndex(mIPack, offset+1); } }; diff --git a/src/single_range.h b/src/single_range.h index 10e056c..38e961a 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -27,105 +27,99 @@ namespace MultiArrayTools SingleIndex(const std::shared_ptr >& range); - SingleIndex& operator=(size_t pos) { IB::operator=(pos); return *this; } - std::shared_ptr range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - - private: - - friend IB; // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - static IndexType S_type(SingleIndex const* i) + IndexType type() { return IndexType::SINGLE; } - static SingleIndex& S_ass_op(SingleIndex* i, size_t pos) + SingleIndex& operator=(size_t pos) { - i->mPos = pos; - return *i; + IB::mPos = pos; + return *this; } - static SingleIndex& S_pp_op(SingleIndex* i) + SingleIndex& operator++() { - ++i->mPos; - return *i; + ++IB::mPos; + return *this; } - static SingleIndex& S_mm_op(SingleIndex* i) + SingleIndex& operator--() { - --i->mPos; - return *i; + --IB::mPos; + return *this; } - static int S_pp(SingleIndex* i, std::intptr_t idxPtrNum) + int pp(std::intptr_t idxPtrNum) { - ++(*i); + ++(*this); return 1; } - static int S_mm(SingleIndex* i, std::intptr_t idxPtrNum) + int mm(std::intptr_t idxPtrNum) { - --(*i); + --(*this); return 1; } - static U S_meta(SingleIndex const* i) + U meta() { - return std::dynamic_pointer_cast const>( i->mRangePtr )->get( i->pos() ); + return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); } - static SingleIndex& S_at(SingleIndex* i, const U& metaPos) + SingleIndex& at(const U& metaPos) { - (*i) = std::dynamic_pointer_cast const>( i->mRangePtr )->getMeta( metaPos ); - return *i; + (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); + return *this; } - static size_t S_dim(SingleIndex const* i) // = 1 + size_t dim() // = 1 { return 1; } - static bool S_last(SingleIndex const* i) + bool last() { - return i->mPos == i->mMax - 1; + return IB::mPos == IB::mMax - 1; } - static bool S_first(SingleIndex const* i) + bool first() { - return i->mPos == 0; + return IB::mPos == 0; } - static std::shared_ptr S_range(SingleIndex const* i) + std::shared_ptr range() { - return std::dynamic_pointer_cast( i->mRangePtr ); + return std::dynamic_pointer_cast( IB::mRangePtr ); } template - static void S_getPtr(SingleIndex* i) {} + void getPtr() {} - static std::shared_ptr S_getVPtr(SingleIndex const* i, size_t n) + std::shared_ptr getVPtr(size_t n) { return std::shared_ptr(); } - static size_t S_getStepSize(SingleIndex const* i, size_t n) + size_t getStepSize(size_t n) { return 1; } - static std::string S_id(SingleIndex const* i) { return std::string("sin") + std::to_string(i->mId); } + std::string id() { return std::string("sin") + std::to_string(IB::mId); } - static void S_print(SingleIndex const* i, size_t offset) + void print(size_t offset) { if(offset == 0){ std::cout << " === " << std::endl; } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } - std::cout << S_id(i) << "[" << reinterpret_cast(i) - << "](" << i->mRangePtr << "): " << S_meta(i) << std::endl; + std::cout << id() << "[" << reinterpret_cast(this) + << "](" << IB::mRangePtr << "): " << meta() << std::endl; } };