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 {
 
    40     typedef ArcSetExtender Digraph;
 
    44     typedef typename Parent::Node Node;
 
    45     typedef typename Parent::Arc Arc;
 
    47     int maxId(Node) const {
 
    48       return Parent::maxNodeId();
 
    51     int maxId(Arc) const {
 
    52       return Parent::maxArcId();
 
    55     Node fromId(int id, Node) const {
 
    56       return Parent::nodeFromId(id);
 
    59     Arc fromId(int id, Arc) const {
 
    60       return Parent::arcFromId(id);
 
    63     Node oppositeNode(const Node &n, const Arc &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 arc observer registry.
 
    76     typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier;
 
    80     mutable ArcNotifier arc_notifier;
 
    84     using Parent::notifier;
 
    86     // Gives back the arc alteration notifier.
 
    87     ArcNotifier& notifier(Arc) const {
 
    91     // Iterable extensions
 
    93     class NodeIt : public Node { 
 
    94       const Digraph* digraph;
 
    99       NodeIt(Invalid i) : Node(i) { }
 
   101       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
 
   102 	_graph.first(static_cast<Node&>(*this));
 
   105       NodeIt(const Digraph& _graph, const Node& node) 
 
   106 	: Node(node), digraph(&_graph) {}
 
   108       NodeIt& operator++() { 
 
   109 	digraph->next(*this);
 
   116     class ArcIt : public Arc { 
 
   117       const Digraph* digraph;
 
   122       ArcIt(Invalid i) : Arc(i) { }
 
   124       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
 
   125 	_graph.first(static_cast<Arc&>(*this));
 
   128       ArcIt(const Digraph& _graph, const Arc& e) : 
 
   129 	Arc(e), digraph(&_graph) { }
 
   131       ArcIt& operator++() { 
 
   132 	digraph->next(*this);
 
   139     class OutArcIt : public Arc { 
 
   140       const Digraph* digraph;
 
   145       OutArcIt(Invalid i) : Arc(i) { }
 
   147       OutArcIt(const Digraph& _graph, const Node& node) 
 
   149 	_graph.firstOut(*this, node);
 
   152       OutArcIt(const Digraph& _graph, const Arc& arc) 
 
   153 	: Arc(arc), digraph(&_graph) {}
 
   155       OutArcIt& operator++() { 
 
   156 	digraph->nextOut(*this);
 
   163     class InArcIt : public Arc { 
 
   164       const Digraph* digraph;
 
   169       InArcIt(Invalid i) : Arc(i) { }
 
   171       InArcIt(const Digraph& _graph, const Node& node) 
 
   173 	_graph.firstIn(*this, node);
 
   176       InArcIt(const Digraph& _graph, const Arc& arc) : 
 
   177 	Arc(arc), digraph(&_graph) {}
 
   179       InArcIt& operator++() { 
 
   180 	digraph->nextIn(*this);
 
   186     // \brief Base node of the iterator
 
   188     // Returns the base node (ie. the source in this case) of the iterator
 
   189     Node baseNode(const OutArcIt &e) const {
 
   190       return Parent::source(static_cast<const Arc&>(e));
 
   192     // \brief Running node of the iterator
 
   194     // Returns the running node (ie. the target in this case) of the
 
   196     Node runningNode(const OutArcIt &e) const {
 
   197       return Parent::target(static_cast<const Arc&>(e));
 
   200     // \brief Base node of the iterator
 
   202     // Returns the base node (ie. the target in this case) of the iterator
 
   203     Node baseNode(const InArcIt &e) const {
 
   204       return Parent::target(static_cast<const Arc&>(e));
 
   206     // \brief Running node of the iterator
 
   208     // Returns the running node (ie. the source in this case) of the
 
   210     Node runningNode(const InArcIt &e) const {
 
   211       return Parent::source(static_cast<const Arc&>(e));
 
   216     // Mappable extension
 
   218     template <typename _Value>
 
   220       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
 
   222       typedef ArcSetExtender Digraph;
 
   223       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 Digraph;
 
   283     typedef typename Parent::Node Node;
 
   284     typedef typename Parent::Arc Arc;
 
   285     typedef typename Parent::Edge Edge;
 
   288     int maxId(Node) const {
 
   289       return Parent::maxNodeId();
 
   292     int maxId(Arc) const {
 
   293       return Parent::maxArcId();
 
   296     int maxId(Edge) const {
 
   297       return Parent::maxEdgeId();
 
   300     Node fromId(int id, Node) const {
 
   301       return Parent::nodeFromId(id);
 
   304     Arc fromId(int id, Arc) const {
 
   305       return Parent::arcFromId(id);
 
   308     Edge fromId(int id, Edge) const {
 
   309       return Parent::edgeFromId(id);
 
   312     Node oppositeNode(const Node &n, const Edge &e) const {
 
   313       if( n == Parent::u(e))
 
   315       else if( n == Parent::v(e))
 
   321     Arc oppositeArc(const Arc &e) const {
 
   322       return Parent::direct(e, !Parent::direction(e));
 
   325     using Parent::direct;
 
   326     Arc direct(const Edge &e, const Node &s) const {
 
   327       return Parent::direct(e, Parent::u(e) == s);
 
   330     typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
 
   331     typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
 
   336     mutable ArcNotifier arc_notifier;
 
   337     mutable EdgeNotifier edge_notifier;
 
   341     using Parent::notifier;
 
   343     ArcNotifier& notifier(Arc) const {
 
   347     EdgeNotifier& notifier(Edge) const {
 
   348       return edge_notifier;
 
   352     class NodeIt : public Node { 
 
   353       const Digraph* digraph;
 
   358       NodeIt(Invalid i) : Node(i) { }
 
   360       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
 
   361 	_graph.first(static_cast<Node&>(*this));
 
   364       NodeIt(const Digraph& _graph, const Node& node) 
 
   365 	: Node(node), digraph(&_graph) {}
 
   367       NodeIt& operator++() { 
 
   368 	digraph->next(*this);
 
   375     class ArcIt : public Arc { 
 
   376       const Digraph* digraph;
 
   381       ArcIt(Invalid i) : Arc(i) { }
 
   383       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
 
   384 	_graph.first(static_cast<Arc&>(*this));
 
   387       ArcIt(const Digraph& _graph, const Arc& e) : 
 
   388 	Arc(e), digraph(&_graph) { }
 
   390       ArcIt& operator++() { 
 
   391 	digraph->next(*this);
 
   398     class OutArcIt : public Arc { 
 
   399       const Digraph* digraph;
 
   404       OutArcIt(Invalid i) : Arc(i) { }
 
   406       OutArcIt(const Digraph& _graph, const Node& node) 
 
   408 	_graph.firstOut(*this, node);
 
   411       OutArcIt(const Digraph& _graph, const Arc& arc) 
 
   412 	: Arc(arc), digraph(&_graph) {}
 
   414       OutArcIt& operator++() { 
 
   415 	digraph->nextOut(*this);
 
   422     class InArcIt : public Arc { 
 
   423       const Digraph* digraph;
 
   428       InArcIt(Invalid i) : Arc(i) { }
 
   430       InArcIt(const Digraph& _graph, const Node& node) 
 
   432 	_graph.firstIn(*this, node);
 
   435       InArcIt(const Digraph& _graph, const Arc& arc) : 
 
   436 	Arc(arc), digraph(&_graph) {}
 
   438       InArcIt& operator++() { 
 
   439 	digraph->nextIn(*this);
 
   446     class EdgeIt : public Parent::Edge { 
 
   447       const Digraph* digraph;
 
   452       EdgeIt(Invalid i) : Edge(i) { }
 
   454       explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
 
   455 	_graph.first(static_cast<Edge&>(*this));
 
   458       EdgeIt(const Digraph& _graph, const Edge& e) : 
 
   459 	Edge(e), digraph(&_graph) { }
 
   461       EdgeIt& operator++() { 
 
   462 	digraph->next(*this);
 
   468     class IncEdgeIt : public Parent::Edge {
 
   469       friend class EdgeSetExtender;
 
   470       const Digraph* digraph;
 
   476       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
 
   478       IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
 
   479 	_graph.firstInc(*this, direction, n);
 
   482       IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n)
 
   483 	: digraph(&_graph), Edge(ue) {
 
   484 	direction = (_graph.source(ue) == n);
 
   487       IncEdgeIt& operator++() {
 
   488 	digraph->nextInc(*this, direction);
 
   493     // \brief Base node of the iterator
 
   495     // Returns the base node (ie. the source in this case) of the iterator
 
   496     Node baseNode(const OutArcIt &e) const {
 
   497       return Parent::source(static_cast<const Arc&>(e));
 
   499     // \brief Running node of the iterator
 
   501     // Returns the running node (ie. the target in this case) of the
 
   503     Node runningNode(const OutArcIt &e) const {
 
   504       return Parent::target(static_cast<const Arc&>(e));
 
   507     // \brief Base node of the iterator
 
   509     // Returns the base node (ie. the target in this case) of the iterator
 
   510     Node baseNode(const InArcIt &e) const {
 
   511       return Parent::target(static_cast<const Arc&>(e));
 
   513     // \brief Running node of the iterator
 
   515     // Returns the running node (ie. the source in this case) of the
 
   517     Node runningNode(const InArcIt &e) const {
 
   518       return Parent::source(static_cast<const Arc&>(e));
 
   521     // Base node of the iterator
 
   523     // Returns the base node of the iterator
 
   524     Node baseNode(const IncEdgeIt &e) const {
 
   525       return e.direction ? u(e) : v(e);
 
   527     // Running node of the iterator
 
   529     // Returns the running node of the iterator
 
   530     Node runningNode(const IncEdgeIt &e) const {
 
   531       return e.direction ? v(e) : u(e);
 
   535     template <typename _Value>
 
   537       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
 
   539       typedef EdgeSetExtender Digraph;
 
   540       typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
 
   542       ArcMap(const Digraph& _g) 
 
   544       ArcMap(const Digraph& _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<Digraph, Edge, _Value> > {
 
   564       typedef EdgeSetExtender Digraph;
 
   565       typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
 
   567       EdgeMap(const Digraph& _g) 
 
   570       EdgeMap(const Digraph& _g, const _Value& _v) 
 
   573       EdgeMap& operator=(const EdgeMap& cmap) {
 
   574 	return operator=<EdgeMap>(cmap);
 
   577       template <typename CMap>
 
   578       EdgeMap& operator=(const CMap& cmap) {
 
   579         Parent::operator=(cmap);
 
   586     // Alteration extension
 
   588     Edge addEdge(const Node& from, const Node& to) {
 
   589       Edge edge = Parent::addEdge(from, to);
 
   590       notifier(Edge()).add(edge);
 
   591       std::vector<Arc> arcs;
 
   592       arcs.push_back(Parent::direct(edge, true));
 
   593       arcs.push_back(Parent::direct(edge, false));
 
   594       notifier(Arc()).add(arcs);
 
   599       notifier(Arc()).clear();
 
   600       notifier(Edge()).clear();
 
   604     void erase(const Edge& edge) {
 
   605       std::vector<Arc> arcs;
 
   606       arcs.push_back(Parent::direct(edge, true));
 
   607       arcs.push_back(Parent::direct(edge, false));
 
   608       notifier(Arc()).erase(arcs);
 
   609       notifier(Edge()).erase(edge);
 
   615       arc_notifier.setContainer(*this);
 
   616       edge_notifier.setContainer(*this);
 
   620       edge_notifier.clear();
 
   621       arc_notifier.clear();