shift template recursion methods in a single struct 'PackNum<N>'

This commit is contained in:
Christian Zimmermann 2017-07-26 16:45:09 +02:00
parent c13c51bd51
commit 5f71b5055d
2 changed files with 207 additions and 479 deletions

View file

@ -9,213 +9,69 @@ namespace MultiArrayTools
namespace namespace
{ {
template <size_t N> template <size_t N>
struct IndexGetter struct PackNum
{ {
template <class MultiIndex> template <class MultiIndex>
static IndefinitIndexBase& getIndex(MultiIndex& in, size_t n) static IndexBase& getIndex(MultiIndex& in, size_t n)
{ {
if(n == N){ if(n == N){
return in.getIndex<N>(); return in.getIndex<N>();
} }
else { else {
return IndexGetter<N-1>::getIndex(in, n); return PackNum<N-1>::getIndex(in, n);
} }
} }
template <class MultiIndex> template <class MultiIndex>
static const IndefinitIndexBase& getIndex(const MultiIndex& in, size_t n) static const IndexBase& getIndex(const MultiIndex& in, size_t n)
{ {
if(n == N){ if(n == N){
return in.getIndex<N>(); return in.getIndex<N>();
} }
else { else {
return IndexGetter<N-1>::getIndex(in, n); return PackNum<N-1>::getIndex(in, n);
}
}
};
template <>
struct IndexGetter<0>
{
template <class MultiIndex>
static IndefinitIndexBase& getIndex(MultiIndex& in, size_t n)
{
return in.getIndex<0>();
}
template <class MultiIndex>
static const IndefinitIndexBase& getIndex(const MultiIndex& in, size_t n)
{
return in.getIndex<0>();
}
};
template <size_t N>
struct Evaluation
{
template <class MultiIndex>
static size_t evaluate(const MultiIndex& index)
{
const auto& subIndex = index.template getIndex<N>();
return Evaluation<N-1>::evaluate(index) * subIndex.max() + subIndex.pos();
}
};
template <>
struct Evaluation<0>
{
template <class MultiIndex>
static size_t evaluate(const MultiIndex& index)
{
const auto& subIndex = index.template getIndex<0>();
return subIndex.pos();
}
};
// rewrite !!
template <size_t DIGIT>
struct SubIteration
{
// use 'plus' as few as possible !!
template <class MultiIndex>
static inline void plus(MultiIndex& index, int num)
{
auto& si = index.template getIndex<DIGIT>();
si.setPosRel(num, &index);
size_t oor = si.outOfRange();
while(oor > 0){
SubIteration<DIGIT-1>::pp(index);
SubIteration<DIGIT>::plus(index, -si.max());
}
while(oor < 0){
SubIteration<DIGIT-1>::mm(index);
SubIteration<DIGIT>::plus(index, si.max());
} }
} }
template <class MultiIndex> template <class... Indices>
static inline void pp(MultiIndex& index) static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{ {
auto& si = index.template getIndex<DIGIT>(); auto& si = *std::get<N>(ip);
if(si.pos() == si.atEdge()){ if(si.pos() == si.last()){
si.setPos(0, &index); si = 0;
SubIteration<DIGIT-1>::pp(index); PackNum<N-1>::pp(index);
} }
else { else {
si.setPosRel(1, &index); ++si;
} }
} }
template <class MultiIndex> template <class... Indices>
static inline void mm(MultiIndex& index) static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{ {
auto& si = index.template getIndex<DIGIT>(); auto& si = *std::get<N>(ip);
if(si.pos() == si.atEdge()){ if(si.pos() == si.atEdge()){
si.setPos(si.atEdge(), &index); si = si.max();
SubIteration<DIGIT-1>::mm(index); PackNum<N-1>::mm(index);
} }
else { else {
si.setPosRel(1, &index); --si;
} }
} }
};
template <>
struct SubIteration<0>
{
template <class MultiIndex>
static inline void plus(MultiIndex& index, int num)
{
auto& si = index.template getIndex<0>();
si.setPosRel(num, &index);
}
template <class MultiIndex>
static inline void pp(MultiIndex& index)
{
auto& si = index.template getIndex<0>();
si.setPosRel(1, &index);
}
template <class MultiIndex>
static inline void mm(MultiIndex& index)
{
auto& si = index.template getIndex<0>();
si.setPosRel(-1, &index);
}
};
template <size_t N>
struct IndexSubOrder
{
template <class IndexPack>
static void subOrd(IndexPack& iPack, IndefinitIndexBase* major)
{
std::get<N>(iPack).subOrd(major);
IndexSubOrder<N-1>::subOrd(iPack, major);
}
};
template <>
struct IndexSubOrder<0>
{
template <class IndexPack>
static void subOrd(IndexPack& iPack, IndefinitIndexBase* major)
{
std::get<0>(iPack).subOrd(major);
}
};
template <size_t N>
struct PositionCopy
{
template <class MultiIndex>
static void copyPos(MultiIndex& target, const MultiIndex& source)
{
target.template getIndex<N>().copyPos( source.template getIndex<N>() );
PositionCopy<N-1>::copyPos(target, source);
}
};
template <>
struct PositionCopy<0>
{
template <class MultiIndex>
static void copyPos(MultiIndex& target, const MultiIndex& source)
{
target.template getIndex<0>().copyPos( source.template getIndex<0>() );
}
};
template <size_t N>
struct TupleSize
{
template <class RangeTuple> template <class RangeTuple>
static size_t getSize(const RangeTuple& rt) static size_t getSize(const RangeTuple& rt)
{ {
return std::get<N>(rt).size() * TupleSize<N-1>::getSize(rt); return std::get<N>(rt).size() * PackNum<N-1>::getSize(rt);
} }
};
template <>
struct TupleSize<0>
{
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<0>(rt).size();
}
};
template <size_t N>
struct IndexSetter
{
template <class... Ranges> template <class... Ranges>
static void setBegin(MultiRange<Ranges...>::IndexType& i, const MultiRange<Ranges...>::SpaceType& r) static void setBegin(MultiRange<Ranges...>::IndexType& i, const MultiRange<Ranges...>::SpaceType& r)
{ {
*std::get<N>(i) = std::get<N>(r)->begin(); *std::get<N>(i) = std::get<N>(r)->begin();
IndexSetter<N-1>::setBegin(i,r); PackNum<N-1>::setBegin(i,r);
} }
template <class... Ranges> template <class... Ranges>
@ -223,13 +79,130 @@ namespace MultiArrayTools
{ {
*std::get<N>(i) = std::get<N>(r)->end(); *std::get<N>(i) = std::get<N>(r)->end();
*std::get<N>(i) -= 1; *std::get<N>(i) -= 1;
IndexSetter<N-1>::setEnd(i,r); PackNum<N-1>::setEnd(i,r);
} }
template <class... Ranges>
static void buildRangeVec(std::vector<MultiRangeType>& rvec, const MultiRange<Ranges...>::SpaceType& rs)
{
rvec.push_back(std::get<sizeof...(Ranges)-N-1>(rs)->type());
PackNum<N-1>::buildRangeVec(rvec, rs);
}
template <class... Indices>
static void getMetaPos(std::tuple<decltype(Indices().meta())...>& target,
const typename MultiIndex<Indices...>::IndexPack& source)
{
std::get<N>(target) = std::get<N>(source)->meta();
PackNum<N-1>::getMetaPos(target, source);
}
template <class... Indices>
static void setMeta(typename MultiIndex<Indices...>::IndexPack& target,
const typename MultiIndex<Indices...>::MetaType& source)
{
std::get<N>(target).atMeta( std::get<N>(source) );
PackNum<N-1>::setMeta(target, source);
}
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto i = std::get<N>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
if(ownPos == pos){
PackNum<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
}
template <class IndexPack, class... Indices>
static auto setFromPointerList(IndexPack& ipack,
std::vector<std::shared_ptr<IndefinitIndexBase> >& ptrList,
std::shared_ptr<Indices>&... inds)
-> decltype(PackNum<N-1>::setFromPointerList(ipack,
std::shared_ptr<decltype(std::get<N>(ipack))>,
newPtr, inds...))
{
typedef std::shared_ptr<decltype(std::get<N>(ipack))> NewIndexPtrType;
NewIndexPtrType newPtr = dynamic_pointer_cast<NewIndexPtrType>(ptrList.at(N));
return PackNum<N-1>::setFromPointerList(ipack, ptrList, newPtr, inds...);
}
template <class MRange, class... Indices>
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef decltype(range.template get<N>()) SubIndexType;
typedef decltype(std::get<N>(ip).get()) TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types");
std::get<N>(ip).swap( std::make_shared<SubIndexType>( range.template get<N>() ) );
PackNum<N-1>::construct(ip, range);
}
template <class... Indices>
static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const MultiIndex<Indices...>& ind)
{
typedef decltype(ind.template get<N>()) SubIndexType;
std::get<N>(ip).swap( std::make_shared<SubIndexType>( ind.template get<N>() ) );
PackNum<N-1>::copy(ip, ind);
}
template <class... Indices>
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
return std::get<N>(iPtrTup)->pos() +
PackNum<N-1>::makePos(iPtrTup) * std::get<N-1>(iPtrTup)->max();
}
template <typename... Ts>
static void print(std::ostream& os, const std::tuple<Ts...>& meta)
{
PackNum<N-1>::print(os, meta);
os << std::get<N>(meta) << '\t';
}
}; };
template<> template<>
struct IndexSetter<0> struct PackNum<0>
{ {
template <class MultiIndex>
static IndefinitIndexBase& getIndex(MultiIndex& in, size_t n)
{
return in.getIndex<0>();
}
template <class MultiIndex>
static const IndefinitIndexBase& getIndex(const MultiIndex& in, size_t n)
{
return in.getIndex<0>();
}
template <class MultiIndex>
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
++si;
}
template <class MultiIndex>
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
--si;
}
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<0>(rt).size();
}
template <class... Ranges> template <class... Ranges>
static void setBegin(MultiRange<Ranges...>::IndexType& i, const MultiRange<Ranges...>::SpaceType& r) static void setBegin(MultiRange<Ranges...>::IndexType& i, const MultiRange<Ranges...>::SpaceType& r)
{ {
@ -242,105 +215,27 @@ namespace MultiArrayTools
*std::get<0>(i) = std::get<0>(r)->end(); *std::get<0>(i) = std::get<0>(r)->end();
*std::get<0>(i) -= 1; *std::get<0>(i) -= 1;
} }
};
template <size_t N>
struct RangeVecBuilder
{
template <class... Ranges>
static void buildRangeVec(std::vector<MultiRangeType>& rvec, const MultiRange<Ranges...>::SpaceType& rs)
{
rvec.push_back(std::get<sizeof...(Ranges)-N-1>(rs)->type());
RangeVecBuilder<N-1>::buildRangeVec(rvec, rs);
}
};
template <>
struct RangeVecBuilder<0>
{
template <class... Ranges> template <class... Ranges>
static void buildRangeVec(std::vector<MultiRangeType>& rvec, const MultiRange<Ranges...>::SpaceType& rs) static void buildRangeVec(std::vector<MultiRangeType>& rvec, const MultiRange<Ranges...>::SpaceType& rs)
{ {
rvec.push_back(std::get<sizeof...(Ranges)-1>(rs)->type()); rvec.push_back(std::get<sizeof...(Ranges)-1>(rs)->type());
} }
};
// HERE !!!
template <size_t N>
struct MetaPosGetter
{
template <class... Indices> template <class... Indices>
static void getMetaPos(std::tuple<decltype(Indices().getMetaPos())...>& target, static void getMetaPos(std::tuple<decltype(Indices().meta())...>& target,
const typename MultiIndex<Indices...>::IndexPack& source) const typename MultiIndex<Indices...>::IndexPack& source)
{ {
std::get<N>(target) = std::get<N>(source)->getMetaPos(); std::get<0>(target) = std::get<0>(source)->meta();
MetaPosGetter<N-1>::getMetaPos(target, source);
} }
};
template <>
struct MetaPosGetter<0>
{
template <class... Indices>
static void getMetaPos(std::tuple<decltype(Indices().getMetaPos())...>& target,
const typename MultiIndex<Indices...>::IndexPack& source)
{
std::get<0>(target) = std::get<0>(source)->getMetaPos();
}
};
template <size_t N>
struct MetaSetter
{
template <class... Indices>
static void setMeta(typename MultiIndex<Indices...>::IndexPack& target,
const typename MultiIndex<Indices...>::MetaType& source)
{
std::get<N>(target).atMeta( std::get<N>(source) );
MetaSetter<N-1>::setMeta(target, source);
}
};
template <>
struct MetaSetter<0>
{
template <class... Indices> template <class... Indices>
static void setMeta(typename MultiIndex<Indices...>::IndexPack& target, static void setMeta(typename MultiIndex<Indices...>::IndexPack& target,
const typename MultiIndex<Indices...>::MetaType& source) const typename MultiIndex<Indices...>::MetaType& source)
{ {
std::get<0>(target).atMeta( std::get<0>(source) ); std::get<0>(target).atMeta( std::get<0>(source) );
} }
};
template <size_t N>
struct IndexPackSetter
{
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto i = std::get<N>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
if(ownPos == pos){
IndexPackSetter<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
}
template <class IndexPack, class... Indices>
static auto setFromPointerList(IndexPack& ipack,
std::vector<std::shared_ptr<IndefinitIndexBase> >& ptrList,
std::shared_ptr<Indices>&... inds)
-> decltype(IndexPackSetter<N-1>::setFromPointerList(ipack,
std::shared_ptr<decltype(std::get<N>(ipack))>,
newPtr, inds...))
{
typedef std::shared_ptr<decltype(std::get<N>(ipack))> NewIndexPtrType;
NewIndexPtrType newPtr = dynamic_pointer_cast<NewIndexPtrType>(ptrList.at(N));
return IndexPackSetter<N-1>::setFromPointerList(ipack, ptrList, newPtr, inds...);
}
};
template <>
struct IndexPackSetter<0>
{
template <class IndexPack> template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos) static void setIndexPack(IndexPack& iPack, size_t pos)
{ {
@ -360,98 +255,55 @@ namespace MultiArrayTools
return std::make_tuple(newPtr, inds...); return std::make_tuple(newPtr, inds...);
} }
};
template <size_t N>
struct SubIndexConstruct
{
template <class MRange, class... Indices> template <class MRange, class... Indices>
void construct(std::tuple<std::shared_ptr<Indices>...>& ip, static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef decltype(range.template get<N>()) SubIndexType;
typedef decltype(std::get<N>(ip).get()) wTypeFromIndexPack;
static_assert(is_same<SubIndexType,SubIndexType>::value,
"inconsiśtent types");
std::get<N>(ip).swap( std::make_shared<SubIndexType> ( range.template get<N>() ) );
SubIndexConstruct<N-1>::construct(ip, range);
}
template <class... Indices>
void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const MultiIndex<Indices...>& ind)
{
typedef decltype(ind.template get<N>()) SubIndexType;
std::get<N>(ip).swap( std::make_shared<SubIndexType>( ind.template get<N>() ) );
SubIndexConstruct<N-1>::copy(ip, ind);
}
};
template <>
struct SubIndexConstruct<0>
{
template <class MRange, class... Indices>
void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range) const MRange& range)
{ {
typedef decltype(range.template get<0>()) SubIndexType; typedef decltype(range.template get<0>()) SubIndexType;
typedef decltype(std::get<0>(ip).get()) wTypeFromIndexPack; typedef decltype(std::get<0>(ip).get()) TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,SubIndexType>::value, static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types"); "inconsiśtent types");
std::get<0>(ip).swap( std::make_shared<SubIndexType>( range.template get<0>() ) ); std::get<0>(ip).swap( std::make_shared<SubIndexType>( range.template get<0>() ) );
} }
template <class... Indices> template <class... Indices>
void copy(std::tuple<std::shared_ptr<Indices>...>& ip, static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const MultiIndex<Indices...>& ind) const MultiIndex<Indices...>& ind)
{ {
typedef decltype(ind.template get<0>()) SubIndexType; typedef decltype(ind.template get<0>()) SubIndexType;
std::get<0>(ip).swap( std::make_shared<SubIndexType>( ind.template get<0>() ) ); std::get<0>(ip).swap( std::make_shared<SubIndexType>( ind.template get<0>() ) );
} }
};
template <size_t N>
struct PosGetter
{
template <class... Indices> template <class... Indices>
size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup) static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
return std::get<N>(iPtrTup)->pos() +
PosGetter<N-1>::makePos(iPtrTup) * std::get<N-1>(iPtrTup)->max();
}
};
template <>
struct PosGetter<0>
{
template <class... Indices>
size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{ {
return std::get<0>(iPtrTup)->pos(); return std::get<0>(iPtrTup)->pos();
} }
};
template <typename... Ts>
static void print(std::ostream& os, const std::tuple<Ts...>& meta)
{
os << std::get<0>(meta) << '\t';
}
};
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) : MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) :
IndexInterface<std::tuple<decltype(Indices().meta())...> >(in) IndexInterface<std::tuple<decltype(Indices().meta())...> >(in)
{ {
SubIndexConstruct<sizeof...(Indices)-1>::copy(mIPack, in); PackNum<sizeof...(Indices)-1>::copy(mIPack, in);
mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack); mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in)
{ {
IndexI::operator=(in); IndexI::operator=(in);
SubIndexConstruct<sizeof...(Indices)-1>::copy(mIPack, in); PackNum<sizeof...(Indices)-1>::copy(mIPack, in);
mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack); mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }
@ -460,135 +312,69 @@ namespace MultiArrayTools
MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) : MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<std::tuple<decltype(Indices().meta())...> >(range, 0) IndexInterface<std::tuple<decltype(Indices().meta())...> >(range, 0)
{ {
SubIndexConstruct<sizeof...(Indices)-1>::construct(mIPack, *range); PackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
mPos = PosGetter<sizeof...(Indices)-1>::makePos(mIPack); mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++() MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{ {
SubIteration<sizeof...(Indices)-1>::pp(*this); PackNum<sizeof...(Indices)-1>::pp( mIPack );
//plus(*this, sizeof...(Indices)-1, 1); ++mPos;
IIB::setPosRel(1);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--() MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{ {
SubIteration<sizeof...(Indices)-1>::mm(*this); PackNum<sizeof...(Indices)-1>::mm( mIPack );
//plus(*this, sizeof...(Indices)-1, -1); --mPos;
IIB::setPosRel(-1);
return *this; return *this;
} }
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n)
{
SubIteration<sizeof...(Indices)-1>::plus(*this, n);
//plus(*this, sizeof...(Indices)-1, n);
IIB::setPosRel(n);
return *this;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n)
{
SubIteration<sizeof...(Indices)-1>::plus(*this, -n);
//plus(*this, sizeof...(Indices)-1, -n);
IIB::setPosRel(-n);
return *this;
}
template <class... Indices>
bool MultiIndex<Indices...>::operator==(const MultiIndex<Indices...>& in)
{
return IB::mRange == in.mRange and IIB::pos() == in.pos();
}
template <class... Indices>
bool MultiIndex<Indices...>::operator!=(const MultiIndex<Indices...>& in)
{
return IB::mRange != in.mRange or IIB::pos() != in.pos();
}
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(size_t pos) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(size_t pos)
{ {
IIB::setPos( pos ); mPos = pos;
IndexPackSetter<sizeof...(Indices)-1>::setIndexPack(mIPack, pos); PackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiRangeType MultiIndex<Indices...>::rangeType() const template <size_t DIR>
MultiIndex<Indices...>& MultiIndex<Indices...>::up()
{ {
return IB::mRange->type(); static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
} mPos += /*!!!*/;
PackNum<DIR>::pp( mIPack );
template <class... Indices>
size_t MultiIndex<Indices...>::evaluate(const MultiIndex<Indices...>& in) const
{
size_t res = Evaluation<sizeof...(Indices)-1>::evaluate(in);
return res;
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(Indices&&... inds)
{
mIPack = std::make_tuple(inds...);
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this);
IIB::mPos = evaluate(*this);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(const Indices&... inds) template <size_t DIR>
MultiIndex<Indices...>& MultiIndex<Indices...>::down()
{ {
mIPack = std::make_tuple(Indices(inds)...); static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IndexSubOrder<sizeof...(Indices)-1>::subOrd(mIPack, this); mPos -= /*!!!*/;
IIB::mPos = evaluate(*this); PackNum<DIR>::mm( mIPack );
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
size_t MultiIndex<Indices...>::dim() const size_t MultiIndex<Indices...>::dim() const
{ {
size_t res = 1; return sizeof...(Indices);
for(size_t i = 0; i != sizeof...(Indices); ++i){
res *= get(i).dim();
}
return res;
} }
template <class... Indices> template <class... Indices>
template <size_t N> template <size_t N>
auto getIndex() -> decltype(*std::get<N>(mIPack))& auto MultiIndex<Indices...>::get() const -> const decltype(*std::get<N>(mIPack))&
//typename std::tuple_element<N, std::tuple<Indices...> >::type& MultiIndex<Indices...>::getIndex()
{ {
return *std::get<N>(mIPack); return *std::get<N>(mIPack);
} }
template <class... Indices> template <class... Indices>
template <size_t N> const IndexBase& MultiIndex<Indices...>::get(size_t n) const
auto getIndex() const -> const decltype(*std::get<N>(mIPack))&
{
return *std::get<N>(mIPack);
}
template <class... Indices>
IndefinitIndexBase& MultiIndex<Indices...>::get(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
MultiIndex<Indices...>* t = this;
return IndexGetter<sizeof...(Indices)-1>::getIndex(*t, n);
}
template <class... Indices>
const IndefinitIndexBase& MultiIndex<Indices...>::get(size_t n) const
{ {
if(n >= sizeof...(Indices)){ if(n >= sizeof...(Indices)){
assert(0); assert(0);
@ -599,66 +385,20 @@ namespace MultiArrayTools
} }
template <class... Indices> template <class... Indices>
typename MultiIndex<Indices...>::MetaType MultiIndex<Indices...>::getMetaPos() const typename MultiIndex<Indices...>::MetaType MultiIndex<Indices...>::meta() const
{ {
MetaType metaTuple; MetaType metaTuple;
MetaPosGetter<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack); PackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple; return metaTuple;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::atMeta(const MultiIndex<Indices...>::MetaType& metaPos) MultiIndex<Indices...>& MultiIndex<Indices...>::at(const MultiIndex<Indices...>::MetaType& metaPos)
{ {
MetaSetter<sizeof...(Indices)-1>::setMeta(mIPack, metaPos); PackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
return *this; return *this;
} }
template <class... Indices>
void MultiIndex<Indices...>::copyPos(const MultiIndex<Indices...>& in)
{
PositionCopy<sizeof...(Indices)-1>::copyPos(*this, in);
}
template <class... Indices>
size_t MultiIndex<Indices...>::giveSubStepSize(IndefinitIndexBase* subIndex)
{
size_t sss = 1;
for(size_t i = sizeof...(Indices)-1; i != 0; --i){
IndefinitIndexBase* iibPtr = &get(i);
if(iibPtr == subIndex){
return sss;
}
sss *= iibPtr->max();
}
if(&get(0) == subIndex){
return sss;
}
else {
return 0;
}
}
template <size_t N>
struct TuplePrinter
{
template <typename... Ts>
static void print(std::ostream& os, const std::tuple<Ts...>& meta)
{
TuplePrinter<N-1>::print(os, meta);
os << std::get<N>(meta) << '\t';
}
};
template <>
struct TuplePrinter<0>
{
template <typename... Ts>
static void print(std::ostream& os, const std::tuple<Ts...>& meta)
{
os << std::get<0>(meta) << '\t';
}
};
/****************** /******************
* MultiRange * * MultiRange *
******************/ ******************/
@ -669,14 +409,6 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const SpaceType& space) : mSpace(space) {} MultiRange<Ranges...>::MultiRange(const SpaceType& space) : mSpace(space) {}
template <class... Ranges>
template <size_t N>
auto getRange() -> decltype(*std::get<N>(mSpace))&
//typename std::tuple_element<N, std::tuple<Ranges...> >::type& MultiRange<Ranges...>::getRange()
{
return *std::get<N>(mSpace);
}
template <class... Ranges> template <class... Ranges>
template <size_t N> template <size_t N>
auto getRange() const -> const decltype(*std::get<N>(mSpace))& auto getRange() const -> const decltype(*std::get<N>(mSpace))&
@ -688,14 +420,14 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
size_t MultiRange<Ranges...>::size() const size_t MultiRange<Ranges...>::size() const
{ {
return TupleSize<sizeof...(Ranges)-1>::getSize(mSpace); return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
} }
template <class... Ranges> template <class... Ranges>
MultiRangeType MultiRange<Ranges...>::type() const MultiRangeType MultiRange<Ranges...>::type() const
{ {
std::vector<MultiRangeType> rvec; std::vector<MultiRangeType> rvec;
RangeVecBuilder<sizeof...(Ranges)-1>::buildRangeVec(rvec, mSpace); PackNum<sizeof...(Ranges)-1>::buildRangeVec(rvec, mSpace);
return MultiRangeType(rvec); return MultiRangeType(rvec);
} }
@ -709,7 +441,7 @@ namespace MultiArrayTools
MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::begin() const MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::begin() const
{ {
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is; std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
IndexSetter<sizeof...(Ranges)-1>::setBegin(is,mSpace); PackNum<sizeof...(Ranges)-1>::setBegin(is,mSpace);
return MultiIndex<typename Ranges::IndexType...>(this, is); return MultiIndex<typename Ranges::IndexType...>(this, is);
} }
@ -717,7 +449,7 @@ namespace MultiArrayTools
MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::end() const MultiIndex<typename Ranges::IndexType...> MultiRange<Ranges...>::end() const
{ {
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is; std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
IndexSetter<sizeof...(Ranges)-1>::setEnd(is,mSpace); PackNum<sizeof...(Ranges)-1>::setEnd(is,mSpace);
return ++MultiIndex<typename Ranges::IndexType...>(this, is); return ++MultiIndex<typename Ranges::IndexType...>(this, is);
} }
@ -725,7 +457,7 @@ namespace MultiArrayTools
std::shared_ptr<IndefinitIndexBase> MultiRange<Ranges...>::indexInstance() const std::shared_ptr<IndefinitIndexBase> MultiRange<Ranges...>::indexInstance() const
{ {
std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is; std::tuple<std::shared_ptr<typename Ranges::IndexType>...> is;
IndexSetter<sizeof...(Ranges)-1>::setBegin(is,mSpace); PackNum<sizeof...(Ranges)-1>::setBegin(is,mSpace);
std::shared_ptr<IndefinitIndexBase> sptr(new MultiIndex<typename Ranges::IndexType...>(this, is)); std::shared_ptr<IndefinitIndexBase> sptr(new MultiIndex<typename Ranges::IndexType...>(this, is));
return sptr; return sptr;
} }
@ -735,6 +467,6 @@ template <typename... Ts>
std::ostream& operator<<(std::ostream& os, std::ostream& operator<<(std::ostream& os,
const std::tuple<Ts...>& meta) const std::tuple<Ts...>& meta)
{ {
MultiArrayTools::TuplePrinter<sizeof...(Ts)-1>::print(os, meta); MultiArrayTools::PackNum<sizeof...(Ts)-1>::print(os, meta);
return os; return os;
} }

View file

@ -23,13 +23,11 @@ namespace MultiArrayTools
typedef std::tuple<decltype(Indices().meta())...> MetaType; typedef std::tuple<decltype(Indices().meta())...> MetaType;
typedef IndexInterface<MetaType> IndexI; typedef IndexInterface<MetaType> IndexI;
public: public:
MultiIndex() = default; MultiIndex() = default;
// NO DEFAULT HERE !!! // NO DEFAULT HERE !!!
// ( have to subord sub-indices (mMajor) correctly, and not only copy their mMajor pointer to 'in' // ( have to assign sub-indices (ptr!) correctly )
// which is not major any more in copies!! )
MultiIndex(const MultiIndex& in); MultiIndex(const MultiIndex& in);
MultiIndex& operator=(const MultiIndex& in); MultiIndex& operator=(const MultiIndex& in);
@ -38,24 +36,22 @@ namespace MultiArrayTools
virtual MultiIndex& operator++() override; virtual MultiIndex& operator++() override;
virtual MultiIndex& operator--() override; virtual MultiIndex& operator--() override;
virtual MultiIndex& operator+=(int n) override;
virtual MultiIndex& operator-=(int n) override;
virtual MultiIndex& operator=(size_t pos) override; virtual MultiIndex& operator=(size_t pos) override;
template <size_t DIR>
MultiIndex& up();
template <size_t DIR>
MultiIndex& down();
template <size_t N> template <size_t N>
auto get() const -> decltype(*std::get<N>(mIPack))&; auto get() const -> decltype(*std::get<N>(mIPack))&;
//typename std::tuple_element<N, std::tuple<std::shared_ptr<Indices>...> >::type const& getIndex() const;
const IndexBase& get(size_t n) const; const IndexBase& get(size_t n) const;
virtual MetaType meta() const override; virtual MetaType meta() const override;
MultiIndex& atMeta(const MetaType& metaPos); virtual MultiIndex& at(const MetaType& metaPos) override;
MultiIndex& operator()(Indices&&... inds);
MultiIndex& operator()(const Indices&... inds);
// dimension of MultiRange; includes ALL degrees of freedom
virtual size_t dim() const override; virtual size_t dim() const override;
protected: protected: