# HG changeset patch
# User Peter Kovacs
# Date 1362737398 -3600
# Node ID eb2f42b62296f5f6feec449b010506b0e30cd1e5
# Parent 15d7c5eadacadbf50145095312677c6837327282
Add operator[] to some Path structures + improve API doc (#250)
diff --git a/lemon/path.h b/lemon/path.h
--- a/lemon/path.h
+++ b/lemon/path.h
@@ -42,9 +42,9 @@
/// A structure for representing directed path in a digraph.
/// \tparam GR The digraph type in which the path is.
///
- /// In a sense, the path can be treated as a list of arcs. The
- /// LEMON path type stores just this list. As a consequence, it
- /// cannot enumerate the nodes of the path and the source node of
+ /// In a sense, a path can be treated as a list of arcs. The
+ /// LEMON path type stores just this list. As a consequence it
+ /// cannot enumerate the nodes in the path and the source node of
/// a zero length path is undefined.
///
/// This implementation is a back and front insertable and erasable
@@ -150,7 +150,8 @@
/// \brief The n-th arc.
///
- /// \pre \c n is in the `[0..length() - 1]` range.
+ /// Gives back the n-th arc. This function runs in O(1) time.
+ /// \pre \c n is in the range `[0..length() - 1]`.
const Arc& nth(int n) const {
return n < int(head.size()) ? *(head.rbegin() + n) :
*(tail.begin() + (n - head.size()));
@@ -162,6 +163,15 @@
ArcIt nthIt(int n) const {
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 {
@@ -243,15 +253,15 @@
/// A structure for representing directed path in a digraph.
/// \tparam GR The digraph type in which the path is.
///
- /// In a sense, the path can be treated as a list of arcs. The
+ /// In a sense, a path can be treated as a list of arcs. The
/// LEMON path type stores just this list. As a consequence it
- /// cannot enumerate the nodes in the path and the zero length paths
- /// cannot store the source.
+ /// cannot enumerate the nodes in the path and the source node of
+ /// a zero length path is undefined.
///
/// This implementation is a just back insertable and erasable path
/// type. It can be indexed in O(1) time. The back insertion and
/// erasure is amortized O(1) time. This implementation is faster
- /// then the \c Path type because it use just one vector for the
+ /// than the \c Path type because it use just one vector for the
/// arcs.
template
class SimplePath {
@@ -355,7 +365,8 @@
/// \brief The n-th arc.
///
- /// \pre \c n is in the `[0..length() - 1]` range.
+ /// Gives back the n-th arc. This function runs in O(1) time.
+ /// \pre \c n is in the range `[0..length() - 1]`.
const Arc& nth(int n) const {
return data[n];
}
@@ -364,6 +375,15 @@
ArcIt nthIt(int n) const {
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 {
@@ -420,10 +440,10 @@
/// A structure for representing directed path in a digraph.
/// \tparam GR The digraph type in which the path is.
///
- /// In a sense, the path can be treated as a list of arcs. The
+ /// In a sense, a path can be treated as a list of arcs. The
/// LEMON path type stores just this list. As a consequence it
- /// cannot enumerate the nodes in the path and the zero length paths
- /// cannot store the source.
+ /// cannot enumerate the nodes in the path and the source node of
+ /// a zero length path is undefined.
///
/// This implementation is a back and front insertable and erasable
/// path type. It can be indexed in O(k) time, where k is the rank
@@ -546,7 +566,7 @@
/// \brief The n-th arc.
///
/// This function looks for the n-th arc in O(n) time.
- /// \pre \c n is in the `[0..length() - 1]` range.
+ /// \pre \c n is in the range `[0..length() - 1]`.
const Arc& nth(int n) const {
Node *node = first;
for (int i = 0; i < n; ++i) {
@@ -732,7 +752,7 @@
/// starting with
/// \c it will put into \c tpath. If \c tpath have arcs
/// before the operation they are removed first. The time
- /// complexity of this function is O(1) plus the the time of emtying
+ /// complexity of this function is O(1) plus the time of emtying
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath
void split(ArcIt it, ListPath& tpath) {
tpath.clear();
@@ -773,7 +793,7 @@
/// A structure for representing directed path in a digraph.
/// \tparam GR The digraph type in which the path is.
///
- /// In a sense, the path can be treated as a list of arcs. The
+ /// In a sense, a path can be treated as a list of arcs. The
/// LEMON path type stores just this list. As a consequence it
/// cannot enumerate the nodes in the path and the source node of
/// a zero length path is undefined.
@@ -782,7 +802,7 @@
/// or copy assigned from another path, but otherwise it cannot be
/// modified.
///
- /// Being the the most memory efficient path type in LEMON,
+ /// Being the most memory efficient path type in LEMON,
/// it is intented to be
/// used when you want to store a large number of paths.
template
@@ -885,7 +905,8 @@
/// \brief The n-th arc.
///
- /// \pre \c n is in the `[0..length() - 1]` range.
+ /// Gives back the n-th arc. This function runs in O(1) time.
+ /// \pre \c n is in the range `[0..length() - 1]`.
const Arc& nth(int n) const {
return arcs[n];
}
@@ -895,13 +916,22 @@
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; }
/// \brief Return true when the path is empty.
int empty() const { return len == 0; }
- /// \brief Erase all arcs in the digraph.
+ /// \brief Reset the path to an empty one.
void clear() {
len = 0;
if (arcs) delete[] arcs;
@@ -984,6 +1014,7 @@
bool buildEnable = BuildTagIndicator::value>
struct PathCopySelectorForward {
static void copy(const From& from, To& to) {
+ std::cout << "standard path copy...\n";
to.clear();
for (typename From::ArcIt it(from); it != INVALID; ++it) {
to.addBack(it);
@@ -1091,15 +1122,17 @@
return path.empty() ? INVALID : digraph.target(path.back());
}
- /// \brief Class which helps to iterate through the nodes of a path
+ /// \brief Class for iterating through the nodes of a path
///
- /// In a sense, the path can be treated as a list of arcs. The
- /// LEMON path type stores only this list. As a consequence, it
- /// cannot enumerate the nodes in the path and the zero length paths
- /// cannot have a source node.
+ /// Class for iterating through the nodes of a path.
///
- /// This class implements the node iterator of a path structure. To
- /// provide this feature, the underlying digraph should be passed to
+ /// In a sense, a path can be treated as a list of arcs. The
+ /// LEMON path type stores only this list. As a consequence it
+ /// cannot enumerate the nodes in the path and the source node of
+ /// a zero length path is undefined.
+ ///
+ /// However, this class implements a node iterator for path structures.
+ /// To provide this feature, the underlying digraph should be passed to
/// the constructor of the iterator.
template
class PathNodeIt {
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,10 @@
checkBackAndFrontInsertablePath >();
checkBackInsertablePath >();
+ checkSubscriptOperator >();
+ checkSubscriptOperator >();
+ checkSubscriptOperator >();
+
checkListPathSplitAndSplice();
}
@@ -273,6 +277,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()