From c361c2337f272f087245eb968a1f7bde42d0dcd4 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 22 Jul 2018 18:32:43 +0200 Subject: [PATCH] start implementing string to range parsing --- src/include/ranges/range_base.h | 4 + src/include/ranges/range_types/header.h | 6 +- src/include/ranges/range_types/null_range.h | 5 +- src/include/ranges/range_types/space_range.h | 2 + src/include/ranges/range_types/spin_range.h | 2 + src/include/ranges/single_range.h | 5 +- src/include/ranges/type_map.h | 82 ++++++++++++++++++++ src/include/ranges/x_to_string.h | 4 +- src/lib/ranges/range_base.cc | 59 ++++++++++++++ src/lib/ranges/range_types/null_range.cc | 5 ++ src/lib/ranges/range_types/space_range.cc | 6 ++ src/lib/ranges/range_types/spin_range.cc | 5 ++ 12 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 src/include/ranges/type_map.h diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 5ec5d03..061f64c 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -39,6 +39,8 @@ namespace MultiArrayTools int spaceType = static_cast( SpaceType::NONE ); size_t metaSize = 0; // size of meta data int multiple = 0; // = 1 if multi range + int metaType = 0; // type of meta data + inline size_t v() const { return version; } }; class RangeFactoryBase @@ -58,6 +60,8 @@ namespace MultiArrayTools // call this function before returning product !! void setSelf(); }; + + std::shared_ptr createRangeFactory(const std::vector& data); class RangeBase { diff --git a/src/include/ranges/range_types/header.h b/src/include/ranges/range_types/header.h index 0d308f6..1c3bcd8 100644 --- a/src/include/ranges/range_types/header.h +++ b/src/include/ranges/range_types/header.h @@ -10,6 +10,9 @@ #ifdef __incl_this__ +#ifndef __ranges_header_included__ +#define __ranges_header_included__ + #define __ranges_header__ //#ifndef __ranges_header__ //#define __ranges_header__ @@ -20,7 +23,8 @@ #include "classic_range.h" #undef __ranges_header__ -//#endif + +#endif #endif #undef __incl_this__ diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index 900bcee..e078cb6 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -16,7 +16,7 @@ namespace MultiArrayTools std::shared_ptr > nullr(); std::shared_ptr nulli(); - + template <> class SingleRangeFactory : public RangeFactoryBase { @@ -76,6 +76,9 @@ namespace MultiArrayTools typedef SingleRange NullRange; typedef SingleRangeFactory NullRF; + + std::shared_ptr mkNUL(char* dp, size_t size); + } diff --git a/src/include/ranges/range_types/space_range.h b/src/include/ranges/range_types/space_range.h index e64c754..3be549c 100644 --- a/src/include/ranges/range_types/space_range.h +++ b/src/include/ranges/range_types/space_range.h @@ -66,6 +66,8 @@ namespace MultiArrayTools typedef SingleRange PSpaceRange; typedef SingleRangeFactory PSpaceRF; + std::shared_ptr mkPSPACE(char* dp, size_t size); + template struct PromoteMSpaceRange { diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index f7b7f80..02c4e7c 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -70,6 +70,8 @@ namespace MultiArrayTools typedef SingleRange SpinRange; typedef SingleRangeFactory SpinRF; + + std::shared_ptr mkSPIN(char* dp, size_t size); } diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index e4d0bbf..f8dbe11 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -393,8 +393,9 @@ namespace MultiArrayTools out.reserve(h.metaSize + sizeof(DataHeader)); char* hcp = reinterpret_cast(&h); out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); - const char* scp = reinterpret_cast(mSpace.data()); - out.insert(out.end(), scp, scp + h.metaSize); + stringCat(out, mSpace); + //const char* scp = reinterpret_cast(mSpace.data()); + //out.insert(out.end(), scp, scp + h.metaSize); return out; } diff --git a/src/include/ranges/type_map.h b/src/include/ranges/type_map.h new file mode 100644 index 0000000..695e884 --- /dev/null +++ b/src/include/ranges/type_map.h @@ -0,0 +1,82 @@ + +#ifndef __ma_type_map_h__ +#define __ma_type_map_h__ + +#include + +namespace MultiArrayTools +{ + + template + struct TypeMap + { + typedef void type; + }; + + template + struct NumTypeMap + { + static constexpr size_t num = 0; + }; + + template + void stringCat(std::vector& out, const std::vector& in) + { + const char* scp = reinterpret_cast(in.data()); + out.insert(out.end(), scp, scp + in.size() * sizeof(T)); + } + + template + void metaCat(std::vector& vec, char* begin, size_t size) + { + T* tp = reinterpret_cast( begin ); + vec.insert(vec.end(), tp, tp + size / sizeof(T)); + } + + template <> + void stringCat(std::vector& out, const std::vector& in) + { + std::string tmp = ""; + for(auto& x: in){ + tmp += x + '\0'; + } + const char* scp = reinterpret_cast(tmp.data()); + out.insert(out.end(), scp, scp + tmp.size()); + } + + template <> + void metaCat(std::vector& vec, char* begin, size_t size) + { + size_t pos = 0; + std::string tmp(begin, size); + while(pos != size){ + size_t termpos = tmp.find_first_of('\0'); + vec.push_back(tmp.substr(pos,termpos)); + pos = termpos + 1; // skip '\0' + } + } + +#define include_type(t,n) template <> struct TypeMap { typedef t type; };\ + template <> struct NumTypeMap { static constexpr size_t num = n; }; + + include_type(size_t,1) + include_type(int,2) + include_type(char,3) + include_type(float,4) + include_type(double,5) + include_type(std::string,6) + +#undef include_type +} +#endif + +#ifdef register_type + +register_type(1) +register_type(2) +register_type(3) +register_type(4) +register_type(5) +register_type(6) + +#endif diff --git a/src/include/ranges/x_to_string.h b/src/include/ranges/x_to_string.h index 81dc63a..77722b2 100644 --- a/src/include/ranges/x_to_string.h +++ b/src/include/ranges/x_to_string.h @@ -53,7 +53,7 @@ namespace MultiArrayHelper { std::string out = "[ "; for(auto& y: x){ - out += y + " , "; + out += xToString(y) + " , "; } out.pop_back(); out.back() = ']'; @@ -65,7 +65,7 @@ namespace MultiArrayHelper { std::string out = "[ "; for(auto& y: x){ - out += y + " , "; + out += xToString(y) + " , "; } out.pop_back(); out.back() = ']'; diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index 5df5738..7a6ff22 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -1,15 +1,74 @@ #include "ranges/range_base.h" +#include "ranges/rheader.h" +#include "ranges/type_map.h" +#include "ranges/range_types/header.h" namespace MultiArrayTools { + using namespace MultiArrayHelpers; + size_t indexId() { static size_t id = 0; ++id; return id; } + + std::shared_ptr createRangeFactory(char** dp) + { + DataHeader h = *reinterpret_cast(*dp); + *dp += sizeof(DataHeader); + + std::shared_ptr out = nullptr; + + if(h.multiple != 0){ + if(h.spaceType == static_cast( SpaceType::ANY )) { + // multi range + out = mkMULTI(&dp); + } + else if(h.spaceType == static_cast( SpaceType::ANON ) ) { + // anonymous range + out = mkANONYMOUS(&dp); + } + else { + assert(0); + } + } + else { + if(h.spaceType == static_cast( SpaceType::ANY ) ) { + // generic single range + if(h.metaType == -1){ + assert(0); + } +#define register_type(x) else if(x == h.metaType) {\ + std::vector::type> vd;\ + metaCat(vd, *dp, h.metaSize); \ + out = std::make_shared::type, \ + SpaceType::ANY> >(vd); } +#include "ranges/type_map.h" +#undef register_type + else { + assert(0); + } + + } + else if(h.spaceType == static_cast( SpaceType::NONE ) ) { + // classic range + size_t size = *reinterpret_cast(*dp); + out = std::make_shared >(size); + } +#define inlcude_range_type(x,n) out = mk##x(*dp, h.metaSize); +#include "ranges/range_types/header.h" +#undef inlcude_range_type + else { + assert(0); + } + *dp += h.metaSize; + } + return out; + } /************************* * RangeFactoryBase * diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc index dac0f90..1a967aa 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -4,6 +4,11 @@ namespace MultiArrayTools { + std::shared_ptr mkNUL(char* dp, size_t size) + { + return make_shared(); + } + /******************** * SingleRange * ********************/ diff --git a/src/lib/ranges/range_types/space_range.cc b/src/lib/ranges/range_types/space_range.cc index b126bc5..8248b38 100644 --- a/src/lib/ranges/range_types/space_range.cc +++ b/src/lib/ranges/range_types/space_range.cc @@ -4,6 +4,12 @@ namespace MultiArrayTools { + std::shared_ptr mkPSPACE(char* dp, size_t size) + { + size_t max = *reinterpret_cast(dp); + return std::make_shared(max); + } + /******************** * SingleRange * ********************/ diff --git a/src/lib/ranges/range_types/spin_range.cc b/src/lib/ranges/range_types/spin_range.cc index a2e1b96..1a3ffae 100644 --- a/src/lib/ranges/range_types/spin_range.cc +++ b/src/lib/ranges/range_types/spin_range.cc @@ -4,6 +4,11 @@ namespace MultiArrayTools { + std::shared_ptr mkSPIN(char* dp, size_t size) + { + std::make_shared(); + } + /******************** * SingleRange * ********************/