1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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
19 #ifndef LEMON_ADAPTORS_H
20 #define LEMON_ADAPTORS_H
22 /// \ingroup graph_adaptors
24 /// \brief Several graph adaptors
26 /// This file contains several useful adaptors for digraphs and graphs.
28 #include <lemon/core.h>
29 #include <lemon/maps.h>
30 #include <lemon/bits/variant.h>
32 #include <lemon/bits/graph_adaptor_extender.h>
33 #include <lemon/tolerance.h>
39 template<typename _Digraph>
40 class DigraphAdaptorBase {
42 typedef _Digraph Digraph;
43 typedef DigraphAdaptorBase Adaptor;
44 typedef Digraph ParentDigraph;
48 DigraphAdaptorBase() : _digraph(0) { }
49 void setDigraph(Digraph& digraph) { _digraph = &digraph; }
52 DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
54 typedef typename Digraph::Node Node;
55 typedef typename Digraph::Arc Arc;
57 void first(Node& i) const { _digraph->first(i); }
58 void first(Arc& i) const { _digraph->first(i); }
59 void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
60 void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
62 void next(Node& i) const { _digraph->next(i); }
63 void next(Arc& i) const { _digraph->next(i); }
64 void nextIn(Arc& i) const { _digraph->nextIn(i); }
65 void nextOut(Arc& i) const { _digraph->nextOut(i); }
67 Node source(const Arc& a) const { return _digraph->source(a); }
68 Node target(const Arc& a) const { return _digraph->target(a); }
70 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
71 int nodeNum() const { return _digraph->nodeNum(); }
73 typedef EdgeNumTagIndicator<Digraph> EdgeNumTag;
74 int arcNum() const { return _digraph->arcNum(); }
76 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag;
77 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) {
78 return _digraph->findArc(u, v, prev);
81 Node addNode() { return _digraph->addNode(); }
82 Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
84 void erase(const Node& n) const { _digraph->erase(n); }
85 void erase(const Arc& a) const { _digraph->erase(a); }
87 void clear() const { _digraph->clear(); }
89 int id(const Node& n) const { return _digraph->id(n); }
90 int id(const Arc& a) const { return _digraph->id(a); }
92 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
93 Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
95 int maxNodeId() const { return _digraph->maxNodeId(); }
96 int maxArcId() const { return _digraph->maxArcId(); }
98 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
99 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
101 typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier;
102 ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
104 template <typename _Value>
105 class NodeMap : public Digraph::template NodeMap<_Value> {
108 typedef typename Digraph::template NodeMap<_Value> Parent;
110 explicit NodeMap(const Adaptor& adaptor)
111 : Parent(*adaptor._digraph) {}
113 NodeMap(const Adaptor& adaptor, const _Value& value)
114 : Parent(*adaptor._digraph, value) { }
117 NodeMap& operator=(const NodeMap& cmap) {
118 return operator=<NodeMap>(cmap);
121 template <typename CMap>
122 NodeMap& operator=(const CMap& cmap) {
123 Parent::operator=(cmap);
129 template <typename _Value>
130 class ArcMap : public Digraph::template ArcMap<_Value> {
133 typedef typename Digraph::template ArcMap<_Value> Parent;
135 explicit ArcMap(const Adaptor& adaptor)
136 : Parent(*adaptor._digraph) {}
138 ArcMap(const Adaptor& adaptor, const _Value& value)
139 : Parent(*adaptor._digraph, value) {}
142 ArcMap& operator=(const ArcMap& cmap) {
143 return operator=<ArcMap>(cmap);
146 template <typename CMap>
147 ArcMap& operator=(const CMap& cmap) {
148 Parent::operator=(cmap);
156 template<typename _Graph>
157 class GraphAdaptorBase {
159 typedef _Graph Graph;
160 typedef Graph ParentGraph;
165 GraphAdaptorBase() : _graph(0) {}
167 void setGraph(Graph& graph) { _graph = &graph; }
170 GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
172 typedef typename Graph::Node Node;
173 typedef typename Graph::Arc Arc;
174 typedef typename Graph::Edge Edge;
176 void first(Node& i) const { _graph->first(i); }
177 void first(Arc& i) const { _graph->first(i); }
178 void first(Edge& i) const { _graph->first(i); }
179 void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
180 void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
181 void firstInc(Edge &i, bool &d, const Node &n) const {
182 _graph->firstInc(i, d, n);
185 void next(Node& i) const { _graph->next(i); }
186 void next(Arc& i) const { _graph->next(i); }
187 void next(Edge& i) const { _graph->next(i); }
188 void nextIn(Arc& i) const { _graph->nextIn(i); }
189 void nextOut(Arc& i) const { _graph->nextOut(i); }
190 void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
192 Node u(const Edge& e) const { return _graph->u(e); }
193 Node v(const Edge& e) const { return _graph->v(e); }
195 Node source(const Arc& a) const { return _graph->source(a); }
196 Node target(const Arc& a) const { return _graph->target(a); }
198 typedef NodeNumTagIndicator<Graph> NodeNumTag;
199 int nodeNum() const { return _graph->nodeNum(); }
201 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
202 int arcNum() const { return _graph->arcNum(); }
203 int edgeNum() const { return _graph->edgeNum(); }
205 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
206 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) {
207 return _graph->findArc(u, v, prev);
209 Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) {
210 return _graph->findEdge(u, v, prev);
213 Node addNode() { return _graph->addNode(); }
214 Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
216 void erase(const Node& i) { _graph->erase(i); }
217 void erase(const Edge& i) { _graph->erase(i); }
219 void clear() { _graph->clear(); }
221 bool direction(const Arc& a) const { return _graph->direction(a); }
222 Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
224 int id(const Node& v) const { return _graph->id(v); }
225 int id(const Arc& a) const { return _graph->id(a); }
226 int id(const Edge& e) const { return _graph->id(e); }
228 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
229 Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
230 Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
232 int maxNodeId() const { return _graph->maxNodeId(); }
233 int maxArcId() const { return _graph->maxArcId(); }
234 int maxEdgeId() const { return _graph->maxEdgeId(); }
236 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
237 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
239 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
240 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
242 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
243 EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
245 template <typename _Value>
246 class NodeMap : public Graph::template NodeMap<_Value> {
248 typedef typename Graph::template NodeMap<_Value> Parent;
249 explicit NodeMap(const GraphAdaptorBase<Graph>& adapter)
250 : Parent(*adapter._graph) {}
251 NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
252 : Parent(*adapter._graph, value) {}
255 NodeMap& operator=(const NodeMap& cmap) {
256 return operator=<NodeMap>(cmap);
259 template <typename CMap>
260 NodeMap& operator=(const CMap& cmap) {
261 Parent::operator=(cmap);
267 template <typename _Value>
268 class ArcMap : public Graph::template ArcMap<_Value> {
270 typedef typename Graph::template ArcMap<_Value> Parent;
271 explicit ArcMap(const GraphAdaptorBase<Graph>& adapter)
272 : Parent(*adapter._graph) {}
273 ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
274 : Parent(*adapter._graph, value) {}
277 ArcMap& operator=(const ArcMap& cmap) {
278 return operator=<ArcMap>(cmap);
281 template <typename CMap>
282 ArcMap& operator=(const CMap& cmap) {
283 Parent::operator=(cmap);
288 template <typename _Value>
289 class EdgeMap : public Graph::template EdgeMap<_Value> {
291 typedef typename Graph::template EdgeMap<_Value> Parent;
292 explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter)
293 : Parent(*adapter._graph) {}
294 EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
295 : Parent(*adapter._graph, value) {}
298 EdgeMap& operator=(const EdgeMap& cmap) {
299 return operator=<EdgeMap>(cmap);
302 template <typename CMap>
303 EdgeMap& operator=(const CMap& cmap) {
304 Parent::operator=(cmap);
311 template <typename _Digraph>
312 class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> {
314 typedef _Digraph Digraph;
315 typedef DigraphAdaptorBase<_Digraph> Parent;
317 ReverseDigraphBase() : Parent() { }
319 typedef typename Parent::Node Node;
320 typedef typename Parent::Arc Arc;
322 void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
323 void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
325 void nextIn(Arc& a) const { Parent::nextOut(a); }
326 void nextOut(Arc& a) const { Parent::nextIn(a); }
328 Node source(const Arc& a) const { return Parent::target(a); }
329 Node target(const Arc& a) const { return Parent::source(a); }
331 Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
333 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag;
334 Arc findArc(const Node& u, const Node& v,
335 const Arc& prev = INVALID) {
336 return Parent::findArc(v, u, prev);
341 /// \ingroup graph_adaptors
343 /// \brief A digraph adaptor which reverses the orientation of the arcs.
345 /// ReverseDigraph reverses the arcs in the adapted digraph. The
346 /// SubDigraph is conform to the \ref concepts::Digraph
347 /// "Digraph concept".
349 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
350 /// "Digraph concept". The type can be specified to be const.
351 template<typename _Digraph>
352 class ReverseDigraph :
353 public DigraphAdaptorExtender<ReverseDigraphBase<_Digraph> > {
355 typedef _Digraph Digraph;
356 typedef DigraphAdaptorExtender<
357 ReverseDigraphBase<_Digraph> > Parent;
362 /// \brief Constructor
364 /// Creates a reverse digraph adaptor for the given digraph
365 explicit ReverseDigraph(Digraph& digraph) {
366 Parent::setDigraph(digraph);
370 /// \brief Just gives back a reverse digraph adaptor
372 /// Just gives back a reverse digraph adaptor
373 template<typename Digraph>
374 ReverseDigraph<const Digraph> reverseDigraph(const Digraph& digraph) {
375 return ReverseDigraph<const Digraph>(digraph);
378 template <typename _Digraph, typename _NodeFilterMap,
379 typename _ArcFilterMap, bool _checked = true>
380 class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
382 typedef _Digraph Digraph;
383 typedef _NodeFilterMap NodeFilterMap;
384 typedef _ArcFilterMap ArcFilterMap;
386 typedef SubDigraphBase Adaptor;
387 typedef DigraphAdaptorBase<_Digraph> Parent;
389 NodeFilterMap* _node_filter;
390 ArcFilterMap* _arc_filter;
392 : Parent(), _node_filter(0), _arc_filter(0) { }
394 void setNodeFilterMap(NodeFilterMap& node_filter) {
395 _node_filter = &node_filter;
397 void setArcFilterMap(ArcFilterMap& arc_filter) {
398 _arc_filter = &arc_filter;
403 typedef typename Parent::Node Node;
404 typedef typename Parent::Arc Arc;
406 void first(Node& i) const {
408 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
411 void first(Arc& i) const {
413 while (i != INVALID && (!(*_arc_filter)[i]
414 || !(*_node_filter)[Parent::source(i)]
415 || !(*_node_filter)[Parent::target(i)]))
419 void firstIn(Arc& i, const Node& n) const {
420 Parent::firstIn(i, n);
421 while (i != INVALID && (!(*_arc_filter)[i]
422 || !(*_node_filter)[Parent::source(i)]))
426 void firstOut(Arc& i, const Node& n) const {
427 Parent::firstOut(i, n);
428 while (i != INVALID && (!(*_arc_filter)[i]
429 || !(*_node_filter)[Parent::target(i)]))
433 void next(Node& i) const {
435 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
438 void next(Arc& i) const {
440 while (i != INVALID && (!(*_arc_filter)[i]
441 || !(*_node_filter)[Parent::source(i)]
442 || !(*_node_filter)[Parent::target(i)]))
446 void nextIn(Arc& i) const {
448 while (i != INVALID && (!(*_arc_filter)[i]
449 || !(*_node_filter)[Parent::source(i)]))
453 void nextOut(Arc& i) const {
455 while (i != INVALID && (!(*_arc_filter)[i]
456 || !(*_node_filter)[Parent::target(i)]))
460 void hide(const Node& n) const { _node_filter->set(n, false); }
461 void hide(const Arc& a) const { _arc_filter->set(a, false); }
463 void unHide(const Node& n) const { _node_filter->set(n, true); }
464 void unHide(const Arc& a) const { _arc_filter->set(a, true); }
466 bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
467 bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; }
469 typedef False NodeNumTag;
470 typedef False EdgeNumTag;
472 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag;
473 Arc findArc(const Node& source, const Node& target,
474 const Arc& prev = INVALID) {
475 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
478 Arc arc = Parent::findArc(source, target, prev);
479 while (arc != INVALID && !(*_arc_filter)[arc]) {
480 arc = Parent::findArc(source, target, arc);
485 template <typename _Value>
486 class NodeMap : public SubMapExtender<Adaptor,
487 typename Parent::template NodeMap<_Value> > {
489 typedef _Value Value;
490 typedef SubMapExtender<Adaptor, typename Parent::
491 template NodeMap<Value> > MapParent;
493 NodeMap(const Adaptor& adaptor)
494 : MapParent(adaptor) {}
495 NodeMap(const Adaptor& adaptor, const Value& value)
496 : MapParent(adaptor, value) {}
499 NodeMap& operator=(const NodeMap& cmap) {
500 return operator=<NodeMap>(cmap);
503 template <typename CMap>
504 NodeMap& operator=(const CMap& cmap) {
505 MapParent::operator=(cmap);
510 template <typename _Value>
511 class ArcMap : public SubMapExtender<Adaptor,
512 typename Parent::template ArcMap<_Value> > {
514 typedef _Value Value;
515 typedef SubMapExtender<Adaptor, typename Parent::
516 template ArcMap<Value> > MapParent;
518 ArcMap(const Adaptor& adaptor)
519 : MapParent(adaptor) {}
520 ArcMap(const Adaptor& adaptor, const Value& value)
521 : MapParent(adaptor, value) {}
524 ArcMap& operator=(const ArcMap& cmap) {
525 return operator=<ArcMap>(cmap);
528 template <typename CMap>
529 ArcMap& operator=(const CMap& cmap) {
530 MapParent::operator=(cmap);
537 template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap>
538 class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false>
539 : public DigraphAdaptorBase<_Digraph> {
541 typedef _Digraph Digraph;
542 typedef _NodeFilterMap NodeFilterMap;
543 typedef _ArcFilterMap ArcFilterMap;
545 typedef SubDigraphBase Adaptor;
546 typedef DigraphAdaptorBase<Digraph> Parent;
548 NodeFilterMap* _node_filter;
549 ArcFilterMap* _arc_filter;
551 : Parent(), _node_filter(0), _arc_filter(0) { }
553 void setNodeFilterMap(NodeFilterMap& node_filter) {
554 _node_filter = &node_filter;
556 void setArcFilterMap(ArcFilterMap& arc_filter) {
557 _arc_filter = &arc_filter;
562 typedef typename Parent::Node Node;
563 typedef typename Parent::Arc Arc;
565 void first(Node& i) const {
567 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
570 void first(Arc& i) const {
572 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
575 void firstIn(Arc& i, const Node& n) const {
576 Parent::firstIn(i, n);
577 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
580 void firstOut(Arc& i, const Node& n) const {
581 Parent::firstOut(i, n);
582 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
585 void next(Node& i) const {
587 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
589 void next(Arc& i) const {
591 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
593 void nextIn(Arc& i) const {
595 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
598 void nextOut(Arc& i) const {
600 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
603 void hide(const Node& n) const { _node_filter->set(n, false); }
604 void hide(const Arc& e) const { _arc_filter->set(e, false); }
606 void unHide(const Node& n) const { _node_filter->set(n, true); }
607 void unHide(const Arc& e) const { _arc_filter->set(e, true); }
609 bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
610 bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; }
612 typedef False NodeNumTag;
613 typedef False EdgeNumTag;
615 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag;
616 Arc findArc(const Node& source, const Node& target,
617 const Arc& prev = INVALID) {
618 if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
621 Arc arc = Parent::findArc(source, target, prev);
622 while (arc != INVALID && !(*_arc_filter)[arc]) {
623 arc = Parent::findArc(source, target, arc);
628 template <typename _Value>
629 class NodeMap : public SubMapExtender<Adaptor,
630 typename Parent::template NodeMap<_Value> > {
632 typedef _Value Value;
633 typedef SubMapExtender<Adaptor, typename Parent::
634 template NodeMap<Value> > MapParent;
636 NodeMap(const Adaptor& adaptor)
637 : MapParent(adaptor) {}
638 NodeMap(const Adaptor& adaptor, const Value& value)
639 : MapParent(adaptor, value) {}
642 NodeMap& operator=(const NodeMap& cmap) {
643 return operator=<NodeMap>(cmap);
646 template <typename CMap>
647 NodeMap& operator=(const CMap& cmap) {
648 MapParent::operator=(cmap);
653 template <typename _Value>
654 class ArcMap : public SubMapExtender<Adaptor,
655 typename Parent::template ArcMap<_Value> > {
657 typedef _Value Value;
658 typedef SubMapExtender<Adaptor, typename Parent::
659 template ArcMap<Value> > MapParent;
661 ArcMap(const Adaptor& adaptor)
662 : MapParent(adaptor) {}
663 ArcMap(const Adaptor& adaptor, const Value& value)
664 : MapParent(adaptor, value) {}
667 ArcMap& operator=(const ArcMap& cmap) {
668 return operator=<ArcMap>(cmap);
671 template <typename CMap>
672 ArcMap& operator=(const CMap& cmap) {
673 MapParent::operator=(cmap);
680 /// \ingroup graph_adaptors
682 /// \brief An adaptor for hiding nodes and arcs in a digraph
684 /// SubDigraph hides nodes and arcs in a digraph. A bool node map
685 /// and a bool arc map must be specified, which define the filters
686 /// for nodes and arcs. Just the nodes and arcs with true value are
687 /// shown in the subdigraph. The SubDigraph is conform to the \ref
688 /// concepts::Digraph "Digraph concept". If the \c _checked parameter
689 /// is true, then the arcs incident to filtered nodes are also
692 /// \tparam _Digraph It must be conform to the \ref
693 /// concepts::Digraph "Digraph concept". The type can be specified
695 /// \tparam _NodeFilterMap A bool valued node map of the the adapted digraph.
696 /// \tparam _ArcFilterMap A bool valued arc map of the the adapted digraph.
697 /// \tparam _checked If the parameter is false then the arc filtering
698 /// is not checked with respect to node filter. Otherwise, each arc
699 /// is automatically filtered, which is incident to a filtered node.
703 template<typename _Digraph,
704 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
705 typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>,
706 bool _checked = true>
708 : public DigraphAdaptorExtender<
709 SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, _checked> > {
711 typedef _Digraph Digraph;
712 typedef _NodeFilterMap NodeFilterMap;
713 typedef _ArcFilterMap ArcFilterMap;
715 typedef DigraphAdaptorExtender<
716 SubDigraphBase<Digraph, NodeFilterMap, ArcFilterMap, _checked> >
719 typedef typename Parent::Node Node;
720 typedef typename Parent::Arc Arc;
726 /// \brief Constructor
728 /// Creates a subdigraph for the given digraph with
729 /// given node and arc map filters.
730 SubDigraph(Digraph& digraph, NodeFilterMap& node_filter,
731 ArcFilterMap& arc_filter) {
733 setNodeFilterMap(node_filter);
734 setArcFilterMap(arc_filter);
737 /// \brief Hides the node of the graph
739 /// This function hides \c n in the digraph, i.e. the iteration
740 /// jumps over it. This is done by simply setting the value of \c n
741 /// to be false in the corresponding node-map.
742 void hide(const Node& n) const { Parent::hide(n); }
744 /// \brief Hides the arc of the graph
746 /// This function hides \c a in the digraph, i.e. the iteration
747 /// jumps over it. This is done by simply setting the value of \c a
748 /// to be false in the corresponding arc-map.
749 void hide(const Arc& a) const { Parent::hide(a); }
751 /// \brief Unhides the node of the graph
753 /// The value of \c n is set to be true in the node-map which stores
754 /// hide information. If \c n was hidden previuosly, then it is shown
756 void unHide(const Node& n) const { Parent::unHide(n); }
758 /// \brief Unhides the arc of the graph
760 /// The value of \c a is set to be true in the arc-map which stores
761 /// hide information. If \c a was hidden previuosly, then it is shown
763 void unHide(const Arc& a) const { Parent::unHide(a); }
765 /// \brief Returns true if \c n is hidden.
767 /// Returns true if \c n is hidden.
769 bool hidden(const Node& n) const { return Parent::hidden(n); }
771 /// \brief Returns true if \c a is hidden.
773 /// Returns true if \c a is hidden.
775 bool hidden(const Arc& a) const { return Parent::hidden(a); }
779 /// \brief Just gives back a subdigraph
781 /// Just gives back a subdigraph
782 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
783 SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap>
784 subDigraph(const Digraph& digraph, NodeFilterMap& nfm, ArcFilterMap& afm) {
785 return SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap>
789 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
790 SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap>
791 subDigraph(const Digraph& digraph,
792 const NodeFilterMap& nfm, ArcFilterMap& afm) {
793 return SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap>
797 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
798 SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap>
799 subDigraph(const Digraph& digraph,
800 NodeFilterMap& nfm, const ArcFilterMap& afm) {
801 return SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap>
805 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap>
806 SubDigraph<const Digraph, const NodeFilterMap, const ArcFilterMap>
807 subDigraph(const Digraph& digraph,
808 const NodeFilterMap& nfm, const ArcFilterMap& afm) {
809 return SubDigraph<const Digraph, const NodeFilterMap,
810 const ArcFilterMap>(digraph, nfm, afm);
814 template <typename _Graph, typename NodeFilterMap,
815 typename EdgeFilterMap, bool _checked = true>
816 class SubGraphBase : public GraphAdaptorBase<_Graph> {
818 typedef _Graph Graph;
819 typedef SubGraphBase Adaptor;
820 typedef GraphAdaptorBase<_Graph> Parent;
823 NodeFilterMap* _node_filter_map;
824 EdgeFilterMap* _edge_filter_map;
827 : Parent(), _node_filter_map(0), _edge_filter_map(0) { }
829 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
830 _node_filter_map=&node_filter_map;
832 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
833 _edge_filter_map=&edge_filter_map;
838 typedef typename Parent::Node Node;
839 typedef typename Parent::Arc Arc;
840 typedef typename Parent::Edge Edge;
842 void first(Node& i) const {
844 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
847 void first(Arc& i) const {
849 while (i!=INVALID && (!(*_edge_filter_map)[i]
850 || !(*_node_filter_map)[Parent::source(i)]
851 || !(*_node_filter_map)[Parent::target(i)]))
855 void first(Edge& i) const {
857 while (i!=INVALID && (!(*_edge_filter_map)[i]
858 || !(*_node_filter_map)[Parent::u(i)]
859 || !(*_node_filter_map)[Parent::v(i)]))
863 void firstIn(Arc& i, const Node& n) const {
864 Parent::firstIn(i, n);
865 while (i!=INVALID && (!(*_edge_filter_map)[i]
866 || !(*_node_filter_map)[Parent::source(i)]))
870 void firstOut(Arc& i, const Node& n) const {
871 Parent::firstOut(i, n);
872 while (i!=INVALID && (!(*_edge_filter_map)[i]
873 || !(*_node_filter_map)[Parent::target(i)]))
877 void firstInc(Edge& i, bool& d, const Node& n) const {
878 Parent::firstInc(i, d, n);
879 while (i!=INVALID && (!(*_edge_filter_map)[i]
880 || !(*_node_filter_map)[Parent::u(i)]
881 || !(*_node_filter_map)[Parent::v(i)]))
882 Parent::nextInc(i, d);
885 void next(Node& i) const {
887 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
890 void next(Arc& i) const {
892 while (i!=INVALID && (!(*_edge_filter_map)[i]
893 || !(*_node_filter_map)[Parent::source(i)]
894 || !(*_node_filter_map)[Parent::target(i)]))
898 void next(Edge& i) const {
900 while (i!=INVALID && (!(*_edge_filter_map)[i]
901 || !(*_node_filter_map)[Parent::u(i)]
902 || !(*_node_filter_map)[Parent::v(i)]))
906 void nextIn(Arc& i) const {
908 while (i!=INVALID && (!(*_edge_filter_map)[i]
909 || !(*_node_filter_map)[Parent::source(i)]))
913 void nextOut(Arc& i) const {
915 while (i!=INVALID && (!(*_edge_filter_map)[i]
916 || !(*_node_filter_map)[Parent::target(i)]))
920 void nextInc(Edge& i, bool& d) const {
921 Parent::nextInc(i, d);
922 while (i!=INVALID && (!(*_edge_filter_map)[i]
923 || !(*_node_filter_map)[Parent::u(i)]
924 || !(*_node_filter_map)[Parent::v(i)]))
925 Parent::nextInc(i, d);
928 void hide(const Node& n) const { _node_filter_map->set(n, false); }
929 void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
931 void unHide(const Node& n) const { _node_filter_map->set(n, true); }
932 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
934 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
935 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
937 typedef False NodeNumTag;
938 typedef False EdgeNumTag;
940 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
941 Arc findArc(const Node& u, const Node& v,
942 const Arc& prev = INVALID) {
943 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
946 Arc arc = Parent::findArc(u, v, prev);
947 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
948 arc = Parent::findArc(u, v, arc);
952 Edge findEdge(const Node& u, const Node& v,
953 const Edge& prev = INVALID) {
954 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
957 Edge edge = Parent::findEdge(u, v, prev);
958 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
959 edge = Parent::findEdge(u, v, edge);
964 template <typename _Value>
965 class NodeMap : public SubMapExtender<Adaptor,
966 typename Parent::template NodeMap<_Value> > {
968 typedef _Value Value;
969 typedef SubMapExtender<Adaptor, typename Parent::
970 template NodeMap<Value> > MapParent;
972 NodeMap(const Adaptor& adaptor)
973 : MapParent(adaptor) {}
974 NodeMap(const Adaptor& adaptor, const Value& value)
975 : MapParent(adaptor, value) {}
978 NodeMap& operator=(const NodeMap& cmap) {
979 return operator=<NodeMap>(cmap);
982 template <typename CMap>
983 NodeMap& operator=(const CMap& cmap) {
984 MapParent::operator=(cmap);
989 template <typename _Value>
990 class ArcMap : public SubMapExtender<Adaptor,
991 typename Parent::template ArcMap<_Value> > {
993 typedef _Value Value;
994 typedef SubMapExtender<Adaptor, typename Parent::
995 template ArcMap<Value> > MapParent;
997 ArcMap(const Adaptor& adaptor)
998 : MapParent(adaptor) {}
999 ArcMap(const Adaptor& adaptor, const Value& value)
1000 : MapParent(adaptor, value) {}
1003 ArcMap& operator=(const ArcMap& cmap) {
1004 return operator=<ArcMap>(cmap);
1007 template <typename CMap>
1008 ArcMap& operator=(const CMap& cmap) {
1009 MapParent::operator=(cmap);
1014 template <typename _Value>
1015 class EdgeMap : public SubMapExtender<Adaptor,
1016 typename Parent::template EdgeMap<_Value> > {
1018 typedef _Value Value;
1019 typedef SubMapExtender<Adaptor, typename Parent::
1020 template EdgeMap<Value> > MapParent;
1022 EdgeMap(const Adaptor& adaptor)
1023 : MapParent(adaptor) {}
1025 EdgeMap(const Adaptor& adaptor, const Value& value)
1026 : MapParent(adaptor, value) {}
1029 EdgeMap& operator=(const EdgeMap& cmap) {
1030 return operator=<EdgeMap>(cmap);
1033 template <typename CMap>
1034 EdgeMap& operator=(const CMap& cmap) {
1035 MapParent::operator=(cmap);
1042 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
1043 class SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
1044 : public GraphAdaptorBase<_Graph> {
1046 typedef _Graph Graph;
1047 typedef SubGraphBase Adaptor;
1048 typedef GraphAdaptorBase<_Graph> Parent;
1050 NodeFilterMap* _node_filter_map;
1051 EdgeFilterMap* _edge_filter_map;
1052 SubGraphBase() : Parent(),
1053 _node_filter_map(0), _edge_filter_map(0) { }
1055 void setNodeFilterMap(NodeFilterMap& node_filter_map) {
1056 _node_filter_map=&node_filter_map;
1058 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
1059 _edge_filter_map=&edge_filter_map;
1064 typedef typename Parent::Node Node;
1065 typedef typename Parent::Arc Arc;
1066 typedef typename Parent::Edge Edge;
1068 void first(Node& i) const {
1070 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1073 void first(Arc& i) const {
1075 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1078 void first(Edge& i) const {
1080 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1083 void firstIn(Arc& i, const Node& n) const {
1084 Parent::firstIn(i, n);
1085 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1088 void firstOut(Arc& i, const Node& n) const {
1089 Parent::firstOut(i, n);
1090 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1093 void firstInc(Edge& i, bool& d, const Node& n) const {
1094 Parent::firstInc(i, d, n);
1095 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1098 void next(Node& i) const {
1100 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1102 void next(Arc& i) const {
1104 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1106 void next(Edge& i) const {
1108 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1110 void nextIn(Arc& i) const {
1112 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1115 void nextOut(Arc& i) const {
1117 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1119 void nextInc(Edge& i, bool& d) const {
1120 Parent::nextInc(i, d);
1121 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1124 void hide(const Node& n) const { _node_filter_map->set(n, false); }
1125 void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
1127 void unHide(const Node& n) const { _node_filter_map->set(n, true); }
1128 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
1130 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
1131 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
1133 typedef False NodeNumTag;
1134 typedef False EdgeNumTag;
1136 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1137 Arc findArc(const Node& u, const Node& v,
1138 const Arc& prev = INVALID) {
1139 Arc arc = Parent::findArc(u, v, prev);
1140 while (arc != INVALID && !(*_edge_filter_map)[arc]) {
1141 arc = Parent::findArc(u, v, arc);
1145 Edge findEdge(const Node& u, const Node& v,
1146 const Edge& prev = INVALID) {
1147 Edge edge = Parent::findEdge(u, v, prev);
1148 while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1149 edge = Parent::findEdge(u, v, edge);
1154 template <typename _Value>
1155 class NodeMap : public SubMapExtender<Adaptor,
1156 typename Parent::template NodeMap<_Value> > {
1158 typedef _Value Value;
1159 typedef SubMapExtender<Adaptor, typename Parent::
1160 template NodeMap<Value> > MapParent;
1162 NodeMap(const Adaptor& adaptor)
1163 : MapParent(adaptor) {}
1164 NodeMap(const Adaptor& adaptor, const Value& value)
1165 : MapParent(adaptor, value) {}
1168 NodeMap& operator=(const NodeMap& cmap) {
1169 return operator=<NodeMap>(cmap);
1172 template <typename CMap>
1173 NodeMap& operator=(const CMap& cmap) {
1174 MapParent::operator=(cmap);
1179 template <typename _Value>
1180 class ArcMap : public SubMapExtender<Adaptor,
1181 typename Parent::template ArcMap<_Value> > {
1183 typedef _Value Value;
1184 typedef SubMapExtender<Adaptor, typename Parent::
1185 template ArcMap<Value> > MapParent;
1187 ArcMap(const Adaptor& adaptor)
1188 : MapParent(adaptor) {}
1189 ArcMap(const Adaptor& adaptor, const Value& value)
1190 : MapParent(adaptor, value) {}
1193 ArcMap& operator=(const ArcMap& cmap) {
1194 return operator=<ArcMap>(cmap);
1197 template <typename CMap>
1198 ArcMap& operator=(const CMap& cmap) {
1199 MapParent::operator=(cmap);
1204 template <typename _Value>
1205 class EdgeMap : public SubMapExtender<Adaptor,
1206 typename Parent::template EdgeMap<_Value> > {
1208 typedef _Value Value;
1209 typedef SubMapExtender<Adaptor, typename Parent::
1210 template EdgeMap<Value> > MapParent;
1212 EdgeMap(const Adaptor& adaptor)
1213 : MapParent(adaptor) {}
1215 EdgeMap(const Adaptor& adaptor, const _Value& value)
1216 : MapParent(adaptor, value) {}
1219 EdgeMap& operator=(const EdgeMap& cmap) {
1220 return operator=<EdgeMap>(cmap);
1223 template <typename CMap>
1224 EdgeMap& operator=(const CMap& cmap) {
1225 MapParent::operator=(cmap);
1232 /// \ingroup graph_adaptors
1234 /// \brief A graph adaptor for hiding nodes and edges in an
1235 /// undirected graph.
1237 /// SubGraph hides nodes and edges in a graph. A bool node map and a
1238 /// bool edge map must be specified, which define the filters for
1239 /// nodes and edges. Just the nodes and edges with true value are
1240 /// shown in the subgraph. The SubGraph is conform to the \ref
1241 /// concepts::Graph "Graph concept". If the \c _checked parameter is
1242 /// true, then the edges incident to filtered nodes are also
1245 /// \tparam _Graph It must be conform to the \ref
1246 /// concepts::Graph "Graph concept". The type can be specified
1248 /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph.
1249 /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted graph.
1250 /// \tparam _checked If the parameter is false then the edge filtering
1251 /// is not checked with respect to node filter. Otherwise, each edge
1252 /// is automatically filtered, which is incident to a filtered node.
1254 /// \see FilterNodes
1255 /// \see FilterEdges
1256 template<typename _Graph, typename NodeFilterMap,
1257 typename EdgeFilterMap, bool _checked = true>
1259 : public GraphAdaptorExtender<
1260 SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, _checked> > {
1262 typedef _Graph Graph;
1263 typedef GraphAdaptorExtender<
1264 SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
1266 typedef typename Parent::Node Node;
1267 typedef typename Parent::Edge Edge;
1273 /// \brief Constructor
1275 /// Creates a subgraph for the given graph with given node and
1276 /// edge map filters.
1277 SubGraph(Graph& _graph, NodeFilterMap& node_filter_map,
1278 EdgeFilterMap& edge_filter_map) {
1280 setNodeFilterMap(node_filter_map);
1281 setEdgeFilterMap(edge_filter_map);
1284 /// \brief Hides the node of the graph
1286 /// This function hides \c n in the graph, i.e. the iteration
1287 /// jumps over it. This is done by simply setting the value of \c n
1288 /// to be false in the corresponding node-map.
1289 void hide(const Node& n) const { Parent::hide(n); }
1291 /// \brief Hides the edge of the graph
1293 /// This function hides \c e in the graph, i.e. the iteration
1294 /// jumps over it. This is done by simply setting the value of \c e
1295 /// to be false in the corresponding edge-map.
1296 void hide(const Edge& e) const { Parent::hide(e); }
1298 /// \brief Unhides the node of the graph
1300 /// The value of \c n is set to be true in the node-map which stores
1301 /// hide information. If \c n was hidden previuosly, then it is shown
1303 void unHide(const Node& n) const { Parent::unHide(n); }
1305 /// \brief Unhides the edge of the graph
1307 /// The value of \c e is set to be true in the edge-map which stores
1308 /// hide information. If \c e was hidden previuosly, then it is shown
1310 void unHide(const Edge& e) const { Parent::unHide(e); }
1312 /// \brief Returns true if \c n is hidden.
1314 /// Returns true if \c n is hidden.
1316 bool hidden(const Node& n) const { return Parent::hidden(n); }
1318 /// \brief Returns true if \c e is hidden.
1320 /// Returns true if \c e is hidden.
1322 bool hidden(const Edge& e) const { return Parent::hidden(e); }
1325 /// \brief Just gives back a subgraph
1327 /// Just gives back a subgraph
1328 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1329 SubGraph<const Graph, NodeFilterMap, ArcFilterMap>
1330 subGraph(const Graph& graph, NodeFilterMap& nfm, ArcFilterMap& efm) {
1331 return SubGraph<const Graph, NodeFilterMap, ArcFilterMap>(graph, nfm, efm);
1334 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1335 SubGraph<const Graph, const NodeFilterMap, ArcFilterMap>
1336 subGraph(const Graph& graph,
1337 const NodeFilterMap& nfm, ArcFilterMap& efm) {
1338 return SubGraph<const Graph, const NodeFilterMap, ArcFilterMap>
1342 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1343 SubGraph<const Graph, NodeFilterMap, const ArcFilterMap>
1344 subGraph(const Graph& graph,
1345 NodeFilterMap& nfm, const ArcFilterMap& efm) {
1346 return SubGraph<const Graph, NodeFilterMap, const ArcFilterMap>
1350 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap>
1351 SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap>
1352 subGraph(const Graph& graph,
1353 const NodeFilterMap& nfm, const ArcFilterMap& efm) {
1354 return SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap>
1358 /// \ingroup graph_adaptors
1360 /// \brief An adaptor for hiding nodes from a digraph or a graph.
1362 /// FilterNodes adaptor hides nodes in a graph or a digraph. A bool
1363 /// node map must be specified, which defines the filters for
1364 /// nodes. Just the unfiltered nodes and the arcs or edges incident
1365 /// to unfiltered nodes are shown in the subdigraph or subgraph. The
1366 /// FilterNodes is conform to the \ref concepts::Digraph
1367 /// "Digraph concept" or \ref concepts::Graph "Graph concept" depending
1368 /// on the \c _Digraph template parameter. If the \c _checked
1369 /// parameter is true, then the arc or edges incident to filtered nodes
1370 /// are also filtered out.
1372 /// \tparam _Digraph It must be conform to the \ref
1373 /// concepts::Digraph "Digraph concept" or \ref concepts::Graph
1374 /// "Graph concept". The type can be specified to be const.
1375 /// \tparam _NodeFilterMap A bool valued node map of the the adapted graph.
1376 /// \tparam _checked If the parameter is false then the arc or edge
1377 /// filtering is not checked with respect to node filter. In this
1378 /// case just isolated nodes can be filtered out from the
1381 template<typename _Digraph,
1382 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
1383 bool _checked = true>
1385 template<typename _Digraph,
1386 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>,
1387 bool _checked = true,
1388 typename Enable = void>
1391 : public SubDigraph<_Digraph, _NodeFilterMap,
1392 ConstMap<typename _Digraph::Arc, bool>, _checked> {
1395 typedef _Digraph Digraph;
1396 typedef _NodeFilterMap NodeFilterMap;
1398 typedef SubDigraph<Digraph, NodeFilterMap,
1399 ConstMap<typename Digraph::Arc, bool>, _checked>
1402 typedef typename Parent::Node Node;
1405 ConstMap<typename Digraph::Arc, bool> const_true_map;
1407 FilterNodes() : const_true_map(true) {
1408 Parent::setArcFilterMap(const_true_map);
1413 /// \brief Constructor
1415 /// Creates an adaptor for the given digraph or graph with
1416 /// given node filter map.
1417 FilterNodes(Digraph& _digraph, NodeFilterMap& node_filter) :
1418 Parent(), const_true_map(true) {
1419 Parent::setDigraph(_digraph);
1420 Parent::setNodeFilterMap(node_filter);
1421 Parent::setArcFilterMap(const_true_map);
1424 /// \brief Hides the node of the graph
1426 /// This function hides \c n in the digraph or graph, i.e. the iteration
1427 /// jumps over it. This is done by simply setting the value of \c n
1428 /// to be false in the corresponding node map.
1429 void hide(const Node& n) const { Parent::hide(n); }
1431 /// \brief Unhides the node of the graph
1433 /// The value of \c n is set to be true in the node-map which stores
1434 /// hide information. If \c n was hidden previuosly, then it is shown
1436 void unHide(const Node& n) const { Parent::unHide(n); }
1438 /// \brief Returns true if \c n is hidden.
1440 /// Returns true if \c n is hidden.
1442 bool hidden(const Node& n) const { return Parent::hidden(n); }
1446 template<typename _Graph, typename _NodeFilterMap, bool _checked>
1447 class FilterNodes<_Graph, _NodeFilterMap, _checked,
1448 typename enable_if<UndirectedTagIndicator<_Graph> >::type>
1449 : public SubGraph<_Graph, _NodeFilterMap,
1450 ConstMap<typename _Graph::Edge, bool>, _checked> {
1452 typedef _Graph Graph;
1453 typedef _NodeFilterMap NodeFilterMap;
1454 typedef SubGraph<Graph, NodeFilterMap,
1455 ConstMap<typename Graph::Edge, bool> > Parent;
1457 typedef typename Parent::Node Node;
1459 ConstMap<typename Graph::Edge, bool> const_true_map;
1461 FilterNodes() : const_true_map(true) {
1462 Parent::setEdgeFilterMap(const_true_map);
1467 FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) :
1468 Parent(), const_true_map(true) {
1469 Parent::setGraph(_graph);
1470 Parent::setNodeFilterMap(node_filter_map);
1471 Parent::setEdgeFilterMap(const_true_map);
1474 void hide(const Node& n) const { Parent::hide(n); }
1475 void unHide(const Node& n) const { Parent::unHide(n); }
1476 bool hidden(const Node& n) const { return Parent::hidden(n); }
1481 /// \brief Just gives back a FilterNodes adaptor
1483 /// Just gives back a FilterNodes adaptor
1484 template<typename Digraph, typename NodeFilterMap>
1485 FilterNodes<const Digraph, NodeFilterMap>
1486 filterNodes(const Digraph& digraph, NodeFilterMap& nfm) {
1487 return FilterNodes<const Digraph, NodeFilterMap>(digraph, nfm);
1490 template<typename Digraph, typename NodeFilterMap>
1491 FilterNodes<const Digraph, const NodeFilterMap>
1492 filterNodes(const Digraph& digraph, const NodeFilterMap& nfm) {
1493 return FilterNodes<const Digraph, const NodeFilterMap>(digraph, nfm);
1496 /// \ingroup graph_adaptors
1498 /// \brief An adaptor for hiding arcs from a digraph.
1500 /// FilterArcs adaptor hides arcs in a digraph. A bool arc map must
1501 /// be specified, which defines the filters for arcs. Just the
1502 /// unfiltered arcs are shown in the subdigraph. The FilterArcs is
1503 /// conform to the \ref concepts::Digraph "Digraph concept".
1505 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
1506 /// "Digraph concept". The type can be specified to be const.
1507 /// \tparam _ArcFilterMap A bool valued arc map of the the adapted
1509 template<typename _Digraph, typename _ArcFilterMap>
1511 public SubDigraph<_Digraph, ConstMap<typename _Digraph::Node, bool>,
1512 _ArcFilterMap, false> {
1514 typedef _Digraph Digraph;
1515 typedef _ArcFilterMap ArcFilterMap;
1517 typedef SubDigraph<Digraph, ConstMap<typename Digraph::Node, bool>,
1518 ArcFilterMap, false> Parent;
1520 typedef typename Parent::Arc Arc;
1523 ConstMap<typename Digraph::Node, bool> const_true_map;
1525 FilterArcs() : const_true_map(true) {
1526 Parent::setNodeFilterMap(const_true_map);
1531 /// \brief Constructor
1533 /// Creates a FilterArcs adaptor for the given graph with
1534 /// given arc map filter.
1535 FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter)
1536 : Parent(), const_true_map(true) {
1537 Parent::setDigraph(digraph);
1538 Parent::setNodeFilterMap(const_true_map);
1539 Parent::setArcFilterMap(arc_filter);
1542 /// \brief Hides the arc of the graph
1544 /// This function hides \c a in the graph, i.e. the iteration
1545 /// jumps over it. This is done by simply setting the value of \c a
1546 /// to be false in the corresponding arc map.
1547 void hide(const Arc& a) const { Parent::hide(a); }
1549 /// \brief Unhides the arc of the graph
1551 /// The value of \c a is set to be true in the arc-map which stores
1552 /// hide information. If \c a was hidden previuosly, then it is shown
1554 void unHide(const Arc& a) const { Parent::unHide(a); }
1556 /// \brief Returns true if \c a is hidden.
1558 /// Returns true if \c a is hidden.
1560 bool hidden(const Arc& a) const { return Parent::hidden(a); }
1564 /// \brief Just gives back an FilterArcs adaptor
1566 /// Just gives back an FilterArcs adaptor
1567 template<typename Digraph, typename ArcFilterMap>
1568 FilterArcs<const Digraph, ArcFilterMap>
1569 filterArcs(const Digraph& digraph, ArcFilterMap& afm) {
1570 return FilterArcs<const Digraph, ArcFilterMap>(digraph, afm);
1573 template<typename Digraph, typename ArcFilterMap>
1574 FilterArcs<const Digraph, const ArcFilterMap>
1575 filterArcs(const Digraph& digraph, const ArcFilterMap& afm) {
1576 return FilterArcs<const Digraph, const ArcFilterMap>(digraph, afm);
1579 /// \ingroup graph_adaptors
1581 /// \brief An adaptor for hiding edges from a graph.
1583 /// FilterEdges adaptor hides edges in a digraph. A bool edge map must
1584 /// be specified, which defines the filters for edges. Just the
1585 /// unfiltered edges are shown in the subdigraph. The FilterEdges is
1586 /// conform to the \ref concepts::Graph "Graph concept".
1588 /// \tparam _Graph It must be conform to the \ref concepts::Graph
1589 /// "Graph concept". The type can be specified to be const.
1590 /// \tparam _EdgeFilterMap A bool valued edge map of the the adapted
1592 template<typename _Graph, typename _EdgeFilterMap>
1594 public SubGraph<_Graph, ConstMap<typename _Graph::Node,bool>,
1595 _EdgeFilterMap, false> {
1597 typedef _Graph Graph;
1598 typedef _EdgeFilterMap EdgeFilterMap;
1599 typedef SubGraph<Graph, ConstMap<typename Graph::Node,bool>,
1600 EdgeFilterMap, false> Parent;
1601 typedef typename Parent::Edge Edge;
1603 ConstMap<typename Graph::Node, bool> const_true_map;
1605 FilterEdges() : const_true_map(true) {
1606 Parent::setNodeFilterMap(const_true_map);
1611 /// \brief Constructor
1613 /// Creates a FilterEdges adaptor for the given graph with
1614 /// given edge map filters.
1615 FilterEdges(Graph& _graph, EdgeFilterMap& edge_filter_map) :
1616 Parent(), const_true_map(true) {
1617 Parent::setGraph(_graph);
1618 Parent::setNodeFilterMap(const_true_map);
1619 Parent::setEdgeFilterMap(edge_filter_map);
1622 /// \brief Hides the edge of the graph
1624 /// This function hides \c e in the graph, i.e. the iteration
1625 /// jumps over it. This is done by simply setting the value of \c e
1626 /// to be false in the corresponding edge-map.
1627 void hide(const Edge& e) const { Parent::hide(e); }
1629 /// \brief Unhides the edge of the graph
1631 /// The value of \c e is set to be true in the edge-map which stores
1632 /// hide information. If \c e was hidden previuosly, then it is shown
1634 void unHide(const Edge& e) const { Parent::unHide(e); }
1636 /// \brief Returns true if \c e is hidden.
1638 /// Returns true if \c e is hidden.
1640 bool hidden(const Edge& e) const { return Parent::hidden(e); }
1644 /// \brief Just gives back a FilterEdges adaptor
1646 /// Just gives back a FilterEdges adaptor
1647 template<typename Graph, typename EdgeFilterMap>
1648 FilterEdges<const Graph, EdgeFilterMap>
1649 filterEdges(const Graph& graph, EdgeFilterMap& efm) {
1650 return FilterEdges<const Graph, EdgeFilterMap>(graph, efm);
1653 template<typename Graph, typename EdgeFilterMap>
1654 FilterEdges<const Graph, const EdgeFilterMap>
1655 filterEdges(const Graph& graph, const EdgeFilterMap& efm) {
1656 return FilterEdges<const Graph, const EdgeFilterMap>(graph, efm);
1659 template <typename _Digraph>
1660 class UndirectorBase {
1662 typedef _Digraph Digraph;
1663 typedef UndirectorBase Adaptor;
1665 typedef True UndirectedTag;
1667 typedef typename Digraph::Arc Edge;
1668 typedef typename Digraph::Node Node;
1670 class Arc : public Edge {
1671 friend class UndirectorBase;
1675 Arc(const Edge& edge, bool forward) :
1676 Edge(edge), _forward(forward) {}
1681 Arc(Invalid) : Edge(INVALID), _forward(true) {}
1683 bool operator==(const Arc &other) const {
1684 return _forward == other._forward &&
1685 static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1687 bool operator!=(const Arc &other) const {
1688 return _forward != other._forward ||
1689 static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1691 bool operator<(const Arc &other) const {
1692 return _forward < other._forward ||
1693 (_forward == other._forward &&
1694 static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1700 void first(Node& n) const {
1704 void next(Node& n) const {
1708 void first(Arc& a) const {
1713 void next(Arc& a) const {
1722 void first(Edge& e) const {
1726 void next(Edge& e) const {
1730 void firstOut(Arc& a, const Node& n) const {
1731 _digraph->firstIn(a, n);
1732 if( static_cast<const Edge&>(a) != INVALID ) {
1735 _digraph->firstOut(a, n);
1739 void nextOut(Arc &a) const {
1741 Node n = _digraph->target(a);
1742 _digraph->nextIn(a);
1743 if (static_cast<const Edge&>(a) == INVALID ) {
1744 _digraph->firstOut(a, n);
1749 _digraph->nextOut(a);
1753 void firstIn(Arc &a, const Node &n) const {
1754 _digraph->firstOut(a, n);
1755 if (static_cast<const Edge&>(a) != INVALID ) {
1758 _digraph->firstIn(a, n);
1762 void nextIn(Arc &a) const {
1764 Node n = _digraph->source(a);
1765 _digraph->nextOut(a);
1766 if( static_cast<const Edge&>(a) == INVALID ) {
1767 _digraph->firstIn(a, n);
1772 _digraph->nextIn(a);
1776 void firstInc(Edge &e, bool &d, const Node &n) const {
1778 _digraph->firstOut(e, n);
1779 if (e != INVALID) return;
1781 _digraph->firstIn(e, n);
1784 void nextInc(Edge &e, bool &d) const {
1786 Node s = _digraph->source(e);
1787 _digraph->nextOut(e);
1788 if (e != INVALID) return;
1790 _digraph->firstIn(e, s);
1792 _digraph->nextIn(e);
1796 Node u(const Edge& e) const {
1797 return _digraph->source(e);
1800 Node v(const Edge& e) const {
1801 return _digraph->target(e);
1804 Node source(const Arc &a) const {
1805 return a._forward ? _digraph->source(a) : _digraph->target(a);
1808 Node target(const Arc &a) const {
1809 return a._forward ? _digraph->target(a) : _digraph->source(a);
1812 static Arc direct(const Edge &e, bool d) {
1815 Arc direct(const Edge &e, const Node& n) const {
1816 return Arc(e, _digraph->source(e) == n);
1819 static bool direction(const Arc &a) { return a._forward; }
1821 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1822 Arc arcFromId(int ix) const {
1823 return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1825 Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1827 int id(const Node &n) const { return _digraph->id(n); }
1828 int id(const Arc &a) const {
1829 return (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1831 int id(const Edge &e) const { return _digraph->id(e); }
1833 int maxNodeId() const { return _digraph->maxNodeId(); }
1834 int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
1835 int maxEdgeId() const { return _digraph->maxArcId(); }
1837 Node addNode() { return _digraph->addNode(); }
1838 Edge addEdge(const Node& u, const Node& v) {
1839 return _digraph->addArc(u, v);
1842 void erase(const Node& i) { _digraph->erase(i); }
1843 void erase(const Edge& i) { _digraph->erase(i); }
1845 void clear() { _digraph->clear(); }
1847 typedef NodeNumTagIndicator<Digraph> NodeNumTag;
1848 int nodeNum() const { return 2 * _digraph->arcNum(); }
1849 typedef EdgeNumTagIndicator<Digraph> EdgeNumTag;
1850 int arcNum() const { return 2 * _digraph->arcNum(); }
1851 int edgeNum() const { return _digraph->arcNum(); }
1853 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag;
1854 Arc findArc(Node s, Node t, Arc p = INVALID) const {
1856 Edge arc = _digraph->findArc(s, t);
1857 if (arc != INVALID) return direct(arc, true);
1858 arc = _digraph->findArc(t, s);
1859 if (arc != INVALID) return direct(arc, false);
1860 } else if (direction(p)) {
1861 Edge arc = _digraph->findArc(s, t, p);
1862 if (arc != INVALID) return direct(arc, true);
1863 arc = _digraph->findArc(t, s);
1864 if (arc != INVALID) return direct(arc, false);
1866 Edge arc = _digraph->findArc(t, s, p);
1867 if (arc != INVALID) return direct(arc, false);
1872 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
1875 Edge arc = _digraph->findArc(s, t);
1876 if (arc != INVALID) return arc;
1877 arc = _digraph->findArc(t, s);
1878 if (arc != INVALID) return arc;
1879 } else if (_digraph->s(p) == s) {
1880 Edge arc = _digraph->findArc(s, t, p);
1881 if (arc != INVALID) return arc;
1882 arc = _digraph->findArc(t, s);
1883 if (arc != INVALID) return arc;
1885 Edge arc = _digraph->findArc(t, s, p);
1886 if (arc != INVALID) return arc;
1889 return _digraph->findArc(s, t, p);
1896 template <typename _Value>
1900 typedef typename Digraph::template ArcMap<_Value> MapImpl;
1904 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
1906 typedef _Value Value;
1909 ArcMapBase(const Adaptor& adaptor) :
1910 _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
1912 ArcMapBase(const Adaptor& adaptor, const Value& v)
1913 : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {}
1915 void set(const Arc& a, const Value& v) {
1919 _backward.set(a, v);
1923 typename MapTraits<MapImpl>::ConstReturnValue
1924 operator[](const Arc& a) const {
1928 return _backward[a];
1932 typename MapTraits<MapImpl>::ReturnValue
1933 operator[](const Arc& a) {
1937 return _backward[a];
1943 MapImpl _forward, _backward;
1949 template <typename _Value>
1950 class NodeMap : public Digraph::template NodeMap<_Value> {
1953 typedef _Value Value;
1954 typedef typename Digraph::template NodeMap<Value> Parent;
1956 explicit NodeMap(const Adaptor& adaptor)
1957 : Parent(*adaptor._digraph) {}
1959 NodeMap(const Adaptor& adaptor, const _Value& value)
1960 : Parent(*adaptor._digraph, value) { }
1963 NodeMap& operator=(const NodeMap& cmap) {
1964 return operator=<NodeMap>(cmap);
1967 template <typename CMap>
1968 NodeMap& operator=(const CMap& cmap) {
1969 Parent::operator=(cmap);
1975 template <typename _Value>
1977 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
1980 typedef _Value Value;
1981 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
1983 ArcMap(const Adaptor& adaptor)
1984 : Parent(adaptor) {}
1986 ArcMap(const Adaptor& adaptor, const Value& value)
1987 : Parent(adaptor, value) {}
1990 ArcMap& operator=(const ArcMap& cmap) {
1991 return operator=<ArcMap>(cmap);
1994 template <typename CMap>
1995 ArcMap& operator=(const CMap& cmap) {
1996 Parent::operator=(cmap);
2001 template <typename _Value>
2002 class EdgeMap : public Digraph::template ArcMap<_Value> {
2005 typedef _Value Value;
2006 typedef typename Digraph::template ArcMap<Value> Parent;
2008 explicit EdgeMap(const Adaptor& adaptor)
2009 : Parent(*adaptor._digraph) {}
2011 EdgeMap(const Adaptor& adaptor, const Value& value)
2012 : Parent(*adaptor._digraph, value) {}
2015 EdgeMap& operator=(const EdgeMap& cmap) {
2016 return operator=<EdgeMap>(cmap);
2019 template <typename CMap>
2020 EdgeMap& operator=(const CMap& cmap) {
2021 Parent::operator=(cmap);
2027 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
2028 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2032 UndirectorBase() : _digraph(0) {}
2036 void setDigraph(Digraph& digraph) {
2037 _digraph = &digraph;
2042 /// \ingroup graph_adaptors
2044 /// \brief Undirect the graph
2046 /// This adaptor makes an undirected graph from a directed
2047 /// graph. All arcs of the underlying digraph will be showed in the
2048 /// adaptor as an edge. The Orienter adaptor is conform to the \ref
2049 /// concepts::Graph "Graph concept".
2051 /// \tparam _Digraph It must be conform to the \ref
2052 /// concepts::Digraph "Digraph concept". The type can be specified
2054 template<typename _Digraph>
2056 : public GraphAdaptorExtender<UndirectorBase<_Digraph> > {
2058 typedef _Digraph Digraph;
2059 typedef GraphAdaptorExtender<UndirectorBase<Digraph> > Parent;
2064 /// \brief Constructor
2066 /// Creates a undirected graph from the given digraph
2067 Undirector(_Digraph& digraph) {
2068 setDigraph(digraph);
2071 /// \brief ArcMap combined from two original ArcMap
2073 /// This class adapts two original digraph ArcMap to
2074 /// get an arc map on the undirected graph.
2075 template <typename _ForwardMap, typename _BackwardMap>
2076 class CombinedArcMap {
2079 typedef _ForwardMap ForwardMap;
2080 typedef _BackwardMap BackwardMap;
2082 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2084 typedef typename ForwardMap::Value Value;
2085 typedef typename Parent::Arc Key;
2087 /// \brief Constructor
2090 CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2091 : _forward(&forward), _backward(&backward) {}
2094 /// \brief Sets the value associated with a key.
2096 /// Sets the value associated with a key.
2097 void set(const Key& e, const Value& a) {
2098 if (Parent::direction(e)) {
2099 _forward->set(e, a);
2101 _backward->set(e, a);
2105 /// \brief Returns the value associated with a key.
2107 /// Returns the value associated with a key.
2108 typename MapTraits<ForwardMap>::ConstReturnValue
2109 operator[](const Key& e) const {
2110 if (Parent::direction(e)) {
2111 return (*_forward)[e];
2113 return (*_backward)[e];
2117 /// \brief Returns the value associated with a key.
2119 /// Returns the value associated with a key.
2120 typename MapTraits<ForwardMap>::ReturnValue
2121 operator[](const Key& e) {
2122 if (Parent::direction(e)) {
2123 return (*_forward)[e];
2125 return (*_backward)[e];
2131 ForwardMap* _forward;
2132 BackwardMap* _backward;
2136 /// \brief Just gives back a combined arc map
2138 /// Just gives back a combined arc map
2139 template <typename ForwardMap, typename BackwardMap>
2140 static CombinedArcMap<ForwardMap, BackwardMap>
2141 combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2142 return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2145 template <typename ForwardMap, typename BackwardMap>
2146 static CombinedArcMap<const ForwardMap, BackwardMap>
2147 combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2148 return CombinedArcMap<const ForwardMap,
2149 BackwardMap>(forward, backward);
2152 template <typename ForwardMap, typename BackwardMap>
2153 static CombinedArcMap<ForwardMap, const BackwardMap>
2154 combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2155 return CombinedArcMap<ForwardMap,
2156 const BackwardMap>(forward, backward);
2159 template <typename ForwardMap, typename BackwardMap>
2160 static CombinedArcMap<const ForwardMap, const BackwardMap>
2161 combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2162 return CombinedArcMap<const ForwardMap,
2163 const BackwardMap>(forward, backward);
2168 /// \brief Just gives back an undirected view of the given digraph
2170 /// Just gives back an undirected view of the given digraph
2171 template<typename Digraph>
2172 Undirector<const Digraph>
2173 undirector(const Digraph& digraph) {
2174 return Undirector<const Digraph>(digraph);
2177 template <typename _Graph, typename _DirectionMap>
2178 class OrienterBase {
2181 typedef _Graph Graph;
2182 typedef _DirectionMap DirectionMap;
2184 typedef typename Graph::Node Node;
2185 typedef typename Graph::Edge Arc;
2187 void reverseArc(const Arc& arc) {
2188 _direction->set(arc, !(*_direction)[arc]);
2191 void first(Node& i) const { _graph->first(i); }
2192 void first(Arc& i) const { _graph->first(i); }
2193 void firstIn(Arc& i, const Node& n) const {
2195 _graph->firstInc(i, d, n);
2196 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2198 void firstOut(Arc& i, const Node& n ) const {
2200 _graph->firstInc(i, d, n);
2201 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2204 void next(Node& i) const { _graph->next(i); }
2205 void next(Arc& i) const { _graph->next(i); }
2206 void nextIn(Arc& i) const {
2207 bool d = !(*_direction)[i];
2208 _graph->nextInc(i, d);
2209 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2211 void nextOut(Arc& i) const {
2212 bool d = (*_direction)[i];
2213 _graph->nextInc(i, d);
2214 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2217 Node source(const Arc& e) const {
2218 return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2220 Node target(const Arc& e) const {
2221 return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2224 typedef NodeNumTagIndicator<Graph> NodeNumTag;
2225 int nodeNum() const { return _graph->nodeNum(); }
2227 typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
2228 int arcNum() const { return _graph->edgeNum(); }
2230 typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
2231 Arc findArc(const Node& u, const Node& v,
2232 const Arc& prev = INVALID) {
2234 bool d = arc == INVALID ? true : (*_direction)[arc];
2236 arc = _graph->findEdge(u, v, arc);
2237 while (arc != INVALID && !(*_direction)[arc]) {
2238 _graph->findEdge(u, v, arc);
2240 if (arc != INVALID) return arc;
2242 _graph->findEdge(v, u, arc);
2243 while (arc != INVALID && (*_direction)[arc]) {
2244 _graph->findEdge(u, v, arc);
2250 return Node(_graph->addNode());
2253 Arc addArc(const Node& u, const Node& v) {
2254 Arc arc = _graph->addArc(u, v);
2255 _direction->set(arc, _graph->source(arc) == u);
2259 void erase(const Node& i) { _graph->erase(i); }
2260 void erase(const Arc& i) { _graph->erase(i); }
2262 void clear() { _graph->clear(); }
2264 int id(const Node& v) const { return _graph->id(v); }
2265 int id(const Arc& e) const { return _graph->id(e); }
2267 Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2268 Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2270 int maxNodeId() const { return _graph->maxNodeId(); }
2271 int maxArcId() const { return _graph->maxEdgeId(); }
2273 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
2274 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2276 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
2277 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2279 template <typename _Value>
2280 class NodeMap : public _Graph::template NodeMap<_Value> {
2283 typedef typename _Graph::template NodeMap<_Value> Parent;
2285 explicit NodeMap(const OrienterBase& adapter)
2286 : Parent(*adapter._graph) {}
2288 NodeMap(const OrienterBase& adapter, const _Value& value)
2289 : Parent(*adapter._graph, value) {}
2292 NodeMap& operator=(const NodeMap& cmap) {
2293 return operator=<NodeMap>(cmap);
2296 template <typename CMap>
2297 NodeMap& operator=(const CMap& cmap) {
2298 Parent::operator=(cmap);
2304 template <typename _Value>
2305 class ArcMap : public _Graph::template EdgeMap<_Value> {
2308 typedef typename Graph::template EdgeMap<_Value> Parent;
2310 explicit ArcMap(const OrienterBase& adapter)
2311 : Parent(*adapter._graph) { }
2313 ArcMap(const OrienterBase& adapter, const _Value& value)
2314 : Parent(*adapter._graph, value) { }
2317 ArcMap& operator=(const ArcMap& cmap) {
2318 return operator=<ArcMap>(cmap);
2321 template <typename CMap>
2322 ArcMap& operator=(const CMap& cmap) {
2323 Parent::operator=(cmap);
2332 DirectionMap* _direction;
2334 void setDirectionMap(DirectionMap& direction) {
2335 _direction = &direction;
2338 void setGraph(Graph& graph) {
2344 /// \ingroup graph_adaptors
2346 /// \brief Orients the edges of the graph to get a digraph
2348 /// This adaptor orients each edge in the undirected graph. The
2349 /// direction of the arcs stored in an edge node map. The arcs can
2350 /// be easily reverted by the \c reverseArc() member function in the
2351 /// adaptor. The Orienter adaptor is conform to the \ref
2352 /// concepts::Digraph "Digraph concept".
2354 /// \tparam _Graph It must be conform to the \ref concepts::Graph
2355 /// "Graph concept". The type can be specified to be const.
2356 /// \tparam _DirectionMap A bool valued edge map of the the adapted
2360 template<typename _Graph,
2361 typename DirectionMap = typename _Graph::template EdgeMap<bool> >
2363 public DigraphAdaptorExtender<OrienterBase<_Graph, DirectionMap> > {
2365 typedef _Graph Graph;
2366 typedef DigraphAdaptorExtender<
2367 OrienterBase<_Graph, DirectionMap> > Parent;
2368 typedef typename Parent::Arc Arc;
2373 /// \brief Constructor of the adaptor
2375 /// Constructor of the adaptor
2376 Orienter(Graph& graph, DirectionMap& direction) {
2378 setDirectionMap(direction);
2381 /// \brief Reverse arc
2383 /// It reverse the given arc. It simply negate the direction in the map.
2384 void reverseArc(const Arc& a) {
2385 Parent::reverseArc(a);
2389 /// \brief Just gives back a Orienter
2391 /// Just gives back a Orienter
2392 template<typename Graph, typename DirectionMap>
2393 Orienter<const Graph, DirectionMap>
2394 orienter(const Graph& graph, DirectionMap& dm) {
2395 return Orienter<const Graph, DirectionMap>(graph, dm);
2398 template<typename Graph, typename DirectionMap>
2399 Orienter<const Graph, const DirectionMap>
2400 orienter(const Graph& graph, const DirectionMap& dm) {
2401 return Orienter<const Graph, const DirectionMap>(graph, dm);
2404 namespace _adaptor_bits {
2406 template<typename _Digraph,
2407 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2408 typename _FlowMap = _CapacityMap,
2409 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2410 class ResForwardFilter {
2413 typedef _Digraph Digraph;
2414 typedef _CapacityMap CapacityMap;
2415 typedef _FlowMap FlowMap;
2416 typedef _Tolerance Tolerance;
2418 typedef typename Digraph::Arc Key;
2423 const CapacityMap* _capacity;
2424 const FlowMap* _flow;
2425 Tolerance _tolerance;
2428 ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2429 const Tolerance& tolerance = Tolerance())
2430 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2432 bool operator[](const typename Digraph::Arc& a) const {
2433 return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2437 template<typename _Digraph,
2438 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2439 typename _FlowMap = _CapacityMap,
2440 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2441 class ResBackwardFilter {
2444 typedef _Digraph Digraph;
2445 typedef _CapacityMap CapacityMap;
2446 typedef _FlowMap FlowMap;
2447 typedef _Tolerance Tolerance;
2449 typedef typename Digraph::Arc Key;
2454 const CapacityMap* _capacity;
2455 const FlowMap* _flow;
2456 Tolerance _tolerance;
2460 ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2461 const Tolerance& tolerance = Tolerance())
2462 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2464 bool operator[](const typename Digraph::Arc& a) const {
2465 return _tolerance.positive((*_flow)[a]);
2471 /// \ingroup graph_adaptors
2473 /// \brief An adaptor for composing the residual graph for directed
2474 /// flow and circulation problems.
2476 /// An adaptor for composing the residual graph for directed flow and
2477 /// circulation problems. Let \f$ G=(V, A) \f$ be a directed graph
2478 /// and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$,
2479 /// be functions on the arc-set.
2481 /// Then Residual implements the digraph structure with
2482 /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} \f$,
2483 /// where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
2484 /// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so
2485 /// called residual graph. When we take the union
2486 /// \f$ A_{forward}\cup A_{backward} \f$, multiplicities are counted,
2487 /// i.e. if an arc is in both \f$ A_{forward} \f$ and
2488 /// \f$ A_{backward} \f$, then in the adaptor it appears in both
2491 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
2492 /// "Digraph concept". The type is implicitly const.
2493 /// \tparam _CapacityMap An arc map of some numeric type, it defines
2494 /// the capacities in the flow problem. The map is implicitly const.
2495 /// \tparam _FlowMap An arc map of some numeric type, it defines
2496 /// the capacities in the flow problem.
2497 /// \tparam _Tolerance Handler for inexact computation.
2498 template<typename _Digraph,
2499 typename _CapacityMap = typename _Digraph::template ArcMap<int>,
2500 typename _FlowMap = _CapacityMap,
2501 typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
2504 Undirector<const _Digraph>,
2505 typename Undirector<const _Digraph>::template CombinedArcMap<
2506 _adaptor_bits::ResForwardFilter<const _Digraph, _CapacityMap,
2507 _FlowMap, _Tolerance>,
2508 _adaptor_bits::ResBackwardFilter<const _Digraph, _CapacityMap,
2509 _FlowMap, _Tolerance> > >
2513 typedef _Digraph Digraph;
2514 typedef _CapacityMap CapacityMap;
2515 typedef _FlowMap FlowMap;
2516 typedef _Tolerance Tolerance;
2518 typedef typename CapacityMap::Value Value;
2519 typedef Residual Adaptor;
2523 typedef Undirector<const Digraph> Undirected;
2525 typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap,
2526 FlowMap, Tolerance> ForwardFilter;
2528 typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap,
2529 FlowMap, Tolerance> BackwardFilter;
2531 typedef typename Undirected::
2532 template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2534 typedef FilterArcs<Undirected, ArcFilter> Parent;
2536 const CapacityMap* _capacity;
2540 ForwardFilter _forward_filter;
2541 BackwardFilter _backward_filter;
2542 ArcFilter _arc_filter;
2546 /// \brief Constructor of the residual digraph.
2548 /// Constructor of the residual graph. The parameters are the digraph,
2549 /// the flow map, the capacity map and a tolerance object.
2550 Residual(const Digraph& digraph, const CapacityMap& capacity,
2551 FlowMap& flow, const Tolerance& tolerance = Tolerance())
2552 : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph),
2553 _forward_filter(capacity, flow, tolerance),
2554 _backward_filter(capacity, flow, tolerance),
2555 _arc_filter(_forward_filter, _backward_filter)
2557 Parent::setDigraph(_graph);
2558 Parent::setArcFilterMap(_arc_filter);
2561 typedef typename Parent::Arc Arc;
2563 /// \brief Gives back the residual capacity of the arc.
2565 /// Gives back the residual capacity of the arc.
2566 Value residualCapacity(const Arc& a) const {
2567 if (Undirected::direction(a)) {
2568 return (*_capacity)[a] - (*_flow)[a];
2574 /// \brief Augment on the given arc in the residual graph.
2576 /// Augment on the given arc in the residual graph. It increase
2577 /// or decrease the flow on the original arc depend on the direction
2578 /// of the residual arc.
2579 void augment(const Arc& a, const Value& v) const {
2580 if (Undirected::direction(a)) {
2581 _flow->set(a, (*_flow)[a] + v);
2583 _flow->set(a, (*_flow)[a] - v);
2587 /// \brief Returns the direction of the arc.
2589 /// Returns true when the arc is same oriented as the original arc.
2590 static bool forward(const Arc& a) {
2591 return Undirected::direction(a);
2594 /// \brief Returns the direction of the arc.
2596 /// Returns true when the arc is opposite oriented as the original arc.
2597 static bool backward(const Arc& a) {
2598 return !Undirected::direction(a);
2601 /// \brief Gives back the forward oriented residual arc.
2603 /// Gives back the forward oriented residual arc.
2604 static Arc forward(const typename Digraph::Arc& a) {
2605 return Undirected::direct(a, true);
2608 /// \brief Gives back the backward oriented residual arc.
2610 /// Gives back the backward oriented residual arc.
2611 static Arc backward(const typename Digraph::Arc& a) {
2612 return Undirected::direct(a, false);
2615 /// \brief Residual capacity map.
2617 /// In generic residual graph the residual capacity can be obtained
2619 class ResidualCapacity {
2621 const Adaptor* _adaptor;
2626 typedef typename _CapacityMap::Value Value;
2629 ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {}
2632 Value operator[](const Arc& a) const {
2633 return _adaptor->residualCapacity(a);
2640 template <typename _Digraph>
2641 class SplitNodesBase {
2644 typedef _Digraph Digraph;
2645 typedef DigraphAdaptorBase<const _Digraph> Parent;
2646 typedef SplitNodesBase Adaptor;
2648 typedef typename Digraph::Node DigraphNode;
2649 typedef typename Digraph::Arc DigraphArc;
2656 template <typename T> class NodeMapBase;
2657 template <typename T> class ArcMapBase;
2661 class Node : public DigraphNode {
2662 friend class SplitNodesBase;
2663 template <typename T> friend class NodeMapBase;
2667 Node(DigraphNode node, bool in)
2668 : DigraphNode(node), _in(in) {}
2673 Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2675 bool operator==(const Node& node) const {
2676 return DigraphNode::operator==(node) && _in == node._in;
2679 bool operator!=(const Node& node) const {
2680 return !(*this == node);
2683 bool operator<(const Node& node) const {
2684 return DigraphNode::operator<(node) ||
2685 (DigraphNode::operator==(node) && _in < node._in);
2690 friend class SplitNodesBase;
2691 template <typename T> friend class ArcMapBase;
2693 typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2695 explicit Arc(const DigraphArc& arc) : _item(arc) {}
2696 explicit Arc(const DigraphNode& node) : _item(node) {}
2702 Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2704 bool operator==(const Arc& arc) const {
2705 if (_item.firstState()) {
2706 if (arc._item.firstState()) {
2707 return _item.first() == arc._item.first();
2710 if (arc._item.secondState()) {
2711 return _item.second() == arc._item.second();
2717 bool operator!=(const Arc& arc) const {
2718 return !(*this == arc);
2721 bool operator<(const Arc& arc) const {
2722 if (_item.firstState()) {
2723 if (arc._item.firstState()) {
2724 return _item.first() < arc._item.first();
2728 if (arc._item.secondState()) {
2729 return _item.second() < arc._item.second();
2735 operator DigraphArc() const { return _item.first(); }
2736 operator DigraphNode() const { return _item.second(); }
2740 void first(Node& n) const {
2745 void next(Node& n) const {
2754 void first(Arc& e) const {
2755 e._item.setSecond();
2756 _digraph->first(e._item.second());
2757 if (e._item.second() == INVALID) {
2759 _digraph->first(e._item.first());
2763 void next(Arc& e) const {
2764 if (e._item.secondState()) {
2765 _digraph->next(e._item.second());
2766 if (e._item.second() == INVALID) {
2768 _digraph->first(e._item.first());
2771 _digraph->next(e._item.first());
2775 void firstOut(Arc& e, const Node& n) const {
2777 e._item.setSecond(n);
2780 _digraph->firstOut(e._item.first(), n);
2784 void nextOut(Arc& e) const {
2785 if (!e._item.firstState()) {
2786 e._item.setFirst(INVALID);
2788 _digraph->nextOut(e._item.first());
2792 void firstIn(Arc& e, const Node& n) const {
2794 e._item.setSecond(n);
2797 _digraph->firstIn(e._item.first(), n);
2801 void nextIn(Arc& e) const {
2802 if (!e._item.firstState()) {
2803 e._item.setFirst(INVALID);
2805 _digraph->nextIn(e._item.first());
2809 Node source(const Arc& e) const {
2810 if (e._item.firstState()) {
2811 return Node(_digraph->source(e._item.first()), false);
2813 return Node(e._item.second(), true);
2817 Node target(const Arc& e) const {
2818 if (e._item.firstState()) {
2819 return Node(_digraph->target(e._item.first()), true);
2821 return Node(e._item.second(), false);
2825 int id(const Node& n) const {
2826 return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
2828 Node nodeFromId(int ix) const {
2829 return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
2831 int maxNodeId() const {
2832 return 2 * _digraph->maxNodeId() + 1;
2835 int id(const Arc& e) const {
2836 if (e._item.firstState()) {
2837 return _digraph->id(e._item.first()) << 1;
2839 return (_digraph->id(e._item.second()) << 1) | 1;
2842 Arc arcFromId(int ix) const {
2843 if ((ix & 1) == 0) {
2844 return Arc(_digraph->arcFromId(ix >> 1));
2846 return Arc(_digraph->nodeFromId(ix >> 1));
2849 int maxArcId() const {
2850 return std::max(_digraph->maxNodeId() << 1,
2851 (_digraph->maxArcId() << 1) | 1);
2854 static bool inNode(const Node& n) {
2858 static bool outNode(const Node& n) {
2862 static bool origArc(const Arc& e) {
2863 return e._item.firstState();
2866 static bool bindArc(const Arc& e) {
2867 return e._item.secondState();
2870 static Node inNode(const DigraphNode& n) {
2871 return Node(n, true);
2874 static Node outNode(const DigraphNode& n) {
2875 return Node(n, false);
2878 static Arc arc(const DigraphNode& n) {
2882 static Arc arc(const DigraphArc& e) {
2886 typedef True NodeNumTag;
2888 int nodeNum() const {
2889 return 2 * countNodes(*_digraph);
2892 typedef True EdgeNumTag;
2893 int arcNum() const {
2894 return countArcs(*_digraph) + countNodes(*_digraph);
2897 typedef True FindEdgeTag;
2898 Arc findArc(const Node& u, const Node& v,
2899 const Arc& prev = INVALID) const {
2902 if (static_cast<const DigraphNode&>(u) ==
2903 static_cast<const DigraphNode&>(v) && prev == INVALID) {
2909 return Arc(::lemon::findArc(*_digraph, u, v, prev));
2917 template <typename _Value>
2919 : public MapTraits<typename Parent::template NodeMap<_Value> > {
2920 typedef typename Parent::template NodeMap<_Value> NodeImpl;
2923 typedef _Value Value;
2925 NodeMapBase(const Adaptor& adaptor)
2926 : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
2927 NodeMapBase(const Adaptor& adaptor, const Value& value)
2928 : _in_map(*adaptor._digraph, value),
2929 _out_map(*adaptor._digraph, value) {}
2931 void set(const Node& key, const Value& val) {
2932 if (Adaptor::inNode(key)) { _in_map.set(key, val); }
2933 else {_out_map.set(key, val); }
2936 typename MapTraits<NodeImpl>::ReturnValue
2937 operator[](const Node& key) {
2938 if (Adaptor::inNode(key)) { return _in_map[key]; }
2939 else { return _out_map[key]; }
2942 typename MapTraits<NodeImpl>::ConstReturnValue
2943 operator[](const Node& key) const {
2944 if (Adaptor::inNode(key)) { return _in_map[key]; }
2945 else { return _out_map[key]; }
2949 NodeImpl _in_map, _out_map;
2952 template <typename _Value>
2954 : public MapTraits<typename Parent::template ArcMap<_Value> > {
2955 typedef typename Parent::template ArcMap<_Value> ArcImpl;
2956 typedef typename Parent::template NodeMap<_Value> NodeImpl;
2959 typedef _Value Value;
2961 ArcMapBase(const Adaptor& adaptor)
2962 : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
2963 ArcMapBase(const Adaptor& adaptor, const Value& value)
2964 : _arc_map(*adaptor._digraph, value),
2965 _node_map(*adaptor._digraph, value) {}
2967 void set(const Arc& key, const Value& val) {
2968 if (Adaptor::origArc(key)) {
2969 _arc_map.set(key._item.first(), val);
2971 _node_map.set(key._item.second(), val);
2975 typename MapTraits<ArcImpl>::ReturnValue
2976 operator[](const Arc& key) {
2977 if (Adaptor::origArc(key)) {
2978 return _arc_map[key._item.first()];
2980 return _node_map[key._item.second()];
2984 typename MapTraits<ArcImpl>::ConstReturnValue
2985 operator[](const Arc& key) const {
2986 if (Adaptor::origArc(key)) {
2987 return _arc_map[key._item.first()];
2989 return _node_map[key._item.second()];
3000 template <typename _Value>
3002 : public SubMapExtender<Adaptor, NodeMapBase<_Value> >
3005 typedef _Value Value;
3006 typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent;
3008 NodeMap(const Adaptor& adaptor)
3009 : Parent(adaptor) {}
3011 NodeMap(const Adaptor& adaptor, const Value& value)
3012 : Parent(adaptor, value) {}
3015 NodeMap& operator=(const NodeMap& cmap) {
3016 return operator=<NodeMap>(cmap);
3019 template <typename CMap>
3020 NodeMap& operator=(const CMap& cmap) {
3021 Parent::operator=(cmap);
3026 template <typename _Value>
3028 : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
3031 typedef _Value Value;
3032 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
3034 ArcMap(const Adaptor& adaptor)
3035 : Parent(adaptor) {}
3037 ArcMap(const Adaptor& adaptor, const Value& value)
3038 : Parent(adaptor, value) {}
3041 ArcMap& operator=(const ArcMap& cmap) {
3042 return operator=<ArcMap>(cmap);
3045 template <typename CMap>
3046 ArcMap& operator=(const CMap& cmap) {
3047 Parent::operator=(cmap);
3054 SplitNodesBase() : _digraph(0) {}
3058 void setDigraph(Digraph& digraph) {
3059 _digraph = &digraph;
3064 /// \ingroup graph_adaptors
3066 /// \brief Split the nodes of a directed graph
3068 /// The SplitNodes adaptor splits each node into an in-node and an
3069 /// out-node. Formaly, the adaptor replaces each \f$ u \f$ node in
3070 /// the digraph with two nodes(namely node \f$ u_{in} \f$ and node
3071 /// \f$ u_{out} \f$). If there is a \f$ (v, u) \f$ arc in the
3072 /// original digraph the new target of the arc will be \f$ u_{in} \f$
3073 /// and similarly the source of the original \f$ (u, v) \f$ arc
3074 /// will be \f$ u_{out} \f$. The adaptor will add for each node in
3075 /// the original digraph an additional arc which connects
3076 /// \f$ (u_{in}, u_{out}) \f$.
3078 /// The aim of this class is to run algorithm with node costs if the
3079 /// algorithm can use directly just arc costs. In this case we should use
3080 /// a \c SplitNodes and set the node cost of the graph to the
3081 /// bind arc in the adapted graph.
3083 /// \tparam _Digraph It must be conform to the \ref concepts::Digraph
3084 /// "Digraph concept". The type can be specified to be const.
3085 template <typename _Digraph>
3087 : public DigraphAdaptorExtender<SplitNodesBase<_Digraph> > {
3089 typedef _Digraph Digraph;
3090 typedef DigraphAdaptorExtender<SplitNodesBase<Digraph> > Parent;
3092 typedef typename Digraph::Node DigraphNode;
3093 typedef typename Digraph::Arc DigraphArc;
3095 typedef typename Parent::Node Node;
3096 typedef typename Parent::Arc Arc;
3098 /// \brief Constructor of the adaptor.
3100 /// Constructor of the adaptor.
3101 SplitNodes(Digraph& g) {
3102 Parent::setDigraph(g);
3105 /// \brief Returns true when the node is in-node.
3107 /// Returns true when the node is in-node.
3108 static bool inNode(const Node& n) {
3109 return Parent::inNode(n);
3112 /// \brief Returns true when the node is out-node.
3114 /// Returns true when the node is out-node.
3115 static bool outNode(const Node& n) {
3116 return Parent::outNode(n);
3119 /// \brief Returns true when the arc is arc in the original digraph.
3121 /// Returns true when the arc is arc in the original digraph.
3122 static bool origArc(const Arc& a) {
3123 return Parent::origArc(a);
3126 /// \brief Returns true when the arc binds an in-node and an out-node.
3128 /// Returns true when the arc binds an in-node and an out-node.
3129 static bool bindArc(const Arc& a) {
3130 return Parent::bindArc(a);
3133 /// \brief Gives back the in-node created from the \c node.
3135 /// Gives back the in-node created from the \c node.
3136 static Node inNode(const DigraphNode& n) {
3137 return Parent::inNode(n);
3140 /// \brief Gives back the out-node created from the \c node.
3142 /// Gives back the out-node created from the \c node.
3143 static Node outNode(const DigraphNode& n) {
3144 return Parent::outNode(n);
3147 /// \brief Gives back the arc binds the two part of the node.
3149 /// Gives back the arc binds the two part of the node.
3150 static Arc arc(const DigraphNode& n) {
3151 return Parent::arc(n);
3154 /// \brief Gives back the arc of the original arc.
3156 /// Gives back the arc of the original arc.
3157 static Arc arc(const DigraphArc& a) {
3158 return Parent::arc(a);
3161 /// \brief NodeMap combined from two original NodeMap
3163 /// This class adapt two of the original digraph NodeMap to
3164 /// get a node map on the adapted digraph.
3165 template <typename InNodeMap, typename OutNodeMap>
3166 class CombinedNodeMap {
3170 typedef typename InNodeMap::Value Value;
3172 /// \brief Constructor
3175 CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3176 : _in_map(in_map), _out_map(out_map) {}
3178 /// \brief The subscript operator.
3180 /// The subscript operator.
3181 Value& operator[](const Key& key) {
3182 if (Parent::inNode(key)) {
3183 return _in_map[key];
3185 return _out_map[key];
3189 /// \brief The const subscript operator.
3191 /// The const subscript operator.
3192 Value operator[](const Key& key) const {
3193 if (Parent::inNode(key)) {
3194 return _in_map[key];
3196 return _out_map[key];
3200 /// \brief The setter function of the map.
3202 /// The setter function of the map.
3203 void set(const Key& key, const Value& value) {
3204 if (Parent::inNode(key)) {
3205 _in_map.set(key, value);
3207 _out_map.set(key, value);
3214 OutNodeMap& _out_map;
3219 /// \brief Just gives back a combined node map
3221 /// Just gives back a combined node map
3222 template <typename InNodeMap, typename OutNodeMap>
3223 static CombinedNodeMap<InNodeMap, OutNodeMap>
3224 combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3225 return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3228 template <typename InNodeMap, typename OutNodeMap>
3229 static CombinedNodeMap<const InNodeMap, OutNodeMap>
3230 combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3231 return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3234 template <typename InNodeMap, typename OutNodeMap>
3235 static CombinedNodeMap<InNodeMap, const OutNodeMap>
3236 combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3237 return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3240 template <typename InNodeMap, typename OutNodeMap>
3241 static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3242 combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3243 return CombinedNodeMap<const InNodeMap,
3244 const OutNodeMap>(in_map, out_map);
3247 /// \brief ArcMap combined from an original ArcMap and a NodeMap
3249 /// This class adapt an original ArcMap and a NodeMap to get an
3250 /// arc map on the adapted digraph
3251 template <typename DigraphArcMap, typename DigraphNodeMap>
3252 class CombinedArcMap {
3256 typedef typename DigraphArcMap::Value Value;
3258 /// \brief Constructor
3261 CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map)
3262 : _arc_map(arc_map), _node_map(node_map) {}
3264 /// \brief The subscript operator.
3266 /// The subscript operator.
3267 void set(const Arc& arc, const Value& val) {
3268 if (Parent::origArc(arc)) {
3269 _arc_map.set(arc, val);
3271 _node_map.set(arc, val);
3275 /// \brief The const subscript operator.
3277 /// The const subscript operator.
3278 Value operator[](const Key& arc) const {
3279 if (Parent::origArc(arc)) {
3280 return _arc_map[arc];
3282 return _node_map[arc];
3286 /// \brief The const subscript operator.
3288 /// The const subscript operator.
3289 Value& operator[](const Key& arc) {
3290 if (Parent::origArc(arc)) {
3291 return _arc_map[arc];
3293 return _node_map[arc];
3298 DigraphArcMap& _arc_map;
3299 DigraphNodeMap& _node_map;
3302 /// \brief Just gives back a combined arc map
3304 /// Just gives back a combined arc map
3305 template <typename DigraphArcMap, typename DigraphNodeMap>
3306 static CombinedArcMap<DigraphArcMap, DigraphNodeMap>
3307 combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) {
3308 return CombinedArcMap<DigraphArcMap, DigraphNodeMap>(arc_map, node_map);
3311 template <typename DigraphArcMap, typename DigraphNodeMap>
3312 static CombinedArcMap<const DigraphArcMap, DigraphNodeMap>
3313 combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) {
3314 return CombinedArcMap<const DigraphArcMap,
3315 DigraphNodeMap>(arc_map, node_map);
3318 template <typename DigraphArcMap, typename DigraphNodeMap>
3319 static CombinedArcMap<DigraphArcMap, const DigraphNodeMap>
3320 combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) {
3321 return CombinedArcMap<DigraphArcMap,
3322 const DigraphNodeMap>(arc_map, node_map);
3325 template <typename DigraphArcMap, typename DigraphNodeMap>
3326 static CombinedArcMap<const DigraphArcMap, const DigraphNodeMap>
3327 combinedArcMap(const DigraphArcMap& arc_map,
3328 const DigraphNodeMap& node_map) {
3329 return CombinedArcMap<const DigraphArcMap,
3330 const DigraphNodeMap>(arc_map, node_map);
3335 /// \brief Just gives back a node splitter
3337 /// Just gives back a node splitter
3338 template<typename Digraph>
3340 splitNodes(const Digraph& digraph) {
3341 return SplitNodes<Digraph>(digraph);
3347 #endif //LEMON_ADAPTORS_H