1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
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/core.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));
77 /// First node of the edge
78 Node u(const Edge &e) const {
79 return Parent::source(e);
82 /// Source of the given arc
83 Node source(const Arc &e) const {
84 return e.forward ? Parent::source(e) : Parent::target(e);
87 /// Second node of the edge
88 Node v(const Edge &e) const {
89 return Parent::target(e);
92 /// Target of the given arc
93 Node target(const Arc &e) const {
94 return e.forward ? Parent::target(e) : Parent::source(e);
97 /// \brief Directed arc from an edge.
99 /// Returns a directed arc corresponding to the specified edge.
100 /// If the given bool is true, the first node of the given edge and
101 /// the source node of the returned arc are the same.
102 static Arc direct(const Edge &e, bool d) {
106 /// Returns whether the given directed arc has the same orientation
107 /// as the corresponding edge.
108 static bool direction(const Arc &a) { return a.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();
233 return 2 * Parent::arcNum();
236 int edgeNum() const {
237 return Parent::arcNum();
240 Arc findArc(Node s, Node t, Arc p = INVALID) const {
242 Edge arc = Parent::findArc(s, t);
243 if (arc != INVALID) return direct(arc, true);
244 arc = Parent::findArc(t, s);
245 if (arc != INVALID) return direct(arc, false);
246 } else if (direction(p)) {
247 Edge arc = Parent::findArc(s, t, p);
248 if (arc != INVALID) return direct(arc, true);
249 arc = Parent::findArc(t, s);
250 if (arc != INVALID) return direct(arc, false);
252 Edge arc = Parent::findArc(t, s, p);
253 if (arc != INVALID) return direct(arc, false);
258 Edge findEdge(Node s, Node t, Edge p = INVALID) const {
261 Edge arc = Parent::findArc(s, t);
262 if (arc != INVALID) return arc;
263 arc = Parent::findArc(t, s);
264 if (arc != INVALID) return arc;
265 } else if (Parent::s(p) == s) {
266 Edge arc = Parent::findArc(s, t, p);
267 if (arc != INVALID) return arc;
268 arc = Parent::findArc(t, s);
269 if (arc != INVALID) return arc;
271 Edge arc = Parent::findArc(t, s, p);
272 if (arc != INVALID) return arc;
275 return Parent::findArc(s, t, p);
281 template <typename Base>
282 class BidirBpGraphExtender : public Base {
285 typedef BidirBpGraphExtender Digraph;
287 typedef typename Parent::Node Node;
288 typedef typename Parent::Edge Edge;
296 class Red : public Node {
297 friend class BidirBpGraphExtender;
300 Red(const Node& node) : Node(node) {
301 LEMON_DEBUG(Parent::red(node) || node == INVALID,
302 typename Parent::NodeSetError());
304 Red& operator=(const Node& node) {
305 LEMON_DEBUG(Parent::red(node) || node == INVALID,
306 typename Parent::NodeSetError());
307 Node::operator=(node);
310 Red(Invalid) : Node(INVALID) {}
311 Red& operator=(Invalid) {
312 Node::operator=(INVALID);
317 void first(Red& node) const {
318 Parent::firstRed(static_cast<Node&>(node));
320 void next(Red& node) const {
321 Parent::nextRed(static_cast<Node&>(node));
324 int id(const Red& node) const {
325 return Parent::redId(node);
328 class Blue : public Node {
329 friend class BidirBpGraphExtender;
332 Blue(const Node& node) : Node(node) {
333 LEMON_DEBUG(Parent::blue(node) || node == INVALID,
334 typename Parent::NodeSetError());
336 Blue& operator=(const Node& node) {
337 LEMON_DEBUG(Parent::blue(node) || node == INVALID,
338 typename Parent::NodeSetError());
339 Node::operator=(node);
342 Blue(Invalid) : Node(INVALID) {}
343 Blue& operator=(Invalid) {
344 Node::operator=(INVALID);
349 void first(Blue& node) const {
350 Parent::firstBlue(static_cast<Node&>(node));
352 void next(Blue& node) const {
353 Parent::nextBlue(static_cast<Node&>(node));
356 int id(const Blue& node) const {
357 return Parent::redId(node);
360 Node source(const Edge& arc) const {
363 Node target(const Edge& arc) const {
367 void firstInc(Edge& arc, bool& dir, const Node& node) const {
368 if (Parent::red(node)) {
369 Parent::firstFromRed(arc, node);
372 Parent::firstFromBlue(arc, node);
373 dir = static_cast<Edge&>(arc) == INVALID;
376 void nextInc(Edge& arc, bool& dir) const {
378 Parent::nextFromRed(arc);
380 Parent::nextFromBlue(arc);
381 if (arc == INVALID) dir = true;
385 class Arc : public Edge {
386 friend class BidirBpGraphExtender;
390 Arc(const Edge& arc, bool _forward)
391 : Edge(arc), forward(_forward) {}
395 Arc (Invalid) : Edge(INVALID), forward(true) {}
396 bool operator==(const Arc& i) const {
397 return Edge::operator==(i) && forward == i.forward;
399 bool operator!=(const Arc& i) const {
400 return Edge::operator!=(i) || forward != i.forward;
402 bool operator<(const Arc& i) const {
403 return Edge::operator<(i) ||
404 (!(i.forward<forward) && Edge(*this)<Edge(i));
408 void first(Arc& arc) const {
409 Parent::first(static_cast<Edge&>(arc));
413 void next(Arc& arc) const {
415 Parent::next(static_cast<Edge&>(arc));
417 arc.forward = !arc.forward;
420 void firstOut(Arc& arc, const Node& node) const {
421 if (Parent::red(node)) {
422 Parent::firstFromRed(arc, node);
425 Parent::firstFromBlue(arc, node);
426 arc.forward = static_cast<Edge&>(arc) == INVALID;
429 void nextOut(Arc& arc) const {
431 Parent::nextFromRed(arc);
433 Parent::nextFromBlue(arc);
434 arc.forward = static_cast<Edge&>(arc) == INVALID;
438 void firstIn(Arc& arc, const Node& node) const {
439 if (Parent::blue(node)) {
440 Parent::firstFromBlue(arc, node);
443 Parent::firstFromRed(arc, node);
444 arc.forward = static_cast<Edge&>(arc) == INVALID;
447 void nextIn(Arc& arc) const {
449 Parent::nextFromBlue(arc);
451 Parent::nextFromRed(arc);
452 arc.forward = static_cast<Edge&>(arc) == INVALID;
456 Node source(const Arc& arc) const {
457 return arc.forward ? Parent::red(arc) : Parent::blue(arc);
459 Node target(const Arc& arc) const {
460 return arc.forward ? Parent::blue(arc) : Parent::red(arc);
463 int id(const Arc& arc) const {
464 return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
465 (arc.forward ? 0 : 1);
467 Arc arcFromId(int ix) const {
468 return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
470 int maxArcId() const {
471 return (Parent::maxEdgeId() << 1) + 1;
474 bool direction(const Arc& arc) const {
478 Arc direct(const Edge& arc, bool dir) const {
479 return Arc(arc, dir);
483 return 2 * Parent::edgeNum();
486 int edgeNum() const {
487 return Parent::edgeNum();