src/work/klao/path.h
author beckerjc
Thu, 29 Apr 2004 17:23:56 +0000
changeset 484 13e57edac8ed
parent 434 1ce1b4cd8dd5
child 493 bbd1db03f0fe
permissions -rw-r--r--
Move unionfind.h in Doxyfile too
     1 // -*- c++ -*- //
     2 
     3 ///ingroup datas
     4 ///\file
     5 ///\brief Class for representing paths in graphs.
     6 
     7 #ifndef HUGO_PATH_H
     8 #define HUGO_PATH_H
     9 
    10 #include <deque>
    11 #include <vector>
    12 #include <algorithm>
    13 
    14 #include <invalid.h>
    15 
    16 namespace hugo {
    17 
    18   /// \addtogroup datas
    19   /// @{
    20 
    21   ///A container for directed paths
    22 
    23   ///\param Graph The graph type in which the path is.
    24   ///
    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>
    31   class DirPath {
    32   public:
    33     typedef typename Graph::Edge GraphEdge;
    34     typedef typename Graph::Node GraphNode;
    35     class NodeIt;
    36     class EdgeIt;
    37 
    38   protected:
    39     const Graph *gr;
    40     typedef std::vector<GraphEdge> Container;
    41     Container edges;
    42 
    43   public:
    44 
    45     /// Constructor
    46     
    47     /// \param _G The graph in which the path is.
    48     ///
    49     DirPath(const Graph &_G) : gr(&_G) {}
    50 
    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);
    57 
    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]);
    62     }
    63     GraphNode to() const {
    64       return empty() ? INVALID : gr->head(edges[length()-1]);
    65     }
    66 
    67     template<typename It>
    68     It& first(It &i) const { return i=It(*this); }
    69 
    70     template<typename It>
    71     It& nth(It &i, int n) const { return i=It(*this, n); }
    72 
    73     template<typename It>
    74     bool valid(const It &i) const { return i.valid(); }
    75 
    76     template<typename It>
    77     It& next(It &e) const { return ++e; }
    78 
    79     /// \todo !
    80     NodeIt head(const EdgeIt& e) const;
    81     NodeIt tail(const EdgeIt& e) const;
    82 
    83 
    84     /*** Iterator classes ***/
    85     class EdgeIt {
    86       friend class DirPath;
    87 
    88       int idx;
    89       const DirPath *p;
    90     public:
    91       EdgeIt() {}
    92       EdgeIt(Invalid) : idx(-1), p(0) {}
    93       EdgeIt(const DirPath &_p, int _idx = 0) :
    94 	idx(_idx), p(&_p) { validate(); }
    95 
    96       bool valid() const { return idx!=-1; }
    97 
    98       operator GraphEdge () const {
    99 	return valid() ? p->edges[idx] : INVALID;
   100       }
   101       EdgeIt& operator++() { ++idx; validate(); return *this; }
   102 
   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; }
   106 
   107     private:
   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; }
   111     };
   112 
   113     class NodeIt {
   114       friend class DirPath;
   115 
   116       int idx;
   117       const DirPath *p;
   118     public:
   119       NodeIt() {}
   120       NodeIt(Invalid) : idx(-1), p(0) {}
   121       NodeIt(const DirPath &_p, int _idx = 0) :
   122 	idx(_idx), p(&_p) { validate(); }
   123 
   124       bool valid() const { return idx!=-1; }
   125 
   126       operator const GraphEdge& () const {
   127 	if(idx >= p->length())
   128 	  return p->to();
   129 	else if(idx >= 0)
   130 	  return p->gr->tail(p->edges[idx]);
   131 	else
   132 	  return INVALID;
   133       }
   134       NodeIt& operator++() { ++idx; validate(); return *this; }
   135 
   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; }
   139 
   140     private:
   141       void validate() { if( size_t(idx) > p->length() ) idx=-1; }
   142     };
   143 
   144     friend class Builder;    
   145 
   146     ///Class to build paths
   147 
   148     ///\ingroup datas
   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.
   153     class Builder {
   154       DirPath &P;
   155       Container d;
   156 
   157     public:
   158       ///Constructor
   159 
   160       ///\param _P the path you want to build.
   161       ///
   162       Builder(DirPath &_P) : P(_P) {}
   163 
   164       ///Set the first node of the path.
   165       
   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 &) { }
   170       
   171       ///Push a new edge to the front of the path
   172 
   173       ///Push a new edge to the front of the path.
   174       ///\sa setFirst()
   175       bool pushFront(const GraphEdge& e) {
   176 	if( empty() || P.gr->head(e)==from() ) {
   177 	  d.push_back(e);
   178 	  return true;
   179 	}
   180 	return false;
   181       }
   182       ///Push a new edge to the back of the path
   183 
   184       ///Push a new edge to the back of the path.
   185       ///\sa setFirst()
   186       bool pushBack(const GraphEdge& e) {
   187 	if( empty() || P.gr->tail(e)==to() ) {
   188 	  P.edges.push_back(e);
   189 	  return true;
   190 	}
   191 	return false;
   192       }
   193 
   194       ///Commit the changes to the path.
   195       void commit() {
   196 	if( !d.empty() ) {
   197 	  P.edges.insert(P.edges.begin(), d.rbegin(), d.rend());
   198 	  d.clear();
   199 	}
   200       }
   201 
   202       ///Desctuctor
   203 
   204       ///The desctuctor.
   205       ///It commit also commit the changes.
   206       ///\todo Is this what we want?
   207       ~Builder() { commit(); }
   208 
   209       // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
   210       // Hogy kenyelmes egy ilyet hasznalni?
   211       void reserve(size_t r) {
   212 	d.reserve(r);
   213 	P.edges.reserve(P.length()+r);
   214       }
   215 
   216     private:
   217       bool empty() { return d.empty() && P.empty(); }
   218 
   219       GraphNode from() const {
   220 	if( ! d.empty() )
   221 	  return P.gr->tail(d[d.size()-1]);
   222 	else if( ! P.empty() )
   223 	  return P.gr->tail(P.edges[0]);
   224 	else
   225 	  return INVALID;
   226       }
   227       GraphNode to() const {
   228 	if( ! P.empty() )
   229 	  return P.gr->head(P.edges[P.length()-1]);
   230 	else if( ! d.empty() )
   231 	  return P.gr->head(d[0]);
   232 	else
   233 	  return INVALID;
   234       }
   235 
   236     };
   237 
   238   };
   239 
   240 
   241 
   242 
   243 
   244 
   245 
   246 
   247 
   248 
   249   /**********************************************************************/
   250 
   251 
   252   /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
   253      elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
   254 
   255   template<typename Graph>
   256   class DynamicPath {
   257 
   258   public:
   259     typedef typename Graph::Edge GraphEdge;
   260     typedef typename Graph::Node GraphNode;
   261     class NodeIt;
   262     class EdgeIt;
   263 
   264   protected:
   265     Graph& G;
   266     // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
   267     // iranyitasat:
   268     GraphNode _first, _last;
   269     typedef std::deque<GraphEdge> Container;
   270     Container edges;
   271 
   272   public:
   273 
   274     DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
   275 
   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);
   283     
   284     size_t length() const { return edges.size(); }
   285     GraphNode from() const { return _first; }
   286     GraphNode to() const { return _last; }
   287 
   288     NodeIt& first(NodeIt &n) const { return nth(n, 0); }
   289     EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
   290     template<typename It>
   291     It first() const { 
   292       It e;
   293       first(e);
   294       return e; 
   295     }
   296 
   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 { 
   301       It e;
   302       nth(e, n);
   303       return e; 
   304     }
   305 
   306     bool valid(const NodeIt &n) const { return n.idx <= length(); }
   307     bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
   308 
   309     bool isForward(const EdgeIt &e) const { return e.forw; }
   310 
   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(); }
   315 
   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);
   321     }
   322 
   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);
   333 
   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;
   340 
   341     // FIXME: ezeknek valami jobb nev kellene!!!
   342     GraphEdge graphEdge(const EdgeIt& e) const;
   343     GraphNode graphNode(const NodeIt& n) const;
   344 
   345 
   346     /*** Iterator classes ***/
   347     class EdgeIt {
   348       friend class DynamicPath;
   349 
   350       typename Container::const_iterator it;
   351       bool forw;
   352     public:
   353       // FIXME: jarna neki ilyen is...
   354       // EdgeIt(Invalid);
   355 
   356       bool forward() const { return forw; }
   357 
   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; }
   361     };
   362 
   363     class NodeIt {
   364       friend class DynamicPath;
   365 
   366       size_t idx;
   367       bool tail;  // Is this node the tail of the edge with same idx?
   368 
   369     public:
   370       // FIXME: jarna neki ilyen is...
   371       // NodeIt(Invalid);
   372 
   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; }
   376     };
   377 
   378   private:
   379     bool edgeIncident(const GraphEdge &e, const GraphNode &a,
   380 		      GraphNode &b);
   381     bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
   382   };
   383 
   384   template<typename Gr>
   385   typename DynamicPath<Gr>::EdgeIt&
   386   DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
   387     if( e.it == edges.end() ) 
   388       return e;
   389 
   390     GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
   391     ++e.it;
   392 
   393     // Invalid edgeit is always forward :)
   394     if( e.it == edges.end() ) {
   395       e.forw = true;
   396       return e;
   397     }
   398 
   399     e.forw = ( G.tail(*e.it) == common_node );
   400     return e;
   401   }
   402 
   403   template<typename Gr>
   404   typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
   405     if( n.idx >= length() ) {
   406       // FIXME: invalid
   407       n.idx = length()+1;
   408       return n;
   409     }
   410 
   411     
   412     GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
   413 			      G.tail(edges[n.idx]) );
   414     ++n.idx;
   415     if( n.idx < length() ) {
   416       n.tail = ( next_node == G.tail(edges[n.idx]) );
   417     }
   418     else {
   419       n.tail = true;
   420     }
   421 
   422     return n;
   423   }
   424 
   425   template<typename Gr>
   426   bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
   427 			  GraphNode &b) {
   428     if( G.tail(e) == a ) {
   429       b=G.head(e);
   430       return true;
   431     }
   432     if( G.head(e) == a ) {
   433       b=G.tail(e);
   434       return true;
   435     }
   436     return false;
   437   }
   438 
   439   template<typename Gr>
   440   bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
   441 			     const GraphEdge &f) {
   442     if( edgeIncident(f, G.tail(e), _last) ) {
   443       _first = G.head(e);
   444       return true;
   445     }
   446     if( edgeIncident(f, G.head(e), _last) ) {
   447       _first = G.tail(e);
   448       return true;
   449     }
   450     return false;
   451   }
   452 
   453   template<typename Gr>
   454   bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
   455     if( G.valid(_first) ) {
   456 	if( edgeIncident(e, _first, _first) ) {
   457 	  edges.push_front(e);
   458 	  return true;
   459 	}
   460 	else
   461 	  return false;
   462     }
   463     else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
   464       edges.push_front(e);
   465       return true;
   466     }
   467     else
   468       return false;
   469   }
   470 
   471   template<typename Gr>
   472   bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
   473     if( G.valid(_last) ) {
   474 	if( edgeIncident(e, _last, _last) ) {
   475 	  edges.push_back(e);
   476 	  return true;
   477 	}
   478 	else
   479 	  return false;
   480     }
   481     else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
   482       edges.push_back(e);
   483       return true;
   484     }
   485     else
   486       return false;
   487   }
   488 
   489 
   490   template<typename Gr>
   491   bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
   492     if( G.valid(_first) ) {
   493       return _first == n;
   494     }
   495     else {
   496       if( length() > 0) {
   497 	if( edgeIncident(edges[0], n, _last) ) {
   498 	  _first = n;
   499 	  return true;
   500 	}
   501 	else return false;
   502       }
   503       else {
   504 	_first = _last = n;
   505 	return true;
   506       }
   507     }
   508   }
   509 
   510   template<typename Gr>
   511   bool DynamicPath<Gr>::setTo(const GraphNode &n) {
   512     if( G.valid(_last) ) {
   513       return _last == n;
   514     }
   515     else {
   516       if( length() > 0) {
   517 	if( edgeIncident(edges[0], n, _first) ) {
   518 	  _last = n;
   519 	  return true;
   520 	}
   521 	else return false;
   522       }
   523       else {
   524 	_first = _last = n;
   525 	return true;
   526       }
   527     }
   528   }
   529 
   530 
   531   template<typename Gr>
   532   typename DynamicPath<Gr>::NodeIt
   533   DynamicPath<Gr>::tail(const EdgeIt& e) const {
   534     NodeIt n;
   535 
   536     if( e.it == edges.end() ) {
   537       // FIXME: invalid-> invalid
   538       n.idx = length() + 1;
   539       n.tail = true;
   540       return n;
   541     }
   542 
   543     n.idx = e.it-edges.begin();
   544     n.tail = e.forw;
   545     return n;
   546   }
   547 
   548   template<typename Gr>
   549   typename DynamicPath<Gr>::NodeIt
   550   DynamicPath<Gr>::head(const EdgeIt& e) const {
   551     if( e.it == edges.end()-1 ) {
   552       return _last;
   553     }
   554 
   555     EdgeIt next_edge = e;
   556     next(next_edge);
   557     return tail(next_edge);
   558   }
   559       
   560   template<typename Gr>
   561   typename DynamicPath<Gr>::GraphEdge
   562   DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
   563     if( e.it != edges.end() ) {
   564       return *e.it;
   565     }
   566     else {
   567       return INVALID;
   568     }
   569   }
   570   
   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]);
   576     }
   577     else if( n.idx == length() ) {
   578       return _last;
   579     }
   580     else {
   581       return INVALID;
   582     }
   583   }
   584 
   585   template<typename Gr>
   586   typename DynamicPath<Gr>::EdgeIt&
   587   DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
   588     if( k>=length() ) {
   589       // FIXME: invalid EdgeIt
   590       e.it = edges.end();
   591       e.forw = true;
   592       return e;
   593     }
   594 
   595     e.it = edges.begin()+k;
   596     if(k==0) {
   597       e.forw = ( G.tail(*e.it) == _first );
   598     }
   599     else {
   600       e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
   601 		 G.tail(*e.it) == G.head(edges[k-1]) );
   602     }
   603     return e;
   604   }
   605     
   606   template<typename Gr>
   607   typename DynamicPath<Gr>::NodeIt&
   608   DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
   609     if( k>length() ) {
   610       // FIXME: invalid NodeIt
   611       n.idx = length()+1;
   612       n.tail = true;
   613       return n;
   614     }
   615     if( k==length() ) {
   616       n.idx = length();
   617       n.tail = true;
   618       return n;
   619     }
   620     n = tail(nth<EdgeIt>(k));
   621     return n;
   622   }
   623 
   624   // Reszut konstruktorok:
   625 
   626 
   627   template<typename Gr>
   628   DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
   629 			       const EdgeIt &b) :
   630     G(P.G), edges(a.it, b.it)    // WARNING: if b.it < a.it this will blow up! 
   631   {
   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) );
   636       }
   637       else {
   638 	_last = P._last;
   639       }
   640     }
   641   }
   642 
   643   template<typename Gr>
   644   DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
   645 			       const NodeIt &b) : G(P.G)
   646   {
   647     if( !P.valid(a) || !P.valid(b) )
   648       return;
   649 
   650     int ai = a.idx, bi = b.idx;
   651     if( bi<ai )
   652       std::swap(ai,bi);
   653     
   654     edges.resize(bi-ai);
   655     copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
   656 
   657     _first = P.graphNode(a);
   658     _last = P.graphNode(b);
   659   }
   660 
   661   ///@}
   662 
   663 } // namespace hugo
   664 
   665 #endif // HUGO_PATH_H