1 |
1 |
/* -*- C++ -*-
|
2 |
2 |
*
|
3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
4 |
4 |
*
|
5 |
5 |
* Copyright (C) 2003-2008
|
6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
8 |
8 |
*
|
9 |
9 |
* Permission to use, modify and distribute this software is granted
|
10 |
10 |
* provided that this copyright notice appears in all copies. For
|
11 |
11 |
* precise terms see the accompanying LICENSE file.
|
12 |
12 |
*
|
13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
15 |
15 |
* purpose.
|
16 |
16 |
*
|
17 |
17 |
*/
|
18 |
18 |
|
19 |
19 |
///\ingroup paths
|
20 |
20 |
///\file
|
21 |
21 |
///\brief Classes for representing paths in digraphs.
|
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 |
|
#include <lemon/path_utils.h>
|
31 |
30 |
#include <lemon/error.h>
|
32 |
31 |
#include <lemon/bits/invalid.h>
|
33 |
32 |
|
34 |
33 |
namespace lemon {
|
35 |
34 |
|
36 |
35 |
/// \addtogroup paths
|
37 |
36 |
/// @{
|
38 |
37 |
|
39 |
38 |
|
40 |
39 |
/// \brief A structure for representing directed paths in a digraph.
|
41 |
40 |
///
|
42 |
41 |
/// A structure for representing directed path in a digraph.
|
43 |
42 |
/// \param Digraph The digraph type in which the path is.
|
44 |
43 |
///
|
45 |
44 |
/// In a sense, the path can be treated as a list of arcs. The
|
46 |
45 |
/// lemon path type stores just this list. As a consequence, it
|
47 |
46 |
/// cannot enumerate the nodes of the path and the source node of
|
48 |
47 |
/// a zero length path is undefined.
|
49 |
48 |
///
|
50 |
49 |
/// This implementation is a back and front insertable and erasable
|
51 |
50 |
/// path type. It can be indexed in O(1) time. The front and back
|
52 |
51 |
/// insertion and erase is done in O(1) (amortized) time. The
|
53 |
52 |
/// implementation uses two vectors for storing the front and back
|
54 |
53 |
/// insertions.
|
55 |
54 |
template <typename _Digraph>
|
56 |
55 |
class Path {
|
57 |
56 |
public:
|
58 |
57 |
|
59 |
58 |
typedef _Digraph Digraph;
|
60 |
59 |
typedef typename Digraph::Arc Arc;
|
61 |
60 |
|
62 |
61 |
/// \brief Default constructor
|
63 |
62 |
///
|
64 |
63 |
/// Default constructor
|
65 |
64 |
Path() {}
|
66 |
65 |
|
67 |
66 |
/// \brief Template copy constructor
|
68 |
67 |
///
|
69 |
68 |
/// This constuctor initializes the path from any other path type.
|
70 |
69 |
/// It simply makes a copy of the given path.
|
71 |
70 |
template <typename CPath>
|
72 |
71 |
Path(const CPath& cpath) {
|
73 |
72 |
copyPath(*this, cpath);
|
74 |
73 |
}
|
75 |
74 |
|
76 |
75 |
/// \brief Template copy assignment
|
77 |
76 |
///
|
78 |
77 |
/// This operator makes a copy of a path of any other type.
|
79 |
78 |
template <typename CPath>
|
80 |
79 |
Path& operator=(const CPath& cpath) {
|
81 |
80 |
copyPath(*this, cpath);
|
82 |
81 |
return *this;
|
83 |
82 |
}
|
84 |
83 |
|
85 |
84 |
/// \brief Lemon style iterator for path arcs
|
86 |
85 |
///
|
87 |
86 |
/// This class is used to iterate on the arcs of the paths.
|
88 |
87 |
class ArcIt {
|
89 |
88 |
friend class Path;
|
90 |
89 |
public:
|
91 |
90 |
/// \brief Default constructor
|
92 |
91 |
ArcIt() {}
|
93 |
92 |
/// \brief Invalid constructor
|
94 |
93 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
95 |
94 |
/// \brief Initializate the iterator to the first arc of path
|
96 |
95 |
ArcIt(const Path &_path)
|
97 |
96 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
98 |
97 |
|
99 |
98 |
private:
|
100 |
99 |
|
101 |
100 |
ArcIt(const Path &_path, int _idx)
|
102 |
101 |
: path(&_path), idx(_idx) {}
|
103 |
102 |
|
104 |
103 |
public:
|
105 |
104 |
|
106 |
105 |
/// \brief Conversion to Arc
|
107 |
106 |
operator const Arc&() const {
|
108 |
107 |
return path->nth(idx);
|
109 |
108 |
}
|
110 |
109 |
|
111 |
110 |
/// \brief Next arc
|
112 |
111 |
ArcIt& operator++() {
|
113 |
112 |
++idx;
|
114 |
113 |
if (idx >= path->length()) idx = -1;
|
115 |
114 |
return *this;
|
116 |
115 |
}
|
117 |
116 |
|
118 |
117 |
/// \brief Comparison operator
|
119 |
118 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
120 |
119 |
/// \brief Comparison operator
|
121 |
120 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
122 |
121 |
/// \brief Comparison operator
|
123 |
122 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
124 |
123 |
|
125 |
124 |
private:
|
126 |
125 |
const Path *path;
|
127 |
126 |
int idx;
|
128 |
127 |
};
|
129 |
128 |
|
130 |
129 |
/// \brief Length of the path.
|
131 |
130 |
int length() const { return head.size() + tail.size(); }
|
132 |
131 |
/// \brief Return whether the path is empty.
|
133 |
132 |
bool empty() const { return head.empty() && tail.empty(); }
|
134 |
133 |
|
135 |
134 |
/// \brief Reset the path to an empty one.
|
136 |
135 |
void clear() { head.clear(); tail.clear(); }
|
137 |
136 |
|
138 |
137 |
/// \brief The nth arc.
|
139 |
138 |
///
|
140 |
139 |
/// \pre n is in the [0..length() - 1] range
|
141 |
140 |
const Arc& nth(int n) const {
|
142 |
141 |
return n < int(head.size()) ? *(head.rbegin() + n) :
|
143 |
142 |
*(tail.begin() + (n - head.size()));
|
144 |
143 |
}
|
145 |
144 |
|
146 |
145 |
/// \brief Initialize arc iterator to point to the nth arc
|
147 |
146 |
///
|
148 |
147 |
/// \pre n is in the [0..length() - 1] range
|
149 |
148 |
ArcIt nthIt(int n) const {
|
150 |
149 |
return ArcIt(*this, n);
|
151 |
150 |
}
|
152 |
151 |
|
153 |
152 |
/// \brief The first arc of the path
|
154 |
153 |
const Arc& front() const {
|
155 |
154 |
return head.empty() ? tail.front() : head.back();
|
156 |
155 |
}
|
157 |
156 |
|
158 |
157 |
/// \brief Add a new arc before the current path
|
159 |
158 |
void addFront(const Arc& arc) {
|
160 |
159 |
head.push_back(arc);
|
161 |
160 |
}
|
162 |
161 |
|
163 |
162 |
/// \brief Erase the first arc of the path
|
164 |
163 |
void eraseFront() {
|
165 |
164 |
if (!head.empty()) {
|
166 |
165 |
head.pop_back();
|
167 |
166 |
} else {
|
168 |
167 |
head.clear();
|
169 |
168 |
int halfsize = tail.size() / 2;
|
170 |
169 |
head.resize(halfsize);
|
171 |
170 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
|
172 |
171 |
head.rbegin());
|
173 |
172 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
|
174 |
173 |
tail.resize(tail.size() - halfsize - 1);
|
175 |
174 |
}
|
176 |
175 |
}
|
177 |
176 |
|
178 |
177 |
/// \brief The last arc of the path
|
179 |
178 |
const Arc& back() const {
|
180 |
179 |
return tail.empty() ? head.front() : tail.back();
|
181 |
180 |
}
|
182 |
181 |
|
183 |
182 |
/// \brief Add a new arc behind the current path
|
184 |
183 |
void addBack(const Arc& arc) {
|
185 |
184 |
tail.push_back(arc);
|
186 |
185 |
}
|
187 |
186 |
|
188 |
187 |
/// \brief Erase the last arc of the path
|
189 |
188 |
void eraseBack() {
|
190 |
189 |
if (!tail.empty()) {
|
191 |
190 |
tail.pop_back();
|
192 |
191 |
} else {
|
193 |
192 |
int halfsize = head.size() / 2;
|
194 |
193 |
tail.resize(halfsize);
|
195 |
194 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1,
|
196 |
195 |
tail.rbegin());
|
197 |
196 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
|
198 |
197 |
head.resize(head.size() - halfsize - 1);
|
199 |
198 |
}
|
200 |
199 |
}
|
201 |
200 |
|
202 |
201 |
typedef True BuildTag;
|
203 |
202 |
|
204 |
203 |
template <typename CPath>
|
205 |
204 |
void build(const CPath& path) {
|
206 |
205 |
int len = path.length();
|
207 |
206 |
tail.reserve(len);
|
208 |
207 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
209 |
208 |
tail.push_back(it);
|
210 |
209 |
}
|
211 |
210 |
}
|
212 |
211 |
|
213 |
212 |
template <typename CPath>
|
214 |
213 |
void buildRev(const CPath& path) {
|
215 |
214 |
int len = path.length();
|
216 |
215 |
head.reserve(len);
|
217 |
216 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
218 |
217 |
head.push_back(it);
|
219 |
218 |
}
|
220 |
219 |
}
|
221 |
220 |
|
222 |
221 |
protected:
|
... |
... |
@@ -707,197 +706,373 @@
|
707 |
706 |
first = last = 0;
|
708 |
707 |
}
|
709 |
708 |
it.node->prev = 0;
|
710 |
709 |
}
|
711 |
710 |
}
|
712 |
711 |
|
713 |
712 |
|
714 |
713 |
typedef True BuildTag;
|
715 |
714 |
|
716 |
715 |
template <typename CPath>
|
717 |
716 |
void build(const CPath& path) {
|
718 |
717 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
719 |
718 |
addBack(it);
|
720 |
719 |
}
|
721 |
720 |
}
|
722 |
721 |
|
723 |
722 |
template <typename CPath>
|
724 |
723 |
void buildRev(const CPath& path) {
|
725 |
724 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
726 |
725 |
addFront(it);
|
727 |
726 |
}
|
728 |
727 |
}
|
729 |
728 |
|
730 |
729 |
};
|
731 |
730 |
|
732 |
731 |
/// \brief A structure for representing directed paths in a digraph.
|
733 |
732 |
///
|
734 |
733 |
/// A structure for representing directed path in a digraph.
|
735 |
734 |
/// \param Digraph The digraph type in which the path is.
|
736 |
735 |
///
|
737 |
736 |
/// In a sense, the path can be treated as a list of arcs. The
|
738 |
737 |
/// lemon path type stores just this list. As a consequence it
|
739 |
738 |
/// cannot enumerate the nodes in the path and the source node of
|
740 |
739 |
/// a zero length path is undefined.
|
741 |
740 |
///
|
742 |
741 |
/// This implementation is completly static, i.e. it can be copy constucted
|
743 |
742 |
/// or copy assigned from another path, but otherwise it cannot be
|
744 |
743 |
/// modified.
|
745 |
744 |
///
|
746 |
745 |
/// Being the the most memory efficient path type in LEMON,
|
747 |
746 |
/// it is intented to be
|
748 |
747 |
/// used when you want to store a large number of paths.
|
749 |
748 |
template <typename _Digraph>
|
750 |
749 |
class StaticPath {
|
751 |
750 |
public:
|
752 |
751 |
|
753 |
752 |
typedef _Digraph Digraph;
|
754 |
753 |
typedef typename Digraph::Arc Arc;
|
755 |
754 |
|
756 |
755 |
/// \brief Default constructor
|
757 |
756 |
///
|
758 |
757 |
/// Default constructor
|
759 |
758 |
StaticPath() : len(0), arcs(0) {}
|
760 |
759 |
|
761 |
760 |
/// \brief Template copy constructor
|
762 |
761 |
///
|
763 |
762 |
/// This path can be initialized from any other path type.
|
764 |
763 |
template <typename CPath>
|
765 |
764 |
StaticPath(const CPath& cpath) : arcs(0) {
|
766 |
765 |
copyPath(*this, cpath);
|
767 |
766 |
}
|
768 |
767 |
|
769 |
768 |
/// \brief Destructor of the path
|
770 |
769 |
///
|
771 |
770 |
/// Destructor of the path
|
772 |
771 |
~StaticPath() {
|
773 |
772 |
if (arcs) delete[] arcs;
|
774 |
773 |
}
|
775 |
774 |
|
776 |
775 |
/// \brief Template copy assignment
|
777 |
776 |
///
|
778 |
777 |
/// This path can be made equal to any other path type. It simply
|
779 |
778 |
/// makes a copy of the given path.
|
780 |
779 |
template <typename CPath>
|
781 |
780 |
StaticPath& operator=(const CPath& cpath) {
|
782 |
781 |
copyPath(*this, cpath);
|
783 |
782 |
return *this;
|
784 |
783 |
}
|
785 |
784 |
|
786 |
785 |
/// \brief Iterator class to iterate on the arcs of the paths
|
787 |
786 |
///
|
788 |
787 |
/// This class is used to iterate on the arcs of the paths
|
789 |
788 |
///
|
790 |
789 |
/// Of course it converts to Digraph::Arc
|
791 |
790 |
class ArcIt {
|
792 |
791 |
friend class StaticPath;
|
793 |
792 |
public:
|
794 |
793 |
/// Default constructor
|
795 |
794 |
ArcIt() {}
|
796 |
795 |
/// Invalid constructor
|
797 |
796 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
798 |
797 |
/// Initializate the constructor to the first arc of path
|
799 |
798 |
ArcIt(const StaticPath &_path)
|
800 |
799 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
801 |
800 |
|
802 |
801 |
private:
|
803 |
802 |
|
804 |
803 |
/// Constructor with starting point
|
805 |
804 |
ArcIt(const StaticPath &_path, int _idx)
|
806 |
805 |
: idx(_idx), path(&_path) {}
|
807 |
806 |
|
808 |
807 |
public:
|
809 |
808 |
|
810 |
809 |
///Conversion to Digraph::Arc
|
811 |
810 |
operator const Arc&() const {
|
812 |
811 |
return path->nth(idx);
|
813 |
812 |
}
|
814 |
813 |
|
815 |
814 |
/// Next arc
|
816 |
815 |
ArcIt& operator++() {
|
817 |
816 |
++idx;
|
818 |
817 |
if (idx >= path->length()) idx = -1;
|
819 |
818 |
return *this;
|
820 |
819 |
}
|
821 |
820 |
|
822 |
821 |
/// Comparison operator
|
823 |
822 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
824 |
823 |
/// Comparison operator
|
825 |
824 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
826 |
825 |
/// Comparison operator
|
827 |
826 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
828 |
827 |
|
829 |
828 |
private:
|
830 |
829 |
const StaticPath *path;
|
831 |
830 |
int idx;
|
832 |
831 |
};
|
833 |
832 |
|
834 |
833 |
/// \brief The nth arc.
|
835 |
834 |
///
|
836 |
835 |
/// \pre n is in the [0..length() - 1] range
|
837 |
836 |
const Arc& nth(int n) const {
|
838 |
837 |
return arcs[n];
|
839 |
838 |
}
|
840 |
839 |
|
841 |
840 |
/// \brief The arc iterator pointing to the nth arc.
|
842 |
841 |
ArcIt nthIt(int n) const {
|
843 |
842 |
return ArcIt(*this, n);
|
844 |
843 |
}
|
845 |
844 |
|
846 |
845 |
/// \brief The length of the path.
|
847 |
846 |
int length() const { return len; }
|
848 |
847 |
|
849 |
848 |
/// \brief Return true when the path is empty.
|
850 |
849 |
int empty() const { return len == 0; }
|
851 |
850 |
|
852 |
851 |
/// \break Erase all arcs in the digraph.
|
853 |
852 |
void clear() {
|
854 |
853 |
len = 0;
|
855 |
854 |
if (arcs) delete[] arcs;
|
856 |
855 |
arcs = 0;
|
857 |
856 |
}
|
858 |
857 |
|
859 |
858 |
/// \brief The first arc of the path.
|
860 |
859 |
const Arc& front() const {
|
861 |
860 |
return arcs[0];
|
862 |
861 |
}
|
863 |
862 |
|
864 |
863 |
/// \brief The last arc of the path.
|
865 |
864 |
const Arc& back() const {
|
866 |
865 |
return arcs[len - 1];
|
867 |
866 |
}
|
868 |
867 |
|
869 |
868 |
|
870 |
869 |
typedef True BuildTag;
|
871 |
870 |
|
872 |
871 |
template <typename CPath>
|
873 |
872 |
void build(const CPath& path) {
|
874 |
873 |
len = path.length();
|
875 |
874 |
arcs = new Arc[len];
|
876 |
875 |
int index = 0;
|
877 |
876 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
878 |
877 |
arcs[index] = it;
|
879 |
878 |
++index;
|
880 |
879 |
}
|
881 |
880 |
}
|
882 |
881 |
|
883 |
882 |
template <typename CPath>
|
884 |
883 |
void buildRev(const CPath& path) {
|
885 |
884 |
len = path.length();
|
886 |
885 |
arcs = new Arc[len];
|
887 |
886 |
int index = len;
|
888 |
887 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
889 |
888 |
--index;
|
890 |
889 |
arcs[index] = it;
|
891 |
890 |
}
|
892 |
891 |
}
|
893 |
892 |
|
894 |
893 |
private:
|
895 |
894 |
int len;
|
896 |
895 |
Arc* arcs;
|
897 |
896 |
};
|
898 |
897 |
|
|
898 |
///////////////////////////////////////////////////////////////////////
|
|
899 |
// Additional utilities
|
|
900 |
///////////////////////////////////////////////////////////////////////
|
|
901 |
|
|
902 |
namespace _path_bits {
|
|
903 |
|
|
904 |
template <typename Path, typename Enable = void>
|
|
905 |
struct RevTagIndicator {
|
|
906 |
static const bool value = false;
|
|
907 |
};
|
|
908 |
|
|
909 |
template <typename Digraph>
|
|
910 |
struct RevTagIndicator<
|
|
911 |
Digraph,
|
|
912 |
typename enable_if<typename Digraph::RevTag, void>::type
|
|
913 |
> {
|
|
914 |
static const bool value = true;
|
|
915 |
};
|
|
916 |
|
|
917 |
template <typename Target, typename Source,
|
|
918 |
typename BuildEnable = void, typename RevEnable = void>
|
|
919 |
struct PathCopySelector {
|
|
920 |
static void copy(Target& target, const Source& source) {
|
|
921 |
target.clear();
|
|
922 |
for (typename Source::ArcIt it(source); it != INVALID; ++it) {
|
|
923 |
target.addBack(it);
|
|
924 |
}
|
|
925 |
}
|
|
926 |
};
|
|
927 |
|
|
928 |
template <typename Target, typename Source, typename BuildEnable>
|
|
929 |
struct PathCopySelector<
|
|
930 |
Target, Source, BuildEnable,
|
|
931 |
typename enable_if<typename Source::RevPathTag, void>::type> {
|
|
932 |
static void copy(Target& target, const Source& source) {
|
|
933 |
target.clear();
|
|
934 |
for (typename Source::RevArcIt it(source); it != INVALID; ++it) {
|
|
935 |
target.addFront(it);
|
|
936 |
}
|
|
937 |
}
|
|
938 |
};
|
|
939 |
|
|
940 |
template <typename Target, typename Source, typename RevEnable>
|
|
941 |
struct PathCopySelector<
|
|
942 |
Target, Source,
|
|
943 |
typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
|
|
944 |
static void copy(Target& target, const Source& source) {
|
|
945 |
target.clear();
|
|
946 |
target.build(source);
|
|
947 |
}
|
|
948 |
};
|
|
949 |
|
|
950 |
template <typename Target, typename Source>
|
|
951 |
struct PathCopySelector<
|
|
952 |
Target, Source,
|
|
953 |
typename enable_if<typename Target::BuildTag, void>::type,
|
|
954 |
typename enable_if<typename Source::RevPathTag, void>::type> {
|
|
955 |
static void copy(Target& target, const Source& source) {
|
|
956 |
target.clear();
|
|
957 |
target.buildRev(source);
|
|
958 |
}
|
|
959 |
};
|
|
960 |
|
|
961 |
}
|
|
962 |
|
|
963 |
|
|
964 |
/// \brief Make a copy of a path.
|
|
965 |
///
|
|
966 |
/// This function makes a copy of a path.
|
|
967 |
template <typename Target, typename Source>
|
|
968 |
void copyPath(Target& target, const Source& source) {
|
|
969 |
checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>();
|
|
970 |
_path_bits::PathCopySelector<Target, Source>::copy(target, source);
|
|
971 |
}
|
|
972 |
|
|
973 |
/// \brief Check the consistency of a path.
|
|
974 |
///
|
|
975 |
/// This function checks that the target of each arc is the same
|
|
976 |
/// as the source of the next one.
|
|
977 |
///
|
|
978 |
template <typename Digraph, typename Path>
|
|
979 |
bool checkPath(const Digraph& digraph, const Path& path) {
|
|
980 |
typename Path::ArcIt it(path);
|
|
981 |
if (it == INVALID) return true;
|
|
982 |
typename Digraph::Node node = digraph.target(it);
|
|
983 |
++it;
|
|
984 |
while (it != INVALID) {
|
|
985 |
if (digraph.source(it) != node) return false;
|
|
986 |
node = digraph.target(it);
|
|
987 |
++it;
|
|
988 |
}
|
|
989 |
return true;
|
|
990 |
}
|
|
991 |
|
|
992 |
/// \brief The source of a path
|
|
993 |
///
|
|
994 |
/// This function returns the source of the given path.
|
|
995 |
template <typename Digraph, typename Path>
|
|
996 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
|
|
997 |
return digraph.source(path.front());
|
|
998 |
}
|
|
999 |
|
|
1000 |
/// \brief The target of a path
|
|
1001 |
///
|
|
1002 |
/// This function returns the target of the given path.
|
|
1003 |
template <typename Digraph, typename Path>
|
|
1004 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
|
|
1005 |
return digraph.target(path.back());
|
|
1006 |
}
|
|
1007 |
|
|
1008 |
/// \brief Class which helps to iterate through the nodes of a path
|
|
1009 |
///
|
|
1010 |
/// In a sense, the path can be treated as a list of arcs. The
|
|
1011 |
/// lemon path type stores only this list. As a consequence, it
|
|
1012 |
/// cannot enumerate the nodes in the path and the zero length paths
|
|
1013 |
/// cannot have a source node.
|
|
1014 |
///
|
|
1015 |
/// This class implements the node iterator of a path structure. To
|
|
1016 |
/// provide this feature, the underlying digraph should be passed to
|
|
1017 |
/// the constructor of the iterator.
|
|
1018 |
template <typename Path>
|
|
1019 |
class PathNodeIt {
|
|
1020 |
private:
|
|
1021 |
const typename Path::Digraph *_digraph;
|
|
1022 |
typename Path::ArcIt _it;
|
|
1023 |
typename Path::Digraph::Node _nd;
|
|
1024 |
|
|
1025 |
public:
|
|
1026 |
|
|
1027 |
typedef typename Path::Digraph Digraph;
|
|
1028 |
typedef typename Digraph::Node Node;
|
|
1029 |
|
|
1030 |
/// Default constructor
|
|
1031 |
PathNodeIt() {}
|
|
1032 |
/// Invalid constructor
|
|
1033 |
PathNodeIt(Invalid)
|
|
1034 |
: _digraph(0), _it(INVALID), _nd(INVALID) {}
|
|
1035 |
/// Constructor
|
|
1036 |
PathNodeIt(const Digraph& digraph, const Path& path)
|
|
1037 |
: _digraph(&digraph), _it(path) {
|
|
1038 |
_nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
|
|
1039 |
}
|
|
1040 |
/// Constructor
|
|
1041 |
PathNodeIt(const Digraph& digraph, const Path& path, const Node& src)
|
|
1042 |
: _digraph(&digraph), _it(path), _nd(src) {}
|
|
1043 |
|
|
1044 |
///Conversion to Digraph::Node
|
|
1045 |
operator Node() const {
|
|
1046 |
return _nd;
|
|
1047 |
}
|
|
1048 |
|
|
1049 |
/// Next node
|
|
1050 |
PathNodeIt& operator++() {
|
|
1051 |
if (_it == INVALID) _nd = INVALID;
|
|
1052 |
else {
|
|
1053 |
_nd = _digraph->target(_it);
|
|
1054 |
++_it;
|
|
1055 |
}
|
|
1056 |
return *this;
|
|
1057 |
}
|
|
1058 |
|
|
1059 |
/// Comparison operator
|
|
1060 |
bool operator==(const PathNodeIt& n) const {
|
|
1061 |
return _it == n._it && _nd == n._nd;
|
|
1062 |
}
|
|
1063 |
/// Comparison operator
|
|
1064 |
bool operator!=(const PathNodeIt& n) const {
|
|
1065 |
return _it != n._it || _nd != n._nd;
|
|
1066 |
}
|
|
1067 |
/// Comparison operator
|
|
1068 |
bool operator<(const PathNodeIt& n) const {
|
|
1069 |
return (_it < n._it && _nd != INVALID);
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
};
|
|
1073 |
|
899 |
1074 |
///@}
|
900 |
1075 |
|
901 |
1076 |
} // namespace lemon
|
902 |
1077 |
|
903 |
1078 |
#endif // LEMON_PATH_H
|