3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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/bits/invalid.h>
23 #include <lemon/error.h>
25 #include <lemon/bits/default_map.h>
29 ///\brief Extenders for the edge set types
32 /// \ingroup graphbits
34 /// \brief Extender for the EdgeSets
35 template <typename Base>
36 class EdgeSetExtender : public Base {
40 typedef EdgeSetExtender Graph;
44 typedef typename Parent::Node Node;
45 typedef typename Parent::Edge Edge;
47 int maxId(Node) const {
48 return Parent::maxNodeId();
51 int maxId(Edge) const {
52 return Parent::maxEdgeId();
55 Node fromId(int id, Node) const {
56 return Parent::nodeFromId(id);
59 Edge fromId(int id, Edge) const {
60 return Parent::edgeFromId(id);
63 Node oppositeNode(const Node &n, const Edge &e) const {
64 if (n == Parent::source(e))
65 return Parent::target(e);
66 else if(n==Parent::target(e))
67 return Parent::source(e);
73 // Alteration notifier extensions
75 /// The edge observer registry.
76 typedef AlterationNotifier<Edge> EdgeNotifier;
80 mutable EdgeNotifier edge_notifier;
84 using Parent::getNotifier;
86 /// \brief Gives back the edge alteration notifier.
88 /// Gives back the edge alteration notifier.
89 EdgeNotifier& getNotifier(Edge) const {
93 // Iterable extensions
95 class NodeIt : public Node {
101 NodeIt(Invalid i) : Node(i) { }
103 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
104 _graph.first(*static_cast<Node*>(this));
107 NodeIt(const Graph& _graph, const Node& node)
108 : Node(node), graph(&_graph) {}
110 NodeIt& operator++() {
118 class EdgeIt : public Edge {
124 EdgeIt(Invalid i) : Edge(i) { }
126 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
127 _graph.first(*static_cast<Edge*>(this));
130 EdgeIt(const Graph& _graph, const Edge& e) :
131 Edge(e), graph(&_graph) { }
133 EdgeIt& operator++() {
141 class OutEdgeIt : public Edge {
147 OutEdgeIt(Invalid i) : Edge(i) { }
149 OutEdgeIt(const Graph& _graph, const Node& node)
151 _graph.firstOut(*this, node);
154 OutEdgeIt(const Graph& _graph, const Edge& edge)
155 : Edge(edge), graph(&_graph) {}
157 OutEdgeIt& operator++() {
158 graph->nextOut(*this);
165 class InEdgeIt : public Edge {
171 InEdgeIt(Invalid i) : Edge(i) { }
173 InEdgeIt(const Graph& _graph, const Node& node)
175 _graph.firstIn(*this, node);
178 InEdgeIt(const Graph& _graph, const Edge& edge) :
179 Edge(edge), graph(&_graph) {}
181 InEdgeIt& operator++() {
182 graph->nextIn(*this);
188 /// \brief Base node of the iterator
190 /// Returns the base node (ie. the source in this case) of the iterator
191 Node baseNode(const OutEdgeIt &e) const {
192 return Parent::source((Edge)e);
194 /// \brief Running node of the iterator
196 /// Returns the running node (ie. the target in this case) of the
198 Node runningNode(const OutEdgeIt &e) const {
199 return Parent::target((Edge)e);
202 /// \brief Base node of the iterator
204 /// Returns the base node (ie. the target in this case) of the iterator
205 Node baseNode(const InEdgeIt &e) const {
206 return Parent::target((Edge)e);
208 /// \brief Running node of the iterator
210 /// Returns the running node (ie. the source in this case) of the
212 Node runningNode(const InEdgeIt &e) const {
213 return Parent::source((Edge)e);
218 // Mappable extension
220 template <typename _Value>
222 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
224 typedef EdgeSetExtender Graph;
225 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
227 EdgeMap(const Graph& _g)
229 EdgeMap(const Graph& _g, const _Value& _v)
232 EdgeMap& operator=(const EdgeMap& cmap) {
233 return operator=<EdgeMap>(cmap);
236 template <typename CMap>
237 EdgeMap& operator=(const CMap& cmap) {
238 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
239 const typename Parent::Graph* graph = Parent::getGraph();
241 for (graph->first(it); it != INVALID; graph->next(it)) {
242 Parent::set(it, cmap[it]);
249 // Alteration extension
251 Edge addEdge(const Node& from, const Node& to) {
252 Edge edge = Parent::addEdge(from, to);
253 getNotifier(Edge()).add(edge);
258 getNotifier(Edge()).clear();
262 void erase(const Edge& edge) {
263 getNotifier(Edge()).erase(edge);
269 edge_notifier.clear();
275 /// \ingroup graphbits
277 /// \brief Extender for the UEdgeSets
278 template <typename Base>
279 class UEdgeSetExtender : public Base {
284 typedef UEdgeSetExtender Graph;
286 typedef typename Parent::Node Node;
287 typedef typename Parent::Edge Edge;
288 typedef typename Parent::UEdge UEdge;
291 int maxId(Node) const {
292 return Parent::maxNodeId();
295 int maxId(Edge) const {
296 return Parent::maxEdgeId();
299 int maxId(UEdge) const {
300 return Parent::maxUEdgeId();
303 Node fromId(int id, Node) const {
304 return Parent::nodeFromId(id);
307 Edge fromId(int id, Edge) const {
308 return Parent::edgeFromId(id);
311 UEdge fromId(int id, UEdge) const {
312 return Parent::uEdgeFromId(id);
315 Node oppositeNode(const Node &n, const UEdge &e) const {
316 if( n == Parent::source(e))
317 return Parent::target(e);
318 else if( n == Parent::target(e))
319 return Parent::source(e);
324 Edge oppositeEdge(const Edge &e) const {
325 return Parent::direct(e, !Parent::direction(e));
328 using Parent::direct;
329 Edge direct(const UEdge &ue, const Node &s) const {
330 return Parent::direct(ue, Parent::source(ue) == s);
333 typedef AlterationNotifier<Edge> EdgeNotifier;
334 typedef AlterationNotifier<UEdge> UEdgeNotifier;
339 mutable EdgeNotifier edge_notifier;
340 mutable UEdgeNotifier uedge_notifier;
344 using Parent::getNotifier;
346 EdgeNotifier& getNotifier(Edge) const {
347 return edge_notifier;
350 UEdgeNotifier& getNotifier(UEdge) const {
351 return uedge_notifier;
355 class NodeIt : public Node {
361 NodeIt(Invalid i) : Node(i) { }
363 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
364 _graph.first(*static_cast<Node*>(this));
367 NodeIt(const Graph& _graph, const Node& node)
368 : Node(node), graph(&_graph) {}
370 NodeIt& operator++() {
378 class EdgeIt : public Edge {
384 EdgeIt(Invalid i) : Edge(i) { }
386 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
387 _graph.first(*static_cast<Edge*>(this));
390 EdgeIt(const Graph& _graph, const Edge& e) :
391 Edge(e), graph(&_graph) { }
393 EdgeIt& operator++() {
401 class OutEdgeIt : public Edge {
407 OutEdgeIt(Invalid i) : Edge(i) { }
409 OutEdgeIt(const Graph& _graph, const Node& node)
411 _graph.firstOut(*this, node);
414 OutEdgeIt(const Graph& _graph, const Edge& edge)
415 : Edge(edge), graph(&_graph) {}
417 OutEdgeIt& operator++() {
418 graph->nextOut(*this);
425 class InEdgeIt : public Edge {
431 InEdgeIt(Invalid i) : Edge(i) { }
433 InEdgeIt(const Graph& _graph, const Node& node)
435 _graph.firstIn(*this, node);
438 InEdgeIt(const Graph& _graph, const Edge& edge) :
439 Edge(edge), graph(&_graph) {}
441 InEdgeIt& operator++() {
442 graph->nextIn(*this);
449 class UEdgeIt : public Parent::UEdge {
455 UEdgeIt(Invalid i) : UEdge(i) { }
457 explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
458 _graph.first(*static_cast<UEdge*>(this));
461 UEdgeIt(const Graph& _graph, const UEdge& e) :
462 UEdge(e), graph(&_graph) { }
464 UEdgeIt& operator++() {
471 class IncEdgeIt : public Parent::UEdge {
472 friend class UEdgeSetExtender;
479 IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
481 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
482 _graph.firstInc(*this, direction, n);
485 IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
486 : graph(&_graph), UEdge(ue) {
487 direction = (_graph.source(ue) == n);
490 IncEdgeIt& operator++() {
491 graph->nextInc(*this, direction);
496 /// \brief Base node of the iterator
498 /// Returns the base node (ie. the source in this case) of the iterator
499 Node baseNode(const OutEdgeIt &e) const {
500 return Parent::source((Edge)e);
502 /// \brief Running node of the iterator
504 /// Returns the running node (ie. the target in this case) of the
506 Node runningNode(const OutEdgeIt &e) const {
507 return Parent::target((Edge)e);
510 /// \brief Base node of the iterator
512 /// Returns the base node (ie. the target in this case) of the iterator
513 Node baseNode(const InEdgeIt &e) const {
514 return Parent::target((Edge)e);
516 /// \brief Running node of the iterator
518 /// Returns the running node (ie. the source in this case) of the
520 Node runningNode(const InEdgeIt &e) const {
521 return Parent::source((Edge)e);
524 /// Base node of the iterator
526 /// Returns the base node of the iterator
527 Node baseNode(const IncEdgeIt &e) const {
528 return e.direction ? source(e) : target(e);
530 /// Running node of the iterator
532 /// Returns the running node of the iterator
533 Node runningNode(const IncEdgeIt &e) const {
534 return e.direction ? target(e) : source(e);
538 template <typename _Value>
540 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
542 typedef UEdgeSetExtender Graph;
543 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
545 EdgeMap(const Graph& _g)
547 EdgeMap(const Graph& _g, const _Value& _v)
550 EdgeMap& operator=(const EdgeMap& cmap) {
551 return operator=<EdgeMap>(cmap);
554 template <typename CMap>
555 EdgeMap& operator=(const CMap& cmap) {
556 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
557 const typename Parent::Graph* graph = Parent::getGraph();
559 for (graph->first(it); it != INVALID; graph->next(it)) {
560 Parent::set(it, cmap[it]);
567 template <typename _Value>
569 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
571 typedef UEdgeSetExtender Graph;
572 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
574 UEdgeMap(const Graph& _g)
576 UEdgeMap(const Graph& _g, const _Value& _v)
579 UEdgeMap& operator=(const UEdgeMap& cmap) {
580 return operator=<UEdgeMap>(cmap);
583 template <typename CMap>
584 UEdgeMap& operator=(const CMap& cmap) {
585 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
586 const typename Parent::Graph* graph = Parent::getGraph();
588 for (graph->first(it); it != INVALID; graph->next(it)) {
589 Parent::set(it, cmap[it]);
596 // Alteration extension
598 UEdge addEdge(const Node& from, const Node& to) {
599 UEdge uedge = Parent::addEdge(from, to);
600 getNotifier(UEdge()).add(uedge);
601 getNotifier(Edge()).add(Parent::direct(uedge, true));
602 getNotifier(Edge()).add(Parent::direct(uedge, false));
607 getNotifier(Edge()).clear();
608 getNotifier(UEdge()).clear();
612 void erase(const UEdge& uedge) {
613 getNotifier(Edge()).erase(Parent::direct(uedge, true));
614 getNotifier(Edge()).erase(Parent::direct(uedge, false));
615 getNotifier(UEdge()).erase(uedge);
616 Parent::erase(uedge);
620 ~UEdgeSetExtender() {
621 getNotifier(Edge()).clear();
622 getNotifier(UEdge()).clear();