From 01703dec28c37957cc8bf0398e0c4c59ec3df677 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 28 May 2021 21:20:07 +0200 Subject: [PATCH] container_index: split header -> .h .cc.h --- src/include/container_index.cc.h | 447 ++++++++++++++++++++++++++ src/include/container_index.h | 474 +--------------------------- src/include/multi_array_header.cc.h | 1 + 3 files changed, 450 insertions(+), 472 deletions(-) create mode 100644 src/include/container_index.cc.h diff --git a/src/include/container_index.cc.h b/src/include/container_index.cc.h new file mode 100644 index 0000000..6c0f482 --- /dev/null +++ b/src/include/container_index.cc.h @@ -0,0 +1,447 @@ + +#include "container_index.h" + +namespace MultiArrayTools +{ + + template + ConstContainerIndex::ConstContainerIndex(const ConstContainerIndex& in, bool copy) : + IB(in), + mNonTrivialBlocks(in.mNonTrivialBlocks), + mExternControl(false), + mBlockSizes(in.mBlockSizes), + mData(in.mData), + mCPos(in.mCPos), + mObjPtrNum(in.mObjPtrNum) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) + { + typedef typename std::remove_reference(mIPack))>::type + SubType; + std::get(mIPack) = std::make_shared( in.template get() ) ; + return true; + }); + } + + template + ConstContainerIndex& ConstContainerIndex::copy(const ConstContainerIndex& in) + { + IB::operator=(in); + mNonTrivialBlocks = in.mNonTrivialBlocks; + mExternControl = false; + mBlockSizes = in.mBlockSizes; + mData = in.mData; + mCPos = in.mCPos; + mObjPtrNum = in.mObjPtrNum; + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) + { + typedef typename std::remove_reference(mIPack))>::type + SubType; + std::get(mIPack) = std::make_shared( in.template get() ) ; + return true; + }); + return *this; + } + + template + template + ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum) : + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) + { + std::get(mBlockSizes) = 1; + sfor_mn + ( [&](auto i) { + auto r = range->template getPtr(); + std::get(mIPack) = r->beginPtr(); + *std::get(mIPack) = 0; + + std::get(mBlockSizes) = sfor_p + ( [&](auto j) { return std::get(mIPack)->max(); } , + [&](auto a, auto b) { return a * b; }); + return 0; + }); + + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + } + + template + template + ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, + const std::array& blockSizes) : + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) + { + sfor_mn + ( [&](auto i) { + auto r = range->template getPtr(); + std::get(mIPack) = r->beginPtr(); + *std::get(mIPack) = 0; + return 0; + }); + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + mNonTrivialBlocks = true; + } + + template + template + ConstContainerIndex& + ConstContainerIndex::operator=(const ConstContainerIndex& in) + { + mIPack = in.mIPack; + return (*this)(); + } + + + template + ConstContainerIndex& ConstContainerIndex::sync() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + } + return *this; + } + + template + template + auto ConstContainerIndex::get() const -> decltype( *std::get( mIPack ) )& + { + return *std::get( mIPack ); + } + + template + template + auto ConstContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& + { + return std::get( mIPack ); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()(const std::shared_ptr&... inds) + { + return (*this)(std::make_tuple(inds...)); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()(const std::tuple...>& inds) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(mIPack) = std::get(inds); return 0; } ); + mExternControl = true; + return sync(); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()() + { + return sync(); + } + + template + IndexType ConstContainerIndex::type() const { return IndexType::CONT; } + + template + ConstContainerIndex& ConstContainerIndex::operator++() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + } + sfor_m + ( [&](auto i) { + auto& si = *std::get( mIPack ); + if(si.last() and i != 0) { si = 0; return true; } + else { ++si; return false; } + return false; + } ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + ++IB::mPos; + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::operator--() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + } + sfor_m + ( [&](auto i) { + auto& si = *std::get( mIPack ); + if(si.first() and i != 0) { si = si.max()-1; return true; } + else { --si; return false; } + return false; + } ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + --IB::mPos; + return *this; + + } + + template + ConstContainerIndex& ConstContainerIndex::operator=(size_t pos) + { + IB::mPos = pos; + RangeHelper::setIndexPack(mIPack, pos); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + return *this; + } + + template + int ConstContainerIndex::pp(std::intptr_t idxPtrNum) + { + const int tmp = RangeHelper::ppx(mIPack, mBlockSizes, idxPtrNum); + IB::mPos += tmp; + return tmp; + } + + template + int ConstContainerIndex::mm(std::intptr_t idxPtrNum) + { + const int tmp = RangeHelper::mmx(mIPack, mBlockSizes, idxPtrNum); + IB::mPos -= tmp; + return tmp; + } + + template + std::string ConstContainerIndex::stringMeta() const + { + return std::dynamic_pointer_cast( IB::mRangePtr )->stringMeta(IB::mPos); + } + + template + typename ConstContainerIndex::MetaType ConstContainerIndex::meta() const + { + MetaType metaTuple; + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(metaTuple) = std::get(mIPack)->meta(); return 0; } ); + return metaTuple; + } + + template + ConstContainerIndex& ConstContainerIndex::at(const MetaType& metaPos) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(mIPack)->at( std::get(metaPos) ); return 0; } ); + IB::mPos = RangeHelper::makePos(mIPack, mBlockSizes); + return *this; + } + + template + size_t ConstContainerIndex::dim() const + { + return sizeof...(Indices); + } + + template + bool ConstContainerIndex::first() const + { + return IB::pos() == 0; + } + + template + bool ConstContainerIndex::last() const + { + return IB::pos() == IB::mMax - 1; + } + + template + bool ConstContainerIndex::sliceMode() const + { + return mNonTrivialBlocks; + } + + template + std::shared_ptr::RangeType> + ConstContainerIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + template + auto ConstContainerIndex::getPtr() -> decltype( std::get( mIPack ) )& + { + return std::get( mIPack ); + } + + template + size_t ConstContainerIndex::getStepSize(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + template + auto ConstContainerIndex::ifor(size_t step, Exprs exs) const + { + return RangeHelper::mkFor<0>(step, mIPack, mBlockSizes, exs); + } + + template + template + auto ConstContainerIndex::iforh(size_t step, Exprs exs) const + { + return RangeHelper::mkForh<0>(step, mIPack, mBlockSizes, exs); + } + + template + template + auto ConstContainerIndex::pifor(size_t step, Exprs exs) const + { + return RangeHelper::mkPFor<0>(step, mIPack, mBlockSizes, exs); + } + + template + std::intptr_t ConstContainerIndex::container() const + { + return mObjPtrNum; + } + + template + ConstContainerIndex& ConstContainerIndex:: + format(const std::array& blocks) + { + mBlockSizes = blocks; + mNonTrivialBlocks = true; + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::setData(const T* data) + { + mData = data; + return *this; + } + + template + const T& ConstContainerIndex::operator*() const + { + return mData[mCPos]; + } + + template + const T* ConstContainerIndex::operator->() const + { + return &mData[mCPos]; + } + + template + ConstContainerIndex ConstContainerIndex::operator++(int) + { + auto tmp = *this; + ++(*this); + return tmp; + } + + template + ConstContainerIndex ConstContainerIndex::operator--(int) + { + auto tmp = *this; + --(*this); + return tmp; + } + + template + ConstContainerIndex& ConstContainerIndex::operator+=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::operator-=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + return *this; + } + + template + ConstContainerIndex ConstContainerIndex::operator+(int num) const + { + auto tmp = *this; + return tmp += num; + } + + template + ConstContainerIndex ConstContainerIndex::operator-(int num) const + { + auto tmp = *this; + return tmp -= num; + } + + template + int ConstContainerIndex::operator-(const ConstContainerIndex& it) const + { + return static_cast( IB::mPos ) - static_cast( it.pos() ); + } + + template + const T& ConstContainerIndex::operator[](int num) const + { + return mData[IB::mPos + num]; + } + + template + bool ConstContainerIndex::operator<(const ConstContainerIndex& it) const + { + return IB::mPos < it.pos(); + } + + template + bool ConstContainerIndex::operator>(const ConstContainerIndex& it) const + { + return IB::mPos > it.pos(); + } + + template + bool ConstContainerIndex::operator<=(const ConstContainerIndex& it) const + { + return IB::mPos <= it.pos(); + } + + template + bool ConstContainerIndex::operator>=(const ConstContainerIndex& it) const + { + return IB::mPos >= it.pos(); + } + +} // namespace MultiArrayTools diff --git a/src/include/container_index.h b/src/include/container_index.h index ee3101f..876caf3 100644 --- a/src/include/container_index.h +++ b/src/include/container_index.h @@ -52,7 +52,6 @@ namespace MultiArrayTools std::array mBlockSizes; const T* mData = nullptr; size_t mCPos; - //const MultiArrayBase* mMa = nullptr; std::intptr_t mObjPtrNum; public: @@ -60,44 +59,9 @@ namespace MultiArrayTools ConstContainerIndex(const ConstContainerIndex& in) = default; ConstContainerIndex& operator=(const ConstContainerIndex& in) = default; - ConstContainerIndex(const ConstContainerIndex& in, bool copy) : - IB(in), - mNonTrivialBlocks(in.mNonTrivialBlocks), - mExternControl(false), - mBlockSizes(in.mBlockSizes), - mData(in.mData), - mCPos(in.mCPos), - mObjPtrNum(in.mObjPtrNum) - { - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) - { - typedef typename std::remove_reference(mIPack))>::type - SubType; - std::get(mIPack) = std::make_shared( in.template get() ) ; - return true; - }); - } + ConstContainerIndex(const ConstContainerIndex& in, bool copy); - ConstContainerIndex& copy(const ConstContainerIndex& in) - { - IB::operator=(in); - mNonTrivialBlocks = in.mNonTrivialBlocks; - mExternControl = false; - mBlockSizes = in.mBlockSizes; - mData = in.mData; - mCPos = in.mCPos; - mObjPtrNum = in.mObjPtrNum; - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) - { - typedef typename std::remove_reference(mIPack))>::type - SubType; - std::get(mIPack) = std::make_shared( in.template get() ) ; - return true; - }); - return *this; - } + ConstContainerIndex& copy(const ConstContainerIndex& in); template ConstContainerIndex& operator=(const ConstContainerIndex& in); @@ -195,438 +159,4 @@ namespace MultiArrayTools } // end namespace MultiArrayTools -/* ========================= * - * --- TEMPLATE CODE --- * - * ========================= */ - -namespace MultiArrayTools -{ - namespace - { - using namespace MultiArrayHelper; - } - - /********************** - * ConstContainerIndex * - **********************/ - - template - template - ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, - std::intptr_t objPtrNum) : - IndexInterface,std::tuple >(range, 0), - mObjPtrNum(objPtrNum) - { - std::get(mBlockSizes) = 1; - sfor_mn - ( [&](auto i) { - auto r = range->template getPtr(); - std::get(mIPack) = r->beginPtr(); - *std::get(mIPack) = 0; - - std::get(mBlockSizes) = sfor_p - ( [&](auto j) { return std::get(mIPack)->max(); } , - [&](auto a, auto b) { return a * b; }); - return 0; - }); - - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - } - - template - template - ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, - std::intptr_t objPtrNum, - const std::array& blockSizes) : - IndexInterface,std::tuple >(range, 0), - mObjPtrNum(objPtrNum) - { - sfor_mn - ( [&](auto i) { - auto r = range->template getPtr(); - std::get(mIPack) = r->beginPtr(); - *std::get(mIPack) = 0; - return 0; - }); - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - mNonTrivialBlocks = true; - } - - template - template - ConstContainerIndex& - ConstContainerIndex::operator=(const ConstContainerIndex& in) - { - mIPack = in.mIPack; - return (*this)(); - } - - - template - ConstContainerIndex& ConstContainerIndex::sync() - { - if(mExternControl){ - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - } - return *this; - } - - template - template - auto ConstContainerIndex::get() const -> decltype( *std::get( mIPack ) )& - { - return *std::get( mIPack ); - } - - template - template - auto ConstContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& - { - return std::get( mIPack ); - } - - template - ConstContainerIndex& ConstContainerIndex::operator()(const std::shared_ptr&... inds) - { - return (*this)(std::make_tuple(inds...)); - } - - template - ConstContainerIndex& ConstContainerIndex::operator()(const std::tuple...>& inds) - { - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(mIPack) = std::get(inds); return 0; } ); - mExternControl = true; - return sync(); - } - - template - ConstContainerIndex& ConstContainerIndex::operator()() - { - return sync(); - } - - template - IndexType ConstContainerIndex::type() const { return IndexType::CONT; } - - template - ConstContainerIndex& ConstContainerIndex::operator++() - { - if(mExternControl){ - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); - } - sfor_m - ( [&](auto i) { - auto& si = *std::get( mIPack ); - if(si.last() and i != 0) { si = 0; return true; } - else { ++si; return false; } - return false; - } ); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - ++IB::mPos; - return *this; - } - - template - ConstContainerIndex& ConstContainerIndex::operator--() - { - if(mExternControl){ - IB::mPos = sfor_m - ( [&](auto i) { return std::get(mIPack); }, - [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); - } - sfor_m - ( [&](auto i) { - auto& si = *std::get( mIPack ); - if(si.first() and i != 0) { si = si.max()-1; return true; } - else { --si; return false; } - return false; - } ); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - --IB::mPos; - return *this; - - } - - template - ConstContainerIndex& ConstContainerIndex::operator=(size_t pos) - { - IB::mPos = pos; - RangeHelper::setIndexPack(mIPack, pos); - mCPos = RangeHelper::makePos(mIPack, mBlockSizes); - return *this; - } - - template - int ConstContainerIndex::pp(std::intptr_t idxPtrNum) - { - const int tmp = RangeHelper::ppx(mIPack, mBlockSizes, idxPtrNum); - IB::mPos += tmp; - return tmp; - } - - template - int ConstContainerIndex::mm(std::intptr_t idxPtrNum) - { - const int tmp = RangeHelper::mmx(mIPack, mBlockSizes, idxPtrNum); - IB::mPos -= tmp; - return tmp; - } - - template - std::string ConstContainerIndex::stringMeta() const - { - return std::dynamic_pointer_cast( IB::mRangePtr )->stringMeta(IB::mPos); - } - - template - typename ConstContainerIndex::MetaType ConstContainerIndex::meta() const - { - MetaType metaTuple; - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(metaTuple) = std::get(mIPack)->meta(); return 0; } ); - return metaTuple; - } - - template - ConstContainerIndex& ConstContainerIndex::at(const MetaType& metaPos) - { - sfor_pn<0,sizeof...(Indices)> - ( [&](auto i) { std::get(mIPack)->at( std::get(metaPos) ); return 0; } ); - IB::mPos = RangeHelper::makePos(mIPack, mBlockSizes); - return *this; - } - - template - size_t ConstContainerIndex::dim() const - { - return sizeof...(Indices); - } - - template - bool ConstContainerIndex::first() const - { - return IB::pos() == 0; - } - - template - bool ConstContainerIndex::last() const - { - return IB::pos() == IB::mMax - 1; - } - - template - bool ConstContainerIndex::sliceMode() const - { - return mNonTrivialBlocks; - } - - template - std::shared_ptr::RangeType> - ConstContainerIndex::range() - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } - - template - template - auto ConstContainerIndex::getPtr() -> decltype( std::get( mIPack ) )& - { - return std::get( mIPack ); - } - - template - size_t ConstContainerIndex::getStepSize(size_t n) - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } - - template - template - auto ConstContainerIndex::ifor(size_t step, Exprs exs) const - { - return RangeHelper::mkFor<0>(step, mIPack, mBlockSizes, exs); - } - - template - template - auto ConstContainerIndex::iforh(size_t step, Exprs exs) const - { - return RangeHelper::mkForh<0>(step, mIPack, mBlockSizes, exs); - } - - template - template - auto ConstContainerIndex::pifor(size_t step, Exprs exs) const - { - return RangeHelper::mkPFor<0>(step, mIPack, mBlockSizes, exs); - } - - template - std::intptr_t ConstContainerIndex::container() const - { - return mObjPtrNum; - } - - template - ConstContainerIndex& ConstContainerIndex:: - format(const std::array& blocks) - { - mBlockSizes = blocks; - mNonTrivialBlocks = true; - return *this; - } - - template - ConstContainerIndex& ConstContainerIndex::setData(const T* data) - { - mData = data; - return *this; - } - - template - const T& ConstContainerIndex::operator*() const - { - //return mMa[*this]; - return mData[mCPos]; - //return mData[IB::mPos]; - } - - template - const T* ConstContainerIndex::operator->() const - { - //return &mMa[*this]; - return &mData[mCPos]; - } - /* - template - T& ConstContainerIndex::operator*() - { - //return mMa[*this]; - return mData[IB::mPos]; - } - - template - T* ConstContainerIndex::operator->() - { - //return &mMa[*this]; - return &mData[IB::mPos]; - } - */ - template - ConstContainerIndex ConstContainerIndex::operator++(int) - { - auto tmp = *this; - ++(*this); - return tmp; - } - - template - ConstContainerIndex ConstContainerIndex::operator--(int) - { - auto tmp = *this; - --(*this); - return tmp; - } - - template - ConstContainerIndex& ConstContainerIndex::operator+=(int diff) - { - if(diff < 0){ - for(int i = 0; i != diff; ++i){ - (*this)--; - } - } - else { - for(int i = 0; i != diff; ++i){ - (*this)++; - } - } - return *this; - } - - template - ConstContainerIndex& ConstContainerIndex::operator-=(int diff) - { - if(diff < 0){ - for(int i = 0; i != diff; ++i){ - (*this)++; - } - } - else { - for(int i = 0; i != diff; ++i){ - (*this)--; - } - } - return *this; - } - - template - ConstContainerIndex ConstContainerIndex::operator+(int num) const - { - auto tmp = *this; - return tmp += num; - } - - template - ConstContainerIndex ConstContainerIndex::operator-(int num) const - { - auto tmp = *this; - return tmp -= num; - } - - template - int ConstContainerIndex::operator-(const ConstContainerIndex& it) const - { - return static_cast( IB::mPos ) - static_cast( it.pos() ); - } - - template - const T& ConstContainerIndex::operator[](int num) const - { - return mData[IB::mPos + num]; - } - - template - bool ConstContainerIndex::operator<(const ConstContainerIndex& it) const - { - return IB::mPos < it.pos(); - } - - template - bool ConstContainerIndex::operator>(const ConstContainerIndex& it) const - { - return IB::mPos > it.pos(); - } - - template - bool ConstContainerIndex::operator<=(const ConstContainerIndex& it) const - { - return IB::mPos <= it.pos(); - } - - template - bool ConstContainerIndex::operator>=(const ConstContainerIndex& it) const - { - return IB::mPos >= it.pos(); - } - -} // end namespace MultiArrayTools - - #endif diff --git a/src/include/multi_array_header.cc.h b/src/include/multi_array_header.cc.h index 78663a4..1149d19 100644 --- a/src/include/multi_array_header.cc.h +++ b/src/include/multi_array_header.cc.h @@ -1,6 +1,7 @@ #include "ranges/ranges_header.cc.h" +#include "container_index.cc.h" #include "multi_array_operation.cc.h" #include "functional_multi_array.cc.h" #include "helper_tools.cc.h"