43#ifndef DOMI_MDARRAYVIEW_HPP
44#define DOMI_MDARRAYVIEW_HPP
50#include "Teuchos_Array.hpp"
51#include "Teuchos_ArrayView.hpp"
52#include "Teuchos_ConstTypeTraits.hpp"
53#include "Teuchos_RCPNode.hpp"
56#include "Domi_ConfigDefs.hpp"
57#include "Domi_Exceptions.hpp"
58#include "Domi_Utils.hpp"
60#include "Domi_MDIterator.hpp"
61#include "Domi_MDRevIterator.hpp"
69template<
typename T >
class MDArray;
70template<
typename T >
class MDArrayView;
71template<
typename T >
class MDArrayRCP;
113template<
typename T >
165 const Teuchos::ArrayView< dim_type > &
dims,
166 const Layout
layout = DEFAULT_ORDER);
189 const Teuchos::Array< dim_type > &
dims,
190 const Teuchos::Array< size_type > &
strides,
191 const Layout
layout = DEFAULT_ORDER);
246 inline const Teuchos::Array< dim_type > &
dimensions()
const;
261 inline const Teuchos::Array< size_type > &
strides()
const;
265 inline const Teuchos::ArrayView< T > &
arrayView()
const;
491 dim_type
n, dim_type
p, ...);
563 dim_type
m, dim_type
n)
const;
587 dim_type
m, dim_type
n, dim_type
p, ...)
const;
621 const T &
at(dim_type
i, ...)
const;
651 template<
typename T2 >
656 template<
typename T2 >
661 template<
typename T2 >
668 template<
typename T2 >
676 Teuchos::Array< dim_type > _dimensions;
677 Teuchos::Array< size_type > _strides;
678 Teuchos::ArrayView< T > _array;
686 void assertAxis(
int axis)
const;
691 void assertIndex(dim_type
i,
int axis)
const;
705template<
typename T >
708 _dimensions(Teuchos::
tuple< dim_type >(0)),
709 _strides(Teuchos::
tuple< size_type >(1)),
711 _layout(DEFAULT_ORDER),
719template<
typename T >
721 const Teuchos::ArrayView< dim_type > &
dims,
722 const Layout layout) :
724 _strides(computeStrides< size_type, dim_type >(
dims, layout)),
727 _ptr(_array.getRawPtr()),
732 "Teuchos::ArrayView size too small for "
738template<
typename T >
740 const Teuchos::Array< dim_type > &
dims,
741 const Teuchos::Array< size_type > & strides,
742 const Layout layout) :
747 _ptr(_array.getRawPtr()),
750 const size_type
required = computeSize<
const size_type,
755 "Teuchos::ArrayView size too small for "
756 "dimensions and strides");
761template<
typename T >
763 _dimensions(array._dimensions),
764 _strides(array._strides),
765 _array(array._array),
766 _layout(array._layout),
767 _ptr(_array.getRawPtr()),
774template<
typename T >
787 parent.assertIndex(index, axis);
791 size_type
n =
parent._dimensions.size();
795 _dimensions.push_back(1);
796 _strides.push_back(1);
800 for (
int myAxis = 0; myAxis < n; myAxis++)
803 _dimensions.push_back(parent._dimensions[myAxis]);
804 _strides.push_back(parent._strides[myAxis]);
808 _array = parent._array.view(offset,
809 computeSize(_dimensions(),
811 _ptr = _array.getRawPtr();
816template<
typename T >
820 _dimensions(parent._dimensions),
821 _strides(parent._strides),
823 _layout(parent._layout),
828 parent.assertAxis(axis);
830 Slice bounds = slice.bounds(_dimensions[axis]);
832 size_type offset = bounds.start() * _strides[axis];
834 _dimensions[axis] = (bounds.stop() - bounds.start()) / bounds.step();
836 _strides[axis] *= bounds.step();
838 _array = parent._array.view(offset,
839 computeSize(_dimensions(),
841 _ptr = _array.getRawPtr();
846template<
typename T >
850 _dimensions =
array._dimensions;
851 _strides =
array._strides;
852 _array =
array._array;
853 _layout =
array._layout;
855 _next_axis =
array._next_axis;
861template<
typename T >
868template<
typename T >
872 return _dimensions.size();
877template<
typename T >
878const Teuchos::Array< dim_type > &
886template<
typename T >
890#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
893 return _dimensions[axis];
898template<
typename T >
902 return computeSize(_dimensions(), _strides());
907template<
typename T >
908const Teuchos::Array< size_type > &
916template<
typename T >
917const Teuchos::ArrayView< T > &
925template<
typename T >
926const Teuchos::ArrayView< const T > &
929 return Teuchos::av_const_cast< const T >(_array);
934template<
typename T >
943template<
typename T >
958template<
typename T >
967template<
typename T >
977template<
typename T >
986template<
typename T >
996template<
typename T >
1005template<
typename T >
1015template<
typename T >
1024template<
typename T >
1034template<
typename T >
1043template<
typename T >
1053template<
typename T >
1065template<
typename T >
1073 result._next_axis = _next_axis;
1080template<
typename T >
1088 result._next_axis = _next_axis;
1095template<
typename T >
1102 result._next_axis = _next_axis + 1;
1103 if (
result._next_axis >= _dimensions.size())
1111template<
typename T >
1118 result._next_axis = _next_axis + 1;
1119 if (
result._next_axis >= _dimensions.size())
1127template<
typename T >
1131#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1134 "Attempt to access " << _dimensions.size() <<
"D array with 1 index"
1138 return _ptr[
i * _strides[0]];
1143template<
typename T >
1148#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1151 "Attempt to access " << _dimensions.size() <<
"D array with 2 indexes"
1156 return _ptr[
i * _strides[0] +
j * _strides[1]];
1161template<
typename T >
1167#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1170 "Attempt to access " << _dimensions.size() <<
"D array with 3 indexes"
1176 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2]];
1181template<
typename T >
1188#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1191 "Attempt to access " << _dimensions.size() <<
"D array with 4 indexes"
1198 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1204template<
typename T >
1212#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1215 "Attempt to access " << _dimensions.size() <<
"D array with 5 indexes"
1223 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1224 m * _strides[3] +
n * _strides[4]];
1229template<
typename T >
1239#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1242 "Attempt to access " << _dimensions.size() <<
"D array with too many indexes"
1252 size_type
offset =
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1253 m * _strides[3] +
n * _strides[4] +
p * _strides[5];
1255 for (
int axis = 6; axis < _dimensions.size(); axis++)
1258#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1259 assertIndex(
q, axis);
1269template<
typename T >
1273#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1276 "Attempt to access " << _dimensions.size() <<
"D array with 1 index"
1280 return _ptr[
i * _strides[0]];
1285template<
typename T >
1290#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1293 "Attempt to access " << _dimensions.size() <<
"D array with 2 indexes"
1298 return _ptr[
i * _strides[0] +
j * _strides[1]];
1303template<
typename T >
1309#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1312 "Attempt to access " << _dimensions.size() <<
"D array with 3 indexes"
1318 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2]];
1323template<
typename T >
1330#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1333 "Attempt to access " << _dimensions.size() <<
"D array with 4 indexes"
1340 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1346template<
typename T >
1354#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1357 "Attempt to access " << _dimensions.size() <<
"D array with 5 indexes"
1365 return _ptr[
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1366 m * _strides[3] +
n * _strides[4]];
1371template<
typename T >
1381#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1384 "Attempt to access " << _dimensions.size() <<
"D array with too many indexes"
1394 size_type
offset =
i * _strides[0] +
j * _strides[1] +
k * _strides[2] +
1395 m * _strides[3] +
n * _strides[4] +
p * _strides[5];
1397 for (
int axis = 6; axis < _dimensions.size(); axis++)
1400#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1401 assertIndex(
q, axis);
1411template<
typename T >
1421template<
typename T >
1427 size_type
offset =
i * _strides[0];
1429 for (
int axis = 1; axis < _dimensions.size(); axis++)
1432 assertIndex(
j, axis);
1441template<
typename T >
1447 size_type
offset =
i * _strides[0];
1449 for (
int axis = 1; axis < _dimensions.size(); axis++)
1452 assertIndex(
j, axis);
1461template<
typename T >
1465#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1474template<
typename T >
1485template<
typename T >
1489 std::stringstream
ss;
1491 for (size_type
i = 0;
i < _dimensions[0];
i++)
1499 if (
i < _dimensions[0]-1)
1508 ss << std::endl <<
" ";
1519template<
typename T >
1523 return _array.getRawPtr();
1528template<
typename T >
1532 return _array.getRawPtr();
1537template<
typename T >
1540 if (
a1._dimensions !=
a2._dimensions)
return false;
1541 if (
a1._layout !=
a2._layout )
return false;
1546 if (*
it1 != *
it2)
return false;
1553template<
typename T >
1561template<
typename T >
1569template<
typename T >
1580template<
typename T >
1582MDArrayView< T >::assertAxis(
int axis)
const
1585 !(0 <= axis && axis < _dimensions.
size()),
1587 "MDArrayView<T>::assertAxis(axis=" << axis <<
"): out of "
1588 <<
"range axis in [0, " << _dimensions.
size() <<
")"
1592template<
typename T >
1594MDArrayView< T >::assertIndex(dim_type i,
int axis)
const
1597 !(0 <=
i &&
i < _dimensions[axis]), RangeError,
1598 "MDArrayView<T>::assertIndex(i=" <<
i <<
",axis=" << axis <<
"): out of "
1599 <<
"range i in [0, " << _dimensions[axis] <<
")"
A Slice defines a subset of a container.
Memory-safe templated multi-dimensional array class.
Definition Domi_MDArray.hpp:287
const Teuchos::Array< T > & array() const
Return the underlying Teuchos::Array
Definition Domi_MDArray.hpp:1055
MDArrayView< T > operator[](dim_type i)
Sub-array access operator. The returned MDArrayView object will have one fewer dimensions than the ca...
Definition Domi_MDArray.hpp:1225
std::string toString() const
Convert the MDArray to a string representation.
Definition Domi_MDArray.hpp:1705
void swap(MDArray< T > &a)
Swap this MDArray with the given MDArray
Definition Domi_MDArray.hpp:1672
MDArray()
Default constructor.
Definition Domi_MDArray.hpp:915
size_type size() const
Return the total size of the MDArray
Definition Domi_MDArray.hpp:1037
MDArrayView< T > operator()()
Conversion to non-const MDArrayView
Definition Domi_MDArray.hpp:1267
int numDims() const
Return the number of dimensions.
Definition Domi_MDArray.hpp:1007
iterator end()
Return the ending iterator.
Definition Domi_MDArray.hpp:1082
iterator begin()
Return the beginning iterator.
Definition Domi_MDArray.hpp:1073
Memory-safe templated multi-dimensional array view class.
Definition Domi_MDArrayView.hpp:115
const MDArrayView< T > operator[](dim_type i) const
Sub-array const access operator. The returned MDArrayView object will have one fewer dimensions than ...
Definition Domi_MDArrayView.hpp:1082
const_reverse_iterator crend() const
Return the ending const_reverse_iterator.
Definition Domi_MDArrayView.hpp:1045
const T & operator()(dim_type i) const
Const 1D element access operator.
Definition Domi_MDArrayView.hpp:1271
const T & const_reference
Const reference type.
Definition Domi_MDArrayView.hpp:134
const T & operator()(dim_type i, dim_type j) const
Const 2D element access operator.
Definition Domi_MDArrayView.hpp:1287
size_type size() const
Return the total size of the MDArrayView
Definition Domi_MDArrayView.hpp:900
reverse_iterator rbegin()
Return the beginning reverse_iterator.
Definition Domi_MDArrayView.hpp:1017
const T * getRawPtr() const
Return a const raw pointer to the beginning of the MDArrayView or NULL if unsized.
Definition Domi_MDArrayView.hpp:1521
const_reverse_iterator crbegin() const
Return the beginning const_reverse_iterator.
Definition Domi_MDArrayView.hpp:1036
friend bool operator!=(const MDArrayView< T2 > &a1, const MDArrayView< T2 > &a2)
Inequality operator.
bool operator!=(const MDArrayView< T > &a1, const MDArrayView< T > &a2)
Inequality operator.
Definition Domi_MDArrayView.hpp:1554
const T * const_pointer
Const pointer type.
Definition Domi_MDArrayView.hpp:128
friend void swap(MDArrayView< T2 > &a1, MDArrayView< T2 > &a2)
Swap function.
MDArrayView< const T > getConst() const
Return an MDArrayView< const T > of an MDArrayView< T > object.
Definition Domi_MDArrayView.hpp:1055
const Teuchos::Array< dim_type > & dimensions() const
Return the array of dimensions.
Definition Domi_MDArrayView.hpp:879
const T & operator()(dim_type i, dim_type j, dim_type k, dim_type m, dim_type n) const
Const 5D element access operator.
Definition Domi_MDArrayView.hpp:1348
bool operator==(const MDArrayView< T > &a1, const MDArrayView< T > &a2)
Equality operator.
Definition Domi_MDArrayView.hpp:1538
static bool hasBoundsChecking()
Return true if MDArrayView has been compiled with bounds checking on.
Definition Domi_MDArrayView.hpp:1463
iterator end()
Return the ending iterator.
Definition Domi_MDArrayView.hpp:969
MDArrayView< T > operator[](dim_type i)
Sub-array access operator. The returned MDArrayView object will have one fewer dimensions than the ca...
Definition Domi_MDArrayView.hpp:1067
MDArrayView(const MDArrayView< T > &array)
Copy constructor.
Definition Domi_MDArrayView.hpp:762
T & at(dim_type i,...)
Non-const single element access method with bounds checking.
Definition Domi_MDArrayView.hpp:1423
const_iterator begin() const
Return the beginning const_iterator.
Definition Domi_MDArrayView.hpp:979
std::string toString() const
Convert the MDArrayView to a string representation.
Definition Domi_MDArrayView.hpp:1476
const_iterator end() const
Return the ending const_iterator.
Definition Domi_MDArrayView.hpp:988
T & operator()(dim_type i, dim_type j, dim_type k, dim_type m)
Non-const 4D element access operator.
Definition Domi_MDArrayView.hpp:1183
T & operator()(dim_type i, dim_type j, dim_type k, dim_type m, dim_type n)
Non-const 5D element access operator.
Definition Domi_MDArrayView.hpp:1206
const Teuchos::Array< size_type > & strides() const
Return the indexing strides.
Definition Domi_MDArrayView.hpp:909
const T & operator()(dim_type i, dim_type j, dim_type k, dim_type m) const
Const 4D element access operator.
Definition Domi_MDArrayView.hpp:1325
T & operator()(dim_type i)
Non-const 1D element access operator.
Definition Domi_MDArrayView.hpp:1129
T & reference
Reference type.
Definition Domi_MDArrayView.hpp:131
MDArrayView(Teuchos::ENull null_arg=Teuchos::null)
Default constructor.
Definition Domi_MDArrayView.hpp:707
const Teuchos::ArrayView< T > & arrayView() const
Return the underlying Teuchos::ArrayView
Definition Domi_MDArrayView.hpp:918
T & operator()(dim_type i, dim_type j, dim_type k)
Non-const 3D element access operator.
Definition Domi_MDArrayView.hpp:1163
MDArrayView< T > & operator=(const MDArrayView< T > &array)
Assignment operator.
Definition Domi_MDArrayView.hpp:848
T * pointer
Pointer type.
Definition Domi_MDArrayView.hpp:125
MDArrayView(const Teuchos::ArrayView< T > &array, const Teuchos::ArrayView< dim_type > &dims, const Layout layout=DEFAULT_ORDER)
Constructor with a source Teuchos::ArrayView, dimensions, and optional storage order.
Definition Domi_MDArrayView.hpp:720
const_iterator cend() const
Return the ending const_iterator.
Definition Domi_MDArrayView.hpp:1007
T * getRawPtr()
Return a raw pointer to the beginning of the MDArrayView or NULL if unsized.
Definition Domi_MDArrayView.hpp:1530
dim_type dimension(int axis) const
Return the dimension of the given axis.
Definition Domi_MDArrayView.hpp:888
const MDArrayView< T > operator[](Slice s) const
Sub-array const access operator. The returned MDArrayView object will have the same number of dimensi...
Definition Domi_MDArrayView.hpp:1113
friend std::ostream & operator<<(std::ostream &os, const MDArrayView< T2 > &a)
Stream output operator.
const T & operator()(dim_type i, dim_type j, dim_type k, dim_type m, dim_type n, dim_type p,...) const
Const 6D and higher element access operator.
Definition Domi_MDArrayView.hpp:1373
~MDArrayView()
Destructor.
Definition Domi_MDArrayView.hpp:862
MDArrayView< T > operator[](Slice s)
Sub-array access operator. The returned MDArrayView object will have the same number of dimensions as...
Definition Domi_MDArrayView.hpp:1097
const Teuchos::ArrayView< const T > & arrayViewConst() const
Return const version of the underlying Teuchos::ArrayView
Definition Domi_MDArrayView.hpp:927
iterator begin()
Return the beginning iterator.
Definition Domi_MDArrayView.hpp:960
Layout layout() const
Return the storage order.
Definition Domi_MDArrayView.hpp:936
T & operator()(dim_type i, dim_type j)
Non-const 2D element access operator.
Definition Domi_MDArrayView.hpp:1145
const_iterator cbegin() const
Return the beginning const_iterator.
Definition Domi_MDArrayView.hpp:998
friend bool operator==(const MDArrayView< T2 > &a1, const MDArrayView< T2 > &a2)
Equality operator.
const T & operator()(dim_type i, dim_type j, dim_type k) const
Const 3D element access operator.
Definition Domi_MDArrayView.hpp:1305
bool contiguous() const
Return whether the MDArrayView is contiguous in memory.
Definition Domi_MDArrayView.hpp:945
void assign(const T &value)
Assign a value to all elements of the MDArrayView
Definition Domi_MDArrayView.hpp:1413
reverse_iterator rend()
Return the ending reverse_iterator.
Definition Domi_MDArrayView.hpp:1026
int numDims() const
Return the number of dimensions.
Definition Domi_MDArrayView.hpp:870
T & operator()(dim_type i, dim_type j, dim_type k, dim_type m, dim_type n, dim_type p,...)
Non-const 6D and higher element access operator.
Definition Domi_MDArrayView.hpp:1231
MDArrayView(const Teuchos::ArrayView< T > &array, const Teuchos::Array< dim_type > &dims, const Teuchos::Array< size_type > &strides, const Layout layout=DEFAULT_ORDER)
Constructor with a source Teuchos::ArrayView, dimensions, strides, and optional storage order.
Definition Domi_MDArrayView.hpp:739
const T & at(dim_type i,...) const
Const single element access method with bounds checking.
Definition Domi_MDArrayView.hpp:1443
T value_type
Value type.
Definition Domi_MDArrayView.hpp:122
Iterator class suitable for multi-dimensional arrays.
Definition Domi_MDIterator.hpp:102
Reverse iterator class suitable for multi-dimensional arrays.
Definition Domi_MDRevIterator.hpp:100
Range Error exception type.
Definition Domi_Exceptions.hpp:66
A Slice contains a start, stop, and step index, describing a subset of an ordered container.
Definition Domi_Slice.hpp:138