Domi
Multi-dimensional, distributed data structures
Loading...
Searching...
No Matches
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
57namespace 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.
63template< typename T > class MDArrayRCP;
64
69template< typename T >
71 const MDArrayRCP< T > & a2);
72
78template< typename T >
79bool operator==(const MDArray< T > & a1,
80 const MDArrayRCP< T > & a2);
81
87template< typename T >
89 const MDArray< T > & a2);
90
96template< typename T >
98 const MDArrayRCP< T > & a2);
99
105template< typename T >
107 const MDArrayView< T > & a2);
108
113template< typename T >
115 const MDArrayRCP< T > & a2);
116
122template< typename T >
123bool operator!=(const MDArray< T > & a1,
124 const MDArrayRCP< T > & a2);
125
131template< typename T >
133 const MDArrayRCP< T > & a2);
134
140template< typename T >
142 const MDArrayView< T > & a2);
143
153template< typename T >
155{
156public:
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
277
283
286 inline ~MDArrayRCP();
287
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
348
352
356
360
364
368
372
376
380
384
386
389
392 inline bool is_null() const;
393
396 inline pointer operator->() const;
397
401
404 inline pointer get() const;
405
407
410
415
420
425
430
434 inline operator MDArrayView< T >() const;
435
439 inline operator MDArrayView< const T >() const;
440
442
445
456
466 const MDArrayView< T > operator[](dim_type i) const;
467
479
491
495
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
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
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
852private:
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
872template< typename T >
873MDArrayRCP< 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
884template< typename T >
885MDArrayRCP< 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),
896 "Teuchos::ArrayView size too small for "
897 "dimensions");
898}
899
901
902template< typename T >
903MDArrayRCP< 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
916template< typename T >
917MDArrayRCP< 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
929template< typename T >
930MDArrayRCP< 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
944template< 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
956template< 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
976template< typename T >
978{
979}
980
982
983template< 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
997template< typename T >
998int
1000{
1001 return _dimensions.size();
1002}
1003
1005
1006template< typename T >
1007const Teuchos::Array< dim_type > &
1009{
1010 return _dimensions;
1011}
1012
1014
1015template< typename T >
1016dim_type
1018{
1019#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1020 assertAxis(axis);
1021#endif
1022 return _dimensions[axis];
1023}
1024
1026
1027template< typename T >
1028size_type
1030{
1031 return _array.size();
1032}
1033
1035
1036template< typename T >
1037const Teuchos::Array< size_type > &
1039{
1040 return _strides;
1041}
1042
1044
1045template< typename T >
1046const Teuchos::ArrayRCP< T > &
1048{
1049 return _array;
1050}
1051
1053
1054template< typename T >
1055const Layout
1057{
1058 return _layout;
1059}
1060
1062
1063template< typename T >
1066{
1067 return iterator(*this);
1068}
1069
1071
1072template< typename T >
1075{
1076 // Return the iterator corresponding to the last element
1077 return iterator(*this, true);
1078}
1079
1081
1082template< typename T >
1085{
1086 return const_iterator(*this);
1087}
1088
1090
1091template< typename T >
1094{
1095 // Return the iterator corresponding to the last element
1096 return const_iterator(*this, true);
1097}
1098
1100
1101template< typename T >
1104{
1105 return const_iterator(*this);
1106}
1107
1109
1110template< typename T >
1113{
1114 // Return the iterator corresponding to the last element
1115 return const_iterator(*this, true);
1116}
1117
1119
1120template< typename T >
1123{
1124 return reverse_iterator(*this);
1125}
1126
1128
1129template< typename T >
1132{
1133 // Return the reverse_iterator corresponding to the last element
1134 return reverse_iterator(*this, true);
1135}
1136
1138
1139template< typename T >
1142{
1143 return const_reverse_iterator(*this);
1144}
1145
1147
1148template< typename T >
1151{
1152 // Return the reverse_iterator corresponding to the last element
1153 return const_reverse_iterator(*this, true);
1154}
1155
1157
1158template< typename T >
1159bool
1161{
1162 return _array.is_null();
1163}
1164
1166
1167template< typename T >
1168T *
1170{
1171 return _array.operator->();
1172}
1173
1175
1176template< typename T >
1177T &
1179{
1180 return _array.operator*();
1181}
1182
1184
1185template< typename T >
1186T *
1188{
1189 return _array.get();
1190}
1191
1193
1194template< typename T >
1197{
1198 return MDArrayView< T >(_array(), _dimensions, _layout);
1199}
1200
1202
1203template< typename T >
1204const MDArrayView< T >
1206{
1207 Teuchos::Array< dim_type > dims(_dimensions);
1208 return MDArrayView< T >(_array(), dims(), _layout);
1209}
1210
1212
1213template< typename T >
1216{
1217 return MDArrayView< const T >(_array(), _dimensions, _layout);
1218}
1219
1221
1222template< typename T >
1225{
1226 return MDArrayView< const T >(_array(), _dimensions, _layout);
1227}
1228
1230
1231template< typename T >
1233{
1234 return mdArrayView();
1235}
1236
1238
1239template< typename T >
1241{
1242 return mdArrayViewConst();
1243}
1244
1246
1247template< 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
1258template< typename T >
1259const 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
1269template< typename T >
1270const MDArrayView< T >
1272{
1273 // Note: Slices produce safe indexes
1274 return mdArrayView()[s];
1275}
1276
1278
1279template< typename T >
1282{
1283 // Note: Slices produce safe indexes
1284 return mdArrayView()[s];
1285}
1286
1288
1289template< typename T >
1292{
1293 return mdArrayView();
1294}
1295
1297
1298template< typename T >
1299const MDArrayView< T >
1301{
1302 return mdArrayView();
1303}
1304
1306
1307template< typename T >
1308T &
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
1323template< typename T >
1324T &
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
1341template< typename T >
1342T &
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
1361template< typename T >
1362T &
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
1384template< typename T >
1385T &
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
1409template< typename T >
1410T &
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
1449template< typename T >
1450const 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
1465template< typename T >
1466const 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
1483template< typename T >
1484const 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
1503template< typename T >
1504const 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
1526template< typename T >
1527const 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
1551template< typename T >
1552const 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
1591template< typename T >
1592void
1594{
1595 for (iterator it = begin(); it != end(); ++it)
1596 *it = value;
1597}
1598
1600
1601template< typename T >
1602T &
1603MDArrayRCP< 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
1621template< typename T >
1622const T &
1623MDArrayRCP< 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
1641template< typename T >
1642size_type
1644{
1645 return _array.capacity();
1646}
1647
1649
1650template< typename T >
1651void
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
1664template< typename T >
1665bool
1667{
1668 return (_array.size() == 0);
1669}
1670
1672
1673template< typename T >
1674size_type
1676{
1677 return _array.max_size();
1678}
1679
1681
1682template< typename T >
1683void
1684MDArrayRCP< 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
1694template< typename T >
1695bool
1697{
1698#ifdef HAVE_DOMI_ARRAY_BOUNDSCHECK
1699 return true;
1700#else
1701 return false;
1702#endif
1703}
1704
1706
1707template< typename T >
1708std::string
1710{
1711 return mdArrayView().toString();
1712}
1713
1715
1716template< typename T >
1717const T *
1719{
1720 return _array.getRawPtr();
1721}
1722
1724
1725template< typename T >
1726T *
1728{
1729 return _array.getRawPtr();
1730}
1731
1733
1734template< typename T >
1735bool operator==(const MDArrayRCP< T > & a1,
1736 const MDArrayRCP< T > & a2)
1737{
1738 return (a1() == a2());
1739}
1740
1742
1743template< typename T >
1744bool operator==(const MDArray< T > & a1,
1745 const MDArrayRCP< T > & a2)
1746{
1747 return (a1() == a2());
1748}
1749
1751
1752template< typename T >
1753bool operator==(const MDArrayRCP< T > & a1,
1754 const MDArray< T > & a2)
1755{
1756 return (a1() == a2());
1757}
1758
1760
1761template< typename T >
1762bool operator==(const MDArrayView< T > & a1,
1763 const MDArrayRCP< T > & a2)
1764{
1765 return (a1 == a2());
1766}
1767
1769
1770template< typename T >
1771bool operator==(const MDArrayRCP< T > & a1,
1772 const MDArrayView< T > & a2)
1773{
1774 return (a1() == a2);
1775}
1776
1778
1779template< typename T >
1780bool operator!=(const MDArrayRCP< T > & a1,
1781 const MDArrayRCP< T > & a2)
1782{
1783 return not (a1 == a2);
1784}
1785
1787
1788template< typename T >
1789bool operator!=(const MDArray< T > & a1, const MDArrayRCP< T > & a2)
1790{
1791 return (a1() != a2());
1792}
1793
1795
1796template< typename T >
1797bool operator!=(const MDArrayRCP< T > & a1, const MDArray< T > & a2)
1798{
1799 return (a1() != a2());
1800}
1801
1803
1804template< typename T >
1805bool operator!=(const MDArrayView< T > & a1,
1806 const MDArrayRCP< T > & a2)
1807{
1808 return (a1 != a2());
1809}
1810
1812
1813template< typename T >
1814bool operator!=(const MDArrayRCP< T > & a1,
1815 const MDArrayView< T > & a2)
1816{
1817 return (a1() != a2);
1818}
1819
1821
1822template< typename T >
1823std::ostream & operator<<(std::ostream & os,
1824 const MDArrayRCP< T > & a)
1825{
1826 os << a.toString();
1827 return os;
1828}
1829
1831// Private implementations
1833
1834template< typename T >
1835void
1836MDArrayRCP< 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
1848template< typename T >
1849void
1850MDArrayRCP< 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
Memory-safe templated multi-dimensional array class.
Definition: Domi_MDArray.hpp:287
Memory-safe, reference-counted, templated, multi-dimensional array class.
Definition: Domi_MDArrayRCP.hpp:155
MDArrayRCP(const Teuchos::ArrayView< T > &array, const Teuchos::ArrayView< dim_type > &dims, Layout layout=DEFAULT_ORDER)
Constructor with Teuchos::ArrayView source, dimensions, and optional storage order flag.
Definition: Domi_MDArrayRCP.hpp:885
void assign(const_reference value)
Assign a value to all elements of the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1593
friend bool operator==(const MDArray< T2 > &a1, const MDArrayRCP< T2 > &a2)
MDArray/MDArrayRCP equality operator.
const MDArrayView< const T > mdArrayViewConst() const
Perform an explicit conversion to a const MDArrayView<const T>
Definition: Domi_MDArrayRCP.hpp:1224
MDArrayRCP(const MDArrayView< T > &source)
Deep copy constructor.
Definition: Domi_MDArrayRCP.hpp:957
const Layout layout() const
Return the storage order.
Definition: Domi_MDArrayRCP.hpp:1056
reverse_iterator rbegin()
Return the beginning reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1122
friend bool operator!=(const MDArrayRCP< T2 > &a1, const MDArray< T2 > &a2)
MDArrayRCP/MDArray inequality operator.
pointer getRawPtr()
Return a raw pointer to the beginning of the MDArrayRCP or NULL if unsized.
Definition: Domi_MDArrayRCP.hpp:1727
iterator end()
Return the ending iterator.
Definition: Domi_MDArrayRCP.hpp:1074
T & operator()(dim_type i, dim_type j)
Non-const 2D element access operator.
Definition: Domi_MDArrayRCP.hpp:1325
friend bool operator!=(const MDArrayRCP< T2 > &a1, const MDArrayView< T2 > &a2)
MDArrayRCP/MDArrayView inequality operator.
const Teuchos::Array< dim_type > & dimensions() const
Return the array of dimensions.
Definition: Domi_MDArrayRCP.hpp:1008
friend bool operator!=(const MDArray< T2 > &a1, const MDArrayRCP< T2 > &a2)
MDArray/MDArrayRCP inequality operator.
friend bool operator==(const MDArrayRCP< T2 > &a1, const MDArray< T2 > &a2)
MDArrayRCP/MDArray equality operator.
bool operator==(const MDArray< T > &a1, const MDArrayRCP< T > &a2)
MDArray/MDArrayRCP equality operator.
Definition: Domi_MDArrayRCP.hpp:1744
const_iterator cend() const
Return the ending const_iterator.
Definition: Domi_MDArrayRCP.hpp:1112
MDArrayView< T > operator()()
Conversion to MDArrayView
Definition: Domi_MDArrayRCP.hpp:1291
MDArrayView< T > mdArrayView()
Perform an explicit conversion to a non-const MDArrayView<T>
Definition: Domi_MDArrayRCP.hpp:1196
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_MDArrayRCP.hpp:1528
friend bool operator==(const MDArrayView< T2 > &a1, const MDArrayRCP< T2 > &a2)
MDArrayView/MDArrayRCP equality operator.
const T & operator()(dim_type i, dim_type j, dim_type k) const
Const 3D element access operator.
Definition: Domi_MDArrayRCP.hpp:1485
pointer operator->() const
Pointer -> access to members of underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1169
size_type capacity() const
Return the capacity of the underlying Teuchos::ArrayRCP
Definition: Domi_MDArrayRCP.hpp:1643
MDArrayRCP(const MDArrayRCP< T > &r_ptr)
Shallow copy constructor.
Definition: Domi_MDArrayRCP.hpp:945
bool operator!=(const MDArray< T > &a1, const MDArrayRCP< T > &a2)
MDArray/MDArrayRCP inequality operator.
Definition: Domi_MDArrayRCP.hpp:1789
int numDims() const
Return the number of dimensions.
Definition: Domi_MDArrayRCP.hpp:999
const MDArrayView< T > mdArrayView() const
Perform an explicit conversion to a const MDArrayView<T>
Definition: Domi_MDArrayRCP.hpp:1205
bool operator!=(const MDArrayRCP< T > &a1, const MDArrayRCP< T > &a2)
Inequality operator.
Definition: Domi_MDArrayRCP.hpp:1780
friend bool operator==(const MDArrayRCP< T2 > &a1, const MDArrayView< T2 > &a2)
MDArrayRCP/MDArrayView equality operator.
const_reverse_iterator crend() const
Return the ending const_reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1150
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_MDArrayRCP.hpp:1411
const_reverse_iterator crbegin() const
Return the beginning const_reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1141
const MDArrayView< T > operator()() const
Conversion to const MDArrayView
Definition: Domi_MDArrayRCP.hpp:1300
bool operator!=(const MDArrayView< T > &a1, const MDArrayRCP< T > &a2)
MDArrayView/MDArrayRCP inequality operator.
Definition: Domi_MDArrayRCP.hpp:1805
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_MDArrayRCP.hpp:1553
friend bool operator!=(const MDArrayRCP< T2 > &a1, const MDArrayRCP< T2 > &a2)
Inequality operator.
iterator begin()
Return the beginning iterator.
Definition: Domi_MDArrayRCP.hpp:1065
bool operator==(const MDArrayView< T > &a1, const MDArrayRCP< T > &a2)
MDArrayView/MDArrayRCP equality operator.
Definition: Domi_MDArrayRCP.hpp:1762
bool operator==(const MDArrayRCP< T > &a1, const MDArrayView< T > &a2)
MDArrayRCP/MDArrayView equality operator.
Definition: Domi_MDArrayRCP.hpp:1771
const T & operator()(dim_type i) const
Const 1D element access operator.
Definition: Domi_MDArrayRCP.hpp:1451
T & operator()(dim_type i)
Non-const 1D element access operator.
Definition: Domi_MDArrayRCP.hpp:1309
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
friend bool operator!=(const MDArrayView< T2 > &a1, const MDArrayRCP< T2 > &a2)
MDArrayView/MDArrayRCP inequality operator.
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_MDArrayRCP.hpp:1260
T value_type
Value type.
Definition: Domi_MDArrayRCP.hpp:162
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_MDArrayRCP.hpp:1386
T * pointer
Pointer type.
Definition: Domi_MDArrayRCP.hpp:165
const Teuchos::Array< size_type > & strides() const
Return the indexing strides.
Definition: Domi_MDArrayRCP.hpp:1038
friend bool operator==(const MDArrayRCP< T2 > &a1, const MDArrayRCP< T2 > &a2)
Equality operator.
bool is_null() const
Return true if the underlying pointer is null.
Definition: Domi_MDArrayRCP.hpp:1160
MDArrayRCP(const Teuchos::ArrayView< dim_type > &dims, Layout layout)
Constructor with dimensions and storage order flag.
Definition: Domi_MDArrayRCP.hpp:917
friend std::ostream & operator<<(std::ostream &os, const MDArrayRCP< T2 > &a)
Stream output operator.
MDArrayRCP(const Teuchos::ArrayView< dim_type > &dims, const_reference val=T(), Layout layout=DEFAULT_ORDER)
Constructor with dimensions, default value and optional storage order flag.
Definition: Domi_MDArrayRCP.hpp:903
MDArrayRCP(Teuchos::ENull null_arg=Teuchos::null)
Default constructor.
Definition: Domi_MDArrayRCP.hpp:873
reference at(dim_type i,...)
Non-const single element access method with bounds checking.
Definition: Domi_MDArrayRCP.hpp:1603
T & reference
Reference type.
Definition: Domi_MDArrayRCP.hpp:171
const_pointer getRawPtr() const
Return a const raw pointer to the beginning of the MDArrayRCP or NULL if unsized.
Definition: Domi_MDArrayRCP.hpp:1718
const T * const_pointer
Const pointer type.
Definition: Domi_MDArrayRCP.hpp:168
static bool hasBoundsChecking()
Return true if MDArrayRCP has been compiled with bounds checking on.
Definition: Domi_MDArrayRCP.hpp:1696
reference operator*()
Dereference the underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1178
std::string toString() const
Convert the MDArrayRCP to a string representation.
Definition: Domi_MDArrayRCP.hpp:1709
pointer get() const
Get the raw C++ pointer to the underlying data buffer.
Definition: Domi_MDArrayRCP.hpp:1187
const_reference at(dim_type i,...) const
Const single element access method with bounds checking.
Definition: Domi_MDArrayRCP.hpp:1623
void clear()
Clear the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1652
bool operator==(const MDArrayRCP< T > &a1, const MDArrayRCP< T > &a2)
Equality operator.
Definition: Domi_MDArrayRCP.hpp:1735
const T & const_reference
Const reference type.
Definition: Domi_MDArrayRCP.hpp:174
T & operator()(dim_type i, dim_type j, dim_type k)
Non-const 3D element access operator.
Definition: Domi_MDArrayRCP.hpp:1343
const T & operator()(dim_type i, dim_type j) const
Const 2D element access operator.
Definition: Domi_MDArrayRCP.hpp:1467
T & operator()(dim_type i, dim_type j, dim_type k, dim_type m)
Non-const 4D element access operator.
Definition: Domi_MDArrayRCP.hpp:1363
MDArrayView< T > operator[](Slice s)
Sub-array access operator. The returned MDArrayView object will have the same number of dimensions as...
Definition: Domi_MDArrayRCP.hpp:1281
bool empty() const
Return whether the MDArrayRCP is empty.
Definition: Domi_MDArrayRCP.hpp:1666
const_iterator end() const
Return the ending const_iterator.
Definition: Domi_MDArrayRCP.hpp:1093
const_iterator cbegin() const
Return the beginning const_iterator.
Definition: Domi_MDArrayRCP.hpp:1103
void resize(const Teuchos::ArrayView< dim_type > &dims)
Resize the MDArrayRCP based on the given dimensions.
Definition: Domi_MDArrayRCP.hpp:1684
MDArrayRCP(const Teuchos::ArrayView< dim_type > &dims, const Teuchos::ArrayView< size_type > &strides, T *data, Layout layout=DEFAULT_ORDER)
Low-level view constructor.
Definition: Domi_MDArrayRCP.hpp:930
size_type max_size() const
Return the maximum allowable size for the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1675
bool operator==(const MDArrayRCP< T > &a1, const MDArray< T > &a2)
MDArrayRCP/MDArray equality operator.
Definition: Domi_MDArrayRCP.hpp:1753
const_iterator begin() const
Return the beginning const_iterator.
Definition: Domi_MDArrayRCP.hpp:1084
const Teuchos::ArrayRCP< T > & arrayRCP() const
Return the underlying Teuchos::ArrayRCP
Definition: Domi_MDArrayRCP.hpp:1047
~MDArrayRCP()
Destructor.
Definition: Domi_MDArrayRCP.hpp:977
reverse_iterator rend()
Return the ending reverse_iterator.
Definition: Domi_MDArrayRCP.hpp:1131
MDArrayView< const T > mdArrayViewConst()
Perform an explicit conversion to a non-const MDArrayView<const T>
Definition: Domi_MDArrayRCP.hpp:1215
const T & operator()(dim_type i, dim_type j, dim_type k, dim_type m) const
Const 4D element access operator.
Definition: Domi_MDArrayRCP.hpp:1505
dim_type dimension(int axis) const
Return the dimension of the given axis.
Definition: Domi_MDArrayRCP.hpp:1017
size_type size() const
Return the total size of the MDArrayRCP
Definition: Domi_MDArrayRCP.hpp:1029
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_MDArrayRCP.hpp:1271
bool operator!=(const MDArrayRCP< T > &a1, const MDArrayView< T > &a2)
MDArrayRCP/MDArrayView inequality operator.
Definition: Domi_MDArrayRCP.hpp:1814
MDArrayRCP< T > & operator=(const MDArrayRCP< T > &r_ptr)
Assignment operator.
Definition: Domi_MDArrayRCP.hpp:985
Memory-safe templated multi-dimensional array view class.
Definition: Domi_MDArrayView.hpp:115
const_iterator cend() const
Return the ending const_iterator.
Definition: Domi_MDArrayView.hpp:1007
const_iterator cbegin() const
Return the beginning const_iterator.
Definition: Domi_MDArrayView.hpp:998
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

Generated for Domi by doxygen 1.9.6