klao@946
|
1 |
// -*- c++ -*-
|
klao@946
|
2 |
#ifndef LEMON_ITERABLE_GRAPH_EXTENDER_H
|
klao@946
|
3 |
#define LEMON_ITERABLE_GRAPH_EXTENDER_H
|
klao@946
|
4 |
|
klao@946
|
5 |
#include <lemon/invalid.h>
|
alpar@1448
|
6 |
#include <lemon/utility.h>
|
klao@946
|
7 |
|
klao@946
|
8 |
namespace lemon {
|
klao@946
|
9 |
|
klao@946
|
10 |
template <typename _Base>
|
klao@946
|
11 |
class IterableGraphExtender : public _Base {
|
klao@962
|
12 |
public:
|
klao@946
|
13 |
|
alpar@1448
|
14 |
/// Indicates that the graph is undirected.
|
alpar@1448
|
15 |
|
alpar@1448
|
16 |
///\todo Better name?
|
alpar@1448
|
17 |
///
|
alpar@1448
|
18 |
///\bug Should it be here?
|
alpar@1448
|
19 |
typedef False UndirTag;
|
alpar@1448
|
20 |
|
klao@946
|
21 |
typedef _Base Parent;
|
klao@946
|
22 |
typedef IterableGraphExtender<_Base> Graph;
|
klao@946
|
23 |
|
klao@946
|
24 |
typedef typename Parent::Node Node;
|
klao@946
|
25 |
typedef typename Parent::Edge Edge;
|
klao@946
|
26 |
|
klao@946
|
27 |
|
klao@946
|
28 |
class NodeIt : public Node {
|
klao@946
|
29 |
const Graph* graph;
|
klao@946
|
30 |
public:
|
klao@946
|
31 |
|
klao@946
|
32 |
NodeIt() {}
|
klao@946
|
33 |
|
klao@946
|
34 |
NodeIt(Invalid i) : Node(i) { }
|
klao@946
|
35 |
|
klao@962
|
36 |
explicit NodeIt(const Graph& _graph) : graph(&_graph) {
|
klao@946
|
37 |
_graph.first(*static_cast<Node*>(this));
|
klao@946
|
38 |
}
|
klao@946
|
39 |
|
klao@946
|
40 |
NodeIt(const Graph& _graph, const Node& node)
|
klao@946
|
41 |
: Node(node), graph(&_graph) {}
|
klao@946
|
42 |
|
klao@946
|
43 |
NodeIt& operator++() {
|
klao@946
|
44 |
graph->next(*this);
|
klao@946
|
45 |
return *this;
|
klao@946
|
46 |
}
|
klao@946
|
47 |
|
klao@946
|
48 |
};
|
klao@946
|
49 |
|
klao@946
|
50 |
|
klao@946
|
51 |
class EdgeIt : public Edge {
|
klao@946
|
52 |
const Graph* graph;
|
klao@946
|
53 |
public:
|
klao@946
|
54 |
|
klao@946
|
55 |
EdgeIt() { }
|
klao@946
|
56 |
|
klao@946
|
57 |
EdgeIt(Invalid i) : Edge(i) { }
|
klao@946
|
58 |
|
klao@962
|
59 |
explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
|
klao@946
|
60 |
_graph.first(*static_cast<Edge*>(this));
|
klao@946
|
61 |
}
|
klao@946
|
62 |
|
klao@946
|
63 |
EdgeIt(const Graph& _graph, const Edge& e) :
|
klao@946
|
64 |
Edge(e), graph(&_graph) { }
|
klao@946
|
65 |
|
klao@946
|
66 |
EdgeIt& operator++() {
|
klao@946
|
67 |
graph->next(*this);
|
klao@946
|
68 |
return *this;
|
klao@946
|
69 |
}
|
klao@946
|
70 |
|
klao@946
|
71 |
};
|
klao@946
|
72 |
|
klao@946
|
73 |
|
klao@946
|
74 |
class OutEdgeIt : public Edge {
|
klao@946
|
75 |
const Graph* graph;
|
klao@946
|
76 |
public:
|
klao@946
|
77 |
|
klao@946
|
78 |
OutEdgeIt() { }
|
klao@946
|
79 |
|
klao@946
|
80 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
klao@946
|
81 |
|
klao@946
|
82 |
OutEdgeIt(const Graph& _graph, const Node& node)
|
klao@962
|
83 |
: graph(&_graph) {
|
klao@946
|
84 |
_graph.firstOut(*this, node);
|
klao@946
|
85 |
}
|
klao@946
|
86 |
|
klao@946
|
87 |
OutEdgeIt(const Graph& _graph, const Edge& edge)
|
klao@946
|
88 |
: Edge(edge), graph(&_graph) {}
|
klao@946
|
89 |
|
klao@946
|
90 |
OutEdgeIt& operator++() {
|
klao@946
|
91 |
graph->nextOut(*this);
|
klao@946
|
92 |
return *this;
|
klao@946
|
93 |
}
|
klao@946
|
94 |
|
klao@946
|
95 |
};
|
klao@946
|
96 |
|
klao@946
|
97 |
|
klao@946
|
98 |
class InEdgeIt : public Edge {
|
klao@946
|
99 |
const Graph* graph;
|
klao@946
|
100 |
public:
|
klao@946
|
101 |
|
klao@946
|
102 |
InEdgeIt() { }
|
klao@946
|
103 |
|
klao@946
|
104 |
InEdgeIt(Invalid i) : Edge(i) { }
|
klao@946
|
105 |
|
klao@946
|
106 |
InEdgeIt(const Graph& _graph, const Node& node)
|
klao@962
|
107 |
: graph(&_graph) {
|
klao@946
|
108 |
_graph.firstIn(*this, node);
|
klao@946
|
109 |
}
|
klao@946
|
110 |
|
klao@946
|
111 |
InEdgeIt(const Graph& _graph, const Edge& edge) :
|
klao@946
|
112 |
Edge(edge), graph(&_graph) {}
|
klao@946
|
113 |
|
klao@946
|
114 |
InEdgeIt& operator++() {
|
klao@946
|
115 |
graph->nextIn(*this);
|
klao@946
|
116 |
return *this;
|
klao@946
|
117 |
}
|
klao@946
|
118 |
|
klao@946
|
119 |
};
|
klao@946
|
120 |
|
deba@1627
|
121 |
/// \brief Base node of the iterator
|
klao@1158
|
122 |
///
|
klao@1158
|
123 |
/// Returns the base node (ie. the source in this case) of the iterator
|
klao@1158
|
124 |
Node baseNode(const OutEdgeIt &e) const {
|
deba@1564
|
125 |
return Parent::source((Edge)e);
|
klao@1158
|
126 |
}
|
deba@1627
|
127 |
/// \brief Running node of the iterator
|
klao@1158
|
128 |
///
|
klao@1158
|
129 |
/// Returns the running node (ie. the target in this case) of the
|
klao@1158
|
130 |
/// iterator
|
klao@1158
|
131 |
Node runningNode(const OutEdgeIt &e) const {
|
deba@1564
|
132 |
return Parent::target((Edge)e);
|
klao@1158
|
133 |
}
|
klao@1158
|
134 |
|
deba@1627
|
135 |
/// \brief Base node of the iterator
|
klao@1158
|
136 |
///
|
klao@1158
|
137 |
/// Returns the base node (ie. the target in this case) of the iterator
|
klao@1158
|
138 |
Node baseNode(const InEdgeIt &e) const {
|
deba@1564
|
139 |
return Parent::target((Edge)e);
|
klao@1158
|
140 |
}
|
deba@1627
|
141 |
/// \brief Running node of the iterator
|
klao@1158
|
142 |
///
|
klao@1158
|
143 |
/// Returns the running node (ie. the source in this case) of the
|
klao@1158
|
144 |
/// iterator
|
klao@1158
|
145 |
Node runningNode(const InEdgeIt &e) const {
|
deba@1564
|
146 |
return Parent::source((Edge)e);
|
klao@1158
|
147 |
}
|
klao@1158
|
148 |
|
klao@946
|
149 |
using Parent::first;
|
klao@946
|
150 |
|
deba@1627
|
151 |
/// \brief The opposite node on the given edge.
|
deba@1627
|
152 |
///
|
deba@1627
|
153 |
/// Gives back the opposite on the given edge.
|
deba@1627
|
154 |
Node oppositeNode(const Node& n, const Edge& e) const {
|
deba@1627
|
155 |
if (Parent::source(e) == n) {
|
deba@1627
|
156 |
return Parent::target(e);
|
deba@1627
|
157 |
} else {
|
deba@1627
|
158 |
return Parent::source(e);
|
deba@1627
|
159 |
}
|
deba@1627
|
160 |
}
|
deba@1627
|
161 |
|
klao@946
|
162 |
private:
|
klao@946
|
163 |
|
klao@1230
|
164 |
// void first(NodeIt &) const;
|
klao@1230
|
165 |
// void first(EdgeIt &) const;
|
klao@1230
|
166 |
// void first(OutEdgeIt &) const;
|
klao@1230
|
167 |
// void first(InEdgeIt &) const;
|
klao@946
|
168 |
|
klao@946
|
169 |
};
|
klao@1158
|
170 |
|
klao@1158
|
171 |
|
klao@1158
|
172 |
|
klao@1158
|
173 |
|
klao@1158
|
174 |
|
klao@946
|
175 |
|
klao@962
|
176 |
template <typename _Base>
|
klao@962
|
177 |
class IterableUndirGraphExtender : public IterableGraphExtender<_Base> {
|
klao@962
|
178 |
public:
|
klao@962
|
179 |
|
alpar@1448
|
180 |
/// Indicates that the graph is undirected.
|
alpar@1448
|
181 |
|
alpar@1448
|
182 |
///\todo Better name?
|
alpar@1448
|
183 |
///
|
alpar@1448
|
184 |
///\bug Should it be here?
|
alpar@1448
|
185 |
///\bug Should be tested in the concept checker whether it is defined
|
alpar@1448
|
186 |
///correctly.
|
alpar@1448
|
187 |
typedef True UndirTag;
|
alpar@1448
|
188 |
|
klao@962
|
189 |
typedef IterableGraphExtender<_Base> Parent;
|
klao@962
|
190 |
typedef IterableUndirGraphExtender<_Base> Graph;
|
klao@1021
|
191 |
typedef typename Parent::Node Node;
|
deba@1627
|
192 |
typedef typename Parent::Edge Edge;
|
klao@962
|
193 |
typedef typename Parent::UndirEdge UndirEdge;
|
klao@962
|
194 |
|
klao@1230
|
195 |
class UndirEdgeIt : public Parent::UndirEdge {
|
klao@962
|
196 |
const Graph* graph;
|
klao@962
|
197 |
public:
|
klao@962
|
198 |
|
klao@962
|
199 |
UndirEdgeIt() { }
|
klao@962
|
200 |
|
klao@962
|
201 |
UndirEdgeIt(Invalid i) : UndirEdge(i) { }
|
klao@962
|
202 |
|
klao@962
|
203 |
explicit UndirEdgeIt(const Graph& _graph) : graph(&_graph) {
|
klao@962
|
204 |
_graph.first(*static_cast<UndirEdge*>(this));
|
klao@962
|
205 |
}
|
klao@962
|
206 |
|
klao@962
|
207 |
UndirEdgeIt(const Graph& _graph, const UndirEdge& e) :
|
klao@962
|
208 |
UndirEdge(e), graph(&_graph) { }
|
klao@962
|
209 |
|
klao@962
|
210 |
UndirEdgeIt& operator++() {
|
klao@962
|
211 |
graph->next(*this);
|
klao@962
|
212 |
return *this;
|
klao@962
|
213 |
}
|
klao@962
|
214 |
|
klao@962
|
215 |
};
|
klao@962
|
216 |
|
klao@1230
|
217 |
class IncEdgeIt : public Parent::UndirEdge {
|
klao@1021
|
218 |
const Graph* graph;
|
klao@1021
|
219 |
bool forward;
|
klao@1021
|
220 |
friend class IterableUndirGraphExtender;
|
klao@1021
|
221 |
template <typename G>
|
klao@1021
|
222 |
friend class UndirGraphExtender;
|
klao@1021
|
223 |
public:
|
klao@1021
|
224 |
|
klao@1030
|
225 |
IncEdgeIt() { }
|
klao@1021
|
226 |
|
klao@1030
|
227 |
IncEdgeIt(Invalid i) : UndirEdge(i), forward(false) { }
|
klao@1021
|
228 |
|
klao@1158
|
229 |
IncEdgeIt(const Graph& _graph, const Node &n)
|
klao@1021
|
230 |
: graph(&_graph)
|
klao@1021
|
231 |
{
|
klao@1021
|
232 |
_graph._dirFirstOut(*this, n);
|
klao@1021
|
233 |
}
|
klao@1021
|
234 |
|
klao@1158
|
235 |
IncEdgeIt(const Graph& _graph, const UndirEdge &ue, const Node &n)
|
klao@1158
|
236 |
: graph(&_graph), UndirEdge(ue)
|
klao@1158
|
237 |
{
|
klao@1158
|
238 |
forward = (_graph.source(ue) == n);
|
klao@1158
|
239 |
}
|
klao@1021
|
240 |
|
klao@1030
|
241 |
IncEdgeIt& operator++() {
|
klao@1021
|
242 |
graph->_dirNextOut(*this);
|
klao@1021
|
243 |
return *this;
|
klao@1021
|
244 |
}
|
klao@1021
|
245 |
};
|
klao@1021
|
246 |
|
klao@1158
|
247 |
using Parent::baseNode;
|
klao@1158
|
248 |
using Parent::runningNode;
|
klao@1158
|
249 |
|
klao@1158
|
250 |
/// Base node of the iterator
|
klao@1158
|
251 |
///
|
klao@1158
|
252 |
/// Returns the base node of the iterator
|
klao@1158
|
253 |
Node baseNode(const IncEdgeIt &e) const {
|
klao@1021
|
254 |
return _dirSource(e);
|
klao@1021
|
255 |
}
|
klao@1158
|
256 |
/// Running node of the iterator
|
klao@1158
|
257 |
///
|
klao@1158
|
258 |
/// Returns the running node of the iterator
|
klao@1158
|
259 |
Node runningNode(const IncEdgeIt &e) const {
|
klao@1021
|
260 |
return _dirTarget(e);
|
klao@1021
|
261 |
}
|
klao@1021
|
262 |
|
deba@1627
|
263 |
/// \brief The opposite node on the given undirected edge.
|
deba@1627
|
264 |
///
|
deba@1627
|
265 |
/// Gives back the opposite on the given undirected edge.
|
deba@1627
|
266 |
Node oppositeNode(const Node& n, const UndirEdge& e) const {
|
deba@1627
|
267 |
if (Parent::source(e) == n) {
|
deba@1627
|
268 |
return Parent::target(e);
|
deba@1627
|
269 |
} else {
|
deba@1627
|
270 |
return Parent::source(e);
|
deba@1627
|
271 |
}
|
deba@1627
|
272 |
}
|
deba@1627
|
273 |
|
klao@962
|
274 |
};
|
klao@946
|
275 |
}
|
klao@946
|
276 |
|
klao@946
|
277 |
#endif // LEMON_GRAPH_EXTENDER_H
|