Wrong member variable settings bug fix. (Ticket #95)
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_BASE_EXTENDER_H
20 #define LEMON_BITS_BASE_EXTENDER_H
22 #include <lemon/bits/invalid.h>
23 #include <lemon/error.h>
25 #include <lemon/bits/map_extender.h>
26 #include <lemon/bits/default_map.h>
28 #include <lemon/concept_check.h>
29 #include <lemon/concepts/maps.h>
31 ///\ingroup digraphbits
33 ///\brief Extenders for the digraph types
36 /// \ingroup digraphbits
38 /// \brief BaseDigraph to BaseGraph extender
39 template <typename Base>
40 class UndirDigraphExtender : public Base {
45 typedef typename Parent::Arc Edge;
46 typedef typename Parent::Node Node;
48 typedef True UndirectedTag;
50 class Arc : public Edge {
51 friend class UndirDigraphExtender;
56 Arc(const Edge &ue, bool _forward) :
57 Edge(ue), forward(_forward) {}
62 /// Invalid arc constructor
63 Arc(Invalid i) : Edge(i), forward(true) {}
65 bool operator==(const Arc &that) const {
66 return forward==that.forward && Edge(*this)==Edge(that);
68 bool operator!=(const Arc &that) const {
69 return forward!=that.forward || Edge(*this)!=Edge(that);
71 bool operator<(const Arc &that) const {
72 return forward<that.forward ||
73 (!(that.forward<forward) && Edge(*this)<Edge(that));
81 /// Source of the given Arc.
82 Node source(const Arc &e) const {
83 return e.forward ? Parent::source(e) : Parent::target(e);
88 /// Target of the given Arc.
89 Node target(const Arc &e) const {
90 return e.forward ? Parent::target(e) : Parent::source(e);
93 /// \brief Directed arc from an edge.
95 /// Returns a directed arc corresponding to the specified Edge.
96 /// If the given bool is true the given edge and the
97 /// returned arc have the same source node.
98 static Arc direct(const Edge &ue, bool d) {
102 /// Returns whether the given directed arc is same orientation as the
103 /// corresponding edge.
105 /// \todo reference to the corresponding point of the undirected digraph
106 /// concept. "What does the direction of an edge mean?"
107 static bool direction(const Arc &e) { return e.forward; }
113 void first(Arc &e) const {
118 void next(Arc &e) const {
128 void firstOut(Arc &e, const Node &n) const {
129 Parent::firstIn(e,n);
130 if( Edge(e) != INVALID ) {
134 Parent::firstOut(e,n);
138 void nextOut(Arc &e) const {
140 Node n = Parent::target(e);
142 if( Edge(e) == INVALID ) {
143 Parent::firstOut(e, n);
152 void firstIn(Arc &e, const Node &n) const {
153 Parent::firstOut(e,n);
154 if( Edge(e) != INVALID ) {
158 Parent::firstIn(e,n);
162 void nextIn(Arc &e) const {
164 Node n = Parent::source(e);
166 if( Edge(e) == INVALID ) {
167 Parent::firstIn(e, n);
176 void firstInc(Edge &e, bool &d, const Node &n) const {
178 Parent::firstOut(e, n);
179 if (e != INVALID) return;
181 Parent::firstIn(e, n);
184 void nextInc(Edge &e, bool &d) const {
186 Node s = Parent::source(e);
188 if (e != INVALID) return;
190 Parent::firstIn(e, s);
196 Node nodeFromId(int ix) const {
197 return Parent::nodeFromId(ix);
200 Arc arcFromId(int ix) const {
201 return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
204 Edge edgeFromId(int ix) const {
205 return Parent::arcFromId(ix);
208 int id(const Node &n) const {
209 return Parent::id(n);
212 int id(const Edge &e) const {
213 return Parent::id(e);
216 int id(const Arc &e) const {
217 return 2 * Parent::id(e) + int(e.forward);
220 int maxNodeId() const {
221 return Parent::maxNodeId();
224 int maxArcId() const {
225 return 2 * Parent::maxArcId() + 1;
228 int maxEdgeId() const {
229 return Parent::maxArcId();
234 return 2 * Parent::arcNum();
237 int edgeNum() const {
238 return Parent::arcNum();
241 Arc findArc(Node s, Node t, Arc p = INVALID) const {
243 Edge arc = Parent::findArc(s, t);
244 if (arc != INVALID) return direct(arc, true);
245 arc = Parent::findArc(t, s);
246 if (arc != INVALID) return direct(arc, false);
247 } else if (direction(p)) {
248 Edge arc = Parent::findArc(s, t, p);
249 if (arc != INVALID) return direct(arc, true);
250 arc = Parent::findArc(t, s);
251 if (arc != INVALID) return direct(arc, false);
253 Edge arc = Parent::findArc(t, s, p);
254 if (arc != INVALID) return direct(arc, false);
259 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
262 Edge arc = Parent::findArc(s, t);
263 if (arc != INVALID) return arc;
264 arc = Parent::findArc(t, s);
265 if (arc != INVALID) return arc;
266 } else if (Parent::s(p) == s) {
267 Edge arc = Parent::findArc(s, t, p);
268 if (arc != INVALID) return arc;
269 arc = Parent::findArc(t, s);
270 if (arc != INVALID) return arc;
272 Edge arc = Parent::findArc(t, s, p);
273 if (arc != INVALID) return arc;
276 return Parent::findArc(s, t, p);
282 template <typename Base>
283 class BidirBpGraphExtender : public Base {
286 typedef BidirBpGraphExtender Digraph;
288 typedef typename Parent::Node Node;
289 typedef typename Parent::Edge Edge;
297 class Red : public Node {
298 friend class BidirBpGraphExtender;
301 Red(const Node& node) : Node(node) {
302 LEMON_ASSERT(Parent::red(node) || node == INVALID,
303 typename Parent::NodeSetError());
305 Red& operator=(const Node& node) {
306 LEMON_ASSERT(Parent::red(node) || node == INVALID,
307 typename Parent::NodeSetError());
308 Node::operator=(node);
311 Red(Invalid) : Node(INVALID) {}
312 Red& operator=(Invalid) {
313 Node::operator=(INVALID);
318 void first(Red& node) const {
319 Parent::firstRed(static_cast<Node&>(node));
321 void next(Red& node) const {
322 Parent::nextRed(static_cast<Node&>(node));
325 int id(const Red& node) const {
326 return Parent::redId(node);
329 class Blue : public Node {
330 friend class BidirBpGraphExtender;
333 Blue(const Node& node) : Node(node) {
334 LEMON_ASSERT(Parent::blue(node) || node == INVALID,
335 typename Parent::NodeSetError());
337 Blue& operator=(const Node& node) {
338 LEMON_ASSERT(Parent::blue(node) || node == INVALID,
339 typename Parent::NodeSetError());
340 Node::operator=(node);
343 Blue(Invalid) : Node(INVALID) {}
344 Blue& operator=(Invalid) {
345 Node::operator=(INVALID);
350 void first(Blue& node) const {
351 Parent::firstBlue(static_cast<Node&>(node));
353 void next(Blue& node) const {
354 Parent::nextBlue(static_cast<Node&>(node));
357 int id(const Blue& node) const {
358 return Parent::redId(node);
361 Node source(const Edge& arc) const {
364 Node target(const Edge& arc) const {
368 void firstInc(Edge& arc, bool& dir, const Node& node) const {
369 if (Parent::red(node)) {
370 Parent::firstFromRed(arc, node);
373 Parent::firstFromBlue(arc, node);
374 dir = static_cast<Edge&>(arc) == INVALID;
377 void nextInc(Edge& arc, bool& dir) const {
379 Parent::nextFromRed(arc);
381 Parent::nextFromBlue(arc);
382 if (arc == INVALID) dir = true;
386 class Arc : public Edge {
387 friend class BidirBpGraphExtender;
391 Arc(const Edge& arc, bool _forward)
392 : Edge(arc), forward(_forward) {}
396 Arc (Invalid) : Edge(INVALID), forward(true) {}
397 bool operator==(const Arc& i) const {
398 return Edge::operator==(i) && forward == i.forward;
400 bool operator!=(const Arc& i) const {
401 return Edge::operator!=(i) || forward != i.forward;
403 bool operator<(const Arc& i) const {
404 return Edge::operator<(i) ||
405 (!(i.forward<forward) && Edge(*this)<Edge(i));
409 void first(Arc& arc) const {
410 Parent::first(static_cast<Edge&>(arc));
414 void next(Arc& arc) const {
416 Parent::next(static_cast<Edge&>(arc));
418 arc.forward = !arc.forward;
421 void firstOut(Arc& arc, const Node& node) const {
422 if (Parent::red(node)) {
423 Parent::firstFromRed(arc, node);
426 Parent::firstFromBlue(arc, node);
427 arc.forward = static_cast<Edge&>(arc) == INVALID;
430 void nextOut(Arc& arc) const {
432 Parent::nextFromRed(arc);
434 Parent::nextFromBlue(arc);
435 arc.forward = static_cast<Edge&>(arc) == INVALID;
439 void firstIn(Arc& arc, const Node& node) const {
440 if (Parent::blue(node)) {
441 Parent::firstFromBlue(arc, node);
444 Parent::firstFromRed(arc, node);
445 arc.forward = static_cast<Edge&>(arc) == INVALID;
448 void nextIn(Arc& arc) const {
450 Parent::nextFromBlue(arc);
452 Parent::nextFromRed(arc);
453 arc.forward = static_cast<Edge&>(arc) == INVALID;
457 Node source(const Arc& arc) const {
458 return arc.forward ? Parent::red(arc) : Parent::blue(arc);
460 Node target(const Arc& arc) const {
461 return arc.forward ? Parent::blue(arc) : Parent::red(arc);
464 int id(const Arc& arc) const {
465 return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
466 (arc.forward ? 0 : 1);
468 Arc arcFromId(int ix) const {
469 return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
471 int maxArcId() const {
472 return (Parent::maxEdgeId() << 1) + 1;
475 bool direction(const Arc& arc) const {
479 Arc direct(const Edge& arc, bool dir) const {
480 return Arc(arc, dir);
484 return 2 * Parent::edgeNum();
487 int edgeNum() const {
488 return Parent::edgeNum();