Add operator[] to Path structures (#250) default tip
authorPeter Kovacs <kpeter@inf.elte.hu>
Sat, 17 Feb 2018 23:44:32 +0100
changeset 12024fd76139b69e
parent 1201 1f4f01870c1e
Add operator[] to Path structures (#250)
lemon/path.h
test/path_test.cc
     1.1 --- a/lemon/path.h	Sat Feb 17 23:44:15 2018 +0100
     1.2 +++ b/lemon/path.h	Sat Feb 17 23:44:32 2018 +0100
     1.3 @@ -182,6 +182,15 @@
     1.4        return ArcIt(*this, n);
     1.5      }
     1.6  
     1.7 +    /// \brief The n-th arc.
     1.8 +    ///
     1.9 +    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
    1.10 +    /// it runs in O(1) time.
    1.11 +    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    1.12 +    const Arc& operator[](int n) const {
    1.13 +      return nth(n);
    1.14 +    }
    1.15 +
    1.16      /// \brief The first arc of the path
    1.17      const Arc& front() const {
    1.18        return head.empty() ? tail.front() : head.back();
    1.19 @@ -402,6 +411,15 @@
    1.20        return ArcIt(*this, n);
    1.21      }
    1.22  
    1.23 +    /// \brief The n-th arc.
    1.24 +    ///
    1.25 +    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
    1.26 +    /// it runs in O(1) time.
    1.27 +    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    1.28 +    const Arc& operator[](int n) const {
    1.29 +      return data[n];
    1.30 +    }
    1.31 +
    1.32      /// \brief The first arc of the path.
    1.33      const Arc& front() const {
    1.34        return data.front();
    1.35 @@ -618,6 +636,15 @@
    1.36        return ArcIt(*this, node);
    1.37      }
    1.38  
    1.39 +    /// \brief The n-th arc.
    1.40 +    ///
    1.41 +    /// Looks for the n-th arc in O(n) time. This operator is just an alias
    1.42 +    /// for \ref nth().
    1.43 +    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    1.44 +    const Arc& operator[](int n) const {
    1.45 +      return nth(n);
    1.46 +    }
    1.47 +
    1.48      /// \brief Length of the path.
    1.49      int length() const {
    1.50        int len = 0;
    1.51 @@ -966,6 +993,15 @@
    1.52        return ArcIt(*this, n);
    1.53      }
    1.54  
    1.55 +    /// \brief The n-th arc.
    1.56 +    ///
    1.57 +    /// Gives back the n-th arc. This operator is just an alias for \ref nth(),
    1.58 +    /// it runs in O(1) time.
    1.59 +    /// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
    1.60 +    const Arc& operator[](int n) const {
    1.61 +      return _arcs[n];
    1.62 +    }
    1.63 +
    1.64      /// \brief The length of the path.
    1.65      int length() const { return len; }
    1.66  
     2.1 --- a/test/path_test.cc	Sat Feb 17 23:44:15 2018 +0100
     2.2 +++ b/test/path_test.cc	Sat Feb 17 23:44:32 2018 +0100
     2.3 @@ -108,6 +108,11 @@
     2.4      checkBackAndFrontInsertablePath<ListPath<GR> >();
     2.5      checkBackInsertablePath<SimplePath<GR> >();
     2.6  
     2.7 +    checkSubscriptOperator<Path<GR> >();
     2.8 +    checkSubscriptOperator<SimplePath<GR> >();
     2.9 +    checkSubscriptOperator<StaticPath<GR> >();
    2.10 +    checkSubscriptOperator<ListPath<GR> >();
    2.11 +
    2.12      checkListPathSplitAndSplice();
    2.13    }
    2.14  
    2.15 @@ -273,6 +278,25 @@
    2.16      check(checkPath(cgr, cp), "Wrong checkPath()");
    2.17    }
    2.18  
    2.19 +  template <typename P>
    2.20 +  void checkSubscriptOperator() {
    2.21 +    SimplePath<GR> p0;
    2.22 +    p0.addBack(a1);
    2.23 +    p0.addBack(a3);
    2.24 +    p0.addBack(a2);
    2.25 +    P p = p0;
    2.26 +    check(!p.empty(), "Wrong empty()");
    2.27 +    check(p.length() == 3, "Wrong length");
    2.28 +    check(p.front() == a1, "Wrong front()");
    2.29 +    check(p.back() == a2, "Wrong back()");
    2.30 +    check(p.nth(0) == a1, "Wrong nth()");
    2.31 +    check(p.nth(1) == a3, "Wrong nth()");
    2.32 +    check(p.nth(2) == a2, "Wrong nth()");
    2.33 +    check(p[0] == a1, "Wrong operator[]");
    2.34 +    check(p[1] == a3, "Wrong operator[]");
    2.35 +    check(p[2] == a2, "Wrong operator[]");
    2.36 +  }
    2.37 +
    2.38    void checkListPathSplitAndSplice() {
    2.39  
    2.40      // Build a path with spliceFront() and spliceBack()