66#include < cassert>
77#include < vector>
88
9+ #include " boost/multi_array.hpp"
910#include " odb/dbStream.h"
1011#include " odb/odb.h"
1112
@@ -18,8 +19,8 @@ class dbMatrix
1819 dbMatrix () = default ;
1920 dbMatrix (uint n, uint m);
2021
21- uint numRows () const { return _n ; }
22- uint numCols () const { return _m ; }
22+ uint numRows () const { return _matrix. shape ()[ 0 ] ; }
23+ uint numCols () const { return _matrix. shape ()[ 1 ] ; }
2324 uint numElems () const ;
2425
2526 void resize (uint n, uint m);
@@ -28,13 +29,13 @@ class dbMatrix
2829 const T& operator ()(uint i, uint j) const ;
2930 T& operator ()(uint i, uint j);
3031
32+ dbMatrix<T>& operator =(const dbMatrix<T>& rhs);
33+
3134 bool operator ==(const dbMatrix<T>& rhs) const ;
3235 bool operator !=(const dbMatrix<T>& rhs) const { return !operator ==(rhs); }
3336
3437 private:
35- uint _n = 0 ;
36- uint _m = 0 ;
37- std::vector<std::vector<T>> _matrix;
38+ boost::multi_array<T, 2 > _matrix;
3839};
3940
4041template <class T >
@@ -74,47 +75,36 @@ inline dbIStream& operator>>(dbIStream& stream, dbMatrix<T>& mat)
7475template <class T >
7576inline dbMatrix<T>::dbMatrix(uint n, uint m)
7677{
77- _n = n;
78- _m = m;
7978 resize (n, m);
8079}
8180
8281template <class T >
8382inline void dbMatrix<T>::clear()
8483{
85- _n = _m = 0 ;
86- _matrix.clear ();
84+ _matrix.resize (boost::extents[0 ][0 ]);
8785}
8886
8987template <class T >
9088inline uint dbMatrix<T>::numElems() const
9189{
92- return _n * _m ;
90+ return _matrix. num_elements () ;
9391}
9492
9593template <class T >
9694inline void dbMatrix<T>::resize(uint n, uint m)
9795{
98- _n = n;
99- _m = m;
100- _matrix.resize (n);
101-
102- for (uint i = 0 ; i < _n; ++i) {
103- _matrix[i].resize (m);
104- }
96+ _matrix.resize (boost::extents[n][m]);
10597}
10698
10799template <class T >
108100inline const T& dbMatrix<T>::operator ()(uint i, uint j) const
109101{
110- assert ((i >= 0 ) && (i < _n) && (j >= 0 ) && (j < _m));
111102 return _matrix[i][j];
112103}
113104
114105template <class T >
115106inline T& dbMatrix<T>::operator ()(uint i, uint j)
116107{
117- assert ((i >= 0 ) && (i < _n) && (j >= 0 ) && (j < _m));
118108 return _matrix[i][j];
119109}
120110
@@ -124,4 +114,23 @@ inline bool dbMatrix<T>::operator==(const dbMatrix<T>& rhs) const
124114 return _matrix == rhs._matrix ;
125115}
126116
117+ template <class T >
118+ inline dbMatrix<T>& dbMatrix<T>::operator =(const dbMatrix<T>& rhs)
119+ {
120+ if (this == &rhs) {
121+ return *this ;
122+ }
123+
124+ const auto lhs_shape = _matrix.shape ();
125+ const auto rhs_shape = rhs._matrix .shape ();
126+
127+ if (lhs_shape[0 ] != rhs_shape[0 ] || lhs_shape[1 ] != rhs_shape[1 ]) {
128+ std::vector<size_t > new_extents{rhs_shape[0 ], rhs_shape[1 ]};
129+ _matrix.resize (new_extents);
130+ }
131+ _matrix = rhs._matrix ;
132+
133+ return *this ;
134+ }
135+
127136} // namespace odb
0 commit comments