diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 1213bd8..6adab6d 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -75,6 +75,8 @@ namespace MultiArrayTools virtual IndexType begin() const final; virtual IndexType end() const final; + virtual SpaceType spaceType() const final; + virtual std::string stringMeta(size_t pos) const final; virtual std::vector data() const final; diff --git a/src/include/ranges/container_range.h b/src/include/ranges/container_range.h index 9e396bc..cda6287 100644 --- a/src/include/ranges/container_range.h +++ b/src/include/ranges/container_range.h @@ -32,6 +32,8 @@ namespace MultiArrayTools static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t totalDim() { return mkTotalDim(); } + static constexpr SpaceType STYPE = SpaceType::ANY; + private: ContainerIndex() = default; diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index 6a47265..e13666c 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -40,6 +40,8 @@ namespace MultiArrayTools static constexpr IndexType sType() { return IndexType::MULTI; } static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t totalDim() { return mkTotalDim(); } + + static constexpr SpaceType STYPE = SpaceType::ANY; private: @@ -182,6 +184,8 @@ namespace MultiArrayTools virtual size_t dim() const final; virtual size_t size() const final; + virtual SpaceType spaceType() const final; + virtual std::string stringMeta(size_t pos) const final; virtual std::vector data() const final; @@ -215,6 +219,9 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } + // -> define in range_base.cc + std::shared_ptr mkMULTI(char** dp); + /****************** * MultiIndex * ******************/ @@ -533,6 +540,12 @@ namespace MultiArrayTools return RPackNum::getSize(mSpace); } + template + SpaceType MultiRange::spaceType() const + { + return SpaceType::ANY; + } + template const typename MultiRange::Space& MultiRange::space() const { diff --git a/src/include/ranges/multi_range_register.h b/src/include/ranges/multi_range_register.h new file mode 100644 index 0000000..4b888db --- /dev/null +++ b/src/include/ranges/multi_range_register.h @@ -0,0 +1,9 @@ + +#ifdef register_multi3 + +typedef SingleRange SCRange; + +register_multi3(PSpaceRange,PSpaceRange,PSpaceRange) +register_multi3(SCRange,SCRange,SCRange) + +#endif diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 6785b1c..1824755 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -22,7 +22,7 @@ namespace MultiArrayTools enum class SpaceType { NONE = 0, // meta data is that of a classic range, i.e. 0,1,2,...,N-1 - ANY = 1, // meta data is arbitrary, i.e. explicitly stored + ANY = 1, // meta data is arbitrary, i.e. explicitly stored; range could be multiple #define include_range_type(x,n) x = n, #include "range_types/header.h" #undef include_range_type @@ -79,6 +79,8 @@ namespace MultiArrayTools virtual std::string stringMeta(size_t pos) const = 0; virtual std::vector data() const = 0; // usefull when writing to files, etc... + + virtual SpaceType spaceType() const = 0; //virtual bool regular() const = 0; // integer distance (e.g. 2,3,4,...) //virtual bool linear() const = 0; // 1dim valuable (e.g. 2.45, 3.12, 3.56,...) @@ -101,6 +103,8 @@ namespace MultiArrayTools //typedef typename Index::MetaType MetaType; typedef Index IndexType; + static constexpr SpaceType STYPE = IndexType::STYPE; + virtual Index begin() const = 0; virtual Index end() const = 0; diff --git a/src/include/ranges/range_types/classic_range.h b/src/include/ranges/range_types/classic_range.h index 8415aae..9b8cb55 100644 --- a/src/include/ranges/range_types/classic_range.h +++ b/src/include/ranges/range_types/classic_range.h @@ -38,6 +38,8 @@ namespace MultiArrayTools virtual size_t size() const override; virtual size_t dim() const override; + virtual SpaceType spaceType() const override; + virtual std::string stringMeta(size_t pos) const override; virtual std::vector data() const override; diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index db3f885..d875d54 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -47,6 +47,8 @@ namespace MultiArrayTools virtual std::string stringMeta(size_t pos) const override; virtual std::vector data() const override; + + virtual SpaceType spaceType() const override; size_t get(size_t pos) const; size_t getMeta(size_t metapos) const; diff --git a/src/include/ranges/range_types/space_range.h b/src/include/ranges/range_types/space_range.h index 9558c25..18c5595 100644 --- a/src/include/ranges/range_types/space_range.h +++ b/src/include/ranges/range_types/space_range.h @@ -40,6 +40,8 @@ namespace MultiArrayTools virtual std::string stringMeta(size_t pos) const override; virtual std::vector data() const override; + virtual SpaceType spaceType() const override; + int get(size_t pos) const; size_t getMeta(int metaPos) const; @@ -75,10 +77,12 @@ namespace MultiArrayTools struct PromoteMSpaceRange { template - static auto mk(MultiRange) -> MultiRange; + static auto mk(const MultiRange&) + -> MultiRange; template - static auto mkf(MultiRangeFactory) -> MultiRangeFactory; + static auto mkf(const MultiRangeFactory&) + -> MultiRangeFactory; }; diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index 91010b5..05e8025 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -40,6 +40,8 @@ namespace MultiArrayTools virtual std::string stringMeta(size_t pos) const override; virtual std::vector data() const override; + + virtual SpaceType spaceType() const override; size_t get(size_t pos) const; size_t getMeta(size_t metaPos) const; diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index 3d4e808..3f21e4b 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -314,6 +314,21 @@ namespace MultiArrayHelper out.insert(out.end(), part.begin(), part.end()); RPackNum::fillRangeDataVec(out, tp); } + + template + static inline bool compareSpaceTypes(const std::vector >& rbvec) + { + return rbvec[SIZE-N-1]->spaceType() == Range::STYPE and RPackNum::template compareSpaceTypes(rbvec); + } + + template + static inline void setSpace(const std::vector >& rbvec, + std::tuple...>& stp) + { + typedef typename std::remove_reference( stp ))>::type RType; + std::get( stp ) = std::dynamic_pointer_cast( rbvec[N] ); + RPackNum::setSpace(rbvec, stp); + } }; @@ -527,6 +542,20 @@ namespace MultiArrayHelper out.insert(out.end(), part.begin(), part.end()); } + template + static inline bool compareSpaceTypes(const std::vector >& rbvec) + { + return rbvec[SIZE-1]->spaceType() == Range::STYPE; + } + + template + static inline void setSpace(const std::vector >& rbvec, + std::tuple...>& stp) + { + typedef typename std::remove_reference( stp ))>::type RType; + std::get<0>( stp ) = std::dynamic_pointer_cast( rbvec[0] ); + } + }; template diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 8ce4b23..df3dce6 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -38,6 +38,8 @@ namespace MultiArrayTools static constexpr IndexType sType() { return IndexType::SINGLE; } static constexpr size_t totalDim() { return 1; } static constexpr size_t sDim() { return 1; } + + static constexpr SpaceType STYPE = TYPE; // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== @@ -107,6 +109,8 @@ namespace MultiArrayTools virtual size_t size() const override; virtual size_t dim() const override; + virtual SpaceType spaceType() const override; + virtual std::string stringMeta(size_t pos) const override; virtual std::vector data() const override; @@ -377,6 +381,12 @@ namespace MultiArrayTools return 1; } + template + SpaceType SingleRange::spaceType() const + { + return TYPE; + } + template std::string SingleRange::stringMeta(size_t pos) const { diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 694ab14..cc5c42e 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -77,6 +77,11 @@ namespace MultiArrayTools return 1; } + SpaceType AnonymousRange::spaceType() const + { + return SpaceType::ANON; + } + std::string AnonymousRange::stringMeta(size_t pos) const { std::string out = "[ "; diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index 55915ac..7a70339 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -12,6 +12,32 @@ namespace MultiArrayTools //using namespace MultiArrayHelpers; + template + using STP = std::tuple...>; + + typedef std::vector > RVEC; + + template + inline bool compareSpaceTypes(const RVEC& rvec) + { + return RPackNum:: + template compareSpaceTypes(rvec); + } + + template + inline bool setFactory(const RVEC& rvec, std::shared_ptr& fptr) + { + if(compareSpaceTypes(rvec)) { + STP stp; + RPackNum::setSpace(rvec, stp); + fptr = std::make_shared >(stp); + return true; + } + else { + return false; + } + } + size_t indexId() { static size_t id = 0; @@ -19,16 +45,59 @@ namespace MultiArrayTools return id; } - // !!!!! - std::shared_ptr mkMULTI(char** dp) + std::shared_ptr mkMULTI(char** dp, size_t metaSize) { - return nullptr; + std::shared_ptr out = nullptr; + RVEC rvec(metaSize); + for(size_t i = 0; i != metaSize; ++i){ + auto ff = createRangeFactory(dp); + rvec[i] = ff->create(); + } + + if(metaSize == 0){ + assert(0); + } + else if(metaSize == 1) { +#define register_multi1(TT0) if(setFactory(rvec, out)) {} else +#include "ranges/multi_range_register.h" +#undef register_multi1 + assert(0); + } + else if(metaSize == 2) { +#define register_multi2(TT0,TT1) if(setFactory(rvec, out)) {} else +#include "ranges/multi_range_register.h" +#undef register_multi2 + assert(0); + } + else if(metaSize == 3) { +#define register_multi3(TT0,TT1,TT2) if(setFactory(rvec, out)) {} else +#include "ranges/multi_range_register.h" +#undef register_multi3 + assert(0); + } + else if(metaSize == 4) { +#define register_multi4(TT0,TT1,TT2,TT3) if(setFactory(rvec, out)) {} else +#include "ranges/multi_range_register.h" +#undef register_multi4 + assert(0); + } + else { + assert(0); + } + + return out; } - // !!!!! - std::shared_ptr mkANONYMOUS(char** dp) + std::shared_ptr mkANONYMOUS(char** dp, size_t metaSize) { - return nullptr; + std::shared_ptr out = nullptr; + auto arf = std::make_shared(); + for(size_t i = 0; i != metaSize; ++i){ + auto ff = createRangeFactory(dp); + arf->append( ff->create() ); + } + out = arf; + return out; } std::shared_ptr createRangeFactory(char** dp) @@ -41,26 +110,23 @@ namespace MultiArrayTools if(h.multiple != 0){ if(h.spaceType == static_cast( SpaceType::ANY )) { // multi range - out = mkMULTI(dp); + out = mkMULTI(dp, h.metaSize); } else if(h.spaceType == static_cast( SpaceType::ANON ) ) { // anonymous range - out = mkANONYMOUS(dp); + out = mkANONYMOUS(dp, h.metaSize); } else { assert(0); } } else { - VCHECK(h.spaceType); if(h.spaceType == static_cast( SpaceType::ANY ) ) { - VCHECK(h.metaType) // generic single range if(h.metaType == -1){ assert(0); } #define register_type(x) else if(x == h.metaType) {\ - VCHECK(x);\ std::vector::type> vd;\ metaCat(vd, *dp, h.metaSize); \ out = std::make_shared::type, \ diff --git a/src/lib/ranges/range_types/classic_range.cc b/src/lib/ranges/range_types/classic_range.cc index 6432e0c..d942fa9 100644 --- a/src/lib/ranges/range_types/classic_range.cc +++ b/src/lib/ranges/range_types/classic_range.cc @@ -48,6 +48,11 @@ namespace MultiArrayTools return 1; } + SpaceType SingleRange::spaceType() const + { + return SpaceType::NONE; + } + std::string SingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc index 7653806..86beaaf 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -68,6 +68,11 @@ namespace MultiArrayTools return 1; } + SpaceType SingleRange::spaceType() const + { + return SpaceType::NUL; + } + std::string SingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); diff --git a/src/lib/ranges/range_types/space_range.cc b/src/lib/ranges/range_types/space_range.cc index 8248b38..d28b7cf 100644 --- a/src/lib/ranges/range_types/space_range.cc +++ b/src/lib/ranges/range_types/space_range.cc @@ -54,6 +54,11 @@ namespace MultiArrayTools return 1; } + SpaceType SingleRange::spaceType() const + { + return SpaceType::PSPACE; + } + std::string SingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); diff --git a/src/lib/ranges/range_types/spin_range.cc b/src/lib/ranges/range_types/spin_range.cc index 137cc6c..501283e 100644 --- a/src/lib/ranges/range_types/spin_range.cc +++ b/src/lib/ranges/range_types/spin_range.cc @@ -51,6 +51,11 @@ namespace MultiArrayTools return 1; } + SpaceType SingleRange::spaceType() const + { + return SpaceType::SPIN; + } + std::string SingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos)); diff --git a/src/tests/ranges/anonymous_unit_test.cc b/src/tests/ranges/anonymous_unit_test.cc index d8865f0..9a4f0f5 100644 --- a/src/tests/ranges/anonymous_unit_test.cc +++ b/src/tests/ranges/anonymous_unit_test.cc @@ -127,7 +127,6 @@ namespace { { std::vector vv = sr1ptr->data(); char* dp = vv.data(); - VCHECK(vv.size()); auto ff = createRangeFactory(&dp); auto ss = std::dynamic_pointer_cast( ff->create() ); @@ -144,10 +143,6 @@ namespace { ClassicRF crf(5); SpinRF srf; NameRF nrf({"ab", "cdef", "gh", "ijklmno"}); - - VCHECK(sizeof(DataHeader)); - VCHECK(sizeof(char)); - VCHECK(sizeof(size_t)); auto cr = std::dynamic_pointer_cast( crf.create() ); auto sr = std::dynamic_pointer_cast( srf.create() ); @@ -160,10 +155,6 @@ namespace { char* sp = sv.data(); char* np = nv.data(); - VCHECK(cv.size()); - VCHECK(sv.size()); - VCHECK(nv.size()); - auto crf2 = createRangeFactory(&cp); auto cr2 = std::dynamic_pointer_cast( crf2->create() ); @@ -183,21 +174,42 @@ namespace { EXPECT_EQ(nr2->get(3), nr->get(3)); } - /* - TEST_F(AnonymousTest, ToString3) + + TEST_F(AnonymousTest, ToStringMulti) + { + std::vector vv = m3rptr->data(); + char* dp = vv.data(); + + auto ff2 = std::dynamic_pointer_cast( createRangeFactory(&dp) ); + auto mr2 = std::dynamic_pointer_cast( ff2->create() ); + + EXPECT_EQ(mr2->size(), m3rptr->size()); + EXPECT_EQ(mr2->template getPtr<0>()->size(), + m3rptr->template getPtr<0>()->size()); + EXPECT_EQ(mr2->template getPtr<1>()->size(), + m3rptr->template getPtr<1>()->size()); + EXPECT_EQ(mr2->template getPtr<2>()->size(), + m3rptr->template getPtr<2>()->size()); + auto jj = m3rptr->begin(); + for(auto ii = mr2->begin(); ii.pos() != ii.max(); ++ii, jj = ii.pos()){ + EXPECT_EQ(ii.meta(), jj.meta()); + } + } + + TEST_F(AnonymousTest, ToStringAnonymous) { AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); auto ar = std::dynamic_pointer_cast( arf2.create() ); - std::vector vv = ar->data(); - - auto ff2 = std::dynamic_pointer_cast( createRangeFactory(vv) ); - auto ar2 = createExplicit( ff2 ); + char* dp = vv.data(); + + auto ff2 = std::dynamic_pointer_cast( createRangeFactory(&dp) ); + auto ar2 = std::dynamic_pointer_cast( ff2->create() ); EXPECT_EQ(ar2->size(), ar->size()); EXPECT_EQ(ar2->anonymousDim(), ar->anonymousDim()); - }*/ - + } + } // end namespace int main(int argc, char** argv)