Skeletons have been simplified.
"Optional features" have been deleted.
Map skeletons have been renamed.
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.
19 ///\brief Classes for representing paths in graphs.
28 #include <hugo/invalid.h>
29 #include <hugo/error.h>
38 //! \brief A structure for representing directed path 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, typename DM = DefaultDebugMode>
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) {
75 if( DM::range_check && (!a.valid() || !b.valid) ) {
76 // FIXME: this check should be more elaborate...
77 fault("DirPath, subpath ctor: invalid bounding nodes");
80 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
83 /// \brief Subpath constructor.
85 /// Subpath defined by two edges. Contains edges in [a,b)
86 /// \warning It is an error if the two edges are not in order!
87 DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
88 if( DM::range_check && (!a.valid() || !b.valid) ) {
89 // FIXME: this check should be more elaborate...
90 fault("DirPath, subpath ctor: invalid bounding nodes");
93 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
96 /// Length of the path.
97 size_t length() const { return edges.size(); }
98 /// Returns whether the path is empty.
99 bool empty() const { return edges.empty(); }
101 /// Resets the path to an empty path.
102 void clear() { edges.clear(); }
104 /// \brief Starting point of the path.
106 /// Starting point of the path.
107 /// Returns INVALID if the path is empty.
108 GraphNode from() const {
109 return empty() ? INVALID : gr->tail(edges[0]);
111 /// \brief End point of the path.
113 /// End point of the path.
114 /// Returns INVALID if the path is empty.
115 GraphNode to() const {
116 return empty() ? INVALID : gr->head(edges[length()-1]);
119 /// \brief Initializes node or edge iterator to point to the first
123 template<typename It>
124 It& first(It &i) const { return i=It(*this); }
126 /// \brief Initializes node iterator to point to the node of a given index.
127 NodeIt& nth(NodeIt &i, int n) const {
128 if( DM::range_check && (n<0 || n>int(length())) )
129 fault("DirPath::nth: index out of range");
130 return i=NodeIt(*this, n);
133 /// \brief Initializes edge iterator to point to the edge of a given index.
134 EdgeIt& nth(EdgeIt &i, int n) const {
135 if( DM::range_check && (n<0 || n>=int(length())) )
136 fault("DirPath::nth: index out of range");
137 return i=EdgeIt(*this, n);
140 /// Checks validity of a node or edge iterator.
141 template<typename It>
143 bool valid(const It &i) { return i.valid(); }
145 /// Steps the given node or edge iterator.
146 template<typename It>
149 if( DM::range_check && !e.valid() )
150 fault("DirPath::next() on invalid iterator");
154 /// \brief Returns node iterator pointing to the head node of the
155 /// given edge iterator.
156 NodeIt head(const EdgeIt& e) const {
157 if( DM::range_check && !e.valid() )
158 fault("DirPath::head() on invalid iterator");
159 return NodeIt(*this, e.idx+1);
162 /// \brief Returns node iterator pointing to the tail node of the
163 /// given edge iterator.
164 NodeIt tail(const EdgeIt& e) const {
165 if( DM::range_check && !e.valid() )
166 fault("DirPath::tail() on invalid iterator");
167 return NodeIt(*this, e.idx);
171 /* Iterator classes */
174 * \brief Iterator class to iterate on the edges of the paths
177 * This class is used to iterate on the edges of the paths
179 * Of course it converts to Graph::Edge
181 * \todo Its interface differs from the standard edge iterator.
185 friend class DirPath;
190 /// Default constructor
192 /// Invalid constructor
193 EdgeIt(Invalid) : idx(-1), p(0) {}
194 /// Constructor with starting point
195 EdgeIt(const DirPath &_p, int _idx = 0) :
196 idx(_idx), p(&_p) { validate(); }
199 bool valid() const { return idx!=-1; }
201 ///Conversion to Graph::Edge
202 operator GraphEdge () const {
203 return valid() ? p->edges[idx] : INVALID;
207 EdgeIt& operator++() { ++idx; validate(); return *this; }
209 /// Comparison operator
210 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
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; }
217 // FIXME: comparison between signed and unsigned...
218 // Jo ez igy? Vagy esetleg legyen a length() int?
219 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
223 * \brief Iterator class to iterate on the nodes of the paths
226 * This class is used to iterate on the nodes of the paths
228 * Of course it converts to Graph::Node
230 * \todo Its interface differs from the standard node iterator.
234 friend class DirPath;
239 /// Default constructor
241 /// Invalid constructor
242 NodeIt(Invalid) : idx(-1), p(0) {}
243 /// Constructor with starting point
244 NodeIt(const DirPath &_p, int _idx = 0) :
245 idx(_idx), p(&_p) { validate(); }
248 bool valid() const { return idx!=-1; }
250 ///Conversion to Graph::Node
251 operator const GraphNode& () const {
252 if(idx >= p->length())
255 return p->gr->tail(p->edges[idx]);
260 NodeIt& operator++() { ++idx; validate(); return *this; }
262 /// Comparison operator
263 bool operator==(const NodeIt& e) const { return idx==e.idx; }
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; }
270 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
273 friend class Builder;
276 * \brief Class to build paths
279 * This class is used to fill a path with edges.
281 * You can push new edges to the front and to the back of the path in
282 * arbitrary order then you should commit these changes to the graph.
284 * Fundamentally, for most "Paths" (classes fulfilling the
285 * PathConcept) while the builder is active (after the first modifying
286 * operation and until the commit()) the original Path is in a
287 * "transitional" state (operations on it have undefined result). But
288 * in the case of DirPath the original path remains unchanged until the
289 * commit. However we don't recomend that you use this feature.
293 Container front, back;
296 ///\param _P the path you want to fill in.
298 Builder(DirPath &_P) : P(_P) {}
300 /// Sets the starting node of the path.
302 /// Sets the starting node of the path. Edge added to the path
303 /// afterwards have to be incident to this node.
304 /// It should be called iff the path is empty and before any call to
305 /// \ref pushFront() or \ref pushBack()
306 void setStart(const GraphNode &) {}
308 ///Push a new edge to the front of the path
310 ///Push a new edge to the front of the path.
312 void pushFront(const GraphEdge& e) {
313 if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
314 fault("DirPath::Builder::pushFront: nonincident edge");
319 ///Push a new edge to the back of the path
321 ///Push a new edge to the back of the path.
323 void pushBack(const GraphEdge& e) {
324 if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
325 fault("DirPath::Builder::pushBack: nonincident edge");
330 ///Commit the changes to the path.
332 if( !(front.empty() && back.empty()) ) {
334 tmp.reserve(front.size()+back.size()+P.length());
335 tmp.insert(tmp.end(), front.rbegin(), front.rend());
336 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
337 tmp.insert(tmp.end(), back.begin(), back.end());
344 // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
345 // Hogy kenyelmes egy ilyet hasznalni?
347 ///Reserve storage in advance for the builder
349 ///If you know an reasonable upper bound of the number of the edges
350 ///to add, using this function you can speed up the building.
351 void reserve(size_t r) {
358 return front.empty() && back.empty() && P.empty();
361 GraphNode from() const {
362 if( ! front.empty() )
363 return P.gr->tail(front[front.size()-1]);
364 else if( ! P.empty() )
365 return P.gr->tail(P.edges[0]);
366 else if( ! back.empty() )
367 return P.gr->tail(back[0]);
371 GraphNode to() const {
373 return P.gr->head(back[back.size()-1]);
374 else if( ! P.empty() )
375 return P.gr->head(P.edges[P.length()-1]);
376 else if( ! front.empty() )
377 return P.gr->head(front[0]);
395 /**********************************************************************/
398 //! \brief A structure for representing undirected path in a graph.
400 //! A structure for representing undirected path in a graph. Ie. this is
401 //! a path in a \e directed graph but the edges should not be directed
404 //! \param Graph The graph type in which the path is.
405 //! \param DM DebugMode, defaults to DefaultDebugMode.
407 //! In a sense, the path can be treated as a graph, for is has \c NodeIt
408 //! and \c EdgeIt with the same usage. These types converts to the \c Node
409 //! and \c Edge of the original graph.
411 //! \todo Thoroughfully check all the range and consistency tests.
412 template<typename Graph, typename DM = DefaultDebugMode>
415 /// Edge type of the underlying graph.
416 typedef typename Graph::Edge GraphEdge;
417 /// Node type of the underlying graph.
418 typedef typename Graph::Node GraphNode;
424 typedef std::vector<GraphEdge> Container;
429 /// \param _G The graph in which the path is.
431 UndirPath(const Graph &_G) : gr(&_G) {}
433 /// \brief Subpath constructor.
435 /// Subpath defined by two nodes.
436 /// \warning It is an error if the two edges are not in order!
437 UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
438 if( DM::range_check && (!a.valid() || !b.valid) ) {
439 // FIXME: this check should be more elaborate...
440 fault("UndirPath, subpath ctor: invalid bounding nodes");
443 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
446 /// \brief Subpath constructor.
448 /// Subpath defined by two edges. Contains edges in [a,b)
449 /// \warning It is an error if the two edges are not in order!
450 UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
451 if( DM::range_check && (!a.valid() || !b.valid) ) {
452 // FIXME: this check should be more elaborate...
453 fault("UndirPath, subpath ctor: invalid bounding nodes");
456 edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
459 /// Length of the path.
460 size_t length() const { return edges.size(); }
461 /// Returns whether the path is empty.
462 bool empty() const { return edges.empty(); }
464 /// Resets the path to an empty path.
465 void clear() { edges.clear(); }
467 /// \brief Starting point of the path.
469 /// Starting point of the path.
470 /// Returns INVALID if the path is empty.
471 GraphNode from() const {
472 return empty() ? INVALID : gr->tail(edges[0]);
474 /// \brief End point of the path.
476 /// End point of the path.
477 /// Returns INVALID if the path is empty.
478 GraphNode to() const {
479 return empty() ? INVALID : gr->head(edges[length()-1]);
482 /// \brief Initializes node or edge iterator to point to the first
486 template<typename It>
487 It& first(It &i) const { return i=It(*this); }
489 /// \brief Initializes node iterator to point to the node of a given index.
490 NodeIt& nth(NodeIt &i, int n) const {
491 if( DM::range_check && (n<0 || n>int(length())) )
492 fault("UndirPath::nth: index out of range");
493 return i=NodeIt(*this, n);
496 /// \brief Initializes edge iterator to point to the edge of a given index.
497 EdgeIt& nth(EdgeIt &i, int n) const {
498 if( DM::range_check && (n<0 || n>=int(length())) )
499 fault("UndirPath::nth: index out of range");
500 return i=EdgeIt(*this, n);
503 /// Checks validity of a node or edge iterator.
504 template<typename It>
506 bool valid(const It &i) { return i.valid(); }
508 /// Steps the given node or edge iterator.
509 template<typename It>
512 if( DM::range_check && !e.valid() )
513 fault("UndirPath::next() on invalid iterator");
517 /// \brief Returns node iterator pointing to the head node of the
518 /// given edge iterator.
519 NodeIt head(const EdgeIt& e) const {
520 if( DM::range_check && !e.valid() )
521 fault("UndirPath::head() on invalid iterator");
522 return NodeIt(*this, e.idx+1);
525 /// \brief Returns node iterator pointing to the tail node of the
526 /// given edge iterator.
527 NodeIt tail(const EdgeIt& e) const {
528 if( DM::range_check && !e.valid() )
529 fault("UndirPath::tail() on invalid iterator");
530 return NodeIt(*this, e.idx);
536 * \brief Iterator class to iterate on the edges of the paths
539 * This class is used to iterate on the edges of the paths
541 * Of course it converts to Graph::Edge
543 * \todo Its interface differs from the standard edge iterator.
547 friend class UndirPath;
552 /// Default constructor
554 /// Invalid constructor
555 EdgeIt(Invalid) : idx(-1), p(0) {}
556 /// Constructor with starting point
557 EdgeIt(const UndirPath &_p, int _idx = 0) :
558 idx(_idx), p(&_p) { validate(); }
561 bool valid() const { return idx!=-1; }
563 ///Conversion to Graph::Edge
564 operator GraphEdge () const {
565 return valid() ? p->edges[idx] : INVALID;
568 EdgeIt& operator++() { ++idx; validate(); return *this; }
570 /// Comparison operator
571 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
572 /// Comparison operator
573 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
574 /// Comparison operator
575 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
578 // FIXME: comparison between signed and unsigned...
579 // Jo ez igy? Vagy esetleg legyen a length() int?
580 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
584 * \brief Iterator class to iterate on the nodes of the paths
587 * This class is used to iterate on the nodes of the paths
589 * Of course it converts to Graph::Node
591 * \todo Its interface differs from the standard node iterator.
595 friend class UndirPath;
600 /// Default constructor
602 /// Invalid constructor
603 NodeIt(Invalid) : idx(-1), p(0) {}
604 /// Constructor with starting point
605 NodeIt(const UndirPath &_p, int _idx = 0) :
606 idx(_idx), p(&_p) { validate(); }
609 bool valid() const { return idx!=-1; }
611 ///Conversion to Graph::Node
612 operator const GraphNode& () const {
613 if(idx >= p->length())
616 return p->gr->tail(p->edges[idx]);
621 NodeIt& operator++() { ++idx; validate(); return *this; }
623 /// Comparison operator
624 bool operator==(const NodeIt& e) const { return idx==e.idx; }
625 /// Comparison operator
626 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
627 /// Comparison operator
628 bool operator<(const NodeIt& e) const { return idx<e.idx; }
631 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
634 friend class Builder;
637 * \brief Class to build paths
640 * This class is used to fill a path with edges.
642 * You can push new edges to the front and to the back of the path in
643 * arbitrary order then you should commit these changes to the graph.
645 * Fundamentally, for most "Paths" (classes fulfilling the
646 * PathConcept) while the builder is active (after the first modifying
647 * operation and until the commit()) the original Path is in a
648 * "transitional" state (operations ot it have undefined result). But
649 * in the case of UndirPath the original path is unchanged until the
650 * commit. However we don't recomend that you use this feature.
654 Container front, back;
657 ///\param _P the path you want to fill in.
659 Builder(UndirPath &_P) : P(_P) {}
661 /// Sets the starting node of the path.
663 /// Sets the starting node of the path. Edge added to the path
664 /// afterwards have to be incident to this node.
665 /// It should be called iff the path is empty and before any call to
666 /// \ref pushFront() or \ref pushBack()
667 void setStart(const GraphNode &) {}
669 ///Push a new edge to the front of the path
671 ///Push a new edge to the front of the path.
673 void pushFront(const GraphEdge& e) {
674 if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
675 fault("UndirPath::Builder::pushFront: nonincident edge");
680 ///Push a new edge to the back of the path
682 ///Push a new edge to the back of the path.
684 void pushBack(const GraphEdge& e) {
685 if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
686 fault("UndirPath::Builder::pushBack: nonincident edge");
691 ///Commit the changes to the path.
693 if( !(front.empty() && back.empty()) ) {
695 tmp.reserve(front.size()+back.size()+P.length());
696 tmp.insert(tmp.end(), front.rbegin(), front.rend());
697 tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
698 tmp.insert(tmp.end(), back.begin(), back.end());
705 // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
706 // Hogy kenyelmes egy ilyet hasznalni?
708 ///Reserve storage in advance for the builder
710 ///If you know an reasonable upper bound of the number of the edges
711 ///to add, using this function you can speed up the building.
712 void reserve(size_t r) {
719 return front.empty() && back.empty() && P.empty();
722 GraphNode from() const {
723 if( ! front.empty() )
724 return P.gr->tail(front[front.size()-1]);
725 else if( ! P.empty() )
726 return P.gr->tail(P.edges[0]);
727 else if( ! back.empty() )
728 return P.gr->tail(back[0]);
732 GraphNode to() const {
734 return P.gr->head(back[back.size()-1]);
735 else if( ! P.empty() )
736 return P.gr->head(P.edges[P.length()-1]);
737 else if( ! front.empty() )
738 return P.gr->head(front[0]);
756 /**********************************************************************/
759 /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
760 elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
762 template<typename Graph>
766 typedef typename Graph::Edge GraphEdge;
767 typedef typename Graph::Node GraphNode;
773 // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
775 GraphNode _first, _last;
776 typedef std::deque<GraphEdge> Container;
781 DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
783 /// Subpath defined by two nodes.
784 /// Nodes may be in reversed order, then
785 /// we contstruct the reversed path.
786 DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
787 /// Subpath defined by two edges. Contains edges in [a,b)
788 /// It is an error if the two edges are not in order!
789 DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
791 size_t length() const { return edges.size(); }
792 GraphNode from() const { return _first; }
793 GraphNode to() const { return _last; }
795 NodeIt& first(NodeIt &n) const { return nth(n, 0); }
796 EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
797 template<typename It>
804 NodeIt& nth(NodeIt &, size_t) const;
805 EdgeIt& nth(EdgeIt &, size_t) const;
806 template<typename It>
807 It nth(size_t n) const {
813 bool valid(const NodeIt &n) const { return n.idx <= length(); }
814 bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
816 bool isForward(const EdgeIt &e) const { return e.forw; }
818 /// index of a node on the path. Returns length+2 for the invalid NodeIt
819 int index(const NodeIt &n) const { return n.idx; }
820 /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
821 int index(const EdgeIt &e) const { return e.it - edges.begin(); }
823 EdgeIt& next(EdgeIt &e) const;
824 NodeIt& next(NodeIt &n) const;
825 template <typename It>
826 It getNext(It it) const {
827 It tmp(it); return next(tmp);
830 // A path is constructed using the following four functions.
831 // They return false if the requested operation is inconsistent
832 // with the path constructed so far.
833 // If your path has only one edge you MUST set either "from" or "to"!
834 // So you probably SHOULD call it in any case to be safe (and check the
835 // returned value to check if your path is consistent with your idea).
836 bool pushFront(const GraphEdge &e);
837 bool pushBack(const GraphEdge &e);
838 bool setFrom(const GraphNode &n);
839 bool setTo(const GraphNode &n);
841 // WARNING: these two functions return the head/tail of an edge with
842 // respect to the direction of the path!
843 // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
844 // P.forward(e) is true (or the edge is a loop)!
845 NodeIt head(const EdgeIt& e) const;
846 NodeIt tail(const EdgeIt& e) const;
848 // FIXME: ezeknek valami jobb nev kellene!!!
849 GraphEdge graphEdge(const EdgeIt& e) const;
850 GraphNode graphNode(const NodeIt& n) const;
853 /*** Iterator classes ***/
855 friend class DynamicPath;
857 typename Container::const_iterator it;
860 // FIXME: jarna neki ilyen is...
863 bool forward() const { return forw; }
865 bool operator==(const EdgeIt& e) const { return it==e.it; }
866 bool operator!=(const EdgeIt& e) const { return it!=e.it; }
867 bool operator<(const EdgeIt& e) const { return it<e.it; }
871 friend class DynamicPath;
874 bool tail; // Is this node the tail of the edge with same idx?
877 // FIXME: jarna neki ilyen is...
880 bool operator==(const NodeIt& n) const { return idx==n.idx; }
881 bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
882 bool operator<(const NodeIt& n) const { return idx<n.idx; }
886 bool edgeIncident(const GraphEdge &e, const GraphNode &a,
888 bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
891 template<typename Gr>
892 typename DynamicPath<Gr>::EdgeIt&
893 DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
894 if( e.it == edges.end() )
897 GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
900 // Invalid edgeit is always forward :)
901 if( e.it == edges.end() ) {
906 e.forw = ( G.tail(*e.it) == common_node );
910 template<typename Gr>
911 typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
912 if( n.idx >= length() ) {
919 GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
920 G.tail(edges[n.idx]) );
922 if( n.idx < length() ) {
923 n.tail = ( next_node == G.tail(edges[n.idx]) );
932 template<typename Gr>
933 bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
935 if( G.tail(e) == a ) {
939 if( G.head(e) == a ) {
946 template<typename Gr>
947 bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
948 const GraphEdge &f) {
949 if( edgeIncident(f, G.tail(e), _last) ) {
953 if( edgeIncident(f, G.head(e), _last) ) {
960 template<typename Gr>
961 bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
962 if( G.valid(_first) ) {
963 if( edgeIncident(e, _first, _first) ) {
970 else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
978 template<typename Gr>
979 bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
980 if( G.valid(_last) ) {
981 if( edgeIncident(e, _last, _last) ) {
988 else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
997 template<typename Gr>
998 bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
999 if( G.valid(_first) ) {
1004 if( edgeIncident(edges[0], n, _last) ) {
1017 template<typename Gr>
1018 bool DynamicPath<Gr>::setTo(const GraphNode &n) {
1019 if( G.valid(_last) ) {
1024 if( edgeIncident(edges[0], n, _first) ) {
1038 template<typename Gr>
1039 typename DynamicPath<Gr>::NodeIt
1040 DynamicPath<Gr>::tail(const EdgeIt& e) const {
1043 if( e.it == edges.end() ) {
1044 // FIXME: invalid-> invalid
1045 n.idx = length() + 1;
1050 n.idx = e.it-edges.begin();
1055 template<typename Gr>
1056 typename DynamicPath<Gr>::NodeIt
1057 DynamicPath<Gr>::head(const EdgeIt& e) const {
1058 if( e.it == edges.end()-1 ) {
1062 EdgeIt next_edge = e;
1064 return tail(next_edge);
1067 template<typename Gr>
1068 typename DynamicPath<Gr>::GraphEdge
1069 DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
1070 if( e.it != edges.end() ) {
1078 template<typename Gr>
1079 typename DynamicPath<Gr>::GraphNode
1080 DynamicPath<Gr>::graphNode(const NodeIt& n) const {
1081 if( n.idx < length() ) {
1082 return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
1084 else if( n.idx == length() ) {
1092 template<typename Gr>
1093 typename DynamicPath<Gr>::EdgeIt&
1094 DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
1096 // FIXME: invalid EdgeIt
1102 e.it = edges.begin()+k;
1104 e.forw = ( G.tail(*e.it) == _first );
1107 e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
1108 G.tail(*e.it) == G.head(edges[k-1]) );
1113 template<typename Gr>
1114 typename DynamicPath<Gr>::NodeIt&
1115 DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
1117 // FIXME: invalid NodeIt
1127 n = tail(nth<EdgeIt>(k));
1131 // Reszut konstruktorok:
1134 template<typename Gr>
1135 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
1137 G(P.G), edges(a.it, b.it) // WARNING: if b.it < a.it this will blow up!
1139 if( G.valid(P._first) && a.it < P.edges.end() ) {
1140 _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
1141 if( b.it < P.edges.end() ) {
1142 _last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
1150 template<typename Gr>
1151 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
1152 const NodeIt &b) : G(P.G)
1154 if( !P.valid(a) || !P.valid(b) )
1157 int ai = a.idx, bi = b.idx;
1161 edges.resize(bi-ai);
1162 copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
1164 _first = P.graphNode(a);
1165 _last = P.graphNode(b);
1172 #endif // HUGO_PATH_H