From 388440764e3e18c01e09c7aab384171d850f70ce Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 12 Nov 2019 13:30:26 +0100 Subject: [PATCH] fix: consistent streaming of tuples --- src/include/map_range.cc.h | 5 +- src/include/ranges/dynamic_range.cc.h | 6 +-- src/include/ranges/multi_range.h | 3 +- src/include/ranges/rpack_num.h | 18 +++---- src/tests/ranges/index_unit_test.cc | 71 ++++++++++++++++++++++----- 5 files changed, 77 insertions(+), 26 deletions(-) diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index da601a9..29bf21d 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -594,7 +594,10 @@ namespace MultiArrayTools template size_t GenMapRange::cmeta(char* target, size_t pos) const { - return RPackNum::getCMeta(target,pos,mSpace,cmetaSize()); + //MetaType* xtarget = reinterpret_cast(target); + assert(0); + return 0; + //return RPackNum::getCMeta(xtarget,pos,mSpace,cmetaSize()); } template diff --git a/src/include/ranges/dynamic_range.cc.h b/src/include/ranges/dynamic_range.cc.h index 4d71dad..05a62ea 100644 --- a/src/include/ranges/dynamic_range.cc.h +++ b/src/include/ranges/dynamic_range.cc.h @@ -529,16 +529,16 @@ namespace MultiArrayTools template std::string DynamicRange::stringMeta(size_t pos) const { - std::string out = "[ "; + std::string out = ""; //size_t xpos = pos; for(size_t i = mOrig.size(); i != 0; --i) { auto& x = mOrig[i-1]; const size_t redpos = pos % x->size(); - out = ( (i == mOrig.size()) ? out : out + " , " ) + x->stringMeta(redpos); + out = x->stringMeta(redpos) + ( (i == mOrig.size()) ? out : ", " + out ); pos -= redpos; pos /= x->size(); } - out += " ]"; + out = "[" + out + "]"; return out; } diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index f37b115..3e45a8b 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -596,7 +596,8 @@ namespace MultiArrayTools size_t MultiRange::cmeta(char* target, size_t pos) const { const size_t off = cmetaSize(); - return RPackNum::getCMeta(target,pos,mSpace,off); + MetaType* xtarget = reinterpret_cast(target); + return RPackNum::getCMeta(xtarget,pos,mSpace,off); } template diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index 7ecdcd5..e0a606c 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -370,17 +370,17 @@ namespace MultiArrayHelper RPackNum::setSpace(rbvec, stp); } - template - static inline size_t getCMeta(char* target, size_t pos, const std::tuple...>& stp, - size_t off) + template + static inline size_t getCMeta(MetaType* xtarget, size_t pos, + const std::tuple...>& stp, size_t off) { //constexpr size_t NN = sizeof...(Ranges); auto& r = *std::get(stp); const size_t ownPos = pos % r.size(); const size_t s = r.cmetaSize(); off -= s; - r.cmeta(target+off,ownPos); - return s + RPackNum::getCMeta(target, (pos - ownPos) / r.size(), stp, off); + r.cmeta(reinterpret_cast(&std::get(*xtarget)), ownPos); + return s + RPackNum::getCMeta(xtarget, (pos - ownPos) / r.size(), stp, off); } template @@ -668,9 +668,9 @@ namespace MultiArrayHelper std::get<0>( stp ) = std::dynamic_pointer_cast( rbvec[0] ); } - template - static inline size_t getCMeta(char* target, size_t pos, const std::tuple...>& stp, - size_t off) + template + static inline size_t getCMeta(MetaType* xtarget, size_t pos, + const std::tuple...>& stp, size_t off) { //constexpr size_t NN = sizeof...(Ranges); auto& r = *std::get<0>(stp); @@ -678,7 +678,7 @@ namespace MultiArrayHelper const size_t s = r.cmetaSize(); off -= s; assert(off == 0); - r.cmeta(target,ownPos); + r.cmeta(reinterpret_cast(&std::get<0>(*xtarget)), ownPos); return s; } diff --git a/src/tests/ranges/index_unit_test.cc b/src/tests/ranges/index_unit_test.cc index b7f4ceb..170e7f9 100644 --- a/src/tests/ranges/index_unit_test.cc +++ b/src/tests/ranges/index_unit_test.cc @@ -251,26 +251,73 @@ namespace { EXPECT_EQ(ci2().pos(), ci2.max()-1); } + const char& resolve0(const vector& cv) + { + return cv[0]; + } + + const std::tuple& resolve1(const vector& cv) + { + return *reinterpret_cast*>(cv.data()+1); + } + TEST_F(DynIndexTest, Iterate) { for(auto i = dr->begin(); i.pos() != i.max(); ++i){ EXPECT_EQ(i.meta().size(), 4*sizeof(char)); } auto j = dr->begin(); - EXPECT_EQ(j.meta(), vector({'e','a','1','0'})); - EXPECT_EQ((++j).meta(), vector({'e','a','1','7'})); - EXPECT_EQ((++j).meta(), vector({'e','b','1','0'})); - EXPECT_EQ((++j).meta(), vector({'e','b','1','7'})); + //std::cout << j.stringMeta() << std::endl; + EXPECT_EQ(j.stringMeta(), "[e, [a,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'e'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','0')); - EXPECT_EQ((++j).meta(), vector({'b','a','1','0'})); - EXPECT_EQ((++j).meta(), vector({'b','a','1','7'})); - EXPECT_EQ((++j).meta(), vector({'b','b','1','0'})); - EXPECT_EQ((++j).meta(), vector({'b','b','1','7'})); + EXPECT_EQ((++j).stringMeta(), "[e, [a,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'e'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','7')); + + EXPECT_EQ((++j).stringMeta(), "[e, [b,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'e'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','0')); + + EXPECT_EQ((++j).stringMeta(), "[e, [b,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'e'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','7')); + + + EXPECT_EQ((++j).stringMeta(), "[b, [a,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'b'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','0')); + + EXPECT_EQ((++j).stringMeta(), "[b, [a,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'b'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','7')); + + EXPECT_EQ((++j).stringMeta(), "[b, [b,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'b'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','0')); + + EXPECT_EQ((++j).stringMeta(), "[b, [b,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'b'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','7')); + + + EXPECT_EQ((++j).stringMeta(), "[n, [a,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'n'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','0')); + + EXPECT_EQ((++j).stringMeta(), "[n, [a,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'n'); + EXPECT_EQ(resolve1(j.meta()), mkt('a','1','7')); + + EXPECT_EQ((++j).stringMeta(), "[n, [b,1,0]]"); + EXPECT_EQ(resolve0(j.meta()), 'n'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','0')); + + EXPECT_EQ((++j).stringMeta(), "[n, [b,1,7]]"); + EXPECT_EQ(resolve0(j.meta()), 'n'); + EXPECT_EQ(resolve1(j.meta()), mkt('b','1','7')); - EXPECT_EQ((++j).meta(), vector({'n','a','1','0'})); - EXPECT_EQ((++j).meta(), vector({'n','a','1','7'})); - EXPECT_EQ((++j).meta(), vector({'n','b','1','0'})); - EXPECT_EQ((++j).meta(), vector({'n','b','1','7'})); } } // end namespace