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 typename Parent::Node Node;
284 typedef typename Parent::Arc Arc;
285 typedef typename Parent::Edge Edge;
287 int maxId(Node) const {
288 return Parent::maxNodeId();
291 int maxId(Arc) const {
292 return Parent::maxArcId();
295 int maxId(Edge) const {
296 return Parent::maxEdgeId();
299 Node fromId(int id, Node) const {
300 return Parent::nodeFromId(id);
303 Arc fromId(int id, Arc) const {
304 return Parent::arcFromId(id);
307 Edge fromId(int id, Edge) const {
308 return Parent::edgeFromId(id);
311 Node oppositeNode(const Node &n, const Edge &e) const {
312 if( n == Parent::u(e))
314 else if( n == Parent::v(e))
320 Arc oppositeArc(const Arc &e) const {
321 return Parent::direct(e, !Parent::direction(e));
324 using Parent::direct;
325 Arc direct(const Edge &e, const Node &s) const {
326 return Parent::direct(e, Parent::u(e) == s);
329 typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
330 typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
335 mutable ArcNotifier arc_notifier;
336 mutable EdgeNotifier edge_notifier;
340 using Parent::notifier;
342 ArcNotifier& notifier(Arc) const {
346 EdgeNotifier& notifier(Edge) const {
347 return edge_notifier;
351 class NodeIt : public Node {
357 NodeIt(Invalid i) : Node(i) { }
359 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
360 _graph.first(static_cast<Node&>(*this));
363 NodeIt(const Graph& _graph, const Node& node)
364 : Node(node), graph(&_graph) {}
366 NodeIt& operator++() {
374 class ArcIt : public Arc {
380 ArcIt(Invalid i) : Arc(i) { }
382 explicit ArcIt(const Graph& _graph) : graph(&_graph) {
383 _graph.first(static_cast<Arc&>(*this));
386 ArcIt(const Graph& _graph, const Arc& e) :
387 Arc(e), graph(&_graph) { }
389 ArcIt& operator++() {
397 class OutArcIt : public Arc {
403 OutArcIt(Invalid i) : Arc(i) { }
405 OutArcIt(const Graph& _graph, const Node& node)
407 _graph.firstOut(*this, node);
410 OutArcIt(const Graph& _graph, const Arc& arc)
411 : Arc(arc), graph(&_graph) {}
413 OutArcIt& operator++() {
414 graph->nextOut(*this);
421 class InArcIt : public Arc {
427 InArcIt(Invalid i) : Arc(i) { }
429 InArcIt(const Graph& _graph, const Node& node)
431 _graph.firstIn(*this, node);
434 InArcIt(const Graph& _graph, const Arc& arc) :
435 Arc(arc), graph(&_graph) {}
437 InArcIt& operator++() {
438 graph->nextIn(*this);
445 class EdgeIt : public Parent::Edge {
451 EdgeIt(Invalid i) : Edge(i) { }
453 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
454 _graph.first(static_cast<Edge&>(*this));
457 EdgeIt(const Graph& _graph, const Edge& e) :
458 Edge(e), graph(&_graph) { }
460 EdgeIt& operator++() {
467 class IncEdgeIt : public Parent::Edge {
468 friend class EdgeSetExtender;
475 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
477 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
478 _graph.firstInc(*this, direction, n);
481 IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
482 : graph(&_graph), Edge(ue) {
483 direction = (_graph.source(ue) == n);
486 IncEdgeIt& operator++() {
487 graph->nextInc(*this, direction);
492 // \brief Base node of the iterator
494 // Returns the base node (ie. the source in this case) of the iterator
495 Node baseNode(const OutArcIt &e) const {
496 return Parent::source(static_cast<const Arc&>(e));
498 // \brief Running node of the iterator
500 // Returns the running node (ie. the target in this case) of the
502 Node runningNode(const OutArcIt &e) const {
503 return Parent::target(static_cast<const Arc&>(e));
506 // \brief Base node of the iterator
508 // Returns the base node (ie. the target in this case) of the iterator
509 Node baseNode(const InArcIt &e) const {
510 return Parent::target(static_cast<const Arc&>(e));
512 // \brief Running node of the iterator
514 // Returns the running node (ie. the source in this case) of the
516 Node runningNode(const InArcIt &e) const {
517 return Parent::source(static_cast<const Arc&>(e));
520 // Base node of the iterator
522 // Returns the base node of the iterator
523 Node baseNode(const IncEdgeIt &e) const {
524 return e.direction ? u(e) : v(e);
526 // Running node of the iterator
528 // Returns the running node of the iterator
529 Node runningNode(const IncEdgeIt &e) const {
530 return e.direction ? v(e) : u(e);
534 template <typename _Value>
536 : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
537 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
540 explicit ArcMap(const Graph& _g)
542 ArcMap(const Graph& _g, const _Value& _v)
545 ArcMap& operator=(const ArcMap& cmap) {
546 return operator=<ArcMap>(cmap);
549 template <typename CMap>
550 ArcMap& operator=(const CMap& cmap) {
551 Parent::operator=(cmap);
558 template <typename _Value>
560 : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
561 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
564 explicit EdgeMap(const Graph& _g)
567 EdgeMap(const Graph& _g, const _Value& _v)
570 EdgeMap& operator=(const EdgeMap& cmap) {
571 return operator=<EdgeMap>(cmap);
574 template <typename CMap>
575 EdgeMap& operator=(const CMap& cmap) {
576 Parent::operator=(cmap);
583 // Alteration extension
585 Edge addEdge(const Node& from, const Node& to) {
586 Edge edge = Parent::addEdge(from, to);
587 notifier(Edge()).add(edge);
588 std::vector<Arc> arcs;
589 arcs.push_back(Parent::direct(edge, true));
590 arcs.push_back(Parent::direct(edge, false));
591 notifier(Arc()).add(arcs);
596 notifier(Arc()).clear();
597 notifier(Edge()).clear();
601 void erase(const Edge& edge) {
602 std::vector<Arc> arcs;
603 arcs.push_back(Parent::direct(edge, true));
604 arcs.push_back(Parent::direct(edge, false));
605 notifier(Arc()).erase(arcs);
606 notifier(Edge()).erase(edge);
612 arc_notifier.setContainer(*this);
613 edge_notifier.setContainer(*this);
617 edge_notifier.clear();
618 arc_notifier.clear();