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>
28 ///\ingroup digraphbits
30 ///\brief Extenders for the digraph adaptor types
33 /// \ingroup digraphbits
35 /// \brief Extender for the DigraphAdaptors
36 template <typename _Digraph>
37 class DigraphAdaptorExtender : public _Digraph {
40 typedef _Digraph Parent;
41 typedef _Digraph Digraph;
42 typedef DigraphAdaptorExtender Adaptor;
46 typedef typename Parent::Node Node;
47 typedef typename Parent::Arc Arc;
49 int maxId(Node) const {
50 return Parent::maxNodeId();
53 int maxId(Arc) const {
54 return Parent::maxArcId();
57 Node fromId(int id, Node) const {
58 return Parent::nodeFromId(id);
61 Arc fromId(int id, Arc) const {
62 return Parent::arcFromId(id);
65 Node oppositeNode(const Node &n, const Arc &e) const {
66 if (n == Parent::source(e))
67 return Parent::target(e);
68 else if(n==Parent::target(e))
69 return Parent::source(e);
74 class NodeIt : public Node {
75 const Adaptor* _adaptor;
80 NodeIt(Invalid i) : Node(i) { }
82 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
83 _adaptor->first(static_cast<Node&>(*this));
86 NodeIt(const Adaptor& adaptor, const Node& node)
87 : Node(node), _adaptor(&adaptor) {}
89 NodeIt& operator++() {
90 _adaptor->next(*this);
97 class ArcIt : public Arc {
98 const Adaptor* _adaptor;
103 ArcIt(Invalid i) : Arc(i) { }
105 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
106 _adaptor->first(static_cast<Arc&>(*this));
109 ArcIt(const Adaptor& adaptor, const Arc& e) :
110 Arc(e), _adaptor(&adaptor) { }
112 ArcIt& operator++() {
113 _adaptor->next(*this);
120 class OutArcIt : public Arc {
121 const Adaptor* _adaptor;
126 OutArcIt(Invalid i) : Arc(i) { }
128 OutArcIt(const Adaptor& adaptor, const Node& node)
129 : _adaptor(&adaptor) {
130 _adaptor->firstOut(*this, node);
133 OutArcIt(const Adaptor& adaptor, const Arc& arc)
134 : Arc(arc), _adaptor(&adaptor) {}
136 OutArcIt& operator++() {
137 _adaptor->nextOut(*this);
144 class InArcIt : public Arc {
145 const Adaptor* _adaptor;
150 InArcIt(Invalid i) : Arc(i) { }
152 InArcIt(const Adaptor& adaptor, const Node& node)
153 : _adaptor(&adaptor) {
154 _adaptor->firstIn(*this, node);
157 InArcIt(const Adaptor& adaptor, const Arc& arc) :
158 Arc(arc), _adaptor(&adaptor) {}
160 InArcIt& operator++() {
161 _adaptor->nextIn(*this);
167 /// \brief Base node of the iterator
169 /// Returns the base node (ie. the source in this case) of the iterator
170 Node baseNode(const OutArcIt &e) const {
171 return Parent::source(e);
173 /// \brief Running node of the iterator
175 /// Returns the running node (ie. the target in this case) of the
177 Node runningNode(const OutArcIt &e) const {
178 return Parent::target(e);
181 /// \brief Base node of the iterator
183 /// Returns the base node (ie. the target in this case) of the iterator
184 Node baseNode(const InArcIt &e) const {
185 return Parent::target(e);
187 /// \brief Running node of the iterator
189 /// Returns the running node (ie. the source in this case) of the
191 Node runningNode(const InArcIt &e) const {
192 return Parent::source(e);
198 /// \ingroup digraphbits
200 /// \brief Extender for the GraphAdaptors
201 template <typename _Graph>
202 class GraphAdaptorExtender : public _Graph {
205 typedef _Graph Parent;
206 typedef _Graph Graph;
207 typedef GraphAdaptorExtender Adaptor;
209 typedef typename Parent::Node Node;
210 typedef typename Parent::Arc Arc;
211 typedef typename Parent::Edge Edge;
215 int maxId(Node) const {
216 return Parent::maxNodeId();
219 int maxId(Arc) const {
220 return Parent::maxArcId();
223 int maxId(Edge) const {
224 return Parent::maxEdgeId();
227 Node fromId(int id, Node) const {
228 return Parent::nodeFromId(id);
231 Arc fromId(int id, Arc) const {
232 return Parent::arcFromId(id);
235 Edge fromId(int id, Edge) const {
236 return Parent::edgeFromId(id);
239 Node oppositeNode(const Node &n, const Edge &e) const {
240 if( n == Parent::u(e))
242 else if( n == Parent::v(e))
248 Arc oppositeArc(const Arc &a) const {
249 return Parent::direct(a, !Parent::direction(a));
252 using Parent::direct;
253 Arc direct(const Edge &e, const Node &s) const {
254 return Parent::direct(e, Parent::u(e) == s);
258 class NodeIt : public Node {
259 const Adaptor* _adaptor;
264 NodeIt(Invalid i) : Node(i) { }
266 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
267 _adaptor->first(static_cast<Node&>(*this));
270 NodeIt(const Adaptor& adaptor, const Node& node)
271 : Node(node), _adaptor(&adaptor) {}
273 NodeIt& operator++() {
274 _adaptor->next(*this);
281 class ArcIt : public Arc {
282 const Adaptor* _adaptor;
287 ArcIt(Invalid i) : Arc(i) { }
289 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
290 _adaptor->first(static_cast<Arc&>(*this));
293 ArcIt(const Adaptor& adaptor, const Arc& e) :
294 Arc(e), _adaptor(&adaptor) { }
296 ArcIt& operator++() {
297 _adaptor->next(*this);
304 class OutArcIt : public Arc {
305 const Adaptor* _adaptor;
310 OutArcIt(Invalid i) : Arc(i) { }
312 OutArcIt(const Adaptor& adaptor, const Node& node)
313 : _adaptor(&adaptor) {
314 _adaptor->firstOut(*this, node);
317 OutArcIt(const Adaptor& adaptor, const Arc& arc)
318 : Arc(arc), _adaptor(&adaptor) {}
320 OutArcIt& operator++() {
321 _adaptor->nextOut(*this);
328 class InArcIt : public Arc {
329 const Adaptor* _adaptor;
334 InArcIt(Invalid i) : Arc(i) { }
336 InArcIt(const Adaptor& adaptor, const Node& node)
337 : _adaptor(&adaptor) {
338 _adaptor->firstIn(*this, node);
341 InArcIt(const Adaptor& adaptor, const Arc& arc) :
342 Arc(arc), _adaptor(&adaptor) {}
344 InArcIt& operator++() {
345 _adaptor->nextIn(*this);
351 class EdgeIt : public Parent::Edge {
352 const Adaptor* _adaptor;
357 EdgeIt(Invalid i) : Edge(i) { }
359 explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
360 _adaptor->first(static_cast<Edge&>(*this));
363 EdgeIt(const Adaptor& adaptor, const Edge& e) :
364 Edge(e), _adaptor(&adaptor) { }
366 EdgeIt& operator++() {
367 _adaptor->next(*this);
373 class IncEdgeIt : public Edge {
374 friend class GraphAdaptorExtender;
375 const Adaptor* _adaptor;
381 IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
383 IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
384 _adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
387 IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
388 : _adaptor(&adaptor), Edge(e) {
389 direction = (_adaptor->u(e) == n);
392 IncEdgeIt& operator++() {
393 _adaptor->nextInc(*this, direction);
398 /// \brief Base node of the iterator
400 /// Returns the base node (ie. the source in this case) of the iterator
401 Node baseNode(const OutArcIt &a) const {
402 return Parent::source(a);
404 /// \brief Running node of the iterator
406 /// Returns the running node (ie. the target in this case) of the
408 Node runningNode(const OutArcIt &a) const {
409 return Parent::target(a);
412 /// \brief Base node of the iterator
414 /// Returns the base node (ie. the target in this case) of the iterator
415 Node baseNode(const InArcIt &a) const {
416 return Parent::target(a);
418 /// \brief Running node of the iterator
420 /// Returns the running node (ie. the source in this case) of the
422 Node runningNode(const InArcIt &a) const {
423 return Parent::source(a);
426 /// Base node of the iterator
428 /// Returns the base node of the iterator
429 Node baseNode(const IncEdgeIt &e) const {
430 return e.direction ? Parent::u(e) : Parent::v(e);
432 /// Running node of the iterator
434 /// Returns the running node of the iterator
435 Node runningNode(const IncEdgeIt &e) const {
436 return e.direction ? Parent::v(e) : Parent::u(e);