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     /// \brief Gives back the arc alteration notifier.
 
    88     /// Gives back the arc alteration notifier.
 
    89     ArcNotifier& notifier(Arc) const {
 
    93     // Iterable extensions
 
    95     class NodeIt : public Node { 
 
    96       const Digraph* digraph;
 
   101       NodeIt(Invalid i) : Node(i) { }
 
   103       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
 
   104 	_graph.first(static_cast<Node&>(*this));
 
   107       NodeIt(const Digraph& _graph, const Node& node) 
 
   108 	: Node(node), digraph(&_graph) {}
 
   110       NodeIt& operator++() { 
 
   111 	digraph->next(*this);
 
   118     class ArcIt : public Arc { 
 
   119       const Digraph* digraph;
 
   124       ArcIt(Invalid i) : Arc(i) { }
 
   126       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
 
   127 	_graph.first(static_cast<Arc&>(*this));
 
   130       ArcIt(const Digraph& _graph, const Arc& e) : 
 
   131 	Arc(e), digraph(&_graph) { }
 
   133       ArcIt& operator++() { 
 
   134 	digraph->next(*this);
 
   141     class OutArcIt : public Arc { 
 
   142       const Digraph* digraph;
 
   147       OutArcIt(Invalid i) : Arc(i) { }
 
   149       OutArcIt(const Digraph& _graph, const Node& node) 
 
   151 	_graph.firstOut(*this, node);
 
   154       OutArcIt(const Digraph& _graph, const Arc& arc) 
 
   155 	: Arc(arc), digraph(&_graph) {}
 
   157       OutArcIt& operator++() { 
 
   158 	digraph->nextOut(*this);
 
   165     class InArcIt : public Arc { 
 
   166       const Digraph* digraph;
 
   171       InArcIt(Invalid i) : Arc(i) { }
 
   173       InArcIt(const Digraph& _graph, const Node& node) 
 
   175 	_graph.firstIn(*this, node);
 
   178       InArcIt(const Digraph& _graph, const Arc& arc) : 
 
   179 	Arc(arc), digraph(&_graph) {}
 
   181       InArcIt& operator++() { 
 
   182 	digraph->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 OutArcIt &e) const {
 
   192       return Parent::source(static_cast<const Arc&>(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 OutArcIt &e) const {
 
   199       return Parent::target(static_cast<const Arc&>(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 InArcIt &e) const {
 
   206       return Parent::target(static_cast<const Arc&>(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 InArcIt &e) const {
 
   213       return Parent::source(static_cast<const Arc&>(e));
 
   218     // Mappable extension
 
   220     template <typename _Value>
 
   222       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
 
   224       typedef ArcSetExtender Digraph;
 
   225       typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
 
   227       explicit ArcMap(const Digraph& _g) 
 
   229       ArcMap(const Digraph& _g, const _Value& _v) 
 
   232       ArcMap& operator=(const ArcMap& cmap) {
 
   233 	return operator=<ArcMap>(cmap);
 
   236       template <typename CMap>
 
   237       ArcMap& operator=(const CMap& cmap) {
 
   238         Parent::operator=(cmap);
 
   245     // Alteration extension
 
   247     Arc addArc(const Node& from, const Node& to) {
 
   248       Arc arc = Parent::addArc(from, to);
 
   249       notifier(Arc()).add(arc);
 
   254       notifier(Arc()).clear();
 
   258     void erase(const Arc& arc) {
 
   259       notifier(Arc()).erase(arc);
 
   264       arc_notifier.setContainer(*this);
 
   268       arc_notifier.clear();
 
   274   /// \ingroup digraphbits
 
   276   /// \brief Extender for the EdgeSets
 
   277   template <typename Base>
 
   278   class EdgeSetExtender : public Base {
 
   283     typedef EdgeSetExtender Digraph;
 
   285     typedef typename Parent::Node Node;
 
   286     typedef typename Parent::Arc Arc;
 
   287     typedef typename Parent::Edge Edge;
 
   290     int maxId(Node) const {
 
   291       return Parent::maxNodeId();
 
   294     int maxId(Arc) const {
 
   295       return Parent::maxArcId();
 
   298     int maxId(Edge) const {
 
   299       return Parent::maxEdgeId();
 
   302     Node fromId(int id, Node) const {
 
   303       return Parent::nodeFromId(id);
 
   306     Arc fromId(int id, Arc) const {
 
   307       return Parent::arcFromId(id);
 
   310     Edge fromId(int id, Edge) const {
 
   311       return Parent::edgeFromId(id);
 
   314     Node oppositeNode(const Node &n, const Edge &e) const {
 
   315       if( n == Parent::u(e))
 
   317       else if( n == Parent::v(e))
 
   323     Arc oppositeArc(const Arc &e) const {
 
   324       return Parent::direct(e, !Parent::direction(e));
 
   327     using Parent::direct;
 
   328     Arc direct(const Edge &e, const Node &s) const {
 
   329       return Parent::direct(e, Parent::u(e) == s);
 
   332     typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
 
   333     typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
 
   338     mutable ArcNotifier arc_notifier;
 
   339     mutable EdgeNotifier edge_notifier;
 
   343     using Parent::notifier;
 
   345     ArcNotifier& notifier(Arc) const {
 
   349     EdgeNotifier& notifier(Edge) const {
 
   350       return edge_notifier;
 
   354     class NodeIt : public Node { 
 
   355       const Digraph* digraph;
 
   360       NodeIt(Invalid i) : Node(i) { }
 
   362       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
 
   363 	_graph.first(static_cast<Node&>(*this));
 
   366       NodeIt(const Digraph& _graph, const Node& node) 
 
   367 	: Node(node), digraph(&_graph) {}
 
   369       NodeIt& operator++() { 
 
   370 	digraph->next(*this);
 
   377     class ArcIt : public Arc { 
 
   378       const Digraph* digraph;
 
   383       ArcIt(Invalid i) : Arc(i) { }
 
   385       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
 
   386 	_graph.first(static_cast<Arc&>(*this));
 
   389       ArcIt(const Digraph& _graph, const Arc& e) : 
 
   390 	Arc(e), digraph(&_graph) { }
 
   392       ArcIt& operator++() { 
 
   393 	digraph->next(*this);
 
   400     class OutArcIt : public Arc { 
 
   401       const Digraph* digraph;
 
   406       OutArcIt(Invalid i) : Arc(i) { }
 
   408       OutArcIt(const Digraph& _graph, const Node& node) 
 
   410 	_graph.firstOut(*this, node);
 
   413       OutArcIt(const Digraph& _graph, const Arc& arc) 
 
   414 	: Arc(arc), digraph(&_graph) {}
 
   416       OutArcIt& operator++() { 
 
   417 	digraph->nextOut(*this);
 
   424     class InArcIt : public Arc { 
 
   425       const Digraph* digraph;
 
   430       InArcIt(Invalid i) : Arc(i) { }
 
   432       InArcIt(const Digraph& _graph, const Node& node) 
 
   434 	_graph.firstIn(*this, node);
 
   437       InArcIt(const Digraph& _graph, const Arc& arc) : 
 
   438 	Arc(arc), digraph(&_graph) {}
 
   440       InArcIt& operator++() { 
 
   441 	digraph->nextIn(*this);
 
   448     class EdgeIt : public Parent::Edge { 
 
   449       const Digraph* digraph;
 
   454       EdgeIt(Invalid i) : Edge(i) { }
 
   456       explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
 
   457 	_graph.first(static_cast<Edge&>(*this));
 
   460       EdgeIt(const Digraph& _graph, const Edge& e) : 
 
   461 	Edge(e), digraph(&_graph) { }
 
   463       EdgeIt& operator++() { 
 
   464 	digraph->next(*this);
 
   470     class IncEdgeIt : public Parent::Edge {
 
   471       friend class EdgeSetExtender;
 
   472       const Digraph* digraph;
 
   478       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
 
   480       IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
 
   481 	_graph.firstInc(*this, direction, n);
 
   484       IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n)
 
   485 	: digraph(&_graph), Edge(ue) {
 
   486 	direction = (_graph.source(ue) == n);
 
   489       IncEdgeIt& operator++() {
 
   490 	digraph->nextInc(*this, direction);
 
   495     /// \brief Base node of the iterator
 
   497     /// Returns the base node (ie. the source in this case) of the iterator
 
   498     Node baseNode(const OutArcIt &e) const {
 
   499       return Parent::source(static_cast<const Arc&>(e));
 
   501     /// \brief Running node of the iterator
 
   503     /// Returns the running node (ie. the target in this case) of the
 
   505     Node runningNode(const OutArcIt &e) const {
 
   506       return Parent::target(static_cast<const Arc&>(e));
 
   509     /// \brief Base node of the iterator
 
   511     /// Returns the base node (ie. the target in this case) of the iterator
 
   512     Node baseNode(const InArcIt &e) const {
 
   513       return Parent::target(static_cast<const Arc&>(e));
 
   515     /// \brief Running node of the iterator
 
   517     /// Returns the running node (ie. the source in this case) of the
 
   519     Node runningNode(const InArcIt &e) const {
 
   520       return Parent::source(static_cast<const Arc&>(e));
 
   523     /// Base node of the iterator
 
   525     /// Returns the base node of the iterator
 
   526     Node baseNode(const IncEdgeIt &e) const {
 
   527       return e.direction ? u(e) : v(e);
 
   529     /// Running node of the iterator
 
   531     /// Returns the running node of the iterator
 
   532     Node runningNode(const IncEdgeIt &e) const {
 
   533       return e.direction ? v(e) : u(e);
 
   537     template <typename _Value>
 
   539       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
 
   541       typedef EdgeSetExtender Digraph;
 
   542       typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
 
   544       ArcMap(const Digraph& _g) 
 
   546       ArcMap(const Digraph& _g, const _Value& _v) 
 
   549       ArcMap& operator=(const ArcMap& cmap) {
 
   550 	return operator=<ArcMap>(cmap);
 
   553       template <typename CMap>
 
   554       ArcMap& operator=(const CMap& cmap) {
 
   555         Parent::operator=(cmap);
 
   562     template <typename _Value>
 
   564       : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
 
   566       typedef EdgeSetExtender Digraph;
 
   567       typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
 
   569       EdgeMap(const Digraph& _g) 
 
   572       EdgeMap(const Digraph& _g, const _Value& _v) 
 
   575       EdgeMap& operator=(const EdgeMap& cmap) {
 
   576 	return operator=<EdgeMap>(cmap);
 
   579       template <typename CMap>
 
   580       EdgeMap& operator=(const CMap& cmap) {
 
   581         Parent::operator=(cmap);
 
   588     // Alteration extension
 
   590     Edge addEdge(const Node& from, const Node& to) {
 
   591       Edge edge = Parent::addEdge(from, to);
 
   592       notifier(Edge()).add(edge);
 
   593       std::vector<Arc> arcs;
 
   594       arcs.push_back(Parent::direct(edge, true));
 
   595       arcs.push_back(Parent::direct(edge, false));
 
   596       notifier(Arc()).add(arcs);
 
   601       notifier(Arc()).clear();
 
   602       notifier(Edge()).clear();
 
   606     void erase(const Edge& edge) {
 
   607       std::vector<Arc> arcs;
 
   608       arcs.push_back(Parent::direct(edge, true));
 
   609       arcs.push_back(Parent::direct(edge, false));
 
   610       notifier(Arc()).erase(arcs);
 
   611       notifier(Edge()).erase(edge);
 
   617       arc_notifier.setContainer(*this);
 
   618       edge_notifier.setContainer(*this);
 
   622       edge_notifier.clear();
 
   623       arc_notifier.clear();