43 #ifndef DOMI_MDREVITERATOR_HPP
44 #define DOMI_MDREVITERATOR_HPP
50 #include "Domi_Exceptions.hpp"
51 #include "Domi_Utils.hpp"
97 template<
class MDARRAY >
98 class MDRevIterator :
public std::iterator< std::bidirectional_iterator_tag,
99 typename MDARRAY::value_type >
131 bool end_index =
false);
145 const Teuchos::ArrayView< dim_type > &
index);
205 inline dim_type
index(
int axis)
const;
209 template<
typename T2 >
210 friend std::ostream & operator<<(std::ostream & os, const MDRevIterator< T2 > & a);
216 const Teuchos::Array< dim_type > _dimensions;
220 const Teuchos::Array< size_type > _strides;
231 Teuchos::Array< dim_type > _index;
242 void assign_begin_index();
249 void assign_end_index();
252 void assert_index(dim_type i,
int axis)
const;
260 template<
class MDARRAY >
263 _dimensions(mdarray._dimensions),
264 _strides(mdarray._strides),
266 _layout(mdarray._layout),
267 _index(mdarray.numDims())
273 if (computeSize(_dimensions) == 0)
276 assign_begin_index();
282 template<
class MDARRAY >
285 const Teuchos::ArrayView< dim_type > & index) :
286 _dimensions(mdarray._dimensions),
287 _strides(mdarray._strides),
289 _layout(mdarray._layout),
292 TEUCHOS_TEST_FOR_EXCEPTION(
293 (_dimensions.size() != _index.size()),
RangeError,
294 "Input array has " << _dimensions.size() <<
" dimensions, while index "
295 "has " << _index.size());
296 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
297 for (_axis = 0; _axis < _index.size(); ++_axis)
298 assert_index(_index[_axis], _axis);
304 template<
class MDARRAY >
307 _dimensions(source._dimensions),
308 _strides(source._strides),
310 _layout(source._layout),
311 _index(source._index)
317 template<
class MDARRAY >
324 template<
class MDARRAY >
328 _dimensions = source._dimensions;
329 _strides = source._strides;
331 _layout = source._layout;
332 _index = source._index;
338 template<
class MDARRAY >
344 if (_ptr != other._ptr)
return false;
346 for (_axis = 0; _axis < _index.size(); _axis++)
347 if (_index[_axis] != other._index[_axis])
return false;
354 template<
class MDARRAY >
359 return !(*
this == other);
364 template<
class MDARRAY >
368 size_type offset = 0;
369 for (_axis=0; _axis < _index.size(); ++_axis)
370 offset += _index[_axis] * _strides[_axis];
376 template<
class MDARRAY >
385 template<
class MDARRAY >
389 if (_layout == FIRST_INDEX_FASTEST)
396 _done = (_index[_axis] >= 0);
399 _index[_axis] = _dimensions[_axis] - 1;
401 if (_axis >= _index.size())
411 _axis = _dimensions.size() - 1;
416 _done = (_index[_axis] >= 0);
419 _index[_axis] = _dimensions[_axis] - 1;
434 template<
class MDARRAY >
445 template<
class MDARRAY >
449 if (_layout == FIRST_INDEX_FASTEST)
456 _done = (_index[_axis] < _dimensions[_axis]);
461 if (_axis >= _index.size())
471 _axis = _dimensions.size() - 1;
476 _done = (_index[_axis] < _dimensions[_axis]);
494 template<
class MDARRAY >
505 template<
class MDARRAY >
515 template<
typename T >
516 std::ostream & operator<<(std::ostream & os, const MDRevIterator< T > & a)
526 template<
class MDARRAY >
528 MDRevIterator< MDARRAY >::assign_begin_index()
531 axis < _index.size(); ++axis)
532 _index[axis] = _dimensions[axis] - 1;
537 template<
class MDARRAY >
539 MDRevIterator< MDARRAY >::assign_end_index()
543 axis < _index.size(); ++axis)
549 template<
class MDARRAY >
551 MDRevIterator< MDARRAY >::
552 assert_index(dim_type i,
555 TEUCHOS_TEST_FOR_EXCEPTION(
556 !(0 <= i && i < _dimensions[axis]), RangeError,
557 "MDRevIterator<MDARRAY>::assert_index(i=" << i <<
",axis=" << axis <<
"): out"
558 <<
" of range i in [0, " << _dimensions[axis] <<
")"
MDRevIterator< MDARRAY > & operator--()
Prefix decrement operator.
Definition: Domi_MDRevIterator.hpp:447
MDARRAY::pointer pointer
Pointer type.
Definition: Domi_MDRevIterator.hpp:110
MDRevIterator< MDARRAY > & operator=(const MDRevIterator< MDARRAY > &source)
Assignment operator.
Definition: Domi_MDRevIterator.hpp:326
Range Error exception type.
Definition: Domi_Exceptions.hpp:65
MDRevIterator< MDARRAY > & operator++()
Prefix increment operator.
Definition: Domi_MDRevIterator.hpp:387
MDARRAY::value_type value_type
Value type.
Definition: Domi_MDRevIterator.hpp:107
bool operator!=(const MDRevIterator< MDARRAY > &other) const
Inequality operator.
Definition: Domi_MDRevIterator.hpp:357
MDRevIterator(const MDARRAY &mdarray, bool end_index=false)
MDRevIterator constructor.
Definition: Domi_MDRevIterator.hpp:261
value_type & operator*()
Dereferencing operator.
Definition: Domi_MDRevIterator.hpp:366
dim_type index(int axis) const
Return the current index value along the given axis.
Definition: Domi_MDRevIterator.hpp:508
~MDRevIterator()
Destructor.
Definition: Domi_MDRevIterator.hpp:318
Reverse iterator class suitable for multi-dimensional arrays.
Definition: Domi_MDRevIterator.hpp:98
pointer operator->() const
Dereferencing arrow operator.
Definition: Domi_MDRevIterator.hpp:378
bool operator==(const MDRevIterator< MDARRAY > &other) const
Equality operator.
Definition: Domi_MDRevIterator.hpp:341