Oops. I did not mean to break the repository.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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>
33 ///\brief Extenders for the graph types
36 /// \ingroup graphbits
38 /// \brief BaseGraph to BaseUGraph extender
39 template <typename Base>
40 class UndirGraphExtender : public Base {
45 typedef typename Parent::Edge UEdge;
46 typedef typename Parent::Node Node;
48 typedef True UndirectedTag;
50 class Edge : public UEdge {
51 friend class UndirGraphExtender;
56 Edge(const UEdge &ue, bool _forward) :
57 UEdge(ue), forward(_forward) {}
62 /// Invalid edge constructor
63 Edge(Invalid i) : UEdge(i), forward(true) {}
65 bool operator==(const Edge &that) const {
66 return forward==that.forward && UEdge(*this)==UEdge(that);
68 bool operator!=(const Edge &that) const {
69 return forward!=that.forward || UEdge(*this)!=UEdge(that);
71 bool operator<(const Edge &that) const {
72 return forward<that.forward ||
73 (!(that.forward<forward) && UEdge(*this)<UEdge(that));
81 /// Source of the given Edge.
82 Node source(const Edge &e) const {
83 return e.forward ? Parent::source(e) : Parent::target(e);
88 /// Target of the given Edge.
89 Node target(const Edge &e) const {
90 return e.forward ? Parent::target(e) : Parent::source(e);
93 /// \brief Directed edge from an undirected edge.
95 /// Returns a directed edge corresponding to the specified UEdge.
96 /// If the given bool is true the given undirected edge and the
97 /// returned edge have the same source node.
98 static Edge direct(const UEdge &ue, bool d) {
102 /// Returns whether the given directed edge is same orientation as the
103 /// corresponding undirected edge.
105 /// \todo reference to the corresponding point of the undirected graph
106 /// concept. "What does the direction of an undirected edge mean?"
107 static bool direction(const Edge &e) { return e.forward; }
113 void first(Edge &e) const {
118 void next(Edge &e) const {
128 void firstOut(Edge &e, const Node &n) const {
129 Parent::firstIn(e,n);
130 if( UEdge(e) != INVALID ) {
134 Parent::firstOut(e,n);
138 void nextOut(Edge &e) const {
140 Node n = Parent::target(e);
142 if( UEdge(e) == INVALID ) {
143 Parent::firstOut(e, n);
152 void firstIn(Edge &e, const Node &n) const {
153 Parent::firstOut(e,n);
154 if( UEdge(e) != INVALID ) {
158 Parent::firstIn(e,n);
162 void nextIn(Edge &e) const {
164 Node n = Parent::source(e);
166 if( UEdge(e) == INVALID ) {
167 Parent::firstIn(e, n);
176 void firstInc(UEdge &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(UEdge &e, bool &d) const {
186 Node s = Parent::source(e);
188 if (e != INVALID) return;
190 Parent::firstIn(e, s);
196 Node nodeFromId(int id) const {
197 return Parent::nodeFromId(id);
200 Edge edgeFromId(int id) const {
201 return direct(Parent::edgeFromId(id >> 1), bool(id & 1));
204 UEdge uEdgeFromId(int id) const {
205 return Parent::edgeFromId(id);
208 int id(const Node &n) const {
209 return Parent::id(n);
212 int id(const UEdge &e) const {
213 return Parent::id(e);
216 int id(const Edge &e) const {
217 return 2 * Parent::id(e) + int(e.forward);
220 int maxNodeId() const {
221 return Parent::maxNodeId();
224 int maxEdgeId() const {
225 return 2 * Parent::maxEdgeId() + 1;
228 int maxUEdgeId() const {
229 return Parent::maxEdgeId();
233 int edgeNum() const {
234 return 2 * Parent::edgeNum();
237 int uEdgeNum() const {
238 return Parent::edgeNum();
241 Edge findEdge(Node source, Node target, Edge prev = INVALID) const {
242 if (prev == INVALID) {
243 UEdge edge = Parent::findEdge(source, target);
244 if (edge != INVALID) return direct(edge, true);
245 edge = Parent::findEdge(target, source);
246 if (edge != INVALID) return direct(edge, false);
247 } else if (direction(prev)) {
248 UEdge edge = Parent::findEdge(source, target, prev);
249 if (edge != INVALID) return direct(edge, true);
250 edge = Parent::findEdge(target, source);
251 if (edge != INVALID) return direct(edge, false);
253 UEdge edge = Parent::findEdge(target, source, prev);
254 if (edge != INVALID) return direct(edge, false);
259 UEdge findUEdge(Node source, Node target, UEdge prev = INVALID) const {
260 if (source != target) {
261 if (prev == INVALID) {
262 UEdge edge = Parent::findEdge(source, target);
263 if (edge != INVALID) return edge;
264 edge = Parent::findEdge(target, source);
265 if (edge != INVALID) return edge;
266 } else if (Parent::source(prev) == source) {
267 UEdge edge = Parent::findEdge(source, target, prev);
268 if (edge != INVALID) return edge;
269 edge = Parent::findEdge(target, source);
270 if (edge != INVALID) return edge;
272 UEdge edge = Parent::findEdge(target, source, prev);
273 if (edge != INVALID) return edge;
276 return Parent::findEdge(source, target, prev);
282 template <typename Base>
283 class BidirBpUGraphExtender : public Base {
286 typedef BidirBpUGraphExtender Graph;
288 typedef typename Parent::Node Node;
289 typedef typename Parent::UEdge UEdge;
297 class ANode : public Node {
298 friend class BidirBpUGraphExtender;
301 ANode(const Node& node) : Node(node) {
302 LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
303 typename Parent::NodeSetError());
305 ANode& operator=(const Node& node) {
306 LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
307 typename Parent::NodeSetError());
308 Node::operator=(node);
311 ANode(Invalid) : Node(INVALID) {}
314 void first(ANode& node) const {
315 Parent::firstANode(static_cast<Node&>(node));
317 void next(ANode& node) const {
318 Parent::nextANode(static_cast<Node&>(node));
321 int id(const ANode& node) const {
322 return Parent::aNodeId(node);
325 class BNode : public Node {
326 friend class BidirBpUGraphExtender;
329 BNode(const Node& node) : Node(node) {
330 LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
331 typename Parent::NodeSetError());
333 BNode& operator=(const Node& node) {
334 LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
335 typename Parent::NodeSetError());
336 Node::operator=(node);
339 BNode(Invalid) : Node(INVALID) {}
342 void first(BNode& node) const {
343 Parent::firstBNode(static_cast<Node&>(node));
345 void next(BNode& node) const {
346 Parent::nextBNode(static_cast<Node&>(node));
349 int id(const BNode& node) const {
350 return Parent::aNodeId(node);
353 Node source(const UEdge& edge) const {
356 Node target(const UEdge& edge) const {
360 void firstInc(UEdge& edge, bool& direction, const Node& node) const {
361 if (Parent::aNode(node)) {
362 Parent::firstFromANode(edge, node);
365 Parent::firstFromBNode(edge, node);
366 direction = static_cast<UEdge&>(edge) == INVALID;
369 void nextInc(UEdge& edge, bool& direction) const {
371 Parent::nextFromANode(edge);
373 Parent::nextFromBNode(edge);
374 if (edge == INVALID) direction = true;
378 class Edge : public UEdge {
379 friend class BidirBpUGraphExtender;
383 Edge(const UEdge& edge, bool _forward)
384 : UEdge(edge), forward(_forward) {}
388 Edge (Invalid) : UEdge(INVALID), forward(true) {}
389 bool operator==(const Edge& i) const {
390 return UEdge::operator==(i) && forward == i.forward;
392 bool operator!=(const Edge& i) const {
393 return UEdge::operator!=(i) || forward != i.forward;
395 bool operator<(const Edge& i) const {
396 return UEdge::operator<(i) ||
397 (!(i.forward<forward) && UEdge(*this)<UEdge(i));
401 void first(Edge& edge) const {
402 Parent::first(static_cast<UEdge&>(edge));
406 void next(Edge& edge) const {
408 Parent::next(static_cast<UEdge&>(edge));
410 edge.forward = !edge.forward;
413 void firstOut(Edge& edge, const Node& node) const {
414 if (Parent::aNode(node)) {
415 Parent::firstFromANode(edge, node);
418 Parent::firstFromBNode(edge, node);
419 edge.forward = static_cast<UEdge&>(edge) == INVALID;
422 void nextOut(Edge& edge) const {
424 Parent::nextFromANode(edge);
426 Parent::nextFromBNode(edge);
427 edge.forward = static_cast<UEdge&>(edge) == INVALID;
431 void firstIn(Edge& edge, const Node& node) const {
432 if (Parent::bNode(node)) {
433 Parent::firstFromBNode(edge, node);
436 Parent::firstFromANode(edge, node);
437 edge.forward = static_cast<UEdge&>(edge) == INVALID;
440 void nextIn(Edge& edge) const {
442 Parent::nextFromBNode(edge);
444 Parent::nextFromANode(edge);
445 edge.forward = static_cast<UEdge&>(edge) == INVALID;
449 Node source(const Edge& edge) const {
450 return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
452 Node target(const Edge& edge) const {
453 return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
456 int id(const Edge& edge) const {
457 return (Parent::id(static_cast<const UEdge&>(edge)) << 1) +
458 (edge.forward ? 0 : 1);
460 Edge edgeFromId(int id) const {
461 return Edge(Parent::fromUEdgeId(id >> 1), (id & 1) == 0);
463 int maxEdgeId() const {
464 return (Parent::maxUEdgeId() << 1) + 1;
467 bool direction(const Edge& edge) const {
471 Edge direct(const UEdge& edge, bool direction) const {
472 return Edge(edge, direction);
475 int edgeNum() const {
476 return 2 * Parent::uEdgeNum();
479 int uEdgeNum() const {
480 return Parent::uEdgeNum();