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_GRAPH_ADAPTOR_EXTENDER_H
20 #define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
22 #include <lemon/core.h>
23 #include <lemon/error.h>
25 #include <lemon/bits/default_map.h>
29 template <typename _Digraph>
30 class DigraphAdaptorExtender : public _Digraph {
33 typedef _Digraph Parent;
34 typedef _Digraph Digraph;
35 typedef DigraphAdaptorExtender Adaptor;
39 typedef typename Parent::Node Node;
40 typedef typename Parent::Arc Arc;
42 int maxId(Node) const {
43 return Parent::maxNodeId();
46 int maxId(Arc) const {
47 return Parent::maxArcId();
50 Node fromId(int id, Node) const {
51 return Parent::nodeFromId(id);
54 Arc fromId(int id, Arc) const {
55 return Parent::arcFromId(id);
58 Node oppositeNode(const Node &n, const Arc &e) const {
59 if (n == Parent::source(e))
60 return Parent::target(e);
61 else if(n==Parent::target(e))
62 return Parent::source(e);
67 class NodeIt : public Node {
68 const Adaptor* _adaptor;
73 NodeIt(Invalid i) : Node(i) { }
75 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
76 _adaptor->first(static_cast<Node&>(*this));
79 NodeIt(const Adaptor& adaptor, const Node& node)
80 : Node(node), _adaptor(&adaptor) {}
82 NodeIt& operator++() {
83 _adaptor->next(*this);
90 class ArcIt : public Arc {
91 const Adaptor* _adaptor;
96 ArcIt(Invalid i) : Arc(i) { }
98 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
99 _adaptor->first(static_cast<Arc&>(*this));
102 ArcIt(const Adaptor& adaptor, const Arc& e) :
103 Arc(e), _adaptor(&adaptor) { }
105 ArcIt& operator++() {
106 _adaptor->next(*this);
113 class OutArcIt : public Arc {
114 const Adaptor* _adaptor;
119 OutArcIt(Invalid i) : Arc(i) { }
121 OutArcIt(const Adaptor& adaptor, const Node& node)
122 : _adaptor(&adaptor) {
123 _adaptor->firstOut(*this, node);
126 OutArcIt(const Adaptor& adaptor, const Arc& arc)
127 : Arc(arc), _adaptor(&adaptor) {}
129 OutArcIt& operator++() {
130 _adaptor->nextOut(*this);
137 class InArcIt : public Arc {
138 const Adaptor* _adaptor;
143 InArcIt(Invalid i) : Arc(i) { }
145 InArcIt(const Adaptor& adaptor, const Node& node)
146 : _adaptor(&adaptor) {
147 _adaptor->firstIn(*this, node);
150 InArcIt(const Adaptor& adaptor, const Arc& arc) :
151 Arc(arc), _adaptor(&adaptor) {}
153 InArcIt& operator++() {
154 _adaptor->nextIn(*this);
160 Node baseNode(const OutArcIt &e) const {
161 return Parent::source(e);
163 Node runningNode(const OutArcIt &e) const {
164 return Parent::target(e);
167 Node baseNode(const InArcIt &e) const {
168 return Parent::target(e);
170 Node runningNode(const InArcIt &e) const {
171 return Parent::source(e);
177 /// \ingroup digraphbits
179 /// \brief Extender for the GraphAdaptors
180 template <typename _Graph>
181 class GraphAdaptorExtender : public _Graph {
184 typedef _Graph Parent;
185 typedef _Graph Graph;
186 typedef GraphAdaptorExtender Adaptor;
188 typedef typename Parent::Node Node;
189 typedef typename Parent::Arc Arc;
190 typedef typename Parent::Edge Edge;
194 int maxId(Node) const {
195 return Parent::maxNodeId();
198 int maxId(Arc) const {
199 return Parent::maxArcId();
202 int maxId(Edge) const {
203 return Parent::maxEdgeId();
206 Node fromId(int id, Node) const {
207 return Parent::nodeFromId(id);
210 Arc fromId(int id, Arc) const {
211 return Parent::arcFromId(id);
214 Edge fromId(int id, Edge) const {
215 return Parent::edgeFromId(id);
218 Node oppositeNode(const Node &n, const Edge &e) const {
219 if( n == Parent::u(e))
221 else if( n == Parent::v(e))
227 Arc oppositeArc(const Arc &a) const {
228 return Parent::direct(a, !Parent::direction(a));
231 using Parent::direct;
232 Arc direct(const Edge &e, const Node &s) const {
233 return Parent::direct(e, Parent::u(e) == s);
237 class NodeIt : public Node {
238 const Adaptor* _adaptor;
243 NodeIt(Invalid i) : Node(i) { }
245 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
246 _adaptor->first(static_cast<Node&>(*this));
249 NodeIt(const Adaptor& adaptor, const Node& node)
250 : Node(node), _adaptor(&adaptor) {}
252 NodeIt& operator++() {
253 _adaptor->next(*this);
260 class ArcIt : public Arc {
261 const Adaptor* _adaptor;
266 ArcIt(Invalid i) : Arc(i) { }
268 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
269 _adaptor->first(static_cast<Arc&>(*this));
272 ArcIt(const Adaptor& adaptor, const Arc& e) :
273 Arc(e), _adaptor(&adaptor) { }
275 ArcIt& operator++() {
276 _adaptor->next(*this);
283 class OutArcIt : public Arc {
284 const Adaptor* _adaptor;
289 OutArcIt(Invalid i) : Arc(i) { }
291 OutArcIt(const Adaptor& adaptor, const Node& node)
292 : _adaptor(&adaptor) {
293 _adaptor->firstOut(*this, node);
296 OutArcIt(const Adaptor& adaptor, const Arc& arc)
297 : Arc(arc), _adaptor(&adaptor) {}
299 OutArcIt& operator++() {
300 _adaptor->nextOut(*this);
307 class InArcIt : public Arc {
308 const Adaptor* _adaptor;
313 InArcIt(Invalid i) : Arc(i) { }
315 InArcIt(const Adaptor& adaptor, const Node& node)
316 : _adaptor(&adaptor) {
317 _adaptor->firstIn(*this, node);
320 InArcIt(const Adaptor& adaptor, const Arc& arc) :
321 Arc(arc), _adaptor(&adaptor) {}
323 InArcIt& operator++() {
324 _adaptor->nextIn(*this);
330 class EdgeIt : public Parent::Edge {
331 const Adaptor* _adaptor;
336 EdgeIt(Invalid i) : Edge(i) { }
338 explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
339 _adaptor->first(static_cast<Edge&>(*this));
342 EdgeIt(const Adaptor& adaptor, const Edge& e) :
343 Edge(e), _adaptor(&adaptor) { }
345 EdgeIt& operator++() {
346 _adaptor->next(*this);
352 class IncEdgeIt : public Edge {
353 friend class GraphAdaptorExtender;
354 const Adaptor* _adaptor;
360 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
362 IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
363 _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
366 IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
367 : _adaptor(&adaptor), Edge(e) {
368 direction = (_adaptor->u(e) == n);
371 IncEdgeIt& operator++() {
372 _adaptor->nextInc(*this, direction);
377 Node baseNode(const OutArcIt &a) const {
378 return Parent::source(a);
380 Node runningNode(const OutArcIt &a) const {
381 return Parent::target(a);
384 Node baseNode(const InArcIt &a) const {
385 return Parent::target(a);
387 Node runningNode(const InArcIt &a) const {
388 return Parent::source(a);
391 Node baseNode(const IncEdgeIt &e) const {
392 return e.direction ? Parent::u(e) : Parent::v(e);
394 Node runningNode(const IncEdgeIt &e) const {
395 return e.direction ? Parent::v(e) : Parent::u(e);