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_GRAPH_ADAPTOR_EXTENDER_H
20 #define LEMON_GRAPH_ADAPTOR_EXTENDER_H
25 template <typename Base>
26 class GraphAdaptorExtender : public Base {
30 typedef GraphAdaptorExtender Graph;
34 typedef typename Parent::Node Node;
35 typedef typename Parent::Edge Edge;
37 int maxId(Node) const {
38 return Parent::maxNodeId();
41 int maxId(Edge) const {
42 return Parent::maxEdgeId();
45 Node fromId(int id, Node) const {
46 return Parent::nodeFromId(id);
49 Edge fromId(int id, Edge) const {
50 return Parent::edgeFromId(id);
53 Node oppositeNode(const Node &n, const Edge &e) const {
54 if (n == Parent::source(e))
55 return Parent::target(e);
56 else if(n==Parent::target(e))
57 return Parent::source(e);
62 class NodeIt : public Node {
68 NodeIt(Invalid i) : Node(i) { }
70 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
71 _graph.first(*static_cast<Node*>(this));
74 NodeIt(const Graph& _graph, const Node& node)
75 : Node(node), graph(&_graph) {}
77 NodeIt& operator++() {
85 class EdgeIt : public Edge {
91 EdgeIt(Invalid i) : Edge(i) { }
93 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
94 _graph.first(*static_cast<Edge*>(this));
97 EdgeIt(const Graph& _graph, const Edge& e) :
98 Edge(e), graph(&_graph) { }
100 EdgeIt& operator++() {
108 class OutEdgeIt : public Edge {
114 OutEdgeIt(Invalid i) : Edge(i) { }
116 OutEdgeIt(const Graph& _graph, const Node& node)
118 _graph.firstOut(*this, node);
121 OutEdgeIt(const Graph& _graph, const Edge& edge)
122 : Edge(edge), graph(&_graph) {}
124 OutEdgeIt& operator++() {
125 graph->nextOut(*this);
132 class InEdgeIt : public Edge {
138 InEdgeIt(Invalid i) : Edge(i) { }
140 InEdgeIt(const Graph& _graph, const Node& node)
142 _graph.firstIn(*this, node);
145 InEdgeIt(const Graph& _graph, const Edge& edge) :
146 Edge(edge), graph(&_graph) {}
148 InEdgeIt& operator++() {
149 graph->nextIn(*this);
155 /// \brief Base node of the iterator
157 /// Returns the base node (ie. the source in this case) of the iterator
158 Node baseNode(const OutEdgeIt &e) const {
159 return Parent::source(e);
161 /// \brief Running node of the iterator
163 /// Returns the running node (ie. the target in this case) of the
165 Node runningNode(const OutEdgeIt &e) const {
166 return Parent::target(e);
169 /// \brief Base node of the iterator
171 /// Returns the base node (ie. the target in this case) of the iterator
172 Node baseNode(const InEdgeIt &e) const {
173 return Parent::target(e);
175 /// \brief Running node of the iterator
177 /// Returns the running node (ie. the source in this case) of the
179 Node runningNode(const InEdgeIt &e) const {
180 return Parent::source(e);
186 template <typename Base>
187 class UGraphAdaptorExtender : public Base {
191 typedef UGraphAdaptorExtender Graph;
193 typedef typename Parent::Node Node;
194 typedef typename Parent::Edge Edge;
195 typedef typename Parent::UEdge UEdge;
199 int maxId(Node) const {
200 return Parent::maxNodeId();
203 int maxId(Edge) const {
204 return Parent::maxEdgeId();
207 int maxId(UEdge) const {
208 return Parent::maxUEdgeId();
211 Node fromId(int id, Node) const {
212 return Parent::nodeFromId(id);
215 Edge fromId(int id, Edge) const {
216 return Parent::edgeFromId(id);
219 UEdge fromId(int id, UEdge) const {
220 return Parent::uEdgeFromId(id);
223 Node oppositeNode(const Node &n, const UEdge &e) const {
224 if( n == Parent::source(e))
225 return Parent::target(e);
226 else if( n == Parent::target(e))
227 return Parent::source(e);
232 Edge oppositeEdge(const Edge &e) const {
233 return Parent::direct(e, !Parent::direction(e));
236 using Parent::direct;
237 Edge direct(const UEdge &ue, const Node &s) const {
238 return Parent::direct(ue, Parent::source(ue) == s);
242 class NodeIt : public Node {
248 NodeIt(Invalid i) : Node(i) { }
250 explicit NodeIt(const Graph& _graph) : graph(&_graph) {
251 _graph.first(*static_cast<Node*>(this));
254 NodeIt(const Graph& _graph, const Node& node)
255 : Node(node), graph(&_graph) {}
257 NodeIt& operator++() {
265 class EdgeIt : public Edge {
271 EdgeIt(Invalid i) : Edge(i) { }
273 explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
274 _graph.first(*static_cast<Edge*>(this));
277 EdgeIt(const Graph& _graph, const Edge& e) :
278 Edge(e), graph(&_graph) { }
280 EdgeIt& operator++() {
288 class OutEdgeIt : public Edge {
294 OutEdgeIt(Invalid i) : Edge(i) { }
296 OutEdgeIt(const Graph& _graph, const Node& node)
298 _graph.firstOut(*this, node);
301 OutEdgeIt(const Graph& _graph, const Edge& edge)
302 : Edge(edge), graph(&_graph) {}
304 OutEdgeIt& operator++() {
305 graph->nextOut(*this);
312 class InEdgeIt : public Edge {
318 InEdgeIt(Invalid i) : Edge(i) { }
320 InEdgeIt(const Graph& _graph, const Node& node)
322 _graph.firstIn(*this, node);
325 InEdgeIt(const Graph& _graph, const Edge& edge) :
326 Edge(edge), graph(&_graph) {}
328 InEdgeIt& operator++() {
329 graph->nextIn(*this);
335 class UEdgeIt : public Parent::UEdge {
341 UEdgeIt(Invalid i) : UEdge(i) { }
343 explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
344 _graph.first(*static_cast<UEdge*>(this));
347 UEdgeIt(const Graph& _graph, const UEdge& e) :
348 UEdge(e), graph(&_graph) { }
350 UEdgeIt& operator++() {
357 class IncEdgeIt : public Parent::UEdge {
358 friend class UGraphAdaptorExtender;
365 IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
367 IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
368 _graph.firstInc(static_cast<UEdge&>(*this), direction, n);
371 IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
372 : graph(&_graph), UEdge(ue) {
373 direction = (_graph.source(ue) == n);
376 IncEdgeIt& operator++() {
377 graph->nextInc(*this, direction);
382 /// \brief Base node of the iterator
384 /// Returns the base node (ie. the source in this case) of the iterator
385 Node baseNode(const OutEdgeIt &e) const {
386 return Parent::source((Edge)e);
388 /// \brief Running node of the iterator
390 /// Returns the running node (ie. the target in this case) of the
392 Node runningNode(const OutEdgeIt &e) const {
393 return Parent::target((Edge)e);
396 /// \brief Base node of the iterator
398 /// Returns the base node (ie. the target in this case) of the iterator
399 Node baseNode(const InEdgeIt &e) const {
400 return Parent::target((Edge)e);
402 /// \brief Running node of the iterator
404 /// Returns the running node (ie. the source in this case) of the
406 Node runningNode(const InEdgeIt &e) const {
407 return Parent::source((Edge)e);
410 /// Base node of the iterator
412 /// Returns the base node of the iterator
413 Node baseNode(const IncEdgeIt &e) const {
414 return e.direction ? source(e) : target(e);
416 /// Running node of the iterator
418 /// Returns the running node of the iterator
419 Node runningNode(const IncEdgeIt &e) const {
420 return e.direction ? target(e) : source(e);