lemon/bits/edge_set_extender.h
branch1.1
changeset 782 1309a803a057
parent 725 8af504c71ac4
child 793 8d2e55fac752
equal deleted inserted replaced
6:a1a78c74df98 7:640139f74440
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2011
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
    61       return Parent::arcFromId(id);
    61       return Parent::arcFromId(id);
    62     }
    62     }
    63 
    63 
    64     Node oppositeNode(const Node &n, const Arc &e) const {
    64     Node oppositeNode(const Node &n, const Arc &e) const {
    65       if (n == Parent::source(e))
    65       if (n == Parent::source(e))
    66 	return Parent::target(e);
    66         return Parent::target(e);
    67       else if(n==Parent::target(e))
    67       else if(n==Parent::target(e))
    68 	return Parent::source(e);
    68         return Parent::source(e);
    69       else
    69       else
    70 	return INVALID;
    70         return INVALID;
    71     }
    71     }
    72 
    72 
    73 
    73 
    74     // Alteration notifier extensions
    74     // Alteration notifier extensions
    75 
    75 
    89       return arc_notifier;
    89       return arc_notifier;
    90     }
    90     }
    91 
    91 
    92     // Iterable extensions
    92     // Iterable extensions
    93 
    93 
    94     class NodeIt : public Node { 
    94     class NodeIt : public Node {
    95       const Digraph* digraph;
    95       const Digraph* digraph;
    96     public:
    96     public:
    97 
    97 
    98       NodeIt() {}
    98       NodeIt() {}
    99 
    99 
   100       NodeIt(Invalid i) : Node(i) { }
   100       NodeIt(Invalid i) : Node(i) { }
   101 
   101 
   102       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
   102       explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
   103 	_graph.first(static_cast<Node&>(*this));
   103         _graph.first(static_cast<Node&>(*this));
   104       }
   104       }
   105 
   105 
   106       NodeIt(const Digraph& _graph, const Node& node) 
   106       NodeIt(const Digraph& _graph, const Node& node)
   107 	: Node(node), digraph(&_graph) {}
   107         : Node(node), digraph(&_graph) {}
   108 
   108 
   109       NodeIt& operator++() { 
   109       NodeIt& operator++() {
   110 	digraph->next(*this);
   110         digraph->next(*this);
   111 	return *this; 
   111         return *this;
   112       }
   112       }
   113 
   113 
   114     };
   114     };
   115 
   115 
   116 
   116 
   117     class ArcIt : public Arc { 
   117     class ArcIt : public Arc {
   118       const Digraph* digraph;
   118       const Digraph* digraph;
   119     public:
   119     public:
   120 
   120 
   121       ArcIt() { }
   121       ArcIt() { }
   122 
   122 
   123       ArcIt(Invalid i) : Arc(i) { }
   123       ArcIt(Invalid i) : Arc(i) { }
   124 
   124 
   125       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
   125       explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
   126 	_graph.first(static_cast<Arc&>(*this));
   126         _graph.first(static_cast<Arc&>(*this));
   127       }
   127       }
   128 
   128 
   129       ArcIt(const Digraph& _graph, const Arc& e) : 
   129       ArcIt(const Digraph& _graph, const Arc& e) :
   130 	Arc(e), digraph(&_graph) { }
   130         Arc(e), digraph(&_graph) { }
   131 
   131 
   132       ArcIt& operator++() { 
   132       ArcIt& operator++() {
   133 	digraph->next(*this);
   133         digraph->next(*this);
   134 	return *this; 
   134         return *this;
   135       }
   135       }
   136 
   136 
   137     };
   137     };
   138 
   138 
   139 
   139 
   140     class OutArcIt : public Arc { 
   140     class OutArcIt : public Arc {
   141       const Digraph* digraph;
   141       const Digraph* digraph;
   142     public:
   142     public:
   143 
   143 
   144       OutArcIt() { }
   144       OutArcIt() { }
   145 
   145 
   146       OutArcIt(Invalid i) : Arc(i) { }
   146       OutArcIt(Invalid i) : Arc(i) { }
   147 
   147 
   148       OutArcIt(const Digraph& _graph, const Node& node) 
   148       OutArcIt(const Digraph& _graph, const Node& node)
   149 	: digraph(&_graph) {
   149         : digraph(&_graph) {
   150 	_graph.firstOut(*this, node);
   150         _graph.firstOut(*this, node);
   151       }
   151       }
   152 
   152 
   153       OutArcIt(const Digraph& _graph, const Arc& arc) 
   153       OutArcIt(const Digraph& _graph, const Arc& arc)
   154 	: Arc(arc), digraph(&_graph) {}
   154         : Arc(arc), digraph(&_graph) {}
   155 
   155 
   156       OutArcIt& operator++() { 
   156       OutArcIt& operator++() {
   157 	digraph->nextOut(*this);
   157         digraph->nextOut(*this);
   158 	return *this; 
   158         return *this;
   159       }
   159       }
   160 
   160 
   161     };
   161     };
   162 
   162 
   163 
   163 
   164     class InArcIt : public Arc { 
   164     class InArcIt : public Arc {
   165       const Digraph* digraph;
   165       const Digraph* digraph;
   166     public:
   166     public:
   167 
   167 
   168       InArcIt() { }
   168       InArcIt() { }
   169 
   169 
   170       InArcIt(Invalid i) : Arc(i) { }
   170       InArcIt(Invalid i) : Arc(i) { }
   171 
   171 
   172       InArcIt(const Digraph& _graph, const Node& node) 
   172       InArcIt(const Digraph& _graph, const Node& node)
   173 	: digraph(&_graph) {
   173         : digraph(&_graph) {
   174 	_graph.firstIn(*this, node);
   174         _graph.firstIn(*this, node);
   175       }
   175       }
   176 
   176 
   177       InArcIt(const Digraph& _graph, const Arc& arc) : 
   177       InArcIt(const Digraph& _graph, const Arc& arc) :
   178 	Arc(arc), digraph(&_graph) {}
   178         Arc(arc), digraph(&_graph) {}
   179 
   179 
   180       InArcIt& operator++() { 
   180       InArcIt& operator++() {
   181 	digraph->nextIn(*this);
   181         digraph->nextIn(*this);
   182 	return *this; 
   182         return *this;
   183       }
   183       }
   184 
   184 
   185     };
   185     };
   186 
   186 
   187     // \brief Base node of the iterator
   187     // \brief Base node of the iterator
   213     }
   213     }
   214 
   214 
   215     using Parent::first;
   215     using Parent::first;
   216 
   216 
   217     // Mappable extension
   217     // Mappable extension
   218     
   218 
   219     template <typename _Value>
   219     template <typename _Value>
   220     class ArcMap 
   220     class ArcMap
   221       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
   221       : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
   222       typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
   222       typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
   223 
   223 
   224     public:
   224     public:
   225       explicit ArcMap(const Digraph& _g) 
   225       explicit ArcMap(const Digraph& _g)
   226 	: Parent(_g) {}
   226         : Parent(_g) {}
   227       ArcMap(const Digraph& _g, const _Value& _v) 
   227       ArcMap(const Digraph& _g, const _Value& _v)
   228 	: Parent(_g, _v) {}
   228         : Parent(_g, _v) {}
   229 
   229 
   230       ArcMap& operator=(const ArcMap& cmap) {
   230       ArcMap& operator=(const ArcMap& cmap) {
   231 	return operator=<ArcMap>(cmap);
   231         return operator=<ArcMap>(cmap);
   232       }
   232       }
   233 
   233 
   234       template <typename CMap>
   234       template <typename CMap>
   235       ArcMap& operator=(const CMap& cmap) {
   235       ArcMap& operator=(const CMap& cmap) {
   236         Parent::operator=(cmap);
   236         Parent::operator=(cmap);
   237 	return *this;
   237         return *this;
   238       }
   238       }
   239 
   239 
   240     };
   240     };
   241 
   241 
   242 
   242 
   245     Arc addArc(const Node& from, const Node& to) {
   245     Arc addArc(const Node& from, const Node& to) {
   246       Arc arc = Parent::addArc(from, to);
   246       Arc arc = Parent::addArc(from, to);
   247       notifier(Arc()).add(arc);
   247       notifier(Arc()).add(arc);
   248       return arc;
   248       return arc;
   249     }
   249     }
   250     
   250 
   251     void clear() {
   251     void clear() {
   252       notifier(Arc()).clear();
   252       notifier(Arc()).clear();
   253       Parent::clear();
   253       Parent::clear();
   254     }
   254     }
   255 
   255 
   310       return Parent::edgeFromId(id);
   310       return Parent::edgeFromId(id);
   311     }
   311     }
   312 
   312 
   313     Node oppositeNode(const Node &n, const Edge &e) const {
   313     Node oppositeNode(const Node &n, const Edge &e) const {
   314       if( n == Parent::u(e))
   314       if( n == Parent::u(e))
   315 	return Parent::v(e);
   315         return Parent::v(e);
   316       else if( n == Parent::v(e))
   316       else if( n == Parent::v(e))
   317 	return Parent::u(e);
   317         return Parent::u(e);
   318       else
   318       else
   319 	return INVALID;
   319         return INVALID;
   320     }
   320     }
   321 
   321 
   322     Arc oppositeArc(const Arc &e) const {
   322     Arc oppositeArc(const Arc &e) const {
   323       return Parent::direct(e, !Parent::direction(e));
   323       return Parent::direct(e, !Parent::direction(e));
   324     }
   324     }
   338     mutable EdgeNotifier edge_notifier;
   338     mutable EdgeNotifier edge_notifier;
   339 
   339 
   340   public:
   340   public:
   341 
   341 
   342     using Parent::notifier;
   342     using Parent::notifier;
   343     
   343 
   344     ArcNotifier& notifier(Arc) const {
   344     ArcNotifier& notifier(Arc) const {
   345       return arc_notifier;
   345       return arc_notifier;
   346     }
   346     }
   347 
   347 
   348     EdgeNotifier& notifier(Edge) const {
   348     EdgeNotifier& notifier(Edge) const {
   349       return edge_notifier;
   349       return edge_notifier;
   350     }
   350     }
   351 
   351 
   352 
   352 
   353     class NodeIt : public Node { 
   353     class NodeIt : public Node {
   354       const Graph* graph;
   354       const Graph* graph;
   355     public:
   355     public:
   356 
   356 
   357       NodeIt() {}
   357       NodeIt() {}
   358 
   358 
   359       NodeIt(Invalid i) : Node(i) { }
   359       NodeIt(Invalid i) : Node(i) { }
   360 
   360 
   361       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   361       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   362 	_graph.first(static_cast<Node&>(*this));
   362         _graph.first(static_cast<Node&>(*this));
   363       }
   363       }
   364 
   364 
   365       NodeIt(const Graph& _graph, const Node& node) 
   365       NodeIt(const Graph& _graph, const Node& node)
   366 	: Node(node), graph(&_graph) {}
   366         : Node(node), graph(&_graph) {}
   367 
   367 
   368       NodeIt& operator++() { 
   368       NodeIt& operator++() {
   369 	graph->next(*this);
   369         graph->next(*this);
   370 	return *this; 
   370         return *this;
   371       }
   371       }
   372 
   372 
   373     };
   373     };
   374 
   374 
   375 
   375 
   376     class ArcIt : public Arc { 
   376     class ArcIt : public Arc {
   377       const Graph* graph;
   377       const Graph* graph;
   378     public:
   378     public:
   379 
   379 
   380       ArcIt() { }
   380       ArcIt() { }
   381 
   381 
   382       ArcIt(Invalid i) : Arc(i) { }
   382       ArcIt(Invalid i) : Arc(i) { }
   383 
   383 
   384       explicit ArcIt(const Graph& _graph) : graph(&_graph) {
   384       explicit ArcIt(const Graph& _graph) : graph(&_graph) {
   385 	_graph.first(static_cast<Arc&>(*this));
   385         _graph.first(static_cast<Arc&>(*this));
   386       }
   386       }
   387 
   387 
   388       ArcIt(const Graph& _graph, const Arc& e) : 
   388       ArcIt(const Graph& _graph, const Arc& e) :
   389 	Arc(e), graph(&_graph) { }
   389         Arc(e), graph(&_graph) { }
   390 
   390 
   391       ArcIt& operator++() { 
   391       ArcIt& operator++() {
   392 	graph->next(*this);
   392         graph->next(*this);
   393 	return *this; 
   393         return *this;
   394       }
   394       }
   395 
   395 
   396     };
   396     };
   397 
   397 
   398 
   398 
   399     class OutArcIt : public Arc { 
   399     class OutArcIt : public Arc {
   400       const Graph* graph;
   400       const Graph* graph;
   401     public:
   401     public:
   402 
   402 
   403       OutArcIt() { }
   403       OutArcIt() { }
   404 
   404 
   405       OutArcIt(Invalid i) : Arc(i) { }
   405       OutArcIt(Invalid i) : Arc(i) { }
   406 
   406 
   407       OutArcIt(const Graph& _graph, const Node& node) 
   407       OutArcIt(const Graph& _graph, const Node& node)
   408 	: graph(&_graph) {
   408         : graph(&_graph) {
   409 	_graph.firstOut(*this, node);
   409         _graph.firstOut(*this, node);
   410       }
   410       }
   411 
   411 
   412       OutArcIt(const Graph& _graph, const Arc& arc) 
   412       OutArcIt(const Graph& _graph, const Arc& arc)
   413 	: Arc(arc), graph(&_graph) {}
   413         : Arc(arc), graph(&_graph) {}
   414 
   414 
   415       OutArcIt& operator++() { 
   415       OutArcIt& operator++() {
   416 	graph->nextOut(*this);
   416         graph->nextOut(*this);
   417 	return *this; 
   417         return *this;
   418       }
   418       }
   419 
   419 
   420     };
   420     };
   421 
   421 
   422 
   422 
   423     class InArcIt : public Arc { 
   423     class InArcIt : public Arc {
   424       const Graph* graph;
   424       const Graph* graph;
   425     public:
   425     public:
   426 
   426 
   427       InArcIt() { }
   427       InArcIt() { }
   428 
   428 
   429       InArcIt(Invalid i) : Arc(i) { }
   429       InArcIt(Invalid i) : Arc(i) { }
   430 
   430 
   431       InArcIt(const Graph& _graph, const Node& node) 
   431       InArcIt(const Graph& _graph, const Node& node)
   432 	: graph(&_graph) {
   432         : graph(&_graph) {
   433 	_graph.firstIn(*this, node);
   433         _graph.firstIn(*this, node);
   434       }
   434       }
   435 
   435 
   436       InArcIt(const Graph& _graph, const Arc& arc) : 
   436       InArcIt(const Graph& _graph, const Arc& arc) :
   437 	Arc(arc), graph(&_graph) {}
   437         Arc(arc), graph(&_graph) {}
   438 
   438 
   439       InArcIt& operator++() { 
   439       InArcIt& operator++() {
   440 	graph->nextIn(*this);
   440         graph->nextIn(*this);
   441 	return *this; 
   441         return *this;
   442       }
   442       }
   443 
   443 
   444     };
   444     };
   445 
   445 
   446 
   446 
   447     class EdgeIt : public Parent::Edge { 
   447     class EdgeIt : public Parent::Edge {
   448       const Graph* graph;
   448       const Graph* graph;
   449     public:
   449     public:
   450 
   450 
   451       EdgeIt() { }
   451       EdgeIt() { }
   452 
   452 
   453       EdgeIt(Invalid i) : Edge(i) { }
   453       EdgeIt(Invalid i) : Edge(i) { }
   454 
   454 
   455       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   455       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   456 	_graph.first(static_cast<Edge&>(*this));
   456         _graph.first(static_cast<Edge&>(*this));
   457       }
   457       }
   458 
   458 
   459       EdgeIt(const Graph& _graph, const Edge& e) : 
   459       EdgeIt(const Graph& _graph, const Edge& e) :
   460 	Edge(e), graph(&_graph) { }
   460         Edge(e), graph(&_graph) { }
   461 
   461 
   462       EdgeIt& operator++() { 
   462       EdgeIt& operator++() {
   463 	graph->next(*this);
   463         graph->next(*this);
   464 	return *this; 
   464         return *this;
   465       }
   465       }
   466 
   466 
   467     };
   467     };
   468 
   468 
   469     class IncEdgeIt : public Parent::Edge {
   469     class IncEdgeIt : public Parent::Edge {
   475       IncEdgeIt() { }
   475       IncEdgeIt() { }
   476 
   476 
   477       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
   477       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
   478 
   478 
   479       IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
   479       IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
   480 	_graph.firstInc(*this, direction, n);
   480         _graph.firstInc(*this, direction, n);
   481       }
   481       }
   482 
   482 
   483       IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
   483       IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
   484 	: graph(&_graph), Edge(ue) {
   484         : graph(&_graph), Edge(ue) {
   485 	direction = (_graph.source(ue) == n);
   485         direction = (_graph.source(ue) == n);
   486       }
   486       }
   487 
   487 
   488       IncEdgeIt& operator++() {
   488       IncEdgeIt& operator++() {
   489 	graph->nextInc(*this, direction);
   489         graph->nextInc(*this, direction);
   490 	return *this; 
   490         return *this;
   491       }
   491       }
   492     };
   492     };
   493 
   493 
   494     // \brief Base node of the iterator
   494     // \brief Base node of the iterator
   495     //
   495     //
   532       return e.direction ? v(e) : u(e);
   532       return e.direction ? v(e) : u(e);
   533     }
   533     }
   534 
   534 
   535 
   535 
   536     template <typename _Value>
   536     template <typename _Value>
   537     class ArcMap 
   537     class ArcMap
   538       : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
   538       : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
   539       typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
   539       typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
   540 
   540 
   541     public:
   541     public:
   542       explicit ArcMap(const Graph& _g) 
   542       explicit ArcMap(const Graph& _g)
   543 	: Parent(_g) {}
   543         : Parent(_g) {}
   544       ArcMap(const Graph& _g, const _Value& _v) 
   544       ArcMap(const Graph& _g, const _Value& _v)
   545 	: Parent(_g, _v) {}
   545         : Parent(_g, _v) {}
   546 
   546 
   547       ArcMap& operator=(const ArcMap& cmap) {
   547       ArcMap& operator=(const ArcMap& cmap) {
   548 	return operator=<ArcMap>(cmap);
   548         return operator=<ArcMap>(cmap);
   549       }
   549       }
   550 
   550 
   551       template <typename CMap>
   551       template <typename CMap>
   552       ArcMap& operator=(const CMap& cmap) {
   552       ArcMap& operator=(const CMap& cmap) {
   553         Parent::operator=(cmap);
   553         Parent::operator=(cmap);
   554 	return *this;
   554         return *this;
   555       }
   555       }
   556 
   556 
   557     };
   557     };
   558 
   558 
   559 
   559 
   560     template <typename _Value>
   560     template <typename _Value>
   561     class EdgeMap 
   561     class EdgeMap
   562       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
   562       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
   563       typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   563       typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   564 
   564 
   565     public:
   565     public:
   566       explicit EdgeMap(const Graph& _g) 
   566       explicit EdgeMap(const Graph& _g)
   567 	: Parent(_g) {}
   567         : Parent(_g) {}
   568 
   568 
   569       EdgeMap(const Graph& _g, const _Value& _v) 
   569       EdgeMap(const Graph& _g, const _Value& _v)
   570 	: Parent(_g, _v) {}
   570         : Parent(_g, _v) {}
   571 
   571 
   572       EdgeMap& operator=(const EdgeMap& cmap) {
   572       EdgeMap& operator=(const EdgeMap& cmap) {
   573 	return operator=<EdgeMap>(cmap);
   573         return operator=<EdgeMap>(cmap);
   574       }
   574       }
   575 
   575 
   576       template <typename CMap>
   576       template <typename CMap>
   577       EdgeMap& operator=(const CMap& cmap) {
   577       EdgeMap& operator=(const CMap& cmap) {
   578         Parent::operator=(cmap);
   578         Parent::operator=(cmap);
   579 	return *this;
   579         return *this;
   580       }
   580       }
   581 
   581 
   582     };
   582     };
   583 
   583 
   584 
   584 
   591       arcs.push_back(Parent::direct(edge, true));
   591       arcs.push_back(Parent::direct(edge, true));
   592       arcs.push_back(Parent::direct(edge, false));
   592       arcs.push_back(Parent::direct(edge, false));
   593       notifier(Arc()).add(arcs);
   593       notifier(Arc()).add(arcs);
   594       return edge;
   594       return edge;
   595     }
   595     }
   596     
   596 
   597     void clear() {
   597     void clear() {
   598       notifier(Arc()).clear();
   598       notifier(Arc()).clear();
   599       notifier(Edge()).clear();
   599       notifier(Edge()).clear();
   600       Parent::clear();
   600       Parent::clear();
   601     }
   601     }
   617 
   617 
   618     ~EdgeSetExtender() {
   618     ~EdgeSetExtender() {
   619       edge_notifier.clear();
   619       edge_notifier.clear();
   620       arc_notifier.clear();
   620       arc_notifier.clear();
   621     }
   621     }
   622     
   622 
   623   };
   623   };
   624 
   624 
   625 }
   625 }
   626 
   626 
   627 #endif
   627 #endif