slight changes in range interface (typeNum) + fixes in dynamic range meta/index

This commit is contained in:
Christian Zimmermann 2019-03-19 20:58:59 +01:00
parent 8ed5d01c46
commit 8a37107ac1
23 changed files with 308 additions and 84 deletions

View file

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8)
project(multi_array) project(multi_array)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -O0 -march=native -faligned-new -funroll-loops -fopenmp")
enable_testing() enable_testing()

View file

@ -497,15 +497,23 @@ namespace MultiArrayTools
} }
template <class MapF, SpaceType XSTYPE, class... Ranges> template <class MapF, SpaceType XSTYPE, class... Ranges>
size_t GenMapRange<MapF,XSTYPE,Ranges...>::typeNum() const vector<size_t> GenMapRange<MapF,XSTYPE,Ranges...>::typeNum() const
{ {
return 0; vector<size_t> o;
RPackNum<sizeof...(Ranges)-1>::getTypeNum(o,mSpace);
return o;
} }
template <class MapF, SpaceType XSTYPE, class... Ranges> template <class MapF, SpaceType XSTYPE, class... Ranges>
size_t GenMapRange<MapF,XSTYPE,Ranges...>::cmeta(char* target, size_t pos) const size_t GenMapRange<MapF,XSTYPE,Ranges...>::cmeta(char* target, size_t pos) const
{ {
return RPackNum<sizeof...(Ranges)-1>::getCMeta(target,pos,mSpace); return RPackNum<sizeof...(Ranges)-1>::getCMeta(target,pos,mSpace,cmetaSize());
}
template <class MapF, SpaceType XSTYPE, class... Ranges>
size_t GenMapRange<MapF,XSTYPE,Ranges...>::cmetaSize() const
{
return RPackNum<sizeof...(Ranges)-1>::getCMetaSize(mSpace);
} }
template <class MapF, SpaceType XSTYPE, class... Ranges> template <class MapF, SpaceType XSTYPE, class... Ranges>

View file

@ -231,7 +231,7 @@ namespace MultiArrayTools
GenMapRange(const MapF& mapf, const Space& space); GenMapRange(const MapF& mapf, const Space& space);
Space mSpace; Space mSpace;
const MapF& mMapf; MapF mMapf;
std::shared_ptr<ORType> mOutRange; std::shared_ptr<ORType> mOutRange;
MultiArray<size_t,ORType> mMapMult; MultiArray<size_t,ORType> mMapMult;
@ -257,8 +257,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -80,8 +80,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -176,6 +176,8 @@ namespace MultiArrayTools
template <class EC> template <class EC>
DynamicIndex<EC>& DynamicIndex<EC>::operator++() DynamicIndex<EC>& DynamicIndex<EC>::operator++()
{ {
++IB::mPos;
if(mIvecInit){
size_t ipos = mIVec.size()-1; size_t ipos = mIVec.size()-1;
auto& ii = mIVec[ipos].first; auto& ii = mIVec[ipos].first;
auto& jj = mIVec[ipos-1].first; auto& jj = mIVec[ipos-1].first;
@ -184,12 +186,15 @@ namespace MultiArrayTools
++(*jj); ++(*jj);
--ipos; --ipos;
} }
}
return *this; return *this;
} }
template <class EC> template <class EC>
DynamicIndex<EC>& DynamicIndex<EC>::operator--() DynamicIndex<EC>& DynamicIndex<EC>::operator--()
{ {
--IB::mPos;
if(mIvecInit){
size_t ipos = mIVec.size()-1; size_t ipos = mIVec.size()-1;
auto& ii = mIVec[ipos].first; auto& ii = mIVec[ipos].first;
auto& jj = mIVec[ipos-1].first; auto& jj = mIVec[ipos-1].first;
@ -198,12 +203,14 @@ namespace MultiArrayTools
--(*jj); --(*jj);
--ipos; --ipos;
} }
}
return *this; return *this;
} }
template <class EC> template <class EC>
DynamicIndex<EC>& DynamicIndex<EC>::sync() DynamicIndex<EC>& DynamicIndex<EC>::sync()
{ {
assert(mIvecInit);
size_t sv = 1; size_t sv = 1;
IB::mPos = 0; IB::mPos = 0;
for(size_t i = 0; i != mIVec.size(); ++i){ for(size_t i = 0; i != mIVec.size(); ++i){
@ -217,6 +224,7 @@ namespace MultiArrayTools
template <class EC> template <class EC>
DynamicIndex<EC>& DynamicIndex<EC>::operator()(const IVecT& ivec) DynamicIndex<EC>& DynamicIndex<EC>::operator()(const IVecT& ivec)
{ {
mIvecInit = true;
mIVec = ivec; mIVec = ivec;
sync(); sync();
return *this; return *this;
@ -225,6 +233,7 @@ namespace MultiArrayTools
template <class EC> template <class EC>
DynamicIndex<EC>& DynamicIndex<EC>::operator()(const vector<std::shared_ptr<IndexW<EC>>>& ivec) DynamicIndex<EC>& DynamicIndex<EC>::operator()(const vector<std::shared_ptr<IndexW<EC>>>& ivec)
{ {
mIvecInit = true;
assert(mIVec.size() == ivec.size()); assert(mIVec.size() == ivec.size());
for(size_t i = 0; i != mIVec.size(); ++i){ for(size_t i = 0; i != mIVec.size(); ++i){
mIVec[i].first = ivec[i]; mIVec[i].first = ivec[i];
@ -237,6 +246,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
DynamicIndex<EC>& DynamicIndex<EC>::operator()(const std::shared_ptr<Indices>&... is) DynamicIndex<EC>& DynamicIndex<EC>::operator()(const std::shared_ptr<Indices>&... is)
{ {
mIvecInit = true;
vector<std::shared_ptr<IndexW<EC>>> tmp = vector<std::shared_ptr<IndexW<EC>>> tmp =
{ std::make_shared<IndexWrapper<Indices,EC>>(is)... }; { std::make_shared<IndexWrapper<Indices,EC>>(is)... };
@ -320,6 +330,7 @@ namespace MultiArrayTools
template <class EC> template <class EC>
const IndexW<EC>& DynamicIndex<EC>::get(size_t n) const const IndexW<EC>& DynamicIndex<EC>::get(size_t n) const
{ {
assert(mIvecInit);
return *mIVec[n].first; return *mIVec[n].first;
} }
@ -439,7 +450,7 @@ namespace MultiArrayTools
template <class EC> template <class EC>
typename DynamicRange<EC>::MetaType DynamicRange<EC>::get(size_t pos) const typename DynamicRange<EC>::MetaType DynamicRange<EC>::get(size_t pos) const
{ {
vector<char> out(cmeta(nullptr,0)); vector<char> out(cmetaSize());
cmeta(out.data(),pos); cmeta(out.data(),pos);
return out; return out;
} }
@ -475,22 +486,42 @@ namespace MultiArrayTools
} }
template <class EC> template <class EC>
size_t DynamicRange<EC>::typeNum() const vector<size_t> DynamicRange<EC>::typeNum() const
{ {
return 0; vector<size_t> o;
for(auto& x: mOrig){
auto tn = x->typeNum();
o.insert(o.end(), tn.begin(), tn.end());
}
return o;
} }
template <class EC> template <class EC>
size_t DynamicRange<EC>::cmeta(char* target, size_t pos) const size_t DynamicRange<EC>::cmeta(char* target, size_t pos) const
{ {
size_t out = 0; size_t out = 0;
size_t off = cmetaSize();
for(size_t i = mOrig.size(); i != 0; --i) { for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1]; auto& x = mOrig[i-1];
const size_t redpos = pos % x->size(); const size_t redpos = pos % x->size();
const size_t offset = x->cmeta(target+out,redpos); const size_t s = x->cmetaSize();
out += offset; out += s;
off -= s;
pos -= redpos; pos -= redpos;
pos /= x->size(); pos /= x->size();
x->cmeta(target+off,redpos);
}
assert(off == 0);
return out;
}
template <class EC>
size_t DynamicRange<EC>::cmetaSize() const
{
size_t out = 0;
for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1];
out += x->cmetaSize();
} }
return out; return out;
} }

View file

@ -177,6 +177,7 @@ namespace MultiArrayTools
typedef vector<std::pair<std::shared_ptr<IndexW<EC>>,size_t>> IVecT; typedef vector<std::pair<std::shared_ptr<IndexW<EC>>,size_t>> IVecT;
IVecT mIVec; IVecT mIVec;
bool mIvecInit = false;
public: public:
typedef IndexInterface<DynamicIndex<EC>,vector<char>> IB; typedef IndexInterface<DynamicIndex<EC>,vector<char>> IB;
@ -322,8 +323,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -200,8 +200,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;
@ -584,15 +585,24 @@ namespace MultiArrayTools
} }
template <class... Ranges> template <class... Ranges>
size_t MultiRange<Ranges...>::typeNum() const vector<size_t> MultiRange<Ranges...>::typeNum() const
{ {
return 0; vector<size_t> o;
RPackNum<sizeof...(Ranges)-1>::getTypeNum(o,mSpace);
return o;
} }
template <class... Ranges> template <class... Ranges>
size_t MultiRange<Ranges...>::cmeta(char* target, size_t pos) const size_t MultiRange<Ranges...>::cmeta(char* target, size_t pos) const
{ {
return RPackNum<sizeof...(Ranges)-1>::getCMeta(target,pos,mSpace); const size_t off = cmetaSize();
return RPackNum<sizeof...(Ranges)-1>::getCMeta(target,pos,mSpace,off);
}
template <class... Ranges>
size_t MultiRange<Ranges...>::cmetaSize() const
{
return RPackNum<sizeof...(Ranges)-1>::getCMetaSize(mSpace);
} }
template <class... Ranges> template <class... Ranges>

View file

@ -80,8 +80,9 @@ namespace MultiArrayTools
std::intptr_t id() const; std::intptr_t id() const;
virtual size_t typeNum() const = 0; virtual vector<size_t> typeNum() const = 0;
virtual size_t cmeta(char* target, size_t pos) const = 0; virtual size_t cmeta(char* target, size_t pos) const = 0;
virtual size_t cmetaSize() const = 0;
virtual std::string stringMeta(size_t pos) const = 0; virtual std::string stringMeta(size_t pos) const = 0;
virtual vector<char> data() const = 0; // usefull when writing to files, etc... virtual vector<char> data() const = 0; // usefull when writing to files, etc...

View file

@ -42,8 +42,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -46,8 +46,9 @@ namespace MultiArrayTools
virtual size_t size() const final; virtual size_t size() const final;
virtual size_t dim() const final; virtual size_t dim() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -38,8 +38,9 @@ namespace MultiArrayTools
virtual size_t size() const final; virtual size_t size() const final;
virtual size_t dim() const final; virtual size_t dim() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -39,8 +39,9 @@ namespace MultiArrayTools
virtual size_t size() const final; virtual size_t size() const final;
virtual size_t dim() const final; virtual size_t dim() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;

View file

@ -371,13 +371,33 @@ namespace MultiArrayHelper
} }
template <class... Ranges> template <class... Ranges>
static inline size_t getCMeta(char* target, size_t pos, const std::tuple<std::shared_ptr<Ranges>...>& stp) static inline size_t getCMeta(char* target, size_t pos, const std::tuple<std::shared_ptr<Ranges>...>& stp,
size_t off)
{
//constexpr size_t NN = sizeof...(Ranges);
auto& r = *std::get<N>(stp);
const size_t ownPos = pos % r.size();
const size_t s = r.cmetaSize();
off -= s;
r.cmeta(target+off,ownPos);
return s + RPackNum<N-1>::getCMeta(target, (pos - ownPos) / r.size(), stp, off);
}
template <class... Ranges>
static inline size_t getCMetaSize(const std::tuple<std::shared_ptr<Ranges>...>& stp)
{ {
constexpr size_t NN = sizeof...(Ranges); constexpr size_t NN = sizeof...(Ranges);
auto& r = *std::get<NN-N-1>(stp); auto& r = *std::get<NN-N-1>(stp);
const size_t ownPos = pos % r.size(); return r.cmetaSize() + RPackNum<N-1>::getCMetaSize(stp);
const size_t offset = r.cmeta(target,ownPos); }
return offset + RPackNum<N-1>::getCMeta(target + offset , (pos - ownPos) / r.size(), stp);
template <class... Ranges>
static inline void getTypeNum(vector<size_t>& res, const std::tuple<std::shared_ptr<Ranges>...>& stp)
{
auto& r = *std::get<N>(stp);
auto tn = r.typeNum();
res.insert(res.begin(), tn.begin(), tn.end());
RPackNum<N-1>::getTypeNum(res, stp);
} }
}; };
@ -649,12 +669,33 @@ namespace MultiArrayHelper
} }
template <class... Ranges> template <class... Ranges>
static inline size_t getCMeta(char* target, size_t pos, const std::tuple<std::shared_ptr<Ranges>...>& stp) static inline size_t getCMeta(char* target, size_t pos, const std::tuple<std::shared_ptr<Ranges>...>& stp,
size_t off)
{
//constexpr size_t NN = sizeof...(Ranges);
auto& r = *std::get<0>(stp);
const size_t ownPos = pos % r.size();
const size_t s = r.cmetaSize();
off -= s;
assert(off == 0);
r.cmeta(target,ownPos);
return s;
}
template <class... Ranges>
static inline size_t getCMetaSize(const std::tuple<std::shared_ptr<Ranges>...>& stp)
{ {
constexpr size_t NN = sizeof...(Ranges); constexpr size_t NN = sizeof...(Ranges);
auto& r = *std::get<NN-1>(stp); auto& r = *std::get<NN-1>(stp);
const size_t ownPos = pos % r.size(); return r.cmetaSize();
return r.cmeta(target,ownPos); }
template <class... Ranges>
static inline void getTypeNum(vector<size_t>& res, const std::tuple<std::shared_ptr<Ranges>...>& stp)
{
auto& r = *std::get<0>(stp);
auto tn = r.typeNum();
res.insert(res.begin(), tn.begin(), tn.end());
} }
}; };

View file

@ -232,8 +232,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;
@ -582,17 +583,21 @@ namespace MultiArrayTools
} }
template <typename U, SpaceType TYPE, size_t S> template <typename U, SpaceType TYPE, size_t S>
size_t GenSingleRange<U,TYPE,S>::typeNum() const vector<size_t> GenSingleRange<U,TYPE,S>::typeNum() const
{ {
return NumTypeMap<U>::num; return {NumTypeMap<U>::num};
} }
template <typename U, SpaceType TYPE, size_t S> template <typename U, SpaceType TYPE, size_t S>
size_t GenSingleRange<U,TYPE,S>::cmeta(char* target, size_t pos) const size_t GenSingleRange<U,TYPE,S>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<U*>(target) = mSpace[pos]; *reinterpret_cast<U*>(target) = mSpace[pos];
return sizeof(U);
} }
template <typename U, SpaceType TYPE, size_t S>
size_t GenSingleRange<U,TYPE,S>::cmetaSize() const
{
return sizeof(U); return sizeof(U);
} }

View file

@ -120,8 +120,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;
@ -404,7 +405,7 @@ namespace MultiArrayTools
} }
template <class Range> template <class Range>
size_t SubRange<Range>::typeNum() const vector<size_t> SubRange<Range>::typeNum() const
{ {
return mFullRange->typeNum(); return mFullRange->typeNum();
} }
@ -415,6 +416,12 @@ namespace MultiArrayTools
return mFullRange->cmeta(target, mSubSet[pos]); return mFullRange->cmeta(target, mSubSet[pos]);
} }
template <class Range>
size_t SubRange<Range>::cmetaSize() const
{
return mFullRange->cmetaSize();
}
template <class Range> template <class Range>
std::string SubRange<Range>::stringMeta(size_t pos) const std::string SubRange<Range>::stringMeta(size_t pos) const
{ {

View file

@ -16,6 +16,7 @@ namespace MultiArrayTools
struct TypeMap struct TypeMap
{ {
typedef void type; typedef void type;
static constexpr size_t size = 0;
}; };
template <typename T> template <typename T>
@ -85,7 +86,7 @@ namespace MultiArrayTools
#define XCOMMAX() , #define XCOMMAX() ,
#define include_type(t,n) template <> struct TypeMap<n> { typedef t type; };\ #define include_type(t,n) template <> struct TypeMap<n> { typedef t type; static constexpr size_t size = sizeof(t); }; \
template <> struct NumTypeMap<t> { static constexpr size_t num = n; }; template <> struct NumTypeMap<t> { static constexpr size_t num = n; };
include_type(size_t,1) include_type(size_t,1)
@ -110,11 +111,26 @@ namespace MultiArrayTools
include_type(std::array<int XCOMMAX() 9>,902) include_type(std::array<int XCOMMAX() 9>,902)
#undef include_type #undef include_type
inline size_t sizeFromTypeNum(size_t tn)
{
if(tn == 1){ return sizeof(size_t); }
else if(tn == 2){ return sizeof(int); }
else if(tn == 3){ return sizeof(char); }
else if(tn == 4){ return sizeof(float); }
else if(tn == 5){ return sizeof(double); }
else if(tn == 6){ return sizeof(std::string); }
else if(tn > 6 and tn < 100){ assert(0); }
else if(tn >= 100 and tn < 200) { return sizeof(std::vector<char>); }
else { const size_t nx = tn % 100; return sizeFromTypeNum(nx)*(tn-nx)/100; }
} }
}
#endif #endif
#undef XCOMMAX #undef XCOMMAX
#define register_all_types \ #define register_all_types \
register_type(1) \ register_type(1) \
register_type(2) \ register_type(2) \

View file

@ -112,8 +112,9 @@ namespace MultiArrayTools
virtual SpaceType spaceType() const final; virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final; virtual DataHeader dataHeader() const final;
virtual size_t typeNum() const final; virtual vector<size_t> typeNum() const final;
virtual size_t cmeta(char* target, size_t pos) const final; virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final; virtual std::string stringMeta(size_t pos) const final;
virtual vector<char> data() const final; virtual vector<char> data() const final;
@ -352,17 +353,21 @@ namespace MultiArrayTools
} }
template <typename U> template <typename U>
size_t ValueRange<U>::typeNum() const vector<size_t> ValueRange<U>::typeNum() const
{ {
return NumTypeMap<U>::num; return {NumTypeMap<U>::num};
} }
template <typename U> template <typename U>
size_t ValueRange<U>::cmeta(char* target, size_t pos) const size_t ValueRange<U>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<U*>(target) = *mMeta; *reinterpret_cast<U*>(target) = *mMeta;
return sizeof(U);
} }
template <typename U>
size_t ValueRange<U>::cmetaSize() const
{
return sizeof(U); return sizeof(U);
} }

View file

@ -93,21 +93,39 @@ namespace MultiArrayTools
return mEmpty; return mEmpty;
} }
size_t AnonymousRange::typeNum() const vector<size_t> AnonymousRange::typeNum() const
{ {
return 0; vector<size_t> o;
for(auto& x: mOrig){
auto tn = x->typeNum();
o.insert(o.end(), tn.begin(), tn.end());
}
return o;
} }
size_t AnonymousRange::cmeta(char* target, size_t pos) const size_t AnonymousRange::cmeta(char* target, size_t pos) const
{ {
size_t out = 0; size_t out = 0;
size_t off = cmetaSize();
for(size_t i = mOrig.size(); i != 0; --i) { for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1]; auto& x = mOrig[i-1];
const size_t redpos = pos % x->size(); const size_t redpos = pos % x->size();
const size_t offset = x->cmeta(target+out, redpos); const size_t s = x->cmetaSize();
out += offset; out += s;
off -= s;
pos -= redpos; pos -= redpos;
pos /= x->size(); pos /= x->size();
x->cmeta(target+off,redpos);
}
return out;
}
size_t AnonymousRange::cmetaSize() const
{
size_t out = 0;
for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1];
out += x->cmetaSize();
} }
return out; return out;
} }

View file

@ -53,16 +53,19 @@ namespace MultiArrayTools
return SpaceType::NONE; return SpaceType::NONE;
} }
size_t GenSingleRange<size_t,SpaceType::NONE,-1>::typeNum() const vector<size_t> GenSingleRange<size_t,SpaceType::NONE,-1>::typeNum() const
{ {
return NumTypeMap<size_t>::num; return {NumTypeMap<size_t>::num};
} }
size_t GenSingleRange<size_t,SpaceType::NONE,-1>::cmeta(char* target, size_t pos) const size_t GenSingleRange<size_t,SpaceType::NONE,-1>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<size_t*>(target) = pos; *reinterpret_cast<size_t*>(target) = pos;
return sizeof(size_t);
} }
size_t GenSingleRange<size_t,SpaceType::NONE,-1>::cmetaSize() const
{
return sizeof(size_t); return sizeof(size_t);
} }

View file

@ -76,16 +76,19 @@ namespace MultiArrayTools
return SpaceType::NUL; return SpaceType::NUL;
} }
size_t GenSingleRange<size_t,SpaceType::NUL,0>::typeNum() const vector<size_t> GenSingleRange<size_t,SpaceType::NUL,0>::typeNum() const
{ {
return NumTypeMap<size_t>::num; return {NumTypeMap<size_t>::num};
} }
size_t GenSingleRange<size_t,SpaceType::NUL,0>::cmeta(char* target, size_t pos) const size_t GenSingleRange<size_t,SpaceType::NUL,0>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<size_t*>(target) = 0; *reinterpret_cast<size_t*>(target) = 0;
return sizeof(size_t);
} }
size_t GenSingleRange<size_t,SpaceType::NUL,0>::cmetaSize() const
{
return sizeof(size_t); return sizeof(size_t);
} }

View file

@ -59,16 +59,19 @@ namespace MultiArrayTools
return SpaceType::PSPACE; return SpaceType::PSPACE;
} }
size_t GenSingleRange<int,SpaceType::PSPACE,-1>::typeNum() const vector<size_t> GenSingleRange<int,SpaceType::PSPACE,-1>::typeNum() const
{ {
return NumTypeMap<int>::num; return {NumTypeMap<int>::num};
} }
size_t GenSingleRange<int,SpaceType::PSPACE,-1>::cmeta(char* target, size_t pos) const size_t GenSingleRange<int,SpaceType::PSPACE,-1>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<int*>(target) = get(pos); *reinterpret_cast<int*>(target) = get(pos);
return sizeof(int);
} }
size_t GenSingleRange<int,SpaceType::PSPACE,-1>::cmetaSize() const
{
return sizeof(int); return sizeof(int);
} }

View file

@ -56,16 +56,19 @@ namespace MultiArrayTools
return SpaceType::SPIN; return SpaceType::SPIN;
} }
size_t GenSingleRange<size_t,SpaceType::SPIN,4>::typeNum() const vector<size_t> GenSingleRange<size_t,SpaceType::SPIN,4>::typeNum() const
{ {
return NumTypeMap<size_t>::num; return {NumTypeMap<size_t>::num};
} }
size_t GenSingleRange<size_t,SpaceType::SPIN,4>::cmeta(char* target, size_t pos) const size_t GenSingleRange<size_t,SpaceType::SPIN,4>::cmeta(char* target, size_t pos) const
{ {
if(target){
*reinterpret_cast<size_t*>(target) = pos; *reinterpret_cast<size_t*>(target) = pos;
return sizeof(size_t);
} }
size_t GenSingleRange<size_t,SpaceType::SPIN,4>::cmetaSize() const
{
return sizeof(size_t); return sizeof(size_t);
} }

View file

@ -5,7 +5,8 @@
#include <iostream> #include <iostream>
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "expressions.h"
#include "ranges/dynamic_range.cc.h"
//#include "multi_array_header.h" //#include "multi_array_header.h"
namespace MAT = MultiArrayTools; namespace MAT = MultiArrayTools;
@ -35,6 +36,44 @@ namespace {
return std::make_tuple(ts...); return std::make_tuple(ts...);
} }
class DynIndexTest : public ::testing::Test
{
protected:
typedef DynamicRangeFactory<Expressions1> DRF;
typedef DRF::oType DR;
typedef SingleRangeFactory<char,SpaceType::ANY> SRF;
typedef SRF::oType SRange;
typedef MultiRangeFactory<SRange,SRange,SRange> M3RF;
typedef M3RF::oType M3Range;
DynIndexTest()
{
SRF srf({'e', 'b', 'n'});
sr = std::dynamic_pointer_cast<SRange>( srf.create() );
SRF srf1({'a', 'b'});
SRF srf2({'1'});
SRF srf3({'0', '7'});
auto sr1 = std::dynamic_pointer_cast<SRange>( srf1.create() );
auto sr2 = std::dynamic_pointer_cast<SRange>( srf2.create() );
auto sr3 = std::dynamic_pointer_cast<SRange>( srf3.create() );
M3RF mrf(sr1,sr2,sr3);
mr = std::dynamic_pointer_cast<M3Range>( mrf.create() );
vector<std::shared_ptr<RangeBase>> rv(2);
rv[0] = sr;
rv[1] = mr;
DRF drf(rv);
dr = std::dynamic_pointer_cast<DR>( drf.create() );
}
std::shared_ptr<DR> dr;
std::shared_ptr<SRange> sr;
std::shared_ptr<M3Range> mr;
};
class IndexTest : public ::testing::Test class IndexTest : public ::testing::Test
{ {
protected: protected:
@ -212,6 +251,28 @@ namespace {
EXPECT_EQ(ci2().pos(), ci2.max()-1); EXPECT_EQ(ci2().pos(), ci2.max()-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<char>({'e','a','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'e','a','1','7'}));
EXPECT_EQ((++j).meta(), vector<char>({'e','b','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'e','b','1','7'}));
EXPECT_EQ((++j).meta(), vector<char>({'b','a','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'b','a','1','7'}));
EXPECT_EQ((++j).meta(), vector<char>({'b','b','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'b','b','1','7'}));
EXPECT_EQ((++j).meta(), vector<char>({'n','a','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'n','a','1','7'}));
EXPECT_EQ((++j).meta(), vector<char>({'n','b','1','0'}));
EXPECT_EQ((++j).meta(), vector<char>({'n','b','1','7'}));
}
} // end namespace } // end namespace
int main(int argc, char** argv) int main(int argc, char** argv)