Some commenticide was applied.
4 @defgroup paths Path Structures
6 \brief Path structures implemented in Hugo.
8 Hugolib provides flexible data structures
11 All of them have the same interface, especially they can be built or extended
12 using a standard Builder subclass. This make is easy to have e.g. the Dijkstra
13 algorithm to store its result in any kind of path structure.
15 \sa hugo::skeleton::Path
21 ///\brief Classes for representing paths in graphs.
30 #include <hugo/invalid.h>
38 //! \brief A structure for representing directed paths in a graph.
40 //! A structure for representing directed path in a graph.
41 //! \param Graph The graph type in which the path is.
42 //! \param DM DebugMode, defaults to DefaultDebugMode.
44 //! In a sense, the path can be treated as a graph, for is has \c NodeIt
45 //! and \c EdgeIt with the same usage. These types converts to the \c Node
46 //! and \c Edge of the original graph.
48 //! \todo Thoroughfully check all the range and consistency tests.
49 template<typename Graph>
52 /// Edge type of the underlying graph.
53 typedef typename Graph::Edge GraphEdge;
54 /// Node type of the underlying graph.
55 typedef typename Graph::Node GraphNode;
61 typedef std::vector<GraphEdge> Container;
66 /// \param _G The graph in which the path is.
68 DirPath(const Graph &_G) : gr(&_G) {}
70 /// \brief Subpath constructor.
72 /// Subpath defined by two nodes.
73 /// \warning It is an error if the two edges are not in order!
74 DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
76 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
79 /// \brief Subpath constructor.
81 /// Subpath defined by two edges. Contains edges in [a,b)
82 /// \warning It is an error if the two edges are not in order!
83 DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
85 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
88 /// Length of the path.
89 size_t length() const { return edges.size(); }
90 /// Returns whether the path is empty.
91 bool empty() const { return edges.empty(); }
93 /// Resets the path to an empty path.
94 void clear() { edges.clear(); }
96 /// \brief Starting point of the path.
98 /// Starting point of the path.
99 /// Returns INVALID if the path is empty.
100 GraphNode tail() const {
101 return empty() ? INVALID : gr->tail(edges[0]);
103 /// \brief End point of the path.
105 /// End point of the path.
106 /// Returns INVALID if the path is empty.
107 GraphNode head() const {
108 return empty() ? INVALID : gr->head(edges[length()-1]);
111 /// \brief Initializes node or edge iterator to point to the first
115 template<typename It>
116 It& first(It &i) const { return i=It(*this); }
118 /// \brief Initializes node iterator to point to the node of a given index.
119 NodeIt& nth(NodeIt &i, int n) const {
120 return i=NodeIt(*this, n);
123 /// \brief Initializes edge iterator to point to the edge of a given index.
124 EdgeIt& nth(EdgeIt &i, int n) const {
125 return i=EdgeIt(*this, n);
128 /// \brief Returns node iterator pointing to the head node of the
129 /// given edge iterator.
130 NodeIt head(const EdgeIt& e) const {
131 return NodeIt(*this, e.idx+1);
134 /// \brief Returns node iterator pointing to the tail node of the
135 /// given edge iterator.
136 NodeIt tail(const EdgeIt& e) const {
137 return NodeIt(*this, e.idx);
141 /* Iterator classes */
144 * \brief Iterator class to iterate on the edges of the paths
147 * This class is used to iterate on the edges of the paths
149 * Of course it converts to Graph::Edge
153 friend class DirPath;
158 /// Default constructor
160 /// Invalid constructor
161 EdgeIt(Invalid) : idx(-1), p(0) {}
162 /// Constructor with starting point
163 EdgeIt(const DirPath &_p, int _idx = 0) :
164 idx(_idx), p(&_p) { validate(); }
167 bool valid() const { return idx!=-1; }
169 ///Conversion to Graph::Edge
170 operator GraphEdge () const {
171 return valid() ? p->edges[idx] : INVALID;
175 EdgeIt& operator++() { ++idx; validate(); return *this; }
177 /// Comparison operator
178 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
179 /// Comparison operator
180 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
181 /// Comparison operator
182 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
185 // FIXME: comparison between signed and unsigned...
186 // Jo ez igy? Vagy esetleg legyen a length() int?
187 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
191 * \brief Iterator class to iterate on the nodes of the paths
194 * This class is used to iterate on the nodes of the paths
196 * Of course it converts to Graph::Node
200 friend class DirPath;
205 /// Default constructor
207 /// Invalid constructor
208 NodeIt(Invalid) : idx(-1), p(0) {}
209 /// Constructor with starting point
210 NodeIt(const DirPath &_p, int _idx = 0) :
211 idx(_idx), p(&_p) { validate(); }
214 bool valid() const { return idx!=-1; }
216 ///Conversion to Graph::Node
217 operator const GraphNode& () const {
218 if(idx >= p->length())
221 return p->gr->tail(p->edges[idx]);
226 NodeIt& operator++() { ++idx; validate(); return *this; }
228 /// Comparison operator
229 bool operator==(const NodeIt& e) const { return idx==e.idx; }
230 /// Comparison operator
231 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
232 /// Comparison operator
233 bool operator<(const NodeIt& e) const { return idx<e.idx; }
236 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
239 friend class Builder;
242 * \brief Class to build paths
245 * This class is used to fill a path with edges.
247 * You can push new edges to the front and to the back of the path in
248 * arbitrary order then you should commit these changes to the graph.
250 * Fundamentally, for most "Paths" (classes fulfilling the
251 * PathConcept) while the builder is active (after the first modifying
252 * operation and until the commit()) the original Path is in a
253 * "transitional" state (operations on it have undefined result). But
254 * in the case of DirPath the original path remains unchanged until the
255 * commit. However we don't recomend that you use this feature.
259 Container front, back;
262 ///\param _P the path you want to fill in.
264 Builder(DirPath &_P) : P(_P) {}
266 /// Sets the starting node of the path.
268 /// Sets the starting node of the path. Edge added to the path
269 /// afterwards have to be incident to this node.
270 /// It should be called if and only if
271 /// the path is empty and before any call to
272 /// \ref pushFront() or \ref pushBack()
273 void setStartNode(const GraphNode &) {}
275 ///Push a new edge to the front of the path
277 ///Push a new edge to the front of the path.
279 void pushFront(const GraphEdge& e) {
283 ///Push a new edge to the back of the path
285 ///Push a new edge to the back of the path.
287 void pushBack(const GraphEdge& e) {
291 ///Commit the changes to the path.
293 if( !front.empty() || !back.empty() ) {
295 tmp.reserve(front.size()+back.size()+P.length());
296 tmp.insert(tmp.end(), front.rbegin(), front.rend());
297 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
298 tmp.insert(tmp.end(), back.begin(), back.end());
305 ///Reserve storage for the builder in advance.
307 ///If you know a reasonable upper bound of the number of the edges
308 ///to add to the front, using this function you can speed up the building.
310 void reserveFront(size_t r) {front.reserve(r);}
312 ///Reserve storage for the builder in advance.
314 ///If you know a reasonable upper bound of the number of the edges
315 ///to add to the back, using this function you can speed up the building.
317 void reserveBack(size_t r) {back.reserve(r);}
321 return front.empty() && back.empty() && P.empty();
324 GraphNode tail() const {
325 if( ! front.empty() )
326 return P.gr->tail(front[front.size()-1]);
327 else if( ! P.empty() )
328 return P.gr->tail(P.edges[0]);
329 else if( ! back.empty() )
330 return P.gr->tail(back[0]);
334 GraphNode head() const {
336 return P.gr->head(back[back.size()-1]);
337 else if( ! P.empty() )
338 return P.gr->head(P.edges[P.length()-1]);
339 else if( ! front.empty() )
340 return P.gr->head(front[0]);
358 /**********************************************************************/
361 //! \brief A structure for representing undirected path in a graph.
363 //! A structure for representing undirected path in a graph. Ie. this is
364 //! a path in a \e directed graph but the edges should not be directed
367 //! \param Graph The graph type in which the path is.
368 //! \param DM DebugMode, defaults to DefaultDebugMode.
370 //! In a sense, the path can be treated as a graph, for is has \c NodeIt
371 //! and \c EdgeIt with the same usage. These types converts to the \c Node
372 //! and \c Edge of the original graph.
374 //! \todo Thoroughfully check all the range and consistency tests.
375 template<typename Graph>
378 /// Edge type of the underlying graph.
379 typedef typename Graph::Edge GraphEdge;
380 /// Node type of the underlying graph.
381 typedef typename Graph::Node GraphNode;
387 typedef std::vector<GraphEdge> Container;
392 /// \param _G The graph in which the path is.
394 UndirPath(const Graph &_G) : gr(&_G) {}
396 /// \brief Subpath constructor.
398 /// Subpath defined by two nodes.
399 /// \warning It is an error if the two edges are not in order!
400 UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
402 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
405 /// \brief Subpath constructor.
407 /// Subpath defined by two edges. Contains edges in [a,b)
408 /// \warning It is an error if the two edges are not in order!
409 UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
411 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
414 /// Length of the path.
415 size_t length() const { return edges.size(); }
416 /// Returns whether the path is empty.
417 bool empty() const { return edges.empty(); }
419 /// Resets the path to an empty path.
420 void clear() { edges.clear(); }
422 /// \brief Starting point of the path.
424 /// Starting point of the path.
425 /// Returns INVALID if the path is empty.
426 GraphNode tail() const {
427 return empty() ? INVALID : gr->tail(edges[0]);
429 /// \brief End point of the path.
431 /// End point of the path.
432 /// Returns INVALID if the path is empty.
433 GraphNode head() const {
434 return empty() ? INVALID : gr->head(edges[length()-1]);
437 /// \brief Initializes node or edge iterator to point to the first
441 template<typename It>
442 It& first(It &i) const { return i=It(*this); }
444 /// \brief Initializes node iterator to point to the node of a given index.
445 NodeIt& nth(NodeIt &i, int n) const {
446 return i=NodeIt(*this, n);
449 /// \brief Initializes edge iterator to point to the edge of a given index.
450 EdgeIt& nth(EdgeIt &i, int n) const {
451 return i=EdgeIt(*this, n);
454 /// Checks validity of a node or edge iterator.
455 template<typename It>
457 bool valid(const It &i) { return i.valid(); }
459 /// Steps the given node or edge iterator.
460 template<typename It>
466 /// \brief Returns node iterator pointing to the head node of the
467 /// given edge iterator.
468 NodeIt head(const EdgeIt& e) const {
469 return NodeIt(*this, e.idx+1);
472 /// \brief Returns node iterator pointing to the tail node of the
473 /// given edge iterator.
474 NodeIt tail(const EdgeIt& e) const {
475 return NodeIt(*this, e.idx);
481 * \brief Iterator class to iterate on the edges of the paths
484 * This class is used to iterate on the edges of the paths
486 * Of course it converts to Graph::Edge
488 * \todo Its interface differs from the standard edge iterator.
492 friend class UndirPath;
497 /// Default constructor
499 /// Invalid constructor
500 EdgeIt(Invalid) : idx(-1), p(0) {}
501 /// Constructor with starting point
502 EdgeIt(const UndirPath &_p, int _idx = 0) :
503 idx(_idx), p(&_p) { validate(); }
506 bool valid() const { return idx!=-1; }
508 ///Conversion to Graph::Edge
509 operator GraphEdge () const {
510 return valid() ? p->edges[idx] : INVALID;
513 EdgeIt& operator++() { ++idx; validate(); return *this; }
515 /// Comparison operator
516 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
517 /// Comparison operator
518 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
519 /// Comparison operator
520 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
523 // FIXME: comparison between signed and unsigned...
524 // Jo ez igy? Vagy esetleg legyen a length() int?
525 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
529 * \brief Iterator class to iterate on the nodes of the paths
532 * This class is used to iterate on the nodes of the paths
534 * Of course it converts to Graph::Node
536 * \todo Its interface differs from the standard node iterator.
540 friend class UndirPath;
545 /// Default constructor
547 /// Invalid constructor
548 NodeIt(Invalid) : idx(-1), p(0) {}
549 /// Constructor with starting point
550 NodeIt(const UndirPath &_p, int _idx = 0) :
551 idx(_idx), p(&_p) { validate(); }
554 bool valid() const { return idx!=-1; }
556 ///Conversion to Graph::Node
557 operator const GraphNode& () const {
558 if(idx >= p->length())
561 return p->gr->tail(p->edges[idx]);
566 NodeIt& operator++() { ++idx; validate(); return *this; }
568 /// Comparison operator
569 bool operator==(const NodeIt& e) const { return idx==e.idx; }
570 /// Comparison operator
571 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
572 /// Comparison operator
573 bool operator<(const NodeIt& e) const { return idx<e.idx; }
576 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
579 friend class Builder;
582 * \brief Class to build paths
585 * This class is used to fill a path with edges.
587 * You can push new edges to the front and to the back of the path in
588 * arbitrary order then you should commit these changes to the graph.
590 * Fundamentally, for most "Paths" (classes fulfilling the
591 * PathConcept) while the builder is active (after the first modifying
592 * operation and until the commit()) the original Path is in a
593 * "transitional" state (operations ot it have undefined result). But
594 * in the case of UndirPath the original path is unchanged until the
595 * commit. However we don't recomend that you use this feature.
599 Container front, back;
602 ///\param _P the path you want to fill in.
604 Builder(UndirPath &_P) : P(_P) {}
606 /// Sets the starting node of the path.
608 /// Sets the starting node of the path. Edge added to the path
609 /// afterwards have to be incident to this node.
610 /// It should be called if and only if
611 /// the path is empty and before any call to
612 /// \ref pushFront() or \ref pushBack()
613 void setStartNode(const GraphNode &) {}
615 ///Push a new edge to the front of the path
617 ///Push a new edge to the front of the path.
619 void pushFront(const GraphEdge& e) {
623 ///Push a new edge to the back of the path
625 ///Push a new edge to the back of the path.
627 void pushBack(const GraphEdge& e) {
631 ///Commit the changes to the path.
633 if( !(front.empty() && back.empty()) ) {
635 tmp.reserve(front.size()+back.size()+P.length());
636 tmp.insert(tmp.end(), front.rbegin(), front.rend());
637 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
638 tmp.insert(tmp.end(), back.begin(), back.end());
646 ///Reserve storage for the builder in advance.
648 ///If you know a reasonable upper bound of the number of the edges
649 ///to add to the front, using this function you can speed up the building.
651 void reserveFront(size_t r) {front.reserve(r);}
653 ///Reserve storage for the builder in advance.
655 ///If you know a reasonable upper bound of the number of the edges
656 ///to add to the back, using this function you can speed up the building.
658 void reserveBack(size_t r) {back.reserve(r);}
662 return front.empty() && back.empty() && P.empty();
665 GraphNode tail() const {
666 if( ! front.empty() )
667 return P.gr->tail(front[front.size()-1]);
668 else if( ! P.empty() )
669 return P.gr->tail(P.edges[0]);
670 else if( ! back.empty() )
671 return P.gr->tail(back[0]);
675 GraphNode head() const {
677 return P.gr->head(back[back.size()-1]);
678 else if( ! P.empty() )
679 return P.gr->head(P.edges[P.length()-1]);
680 else if( ! front.empty() )
681 return P.gr->head(front[0]);
695 #endif // HUGO_PATH_H