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_EDGE_SET_H
20 #define LEMON_EDGE_SET_H
22 #include <lemon/core.h>
23 #include <lemon/bits/edge_set_extender.h>
25 /// \ingroup semi_adaptors
27 /// \brief ArcSet and EdgeSet classes.
29 /// Graphs which use another graph's node-set as own.
32 template <typename GR>
33 class ListArcSetBase {
36 typedef typename GR::Node Node;
37 typedef typename GR::NodeIt NodeIt;
42 int first_out, first_in;
43 NodeT() : first_out(-1), first_in(-1) {}
46 typedef typename ItemSetTraits<GR, Node>::
47 template Map<NodeT>::Type NodesImplBase;
49 NodesImplBase* _nodes;
53 int next_out, next_in;
54 int prev_out, prev_in;
55 ArcT() : prev_out(-1), prev_in(-1) {}
58 std::vector<ArcT> arcs;
65 void initalize(const GR& graph, NodesImplBase& nodes) {
73 friend class ListArcSetBase<GR>;
75 Arc(int _id) : id(_id) {}
79 Arc(Invalid) : id(-1) {}
80 bool operator==(const Arc& arc) const { return id == arc.id; }
81 bool operator!=(const Arc& arc) const { return id != arc.id; }
82 bool operator<(const Arc& arc) const { return id < arc.id; }
85 ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
87 Arc addArc(const Node& u, const Node& v) {
89 if (first_free_arc == -1) {
91 arcs.push_back(ArcT());
94 first_free_arc = arcs[first_free_arc].next_in;
96 arcs[n].next_in = (*_nodes)[v].first_in;
97 if ((*_nodes)[v].first_in != -1) {
98 arcs[(*_nodes)[v].first_in].prev_in = n;
100 (*_nodes)[v].first_in = n;
101 arcs[n].next_out = (*_nodes)[u].first_out;
102 if ((*_nodes)[u].first_out != -1) {
103 arcs[(*_nodes)[u].first_out].prev_out = n;
105 (*_nodes)[u].first_out = n;
111 void erase(const Arc& arc) {
113 if (arcs[n].prev_in != -1) {
114 arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
116 (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
118 if (arcs[n].next_in != -1) {
119 arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
122 if (arcs[n].prev_out != -1) {
123 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
125 (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
127 if (arcs[n].next_out != -1) {
128 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
135 for (first(node); node != INVALID; next(node)) {
136 (*_nodes)[node].first_in = -1;
137 (*_nodes)[node].first_out = -1;
144 void first(Node& node) const {
148 void next(Node& node) const {
152 void first(Arc& arc) const {
155 while (node != INVALID && (*_nodes)[node].first_in == -1) {
158 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
161 void next(Arc& arc) const {
162 if (arcs[arc.id].next_in != -1) {
163 arc.id = arcs[arc.id].next_in;
165 Node node = arcs[arc.id].target;
167 while (node != INVALID && (*_nodes)[node].first_in == -1) {
170 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
174 void firstOut(Arc& arc, const Node& node) const {
175 arc.id = (*_nodes)[node].first_out;
178 void nextOut(Arc& arc) const {
179 arc.id = arcs[arc.id].next_out;
182 void firstIn(Arc& arc, const Node& node) const {
183 arc.id = (*_nodes)[node].first_in;
186 void nextIn(Arc& arc) const {
187 arc.id = arcs[arc.id].next_in;
190 int id(const Node& node) const { return _graph->id(node); }
191 int id(const Arc& arc) const { return arc.id; }
193 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
194 Arc arcFromId(int ix) const { return Arc(ix); }
196 int maxNodeId() const { return _graph->maxNodeId(); };
197 int maxArcId() const { return arcs.size() - 1; }
199 Node source(const Arc& arc) const { return arcs[arc.id].source;}
200 Node target(const Arc& arc) const { return arcs[arc.id].target;}
202 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
204 NodeNotifier& notifier(Node) const {
205 return _graph->notifier(Node());
208 template <typename V>
209 class NodeMap : public GR::template NodeMap<V> {
210 typedef typename GR::template NodeMap<V> Parent;
214 explicit NodeMap(const ListArcSetBase<GR>& arcset)
215 : Parent(*arcset._graph) {}
217 NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
218 : Parent(*arcset._graph, value) {}
220 NodeMap& operator=(const NodeMap& cmap) {
221 return operator=<NodeMap>(cmap);
224 template <typename CMap>
225 NodeMap& operator=(const CMap& cmap) {
226 Parent::operator=(cmap);
233 /// \ingroup semi_adaptors
235 /// \brief Digraph using a node set of another digraph or graph and
238 /// This structure can be used to establish another directed graph
239 /// over a node set of an existing one. This class uses the same
240 /// Node type as the underlying graph, and each valid node of the
241 /// original graph is valid in this arc set, therefore the node
242 /// objects of the original graph can be used directly with this
243 /// class. The node handling functions (id handling, observing, and
244 /// iterators) works equivalently as in the original graph.
246 /// This implementation is based on doubly-linked lists, from each
247 /// node the outgoing and the incoming arcs make up lists, therefore
248 /// one arc can be erased in constant time. It also makes possible,
249 /// that node can be removed from the underlying graph, in this case
250 /// all arcs incident to the given node is erased from the arc set.
252 /// \param GR The type of the graph which shares its node set with
253 /// this class. Its interface must conform to the
254 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
257 /// This class fully conforms to the \ref concepts::Digraph
258 /// "Digraph" concept.
259 template <typename GR>
260 class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > {
261 typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
265 typedef typename Parent::Node Node;
266 typedef typename Parent::Arc Arc;
268 typedef typename Parent::NodesImplBase NodesImplBase;
270 void eraseNode(const Node& node) {
272 Parent::firstOut(arc, node);
273 while (arc != INVALID ) {
275 Parent::firstOut(arc, node);
278 Parent::firstIn(arc, node);
279 while (arc != INVALID ) {
281 Parent::firstIn(arc, node);
289 class NodesImpl : public NodesImplBase {
290 typedef NodesImplBase Parent;
293 NodesImpl(const GR& graph, ListArcSet& arcset)
294 : Parent(graph), _arcset(arcset) {}
296 virtual ~NodesImpl() {}
300 virtual void erase(const Node& node) {
301 _arcset.eraseNode(node);
304 virtual void erase(const std::vector<Node>& nodes) {
305 for (int i = 0; i < int(nodes.size()); ++i) {
306 _arcset.eraseNode(nodes[i]);
308 Parent::erase(nodes);
310 virtual void clear() {
311 _arcset.clearNodes();
323 /// \brief Constructor of the ArcSet.
325 /// Constructor of the ArcSet.
326 ListArcSet(const GR& graph) : _nodes(graph, *this) {
327 Parent::initalize(graph, _nodes);
330 /// \brief Add a new arc to the digraph.
332 /// Add a new arc to the digraph with source node \c s
333 /// and target node \c t.
334 /// \return The new arc.
335 Arc addArc(const Node& s, const Node& t) {
336 return Parent::addArc(s, t);
339 /// \brief Erase an arc from the digraph.
341 /// Erase an arc \c a from the digraph.
342 void erase(const Arc& a) {
343 return Parent::erase(a);
348 template <typename GR>
349 class ListEdgeSetBase {
352 typedef typename GR::Node Node;
353 typedef typename GR::NodeIt NodeIt;
359 NodeT() : first_out(-1) {}
362 typedef typename ItemSetTraits<GR, Node>::
363 template Map<NodeT>::Type NodesImplBase;
365 NodesImplBase* _nodes;
369 int prev_out, next_out;
370 ArcT() : prev_out(-1), next_out(-1) {}
373 std::vector<ArcT> arcs;
380 void initalize(const GR& graph, NodesImplBase& nodes) {
388 friend class ListEdgeSetBase;
392 explicit Edge(int _id) { id = _id;}
396 Edge (Invalid) { id = -1; }
397 bool operator==(const Edge& arc) const {return id == arc.id;}
398 bool operator!=(const Edge& arc) const {return id != arc.id;}
399 bool operator<(const Edge& arc) const {return id < arc.id;}
403 friend class ListEdgeSetBase;
405 Arc(int _id) : id(_id) {}
408 operator Edge() const { return edgeFromId(id / 2); }
411 Arc(Invalid) : id(-1) {}
412 bool operator==(const Arc& arc) const { return id == arc.id; }
413 bool operator!=(const Arc& arc) const { return id != arc.id; }
414 bool operator<(const Arc& arc) const { return id < arc.id; }
417 ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
419 Edge addEdge(const Node& u, const Node& v) {
422 if (first_free_arc == -1) {
424 arcs.push_back(ArcT());
425 arcs.push_back(ArcT());
428 first_free_arc = arcs[n].next_out;
432 arcs[n | 1].target = v;
434 arcs[n].next_out = (*_nodes)[v].first_out;
435 if ((*_nodes)[v].first_out != -1) {
436 arcs[(*_nodes)[v].first_out].prev_out = n;
438 (*_nodes)[v].first_out = n;
439 arcs[n].prev_out = -1;
441 if ((*_nodes)[u].first_out != -1) {
442 arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
444 arcs[n | 1].next_out = (*_nodes)[u].first_out;
445 (*_nodes)[u].first_out = (n | 1);
446 arcs[n | 1].prev_out = -1;
451 void erase(const Edge& arc) {
454 if (arcs[n].next_out != -1) {
455 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
458 if (arcs[n].prev_out != -1) {
459 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
461 (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
464 if (arcs[n | 1].next_out != -1) {
465 arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
468 if (arcs[n | 1].prev_out != -1) {
469 arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
471 (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
474 arcs[n].next_out = first_free_arc;
481 for (first(node); node != INVALID; next(node)) {
482 (*_nodes)[node].first_out = -1;
489 void first(Node& node) const {
493 void next(Node& node) const {
497 void first(Arc& arc) const {
500 while (node != INVALID && (*_nodes)[node].first_out == -1) {
503 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
506 void next(Arc& arc) const {
507 if (arcs[arc.id].next_out != -1) {
508 arc.id = arcs[arc.id].next_out;
510 Node node = arcs[arc.id ^ 1].target;
512 while(node != INVALID && (*_nodes)[node].first_out == -1) {
515 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
519 void first(Edge& edge) const {
522 while (node != INVALID) {
523 edge.id = (*_nodes)[node].first_out;
524 while ((edge.id & 1) != 1) {
525 edge.id = arcs[edge.id].next_out;
536 void next(Edge& edge) const {
537 Node node = arcs[edge.id * 2].target;
538 edge.id = arcs[(edge.id * 2) | 1].next_out;
539 while ((edge.id & 1) != 1) {
540 edge.id = arcs[edge.id].next_out;
547 while (node != INVALID) {
548 edge.id = (*_nodes)[node].first_out;
549 while ((edge.id & 1) != 1) {
550 edge.id = arcs[edge.id].next_out;
561 void firstOut(Arc& arc, const Node& node) const {
562 arc.id = (*_nodes)[node].first_out;
565 void nextOut(Arc& arc) const {
566 arc.id = arcs[arc.id].next_out;
569 void firstIn(Arc& arc, const Node& node) const {
570 arc.id = (((*_nodes)[node].first_out) ^ 1);
571 if (arc.id == -2) arc.id = -1;
574 void nextIn(Arc& arc) const {
575 arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
576 if (arc.id == -2) arc.id = -1;
579 void firstInc(Edge &arc, bool& dir, const Node& node) const {
580 int de = (*_nodes)[node].first_out;
583 dir = ((de & 1) == 1);
589 void nextInc(Edge &arc, bool& dir) const {
590 int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
593 dir = ((de & 1) == 1);
600 static bool direction(Arc arc) {
601 return (arc.id & 1) == 1;
604 static Arc direct(Edge edge, bool dir) {
605 return Arc(edge.id * 2 + (dir ? 1 : 0));
608 int id(const Node& node) const { return _graph->id(node); }
609 static int id(Arc e) { return e.id; }
610 static int id(Edge e) { return e.id; }
612 Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
613 static Arc arcFromId(int id) { return Arc(id);}
614 static Edge edgeFromId(int id) { return Edge(id);}
616 int maxNodeId() const { return _graph->maxNodeId(); };
617 int maxEdgeId() const { return arcs.size() / 2 - 1; }
618 int maxArcId() const { return arcs.size()-1; }
620 Node source(Arc e) const { return arcs[e.id ^ 1].target; }
621 Node target(Arc e) const { return arcs[e.id].target; }
623 Node u(Edge e) const { return arcs[2 * e.id].target; }
624 Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
626 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
628 NodeNotifier& notifier(Node) const {
629 return _graph->notifier(Node());
632 template <typename V>
633 class NodeMap : public GR::template NodeMap<V> {
634 typedef typename GR::template NodeMap<V> Parent;
638 explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
639 : Parent(*arcset._graph) {}
641 NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
642 : Parent(*arcset._graph, value) {}
644 NodeMap& operator=(const NodeMap& cmap) {
645 return operator=<NodeMap>(cmap);
648 template <typename CMap>
649 NodeMap& operator=(const CMap& cmap) {
650 Parent::operator=(cmap);
657 /// \ingroup semi_adaptors
659 /// \brief Graph using a node set of another digraph or graph and an
662 /// This structure can be used to establish another graph over a
663 /// node set of an existing one. This class uses the same Node type
664 /// as the underlying graph, and each valid node of the original
665 /// graph is valid in this arc set, therefore the node objects of
666 /// the original graph can be used directly with this class. The
667 /// node handling functions (id handling, observing, and iterators)
668 /// works equivalently as in the original graph.
670 /// This implementation is based on doubly-linked lists, from each
671 /// node the incident edges make up lists, therefore one edge can be
672 /// erased in constant time. It also makes possible, that node can
673 /// be removed from the underlying graph, in this case all edges
674 /// incident to the given node is erased from the arc set.
676 /// \param GR The type of the graph which shares its node set
677 /// with this class. Its interface must conform to the
678 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
681 /// This class fully conforms to the \ref concepts::Graph "Graph"
683 template <typename GR>
684 class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > {
685 typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
689 typedef typename Parent::Node Node;
690 typedef typename Parent::Arc Arc;
691 typedef typename Parent::Edge Edge;
693 typedef typename Parent::NodesImplBase NodesImplBase;
695 void eraseNode(const Node& node) {
697 Parent::firstOut(arc, node);
698 while (arc != INVALID ) {
700 Parent::firstOut(arc, node);
709 class NodesImpl : public NodesImplBase {
710 typedef NodesImplBase Parent;
713 NodesImpl(const GR& graph, ListEdgeSet& arcset)
714 : Parent(graph), _arcset(arcset) {}
716 virtual ~NodesImpl() {}
720 virtual void erase(const Node& node) {
721 _arcset.eraseNode(node);
724 virtual void erase(const std::vector<Node>& nodes) {
725 for (int i = 0; i < int(nodes.size()); ++i) {
726 _arcset.eraseNode(nodes[i]);
728 Parent::erase(nodes);
730 virtual void clear() {
731 _arcset.clearNodes();
736 ListEdgeSet& _arcset;
743 /// \brief Constructor of the EdgeSet.
745 /// Constructor of the EdgeSet.
746 ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
747 Parent::initalize(graph, _nodes);
750 /// \brief Add a new edge to the graph.
752 /// Add a new edge to the graph with node \c u
753 /// and node \c v endpoints.
754 /// \return The new edge.
755 Edge addEdge(const Node& u, const Node& v) {
756 return Parent::addEdge(u, v);
759 /// \brief Erase an edge from the graph.
761 /// Erase the edge \c e from the graph.
762 void erase(const Edge& e) {
763 return Parent::erase(e);
768 template <typename GR>
769 class SmartArcSetBase {
772 typedef typename GR::Node Node;
773 typedef typename GR::NodeIt NodeIt;
778 int first_out, first_in;
779 NodeT() : first_out(-1), first_in(-1) {}
782 typedef typename ItemSetTraits<GR, Node>::
783 template Map<NodeT>::Type NodesImplBase;
785 NodesImplBase* _nodes;
789 int next_out, next_in;
793 std::vector<ArcT> arcs;
797 void initalize(const GR& graph, NodesImplBase& nodes) {
805 friend class SmartArcSetBase<GR>;
807 Arc(int _id) : id(_id) {}
811 Arc(Invalid) : id(-1) {}
812 bool operator==(const Arc& arc) const { return id == arc.id; }
813 bool operator!=(const Arc& arc) const { return id != arc.id; }
814 bool operator<(const Arc& arc) const { return id < arc.id; }
819 Arc addArc(const Node& u, const Node& v) {
821 arcs.push_back(ArcT());
822 arcs[n].next_in = (*_nodes)[v].first_in;
823 (*_nodes)[v].first_in = n;
824 arcs[n].next_out = (*_nodes)[u].first_out;
825 (*_nodes)[u].first_out = n;
833 for (first(node); node != INVALID; next(node)) {
834 (*_nodes)[node].first_in = -1;
835 (*_nodes)[node].first_out = -1;
840 void first(Node& node) const {
844 void next(Node& node) const {
848 void first(Arc& arc) const {
849 arc.id = arcs.size() - 1;
852 void next(Arc& arc) const {
856 void firstOut(Arc& arc, const Node& node) const {
857 arc.id = (*_nodes)[node].first_out;
860 void nextOut(Arc& arc) const {
861 arc.id = arcs[arc.id].next_out;
864 void firstIn(Arc& arc, const Node& node) const {
865 arc.id = (*_nodes)[node].first_in;
868 void nextIn(Arc& arc) const {
869 arc.id = arcs[arc.id].next_in;
872 int id(const Node& node) const { return _graph->id(node); }
873 int id(const Arc& arc) const { return arc.id; }
875 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
876 Arc arcFromId(int ix) const { return Arc(ix); }
878 int maxNodeId() const { return _graph->maxNodeId(); };
879 int maxArcId() const { return arcs.size() - 1; }
881 Node source(const Arc& arc) const { return arcs[arc.id].source;}
882 Node target(const Arc& arc) const { return arcs[arc.id].target;}
884 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
886 NodeNotifier& notifier(Node) const {
887 return _graph->notifier(Node());
890 template <typename V>
891 class NodeMap : public GR::template NodeMap<V> {
892 typedef typename GR::template NodeMap<V> Parent;
896 explicit NodeMap(const SmartArcSetBase<GR>& arcset)
897 : Parent(*arcset._graph) { }
899 NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
900 : Parent(*arcset._graph, value) { }
902 NodeMap& operator=(const NodeMap& cmap) {
903 return operator=<NodeMap>(cmap);
906 template <typename CMap>
907 NodeMap& operator=(const CMap& cmap) {
908 Parent::operator=(cmap);
916 /// \ingroup semi_adaptors
918 /// \brief Digraph using a node set of another digraph or graph and
921 /// This structure can be used to establish another directed graph
922 /// over a node set of an existing one. This class uses the same
923 /// Node type as the underlying graph, and each valid node of the
924 /// original graph is valid in this arc set, therefore the node
925 /// objects of the original graph can be used directly with this
926 /// class. The node handling functions (id handling, observing, and
927 /// iterators) works equivalently as in the original graph.
929 /// \param GR The type of the graph which shares its node set with
930 /// this class. Its interface must conform to the
931 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
934 /// This implementation is slightly faster than the \c ListArcSet,
935 /// because it uses continuous storage for arcs and it uses just
936 /// single-linked lists for enumerate outgoing and incoming
937 /// arcs. Therefore the arcs cannot be erased from the arc sets.
939 /// \warning If a node is erased from the underlying graph and this
940 /// node is the source or target of one arc in the arc set, then
941 /// the arc set is invalidated, and it cannot be used anymore. The
942 /// validity can be checked with the \c valid() member function.
944 /// This class fully conforms to the \ref concepts::Digraph
945 /// "Digraph" concept.
946 template <typename GR>
947 class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > {
948 typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
952 typedef typename Parent::Node Node;
953 typedef typename Parent::Arc Arc;
957 typedef typename Parent::NodesImplBase NodesImplBase;
959 void eraseNode(const Node& node) {
960 if (typename Parent::InArcIt(*this, node) == INVALID &&
961 typename Parent::OutArcIt(*this, node) == INVALID) {
964 throw typename NodesImplBase::Notifier::ImmediateDetach();
971 class NodesImpl : public NodesImplBase {
972 typedef NodesImplBase Parent;
975 NodesImpl(const GR& graph, SmartArcSet& arcset)
976 : Parent(graph), _arcset(arcset) {}
978 virtual ~NodesImpl() {}
980 bool attached() const {
981 return Parent::attached();
986 virtual void erase(const Node& node) {
988 _arcset.eraseNode(node);
990 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
995 virtual void erase(const std::vector<Node>& nodes) {
997 for (int i = 0; i < int(nodes.size()); ++i) {
998 _arcset.eraseNode(nodes[i]);
1000 Parent::erase(nodes);
1001 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1006 virtual void clear() {
1007 _arcset.clearNodes();
1012 SmartArcSet& _arcset;
1019 /// \brief Constructor of the ArcSet.
1021 /// Constructor of the ArcSet.
1022 SmartArcSet(const GR& graph) : _nodes(graph, *this) {
1023 Parent::initalize(graph, _nodes);
1026 /// \brief Add a new arc to the digraph.
1028 /// Add a new arc to the digraph with source node \c s
1029 /// and target node \c t.
1030 /// \return The new arc.
1031 Arc addArc(const Node& s, const Node& t) {
1032 return Parent::addArc(s, t);
1035 /// \brief Validity check
1037 /// This functions gives back false if the ArcSet is
1038 /// invalidated. It occurs when a node in the underlying graph is
1039 /// erased and it is not isolated in the ArcSet.
1040 bool valid() const {
1041 return _nodes.attached();
1047 template <typename GR>
1048 class SmartEdgeSetBase {
1051 typedef typename GR::Node Node;
1052 typedef typename GR::NodeIt NodeIt;
1058 NodeT() : first_out(-1) {}
1061 typedef typename ItemSetTraits<GR, Node>::
1062 template Map<NodeT>::Type NodesImplBase;
1064 NodesImplBase* _nodes;
1072 std::vector<ArcT> arcs;
1076 void initalize(const GR& graph, NodesImplBase& nodes) {
1084 friend class SmartEdgeSetBase;
1088 explicit Edge(int _id) { id = _id;}
1092 Edge (Invalid) { id = -1; }
1093 bool operator==(const Edge& arc) const {return id == arc.id;}
1094 bool operator!=(const Edge& arc) const {return id != arc.id;}
1095 bool operator<(const Edge& arc) const {return id < arc.id;}
1099 friend class SmartEdgeSetBase;
1101 Arc(int _id) : id(_id) {}
1104 operator Edge() const { return edgeFromId(id / 2); }
1107 Arc(Invalid) : id(-1) {}
1108 bool operator==(const Arc& arc) const { return id == arc.id; }
1109 bool operator!=(const Arc& arc) const { return id != arc.id; }
1110 bool operator<(const Arc& arc) const { return id < arc.id; }
1113 SmartEdgeSetBase() {}
1115 Edge addEdge(const Node& u, const Node& v) {
1116 int n = arcs.size();
1117 arcs.push_back(ArcT());
1118 arcs.push_back(ArcT());
1121 arcs[n | 1].target = v;
1123 arcs[n].next_out = (*_nodes)[v].first_out;
1124 (*_nodes)[v].first_out = n;
1126 arcs[n | 1].next_out = (*_nodes)[u].first_out;
1127 (*_nodes)[u].first_out = (n | 1);
1134 for (first(node); node != INVALID; next(node)) {
1135 (*_nodes)[node].first_out = -1;
1140 void first(Node& node) const {
1141 _graph->first(node);
1144 void next(Node& node) const {
1148 void first(Arc& arc) const {
1149 arc.id = arcs.size() - 1;
1152 void next(Arc& arc) const {
1156 void first(Edge& arc) const {
1157 arc.id = arcs.size() / 2 - 1;
1160 void next(Edge& arc) const {
1164 void firstOut(Arc& arc, const Node& node) const {
1165 arc.id = (*_nodes)[node].first_out;
1168 void nextOut(Arc& arc) const {
1169 arc.id = arcs[arc.id].next_out;
1172 void firstIn(Arc& arc, const Node& node) const {
1173 arc.id = (((*_nodes)[node].first_out) ^ 1);
1174 if (arc.id == -2) arc.id = -1;
1177 void nextIn(Arc& arc) const {
1178 arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
1179 if (arc.id == -2) arc.id = -1;
1182 void firstInc(Edge &arc, bool& dir, const Node& node) const {
1183 int de = (*_nodes)[node].first_out;
1186 dir = ((de & 1) == 1);
1192 void nextInc(Edge &arc, bool& dir) const {
1193 int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
1196 dir = ((de & 1) == 1);
1203 static bool direction(Arc arc) {
1204 return (arc.id & 1) == 1;
1207 static Arc direct(Edge edge, bool dir) {
1208 return Arc(edge.id * 2 + (dir ? 1 : 0));
1211 int id(Node node) const { return _graph->id(node); }
1212 static int id(Arc arc) { return arc.id; }
1213 static int id(Edge arc) { return arc.id; }
1215 Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
1216 static Arc arcFromId(int id) { return Arc(id); }
1217 static Edge edgeFromId(int id) { return Edge(id);}
1219 int maxNodeId() const { return _graph->maxNodeId(); };
1220 int maxArcId() const { return arcs.size() - 1; }
1221 int maxEdgeId() const { return arcs.size() / 2 - 1; }
1223 Node source(Arc e) const { return arcs[e.id ^ 1].target; }
1224 Node target(Arc e) const { return arcs[e.id].target; }
1226 Node u(Edge e) const { return arcs[2 * e.id].target; }
1227 Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
1229 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
1231 NodeNotifier& notifier(Node) const {
1232 return _graph->notifier(Node());
1235 template <typename V>
1236 class NodeMap : public GR::template NodeMap<V> {
1237 typedef typename GR::template NodeMap<V> Parent;
1241 explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
1242 : Parent(*arcset._graph) { }
1244 NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
1245 : Parent(*arcset._graph, value) { }
1247 NodeMap& operator=(const NodeMap& cmap) {
1248 return operator=<NodeMap>(cmap);
1251 template <typename CMap>
1252 NodeMap& operator=(const CMap& cmap) {
1253 Parent::operator=(cmap);
1260 /// \ingroup semi_adaptors
1262 /// \brief Graph using a node set of another digraph or graph and an
1265 /// This structure can be used to establish another graph over a
1266 /// node set of an existing one. This class uses the same Node type
1267 /// as the underlying graph, and each valid node of the original
1268 /// graph is valid in this arc set, therefore the node objects of
1269 /// the original graph can be used directly with this class. The
1270 /// node handling functions (id handling, observing, and iterators)
1271 /// works equivalently as in the original graph.
1273 /// \param GR The type of the graph which shares its node set
1274 /// with this class. Its interface must conform to the
1275 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
1278 /// This implementation is slightly faster than the \c ListEdgeSet,
1279 /// because it uses continuous storage for edges and it uses just
1280 /// single-linked lists for enumerate incident edges. Therefore the
1281 /// edges cannot be erased from the edge sets.
1283 /// \warning If a node is erased from the underlying graph and this
1284 /// node is incident to one edge in the edge set, then the edge set
1285 /// is invalidated, and it cannot be used anymore. The validity can
1286 /// be checked with the \c valid() member function.
1288 /// This class fully conforms to the \ref concepts::Graph
1289 /// "Graph" concept.
1290 template <typename GR>
1291 class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > {
1292 typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
1296 typedef typename Parent::Node Node;
1297 typedef typename Parent::Arc Arc;
1298 typedef typename Parent::Edge Edge;
1302 typedef typename Parent::NodesImplBase NodesImplBase;
1304 void eraseNode(const Node& node) {
1305 if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
1308 throw typename NodesImplBase::Notifier::ImmediateDetach();
1315 class NodesImpl : public NodesImplBase {
1316 typedef NodesImplBase Parent;
1319 NodesImpl(const GR& graph, SmartEdgeSet& arcset)
1320 : Parent(graph), _arcset(arcset) {}
1322 virtual ~NodesImpl() {}
1324 bool attached() const {
1325 return Parent::attached();
1330 virtual void erase(const Node& node) {
1332 _arcset.eraseNode(node);
1333 Parent::erase(node);
1334 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1339 virtual void erase(const std::vector<Node>& nodes) {
1341 for (int i = 0; i < int(nodes.size()); ++i) {
1342 _arcset.eraseNode(nodes[i]);
1344 Parent::erase(nodes);
1345 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1350 virtual void clear() {
1351 _arcset.clearNodes();
1356 SmartEdgeSet& _arcset;
1363 /// \brief Constructor of the EdgeSet.
1365 /// Constructor of the EdgeSet.
1366 SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
1367 Parent::initalize(graph, _nodes);
1370 /// \brief Add a new edge to the graph.
1372 /// Add a new edge to the graph with node \c u
1373 /// and node \c v endpoints.
1374 /// \return The new edge.
1375 Edge addEdge(const Node& u, const Node& v) {
1376 return Parent::addEdge(u, v);
1379 /// \brief Validity check
1381 /// This functions gives back false if the EdgeSet is
1382 /// invalidated. It occurs when a node in the underlying graph is
1383 /// erased and it is not isolated in the EdgeSet.
1384 bool valid() const {
1385 return _nodes.attached();