Remove function and some todos.
5 * Class for representing paths in graphs.
19 /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
20 elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
22 template<typename Graph>
26 typedef typename Graph::Edge GraphEdge;
27 typedef typename Graph::Node GraphNode;
33 // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
35 GraphNode _first, _last;
36 typedef std::deque<GraphEdge> Container;
41 Path(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
43 /// Subpath defined by two nodes.
44 /// Nodes may be in reversed order, then
45 /// we contstruct the reversed path.
46 Path(const Path &P, const NodeIt &a, const NodeIt &b);
47 /// Subpath defined by two edges. Contains edges in [a,b)
48 /// It is an error if the two edges are not in order!
49 Path(const Path &P, const EdgeIt &a, const EdgeIt &b);
51 size_t length() const { return edges.size(); }
52 GraphNode from() const { return _first; }
53 GraphNode to() const { return _last; }
55 NodeIt& first(NodeIt &n) const { return nth(n, 0); }
56 EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
64 NodeIt& nth(NodeIt &, size_t) const;
65 EdgeIt& nth(EdgeIt &, size_t) const;
67 It nth(size_t n) const {
73 bool valid(const NodeIt &n) const { return n.idx <= length(); }
74 bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
76 bool isForward(const EdgeIt &e) const { return e.forw; }
78 /// index of a node on the path. Returns length+2 for the invalid NodeIt
79 int index(const NodeIt &n) const { return n.idx; }
80 /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
81 int index(const EdgeIt &e) const { return e.it - edges.begin(); }
83 EdgeIt& next(EdgeIt &e) const;
84 NodeIt& next(NodeIt &n) const;
85 template <typename It>
86 It getNext(It it) const {
87 It tmp(it); return next(tmp);
90 // A path is constructed using the following four functions.
91 // They return false if the requested operation is inconsistent
92 // with the path constructed so far.
93 // If your path has only one edge you MUST set either "from" or "to"!
94 // So you probably SHOULD call it in any case to be safe (and check the
95 // returned value to check if your path is consistent with your idea).
96 bool pushFront(const GraphEdge &e);
97 bool pushBack(const GraphEdge &e);
98 bool setFrom(const GraphNode &n);
99 bool setTo(const GraphNode &n);
101 // WARNING: these two functions return the head/tail of an edge with
102 // respect to the direction of the path!
103 // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
104 // P.forward(e) is true (or the edge is a loop)!
105 NodeIt head(const EdgeIt& e) const;
106 NodeIt tail(const EdgeIt& e) const;
108 // FIXME: ezeknek valami jobb nev kellene!!!
109 GraphEdge graphEdge(const EdgeIt& e) const;
110 GraphNode graphNode(const NodeIt& n) const;
113 /*** Iterator classes ***/
117 typename Container::const_iterator it;
120 // FIXME: jarna neki ilyen is...
123 bool forward() const { return forw; }
125 bool operator==(const EdgeIt& e) const { return it==e.it; }
126 bool operator!=(const EdgeIt& e) const { return it!=e.it; }
127 bool operator<(const EdgeIt& e) const { return it<e.it; }
135 bool tail; // Is this node the tail of the edge with same idx?
138 // FIXME: jarna neki ilyen is...
141 bool operator==(const NodeIt& n) const { return idx==n.idx; }
142 bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
143 bool operator<(const NodeIt& n) const { return idx<n.idx; }
147 bool edgeIncident(const GraphEdge &e, const GraphNode &a,
149 bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
152 template<typename Gr>
153 typename Path<Gr>::EdgeIt&
154 Path<Gr>::next(Path::EdgeIt &e) const {
155 if( e.it == edges.end() )
158 GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
161 // Invalid edgeit is always forward :)
162 if( e.it == edges.end() ) {
167 e.forw = ( G.tail(*e.it) == common_node );
171 template<typename Gr>
172 typename Path<Gr>::NodeIt& Path<Gr>::next(NodeIt &n) const {
173 if( n.idx >= length() ) {
180 GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
181 G.tail(edges[n.idx]) );
183 if( n.idx < length() ) {
184 n.tail = ( next_node == G.tail(edges[n.idx]) );
193 template<typename Gr>
194 bool Path<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
196 if( G.tail(e) == a ) {
200 if( G.head(e) == a ) {
207 template<typename Gr>
208 bool Path<Gr>::connectTwoEdges(const GraphEdge &e,
209 const GraphEdge &f) {
210 if( edgeIncident(f, G.tail(e), _last) ) {
214 if( edgeIncident(f, G.head(e), _last) ) {
221 template<typename Gr>
222 bool Path<Gr>::pushFront(const GraphEdge &e) {
223 if( G.valid(_first) ) {
224 if( edgeIncident(e, _first, _first) ) {
231 else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
239 template<typename Gr>
240 bool Path<Gr>::pushBack(const GraphEdge &e) {
241 if( G.valid(_last) ) {
242 if( edgeIncident(e, _last, _last) ) {
249 else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
258 template<typename Gr>
259 bool Path<Gr>::setFrom(const GraphNode &n) {
260 if( G.valid(_first) ) {
265 if( edgeIncident(edges[0], n, _last) ) {
278 template<typename Gr>
279 bool Path<Gr>::setTo(const GraphNode &n) {
280 if( G.valid(_last) ) {
285 if( edgeIncident(edges[0], n, _first) ) {
299 template<typename Gr>
300 typename Path<Gr>::NodeIt
301 Path<Gr>::tail(const EdgeIt& e) const {
304 if( e.it == edges.end() ) {
305 // FIXME: invalid-> invalid
306 n.idx = length() + 1;
311 n.idx = e.it-edges.begin();
316 template<typename Gr>
317 typename Path<Gr>::NodeIt
318 Path<Gr>::head(const EdgeIt& e) const {
319 if( e.it == edges.end()-1 ) {
323 EdgeIt next_edge = e;
325 return tail(next_edge);
328 template<typename Gr>
329 typename Path<Gr>::GraphEdge
330 Path<Gr>::graphEdge(const EdgeIt& e) const {
331 if( e.it != edges.end() ) {
339 template<typename Gr>
340 typename Path<Gr>::GraphNode
341 Path<Gr>::graphNode(const NodeIt& n) const {
342 if( n.idx < length() ) {
343 return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
345 else if( n.idx == length() ) {
353 template<typename Gr>
354 typename Path<Gr>::EdgeIt& Path<Gr>::nth(EdgeIt &e, size_t k) const {
355 if( k<0 || k>=length() ) {
356 // FIXME: invalid EdgeIt
362 e.it = edges.begin()+k;
364 e.forw = ( G.tail(*e.it) == _first );
367 e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
368 G.tail(*e.it) == G.head(edges[k-1]) );
373 template<typename Gr>
374 typename Path<Gr>::NodeIt& Path<Gr>::nth(NodeIt &n, size_t k) const {
375 if( k<0 || k>length() ) {
376 // FIXME: invalid NodeIt
386 n = tail(nth<EdgeIt>(k));
390 // Reszut konstruktorok:
393 template<typename Gr>
394 Path<Gr>::Path(const Path &P, const EdgeIt &a, const EdgeIt &b) :
395 G(P.G), edges(a.it, b.it) // WARNING: if b.it < a.it this will blow up!
397 if( G.valid(P._first) && a.it < P.edges.end() ) {
398 _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
399 if( b.it < P.edges.end() ) {
400 _last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
408 template<typename Gr>
409 Path<Gr>::Path(const Path &P, const NodeIt &a, const NodeIt &b) :
412 if( !P.valid(a) || !P.valid(b) )
415 int ai = a.idx, bi = b.idx;
420 copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
422 _first = P.graphNode(a);
423 _last = P.graphNode(b);
429 #endif // HUGO_PATH_H