# 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
|
|
164 | 164 | return ArcIt(*this, n); |
165 | 165 | } |
166 | 166 | |
| 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 | |
167 | 176 | /// \brief The first arc of the path |
168 | 177 | const Arc& front() const { |
169 | 178 | return head.empty() ? tail.front() : head.back(); |
… |
… |
|
367 | 376 | return ArcIt(*this, n); |
368 | 377 | } |
369 | 378 | |
| 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 | |
370 | 388 | /// \brief The first arc of the path. |
371 | 389 | const Arc& front() const { |
372 | 390 | return data.front(); |
… |
… |
|
566 | 584 | return ArcIt(*this, node); |
567 | 585 | } |
568 | 586 | |
| 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 | |
569 | 596 | /// \brief Length of the path. |
570 | 597 | int length() const { |
571 | 598 | int len = 0; |
… |
… |
|
897 | 924 | return ArcIt(*this, n); |
898 | 925 | } |
899 | 926 | |
| 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 | |
900 | 936 | /// \brief The length of the path. |
901 | 937 | int length() const { return len; } |
902 | 938 | |
diff --git a/test/path_test.cc b/test/path_test.cc
a
|
b
|
|
108 | 108 | checkBackAndFrontInsertablePath<ListPath<GR> >(); |
109 | 109 | checkBackInsertablePath<SimplePath<GR> >(); |
110 | 110 | |
| 111 | checkSubscriptOperator<Path<GR> >(); |
| 112 | checkSubscriptOperator<SimplePath<GR> >(); |
| 113 | checkSubscriptOperator<StaticPath<GR> >(); |
| 114 | checkSubscriptOperator<ListPath<GR> >(); |
| 115 | |
111 | 116 | checkListPathSplitAndSplice(); |
112 | 117 | } |
113 | 118 | |
… |
… |
|
273 | 278 | check(checkPath(cgr, cp), "Wrong checkPath()"); |
274 | 279 | } |
275 | 280 | |
| 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 | |
276 | 300 | void checkListPathSplitAndSplice() { |
277 | 301 | |
278 | 302 | // Build a path with spliceFront() and spliceBack() |