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_BITS_EDGE_SET_EXTENDER_H
20 #define LEMON_BITS_EDGE_SET_EXTENDER_H
22 #include <lemon/core.h>
23 #include <lemon/error.h>
24 #include <lemon/bits/default_map.h>
25 #include <lemon/bits/map_extender.h>
27 //\ingroup digraphbits
29 //\brief Extenders for the arc set types
32 // \ingroup digraphbits
34 // \brief Extender for the ArcSets
35 template <typename Base>
36 class ArcSetExtender : public Base {
41 typedef ArcSetExtender Digraph;
45 typedef typename Parent::Node Node;
46 typedef typename Parent::Arc Arc;
48 int maxId(Node) const {
49 return Parent::maxNodeId();
52 int maxId(Arc) const {
53 return Parent::maxArcId();
56 Node fromId(int id, Node) const {
57 return Parent::nodeFromId(id);
60 Arc fromId(int id, Arc) const {
61 return Parent::arcFromId(id);
64 Node oppositeNode(const Node &n, const Arc &e) const {
65 if (n == Parent::source(e))
66 return Parent::target(e);
67 else if(n==Parent::target(e))
68 return Parent::source(e);
74 // Alteration notifier extensions
76 // The arc observer registry.
77 typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier;
81 mutable ArcNotifier arc_notifier;
85 using Parent::notifier;
87 // Gives back the arc alteration notifier.
88 ArcNotifier& notifier(Arc) const {
92 // Iterable extensions
94 class NodeIt : public Node {
95 const Digraph* digraph;
100 NodeIt(Invalid i) : Node(i) { }
102 explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
103 _graph.first(static_cast<Node&>(*this));
106 NodeIt(const Digraph& _graph, const Node& node)
107 : Node(node), digraph(&_graph) {}
109 NodeIt& operator++() {
110 digraph->next(*this);
117 class ArcIt : public Arc {
118 const Digraph* digraph;
123 ArcIt(Invalid i) : Arc(i) { }
125 explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
126 _graph.first(static_cast<Arc&>(*this));
129 ArcIt(const Digraph& _graph, const Arc& e) :
130 Arc(e), digraph(&_graph) { }
132 ArcIt& operator++() {
133 digraph->next(*this);
140 class OutArcIt : public Arc {
141 const Digraph* digraph;
146 OutArcIt(Invalid i) : Arc(i) { }
148 OutArcIt(const Digraph& _graph, const Node& node)
150 _graph.firstOut(*this, node);
153 OutArcIt(const Digraph& _graph, const Arc& arc)
154 : Arc(arc), digraph(&_graph) {}
156 OutArcIt& operator++() {
157 digraph->nextOut(*this);
164 class InArcIt : public Arc {
165 const Digraph* digraph;
170 InArcIt(Invalid i) : Arc(i) { }
172 InArcIt(const Digraph& _graph, const Node& node)
174 _graph.firstIn(*this, node);
177 InArcIt(const Digraph& _graph, const Arc& arc) :
178 Arc(arc), digraph(&_graph) {}
180 InArcIt& operator++() {
181 digraph->nextIn(*this);
187 // \brief Base node of the iterator
189 // Returns the base node (ie. the source in this case) of the iterator
190 Node baseNode(const OutArcIt &e) const {
191 return Parent::source(static_cast<const Arc&>(e));
193 // \brief Running node of the iterator
195 // Returns the running node (ie. the target in this case) of the
197 Node runningNode(const OutArcIt &e) const {
198 return Parent::target(static_cast<const Arc&>(e));
201 // \brief Base node of the iterator
203 // Returns the base node (ie. the target in this case) of the iterator
204 Node baseNode(const InArcIt &e) const {
205 return Parent::target(static_cast<const Arc&>(e));
207 // \brief Running node of the iterator
209 // Returns the running node (ie. the source in this case) of the
211 Node runningNode(const InArcIt &e) const {
212 return Parent::source(static_cast<const Arc&>(e));
217 // Mappable extension
219 template <typename _Value>
221 : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
222 typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
225 explicit ArcMap(const Digraph& _g)
227 ArcMap(const Digraph& _g, const _Value& _v)
230 ArcMap& operator=(const ArcMap& cmap) {
231 return operator=<ArcMap>(cmap);
234 template <typename CMap>
235 ArcMap& operator=(const CMap& cmap) {
236 Parent::operator=(cmap);
243 // Alteration extension
245 Arc addArc(const Node& from, const Node& to) {
246 Arc arc = Parent::addArc(from, to);
247 notifier(Arc()).add(arc);
252 notifier(Arc()).clear();
256 void erase(const Arc& arc) {
257 notifier(Arc()).erase(arc);
262 arc_notifier.setContainer(*this);
266 arc_notifier.clear();
272 // \ingroup digraphbits
274 // \brief Extender for the EdgeSets
275 template <typename Base>
276 class EdgeSetExtender : public Base {
281 typedef EdgeSetExtender Graph;
283 typedef True UndirectedTag;
285 typedef typename Parent::Node Node;
286 typedef typename Parent::Arc Arc;
287 typedef typename Parent::Edge Edge;
289 int maxId(Node) const {
290 return Parent::maxNodeId();
293 int maxId(Arc) const {
294 return Parent::maxArcId();
297 int maxId(Edge) const {
298 return Parent::maxEdgeId();
301 Node fromId(int id, Node) const {
302 return Parent::nodeFromId(id);
305 Arc fromId(int id, Arc) const {
306 return Parent::arcFromId(id);
309 Edge fromId(int id, Edge) const {
310 return Parent::edgeFromId(id);
313 Node oppositeNode(const Node &n, const Edge &e) const {
314 if( n == Parent::u(e))
316 else if( n == Parent::v(e))
322 Arc oppositeArc(const Arc &e) const {
323 return Parent::direct(e, !Parent::direction(e));
326 using Parent::direct;
327 Arc direct(const Edge &e, const Node &s) const {
328 return Parent::direct(e, Parent::u(e) == s);
331 typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
332 typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
337 mutable ArcNotifier arc_notifier;
338 mutable EdgeNotifier edge_notifier;
342 using Parent::notifier;
344 ArcNotifier& notifier(Arc) const {
348 EdgeNotifier& notifier(Edge) const {
349 return edge_notifier;
353 class NodeIt : public Node {
359 NodeIt(Invalid i) : Node(i) { }
361 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
362 _graph.first(static_cast<Node&>(*this));
365 NodeIt(const Graph& _graph, const Node& node)
366 : Node(node), graph(&_graph) {}
368 NodeIt& operator++() {
376 class ArcIt : public Arc {
382 ArcIt(Invalid i) : Arc(i) { }
384 explicit ArcIt(const Graph& _graph) : graph(&_graph) {
385 _graph.first(static_cast<Arc&>(*this));
388 ArcIt(const Graph& _graph, const Arc& e) :
389 Arc(e), graph(&_graph) { }
391 ArcIt& operator++() {
399 class OutArcIt : public Arc {
405 OutArcIt(Invalid i) : Arc(i) { }
407 OutArcIt(const Graph& _graph, const Node& node)
409 _graph.firstOut(*this, node);
412 OutArcIt(const Graph& _graph, const Arc& arc)
413 : Arc(arc), graph(&_graph) {}
415 OutArcIt& operator++() {
416 graph->nextOut(*this);
423 class InArcIt : public Arc {
429 InArcIt(Invalid i) : Arc(i) { }
431 InArcIt(const Graph& _graph, const Node& node)
433 _graph.firstIn(*this, node);
436 InArcIt(const Graph& _graph, const Arc& arc) :
437 Arc(arc), graph(&_graph) {}
439 InArcIt& operator++() {
440 graph->nextIn(*this);
447 class EdgeIt : public Parent::Edge {
453 EdgeIt(Invalid i) : Edge(i) { }
455 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
456 _graph.first(static_cast<Edge&>(*this));
459 EdgeIt(const Graph& _graph, const Edge& e) :
460 Edge(e), graph(&_graph) { }
462 EdgeIt& operator++() {
469 class IncEdgeIt : public Parent::Edge {
470 friend class EdgeSetExtender;
477 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
479 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
480 _graph.firstInc(*this, direction, n);
483 IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
484 : graph(&_graph), Edge(ue) {
485 direction = (_graph.source(ue) == n);
488 IncEdgeIt& operator++() {
489 graph->nextInc(*this, direction);
494 // \brief Base node of the iterator
496 // Returns the base node (ie. the source in this case) of the iterator
497 Node baseNode(const OutArcIt &e) const {
498 return Parent::source(static_cast<const Arc&>(e));
500 // \brief Running node of the iterator
502 // Returns the running node (ie. the target in this case) of the
504 Node runningNode(const OutArcIt &e) const {
505 return Parent::target(static_cast<const Arc&>(e));
508 // \brief Base node of the iterator
510 // Returns the base node (ie. the target in this case) of the iterator
511 Node baseNode(const InArcIt &e) const {
512 return Parent::target(static_cast<const Arc&>(e));
514 // \brief Running node of the iterator
516 // Returns the running node (ie. the source in this case) of the
518 Node runningNode(const InArcIt &e) const {
519 return Parent::source(static_cast<const Arc&>(e));
522 // Base node of the iterator
524 // Returns the base node of the iterator
525 Node baseNode(const IncEdgeIt &e) const {
526 return e.direction ? u(e) : v(e);
528 // Running node of the iterator
530 // Returns the running node of the iterator
531 Node runningNode(const IncEdgeIt &e) const {
532 return e.direction ? v(e) : u(e);
536 template <typename _Value>
538 : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
539 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
542 explicit ArcMap(const Graph& _g)
544 ArcMap(const Graph& _g, const _Value& _v)
547 ArcMap& operator=(const ArcMap& cmap) {
548 return operator=<ArcMap>(cmap);
551 template <typename CMap>
552 ArcMap& operator=(const CMap& cmap) {
553 Parent::operator=(cmap);
560 template <typename _Value>
562 : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
563 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
566 explicit EdgeMap(const Graph& _g)
569 EdgeMap(const Graph& _g, const _Value& _v)
572 EdgeMap& operator=(const EdgeMap& cmap) {
573 return operator=<EdgeMap>(cmap);
576 template <typename CMap>
577 EdgeMap& operator=(const CMap& cmap) {
578 Parent::operator=(cmap);
585 // Alteration extension
587 Edge addEdge(const Node& from, const Node& to) {
588 Edge edge = Parent::addEdge(from, to);
589 notifier(Edge()).add(edge);
590 std::vector<Arc> arcs;
591 arcs.push_back(Parent::direct(edge, true));
592 arcs.push_back(Parent::direct(edge, false));
593 notifier(Arc()).add(arcs);
598 notifier(Arc()).clear();
599 notifier(Edge()).clear();
603 void erase(const Edge& edge) {
604 std::vector<Arc> arcs;
605 arcs.push_back(Parent::direct(edge, true));
606 arcs.push_back(Parent::direct(edge, false));
607 notifier(Arc()).erase(arcs);
608 notifier(Edge()).erase(edge);
614 arc_notifier.setContainer(*this);
615 edge_notifier.setContainer(*this);
619 edge_notifier.clear();
620 arc_notifier.clear();