43 #ifndef DOMI_MDITERATOR_HPP
44 #define DOMI_MDITERATOR_HPP
50 #include "Domi_Exceptions.hpp"
51 #include "Domi_Utils.hpp"
99 template<
class MDARRAY >
100 class MDIterator :
public std::iterator< std::bidirectional_iterator_tag,
101 typename MDARRAY::value_type >
133 bool end_index =
false);
147 const Teuchos::ArrayView< dim_type > &
index);
206 inline dim_type
index(
int axis)
const;
210 template<
typename T2 >
211 friend std::ostream & operator<<(std::ostream & os, const MDIterator< T2 > & a);
217 const Teuchos::Array< dim_type > _dimensions;
221 const Teuchos::Array< size_type > _strides;
232 Teuchos::Array< dim_type > _index;
247 void assign_end_index();
250 void assert_index(dim_type i,
int axis)
const;
258 template<
class MDARRAY >
261 _dimensions(mdarray._dimensions),
262 _strides(mdarray._strides),
264 _layout(mdarray._layout),
265 _index(mdarray.numDims())
271 if (computeSize(_dimensions) == 0)
274 _index.assign(_dimensions.size(), 0);
280 template<
class MDARRAY >
283 const Teuchos::ArrayView< dim_type > & index) :
284 _dimensions(mdarray._dimensions),
285 _strides(mdarray._strides),
287 _layout(mdarray._layout),
290 TEUCHOS_TEST_FOR_EXCEPTION(
291 (_dimensions.size() != _index.size()),
RangeError,
292 "Input array has " << _dimensions.size() <<
" dimensions, while index "
293 "has " << _index.size());
294 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
295 for (_axis = 0; _axis < _index.size(); ++_axis)
296 assert_index(_index[_axis], _axis);
302 template<
class MDARRAY >
304 _dimensions(source._dimensions),
305 _strides(source._strides),
307 _layout(source._layout),
308 _index(source._index)
314 template<
class MDARRAY >
321 template<
class MDARRAY >
325 _dimensions = source._dimensions;
326 _strides = source._strides;
328 _layout = source._layout;
329 _index = source._index;
335 template<
class MDARRAY >
340 if (_ptr != other._ptr)
return false;
342 for (_axis = 0; _axis < _index.size(); _axis++)
343 if (_index[_axis] != other._index[_axis])
return false;
350 template<
class MDARRAY >
354 return !(*
this == other);
359 template<
class MDARRAY >
363 size_type offset = 0;
364 for (_axis=0; _axis < _index.size(); ++_axis)
365 offset += _index[_axis] * _strides[_axis];
371 template<
class MDARRAY >
380 template<
class MDARRAY >
384 if (_layout == FIRST_INDEX_FASTEST)
391 _done = (_index[_axis] < _dimensions[_axis]);
396 if (_axis >= _index.size())
406 _axis = _dimensions.size() - 1;
411 _done = (_index[_axis] < _dimensions[_axis]);
429 template<
class MDARRAY >
440 template<
class MDARRAY >
444 if (_layout == FIRST_INDEX_FASTEST)
451 _done = (_index[_axis] >= 0);
454 _index[_axis] = _dimensions[_axis] - 1;
456 if (_axis >= _index.size())
466 _axis = _dimensions.size() - 1;
471 _done = (_index[_axis] >= 0);
474 _index[_axis] = _dimensions[_axis] - 1;
489 template<
class MDARRAY >
500 template<
class MDARRAY >
510 template<
typename T >
511 std::ostream & operator<<(std::ostream & os, const MDIterator< T > & a)
521 template<
class MDARRAY >
523 MDIterator< MDARRAY >::assign_end_index()
528 for (
int axis = 0; axis < _index.size(); ++axis)
529 _index[axis] = _dimensions[axis];
534 template<
class MDARRAY >
536 MDIterator< MDARRAY >::
537 assert_index(dim_type i,
540 TEUCHOS_TEST_FOR_EXCEPTION(
541 !(0 <= i && i < _dimensions[axis]), RangeError,
542 "MDIterator<MDARRAY>::assert_index(i=" << i <<
",axis=" << axis <<
"): out"
543 <<
" of range i in [0, " << _dimensions[axis] <<
")"
MDARRAY::pointer pointer
Pointer type.
Definition: Domi_MDIterator.hpp:112
Iterator class suitable for multi-dimensional arrays.
Definition: Domi_MDIterator.hpp:100
Range Error exception type.
Definition: Domi_Exceptions.hpp:65
~MDIterator()
Destructor.
Definition: Domi_MDIterator.hpp:315
pointer operator->() const
Dereferencing arrow operator.
Definition: Domi_MDIterator.hpp:373
MDIterator< MDARRAY > & operator++()
Prefix increment operator.
Definition: Domi_MDIterator.hpp:382
MDIterator< MDARRAY > & operator=(const MDIterator< MDARRAY > &source)
Assignment operator.
Definition: Domi_MDIterator.hpp:323
bool operator!=(const MDIterator< MDARRAY > &other) const
Inequality operator.
Definition: Domi_MDIterator.hpp:352
MDARRAY::value_type value_type
Value type.
Definition: Domi_MDIterator.hpp:109
MDIterator< MDARRAY > & operator--()
Prefix decrement operator.
Definition: Domi_MDIterator.hpp:442
value_type & operator*()
Dereferencing operator.
Definition: Domi_MDIterator.hpp:361
dim_type index(int axis) const
Return the current index value along the given axis.
Definition: Domi_MDIterator.hpp:503
MDIterator(const MDARRAY &mdarray, bool end_index=false)
MDIterator constructor.
Definition: Domi_MDIterator.hpp:259
bool operator==(const MDIterator< MDARRAY > &other) const
Equality operator.
Definition: Domi_MDIterator.hpp:337