[946] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2391] | 5 | * Copyright (C) 2003-2007 |
---|
[1956] | 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> |
---|
[2235] | 26 | #include <algorithm> |
---|
[946] | 27 | |
---|
[1993] | 28 | #include <lemon/bits/invalid.h> |
---|
| 29 | #include <lemon/bits/utility.h> |
---|
[1413] | 30 | #include <lemon/maps.h> |
---|
[1993] | 31 | #include <lemon/bits/traits.h> |
---|
[1990] | 32 | |
---|
[1459] | 33 | #include <lemon/bits/alteration_notifier.h> |
---|
[1990] | 34 | #include <lemon/bits/default_map.h> |
---|
[946] | 35 | |
---|
[947] | 36 | ///\ingroup gutils |
---|
[946] | 37 | ///\file |
---|
[947] | 38 | ///\brief Graph utilities. |
---|
[946] | 39 | |
---|
| 40 | namespace lemon { |
---|
| 41 | |
---|
[1267] | 42 | /// \addtogroup gutils |
---|
| 43 | /// @{ |
---|
[947] | 44 | |
---|
[1756] | 45 | ///Creates convenience typedefs for the graph types and iterators |
---|
| 46 | |
---|
| 47 | ///This \c \#define creates convenience typedefs for the following types |
---|
| 48 | ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt, |
---|
[2031] | 49 | ///\c OutEdgeIt |
---|
[1756] | 50 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 51 | ///\code |
---|
[2510] | 52 | /// GRAPH_TYPEDEFS(typename G); |
---|
[1756] | 53 | ///\endcode |
---|
| 54 | /// |
---|
| 55 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 56 | ///template typedefs in C++. |
---|
[1804] | 57 | #define GRAPH_TYPEDEFS(Graph) \ |
---|
| 58 | typedef Graph:: Node Node; \ |
---|
| 59 | typedef Graph:: NodeIt NodeIt; \ |
---|
| 60 | typedef Graph:: Edge Edge; \ |
---|
| 61 | typedef Graph:: EdgeIt EdgeIt; \ |
---|
| 62 | typedef Graph:: InEdgeIt InEdgeIt; \ |
---|
[2510] | 63 | typedef Graph::OutEdgeIt OutEdgeIt |
---|
[2031] | 64 | |
---|
[1756] | 65 | ///Creates convenience typedefs for the undirected graph types and iterators |
---|
| 66 | |
---|
| 67 | ///This \c \#define creates the same convenience typedefs as defined by |
---|
| 68 | ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates |
---|
[1909] | 69 | ///\c UEdge, \c UEdgeIt, \c IncEdgeIt, |
---|
[1756] | 70 | /// |
---|
| 71 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 72 | ///\code |
---|
[2510] | 73 | /// UGRAPH_TYPEDEFS(typename G); |
---|
[1756] | 74 | ///\endcode |
---|
| 75 | /// |
---|
| 76 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 77 | ///template typedefs in C++. |
---|
[1992] | 78 | #define UGRAPH_TYPEDEFS(Graph) \ |
---|
[2510] | 79 | GRAPH_TYPEDEFS(Graph); \ |
---|
[1909] | 80 | typedef Graph:: UEdge UEdge; \ |
---|
| 81 | typedef Graph:: UEdgeIt UEdgeIt; \ |
---|
[2510] | 82 | typedef Graph:: IncEdgeIt IncEdgeIt |
---|
[1756] | 83 | |
---|
[2031] | 84 | ///\brief Creates convenience typedefs for the bipartite undirected graph |
---|
| 85 | ///types and iterators |
---|
| 86 | |
---|
| 87 | ///This \c \#define creates the same convenience typedefs as defined by |
---|
| 88 | ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates |
---|
| 89 | ///\c ANodeIt, \c BNodeIt, |
---|
| 90 | /// |
---|
| 91 | ///\note If \c G it a template parameter, it should be used in this way. |
---|
| 92 | ///\code |
---|
[2510] | 93 | /// BPUGRAPH_TYPEDEFS(typename G); |
---|
[2031] | 94 | ///\endcode |
---|
| 95 | /// |
---|
| 96 | ///\warning There are no typedefs for the graph maps because of the lack of |
---|
| 97 | ///template typedefs in C++. |
---|
| 98 | #define BPUGRAPH_TYPEDEFS(Graph) \ |
---|
[2510] | 99 | UGRAPH_TYPEDEFS(Graph); \ |
---|
[2286] | 100 | typedef Graph::ANode ANode; \ |
---|
| 101 | typedef Graph::BNode BNode; \ |
---|
[2031] | 102 | typedef Graph::ANodeIt ANodeIt; \ |
---|
[2510] | 103 | typedef Graph::BNodeIt BNodeIt |
---|
[1756] | 104 | |
---|
[946] | 105 | /// \brief Function to count the items in the graph. |
---|
| 106 | /// |
---|
[1540] | 107 | /// This function counts the items (nodes, edges etc) in the graph. |
---|
[946] | 108 | /// The complexity of the function is O(n) because |
---|
| 109 | /// it iterates on all of the items. |
---|
| 110 | |
---|
[2020] | 111 | template <typename Graph, typename Item> |
---|
[977] | 112 | inline int countItems(const Graph& g) { |
---|
[2020] | 113 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
[946] | 114 | int num = 0; |
---|
[977] | 115 | for (ItemIt it(g); it != INVALID; ++it) { |
---|
[946] | 116 | ++num; |
---|
| 117 | } |
---|
| 118 | return num; |
---|
| 119 | } |
---|
| 120 | |
---|
[977] | 121 | // Node counting: |
---|
| 122 | |
---|
[2020] | 123 | namespace _graph_utils_bits { |
---|
| 124 | |
---|
| 125 | template <typename Graph, typename Enable = void> |
---|
| 126 | struct CountNodesSelector { |
---|
| 127 | static int count(const Graph &g) { |
---|
| 128 | return countItems<Graph, typename Graph::Node>(g); |
---|
| 129 | } |
---|
| 130 | }; |
---|
[977] | 131 | |
---|
[2020] | 132 | template <typename Graph> |
---|
| 133 | struct CountNodesSelector< |
---|
| 134 | Graph, typename |
---|
| 135 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 136 | { |
---|
| 137 | static int count(const Graph &g) { |
---|
| 138 | return g.nodeNum(); |
---|
| 139 | } |
---|
| 140 | }; |
---|
[977] | 141 | } |
---|
| 142 | |
---|
[946] | 143 | /// \brief Function to count the nodes in the graph. |
---|
| 144 | /// |
---|
| 145 | /// This function counts the nodes in the graph. |
---|
| 146 | /// The complexity of the function is O(n) but for some |
---|
[1526] | 147 | /// graph structures it is specialized to run in O(1). |
---|
[977] | 148 | /// |
---|
[2485] | 149 | /// If the graph contains a \e nodeNum() member function and a |
---|
| 150 | /// \e NodeNumTag tag then this function calls directly the member |
---|
| 151 | /// function to query the cardinality of the node set. |
---|
[946] | 152 | template <typename Graph> |
---|
[977] | 153 | inline int countNodes(const Graph& g) { |
---|
[2020] | 154 | return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
---|
[977] | 155 | } |
---|
| 156 | |
---|
[2029] | 157 | namespace _graph_utils_bits { |
---|
| 158 | |
---|
| 159 | template <typename Graph, typename Enable = void> |
---|
| 160 | struct CountANodesSelector { |
---|
| 161 | static int count(const Graph &g) { |
---|
| 162 | return countItems<Graph, typename Graph::ANode>(g); |
---|
| 163 | } |
---|
| 164 | }; |
---|
| 165 | |
---|
| 166 | template <typename Graph> |
---|
| 167 | struct CountANodesSelector< |
---|
| 168 | Graph, typename |
---|
| 169 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 170 | { |
---|
| 171 | static int count(const Graph &g) { |
---|
[2186] | 172 | return g.aNodeNum(); |
---|
[2029] | 173 | } |
---|
| 174 | }; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /// \brief Function to count the anodes in the graph. |
---|
| 178 | /// |
---|
| 179 | /// This function counts the anodes in the graph. |
---|
| 180 | /// The complexity of the function is O(an) but for some |
---|
| 181 | /// graph structures it is specialized to run in O(1). |
---|
| 182 | /// |
---|
[2485] | 183 | /// If the graph contains an \e aNodeNum() member function and a |
---|
| 184 | /// \e NodeNumTag tag then this function calls directly the member |
---|
| 185 | /// function to query the cardinality of the A-node set. |
---|
[2029] | 186 | template <typename Graph> |
---|
| 187 | inline int countANodes(const Graph& g) { |
---|
| 188 | return _graph_utils_bits::CountANodesSelector<Graph>::count(g); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | namespace _graph_utils_bits { |
---|
| 192 | |
---|
| 193 | template <typename Graph, typename Enable = void> |
---|
| 194 | struct CountBNodesSelector { |
---|
| 195 | static int count(const Graph &g) { |
---|
| 196 | return countItems<Graph, typename Graph::BNode>(g); |
---|
| 197 | } |
---|
| 198 | }; |
---|
| 199 | |
---|
| 200 | template <typename Graph> |
---|
| 201 | struct CountBNodesSelector< |
---|
| 202 | Graph, typename |
---|
| 203 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 204 | { |
---|
| 205 | static int count(const Graph &g) { |
---|
[2186] | 206 | return g.bNodeNum(); |
---|
[2029] | 207 | } |
---|
| 208 | }; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /// \brief Function to count the bnodes in the graph. |
---|
| 212 | /// |
---|
| 213 | /// This function counts the bnodes in the graph. |
---|
| 214 | /// The complexity of the function is O(bn) but for some |
---|
| 215 | /// graph structures it is specialized to run in O(1). |
---|
| 216 | /// |
---|
[2485] | 217 | /// If the graph contains a \e bNodeNum() member function and a |
---|
| 218 | /// \e NodeNumTag tag then this function calls directly the member |
---|
| 219 | /// function to query the cardinality of the B-node set. |
---|
[2029] | 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). |
---|
[2485] | 253 | /// |
---|
| 254 | /// If the graph contains a \e edgeNum() member function and a |
---|
| 255 | /// \e EdgeNumTag tag then this function calls directly the member |
---|
| 256 | /// function to query the cardinality of the edge set. |
---|
[946] | 257 | template <typename Graph> |
---|
[977] | 258 | inline int countEdges(const Graph& g) { |
---|
[2020] | 259 | return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
---|
[946] | 260 | } |
---|
| 261 | |
---|
[1053] | 262 | // Undirected edge counting: |
---|
[2020] | 263 | namespace _graph_utils_bits { |
---|
| 264 | |
---|
| 265 | template <typename Graph, typename Enable = void> |
---|
| 266 | struct CountUEdgesSelector { |
---|
| 267 | static int count(const Graph &g) { |
---|
| 268 | return countItems<Graph, typename Graph::UEdge>(g); |
---|
| 269 | } |
---|
| 270 | }; |
---|
[1053] | 271 | |
---|
[2020] | 272 | template <typename Graph> |
---|
| 273 | struct CountUEdgesSelector< |
---|
| 274 | Graph, |
---|
| 275 | typename enable_if<typename Graph::EdgeNumTag, void>::type> |
---|
| 276 | { |
---|
| 277 | static int count(const Graph &g) { |
---|
| 278 | return g.uEdgeNum(); |
---|
| 279 | } |
---|
| 280 | }; |
---|
[1053] | 281 | } |
---|
| 282 | |
---|
[1526] | 283 | /// \brief Function to count the undirected edges in the graph. |
---|
[946] | 284 | /// |
---|
[1526] | 285 | /// This function counts the undirected edges in the graph. |
---|
[946] | 286 | /// The complexity of the function is O(e) but for some |
---|
[1540] | 287 | /// graph structures it is specialized to run in O(1). |
---|
[2485] | 288 | /// |
---|
| 289 | /// If the graph contains a \e uEdgeNum() member function and a |
---|
| 290 | /// \e EdgeNumTag tag then this function calls directly the member |
---|
| 291 | /// function to query the cardinality of the undirected edge set. |
---|
[946] | 292 | template <typename Graph> |
---|
[1909] | 293 | inline int countUEdges(const Graph& g) { |
---|
[2020] | 294 | return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g); |
---|
| 295 | |
---|
[946] | 296 | } |
---|
| 297 | |
---|
[977] | 298 | |
---|
[946] | 299 | template <typename Graph, typename DegIt> |
---|
| 300 | inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 301 | int num = 0; |
---|
| 302 | for (DegIt it(_g, _n); it != INVALID; ++it) { |
---|
| 303 | ++num; |
---|
| 304 | } |
---|
| 305 | return num; |
---|
| 306 | } |
---|
[967] | 307 | |
---|
[1531] | 308 | /// \brief Function to count the number of the out-edges from node \c n. |
---|
| 309 | /// |
---|
| 310 | /// This function counts the number of the out-edges from node \c n |
---|
| 311 | /// in the graph. |
---|
| 312 | template <typename Graph> |
---|
| 313 | inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 314 | return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /// \brief Function to count the number of the in-edges to node \c n. |
---|
| 318 | /// |
---|
| 319 | /// This function counts the number of the in-edges to node \c n |
---|
| 320 | /// in the graph. |
---|
| 321 | template <typename Graph> |
---|
| 322 | inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 323 | return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n); |
---|
| 324 | } |
---|
| 325 | |
---|
[1704] | 326 | /// \brief Function to count the number of the inc-edges to node \c n. |
---|
[1679] | 327 | /// |
---|
[1704] | 328 | /// This function counts the number of the inc-edges to node \c n |
---|
[1679] | 329 | /// in the graph. |
---|
| 330 | template <typename Graph> |
---|
| 331 | inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 332 | return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
---|
| 333 | } |
---|
| 334 | |
---|
[2020] | 335 | namespace _graph_utils_bits { |
---|
| 336 | |
---|
| 337 | template <typename Graph, typename Enable = void> |
---|
| 338 | struct FindEdgeSelector { |
---|
| 339 | typedef typename Graph::Node Node; |
---|
| 340 | typedef typename Graph::Edge Edge; |
---|
| 341 | static Edge find(const Graph &g, Node u, Node v, Edge e) { |
---|
| 342 | if (e == INVALID) { |
---|
| 343 | g.firstOut(e, u); |
---|
| 344 | } else { |
---|
| 345 | g.nextOut(e); |
---|
| 346 | } |
---|
| 347 | while (e != INVALID && g.target(e) != v) { |
---|
| 348 | g.nextOut(e); |
---|
| 349 | } |
---|
| 350 | return e; |
---|
| 351 | } |
---|
| 352 | }; |
---|
[1531] | 353 | |
---|
[2020] | 354 | template <typename Graph> |
---|
| 355 | struct FindEdgeSelector< |
---|
| 356 | Graph, |
---|
| 357 | typename enable_if<typename Graph::FindEdgeTag, void>::type> |
---|
| 358 | { |
---|
| 359 | typedef typename Graph::Node Node; |
---|
| 360 | typedef typename Graph::Edge Edge; |
---|
| 361 | static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
---|
| 362 | return g.findEdge(u, v, prev); |
---|
| 363 | } |
---|
| 364 | }; |
---|
[1565] | 365 | } |
---|
| 366 | |
---|
| 367 | /// \brief Finds an edge between two nodes of a graph. |
---|
| 368 | /// |
---|
[967] | 369 | /// Finds an edge from node \c u to node \c v in graph \c g. |
---|
| 370 | /// |
---|
| 371 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 372 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 373 | /// the next edge from \c u to \c v after \c prev. |
---|
| 374 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 375 | /// |
---|
| 376 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
[1946] | 377 | ///\code |
---|
[967] | 378 | /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) { |
---|
| 379 | /// ... |
---|
| 380 | /// } |
---|
[1946] | 381 | ///\endcode |
---|
[2155] | 382 | /// |
---|
[2235] | 383 | ///\sa EdgeLookUp |
---|
[2476] | 384 | ///\sa AllEdgeLookUp |
---|
[2155] | 385 | ///\sa ConEdgeIt |
---|
[967] | 386 | template <typename Graph> |
---|
[2286] | 387 | inline typename Graph::Edge |
---|
| 388 | findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
---|
| 389 | typename Graph::Edge prev = INVALID) { |
---|
[2020] | 390 | return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev); |
---|
[967] | 391 | } |
---|
[1531] | 392 | |
---|
[1565] | 393 | /// \brief Iterator for iterating on edges connected the same nodes. |
---|
| 394 | /// |
---|
| 395 | /// Iterator for iterating on edges connected the same nodes. It is |
---|
| 396 | /// higher level interface for the findEdge() function. You can |
---|
[1591] | 397 | /// use it the following way: |
---|
[1946] | 398 | ///\code |
---|
[1565] | 399 | /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
---|
| 400 | /// ... |
---|
| 401 | /// } |
---|
[1946] | 402 | ///\endcode |
---|
[2155] | 403 | /// |
---|
| 404 | ///\sa findEdge() |
---|
[2235] | 405 | ///\sa EdgeLookUp |
---|
[2474] | 406 | ///\sa AllEdgeLookUp |
---|
[1565] | 407 | /// |
---|
| 408 | /// \author Balazs Dezso |
---|
| 409 | template <typename _Graph> |
---|
| 410 | class ConEdgeIt : public _Graph::Edge { |
---|
| 411 | public: |
---|
| 412 | |
---|
| 413 | typedef _Graph Graph; |
---|
| 414 | typedef typename Graph::Edge Parent; |
---|
| 415 | |
---|
| 416 | typedef typename Graph::Edge Edge; |
---|
| 417 | typedef typename Graph::Node Node; |
---|
| 418 | |
---|
| 419 | /// \brief Constructor. |
---|
| 420 | /// |
---|
| 421 | /// Construct a new ConEdgeIt iterating on the edges which |
---|
| 422 | /// connects the \c u and \c v node. |
---|
| 423 | ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) { |
---|
| 424 | Parent::operator=(findEdge(graph, u, v)); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /// \brief Constructor. |
---|
| 428 | /// |
---|
| 429 | /// Construct a new ConEdgeIt which continues the iterating from |
---|
| 430 | /// the \c e edge. |
---|
| 431 | ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {} |
---|
| 432 | |
---|
| 433 | /// \brief Increment operator. |
---|
| 434 | /// |
---|
| 435 | /// It increments the iterator and gives back the next edge. |
---|
| 436 | ConEdgeIt& operator++() { |
---|
| 437 | Parent::operator=(findEdge(graph, graph.source(*this), |
---|
| 438 | graph.target(*this), *this)); |
---|
| 439 | return *this; |
---|
| 440 | } |
---|
| 441 | private: |
---|
| 442 | const Graph& graph; |
---|
| 443 | }; |
---|
| 444 | |
---|
[2020] | 445 | namespace _graph_utils_bits { |
---|
| 446 | |
---|
| 447 | template <typename Graph, typename Enable = void> |
---|
| 448 | struct FindUEdgeSelector { |
---|
| 449 | typedef typename Graph::Node Node; |
---|
| 450 | typedef typename Graph::UEdge UEdge; |
---|
| 451 | static UEdge find(const Graph &g, Node u, Node v, UEdge e) { |
---|
| 452 | bool b; |
---|
| 453 | if (u != v) { |
---|
| 454 | if (e == INVALID) { |
---|
[2031] | 455 | g.firstInc(e, b, u); |
---|
[2020] | 456 | } else { |
---|
| 457 | b = g.source(e) == u; |
---|
| 458 | g.nextInc(e, b); |
---|
| 459 | } |
---|
[2064] | 460 | while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { |
---|
[2020] | 461 | g.nextInc(e, b); |
---|
| 462 | } |
---|
| 463 | } else { |
---|
| 464 | if (e == INVALID) { |
---|
[2031] | 465 | g.firstInc(e, b, u); |
---|
[2020] | 466 | } else { |
---|
| 467 | b = true; |
---|
| 468 | g.nextInc(e, b); |
---|
| 469 | } |
---|
| 470 | while (e != INVALID && (!b || g.target(e) != v)) { |
---|
| 471 | g.nextInc(e, b); |
---|
| 472 | } |
---|
| 473 | } |
---|
| 474 | return e; |
---|
| 475 | } |
---|
| 476 | }; |
---|
[1704] | 477 | |
---|
[2020] | 478 | template <typename Graph> |
---|
| 479 | struct FindUEdgeSelector< |
---|
| 480 | Graph, |
---|
| 481 | typename enable_if<typename Graph::FindEdgeTag, void>::type> |
---|
| 482 | { |
---|
| 483 | typedef typename Graph::Node Node; |
---|
| 484 | typedef typename Graph::UEdge UEdge; |
---|
| 485 | static UEdge find(const Graph &g, Node u, Node v, UEdge prev) { |
---|
| 486 | return g.findUEdge(u, v, prev); |
---|
| 487 | } |
---|
| 488 | }; |
---|
[1704] | 489 | } |
---|
| 490 | |
---|
[1909] | 491 | /// \brief Finds an uedge between two nodes of a graph. |
---|
[1704] | 492 | /// |
---|
[1909] | 493 | /// Finds an uedge from node \c u to node \c v in graph \c g. |
---|
[2020] | 494 | /// If the node \c u and node \c v is equal then each loop edge |
---|
| 495 | /// will be enumerated. |
---|
[1704] | 496 | /// |
---|
| 497 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 498 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 499 | /// the next edge from \c u to \c v after \c prev. |
---|
| 500 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 501 | /// |
---|
| 502 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
[1946] | 503 | ///\code |
---|
[1909] | 504 | /// for(UEdge e = findUEdge(g,u,v); e != INVALID; |
---|
| 505 | /// e = findUEdge(g,u,v,e)) { |
---|
[1704] | 506 | /// ... |
---|
| 507 | /// } |
---|
[1946] | 508 | ///\endcode |
---|
[2155] | 509 | /// |
---|
| 510 | ///\sa ConEdgeIt |
---|
| 511 | |
---|
[1704] | 512 | template <typename Graph> |
---|
[2286] | 513 | inline typename Graph::UEdge |
---|
| 514 | findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
---|
| 515 | typename Graph::UEdge p = INVALID) { |
---|
[2031] | 516 | return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p); |
---|
[1704] | 517 | } |
---|
| 518 | |
---|
[1909] | 519 | /// \brief Iterator for iterating on uedges connected the same nodes. |
---|
[1704] | 520 | /// |
---|
[1909] | 521 | /// Iterator for iterating on uedges connected the same nodes. It is |
---|
| 522 | /// higher level interface for the findUEdge() function. You can |
---|
[1704] | 523 | /// use it the following way: |
---|
[1946] | 524 | ///\code |
---|
[1909] | 525 | /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
---|
[1704] | 526 | /// ... |
---|
| 527 | /// } |
---|
[1946] | 528 | ///\endcode |
---|
[1704] | 529 | /// |
---|
[2155] | 530 | ///\sa findUEdge() |
---|
| 531 | /// |
---|
[1704] | 532 | /// \author Balazs Dezso |
---|
| 533 | template <typename _Graph> |
---|
[1909] | 534 | class ConUEdgeIt : public _Graph::UEdge { |
---|
[1704] | 535 | public: |
---|
| 536 | |
---|
| 537 | typedef _Graph Graph; |
---|
[1909] | 538 | typedef typename Graph::UEdge Parent; |
---|
[1704] | 539 | |
---|
[1909] | 540 | typedef typename Graph::UEdge UEdge; |
---|
[1704] | 541 | typedef typename Graph::Node Node; |
---|
| 542 | |
---|
| 543 | /// \brief Constructor. |
---|
| 544 | /// |
---|
[1909] | 545 | /// Construct a new ConUEdgeIt iterating on the edges which |
---|
[1704] | 546 | /// connects the \c u and \c v node. |
---|
[1909] | 547 | ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) { |
---|
| 548 | Parent::operator=(findUEdge(graph, u, v)); |
---|
[1704] | 549 | } |
---|
| 550 | |
---|
| 551 | /// \brief Constructor. |
---|
| 552 | /// |
---|
[1909] | 553 | /// Construct a new ConUEdgeIt which continues the iterating from |
---|
[1704] | 554 | /// the \c e edge. |
---|
[1909] | 555 | ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {} |
---|
[1704] | 556 | |
---|
| 557 | /// \brief Increment operator. |
---|
| 558 | /// |
---|
| 559 | /// It increments the iterator and gives back the next edge. |
---|
[1909] | 560 | ConUEdgeIt& operator++() { |
---|
| 561 | Parent::operator=(findUEdge(graph, graph.source(*this), |
---|
[1829] | 562 | graph.target(*this), *this)); |
---|
[1704] | 563 | return *this; |
---|
| 564 | } |
---|
| 565 | private: |
---|
| 566 | const Graph& graph; |
---|
| 567 | }; |
---|
| 568 | |
---|
[1540] | 569 | /// \brief Copy a map. |
---|
[964] | 570 | /// |
---|
[2485] | 571 | /// This function copies the \c from map to the \c to map. It uses the |
---|
[1540] | 572 | /// given iterator to iterate on the data structure and it uses the \c ref |
---|
[2485] | 573 | /// mapping to convert the from's keys to the to's keys. |
---|
| 574 | template <typename To, typename From, |
---|
[1531] | 575 | typename ItemIt, typename Ref> |
---|
[2485] | 576 | void copyMap(To& to, const From& from, |
---|
[1531] | 577 | ItemIt it, const Ref& ref) { |
---|
| 578 | for (; it != INVALID; ++it) { |
---|
[2485] | 579 | to[ref[it]] = from[it]; |
---|
[946] | 580 | } |
---|
| 581 | } |
---|
| 582 | |
---|
[2485] | 583 | /// \brief Copy the from map to the to map. |
---|
[1531] | 584 | /// |
---|
[2485] | 585 | /// Copy the \c from map to the \c to map. It uses the given iterator |
---|
[1531] | 586 | /// to iterate on the data structure. |
---|
[2485] | 587 | template <typename To, typename From, typename ItemIt> |
---|
| 588 | void copyMap(To& to, const From& from, ItemIt it) { |
---|
[1531] | 589 | for (; it != INVALID; ++it) { |
---|
[2485] | 590 | to[it] = from[it]; |
---|
[946] | 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
[2286] | 594 | namespace _graph_utils_bits { |
---|
| 595 | |
---|
| 596 | template <typename Graph, typename Item, typename RefMap> |
---|
| 597 | class MapCopyBase { |
---|
| 598 | public: |
---|
[2485] | 599 | virtual void copy(const Graph& from, const RefMap& refMap) = 0; |
---|
[2286] | 600 | |
---|
| 601 | virtual ~MapCopyBase() {} |
---|
| 602 | }; |
---|
| 603 | |
---|
| 604 | template <typename Graph, typename Item, typename RefMap, |
---|
[2485] | 605 | typename ToMap, typename FromMap> |
---|
[2286] | 606 | class MapCopy : public MapCopyBase<Graph, Item, RefMap> { |
---|
| 607 | public: |
---|
| 608 | |
---|
[2485] | 609 | MapCopy(ToMap& tmap, const FromMap& map) |
---|
[2286] | 610 | : _tmap(tmap), _map(map) {} |
---|
| 611 | |
---|
| 612 | virtual void copy(const Graph& graph, const RefMap& refMap) { |
---|
| 613 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
| 614 | for (ItemIt it(graph); it != INVALID; ++it) { |
---|
| 615 | _tmap.set(refMap[it], _map[it]); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | private: |
---|
[2485] | 620 | ToMap& _tmap; |
---|
| 621 | const FromMap& _map; |
---|
[2286] | 622 | }; |
---|
| 623 | |
---|
[2290] | 624 | template <typename Graph, typename Item, typename RefMap, typename It> |
---|
| 625 | class ItemCopy : public MapCopyBase<Graph, Item, RefMap> { |
---|
| 626 | public: |
---|
| 627 | |
---|
| 628 | ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
---|
| 629 | |
---|
| 630 | virtual void copy(const Graph&, const RefMap& refMap) { |
---|
| 631 | _it = refMap[_item]; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | private: |
---|
| 635 | It& _it; |
---|
| 636 | Item _item; |
---|
| 637 | }; |
---|
| 638 | |
---|
[2286] | 639 | template <typename Graph, typename Item, typename RefMap, typename Ref> |
---|
| 640 | class RefCopy : public MapCopyBase<Graph, Item, RefMap> { |
---|
| 641 | public: |
---|
| 642 | |
---|
| 643 | RefCopy(Ref& map) : _map(map) {} |
---|
| 644 | |
---|
| 645 | virtual void copy(const Graph& graph, const RefMap& refMap) { |
---|
| 646 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
| 647 | for (ItemIt it(graph); it != INVALID; ++it) { |
---|
| 648 | _map.set(it, refMap[it]); |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | private: |
---|
| 653 | Ref& _map; |
---|
| 654 | }; |
---|
| 655 | |
---|
| 656 | template <typename Graph, typename Item, typename RefMap, |
---|
| 657 | typename CrossRef> |
---|
| 658 | class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> { |
---|
| 659 | public: |
---|
| 660 | |
---|
| 661 | CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
---|
| 662 | |
---|
| 663 | virtual void copy(const Graph& graph, const RefMap& refMap) { |
---|
| 664 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
| 665 | for (ItemIt it(graph); it != INVALID; ++it) { |
---|
| 666 | _cmap.set(refMap[it], it); |
---|
| 667 | } |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | private: |
---|
| 671 | CrossRef& _cmap; |
---|
| 672 | }; |
---|
| 673 | |
---|
[2290] | 674 | template <typename Graph, typename Enable = void> |
---|
| 675 | struct GraphCopySelector { |
---|
[2485] | 676 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
---|
| 677 | static void copy(Graph &to, const From& from, |
---|
[2290] | 678 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
---|
[2485] | 679 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
---|
| 680 | nodeRefMap[it] = to.addNode(); |
---|
[2290] | 681 | } |
---|
[2485] | 682 | for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
---|
| 683 | edgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)], |
---|
| 684 | nodeRefMap[from.target(it)]); |
---|
[2290] | 685 | } |
---|
| 686 | } |
---|
| 687 | }; |
---|
| 688 | |
---|
| 689 | template <typename Graph> |
---|
| 690 | struct GraphCopySelector< |
---|
| 691 | Graph, |
---|
[2329] | 692 | typename enable_if<typename Graph::BuildTag, void>::type> |
---|
[2290] | 693 | { |
---|
[2485] | 694 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
---|
| 695 | static void copy(Graph &to, const From& from, |
---|
[2290] | 696 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
---|
[2485] | 697 | to.build(from, nodeRefMap, edgeRefMap); |
---|
[2290] | 698 | } |
---|
| 699 | }; |
---|
| 700 | |
---|
| 701 | template <typename UGraph, typename Enable = void> |
---|
| 702 | struct UGraphCopySelector { |
---|
[2485] | 703 | template <typename From, typename NodeRefMap, typename UEdgeRefMap> |
---|
| 704 | static void copy(UGraph &to, const From& from, |
---|
[2290] | 705 | NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) { |
---|
[2485] | 706 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
---|
| 707 | nodeRefMap[it] = to.addNode(); |
---|
[2290] | 708 | } |
---|
[2485] | 709 | for (typename From::UEdgeIt it(from); it != INVALID; ++it) { |
---|
| 710 | uEdgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)], |
---|
| 711 | nodeRefMap[from.target(it)]); |
---|
[2290] | 712 | } |
---|
| 713 | } |
---|
| 714 | }; |
---|
| 715 | |
---|
| 716 | template <typename UGraph> |
---|
| 717 | struct UGraphCopySelector< |
---|
| 718 | UGraph, |
---|
[2329] | 719 | typename enable_if<typename UGraph::BuildTag, void>::type> |
---|
[2290] | 720 | { |
---|
[2485] | 721 | template <typename From, typename NodeRefMap, typename UEdgeRefMap> |
---|
| 722 | static void copy(UGraph &to, const From& from, |
---|
[2290] | 723 | NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) { |
---|
[2485] | 724 | to.build(from, nodeRefMap, uEdgeRefMap); |
---|
[2290] | 725 | } |
---|
| 726 | }; |
---|
| 727 | |
---|
| 728 | template <typename BpUGraph, typename Enable = void> |
---|
| 729 | struct BpUGraphCopySelector { |
---|
[2485] | 730 | template <typename From, typename ANodeRefMap, |
---|
[2290] | 731 | typename BNodeRefMap, typename UEdgeRefMap> |
---|
[2485] | 732 | static void copy(BpUGraph &to, const From& from, |
---|
[2290] | 733 | ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap, |
---|
| 734 | UEdgeRefMap& uEdgeRefMap) { |
---|
[2485] | 735 | for (typename From::ANodeIt it(from); it != INVALID; ++it) { |
---|
| 736 | aNodeRefMap[it] = to.addANode(); |
---|
[2290] | 737 | } |
---|
[2485] | 738 | for (typename From::BNodeIt it(from); it != INVALID; ++it) { |
---|
| 739 | bNodeRefMap[it] = to.addBNode(); |
---|
[2290] | 740 | } |
---|
[2485] | 741 | for (typename From::UEdgeIt it(from); it != INVALID; ++it) { |
---|
| 742 | uEdgeRefMap[it] = to.addEdge(aNodeRefMap[from.aNode(it)], |
---|
| 743 | bNodeRefMap[from.bNode(it)]); |
---|
[2290] | 744 | } |
---|
| 745 | } |
---|
| 746 | }; |
---|
| 747 | |
---|
| 748 | template <typename BpUGraph> |
---|
| 749 | struct BpUGraphCopySelector< |
---|
| 750 | BpUGraph, |
---|
[2329] | 751 | typename enable_if<typename BpUGraph::BuildTag, void>::type> |
---|
[2290] | 752 | { |
---|
[2485] | 753 | template <typename From, typename ANodeRefMap, |
---|
[2290] | 754 | typename BNodeRefMap, typename UEdgeRefMap> |
---|
[2485] | 755 | static void copy(BpUGraph &to, const From& from, |
---|
[2290] | 756 | ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap, |
---|
| 757 | UEdgeRefMap& uEdgeRefMap) { |
---|
[2485] | 758 | to.build(from, aNodeRefMap, bNodeRefMap, uEdgeRefMap); |
---|
[2290] | 759 | } |
---|
| 760 | }; |
---|
| 761 | |
---|
| 762 | |
---|
[2286] | 763 | } |
---|
| 764 | |
---|
[1540] | 765 | /// \brief Class to copy a graph. |
---|
[1531] | 766 | /// |
---|
[2006] | 767 | /// Class to copy a graph to another graph (duplicate a graph). The |
---|
[1540] | 768 | /// simplest way of using it is through the \c copyGraph() function. |
---|
[2485] | 769 | template <typename To, typename From> |
---|
[1267] | 770 | class GraphCopy { |
---|
[2286] | 771 | private: |
---|
| 772 | |
---|
[2485] | 773 | typedef typename From::Node Node; |
---|
| 774 | typedef typename From::NodeIt NodeIt; |
---|
| 775 | typedef typename From::Edge Edge; |
---|
| 776 | typedef typename From::EdgeIt EdgeIt; |
---|
[946] | 777 | |
---|
[2485] | 778 | typedef typename To::Node TNode; |
---|
| 779 | typedef typename To::Edge TEdge; |
---|
[2286] | 780 | |
---|
[2485] | 781 | typedef typename From::template NodeMap<TNode> NodeRefMap; |
---|
| 782 | typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
---|
[2286] | 783 | |
---|
| 784 | |
---|
| 785 | public: |
---|
| 786 | |
---|
[946] | 787 | |
---|
[1531] | 788 | /// \brief Constructor for the GraphCopy. |
---|
| 789 | /// |
---|
[2485] | 790 | /// It copies the content of the \c _from graph into the |
---|
| 791 | /// \c _to graph. |
---|
| 792 | GraphCopy(To& _to, const From& _from) |
---|
| 793 | : from(_from), to(_to) {} |
---|
[2286] | 794 | |
---|
| 795 | /// \brief Destructor of the GraphCopy |
---|
| 796 | /// |
---|
| 797 | /// Destructor of the GraphCopy |
---|
| 798 | ~GraphCopy() { |
---|
[2386] | 799 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2286] | 800 | delete nodeMapCopies[i]; |
---|
[1531] | 801 | } |
---|
[2386] | 802 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2286] | 803 | delete edgeMapCopies[i]; |
---|
[1531] | 804 | } |
---|
[2286] | 805 | |
---|
[1267] | 806 | } |
---|
[946] | 807 | |
---|
[1531] | 808 | /// \brief Copies the node references into the given map. |
---|
| 809 | /// |
---|
| 810 | /// Copies the node references into the given map. |
---|
| 811 | template <typename NodeRef> |
---|
[2286] | 812 | GraphCopy& nodeRef(NodeRef& map) { |
---|
[2485] | 813 | nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, |
---|
[2286] | 814 | NodeRefMap, NodeRef>(map)); |
---|
[1531] | 815 | return *this; |
---|
[1267] | 816 | } |
---|
[1531] | 817 | |
---|
[2290] | 818 | /// \brief Copies the node cross references into the given map. |
---|
[1531] | 819 | /// |
---|
[2290] | 820 | /// Copies the node cross references (reverse references) into |
---|
| 821 | /// the given map. |
---|
[2286] | 822 | template <typename NodeCrossRef> |
---|
| 823 | GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
[2485] | 824 | nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
---|
[2286] | 825 | NodeRefMap, NodeCrossRef>(map)); |
---|
[1531] | 826 | return *this; |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | /// \brief Make copy of the given map. |
---|
| 830 | /// |
---|
| 831 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 832 | /// The new map's key type is the to graph's node type, |
---|
| 833 | /// and the copied map's key type is the from graph's node |
---|
[1531] | 834 | /// type. |
---|
[2485] | 835 | template <typename ToMap, typename FromMap> |
---|
| 836 | GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
---|
| 837 | nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, |
---|
| 838 | NodeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2286] | 839 | return *this; |
---|
| 840 | } |
---|
| 841 | |
---|
[2290] | 842 | /// \brief Make a copy of the given node. |
---|
| 843 | /// |
---|
| 844 | /// Make a copy of the given node. |
---|
[2386] | 845 | GraphCopy& node(TNode& tnode, const Node& snode) { |
---|
[2485] | 846 | nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
---|
[2386] | 847 | NodeRefMap, TNode>(tnode, snode)); |
---|
[2290] | 848 | return *this; |
---|
| 849 | } |
---|
| 850 | |
---|
[2286] | 851 | /// \brief Copies the edge references into the given map. |
---|
| 852 | /// |
---|
| 853 | /// Copies the edge references into the given map. |
---|
| 854 | template <typename EdgeRef> |
---|
| 855 | GraphCopy& edgeRef(EdgeRef& map) { |
---|
[2485] | 856 | edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
---|
[2286] | 857 | EdgeRefMap, EdgeRef>(map)); |
---|
| 858 | return *this; |
---|
| 859 | } |
---|
| 860 | |
---|
[2290] | 861 | /// \brief Copies the edge cross references into the given map. |
---|
[2286] | 862 | /// |
---|
[2290] | 863 | /// Copies the edge cross references (reverse references) into |
---|
| 864 | /// the given map. |
---|
[2286] | 865 | template <typename EdgeCrossRef> |
---|
| 866 | GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
---|
[2485] | 867 | edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge, |
---|
[2286] | 868 | EdgeRefMap, EdgeCrossRef>(map)); |
---|
[1531] | 869 | return *this; |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | /// \brief Make copy of the given map. |
---|
| 873 | /// |
---|
| 874 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 875 | /// The new map's key type is the to graph's edge type, |
---|
| 876 | /// and the copied map's key type is the from graph's edge |
---|
[1531] | 877 | /// type. |
---|
[2485] | 878 | template <typename ToMap, typename FromMap> |
---|
| 879 | GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
---|
| 880 | edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
---|
| 881 | EdgeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[1531] | 882 | return *this; |
---|
| 883 | } |
---|
| 884 | |
---|
[2290] | 885 | /// \brief Make a copy of the given edge. |
---|
| 886 | /// |
---|
| 887 | /// Make a copy of the given edge. |
---|
[2386] | 888 | GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
---|
[2485] | 889 | edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
---|
[2386] | 890 | EdgeRefMap, TEdge>(tedge, sedge)); |
---|
[2290] | 891 | return *this; |
---|
| 892 | } |
---|
| 893 | |
---|
[2286] | 894 | /// \brief Executes the copies. |
---|
[1531] | 895 | /// |
---|
[2286] | 896 | /// Executes the copies. |
---|
| 897 | void run() { |
---|
[2485] | 898 | NodeRefMap nodeRefMap(from); |
---|
| 899 | EdgeRefMap edgeRefMap(from); |
---|
| 900 | _graph_utils_bits::GraphCopySelector<To>:: |
---|
| 901 | copy(to, from, nodeRefMap, edgeRefMap); |
---|
[2386] | 902 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2485] | 903 | nodeMapCopies[i]->copy(from, nodeRefMap); |
---|
[2286] | 904 | } |
---|
[2386] | 905 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2485] | 906 | edgeMapCopies[i]->copy(from, edgeRefMap); |
---|
[2290] | 907 | } |
---|
[1531] | 908 | } |
---|
| 909 | |
---|
[2290] | 910 | protected: |
---|
| 911 | |
---|
| 912 | |
---|
[2485] | 913 | const From& from; |
---|
| 914 | To& to; |
---|
[1531] | 915 | |
---|
[2485] | 916 | std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
[2286] | 917 | nodeMapCopies; |
---|
| 918 | |
---|
[2485] | 919 | std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
---|
[2286] | 920 | edgeMapCopies; |
---|
| 921 | |
---|
[1267] | 922 | }; |
---|
[946] | 923 | |
---|
[2006] | 924 | /// \brief Copy a graph to another graph. |
---|
[1531] | 925 | /// |
---|
[2006] | 926 | /// Copy a graph to another graph. |
---|
[1531] | 927 | /// The usage of the function: |
---|
| 928 | /// |
---|
[1946] | 929 | ///\code |
---|
[2286] | 930 | /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run(); |
---|
[1946] | 931 | ///\endcode |
---|
[1531] | 932 | /// |
---|
| 933 | /// After the copy the \c nr map will contain the mapping from the |
---|
[2534] | 934 | /// nodes of the \c from graph to the nodes of the \c to graph and |
---|
| 935 | /// \c ecr will contain the mapping from the edges of the \c to graph |
---|
| 936 | /// to the edges of the \c from graph. |
---|
[2290] | 937 | /// |
---|
| 938 | /// \see GraphCopy |
---|
[2485] | 939 | template <typename To, typename From> |
---|
| 940 | GraphCopy<To, From> copyGraph(To& to, const From& from) { |
---|
| 941 | return GraphCopy<To, From>(to, from); |
---|
[1531] | 942 | } |
---|
[946] | 943 | |
---|
[1720] | 944 | /// \brief Class to copy an undirected graph. |
---|
| 945 | /// |
---|
[2006] | 946 | /// Class to copy an undirected graph to another graph (duplicate a graph). |
---|
[1909] | 947 | /// The simplest way of using it is through the \c copyUGraph() function. |
---|
[2485] | 948 | template <typename To, typename From> |
---|
[1909] | 949 | class UGraphCopy { |
---|
[2286] | 950 | private: |
---|
| 951 | |
---|
[2485] | 952 | typedef typename From::Node Node; |
---|
| 953 | typedef typename From::NodeIt NodeIt; |
---|
| 954 | typedef typename From::Edge Edge; |
---|
| 955 | typedef typename From::EdgeIt EdgeIt; |
---|
| 956 | typedef typename From::UEdge UEdge; |
---|
| 957 | typedef typename From::UEdgeIt UEdgeIt; |
---|
[1720] | 958 | |
---|
[2485] | 959 | typedef typename To::Node TNode; |
---|
| 960 | typedef typename To::Edge TEdge; |
---|
| 961 | typedef typename To::UEdge TUEdge; |
---|
[1720] | 962 | |
---|
[2485] | 963 | typedef typename From::template NodeMap<TNode> NodeRefMap; |
---|
| 964 | typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap; |
---|
[1720] | 965 | |
---|
| 966 | struct EdgeRefMap { |
---|
[2485] | 967 | EdgeRefMap(const To& _to, const From& _from, |
---|
[2286] | 968 | const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) |
---|
[2485] | 969 | : to(_to), from(_from), |
---|
[2286] | 970 | uedge_ref(_uedge_ref), node_ref(_node_ref) {} |
---|
| 971 | |
---|
[2485] | 972 | typedef typename From::Edge Key; |
---|
| 973 | typedef typename To::Edge Value; |
---|
[1720] | 974 | |
---|
[2286] | 975 | Value operator[](const Key& key) const { |
---|
[2386] | 976 | bool forward = |
---|
[2485] | 977 | (from.direction(key) == |
---|
| 978 | (node_ref[from.source(static_cast<const UEdge&>(key))] == |
---|
| 979 | to.source(uedge_ref[static_cast<const UEdge&>(key)]))); |
---|
| 980 | return to.direct(uedge_ref[key], forward); |
---|
[1720] | 981 | } |
---|
| 982 | |
---|
[2485] | 983 | const To& to; |
---|
| 984 | const From& from; |
---|
[2286] | 985 | const UEdgeRefMap& uedge_ref; |
---|
| 986 | const NodeRefMap& node_ref; |
---|
[1720] | 987 | }; |
---|
[2286] | 988 | |
---|
[1720] | 989 | |
---|
[2286] | 990 | public: |
---|
[1720] | 991 | |
---|
[2286] | 992 | |
---|
| 993 | /// \brief Constructor for the GraphCopy. |
---|
[1720] | 994 | /// |
---|
[2485] | 995 | /// It copies the content of the \c _from graph into the |
---|
| 996 | /// \c _to graph. |
---|
| 997 | UGraphCopy(To& _to, const From& _from) |
---|
| 998 | : from(_from), to(_to) {} |
---|
[2286] | 999 | |
---|
| 1000 | /// \brief Destructor of the GraphCopy |
---|
| 1001 | /// |
---|
| 1002 | /// Destructor of the GraphCopy |
---|
| 1003 | ~UGraphCopy() { |
---|
[2386] | 1004 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2286] | 1005 | delete nodeMapCopies[i]; |
---|
[1720] | 1006 | } |
---|
[2386] | 1007 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2286] | 1008 | delete edgeMapCopies[i]; |
---|
[1720] | 1009 | } |
---|
[2386] | 1010 | for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) { |
---|
[2286] | 1011 | delete uEdgeMapCopies[i]; |
---|
| 1012 | } |
---|
| 1013 | |
---|
[1720] | 1014 | } |
---|
| 1015 | |
---|
| 1016 | /// \brief Copies the node references into the given map. |
---|
| 1017 | /// |
---|
| 1018 | /// Copies the node references into the given map. |
---|
| 1019 | template <typename NodeRef> |
---|
[2286] | 1020 | UGraphCopy& nodeRef(NodeRef& map) { |
---|
[2485] | 1021 | nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, |
---|
[2286] | 1022 | NodeRefMap, NodeRef>(map)); |
---|
[1720] | 1023 | return *this; |
---|
| 1024 | } |
---|
| 1025 | |
---|
[2290] | 1026 | /// \brief Copies the node cross references into the given map. |
---|
[1720] | 1027 | /// |
---|
[2290] | 1028 | /// Copies the node cross references (reverse references) into |
---|
| 1029 | /// the given map. |
---|
[2286] | 1030 | template <typename NodeCrossRef> |
---|
| 1031 | UGraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
[2485] | 1032 | nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
---|
[2286] | 1033 | NodeRefMap, NodeCrossRef>(map)); |
---|
[1720] | 1034 | return *this; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | /// \brief Make copy of the given map. |
---|
| 1038 | /// |
---|
| 1039 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1040 | /// The new map's key type is the to graph's node type, |
---|
| 1041 | /// and the copied map's key type is the from graph's node |
---|
[1720] | 1042 | /// type. |
---|
[2485] | 1043 | template <typename ToMap, typename FromMap> |
---|
| 1044 | UGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1045 | nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, |
---|
| 1046 | NodeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2286] | 1047 | return *this; |
---|
| 1048 | } |
---|
| 1049 | |
---|
[2290] | 1050 | /// \brief Make a copy of the given node. |
---|
| 1051 | /// |
---|
| 1052 | /// Make a copy of the given node. |
---|
[2386] | 1053 | UGraphCopy& node(TNode& tnode, const Node& snode) { |
---|
[2485] | 1054 | nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
---|
[2386] | 1055 | NodeRefMap, TNode>(tnode, snode)); |
---|
[2290] | 1056 | return *this; |
---|
| 1057 | } |
---|
| 1058 | |
---|
[2286] | 1059 | /// \brief Copies the edge references into the given map. |
---|
| 1060 | /// |
---|
| 1061 | /// Copies the edge references into the given map. |
---|
| 1062 | template <typename EdgeRef> |
---|
| 1063 | UGraphCopy& edgeRef(EdgeRef& map) { |
---|
[2485] | 1064 | edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
---|
[2286] | 1065 | EdgeRefMap, EdgeRef>(map)); |
---|
| 1066 | return *this; |
---|
| 1067 | } |
---|
| 1068 | |
---|
[2290] | 1069 | /// \brief Copies the edge cross references into the given map. |
---|
[2286] | 1070 | /// |
---|
[2290] | 1071 | /// Copies the edge cross references (reverse references) into |
---|
| 1072 | /// the given map. |
---|
[2286] | 1073 | template <typename EdgeCrossRef> |
---|
| 1074 | UGraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
---|
[2485] | 1075 | edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge, |
---|
[2286] | 1076 | EdgeRefMap, EdgeCrossRef>(map)); |
---|
[1720] | 1077 | return *this; |
---|
| 1078 | } |
---|
| 1079 | |
---|
| 1080 | /// \brief Make copy of the given map. |
---|
| 1081 | /// |
---|
| 1082 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1083 | /// The new map's key type is the to graph's edge type, |
---|
| 1084 | /// and the copied map's key type is the from graph's edge |
---|
[1720] | 1085 | /// type. |
---|
[2485] | 1086 | template <typename ToMap, typename FromMap> |
---|
| 1087 | UGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1088 | edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
---|
| 1089 | EdgeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2286] | 1090 | return *this; |
---|
| 1091 | } |
---|
| 1092 | |
---|
[2290] | 1093 | /// \brief Make a copy of the given edge. |
---|
[2286] | 1094 | /// |
---|
[2290] | 1095 | /// Make a copy of the given edge. |
---|
[2386] | 1096 | UGraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
---|
[2485] | 1097 | edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
---|
[2386] | 1098 | EdgeRefMap, TEdge>(tedge, sedge)); |
---|
[2290] | 1099 | return *this; |
---|
| 1100 | } |
---|
| 1101 | |
---|
| 1102 | /// \brief Copies the undirected edge references into the given map. |
---|
| 1103 | /// |
---|
| 1104 | /// Copies the undirected edge references into the given map. |
---|
[2286] | 1105 | template <typename UEdgeRef> |
---|
| 1106 | UGraphCopy& uEdgeRef(UEdgeRef& map) { |
---|
[2485] | 1107 | uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge, |
---|
[2286] | 1108 | UEdgeRefMap, UEdgeRef>(map)); |
---|
| 1109 | return *this; |
---|
| 1110 | } |
---|
| 1111 | |
---|
[2290] | 1112 | /// \brief Copies the undirected edge cross references into the given map. |
---|
[2286] | 1113 | /// |
---|
[2290] | 1114 | /// Copies the undirected edge cross references (reverse |
---|
| 1115 | /// references) into the given map. |
---|
[2286] | 1116 | template <typename UEdgeCrossRef> |
---|
| 1117 | UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) { |
---|
[2485] | 1118 | uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, |
---|
[2286] | 1119 | UEdge, UEdgeRefMap, UEdgeCrossRef>(map)); |
---|
[1720] | 1120 | return *this; |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | /// \brief Make copy of the given map. |
---|
| 1124 | /// |
---|
| 1125 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1126 | /// The new map's key type is the to graph's undirected edge type, |
---|
| 1127 | /// and the copied map's key type is the from graph's undirected edge |
---|
[1720] | 1128 | /// type. |
---|
[2485] | 1129 | template <typename ToMap, typename FromMap> |
---|
| 1130 | UGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1131 | uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge, |
---|
| 1132 | UEdgeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[1720] | 1133 | return *this; |
---|
| 1134 | } |
---|
| 1135 | |
---|
[2290] | 1136 | /// \brief Make a copy of the given undirected edge. |
---|
| 1137 | /// |
---|
| 1138 | /// Make a copy of the given undirected edge. |
---|
[2386] | 1139 | UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) { |
---|
[2485] | 1140 | uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge, |
---|
[2386] | 1141 | UEdgeRefMap, TUEdge>(tuedge, suedge)); |
---|
[2290] | 1142 | return *this; |
---|
| 1143 | } |
---|
| 1144 | |
---|
[2286] | 1145 | /// \brief Executes the copies. |
---|
[1720] | 1146 | /// |
---|
[2286] | 1147 | /// Executes the copies. |
---|
| 1148 | void run() { |
---|
[2485] | 1149 | NodeRefMap nodeRefMap(from); |
---|
| 1150 | UEdgeRefMap uEdgeRefMap(from); |
---|
| 1151 | EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap); |
---|
| 1152 | _graph_utils_bits::UGraphCopySelector<To>:: |
---|
| 1153 | copy(to, from, nodeRefMap, uEdgeRefMap); |
---|
[2386] | 1154 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2485] | 1155 | nodeMapCopies[i]->copy(from, nodeRefMap); |
---|
[2286] | 1156 | } |
---|
[2386] | 1157 | for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) { |
---|
[2485] | 1158 | uEdgeMapCopies[i]->copy(from, uEdgeRefMap); |
---|
[2286] | 1159 | } |
---|
[2386] | 1160 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2485] | 1161 | edgeMapCopies[i]->copy(from, edgeRefMap); |
---|
[2286] | 1162 | } |
---|
[1720] | 1163 | } |
---|
| 1164 | |
---|
| 1165 | private: |
---|
[1192] | 1166 | |
---|
[2485] | 1167 | const From& from; |
---|
| 1168 | To& to; |
---|
[947] | 1169 | |
---|
[2485] | 1170 | std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
[2286] | 1171 | nodeMapCopies; |
---|
| 1172 | |
---|
[2485] | 1173 | std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
---|
[2286] | 1174 | edgeMapCopies; |
---|
| 1175 | |
---|
[2485] | 1176 | std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* > |
---|
[2286] | 1177 | uEdgeMapCopies; |
---|
| 1178 | |
---|
[1192] | 1179 | }; |
---|
| 1180 | |
---|
[2290] | 1181 | /// \brief Copy an undirected graph to another graph. |
---|
[1720] | 1182 | /// |
---|
[2290] | 1183 | /// Copy an undirected graph to another graph. |
---|
[1720] | 1184 | /// The usage of the function: |
---|
| 1185 | /// |
---|
[1946] | 1186 | ///\code |
---|
[2286] | 1187 | /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run(); |
---|
[1946] | 1188 | ///\endcode |
---|
[1720] | 1189 | /// |
---|
| 1190 | /// After the copy the \c nr map will contain the mapping from the |
---|
[2534] | 1191 | /// nodes of the \c from graph to the nodes of the \c to graph and |
---|
| 1192 | /// \c ecr will contain the mapping from the edges of the \c to graph |
---|
| 1193 | /// to the edges of the \c from graph. |
---|
[2290] | 1194 | /// |
---|
| 1195 | /// \see UGraphCopy |
---|
[2485] | 1196 | template <typename To, typename From> |
---|
| 1197 | UGraphCopy<To, From> |
---|
| 1198 | copyUGraph(To& to, const From& from) { |
---|
| 1199 | return UGraphCopy<To, From>(to, from); |
---|
[1720] | 1200 | } |
---|
[1192] | 1201 | |
---|
[2290] | 1202 | /// \brief Class to copy a bipartite undirected graph. |
---|
| 1203 | /// |
---|
| 1204 | /// Class to copy a bipartite undirected graph to another graph |
---|
| 1205 | /// (duplicate a graph). The simplest way of using it is through |
---|
| 1206 | /// the \c copyBpUGraph() function. |
---|
[2485] | 1207 | template <typename To, typename From> |
---|
[2290] | 1208 | class BpUGraphCopy { |
---|
| 1209 | private: |
---|
| 1210 | |
---|
[2485] | 1211 | typedef typename From::Node Node; |
---|
| 1212 | typedef typename From::ANode ANode; |
---|
| 1213 | typedef typename From::BNode BNode; |
---|
| 1214 | typedef typename From::NodeIt NodeIt; |
---|
| 1215 | typedef typename From::Edge Edge; |
---|
| 1216 | typedef typename From::EdgeIt EdgeIt; |
---|
| 1217 | typedef typename From::UEdge UEdge; |
---|
| 1218 | typedef typename From::UEdgeIt UEdgeIt; |
---|
[2290] | 1219 | |
---|
[2485] | 1220 | typedef typename To::Node TNode; |
---|
| 1221 | typedef typename To::Edge TEdge; |
---|
| 1222 | typedef typename To::UEdge TUEdge; |
---|
[2290] | 1223 | |
---|
[2485] | 1224 | typedef typename From::template ANodeMap<TNode> ANodeRefMap; |
---|
| 1225 | typedef typename From::template BNodeMap<TNode> BNodeRefMap; |
---|
| 1226 | typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap; |
---|
[2290] | 1227 | |
---|
| 1228 | struct NodeRefMap { |
---|
[2485] | 1229 | NodeRefMap(const From& _from, const ANodeRefMap& _anode_ref, |
---|
[2290] | 1230 | const BNodeRefMap& _bnode_ref) |
---|
[2485] | 1231 | : from(_from), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {} |
---|
[2290] | 1232 | |
---|
[2485] | 1233 | typedef typename From::Node Key; |
---|
| 1234 | typedef typename To::Node Value; |
---|
[2290] | 1235 | |
---|
| 1236 | Value operator[](const Key& key) const { |
---|
[2485] | 1237 | return from.aNode(key) ? anode_ref[key] : bnode_ref[key]; |
---|
[2290] | 1238 | } |
---|
| 1239 | |
---|
[2485] | 1240 | const From& from; |
---|
[2290] | 1241 | const ANodeRefMap& anode_ref; |
---|
| 1242 | const BNodeRefMap& bnode_ref; |
---|
| 1243 | }; |
---|
| 1244 | |
---|
| 1245 | struct EdgeRefMap { |
---|
[2485] | 1246 | EdgeRefMap(const To& _to, const From& _from, |
---|
[2290] | 1247 | const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) |
---|
[2485] | 1248 | : to(_to), from(_from), |
---|
[2290] | 1249 | uedge_ref(_uedge_ref), node_ref(_node_ref) {} |
---|
| 1250 | |
---|
[2485] | 1251 | typedef typename From::Edge Key; |
---|
| 1252 | typedef typename To::Edge Value; |
---|
[2290] | 1253 | |
---|
| 1254 | Value operator[](const Key& key) const { |
---|
[2386] | 1255 | bool forward = |
---|
[2485] | 1256 | (from.direction(key) == |
---|
| 1257 | (node_ref[from.source(static_cast<const UEdge&>(key))] == |
---|
| 1258 | to.source(uedge_ref[static_cast<const UEdge&>(key)]))); |
---|
| 1259 | return to.direct(uedge_ref[key], forward); |
---|
[2290] | 1260 | } |
---|
| 1261 | |
---|
[2485] | 1262 | const To& to; |
---|
| 1263 | const From& from; |
---|
[2290] | 1264 | const UEdgeRefMap& uedge_ref; |
---|
| 1265 | const NodeRefMap& node_ref; |
---|
| 1266 | }; |
---|
| 1267 | |
---|
| 1268 | public: |
---|
| 1269 | |
---|
| 1270 | |
---|
| 1271 | /// \brief Constructor for the GraphCopy. |
---|
| 1272 | /// |
---|
[2485] | 1273 | /// It copies the content of the \c _from graph into the |
---|
| 1274 | /// \c _to graph. |
---|
| 1275 | BpUGraphCopy(To& _to, const From& _from) |
---|
| 1276 | : from(_from), to(_to) {} |
---|
[2290] | 1277 | |
---|
| 1278 | /// \brief Destructor of the GraphCopy |
---|
| 1279 | /// |
---|
| 1280 | /// Destructor of the GraphCopy |
---|
| 1281 | ~BpUGraphCopy() { |
---|
[2386] | 1282 | for (int i = 0; i < int(aNodeMapCopies.size()); ++i) { |
---|
[2290] | 1283 | delete aNodeMapCopies[i]; |
---|
| 1284 | } |
---|
[2386] | 1285 | for (int i = 0; i < int(bNodeMapCopies.size()); ++i) { |
---|
[2290] | 1286 | delete bNodeMapCopies[i]; |
---|
| 1287 | } |
---|
[2386] | 1288 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2290] | 1289 | delete nodeMapCopies[i]; |
---|
| 1290 | } |
---|
[2386] | 1291 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2290] | 1292 | delete edgeMapCopies[i]; |
---|
| 1293 | } |
---|
[2386] | 1294 | for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) { |
---|
[2290] | 1295 | delete uEdgeMapCopies[i]; |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | } |
---|
| 1299 | |
---|
| 1300 | /// \brief Copies the A-node references into the given map. |
---|
| 1301 | /// |
---|
| 1302 | /// Copies the A-node references into the given map. |
---|
| 1303 | template <typename ANodeRef> |
---|
| 1304 | BpUGraphCopy& aNodeRef(ANodeRef& map) { |
---|
[2485] | 1305 | aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, ANode, |
---|
[2290] | 1306 | ANodeRefMap, ANodeRef>(map)); |
---|
| 1307 | return *this; |
---|
| 1308 | } |
---|
| 1309 | |
---|
| 1310 | /// \brief Copies the A-node cross references into the given map. |
---|
| 1311 | /// |
---|
| 1312 | /// Copies the A-node cross references (reverse references) into |
---|
| 1313 | /// the given map. |
---|
| 1314 | template <typename ANodeCrossRef> |
---|
| 1315 | BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) { |
---|
[2485] | 1316 | aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, |
---|
[2290] | 1317 | ANode, ANodeRefMap, ANodeCrossRef>(map)); |
---|
| 1318 | return *this; |
---|
| 1319 | } |
---|
| 1320 | |
---|
| 1321 | /// \brief Make copy of the given A-node map. |
---|
| 1322 | /// |
---|
| 1323 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1324 | /// The new map's key type is the to graph's node type, |
---|
| 1325 | /// and the copied map's key type is the from graph's node |
---|
[2290] | 1326 | /// type. |
---|
[2485] | 1327 | template <typename ToMap, typename FromMap> |
---|
| 1328 | BpUGraphCopy& aNodeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1329 | aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, ANode, |
---|
| 1330 | ANodeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2290] | 1331 | return *this; |
---|
| 1332 | } |
---|
| 1333 | |
---|
| 1334 | /// \brief Copies the B-node references into the given map. |
---|
| 1335 | /// |
---|
| 1336 | /// Copies the B-node references into the given map. |
---|
| 1337 | template <typename BNodeRef> |
---|
| 1338 | BpUGraphCopy& bNodeRef(BNodeRef& map) { |
---|
[2485] | 1339 | bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, BNode, |
---|
[2290] | 1340 | BNodeRefMap, BNodeRef>(map)); |
---|
| 1341 | return *this; |
---|
| 1342 | } |
---|
| 1343 | |
---|
| 1344 | /// \brief Copies the B-node cross references into the given map. |
---|
| 1345 | /// |
---|
| 1346 | /// Copies the B-node cross references (reverse references) into |
---|
| 1347 | /// the given map. |
---|
| 1348 | template <typename BNodeCrossRef> |
---|
| 1349 | BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) { |
---|
[2485] | 1350 | bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, |
---|
[2290] | 1351 | BNode, BNodeRefMap, BNodeCrossRef>(map)); |
---|
| 1352 | return *this; |
---|
| 1353 | } |
---|
| 1354 | |
---|
| 1355 | /// \brief Make copy of the given B-node map. |
---|
| 1356 | /// |
---|
| 1357 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1358 | /// The new map's key type is the to graph's node type, |
---|
| 1359 | /// and the copied map's key type is the from graph's node |
---|
[2290] | 1360 | /// type. |
---|
[2485] | 1361 | template <typename ToMap, typename FromMap> |
---|
| 1362 | BpUGraphCopy& bNodeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1363 | bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, BNode, |
---|
| 1364 | BNodeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2290] | 1365 | return *this; |
---|
| 1366 | } |
---|
| 1367 | /// \brief Copies the node references into the given map. |
---|
| 1368 | /// |
---|
| 1369 | /// Copies the node references into the given map. |
---|
| 1370 | template <typename NodeRef> |
---|
| 1371 | BpUGraphCopy& nodeRef(NodeRef& map) { |
---|
[2485] | 1372 | nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, |
---|
[2290] | 1373 | NodeRefMap, NodeRef>(map)); |
---|
| 1374 | return *this; |
---|
| 1375 | } |
---|
| 1376 | |
---|
| 1377 | /// \brief Copies the node cross references into the given map. |
---|
| 1378 | /// |
---|
| 1379 | /// Copies the node cross references (reverse references) into |
---|
| 1380 | /// the given map. |
---|
| 1381 | template <typename NodeCrossRef> |
---|
| 1382 | BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
[2485] | 1383 | nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
---|
[2290] | 1384 | NodeRefMap, NodeCrossRef>(map)); |
---|
| 1385 | return *this; |
---|
| 1386 | } |
---|
| 1387 | |
---|
| 1388 | /// \brief Make copy of the given map. |
---|
| 1389 | /// |
---|
| 1390 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1391 | /// The new map's key type is the to graph's node type, |
---|
| 1392 | /// and the copied map's key type is the from graph's node |
---|
[2290] | 1393 | /// type. |
---|
[2485] | 1394 | template <typename ToMap, typename FromMap> |
---|
| 1395 | BpUGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1396 | nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, |
---|
| 1397 | NodeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2290] | 1398 | return *this; |
---|
| 1399 | } |
---|
| 1400 | |
---|
| 1401 | /// \brief Make a copy of the given node. |
---|
| 1402 | /// |
---|
| 1403 | /// Make a copy of the given node. |
---|
[2386] | 1404 | BpUGraphCopy& node(TNode& tnode, const Node& snode) { |
---|
[2485] | 1405 | nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
---|
[2386] | 1406 | NodeRefMap, TNode>(tnode, snode)); |
---|
[2290] | 1407 | return *this; |
---|
| 1408 | } |
---|
| 1409 | |
---|
| 1410 | /// \brief Copies the edge references into the given map. |
---|
| 1411 | /// |
---|
| 1412 | /// Copies the edge references into the given map. |
---|
| 1413 | template <typename EdgeRef> |
---|
| 1414 | BpUGraphCopy& edgeRef(EdgeRef& map) { |
---|
[2485] | 1415 | edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
---|
[2290] | 1416 | EdgeRefMap, EdgeRef>(map)); |
---|
| 1417 | return *this; |
---|
| 1418 | } |
---|
| 1419 | |
---|
| 1420 | /// \brief Copies the edge cross references into the given map. |
---|
| 1421 | /// |
---|
| 1422 | /// Copies the edge cross references (reverse references) into |
---|
| 1423 | /// the given map. |
---|
| 1424 | template <typename EdgeCrossRef> |
---|
| 1425 | BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
---|
[2485] | 1426 | edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge, |
---|
[2290] | 1427 | EdgeRefMap, EdgeCrossRef>(map)); |
---|
| 1428 | return *this; |
---|
| 1429 | } |
---|
| 1430 | |
---|
| 1431 | /// \brief Make copy of the given map. |
---|
| 1432 | /// |
---|
| 1433 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1434 | /// The new map's key type is the to graph's edge type, |
---|
| 1435 | /// and the copied map's key type is the from graph's edge |
---|
[2290] | 1436 | /// type. |
---|
[2485] | 1437 | template <typename ToMap, typename FromMap> |
---|
| 1438 | BpUGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1439 | edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
---|
| 1440 | EdgeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2290] | 1441 | return *this; |
---|
| 1442 | } |
---|
| 1443 | |
---|
| 1444 | /// \brief Make a copy of the given edge. |
---|
| 1445 | /// |
---|
| 1446 | /// Make a copy of the given edge. |
---|
[2386] | 1447 | BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
---|
[2485] | 1448 | edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
---|
[2386] | 1449 | EdgeRefMap, TEdge>(tedge, sedge)); |
---|
[2290] | 1450 | return *this; |
---|
| 1451 | } |
---|
| 1452 | |
---|
| 1453 | /// \brief Copies the undirected edge references into the given map. |
---|
| 1454 | /// |
---|
| 1455 | /// Copies the undirected edge references into the given map. |
---|
| 1456 | template <typename UEdgeRef> |
---|
| 1457 | BpUGraphCopy& uEdgeRef(UEdgeRef& map) { |
---|
[2485] | 1458 | uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge, |
---|
[2290] | 1459 | UEdgeRefMap, UEdgeRef>(map)); |
---|
| 1460 | return *this; |
---|
| 1461 | } |
---|
| 1462 | |
---|
| 1463 | /// \brief Copies the undirected edge cross references into the given map. |
---|
| 1464 | /// |
---|
| 1465 | /// Copies the undirected edge cross references (reverse |
---|
| 1466 | /// references) into the given map. |
---|
| 1467 | template <typename UEdgeCrossRef> |
---|
| 1468 | BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) { |
---|
[2485] | 1469 | uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, |
---|
[2290] | 1470 | UEdge, UEdgeRefMap, UEdgeCrossRef>(map)); |
---|
| 1471 | return *this; |
---|
| 1472 | } |
---|
| 1473 | |
---|
| 1474 | /// \brief Make copy of the given map. |
---|
| 1475 | /// |
---|
| 1476 | /// Makes copy of the given map for the newly created graph. |
---|
[2485] | 1477 | /// The new map's key type is the to graph's undirected edge type, |
---|
| 1478 | /// and the copied map's key type is the from graph's undirected edge |
---|
[2290] | 1479 | /// type. |
---|
[2485] | 1480 | template <typename ToMap, typename FromMap> |
---|
| 1481 | BpUGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) { |
---|
| 1482 | uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge, |
---|
| 1483 | UEdgeRefMap, ToMap, FromMap>(tmap, map)); |
---|
[2290] | 1484 | return *this; |
---|
| 1485 | } |
---|
| 1486 | |
---|
| 1487 | /// \brief Make a copy of the given undirected edge. |
---|
| 1488 | /// |
---|
| 1489 | /// Make a copy of the given undirected edge. |
---|
[2386] | 1490 | BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) { |
---|
[2485] | 1491 | uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge, |
---|
[2386] | 1492 | UEdgeRefMap, TUEdge>(tuedge, suedge)); |
---|
[2290] | 1493 | return *this; |
---|
| 1494 | } |
---|
| 1495 | |
---|
| 1496 | /// \brief Executes the copies. |
---|
| 1497 | /// |
---|
| 1498 | /// Executes the copies. |
---|
| 1499 | void run() { |
---|
[2485] | 1500 | ANodeRefMap aNodeRefMap(from); |
---|
| 1501 | BNodeRefMap bNodeRefMap(from); |
---|
| 1502 | NodeRefMap nodeRefMap(from, aNodeRefMap, bNodeRefMap); |
---|
| 1503 | UEdgeRefMap uEdgeRefMap(from); |
---|
| 1504 | EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap); |
---|
| 1505 | _graph_utils_bits::BpUGraphCopySelector<To>:: |
---|
| 1506 | copy(to, from, aNodeRefMap, bNodeRefMap, uEdgeRefMap); |
---|
[2386] | 1507 | for (int i = 0; i < int(aNodeMapCopies.size()); ++i) { |
---|
[2485] | 1508 | aNodeMapCopies[i]->copy(from, aNodeRefMap); |
---|
[2290] | 1509 | } |
---|
[2386] | 1510 | for (int i = 0; i < int(bNodeMapCopies.size()); ++i) { |
---|
[2485] | 1511 | bNodeMapCopies[i]->copy(from, bNodeRefMap); |
---|
[2290] | 1512 | } |
---|
[2386] | 1513 | for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
---|
[2485] | 1514 | nodeMapCopies[i]->copy(from, nodeRefMap); |
---|
[2290] | 1515 | } |
---|
[2386] | 1516 | for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) { |
---|
[2485] | 1517 | uEdgeMapCopies[i]->copy(from, uEdgeRefMap); |
---|
[2290] | 1518 | } |
---|
[2386] | 1519 | for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
---|
[2485] | 1520 | edgeMapCopies[i]->copy(from, edgeRefMap); |
---|
[2290] | 1521 | } |
---|
| 1522 | } |
---|
| 1523 | |
---|
| 1524 | private: |
---|
| 1525 | |
---|
[2485] | 1526 | const From& from; |
---|
| 1527 | To& to; |
---|
[2290] | 1528 | |
---|
[2485] | 1529 | std::vector<_graph_utils_bits::MapCopyBase<From, ANode, ANodeRefMap>* > |
---|
[2290] | 1530 | aNodeMapCopies; |
---|
| 1531 | |
---|
[2485] | 1532 | std::vector<_graph_utils_bits::MapCopyBase<From, BNode, BNodeRefMap>* > |
---|
[2290] | 1533 | bNodeMapCopies; |
---|
| 1534 | |
---|
[2485] | 1535 | std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
[2290] | 1536 | nodeMapCopies; |
---|
| 1537 | |
---|
[2485] | 1538 | std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
---|
[2290] | 1539 | edgeMapCopies; |
---|
| 1540 | |
---|
[2485] | 1541 | std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* > |
---|
[2290] | 1542 | uEdgeMapCopies; |
---|
| 1543 | |
---|
| 1544 | }; |
---|
| 1545 | |
---|
| 1546 | /// \brief Copy a bipartite undirected graph to another graph. |
---|
| 1547 | /// |
---|
| 1548 | /// Copy a bipartite undirected graph to another graph. |
---|
| 1549 | /// The usage of the function: |
---|
| 1550 | /// |
---|
| 1551 | ///\code |
---|
| 1552 | /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run(); |
---|
| 1553 | ///\endcode |
---|
| 1554 | /// |
---|
| 1555 | /// After the copy the \c nr map will contain the mapping from the |
---|
[2534] | 1556 | /// nodes of the \c from graph to the nodes of the \c to graph and |
---|
| 1557 | /// \c ecr will contain the mapping from the edges of the \c to graph |
---|
| 1558 | /// to the edges of the \c from graph. |
---|
[2290] | 1559 | /// |
---|
| 1560 | /// \see BpUGraphCopy |
---|
[2485] | 1561 | template <typename To, typename From> |
---|
| 1562 | BpUGraphCopy<To, From> |
---|
| 1563 | copyBpUGraph(To& to, const From& from) { |
---|
| 1564 | return BpUGraphCopy<To, From>(to, from); |
---|
[2290] | 1565 | } |
---|
| 1566 | |
---|
[1192] | 1567 | |
---|
| 1568 | /// @} |
---|
[1402] | 1569 | |
---|
| 1570 | /// \addtogroup graph_maps |
---|
| 1571 | /// @{ |
---|
| 1572 | |
---|
[1413] | 1573 | /// Provides an immutable and unique id for each item in the graph. |
---|
| 1574 | |
---|
[1540] | 1575 | /// The IdMap class provides a unique and immutable id for each item of the |
---|
| 1576 | /// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
---|
| 1577 | /// different items (nodes) get different ids <li>\b immutable: the id of an |
---|
| 1578 | /// item (node) does not change (even if you delete other nodes). </ul> |
---|
| 1579 | /// Through this map you get access (i.e. can read) the inner id values of |
---|
| 1580 | /// the items stored in the graph. This map can be inverted with its member |
---|
| 1581 | /// class \c InverseMap. |
---|
[1413] | 1582 | /// |
---|
| 1583 | template <typename _Graph, typename _Item> |
---|
| 1584 | class IdMap { |
---|
| 1585 | public: |
---|
| 1586 | typedef _Graph Graph; |
---|
| 1587 | typedef int Value; |
---|
| 1588 | typedef _Item Item; |
---|
| 1589 | typedef _Item Key; |
---|
| 1590 | |
---|
| 1591 | /// \brief Constructor. |
---|
| 1592 | /// |
---|
[2331] | 1593 | /// Constructor of the map. |
---|
[2286] | 1594 | explicit IdMap(const Graph& _graph) : graph(&_graph) {} |
---|
[1413] | 1595 | |
---|
| 1596 | /// \brief Gives back the \e id of the item. |
---|
| 1597 | /// |
---|
[2331] | 1598 | /// Gives back the immutable and unique \e id of the item. |
---|
[1413] | 1599 | int operator[](const Item& item) const { return graph->id(item);} |
---|
| 1600 | |
---|
[2331] | 1601 | /// \brief Gives back the item by its id. |
---|
| 1602 | /// |
---|
| 1603 | /// Gives back the item by its id. |
---|
| 1604 | Item operator()(int id) { return graph->fromId(id, Item()); } |
---|
[1413] | 1605 | |
---|
| 1606 | private: |
---|
| 1607 | const Graph* graph; |
---|
| 1608 | |
---|
| 1609 | public: |
---|
| 1610 | |
---|
[1540] | 1611 | /// \brief The class represents the inverse of its owner (IdMap). |
---|
[1413] | 1612 | /// |
---|
[1540] | 1613 | /// The class represents the inverse of its owner (IdMap). |
---|
[1413] | 1614 | /// \see inverse() |
---|
| 1615 | class InverseMap { |
---|
| 1616 | public: |
---|
[1419] | 1617 | |
---|
[1413] | 1618 | /// \brief Constructor. |
---|
| 1619 | /// |
---|
| 1620 | /// Constructor for creating an id-to-item map. |
---|
[2286] | 1621 | explicit InverseMap(const Graph& _graph) : graph(&_graph) {} |
---|
[1413] | 1622 | |
---|
| 1623 | /// \brief Constructor. |
---|
| 1624 | /// |
---|
| 1625 | /// Constructor for creating an id-to-item map. |
---|
[2286] | 1626 | explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {} |
---|
[1413] | 1627 | |
---|
| 1628 | /// \brief Gives back the given item from its id. |
---|
| 1629 | /// |
---|
| 1630 | /// Gives back the given item from its id. |
---|
| 1631 | /// |
---|
| 1632 | Item operator[](int id) const { return graph->fromId(id, Item());} |
---|
[2331] | 1633 | |
---|
[1413] | 1634 | private: |
---|
| 1635 | const Graph* graph; |
---|
| 1636 | }; |
---|
| 1637 | |
---|
| 1638 | /// \brief Gives back the inverse of the map. |
---|
| 1639 | /// |
---|
[1540] | 1640 | /// Gives back the inverse of the IdMap. |
---|
[1413] | 1641 | InverseMap inverse() const { return InverseMap(*graph);} |
---|
| 1642 | |
---|
| 1643 | }; |
---|
| 1644 | |
---|
| 1645 | |
---|
[1526] | 1646 | /// \brief General invertable graph-map type. |
---|
[1402] | 1647 | |
---|
[1540] | 1648 | /// This type provides simple invertable graph-maps. |
---|
[1526] | 1649 | /// The InvertableMap wraps an arbitrary ReadWriteMap |
---|
| 1650 | /// and if a key is set to a new value then store it |
---|
[1402] | 1651 | /// in the inverse map. |
---|
[1931] | 1652 | /// |
---|
| 1653 | /// The values of the map can be accessed |
---|
| 1654 | /// with stl compatible forward iterator. |
---|
| 1655 | /// |
---|
[1402] | 1656 | /// \param _Graph The graph type. |
---|
[1830] | 1657 | /// \param _Item The item type of the graph. |
---|
| 1658 | /// \param _Value The value type of the map. |
---|
[1931] | 1659 | /// |
---|
| 1660 | /// \see IterableValueMap |
---|
[1830] | 1661 | template <typename _Graph, typename _Item, typename _Value> |
---|
[2287] | 1662 | class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> { |
---|
[1931] | 1663 | private: |
---|
| 1664 | |
---|
[2287] | 1665 | typedef DefaultMap<_Graph, _Item, _Value> Map; |
---|
[1931] | 1666 | typedef _Graph Graph; |
---|
| 1667 | |
---|
[2287] | 1668 | typedef std::map<_Value, _Item> Container; |
---|
[1931] | 1669 | Container invMap; |
---|
| 1670 | |
---|
| 1671 | public: |
---|
| 1672 | |
---|
[2287] | 1673 | /// The key type of InvertableMap (Node, Edge, UEdge). |
---|
| 1674 | typedef typename Map::Key Key; |
---|
| 1675 | /// The value type of the InvertableMap. |
---|
| 1676 | typedef typename Map::Value Value; |
---|
| 1677 | |
---|
[1931] | 1678 | |
---|
| 1679 | |
---|
[1402] | 1680 | /// \brief Constructor. |
---|
| 1681 | /// |
---|
[1413] | 1682 | /// Construct a new InvertableMap for the graph. |
---|
[1402] | 1683 | /// |
---|
[2286] | 1684 | explicit InvertableMap(const Graph& graph) : Map(graph) {} |
---|
[1931] | 1685 | |
---|
| 1686 | /// \brief Forward iterator for values. |
---|
| 1687 | /// |
---|
| 1688 | /// This iterator is an stl compatible forward |
---|
| 1689 | /// iterator on the values of the map. The values can |
---|
| 1690 | /// be accessed in the [beginValue, endValue) range. |
---|
| 1691 | /// |
---|
| 1692 | class ValueIterator |
---|
| 1693 | : public std::iterator<std::forward_iterator_tag, Value> { |
---|
| 1694 | friend class InvertableMap; |
---|
| 1695 | private: |
---|
| 1696 | ValueIterator(typename Container::const_iterator _it) |
---|
| 1697 | : it(_it) {} |
---|
| 1698 | public: |
---|
| 1699 | |
---|
| 1700 | ValueIterator() {} |
---|
| 1701 | |
---|
| 1702 | ValueIterator& operator++() { ++it; return *this; } |
---|
| 1703 | ValueIterator operator++(int) { |
---|
| 1704 | ValueIterator tmp(*this); |
---|
| 1705 | operator++(); |
---|
| 1706 | return tmp; |
---|
| 1707 | } |
---|
| 1708 | |
---|
| 1709 | const Value& operator*() const { return it->first; } |
---|
| 1710 | const Value* operator->() const { return &(it->first); } |
---|
| 1711 | |
---|
| 1712 | bool operator==(ValueIterator jt) const { return it == jt.it; } |
---|
| 1713 | bool operator!=(ValueIterator jt) const { return it != jt.it; } |
---|
| 1714 | |
---|
| 1715 | private: |
---|
| 1716 | typename Container::const_iterator it; |
---|
| 1717 | }; |
---|
| 1718 | |
---|
| 1719 | /// \brief Returns an iterator to the first value. |
---|
| 1720 | /// |
---|
| 1721 | /// Returns an stl compatible iterator to the |
---|
| 1722 | /// first value of the map. The values of the |
---|
| 1723 | /// map can be accessed in the [beginValue, endValue) |
---|
| 1724 | /// range. |
---|
| 1725 | ValueIterator beginValue() const { |
---|
| 1726 | return ValueIterator(invMap.begin()); |
---|
| 1727 | } |
---|
| 1728 | |
---|
| 1729 | /// \brief Returns an iterator after the last value. |
---|
| 1730 | /// |
---|
| 1731 | /// Returns an stl compatible iterator after the |
---|
| 1732 | /// last value of the map. The values of the |
---|
| 1733 | /// map can be accessed in the [beginValue, endValue) |
---|
| 1734 | /// range. |
---|
| 1735 | ValueIterator endValue() const { |
---|
| 1736 | return ValueIterator(invMap.end()); |
---|
| 1737 | } |
---|
[1402] | 1738 | |
---|
| 1739 | /// \brief The setter function of the map. |
---|
| 1740 | /// |
---|
[1413] | 1741 | /// Sets the mapped value. |
---|
[1402] | 1742 | void set(const Key& key, const Value& val) { |
---|
| 1743 | Value oldval = Map::operator[](key); |
---|
[1413] | 1744 | typename Container::iterator it = invMap.find(oldval); |
---|
[1402] | 1745 | if (it != invMap.end() && it->second == key) { |
---|
| 1746 | invMap.erase(it); |
---|
| 1747 | } |
---|
| 1748 | invMap.insert(make_pair(val, key)); |
---|
| 1749 | Map::set(key, val); |
---|
| 1750 | } |
---|
| 1751 | |
---|
| 1752 | /// \brief The getter function of the map. |
---|
| 1753 | /// |
---|
| 1754 | /// It gives back the value associated with the key. |
---|
[1931] | 1755 | typename MapTraits<Map>::ConstReturnValue |
---|
| 1756 | operator[](const Key& key) const { |
---|
[1402] | 1757 | return Map::operator[](key); |
---|
| 1758 | } |
---|
| 1759 | |
---|
[2331] | 1760 | /// \brief Gives back the item by its value. |
---|
| 1761 | /// |
---|
| 1762 | /// Gives back the item by its value. |
---|
| 1763 | Key operator()(const Value& key) const { |
---|
| 1764 | typename Container::const_iterator it = invMap.find(key); |
---|
| 1765 | return it != invMap.end() ? it->second : INVALID; |
---|
| 1766 | } |
---|
| 1767 | |
---|
[1515] | 1768 | protected: |
---|
| 1769 | |
---|
[1402] | 1770 | /// \brief Erase the key from the map. |
---|
| 1771 | /// |
---|
| 1772 | /// Erase the key to the map. It is called by the |
---|
| 1773 | /// \c AlterationNotifier. |
---|
| 1774 | virtual void erase(const Key& key) { |
---|
| 1775 | Value val = Map::operator[](key); |
---|
[1413] | 1776 | typename Container::iterator it = invMap.find(val); |
---|
[1402] | 1777 | if (it != invMap.end() && it->second == key) { |
---|
| 1778 | invMap.erase(it); |
---|
| 1779 | } |
---|
| 1780 | Map::erase(key); |
---|
| 1781 | } |
---|
| 1782 | |
---|
[1829] | 1783 | /// \brief Erase more keys from the map. |
---|
| 1784 | /// |
---|
| 1785 | /// Erase more keys from the map. It is called by the |
---|
| 1786 | /// \c AlterationNotifier. |
---|
| 1787 | virtual void erase(const std::vector<Key>& keys) { |
---|
[2386] | 1788 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
[1829] | 1789 | Value val = Map::operator[](keys[i]); |
---|
| 1790 | typename Container::iterator it = invMap.find(val); |
---|
| 1791 | if (it != invMap.end() && it->second == keys[i]) { |
---|
| 1792 | invMap.erase(it); |
---|
| 1793 | } |
---|
| 1794 | } |
---|
| 1795 | Map::erase(keys); |
---|
| 1796 | } |
---|
| 1797 | |
---|
[1402] | 1798 | /// \brief Clear the keys from the map and inverse map. |
---|
| 1799 | /// |
---|
| 1800 | /// Clear the keys from the map and inverse map. It is called by the |
---|
| 1801 | /// \c AlterationNotifier. |
---|
| 1802 | virtual void clear() { |
---|
| 1803 | invMap.clear(); |
---|
| 1804 | Map::clear(); |
---|
| 1805 | } |
---|
| 1806 | |
---|
[1413] | 1807 | public: |
---|
| 1808 | |
---|
| 1809 | /// \brief The inverse map type. |
---|
| 1810 | /// |
---|
| 1811 | /// The inverse of this map. The subscript operator of the map |
---|
| 1812 | /// gives back always the item what was last assigned to the value. |
---|
| 1813 | class InverseMap { |
---|
| 1814 | public: |
---|
| 1815 | /// \brief Constructor of the InverseMap. |
---|
| 1816 | /// |
---|
| 1817 | /// Constructor of the InverseMap. |
---|
[2286] | 1818 | explicit InverseMap(const InvertableMap& _inverted) |
---|
| 1819 | : inverted(_inverted) {} |
---|
[1413] | 1820 | |
---|
| 1821 | /// The value type of the InverseMap. |
---|
| 1822 | typedef typename InvertableMap::Key Value; |
---|
| 1823 | /// The key type of the InverseMap. |
---|
| 1824 | typedef typename InvertableMap::Value Key; |
---|
| 1825 | |
---|
| 1826 | /// \brief Subscript operator. |
---|
| 1827 | /// |
---|
| 1828 | /// Subscript operator. It gives back always the item |
---|
| 1829 | /// what was last assigned to the value. |
---|
| 1830 | Value operator[](const Key& key) const { |
---|
[2331] | 1831 | return inverted(key); |
---|
[1413] | 1832 | } |
---|
| 1833 | |
---|
| 1834 | private: |
---|
| 1835 | const InvertableMap& inverted; |
---|
| 1836 | }; |
---|
| 1837 | |
---|
[2094] | 1838 | /// \brief It gives back the just readable inverse map. |
---|
[1402] | 1839 | /// |
---|
[2094] | 1840 | /// It gives back the just readable inverse map. |
---|
[1413] | 1841 | InverseMap inverse() const { |
---|
| 1842 | return InverseMap(*this); |
---|
[1402] | 1843 | } |
---|
| 1844 | |
---|
| 1845 | |
---|
[1413] | 1846 | |
---|
[1402] | 1847 | }; |
---|
| 1848 | |
---|
| 1849 | /// \brief Provides a mutable, continuous and unique descriptor for each |
---|
| 1850 | /// item in the graph. |
---|
| 1851 | /// |
---|
[1540] | 1852 | /// The DescriptorMap class provides a unique and continuous (but mutable) |
---|
| 1853 | /// descriptor (id) for each item of the same type (e.g. node) in the |
---|
| 1854 | /// graph. This id is <ul><li>\b unique: different items (nodes) get |
---|
| 1855 | /// different ids <li>\b continuous: the range of the ids is the set of |
---|
| 1856 | /// integers between 0 and \c n-1, where \c n is the number of the items of |
---|
| 1857 | /// this type (e.g. nodes) (so the id of a node can change if you delete an |
---|
| 1858 | /// other node, i.e. this id is mutable). </ul> This map can be inverted |
---|
| 1859 | /// with its member class \c InverseMap. |
---|
[1402] | 1860 | /// |
---|
| 1861 | /// \param _Graph The graph class the \c DescriptorMap belongs to. |
---|
| 1862 | /// \param _Item The Item is the Key of the Map. It may be Node, Edge or |
---|
[1909] | 1863 | /// UEdge. |
---|
[1830] | 1864 | template <typename _Graph, typename _Item> |
---|
[2287] | 1865 | class DescriptorMap : protected DefaultMap<_Graph, _Item, int> { |
---|
[1402] | 1866 | |
---|
| 1867 | typedef _Item Item; |
---|
[2287] | 1868 | typedef DefaultMap<_Graph, _Item, int> Map; |
---|
[1402] | 1869 | |
---|
| 1870 | public: |
---|
| 1871 | /// The graph class of DescriptorMap. |
---|
| 1872 | typedef _Graph Graph; |
---|
| 1873 | |
---|
[1909] | 1874 | /// The key type of DescriptorMap (Node, Edge, UEdge). |
---|
[2287] | 1875 | typedef typename Map::Key Key; |
---|
[1402] | 1876 | /// The value type of DescriptorMap. |
---|
[2287] | 1877 | typedef typename Map::Value Value; |
---|
[1402] | 1878 | |
---|
| 1879 | /// \brief Constructor. |
---|
| 1880 | /// |
---|
[1413] | 1881 | /// Constructor for descriptor map. |
---|
[2286] | 1882 | explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
---|
[2201] | 1883 | Item it; |
---|
[2386] | 1884 | const typename Map::Notifier* nf = Map::notifier(); |
---|
| 1885 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
[2201] | 1886 | Map::set(it, invMap.size()); |
---|
| 1887 | invMap.push_back(it); |
---|
| 1888 | } |
---|
[1402] | 1889 | } |
---|
| 1890 | |
---|
[1515] | 1891 | protected: |
---|
| 1892 | |
---|
[1402] | 1893 | /// \brief Add a new key to the map. |
---|
| 1894 | /// |
---|
| 1895 | /// Add a new key to the map. It is called by the |
---|
| 1896 | /// \c AlterationNotifier. |
---|
| 1897 | virtual void add(const Item& item) { |
---|
| 1898 | Map::add(item); |
---|
| 1899 | Map::set(item, invMap.size()); |
---|
| 1900 | invMap.push_back(item); |
---|
| 1901 | } |
---|
| 1902 | |
---|
[1829] | 1903 | /// \brief Add more new keys to the map. |
---|
| 1904 | /// |
---|
| 1905 | /// Add more new keys to the map. It is called by the |
---|
| 1906 | /// \c AlterationNotifier. |
---|
| 1907 | virtual void add(const std::vector<Item>& items) { |
---|
| 1908 | Map::add(items); |
---|
[2386] | 1909 | for (int i = 0; i < int(items.size()); ++i) { |
---|
[1829] | 1910 | Map::set(items[i], invMap.size()); |
---|
| 1911 | invMap.push_back(items[i]); |
---|
| 1912 | } |
---|
| 1913 | } |
---|
| 1914 | |
---|
[1402] | 1915 | /// \brief Erase the key from the map. |
---|
| 1916 | /// |
---|
[1829] | 1917 | /// Erase the key from the map. It is called by the |
---|
[1402] | 1918 | /// \c AlterationNotifier. |
---|
| 1919 | virtual void erase(const Item& item) { |
---|
| 1920 | Map::set(invMap.back(), Map::operator[](item)); |
---|
| 1921 | invMap[Map::operator[](item)] = invMap.back(); |
---|
[1413] | 1922 | invMap.pop_back(); |
---|
[1402] | 1923 | Map::erase(item); |
---|
| 1924 | } |
---|
| 1925 | |
---|
[1829] | 1926 | /// \brief Erase more keys from the map. |
---|
| 1927 | /// |
---|
| 1928 | /// Erase more keys from the map. It is called by the |
---|
| 1929 | /// \c AlterationNotifier. |
---|
| 1930 | virtual void erase(const std::vector<Item>& items) { |
---|
[2386] | 1931 | for (int i = 0; i < int(items.size()); ++i) { |
---|
[1829] | 1932 | Map::set(invMap.back(), Map::operator[](items[i])); |
---|
| 1933 | invMap[Map::operator[](items[i])] = invMap.back(); |
---|
| 1934 | invMap.pop_back(); |
---|
| 1935 | } |
---|
| 1936 | Map::erase(items); |
---|
| 1937 | } |
---|
| 1938 | |
---|
[1402] | 1939 | /// \brief Build the unique map. |
---|
| 1940 | /// |
---|
| 1941 | /// Build the unique map. It is called by the |
---|
| 1942 | /// \c AlterationNotifier. |
---|
| 1943 | virtual void build() { |
---|
| 1944 | Map::build(); |
---|
| 1945 | Item it; |
---|
[2386] | 1946 | const typename Map::Notifier* nf = Map::notifier(); |
---|
| 1947 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
[1402] | 1948 | Map::set(it, invMap.size()); |
---|
| 1949 | invMap.push_back(it); |
---|
| 1950 | } |
---|
| 1951 | } |
---|
| 1952 | |
---|
| 1953 | /// \brief Clear the keys from the map. |
---|
| 1954 | /// |
---|
| 1955 | /// Clear the keys from the map. It is called by the |
---|
| 1956 | /// \c AlterationNotifier. |
---|
| 1957 | virtual void clear() { |
---|
| 1958 | invMap.clear(); |
---|
| 1959 | Map::clear(); |
---|
| 1960 | } |
---|
| 1961 | |
---|
[1538] | 1962 | public: |
---|
| 1963 | |
---|
[1931] | 1964 | /// \brief Returns the maximal value plus one. |
---|
| 1965 | /// |
---|
| 1966 | /// Returns the maximal value plus one in the map. |
---|
| 1967 | unsigned int size() const { |
---|
| 1968 | return invMap.size(); |
---|
| 1969 | } |
---|
| 1970 | |
---|
[1552] | 1971 | /// \brief Swaps the position of the two items in the map. |
---|
| 1972 | /// |
---|
| 1973 | /// Swaps the position of the two items in the map. |
---|
| 1974 | void swap(const Item& p, const Item& q) { |
---|
| 1975 | int pi = Map::operator[](p); |
---|
| 1976 | int qi = Map::operator[](q); |
---|
| 1977 | Map::set(p, qi); |
---|
| 1978 | invMap[qi] = p; |
---|
| 1979 | Map::set(q, pi); |
---|
| 1980 | invMap[pi] = q; |
---|
| 1981 | } |
---|
| 1982 | |
---|
[1402] | 1983 | /// \brief Gives back the \e descriptor of the item. |
---|
| 1984 | /// |
---|
| 1985 | /// Gives back the mutable and unique \e descriptor of the map. |
---|
| 1986 | int operator[](const Item& item) const { |
---|
| 1987 | return Map::operator[](item); |
---|
| 1988 | } |
---|
[2331] | 1989 | |
---|
| 1990 | /// \brief Gives back the item by its descriptor. |
---|
| 1991 | /// |
---|
| 1992 | /// Gives back th item by its descriptor. |
---|
| 1993 | Item operator()(int id) const { |
---|
| 1994 | return invMap[id]; |
---|
| 1995 | } |
---|
[1402] | 1996 | |
---|
[1413] | 1997 | private: |
---|
| 1998 | |
---|
| 1999 | typedef std::vector<Item> Container; |
---|
| 2000 | Container invMap; |
---|
| 2001 | |
---|
| 2002 | public: |
---|
[1540] | 2003 | /// \brief The inverse map type of DescriptorMap. |
---|
[1413] | 2004 | /// |
---|
[1540] | 2005 | /// The inverse map type of DescriptorMap. |
---|
[1413] | 2006 | class InverseMap { |
---|
| 2007 | public: |
---|
| 2008 | /// \brief Constructor of the InverseMap. |
---|
| 2009 | /// |
---|
| 2010 | /// Constructor of the InverseMap. |
---|
[2286] | 2011 | explicit InverseMap(const DescriptorMap& _inverted) |
---|
[1413] | 2012 | : inverted(_inverted) {} |
---|
| 2013 | |
---|
| 2014 | |
---|
| 2015 | /// The value type of the InverseMap. |
---|
| 2016 | typedef typename DescriptorMap::Key Value; |
---|
| 2017 | /// The key type of the InverseMap. |
---|
| 2018 | typedef typename DescriptorMap::Value Key; |
---|
| 2019 | |
---|
| 2020 | /// \brief Subscript operator. |
---|
| 2021 | /// |
---|
| 2022 | /// Subscript operator. It gives back the item |
---|
| 2023 | /// that the descriptor belongs to currently. |
---|
| 2024 | Value operator[](const Key& key) const { |
---|
[2331] | 2025 | return inverted(key); |
---|
[1413] | 2026 | } |
---|
[1470] | 2027 | |
---|
| 2028 | /// \brief Size of the map. |
---|
| 2029 | /// |
---|
| 2030 | /// Returns the size of the map. |
---|
[1931] | 2031 | unsigned int size() const { |
---|
[2331] | 2032 | return inverted.size(); |
---|
[1470] | 2033 | } |
---|
[1413] | 2034 | |
---|
| 2035 | private: |
---|
| 2036 | const DescriptorMap& inverted; |
---|
| 2037 | }; |
---|
| 2038 | |
---|
[1402] | 2039 | /// \brief Gives back the inverse of the map. |
---|
| 2040 | /// |
---|
| 2041 | /// Gives back the inverse of the map. |
---|
| 2042 | const InverseMap inverse() const { |
---|
[1413] | 2043 | return InverseMap(*this); |
---|
[1402] | 2044 | } |
---|
| 2045 | }; |
---|
| 2046 | |
---|
| 2047 | /// \brief Returns the source of the given edge. |
---|
| 2048 | /// |
---|
| 2049 | /// The SourceMap gives back the source Node of the given edge. |
---|
[2474] | 2050 | /// \see TargetMap |
---|
[1402] | 2051 | /// \author Balazs Dezso |
---|
| 2052 | template <typename Graph> |
---|
| 2053 | class SourceMap { |
---|
| 2054 | public: |
---|
[1419] | 2055 | |
---|
[1402] | 2056 | typedef typename Graph::Node Value; |
---|
| 2057 | typedef typename Graph::Edge Key; |
---|
| 2058 | |
---|
| 2059 | /// \brief Constructor |
---|
| 2060 | /// |
---|
| 2061 | /// Constructor |
---|
| 2062 | /// \param _graph The graph that the map belongs to. |
---|
[2286] | 2063 | explicit SourceMap(const Graph& _graph) : graph(_graph) {} |
---|
[1402] | 2064 | |
---|
| 2065 | /// \brief The subscript operator. |
---|
| 2066 | /// |
---|
| 2067 | /// The subscript operator. |
---|
| 2068 | /// \param edge The edge |
---|
| 2069 | /// \return The source of the edge |
---|
[1679] | 2070 | Value operator[](const Key& edge) const { |
---|
[1402] | 2071 | return graph.source(edge); |
---|
| 2072 | } |
---|
| 2073 | |
---|
| 2074 | private: |
---|
| 2075 | const Graph& graph; |
---|
| 2076 | }; |
---|
| 2077 | |
---|
[2474] | 2078 | /// \brief Returns a \ref SourceMap class. |
---|
[1402] | 2079 | /// |
---|
| 2080 | /// This function just returns an \ref SourceMap class. |
---|
| 2081 | /// \relates SourceMap |
---|
| 2082 | template <typename Graph> |
---|
| 2083 | inline SourceMap<Graph> sourceMap(const Graph& graph) { |
---|
| 2084 | return SourceMap<Graph>(graph); |
---|
| 2085 | } |
---|
| 2086 | |
---|
| 2087 | /// \brief Returns the target of the given edge. |
---|
| 2088 | /// |
---|
| 2089 | /// The TargetMap gives back the target Node of the given edge. |
---|
[2474] | 2090 | /// \see SourceMap |
---|
[1402] | 2091 | /// \author Balazs Dezso |
---|
| 2092 | template <typename Graph> |
---|
| 2093 | class TargetMap { |
---|
| 2094 | public: |
---|
[1419] | 2095 | |
---|
[1402] | 2096 | typedef typename Graph::Node Value; |
---|
| 2097 | typedef typename Graph::Edge Key; |
---|
| 2098 | |
---|
| 2099 | /// \brief Constructor |
---|
| 2100 | /// |
---|
| 2101 | /// Constructor |
---|
| 2102 | /// \param _graph The graph that the map belongs to. |
---|
[2286] | 2103 | explicit TargetMap(const Graph& _graph) : graph(_graph) {} |
---|
[1402] | 2104 | |
---|
| 2105 | /// \brief The subscript operator. |
---|
| 2106 | /// |
---|
| 2107 | /// The subscript operator. |
---|
[1536] | 2108 | /// \param e The edge |
---|
[1402] | 2109 | /// \return The target of the edge |
---|
[1679] | 2110 | Value operator[](const Key& e) const { |
---|
[1536] | 2111 | return graph.target(e); |
---|
[1402] | 2112 | } |
---|
| 2113 | |
---|
| 2114 | private: |
---|
| 2115 | const Graph& graph; |
---|
| 2116 | }; |
---|
| 2117 | |
---|
[2474] | 2118 | /// \brief Returns a \ref TargetMap class. |
---|
[1515] | 2119 | /// |
---|
[1540] | 2120 | /// This function just returns a \ref TargetMap class. |
---|
[1402] | 2121 | /// \relates TargetMap |
---|
| 2122 | template <typename Graph> |
---|
| 2123 | inline TargetMap<Graph> targetMap(const Graph& graph) { |
---|
| 2124 | return TargetMap<Graph>(graph); |
---|
| 2125 | } |
---|
| 2126 | |
---|
[1540] | 2127 | /// \brief Returns the "forward" directed edge view of an undirected edge. |
---|
[1419] | 2128 | /// |
---|
[1540] | 2129 | /// Returns the "forward" directed edge view of an undirected edge. |
---|
[2474] | 2130 | /// \see BackwardMap |
---|
[1419] | 2131 | /// \author Balazs Dezso |
---|
| 2132 | template <typename Graph> |
---|
| 2133 | class ForwardMap { |
---|
| 2134 | public: |
---|
| 2135 | |
---|
| 2136 | typedef typename Graph::Edge Value; |
---|
[1909] | 2137 | typedef typename Graph::UEdge Key; |
---|
[1419] | 2138 | |
---|
| 2139 | /// \brief Constructor |
---|
| 2140 | /// |
---|
| 2141 | /// Constructor |
---|
| 2142 | /// \param _graph The graph that the map belongs to. |
---|
[2286] | 2143 | explicit ForwardMap(const Graph& _graph) : graph(_graph) {} |
---|
[1419] | 2144 | |
---|
| 2145 | /// \brief The subscript operator. |
---|
| 2146 | /// |
---|
| 2147 | /// The subscript operator. |
---|
| 2148 | /// \param key An undirected edge |
---|
| 2149 | /// \return The "forward" directed edge view of undirected edge |
---|
| 2150 | Value operator[](const Key& key) const { |
---|
[1627] | 2151 | return graph.direct(key, true); |
---|
[1419] | 2152 | } |
---|
| 2153 | |
---|
| 2154 | private: |
---|
| 2155 | const Graph& graph; |
---|
| 2156 | }; |
---|
| 2157 | |
---|
[2474] | 2158 | /// \brief Returns a \ref ForwardMap class. |
---|
[1515] | 2159 | /// |
---|
[1419] | 2160 | /// This function just returns an \ref ForwardMap class. |
---|
| 2161 | /// \relates ForwardMap |
---|
| 2162 | template <typename Graph> |
---|
| 2163 | inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
---|
| 2164 | return ForwardMap<Graph>(graph); |
---|
| 2165 | } |
---|
| 2166 | |
---|
[1540] | 2167 | /// \brief Returns the "backward" directed edge view of an undirected edge. |
---|
[1419] | 2168 | /// |
---|
[1540] | 2169 | /// Returns the "backward" directed edge view of an undirected edge. |
---|
[2474] | 2170 | /// \see ForwardMap |
---|
[1419] | 2171 | /// \author Balazs Dezso |
---|
| 2172 | template <typename Graph> |
---|
| 2173 | class BackwardMap { |
---|
| 2174 | public: |
---|
| 2175 | |
---|
| 2176 | typedef typename Graph::Edge Value; |
---|
[1909] | 2177 | typedef typename Graph::UEdge Key; |
---|
[1419] | 2178 | |
---|
| 2179 | /// \brief Constructor |
---|
| 2180 | /// |
---|
| 2181 | /// Constructor |
---|
| 2182 | /// \param _graph The graph that the map belongs to. |
---|
[2286] | 2183 | explicit BackwardMap(const Graph& _graph) : graph(_graph) {} |
---|
[1419] | 2184 | |
---|
| 2185 | /// \brief The subscript operator. |
---|
| 2186 | /// |
---|
| 2187 | /// The subscript operator. |
---|
| 2188 | /// \param key An undirected edge |
---|
| 2189 | /// \return The "backward" directed edge view of undirected edge |
---|
| 2190 | Value operator[](const Key& key) const { |
---|
[1627] | 2191 | return graph.direct(key, false); |
---|
[1419] | 2192 | } |
---|
| 2193 | |
---|
| 2194 | private: |
---|
| 2195 | const Graph& graph; |
---|
| 2196 | }; |
---|
| 2197 | |
---|
| 2198 | /// \brief Returns a \ref BackwardMap class |
---|
| 2199 | |
---|
[1540] | 2200 | /// This function just returns a \ref BackwardMap class. |
---|
[1419] | 2201 | /// \relates BackwardMap |
---|
| 2202 | template <typename Graph> |
---|
| 2203 | inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
---|
| 2204 | return BackwardMap<Graph>(graph); |
---|
| 2205 | } |
---|
| 2206 | |
---|
[1695] | 2207 | /// \brief Potential difference map |
---|
| 2208 | /// |
---|
| 2209 | /// If there is an potential map on the nodes then we |
---|
| 2210 | /// can get an edge map as we get the substraction of the |
---|
| 2211 | /// values of the target and source. |
---|
| 2212 | template <typename Graph, typename NodeMap> |
---|
| 2213 | class PotentialDifferenceMap { |
---|
[1515] | 2214 | public: |
---|
[1695] | 2215 | typedef typename Graph::Edge Key; |
---|
| 2216 | typedef typename NodeMap::Value Value; |
---|
| 2217 | |
---|
| 2218 | /// \brief Constructor |
---|
| 2219 | /// |
---|
| 2220 | /// Contructor of the map |
---|
[2286] | 2221 | explicit PotentialDifferenceMap(const Graph& _graph, |
---|
| 2222 | const NodeMap& _potential) |
---|
[1695] | 2223 | : graph(_graph), potential(_potential) {} |
---|
| 2224 | |
---|
| 2225 | /// \brief Const subscription operator |
---|
| 2226 | /// |
---|
| 2227 | /// Const subscription operator |
---|
| 2228 | Value operator[](const Key& edge) const { |
---|
| 2229 | return potential[graph.target(edge)] - potential[graph.source(edge)]; |
---|
| 2230 | } |
---|
| 2231 | |
---|
| 2232 | private: |
---|
| 2233 | const Graph& graph; |
---|
| 2234 | const NodeMap& potential; |
---|
| 2235 | }; |
---|
| 2236 | |
---|
[2474] | 2237 | /// \brief Returns a PotentialDifferenceMap. |
---|
[1695] | 2238 | /// |
---|
[2474] | 2239 | /// This function just returns a PotentialDifferenceMap. |
---|
[1695] | 2240 | /// \relates PotentialDifferenceMap |
---|
| 2241 | template <typename Graph, typename NodeMap> |
---|
| 2242 | PotentialDifferenceMap<Graph, NodeMap> |
---|
| 2243 | potentialDifferenceMap(const Graph& graph, const NodeMap& potential) { |
---|
| 2244 | return PotentialDifferenceMap<Graph, NodeMap>(graph, potential); |
---|
| 2245 | } |
---|
| 2246 | |
---|
[1515] | 2247 | /// \brief Map of the node in-degrees. |
---|
[1453] | 2248 | /// |
---|
[1540] | 2249 | /// This map returns the in-degree of a node. Once it is constructed, |
---|
[1515] | 2250 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
[1540] | 2251 | /// in constant time. On the other hand, the values are updated automatically |
---|
[1515] | 2252 | /// whenever the graph changes. |
---|
| 2253 | /// |
---|
[1729] | 2254 | /// \warning Besides addNode() and addEdge(), a graph structure may provide |
---|
[1730] | 2255 | /// alternative ways to modify the graph. The correct behavior of InDegMap |
---|
[1829] | 2256 | /// is not guarantied if these additional features are used. For example |
---|
| 2257 | /// the functions \ref ListGraph::changeSource() "changeSource()", |
---|
[1729] | 2258 | /// \ref ListGraph::changeTarget() "changeTarget()" and |
---|
| 2259 | /// \ref ListGraph::reverseEdge() "reverseEdge()" |
---|
| 2260 | /// of \ref ListGraph will \e not update the degree values correctly. |
---|
| 2261 | /// |
---|
[1515] | 2262 | /// \sa OutDegMap |
---|
| 2263 | |
---|
[1453] | 2264 | template <typename _Graph> |
---|
[1515] | 2265 | class InDegMap |
---|
[1999] | 2266 | : protected ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 2267 | ::ItemNotifier::ObserverBase { |
---|
[1515] | 2268 | |
---|
[1453] | 2269 | public: |
---|
[1515] | 2270 | |
---|
| 2271 | typedef _Graph Graph; |
---|
[1453] | 2272 | typedef int Value; |
---|
[1515] | 2273 | typedef typename Graph::Node Key; |
---|
| 2274 | |
---|
[1999] | 2275 | typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 2276 | ::ItemNotifier::ObserverBase Parent; |
---|
| 2277 | |
---|
[1515] | 2278 | private: |
---|
| 2279 | |
---|
[1990] | 2280 | class AutoNodeMap : public DefaultMap<_Graph, Key, int> { |
---|
[1515] | 2281 | public: |
---|
| 2282 | |
---|
[1990] | 2283 | typedef DefaultMap<_Graph, Key, int> Parent; |
---|
[2002] | 2284 | typedef typename Parent::Graph Graph; |
---|
[1515] | 2285 | |
---|
| 2286 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 2287 | |
---|
[1829] | 2288 | virtual void add(const Key& key) { |
---|
[1515] | 2289 | Parent::add(key); |
---|
| 2290 | Parent::set(key, 0); |
---|
| 2291 | } |
---|
[1931] | 2292 | |
---|
[1829] | 2293 | virtual void add(const std::vector<Key>& keys) { |
---|
| 2294 | Parent::add(keys); |
---|
[2386] | 2295 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
[1829] | 2296 | Parent::set(keys[i], 0); |
---|
| 2297 | } |
---|
| 2298 | } |
---|
[1515] | 2299 | }; |
---|
| 2300 | |
---|
| 2301 | public: |
---|
[1453] | 2302 | |
---|
| 2303 | /// \brief Constructor. |
---|
| 2304 | /// |
---|
| 2305 | /// Constructor for creating in-degree map. |
---|
[2286] | 2306 | explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[2384] | 2307 | Parent::attach(graph.notifier(typename _Graph::Edge())); |
---|
[1515] | 2308 | |
---|
| 2309 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2310 | deg[it] = countInEdges(graph, it); |
---|
| 2311 | } |
---|
[1453] | 2312 | } |
---|
| 2313 | |
---|
[1459] | 2314 | /// Gives back the in-degree of a Node. |
---|
[1515] | 2315 | int operator[](const Key& key) const { |
---|
| 2316 | return deg[key]; |
---|
[1459] | 2317 | } |
---|
[1453] | 2318 | |
---|
| 2319 | protected: |
---|
[1515] | 2320 | |
---|
| 2321 | typedef typename Graph::Edge Edge; |
---|
| 2322 | |
---|
| 2323 | virtual void add(const Edge& edge) { |
---|
| 2324 | ++deg[graph.target(edge)]; |
---|
[1453] | 2325 | } |
---|
| 2326 | |
---|
[1931] | 2327 | virtual void add(const std::vector<Edge>& edges) { |
---|
[2386] | 2328 | for (int i = 0; i < int(edges.size()); ++i) { |
---|
[1931] | 2329 | ++deg[graph.target(edges[i])]; |
---|
| 2330 | } |
---|
| 2331 | } |
---|
| 2332 | |
---|
[1515] | 2333 | virtual void erase(const Edge& edge) { |
---|
| 2334 | --deg[graph.target(edge)]; |
---|
| 2335 | } |
---|
| 2336 | |
---|
[1931] | 2337 | virtual void erase(const std::vector<Edge>& edges) { |
---|
[2386] | 2338 | for (int i = 0; i < int(edges.size()); ++i) { |
---|
[1931] | 2339 | --deg[graph.target(edges[i])]; |
---|
| 2340 | } |
---|
| 2341 | } |
---|
| 2342 | |
---|
[1515] | 2343 | virtual void build() { |
---|
| 2344 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2345 | deg[it] = countInEdges(graph, it); |
---|
| 2346 | } |
---|
| 2347 | } |
---|
| 2348 | |
---|
| 2349 | virtual void clear() { |
---|
| 2350 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2351 | deg[it] = 0; |
---|
| 2352 | } |
---|
| 2353 | } |
---|
| 2354 | private: |
---|
[1506] | 2355 | |
---|
[1515] | 2356 | const _Graph& graph; |
---|
| 2357 | AutoNodeMap deg; |
---|
[1459] | 2358 | }; |
---|
| 2359 | |
---|
[1515] | 2360 | /// \brief Map of the node out-degrees. |
---|
| 2361 | /// |
---|
[1540] | 2362 | /// This map returns the out-degree of a node. Once it is constructed, |
---|
[1515] | 2363 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
[1540] | 2364 | /// in constant time. On the other hand, the values are updated automatically |
---|
[1515] | 2365 | /// whenever the graph changes. |
---|
| 2366 | /// |
---|
[1729] | 2367 | /// \warning Besides addNode() and addEdge(), a graph structure may provide |
---|
[1730] | 2368 | /// alternative ways to modify the graph. The correct behavior of OutDegMap |
---|
[1829] | 2369 | /// is not guarantied if these additional features are used. For example |
---|
| 2370 | /// the functions \ref ListGraph::changeSource() "changeSource()", |
---|
[1729] | 2371 | /// \ref ListGraph::changeTarget() "changeTarget()" and |
---|
| 2372 | /// \ref ListGraph::reverseEdge() "reverseEdge()" |
---|
| 2373 | /// of \ref ListGraph will \e not update the degree values correctly. |
---|
| 2374 | /// |
---|
[1555] | 2375 | /// \sa InDegMap |
---|
[1459] | 2376 | |
---|
| 2377 | template <typename _Graph> |
---|
[1515] | 2378 | class OutDegMap |
---|
[1999] | 2379 | : protected ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 2380 | ::ItemNotifier::ObserverBase { |
---|
[1515] | 2381 | |
---|
[1459] | 2382 | public: |
---|
[1999] | 2383 | |
---|
| 2384 | typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> |
---|
| 2385 | ::ItemNotifier::ObserverBase Parent; |
---|
[1515] | 2386 | |
---|
| 2387 | typedef _Graph Graph; |
---|
[1459] | 2388 | typedef int Value; |
---|
[1515] | 2389 | typedef typename Graph::Node Key; |
---|
| 2390 | |
---|
| 2391 | private: |
---|
| 2392 | |
---|
[1990] | 2393 | class AutoNodeMap : public DefaultMap<_Graph, Key, int> { |
---|
[1515] | 2394 | public: |
---|
| 2395 | |
---|
[1990] | 2396 | typedef DefaultMap<_Graph, Key, int> Parent; |
---|
[2002] | 2397 | typedef typename Parent::Graph Graph; |
---|
[1515] | 2398 | |
---|
| 2399 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 2400 | |
---|
[1829] | 2401 | virtual void add(const Key& key) { |
---|
[1515] | 2402 | Parent::add(key); |
---|
| 2403 | Parent::set(key, 0); |
---|
| 2404 | } |
---|
[1829] | 2405 | virtual void add(const std::vector<Key>& keys) { |
---|
| 2406 | Parent::add(keys); |
---|
[2386] | 2407 | for (int i = 0; i < int(keys.size()); ++i) { |
---|
[1829] | 2408 | Parent::set(keys[i], 0); |
---|
| 2409 | } |
---|
| 2410 | } |
---|
[1515] | 2411 | }; |
---|
| 2412 | |
---|
| 2413 | public: |
---|
[1459] | 2414 | |
---|
| 2415 | /// \brief Constructor. |
---|
| 2416 | /// |
---|
| 2417 | /// Constructor for creating out-degree map. |
---|
[2286] | 2418 | explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[2384] | 2419 | Parent::attach(graph.notifier(typename _Graph::Edge())); |
---|
[1515] | 2420 | |
---|
| 2421 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2422 | deg[it] = countOutEdges(graph, it); |
---|
| 2423 | } |
---|
[1459] | 2424 | } |
---|
| 2425 | |
---|
[1990] | 2426 | /// Gives back the out-degree of a Node. |
---|
[1515] | 2427 | int operator[](const Key& key) const { |
---|
| 2428 | return deg[key]; |
---|
[1459] | 2429 | } |
---|
| 2430 | |
---|
| 2431 | protected: |
---|
[1515] | 2432 | |
---|
| 2433 | typedef typename Graph::Edge Edge; |
---|
| 2434 | |
---|
| 2435 | virtual void add(const Edge& edge) { |
---|
| 2436 | ++deg[graph.source(edge)]; |
---|
[1459] | 2437 | } |
---|
| 2438 | |
---|
[1931] | 2439 | virtual void add(const std::vector<Edge>& edges) { |
---|
[2386] | 2440 | for (int i = 0; i < int(edges.size()); ++i) { |
---|
[1931] | 2441 | ++deg[graph.source(edges[i])]; |
---|
| 2442 | } |
---|
| 2443 | } |
---|
| 2444 | |
---|
[1515] | 2445 | virtual void erase(const Edge& edge) { |
---|
| 2446 | --deg[graph.source(edge)]; |
---|
| 2447 | } |
---|
| 2448 | |
---|
[1931] | 2449 | virtual void erase(const std::vector<Edge>& edges) { |
---|
[2386] | 2450 | for (int i = 0; i < int(edges.size()); ++i) { |
---|
[1931] | 2451 | --deg[graph.source(edges[i])]; |
---|
| 2452 | } |
---|
| 2453 | } |
---|
| 2454 | |
---|
[1515] | 2455 | virtual void build() { |
---|
| 2456 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2457 | deg[it] = countOutEdges(graph, it); |
---|
| 2458 | } |
---|
| 2459 | } |
---|
| 2460 | |
---|
| 2461 | virtual void clear() { |
---|
| 2462 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 2463 | deg[it] = 0; |
---|
| 2464 | } |
---|
| 2465 | } |
---|
| 2466 | private: |
---|
[1506] | 2467 | |
---|
[1515] | 2468 | const _Graph& graph; |
---|
| 2469 | AutoNodeMap deg; |
---|
[1453] | 2470 | }; |
---|
| 2471 | |
---|
[1695] | 2472 | |
---|
[2235] | 2473 | ///Fast edge look up between given endpoints. |
---|
| 2474 | |
---|
| 2475 | ///\ingroup gutils |
---|
| 2476 | ///Using this class, you can find an edge in a graph from a given |
---|
| 2477 | ///source to a given target in time <em>O(log d)</em>, |
---|
| 2478 | ///where <em>d</em> is the out-degree of the source node. |
---|
| 2479 | /// |
---|
| 2480 | ///It is not possible to find \e all parallel edges between two nodes. |
---|
| 2481 | ///Use \ref AllEdgeLookUp for this purpose. |
---|
| 2482 | /// |
---|
| 2483 | ///\warning This class is static, so you should refresh() (or at least |
---|
| 2484 | ///refresh(Node)) this data structure |
---|
| 2485 | ///whenever the graph changes. This is a time consuming (superlinearly |
---|
| 2486 | ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges). |
---|
| 2487 | /// |
---|
| 2488 | ///\param G The type of the underlying graph. |
---|
| 2489 | /// |
---|
| 2490 | ///\sa AllEdgeLookUp |
---|
| 2491 | template<class G> |
---|
| 2492 | class EdgeLookUp |
---|
| 2493 | { |
---|
| 2494 | public: |
---|
[2510] | 2495 | GRAPH_TYPEDEFS(typename G); |
---|
[2235] | 2496 | typedef G Graph; |
---|
| 2497 | |
---|
| 2498 | protected: |
---|
| 2499 | const Graph &_g; |
---|
| 2500 | typename Graph::template NodeMap<Edge> _head; |
---|
| 2501 | typename Graph::template EdgeMap<Edge> _left; |
---|
| 2502 | typename Graph::template EdgeMap<Edge> _right; |
---|
| 2503 | |
---|
| 2504 | class EdgeLess { |
---|
| 2505 | const Graph &g; |
---|
| 2506 | public: |
---|
| 2507 | EdgeLess(const Graph &_g) : g(_g) {} |
---|
| 2508 | bool operator()(Edge a,Edge b) const |
---|
| 2509 | { |
---|
| 2510 | return g.target(a)<g.target(b); |
---|
| 2511 | } |
---|
| 2512 | }; |
---|
| 2513 | |
---|
| 2514 | public: |
---|
| 2515 | |
---|
| 2516 | ///Constructor |
---|
| 2517 | |
---|
| 2518 | ///Constructor. |
---|
| 2519 | /// |
---|
| 2520 | ///It builds up the search database, which remains valid until the graph |
---|
| 2521 | ///changes. |
---|
| 2522 | EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
---|
| 2523 | |
---|
| 2524 | private: |
---|
| 2525 | Edge refresh_rec(std::vector<Edge> &v,int a,int b) |
---|
| 2526 | { |
---|
| 2527 | int m=(a+b)/2; |
---|
| 2528 | Edge me=v[m]; |
---|
| 2529 | _left[me] = a<m?refresh_rec(v,a,m-1):INVALID; |
---|
| 2530 | _right[me] = m<b?refresh_rec(v,m+1,b):INVALID; |
---|
| 2531 | return me; |
---|
| 2532 | } |
---|
| 2533 | public: |
---|
| 2534 | ///Refresh the data structure at a node. |
---|
| 2535 | |
---|
| 2536 | ///Build up the search database of node \c n. |
---|
| 2537 | /// |
---|
| 2538 | ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
---|
| 2539 | ///the number of the outgoing edges of \c n. |
---|
| 2540 | void refresh(Node n) |
---|
| 2541 | { |
---|
| 2542 | std::vector<Edge> v; |
---|
| 2543 | for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
---|
| 2544 | if(v.size()) { |
---|
| 2545 | std::sort(v.begin(),v.end(),EdgeLess(_g)); |
---|
| 2546 | _head[n]=refresh_rec(v,0,v.size()-1); |
---|
| 2547 | } |
---|
| 2548 | else _head[n]=INVALID; |
---|
| 2549 | } |
---|
| 2550 | ///Refresh the full data structure. |
---|
| 2551 | |
---|
| 2552 | ///Build up the full search database. In fact, it simply calls |
---|
| 2553 | ///\ref refresh(Node) "refresh(n)" for each node \c n. |
---|
| 2554 | /// |
---|
| 2555 | ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
---|
| 2556 | ///the number of the edges of \c n and <em>D</em> is the maximum |
---|
| 2557 | ///out-degree of the graph. |
---|
| 2558 | |
---|
| 2559 | void refresh() |
---|
| 2560 | { |
---|
| 2561 | for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
---|
| 2562 | } |
---|
| 2563 | |
---|
| 2564 | ///Find an edge between two nodes. |
---|
| 2565 | |
---|
| 2566 | ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where |
---|
| 2567 | /// <em>d</em> is the number of outgoing edges of \c s. |
---|
| 2568 | ///\param s The source node |
---|
| 2569 | ///\param t The target node |
---|
| 2570 | ///\return An edge from \c s to \c t if there exists, |
---|
| 2571 | ///\ref INVALID otherwise. |
---|
| 2572 | /// |
---|
| 2573 | ///\warning If you change the graph, refresh() must be called before using |
---|
| 2574 | ///this operator. If you change the outgoing edges of |
---|
| 2575 | ///a single node \c n, then |
---|
| 2576 | ///\ref refresh(Node) "refresh(n)" is enough. |
---|
| 2577 | /// |
---|
| 2578 | Edge operator()(Node s, Node t) const |
---|
| 2579 | { |
---|
| 2580 | Edge e; |
---|
| 2581 | for(e=_head[s]; |
---|
| 2582 | e!=INVALID&&_g.target(e)!=t; |
---|
| 2583 | e = t < _g.target(e)?_left[e]:_right[e]) ; |
---|
| 2584 | return e; |
---|
| 2585 | } |
---|
| 2586 | |
---|
| 2587 | }; |
---|
| 2588 | |
---|
| 2589 | ///Fast look up of all edges between given endpoints. |
---|
| 2590 | |
---|
| 2591 | ///\ingroup gutils |
---|
| 2592 | ///This class is the same as \ref EdgeLookUp, with the addition |
---|
| 2593 | ///that it makes it possible to find all edges between given endpoints. |
---|
| 2594 | /// |
---|
| 2595 | ///\warning This class is static, so you should refresh() (or at least |
---|
| 2596 | ///refresh(Node)) this data structure |
---|
| 2597 | ///whenever the graph changes. This is a time consuming (superlinearly |
---|
| 2598 | ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges). |
---|
| 2599 | /// |
---|
| 2600 | ///\param G The type of the underlying graph. |
---|
| 2601 | /// |
---|
| 2602 | ///\sa EdgeLookUp |
---|
| 2603 | template<class G> |
---|
| 2604 | class AllEdgeLookUp : public EdgeLookUp<G> |
---|
| 2605 | { |
---|
| 2606 | using EdgeLookUp<G>::_g; |
---|
| 2607 | using EdgeLookUp<G>::_right; |
---|
| 2608 | using EdgeLookUp<G>::_left; |
---|
| 2609 | using EdgeLookUp<G>::_head; |
---|
| 2610 | |
---|
[2510] | 2611 | GRAPH_TYPEDEFS(typename G); |
---|
[2235] | 2612 | typedef G Graph; |
---|
| 2613 | |
---|
| 2614 | typename Graph::template EdgeMap<Edge> _next; |
---|
| 2615 | |
---|
| 2616 | Edge refreshNext(Edge head,Edge next=INVALID) |
---|
| 2617 | { |
---|
| 2618 | if(head==INVALID) return next; |
---|
| 2619 | else { |
---|
| 2620 | next=refreshNext(_right[head],next); |
---|
| 2621 | // _next[head]=next; |
---|
| 2622 | _next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
---|
| 2623 | ? next : INVALID; |
---|
| 2624 | return refreshNext(_left[head],head); |
---|
| 2625 | } |
---|
| 2626 | } |
---|
| 2627 | |
---|
| 2628 | void refreshNext() |
---|
| 2629 | { |
---|
| 2630 | for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
---|
| 2631 | } |
---|
| 2632 | |
---|
| 2633 | public: |
---|
| 2634 | ///Constructor |
---|
| 2635 | |
---|
| 2636 | ///Constructor. |
---|
| 2637 | /// |
---|
| 2638 | ///It builds up the search database, which remains valid until the graph |
---|
| 2639 | ///changes. |
---|
| 2640 | AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();} |
---|
| 2641 | |
---|
| 2642 | ///Refresh the data structure at a node. |
---|
| 2643 | |
---|
| 2644 | ///Build up the search database of node \c n. |
---|
| 2645 | /// |
---|
| 2646 | ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
---|
| 2647 | ///the number of the outgoing edges of \c n. |
---|
| 2648 | |
---|
| 2649 | void refresh(Node n) |
---|
| 2650 | { |
---|
| 2651 | EdgeLookUp<G>::refresh(n); |
---|
| 2652 | refreshNext(_head[n]); |
---|
| 2653 | } |
---|
| 2654 | |
---|
| 2655 | ///Refresh the full data structure. |
---|
| 2656 | |
---|
| 2657 | ///Build up the full search database. In fact, it simply calls |
---|
| 2658 | ///\ref refresh(Node) "refresh(n)" for each node \c n. |
---|
| 2659 | /// |
---|
| 2660 | ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
---|
| 2661 | ///the number of the edges of \c n and <em>D</em> is the maximum |
---|
| 2662 | ///out-degree of the graph. |
---|
| 2663 | |
---|
| 2664 | void refresh() |
---|
| 2665 | { |
---|
| 2666 | for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
---|
| 2667 | } |
---|
| 2668 | |
---|
| 2669 | ///Find an edge between two nodes. |
---|
| 2670 | |
---|
| 2671 | ///Find an edge between two nodes. |
---|
| 2672 | ///\param s The source node |
---|
| 2673 | ///\param t The target node |
---|
| 2674 | ///\param prev The previous edge between \c s and \c t. It it is INVALID or |
---|
| 2675 | ///not given, the operator finds the first appropriate edge. |
---|
[2350] | 2676 | ///\return An edge from \c s to \c t after \c prev or |
---|
[2235] | 2677 | ///\ref INVALID if there is no more. |
---|
| 2678 | /// |
---|
| 2679 | ///For example, you can count the number of edges from \c u to \c v in the |
---|
| 2680 | ///following way. |
---|
| 2681 | ///\code |
---|
| 2682 | ///AllEdgeLookUp<ListGraph> ae(g); |
---|
| 2683 | ///... |
---|
| 2684 | ///int n=0; |
---|
| 2685 | ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
---|
| 2686 | ///\endcode |
---|
| 2687 | /// |
---|
| 2688 | ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where |
---|
| 2689 | /// <em>d</em> is the number of outgoing edges of \c s. Then, the |
---|
| 2690 | ///consecutive edges are found in constant time. |
---|
| 2691 | /// |
---|
| 2692 | ///\warning If you change the graph, refresh() must be called before using |
---|
| 2693 | ///this operator. If you change the outgoing edges of |
---|
| 2694 | ///a single node \c n, then |
---|
| 2695 | ///\ref refresh(Node) "refresh(n)" is enough. |
---|
| 2696 | /// |
---|
| 2697 | #ifdef DOXYGEN |
---|
| 2698 | Edge operator()(Node s, Node t, Edge prev=INVALID) const {} |
---|
| 2699 | #else |
---|
| 2700 | using EdgeLookUp<G>::operator() ; |
---|
| 2701 | Edge operator()(Node s, Node t, Edge prev) const |
---|
| 2702 | { |
---|
| 2703 | return prev==INVALID?(*this)(s,t):_next[prev]; |
---|
| 2704 | } |
---|
| 2705 | #endif |
---|
| 2706 | |
---|
| 2707 | }; |
---|
| 2708 | |
---|
[1402] | 2709 | /// @} |
---|
| 2710 | |
---|
[947] | 2711 | } //END OF NAMESPACE LEMON |
---|
[946] | 2712 | |
---|
| 2713 | #endif |
---|