Ut struktura. Elso valtozat.
5 * Class for representing paths in graphs.
18 /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
19 elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
21 template<typename Graph>
25 typedef typename Graph::Edge GraphEdge;
26 typedef typename Graph::Node GraphNode;
32 // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
34 GraphNode _first, _last;
35 typedef std::deque<GraphEdge> Container;
40 Path(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
42 /// Subpath defined by two nodes
43 Path(Path const& P, NodeIt a, NodeIt b) { FIXME; }
44 /// Subpath defined by tow edges. Contains edges in [a,b)
45 Path(Path const& P, EdgeIt a, EdgeIt b) { FIXME; }
47 size_t length() const { return edges.size(); }
48 GraphNode from() const { return _first; }
49 GraphNode to() const { return _last; }
51 NodeIt& first(NodeIt &n) const { return nth(n, 0); }
52 EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
60 NodeIt& nth(NodeIt &, size_t) const;
61 EdgeIt& nth(EdgeIt &, size_t) const;
63 It nth(size_t n) const {
69 bool valid(const NodeIt &n) const { return n.idx <= length(); }
70 bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
72 bool isForward(const EdgeIt &e) const { return e.forw; }
74 EdgeIt& next(EdgeIt &e) const;
75 NodeIt& next(NodeIt &n) const;
76 template <typename It>
77 It getNext(It it) const {
78 It tmp(it); return next(tmp);
81 // A path is constructed using the following four functions.
82 // They return false if the requested operation is inconsistent
83 // with the path constructed so far.
84 // If your path has only one edge you MUST set either "from" or "to"!
85 // So you probably SHOULD call it in any case to be safe (and check the
86 // returned value to check if your path is consistent with your idea).
87 bool pushFront(const GraphEdge &e);
88 bool pushBack(const GraphEdge &e);
89 bool setFrom(const GraphNode &n);
90 bool setTo(const GraphNode &n);
92 // WARNING: these two functions return the head/tail of an edge with
93 // respect to the direction of the path!
94 // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if
95 // P.forward(e) is true (or the edge is a loop)!
96 NodeIt head(const EdgeIt& e) const;
97 NodeIt tail(const EdgeIt& e) const;
99 // FIXME: ezeknek valami jobb nev kellene!!!
100 GraphEdge graphEdge(const EdgeIt& e) const;
101 GraphNode graphNode(const NodeIt& n) const;
104 /*** Iterator classes ***/
108 typename Container::const_iterator it;
111 // FIXME: jarna neki ilyen is...
114 bool forward() const { return forw; }
116 bool operator==(const EdgeIt& e) const { return it==e.it; }
117 bool operator!=(const EdgeIt& e) const { return it!=e.it; }
118 bool operator<(const EdgeIt& e) const { return it<e.it; }
126 bool tail; // Is this node the tail of the edge with same idx?
129 // FIXME: jarna neki ilyen is...
132 bool operator==(const NodeIt& n) const { return idx==n.idx; }
133 bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
134 bool operator<(const NodeIt& n) const { return idx<n.idx; }
138 bool edgeIncident(const GraphEdge &e, const GraphNode &a,
140 bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
143 template<typename Gr>
144 typename Path<Gr>::EdgeIt&
145 Path<Gr>::next(Path::EdgeIt &e) const {
146 if( e.it == edges.end() )
149 GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
152 // Invalid edgeit is always forward :)
153 if( e.it == edges.end() ) {
158 e.forw = ( G.tail(*e.it) == common_node );
162 template<typename Gr>
163 typename Path<Gr>::NodeIt& Path<Gr>::next(NodeIt &n) const {
164 if( n.idx >= length() ) {
171 GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
172 G.tail(edges[n.idx]) );
174 if( n.idx < length() ) {
175 n.tail = ( next_node == G.tail(edges[n.idx]) );
184 template<typename Gr>
185 bool Path<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
187 if( G.tail(e) == a ) {
191 if( G.head(e) == a ) {
198 template<typename Gr>
199 bool Path<Gr>::connectTwoEdges(const GraphEdge &e,
200 const GraphEdge &f) {
201 if( edgeIncident(f, G.tail(e), _last) ) {
205 if( edgeIncident(f, G.head(e), _last) ) {
212 template<typename Gr>
213 bool Path<Gr>::pushFront(const GraphEdge &e) {
214 if( G.valid(_first) ) {
215 if( edgeIncident(e, _first, _first) ) {
222 else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
230 template<typename Gr>
231 bool Path<Gr>::pushBack(const GraphEdge &e) {
232 if( G.valid(_last) ) {
233 if( edgeIncident(e, _last, _last) ) {
240 else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
249 template<typename Gr>
250 bool Path<Gr>::setFrom(const GraphNode &n) {
251 if( G.valid(_first) ) {
256 if( edgeIncident(edges[0], n, _last) ) {
269 template<typename Gr>
270 bool Path<Gr>::setTo(const GraphNode &n) {
271 if( G.valid(_last) ) {
276 if( edgeIncident(edges[0], n, _first) ) {
290 template<typename Gr>
291 typename Path<Gr>::NodeIt
292 Path<Gr>::tail(const EdgeIt& e) const {
295 if( e.it == edges.end() ) {
296 // FIXME: invalid-> invalid
297 n.idx = length() + 1;
302 n.idx = e.it-edges.begin();
306 template<typename Gr>
307 typename Path<Gr>::NodeIt
308 Path<Gr>::head(const EdgeIt& e) const {
309 if( e.it == edges.end()-1 ) {
313 EdgeIt next_edge = e;
315 return tail(next_edge);
318 template<typename Gr>
319 typename Path<Gr>::GraphEdge
320 Path<Gr>::graphEdge(const EdgeIt& e) const {
321 if( e.it != edges.end() ) {
329 template<typename Gr>
330 typename Path<Gr>::GraphNode
331 Path<Gr>::graphNode(const NodeIt& n) const {
332 if( n.idx < length() ) {
333 return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
335 else if( n.idx == length() ) {
343 template<typename Gr>
344 typename Path<Gr>::EdgeIt& Path<Gr>::nth(EdgeIt &e, size_t k) const {
345 if( k<0 || k>=length() ) {
346 // FIXME: invalid EdgeIt
352 e.it = edges.begin()+k;
354 e.forw = ( G.tail(*e.it) == _first );
357 e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
358 G.tail(*e.it) == G.head(edges[k-1]) );
363 template<typename Gr>
364 typename Path<Gr>::NodeIt& Path<Gr>::nth(NodeIt &n, size_t k) const {
365 if( k<0 || k>length() ) {
366 // FIXME: invalid NodeIt
376 n = tail(nth<EdgeIt>(k));
383 #endif // HUGO_PATH_H