... | ... |
@@ -22,49 +22,49 @@ |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_PATH_H |
25 | 25 |
#define LEMON_PATH_H |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <algorithm> |
29 | 29 |
|
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/concepts/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup paths |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
41 | 41 |
/// |
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 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 |
/// |
|
46 |
/// LEMON path type stores just this list. As a consequence, it |
|
47 | 47 |
/// cannot enumerate the nodes of 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 |
51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
53 | 53 |
/// implementation uses two vectors for storing the front and back |
54 | 54 |
/// insertions. |
55 | 55 |
template <typename GR> |
56 | 56 |
class Path { |
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
typedef GR Digraph; |
60 | 60 |
typedef typename Digraph::Arc Arc; |
61 | 61 |
|
62 | 62 |
/// \brief Default constructor |
63 | 63 |
/// |
64 | 64 |
/// Default constructor |
65 | 65 |
Path() {} |
66 | 66 |
|
67 | 67 |
/// \brief Template copy constructor |
68 | 68 |
/// |
69 | 69 |
/// This constuctor initializes the path from any other path type. |
70 | 70 |
/// It simply makes a copy of the given path. |
... | ... |
@@ -114,57 +114,57 @@ |
114 | 114 |
if (idx >= path->length()) idx = -1; |
115 | 115 |
return *this; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
/// \brief Comparison operator |
119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
120 | 120 |
/// \brief Comparison operator |
121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
122 | 122 |
/// \brief Comparison operator |
123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
124 | 124 |
|
125 | 125 |
private: |
126 | 126 |
const Path *path; |
127 | 127 |
int idx; |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
/// \brief Length of the path. |
131 | 131 |
int length() const { return head.size() + tail.size(); } |
132 | 132 |
/// \brief Return whether the path is empty. |
133 | 133 |
bool empty() const { return head.empty() && tail.empty(); } |
134 | 134 |
|
135 | 135 |
/// \brief Reset the path to an empty one. |
136 | 136 |
void clear() { head.clear(); tail.clear(); } |
137 | 137 |
|
138 |
/// \brief The |
|
138 |
/// \brief The n-th arc. |
|
139 | 139 |
/// |
140 | 140 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
141 | 141 |
const Arc& nth(int n) const { |
142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
143 | 143 |
*(tail.begin() + (n - head.size())); |
144 | 144 |
} |
145 | 145 |
|
146 |
/// \brief Initialize arc iterator to point to the |
|
146 |
/// \brief Initialize arc iterator to point to the n-th arc |
|
147 | 147 |
/// |
148 | 148 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
149 | 149 |
ArcIt nthIt(int n) const { |
150 | 150 |
return ArcIt(*this, n); |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
/// \brief The first arc of the path |
154 | 154 |
const Arc& front() const { |
155 | 155 |
return head.empty() ? tail.front() : head.back(); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
/// \brief Add a new arc before the current path |
159 | 159 |
void addFront(const Arc& arc) { |
160 | 160 |
head.push_back(arc); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
/// \brief Erase the first arc of the path |
164 | 164 |
void eraseFront() { |
165 | 165 |
if (!head.empty()) { |
166 | 166 |
head.pop_back(); |
167 | 167 |
} else { |
168 | 168 |
head.clear(); |
169 | 169 |
int halfsize = tail.size() / 2; |
170 | 170 |
head.resize(halfsize); |
... | ... |
@@ -210,49 +210,49 @@ |
210 | 210 |
} |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
template <typename CPath> |
214 | 214 |
void buildRev(const CPath& path) { |
215 | 215 |
int len = path.length(); |
216 | 216 |
head.reserve(len); |
217 | 217 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
218 | 218 |
head.push_back(it); |
219 | 219 |
} |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
protected: |
223 | 223 |
typedef std::vector<Arc> Container; |
224 | 224 |
Container head, tail; |
225 | 225 |
|
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
/// \brief A structure for representing directed paths in a digraph. |
229 | 229 |
/// |
230 | 230 |
/// A structure for representing directed path in a digraph. |
231 | 231 |
/// \tparam GR The digraph type in which the path is. |
232 | 232 |
/// |
233 | 233 |
/// In a sense, the path can be treated as a list of arcs. The |
234 |
/// |
|
234 |
/// LEMON path type stores just this list. As a consequence it |
|
235 | 235 |
/// cannot enumerate the nodes in the path and the zero length paths |
236 | 236 |
/// cannot store the source. |
237 | 237 |
/// |
238 | 238 |
/// This implementation is a just back insertable and erasable path |
239 | 239 |
/// type. It can be indexed in O(1) time. The back insertion and |
240 | 240 |
/// erasure is amortized O(1) time. This implementation is faster |
241 | 241 |
/// then the \c Path type because it use just one vector for the |
242 | 242 |
/// arcs. |
243 | 243 |
template <typename GR> |
244 | 244 |
class SimplePath { |
245 | 245 |
public: |
246 | 246 |
|
247 | 247 |
typedef GR Digraph; |
248 | 248 |
typedef typename Digraph::Arc Arc; |
249 | 249 |
|
250 | 250 |
/// \brief Default constructor |
251 | 251 |
/// |
252 | 252 |
/// Default constructor |
253 | 253 |
SimplePath() {} |
254 | 254 |
|
255 | 255 |
/// \brief Template copy constructor |
256 | 256 |
/// |
257 | 257 |
/// This path can be initialized with any other path type. It just |
258 | 258 |
/// makes a copy of the given path. |
... | ... |
@@ -306,56 +306,56 @@ |
306 | 306 |
if (idx >= path->length()) idx = -1; |
307 | 307 |
return *this; |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
/// Comparison operator |
311 | 311 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
312 | 312 |
/// Comparison operator |
313 | 313 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
314 | 314 |
/// Comparison operator |
315 | 315 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
const SimplePath *path; |
319 | 319 |
int idx; |
320 | 320 |
}; |
321 | 321 |
|
322 | 322 |
/// \brief Length of the path. |
323 | 323 |
int length() const { return data.size(); } |
324 | 324 |
/// \brief Return true if the path is empty. |
325 | 325 |
bool empty() const { return data.empty(); } |
326 | 326 |
|
327 | 327 |
/// \brief Reset the path to an empty one. |
328 | 328 |
void clear() { data.clear(); } |
329 | 329 |
|
330 |
/// \brief The |
|
330 |
/// \brief The n-th arc. |
|
331 | 331 |
/// |
332 | 332 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
333 | 333 |
const Arc& nth(int n) const { |
334 | 334 |
return data[n]; |
335 | 335 |
} |
336 | 336 |
|
337 |
/// \brief Initializes arc iterator to point to the |
|
337 |
/// \brief Initializes arc iterator to point to the n-th arc. |
|
338 | 338 |
ArcIt nthIt(int n) const { |
339 | 339 |
return ArcIt(*this, n); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
/// \brief The first arc of the path. |
343 | 343 |
const Arc& front() const { |
344 | 344 |
return data.front(); |
345 | 345 |
} |
346 | 346 |
|
347 | 347 |
/// \brief The last arc of the path. |
348 | 348 |
const Arc& back() const { |
349 | 349 |
return data.back(); |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
/// \brief Add a new arc behind the current path. |
353 | 353 |
void addBack(const Arc& arc) { |
354 | 354 |
data.push_back(arc); |
355 | 355 |
} |
356 | 356 |
|
357 | 357 |
/// \brief Erase the last arc of the path |
358 | 358 |
void eraseBack() { |
359 | 359 |
data.pop_back(); |
360 | 360 |
} |
361 | 361 |
|
... | ... |
@@ -374,49 +374,49 @@ |
374 | 374 |
|
375 | 375 |
template <typename CPath> |
376 | 376 |
void buildRev(const CPath& path) { |
377 | 377 |
int len = path.length(); |
378 | 378 |
data.resize(len); |
379 | 379 |
int index = len; |
380 | 380 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
381 | 381 |
--index; |
382 | 382 |
data[index] = it;; |
383 | 383 |
} |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
protected: |
387 | 387 |
typedef std::vector<Arc> Container; |
388 | 388 |
Container data; |
389 | 389 |
|
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
/// \brief A structure for representing directed paths in a digraph. |
393 | 393 |
/// |
394 | 394 |
/// A structure for representing directed path in a digraph. |
395 | 395 |
/// \tparam GR The digraph type in which the path is. |
396 | 396 |
/// |
397 | 397 |
/// In a sense, the path can be treated as a list of arcs. The |
398 |
/// |
|
398 |
/// LEMON path type stores just this list. As a consequence it |
|
399 | 399 |
/// cannot enumerate the nodes in the path and the zero length paths |
400 | 400 |
/// cannot store the source. |
401 | 401 |
/// |
402 | 402 |
/// This implementation is a back and front insertable and erasable |
403 | 403 |
/// path type. It can be indexed in O(k) time, where k is the rank |
404 | 404 |
/// of the arc in the path. The length can be computed in O(n) |
405 | 405 |
/// time. The front and back insertion and erasure is O(1) time |
406 | 406 |
/// and it can be splited and spliced in O(1) time. |
407 | 407 |
template <typename GR> |
408 | 408 |
class ListPath { |
409 | 409 |
public: |
410 | 410 |
|
411 | 411 |
typedef GR Digraph; |
412 | 412 |
typedef typename Digraph::Arc Arc; |
413 | 413 |
|
414 | 414 |
protected: |
415 | 415 |
|
416 | 416 |
// the std::list<> is incompatible |
417 | 417 |
// hard to create invalid iterator |
418 | 418 |
struct Node { |
419 | 419 |
Arc arc; |
420 | 420 |
Node *next, *prev; |
421 | 421 |
}; |
422 | 422 |
|
... | ... |
@@ -483,61 +483,61 @@ |
483 | 483 |
|
484 | 484 |
///Conversion to Digraph::Arc |
485 | 485 |
operator const Arc&() const { |
486 | 486 |
return node->arc; |
487 | 487 |
} |
488 | 488 |
|
489 | 489 |
/// Next arc |
490 | 490 |
ArcIt& operator++() { |
491 | 491 |
node = node->next; |
492 | 492 |
return *this; |
493 | 493 |
} |
494 | 494 |
|
495 | 495 |
/// Comparison operator |
496 | 496 |
bool operator==(const ArcIt& e) const { return node==e.node; } |
497 | 497 |
/// Comparison operator |
498 | 498 |
bool operator!=(const ArcIt& e) const { return node!=e.node; } |
499 | 499 |
/// Comparison operator |
500 | 500 |
bool operator<(const ArcIt& e) const { return node<e.node; } |
501 | 501 |
|
502 | 502 |
private: |
503 | 503 |
const ListPath *path; |
504 | 504 |
Node *node; |
505 | 505 |
}; |
506 | 506 |
|
507 |
/// \brief The |
|
507 |
/// \brief The n-th arc. |
|
508 | 508 |
/// |
509 |
/// This function looks for the |
|
509 |
/// This function looks for the n-th arc in O(n) time. |
|
510 | 510 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
511 | 511 |
const Arc& nth(int n) const { |
512 | 512 |
Node *node = first; |
513 | 513 |
for (int i = 0; i < n; ++i) { |
514 | 514 |
node = node->next; |
515 | 515 |
} |
516 | 516 |
return node->arc; |
517 | 517 |
} |
518 | 518 |
|
519 |
/// \brief Initializes arc iterator to point to the |
|
519 |
/// \brief Initializes arc iterator to point to the n-th arc. |
|
520 | 520 |
ArcIt nthIt(int n) const { |
521 | 521 |
Node *node = first; |
522 | 522 |
for (int i = 0; i < n; ++i) { |
523 | 523 |
node = node->next; |
524 | 524 |
} |
525 | 525 |
return ArcIt(*this, node); |
526 | 526 |
} |
527 | 527 |
|
528 | 528 |
/// \brief Length of the path. |
529 | 529 |
int length() const { |
530 | 530 |
int len = 0; |
531 | 531 |
Node *node = first; |
532 | 532 |
while (node != 0) { |
533 | 533 |
node = node->next; |
534 | 534 |
++len; |
535 | 535 |
} |
536 | 536 |
return len; |
537 | 537 |
} |
538 | 538 |
|
539 | 539 |
/// \brief Return true if the path is empty. |
540 | 540 |
bool empty() const { return first == 0; } |
541 | 541 |
|
542 | 542 |
/// \brief Reset the path to an empty one. |
543 | 543 |
void clear() { |
... | ... |
@@ -714,49 +714,49 @@ |
714 | 714 |
typedef True BuildTag; |
715 | 715 |
|
716 | 716 |
template <typename CPath> |
717 | 717 |
void build(const CPath& path) { |
718 | 718 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
719 | 719 |
addBack(it); |
720 | 720 |
} |
721 | 721 |
} |
722 | 722 |
|
723 | 723 |
template <typename CPath> |
724 | 724 |
void buildRev(const CPath& path) { |
725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
726 | 726 |
addFront(it); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
}; |
731 | 731 |
|
732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
733 | 733 |
/// |
734 | 734 |
/// A structure for representing directed path in a digraph. |
735 | 735 |
/// \tparam GR The digraph type in which the path is. |
736 | 736 |
/// |
737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
738 |
/// |
|
738 |
/// LEMON path type stores just this list. As a consequence it |
|
739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
740 | 740 |
/// a zero length path is undefined. |
741 | 741 |
/// |
742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
744 | 744 |
/// modified. |
745 | 745 |
/// |
746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
747 | 747 |
/// it is intented to be |
748 | 748 |
/// used when you want to store a large number of paths. |
749 | 749 |
template <typename GR> |
750 | 750 |
class StaticPath { |
751 | 751 |
public: |
752 | 752 |
|
753 | 753 |
typedef GR Digraph; |
754 | 754 |
typedef typename Digraph::Arc Arc; |
755 | 755 |
|
756 | 756 |
/// \brief Default constructor |
757 | 757 |
/// |
758 | 758 |
/// Default constructor |
759 | 759 |
StaticPath() : len(0), arcs(0) {} |
760 | 760 |
|
761 | 761 |
/// \brief Template copy constructor |
762 | 762 |
/// |
... | ... |
@@ -810,56 +810,56 @@ |
810 | 810 |
///Conversion to Digraph::Arc |
811 | 811 |
operator const Arc&() const { |
812 | 812 |
return path->nth(idx); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
/// Next arc |
816 | 816 |
ArcIt& operator++() { |
817 | 817 |
++idx; |
818 | 818 |
if (idx >= path->length()) idx = -1; |
819 | 819 |
return *this; |
820 | 820 |
} |
821 | 821 |
|
822 | 822 |
/// Comparison operator |
823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
824 | 824 |
/// Comparison operator |
825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
826 | 826 |
/// Comparison operator |
827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
828 | 828 |
|
829 | 829 |
private: |
830 | 830 |
const StaticPath *path; |
831 | 831 |
int idx; |
832 | 832 |
}; |
833 | 833 |
|
834 |
/// \brief The |
|
834 |
/// \brief The n-th arc. |
|
835 | 835 |
/// |
836 | 836 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
837 | 837 |
const Arc& nth(int n) const { |
838 | 838 |
return arcs[n]; |
839 | 839 |
} |
840 | 840 |
|
841 |
/// \brief The arc iterator pointing to the |
|
841 |
/// \brief The arc iterator pointing to the n-th arc. |
|
842 | 842 |
ArcIt nthIt(int n) const { |
843 | 843 |
return ArcIt(*this, n); |
844 | 844 |
} |
845 | 845 |
|
846 | 846 |
/// \brief The length of the path. |
847 | 847 |
int length() const { return len; } |
848 | 848 |
|
849 | 849 |
/// \brief Return true when the path is empty. |
850 | 850 |
int empty() const { return len == 0; } |
851 | 851 |
|
852 | 852 |
/// \brief Erase all arcs in the digraph. |
853 | 853 |
void clear() { |
854 | 854 |
len = 0; |
855 | 855 |
if (arcs) delete[] arcs; |
856 | 856 |
arcs = 0; |
857 | 857 |
} |
858 | 858 |
|
859 | 859 |
/// \brief The first arc of the path. |
860 | 860 |
const Arc& front() const { |
861 | 861 |
return arcs[0]; |
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
/// \brief The last arc of the path. |
865 | 865 |
const Arc& back() const { |
... | ... |
@@ -1021,49 +1021,49 @@ |
1021 | 1021 |
return true; |
1022 | 1022 |
} |
1023 | 1023 |
|
1024 | 1024 |
/// \brief The source of a path |
1025 | 1025 |
/// |
1026 | 1026 |
/// This function returns the source node of the given path. |
1027 | 1027 |
/// If the path is empty, then it returns \c INVALID. |
1028 | 1028 |
template <typename Digraph, typename Path> |
1029 | 1029 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) { |
1030 | 1030 |
return path.empty() ? INVALID : digraph.source(path.front()); |
1031 | 1031 |
} |
1032 | 1032 |
|
1033 | 1033 |
/// \brief The target of a path |
1034 | 1034 |
/// |
1035 | 1035 |
/// This function returns the target node of the given path. |
1036 | 1036 |
/// If the path is empty, then it returns \c INVALID. |
1037 | 1037 |
template <typename Digraph, typename Path> |
1038 | 1038 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) { |
1039 | 1039 |
return path.empty() ? INVALID : digraph.target(path.back()); |
1040 | 1040 |
} |
1041 | 1041 |
|
1042 | 1042 |
/// \brief Class which helps to iterate through the nodes of a path |
1043 | 1043 |
/// |
1044 | 1044 |
/// In a sense, the path can be treated as a list of arcs. The |
1045 |
/// |
|
1045 |
/// LEMON path type stores only this list. As a consequence, it |
|
1046 | 1046 |
/// cannot enumerate the nodes in the path and the zero length paths |
1047 | 1047 |
/// cannot have a source node. |
1048 | 1048 |
/// |
1049 | 1049 |
/// This class implements the node iterator of a path structure. To |
1050 | 1050 |
/// provide this feature, the underlying digraph should be passed to |
1051 | 1051 |
/// the constructor of the iterator. |
1052 | 1052 |
template <typename Path> |
1053 | 1053 |
class PathNodeIt { |
1054 | 1054 |
private: |
1055 | 1055 |
const typename Path::Digraph *_digraph; |
1056 | 1056 |
typename Path::ArcIt _it; |
1057 | 1057 |
typename Path::Digraph::Node _nd; |
1058 | 1058 |
|
1059 | 1059 |
public: |
1060 | 1060 |
|
1061 | 1061 |
typedef typename Path::Digraph Digraph; |
1062 | 1062 |
typedef typename Digraph::Node Node; |
1063 | 1063 |
|
1064 | 1064 |
/// Default constructor |
1065 | 1065 |
PathNodeIt() {} |
1066 | 1066 |
/// Invalid constructor |
1067 | 1067 |
PathNodeIt(Invalid) |
1068 | 1068 |
: _digraph(0), _it(INVALID), _nd(INVALID) {} |
1069 | 1069 |
/// Constructor |
0 comments (0 inline)