COIN-OR::LEMON - Graph Library

Ticket #250: 250-new-dbf9eee91ed2.patch

File 250-new-dbf9eee91ed2.patch, 2.8 KB (added by Peter Kovacs, 15 months ago)
  • lemon/path.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1518907472 -3600
    #      Sat Feb 17 23:44:32 2018 +0100
    # Node ID dbf9eee91ed21dd350637aaac416cc8d930f6101
    # Parent  ba6afb21b6fd147e765de15c3b197f40cd608afb
    Add operator[] to Path structures (#250)
    
    diff --git a/lemon/path.h b/lemon/path.h
    a b  
    164164      return ArcIt(*this, n);
    165165    }
    166166
     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    }
     175
    167176    /// \brief The first arc of the path
    168177    const Arc& front() const {
    169178      return head.empty() ? tail.front() : head.back();
     
    367376      return ArcIt(*this, n);
    368377    }
    369378
     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    }
     387
    370388    /// \brief The first arc of the path.
    371389    const Arc& front() const {
    372390      return data.front();
     
    897915      return ArcIt(*this, n);
    898916    }
    899917
     918    /// \brief The n-th arc.
     919    ///
     920    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
     921    /// it runs in O(1) time.
     922    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     923    const Arc& operator[](int n) const {
     924      return arcs[n];
     925    }
     926
    900927    /// \brief The length of the path.
    901928    int length() const { return len; }
    902929
  • 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()