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