lemon/path.h
changeset 1130 0759d974de81
parent 1092 dceba191c00d
child 1201 1f4f01870c1e
     1.1 --- a/lemon/path.h	Thu Apr 02 22:34:03 2015 +0200
     1.2 +++ b/lemon/path.h	Sun Jan 05 22:24:56 2014 +0100
     1.3 @@ -30,6 +30,7 @@
     1.4  #include <lemon/error.h>
     1.5  #include <lemon/core.h>
     1.6  #include <lemon/concepts/path.h>
     1.7 +#include <lemon/bits/stl_iterators.h>
     1.8  
     1.9  namespace lemon {
    1.10  
    1.11 @@ -140,6 +141,23 @@
    1.12        int idx;
    1.13      };
    1.14  
    1.15 +    /// \brief Gets the collection of the arcs of the path.
    1.16 +    ///
    1.17 +    /// This function can be used for iterating on the
    1.18 +    /// arcs of the path. It returns a wrapped
    1.19 +    /// ArcIt, which looks like an STL container
    1.20 +    /// (by having begin() and end()) which you can use in range-based
    1.21 +    /// for loops, STL algorithms, etc.
    1.22 +    /// For example you can write:
    1.23 +    ///\code
    1.24 +    /// for(auto a: p.arcs())
    1.25 +    ///   doSomething(a);
    1.26 +    ///\endcode
    1.27 +    LemonRangeWrapper1<ArcIt, Path> arcs() const {
    1.28 +      return LemonRangeWrapper1<ArcIt, Path>(*this);
    1.29 +    }
    1.30 +
    1.31 +
    1.32      /// \brief Length of the path.
    1.33      int length() const { return head.size() + tail.size(); }
    1.34      /// \brief Return whether the path is empty.
    1.35 @@ -345,6 +363,23 @@
    1.36        int idx;
    1.37      };
    1.38  
    1.39 +    /// \brief Gets the collection of the arcs of the path.
    1.40 +    ///
    1.41 +    /// This function can be used for iterating on the
    1.42 +    /// arcs of the path. It returns a wrapped
    1.43 +    /// ArcIt, which looks like an STL container
    1.44 +    /// (by having begin() and end()) which you can use in range-based
    1.45 +    /// for loops, STL algorithms, etc.
    1.46 +    /// For example you can write:
    1.47 +    ///\code
    1.48 +    /// for(auto a: p.arcs())
    1.49 +    ///   doSomething(a);
    1.50 +    ///\endcode
    1.51 +    LemonRangeWrapper1<ArcIt, SimplePath> arcs() const {
    1.52 +      return LemonRangeWrapper1<ArcIt, SimplePath>(*this);
    1.53 +    }
    1.54 +
    1.55 +
    1.56      /// \brief Length of the path.
    1.57      int length() const { return data.size(); }
    1.58      /// \brief Return true if the path is empty.
    1.59 @@ -543,6 +578,23 @@
    1.60        Node *node;
    1.61      };
    1.62  
    1.63 +    /// \brief Gets the collection of the arcs of the path.
    1.64 +    ///
    1.65 +    /// This function can be used for iterating on the
    1.66 +    /// arcs of the path. It returns a wrapped
    1.67 +    /// ArcIt, which looks like an STL container
    1.68 +    /// (by having begin() and end()) which you can use in range-based
    1.69 +    /// for loops, STL algorithms, etc.
    1.70 +    /// For example you can write:
    1.71 +    ///\code
    1.72 +    /// for(auto a: p.arcs())
    1.73 +    ///   doSomething(a);
    1.74 +    ///\endcode
    1.75 +    LemonRangeWrapper1<ArcIt, ListPath> arcs() const {
    1.76 +      return LemonRangeWrapper1<ArcIt, ListPath>(*this);
    1.77 +    }
    1.78 +
    1.79 +
    1.80      /// \brief The n-th arc.
    1.81      ///
    1.82      /// This function looks for the n-th arc in O(n) time.
    1.83 @@ -795,11 +847,11 @@
    1.84      /// \brief Default constructor
    1.85      ///
    1.86      /// Default constructor
    1.87 -    StaticPath() : len(0), arcs(0) {}
    1.88 +    StaticPath() : len(0), _arcs(0) {}
    1.89  
    1.90      /// \brief Copy constructor
    1.91      ///
    1.92 -    StaticPath(const StaticPath& cpath) : arcs(0) {
    1.93 +    StaticPath(const StaticPath& cpath) : _arcs(0) {
    1.94        pathCopy(cpath, *this);
    1.95      }
    1.96  
    1.97 @@ -807,7 +859,7 @@
    1.98      ///
    1.99      /// This path can be initialized from any other path type.
   1.100      template <typename CPath>
   1.101 -    StaticPath(const CPath& cpath) : arcs(0) {
   1.102 +    StaticPath(const CPath& cpath) : _arcs(0) {
   1.103        pathCopy(cpath, *this);
   1.104      }
   1.105  
   1.106 @@ -815,7 +867,7 @@
   1.107      ///
   1.108      /// Destructor of the path
   1.109      ~StaticPath() {
   1.110 -      if (arcs) delete[] arcs;
   1.111 +      if (_arcs) delete[] _arcs;
   1.112      }
   1.113  
   1.114      /// \brief Copy assignment
   1.115 @@ -882,12 +934,29 @@
   1.116        const StaticPath *path;
   1.117        int idx;
   1.118      };
   1.119 +    
   1.120 +    /// \brief Gets the collection of the arcs of the path.
   1.121 +    ///
   1.122 +    /// This function can be used for iterating on the
   1.123 +    /// arcs of the path. It returns a wrapped
   1.124 +    /// ArcIt, which looks like an STL container
   1.125 +    /// (by having begin() and end()) which you can use in range-based
   1.126 +    /// for loops, STL algorithms, etc.
   1.127 +    /// For example you can write:
   1.128 +    ///\code
   1.129 +    /// for(auto a: p.arcs())
   1.130 +    ///   doSomething(a);
   1.131 +    ///\endcode
   1.132 +    LemonRangeWrapper1<ArcIt, StaticPath> arcs() const {
   1.133 +      return LemonRangeWrapper1<ArcIt, StaticPath>(*this);
   1.134 +    }
   1.135 +    
   1.136  
   1.137      /// \brief The n-th arc.
   1.138      ///
   1.139      /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
   1.140      const Arc& nth(int n) const {
   1.141 -      return arcs[n];
   1.142 +      return _arcs[n];
   1.143      }
   1.144  
   1.145      /// \brief The arc iterator pointing to the n-th arc.
   1.146 @@ -904,18 +973,18 @@
   1.147      /// \brief Erase all arcs in the digraph.
   1.148      void clear() {
   1.149        len = 0;
   1.150 -      if (arcs) delete[] arcs;
   1.151 -      arcs = 0;
   1.152 +      if (_arcs) delete[] _arcs;
   1.153 +      _arcs = 0;
   1.154      }
   1.155  
   1.156      /// \brief The first arc of the path.
   1.157      const Arc& front() const {
   1.158 -      return arcs[0];
   1.159 +      return _arcs[0];
   1.160      }
   1.161  
   1.162      /// \brief The last arc of the path.
   1.163      const Arc& back() const {
   1.164 -      return arcs[len - 1];
   1.165 +      return _arcs[len - 1];
   1.166      }
   1.167  
   1.168  
   1.169 @@ -924,10 +993,10 @@
   1.170      template <typename CPath>
   1.171      void build(const CPath& path) {
   1.172        len = path.length();
   1.173 -      arcs = new Arc[len];
   1.174 +      _arcs = new Arc[len];
   1.175        int index = 0;
   1.176        for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
   1.177 -        arcs[index] = it;
   1.178 +        _arcs[index] = it;
   1.179          ++index;
   1.180        }
   1.181      }
   1.182 @@ -935,17 +1004,17 @@
   1.183      template <typename CPath>
   1.184      void buildRev(const CPath& path) {
   1.185        len = path.length();
   1.186 -      arcs = new Arc[len];
   1.187 +      _arcs = new Arc[len];
   1.188        int index = len;
   1.189        for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
   1.190          --index;
   1.191 -        arcs[index] = it;
   1.192 +        _arcs[index] = it;
   1.193        }
   1.194      }
   1.195  
   1.196    private:
   1.197      int len;
   1.198 -    Arc* arcs;
   1.199 +    Arc* _arcs;
   1.200    };
   1.201  
   1.202    ///////////////////////////////////////////////////////////////////////
   1.203 @@ -1157,6 +1226,25 @@
   1.204  
   1.205    };
   1.206  
   1.207 +  /// \brief Gets the collection of the nodes of the path.
   1.208 +  ///
   1.209 +  /// This function can be used for iterating on the
   1.210 +  /// nodes of the path. It returns a wrapped
   1.211 +  /// PathNodeIt, which looks like an STL container
   1.212 +  /// (by having begin() and end()) which you can use in range-based
   1.213 +  /// for loops, STL algorithms, etc.
   1.214 +  /// For example you can write:
   1.215 +  ///\code
   1.216 +  /// for(auto u: pathNodes(g,p))
   1.217 +  ///   doSomething(u);
   1.218 +  ///\endcode
   1.219 +  template<typename Path>
   1.220 +  LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>
   1.221 +      pathNodes(const typename Path::Digraph &g, const Path &p) {
   1.222 +    return
   1.223 +        LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>(g,p);
   1.224 +  }
   1.225 +
   1.226    ///@}
   1.227  
   1.228  } // namespace lemon