[946] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[946] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_GRAPH_UTILS_H |
---|
| 20 | #define LEMON_GRAPH_UTILS_H |
---|
| 21 | |
---|
| 22 | #include <iterator> |
---|
[1419] | 23 | #include <vector> |
---|
[1402] | 24 | #include <map> |
---|
[1695] | 25 | #include <cmath> |
---|
[946] | 26 | |
---|
[1993] | 27 | #include <lemon/bits/invalid.h> |
---|
| 28 | #include <lemon/bits/utility.h> |
---|
[1413] | 29 | #include <lemon/maps.h> |
---|
[1993] | 30 | #include <lemon/bits/traits.h> |
---|
[1990] | 31 | |
---|
[1459] | 32 | #include <lemon/bits/alteration_notifier.h> |
---|
[1990] | 33 | #include <lemon/bits/default_map.h> |
---|
[946] | 34 | |
---|
[947] | 35 | ///\ingroup gutils |
---|
[946] | 36 | ///\file |
---|
[947] | 37 | ///\brief Graph utilities. |
---|
[946] | 38 | /// |
---|
[964] | 39 | /// |
---|
[946] | 40 | |
---|
| 41 | |
---|
| 42 | namespace lemon { |
---|
| 43 | |
---|
[1267] | 44 | /// \addtogroup gutils |
---|
| 45 | /// @{ |
---|
[947] | 46 | |
---|
[1756] | 47 | ///Creates convenience typedefs for the graph types and iterators |
---|
| 48 | |
---|
| 49 | ///This \c \#define creates convenience typedefs for the following types |
---|
| 50 | ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt, |
---|
[2031] | 51 | ///\c OutEdgeIt |
---|
[1756] | 52 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 53 | ///\code |
---|
| 54 | /// GRAPH_TYPEDEFS(typename G) |
---|
| 55 | ///\endcode |
---|
| 56 | /// |
---|
| 57 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 58 | ///template typedefs in C++. |
---|
[1804] | 59 | #define GRAPH_TYPEDEFS(Graph) \ |
---|
| 60 | typedef Graph:: Node Node; \ |
---|
| 61 | typedef Graph:: NodeIt NodeIt; \ |
---|
| 62 | typedef Graph:: Edge Edge; \ |
---|
| 63 | typedef Graph:: EdgeIt EdgeIt; \ |
---|
| 64 | typedef Graph:: InEdgeIt InEdgeIt; \ |
---|
[1811] | 65 | typedef Graph::OutEdgeIt OutEdgeIt; |
---|
[2031] | 66 | |
---|
[1756] | 67 | ///Creates convenience typedefs for the undirected graph types and iterators |
---|
| 68 | |
---|
| 69 | ///This \c \#define creates the same convenience typedefs as defined by |
---|
| 70 | ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates |
---|
[1909] | 71 | ///\c UEdge, \c UEdgeIt, \c IncEdgeIt, |
---|
[1756] | 72 | /// |
---|
| 73 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 74 | ///\code |
---|
[1992] | 75 | /// UGRAPH_TYPEDEFS(typename G) |
---|
[1756] | 76 | ///\endcode |
---|
| 77 | /// |
---|
| 78 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 79 | ///template typedefs in C++. |
---|
[1992] | 80 | #define UGRAPH_TYPEDEFS(Graph) \ |
---|
[1804] | 81 | GRAPH_TYPEDEFS(Graph) \ |
---|
[1909] | 82 | typedef Graph:: UEdge UEdge; \ |
---|
| 83 | typedef Graph:: UEdgeIt UEdgeIt; \ |
---|
[1811] | 84 | typedef Graph:: IncEdgeIt IncEdgeIt; |
---|
[1909] | 85 | // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap; |
---|
| 86 | // typedef Graph::template UEdgeMap<int> IntUEdgeMap; |
---|
| 87 | // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap; |
---|
[1756] | 88 | |
---|
[2031] | 89 | ///\brief Creates convenience typedefs for the bipartite undirected graph |
---|
| 90 | ///types and iterators |
---|
| 91 | |
---|
| 92 | ///This \c \#define creates the same convenience typedefs as defined by |
---|
| 93 | ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates |
---|
| 94 | ///\c ANodeIt, \c BNodeIt, |
---|
| 95 | /// |
---|
| 96 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 97 | ///\code |
---|
| 98 | /// BPUGRAPH_TYPEDEFS(typename G) |
---|
| 99 | ///\endcode |
---|
| 100 | /// |
---|
| 101 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 102 | ///template typedefs in C++. |
---|
| 103 | #define BPUGRAPH_TYPEDEFS(Graph) \ |
---|
| 104 | UGRAPH_TYPEDEFS(Graph) \ |
---|
| 105 | typedef Graph::ANodeIt ANodeIt; \ |
---|
| 106 | typedef Graph::BNodeIt BNodeIt; |
---|
[1756] | 107 | |
---|
[946] | 108 | /// \brief Function to count the items in the graph. |
---|
| 109 | /// |
---|
[1540] | 110 | /// This function counts the items (nodes, edges etc) in the graph. |
---|
[946] | 111 | /// The complexity of the function is O(n) because |
---|
| 112 | /// it iterates on all of the items. |
---|
| 113 | |
---|
[2020] | 114 | template <typename Graph, typename Item> |
---|
[977] | 115 | inline int countItems(const Graph& g) { |
---|
[2020] | 116 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
[946] | 117 | int num = 0; |
---|
[977] | 118 | for (ItemIt it(g); it != INVALID; ++it) { |
---|
[946] | 119 | ++num; |
---|
| 120 | } |
---|
| 121 | return num; |
---|
| 122 | } |
---|
| 123 | |
---|
[977] | 124 | // Node counting: |
---|
| 125 | |
---|
[2020] | 126 | namespace _graph_utils_bits { |
---|
| 127 | |
---|
| 128 | template <typename Graph, typename Enable = void> |
---|
| 129 | struct CountNodesSelector { |
---|
| 130 | static int count(const Graph &g) { |
---|
| 131 | return countItems<Graph, typename Graph::Node>(g); |
---|
| 132 | } |
---|
| 133 | }; |
---|
[977] | 134 | |
---|
[2020] | 135 | template <typename Graph> |
---|
| 136 | struct CountNodesSelector< |
---|
| 137 | Graph, typename |
---|
| 138 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 139 | { |
---|
| 140 | static int count(const Graph &g) { |
---|
| 141 | return g.nodeNum(); |
---|
| 142 | } |
---|
| 143 | }; |
---|
[977] | 144 | } |
---|
| 145 | |
---|
[946] | 146 | /// \brief Function to count the nodes in the graph. |
---|
| 147 | /// |
---|
| 148 | /// This function counts the nodes in the graph. |
---|
| 149 | /// The complexity of the function is O(n) but for some |
---|
[1526] | 150 | /// graph structures it is specialized to run in O(1). |
---|
[977] | 151 | /// |
---|
| 152 | /// \todo refer how to specialize it |
---|
[946] | 153 | |
---|
| 154 | template <typename Graph> |
---|
[977] | 155 | inline int countNodes(const Graph& g) { |
---|
[2020] | 156 | return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
---|
[977] | 157 | } |
---|
| 158 | |
---|
[2029] | 159 | namespace _graph_utils_bits { |
---|
| 160 | |
---|
| 161 | template <typename Graph, typename Enable = void> |
---|
| 162 | struct CountANodesSelector { |
---|
| 163 | static int count(const Graph &g) { |
---|
| 164 | return countItems<Graph, typename Graph::ANode>(g); |
---|
| 165 | } |
---|
| 166 | }; |
---|
| 167 | |
---|
| 168 | template <typename Graph> |
---|
| 169 | struct CountANodesSelector< |
---|
| 170 | Graph, typename |
---|
| 171 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 172 | { |
---|
| 173 | static int count(const Graph &g) { |
---|
| 174 | return g.nodeNum(); |
---|
| 175 | } |
---|
| 176 | }; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /// \brief Function to count the anodes in the graph. |
---|
| 180 | /// |
---|
| 181 | /// This function counts the anodes in the graph. |
---|
| 182 | /// The complexity of the function is O(an) but for some |
---|
| 183 | /// graph structures it is specialized to run in O(1). |
---|
| 184 | /// |
---|
| 185 | /// \todo refer how to specialize it |
---|
| 186 | |
---|
| 187 | template <typename Graph> |
---|
| 188 | inline int countANodes(const Graph& g) { |
---|
| 189 | return _graph_utils_bits::CountANodesSelector<Graph>::count(g); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | namespace _graph_utils_bits { |
---|
| 193 | |
---|
| 194 | template <typename Graph, typename Enable = void> |
---|
| 195 | struct CountBNodesSelector { |
---|
| 196 | static int count(const Graph &g) { |
---|
| 197 | return countItems<Graph, typename Graph::BNode>(g); |
---|
| 198 | } |
---|
| 199 | }; |
---|
| 200 | |
---|
| 201 | template <typename Graph> |
---|
| 202 | struct CountBNodesSelector< |
---|
| 203 | Graph, typename |
---|
| 204 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 205 | { |
---|
| 206 | static int count(const Graph &g) { |
---|
| 207 | return g.nodeNum(); |
---|
| 208 | } |
---|
| 209 | }; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /// \brief Function to count the bnodes in the graph. |
---|
| 213 | /// |
---|
| 214 | /// This function counts the bnodes in the graph. |
---|
| 215 | /// The complexity of the function is O(bn) but for some |
---|
| 216 | /// graph structures it is specialized to run in O(1). |
---|
| 217 | /// |
---|
| 218 | /// \todo refer how to specialize it |
---|
| 219 | |
---|
| 220 | template <typename Graph> |
---|
| 221 | inline int countBNodes(const Graph& g) { |
---|
| 222 | return _graph_utils_bits::CountBNodesSelector<Graph>::count(g); |
---|
| 223 | } |
---|
| 224 | |
---|
[2020] | 225 | |
---|
[977] | 226 | // Edge counting: |
---|
| 227 | |
---|
[2020] | 228 | namespace _graph_utils_bits { |
---|
| 229 | |
---|
| 230 | template <typename Graph, typename Enable = void> |
---|
| 231 | struct CountEdgesSelector { |
---|
| 232 | static int count(const Graph &g) { |
---|
| 233 | return countItems<Graph, typename Graph::Edge>(g); |
---|
| 234 | } |
---|
| 235 | }; |
---|
[977] | 236 | |
---|
[2020] | 237 | template <typename Graph> |
---|
| 238 | struct CountEdgesSelector< |
---|
| 239 | Graph, |
---|
| 240 | typename enable_if<typename Graph::EdgeNumTag, void>::type> |
---|
| 241 | { |
---|
| 242 | static int count(const Graph &g) { |
---|
| 243 | return g.edgeNum(); |
---|
| 244 | } |
---|
| 245 | }; |
---|
[946] | 246 | } |
---|
| 247 | |
---|
| 248 | /// \brief Function to count the edges in the graph. |
---|
| 249 | /// |
---|
| 250 | /// This function counts the edges in the graph. |
---|
| 251 | /// The complexity of the function is O(e) but for some |
---|
[1526] | 252 | /// graph structures it is specialized to run in O(1). |
---|
[977] | 253 | |
---|
[946] | 254 | template <typename Graph> |
---|
[977] | 255 | inline int countEdges(const Graph& g) { |
---|
[2020] | 256 | return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
---|
[946] | 257 | } |
---|
| 258 | |
---|
[1053] | 259 | // Undirected edge counting: |
---|
[2020] | 260 | namespace _graph_utils_bits { |
---|
| 261 | |
---|
| 262 | template <typename Graph, typename Enable = void> |
---|
| 263 | struct CountUEdgesSelector { |
---|
| 264 | static int count(const Graph &g) { |
---|
| 265 | return countItems<Graph, typename Graph::UEdge>(g); |
---|
| 266 | } |
---|
| 267 | }; |
---|
[1053] | 268 | |
---|
[2020] | 269 | template <typename Graph> |
---|
| 270 | struct CountUEdgesSelector< |
---|
| 271 | Graph, |
---|
| 272 | typename enable_if<typename Graph::EdgeNumTag, void>::type> |
---|
| 273 | { |
---|
| 274 | static int count(const Graph &g) { |
---|
| 275 | return g.uEdgeNum(); |
---|
| 276 | } |
---|
| 277 | }; |
---|
[1053] | 278 | } |
---|
| 279 | |
---|
[1526] | 280 | /// \brief Function to count the undirected edges in the graph. |
---|
[946] | 281 | /// |
---|
[1526] | 282 | /// This function counts the undirected edges in the graph. |
---|
[946] | 283 | /// The complexity of the function is O(e) but for some |
---|
[1540] | 284 | /// graph structures it is specialized to run in O(1). |
---|
[1053] | 285 | |
---|
[946] | 286 | template <typename Graph> |
---|
[1909] | 287 | inline int countUEdges(const Graph& g) { |
---|
[2020] | 288 | return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g); |
---|
| 289 | |
---|
[946] | 290 | } |
---|
| 291 | |
---|
[977] | 292 | |
---|
[946] | 293 | template <typename Graph, typename DegIt> |
---|
| 294 | inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 295 | int num = 0; |
---|
| 296 | for (DegIt it(_g, _n); it != INVALID; ++it) { |
---|
| 297 | ++num; |
---|
| 298 | } |
---|
| 299 | return num; |
---|
| 300 | } |
---|
[967] | 301 | |
---|
[1531] | 302 | /// \brief Function to count the number of the out-edges from node \c n. |
---|
| 303 | /// |
---|
| 304 | /// This function counts the number of the out-edges from node \c n |
---|
| 305 | /// in the graph. |
---|
| 306 | template <typename Graph> |
---|
| 307 | inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 308 | return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /// \brief Function to count the number of the in-edges to node \c n. |
---|
| 312 | /// |
---|
| 313 | /// This function counts the number of the in-edges to node \c n |
---|
| 314 | /// in the graph. |
---|
| 315 | template <typename Graph> |
---|
| 316 | inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 317 | return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n); |
---|
| 318 | } |
---|
| 319 | |
---|
[1704] | 320 | /// \brief Function to count the number of the inc-edges to node \c n. |
---|
[1679] | 321 | /// |
---|
[1704] | 322 | /// This function counts the number of the inc-edges to node \c n |
---|
[1679] | 323 | /// in the graph. |
---|
| 324 | template <typename Graph> |
---|
| 325 | inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 326 | return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
---|
| 327 | } |
---|
| 328 | |
---|
[2020] | 329 | namespace _graph_utils_bits { |
---|
| 330 | |
---|
| 331 | template <typename Graph, typename Enable = void> |
---|
| 332 | struct FindEdgeSelector { |
---|
| 333 | typedef typename Graph::Node Node; |
---|
| 334 | typedef typename Graph::Edge Edge; |
---|
| 335 | static Edge find(const Graph &g, Node u, Node v, Edge e) { |
---|
| 336 | if (e == INVALID) { |
---|
| 337 | g.firstOut(e, u); |
---|
| 338 | } else { |
---|
| 339 | g.nextOut(e); |
---|
| 340 | } |
---|
| 341 | while (e != INVALID && g.target(e) != v) { |
---|
| 342 | g.nextOut(e); |
---|
| 343 | } |
---|
| 344 | return e; |
---|
| 345 | } |
---|
| 346 | }; |
---|
[1531] | 347 | |
---|
[2020] | 348 | template <typename Graph> |
---|
| 349 | struct FindEdgeSelector< |
---|
| 350 | Graph, |
---|
| 351 | typename enable_if<typename Graph::FindEdgeTag, void>::type> |
---|
| 352 | { |
---|
| 353 | typedef typename Graph::Node Node; |
---|
| 354 | typedef typename Graph::Edge Edge; |
---|
| 355 | static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
---|
| 356 | return g.findEdge(u, v, prev); |
---|
| 357 | } |
---|
| 358 | }; |
---|
[1565] | 359 | } |
---|
| 360 | |
---|
| 361 | /// \brief Finds an edge between two nodes of a graph. |
---|
| 362 | /// |
---|
[967] | 363 | /// Finds an edge from node \c u to node \c v in graph \c g. |
---|
| 364 | /// |
---|
| 365 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 366 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 367 | /// the next edge from \c u to \c v after \c prev. |
---|
| 368 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 369 | /// |
---|
| 370 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
[1946] | 371 | ///\code |
---|
[967] | 372 | /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) { |
---|
| 373 | /// ... |
---|
| 374 | /// } |
---|
[1946] | 375 | ///\endcode |
---|
[967] | 376 | template <typename Graph> |
---|
[1565] | 377 | inline typename Graph::Edge findEdge(const Graph &g, |
---|
| 378 | typename Graph::Node u, |
---|
| 379 | typename Graph::Node v, |
---|
| 380 | typename Graph::Edge prev = INVALID) { |
---|
[2020] | 381 | return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev); |
---|
[967] | 382 | } |
---|
[1531] | 383 | |
---|
[1565] | 384 | /// \brief Iterator for iterating on edges connected the same nodes. |
---|
| 385 | /// |
---|
| 386 | /// Iterator for iterating on edges connected the same nodes. It is |
---|
| 387 | /// higher level interface for the findEdge() function. You can |
---|
[1591] | 388 | /// use it the following way: |
---|
[1946] | 389 | ///\code |
---|
[1565] | 390 | /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
---|
| 391 | /// ... |
---|
| 392 | /// } |
---|
[1946] | 393 | ///\endcode |
---|
[1565] | 394 | /// |
---|
| 395 | /// \author Balazs Dezso |
---|
| 396 | template <typename _Graph> |
---|
| 397 | class ConEdgeIt : public _Graph::Edge { |
---|
| 398 | public: |
---|
| 399 | |
---|
| 400 | typedef _Graph Graph; |
---|
| 401 | typedef typename Graph::Edge Parent; |
---|
| 402 | |
---|
| 403 | typedef typename Graph::Edge Edge; |
---|
| 404 | typedef typename Graph::Node Node; |
---|
| 405 | |
---|
| 406 | /// \brief Constructor. |
---|
| 407 | /// |
---|
| 408 | /// Construct a new ConEdgeIt iterating on the edges which |
---|
| 409 | /// connects the \c u and \c v node. |
---|
| 410 | ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) { |
---|
| 411 | Parent::operator=(findEdge(graph, u, v)); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /// \brief Constructor. |
---|
| 415 | /// |
---|
| 416 | /// Construct a new ConEdgeIt which continues the iterating from |
---|
| 417 | /// the \c e edge. |
---|
| 418 | ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {} |
---|
| 419 | |
---|
| 420 | /// \brief Increment operator. |
---|
| 421 | /// |
---|
| 422 | /// It increments the iterator and gives back the next edge. |
---|
| 423 | ConEdgeIt& operator++() { |
---|
| 424 | Parent::operator=(findEdge(graph, graph.source(*this), |
---|
| 425 | graph.target(*this), *this)); |
---|
| 426 | return *this; |
---|
| 427 | } |
---|
| 428 | private: |
---|
| 429 | const Graph& graph; |
---|
| 430 | }; |
---|
| 431 | |
---|
[2020] | 432 | namespace _graph_utils_bits { |
---|
| 433 | |
---|
| 434 | template <typename Graph, typename Enable = void> |
---|
| 435 | struct FindUEdgeSelector { |
---|
| 436 | typedef typename Graph::Node Node; |
---|
| 437 | typedef typename Graph::UEdge UEdge; |
---|
| 438 | static UEdge find(const Graph &g, Node u, Node v, UEdge e) { |
---|
| 439 | bool b; |
---|
| 440 | if (u != v) { |
---|
| 441 | if (e == INVALID) { |
---|
[2031] | 442 | g.firstInc(e, b, u); |
---|
[2020] | 443 | } else { |
---|
| 444 | b = g.source(e) == u; |
---|
| 445 | g.nextInc(e, b); |
---|
| 446 | } |
---|
[2064] | 447 | while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { |
---|
[2020] | 448 | g.nextInc(e, b); |
---|
| 449 | } |
---|
| 450 | } else { |
---|
| 451 | if (e == INVALID) { |
---|
[2031] | 452 | g.firstInc(e, b, u); |
---|
[2020] | 453 | } else { |
---|
| 454 | b = true; |
---|
| 455 | g.nextInc(e, b); |
---|
| 456 | } |
---|
| 457 | while (e != INVALID && (!b || g.target(e) != v)) { |
---|
| 458 | g.nextInc(e, b); |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | return e; |
---|
| 462 | } |
---|
| 463 | }; |
---|
[1704] | 464 | |
---|
[2020] | 465 | template <typename Graph> |
---|
| 466 | struct FindUEdgeSelector< |
---|
| 467 | Graph, |
---|
| 468 | typename enable_if<typename Graph::FindEdgeTag, void>::type> |
---|
| 469 | { |
---|
| 470 | typedef typename Graph::Node Node; |
---|
| 471 | typedef typename Graph::UEdge UEdge; |
---|
| 472 | static UEdge find(const Graph &g, Node u, Node v, UEdge prev) { |
---|
| 473 | return g.findUEdge(u, v, prev); |
---|
| 474 | } |
---|
| 475 | }; |
---|
[1704] | 476 | } |
---|
| 477 | |
---|
[1909] | 478 | /// \brief Finds an uedge between two nodes of a graph. |
---|
[1704] | 479 | /// |
---|
[1909] | 480 | /// Finds an uedge from node \c u to node \c v in graph \c g. |
---|
[2020] | 481 | /// If the node \c u and node \c v is equal then each loop edge |
---|
| 482 | /// will be enumerated. |
---|
[1704] | 483 | /// |
---|
| 484 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 485 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 486 | /// the next edge from \c u to \c v after \c prev. |
---|
| 487 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 488 | /// |
---|
| 489 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
[1946] | 490 | ///\code |
---|
[1909] | 491 | /// for(UEdge e = findUEdge(g,u,v); e != INVALID; |
---|
| 492 | /// e = findUEdge(g,u,v,e)) { |
---|
[1704] | 493 | /// ... |
---|
| 494 | /// } |
---|
[1946] | 495 | ///\endcode |
---|
[1704] | 496 | template <typename Graph> |
---|
[2031] | 497 | inline typename Graph::UEdge findUEdge(const Graph &g, |
---|
| 498 | typename Graph::Node u, |
---|
| 499 | typename Graph::Node v, |
---|
| 500 | typename Graph::UEdge p = INVALID) { |
---|
| 501 | return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p); |
---|
[1704] | 502 | } |
---|
| 503 | |
---|
[1909] | 504 | /// \brief Iterator for iterating on uedges connected the same nodes. |
---|
[1704] | 505 | /// |
---|
[1909] | 506 | /// Iterator for iterating on uedges connected the same nodes. It is |
---|
| 507 | /// higher level interface for the findUEdge() function. You can |
---|
[1704] | 508 | /// use it the following way: |
---|
[1946] | 509 | ///\code |
---|
[1909] | 510 | /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
---|
[1704] | 511 | /// ... |
---|
| 512 | /// } |
---|
[1946] | 513 | ///\endcode |
---|
[1704] | 514 | /// |
---|
| 515 | /// \author Balazs Dezso |
---|
| 516 | template <typename _Graph> |
---|
[1909] | 517 | class ConUEdgeIt : public _Graph::UEdge { |
---|
[1704] | 518 | public: |
---|
| 519 | |
---|
| 520 | typedef _Graph Graph; |
---|
[1909] | 521 | typedef typename Graph::UEdge Parent; |
---|
[1704] | 522 | |
---|
[1909] | 523 | typedef typename Graph::UEdge UEdge; |
---|
[1704] | 524 | typedef typename Graph::Node Node; |
---|
| 525 | |
---|
| 526 | /// \brief Constructor. |
---|
| 527 | /// |
---|
[1909] | 528 | /// Construct a new ConUEdgeIt iterating on the edges which |
---|
[1704] | 529 | /// connects the \c u and \c v node. |
---|
[1909] | 530 | ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) { |
---|
| 531 | Parent::operator=(findUEdge(graph, u, v)); |
---|
[1704] | 532 | } |
---|
| 533 | |
---|
| 534 | /// \brief Constructor. |
---|
| 535 | /// |
---|
[1909] | 536 | /// Construct a new ConUEdgeIt which continues the iterating from |
---|
[1704] | 537 | /// the \c e edge. |
---|
[1909] | 538 | ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {} |
---|
[1704] | 539 | |
---|
| 540 | /// \brief Increment operator. |
---|
| 541 | /// |
---|
| 542 | /// It increments the iterator and gives back the next edge. |
---|
[1909] | 543 | ConUEdgeIt& operator++() { |
---|
| 544 | Parent::operator=(findUEdge(graph, graph.source(*this), |
---|
[1829] | 545 | graph.target(*this), *this)); |
---|
[1704] | 546 | return *this; |
---|
| 547 | } |
---|
| 548 | private: |
---|
| 549 | const Graph& graph; |
---|
| 550 | }; |
---|
| 551 | |
---|
[1540] | 552 | /// \brief Copy a map. |
---|
[964] | 553 | /// |
---|
[1547] | 554 | /// This function copies the \c source map to the \c target map. It uses the |
---|
[1540] | 555 | /// given iterator to iterate on the data structure and it uses the \c ref |
---|
| 556 | /// mapping to convert the source's keys to the target's keys. |
---|
[1531] | 557 | template <typename Target, typename Source, |
---|
| 558 | typename ItemIt, typename Ref> |
---|
| 559 | void copyMap(Target& target, const Source& source, |
---|
| 560 | ItemIt it, const Ref& ref) { |
---|
| 561 | for (; it != INVALID; ++it) { |
---|
| 562 | target[ref[it]] = source[it]; |
---|
[946] | 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
[1531] | 566 | /// \brief Copy the source map to the target map. |
---|
| 567 | /// |
---|
| 568 | /// Copy the \c source map to the \c target map. It uses the given iterator |
---|
| 569 | /// to iterate on the data structure. |
---|
[1830] | 570 | template <typename Target, typename Source, typename ItemIt> |
---|
[1531] | 571 | void copyMap(Target& target, const Source& source, ItemIt it) { |
---|
| 572 | for (; it != INVALID; ++it) { |
---|
| 573 | target[it] = source[it]; |
---|
[946] | 574 | } |
---|
| 575 | } |
---|
| 576 | |
---|
[1540] | 577 | /// \brief Class to copy a graph. |
---|
[1531] | 578 | /// |
---|
[2006] | 579 | /// Class to copy a graph to another graph (duplicate a graph). The |
---|
[1540] | 580 | /// simplest way of using it is through the \c copyGraph() function. |
---|
[1531] | 581 | template <typename Target, typename Source> |
---|
[1267] | 582 | class GraphCopy { |
---|
[1531] | 583 | public: |
---|
| 584 | typedef typename Source::Node Node; |
---|
| 585 | typedef typename Source::NodeIt NodeIt; |
---|
| 586 | typedef typename Source::Edge Edge; |
---|
| 587 | typedef typename Source::EdgeIt EdgeIt; |
---|
[946] | 588 | |
---|
[1531] | 589 | typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap; |
---|
| 590 | typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap; |
---|
[946] | 591 | |
---|
[1531] | 592 | /// \brief Constructor for the GraphCopy. |
---|
| 593 | /// |
---|
| 594 | /// It copies the content of the \c _source graph into the |
---|
| 595 | /// \c _target graph. It creates also two references, one beetween |
---|
| 596 | /// the two nodeset and one beetween the two edgesets. |
---|
| 597 | GraphCopy(Target& _target, const Source& _source) |
---|
| 598 | : source(_source), target(_target), |
---|
| 599 | nodeRefMap(_source), edgeRefMap(_source) { |
---|
| 600 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 601 | nodeRefMap[it] = target.addNode(); |
---|
| 602 | } |
---|
| 603 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 604 | edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], |
---|
| 605 | nodeRefMap[source.target(it)]); |
---|
| 606 | } |
---|
[1267] | 607 | } |
---|
[946] | 608 | |
---|
[1531] | 609 | /// \brief Copies the node references into the given map. |
---|
| 610 | /// |
---|
| 611 | /// Copies the node references into the given map. |
---|
| 612 | template <typename NodeRef> |
---|
| 613 | const GraphCopy& nodeRef(NodeRef& map) const { |
---|
| 614 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 615 | map.set(it, nodeRefMap[it]); |
---|
| 616 | } |
---|
| 617 | return *this; |
---|
[1267] | 618 | } |
---|
[1531] | 619 | |
---|
| 620 | /// \brief Reverse and copies the node references into the given map. |
---|
| 621 | /// |
---|
| 622 | /// Reverse and copies the node references into the given map. |
---|
| 623 | template <typename NodeRef> |
---|
| 624 | const GraphCopy& nodeCrossRef(NodeRef& map) const { |
---|
| 625 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 626 | map.set(nodeRefMap[it], it); |
---|
| 627 | } |
---|
| 628 | return *this; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | /// \brief Copies the edge references into the given map. |
---|
| 632 | /// |
---|
| 633 | /// Copies the edge references into the given map. |
---|
| 634 | template <typename EdgeRef> |
---|
| 635 | const GraphCopy& edgeRef(EdgeRef& map) const { |
---|
| 636 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 637 | map.set(it, edgeRefMap[it]); |
---|
| 638 | } |
---|
| 639 | return *this; |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | /// \brief Reverse and copies the edge references into the given map. |
---|
| 643 | /// |
---|
| 644 | /// Reverse and copies the edge references into the given map. |
---|
| 645 | template <typename EdgeRef> |
---|
| 646 | const GraphCopy& edgeCrossRef(EdgeRef& map) const { |
---|
| 647 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 648 | map.set(edgeRefMap[it], it); |
---|
| 649 | } |
---|
| 650 | return *this; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | /// \brief Make copy of the given map. |
---|
| 654 | /// |
---|
| 655 | /// Makes copy of the given map for the newly created graph. |
---|
| 656 | /// The new map's key type is the target graph's node type, |
---|
| 657 | /// and the copied map's key type is the source graph's node |
---|
| 658 | /// type. |
---|
| 659 | template <typename TargetMap, typename SourceMap> |
---|
| 660 | const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const { |
---|
| 661 | copyMap(tMap, sMap, NodeIt(source), nodeRefMap); |
---|
| 662 | return *this; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | /// \brief Make copy of the given map. |
---|
| 666 | /// |
---|
| 667 | /// Makes copy of the given map for the newly created graph. |
---|
| 668 | /// The new map's key type is the target graph's edge type, |
---|
| 669 | /// and the copied map's key type is the source graph's edge |
---|
| 670 | /// type. |
---|
| 671 | template <typename TargetMap, typename SourceMap> |
---|
| 672 | const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const { |
---|
| 673 | copyMap(tMap, sMap, EdgeIt(source), edgeRefMap); |
---|
| 674 | return *this; |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | /// \brief Gives back the stored node references. |
---|
| 678 | /// |
---|
| 679 | /// Gives back the stored node references. |
---|
| 680 | const NodeRefMap& nodeRef() const { |
---|
| 681 | return nodeRefMap; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | /// \brief Gives back the stored edge references. |
---|
| 685 | /// |
---|
| 686 | /// Gives back the stored edge references. |
---|
| 687 | const EdgeRefMap& edgeRef() const { |
---|
| 688 | return edgeRefMap; |
---|
| 689 | } |
---|
| 690 | |
---|
[1981] | 691 | void run() const {} |
---|
[1720] | 692 | |
---|
[1531] | 693 | private: |
---|
| 694 | |
---|
| 695 | const Source& source; |
---|
| 696 | Target& target; |
---|
| 697 | |
---|
| 698 | NodeRefMap nodeRefMap; |
---|
| 699 | EdgeRefMap edgeRefMap; |
---|
[1267] | 700 | }; |
---|
[946] | 701 | |
---|
[2006] | 702 | /// \brief Copy a graph to another graph. |
---|
[1531] | 703 | /// |
---|
[2006] | 704 | /// Copy a graph to another graph. |
---|
[1531] | 705 | /// The usage of the function: |
---|
| 706 | /// |
---|
[1946] | 707 | ///\code |
---|
[1531] | 708 | /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr); |
---|
[1946] | 709 | ///\endcode |
---|
[1531] | 710 | /// |
---|
| 711 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 712 | /// source graph's nodes to the target graph's nodes and the \c ecr will |
---|
[1540] | 713 | /// contain the mapping from the target graph's edges to the source's |
---|
[1531] | 714 | /// edges. |
---|
| 715 | template <typename Target, typename Source> |
---|
| 716 | GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) { |
---|
| 717 | return GraphCopy<Target, Source>(target, source); |
---|
| 718 | } |
---|
[946] | 719 | |
---|
[1720] | 720 | /// \brief Class to copy an undirected graph. |
---|
| 721 | /// |
---|
[2006] | 722 | /// Class to copy an undirected graph to another graph (duplicate a graph). |
---|
[1909] | 723 | /// The simplest way of using it is through the \c copyUGraph() function. |
---|
[1720] | 724 | template <typename Target, typename Source> |
---|
[1909] | 725 | class UGraphCopy { |
---|
[1720] | 726 | public: |
---|
| 727 | typedef typename Source::Node Node; |
---|
| 728 | typedef typename Source::NodeIt NodeIt; |
---|
| 729 | typedef typename Source::Edge Edge; |
---|
| 730 | typedef typename Source::EdgeIt EdgeIt; |
---|
[1909] | 731 | typedef typename Source::UEdge UEdge; |
---|
| 732 | typedef typename Source::UEdgeIt UEdgeIt; |
---|
[1720] | 733 | |
---|
| 734 | typedef typename Source:: |
---|
| 735 | template NodeMap<typename Target::Node> NodeRefMap; |
---|
| 736 | |
---|
| 737 | typedef typename Source:: |
---|
[1909] | 738 | template UEdgeMap<typename Target::UEdge> UEdgeRefMap; |
---|
[1720] | 739 | |
---|
| 740 | private: |
---|
| 741 | |
---|
| 742 | struct EdgeRefMap { |
---|
[1909] | 743 | EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {} |
---|
[1720] | 744 | typedef typename Source::Edge Key; |
---|
| 745 | typedef typename Target::Edge Value; |
---|
| 746 | |
---|
| 747 | Value operator[](const Key& key) { |
---|
[1909] | 748 | return gc.target.direct(gc.uEdgeRef[key], |
---|
[1720] | 749 | gc.target.direction(key)); |
---|
| 750 | } |
---|
| 751 | |
---|
[1909] | 752 | UGraphCopy& gc; |
---|
[1720] | 753 | }; |
---|
| 754 | |
---|
[1192] | 755 | public: |
---|
[1720] | 756 | |
---|
[1909] | 757 | /// \brief Constructor for the UGraphCopy. |
---|
[1720] | 758 | /// |
---|
| 759 | /// It copies the content of the \c _source graph into the |
---|
| 760 | /// \c _target graph. It creates also two references, one beetween |
---|
| 761 | /// the two nodeset and one beetween the two edgesets. |
---|
[1909] | 762 | UGraphCopy(Target& _target, const Source& _source) |
---|
[1720] | 763 | : source(_source), target(_target), |
---|
[1909] | 764 | nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) { |
---|
[1720] | 765 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 766 | nodeRefMap[it] = target.addNode(); |
---|
| 767 | } |
---|
[1909] | 768 | for (UEdgeIt it(source); it != INVALID; ++it) { |
---|
| 769 | uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], |
---|
[1720] | 770 | nodeRefMap[source.target(it)]); |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | /// \brief Copies the node references into the given map. |
---|
| 775 | /// |
---|
| 776 | /// Copies the node references into the given map. |
---|
| 777 | template <typename NodeRef> |
---|
[1909] | 778 | const UGraphCopy& nodeRef(NodeRef& map) const { |
---|
[1720] | 779 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 780 | map.set(it, nodeRefMap[it]); |
---|
| 781 | } |
---|
| 782 | return *this; |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | /// \brief Reverse and copies the node references into the given map. |
---|
| 786 | /// |
---|
| 787 | /// Reverse and copies the node references into the given map. |
---|
| 788 | template <typename NodeRef> |
---|
[1909] | 789 | const UGraphCopy& nodeCrossRef(NodeRef& map) const { |
---|
[1720] | 790 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 791 | map.set(nodeRefMap[it], it); |
---|
| 792 | } |
---|
| 793 | return *this; |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | /// \brief Copies the edge references into the given map. |
---|
| 797 | /// |
---|
| 798 | /// Copies the edge references into the given map. |
---|
| 799 | template <typename EdgeRef> |
---|
[1909] | 800 | const UGraphCopy& edgeRef(EdgeRef& map) const { |
---|
[1720] | 801 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 802 | map.set(edgeRefMap[it], it); |
---|
| 803 | } |
---|
| 804 | return *this; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | /// \brief Reverse and copies the undirected edge references into the |
---|
| 808 | /// given map. |
---|
| 809 | /// |
---|
| 810 | /// Reverse and copies the undirected edge references into the given map. |
---|
| 811 | template <typename EdgeRef> |
---|
[1909] | 812 | const UGraphCopy& edgeCrossRef(EdgeRef& map) const { |
---|
[1720] | 813 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 814 | map.set(it, edgeRefMap[it]); |
---|
| 815 | } |
---|
| 816 | return *this; |
---|
| 817 | } |
---|
| 818 | |
---|
| 819 | /// \brief Copies the undirected edge references into the given map. |
---|
| 820 | /// |
---|
| 821 | /// Copies the undirected edge references into the given map. |
---|
| 822 | template <typename EdgeRef> |
---|
[1909] | 823 | const UGraphCopy& uEdgeRef(EdgeRef& map) const { |
---|
| 824 | for (UEdgeIt it(source); it != INVALID; ++it) { |
---|
| 825 | map.set(it, uEdgeRefMap[it]); |
---|
[1720] | 826 | } |
---|
| 827 | return *this; |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | /// \brief Reverse and copies the undirected edge references into the |
---|
| 831 | /// given map. |
---|
| 832 | /// |
---|
| 833 | /// Reverse and copies the undirected edge references into the given map. |
---|
| 834 | template <typename EdgeRef> |
---|
[1909] | 835 | const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const { |
---|
| 836 | for (UEdgeIt it(source); it != INVALID; ++it) { |
---|
| 837 | map.set(uEdgeRefMap[it], it); |
---|
[1720] | 838 | } |
---|
| 839 | return *this; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | /// \brief Make copy of the given map. |
---|
| 843 | /// |
---|
| 844 | /// Makes copy of the given map for the newly created graph. |
---|
| 845 | /// The new map's key type is the target graph's node type, |
---|
| 846 | /// and the copied map's key type is the source graph's node |
---|
| 847 | /// type. |
---|
| 848 | template <typename TargetMap, typename SourceMap> |
---|
[1909] | 849 | const UGraphCopy& nodeMap(TargetMap& tMap, |
---|
[1720] | 850 | const SourceMap& sMap) const { |
---|
| 851 | copyMap(tMap, sMap, NodeIt(source), nodeRefMap); |
---|
| 852 | return *this; |
---|
| 853 | } |
---|
| 854 | |
---|
| 855 | /// \brief Make copy of the given map. |
---|
| 856 | /// |
---|
| 857 | /// Makes copy of the given map for the newly created graph. |
---|
| 858 | /// The new map's key type is the target graph's edge type, |
---|
| 859 | /// and the copied map's key type is the source graph's edge |
---|
| 860 | /// type. |
---|
| 861 | template <typename TargetMap, typename SourceMap> |
---|
[1909] | 862 | const UGraphCopy& edgeMap(TargetMap& tMap, |
---|
[1720] | 863 | const SourceMap& sMap) const { |
---|
| 864 | copyMap(tMap, sMap, EdgeIt(source), edgeRefMap); |
---|
| 865 | return *this; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /// \brief Make copy of the given map. |
---|
| 869 | /// |
---|
| 870 | /// Makes copy of the given map for the newly created graph. |
---|
| 871 | /// The new map's key type is the target graph's edge type, |
---|
| 872 | /// and the copied map's key type is the source graph's edge |
---|
| 873 | /// type. |
---|
| 874 | template <typename TargetMap, typename SourceMap> |
---|
[1909] | 875 | const UGraphCopy& uEdgeMap(TargetMap& tMap, |
---|
[1720] | 876 | const SourceMap& sMap) const { |
---|
[1909] | 877 | copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap); |
---|
[1720] | 878 | return *this; |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | /// \brief Gives back the stored node references. |
---|
| 882 | /// |
---|
| 883 | /// Gives back the stored node references. |
---|
| 884 | const NodeRefMap& nodeRef() const { |
---|
| 885 | return nodeRefMap; |
---|
| 886 | } |
---|
| 887 | |
---|
| 888 | /// \brief Gives back the stored edge references. |
---|
| 889 | /// |
---|
| 890 | /// Gives back the stored edge references. |
---|
| 891 | const EdgeRefMap& edgeRef() const { |
---|
| 892 | return edgeRefMap; |
---|
| 893 | } |
---|
| 894 | |
---|
[1909] | 895 | /// \brief Gives back the stored uedge references. |
---|
[1720] | 896 | /// |
---|
[1909] | 897 | /// Gives back the stored uedge references. |
---|
| 898 | const UEdgeRefMap& uEdgeRef() const { |
---|
| 899 | return uEdgeRefMap; |
---|
[1720] | 900 | } |
---|
| 901 | |
---|
[1981] | 902 | void run() const {} |
---|
[1720] | 903 | |
---|
| 904 | private: |
---|
[1192] | 905 | |
---|
[1720] | 906 | const Source& source; |
---|
| 907 | Target& target; |
---|
[947] | 908 | |
---|
[1720] | 909 | NodeRefMap nodeRefMap; |
---|
| 910 | EdgeRefMap edgeRefMap; |
---|
[1909] | 911 | UEdgeRefMap uEdgeRefMap; |
---|
[1192] | 912 | }; |
---|
| 913 | |
---|
[2006] | 914 | /// \brief Copy a graph to another graph. |
---|
[1720] | 915 | /// |
---|
[2006] | 916 | /// Copy a graph to another graph. |
---|
[1720] | 917 | /// The usage of the function: |
---|
| 918 | /// |
---|
[1946] | 919 | ///\code |
---|
[2022] | 920 | /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr); |
---|
[1946] | 921 | ///\endcode |
---|
[1720] | 922 | /// |
---|
| 923 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 924 | /// source graph's nodes to the target graph's nodes and the \c ecr will |
---|
| 925 | /// contain the mapping from the target graph's edges to the source's |
---|
| 926 | /// edges. |
---|
| 927 | template <typename Target, typename Source> |
---|
[1909] | 928 | UGraphCopy<Target, Source> |
---|
| 929 | copyUGraph(Target& target, const Source& source) { |
---|
| 930 | return UGraphCopy<Target, Source>(target, source); |
---|
[1720] | 931 | } |
---|
[1192] | 932 | |
---|
| 933 | |
---|
| 934 | /// @} |
---|
[1402] | 935 | |
---|
| 936 | /// \addtogroup graph_maps |
---|
| 937 | /// @{ |
---|
| 938 | |
---|
[1413] | 939 | /// Provides an immutable and unique id for each item in the graph. |
---|
| 940 | |
---|
[1540] | 941 | /// The IdMap class provides a unique and immutable id for each item of the |
---|
| 942 | /// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
---|
| 943 | /// different items (nodes) get different ids <li>\b immutable: the id of an |
---|
| 944 | /// item (node) does not change (even if you delete other nodes). </ul> |
---|
| 945 | /// Through this map you get access (i.e. can read) the inner id values of |
---|
| 946 | /// the items stored in the graph. This map can be inverted with its member |
---|
| 947 | /// class \c InverseMap. |
---|
[1413] | 948 | /// |
---|
| 949 | template <typename _Graph, typename _Item> |
---|
| 950 | class IdMap { |
---|
| 951 | public: |
---|
| 952 | typedef _Graph Graph; |
---|
| 953 | typedef int Value; |
---|
| 954 | typedef _Item Item; |
---|
| 955 | typedef _Item Key; |
---|
| 956 | |
---|
| 957 | /// \brief Constructor. |
---|
| 958 | /// |
---|
| 959 | /// Constructor for creating id map. |
---|
| 960 | IdMap(const Graph& _graph) : graph(&_graph) {} |
---|
| 961 | |
---|
| 962 | /// \brief Gives back the \e id of the item. |
---|
| 963 | /// |
---|
| 964 | /// Gives back the immutable and unique \e id of the map. |
---|
| 965 | int operator[](const Item& item) const { return graph->id(item);} |
---|
| 966 | |
---|
| 967 | |
---|
| 968 | private: |
---|
| 969 | const Graph* graph; |
---|
| 970 | |
---|
| 971 | public: |
---|
| 972 | |
---|
[1540] | 973 | /// \brief The class represents the inverse of its owner (IdMap). |
---|
[1413] | 974 | /// |
---|
[1540] | 975 | /// The class represents the inverse of its owner (IdMap). |
---|
[1413] | 976 | /// \see inverse() |
---|
| 977 | class InverseMap { |
---|
| 978 | public: |
---|
[1419] | 979 | |
---|
[1413] | 980 | /// \brief Constructor. |
---|
| 981 | /// |
---|
| 982 | /// Constructor for creating an id-to-item map. |
---|
| 983 | InverseMap(const Graph& _graph) : graph(&_graph) {} |
---|
| 984 | |
---|
| 985 | /// \brief Constructor. |
---|
| 986 | /// |
---|
| 987 | /// Constructor for creating an id-to-item map. |
---|
| 988 | InverseMap(const IdMap& idMap) : graph(idMap.graph) {} |
---|
| 989 | |
---|
| 990 | /// \brief Gives back the given item from its id. |
---|
| 991 | /// |
---|
| 992 | /// Gives back the given item from its id. |
---|
| 993 | /// |
---|
| 994 | Item operator[](int id) const { return graph->fromId(id, Item());} |
---|
| 995 | private: |
---|
| 996 | const Graph* graph; |
---|
| 997 | }; |
---|
| 998 | |
---|
| 999 | /// \brief Gives back the inverse of the map. |
---|
| 1000 | /// |
---|
[1540] | 1001 | /// Gives back the inverse of the IdMap. |
---|
[1413] | 1002 | InverseMap inverse() const { return InverseMap(*graph);} |
---|
| 1003 | |
---|
| 1004 | }; |
---|
| 1005 | |
---|
| 1006 | |
---|
[1526] | 1007 | /// \brief General invertable graph-map type. |
---|
[1402] | 1008 | |
---|
[1540] | 1009 | /// This type provides simple invertable graph-maps. |
---|
[1526] | 1010 | /// The InvertableMap wraps an arbitrary ReadWriteMap |
---|
| 1011 | /// and if a key is set to a new value then store it |
---|
[1402] | 1012 | /// in the inverse map. |
---|
[1931] | 1013 | /// |
---|
| 1014 | /// The values of the map can be accessed |
---|
| 1015 | /// with stl compatible forward iterator. |
---|
| 1016 | /// |
---|
[1402] | 1017 | /// \param _Graph The graph type. |
---|
[1830] | 1018 | /// \param _Item The item type of the graph. |
---|
| 1019 | /// \param _Value The value type of the map. |
---|
[1931] | 1020 | /// |
---|
| 1021 | /// \see IterableValueMap |
---|
[1830] | 1022 | #ifndef DOXYGEN |
---|
| 1023 | /// \param _Map A ReadWriteMap mapping from the item type to integer. |
---|
[1402] | 1024 | template < |
---|
[1990] | 1025 | typename _Graph, typename _Item, typename _Value, |
---|
| 1026 | typename _Map = DefaultMap<_Graph, _Item, _Value> |
---|
[1402] | 1027 | > |
---|
[1830] | 1028 | #else |
---|
| 1029 | template <typename _Graph, typename _Item, typename _Value> |
---|
| 1030 | #endif |
---|
[1413] | 1031 | class InvertableMap : protected _Map { |
---|
[1402] | 1032 | public: |
---|
[1413] | 1033 | |
---|
[1909] | 1034 | /// The key type of InvertableMap (Node, Edge, UEdge). |
---|
[1402] | 1035 | typedef typename _Map::Key Key; |
---|
[1413] | 1036 | /// The value type of the InvertableMap. |
---|
[1402] | 1037 | typedef typename _Map::Value Value; |
---|
| 1038 | |
---|
[1931] | 1039 | private: |
---|
| 1040 | |
---|
| 1041 | typedef _Map Map; |
---|
| 1042 | typedef _Graph Graph; |
---|
| 1043 | |
---|
| 1044 | typedef std::map<Value, Key> Container; |
---|
| 1045 | Container invMap; |
---|
| 1046 | |
---|
| 1047 | public: |
---|
| 1048 | |
---|
| 1049 | |
---|
| 1050 | |
---|
[1402] | 1051 | /// \brief Constructor. |
---|
| 1052 | /// |
---|
[1413] | 1053 | /// Construct a new InvertableMap for the graph. |
---|
[1402] | 1054 | /// |
---|
[1413] | 1055 | InvertableMap(const Graph& graph) : Map(graph) {} |
---|
[1931] | 1056 | |
---|
| 1057 | /// \brief Forward iterator for values. |
---|
| 1058 | /// |
---|
| 1059 | /// This iterator is an stl compatible forward |
---|
| 1060 | /// iterator on the values of the map. The values can |
---|
| 1061 | /// be accessed in the [beginValue, endValue) range. |
---|
| 1062 | /// |
---|
| 1063 | class ValueIterator |
---|
| 1064 | : public std::iterator<std::forward_iterator_tag, Value> { |
---|
| 1065 | friend class InvertableMap; |
---|
| 1066 | private: |
---|
| 1067 | ValueIterator(typename Container::const_iterator _it) |
---|
| 1068 | : it(_it) {} |
---|
| 1069 | public: |
---|
| 1070 | |
---|
| 1071 | ValueIterator() {} |
---|
| 1072 | |
---|
| 1073 | ValueIterator& operator++() { ++it; return *this; } |
---|
| 1074 | ValueIterator operator++(int) { |
---|
| 1075 | ValueIterator tmp(*this); |
---|
| 1076 | operator++(); |
---|
| 1077 | return tmp; |
---|
| 1078 | } |
---|
| 1079 | |
---|
| 1080 | const Value& operator*() const { return it->first; } |
---|
| 1081 | const Value* operator->() const { return &(it->first); } |
---|
| 1082 | |
---|
| 1083 | bool operator==(ValueIterator jt) const { return it == jt.it; } |
---|
| 1084 | bool operator!=(ValueIterator jt) const { return it != jt.it; } |
---|
| 1085 | |
---|
| 1086 | private: |
---|
| 1087 | typename Container::const_iterator it; |
---|
| 1088 | }; |
---|
| 1089 | |
---|
| 1090 | /// \brief Returns an iterator to the first value. |
---|
| 1091 | /// |
---|
| 1092 | /// Returns an stl compatible iterator to the |
---|
| 1093 | /// first value of the map. The values of the |
---|
| 1094 | /// map can be accessed in the [beginValue, endValue) |
---|
| 1095 | /// range. |
---|
| 1096 | ValueIterator beginValue() const { |
---|
| 1097 | return ValueIterator(invMap.begin()); |
---|
| 1098 | } |
---|
| 1099 | |
---|
| 1100 | /// \brief Returns an iterator after the last value. |
---|
| 1101 | /// |
---|
| 1102 | /// Returns an stl compatible iterator after the |
---|
| 1103 | /// last value of the map. The values of the |
---|
| 1104 | /// map can be accessed in the [beginValue, endValue) |
---|
| 1105 | /// range. |
---|
| 1106 | ValueIterator endValue() const { |
---|
| 1107 | return ValueIterator(invMap.end()); |
---|
| 1108 | } |
---|
[1402] | 1109 | |
---|
| 1110 | /// \brief The setter function of the map. |
---|
| 1111 | /// |
---|
[1413] | 1112 | /// Sets the mapped value. |
---|
[1402] | 1113 | void set(const Key& key, const Value& val) { |
---|
| 1114 | Value oldval = Map::operator[](key); |
---|
[1413] | 1115 | typename Container::iterator it = invMap.find(oldval); |
---|
[1402] | 1116 | if (it != invMap.end() && it->second == key) { |
---|
| 1117 | invMap.erase(it); |
---|
| 1118 | } |
---|
| 1119 | invMap.insert(make_pair(val, key)); |
---|
| 1120 | Map::set(key, val); |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | /// \brief The getter function of the map. |
---|
| 1124 | /// |
---|
| 1125 | /// It gives back the value associated with the key. |
---|
[1931] | 1126 | typename MapTraits<Map>::ConstReturnValue |
---|
| 1127 | operator[](const Key& key) const { |
---|
[1402] | 1128 | return Map::operator[](key); |
---|
| 1129 | } |
---|
| 1130 | |
---|
[1515] | 1131 | protected: |
---|
| 1132 | |
---|
[1402] | 1133 | /// \brief Erase the key from the map. |
---|
| 1134 | /// |
---|
| 1135 | /// Erase the key to the map. It is called by the |
---|
| 1136 | /// \c AlterationNotifier. |
---|
| 1137 | virtual void erase(const Key& key) { |
---|
| 1138 | Value val = Map::operator[](key); |
---|
[1413] | 1139 | typename Container::iterator it = invMap.find(val); |
---|
[1402] | 1140 | if (it != invMap.end() && it->second == key) { |
---|
| 1141 | invMap.erase(it); |
---|
| 1142 | } |
---|
| 1143 | Map::erase(key); |
---|
| 1144 | } |
---|
| 1145 | |
---|
[1829] | 1146 | /// \brief Erase more keys from the map. |
---|
| 1147 | /// |
---|
| 1148 | /// Erase more keys from the map. It is called by the |
---|
| 1149 | /// \c AlterationNotifier. |
---|
| 1150 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 1151 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 1152 | Value val = Map::operator[](keys[i]); |
---|
| 1153 | typename Container::iterator it = invMap.find(val); |
---|
| 1154 | if (it != invMap.end() && it->second == keys[i]) { |
---|
| 1155 | invMap.erase(it); |
---|
| 1156 | } |
---|
| 1157 | } |
---|
| 1158 | Map::erase(keys); |
---|
| 1159 | } |
---|
| 1160 | |
---|
[1402] | 1161 | /// \brief Clear the keys from the map and inverse map. |
---|
| 1162 | /// |
---|
| 1163 | /// Clear the keys from the map and inverse map. It is called by the |
---|
| 1164 | /// \c AlterationNotifier. |
---|
| 1165 | virtual void clear() { |
---|
| 1166 | invMap.clear(); |
---|
| 1167 | Map::clear(); |
---|
| 1168 | } |
---|
| 1169 | |
---|
[1413] | 1170 | public: |
---|
| 1171 | |
---|
| 1172 | /// \brief The inverse map type. |
---|
| 1173 | /// |
---|
| 1174 | /// The inverse of this map. The subscript operator of the map |
---|
| 1175 | /// gives back always the item what was last assigned to the value. |
---|
| 1176 | class InverseMap { |
---|
| 1177 | public: |
---|
| 1178 | /// \brief Constructor of the InverseMap. |
---|
| 1179 | /// |
---|
| 1180 | /// Constructor of the InverseMap. |
---|
| 1181 | InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {} |
---|
| 1182 | |
---|
| 1183 | /// The value type of the InverseMap. |
---|
| 1184 | typedef typename InvertableMap::Key Value; |
---|
| 1185 | /// The key type of the InverseMap. |
---|
| 1186 | typedef typename InvertableMap::Value Key; |
---|
| 1187 | |
---|
| 1188 | /// \brief Subscript operator. |
---|
| 1189 | /// |
---|
| 1190 | /// Subscript operator. It gives back always the item |
---|
| 1191 | /// what was last assigned to the value. |
---|
| 1192 | Value operator[](const Key& key) const { |
---|
| 1193 | typename Container::const_iterator it = inverted.invMap.find(key); |
---|
| 1194 | return it->second; |
---|
| 1195 | } |
---|
| 1196 | |
---|
| 1197 | private: |
---|
| 1198 | const InvertableMap& inverted; |
---|
| 1199 | }; |
---|
| 1200 | |
---|
[2094] | 1201 | /// \brief It gives back the just readable inverse map. |
---|
[1402] | 1202 | /// |
---|
[2094] | 1203 | /// It gives back the just readable inverse map. |
---|
[1413] | 1204 | InverseMap inverse() const { |
---|
| 1205 | return InverseMap(*this); |
---|
[1402] | 1206 | } |
---|
| 1207 | |
---|
| 1208 | |
---|
[1413] | 1209 | |
---|
[1402] | 1210 | }; |
---|
| 1211 | |
---|
| 1212 | /// \brief Provides a mutable, continuous and unique descriptor for each |
---|
| 1213 | /// item in the graph. |
---|
| 1214 | /// |
---|
[1540] | 1215 | /// The DescriptorMap class provides a unique and continuous (but mutable) |
---|
| 1216 | /// descriptor (id) for each item of the same type (e.g. node) in the |
---|
| 1217 | /// graph. This id is <ul><li>\b unique: different items (nodes) get |
---|
| 1218 | /// different ids <li>\b continuous: the range of the ids is the set of |
---|
| 1219 | /// integers between 0 and \c n-1, where \c n is the number of the items of |
---|
| 1220 | /// this type (e.g. nodes) (so the id of a node can change if you delete an |
---|
| 1221 | /// other node, i.e. this id is mutable). </ul> This map can be inverted |
---|
| 1222 | /// with its member class \c InverseMap. |
---|
[1402] | 1223 | /// |
---|
| 1224 | /// \param _Graph The graph class the \c DescriptorMap belongs to. |
---|
| 1225 | /// \param _Item The Item is the Key of the Map. It may be Node, Edge or |
---|
[1909] | 1226 | /// UEdge. |
---|
[1830] | 1227 | #ifndef DOXYGEN |
---|
[1402] | 1228 | /// \param _Map A ReadWriteMap mapping from the item type to integer. |
---|
| 1229 | template < |
---|
[1990] | 1230 | typename _Graph, typename _Item, |
---|
| 1231 | typename _Map = DefaultMap<_Graph, _Item, int> |
---|
[1402] | 1232 | > |
---|
[1830] | 1233 | #else |
---|
| 1234 | template <typename _Graph, typename _Item> |
---|
| 1235 | #endif |
---|
[1402] | 1236 | class DescriptorMap : protected _Map { |
---|
| 1237 | |
---|
| 1238 | typedef _Item Item; |
---|
| 1239 | typedef _Map Map; |
---|
| 1240 | |
---|
| 1241 | public: |
---|
| 1242 | /// The graph class of DescriptorMap. |
---|
| 1243 | typedef _Graph Graph; |
---|
| 1244 | |
---|
[1909] | 1245 | /// The key type of DescriptorMap (Node, Edge, UEdge). |
---|
[1402] | 1246 | typedef typename _Map::Key Key; |
---|
| 1247 | /// The value type of DescriptorMap. |
---|
| 1248 | typedef typename _Map::Value Value; |
---|
| 1249 | |
---|
| 1250 | /// \brief Constructor. |
---|
| 1251 | /// |
---|
[1413] | 1252 | /// Constructor for descriptor map. |
---|
[1402] | 1253 | DescriptorMap(const Graph& _graph) : Map(_graph) { |
---|
| 1254 | build(); |
---|
| 1255 | } |
---|
| 1256 | |
---|
[1515] | 1257 | protected: |
---|
| 1258 | |
---|
[1402] | 1259 | /// \brief Add a new key to the map. |
---|
| 1260 | /// |
---|
| 1261 | /// Add a new key to the map. It is called by the |
---|
| 1262 | /// \c AlterationNotifier. |
---|
| 1263 | virtual void add(const Item& item) { |
---|
| 1264 | Map::add(item); |
---|
| 1265 | Map::set(item, invMap.size()); |
---|
| 1266 | invMap.push_back(item); |
---|
| 1267 | } |
---|
| 1268 | |
---|
[1829] | 1269 | /// \brief Add more new keys to the map. |
---|
| 1270 | /// |
---|
| 1271 | /// Add more new keys to the map. It is called by the |
---|
| 1272 | /// \c AlterationNotifier. |
---|
| 1273 | virtual void add(const std::vector<Item>& items) { |
---|
| 1274 | Map::add(items); |
---|
| 1275 | for (int i = 0; i < (int)items.size(); ++i) { |
---|
| 1276 | Map::set(items[i], invMap.size()); |
---|
| 1277 | invMap.push_back(items[i]); |
---|
| 1278 | } |
---|
| 1279 | } |
---|
| 1280 | |
---|
[1402] | 1281 | /// \brief Erase the key from the map. |
---|
| 1282 | /// |
---|
[1829] | 1283 | /// Erase the key from the map. It is called by the |
---|
[1402] | 1284 | /// \c AlterationNotifier. |
---|
| 1285 | virtual void erase(const Item& item) { |
---|
| 1286 | Map::set(invMap.back(), Map::operator[](item)); |
---|
| 1287 | invMap[Map::operator[](item)] = invMap.back(); |
---|
[1413] | 1288 | invMap.pop_back(); |
---|
[1402] | 1289 | Map::erase(item); |
---|
| 1290 | } |
---|
| 1291 | |
---|
[1829] | 1292 | /// \brief Erase more keys from the map. |
---|
| 1293 | /// |
---|
| 1294 | /// Erase more keys from the map. It is called by the |
---|
| 1295 | /// \c AlterationNotifier. |
---|
| 1296 | virtual void erase(const std::vector<Item>& items) { |
---|
| 1297 | for (int i = 0; i < (int)items.size(); ++i) { |
---|
| 1298 | Map::set(invMap.back(), Map::operator[](items[i])); |
---|
| 1299 | invMap[Map::operator[](items[i])] = invMap.back(); |
---|
| 1300 | invMap.pop_back(); |
---|
| 1301 | } |
---|
| 1302 | Map::erase(items); |
---|
| 1303 | } |
---|
| 1304 | |
---|
[1402] | 1305 | /// \brief Build the unique map. |
---|
| 1306 | /// |
---|
| 1307 | /// Build the unique map. It is called by the |
---|
| 1308 | /// \c AlterationNotifier. |
---|
| 1309 | virtual void build() { |
---|
| 1310 | Map::build(); |
---|
| 1311 | Item it; |
---|
[1999] | 1312 | const typename Map::Notifier* notifier = Map::getNotifier(); |
---|
| 1313 | for (notifier->first(it); it != INVALID; notifier->next(it)) { |
---|
[1402] | 1314 | Map::set(it, invMap.size()); |
---|
| 1315 | invMap.push_back(it); |
---|
| 1316 | } |
---|
| 1317 | } |
---|
| 1318 | |
---|
| 1319 | /// \brief Clear the keys from the map. |
---|
| 1320 | /// |
---|
| 1321 | /// Clear the keys from the map. It is called by the |
---|
| 1322 | /// \c AlterationNotifier. |
---|
| 1323 | virtual void clear() { |
---|
| 1324 | invMap.clear(); |
---|
| 1325 | Map::clear(); |
---|
| 1326 | } |
---|
| 1327 | |
---|
[1538] | 1328 | public: |
---|
| 1329 | |
---|
[1931] | 1330 | /// \brief Returns the maximal value plus one. |
---|
| 1331 | /// |
---|
| 1332 | /// Returns the maximal value plus one in the map. |
---|
| 1333 | unsigned int size() const { |
---|
| 1334 | return invMap.size(); |
---|
| 1335 | } |
---|
| 1336 | |
---|
[1552] | 1337 | /// \brief Swaps the position of the two items in the map. |
---|
| 1338 | /// |
---|
| 1339 | /// Swaps the position of the two items in the map. |
---|
| 1340 | void swap(const Item& p, const Item& q) { |
---|
| 1341 | int pi = Map::operator[](p); |
---|
| 1342 | int qi = Map::operator[](q); |
---|
| 1343 | Map::set(p, qi); |
---|
| 1344 | invMap[qi] = p; |
---|
| 1345 | Map::set(q, pi); |
---|
| 1346 | invMap[pi] = q; |
---|
| 1347 | } |
---|
| 1348 | |
---|
[1402] | 1349 | /// \brief Gives back the \e descriptor of the item. |
---|
| 1350 | /// |
---|
| 1351 | /// Gives back the mutable and unique \e descriptor of the map. |
---|
| 1352 | int operator[](const Item& item) const { |
---|
| 1353 | return Map::operator[](item); |
---|
| 1354 | } |
---|
| 1355 | |
---|
[1413] | 1356 | private: |
---|
| 1357 | |
---|
| 1358 | typedef std::vector<Item> Container; |
---|
| 1359 | Container invMap; |
---|
| 1360 | |
---|
| 1361 | public: |
---|
[1540] | 1362 | /// \brief The inverse map type of DescriptorMap. |
---|
[1413] | 1363 | /// |
---|
[1540] | 1364 | /// The inverse map type of DescriptorMap. |
---|
[1413] | 1365 | class InverseMap { |
---|
| 1366 | public: |
---|
| 1367 | /// \brief Constructor of the InverseMap. |
---|
| 1368 | /// |
---|
| 1369 | /// Constructor of the InverseMap. |
---|
| 1370 | InverseMap(const DescriptorMap& _inverted) |
---|
| 1371 | : inverted(_inverted) {} |
---|
| 1372 | |
---|
| 1373 | |
---|
| 1374 | /// The value type of the InverseMap. |
---|
| 1375 | typedef typename DescriptorMap::Key Value; |
---|
| 1376 | /// The key type of the InverseMap. |
---|
| 1377 | typedef typename DescriptorMap::Value Key; |
---|
| 1378 | |
---|
| 1379 | /// \brief Subscript operator. |
---|
| 1380 | /// |
---|
| 1381 | /// Subscript operator. It gives back the item |
---|
| 1382 | /// that the descriptor belongs to currently. |
---|
| 1383 | Value operator[](const Key& key) const { |
---|
| 1384 | return inverted.invMap[key]; |
---|
| 1385 | } |
---|
[1470] | 1386 | |
---|
| 1387 | /// \brief Size of the map. |
---|
| 1388 | /// |
---|
| 1389 | /// Returns the size of the map. |
---|
[1931] | 1390 | unsigned int size() const { |
---|
[1470] | 1391 | return inverted.invMap.size(); |
---|
| 1392 | } |
---|
[1413] | 1393 | |
---|
| 1394 | private: |
---|
| 1395 | const DescriptorMap& inverted; |
---|
| 1396 | }; |
---|
| 1397 | |
---|
[1402] | 1398 | /// \brief Gives back the inverse of the map. |
---|
| 1399 | /// |
---|
| 1400 | /// Gives back the inverse of the map. |
---|
| 1401 | const InverseMap inverse() const { |
---|
[1413] | 1402 | return InverseMap(*this); |
---|
[1402] | 1403 | } |
---|
| 1404 | }; |
---|
| 1405 | |
---|
| 1406 | /// \brief Returns the source of the given edge. |
---|
| 1407 | /// |
---|
| 1408 | /// The SourceMap gives back the source Node of the given edge. |
---|
| 1409 | /// \author Balazs Dezso |
---|
| 1410 | template <typename Graph> |
---|
| 1411 | class SourceMap { |
---|
| 1412 | public: |
---|
[1419] | 1413 | |
---|
[1402] | 1414 | typedef typename Graph::Node Value; |
---|
| 1415 | typedef typename Graph::Edge Key; |
---|
| 1416 | |
---|
| 1417 | /// \brief Constructor |
---|
| 1418 | /// |
---|
| 1419 | /// Constructor |
---|
| 1420 | /// \param _graph The graph that the map belongs to. |
---|
| 1421 | SourceMap(const Graph& _graph) : graph(_graph) {} |
---|
| 1422 | |
---|
| 1423 | /// \brief The subscript operator. |
---|
| 1424 | /// |
---|
| 1425 | /// The subscript operator. |
---|
| 1426 | /// \param edge The edge |
---|
| 1427 | /// \return The source of the edge |
---|
[1679] | 1428 | Value operator[](const Key& edge) const { |
---|
[1402] | 1429 | return graph.source(edge); |
---|
| 1430 | } |
---|
| 1431 | |
---|
| 1432 | private: |
---|
| 1433 | const Graph& graph; |
---|
| 1434 | }; |
---|
| 1435 | |
---|
| 1436 | /// \brief Returns a \ref SourceMap class |
---|
| 1437 | /// |
---|
| 1438 | /// This function just returns an \ref SourceMap class. |
---|
| 1439 | /// \relates SourceMap |
---|
| 1440 | template <typename Graph> |
---|
| 1441 | inline SourceMap<Graph> sourceMap(const Graph& graph) { |
---|
| 1442 | return SourceMap<Graph>(graph); |
---|
| 1443 | } |
---|
| 1444 | |
---|
| 1445 | /// \brief Returns the target of the given edge. |
---|
| 1446 | /// |
---|
| 1447 | /// The TargetMap gives back the target Node of the given edge. |
---|
| 1448 | /// \author Balazs Dezso |
---|
| 1449 | template <typename Graph> |
---|
| 1450 | class TargetMap { |
---|
| 1451 | public: |
---|
[1419] | 1452 | |
---|
[1402] | 1453 | typedef typename Graph::Node Value; |
---|
| 1454 | typedef typename Graph::Edge Key; |
---|
| 1455 | |
---|
| 1456 | /// \brief Constructor |
---|
| 1457 | /// |
---|
| 1458 | /// Constructor |
---|
| 1459 | /// \param _graph The graph that the map belongs to. |
---|
| 1460 | TargetMap(const Graph& _graph) : graph(_graph) {} |
---|
| 1461 | |
---|
| 1462 | /// \brief The subscript operator. |
---|
| 1463 | /// |
---|
| 1464 | /// The subscript operator. |
---|
[1536] | 1465 | /// \param e The edge |
---|
[1402] | 1466 | /// \return The target of the edge |
---|
[1679] | 1467 | Value operator[](const Key& e) const { |
---|
[1536] | 1468 | return graph.target(e); |
---|
[1402] | 1469 | } |
---|
| 1470 | |
---|
| 1471 | private: |
---|
| 1472 | const Graph& graph; |
---|
| 1473 | }; |
---|
| 1474 | |
---|
| 1475 | /// \brief Returns a \ref TargetMap class |
---|
[1515] | 1476 | /// |
---|
[1540] | 1477 | /// This function just returns a \ref TargetMap class. |
---|
[1402] | 1478 | /// \relates TargetMap |
---|
| 1479 | template <typename Graph> |
---|
| 1480 | inline TargetMap<Graph> targetMap(const Graph& graph) { |
---|
| 1481 | return TargetMap<Graph>(graph); |
---|
| 1482 | } |
---|
| 1483 | |
---|
[1540] | 1484 | /// \brief Returns the "forward" directed edge view of an undirected edge. |
---|
[1419] | 1485 | /// |
---|
[1540] | 1486 | /// Returns the "forward" directed edge view of an undirected edge. |
---|
[1419] | 1487 | /// \author Balazs Dezso |
---|
| 1488 | template <typename Graph> |
---|
| 1489 | class ForwardMap { |
---|
| 1490 | public: |
---|
| 1491 | |
---|
| 1492 | typedef typename Graph::Edge Value; |
---|
[1909] | 1493 | typedef typename Graph::UEdge Key; |
---|
[1419] | 1494 | |
---|
| 1495 | /// \brief Constructor |
---|
| 1496 | /// |
---|
| 1497 | /// Constructor |
---|
| 1498 | /// \param _graph The graph that the map belongs to. |
---|
| 1499 | ForwardMap(const Graph& _graph) : graph(_graph) {} |
---|
| 1500 | |
---|
| 1501 | /// \brief The subscript operator. |
---|
| 1502 | /// |
---|
| 1503 | /// The subscript operator. |
---|
| 1504 | /// \param key An undirected edge |
---|
| 1505 | /// \return The "forward" directed edge view of undirected edge |
---|
| 1506 | Value operator[](const Key& key) const { |
---|
[1627] | 1507 | return graph.direct(key, true); |
---|
[1419] | 1508 | } |
---|
| 1509 | |
---|
| 1510 | private: |
---|
| 1511 | const Graph& graph; |
---|
| 1512 | }; |
---|
| 1513 | |
---|
| 1514 | /// \brief Returns a \ref ForwardMap class |
---|
[1515] | 1515 | /// |
---|
[1419] | 1516 | /// This function just returns an \ref ForwardMap class. |
---|
| 1517 | /// \relates ForwardMap |
---|
| 1518 | template <typename Graph> |
---|
| 1519 | inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
---|
| 1520 | return ForwardMap<Graph>(graph); |
---|
| 1521 | } |
---|
| 1522 | |
---|
[1540] | 1523 | /// \brief Returns the "backward" directed edge view of an undirected edge. |
---|
[1419] | 1524 | /// |
---|
[1540] | 1525 | /// Returns the "backward" directed edge view of an undirected edge. |
---|
[1419] | 1526 | /// \author Balazs Dezso |
---|
| 1527 | template <typename Graph> |
---|
| 1528 | class BackwardMap { |
---|
| 1529 | public: |
---|
| 1530 | |
---|
| 1531 | typedef typename Graph::Edge Value; |
---|
[1909] | 1532 | typedef typename Graph::UEdge Key; |
---|
[1419] | 1533 | |
---|
| 1534 | /// \brief Constructor |
---|
| 1535 | /// |
---|
| 1536 | /// Constructor |
---|
| 1537 | /// \param _graph The graph that the map belongs to. |
---|
| 1538 | BackwardMap(const Graph& _graph) : graph(_graph) {} |
---|
| 1539 | |
---|
| 1540 | /// \brief The subscript operator. |
---|
| 1541 | /// |
---|
| 1542 | /// The subscript operator. |
---|
| 1543 | /// \param key An undirected edge |
---|
| 1544 | /// \return The "backward" directed edge view of undirected edge |
---|
| 1545 | Value operator[](const Key& key) const { |
---|
[1627] | 1546 | return graph.direct(key, false); |
---|
[1419] | 1547 | } |
---|
| 1548 | |
---|
| 1549 | private: |
---|
| 1550 | const Graph& graph; |
---|
| 1551 | }; |
---|
| 1552 | |
---|
| 1553 | /// \brief Returns a \ref BackwardMap class |
---|
| 1554 | |
---|
[1540] | 1555 | /// This function just returns a \ref BackwardMap class. |
---|
[1419] | 1556 | /// \relates BackwardMap |
---|
| 1557 | template <typename Graph> |
---|
| 1558 | inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
---|
| 1559 | return BackwardMap<Graph>(graph); |
---|
| 1560 | } |
---|
| 1561 | |
---|
[1695] | 1562 | /// \brief Potential difference map |
---|
| 1563 | /// |
---|
| 1564 | /// If there is an potential map on the nodes then we |
---|
| 1565 | /// can get an edge map as we get the substraction of the |
---|
| 1566 | /// values of the target and source. |
---|
| 1567 | template <typename Graph, typename NodeMap> |
---|
| 1568 | class PotentialDifferenceMap { |
---|
[1515] | 1569 | public: |
---|
[1695] | 1570 | typedef typename Graph::Edge Key; |
---|
| 1571 | typedef typename NodeMap::Value Value; |
---|
| 1572 | |
---|
| 1573 | /// \brief Constructor |
---|
| 1574 | /// |
---|
| 1575 | /// Contructor of the map |
---|
| 1576 | PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) |
---|
| 1577 | : graph(_graph), potential(_potential) {} |
---|
| 1578 | |
---|
| 1579 | /// \brief Const subscription operator |
---|
| 1580 | /// |
---|
| 1581 | /// Const subscription operator |
---|
| 1582 | Value operator[](const Key& edge) const { |
---|
| 1583 | return potential[graph.target(edge)] - potential[graph.source(edge)]; |
---|
| 1584 | } |
---|
| 1585 | |
---|
| 1586 | private: |
---|
| 1587 | const Graph& graph; |
---|
| 1588 | const NodeMap& potential; |
---|
| 1589 | }; |
---|
| 1590 | |
---|
| 1591 | /// \brief Just returns a PotentialDifferenceMap |
---|
| 1592 | /// |
---|
| 1593 | /// Just returns a PotentialDifferenceMap |
---|
| 1594 | /// \relates PotentialDifferenceMap |
---|
| 1595 | template <typename Graph, typename NodeMap> |
---|
| 1596 | PotentialDifferenceMap<Graph, NodeMap> |
---|
| 1597 | potentialDifferenceMap(const Graph& graph, const NodeMap& potential) { |
---|
| 1598 | return PotentialDifferenceMap<Graph, NodeMap>(graph, potential); |
---|
| 1599 | } |
---|
| 1600 | |
---|
[1515] | 1601 | /// \brief Map of the node in-degrees. |
---|
[1453] | 1602 | /// |
---|
[1540] | 1603 | /// This map returns the in-degree of a node. Once it is constructed, |
---|
[1515] | 1604 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
[1540] | 1605 | /// in constant time. On the other hand, the values are updated automatically |
---|
[1515] | 1606 | /// whenever the graph changes. |
---|
| 1607 | /// |
---|
[1729] | 1608 | /// \warning Besides addNode() and addEdge(), a graph structure may provide |
---|
[1730] | 1609 | /// alternative ways to modify the graph. The correct behavior of InDegMap |
---|
[1829] | 1610 | /// is not guarantied if these additional features are used. For example |
---|
| 1611 | /// the functions \ref ListGraph::changeSource() "changeSource()", |
---|
[1729] | 1612 | /// \ref ListGraph::changeTarget() "changeTarget()" and |
---|
| 1613 | /// \ref ListGraph::reverseEdge() "reverseEdge()" |
---|
| 1614 | /// of \ref ListGraph will \e not update the degree values correctly. |
---|
| 1615 | /// |
---|
[1515] | 1616 | /// \sa OutDegMap |
---|
| 1617 | |
---|
[1453] | 1618 | template <typename _Graph> |
---|
[1515] | 1619 | class InDegMap |
---|
[1999] | 1620 | : protected ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 1621 | ::ItemNotifier::ObserverBase { |
---|
[1515] | 1622 | |
---|
[1453] | 1623 | public: |
---|
[1515] | 1624 | |
---|
| 1625 | typedef _Graph Graph; |
---|
[1453] | 1626 | typedef int Value; |
---|
[1515] | 1627 | typedef typename Graph::Node Key; |
---|
| 1628 | |
---|
[1999] | 1629 | typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 1630 | ::ItemNotifier::ObserverBase Parent; |
---|
| 1631 | |
---|
[1515] | 1632 | private: |
---|
| 1633 | |
---|
[1990] | 1634 | class AutoNodeMap : public DefaultMap<_Graph, Key, int> { |
---|
[1515] | 1635 | public: |
---|
| 1636 | |
---|
[1990] | 1637 | typedef DefaultMap<_Graph, Key, int> Parent; |
---|
[2002] | 1638 | typedef typename Parent::Graph Graph; |
---|
[1515] | 1639 | |
---|
| 1640 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 1641 | |
---|
[1829] | 1642 | virtual void add(const Key& key) { |
---|
[1515] | 1643 | Parent::add(key); |
---|
| 1644 | Parent::set(key, 0); |
---|
| 1645 | } |
---|
[1931] | 1646 | |
---|
[1829] | 1647 | virtual void add(const std::vector<Key>& keys) { |
---|
| 1648 | Parent::add(keys); |
---|
| 1649 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 1650 | Parent::set(keys[i], 0); |
---|
| 1651 | } |
---|
| 1652 | } |
---|
[1515] | 1653 | }; |
---|
| 1654 | |
---|
| 1655 | public: |
---|
[1453] | 1656 | |
---|
| 1657 | /// \brief Constructor. |
---|
| 1658 | /// |
---|
| 1659 | /// Constructor for creating in-degree map. |
---|
[1515] | 1660 | InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[1999] | 1661 | Parent::attach(graph.getNotifier(typename _Graph::Edge())); |
---|
[1515] | 1662 | |
---|
| 1663 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1664 | deg[it] = countInEdges(graph, it); |
---|
| 1665 | } |
---|
[1453] | 1666 | } |
---|
| 1667 | |
---|
[1459] | 1668 | /// Gives back the in-degree of a Node. |
---|
[1515] | 1669 | int operator[](const Key& key) const { |
---|
| 1670 | return deg[key]; |
---|
[1459] | 1671 | } |
---|
[1453] | 1672 | |
---|
| 1673 | protected: |
---|
[1515] | 1674 | |
---|
| 1675 | typedef typename Graph::Edge Edge; |
---|
| 1676 | |
---|
| 1677 | virtual void add(const Edge& edge) { |
---|
| 1678 | ++deg[graph.target(edge)]; |
---|
[1453] | 1679 | } |
---|
| 1680 | |
---|
[1931] | 1681 | virtual void add(const std::vector<Edge>& edges) { |
---|
| 1682 | for (int i = 0; i < (int)edges.size(); ++i) { |
---|
| 1683 | ++deg[graph.target(edges[i])]; |
---|
| 1684 | } |
---|
| 1685 | } |
---|
| 1686 | |
---|
[1515] | 1687 | virtual void erase(const Edge& edge) { |
---|
| 1688 | --deg[graph.target(edge)]; |
---|
| 1689 | } |
---|
| 1690 | |
---|
[1931] | 1691 | virtual void erase(const std::vector<Edge>& edges) { |
---|
| 1692 | for (int i = 0; i < (int)edges.size(); ++i) { |
---|
| 1693 | --deg[graph.target(edges[i])]; |
---|
| 1694 | } |
---|
| 1695 | } |
---|
| 1696 | |
---|
[1515] | 1697 | virtual void build() { |
---|
| 1698 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1699 | deg[it] = countInEdges(graph, it); |
---|
| 1700 | } |
---|
| 1701 | } |
---|
| 1702 | |
---|
| 1703 | virtual void clear() { |
---|
| 1704 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1705 | deg[it] = 0; |
---|
| 1706 | } |
---|
| 1707 | } |
---|
| 1708 | private: |
---|
[1506] | 1709 | |
---|
[1515] | 1710 | const _Graph& graph; |
---|
| 1711 | AutoNodeMap deg; |
---|
[1459] | 1712 | }; |
---|
| 1713 | |
---|
[1515] | 1714 | /// \brief Map of the node out-degrees. |
---|
| 1715 | /// |
---|
[1540] | 1716 | /// This map returns the out-degree of a node. Once it is constructed, |
---|
[1515] | 1717 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
[1540] | 1718 | /// in constant time. On the other hand, the values are updated automatically |
---|
[1515] | 1719 | /// whenever the graph changes. |
---|
| 1720 | /// |
---|
[1729] | 1721 | /// \warning Besides addNode() and addEdge(), a graph structure may provide |
---|
[1730] | 1722 | /// alternative ways to modify the graph. The correct behavior of OutDegMap |
---|
[1829] | 1723 | /// is not guarantied if these additional features are used. For example |
---|
| 1724 | /// the functions \ref ListGraph::changeSource() "changeSource()", |
---|
[1729] | 1725 | /// \ref ListGraph::changeTarget() "changeTarget()" and |
---|
| 1726 | /// \ref ListGraph::reverseEdge() "reverseEdge()" |
---|
| 1727 | /// of \ref ListGraph will \e not update the degree values correctly. |
---|
| 1728 | /// |
---|
[1555] | 1729 | /// \sa InDegMap |
---|
[1459] | 1730 | |
---|
| 1731 | template <typename _Graph> |
---|
[1515] | 1732 | class OutDegMap |
---|
[1999] | 1733 | : protected ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 1734 | ::ItemNotifier::ObserverBase { |
---|
[1515] | 1735 | |
---|
[1459] | 1736 | public: |
---|
[1999] | 1737 | |
---|
| 1738 | typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 1739 | ::ItemNotifier::ObserverBase Parent; |
---|
[1515] | 1740 | |
---|
| 1741 | typedef _Graph Graph; |
---|
[1459] | 1742 | typedef int Value; |
---|
[1515] | 1743 | typedef typename Graph::Node Key; |
---|
| 1744 | |
---|
| 1745 | private: |
---|
| 1746 | |
---|
[1990] | 1747 | class AutoNodeMap : public DefaultMap<_Graph, Key, int> { |
---|
[1515] | 1748 | public: |
---|
| 1749 | |
---|
[1990] | 1750 | typedef DefaultMap<_Graph, Key, int> Parent; |
---|
[2002] | 1751 | typedef typename Parent::Graph Graph; |
---|
[1515] | 1752 | |
---|
| 1753 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 1754 | |
---|
[1829] | 1755 | virtual void add(const Key& key) { |
---|
[1515] | 1756 | Parent::add(key); |
---|
| 1757 | Parent::set(key, 0); |
---|
| 1758 | } |
---|
[1829] | 1759 | virtual void add(const std::vector<Key>& keys) { |
---|
| 1760 | Parent::add(keys); |
---|
| 1761 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 1762 | Parent::set(keys[i], 0); |
---|
| 1763 | } |
---|
| 1764 | } |
---|
[1515] | 1765 | }; |
---|
| 1766 | |
---|
| 1767 | public: |
---|
[1459] | 1768 | |
---|
| 1769 | /// \brief Constructor. |
---|
| 1770 | /// |
---|
| 1771 | /// Constructor for creating out-degree map. |
---|
[1515] | 1772 | OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[1999] | 1773 | Parent::attach(graph.getNotifier(typename _Graph::Edge())); |
---|
[1515] | 1774 | |
---|
| 1775 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1776 | deg[it] = countOutEdges(graph, it); |
---|
| 1777 | } |
---|
[1459] | 1778 | } |
---|
| 1779 | |
---|
[1990] | 1780 | /// Gives back the out-degree of a Node. |
---|
[1515] | 1781 | int operator[](const Key& key) const { |
---|
| 1782 | return deg[key]; |
---|
[1459] | 1783 | } |
---|
| 1784 | |
---|
| 1785 | protected: |
---|
[1515] | 1786 | |
---|
| 1787 | typedef typename Graph::Edge Edge; |
---|
| 1788 | |
---|
| 1789 | virtual void add(const Edge& edge) { |
---|
| 1790 | ++deg[graph.source(edge)]; |
---|
[1459] | 1791 | } |
---|
| 1792 | |
---|
[1931] | 1793 | virtual void add(const std::vector<Edge>& edges) { |
---|
| 1794 | for (int i = 0; i < (int)edges.size(); ++i) { |
---|
| 1795 | ++deg[graph.source(edges[i])]; |
---|
| 1796 | } |
---|
| 1797 | } |
---|
| 1798 | |
---|
[1515] | 1799 | virtual void erase(const Edge& edge) { |
---|
| 1800 | --deg[graph.source(edge)]; |
---|
| 1801 | } |
---|
| 1802 | |
---|
[1931] | 1803 | virtual void erase(const std::vector<Edge>& edges) { |
---|
| 1804 | for (int i = 0; i < (int)edges.size(); ++i) { |
---|
| 1805 | --deg[graph.source(edges[i])]; |
---|
| 1806 | } |
---|
| 1807 | } |
---|
| 1808 | |
---|
[1515] | 1809 | virtual void build() { |
---|
| 1810 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1811 | deg[it] = countOutEdges(graph, it); |
---|
| 1812 | } |
---|
| 1813 | } |
---|
| 1814 | |
---|
| 1815 | virtual void clear() { |
---|
| 1816 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1817 | deg[it] = 0; |
---|
| 1818 | } |
---|
| 1819 | } |
---|
| 1820 | private: |
---|
[1506] | 1821 | |
---|
[1515] | 1822 | const _Graph& graph; |
---|
| 1823 | AutoNodeMap deg; |
---|
[1453] | 1824 | }; |
---|
| 1825 | |
---|
[1695] | 1826 | |
---|
[1402] | 1827 | /// @} |
---|
| 1828 | |
---|
[947] | 1829 | } //END OF NAMESPACE LEMON |
---|
[946] | 1830 | |
---|
| 1831 | #endif |
---|