1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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);
116 LemonRangeWrapper1<NodeIt, Digraph> nodes() const {
117 return LemonRangeWrapper1<NodeIt, Digraph>(*this);
120 class ArcIt : public Arc {
121 const Digraph* digraph;
126 ArcIt(Invalid i) : Arc(i) { }
128 explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
129 _graph.first(static_cast<Arc&>(*this));
132 ArcIt(const Digraph& _graph, const Arc& e) :
133 Arc(e), digraph(&_graph) { }
135 ArcIt& operator++() {
136 digraph->next(*this);
142 LemonRangeWrapper1<ArcIt, Digraph> arcs() const {
143 return LemonRangeWrapper1<ArcIt, Digraph>(*this);
146 class OutArcIt : public Arc {
147 const Digraph* digraph;
152 OutArcIt(Invalid i) : Arc(i) { }
154 OutArcIt(const Digraph& _graph, const Node& node)
156 _graph.firstOut(*this, node);
159 OutArcIt(const Digraph& _graph, const Arc& arc)
160 : Arc(arc), digraph(&_graph) {}
162 OutArcIt& operator++() {
163 digraph->nextOut(*this);
169 LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const {
170 return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u);
173 class InArcIt : public Arc {
174 const Digraph* digraph;
179 InArcIt(Invalid i) : Arc(i) { }
181 InArcIt(const Digraph& _graph, const Node& node)
183 _graph.firstIn(*this, node);
186 InArcIt(const Digraph& _graph, const Arc& arc) :
187 Arc(arc), digraph(&_graph) {}
189 InArcIt& operator++() {
190 digraph->nextIn(*this);
196 LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const {
197 return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u);
200 // \brief Base node of the iterator
202 // Returns the base node (ie. the source in this case) of the iterator
203 Node baseNode(const OutArcIt &e) const {
204 return Parent::source(static_cast<const Arc&>(e));
206 // \brief Running node of the iterator
208 // Returns the running node (ie. the target in this case) of the
210 Node runningNode(const OutArcIt &e) const {
211 return Parent::target(static_cast<const Arc&>(e));
214 // \brief Base node of the iterator
216 // Returns the base node (ie. the target in this case) of the iterator
217 Node baseNode(const InArcIt &e) const {
218 return Parent::target(static_cast<const Arc&>(e));
220 // \brief Running node of the iterator
222 // Returns the running node (ie. the source in this case) of the
224 Node runningNode(const InArcIt &e) const {
225 return Parent::source(static_cast<const Arc&>(e));
230 // Mappable extension
232 template <typename _Value>
234 : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
235 typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
238 explicit ArcMap(const Digraph& _g)
240 ArcMap(const Digraph& _g, const _Value& _v)
243 ArcMap& operator=(const ArcMap& cmap) {
244 return operator=<ArcMap>(cmap);
247 template <typename CMap>
248 ArcMap& operator=(const CMap& cmap) {
249 Parent::operator=(cmap);
256 // Alteration extension
258 Arc addArc(const Node& from, const Node& to) {
259 Arc arc = Parent::addArc(from, to);
260 notifier(Arc()).add(arc);
265 notifier(Arc()).clear();
269 void erase(const Arc& arc) {
270 notifier(Arc()).erase(arc);
275 arc_notifier.setContainer(*this);
279 arc_notifier.clear();
285 // \ingroup digraphbits
287 // \brief Extender for the EdgeSets
288 template <typename Base>
289 class EdgeSetExtender : public Base {
294 typedef EdgeSetExtender Graph;
296 typedef True UndirectedTag;
298 typedef typename Parent::Node Node;
299 typedef typename Parent::Arc Arc;
300 typedef typename Parent::Edge Edge;
302 int maxId(Node) const {
303 return Parent::maxNodeId();
306 int maxId(Arc) const {
307 return Parent::maxArcId();
310 int maxId(Edge) const {
311 return Parent::maxEdgeId();
314 Node fromId(int id, Node) const {
315 return Parent::nodeFromId(id);
318 Arc fromId(int id, Arc) const {
319 return Parent::arcFromId(id);
322 Edge fromId(int id, Edge) const {
323 return Parent::edgeFromId(id);
326 Node oppositeNode(const Node &n, const Edge &e) const {
327 if( n == Parent::u(e))
329 else if( n == Parent::v(e))
335 Arc oppositeArc(const Arc &e) const {
336 return Parent::direct(e, !Parent::direction(e));
339 using Parent::direct;
340 Arc direct(const Edge &e, const Node &s) const {
341 return Parent::direct(e, Parent::u(e) == s);
344 typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
345 typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
350 mutable ArcNotifier arc_notifier;
351 mutable EdgeNotifier edge_notifier;
355 using Parent::notifier;
357 ArcNotifier& notifier(Arc) const {
361 EdgeNotifier& notifier(Edge) const {
362 return edge_notifier;
366 class NodeIt : public Node {
372 NodeIt(Invalid i) : Node(i) { }
374 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
375 _graph.first(static_cast<Node&>(*this));
378 NodeIt(const Graph& _graph, const Node& node)
379 : Node(node), graph(&_graph) {}
381 NodeIt& operator++() {
388 LemonRangeWrapper1<NodeIt, Graph> nodes() const {
389 return LemonRangeWrapper1<NodeIt, Graph>(*this);
392 class ArcIt : public Arc {
398 ArcIt(Invalid i) : Arc(i) { }
400 explicit ArcIt(const Graph& _graph) : graph(&_graph) {
401 _graph.first(static_cast<Arc&>(*this));
404 ArcIt(const Graph& _graph, const Arc& e) :
405 Arc(e), graph(&_graph) { }
407 ArcIt& operator++() {
414 LemonRangeWrapper1<ArcIt, Graph> arcs() const {
415 return LemonRangeWrapper1<ArcIt, Graph>(*this);
418 class OutArcIt : public Arc {
424 OutArcIt(Invalid i) : Arc(i) { }
426 OutArcIt(const Graph& _graph, const Node& node)
428 _graph.firstOut(*this, node);
431 OutArcIt(const Graph& _graph, const Arc& arc)
432 : Arc(arc), graph(&_graph) {}
434 OutArcIt& operator++() {
435 graph->nextOut(*this);
441 LemonRangeWrapper2<OutArcIt, Graph, Node> outArcs(const Node& u) const {
442 return LemonRangeWrapper2<OutArcIt, Graph, Node>(*this, u);
445 class InArcIt : public Arc {
451 InArcIt(Invalid i) : Arc(i) { }
453 InArcIt(const Graph& _graph, const Node& node)
455 _graph.firstIn(*this, node);
458 InArcIt(const Graph& _graph, const Arc& arc) :
459 Arc(arc), graph(&_graph) {}
461 InArcIt& operator++() {
462 graph->nextIn(*this);
468 LemonRangeWrapper2<InArcIt, Graph, Node> inArcs(const Node& u) const {
469 return LemonRangeWrapper2<InArcIt, Graph, Node>(*this, u);
472 class EdgeIt : public Parent::Edge {
478 EdgeIt(Invalid i) : Edge(i) { }
480 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
481 _graph.first(static_cast<Edge&>(*this));
484 EdgeIt(const Graph& _graph, const Edge& e) :
485 Edge(e), graph(&_graph) { }
487 EdgeIt& operator++() {
494 LemonRangeWrapper1<EdgeIt, Graph> edges() const {
495 return LemonRangeWrapper1<EdgeIt, Graph>(*this);
498 class IncEdgeIt : public Parent::Edge {
499 friend class EdgeSetExtender;
506 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
508 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
509 _graph.firstInc(*this, direction, n);
512 IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
513 : graph(&_graph), Edge(ue) {
514 direction = (_graph.source(ue) == n);
517 IncEdgeIt& operator++() {
518 graph->nextInc(*this, direction);
523 LemonRangeWrapper2<IncEdgeIt, Graph, Node> incEdges(const Node& u) const {
524 return LemonRangeWrapper2<IncEdgeIt, Graph, Node>(*this, u);
527 // \brief Base node of the iterator
529 // Returns the base node (ie. the source in this case) of the iterator
530 Node baseNode(const OutArcIt &e) const {
531 return Parent::source(static_cast<const Arc&>(e));
533 // \brief Running node of the iterator
535 // Returns the running node (ie. the target in this case) of the
537 Node runningNode(const OutArcIt &e) const {
538 return Parent::target(static_cast<const Arc&>(e));
541 // \brief Base node of the iterator
543 // Returns the base node (ie. the target in this case) of the iterator
544 Node baseNode(const InArcIt &e) const {
545 return Parent::target(static_cast<const Arc&>(e));
547 // \brief Running node of the iterator
549 // Returns the running node (ie. the source in this case) of the
551 Node runningNode(const InArcIt &e) const {
552 return Parent::source(static_cast<const Arc&>(e));
555 // Base node of the iterator
557 // Returns the base node of the iterator
558 Node baseNode(const IncEdgeIt &e) const {
559 return e.direction ? this->u(e) : this->v(e);
561 // Running node of the iterator
563 // Returns the running node of the iterator
564 Node runningNode(const IncEdgeIt &e) const {
565 return e.direction ? this->v(e) : this->u(e);
569 template <typename _Value>
571 : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
572 typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
575 explicit ArcMap(const Graph& _g)
577 ArcMap(const Graph& _g, const _Value& _v)
580 ArcMap& operator=(const ArcMap& cmap) {
581 return operator=<ArcMap>(cmap);
584 template <typename CMap>
585 ArcMap& operator=(const CMap& cmap) {
586 Parent::operator=(cmap);
593 template <typename _Value>
595 : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
596 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
599 explicit EdgeMap(const Graph& _g)
602 EdgeMap(const Graph& _g, const _Value& _v)
605 EdgeMap& operator=(const EdgeMap& cmap) {
606 return operator=<EdgeMap>(cmap);
609 template <typename CMap>
610 EdgeMap& operator=(const CMap& cmap) {
611 Parent::operator=(cmap);
618 // Alteration extension
620 Edge addEdge(const Node& from, const Node& to) {
621 Edge edge = Parent::addEdge(from, to);
622 notifier(Edge()).add(edge);
623 std::vector<Arc> arcs;
624 arcs.push_back(Parent::direct(edge, true));
625 arcs.push_back(Parent::direct(edge, false));
626 notifier(Arc()).add(arcs);
631 notifier(Arc()).clear();
632 notifier(Edge()).clear();
636 void erase(const Edge& edge) {
637 std::vector<Arc> arcs;
638 arcs.push_back(Parent::direct(edge, true));
639 arcs.push_back(Parent::direct(edge, false));
640 notifier(Arc()).erase(arcs);
641 notifier(Edge()).erase(edge);
647 arc_notifier.setContainer(*this);
648 edge_notifier.setContainer(*this);
652 edge_notifier.clear();
653 arc_notifier.clear();