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