| [220] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * This file is a part of LEMON, a generic C++ optimization library. | 
|---|
|  | 4 | * | 
|---|
| [440] | 5 | * Copyright (C) 2003-2009 | 
|---|
| [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 |  | 
|---|
|  | 22 | #include <vector> | 
|---|
|  | 23 | #include <algorithm> | 
|---|
|  | 24 |  | 
|---|
| [512] | 25 | #include <lemon/config.h> | 
|---|
| [220] | 26 | #include <lemon/bits/enable_if.h> | 
|---|
|  | 27 | #include <lemon/bits/traits.h> | 
|---|
| [319] | 28 | #include <lemon/assert.h> | 
|---|
| [220] | 29 |  | 
|---|
| [671] | 30 | // Disable the following warnings when compiling with MSVC: | 
|---|
|  | 31 | // C4250: 'class1' : inherits 'class2::member' via dominance | 
|---|
|  | 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 | 
|---|
|  | 37 | #pragma warning( disable : 4250 4355 4503 4800 4996 ) | 
|---|
|  | 38 | #endif | 
|---|
|  | 39 |  | 
|---|
| [1068] | 40 | #ifdef __GNUC__ | 
|---|
| [1077] | 41 | #define GCC_VERSION (__GNUC__ * 10000                   \ | 
|---|
|  | 42 | + __GNUC_MINOR__ * 100             \ | 
|---|
|  | 43 | + __GNUC_PATCHLEVEL__) | 
|---|
|  | 44 | #endif | 
|---|
|  | 45 |  | 
|---|
|  | 46 | #if GCC_VERSION >= 40800 | 
|---|
| [1068] | 47 | // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8 | 
|---|
|  | 48 | #pragma GCC diagnostic ignored "-Wunused-local-typedefs" | 
|---|
|  | 49 | #endif | 
|---|
|  | 50 |  | 
|---|
| [220] | 51 | ///\file | 
|---|
|  | 52 | ///\brief LEMON core utilities. | 
|---|
| [229] | 53 | /// | 
|---|
|  | 54 | ///This header file contains core utilities for LEMON. | 
|---|
| [233] | 55 | ///It is automatically included by all graph types, therefore it usually | 
|---|
| [229] | 56 | ///do not have to be included directly. | 
|---|
| [220] | 57 |  | 
|---|
|  | 58 | namespace lemon { | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /// \brief Dummy type to make it easier to create invalid iterators. | 
|---|
|  | 61 | /// | 
|---|
|  | 62 | /// Dummy type to make it easier to create invalid iterators. | 
|---|
|  | 63 | /// See \ref INVALID for the usage. | 
|---|
|  | 64 | struct Invalid { | 
|---|
|  | 65 | public: | 
|---|
|  | 66 | bool operator==(Invalid) { return true;  } | 
|---|
|  | 67 | bool operator!=(Invalid) { return false; } | 
|---|
|  | 68 | bool operator< (Invalid) { return false; } | 
|---|
|  | 69 | }; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | /// \brief Invalid iterators. | 
|---|
|  | 72 | /// | 
|---|
|  | 73 | /// \ref Invalid is a global type that converts to each iterator | 
|---|
|  | 74 | /// in such a way that the value of the target iterator will be invalid. | 
|---|
|  | 75 | #ifdef LEMON_ONLY_TEMPLATES | 
|---|
|  | 76 | const Invalid INVALID = Invalid(); | 
|---|
|  | 77 | #else | 
|---|
|  | 78 | extern const Invalid INVALID; | 
|---|
|  | 79 | #endif | 
|---|
|  | 80 |  | 
|---|
|  | 81 | /// \addtogroup gutils | 
|---|
|  | 82 | /// @{ | 
|---|
|  | 83 |  | 
|---|
| [300] | 84 | ///Create convenience typedefs for the digraph types and iterators | 
|---|
| [220] | 85 |  | 
|---|
| [282] | 86 | ///This \c \#define creates convenient type definitions for the following | 
|---|
|  | 87 | ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, | 
|---|
| [220] | 88 | ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, | 
|---|
|  | 89 | ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. | 
|---|
|  | 90 | /// | 
|---|
|  | 91 | ///\note If the graph type is a dependent type, ie. the graph type depend | 
|---|
|  | 92 | ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() | 
|---|
|  | 93 | ///macro. | 
|---|
|  | 94 | #define DIGRAPH_TYPEDEFS(Digraph)                                       \ | 
|---|
|  | 95 | typedef Digraph::Node Node;                                           \ | 
|---|
|  | 96 | typedef Digraph::NodeIt NodeIt;                                       \ | 
|---|
|  | 97 | typedef Digraph::Arc Arc;                                             \ | 
|---|
|  | 98 | typedef Digraph::ArcIt ArcIt;                                         \ | 
|---|
|  | 99 | typedef Digraph::InArcIt InArcIt;                                     \ | 
|---|
|  | 100 | typedef Digraph::OutArcIt OutArcIt;                                   \ | 
|---|
|  | 101 | typedef Digraph::NodeMap<bool> BoolNodeMap;                           \ | 
|---|
|  | 102 | typedef Digraph::NodeMap<int> IntNodeMap;                             \ | 
|---|
|  | 103 | typedef Digraph::NodeMap<double> DoubleNodeMap;                       \ | 
|---|
|  | 104 | typedef Digraph::ArcMap<bool> BoolArcMap;                             \ | 
|---|
|  | 105 | typedef Digraph::ArcMap<int> IntArcMap;                               \ | 
|---|
| [300] | 106 | typedef Digraph::ArcMap<double> DoubleArcMap | 
|---|
| [220] | 107 |  | 
|---|
| [300] | 108 | ///Create convenience typedefs for the digraph types and iterators | 
|---|
| [220] | 109 |  | 
|---|
|  | 110 | ///\see DIGRAPH_TYPEDEFS | 
|---|
|  | 111 | /// | 
|---|
|  | 112 | ///\note Use this macro, if the graph type is a dependent type, | 
|---|
|  | 113 | ///ie. the graph type depend on a template parameter. | 
|---|
|  | 114 | #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \ | 
|---|
|  | 115 | typedef typename Digraph::Node Node;                                  \ | 
|---|
|  | 116 | typedef typename Digraph::NodeIt NodeIt;                              \ | 
|---|
|  | 117 | typedef typename Digraph::Arc Arc;                                    \ | 
|---|
|  | 118 | typedef typename Digraph::ArcIt ArcIt;                                \ | 
|---|
|  | 119 | typedef typename Digraph::InArcIt InArcIt;                            \ | 
|---|
|  | 120 | typedef typename Digraph::OutArcIt OutArcIt;                          \ | 
|---|
|  | 121 | typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \ | 
|---|
|  | 122 | typedef typename Digraph::template NodeMap<int> IntNodeMap;           \ | 
|---|
|  | 123 | typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \ | 
|---|
|  | 124 | typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \ | 
|---|
|  | 125 | typedef typename Digraph::template ArcMap<int> IntArcMap;             \ | 
|---|
| [300] | 126 | typedef typename Digraph::template ArcMap<double> DoubleArcMap | 
|---|
| [220] | 127 |  | 
|---|
| [300] | 128 | ///Create convenience typedefs for the graph types and iterators | 
|---|
| [220] | 129 |  | 
|---|
| [282] | 130 | ///This \c \#define creates the same convenient type definitions as defined | 
|---|
| [220] | 131 | ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates | 
|---|
|  | 132 | ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, | 
|---|
|  | 133 | ///\c DoubleEdgeMap. | 
|---|
|  | 134 | /// | 
|---|
|  | 135 | ///\note If the graph type is a dependent type, ie. the graph type depend | 
|---|
| [282] | 136 | ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() | 
|---|
| [220] | 137 | ///macro. | 
|---|
|  | 138 | #define GRAPH_TYPEDEFS(Graph)                                           \ | 
|---|
|  | 139 | DIGRAPH_TYPEDEFS(Graph);                                              \ | 
|---|
|  | 140 | typedef Graph::Edge Edge;                                             \ | 
|---|
|  | 141 | typedef Graph::EdgeIt EdgeIt;                                         \ | 
|---|
|  | 142 | typedef Graph::IncEdgeIt IncEdgeIt;                                   \ | 
|---|
|  | 143 | typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \ | 
|---|
|  | 144 | typedef Graph::EdgeMap<int> IntEdgeMap;                               \ | 
|---|
| [300] | 145 | typedef Graph::EdgeMap<double> DoubleEdgeMap | 
|---|
| [220] | 146 |  | 
|---|
| [300] | 147 | ///Create convenience typedefs for the graph types and iterators | 
|---|
| [220] | 148 |  | 
|---|
|  | 149 | ///\see GRAPH_TYPEDEFS | 
|---|
|  | 150 | /// | 
|---|
|  | 151 | ///\note Use this macro, if the graph type is a dependent type, | 
|---|
|  | 152 | ///ie. the graph type depend on a template parameter. | 
|---|
|  | 153 | #define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \ | 
|---|
|  | 154 | TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \ | 
|---|
|  | 155 | typedef typename Graph::Edge Edge;                                    \ | 
|---|
|  | 156 | typedef typename Graph::EdgeIt EdgeIt;                                \ | 
|---|
|  | 157 | typedef typename Graph::IncEdgeIt IncEdgeIt;                          \ | 
|---|
|  | 158 | typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \ | 
|---|
|  | 159 | typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \ | 
|---|
| [300] | 160 | typedef typename Graph::template EdgeMap<double> DoubleEdgeMap | 
|---|
| [220] | 161 |  | 
|---|
| [282] | 162 | /// \brief Function to count the items in a graph. | 
|---|
| [220] | 163 | /// | 
|---|
| [282] | 164 | /// This function counts the items (nodes, arcs etc.) in a graph. | 
|---|
|  | 165 | /// The complexity of the function is linear because | 
|---|
| [220] | 166 | /// it iterates on all of the items. | 
|---|
|  | 167 | template <typename Graph, typename Item> | 
|---|
|  | 168 | inline int countItems(const Graph& g) { | 
|---|
|  | 169 | typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; | 
|---|
|  | 170 | int num = 0; | 
|---|
|  | 171 | for (ItemIt it(g); it != INVALID; ++it) { | 
|---|
|  | 172 | ++num; | 
|---|
|  | 173 | } | 
|---|
|  | 174 | return num; | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 | // Node counting: | 
|---|
|  | 178 |  | 
|---|
|  | 179 | namespace _core_bits { | 
|---|
|  | 180 |  | 
|---|
|  | 181 | template <typename Graph, typename Enable = void> | 
|---|
|  | 182 | struct CountNodesSelector { | 
|---|
|  | 183 | static int count(const Graph &g) { | 
|---|
|  | 184 | return countItems<Graph, typename Graph::Node>(g); | 
|---|
|  | 185 | } | 
|---|
|  | 186 | }; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | template <typename Graph> | 
|---|
|  | 189 | struct CountNodesSelector< | 
|---|
|  | 190 | Graph, typename | 
|---|
|  | 191 | enable_if<typename Graph::NodeNumTag, void>::type> | 
|---|
|  | 192 | { | 
|---|
|  | 193 | static int count(const Graph &g) { | 
|---|
|  | 194 | return g.nodeNum(); | 
|---|
|  | 195 | } | 
|---|
|  | 196 | }; | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
|  | 199 | /// \brief Function to count the nodes in the graph. | 
|---|
|  | 200 | /// | 
|---|
|  | 201 | /// This function counts the nodes in the graph. | 
|---|
| [282] | 202 | /// The complexity of the function is <em>O</em>(<em>n</em>), but for some | 
|---|
|  | 203 | /// graph structures it is specialized to run in <em>O</em>(1). | 
|---|
| [220] | 204 | /// | 
|---|
| [282] | 205 | /// \note If the graph contains a \c nodeNum() member function and a | 
|---|
|  | 206 | /// \c NodeNumTag tag then this function calls directly the member | 
|---|
| [220] | 207 | /// function to query the cardinality of the node set. | 
|---|
|  | 208 | template <typename Graph> | 
|---|
|  | 209 | inline int countNodes(const Graph& g) { | 
|---|
|  | 210 | return _core_bits::CountNodesSelector<Graph>::count(g); | 
|---|
|  | 211 | } | 
|---|
|  | 212 |  | 
|---|
|  | 213 | // Arc counting: | 
|---|
|  | 214 |  | 
|---|
|  | 215 | namespace _core_bits { | 
|---|
|  | 216 |  | 
|---|
|  | 217 | template <typename Graph, typename Enable = void> | 
|---|
|  | 218 | struct CountArcsSelector { | 
|---|
|  | 219 | static int count(const Graph &g) { | 
|---|
|  | 220 | return countItems<Graph, typename Graph::Arc>(g); | 
|---|
|  | 221 | } | 
|---|
|  | 222 | }; | 
|---|
|  | 223 |  | 
|---|
|  | 224 | template <typename Graph> | 
|---|
|  | 225 | struct CountArcsSelector< | 
|---|
|  | 226 | Graph, | 
|---|
|  | 227 | typename enable_if<typename Graph::ArcNumTag, void>::type> | 
|---|
|  | 228 | { | 
|---|
|  | 229 | static int count(const Graph &g) { | 
|---|
|  | 230 | return g.arcNum(); | 
|---|
|  | 231 | } | 
|---|
|  | 232 | }; | 
|---|
|  | 233 | } | 
|---|
|  | 234 |  | 
|---|
|  | 235 | /// \brief Function to count the arcs in the graph. | 
|---|
|  | 236 | /// | 
|---|
|  | 237 | /// This function counts the arcs in the graph. | 
|---|
| [282] | 238 | /// The complexity of the function is <em>O</em>(<em>m</em>), but for some | 
|---|
|  | 239 | /// graph structures it is specialized to run in <em>O</em>(1). | 
|---|
| [220] | 240 | /// | 
|---|
| [282] | 241 | /// \note If the graph contains a \c arcNum() member function and a | 
|---|
|  | 242 | /// \c ArcNumTag tag then this function calls directly the member | 
|---|
| [220] | 243 | /// function to query the cardinality of the arc set. | 
|---|
|  | 244 | template <typename Graph> | 
|---|
|  | 245 | inline int countArcs(const Graph& g) { | 
|---|
|  | 246 | return _core_bits::CountArcsSelector<Graph>::count(g); | 
|---|
|  | 247 | } | 
|---|
|  | 248 |  | 
|---|
|  | 249 | // Edge counting: | 
|---|
| [282] | 250 |  | 
|---|
| [220] | 251 | namespace _core_bits { | 
|---|
|  | 252 |  | 
|---|
|  | 253 | template <typename Graph, typename Enable = void> | 
|---|
|  | 254 | struct CountEdgesSelector { | 
|---|
|  | 255 | static int count(const Graph &g) { | 
|---|
|  | 256 | return countItems<Graph, typename Graph::Edge>(g); | 
|---|
|  | 257 | } | 
|---|
|  | 258 | }; | 
|---|
|  | 259 |  | 
|---|
|  | 260 | template <typename Graph> | 
|---|
|  | 261 | struct CountEdgesSelector< | 
|---|
|  | 262 | Graph, | 
|---|
|  | 263 | typename enable_if<typename Graph::EdgeNumTag, void>::type> | 
|---|
|  | 264 | { | 
|---|
|  | 265 | static int count(const Graph &g) { | 
|---|
|  | 266 | return g.edgeNum(); | 
|---|
|  | 267 | } | 
|---|
|  | 268 | }; | 
|---|
|  | 269 | } | 
|---|
|  | 270 |  | 
|---|
|  | 271 | /// \brief Function to count the edges in the graph. | 
|---|
|  | 272 | /// | 
|---|
|  | 273 | /// This function counts the edges in the graph. | 
|---|
| [282] | 274 | /// The complexity of the function is <em>O</em>(<em>m</em>), but for some | 
|---|
|  | 275 | /// graph structures it is specialized to run in <em>O</em>(1). | 
|---|
| [220] | 276 | /// | 
|---|
| [282] | 277 | /// \note If the graph contains a \c edgeNum() member function and a | 
|---|
|  | 278 | /// \c EdgeNumTag tag then this function calls directly the member | 
|---|
| [220] | 279 | /// function to query the cardinality of the edge set. | 
|---|
|  | 280 | template <typename Graph> | 
|---|
|  | 281 | inline int countEdges(const Graph& g) { | 
|---|
|  | 282 | return _core_bits::CountEdgesSelector<Graph>::count(g); | 
|---|
|  | 283 |  | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 |  | 
|---|
|  | 287 | template <typename Graph, typename DegIt> | 
|---|
|  | 288 | inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { | 
|---|
|  | 289 | int num = 0; | 
|---|
|  | 290 | for (DegIt it(_g, _n); it != INVALID; ++it) { | 
|---|
|  | 291 | ++num; | 
|---|
|  | 292 | } | 
|---|
|  | 293 | return num; | 
|---|
|  | 294 | } | 
|---|
|  | 295 |  | 
|---|
|  | 296 | /// \brief Function to count the number of the out-arcs from node \c n. | 
|---|
|  | 297 | /// | 
|---|
|  | 298 | /// This function counts the number of the out-arcs from node \c n | 
|---|
| [282] | 299 | /// in the graph \c g. | 
|---|
| [220] | 300 | template <typename Graph> | 
|---|
| [282] | 301 | inline int countOutArcs(const Graph& g,  const typename Graph::Node& n) { | 
|---|
|  | 302 | return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); | 
|---|
| [220] | 303 | } | 
|---|
|  | 304 |  | 
|---|
|  | 305 | /// \brief Function to count the number of the in-arcs to node \c n. | 
|---|
|  | 306 | /// | 
|---|
|  | 307 | /// This function counts the number of the in-arcs to node \c n | 
|---|
| [282] | 308 | /// in the graph \c g. | 
|---|
| [220] | 309 | template <typename Graph> | 
|---|
| [282] | 310 | inline int countInArcs(const Graph& g,  const typename Graph::Node& n) { | 
|---|
|  | 311 | return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); | 
|---|
| [220] | 312 | } | 
|---|
|  | 313 |  | 
|---|
|  | 314 | /// \brief Function to count the number of the inc-edges to node \c n. | 
|---|
|  | 315 | /// | 
|---|
|  | 316 | /// This function counts the number of the inc-edges to node \c n | 
|---|
| [282] | 317 | /// in the undirected graph \c g. | 
|---|
| [220] | 318 | template <typename Graph> | 
|---|
| [282] | 319 | inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) { | 
|---|
|  | 320 | return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); | 
|---|
| [220] | 321 | } | 
|---|
|  | 322 |  | 
|---|
|  | 323 | namespace _core_bits { | 
|---|
|  | 324 |  | 
|---|
|  | 325 | template <typename Digraph, typename Item, typename RefMap> | 
|---|
|  | 326 | class MapCopyBase { | 
|---|
|  | 327 | public: | 
|---|
|  | 328 | virtual void copy(const Digraph& from, const RefMap& refMap) = 0; | 
|---|
|  | 329 |  | 
|---|
|  | 330 | virtual ~MapCopyBase() {} | 
|---|
|  | 331 | }; | 
|---|
|  | 332 |  | 
|---|
|  | 333 | template <typename Digraph, typename Item, typename RefMap, | 
|---|
| [282] | 334 | typename FromMap, typename ToMap> | 
|---|
| [220] | 335 | class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { | 
|---|
|  | 336 | public: | 
|---|
|  | 337 |  | 
|---|
| [282] | 338 | MapCopy(const FromMap& map, ToMap& tmap) | 
|---|
|  | 339 | : _map(map), _tmap(tmap) {} | 
|---|
| [220] | 340 |  | 
|---|
|  | 341 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { | 
|---|
|  | 342 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; | 
|---|
|  | 343 | for (ItemIt it(digraph); it != INVALID; ++it) { | 
|---|
|  | 344 | _tmap.set(refMap[it], _map[it]); | 
|---|
|  | 345 | } | 
|---|
|  | 346 | } | 
|---|
|  | 347 |  | 
|---|
|  | 348 | private: | 
|---|
| [282] | 349 | const FromMap& _map; | 
|---|
| [220] | 350 | ToMap& _tmap; | 
|---|
|  | 351 | }; | 
|---|
|  | 352 |  | 
|---|
|  | 353 | template <typename Digraph, typename Item, typename RefMap, typename It> | 
|---|
|  | 354 | class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { | 
|---|
|  | 355 | public: | 
|---|
|  | 356 |  | 
|---|
| [282] | 357 | ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} | 
|---|
| [220] | 358 |  | 
|---|
|  | 359 | virtual void copy(const Digraph&, const RefMap& refMap) { | 
|---|
|  | 360 | _it = refMap[_item]; | 
|---|
|  | 361 | } | 
|---|
|  | 362 |  | 
|---|
|  | 363 | private: | 
|---|
| [282] | 364 | Item _item; | 
|---|
| [220] | 365 | It& _it; | 
|---|
|  | 366 | }; | 
|---|
|  | 367 |  | 
|---|
|  | 368 | template <typename Digraph, typename Item, typename RefMap, typename Ref> | 
|---|
|  | 369 | class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { | 
|---|
|  | 370 | public: | 
|---|
|  | 371 |  | 
|---|
|  | 372 | RefCopy(Ref& map) : _map(map) {} | 
|---|
|  | 373 |  | 
|---|
|  | 374 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { | 
|---|
|  | 375 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; | 
|---|
|  | 376 | for (ItemIt it(digraph); it != INVALID; ++it) { | 
|---|
|  | 377 | _map.set(it, refMap[it]); | 
|---|
|  | 378 | } | 
|---|
|  | 379 | } | 
|---|
|  | 380 |  | 
|---|
|  | 381 | private: | 
|---|
|  | 382 | Ref& _map; | 
|---|
|  | 383 | }; | 
|---|
|  | 384 |  | 
|---|
|  | 385 | template <typename Digraph, typename Item, typename RefMap, | 
|---|
|  | 386 | typename CrossRef> | 
|---|
|  | 387 | class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { | 
|---|
|  | 388 | public: | 
|---|
|  | 389 |  | 
|---|
|  | 390 | CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} | 
|---|
|  | 391 |  | 
|---|
|  | 392 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { | 
|---|
|  | 393 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; | 
|---|
|  | 394 | for (ItemIt it(digraph); it != INVALID; ++it) { | 
|---|
|  | 395 | _cmap.set(refMap[it], it); | 
|---|
|  | 396 | } | 
|---|
|  | 397 | } | 
|---|
|  | 398 |  | 
|---|
|  | 399 | private: | 
|---|
|  | 400 | CrossRef& _cmap; | 
|---|
|  | 401 | }; | 
|---|
|  | 402 |  | 
|---|
|  | 403 | template <typename Digraph, typename Enable = void> | 
|---|
|  | 404 | struct DigraphCopySelector { | 
|---|
|  | 405 | template <typename From, typename NodeRefMap, typename ArcRefMap> | 
|---|
| [282] | 406 | static void copy(const From& from, Digraph &to, | 
|---|
| [220] | 407 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { | 
|---|
| [890] | 408 | to.clear(); | 
|---|
| [220] | 409 | for (typename From::NodeIt it(from); it != INVALID; ++it) { | 
|---|
|  | 410 | nodeRefMap[it] = to.addNode(); | 
|---|
|  | 411 | } | 
|---|
|  | 412 | for (typename From::ArcIt it(from); it != INVALID; ++it) { | 
|---|
|  | 413 | arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], | 
|---|
|  | 414 | nodeRefMap[from.target(it)]); | 
|---|
|  | 415 | } | 
|---|
|  | 416 | } | 
|---|
|  | 417 | }; | 
|---|
|  | 418 |  | 
|---|
|  | 419 | template <typename Digraph> | 
|---|
|  | 420 | struct DigraphCopySelector< | 
|---|
|  | 421 | Digraph, | 
|---|
|  | 422 | typename enable_if<typename Digraph::BuildTag, void>::type> | 
|---|
|  | 423 | { | 
|---|
|  | 424 | template <typename From, typename NodeRefMap, typename ArcRefMap> | 
|---|
| [282] | 425 | static void copy(const From& from, Digraph &to, | 
|---|
| [220] | 426 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { | 
|---|
|  | 427 | to.build(from, nodeRefMap, arcRefMap); | 
|---|
|  | 428 | } | 
|---|
|  | 429 | }; | 
|---|
|  | 430 |  | 
|---|
|  | 431 | template <typename Graph, typename Enable = void> | 
|---|
|  | 432 | struct GraphCopySelector { | 
|---|
|  | 433 | template <typename From, typename NodeRefMap, typename EdgeRefMap> | 
|---|
| [282] | 434 | static void copy(const From& from, Graph &to, | 
|---|
| [220] | 435 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { | 
|---|
| [890] | 436 | to.clear(); | 
|---|
| [220] | 437 | for (typename From::NodeIt it(from); it != INVALID; ++it) { | 
|---|
|  | 438 | nodeRefMap[it] = to.addNode(); | 
|---|
|  | 439 | } | 
|---|
|  | 440 | for (typename From::EdgeIt it(from); it != INVALID; ++it) { | 
|---|
|  | 441 | edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], | 
|---|
|  | 442 | nodeRefMap[from.v(it)]); | 
|---|
|  | 443 | } | 
|---|
|  | 444 | } | 
|---|
|  | 445 | }; | 
|---|
|  | 446 |  | 
|---|
|  | 447 | template <typename Graph> | 
|---|
|  | 448 | struct GraphCopySelector< | 
|---|
|  | 449 | Graph, | 
|---|
|  | 450 | typename enable_if<typename Graph::BuildTag, void>::type> | 
|---|
|  | 451 | { | 
|---|
|  | 452 | template <typename From, typename NodeRefMap, typename EdgeRefMap> | 
|---|
| [282] | 453 | static void copy(const From& from, Graph &to, | 
|---|
| [220] | 454 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { | 
|---|
|  | 455 | to.build(from, nodeRefMap, edgeRefMap); | 
|---|
|  | 456 | } | 
|---|
|  | 457 | }; | 
|---|
|  | 458 |  | 
|---|
|  | 459 | } | 
|---|
|  | 460 |  | 
|---|
|  | 461 | /// \brief Class to copy a digraph. | 
|---|
|  | 462 | /// | 
|---|
|  | 463 | /// Class to copy a digraph to another digraph (duplicate a digraph). The | 
|---|
| [282] | 464 | /// simplest way of using it is through the \c digraphCopy() function. | 
|---|
| [220] | 465 | /// | 
|---|
| [282] | 466 | /// This class not only make a copy of a digraph, but it can create | 
|---|
| [220] | 467 | /// references and cross references between the nodes and arcs of | 
|---|
| [282] | 468 | /// the two digraphs, and it can copy maps to use with the newly created | 
|---|
|  | 469 | /// digraph. | 
|---|
| [220] | 470 | /// | 
|---|
| [282] | 471 | /// To make a copy from a digraph, first an instance of DigraphCopy | 
|---|
|  | 472 | /// should be created, then the data belongs to the digraph should | 
|---|
| [220] | 473 | /// assigned to copy. In the end, the \c run() member should be | 
|---|
|  | 474 | /// called. | 
|---|
|  | 475 | /// | 
|---|
| [282] | 476 | /// The next code copies a digraph with several data: | 
|---|
| [220] | 477 | ///\code | 
|---|
| [282] | 478 | ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); | 
|---|
|  | 479 | ///  // Create references for the nodes | 
|---|
| [220] | 480 | ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); | 
|---|
| [282] | 481 | ///  cg.nodeRef(nr); | 
|---|
|  | 482 | ///  // Create cross references (inverse) for the arcs | 
|---|
| [220] | 483 | ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); | 
|---|
| [282] | 484 | ///  cg.arcCrossRef(acr); | 
|---|
|  | 485 | ///  // Copy an arc map | 
|---|
| [220] | 486 | ///  OrigGraph::ArcMap<double> oamap(orig_graph); | 
|---|
|  | 487 | ///  NewGraph::ArcMap<double> namap(new_graph); | 
|---|
| [282] | 488 | ///  cg.arcMap(oamap, namap); | 
|---|
|  | 489 | ///  // Copy a node | 
|---|
| [220] | 490 | ///  OrigGraph::Node on; | 
|---|
|  | 491 | ///  NewGraph::Node nn; | 
|---|
| [282] | 492 | ///  cg.node(on, nn); | 
|---|
|  | 493 | ///  // Execute copying | 
|---|
|  | 494 | ///  cg.run(); | 
|---|
| [220] | 495 | ///\endcode | 
|---|
| [282] | 496 | template <typename From, typename To> | 
|---|
| [220] | 497 | class DigraphCopy { | 
|---|
|  | 498 | private: | 
|---|
|  | 499 |  | 
|---|
|  | 500 | typedef typename From::Node Node; | 
|---|
|  | 501 | typedef typename From::NodeIt NodeIt; | 
|---|
|  | 502 | typedef typename From::Arc Arc; | 
|---|
|  | 503 | typedef typename From::ArcIt ArcIt; | 
|---|
|  | 504 |  | 
|---|
|  | 505 | typedef typename To::Node TNode; | 
|---|
|  | 506 | typedef typename To::Arc TArc; | 
|---|
|  | 507 |  | 
|---|
|  | 508 | typedef typename From::template NodeMap<TNode> NodeRefMap; | 
|---|
|  | 509 | typedef typename From::template ArcMap<TArc> ArcRefMap; | 
|---|
|  | 510 |  | 
|---|
|  | 511 | public: | 
|---|
|  | 512 |  | 
|---|
| [282] | 513 | /// \brief Constructor of DigraphCopy. | 
|---|
| [220] | 514 | /// | 
|---|
| [282] | 515 | /// Constructor of DigraphCopy for copying the content of the | 
|---|
|  | 516 | /// \c from digraph into the \c to digraph. | 
|---|
|  | 517 | DigraphCopy(const From& from, To& to) | 
|---|
| [220] | 518 | : _from(from), _to(to) {} | 
|---|
|  | 519 |  | 
|---|
| [282] | 520 | /// \brief Destructor of DigraphCopy | 
|---|
| [220] | 521 | /// | 
|---|
| [282] | 522 | /// Destructor of DigraphCopy. | 
|---|
| [220] | 523 | ~DigraphCopy() { | 
|---|
|  | 524 | for (int i = 0; i < int(_node_maps.size()); ++i) { | 
|---|
|  | 525 | delete _node_maps[i]; | 
|---|
|  | 526 | } | 
|---|
|  | 527 | for (int i = 0; i < int(_arc_maps.size()); ++i) { | 
|---|
|  | 528 | delete _arc_maps[i]; | 
|---|
|  | 529 | } | 
|---|
|  | 530 |  | 
|---|
|  | 531 | } | 
|---|
|  | 532 |  | 
|---|
| [282] | 533 | /// \brief Copy the node references into the given map. | 
|---|
| [220] | 534 | /// | 
|---|
| [282] | 535 | /// This function copies the node references into the given map. | 
|---|
|  | 536 | /// The parameter should be a map, whose key type is the Node type of | 
|---|
|  | 537 | /// the source digraph, while the value type is the Node type of the | 
|---|
|  | 538 | /// destination digraph. | 
|---|
| [220] | 539 | template <typename NodeRef> | 
|---|
|  | 540 | DigraphCopy& nodeRef(NodeRef& map) { | 
|---|
|  | 541 | _node_maps.push_back(new _core_bits::RefCopy<From, Node, | 
|---|
|  | 542 | NodeRefMap, NodeRef>(map)); | 
|---|
|  | 543 | return *this; | 
|---|
|  | 544 | } | 
|---|
|  | 545 |  | 
|---|
| [282] | 546 | /// \brief Copy the node cross references into the given map. | 
|---|
| [220] | 547 | /// | 
|---|
| [282] | 548 | /// This function copies the node cross references (reverse references) | 
|---|
|  | 549 | /// into the given map. The parameter should be a map, whose key type | 
|---|
|  | 550 | /// is the Node type of the destination digraph, while the value type is | 
|---|
|  | 551 | /// the Node type of the source digraph. | 
|---|
| [220] | 552 | template <typename NodeCrossRef> | 
|---|
|  | 553 | DigraphCopy& nodeCrossRef(NodeCrossRef& map) { | 
|---|
|  | 554 | _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, | 
|---|
|  | 555 | NodeRefMap, NodeCrossRef>(map)); | 
|---|
|  | 556 | return *this; | 
|---|
|  | 557 | } | 
|---|
|  | 558 |  | 
|---|
| [282] | 559 | /// \brief Make a copy of the given node map. | 
|---|
| [220] | 560 | /// | 
|---|
| [282] | 561 | /// This function makes a copy of the given node map for the newly | 
|---|
|  | 562 | /// created digraph. | 
|---|
|  | 563 | /// The key type of the new map \c tmap should be the Node type of the | 
|---|
|  | 564 | /// destination digraph, and the key type of the original map \c map | 
|---|
|  | 565 | /// should be the Node type of the source digraph. | 
|---|
|  | 566 | template <typename FromMap, typename ToMap> | 
|---|
|  | 567 | DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { | 
|---|
| [220] | 568 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, | 
|---|
| [282] | 569 | NodeRefMap, FromMap, ToMap>(map, tmap)); | 
|---|
| [220] | 570 | return *this; | 
|---|
|  | 571 | } | 
|---|
|  | 572 |  | 
|---|
|  | 573 | /// \brief Make a copy of the given node. | 
|---|
|  | 574 | /// | 
|---|
| [282] | 575 | /// This function makes a copy of the given node. | 
|---|
|  | 576 | DigraphCopy& node(const Node& node, TNode& tnode) { | 
|---|
| [220] | 577 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, | 
|---|
| [282] | 578 | NodeRefMap, TNode>(node, tnode)); | 
|---|
| [220] | 579 | return *this; | 
|---|
|  | 580 | } | 
|---|
|  | 581 |  | 
|---|
| [282] | 582 | /// \brief Copy the arc references into the given map. | 
|---|
| [220] | 583 | /// | 
|---|
| [282] | 584 | /// This function copies the arc references into the given map. | 
|---|
|  | 585 | /// The parameter should be a map, whose key type is the Arc type of | 
|---|
|  | 586 | /// the source digraph, while the value type is the Arc type of the | 
|---|
|  | 587 | /// destination digraph. | 
|---|
| [220] | 588 | template <typename ArcRef> | 
|---|
|  | 589 | DigraphCopy& arcRef(ArcRef& map) { | 
|---|
|  | 590 | _arc_maps.push_back(new _core_bits::RefCopy<From, Arc, | 
|---|
|  | 591 | ArcRefMap, ArcRef>(map)); | 
|---|
|  | 592 | return *this; | 
|---|
|  | 593 | } | 
|---|
|  | 594 |  | 
|---|
| [282] | 595 | /// \brief Copy the arc cross references into the given map. | 
|---|
| [220] | 596 | /// | 
|---|
| [282] | 597 | /// This function copies the arc cross references (reverse references) | 
|---|
|  | 598 | /// into the given map. The parameter should be a map, whose key type | 
|---|
|  | 599 | /// is the Arc type of the destination digraph, while the value type is | 
|---|
|  | 600 | /// the Arc type of the source digraph. | 
|---|
| [220] | 601 | template <typename ArcCrossRef> | 
|---|
|  | 602 | DigraphCopy& arcCrossRef(ArcCrossRef& map) { | 
|---|
|  | 603 | _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, | 
|---|
|  | 604 | ArcRefMap, ArcCrossRef>(map)); | 
|---|
|  | 605 | return *this; | 
|---|
|  | 606 | } | 
|---|
|  | 607 |  | 
|---|
| [282] | 608 | /// \brief Make a copy of the given arc map. | 
|---|
| [220] | 609 | /// | 
|---|
| [282] | 610 | /// This function makes a copy of the given arc map for the newly | 
|---|
|  | 611 | /// created digraph. | 
|---|
|  | 612 | /// The key type of the new map \c tmap should be the Arc type of the | 
|---|
|  | 613 | /// destination digraph, and the key type of the original map \c map | 
|---|
|  | 614 | /// should be the Arc type of the source digraph. | 
|---|
|  | 615 | template <typename FromMap, typename ToMap> | 
|---|
|  | 616 | DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { | 
|---|
| [220] | 617 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, | 
|---|
| [282] | 618 | ArcRefMap, FromMap, ToMap>(map, tmap)); | 
|---|
| [220] | 619 | return *this; | 
|---|
|  | 620 | } | 
|---|
|  | 621 |  | 
|---|
|  | 622 | /// \brief Make a copy of the given arc. | 
|---|
|  | 623 | /// | 
|---|
| [282] | 624 | /// This function makes a copy of the given arc. | 
|---|
|  | 625 | DigraphCopy& arc(const Arc& arc, TArc& tarc) { | 
|---|
| [220] | 626 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, | 
|---|
| [282] | 627 | ArcRefMap, TArc>(arc, tarc)); | 
|---|
| [220] | 628 | return *this; | 
|---|
|  | 629 | } | 
|---|
|  | 630 |  | 
|---|
| [282] | 631 | /// \brief Execute copying. | 
|---|
| [220] | 632 | /// | 
|---|
| [282] | 633 | /// This function executes the copying of the digraph along with the | 
|---|
|  | 634 | /// copying of the assigned data. | 
|---|
| [220] | 635 | void run() { | 
|---|
|  | 636 | NodeRefMap nodeRefMap(_from); | 
|---|
|  | 637 | ArcRefMap arcRefMap(_from); | 
|---|
|  | 638 | _core_bits::DigraphCopySelector<To>:: | 
|---|
| [282] | 639 | copy(_from, _to, nodeRefMap, arcRefMap); | 
|---|
| [220] | 640 | for (int i = 0; i < int(_node_maps.size()); ++i) { | 
|---|
|  | 641 | _node_maps[i]->copy(_from, nodeRefMap); | 
|---|
|  | 642 | } | 
|---|
|  | 643 | for (int i = 0; i < int(_arc_maps.size()); ++i) { | 
|---|
|  | 644 | _arc_maps[i]->copy(_from, arcRefMap); | 
|---|
|  | 645 | } | 
|---|
|  | 646 | } | 
|---|
|  | 647 |  | 
|---|
|  | 648 | protected: | 
|---|
|  | 649 |  | 
|---|
|  | 650 | const From& _from; | 
|---|
|  | 651 | To& _to; | 
|---|
|  | 652 |  | 
|---|
|  | 653 | std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > | 
|---|
| [282] | 654 | _node_maps; | 
|---|
| [220] | 655 |  | 
|---|
|  | 656 | std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > | 
|---|
| [282] | 657 | _arc_maps; | 
|---|
| [220] | 658 |  | 
|---|
|  | 659 | }; | 
|---|
|  | 660 |  | 
|---|
|  | 661 | /// \brief Copy a digraph to another digraph. | 
|---|
|  | 662 | /// | 
|---|
| [282] | 663 | /// This function copies a digraph to another digraph. | 
|---|
|  | 664 | /// The complete usage of it is detailed in the DigraphCopy class, but | 
|---|
|  | 665 | /// a short example shows a basic work: | 
|---|
| [220] | 666 | ///\code | 
|---|
| [282] | 667 | /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); | 
|---|
| [220] | 668 | ///\endcode | 
|---|
|  | 669 | /// | 
|---|
|  | 670 | /// After the copy the \c nr map will contain the mapping from the | 
|---|
|  | 671 | /// nodes of the \c from digraph to the nodes of the \c to digraph and | 
|---|
| [282] | 672 | /// \c acr will contain the mapping from the arcs of the \c to digraph | 
|---|
| [220] | 673 | /// to the arcs of the \c from digraph. | 
|---|
|  | 674 | /// | 
|---|
|  | 675 | /// \see DigraphCopy | 
|---|
| [282] | 676 | template <typename From, typename To> | 
|---|
|  | 677 | DigraphCopy<From, To> digraphCopy(const From& from, To& to) { | 
|---|
|  | 678 | return DigraphCopy<From, To>(from, to); | 
|---|
| [220] | 679 | } | 
|---|
|  | 680 |  | 
|---|
|  | 681 | /// \brief Class to copy a graph. | 
|---|
|  | 682 | /// | 
|---|
|  | 683 | /// Class to copy a graph to another graph (duplicate a graph). The | 
|---|
| [282] | 684 | /// simplest way of using it is through the \c graphCopy() function. | 
|---|
| [220] | 685 | /// | 
|---|
| [282] | 686 | /// This class not only make a copy of a graph, but it can create | 
|---|
| [220] | 687 | /// references and cross references between the nodes, edges and arcs of | 
|---|
| [282] | 688 | /// the two graphs, and it can copy maps for using with the newly created | 
|---|
|  | 689 | /// graph. | 
|---|
| [220] | 690 | /// | 
|---|
|  | 691 | /// To make a copy from a graph, first an instance of GraphCopy | 
|---|
|  | 692 | /// should be created, then the data belongs to the graph should | 
|---|
|  | 693 | /// assigned to copy. In the end, the \c run() member should be | 
|---|
|  | 694 | /// called. | 
|---|
|  | 695 | /// | 
|---|
|  | 696 | /// The next code copies a graph with several data: | 
|---|
|  | 697 | ///\code | 
|---|
| [282] | 698 | ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); | 
|---|
|  | 699 | ///  // Create references for the nodes | 
|---|
| [220] | 700 | ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); | 
|---|
| [282] | 701 | ///  cg.nodeRef(nr); | 
|---|
|  | 702 | ///  // Create cross references (inverse) for the edges | 
|---|
|  | 703 | ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); | 
|---|
|  | 704 | ///  cg.edgeCrossRef(ecr); | 
|---|
|  | 705 | ///  // Copy an edge map | 
|---|
|  | 706 | ///  OrigGraph::EdgeMap<double> oemap(orig_graph); | 
|---|
|  | 707 | ///  NewGraph::EdgeMap<double> nemap(new_graph); | 
|---|
|  | 708 | ///  cg.edgeMap(oemap, nemap); | 
|---|
|  | 709 | ///  // Copy a node | 
|---|
| [220] | 710 | ///  OrigGraph::Node on; | 
|---|
|  | 711 | ///  NewGraph::Node nn; | 
|---|
| [282] | 712 | ///  cg.node(on, nn); | 
|---|
|  | 713 | ///  // Execute copying | 
|---|
|  | 714 | ///  cg.run(); | 
|---|
| [220] | 715 | ///\endcode | 
|---|
| [282] | 716 | template <typename From, typename To> | 
|---|
| [220] | 717 | class GraphCopy { | 
|---|
|  | 718 | private: | 
|---|
|  | 719 |  | 
|---|
|  | 720 | typedef typename From::Node Node; | 
|---|
|  | 721 | typedef typename From::NodeIt NodeIt; | 
|---|
|  | 722 | typedef typename From::Arc Arc; | 
|---|
|  | 723 | typedef typename From::ArcIt ArcIt; | 
|---|
|  | 724 | typedef typename From::Edge Edge; | 
|---|
|  | 725 | typedef typename From::EdgeIt EdgeIt; | 
|---|
|  | 726 |  | 
|---|
|  | 727 | typedef typename To::Node TNode; | 
|---|
|  | 728 | typedef typename To::Arc TArc; | 
|---|
|  | 729 | typedef typename To::Edge TEdge; | 
|---|
|  | 730 |  | 
|---|
|  | 731 | typedef typename From::template NodeMap<TNode> NodeRefMap; | 
|---|
|  | 732 | typedef typename From::template EdgeMap<TEdge> EdgeRefMap; | 
|---|
|  | 733 |  | 
|---|
|  | 734 | struct ArcRefMap { | 
|---|
| [282] | 735 | ArcRefMap(const From& from, const To& to, | 
|---|
| [220] | 736 | const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) | 
|---|
| [282] | 737 | : _from(from), _to(to), | 
|---|
| [220] | 738 | _edge_ref(edge_ref), _node_ref(node_ref) {} | 
|---|
|  | 739 |  | 
|---|
|  | 740 | typedef typename From::Arc Key; | 
|---|
|  | 741 | typedef typename To::Arc Value; | 
|---|
|  | 742 |  | 
|---|
|  | 743 | Value operator[](const Key& key) const { | 
|---|
|  | 744 | bool forward = _from.u(key) != _from.v(key) ? | 
|---|
|  | 745 | _node_ref[_from.source(key)] == | 
|---|
|  | 746 | _to.source(_to.direct(_edge_ref[key], true)) : | 
|---|
|  | 747 | _from.direction(key); | 
|---|
|  | 748 | return _to.direct(_edge_ref[key], forward); | 
|---|
|  | 749 | } | 
|---|
|  | 750 |  | 
|---|
| [282] | 751 | const From& _from; | 
|---|
| [220] | 752 | const To& _to; | 
|---|
|  | 753 | const EdgeRefMap& _edge_ref; | 
|---|
|  | 754 | const NodeRefMap& _node_ref; | 
|---|
|  | 755 | }; | 
|---|
|  | 756 |  | 
|---|
|  | 757 | public: | 
|---|
|  | 758 |  | 
|---|
| [282] | 759 | /// \brief Constructor of GraphCopy. | 
|---|
| [220] | 760 | /// | 
|---|
| [282] | 761 | /// Constructor of GraphCopy for copying the content of the | 
|---|
|  | 762 | /// \c from graph into the \c to graph. | 
|---|
|  | 763 | GraphCopy(const From& from, To& to) | 
|---|
| [220] | 764 | : _from(from), _to(to) {} | 
|---|
|  | 765 |  | 
|---|
| [282] | 766 | /// \brief Destructor of GraphCopy | 
|---|
| [220] | 767 | /// | 
|---|
| [282] | 768 | /// Destructor of GraphCopy. | 
|---|
| [220] | 769 | ~GraphCopy() { | 
|---|
|  | 770 | for (int i = 0; i < int(_node_maps.size()); ++i) { | 
|---|
|  | 771 | delete _node_maps[i]; | 
|---|
|  | 772 | } | 
|---|
|  | 773 | for (int i = 0; i < int(_arc_maps.size()); ++i) { | 
|---|
|  | 774 | delete _arc_maps[i]; | 
|---|
|  | 775 | } | 
|---|
|  | 776 | for (int i = 0; i < int(_edge_maps.size()); ++i) { | 
|---|
|  | 777 | delete _edge_maps[i]; | 
|---|
|  | 778 | } | 
|---|
|  | 779 | } | 
|---|
|  | 780 |  | 
|---|
| [282] | 781 | /// \brief Copy the node references into the given map. | 
|---|
| [220] | 782 | /// | 
|---|
| [282] | 783 | /// This function copies the node references into the given map. | 
|---|
|  | 784 | /// The parameter should be a map, whose key type is the Node type of | 
|---|
|  | 785 | /// the source graph, while the value type is the Node type of the | 
|---|
|  | 786 | /// destination graph. | 
|---|
| [220] | 787 | template <typename NodeRef> | 
|---|
|  | 788 | GraphCopy& nodeRef(NodeRef& map) { | 
|---|
|  | 789 | _node_maps.push_back(new _core_bits::RefCopy<From, Node, | 
|---|
|  | 790 | NodeRefMap, NodeRef>(map)); | 
|---|
|  | 791 | return *this; | 
|---|
|  | 792 | } | 
|---|
|  | 793 |  | 
|---|
| [282] | 794 | /// \brief Copy the node cross references into the given map. | 
|---|
| [220] | 795 | /// | 
|---|
| [282] | 796 | /// This function copies the node cross references (reverse references) | 
|---|
|  | 797 | /// into the given map. The parameter should be a map, whose key type | 
|---|
|  | 798 | /// is the Node type of the destination graph, while the value type is | 
|---|
|  | 799 | /// the Node type of the source graph. | 
|---|
| [220] | 800 | template <typename NodeCrossRef> | 
|---|
|  | 801 | GraphCopy& nodeCrossRef(NodeCrossRef& map) { | 
|---|
|  | 802 | _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, | 
|---|
|  | 803 | NodeRefMap, NodeCrossRef>(map)); | 
|---|
|  | 804 | return *this; | 
|---|
|  | 805 | } | 
|---|
|  | 806 |  | 
|---|
| [282] | 807 | /// \brief Make a copy of the given node map. | 
|---|
| [220] | 808 | /// | 
|---|
| [282] | 809 | /// This function makes a copy of the given node map for the newly | 
|---|
|  | 810 | /// created graph. | 
|---|
|  | 811 | /// The key type of the new map \c tmap should be the Node type of the | 
|---|
|  | 812 | /// destination graph, and the key type of the original map \c map | 
|---|
|  | 813 | /// should be the Node type of the source graph. | 
|---|
|  | 814 | template <typename FromMap, typename ToMap> | 
|---|
|  | 815 | GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { | 
|---|
| [220] | 816 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, | 
|---|
| [282] | 817 | NodeRefMap, FromMap, ToMap>(map, tmap)); | 
|---|
| [220] | 818 | return *this; | 
|---|
|  | 819 | } | 
|---|
|  | 820 |  | 
|---|
|  | 821 | /// \brief Make a copy of the given node. | 
|---|
|  | 822 | /// | 
|---|
| [282] | 823 | /// This function makes a copy of the given node. | 
|---|
|  | 824 | GraphCopy& node(const Node& node, TNode& tnode) { | 
|---|
| [220] | 825 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, | 
|---|
| [282] | 826 | NodeRefMap, TNode>(node, tnode)); | 
|---|
| [220] | 827 | return *this; | 
|---|
|  | 828 | } | 
|---|
|  | 829 |  | 
|---|
| [282] | 830 | /// \brief Copy the arc references into the given map. | 
|---|
| [220] | 831 | /// | 
|---|
| [282] | 832 | /// This function copies the arc references into the given map. | 
|---|
|  | 833 | /// The parameter should be a map, whose key type is the Arc type of | 
|---|
|  | 834 | /// the source graph, while the value type is the Arc type of the | 
|---|
|  | 835 | /// destination graph. | 
|---|
| [220] | 836 | template <typename ArcRef> | 
|---|
|  | 837 | GraphCopy& arcRef(ArcRef& map) { | 
|---|
|  | 838 | _arc_maps.push_back(new _core_bits::RefCopy<From, Arc, | 
|---|
|  | 839 | ArcRefMap, ArcRef>(map)); | 
|---|
|  | 840 | return *this; | 
|---|
|  | 841 | } | 
|---|
|  | 842 |  | 
|---|
| [282] | 843 | /// \brief Copy the arc cross references into the given map. | 
|---|
| [220] | 844 | /// | 
|---|
| [282] | 845 | /// This function copies the arc cross references (reverse references) | 
|---|
|  | 846 | /// into the given map. The parameter should be a map, whose key type | 
|---|
|  | 847 | /// is the Arc type of the destination graph, while the value type is | 
|---|
|  | 848 | /// the Arc type of the source graph. | 
|---|
| [220] | 849 | template <typename ArcCrossRef> | 
|---|
|  | 850 | GraphCopy& arcCrossRef(ArcCrossRef& map) { | 
|---|
|  | 851 | _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, | 
|---|
|  | 852 | ArcRefMap, ArcCrossRef>(map)); | 
|---|
|  | 853 | return *this; | 
|---|
|  | 854 | } | 
|---|
|  | 855 |  | 
|---|
| [282] | 856 | /// \brief Make a copy of the given arc map. | 
|---|
| [220] | 857 | /// | 
|---|
| [282] | 858 | /// This function makes a copy of the given arc map for the newly | 
|---|
|  | 859 | /// created graph. | 
|---|
|  | 860 | /// The key type of the new map \c tmap should be the Arc type of the | 
|---|
|  | 861 | /// destination graph, and the key type of the original map \c map | 
|---|
|  | 862 | /// should be the Arc type of the source graph. | 
|---|
|  | 863 | template <typename FromMap, typename ToMap> | 
|---|
|  | 864 | GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { | 
|---|
| [220] | 865 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, | 
|---|
| [282] | 866 | ArcRefMap, FromMap, ToMap>(map, tmap)); | 
|---|
| [220] | 867 | return *this; | 
|---|
|  | 868 | } | 
|---|
|  | 869 |  | 
|---|
|  | 870 | /// \brief Make a copy of the given arc. | 
|---|
|  | 871 | /// | 
|---|
| [282] | 872 | /// This function makes a copy of the given arc. | 
|---|
|  | 873 | GraphCopy& arc(const Arc& arc, TArc& tarc) { | 
|---|
| [220] | 874 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, | 
|---|
| [282] | 875 | ArcRefMap, TArc>(arc, tarc)); | 
|---|
| [220] | 876 | return *this; | 
|---|
|  | 877 | } | 
|---|
|  | 878 |  | 
|---|
| [282] | 879 | /// \brief Copy the edge references into the given map. | 
|---|
| [220] | 880 | /// | 
|---|
| [282] | 881 | /// This function copies the edge references into the given map. | 
|---|
|  | 882 | /// The parameter should be a map, whose key type is the Edge type of | 
|---|
|  | 883 | /// the source graph, while the value type is the Edge type of the | 
|---|
|  | 884 | /// destination graph. | 
|---|
| [220] | 885 | template <typename EdgeRef> | 
|---|
|  | 886 | GraphCopy& edgeRef(EdgeRef& map) { | 
|---|
|  | 887 | _edge_maps.push_back(new _core_bits::RefCopy<From, Edge, | 
|---|
|  | 888 | EdgeRefMap, EdgeRef>(map)); | 
|---|
|  | 889 | return *this; | 
|---|
|  | 890 | } | 
|---|
|  | 891 |  | 
|---|
| [282] | 892 | /// \brief Copy the edge cross references into the given map. | 
|---|
| [220] | 893 | /// | 
|---|
| [282] | 894 | /// This function copies the edge cross references (reverse references) | 
|---|
|  | 895 | /// into the given map. The parameter should be a map, whose key type | 
|---|
|  | 896 | /// is the Edge type of the destination graph, while the value type is | 
|---|
|  | 897 | /// the Edge type of the source graph. | 
|---|
| [220] | 898 | template <typename EdgeCrossRef> | 
|---|
|  | 899 | GraphCopy& edgeCrossRef(EdgeCrossRef& map) { | 
|---|
|  | 900 | _edge_maps.push_back(new _core_bits::CrossRefCopy<From, | 
|---|
|  | 901 | Edge, EdgeRefMap, EdgeCrossRef>(map)); | 
|---|
|  | 902 | return *this; | 
|---|
|  | 903 | } | 
|---|
|  | 904 |  | 
|---|
| [282] | 905 | /// \brief Make a copy of the given edge map. | 
|---|
| [220] | 906 | /// | 
|---|
| [282] | 907 | /// This function makes a copy of the given edge map for the newly | 
|---|
|  | 908 | /// created graph. | 
|---|
|  | 909 | /// The key type of the new map \c tmap should be the Edge type of the | 
|---|
|  | 910 | /// destination graph, and the key type of the original map \c map | 
|---|
|  | 911 | /// should be the Edge type of the source graph. | 
|---|
|  | 912 | template <typename FromMap, typename ToMap> | 
|---|
|  | 913 | GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { | 
|---|
| [220] | 914 | _edge_maps.push_back(new _core_bits::MapCopy<From, Edge, | 
|---|
| [282] | 915 | EdgeRefMap, FromMap, ToMap>(map, tmap)); | 
|---|
| [220] | 916 | return *this; | 
|---|
|  | 917 | } | 
|---|
|  | 918 |  | 
|---|
|  | 919 | /// \brief Make a copy of the given edge. | 
|---|
|  | 920 | /// | 
|---|
| [282] | 921 | /// This function makes a copy of the given edge. | 
|---|
|  | 922 | GraphCopy& edge(const Edge& edge, TEdge& tedge) { | 
|---|
| [220] | 923 | _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, | 
|---|
| [282] | 924 | EdgeRefMap, TEdge>(edge, tedge)); | 
|---|
| [220] | 925 | return *this; | 
|---|
|  | 926 | } | 
|---|
|  | 927 |  | 
|---|
| [282] | 928 | /// \brief Execute copying. | 
|---|
| [220] | 929 | /// | 
|---|
| [282] | 930 | /// This function executes the copying of the graph along with the | 
|---|
|  | 931 | /// copying of the assigned data. | 
|---|
| [220] | 932 | void run() { | 
|---|
|  | 933 | NodeRefMap nodeRefMap(_from); | 
|---|
|  | 934 | EdgeRefMap edgeRefMap(_from); | 
|---|
| [282] | 935 | ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); | 
|---|
| [220] | 936 | _core_bits::GraphCopySelector<To>:: | 
|---|
| [282] | 937 | copy(_from, _to, nodeRefMap, edgeRefMap); | 
|---|
| [220] | 938 | for (int i = 0; i < int(_node_maps.size()); ++i) { | 
|---|
|  | 939 | _node_maps[i]->copy(_from, nodeRefMap); | 
|---|
|  | 940 | } | 
|---|
|  | 941 | for (int i = 0; i < int(_edge_maps.size()); ++i) { | 
|---|
|  | 942 | _edge_maps[i]->copy(_from, edgeRefMap); | 
|---|
|  | 943 | } | 
|---|
|  | 944 | for (int i = 0; i < int(_arc_maps.size()); ++i) { | 
|---|
|  | 945 | _arc_maps[i]->copy(_from, arcRefMap); | 
|---|
|  | 946 | } | 
|---|
|  | 947 | } | 
|---|
|  | 948 |  | 
|---|
|  | 949 | private: | 
|---|
|  | 950 |  | 
|---|
|  | 951 | const From& _from; | 
|---|
|  | 952 | To& _to; | 
|---|
|  | 953 |  | 
|---|
|  | 954 | std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > | 
|---|
| [282] | 955 | _node_maps; | 
|---|
| [220] | 956 |  | 
|---|
|  | 957 | std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > | 
|---|
| [282] | 958 | _arc_maps; | 
|---|
| [220] | 959 |  | 
|---|
|  | 960 | std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > | 
|---|
| [282] | 961 | _edge_maps; | 
|---|
| [220] | 962 |  | 
|---|
|  | 963 | }; | 
|---|
|  | 964 |  | 
|---|
|  | 965 | /// \brief Copy a graph to another graph. | 
|---|
|  | 966 | /// | 
|---|
| [282] | 967 | /// This function copies a graph to another graph. | 
|---|
|  | 968 | /// The complete usage of it is detailed in the GraphCopy class, | 
|---|
|  | 969 | /// but a short example shows a basic work: | 
|---|
| [220] | 970 | ///\code | 
|---|
| [282] | 971 | /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); | 
|---|
| [220] | 972 | ///\endcode | 
|---|
|  | 973 | /// | 
|---|
|  | 974 | /// After the copy the \c nr map will contain the mapping from the | 
|---|
|  | 975 | /// nodes of the \c from graph to the nodes of the \c to graph and | 
|---|
| [282] | 976 | /// \c ecr will contain the mapping from the edges of the \c to graph | 
|---|
|  | 977 | /// to the edges of the \c from graph. | 
|---|
| [220] | 978 | /// | 
|---|
|  | 979 | /// \see GraphCopy | 
|---|
| [282] | 980 | template <typename From, typename To> | 
|---|
|  | 981 | GraphCopy<From, To> | 
|---|
|  | 982 | graphCopy(const From& from, To& to) { | 
|---|
|  | 983 | return GraphCopy<From, To>(from, to); | 
|---|
| [220] | 984 | } | 
|---|
|  | 985 |  | 
|---|
|  | 986 | namespace _core_bits { | 
|---|
|  | 987 |  | 
|---|
|  | 988 | template <typename Graph, typename Enable = void> | 
|---|
|  | 989 | struct FindArcSelector { | 
|---|
|  | 990 | typedef typename Graph::Node Node; | 
|---|
|  | 991 | typedef typename Graph::Arc Arc; | 
|---|
|  | 992 | static Arc find(const Graph &g, Node u, Node v, Arc e) { | 
|---|
|  | 993 | if (e == INVALID) { | 
|---|
|  | 994 | g.firstOut(e, u); | 
|---|
|  | 995 | } else { | 
|---|
|  | 996 | g.nextOut(e); | 
|---|
|  | 997 | } | 
|---|
|  | 998 | while (e != INVALID && g.target(e) != v) { | 
|---|
|  | 999 | g.nextOut(e); | 
|---|
|  | 1000 | } | 
|---|
|  | 1001 | return e; | 
|---|
|  | 1002 | } | 
|---|
|  | 1003 | }; | 
|---|
|  | 1004 |  | 
|---|
|  | 1005 | template <typename Graph> | 
|---|
|  | 1006 | struct FindArcSelector< | 
|---|
|  | 1007 | Graph, | 
|---|
| [282] | 1008 | typename enable_if<typename Graph::FindArcTag, void>::type> | 
|---|
| [220] | 1009 | { | 
|---|
|  | 1010 | typedef typename Graph::Node Node; | 
|---|
|  | 1011 | typedef typename Graph::Arc Arc; | 
|---|
|  | 1012 | static Arc find(const Graph &g, Node u, Node v, Arc prev) { | 
|---|
|  | 1013 | return g.findArc(u, v, prev); | 
|---|
|  | 1014 | } | 
|---|
|  | 1015 | }; | 
|---|
|  | 1016 | } | 
|---|
|  | 1017 |  | 
|---|
| [282] | 1018 | /// \brief Find an arc between two nodes of a digraph. | 
|---|
| [220] | 1019 | /// | 
|---|
| [282] | 1020 | /// This function finds an arc from node \c u to node \c v in the | 
|---|
|  | 1021 | /// digraph \c g. | 
|---|
| [220] | 1022 | /// | 
|---|
|  | 1023 | /// If \c prev is \ref INVALID (this is the default value), then | 
|---|
|  | 1024 | /// it finds the first arc from \c u to \c v. Otherwise it looks for | 
|---|
|  | 1025 | /// the next arc from \c u to \c v after \c prev. | 
|---|
|  | 1026 | /// \return The found arc or \ref INVALID if there is no such an arc. | 
|---|
|  | 1027 | /// | 
|---|
|  | 1028 | /// Thus you can iterate through each arc from \c u to \c v as it follows. | 
|---|
|  | 1029 | ///\code | 
|---|
| [282] | 1030 | /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { | 
|---|
| [220] | 1031 | ///   ... | 
|---|
|  | 1032 | /// } | 
|---|
|  | 1033 | ///\endcode | 
|---|
|  | 1034 | /// | 
|---|
| [282] | 1035 | /// \note \ref ConArcIt provides iterator interface for the same | 
|---|
|  | 1036 | /// functionality. | 
|---|
|  | 1037 | /// | 
|---|
| [220] | 1038 | ///\sa ConArcIt | 
|---|
| [282] | 1039 | ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp | 
|---|
| [220] | 1040 | template <typename Graph> | 
|---|
|  | 1041 | inline typename Graph::Arc | 
|---|
|  | 1042 | findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, | 
|---|
|  | 1043 | typename Graph::Arc prev = INVALID) { | 
|---|
|  | 1044 | return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); | 
|---|
|  | 1045 | } | 
|---|
|  | 1046 |  | 
|---|
| [282] | 1047 | /// \brief Iterator for iterating on parallel arcs connecting the same nodes. | 
|---|
| [220] | 1048 | /// | 
|---|
| [282] | 1049 | /// Iterator for iterating on parallel arcs connecting the same nodes. It is | 
|---|
|  | 1050 | /// a higher level interface for the \ref findArc() function. You can | 
|---|
| [220] | 1051 | /// use it the following way: | 
|---|
|  | 1052 | ///\code | 
|---|
|  | 1053 | /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { | 
|---|
|  | 1054 | ///   ... | 
|---|
|  | 1055 | /// } | 
|---|
|  | 1056 | ///\endcode | 
|---|
|  | 1057 | /// | 
|---|
|  | 1058 | ///\sa findArc() | 
|---|
| [282] | 1059 | ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp | 
|---|
| [559] | 1060 | template <typename GR> | 
|---|
|  | 1061 | class ConArcIt : public GR::Arc { | 
|---|
| [617] | 1062 | typedef typename GR::Arc Parent; | 
|---|
|  | 1063 |  | 
|---|
| [220] | 1064 | public: | 
|---|
|  | 1065 |  | 
|---|
| [617] | 1066 | typedef typename GR::Arc Arc; | 
|---|
|  | 1067 | typedef typename GR::Node Node; | 
|---|
| [220] | 1068 |  | 
|---|
|  | 1069 | /// \brief Constructor. | 
|---|
|  | 1070 | /// | 
|---|
| [282] | 1071 | /// Construct a new ConArcIt iterating on the arcs that | 
|---|
|  | 1072 | /// connects nodes \c u and \c v. | 
|---|
| [617] | 1073 | ConArcIt(const GR& g, Node u, Node v) : _graph(g) { | 
|---|
| [220] | 1074 | Parent::operator=(findArc(_graph, u, v)); | 
|---|
|  | 1075 | } | 
|---|
|  | 1076 |  | 
|---|
|  | 1077 | /// \brief Constructor. | 
|---|
|  | 1078 | /// | 
|---|
| [282] | 1079 | /// Construct a new ConArcIt that continues the iterating from arc \c a. | 
|---|
| [617] | 1080 | ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {} | 
|---|
| [220] | 1081 |  | 
|---|
|  | 1082 | /// \brief Increment operator. | 
|---|
|  | 1083 | /// | 
|---|
|  | 1084 | /// It increments the iterator and gives back the next arc. | 
|---|
|  | 1085 | ConArcIt& operator++() { | 
|---|
|  | 1086 | Parent::operator=(findArc(_graph, _graph.source(*this), | 
|---|
|  | 1087 | _graph.target(*this), *this)); | 
|---|
|  | 1088 | return *this; | 
|---|
|  | 1089 | } | 
|---|
|  | 1090 | private: | 
|---|
| [617] | 1091 | const GR& _graph; | 
|---|
| [220] | 1092 | }; | 
|---|
|  | 1093 |  | 
|---|
|  | 1094 | namespace _core_bits { | 
|---|
|  | 1095 |  | 
|---|
|  | 1096 | template <typename Graph, typename Enable = void> | 
|---|
|  | 1097 | struct FindEdgeSelector { | 
|---|
|  | 1098 | typedef typename Graph::Node Node; | 
|---|
|  | 1099 | typedef typename Graph::Edge Edge; | 
|---|
|  | 1100 | static Edge find(const Graph &g, Node u, Node v, Edge e) { | 
|---|
|  | 1101 | bool b; | 
|---|
|  | 1102 | if (u != v) { | 
|---|
|  | 1103 | if (e == INVALID) { | 
|---|
|  | 1104 | g.firstInc(e, b, u); | 
|---|
|  | 1105 | } else { | 
|---|
|  | 1106 | b = g.u(e) == u; | 
|---|
|  | 1107 | g.nextInc(e, b); | 
|---|
|  | 1108 | } | 
|---|
|  | 1109 | while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { | 
|---|
|  | 1110 | g.nextInc(e, b); | 
|---|
|  | 1111 | } | 
|---|
|  | 1112 | } else { | 
|---|
|  | 1113 | if (e == INVALID) { | 
|---|
|  | 1114 | g.firstInc(e, b, u); | 
|---|
|  | 1115 | } else { | 
|---|
|  | 1116 | b = true; | 
|---|
|  | 1117 | g.nextInc(e, b); | 
|---|
|  | 1118 | } | 
|---|
|  | 1119 | while (e != INVALID && (!b || g.v(e) != v)) { | 
|---|
|  | 1120 | g.nextInc(e, b); | 
|---|
|  | 1121 | } | 
|---|
|  | 1122 | } | 
|---|
|  | 1123 | return e; | 
|---|
|  | 1124 | } | 
|---|
|  | 1125 | }; | 
|---|
|  | 1126 |  | 
|---|
|  | 1127 | template <typename Graph> | 
|---|
|  | 1128 | struct FindEdgeSelector< | 
|---|
|  | 1129 | Graph, | 
|---|
|  | 1130 | typename enable_if<typename Graph::FindEdgeTag, void>::type> | 
|---|
|  | 1131 | { | 
|---|
|  | 1132 | typedef typename Graph::Node Node; | 
|---|
|  | 1133 | typedef typename Graph::Edge Edge; | 
|---|
|  | 1134 | static Edge find(const Graph &g, Node u, Node v, Edge prev) { | 
|---|
|  | 1135 | return g.findEdge(u, v, prev); | 
|---|
|  | 1136 | } | 
|---|
|  | 1137 | }; | 
|---|
|  | 1138 | } | 
|---|
|  | 1139 |  | 
|---|
| [282] | 1140 | /// \brief Find an edge between two nodes of a graph. | 
|---|
| [220] | 1141 | /// | 
|---|
| [282] | 1142 | /// This function finds an edge from node \c u to node \c v in graph \c g. | 
|---|
|  | 1143 | /// If node \c u and node \c v is equal then each loop edge | 
|---|
| [220] | 1144 | /// will be enumerated once. | 
|---|
|  | 1145 | /// | 
|---|
|  | 1146 | /// If \c prev is \ref INVALID (this is the default value), then | 
|---|
| [282] | 1147 | /// it finds the first edge from \c u to \c v. Otherwise it looks for | 
|---|
|  | 1148 | /// the next edge from \c u to \c v after \c prev. | 
|---|
|  | 1149 | /// \return The found edge or \ref INVALID if there is no such an edge. | 
|---|
| [220] | 1150 | /// | 
|---|
| [282] | 1151 | /// Thus you can iterate through each edge between \c u and \c v | 
|---|
|  | 1152 | /// as it follows. | 
|---|
| [220] | 1153 | ///\code | 
|---|
| [282] | 1154 | /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { | 
|---|
| [220] | 1155 | ///   ... | 
|---|
|  | 1156 | /// } | 
|---|
|  | 1157 | ///\endcode | 
|---|
|  | 1158 | /// | 
|---|
| [282] | 1159 | /// \note \ref ConEdgeIt provides iterator interface for the same | 
|---|
|  | 1160 | /// functionality. | 
|---|
|  | 1161 | /// | 
|---|
| [220] | 1162 | ///\sa ConEdgeIt | 
|---|
|  | 1163 | template <typename Graph> | 
|---|
|  | 1164 | inline typename Graph::Edge | 
|---|
|  | 1165 | findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, | 
|---|
|  | 1166 | typename Graph::Edge p = INVALID) { | 
|---|
|  | 1167 | return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); | 
|---|
|  | 1168 | } | 
|---|
|  | 1169 |  | 
|---|
| [282] | 1170 | /// \brief Iterator for iterating on parallel edges connecting the same nodes. | 
|---|
| [220] | 1171 | /// | 
|---|
| [282] | 1172 | /// Iterator for iterating on parallel edges connecting the same nodes. | 
|---|
|  | 1173 | /// It is a higher level interface for the findEdge() function. You can | 
|---|
| [220] | 1174 | /// use it the following way: | 
|---|
|  | 1175 | ///\code | 
|---|
| [282] | 1176 | /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { | 
|---|
| [220] | 1177 | ///   ... | 
|---|
|  | 1178 | /// } | 
|---|
|  | 1179 | ///\endcode | 
|---|
|  | 1180 | /// | 
|---|
|  | 1181 | ///\sa findEdge() | 
|---|
| [559] | 1182 | template <typename GR> | 
|---|
|  | 1183 | class ConEdgeIt : public GR::Edge { | 
|---|
| [617] | 1184 | typedef typename GR::Edge Parent; | 
|---|
|  | 1185 |  | 
|---|
| [220] | 1186 | public: | 
|---|
|  | 1187 |  | 
|---|
| [617] | 1188 | typedef typename GR::Edge Edge; | 
|---|
|  | 1189 | typedef typename GR::Node Node; | 
|---|
| [220] | 1190 |  | 
|---|
|  | 1191 | /// \brief Constructor. | 
|---|
|  | 1192 | /// | 
|---|
| [282] | 1193 | /// Construct a new ConEdgeIt iterating on the edges that | 
|---|
|  | 1194 | /// connects nodes \c u and \c v. | 
|---|
| [617] | 1195 | ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) { | 
|---|
| [429] | 1196 | Parent::operator=(findEdge(_graph, _u, _v)); | 
|---|
| [220] | 1197 | } | 
|---|
|  | 1198 |  | 
|---|
|  | 1199 | /// \brief Constructor. | 
|---|
|  | 1200 | /// | 
|---|
| [282] | 1201 | /// Construct a new ConEdgeIt that continues iterating from edge \c e. | 
|---|
| [617] | 1202 | ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {} | 
|---|
| [220] | 1203 |  | 
|---|
|  | 1204 | /// \brief Increment operator. | 
|---|
|  | 1205 | /// | 
|---|
|  | 1206 | /// It increments the iterator and gives back the next edge. | 
|---|
|  | 1207 | ConEdgeIt& operator++() { | 
|---|
| [429] | 1208 | Parent::operator=(findEdge(_graph, _u, _v, *this)); | 
|---|
| [220] | 1209 | return *this; | 
|---|
|  | 1210 | } | 
|---|
|  | 1211 | private: | 
|---|
| [617] | 1212 | const GR& _graph; | 
|---|
| [429] | 1213 | Node _u, _v; | 
|---|
| [220] | 1214 | }; | 
|---|
|  | 1215 |  | 
|---|
|  | 1216 |  | 
|---|
| [282] | 1217 | ///Dynamic arc look-up between given endpoints. | 
|---|
| [220] | 1218 |  | 
|---|
|  | 1219 | ///Using this class, you can find an arc in a digraph from a given | 
|---|
| [282] | 1220 | ///source to a given target in amortized time <em>O</em>(log<em>d</em>), | 
|---|
| [220] | 1221 | ///where <em>d</em> is the out-degree of the source node. | 
|---|
|  | 1222 | /// | 
|---|
|  | 1223 | ///It is possible to find \e all parallel arcs between two nodes with | 
|---|
| [233] | 1224 | ///the \c operator() member. | 
|---|
| [220] | 1225 | /// | 
|---|
| [282] | 1226 | ///This is a dynamic data structure. Consider to use \ref ArcLookUp or | 
|---|
|  | 1227 | ///\ref AllArcLookUp if your digraph is not changed so frequently. | 
|---|
| [220] | 1228 | /// | 
|---|
| [282] | 1229 | ///This class uses a self-adjusting binary search tree, the Splay tree | 
|---|
|  | 1230 | ///of Sleator and Tarjan to guarantee the logarithmic amortized | 
|---|
|  | 1231 | ///time bound for arc look-ups. This class also guarantees the | 
|---|
| [220] | 1232 | ///optimal time bound in a constant factor for any distribution of | 
|---|
|  | 1233 | ///queries. | 
|---|
|  | 1234 | /// | 
|---|
| [559] | 1235 | ///\tparam GR The type of the underlying digraph. | 
|---|
| [220] | 1236 | /// | 
|---|
|  | 1237 | ///\sa ArcLookUp | 
|---|
|  | 1238 | ///\sa AllArcLookUp | 
|---|
| [559] | 1239 | template <typename GR> | 
|---|
| [220] | 1240 | class DynArcLookUp | 
|---|
| [559] | 1241 | : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase | 
|---|
| [220] | 1242 | { | 
|---|
| [559] | 1243 | typedef typename ItemSetTraits<GR, typename GR::Arc> | 
|---|
| [220] | 1244 | ::ItemNotifier::ObserverBase Parent; | 
|---|
|  | 1245 |  | 
|---|
| [559] | 1246 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); | 
|---|
| [617] | 1247 |  | 
|---|
|  | 1248 | public: | 
|---|
|  | 1249 |  | 
|---|
|  | 1250 | /// The Digraph type | 
|---|
| [559] | 1251 | typedef GR Digraph; | 
|---|
| [220] | 1252 |  | 
|---|
|  | 1253 | protected: | 
|---|
|  | 1254 |  | 
|---|
| [559] | 1255 | class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type { | 
|---|
| [617] | 1256 | typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent; | 
|---|
|  | 1257 |  | 
|---|
| [220] | 1258 | public: | 
|---|
|  | 1259 |  | 
|---|
| [559] | 1260 | AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {} | 
|---|
| [220] | 1261 |  | 
|---|
|  | 1262 | virtual void add(const Node& node) { | 
|---|
|  | 1263 | Parent::add(node); | 
|---|
|  | 1264 | Parent::set(node, INVALID); | 
|---|
|  | 1265 | } | 
|---|
|  | 1266 |  | 
|---|
|  | 1267 | virtual void add(const std::vector<Node>& nodes) { | 
|---|
|  | 1268 | Parent::add(nodes); | 
|---|
|  | 1269 | for (int i = 0; i < int(nodes.size()); ++i) { | 
|---|
|  | 1270 | Parent::set(nodes[i], INVALID); | 
|---|
|  | 1271 | } | 
|---|
|  | 1272 | } | 
|---|
|  | 1273 |  | 
|---|
|  | 1274 | virtual void build() { | 
|---|
|  | 1275 | Parent::build(); | 
|---|
|  | 1276 | Node it; | 
|---|
|  | 1277 | typename Parent::Notifier* nf = Parent::notifier(); | 
|---|
|  | 1278 | for (nf->first(it); it != INVALID; nf->next(it)) { | 
|---|
|  | 1279 | Parent::set(it, INVALID); | 
|---|
|  | 1280 | } | 
|---|
|  | 1281 | } | 
|---|
|  | 1282 | }; | 
|---|
|  | 1283 |  | 
|---|
|  | 1284 | class ArcLess { | 
|---|
|  | 1285 | const Digraph &g; | 
|---|
|  | 1286 | public: | 
|---|
|  | 1287 | ArcLess(const Digraph &_g) : g(_g) {} | 
|---|
|  | 1288 | bool operator()(Arc a,Arc b) const | 
|---|
|  | 1289 | { | 
|---|
|  | 1290 | return g.target(a)<g.target(b); | 
|---|
|  | 1291 | } | 
|---|
|  | 1292 | }; | 
|---|
|  | 1293 |  | 
|---|
| [617] | 1294 | protected: | 
|---|
|  | 1295 |  | 
|---|
|  | 1296 | const Digraph &_g; | 
|---|
|  | 1297 | AutoNodeMap _head; | 
|---|
|  | 1298 | typename Digraph::template ArcMap<Arc> _parent; | 
|---|
|  | 1299 | typename Digraph::template ArcMap<Arc> _left; | 
|---|
|  | 1300 | typename Digraph::template ArcMap<Arc> _right; | 
|---|
|  | 1301 |  | 
|---|
| [220] | 1302 | public: | 
|---|
|  | 1303 |  | 
|---|
|  | 1304 | ///Constructor | 
|---|
|  | 1305 |  | 
|---|
|  | 1306 | ///Constructor. | 
|---|
|  | 1307 | /// | 
|---|
|  | 1308 | ///It builds up the search database. | 
|---|
|  | 1309 | DynArcLookUp(const Digraph &g) | 
|---|
|  | 1310 | : _g(g),_head(g),_parent(g),_left(g),_right(g) | 
|---|
|  | 1311 | { | 
|---|
|  | 1312 | Parent::attach(_g.notifier(typename Digraph::Arc())); | 
|---|
|  | 1313 | refresh(); | 
|---|
|  | 1314 | } | 
|---|
|  | 1315 |  | 
|---|
|  | 1316 | protected: | 
|---|
|  | 1317 |  | 
|---|
|  | 1318 | virtual void add(const Arc& arc) { | 
|---|
|  | 1319 | insert(arc); | 
|---|
|  | 1320 | } | 
|---|
|  | 1321 |  | 
|---|
|  | 1322 | virtual void add(const std::vector<Arc>& arcs) { | 
|---|
|  | 1323 | for (int i = 0; i < int(arcs.size()); ++i) { | 
|---|
|  | 1324 | insert(arcs[i]); | 
|---|
|  | 1325 | } | 
|---|
|  | 1326 | } | 
|---|
|  | 1327 |  | 
|---|
|  | 1328 | virtual void erase(const Arc& arc) { | 
|---|
|  | 1329 | remove(arc); | 
|---|
|  | 1330 | } | 
|---|
|  | 1331 |  | 
|---|
|  | 1332 | virtual void erase(const std::vector<Arc>& arcs) { | 
|---|
|  | 1333 | for (int i = 0; i < int(arcs.size()); ++i) { | 
|---|
|  | 1334 | remove(arcs[i]); | 
|---|
|  | 1335 | } | 
|---|
|  | 1336 | } | 
|---|
|  | 1337 |  | 
|---|
|  | 1338 | virtual void build() { | 
|---|
|  | 1339 | refresh(); | 
|---|
|  | 1340 | } | 
|---|
|  | 1341 |  | 
|---|
|  | 1342 | virtual void clear() { | 
|---|
|  | 1343 | for(NodeIt n(_g);n!=INVALID;++n) { | 
|---|
| [581] | 1344 | _head[n] = INVALID; | 
|---|
| [220] | 1345 | } | 
|---|
|  | 1346 | } | 
|---|
|  | 1347 |  | 
|---|
|  | 1348 | void insert(Arc arc) { | 
|---|
|  | 1349 | Node s = _g.source(arc); | 
|---|
|  | 1350 | Node t = _g.target(arc); | 
|---|
| [581] | 1351 | _left[arc] = INVALID; | 
|---|
|  | 1352 | _right[arc] = INVALID; | 
|---|
| [220] | 1353 |  | 
|---|
|  | 1354 | Arc e = _head[s]; | 
|---|
|  | 1355 | if (e == INVALID) { | 
|---|
| [581] | 1356 | _head[s] = arc; | 
|---|
|  | 1357 | _parent[arc] = INVALID; | 
|---|
| [220] | 1358 | return; | 
|---|
|  | 1359 | } | 
|---|
|  | 1360 | while (true) { | 
|---|
|  | 1361 | if (t < _g.target(e)) { | 
|---|
|  | 1362 | if (_left[e] == INVALID) { | 
|---|
| [581] | 1363 | _left[e] = arc; | 
|---|
|  | 1364 | _parent[arc] = e; | 
|---|
| [220] | 1365 | splay(arc); | 
|---|
|  | 1366 | return; | 
|---|
|  | 1367 | } else { | 
|---|
|  | 1368 | e = _left[e]; | 
|---|
|  | 1369 | } | 
|---|
|  | 1370 | } else { | 
|---|
|  | 1371 | if (_right[e] == INVALID) { | 
|---|
| [581] | 1372 | _right[e] = arc; | 
|---|
|  | 1373 | _parent[arc] = e; | 
|---|
| [220] | 1374 | splay(arc); | 
|---|
|  | 1375 | return; | 
|---|
|  | 1376 | } else { | 
|---|
|  | 1377 | e = _right[e]; | 
|---|
|  | 1378 | } | 
|---|
|  | 1379 | } | 
|---|
|  | 1380 | } | 
|---|
|  | 1381 | } | 
|---|
|  | 1382 |  | 
|---|
|  | 1383 | void remove(Arc arc) { | 
|---|
|  | 1384 | if (_left[arc] == INVALID) { | 
|---|
|  | 1385 | if (_right[arc] != INVALID) { | 
|---|
| [581] | 1386 | _parent[_right[arc]] = _parent[arc]; | 
|---|
| [220] | 1387 | } | 
|---|
|  | 1388 | if (_parent[arc] != INVALID) { | 
|---|
|  | 1389 | if (_left[_parent[arc]] == arc) { | 
|---|
| [581] | 1390 | _left[_parent[arc]] = _right[arc]; | 
|---|
| [220] | 1391 | } else { | 
|---|
| [581] | 1392 | _right[_parent[arc]] = _right[arc]; | 
|---|
| [220] | 1393 | } | 
|---|
|  | 1394 | } else { | 
|---|
| [581] | 1395 | _head[_g.source(arc)] = _right[arc]; | 
|---|
| [220] | 1396 | } | 
|---|
|  | 1397 | } else if (_right[arc] == INVALID) { | 
|---|
| [581] | 1398 | _parent[_left[arc]] = _parent[arc]; | 
|---|
| [220] | 1399 | if (_parent[arc] != INVALID) { | 
|---|
|  | 1400 | if (_left[_parent[arc]] == arc) { | 
|---|
| [581] | 1401 | _left[_parent[arc]] = _left[arc]; | 
|---|
| [220] | 1402 | } else { | 
|---|
| [581] | 1403 | _right[_parent[arc]] = _left[arc]; | 
|---|
| [220] | 1404 | } | 
|---|
|  | 1405 | } else { | 
|---|
| [581] | 1406 | _head[_g.source(arc)] = _left[arc]; | 
|---|
| [220] | 1407 | } | 
|---|
|  | 1408 | } else { | 
|---|
|  | 1409 | Arc e = _left[arc]; | 
|---|
|  | 1410 | if (_right[e] != INVALID) { | 
|---|
|  | 1411 | e = _right[e]; | 
|---|
|  | 1412 | while (_right[e] != INVALID) { | 
|---|
|  | 1413 | e = _right[e]; | 
|---|
|  | 1414 | } | 
|---|
|  | 1415 | Arc s = _parent[e]; | 
|---|
| [581] | 1416 | _right[_parent[e]] = _left[e]; | 
|---|
| [220] | 1417 | if (_left[e] != INVALID) { | 
|---|
| [581] | 1418 | _parent[_left[e]] = _parent[e]; | 
|---|
| [220] | 1419 | } | 
|---|
|  | 1420 |  | 
|---|
| [581] | 1421 | _left[e] = _left[arc]; | 
|---|
|  | 1422 | _parent[_left[arc]] = e; | 
|---|
|  | 1423 | _right[e] = _right[arc]; | 
|---|
|  | 1424 | _parent[_right[arc]] = e; | 
|---|
| [220] | 1425 |  | 
|---|
| [581] | 1426 | _parent[e] = _parent[arc]; | 
|---|
| [220] | 1427 | if (_parent[arc] != INVALID) { | 
|---|
|  | 1428 | if (_left[_parent[arc]] == arc) { | 
|---|
| [581] | 1429 | _left[_parent[arc]] = e; | 
|---|
| [220] | 1430 | } else { | 
|---|
| [581] | 1431 | _right[_parent[arc]] = e; | 
|---|
| [220] | 1432 | } | 
|---|
|  | 1433 | } | 
|---|
|  | 1434 | splay(s); | 
|---|
|  | 1435 | } else { | 
|---|
| [581] | 1436 | _right[e] = _right[arc]; | 
|---|
|  | 1437 | _parent[_right[arc]] = e; | 
|---|
|  | 1438 | _parent[e] = _parent[arc]; | 
|---|
| [220] | 1439 |  | 
|---|
|  | 1440 | if (_parent[arc] != INVALID) { | 
|---|
|  | 1441 | if (_left[_parent[arc]] == arc) { | 
|---|
| [581] | 1442 | _left[_parent[arc]] = e; | 
|---|
| [220] | 1443 | } else { | 
|---|
| [581] | 1444 | _right[_parent[arc]] = e; | 
|---|
| [220] | 1445 | } | 
|---|
|  | 1446 | } else { | 
|---|
| [581] | 1447 | _head[_g.source(arc)] = e; | 
|---|
| [220] | 1448 | } | 
|---|
|  | 1449 | } | 
|---|
|  | 1450 | } | 
|---|
|  | 1451 | } | 
|---|
|  | 1452 |  | 
|---|
|  | 1453 | Arc refreshRec(std::vector<Arc> &v,int a,int b) | 
|---|
|  | 1454 | { | 
|---|
|  | 1455 | int m=(a+b)/2; | 
|---|
|  | 1456 | Arc me=v[m]; | 
|---|
|  | 1457 | if (a < m) { | 
|---|
|  | 1458 | Arc left = refreshRec(v,a,m-1); | 
|---|
| [581] | 1459 | _left[me] = left; | 
|---|
|  | 1460 | _parent[left] = me; | 
|---|
| [220] | 1461 | } else { | 
|---|
| [581] | 1462 | _left[me] = INVALID; | 
|---|
| [220] | 1463 | } | 
|---|
|  | 1464 | if (m < b) { | 
|---|
|  | 1465 | Arc right = refreshRec(v,m+1,b); | 
|---|
| [581] | 1466 | _right[me] = right; | 
|---|
|  | 1467 | _parent[right] = me; | 
|---|
| [220] | 1468 | } else { | 
|---|
| [581] | 1469 | _right[me] = INVALID; | 
|---|
| [220] | 1470 | } | 
|---|
|  | 1471 | return me; | 
|---|
|  | 1472 | } | 
|---|
|  | 1473 |  | 
|---|
|  | 1474 | void refresh() { | 
|---|
|  | 1475 | for(NodeIt n(_g);n!=INVALID;++n) { | 
|---|
|  | 1476 | std::vector<Arc> v; | 
|---|
| [233] | 1477 | for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); | 
|---|
|  | 1478 | if (!v.empty()) { | 
|---|
| [220] | 1479 | std::sort(v.begin(),v.end(),ArcLess(_g)); | 
|---|
|  | 1480 | Arc head = refreshRec(v,0,v.size()-1); | 
|---|
| [581] | 1481 | _head[n] = head; | 
|---|
|  | 1482 | _parent[head] = INVALID; | 
|---|
| [220] | 1483 | } | 
|---|
| [581] | 1484 | else _head[n] = INVALID; | 
|---|
| [220] | 1485 | } | 
|---|
|  | 1486 | } | 
|---|
|  | 1487 |  | 
|---|
|  | 1488 | void zig(Arc v) { | 
|---|
|  | 1489 | Arc w = _parent[v]; | 
|---|
| [581] | 1490 | _parent[v] = _parent[w]; | 
|---|
|  | 1491 | _parent[w] = v; | 
|---|
|  | 1492 | _left[w] = _right[v]; | 
|---|
|  | 1493 | _right[v] = w; | 
|---|
| [220] | 1494 | if (_parent[v] != INVALID) { | 
|---|
|  | 1495 | if (_right[_parent[v]] == w) { | 
|---|
| [581] | 1496 | _right[_parent[v]] = v; | 
|---|
| [220] | 1497 | } else { | 
|---|
| [581] | 1498 | _left[_parent[v]] = v; | 
|---|
| [220] | 1499 | } | 
|---|
|  | 1500 | } | 
|---|
|  | 1501 | if (_left[w] != INVALID){ | 
|---|
| [581] | 1502 | _parent[_left[w]] = w; | 
|---|
| [220] | 1503 | } | 
|---|
|  | 1504 | } | 
|---|
|  | 1505 |  | 
|---|
|  | 1506 | void zag(Arc v) { | 
|---|
|  | 1507 | Arc w = _parent[v]; | 
|---|
| [581] | 1508 | _parent[v] = _parent[w]; | 
|---|
|  | 1509 | _parent[w] = v; | 
|---|
|  | 1510 | _right[w] = _left[v]; | 
|---|
|  | 1511 | _left[v] = w; | 
|---|
| [220] | 1512 | if (_parent[v] != INVALID){ | 
|---|
|  | 1513 | if (_left[_parent[v]] == w) { | 
|---|
| [581] | 1514 | _left[_parent[v]] = v; | 
|---|
| [220] | 1515 | } else { | 
|---|
| [581] | 1516 | _right[_parent[v]] = v; | 
|---|
| [220] | 1517 | } | 
|---|
|  | 1518 | } | 
|---|
|  | 1519 | if (_right[w] != INVALID){ | 
|---|
| [581] | 1520 | _parent[_right[w]] = w; | 
|---|
| [220] | 1521 | } | 
|---|
|  | 1522 | } | 
|---|
|  | 1523 |  | 
|---|
|  | 1524 | void splay(Arc v) { | 
|---|
|  | 1525 | while (_parent[v] != INVALID) { | 
|---|
|  | 1526 | if (v == _left[_parent[v]]) { | 
|---|
|  | 1527 | if (_parent[_parent[v]] == INVALID) { | 
|---|
|  | 1528 | zig(v); | 
|---|
|  | 1529 | } else { | 
|---|
|  | 1530 | if (_parent[v] == _left[_parent[_parent[v]]]) { | 
|---|
|  | 1531 | zig(_parent[v]); | 
|---|
|  | 1532 | zig(v); | 
|---|
|  | 1533 | } else { | 
|---|
|  | 1534 | zig(v); | 
|---|
|  | 1535 | zag(v); | 
|---|
|  | 1536 | } | 
|---|
|  | 1537 | } | 
|---|
|  | 1538 | } else { | 
|---|
|  | 1539 | if (_parent[_parent[v]] == INVALID) { | 
|---|
|  | 1540 | zag(v); | 
|---|
|  | 1541 | } else { | 
|---|
|  | 1542 | if (_parent[v] == _left[_parent[_parent[v]]]) { | 
|---|
|  | 1543 | zag(v); | 
|---|
|  | 1544 | zig(v); | 
|---|
|  | 1545 | } else { | 
|---|
|  | 1546 | zag(_parent[v]); | 
|---|
|  | 1547 | zag(v); | 
|---|
|  | 1548 | } | 
|---|
|  | 1549 | } | 
|---|
|  | 1550 | } | 
|---|
|  | 1551 | } | 
|---|
|  | 1552 | _head[_g.source(v)] = v; | 
|---|
|  | 1553 | } | 
|---|
|  | 1554 |  | 
|---|
|  | 1555 |  | 
|---|
|  | 1556 | public: | 
|---|
|  | 1557 |  | 
|---|
|  | 1558 | ///Find an arc between two nodes. | 
|---|
|  | 1559 |  | 
|---|
| [233] | 1560 | ///Find an arc between two nodes. | 
|---|
| [282] | 1561 | ///\param s The source node. | 
|---|
|  | 1562 | ///\param t The target node. | 
|---|
| [233] | 1563 | ///\param p The previous arc between \c s and \c t. It it is INVALID or | 
|---|
|  | 1564 | ///not given, the operator finds the first appropriate arc. | 
|---|
|  | 1565 | ///\return An arc from \c s to \c t after \c p or | 
|---|
|  | 1566 | ///\ref INVALID if there is no more. | 
|---|
|  | 1567 | /// | 
|---|
|  | 1568 | ///For example, you can count the number of arcs from \c u to \c v in the | 
|---|
|  | 1569 | ///following way. | 
|---|
|  | 1570 | ///\code | 
|---|
|  | 1571 | ///DynArcLookUp<ListDigraph> ae(g); | 
|---|
|  | 1572 | ///... | 
|---|
| [282] | 1573 | ///int n = 0; | 
|---|
|  | 1574 | ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; | 
|---|
| [233] | 1575 | ///\endcode | 
|---|
|  | 1576 | /// | 
|---|
| [282] | 1577 | ///Finding the arcs take at most <em>O</em>(log<em>d</em>) | 
|---|
| [233] | 1578 | ///amortized time, specifically, the time complexity of the lookups | 
|---|
|  | 1579 | ///is equal to the optimal search tree implementation for the | 
|---|
|  | 1580 | ///current query distribution in a constant factor. | 
|---|
|  | 1581 | /// | 
|---|
|  | 1582 | ///\note This is a dynamic data structure, therefore the data | 
|---|
| [282] | 1583 | ///structure is updated after each graph alteration. Thus although | 
|---|
|  | 1584 | ///this data structure is theoretically faster than \ref ArcLookUp | 
|---|
| [313] | 1585 | ///and \ref AllArcLookUp, it often provides worse performance than | 
|---|
| [233] | 1586 | ///them. | 
|---|
|  | 1587 | Arc operator()(Node s, Node t, Arc p = INVALID) const  { | 
|---|
|  | 1588 | if (p == INVALID) { | 
|---|
|  | 1589 | Arc a = _head[s]; | 
|---|
|  | 1590 | if (a == INVALID) return INVALID; | 
|---|
|  | 1591 | Arc r = INVALID; | 
|---|
|  | 1592 | while (true) { | 
|---|
|  | 1593 | if (_g.target(a) < t) { | 
|---|
|  | 1594 | if (_right[a] == INVALID) { | 
|---|
|  | 1595 | const_cast<DynArcLookUp&>(*this).splay(a); | 
|---|
|  | 1596 | return r; | 
|---|
|  | 1597 | } else { | 
|---|
|  | 1598 | a = _right[a]; | 
|---|
|  | 1599 | } | 
|---|
|  | 1600 | } else { | 
|---|
|  | 1601 | if (_g.target(a) == t) { | 
|---|
|  | 1602 | r = a; | 
|---|
|  | 1603 | } | 
|---|
|  | 1604 | if (_left[a] == INVALID) { | 
|---|
|  | 1605 | const_cast<DynArcLookUp&>(*this).splay(a); | 
|---|
|  | 1606 | return r; | 
|---|
|  | 1607 | } else { | 
|---|
|  | 1608 | a = _left[a]; | 
|---|
|  | 1609 | } | 
|---|
|  | 1610 | } | 
|---|
|  | 1611 | } | 
|---|
|  | 1612 | } else { | 
|---|
|  | 1613 | Arc a = p; | 
|---|
|  | 1614 | if (_right[a] != INVALID) { | 
|---|
|  | 1615 | a = _right[a]; | 
|---|
|  | 1616 | while (_left[a] != INVALID) { | 
|---|
|  | 1617 | a = _left[a]; | 
|---|
|  | 1618 | } | 
|---|
| [220] | 1619 | const_cast<DynArcLookUp&>(*this).splay(a); | 
|---|
| [233] | 1620 | } else { | 
|---|
|  | 1621 | while (_parent[a] != INVALID && _right[_parent[a]] ==  a) { | 
|---|
|  | 1622 | a = _parent[a]; | 
|---|
|  | 1623 | } | 
|---|
|  | 1624 | if (_parent[a] == INVALID) { | 
|---|
| [220] | 1625 | return INVALID; | 
|---|
|  | 1626 | } else { | 
|---|
| [233] | 1627 | a = _parent[a]; | 
|---|
| [220] | 1628 | const_cast<DynArcLookUp&>(*this).splay(a); | 
|---|
|  | 1629 | } | 
|---|
|  | 1630 | } | 
|---|
| [233] | 1631 | if (_g.target(a) == t) return a; | 
|---|
|  | 1632 | else return INVALID; | 
|---|
| [220] | 1633 | } | 
|---|
|  | 1634 | } | 
|---|
|  | 1635 |  | 
|---|
|  | 1636 | }; | 
|---|
|  | 1637 |  | 
|---|
| [282] | 1638 | ///Fast arc look-up between given endpoints. | 
|---|
| [220] | 1639 |  | 
|---|
|  | 1640 | ///Using this class, you can find an arc in a digraph from a given | 
|---|
| [282] | 1641 | ///source to a given target in time <em>O</em>(log<em>d</em>), | 
|---|
| [220] | 1642 | ///where <em>d</em> is the out-degree of the source node. | 
|---|
|  | 1643 | /// | 
|---|
|  | 1644 | ///It is not possible to find \e all parallel arcs between two nodes. | 
|---|
|  | 1645 | ///Use \ref AllArcLookUp for this purpose. | 
|---|
|  | 1646 | /// | 
|---|
| [282] | 1647 | ///\warning This class is static, so you should call refresh() (or at | 
|---|
|  | 1648 | ///least refresh(Node)) to refresh this data structure whenever the | 
|---|
|  | 1649 | ///digraph changes. This is a time consuming (superlinearly proportional | 
|---|
|  | 1650 | ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). | 
|---|
| [220] | 1651 | /// | 
|---|
| [559] | 1652 | ///\tparam GR The type of the underlying digraph. | 
|---|
| [220] | 1653 | /// | 
|---|
|  | 1654 | ///\sa DynArcLookUp | 
|---|
|  | 1655 | ///\sa AllArcLookUp | 
|---|
| [559] | 1656 | template<class GR> | 
|---|
| [220] | 1657 | class ArcLookUp | 
|---|
|  | 1658 | { | 
|---|
| [617] | 1659 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); | 
|---|
|  | 1660 |  | 
|---|
| [220] | 1661 | public: | 
|---|
| [617] | 1662 |  | 
|---|
|  | 1663 | /// The Digraph type | 
|---|
| [559] | 1664 | typedef GR Digraph; | 
|---|
| [220] | 1665 |  | 
|---|
|  | 1666 | protected: | 
|---|
|  | 1667 | const Digraph &_g; | 
|---|
|  | 1668 | typename Digraph::template NodeMap<Arc> _head; | 
|---|
|  | 1669 | typename Digraph::template ArcMap<Arc> _left; | 
|---|
|  | 1670 | typename Digraph::template ArcMap<Arc> _right; | 
|---|
|  | 1671 |  | 
|---|
|  | 1672 | class ArcLess { | 
|---|
|  | 1673 | const Digraph &g; | 
|---|
|  | 1674 | public: | 
|---|
|  | 1675 | ArcLess(const Digraph &_g) : g(_g) {} | 
|---|
|  | 1676 | bool operator()(Arc a,Arc b) const | 
|---|
|  | 1677 | { | 
|---|
|  | 1678 | return g.target(a)<g.target(b); | 
|---|
|  | 1679 | } | 
|---|
|  | 1680 | }; | 
|---|
|  | 1681 |  | 
|---|
|  | 1682 | public: | 
|---|
|  | 1683 |  | 
|---|
|  | 1684 | ///Constructor | 
|---|
|  | 1685 |  | 
|---|
|  | 1686 | ///Constructor. | 
|---|
|  | 1687 | /// | 
|---|
|  | 1688 | ///It builds up the search database, which remains valid until the digraph | 
|---|
|  | 1689 | ///changes. | 
|---|
|  | 1690 | ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} | 
|---|
|  | 1691 |  | 
|---|
|  | 1692 | private: | 
|---|
|  | 1693 | Arc refreshRec(std::vector<Arc> &v,int a,int b) | 
|---|
|  | 1694 | { | 
|---|
|  | 1695 | int m=(a+b)/2; | 
|---|
|  | 1696 | Arc me=v[m]; | 
|---|
|  | 1697 | _left[me] = a<m?refreshRec(v,a,m-1):INVALID; | 
|---|
|  | 1698 | _right[me] = m<b?refreshRec(v,m+1,b):INVALID; | 
|---|
|  | 1699 | return me; | 
|---|
|  | 1700 | } | 
|---|
|  | 1701 | public: | 
|---|
| [282] | 1702 | ///Refresh the search data structure at a node. | 
|---|
| [220] | 1703 |  | 
|---|
|  | 1704 | ///Build up the search database of node \c n. | 
|---|
|  | 1705 | /// | 
|---|
| [282] | 1706 | ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> | 
|---|
|  | 1707 | ///is the number of the outgoing arcs of \c n. | 
|---|
| [220] | 1708 | void refresh(Node n) | 
|---|
|  | 1709 | { | 
|---|
|  | 1710 | std::vector<Arc> v; | 
|---|
|  | 1711 | for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); | 
|---|
|  | 1712 | if(v.size()) { | 
|---|
|  | 1713 | std::sort(v.begin(),v.end(),ArcLess(_g)); | 
|---|
|  | 1714 | _head[n]=refreshRec(v,0,v.size()-1); | 
|---|
|  | 1715 | } | 
|---|
|  | 1716 | else _head[n]=INVALID; | 
|---|
|  | 1717 | } | 
|---|
|  | 1718 | ///Refresh the full data structure. | 
|---|
|  | 1719 |  | 
|---|
|  | 1720 | ///Build up the full search database. In fact, it simply calls | 
|---|
|  | 1721 | ///\ref refresh(Node) "refresh(n)" for each node \c n. | 
|---|
|  | 1722 | /// | 
|---|
| [282] | 1723 | ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is | 
|---|
|  | 1724 | ///the number of the arcs in the digraph and <em>D</em> is the maximum | 
|---|
| [220] | 1725 | ///out-degree of the digraph. | 
|---|
|  | 1726 | void refresh() | 
|---|
|  | 1727 | { | 
|---|
|  | 1728 | for(NodeIt n(_g);n!=INVALID;++n) refresh(n); | 
|---|
|  | 1729 | } | 
|---|
|  | 1730 |  | 
|---|
|  | 1731 | ///Find an arc between two nodes. | 
|---|
|  | 1732 |  | 
|---|
| [313] | 1733 | ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), | 
|---|
|  | 1734 | ///where <em>d</em> is the number of outgoing arcs of \c s. | 
|---|
| [282] | 1735 | ///\param s The source node. | 
|---|
|  | 1736 | ///\param t The target node. | 
|---|
| [220] | 1737 | ///\return An arc from \c s to \c t if there exists, | 
|---|
|  | 1738 | ///\ref INVALID otherwise. | 
|---|
|  | 1739 | /// | 
|---|
|  | 1740 | ///\warning If you change the digraph, refresh() must be called before using | 
|---|
|  | 1741 | ///this operator. If you change the outgoing arcs of | 
|---|
| [282] | 1742 | ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. | 
|---|
| [220] | 1743 | Arc operator()(Node s, Node t) const | 
|---|
|  | 1744 | { | 
|---|
|  | 1745 | Arc e; | 
|---|
|  | 1746 | for(e=_head[s]; | 
|---|
|  | 1747 | e!=INVALID&&_g.target(e)!=t; | 
|---|
|  | 1748 | e = t < _g.target(e)?_left[e]:_right[e]) ; | 
|---|
|  | 1749 | return e; | 
|---|
|  | 1750 | } | 
|---|
|  | 1751 |  | 
|---|
|  | 1752 | }; | 
|---|
|  | 1753 |  | 
|---|
| [282] | 1754 | ///Fast look-up of all arcs between given endpoints. | 
|---|
| [220] | 1755 |  | 
|---|
|  | 1756 | ///This class is the same as \ref ArcLookUp, with the addition | 
|---|
| [282] | 1757 | ///that it makes it possible to find all parallel arcs between given | 
|---|
|  | 1758 | ///endpoints. | 
|---|
| [220] | 1759 | /// | 
|---|
| [282] | 1760 | ///\warning This class is static, so you should call refresh() (or at | 
|---|
|  | 1761 | ///least refresh(Node)) to refresh this data structure whenever the | 
|---|
|  | 1762 | ///digraph changes. This is a time consuming (superlinearly proportional | 
|---|
|  | 1763 | ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). | 
|---|
| [220] | 1764 | /// | 
|---|
| [559] | 1765 | ///\tparam GR The type of the underlying digraph. | 
|---|
| [220] | 1766 | /// | 
|---|
|  | 1767 | ///\sa DynArcLookUp | 
|---|
|  | 1768 | ///\sa ArcLookUp | 
|---|
| [559] | 1769 | template<class GR> | 
|---|
|  | 1770 | class AllArcLookUp : public ArcLookUp<GR> | 
|---|
| [220] | 1771 | { | 
|---|
| [559] | 1772 | using ArcLookUp<GR>::_g; | 
|---|
|  | 1773 | using ArcLookUp<GR>::_right; | 
|---|
|  | 1774 | using ArcLookUp<GR>::_left; | 
|---|
|  | 1775 | using ArcLookUp<GR>::_head; | 
|---|
| [220] | 1776 |  | 
|---|
| [559] | 1777 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); | 
|---|
| [220] | 1778 |  | 
|---|
| [617] | 1779 | typename GR::template ArcMap<Arc> _next; | 
|---|
| [220] | 1780 |  | 
|---|
|  | 1781 | Arc refreshNext(Arc head,Arc next=INVALID) | 
|---|
|  | 1782 | { | 
|---|
|  | 1783 | if(head==INVALID) return next; | 
|---|
|  | 1784 | else { | 
|---|
|  | 1785 | next=refreshNext(_right[head],next); | 
|---|
|  | 1786 | _next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) | 
|---|
|  | 1787 | ? next : INVALID; | 
|---|
|  | 1788 | return refreshNext(_left[head],head); | 
|---|
|  | 1789 | } | 
|---|
|  | 1790 | } | 
|---|
|  | 1791 |  | 
|---|
|  | 1792 | void refreshNext() | 
|---|
|  | 1793 | { | 
|---|
|  | 1794 | for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); | 
|---|
|  | 1795 | } | 
|---|
|  | 1796 |  | 
|---|
|  | 1797 | public: | 
|---|
| [617] | 1798 |  | 
|---|
|  | 1799 | /// The Digraph type | 
|---|
|  | 1800 | typedef GR Digraph; | 
|---|
|  | 1801 |  | 
|---|
| [220] | 1802 | ///Constructor | 
|---|
|  | 1803 |  | 
|---|
|  | 1804 | ///Constructor. | 
|---|
|  | 1805 | /// | 
|---|
|  | 1806 | ///It builds up the search database, which remains valid until the digraph | 
|---|
|  | 1807 | ///changes. | 
|---|
| [559] | 1808 | AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();} | 
|---|
| [220] | 1809 |  | 
|---|
|  | 1810 | ///Refresh the data structure at a node. | 
|---|
|  | 1811 |  | 
|---|
|  | 1812 | ///Build up the search database of node \c n. | 
|---|
|  | 1813 | /// | 
|---|
| [282] | 1814 | ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is | 
|---|
| [220] | 1815 | ///the number of the outgoing arcs of \c n. | 
|---|
|  | 1816 | void refresh(Node n) | 
|---|
|  | 1817 | { | 
|---|
| [559] | 1818 | ArcLookUp<GR>::refresh(n); | 
|---|
| [220] | 1819 | refreshNext(_head[n]); | 
|---|
|  | 1820 | } | 
|---|
|  | 1821 |  | 
|---|
|  | 1822 | ///Refresh the full data structure. | 
|---|
|  | 1823 |  | 
|---|
|  | 1824 | ///Build up the full search database. In fact, it simply calls | 
|---|
|  | 1825 | ///\ref refresh(Node) "refresh(n)" for each node \c n. | 
|---|
|  | 1826 | /// | 
|---|
| [282] | 1827 | ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is | 
|---|
|  | 1828 | ///the number of the arcs in the digraph and <em>D</em> is the maximum | 
|---|
| [220] | 1829 | ///out-degree of the digraph. | 
|---|
|  | 1830 | void refresh() | 
|---|
|  | 1831 | { | 
|---|
|  | 1832 | for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); | 
|---|
|  | 1833 | } | 
|---|
|  | 1834 |  | 
|---|
|  | 1835 | ///Find an arc between two nodes. | 
|---|
|  | 1836 |  | 
|---|
|  | 1837 | ///Find an arc between two nodes. | 
|---|
| [282] | 1838 | ///\param s The source node. | 
|---|
|  | 1839 | ///\param t The target node. | 
|---|
| [220] | 1840 | ///\param prev The previous arc between \c s and \c t. It it is INVALID or | 
|---|
|  | 1841 | ///not given, the operator finds the first appropriate arc. | 
|---|
|  | 1842 | ///\return An arc from \c s to \c t after \c prev or | 
|---|
|  | 1843 | ///\ref INVALID if there is no more. | 
|---|
|  | 1844 | /// | 
|---|
|  | 1845 | ///For example, you can count the number of arcs from \c u to \c v in the | 
|---|
|  | 1846 | ///following way. | 
|---|
|  | 1847 | ///\code | 
|---|
|  | 1848 | ///AllArcLookUp<ListDigraph> ae(g); | 
|---|
|  | 1849 | ///... | 
|---|
| [282] | 1850 | ///int n = 0; | 
|---|
|  | 1851 | ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; | 
|---|
| [220] | 1852 | ///\endcode | 
|---|
|  | 1853 | /// | 
|---|
| [313] | 1854 | ///Finding the first arc take <em>O</em>(log<em>d</em>) time, | 
|---|
|  | 1855 | ///where <em>d</em> is the number of outgoing arcs of \c s. Then the | 
|---|
| [220] | 1856 | ///consecutive arcs are found in constant time. | 
|---|
|  | 1857 | /// | 
|---|
|  | 1858 | ///\warning If you change the digraph, refresh() must be called before using | 
|---|
|  | 1859 | ///this operator. If you change the outgoing arcs of | 
|---|
| [282] | 1860 | ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. | 
|---|
| [220] | 1861 | /// | 
|---|
| [993] | 1862 | Arc operator()(Node s, Node t, Arc prev=INVALID) const | 
|---|
| [220] | 1863 | { | 
|---|
| [993] | 1864 | if(prev==INVALID) | 
|---|
|  | 1865 | { | 
|---|
|  | 1866 | Arc f=INVALID; | 
|---|
|  | 1867 | Arc e; | 
|---|
|  | 1868 | for(e=_head[s]; | 
|---|
|  | 1869 | e!=INVALID&&_g.target(e)!=t; | 
|---|
|  | 1870 | e = t < _g.target(e)?_left[e]:_right[e]) ; | 
|---|
|  | 1871 | while(e!=INVALID) | 
|---|
|  | 1872 | if(_g.target(e)==t) | 
|---|
|  | 1873 | { | 
|---|
|  | 1874 | f = e; | 
|---|
|  | 1875 | e = _left[e]; | 
|---|
|  | 1876 | } | 
|---|
|  | 1877 | else e = _right[e]; | 
|---|
|  | 1878 | return f; | 
|---|
|  | 1879 | } | 
|---|
|  | 1880 | else return _next[prev]; | 
|---|
| [220] | 1881 | } | 
|---|
|  | 1882 |  | 
|---|
|  | 1883 | }; | 
|---|
|  | 1884 |  | 
|---|
|  | 1885 | /// @} | 
|---|
|  | 1886 |  | 
|---|
|  | 1887 | } //namespace lemon | 
|---|
|  | 1888 |  | 
|---|
|  | 1889 | #endif | 
|---|