COIN-OR::LEMON - Graph Library

Ticket #250: 250-eb2f42b62296.patch

File 250-eb2f42b62296.patch, 9.4 KB (added by Peter Kovacs, 6 years ago)
  • lemon/path.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1362737398 -3600
    # Node ID eb2f42b62296f5f6feec449b010506b0e30cd1e5
    # Parent  15d7c5eadacadbf50145095312677c6837327282
    Add operator[] to some Path structures + improve API doc (#250)
    
    diff --git a/lemon/path.h b/lemon/path.h
    a b  
    4242  /// A structure for representing directed path in a digraph.
    4343  /// \tparam GR The digraph type in which the path is.
    4444  ///
    45   /// In a sense, the path can be treated as a list of arcs. The
    46   /// LEMON path type stores just this list. As a consequence, it
    47   /// cannot enumerate the nodes of the path and the source node of
     45  /// In a sense, a path can be treated as a list of arcs. The
     46  /// LEMON path type stores just this list. As a consequence it
     47  /// cannot enumerate the nodes in the path and the source node of
    4848  /// a zero length path is undefined.
    4949  ///
    5050  /// This implementation is a back and front insertable and erasable
     
    150150
    151151    /// \brief The n-th arc.
    152152    ///
    153     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
     153    /// Gives back the n-th arc. This function runs in O(1) time.
     154    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    154155    const Arc& nth(int n) const {
    155156      return n < int(head.size()) ? *(head.rbegin() + n) :
    156157        *(tail.begin() + (n - head.size()));
     
    162163    ArcIt nthIt(int n) const {
    163164      return ArcIt(*this, n);
    164165    }
     166   
     167    /// \brief The n-th arc.
     168    ///
     169    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
     170    /// it runs in O(1) time.
     171    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     172    const Arc& operator[](int n) const {
     173      return nth(n);
     174    }
    165175
    166176    /// \brief The first arc of the path
    167177    const Arc& front() const {
     
    243253  /// A structure for representing directed path in a digraph.
    244254  /// \tparam GR The digraph type in which the path is.
    245255  ///
    246   /// In a sense, the path can be treated as a list of arcs. The
     256  /// In a sense, a path can be treated as a list of arcs. The
    247257  /// LEMON path type stores just this list. As a consequence it
    248   /// cannot enumerate the nodes in the path and the zero length paths
    249   /// cannot store the source.
     258  /// cannot enumerate the nodes in the path and the source node of
     259  /// a zero length path is undefined.
    250260  ///
    251261  /// This implementation is a just back insertable and erasable path
    252262  /// type. It can be indexed in O(1) time. The back insertion and
    253263  /// erasure is amortized O(1) time. This implementation is faster
    254   /// then the \c Path type because it use just one vector for the
     264  /// than the \c Path type because it use just one vector for the
    255265  /// arcs.
    256266  template <typename GR>
    257267  class SimplePath {
     
    355365
    356366    /// \brief The n-th arc.
    357367    ///
    358     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
     368    /// Gives back the n-th arc. This function runs in O(1) time.
     369    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    359370    const Arc& nth(int n) const {
    360371      return data[n];
    361372    }
     
    364375    ArcIt nthIt(int n) const {
    365376      return ArcIt(*this, n);
    366377    }
     378   
     379    /// \brief The n-th arc.
     380    ///
     381    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
     382    /// it runs in O(1) time.
     383    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     384    const Arc& operator[](int n) const {
     385      return data[n];
     386    }
    367387
    368388    /// \brief The first arc of the path.
    369389    const Arc& front() const {
     
    420440  /// A structure for representing directed path in a digraph.
    421441  /// \tparam GR The digraph type in which the path is.
    422442  ///
    423   /// In a sense, the path can be treated as a list of arcs. The
     443  /// In a sense, a path can be treated as a list of arcs. The
    424444  /// LEMON path type stores just this list. As a consequence it
    425   /// cannot enumerate the nodes in the path and the zero length paths
    426   /// cannot store the source.
     445  /// cannot enumerate the nodes in the path and the source node of
     446  /// a zero length path is undefined.
    427447  ///
    428448  /// This implementation is a back and front insertable and erasable
    429449  /// path type. It can be indexed in O(k) time, where k is the rank
     
    546566    /// \brief The n-th arc.
    547567    ///
    548568    /// This function looks for the n-th arc in O(n) time.
    549     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
     569    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    550570    const Arc& nth(int n) const {
    551571      Node *node = first;
    552572      for (int i = 0; i < n; ++i) {
     
    732752    /// starting with
    733753    /// \c it will put into \c tpath. If \c tpath have arcs
    734754    /// before the operation they are removed first.  The time
    735     /// complexity of this function is O(1) plus the the time of emtying
     755    /// complexity of this function is O(1) plus the time of emtying
    736756    /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
    737757    void split(ArcIt it, ListPath& tpath) {
    738758      tpath.clear();
     
    773793  /// A structure for representing directed path in a digraph.
    774794  /// \tparam GR The digraph type in which the path is.
    775795  ///
    776   /// In a sense, the path can be treated as a list of arcs. The
     796  /// In a sense, a path can be treated as a list of arcs. The
    777797  /// LEMON path type stores just this list. As a consequence it
    778798  /// cannot enumerate the nodes in the path and the source node of
    779799  /// a zero length path is undefined.
     
    782802  /// or copy assigned from another path, but otherwise it cannot be
    783803  /// modified.
    784804  ///
    785   /// Being the the most memory efficient path type in LEMON,
     805  /// Being the most memory efficient path type in LEMON,
    786806  /// it is intented to be
    787807  /// used when you want to store a large number of paths.
    788808  template <typename GR>
     
    885905
    886906    /// \brief The n-th arc.
    887907    ///
    888     /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
     908    /// Gives back the n-th arc. This function runs in O(1) time.
     909    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    889910    const Arc& nth(int n) const {
    890911      return arcs[n];
    891912    }
     
    895916      return ArcIt(*this, n);
    896917    }
    897918
     919    /// \brief The n-th arc.
     920    ///
     921    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
     922    /// it runs in O(1) time.
     923    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     924    const Arc& operator[](int n) const {
     925      return arcs[n];
     926    }
     927
    898928    /// \brief The length of the path.
    899929    int length() const { return len; }
    900930
    901931    /// \brief Return true when the path is empty.
    902932    int empty() const { return len == 0; }
    903933
    904     /// \brief Erase all arcs in the digraph.
     934    /// \brief Reset the path to an empty one.
    905935    void clear() {
    906936      len = 0;
    907937      if (arcs) delete[] arcs;
     
    9841014              bool buildEnable = BuildTagIndicator<To>::value>
    9851015    struct PathCopySelectorForward {
    9861016      static void copy(const From& from, To& to) {
     1017        std::cout << "standard path copy...\n";
    9871018        to.clear();
    9881019        for (typename From::ArcIt it(from); it != INVALID; ++it) {
    9891020          to.addBack(it);
     
    10911122    return path.empty() ? INVALID : digraph.target(path.back());
    10921123  }
    10931124
    1094   /// \brief Class which helps to iterate through the nodes of a path
     1125  /// \brief Class for iterating through the nodes of a path
    10951126  ///
    1096   /// In a sense, the path can be treated as a list of arcs. The
    1097   /// LEMON path type stores only this list. As a consequence, it
    1098   /// cannot enumerate the nodes in the path and the zero length paths
    1099   /// cannot have a source node.
     1127  /// Class for iterating through the nodes of a path.
    11001128  ///
    1101   /// This class implements the node iterator of a path structure. To
    1102   /// provide this feature, the underlying digraph should be passed to
     1129  /// In a sense, a path can be treated as a list of arcs. The
     1130  /// LEMON path type stores only this list. As a consequence it
     1131  /// cannot enumerate the nodes in the path and the source node of
     1132  /// a zero length path is undefined.
     1133  ///
     1134  /// However, this class implements a node iterator for path structures.
     1135  /// To provide this feature, the underlying digraph should be passed to
    11031136  /// the constructor of the iterator.
    11041137  template <typename Path>
    11051138  class PathNodeIt {
  • test/path_test.cc

    diff --git a/test/path_test.cc b/test/path_test.cc
    a b  
    108108    checkBackAndFrontInsertablePath<ListPath<GR> >();
    109109    checkBackInsertablePath<SimplePath<GR> >();
    110110
     111    checkSubscriptOperator<Path<GR> >();
     112    checkSubscriptOperator<SimplePath<GR> >();
     113    checkSubscriptOperator<StaticPath<GR> >();
     114
    111115    checkListPathSplitAndSplice();
    112116  }
    113117
     
    273277    check(checkPath(cgr, cp), "Wrong checkPath()");
    274278  }
    275279
     280  template <typename P>
     281  void checkSubscriptOperator() {
     282    SimplePath<GR> p0;
     283    p0.addBack(a1);
     284    p0.addBack(a3);
     285    p0.addBack(a2);
     286    P p = p0;
     287    check(!p.empty(), "Wrong empty()");
     288    check(p.length() == 3, "Wrong length");
     289    check(p.front() == a1, "Wrong front()");
     290    check(p.back() == a2, "Wrong back()");
     291    check(p.nth(0) == a1, "Wrong nth()");
     292    check(p.nth(1) == a3, "Wrong nth()");
     293    check(p.nth(2) == a2, "Wrong nth()");
     294    check(p[0] == a1, "Wrong operator[]");
     295    check(p[1] == a3, "Wrong operator[]");
     296    check(p[2] == a2, "Wrong operator[]");
     297  }
     298
    276299  void checkListPathSplitAndSplice() {
    277300
    278301    // Build a path with spliceFront() and spliceBack()