4343#define BASE_ANALYSIS_MCM_MCMGRAPH_H_INCLUDED
4444
4545#include " base/basic_types.h"
46+ #include < memory>
47+ #include < utility>
4648
4749namespace Graphs {
4850class 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
6668class MCMnode {
6769public:
@@ -74,12 +76,12 @@ class MCMnode {
7476};
7577
7678struct 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
8486class MCMgraph {
8587public:
@@ -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
208223private:
@@ -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