hegyi@815: // -*- c++ -*- //
hegyi@815: 
hegyi@815: /**
hegyi@815: @defgroup paths Path Structures
hegyi@815: @ingroup datas
hegyi@815: \brief Path structures implemented in Hugo.
hegyi@815: 
hegyi@815: Hugolib provides flexible data structures
hegyi@815: to work with paths.
hegyi@815: 
hegyi@815: All of them have the same interface, especially they can be built or extended
hegyi@815: using a standard Builder subclass. This make is easy to have e.g. the Dijkstra
hegyi@815: algorithm to store its result in any kind of path structure.
hegyi@815: 
hegyi@815: \sa hugo::skeleton::Path
hegyi@815: 
hegyi@815: */
hegyi@815: 
hegyi@815: ///\ingroup paths
hegyi@815: ///\file
hegyi@815: ///\brief Classes for representing paths in graphs.
hegyi@815: 
hegyi@815: #ifndef HUGO_PATH_H
hegyi@815: #define HUGO_PATH_H
hegyi@815: 
hegyi@815: #include <deque>
hegyi@815: #include <vector>
hegyi@815: #include <algorithm>
hegyi@815: 
hegyi@815: #include <hugo/invalid.h>
hegyi@815: #include <hugo/error.h>
hegyi@815: #include <debug.h>
hegyi@815: 
hegyi@815: namespace hugo {
hegyi@815: 
hegyi@815:   /// \addtogroup paths
hegyi@815:   /// @{
hegyi@815: 
hegyi@815: 
hegyi@815:   //! \brief A structure for representing directed paths in a graph.
hegyi@815:   //!
hegyi@815:   //! A structure for representing directed path in a graph.
hegyi@815:   //! \param Graph The graph type in which the path is.
hegyi@815:   //! \param DM DebugMode, defaults to DefaultDebugMode.
hegyi@815:   //! 
hegyi@815:   //! In a sense, the path can be treated as a graph, for is has \c NodeIt
hegyi@815:   //! and \c EdgeIt with the same usage. These types converts to the \c Node
hegyi@815:   //! and \c Edge of the original graph.
hegyi@815:   //!
hegyi@815:   //! \todo Thoroughfully check all the range and consistency tests.
hegyi@815:   template<typename Graph, typename DM = DefaultDebugMode>
hegyi@815:   class DirPath {
hegyi@815:   public:
hegyi@815:     /// Edge type of the underlying graph.
hegyi@815:     typedef typename Graph::Edge GraphEdge; 
hegyi@815:     /// Node type of the underlying graph.
hegyi@815:     typedef typename Graph::Node GraphNode;
hegyi@815:     class NodeIt;
hegyi@815:     class EdgeIt;
hegyi@815: 
hegyi@815:   protected:
hegyi@815:     const Graph *gr;
hegyi@815:     typedef std::vector<GraphEdge> Container;
hegyi@815:     Container edges;
hegyi@815: 
hegyi@815:   public:
hegyi@815: 
hegyi@815:     /// \param _G The graph in which the path is.
hegyi@815:     ///
hegyi@815:     DirPath(const Graph &_G) : gr(&_G) {}
hegyi@815: 
hegyi@815:     /// \brief Subpath constructor.
hegyi@815:     ///
hegyi@815:     /// Subpath defined by two nodes.
hegyi@815:     /// \warning It is an error if the two edges are not in order!
hegyi@815:     DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
hegyi@815:       if( DM::range_check && (!a.valid() || !b.valid) ) {
hegyi@815: 	// FIXME: this check should be more elaborate...
hegyi@815: 	fault("DirPath, subpath ctor: invalid bounding nodes");
hegyi@815:       }
hegyi@815:       gr = P.gr;
hegyi@815:       edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Subpath constructor.
hegyi@815:     ///
hegyi@815:     /// Subpath defined by two edges. Contains edges in [a,b)
hegyi@815:     /// \warning It is an error if the two edges are not in order!
hegyi@815:     DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
hegyi@815:       if( DM::range_check && (!a.valid() || !b.valid) ) {
hegyi@815: 	// FIXME: this check should be more elaborate...
hegyi@815: 	fault("DirPath, subpath ctor: invalid bounding nodes");
hegyi@815:       }
hegyi@815:       gr = P.gr;
hegyi@815:       edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// Length of the path.
hegyi@815:     size_t length() const { return edges.size(); }
hegyi@815:     /// Returns whether the path is empty.
hegyi@815:     bool empty() const { return edges.empty(); }
hegyi@815: 
hegyi@815:     /// Resets the path to an empty path.
hegyi@815:     void clear() { edges.clear(); }
hegyi@815: 
hegyi@815:     /// \brief Starting point of the path.
hegyi@815:     ///
hegyi@815:     /// Starting point of the path.
hegyi@815:     /// Returns INVALID if the path is empty.
hegyi@815:     GraphNode from() const {
hegyi@815:       return empty() ? INVALID : gr->tail(edges[0]);
hegyi@815:     }
hegyi@815:     /// \brief End point of the path.
hegyi@815:     ///
hegyi@815:     /// End point of the path.
hegyi@815:     /// Returns INVALID if the path is empty.
hegyi@815:     GraphNode to() const {
hegyi@815:       return empty() ? INVALID : gr->head(edges[length()-1]);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Initializes node or edge iterator to point to the first
hegyi@815:     /// node or edge.
hegyi@815:     ///
hegyi@815:     /// \sa nth
hegyi@815:     template<typename It>
hegyi@815:     It& first(It &i) const { return i=It(*this); }
hegyi@815: 
hegyi@815:     /// \brief Initializes node iterator to point to the node of a given index.
hegyi@815:     NodeIt& nth(NodeIt &i, int n) const {
hegyi@815:       if( DM::range_check && (n<0 || n>int(length())) )
hegyi@815: 	fault("DirPath::nth: index out of range");
hegyi@815:       return i=NodeIt(*this, n);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Initializes edge iterator to point to the edge of a given index.
hegyi@815:     EdgeIt& nth(EdgeIt &i, int n) const {
hegyi@815:       if( DM::range_check && (n<0 || n>=int(length())) )
hegyi@815: 	fault("DirPath::nth: index out of range");
hegyi@815:       return i=EdgeIt(*this, n);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// Checks validity of a node or edge iterator.
hegyi@815:     template<typename It>
hegyi@815:     static
hegyi@815:     bool valid(const It &i) { return i.valid(); }
hegyi@815: 
hegyi@815:     /// Steps the given node or edge iterator.
hegyi@815:     template<typename It>
hegyi@815:     static
hegyi@815:     It& next(It &e) {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("DirPath::next() on invalid iterator");
hegyi@815:       return ++e;
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Returns node iterator pointing to the head node of the
hegyi@815:     /// given edge iterator.
hegyi@815:     NodeIt head(const EdgeIt& e) const {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("DirPath::head() on invalid iterator");
hegyi@815:       return NodeIt(*this, e.idx+1);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Returns node iterator pointing to the tail node of the
hegyi@815:     /// given edge iterator.
hegyi@815:     NodeIt tail(const EdgeIt& e) const {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("DirPath::tail() on invalid iterator");
hegyi@815:       return NodeIt(*this, e.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815: 
hegyi@815:     /* Iterator classes */
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Iterator class to iterate on the edges of the paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to iterate on the edges of the paths
hegyi@815:      *
hegyi@815:      * Of course it converts to Graph::Edge
hegyi@815:      * 
hegyi@815:      * \todo Its interface differs from the standard edge iterator.
hegyi@815:      * Yes, it shouldn't.
hegyi@815:      */
hegyi@815:     class EdgeIt {
hegyi@815:       friend class DirPath;
hegyi@815: 
hegyi@815:       int idx;
hegyi@815:       const DirPath *p;
hegyi@815:     public:
hegyi@815:       /// Default constructor
hegyi@815:       EdgeIt() {}
hegyi@815:       /// Invalid constructor
hegyi@815:       EdgeIt(Invalid) : idx(-1), p(0) {}
hegyi@815:       /// Constructor with starting point
hegyi@815:       EdgeIt(const DirPath &_p, int _idx = 0) :
hegyi@815: 	idx(_idx), p(&_p) { validate(); }
hegyi@815: 
hegyi@815:       ///Validity check
hegyi@815:       bool valid() const { return idx!=-1; }
hegyi@815: 
hegyi@815:       ///Conversion to Graph::Edge
hegyi@815:       operator GraphEdge () const {
hegyi@815: 	return valid() ? p->edges[idx] : INVALID;
hegyi@815:       }
hegyi@815: 
hegyi@815:       /// Next edge
hegyi@815:       EdgeIt& operator++() { ++idx; validate(); return *this; }
hegyi@815: 
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator==(const EdgeIt& e) const { return idx==e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator<(const EdgeIt& e) const { return idx<e.idx; }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       // FIXME: comparison between signed and unsigned...
hegyi@815:       // Jo ez igy? Vagy esetleg legyen a length() int?
hegyi@815:       void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
hegyi@815:     };
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Iterator class to iterate on the nodes of the paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to iterate on the nodes of the paths
hegyi@815:      *
hegyi@815:      * Of course it converts to Graph::Node
hegyi@815:      * 
hegyi@815:      * \todo Its interface differs from the standard node iterator.
hegyi@815:      * Yes, it shouldn't.
hegyi@815:      */
hegyi@815:     class NodeIt {
hegyi@815:       friend class DirPath;
hegyi@815: 
hegyi@815:       int idx;
hegyi@815:       const DirPath *p;
hegyi@815:     public:
hegyi@815:       /// Default constructor
hegyi@815:       NodeIt() {}
hegyi@815:       /// Invalid constructor
hegyi@815:       NodeIt(Invalid) : idx(-1), p(0) {}
hegyi@815:       /// Constructor with starting point
hegyi@815:       NodeIt(const DirPath &_p, int _idx = 0) :
hegyi@815: 	idx(_idx), p(&_p) { validate(); }
hegyi@815: 
hegyi@815:       ///Validity check
hegyi@815:       bool valid() const { return idx!=-1; }
hegyi@815: 
hegyi@815:       ///Conversion to Graph::Node
hegyi@815:       operator const GraphNode& () const {
hegyi@815: 	if(idx >= p->length())
hegyi@815: 	  return p->to();
hegyi@815: 	else if(idx >= 0)
hegyi@815: 	  return p->gr->tail(p->edges[idx]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815:       /// Next node
hegyi@815:       NodeIt& operator++() { ++idx; validate(); return *this; }
hegyi@815: 
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator==(const NodeIt& e) const { return idx==e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator<(const NodeIt& e) const { return idx<e.idx; }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       void validate() { if( size_t(idx) > p->length() ) idx=-1; }
hegyi@815:     };
hegyi@815: 
hegyi@815:     friend class Builder;    
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Class to build paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to fill a path with edges.
hegyi@815:      *
hegyi@815:      * You can push new edges to the front and to the back of the path in
hegyi@815:      * arbitrary order then you should commit these changes to the graph.
hegyi@815:      *
hegyi@815:      * Fundamentally, for most "Paths" (classes fulfilling the
hegyi@815:      * PathConcept) while the builder is active (after the first modifying
hegyi@815:      * operation and until the commit()) the original Path is in a
hegyi@815:      * "transitional" state (operations on it have undefined result). But
hegyi@815:      * in the case of DirPath the original path remains unchanged until the
hegyi@815:      * commit. However we don't recomend that you use this feature.
hegyi@815:      */
hegyi@815:     class Builder {
hegyi@815:       DirPath &P;
hegyi@815:       Container front, back;
hegyi@815: 
hegyi@815:     public:
hegyi@815:       ///\param _P the path you want to fill in.
hegyi@815:       ///
hegyi@815:       Builder(DirPath &_P) : P(_P) {}
hegyi@815: 
hegyi@815:       /// Sets the starting node of the path.
hegyi@815:       
hegyi@815:       /// Sets the starting node of the path. Edge added to the path
hegyi@815:       /// afterwards have to be incident to this node.
hegyi@815:       /// It should be called iff the path is empty and before any call to
hegyi@815:       /// \ref pushFront() or \ref pushBack()
hegyi@815:       void setStartNode(const GraphNode &) {}
hegyi@815: 
hegyi@815:       ///Push a new edge to the front of the path
hegyi@815: 
hegyi@815:       ///Push a new edge to the front of the path.
hegyi@815:       ///\sa setStartNode
hegyi@815:       void pushFront(const GraphEdge& e) {
hegyi@815: 	if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
hegyi@815: 	  fault("DirPath::Builder::pushFront: nonincident edge");
hegyi@815: 	}
hegyi@815: 	front.push_back(e);
hegyi@815:       }
hegyi@815: 
hegyi@815:       ///Push a new edge to the back of the path
hegyi@815: 
hegyi@815:       ///Push a new edge to the back of the path.
hegyi@815:       ///\sa setStartNode
hegyi@815:       void pushBack(const GraphEdge& e) {
hegyi@815: 	if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
hegyi@815: 	  fault("DirPath::Builder::pushBack: nonincident edge");
hegyi@815: 	}
hegyi@815: 	back.push_back(e);
hegyi@815:       }
hegyi@815: 
hegyi@815:       ///Commit the changes to the path.
hegyi@815:       void commit() {
hegyi@815: 	if( !(front.empty() && back.empty()) ) {
hegyi@815: 	  Container tmp;
hegyi@815: 	  tmp.reserve(front.size()+back.size()+P.length());
hegyi@815: 	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
hegyi@815: 	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
hegyi@815: 	  tmp.insert(tmp.end(), back.begin(), back.end());
hegyi@815: 	  P.edges.swap(tmp);
hegyi@815: 	  front.clear();
hegyi@815: 	  back.clear();
hegyi@815: 	}
hegyi@815:       }
hegyi@815: 
hegyi@815:       // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
hegyi@815:       // Hogy kenyelmes egy ilyet hasznalni?
hegyi@815:   
hegyi@815:       ///Reserve storage for the builder in advance.
hegyi@815: 
hegyi@815:       ///If you know an reasonable upper bound of the number of the edges
hegyi@815:       ///to add, using this function you can speed up the building.
hegyi@815:       void reserve(size_t r) {
hegyi@815: 	front.reserve(r);
hegyi@815: 	back.reserve(r);
hegyi@815:       }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       bool empty() {
hegyi@815: 	return front.empty() && back.empty() && P.empty();
hegyi@815:       }
hegyi@815: 
hegyi@815:       GraphNode from() const {
hegyi@815: 	if( ! front.empty() )
hegyi@815: 	  return P.gr->tail(front[front.size()-1]);
hegyi@815: 	else if( ! P.empty() )
hegyi@815: 	  return P.gr->tail(P.edges[0]);
hegyi@815: 	else if( ! back.empty() )
hegyi@815: 	  return P.gr->tail(back[0]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815:       GraphNode to() const {
hegyi@815: 	if( ! back.empty() )
hegyi@815: 	  return P.gr->head(back[back.size()-1]);
hegyi@815: 	else if( ! P.empty() )
hegyi@815: 	  return P.gr->head(P.edges[P.length()-1]);
hegyi@815: 	else if( ! front.empty() )
hegyi@815: 	  return P.gr->head(front[0]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815: 
hegyi@815:     };
hegyi@815: 
hegyi@815:   };
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815:   /**********************************************************************/
hegyi@815: 
hegyi@815: 
hegyi@815:   //! \brief A structure for representing undirected path in a graph.
hegyi@815:   //!
hegyi@815:   //! A structure for representing undirected path in a graph. Ie. this is
hegyi@815:   //! a path in a \e directed graph but the edges should not be directed
hegyi@815:   //! forward.
hegyi@815:   //!
hegyi@815:   //! \param Graph The graph type in which the path is.
hegyi@815:   //! \param DM DebugMode, defaults to DefaultDebugMode.
hegyi@815:   //! 
hegyi@815:   //! In a sense, the path can be treated as a graph, for is has \c NodeIt
hegyi@815:   //! and \c EdgeIt with the same usage. These types converts to the \c Node
hegyi@815:   //! and \c Edge of the original graph.
hegyi@815:   //!
hegyi@815:   //! \todo Thoroughfully check all the range and consistency tests.
hegyi@815:   template<typename Graph, typename DM = DefaultDebugMode>
hegyi@815:   class UndirPath {
hegyi@815:   public:
hegyi@815:     /// Edge type of the underlying graph.
hegyi@815:     typedef typename Graph::Edge GraphEdge;
hegyi@815:      /// Node type of the underlying graph.
hegyi@815:    typedef typename Graph::Node GraphNode;
hegyi@815:     class NodeIt;
hegyi@815:     class EdgeIt;
hegyi@815: 
hegyi@815:   protected:
hegyi@815:     const Graph *gr;
hegyi@815:     typedef std::vector<GraphEdge> Container;
hegyi@815:     Container edges;
hegyi@815: 
hegyi@815:   public:
hegyi@815: 
hegyi@815:     /// \param _G The graph in which the path is.
hegyi@815:     ///
hegyi@815:     UndirPath(const Graph &_G) : gr(&_G) {}
hegyi@815: 
hegyi@815:     /// \brief Subpath constructor.
hegyi@815:     ///
hegyi@815:     /// Subpath defined by two nodes.
hegyi@815:     /// \warning It is an error if the two edges are not in order!
hegyi@815:     UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
hegyi@815:       if( DM::range_check && (!a.valid() || !b.valid) ) {
hegyi@815: 	// FIXME: this check should be more elaborate...
hegyi@815: 	fault("UndirPath, subpath ctor: invalid bounding nodes");
hegyi@815:       }
hegyi@815:       gr = P.gr;
hegyi@815:       edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Subpath constructor.
hegyi@815:     ///
hegyi@815:     /// Subpath defined by two edges. Contains edges in [a,b)
hegyi@815:     /// \warning It is an error if the two edges are not in order!
hegyi@815:     UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
hegyi@815:       if( DM::range_check && (!a.valid() || !b.valid) ) {
hegyi@815: 	// FIXME: this check should be more elaborate...
hegyi@815: 	fault("UndirPath, subpath ctor: invalid bounding nodes");
hegyi@815:       }
hegyi@815:       gr = P.gr;
hegyi@815:       edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// Length of the path.
hegyi@815:     size_t length() const { return edges.size(); }
hegyi@815:     /// Returns whether the path is empty.
hegyi@815:     bool empty() const { return edges.empty(); }
hegyi@815: 
hegyi@815:     /// Resets the path to an empty path.
hegyi@815:     void clear() { edges.clear(); }
hegyi@815: 
hegyi@815:     /// \brief Starting point of the path.
hegyi@815:     ///
hegyi@815:     /// Starting point of the path.
hegyi@815:     /// Returns INVALID if the path is empty.
hegyi@815:     GraphNode from() const {
hegyi@815:       return empty() ? INVALID : gr->tail(edges[0]);
hegyi@815:     }
hegyi@815:     /// \brief End point of the path.
hegyi@815:     ///
hegyi@815:     /// End point of the path.
hegyi@815:     /// Returns INVALID if the path is empty.
hegyi@815:     GraphNode to() const {
hegyi@815:       return empty() ? INVALID : gr->head(edges[length()-1]);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Initializes node or edge iterator to point to the first
hegyi@815:     /// node or edge.
hegyi@815:     ///
hegyi@815:     /// \sa nth
hegyi@815:     template<typename It>
hegyi@815:     It& first(It &i) const { return i=It(*this); }
hegyi@815: 
hegyi@815:     /// \brief Initializes node iterator to point to the node of a given index.
hegyi@815:     NodeIt& nth(NodeIt &i, int n) const {
hegyi@815:       if( DM::range_check && (n<0 || n>int(length())) )
hegyi@815: 	fault("UndirPath::nth: index out of range");
hegyi@815:       return i=NodeIt(*this, n);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Initializes edge iterator to point to the edge of a given index.
hegyi@815:     EdgeIt& nth(EdgeIt &i, int n) const {
hegyi@815:       if( DM::range_check && (n<0 || n>=int(length())) )
hegyi@815: 	fault("UndirPath::nth: index out of range");
hegyi@815:       return i=EdgeIt(*this, n);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// Checks validity of a node or edge iterator.
hegyi@815:     template<typename It>
hegyi@815:     static
hegyi@815:     bool valid(const It &i) { return i.valid(); }
hegyi@815: 
hegyi@815:     /// Steps the given node or edge iterator.
hegyi@815:     template<typename It>
hegyi@815:     static
hegyi@815:     It& next(It &e) {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("UndirPath::next() on invalid iterator");
hegyi@815:       return ++e;
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Returns node iterator pointing to the head node of the
hegyi@815:     /// given edge iterator.
hegyi@815:     NodeIt head(const EdgeIt& e) const {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("UndirPath::head() on invalid iterator");
hegyi@815:       return NodeIt(*this, e.idx+1);
hegyi@815:     }
hegyi@815: 
hegyi@815:     /// \brief Returns node iterator pointing to the tail node of the
hegyi@815:     /// given edge iterator.
hegyi@815:     NodeIt tail(const EdgeIt& e) const {
hegyi@815:       if( DM::range_check && !e.valid() )
hegyi@815: 	fault("UndirPath::tail() on invalid iterator");
hegyi@815:       return NodeIt(*this, e.idx);
hegyi@815:     }
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Iterator class to iterate on the edges of the paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to iterate on the edges of the paths
hegyi@815:      *
hegyi@815:      * Of course it converts to Graph::Edge
hegyi@815:      * 
hegyi@815:      * \todo Its interface differs from the standard edge iterator.
hegyi@815:      * Yes, it shouldn't.
hegyi@815:      */
hegyi@815:     class EdgeIt {
hegyi@815:       friend class UndirPath;
hegyi@815: 
hegyi@815:       int idx;
hegyi@815:       const UndirPath *p;
hegyi@815:     public:
hegyi@815:       /// Default constructor
hegyi@815:       EdgeIt() {}
hegyi@815:       /// Invalid constructor
hegyi@815:       EdgeIt(Invalid) : idx(-1), p(0) {}
hegyi@815:       /// Constructor with starting point
hegyi@815:       EdgeIt(const UndirPath &_p, int _idx = 0) :
hegyi@815: 	idx(_idx), p(&_p) { validate(); }
hegyi@815: 
hegyi@815:       ///Validity check
hegyi@815:       bool valid() const { return idx!=-1; }
hegyi@815: 
hegyi@815:       ///Conversion to Graph::Edge
hegyi@815:       operator GraphEdge () const {
hegyi@815: 	return valid() ? p->edges[idx] : INVALID;
hegyi@815:       }
hegyi@815:       /// Next edge
hegyi@815:      EdgeIt& operator++() { ++idx; validate(); return *this; }
hegyi@815: 
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator==(const EdgeIt& e) const { return idx==e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator<(const EdgeIt& e) const { return idx<e.idx; }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       // FIXME: comparison between signed and unsigned...
hegyi@815:       // Jo ez igy? Vagy esetleg legyen a length() int?
hegyi@815:       void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
hegyi@815:     };
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Iterator class to iterate on the nodes of the paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to iterate on the nodes of the paths
hegyi@815:      *
hegyi@815:      * Of course it converts to Graph::Node
hegyi@815:      * 
hegyi@815:      * \todo Its interface differs from the standard node iterator.
hegyi@815:      * Yes, it shouldn't.
hegyi@815:      */
hegyi@815:     class NodeIt {
hegyi@815:       friend class UndirPath;
hegyi@815: 
hegyi@815:       int idx;
hegyi@815:       const UndirPath *p;
hegyi@815:     public:
hegyi@815:       /// Default constructor
hegyi@815:       NodeIt() {}
hegyi@815:       /// Invalid constructor
hegyi@815:       NodeIt(Invalid) : idx(-1), p(0) {}
hegyi@815:       /// Constructor with starting point
hegyi@815:       NodeIt(const UndirPath &_p, int _idx = 0) :
hegyi@815: 	idx(_idx), p(&_p) { validate(); }
hegyi@815: 
hegyi@815:       ///Validity check
hegyi@815:       bool valid() const { return idx!=-1; }
hegyi@815: 
hegyi@815:       ///Conversion to Graph::Node
hegyi@815:       operator const GraphNode& () const {
hegyi@815: 	if(idx >= p->length())
hegyi@815: 	  return p->to();
hegyi@815: 	else if(idx >= 0)
hegyi@815: 	  return p->gr->tail(p->edges[idx]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815:       /// Next node
hegyi@815:       NodeIt& operator++() { ++idx; validate(); return *this; }
hegyi@815: 
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator==(const NodeIt& e) const { return idx==e.idx; }
hegyi@815:       /// Comparison operator
hegyi@815:       bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
hegyi@815:        /// Comparison operator
hegyi@815:      bool operator<(const NodeIt& e) const { return idx<e.idx; }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       void validate() { if( size_t(idx) > p->length() ) idx=-1; }
hegyi@815:     };
hegyi@815: 
hegyi@815:     friend class Builder;    
hegyi@815: 
hegyi@815:     /**
hegyi@815:      * \brief Class to build paths
hegyi@815:      * 
hegyi@815:      * \ingroup paths
hegyi@815:      * This class is used to fill a path with edges.
hegyi@815:      *
hegyi@815:      * You can push new edges to the front and to the back of the path in
hegyi@815:      * arbitrary order then you should commit these changes to the graph.
hegyi@815:      *
hegyi@815:      * Fundamentally, for most "Paths" (classes fulfilling the
hegyi@815:      * PathConcept) while the builder is active (after the first modifying
hegyi@815:      * operation and until the commit()) the original Path is in a
hegyi@815:      * "transitional" state (operations ot it have undefined result). But
hegyi@815:      * in the case of UndirPath the original path is unchanged until the
hegyi@815:      * commit. However we don't recomend that you use this feature.
hegyi@815:      */
hegyi@815:     class Builder {
hegyi@815:       UndirPath &P;
hegyi@815:       Container front, back;
hegyi@815: 
hegyi@815:     public:
hegyi@815:       ///\param _P the path you want to fill in.
hegyi@815:       ///
hegyi@815:       Builder(UndirPath &_P) : P(_P) {}
hegyi@815: 
hegyi@815:       /// Sets the starting node of the path.
hegyi@815:       
hegyi@815:       /// Sets the starting node of the path. Edge added to the path
hegyi@815:       /// afterwards have to be incident to this node.
hegyi@815:       /// It should be called iff the path is empty and before any call to
hegyi@815:       /// \ref pushFront() or \ref pushBack()
hegyi@815:       void setStartNode(const GraphNode &) {}
hegyi@815: 
hegyi@815:       ///Push a new edge to the front of the path
hegyi@815: 
hegyi@815:       ///Push a new edge to the front of the path.
hegyi@815:       ///\sa setStartNode
hegyi@815:       void pushFront(const GraphEdge& e) {
hegyi@815: 	if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
hegyi@815: 	  fault("UndirPath::Builder::pushFront: nonincident edge");
hegyi@815: 	}
hegyi@815: 	front.push_back(e);
hegyi@815:       }
hegyi@815: 
hegyi@815:       ///Push a new edge to the back of the path
hegyi@815: 
hegyi@815:       ///Push a new edge to the back of the path.
hegyi@815:       ///\sa setStartNode
hegyi@815:       void pushBack(const GraphEdge& e) {
hegyi@815: 	if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
hegyi@815: 	  fault("UndirPath::Builder::pushBack: nonincident edge");
hegyi@815: 	}
hegyi@815: 	back.push_back(e);
hegyi@815:       }
hegyi@815: 
hegyi@815:       ///Commit the changes to the path.
hegyi@815:       void commit() {
hegyi@815: 	if( !(front.empty() && back.empty()) ) {
hegyi@815: 	  Container tmp;
hegyi@815: 	  tmp.reserve(front.size()+back.size()+P.length());
hegyi@815: 	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
hegyi@815: 	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
hegyi@815: 	  tmp.insert(tmp.end(), back.begin(), back.end());
hegyi@815: 	  P.edges.swap(tmp);
hegyi@815: 	  front.clear();
hegyi@815: 	  back.clear();
hegyi@815: 	}
hegyi@815:       }
hegyi@815: 
hegyi@815:       // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
hegyi@815:       // Hogy kenyelmes egy ilyet hasznalni?
hegyi@815: 
hegyi@815:       ///Reserve storage for the builder in advance.
hegyi@815: 
hegyi@815:       ///If you know an reasonable upper bound of the number of the edges
hegyi@815:       ///to add, using this function you can speed up the building.
hegyi@815:        void reserve(size_t r) {
hegyi@815: 	front.reserve(r);
hegyi@815: 	back.reserve(r);
hegyi@815:       }
hegyi@815: 
hegyi@815:     private:
hegyi@815:       bool empty() {
hegyi@815: 	return front.empty() && back.empty() && P.empty();
hegyi@815:       }
hegyi@815: 
hegyi@815:       GraphNode from() const {
hegyi@815: 	if( ! front.empty() )
hegyi@815: 	  return P.gr->tail(front[front.size()-1]);
hegyi@815: 	else if( ! P.empty() )
hegyi@815: 	  return P.gr->tail(P.edges[0]);
hegyi@815: 	else if( ! back.empty() )
hegyi@815: 	  return P.gr->tail(back[0]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815:       GraphNode to() const {
hegyi@815: 	if( ! back.empty() )
hegyi@815: 	  return P.gr->head(back[back.size()-1]);
hegyi@815: 	else if( ! P.empty() )
hegyi@815: 	  return P.gr->head(P.edges[P.length()-1]);
hegyi@815: 	else if( ! front.empty() )
hegyi@815: 	  return P.gr->head(front[0]);
hegyi@815: 	else
hegyi@815: 	  return INVALID;
hegyi@815:       }
hegyi@815: 
hegyi@815:     };
hegyi@815: 
hegyi@815:   };
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815: 
hegyi@815:   /**********************************************************************/
hegyi@815: 
hegyi@815: 
hegyi@815:   /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
hegyi@815:      elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
hegyi@815: 
hegyi@815:   template<typename Graph>
hegyi@815:   class DynamicPath {
hegyi@815: 
hegyi@815:   public:
hegyi@815:     typedef typename Graph::Edge GraphEdge;
hegyi@815:     typedef typename Graph::Node GraphNode;
hegyi@815:     class NodeIt;
hegyi@815:     class EdgeIt;
hegyi@815: 
hegyi@815:   protected:
hegyi@815:     Graph& G;
hegyi@815:     // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
hegyi@815:     // iranyitasat:
hegyi@815:     GraphNode _first, _last;
hegyi@815:     typedef std::deque<GraphEdge> Container;
hegyi@815:     Container edges;
hegyi@815: 
hegyi@815:   public:
hegyi@815: 
hegyi@815:     DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
hegyi@815: 
hegyi@815:     /// Subpath defined by two nodes.
hegyi@815:     /// Nodes may be in reversed order, then
hegyi@815:     /// we contstruct the reversed path.
hegyi@815:     DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
hegyi@815:     /// Subpath defined by two edges. Contains edges in [a,b)
hegyi@815:     /// It is an error if the two edges are not in order!
hegyi@815:     DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
hegyi@815:     
hegyi@815:     size_t length() const { return edges.size(); }
hegyi@815:     GraphNode from() const { return _first; }
hegyi@815:     GraphNode to() const { return _last; }
hegyi@815: 
hegyi@815:     NodeIt& first(NodeIt &n) const { return nth(n, 0); }
hegyi@815:     EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
hegyi@815:     template<typename It>
hegyi@815:     It first() const { 
hegyi@815:       It e;
hegyi@815:       first(e);
hegyi@815:       return e; 
hegyi@815:     }
hegyi@815: 
hegyi@815:     NodeIt& nth(NodeIt &, size_t) const;
hegyi@815:     EdgeIt& nth(EdgeIt &, size_t) const;
hegyi@815:     template<typename It>
hegyi@815:     It nth(size_t n) const { 
hegyi@815:       It e;
hegyi@815:       nth(e, n);
hegyi@815:       return e; 
hegyi@815:     }
hegyi@815: 
hegyi@815:     bool valid(const NodeIt &n) const { return n.idx <= length(); }
hegyi@815:     bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
hegyi@815: 
hegyi@815:     bool isForward(const EdgeIt &e) const { return e.forw; }
hegyi@815: 
hegyi@815:     /// index of a node on the path. Returns length+2 for the invalid NodeIt
hegyi@815:     int index(const NodeIt &n) const { return n.idx; }
hegyi@815:     /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
hegyi@815:     int index(const EdgeIt &e) const { return e.it - edges.begin(); }
hegyi@815: 
hegyi@815:     EdgeIt& next(EdgeIt &e) const;
hegyi@815:     NodeIt& next(NodeIt &n) const;
hegyi@815:     template <typename It>
hegyi@815:     It getNext(It it) const {
hegyi@815:       It tmp(it); return next(tmp);
hegyi@815:     }
hegyi@815: 
hegyi@815:     // A path is constructed using the following four functions.
hegyi@815:     // They return false if the requested operation is inconsistent
hegyi@815:     // with the path constructed so far.
hegyi@815:     // If your path has only one edge you MUST set either "from" or "to"!
hegyi@815:     // So you probably SHOULD call it in any case to be safe (and check the
hegyi@815:     // returned value to check if your path is consistent with your idea).
hegyi@815:     bool pushFront(const GraphEdge &e);
hegyi@815:     bool pushBack(const GraphEdge &e);
hegyi@815:     bool setFrom(const GraphNode &n);
hegyi@815:     bool setTo(const GraphNode &n);
hegyi@815: 
hegyi@815:     // WARNING: these two functions return the head/tail of an edge with
hegyi@815:     // respect to the direction of the path!
hegyi@815:     // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if 
hegyi@815:     // P.forward(e) is true (or the edge is a loop)!
hegyi@815:     NodeIt head(const EdgeIt& e) const;
hegyi@815:     NodeIt tail(const EdgeIt& e) const;
hegyi@815: 
hegyi@815:     // FIXME: ezeknek valami jobb nev kellene!!!
hegyi@815:     GraphEdge graphEdge(const EdgeIt& e) const;
hegyi@815:     GraphNode graphNode(const NodeIt& n) const;
hegyi@815: 
hegyi@815: 
hegyi@815:     /*** Iterator classes ***/
hegyi@815:     class EdgeIt {
hegyi@815:       friend class DynamicPath;
hegyi@815: 
hegyi@815:       typename Container::const_iterator it;
hegyi@815:       bool forw;
hegyi@815:     public:
hegyi@815:       // FIXME: jarna neki ilyen is...
hegyi@815:       // EdgeIt(Invalid);
hegyi@815: 
hegyi@815:       bool forward() const { return forw; }
hegyi@815: 
hegyi@815:       bool operator==(const EdgeIt& e) const { return it==e.it; }
hegyi@815:       bool operator!=(const EdgeIt& e) const { return it!=e.it; }
hegyi@815:       bool operator<(const EdgeIt& e) const { return it<e.it; }
hegyi@815:     };
hegyi@815: 
hegyi@815:     class NodeIt {
hegyi@815:       friend class DynamicPath;
hegyi@815: 
hegyi@815:       size_t idx;
hegyi@815:       bool tail;  // Is this node the tail of the edge with same idx?
hegyi@815: 
hegyi@815:     public:
hegyi@815:       // FIXME: jarna neki ilyen is...
hegyi@815:       // NodeIt(Invalid);
hegyi@815: 
hegyi@815:       bool operator==(const NodeIt& n) const { return idx==n.idx; }
hegyi@815:       bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
hegyi@815:       bool operator<(const NodeIt& n) const { return idx<n.idx; }
hegyi@815:     };
hegyi@815: 
hegyi@815:   private:
hegyi@815:     bool edgeIncident(const GraphEdge &e, const GraphNode &a,
hegyi@815: 		      GraphNode &b);
hegyi@815:     bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
hegyi@815:   };
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::EdgeIt&
hegyi@815:   DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
hegyi@815:     if( e.it == edges.end() ) 
hegyi@815:       return e;
hegyi@815: 
hegyi@815:     GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
hegyi@815:     ++e.it;
hegyi@815: 
hegyi@815:     // Invalid edgeit is always forward :)
hegyi@815:     if( e.it == edges.end() ) {
hegyi@815:       e.forw = true;
hegyi@815:       return e;
hegyi@815:     }
hegyi@815: 
hegyi@815:     e.forw = ( G.tail(*e.it) == common_node );
hegyi@815:     return e;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
hegyi@815:     if( n.idx >= length() ) {
hegyi@815:       // FIXME: invalid
hegyi@815:       n.idx = length()+1;
hegyi@815:       return n;
hegyi@815:     }
hegyi@815: 
hegyi@815:     
hegyi@815:     GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
hegyi@815: 			      G.tail(edges[n.idx]) );
hegyi@815:     ++n.idx;
hegyi@815:     if( n.idx < length() ) {
hegyi@815:       n.tail = ( next_node == G.tail(edges[n.idx]) );
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       n.tail = true;
hegyi@815:     }
hegyi@815: 
hegyi@815:     return n;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
hegyi@815: 			  GraphNode &b) {
hegyi@815:     if( G.tail(e) == a ) {
hegyi@815:       b=G.head(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     if( G.head(e) == a ) {
hegyi@815:       b=G.tail(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     return false;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
hegyi@815: 			     const GraphEdge &f) {
hegyi@815:     if( edgeIncident(f, G.tail(e), _last) ) {
hegyi@815:       _first = G.head(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     if( edgeIncident(f, G.head(e), _last) ) {
hegyi@815:       _first = G.tail(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     return false;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
hegyi@815:     if( G.valid(_first) ) {
hegyi@815: 	if( edgeIncident(e, _first, _first) ) {
hegyi@815: 	  edges.push_front(e);
hegyi@815: 	  return true;
hegyi@815: 	}
hegyi@815: 	else
hegyi@815: 	  return false;
hegyi@815:     }
hegyi@815:     else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
hegyi@815:       edges.push_front(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     else
hegyi@815:       return false;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
hegyi@815:     if( G.valid(_last) ) {
hegyi@815: 	if( edgeIncident(e, _last, _last) ) {
hegyi@815: 	  edges.push_back(e);
hegyi@815: 	  return true;
hegyi@815: 	}
hegyi@815: 	else
hegyi@815: 	  return false;
hegyi@815:     }
hegyi@815:     else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
hegyi@815:       edges.push_back(e);
hegyi@815:       return true;
hegyi@815:     }
hegyi@815:     else
hegyi@815:       return false;
hegyi@815:   }
hegyi@815: 
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
hegyi@815:     if( G.valid(_first) ) {
hegyi@815:       return _first == n;
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       if( length() > 0) {
hegyi@815: 	if( edgeIncident(edges[0], n, _last) ) {
hegyi@815: 	  _first = n;
hegyi@815: 	  return true;
hegyi@815: 	}
hegyi@815: 	else return false;
hegyi@815:       }
hegyi@815:       else {
hegyi@815: 	_first = _last = n;
hegyi@815: 	return true;
hegyi@815:       }
hegyi@815:     }
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   bool DynamicPath<Gr>::setTo(const GraphNode &n) {
hegyi@815:     if( G.valid(_last) ) {
hegyi@815:       return _last == n;
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       if( length() > 0) {
hegyi@815: 	if( edgeIncident(edges[0], n, _first) ) {
hegyi@815: 	  _last = n;
hegyi@815: 	  return true;
hegyi@815: 	}
hegyi@815: 	else return false;
hegyi@815:       }
hegyi@815:       else {
hegyi@815: 	_first = _last = n;
hegyi@815: 	return true;
hegyi@815:       }
hegyi@815:     }
hegyi@815:   }
hegyi@815: 
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::NodeIt
hegyi@815:   DynamicPath<Gr>::tail(const EdgeIt& e) const {
hegyi@815:     NodeIt n;
hegyi@815: 
hegyi@815:     if( e.it == edges.end() ) {
hegyi@815:       // FIXME: invalid-> invalid
hegyi@815:       n.idx = length() + 1;
hegyi@815:       n.tail = true;
hegyi@815:       return n;
hegyi@815:     }
hegyi@815: 
hegyi@815:     n.idx = e.it-edges.begin();
hegyi@815:     n.tail = e.forw;
hegyi@815:     return n;
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::NodeIt
hegyi@815:   DynamicPath<Gr>::head(const EdgeIt& e) const {
hegyi@815:     if( e.it == edges.end()-1 ) {
hegyi@815:       return _last;
hegyi@815:     }
hegyi@815: 
hegyi@815:     EdgeIt next_edge = e;
hegyi@815:     next(next_edge);
hegyi@815:     return tail(next_edge);
hegyi@815:   }
hegyi@815:       
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::GraphEdge
hegyi@815:   DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
hegyi@815:     if( e.it != edges.end() ) {
hegyi@815:       return *e.it;
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       return INVALID;
hegyi@815:     }
hegyi@815:   }
hegyi@815:   
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::GraphNode
hegyi@815:   DynamicPath<Gr>::graphNode(const NodeIt& n) const {
hegyi@815:     if( n.idx < length() ) {
hegyi@815:       return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
hegyi@815:     }
hegyi@815:     else if( n.idx == length() ) {
hegyi@815:       return _last;
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       return INVALID;
hegyi@815:     }
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::EdgeIt&
hegyi@815:   DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
hegyi@815:     if( k>=length() ) {
hegyi@815:       // FIXME: invalid EdgeIt
hegyi@815:       e.it = edges.end();
hegyi@815:       e.forw = true;
hegyi@815:       return e;
hegyi@815:     }
hegyi@815: 
hegyi@815:     e.it = edges.begin()+k;
hegyi@815:     if(k==0) {
hegyi@815:       e.forw = ( G.tail(*e.it) == _first );
hegyi@815:     }
hegyi@815:     else {
hegyi@815:       e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
hegyi@815: 		 G.tail(*e.it) == G.head(edges[k-1]) );
hegyi@815:     }
hegyi@815:     return e;
hegyi@815:   }
hegyi@815:     
hegyi@815:   template<typename Gr>
hegyi@815:   typename DynamicPath<Gr>::NodeIt&
hegyi@815:   DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
hegyi@815:     if( k>length() ) {
hegyi@815:       // FIXME: invalid NodeIt
hegyi@815:       n.idx = length()+1;
hegyi@815:       n.tail = true;
hegyi@815:       return n;
hegyi@815:     }
hegyi@815:     if( k==length() ) {
hegyi@815:       n.idx = length();
hegyi@815:       n.tail = true;
hegyi@815:       return n;
hegyi@815:     }
hegyi@815:     n = tail(nth<EdgeIt>(k));
hegyi@815:     return n;
hegyi@815:   }
hegyi@815: 
hegyi@815:   // Reszut konstruktorok:
hegyi@815: 
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
hegyi@815: 			       const EdgeIt &b) :
hegyi@815:     G(P.G), edges(a.it, b.it)    // WARNING: if b.it < a.it this will blow up! 
hegyi@815:   {
hegyi@815:     if( G.valid(P._first) && a.it < P.edges.end() ) {
hegyi@815:       _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
hegyi@815:       if( b.it < P.edges.end() ) {
hegyi@815: 	_last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
hegyi@815:       }
hegyi@815:       else {
hegyi@815: 	_last = P._last;
hegyi@815:       }
hegyi@815:     }
hegyi@815:   }
hegyi@815: 
hegyi@815:   template<typename Gr>
hegyi@815:   DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
hegyi@815: 			       const NodeIt &b) : G(P.G)
hegyi@815:   {
hegyi@815:     if( !P.valid(a) || !P.valid(b) )
hegyi@815:       return;
hegyi@815: 
hegyi@815:     int ai = a.idx, bi = b.idx;
hegyi@815:     if( bi<ai )
hegyi@815:       std::swap(ai,bi);
hegyi@815:     
hegyi@815:     edges.resize(bi-ai);
hegyi@815:     copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
hegyi@815: 
hegyi@815:     _first = P.graphNode(a);
hegyi@815:     _last = P.graphNode(b);
hegyi@815:   }
hegyi@815: 
hegyi@815:   ///@}
hegyi@815: 
hegyi@815: } // namespace hugo
hegyi@815: 
hegyi@815: #endif // HUGO_PATH_H