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