[220] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
| 4 | * |
---|
[1092] | 5 | * Copyright (C) 2003-2013 |
---|
[220] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 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_CORE_H |
---|
| 20 | #define LEMON_CORE_H |
---|
| 21 | |
---|
[1135] | 22 | ///\file |
---|
| 23 | ///\brief LEMON core utilities. |
---|
| 24 | /// |
---|
| 25 | ///This header file contains core utilities for LEMON. |
---|
| 26 | ///It is automatically included by all graph types, therefore it usually |
---|
| 27 | ///do not have to be included directly. |
---|
[220] | 28 | |
---|
[671] | 29 | // Disable the following warnings when compiling with MSVC: |
---|
| 30 | // C4250: 'class1' : inherits 'class2::member' via dominance |
---|
[1123] | 31 | // C4267: conversion from 'size_t' to 'type', possible loss of data |
---|
[671] | 32 | // C4355: 'this' : used in base member initializer list |
---|
| 33 | // C4503: 'function' : decorated name length exceeded, name was truncated |
---|
| 34 | // C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
---|
| 35 | // C4996: 'function': was declared deprecated |
---|
| 36 | #ifdef _MSC_VER |
---|
[1123] | 37 | #pragma warning( disable : 4250 4267 4355 4503 4800 4996 ) |
---|
[671] | 38 | #endif |
---|
| 39 | |
---|
[1121] | 40 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) |
---|
[1068] | 41 | // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8 |
---|
| 42 | #pragma GCC diagnostic ignored "-Wunused-local-typedefs" |
---|
| 43 | #endif |
---|
| 44 | |
---|
[1135] | 45 | #include <vector> |
---|
| 46 | #include <algorithm> |
---|
| 47 | |
---|
| 48 | #include <lemon/config.h> |
---|
| 49 | #include <lemon/bits/enable_if.h> |
---|
| 50 | #include <lemon/bits/traits.h> |
---|
| 51 | #include <lemon/assert.h> |
---|
| 52 | |
---|
| 53 | |
---|
[220] | 54 | |
---|
| 55 | namespace lemon { |
---|
| 56 | |
---|
| 57 | /// \brief Dummy type to make it easier to create invalid iterators. |
---|
| 58 | /// |
---|
| 59 | /// Dummy type to make it easier to create invalid iterators. |
---|
| 60 | /// See \ref INVALID for the usage. |
---|
| 61 | struct Invalid { |
---|
| 62 | public: |
---|
| 63 | bool operator==(Invalid) { return true; } |
---|
| 64 | bool operator!=(Invalid) { return false; } |
---|
| 65 | bool operator< (Invalid) { return false; } |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | /// \brief Invalid iterators. |
---|
| 69 | /// |
---|
| 70 | /// \ref Invalid is a global type that converts to each iterator |
---|
| 71 | /// in such a way that the value of the target iterator will be invalid. |
---|
| 72 | #ifdef LEMON_ONLY_TEMPLATES |
---|
| 73 | const Invalid INVALID = Invalid(); |
---|
| 74 | #else |
---|
| 75 | extern const Invalid INVALID; |
---|
| 76 | #endif |
---|
| 77 | |
---|
| 78 | /// \addtogroup gutils |
---|
| 79 | /// @{ |
---|
| 80 | |
---|
[300] | 81 | ///Create convenience typedefs for the digraph types and iterators |
---|
[220] | 82 | |
---|
[282] | 83 | ///This \c \#define creates convenient type definitions for the following |
---|
| 84 | ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
---|
[220] | 85 | ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
---|
| 86 | ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
---|
| 87 | /// |
---|
| 88 | ///\note If the graph type is a dependent type, ie. the graph type depend |
---|
| 89 | ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
---|
| 90 | ///macro. |
---|
| 91 | #define DIGRAPH_TYPEDEFS(Digraph) \ |
---|
| 92 | typedef Digraph::Node Node; \ |
---|
| 93 | typedef Digraph::NodeIt NodeIt; \ |
---|
| 94 | typedef Digraph::Arc Arc; \ |
---|
| 95 | typedef Digraph::ArcIt ArcIt; \ |
---|
| 96 | typedef Digraph::InArcIt InArcIt; \ |
---|
| 97 | typedef Digraph::OutArcIt OutArcIt; \ |
---|
| 98 | typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
---|
| 99 | typedef Digraph::NodeMap<int> IntNodeMap; \ |
---|
| 100 | typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
---|
| 101 | typedef Digraph::ArcMap<bool> BoolArcMap; \ |
---|
| 102 | typedef Digraph::ArcMap<int> IntArcMap; \ |
---|
[300] | 103 | typedef Digraph::ArcMap<double> DoubleArcMap |
---|
[220] | 104 | |
---|
[300] | 105 | ///Create convenience typedefs for the digraph types and iterators |
---|
[220] | 106 | |
---|
| 107 | ///\see DIGRAPH_TYPEDEFS |
---|
| 108 | /// |
---|
| 109 | ///\note Use this macro, if the graph type is a dependent type, |
---|
| 110 | ///ie. the graph type depend on a template parameter. |
---|
| 111 | #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
---|
| 112 | typedef typename Digraph::Node Node; \ |
---|
| 113 | typedef typename Digraph::NodeIt NodeIt; \ |
---|
| 114 | typedef typename Digraph::Arc Arc; \ |
---|
| 115 | typedef typename Digraph::ArcIt ArcIt; \ |
---|
| 116 | typedef typename Digraph::InArcIt InArcIt; \ |
---|
| 117 | typedef typename Digraph::OutArcIt OutArcIt; \ |
---|
| 118 | typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
---|
| 119 | typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
---|
| 120 | typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
---|
| 121 | typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
---|
| 122 | typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
---|
[300] | 123 | typedef typename Digraph::template ArcMap<double> DoubleArcMap |
---|
[220] | 124 | |
---|
[300] | 125 | ///Create convenience typedefs for the graph types and iterators |
---|
[220] | 126 | |
---|
[282] | 127 | ///This \c \#define creates the same convenient type definitions as defined |
---|
[220] | 128 | ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
---|
| 129 | ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
---|
| 130 | ///\c DoubleEdgeMap. |
---|
| 131 | /// |
---|
| 132 | ///\note If the graph type is a dependent type, ie. the graph type depend |
---|
[282] | 133 | ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
---|
[220] | 134 | ///macro. |
---|
| 135 | #define GRAPH_TYPEDEFS(Graph) \ |
---|
| 136 | DIGRAPH_TYPEDEFS(Graph); \ |
---|
| 137 | typedef Graph::Edge Edge; \ |
---|
| 138 | typedef Graph::EdgeIt EdgeIt; \ |
---|
| 139 | typedef Graph::IncEdgeIt IncEdgeIt; \ |
---|
| 140 | typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
---|
| 141 | typedef Graph::EdgeMap<int> IntEdgeMap; \ |
---|
[300] | 142 | typedef Graph::EdgeMap<double> DoubleEdgeMap |
---|
[220] | 143 | |
---|
[300] | 144 | ///Create convenience typedefs for the graph types and iterators |
---|
[220] | 145 | |
---|
| 146 | ///\see GRAPH_TYPEDEFS |
---|
| 147 | /// |
---|
| 148 | ///\note Use this macro, if the graph type is a dependent type, |
---|
| 149 | ///ie. the graph type depend on a template parameter. |
---|
| 150 | #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
---|
| 151 | TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
---|
| 152 | typedef typename Graph::Edge Edge; \ |
---|
| 153 | typedef typename Graph::EdgeIt EdgeIt; \ |
---|
| 154 | typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
---|
| 155 | typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
---|
| 156 | typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
---|
[300] | 157 | typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
---|
[220] | 158 | |
---|
[1019] | 159 | ///Create convenience typedefs for the bipartite graph types and iterators |
---|
| 160 | |
---|
[1026] | 161 | ///This \c \#define creates the same convenient type definitions as |
---|
| 162 | ///defined by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it |
---|
| 163 | ///creates \c RedNode, \c RedNodeIt, \c BoolRedNodeMap, |
---|
| 164 | ///\c IntRedNodeMap, \c DoubleRedNodeMap, \c BlueNode, \c BlueNodeIt, |
---|
| 165 | ///\c BoolBlueNodeMap, \c IntBlueNodeMap, \c DoubleBlueNodeMap. |
---|
[1019] | 166 | /// |
---|
| 167 | ///\note If the graph type is a dependent type, ie. the graph type depend |
---|
| 168 | ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS() |
---|
| 169 | ///macro. |
---|
| 170 | #define BPGRAPH_TYPEDEFS(BpGraph) \ |
---|
| 171 | GRAPH_TYPEDEFS(BpGraph); \ |
---|
| 172 | typedef BpGraph::RedNode RedNode; \ |
---|
[1026] | 173 | typedef BpGraph::RedNodeIt RedNodeIt; \ |
---|
| 174 | typedef BpGraph::RedNodeMap<bool> BoolRedNodeMap; \ |
---|
| 175 | typedef BpGraph::RedNodeMap<int> IntRedNodeMap; \ |
---|
| 176 | typedef BpGraph::RedNodeMap<double> DoubleRedNodeMap; \ |
---|
[1019] | 177 | typedef BpGraph::BlueNode BlueNode; \ |
---|
[1026] | 178 | typedef BpGraph::BlueNodeIt BlueNodeIt; \ |
---|
| 179 | typedef BpGraph::BlueNodeMap<bool> BoolBlueNodeMap; \ |
---|
| 180 | typedef BpGraph::BlueNodeMap<int> IntBlueNodeMap; \ |
---|
| 181 | typedef BpGraph::BlueNodeMap<double> DoubleBlueNodeMap |
---|
[1019] | 182 | |
---|
| 183 | ///Create convenience typedefs for the bipartite graph types and iterators |
---|
| 184 | |
---|
| 185 | ///\see BPGRAPH_TYPEDEFS |
---|
| 186 | /// |
---|
| 187 | ///\note Use this macro, if the graph type is a dependent type, |
---|
| 188 | ///ie. the graph type depend on a template parameter. |
---|
[1026] | 189 | #define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph) \ |
---|
| 190 | TEMPLATE_GRAPH_TYPEDEFS(BpGraph); \ |
---|
| 191 | typedef typename BpGraph::RedNode RedNode; \ |
---|
| 192 | typedef typename BpGraph::RedNodeIt RedNodeIt; \ |
---|
| 193 | typedef typename BpGraph::template RedNodeMap<bool> BoolRedNodeMap; \ |
---|
| 194 | typedef typename BpGraph::template RedNodeMap<int> IntRedNodeMap; \ |
---|
| 195 | typedef typename BpGraph::template RedNodeMap<double> DoubleRedNodeMap; \ |
---|
| 196 | typedef typename BpGraph::BlueNode BlueNode; \ |
---|
| 197 | typedef typename BpGraph::BlueNodeIt BlueNodeIt; \ |
---|
| 198 | typedef typename BpGraph::template BlueNodeMap<bool> BoolBlueNodeMap; \ |
---|
| 199 | typedef typename BpGraph::template BlueNodeMap<int> IntBlueNodeMap; \ |
---|
| 200 | typedef typename BpGraph::template BlueNodeMap<double> DoubleBlueNodeMap |
---|
[1019] | 201 | |
---|
[282] | 202 | /// \brief Function to count the items in a graph. |
---|
[220] | 203 | /// |
---|
[282] | 204 | /// This function counts the items (nodes, arcs etc.) in a graph. |
---|
| 205 | /// The complexity of the function is linear because |
---|
[220] | 206 | /// it iterates on all of the items. |
---|
| 207 | template <typename Graph, typename Item> |
---|
| 208 | inline int countItems(const Graph& g) { |
---|
| 209 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
---|
| 210 | int num = 0; |
---|
| 211 | for (ItemIt it(g); it != INVALID; ++it) { |
---|
| 212 | ++num; |
---|
| 213 | } |
---|
| 214 | return num; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | // Node counting: |
---|
| 218 | |
---|
| 219 | namespace _core_bits { |
---|
| 220 | |
---|
| 221 | template <typename Graph, typename Enable = void> |
---|
| 222 | struct CountNodesSelector { |
---|
| 223 | static int count(const Graph &g) { |
---|
| 224 | return countItems<Graph, typename Graph::Node>(g); |
---|
| 225 | } |
---|
| 226 | }; |
---|
| 227 | |
---|
| 228 | template <typename Graph> |
---|
| 229 | struct CountNodesSelector< |
---|
| 230 | Graph, typename |
---|
| 231 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
| 232 | { |
---|
| 233 | static int count(const Graph &g) { |
---|
| 234 | return g.nodeNum(); |
---|
| 235 | } |
---|
| 236 | }; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /// \brief Function to count the nodes in the graph. |
---|
| 240 | /// |
---|
| 241 | /// This function counts the nodes in the graph. |
---|
[282] | 242 | /// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
---|
| 243 | /// graph structures it is specialized to run in <em>O</em>(1). |
---|
[220] | 244 | /// |
---|
[282] | 245 | /// \note If the graph contains a \c nodeNum() member function and a |
---|
| 246 | /// \c NodeNumTag tag then this function calls directly the member |
---|
[220] | 247 | /// function to query the cardinality of the node set. |
---|
| 248 | template <typename Graph> |
---|
| 249 | inline int countNodes(const Graph& g) { |
---|
| 250 | return _core_bits::CountNodesSelector<Graph>::count(g); |
---|
| 251 | } |
---|
| 252 | |
---|
[1019] | 253 | namespace _graph_utils_bits { |
---|
[1092] | 254 | |
---|
[1019] | 255 | template <typename Graph, typename Enable = void> |
---|
| 256 | struct CountRedNodesSelector { |
---|
| 257 | static int count(const Graph &g) { |
---|
| 258 | return countItems<Graph, typename Graph::RedNode>(g); |
---|
| 259 | } |
---|
| 260 | }; |
---|
| 261 | |
---|
| 262 | template <typename Graph> |
---|
| 263 | struct CountRedNodesSelector< |
---|
[1092] | 264 | Graph, typename |
---|
| 265 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
[1019] | 266 | { |
---|
| 267 | static int count(const Graph &g) { |
---|
| 268 | return g.redNum(); |
---|
| 269 | } |
---|
[1092] | 270 | }; |
---|
[1019] | 271 | } |
---|
| 272 | |
---|
| 273 | /// \brief Function to count the red nodes in the graph. |
---|
| 274 | /// |
---|
| 275 | /// This function counts the red nodes in the graph. |
---|
| 276 | /// The complexity of the function is O(n) but for some |
---|
| 277 | /// graph structures it is specialized to run in O(1). |
---|
| 278 | /// |
---|
[1092] | 279 | /// If the graph contains a \e redNum() member function and a |
---|
[1019] | 280 | /// \e NodeNumTag tag then this function calls directly the member |
---|
| 281 | /// function to query the cardinality of the node set. |
---|
| 282 | template <typename Graph> |
---|
| 283 | inline int countRedNodes(const Graph& g) { |
---|
| 284 | return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | namespace _graph_utils_bits { |
---|
[1092] | 288 | |
---|
[1019] | 289 | template <typename Graph, typename Enable = void> |
---|
| 290 | struct CountBlueNodesSelector { |
---|
| 291 | static int count(const Graph &g) { |
---|
| 292 | return countItems<Graph, typename Graph::BlueNode>(g); |
---|
| 293 | } |
---|
| 294 | }; |
---|
| 295 | |
---|
| 296 | template <typename Graph> |
---|
| 297 | struct CountBlueNodesSelector< |
---|
[1092] | 298 | Graph, typename |
---|
| 299 | enable_if<typename Graph::NodeNumTag, void>::type> |
---|
[1019] | 300 | { |
---|
| 301 | static int count(const Graph &g) { |
---|
| 302 | return g.blueNum(); |
---|
| 303 | } |
---|
[1092] | 304 | }; |
---|
[1019] | 305 | } |
---|
| 306 | |
---|
| 307 | /// \brief Function to count the blue nodes in the graph. |
---|
| 308 | /// |
---|
| 309 | /// This function counts the blue nodes in the graph. |
---|
| 310 | /// The complexity of the function is O(n) but for some |
---|
| 311 | /// graph structures it is specialized to run in O(1). |
---|
| 312 | /// |
---|
[1092] | 313 | /// If the graph contains a \e blueNum() member function and a |
---|
[1019] | 314 | /// \e NodeNumTag tag then this function calls directly the member |
---|
| 315 | /// function to query the cardinality of the node set. |
---|
| 316 | template <typename Graph> |
---|
| 317 | inline int countBlueNodes(const Graph& g) { |
---|
| 318 | return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g); |
---|
| 319 | } |
---|
| 320 | |
---|
[220] | 321 | // Arc counting: |
---|
| 322 | |
---|
| 323 | namespace _core_bits { |
---|
| 324 | |
---|
| 325 | template <typename Graph, typename Enable = void> |
---|
| 326 | struct CountArcsSelector { |
---|
| 327 | static int count(const Graph &g) { |
---|
| 328 | return countItems<Graph, typename Graph::Arc>(g); |
---|
| 329 | } |
---|
| 330 | }; |
---|
| 331 | |
---|
| 332 | template <typename Graph> |
---|
| 333 | struct CountArcsSelector< |
---|
| 334 | Graph, |
---|
| 335 | typename enable_if<typename Graph::ArcNumTag, void>::type> |
---|
| 336 | { |
---|
| 337 | static int count(const Graph &g) { |
---|
| 338 | return g.arcNum(); |
---|
| 339 | } |
---|
| 340 | }; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /// \brief Function to count the arcs in the graph. |
---|
| 344 | /// |
---|
| 345 | /// This function counts the arcs in the graph. |
---|
[282] | 346 | /// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
---|
| 347 | /// graph structures it is specialized to run in <em>O</em>(1). |
---|
[220] | 348 | /// |
---|
[282] | 349 | /// \note If the graph contains a \c arcNum() member function and a |
---|
| 350 | /// \c ArcNumTag tag then this function calls directly the member |
---|
[220] | 351 | /// function to query the cardinality of the arc set. |
---|
| 352 | template <typename Graph> |
---|
| 353 | inline int countArcs(const Graph& g) { |
---|
| 354 | return _core_bits::CountArcsSelector<Graph>::count(g); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | // Edge counting: |
---|
[282] | 358 | |
---|
[220] | 359 | namespace _core_bits { |
---|
| 360 | |
---|
| 361 | template <typename Graph, typename Enable = void> |
---|
| 362 | struct CountEdgesSelector { |
---|
| 363 | static int count(const Graph &g) { |
---|
| 364 | return countItems<Graph, typename Graph::Edge>(g); |
---|
| 365 | } |
---|
| 366 | }; |
---|
| 367 | |
---|
| 368 | template <typename Graph> |
---|
| 369 | struct CountEdgesSelector< |
---|
| 370 | Graph, |
---|
| 371 | typename enable_if<typename Graph::EdgeNumTag, void>::type> |
---|
| 372 | { |
---|
| 373 | static int count(const Graph &g) { |
---|
| 374 | return g.edgeNum(); |
---|
| 375 | } |
---|
| 376 | }; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /// \brief Function to count the edges in the graph. |
---|
| 380 | /// |
---|
| 381 | /// This function counts the edges in the graph. |
---|
[282] | 382 | /// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
---|
| 383 | /// graph structures it is specialized to run in <em>O</em>(1). |
---|
[220] | 384 | /// |
---|
[282] | 385 | /// \note If the graph contains a \c edgeNum() member function and a |
---|
| 386 | /// \c EdgeNumTag tag then this function calls directly the member |
---|
[220] | 387 | /// function to query the cardinality of the edge set. |
---|
| 388 | template <typename Graph> |
---|
| 389 | inline int countEdges(const Graph& g) { |
---|
| 390 | return _core_bits::CountEdgesSelector<Graph>::count(g); |
---|
| 391 | |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | template <typename Graph, typename DegIt> |
---|
| 396 | inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 397 | int num = 0; |
---|
| 398 | for (DegIt it(_g, _n); it != INVALID; ++it) { |
---|
| 399 | ++num; |
---|
| 400 | } |
---|
| 401 | return num; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | /// \brief Function to count the number of the out-arcs from node \c n. |
---|
| 405 | /// |
---|
| 406 | /// This function counts the number of the out-arcs from node \c n |
---|
[282] | 407 | /// in the graph \c g. |
---|
[220] | 408 | template <typename Graph> |
---|
[282] | 409 | inline int countOutArcs(const Graph& g, const typename Graph::Node& n) { |
---|
| 410 | return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
---|
[220] | 411 | } |
---|
| 412 | |
---|
| 413 | /// \brief Function to count the number of the in-arcs to node \c n. |
---|
| 414 | /// |
---|
| 415 | /// This function counts the number of the in-arcs to node \c n |
---|
[282] | 416 | /// in the graph \c g. |
---|
[220] | 417 | template <typename Graph> |
---|
[282] | 418 | inline int countInArcs(const Graph& g, const typename Graph::Node& n) { |
---|
| 419 | return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
---|
[220] | 420 | } |
---|
| 421 | |
---|
| 422 | /// \brief Function to count the number of the inc-edges to node \c n. |
---|
| 423 | /// |
---|
| 424 | /// This function counts the number of the inc-edges to node \c n |
---|
[282] | 425 | /// in the undirected graph \c g. |
---|
[220] | 426 | template <typename Graph> |
---|
[282] | 427 | inline int countIncEdges(const Graph& g, const typename Graph::Node& n) { |
---|
| 428 | return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
---|
[220] | 429 | } |
---|
| 430 | |
---|
| 431 | namespace _core_bits { |
---|
| 432 | |
---|
| 433 | template <typename Digraph, typename Item, typename RefMap> |
---|
| 434 | class MapCopyBase { |
---|
| 435 | public: |
---|
| 436 | virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
---|
| 437 | |
---|
| 438 | virtual ~MapCopyBase() {} |
---|
| 439 | }; |
---|
| 440 | |
---|
| 441 | template <typename Digraph, typename Item, typename RefMap, |
---|
[282] | 442 | typename FromMap, typename ToMap> |
---|
[220] | 443 | class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
---|
| 444 | public: |
---|
| 445 | |
---|
[282] | 446 | MapCopy(const FromMap& map, ToMap& tmap) |
---|
| 447 | : _map(map), _tmap(tmap) {} |
---|
[220] | 448 | |
---|
| 449 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
---|
| 450 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
---|
| 451 | for (ItemIt it(digraph); it != INVALID; ++it) { |
---|
| 452 | _tmap.set(refMap[it], _map[it]); |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | private: |
---|
[282] | 457 | const FromMap& _map; |
---|
[220] | 458 | ToMap& _tmap; |
---|
| 459 | }; |
---|
| 460 | |
---|
| 461 | template <typename Digraph, typename Item, typename RefMap, typename It> |
---|
| 462 | class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
---|
| 463 | public: |
---|
| 464 | |
---|
[282] | 465 | ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
---|
[220] | 466 | |
---|
| 467 | virtual void copy(const Digraph&, const RefMap& refMap) { |
---|
| 468 | _it = refMap[_item]; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | private: |
---|
[282] | 472 | Item _item; |
---|
[220] | 473 | It& _it; |
---|
| 474 | }; |
---|
| 475 | |
---|
| 476 | template <typename Digraph, typename Item, typename RefMap, typename Ref> |
---|
| 477 | class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
---|
| 478 | public: |
---|
| 479 | |
---|
| 480 | RefCopy(Ref& map) : _map(map) {} |
---|
| 481 | |
---|
| 482 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
---|
| 483 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
---|
| 484 | for (ItemIt it(digraph); it != INVALID; ++it) { |
---|
| 485 | _map.set(it, refMap[it]); |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | private: |
---|
| 490 | Ref& _map; |
---|
| 491 | }; |
---|
| 492 | |
---|
| 493 | template <typename Digraph, typename Item, typename RefMap, |
---|
| 494 | typename CrossRef> |
---|
| 495 | class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
---|
| 496 | public: |
---|
| 497 | |
---|
| 498 | CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
---|
| 499 | |
---|
| 500 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
---|
| 501 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
---|
| 502 | for (ItemIt it(digraph); it != INVALID; ++it) { |
---|
| 503 | _cmap.set(refMap[it], it); |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | private: |
---|
| 508 | CrossRef& _cmap; |
---|
| 509 | }; |
---|
| 510 | |
---|
| 511 | template <typename Digraph, typename Enable = void> |
---|
| 512 | struct DigraphCopySelector { |
---|
| 513 | template <typename From, typename NodeRefMap, typename ArcRefMap> |
---|
[282] | 514 | static void copy(const From& from, Digraph &to, |
---|
[220] | 515 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
---|
[890] | 516 | to.clear(); |
---|
[220] | 517 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
---|
| 518 | nodeRefMap[it] = to.addNode(); |
---|
| 519 | } |
---|
| 520 | for (typename From::ArcIt it(from); it != INVALID; ++it) { |
---|
| 521 | arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
---|
| 522 | nodeRefMap[from.target(it)]); |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | }; |
---|
| 526 | |
---|
| 527 | template <typename Digraph> |
---|
| 528 | struct DigraphCopySelector< |
---|
| 529 | Digraph, |
---|
| 530 | typename enable_if<typename Digraph::BuildTag, void>::type> |
---|
| 531 | { |
---|
| 532 | template <typename From, typename NodeRefMap, typename ArcRefMap> |
---|
[282] | 533 | static void copy(const From& from, Digraph &to, |
---|
[220] | 534 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
---|
| 535 | to.build(from, nodeRefMap, arcRefMap); |
---|
| 536 | } |
---|
| 537 | }; |
---|
| 538 | |
---|
| 539 | template <typename Graph, typename Enable = void> |
---|
| 540 | struct GraphCopySelector { |
---|
| 541 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
---|
[282] | 542 | static void copy(const From& from, Graph &to, |
---|
[220] | 543 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
---|
[890] | 544 | to.clear(); |
---|
[220] | 545 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
---|
| 546 | nodeRefMap[it] = to.addNode(); |
---|
| 547 | } |
---|
| 548 | for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
---|
| 549 | edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
---|
| 550 | nodeRefMap[from.v(it)]); |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | }; |
---|
| 554 | |
---|
| 555 | template <typename Graph> |
---|
| 556 | struct GraphCopySelector< |
---|
| 557 | Graph, |
---|
| 558 | typename enable_if<typename Graph::BuildTag, void>::type> |
---|
| 559 | { |
---|
| 560 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
---|
[282] | 561 | static void copy(const From& from, Graph &to, |
---|
[1025] | 562 | NodeRefMap& nodeRefMap, |
---|
| 563 | EdgeRefMap& edgeRefMap) { |
---|
[220] | 564 | to.build(from, nodeRefMap, edgeRefMap); |
---|
| 565 | } |
---|
| 566 | }; |
---|
| 567 | |
---|
[1022] | 568 | template <typename BpGraph, typename Enable = void> |
---|
| 569 | struct BpGraphCopySelector { |
---|
[1025] | 570 | template <typename From, typename RedNodeRefMap, |
---|
| 571 | typename BlueNodeRefMap, typename EdgeRefMap> |
---|
[1022] | 572 | static void copy(const From& from, BpGraph &to, |
---|
[1025] | 573 | RedNodeRefMap& redNodeRefMap, |
---|
| 574 | BlueNodeRefMap& blueNodeRefMap, |
---|
| 575 | EdgeRefMap& edgeRefMap) { |
---|
[1022] | 576 | to.clear(); |
---|
[1026] | 577 | for (typename From::RedNodeIt it(from); it != INVALID; ++it) { |
---|
[1025] | 578 | redNodeRefMap[it] = to.addRedNode(); |
---|
[1022] | 579 | } |
---|
[1026] | 580 | for (typename From::BlueNodeIt it(from); it != INVALID; ++it) { |
---|
[1025] | 581 | blueNodeRefMap[it] = to.addBlueNode(); |
---|
[1022] | 582 | } |
---|
| 583 | for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
---|
[1025] | 584 | edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)], |
---|
| 585 | blueNodeRefMap[from.blueNode(it)]); |
---|
[1022] | 586 | } |
---|
| 587 | } |
---|
| 588 | }; |
---|
| 589 | |
---|
| 590 | template <typename BpGraph> |
---|
| 591 | struct BpGraphCopySelector< |
---|
| 592 | BpGraph, |
---|
| 593 | typename enable_if<typename BpGraph::BuildTag, void>::type> |
---|
| 594 | { |
---|
[1025] | 595 | template <typename From, typename RedNodeRefMap, |
---|
| 596 | typename BlueNodeRefMap, typename EdgeRefMap> |
---|
[1022] | 597 | static void copy(const From& from, BpGraph &to, |
---|
[1025] | 598 | RedNodeRefMap& redNodeRefMap, |
---|
| 599 | BlueNodeRefMap& blueNodeRefMap, |
---|
| 600 | EdgeRefMap& edgeRefMap) { |
---|
| 601 | to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap); |
---|
[1022] | 602 | } |
---|
| 603 | }; |
---|
| 604 | |
---|
[220] | 605 | } |
---|
| 606 | |
---|
[919] | 607 | /// \brief Check whether a graph is undirected. |
---|
[883] | 608 | /// |
---|
| 609 | /// This function returns \c true if the given graph is undirected. |
---|
| 610 | #ifdef DOXYGEN |
---|
| 611 | template <typename GR> |
---|
| 612 | bool undirected(const GR& g) { return false; } |
---|
| 613 | #else |
---|
| 614 | template <typename GR> |
---|
| 615 | typename enable_if<UndirectedTagIndicator<GR>, bool>::type |
---|
| 616 | undirected(const GR&) { |
---|
| 617 | return true; |
---|
| 618 | } |
---|
| 619 | template <typename GR> |
---|
| 620 | typename disable_if<UndirectedTagIndicator<GR>, bool>::type |
---|
| 621 | undirected(const GR&) { |
---|
| 622 | return false; |
---|
| 623 | } |
---|
| 624 | #endif |
---|
| 625 | |
---|
[220] | 626 | /// \brief Class to copy a digraph. |
---|
| 627 | /// |
---|
| 628 | /// Class to copy a digraph to another digraph (duplicate a digraph). The |
---|
[282] | 629 | /// simplest way of using it is through the \c digraphCopy() function. |
---|
[220] | 630 | /// |
---|
[282] | 631 | /// This class not only make a copy of a digraph, but it can create |
---|
[220] | 632 | /// references and cross references between the nodes and arcs of |
---|
[282] | 633 | /// the two digraphs, and it can copy maps to use with the newly created |
---|
| 634 | /// digraph. |
---|
[220] | 635 | /// |
---|
[282] | 636 | /// To make a copy from a digraph, first an instance of DigraphCopy |
---|
| 637 | /// should be created, then the data belongs to the digraph should |
---|
[220] | 638 | /// assigned to copy. In the end, the \c run() member should be |
---|
| 639 | /// called. |
---|
| 640 | /// |
---|
[282] | 641 | /// The next code copies a digraph with several data: |
---|
[220] | 642 | ///\code |
---|
[282] | 643 | /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
---|
| 644 | /// // Create references for the nodes |
---|
[220] | 645 | /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
---|
[282] | 646 | /// cg.nodeRef(nr); |
---|
| 647 | /// // Create cross references (inverse) for the arcs |
---|
[220] | 648 | /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
---|
[282] | 649 | /// cg.arcCrossRef(acr); |
---|
| 650 | /// // Copy an arc map |
---|
[220] | 651 | /// OrigGraph::ArcMap<double> oamap(orig_graph); |
---|
| 652 | /// NewGraph::ArcMap<double> namap(new_graph); |
---|
[282] | 653 | /// cg.arcMap(oamap, namap); |
---|
| 654 | /// // Copy a node |
---|
[220] | 655 | /// OrigGraph::Node on; |
---|
| 656 | /// NewGraph::Node nn; |
---|
[282] | 657 | /// cg.node(on, nn); |
---|
| 658 | /// // Execute copying |
---|
| 659 | /// cg.run(); |
---|
[220] | 660 | ///\endcode |
---|
[282] | 661 | template <typename From, typename To> |
---|
[220] | 662 | class DigraphCopy { |
---|
| 663 | private: |
---|
| 664 | |
---|
| 665 | typedef typename From::Node Node; |
---|
| 666 | typedef typename From::NodeIt NodeIt; |
---|
| 667 | typedef typename From::Arc Arc; |
---|
| 668 | typedef typename From::ArcIt ArcIt; |
---|
| 669 | |
---|
| 670 | typedef typename To::Node TNode; |
---|
| 671 | typedef typename To::Arc TArc; |
---|
| 672 | |
---|
| 673 | typedef typename From::template NodeMap<TNode> NodeRefMap; |
---|
| 674 | typedef typename From::template ArcMap<TArc> ArcRefMap; |
---|
| 675 | |
---|
| 676 | public: |
---|
| 677 | |
---|
[282] | 678 | /// \brief Constructor of DigraphCopy. |
---|
[220] | 679 | /// |
---|
[282] | 680 | /// Constructor of DigraphCopy for copying the content of the |
---|
| 681 | /// \c from digraph into the \c to digraph. |
---|
| 682 | DigraphCopy(const From& from, To& to) |
---|
[220] | 683 | : _from(from), _to(to) {} |
---|
| 684 | |
---|
[282] | 685 | /// \brief Destructor of DigraphCopy |
---|
[220] | 686 | /// |
---|
[282] | 687 | /// Destructor of DigraphCopy. |
---|
[220] | 688 | ~DigraphCopy() { |
---|
| 689 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 690 | delete _node_maps[i]; |
---|
| 691 | } |
---|
| 692 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 693 | delete _arc_maps[i]; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | } |
---|
| 697 | |
---|
[282] | 698 | /// \brief Copy the node references into the given map. |
---|
[220] | 699 | /// |
---|
[282] | 700 | /// This function copies the node references into the given map. |
---|
| 701 | /// The parameter should be a map, whose key type is the Node type of |
---|
| 702 | /// the source digraph, while the value type is the Node type of the |
---|
| 703 | /// destination digraph. |
---|
[220] | 704 | template <typename NodeRef> |
---|
| 705 | DigraphCopy& nodeRef(NodeRef& map) { |
---|
| 706 | _node_maps.push_back(new _core_bits::RefCopy<From, Node, |
---|
| 707 | NodeRefMap, NodeRef>(map)); |
---|
| 708 | return *this; |
---|
| 709 | } |
---|
| 710 | |
---|
[282] | 711 | /// \brief Copy the node cross references into the given map. |
---|
[220] | 712 | /// |
---|
[282] | 713 | /// This function copies the node cross references (reverse references) |
---|
| 714 | /// into the given map. The parameter should be a map, whose key type |
---|
| 715 | /// is the Node type of the destination digraph, while the value type is |
---|
| 716 | /// the Node type of the source digraph. |
---|
[220] | 717 | template <typename NodeCrossRef> |
---|
| 718 | DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
| 719 | _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
---|
| 720 | NodeRefMap, NodeCrossRef>(map)); |
---|
| 721 | return *this; |
---|
| 722 | } |
---|
| 723 | |
---|
[282] | 724 | /// \brief Make a copy of the given node map. |
---|
[220] | 725 | /// |
---|
[282] | 726 | /// This function makes a copy of the given node map for the newly |
---|
| 727 | /// created digraph. |
---|
| 728 | /// The key type of the new map \c tmap should be the Node type of the |
---|
| 729 | /// destination digraph, and the key type of the original map \c map |
---|
| 730 | /// should be the Node type of the source digraph. |
---|
| 731 | template <typename FromMap, typename ToMap> |
---|
| 732 | DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
---|
[220] | 733 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, |
---|
[282] | 734 | NodeRefMap, FromMap, ToMap>(map, tmap)); |
---|
[220] | 735 | return *this; |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | /// \brief Make a copy of the given node. |
---|
| 739 | /// |
---|
[282] | 740 | /// This function makes a copy of the given node. |
---|
| 741 | DigraphCopy& node(const Node& node, TNode& tnode) { |
---|
[220] | 742 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
---|
[282] | 743 | NodeRefMap, TNode>(node, tnode)); |
---|
[220] | 744 | return *this; |
---|
| 745 | } |
---|
| 746 | |
---|
[282] | 747 | /// \brief Copy the arc references into the given map. |
---|
[220] | 748 | /// |
---|
[282] | 749 | /// This function copies the arc references into the given map. |
---|
| 750 | /// The parameter should be a map, whose key type is the Arc type of |
---|
| 751 | /// the source digraph, while the value type is the Arc type of the |
---|
| 752 | /// destination digraph. |
---|
[220] | 753 | template <typename ArcRef> |
---|
| 754 | DigraphCopy& arcRef(ArcRef& map) { |
---|
| 755 | _arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
---|
| 756 | ArcRefMap, ArcRef>(map)); |
---|
| 757 | return *this; |
---|
| 758 | } |
---|
| 759 | |
---|
[282] | 760 | /// \brief Copy the arc cross references into the given map. |
---|
[220] | 761 | /// |
---|
[282] | 762 | /// This function copies the arc cross references (reverse references) |
---|
| 763 | /// into the given map. The parameter should be a map, whose key type |
---|
| 764 | /// is the Arc type of the destination digraph, while the value type is |
---|
| 765 | /// the Arc type of the source digraph. |
---|
[220] | 766 | template <typename ArcCrossRef> |
---|
| 767 | DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
---|
| 768 | _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
---|
| 769 | ArcRefMap, ArcCrossRef>(map)); |
---|
| 770 | return *this; |
---|
| 771 | } |
---|
| 772 | |
---|
[282] | 773 | /// \brief Make a copy of the given arc map. |
---|
[220] | 774 | /// |
---|
[282] | 775 | /// This function makes a copy of the given arc map for the newly |
---|
| 776 | /// created digraph. |
---|
| 777 | /// The key type of the new map \c tmap should be the Arc type of the |
---|
| 778 | /// destination digraph, and the key type of the original map \c map |
---|
| 779 | /// should be the Arc type of the source digraph. |
---|
| 780 | template <typename FromMap, typename ToMap> |
---|
| 781 | DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
---|
[220] | 782 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
---|
[282] | 783 | ArcRefMap, FromMap, ToMap>(map, tmap)); |
---|
[220] | 784 | return *this; |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | /// \brief Make a copy of the given arc. |
---|
| 788 | /// |
---|
[282] | 789 | /// This function makes a copy of the given arc. |
---|
| 790 | DigraphCopy& arc(const Arc& arc, TArc& tarc) { |
---|
[220] | 791 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
---|
[282] | 792 | ArcRefMap, TArc>(arc, tarc)); |
---|
[220] | 793 | return *this; |
---|
| 794 | } |
---|
| 795 | |
---|
[282] | 796 | /// \brief Execute copying. |
---|
[220] | 797 | /// |
---|
[282] | 798 | /// This function executes the copying of the digraph along with the |
---|
| 799 | /// copying of the assigned data. |
---|
[220] | 800 | void run() { |
---|
| 801 | NodeRefMap nodeRefMap(_from); |
---|
| 802 | ArcRefMap arcRefMap(_from); |
---|
| 803 | _core_bits::DigraphCopySelector<To>:: |
---|
[282] | 804 | copy(_from, _to, nodeRefMap, arcRefMap); |
---|
[220] | 805 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 806 | _node_maps[i]->copy(_from, nodeRefMap); |
---|
| 807 | } |
---|
| 808 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 809 | _arc_maps[i]->copy(_from, arcRefMap); |
---|
| 810 | } |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | protected: |
---|
| 814 | |
---|
| 815 | const From& _from; |
---|
| 816 | To& _to; |
---|
| 817 | |
---|
| 818 | std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
[282] | 819 | _node_maps; |
---|
[220] | 820 | |
---|
| 821 | std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
---|
[282] | 822 | _arc_maps; |
---|
[220] | 823 | |
---|
| 824 | }; |
---|
| 825 | |
---|
| 826 | /// \brief Copy a digraph to another digraph. |
---|
| 827 | /// |
---|
[282] | 828 | /// This function copies a digraph to another digraph. |
---|
| 829 | /// The complete usage of it is detailed in the DigraphCopy class, but |
---|
| 830 | /// a short example shows a basic work: |
---|
[220] | 831 | ///\code |
---|
[282] | 832 | /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
---|
[220] | 833 | ///\endcode |
---|
| 834 | /// |
---|
| 835 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 836 | /// nodes of the \c from digraph to the nodes of the \c to digraph and |
---|
[282] | 837 | /// \c acr will contain the mapping from the arcs of the \c to digraph |
---|
[220] | 838 | /// to the arcs of the \c from digraph. |
---|
| 839 | /// |
---|
| 840 | /// \see DigraphCopy |
---|
[282] | 841 | template <typename From, typename To> |
---|
| 842 | DigraphCopy<From, To> digraphCopy(const From& from, To& to) { |
---|
| 843 | return DigraphCopy<From, To>(from, to); |
---|
[220] | 844 | } |
---|
| 845 | |
---|
| 846 | /// \brief Class to copy a graph. |
---|
| 847 | /// |
---|
| 848 | /// Class to copy a graph to another graph (duplicate a graph). The |
---|
[282] | 849 | /// simplest way of using it is through the \c graphCopy() function. |
---|
[220] | 850 | /// |
---|
[282] | 851 | /// This class not only make a copy of a graph, but it can create |
---|
[220] | 852 | /// references and cross references between the nodes, edges and arcs of |
---|
[282] | 853 | /// the two graphs, and it can copy maps for using with the newly created |
---|
| 854 | /// graph. |
---|
[220] | 855 | /// |
---|
| 856 | /// To make a copy from a graph, first an instance of GraphCopy |
---|
| 857 | /// should be created, then the data belongs to the graph should |
---|
| 858 | /// assigned to copy. In the end, the \c run() member should be |
---|
| 859 | /// called. |
---|
| 860 | /// |
---|
| 861 | /// The next code copies a graph with several data: |
---|
| 862 | ///\code |
---|
[282] | 863 | /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
---|
| 864 | /// // Create references for the nodes |
---|
[220] | 865 | /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
---|
[282] | 866 | /// cg.nodeRef(nr); |
---|
| 867 | /// // Create cross references (inverse) for the edges |
---|
| 868 | /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
---|
| 869 | /// cg.edgeCrossRef(ecr); |
---|
| 870 | /// // Copy an edge map |
---|
| 871 | /// OrigGraph::EdgeMap<double> oemap(orig_graph); |
---|
| 872 | /// NewGraph::EdgeMap<double> nemap(new_graph); |
---|
| 873 | /// cg.edgeMap(oemap, nemap); |
---|
| 874 | /// // Copy a node |
---|
[220] | 875 | /// OrigGraph::Node on; |
---|
| 876 | /// NewGraph::Node nn; |
---|
[282] | 877 | /// cg.node(on, nn); |
---|
| 878 | /// // Execute copying |
---|
| 879 | /// cg.run(); |
---|
[220] | 880 | ///\endcode |
---|
[282] | 881 | template <typename From, typename To> |
---|
[220] | 882 | class GraphCopy { |
---|
| 883 | private: |
---|
| 884 | |
---|
| 885 | typedef typename From::Node Node; |
---|
| 886 | typedef typename From::NodeIt NodeIt; |
---|
| 887 | typedef typename From::Arc Arc; |
---|
| 888 | typedef typename From::ArcIt ArcIt; |
---|
| 889 | typedef typename From::Edge Edge; |
---|
| 890 | typedef typename From::EdgeIt EdgeIt; |
---|
| 891 | |
---|
| 892 | typedef typename To::Node TNode; |
---|
| 893 | typedef typename To::Arc TArc; |
---|
| 894 | typedef typename To::Edge TEdge; |
---|
| 895 | |
---|
| 896 | typedef typename From::template NodeMap<TNode> NodeRefMap; |
---|
| 897 | typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
---|
| 898 | |
---|
| 899 | struct ArcRefMap { |
---|
[282] | 900 | ArcRefMap(const From& from, const To& to, |
---|
[220] | 901 | const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
---|
[282] | 902 | : _from(from), _to(to), |
---|
[220] | 903 | _edge_ref(edge_ref), _node_ref(node_ref) {} |
---|
| 904 | |
---|
| 905 | typedef typename From::Arc Key; |
---|
| 906 | typedef typename To::Arc Value; |
---|
| 907 | |
---|
| 908 | Value operator[](const Key& key) const { |
---|
| 909 | bool forward = _from.u(key) != _from.v(key) ? |
---|
| 910 | _node_ref[_from.source(key)] == |
---|
| 911 | _to.source(_to.direct(_edge_ref[key], true)) : |
---|
| 912 | _from.direction(key); |
---|
| 913 | return _to.direct(_edge_ref[key], forward); |
---|
| 914 | } |
---|
| 915 | |
---|
[282] | 916 | const From& _from; |
---|
[220] | 917 | const To& _to; |
---|
| 918 | const EdgeRefMap& _edge_ref; |
---|
| 919 | const NodeRefMap& _node_ref; |
---|
| 920 | }; |
---|
| 921 | |
---|
| 922 | public: |
---|
| 923 | |
---|
[282] | 924 | /// \brief Constructor of GraphCopy. |
---|
[220] | 925 | /// |
---|
[282] | 926 | /// Constructor of GraphCopy for copying the content of the |
---|
| 927 | /// \c from graph into the \c to graph. |
---|
| 928 | GraphCopy(const From& from, To& to) |
---|
[220] | 929 | : _from(from), _to(to) {} |
---|
| 930 | |
---|
[282] | 931 | /// \brief Destructor of GraphCopy |
---|
[220] | 932 | /// |
---|
[282] | 933 | /// Destructor of GraphCopy. |
---|
[220] | 934 | ~GraphCopy() { |
---|
| 935 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 936 | delete _node_maps[i]; |
---|
| 937 | } |
---|
| 938 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 939 | delete _arc_maps[i]; |
---|
| 940 | } |
---|
| 941 | for (int i = 0; i < int(_edge_maps.size()); ++i) { |
---|
| 942 | delete _edge_maps[i]; |
---|
| 943 | } |
---|
| 944 | } |
---|
| 945 | |
---|
[282] | 946 | /// \brief Copy the node references into the given map. |
---|
[220] | 947 | /// |
---|
[282] | 948 | /// This function copies the node references into the given map. |
---|
| 949 | /// The parameter should be a map, whose key type is the Node type of |
---|
| 950 | /// the source graph, while the value type is the Node type of the |
---|
| 951 | /// destination graph. |
---|
[220] | 952 | template <typename NodeRef> |
---|
| 953 | GraphCopy& nodeRef(NodeRef& map) { |
---|
| 954 | _node_maps.push_back(new _core_bits::RefCopy<From, Node, |
---|
| 955 | NodeRefMap, NodeRef>(map)); |
---|
| 956 | return *this; |
---|
| 957 | } |
---|
| 958 | |
---|
[282] | 959 | /// \brief Copy the node cross references into the given map. |
---|
[220] | 960 | /// |
---|
[282] | 961 | /// This function copies the node cross references (reverse references) |
---|
| 962 | /// into the given map. The parameter should be a map, whose key type |
---|
| 963 | /// is the Node type of the destination graph, while the value type is |
---|
| 964 | /// the Node type of the source graph. |
---|
[220] | 965 | template <typename NodeCrossRef> |
---|
| 966 | GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
| 967 | _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
---|
| 968 | NodeRefMap, NodeCrossRef>(map)); |
---|
| 969 | return *this; |
---|
| 970 | } |
---|
| 971 | |
---|
[282] | 972 | /// \brief Make a copy of the given node map. |
---|
[220] | 973 | /// |
---|
[282] | 974 | /// This function makes a copy of the given node map for the newly |
---|
| 975 | /// created graph. |
---|
| 976 | /// The key type of the new map \c tmap should be the Node type of the |
---|
| 977 | /// destination graph, and the key type of the original map \c map |
---|
| 978 | /// should be the Node type of the source graph. |
---|
| 979 | template <typename FromMap, typename ToMap> |
---|
| 980 | GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
---|
[220] | 981 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, |
---|
[282] | 982 | NodeRefMap, FromMap, ToMap>(map, tmap)); |
---|
[220] | 983 | return *this; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | /// \brief Make a copy of the given node. |
---|
| 987 | /// |
---|
[282] | 988 | /// This function makes a copy of the given node. |
---|
| 989 | GraphCopy& node(const Node& node, TNode& tnode) { |
---|
[220] | 990 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
---|
[282] | 991 | NodeRefMap, TNode>(node, tnode)); |
---|
[220] | 992 | return *this; |
---|
| 993 | } |
---|
| 994 | |
---|
[282] | 995 | /// \brief Copy the arc references into the given map. |
---|
[220] | 996 | /// |
---|
[282] | 997 | /// This function copies the arc references into the given map. |
---|
| 998 | /// The parameter should be a map, whose key type is the Arc type of |
---|
| 999 | /// the source graph, while the value type is the Arc type of the |
---|
| 1000 | /// destination graph. |
---|
[220] | 1001 | template <typename ArcRef> |
---|
| 1002 | GraphCopy& arcRef(ArcRef& map) { |
---|
| 1003 | _arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
---|
| 1004 | ArcRefMap, ArcRef>(map)); |
---|
| 1005 | return *this; |
---|
| 1006 | } |
---|
| 1007 | |
---|
[282] | 1008 | /// \brief Copy the arc cross references into the given map. |
---|
[220] | 1009 | /// |
---|
[282] | 1010 | /// This function copies the arc cross references (reverse references) |
---|
| 1011 | /// into the given map. The parameter should be a map, whose key type |
---|
| 1012 | /// is the Arc type of the destination graph, while the value type is |
---|
| 1013 | /// the Arc type of the source graph. |
---|
[220] | 1014 | template <typename ArcCrossRef> |
---|
| 1015 | GraphCopy& arcCrossRef(ArcCrossRef& map) { |
---|
| 1016 | _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
---|
| 1017 | ArcRefMap, ArcCrossRef>(map)); |
---|
| 1018 | return *this; |
---|
| 1019 | } |
---|
| 1020 | |
---|
[282] | 1021 | /// \brief Make a copy of the given arc map. |
---|
[220] | 1022 | /// |
---|
[282] | 1023 | /// This function makes a copy of the given arc map for the newly |
---|
| 1024 | /// created graph. |
---|
| 1025 | /// The key type of the new map \c tmap should be the Arc type of the |
---|
| 1026 | /// destination graph, and the key type of the original map \c map |
---|
| 1027 | /// should be the Arc type of the source graph. |
---|
| 1028 | template <typename FromMap, typename ToMap> |
---|
| 1029 | GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
---|
[220] | 1030 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
---|
[282] | 1031 | ArcRefMap, FromMap, ToMap>(map, tmap)); |
---|
[220] | 1032 | return *this; |
---|
| 1033 | } |
---|
| 1034 | |
---|
| 1035 | /// \brief Make a copy of the given arc. |
---|
| 1036 | /// |
---|
[282] | 1037 | /// This function makes a copy of the given arc. |
---|
| 1038 | GraphCopy& arc(const Arc& arc, TArc& tarc) { |
---|
[220] | 1039 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
---|
[282] | 1040 | ArcRefMap, TArc>(arc, tarc)); |
---|
[220] | 1041 | return *this; |
---|
| 1042 | } |
---|
| 1043 | |
---|
[282] | 1044 | /// \brief Copy the edge references into the given map. |
---|
[220] | 1045 | /// |
---|
[282] | 1046 | /// This function copies the edge references into the given map. |
---|
| 1047 | /// The parameter should be a map, whose key type is the Edge type of |
---|
| 1048 | /// the source graph, while the value type is the Edge type of the |
---|
| 1049 | /// destination graph. |
---|
[220] | 1050 | template <typename EdgeRef> |
---|
| 1051 | GraphCopy& edgeRef(EdgeRef& map) { |
---|
| 1052 | _edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
---|
| 1053 | EdgeRefMap, EdgeRef>(map)); |
---|
| 1054 | return *this; |
---|
| 1055 | } |
---|
| 1056 | |
---|
[282] | 1057 | /// \brief Copy the edge cross references into the given map. |
---|
[220] | 1058 | /// |
---|
[282] | 1059 | /// This function copies the edge cross references (reverse references) |
---|
| 1060 | /// into the given map. The parameter should be a map, whose key type |
---|
| 1061 | /// is the Edge type of the destination graph, while the value type is |
---|
| 1062 | /// the Edge type of the source graph. |
---|
[220] | 1063 | template <typename EdgeCrossRef> |
---|
| 1064 | GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
---|
| 1065 | _edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
---|
| 1066 | Edge, EdgeRefMap, EdgeCrossRef>(map)); |
---|
| 1067 | return *this; |
---|
| 1068 | } |
---|
| 1069 | |
---|
[282] | 1070 | /// \brief Make a copy of the given edge map. |
---|
[220] | 1071 | /// |
---|
[282] | 1072 | /// This function makes a copy of the given edge map for the newly |
---|
| 1073 | /// created graph. |
---|
| 1074 | /// The key type of the new map \c tmap should be the Edge type of the |
---|
| 1075 | /// destination graph, and the key type of the original map \c map |
---|
| 1076 | /// should be the Edge type of the source graph. |
---|
| 1077 | template <typename FromMap, typename ToMap> |
---|
| 1078 | GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
---|
[220] | 1079 | _edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
---|
[282] | 1080 | EdgeRefMap, FromMap, ToMap>(map, tmap)); |
---|
[220] | 1081 | return *this; |
---|
| 1082 | } |
---|
| 1083 | |
---|
| 1084 | /// \brief Make a copy of the given edge. |
---|
| 1085 | /// |
---|
[282] | 1086 | /// This function makes a copy of the given edge. |
---|
| 1087 | GraphCopy& edge(const Edge& edge, TEdge& tedge) { |
---|
[220] | 1088 | _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
---|
[282] | 1089 | EdgeRefMap, TEdge>(edge, tedge)); |
---|
[220] | 1090 | return *this; |
---|
| 1091 | } |
---|
| 1092 | |
---|
[282] | 1093 | /// \brief Execute copying. |
---|
[220] | 1094 | /// |
---|
[282] | 1095 | /// This function executes the copying of the graph along with the |
---|
| 1096 | /// copying of the assigned data. |
---|
[220] | 1097 | void run() { |
---|
| 1098 | NodeRefMap nodeRefMap(_from); |
---|
| 1099 | EdgeRefMap edgeRefMap(_from); |
---|
[282] | 1100 | ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
---|
[220] | 1101 | _core_bits::GraphCopySelector<To>:: |
---|
[282] | 1102 | copy(_from, _to, nodeRefMap, edgeRefMap); |
---|
[220] | 1103 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 1104 | _node_maps[i]->copy(_from, nodeRefMap); |
---|
| 1105 | } |
---|
| 1106 | for (int i = 0; i < int(_edge_maps.size()); ++i) { |
---|
| 1107 | _edge_maps[i]->copy(_from, edgeRefMap); |
---|
| 1108 | } |
---|
| 1109 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 1110 | _arc_maps[i]->copy(_from, arcRefMap); |
---|
| 1111 | } |
---|
| 1112 | } |
---|
| 1113 | |
---|
| 1114 | private: |
---|
| 1115 | |
---|
| 1116 | const From& _from; |
---|
| 1117 | To& _to; |
---|
| 1118 | |
---|
| 1119 | std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
[282] | 1120 | _node_maps; |
---|
[220] | 1121 | |
---|
| 1122 | std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
---|
[282] | 1123 | _arc_maps; |
---|
[220] | 1124 | |
---|
| 1125 | std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
---|
[282] | 1126 | _edge_maps; |
---|
[220] | 1127 | |
---|
| 1128 | }; |
---|
| 1129 | |
---|
| 1130 | /// \brief Copy a graph to another graph. |
---|
| 1131 | /// |
---|
[282] | 1132 | /// This function copies a graph to another graph. |
---|
| 1133 | /// The complete usage of it is detailed in the GraphCopy class, |
---|
| 1134 | /// but a short example shows a basic work: |
---|
[220] | 1135 | ///\code |
---|
[282] | 1136 | /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
---|
[220] | 1137 | ///\endcode |
---|
| 1138 | /// |
---|
| 1139 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 1140 | /// nodes of the \c from graph to the nodes of the \c to graph and |
---|
[282] | 1141 | /// \c ecr will contain the mapping from the edges of the \c to graph |
---|
| 1142 | /// to the edges of the \c from graph. |
---|
[220] | 1143 | /// |
---|
| 1144 | /// \see GraphCopy |
---|
[282] | 1145 | template <typename From, typename To> |
---|
| 1146 | GraphCopy<From, To> |
---|
| 1147 | graphCopy(const From& from, To& to) { |
---|
| 1148 | return GraphCopy<From, To>(from, to); |
---|
[220] | 1149 | } |
---|
| 1150 | |
---|
[1022] | 1151 | /// \brief Class to copy a bipartite graph. |
---|
| 1152 | /// |
---|
| 1153 | /// Class to copy a bipartite graph to another graph (duplicate a |
---|
| 1154 | /// graph). The simplest way of using it is through the |
---|
| 1155 | /// \c bpGraphCopy() function. |
---|
| 1156 | /// |
---|
| 1157 | /// This class not only make a copy of a bipartite graph, but it can |
---|
| 1158 | /// create references and cross references between the nodes, edges |
---|
| 1159 | /// and arcs of the two graphs, and it can copy maps for using with |
---|
| 1160 | /// the newly created graph. |
---|
| 1161 | /// |
---|
| 1162 | /// To make a copy from a graph, first an instance of BpGraphCopy |
---|
| 1163 | /// should be created, then the data belongs to the graph should |
---|
| 1164 | /// assigned to copy. In the end, the \c run() member should be |
---|
| 1165 | /// called. |
---|
| 1166 | /// |
---|
| 1167 | /// The next code copies a graph with several data: |
---|
| 1168 | ///\code |
---|
| 1169 | /// BpGraphCopy<OrigBpGraph, NewBpGraph> cg(orig_graph, new_graph); |
---|
| 1170 | /// // Create references for the nodes |
---|
| 1171 | /// OrigBpGraph::NodeMap<NewBpGraph::Node> nr(orig_graph); |
---|
| 1172 | /// cg.nodeRef(nr); |
---|
| 1173 | /// // Create cross references (inverse) for the edges |
---|
| 1174 | /// NewBpGraph::EdgeMap<OrigBpGraph::Edge> ecr(new_graph); |
---|
| 1175 | /// cg.edgeCrossRef(ecr); |
---|
[1026] | 1176 | /// // Copy a red node map |
---|
| 1177 | /// OrigBpGraph::RedNodeMap<double> ormap(orig_graph); |
---|
| 1178 | /// NewBpGraph::RedNodeMap<double> nrmap(new_graph); |
---|
| 1179 | /// cg.redNodeMap(ormap, nrmap); |
---|
[1022] | 1180 | /// // Copy a node |
---|
| 1181 | /// OrigBpGraph::Node on; |
---|
| 1182 | /// NewBpGraph::Node nn; |
---|
| 1183 | /// cg.node(on, nn); |
---|
| 1184 | /// // Execute copying |
---|
| 1185 | /// cg.run(); |
---|
| 1186 | ///\endcode |
---|
| 1187 | template <typename From, typename To> |
---|
| 1188 | class BpGraphCopy { |
---|
| 1189 | private: |
---|
| 1190 | |
---|
| 1191 | typedef typename From::Node Node; |
---|
| 1192 | typedef typename From::RedNode RedNode; |
---|
| 1193 | typedef typename From::BlueNode BlueNode; |
---|
| 1194 | typedef typename From::NodeIt NodeIt; |
---|
| 1195 | typedef typename From::Arc Arc; |
---|
| 1196 | typedef typename From::ArcIt ArcIt; |
---|
| 1197 | typedef typename From::Edge Edge; |
---|
| 1198 | typedef typename From::EdgeIt EdgeIt; |
---|
| 1199 | |
---|
| 1200 | typedef typename To::Node TNode; |
---|
[1025] | 1201 | typedef typename To::RedNode TRedNode; |
---|
| 1202 | typedef typename To::BlueNode TBlueNode; |
---|
[1022] | 1203 | typedef typename To::Arc TArc; |
---|
| 1204 | typedef typename To::Edge TEdge; |
---|
| 1205 | |
---|
[1026] | 1206 | typedef typename From::template RedNodeMap<TRedNode> RedNodeRefMap; |
---|
| 1207 | typedef typename From::template BlueNodeMap<TBlueNode> BlueNodeRefMap; |
---|
[1022] | 1208 | typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
---|
| 1209 | |
---|
[1025] | 1210 | struct NodeRefMap { |
---|
| 1211 | NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref, |
---|
| 1212 | const BlueNodeRefMap& blue_node_ref) |
---|
| 1213 | : _from(from), _red_node_ref(red_node_ref), |
---|
| 1214 | _blue_node_ref(blue_node_ref) {} |
---|
| 1215 | |
---|
| 1216 | typedef typename From::Node Key; |
---|
| 1217 | typedef typename To::Node Value; |
---|
| 1218 | |
---|
| 1219 | Value operator[](const Key& key) const { |
---|
[1027] | 1220 | if (_from.red(key)) { |
---|
| 1221 | return _red_node_ref[_from.asRedNodeUnsafe(key)]; |
---|
[1025] | 1222 | } else { |
---|
[1027] | 1223 | return _blue_node_ref[_from.asBlueNodeUnsafe(key)]; |
---|
[1025] | 1224 | } |
---|
| 1225 | } |
---|
| 1226 | |
---|
| 1227 | const From& _from; |
---|
| 1228 | const RedNodeRefMap& _red_node_ref; |
---|
| 1229 | const BlueNodeRefMap& _blue_node_ref; |
---|
| 1230 | }; |
---|
| 1231 | |
---|
[1022] | 1232 | struct ArcRefMap { |
---|
| 1233 | ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref) |
---|
| 1234 | : _from(from), _to(to), _edge_ref(edge_ref) {} |
---|
| 1235 | |
---|
| 1236 | typedef typename From::Arc Key; |
---|
| 1237 | typedef typename To::Arc Value; |
---|
| 1238 | |
---|
| 1239 | Value operator[](const Key& key) const { |
---|
| 1240 | return _to.direct(_edge_ref[key], _from.direction(key)); |
---|
| 1241 | } |
---|
| 1242 | |
---|
| 1243 | const From& _from; |
---|
| 1244 | const To& _to; |
---|
| 1245 | const EdgeRefMap& _edge_ref; |
---|
| 1246 | }; |
---|
| 1247 | |
---|
| 1248 | public: |
---|
| 1249 | |
---|
| 1250 | /// \brief Constructor of BpGraphCopy. |
---|
| 1251 | /// |
---|
| 1252 | /// Constructor of BpGraphCopy for copying the content of the |
---|
| 1253 | /// \c from graph into the \c to graph. |
---|
| 1254 | BpGraphCopy(const From& from, To& to) |
---|
| 1255 | : _from(from), _to(to) {} |
---|
| 1256 | |
---|
| 1257 | /// \brief Destructor of BpGraphCopy |
---|
| 1258 | /// |
---|
| 1259 | /// Destructor of BpGraphCopy. |
---|
| 1260 | ~BpGraphCopy() { |
---|
| 1261 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 1262 | delete _node_maps[i]; |
---|
| 1263 | } |
---|
| 1264 | for (int i = 0; i < int(_red_maps.size()); ++i) { |
---|
| 1265 | delete _red_maps[i]; |
---|
| 1266 | } |
---|
| 1267 | for (int i = 0; i < int(_blue_maps.size()); ++i) { |
---|
| 1268 | delete _blue_maps[i]; |
---|
| 1269 | } |
---|
| 1270 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 1271 | delete _arc_maps[i]; |
---|
| 1272 | } |
---|
| 1273 | for (int i = 0; i < int(_edge_maps.size()); ++i) { |
---|
| 1274 | delete _edge_maps[i]; |
---|
| 1275 | } |
---|
| 1276 | } |
---|
| 1277 | |
---|
| 1278 | /// \brief Copy the node references into the given map. |
---|
| 1279 | /// |
---|
| 1280 | /// This function copies the node references into the given map. |
---|
| 1281 | /// The parameter should be a map, whose key type is the Node type of |
---|
| 1282 | /// the source graph, while the value type is the Node type of the |
---|
| 1283 | /// destination graph. |
---|
| 1284 | template <typename NodeRef> |
---|
| 1285 | BpGraphCopy& nodeRef(NodeRef& map) { |
---|
| 1286 | _node_maps.push_back(new _core_bits::RefCopy<From, Node, |
---|
| 1287 | NodeRefMap, NodeRef>(map)); |
---|
| 1288 | return *this; |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | /// \brief Copy the node cross references into the given map. |
---|
| 1292 | /// |
---|
| 1293 | /// This function copies the node cross references (reverse references) |
---|
| 1294 | /// into the given map. The parameter should be a map, whose key type |
---|
| 1295 | /// is the Node type of the destination graph, while the value type is |
---|
| 1296 | /// the Node type of the source graph. |
---|
| 1297 | template <typename NodeCrossRef> |
---|
| 1298 | BpGraphCopy& nodeCrossRef(NodeCrossRef& map) { |
---|
| 1299 | _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
---|
| 1300 | NodeRefMap, NodeCrossRef>(map)); |
---|
| 1301 | return *this; |
---|
| 1302 | } |
---|
| 1303 | |
---|
| 1304 | /// \brief Make a copy of the given node map. |
---|
| 1305 | /// |
---|
| 1306 | /// This function makes a copy of the given node map for the newly |
---|
| 1307 | /// created graph. |
---|
| 1308 | /// The key type of the new map \c tmap should be the Node type of the |
---|
| 1309 | /// destination graph, and the key type of the original map \c map |
---|
| 1310 | /// should be the Node type of the source graph. |
---|
| 1311 | template <typename FromMap, typename ToMap> |
---|
| 1312 | BpGraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
---|
| 1313 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, |
---|
| 1314 | NodeRefMap, FromMap, ToMap>(map, tmap)); |
---|
| 1315 | return *this; |
---|
| 1316 | } |
---|
| 1317 | |
---|
| 1318 | /// \brief Make a copy of the given node. |
---|
| 1319 | /// |
---|
| 1320 | /// This function makes a copy of the given node. |
---|
| 1321 | BpGraphCopy& node(const Node& node, TNode& tnode) { |
---|
| 1322 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
---|
| 1323 | NodeRefMap, TNode>(node, tnode)); |
---|
| 1324 | return *this; |
---|
| 1325 | } |
---|
| 1326 | |
---|
| 1327 | /// \brief Copy the red node references into the given map. |
---|
| 1328 | /// |
---|
| 1329 | /// This function copies the red node references into the given |
---|
| 1330 | /// map. The parameter should be a map, whose key type is the |
---|
| 1331 | /// Node type of the source graph with the red item set, while the |
---|
| 1332 | /// value type is the Node type of the destination graph. |
---|
| 1333 | template <typename RedRef> |
---|
| 1334 | BpGraphCopy& redRef(RedRef& map) { |
---|
| 1335 | _red_maps.push_back(new _core_bits::RefCopy<From, RedNode, |
---|
[1025] | 1336 | RedNodeRefMap, RedRef>(map)); |
---|
[1022] | 1337 | return *this; |
---|
| 1338 | } |
---|
| 1339 | |
---|
| 1340 | /// \brief Copy the red node cross references into the given map. |
---|
| 1341 | /// |
---|
| 1342 | /// This function copies the red node cross references (reverse |
---|
| 1343 | /// references) into the given map. The parameter should be a map, |
---|
| 1344 | /// whose key type is the Node type of the destination graph with |
---|
| 1345 | /// the red item set, while the value type is the Node type of the |
---|
| 1346 | /// source graph. |
---|
| 1347 | template <typename RedCrossRef> |
---|
| 1348 | BpGraphCopy& redCrossRef(RedCrossRef& map) { |
---|
| 1349 | _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode, |
---|
[1025] | 1350 | RedNodeRefMap, RedCrossRef>(map)); |
---|
[1022] | 1351 | return *this; |
---|
| 1352 | } |
---|
| 1353 | |
---|
| 1354 | /// \brief Make a copy of the given red node map. |
---|
| 1355 | /// |
---|
| 1356 | /// This function makes a copy of the given red node map for the newly |
---|
| 1357 | /// created graph. |
---|
| 1358 | /// The key type of the new map \c tmap should be the Node type of |
---|
| 1359 | /// the destination graph with the red items, and the key type of |
---|
| 1360 | /// the original map \c map should be the Node type of the source |
---|
| 1361 | /// graph. |
---|
| 1362 | template <typename FromMap, typename ToMap> |
---|
[1026] | 1363 | BpGraphCopy& redNodeMap(const FromMap& map, ToMap& tmap) { |
---|
[1022] | 1364 | _red_maps.push_back(new _core_bits::MapCopy<From, RedNode, |
---|
[1025] | 1365 | RedNodeRefMap, FromMap, ToMap>(map, tmap)); |
---|
| 1366 | return *this; |
---|
| 1367 | } |
---|
| 1368 | |
---|
| 1369 | /// \brief Make a copy of the given red node. |
---|
| 1370 | /// |
---|
| 1371 | /// This function makes a copy of the given red node. |
---|
| 1372 | BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) { |
---|
| 1373 | _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode, |
---|
| 1374 | RedNodeRefMap, TRedNode>(node, tnode)); |
---|
[1022] | 1375 | return *this; |
---|
| 1376 | } |
---|
| 1377 | |
---|
| 1378 | /// \brief Copy the blue node references into the given map. |
---|
| 1379 | /// |
---|
| 1380 | /// This function copies the blue node references into the given |
---|
| 1381 | /// map. The parameter should be a map, whose key type is the |
---|
| 1382 | /// Node type of the source graph with the blue item set, while the |
---|
| 1383 | /// value type is the Node type of the destination graph. |
---|
| 1384 | template <typename BlueRef> |
---|
| 1385 | BpGraphCopy& blueRef(BlueRef& map) { |
---|
| 1386 | _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode, |
---|
[1025] | 1387 | BlueNodeRefMap, BlueRef>(map)); |
---|
[1022] | 1388 | return *this; |
---|
| 1389 | } |
---|
| 1390 | |
---|
| 1391 | /// \brief Copy the blue node cross references into the given map. |
---|
| 1392 | /// |
---|
| 1393 | /// This function copies the blue node cross references (reverse |
---|
| 1394 | /// references) into the given map. The parameter should be a map, |
---|
| 1395 | /// whose key type is the Node type of the destination graph with |
---|
| 1396 | /// the blue item set, while the value type is the Node type of the |
---|
| 1397 | /// source graph. |
---|
| 1398 | template <typename BlueCrossRef> |
---|
| 1399 | BpGraphCopy& blueCrossRef(BlueCrossRef& map) { |
---|
| 1400 | _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode, |
---|
[1025] | 1401 | BlueNodeRefMap, BlueCrossRef>(map)); |
---|
[1022] | 1402 | return *this; |
---|
| 1403 | } |
---|
| 1404 | |
---|
| 1405 | /// \brief Make a copy of the given blue node map. |
---|
| 1406 | /// |
---|
| 1407 | /// This function makes a copy of the given blue node map for the newly |
---|
| 1408 | /// created graph. |
---|
| 1409 | /// The key type of the new map \c tmap should be the Node type of |
---|
| 1410 | /// the destination graph with the blue items, and the key type of |
---|
| 1411 | /// the original map \c map should be the Node type of the source |
---|
| 1412 | /// graph. |
---|
| 1413 | template <typename FromMap, typename ToMap> |
---|
[1026] | 1414 | BpGraphCopy& blueNodeMap(const FromMap& map, ToMap& tmap) { |
---|
[1022] | 1415 | _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode, |
---|
[1025] | 1416 | BlueNodeRefMap, FromMap, ToMap>(map, tmap)); |
---|
| 1417 | return *this; |
---|
| 1418 | } |
---|
| 1419 | |
---|
| 1420 | /// \brief Make a copy of the given blue node. |
---|
| 1421 | /// |
---|
| 1422 | /// This function makes a copy of the given blue node. |
---|
| 1423 | BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) { |
---|
| 1424 | _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode, |
---|
| 1425 | BlueNodeRefMap, TBlueNode>(node, tnode)); |
---|
[1022] | 1426 | return *this; |
---|
| 1427 | } |
---|
| 1428 | |
---|
| 1429 | /// \brief Copy the arc references into the given map. |
---|
| 1430 | /// |
---|
| 1431 | /// This function copies the arc references into the given map. |
---|
| 1432 | /// The parameter should be a map, whose key type is the Arc type of |
---|
| 1433 | /// the source graph, while the value type is the Arc type of the |
---|
| 1434 | /// destination graph. |
---|
| 1435 | template <typename ArcRef> |
---|
| 1436 | BpGraphCopy& arcRef(ArcRef& map) { |
---|
| 1437 | _arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
---|
| 1438 | ArcRefMap, ArcRef>(map)); |
---|
| 1439 | return *this; |
---|
| 1440 | } |
---|
| 1441 | |
---|
| 1442 | /// \brief Copy the arc cross references into the given map. |
---|
| 1443 | /// |
---|
| 1444 | /// This function copies the arc cross references (reverse references) |
---|
| 1445 | /// into the given map. The parameter should be a map, whose key type |
---|
| 1446 | /// is the Arc type of the destination graph, while the value type is |
---|
| 1447 | /// the Arc type of the source graph. |
---|
| 1448 | template <typename ArcCrossRef> |
---|
| 1449 | BpGraphCopy& arcCrossRef(ArcCrossRef& map) { |
---|
| 1450 | _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
---|
| 1451 | ArcRefMap, ArcCrossRef>(map)); |
---|
| 1452 | return *this; |
---|
| 1453 | } |
---|
| 1454 | |
---|
| 1455 | /// \brief Make a copy of the given arc map. |
---|
| 1456 | /// |
---|
| 1457 | /// This function makes a copy of the given arc map for the newly |
---|
| 1458 | /// created graph. |
---|
| 1459 | /// The key type of the new map \c tmap should be the Arc type of the |
---|
| 1460 | /// destination graph, and the key type of the original map \c map |
---|
| 1461 | /// should be the Arc type of the source graph. |
---|
| 1462 | template <typename FromMap, typename ToMap> |
---|
| 1463 | BpGraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
---|
| 1464 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
---|
| 1465 | ArcRefMap, FromMap, ToMap>(map, tmap)); |
---|
| 1466 | return *this; |
---|
| 1467 | } |
---|
| 1468 | |
---|
| 1469 | /// \brief Make a copy of the given arc. |
---|
| 1470 | /// |
---|
| 1471 | /// This function makes a copy of the given arc. |
---|
| 1472 | BpGraphCopy& arc(const Arc& arc, TArc& tarc) { |
---|
| 1473 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
---|
| 1474 | ArcRefMap, TArc>(arc, tarc)); |
---|
| 1475 | return *this; |
---|
| 1476 | } |
---|
| 1477 | |
---|
| 1478 | /// \brief Copy the edge references into the given map. |
---|
| 1479 | /// |
---|
| 1480 | /// This function copies the edge references into the given map. |
---|
| 1481 | /// The parameter should be a map, whose key type is the Edge type of |
---|
| 1482 | /// the source graph, while the value type is the Edge type of the |
---|
| 1483 | /// destination graph. |
---|
| 1484 | template <typename EdgeRef> |
---|
| 1485 | BpGraphCopy& edgeRef(EdgeRef& map) { |
---|
| 1486 | _edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
---|
| 1487 | EdgeRefMap, EdgeRef>(map)); |
---|
| 1488 | return *this; |
---|
| 1489 | } |
---|
| 1490 | |
---|
| 1491 | /// \brief Copy the edge cross references into the given map. |
---|
| 1492 | /// |
---|
| 1493 | /// This function copies the edge cross references (reverse references) |
---|
| 1494 | /// into the given map. The parameter should be a map, whose key type |
---|
| 1495 | /// is the Edge type of the destination graph, while the value type is |
---|
| 1496 | /// the Edge type of the source graph. |
---|
| 1497 | template <typename EdgeCrossRef> |
---|
| 1498 | BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
---|
| 1499 | _edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
---|
| 1500 | Edge, EdgeRefMap, EdgeCrossRef>(map)); |
---|
| 1501 | return *this; |
---|
| 1502 | } |
---|
| 1503 | |
---|
| 1504 | /// \brief Make a copy of the given edge map. |
---|
| 1505 | /// |
---|
| 1506 | /// This function makes a copy of the given edge map for the newly |
---|
| 1507 | /// created graph. |
---|
| 1508 | /// The key type of the new map \c tmap should be the Edge type of the |
---|
| 1509 | /// destination graph, and the key type of the original map \c map |
---|
| 1510 | /// should be the Edge type of the source graph. |
---|
| 1511 | template <typename FromMap, typename ToMap> |
---|
| 1512 | BpGraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
---|
| 1513 | _edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
---|
| 1514 | EdgeRefMap, FromMap, ToMap>(map, tmap)); |
---|
| 1515 | return *this; |
---|
| 1516 | } |
---|
| 1517 | |
---|
| 1518 | /// \brief Make a copy of the given edge. |
---|
| 1519 | /// |
---|
| 1520 | /// This function makes a copy of the given edge. |
---|
| 1521 | BpGraphCopy& edge(const Edge& edge, TEdge& tedge) { |
---|
| 1522 | _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
---|
| 1523 | EdgeRefMap, TEdge>(edge, tedge)); |
---|
| 1524 | return *this; |
---|
| 1525 | } |
---|
| 1526 | |
---|
| 1527 | /// \brief Execute copying. |
---|
| 1528 | /// |
---|
| 1529 | /// This function executes the copying of the graph along with the |
---|
| 1530 | /// copying of the assigned data. |
---|
| 1531 | void run() { |
---|
[1025] | 1532 | RedNodeRefMap redNodeRefMap(_from); |
---|
| 1533 | BlueNodeRefMap blueNodeRefMap(_from); |
---|
| 1534 | NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap); |
---|
[1022] | 1535 | EdgeRefMap edgeRefMap(_from); |
---|
| 1536 | ArcRefMap arcRefMap(_from, _to, edgeRefMap); |
---|
| 1537 | _core_bits::BpGraphCopySelector<To>:: |
---|
[1025] | 1538 | copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap); |
---|
[1022] | 1539 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
---|
| 1540 | _node_maps[i]->copy(_from, nodeRefMap); |
---|
| 1541 | } |
---|
| 1542 | for (int i = 0; i < int(_red_maps.size()); ++i) { |
---|
[1025] | 1543 | _red_maps[i]->copy(_from, redNodeRefMap); |
---|
[1022] | 1544 | } |
---|
| 1545 | for (int i = 0; i < int(_blue_maps.size()); ++i) { |
---|
[1025] | 1546 | _blue_maps[i]->copy(_from, blueNodeRefMap); |
---|
[1022] | 1547 | } |
---|
| 1548 | for (int i = 0; i < int(_edge_maps.size()); ++i) { |
---|
| 1549 | _edge_maps[i]->copy(_from, edgeRefMap); |
---|
| 1550 | } |
---|
| 1551 | for (int i = 0; i < int(_arc_maps.size()); ++i) { |
---|
| 1552 | _arc_maps[i]->copy(_from, arcRefMap); |
---|
| 1553 | } |
---|
| 1554 | } |
---|
| 1555 | |
---|
| 1556 | private: |
---|
| 1557 | |
---|
| 1558 | const From& _from; |
---|
| 1559 | To& _to; |
---|
| 1560 | |
---|
| 1561 | std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
---|
| 1562 | _node_maps; |
---|
| 1563 | |
---|
[1025] | 1564 | std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* > |
---|
[1022] | 1565 | _red_maps; |
---|
| 1566 | |
---|
[1025] | 1567 | std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* > |
---|
[1022] | 1568 | _blue_maps; |
---|
| 1569 | |
---|
| 1570 | std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
---|
| 1571 | _arc_maps; |
---|
| 1572 | |
---|
| 1573 | std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
---|
| 1574 | _edge_maps; |
---|
| 1575 | |
---|
| 1576 | }; |
---|
| 1577 | |
---|
| 1578 | /// \brief Copy a graph to another graph. |
---|
| 1579 | /// |
---|
| 1580 | /// This function copies a graph to another graph. |
---|
| 1581 | /// The complete usage of it is detailed in the BpGraphCopy class, |
---|
| 1582 | /// but a short example shows a basic work: |
---|
| 1583 | ///\code |
---|
| 1584 | /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
---|
| 1585 | ///\endcode |
---|
| 1586 | /// |
---|
| 1587 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 1588 | /// nodes of the \c from graph to the nodes of the \c to graph and |
---|
| 1589 | /// \c ecr will contain the mapping from the edges of the \c to graph |
---|
| 1590 | /// to the edges of the \c from graph. |
---|
| 1591 | /// |
---|
| 1592 | /// \see BpGraphCopy |
---|
| 1593 | template <typename From, typename To> |
---|
| 1594 | BpGraphCopy<From, To> |
---|
| 1595 | bpGraphCopy(const From& from, To& to) { |
---|
| 1596 | return BpGraphCopy<From, To>(from, to); |
---|
| 1597 | } |
---|
| 1598 | |
---|
[220] | 1599 | namespace _core_bits { |
---|
| 1600 | |
---|
| 1601 | template <typename Graph, typename Enable = void> |
---|
| 1602 | struct FindArcSelector { |
---|
| 1603 | typedef typename Graph::Node Node; |
---|
| 1604 | typedef typename Graph::Arc Arc; |
---|
| 1605 | static Arc find(const Graph &g, Node u, Node v, Arc e) { |
---|
| 1606 | if (e == INVALID) { |
---|
| 1607 | g.firstOut(e, u); |
---|
| 1608 | } else { |
---|
| 1609 | g.nextOut(e); |
---|
| 1610 | } |
---|
| 1611 | while (e != INVALID && g.target(e) != v) { |
---|
| 1612 | g.nextOut(e); |
---|
| 1613 | } |
---|
| 1614 | return e; |
---|
| 1615 | } |
---|
| 1616 | }; |
---|
| 1617 | |
---|
| 1618 | template <typename Graph> |
---|
| 1619 | struct FindArcSelector< |
---|
| 1620 | Graph, |
---|
[282] | 1621 | typename enable_if<typename Graph::FindArcTag, void>::type> |
---|
[220] | 1622 | { |
---|
| 1623 | typedef typename Graph::Node Node; |
---|
| 1624 | typedef typename Graph::Arc Arc; |
---|
| 1625 | static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
---|
| 1626 | return g.findArc(u, v, prev); |
---|
| 1627 | } |
---|
| 1628 | }; |
---|
| 1629 | } |
---|
| 1630 | |
---|
[282] | 1631 | /// \brief Find an arc between two nodes of a digraph. |
---|
[220] | 1632 | /// |
---|
[282] | 1633 | /// This function finds an arc from node \c u to node \c v in the |
---|
| 1634 | /// digraph \c g. |
---|
[220] | 1635 | /// |
---|
| 1636 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 1637 | /// it finds the first arc from \c u to \c v. Otherwise it looks for |
---|
| 1638 | /// the next arc from \c u to \c v after \c prev. |
---|
| 1639 | /// \return The found arc or \ref INVALID if there is no such an arc. |
---|
| 1640 | /// |
---|
| 1641 | /// Thus you can iterate through each arc from \c u to \c v as it follows. |
---|
| 1642 | ///\code |
---|
[282] | 1643 | /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
---|
[220] | 1644 | /// ... |
---|
| 1645 | /// } |
---|
| 1646 | ///\endcode |
---|
| 1647 | /// |
---|
[282] | 1648 | /// \note \ref ConArcIt provides iterator interface for the same |
---|
| 1649 | /// functionality. |
---|
| 1650 | /// |
---|
[220] | 1651 | ///\sa ConArcIt |
---|
[282] | 1652 | ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
---|
[220] | 1653 | template <typename Graph> |
---|
| 1654 | inline typename Graph::Arc |
---|
| 1655 | findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
---|
| 1656 | typename Graph::Arc prev = INVALID) { |
---|
| 1657 | return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
---|
| 1658 | } |
---|
| 1659 | |
---|
[282] | 1660 | /// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
---|
[220] | 1661 | /// |
---|
[282] | 1662 | /// Iterator for iterating on parallel arcs connecting the same nodes. It is |
---|
| 1663 | /// a higher level interface for the \ref findArc() function. You can |
---|
[220] | 1664 | /// use it the following way: |
---|
| 1665 | ///\code |
---|
| 1666 | /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
---|
| 1667 | /// ... |
---|
| 1668 | /// } |
---|
| 1669 | ///\endcode |
---|
| 1670 | /// |
---|
| 1671 | ///\sa findArc() |
---|
[282] | 1672 | ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
---|
[559] | 1673 | template <typename GR> |
---|
| 1674 | class ConArcIt : public GR::Arc { |
---|
[617] | 1675 | typedef typename GR::Arc Parent; |
---|
| 1676 | |
---|
[220] | 1677 | public: |
---|
| 1678 | |
---|
[617] | 1679 | typedef typename GR::Arc Arc; |
---|
| 1680 | typedef typename GR::Node Node; |
---|
[220] | 1681 | |
---|
| 1682 | /// \brief Constructor. |
---|
| 1683 | /// |
---|
[282] | 1684 | /// Construct a new ConArcIt iterating on the arcs that |
---|
| 1685 | /// connects nodes \c u and \c v. |
---|
[617] | 1686 | ConArcIt(const GR& g, Node u, Node v) : _graph(g) { |
---|
[220] | 1687 | Parent::operator=(findArc(_graph, u, v)); |
---|
| 1688 | } |
---|
| 1689 | |
---|
| 1690 | /// \brief Constructor. |
---|
| 1691 | /// |
---|
[282] | 1692 | /// Construct a new ConArcIt that continues the iterating from arc \c a. |
---|
[617] | 1693 | ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {} |
---|
[220] | 1694 | |
---|
| 1695 | /// \brief Increment operator. |
---|
| 1696 | /// |
---|
| 1697 | /// It increments the iterator and gives back the next arc. |
---|
| 1698 | ConArcIt& operator++() { |
---|
| 1699 | Parent::operator=(findArc(_graph, _graph.source(*this), |
---|
| 1700 | _graph.target(*this), *this)); |
---|
| 1701 | return *this; |
---|
| 1702 | } |
---|
| 1703 | private: |
---|
[617] | 1704 | const GR& _graph; |
---|
[220] | 1705 | }; |
---|
| 1706 | |
---|
| 1707 | namespace _core_bits { |
---|
| 1708 | |
---|
| 1709 | template <typename Graph, typename Enable = void> |
---|
| 1710 | struct FindEdgeSelector { |
---|
| 1711 | typedef typename Graph::Node Node; |
---|
| 1712 | typedef typename Graph::Edge Edge; |
---|
| 1713 | static Edge find(const Graph &g, Node u, Node v, Edge e) { |
---|
| 1714 | bool b; |
---|
| 1715 | if (u != v) { |
---|
| 1716 | if (e == INVALID) { |
---|
| 1717 | g.firstInc(e, b, u); |
---|
| 1718 | } else { |
---|
| 1719 | b = g.u(e) == u; |
---|
| 1720 | g.nextInc(e, b); |
---|
| 1721 | } |
---|
| 1722 | while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
---|
| 1723 | g.nextInc(e, b); |
---|
| 1724 | } |
---|
| 1725 | } else { |
---|
| 1726 | if (e == INVALID) { |
---|
| 1727 | g.firstInc(e, b, u); |
---|
| 1728 | } else { |
---|
| 1729 | b = true; |
---|
| 1730 | g.nextInc(e, b); |
---|
| 1731 | } |
---|
| 1732 | while (e != INVALID && (!b || g.v(e) != v)) { |
---|
| 1733 | g.nextInc(e, b); |
---|
| 1734 | } |
---|
| 1735 | } |
---|
| 1736 | return e; |
---|
| 1737 | } |
---|
| 1738 | }; |
---|
| 1739 | |
---|
| 1740 | template <typename Graph> |
---|
| 1741 | struct FindEdgeSelector< |
---|
| 1742 | Graph, |
---|
| 1743 | typename enable_if<typename Graph::FindEdgeTag, void>::type> |
---|
| 1744 | { |
---|
| 1745 | typedef typename Graph::Node Node; |
---|
| 1746 | typedef typename Graph::Edge Edge; |
---|
| 1747 | static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
---|
| 1748 | return g.findEdge(u, v, prev); |
---|
| 1749 | } |
---|
| 1750 | }; |
---|
| 1751 | } |
---|
| 1752 | |
---|
[282] | 1753 | /// \brief Find an edge between two nodes of a graph. |
---|
[220] | 1754 | /// |
---|
[282] | 1755 | /// This function finds an edge from node \c u to node \c v in graph \c g. |
---|
| 1756 | /// If node \c u and node \c v is equal then each loop edge |
---|
[220] | 1757 | /// will be enumerated once. |
---|
| 1758 | /// |
---|
| 1759 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
[282] | 1760 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 1761 | /// the next edge from \c u to \c v after \c prev. |
---|
| 1762 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
[220] | 1763 | /// |
---|
[282] | 1764 | /// Thus you can iterate through each edge between \c u and \c v |
---|
| 1765 | /// as it follows. |
---|
[220] | 1766 | ///\code |
---|
[282] | 1767 | /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
---|
[220] | 1768 | /// ... |
---|
| 1769 | /// } |
---|
| 1770 | ///\endcode |
---|
| 1771 | /// |
---|
[282] | 1772 | /// \note \ref ConEdgeIt provides iterator interface for the same |
---|
| 1773 | /// functionality. |
---|
| 1774 | /// |
---|
[220] | 1775 | ///\sa ConEdgeIt |
---|
| 1776 | template <typename Graph> |
---|
| 1777 | inline typename Graph::Edge |
---|
| 1778 | findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
---|
| 1779 | typename Graph::Edge p = INVALID) { |
---|
| 1780 | return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
---|
| 1781 | } |
---|
| 1782 | |
---|
[282] | 1783 | /// \brief Iterator for iterating on parallel edges connecting the same nodes. |
---|
[220] | 1784 | /// |
---|
[282] | 1785 | /// Iterator for iterating on parallel edges connecting the same nodes. |
---|
| 1786 | /// It is a higher level interface for the findEdge() function. You can |
---|
[220] | 1787 | /// use it the following way: |
---|
| 1788 | ///\code |
---|
[282] | 1789 | /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
---|
[220] | 1790 | /// ... |
---|
| 1791 | /// } |
---|
| 1792 | ///\endcode |
---|
| 1793 | /// |
---|
| 1794 | ///\sa findEdge() |
---|
[559] | 1795 | template <typename GR> |
---|
| 1796 | class ConEdgeIt : public GR::Edge { |
---|
[617] | 1797 | typedef typename GR::Edge Parent; |
---|
| 1798 | |
---|
[220] | 1799 | public: |
---|
| 1800 | |
---|
[617] | 1801 | typedef typename GR::Edge Edge; |
---|
| 1802 | typedef typename GR::Node Node; |
---|
[220] | 1803 | |
---|
| 1804 | /// \brief Constructor. |
---|
| 1805 | /// |
---|
[282] | 1806 | /// Construct a new ConEdgeIt iterating on the edges that |
---|
| 1807 | /// connects nodes \c u and \c v. |
---|
[617] | 1808 | ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
---|
[429] | 1809 | Parent::operator=(findEdge(_graph, _u, _v)); |
---|
[220] | 1810 | } |
---|
| 1811 | |
---|
| 1812 | /// \brief Constructor. |
---|
| 1813 | /// |
---|
[282] | 1814 | /// Construct a new ConEdgeIt that continues iterating from edge \c e. |
---|
[617] | 1815 | ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {} |
---|
[220] | 1816 | |
---|
| 1817 | /// \brief Increment operator. |
---|
| 1818 | /// |
---|
| 1819 | /// It increments the iterator and gives back the next edge. |
---|
| 1820 | ConEdgeIt& operator++() { |
---|
[429] | 1821 | Parent::operator=(findEdge(_graph, _u, _v, *this)); |
---|
[220] | 1822 | return *this; |
---|
| 1823 | } |
---|
| 1824 | private: |
---|
[617] | 1825 | const GR& _graph; |
---|
[429] | 1826 | Node _u, _v; |
---|
[220] | 1827 | }; |
---|
| 1828 | |
---|
| 1829 | |
---|
[282] | 1830 | ///Dynamic arc look-up between given endpoints. |
---|
[220] | 1831 | |
---|
| 1832 | ///Using this class, you can find an arc in a digraph from a given |
---|
[282] | 1833 | ///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
---|
[220] | 1834 | ///where <em>d</em> is the out-degree of the source node. |
---|
| 1835 | /// |
---|
| 1836 | ///It is possible to find \e all parallel arcs between two nodes with |
---|
[233] | 1837 | ///the \c operator() member. |
---|
[220] | 1838 | /// |
---|
[282] | 1839 | ///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
---|
| 1840 | ///\ref AllArcLookUp if your digraph is not changed so frequently. |
---|
[220] | 1841 | /// |
---|
[282] | 1842 | ///This class uses a self-adjusting binary search tree, the Splay tree |
---|
| 1843 | ///of Sleator and Tarjan to guarantee the logarithmic amortized |
---|
| 1844 | ///time bound for arc look-ups. This class also guarantees the |
---|
[220] | 1845 | ///optimal time bound in a constant factor for any distribution of |
---|
| 1846 | ///queries. |
---|
| 1847 | /// |
---|
[559] | 1848 | ///\tparam GR The type of the underlying digraph. |
---|
[220] | 1849 | /// |
---|
| 1850 | ///\sa ArcLookUp |
---|
| 1851 | ///\sa AllArcLookUp |
---|
[559] | 1852 | template <typename GR> |
---|
[220] | 1853 | class DynArcLookUp |
---|
[559] | 1854 | : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase |
---|
[220] | 1855 | { |
---|
[559] | 1856 | typedef typename ItemSetTraits<GR, typename GR::Arc> |
---|
[220] | 1857 | ::ItemNotifier::ObserverBase Parent; |
---|
| 1858 | |
---|
[559] | 1859 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
[617] | 1860 | |
---|
| 1861 | public: |
---|
| 1862 | |
---|
| 1863 | /// The Digraph type |
---|
[559] | 1864 | typedef GR Digraph; |
---|
[1092] | 1865 | |
---|
[220] | 1866 | protected: |
---|
| 1867 | |
---|
[877] | 1868 | class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type |
---|
| 1869 | { |
---|
[617] | 1870 | typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent; |
---|
| 1871 | |
---|
[220] | 1872 | public: |
---|
| 1873 | |
---|
[559] | 1874 | AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {} |
---|
[220] | 1875 | |
---|
| 1876 | virtual void add(const Node& node) { |
---|
| 1877 | Parent::add(node); |
---|
| 1878 | Parent::set(node, INVALID); |
---|
| 1879 | } |
---|
| 1880 | |
---|
| 1881 | virtual void add(const std::vector<Node>& nodes) { |
---|
| 1882 | Parent::add(nodes); |
---|
| 1883 | for (int i = 0; i < int(nodes.size()); ++i) { |
---|
| 1884 | Parent::set(nodes[i], INVALID); |
---|
| 1885 | } |
---|
| 1886 | } |
---|
| 1887 | |
---|
| 1888 | virtual void build() { |
---|
| 1889 | Parent::build(); |
---|
| 1890 | Node it; |
---|
| 1891 | typename Parent::Notifier* nf = Parent::notifier(); |
---|
| 1892 | for (nf->first(it); it != INVALID; nf->next(it)) { |
---|
| 1893 | Parent::set(it, INVALID); |
---|
| 1894 | } |
---|
| 1895 | } |
---|
| 1896 | }; |
---|
| 1897 | |
---|
| 1898 | class ArcLess { |
---|
| 1899 | const Digraph &g; |
---|
| 1900 | public: |
---|
| 1901 | ArcLess(const Digraph &_g) : g(_g) {} |
---|
| 1902 | bool operator()(Arc a,Arc b) const |
---|
| 1903 | { |
---|
| 1904 | return g.target(a)<g.target(b); |
---|
| 1905 | } |
---|
| 1906 | }; |
---|
| 1907 | |
---|
[877] | 1908 | protected: |
---|
[617] | 1909 | |
---|
| 1910 | const Digraph &_g; |
---|
| 1911 | AutoNodeMap _head; |
---|
| 1912 | typename Digraph::template ArcMap<Arc> _parent; |
---|
| 1913 | typename Digraph::template ArcMap<Arc> _left; |
---|
| 1914 | typename Digraph::template ArcMap<Arc> _right; |
---|
| 1915 | |
---|
[220] | 1916 | public: |
---|
| 1917 | |
---|
| 1918 | ///Constructor |
---|
| 1919 | |
---|
| 1920 | ///Constructor. |
---|
| 1921 | /// |
---|
| 1922 | ///It builds up the search database. |
---|
| 1923 | DynArcLookUp(const Digraph &g) |
---|
| 1924 | : _g(g),_head(g),_parent(g),_left(g),_right(g) |
---|
| 1925 | { |
---|
| 1926 | Parent::attach(_g.notifier(typename Digraph::Arc())); |
---|
| 1927 | refresh(); |
---|
| 1928 | } |
---|
| 1929 | |
---|
| 1930 | protected: |
---|
| 1931 | |
---|
| 1932 | virtual void add(const Arc& arc) { |
---|
| 1933 | insert(arc); |
---|
| 1934 | } |
---|
| 1935 | |
---|
| 1936 | virtual void add(const std::vector<Arc>& arcs) { |
---|
| 1937 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 1938 | insert(arcs[i]); |
---|
| 1939 | } |
---|
| 1940 | } |
---|
| 1941 | |
---|
| 1942 | virtual void erase(const Arc& arc) { |
---|
| 1943 | remove(arc); |
---|
| 1944 | } |
---|
| 1945 | |
---|
| 1946 | virtual void erase(const std::vector<Arc>& arcs) { |
---|
| 1947 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 1948 | remove(arcs[i]); |
---|
| 1949 | } |
---|
| 1950 | } |
---|
| 1951 | |
---|
| 1952 | virtual void build() { |
---|
| 1953 | refresh(); |
---|
| 1954 | } |
---|
| 1955 | |
---|
| 1956 | virtual void clear() { |
---|
| 1957 | for(NodeIt n(_g);n!=INVALID;++n) { |
---|
[581] | 1958 | _head[n] = INVALID; |
---|
[220] | 1959 | } |
---|
| 1960 | } |
---|
| 1961 | |
---|
| 1962 | void insert(Arc arc) { |
---|
| 1963 | Node s = _g.source(arc); |
---|
| 1964 | Node t = _g.target(arc); |
---|
[581] | 1965 | _left[arc] = INVALID; |
---|
| 1966 | _right[arc] = INVALID; |
---|
[220] | 1967 | |
---|
| 1968 | Arc e = _head[s]; |
---|
| 1969 | if (e == INVALID) { |
---|
[581] | 1970 | _head[s] = arc; |
---|
| 1971 | _parent[arc] = INVALID; |
---|
[220] | 1972 | return; |
---|
| 1973 | } |
---|
| 1974 | while (true) { |
---|
| 1975 | if (t < _g.target(e)) { |
---|
| 1976 | if (_left[e] == INVALID) { |
---|
[581] | 1977 | _left[e] = arc; |
---|
| 1978 | _parent[arc] = e; |
---|
[220] | 1979 | splay(arc); |
---|
| 1980 | return; |
---|
| 1981 | } else { |
---|
| 1982 | e = _left[e]; |
---|
| 1983 | } |
---|
| 1984 | } else { |
---|
| 1985 | if (_right[e] == INVALID) { |
---|
[581] | 1986 | _right[e] = arc; |
---|
| 1987 | _parent[arc] = e; |
---|
[220] | 1988 | splay(arc); |
---|
| 1989 | return; |
---|
| 1990 | } else { |
---|
| 1991 | e = _right[e]; |
---|
| 1992 | } |
---|
| 1993 | } |
---|
| 1994 | } |
---|
| 1995 | } |
---|
| 1996 | |
---|
| 1997 | void remove(Arc arc) { |
---|
| 1998 | if (_left[arc] == INVALID) { |
---|
| 1999 | if (_right[arc] != INVALID) { |
---|
[581] | 2000 | _parent[_right[arc]] = _parent[arc]; |
---|
[220] | 2001 | } |
---|
| 2002 | if (_parent[arc] != INVALID) { |
---|
| 2003 | if (_left[_parent[arc]] == arc) { |
---|
[581] | 2004 | _left[_parent[arc]] = _right[arc]; |
---|
[220] | 2005 | } else { |
---|
[581] | 2006 | _right[_parent[arc]] = _right[arc]; |
---|
[220] | 2007 | } |
---|
| 2008 | } else { |
---|
[581] | 2009 | _head[_g.source(arc)] = _right[arc]; |
---|
[220] | 2010 | } |
---|
| 2011 | } else if (_right[arc] == INVALID) { |
---|
[581] | 2012 | _parent[_left[arc]] = _parent[arc]; |
---|
[220] | 2013 | if (_parent[arc] != INVALID) { |
---|
| 2014 | if (_left[_parent[arc]] == arc) { |
---|
[581] | 2015 | _left[_parent[arc]] = _left[arc]; |
---|
[220] | 2016 | } else { |
---|
[581] | 2017 | _right[_parent[arc]] = _left[arc]; |
---|
[220] | 2018 | } |
---|
| 2019 | } else { |
---|
[581] | 2020 | _head[_g.source(arc)] = _left[arc]; |
---|
[220] | 2021 | } |
---|
| 2022 | } else { |
---|
| 2023 | Arc e = _left[arc]; |
---|
| 2024 | if (_right[e] != INVALID) { |
---|
| 2025 | e = _right[e]; |
---|
| 2026 | while (_right[e] != INVALID) { |
---|
| 2027 | e = _right[e]; |
---|
| 2028 | } |
---|
| 2029 | Arc s = _parent[e]; |
---|
[581] | 2030 | _right[_parent[e]] = _left[e]; |
---|
[220] | 2031 | if (_left[e] != INVALID) { |
---|
[581] | 2032 | _parent[_left[e]] = _parent[e]; |
---|
[220] | 2033 | } |
---|
| 2034 | |
---|
[581] | 2035 | _left[e] = _left[arc]; |
---|
| 2036 | _parent[_left[arc]] = e; |
---|
| 2037 | _right[e] = _right[arc]; |
---|
| 2038 | _parent[_right[arc]] = e; |
---|
[220] | 2039 | |
---|
[581] | 2040 | _parent[e] = _parent[arc]; |
---|
[220] | 2041 | if (_parent[arc] != INVALID) { |
---|
| 2042 | if (_left[_parent[arc]] == arc) { |
---|
[581] | 2043 | _left[_parent[arc]] = e; |
---|
[220] | 2044 | } else { |
---|
[581] | 2045 | _right[_parent[arc]] = e; |
---|
[220] | 2046 | } |
---|
| 2047 | } |
---|
| 2048 | splay(s); |
---|
| 2049 | } else { |
---|
[581] | 2050 | _right[e] = _right[arc]; |
---|
| 2051 | _parent[_right[arc]] = e; |
---|
| 2052 | _parent[e] = _parent[arc]; |
---|
[220] | 2053 | |
---|
| 2054 | if (_parent[arc] != INVALID) { |
---|
| 2055 | if (_left[_parent[arc]] == arc) { |
---|
[581] | 2056 | _left[_parent[arc]] = e; |
---|
[220] | 2057 | } else { |
---|
[581] | 2058 | _right[_parent[arc]] = e; |
---|
[220] | 2059 | } |
---|
| 2060 | } else { |
---|
[581] | 2061 | _head[_g.source(arc)] = e; |
---|
[220] | 2062 | } |
---|
| 2063 | } |
---|
| 2064 | } |
---|
| 2065 | } |
---|
| 2066 | |
---|
| 2067 | Arc refreshRec(std::vector<Arc> &v,int a,int b) |
---|
| 2068 | { |
---|
| 2069 | int m=(a+b)/2; |
---|
| 2070 | Arc me=v[m]; |
---|
| 2071 | if (a < m) { |
---|
| 2072 | Arc left = refreshRec(v,a,m-1); |
---|
[581] | 2073 | _left[me] = left; |
---|
| 2074 | _parent[left] = me; |
---|
[220] | 2075 | } else { |
---|
[581] | 2076 | _left[me] = INVALID; |
---|
[220] | 2077 | } |
---|
| 2078 | if (m < b) { |
---|
| 2079 | Arc right = refreshRec(v,m+1,b); |
---|
[581] | 2080 | _right[me] = right; |
---|
| 2081 | _parent[right] = me; |
---|
[220] | 2082 | } else { |
---|
[581] | 2083 | _right[me] = INVALID; |
---|
[220] | 2084 | } |
---|
| 2085 | return me; |
---|
| 2086 | } |
---|
| 2087 | |
---|
| 2088 | void refresh() { |
---|
| 2089 | for(NodeIt n(_g);n!=INVALID;++n) { |
---|
| 2090 | std::vector<Arc> v; |
---|
[233] | 2091 | for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
---|
| 2092 | if (!v.empty()) { |
---|
[220] | 2093 | std::sort(v.begin(),v.end(),ArcLess(_g)); |
---|
| 2094 | Arc head = refreshRec(v,0,v.size()-1); |
---|
[581] | 2095 | _head[n] = head; |
---|
| 2096 | _parent[head] = INVALID; |
---|
[220] | 2097 | } |
---|
[581] | 2098 | else _head[n] = INVALID; |
---|
[220] | 2099 | } |
---|
| 2100 | } |
---|
| 2101 | |
---|
| 2102 | void zig(Arc v) { |
---|
| 2103 | Arc w = _parent[v]; |
---|
[581] | 2104 | _parent[v] = _parent[w]; |
---|
| 2105 | _parent[w] = v; |
---|
| 2106 | _left[w] = _right[v]; |
---|
| 2107 | _right[v] = w; |
---|
[220] | 2108 | if (_parent[v] != INVALID) { |
---|
| 2109 | if (_right[_parent[v]] == w) { |
---|
[581] | 2110 | _right[_parent[v]] = v; |
---|
[220] | 2111 | } else { |
---|
[581] | 2112 | _left[_parent[v]] = v; |
---|
[220] | 2113 | } |
---|
| 2114 | } |
---|
| 2115 | if (_left[w] != INVALID){ |
---|
[581] | 2116 | _parent[_left[w]] = w; |
---|
[220] | 2117 | } |
---|
| 2118 | } |
---|
| 2119 | |
---|
| 2120 | void zag(Arc v) { |
---|
| 2121 | Arc w = _parent[v]; |
---|
[581] | 2122 | _parent[v] = _parent[w]; |
---|
| 2123 | _parent[w] = v; |
---|
| 2124 | _right[w] = _left[v]; |
---|
| 2125 | _left[v] = w; |
---|
[220] | 2126 | if (_parent[v] != INVALID){ |
---|
| 2127 | if (_left[_parent[v]] == w) { |
---|
[581] | 2128 | _left[_parent[v]] = v; |
---|
[220] | 2129 | } else { |
---|
[581] | 2130 | _right[_parent[v]] = v; |
---|
[220] | 2131 | } |
---|
| 2132 | } |
---|
| 2133 | if (_right[w] != INVALID){ |
---|
[581] | 2134 | _parent[_right[w]] = w; |
---|
[220] | 2135 | } |
---|
| 2136 | } |
---|
| 2137 | |
---|
| 2138 | void splay(Arc v) { |
---|
| 2139 | while (_parent[v] != INVALID) { |
---|
| 2140 | if (v == _left[_parent[v]]) { |
---|
| 2141 | if (_parent[_parent[v]] == INVALID) { |
---|
| 2142 | zig(v); |
---|
| 2143 | } else { |
---|
| 2144 | if (_parent[v] == _left[_parent[_parent[v]]]) { |
---|
| 2145 | zig(_parent[v]); |
---|
| 2146 | zig(v); |
---|
| 2147 | } else { |
---|
| 2148 | zig(v); |
---|
| 2149 | zag(v); |
---|
| 2150 | } |
---|
| 2151 | } |
---|
| 2152 | } else { |
---|
| 2153 | if (_parent[_parent[v]] == INVALID) { |
---|
| 2154 | zag(v); |
---|
| 2155 | } else { |
---|
| 2156 | if (_parent[v] == _left[_parent[_parent[v]]]) { |
---|
| 2157 | zag(v); |
---|
| 2158 | zig(v); |
---|
| 2159 | } else { |
---|
| 2160 | zag(_parent[v]); |
---|
| 2161 | zag(v); |
---|
| 2162 | } |
---|
| 2163 | } |
---|
| 2164 | } |
---|
| 2165 | } |
---|
| 2166 | _head[_g.source(v)] = v; |
---|
| 2167 | } |
---|
| 2168 | |
---|
| 2169 | |
---|
| 2170 | public: |
---|
| 2171 | |
---|
| 2172 | ///Find an arc between two nodes. |
---|
| 2173 | |
---|
[233] | 2174 | ///Find an arc between two nodes. |
---|
[282] | 2175 | ///\param s The source node. |
---|
| 2176 | ///\param t The target node. |
---|
[233] | 2177 | ///\param p The previous arc between \c s and \c t. It it is INVALID or |
---|
| 2178 | ///not given, the operator finds the first appropriate arc. |
---|
| 2179 | ///\return An arc from \c s to \c t after \c p or |
---|
| 2180 | ///\ref INVALID if there is no more. |
---|
| 2181 | /// |
---|
| 2182 | ///For example, you can count the number of arcs from \c u to \c v in the |
---|
| 2183 | ///following way. |
---|
| 2184 | ///\code |
---|
| 2185 | ///DynArcLookUp<ListDigraph> ae(g); |
---|
| 2186 | ///... |
---|
[282] | 2187 | ///int n = 0; |
---|
| 2188 | ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
---|
[233] | 2189 | ///\endcode |
---|
| 2190 | /// |
---|
[282] | 2191 | ///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
---|
[233] | 2192 | ///amortized time, specifically, the time complexity of the lookups |
---|
| 2193 | ///is equal to the optimal search tree implementation for the |
---|
| 2194 | ///current query distribution in a constant factor. |
---|
| 2195 | /// |
---|
| 2196 | ///\note This is a dynamic data structure, therefore the data |
---|
[282] | 2197 | ///structure is updated after each graph alteration. Thus although |
---|
| 2198 | ///this data structure is theoretically faster than \ref ArcLookUp |
---|
[313] | 2199 | ///and \ref AllArcLookUp, it often provides worse performance than |
---|
[233] | 2200 | ///them. |
---|
| 2201 | Arc operator()(Node s, Node t, Arc p = INVALID) const { |
---|
| 2202 | if (p == INVALID) { |
---|
| 2203 | Arc a = _head[s]; |
---|
| 2204 | if (a == INVALID) return INVALID; |
---|
| 2205 | Arc r = INVALID; |
---|
| 2206 | while (true) { |
---|
| 2207 | if (_g.target(a) < t) { |
---|
| 2208 | if (_right[a] == INVALID) { |
---|
| 2209 | const_cast<DynArcLookUp&>(*this).splay(a); |
---|
| 2210 | return r; |
---|
| 2211 | } else { |
---|
| 2212 | a = _right[a]; |
---|
| 2213 | } |
---|
| 2214 | } else { |
---|
| 2215 | if (_g.target(a) == t) { |
---|
| 2216 | r = a; |
---|
| 2217 | } |
---|
| 2218 | if (_left[a] == INVALID) { |
---|
| 2219 | const_cast<DynArcLookUp&>(*this).splay(a); |
---|
| 2220 | return r; |
---|
| 2221 | } else { |
---|
| 2222 | a = _left[a]; |
---|
| 2223 | } |
---|
| 2224 | } |
---|
| 2225 | } |
---|
| 2226 | } else { |
---|
| 2227 | Arc a = p; |
---|
| 2228 | if (_right[a] != INVALID) { |
---|
| 2229 | a = _right[a]; |
---|
| 2230 | while (_left[a] != INVALID) { |
---|
| 2231 | a = _left[a]; |
---|
| 2232 | } |
---|
[220] | 2233 | const_cast<DynArcLookUp&>(*this).splay(a); |
---|
[233] | 2234 | } else { |
---|
| 2235 | while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
---|
| 2236 | a = _parent[a]; |
---|
| 2237 | } |
---|
| 2238 | if (_parent[a] == INVALID) { |
---|
[220] | 2239 | return INVALID; |
---|
| 2240 | } else { |
---|
[233] | 2241 | a = _parent[a]; |
---|
[220] | 2242 | const_cast<DynArcLookUp&>(*this).splay(a); |
---|
| 2243 | } |
---|
| 2244 | } |
---|
[233] | 2245 | if (_g.target(a) == t) return a; |
---|
| 2246 | else return INVALID; |
---|
[220] | 2247 | } |
---|
| 2248 | } |
---|
| 2249 | |
---|
| 2250 | }; |
---|
| 2251 | |
---|
[282] | 2252 | ///Fast arc look-up between given endpoints. |
---|
[220] | 2253 | |
---|
| 2254 | ///Using this class, you can find an arc in a digraph from a given |
---|
[282] | 2255 | ///source to a given target in time <em>O</em>(log<em>d</em>), |
---|
[220] | 2256 | ///where <em>d</em> is the out-degree of the source node. |
---|
| 2257 | /// |
---|
| 2258 | ///It is not possible to find \e all parallel arcs between two nodes. |
---|
| 2259 | ///Use \ref AllArcLookUp for this purpose. |
---|
| 2260 | /// |
---|
[282] | 2261 | ///\warning This class is static, so you should call refresh() (or at |
---|
| 2262 | ///least refresh(Node)) to refresh this data structure whenever the |
---|
| 2263 | ///digraph changes. This is a time consuming (superlinearly proportional |
---|
| 2264 | ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
---|
[220] | 2265 | /// |
---|
[559] | 2266 | ///\tparam GR The type of the underlying digraph. |
---|
[220] | 2267 | /// |
---|
| 2268 | ///\sa DynArcLookUp |
---|
| 2269 | ///\sa AllArcLookUp |
---|
[559] | 2270 | template<class GR> |
---|
[220] | 2271 | class ArcLookUp |
---|
| 2272 | { |
---|
[617] | 2273 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
| 2274 | |
---|
[220] | 2275 | public: |
---|
[617] | 2276 | |
---|
| 2277 | /// The Digraph type |
---|
[559] | 2278 | typedef GR Digraph; |
---|
[220] | 2279 | |
---|
| 2280 | protected: |
---|
| 2281 | const Digraph &_g; |
---|
| 2282 | typename Digraph::template NodeMap<Arc> _head; |
---|
| 2283 | typename Digraph::template ArcMap<Arc> _left; |
---|
| 2284 | typename Digraph::template ArcMap<Arc> _right; |
---|
| 2285 | |
---|
| 2286 | class ArcLess { |
---|
| 2287 | const Digraph &g; |
---|
| 2288 | public: |
---|
| 2289 | ArcLess(const Digraph &_g) : g(_g) {} |
---|
| 2290 | bool operator()(Arc a,Arc b) const |
---|
| 2291 | { |
---|
| 2292 | return g.target(a)<g.target(b); |
---|
| 2293 | } |
---|
| 2294 | }; |
---|
| 2295 | |
---|
| 2296 | public: |
---|
| 2297 | |
---|
| 2298 | ///Constructor |
---|
| 2299 | |
---|
| 2300 | ///Constructor. |
---|
| 2301 | /// |
---|
| 2302 | ///It builds up the search database, which remains valid until the digraph |
---|
| 2303 | ///changes. |
---|
| 2304 | ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
---|
| 2305 | |
---|
| 2306 | private: |
---|
| 2307 | Arc refreshRec(std::vector<Arc> &v,int a,int b) |
---|
| 2308 | { |
---|
| 2309 | int m=(a+b)/2; |
---|
| 2310 | Arc me=v[m]; |
---|
| 2311 | _left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
---|
| 2312 | _right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
---|
| 2313 | return me; |
---|
| 2314 | } |
---|
| 2315 | public: |
---|
[282] | 2316 | ///Refresh the search data structure at a node. |
---|
[220] | 2317 | |
---|
| 2318 | ///Build up the search database of node \c n. |
---|
| 2319 | /// |
---|
[282] | 2320 | ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
---|
| 2321 | ///is the number of the outgoing arcs of \c n. |
---|
[220] | 2322 | void refresh(Node n) |
---|
| 2323 | { |
---|
| 2324 | std::vector<Arc> v; |
---|
| 2325 | for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
---|
| 2326 | if(v.size()) { |
---|
| 2327 | std::sort(v.begin(),v.end(),ArcLess(_g)); |
---|
| 2328 | _head[n]=refreshRec(v,0,v.size()-1); |
---|
| 2329 | } |
---|
| 2330 | else _head[n]=INVALID; |
---|
| 2331 | } |
---|
| 2332 | ///Refresh the full data structure. |
---|
| 2333 | |
---|
| 2334 | ///Build up the full search database. In fact, it simply calls |
---|
| 2335 | ///\ref refresh(Node) "refresh(n)" for each node \c n. |
---|
| 2336 | /// |
---|
[282] | 2337 | ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
---|
| 2338 | ///the number of the arcs in the digraph and <em>D</em> is the maximum |
---|
[220] | 2339 | ///out-degree of the digraph. |
---|
| 2340 | void refresh() |
---|
| 2341 | { |
---|
| 2342 | for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
---|
| 2343 | } |
---|
| 2344 | |
---|
| 2345 | ///Find an arc between two nodes. |
---|
| 2346 | |
---|
[313] | 2347 | ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), |
---|
| 2348 | ///where <em>d</em> is the number of outgoing arcs of \c s. |
---|
[282] | 2349 | ///\param s The source node. |
---|
| 2350 | ///\param t The target node. |
---|
[220] | 2351 | ///\return An arc from \c s to \c t if there exists, |
---|
| 2352 | ///\ref INVALID otherwise. |
---|
| 2353 | /// |
---|
| 2354 | ///\warning If you change the digraph, refresh() must be called before using |
---|
| 2355 | ///this operator. If you change the outgoing arcs of |
---|
[282] | 2356 | ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
---|
[220] | 2357 | Arc operator()(Node s, Node t) const |
---|
| 2358 | { |
---|
| 2359 | Arc e; |
---|
| 2360 | for(e=_head[s]; |
---|
| 2361 | e!=INVALID&&_g.target(e)!=t; |
---|
| 2362 | e = t < _g.target(e)?_left[e]:_right[e]) ; |
---|
| 2363 | return e; |
---|
| 2364 | } |
---|
| 2365 | |
---|
| 2366 | }; |
---|
| 2367 | |
---|
[282] | 2368 | ///Fast look-up of all arcs between given endpoints. |
---|
[220] | 2369 | |
---|
| 2370 | ///This class is the same as \ref ArcLookUp, with the addition |
---|
[282] | 2371 | ///that it makes it possible to find all parallel arcs between given |
---|
| 2372 | ///endpoints. |
---|
[220] | 2373 | /// |
---|
[282] | 2374 | ///\warning This class is static, so you should call refresh() (or at |
---|
| 2375 | ///least refresh(Node)) to refresh this data structure whenever the |
---|
| 2376 | ///digraph changes. This is a time consuming (superlinearly proportional |
---|
| 2377 | ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
---|
[220] | 2378 | /// |
---|
[559] | 2379 | ///\tparam GR The type of the underlying digraph. |
---|
[220] | 2380 | /// |
---|
| 2381 | ///\sa DynArcLookUp |
---|
| 2382 | ///\sa ArcLookUp |
---|
[559] | 2383 | template<class GR> |
---|
| 2384 | class AllArcLookUp : public ArcLookUp<GR> |
---|
[220] | 2385 | { |
---|
[559] | 2386 | using ArcLookUp<GR>::_g; |
---|
| 2387 | using ArcLookUp<GR>::_right; |
---|
| 2388 | using ArcLookUp<GR>::_left; |
---|
| 2389 | using ArcLookUp<GR>::_head; |
---|
[220] | 2390 | |
---|
[559] | 2391 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
[220] | 2392 | |
---|
[617] | 2393 | typename GR::template ArcMap<Arc> _next; |
---|
[220] | 2394 | |
---|
| 2395 | Arc refreshNext(Arc head,Arc next=INVALID) |
---|
| 2396 | { |
---|
| 2397 | if(head==INVALID) return next; |
---|
| 2398 | else { |
---|
| 2399 | next=refreshNext(_right[head],next); |
---|
| 2400 | _next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
---|
| 2401 | ? next : INVALID; |
---|
| 2402 | return refreshNext(_left[head],head); |
---|
| 2403 | } |
---|
| 2404 | } |
---|
| 2405 | |
---|
| 2406 | void refreshNext() |
---|
| 2407 | { |
---|
| 2408 | for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
---|
| 2409 | } |
---|
| 2410 | |
---|
| 2411 | public: |
---|
[617] | 2412 | |
---|
| 2413 | /// The Digraph type |
---|
| 2414 | typedef GR Digraph; |
---|
| 2415 | |
---|
[220] | 2416 | ///Constructor |
---|
| 2417 | |
---|
| 2418 | ///Constructor. |
---|
| 2419 | /// |
---|
| 2420 | ///It builds up the search database, which remains valid until the digraph |
---|
| 2421 | ///changes. |
---|
[559] | 2422 | AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();} |
---|
[220] | 2423 | |
---|
| 2424 | ///Refresh the data structure at a node. |
---|
| 2425 | |
---|
| 2426 | ///Build up the search database of node \c n. |
---|
| 2427 | /// |
---|
[282] | 2428 | ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
---|
[220] | 2429 | ///the number of the outgoing arcs of \c n. |
---|
| 2430 | void refresh(Node n) |
---|
| 2431 | { |
---|
[559] | 2432 | ArcLookUp<GR>::refresh(n); |
---|
[220] | 2433 | refreshNext(_head[n]); |
---|
| 2434 | } |
---|
| 2435 | |
---|
| 2436 | ///Refresh the full data structure. |
---|
| 2437 | |
---|
| 2438 | ///Build up the full search database. In fact, it simply calls |
---|
| 2439 | ///\ref refresh(Node) "refresh(n)" for each node \c n. |
---|
| 2440 | /// |
---|
[282] | 2441 | ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
---|
| 2442 | ///the number of the arcs in the digraph and <em>D</em> is the maximum |
---|
[220] | 2443 | ///out-degree of the digraph. |
---|
| 2444 | void refresh() |
---|
| 2445 | { |
---|
| 2446 | for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
---|
| 2447 | } |
---|
| 2448 | |
---|
| 2449 | ///Find an arc between two nodes. |
---|
| 2450 | |
---|
| 2451 | ///Find an arc between two nodes. |
---|
[282] | 2452 | ///\param s The source node. |
---|
| 2453 | ///\param t The target node. |
---|
[220] | 2454 | ///\param prev The previous arc between \c s and \c t. It it is INVALID or |
---|
| 2455 | ///not given, the operator finds the first appropriate arc. |
---|
| 2456 | ///\return An arc from \c s to \c t after \c prev or |
---|
| 2457 | ///\ref INVALID if there is no more. |
---|
| 2458 | /// |
---|
| 2459 | ///For example, you can count the number of arcs from \c u to \c v in the |
---|
| 2460 | ///following way. |
---|
| 2461 | ///\code |
---|
| 2462 | ///AllArcLookUp<ListDigraph> ae(g); |
---|
| 2463 | ///... |
---|
[282] | 2464 | ///int n = 0; |
---|
| 2465 | ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
---|
[220] | 2466 | ///\endcode |
---|
| 2467 | /// |
---|
[313] | 2468 | ///Finding the first arc take <em>O</em>(log<em>d</em>) time, |
---|
| 2469 | ///where <em>d</em> is the number of outgoing arcs of \c s. Then the |
---|
[220] | 2470 | ///consecutive arcs are found in constant time. |
---|
| 2471 | /// |
---|
| 2472 | ///\warning If you change the digraph, refresh() must be called before using |
---|
| 2473 | ///this operator. If you change the outgoing arcs of |
---|
[282] | 2474 | ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
---|
[220] | 2475 | /// |
---|
[993] | 2476 | Arc operator()(Node s, Node t, Arc prev=INVALID) const |
---|
[220] | 2477 | { |
---|
[993] | 2478 | if(prev==INVALID) |
---|
| 2479 | { |
---|
| 2480 | Arc f=INVALID; |
---|
| 2481 | Arc e; |
---|
| 2482 | for(e=_head[s]; |
---|
| 2483 | e!=INVALID&&_g.target(e)!=t; |
---|
| 2484 | e = t < _g.target(e)?_left[e]:_right[e]) ; |
---|
| 2485 | while(e!=INVALID) |
---|
| 2486 | if(_g.target(e)==t) |
---|
| 2487 | { |
---|
| 2488 | f = e; |
---|
| 2489 | e = _left[e]; |
---|
| 2490 | } |
---|
| 2491 | else e = _right[e]; |
---|
| 2492 | return f; |
---|
| 2493 | } |
---|
| 2494 | else return _next[prev]; |
---|
[220] | 2495 | } |
---|
| 2496 | |
---|
| 2497 | }; |
---|
| 2498 | |
---|
| 2499 | /// @} |
---|
| 2500 | |
---|
| 2501 | } //namespace lemon |
---|
| 2502 | |
---|
| 2503 | #endif |
---|