1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/graph_utils.h Thu Feb 07 21:37:07 2008 +0000
1.3 @@ -0,0 +1,3179 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2008
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_GRAPH_UTILS_H
1.23 +#define LEMON_GRAPH_UTILS_H
1.24 +
1.25 +#include <iterator>
1.26 +#include <vector>
1.27 +#include <map>
1.28 +#include <cmath>
1.29 +#include <algorithm>
1.30 +
1.31 +#include <lemon/bits/invalid.h>
1.32 +#include <lemon/bits/utility.h>
1.33 +#include <lemon/maps.h>
1.34 +#include <lemon/bits/traits.h>
1.35 +
1.36 +#include <lemon/bits/alteration_notifier.h>
1.37 +#include <lemon/bits/default_map.h>
1.38 +
1.39 +///\ingroup gutils
1.40 +///\file
1.41 +///\brief Digraph utilities.
1.42 +
1.43 +namespace lemon {
1.44 +
1.45 + /// \addtogroup gutils
1.46 + /// @{
1.47 +
1.48 + ///Creates convenience typedefs for the digraph types and iterators
1.49 +
1.50 + ///This \c \#define creates convenience typedefs for the following types
1.51 + ///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
1.52 + ///\c OutArcIt
1.53 + ///\note If \c G it a template parameter, it should be used in this way.
1.54 + ///\code
1.55 + /// GRAPH_TYPEDEFS(typename G);
1.56 + ///\endcode
1.57 + ///
1.58 + ///\warning There are no typedefs for the digraph maps because of the lack of
1.59 + ///template typedefs in C++.
1.60 +#define GRAPH_TYPEDEFS(Digraph) \
1.61 + typedef Digraph:: Node Node; \
1.62 + typedef Digraph:: NodeIt NodeIt; \
1.63 + typedef Digraph:: Arc Arc; \
1.64 + typedef Digraph:: ArcIt ArcIt; \
1.65 + typedef Digraph:: InArcIt InArcIt; \
1.66 + typedef Digraph::OutArcIt OutArcIt
1.67 +
1.68 + ///Creates convenience typedefs for the graph types and iterators
1.69 +
1.70 + ///This \c \#define creates the same convenience typedefs as defined by
1.71 + ///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates
1.72 + ///\c Edge, \c EdgeIt, \c IncArcIt,
1.73 + ///
1.74 + ///\note If \c G it a template parameter, it should be used in this way.
1.75 + ///\code
1.76 + /// UGRAPH_TYPEDEFS(typename G);
1.77 + ///\endcode
1.78 + ///
1.79 + ///\warning There are no typedefs for the digraph maps because of the lack of
1.80 + ///template typedefs in C++.
1.81 +#define UGRAPH_TYPEDEFS(Digraph) \
1.82 + GRAPH_TYPEDEFS(Digraph); \
1.83 + typedef Digraph:: Edge Edge; \
1.84 + typedef Digraph:: EdgeIt EdgeIt; \
1.85 + typedef Digraph:: IncArcIt IncArcIt
1.86 +
1.87 + ///\brief Creates convenience typedefs for the bipartite digraph
1.88 + ///types and iterators
1.89 +
1.90 + ///This \c \#define creates the same convenience typedefs as defined by
1.91 + ///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates
1.92 + ///\c RedIt, \c BlueIt,
1.93 + ///
1.94 + ///\note If \c G it a template parameter, it should be used in this way.
1.95 + ///\code
1.96 + /// BPUGRAPH_TYPEDEFS(typename G);
1.97 + ///\endcode
1.98 + ///
1.99 + ///\warning There are no typedefs for the digraph maps because of the lack of
1.100 + ///template typedefs in C++.
1.101 +#define BPUGRAPH_TYPEDEFS(Digraph) \
1.102 + UGRAPH_TYPEDEFS(Digraph); \
1.103 + typedef Digraph::Red Red; \
1.104 + typedef Digraph::Blue Blue; \
1.105 + typedef Digraph::RedIt RedIt; \
1.106 + typedef Digraph::BlueIt BlueIt
1.107 +
1.108 + /// \brief Function to count the items in the digraph.
1.109 + ///
1.110 + /// This function counts the items (nodes, arcs etc) in the digraph.
1.111 + /// The complexity of the function is O(n) because
1.112 + /// it iterates on all of the items.
1.113 +
1.114 + template <typename Digraph, typename Item>
1.115 + inline int countItems(const Digraph& g) {
1.116 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
1.117 + int num = 0;
1.118 + for (ItemIt it(g); it != INVALID; ++it) {
1.119 + ++num;
1.120 + }
1.121 + return num;
1.122 + }
1.123 +
1.124 + // Node counting:
1.125 +
1.126 + namespace _digraph_utils_bits {
1.127 +
1.128 + template <typename Digraph, typename Enable = void>
1.129 + struct CountNodesSelector {
1.130 + static int count(const Digraph &g) {
1.131 + return countItems<Digraph, typename Digraph::Node>(g);
1.132 + }
1.133 + };
1.134 +
1.135 + template <typename Digraph>
1.136 + struct CountNodesSelector<
1.137 + Digraph, typename
1.138 + enable_if<typename Digraph::NodeNumTag, void>::type>
1.139 + {
1.140 + static int count(const Digraph &g) {
1.141 + return g.nodeNum();
1.142 + }
1.143 + };
1.144 + }
1.145 +
1.146 + /// \brief Function to count the nodes in the digraph.
1.147 + ///
1.148 + /// This function counts the nodes in the digraph.
1.149 + /// The complexity of the function is O(n) but for some
1.150 + /// digraph structures it is specialized to run in O(1).
1.151 + ///
1.152 + /// If the digraph contains a \e nodeNum() member function and a
1.153 + /// \e NodeNumTag tag then this function calls directly the member
1.154 + /// function to query the cardinality of the node set.
1.155 + template <typename Digraph>
1.156 + inline int countNodes(const Digraph& g) {
1.157 + return _digraph_utils_bits::CountNodesSelector<Digraph>::count(g);
1.158 + }
1.159 +
1.160 + namespace _digraph_utils_bits {
1.161 +
1.162 + template <typename Digraph, typename Enable = void>
1.163 + struct CountRedsSelector {
1.164 + static int count(const Digraph &g) {
1.165 + return countItems<Digraph, typename Digraph::Red>(g);
1.166 + }
1.167 + };
1.168 +
1.169 + template <typename Digraph>
1.170 + struct CountRedsSelector<
1.171 + Digraph, typename
1.172 + enable_if<typename Digraph::NodeNumTag, void>::type>
1.173 + {
1.174 + static int count(const Digraph &g) {
1.175 + return g.redNum();
1.176 + }
1.177 + };
1.178 + }
1.179 +
1.180 + /// \brief Function to count the reds in the digraph.
1.181 + ///
1.182 + /// This function counts the reds in the digraph.
1.183 + /// The complexity of the function is O(an) but for some
1.184 + /// digraph structures it is specialized to run in O(1).
1.185 + ///
1.186 + /// If the digraph contains an \e redNum() member function and a
1.187 + /// \e NodeNumTag tag then this function calls directly the member
1.188 + /// function to query the cardinality of the A-node set.
1.189 + template <typename Digraph>
1.190 + inline int countReds(const Digraph& g) {
1.191 + return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g);
1.192 + }
1.193 +
1.194 + namespace _digraph_utils_bits {
1.195 +
1.196 + template <typename Digraph, typename Enable = void>
1.197 + struct CountBluesSelector {
1.198 + static int count(const Digraph &g) {
1.199 + return countItems<Digraph, typename Digraph::Blue>(g);
1.200 + }
1.201 + };
1.202 +
1.203 + template <typename Digraph>
1.204 + struct CountBluesSelector<
1.205 + Digraph, typename
1.206 + enable_if<typename Digraph::NodeNumTag, void>::type>
1.207 + {
1.208 + static int count(const Digraph &g) {
1.209 + return g.blueNum();
1.210 + }
1.211 + };
1.212 + }
1.213 +
1.214 + /// \brief Function to count the blues in the digraph.
1.215 + ///
1.216 + /// This function counts the blues in the digraph.
1.217 + /// The complexity of the function is O(bn) but for some
1.218 + /// digraph structures it is specialized to run in O(1).
1.219 + ///
1.220 + /// If the digraph contains a \e blueNum() member function and a
1.221 + /// \e NodeNumTag tag then this function calls directly the member
1.222 + /// function to query the cardinality of the B-node set.
1.223 + template <typename Digraph>
1.224 + inline int countBlues(const Digraph& g) {
1.225 + return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g);
1.226 + }
1.227 +
1.228 +
1.229 + // Arc counting:
1.230 +
1.231 + namespace _digraph_utils_bits {
1.232 +
1.233 + template <typename Digraph, typename Enable = void>
1.234 + struct CountArcsSelector {
1.235 + static int count(const Digraph &g) {
1.236 + return countItems<Digraph, typename Digraph::Arc>(g);
1.237 + }
1.238 + };
1.239 +
1.240 + template <typename Digraph>
1.241 + struct CountArcsSelector<
1.242 + Digraph,
1.243 + typename enable_if<typename Digraph::ArcNumTag, void>::type>
1.244 + {
1.245 + static int count(const Digraph &g) {
1.246 + return g.arcNum();
1.247 + }
1.248 + };
1.249 + }
1.250 +
1.251 + /// \brief Function to count the arcs in the digraph.
1.252 + ///
1.253 + /// This function counts the arcs in the digraph.
1.254 + /// The complexity of the function is O(e) but for some
1.255 + /// digraph structures it is specialized to run in O(1).
1.256 + ///
1.257 + /// If the digraph contains a \e arcNum() member function and a
1.258 + /// \e ArcNumTag tag then this function calls directly the member
1.259 + /// function to query the cardinality of the arc set.
1.260 + template <typename Digraph>
1.261 + inline int countArcs(const Digraph& g) {
1.262 + return _digraph_utils_bits::CountArcsSelector<Digraph>::count(g);
1.263 + }
1.264 +
1.265 + // Undirected arc counting:
1.266 + namespace _digraph_utils_bits {
1.267 +
1.268 + template <typename Digraph, typename Enable = void>
1.269 + struct CountEdgesSelector {
1.270 + static int count(const Digraph &g) {
1.271 + return countItems<Digraph, typename Digraph::Edge>(g);
1.272 + }
1.273 + };
1.274 +
1.275 + template <typename Digraph>
1.276 + struct CountEdgesSelector<
1.277 + Digraph,
1.278 + typename enable_if<typename Digraph::ArcNumTag, void>::type>
1.279 + {
1.280 + static int count(const Digraph &g) {
1.281 + return g.edgeNum();
1.282 + }
1.283 + };
1.284 + }
1.285 +
1.286 + /// \brief Function to count the edges in the digraph.
1.287 + ///
1.288 + /// This function counts the edges in the digraph.
1.289 + /// The complexity of the function is O(e) but for some
1.290 + /// digraph structures it is specialized to run in O(1).
1.291 + ///
1.292 + /// If the digraph contains a \e edgeNum() member function and a
1.293 + /// \e ArcNumTag tag then this function calls directly the member
1.294 + /// function to query the cardinality of the edge set.
1.295 + template <typename Digraph>
1.296 + inline int countEdges(const Digraph& g) {
1.297 + return _digraph_utils_bits::CountEdgesSelector<Digraph>::count(g);
1.298 +
1.299 + }
1.300 +
1.301 +
1.302 + template <typename Digraph, typename DegIt>
1.303 + inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) {
1.304 + int num = 0;
1.305 + for (DegIt it(_g, _n); it != INVALID; ++it) {
1.306 + ++num;
1.307 + }
1.308 + return num;
1.309 + }
1.310 +
1.311 + /// \brief Function to count the number of the out-arcs from node \c n.
1.312 + ///
1.313 + /// This function counts the number of the out-arcs from node \c n
1.314 + /// in the digraph.
1.315 + template <typename Digraph>
1.316 + inline int countOutArcs(const Digraph& _g, const typename Digraph::Node& _n) {
1.317 + return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n);
1.318 + }
1.319 +
1.320 + /// \brief Function to count the number of the in-arcs to node \c n.
1.321 + ///
1.322 + /// This function counts the number of the in-arcs to node \c n
1.323 + /// in the digraph.
1.324 + template <typename Digraph>
1.325 + inline int countInArcs(const Digraph& _g, const typename Digraph::Node& _n) {
1.326 + return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n);
1.327 + }
1.328 +
1.329 + /// \brief Function to count the number of the inc-arcs to node \c n.
1.330 + ///
1.331 + /// This function counts the number of the inc-arcs to node \c n
1.332 + /// in the digraph.
1.333 + template <typename Digraph>
1.334 + inline int countIncArcs(const Digraph& _g, const typename Digraph::Node& _n) {
1.335 + return countNodeDegree<Digraph, typename Digraph::IncArcIt>(_g, _n);
1.336 + }
1.337 +
1.338 + namespace _digraph_utils_bits {
1.339 +
1.340 + template <typename Digraph, typename Enable = void>
1.341 + struct FindArcSelector {
1.342 + typedef typename Digraph::Node Node;
1.343 + typedef typename Digraph::Arc Arc;
1.344 + static Arc find(const Digraph &g, Node u, Node v, Arc e) {
1.345 + if (e == INVALID) {
1.346 + g.firstOut(e, u);
1.347 + } else {
1.348 + g.nextOut(e);
1.349 + }
1.350 + while (e != INVALID && g.target(e) != v) {
1.351 + g.nextOut(e);
1.352 + }
1.353 + return e;
1.354 + }
1.355 + };
1.356 +
1.357 + template <typename Digraph>
1.358 + struct FindArcSelector<
1.359 + Digraph,
1.360 + typename enable_if<typename Digraph::FindArcTag, void>::type>
1.361 + {
1.362 + typedef typename Digraph::Node Node;
1.363 + typedef typename Digraph::Arc Arc;
1.364 + static Arc find(const Digraph &g, Node u, Node v, Arc prev) {
1.365 + return g.findArc(u, v, prev);
1.366 + }
1.367 + };
1.368 + }
1.369 +
1.370 + /// \brief Finds an arc between two nodes of a digraph.
1.371 + ///
1.372 + /// Finds an arc from node \c u to node \c v in digraph \c g.
1.373 + ///
1.374 + /// If \c prev is \ref INVALID (this is the default value), then
1.375 + /// it finds the first arc from \c u to \c v. Otherwise it looks for
1.376 + /// the next arc from \c u to \c v after \c prev.
1.377 + /// \return The found arc or \ref INVALID if there is no such an arc.
1.378 + ///
1.379 + /// Thus you can iterate through each arc from \c u to \c v as it follows.
1.380 + ///\code
1.381 + /// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
1.382 + /// ...
1.383 + /// }
1.384 + ///\endcode
1.385 + ///
1.386 + ///\sa ArcLookUp
1.387 + ///\sa AllArcLookUp
1.388 + ///\sa DynArcLookUp
1.389 + ///\sa ConArcIt
1.390 + template <typename Digraph>
1.391 + inline typename Digraph::Arc
1.392 + findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
1.393 + typename Digraph::Arc prev = INVALID) {
1.394 + return _digraph_utils_bits::FindArcSelector<Digraph>::find(g, u, v, prev);
1.395 + }
1.396 +
1.397 + /// \brief Iterator for iterating on arcs connected the same nodes.
1.398 + ///
1.399 + /// Iterator for iterating on arcs connected the same nodes. It is
1.400 + /// higher level interface for the findArc() function. You can
1.401 + /// use it the following way:
1.402 + ///\code
1.403 + /// for (ConArcIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
1.404 + /// ...
1.405 + /// }
1.406 + ///\endcode
1.407 + ///
1.408 + ///\sa findArc()
1.409 + ///\sa ArcLookUp
1.410 + ///\sa AllArcLookUp
1.411 + ///\sa DynArcLookUp
1.412 + ///
1.413 + /// \author Balazs Dezso
1.414 + template <typename _Digraph>
1.415 + class ConArcIt : public _Digraph::Arc {
1.416 + public:
1.417 +
1.418 + typedef _Digraph Digraph;
1.419 + typedef typename Digraph::Arc Parent;
1.420 +
1.421 + typedef typename Digraph::Arc Arc;
1.422 + typedef typename Digraph::Node Node;
1.423 +
1.424 + /// \brief Constructor.
1.425 + ///
1.426 + /// Construct a new ConArcIt iterating on the arcs which
1.427 + /// connects the \c u and \c v node.
1.428 + ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) {
1.429 + Parent::operator=(findArc(digraph, u, v));
1.430 + }
1.431 +
1.432 + /// \brief Constructor.
1.433 + ///
1.434 + /// Construct a new ConArcIt which continues the iterating from
1.435 + /// the \c e arc.
1.436 + ConArcIt(const Digraph& g, Arc e) : Parent(e), digraph(g) {}
1.437 +
1.438 + /// \brief Increment operator.
1.439 + ///
1.440 + /// It increments the iterator and gives back the next arc.
1.441 + ConArcIt& operator++() {
1.442 + Parent::operator=(findArc(digraph, digraph.source(*this),
1.443 + digraph.target(*this), *this));
1.444 + return *this;
1.445 + }
1.446 + private:
1.447 + const Digraph& digraph;
1.448 + };
1.449 +
1.450 + namespace _digraph_utils_bits {
1.451 +
1.452 + template <typename Digraph, typename Enable = void>
1.453 + struct FindEdgeSelector {
1.454 + typedef typename Digraph::Node Node;
1.455 + typedef typename Digraph::Edge Edge;
1.456 + static Edge find(const Digraph &g, Node u, Node v, Edge e) {
1.457 + bool b;
1.458 + if (u != v) {
1.459 + if (e == INVALID) {
1.460 + g.firstInc(e, b, u);
1.461 + } else {
1.462 + b = g.source(e) == u;
1.463 + g.nextInc(e, b);
1.464 + }
1.465 + while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
1.466 + g.nextInc(e, b);
1.467 + }
1.468 + } else {
1.469 + if (e == INVALID) {
1.470 + g.firstInc(e, b, u);
1.471 + } else {
1.472 + b = true;
1.473 + g.nextInc(e, b);
1.474 + }
1.475 + while (e != INVALID && (!b || g.target(e) != v)) {
1.476 + g.nextInc(e, b);
1.477 + }
1.478 + }
1.479 + return e;
1.480 + }
1.481 + };
1.482 +
1.483 + template <typename Digraph>
1.484 + struct FindEdgeSelector<
1.485 + Digraph,
1.486 + typename enable_if<typename Digraph::FindArcTag, void>::type>
1.487 + {
1.488 + typedef typename Digraph::Node Node;
1.489 + typedef typename Digraph::Edge Edge;
1.490 + static Edge find(const Digraph &g, Node u, Node v, Edge prev) {
1.491 + return g.findEdge(u, v, prev);
1.492 + }
1.493 + };
1.494 + }
1.495 +
1.496 + /// \brief Finds an edge between two nodes of a digraph.
1.497 + ///
1.498 + /// Finds an edge from node \c u to node \c v in digraph \c g.
1.499 + /// If the node \c u and node \c v is equal then each loop arc
1.500 + /// will be enumerated.
1.501 + ///
1.502 + /// If \c prev is \ref INVALID (this is the default value), then
1.503 + /// it finds the first arc from \c u to \c v. Otherwise it looks for
1.504 + /// the next arc from \c u to \c v after \c prev.
1.505 + /// \return The found arc or \ref INVALID if there is no such an arc.
1.506 + ///
1.507 + /// Thus you can iterate through each arc from \c u to \c v as it follows.
1.508 + ///\code
1.509 + /// for(Edge e = findEdge(g,u,v); e != INVALID;
1.510 + /// e = findEdge(g,u,v,e)) {
1.511 + /// ...
1.512 + /// }
1.513 + ///\endcode
1.514 + ///
1.515 + ///\sa ConArcIt
1.516 +
1.517 + template <typename Digraph>
1.518 + inline typename Digraph::Edge
1.519 + findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
1.520 + typename Digraph::Edge p = INVALID) {
1.521 + return _digraph_utils_bits::FindEdgeSelector<Digraph>::find(g, u, v, p);
1.522 + }
1.523 +
1.524 + /// \brief Iterator for iterating on edges connected the same nodes.
1.525 + ///
1.526 + /// Iterator for iterating on edges connected the same nodes. It is
1.527 + /// higher level interface for the findEdge() function. You can
1.528 + /// use it the following way:
1.529 + ///\code
1.530 + /// for (ConEdgeIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
1.531 + /// ...
1.532 + /// }
1.533 + ///\endcode
1.534 + ///
1.535 + ///\sa findEdge()
1.536 + ///
1.537 + /// \author Balazs Dezso
1.538 + template <typename _Digraph>
1.539 + class ConEdgeIt : public _Digraph::Edge {
1.540 + public:
1.541 +
1.542 + typedef _Digraph Digraph;
1.543 + typedef typename Digraph::Edge Parent;
1.544 +
1.545 + typedef typename Digraph::Edge Edge;
1.546 + typedef typename Digraph::Node Node;
1.547 +
1.548 + /// \brief Constructor.
1.549 + ///
1.550 + /// Construct a new ConEdgeIt iterating on the arcs which
1.551 + /// connects the \c u and \c v node.
1.552 + ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) {
1.553 + Parent::operator=(findEdge(digraph, u, v));
1.554 + }
1.555 +
1.556 + /// \brief Constructor.
1.557 + ///
1.558 + /// Construct a new ConEdgeIt which continues the iterating from
1.559 + /// the \c e arc.
1.560 + ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {}
1.561 +
1.562 + /// \brief Increment operator.
1.563 + ///
1.564 + /// It increments the iterator and gives back the next arc.
1.565 + ConEdgeIt& operator++() {
1.566 + Parent::operator=(findEdge(digraph, digraph.source(*this),
1.567 + digraph.target(*this), *this));
1.568 + return *this;
1.569 + }
1.570 + private:
1.571 + const Digraph& digraph;
1.572 + };
1.573 +
1.574 + /// \brief Copy a map.
1.575 + ///
1.576 + /// This function copies the \c from map to the \c to map. It uses the
1.577 + /// given iterator to iterate on the data structure and it uses the \c ref
1.578 + /// mapping to convert the from's keys to the to's keys.
1.579 + template <typename To, typename From,
1.580 + typename ItemIt, typename Ref>
1.581 + void copyMap(To& to, const From& from,
1.582 + ItemIt it, const Ref& ref) {
1.583 + for (; it != INVALID; ++it) {
1.584 + to[ref[it]] = from[it];
1.585 + }
1.586 + }
1.587 +
1.588 + /// \brief Copy the from map to the to map.
1.589 + ///
1.590 + /// Copy the \c from map to the \c to map. It uses the given iterator
1.591 + /// to iterate on the data structure.
1.592 + template <typename To, typename From, typename ItemIt>
1.593 + void copyMap(To& to, const From& from, ItemIt it) {
1.594 + for (; it != INVALID; ++it) {
1.595 + to[it] = from[it];
1.596 + }
1.597 + }
1.598 +
1.599 + namespace _digraph_utils_bits {
1.600 +
1.601 + template <typename Digraph, typename Item, typename RefMap>
1.602 + class MapCopyBase {
1.603 + public:
1.604 + virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
1.605 +
1.606 + virtual ~MapCopyBase() {}
1.607 + };
1.608 +
1.609 + template <typename Digraph, typename Item, typename RefMap,
1.610 + typename ToMap, typename FromMap>
1.611 + class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
1.612 + public:
1.613 +
1.614 + MapCopy(ToMap& tmap, const FromMap& map)
1.615 + : _tmap(tmap), _map(map) {}
1.616 +
1.617 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
1.618 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
1.619 + for (ItemIt it(digraph); it != INVALID; ++it) {
1.620 + _tmap.set(refMap[it], _map[it]);
1.621 + }
1.622 + }
1.623 +
1.624 + private:
1.625 + ToMap& _tmap;
1.626 + const FromMap& _map;
1.627 + };
1.628 +
1.629 + template <typename Digraph, typename Item, typename RefMap, typename It>
1.630 + class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
1.631 + public:
1.632 +
1.633 + ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
1.634 +
1.635 + virtual void copy(const Digraph&, const RefMap& refMap) {
1.636 + _it = refMap[_item];
1.637 + }
1.638 +
1.639 + private:
1.640 + It& _it;
1.641 + Item _item;
1.642 + };
1.643 +
1.644 + template <typename Digraph, typename Item, typename RefMap, typename Ref>
1.645 + class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
1.646 + public:
1.647 +
1.648 + RefCopy(Ref& map) : _map(map) {}
1.649 +
1.650 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
1.651 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
1.652 + for (ItemIt it(digraph); it != INVALID; ++it) {
1.653 + _map.set(it, refMap[it]);
1.654 + }
1.655 + }
1.656 +
1.657 + private:
1.658 + Ref& _map;
1.659 + };
1.660 +
1.661 + template <typename Digraph, typename Item, typename RefMap,
1.662 + typename CrossRef>
1.663 + class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
1.664 + public:
1.665 +
1.666 + CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
1.667 +
1.668 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
1.669 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
1.670 + for (ItemIt it(digraph); it != INVALID; ++it) {
1.671 + _cmap.set(refMap[it], it);
1.672 + }
1.673 + }
1.674 +
1.675 + private:
1.676 + CrossRef& _cmap;
1.677 + };
1.678 +
1.679 + template <typename Digraph, typename Enable = void>
1.680 + struct DigraphCopySelector {
1.681 + template <typename From, typename NodeRefMap, typename ArcRefMap>
1.682 + static void copy(Digraph &to, const From& from,
1.683 + NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
1.684 + for (typename From::NodeIt it(from); it != INVALID; ++it) {
1.685 + nodeRefMap[it] = to.addNode();
1.686 + }
1.687 + for (typename From::ArcIt it(from); it != INVALID; ++it) {
1.688 + arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
1.689 + nodeRefMap[from.target(it)]);
1.690 + }
1.691 + }
1.692 + };
1.693 +
1.694 + template <typename Digraph>
1.695 + struct DigraphCopySelector<
1.696 + Digraph,
1.697 + typename enable_if<typename Digraph::BuildTag, void>::type>
1.698 + {
1.699 + template <typename From, typename NodeRefMap, typename ArcRefMap>
1.700 + static void copy(Digraph &to, const From& from,
1.701 + NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
1.702 + to.build(from, nodeRefMap, arcRefMap);
1.703 + }
1.704 + };
1.705 +
1.706 + template <typename Graph, typename Enable = void>
1.707 + struct GraphCopySelector {
1.708 + template <typename From, typename NodeRefMap, typename EdgeRefMap>
1.709 + static void copy(Graph &to, const From& from,
1.710 + NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
1.711 + for (typename From::NodeIt it(from); it != INVALID; ++it) {
1.712 + nodeRefMap[it] = to.addNode();
1.713 + }
1.714 + for (typename From::EdgeIt it(from); it != INVALID; ++it) {
1.715 + edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
1.716 + nodeRefMap[from.target(it)]);
1.717 + }
1.718 + }
1.719 + };
1.720 +
1.721 + template <typename Graph>
1.722 + struct GraphCopySelector<
1.723 + Graph,
1.724 + typename enable_if<typename Graph::BuildTag, void>::type>
1.725 + {
1.726 + template <typename From, typename NodeRefMap, typename EdgeRefMap>
1.727 + static void copy(Graph &to, const From& from,
1.728 + NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
1.729 + to.build(from, nodeRefMap, edgeRefMap);
1.730 + }
1.731 + };
1.732 +
1.733 + template <typename BpGraph, typename Enable = void>
1.734 + struct BpGraphCopySelector {
1.735 + template <typename From, typename RedRefMap,
1.736 + typename BlueRefMap, typename EdgeRefMap>
1.737 + static void copy(BpGraph &to, const From& from,
1.738 + RedRefMap& redRefMap, BlueRefMap& blueRefMap,
1.739 + EdgeRefMap& edgeRefMap) {
1.740 + for (typename From::RedIt it(from); it != INVALID; ++it) {
1.741 + redRefMap[it] = to.addRed();
1.742 + }
1.743 + for (typename From::BlueIt it(from); it != INVALID; ++it) {
1.744 + blueRefMap[it] = to.addBlue();
1.745 + }
1.746 + for (typename From::EdgeIt it(from); it != INVALID; ++it) {
1.747 + edgeRefMap[it] = to.addArc(redRefMap[from.red(it)],
1.748 + blueRefMap[from.blue(it)]);
1.749 + }
1.750 + }
1.751 + };
1.752 +
1.753 + template <typename BpGraph>
1.754 + struct BpGraphCopySelector<
1.755 + BpGraph,
1.756 + typename enable_if<typename BpGraph::BuildTag, void>::type>
1.757 + {
1.758 + template <typename From, typename RedRefMap,
1.759 + typename BlueRefMap, typename EdgeRefMap>
1.760 + static void copy(BpGraph &to, const From& from,
1.761 + RedRefMap& redRefMap, BlueRefMap& blueRefMap,
1.762 + EdgeRefMap& edgeRefMap) {
1.763 + to.build(from, redRefMap, blueRefMap, edgeRefMap);
1.764 + }
1.765 + };
1.766 +
1.767 +
1.768 + }
1.769 +
1.770 + /// \brief Class to copy a digraph.
1.771 + ///
1.772 + /// Class to copy a digraph to another digraph (duplicate a digraph). The
1.773 + /// simplest way of using it is through the \c copyDigraph() function.
1.774 + template <typename To, typename From>
1.775 + class DigraphCopy {
1.776 + private:
1.777 +
1.778 + typedef typename From::Node Node;
1.779 + typedef typename From::NodeIt NodeIt;
1.780 + typedef typename From::Arc Arc;
1.781 + typedef typename From::ArcIt ArcIt;
1.782 +
1.783 + typedef typename To::Node TNode;
1.784 + typedef typename To::Arc TArc;
1.785 +
1.786 + typedef typename From::template NodeMap<TNode> NodeRefMap;
1.787 + typedef typename From::template ArcMap<TArc> ArcRefMap;
1.788 +
1.789 +
1.790 + public:
1.791 +
1.792 +
1.793 + /// \brief Constructor for the DigraphCopy.
1.794 + ///
1.795 + /// It copies the content of the \c _from digraph into the
1.796 + /// \c _to digraph.
1.797 + DigraphCopy(To& _to, const From& _from)
1.798 + : from(_from), to(_to) {}
1.799 +
1.800 + /// \brief Destructor of the DigraphCopy
1.801 + ///
1.802 + /// Destructor of the DigraphCopy
1.803 + ~DigraphCopy() {
1.804 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.805 + delete nodeMapCopies[i];
1.806 + }
1.807 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.808 + delete arcMapCopies[i];
1.809 + }
1.810 +
1.811 + }
1.812 +
1.813 + /// \brief Copies the node references into the given map.
1.814 + ///
1.815 + /// Copies the node references into the given map.
1.816 + template <typename NodeRef>
1.817 + DigraphCopy& nodeRef(NodeRef& map) {
1.818 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
1.819 + NodeRefMap, NodeRef>(map));
1.820 + return *this;
1.821 + }
1.822 +
1.823 + /// \brief Copies the node cross references into the given map.
1.824 + ///
1.825 + /// Copies the node cross references (reverse references) into
1.826 + /// the given map.
1.827 + template <typename NodeCrossRef>
1.828 + DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
1.829 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
1.830 + NodeRefMap, NodeCrossRef>(map));
1.831 + return *this;
1.832 + }
1.833 +
1.834 + /// \brief Make copy of the given map.
1.835 + ///
1.836 + /// Makes copy of the given map for the newly created digraph.
1.837 + /// The new map's key type is the to digraph's node type,
1.838 + /// and the copied map's key type is the from digraph's node
1.839 + /// type.
1.840 + template <typename ToMap, typename FromMap>
1.841 + DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
1.842 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
1.843 + NodeRefMap, ToMap, FromMap>(tmap, map));
1.844 + return *this;
1.845 + }
1.846 +
1.847 + /// \brief Make a copy of the given node.
1.848 + ///
1.849 + /// Make a copy of the given node.
1.850 + DigraphCopy& node(TNode& tnode, const Node& snode) {
1.851 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
1.852 + NodeRefMap, TNode>(tnode, snode));
1.853 + return *this;
1.854 + }
1.855 +
1.856 + /// \brief Copies the arc references into the given map.
1.857 + ///
1.858 + /// Copies the arc references into the given map.
1.859 + template <typename ArcRef>
1.860 + DigraphCopy& arcRef(ArcRef& map) {
1.861 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
1.862 + ArcRefMap, ArcRef>(map));
1.863 + return *this;
1.864 + }
1.865 +
1.866 + /// \brief Copies the arc cross references into the given map.
1.867 + ///
1.868 + /// Copies the arc cross references (reverse references) into
1.869 + /// the given map.
1.870 + template <typename ArcCrossRef>
1.871 + DigraphCopy& arcCrossRef(ArcCrossRef& map) {
1.872 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
1.873 + ArcRefMap, ArcCrossRef>(map));
1.874 + return *this;
1.875 + }
1.876 +
1.877 + /// \brief Make copy of the given map.
1.878 + ///
1.879 + /// Makes copy of the given map for the newly created digraph.
1.880 + /// The new map's key type is the to digraph's arc type,
1.881 + /// and the copied map's key type is the from digraph's arc
1.882 + /// type.
1.883 + template <typename ToMap, typename FromMap>
1.884 + DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
1.885 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
1.886 + ArcRefMap, ToMap, FromMap>(tmap, map));
1.887 + return *this;
1.888 + }
1.889 +
1.890 + /// \brief Make a copy of the given arc.
1.891 + ///
1.892 + /// Make a copy of the given arc.
1.893 + DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
1.894 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
1.895 + ArcRefMap, TArc>(tarc, sarc));
1.896 + return *this;
1.897 + }
1.898 +
1.899 + /// \brief Executes the copies.
1.900 + ///
1.901 + /// Executes the copies.
1.902 + void run() {
1.903 + NodeRefMap nodeRefMap(from);
1.904 + ArcRefMap arcRefMap(from);
1.905 + _digraph_utils_bits::DigraphCopySelector<To>::
1.906 + copy(to, from, nodeRefMap, arcRefMap);
1.907 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.908 + nodeMapCopies[i]->copy(from, nodeRefMap);
1.909 + }
1.910 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.911 + arcMapCopies[i]->copy(from, arcRefMap);
1.912 + }
1.913 + }
1.914 +
1.915 + protected:
1.916 +
1.917 +
1.918 + const From& from;
1.919 + To& to;
1.920 +
1.921 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
1.922 + nodeMapCopies;
1.923 +
1.924 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1.925 + arcMapCopies;
1.926 +
1.927 + };
1.928 +
1.929 + /// \brief Copy a digraph to another digraph.
1.930 + ///
1.931 + /// Copy a digraph to another digraph.
1.932 + /// The usage of the function:
1.933 + ///
1.934 + ///\code
1.935 + /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
1.936 + ///\endcode
1.937 + ///
1.938 + /// After the copy the \c nr map will contain the mapping from the
1.939 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
1.940 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
1.941 + /// to the arcs of the \c from digraph.
1.942 + ///
1.943 + /// \see DigraphCopy
1.944 + template <typename To, typename From>
1.945 + DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
1.946 + return DigraphCopy<To, From>(to, from);
1.947 + }
1.948 +
1.949 + /// \brief Class to copy an graph.
1.950 + ///
1.951 + /// Class to copy an graph to another digraph (duplicate a digraph).
1.952 + /// The simplest way of using it is through the \c copyGraph() function.
1.953 + template <typename To, typename From>
1.954 + class GraphCopy {
1.955 + private:
1.956 +
1.957 + typedef typename From::Node Node;
1.958 + typedef typename From::NodeIt NodeIt;
1.959 + typedef typename From::Arc Arc;
1.960 + typedef typename From::ArcIt ArcIt;
1.961 + typedef typename From::Edge Edge;
1.962 + typedef typename From::EdgeIt EdgeIt;
1.963 +
1.964 + typedef typename To::Node TNode;
1.965 + typedef typename To::Arc TArc;
1.966 + typedef typename To::Edge TEdge;
1.967 +
1.968 + typedef typename From::template NodeMap<TNode> NodeRefMap;
1.969 + typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
1.970 +
1.971 + struct ArcRefMap {
1.972 + ArcRefMap(const To& _to, const From& _from,
1.973 + const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
1.974 + : to(_to), from(_from),
1.975 + edge_ref(_edge_ref), node_ref(_node_ref) {}
1.976 +
1.977 + typedef typename From::Arc Key;
1.978 + typedef typename To::Arc Value;
1.979 +
1.980 + Value operator[](const Key& key) const {
1.981 + bool forward =
1.982 + (from.direction(key) ==
1.983 + (node_ref[from.source(static_cast<const Edge&>(key))] ==
1.984 + to.source(edge_ref[static_cast<const Edge&>(key)])));
1.985 + return to.direct(edge_ref[key], forward);
1.986 + }
1.987 +
1.988 + const To& to;
1.989 + const From& from;
1.990 + const EdgeRefMap& edge_ref;
1.991 + const NodeRefMap& node_ref;
1.992 + };
1.993 +
1.994 +
1.995 + public:
1.996 +
1.997 +
1.998 + /// \brief Constructor for the DigraphCopy.
1.999 + ///
1.1000 + /// It copies the content of the \c _from digraph into the
1.1001 + /// \c _to digraph.
1.1002 + GraphCopy(To& _to, const From& _from)
1.1003 + : from(_from), to(_to) {}
1.1004 +
1.1005 + /// \brief Destructor of the DigraphCopy
1.1006 + ///
1.1007 + /// Destructor of the DigraphCopy
1.1008 + ~GraphCopy() {
1.1009 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.1010 + delete nodeMapCopies[i];
1.1011 + }
1.1012 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.1013 + delete arcMapCopies[i];
1.1014 + }
1.1015 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1.1016 + delete edgeMapCopies[i];
1.1017 + }
1.1018 +
1.1019 + }
1.1020 +
1.1021 + /// \brief Copies the node references into the given map.
1.1022 + ///
1.1023 + /// Copies the node references into the given map.
1.1024 + template <typename NodeRef>
1.1025 + GraphCopy& nodeRef(NodeRef& map) {
1.1026 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
1.1027 + NodeRefMap, NodeRef>(map));
1.1028 + return *this;
1.1029 + }
1.1030 +
1.1031 + /// \brief Copies the node cross references into the given map.
1.1032 + ///
1.1033 + /// Copies the node cross references (reverse references) into
1.1034 + /// the given map.
1.1035 + template <typename NodeCrossRef>
1.1036 + GraphCopy& nodeCrossRef(NodeCrossRef& map) {
1.1037 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
1.1038 + NodeRefMap, NodeCrossRef>(map));
1.1039 + return *this;
1.1040 + }
1.1041 +
1.1042 + /// \brief Make copy of the given map.
1.1043 + ///
1.1044 + /// Makes copy of the given map for the newly created digraph.
1.1045 + /// The new map's key type is the to digraph's node type,
1.1046 + /// and the copied map's key type is the from digraph's node
1.1047 + /// type.
1.1048 + template <typename ToMap, typename FromMap>
1.1049 + GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
1.1050 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
1.1051 + NodeRefMap, ToMap, FromMap>(tmap, map));
1.1052 + return *this;
1.1053 + }
1.1054 +
1.1055 + /// \brief Make a copy of the given node.
1.1056 + ///
1.1057 + /// Make a copy of the given node.
1.1058 + GraphCopy& node(TNode& tnode, const Node& snode) {
1.1059 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
1.1060 + NodeRefMap, TNode>(tnode, snode));
1.1061 + return *this;
1.1062 + }
1.1063 +
1.1064 + /// \brief Copies the arc references into the given map.
1.1065 + ///
1.1066 + /// Copies the arc references into the given map.
1.1067 + template <typename ArcRef>
1.1068 + GraphCopy& arcRef(ArcRef& map) {
1.1069 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
1.1070 + ArcRefMap, ArcRef>(map));
1.1071 + return *this;
1.1072 + }
1.1073 +
1.1074 + /// \brief Copies the arc cross references into the given map.
1.1075 + ///
1.1076 + /// Copies the arc cross references (reverse references) into
1.1077 + /// the given map.
1.1078 + template <typename ArcCrossRef>
1.1079 + GraphCopy& arcCrossRef(ArcCrossRef& map) {
1.1080 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
1.1081 + ArcRefMap, ArcCrossRef>(map));
1.1082 + return *this;
1.1083 + }
1.1084 +
1.1085 + /// \brief Make copy of the given map.
1.1086 + ///
1.1087 + /// Makes copy of the given map for the newly created digraph.
1.1088 + /// The new map's key type is the to digraph's arc type,
1.1089 + /// and the copied map's key type is the from digraph's arc
1.1090 + /// type.
1.1091 + template <typename ToMap, typename FromMap>
1.1092 + GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
1.1093 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
1.1094 + ArcRefMap, ToMap, FromMap>(tmap, map));
1.1095 + return *this;
1.1096 + }
1.1097 +
1.1098 + /// \brief Make a copy of the given arc.
1.1099 + ///
1.1100 + /// Make a copy of the given arc.
1.1101 + GraphCopy& arc(TArc& tarc, const Arc& sarc) {
1.1102 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
1.1103 + ArcRefMap, TArc>(tarc, sarc));
1.1104 + return *this;
1.1105 + }
1.1106 +
1.1107 + /// \brief Copies the edge references into the given map.
1.1108 + ///
1.1109 + /// Copies the edge references into the given map.
1.1110 + template <typename EdgeRef>
1.1111 + GraphCopy& edgeRef(EdgeRef& map) {
1.1112 + edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
1.1113 + EdgeRefMap, EdgeRef>(map));
1.1114 + return *this;
1.1115 + }
1.1116 +
1.1117 + /// \brief Copies the edge cross references into the given map.
1.1118 + ///
1.1119 + /// Copies the edge cross references (reverse
1.1120 + /// references) into the given map.
1.1121 + template <typename EdgeCrossRef>
1.1122 + GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1.1123 + edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
1.1124 + Edge, EdgeRefMap, EdgeCrossRef>(map));
1.1125 + return *this;
1.1126 + }
1.1127 +
1.1128 + /// \brief Make copy of the given map.
1.1129 + ///
1.1130 + /// Makes copy of the given map for the newly created digraph.
1.1131 + /// The new map's key type is the to digraph's edge type,
1.1132 + /// and the copied map's key type is the from digraph's edge
1.1133 + /// type.
1.1134 + template <typename ToMap, typename FromMap>
1.1135 + GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
1.1136 + edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
1.1137 + EdgeRefMap, ToMap, FromMap>(tmap, map));
1.1138 + return *this;
1.1139 + }
1.1140 +
1.1141 + /// \brief Make a copy of the given edge.
1.1142 + ///
1.1143 + /// Make a copy of the given edge.
1.1144 + GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
1.1145 + edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
1.1146 + EdgeRefMap, TEdge>(tedge, sedge));
1.1147 + return *this;
1.1148 + }
1.1149 +
1.1150 + /// \brief Executes the copies.
1.1151 + ///
1.1152 + /// Executes the copies.
1.1153 + void run() {
1.1154 + NodeRefMap nodeRefMap(from);
1.1155 + EdgeRefMap edgeRefMap(from);
1.1156 + ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
1.1157 + _digraph_utils_bits::GraphCopySelector<To>::
1.1158 + copy(to, from, nodeRefMap, edgeRefMap);
1.1159 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.1160 + nodeMapCopies[i]->copy(from, nodeRefMap);
1.1161 + }
1.1162 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1.1163 + edgeMapCopies[i]->copy(from, edgeRefMap);
1.1164 + }
1.1165 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.1166 + arcMapCopies[i]->copy(from, arcRefMap);
1.1167 + }
1.1168 + }
1.1169 +
1.1170 + private:
1.1171 +
1.1172 + const From& from;
1.1173 + To& to;
1.1174 +
1.1175 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
1.1176 + nodeMapCopies;
1.1177 +
1.1178 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1.1179 + arcMapCopies;
1.1180 +
1.1181 + std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1.1182 + edgeMapCopies;
1.1183 +
1.1184 + };
1.1185 +
1.1186 + /// \brief Copy an graph to another digraph.
1.1187 + ///
1.1188 + /// Copy an graph to another digraph.
1.1189 + /// The usage of the function:
1.1190 + ///
1.1191 + ///\code
1.1192 + /// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
1.1193 + ///\endcode
1.1194 + ///
1.1195 + /// After the copy the \c nr map will contain the mapping from the
1.1196 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
1.1197 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
1.1198 + /// to the arcs of the \c from digraph.
1.1199 + ///
1.1200 + /// \see GraphCopy
1.1201 + template <typename To, typename From>
1.1202 + GraphCopy<To, From>
1.1203 + copyGraph(To& to, const From& from) {
1.1204 + return GraphCopy<To, From>(to, from);
1.1205 + }
1.1206 +
1.1207 + /// \brief Class to copy a bipartite digraph.
1.1208 + ///
1.1209 + /// Class to copy a bipartite digraph to another digraph
1.1210 + /// (duplicate a digraph). The simplest way of using it is through
1.1211 + /// the \c copyBpGraph() function.
1.1212 + template <typename To, typename From>
1.1213 + class BpGraphCopy {
1.1214 + private:
1.1215 +
1.1216 + typedef typename From::Node Node;
1.1217 + typedef typename From::Red Red;
1.1218 + typedef typename From::Blue Blue;
1.1219 + typedef typename From::NodeIt NodeIt;
1.1220 + typedef typename From::Arc Arc;
1.1221 + typedef typename From::ArcIt ArcIt;
1.1222 + typedef typename From::Edge Edge;
1.1223 + typedef typename From::EdgeIt EdgeIt;
1.1224 +
1.1225 + typedef typename To::Node TNode;
1.1226 + typedef typename To::Arc TArc;
1.1227 + typedef typename To::Edge TEdge;
1.1228 +
1.1229 + typedef typename From::template RedMap<TNode> RedRefMap;
1.1230 + typedef typename From::template BlueMap<TNode> BlueRefMap;
1.1231 + typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
1.1232 +
1.1233 + struct NodeRefMap {
1.1234 + NodeRefMap(const From& _from, const RedRefMap& _red_ref,
1.1235 + const BlueRefMap& _blue_ref)
1.1236 + : from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {}
1.1237 +
1.1238 + typedef typename From::Node Key;
1.1239 + typedef typename To::Node Value;
1.1240 +
1.1241 + Value operator[](const Key& key) const {
1.1242 + return from.red(key) ? red_ref[key] : blue_ref[key];
1.1243 + }
1.1244 +
1.1245 + const From& from;
1.1246 + const RedRefMap& red_ref;
1.1247 + const BlueRefMap& blue_ref;
1.1248 + };
1.1249 +
1.1250 + struct ArcRefMap {
1.1251 + ArcRefMap(const To& _to, const From& _from,
1.1252 + const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
1.1253 + : to(_to), from(_from),
1.1254 + edge_ref(_edge_ref), node_ref(_node_ref) {}
1.1255 +
1.1256 + typedef typename From::Arc Key;
1.1257 + typedef typename To::Arc Value;
1.1258 +
1.1259 + Value operator[](const Key& key) const {
1.1260 + bool forward =
1.1261 + (from.direction(key) ==
1.1262 + (node_ref[from.source(static_cast<const Edge&>(key))] ==
1.1263 + to.source(edge_ref[static_cast<const Edge&>(key)])));
1.1264 + return to.direct(edge_ref[key], forward);
1.1265 + }
1.1266 +
1.1267 + const To& to;
1.1268 + const From& from;
1.1269 + const EdgeRefMap& edge_ref;
1.1270 + const NodeRefMap& node_ref;
1.1271 + };
1.1272 +
1.1273 + public:
1.1274 +
1.1275 +
1.1276 + /// \brief Constructor for the DigraphCopy.
1.1277 + ///
1.1278 + /// It copies the content of the \c _from digraph into the
1.1279 + /// \c _to digraph.
1.1280 + BpGraphCopy(To& _to, const From& _from)
1.1281 + : from(_from), to(_to) {}
1.1282 +
1.1283 + /// \brief Destructor of the DigraphCopy
1.1284 + ///
1.1285 + /// Destructor of the DigraphCopy
1.1286 + ~BpGraphCopy() {
1.1287 + for (int i = 0; i < int(redMapCopies.size()); ++i) {
1.1288 + delete redMapCopies[i];
1.1289 + }
1.1290 + for (int i = 0; i < int(blueMapCopies.size()); ++i) {
1.1291 + delete blueMapCopies[i];
1.1292 + }
1.1293 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.1294 + delete nodeMapCopies[i];
1.1295 + }
1.1296 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.1297 + delete arcMapCopies[i];
1.1298 + }
1.1299 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1.1300 + delete edgeMapCopies[i];
1.1301 + }
1.1302 +
1.1303 + }
1.1304 +
1.1305 + /// \brief Copies the A-node references into the given map.
1.1306 + ///
1.1307 + /// Copies the A-node references into the given map.
1.1308 + template <typename RedRef>
1.1309 + BpGraphCopy& redRef(RedRef& map) {
1.1310 + redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red,
1.1311 + RedRefMap, RedRef>(map));
1.1312 + return *this;
1.1313 + }
1.1314 +
1.1315 + /// \brief Copies the A-node cross references into the given map.
1.1316 + ///
1.1317 + /// Copies the A-node cross references (reverse references) into
1.1318 + /// the given map.
1.1319 + template <typename RedCrossRef>
1.1320 + BpGraphCopy& redCrossRef(RedCrossRef& map) {
1.1321 + redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
1.1322 + Red, RedRefMap, RedCrossRef>(map));
1.1323 + return *this;
1.1324 + }
1.1325 +
1.1326 + /// \brief Make copy of the given A-node map.
1.1327 + ///
1.1328 + /// Makes copy of the given map for the newly created digraph.
1.1329 + /// The new map's key type is the to digraph's node type,
1.1330 + /// and the copied map's key type is the from digraph's node
1.1331 + /// type.
1.1332 + template <typename ToMap, typename FromMap>
1.1333 + BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) {
1.1334 + redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red,
1.1335 + RedRefMap, ToMap, FromMap>(tmap, map));
1.1336 + return *this;
1.1337 + }
1.1338 +
1.1339 + /// \brief Copies the B-node references into the given map.
1.1340 + ///
1.1341 + /// Copies the B-node references into the given map.
1.1342 + template <typename BlueRef>
1.1343 + BpGraphCopy& blueRef(BlueRef& map) {
1.1344 + blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue,
1.1345 + BlueRefMap, BlueRef>(map));
1.1346 + return *this;
1.1347 + }
1.1348 +
1.1349 + /// \brief Copies the B-node cross references into the given map.
1.1350 + ///
1.1351 + /// Copies the B-node cross references (reverse references) into
1.1352 + /// the given map.
1.1353 + template <typename BlueCrossRef>
1.1354 + BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
1.1355 + blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
1.1356 + Blue, BlueRefMap, BlueCrossRef>(map));
1.1357 + return *this;
1.1358 + }
1.1359 +
1.1360 + /// \brief Make copy of the given B-node map.
1.1361 + ///
1.1362 + /// Makes copy of the given map for the newly created digraph.
1.1363 + /// The new map's key type is the to digraph's node type,
1.1364 + /// and the copied map's key type is the from digraph's node
1.1365 + /// type.
1.1366 + template <typename ToMap, typename FromMap>
1.1367 + BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) {
1.1368 + blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue,
1.1369 + BlueRefMap, ToMap, FromMap>(tmap, map));
1.1370 + return *this;
1.1371 + }
1.1372 + /// \brief Copies the node references into the given map.
1.1373 + ///
1.1374 + /// Copies the node references into the given map.
1.1375 + template <typename NodeRef>
1.1376 + BpGraphCopy& nodeRef(NodeRef& map) {
1.1377 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
1.1378 + NodeRefMap, NodeRef>(map));
1.1379 + return *this;
1.1380 + }
1.1381 +
1.1382 + /// \brief Copies the node cross references into the given map.
1.1383 + ///
1.1384 + /// Copies the node cross references (reverse references) into
1.1385 + /// the given map.
1.1386 + template <typename NodeCrossRef>
1.1387 + BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1.1388 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
1.1389 + NodeRefMap, NodeCrossRef>(map));
1.1390 + return *this;
1.1391 + }
1.1392 +
1.1393 + /// \brief Make copy of the given map.
1.1394 + ///
1.1395 + /// Makes copy of the given map for the newly created digraph.
1.1396 + /// The new map's key type is the to digraph's node type,
1.1397 + /// and the copied map's key type is the from digraph's node
1.1398 + /// type.
1.1399 + template <typename ToMap, typename FromMap>
1.1400 + BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
1.1401 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
1.1402 + NodeRefMap, ToMap, FromMap>(tmap, map));
1.1403 + return *this;
1.1404 + }
1.1405 +
1.1406 + /// \brief Make a copy of the given node.
1.1407 + ///
1.1408 + /// Make a copy of the given node.
1.1409 + BpGraphCopy& node(TNode& tnode, const Node& snode) {
1.1410 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
1.1411 + NodeRefMap, TNode>(tnode, snode));
1.1412 + return *this;
1.1413 + }
1.1414 +
1.1415 + /// \brief Copies the arc references into the given map.
1.1416 + ///
1.1417 + /// Copies the arc references into the given map.
1.1418 + template <typename ArcRef>
1.1419 + BpGraphCopy& arcRef(ArcRef& map) {
1.1420 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
1.1421 + ArcRefMap, ArcRef>(map));
1.1422 + return *this;
1.1423 + }
1.1424 +
1.1425 + /// \brief Copies the arc cross references into the given map.
1.1426 + ///
1.1427 + /// Copies the arc cross references (reverse references) into
1.1428 + /// the given map.
1.1429 + template <typename ArcCrossRef>
1.1430 + BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
1.1431 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
1.1432 + ArcRefMap, ArcCrossRef>(map));
1.1433 + return *this;
1.1434 + }
1.1435 +
1.1436 + /// \brief Make copy of the given map.
1.1437 + ///
1.1438 + /// Makes copy of the given map for the newly created digraph.
1.1439 + /// The new map's key type is the to digraph's arc type,
1.1440 + /// and the copied map's key type is the from digraph's arc
1.1441 + /// type.
1.1442 + template <typename ToMap, typename FromMap>
1.1443 + BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
1.1444 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
1.1445 + ArcRefMap, ToMap, FromMap>(tmap, map));
1.1446 + return *this;
1.1447 + }
1.1448 +
1.1449 + /// \brief Make a copy of the given arc.
1.1450 + ///
1.1451 + /// Make a copy of the given arc.
1.1452 + BpGraphCopy& arc(TArc& tarc, const Arc& sarc) {
1.1453 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
1.1454 + ArcRefMap, TArc>(tarc, sarc));
1.1455 + return *this;
1.1456 + }
1.1457 +
1.1458 + /// \brief Copies the edge references into the given map.
1.1459 + ///
1.1460 + /// Copies the edge references into the given map.
1.1461 + template <typename EdgeRef>
1.1462 + BpGraphCopy& edgeRef(EdgeRef& map) {
1.1463 + edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
1.1464 + EdgeRefMap, EdgeRef>(map));
1.1465 + return *this;
1.1466 + }
1.1467 +
1.1468 + /// \brief Copies the edge cross references into the given map.
1.1469 + ///
1.1470 + /// Copies the edge cross references (reverse
1.1471 + /// references) into the given map.
1.1472 + template <typename EdgeCrossRef>
1.1473 + BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1.1474 + edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
1.1475 + Edge, EdgeRefMap, EdgeCrossRef>(map));
1.1476 + return *this;
1.1477 + }
1.1478 +
1.1479 + /// \brief Make copy of the given map.
1.1480 + ///
1.1481 + /// Makes copy of the given map for the newly created digraph.
1.1482 + /// The new map's key type is the to digraph's edge type,
1.1483 + /// and the copied map's key type is the from digraph's edge
1.1484 + /// type.
1.1485 + template <typename ToMap, typename FromMap>
1.1486 + BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
1.1487 + edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
1.1488 + EdgeRefMap, ToMap, FromMap>(tmap, map));
1.1489 + return *this;
1.1490 + }
1.1491 +
1.1492 + /// \brief Make a copy of the given edge.
1.1493 + ///
1.1494 + /// Make a copy of the given edge.
1.1495 + BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
1.1496 + edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
1.1497 + EdgeRefMap, TEdge>(tedge, sedge));
1.1498 + return *this;
1.1499 + }
1.1500 +
1.1501 + /// \brief Executes the copies.
1.1502 + ///
1.1503 + /// Executes the copies.
1.1504 + void run() {
1.1505 + RedRefMap redRefMap(from);
1.1506 + BlueRefMap blueRefMap(from);
1.1507 + NodeRefMap nodeRefMap(from, redRefMap, blueRefMap);
1.1508 + EdgeRefMap edgeRefMap(from);
1.1509 + ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
1.1510 + _digraph_utils_bits::BpGraphCopySelector<To>::
1.1511 + copy(to, from, redRefMap, blueRefMap, edgeRefMap);
1.1512 + for (int i = 0; i < int(redMapCopies.size()); ++i) {
1.1513 + redMapCopies[i]->copy(from, redRefMap);
1.1514 + }
1.1515 + for (int i = 0; i < int(blueMapCopies.size()); ++i) {
1.1516 + blueMapCopies[i]->copy(from, blueRefMap);
1.1517 + }
1.1518 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1.1519 + nodeMapCopies[i]->copy(from, nodeRefMap);
1.1520 + }
1.1521 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1.1522 + edgeMapCopies[i]->copy(from, edgeRefMap);
1.1523 + }
1.1524 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
1.1525 + arcMapCopies[i]->copy(from, arcRefMap);
1.1526 + }
1.1527 + }
1.1528 +
1.1529 + private:
1.1530 +
1.1531 + const From& from;
1.1532 + To& to;
1.1533 +
1.1534 + std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* >
1.1535 + redMapCopies;
1.1536 +
1.1537 + std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* >
1.1538 + blueMapCopies;
1.1539 +
1.1540 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
1.1541 + nodeMapCopies;
1.1542 +
1.1543 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1.1544 + arcMapCopies;
1.1545 +
1.1546 + std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1.1547 + edgeMapCopies;
1.1548 +
1.1549 + };
1.1550 +
1.1551 + /// \brief Copy a bipartite digraph to another digraph.
1.1552 + ///
1.1553 + /// Copy a bipartite digraph to another digraph.
1.1554 + /// The usage of the function:
1.1555 + ///
1.1556 + ///\code
1.1557 + /// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run();
1.1558 + ///\endcode
1.1559 + ///
1.1560 + /// After the copy the \c nr map will contain the mapping from the
1.1561 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
1.1562 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
1.1563 + /// to the arcs of the \c from digraph.
1.1564 + ///
1.1565 + /// \see BpGraphCopy
1.1566 + template <typename To, typename From>
1.1567 + BpGraphCopy<To, From>
1.1568 + copyBpGraph(To& to, const From& from) {
1.1569 + return BpGraphCopy<To, From>(to, from);
1.1570 + }
1.1571 +
1.1572 +
1.1573 + /// @}
1.1574 +
1.1575 + /// \addtogroup digraph_maps
1.1576 + /// @{
1.1577 +
1.1578 + /// Provides an immutable and unique id for each item in the digraph.
1.1579 +
1.1580 + /// The IdMap class provides a unique and immutable id for each item of the
1.1581 + /// same type (e.g. node) in the digraph. This id is <ul><li>\b unique:
1.1582 + /// different items (nodes) get different ids <li>\b immutable: the id of an
1.1583 + /// item (node) does not change (even if you delete other nodes). </ul>
1.1584 + /// Through this map you get access (i.e. can read) the inner id values of
1.1585 + /// the items stored in the digraph. This map can be inverted with its member
1.1586 + /// class \c InverseMap.
1.1587 + ///
1.1588 + template <typename _Digraph, typename _Item>
1.1589 + class IdMap {
1.1590 + public:
1.1591 + typedef _Digraph Digraph;
1.1592 + typedef int Value;
1.1593 + typedef _Item Item;
1.1594 + typedef _Item Key;
1.1595 +
1.1596 + /// \brief Constructor.
1.1597 + ///
1.1598 + /// Constructor of the map.
1.1599 + explicit IdMap(const Digraph& _digraph) : digraph(&_digraph) {}
1.1600 +
1.1601 + /// \brief Gives back the \e id of the item.
1.1602 + ///
1.1603 + /// Gives back the immutable and unique \e id of the item.
1.1604 + int operator[](const Item& item) const { return digraph->id(item);}
1.1605 +
1.1606 + /// \brief Gives back the item by its id.
1.1607 + ///
1.1608 + /// Gives back the item by its id.
1.1609 + Item operator()(int id) { return digraph->fromId(id, Item()); }
1.1610 +
1.1611 + private:
1.1612 + const Digraph* digraph;
1.1613 +
1.1614 + public:
1.1615 +
1.1616 + /// \brief The class represents the inverse of its owner (IdMap).
1.1617 + ///
1.1618 + /// The class represents the inverse of its owner (IdMap).
1.1619 + /// \see inverse()
1.1620 + class InverseMap {
1.1621 + public:
1.1622 +
1.1623 + /// \brief Constructor.
1.1624 + ///
1.1625 + /// Constructor for creating an id-to-item map.
1.1626 + explicit InverseMap(const Digraph& _digraph) : digraph(&_digraph) {}
1.1627 +
1.1628 + /// \brief Constructor.
1.1629 + ///
1.1630 + /// Constructor for creating an id-to-item map.
1.1631 + explicit InverseMap(const IdMap& idMap) : digraph(idMap.digraph) {}
1.1632 +
1.1633 + /// \brief Gives back the given item from its id.
1.1634 + ///
1.1635 + /// Gives back the given item from its id.
1.1636 + ///
1.1637 + Item operator[](int id) const { return digraph->fromId(id, Item());}
1.1638 +
1.1639 + private:
1.1640 + const Digraph* digraph;
1.1641 + };
1.1642 +
1.1643 + /// \brief Gives back the inverse of the map.
1.1644 + ///
1.1645 + /// Gives back the inverse of the IdMap.
1.1646 + InverseMap inverse() const { return InverseMap(*digraph);}
1.1647 +
1.1648 + };
1.1649 +
1.1650 +
1.1651 + /// \brief General invertable digraph-map type.
1.1652 +
1.1653 + /// This type provides simple invertable digraph-maps.
1.1654 + /// The InvertableMap wraps an arbitrary ReadWriteMap
1.1655 + /// and if a key is set to a new value then store it
1.1656 + /// in the inverse map.
1.1657 + ///
1.1658 + /// The values of the map can be accessed
1.1659 + /// with stl compatible forward iterator.
1.1660 + ///
1.1661 + /// \param _Digraph The digraph type.
1.1662 + /// \param _Item The item type of the digraph.
1.1663 + /// \param _Value The value type of the map.
1.1664 + ///
1.1665 + /// \see IterableValueMap
1.1666 + template <typename _Digraph, typename _Item, typename _Value>
1.1667 + class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> {
1.1668 + private:
1.1669 +
1.1670 + typedef DefaultMap<_Digraph, _Item, _Value> Map;
1.1671 + typedef _Digraph Digraph;
1.1672 +
1.1673 + typedef std::map<_Value, _Item> Container;
1.1674 + Container invMap;
1.1675 +
1.1676 + public:
1.1677 +
1.1678 + /// The key type of InvertableMap (Node, Arc, Edge).
1.1679 + typedef typename Map::Key Key;
1.1680 + /// The value type of the InvertableMap.
1.1681 + typedef typename Map::Value Value;
1.1682 +
1.1683 +
1.1684 +
1.1685 + /// \brief Constructor.
1.1686 + ///
1.1687 + /// Construct a new InvertableMap for the digraph.
1.1688 + ///
1.1689 + explicit InvertableMap(const Digraph& digraph) : Map(digraph) {}
1.1690 +
1.1691 + /// \brief Forward iterator for values.
1.1692 + ///
1.1693 + /// This iterator is an stl compatible forward
1.1694 + /// iterator on the values of the map. The values can
1.1695 + /// be accessed in the [beginValue, endValue) range.
1.1696 + ///
1.1697 + class ValueIterator
1.1698 + : public std::iterator<std::forward_iterator_tag, Value> {
1.1699 + friend class InvertableMap;
1.1700 + private:
1.1701 + ValueIterator(typename Container::const_iterator _it)
1.1702 + : it(_it) {}
1.1703 + public:
1.1704 +
1.1705 + ValueIterator() {}
1.1706 +
1.1707 + ValueIterator& operator++() { ++it; return *this; }
1.1708 + ValueIterator operator++(int) {
1.1709 + ValueIterator tmp(*this);
1.1710 + operator++();
1.1711 + return tmp;
1.1712 + }
1.1713 +
1.1714 + const Value& operator*() const { return it->first; }
1.1715 + const Value* operator->() const { return &(it->first); }
1.1716 +
1.1717 + bool operator==(ValueIterator jt) const { return it == jt.it; }
1.1718 + bool operator!=(ValueIterator jt) const { return it != jt.it; }
1.1719 +
1.1720 + private:
1.1721 + typename Container::const_iterator it;
1.1722 + };
1.1723 +
1.1724 + /// \brief Returns an iterator to the first value.
1.1725 + ///
1.1726 + /// Returns an stl compatible iterator to the
1.1727 + /// first value of the map. The values of the
1.1728 + /// map can be accessed in the [beginValue, endValue)
1.1729 + /// range.
1.1730 + ValueIterator beginValue() const {
1.1731 + return ValueIterator(invMap.begin());
1.1732 + }
1.1733 +
1.1734 + /// \brief Returns an iterator after the last value.
1.1735 + ///
1.1736 + /// Returns an stl compatible iterator after the
1.1737 + /// last value of the map. The values of the
1.1738 + /// map can be accessed in the [beginValue, endValue)
1.1739 + /// range.
1.1740 + ValueIterator endValue() const {
1.1741 + return ValueIterator(invMap.end());
1.1742 + }
1.1743 +
1.1744 + /// \brief The setter function of the map.
1.1745 + ///
1.1746 + /// Sets the mapped value.
1.1747 + void set(const Key& key, const Value& val) {
1.1748 + Value oldval = Map::operator[](key);
1.1749 + typename Container::iterator it = invMap.find(oldval);
1.1750 + if (it != invMap.end() && it->second == key) {
1.1751 + invMap.erase(it);
1.1752 + }
1.1753 + invMap.insert(make_pair(val, key));
1.1754 + Map::set(key, val);
1.1755 + }
1.1756 +
1.1757 + /// \brief The getter function of the map.
1.1758 + ///
1.1759 + /// It gives back the value associated with the key.
1.1760 + typename MapTraits<Map>::ConstReturnValue
1.1761 + operator[](const Key& key) const {
1.1762 + return Map::operator[](key);
1.1763 + }
1.1764 +
1.1765 + /// \brief Gives back the item by its value.
1.1766 + ///
1.1767 + /// Gives back the item by its value.
1.1768 + Key operator()(const Value& key) const {
1.1769 + typename Container::const_iterator it = invMap.find(key);
1.1770 + return it != invMap.end() ? it->second : INVALID;
1.1771 + }
1.1772 +
1.1773 + protected:
1.1774 +
1.1775 + /// \brief Erase the key from the map.
1.1776 + ///
1.1777 + /// Erase the key to the map. It is called by the
1.1778 + /// \c AlterationNotifier.
1.1779 + virtual void erase(const Key& key) {
1.1780 + Value val = Map::operator[](key);
1.1781 + typename Container::iterator it = invMap.find(val);
1.1782 + if (it != invMap.end() && it->second == key) {
1.1783 + invMap.erase(it);
1.1784 + }
1.1785 + Map::erase(key);
1.1786 + }
1.1787 +
1.1788 + /// \brief Erase more keys from the map.
1.1789 + ///
1.1790 + /// Erase more keys from the map. It is called by the
1.1791 + /// \c AlterationNotifier.
1.1792 + virtual void erase(const std::vector<Key>& keys) {
1.1793 + for (int i = 0; i < int(keys.size()); ++i) {
1.1794 + Value val = Map::operator[](keys[i]);
1.1795 + typename Container::iterator it = invMap.find(val);
1.1796 + if (it != invMap.end() && it->second == keys[i]) {
1.1797 + invMap.erase(it);
1.1798 + }
1.1799 + }
1.1800 + Map::erase(keys);
1.1801 + }
1.1802 +
1.1803 + /// \brief Clear the keys from the map and inverse map.
1.1804 + ///
1.1805 + /// Clear the keys from the map and inverse map. It is called by the
1.1806 + /// \c AlterationNotifier.
1.1807 + virtual void clear() {
1.1808 + invMap.clear();
1.1809 + Map::clear();
1.1810 + }
1.1811 +
1.1812 + public:
1.1813 +
1.1814 + /// \brief The inverse map type.
1.1815 + ///
1.1816 + /// The inverse of this map. The subscript operator of the map
1.1817 + /// gives back always the item what was last assigned to the value.
1.1818 + class InverseMap {
1.1819 + public:
1.1820 + /// \brief Constructor of the InverseMap.
1.1821 + ///
1.1822 + /// Constructor of the InverseMap.
1.1823 + explicit InverseMap(const InvertableMap& _inverted)
1.1824 + : inverted(_inverted) {}
1.1825 +
1.1826 + /// The value type of the InverseMap.
1.1827 + typedef typename InvertableMap::Key Value;
1.1828 + /// The key type of the InverseMap.
1.1829 + typedef typename InvertableMap::Value Key;
1.1830 +
1.1831 + /// \brief Subscript operator.
1.1832 + ///
1.1833 + /// Subscript operator. It gives back always the item
1.1834 + /// what was last assigned to the value.
1.1835 + Value operator[](const Key& key) const {
1.1836 + return inverted(key);
1.1837 + }
1.1838 +
1.1839 + private:
1.1840 + const InvertableMap& inverted;
1.1841 + };
1.1842 +
1.1843 + /// \brief It gives back the just readable inverse map.
1.1844 + ///
1.1845 + /// It gives back the just readable inverse map.
1.1846 + InverseMap inverse() const {
1.1847 + return InverseMap(*this);
1.1848 + }
1.1849 +
1.1850 +
1.1851 +
1.1852 + };
1.1853 +
1.1854 + /// \brief Provides a mutable, continuous and unique descriptor for each
1.1855 + /// item in the digraph.
1.1856 + ///
1.1857 + /// The DescriptorMap class provides a unique and continuous (but mutable)
1.1858 + /// descriptor (id) for each item of the same type (e.g. node) in the
1.1859 + /// digraph. This id is <ul><li>\b unique: different items (nodes) get
1.1860 + /// different ids <li>\b continuous: the range of the ids is the set of
1.1861 + /// integers between 0 and \c n-1, where \c n is the number of the items of
1.1862 + /// this type (e.g. nodes) (so the id of a node can change if you delete an
1.1863 + /// other node, i.e. this id is mutable). </ul> This map can be inverted
1.1864 + /// with its member class \c InverseMap.
1.1865 + ///
1.1866 + /// \param _Digraph The digraph class the \c DescriptorMap belongs to.
1.1867 + /// \param _Item The Item is the Key of the Map. It may be Node, Arc or
1.1868 + /// Edge.
1.1869 + template <typename _Digraph, typename _Item>
1.1870 + class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> {
1.1871 +
1.1872 + typedef _Item Item;
1.1873 + typedef DefaultMap<_Digraph, _Item, int> Map;
1.1874 +
1.1875 + public:
1.1876 + /// The digraph class of DescriptorMap.
1.1877 + typedef _Digraph Digraph;
1.1878 +
1.1879 + /// The key type of DescriptorMap (Node, Arc, Edge).
1.1880 + typedef typename Map::Key Key;
1.1881 + /// The value type of DescriptorMap.
1.1882 + typedef typename Map::Value Value;
1.1883 +
1.1884 + /// \brief Constructor.
1.1885 + ///
1.1886 + /// Constructor for descriptor map.
1.1887 + explicit DescriptorMap(const Digraph& _digraph) : Map(_digraph) {
1.1888 + Item it;
1.1889 + const typename Map::Notifier* nf = Map::notifier();
1.1890 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.1891 + Map::set(it, invMap.size());
1.1892 + invMap.push_back(it);
1.1893 + }
1.1894 + }
1.1895 +
1.1896 + protected:
1.1897 +
1.1898 + /// \brief Add a new key to the map.
1.1899 + ///
1.1900 + /// Add a new key to the map. It is called by the
1.1901 + /// \c AlterationNotifier.
1.1902 + virtual void add(const Item& item) {
1.1903 + Map::add(item);
1.1904 + Map::set(item, invMap.size());
1.1905 + invMap.push_back(item);
1.1906 + }
1.1907 +
1.1908 + /// \brief Add more new keys to the map.
1.1909 + ///
1.1910 + /// Add more new keys to the map. It is called by the
1.1911 + /// \c AlterationNotifier.
1.1912 + virtual void add(const std::vector<Item>& items) {
1.1913 + Map::add(items);
1.1914 + for (int i = 0; i < int(items.size()); ++i) {
1.1915 + Map::set(items[i], invMap.size());
1.1916 + invMap.push_back(items[i]);
1.1917 + }
1.1918 + }
1.1919 +
1.1920 + /// \brief Erase the key from the map.
1.1921 + ///
1.1922 + /// Erase the key from the map. It is called by the
1.1923 + /// \c AlterationNotifier.
1.1924 + virtual void erase(const Item& item) {
1.1925 + Map::set(invMap.back(), Map::operator[](item));
1.1926 + invMap[Map::operator[](item)] = invMap.back();
1.1927 + invMap.pop_back();
1.1928 + Map::erase(item);
1.1929 + }
1.1930 +
1.1931 + /// \brief Erase more keys from the map.
1.1932 + ///
1.1933 + /// Erase more keys from the map. It is called by the
1.1934 + /// \c AlterationNotifier.
1.1935 + virtual void erase(const std::vector<Item>& items) {
1.1936 + for (int i = 0; i < int(items.size()); ++i) {
1.1937 + Map::set(invMap.back(), Map::operator[](items[i]));
1.1938 + invMap[Map::operator[](items[i])] = invMap.back();
1.1939 + invMap.pop_back();
1.1940 + }
1.1941 + Map::erase(items);
1.1942 + }
1.1943 +
1.1944 + /// \brief Build the unique map.
1.1945 + ///
1.1946 + /// Build the unique map. It is called by the
1.1947 + /// \c AlterationNotifier.
1.1948 + virtual void build() {
1.1949 + Map::build();
1.1950 + Item it;
1.1951 + const typename Map::Notifier* nf = Map::notifier();
1.1952 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.1953 + Map::set(it, invMap.size());
1.1954 + invMap.push_back(it);
1.1955 + }
1.1956 + }
1.1957 +
1.1958 + /// \brief Clear the keys from the map.
1.1959 + ///
1.1960 + /// Clear the keys from the map. It is called by the
1.1961 + /// \c AlterationNotifier.
1.1962 + virtual void clear() {
1.1963 + invMap.clear();
1.1964 + Map::clear();
1.1965 + }
1.1966 +
1.1967 + public:
1.1968 +
1.1969 + /// \brief Returns the maximal value plus one.
1.1970 + ///
1.1971 + /// Returns the maximal value plus one in the map.
1.1972 + unsigned int size() const {
1.1973 + return invMap.size();
1.1974 + }
1.1975 +
1.1976 + /// \brief Swaps the position of the two items in the map.
1.1977 + ///
1.1978 + /// Swaps the position of the two items in the map.
1.1979 + void swap(const Item& p, const Item& q) {
1.1980 + int pi = Map::operator[](p);
1.1981 + int qi = Map::operator[](q);
1.1982 + Map::set(p, qi);
1.1983 + invMap[qi] = p;
1.1984 + Map::set(q, pi);
1.1985 + invMap[pi] = q;
1.1986 + }
1.1987 +
1.1988 + /// \brief Gives back the \e descriptor of the item.
1.1989 + ///
1.1990 + /// Gives back the mutable and unique \e descriptor of the map.
1.1991 + int operator[](const Item& item) const {
1.1992 + return Map::operator[](item);
1.1993 + }
1.1994 +
1.1995 + /// \brief Gives back the item by its descriptor.
1.1996 + ///
1.1997 + /// Gives back th item by its descriptor.
1.1998 + Item operator()(int id) const {
1.1999 + return invMap[id];
1.2000 + }
1.2001 +
1.2002 + private:
1.2003 +
1.2004 + typedef std::vector<Item> Container;
1.2005 + Container invMap;
1.2006 +
1.2007 + public:
1.2008 + /// \brief The inverse map type of DescriptorMap.
1.2009 + ///
1.2010 + /// The inverse map type of DescriptorMap.
1.2011 + class InverseMap {
1.2012 + public:
1.2013 + /// \brief Constructor of the InverseMap.
1.2014 + ///
1.2015 + /// Constructor of the InverseMap.
1.2016 + explicit InverseMap(const DescriptorMap& _inverted)
1.2017 + : inverted(_inverted) {}
1.2018 +
1.2019 +
1.2020 + /// The value type of the InverseMap.
1.2021 + typedef typename DescriptorMap::Key Value;
1.2022 + /// The key type of the InverseMap.
1.2023 + typedef typename DescriptorMap::Value Key;
1.2024 +
1.2025 + /// \brief Subscript operator.
1.2026 + ///
1.2027 + /// Subscript operator. It gives back the item
1.2028 + /// that the descriptor belongs to currently.
1.2029 + Value operator[](const Key& key) const {
1.2030 + return inverted(key);
1.2031 + }
1.2032 +
1.2033 + /// \brief Size of the map.
1.2034 + ///
1.2035 + /// Returns the size of the map.
1.2036 + unsigned int size() const {
1.2037 + return inverted.size();
1.2038 + }
1.2039 +
1.2040 + private:
1.2041 + const DescriptorMap& inverted;
1.2042 + };
1.2043 +
1.2044 + /// \brief Gives back the inverse of the map.
1.2045 + ///
1.2046 + /// Gives back the inverse of the map.
1.2047 + const InverseMap inverse() const {
1.2048 + return InverseMap(*this);
1.2049 + }
1.2050 + };
1.2051 +
1.2052 + /// \brief Returns the source of the given arc.
1.2053 + ///
1.2054 + /// The SourceMap gives back the source Node of the given arc.
1.2055 + /// \see TargetMap
1.2056 + /// \author Balazs Dezso
1.2057 + template <typename Digraph>
1.2058 + class SourceMap {
1.2059 + public:
1.2060 +
1.2061 + typedef typename Digraph::Node Value;
1.2062 + typedef typename Digraph::Arc Key;
1.2063 +
1.2064 + /// \brief Constructor
1.2065 + ///
1.2066 + /// Constructor
1.2067 + /// \param _digraph The digraph that the map belongs to.
1.2068 + explicit SourceMap(const Digraph& _digraph) : digraph(_digraph) {}
1.2069 +
1.2070 + /// \brief The subscript operator.
1.2071 + ///
1.2072 + /// The subscript operator.
1.2073 + /// \param arc The arc
1.2074 + /// \return The source of the arc
1.2075 + Value operator[](const Key& arc) const {
1.2076 + return digraph.source(arc);
1.2077 + }
1.2078 +
1.2079 + private:
1.2080 + const Digraph& digraph;
1.2081 + };
1.2082 +
1.2083 + /// \brief Returns a \ref SourceMap class.
1.2084 + ///
1.2085 + /// This function just returns an \ref SourceMap class.
1.2086 + /// \relates SourceMap
1.2087 + template <typename Digraph>
1.2088 + inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
1.2089 + return SourceMap<Digraph>(digraph);
1.2090 + }
1.2091 +
1.2092 + /// \brief Returns the target of the given arc.
1.2093 + ///
1.2094 + /// The TargetMap gives back the target Node of the given arc.
1.2095 + /// \see SourceMap
1.2096 + /// \author Balazs Dezso
1.2097 + template <typename Digraph>
1.2098 + class TargetMap {
1.2099 + public:
1.2100 +
1.2101 + typedef typename Digraph::Node Value;
1.2102 + typedef typename Digraph::Arc Key;
1.2103 +
1.2104 + /// \brief Constructor
1.2105 + ///
1.2106 + /// Constructor
1.2107 + /// \param _digraph The digraph that the map belongs to.
1.2108 + explicit TargetMap(const Digraph& _digraph) : digraph(_digraph) {}
1.2109 +
1.2110 + /// \brief The subscript operator.
1.2111 + ///
1.2112 + /// The subscript operator.
1.2113 + /// \param e The arc
1.2114 + /// \return The target of the arc
1.2115 + Value operator[](const Key& e) const {
1.2116 + return digraph.target(e);
1.2117 + }
1.2118 +
1.2119 + private:
1.2120 + const Digraph& digraph;
1.2121 + };
1.2122 +
1.2123 + /// \brief Returns a \ref TargetMap class.
1.2124 + ///
1.2125 + /// This function just returns a \ref TargetMap class.
1.2126 + /// \relates TargetMap
1.2127 + template <typename Digraph>
1.2128 + inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
1.2129 + return TargetMap<Digraph>(digraph);
1.2130 + }
1.2131 +
1.2132 + /// \brief Returns the "forward" directed arc view of an edge.
1.2133 + ///
1.2134 + /// Returns the "forward" directed arc view of an edge.
1.2135 + /// \see BackwardMap
1.2136 + /// \author Balazs Dezso
1.2137 + template <typename Digraph>
1.2138 + class ForwardMap {
1.2139 + public:
1.2140 +
1.2141 + typedef typename Digraph::Arc Value;
1.2142 + typedef typename Digraph::Edge Key;
1.2143 +
1.2144 + /// \brief Constructor
1.2145 + ///
1.2146 + /// Constructor
1.2147 + /// \param _digraph The digraph that the map belongs to.
1.2148 + explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {}
1.2149 +
1.2150 + /// \brief The subscript operator.
1.2151 + ///
1.2152 + /// The subscript operator.
1.2153 + /// \param key An edge
1.2154 + /// \return The "forward" directed arc view of edge
1.2155 + Value operator[](const Key& key) const {
1.2156 + return digraph.direct(key, true);
1.2157 + }
1.2158 +
1.2159 + private:
1.2160 + const Digraph& digraph;
1.2161 + };
1.2162 +
1.2163 + /// \brief Returns a \ref ForwardMap class.
1.2164 + ///
1.2165 + /// This function just returns an \ref ForwardMap class.
1.2166 + /// \relates ForwardMap
1.2167 + template <typename Digraph>
1.2168 + inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) {
1.2169 + return ForwardMap<Digraph>(digraph);
1.2170 + }
1.2171 +
1.2172 + /// \brief Returns the "backward" directed arc view of an edge.
1.2173 + ///
1.2174 + /// Returns the "backward" directed arc view of an edge.
1.2175 + /// \see ForwardMap
1.2176 + /// \author Balazs Dezso
1.2177 + template <typename Digraph>
1.2178 + class BackwardMap {
1.2179 + public:
1.2180 +
1.2181 + typedef typename Digraph::Arc Value;
1.2182 + typedef typename Digraph::Edge Key;
1.2183 +
1.2184 + /// \brief Constructor
1.2185 + ///
1.2186 + /// Constructor
1.2187 + /// \param _digraph The digraph that the map belongs to.
1.2188 + explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {}
1.2189 +
1.2190 + /// \brief The subscript operator.
1.2191 + ///
1.2192 + /// The subscript operator.
1.2193 + /// \param key An edge
1.2194 + /// \return The "backward" directed arc view of edge
1.2195 + Value operator[](const Key& key) const {
1.2196 + return digraph.direct(key, false);
1.2197 + }
1.2198 +
1.2199 + private:
1.2200 + const Digraph& digraph;
1.2201 + };
1.2202 +
1.2203 + /// \brief Returns a \ref BackwardMap class
1.2204 +
1.2205 + /// This function just returns a \ref BackwardMap class.
1.2206 + /// \relates BackwardMap
1.2207 + template <typename Digraph>
1.2208 + inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) {
1.2209 + return BackwardMap<Digraph>(digraph);
1.2210 + }
1.2211 +
1.2212 + /// \brief Potential difference map
1.2213 + ///
1.2214 + /// If there is an potential map on the nodes then we
1.2215 + /// can get an arc map as we get the substraction of the
1.2216 + /// values of the target and source.
1.2217 + template <typename Digraph, typename NodeMap>
1.2218 + class PotentialDifferenceMap {
1.2219 + public:
1.2220 + typedef typename Digraph::Arc Key;
1.2221 + typedef typename NodeMap::Value Value;
1.2222 +
1.2223 + /// \brief Constructor
1.2224 + ///
1.2225 + /// Contructor of the map
1.2226 + explicit PotentialDifferenceMap(const Digraph& _digraph,
1.2227 + const NodeMap& _potential)
1.2228 + : digraph(_digraph), potential(_potential) {}
1.2229 +
1.2230 + /// \brief Const subscription operator
1.2231 + ///
1.2232 + /// Const subscription operator
1.2233 + Value operator[](const Key& arc) const {
1.2234 + return potential[digraph.target(arc)] - potential[digraph.source(arc)];
1.2235 + }
1.2236 +
1.2237 + private:
1.2238 + const Digraph& digraph;
1.2239 + const NodeMap& potential;
1.2240 + };
1.2241 +
1.2242 + /// \brief Returns a PotentialDifferenceMap.
1.2243 + ///
1.2244 + /// This function just returns a PotentialDifferenceMap.
1.2245 + /// \relates PotentialDifferenceMap
1.2246 + template <typename Digraph, typename NodeMap>
1.2247 + PotentialDifferenceMap<Digraph, NodeMap>
1.2248 + potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
1.2249 + return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
1.2250 + }
1.2251 +
1.2252 + /// \brief Map of the node in-degrees.
1.2253 + ///
1.2254 + /// This map returns the in-degree of a node. Once it is constructed,
1.2255 + /// the degrees are stored in a standard NodeMap, so each query is done
1.2256 + /// in constant time. On the other hand, the values are updated automatically
1.2257 + /// whenever the digraph changes.
1.2258 + ///
1.2259 + /// \warning Besides addNode() and addArc(), a digraph structure may provide
1.2260 + /// alternative ways to modify the digraph. The correct behavior of InDegMap
1.2261 + /// is not guarantied if these additional features are used. For example
1.2262 + /// the functions \ref ListDigraph::changeSource() "changeSource()",
1.2263 + /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.2264 + /// \ref ListDigraph::reverseArc() "reverseArc()"
1.2265 + /// of \ref ListDigraph will \e not update the degree values correctly.
1.2266 + ///
1.2267 + /// \sa OutDegMap
1.2268 +
1.2269 + template <typename _Digraph>
1.2270 + class InDegMap
1.2271 + : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2272 + ::ItemNotifier::ObserverBase {
1.2273 +
1.2274 + public:
1.2275 +
1.2276 + typedef _Digraph Digraph;
1.2277 + typedef int Value;
1.2278 + typedef typename Digraph::Node Key;
1.2279 +
1.2280 + typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2281 + ::ItemNotifier::ObserverBase Parent;
1.2282 +
1.2283 + private:
1.2284 +
1.2285 + class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
1.2286 + public:
1.2287 +
1.2288 + typedef DefaultMap<_Digraph, Key, int> Parent;
1.2289 + typedef typename Parent::Digraph Digraph;
1.2290 +
1.2291 + AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
1.2292 +
1.2293 + virtual void add(const Key& key) {
1.2294 + Parent::add(key);
1.2295 + Parent::set(key, 0);
1.2296 + }
1.2297 +
1.2298 + virtual void add(const std::vector<Key>& keys) {
1.2299 + Parent::add(keys);
1.2300 + for (int i = 0; i < int(keys.size()); ++i) {
1.2301 + Parent::set(keys[i], 0);
1.2302 + }
1.2303 + }
1.2304 +
1.2305 + virtual void build() {
1.2306 + Parent::build();
1.2307 + Key it;
1.2308 + typename Parent::Notifier* nf = Parent::notifier();
1.2309 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.2310 + Parent::set(it, 0);
1.2311 + }
1.2312 + }
1.2313 + };
1.2314 +
1.2315 + public:
1.2316 +
1.2317 + /// \brief Constructor.
1.2318 + ///
1.2319 + /// Constructor for creating in-degree map.
1.2320 + explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
1.2321 + Parent::attach(digraph.notifier(typename _Digraph::Arc()));
1.2322 +
1.2323 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2324 + deg[it] = countInArcs(digraph, it);
1.2325 + }
1.2326 + }
1.2327 +
1.2328 + /// Gives back the in-degree of a Node.
1.2329 + int operator[](const Key& key) const {
1.2330 + return deg[key];
1.2331 + }
1.2332 +
1.2333 + protected:
1.2334 +
1.2335 + typedef typename Digraph::Arc Arc;
1.2336 +
1.2337 + virtual void add(const Arc& arc) {
1.2338 + ++deg[digraph.target(arc)];
1.2339 + }
1.2340 +
1.2341 + virtual void add(const std::vector<Arc>& arcs) {
1.2342 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2343 + ++deg[digraph.target(arcs[i])];
1.2344 + }
1.2345 + }
1.2346 +
1.2347 + virtual void erase(const Arc& arc) {
1.2348 + --deg[digraph.target(arc)];
1.2349 + }
1.2350 +
1.2351 + virtual void erase(const std::vector<Arc>& arcs) {
1.2352 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2353 + --deg[digraph.target(arcs[i])];
1.2354 + }
1.2355 + }
1.2356 +
1.2357 + virtual void build() {
1.2358 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2359 + deg[it] = countInArcs(digraph, it);
1.2360 + }
1.2361 + }
1.2362 +
1.2363 + virtual void clear() {
1.2364 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2365 + deg[it] = 0;
1.2366 + }
1.2367 + }
1.2368 + private:
1.2369 +
1.2370 + const _Digraph& digraph;
1.2371 + AutoNodeMap deg;
1.2372 + };
1.2373 +
1.2374 + /// \brief Map of the node out-degrees.
1.2375 + ///
1.2376 + /// This map returns the out-degree of a node. Once it is constructed,
1.2377 + /// the degrees are stored in a standard NodeMap, so each query is done
1.2378 + /// in constant time. On the other hand, the values are updated automatically
1.2379 + /// whenever the digraph changes.
1.2380 + ///
1.2381 + /// \warning Besides addNode() and addArc(), a digraph structure may provide
1.2382 + /// alternative ways to modify the digraph. The correct behavior of OutDegMap
1.2383 + /// is not guarantied if these additional features are used. For example
1.2384 + /// the functions \ref ListDigraph::changeSource() "changeSource()",
1.2385 + /// \ref ListDigraph::changeTarget() "changeTarget()" and
1.2386 + /// \ref ListDigraph::reverseArc() "reverseArc()"
1.2387 + /// of \ref ListDigraph will \e not update the degree values correctly.
1.2388 + ///
1.2389 + /// \sa InDegMap
1.2390 +
1.2391 + template <typename _Digraph>
1.2392 + class OutDegMap
1.2393 + : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2394 + ::ItemNotifier::ObserverBase {
1.2395 +
1.2396 + public:
1.2397 +
1.2398 + typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
1.2399 + ::ItemNotifier::ObserverBase Parent;
1.2400 +
1.2401 + typedef _Digraph Digraph;
1.2402 + typedef int Value;
1.2403 + typedef typename Digraph::Node Key;
1.2404 +
1.2405 + private:
1.2406 +
1.2407 + class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
1.2408 + public:
1.2409 +
1.2410 + typedef DefaultMap<_Digraph, Key, int> Parent;
1.2411 + typedef typename Parent::Digraph Digraph;
1.2412 +
1.2413 + AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
1.2414 +
1.2415 + virtual void add(const Key& key) {
1.2416 + Parent::add(key);
1.2417 + Parent::set(key, 0);
1.2418 + }
1.2419 + virtual void add(const std::vector<Key>& keys) {
1.2420 + Parent::add(keys);
1.2421 + for (int i = 0; i < int(keys.size()); ++i) {
1.2422 + Parent::set(keys[i], 0);
1.2423 + }
1.2424 + }
1.2425 + virtual void build() {
1.2426 + Parent::build();
1.2427 + Key it;
1.2428 + typename Parent::Notifier* nf = Parent::notifier();
1.2429 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.2430 + Parent::set(it, 0);
1.2431 + }
1.2432 + }
1.2433 + };
1.2434 +
1.2435 + public:
1.2436 +
1.2437 + /// \brief Constructor.
1.2438 + ///
1.2439 + /// Constructor for creating out-degree map.
1.2440 + explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
1.2441 + Parent::attach(digraph.notifier(typename _Digraph::Arc()));
1.2442 +
1.2443 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2444 + deg[it] = countOutArcs(digraph, it);
1.2445 + }
1.2446 + }
1.2447 +
1.2448 + /// Gives back the out-degree of a Node.
1.2449 + int operator[](const Key& key) const {
1.2450 + return deg[key];
1.2451 + }
1.2452 +
1.2453 + protected:
1.2454 +
1.2455 + typedef typename Digraph::Arc Arc;
1.2456 +
1.2457 + virtual void add(const Arc& arc) {
1.2458 + ++deg[digraph.source(arc)];
1.2459 + }
1.2460 +
1.2461 + virtual void add(const std::vector<Arc>& arcs) {
1.2462 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2463 + ++deg[digraph.source(arcs[i])];
1.2464 + }
1.2465 + }
1.2466 +
1.2467 + virtual void erase(const Arc& arc) {
1.2468 + --deg[digraph.source(arc)];
1.2469 + }
1.2470 +
1.2471 + virtual void erase(const std::vector<Arc>& arcs) {
1.2472 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2473 + --deg[digraph.source(arcs[i])];
1.2474 + }
1.2475 + }
1.2476 +
1.2477 + virtual void build() {
1.2478 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2479 + deg[it] = countOutArcs(digraph, it);
1.2480 + }
1.2481 + }
1.2482 +
1.2483 + virtual void clear() {
1.2484 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
1.2485 + deg[it] = 0;
1.2486 + }
1.2487 + }
1.2488 + private:
1.2489 +
1.2490 + const _Digraph& digraph;
1.2491 + AutoNodeMap deg;
1.2492 + };
1.2493 +
1.2494 +
1.2495 + ///Dynamic arc look up between given endpoints.
1.2496 +
1.2497 + ///\ingroup gutils
1.2498 + ///Using this class, you can find an arc in a digraph from a given
1.2499 + ///source to a given target in amortized time <em>O(log d)</em>,
1.2500 + ///where <em>d</em> is the out-degree of the source node.
1.2501 + ///
1.2502 + ///It is possible to find \e all parallel arcs between two nodes with
1.2503 + ///the \c findFirst() and \c findNext() members.
1.2504 + ///
1.2505 + ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
1.2506 + ///digraph do not changed so frequently.
1.2507 + ///
1.2508 + ///This class uses a self-adjusting binary search tree, Sleator's
1.2509 + ///and Tarjan's Splay tree for guarantee the logarithmic amortized
1.2510 + ///time bound for arc lookups. This class also guarantees the
1.2511 + ///optimal time bound in a constant factor for any distribution of
1.2512 + ///queries.
1.2513 + ///
1.2514 + ///\param G The type of the underlying digraph.
1.2515 + ///
1.2516 + ///\sa ArcLookUp
1.2517 + ///\sa AllArcLookUp
1.2518 + template<class G>
1.2519 + class DynArcLookUp
1.2520 + : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
1.2521 + {
1.2522 + public:
1.2523 + typedef typename ItemSetTraits<G, typename G::Arc>
1.2524 + ::ItemNotifier::ObserverBase Parent;
1.2525 +
1.2526 + GRAPH_TYPEDEFS(typename G);
1.2527 + typedef G Digraph;
1.2528 +
1.2529 + protected:
1.2530 +
1.2531 + class AutoNodeMap : public DefaultMap<G, Node, Arc> {
1.2532 + public:
1.2533 +
1.2534 + typedef DefaultMap<G, Node, Arc> Parent;
1.2535 +
1.2536 + AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1.2537 +
1.2538 + virtual void add(const Node& node) {
1.2539 + Parent::add(node);
1.2540 + Parent::set(node, INVALID);
1.2541 + }
1.2542 +
1.2543 + virtual void add(const std::vector<Node>& nodes) {
1.2544 + Parent::add(nodes);
1.2545 + for (int i = 0; i < int(nodes.size()); ++i) {
1.2546 + Parent::set(nodes[i], INVALID);
1.2547 + }
1.2548 + }
1.2549 +
1.2550 + virtual void build() {
1.2551 + Parent::build();
1.2552 + Node it;
1.2553 + typename Parent::Notifier* nf = Parent::notifier();
1.2554 + for (nf->first(it); it != INVALID; nf->next(it)) {
1.2555 + Parent::set(it, INVALID);
1.2556 + }
1.2557 + }
1.2558 + };
1.2559 +
1.2560 + const Digraph &_g;
1.2561 + AutoNodeMap _head;
1.2562 + typename Digraph::template ArcMap<Arc> _parent;
1.2563 + typename Digraph::template ArcMap<Arc> _left;
1.2564 + typename Digraph::template ArcMap<Arc> _right;
1.2565 +
1.2566 + class ArcLess {
1.2567 + const Digraph &g;
1.2568 + public:
1.2569 + ArcLess(const Digraph &_g) : g(_g) {}
1.2570 + bool operator()(Arc a,Arc b) const
1.2571 + {
1.2572 + return g.target(a)<g.target(b);
1.2573 + }
1.2574 + };
1.2575 +
1.2576 + public:
1.2577 +
1.2578 + ///Constructor
1.2579 +
1.2580 + ///Constructor.
1.2581 + ///
1.2582 + ///It builds up the search database.
1.2583 + DynArcLookUp(const Digraph &g)
1.2584 + : _g(g),_head(g),_parent(g),_left(g),_right(g)
1.2585 + {
1.2586 + Parent::attach(_g.notifier(typename Digraph::Arc()));
1.2587 + refresh();
1.2588 + }
1.2589 +
1.2590 + protected:
1.2591 +
1.2592 + virtual void add(const Arc& arc) {
1.2593 + insert(arc);
1.2594 + }
1.2595 +
1.2596 + virtual void add(const std::vector<Arc>& arcs) {
1.2597 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2598 + insert(arcs[i]);
1.2599 + }
1.2600 + }
1.2601 +
1.2602 + virtual void erase(const Arc& arc) {
1.2603 + remove(arc);
1.2604 + }
1.2605 +
1.2606 + virtual void erase(const std::vector<Arc>& arcs) {
1.2607 + for (int i = 0; i < int(arcs.size()); ++i) {
1.2608 + remove(arcs[i]);
1.2609 + }
1.2610 + }
1.2611 +
1.2612 + virtual void build() {
1.2613 + refresh();
1.2614 + }
1.2615 +
1.2616 + virtual void clear() {
1.2617 + for(NodeIt n(_g);n!=INVALID;++n) {
1.2618 + _head.set(n, INVALID);
1.2619 + }
1.2620 + }
1.2621 +
1.2622 + void insert(Arc arc) {
1.2623 + Node s = _g.source(arc);
1.2624 + Node t = _g.target(arc);
1.2625 + _left.set(arc, INVALID);
1.2626 + _right.set(arc, INVALID);
1.2627 +
1.2628 + Arc e = _head[s];
1.2629 + if (e == INVALID) {
1.2630 + _head.set(s, arc);
1.2631 + _parent.set(arc, INVALID);
1.2632 + return;
1.2633 + }
1.2634 + while (true) {
1.2635 + if (t < _g.target(e)) {
1.2636 + if (_left[e] == INVALID) {
1.2637 + _left.set(e, arc);
1.2638 + _parent.set(arc, e);
1.2639 + splay(arc);
1.2640 + return;
1.2641 + } else {
1.2642 + e = _left[e];
1.2643 + }
1.2644 + } else {
1.2645 + if (_right[e] == INVALID) {
1.2646 + _right.set(e, arc);
1.2647 + _parent.set(arc, e);
1.2648 + splay(arc);
1.2649 + return;
1.2650 + } else {
1.2651 + e = _right[e];
1.2652 + }
1.2653 + }
1.2654 + }
1.2655 + }
1.2656 +
1.2657 + void remove(Arc arc) {
1.2658 + if (_left[arc] == INVALID) {
1.2659 + if (_right[arc] != INVALID) {
1.2660 + _parent.set(_right[arc], _parent[arc]);
1.2661 + }
1.2662 + if (_parent[arc] != INVALID) {
1.2663 + if (_left[_parent[arc]] == arc) {
1.2664 + _left.set(_parent[arc], _right[arc]);
1.2665 + } else {
1.2666 + _right.set(_parent[arc], _right[arc]);
1.2667 + }
1.2668 + } else {
1.2669 + _head.set(_g.source(arc), _right[arc]);
1.2670 + }
1.2671 + } else if (_right[arc] == INVALID) {
1.2672 + _parent.set(_left[arc], _parent[arc]);
1.2673 + if (_parent[arc] != INVALID) {
1.2674 + if (_left[_parent[arc]] == arc) {
1.2675 + _left.set(_parent[arc], _left[arc]);
1.2676 + } else {
1.2677 + _right.set(_parent[arc], _left[arc]);
1.2678 + }
1.2679 + } else {
1.2680 + _head.set(_g.source(arc), _left[arc]);
1.2681 + }
1.2682 + } else {
1.2683 + Arc e = _left[arc];
1.2684 + if (_right[e] != INVALID) {
1.2685 + e = _right[e];
1.2686 + while (_right[e] != INVALID) {
1.2687 + e = _right[e];
1.2688 + }
1.2689 + Arc s = _parent[e];
1.2690 + _right.set(_parent[e], _left[e]);
1.2691 + if (_left[e] != INVALID) {
1.2692 + _parent.set(_left[e], _parent[e]);
1.2693 + }
1.2694 +
1.2695 + _left.set(e, _left[arc]);
1.2696 + _parent.set(_left[arc], e);
1.2697 + _right.set(e, _right[arc]);
1.2698 + _parent.set(_right[arc], e);
1.2699 +
1.2700 + _parent.set(e, _parent[arc]);
1.2701 + if (_parent[arc] != INVALID) {
1.2702 + if (_left[_parent[arc]] == arc) {
1.2703 + _left.set(_parent[arc], e);
1.2704 + } else {
1.2705 + _right.set(_parent[arc], e);
1.2706 + }
1.2707 + }
1.2708 + splay(s);
1.2709 + } else {
1.2710 + _right.set(e, _right[arc]);
1.2711 + _parent.set(_right[arc], e);
1.2712 +
1.2713 + if (_parent[arc] != INVALID) {
1.2714 + if (_left[_parent[arc]] == arc) {
1.2715 + _left.set(_parent[arc], e);
1.2716 + } else {
1.2717 + _right.set(_parent[arc], e);
1.2718 + }
1.2719 + } else {
1.2720 + _head.set(_g.source(arc), e);
1.2721 + }
1.2722 + }
1.2723 + }
1.2724 + }
1.2725 +
1.2726 + Arc refreshRec(std::vector<Arc> &v,int a,int b)
1.2727 + {
1.2728 + int m=(a+b)/2;
1.2729 + Arc me=v[m];
1.2730 + if (a < m) {
1.2731 + Arc left = refreshRec(v,a,m-1);
1.2732 + _left.set(me, left);
1.2733 + _parent.set(left, me);
1.2734 + } else {
1.2735 + _left.set(me, INVALID);
1.2736 + }
1.2737 + if (m < b) {
1.2738 + Arc right = refreshRec(v,m+1,b);
1.2739 + _right.set(me, right);
1.2740 + _parent.set(right, me);
1.2741 + } else {
1.2742 + _right.set(me, INVALID);
1.2743 + }
1.2744 + return me;
1.2745 + }
1.2746 +
1.2747 + void refresh() {
1.2748 + for(NodeIt n(_g);n!=INVALID;++n) {
1.2749 + std::vector<Arc> v;
1.2750 + for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1.2751 + if(v.size()) {
1.2752 + std::sort(v.begin(),v.end(),ArcLess(_g));
1.2753 + Arc head = refreshRec(v,0,v.size()-1);
1.2754 + _head.set(n, head);
1.2755 + _parent.set(head, INVALID);
1.2756 + }
1.2757 + else _head.set(n, INVALID);
1.2758 + }
1.2759 + }
1.2760 +
1.2761 + void zig(Arc v) {
1.2762 + Arc w = _parent[v];
1.2763 + _parent.set(v, _parent[w]);
1.2764 + _parent.set(w, v);
1.2765 + _left.set(w, _right[v]);
1.2766 + _right.set(v, w);
1.2767 + if (_parent[v] != INVALID) {
1.2768 + if (_right[_parent[v]] == w) {
1.2769 + _right.set(_parent[v], v);
1.2770 + } else {
1.2771 + _left.set(_parent[v], v);
1.2772 + }
1.2773 + }
1.2774 + if (_left[w] != INVALID){
1.2775 + _parent.set(_left[w], w);
1.2776 + }
1.2777 + }
1.2778 +
1.2779 + void zag(Arc v) {
1.2780 + Arc w = _parent[v];
1.2781 + _parent.set(v, _parent[w]);
1.2782 + _parent.set(w, v);
1.2783 + _right.set(w, _left[v]);
1.2784 + _left.set(v, w);
1.2785 + if (_parent[v] != INVALID){
1.2786 + if (_left[_parent[v]] == w) {
1.2787 + _left.set(_parent[v], v);
1.2788 + } else {
1.2789 + _right.set(_parent[v], v);
1.2790 + }
1.2791 + }
1.2792 + if (_right[w] != INVALID){
1.2793 + _parent.set(_right[w], w);
1.2794 + }
1.2795 + }
1.2796 +
1.2797 + void splay(Arc v) {
1.2798 + while (_parent[v] != INVALID) {
1.2799 + if (v == _left[_parent[v]]) {
1.2800 + if (_parent[_parent[v]] == INVALID) {
1.2801 + zig(v);
1.2802 + } else {
1.2803 + if (_parent[v] == _left[_parent[_parent[v]]]) {
1.2804 + zig(_parent[v]);
1.2805 + zig(v);
1.2806 + } else {
1.2807 + zig(v);
1.2808 + zag(v);
1.2809 + }
1.2810 + }
1.2811 + } else {
1.2812 + if (_parent[_parent[v]] == INVALID) {
1.2813 + zag(v);
1.2814 + } else {
1.2815 + if (_parent[v] == _left[_parent[_parent[v]]]) {
1.2816 + zag(v);
1.2817 + zig(v);
1.2818 + } else {
1.2819 + zag(_parent[v]);
1.2820 + zag(v);
1.2821 + }
1.2822 + }
1.2823 + }
1.2824 + }
1.2825 + _head[_g.source(v)] = v;
1.2826 + }
1.2827 +
1.2828 +
1.2829 + public:
1.2830 +
1.2831 + ///Find an arc between two nodes.
1.2832 +
1.2833 + ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
1.2834 + /// <em>d</em> is the number of outgoing arcs of \c s.
1.2835 + ///\param s The source node
1.2836 + ///\param t The target node
1.2837 + ///\return An arc from \c s to \c t if there exists,
1.2838 + ///\ref INVALID otherwise.
1.2839 + Arc operator()(Node s, Node t) const
1.2840 + {
1.2841 + Arc e = _head[s];
1.2842 + while (true) {
1.2843 + if (_g.target(e) == t) {
1.2844 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2845 + return e;
1.2846 + } else if (t < _g.target(e)) {
1.2847 + if (_left[e] == INVALID) {
1.2848 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2849 + return INVALID;
1.2850 + } else {
1.2851 + e = _left[e];
1.2852 + }
1.2853 + } else {
1.2854 + if (_right[e] == INVALID) {
1.2855 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2856 + return INVALID;
1.2857 + } else {
1.2858 + e = _right[e];
1.2859 + }
1.2860 + }
1.2861 + }
1.2862 + }
1.2863 +
1.2864 + ///Find the first arc between two nodes.
1.2865 +
1.2866 + ///Find the first arc between two nodes in time
1.2867 + /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
1.2868 + /// outgoing arcs of \c s.
1.2869 + ///\param s The source node
1.2870 + ///\param t The target node
1.2871 + ///\return An arc from \c s to \c t if there exists, \ref INVALID
1.2872 + /// otherwise.
1.2873 + Arc findFirst(Node s, Node t) const
1.2874 + {
1.2875 + Arc e = _head[s];
1.2876 + Arc r = INVALID;
1.2877 + while (true) {
1.2878 + if (_g.target(e) < t) {
1.2879 + if (_right[e] == INVALID) {
1.2880 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2881 + return r;
1.2882 + } else {
1.2883 + e = _right[e];
1.2884 + }
1.2885 + } else {
1.2886 + if (_g.target(e) == t) {
1.2887 + r = e;
1.2888 + }
1.2889 + if (_left[e] == INVALID) {
1.2890 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2891 + return r;
1.2892 + } else {
1.2893 + e = _left[e];
1.2894 + }
1.2895 + }
1.2896 + }
1.2897 + }
1.2898 +
1.2899 + ///Find the next arc between two nodes.
1.2900 +
1.2901 + ///Find the next arc between two nodes in time
1.2902 + /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
1.2903 + /// outgoing arcs of \c s.
1.2904 + ///\param s The source node
1.2905 + ///\param t The target node
1.2906 + ///\return An arc from \c s to \c t if there exists, \ref INVALID
1.2907 + /// otherwise.
1.2908 +
1.2909 + ///\note If \c e is not the result of the previous \c findFirst()
1.2910 + ///operation then the amorized time bound can not be guaranteed.
1.2911 +#ifdef DOXYGEN
1.2912 + Arc findNext(Node s, Node t, Arc e) const
1.2913 +#else
1.2914 + Arc findNext(Node, Node t, Arc e) const
1.2915 +#endif
1.2916 + {
1.2917 + if (_right[e] != INVALID) {
1.2918 + e = _right[e];
1.2919 + while (_left[e] != INVALID) {
1.2920 + e = _left[e];
1.2921 + }
1.2922 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2923 + } else {
1.2924 + while (_parent[e] != INVALID && _right[_parent[e]] == e) {
1.2925 + e = _parent[e];
1.2926 + }
1.2927 + if (_parent[e] == INVALID) {
1.2928 + return INVALID;
1.2929 + } else {
1.2930 + e = _parent[e];
1.2931 + const_cast<DynArcLookUp&>(*this).splay(e);
1.2932 + }
1.2933 + }
1.2934 + if (_g.target(e) == t) return e;
1.2935 + else return INVALID;
1.2936 + }
1.2937 +
1.2938 + };
1.2939 +
1.2940 + ///Fast arc look up between given endpoints.
1.2941 +
1.2942 + ///\ingroup gutils
1.2943 + ///Using this class, you can find an arc in a digraph from a given
1.2944 + ///source to a given target in time <em>O(log d)</em>,
1.2945 + ///where <em>d</em> is the out-degree of the source node.
1.2946 + ///
1.2947 + ///It is not possible to find \e all parallel arcs between two nodes.
1.2948 + ///Use \ref AllArcLookUp for this purpose.
1.2949 + ///
1.2950 + ///\warning This class is static, so you should refresh() (or at least
1.2951 + ///refresh(Node)) this data structure
1.2952 + ///whenever the digraph changes. This is a time consuming (superlinearly
1.2953 + ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
1.2954 + ///
1.2955 + ///\param G The type of the underlying digraph.
1.2956 + ///
1.2957 + ///\sa DynArcLookUp
1.2958 + ///\sa AllArcLookUp
1.2959 + template<class G>
1.2960 + class ArcLookUp
1.2961 + {
1.2962 + public:
1.2963 + GRAPH_TYPEDEFS(typename G);
1.2964 + typedef G Digraph;
1.2965 +
1.2966 + protected:
1.2967 + const Digraph &_g;
1.2968 + typename Digraph::template NodeMap<Arc> _head;
1.2969 + typename Digraph::template ArcMap<Arc> _left;
1.2970 + typename Digraph::template ArcMap<Arc> _right;
1.2971 +
1.2972 + class ArcLess {
1.2973 + const Digraph &g;
1.2974 + public:
1.2975 + ArcLess(const Digraph &_g) : g(_g) {}
1.2976 + bool operator()(Arc a,Arc b) const
1.2977 + {
1.2978 + return g.target(a)<g.target(b);
1.2979 + }
1.2980 + };
1.2981 +
1.2982 + public:
1.2983 +
1.2984 + ///Constructor
1.2985 +
1.2986 + ///Constructor.
1.2987 + ///
1.2988 + ///It builds up the search database, which remains valid until the digraph
1.2989 + ///changes.
1.2990 + ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1.2991 +
1.2992 + private:
1.2993 + Arc refreshRec(std::vector<Arc> &v,int a,int b)
1.2994 + {
1.2995 + int m=(a+b)/2;
1.2996 + Arc me=v[m];
1.2997 + _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1.2998 + _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1.2999 + return me;
1.3000 + }
1.3001 + public:
1.3002 + ///Refresh the data structure at a node.
1.3003 +
1.3004 + ///Build up the search database of node \c n.
1.3005 + ///
1.3006 + ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
1.3007 + ///the number of the outgoing arcs of \c n.
1.3008 + void refresh(Node n)
1.3009 + {
1.3010 + std::vector<Arc> v;
1.3011 + for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1.3012 + if(v.size()) {
1.3013 + std::sort(v.begin(),v.end(),ArcLess(_g));
1.3014 + _head[n]=refreshRec(v,0,v.size()-1);
1.3015 + }
1.3016 + else _head[n]=INVALID;
1.3017 + }
1.3018 + ///Refresh the full data structure.
1.3019 +
1.3020 + ///Build up the full search database. In fact, it simply calls
1.3021 + ///\ref refresh(Node) "refresh(n)" for each node \c n.
1.3022 + ///
1.3023 + ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
1.3024 + ///the number of the arcs of \c n and <em>D</em> is the maximum
1.3025 + ///out-degree of the digraph.
1.3026 +
1.3027 + void refresh()
1.3028 + {
1.3029 + for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1.3030 + }
1.3031 +
1.3032 + ///Find an arc between two nodes.
1.3033 +
1.3034 + ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
1.3035 + /// <em>d</em> is the number of outgoing arcs of \c s.
1.3036 + ///\param s The source node
1.3037 + ///\param t The target node
1.3038 + ///\return An arc from \c s to \c t if there exists,
1.3039 + ///\ref INVALID otherwise.
1.3040 + ///
1.3041 + ///\warning If you change the digraph, refresh() must be called before using
1.3042 + ///this operator. If you change the outgoing arcs of
1.3043 + ///a single node \c n, then
1.3044 + ///\ref refresh(Node) "refresh(n)" is enough.
1.3045 + ///
1.3046 + Arc operator()(Node s, Node t) const
1.3047 + {
1.3048 + Arc e;
1.3049 + for(e=_head[s];
1.3050 + e!=INVALID&&_g.target(e)!=t;
1.3051 + e = t < _g.target(e)?_left[e]:_right[e]) ;
1.3052 + return e;
1.3053 + }
1.3054 +
1.3055 + };
1.3056 +
1.3057 + ///Fast look up of all arcs between given endpoints.
1.3058 +
1.3059 + ///\ingroup gutils
1.3060 + ///This class is the same as \ref ArcLookUp, with the addition
1.3061 + ///that it makes it possible to find all arcs between given endpoints.
1.3062 + ///
1.3063 + ///\warning This class is static, so you should refresh() (or at least
1.3064 + ///refresh(Node)) this data structure
1.3065 + ///whenever the digraph changes. This is a time consuming (superlinearly
1.3066 + ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
1.3067 + ///
1.3068 + ///\param G The type of the underlying digraph.
1.3069 + ///
1.3070 + ///\sa DynArcLookUp
1.3071 + ///\sa ArcLookUp
1.3072 + template<class G>
1.3073 + class AllArcLookUp : public ArcLookUp<G>
1.3074 + {
1.3075 + using ArcLookUp<G>::_g;
1.3076 + using ArcLookUp<G>::_right;
1.3077 + using ArcLookUp<G>::_left;
1.3078 + using ArcLookUp<G>::_head;
1.3079 +
1.3080 + GRAPH_TYPEDEFS(typename G);
1.3081 + typedef G Digraph;
1.3082 +
1.3083 + typename Digraph::template ArcMap<Arc> _next;
1.3084 +
1.3085 + Arc refreshNext(Arc head,Arc next=INVALID)
1.3086 + {
1.3087 + if(head==INVALID) return next;
1.3088 + else {
1.3089 + next=refreshNext(_right[head],next);
1.3090 +// _next[head]=next;
1.3091 + _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1.3092 + ? next : INVALID;
1.3093 + return refreshNext(_left[head],head);
1.3094 + }
1.3095 + }
1.3096 +
1.3097 + void refreshNext()
1.3098 + {
1.3099 + for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1.3100 + }
1.3101 +
1.3102 + public:
1.3103 + ///Constructor
1.3104 +
1.3105 + ///Constructor.
1.3106 + ///
1.3107 + ///It builds up the search database, which remains valid until the digraph
1.3108 + ///changes.
1.3109 + AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
1.3110 +
1.3111 + ///Refresh the data structure at a node.
1.3112 +
1.3113 + ///Build up the search database of node \c n.
1.3114 + ///
1.3115 + ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
1.3116 + ///the number of the outgoing arcs of \c n.
1.3117 +
1.3118 + void refresh(Node n)
1.3119 + {
1.3120 + ArcLookUp<G>::refresh(n);
1.3121 + refreshNext(_head[n]);
1.3122 + }
1.3123 +
1.3124 + ///Refresh the full data structure.
1.3125 +
1.3126 + ///Build up the full search database. In fact, it simply calls
1.3127 + ///\ref refresh(Node) "refresh(n)" for each node \c n.
1.3128 + ///
1.3129 + ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
1.3130 + ///the number of the arcs of \c n and <em>D</em> is the maximum
1.3131 + ///out-degree of the digraph.
1.3132 +
1.3133 + void refresh()
1.3134 + {
1.3135 + for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1.3136 + }
1.3137 +
1.3138 + ///Find an arc between two nodes.
1.3139 +
1.3140 + ///Find an arc between two nodes.
1.3141 + ///\param s The source node
1.3142 + ///\param t The target node
1.3143 + ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1.3144 + ///not given, the operator finds the first appropriate arc.
1.3145 + ///\return An arc from \c s to \c t after \c prev or
1.3146 + ///\ref INVALID if there is no more.
1.3147 + ///
1.3148 + ///For example, you can count the number of arcs from \c u to \c v in the
1.3149 + ///following way.
1.3150 + ///\code
1.3151 + ///AllArcLookUp<ListDigraph> ae(g);
1.3152 + ///...
1.3153 + ///int n=0;
1.3154 + ///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
1.3155 + ///\endcode
1.3156 + ///
1.3157 + ///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
1.3158 + /// <em>d</em> is the number of outgoing arcs of \c s. Then, the
1.3159 + ///consecutive arcs are found in constant time.
1.3160 + ///
1.3161 + ///\warning If you change the digraph, refresh() must be called before using
1.3162 + ///this operator. If you change the outgoing arcs of
1.3163 + ///a single node \c n, then
1.3164 + ///\ref refresh(Node) "refresh(n)" is enough.
1.3165 + ///
1.3166 +#ifdef DOXYGEN
1.3167 + Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1.3168 +#else
1.3169 + using ArcLookUp<G>::operator() ;
1.3170 + Arc operator()(Node s, Node t, Arc prev) const
1.3171 + {
1.3172 + return prev==INVALID?(*this)(s,t):_next[prev];
1.3173 + }
1.3174 +#endif
1.3175 +
1.3176 + };
1.3177 +
1.3178 + /// @}
1.3179 +
1.3180 +} //END OF NAMESPACE LEMON
1.3181 +
1.3182 +#endif