diff --git a/src/base_def.h b/src/base_def.h index c84f2d5..327f4df 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -51,7 +51,7 @@ namespace MultiArrayTools ***********************/ // range_base.h - enum class RangeType; + enum class SpaceType; // range_base.h class RangeFactoryBase; @@ -76,15 +76,15 @@ namespace MultiArrayTools class IndexInterface; // single_range.h - template + template class SingleRange; // single_range.h - template + template class SingleRangeFactory; // single_range.h - template + template class SingleIndex; // multi_range.h diff --git a/src/index_unit_test.cc b/src/index_unit_test.cc index db3fb71..a4c325d 100644 --- a/src/index_unit_test.cc +++ b/src/index_unit_test.cc @@ -37,7 +37,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; typedef MultiRangeFactory M3RF; diff --git a/src/ma_unit_test.cc b/src/ma_unit_test.cc index 127b524..52cb8ae 100644 --- a/src/ma_unit_test.cc +++ b/src/ma_unit_test.cc @@ -38,7 +38,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; MATest_1Dim() @@ -58,7 +58,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; typedef MultiRangeFactory MRF; diff --git a/src/multi_range.h b/src/multi_range.h index eb537ee..34b5afb 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -68,119 +68,34 @@ namespace MultiArrayTools // Do NOT share index instances between two or more MultiIndex instances MultiIndex& operator()(std::shared_ptr&... indices); - std::shared_ptr range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - IndexType type() { return IndexType::MULTI; } + IndexType type(); - MultiIndex& operator=(size_t pos) - { - IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); - return *this; - } + MultiIndex& operator=(size_t pos); - MultiIndex& operator++() - { - PackNum::pp( mIPack ); - ++IB::mPos; - return *this; - } + MultiIndex& operator++(); + MultiIndex& operator--(); - MultiIndex& operator--() - { - PackNum::mm( mIPack ); - --IB::mPos; - return *this; - } + int pp(std::intptr_t idxPtrNum); + int mm(std::intptr_t idxPtrNum); - int pp(std::intptr_t idxPtrNum) - { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); - IB::mPos += tmp; - return tmp; - } - - int mm(std::intptr_t idxPtrNum) - { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); - IB::mPos -= tmp; - return tmp; - } - - MetaType meta() - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); - return metaTuple; - } - - MultiIndex& at(const MetaType& metaPos) - { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } + MetaType meta(); + MultiIndex& at(const MetaType& metaPos); - size_t dim() - { - return sizeof...(Indices); - } - - bool first() - { - return IB::mPos == 0; - } - - bool last() - { - return IB::mPos == IB::mMax - 1; - } - - std::shared_ptr range() - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } + size_t dim(); + bool first(); + bool last(); + std::shared_ptr range(); template - auto getPtr() -> decltype( std::get( mIPack ) )& - { - return std::get(mIPack); - } + auto getPtr() -> decltype( std::get( mIPack ) )&; - //const IndexBase& get(size_t n); - std::shared_ptr getVPtr(size_t n) - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - MultiIndex const* t = this; - return PackNum::getIndexPtr(*t, n); - } - - size_t getStepSize(size_t n) - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } + std::shared_ptr getVPtr(size_t n); + size_t getStepSize(size_t n); - std::string id() { return std::string("mul") + std::to_string(IB::mId); } - - 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 << id() << "[" << reinterpret_cast(this) - << "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl; - PackNum::printIndex(mIPack, offset+1); - } + std::string id(); + void print(size_t offset); }; /************************* @@ -345,6 +260,140 @@ namespace MultiArrayTools PackNum::setIndexPack(mIPack, IB::mPos); return *this; } + + template + IndexType MultiIndex::type() + { + return IndexType::MULTI; + } + + template + MultiIndex& MultiIndex::operator=(size_t pos) + { + IB::mPos = pos; + PackNum::setIndexPack(mIPack, pos); + return *this; + } + + template + MultiIndex& MultiIndex::operator++() + { + PackNum::pp( mIPack ); + ++IB::mPos; + return *this; + } + + template + MultiIndex& MultiIndex::operator--() + { + PackNum::mm( mIPack ); + --IB::mPos; + return *this; + } + + template + int MultiIndex::pp(std::intptr_t idxPtrNum) + { + int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); + IB::mPos += tmp; + return tmp; + } + + template + int MultiIndex::mm(std::intptr_t idxPtrNum) + { + int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); + IB::mPos -= tmp; + return tmp; + } + + template + typename MultiIndex::MetaType MultiIndex::meta() + { + MetaType metaTuple; + PackNum::getMetaPos(metaTuple, mIPack); + return metaTuple; + } + + template + MultiIndex& MultiIndex::at(const MetaType& metaPos) + { + PackNum::setMeta(mIPack, metaPos); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } + + template + size_t MultiIndex::dim() + { + return sizeof...(Indices); + } + + template + bool MultiIndex::first() + { + return IB::mPos == 0; + } + + template + bool MultiIndex::last() + { + return IB::mPos == IB::mMax - 1; + } + + template + std::shared_ptr::RangeType> + MultiIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + template + auto MultiIndex::getPtr() -> decltype( std::get( mIPack ) )& + { + return std::get(mIPack); + } + + template + std::shared_ptr MultiIndex::getVPtr(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + MultiIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } + + template + size_t MultiIndex::getStepSize(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + std::string MultiIndex::id() + { + return std::string("mul") + std::to_string(IB::mId); + } + + template + void MultiIndex::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); + } + /************************* * MultiRangeFactory * diff --git a/src/op_unit_test.cc b/src/op_unit_test.cc index 67de8db..83b6e04 100644 --- a/src/op_unit_test.cc +++ b/src/op_unit_test.cc @@ -67,7 +67,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; OpTest_1Dim() @@ -86,7 +86,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; typedef MultiRangeFactory MRF; @@ -130,7 +130,7 @@ namespace { { protected: - typedef SingleRangeFactory SRF; + typedef SingleRangeFactory SRF; typedef SRF::oType SRange; typedef MultiRangeFactory MRF; diff --git a/src/range_base.h b/src/range_base.h index 2c564a6..364799d 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -12,7 +12,7 @@ namespace MultiArrayTools { - enum class RangeType + enum class SpaceType { NONE = 0, ANY = 1, diff --git a/src/range_types/spin_range.h b/src/range_types/spin_range.h index 73c990d..54ae06d 100644 --- a/src/range_types/spin_range.h +++ b/src/range_types/spin_range.h @@ -12,14 +12,14 @@ include_range_type(SPIN,2) namespace MultiArrayTools { - typedef SingleIndex SpinIndex; + typedef SingleIndex SpinIndex; template <> - class SingleRangeFactory : public RangeFactoryBase + class SingleRangeFactory : public RangeFactoryBase { public: - typedef SingleRange oType; + typedef SingleRange oType; SingleRangeFactory(); std::shared_ptr create(); @@ -27,11 +27,11 @@ namespace MultiArrayTools }; template <> - class SingleRange : public RangeInterface + class SingleRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; + typedef typename RangeInterface >::IndexType IndexType; virtual size_t size() const override; virtual size_t dim() const override; @@ -43,13 +43,13 @@ namespace MultiArrayTools virtual IndexType end() const override; virtual std::shared_ptr index() const override; - friend SingleRangeFactory; + friend SingleRangeFactory; static const bool defaultable = true; static const size_t mSpinNum = 4; - static SingleRangeFactory factory() - { return SingleRangeFactory(); } + static SingleRangeFactory factory() + { return SingleRangeFactory(); } protected: @@ -59,8 +59,8 @@ namespace MultiArrayTools //SingleRange(size_t spinNum); }; - typedef SingleRange SpinRange; - typedef SingleRangeFactory SpinRF; + typedef SingleRange SpinRange; + typedef SingleRangeFactory SpinRF; } /* ========================= * @@ -73,16 +73,16 @@ namespace MultiArrayTools * SingleRange * ********************/ - SingleRangeFactory::SingleRangeFactory() + SingleRangeFactory::SingleRangeFactory() { // Quasi Singleton if(not mProd){ - mProd = std::shared_ptr( new SingleRange() ); + mProd = std::shared_ptr( new SingleRange() ); setSelf(); } } - std::shared_ptr SingleRangeFactory::create() + std::shared_ptr SingleRangeFactory::create() { return mProd; } @@ -91,49 +91,49 @@ namespace MultiArrayTools * SingleRange * ********************/ - size_t SingleRange::get(size_t pos) const + size_t SingleRange::get(size_t pos) const { return pos; } - size_t SingleRange::getMeta(size_t metaPos) const + size_t SingleRange::getMeta(size_t metaPos) const { return metaPos; } - size_t SingleRange::size() const + size_t SingleRange::size() const { return mSpinNum; } - size_t SingleRange::dim() const + size_t SingleRange::dim() const { return 1; } - typename SingleRange::IndexType SingleRange::begin() const + typename SingleRange::IndexType SingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast > + SingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - typename SingleRange::IndexType SingleRange::end() const + typename SingleRange::IndexType SingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast > + SingleIndex i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; } // put this in the interface class !!! - std::shared_ptr SingleRange::index() const + std::shared_ptr SingleRange::index() const { typedef IndexWrapper IW; return std::make_shared ( std::make_shared - ( std::dynamic_pointer_cast > + ( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ) ); } diff --git a/src/single_range.h b/src/single_range.h index 38e961a..fd3e67d 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -14,7 +14,7 @@ namespace MultiArrayTools { - template + template class SingleIndex : public IndexInterface,U> { public: @@ -27,103 +27,37 @@ namespace MultiArrayTools SingleIndex(const std::shared_ptr >& range); - std::shared_ptr range() const { return std::dynamic_pointer_cast( IB::mRangePtr ); } - // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== - IndexType type() - { - return IndexType::SINGLE; - } + IndexType type(); - SingleIndex& operator=(size_t pos) - { - IB::mPos = pos; - return *this; - } + SingleIndex& operator=(size_t pos); + SingleIndex& operator++(); + SingleIndex& operator--(); - SingleIndex& operator++() - { - ++IB::mPos; - return *this; - } - - SingleIndex& operator--() - { - --IB::mPos; - return *this; - } - - int pp(std::intptr_t idxPtrNum) - { - ++(*this); - return 1; - } - - int mm(std::intptr_t idxPtrNum) - { - --(*this); - return 1; - } + int pp(std::intptr_t idxPtrNum); + int mm(std::intptr_t idxPtrNum); - U meta() - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); - } - - SingleIndex& at(const U& metaPos) - { - (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); - return *this; - } + U meta(); + SingleIndex& at(const U& metaPos); - size_t dim() // = 1 - { - return 1; - } + size_t dim(); // = 1 + bool last(); + bool first(); - bool last() - { - return IB::mPos == IB::mMax - 1; - } - - bool first() - { - return IB::mPos == 0; - } - - std::shared_ptr range() - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } + std::shared_ptr range(); template - void getPtr() {} + void getPtr(); - std::shared_ptr getVPtr(size_t n) - { - return std::shared_ptr(); - } + std::shared_ptr getVPtr(size_t n); + size_t getStepSize(size_t n); - size_t getStepSize(size_t n) - { - return 1; - } - - std::string id() { return std::string("sin") + std::to_string(IB::mId); } - - 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 << id() << "[" << reinterpret_cast(this) - << "](" << IB::mRangePtr << "): " << meta() << std::endl; - } + std::string id(); + void print(size_t offset); }; - template + template class SingleRangeFactory : public RangeFactoryBase { public: @@ -136,7 +70,7 @@ namespace MultiArrayTools }; - template + template class SingleRange : public RangeInterface > { public: @@ -180,21 +114,133 @@ namespace MultiArrayTools * SingleIndex * ******************/ - template + template SingleIndex::SingleIndex(const std::shared_ptr >& range) : IndexInterface,U>(range, 0) {} + + template + IndexType SingleIndex::type() + { + return IndexType::SINGLE; + } + + template + SingleIndex& SingleIndex::operator=(size_t pos) + { + IB::mPos = pos; + return *this; + } + + template + SingleIndex& SingleIndex::operator++() + { + ++IB::mPos; + return *this; + } + + template + SingleIndex& SingleIndex::operator--() + { + --IB::mPos; + return *this; + } + + template + int SingleIndex::pp(std::intptr_t idxPtrNum) + { + ++(*this); + return 1; + } + + template + int SingleIndex::mm(std::intptr_t idxPtrNum) + { + --(*this); + return 1; + } + + template + U SingleIndex::meta() + { + return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); + } + + template + SingleIndex& SingleIndex::at(const U& metaPos) + { + (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); + return *this; + } + + template + size_t SingleIndex::dim() // = 1 + { + return 1; + } + + template + bool SingleIndex::last() + { + return IB::mPos == IB::mMax - 1; + } + + template + bool SingleIndex::first() + { + return IB::mPos == 0; + } + + template + std::shared_ptr::RangeType> SingleIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + template + void SingleIndex::getPtr() {} + + template + std::shared_ptr SingleIndex::getVPtr(size_t n) + { + return std::shared_ptr(); + } + + template + size_t SingleIndex::getStepSize(size_t n) + { + return 1; + } + + template + std::string SingleIndex::id() + { + return std::string("sin") + std::to_string(IB::mId); + } + + template + void SingleIndex::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; + } + /******************** * SingleRange * ********************/ - template + template SingleRangeFactory::SingleRangeFactory(const std::vector& space) { mProd = std::shared_ptr( new SingleRange( space ) ); } - template + template std::shared_ptr SingleRangeFactory::create() { setSelf(); @@ -205,17 +251,17 @@ namespace MultiArrayTools * SingleRange * ********************/ - template + template SingleRange::SingleRange(const std::vector& space) : RangeInterface >(), mSpace(space) {} - template + template const U& SingleRange::get(size_t pos) const { return mSpace[pos]; } - template + template size_t SingleRange::getMeta(const U& metaPos) const { size_t cnt = 0; @@ -228,19 +274,19 @@ namespace MultiArrayTools return cnt; } - template + template size_t SingleRange::size() const { return mSpace.size(); } - template + template size_t SingleRange::dim() const { return 1; } - template + template typename SingleRange::IndexType SingleRange::begin() const { SingleIndex i( std::dynamic_pointer_cast > @@ -249,7 +295,7 @@ namespace MultiArrayTools return i; } - template + template typename SingleRange::IndexType SingleRange::end() const { SingleIndex i( std::dynamic_pointer_cast > @@ -259,7 +305,7 @@ namespace MultiArrayTools } // put this in the interface class !!! - template + template std::shared_ptr SingleRange::index() const { typedef IndexWrapper IW;