From 3c653535bc8d91bc3de3c2f46c965f43be74a796 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 3 Nov 2018 22:03:35 +0100 Subject: [PATCH] some fixes (expressions) + dynamic <-> anonymous cast utilities --- src/include/helper_tools.cc.h | 69 ++++++++++++++++++++++++++++ src/include/helper_tools.h | 20 +++++++- src/include/multi_array.cc.h | 11 ++++- src/include/multi_array.h | 3 ++ src/include/ranges/anonymous_range.h | 6 ++- src/include/slice.cc.h | 9 ++++ src/include/slice.h | 2 + src/lib/ranges/anonymous_range.cc | 22 ++++++++- 8 files changed, 137 insertions(+), 5 deletions(-) diff --git a/src/include/helper_tools.cc.h b/src/include/helper_tools.cc.h index d67c4f8..22a9e06 100644 --- a/src/include/helper_tools.cc.h +++ b/src/include/helper_tools.cc.h @@ -131,4 +131,73 @@ namespace MultiArrayTools } } + namespace + { + template + struct CopyRanges + { + template + static inline void exec(const Space1& space1, Space2& space2) + { + std::get(space2) = std::get(space1); + CopyRanges::exec(space1,space2); + } + }; + + template <> + struct CopyRanges<0> + { + template + static inline void exec(const Space1& space1, Space2& space2) + { + std::get<0>(space2) = std::get<0>(space1); + } + }; + } + + template + auto anonToDynView(const MultiArray& ma) + -> ConstSlice> + { + constexpr size_t LAST = sizeof...(RangeTypes)+1; + DynamicRangeFactory drf(rptr(ma)->orig()); + std::tuple,std::shared_ptr..., + std::shared_ptr>> mNSpace; + CopyRanges::exec(ma.range()->space(),mNSpace); + std::get(mNSpace) = createExplicit( drf ); + return ConstSlice>(mNSpace, ma.data()); + } + + template + auto dynToAnonMove(MultiArray>&& ma) + -> MultiArray + { + constexpr size_t LAST = sizeof...(RangeTypes)+1; + AnonymousRangeFactory arf(rptr(ma)->orig()); + std::tuple,std::shared_ptr..., + std::shared_ptr> mNSpace; + CopyRanges::exec(ma.range()->space(),mNSpace); + std::get(mNSpace) = createExplicit( arf ); + return ma.format(mNSpace); + } + + template + auto anonToDynView(const MultiArray& ma) + -> ConstSlice> + { + DynamicRangeFactory drf(rptr<0>(ma)->orig()); + auto mNSpace = std::make_tuple( createExplicit( drf ) ); + return ConstSlice>(mNSpace, ma.data()); + } + + template + auto dynToAnonMove(MultiArray>&& ma) + -> MultiArray + { + AnonymousRangeFactory arf(rptr<0>(ma)->orig()); + auto mNSpace = std::make_tuple( createExplicit( arf ) ); + return ma.format(mNSpace); + } + + } diff --git a/src/include/helper_tools.h b/src/include/helper_tools.h index 884e6f5..39a5afb 100644 --- a/src/include/helper_tools.h +++ b/src/include/helper_tools.h @@ -56,13 +56,29 @@ namespace MultiArrayTools -> std::shared_ptr; template - auto prtr(const MArray& ma) + auto rptr(const MArray& ma) -> decltype(ma.template getRangePtr()); template auto dynamic(const MArray& ma, bool slice = false) -> std::shared_ptr>>; - + + template + auto anonToDynView(const MultiArray& ma) + -> ConstSlice>; + + template + auto dynToAnonMove(MultiArray>&& ma) + -> MultiArray; + + template + auto anonToDynView(const MultiArray& ma) + -> ConstSlice>; + + template + auto dynToAnonMove(MultiArray>&& ma) + -> MultiArray; + template inline void For(const std::shared_ptr& ind, const std::function& ll) { diff --git a/src/include/multi_array.cc.h b/src/include/multi_array.cc.h index aa4990d..eb39f66 100644 --- a/src/include/multi_array.cc.h +++ b/src/include/multi_array.cc.h @@ -152,9 +152,18 @@ namespace MultiArrayTools template MultiArray MultiArray::format(const std::shared_ptr&... nrs) { + MAB::mInit = false; return MultiArray( nrs... , std::move(mCont) ); } - + + template + template + MultiArray MultiArray::format(const std::tuple...>& nrs) + { + MAB::mInit = false; + return MultiArray( nrs , std::move(mCont) ); + } + template const T* MultiArray::data() const { diff --git a/src/include/multi_array.h b/src/include/multi_array.h index 766f1c1..7b887ae 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -67,6 +67,9 @@ namespace MultiArrayTools template MultiArray format(const std::shared_ptr&... nrs); // reformat array using 'nr' which in // total must have the same size as mRange + + template + MultiArray format(const std::tuple...>& nrs); virtual const T* data() const override; virtual T* data() override; diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 39e7144..43b84d1 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -36,6 +36,8 @@ namespace MultiArrayTools template AnonymousRangeFactory(std::shared_ptr... origs); + AnonymousRangeFactory(const std::vector>& origs); + template void append(std::shared_ptr r); @@ -110,6 +112,8 @@ namespace MultiArrayTools template SingleRange(std::shared_ptr... origs); + SingleRange(const std::vector>& origs); + size_t mSize = 1; bool mEmpty = true; @@ -218,7 +222,7 @@ namespace MultiArrayTools mEmpty = false; } } - + template std::shared_ptr SingleRange::fullsub(size_t num) const { diff --git a/src/include/slice.cc.h b/src/include/slice.cc.h index 72bbf94..1006eb3 100644 --- a/src/include/slice.cc.h +++ b/src/include/slice.cc.h @@ -13,6 +13,15 @@ namespace MultiArrayTools { MAB::mProtoI->format(blocks); } + + template + ConstSlice::ConstSlice(const std::tuple...>& ranges, + const T* data) : + MultiArrayBase(ranges), + mData(data) + { + MAB::mInit = true; + } template ConstSlice::ConstSlice(const std::shared_ptr&... ranges, const T* data) : diff --git a/src/include/slice.h b/src/include/slice.h index 7b976fb..ff6eb70 100644 --- a/src/include/slice.h +++ b/src/include/slice.h @@ -20,6 +20,8 @@ namespace MultiArrayTools DEFAULT_MEMBERS(ConstSlice); + ConstSlice(const std::tuple...>& ranges, + const T* data = nullptr); ConstSlice(const std::shared_ptr&... ranges, const T* data = nullptr); ConstSlice(const MultiArrayBase& ma, SIZET... sizes); diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 2d3b368..a52e494 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -11,8 +11,14 @@ namespace MultiArrayTools { mProd = std::shared_ptr( new AnonymousRange() ); } + std::map,std::vector > AnonymousRangeFactory::mAleadyCreated; + + AnonymousRangeFactory::AnonymousRangeFactory(const std::vector>& origs) + { + mProd = std::shared_ptr( new AnonymousRange( origs ) ); + } std::shared_ptr AnonymousRangeFactory::checkIfCreated(const std::vector >& pvec) { @@ -157,7 +163,21 @@ namespace MultiArrayTools { return mOrig; } - + + SingleRange::SingleRange(const std::vector>& origs) : + RangeInterface(), + mOrig(origs) + { + mSize = 1; + for(auto& x: mOrig){ + mSize *= x->size(); + } + if(mOrig.size()){ + mEmpty = false; + } + } + + /***************** * Functions * *****************/