Skip to content

Commit 4247b75

Browse files
committed
sync
1 parent 74fb8f2 commit 4247b75

File tree

9 files changed

+261
-265
lines changed

9 files changed

+261
-265
lines changed

include/maxplus/algebra/mpmatrix.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@
4343

4444
#include "base/analysis/mcm/mcmgraph.h"
4545
#include "mptype.h"
46+
#include <memory>
4647
#include <unordered_set>
4748
#include <vector>
4849

50+
4951
class CString;
5052

5153
namespace MaxPlus {
@@ -311,13 +313,18 @@ class ExtendedMatrix : public Matrix {
311313
* More efficient than vector<MaxPlus::Vector>
312314
****************************************************/
313315

314-
class VectorList : private std::vector<Vector *> {
316+
class VectorList : private std::vector<std::unique_ptr<Vector>> {
315317
public:
316318
explicit VectorList(unsigned int oneVectorSizeInit);
317-
~VectorList();
319+
~VectorList() = default;
318320

319321
VectorList(VectorList &&) = default;
320322
VectorList &operator=(VectorList &&) = delete;
323+
324+
// Implicit copying is not allowed
325+
// => Intentionally private and not implemented
326+
VectorList(const VectorList &)=delete;
327+
VectorList &operator=(const VectorList &)=delete;
321328

322329
[[nodiscard]] const Vector &vectorRefAt(unsigned int n) const; // vector at index 'n'
323330
Vector &vectorRefAt(unsigned int n);
@@ -336,24 +343,13 @@ class VectorList : private std::vector<Vector *> {
336343
// similar - differs by a constant within a threshold
337344

338345
private:
339-
// Implicit copying is not allowed
340-
// => Intentionally private and not implemented
341-
VectorList(const VectorList &);
342-
VectorList &operator=(const VectorList &);
343-
344346
const unsigned int oneVectorSize;
345347
};
346348

347349
inline VectorList::VectorList(unsigned int oneVectorSizeInit) : oneVectorSize(oneVectorSizeInit) {
348350
assert(oneVectorSize > 0);
349351
}
350352

351-
inline VectorList::~VectorList() {
352-
for (unsigned int pos = 0; pos < size(); pos++) {
353-
delete this->at(pos);
354-
}
355-
}
356-
357353
inline const Vector &VectorList::vectorRefAt(unsigned int n) const { return *this->at(n); }
358354

359355
inline Vector &VectorList::vectorRefAt(unsigned int n) { return *this->at(n); }
@@ -363,13 +359,13 @@ inline const Vector &VectorList::lastVectorRef() const { return *this->at(this->
363359
inline Vector &VectorList::lastVectorRef() { return *this->at(this->size() - 1); }
364360

365361
inline unsigned int VectorList::getSize() const {
366-
return static_cast<unsigned int>(vector<Vector *>::size());
362+
return static_cast<unsigned int>(this->size());
367363
}
368364

369365
inline void VectorList::grow() {
370366
auto last = static_cast<unsigned int>(this->size());
371367
this->resize(last + 1);
372-
this->at(last) = new Vector(oneVectorSize, MP_MINUSINFINITY);
368+
this->insert(this->begin()+last, std::make_unique<Vector>(oneVectorSize, MP_MINUSINFINITY));
373369
}
374370

375371
} // namespace MaxPlus

include/maxplus/algebra/mptype.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ using MPDelay = MPTime;
8787
// MP_MAX()
8888
//==============================
8989

90-
inline MPTime MP_MAX(MPTime a, MPTime b) { return (((CDouble)a) > ((CDouble)b)) ? (a) : (b); }
90+
inline MPTime MP_MAX(MPTime a, MPTime b) { return (static_cast<CDouble>(a)) > (static_cast<CDouble>(b)) ? (a) : (b); }
9191

9292
inline MPTime MP_MAX(CDouble a, MPTime b) { return MP_MAX(MPTime(a), b); }
9393

@@ -99,7 +99,7 @@ inline CDouble MP_MAX(CDouble a, CDouble b) { return CDouble(MP_MAX(MPTime(a), M
9999
// MP_MIN()
100100
//==============================
101101

102-
inline MPTime MP_MIN(MPTime a, MPTime b) { return (((CDouble)a) < ((CDouble)b)) ? (a) : (b); }
102+
inline MPTime MP_MIN(MPTime a, MPTime b) { return (static_cast<CDouble>(a)) < (static_cast<CDouble>(b)) ? (a) : (b); }
103103

104104
inline MPTime MP_MIN(CDouble a, MPTime b) { return MP_MIN(MPTime(a), b); }
105105

@@ -168,9 +168,9 @@ inline MPTime operator*(MPTime a, MPTime b) {
168168
return MPTime(static_cast<CDouble>(a) * static_cast<CDouble>(b));
169169
}
170170

171-
inline const MPTime operator*(CDouble a, MPTime b) { return MPTime(a) * MPTime(b); }
171+
inline MPTime operator*(CDouble a, MPTime b) { return MPTime(a) * MPTime(b); }
172172

173-
inline const MPTime operator*(MPTime a, CDouble b) { return MPTime(a) * MPTime(b); }
173+
inline MPTime operator*(MPTime a, CDouble b) { return MPTime(a) * MPTime(b); }
174174

175175
inline MPTime &MPTime::operator+=(MPTime a) {
176176
*this = *this + a;
@@ -179,7 +179,7 @@ inline MPTime &MPTime::operator+=(MPTime a) {
179179

180180
inline MPTime &MPTime::operator-=(MPTime a) {
181181
assert(!a.isMinusInfinity());
182-
*this = *this + MPTime(CDouble(-a));
182+
*this = *this + (-a);
183183
return *this;
184184
}
185185

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ namespace Graphs {
5454
* The function computes the maximum cycle mean of an MCMgraph using Karp's
5555
* algorithm.
5656
*/
57-
CDouble maximumCycleMeanKarp(MCMgraph *g);
58-
CDouble maximumCycleMeanKarpDouble(MCMgraph *g, MCMnode **criticalNode = NULL);
57+
CDouble maximumCycleMeanKarp(const MCMgraph& g);
58+
CDouble maximumCycleMeanKarpDouble(const MCMgraph& g, const MCMnode *criticalNode = nullptr);
5959

6060
/**
6161
* mcmGetAdjacentActors ()

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

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#define BASE_ANALYSIS_MCM_MCMGRAPH_H_INCLUDED
4444

4545
#include "base/basic_types.h"
46+
#include <memory>
47+
#include <utility>
4648

4749
namespace Graphs {
4850
class MCMnode;
@@ -53,15 +55,15 @@ class MCMedge {
5355
MCMedge(CId eId, bool eVisible);
5456
CId id;
5557
bool visible;
56-
MCMnode *src;
57-
MCMnode *dst;
58+
std::shared_ptr<MCMnode> src;
59+
std::shared_ptr<MCMnode> dst;
5860
CDouble w;
5961
CDouble d;
6062
};
6163

62-
typedef list<MCMedge *> MCMedges;
63-
typedef MCMedges::iterator MCMedgesIter;
64-
typedef MCMedges::const_iterator MCMedgesCIter;
64+
using MCMedges = list<std::shared_ptr<MCMedge>>;
65+
using MCMedgesIter = MCMedges::iterator;
66+
using MCMedgesCIter = MCMedges::const_iterator;
6567

6668
class MCMnode {
6769
public:
@@ -74,12 +76,12 @@ class MCMnode {
7476
};
7577

7678
struct MCMNodeLess {
77-
bool operator()(MCMnode *const &lhs, MCMnode *const &rhs) const { return lhs->id < rhs->id; };
79+
bool operator()(std::shared_ptr<MCMnode> const &lhs, std::shared_ptr<MCMnode> const &rhs) const { return lhs->id < rhs->id; };
7880
};
7981

80-
typedef list<MCMnode *> MCMnodes;
81-
typedef MCMnodes::iterator MCMnodesIter;
82-
typedef MCMnodes::const_iterator MCMnodesCIter;
82+
using MCMnodes = list<std::shared_ptr<MCMnode>>;
83+
using MCMnodesIter = MCMnodes::iterator;
84+
using MCMnodesCIter = MCMnodes::const_iterator;
8385

8486
class MCMgraph {
8587
public:
@@ -92,60 +94,69 @@ class MCMgraph {
9294
// Copy Constructor
9395
MCMgraph(const MCMgraph &g);
9496

95-
const MCMnodes &getNodes() { return nodes; };
97+
const MCMnodes &getNodes() const { return nodes; };
9698

97-
uint nrVisibleNodes() {
99+
uint nrVisibleNodes() const {
98100
uint nrNodes = 0;
99-
for (MCMnodesIter iter = nodes.begin(); iter != nodes.end(); iter++)
100-
if ((*iter)->visible)
101+
for (auto &node : nodes) {
102+
if (node->visible) {
101103
nrNodes++;
104+
}
105+
}
102106
return nrNodes;
103107
};
104-
MCMnode *getNode(CId id) {
105-
for (MCMnodes::iterator i = nodes.begin(); i != nodes.end(); i++)
106-
if ((*i)->id == id)
107-
return (*i);
108-
return NULL;
108+
std::shared_ptr<MCMnode> getNode(CId id) {
109+
for (auto &node : nodes) {
110+
if (node->id == id) {
111+
return node;
112+
}
113+
}
114+
return nullptr;
109115
};
110116

111-
const MCMedges &getEdges() { return edges; };
117+
const MCMedges &getEdges() const { return edges; };
112118

113-
MCMedge *getEdge(CId id) {
114-
for (MCMedges::iterator i = edges.begin(); i != edges.end(); i++)
115-
if ((*i)->id == id)
116-
return (*i);
117-
return NULL;
119+
std::shared_ptr<MCMedge> getEdge(CId id) {
120+
for (auto &edge : edges) {
121+
if (edge->id == id) {
122+
return edge;
123+
}
124+
}
125+
return nullptr;
118126
};
119127

120-
MCMedge *getEdge(CId srcId, CId dstId) {
121-
for (MCMedges::iterator i = edges.begin(); i != edges.end(); i++)
122-
if ((*i)->src->id == srcId) {
123-
if ((*i)->dst->id == dstId) {
124-
return (*i);
128+
std::shared_ptr<MCMedge> getEdge(CId srcId, CId dstId) {
129+
for (auto &edge : edges) {
130+
if (edge->src->id == srcId) {
131+
if (edge->dst->id == dstId) {
132+
return edge;
125133
}
126134
}
127-
return NULL;
135+
}
136+
return nullptr;
128137
};
129138

130-
uint nrVisibleEdges() {
139+
[[nodiscard]] uint nrVisibleEdges() const {
131140
uint nrEdges = 0;
132-
for (MCMedgesIter iter = edges.begin(); iter != edges.end(); iter++)
133-
if ((*iter)->visible)
141+
for (const auto &edge : edges) {
142+
if (edge->visible) {
134143
nrEdges++;
144+
}
145+
}
135146
return nrEdges;
136147
};
137148

138149
// Construction
139150

140151
// Add a node to the MCM graph
141-
void addNode(MCMnode *n) {
152+
void addNode(const std::shared_ptr<MCMnode> &n) {
142153
// Add the node to the MCM graph
143154
this->nodes.push_back(n);
144155
}
145156

146157
// Remove a node from the MCMgraph.
147158
// Note: containers of nodes are lists, so remove is expensive!
148-
void removeNode(MCMnode *n) {
159+
void removeNode(const std::shared_ptr<MCMnode> &n) {
149160
// remove any remaining edges
150161
while (!n->in.empty()) {
151162
this->removeEdge(*(n->in.begin()));
@@ -158,26 +169,30 @@ class MCMgraph {
158169
}
159170

160171
// Add an edge to the MCMgraph.
161-
MCMedge *addEdge(CId id, MCMnode *src, MCMnode *dst, CDouble w, CDouble d) {
162-
MCMedge *e = new MCMedge(id, true);
163-
e->src = src;
164-
e->dst = dst;
172+
std::shared_ptr<MCMedge> addEdge(CId id,
173+
std::shared_ptr<MCMnode> src,
174+
std::shared_ptr<MCMnode> dst,
175+
CDouble w,
176+
CDouble d) {
177+
std::shared_ptr<MCMedge> e = std::make_shared<MCMedge>(id, true);
178+
e->src = std::move(src);
179+
e->dst = std::move(dst);
165180
e->w = w;
166181
e->d = d;
167182
this->addEdge(e);
168183
return e;
169184
}
170185

171186
// Add an edge to the MCMgraph.
172-
void addEdge(MCMedge *e) {
187+
void addEdge(std::shared_ptr<MCMedge> e) {
173188
this->edges.push_back(e);
174189
e->src->out.push_back(e);
175190
e->dst->in.push_back(e);
176191
}
177192

178193
// Remove an edge from the MCMgraph.
179194
// Note: containers of edges are lists, so remove is expensive!
180-
void removeEdge(MCMedge *e) {
195+
void removeEdge(std::shared_ptr<MCMedge> e) {
181196
this->edges.remove(e);
182197
e->src->out.remove(e);
183198
e->dst->in.remove(e);
@@ -190,19 +205,19 @@ class MCMgraph {
190205
// of nodes and for some edge (w1, d1) there exists a different edge
191206
// (w2, d2) such that d2<=d1 and w2>=w1, then (w2, d2) is removed
192207
// Note this algorithm does currently not distinguish visible and invisible edges!
193-
MCMgraph *pruneEdges(void);
208+
std::shared_ptr<MCMgraph> pruneEdges();
194209

195-
CDouble calculateMaximumCycleMeanKarp();
196-
CDouble calculateMaximumCycleMeanKarpDouble(MCMnode **criticalNode = NULL);
210+
[[nodiscard]] CDouble calculateMaximumCycleMeanKarp() const;
211+
[[nodiscard]] CDouble calculateMaximumCycleMeanKarpDouble(const MCMnode *criticalNode = nullptr) const;
197212

198-
CDouble calculateMaximumCycleRatioAndCriticalCycleYoungTarjanOrlin(MCMedge ***cycle = NULL,
199-
uint *len = NULL);
213+
[[nodiscard]] CDouble calculateMaximumCycleRatioAndCriticalCycleYoungTarjanOrlin(std::shared_ptr<MCMedge> **cycle = nullptr,
214+
uint *len = nullptr);
200215

201-
MCMgraph normalize(CDouble mu) const;
202-
MCMgraph normalize(const std::map<CId, CDouble> &mu) const;
203-
std::map<CId, CDouble> longestPaths(const CId rootNodeId) const;
204-
std::map<CId, CDouble> normalizedLongestPaths(const CId rootNodeId, const CDouble mu) const;
205-
std::map<CId, CDouble> normalizedLongestPaths(const CId rootNodeId,
216+
[[nodiscard]] MCMgraph normalize(CDouble mu) const;
217+
[[nodiscard]] MCMgraph normalize(const std::map<CId, CDouble> &mu) const;
218+
[[nodiscard]] std::map<CId, CDouble> longestPaths( CId rootNodeId) const;
219+
[[nodiscard]] std::map<CId, CDouble> normalizedLongestPaths( CId rootNodeId, CDouble mu) const;
220+
[[nodiscard]] std::map<CId, CDouble> normalizedLongestPaths( CId rootNodeId,
206221
const std::map<CId, CDouble> &) const;
207222

208223
private:
@@ -213,8 +228,8 @@ class MCMgraph {
213228
MCMedges edges;
214229
};
215230

216-
typedef list<MCMgraph *> MCMgraphs;
217-
typedef MCMgraphs::iterator MCMgraphsIter;
231+
using MCMgraphs = list<std::shared_ptr<MCMgraph>>;
232+
using MCMgraphsIter = MCMgraphs::iterator;
218233

219234
/**
220235
* Extract the strongly connected components from the graph. These components
@@ -227,7 +242,7 @@ typedef MCMgraphs::iterator MCMgraphsIter;
227242
* MCM algorithms work also on this graph (which reduces the execution time
228243
* needed in some of the conversion algorithms).
229244
*/
230-
void stronglyConnectedMCMgraph(MCMgraph *g,
245+
void stronglyConnectedMCMgraph(const MCMgraph& g,
231246
MCMgraphs &components,
232247
bool includeComponentsWithoutEdges = false);
233248

@@ -236,7 +251,7 @@ void stronglyConnectedMCMgraph(MCMgraph *g,
236251
* The function removes all hidden nodes and edges from the graph. All visible
237252
* edges are assigned a new id starting in the range [0,nrNodes()).
238253
*/
239-
void relabelMCMgraph(MCMgraph *g);
254+
void relabelMCMgraph(std::shared_ptr<MCMgraph> g);
240255

241256
/**
242257
* addLongestDelayEdgesToMCMgraph ()
@@ -245,7 +260,7 @@ void relabelMCMgraph(MCMgraph *g);
245260
* with no delay are removed and edges with more then one delay element
246261
* are converted into a sequence of edges with one delay element.
247262
*/
248-
void addLongestDelayEdgesToMCMgraph(MCMgraph *g);
263+
void addLongestDelayEdgesToMCMgraph(std::shared_ptr<MCMgraph> g);
249264

250265
} // namespace Graphs
251266
#endif

0 commit comments

Comments
 (0)