The first version of new path test program. The old became old_path_test.
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>
31 #include <hugo/error.h>
32 //#include <hugo/debug.h>
40 //! \brief A structure for representing directed paths in a graph.
42 //! A structure for representing directed path in a graph.
43 //! \param Graph The graph type in which the path is.
44 //! \param DM DebugMode, defaults to DefaultDebugMode.
46 //! In a sense, the path can be treated as a graph, for is has \c NodeIt
47 //! and \c EdgeIt with the same usage. These types converts to the \c Node
48 //! and \c Edge of the original graph.
50 //! \todo Thoroughfully check all the range and consistency tests.
51 template<typename Graph>
54 /// Edge type of the underlying graph.
55 typedef typename Graph::Edge GraphEdge;
56 /// Node type of the underlying graph.
57 typedef typename Graph::Node GraphNode;
63 typedef std::vector<GraphEdge> Container;
68 /// \param _G The graph in which the path is.
70 DirPath(const Graph &_G) : gr(&_G) {}
72 /// \brief Subpath constructor.
74 /// Subpath defined by two nodes.
75 /// \warning It is an error if the two edges are not in order!
76 DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
77 if(!a.valid() || !b.valid) {
78 // FIXME: this check should be more elaborate...
79 fault("DirPath, subpath ctor: invalid bounding nodes");
82 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
85 /// \brief Subpath constructor.
87 /// Subpath defined by two edges. Contains edges in [a,b)
88 /// \warning It is an error if the two edges are not in order!
89 DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
90 if (!a.valid() || !b.valid) {
91 // FIXME: this check should be more elaborate...
92 fault("DirPath, subpath ctor: invalid bounding nodes");
95 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
98 /// Length of the path.
99 size_t length() const { return edges.size(); }
100 /// Returns whether the path is empty.
101 bool empty() const { return edges.empty(); }
103 /// Resets the path to an empty path.
104 void clear() { edges.clear(); }
106 /// \brief Starting point of the path.
108 /// Starting point of the path.
109 /// Returns INVALID if the path is empty.
110 GraphNode tail() const {
111 return empty() ? INVALID : gr->tail(edges[0]);
113 /// \brief End point of the path.
115 /// End point of the path.
116 /// Returns INVALID if the path is empty.
117 GraphNode head() const {
118 return empty() ? INVALID : gr->head(edges[length()-1]);
121 /// \brief Initializes node or edge iterator to point to the first
125 template<typename It>
126 It& first(It &i) const { return i=It(*this); }
128 /// \brief Initializes node iterator to point to the node of a given index.
129 NodeIt& nth(NodeIt &i, int n) const {
130 if(n<0 || n>int(length()))
131 fault("DirPath::nth: index out of range");
132 return i=NodeIt(*this, n);
135 /// \brief Initializes edge iterator to point to the edge of a given index.
136 EdgeIt& nth(EdgeIt &i, int n) const {
137 if(n<0 || n>=int(length()))
138 fault("DirPath::nth: index out of range");
139 return i=EdgeIt(*this, n);
142 /// Checks validity of a node or edge iterator.
143 template<typename It>
145 bool valid(const It &i) { return i.valid(); }
147 /// Steps the given node or edge iterator.
148 template<typename It>
152 fault("DirPath::next() on invalid iterator");
156 /// \brief Returns node iterator pointing to the head node of the
157 /// given edge iterator.
158 NodeIt head(const EdgeIt& e) const {
160 fault("DirPath::head() on invalid iterator");
161 return NodeIt(*this, e.idx+1);
164 /// \brief Returns node iterator pointing to the tail node of the
165 /// given edge iterator.
166 NodeIt tail(const EdgeIt& e) const {
168 fault("DirPath::tail() on invalid iterator");
169 return NodeIt(*this, e.idx);
173 /* Iterator classes */
176 * \brief Iterator class to iterate on the edges of the paths
179 * This class is used to iterate on the edges of the paths
181 * Of course it converts to Graph::Edge
183 * \todo Its interface differs from the standard edge iterator.
187 friend class DirPath;
192 /// Default constructor
194 /// Invalid constructor
195 EdgeIt(Invalid) : idx(-1), p(0) {}
196 /// Constructor with starting point
197 EdgeIt(const DirPath &_p, int _idx = 0) :
198 idx(_idx), p(&_p) { validate(); }
201 bool valid() const { return idx!=-1; }
203 ///Conversion to Graph::Edge
204 operator GraphEdge () const {
205 return valid() ? p->edges[idx] : INVALID;
209 EdgeIt& operator++() { ++idx; validate(); return *this; }
211 /// Comparison operator
212 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
213 /// Comparison operator
214 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
215 /// Comparison operator
216 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
219 // FIXME: comparison between signed and unsigned...
220 // Jo ez igy? Vagy esetleg legyen a length() int?
221 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
225 * \brief Iterator class to iterate on the nodes of the paths
228 * This class is used to iterate on the nodes of the paths
230 * Of course it converts to Graph::Node
232 * \todo Its interface differs from the standard node iterator.
236 friend class DirPath;
241 /// Default constructor
243 /// Invalid constructor
244 NodeIt(Invalid) : idx(-1), p(0) {}
245 /// Constructor with starting point
246 NodeIt(const DirPath &_p, int _idx = 0) :
247 idx(_idx), p(&_p) { validate(); }
250 bool valid() const { return idx!=-1; }
252 ///Conversion to Graph::Node
253 operator const GraphNode& () const {
254 if(idx >= p->length())
257 return p->gr->tail(p->edges[idx]);
262 NodeIt& operator++() { ++idx; validate(); return *this; }
264 /// Comparison operator
265 bool operator==(const NodeIt& e) const { return idx==e.idx; }
266 /// Comparison operator
267 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
268 /// Comparison operator
269 bool operator<(const NodeIt& e) const { return idx<e.idx; }
272 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
275 friend class Builder;
278 * \brief Class to build paths
281 * This class is used to fill a path with edges.
283 * You can push new edges to the front and to the back of the path in
284 * arbitrary order then you should commit these changes to the graph.
286 * Fundamentally, for most "Paths" (classes fulfilling the
287 * PathConcept) while the builder is active (after the first modifying
288 * operation and until the commit()) the original Path is in a
289 * "transitional" state (operations on it have undefined result). But
290 * in the case of DirPath the original path remains unchanged until the
291 * commit. However we don't recomend that you use this feature.
295 Container front, back;
298 ///\param _P the path you want to fill in.
300 Builder(DirPath &_P) : P(_P) {}
302 /// Sets the starting node of the path.
304 /// Sets the starting node of the path. Edge added to the path
305 /// afterwards have to be incident to this node.
306 /// It should be called iff the path is empty and before any call to
307 /// \ref pushFront() or \ref pushBack()
308 void setStartNode(const GraphNode &) {}
310 ///Push a new edge to the front of the path
312 ///Push a new edge to the front of the path.
314 void pushFront(const GraphEdge& e) {
315 if( !empty() && P.gr->head(e)!=tail() ) {
316 fault("DirPath::Builder::pushFront: nonincident edge");
321 ///Push a new edge to the back of the path
323 ///Push a new edge to the back of the path.
325 void pushBack(const GraphEdge& e) {
326 if( !empty() && P.gr->tail(e)!=head() ) {
327 fault("DirPath::Builder::pushBack: nonincident edge");
332 ///Commit the changes to the path.
334 if( !(front.empty() && back.empty()) ) {
336 tmp.reserve(front.size()+back.size()+P.length());
337 tmp.insert(tmp.end(), front.rbegin(), front.rend());
338 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
339 tmp.insert(tmp.end(), back.begin(), back.end());
346 // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
347 // Hogy kenyelmes egy ilyet hasznalni?
349 ///Reserve storage for the builder in advance.
351 ///If you know an reasonable upper bound of the number of the edges
352 ///to add, using this function you can speed up the building.
353 void reserve(size_t r) {
358 void reserveFront(size_t r) {}
359 void reserveBack(size_t r) {}
363 return front.empty() && back.empty() && P.empty();
366 GraphNode tail() const {
367 if( ! front.empty() )
368 return P.gr->tail(front[front.size()-1]);
369 else if( ! P.empty() )
370 return P.gr->tail(P.edges[0]);
371 else if( ! back.empty() )
372 return P.gr->tail(back[0]);
376 GraphNode head() const {
378 return P.gr->head(back[back.size()-1]);
379 else if( ! P.empty() )
380 return P.gr->head(P.edges[P.length()-1]);
381 else if( ! front.empty() )
382 return P.gr->head(front[0]);
400 /**********************************************************************/
403 //! \brief A structure for representing undirected path in a graph.
405 //! A structure for representing undirected path in a graph. Ie. this is
406 //! a path in a \e directed graph but the edges should not be directed
409 //! \param Graph The graph type in which the path is.
410 //! \param DM DebugMode, defaults to DefaultDebugMode.
412 //! In a sense, the path can be treated as a graph, for is has \c NodeIt
413 //! and \c EdgeIt with the same usage. These types converts to the \c Node
414 //! and \c Edge of the original graph.
416 //! \todo Thoroughfully check all the range and consistency tests.
417 template<typename Graph>
420 /// Edge type of the underlying graph.
421 typedef typename Graph::Edge GraphEdge;
422 /// Node type of the underlying graph.
423 typedef typename Graph::Node GraphNode;
429 typedef std::vector<GraphEdge> Container;
434 /// \param _G The graph in which the path is.
436 UndirPath(const Graph &_G) : gr(&_G) {}
438 /// \brief Subpath constructor.
440 /// Subpath defined by two nodes.
441 /// \warning It is an error if the two edges are not in order!
442 UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
443 if(!a.valid() || !b.valid) {
444 // FIXME: this check should be more elaborate...
445 fault("UndirPath, subpath ctor: invalid bounding nodes");
448 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
451 /// \brief Subpath constructor.
453 /// Subpath defined by two edges. Contains edges in [a,b)
454 /// \warning It is an error if the two edges are not in order!
455 UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
456 if(!a.valid() || !b.valid) {
457 // FIXME: this check should be more elaborate...
458 fault("UndirPath, subpath ctor: invalid bounding nodes");
461 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
464 /// Length of the path.
465 size_t length() const { return edges.size(); }
466 /// Returns whether the path is empty.
467 bool empty() const { return edges.empty(); }
469 /// Resets the path to an empty path.
470 void clear() { edges.clear(); }
472 /// \brief Starting point of the path.
474 /// Starting point of the path.
475 /// Returns INVALID if the path is empty.
476 GraphNode tail() const {
477 return empty() ? INVALID : gr->tail(edges[0]);
479 /// \brief End point of the path.
481 /// End point of the path.
482 /// Returns INVALID if the path is empty.
483 GraphNode head() const {
484 return empty() ? INVALID : gr->head(edges[length()-1]);
487 /// \brief Initializes node or edge iterator to point to the first
491 template<typename It>
492 It& first(It &i) const { return i=It(*this); }
494 /// \brief Initializes node iterator to point to the node of a given index.
495 NodeIt& nth(NodeIt &i, int n) const {
496 if(n<0 || n>int(length()))
497 fault("UndirPath::nth: index out of range");
498 return i=NodeIt(*this, n);
501 /// \brief Initializes edge iterator to point to the edge of a given index.
502 EdgeIt& nth(EdgeIt &i, int n) const {
503 if(n<0 || n>=int(length()))
504 fault("UndirPath::nth: index out of range");
505 return i=EdgeIt(*this, n);
508 /// Checks validity of a node or edge iterator.
509 template<typename It>
511 bool valid(const It &i) { return i.valid(); }
513 /// Steps the given node or edge iterator.
514 template<typename It>
518 fault("UndirPath::next() on invalid iterator");
522 /// \brief Returns node iterator pointing to the head node of the
523 /// given edge iterator.
524 NodeIt head(const EdgeIt& e) const {
526 fault("UndirPath::head() on invalid iterator");
527 return NodeIt(*this, e.idx+1);
530 /// \brief Returns node iterator pointing to the tail node of the
531 /// given edge iterator.
532 NodeIt tail(const EdgeIt& e) const {
534 fault("UndirPath::tail() on invalid iterator");
535 return NodeIt(*this, e.idx);
541 * \brief Iterator class to iterate on the edges of the paths
544 * This class is used to iterate on the edges of the paths
546 * Of course it converts to Graph::Edge
548 * \todo Its interface differs from the standard edge iterator.
552 friend class UndirPath;
557 /// Default constructor
559 /// Invalid constructor
560 EdgeIt(Invalid) : idx(-1), p(0) {}
561 /// Constructor with starting point
562 EdgeIt(const UndirPath &_p, int _idx = 0) :
563 idx(_idx), p(&_p) { validate(); }
566 bool valid() const { return idx!=-1; }
568 ///Conversion to Graph::Edge
569 operator GraphEdge () const {
570 return valid() ? p->edges[idx] : INVALID;
573 EdgeIt& operator++() { ++idx; validate(); return *this; }
575 /// Comparison operator
576 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
577 /// Comparison operator
578 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
579 /// Comparison operator
580 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
583 // FIXME: comparison between signed and unsigned...
584 // Jo ez igy? Vagy esetleg legyen a length() int?
585 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
589 * \brief Iterator class to iterate on the nodes of the paths
592 * This class is used to iterate on the nodes of the paths
594 * Of course it converts to Graph::Node
596 * \todo Its interface differs from the standard node iterator.
600 friend class UndirPath;
605 /// Default constructor
607 /// Invalid constructor
608 NodeIt(Invalid) : idx(-1), p(0) {}
609 /// Constructor with starting point
610 NodeIt(const UndirPath &_p, int _idx = 0) :
611 idx(_idx), p(&_p) { validate(); }
614 bool valid() const { return idx!=-1; }
616 ///Conversion to Graph::Node
617 operator const GraphNode& () const {
618 if(idx >= p->length())
621 return p->gr->tail(p->edges[idx]);
626 NodeIt& operator++() { ++idx; validate(); return *this; }
628 /// Comparison operator
629 bool operator==(const NodeIt& e) const { return idx==e.idx; }
630 /// Comparison operator
631 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
632 /// Comparison operator
633 bool operator<(const NodeIt& e) const { return idx<e.idx; }
636 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
639 friend class Builder;
642 * \brief Class to build paths
645 * This class is used to fill a path with edges.
647 * You can push new edges to the front and to the back of the path in
648 * arbitrary order then you should commit these changes to the graph.
650 * Fundamentally, for most "Paths" (classes fulfilling the
651 * PathConcept) while the builder is active (after the first modifying
652 * operation and until the commit()) the original Path is in a
653 * "transitional" state (operations ot it have undefined result). But
654 * in the case of UndirPath the original path is unchanged until the
655 * commit. However we don't recomend that you use this feature.
659 Container front, back;
662 ///\param _P the path you want to fill in.
664 Builder(UndirPath &_P) : P(_P) {}
666 /// Sets the starting node of the path.
668 /// Sets the starting node of the path. Edge added to the path
669 /// afterwards have to be incident to this node.
670 /// It should be called iff the path is empty and before any call to
671 /// \ref pushFront() or \ref pushBack()
672 void setStartNode(const GraphNode &) {}
674 ///Push a new edge to the front of the path
676 ///Push a new edge to the front of the path.
678 void pushFront(const GraphEdge& e) {
679 if( !empty() && P.gr->head(e)!=tail() ) {
680 fault("UndirPath::Builder::pushFront: nonincident edge");
685 ///Push a new edge to the back of the path
687 ///Push a new edge to the back of the path.
689 void pushBack(const GraphEdge& e) {
690 if( !empty() && P.gr->tail(e)!=head() ) {
691 fault("UndirPath::Builder::pushBack: nonincident edge");
696 ///Commit the changes to the path.
698 if( !(front.empty() && back.empty()) ) {
700 tmp.reserve(front.size()+back.size()+P.length());
701 tmp.insert(tmp.end(), front.rbegin(), front.rend());
702 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
703 tmp.insert(tmp.end(), back.begin(), back.end());
710 // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
711 // Hogy kenyelmes egy ilyet hasznalni?
713 ///Reserve storage for the builder in advance.
715 ///If you know an reasonable upper bound of the number of the edges
716 ///to add, using this function you can speed up the building.
717 void reserve(size_t r) {
722 void reserveFront(size_t r) {}
723 void reserveBack(size_t r) {}
727 return front.empty() && back.empty() && P.empty();
730 GraphNode tail() const {
731 if( ! front.empty() )
732 return P.gr->tail(front[front.size()-1]);
733 else if( ! P.empty() )
734 return P.gr->tail(P.edges[0]);
735 else if( ! back.empty() )
736 return P.gr->tail(back[0]);
740 GraphNode head() const {
742 return P.gr->head(back[back.size()-1]);
743 else if( ! P.empty() )
744 return P.gr->head(P.edges[P.length()-1]);
745 else if( ! front.empty() )
746 return P.gr->head(front[0]);
764 /**********************************************************************/
767 /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
768 elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
770 template<typename Graph>
774 typedef typename Graph::Edge GraphEdge;
775 typedef typename Graph::Node GraphNode;
781 // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
783 GraphNode _first, _last;
784 typedef std::deque<GraphEdge> Container;
789 DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
791 /// Subpath defined by two nodes.
792 /// Nodes may be in reversed order, then
793 /// we contstruct the reversed path.
794 DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
795 /// Subpath defined by two edges. Contains edges in [a,b)
796 /// It is an error if the two edges are not in order!
797 DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
799 size_t length() const { return edges.size(); }
800 GraphNode tail() const { return _first; }
801 GraphNode head() const { return _last; }
803 NodeIt& first(NodeIt &n) const { return nth(n, 0); }
804 EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
805 template<typename It>
812 NodeIt& nth(NodeIt &, size_t) const;
813 EdgeIt& nth(EdgeIt &, size_t) const;
814 template<typename It>
815 It nth(size_t n) const {
821 bool valid(const NodeIt &n) const { return n.idx <= length(); }
822 bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
824 bool isForward(const EdgeIt &e) const { return e.forw; }
826 /// index of a node on the path. Returns length+2 for the invalid NodeIt
827 int index(const NodeIt &n) const { return n.idx; }
828 /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
829 int index(const EdgeIt &e) const { return e.it - edges.begin(); }
831 EdgeIt& next(EdgeIt &e) const;
832 NodeIt& next(NodeIt &n) const;
833 template <typename It>
834 It getNext(It it) const {
835 It tmp(it); return next(tmp);
838 // A path is constructed using the following four functions.
839 // They return false if the requested operation is inconsistent
840 // with the path constructed so far.
841 // If your path has only one edge you MUST set either "from" or "to"!
842 // So you probably SHOULD call it in any case to be safe (and check the
843 // returned value to check if your path is consistent with your idea).
844 bool pushFront(const GraphEdge &e);
845 bool pushBack(const GraphEdge &e);
846 bool setFrom(const GraphNode &n);
847 bool setTo(const GraphNode &n);
849 // WARNING: these two functions return the head/tail of an edge with
850 // respect to the direction of the path!
851 // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
852 // P.forward(e) is true (or the edge is a loop)!
853 NodeIt head(const EdgeIt& e) const;
854 NodeIt tail(const EdgeIt& e) const;
856 // FIXME: ezeknek valami jobb nev kellene!!!
857 GraphEdge graphEdge(const EdgeIt& e) const;
858 GraphNode graphNode(const NodeIt& n) const;
861 /*** Iterator classes ***/
863 friend class DynamicPath;
865 typename Container::const_iterator it;
868 // FIXME: jarna neki ilyen is...
871 bool forward() const { return forw; }
873 bool operator==(const EdgeIt& e) const { return it==e.it; }
874 bool operator!=(const EdgeIt& e) const { return it!=e.it; }
875 bool operator<(const EdgeIt& e) const { return it<e.it; }
879 friend class DynamicPath;
882 bool tail; // Is this node the tail of the edge with same idx?
885 // FIXME: jarna neki ilyen is...
888 bool operator==(const NodeIt& n) const { return idx==n.idx; }
889 bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
890 bool operator<(const NodeIt& n) const { return idx<n.idx; }
894 bool edgeIncident(const GraphEdge &e, const GraphNode &a,
896 bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
899 template<typename Gr>
900 typename DynamicPath<Gr>::EdgeIt&
901 DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
902 if( e.it == edges.end() )
905 GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
908 // Invalid edgeit is always forward :)
909 if( e.it == edges.end() ) {
914 e.forw = ( G.tail(*e.it) == common_node );
918 template<typename Gr>
919 typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
920 if( n.idx >= length() ) {
927 GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
928 G.tail(edges[n.idx]) );
930 if( n.idx < length() ) {
931 n.tail = ( next_node == G.tail(edges[n.idx]) );
940 template<typename Gr>
941 bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
943 if( G.tail(e) == a ) {
947 if( G.head(e) == a ) {
954 template<typename Gr>
955 bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
956 const GraphEdge &f) {
957 if( edgeIncident(f, G.tail(e), _last) ) {
961 if( edgeIncident(f, G.head(e), _last) ) {
968 template<typename Gr>
969 bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
970 if( G.valid(_first) ) {
971 if( edgeIncident(e, _first, _first) ) {
978 else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
986 template<typename Gr>
987 bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
988 if( G.valid(_last) ) {
989 if( edgeIncident(e, _last, _last) ) {
996 else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
1005 template<typename Gr>
1006 bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
1007 if( G.valid(_first) ) {
1012 if( edgeIncident(edges[0], n, _last) ) {
1025 template<typename Gr>
1026 bool DynamicPath<Gr>::setTo(const GraphNode &n) {
1027 if( G.valid(_last) ) {
1032 if( edgeIncident(edges[0], n, _first) ) {
1046 template<typename Gr>
1047 typename DynamicPath<Gr>::NodeIt
1048 DynamicPath<Gr>::tail(const EdgeIt& e) const {
1051 if( e.it == edges.end() ) {
1052 // FIXME: invalid-> invalid
1053 n.idx = length() + 1;
1058 n.idx = e.it-edges.begin();
1063 template<typename Gr>
1064 typename DynamicPath<Gr>::NodeIt
1065 DynamicPath<Gr>::head(const EdgeIt& e) const {
1066 if( e.it == edges.end()-1 ) {
1070 EdgeIt next_edge = e;
1072 return tail(next_edge);
1075 template<typename Gr>
1076 typename DynamicPath<Gr>::GraphEdge
1077 DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
1078 if( e.it != edges.end() ) {
1086 template<typename Gr>
1087 typename DynamicPath<Gr>::GraphNode
1088 DynamicPath<Gr>::graphNode(const NodeIt& n) const {
1089 if( n.idx < length() ) {
1090 return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
1092 else if( n.idx == length() ) {
1100 template<typename Gr>
1101 typename DynamicPath<Gr>::EdgeIt&
1102 DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
1104 // FIXME: invalid EdgeIt
1110 e.it = edges.begin()+k;
1112 e.forw = ( G.tail(*e.it) == _first );
1115 e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
1116 G.tail(*e.it) == G.head(edges[k-1]) );
1121 template<typename Gr>
1122 typename DynamicPath<Gr>::NodeIt&
1123 DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
1125 // FIXME: invalid NodeIt
1135 n = tail(nth<EdgeIt>(k));
1139 // Reszut konstruktorok:
1142 template<typename Gr>
1143 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
1145 G(P.G), edges(a.it, b.it) // WARNING: if b.it < a.it this will blow up!
1147 if( G.valid(P._first) && a.it < P.edges.end() ) {
1148 _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
1149 if( b.it < P.edges.end() ) {
1150 _last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
1158 template<typename Gr>
1159 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
1160 const NodeIt &b) : G(P.G)
1162 if( !P.valid(a) || !P.valid(b) )
1165 int ai = a.idx, bi = b.idx;
1169 edges.resize(bi-ai);
1170 copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
1172 _first = P.graphNode(a);
1173 _last = P.graphNode(b);
1180 #endif // HUGO_PATH_H