hegyi@819
|
1 |
// -*- c++ -*- //
|
hegyi@819
|
2 |
|
hegyi@819
|
3 |
/**
|
hegyi@819
|
4 |
@defgroup paths Path Structures
|
hegyi@819
|
5 |
@ingroup datas
|
hegyi@819
|
6 |
\brief Path structures implemented in Hugo.
|
hegyi@819
|
7 |
|
hegyi@819
|
8 |
Hugolib provides flexible data structures
|
hegyi@819
|
9 |
to work with paths.
|
hegyi@819
|
10 |
|
hegyi@819
|
11 |
All of them have the same interface, especially they can be built or extended
|
hegyi@819
|
12 |
using a standard Builder subclass. This make is easy to have e.g. the Dijkstra
|
hegyi@819
|
13 |
algorithm to store its result in any kind of path structure.
|
hegyi@819
|
14 |
|
hegyi@819
|
15 |
\sa hugo::skeleton::Path
|
hegyi@819
|
16 |
|
hegyi@819
|
17 |
*/
|
hegyi@819
|
18 |
|
hegyi@819
|
19 |
///\ingroup paths
|
hegyi@819
|
20 |
///\file
|
hegyi@819
|
21 |
///\brief Classes for representing paths in graphs.
|
hegyi@819
|
22 |
|
hegyi@819
|
23 |
#ifndef HUGO_PATH_H
|
hegyi@819
|
24 |
#define HUGO_PATH_H
|
hegyi@819
|
25 |
|
hegyi@819
|
26 |
#include <deque>
|
hegyi@819
|
27 |
#include <vector>
|
hegyi@819
|
28 |
#include <algorithm>
|
hegyi@819
|
29 |
|
hegyi@819
|
30 |
#include <hugo/invalid.h>
|
hegyi@819
|
31 |
|
hegyi@819
|
32 |
namespace hugo {
|
hegyi@819
|
33 |
|
hegyi@819
|
34 |
/// \addtogroup paths
|
hegyi@819
|
35 |
/// @{
|
hegyi@819
|
36 |
|
hegyi@819
|
37 |
|
hegyi@819
|
38 |
//! \brief A structure for representing directed paths in a graph.
|
hegyi@819
|
39 |
//!
|
hegyi@819
|
40 |
//! A structure for representing directed path in a graph.
|
hegyi@819
|
41 |
//! \param Graph The graph type in which the path is.
|
hegyi@819
|
42 |
//! \param DM DebugMode, defaults to DefaultDebugMode.
|
hegyi@837
|
43 |
//!
|
hegyi@819
|
44 |
//! In a sense, the path can be treated as a graph, for is has \c NodeIt
|
hegyi@819
|
45 |
//! and \c EdgeIt with the same usage. These types converts to the \c Node
|
hegyi@819
|
46 |
//! and \c Edge of the original graph.
|
hegyi@819
|
47 |
//!
|
hegyi@819
|
48 |
//! \todo Thoroughfully check all the range and consistency tests.
|
hegyi@831
|
49 |
template<typename Graph>
|
hegyi@819
|
50 |
class DirPath {
|
hegyi@819
|
51 |
public:
|
hegyi@819
|
52 |
/// Edge type of the underlying graph.
|
hegyi@837
|
53 |
typedef typename Graph::Edge GraphEdge;
|
hegyi@819
|
54 |
/// Node type of the underlying graph.
|
hegyi@819
|
55 |
typedef typename Graph::Node GraphNode;
|
hegyi@819
|
56 |
class NodeIt;
|
hegyi@819
|
57 |
class EdgeIt;
|
hegyi@819
|
58 |
|
hegyi@819
|
59 |
protected:
|
hegyi@819
|
60 |
const Graph *gr;
|
hegyi@819
|
61 |
typedef std::vector<GraphEdge> Container;
|
hegyi@819
|
62 |
Container edges;
|
hegyi@819
|
63 |
|
hegyi@819
|
64 |
public:
|
hegyi@819
|
65 |
|
hegyi@819
|
66 |
/// \param _G The graph in which the path is.
|
hegyi@819
|
67 |
///
|
hegyi@819
|
68 |
DirPath(const Graph &_G) : gr(&_G) {}
|
hegyi@819
|
69 |
|
hegyi@819
|
70 |
/// \brief Subpath constructor.
|
hegyi@819
|
71 |
///
|
hegyi@819
|
72 |
/// Subpath defined by two nodes.
|
hegyi@819
|
73 |
/// \warning It is an error if the two edges are not in order!
|
hegyi@819
|
74 |
DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
|
hegyi@819
|
75 |
gr = P.gr;
|
hegyi@819
|
76 |
edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
|
hegyi@819
|
77 |
}
|
hegyi@819
|
78 |
|
hegyi@819
|
79 |
/// \brief Subpath constructor.
|
hegyi@819
|
80 |
///
|
hegyi@819
|
81 |
/// Subpath defined by two edges. Contains edges in [a,b)
|
hegyi@819
|
82 |
/// \warning It is an error if the two edges are not in order!
|
hegyi@819
|
83 |
DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
|
hegyi@819
|
84 |
gr = P.gr;
|
hegyi@819
|
85 |
edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
|
hegyi@819
|
86 |
}
|
hegyi@819
|
87 |
|
hegyi@819
|
88 |
/// Length of the path.
|
hegyi@819
|
89 |
size_t length() const { return edges.size(); }
|
hegyi@819
|
90 |
/// Returns whether the path is empty.
|
hegyi@819
|
91 |
bool empty() const { return edges.empty(); }
|
hegyi@819
|
92 |
|
hegyi@819
|
93 |
/// Resets the path to an empty path.
|
hegyi@819
|
94 |
void clear() { edges.clear(); }
|
hegyi@819
|
95 |
|
hegyi@819
|
96 |
/// \brief Starting point of the path.
|
hegyi@819
|
97 |
///
|
hegyi@819
|
98 |
/// Starting point of the path.
|
hegyi@819
|
99 |
/// Returns INVALID if the path is empty.
|
hegyi@831
|
100 |
GraphNode tail() const {
|
hegyi@819
|
101 |
return empty() ? INVALID : gr->tail(edges[0]);
|
hegyi@819
|
102 |
}
|
hegyi@819
|
103 |
/// \brief End point of the path.
|
hegyi@819
|
104 |
///
|
hegyi@819
|
105 |
/// End point of the path.
|
hegyi@819
|
106 |
/// Returns INVALID if the path is empty.
|
hegyi@831
|
107 |
GraphNode head() const {
|
hegyi@819
|
108 |
return empty() ? INVALID : gr->head(edges[length()-1]);
|
hegyi@819
|
109 |
}
|
hegyi@819
|
110 |
|
hegyi@819
|
111 |
/// \brief Initializes node or edge iterator to point to the first
|
hegyi@819
|
112 |
/// node or edge.
|
hegyi@819
|
113 |
///
|
hegyi@819
|
114 |
/// \sa nth
|
hegyi@819
|
115 |
template<typename It>
|
hegyi@819
|
116 |
It& first(It &i) const { return i=It(*this); }
|
hegyi@819
|
117 |
|
hegyi@819
|
118 |
/// \brief Initializes node iterator to point to the node of a given index.
|
hegyi@819
|
119 |
NodeIt& nth(NodeIt &i, int n) const {
|
hegyi@819
|
120 |
return i=NodeIt(*this, n);
|
hegyi@819
|
121 |
}
|
hegyi@819
|
122 |
|
hegyi@819
|
123 |
/// \brief Initializes edge iterator to point to the edge of a given index.
|
hegyi@819
|
124 |
EdgeIt& nth(EdgeIt &i, int n) const {
|
hegyi@819
|
125 |
return i=EdgeIt(*this, n);
|
hegyi@819
|
126 |
}
|
hegyi@819
|
127 |
|
hegyi@819
|
128 |
/// \brief Returns node iterator pointing to the head node of the
|
hegyi@819
|
129 |
/// given edge iterator.
|
hegyi@819
|
130 |
NodeIt head(const EdgeIt& e) const {
|
hegyi@819
|
131 |
return NodeIt(*this, e.idx+1);
|
hegyi@819
|
132 |
}
|
hegyi@819
|
133 |
|
hegyi@819
|
134 |
/// \brief Returns node iterator pointing to the tail node of the
|
hegyi@819
|
135 |
/// given edge iterator.
|
hegyi@819
|
136 |
NodeIt tail(const EdgeIt& e) const {
|
hegyi@819
|
137 |
return NodeIt(*this, e.idx);
|
hegyi@819
|
138 |
}
|
hegyi@819
|
139 |
|
hegyi@819
|
140 |
|
hegyi@819
|
141 |
/* Iterator classes */
|
hegyi@819
|
142 |
|
hegyi@819
|
143 |
/**
|
hegyi@819
|
144 |
* \brief Iterator class to iterate on the edges of the paths
|
hegyi@837
|
145 |
*
|
hegyi@819
|
146 |
* \ingroup paths
|
hegyi@819
|
147 |
* This class is used to iterate on the edges of the paths
|
hegyi@819
|
148 |
*
|
hegyi@819
|
149 |
* Of course it converts to Graph::Edge
|
hegyi@837
|
150 |
*
|
hegyi@819
|
151 |
*/
|
hegyi@819
|
152 |
class EdgeIt {
|
hegyi@819
|
153 |
friend class DirPath;
|
hegyi@819
|
154 |
|
hegyi@819
|
155 |
int idx;
|
hegyi@819
|
156 |
const DirPath *p;
|
hegyi@819
|
157 |
public:
|
hegyi@819
|
158 |
/// Default constructor
|
hegyi@819
|
159 |
EdgeIt() {}
|
hegyi@819
|
160 |
/// Invalid constructor
|
hegyi@819
|
161 |
EdgeIt(Invalid) : idx(-1), p(0) {}
|
hegyi@819
|
162 |
/// Constructor with starting point
|
hegyi@819
|
163 |
EdgeIt(const DirPath &_p, int _idx = 0) :
|
hegyi@819
|
164 |
idx(_idx), p(&_p) { validate(); }
|
hegyi@819
|
165 |
|
hegyi@819
|
166 |
///Validity check
|
hegyi@819
|
167 |
bool valid() const { return idx!=-1; }
|
hegyi@819
|
168 |
|
hegyi@819
|
169 |
///Conversion to Graph::Edge
|
hegyi@819
|
170 |
operator GraphEdge () const {
|
hegyi@819
|
171 |
return valid() ? p->edges[idx] : INVALID;
|
hegyi@819
|
172 |
}
|
hegyi@819
|
173 |
|
hegyi@819
|
174 |
/// Next edge
|
hegyi@819
|
175 |
EdgeIt& operator++() { ++idx; validate(); return *this; }
|
hegyi@819
|
176 |
|
hegyi@819
|
177 |
/// Comparison operator
|
hegyi@819
|
178 |
bool operator==(const EdgeIt& e) const { return idx==e.idx; }
|
hegyi@819
|
179 |
/// Comparison operator
|
hegyi@819
|
180 |
bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
|
hegyi@819
|
181 |
/// Comparison operator
|
hegyi@819
|
182 |
bool operator<(const EdgeIt& e) const { return idx<e.idx; }
|
hegyi@819
|
183 |
|
hegyi@819
|
184 |
private:
|
hegyi@819
|
185 |
// FIXME: comparison between signed and unsigned...
|
hegyi@819
|
186 |
// Jo ez igy? Vagy esetleg legyen a length() int?
|
hegyi@819
|
187 |
void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
|
hegyi@819
|
188 |
};
|
hegyi@819
|
189 |
|
hegyi@819
|
190 |
/**
|
hegyi@819
|
191 |
* \brief Iterator class to iterate on the nodes of the paths
|
hegyi@837
|
192 |
*
|
hegyi@819
|
193 |
* \ingroup paths
|
hegyi@819
|
194 |
* This class is used to iterate on the nodes of the paths
|
hegyi@819
|
195 |
*
|
hegyi@819
|
196 |
* Of course it converts to Graph::Node
|
hegyi@837
|
197 |
*
|
hegyi@819
|
198 |
*/
|
hegyi@819
|
199 |
class NodeIt {
|
hegyi@819
|
200 |
friend class DirPath;
|
hegyi@819
|
201 |
|
hegyi@819
|
202 |
int idx;
|
hegyi@819
|
203 |
const DirPath *p;
|
hegyi@819
|
204 |
public:
|
hegyi@819
|
205 |
/// Default constructor
|
hegyi@819
|
206 |
NodeIt() {}
|
hegyi@819
|
207 |
/// Invalid constructor
|
hegyi@819
|
208 |
NodeIt(Invalid) : idx(-1), p(0) {}
|
hegyi@819
|
209 |
/// Constructor with starting point
|
hegyi@819
|
210 |
NodeIt(const DirPath &_p, int _idx = 0) :
|
hegyi@819
|
211 |
idx(_idx), p(&_p) { validate(); }
|
hegyi@819
|
212 |
|
hegyi@819
|
213 |
///Validity check
|
hegyi@819
|
214 |
bool valid() const { return idx!=-1; }
|
hegyi@819
|
215 |
|
hegyi@819
|
216 |
///Conversion to Graph::Node
|
hegyi@819
|
217 |
operator const GraphNode& () const {
|
hegyi@819
|
218 |
if(idx >= p->length())
|
hegyi@831
|
219 |
return p->head();
|
hegyi@819
|
220 |
else if(idx >= 0)
|
hegyi@819
|
221 |
return p->gr->tail(p->edges[idx]);
|
hegyi@819
|
222 |
else
|
hegyi@819
|
223 |
return INVALID;
|
hegyi@819
|
224 |
}
|
hegyi@819
|
225 |
/// Next node
|
hegyi@819
|
226 |
NodeIt& operator++() { ++idx; validate(); return *this; }
|
hegyi@819
|
227 |
|
hegyi@819
|
228 |
/// Comparison operator
|
hegyi@819
|
229 |
bool operator==(const NodeIt& e) const { return idx==e.idx; }
|
hegyi@819
|
230 |
/// Comparison operator
|
hegyi@819
|
231 |
bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
|
hegyi@819
|
232 |
/// Comparison operator
|
hegyi@819
|
233 |
bool operator<(const NodeIt& e) const { return idx<e.idx; }
|
hegyi@819
|
234 |
|
hegyi@819
|
235 |
private:
|
hegyi@819
|
236 |
void validate() { if( size_t(idx) > p->length() ) idx=-1; }
|
hegyi@819
|
237 |
};
|
hegyi@819
|
238 |
|
hegyi@837
|
239 |
friend class Builder;
|
hegyi@819
|
240 |
|
hegyi@819
|
241 |
/**
|
hegyi@819
|
242 |
* \brief Class to build paths
|
hegyi@837
|
243 |
*
|
hegyi@819
|
244 |
* \ingroup paths
|
hegyi@819
|
245 |
* This class is used to fill a path with edges.
|
hegyi@819
|
246 |
*
|
hegyi@819
|
247 |
* You can push new edges to the front and to the back of the path in
|
hegyi@819
|
248 |
* arbitrary order then you should commit these changes to the graph.
|
hegyi@819
|
249 |
*
|
hegyi@819
|
250 |
* Fundamentally, for most "Paths" (classes fulfilling the
|
hegyi@819
|
251 |
* PathConcept) while the builder is active (after the first modifying
|
hegyi@819
|
252 |
* operation and until the commit()) the original Path is in a
|
hegyi@819
|
253 |
* "transitional" state (operations on it have undefined result). But
|
hegyi@819
|
254 |
* in the case of DirPath the original path remains unchanged until the
|
hegyi@819
|
255 |
* commit. However we don't recomend that you use this feature.
|
hegyi@819
|
256 |
*/
|
hegyi@819
|
257 |
class Builder {
|
hegyi@819
|
258 |
DirPath &P;
|
hegyi@819
|
259 |
Container front, back;
|
hegyi@819
|
260 |
|
hegyi@819
|
261 |
public:
|
hegyi@819
|
262 |
///\param _P the path you want to fill in.
|
hegyi@819
|
263 |
///
|
hegyi@819
|
264 |
Builder(DirPath &_P) : P(_P) {}
|
hegyi@819
|
265 |
|
hegyi@819
|
266 |
/// Sets the starting node of the path.
|
hegyi@837
|
267 |
|
hegyi@819
|
268 |
/// Sets the starting node of the path. Edge added to the path
|
hegyi@819
|
269 |
/// afterwards have to be incident to this node.
|
alpar@900
|
270 |
/// It should be called if and only if
|
alpar@900
|
271 |
/// the path is empty and before any call to
|
hegyi@819
|
272 |
/// \ref pushFront() or \ref pushBack()
|
hegyi@819
|
273 |
void setStartNode(const GraphNode &) {}
|
hegyi@819
|
274 |
|
hegyi@819
|
275 |
///Push a new edge to the front of the path
|
hegyi@819
|
276 |
|
hegyi@819
|
277 |
///Push a new edge to the front of the path.
|
hegyi@819
|
278 |
///\sa setStartNode
|
hegyi@819
|
279 |
void pushFront(const GraphEdge& e) {
|
hegyi@819
|
280 |
front.push_back(e);
|
hegyi@819
|
281 |
}
|
hegyi@819
|
282 |
|
hegyi@819
|
283 |
///Push a new edge to the back of the path
|
hegyi@819
|
284 |
|
hegyi@819
|
285 |
///Push a new edge to the back of the path.
|
hegyi@819
|
286 |
///\sa setStartNode
|
hegyi@819
|
287 |
void pushBack(const GraphEdge& e) {
|
hegyi@819
|
288 |
back.push_back(e);
|
hegyi@819
|
289 |
}
|
hegyi@819
|
290 |
|
hegyi@819
|
291 |
///Commit the changes to the path.
|
hegyi@819
|
292 |
void commit() {
|
hegyi@837
|
293 |
if( !front.empty() || !back.empty() ) {
|
hegyi@819
|
294 |
Container tmp;
|
hegyi@819
|
295 |
tmp.reserve(front.size()+back.size()+P.length());
|
hegyi@819
|
296 |
tmp.insert(tmp.end(), front.rbegin(), front.rend());
|
hegyi@819
|
297 |
tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
|
hegyi@819
|
298 |
tmp.insert(tmp.end(), back.begin(), back.end());
|
hegyi@819
|
299 |
P.edges.swap(tmp);
|
hegyi@819
|
300 |
front.clear();
|
hegyi@819
|
301 |
back.clear();
|
hegyi@819
|
302 |
}
|
hegyi@819
|
303 |
}
|
hegyi@819
|
304 |
|
hegyi@819
|
305 |
///Reserve storage for the builder in advance.
|
hegyi@819
|
306 |
|
hegyi@837
|
307 |
///If you know a reasonable upper bound of the number of the edges
|
hegyi@837
|
308 |
///to add to the front, using this function you can speed up the building.
|
hegyi@819
|
309 |
|
hegyi@837
|
310 |
void reserveFront(size_t r) {front.reserve(r);}
|
hegyi@837
|
311 |
|
hegyi@837
|
312 |
///Reserve storage for the builder in advance.
|
hegyi@837
|
313 |
|
hegyi@837
|
314 |
///If you know a reasonable upper bound of the number of the edges
|
hegyi@837
|
315 |
///to add to the back, using this function you can speed up the building.
|
hegyi@837
|
316 |
|
hegyi@837
|
317 |
void reserveBack(size_t r) {back.reserve(r);}
|
hegyi@831
|
318 |
|
hegyi@819
|
319 |
private:
|
hegyi@819
|
320 |
bool empty() {
|
hegyi@819
|
321 |
return front.empty() && back.empty() && P.empty();
|
hegyi@819
|
322 |
}
|
hegyi@819
|
323 |
|
hegyi@831
|
324 |
GraphNode tail() const {
|
hegyi@819
|
325 |
if( ! front.empty() )
|
hegyi@819
|
326 |
return P.gr->tail(front[front.size()-1]);
|
hegyi@819
|
327 |
else if( ! P.empty() )
|
hegyi@819
|
328 |
return P.gr->tail(P.edges[0]);
|
hegyi@819
|
329 |
else if( ! back.empty() )
|
hegyi@819
|
330 |
return P.gr->tail(back[0]);
|
hegyi@819
|
331 |
else
|
hegyi@819
|
332 |
return INVALID;
|
hegyi@819
|
333 |
}
|
hegyi@831
|
334 |
GraphNode head() const {
|
hegyi@819
|
335 |
if( ! back.empty() )
|
hegyi@819
|
336 |
return P.gr->head(back[back.size()-1]);
|
hegyi@819
|
337 |
else if( ! P.empty() )
|
hegyi@819
|
338 |
return P.gr->head(P.edges[P.length()-1]);
|
hegyi@819
|
339 |
else if( ! front.empty() )
|
hegyi@819
|
340 |
return P.gr->head(front[0]);
|
hegyi@819
|
341 |
else
|
hegyi@819
|
342 |
return INVALID;
|
hegyi@819
|
343 |
}
|
hegyi@819
|
344 |
|
hegyi@819
|
345 |
};
|
hegyi@819
|
346 |
|
hegyi@819
|
347 |
};
|
hegyi@819
|
348 |
|
hegyi@819
|
349 |
|
hegyi@819
|
350 |
|
hegyi@819
|
351 |
|
hegyi@819
|
352 |
|
hegyi@819
|
353 |
|
hegyi@819
|
354 |
|
hegyi@819
|
355 |
|
hegyi@819
|
356 |
|
hegyi@819
|
357 |
|
hegyi@819
|
358 |
/**********************************************************************/
|
hegyi@819
|
359 |
|
hegyi@819
|
360 |
|
hegyi@819
|
361 |
//! \brief A structure for representing undirected path in a graph.
|
hegyi@819
|
362 |
//!
|
hegyi@819
|
363 |
//! A structure for representing undirected path in a graph. Ie. this is
|
hegyi@819
|
364 |
//! a path in a \e directed graph but the edges should not be directed
|
hegyi@819
|
365 |
//! forward.
|
hegyi@819
|
366 |
//!
|
hegyi@819
|
367 |
//! \param Graph The graph type in which the path is.
|
hegyi@819
|
368 |
//! \param DM DebugMode, defaults to DefaultDebugMode.
|
hegyi@837
|
369 |
//!
|
hegyi@819
|
370 |
//! In a sense, the path can be treated as a graph, for is has \c NodeIt
|
hegyi@819
|
371 |
//! and \c EdgeIt with the same usage. These types converts to the \c Node
|
hegyi@819
|
372 |
//! and \c Edge of the original graph.
|
hegyi@819
|
373 |
//!
|
hegyi@819
|
374 |
//! \todo Thoroughfully check all the range and consistency tests.
|
hegyi@831
|
375 |
template<typename Graph>
|
hegyi@819
|
376 |
class UndirPath {
|
hegyi@819
|
377 |
public:
|
hegyi@819
|
378 |
/// Edge type of the underlying graph.
|
hegyi@819
|
379 |
typedef typename Graph::Edge GraphEdge;
|
hegyi@819
|
380 |
/// Node type of the underlying graph.
|
hegyi@819
|
381 |
typedef typename Graph::Node GraphNode;
|
hegyi@819
|
382 |
class NodeIt;
|
hegyi@819
|
383 |
class EdgeIt;
|
hegyi@819
|
384 |
|
hegyi@819
|
385 |
protected:
|
hegyi@819
|
386 |
const Graph *gr;
|
hegyi@819
|
387 |
typedef std::vector<GraphEdge> Container;
|
hegyi@819
|
388 |
Container edges;
|
hegyi@819
|
389 |
|
hegyi@819
|
390 |
public:
|
hegyi@819
|
391 |
|
hegyi@819
|
392 |
/// \param _G The graph in which the path is.
|
hegyi@819
|
393 |
///
|
hegyi@819
|
394 |
UndirPath(const Graph &_G) : gr(&_G) {}
|
hegyi@819
|
395 |
|
hegyi@819
|
396 |
/// \brief Subpath constructor.
|
hegyi@819
|
397 |
///
|
hegyi@819
|
398 |
/// Subpath defined by two nodes.
|
hegyi@819
|
399 |
/// \warning It is an error if the two edges are not in order!
|
hegyi@819
|
400 |
UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
|
hegyi@819
|
401 |
gr = P.gr;
|
hegyi@819
|
402 |
edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
|
hegyi@819
|
403 |
}
|
hegyi@819
|
404 |
|
hegyi@819
|
405 |
/// \brief Subpath constructor.
|
hegyi@819
|
406 |
///
|
hegyi@819
|
407 |
/// Subpath defined by two edges. Contains edges in [a,b)
|
hegyi@819
|
408 |
/// \warning It is an error if the two edges are not in order!
|
hegyi@819
|
409 |
UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
|
hegyi@819
|
410 |
gr = P.gr;
|
hegyi@819
|
411 |
edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
|
hegyi@819
|
412 |
}
|
hegyi@819
|
413 |
|
hegyi@819
|
414 |
/// Length of the path.
|
hegyi@819
|
415 |
size_t length() const { return edges.size(); }
|
hegyi@819
|
416 |
/// Returns whether the path is empty.
|
hegyi@819
|
417 |
bool empty() const { return edges.empty(); }
|
hegyi@819
|
418 |
|
hegyi@819
|
419 |
/// Resets the path to an empty path.
|
hegyi@819
|
420 |
void clear() { edges.clear(); }
|
hegyi@819
|
421 |
|
hegyi@819
|
422 |
/// \brief Starting point of the path.
|
hegyi@819
|
423 |
///
|
hegyi@819
|
424 |
/// Starting point of the path.
|
hegyi@819
|
425 |
/// Returns INVALID if the path is empty.
|
hegyi@831
|
426 |
GraphNode tail() const {
|
hegyi@819
|
427 |
return empty() ? INVALID : gr->tail(edges[0]);
|
hegyi@819
|
428 |
}
|
hegyi@819
|
429 |
/// \brief End point of the path.
|
hegyi@819
|
430 |
///
|
hegyi@819
|
431 |
/// End point of the path.
|
hegyi@819
|
432 |
/// Returns INVALID if the path is empty.
|
hegyi@831
|
433 |
GraphNode head() const {
|
hegyi@819
|
434 |
return empty() ? INVALID : gr->head(edges[length()-1]);
|
hegyi@819
|
435 |
}
|
hegyi@819
|
436 |
|
hegyi@819
|
437 |
/// \brief Initializes node or edge iterator to point to the first
|
hegyi@819
|
438 |
/// node or edge.
|
hegyi@819
|
439 |
///
|
hegyi@819
|
440 |
/// \sa nth
|
hegyi@819
|
441 |
template<typename It>
|
hegyi@819
|
442 |
It& first(It &i) const { return i=It(*this); }
|
hegyi@819
|
443 |
|
hegyi@819
|
444 |
/// \brief Initializes node iterator to point to the node of a given index.
|
hegyi@819
|
445 |
NodeIt& nth(NodeIt &i, int n) const {
|
hegyi@819
|
446 |
return i=NodeIt(*this, n);
|
hegyi@819
|
447 |
}
|
hegyi@819
|
448 |
|
hegyi@819
|
449 |
/// \brief Initializes edge iterator to point to the edge of a given index.
|
hegyi@819
|
450 |
EdgeIt& nth(EdgeIt &i, int n) const {
|
hegyi@819
|
451 |
return i=EdgeIt(*this, n);
|
hegyi@819
|
452 |
}
|
hegyi@819
|
453 |
|
hegyi@819
|
454 |
/// Checks validity of a node or edge iterator.
|
hegyi@819
|
455 |
template<typename It>
|
hegyi@819
|
456 |
static
|
hegyi@819
|
457 |
bool valid(const It &i) { return i.valid(); }
|
hegyi@819
|
458 |
|
hegyi@819
|
459 |
/// Steps the given node or edge iterator.
|
hegyi@819
|
460 |
template<typename It>
|
hegyi@819
|
461 |
static
|
hegyi@819
|
462 |
It& next(It &e) {
|
hegyi@819
|
463 |
return ++e;
|
hegyi@819
|
464 |
}
|
hegyi@819
|
465 |
|
hegyi@819
|
466 |
/// \brief Returns node iterator pointing to the head node of the
|
hegyi@819
|
467 |
/// given edge iterator.
|
hegyi@819
|
468 |
NodeIt head(const EdgeIt& e) const {
|
hegyi@819
|
469 |
return NodeIt(*this, e.idx+1);
|
hegyi@819
|
470 |
}
|
hegyi@819
|
471 |
|
hegyi@819
|
472 |
/// \brief Returns node iterator pointing to the tail node of the
|
hegyi@819
|
473 |
/// given edge iterator.
|
hegyi@819
|
474 |
NodeIt tail(const EdgeIt& e) const {
|
hegyi@819
|
475 |
return NodeIt(*this, e.idx);
|
hegyi@819
|
476 |
}
|
hegyi@819
|
477 |
|
hegyi@819
|
478 |
|
hegyi@819
|
479 |
|
hegyi@819
|
480 |
/**
|
hegyi@819
|
481 |
* \brief Iterator class to iterate on the edges of the paths
|
hegyi@837
|
482 |
*
|
hegyi@819
|
483 |
* \ingroup paths
|
hegyi@819
|
484 |
* This class is used to iterate on the edges of the paths
|
hegyi@819
|
485 |
*
|
hegyi@819
|
486 |
* Of course it converts to Graph::Edge
|
hegyi@837
|
487 |
*
|
hegyi@819
|
488 |
* \todo Its interface differs from the standard edge iterator.
|
hegyi@819
|
489 |
* Yes, it shouldn't.
|
hegyi@819
|
490 |
*/
|
hegyi@819
|
491 |
class EdgeIt {
|
hegyi@819
|
492 |
friend class UndirPath;
|
hegyi@819
|
493 |
|
hegyi@819
|
494 |
int idx;
|
hegyi@819
|
495 |
const UndirPath *p;
|
hegyi@819
|
496 |
public:
|
hegyi@819
|
497 |
/// Default constructor
|
hegyi@819
|
498 |
EdgeIt() {}
|
hegyi@819
|
499 |
/// Invalid constructor
|
hegyi@819
|
500 |
EdgeIt(Invalid) : idx(-1), p(0) {}
|
hegyi@819
|
501 |
/// Constructor with starting point
|
hegyi@819
|
502 |
EdgeIt(const UndirPath &_p, int _idx = 0) :
|
hegyi@819
|
503 |
idx(_idx), p(&_p) { validate(); }
|
hegyi@819
|
504 |
|
hegyi@819
|
505 |
///Validity check
|
hegyi@819
|
506 |
bool valid() const { return idx!=-1; }
|
hegyi@819
|
507 |
|
hegyi@819
|
508 |
///Conversion to Graph::Edge
|
hegyi@819
|
509 |
operator GraphEdge () const {
|
hegyi@819
|
510 |
return valid() ? p->edges[idx] : INVALID;
|
hegyi@819
|
511 |
}
|
hegyi@819
|
512 |
/// Next edge
|
hegyi@819
|
513 |
EdgeIt& operator++() { ++idx; validate(); return *this; }
|
hegyi@819
|
514 |
|
hegyi@819
|
515 |
/// Comparison operator
|
hegyi@819
|
516 |
bool operator==(const EdgeIt& e) const { return idx==e.idx; }
|
hegyi@819
|
517 |
/// Comparison operator
|
hegyi@819
|
518 |
bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
|
hegyi@819
|
519 |
/// Comparison operator
|
hegyi@819
|
520 |
bool operator<(const EdgeIt& e) const { return idx<e.idx; }
|
hegyi@819
|
521 |
|
hegyi@819
|
522 |
private:
|
hegyi@819
|
523 |
// FIXME: comparison between signed and unsigned...
|
hegyi@819
|
524 |
// Jo ez igy? Vagy esetleg legyen a length() int?
|
hegyi@819
|
525 |
void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
|
hegyi@819
|
526 |
};
|
hegyi@819
|
527 |
|
hegyi@819
|
528 |
/**
|
hegyi@819
|
529 |
* \brief Iterator class to iterate on the nodes of the paths
|
hegyi@837
|
530 |
*
|
hegyi@819
|
531 |
* \ingroup paths
|
hegyi@819
|
532 |
* This class is used to iterate on the nodes of the paths
|
hegyi@819
|
533 |
*
|
hegyi@819
|
534 |
* Of course it converts to Graph::Node
|
hegyi@837
|
535 |
*
|
hegyi@819
|
536 |
* \todo Its interface differs from the standard node iterator.
|
hegyi@819
|
537 |
* Yes, it shouldn't.
|
hegyi@819
|
538 |
*/
|
hegyi@819
|
539 |
class NodeIt {
|
hegyi@819
|
540 |
friend class UndirPath;
|
hegyi@819
|
541 |
|
hegyi@819
|
542 |
int idx;
|
hegyi@819
|
543 |
const UndirPath *p;
|
hegyi@819
|
544 |
public:
|
hegyi@819
|
545 |
/// Default constructor
|
hegyi@819
|
546 |
NodeIt() {}
|
hegyi@819
|
547 |
/// Invalid constructor
|
hegyi@819
|
548 |
NodeIt(Invalid) : idx(-1), p(0) {}
|
hegyi@819
|
549 |
/// Constructor with starting point
|
hegyi@819
|
550 |
NodeIt(const UndirPath &_p, int _idx = 0) :
|
hegyi@819
|
551 |
idx(_idx), p(&_p) { validate(); }
|
hegyi@819
|
552 |
|
hegyi@819
|
553 |
///Validity check
|
hegyi@819
|
554 |
bool valid() const { return idx!=-1; }
|
hegyi@819
|
555 |
|
hegyi@819
|
556 |
///Conversion to Graph::Node
|
hegyi@819
|
557 |
operator const GraphNode& () const {
|
hegyi@819
|
558 |
if(idx >= p->length())
|
hegyi@831
|
559 |
return p->head();
|
hegyi@819
|
560 |
else if(idx >= 0)
|
hegyi@819
|
561 |
return p->gr->tail(p->edges[idx]);
|
hegyi@819
|
562 |
else
|
hegyi@819
|
563 |
return INVALID;
|
hegyi@819
|
564 |
}
|
hegyi@819
|
565 |
/// Next node
|
hegyi@819
|
566 |
NodeIt& operator++() { ++idx; validate(); return *this; }
|
hegyi@819
|
567 |
|
hegyi@819
|
568 |
/// Comparison operator
|
hegyi@819
|
569 |
bool operator==(const NodeIt& e) const { return idx==e.idx; }
|
hegyi@819
|
570 |
/// Comparison operator
|
hegyi@819
|
571 |
bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
|
hegyi@819
|
572 |
/// Comparison operator
|
hegyi@819
|
573 |
bool operator<(const NodeIt& e) const { return idx<e.idx; }
|
hegyi@819
|
574 |
|
hegyi@819
|
575 |
private:
|
hegyi@819
|
576 |
void validate() { if( size_t(idx) > p->length() ) idx=-1; }
|
hegyi@819
|
577 |
};
|
hegyi@819
|
578 |
|
hegyi@837
|
579 |
friend class Builder;
|
hegyi@819
|
580 |
|
hegyi@819
|
581 |
/**
|
hegyi@819
|
582 |
* \brief Class to build paths
|
hegyi@837
|
583 |
*
|
hegyi@819
|
584 |
* \ingroup paths
|
hegyi@819
|
585 |
* This class is used to fill a path with edges.
|
hegyi@819
|
586 |
*
|
hegyi@819
|
587 |
* You can push new edges to the front and to the back of the path in
|
hegyi@819
|
588 |
* arbitrary order then you should commit these changes to the graph.
|
hegyi@819
|
589 |
*
|
hegyi@819
|
590 |
* Fundamentally, for most "Paths" (classes fulfilling the
|
hegyi@819
|
591 |
* PathConcept) while the builder is active (after the first modifying
|
hegyi@819
|
592 |
* operation and until the commit()) the original Path is in a
|
hegyi@819
|
593 |
* "transitional" state (operations ot it have undefined result). But
|
hegyi@819
|
594 |
* in the case of UndirPath the original path is unchanged until the
|
hegyi@819
|
595 |
* commit. However we don't recomend that you use this feature.
|
hegyi@819
|
596 |
*/
|
hegyi@819
|
597 |
class Builder {
|
hegyi@819
|
598 |
UndirPath &P;
|
hegyi@819
|
599 |
Container front, back;
|
hegyi@819
|
600 |
|
hegyi@819
|
601 |
public:
|
hegyi@819
|
602 |
///\param _P the path you want to fill in.
|
hegyi@819
|
603 |
///
|
hegyi@819
|
604 |
Builder(UndirPath &_P) : P(_P) {}
|
hegyi@819
|
605 |
|
hegyi@819
|
606 |
/// Sets the starting node of the path.
|
hegyi@837
|
607 |
|
hegyi@819
|
608 |
/// Sets the starting node of the path. Edge added to the path
|
hegyi@819
|
609 |
/// afterwards have to be incident to this node.
|
alpar@900
|
610 |
/// It should be called if and only if
|
alpar@900
|
611 |
/// the path is empty and before any call to
|
hegyi@819
|
612 |
/// \ref pushFront() or \ref pushBack()
|
hegyi@819
|
613 |
void setStartNode(const GraphNode &) {}
|
hegyi@819
|
614 |
|
hegyi@819
|
615 |
///Push a new edge to the front of the path
|
hegyi@819
|
616 |
|
hegyi@819
|
617 |
///Push a new edge to the front of the path.
|
hegyi@819
|
618 |
///\sa setStartNode
|
hegyi@819
|
619 |
void pushFront(const GraphEdge& e) {
|
hegyi@819
|
620 |
front.push_back(e);
|
hegyi@819
|
621 |
}
|
hegyi@819
|
622 |
|
hegyi@819
|
623 |
///Push a new edge to the back of the path
|
hegyi@819
|
624 |
|
hegyi@819
|
625 |
///Push a new edge to the back of the path.
|
hegyi@819
|
626 |
///\sa setStartNode
|
hegyi@819
|
627 |
void pushBack(const GraphEdge& e) {
|
hegyi@819
|
628 |
back.push_back(e);
|
hegyi@819
|
629 |
}
|
hegyi@819
|
630 |
|
hegyi@819
|
631 |
///Commit the changes to the path.
|
hegyi@819
|
632 |
void commit() {
|
hegyi@819
|
633 |
if( !(front.empty() && back.empty()) ) {
|
hegyi@819
|
634 |
Container tmp;
|
hegyi@819
|
635 |
tmp.reserve(front.size()+back.size()+P.length());
|
hegyi@819
|
636 |
tmp.insert(tmp.end(), front.rbegin(), front.rend());
|
hegyi@819
|
637 |
tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
|
hegyi@819
|
638 |
tmp.insert(tmp.end(), back.begin(), back.end());
|
hegyi@819
|
639 |
P.edges.swap(tmp);
|
hegyi@819
|
640 |
front.clear();
|
hegyi@819
|
641 |
back.clear();
|
hegyi@819
|
642 |
}
|
hegyi@819
|
643 |
}
|
hegyi@819
|
644 |
|
hegyi@819
|
645 |
|
hegyi@819
|
646 |
///Reserve storage for the builder in advance.
|
hegyi@819
|
647 |
|
hegyi@837
|
648 |
///If you know a reasonable upper bound of the number of the edges
|
hegyi@837
|
649 |
///to add to the front, using this function you can speed up the building.
|
hegyi@819
|
650 |
|
hegyi@837
|
651 |
void reserveFront(size_t r) {front.reserve(r);}
|
hegyi@837
|
652 |
|
hegyi@837
|
653 |
///Reserve storage for the builder in advance.
|
hegyi@837
|
654 |
|
hegyi@837
|
655 |
///If you know a reasonable upper bound of the number of the edges
|
hegyi@837
|
656 |
///to add to the back, using this function you can speed up the building.
|
hegyi@837
|
657 |
|
hegyi@837
|
658 |
void reserveBack(size_t r) {back.reserve(r);}
|
hegyi@831
|
659 |
|
hegyi@819
|
660 |
private:
|
hegyi@819
|
661 |
bool empty() {
|
hegyi@819
|
662 |
return front.empty() && back.empty() && P.empty();
|
hegyi@819
|
663 |
}
|
hegyi@819
|
664 |
|
hegyi@831
|
665 |
GraphNode tail() const {
|
hegyi@819
|
666 |
if( ! front.empty() )
|
hegyi@819
|
667 |
return P.gr->tail(front[front.size()-1]);
|
hegyi@819
|
668 |
else if( ! P.empty() )
|
hegyi@819
|
669 |
return P.gr->tail(P.edges[0]);
|
hegyi@819
|
670 |
else if( ! back.empty() )
|
hegyi@819
|
671 |
return P.gr->tail(back[0]);
|
hegyi@819
|
672 |
else
|
hegyi@819
|
673 |
return INVALID;
|
hegyi@819
|
674 |
}
|
hegyi@831
|
675 |
GraphNode head() const {
|
hegyi@819
|
676 |
if( ! back.empty() )
|
hegyi@819
|
677 |
return P.gr->head(back[back.size()-1]);
|
hegyi@819
|
678 |
else if( ! P.empty() )
|
hegyi@819
|
679 |
return P.gr->head(P.edges[P.length()-1]);
|
hegyi@819
|
680 |
else if( ! front.empty() )
|
hegyi@819
|
681 |
return P.gr->head(front[0]);
|
hegyi@819
|
682 |
else
|
hegyi@819
|
683 |
return INVALID;
|
hegyi@819
|
684 |
}
|
hegyi@819
|
685 |
|
hegyi@819
|
686 |
};
|
hegyi@819
|
687 |
|
hegyi@819
|
688 |
};
|
hegyi@819
|
689 |
|
hegyi@819
|
690 |
|
hegyi@819
|
691 |
///@}
|
hegyi@819
|
692 |
|
hegyi@819
|
693 |
} // namespace hugo
|
hegyi@819
|
694 |
|
hegyi@819
|
695 |
#endif // HUGO_PATH_H
|