single index parsing from creation string works

This commit is contained in:
Christian Zimmermann 2018-07-22 21:59:31 +02:00
parent c361c2337f
commit 8dc5dd3317
14 changed files with 161 additions and 38 deletions

View file

@ -9,6 +9,7 @@
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/rpheader.h" #include "ranges/rpheader.h"
#include "ranges/x_to_string.h" #include "ranges/x_to_string.h"
#include "ranges/type_map.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {

View file

@ -15,6 +15,7 @@
#include "ranges/rpack_num.h" #include "ranges/rpack_num.h"
#include "ranges/multi_range_factory_product_map.h" #include "ranges/multi_range_factory_product_map.h"
#include "ranges/x_to_string.h" #include "ranges/x_to_string.h"
#include "ranges/type_map.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {

View file

@ -61,7 +61,7 @@ namespace MultiArrayTools
void setSelf(); void setSelf();
}; };
std::shared_ptr<RangeFactoryBase> createRangeFactory(const std::vector<char>& data); std::shared_ptr<RangeFactoryBase> createRangeFactory(char** dp);
class RangeBase class RangeBase
{ {

View file

@ -1,11 +1,15 @@
#ifdef include_range_type //#ifdef include_range_type
//include_range_type(NONE,0) //include_range_type(NONE,0)
#else //#else
#ifndef include_range_type
#ifdef __ranges_header__ #ifdef __ranges_header__
// assert, that this is only used within range_types/header.h // assert, that this is only used within range_types/header.h
#ifndef __range_type_classic_def__
#define __range_type_classic_def__
namespace MultiArrayTools namespace MultiArrayTools
{ {
typedef SingleIndex<size_t,SpaceType::NONE> ClassicIndex; typedef SingleIndex<size_t,SpaceType::NONE> ClassicIndex;
@ -69,7 +73,7 @@ namespace MultiArrayTools
} }
//#endif // #ifndef __spin_range_h__ #endif // #ifndef __range_type_classic_def__
#endif // #ifdef __ranges_header__ #endif // #ifdef __ranges_header__

View file

@ -10,8 +10,6 @@
#ifdef __incl_this__ #ifdef __incl_this__
#ifndef __ranges_header_included__
#define __ranges_header_included__
#define __ranges_header__ #define __ranges_header__
//#ifndef __ranges_header__ //#ifndef __ranges_header__
@ -24,7 +22,6 @@
#undef __ranges_header__ #undef __ranges_header__
#endif
#endif #endif
#undef __incl_this__ #undef __incl_this__

View file

@ -7,8 +7,8 @@ include_range_type(NUL,-2)
#ifdef __ranges_header__ #ifdef __ranges_header__
// assert, that this is only used within range_types/header.h // assert, that this is only used within range_types/header.h
//#ifndef __spin_range_h__ #ifndef __range_type_null_def__
//#define __spin_range_h__ #define __range_type_null_def__
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -82,7 +82,7 @@ namespace MultiArrayTools
} }
//#endif // #ifndef __spin_range_h__ #endif // #ifndef __range_type_null_def__
#endif // #ifdef __ranges_header__ #endif // #ifdef __ranges_header__

View file

@ -5,6 +5,9 @@ include_range_type(PSPACE,3) // Periodic 1dim space
#ifdef __ranges_header__ #ifdef __ranges_header__
#ifndef __range_type_space_def__
#define __range_type_space_def__
namespace MultiArrayTools namespace MultiArrayTools
{ {
// Periodic 1dim space // Periodic 1dim space
@ -116,6 +119,7 @@ namespace MultiArrayTools
using MSpaceRF = decltype(CreateNDimSpaceRange<N>::template mkf<SpaceRange>()); using MSpaceRF = decltype(CreateNDimSpaceRange<N>::template mkf<SpaceRange>());
} }
#endif // #ifndef __range_type_space_def__
#endif // #ifdef __ranges_header__ #endif // #ifdef __ranges_header__

View file

@ -7,8 +7,8 @@ include_range_type(SPIN,2)
#ifdef __ranges_header__ #ifdef __ranges_header__
// assert, that this is only used within range_types/header.h // assert, that this is only used within range_types/header.h
//#ifndef __spin_range_h__ #ifndef __range_type_spin_def__
//#define __spin_range_h__ #define __range_type_spin_def__
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -75,7 +75,7 @@ namespace MultiArrayTools
} }
//#endif // #ifndef __spin_range_h__ #endif // #ifndef __range_type_spin_def__
#endif // #ifdef __ranges_header__ #endif // #ifdef __ranges_header__

View file

@ -12,6 +12,7 @@
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/x_to_string.h" #include "ranges/x_to_string.h"
#include "ranges/type_map.h"
#include "xfor/xfor.h" #include "xfor/xfor.h"
@ -387,7 +388,8 @@ namespace MultiArrayTools
{ {
DataHeader h; DataHeader h;
h.spaceType = static_cast<int>( TYPE ); h.spaceType = static_cast<int>( TYPE );
h.metaSize = size() * sizeof(U); h.metaSize = metaSize(mSpace);
h.metaType = NumTypeMap<U>::num;
h.multiple = 0; h.multiple = 0;
std::vector<char> out; std::vector<char> out;
out.reserve(h.metaSize + sizeof(DataHeader)); out.reserve(h.metaSize + sizeof(DataHeader));

View file

@ -3,6 +3,7 @@
#define __ma_type_map_h__ #define __ma_type_map_h__
#include <string> #include <string>
#include <vector>
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -20,39 +21,58 @@ namespace MultiArrayTools
}; };
template <typename T> template <typename T>
void stringCat(std::vector<char>& out, const std::vector<T>& in) inline void stringCat(std::vector<char>& out, const std::vector<T>& in)
{ {
const char* scp = reinterpret_cast<const char*>(in.data()); const char* scp = reinterpret_cast<const char*>(in.data());
out.insert(out.end(), scp, scp + in.size() * sizeof(T)); out.insert(out.end(), scp, scp + in.size() * sizeof(T));
} }
template <typename T> template <typename T>
void metaCat(std::vector<T>& vec, char* begin, size_t size) inline size_t metaSize(const std::vector<T>& in)
{
return in.size() * sizeof(T);
}
template <typename T>
inline void metaCat(std::vector<T>& vec, char* begin, size_t size)
{ {
T* tp = reinterpret_cast<T*>( begin ); T* tp = reinterpret_cast<T*>( begin );
vec.insert(vec.end(), tp, tp + size / sizeof(T)); vec.insert(vec.end(), tp, tp + size / sizeof(T));
} }
template <> template <>
void stringCat<std::string>(std::vector<char>& out, const std::vector<std::string>& in) inline void stringCat<std::string>(std::vector<char>& out, const std::vector<std::string>& in)
{ {
std::string tmp = ""; std::string tmp = "";
for(auto& x: in){ for(auto& x: in){
tmp += x + '\0'; tmp += x + '\n';
} }
const char* scp = reinterpret_cast<const char*>(tmp.data()); const char* scp = reinterpret_cast<const char*>(tmp.data());
out.insert(out.end(), scp, scp + tmp.size()); out.insert(out.end(), scp, scp + tmp.size());
} }
template <>
inline size_t metaSize<std::string>(const std::vector<std::string>& in)
{
size_t out = 0;
for(auto& x: in){
out += x.size() + 1;
}
return out;
}
template <> template <>
void metaCat<std::string>(std::vector<std::string>& vec, char* begin, size_t size) inline void metaCat<std::string>(std::vector<std::string>& vec, char* begin, size_t size)
{ {
size_t pos = 0;
std::string tmp(begin, size); std::string tmp(begin, size);
while(pos != size){ size_t pos = 0;
size_t termpos = tmp.find_first_of('\0'); while(pos != tmp.size()){
vec.push_back(tmp.substr(pos,termpos)); std::string es = "\n";
pos = termpos + 1; // skip '\0' size_t termpos = tmp.find_first_of(es, pos);
std::string tmpstr = tmp.substr(pos, termpos-pos);
vec.push_back(tmpstr);
pos = termpos + 1;
} }
} }

View file

@ -1,13 +1,16 @@
//
//#include "ranges/single_range.h"
#include "ranges/type_map.h"
#include "ranges/rheader.h"
#include "ranges/range_types/header.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/rheader.h"
#include "ranges/type_map.h"
#include "ranges/range_types/header.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
using namespace MultiArrayHelpers; //using namespace MultiArrayHelpers;
size_t indexId() size_t indexId()
{ {
@ -16,6 +19,18 @@ namespace MultiArrayTools
return id; return id;
} }
// !!!!!
std::shared_ptr<RangeFactoryBase> mkMULTI(char** dp)
{
return nullptr;
}
// !!!!!
std::shared_ptr<RangeFactoryBase> mkANONYMOUS(char** dp)
{
return nullptr;
}
std::shared_ptr<RangeFactoryBase> createRangeFactory(char** dp) std::shared_ptr<RangeFactoryBase> createRangeFactory(char** dp)
{ {
DataHeader h = *reinterpret_cast<const DataHeader*>(*dp); DataHeader h = *reinterpret_cast<const DataHeader*>(*dp);
@ -26,23 +41,26 @@ namespace MultiArrayTools
if(h.multiple != 0){ if(h.multiple != 0){
if(h.spaceType == static_cast<int>( SpaceType::ANY )) { if(h.spaceType == static_cast<int>( SpaceType::ANY )) {
// multi range // multi range
out = mkMULTI(&dp); out = mkMULTI(dp);
} }
else if(h.spaceType == static_cast<int>( SpaceType::ANON ) ) { else if(h.spaceType == static_cast<int>( SpaceType::ANON ) ) {
// anonymous range // anonymous range
out = mkANONYMOUS(&dp); out = mkANONYMOUS(dp);
} }
else { else {
assert(0); assert(0);
} }
} }
else { else {
VCHECK(h.spaceType);
if(h.spaceType == static_cast<int>( SpaceType::ANY ) ) { if(h.spaceType == static_cast<int>( SpaceType::ANY ) ) {
VCHECK(h.metaType)
// generic single range // generic single range
if(h.metaType == -1){ if(h.metaType == -1){
assert(0); assert(0);
} }
#define register_type(x) else if(x == h.metaType) {\ #define register_type(x) else if(x == h.metaType) {\
VCHECK(x);\
std::vector<TypeMap<x>::type> vd;\ std::vector<TypeMap<x>::type> vd;\
metaCat(vd, *dp, h.metaSize); \ metaCat(vd, *dp, h.metaSize); \
out = std::make_shared<SingleRangeFactory<TypeMap<x>::type, \ out = std::make_shared<SingleRangeFactory<TypeMap<x>::type, \
@ -59,7 +77,8 @@ namespace MultiArrayTools
size_t size = *reinterpret_cast<const size_t*>(*dp); size_t size = *reinterpret_cast<const size_t*>(*dp);
out = std::make_shared<SingleRangeFactory<size_t,SpaceType::NONE> >(size); out = std::make_shared<SingleRangeFactory<size_t,SpaceType::NONE> >(size);
} }
#define inlcude_range_type(x,n) out = mk##x(*dp, h.metaSize); #define include_range_type(x,n) else if(h.spaceType == static_cast<int>( SpaceType::x ) ) { \
out = mk##x(*dp, h.metaSize); }
#include "ranges/range_types/header.h" #include "ranges/range_types/header.h"
#undef inlcude_range_type #undef inlcude_range_type
else { else {

View file

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

View file

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

View file

@ -35,7 +35,7 @@ namespace {
return std::make_tuple(ts...); return std::make_tuple(ts...);
} }
class AnonymousIndexTest : public ::testing::Test class AnonymousTest : public ::testing::Test
{ {
protected: protected:
@ -52,7 +52,7 @@ namespace {
typedef CRF::oType CRange; typedef CRF::oType CRange;
typedef ContainerIndex<double,M3Range::IndexType,SRange::IndexType> CIndex; typedef ContainerIndex<double,M3Range::IndexType,SRange::IndexType> CIndex;
AnonymousIndexTest() AnonymousTest()
{ {
swapFactory<SRF>(rfbptr, { 'e', 'b', 'n' } ); swapFactory<SRF>(rfbptr, { 'e', 'b', 'n' } );
sr1ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() ); sr1ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
@ -89,7 +89,7 @@ namespace {
std::shared_ptr<CRange> cr2ptr; std::shared_ptr<CRange> cr2ptr;
}; };
TEST_F(AnonymousIndexTest, Cast1) TEST_F(AnonymousTest, Cast1)
{ {
AnonymousRangeFactory arf1(sr1ptr,m3rptr); AnonymousRangeFactory arf1(sr1ptr,m3rptr);
auto ar1a = std::dynamic_pointer_cast<AnonymousRange>( arf1.create() ); auto ar1a = std::dynamic_pointer_cast<AnonymousRange>( arf1.create() );
@ -113,7 +113,7 @@ namespace {
EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size()); EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size());
} }
TEST_F(AnonymousIndexTest, Cast2) TEST_F(AnonymousTest, Cast2)
{ {
AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr);
auto ar = std::dynamic_pointer_cast<AnonymousRange>( arf2.create() ); auto ar = std::dynamic_pointer_cast<AnonymousRange>( arf2.create() );
@ -122,6 +122,81 @@ namespace {
EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size()); EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size());
EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size()); EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size());
} }
TEST_F(AnonymousTest, ToString1)
{
std::vector<char> vv = sr1ptr->data();
char* dp = vv.data();
VCHECK(vv.size());
auto ff = createRangeFactory(&dp);
auto ss = std::dynamic_pointer_cast<SRange>( ff->create() );
EXPECT_EQ(sr1ptr->size(), ss->size());
EXPECT_EQ(sr1ptr->get(0), ss->get(0));
EXPECT_EQ(sr1ptr->get(1), ss->get(1));
EXPECT_EQ(sr1ptr->get(2), ss->get(2));
}
TEST_F(AnonymousTest, ToString2)
{
typedef SingleRangeFactory<std::string,SpaceType::ANY> NameRF;
typedef SingleRange<std::string,SpaceType::ANY> NameRange;
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<ClassicRange>( crf.create() );
auto sr = std::dynamic_pointer_cast<SpinRange>( srf.create() );
auto nr = std::dynamic_pointer_cast<NameRange>( nrf.create() );
std::vector<char> cv = cr->data();
std::vector<char> sv = sr->data();
std::vector<char> nv = nr->data();
char* cp = cv.data();
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<ClassicRange>( crf2->create() );
auto srf2 = createRangeFactory(&sp);
auto sr2 = std::dynamic_pointer_cast<SpinRange>( srf2->create() );
auto nrf2 = createRangeFactory(&np);
auto nr2 = std::dynamic_pointer_cast<NameRange>( nrf2->create() );
EXPECT_EQ(cr2->size(), cr->size());
EXPECT_EQ(sr2->size(), sr->size());
EXPECT_EQ(nr2->size(), nr->size());
EXPECT_EQ(nr2->get(0), nr->get(0));
EXPECT_EQ(nr2->get(1), nr->get(1));
EXPECT_EQ(nr2->get(2), nr->get(2));
EXPECT_EQ(nr2->get(3), nr->get(3));
}
/*
TEST_F(AnonymousTest, ToString3)
{
AnonymousRangeFactory arf2(sr1ptr,m3rptr,sr2ptr);
auto ar = std::dynamic_pointer_cast<AnonymousRange>( arf2.create() );
std::vector<char> vv = ar->data();
auto ff2 = std::dynamic_pointer_cast<AnonymousRangeFactory>( createRangeFactory(vv) );
auto ar2 = createExplicit( ff2 );
EXPECT_EQ(ar2->size(), ar->size());
EXPECT_EQ(ar2->anonymousDim(), ar->anonymousDim());
}*/
} // end namespace } // end namespace