1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 ///\brief LEMON core utilities.
25 ///This header file contains core utilities for LEMON.
26 ///It is automatically included by all graph types, therefore it usually
27 ///do not have to be included directly.
29 // Disable the following warnings when compiling with MSVC:
30 // C4250: 'class1' : inherits 'class2::member' via dominance
31 // C4267: conversion from 'size_t' to 'type', possible loss of data
32 // C4355: 'this' : used in base member initializer list
33 // C4503: 'function' : decorated name length exceeded, name was truncated
34 // C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
35 // C4996: 'function': was declared deprecated
37 #pragma warning( disable : 4250 4267 4355 4503 4800 4996 )
40 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
41 // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8
42 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
48 #include <lemon/config.h>
49 #include <lemon/bits/enable_if.h>
50 #include <lemon/bits/traits.h>
51 #include <lemon/assert.h>
57 /// \brief Dummy type to make it easier to create invalid iterators.
59 /// Dummy type to make it easier to create invalid iterators.
60 /// See \ref INVALID for the usage.
63 bool operator==(Invalid) { return true; }
64 bool operator!=(Invalid) { return false; }
65 bool operator< (Invalid) { return false; }
68 /// \brief Invalid iterators.
70 /// \ref Invalid is a global type that converts to each iterator
71 /// in such a way that the value of the target iterator will be invalid.
72 #ifdef LEMON_ONLY_TEMPLATES
73 const Invalid INVALID = Invalid();
75 extern const Invalid INVALID;
78 /// \addtogroup gutils
81 ///Create convenience typedefs for the digraph types and iterators
83 ///This \c \#define creates convenient type definitions for the following
84 ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
85 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
86 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
88 ///\note If the graph type is a dependent type, ie. the graph type depend
89 ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
91 #define DIGRAPH_TYPEDEFS(Digraph) \
92 typedef Digraph::Node Node; \
93 typedef Digraph::NodeIt NodeIt; \
94 typedef Digraph::Arc Arc; \
95 typedef Digraph::ArcIt ArcIt; \
96 typedef Digraph::InArcIt InArcIt; \
97 typedef Digraph::OutArcIt OutArcIt; \
98 typedef Digraph::NodeMap<bool> BoolNodeMap; \
99 typedef Digraph::NodeMap<int> IntNodeMap; \
100 typedef Digraph::NodeMap<double> DoubleNodeMap; \
101 typedef Digraph::ArcMap<bool> BoolArcMap; \
102 typedef Digraph::ArcMap<int> IntArcMap; \
103 typedef Digraph::ArcMap<double> DoubleArcMap
105 ///Create convenience typedefs for the digraph types and iterators
107 ///\see DIGRAPH_TYPEDEFS
109 ///\note Use this macro, if the graph type is a dependent type,
110 ///ie. the graph type depend on a template parameter.
111 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
112 typedef typename Digraph::Node Node; \
113 typedef typename Digraph::NodeIt NodeIt; \
114 typedef typename Digraph::Arc Arc; \
115 typedef typename Digraph::ArcIt ArcIt; \
116 typedef typename Digraph::InArcIt InArcIt; \
117 typedef typename Digraph::OutArcIt OutArcIt; \
118 typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
119 typedef typename Digraph::template NodeMap<int> IntNodeMap; \
120 typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
121 typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
122 typedef typename Digraph::template ArcMap<int> IntArcMap; \
123 typedef typename Digraph::template ArcMap<double> DoubleArcMap
125 ///Create convenience typedefs for the graph types and iterators
127 ///This \c \#define creates the same convenient type definitions as defined
128 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
129 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
132 ///\note If the graph type is a dependent type, ie. the graph type depend
133 ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
135 #define GRAPH_TYPEDEFS(Graph) \
136 DIGRAPH_TYPEDEFS(Graph); \
137 typedef Graph::Edge Edge; \
138 typedef Graph::EdgeIt EdgeIt; \
139 typedef Graph::IncEdgeIt IncEdgeIt; \
140 typedef Graph::EdgeMap<bool> BoolEdgeMap; \
141 typedef Graph::EdgeMap<int> IntEdgeMap; \
142 typedef Graph::EdgeMap<double> DoubleEdgeMap
144 ///Create convenience typedefs for the graph types and iterators
146 ///\see GRAPH_TYPEDEFS
148 ///\note Use this macro, if the graph type is a dependent type,
149 ///ie. the graph type depend on a template parameter.
150 #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
151 TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
152 typedef typename Graph::Edge Edge; \
153 typedef typename Graph::EdgeIt EdgeIt; \
154 typedef typename Graph::IncEdgeIt IncEdgeIt; \
155 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
156 typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
157 typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
159 ///Create convenience typedefs for the bipartite graph types and iterators
161 ///This \c \#define creates the same convenient type definitions as
162 ///defined by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it
163 ///creates \c RedNode, \c RedNodeIt, \c BoolRedNodeMap,
164 ///\c IntRedNodeMap, \c DoubleRedNodeMap, \c BlueNode, \c BlueNodeIt,
165 ///\c BoolBlueNodeMap, \c IntBlueNodeMap, \c DoubleBlueNodeMap.
167 ///\note If the graph type is a dependent type, ie. the graph type depend
168 ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS()
170 #define BPGRAPH_TYPEDEFS(BpGraph) \
171 GRAPH_TYPEDEFS(BpGraph); \
172 typedef BpGraph::RedNode RedNode; \
173 typedef BpGraph::RedNodeIt RedNodeIt; \
174 typedef BpGraph::RedNodeMap<bool> BoolRedNodeMap; \
175 typedef BpGraph::RedNodeMap<int> IntRedNodeMap; \
176 typedef BpGraph::RedNodeMap<double> DoubleRedNodeMap; \
177 typedef BpGraph::BlueNode BlueNode; \
178 typedef BpGraph::BlueNodeIt BlueNodeIt; \
179 typedef BpGraph::BlueNodeMap<bool> BoolBlueNodeMap; \
180 typedef BpGraph::BlueNodeMap<int> IntBlueNodeMap; \
181 typedef BpGraph::BlueNodeMap<double> DoubleBlueNodeMap
183 ///Create convenience typedefs for the bipartite graph types and iterators
185 ///\see BPGRAPH_TYPEDEFS
187 ///\note Use this macro, if the graph type is a dependent type,
188 ///ie. the graph type depend on a template parameter.
189 #define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph) \
190 TEMPLATE_GRAPH_TYPEDEFS(BpGraph); \
191 typedef typename BpGraph::RedNode RedNode; \
192 typedef typename BpGraph::RedNodeIt RedNodeIt; \
193 typedef typename BpGraph::template RedNodeMap<bool> BoolRedNodeMap; \
194 typedef typename BpGraph::template RedNodeMap<int> IntRedNodeMap; \
195 typedef typename BpGraph::template RedNodeMap<double> DoubleRedNodeMap; \
196 typedef typename BpGraph::BlueNode BlueNode; \
197 typedef typename BpGraph::BlueNodeIt BlueNodeIt; \
198 typedef typename BpGraph::template BlueNodeMap<bool> BoolBlueNodeMap; \
199 typedef typename BpGraph::template BlueNodeMap<int> IntBlueNodeMap; \
200 typedef typename BpGraph::template BlueNodeMap<double> DoubleBlueNodeMap
202 /// \brief Function to count the items in a graph.
204 /// This function counts the items (nodes, arcs etc.) in a graph.
205 /// The complexity of the function is linear because
206 /// it iterates on all of the items.
207 template <typename Graph, typename Item>
208 inline int countItems(const Graph& g) {
209 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
211 for (ItemIt it(g); it != INVALID; ++it) {
219 namespace _core_bits {
221 template <typename Graph, typename Enable = void>
222 struct CountNodesSelector {
223 static int count(const Graph &g) {
224 return countItems<Graph, typename Graph::Node>(g);
228 template <typename Graph>
229 struct CountNodesSelector<
231 enable_if<typename Graph::NodeNumTag, void>::type>
233 static int count(const Graph &g) {
239 /// \brief Function to count the nodes in the graph.
241 /// This function counts the nodes in the graph.
242 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
243 /// graph structures it is specialized to run in <em>O</em>(1).
245 /// \note If the graph contains a \c nodeNum() member function and a
246 /// \c NodeNumTag tag then this function calls directly the member
247 /// function to query the cardinality of the node set.
248 template <typename Graph>
249 inline int countNodes(const Graph& g) {
250 return _core_bits::CountNodesSelector<Graph>::count(g);
253 namespace _graph_utils_bits {
255 template <typename Graph, typename Enable = void>
256 struct CountRedNodesSelector {
257 static int count(const Graph &g) {
258 return countItems<Graph, typename Graph::RedNode>(g);
262 template <typename Graph>
263 struct CountRedNodesSelector<
265 enable_if<typename Graph::NodeNumTag, void>::type>
267 static int count(const Graph &g) {
273 /// \brief Function to count the red nodes in the graph.
275 /// This function counts the red nodes in the graph.
276 /// The complexity of the function is O(n) but for some
277 /// graph structures it is specialized to run in O(1).
279 /// If the graph contains a \e redNum() member function and a
280 /// \e NodeNumTag tag then this function calls directly the member
281 /// function to query the cardinality of the node set.
282 template <typename Graph>
283 inline int countRedNodes(const Graph& g) {
284 return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g);
287 namespace _graph_utils_bits {
289 template <typename Graph, typename Enable = void>
290 struct CountBlueNodesSelector {
291 static int count(const Graph &g) {
292 return countItems<Graph, typename Graph::BlueNode>(g);
296 template <typename Graph>
297 struct CountBlueNodesSelector<
299 enable_if<typename Graph::NodeNumTag, void>::type>
301 static int count(const Graph &g) {
307 /// \brief Function to count the blue nodes in the graph.
309 /// This function counts the blue nodes in the graph.
310 /// The complexity of the function is O(n) but for some
311 /// graph structures it is specialized to run in O(1).
313 /// If the graph contains a \e blueNum() member function and a
314 /// \e NodeNumTag tag then this function calls directly the member
315 /// function to query the cardinality of the node set.
316 template <typename Graph>
317 inline int countBlueNodes(const Graph& g) {
318 return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g);
323 namespace _core_bits {
325 template <typename Graph, typename Enable = void>
326 struct CountArcsSelector {
327 static int count(const Graph &g) {
328 return countItems<Graph, typename Graph::Arc>(g);
332 template <typename Graph>
333 struct CountArcsSelector<
335 typename enable_if<typename Graph::ArcNumTag, void>::type>
337 static int count(const Graph &g) {
343 /// \brief Function to count the arcs in the graph.
345 /// This function counts the arcs in the graph.
346 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
347 /// graph structures it is specialized to run in <em>O</em>(1).
349 /// \note If the graph contains a \c arcNum() member function and a
350 /// \c ArcNumTag tag then this function calls directly the member
351 /// function to query the cardinality of the arc set.
352 template <typename Graph>
353 inline int countArcs(const Graph& g) {
354 return _core_bits::CountArcsSelector<Graph>::count(g);
359 namespace _core_bits {
361 template <typename Graph, typename Enable = void>
362 struct CountEdgesSelector {
363 static int count(const Graph &g) {
364 return countItems<Graph, typename Graph::Edge>(g);
368 template <typename Graph>
369 struct CountEdgesSelector<
371 typename enable_if<typename Graph::EdgeNumTag, void>::type>
373 static int count(const Graph &g) {
379 /// \brief Function to count the edges in the graph.
381 /// This function counts the edges in the graph.
382 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
383 /// graph structures it is specialized to run in <em>O</em>(1).
385 /// \note If the graph contains a \c edgeNum() member function and a
386 /// \c EdgeNumTag tag then this function calls directly the member
387 /// function to query the cardinality of the edge set.
388 template <typename Graph>
389 inline int countEdges(const Graph& g) {
390 return _core_bits::CountEdgesSelector<Graph>::count(g);
395 template <typename Graph, typename DegIt>
396 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
398 for (DegIt it(_g, _n); it != INVALID; ++it) {
404 /// \brief Function to count the number of the out-arcs from node \c n.
406 /// This function counts the number of the out-arcs from node \c n
407 /// in the graph \c g.
408 template <typename Graph>
409 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
410 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
413 /// \brief Function to count the number of the in-arcs to node \c n.
415 /// This function counts the number of the in-arcs to node \c n
416 /// in the graph \c g.
417 template <typename Graph>
418 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
419 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
422 /// \brief Function to count the number of the inc-edges to node \c n.
424 /// This function counts the number of the inc-edges to node \c n
425 /// in the undirected graph \c g.
426 template <typename Graph>
427 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
428 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
431 namespace _core_bits {
433 template <typename Digraph, typename Item, typename RefMap>
436 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
438 virtual ~MapCopyBase() {}
441 template <typename Digraph, typename Item, typename RefMap,
442 typename FromMap, typename ToMap>
443 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
446 MapCopy(const FromMap& map, ToMap& tmap)
447 : _map(map), _tmap(tmap) {}
449 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
450 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
451 for (ItemIt it(digraph); it != INVALID; ++it) {
452 _tmap.set(refMap[it], _map[it]);
461 template <typename Digraph, typename Item, typename RefMap, typename It>
462 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
465 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
467 virtual void copy(const Digraph&, const RefMap& refMap) {
476 template <typename Digraph, typename Item, typename RefMap, typename Ref>
477 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
480 RefCopy(Ref& map) : _map(map) {}
482 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
483 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
484 for (ItemIt it(digraph); it != INVALID; ++it) {
485 _map.set(it, refMap[it]);
493 template <typename Digraph, typename Item, typename RefMap,
495 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
498 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
500 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
501 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
502 for (ItemIt it(digraph); it != INVALID; ++it) {
503 _cmap.set(refMap[it], it);
511 template <typename Digraph, typename Enable = void>
512 struct DigraphCopySelector {
513 template <typename From, typename NodeRefMap, typename ArcRefMap>
514 static void copy(const From& from, Digraph &to,
515 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
517 for (typename From::NodeIt it(from); it != INVALID; ++it) {
518 nodeRefMap[it] = to.addNode();
520 for (typename From::ArcIt it(from); it != INVALID; ++it) {
521 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
522 nodeRefMap[from.target(it)]);
527 template <typename Digraph>
528 struct DigraphCopySelector<
530 typename enable_if<typename Digraph::BuildTag, void>::type>
532 template <typename From, typename NodeRefMap, typename ArcRefMap>
533 static void copy(const From& from, Digraph &to,
534 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
535 to.build(from, nodeRefMap, arcRefMap);
539 template <typename Graph, typename Enable = void>
540 struct GraphCopySelector {
541 template <typename From, typename NodeRefMap, typename EdgeRefMap>
542 static void copy(const From& from, Graph &to,
543 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
545 for (typename From::NodeIt it(from); it != INVALID; ++it) {
546 nodeRefMap[it] = to.addNode();
548 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
549 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
550 nodeRefMap[from.v(it)]);
555 template <typename Graph>
556 struct GraphCopySelector<
558 typename enable_if<typename Graph::BuildTag, void>::type>
560 template <typename From, typename NodeRefMap, typename EdgeRefMap>
561 static void copy(const From& from, Graph &to,
562 NodeRefMap& nodeRefMap,
563 EdgeRefMap& edgeRefMap) {
564 to.build(from, nodeRefMap, edgeRefMap);
568 template <typename BpGraph, typename Enable = void>
569 struct BpGraphCopySelector {
570 template <typename From, typename RedNodeRefMap,
571 typename BlueNodeRefMap, typename EdgeRefMap>
572 static void copy(const From& from, BpGraph &to,
573 RedNodeRefMap& redNodeRefMap,
574 BlueNodeRefMap& blueNodeRefMap,
575 EdgeRefMap& edgeRefMap) {
577 for (typename From::RedNodeIt it(from); it != INVALID; ++it) {
578 redNodeRefMap[it] = to.addRedNode();
580 for (typename From::BlueNodeIt it(from); it != INVALID; ++it) {
581 blueNodeRefMap[it] = to.addBlueNode();
583 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
584 edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)],
585 blueNodeRefMap[from.blueNode(it)]);
590 template <typename BpGraph>
591 struct BpGraphCopySelector<
593 typename enable_if<typename BpGraph::BuildTag, void>::type>
595 template <typename From, typename RedNodeRefMap,
596 typename BlueNodeRefMap, typename EdgeRefMap>
597 static void copy(const From& from, BpGraph &to,
598 RedNodeRefMap& redNodeRefMap,
599 BlueNodeRefMap& blueNodeRefMap,
600 EdgeRefMap& edgeRefMap) {
601 to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap);
607 /// \brief Check whether a graph is undirected.
609 /// This function returns \c true if the given graph is undirected.
611 template <typename GR>
612 bool undirected(const GR& g) { return false; }
614 template <typename GR>
615 typename enable_if<UndirectedTagIndicator<GR>, bool>::type
616 undirected(const GR&) {
619 template <typename GR>
620 typename disable_if<UndirectedTagIndicator<GR>, bool>::type
621 undirected(const GR&) {
626 /// \brief Class to copy a digraph.
628 /// Class to copy a digraph to another digraph (duplicate a digraph). The
629 /// simplest way of using it is through the \c digraphCopy() function.
631 /// This class not only make a copy of a digraph, but it can create
632 /// references and cross references between the nodes and arcs of
633 /// the two digraphs, and it can copy maps to use with the newly created
636 /// To make a copy from a digraph, first an instance of DigraphCopy
637 /// should be created, then the data belongs to the digraph should
638 /// assigned to copy. In the end, the \c run() member should be
641 /// The next code copies a digraph with several data:
643 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
644 /// // Create references for the nodes
645 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
647 /// // Create cross references (inverse) for the arcs
648 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
649 /// cg.arcCrossRef(acr);
650 /// // Copy an arc map
651 /// OrigGraph::ArcMap<double> oamap(orig_graph);
652 /// NewGraph::ArcMap<double> namap(new_graph);
653 /// cg.arcMap(oamap, namap);
655 /// OrigGraph::Node on;
656 /// NewGraph::Node nn;
658 /// // Execute copying
661 template <typename From, typename To>
665 typedef typename From::Node Node;
666 typedef typename From::NodeIt NodeIt;
667 typedef typename From::Arc Arc;
668 typedef typename From::ArcIt ArcIt;
670 typedef typename To::Node TNode;
671 typedef typename To::Arc TArc;
673 typedef typename From::template NodeMap<TNode> NodeRefMap;
674 typedef typename From::template ArcMap<TArc> ArcRefMap;
678 /// \brief Constructor of DigraphCopy.
680 /// Constructor of DigraphCopy for copying the content of the
681 /// \c from digraph into the \c to digraph.
682 DigraphCopy(const From& from, To& to)
683 : _from(from), _to(to) {}
685 /// \brief Destructor of DigraphCopy
687 /// Destructor of DigraphCopy.
689 for (int i = 0; i < int(_node_maps.size()); ++i) {
690 delete _node_maps[i];
692 for (int i = 0; i < int(_arc_maps.size()); ++i) {
698 /// \brief Copy the node references into the given map.
700 /// This function copies the node references into the given map.
701 /// The parameter should be a map, whose key type is the Node type of
702 /// the source digraph, while the value type is the Node type of the
703 /// destination digraph.
704 template <typename NodeRef>
705 DigraphCopy& nodeRef(NodeRef& map) {
706 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
707 NodeRefMap, NodeRef>(map));
711 /// \brief Copy the node cross references into the given map.
713 /// This function copies the node cross references (reverse references)
714 /// into the given map. The parameter should be a map, whose key type
715 /// is the Node type of the destination digraph, while the value type is
716 /// the Node type of the source digraph.
717 template <typename NodeCrossRef>
718 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
719 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
720 NodeRefMap, NodeCrossRef>(map));
724 /// \brief Make a copy of the given node map.
726 /// This function makes a copy of the given node map for the newly
728 /// The key type of the new map \c tmap should be the Node type of the
729 /// destination digraph, and the key type of the original map \c map
730 /// should be the Node type of the source digraph.
731 template <typename FromMap, typename ToMap>
732 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
733 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
734 NodeRefMap, FromMap, ToMap>(map, tmap));
738 /// \brief Make a copy of the given node.
740 /// This function makes a copy of the given node.
741 DigraphCopy& node(const Node& node, TNode& tnode) {
742 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
743 NodeRefMap, TNode>(node, tnode));
747 /// \brief Copy the arc references into the given map.
749 /// This function copies the arc references into the given map.
750 /// The parameter should be a map, whose key type is the Arc type of
751 /// the source digraph, while the value type is the Arc type of the
752 /// destination digraph.
753 template <typename ArcRef>
754 DigraphCopy& arcRef(ArcRef& map) {
755 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
756 ArcRefMap, ArcRef>(map));
760 /// \brief Copy the arc cross references into the given map.
762 /// This function copies the arc cross references (reverse references)
763 /// into the given map. The parameter should be a map, whose key type
764 /// is the Arc type of the destination digraph, while the value type is
765 /// the Arc type of the source digraph.
766 template <typename ArcCrossRef>
767 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
768 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
769 ArcRefMap, ArcCrossRef>(map));
773 /// \brief Make a copy of the given arc map.
775 /// This function makes a copy of the given arc map for the newly
777 /// The key type of the new map \c tmap should be the Arc type of the
778 /// destination digraph, and the key type of the original map \c map
779 /// should be the Arc type of the source digraph.
780 template <typename FromMap, typename ToMap>
781 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
782 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
783 ArcRefMap, FromMap, ToMap>(map, tmap));
787 /// \brief Make a copy of the given arc.
789 /// This function makes a copy of the given arc.
790 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
791 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
792 ArcRefMap, TArc>(arc, tarc));
796 /// \brief Execute copying.
798 /// This function executes the copying of the digraph along with the
799 /// copying of the assigned data.
801 NodeRefMap nodeRefMap(_from);
802 ArcRefMap arcRefMap(_from);
803 _core_bits::DigraphCopySelector<To>::
804 copy(_from, _to, nodeRefMap, arcRefMap);
805 for (int i = 0; i < int(_node_maps.size()); ++i) {
806 _node_maps[i]->copy(_from, nodeRefMap);
808 for (int i = 0; i < int(_arc_maps.size()); ++i) {
809 _arc_maps[i]->copy(_from, arcRefMap);
818 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
821 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
826 /// \brief Copy a digraph to another digraph.
828 /// This function copies a digraph to another digraph.
829 /// The complete usage of it is detailed in the DigraphCopy class, but
830 /// a short example shows a basic work:
832 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
835 /// After the copy the \c nr map will contain the mapping from the
836 /// nodes of the \c from digraph to the nodes of the \c to digraph and
837 /// \c acr will contain the mapping from the arcs of the \c to digraph
838 /// to the arcs of the \c from digraph.
841 template <typename From, typename To>
842 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
843 return DigraphCopy<From, To>(from, to);
846 /// \brief Class to copy a graph.
848 /// Class to copy a graph to another graph (duplicate a graph). The
849 /// simplest way of using it is through the \c graphCopy() function.
851 /// This class not only make a copy of a graph, but it can create
852 /// references and cross references between the nodes, edges and arcs of
853 /// the two graphs, and it can copy maps for using with the newly created
856 /// To make a copy from a graph, first an instance of GraphCopy
857 /// should be created, then the data belongs to the graph should
858 /// assigned to copy. In the end, the \c run() member should be
861 /// The next code copies a graph with several data:
863 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
864 /// // Create references for the nodes
865 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
867 /// // Create cross references (inverse) for the edges
868 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
869 /// cg.edgeCrossRef(ecr);
870 /// // Copy an edge map
871 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
872 /// NewGraph::EdgeMap<double> nemap(new_graph);
873 /// cg.edgeMap(oemap, nemap);
875 /// OrigGraph::Node on;
876 /// NewGraph::Node nn;
878 /// // Execute copying
881 template <typename From, typename To>
885 typedef typename From::Node Node;
886 typedef typename From::NodeIt NodeIt;
887 typedef typename From::Arc Arc;
888 typedef typename From::ArcIt ArcIt;
889 typedef typename From::Edge Edge;
890 typedef typename From::EdgeIt EdgeIt;
892 typedef typename To::Node TNode;
893 typedef typename To::Arc TArc;
894 typedef typename To::Edge TEdge;
896 typedef typename From::template NodeMap<TNode> NodeRefMap;
897 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
900 ArcRefMap(const From& from, const To& to,
901 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
902 : _from(from), _to(to),
903 _edge_ref(edge_ref), _node_ref(node_ref) {}
905 typedef typename From::Arc Key;
906 typedef typename To::Arc Value;
908 Value operator[](const Key& key) const {
909 bool forward = _from.u(key) != _from.v(key) ?
910 _node_ref[_from.source(key)] ==
911 _to.source(_to.direct(_edge_ref[key], true)) :
912 _from.direction(key);
913 return _to.direct(_edge_ref[key], forward);
918 const EdgeRefMap& _edge_ref;
919 const NodeRefMap& _node_ref;
924 /// \brief Constructor of GraphCopy.
926 /// Constructor of GraphCopy for copying the content of the
927 /// \c from graph into the \c to graph.
928 GraphCopy(const From& from, To& to)
929 : _from(from), _to(to) {}
931 /// \brief Destructor of GraphCopy
933 /// Destructor of GraphCopy.
935 for (int i = 0; i < int(_node_maps.size()); ++i) {
936 delete _node_maps[i];
938 for (int i = 0; i < int(_arc_maps.size()); ++i) {
941 for (int i = 0; i < int(_edge_maps.size()); ++i) {
942 delete _edge_maps[i];
946 /// \brief Copy the node references into the given map.
948 /// This function copies the node references into the given map.
949 /// The parameter should be a map, whose key type is the Node type of
950 /// the source graph, while the value type is the Node type of the
951 /// destination graph.
952 template <typename NodeRef>
953 GraphCopy& nodeRef(NodeRef& map) {
954 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
955 NodeRefMap, NodeRef>(map));
959 /// \brief Copy the node cross references into the given map.
961 /// This function copies the node cross references (reverse references)
962 /// into the given map. The parameter should be a map, whose key type
963 /// is the Node type of the destination graph, while the value type is
964 /// the Node type of the source graph.
965 template <typename NodeCrossRef>
966 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
967 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
968 NodeRefMap, NodeCrossRef>(map));
972 /// \brief Make a copy of the given node map.
974 /// This function makes a copy of the given node map for the newly
976 /// The key type of the new map \c tmap should be the Node type of the
977 /// destination graph, and the key type of the original map \c map
978 /// should be the Node type of the source graph.
979 template <typename FromMap, typename ToMap>
980 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
981 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
982 NodeRefMap, FromMap, ToMap>(map, tmap));
986 /// \brief Make a copy of the given node.
988 /// This function makes a copy of the given node.
989 GraphCopy& node(const Node& node, TNode& tnode) {
990 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
991 NodeRefMap, TNode>(node, tnode));
995 /// \brief Copy the arc references into the given map.
997 /// This function copies the arc references into the given map.
998 /// The parameter should be a map, whose key type is the Arc type of
999 /// the source graph, while the value type is the Arc type of the
1000 /// destination graph.
1001 template <typename ArcRef>
1002 GraphCopy& arcRef(ArcRef& map) {
1003 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
1004 ArcRefMap, ArcRef>(map));
1008 /// \brief Copy the arc cross references into the given map.
1010 /// This function copies the arc cross references (reverse references)
1011 /// into the given map. The parameter should be a map, whose key type
1012 /// is the Arc type of the destination graph, while the value type is
1013 /// the Arc type of the source graph.
1014 template <typename ArcCrossRef>
1015 GraphCopy& arcCrossRef(ArcCrossRef& map) {
1016 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
1017 ArcRefMap, ArcCrossRef>(map));
1021 /// \brief Make a copy of the given arc map.
1023 /// This function makes a copy of the given arc map for the newly
1025 /// The key type of the new map \c tmap should be the Arc type of the
1026 /// destination graph, and the key type of the original map \c map
1027 /// should be the Arc type of the source graph.
1028 template <typename FromMap, typename ToMap>
1029 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
1030 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
1031 ArcRefMap, FromMap, ToMap>(map, tmap));
1035 /// \brief Make a copy of the given arc.
1037 /// This function makes a copy of the given arc.
1038 GraphCopy& arc(const Arc& arc, TArc& tarc) {
1039 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
1040 ArcRefMap, TArc>(arc, tarc));
1044 /// \brief Copy the edge references into the given map.
1046 /// This function copies the edge references into the given map.
1047 /// The parameter should be a map, whose key type is the Edge type of
1048 /// the source graph, while the value type is the Edge type of the
1049 /// destination graph.
1050 template <typename EdgeRef>
1051 GraphCopy& edgeRef(EdgeRef& map) {
1052 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
1053 EdgeRefMap, EdgeRef>(map));
1057 /// \brief Copy the edge cross references into the given map.
1059 /// This function copies the edge cross references (reverse references)
1060 /// into the given map. The parameter should be a map, whose key type
1061 /// is the Edge type of the destination graph, while the value type is
1062 /// the Edge type of the source graph.
1063 template <typename EdgeCrossRef>
1064 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1065 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
1066 Edge, EdgeRefMap, EdgeCrossRef>(map));
1070 /// \brief Make a copy of the given edge map.
1072 /// This function makes a copy of the given edge map for the newly
1074 /// The key type of the new map \c tmap should be the Edge type of the
1075 /// destination graph, and the key type of the original map \c map
1076 /// should be the Edge type of the source graph.
1077 template <typename FromMap, typename ToMap>
1078 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
1079 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
1080 EdgeRefMap, FromMap, ToMap>(map, tmap));
1084 /// \brief Make a copy of the given edge.
1086 /// This function makes a copy of the given edge.
1087 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
1088 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
1089 EdgeRefMap, TEdge>(edge, tedge));
1093 /// \brief Execute copying.
1095 /// This function executes the copying of the graph along with the
1096 /// copying of the assigned data.
1098 NodeRefMap nodeRefMap(_from);
1099 EdgeRefMap edgeRefMap(_from);
1100 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
1101 _core_bits::GraphCopySelector<To>::
1102 copy(_from, _to, nodeRefMap, edgeRefMap);
1103 for (int i = 0; i < int(_node_maps.size()); ++i) {
1104 _node_maps[i]->copy(_from, nodeRefMap);
1106 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1107 _edge_maps[i]->copy(_from, edgeRefMap);
1109 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1110 _arc_maps[i]->copy(_from, arcRefMap);
1119 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
1122 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1125 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1130 /// \brief Copy a graph to another graph.
1132 /// This function copies a graph to another graph.
1133 /// The complete usage of it is detailed in the GraphCopy class,
1134 /// but a short example shows a basic work:
1136 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
1139 /// After the copy the \c nr map will contain the mapping from the
1140 /// nodes of the \c from graph to the nodes of the \c to graph and
1141 /// \c ecr will contain the mapping from the edges of the \c to graph
1142 /// to the edges of the \c from graph.
1145 template <typename From, typename To>
1147 graphCopy(const From& from, To& to) {
1148 return GraphCopy<From, To>(from, to);
1151 /// \brief Class to copy a bipartite graph.
1153 /// Class to copy a bipartite graph to another graph (duplicate a
1154 /// graph). The simplest way of using it is through the
1155 /// \c bpGraphCopy() function.
1157 /// This class not only make a copy of a bipartite graph, but it can
1158 /// create references and cross references between the nodes, edges
1159 /// and arcs of the two graphs, and it can copy maps for using with
1160 /// the newly created graph.
1162 /// To make a copy from a graph, first an instance of BpGraphCopy
1163 /// should be created, then the data belongs to the graph should
1164 /// assigned to copy. In the end, the \c run() member should be
1167 /// The next code copies a graph with several data:
1169 /// BpGraphCopy<OrigBpGraph, NewBpGraph> cg(orig_graph, new_graph);
1170 /// // Create references for the nodes
1171 /// OrigBpGraph::NodeMap<NewBpGraph::Node> nr(orig_graph);
1173 /// // Create cross references (inverse) for the edges
1174 /// NewBpGraph::EdgeMap<OrigBpGraph::Edge> ecr(new_graph);
1175 /// cg.edgeCrossRef(ecr);
1176 /// // Copy a red node map
1177 /// OrigBpGraph::RedNodeMap<double> ormap(orig_graph);
1178 /// NewBpGraph::RedNodeMap<double> nrmap(new_graph);
1179 /// cg.redNodeMap(ormap, nrmap);
1181 /// OrigBpGraph::Node on;
1182 /// NewBpGraph::Node nn;
1183 /// cg.node(on, nn);
1184 /// // Execute copying
1187 template <typename From, typename To>
1191 typedef typename From::Node Node;
1192 typedef typename From::RedNode RedNode;
1193 typedef typename From::BlueNode BlueNode;
1194 typedef typename From::NodeIt NodeIt;
1195 typedef typename From::Arc Arc;
1196 typedef typename From::ArcIt ArcIt;
1197 typedef typename From::Edge Edge;
1198 typedef typename From::EdgeIt EdgeIt;
1200 typedef typename To::Node TNode;
1201 typedef typename To::RedNode TRedNode;
1202 typedef typename To::BlueNode TBlueNode;
1203 typedef typename To::Arc TArc;
1204 typedef typename To::Edge TEdge;
1206 typedef typename From::template RedNodeMap<TRedNode> RedNodeRefMap;
1207 typedef typename From::template BlueNodeMap<TBlueNode> BlueNodeRefMap;
1208 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
1211 NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref,
1212 const BlueNodeRefMap& blue_node_ref)
1213 : _from(from), _red_node_ref(red_node_ref),
1214 _blue_node_ref(blue_node_ref) {}
1216 typedef typename From::Node Key;
1217 typedef typename To::Node Value;
1219 Value operator[](const Key& key) const {
1220 if (_from.red(key)) {
1221 return _red_node_ref[_from.asRedNodeUnsafe(key)];
1223 return _blue_node_ref[_from.asBlueNodeUnsafe(key)];
1228 const RedNodeRefMap& _red_node_ref;
1229 const BlueNodeRefMap& _blue_node_ref;
1233 ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
1234 : _from(from), _to(to), _edge_ref(edge_ref) {}
1236 typedef typename From::Arc Key;
1237 typedef typename To::Arc Value;
1239 Value operator[](const Key& key) const {
1240 return _to.direct(_edge_ref[key], _from.direction(key));
1245 const EdgeRefMap& _edge_ref;
1250 /// \brief Constructor of BpGraphCopy.
1252 /// Constructor of BpGraphCopy for copying the content of the
1253 /// \c from graph into the \c to graph.
1254 BpGraphCopy(const From& from, To& to)
1255 : _from(from), _to(to) {}
1257 /// \brief Destructor of BpGraphCopy
1259 /// Destructor of BpGraphCopy.
1261 for (int i = 0; i < int(_node_maps.size()); ++i) {
1262 delete _node_maps[i];
1264 for (int i = 0; i < int(_red_maps.size()); ++i) {
1265 delete _red_maps[i];
1267 for (int i = 0; i < int(_blue_maps.size()); ++i) {
1268 delete _blue_maps[i];
1270 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1271 delete _arc_maps[i];
1273 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1274 delete _edge_maps[i];
1278 /// \brief Copy the node references into the given map.
1280 /// This function copies the node references into the given map.
1281 /// The parameter should be a map, whose key type is the Node type of
1282 /// the source graph, while the value type is the Node type of the
1283 /// destination graph.
1284 template <typename NodeRef>
1285 BpGraphCopy& nodeRef(NodeRef& map) {
1286 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
1287 NodeRefMap, NodeRef>(map));
1291 /// \brief Copy the node cross references into the given map.
1293 /// This function copies the node cross references (reverse references)
1294 /// into the given map. The parameter should be a map, whose key type
1295 /// is the Node type of the destination graph, while the value type is
1296 /// the Node type of the source graph.
1297 template <typename NodeCrossRef>
1298 BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1299 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
1300 NodeRefMap, NodeCrossRef>(map));
1304 /// \brief Make a copy of the given node map.
1306 /// This function makes a copy of the given node map for the newly
1308 /// The key type of the new map \c tmap should be the Node type of the
1309 /// destination graph, and the key type of the original map \c map
1310 /// should be the Node type of the source graph.
1311 template <typename FromMap, typename ToMap>
1312 BpGraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
1313 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
1314 NodeRefMap, FromMap, ToMap>(map, tmap));
1318 /// \brief Make a copy of the given node.
1320 /// This function makes a copy of the given node.
1321 BpGraphCopy& node(const Node& node, TNode& tnode) {
1322 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
1323 NodeRefMap, TNode>(node, tnode));
1327 /// \brief Copy the red node references into the given map.
1329 /// This function copies the red node references into the given
1330 /// map. The parameter should be a map, whose key type is the
1331 /// Node type of the source graph with the red item set, while the
1332 /// value type is the Node type of the destination graph.
1333 template <typename RedRef>
1334 BpGraphCopy& redRef(RedRef& map) {
1335 _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
1336 RedNodeRefMap, RedRef>(map));
1340 /// \brief Copy the red node cross references into the given map.
1342 /// This function copies the red node cross references (reverse
1343 /// references) into the given map. The parameter should be a map,
1344 /// whose key type is the Node type of the destination graph with
1345 /// the red item set, while the value type is the Node type of the
1347 template <typename RedCrossRef>
1348 BpGraphCopy& redCrossRef(RedCrossRef& map) {
1349 _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
1350 RedNodeRefMap, RedCrossRef>(map));
1354 /// \brief Make a copy of the given red node map.
1356 /// This function makes a copy of the given red node map for the newly
1358 /// The key type of the new map \c tmap should be the Node type of
1359 /// the destination graph with the red items, and the key type of
1360 /// the original map \c map should be the Node type of the source
1362 template <typename FromMap, typename ToMap>
1363 BpGraphCopy& redNodeMap(const FromMap& map, ToMap& tmap) {
1364 _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
1365 RedNodeRefMap, FromMap, ToMap>(map, tmap));
1369 /// \brief Make a copy of the given red node.
1371 /// This function makes a copy of the given red node.
1372 BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) {
1373 _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode,
1374 RedNodeRefMap, TRedNode>(node, tnode));
1378 /// \brief Copy the blue node references into the given map.
1380 /// This function copies the blue node references into the given
1381 /// map. The parameter should be a map, whose key type is the
1382 /// Node type of the source graph with the blue item set, while the
1383 /// value type is the Node type of the destination graph.
1384 template <typename BlueRef>
1385 BpGraphCopy& blueRef(BlueRef& map) {
1386 _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
1387 BlueNodeRefMap, BlueRef>(map));
1391 /// \brief Copy the blue node cross references into the given map.
1393 /// This function copies the blue node cross references (reverse
1394 /// references) into the given map. The parameter should be a map,
1395 /// whose key type is the Node type of the destination graph with
1396 /// the blue item set, while the value type is the Node type of the
1398 template <typename BlueCrossRef>
1399 BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
1400 _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
1401 BlueNodeRefMap, BlueCrossRef>(map));
1405 /// \brief Make a copy of the given blue node map.
1407 /// This function makes a copy of the given blue node map for the newly
1409 /// The key type of the new map \c tmap should be the Node type of
1410 /// the destination graph with the blue items, and the key type of
1411 /// the original map \c map should be the Node type of the source
1413 template <typename FromMap, typename ToMap>
1414 BpGraphCopy& blueNodeMap(const FromMap& map, ToMap& tmap) {
1415 _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
1416 BlueNodeRefMap, FromMap, ToMap>(map, tmap));
1420 /// \brief Make a copy of the given blue node.
1422 /// This function makes a copy of the given blue node.
1423 BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) {
1424 _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode,
1425 BlueNodeRefMap, TBlueNode>(node, tnode));
1429 /// \brief Copy the arc references into the given map.
1431 /// This function copies the arc references into the given map.
1432 /// The parameter should be a map, whose key type is the Arc type of
1433 /// the source graph, while the value type is the Arc type of the
1434 /// destination graph.
1435 template <typename ArcRef>
1436 BpGraphCopy& arcRef(ArcRef& map) {
1437 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
1438 ArcRefMap, ArcRef>(map));
1442 /// \brief Copy the arc cross references into the given map.
1444 /// This function copies the arc cross references (reverse references)
1445 /// into the given map. The parameter should be a map, whose key type
1446 /// is the Arc type of the destination graph, while the value type is
1447 /// the Arc type of the source graph.
1448 template <typename ArcCrossRef>
1449 BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
1450 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
1451 ArcRefMap, ArcCrossRef>(map));
1455 /// \brief Make a copy of the given arc map.
1457 /// This function makes a copy of the given arc map for the newly
1459 /// The key type of the new map \c tmap should be the Arc type of the
1460 /// destination graph, and the key type of the original map \c map
1461 /// should be the Arc type of the source graph.
1462 template <typename FromMap, typename ToMap>
1463 BpGraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
1464 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
1465 ArcRefMap, FromMap, ToMap>(map, tmap));
1469 /// \brief Make a copy of the given arc.
1471 /// This function makes a copy of the given arc.
1472 BpGraphCopy& arc(const Arc& arc, TArc& tarc) {
1473 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
1474 ArcRefMap, TArc>(arc, tarc));
1478 /// \brief Copy the edge references into the given map.
1480 /// This function copies the edge references into the given map.
1481 /// The parameter should be a map, whose key type is the Edge type of
1482 /// the source graph, while the value type is the Edge type of the
1483 /// destination graph.
1484 template <typename EdgeRef>
1485 BpGraphCopy& edgeRef(EdgeRef& map) {
1486 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
1487 EdgeRefMap, EdgeRef>(map));
1491 /// \brief Copy the edge cross references into the given map.
1493 /// This function copies the edge cross references (reverse references)
1494 /// into the given map. The parameter should be a map, whose key type
1495 /// is the Edge type of the destination graph, while the value type is
1496 /// the Edge type of the source graph.
1497 template <typename EdgeCrossRef>
1498 BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1499 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
1500 Edge, EdgeRefMap, EdgeCrossRef>(map));
1504 /// \brief Make a copy of the given edge map.
1506 /// This function makes a copy of the given edge map for the newly
1508 /// The key type of the new map \c tmap should be the Edge type of the
1509 /// destination graph, and the key type of the original map \c map
1510 /// should be the Edge type of the source graph.
1511 template <typename FromMap, typename ToMap>
1512 BpGraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
1513 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
1514 EdgeRefMap, FromMap, ToMap>(map, tmap));
1518 /// \brief Make a copy of the given edge.
1520 /// This function makes a copy of the given edge.
1521 BpGraphCopy& edge(const Edge& edge, TEdge& tedge) {
1522 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
1523 EdgeRefMap, TEdge>(edge, tedge));
1527 /// \brief Execute copying.
1529 /// This function executes the copying of the graph along with the
1530 /// copying of the assigned data.
1532 RedNodeRefMap redNodeRefMap(_from);
1533 BlueNodeRefMap blueNodeRefMap(_from);
1534 NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap);
1535 EdgeRefMap edgeRefMap(_from);
1536 ArcRefMap arcRefMap(_from, _to, edgeRefMap);
1537 _core_bits::BpGraphCopySelector<To>::
1538 copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap);
1539 for (int i = 0; i < int(_node_maps.size()); ++i) {
1540 _node_maps[i]->copy(_from, nodeRefMap);
1542 for (int i = 0; i < int(_red_maps.size()); ++i) {
1543 _red_maps[i]->copy(_from, redNodeRefMap);
1545 for (int i = 0; i < int(_blue_maps.size()); ++i) {
1546 _blue_maps[i]->copy(_from, blueNodeRefMap);
1548 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1549 _edge_maps[i]->copy(_from, edgeRefMap);
1551 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1552 _arc_maps[i]->copy(_from, arcRefMap);
1561 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
1564 std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* >
1567 std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* >
1570 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1573 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1578 /// \brief Copy a graph to another graph.
1580 /// This function copies a graph to another graph.
1581 /// The complete usage of it is detailed in the BpGraphCopy class,
1582 /// but a short example shows a basic work:
1584 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
1587 /// After the copy the \c nr map will contain the mapping from the
1588 /// nodes of the \c from graph to the nodes of the \c to graph and
1589 /// \c ecr will contain the mapping from the edges of the \c to graph
1590 /// to the edges of the \c from graph.
1592 /// \see BpGraphCopy
1593 template <typename From, typename To>
1594 BpGraphCopy<From, To>
1595 bpGraphCopy(const From& from, To& to) {
1596 return BpGraphCopy<From, To>(from, to);
1599 namespace _core_bits {
1601 template <typename Graph, typename Enable = void>
1602 struct FindArcSelector {
1603 typedef typename Graph::Node Node;
1604 typedef typename Graph::Arc Arc;
1605 static Arc find(const Graph &g, Node u, Node v, Arc e) {
1611 while (e != INVALID && g.target(e) != v) {
1618 template <typename Graph>
1619 struct FindArcSelector<
1621 typename enable_if<typename Graph::FindArcTag, void>::type>
1623 typedef typename Graph::Node Node;
1624 typedef typename Graph::Arc Arc;
1625 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1626 return g.findArc(u, v, prev);
1631 /// \brief Find an arc between two nodes of a digraph.
1633 /// This function finds an arc from node \c u to node \c v in the
1636 /// If \c prev is \ref INVALID (this is the default value), then
1637 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1638 /// the next arc from \c u to \c v after \c prev.
1639 /// \return The found arc or \ref INVALID if there is no such an arc.
1641 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1643 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1648 /// \note \ref ConArcIt provides iterator interface for the same
1652 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1653 template <typename Graph>
1654 inline typename Graph::Arc
1655 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1656 typename Graph::Arc prev = INVALID) {
1657 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1660 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1662 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1663 /// a higher level interface for the \ref findArc() function. You can
1664 /// use it the following way:
1666 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1672 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1673 template <typename GR>
1674 class ConArcIt : public GR::Arc {
1675 typedef typename GR::Arc Parent;
1679 typedef typename GR::Arc Arc;
1680 typedef typename GR::Node Node;
1682 /// \brief Constructor.
1684 /// Construct a new ConArcIt iterating on the arcs that
1685 /// connects nodes \c u and \c v.
1686 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1687 Parent::operator=(findArc(_graph, u, v));
1690 /// \brief Constructor.
1692 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1693 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1695 /// \brief Increment operator.
1697 /// It increments the iterator and gives back the next arc.
1698 ConArcIt& operator++() {
1699 Parent::operator=(findArc(_graph, _graph.source(*this),
1700 _graph.target(*this), *this));
1707 namespace _core_bits {
1709 template <typename Graph, typename Enable = void>
1710 struct FindEdgeSelector {
1711 typedef typename Graph::Node Node;
1712 typedef typename Graph::Edge Edge;
1713 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1717 g.firstInc(e, b, u);
1722 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1727 g.firstInc(e, b, u);
1732 while (e != INVALID && (!b || g.v(e) != v)) {
1740 template <typename Graph>
1741 struct FindEdgeSelector<
1743 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1745 typedef typename Graph::Node Node;
1746 typedef typename Graph::Edge Edge;
1747 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1748 return g.findEdge(u, v, prev);
1753 /// \brief Find an edge between two nodes of a graph.
1755 /// This function finds an edge from node \c u to node \c v in graph \c g.
1756 /// If node \c u and node \c v is equal then each loop edge
1757 /// will be enumerated once.
1759 /// If \c prev is \ref INVALID (this is the default value), then
1760 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1761 /// the next edge from \c u to \c v after \c prev.
1762 /// \return The found edge or \ref INVALID if there is no such an edge.
1764 /// Thus you can iterate through each edge between \c u and \c v
1767 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1772 /// \note \ref ConEdgeIt provides iterator interface for the same
1776 template <typename Graph>
1777 inline typename Graph::Edge
1778 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1779 typename Graph::Edge p = INVALID) {
1780 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1783 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1785 /// Iterator for iterating on parallel edges connecting the same nodes.
1786 /// It is a higher level interface for the findEdge() function. You can
1787 /// use it the following way:
1789 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1795 template <typename GR>
1796 class ConEdgeIt : public GR::Edge {
1797 typedef typename GR::Edge Parent;
1801 typedef typename GR::Edge Edge;
1802 typedef typename GR::Node Node;
1804 /// \brief Constructor.
1806 /// Construct a new ConEdgeIt iterating on the edges that
1807 /// connects nodes \c u and \c v.
1808 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1809 Parent::operator=(findEdge(_graph, _u, _v));
1812 /// \brief Constructor.
1814 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1815 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1817 /// \brief Increment operator.
1819 /// It increments the iterator and gives back the next edge.
1820 ConEdgeIt& operator++() {
1821 Parent::operator=(findEdge(_graph, _u, _v, *this));
1830 ///Dynamic arc look-up between given endpoints.
1832 ///Using this class, you can find an arc in a digraph from a given
1833 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1834 ///where <em>d</em> is the out-degree of the source node.
1836 ///It is possible to find \e all parallel arcs between two nodes with
1837 ///the \c operator() member.
1839 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1840 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1842 ///This class uses a self-adjusting binary search tree, the Splay tree
1843 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1844 ///time bound for arc look-ups. This class also guarantees the
1845 ///optimal time bound in a constant factor for any distribution of
1848 ///\tparam GR The type of the underlying digraph.
1852 template <typename GR>
1854 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1856 typedef typename ItemSetTraits<GR, typename GR::Arc>
1857 ::ItemNotifier::ObserverBase Parent;
1859 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1863 /// The Digraph type
1868 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
1870 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1874 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1876 virtual void add(const Node& node) {
1878 Parent::set(node, INVALID);
1881 virtual void add(const std::vector<Node>& nodes) {
1883 for (int i = 0; i < int(nodes.size()); ++i) {
1884 Parent::set(nodes[i], INVALID);
1888 virtual void build() {
1891 typename Parent::Notifier* nf = Parent::notifier();
1892 for (nf->first(it); it != INVALID; nf->next(it)) {
1893 Parent::set(it, INVALID);
1901 ArcLess(const Digraph &_g) : g(_g) {}
1902 bool operator()(Arc a,Arc b) const
1904 return g.target(a)<g.target(b);
1912 typename Digraph::template ArcMap<Arc> _parent;
1913 typename Digraph::template ArcMap<Arc> _left;
1914 typename Digraph::template ArcMap<Arc> _right;
1922 ///It builds up the search database.
1923 DynArcLookUp(const Digraph &g)
1924 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1926 Parent::attach(_g.notifier(typename Digraph::Arc()));
1932 virtual void add(const Arc& arc) {
1936 virtual void add(const std::vector<Arc>& arcs) {
1937 for (int i = 0; i < int(arcs.size()); ++i) {
1942 virtual void erase(const Arc& arc) {
1946 virtual void erase(const std::vector<Arc>& arcs) {
1947 for (int i = 0; i < int(arcs.size()); ++i) {
1952 virtual void build() {
1956 virtual void clear() {
1957 for(NodeIt n(_g);n!=INVALID;++n) {
1962 void insert(Arc arc) {
1963 Node s = _g.source(arc);
1964 Node t = _g.target(arc);
1965 _left[arc] = INVALID;
1966 _right[arc] = INVALID;
1971 _parent[arc] = INVALID;
1975 if (t < _g.target(e)) {
1976 if (_left[e] == INVALID) {
1985 if (_right[e] == INVALID) {
1997 void remove(Arc arc) {
1998 if (_left[arc] == INVALID) {
1999 if (_right[arc] != INVALID) {
2000 _parent[_right[arc]] = _parent[arc];
2002 if (_parent[arc] != INVALID) {
2003 if (_left[_parent[arc]] == arc) {
2004 _left[_parent[arc]] = _right[arc];
2006 _right[_parent[arc]] = _right[arc];
2009 _head[_g.source(arc)] = _right[arc];
2011 } else if (_right[arc] == INVALID) {
2012 _parent[_left[arc]] = _parent[arc];
2013 if (_parent[arc] != INVALID) {
2014 if (_left[_parent[arc]] == arc) {
2015 _left[_parent[arc]] = _left[arc];
2017 _right[_parent[arc]] = _left[arc];
2020 _head[_g.source(arc)] = _left[arc];
2024 if (_right[e] != INVALID) {
2026 while (_right[e] != INVALID) {
2030 _right[_parent[e]] = _left[e];
2031 if (_left[e] != INVALID) {
2032 _parent[_left[e]] = _parent[e];
2035 _left[e] = _left[arc];
2036 _parent[_left[arc]] = e;
2037 _right[e] = _right[arc];
2038 _parent[_right[arc]] = e;
2040 _parent[e] = _parent[arc];
2041 if (_parent[arc] != INVALID) {
2042 if (_left[_parent[arc]] == arc) {
2043 _left[_parent[arc]] = e;
2045 _right[_parent[arc]] = e;
2050 _right[e] = _right[arc];
2051 _parent[_right[arc]] = e;
2052 _parent[e] = _parent[arc];
2054 if (_parent[arc] != INVALID) {
2055 if (_left[_parent[arc]] == arc) {
2056 _left[_parent[arc]] = e;
2058 _right[_parent[arc]] = e;
2061 _head[_g.source(arc)] = e;
2067 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2072 Arc left = refreshRec(v,a,m-1);
2076 _left[me] = INVALID;
2079 Arc right = refreshRec(v,m+1,b);
2081 _parent[right] = me;
2083 _right[me] = INVALID;
2089 for(NodeIt n(_g);n!=INVALID;++n) {
2091 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
2093 std::sort(v.begin(),v.end(),ArcLess(_g));
2094 Arc head = refreshRec(v,0,v.size()-1);
2096 _parent[head] = INVALID;
2098 else _head[n] = INVALID;
2104 _parent[v] = _parent[w];
2106 _left[w] = _right[v];
2108 if (_parent[v] != INVALID) {
2109 if (_right[_parent[v]] == w) {
2110 _right[_parent[v]] = v;
2112 _left[_parent[v]] = v;
2115 if (_left[w] != INVALID){
2116 _parent[_left[w]] = w;
2122 _parent[v] = _parent[w];
2124 _right[w] = _left[v];
2126 if (_parent[v] != INVALID){
2127 if (_left[_parent[v]] == w) {
2128 _left[_parent[v]] = v;
2130 _right[_parent[v]] = v;
2133 if (_right[w] != INVALID){
2134 _parent[_right[w]] = w;
2139 while (_parent[v] != INVALID) {
2140 if (v == _left[_parent[v]]) {
2141 if (_parent[_parent[v]] == INVALID) {
2144 if (_parent[v] == _left[_parent[_parent[v]]]) {
2153 if (_parent[_parent[v]] == INVALID) {
2156 if (_parent[v] == _left[_parent[_parent[v]]]) {
2166 _head[_g.source(v)] = v;
2172 ///Find an arc between two nodes.
2174 ///Find an arc between two nodes.
2175 ///\param s The source node.
2176 ///\param t The target node.
2177 ///\param p The previous arc between \c s and \c t. It it is INVALID or
2178 ///not given, the operator finds the first appropriate arc.
2179 ///\return An arc from \c s to \c t after \c p or
2180 ///\ref INVALID if there is no more.
2182 ///For example, you can count the number of arcs from \c u to \c v in the
2185 ///DynArcLookUp<ListDigraph> ae(g);
2188 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
2191 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
2192 ///amortized time, specifically, the time complexity of the lookups
2193 ///is equal to the optimal search tree implementation for the
2194 ///current query distribution in a constant factor.
2196 ///\note This is a dynamic data structure, therefore the data
2197 ///structure is updated after each graph alteration. Thus although
2198 ///this data structure is theoretically faster than \ref ArcLookUp
2199 ///and \ref AllArcLookUp, it often provides worse performance than
2201 Arc operator()(Node s, Node t, Arc p = INVALID) const {
2204 if (a == INVALID) return INVALID;
2207 if (_g.target(a) < t) {
2208 if (_right[a] == INVALID) {
2209 const_cast<DynArcLookUp&>(*this).splay(a);
2215 if (_g.target(a) == t) {
2218 if (_left[a] == INVALID) {
2219 const_cast<DynArcLookUp&>(*this).splay(a);
2228 if (_right[a] != INVALID) {
2230 while (_left[a] != INVALID) {
2233 const_cast<DynArcLookUp&>(*this).splay(a);
2235 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
2238 if (_parent[a] == INVALID) {
2242 const_cast<DynArcLookUp&>(*this).splay(a);
2245 if (_g.target(a) == t) return a;
2246 else return INVALID;
2252 ///Fast arc look-up between given endpoints.
2254 ///Using this class, you can find an arc in a digraph from a given
2255 ///source to a given target in time <em>O</em>(log<em>d</em>),
2256 ///where <em>d</em> is the out-degree of the source node.
2258 ///It is not possible to find \e all parallel arcs between two nodes.
2259 ///Use \ref AllArcLookUp for this purpose.
2261 ///\warning This class is static, so you should call refresh() (or at
2262 ///least refresh(Node)) to refresh this data structure whenever the
2263 ///digraph changes. This is a time consuming (superlinearly proportional
2264 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
2266 ///\tparam GR The type of the underlying digraph.
2273 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
2277 /// The Digraph type
2282 typename Digraph::template NodeMap<Arc> _head;
2283 typename Digraph::template ArcMap<Arc> _left;
2284 typename Digraph::template ArcMap<Arc> _right;
2289 ArcLess(const Digraph &_g) : g(_g) {}
2290 bool operator()(Arc a,Arc b) const
2292 return g.target(a)<g.target(b);
2302 ///It builds up the search database, which remains valid until the digraph
2304 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2307 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2311 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
2312 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
2316 ///Refresh the search data structure at a node.
2318 ///Build up the search database of node \c n.
2320 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
2321 ///is the number of the outgoing arcs of \c n.
2322 void refresh(Node n)
2325 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2327 std::sort(v.begin(),v.end(),ArcLess(_g));
2328 _head[n]=refreshRec(v,0,v.size()-1);
2330 else _head[n]=INVALID;
2332 ///Refresh the full data structure.
2334 ///Build up the full search database. In fact, it simply calls
2335 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2337 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
2338 ///the number of the arcs in the digraph and <em>D</em> is the maximum
2339 ///out-degree of the digraph.
2342 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2345 ///Find an arc between two nodes.
2347 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
2348 ///where <em>d</em> is the number of outgoing arcs of \c s.
2349 ///\param s The source node.
2350 ///\param t The target node.
2351 ///\return An arc from \c s to \c t if there exists,
2352 ///\ref INVALID otherwise.
2354 ///\warning If you change the digraph, refresh() must be called before using
2355 ///this operator. If you change the outgoing arcs of
2356 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
2357 Arc operator()(Node s, Node t) const
2361 e!=INVALID&&_g.target(e)!=t;
2362 e = t < _g.target(e)?_left[e]:_right[e]) ;
2368 ///Fast look-up of all arcs between given endpoints.
2370 ///This class is the same as \ref ArcLookUp, with the addition
2371 ///that it makes it possible to find all parallel arcs between given
2374 ///\warning This class is static, so you should call refresh() (or at
2375 ///least refresh(Node)) to refresh this data structure whenever the
2376 ///digraph changes. This is a time consuming (superlinearly proportional
2377 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
2379 ///\tparam GR The type of the underlying digraph.
2384 class AllArcLookUp : public ArcLookUp<GR>
2386 using ArcLookUp<GR>::_g;
2387 using ArcLookUp<GR>::_right;
2388 using ArcLookUp<GR>::_left;
2389 using ArcLookUp<GR>::_head;
2391 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
2393 typename GR::template ArcMap<Arc> _next;
2395 Arc refreshNext(Arc head,Arc next=INVALID)
2397 if(head==INVALID) return next;
2399 next=refreshNext(_right[head],next);
2400 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2402 return refreshNext(_left[head],head);
2408 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2413 /// The Digraph type
2420 ///It builds up the search database, which remains valid until the digraph
2422 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
2424 ///Refresh the data structure at a node.
2426 ///Build up the search database of node \c n.
2428 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
2429 ///the number of the outgoing arcs of \c n.
2430 void refresh(Node n)
2432 ArcLookUp<GR>::refresh(n);
2433 refreshNext(_head[n]);
2436 ///Refresh the full data structure.
2438 ///Build up the full search database. In fact, it simply calls
2439 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2441 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
2442 ///the number of the arcs in the digraph and <em>D</em> is the maximum
2443 ///out-degree of the digraph.
2446 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2449 ///Find an arc between two nodes.
2451 ///Find an arc between two nodes.
2452 ///\param s The source node.
2453 ///\param t The target node.
2454 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
2455 ///not given, the operator finds the first appropriate arc.
2456 ///\return An arc from \c s to \c t after \c prev or
2457 ///\ref INVALID if there is no more.
2459 ///For example, you can count the number of arcs from \c u to \c v in the
2462 ///AllArcLookUp<ListDigraph> ae(g);
2465 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
2468 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
2469 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
2470 ///consecutive arcs are found in constant time.
2472 ///\warning If you change the digraph, refresh() must be called before using
2473 ///this operator. If you change the outgoing arcs of
2474 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
2476 Arc operator()(Node s, Node t, Arc prev=INVALID) const
2483 e!=INVALID&&_g.target(e)!=t;
2484 e = t < _g.target(e)?_left[e]:_right[e]) ;
2494 else return _next[prev];