Skip to content

Commit ea9c54c

Browse files
committed
sync
1 parent 75d8670 commit ea9c54c

File tree

6 files changed

+214
-178
lines changed

6 files changed

+214
-178
lines changed

include/maxplus/algebra/mpmatrix.h

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,31 @@
4646
#include <unordered_set>
4747
#include <vector>
4848

49-
using namespace Graphs;
50-
5149
class CString;
5250

5351
namespace MaxPlus {
5452

53+
using namespace Graphs;
54+
5555
/**
5656
* Vector, represents a max-plus column vector.
5757
*/
5858
class Vector {
5959
public:
60-
Vector(unsigned int size = 0, MPTime value = MP_MINUSINFINITY);
60+
explicit Vector(unsigned int size = 0, MPTime value = MP_MINUSINFINITY);
6161

62-
Vector(std::vector<MPTime> *v);
62+
explicit Vector(std::vector<MPTime> *v);
6363

6464
~Vector();
6565

66-
inline unsigned int getSize(void) const { return (unsigned int)this->table.size(); }
66+
Vector(Vector &&) = default;
67+
Vector &operator=(Vector &&) = default;
68+
69+
[[nodiscard]] inline unsigned int getSize() const {
70+
return static_cast<unsigned int>(this->table.size());
71+
}
6772

68-
inline MPTime get(unsigned int row) const { return this->table[row]; }
73+
[[nodiscard]] inline MPTime get(unsigned int row) const { return this->table[row]; }
6974

7075
void put(unsigned int row, MPTime value);
7176

@@ -80,7 +85,7 @@ class Vector {
8085
void negate();
8186
MPTime normalize();
8287

83-
Vector *add(MPTime increase) const;
88+
[[nodiscard]] Vector *add(MPTime increase) const;
8489

8590
void add(MPTime increase, Vector *result) const;
8691

@@ -115,7 +120,7 @@ class Vector {
115120
* itsPosition returns the index of the (a) smallest finite element is set
116121
* to a pointer to unsigned int, otherwise set or defaults to NULL
117122
*/
118-
MPTime minimalFiniteElement(unsigned int *itsPosition_Ptr = NULL) const;
123+
MPTime minimalFiniteElement(unsigned int *itsPosition_Ptr = nullptr) const;
119124

120125
private:
121126
vector<MPTime> table;
@@ -129,7 +134,7 @@ class Matrix {
129134
* Construct a max-plus matrix of \p N by \p N using the default fill pattern (minus infinity).
130135
* @param N Number of rows and columns
131136
*/
132-
Matrix(unsigned int N);
137+
explicit Matrix(unsigned int N);
133138

134139
/**
135140
* Construct a max-plus matrix of \p nrows by \p nr_cols using the given fill pattern.
@@ -141,33 +146,36 @@ class Matrix {
141146

142147
Matrix(unsigned int nrows, unsigned int nr_cols, unsigned int nr_el);
143148

149+
Matrix(Matrix &&) = default;
150+
Matrix &operator=(Matrix &&) = default;
151+
144152
virtual ~Matrix();
145153

146-
inline unsigned int getRows(void) const { return this->szRows; }
154+
[[nodiscard]] inline unsigned int getRows() const { return this->szRows; }
147155

148-
inline unsigned int getCols(void) const { return this->szCols; }
156+
[[nodiscard]] inline unsigned int getCols() const { return this->szCols; }
149157

150-
unsigned int getSize(void) const;
158+
[[nodiscard]] unsigned int getSize() const;
151159

152-
MPTime get(unsigned int row, unsigned int column) const;
153-
Vector getRowVector(unsigned int row) const;
160+
[[nodiscard]] MPTime get(unsigned int row, unsigned int column) const;
161+
[[nodiscard]] Vector getRowVector(unsigned int row) const;
154162

155163
void put(unsigned int row, unsigned int column, MPTime value);
156164

157165
void paste(unsigned int top_row, unsigned int left_column, const Matrix *pastedMatrix);
158166

159167
void pasteRowVector(unsigned int top_row, unsigned int left_column, const Vector *pastedVector);
160168

161-
virtual Matrix *createCopy() const;
169+
[[nodiscard]] virtual Matrix *createCopy() const;
162170

163-
Matrix *getTransposedCopy() const;
171+
[[nodiscard]] Matrix *getTransposedCopy() const;
164172

165-
virtual Matrix *getSubMatrix(const list<unsigned int> &rowIndices,
166-
const list<unsigned int> &colIndices) const;
173+
[[nodiscard]] virtual Matrix *getSubMatrix(const list<unsigned int> &rowIndices,
174+
const list<unsigned int> &colIndices) const;
167175

168-
Matrix *getSubMatrix(const list<unsigned int> &indices) const;
176+
[[nodiscard]] Matrix *getSubMatrix(const list<unsigned int> &indices) const;
169177

170-
Matrix *getSubMatrixNonSquare(const list<unsigned int> &indices) const;
178+
[[nodiscard]] Matrix *getSubMatrixNonSquare(const list<unsigned int> &indices) const;
171179

172180
/**
173181
* Increases the number of rows of the matrix by n and fills the new elements with -\infty.
@@ -179,28 +187,29 @@ class Matrix {
179187
void toLaTeXString(CString &outString, double scale = 1.0) const;
180188

181189
// Algebraic operations.
182-
Matrix *add(MPTime increase) const;
190+
[[nodiscard]] Matrix *add(MPTime increase) const;
183191

184192
void add(MPTime increase, Matrix *result) const;
185193

186-
Matrix *mp_sub(const Matrix &m) const;
194+
[[nodiscard]] Matrix *mp_sub(const Matrix &m) const;
187195

188-
Matrix *mp_maximum(const Matrix &m) const;
196+
[[nodiscard]] Matrix *mp_maximum(const Matrix &m) const;
189197

190-
void maximum(const Matrix *matB, Matrix *result);
198+
void maximum(const Matrix *matB, Matrix *result) const;
191199

192-
Vector *mp_multiply(const Vector &v) const;
200+
[[nodiscard]] Vector *mp_multiply(const Vector &v) const;
193201

194-
Matrix *mp_multiply(const Matrix &m) const;
202+
[[nodiscard]] Matrix *mp_multiply(const Matrix &m) const;
195203

196-
Matrix *mp_power(const unsigned int p) const;
204+
[[nodiscard]] Matrix *mp_power(unsigned int p) const;
197205

198-
CDouble mp_eigenvalue() const;
206+
[[nodiscard]] CDouble mp_eigenvalue() const;
199207

200-
typedef std::list<std::pair<Vector, CDouble>> EigenvectorList;
201-
typedef std::list<std::pair<Vector, Vector>> GeneralizedEigenvectorList;
202-
std::pair<EigenvectorList, GeneralizedEigenvectorList> mp_generalized_eigenvectors() const;
203-
EigenvectorList mpEigenvectors() const;
208+
using EigenvectorList = std::list<std::pair<Vector, CDouble>>;
209+
using GeneralizedEigenvectorList = std::list<std::pair<Vector, Vector>>;
210+
[[nodiscard]] std::pair<EigenvectorList, GeneralizedEigenvectorList>
211+
mp_generalized_eigenvectors() const;
212+
[[nodiscard]] EigenvectorList mpEigenvectors() const;
204213

205214
Matrix &operator+=(MPTime increase) {
206215
this->add(increase, this);
@@ -224,21 +233,22 @@ class Matrix {
224233
* Return the element having the largest abs() value.
225234
* @return element having the largest abs() value
226235
*/
227-
MPTime largestFiniteElement() const;
228-
MPTime minimalFiniteElement() const;
236+
[[nodiscard]] MPTime largestFiniteElement() const;
237+
[[nodiscard]] MPTime minimalFiniteElement() const;
229238

230-
Matrix *plusClosureMatrix(MPTime posCycleThreshold = MP_EPSILON) const;
239+
[[nodiscard]] Matrix *plusClosureMatrix(MPTime posCycleThreshold = MP_EPSILON) const;
231240

232-
Matrix *starClosureMatrix(MPTime posCycleThreshold = MP_EPSILON) const;
241+
[[nodiscard]] Matrix *starClosureMatrix(MPTime posCycleThreshold = MP_EPSILON) const;
233242

234-
Matrix *allPairLongestPathMatrix(MPTime posCycleThreshold, bool implyZeroSelfEdges) const;
243+
[[nodiscard]] Matrix *allPairLongestPathMatrix(MPTime posCycleThreshold,
244+
bool implyZeroSelfEdges) const;
235245
bool
236246
allPairLongestPathMatrix(MPTime posCycleThreshold, bool implyZeroSelfEdges, Matrix &res) const;
237247

238-
MCMgraph mpMatrixToPrecedenceGraph() const;
248+
[[nodiscard]] MCMgraph mpMatrixToPrecedenceGraph() const;
239249

240250
// factory methods
241-
virtual Matrix *makeMatrix(unsigned int nr_rows, unsigned int nr_cols) const;
251+
[[nodiscard]] virtual Matrix *makeMatrix(unsigned int nr_rows, unsigned int nr_cols) const;
242252

243253
private:
244254
// Implicit copying is not allowed
@@ -250,10 +260,8 @@ class Matrix {
250260
void init(MatrixFill fill);
251261
void init();
252262

253-
private:
254263
Matrix();
255264

256-
private:
257265
vector<MPTime> table;
258266
unsigned int szRows;
259267
unsigned int szCols;
@@ -271,11 +279,11 @@ class ExtendedMatrix : public Matrix {
271279
Matrix(nrows, nr_cols, nr_el) {
272280
this->bufferSets.reserve(nr_el);
273281
}
274-
ExtendedMatrix(unsigned int N) : Matrix(N) {}
282+
explicit ExtendedMatrix(unsigned int N) : Matrix(N) {}
275283

276-
virtual Matrix *createCopy() const {
284+
[[nodiscard]] Matrix *createCopy() const override {
277285
Matrix *newMatrix = Matrix::createCopy();
278-
ExtendedMatrix *newExtendedMatrix = dynamic_cast<ExtendedMatrix *>(newMatrix);
286+
auto *newExtendedMatrix = dynamic_cast<ExtendedMatrix *>(newMatrix);
279287

280288
unsigned int nr_els = this->getRows() * this->getCols();
281289
for (unsigned int pos = 0; pos < nr_els; pos++) {
@@ -286,13 +294,13 @@ class ExtendedMatrix : public Matrix {
286294
}
287295

288296
void put(unsigned int row, unsigned int column, MPTime value, std::unordered_set<int> &);
289-
virtual Matrix *getSubMatrix(const list<unsigned int> &rowIndices,
290-
const list<unsigned int> &colIndices) const;
297+
[[nodiscard]] Matrix *getSubMatrix(const list<unsigned int> &rowIndices,
298+
const list<unsigned int> &colIndices) const override;
291299

292300
// factory methods
293-
virtual Matrix *makeMatrix(unsigned int nr_rows, unsigned int nr_cols) const;
301+
[[nodiscard]] Matrix *makeMatrix(unsigned int nr_rows, unsigned int nr_cols) const override;
294302

295-
std::unordered_set<int> getBufferSet(unsigned int row, unsigned int column) const;
303+
[[nodiscard]] std::unordered_set<int> getBufferSet(unsigned int row, unsigned int column) const;
296304

297305
private:
298306
vector<std::unordered_set<int>> bufferSets;
@@ -305,17 +313,20 @@ class ExtendedMatrix : public Matrix {
305313

306314
class VectorList : private std::vector<Vector *> {
307315
public:
308-
VectorList(unsigned int oneVectorSizeInit);
316+
explicit VectorList(unsigned int oneVectorSizeInit);
309317
~VectorList();
310318

311-
const Vector &vectorRefAt(int n) const; // vector at index 'n'
312-
Vector &vectorRefAt(int n);
319+
VectorList(VectorList &&) = default;
320+
VectorList &operator=(VectorList &&) = delete;
313321

314-
const Vector &lastVectorRef() const; // last vector
322+
[[nodiscard]] const Vector &vectorRefAt(unsigned int n) const; // vector at index 'n'
323+
Vector &vectorRefAt(unsigned int n);
324+
325+
[[nodiscard]] const Vector &lastVectorRef() const; // last vector
315326
Vector &lastVectorRef();
316327

317-
unsigned int getSize() const; // vector count
318-
unsigned int getOneVectorSize() const { return this->oneVectorSize; }
328+
[[nodiscard]] unsigned int getSize() const; // vector count
329+
[[nodiscard]] unsigned int getOneVectorSize() const { return this->oneVectorSize; }
319330

320331
void grow(); // append one vector place
321332

@@ -330,7 +341,6 @@ class VectorList : private std::vector<Vector *> {
330341
VectorList(const VectorList &);
331342
VectorList &operator=(const VectorList &);
332343

333-
private:
334344
const unsigned int oneVectorSize;
335345
};
336346

@@ -344,19 +354,21 @@ inline VectorList::~VectorList() {
344354
}
345355
}
346356

347-
inline const Vector &VectorList::vectorRefAt(int n) const { return *this->at(n); }
357+
inline const Vector &VectorList::vectorRefAt(unsigned int n) const { return *this->at(n); }
348358

349-
inline Vector &VectorList::vectorRefAt(int n) { return *this->at(n); }
359+
inline Vector &VectorList::vectorRefAt(unsigned int n) { return *this->at(n); }
350360

351361
inline const Vector &VectorList::lastVectorRef() const { return *this->at(this->size() - 1); }
352362

353363
inline Vector &VectorList::lastVectorRef() { return *this->at(this->size() - 1); }
354364

355-
inline unsigned int VectorList::getSize() const { return (unsigned int)vector<Vector *>::size(); }
365+
inline unsigned int VectorList::getSize() const {
366+
return static_cast<unsigned int>(vector<Vector *>::size());
367+
}
356368

357369
inline void VectorList::grow() {
358-
unsigned int last = (unsigned int)this->size();
359-
this->resize((size_t)(last + 1));
370+
auto last = static_cast<unsigned int>(this->size());
371+
this->resize(last + 1);
360372
this->at(last) = new Vector(oneVectorSize, MP_MINUSINFINITY);
361373
}
362374

0 commit comments

Comments
 (0)