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 {
31 typedef _Digraph Parent;
32 typedef _Digraph Digraph;
33 typedef DigraphAdaptorExtender Adaptor;
37 typedef typename Parent::Node Node;
38 typedef typename Parent::Arc Arc;
40 int maxId(Node) const {
41 return Parent::maxNodeId();
44 int maxId(Arc) const {
45 return Parent::maxArcId();
48 Node fromId(int id, Node) const {
49 return Parent::nodeFromId(id);
52 Arc fromId(int id, Arc) const {
53 return Parent::arcFromId(id);
56 Node oppositeNode(const Node &n, const Arc &e) const {
57 if (n == Parent::source(e))
58 return Parent::target(e);
59 else if(n==Parent::target(e))
60 return Parent::source(e);
65 class NodeIt : public Node {
66 const Adaptor* _adaptor;
71 NodeIt(Invalid i) : Node(i) { }
73 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
74 _adaptor->first(static_cast<Node&>(*this));
77 NodeIt(const Adaptor& adaptor, const Node& node)
78 : Node(node), _adaptor(&adaptor) {}
80 NodeIt& operator++() {
81 _adaptor->next(*this);
88 class ArcIt : public Arc {
89 const Adaptor* _adaptor;
94 ArcIt(Invalid i) : Arc(i) { }
96 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
97 _adaptor->first(static_cast<Arc&>(*this));
100 ArcIt(const Adaptor& adaptor, const Arc& e) :
101 Arc(e), _adaptor(&adaptor) { }
103 ArcIt& operator++() {
104 _adaptor->next(*this);
111 class OutArcIt : public Arc {
112 const Adaptor* _adaptor;
117 OutArcIt(Invalid i) : Arc(i) { }
119 OutArcIt(const Adaptor& adaptor, const Node& node)
120 : _adaptor(&adaptor) {
121 _adaptor->firstOut(*this, node);
124 OutArcIt(const Adaptor& adaptor, const Arc& arc)
125 : Arc(arc), _adaptor(&adaptor) {}
127 OutArcIt& operator++() {
128 _adaptor->nextOut(*this);
135 class InArcIt : public Arc {
136 const Adaptor* _adaptor;
141 InArcIt(Invalid i) : Arc(i) { }
143 InArcIt(const Adaptor& adaptor, const Node& node)
144 : _adaptor(&adaptor) {
145 _adaptor->firstIn(*this, node);
148 InArcIt(const Adaptor& adaptor, const Arc& arc) :
149 Arc(arc), _adaptor(&adaptor) {}
151 InArcIt& operator++() {
152 _adaptor->nextIn(*this);
158 Node baseNode(const OutArcIt &e) const {
159 return Parent::source(e);
161 Node runningNode(const OutArcIt &e) const {
162 return Parent::target(e);
165 Node baseNode(const InArcIt &e) const {
166 return Parent::target(e);
168 Node runningNode(const InArcIt &e) const {
169 return Parent::source(e);
174 template <typename _Graph>
175 class GraphAdaptorExtender : public _Graph {
178 typedef _Graph Parent;
179 typedef _Graph Graph;
180 typedef GraphAdaptorExtender Adaptor;
182 typedef typename Parent::Node Node;
183 typedef typename Parent::Arc Arc;
184 typedef typename Parent::Edge Edge;
188 int maxId(Node) const {
189 return Parent::maxNodeId();
192 int maxId(Arc) const {
193 return Parent::maxArcId();
196 int maxId(Edge) const {
197 return Parent::maxEdgeId();
200 Node fromId(int id, Node) const {
201 return Parent::nodeFromId(id);
204 Arc fromId(int id, Arc) const {
205 return Parent::arcFromId(id);
208 Edge fromId(int id, Edge) const {
209 return Parent::edgeFromId(id);
212 Node oppositeNode(const Node &n, const Edge &e) const {
213 if( n == Parent::u(e))
215 else if( n == Parent::v(e))
221 Arc oppositeArc(const Arc &a) const {
222 return Parent::direct(a, !Parent::direction(a));
225 using Parent::direct;
226 Arc direct(const Edge &e, const Node &s) const {
227 return Parent::direct(e, Parent::u(e) == s);
231 class NodeIt : public Node {
232 const Adaptor* _adaptor;
237 NodeIt(Invalid i) : Node(i) { }
239 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
240 _adaptor->first(static_cast<Node&>(*this));
243 NodeIt(const Adaptor& adaptor, const Node& node)
244 : Node(node), _adaptor(&adaptor) {}
246 NodeIt& operator++() {
247 _adaptor->next(*this);
254 class ArcIt : public Arc {
255 const Adaptor* _adaptor;
260 ArcIt(Invalid i) : Arc(i) { }
262 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
263 _adaptor->first(static_cast<Arc&>(*this));
266 ArcIt(const Adaptor& adaptor, const Arc& e) :
267 Arc(e), _adaptor(&adaptor) { }
269 ArcIt& operator++() {
270 _adaptor->next(*this);
277 class OutArcIt : public Arc {
278 const Adaptor* _adaptor;
283 OutArcIt(Invalid i) : Arc(i) { }
285 OutArcIt(const Adaptor& adaptor, const Node& node)
286 : _adaptor(&adaptor) {
287 _adaptor->firstOut(*this, node);
290 OutArcIt(const Adaptor& adaptor, const Arc& arc)
291 : Arc(arc), _adaptor(&adaptor) {}
293 OutArcIt& operator++() {
294 _adaptor->nextOut(*this);
301 class InArcIt : public Arc {
302 const Adaptor* _adaptor;
307 InArcIt(Invalid i) : Arc(i) { }
309 InArcIt(const Adaptor& adaptor, const Node& node)
310 : _adaptor(&adaptor) {
311 _adaptor->firstIn(*this, node);
314 InArcIt(const Adaptor& adaptor, const Arc& arc) :
315 Arc(arc), _adaptor(&adaptor) {}
317 InArcIt& operator++() {
318 _adaptor->nextIn(*this);
324 class EdgeIt : public Parent::Edge {
325 const Adaptor* _adaptor;
330 EdgeIt(Invalid i) : Edge(i) { }
332 explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
333 _adaptor->first(static_cast<Edge&>(*this));
336 EdgeIt(const Adaptor& adaptor, const Edge& e) :
337 Edge(e), _adaptor(&adaptor) { }
339 EdgeIt& operator++() {
340 _adaptor->next(*this);
346 class IncEdgeIt : public Edge {
347 friend class GraphAdaptorExtender;
348 const Adaptor* _adaptor;
354 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
356 IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
357 _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
360 IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
361 : _adaptor(&adaptor), Edge(e) {
362 direction = (_adaptor->u(e) == n);
365 IncEdgeIt& operator++() {
366 _adaptor->nextInc(*this, direction);
371 Node baseNode(const OutArcIt &a) const {
372 return Parent::source(a);
374 Node runningNode(const OutArcIt &a) const {
375 return Parent::target(a);
378 Node baseNode(const InArcIt &a) const {
379 return Parent::target(a);
381 Node runningNode(const InArcIt &a) const {
382 return Parent::source(a);
385 Node baseNode(const IncEdgeIt &e) const {
386 return e.direction ? Parent::u(e) : Parent::v(e);
388 Node runningNode(const IncEdgeIt &e) const {
389 return e.direction ? Parent::v(e) : Parent::u(e);