From a8c62d048105049e731b3337e761587a1a7b5486 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 25 Dec 2017 13:44:55 +0100 Subject: [PATCH] some corrections... --- src/multi_array_operation.h | 116 ++++++++++++++++++++++------------- src/operation_utils.h | 2 + src/ranges/container_range.h | 5 +- src/ranges/index_info.h | 17 ++++- src/ranges/multi_range.h | 5 +- src/ranges/rpack_num.h | 14 +++-- 6 files changed, 105 insertions(+), 54 deletions(-) diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index c038870..6ee1c78 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -17,6 +17,8 @@ #include "ranges/rheader.h" #include "pack_num.h" +#include "ranges/index_info.h" + namespace MultiArrayTools { @@ -54,7 +56,8 @@ namespace MultiArrayTools { public: - OperationTemplate(OperationClass* oc); + OperationClass& THIS() { return static_cast(*this); } + const OperationClass& THIS() const { return static_cast(*this); } template auto operator+(const Second& in) const @@ -77,7 +80,8 @@ namespace MultiArrayTools -> Contraction; private: - OperationClass* mOc; + friend OperationClass; + OperationTemplate() = default; }; template @@ -104,11 +108,13 @@ namespace MultiArrayTools const OperationMaster& block() const; protected: - + + std::shared_ptr mkIndex(std::shared_ptr& index); void performAssignment(std::intptr_t blockIndexNum); OpClass const& mSecond; MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; + IndexInfo mIInfo; mutable MBlock mBlock; }; @@ -134,9 +140,14 @@ namespace MultiArrayTools const ConstOperationRoot& block() const; protected: - + + std::shared_ptr + mkIndex(const MultiArrayBase& ma, + const std::shared_ptr&... indices); + MultiArrayBase const& mArrayRef; std::shared_ptr mIndex; + IndexInfo mIInfo; mutable Block mBlock; }; @@ -167,9 +178,14 @@ namespace MultiArrayTools const OperationRoot& block() const; protected: - + + std::shared_ptr + mkIndex(const MultiArrayBase& ma, + const std::shared_ptr&... indices); + MutableMultiArrayBase& mArrayRef; std::shared_ptr mIndex; + IndexInfo mIInfo; mutable MBlock mBlock; std::shared_ptr mBlockIndex; // predefine to save time }; @@ -193,7 +209,7 @@ namespace MultiArrayTools const Operation& block() const; protected: - std::tuple mOps; + std::tuple mOps; mutable BlockResult mRes; }; @@ -214,7 +230,7 @@ namespace MultiArrayTools protected: - Op mOp; + const Op& mOp; std::shared_ptr mInd; mutable BlockResult mRes; }; @@ -266,16 +282,13 @@ namespace MultiArrayTools /*************************** * OperationTemplate * ***************************/ - - template - OperationTemplate::OperationTemplate(OperationClass* oc) : mOc(oc) {} template template auto OperationTemplate::operator+(const Second& in) const -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(THIS(), in); } template @@ -283,7 +296,7 @@ namespace MultiArrayTools auto OperationTemplate::operator-(const Second& in) const -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(THIS(), in); } template @@ -291,7 +304,7 @@ namespace MultiArrayTools auto OperationTemplate::operator*(const Second& in) const -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(THIS(), in); } template @@ -299,7 +312,7 @@ namespace MultiArrayTools auto OperationTemplate::operator/(const Second& in) const -> Operation,OperationClass,Second> { - return Operation,OperationClass,Second>(*mOc, in); + return Operation,OperationClass,Second>(THIS(), in); } template @@ -307,7 +320,7 @@ namespace MultiArrayTools auto OperationTemplate::c(std::shared_ptr& ind) const -> Contraction { - return Contraction(*mOc, ind); + return Contraction(THIS(), ind); } @@ -319,14 +332,8 @@ namespace MultiArrayTools OperationMaster:: OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index) : - mSecond(second), mArrayRef(ma), mIndex() + mSecond(second), mArrayRef(ma), mIndex(mkIndex(index)), mIInfo(*mIndex) { - MultiRangeFactory mrf( index->range() ); - std::shared_ptr > mr = - std::dynamic_pointer_cast >( mrf.create() ); - mIndex = std::make_shared( mr->begin() ); - (*mIndex) = *index; - auto blockIndex = seekBlockIndex( make_viwb( mIndex ), second); std::intptr_t blockIndexNum = blockIndex->getPtrNum(); @@ -341,22 +348,29 @@ namespace MultiArrayTools OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, std::shared_ptr& index, std::shared_ptr blockIndex) : - mSecond(second), mArrayRef(ma), mIndex() + mSecond(second), mArrayRef(ma), mIndex(mkIndex(index)), mIInfo(*mIndex) { - MultiRangeFactory mrf( index->range() ); - std::shared_ptr > mr = - std::dynamic_pointer_cast >( mrf.create() ); - mIndex = std::make_shared( mr->begin() ); - (*mIndex) = *index; - std::intptr_t blockIndexNum = blockIndex->getPtrNum(); second.block(blockIndex, true); performAssignment(blockIndexNum); } + + template + std::shared_ptr::IndexType> + OperationMaster:: + mkIndex(std::shared_ptr& index) + { + MultiRangeFactory mrf( index->range() ); + std::shared_ptr > mr = + std::dynamic_pointer_cast >( mrf.create() ); + auto i = std::make_shared( mr->begin() ); + (*i) = *index; + return i; + } - template + template void OperationMaster::performAssignment(std::intptr_t blockIndexNum) { //size_t cnt = 0; @@ -412,12 +426,21 @@ namespace MultiArrayTools ConstOperationRoot:: ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices) : - OperationTemplate >(this), - mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) - { - (*mIndex)(indices...); - } + //OperationTemplate >(this), + mArrayRef(ma), mIndex( mkIndex(ma,indices...) ), mIInfo(*mIndex) + {} + template + std::shared_ptr::IndexType> + ConstOperationRoot:: + mkIndex(const MultiArrayBase& ma, + const std::shared_ptr&... indices) + { + auto i = std::make_shared( ma.range() ); + (*mIndex)(indices...); + return i; + } + template const Block& ConstOperationRoot::get() const { @@ -450,13 +473,22 @@ namespace MultiArrayTools OperationRoot:: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices) : - OperationTemplate >(this), - mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ), + //OperationTemplate >(this), + mArrayRef(ma), mIndex( mkIndex( ma, indices... ) ), mIInfo(*mIndex), mBlockIndex(nullptr) - { - (*mIndex)(indices...); - } + {} + template + std::shared_ptr::IndexType> + OperationRoot:: + mkIndex(const MultiArrayBase& ma, + const std::shared_ptr&... indices) + { + auto i = std::make_shared( ma.range() ); + (*mIndex)(indices...); + return i; + } + template template OperationMaster OperationRoot::operator=(const OpClass& in) @@ -514,7 +546,7 @@ namespace MultiArrayTools template Operation::Operation(const Ops&... ops) : - OperationTemplate >(this), + //OperationTemplate >(this), mOps(ops...) {} template @@ -548,7 +580,7 @@ namespace MultiArrayTools template Contraction::Contraction(const Op& op, std::shared_ptr ind) : - OperationTemplate >(this), + //OperationTemplate >(this), mOp(op), mInd(ind) {} diff --git a/src/operation_utils.h b/src/operation_utils.h index c837761..c101522 100644 --- a/src/operation_utils.h +++ b/src/operation_utils.h @@ -23,6 +23,8 @@ namespace MultiArrayTools void seekIndexInst(std::shared_ptr i, std::vector >& ivec); + + //void seekIndexInst(const IndexInfo& i, std::vector& ivec); BTSS getBlockType(std::shared_ptr i, std::shared_ptr j, diff --git a/src/ranges/container_range.h b/src/ranges/container_range.h index 888a752..6c0a880 100644 --- a/src/ranges/container_range.h +++ b/src/ranges/container_range.h @@ -337,8 +337,9 @@ namespace MultiArrayTools template std::vector ContainerIndex::infoVec() const { - std::vector out(sizeof...(Indices)); - RPackNum::buildInfoVec(out, mIPack); + std::vector out; + out.reserve(sizeof...(Indices)); + RPackNum::buildInfoVec(out, mIPack, mBlockSizes); return std::move( out ); } diff --git a/src/ranges/index_info.h b/src/ranges/index_info.h index d58230d..d267dc2 100644 --- a/src/ranges/index_info.h +++ b/src/ranges/index_info.h @@ -20,13 +20,15 @@ namespace MultiArrayTools IndexInfo(IndexInfo&& in) = default; IndexInfo& operator=(IndexInfo&& in) = default; - IndexInfo(const IndexInfo& in) = default; - IndexInfo& operator=(const IndexInfo& in) = default; - + //IndexInfo(const IndexInfo& in) = default; + //IndexInfo& operator=(const IndexInfo& in) = default; template IndexInfo(const IndexClass& ind, size_t stepSize = 1); + template + IndexInfo& reassign(const IndexClass& ind, size_t stepSize = 1); + bool operator==(const IndexInfo& in) const; bool operator!=(const IndexInfo& in) const; @@ -64,6 +66,15 @@ namespace MultiArrayTools mMax(ind.max()), mStepSize(stepSize) {} + + template + IndexInfo& IndexInfo::reassign(const IndexClass& ind, size_t stepSize) + { + IndexInfo ii(ind, stepSize); + (*this) = std::move(ii); + return *this; + } + } // end namespace MultiArrayTools diff --git a/src/ranges/multi_range.h b/src/ranges/multi_range.h index 721b4f8..7d4a799 100644 --- a/src/ranges/multi_range.h +++ b/src/ranges/multi_range.h @@ -381,8 +381,9 @@ namespace MultiArrayTools template std::vector MultiIndex::infoVec() const { - std::vector out(sizeof...(Indices)); - RPackNum::buildInfoVec(out, mIPack); + std::vector out; + out.reserve(sizeof...(Indices)); + RPackNum::buildInfoVec(out, mIPack, mBlockSizes); return std::move( out ); } diff --git a/src/ranges/rpack_num.h b/src/ranges/rpack_num.h index fa6ad79..82ddcc3 100644 --- a/src/ranges/rpack_num.h +++ b/src/ranges/rpack_num.h @@ -211,10 +211,12 @@ namespace MultiArrayHelper template static void buildInfoVec(std::vector& out, - std::tuple...>& ip) + const std::tuple...>& ip, + const std::array& bs) { - out.emplace_back(*std::get(ip)); - RPackNum::buildInfoVec(out, ip); + static const size_t POS = sizeof...(Indices)-N-1; + out.emplace_back(*std::get(ip), std::get(bs)); + RPackNum::buildInfoVec(out, ip, bs); } }; @@ -373,9 +375,11 @@ namespace MultiArrayHelper template static void buildInfoVec(std::vector& out, - std::tuple...>& ip) + const std::tuple...>& ip, + const std::array& bs) { - out.emplace_back(*std::get(ip)); + static const size_t POS = sizeof...(Indices)-1; + out.emplace_back(*std::get(ip), std::get(bs)); } };