[Lemon-commits] Peter Kovacs: Add operator[] to Path structures ...
Lemon HG
hg at lemon.cs.elte.hu
Thu Nov 1 20:05:45 CET 2018
details: http://lemon.cs.elte.hu/hg/lemon/rev/4fd76139b69e
changeset: 1421:4fd76139b69e
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Sat Feb 17 23:44:32 2018 +0100
description:
Add operator[] to Path structures (#250)
diffstat:
lemon/path.h | 36 ++++++++++++++++++++++++++++++++++++
test/path_test.cc | 24 ++++++++++++++++++++++++
2 files changed, 60 insertions(+), 0 deletions(-)
diffs (108 lines):
diff --git a/lemon/path.h b/lemon/path.h
--- a/lemon/path.h
+++ b/lemon/path.h
@@ -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 <tt>[0..length() - 1]</tt>.
+ 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 <tt>[0..length() - 1]</tt>.
+ 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 <tt>[0..length() - 1]</tt>.
+ 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 <tt>[0..length() - 1]</tt>.
+ const Arc& operator[](int n) const {
+ return _arcs[n];
+ }
+
/// \brief The length of the path.
int length() const { return len; }
diff --git a/test/path_test.cc b/test/path_test.cc
--- a/test/path_test.cc
+++ b/test/path_test.cc
@@ -108,6 +108,11 @@
checkBackAndFrontInsertablePath<ListPath<GR> >();
checkBackInsertablePath<SimplePath<GR> >();
+ checkSubscriptOperator<Path<GR> >();
+ checkSubscriptOperator<SimplePath<GR> >();
+ checkSubscriptOperator<StaticPath<GR> >();
+ checkSubscriptOperator<ListPath<GR> >();
+
checkListPathSplitAndSplice();
}
@@ -273,6 +278,25 @@
check(checkPath(cgr, cp), "Wrong checkPath()");
}
+ template <typename P>
+ void checkSubscriptOperator() {
+ SimplePath<GR> 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()
More information about the Lemon-commits
mailing list