From 0d951f892aeaa00c7f6c82f3c810f548bded6e21 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 15 Jul 2018 15:22:15 +0200 Subject: [PATCH] im com (while extending anonymous range) --- src/include/ranges/anonymous_range.h | 24 ++++++++++++----- src/include/ranges/rpack_num.h | 39 ++++++++++++++++++++++++++++ src/lib/ranges/anonymous_range.cc | 34 +++++++++++++++++++++++- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index b573c44..c079697 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -4,6 +4,7 @@ #define __anonymous_range_h__ #include +#include //#include "base_def.h" #include "ranges/range_base.h" #include "ranges/rpheader.h" @@ -13,6 +14,10 @@ namespace MultiArrayTools typedef SingleIndex AnonymousIndex; + template + using SIZET = size_t; + + // NOT THREAD SAVE!! class AnonymousRangeFactory : public RangeFactoryBase { public: @@ -31,7 +36,11 @@ namespace MultiArrayTools void append(std::shared_ptr r); std::shared_ptr create(); + + private: + std::shared_ptr checkIfCreated(const std::vector >& pvec); + static std::map,std::vector > mAleadyCreated; }; class AnonymousRange : public RangeInterface @@ -61,7 +70,7 @@ namespace MultiArrayTools std::shared_ptr fullsub(size_t num) const; template - MultiRange scast() const; // save cast + MultiRange scast(SIZET... sizes) const; // save cast friend AnonymousRangeFactory; @@ -110,7 +119,7 @@ namespace MultiArrayTools void AnonymousRangeFactory::append(std::shared_ptr r) { std::dynamic_pointer_cast(mProd)->mOrig.push_back(r); - } + } /*********************** * AnonymousRange * @@ -131,8 +140,8 @@ namespace MultiArrayTools { auto rst = std::make_tuple(origs...); mOrig.resize(sizeof...(RangeTypes)); - //RPackNum::RangesToVec( rst, mOrig ); - //RPackNum::getSize( rst ); + RPackNum::RangesToVec( rst, mOrig ); + RPackNum::getSize( rst ); } template @@ -142,9 +151,12 @@ namespace MultiArrayTools } template - MultiRange AnonymousRange::scast() const + MultiRange AnonymousRange::scast(SIZET... sizes) const { - // !!!!!! + std::tuple rtp; + RPackNum::resolveRangeType(mOrig, rtp, mOrig.size()-1, sizes...); + MultiRangeFactory mrf(rtp); + return std::dynamic_pointer_cast >( mrf->create() ); } /***************** diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index 06947e0..cdab813 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -38,6 +38,26 @@ namespace MultiArrayHelper template inline size_t getStepSize(const Index& ii, std::intptr_t j); + + template + void resolveSetRange(std::shared_ptr rp, std::vector > orig, + size_t origpos, size_t size) + { + assert(size == 1); + rp = std::dynamic_pointer_cast( orig[origpos] ); // catch bad cast here!! + } + + template <> + void resolveSetRange(std::shared_ptr rp, + std::vector > orig, + size_t origpos, size_t size) + { + AnonymousRangeFactory arf; + for(size_t op = origpos - size + 1; op != origpos + 1; ++op){ + arf.append(orig[op]); + } + rp = std::dynamic_pointer_cast( arf.create() ); + } template struct RPackNum @@ -254,6 +274,16 @@ namespace MultiArrayHelper sx *= max; RPackNum::getStepSizeX(ii, j, ss, sx); } + + template + static void resolveRangeType(const std::vector > orig, + const RangeTuple& rtp, size_t off, size_t size, SIZET... sizes) + { + constexpr size_t tps = std::tuple_size::value; + typedef decltype(std::get(rtp)) RangeType; + resolveSetRange(std::get(rtp), orig, off, size); + RPackNum::resolveRangeType(orig, rtp, off-size, sizes...); + } }; @@ -433,6 +463,15 @@ namespace MultiArrayHelper sx *= max; } + template + static void resolveRangeType(const std::vector > orig, + const RangeTuple& rtp, size_t off, size_t size) + { + constexpr size_t tps = std::tuple_size::value; + typedef decltype(std::get(rtp)) RangeType; + resolveSetRange(std::get(rtp), orig, off, size); + } + }; template diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 911e6a2..0ee7622 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -6,10 +6,42 @@ namespace MultiArrayTools /****************************** * AnonymousRangeFactory * ******************************/ + + std::shared_ptr checkIfCreated(const std::vector >& pvec) + { + std::shared_ptr out; + bool check = false; + for(auto& x: mAleadyCreated){ + if(x.second.size() == pvec.size()){ + check = true; + for(size_t i = 0; i != x.size(); ++i){ + if(x.second[i] != static_cast( pvec[i].get() ) ){ + check = false; + break; + } + } + if(check == true){ + out = x.first; + break; + } + } + } + if(not check){ + std::vector app(pvec.size()); + for(size_t i = 0; i != app.size(); ++i){ + app[i] = static_cast( pvec[i].get() ); + } + mAleadyCreated[mProd] = app; + out = mProd; + } + return out; + } + std::shared_ptr AnonymousRangeFactory::create() { - //setSelf(); + mProd = checkIfCreated(); + setSelf(); return mProd; }