klao@225
|
1 |
// -*- c++ -*- //
|
klao@225
|
2 |
|
alpar@434
|
3 |
///ingroup datas
|
alpar@434
|
4 |
///\file
|
alpar@434
|
5 |
///\brief Class for representing paths in graphs.
|
klao@225
|
6 |
|
klao@225
|
7 |
#ifndef HUGO_PATH_H
|
klao@225
|
8 |
#define HUGO_PATH_H
|
klao@225
|
9 |
|
klao@225
|
10 |
#include <deque>
|
klao@369
|
11 |
#include <vector>
|
klao@226
|
12 |
#include <algorithm>
|
klao@225
|
13 |
|
klao@225
|
14 |
#include <invalid.h>
|
klao@225
|
15 |
|
klao@225
|
16 |
namespace hugo {
|
klao@225
|
17 |
|
alpar@434
|
18 |
/// \addtogroup datas
|
alpar@434
|
19 |
/// @{
|
alpar@434
|
20 |
|
alpar@434
|
21 |
///A container for directed paths
|
alpar@434
|
22 |
|
alpar@434
|
23 |
///\param Graph The graph type in which the path is.
|
alpar@434
|
24 |
///
|
alpar@434
|
25 |
///In a sense, the path can be treated as a graph, for is has \c NodeIt
|
alpar@434
|
26 |
///and \c EdgeIt with the same usage. These types converts to the \c Node
|
alpar@434
|
27 |
///and \c Edge of the original graph.
|
alpar@434
|
28 |
///\todo How to clear a path?
|
alpar@434
|
29 |
///\todo Clarify the consistency checks to do.
|
klao@369
|
30 |
template<typename Graph>
|
klao@369
|
31 |
class DirPath {
|
klao@369
|
32 |
public:
|
klao@369
|
33 |
typedef typename Graph::Edge GraphEdge;
|
klao@369
|
34 |
typedef typename Graph::Node GraphNode;
|
klao@369
|
35 |
class NodeIt;
|
klao@369
|
36 |
class EdgeIt;
|
klao@369
|
37 |
|
klao@369
|
38 |
protected:
|
klao@369
|
39 |
const Graph *gr;
|
klao@369
|
40 |
typedef std::vector<GraphEdge> Container;
|
klao@369
|
41 |
Container edges;
|
klao@369
|
42 |
|
klao@369
|
43 |
public:
|
klao@369
|
44 |
|
alpar@434
|
45 |
/// Constructor
|
alpar@434
|
46 |
|
alpar@434
|
47 |
/// \param _G The graph in which the path is.
|
alpar@434
|
48 |
///
|
klao@369
|
49 |
DirPath(const Graph &_G) : gr(&_G) {}
|
klao@369
|
50 |
|
klao@369
|
51 |
/// Subpath defined by two nodes.
|
alpar@434
|
52 |
/// \warning It is an error if the two edges are not in order!
|
klao@369
|
53 |
DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b);
|
klao@369
|
54 |
/// Subpath defined by two edges. Contains edges in [a,b)
|
alpar@434
|
55 |
/// \warning It is an error if the two edges are not in order!
|
klao@369
|
56 |
DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b);
|
klao@369
|
57 |
|
klao@369
|
58 |
size_t length() const { return edges.size(); }
|
klao@369
|
59 |
bool empty() const { return edges.empty(); }
|
klao@369
|
60 |
GraphNode from() const {
|
klao@369
|
61 |
return empty() ? INVALID : gr->tail(edges[0]);
|
klao@369
|
62 |
}
|
klao@369
|
63 |
GraphNode to() const {
|
klao@369
|
64 |
return empty() ? INVALID : gr->head(edges[length()-1]);
|
klao@369
|
65 |
}
|
klao@369
|
66 |
|
klao@369
|
67 |
template<typename It>
|
klao@369
|
68 |
It& first(It &i) const { return i=It(*this); }
|
klao@369
|
69 |
|
klao@369
|
70 |
template<typename It>
|
klao@369
|
71 |
It& nth(It &i, int n) const { return i=It(*this, n); }
|
klao@369
|
72 |
|
klao@369
|
73 |
template<typename It>
|
klao@369
|
74 |
bool valid(const It &i) const { return i.valid(); }
|
klao@369
|
75 |
|
klao@369
|
76 |
template<typename It>
|
klao@369
|
77 |
It& next(It &e) const { return ++e; }
|
klao@369
|
78 |
|
klao@369
|
79 |
/// \todo !
|
klao@369
|
80 |
NodeIt head(const EdgeIt& e) const;
|
klao@369
|
81 |
NodeIt tail(const EdgeIt& e) const;
|
klao@369
|
82 |
|
klao@369
|
83 |
|
klao@369
|
84 |
/*** Iterator classes ***/
|
klao@369
|
85 |
class EdgeIt {
|
klao@369
|
86 |
friend class DirPath;
|
klao@369
|
87 |
|
klao@369
|
88 |
int idx;
|
klao@369
|
89 |
const DirPath *p;
|
klao@369
|
90 |
public:
|
klao@369
|
91 |
EdgeIt() {}
|
klao@369
|
92 |
EdgeIt(Invalid) : idx(-1), p(0) {}
|
klao@369
|
93 |
EdgeIt(const DirPath &_p, int _idx = 0) :
|
klao@369
|
94 |
idx(_idx), p(&_p) { validate(); }
|
klao@369
|
95 |
|
klao@369
|
96 |
bool valid() const { return idx!=-1; }
|
klao@369
|
97 |
|
klao@369
|
98 |
operator GraphEdge () const {
|
klao@369
|
99 |
return valid() ? p->edges[idx] : INVALID;
|
klao@369
|
100 |
}
|
klao@369
|
101 |
EdgeIt& operator++() { ++idx; validate(); return *this; }
|
klao@369
|
102 |
|
klao@369
|
103 |
bool operator==(const EdgeIt& e) const { return idx==e.idx; }
|
klao@369
|
104 |
bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
|
klao@369
|
105 |
bool operator<(const EdgeIt& e) const { return idx<e.idx; }
|
klao@369
|
106 |
|
klao@369
|
107 |
private:
|
klao@369
|
108 |
// FIXME: comparison between signed and unsigned...
|
klao@369
|
109 |
// Jo ez igy? Vagy esetleg legyen a length() int?
|
klao@369
|
110 |
void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
|
klao@369
|
111 |
};
|
klao@369
|
112 |
|
klao@369
|
113 |
class NodeIt {
|
klao@369
|
114 |
friend class DirPath;
|
klao@369
|
115 |
|
klao@369
|
116 |
int idx;
|
klao@369
|
117 |
const DirPath *p;
|
klao@369
|
118 |
public:
|
klao@369
|
119 |
NodeIt() {}
|
klao@369
|
120 |
NodeIt(Invalid) : idx(-1), p(0) {}
|
klao@369
|
121 |
NodeIt(const DirPath &_p, int _idx = 0) :
|
klao@369
|
122 |
idx(_idx), p(&_p) { validate(); }
|
klao@369
|
123 |
|
klao@369
|
124 |
bool valid() const { return idx!=-1; }
|
klao@369
|
125 |
|
klao@369
|
126 |
operator const GraphEdge& () const {
|
klao@369
|
127 |
if(idx >= p->length())
|
klao@369
|
128 |
return p->to();
|
klao@369
|
129 |
else if(idx >= 0)
|
klao@369
|
130 |
return p->gr->tail(p->edges[idx]);
|
klao@369
|
131 |
else
|
klao@369
|
132 |
return INVALID;
|
klao@369
|
133 |
}
|
klao@369
|
134 |
NodeIt& operator++() { ++idx; validate(); return *this; }
|
klao@369
|
135 |
|
klao@369
|
136 |
bool operator==(const NodeIt& e) const { return idx==e.idx; }
|
klao@369
|
137 |
bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
|
klao@369
|
138 |
bool operator<(const NodeIt& e) const { return idx<e.idx; }
|
klao@369
|
139 |
|
klao@369
|
140 |
private:
|
klao@369
|
141 |
void validate() { if( size_t(idx) > p->length() ) idx=-1; }
|
klao@369
|
142 |
};
|
klao@369
|
143 |
|
klao@369
|
144 |
friend class Builder;
|
alpar@434
|
145 |
|
alpar@434
|
146 |
///Class to build paths
|
alpar@434
|
147 |
|
alpar@434
|
148 |
///\ingroup datas
|
alpar@434
|
149 |
///This class is used to build new paths.
|
alpar@434
|
150 |
///You can push new edges to the front and to the back of the path in
|
alpar@434
|
151 |
///arbitrary order the you can commit these changes to the graph.
|
alpar@434
|
152 |
///\todo We must clarify when the path will be in "transitional" state.
|
klao@369
|
153 |
class Builder {
|
klao@369
|
154 |
DirPath &P;
|
klao@369
|
155 |
Container d;
|
klao@369
|
156 |
|
klao@369
|
157 |
public:
|
alpar@434
|
158 |
///Constructor
|
alpar@434
|
159 |
|
alpar@434
|
160 |
///\param _P the path you want to build.
|
alpar@434
|
161 |
///
|
klao@369
|
162 |
Builder(DirPath &_P) : P(_P) {}
|
klao@369
|
163 |
|
alpar@434
|
164 |
///Set the first node of the path.
|
alpar@434
|
165 |
|
alpar@434
|
166 |
///Set the first node of the path.
|
alpar@434
|
167 |
///If the path is empty, this must be call before any call of
|
alpar@434
|
168 |
///\ref pushFront() or \ref pushBack()
|
alpar@434
|
169 |
void setFirst(const GraphNode &) { }
|
alpar@434
|
170 |
|
alpar@434
|
171 |
///Push a new edge to the front of the path
|
alpar@434
|
172 |
|
alpar@434
|
173 |
///Push a new edge to the front of the path.
|
alpar@434
|
174 |
///\sa setFirst()
|
klao@369
|
175 |
bool pushFront(const GraphEdge& e) {
|
klao@369
|
176 |
if( empty() || P.gr->head(e)==from() ) {
|
klao@369
|
177 |
d.push_back(e);
|
klao@369
|
178 |
return true;
|
klao@369
|
179 |
}
|
klao@369
|
180 |
return false;
|
klao@369
|
181 |
}
|
alpar@434
|
182 |
///Push a new edge to the back of the path
|
alpar@434
|
183 |
|
alpar@434
|
184 |
///Push a new edge to the back of the path.
|
alpar@434
|
185 |
///\sa setFirst()
|
klao@369
|
186 |
bool pushBack(const GraphEdge& e) {
|
klao@369
|
187 |
if( empty() || P.gr->tail(e)==to() ) {
|
klao@369
|
188 |
P.edges.push_back(e);
|
klao@369
|
189 |
return true;
|
klao@369
|
190 |
}
|
klao@369
|
191 |
return false;
|
klao@369
|
192 |
}
|
klao@369
|
193 |
|
alpar@434
|
194 |
///Commit the changes to the path.
|
klao@369
|
195 |
void commit() {
|
klao@369
|
196 |
if( !d.empty() ) {
|
klao@369
|
197 |
P.edges.insert(P.edges.begin(), d.rbegin(), d.rend());
|
klao@369
|
198 |
d.clear();
|
klao@369
|
199 |
}
|
klao@369
|
200 |
}
|
klao@369
|
201 |
|
alpar@434
|
202 |
///Desctuctor
|
alpar@434
|
203 |
|
alpar@434
|
204 |
///The desctuctor.
|
alpar@434
|
205 |
///It commit also commit the changes.
|
alpar@434
|
206 |
///\todo Is this what we want?
|
klao@369
|
207 |
~Builder() { commit(); }
|
klao@369
|
208 |
|
klao@369
|
209 |
// FIXME: Hmm, pontosan hogy is kene ezt csinalni?
|
klao@369
|
210 |
// Hogy kenyelmes egy ilyet hasznalni?
|
klao@369
|
211 |
void reserve(size_t r) {
|
klao@369
|
212 |
d.reserve(r);
|
klao@369
|
213 |
P.edges.reserve(P.length()+r);
|
klao@369
|
214 |
}
|
klao@369
|
215 |
|
klao@369
|
216 |
private:
|
klao@369
|
217 |
bool empty() { return d.empty() && P.empty(); }
|
klao@369
|
218 |
|
klao@369
|
219 |
GraphNode from() const {
|
klao@369
|
220 |
if( ! d.empty() )
|
klao@369
|
221 |
return P.gr->tail(d[d.size()-1]);
|
klao@369
|
222 |
else if( ! P.empty() )
|
klao@369
|
223 |
return P.gr->tail(P.edges[0]);
|
klao@369
|
224 |
else
|
klao@369
|
225 |
return INVALID;
|
klao@369
|
226 |
}
|
klao@369
|
227 |
GraphNode to() const {
|
klao@369
|
228 |
if( ! P.empty() )
|
klao@369
|
229 |
return P.gr->head(P.edges[P.length()-1]);
|
klao@369
|
230 |
else if( ! d.empty() )
|
klao@369
|
231 |
return P.gr->head(d[0]);
|
klao@369
|
232 |
else
|
klao@369
|
233 |
return INVALID;
|
klao@369
|
234 |
}
|
klao@369
|
235 |
|
klao@369
|
236 |
};
|
klao@369
|
237 |
|
klao@369
|
238 |
};
|
klao@369
|
239 |
|
klao@369
|
240 |
|
klao@369
|
241 |
|
klao@369
|
242 |
|
klao@369
|
243 |
|
klao@369
|
244 |
|
klao@369
|
245 |
|
klao@369
|
246 |
|
klao@369
|
247 |
|
klao@369
|
248 |
|
klao@369
|
249 |
/**********************************************************************/
|
klao@369
|
250 |
|
klao@369
|
251 |
|
klao@225
|
252 |
/* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
|
klao@225
|
253 |
elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
|
klao@225
|
254 |
|
klao@225
|
255 |
template<typename Graph>
|
klao@369
|
256 |
class DynamicPath {
|
klao@225
|
257 |
|
klao@225
|
258 |
public:
|
klao@225
|
259 |
typedef typename Graph::Edge GraphEdge;
|
klao@225
|
260 |
typedef typename Graph::Node GraphNode;
|
klao@225
|
261 |
class NodeIt;
|
klao@225
|
262 |
class EdgeIt;
|
klao@225
|
263 |
|
klao@225
|
264 |
protected:
|
klao@225
|
265 |
Graph& G;
|
klao@225
|
266 |
// FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
|
klao@225
|
267 |
// iranyitasat:
|
klao@225
|
268 |
GraphNode _first, _last;
|
klao@225
|
269 |
typedef std::deque<GraphEdge> Container;
|
klao@225
|
270 |
Container edges;
|
klao@225
|
271 |
|
klao@225
|
272 |
public:
|
klao@225
|
273 |
|
klao@369
|
274 |
DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
|
klao@225
|
275 |
|
klao@226
|
276 |
/// Subpath defined by two nodes.
|
klao@226
|
277 |
/// Nodes may be in reversed order, then
|
klao@226
|
278 |
/// we contstruct the reversed path.
|
klao@369
|
279 |
DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
|
klao@226
|
280 |
/// Subpath defined by two edges. Contains edges in [a,b)
|
klao@226
|
281 |
/// It is an error if the two edges are not in order!
|
klao@369
|
282 |
DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
|
klao@225
|
283 |
|
klao@225
|
284 |
size_t length() const { return edges.size(); }
|
klao@225
|
285 |
GraphNode from() const { return _first; }
|
klao@225
|
286 |
GraphNode to() const { return _last; }
|
klao@225
|
287 |
|
klao@225
|
288 |
NodeIt& first(NodeIt &n) const { return nth(n, 0); }
|
klao@225
|
289 |
EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
|
klao@225
|
290 |
template<typename It>
|
klao@225
|
291 |
It first() const {
|
klao@225
|
292 |
It e;
|
klao@225
|
293 |
first(e);
|
klao@225
|
294 |
return e;
|
klao@225
|
295 |
}
|
klao@225
|
296 |
|
klao@225
|
297 |
NodeIt& nth(NodeIt &, size_t) const;
|
klao@225
|
298 |
EdgeIt& nth(EdgeIt &, size_t) const;
|
klao@225
|
299 |
template<typename It>
|
klao@225
|
300 |
It nth(size_t n) const {
|
klao@225
|
301 |
It e;
|
klao@225
|
302 |
nth(e, n);
|
klao@225
|
303 |
return e;
|
klao@225
|
304 |
}
|
klao@225
|
305 |
|
klao@225
|
306 |
bool valid(const NodeIt &n) const { return n.idx <= length(); }
|
klao@225
|
307 |
bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
|
klao@225
|
308 |
|
klao@225
|
309 |
bool isForward(const EdgeIt &e) const { return e.forw; }
|
klao@225
|
310 |
|
klao@226
|
311 |
/// index of a node on the path. Returns length+2 for the invalid NodeIt
|
klao@226
|
312 |
int index(const NodeIt &n) const { return n.idx; }
|
klao@226
|
313 |
/// index of an edge on the path. Returns length+1 for the invalid EdgeIt
|
klao@226
|
314 |
int index(const EdgeIt &e) const { return e.it - edges.begin(); }
|
klao@226
|
315 |
|
klao@225
|
316 |
EdgeIt& next(EdgeIt &e) const;
|
klao@225
|
317 |
NodeIt& next(NodeIt &n) const;
|
klao@225
|
318 |
template <typename It>
|
klao@225
|
319 |
It getNext(It it) const {
|
klao@225
|
320 |
It tmp(it); return next(tmp);
|
klao@225
|
321 |
}
|
klao@225
|
322 |
|
klao@225
|
323 |
// A path is constructed using the following four functions.
|
klao@225
|
324 |
// They return false if the requested operation is inconsistent
|
klao@225
|
325 |
// with the path constructed so far.
|
klao@225
|
326 |
// If your path has only one edge you MUST set either "from" or "to"!
|
klao@225
|
327 |
// So you probably SHOULD call it in any case to be safe (and check the
|
klao@225
|
328 |
// returned value to check if your path is consistent with your idea).
|
klao@225
|
329 |
bool pushFront(const GraphEdge &e);
|
klao@225
|
330 |
bool pushBack(const GraphEdge &e);
|
klao@225
|
331 |
bool setFrom(const GraphNode &n);
|
klao@225
|
332 |
bool setTo(const GraphNode &n);
|
klao@225
|
333 |
|
klao@225
|
334 |
// WARNING: these two functions return the head/tail of an edge with
|
klao@225
|
335 |
// respect to the direction of the path!
|
klao@225
|
336 |
// So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
|
klao@225
|
337 |
// P.forward(e) is true (or the edge is a loop)!
|
klao@225
|
338 |
NodeIt head(const EdgeIt& e) const;
|
klao@225
|
339 |
NodeIt tail(const EdgeIt& e) const;
|
klao@225
|
340 |
|
klao@225
|
341 |
// FIXME: ezeknek valami jobb nev kellene!!!
|
klao@225
|
342 |
GraphEdge graphEdge(const EdgeIt& e) const;
|
klao@225
|
343 |
GraphNode graphNode(const NodeIt& n) const;
|
klao@225
|
344 |
|
klao@225
|
345 |
|
klao@225
|
346 |
/*** Iterator classes ***/
|
klao@225
|
347 |
class EdgeIt {
|
klao@369
|
348 |
friend class DynamicPath;
|
klao@225
|
349 |
|
klao@225
|
350 |
typename Container::const_iterator it;
|
klao@225
|
351 |
bool forw;
|
klao@225
|
352 |
public:
|
klao@225
|
353 |
// FIXME: jarna neki ilyen is...
|
klao@225
|
354 |
// EdgeIt(Invalid);
|
klao@225
|
355 |
|
klao@225
|
356 |
bool forward() const { return forw; }
|
klao@225
|
357 |
|
klao@225
|
358 |
bool operator==(const EdgeIt& e) const { return it==e.it; }
|
klao@225
|
359 |
bool operator!=(const EdgeIt& e) const { return it!=e.it; }
|
klao@225
|
360 |
bool operator<(const EdgeIt& e) const { return it<e.it; }
|
klao@225
|
361 |
};
|
klao@225
|
362 |
|
klao@225
|
363 |
class NodeIt {
|
klao@369
|
364 |
friend class DynamicPath;
|
klao@225
|
365 |
|
klao@226
|
366 |
size_t idx;
|
klao@225
|
367 |
bool tail; // Is this node the tail of the edge with same idx?
|
klao@225
|
368 |
|
klao@225
|
369 |
public:
|
klao@225
|
370 |
// FIXME: jarna neki ilyen is...
|
klao@225
|
371 |
// NodeIt(Invalid);
|
klao@225
|
372 |
|
klao@225
|
373 |
bool operator==(const NodeIt& n) const { return idx==n.idx; }
|
klao@225
|
374 |
bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
|
klao@225
|
375 |
bool operator<(const NodeIt& n) const { return idx<n.idx; }
|
klao@225
|
376 |
};
|
klao@225
|
377 |
|
klao@225
|
378 |
private:
|
klao@225
|
379 |
bool edgeIncident(const GraphEdge &e, const GraphNode &a,
|
klao@225
|
380 |
GraphNode &b);
|
klao@225
|
381 |
bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
|
klao@225
|
382 |
};
|
klao@225
|
383 |
|
klao@225
|
384 |
template<typename Gr>
|
klao@369
|
385 |
typename DynamicPath<Gr>::EdgeIt&
|
klao@369
|
386 |
DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
|
klao@225
|
387 |
if( e.it == edges.end() )
|
klao@225
|
388 |
return e;
|
klao@225
|
389 |
|
klao@225
|
390 |
GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
|
klao@225
|
391 |
++e.it;
|
klao@225
|
392 |
|
klao@225
|
393 |
// Invalid edgeit is always forward :)
|
klao@225
|
394 |
if( e.it == edges.end() ) {
|
klao@225
|
395 |
e.forw = true;
|
klao@225
|
396 |
return e;
|
klao@225
|
397 |
}
|
klao@225
|
398 |
|
klao@225
|
399 |
e.forw = ( G.tail(*e.it) == common_node );
|
klao@225
|
400 |
return e;
|
klao@225
|
401 |
}
|
klao@225
|
402 |
|
klao@225
|
403 |
template<typename Gr>
|
klao@369
|
404 |
typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
|
klao@225
|
405 |
if( n.idx >= length() ) {
|
klao@225
|
406 |
// FIXME: invalid
|
klao@225
|
407 |
n.idx = length()+1;
|
klao@225
|
408 |
return n;
|
klao@225
|
409 |
}
|
klao@225
|
410 |
|
klao@225
|
411 |
|
klao@225
|
412 |
GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
|
klao@225
|
413 |
G.tail(edges[n.idx]) );
|
klao@225
|
414 |
++n.idx;
|
klao@225
|
415 |
if( n.idx < length() ) {
|
klao@225
|
416 |
n.tail = ( next_node == G.tail(edges[n.idx]) );
|
klao@225
|
417 |
}
|
klao@225
|
418 |
else {
|
klao@225
|
419 |
n.tail = true;
|
klao@225
|
420 |
}
|
klao@225
|
421 |
|
klao@225
|
422 |
return n;
|
klao@225
|
423 |
}
|
klao@225
|
424 |
|
klao@225
|
425 |
template<typename Gr>
|
klao@369
|
426 |
bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
|
klao@225
|
427 |
GraphNode &b) {
|
klao@225
|
428 |
if( G.tail(e) == a ) {
|
klao@225
|
429 |
b=G.head(e);
|
klao@225
|
430 |
return true;
|
klao@225
|
431 |
}
|
klao@225
|
432 |
if( G.head(e) == a ) {
|
klao@225
|
433 |
b=G.tail(e);
|
klao@225
|
434 |
return true;
|
klao@225
|
435 |
}
|
klao@225
|
436 |
return false;
|
klao@225
|
437 |
}
|
klao@225
|
438 |
|
klao@225
|
439 |
template<typename Gr>
|
klao@369
|
440 |
bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
|
klao@225
|
441 |
const GraphEdge &f) {
|
klao@225
|
442 |
if( edgeIncident(f, G.tail(e), _last) ) {
|
klao@225
|
443 |
_first = G.head(e);
|
klao@225
|
444 |
return true;
|
klao@225
|
445 |
}
|
klao@225
|
446 |
if( edgeIncident(f, G.head(e), _last) ) {
|
klao@225
|
447 |
_first = G.tail(e);
|
klao@225
|
448 |
return true;
|
klao@225
|
449 |
}
|
klao@225
|
450 |
return false;
|
klao@225
|
451 |
}
|
klao@225
|
452 |
|
klao@225
|
453 |
template<typename Gr>
|
klao@369
|
454 |
bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
|
klao@225
|
455 |
if( G.valid(_first) ) {
|
klao@225
|
456 |
if( edgeIncident(e, _first, _first) ) {
|
klao@225
|
457 |
edges.push_front(e);
|
klao@225
|
458 |
return true;
|
klao@225
|
459 |
}
|
klao@225
|
460 |
else
|
klao@225
|
461 |
return false;
|
klao@225
|
462 |
}
|
klao@225
|
463 |
else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
|
klao@225
|
464 |
edges.push_front(e);
|
klao@225
|
465 |
return true;
|
klao@225
|
466 |
}
|
klao@225
|
467 |
else
|
klao@225
|
468 |
return false;
|
klao@225
|
469 |
}
|
klao@225
|
470 |
|
klao@225
|
471 |
template<typename Gr>
|
klao@369
|
472 |
bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
|
klao@225
|
473 |
if( G.valid(_last) ) {
|
klao@225
|
474 |
if( edgeIncident(e, _last, _last) ) {
|
klao@225
|
475 |
edges.push_back(e);
|
klao@225
|
476 |
return true;
|
klao@225
|
477 |
}
|
klao@225
|
478 |
else
|
klao@225
|
479 |
return false;
|
klao@225
|
480 |
}
|
klao@225
|
481 |
else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
|
klao@225
|
482 |
edges.push_back(e);
|
klao@225
|
483 |
return true;
|
klao@225
|
484 |
}
|
klao@225
|
485 |
else
|
klao@225
|
486 |
return false;
|
klao@225
|
487 |
}
|
klao@225
|
488 |
|
klao@225
|
489 |
|
klao@225
|
490 |
template<typename Gr>
|
klao@369
|
491 |
bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
|
klao@225
|
492 |
if( G.valid(_first) ) {
|
klao@225
|
493 |
return _first == n;
|
klao@225
|
494 |
}
|
klao@225
|
495 |
else {
|
klao@225
|
496 |
if( length() > 0) {
|
klao@225
|
497 |
if( edgeIncident(edges[0], n, _last) ) {
|
klao@225
|
498 |
_first = n;
|
klao@225
|
499 |
return true;
|
klao@225
|
500 |
}
|
klao@225
|
501 |
else return false;
|
klao@225
|
502 |
}
|
klao@225
|
503 |
else {
|
klao@225
|
504 |
_first = _last = n;
|
klao@225
|
505 |
return true;
|
klao@225
|
506 |
}
|
klao@225
|
507 |
}
|
klao@225
|
508 |
}
|
klao@225
|
509 |
|
klao@225
|
510 |
template<typename Gr>
|
klao@369
|
511 |
bool DynamicPath<Gr>::setTo(const GraphNode &n) {
|
klao@225
|
512 |
if( G.valid(_last) ) {
|
klao@225
|
513 |
return _last == n;
|
klao@225
|
514 |
}
|
klao@225
|
515 |
else {
|
klao@225
|
516 |
if( length() > 0) {
|
klao@225
|
517 |
if( edgeIncident(edges[0], n, _first) ) {
|
klao@225
|
518 |
_last = n;
|
klao@225
|
519 |
return true;
|
klao@225
|
520 |
}
|
klao@225
|
521 |
else return false;
|
klao@225
|
522 |
}
|
klao@225
|
523 |
else {
|
klao@225
|
524 |
_first = _last = n;
|
klao@225
|
525 |
return true;
|
klao@225
|
526 |
}
|
klao@225
|
527 |
}
|
klao@225
|
528 |
}
|
klao@225
|
529 |
|
klao@225
|
530 |
|
klao@225
|
531 |
template<typename Gr>
|
klao@369
|
532 |
typename DynamicPath<Gr>::NodeIt
|
klao@369
|
533 |
DynamicPath<Gr>::tail(const EdgeIt& e) const {
|
klao@225
|
534 |
NodeIt n;
|
klao@225
|
535 |
|
klao@225
|
536 |
if( e.it == edges.end() ) {
|
klao@225
|
537 |
// FIXME: invalid-> invalid
|
klao@225
|
538 |
n.idx = length() + 1;
|
klao@225
|
539 |
n.tail = true;
|
klao@225
|
540 |
return n;
|
klao@225
|
541 |
}
|
klao@225
|
542 |
|
klao@225
|
543 |
n.idx = e.it-edges.begin();
|
klao@225
|
544 |
n.tail = e.forw;
|
klao@226
|
545 |
return n;
|
klao@225
|
546 |
}
|
klao@225
|
547 |
|
klao@225
|
548 |
template<typename Gr>
|
klao@369
|
549 |
typename DynamicPath<Gr>::NodeIt
|
klao@369
|
550 |
DynamicPath<Gr>::head(const EdgeIt& e) const {
|
klao@225
|
551 |
if( e.it == edges.end()-1 ) {
|
klao@225
|
552 |
return _last;
|
klao@225
|
553 |
}
|
klao@225
|
554 |
|
klao@225
|
555 |
EdgeIt next_edge = e;
|
klao@225
|
556 |
next(next_edge);
|
klao@225
|
557 |
return tail(next_edge);
|
klao@225
|
558 |
}
|
klao@225
|
559 |
|
klao@225
|
560 |
template<typename Gr>
|
klao@369
|
561 |
typename DynamicPath<Gr>::GraphEdge
|
klao@369
|
562 |
DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
|
klao@225
|
563 |
if( e.it != edges.end() ) {
|
klao@225
|
564 |
return *e.it;
|
klao@225
|
565 |
}
|
klao@225
|
566 |
else {
|
klao@225
|
567 |
return INVALID;
|
klao@225
|
568 |
}
|
klao@225
|
569 |
}
|
klao@225
|
570 |
|
klao@225
|
571 |
template<typename Gr>
|
klao@369
|
572 |
typename DynamicPath<Gr>::GraphNode
|
klao@369
|
573 |
DynamicPath<Gr>::graphNode(const NodeIt& n) const {
|
klao@225
|
574 |
if( n.idx < length() ) {
|
klao@225
|
575 |
return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
|
klao@225
|
576 |
}
|
klao@225
|
577 |
else if( n.idx == length() ) {
|
klao@225
|
578 |
return _last;
|
klao@225
|
579 |
}
|
klao@225
|
580 |
else {
|
klao@225
|
581 |
return INVALID;
|
klao@225
|
582 |
}
|
klao@225
|
583 |
}
|
klao@225
|
584 |
|
klao@225
|
585 |
template<typename Gr>
|
klao@369
|
586 |
typename DynamicPath<Gr>::EdgeIt&
|
klao@369
|
587 |
DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
|
klao@225
|
588 |
if( k<0 || k>=length() ) {
|
klao@225
|
589 |
// FIXME: invalid EdgeIt
|
klao@225
|
590 |
e.it = edges.end();
|
klao@225
|
591 |
e.forw = true;
|
klao@225
|
592 |
return e;
|
klao@225
|
593 |
}
|
klao@225
|
594 |
|
klao@225
|
595 |
e.it = edges.begin()+k;
|
klao@225
|
596 |
if(k==0) {
|
klao@225
|
597 |
e.forw = ( G.tail(*e.it) == _first );
|
klao@225
|
598 |
}
|
klao@225
|
599 |
else {
|
klao@225
|
600 |
e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
|
klao@225
|
601 |
G.tail(*e.it) == G.head(edges[k-1]) );
|
klao@225
|
602 |
}
|
klao@225
|
603 |
return e;
|
klao@225
|
604 |
}
|
klao@225
|
605 |
|
klao@225
|
606 |
template<typename Gr>
|
klao@369
|
607 |
typename DynamicPath<Gr>::NodeIt&
|
klao@369
|
608 |
DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
|
klao@225
|
609 |
if( k<0 || k>length() ) {
|
klao@225
|
610 |
// FIXME: invalid NodeIt
|
klao@225
|
611 |
n.idx = length()+1;
|
klao@225
|
612 |
n.tail = true;
|
klao@225
|
613 |
return n;
|
klao@225
|
614 |
}
|
klao@225
|
615 |
if( k==length() ) {
|
klao@225
|
616 |
n.idx = length();
|
klao@225
|
617 |
n.tail = true;
|
klao@225
|
618 |
return n;
|
klao@225
|
619 |
}
|
klao@225
|
620 |
n = tail(nth<EdgeIt>(k));
|
klao@225
|
621 |
return n;
|
klao@225
|
622 |
}
|
klao@225
|
623 |
|
klao@226
|
624 |
// Reszut konstruktorok:
|
klao@226
|
625 |
|
klao@226
|
626 |
|
klao@226
|
627 |
template<typename Gr>
|
klao@369
|
628 |
DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
|
klao@369
|
629 |
const EdgeIt &b) :
|
klao@226
|
630 |
G(P.G), edges(a.it, b.it) // WARNING: if b.it < a.it this will blow up!
|
klao@226
|
631 |
{
|
klao@226
|
632 |
if( G.valid(P._first) && a.it < P.edges.end() ) {
|
klao@226
|
633 |
_first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
|
klao@226
|
634 |
if( b.it < P.edges.end() ) {
|
klao@226
|
635 |
_last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
|
klao@226
|
636 |
}
|
klao@226
|
637 |
else {
|
klao@226
|
638 |
_last = P._last;
|
klao@226
|
639 |
}
|
klao@226
|
640 |
}
|
klao@226
|
641 |
}
|
klao@226
|
642 |
|
klao@226
|
643 |
template<typename Gr>
|
klao@369
|
644 |
DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
|
klao@369
|
645 |
const NodeIt &b) : G(P.G)
|
klao@226
|
646 |
{
|
klao@226
|
647 |
if( !P.valid(a) || !P.valid(b) )
|
klao@226
|
648 |
return;
|
klao@226
|
649 |
|
klao@226
|
650 |
int ai = a.idx, bi = b.idx;
|
klao@226
|
651 |
if( bi<ai )
|
klao@226
|
652 |
swap(ai,bi);
|
klao@226
|
653 |
|
klao@226
|
654 |
edges.resize(bi-ai);
|
klao@226
|
655 |
copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
|
klao@226
|
656 |
|
klao@226
|
657 |
_first = P.graphNode(a);
|
klao@226
|
658 |
_last = P.graphNode(b);
|
klao@226
|
659 |
}
|
klao@226
|
660 |
|
alpar@434
|
661 |
///@}
|
klao@225
|
662 |
|
klao@225
|
663 |
} // namespace hugo
|
klao@225
|
664 |
|
klao@225
|
665 |
#endif // HUGO_PATH_H
|