lemon/bits/base_extender.h
changeset 212 1ae84dea7d09
parent 107 31a2e6d28f61
child 220 a5d8c039f218
equal deleted inserted replaced
1:b78d12b1833c 2:f62ffd1acdaf
     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-2008
     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  *
    61 
    61 
    62       /// Invalid arc constructor
    62       /// Invalid arc constructor
    63       Arc(Invalid i) : Edge(i), forward(true) {}
    63       Arc(Invalid i) : Edge(i), forward(true) {}
    64 
    64 
    65       bool operator==(const Arc &that) const {
    65       bool operator==(const Arc &that) const {
    66 	return forward==that.forward && Edge(*this)==Edge(that);
    66         return forward==that.forward && Edge(*this)==Edge(that);
    67       }
    67       }
    68       bool operator!=(const Arc &that) const {
    68       bool operator!=(const Arc &that) const {
    69 	return forward!=that.forward || Edge(*this)!=Edge(that);
    69         return forward!=that.forward || Edge(*this)!=Edge(that);
    70       }
    70       }
    71       bool operator<(const Arc &that) const {
    71       bool operator<(const Arc &that) const {
    72 	return forward<that.forward ||
    72         return forward<that.forward ||
    73 	  (!(that.forward<forward) && Edge(*this)<Edge(that));
    73           (!(that.forward<forward) && Edge(*this)<Edge(that));
    74       }
    74       }
    75     };
    75     };
    76 
    76 
    77 
    77 
    78 
    78 
   115       e.forward=true;
   115       e.forward=true;
   116     }
   116     }
   117 
   117 
   118     void next(Arc &e) const {
   118     void next(Arc &e) const {
   119       if( e.forward ) {
   119       if( e.forward ) {
   120 	e.forward = false;
   120         e.forward = false;
   121       }
   121       }
   122       else {
   122       else {
   123 	Parent::next(e);
   123         Parent::next(e);
   124 	e.forward = true;
   124         e.forward = true;
   125       }
   125       }
   126     }
   126     }
   127 
   127 
   128     void firstOut(Arc &e, const Node &n) const {
   128     void firstOut(Arc &e, const Node &n) const {
   129       Parent::firstIn(e,n);
   129       Parent::firstIn(e,n);
   130       if( Edge(e) != INVALID ) {
   130       if( Edge(e) != INVALID ) {
   131 	e.forward = false;
   131         e.forward = false;
   132       }
   132       }
   133       else {
   133       else {
   134 	Parent::firstOut(e,n);
   134         Parent::firstOut(e,n);
   135 	e.forward = true;
   135         e.forward = true;
   136       }
   136       }
   137     }
   137     }
   138     void nextOut(Arc &e) const {
   138     void nextOut(Arc &e) const {
   139       if( ! e.forward ) {
   139       if( ! e.forward ) {
   140 	Node n = Parent::target(e);
   140         Node n = Parent::target(e);
   141 	Parent::nextIn(e);
   141         Parent::nextIn(e);
   142 	if( Edge(e) == INVALID ) {
   142         if( Edge(e) == INVALID ) {
   143 	  Parent::firstOut(e, n);
   143           Parent::firstOut(e, n);
   144 	  e.forward = true;
   144           e.forward = true;
   145 	}
   145         }
   146       }
   146       }
   147       else {
   147       else {
   148 	Parent::nextOut(e);
   148         Parent::nextOut(e);
   149       }
   149       }
   150     }
   150     }
   151 
   151 
   152     void firstIn(Arc &e, const Node &n) const {
   152     void firstIn(Arc &e, const Node &n) const {
   153       Parent::firstOut(e,n);
   153       Parent::firstOut(e,n);
   154       if( Edge(e) != INVALID ) {
   154       if( Edge(e) != INVALID ) {
   155 	e.forward = false;
   155         e.forward = false;
   156       }
   156       }
   157       else {
   157       else {
   158 	Parent::firstIn(e,n);
   158         Parent::firstIn(e,n);
   159 	e.forward = true;
   159         e.forward = true;
   160       }
   160       }
   161     }
   161     }
   162     void nextIn(Arc &e) const {
   162     void nextIn(Arc &e) const {
   163       if( ! e.forward ) {
   163       if( ! e.forward ) {
   164 	Node n = Parent::source(e);
   164         Node n = Parent::source(e);
   165 	Parent::nextOut(e);
   165         Parent::nextOut(e);
   166 	if( Edge(e) == INVALID ) {
   166         if( Edge(e) == INVALID ) {
   167 	  Parent::firstIn(e, n);
   167           Parent::firstIn(e, n);
   168 	  e.forward = true;
   168           e.forward = true;
   169 	}
   169         }
   170       }
   170       }
   171       else {
   171       else {
   172 	Parent::nextIn(e);
   172         Parent::nextIn(e);
   173       }
   173       }
   174     }
   174     }
   175 
   175 
   176     void firstInc(Edge &e, bool &d, const Node &n) const {
   176     void firstInc(Edge &e, bool &d, const Node &n) const {
   177       d = true;
   177       d = true;
   181       Parent::firstIn(e, n);
   181       Parent::firstIn(e, n);
   182     }
   182     }
   183 
   183 
   184     void nextInc(Edge &e, bool &d) const {
   184     void nextInc(Edge &e, bool &d) const {
   185       if (d) {
   185       if (d) {
   186 	Node s = Parent::source(e);
   186         Node s = Parent::source(e);
   187 	Parent::nextOut(e);
   187         Parent::nextOut(e);
   188 	if (e != INVALID) return;
   188         if (e != INVALID) return;
   189 	d = false;
   189         d = false;
   190 	Parent::firstIn(e, s);
   190         Parent::firstIn(e, s);
   191       } else {
   191       } else {
   192 	Parent::nextIn(e);
   192         Parent::nextIn(e);
   193       }
   193       }
   194     }
   194     }
   195 
   195 
   196     Node nodeFromId(int ix) const {
   196     Node nodeFromId(int ix) const {
   197       return Parent::nodeFromId(ix);
   197       return Parent::nodeFromId(ix);
   238       return Parent::arcNum();
   238       return Parent::arcNum();
   239     }
   239     }
   240 
   240 
   241     Arc findArc(Node s, Node t, Arc p = INVALID) const {
   241     Arc findArc(Node s, Node t, Arc p = INVALID) const {
   242       if (p == INVALID) {
   242       if (p == INVALID) {
   243 	Edge arc = Parent::findArc(s, t);
   243         Edge arc = Parent::findArc(s, t);
   244 	if (arc != INVALID) return direct(arc, true);
   244         if (arc != INVALID) return direct(arc, true);
   245 	arc = Parent::findArc(t, s);
   245         arc = Parent::findArc(t, s);
   246 	if (arc != INVALID) return direct(arc, false);
   246         if (arc != INVALID) return direct(arc, false);
   247       } else if (direction(p)) {
   247       } else if (direction(p)) {
   248 	Edge arc = Parent::findArc(s, t, p);
   248         Edge arc = Parent::findArc(s, t, p);
   249 	if (arc != INVALID) return direct(arc, true);
   249         if (arc != INVALID) return direct(arc, true);
   250 	arc = Parent::findArc(t, s);
   250         arc = Parent::findArc(t, s);
   251 	if (arc != INVALID) return direct(arc, false);	
   251         if (arc != INVALID) return direct(arc, false);
   252       } else {
   252       } else {
   253 	Edge arc = Parent::findArc(t, s, p);
   253         Edge arc = Parent::findArc(t, s, p);
   254 	if (arc != INVALID) return direct(arc, false);	      
   254         if (arc != INVALID) return direct(arc, false);
   255       }
   255       }
   256       return INVALID;
   256       return INVALID;
   257     }
   257     }
   258 
   258 
   259     Edge findEdge(Node s, Node t, Edge p = INVALID) const {
   259     Edge findEdge(Node s, Node t, Edge p = INVALID) const {
   265           if (arc != INVALID) return arc;
   265           if (arc != INVALID) return arc;
   266         } else if (Parent::s(p) == s) {
   266         } else if (Parent::s(p) == s) {
   267           Edge arc = Parent::findArc(s, t, p);
   267           Edge arc = Parent::findArc(s, t, p);
   268           if (arc != INVALID) return arc;
   268           if (arc != INVALID) return arc;
   269           arc = Parent::findArc(t, s);
   269           arc = Parent::findArc(t, s);
   270           if (arc != INVALID) return arc;	
   270           if (arc != INVALID) return arc;
   271         } else {
   271         } else {
   272           Edge arc = Parent::findArc(t, s, p);
   272           Edge arc = Parent::findArc(t, s, p);
   273           if (arc != INVALID) return arc;	      
   273           if (arc != INVALID) return arc;
   274         }
   274         }
   275       } else {
   275       } else {
   276         return Parent::findArc(s, t, p);
   276         return Parent::findArc(s, t, p);
   277       }
   277       }
   278       return INVALID;
   278       return INVALID;
   297     class Red : public Node {
   297     class Red : public Node {
   298       friend class BidirBpGraphExtender;
   298       friend class BidirBpGraphExtender;
   299     public:
   299     public:
   300       Red() {}
   300       Red() {}
   301       Red(const Node& node) : Node(node) {
   301       Red(const Node& node) : Node(node) {
   302 	LEMON_ASSERT(Parent::red(node) || node == INVALID, 
   302         LEMON_ASSERT(Parent::red(node) || node == INVALID,
   303 		     typename Parent::NodeSetError());
   303                      typename Parent::NodeSetError());
   304       }
   304       }
   305       Red& operator=(const Node& node) {
   305       Red& operator=(const Node& node) {
   306 	LEMON_ASSERT(Parent::red(node) || node == INVALID, 
   306         LEMON_ASSERT(Parent::red(node) || node == INVALID,
   307 		     typename Parent::NodeSetError());
   307                      typename Parent::NodeSetError());
   308         Node::operator=(node);
   308         Node::operator=(node);
   309         return *this;
   309         return *this;
   310       }
   310       }
   311       Red(Invalid) : Node(INVALID) {}
   311       Red(Invalid) : Node(INVALID) {}
   312       Red& operator=(Invalid) {
   312       Red& operator=(Invalid) {
   329     class Blue : public Node {
   329     class Blue : public Node {
   330       friend class BidirBpGraphExtender;
   330       friend class BidirBpGraphExtender;
   331     public:
   331     public:
   332       Blue() {}
   332       Blue() {}
   333       Blue(const Node& node) : Node(node) {
   333       Blue(const Node& node) : Node(node) {
   334 	LEMON_ASSERT(Parent::blue(node) || node == INVALID,
   334         LEMON_ASSERT(Parent::blue(node) || node == INVALID,
   335 		     typename Parent::NodeSetError());
   335                      typename Parent::NodeSetError());
   336       }
   336       }
   337       Blue& operator=(const Node& node) {
   337       Blue& operator=(const Node& node) {
   338 	LEMON_ASSERT(Parent::blue(node) || node == INVALID, 
   338         LEMON_ASSERT(Parent::blue(node) || node == INVALID,
   339 		     typename Parent::NodeSetError());
   339                      typename Parent::NodeSetError());
   340         Node::operator=(node);
   340         Node::operator=(node);
   341         return *this;
   341         return *this;
   342       }
   342       }
   343       Blue(Invalid) : Node(INVALID) {}
   343       Blue(Invalid) : Node(INVALID) {}
   344       Blue& operator=(Invalid) {
   344       Blue& operator=(Invalid) {
   351       Parent::firstBlue(static_cast<Node&>(node));
   351       Parent::firstBlue(static_cast<Node&>(node));
   352     }
   352     }
   353     void next(Blue& node) const {
   353     void next(Blue& node) const {
   354       Parent::nextBlue(static_cast<Node&>(node));
   354       Parent::nextBlue(static_cast<Node&>(node));
   355     }
   355     }
   356   
   356 
   357     int id(const Blue& node) const {
   357     int id(const Blue& node) const {
   358       return Parent::redId(node);
   358       return Parent::redId(node);
   359     }
   359     }
   360 
   360 
   361     Node source(const Edge& arc) const {
   361     Node source(const Edge& arc) const {
   365       return blue(arc);
   365       return blue(arc);
   366     }
   366     }
   367 
   367 
   368     void firstInc(Edge& arc, bool& dir, const Node& node) const {
   368     void firstInc(Edge& arc, bool& dir, const Node& node) const {
   369       if (Parent::red(node)) {
   369       if (Parent::red(node)) {
   370 	Parent::firstFromRed(arc, node);
   370         Parent::firstFromRed(arc, node);
   371 	dir = true;
   371         dir = true;
   372       } else {
   372       } else {
   373 	Parent::firstFromBlue(arc, node);
   373         Parent::firstFromBlue(arc, node);
   374 	dir = static_cast<Edge&>(arc) == INVALID;
   374         dir = static_cast<Edge&>(arc) == INVALID;
   375       }
   375       }
   376     }
   376     }
   377     void nextInc(Edge& arc, bool& dir) const {
   377     void nextInc(Edge& arc, bool& dir) const {
   378       if (dir) {
   378       if (dir) {
   379 	Parent::nextFromRed(arc);
   379         Parent::nextFromRed(arc);
   380       } else {
   380       } else {
   381 	Parent::nextFromBlue(arc);
   381         Parent::nextFromBlue(arc);
   382 	if (arc == INVALID) dir = true;
   382         if (arc == INVALID) dir = true;
   383       }
   383       }
   384     }
   384     }
   385 
   385 
   386     class Arc : public Edge {
   386     class Arc : public Edge {
   387       friend class BidirBpGraphExtender;
   387       friend class BidirBpGraphExtender;
   388     protected:
   388     protected:
   389       bool forward;
   389       bool forward;
   390 
   390 
   391       Arc(const Edge& arc, bool _forward)
   391       Arc(const Edge& arc, bool _forward)
   392 	: Edge(arc), forward(_forward) {}
   392         : Edge(arc), forward(_forward) {}
   393 
   393 
   394     public:
   394     public:
   395       Arc() {}
   395       Arc() {}
   396       Arc (Invalid) : Edge(INVALID), forward(true) {}
   396       Arc (Invalid) : Edge(INVALID), forward(true) {}
   397       bool operator==(const Arc& i) const {
   397       bool operator==(const Arc& i) const {
   398 	return Edge::operator==(i) && forward == i.forward;
   398         return Edge::operator==(i) && forward == i.forward;
   399       }
   399       }
   400       bool operator!=(const Arc& i) const {
   400       bool operator!=(const Arc& i) const {
   401 	return Edge::operator!=(i) || forward != i.forward;
   401         return Edge::operator!=(i) || forward != i.forward;
   402       }
   402       }
   403       bool operator<(const Arc& i) const {
   403       bool operator<(const Arc& i) const {
   404 	return Edge::operator<(i) || 
   404         return Edge::operator<(i) ||
   405 	  (!(i.forward<forward) && Edge(*this)<Edge(i));
   405           (!(i.forward<forward) && Edge(*this)<Edge(i));
   406       }
   406       }
   407     };
   407     };
   408 
   408 
   409     void first(Arc& arc) const {
   409     void first(Arc& arc) const {
   410       Parent::first(static_cast<Edge&>(arc));
   410       Parent::first(static_cast<Edge&>(arc));
   411       arc.forward = true;
   411       arc.forward = true;
   412     }
   412     }
   413 
   413 
   414     void next(Arc& arc) const {
   414     void next(Arc& arc) const {
   415       if (!arc.forward) {
   415       if (!arc.forward) {
   416 	Parent::next(static_cast<Edge&>(arc));
   416         Parent::next(static_cast<Edge&>(arc));
   417       }
   417       }
   418       arc.forward = !arc.forward;
   418       arc.forward = !arc.forward;
   419     }
   419     }
   420 
   420 
   421     void firstOut(Arc& arc, const Node& node) const {
   421     void firstOut(Arc& arc, const Node& node) const {
   422       if (Parent::red(node)) {
   422       if (Parent::red(node)) {
   423 	Parent::firstFromRed(arc, node);
   423         Parent::firstFromRed(arc, node);
   424 	arc.forward = true;
   424         arc.forward = true;
   425       } else {
   425       } else {
   426 	Parent::firstFromBlue(arc, node);
   426         Parent::firstFromBlue(arc, node);
   427 	arc.forward = static_cast<Edge&>(arc) == INVALID;
   427         arc.forward = static_cast<Edge&>(arc) == INVALID;
   428       }
   428       }
   429     }
   429     }
   430     void nextOut(Arc& arc) const {
   430     void nextOut(Arc& arc) const {
   431       if (arc.forward) {
   431       if (arc.forward) {
   432 	Parent::nextFromRed(arc);
   432         Parent::nextFromRed(arc);
   433       } else {
   433       } else {
   434 	Parent::nextFromBlue(arc);
   434         Parent::nextFromBlue(arc);
   435         arc.forward = static_cast<Edge&>(arc) == INVALID;
   435         arc.forward = static_cast<Edge&>(arc) == INVALID;
   436       }
   436       }
   437     }
   437     }
   438 
   438 
   439     void firstIn(Arc& arc, const Node& node) const {
   439     void firstIn(Arc& arc, const Node& node) const {
   440       if (Parent::blue(node)) {
   440       if (Parent::blue(node)) {
   441 	Parent::firstFromBlue(arc, node);
   441         Parent::firstFromBlue(arc, node);
   442 	arc.forward = true;	
   442         arc.forward = true;
   443       } else {
   443       } else {
   444 	Parent::firstFromRed(arc, node);
   444         Parent::firstFromRed(arc, node);
   445 	arc.forward = static_cast<Edge&>(arc) == INVALID;
   445         arc.forward = static_cast<Edge&>(arc) == INVALID;
   446       }
   446       }
   447     }
   447     }
   448     void nextIn(Arc& arc) const {
   448     void nextIn(Arc& arc) const {
   449       if (arc.forward) {
   449       if (arc.forward) {
   450 	Parent::nextFromBlue(arc);
   450         Parent::nextFromBlue(arc);
   451       } else {
   451       } else {
   452 	Parent::nextFromRed(arc);
   452         Parent::nextFromRed(arc);
   453 	arc.forward = static_cast<Edge&>(arc) == INVALID;
   453         arc.forward = static_cast<Edge&>(arc) == INVALID;
   454       }
   454       }
   455     }
   455     }
   456 
   456 
   457     Node source(const Arc& arc) const {
   457     Node source(const Arc& arc) const {
   458       return arc.forward ? Parent::red(arc) : Parent::blue(arc);
   458       return arc.forward ? Parent::red(arc) : Parent::blue(arc);
   460     Node target(const Arc& arc) const {
   460     Node target(const Arc& arc) const {
   461       return arc.forward ? Parent::blue(arc) : Parent::red(arc);
   461       return arc.forward ? Parent::blue(arc) : Parent::red(arc);
   462     }
   462     }
   463 
   463 
   464     int id(const Arc& arc) const {
   464     int id(const Arc& arc) const {
   465       return (Parent::id(static_cast<const Edge&>(arc)) << 1) + 
   465       return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
   466         (arc.forward ? 0 : 1);
   466         (arc.forward ? 0 : 1);
   467     }
   467     }
   468     Arc arcFromId(int ix) const {
   468     Arc arcFromId(int ix) const {
   469       return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
   469       return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
   470     }
   470     }