COIN-OR::LEMON - Graph Library

Ticket #250: 250-new-ac89b1685fbc.patch

File 250-new-ac89b1685fbc.patch, 3.3 KB (added by Peter Kovacs, 6 years 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 ac89b1685fbc2bfc816d59875317146e524ccf36
    # 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();
     
    566584      return ArcIt(*this, node);
    567585    }
    568586
     587    /// \brief The n-th arc.
     588    ///
     589    /// Looks for the n-th arc in O(n) time. This operator is just an alias
     590    /// for \ref nth().
     591    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     592    const Arc& operator[](int n) const {
     593      return nth(n);
     594    }
     595
    569596    /// \brief Length of the path.
    570597    int length() const {
    571598      int len = 0;
     
    897924      return ArcIt(*this, n);
    898925    }
    899926
     927    /// \brief The n-th arc.
     928    ///
     929    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
     930    /// it runs in O(1) time.
     931    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
     932    const Arc& operator[](int n) const {
     933      return arcs[n];
     934    }
     935
    900936    /// \brief The length of the path.
    901937    int length() const { return len; }
    902938
  • 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    checkSubscriptOperator<ListPath<GR> >();
     115
    111116    checkListPathSplitAndSplice();
    112117  }
    113118
     
    273278    check(checkPath(cgr, cp), "Wrong checkPath()");
    274279  }
    275280
     281  template <typename P>
     282  void checkSubscriptOperator() {
     283    SimplePath<GR> p0;
     284    p0.addBack(a1);
     285    p0.addBack(a3);
     286    p0.addBack(a2);
     287    P p = p0;
     288    check(!p.empty(), "Wrong empty()");
     289    check(p.length() == 3, "Wrong length");
     290    check(p.front() == a1, "Wrong front()");
     291    check(p.back() == a2, "Wrong back()");
     292    check(p.nth(0) == a1, "Wrong nth()");
     293    check(p.nth(1) == a3, "Wrong nth()");
     294    check(p.nth(2) == a2, "Wrong nth()");
     295    check(p[0] == a1, "Wrong operator[]");
     296    check(p[1] == a3, "Wrong operator[]");
     297    check(p[2] == a2, "Wrong operator[]");
     298  }
     299
    276300  void checkListPathSplitAndSplice() {
    277301
    278302    // Build a path with spliceFront() and spliceBack()