start refactoring plus functions

This commit is contained in:
Christian Zimmermann 2017-03-24 11:32:33 +01:00
parent b41f2e9f0f
commit 5318146710

View file

@ -74,24 +74,76 @@ namespace MultiArrayTools
}; };
// rewrite !! // rewrite !!
template <class MultiIndex> template <size_t DIGIT>
inline void plus(MultiIndex& index, size_t digit, int num) struct SubIteration
{ {
IndefinitIndexBase& si = index.get(digit); template <class MultiIndex>
si.setPosRel(num); static inline void plus(MultiIndex& index, int num)
//si.setPos( si.pos() + num ); {
size_t oor = si.outOfRange(); IndefinitIndexBase& si = index.get(DIGIT);
if(digit){ si.setPosRel(num);
size_t oor = si.outOfRange();
if(oor > 0){ if(oor > 0){
plus(index, digit - 1, 1); SubIteration<DIGIT-1>::pp(index);
plus(index, digit, -si.max()); SubIteration<DIGIT>::plus(index, -si.max());
} }
else if(oor < 0){ else if(oor < 0){
plus(index, digit - 1, -1); SubIteration<DIGIT-1>::mm(index);
plus(index, digit, si.max()); SubIteration<DIGIT>::plus(index, si.max());
} }
} }
}
template <class MultiIndex>
static inline void pp(MultiIndex& index)
{
IndefinitIndexBase& si = index.get(DIGIT);
si.setPosRel(1);
size_t oor = si.outOfRange();
if(oor > 0){
SubIteration<DIGIT-1>::pp(index);
// set pos to zero ??
SubIteration<DIGIT>::plus(index, -si.max()); // This can be done faster because we know the pos
}
}
template <class MultiIndex>
static inline void mm(MultiIndex& index)
{
IndefinitIndexBase& si = index.get(DIGIT);
si.setPosRel(-1);
size_t oor = si.outOfRange();
if(oor < 0){
SubIteration<DIGIT-1>::mm(index);
// or to max here ??
SubIteration<DIGIT>::plus(index, si.max()); // This can be done faster because we know the pos
}
}
};
template <>
struct SubIteration<0>
{
template <class MultiIndex>
static inline void plus(MultiIndex& index, int num)
{
IndefinitIndexBase& si = index.get(0);
si.setPosRel(num);
}
template <class MultiIndex>
static inline void pp(MultiIndex& index)
{
IndefinitIndexBase& si = index.get(0);
si.setPosRel(1);
}
template <class MultiIndex>
static inline void mm(MultiIndex& index)
{
IndefinitIndexBase& si = index.get(0);
si.setPosRel(-1);
}
};
template <size_t N> template <size_t N>
struct TupleNamer struct TupleNamer
@ -361,7 +413,8 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++() MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{ {
plus(*this, sizeof...(Indices)-1, 1); SubIteration<sizeof...(Indices)-1>::pp(*this);
//plus(*this, sizeof...(Indices)-1, 1);
IIB::setPosRel(1); IIB::setPosRel(1);
return *this; return *this;
} }
@ -369,7 +422,8 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--() MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{ {
plus(*this, sizeof...(Indices)-1, -1); SubIteration<sizeof...(Indices)-1>::mm(*this);
//plus(*this, sizeof...(Indices)-1, -1);
IIB::setPosRel(-1); IIB::setPosRel(-1);
return *this; return *this;
} }
@ -377,7 +431,8 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n) MultiIndex<Indices...>& MultiIndex<Indices...>::operator+=(int n)
{ {
plus(*this, sizeof...(Indices)-1, n); SubIteration<sizeof...(Indices)-1>::plus(*this, n);
//plus(*this, sizeof...(Indices)-1, n);
IIB::setPosRel(n); IIB::setPosRel(n);
return *this; return *this;
} }
@ -385,7 +440,8 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n) MultiIndex<Indices...>& MultiIndex<Indices...>::operator-=(int n)
{ {
plus(*this, sizeof...(Indices)-1, -n); SubIteration<sizeof...(Indices)-1>::plus(*this, -n);
//plus(*this, sizeof...(Indices)-1, -n);
IIB::setPosRel(-n); IIB::setPosRel(-n);
return *this; return *this;
} }