diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index de64fe1..7eaab27 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -12,11 +12,13 @@ namespace MultiArrayTools { - typedef SingleIndex AnonymousIndex; + typedef SingleIndex AnonymousIndex; template using SIZET = size_t; + typedef SingleRange AnonymousRange; + // NOT THREAD SAVE!! class AnonymousRangeFactory : public RangeFactoryBase { @@ -24,7 +26,7 @@ namespace MultiArrayTools typedef AnonymousRange oType; - AnonymousRangeFactory() = default; + AnonymousRangeFactory(); template AnonymousRangeFactory(const std::tuple...>& origs); @@ -46,17 +48,20 @@ namespace MultiArrayTools bool mProductCreated = false; }; - class AnonymousRange : public RangeInterface + template <> + class SingleRange : public RangeInterface { public: - static constexpr bool defaultable = false; - static constexpr size_t ISSTATIC = 0; - static constexpr size_t SIZE = -1; - static constexpr bool HASMETACONT = false; + static constexpr bool defaultable = true; + static constexpr size_t ISSTATIC = 0; + static constexpr size_t SIZE = -1; + static constexpr bool HASMETACONT = false; typedef RangeBase RB; typedef typename RangeInterface::IndexType IndexType; + typedef SingleRange RangeType; + typedef size_t MetaType; virtual size_t size() const override; virtual size_t dim() const override; @@ -73,22 +78,25 @@ namespace MultiArrayTools std::shared_ptr fullsub(size_t num) const; template - MultiRange scast(SIZET... sizes) const; // save cast + std::shared_ptr > scast(SIZET... sizes) const; // save cast friend AnonymousRangeFactory; + static AnonymousRangeFactory factory() + { return AnonymousRangeFactory(); } + protected: - AnonymousRange() = delete; - AnonymousRange(const AnonymousRange& in) = default; + SingleRange() = default; + SingleRange(const AnonymousRange& in) = default; template - AnonymousRange(const std::tuple...>& origs); + SingleRange(const std::tuple...>& origs); template - AnonymousRange(std::shared_ptr... origs); + SingleRange(std::shared_ptr... origs); - size_t mSize = 0; + size_t mSize = 1; std::vector > mOrig; }; @@ -105,7 +113,7 @@ namespace MultiArrayTools /*********************** * AnonymousRange * ***********************/ - + template AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple...>& origs) { @@ -141,12 +149,12 @@ namespace MultiArrayHelper using namespace MultiArrayTools; template <> - inline void resolveSetRange(std::shared_ptr rp, - std::vector > orig, + inline void resolveSetRange(std::shared_ptr& rp, + const std::vector >& orig, size_t origpos, size_t size) { AnonymousRangeFactory arf; - for(size_t op = origpos - size + 1; op != origpos + 1; ++op){ + for(size_t op = origpos; op != origpos + size; ++op){ arf.append(orig[op]); } rp = std::dynamic_pointer_cast( arf.create() ); @@ -170,7 +178,7 @@ namespace MultiArrayTools ***********************/ template - AnonymousRange::AnonymousRange(const std::tuple...>& origs) : + SingleRange::SingleRange(const std::tuple...>& origs) : RangeInterface() { mOrig.resize(sizeof...(RangeTypes)); @@ -179,7 +187,7 @@ namespace MultiArrayTools } template - AnonymousRange::AnonymousRange(std::shared_ptr... origs) : + SingleRange::SingleRange(std::shared_ptr... origs) : RangeInterface() { auto rst = std::make_tuple(origs...); @@ -189,18 +197,18 @@ namespace MultiArrayTools } template - std::shared_ptr AnonymousRange::fullsub(size_t num) const + std::shared_ptr SingleRange::fullsub(size_t num) const { return std::dynamic_pointer_cast( mOrig.at(num) ); } template - MultiRange AnonymousRange::scast(SIZET... sizes) const + std::shared_ptr > SingleRange::scast(SIZET... sizes) const { - std::tuple rtp; - RPackNum::resolveRangeType(mOrig, rtp, mOrig.size()-1, sizes...); + std::tuple...> rtp; + RPackNum::resolveRangeType(mOrig, rtp, 0, sizes...); MultiRangeFactory mrf(rtp); - return std::dynamic_pointer_cast >( mrf->create() ); + return std::dynamic_pointer_cast >( mrf.create() ); } diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 3632d9c..99838ab 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -26,6 +26,7 @@ namespace MultiArrayTools #define include_range_type(x,n) x = n, #include "range_types/header.h" #undef include_range_type + ANON = -1 }; class RangeFactoryBase diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index d1ae3e3..1c0e4a2 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -70,7 +70,7 @@ namespace MultiArrayTools class AnonymousRangeFactory; // anonymous_range.h - class AnonymousRange; + //class AnonymousRange; } diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index c876c36..bfbc05d 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -46,7 +46,7 @@ namespace MultiArrayHelper template - inline void resolveSetRange(std::shared_ptr rp, std::vector > orig, + inline void resolveSetRange(std::shared_ptr& rp, const std::vector >& orig, size_t origpos, size_t size) { assert(size == 1); @@ -278,12 +278,12 @@ namespace MultiArrayHelper } template - static inline void resolveRangeType(const std::vector > orig, - const RangeTuple& rtp, size_t off, size_t size, SIZET... sizes) + static inline void resolveRangeType(const std::vector >& orig, + RangeTuple& rtp, size_t off, size_t size, SIZET... sizes) { constexpr size_t tps = std::tuple_size::value; resolveSetRange(std::get(rtp), orig, off, size); - RPackNum::resolveRangeType(orig, rtp, off-size, sizes...); + RPackNum::resolveRangeType(orig, rtp, off+size, sizes...); } }; @@ -418,7 +418,7 @@ namespace MultiArrayHelper template static inline void RangesToVec(const std::tuple...>& rst, - std::vector >& v) + std::vector >& v) { setRangeToVec(v, std::get<0>(rst)); } @@ -465,8 +465,8 @@ namespace MultiArrayHelper } template - static inline void resolveRangeType(const std::vector > orig, - const RangeTuple& rtp, size_t off, size_t size) + static inline void resolveRangeType(const std::vector >& orig, + RangeTuple& rtp, size_t off, size_t size) { constexpr size_t tps = std::tuple_size::value; resolveSetRange(std::get(rtp), orig, off, size); diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index d48167a..4ab93a6 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -7,6 +7,11 @@ namespace MultiArrayTools * AnonymousRangeFactory * ******************************/ + AnonymousRangeFactory::AnonymousRangeFactory() + { + mProd = std::shared_ptr( new AnonymousRange() ); + } + std::map,std::vector > AnonymousRangeFactory::mAleadyCreated; std::shared_ptr AnonymousRangeFactory::checkIfCreated(const std::vector >& pvec) @@ -75,8 +80,8 @@ namespace MultiArrayTools typename AnonymousRange::IndexType AnonymousRange::begin() const { AnonymousIndex i - (std::dynamic_pointer_cast - ( ClassicRange::factory(size()).create() ) ); // !!! + (std::dynamic_pointer_cast + ( std::shared_ptr(RB::mThis) ) ); i = 0; return i; } @@ -84,8 +89,8 @@ namespace MultiArrayTools typename AnonymousRange::IndexType AnonymousRange::end() const { AnonymousIndex i - ( std::dynamic_pointer_cast - ( ClassicRange::factory(size()).create() ));// !!! + (std::dynamic_pointer_cast + ( std::shared_ptr(RB::mThis) ) ); i = size(); return i; } @@ -103,8 +108,8 @@ namespace MultiArrayTools std::shared_ptr defaultRange(size_t size ) { AnonymousRangeFactory arf - ( std::dynamic_pointer_cast - (ClassicRange::factory(size).create() ) ); + ( std::dynamic_pointer_cast + (AnonymousRange::factory().create() ) ); return std::dynamic_pointer_cast( arf.create() ); } diff --git a/src/tests/ranges/anonymous_unit_test.cc b/src/tests/ranges/anonymous_unit_test.cc index 1ec2d34..0119d8b 100644 --- a/src/tests/ranges/anonymous_unit_test.cc +++ b/src/tests/ranges/anonymous_unit_test.cc @@ -112,6 +112,16 @@ namespace { EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size()); EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size()); } + + TEST_F(AnonymousIndexTest, Cast2) + { + AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); + auto ar = std::dynamic_pointer_cast( arf2.create() ); + auto mr = ar->template scast(1,2); + + EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size()); + EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size()); + } } // end namespace