# HG changeset patch # User Peter Kovacs # Date 1518907472 -3600 # Node ID 4fd76139b69ec938db4d67ce0927ad5923647ed2 # Parent 1f4f01870c1e20fd9ea3c999805f0de2c161c772 Add operator[] to Path structures (#250) diff -r 1f4f01870c1e -r 4fd76139b69e lemon/path.h --- a/lemon/path.h Sat Feb 17 23:44:15 2018 +0100 +++ b/lemon/path.h Sat Feb 17 23:44:32 2018 +0100 @@ -182,6 +182,15 @@ return ArcIt(*this, n); } + /// \brief The n-th arc. + /// + /// Gives back the n-th arc. This operator is just an alias for \ref nth(), + /// it runs in O(1) time. + /// \pre \c n is in the range [0..length() - 1]. + const Arc& operator[](int n) const { + return nth(n); + } + /// \brief The first arc of the path const Arc& front() const { return head.empty() ? tail.front() : head.back(); @@ -402,6 +411,15 @@ return ArcIt(*this, n); } + /// \brief The n-th arc. + /// + /// Gives back the n-th arc. This operator is just an alias for \ref nth(), + /// it runs in O(1) time. + /// \pre \c n is in the range [0..length() - 1]. + const Arc& operator[](int n) const { + return data[n]; + } + /// \brief The first arc of the path. const Arc& front() const { return data.front(); @@ -618,6 +636,15 @@ return ArcIt(*this, node); } + /// \brief The n-th arc. + /// + /// Looks for the n-th arc in O(n) time. This operator is just an alias + /// for \ref nth(). + /// \pre \c n is in the range [0..length() - 1]. + const Arc& operator[](int n) const { + return nth(n); + } + /// \brief Length of the path. int length() const { int len = 0; @@ -966,6 +993,15 @@ return ArcIt(*this, n); } + /// \brief The n-th arc. + /// + /// Gives back the n-th arc. This operator is just an alias for \ref nth(), + /// it runs in O(1) time. + /// \pre \c n is in the range [0..length() - 1]. + const Arc& operator[](int n) const { + return _arcs[n]; + } + /// \brief The length of the path. int length() const { return len; } diff -r 1f4f01870c1e -r 4fd76139b69e test/path_test.cc --- a/test/path_test.cc Sat Feb 17 23:44:15 2018 +0100 +++ b/test/path_test.cc Sat Feb 17 23:44:32 2018 +0100 @@ -108,6 +108,11 @@ checkBackAndFrontInsertablePath >(); checkBackInsertablePath >(); + checkSubscriptOperator >(); + checkSubscriptOperator >(); + checkSubscriptOperator >(); + checkSubscriptOperator >(); + checkListPathSplitAndSplice(); } @@ -273,6 +278,25 @@ check(checkPath(cgr, cp), "Wrong checkPath()"); } + template + void checkSubscriptOperator() { + SimplePath p0; + p0.addBack(a1); + p0.addBack(a3); + p0.addBack(a2); + P p = p0; + check(!p.empty(), "Wrong empty()"); + check(p.length() == 3, "Wrong length"); + check(p.front() == a1, "Wrong front()"); + check(p.back() == a2, "Wrong back()"); + check(p.nth(0) == a1, "Wrong nth()"); + check(p.nth(1) == a3, "Wrong nth()"); + check(p.nth(2) == a2, "Wrong nth()"); + check(p[0] == a1, "Wrong operator[]"); + check(p[1] == a3, "Wrong operator[]"); + check(p[2] == a2, "Wrong operator[]"); + } + void checkListPathSplitAndSplice() { // Build a path with spliceFront() and spliceBack()