1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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_GRAPH_ADAPTOR_EXTENDER_H
20 #define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
22 #include <lemon/core.h>
23 #include <lemon/error.h>
27 template <typename _Digraph>
28 class DigraphAdaptorExtender : public _Digraph {
29 typedef _Digraph Parent;
33 typedef _Digraph Digraph;
34 typedef DigraphAdaptorExtender Adaptor;
38 typedef typename Parent::Node Node;
39 typedef typename Parent::Arc Arc;
41 int maxId(Node) const {
42 return Parent::maxNodeId();
45 int maxId(Arc) const {
46 return Parent::maxArcId();
49 Node fromId(int id, Node) const {
50 return Parent::nodeFromId(id);
53 Arc fromId(int id, Arc) const {
54 return Parent::arcFromId(id);
57 Node oppositeNode(const Node &n, const Arc &e) const {
58 if (n == Parent::source(e))
59 return Parent::target(e);
60 else if(n==Parent::target(e))
61 return Parent::source(e);
66 class NodeIt : public Node {
67 const Adaptor* _adaptor;
72 NodeIt(Invalid i) : Node(i) { }
74 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
75 _adaptor->first(static_cast<Node&>(*this));
78 NodeIt(const Adaptor& adaptor, const Node& node)
79 : Node(node), _adaptor(&adaptor) {}
81 NodeIt& operator++() {
82 _adaptor->next(*this);
89 class ArcIt : public Arc {
90 const Adaptor* _adaptor;
95 ArcIt(Invalid i) : Arc(i) { }
97 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
98 _adaptor->first(static_cast<Arc&>(*this));
101 ArcIt(const Adaptor& adaptor, const Arc& e) :
102 Arc(e), _adaptor(&adaptor) { }
104 ArcIt& operator++() {
105 _adaptor->next(*this);
112 class OutArcIt : public Arc {
113 const Adaptor* _adaptor;
118 OutArcIt(Invalid i) : Arc(i) { }
120 OutArcIt(const Adaptor& adaptor, const Node& node)
121 : _adaptor(&adaptor) {
122 _adaptor->firstOut(*this, node);
125 OutArcIt(const Adaptor& adaptor, const Arc& arc)
126 : Arc(arc), _adaptor(&adaptor) {}
128 OutArcIt& operator++() {
129 _adaptor->nextOut(*this);
136 class InArcIt : public Arc {
137 const Adaptor* _adaptor;
142 InArcIt(Invalid i) : Arc(i) { }
144 InArcIt(const Adaptor& adaptor, const Node& node)
145 : _adaptor(&adaptor) {
146 _adaptor->firstIn(*this, node);
149 InArcIt(const Adaptor& adaptor, const Arc& arc) :
150 Arc(arc), _adaptor(&adaptor) {}
152 InArcIt& operator++() {
153 _adaptor->nextIn(*this);
159 Node baseNode(const OutArcIt &e) const {
160 return Parent::source(e);
162 Node runningNode(const OutArcIt &e) const {
163 return Parent::target(e);
166 Node baseNode(const InArcIt &e) const {
167 return Parent::target(e);
169 Node runningNode(const InArcIt &e) const {
170 return Parent::source(e);
175 template <typename _Graph>
176 class GraphAdaptorExtender : public _Graph {
177 typedef _Graph Parent;
181 typedef _Graph Graph;
182 typedef GraphAdaptorExtender Adaptor;
184 typedef True UndirectedTag;
186 typedef typename Parent::Node Node;
187 typedef typename Parent::Arc Arc;
188 typedef typename Parent::Edge Edge;
192 int maxId(Node) const {
193 return Parent::maxNodeId();
196 int maxId(Arc) const {
197 return Parent::maxArcId();
200 int maxId(Edge) const {
201 return Parent::maxEdgeId();
204 Node fromId(int id, Node) const {
205 return Parent::nodeFromId(id);
208 Arc fromId(int id, Arc) const {
209 return Parent::arcFromId(id);
212 Edge fromId(int id, Edge) const {
213 return Parent::edgeFromId(id);
216 Node oppositeNode(const Node &n, const Edge &e) const {
217 if( n == Parent::u(e))
219 else if( n == Parent::v(e))
225 Arc oppositeArc(const Arc &a) const {
226 return Parent::direct(a, !Parent::direction(a));
229 using Parent::direct;
230 Arc direct(const Edge &e, const Node &s) const {
231 return Parent::direct(e, Parent::u(e) == s);
235 class NodeIt : public Node {
236 const Adaptor* _adaptor;
241 NodeIt(Invalid i) : Node(i) { }
243 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
244 _adaptor->first(static_cast<Node&>(*this));
247 NodeIt(const Adaptor& adaptor, const Node& node)
248 : Node(node), _adaptor(&adaptor) {}
250 NodeIt& operator++() {
251 _adaptor->next(*this);
258 class ArcIt : public Arc {
259 const Adaptor* _adaptor;
264 ArcIt(Invalid i) : Arc(i) { }
266 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
267 _adaptor->first(static_cast<Arc&>(*this));
270 ArcIt(const Adaptor& adaptor, const Arc& e) :
271 Arc(e), _adaptor(&adaptor) { }
273 ArcIt& operator++() {
274 _adaptor->next(*this);
281 class OutArcIt : public Arc {
282 const Adaptor* _adaptor;
287 OutArcIt(Invalid i) : Arc(i) { }
289 OutArcIt(const Adaptor& adaptor, const Node& node)
290 : _adaptor(&adaptor) {
291 _adaptor->firstOut(*this, node);
294 OutArcIt(const Adaptor& adaptor, const Arc& arc)
295 : Arc(arc), _adaptor(&adaptor) {}
297 OutArcIt& operator++() {
298 _adaptor->nextOut(*this);
305 class InArcIt : public Arc {
306 const Adaptor* _adaptor;
311 InArcIt(Invalid i) : Arc(i) { }
313 InArcIt(const Adaptor& adaptor, const Node& node)
314 : _adaptor(&adaptor) {
315 _adaptor->firstIn(*this, node);
318 InArcIt(const Adaptor& adaptor, const Arc& arc) :
319 Arc(arc), _adaptor(&adaptor) {}
321 InArcIt& operator++() {
322 _adaptor->nextIn(*this);
328 class EdgeIt : public Parent::Edge {
329 const Adaptor* _adaptor;
334 EdgeIt(Invalid i) : Edge(i) { }
336 explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
337 _adaptor->first(static_cast<Edge&>(*this));
340 EdgeIt(const Adaptor& adaptor, const Edge& e) :
341 Edge(e), _adaptor(&adaptor) { }
343 EdgeIt& operator++() {
344 _adaptor->next(*this);
350 class IncEdgeIt : public Edge {
351 friend class GraphAdaptorExtender;
352 const Adaptor* _adaptor;
358 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
360 IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
361 _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
364 IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
365 : _adaptor(&adaptor), Edge(e) {
366 direction = (_adaptor->u(e) == n);
369 IncEdgeIt& operator++() {
370 _adaptor->nextInc(*this, direction);
375 Node baseNode(const OutArcIt &a) const {
376 return Parent::source(a);
378 Node runningNode(const OutArcIt &a) const {
379 return Parent::target(a);
382 Node baseNode(const InArcIt &a) const {
383 return Parent::target(a);
385 Node runningNode(const InArcIt &a) const {
386 return Parent::source(a);
389 Node baseNode(const IncEdgeIt &e) const {
390 return e.direction ? Parent::u(e) : Parent::v(e);
392 Node runningNode(const IncEdgeIt &e) const {
393 return e.direction ? Parent::v(e) : Parent::u(e);