Domi
Multi-dimensional, distributed data structures
 All Classes Files Functions Variables Typedefs Friends
Domi_MDArrayRCP.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Domi: Multi-dimensional Distributed Linear Algebra Services
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia
8 // Corporation, the U.S. Government retains certain rights in this
9 // software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact William F. Spotz (wfspotz@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef DOMI_MDARRAYRCP_HPP
44 #define DOMI_MDARRAYRCP_HPP
45 
46 // Standard includes
47 #include <cstdarg>
48 
49 // Teuchos includes
50 #include "Teuchos_ArrayRCPDecl.hpp"
51 
52 // Domi includes
53 #include "Domi_ConfigDefs.hpp"
54 #include "Domi_Utils.hpp"
55 #include "Domi_MDArrayView.hpp"
56 
57 namespace Domi
58 {
59 
60 // I put these non-member template functions here for the same reason
61 // that Ross did the same thing for the Teuchos::Array class. See
62 // Teuchos_Array.hpp for details.
63 template< typename T > class MDArrayRCP;
64 
69 template< typename T >
70 bool operator==(const MDArrayRCP< T > & a1,
71  const MDArrayRCP< T > & a2);
72 
78 template< typename T >
79 bool operator==(const MDArray< T > & a1,
80  const MDArrayRCP< T > & a2);
81 
87 template< typename T >
88 bool operator==(const MDArrayRCP< T > & a1,
89  const MDArray< T > & a2);
90 
96 template< typename T >
97 bool operator==(const MDArrayView< T > & a1,
98  const MDArrayRCP< T > & a2);
99 
105 template< typename T >
106 bool operator==(const MDArrayRCP< T > & a1,
107  const MDArrayView< T > & a2);
108 
113 template< typename T >
114 bool operator!=(const MDArrayRCP< T > & a1,
115  const MDArrayRCP< T > & a2);
116 
122 template< typename T >
123 bool operator!=(const MDArray< T > & a1,
124  const MDArrayRCP< T > & a2);
125 
131 template< typename T >
132 bool operator!=(const MDArrayView< T > & a1,
133  const MDArrayRCP< T > & a2);
134 
140 template< typename T >
141 bool operator!=(const MDArrayRCP< T > & a1,
142  const MDArrayView< T > & a2);
143 
153 template< typename T >
154 class MDArrayRCP
155 {
156 public:
157 
160 
162  typedef T value_type;
163 
165  typedef T* pointer;
166 
168  typedef const T* const_pointer;
169 
171  typedef T& reference;
172 
174  typedef const T& const_reference;
175 
177 
180 
185  inline
186  MDArrayRCP(Teuchos::ENull null_arg = Teuchos::null);
187 
204  inline
205  MDArrayRCP(const Teuchos::ArrayView< T > & array,
206  const Teuchos::ArrayView< dim_type > & dims,
207  Layout layout = DEFAULT_ORDER);
208 
224  inline explicit
225  MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
226  const_reference val = T(),
227  Layout layout = DEFAULT_ORDER);
228 
241  inline explicit
242  MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
243  Layout layout);
244 
265  inline explicit
266  MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
267  const Teuchos::ArrayView< size_type > & strides,
268  T * data,
269  Layout layout = DEFAULT_ORDER);
270 
275  inline
276  MDArrayRCP(const MDArrayRCP< T > & r_ptr);
277 
282  MDArrayRCP(const MDArrayView< T > & source);
283 
286  inline ~MDArrayRCP();
287 
292  inline MDArrayRCP< T > & operator=(const MDArrayRCP< T > & r_ptr);
293 
295 
298 
301  inline int numDims() const;
302 
305  inline const Teuchos::Array< dim_type > & dimensions() const;
306 
312  inline dim_type dimension(int axis) const;
313 
316  inline size_type size() const;
317 
320  inline const Teuchos::Array< size_type > & strides() const;
321 
324  inline const Teuchos::ArrayRCP< T > & arrayRCP() const;
325 
328  inline const Layout layout() const;
329 
331 
334 
335  friend class MDIterator< MDArrayRCP< T > >;
336  friend class MDIterator< MDArrayRCP< const T > >;
337  friend class MDRevIterator< MDArrayRCP< T > >;
338  friend class MDRevIterator< MDArrayRCP< const T > >;
339 
344 
347  iterator begin();
348 
351  iterator end();
352 
355  const_iterator begin() const;
356 
359  const_iterator end() const;
360 
363  const_iterator cbegin() const;
364 
367  const_iterator cend() const;
368 
372 
376 
380 
384 
386 
389 
392  inline bool is_null() const;
393 
396  inline pointer operator->() const;
397 
400  inline reference operator*();
401 
404  inline pointer get() const;
405 
407 
410 
415 
419  const MDArrayView< T > mdArrayView() const;
420 
425 
430 
434  inline operator MDArrayView< T >() const;
435 
439  inline operator MDArrayView< const T >() const;
440 
442 
445 
455  MDArrayView< T > operator[](dim_type i);
456 
466  const MDArrayView< T > operator[](dim_type i) const;
467 
479 
490  const MDArrayView< T > operator[](Slice s) const;
491 
495 
498  const MDArrayView< T > operator()() const;
499 
501 
504 
513  inline T & operator()(dim_type i);
514 
525  inline T & operator()(dim_type i, dim_type j);
526 
539  inline T & operator()(dim_type i, dim_type j, dim_type k);
540 
555  inline T & operator()(dim_type i, dim_type j, dim_type k, dim_type m);
556 
573  inline T & operator()(dim_type i, dim_type j, dim_type k, dim_type m,
574  dim_type n);
575 
597  inline T & operator()(dim_type i, dim_type j, dim_type k, dim_type m,
598  dim_type n, dim_type p, ...);
599 
608  inline const T & operator()(dim_type i) const;
609 
620  inline const T & operator()(dim_type i, dim_type j) const;
621 
634  inline const T & operator()(dim_type i, dim_type j, dim_type k) const;
635 
650  inline const T & operator()(dim_type i, dim_type j, dim_type k,
651  dim_type m) const;
652 
669  inline const T & operator()(dim_type i, dim_type j, dim_type k,
670  dim_type m, dim_type n) const;
671 
693  inline const T & operator()(dim_type i, dim_type j, dim_type k,
694  dim_type m, dim_type n, dim_type p, ...) const;
695 
696 
698 
701 
706  void assign(const_reference value);
707 
717  reference at(dim_type i, ...);
718 
727  const_reference at(dim_type i, ...) const;
728 
731  inline size_type capacity() const;
732 
735  void clear();
736 
739  inline bool empty() const;
740 
744  inline size_type max_size() const;
745 
754  void resize(const Teuchos::ArrayView< dim_type > & dims);
755 
759  inline static bool hasBoundsChecking();
760 
764  std::string toString() const;
765 
769  inline const_pointer getRawPtr() const;
770 
774  inline pointer getRawPtr();
775 
777 
778  // These operators are declared as friends so that the compiler will
779  // do automatic type conversion.
780 
783 
786  template< typename T2 >
787  friend bool operator==(const MDArrayRCP< T2 > & a1,
788  const MDArrayRCP< T2 > & a2);
789 
792  template< typename T2 >
793  friend bool operator==(const MDArray< T2 > & a1,
794  const MDArrayRCP< T2 > & a2);
795 
798  template< typename T2 >
799  friend bool operator==(const MDArrayRCP< T2 > & a1,
800  const MDArray< T2 > & a2);
801 
804  template< typename T2 >
805  friend bool operator==(const MDArrayRCP< T2 > & a1,
806  const MDArrayView< T2 > & a2);
807 
810  template< typename T2 >
811  friend bool operator==(const MDArrayView< T2 > & a1,
812  const MDArrayRCP< T2 > & a2);
813 
816  template< typename T2 >
817  friend bool operator!=(const MDArrayRCP< T2 > & a1,
818  const MDArrayRCP< T2 > & a2);
819 
822  template< typename T2 >
823  friend bool operator!=(const MDArray< T2 > & a1,
824  const MDArrayRCP< T2 > & a2);
825 
828  template< typename T2 >
829  friend bool operator!=(const MDArrayRCP< T2 > & a1,
830  const MDArray< T2 > & a2);
831 
834  template< typename T2 >
835  friend bool operator!=(const MDArrayRCP< T2 > & a1,
836  const MDArrayView< T2 > & a2);
837 
840  template< typename T2 >
841  friend bool operator!=(const MDArrayView< T2 > & a1,
842  const MDArrayRCP< T2 > & a2);
843 
846  template< typename T2 >
847  friend std::ostream & operator<<(std::ostream & os,
848  const MDArrayRCP< T2 > & a);
849 
851 
852 private:
853  Teuchos::Array< dim_type > _dimensions;
854  Teuchos::Array< size_type > _strides;
855  Teuchos::ArrayRCP< T > _array;
856  Layout _layout;
857  pointer _ptr;
858 
859  // Used for array bounds checking
860  void assertAxis(int axis) const;
861 
862  // Used for array bounds checking
863  void assertIndex(dim_type i, int axis) const;
864 };
865 
867 
869 // Implementations //
871 
872 template< typename T >
873 MDArrayRCP< T >::MDArrayRCP(Teuchos::ENull null_arg) :
874  _dimensions(Teuchos::tuple< dim_type >(0)),
875  _strides(Teuchos::tuple< size_type >(1)),
876  _array(),
877  _layout(DEFAULT_ORDER),
878  _ptr()
879 {
880 }
881 
883 
884 template< typename T >
885 MDArrayRCP< T >::MDArrayRCP(const Teuchos::ArrayView< T > & array,
886  const Teuchos::ArrayView< dim_type > & dims,
887  Layout layout) :
888  _dimensions(dims),
889  _strides(computeStrides< size_type, dim_type >(dims, layout)),
890  _array(array.getRawPtr(), 0, array.size(), false),
891  _layout(layout),
892  _ptr(_array.getRawPtr())
893 {
894  TEUCHOS_TEST_FOR_EXCEPTION(array.size() < computeSize(dims),
895  RangeError,
896  "Teuchos::ArrayView size too small for "
897  "dimensions");
898 }
899 
901 
902 template< typename T >
903 MDArrayRCP< T >::MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
904  const T & val,
905  Layout layout) :
906  _dimensions(dims),
907  _strides(computeStrides< size_type, dim_type >(dims, layout)),
908  _array(computeSize(dims), val),
909  _layout(layout),
910  _ptr(_array.getRawPtr())
911 {
912 }
913 
915 
916 template< typename T >
917 MDArrayRCP< T >::MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
918  Layout layout) :
919  _dimensions(dims),
920  _strides(computeStrides< size_type, dim_type >(dims, layout)),
921  _array(computeSize(dims)),
922  _layout(layout),
923  _ptr(_array.getRawPtr())
924 {
925 }
926 
928 
929 template< typename T >
930 MDArrayRCP< T >::MDArrayRCP(const Teuchos::ArrayView< dim_type > & dims,
931  const Teuchos::ArrayView< size_type > & strides,
932  T * data,
933  Layout layout) :
934  _dimensions(dims),
935  _strides(strides),
936  _array(data, 0, computeSize(dims, strides), false),
937  _layout(layout),
938  _ptr(data)
939 {
940 }
941 
943 
944 template< typename T >
946  _dimensions(r_ptr._dimensions),
947  _strides(r_ptr._strides),
948  _array(r_ptr._array),
949  _layout(r_ptr._layout),
950  _ptr(_array.getRawPtr())
951 {
952 }
953 
955 
956 template< typename T >
958  _dimensions(source.dimensions()),
959  _strides(computeStrides< size_type, dim_type >(source.dimensions(),
960  source.layout())),
961  _array(computeSize(source.dimensions())),
962  _layout(source.layout()),
963  _ptr(_array.getRawPtr())
964 {
965  // Copy the values from the MDArrayView to the MDArrayRCP
966  iterator thisit = begin();
967  typename MDArrayView< T >::const_iterator srcit = source.cbegin();
968  for ( ; srcit != source.cend(); ++thisit, ++srcit)
969  {
970  *thisit = *srcit;
971  }
972 }
973 
975 
976 template< typename T >
978 {
979 }
980 
982 
983 template< typename T >
986 {
987  _dimensions = r_ptr._dimensions;
988  _strides = r_ptr._strides;
989  _array = r_ptr._array;
990  _layout = r_ptr._layout;
991  _ptr = r_ptr._ptr;
992  return *this;
993 }
994 
996 
997 template< typename T >
998 int
1000 {
1001  return _dimensions.size();
1002 }
1003 
1005 
1006 template< typename T >
1007 const Teuchos::Array< dim_type > &
1009 {
1010  return _dimensions;
1011 }
1012 
1014 
1015 template< typename T >
1016 dim_type
1018 {
1019 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1020  assertAxis(axis);
1021 #endif
1022  return _dimensions[axis];
1023 }
1024 
1026 
1027 template< typename T >
1028 size_type
1030 {
1031  return _array.size();
1032 }
1033 
1035 
1036 template< typename T >
1037 const Teuchos::Array< size_type > &
1039 {
1040  return _strides;
1041 }
1042 
1044 
1045 template< typename T >
1046 const Teuchos::ArrayRCP< T > &
1048 {
1049  return _array;
1050 }
1051 
1053 
1054 template< typename T >
1055 const Layout
1057 {
1058  return _layout;
1059 }
1060 
1062 
1063 template< typename T >
1066 {
1067  return iterator(*this);
1068 }
1069 
1071 
1072 template< typename T >
1075 {
1076  // Return the iterator corresponding to the last element
1077  return iterator(*this, true);
1078 }
1079 
1081 
1082 template< typename T >
1085 {
1086  return const_iterator(*this);
1087 }
1088 
1090 
1091 template< typename T >
1094 {
1095  // Return the iterator corresponding to the last element
1096  return const_iterator(*this, true);
1097 }
1098 
1100 
1101 template< typename T >
1104 {
1105  return const_iterator(*this);
1106 }
1107 
1109 
1110 template< typename T >
1113 {
1114  // Return the iterator corresponding to the last element
1115  return const_iterator(*this, true);
1116 }
1117 
1119 
1120 template< typename T >
1123 {
1124  return reverse_iterator(*this);
1125 }
1126 
1128 
1129 template< typename T >
1132 {
1133  // Return the reverse_iterator corresponding to the last element
1134  return reverse_iterator(*this, true);
1135 }
1136 
1138 
1139 template< typename T >
1142 {
1143  return const_reverse_iterator(*this);
1144 }
1145 
1147 
1148 template< typename T >
1151 {
1152  // Return the reverse_iterator corresponding to the last element
1153  return const_reverse_iterator(*this, true);
1154 }
1155 
1157 
1158 template< typename T >
1159 bool
1161 {
1162  return _array.is_null();
1163 }
1164 
1166 
1167 template< typename T >
1168 T *
1170 {
1171  return _array.operator->();
1172 }
1173 
1175 
1176 template< typename T >
1177 T &
1179 {
1180  return _array.operator*();
1181 }
1182 
1184 
1185 template< typename T >
1186 T *
1188 {
1189  return _array.get();
1190 }
1191 
1193 
1194 template< typename T >
1197 {
1198  return MDArrayView< T >(_array(), _dimensions, _layout);
1199 }
1200 
1202 
1203 template< typename T >
1204 const MDArrayView< T >
1206 {
1207  Teuchos::Array< dim_type > dims(_dimensions);
1208  return MDArrayView< T >(_array(), dims(), _layout);
1209 }
1210 
1212 
1213 template< typename T >
1216 {
1217  return MDArrayView< const T >(_array(), _dimensions, _layout);
1218 }
1219 
1221 
1222 template< typename T >
1225 {
1226  return MDArrayView< const T >(_array(), _dimensions, _layout);
1227 }
1228 
1230 
1231 template< typename T >
1233 {
1234  return mdArrayView();
1235 }
1236 
1238 
1239 template< typename T >
1241 {
1242  return mdArrayViewConst();
1243 }
1244 
1246 
1247 template< typename T >
1250 {
1251  // Note: array bounds checking, if active, will be performed by the
1252  // MDArrayView class
1253  return mdArrayView()[i];
1254 }
1255 
1257 
1258 template< typename T >
1259 const MDArrayView< T >
1261 {
1262  // Note: array bounds checking, if active, will be performed by the
1263  // MDArrayView class
1264  return mdArrayView()[i];
1265 }
1266 
1268 
1269 template< typename T >
1270 const MDArrayView< T >
1272 {
1273  // Note: Slices produce safe indexes
1274  return mdArrayView()[s];
1275 }
1276 
1278 
1279 template< typename T >
1282 {
1283  // Note: Slices produce safe indexes
1284  return mdArrayView()[s];
1285 }
1286 
1288 
1289 template< typename T >
1292 {
1293  return mdArrayView();
1294 }
1295 
1297 
1298 template< typename T >
1299 const MDArrayView< T >
1301 {
1302  return mdArrayView();
1303 }
1304 
1306 
1307 template< typename T >
1308 T &
1310 {
1311 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1312  TEUCHOS_TEST_FOR_EXCEPTION(
1313  (_dimensions.size() != 1), RangeError,
1314  "Attempt to access " << _dimensions.size() << "D array with 1 index"
1315  );
1316  assertIndex(i, 0);
1317 #endif
1318  return _ptr[i * _strides[0]];
1319 }
1320 
1322 
1323 template< typename T >
1324 T &
1326  dim_type j)
1327 {
1328 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1329  TEUCHOS_TEST_FOR_EXCEPTION(
1330  (_dimensions.size() != 2), RangeError,
1331  "Attempt to access " << _dimensions.size() << "D array with 2 indexes"
1332  );
1333  assertIndex(i, 0);
1334  assertIndex(j, 1);
1335 #endif
1336  return _ptr[i * _strides[0] + j * _strides[1]];
1337 }
1338 
1340 
1341 template< typename T >
1342 T &
1344  dim_type j,
1345  dim_type k)
1346 {
1347 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1348  TEUCHOS_TEST_FOR_EXCEPTION(
1349  (_dimensions.size() != 3), RangeError,
1350  "Attempt to access " << _dimensions.size() << "D array with 3 indexes"
1351  );
1352  assertIndex(i, 0);
1353  assertIndex(j, 1);
1354  assertIndex(k, 2);
1355 #endif
1356  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2]];
1357 }
1358 
1360 
1361 template< typename T >
1362 T &
1364  dim_type j,
1365  dim_type k,
1366  dim_type m)
1367 {
1368 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1369  TEUCHOS_TEST_FOR_EXCEPTION(
1370  (_dimensions.size() != 4), RangeError,
1371  "Attempt to access " << _dimensions.size() << "D array with 4 indexes"
1372  );
1373  assertIndex(i, 0);
1374  assertIndex(j, 1);
1375  assertIndex(k, 2);
1376  assertIndex(m, 3);
1377 #endif
1378  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2] +
1379  m * _strides[3]];
1380 }
1381 
1383 
1384 template< typename T >
1385 T &
1387  dim_type j,
1388  dim_type k,
1389  dim_type m,
1390  dim_type n)
1391 {
1392 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1393  TEUCHOS_TEST_FOR_EXCEPTION(
1394  (_dimensions.size() != 5), RangeError,
1395  "Attempt to access " << _dimensions.size() << "D array with 5 indexes"
1396  );
1397  assertIndex(i, 0);
1398  assertIndex(j, 1);
1399  assertIndex(k, 2);
1400  assertIndex(m, 3);
1401  assertIndex(n, 4);
1402 #endif
1403  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2] +
1404  m * _strides[3] + n * _strides[4]];
1405 }
1406 
1408 
1409 template< typename T >
1410 T &
1412  dim_type j,
1413  dim_type k,
1414  dim_type m,
1415  dim_type n,
1416  dim_type p,
1417  ...)
1418 {
1419 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1420  TEUCHOS_TEST_FOR_EXCEPTION(
1421  (_dimensions.size() < 6), RangeError,
1422  "Attempt to access " << _dimensions.size() << "D array with too many indexes"
1423  );
1424  assertIndex(i, 0);
1425  assertIndex(j, 1);
1426  assertIndex(k, 2);
1427  assertIndex(m, 3);
1428  assertIndex(n, 4);
1429  assertIndex(p, 5);
1430 #endif
1431  va_list indexes;
1432  size_type offset = i * _strides[0] + j * _strides[1] + k * _strides[2] +
1433  m * _strides[3] + n * _strides[4] + p * _strides[5];
1434  va_start(indexes, p);
1435  for (int axis = 6; axis < _dimensions.size(); axis++)
1436  {
1437  dim_type q = va_arg(indexes, dim_type);
1438 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1439  assertIndex(q, axis);
1440 #endif
1441  offset += q * _strides[axis];
1442  }
1443  va_end(indexes);
1444  return _ptr[offset];
1445 }
1446 
1448 
1449 template< typename T >
1450 const T &
1452 {
1453 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1454  TEUCHOS_TEST_FOR_EXCEPTION(
1455  (_dimensions.size() != 1), RangeError,
1456  "Attempt to access " << _dimensions.size() << "D array with 1 index"
1457  );
1458  assertIndex(i, 0);
1459 #endif
1460  return _ptr[i * _strides[0]];
1461 }
1462 
1464 
1465 template< typename T >
1466 const T &
1468  dim_type j) const
1469 {
1470 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1471  TEUCHOS_TEST_FOR_EXCEPTION(
1472  (_dimensions.size() != 2), RangeError,
1473  "Attempt to access " << _dimensions.size() << "D array with 2 indexes"
1474  );
1475  assertIndex(i, 0);
1476  assertIndex(j, 1);
1477 #endif
1478  return _ptr[i * _strides[0] + j * _strides[1]];
1479 }
1480 
1482 
1483 template< typename T >
1484 const T &
1486  dim_type j,
1487  dim_type k) const
1488 {
1489 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1490  TEUCHOS_TEST_FOR_EXCEPTION(
1491  (_dimensions.size() != 3), RangeError,
1492  "Attempt to access " << _dimensions.size() << "D array with 3 indexes"
1493  );
1494  assertIndex(i, 0);
1495  assertIndex(j, 1);
1496  assertIndex(k, 2);
1497 #endif
1498  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2]];
1499 }
1500 
1502 
1503 template< typename T >
1504 const T &
1506  dim_type j,
1507  dim_type k,
1508  dim_type m) const
1509 {
1510 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1511  TEUCHOS_TEST_FOR_EXCEPTION(
1512  (_dimensions.size() != 4), RangeError,
1513  "Attempt to access " << _dimensions.size() << "D array with 4 indexes"
1514  );
1515  assertIndex(i, 0);
1516  assertIndex(j, 1);
1517  assertIndex(k, 2);
1518  assertIndex(m, 3);
1519 #endif
1520  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2] +
1521  m * _strides[3]];
1522 }
1523 
1525 
1526 template< typename T >
1527 const T &
1529  dim_type j,
1530  dim_type k,
1531  dim_type m,
1532  dim_type n) const
1533 {
1534 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1535  TEUCHOS_TEST_FOR_EXCEPTION(
1536  (_dimensions.size() != 5), RangeError,
1537  "Attempt to access " << _dimensions.size() << "D array with 5 indexes"
1538  );
1539  assertIndex(i, 0);
1540  assertIndex(j, 1);
1541  assertIndex(k, 2);
1542  assertIndex(m, 3);
1543  assertIndex(n, 4);
1544 #endif
1545  return _ptr[i * _strides[0] + j * _strides[1] + k * _strides[2] +
1546  m * _strides[3] + n * _strides[4]];
1547 }
1548 
1550 
1551 template< typename T >
1552 const T &
1554  dim_type j,
1555  dim_type k,
1556  dim_type m,
1557  dim_type n,
1558  dim_type p,
1559  ...) const
1560 {
1561 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1562  TEUCHOS_TEST_FOR_EXCEPTION(
1563  (_dimensions.size() < 6), RangeError,
1564  "Attempt to access " << _dimensions.size() << "D array with too many indexes"
1565  );
1566  assertIndex(i, 0);
1567  assertIndex(j, 1);
1568  assertIndex(k, 2);
1569  assertIndex(m, 3);
1570  assertIndex(n, 4);
1571  assertIndex(p, 5);
1572 #endif
1573  va_list indexes;
1574  size_type offset = i * _strides[0] + j * _strides[1] + k * _strides[2] +
1575  m * _strides[3] + n * _strides[4] + p * _strides[5];
1576  va_start(indexes, p);
1577  for (int axis = 6; axis < _dimensions.size(); axis++)
1578  {
1579  dim_type q = va_arg(indexes, dim_type);
1580 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1581  assertIndex(q, axis);
1582 #endif
1583  offset += q * _strides[axis];
1584  }
1585  va_end(indexes);
1586  return _ptr[offset];
1587 }
1588 
1590 
1591 template< typename T >
1592 void
1594 {
1595  for (iterator it = begin(); it != end(); ++it)
1596  *it = value;
1597 }
1598 
1600 
1601 template< typename T >
1602 T &
1603 MDArrayRCP< T >::at(dim_type i, ...)
1604 {
1605  assertIndex(i, 0);
1606  va_list indexes;
1607  size_type offset = i * _strides[0];
1608  va_start(indexes, i);
1609  for (int axis = 1; axis < _dimensions.size(); axis++)
1610  {
1611  dim_type j = va_arg(indexes, dim_type);
1612  assertIndex(j, axis);
1613  offset += j * _strides[axis];
1614  }
1615  va_end(indexes);
1616  return _array[offset];
1617 }
1618 
1620 
1621 template< typename T >
1622 const T &
1623 MDArrayRCP< T >::at(dim_type i, ...) const
1624 {
1625  assertIndex(i, 0);
1626  va_list indexes;
1627  size_type offset = i * _strides[0];
1628  va_start(indexes, i);
1629  for (int axis = 1; axis < _dimensions.size(); axis++)
1630  {
1631  dim_type j = va_arg(indexes, dim_type);
1632  assertIndex(j, axis);
1633  offset += j * _strides[axis];
1634  }
1635  va_end(indexes);
1636  return _array[offset];
1637 }
1638 
1640 
1641 template< typename T >
1642 size_type
1644 {
1645  return _array.capacity();
1646 }
1647 
1649 
1650 template< typename T >
1651 void
1653 {
1654  _dimensions.resize(1);
1655  _dimensions[0] = 0;
1656  _strides.resize(1);
1657  _strides[0] = 1;
1658  _array.clear();
1659  _ptr = _array.getRawPtr();
1660 }
1661 
1663 
1664 template< typename T >
1665 bool
1667 {
1668  return (_array.size() == 0);
1669 }
1670 
1672 
1673 template< typename T >
1674 size_type
1676 {
1677  return _array.max_size();
1678 }
1679 
1681 
1682 template< typename T >
1683 void
1684 MDArrayRCP< T >::resize(const Teuchos::ArrayView< dim_type > & dims)
1685 {
1686  _dimensions.assign(dims.begin(), dims.end());
1687  _strides = computeStrides< size_type, dim_type >(dims, _layout);
1688  _array.resize(computeSize(dims));
1689  _ptr = _array.getRawPtr();
1690 }
1691 
1693 
1694 template< typename T >
1695 bool
1697 {
1698 #ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1699  return true;
1700 #else
1701  return false;
1702 #endif
1703 }
1704 
1706 
1707 template< typename T >
1708 std::string
1710 {
1711  return mdArrayView().toString();
1712 }
1713 
1715 
1716 template< typename T >
1717 const T *
1719 {
1720  return _array.getRawPtr();
1721 }
1722 
1724 
1725 template< typename T >
1726 T *
1728 {
1729  return _array.getRawPtr();
1730 }
1731 
1733 
1734 template< typename T >
1735 bool operator==(const MDArrayRCP< T > & a1,
1736  const MDArrayRCP< T > & a2)
1737 {
1738  return (a1() == a2());
1739 }
1740 
1742 
1743 template< typename T >
1744 bool operator==(const MDArray< T > & a1,
1745  const MDArrayRCP< T > & a2)
1746 {
1747  return (a1() == a2());
1748 }
1749 
1751 
1752 template< typename T >
1753 bool operator==(const MDArrayRCP< T > & a1,
1754  const MDArray< T > & a2)
1755 {
1756  return (a1() == a2());
1757 }
1758 
1760 
1761 template< typename T >
1762 bool operator==(const MDArrayView< T > & a1,
1763  const MDArrayRCP< T > & a2)
1764 {
1765  return (a1 == a2());
1766 }
1767 
1769 
1770 template< typename T >
1771 bool operator==(const MDArrayRCP< T > & a1,
1772  const MDArrayView< T > & a2)
1773 {
1774  return (a1() == a2);
1775 }
1776 
1778 
1779 template< typename T >
1780 bool operator!=(const MDArrayRCP< T > & a1,
1781  const MDArrayRCP< T > & a2)
1782 {
1783  return not (a1 == a2);
1784 }
1785 
1787 
1788 template< typename T >
1789 bool operator!=(const MDArray< T > & a1, const MDArrayRCP< T > & a2)
1790 {
1791  return (a1() != a2());
1792 }
1793 
1795 
1796 template< typename T >
1797 bool operator!=(const MDArrayRCP< T > & a1, const MDArray< T > & a2)
1798 {
1799  return (a1() != a2());
1800 }
1801 
1803 
1804 template< typename T >
1805 bool operator!=(const MDArrayView< T > & a1,
1806  const MDArrayRCP< T > & a2)
1807 {
1808  return (a1 != a2());
1809 }
1810 
1812 
1813 template< typename T >
1814 bool operator!=(const MDArrayRCP< T > & a1,
1815  const MDArrayView< T > & a2)
1816 {
1817  return (a1() != a2);
1818 }
1819 
1821 
1822 template< typename T >
1823 std::ostream & operator<<(std::ostream & os,
1824  const MDArrayRCP< T > & a)
1825 {
1826  os << a.toString();
1827  return os;
1828 }
1829 
1831 // Private implementations
1833 
1834 template< typename T >
1835 void
1836 MDArrayRCP< T >::assertAxis(int axis) const
1837 {
1838  TEUCHOS_TEST_FOR_EXCEPTION(
1839  !(0 <= axis && axis < _dimensions.size()),
1840  RangeError,
1841  "MDArrayRCP<T>::assertAxis(axis=" << axis << "): out of "
1842  << "range axis in [0, " << _dimensions.size() << ")"
1843  );
1844 }
1845 
1847 
1848 template< typename T >
1849 void
1850 MDArrayRCP< T >::assertIndex(dim_type i, int axis) const
1851 {
1852  TEUCHOS_TEST_FOR_EXCEPTION(
1853  !(0 <= i && i < _dimensions[axis]), RangeError,
1854  "MDArrayRCP<T>::assertIndex(i=" << i << ",axis=" << axis << "): out of "
1855  << "range i in [0, " << _dimensions[axis] << ")"
1856  );
1857 }
1858 
1859 } // End namespace Domi
1860 
1861 #endif // DOMI_MDARRAYRCP_HPP
const_pointer getRawPtr() const
Return a const raw pointer to the beginning of the MDArrayRCP or NULL if unsized. ...
Definition: Domi_MDArrayRCP.hpp:1718
int numDims() const
Return the number of dimensions.
Definition: Domi_MDArrayRCP.hpp:999
reference at(dim_type i,...)
Non-const single element access method with bounds checking.
Definition: Domi_MDArrayRCP.hpp:1603
bool is_null() const
Return true if the underlying pointer is null.
Definition: Domi_MDArrayRCP.hpp:1160
void resize(const Teuchos::ArrayView< dim_type > &dims)
Resize the MDArrayRCP based on the given dimensions.
Definition: Domi_MDArrayRCP.hpp:1684
friend std::ostream & operator<<(std::ostream &os, const MDArrayRCP< T2 > &a)
Stream output operator.
T value_type
Value type.
Definition: Domi_MDArrayRCP.hpp:162
const T & const_reference
Const reference type.
Definition: Domi_MDArrayRCP.hpp:174
std::string toString() const
Convert the MDArrayRCP to a string representation.
Definition: Domi_MDArrayRCP.hpp:1709
dim_type dimension(int axis) const
Return the dimension of the given axis.
Definition: Domi_MDArrayRCP.hpp:1017
reference operator*()
Dereference the underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1178
Iterator class suitable for multi-dimensional arrays.
Definition: Domi_MDIterator.hpp:100
Range Error exception type.
Definition: Domi_Exceptions.hpp:65
MDArrayView< const T > mdArrayViewConst()
Perform an explicit conversion to a non-const MDArrayView&lt;const T&gt;
Definition: Domi_MDArrayRCP.hpp:1215
pointer operator->() const
Pointer -&gt; access to members of underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1169
friend bool operator==(const MDArrayRCP< T2 > &a1, const MDArrayRCP< T2 > &a2)
Equality operator.
static bool hasBoundsChecking()
Return true if MDArrayRCP has been compiled with bounds checking on.
Definition: Domi_MDArrayRCP.hpp:1696
reverse_iterator rend()
Return the ending reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1131
const_iterator cend() const
Return the ending const_iterator.
Definition: Domi_MDArrayView.hpp:1007
T * pointer
Pointer type.
Definition: Domi_MDArrayRCP.hpp:165
pointer get() const
Get the raw C++ pointer to the underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1187
MDArrayRCP< T > & operator=(const MDArrayRCP< T > &r_ptr)
Assignment operator.
Definition: Domi_MDArrayRCP.hpp:985
size_type max_size() const
Return the maximum allowable size for the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1675
const Layout layout() const
Return the storage order.
Definition: Domi_MDArrayRCP.hpp:1056
iterator begin()
Return the beginning iterator.
Definition: Domi_MDArrayRCP.hpp:1065
A Slice contains a start, stop, and step index, describing a subset of an ordered container...
Definition: Domi_Slice.hpp:137
void clear()
Clear the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1652
const Teuchos::ArrayRCP< T > & arrayRCP() const
Return the underlying Teuchos::ArrayRCP
Definition: Domi_MDArrayRCP.hpp:1047
const_iterator cbegin() const
Return the beginning const_iterator.
Definition: Domi_MDArrayView.hpp:998
T & reference
Reference type.
Definition: Domi_MDArrayRCP.hpp:171
MDArrayView< T > operator[](dim_type i)
Sub-array access operator. The returned MDArrayView object will have one fewer dimensions than the ca...
Definition: Domi_MDArrayRCP.hpp:1249
~MDArrayRCP()
Destructor.
Definition: Domi_MDArrayRCP.hpp:977
size_type size() const
Return the total size of the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1029
const_reverse_iterator crbegin() const
Return the beginning const_reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1141
MDArrayView< T > mdArrayView()
Perform an explicit conversion to a non-const MDArrayView&lt;T&gt;
Definition: Domi_MDArrayRCP.hpp:1196
Memory-safe templated multi-dimensional array view class.
Definition: Domi_MDArrayView.hpp:70
iterator end()
Return the ending iterator.
Definition: Domi_MDArrayRCP.hpp:1074
const_iterator cbegin() const
Return the beginning const_iterator.
Definition: Domi_MDArrayRCP.hpp:1103
MDArrayRCP(Teuchos::ENull null_arg=Teuchos::null)
Default constructor.
Definition: Domi_MDArrayRCP.hpp:873
const T * const_pointer
Const pointer type.
Definition: Domi_MDArrayRCP.hpp:168
size_type capacity() const
Return the capacity of the underlying Teuchos::ArrayRCP
Definition: Domi_MDArrayRCP.hpp:1643
bool empty() const
Return whether the MDArrayRCP is empty.
Definition: Domi_MDArrayRCP.hpp:1666
MDArrayView< T > operator()()
Conversion to MDArrayView
Definition: Domi_MDArrayRCP.hpp:1291
const Teuchos::Array< dim_type > & dimensions() const
Return the array of dimensions.
Definition: Domi_MDArrayRCP.hpp:1008
reverse_iterator rbegin()
Return the beginning reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1122
Reverse iterator class suitable for multi-dimensional arrays.
Definition: Domi_MDRevIterator.hpp:98
Memory-safe templated multi-dimensional array class.
Definition: Domi_MDArray.hpp:65
const_reverse_iterator crend() const
Return the ending const_reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1150
const_iterator cend() const
Return the ending const_iterator.
Definition: Domi_MDArrayRCP.hpp:1112
Memory-safe, reference-counted, templated, multi-dimensional array class.
Definition: Domi_MDArrayRCP.hpp:63
friend bool operator!=(const MDArrayRCP< T2 > &a1, const MDArrayRCP< T2 > &a2)
Inequality operator.
const Teuchos::Array< size_type > & strides() const
Return the indexing strides.
Definition: Domi_MDArrayRCP.hpp:1038
void assign(const_reference value)
Assign a value to all elements of the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1593

Generated on Fri Apr 19 2024 09:28:43 for Domi by doxygen 1.8.5