Skip to content

Commit 7ee0396

Browse files
committed
refactoring smart pointers
1 parent 7a50d10 commit 7ee0396

File tree

21 files changed

+673
-525
lines changed

21 files changed

+673
-525
lines changed

.vscode/settings.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

include/maxplus/algebra/mpmatrix.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "maxplus/base/analysis/mcm/mcmgraph.h"
4545
#include "mptype.h"
4646
#include <memory>
47-
#include <unordered_set>
4847
#include <vector>
4948

5049
namespace MaxPlus {
@@ -67,11 +66,11 @@ class Vector {
6766
Vector(Vector &&) = default;
6867
Vector &operator=(Vector &&) = delete;
6968

70-
[[nodiscard]] inline unsigned int getSize() const {
69+
[[nodiscard]] unsigned int getSize() const {
7170
return static_cast<unsigned int>(this->table.size());
7271
}
7372

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

7675
void put(unsigned int row, MPTime value);
7776

@@ -127,7 +126,7 @@ class Vector {
127126
std::vector<MPTime> table;
128127
};
129128

130-
enum class MatrixFill { MinusInfinity, Zero, Identity };
129+
enum class MatrixFill { MinusInfinity, Zero, Identity }; // NOLINT(*enum-size)
131130

132131
class Matrix {
133132
public:
@@ -155,9 +154,9 @@ class Matrix {
155154

156155
virtual ~Matrix();
157156

158-
[[nodiscard]] inline unsigned int getRows() const { return this->szRows; }
157+
[[nodiscard]] unsigned int getRows() const { return this->szRows; }
159158

160-
[[nodiscard]] inline unsigned int getCols() const { return this->szCols; }
159+
[[nodiscard]] unsigned int getCols() const { return this->szCols; }
161160

162161
[[nodiscard]] unsigned int getSize() const;
163162

@@ -170,30 +169,30 @@ class Matrix {
170169

171170
void pasteRowVector(unsigned int top_row, unsigned int left_column, const Vector *pastedVector);
172171

173-
[[nodiscard]] virtual std::shared_ptr<Matrix> createCopyPtr() const;
172+
[[nodiscard]] virtual std::unique_ptr<Matrix> createCopyPtr() const;
174173
[[nodiscard]] virtual Matrix createCopy() const;
175174

176-
[[nodiscard]] virtual std::shared_ptr<Matrix> getTransposedCopy() const;
175+
[[nodiscard]] virtual std::unique_ptr<Matrix> getTransposedCopy() const;
177176

178177
[[nodiscard]] Matrix transpose() const;
179178

180179
[[nodiscard]] virtual Matrix getSubMatrix(const std::list<unsigned int> &rowIndices,
181180
const std::list<unsigned int> &colIndices) const;
182181

183-
[[nodiscard]] virtual std::shared_ptr<Matrix>
182+
[[nodiscard]] virtual std::unique_ptr<Matrix>
184183
getSubMatrixPtr(const std::list<unsigned int> &rowIndices,
185184
const std::list<unsigned int> &colIndices) const;
186185

187186
[[nodiscard]] Matrix getSubMatrix(const std::list<unsigned int> &indices) const;
188187

189-
[[nodiscard]] virtual std::shared_ptr<Matrix>
188+
[[nodiscard]] virtual std::unique_ptr<Matrix>
190189
getSubMatrixPtr(const std::list<unsigned int> &indices) const;
191190

192191
[[nodiscard]] Matrix getSubMatrixNonSquare(const std::list<unsigned int> &indices) const;
193192

194193
[[nodiscard]] Matrix getSubMatrixNonSquareRows(const std::list<unsigned int> &rowIndices) const;
195194

196-
[[nodiscard]] virtual std::shared_ptr<Matrix>
195+
[[nodiscard]] virtual std::unique_ptr<Matrix>
197196
getSubMatrixNonSquareRowsPtr(const std::list<unsigned int> &rowIndices) const;
198197

199198
/**
@@ -316,7 +315,7 @@ class VectorList : private std::vector<std::unique_ptr<Vector>> {
316315
// similar - differs by a constant within a threshold
317316

318317
private:
319-
const unsigned int oneVectorSize;
318+
unsigned int oneVectorSize;
320319
};
321320

322321
inline VectorList::VectorList(unsigned int oneVectorSizeInit) : oneVectorSize(oneVectorSizeInit) {
@@ -335,7 +334,7 @@ inline unsigned int VectorList::getSize() const { return static_cast<unsigned in
335334

336335
inline void VectorList::grow() {
337336
auto last = static_cast<unsigned int>(this->size());
338-
this->resize(static_cast<size_t>(last + 1));
337+
this->resize(last + 1);
339338
this->insert(this->begin() + last, std::make_unique<Vector>(oneVectorSize, MP_MINUS_INFINITY));
340339
}
341340

include/maxplus/base/analysis/mcm/mcmgraph.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444

4545
#include "maxplus/base/basic_types.h"
4646
#include <memory>
47-
#include <utility>
4847

49-
using namespace MaxPlus;
5048

5149
namespace MaxPlus::Graphs {
5250
class MCMnode;
@@ -207,7 +205,7 @@ class MCMgraph {
207205
// of nodes and for some edge (w1, d1) there exists a different edge
208206
// (w2, d2) such that d2<=d1 and w2>=w1, then (w2, d2) is removed
209207
// Note this algorithm does currently not distinguish visible and invisible edges!
210-
std::shared_ptr<MCMgraph> pruneEdges();
208+
std::unique_ptr<MCMgraph> pruneEdges();
211209

212210
[[nodiscard]] CDouble calculateMaximumCycleMeanKarp();
213211
[[nodiscard]] CDouble
@@ -216,8 +214,8 @@ class MCMgraph {
216214
[[nodiscard]] CDouble calculateMaximumCycleRatioAndCriticalCycleYoungTarjanOrlin(
217215
std::vector<const MCMedge *> *cycle = nullptr);
218216

219-
[[nodiscard]] std::shared_ptr<MCMgraph> normalize(CDouble mu) const;
220-
[[nodiscard]] std::shared_ptr<MCMgraph> normalize(const std::map<CId, CDouble> &mu) const;
217+
[[nodiscard]] std::unique_ptr<MCMgraph> normalize(CDouble mu) const;
218+
[[nodiscard]] std::unique_ptr<MCMgraph> normalize(const std::map<CId, CDouble> &mu) const;
221219
[[nodiscard]] std::map<CId, CDouble> longestPaths(CId rootNodeId) const;
222220
[[nodiscard]] std::map<CId, CDouble> normalizedLongestPaths(CId rootNodeId, CDouble mu) const;
223221
[[nodiscard]] std::map<CId, CDouble>
@@ -231,7 +229,7 @@ class MCMgraph {
231229
MCMedges edges;
232230
};
233231

234-
using MCMgraphs = std::list<std::shared_ptr<MCMgraph>>;
232+
using MCMgraphs = std::list<std::unique_ptr<MCMgraph>>;
235233
using MCMgraphsIter = MCMgraphs::iterator;
236234

237235
/**

include/maxplus/base/analysis/mcm/mcmhoward.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ namespace MaxPlus::Graphs {
6161
* to a sparse matrix input for Howard's algorithm.
6262
*/
6363
void convertMCMgraphToMatrix(MCMgraph &g,
64-
std::shared_ptr<std::vector<int>> *ij,
65-
std::shared_ptr<std::vector<CDouble>> *A);
64+
std::unique_ptr<std::vector<int>> *ij,
65+
std::unique_ptr<std::vector<CDouble>> *A);
6666

6767
/**
6868
* Howard ()
@@ -88,17 +88,17 @@ void Howard(const std::vector<int> &ij,
8888
const std::vector<CDouble> &A,
8989
int nr_nodes,
9090
int nr_arcs,
91-
std::shared_ptr<std::vector<CDouble>> *chi,
92-
std::shared_ptr<std::vector<CDouble>> *v,
93-
std::shared_ptr<std::vector<int>>(*policy),
91+
std::unique_ptr<std::vector<CDouble>> *chi,
92+
std::unique_ptr<std::vector<CDouble>> *v,
93+
std::unique_ptr<std::vector<int>> *policy,
9494
int *nr_iterations,
9595
int *nr_components);
9696

9797
/**
9898
* maximumCycleMeanHoward ()
9999
* Howard Policy Iteration Algorithm for Max Plus Matrices.
100100
*
101-
* INPUT MCMgraph which must have outoing edges from every node
101+
* INPUT MCMgraph which must have outgoing edges from every node
102102
*
103103
* OUTPUT:
104104
* maximum cycle mean
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Eindhoven University of Technology
3+
* Eindhoven, The Netherlands
4+
* Dept. of Electrical Engineering
5+
* Electronics Systems Group
6+
* Model Based Design Lab (https://computationalmodeling.info/)
7+
*
8+
* Name : mcmkarp.cc
9+
*
10+
* Author : Sander Stuijk (sander@ics.ele.tue.nl)
11+
*
12+
* Date : November 8, 2005
13+
*
14+
* Function : Compute the MCM for an HSDF graph using Karp's
15+
* algorithm.
16+
*
17+
* History :
18+
* 08-11-05 : Initial version.
19+
*
20+
*
21+
* Copyright 2023 Eindhoven University of Technology
22+
*
23+
* Permission is hereby granted, free of charge, to any person obtaining
24+
* a copy of this software and associated documentation files (the “Software”),
25+
* to deal in the Software without restriction, including without limitation
26+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
27+
* and/or sell copies of the Software, and to permit persons to whom the
28+
* Software is furnished to do so, subject to the following conditions:
29+
*
30+
* The above copyright notice and this permission notice shall be included
31+
* in all copies or substantial portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
#ifndef MAXPLUS_BASE_ANALYSIS_MCM_MCMKARP_H_INCLUDED
43+
#define MAXPLUS_BASE_ANALYSIS_MCM_MCMKARP_H_INCLUDED
44+
45+
#include "maxplus/base/analysis/mcm/mcmgraph.h"
46+
47+
namespace MaxPlus::Graphs {
48+
49+
/**
50+
* maximumCycleMeanKarp()
51+
*
52+
* INPUT MCMgraph
53+
*
54+
* OUTPUT:
55+
* maximum cycle mean
56+
*/
57+
CDouble maximumCycleMeanKarp(MCMgraph &mcmGraph);
58+
59+
CDouble maximumCycleMeanKarpGeneral(MCMgraph &g);
60+
61+
/**
62+
* maximumCycleMeanKarpDouble ()
63+
* The function computes the maximum cycle mean of an MCMgGraph using Karp's
64+
* algorithm.
65+
* Note that the following assumptions are made about the MCMgraph
66+
* 1. it is assumed that all nodes in the graph are 'visible'
67+
* 2. it is assumed that the node have id's ranging from 0 up to the number of nodes.
68+
*
69+
* A critical node is only returned if criticalNode is not nullptr.
70+
*/
71+
72+
CDouble maximumCycleMeanKarpDouble(MCMgraph &mcmGraph, const MCMnode **criticalNode = nullptr);
73+
74+
CDouble maximumCycleMeanKarpDoubleGeneral(MCMgraph &g, const MCMnode **criticalNode = nullptr);
75+
76+
} // namespace MaxPlus::Graphs
77+
78+
#endif

include/maxplus/base/analysis/mcm/mcmyto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151
#include "maxplus/base/analysis/mcm/mcmgraph.h"
5252
#include <cstdint>
53-
#include <memory>
5453
#include <vector>
5554

5655
namespace MaxPlus::Graphs {

0 commit comments

Comments
 (0)