From d8ecf51698d99eb90392d15cf512f2ae70c25c9d Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 14 Mar 2019 14:20:38 +0100 Subject: [PATCH] char meta function in ranges --- src/include/map_range.cc.h | 14 +++++++- src/include/map_range.h | 2 ++ src/include/ranges/anonymous_range.h | 2 ++ src/include/ranges/dynamic_range.cc.h | 32 ++++++++++++++----- src/include/ranges/dynamic_range.h | 6 ++-- src/include/ranges/multi_range.h | 16 +++++++++- src/include/ranges/range_base.h | 4 +-- .../ranges/range_types/classic_range.h | 2 ++ src/include/ranges/range_types/null_range.h | 2 ++ src/include/ranges/range_types/space_range.h | 2 ++ src/include/ranges/range_types/spin_range.h | 2 ++ src/include/ranges/rpack_num.h | 31 ++++++++++++++---- src/include/ranges/single_range.h | 29 +++++++++-------- src/include/ranges/subrange.h | 14 ++++++++ src/include/ranges/value_range.h | 19 ++++++++++- src/lib/ranges/anonymous_range.cc | 19 +++++++++++ src/lib/ranges/range_types/classic_range.cc | 13 ++++++++ src/lib/ranges/range_types/null_range.cc | 13 ++++++++ src/lib/ranges/range_types/space_range.cc | 13 ++++++++ src/lib/ranges/range_types/spin_range.cc | 13 ++++++++ 20 files changed, 214 insertions(+), 34 deletions(-) diff --git a/src/include/map_range.cc.h b/src/include/map_range.cc.h index 10a3402..9ea55d8 100644 --- a/src/include/map_range.cc.h +++ b/src/include/map_range.cc.h @@ -496,12 +496,24 @@ namespace MultiArrayTools return mSpace; } + template + size_t GenMapRange::typeNum() const + { + return 0; + } + + template + size_t GenMapRange::cmeta(char* target, size_t pos) const + { + return RPackNum::getCMeta(target,pos,mSpace); + } + template std::string GenMapRange::stringMeta(size_t pos) const { auto i = begin(); i = pos; - return "[ " + RPackNum::metaTupleToString(i.meta()) + " ]"; + return "[ " + RPackNum::getStringMeta(i) + " ]"; } template diff --git a/src/include/map_range.h b/src/include/map_range.h index a67f350..4ef7c9a 100644 --- a/src/include/map_range.h +++ b/src/include/map_range.h @@ -257,6 +257,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index 78ad36c..c820138 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -80,6 +80,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/dynamic_range.cc.h b/src/include/ranges/dynamic_range.cc.h index 3cd20b0..0816a78 100644 --- a/src/include/ranges/dynamic_range.cc.h +++ b/src/include/ranges/dynamic_range.cc.h @@ -439,14 +439,9 @@ namespace MultiArrayTools template typename DynamicRange::MetaType DynamicRange::get(size_t pos) const { - for(size_t i = mOrig.size(); i != 0; --i) { - auto& x = mOrig[i-1]; - const size_t redpos = pos % x->size(); - out = out + x->stringMeta(redpos); - pos -= redpos; - pos /= x->size(); - } - return MetaType(); // !!! + std::vector out(cmeta(nullptr,0)); + cmeta(out.data(),pos); + return out; } template @@ -479,6 +474,27 @@ namespace MultiArrayTools return mEmpty; } + template + size_t DynamicRange::typeNum() const + { + return 0; + } + + template + size_t DynamicRange::cmeta(char* target, size_t pos) const + { + size_t out = 0; + for(size_t i = mOrig.size(); i != 0; --i) { + auto& x = mOrig[i-1]; + const size_t redpos = pos % x->size(); + const size_t offset = x->cmeta(target+out,redpos); + out += offset; + pos -= redpos; + pos /= x->size(); + } + return out; + } + template std::string DynamicRange::stringMeta(size_t pos) const { diff --git a/src/include/ranges/dynamic_range.h b/src/include/ranges/dynamic_range.h index 670a9aa..339ddfc 100644 --- a/src/include/ranges/dynamic_range.h +++ b/src/include/ranges/dynamic_range.h @@ -169,7 +169,7 @@ namespace MultiArrayTools //typedef SingleRange DynamicRange; template - class DynamicIndex : public IndexInterface,DynamicMetaT> + class DynamicIndex : public IndexInterface,std::vector> { private: typedef vector>,size_t>> IVecT; @@ -177,7 +177,7 @@ namespace MultiArrayTools IVecT mIVec; public: - typedef IndexInterface,DynamicMetaT> IB; + typedef IndexInterface,std::vector> IB; typedef std::vector MetaType; typedef DynamicRange RangeType; typedef DynamicIndex IType; @@ -319,6 +319,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index dcf0eb5..62b44dc 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -200,6 +200,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; @@ -581,12 +583,24 @@ namespace MultiArrayTools return mSpace; } + template + size_t MultiRange::typeNum() const + { + return 0; + } + + template + size_t MultiRange::cmeta(char* target, size_t pos) const + { + return RPackNum::getCMeta(target,pos,mSpace); + } + template std::string MultiRange::stringMeta(size_t pos) const { auto i = begin(); i = pos; - return "[ " + RPackNum::metaTupleToString(i.meta()) + " ]"; + return "[ " + RPackNum::getStringMeta(i) + " ]"; } template diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index 0e422ff..00a189f 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -81,8 +81,8 @@ namespace MultiArrayTools std::intptr_t id() const; virtual size_t typeNum() const = 0; - virtual std::string stringMeta(size_t pos) const = 0; - virtual const char* cmeta(size_t pos) const = 0; + virtual size_t cmeta(char* target, size_t pos) const = 0; + virtual std::string stringMeta(size_t pos) const = 0; virtual vector data() const = 0; // usefull when writing to files, etc... virtual SpaceType spaceType() const = 0; diff --git a/src/include/ranges/range_types/classic_range.h b/src/include/ranges/range_types/classic_range.h index ee4ae3c..3792093 100644 --- a/src/include/ranges/range_types/classic_range.h +++ b/src/include/ranges/range_types/classic_range.h @@ -42,6 +42,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index a7db938..6a8a1a0 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -46,6 +46,8 @@ namespace MultiArrayTools virtual size_t size() const final; virtual size_t dim() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/range_types/space_range.h b/src/include/ranges/range_types/space_range.h index 8235ff4..5750d61 100644 --- a/src/include/ranges/range_types/space_range.h +++ b/src/include/ranges/range_types/space_range.h @@ -38,6 +38,8 @@ namespace MultiArrayTools virtual size_t size() const final; virtual size_t dim() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index d720f94..a903c1f 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -39,6 +39,8 @@ namespace MultiArrayTools virtual size_t size() const final; virtual size_t dim() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index 7cabbe0..170ea2e 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -340,10 +340,10 @@ namespace MultiArrayHelper RPackNum::checkIfCreated(p,a); } - template - static inline std::string metaTupleToString(const MetaTuple& mtp) + template + static inline std::string getStringMeta(const MIndex& mi) { - return RPackNum::metaTupleToString(mtp) + " , " + xToString(std::get(mtp)); + return RPackNum::getStringMeta(mi) + " , " + mi.template getPtr()->stringMeta(); } template @@ -369,6 +369,16 @@ namespace MultiArrayHelper std::get( stp ) = std::dynamic_pointer_cast( rbvec[N] ); RPackNum::setSpace(rbvec, stp); } + + template + static inline size_t getCMeta(char* target, size_t pos, const std::tuple...>& stp) + { + constexpr size_t NN = sizeof...(Ranges); + auto& r = *std::get(stp); + const size_t ownPos = pos % r.size(); + const size_t offset = r.cmeta(target,ownPos); + return offset + RPackNum::getCMeta(target + offset , (pos - ownPos) / r.size(), stp); + } }; @@ -610,10 +620,10 @@ namespace MultiArrayHelper return reinterpret_cast( std::get<0>(p).get() ) == a[0]; } - template - static inline std::string metaTupleToString(const MetaTuple& mtp) + template + static inline std::string getStringMeta(const MIndex& mi) { - return xToString(std::get<0>(mtp)); + return mi.template getPtr<0>()->stringMeta(); } template @@ -638,6 +648,15 @@ 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) + { + constexpr size_t NN = sizeof...(Ranges); + auto& r = *std::get(stp); + const size_t ownPos = pos % r.size(); + return r.cmeta(target,ownPos); + } + }; template diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 11a86e3..2eb738f 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -232,9 +232,9 @@ namespace MultiArrayTools virtual DataHeader dataHeader() const final; virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; - virtual const char* cmeta() const final; bool isMeta(const U& metaPos) const; @@ -371,12 +371,6 @@ namespace MultiArrayTools return 1; } - template - size_t GenSingleIndex::typeNum() const - { - return NumTypeMap::num; - } - template std::string GenSingleIndex::stringMeta() const { @@ -389,12 +383,6 @@ namespace MultiArrayTools return MetaPtrHandle::RangeType::HASMETACONT>::getMeta ( mMetaPtr, IB::mPos, mExplicitRangePtr ); } - - template - const char* GenSingleIndex::cmeta() const - { - return reinterpret_cast(mSpace.data()); - } template const U* GenSingleIndex::metaPtr() const @@ -578,7 +566,22 @@ namespace MultiArrayTools { return TYPE; } + + template + size_t GenSingleRange::typeNum() const + { + return NumTypeMap::num; + } + template + size_t GenSingleRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = mSpace[pos]; + } + return sizeof(U); + } + template std::string GenSingleRange::stringMeta(size_t pos) const { diff --git a/src/include/ranges/subrange.h b/src/include/ranges/subrange.h index d963549..7503f2d 100644 --- a/src/include/ranges/subrange.h +++ b/src/include/ranges/subrange.h @@ -120,6 +120,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const final; + virtual size_t cmeta(char* target, size_t pos) const final; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; @@ -401,6 +403,18 @@ namespace MultiArrayTools return h; } + template + size_t SubRange::typeNum() const + { + return mFullRange->typeNum(); + } + + template + size_t SubRange::cmeta(char* target, size_t pos) const + { + return mFullRange->cmeta(target, mSubSet[pos]); + } + template std::string SubRange::stringMeta(size_t pos) const { diff --git a/src/include/ranges/value_range.h b/src/include/ranges/value_range.h index 93b9a23..8e8fb9b 100644 --- a/src/include/ranges/value_range.h +++ b/src/include/ranges/value_range.h @@ -111,6 +111,8 @@ namespace MultiArrayTools virtual SpaceType spaceType() const final; virtual DataHeader dataHeader() const final; + virtual size_t typeNum() const = 0; + virtual size_t cmeta(char* target, size_t pos) const = 0; virtual std::string stringMeta(size_t pos) const final; virtual vector data() const final; @@ -347,7 +349,22 @@ namespace MultiArrayTools { return SpaceType::NONE; } - + + template + size_t ValueRange::typeNum() const + { + return NumTypeMap::num; + } + + template + size_t ValueRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = *mMeta; + } + return sizeof(U); + } + template std::string ValueRange::stringMeta(size_t pos) const { diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index ae3ecb7..1583cc2 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -92,7 +92,26 @@ namespace MultiArrayTools { return mEmpty; } + + size_t AnonymousRange::typeNum() const + { + return 0; + } + size_t AnonymousRange::cmeta(char* target, size_t pos) const + { + size_t out = 0; + for(size_t i = mOrig.size(); i != 0; --i) { + auto& x = mOrig[i-1]; + const size_t redpos = pos % x->size(); + const size_t offset = x->cmeta(target+out, redpos); + out += offset; + pos -= redpos; + pos /= x->size(); + } + return out; + } + std::string AnonymousRange::stringMeta(size_t pos) const { std::string out = "[ "; diff --git a/src/lib/ranges/range_types/classic_range.cc b/src/lib/ranges/range_types/classic_range.cc index 7fbd227..1c22cde 100644 --- a/src/lib/ranges/range_types/classic_range.cc +++ b/src/lib/ranges/range_types/classic_range.cc @@ -52,7 +52,20 @@ namespace MultiArrayTools { return SpaceType::NONE; } + + size_t GenSingleRange::typeNum() const + { + return NumTypeMap::num; + } + size_t GenSingleRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = pos; + } + return sizeof(size_t); + } + std::string GenSingleRange::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 107d4b0..ff2edb7 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -75,7 +75,20 @@ namespace MultiArrayTools { return SpaceType::NUL; } + + size_t GenSingleRange::typeNum() const + { + return NumTypeMap::num; + } + size_t GenSingleRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = 0; + } + return sizeof(size_t); + } + std::string GenSingleRange::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 b867f2a..9bd22f0 100644 --- a/src/lib/ranges/range_types/space_range.cc +++ b/src/lib/ranges/range_types/space_range.cc @@ -58,7 +58,20 @@ namespace MultiArrayTools { return SpaceType::PSPACE; } + + size_t GenSingleRange::typeNum() const + { + return NumTypeMap::num; + } + size_t GenSingleRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = get(pos); + } + return sizeof(int); + } + std::string GenSingleRange::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 191ae2f..497fea7 100644 --- a/src/lib/ranges/range_types/spin_range.cc +++ b/src/lib/ranges/range_types/spin_range.cc @@ -55,7 +55,20 @@ namespace MultiArrayTools { return SpaceType::SPIN; } + + size_t GenSingleRange::typeNum() const + { + return NumTypeMap::num; + } + size_t GenSingleRange::cmeta(char* target, size_t pos) const + { + if(target){ + *reinterpret_cast(target) = pos; + } + return sizeof(size_t); + } + std::string GenSingleRange::stringMeta(size_t pos) const { return std::to_string(get(pos));