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