lemon/bits/edge_set_extender.h
changeset 942 633956ca9421
parent 685 a27356ceb5bd
child 884 6dd226d3dcba
child 964 2b6bffe0e7e8
equal deleted inserted replaced
4:835f04db8f01 5:6ea45d22021c
     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-2010
     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 
   308       return Parent::edgeFromId(id);
   308       return Parent::edgeFromId(id);
   309     }
   309     }
   310 
   310 
   311     Node oppositeNode(const Node &n, const Edge &e) const {
   311     Node oppositeNode(const Node &n, const Edge &e) const {
   312       if( n == Parent::u(e))
   312       if( n == Parent::u(e))
   313 	return Parent::v(e);
   313         return Parent::v(e);
   314       else if( n == Parent::v(e))
   314       else if( n == Parent::v(e))
   315 	return Parent::u(e);
   315         return Parent::u(e);
   316       else
   316       else
   317 	return INVALID;
   317         return INVALID;
   318     }
   318     }
   319 
   319 
   320     Arc oppositeArc(const Arc &e) const {
   320     Arc oppositeArc(const Arc &e) const {
   321       return Parent::direct(e, !Parent::direction(e));
   321       return Parent::direct(e, !Parent::direction(e));
   322     }
   322     }
   336     mutable EdgeNotifier edge_notifier;
   336     mutable EdgeNotifier edge_notifier;
   337 
   337 
   338   public:
   338   public:
   339 
   339 
   340     using Parent::notifier;
   340     using Parent::notifier;
   341     
   341 
   342     ArcNotifier& notifier(Arc) const {
   342     ArcNotifier& notifier(Arc) const {
   343       return arc_notifier;
   343       return arc_notifier;
   344     }
   344     }
   345 
   345 
   346     EdgeNotifier& notifier(Edge) const {
   346     EdgeNotifier& notifier(Edge) const {
   347       return edge_notifier;
   347       return edge_notifier;
   348     }
   348     }
   349 
   349 
   350 
   350 
   351     class NodeIt : public Node { 
   351     class NodeIt : public Node {
   352       const Graph* graph;
   352       const Graph* graph;
   353     public:
   353     public:
   354 
   354 
   355       NodeIt() {}
   355       NodeIt() {}
   356 
   356 
   357       NodeIt(Invalid i) : Node(i) { }
   357       NodeIt(Invalid i) : Node(i) { }
   358 
   358 
   359       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   359       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   360 	_graph.first(static_cast<Node&>(*this));
   360         _graph.first(static_cast<Node&>(*this));
   361       }
   361       }
   362 
   362 
   363       NodeIt(const Graph& _graph, const Node& node) 
   363       NodeIt(const Graph& _graph, const Node& node)
   364 	: Node(node), graph(&_graph) {}
   364         : Node(node), graph(&_graph) {}
   365 
   365 
   366       NodeIt& operator++() { 
   366       NodeIt& operator++() {
   367 	graph->next(*this);
   367         graph->next(*this);
   368 	return *this; 
   368         return *this;
   369       }
   369       }
   370 
   370 
   371     };
   371     };
   372 
   372 
   373 
   373 
   374     class ArcIt : public Arc { 
   374     class ArcIt : public Arc {
   375       const Graph* graph;
   375       const Graph* graph;
   376     public:
   376     public:
   377 
   377 
   378       ArcIt() { }
   378       ArcIt() { }
   379 
   379 
   380       ArcIt(Invalid i) : Arc(i) { }
   380       ArcIt(Invalid i) : Arc(i) { }
   381 
   381 
   382       explicit ArcIt(const Graph& _graph) : graph(&_graph) {
   382       explicit ArcIt(const Graph& _graph) : graph(&_graph) {
   383 	_graph.first(static_cast<Arc&>(*this));
   383         _graph.first(static_cast<Arc&>(*this));
   384       }
   384       }
   385 
   385 
   386       ArcIt(const Graph& _graph, const Arc& e) : 
   386       ArcIt(const Graph& _graph, const Arc& e) :
   387 	Arc(e), graph(&_graph) { }
   387         Arc(e), graph(&_graph) { }
   388 
   388 
   389       ArcIt& operator++() { 
   389       ArcIt& operator++() {
   390 	graph->next(*this);
   390         graph->next(*this);
   391 	return *this; 
   391         return *this;
   392       }
   392       }
   393 
   393 
   394     };
   394     };
   395 
   395 
   396 
   396 
   397     class OutArcIt : public Arc { 
   397     class OutArcIt : public Arc {
   398       const Graph* graph;
   398       const Graph* graph;
   399     public:
   399     public:
   400 
   400 
   401       OutArcIt() { }
   401       OutArcIt() { }
   402 
   402 
   403       OutArcIt(Invalid i) : Arc(i) { }
   403       OutArcIt(Invalid i) : Arc(i) { }
   404 
   404 
   405       OutArcIt(const Graph& _graph, const Node& node) 
   405       OutArcIt(const Graph& _graph, const Node& node)
   406 	: graph(&_graph) {
   406         : graph(&_graph) {
   407 	_graph.firstOut(*this, node);
   407         _graph.firstOut(*this, node);
   408       }
   408       }
   409 
   409 
   410       OutArcIt(const Graph& _graph, const Arc& arc) 
   410       OutArcIt(const Graph& _graph, const Arc& arc)
   411 	: Arc(arc), graph(&_graph) {}
   411         : Arc(arc), graph(&_graph) {}
   412 
   412 
   413       OutArcIt& operator++() { 
   413       OutArcIt& operator++() {
   414 	graph->nextOut(*this);
   414         graph->nextOut(*this);
   415 	return *this; 
   415         return *this;
   416       }
   416       }
   417 
   417 
   418     };
   418     };
   419 
   419 
   420 
   420 
   421     class InArcIt : public Arc { 
   421     class InArcIt : public Arc {
   422       const Graph* graph;
   422       const Graph* graph;
   423     public:
   423     public:
   424 
   424 
   425       InArcIt() { }
   425       InArcIt() { }
   426 
   426 
   427       InArcIt(Invalid i) : Arc(i) { }
   427       InArcIt(Invalid i) : Arc(i) { }
   428 
   428 
   429       InArcIt(const Graph& _graph, const Node& node) 
   429       InArcIt(const Graph& _graph, const Node& node)
   430 	: graph(&_graph) {
   430         : graph(&_graph) {
   431 	_graph.firstIn(*this, node);
   431         _graph.firstIn(*this, node);
   432       }
   432       }
   433 
   433 
   434       InArcIt(const Graph& _graph, const Arc& arc) : 
   434       InArcIt(const Graph& _graph, const Arc& arc) :
   435 	Arc(arc), graph(&_graph) {}
   435         Arc(arc), graph(&_graph) {}
   436 
   436 
   437       InArcIt& operator++() { 
   437       InArcIt& operator++() {
   438 	graph->nextIn(*this);
   438         graph->nextIn(*this);
   439 	return *this; 
   439         return *this;
   440       }
   440       }
   441 
   441 
   442     };
   442     };
   443 
   443 
   444 
   444 
   445     class EdgeIt : public Parent::Edge { 
   445     class EdgeIt : public Parent::Edge {
   446       const Graph* graph;
   446       const Graph* graph;
   447     public:
   447     public:
   448 
   448 
   449       EdgeIt() { }
   449       EdgeIt() { }
   450 
   450 
   451       EdgeIt(Invalid i) : Edge(i) { }
   451       EdgeIt(Invalid i) : Edge(i) { }
   452 
   452 
   453       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   453       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   454 	_graph.first(static_cast<Edge&>(*this));
   454         _graph.first(static_cast<Edge&>(*this));
   455       }
   455       }
   456 
   456 
   457       EdgeIt(const Graph& _graph, const Edge& e) : 
   457       EdgeIt(const Graph& _graph, const Edge& e) :
   458 	Edge(e), graph(&_graph) { }
   458         Edge(e), graph(&_graph) { }
   459 
   459 
   460       EdgeIt& operator++() { 
   460       EdgeIt& operator++() {
   461 	graph->next(*this);
   461         graph->next(*this);
   462 	return *this; 
   462         return *this;
   463       }
   463       }
   464 
   464 
   465     };
   465     };
   466 
   466 
   467     class IncEdgeIt : public Parent::Edge {
   467     class IncEdgeIt : public Parent::Edge {
   473       IncEdgeIt() { }
   473       IncEdgeIt() { }
   474 
   474 
   475       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
   475       IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
   476 
   476 
   477       IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
   477       IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
   478 	_graph.firstInc(*this, direction, n);
   478         _graph.firstInc(*this, direction, n);
   479       }
   479       }
   480 
   480 
   481       IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
   481       IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
   482 	: graph(&_graph), Edge(ue) {
   482         : graph(&_graph), Edge(ue) {
   483 	direction = (_graph.source(ue) == n);
   483         direction = (_graph.source(ue) == n);
   484       }
   484       }
   485 
   485 
   486       IncEdgeIt& operator++() {
   486       IncEdgeIt& operator++() {
   487 	graph->nextInc(*this, direction);
   487         graph->nextInc(*this, direction);
   488 	return *this; 
   488         return *this;
   489       }
   489       }
   490     };
   490     };
   491 
   491 
   492     // \brief Base node of the iterator
   492     // \brief Base node of the iterator
   493     //
   493     //
   530       return e.direction ? v(e) : u(e);
   530       return e.direction ? v(e) : u(e);
   531     }
   531     }
   532 
   532 
   533 
   533 
   534     template <typename _Value>
   534     template <typename _Value>
   535     class ArcMap 
   535     class ArcMap
   536       : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
   536       : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
   537       typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
   537       typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
   538 
   538 
   539     public:
   539     public:
   540       explicit ArcMap(const Graph& _g) 
   540       explicit ArcMap(const Graph& _g)
   541 	: Parent(_g) {}
   541         : Parent(_g) {}
   542       ArcMap(const Graph& _g, const _Value& _v) 
   542       ArcMap(const Graph& _g, const _Value& _v)
   543 	: Parent(_g, _v) {}
   543         : Parent(_g, _v) {}
   544 
   544 
   545       ArcMap& operator=(const ArcMap& cmap) {
   545       ArcMap& operator=(const ArcMap& cmap) {
   546 	return operator=<ArcMap>(cmap);
   546         return operator=<ArcMap>(cmap);
   547       }
   547       }
   548 
   548 
   549       template <typename CMap>
   549       template <typename CMap>
   550       ArcMap& operator=(const CMap& cmap) {
   550       ArcMap& operator=(const CMap& cmap) {
   551         Parent::operator=(cmap);
   551         Parent::operator=(cmap);
   552 	return *this;
   552         return *this;
   553       }
   553       }
   554 
   554 
   555     };
   555     };
   556 
   556 
   557 
   557 
   558     template <typename _Value>
   558     template <typename _Value>
   559     class EdgeMap 
   559     class EdgeMap
   560       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
   560       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
   561       typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   561       typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   562 
   562 
   563     public:
   563     public:
   564       explicit EdgeMap(const Graph& _g) 
   564       explicit EdgeMap(const Graph& _g)
   565 	: Parent(_g) {}
   565         : Parent(_g) {}
   566 
   566 
   567       EdgeMap(const Graph& _g, const _Value& _v) 
   567       EdgeMap(const Graph& _g, const _Value& _v)
   568 	: Parent(_g, _v) {}
   568         : Parent(_g, _v) {}
   569 
   569 
   570       EdgeMap& operator=(const EdgeMap& cmap) {
   570       EdgeMap& operator=(const EdgeMap& cmap) {
   571 	return operator=<EdgeMap>(cmap);
   571         return operator=<EdgeMap>(cmap);
   572       }
   572       }
   573 
   573 
   574       template <typename CMap>
   574       template <typename CMap>
   575       EdgeMap& operator=(const CMap& cmap) {
   575       EdgeMap& operator=(const CMap& cmap) {
   576         Parent::operator=(cmap);
   576         Parent::operator=(cmap);
   577 	return *this;
   577         return *this;
   578       }
   578       }
   579 
   579 
   580     };
   580     };
   581 
   581 
   582 
   582 
   589       arcs.push_back(Parent::direct(edge, true));
   589       arcs.push_back(Parent::direct(edge, true));
   590       arcs.push_back(Parent::direct(edge, false));
   590       arcs.push_back(Parent::direct(edge, false));
   591       notifier(Arc()).add(arcs);
   591       notifier(Arc()).add(arcs);
   592       return edge;
   592       return edge;
   593     }
   593     }
   594     
   594 
   595     void clear() {
   595     void clear() {
   596       notifier(Arc()).clear();
   596       notifier(Arc()).clear();
   597       notifier(Edge()).clear();
   597       notifier(Edge()).clear();
   598       Parent::clear();
   598       Parent::clear();
   599     }
   599     }
   615 
   615 
   616     ~EdgeSetExtender() {
   616     ~EdgeSetExtender() {
   617       edge_notifier.clear();
   617       edge_notifier.clear();
   618       arc_notifier.clear();
   618       arc_notifier.clear();
   619     }
   619     }
   620     
   620 
   621   };
   621   };
   622 
   622 
   623 }
   623 }
   624 
   624 
   625 #endif
   625 #endif