lemon/path.h
changeset 1420 1f4f01870c1e
parent 1336 0759d974de81
child 1421 4fd76139b69e
equal deleted inserted replaced
31:446a1231e699 32:8f064d933553
    41   /// \brief A structure for representing directed paths in a digraph.
    41   /// \brief A structure for representing directed paths in a digraph.
    42   ///
    42   ///
    43   /// A structure for representing directed path in a digraph.
    43   /// A structure for representing directed path in a digraph.
    44   /// \tparam GR The digraph type in which the path is.
    44   /// \tparam GR The digraph type in which the path is.
    45   ///
    45   ///
    46   /// In a sense, the path can be treated as a list of arcs. The
    46   /// In a sense, a path can be treated as a list of arcs. The
    47   /// LEMON path type stores just this list. As a consequence, it
    47   /// LEMON path type simply stores this list. As a consequence, it
    48   /// cannot enumerate the nodes of the path and the source node of
    48   /// cannot enumerate the nodes in the path, and the source node of
    49   /// a zero length path is undefined.
    49   /// a zero-length path is undefined.
    50   ///
    50   ///
    51   /// This implementation is a back and front insertable and erasable
    51   /// This implementation is a back and front insertable and erasable
    52   /// path type. It can be indexed in O(1) time. The front and back
    52   /// path type. It can be indexed in O(1) time. The front and back
    53   /// insertion and erase is done in O(1) (amortized) time. The
    53   /// insertion and erase is done in O(1) (amortized) time. The
    54   /// implementation uses two vectors for storing the front and back
    54   /// implementation uses two vectors for storing the front and back
   166     /// \brief Reset the path to an empty one.
   166     /// \brief Reset the path to an empty one.
   167     void clear() { head.clear(); tail.clear(); }
   167     void clear() { head.clear(); tail.clear(); }
   168 
   168 
   169     /// \brief The n-th arc.
   169     /// \brief The n-th arc.
   170     ///
   170     ///
   171     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
   171     /// Gives back the n-th arc. This function runs in O(1) time.
       
   172     /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
   172     const Arc& nth(int n) const {
   173     const Arc& nth(int n) const {
   173       return n < int(head.size()) ? *(head.rbegin() + n) :
   174       return n < int(head.size()) ? *(head.rbegin() + n) :
   174         *(tail.begin() + (n - head.size()));
   175         *(tail.begin() + (n - head.size()));
   175     }
   176     }
   176 
   177 
   259   /// \brief A structure for representing directed paths in a digraph.
   260   /// \brief A structure for representing directed paths in a digraph.
   260   ///
   261   ///
   261   /// A structure for representing directed path in a digraph.
   262   /// A structure for representing directed path in a digraph.
   262   /// \tparam GR The digraph type in which the path is.
   263   /// \tparam GR The digraph type in which the path is.
   263   ///
   264   ///
   264   /// In a sense, the path can be treated as a list of arcs. The
   265   /// In a sense, a path can be treated as a list of arcs. The
   265   /// LEMON path type stores just this list. As a consequence it
   266   /// LEMON path type simply stores this list. As a consequence, it
   266   /// cannot enumerate the nodes in the path and the zero length paths
   267   /// cannot enumerate the nodes in the path, and the source node of
   267   /// cannot store the source.
   268   /// a zero-length path is undefined.
   268   ///
   269   ///
   269   /// This implementation is a just back insertable and erasable path
   270   /// This implementation is a just back insertable and erasable path
   270   /// type. It can be indexed in O(1) time. The back insertion and
   271   /// type. It can be indexed in O(1) time. The back insertion and
   271   /// erasure is amortized O(1) time. This implementation is faster
   272   /// erasure is amortized O(1) time. This implementation is faster
   272   /// then the \c Path type because it use just one vector for the
   273   /// than the \c Path type because it use just one vector for the
   273   /// arcs.
   274   /// arcs.
   274   template <typename GR>
   275   template <typename GR>
   275   class SimplePath {
   276   class SimplePath {
   276   public:
   277   public:
   277 
   278 
   388     /// \brief Reset the path to an empty one.
   389     /// \brief Reset the path to an empty one.
   389     void clear() { data.clear(); }
   390     void clear() { data.clear(); }
   390 
   391 
   391     /// \brief The n-th arc.
   392     /// \brief The n-th arc.
   392     ///
   393     ///
   393     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
   394     /// Gives back the n-th arc. This function runs in O(1) time.
       
   395     /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
   394     const Arc& nth(int n) const {
   396     const Arc& nth(int n) const {
   395       return data[n];
   397       return data[n];
   396     }
   398     }
   397 
   399 
   398     /// \brief  Initializes arc iterator to point to the n-th arc.
   400     /// \brief  Initializes arc iterator to point to the n-th arc.
   453   /// \brief A structure for representing directed paths in a digraph.
   455   /// \brief A structure for representing directed paths in a digraph.
   454   ///
   456   ///
   455   /// A structure for representing directed path in a digraph.
   457   /// A structure for representing directed path in a digraph.
   456   /// \tparam GR The digraph type in which the path is.
   458   /// \tparam GR The digraph type in which the path is.
   457   ///
   459   ///
   458   /// In a sense, the path can be treated as a list of arcs. The
   460   /// In a sense, a path can be treated as a list of arcs. The
   459   /// LEMON path type stores just this list. As a consequence it
   461   /// LEMON path type simply stores this list. As a consequence, it
   460   /// cannot enumerate the nodes in the path and the zero length paths
   462   /// cannot enumerate the nodes in the path, and the source node of
   461   /// cannot store the source.
   463   /// a zero-length path is undefined.
   462   ///
   464   ///
   463   /// This implementation is a back and front insertable and erasable
   465   /// This implementation is a back and front insertable and erasable
   464   /// path type. It can be indexed in O(k) time, where k is the rank
   466   /// path type. It can be indexed in O(k) time, where k is the rank
   465   /// of the arc in the path. The length can be computed in O(n)
   467   /// of the arc in the path. The length can be computed in O(n)
   466   /// time. The front and back insertion and erasure is O(1) time
   468   /// time. The front and back insertion and erasure is O(1) time
   596 
   598 
   597 
   599 
   598     /// \brief The n-th arc.
   600     /// \brief The n-th arc.
   599     ///
   601     ///
   600     /// This function looks for the n-th arc in O(n) time.
   602     /// This function looks for the n-th arc in O(n) time.
   601     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
   603     /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
   602     const Arc& nth(int n) const {
   604     const Arc& nth(int n) const {
   603       Node *node = first;
   605       Node *node = first;
   604       for (int i = 0; i < n; ++i) {
   606       for (int i = 0; i < n; ++i) {
   605         node = node->next;
   607         node = node->next;
   606       }
   608       }
   782     /// It splits the current path into two parts. The part before
   784     /// It splits the current path into two parts. The part before
   783     /// the iterator \c it will remain in the current path and the part
   785     /// the iterator \c it will remain in the current path and the part
   784     /// starting with
   786     /// starting with
   785     /// \c it will put into \c tpath. If \c tpath have arcs
   787     /// \c it will put into \c tpath. If \c tpath have arcs
   786     /// before the operation they are removed first.  The time
   788     /// before the operation they are removed first.  The time
   787     /// complexity of this function is O(1) plus the the time of emtying
   789     /// complexity of this function is O(1) plus the time of emtying
   788     /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
   790     /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
   789     void split(ArcIt it, ListPath& tpath) {
   791     void split(ArcIt it, ListPath& tpath) {
   790       tpath.clear();
   792       tpath.clear();
   791       if (it.node) {
   793       if (it.node) {
   792         tpath.first = it.node;
   794         tpath.first = it.node;
   823   /// \brief A structure for representing directed paths in a digraph.
   825   /// \brief A structure for representing directed paths in a digraph.
   824   ///
   826   ///
   825   /// A structure for representing directed path in a digraph.
   827   /// A structure for representing directed path in a digraph.
   826   /// \tparam GR The digraph type in which the path is.
   828   /// \tparam GR The digraph type in which the path is.
   827   ///
   829   ///
   828   /// In a sense, the path can be treated as a list of arcs. The
   830   /// In a sense, a path can be treated as a list of arcs. The
   829   /// LEMON path type stores just this list. As a consequence it
   831   /// LEMON path type simply stores this list. As a consequence, it
   830   /// cannot enumerate the nodes in the path and the source node of
   832   /// cannot enumerate the nodes in the path, and the source node of
   831   /// a zero length path is undefined.
   833   /// a zero-length path is undefined.
   832   ///
   834   ///
   833   /// This implementation is completly static, i.e. it can be copy constucted
   835   /// This implementation is completly static, i.e. it can be copy constucted
   834   /// or copy assigned from another path, but otherwise it cannot be
   836   /// or copy assigned from another path, but otherwise it cannot be
   835   /// modified.
   837   /// modified.
   836   ///
   838   ///
   837   /// Being the the most memory efficient path type in LEMON,
   839   /// Being the most memory-efficient path type in LEMON, it is
   838   /// it is intented to be
   840   /// intented to be used when you want to store a large number of paths.
   839   /// used when you want to store a large number of paths.
       
   840   template <typename GR>
   841   template <typename GR>
   841   class StaticPath {
   842   class StaticPath {
   842   public:
   843   public:
   843 
   844 
   844     typedef GR Digraph;
   845     typedef GR Digraph;
   952     }
   953     }
   953     
   954     
   954 
   955 
   955     /// \brief The n-th arc.
   956     /// \brief The n-th arc.
   956     ///
   957     ///
   957     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
   958     /// Gives back the n-th arc. This function runs in O(1) time.
       
   959     /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
   958     const Arc& nth(int n) const {
   960     const Arc& nth(int n) const {
   959       return _arcs[n];
   961       return _arcs[n];
   960     }
   962     }
   961 
   963 
   962     /// \brief The arc iterator pointing to the n-th arc.
   964     /// \brief The arc iterator pointing to the n-th arc.
   968     int length() const { return len; }
   970     int length() const { return len; }
   969 
   971 
   970     /// \brief Return true when the path is empty.
   972     /// \brief Return true when the path is empty.
   971     int empty() const { return len == 0; }
   973     int empty() const { return len == 0; }
   972 
   974 
   973     /// \brief Erase all arcs in the digraph.
   975     /// \brief Reset the path to an empty one.
   974     void clear() {
   976     void clear() {
   975       len = 0;
   977       len = 0;
   976       if (_arcs) delete[] _arcs;
   978       if (_arcs) delete[] _arcs;
   977       _arcs = 0;
   979       _arcs = 0;
   978     }
   980     }
  1158   template <typename Digraph, typename Path>
  1160   template <typename Digraph, typename Path>
  1159   typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
  1161   typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
  1160     return path.empty() ? INVALID : digraph.target(path.back());
  1162     return path.empty() ? INVALID : digraph.target(path.back());
  1161   }
  1163   }
  1162 
  1164 
  1163   /// \brief Class which helps to iterate through the nodes of a path
  1165   /// \brief Class for iterating through the nodes of a path
  1164   ///
  1166   ///
  1165   /// In a sense, the path can be treated as a list of arcs. The
  1167   /// Class for iterating through the nodes of a path.
  1166   /// LEMON path type stores only this list. As a consequence, it
  1168   ///
  1167   /// cannot enumerate the nodes in the path and the zero length paths
  1169   /// In a sense, a path can be treated as a list of arcs. The
  1168   /// cannot have a source node.
  1170   /// LEMON path type simply stores this list. As a consequence, it
  1169   ///
  1171   /// cannot enumerate the nodes in the path, and the source node of
  1170   /// This class implements the node iterator of a path structure. To
  1172   /// a zero-length path is undefined.
  1171   /// provide this feature, the underlying digraph should be passed to
  1173   ///
       
  1174   /// However, this class implements a node iterator for path structures.
       
  1175   /// To provide this feature, the underlying digraph should be passed to
  1172   /// the constructor of the iterator.
  1176   /// the constructor of the iterator.
  1173   template <typename Path>
  1177   template <typename Path>
  1174   class PathNodeIt {
  1178   class PathNodeIt {
  1175   private:
  1179   private:
  1176     const typename Path::Digraph *_digraph;
  1180     const typename Path::Digraph *_digraph;