start implementing string to range parsing

This commit is contained in:
Christian Zimmermann 2018-07-22 18:32:43 +02:00
parent f14edd17e4
commit c361c2337f
12 changed files with 179 additions and 6 deletions

View file

@ -39,6 +39,8 @@ namespace MultiArrayTools
int spaceType = static_cast<int>( 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<RangeFactoryBase> createRangeFactory(const std::vector<char>& data);
class RangeBase
{

View file

@ -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__

View file

@ -16,7 +16,7 @@ namespace MultiArrayTools
std::shared_ptr<SingleRange<size_t,SpaceType::NUL> > nullr();
std::shared_ptr<NullIndex> nulli();
template <>
class SingleRangeFactory<size_t,SpaceType::NUL> : public RangeFactoryBase
{
@ -76,6 +76,9 @@ namespace MultiArrayTools
typedef SingleRange<size_t,SpaceType::NUL> NullRange;
typedef SingleRangeFactory<size_t,SpaceType::NUL> NullRF;
std::shared_ptr<NullRF> mkNUL(char* dp, size_t size);
}

View file

@ -66,6 +66,8 @@ namespace MultiArrayTools
typedef SingleRange<int,SpaceType::PSPACE> PSpaceRange;
typedef SingleRangeFactory<int,SpaceType::PSPACE> PSpaceRF;
std::shared_ptr<PSpaceRF> mkPSPACE(char* dp, size_t size);
template <class SpaceRange>
struct PromoteMSpaceRange
{

View file

@ -70,6 +70,8 @@ namespace MultiArrayTools
typedef SingleRange<size_t,SpaceType::SPIN> SpinRange;
typedef SingleRangeFactory<size_t,SpaceType::SPIN> SpinRF;
std::shared_ptr<SpinRF> mkSPIN(char* dp, size_t size);
}

View file

@ -393,8 +393,9 @@ namespace MultiArrayTools
out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
const char* scp = reinterpret_cast<const char*>(mSpace.data());
out.insert(out.end(), scp, scp + h.metaSize);
stringCat(out, mSpace);
//const char* scp = reinterpret_cast<const char*>(mSpace.data());
//out.insert(out.end(), scp, scp + h.metaSize);
return out;
}

View file

@ -0,0 +1,82 @@
#ifndef __ma_type_map_h__
#define __ma_type_map_h__
#include <string>
namespace MultiArrayTools
{
template <size_t N>
struct TypeMap
{
typedef void type;
};
template <typename T>
struct NumTypeMap
{
static constexpr size_t num = 0;
};
template <typename T>
void stringCat(std::vector<char>& out, const std::vector<T>& in)
{
const char* scp = reinterpret_cast<const char*>(in.data());
out.insert(out.end(), scp, scp + in.size() * sizeof(T));
}
template <typename T>
void metaCat(std::vector<T>& vec, char* begin, size_t size)
{
T* tp = reinterpret_cast<T*>( begin );
vec.insert(vec.end(), tp, tp + size / sizeof(T));
}
template <>
void stringCat<std::string>(std::vector<char>& out, const std::vector<std::string>& in)
{
std::string tmp = "";
for(auto& x: in){
tmp += x + '\0';
}
const char* scp = reinterpret_cast<const char*>(tmp.data());
out.insert(out.end(), scp, scp + tmp.size());
}
template <>
void metaCat<std::string>(std::vector<std::string>& 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<n> { typedef t type; };\
template <> struct NumTypeMap<t> { 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

View file

@ -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() = ']';

View file

@ -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<RangeFactoryBase> createRangeFactory(char** dp)
{
DataHeader h = *reinterpret_cast<const DataHeader*>(*dp);
*dp += sizeof(DataHeader);
std::shared_ptr<RangeFactoryBase> out = nullptr;
if(h.multiple != 0){
if(h.spaceType == static_cast<int>( SpaceType::ANY )) {
// multi range
out = mkMULTI(&dp);
}
else if(h.spaceType == static_cast<int>( SpaceType::ANON ) ) {
// anonymous range
out = mkANONYMOUS(&dp);
}
else {
assert(0);
}
}
else {
if(h.spaceType == static_cast<int>( SpaceType::ANY ) ) {
// generic single range
if(h.metaType == -1){
assert(0);
}
#define register_type(x) else if(x == h.metaType) {\
std::vector<TypeMap<x>::type> vd;\
metaCat(vd, *dp, h.metaSize); \
out = std::make_shared<SingleRangeFactory<TypeMap<x>::type, \
SpaceType::ANY> >(vd); }
#include "ranges/type_map.h"
#undef register_type
else {
assert(0);
}
}
else if(h.spaceType == static_cast<int>( SpaceType::NONE ) ) {
// classic range
size_t size = *reinterpret_cast<const size_t*>(*dp);
out = std::make_shared<SingleRangeFactory<size_t,SpaceType::NONE> >(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 *

View file

@ -4,6 +4,11 @@
namespace MultiArrayTools
{
std::shared_ptr<NullRF> mkNUL(char* dp, size_t size)
{
return make_shared<NullRF>();
}
/********************
* SingleRange *
********************/

View file

@ -4,6 +4,12 @@
namespace MultiArrayTools
{
std::shared_ptr<PSpaceRF> mkPSPACE(char* dp, size_t size)
{
size_t max = *reinterpret_cast<size_t*>(dp);
return std::make_shared<PSpaceRF>(max);
}
/********************
* SingleRange *
********************/

View file

@ -4,6 +4,11 @@
namespace MultiArrayTools
{
std::shared_ptr<SpinRF> mkSPIN(char* dp, size_t size)
{
std::make_shared<SpinRF>();
}
/********************
* SingleRange *
********************/