Skip to content

Commit b1d614f

Browse files
authored
Merge pull request #8379 from The-OpenROAD-Project-staging/odb-matrix
odb: in dbMatrix switch to boost::multi_array
2 parents bc7759f + b210469 commit b1d614f

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/odb/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ cc_library(
6868
"@boost.integer",
6969
"@boost.iostreams",
7070
"@boost.lambda",
71+
"@boost.multi_array",
7172
"@boost.optional",
7273
"@boost.phoenix",
7374
"@boost.polygon",
@@ -133,6 +134,7 @@ cc_library(
133134
"//src/utl",
134135
"@boost.container",
135136
"@boost.geometry",
137+
"@boost.multi_array",
136138
"@boost.polygon",
137139
"@boost.stacktrace",
138140
"@tk_tcl//:tcl",

src/odb/include/odb/dbMatrix.h

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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

4041
template <class T>
@@ -74,47 +75,36 @@ inline dbIStream& operator>>(dbIStream& stream, dbMatrix<T>& mat)
7475
template <class T>
7576
inline dbMatrix<T>::dbMatrix(uint n, uint m)
7677
{
77-
_n = n;
78-
_m = m;
7978
resize(n, m);
8079
}
8180

8281
template <class T>
8382
inline void dbMatrix<T>::clear()
8483
{
85-
_n = _m = 0;
86-
_matrix.clear();
84+
_matrix.resize(boost::extents[0][0]);
8785
}
8886

8987
template <class T>
9088
inline uint dbMatrix<T>::numElems() const
9189
{
92-
return _n * _m;
90+
return _matrix.num_elements();
9391
}
9492

9593
template <class T>
9694
inline 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

10799
template <class T>
108100
inline 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

114105
template <class T>
115106
inline 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

Comments
 (0)