# HG changeset patch # User deba # Date 1121699248 0 # Node ID 96244ea562a3e1007c215b61b018eb4ee5066a78 # Parent 16d316199cf661358f13c876613f2b5367ccda03 Improve findEdge interface ConEdgeIt is a high level replacement of findEdge diff -r 16d316199cf6 -r 96244ea562a3 lemon/graph_utils.h --- a/lemon/graph_utils.h Mon Jul 18 15:05:50 2005 +0000 +++ b/lemon/graph_utils.h Mon Jul 18 15:07:28 2005 +0000 @@ -160,8 +160,35 @@ } - /// Finds an edge between two nodes of a graph. + template + inline + typename enable_if::type + _findEdge(const Graph &g, + typename Graph::Node u, typename Graph::Node v, + typename Graph::Edge prev = INVALID) { + return g.findEdge(u, v, prev); + } + template + inline typename Graph::Edge + _findEdge(Wrap w, + typename Graph::Node u, + typename Graph::Node v, + typename Graph::Edge prev = INVALID) { + const Graph& g = w.value; + if (prev == INVALID) { + typename Graph::OutEdgeIt e(g, u); + while (e != INVALID && g.target(e) != v) ++e; + return e; + } else { + typename Graph::OutEdgeIt e(g, prev); ++e; + while (e != INVALID && g.target(e) != v) ++e; + return e; + } + } + + /// \brief Finds an edge between two nodes of a graph. + /// /// Finds an edge from node \c u to node \c v in graph \c g. /// /// If \c prev is \ref INVALID (this is the default value), then @@ -175,22 +202,65 @@ /// ... /// } /// \endcode - /// \todo We may want to use the "GraphBase" - /// interface here... - /// \bug Untested ... + // /// \todo We may want to use the "GraphBase" + // /// interface here... + // /// It would not work with the undirected graphs. template - typename Graph::Edge findEdge(const Graph &g, - typename Graph::Node u, typename Graph::Node v, - typename Graph::Edge prev = INVALID) - { - typename Graph::OutEdgeIt e(g,prev); - // if(prev==INVALID) g.first(e,u); - if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u); - else ++e; - while(e!=INVALID && g.target(e)!=v) ++e; - return e; + inline typename Graph::Edge findEdge(const Graph &g, + typename Graph::Node u, + typename Graph::Node v, + typename Graph::Edge prev = INVALID) { + return _findEdge(g, u, v, prev); } + /// \brief Iterator for iterating on edges connected the same nodes. + /// + /// Iterator for iterating on edges connected the same nodes. It is + /// higher level interface for the findEdge() function. You can + /// use it the next way: + /// \code + /// for (ConEdgeIt it(g, src, trg); it != INVALID; ++it) { + /// ... + /// } + /// \endcode + /// + /// \author Balazs Dezso + template + class ConEdgeIt : public _Graph::Edge { + public: + + typedef _Graph Graph; + typedef typename Graph::Edge Parent; + + typedef typename Graph::Edge Edge; + typedef typename Graph::Node Node; + + /// \brief Constructor. + /// + /// Construct a new ConEdgeIt iterating on the edges which + /// connects the \c u and \c v node. + ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) { + Parent::operator=(findEdge(graph, u, v)); + } + + /// \brief Constructor. + /// + /// Construct a new ConEdgeIt which continues the iterating from + /// the \c e edge. + ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {} + + /// \brief Increment operator. + /// + /// It increments the iterator and gives back the next edge. + ConEdgeIt& operator++() { + Parent::operator=(findEdge(graph, graph.source(*this), + graph.target(*this), *this)); + return *this; + } + private: + const Graph& graph; + }; + /// \brief Copy a map. /// /// This function copies the \c source map to the \c target map. It uses the