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