# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# 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
|
b
|
|
42 | 42 | /// A structure for representing directed path in a digraph. |
43 | 43 | /// \tparam GR The digraph type in which the path is. |
44 | 44 | /// |
45 | | /// In a sense, the path can be treated as a list of arcs. The |
46 | | /// LEMON path type stores just this list. As a consequence, it |
47 | | /// cannot enumerate the nodes of the path and the source node of |
| 45 | /// In a sense, a path can be treated as a list of arcs. The |
| 46 | /// LEMON path type stores just this list. As a consequence it |
| 47 | /// cannot enumerate the nodes in the path and the source node of |
48 | 48 | /// a zero length path is undefined. |
49 | 49 | /// |
50 | 50 | /// This implementation is a back and front insertable and erasable |
… |
… |
|
150 | 150 | |
151 | 151 | /// \brief The n-th arc. |
152 | 152 | /// |
153 | | /// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 153 | /// Gives back the n-th arc. This function runs in O(1) time. |
| 154 | /// \pre \c n is in the range <tt>[0..length() - 1]</tt>. |
154 | 155 | const Arc& nth(int n) const { |
155 | 156 | return n < int(head.size()) ? *(head.rbegin() + n) : |
156 | 157 | *(tail.begin() + (n - head.size())); |
… |
… |
|
162 | 163 | ArcIt nthIt(int n) const { |
163 | 164 | return ArcIt(*this, n); |
164 | 165 | } |
| 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 | } |
165 | 175 | |
166 | 176 | /// \brief The first arc of the path |
167 | 177 | const Arc& front() const { |
… |
… |
|
243 | 253 | /// A structure for representing directed path in a digraph. |
244 | 254 | /// \tparam GR The digraph type in which the path is. |
245 | 255 | /// |
246 | | /// In a sense, the path can be treated as a list of arcs. The |
| 256 | /// In a sense, a path can be treated as a list of arcs. The |
247 | 257 | /// LEMON path type stores just this list. As a consequence it |
248 | | /// cannot enumerate the nodes in the path and the zero length paths |
249 | | /// cannot store the source. |
| 258 | /// cannot enumerate the nodes in the path and the source node of |
| 259 | /// a zero length path is undefined. |
250 | 260 | /// |
251 | 261 | /// This implementation is a just back insertable and erasable path |
252 | 262 | /// type. It can be indexed in O(1) time. The back insertion and |
253 | 263 | /// erasure is amortized O(1) time. This implementation is faster |
254 | | /// then the \c Path type because it use just one vector for the |
| 264 | /// than the \c Path type because it use just one vector for the |
255 | 265 | /// arcs. |
256 | 266 | template <typename GR> |
257 | 267 | class SimplePath { |
… |
… |
|
355 | 365 | |
356 | 366 | /// \brief The n-th arc. |
357 | 367 | /// |
358 | | /// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 368 | /// Gives back the n-th arc. This function runs in O(1) time. |
| 369 | /// \pre \c n is in the range <tt>[0..length() - 1]</tt>. |
359 | 370 | const Arc& nth(int n) const { |
360 | 371 | return data[n]; |
361 | 372 | } |
… |
… |
|
364 | 375 | ArcIt nthIt(int n) const { |
365 | 376 | return ArcIt(*this, n); |
366 | 377 | } |
| 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 | } |
367 | 387 | |
368 | 388 | /// \brief The first arc of the path. |
369 | 389 | const Arc& front() const { |
… |
… |
|
420 | 440 | /// A structure for representing directed path in a digraph. |
421 | 441 | /// \tparam GR The digraph type in which the path is. |
422 | 442 | /// |
423 | | /// In a sense, the path can be treated as a list of arcs. The |
| 443 | /// In a sense, a path can be treated as a list of arcs. The |
424 | 444 | /// LEMON path type stores just this list. As a consequence it |
425 | | /// cannot enumerate the nodes in the path and the zero length paths |
426 | | /// cannot store the source. |
| 445 | /// cannot enumerate the nodes in the path and the source node of |
| 446 | /// a zero length path is undefined. |
427 | 447 | /// |
428 | 448 | /// This implementation is a back and front insertable and erasable |
429 | 449 | /// path type. It can be indexed in O(k) time, where k is the rank |
… |
… |
|
546 | 566 | /// \brief The n-th arc. |
547 | 567 | /// |
548 | 568 | /// This function looks for the n-th arc in O(n) time. |
549 | | /// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 569 | /// \pre \c n is in the range <tt>[0..length() - 1]</tt>. |
550 | 570 | const Arc& nth(int n) const { |
551 | 571 | Node *node = first; |
552 | 572 | for (int i = 0; i < n; ++i) { |
… |
… |
|
732 | 752 | /// starting with |
733 | 753 | /// \c it will put into \c tpath. If \c tpath have arcs |
734 | 754 | /// before the operation they are removed first. The time |
735 | | /// complexity of this function is O(1) plus the the time of emtying |
| 755 | /// complexity of this function is O(1) plus the time of emtying |
736 | 756 | /// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
737 | 757 | void split(ArcIt it, ListPath& tpath) { |
738 | 758 | tpath.clear(); |
… |
… |
|
773 | 793 | /// A structure for representing directed path in a digraph. |
774 | 794 | /// \tparam GR The digraph type in which the path is. |
775 | 795 | /// |
776 | | /// In a sense, the path can be treated as a list of arcs. The |
| 796 | /// In a sense, a path can be treated as a list of arcs. The |
777 | 797 | /// LEMON path type stores just this list. As a consequence it |
778 | 798 | /// cannot enumerate the nodes in the path and the source node of |
779 | 799 | /// a zero length path is undefined. |
… |
… |
|
782 | 802 | /// or copy assigned from another path, but otherwise it cannot be |
783 | 803 | /// modified. |
784 | 804 | /// |
785 | | /// Being the the most memory efficient path type in LEMON, |
| 805 | /// Being the most memory efficient path type in LEMON, |
786 | 806 | /// it is intented to be |
787 | 807 | /// used when you want to store a large number of paths. |
788 | 808 | template <typename GR> |
… |
… |
|
885 | 905 | |
886 | 906 | /// \brief The n-th arc. |
887 | 907 | /// |
888 | | /// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
| 908 | /// Gives back the n-th arc. This function runs in O(1) time. |
| 909 | /// \pre \c n is in the range <tt>[0..length() - 1]</tt>. |
889 | 910 | const Arc& nth(int n) const { |
890 | 911 | return arcs[n]; |
891 | 912 | } |
… |
… |
|
895 | 916 | return ArcIt(*this, n); |
896 | 917 | } |
897 | 918 | |
| 919 | /// \brief The n-th arc. |
| 920 | /// |
| 921 | /// Gives back the n-th arc. This operator is just an alias for \ref nth(), |
| 922 | /// it runs in O(1) time. |
| 923 | /// \pre \c n is in the range <tt>[0..length() - 1]</tt>. |
| 924 | const Arc& operator[](int n) const { |
| 925 | return arcs[n]; |
| 926 | } |
| 927 | |
898 | 928 | /// \brief The length of the path. |
899 | 929 | int length() const { return len; } |
900 | 930 | |
901 | 931 | /// \brief Return true when the path is empty. |
902 | 932 | int empty() const { return len == 0; } |
903 | 933 | |
904 | | /// \brief Erase all arcs in the digraph. |
| 934 | /// \brief Reset the path to an empty one. |
905 | 935 | void clear() { |
906 | 936 | len = 0; |
907 | 937 | if (arcs) delete[] arcs; |
… |
… |
|
984 | 1014 | bool buildEnable = BuildTagIndicator<To>::value> |
985 | 1015 | struct PathCopySelectorForward { |
986 | 1016 | static void copy(const From& from, To& to) { |
| 1017 | std::cout << "standard path copy...\n"; |
987 | 1018 | to.clear(); |
988 | 1019 | for (typename From::ArcIt it(from); it != INVALID; ++it) { |
989 | 1020 | to.addBack(it); |
… |
… |
|
1091 | 1122 | return path.empty() ? INVALID : digraph.target(path.back()); |
1092 | 1123 | } |
1093 | 1124 | |
1094 | | /// \brief Class which helps to iterate through the nodes of a path |
| 1125 | /// \brief Class for iterating through the nodes of a path |
1095 | 1126 | /// |
1096 | | /// In a sense, the path can be treated as a list of arcs. The |
1097 | | /// LEMON path type stores only this list. As a consequence, it |
1098 | | /// cannot enumerate the nodes in the path and the zero length paths |
1099 | | /// cannot have a source node. |
| 1127 | /// Class for iterating through the nodes of a path. |
1100 | 1128 | /// |
1101 | | /// This class implements the node iterator of a path structure. To |
1102 | | /// provide this feature, the underlying digraph should be passed to |
| 1129 | /// In a sense, a path can be treated as a list of arcs. The |
| 1130 | /// LEMON path type stores only this list. As a consequence it |
| 1131 | /// cannot enumerate the nodes in the path and the source node of |
| 1132 | /// a zero length path is undefined. |
| 1133 | /// |
| 1134 | /// However, this class implements a node iterator for path structures. |
| 1135 | /// To provide this feature, the underlying digraph should be passed to |
1103 | 1136 | /// the constructor of the iterator. |
1104 | 1137 | template <typename Path> |
1105 | 1138 | class PathNodeIt { |
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 | |
111 | 115 | checkListPathSplitAndSplice(); |
112 | 116 | } |
113 | 117 | |
… |
… |
|
273 | 277 | check(checkPath(cgr, cp), "Wrong checkPath()"); |
274 | 278 | } |
275 | 279 | |
| 280 | template <typename P> |
| 281 | void checkSubscriptOperator() { |
| 282 | SimplePath<GR> p0; |
| 283 | p0.addBack(a1); |
| 284 | p0.addBack(a3); |
| 285 | p0.addBack(a2); |
| 286 | P p = p0; |
| 287 | check(!p.empty(), "Wrong empty()"); |
| 288 | check(p.length() == 3, "Wrong length"); |
| 289 | check(p.front() == a1, "Wrong front()"); |
| 290 | check(p.back() == a2, "Wrong back()"); |
| 291 | check(p.nth(0) == a1, "Wrong nth()"); |
| 292 | check(p.nth(1) == a3, "Wrong nth()"); |
| 293 | check(p.nth(2) == a2, "Wrong nth()"); |
| 294 | check(p[0] == a1, "Wrong operator[]"); |
| 295 | check(p[1] == a3, "Wrong operator[]"); |
| 296 | check(p[2] == a2, "Wrong operator[]"); |
| 297 | } |
| 298 | |
276 | 299 | void checkListPathSplitAndSplice() { |
277 | 300 | |
278 | 301 | // Build a path with spliceFront() and spliceBack() |