5 ///\brief Class for representing paths in graphs.
21 ///A container for directed paths
23 ///\param Graph The graph type in which the path is.
25 ///In a sense, the path can be treated as a graph, for is has \c NodeIt
26 ///and \c EdgeIt with the same usage. These types converts to the \c Node
27 ///and \c Edge of the original graph.
28 ///\todo How to clear a path?
29 ///\todo Clarify the consistency checks to do.
30 template<typename Graph>
33 typedef typename Graph::Edge GraphEdge;
34 typedef typename Graph::Node GraphNode;
40 typedef std::vector<GraphEdge> Container;
47 /// \param _G The graph in which the path is.
49 DirPath(const Graph &_G) : gr(&_G) {}
51 /// Subpath defined by two nodes.
52 /// \warning It is an error if the two edges are not in order!
53 DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b);
54 /// Subpath defined by two edges. Contains edges in [a,b)
55 /// \warning It is an error if the two edges are not in order!
56 DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b);
58 size_t length() const { return edges.size(); }
59 bool empty() const { return edges.empty(); }
60 GraphNode from() const {
61 return empty() ? INVALID : gr->tail(edges[0]);
63 GraphNode to() const {
64 return empty() ? INVALID : gr->head(edges[length()-1]);
68 It& first(It &i) const { return i=It(*this); }
71 It& nth(It &i, int n) const { return i=It(*this, n); }
74 bool valid(const It &i) const { return i.valid(); }
77 It& next(It &e) const { return ++e; }
80 NodeIt head(const EdgeIt& e) const;
81 NodeIt tail(const EdgeIt& e) const;
84 /*** Iterator classes ***/
92 EdgeIt(Invalid) : idx(-1), p(0) {}
93 EdgeIt(const DirPath &_p, int _idx = 0) :
94 idx(_idx), p(&_p) { validate(); }
96 bool valid() const { return idx!=-1; }
98 operator GraphEdge () const {
99 return valid() ? p->edges[idx] : INVALID;
101 EdgeIt& operator++() { ++idx; validate(); return *this; }
103 bool operator==(const EdgeIt& e) const { return idx==e.idx; }
104 bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
105 bool operator<(const EdgeIt& e) const { return idx<e.idx; }
108 // FIXME: comparison between signed and unsigned...
109 // Jo ez igy? Vagy esetleg legyen a length() int?
110 void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
114 friend class DirPath;
120 NodeIt(Invalid) : idx(-1), p(0) {}
121 NodeIt(const DirPath &_p, int _idx = 0) :
122 idx(_idx), p(&_p) { validate(); }
124 bool valid() const { return idx!=-1; }
126 operator const GraphEdge& () const {
127 if(idx >= p->length())
130 return p->gr->tail(p->edges[idx]);
134 NodeIt& operator++() { ++idx; validate(); return *this; }
136 bool operator==(const NodeIt& e) const { return idx==e.idx; }
137 bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
138 bool operator<(const NodeIt& e) const { return idx<e.idx; }
141 void validate() { if( size_t(idx) > p->length() ) idx=-1; }
144 friend class Builder;
146 ///Class to build paths
149 ///This class is used to build new paths.
150 ///You can push new edges to the front and to the back of the path in
151 ///arbitrary order the you can commit these changes to the graph.
152 ///\todo We must clarify when the path will be in "transitional" state.
160 ///\param _P the path you want to build.
162 Builder(DirPath &_P) : P(_P) {}
164 ///Set the first node of the path.
166 ///Set the first node of the path.
167 ///If the path is empty, this must be call before any call of
168 ///\ref pushFront() or \ref pushBack()
169 void setFirst(const GraphNode &) { }
171 ///Push a new edge to the front of the path
173 ///Push a new edge to the front of the path.
175 bool pushFront(const GraphEdge& e) {
176 if( empty() || P.gr->head(e)==from() ) {
182 ///Push a new edge to the back of the path
184 ///Push a new edge to the back of the path.
186 bool pushBack(const GraphEdge& e) {
187 if( empty() || P.gr->tail(e)==to() ) {
188 P.edges.push_back(e);
194 ///Commit the changes to the path.
197 P.edges.insert(P.edges.begin(), d.rbegin(), d.rend());
205 ///It commit also commit the changes.
206 ///\todo Is this what we want?
207 ~Builder() { commit(); }
209 // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
210 // Hogy kenyelmes egy ilyet hasznalni?
211 void reserve(size_t r) {
213 P.edges.reserve(P.length()+r);
217 bool empty() { return d.empty() && P.empty(); }
219 GraphNode from() const {
221 return P.gr->tail(d[d.size()-1]);
222 else if( ! P.empty() )
223 return P.gr->tail(P.edges[0]);
227 GraphNode to() const {
229 return P.gr->head(P.edges[P.length()-1]);
230 else if( ! d.empty() )
231 return P.gr->head(d[0]);
249 /**********************************************************************/
252 /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
253 elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
255 template<typename Graph>
259 typedef typename Graph::Edge GraphEdge;
260 typedef typename Graph::Node GraphNode;
266 // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
268 GraphNode _first, _last;
269 typedef std::deque<GraphEdge> Container;
274 DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
276 /// Subpath defined by two nodes.
277 /// Nodes may be in reversed order, then
278 /// we contstruct the reversed path.
279 DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
280 /// Subpath defined by two edges. Contains edges in [a,b)
281 /// It is an error if the two edges are not in order!
282 DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
284 size_t length() const { return edges.size(); }
285 GraphNode from() const { return _first; }
286 GraphNode to() const { return _last; }
288 NodeIt& first(NodeIt &n) const { return nth(n, 0); }
289 EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
290 template<typename It>
297 NodeIt& nth(NodeIt &, size_t) const;
298 EdgeIt& nth(EdgeIt &, size_t) const;
299 template<typename It>
300 It nth(size_t n) const {
306 bool valid(const NodeIt &n) const { return n.idx <= length(); }
307 bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
309 bool isForward(const EdgeIt &e) const { return e.forw; }
311 /// index of a node on the path. Returns length+2 for the invalid NodeIt
312 int index(const NodeIt &n) const { return n.idx; }
313 /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
314 int index(const EdgeIt &e) const { return e.it - edges.begin(); }
316 EdgeIt& next(EdgeIt &e) const;
317 NodeIt& next(NodeIt &n) const;
318 template <typename It>
319 It getNext(It it) const {
320 It tmp(it); return next(tmp);
323 // A path is constructed using the following four functions.
324 // They return false if the requested operation is inconsistent
325 // with the path constructed so far.
326 // If your path has only one edge you MUST set either "from" or "to"!
327 // So you probably SHOULD call it in any case to be safe (and check the
328 // returned value to check if your path is consistent with your idea).
329 bool pushFront(const GraphEdge &e);
330 bool pushBack(const GraphEdge &e);
331 bool setFrom(const GraphNode &n);
332 bool setTo(const GraphNode &n);
334 // WARNING: these two functions return the head/tail of an edge with
335 // respect to the direction of the path!
336 // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
337 // P.forward(e) is true (or the edge is a loop)!
338 NodeIt head(const EdgeIt& e) const;
339 NodeIt tail(const EdgeIt& e) const;
341 // FIXME: ezeknek valami jobb nev kellene!!!
342 GraphEdge graphEdge(const EdgeIt& e) const;
343 GraphNode graphNode(const NodeIt& n) const;
346 /*** Iterator classes ***/
348 friend class DynamicPath;
350 typename Container::const_iterator it;
353 // FIXME: jarna neki ilyen is...
356 bool forward() const { return forw; }
358 bool operator==(const EdgeIt& e) const { return it==e.it; }
359 bool operator!=(const EdgeIt& e) const { return it!=e.it; }
360 bool operator<(const EdgeIt& e) const { return it<e.it; }
364 friend class DynamicPath;
367 bool tail; // Is this node the tail of the edge with same idx?
370 // FIXME: jarna neki ilyen is...
373 bool operator==(const NodeIt& n) const { return idx==n.idx; }
374 bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
375 bool operator<(const NodeIt& n) const { return idx<n.idx; }
379 bool edgeIncident(const GraphEdge &e, const GraphNode &a,
381 bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
384 template<typename Gr>
385 typename DynamicPath<Gr>::EdgeIt&
386 DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
387 if( e.it == edges.end() )
390 GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
393 // Invalid edgeit is always forward :)
394 if( e.it == edges.end() ) {
399 e.forw = ( G.tail(*e.it) == common_node );
403 template<typename Gr>
404 typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
405 if( n.idx >= length() ) {
412 GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
413 G.tail(edges[n.idx]) );
415 if( n.idx < length() ) {
416 n.tail = ( next_node == G.tail(edges[n.idx]) );
425 template<typename Gr>
426 bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
428 if( G.tail(e) == a ) {
432 if( G.head(e) == a ) {
439 template<typename Gr>
440 bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
441 const GraphEdge &f) {
442 if( edgeIncident(f, G.tail(e), _last) ) {
446 if( edgeIncident(f, G.head(e), _last) ) {
453 template<typename Gr>
454 bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
455 if( G.valid(_first) ) {
456 if( edgeIncident(e, _first, _first) ) {
463 else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
471 template<typename Gr>
472 bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
473 if( G.valid(_last) ) {
474 if( edgeIncident(e, _last, _last) ) {
481 else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
490 template<typename Gr>
491 bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
492 if( G.valid(_first) ) {
497 if( edgeIncident(edges[0], n, _last) ) {
510 template<typename Gr>
511 bool DynamicPath<Gr>::setTo(const GraphNode &n) {
512 if( G.valid(_last) ) {
517 if( edgeIncident(edges[0], n, _first) ) {
531 template<typename Gr>
532 typename DynamicPath<Gr>::NodeIt
533 DynamicPath<Gr>::tail(const EdgeIt& e) const {
536 if( e.it == edges.end() ) {
537 // FIXME: invalid-> invalid
538 n.idx = length() + 1;
543 n.idx = e.it-edges.begin();
548 template<typename Gr>
549 typename DynamicPath<Gr>::NodeIt
550 DynamicPath<Gr>::head(const EdgeIt& e) const {
551 if( e.it == edges.end()-1 ) {
555 EdgeIt next_edge = e;
557 return tail(next_edge);
560 template<typename Gr>
561 typename DynamicPath<Gr>::GraphEdge
562 DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
563 if( e.it != edges.end() ) {
571 template<typename Gr>
572 typename DynamicPath<Gr>::GraphNode
573 DynamicPath<Gr>::graphNode(const NodeIt& n) const {
574 if( n.idx < length() ) {
575 return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
577 else if( n.idx == length() ) {
585 template<typename Gr>
586 typename DynamicPath<Gr>::EdgeIt&
587 DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
589 // FIXME: invalid EdgeIt
595 e.it = edges.begin()+k;
597 e.forw = ( G.tail(*e.it) == _first );
600 e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
601 G.tail(*e.it) == G.head(edges[k-1]) );
606 template<typename Gr>
607 typename DynamicPath<Gr>::NodeIt&
608 DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
610 // FIXME: invalid NodeIt
620 n = tail(nth<EdgeIt>(k));
624 // Reszut konstruktorok:
627 template<typename Gr>
628 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
630 G(P.G), edges(a.it, b.it) // WARNING: if b.it < a.it this will blow up!
632 if( G.valid(P._first) && a.it < P.edges.end() ) {
633 _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
634 if( b.it < P.edges.end() ) {
635 _last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
643 template<typename Gr>
644 DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
645 const NodeIt &b) : G(P.G)
647 if( !P.valid(a) || !P.valid(b) )
650 int ai = a.idx, bi = b.idx;
655 copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
657 _first = P.graphNode(a);
658 _last = P.graphNode(b);
665 #endif // HUGO_PATH_H