diff -r 39b6e65574c6 -r 0759d974de81 lemon/path.h --- a/lemon/path.h Thu Apr 02 22:34:03 2015 +0200 +++ b/lemon/path.h Sun Jan 05 22:24:56 2014 +0100 @@ -30,6 +30,7 @@ #include #include #include +#include namespace lemon { @@ -140,6 +141,23 @@ int idx; }; + /// \brief Gets the collection of the arcs of the path. + /// + /// This function can be used for iterating on the + /// arcs of the path. It returns a wrapped + /// ArcIt, which looks like an STL container + /// (by having begin() and end()) which you can use in range-based + /// for loops, STL algorithms, etc. + /// For example you can write: + ///\code + /// for(auto a: p.arcs()) + /// doSomething(a); + ///\endcode + LemonRangeWrapper1 arcs() const { + return LemonRangeWrapper1(*this); + } + + /// \brief Length of the path. int length() const { return head.size() + tail.size(); } /// \brief Return whether the path is empty. @@ -345,6 +363,23 @@ int idx; }; + /// \brief Gets the collection of the arcs of the path. + /// + /// This function can be used for iterating on the + /// arcs of the path. It returns a wrapped + /// ArcIt, which looks like an STL container + /// (by having begin() and end()) which you can use in range-based + /// for loops, STL algorithms, etc. + /// For example you can write: + ///\code + /// for(auto a: p.arcs()) + /// doSomething(a); + ///\endcode + LemonRangeWrapper1 arcs() const { + return LemonRangeWrapper1(*this); + } + + /// \brief Length of the path. int length() const { return data.size(); } /// \brief Return true if the path is empty. @@ -543,6 +578,23 @@ Node *node; }; + /// \brief Gets the collection of the arcs of the path. + /// + /// This function can be used for iterating on the + /// arcs of the path. It returns a wrapped + /// ArcIt, which looks like an STL container + /// (by having begin() and end()) which you can use in range-based + /// for loops, STL algorithms, etc. + /// For example you can write: + ///\code + /// for(auto a: p.arcs()) + /// doSomething(a); + ///\endcode + LemonRangeWrapper1 arcs() const { + return LemonRangeWrapper1(*this); + } + + /// \brief The n-th arc. /// /// This function looks for the n-th arc in O(n) time. @@ -795,11 +847,11 @@ /// \brief Default constructor /// /// Default constructor - StaticPath() : len(0), arcs(0) {} + StaticPath() : len(0), _arcs(0) {} /// \brief Copy constructor /// - StaticPath(const StaticPath& cpath) : arcs(0) { + StaticPath(const StaticPath& cpath) : _arcs(0) { pathCopy(cpath, *this); } @@ -807,7 +859,7 @@ /// /// This path can be initialized from any other path type. template - StaticPath(const CPath& cpath) : arcs(0) { + StaticPath(const CPath& cpath) : _arcs(0) { pathCopy(cpath, *this); } @@ -815,7 +867,7 @@ /// /// Destructor of the path ~StaticPath() { - if (arcs) delete[] arcs; + if (_arcs) delete[] _arcs; } /// \brief Copy assignment @@ -882,12 +934,29 @@ const StaticPath *path; int idx; }; + + /// \brief Gets the collection of the arcs of the path. + /// + /// This function can be used for iterating on the + /// arcs of the path. It returns a wrapped + /// ArcIt, which looks like an STL container + /// (by having begin() and end()) which you can use in range-based + /// for loops, STL algorithms, etc. + /// For example you can write: + ///\code + /// for(auto a: p.arcs()) + /// doSomething(a); + ///\endcode + LemonRangeWrapper1 arcs() const { + return LemonRangeWrapper1(*this); + } + /// \brief The n-th arc. /// /// \pre \c n is in the [0..length() - 1] range. const Arc& nth(int n) const { - return arcs[n]; + return _arcs[n]; } /// \brief The arc iterator pointing to the n-th arc. @@ -904,18 +973,18 @@ /// \brief Erase all arcs in the digraph. void clear() { len = 0; - if (arcs) delete[] arcs; - arcs = 0; + if (_arcs) delete[] _arcs; + _arcs = 0; } /// \brief The first arc of the path. const Arc& front() const { - return arcs[0]; + return _arcs[0]; } /// \brief The last arc of the path. const Arc& back() const { - return arcs[len - 1]; + return _arcs[len - 1]; } @@ -924,10 +993,10 @@ template void build(const CPath& path) { len = path.length(); - arcs = new Arc[len]; + _arcs = new Arc[len]; int index = 0; for (typename CPath::ArcIt it(path); it != INVALID; ++it) { - arcs[index] = it; + _arcs[index] = it; ++index; } } @@ -935,17 +1004,17 @@ template void buildRev(const CPath& path) { len = path.length(); - arcs = new Arc[len]; + _arcs = new Arc[len]; int index = len; for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { --index; - arcs[index] = it; + _arcs[index] = it; } } private: int len; - Arc* arcs; + Arc* _arcs; }; /////////////////////////////////////////////////////////////////////// @@ -1157,6 +1226,25 @@ }; + /// \brief Gets the collection of the nodes of the path. + /// + /// This function can be used for iterating on the + /// nodes of the path. It returns a wrapped + /// PathNodeIt, which looks like an STL container + /// (by having begin() and end()) which you can use in range-based + /// for loops, STL algorithms, etc. + /// For example you can write: + ///\code + /// for(auto u: pathNodes(g,p)) + /// doSomething(u); + ///\endcode + template + LemonRangeWrapper2, typename Path::Digraph, Path> + pathNodes(const typename Path::Digraph &g, const Path &p) { + return + LemonRangeWrapper2, typename Path::Digraph, Path>(g,p); + } + ///@} } // namespace lemon