alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@96
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@96
|
4 |
*
|
alpar@1092
|
5 |
* Copyright (C) 2003-2013
|
alpar@96
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@96
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@96
|
8 |
*
|
alpar@96
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@96
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@96
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@96
|
12 |
*
|
alpar@96
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@96
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@96
|
15 |
* purpose.
|
alpar@96
|
16 |
*
|
alpar@96
|
17 |
*/
|
alpar@96
|
18 |
|
alpar@96
|
19 |
///\ingroup paths
|
alpar@96
|
20 |
///\file
|
alpar@96
|
21 |
///\brief Classes for representing paths in digraphs.
|
alpar@96
|
22 |
///
|
alpar@96
|
23 |
|
alpar@96
|
24 |
#ifndef LEMON_PATH_H
|
alpar@96
|
25 |
#define LEMON_PATH_H
|
alpar@96
|
26 |
|
alpar@96
|
27 |
#include <vector>
|
alpar@96
|
28 |
#include <algorithm>
|
alpar@96
|
29 |
|
alpar@96
|
30 |
#include <lemon/error.h>
|
deba@220
|
31 |
#include <lemon/core.h>
|
alpar@100
|
32 |
#include <lemon/concepts/path.h>
|
ggab90@1130
|
33 |
#include <lemon/bits/stl_iterators.h>
|
alpar@96
|
34 |
|
alpar@96
|
35 |
namespace lemon {
|
alpar@96
|
36 |
|
alpar@96
|
37 |
/// \addtogroup paths
|
alpar@96
|
38 |
/// @{
|
alpar@96
|
39 |
|
alpar@96
|
40 |
|
alpar@96
|
41 |
/// \brief A structure for representing directed paths in a digraph.
|
alpar@96
|
42 |
///
|
alpar@96
|
43 |
/// A structure for representing directed path in a digraph.
|
kpeter@559
|
44 |
/// \tparam GR The digraph type in which the path is.
|
alpar@96
|
45 |
///
|
kpeter@1201
|
46 |
/// In a sense, a path can be treated as a list of arcs. The
|
kpeter@1201
|
47 |
/// LEMON path type simply stores this list. As a consequence, it
|
kpeter@1201
|
48 |
/// cannot enumerate the nodes in the path, and the source node of
|
kpeter@1201
|
49 |
/// a zero-length path is undefined.
|
alpar@96
|
50 |
///
|
alpar@96
|
51 |
/// This implementation is a back and front insertable and erasable
|
alpar@96
|
52 |
/// path type. It can be indexed in O(1) time. The front and back
|
alpar@97
|
53 |
/// insertion and erase is done in O(1) (amortized) time. The
|
alpar@97
|
54 |
/// implementation uses two vectors for storing the front and back
|
alpar@97
|
55 |
/// insertions.
|
kpeter@559
|
56 |
template <typename GR>
|
alpar@96
|
57 |
class Path {
|
alpar@96
|
58 |
public:
|
alpar@96
|
59 |
|
kpeter@559
|
60 |
typedef GR Digraph;
|
alpar@96
|
61 |
typedef typename Digraph::Arc Arc;
|
alpar@96
|
62 |
|
alpar@96
|
63 |
/// \brief Default constructor
|
alpar@96
|
64 |
///
|
alpar@96
|
65 |
/// Default constructor
|
alpar@96
|
66 |
Path() {}
|
alpar@96
|
67 |
|
alpar@990
|
68 |
/// \brief Copy constructor
|
alpar@990
|
69 |
///
|
alpar@990
|
70 |
Path(const Path& cpath) {
|
alpar@990
|
71 |
pathCopy(cpath, *this);
|
alpar@990
|
72 |
}
|
alpar@990
|
73 |
|
alpar@96
|
74 |
/// \brief Template copy constructor
|
alpar@96
|
75 |
///
|
alpar@97
|
76 |
/// This constuctor initializes the path from any other path type.
|
alpar@97
|
77 |
/// It simply makes a copy of the given path.
|
alpar@96
|
78 |
template <typename CPath>
|
alpar@96
|
79 |
Path(const CPath& cpath) {
|
kpeter@516
|
80 |
pathCopy(cpath, *this);
|
alpar@96
|
81 |
}
|
alpar@96
|
82 |
|
alpar@990
|
83 |
/// \brief Copy assignment
|
alpar@990
|
84 |
///
|
alpar@990
|
85 |
Path& operator=(const Path& cpath) {
|
alpar@990
|
86 |
pathCopy(cpath, *this);
|
alpar@990
|
87 |
return *this;
|
alpar@990
|
88 |
}
|
alpar@990
|
89 |
|
alpar@96
|
90 |
/// \brief Template copy assignment
|
alpar@96
|
91 |
///
|
alpar@97
|
92 |
/// This operator makes a copy of a path of any other type.
|
alpar@96
|
93 |
template <typename CPath>
|
alpar@96
|
94 |
Path& operator=(const CPath& cpath) {
|
kpeter@516
|
95 |
pathCopy(cpath, *this);
|
alpar@96
|
96 |
return *this;
|
alpar@96
|
97 |
}
|
alpar@96
|
98 |
|
ladanyi@236
|
99 |
/// \brief LEMON style iterator for path arcs
|
alpar@96
|
100 |
///
|
alpar@96
|
101 |
/// This class is used to iterate on the arcs of the paths.
|
alpar@96
|
102 |
class ArcIt {
|
alpar@96
|
103 |
friend class Path;
|
alpar@96
|
104 |
public:
|
alpar@96
|
105 |
/// \brief Default constructor
|
alpar@96
|
106 |
ArcIt() {}
|
alpar@96
|
107 |
/// \brief Invalid constructor
|
alpar@96
|
108 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
alpar@97
|
109 |
/// \brief Initializate the iterator to the first arc of path
|
alpar@209
|
110 |
ArcIt(const Path &_path)
|
alpar@96
|
111 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
alpar@96
|
112 |
|
alpar@96
|
113 |
private:
|
alpar@96
|
114 |
|
alpar@209
|
115 |
ArcIt(const Path &_path, int _idx)
|
alpar@96
|
116 |
: path(&_path), idx(_idx) {}
|
alpar@96
|
117 |
|
alpar@96
|
118 |
public:
|
alpar@96
|
119 |
|
alpar@96
|
120 |
/// \brief Conversion to Arc
|
alpar@96
|
121 |
operator const Arc&() const {
|
alpar@96
|
122 |
return path->nth(idx);
|
alpar@96
|
123 |
}
|
alpar@96
|
124 |
|
alpar@96
|
125 |
/// \brief Next arc
|
alpar@209
|
126 |
ArcIt& operator++() {
|
alpar@96
|
127 |
++idx;
|
alpar@209
|
128 |
if (idx >= path->length()) idx = -1;
|
alpar@209
|
129 |
return *this;
|
alpar@96
|
130 |
}
|
alpar@96
|
131 |
|
alpar@96
|
132 |
/// \brief Comparison operator
|
alpar@96
|
133 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
alpar@96
|
134 |
/// \brief Comparison operator
|
alpar@96
|
135 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
alpar@96
|
136 |
/// \brief Comparison operator
|
alpar@96
|
137 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
alpar@96
|
138 |
|
alpar@96
|
139 |
private:
|
alpar@96
|
140 |
const Path *path;
|
alpar@96
|
141 |
int idx;
|
alpar@96
|
142 |
};
|
alpar@96
|
143 |
|
ggab90@1130
|
144 |
/// \brief Gets the collection of the arcs of the path.
|
ggab90@1130
|
145 |
///
|
ggab90@1130
|
146 |
/// This function can be used for iterating on the
|
ggab90@1130
|
147 |
/// arcs of the path. It returns a wrapped
|
ggab90@1130
|
148 |
/// ArcIt, which looks like an STL container
|
ggab90@1130
|
149 |
/// (by having begin() and end()) which you can use in range-based
|
ggab90@1130
|
150 |
/// for loops, STL algorithms, etc.
|
ggab90@1130
|
151 |
/// For example you can write:
|
ggab90@1130
|
152 |
///\code
|
ggab90@1130
|
153 |
/// for(auto a: p.arcs())
|
ggab90@1130
|
154 |
/// doSomething(a);
|
ggab90@1130
|
155 |
///\endcode
|
ggab90@1130
|
156 |
LemonRangeWrapper1<ArcIt, Path> arcs() const {
|
ggab90@1130
|
157 |
return LemonRangeWrapper1<ArcIt, Path>(*this);
|
ggab90@1130
|
158 |
}
|
ggab90@1130
|
159 |
|
ggab90@1130
|
160 |
|
alpar@96
|
161 |
/// \brief Length of the path.
|
alpar@96
|
162 |
int length() const { return head.size() + tail.size(); }
|
alpar@97
|
163 |
/// \brief Return whether the path is empty.
|
alpar@96
|
164 |
bool empty() const { return head.empty() && tail.empty(); }
|
alpar@96
|
165 |
|
alpar@97
|
166 |
/// \brief Reset the path to an empty one.
|
alpar@96
|
167 |
void clear() { head.clear(); tail.clear(); }
|
alpar@96
|
168 |
|
kpeter@920
|
169 |
/// \brief The n-th arc.
|
alpar@96
|
170 |
///
|
kpeter@1201
|
171 |
/// Gives back the n-th arc. This function runs in O(1) time.
|
kpeter@1201
|
172 |
/// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
|
alpar@96
|
173 |
const Arc& nth(int n) const {
|
alpar@96
|
174 |
return n < int(head.size()) ? *(head.rbegin() + n) :
|
alpar@96
|
175 |
*(tail.begin() + (n - head.size()));
|
alpar@96
|
176 |
}
|
alpar@96
|
177 |
|
kpeter@920
|
178 |
/// \brief Initialize arc iterator to point to the n-th arc
|
alpar@96
|
179 |
///
|
kpeter@559
|
180 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
|
alpar@96
|
181 |
ArcIt nthIt(int n) const {
|
alpar@96
|
182 |
return ArcIt(*this, n);
|
alpar@96
|
183 |
}
|
alpar@96
|
184 |
|
alpar@97
|
185 |
/// \brief The first arc of the path
|
alpar@96
|
186 |
const Arc& front() const {
|
alpar@96
|
187 |
return head.empty() ? tail.front() : head.back();
|
alpar@96
|
188 |
}
|
alpar@96
|
189 |
|
alpar@96
|
190 |
/// \brief Add a new arc before the current path
|
alpar@96
|
191 |
void addFront(const Arc& arc) {
|
alpar@96
|
192 |
head.push_back(arc);
|
alpar@96
|
193 |
}
|
alpar@96
|
194 |
|
alpar@96
|
195 |
/// \brief Erase the first arc of the path
|
alpar@96
|
196 |
void eraseFront() {
|
alpar@96
|
197 |
if (!head.empty()) {
|
alpar@96
|
198 |
head.pop_back();
|
alpar@96
|
199 |
} else {
|
alpar@96
|
200 |
head.clear();
|
alpar@96
|
201 |
int halfsize = tail.size() / 2;
|
alpar@96
|
202 |
head.resize(halfsize);
|
alpar@96
|
203 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
|
alpar@96
|
204 |
head.rbegin());
|
alpar@96
|
205 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
|
alpar@96
|
206 |
tail.resize(tail.size() - halfsize - 1);
|
alpar@96
|
207 |
}
|
alpar@96
|
208 |
}
|
alpar@96
|
209 |
|
alpar@97
|
210 |
/// \brief The last arc of the path
|
alpar@96
|
211 |
const Arc& back() const {
|
alpar@96
|
212 |
return tail.empty() ? head.front() : tail.back();
|
alpar@96
|
213 |
}
|
alpar@96
|
214 |
|
alpar@96
|
215 |
/// \brief Add a new arc behind the current path
|
alpar@96
|
216 |
void addBack(const Arc& arc) {
|
alpar@96
|
217 |
tail.push_back(arc);
|
alpar@96
|
218 |
}
|
alpar@96
|
219 |
|
alpar@96
|
220 |
/// \brief Erase the last arc of the path
|
alpar@96
|
221 |
void eraseBack() {
|
alpar@96
|
222 |
if (!tail.empty()) {
|
alpar@96
|
223 |
tail.pop_back();
|
alpar@96
|
224 |
} else {
|
alpar@96
|
225 |
int halfsize = head.size() / 2;
|
alpar@96
|
226 |
tail.resize(halfsize);
|
alpar@96
|
227 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1,
|
alpar@96
|
228 |
tail.rbegin());
|
alpar@96
|
229 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
|
alpar@96
|
230 |
head.resize(head.size() - halfsize - 1);
|
alpar@96
|
231 |
}
|
alpar@96
|
232 |
}
|
alpar@96
|
233 |
|
alpar@96
|
234 |
typedef True BuildTag;
|
alpar@96
|
235 |
|
alpar@96
|
236 |
template <typename CPath>
|
alpar@96
|
237 |
void build(const CPath& path) {
|
alpar@96
|
238 |
int len = path.length();
|
alpar@96
|
239 |
tail.reserve(len);
|
alpar@96
|
240 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
241 |
tail.push_back(it);
|
alpar@96
|
242 |
}
|
alpar@96
|
243 |
}
|
alpar@96
|
244 |
|
alpar@96
|
245 |
template <typename CPath>
|
alpar@96
|
246 |
void buildRev(const CPath& path) {
|
alpar@96
|
247 |
int len = path.length();
|
alpar@96
|
248 |
head.reserve(len);
|
alpar@96
|
249 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
250 |
head.push_back(it);
|
alpar@96
|
251 |
}
|
alpar@96
|
252 |
}
|
alpar@96
|
253 |
|
alpar@96
|
254 |
protected:
|
alpar@96
|
255 |
typedef std::vector<Arc> Container;
|
alpar@96
|
256 |
Container head, tail;
|
alpar@96
|
257 |
|
alpar@96
|
258 |
};
|
alpar@96
|
259 |
|
alpar@96
|
260 |
/// \brief A structure for representing directed paths in a digraph.
|
alpar@96
|
261 |
///
|
alpar@96
|
262 |
/// A structure for representing directed path in a digraph.
|
kpeter@559
|
263 |
/// \tparam GR The digraph type in which the path is.
|
alpar@96
|
264 |
///
|
kpeter@1201
|
265 |
/// In a sense, a path can be treated as a list of arcs. The
|
kpeter@1201
|
266 |
/// LEMON path type simply stores this list. As a consequence, it
|
kpeter@1201
|
267 |
/// cannot enumerate the nodes in the path, and the source node of
|
kpeter@1201
|
268 |
/// a zero-length path is undefined.
|
alpar@96
|
269 |
///
|
alpar@96
|
270 |
/// This implementation is a just back insertable and erasable path
|
alpar@96
|
271 |
/// type. It can be indexed in O(1) time. The back insertion and
|
alpar@96
|
272 |
/// erasure is amortized O(1) time. This implementation is faster
|
kpeter@1201
|
273 |
/// than the \c Path type because it use just one vector for the
|
alpar@96
|
274 |
/// arcs.
|
kpeter@559
|
275 |
template <typename GR>
|
alpar@96
|
276 |
class SimplePath {
|
alpar@96
|
277 |
public:
|
alpar@96
|
278 |
|
kpeter@559
|
279 |
typedef GR Digraph;
|
alpar@96
|
280 |
typedef typename Digraph::Arc Arc;
|
alpar@96
|
281 |
|
alpar@96
|
282 |
/// \brief Default constructor
|
alpar@96
|
283 |
///
|
alpar@96
|
284 |
/// Default constructor
|
alpar@96
|
285 |
SimplePath() {}
|
alpar@96
|
286 |
|
alpar@990
|
287 |
/// \brief Copy constructor
|
alpar@990
|
288 |
///
|
alpar@990
|
289 |
SimplePath(const SimplePath& cpath) {
|
alpar@990
|
290 |
pathCopy(cpath, *this);
|
alpar@990
|
291 |
}
|
alpar@990
|
292 |
|
alpar@96
|
293 |
/// \brief Template copy constructor
|
alpar@96
|
294 |
///
|
alpar@96
|
295 |
/// This path can be initialized with any other path type. It just
|
alpar@96
|
296 |
/// makes a copy of the given path.
|
alpar@96
|
297 |
template <typename CPath>
|
alpar@96
|
298 |
SimplePath(const CPath& cpath) {
|
kpeter@516
|
299 |
pathCopy(cpath, *this);
|
alpar@96
|
300 |
}
|
alpar@96
|
301 |
|
alpar@990
|
302 |
/// \brief Copy assignment
|
alpar@990
|
303 |
///
|
alpar@990
|
304 |
SimplePath& operator=(const SimplePath& cpath) {
|
alpar@990
|
305 |
pathCopy(cpath, *this);
|
alpar@990
|
306 |
return *this;
|
alpar@990
|
307 |
}
|
alpar@990
|
308 |
|
alpar@96
|
309 |
/// \brief Template copy assignment
|
alpar@96
|
310 |
///
|
alpar@96
|
311 |
/// This path can be initialized with any other path type. It just
|
alpar@96
|
312 |
/// makes a copy of the given path.
|
alpar@96
|
313 |
template <typename CPath>
|
alpar@96
|
314 |
SimplePath& operator=(const CPath& cpath) {
|
kpeter@516
|
315 |
pathCopy(cpath, *this);
|
alpar@96
|
316 |
return *this;
|
alpar@96
|
317 |
}
|
alpar@96
|
318 |
|
alpar@96
|
319 |
/// \brief Iterator class to iterate on the arcs of the paths
|
alpar@96
|
320 |
///
|
alpar@96
|
321 |
/// This class is used to iterate on the arcs of the paths
|
alpar@96
|
322 |
///
|
alpar@96
|
323 |
/// Of course it converts to Digraph::Arc
|
alpar@96
|
324 |
class ArcIt {
|
alpar@96
|
325 |
friend class SimplePath;
|
alpar@96
|
326 |
public:
|
alpar@96
|
327 |
/// Default constructor
|
alpar@96
|
328 |
ArcIt() {}
|
alpar@96
|
329 |
/// Invalid constructor
|
alpar@96
|
330 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
alpar@96
|
331 |
/// \brief Initializate the constructor to the first arc of path
|
alpar@209
|
332 |
ArcIt(const SimplePath &_path)
|
alpar@96
|
333 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
alpar@96
|
334 |
|
alpar@96
|
335 |
private:
|
alpar@96
|
336 |
|
alpar@96
|
337 |
/// Constructor with starting point
|
alpar@209
|
338 |
ArcIt(const SimplePath &_path, int _idx)
|
kpeter@1044
|
339 |
: path(&_path), idx(_idx) {}
|
alpar@96
|
340 |
|
alpar@96
|
341 |
public:
|
alpar@96
|
342 |
|
alpar@96
|
343 |
///Conversion to Digraph::Arc
|
alpar@96
|
344 |
operator const Arc&() const {
|
alpar@96
|
345 |
return path->nth(idx);
|
alpar@96
|
346 |
}
|
alpar@96
|
347 |
|
alpar@96
|
348 |
/// Next arc
|
alpar@209
|
349 |
ArcIt& operator++() {
|
alpar@96
|
350 |
++idx;
|
alpar@209
|
351 |
if (idx >= path->length()) idx = -1;
|
alpar@209
|
352 |
return *this;
|
alpar@96
|
353 |
}
|
alpar@96
|
354 |
|
alpar@96
|
355 |
/// Comparison operator
|
alpar@96
|
356 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
alpar@96
|
357 |
/// Comparison operator
|
alpar@96
|
358 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
alpar@96
|
359 |
/// Comparison operator
|
alpar@96
|
360 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
alpar@96
|
361 |
|
alpar@96
|
362 |
private:
|
alpar@96
|
363 |
const SimplePath *path;
|
alpar@96
|
364 |
int idx;
|
alpar@96
|
365 |
};
|
alpar@96
|
366 |
|
ggab90@1130
|
367 |
/// \brief Gets the collection of the arcs of the path.
|
ggab90@1130
|
368 |
///
|
ggab90@1130
|
369 |
/// This function can be used for iterating on the
|
ggab90@1130
|
370 |
/// arcs of the path. It returns a wrapped
|
ggab90@1130
|
371 |
/// ArcIt, which looks like an STL container
|
ggab90@1130
|
372 |
/// (by having begin() and end()) which you can use in range-based
|
ggab90@1130
|
373 |
/// for loops, STL algorithms, etc.
|
ggab90@1130
|
374 |
/// For example you can write:
|
ggab90@1130
|
375 |
///\code
|
ggab90@1130
|
376 |
/// for(auto a: p.arcs())
|
ggab90@1130
|
377 |
/// doSomething(a);
|
ggab90@1130
|
378 |
///\endcode
|
ggab90@1130
|
379 |
LemonRangeWrapper1<ArcIt, SimplePath> arcs() const {
|
ggab90@1130
|
380 |
return LemonRangeWrapper1<ArcIt, SimplePath>(*this);
|
ggab90@1130
|
381 |
}
|
ggab90@1130
|
382 |
|
ggab90@1130
|
383 |
|
alpar@96
|
384 |
/// \brief Length of the path.
|
alpar@96
|
385 |
int length() const { return data.size(); }
|
alpar@97
|
386 |
/// \brief Return true if the path is empty.
|
alpar@96
|
387 |
bool empty() const { return data.empty(); }
|
alpar@96
|
388 |
|
alpar@97
|
389 |
/// \brief Reset the path to an empty one.
|
alpar@96
|
390 |
void clear() { data.clear(); }
|
alpar@96
|
391 |
|
kpeter@920
|
392 |
/// \brief The n-th arc.
|
alpar@96
|
393 |
///
|
kpeter@1201
|
394 |
/// Gives back the n-th arc. This function runs in O(1) time.
|
kpeter@1201
|
395 |
/// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
|
alpar@96
|
396 |
const Arc& nth(int n) const {
|
alpar@96
|
397 |
return data[n];
|
alpar@96
|
398 |
}
|
alpar@96
|
399 |
|
kpeter@920
|
400 |
/// \brief Initializes arc iterator to point to the n-th arc.
|
alpar@96
|
401 |
ArcIt nthIt(int n) const {
|
alpar@96
|
402 |
return ArcIt(*this, n);
|
alpar@96
|
403 |
}
|
alpar@96
|
404 |
|
alpar@97
|
405 |
/// \brief The first arc of the path.
|
alpar@96
|
406 |
const Arc& front() const {
|
alpar@96
|
407 |
return data.front();
|
alpar@96
|
408 |
}
|
alpar@96
|
409 |
|
alpar@97
|
410 |
/// \brief The last arc of the path.
|
alpar@96
|
411 |
const Arc& back() const {
|
alpar@96
|
412 |
return data.back();
|
alpar@96
|
413 |
}
|
alpar@96
|
414 |
|
alpar@96
|
415 |
/// \brief Add a new arc behind the current path.
|
alpar@96
|
416 |
void addBack(const Arc& arc) {
|
alpar@96
|
417 |
data.push_back(arc);
|
alpar@96
|
418 |
}
|
alpar@96
|
419 |
|
alpar@96
|
420 |
/// \brief Erase the last arc of the path
|
alpar@96
|
421 |
void eraseBack() {
|
alpar@96
|
422 |
data.pop_back();
|
alpar@96
|
423 |
}
|
alpar@96
|
424 |
|
alpar@96
|
425 |
typedef True BuildTag;
|
alpar@96
|
426 |
|
alpar@96
|
427 |
template <typename CPath>
|
alpar@96
|
428 |
void build(const CPath& path) {
|
alpar@96
|
429 |
int len = path.length();
|
alpar@96
|
430 |
data.resize(len);
|
alpar@96
|
431 |
int index = 0;
|
alpar@96
|
432 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
433 |
data[index] = it;;
|
alpar@96
|
434 |
++index;
|
alpar@96
|
435 |
}
|
alpar@96
|
436 |
}
|
alpar@96
|
437 |
|
alpar@96
|
438 |
template <typename CPath>
|
alpar@96
|
439 |
void buildRev(const CPath& path) {
|
alpar@96
|
440 |
int len = path.length();
|
alpar@96
|
441 |
data.resize(len);
|
alpar@96
|
442 |
int index = len;
|
alpar@96
|
443 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
444 |
--index;
|
alpar@96
|
445 |
data[index] = it;;
|
alpar@96
|
446 |
}
|
alpar@96
|
447 |
}
|
alpar@96
|
448 |
|
alpar@96
|
449 |
protected:
|
alpar@96
|
450 |
typedef std::vector<Arc> Container;
|
alpar@96
|
451 |
Container data;
|
alpar@96
|
452 |
|
alpar@96
|
453 |
};
|
alpar@96
|
454 |
|
alpar@96
|
455 |
/// \brief A structure for representing directed paths in a digraph.
|
alpar@96
|
456 |
///
|
alpar@96
|
457 |
/// A structure for representing directed path in a digraph.
|
kpeter@559
|
458 |
/// \tparam GR The digraph type in which the path is.
|
alpar@96
|
459 |
///
|
kpeter@1201
|
460 |
/// In a sense, a path can be treated as a list of arcs. The
|
kpeter@1201
|
461 |
/// LEMON path type simply stores this list. As a consequence, it
|
kpeter@1201
|
462 |
/// cannot enumerate the nodes in the path, and the source node of
|
kpeter@1201
|
463 |
/// a zero-length path is undefined.
|
alpar@96
|
464 |
///
|
alpar@96
|
465 |
/// This implementation is a back and front insertable and erasable
|
alpar@96
|
466 |
/// path type. It can be indexed in O(k) time, where k is the rank
|
alpar@96
|
467 |
/// of the arc in the path. The length can be computed in O(n)
|
alpar@96
|
468 |
/// time. The front and back insertion and erasure is O(1) time
|
alpar@96
|
469 |
/// and it can be splited and spliced in O(1) time.
|
kpeter@559
|
470 |
template <typename GR>
|
alpar@96
|
471 |
class ListPath {
|
alpar@96
|
472 |
public:
|
alpar@96
|
473 |
|
kpeter@559
|
474 |
typedef GR Digraph;
|
alpar@96
|
475 |
typedef typename Digraph::Arc Arc;
|
alpar@96
|
476 |
|
alpar@96
|
477 |
protected:
|
alpar@96
|
478 |
|
alpar@209
|
479 |
// the std::list<> is incompatible
|
alpar@96
|
480 |
// hard to create invalid iterator
|
alpar@96
|
481 |
struct Node {
|
alpar@96
|
482 |
Arc arc;
|
alpar@96
|
483 |
Node *next, *prev;
|
alpar@96
|
484 |
};
|
alpar@96
|
485 |
|
alpar@96
|
486 |
Node *first, *last;
|
alpar@96
|
487 |
|
alpar@96
|
488 |
std::allocator<Node> alloc;
|
alpar@96
|
489 |
|
alpar@96
|
490 |
public:
|
alpar@209
|
491 |
|
alpar@96
|
492 |
/// \brief Default constructor
|
alpar@96
|
493 |
///
|
alpar@96
|
494 |
/// Default constructor
|
alpar@96
|
495 |
ListPath() : first(0), last(0) {}
|
alpar@96
|
496 |
|
alpar@990
|
497 |
/// \brief Copy constructor
|
alpar@990
|
498 |
///
|
alpar@990
|
499 |
ListPath(const ListPath& cpath) : first(0), last(0) {
|
alpar@990
|
500 |
pathCopy(cpath, *this);
|
alpar@990
|
501 |
}
|
alpar@990
|
502 |
|
alpar@96
|
503 |
/// \brief Template copy constructor
|
alpar@96
|
504 |
///
|
alpar@96
|
505 |
/// This path can be initialized with any other path type. It just
|
alpar@96
|
506 |
/// makes a copy of the given path.
|
alpar@96
|
507 |
template <typename CPath>
|
alpar@96
|
508 |
ListPath(const CPath& cpath) : first(0), last(0) {
|
kpeter@516
|
509 |
pathCopy(cpath, *this);
|
alpar@96
|
510 |
}
|
alpar@96
|
511 |
|
alpar@96
|
512 |
/// \brief Destructor of the path
|
alpar@96
|
513 |
///
|
alpar@96
|
514 |
/// Destructor of the path
|
alpar@96
|
515 |
~ListPath() {
|
alpar@96
|
516 |
clear();
|
alpar@96
|
517 |
}
|
alpar@96
|
518 |
|
alpar@990
|
519 |
/// \brief Copy assignment
|
alpar@990
|
520 |
///
|
alpar@990
|
521 |
ListPath& operator=(const ListPath& cpath) {
|
alpar@990
|
522 |
pathCopy(cpath, *this);
|
alpar@990
|
523 |
return *this;
|
alpar@990
|
524 |
}
|
alpar@990
|
525 |
|
alpar@96
|
526 |
/// \brief Template copy assignment
|
alpar@96
|
527 |
///
|
alpar@96
|
528 |
/// This path can be initialized with any other path type. It just
|
alpar@96
|
529 |
/// makes a copy of the given path.
|
alpar@96
|
530 |
template <typename CPath>
|
alpar@96
|
531 |
ListPath& operator=(const CPath& cpath) {
|
kpeter@516
|
532 |
pathCopy(cpath, *this);
|
alpar@96
|
533 |
return *this;
|
alpar@96
|
534 |
}
|
alpar@96
|
535 |
|
alpar@96
|
536 |
/// \brief Iterator class to iterate on the arcs of the paths
|
alpar@96
|
537 |
///
|
alpar@96
|
538 |
/// This class is used to iterate on the arcs of the paths
|
alpar@96
|
539 |
///
|
alpar@96
|
540 |
/// Of course it converts to Digraph::Arc
|
alpar@96
|
541 |
class ArcIt {
|
alpar@96
|
542 |
friend class ListPath;
|
alpar@96
|
543 |
public:
|
alpar@96
|
544 |
/// Default constructor
|
alpar@96
|
545 |
ArcIt() {}
|
alpar@96
|
546 |
/// Invalid constructor
|
alpar@96
|
547 |
ArcIt(Invalid) : path(0), node(0) {}
|
alpar@96
|
548 |
/// \brief Initializate the constructor to the first arc of path
|
alpar@209
|
549 |
ArcIt(const ListPath &_path)
|
alpar@96
|
550 |
: path(&_path), node(_path.first) {}
|
alpar@96
|
551 |
|
alpar@96
|
552 |
protected:
|
alpar@96
|
553 |
|
alpar@209
|
554 |
ArcIt(const ListPath &_path, Node *_node)
|
alpar@96
|
555 |
: path(&_path), node(_node) {}
|
alpar@96
|
556 |
|
alpar@96
|
557 |
|
alpar@96
|
558 |
public:
|
alpar@96
|
559 |
|
alpar@96
|
560 |
///Conversion to Digraph::Arc
|
alpar@96
|
561 |
operator const Arc&() const {
|
alpar@96
|
562 |
return node->arc;
|
alpar@96
|
563 |
}
|
alpar@96
|
564 |
|
alpar@96
|
565 |
/// Next arc
|
alpar@209
|
566 |
ArcIt& operator++() {
|
alpar@96
|
567 |
node = node->next;
|
alpar@209
|
568 |
return *this;
|
alpar@96
|
569 |
}
|
alpar@96
|
570 |
|
alpar@96
|
571 |
/// Comparison operator
|
alpar@96
|
572 |
bool operator==(const ArcIt& e) const { return node==e.node; }
|
alpar@96
|
573 |
/// Comparison operator
|
alpar@96
|
574 |
bool operator!=(const ArcIt& e) const { return node!=e.node; }
|
alpar@96
|
575 |
/// Comparison operator
|
alpar@96
|
576 |
bool operator<(const ArcIt& e) const { return node<e.node; }
|
alpar@96
|
577 |
|
alpar@96
|
578 |
private:
|
alpar@96
|
579 |
const ListPath *path;
|
alpar@96
|
580 |
Node *node;
|
alpar@96
|
581 |
};
|
alpar@96
|
582 |
|
ggab90@1130
|
583 |
/// \brief Gets the collection of the arcs of the path.
|
ggab90@1130
|
584 |
///
|
ggab90@1130
|
585 |
/// This function can be used for iterating on the
|
ggab90@1130
|
586 |
/// arcs of the path. It returns a wrapped
|
ggab90@1130
|
587 |
/// ArcIt, which looks like an STL container
|
ggab90@1130
|
588 |
/// (by having begin() and end()) which you can use in range-based
|
ggab90@1130
|
589 |
/// for loops, STL algorithms, etc.
|
ggab90@1130
|
590 |
/// For example you can write:
|
ggab90@1130
|
591 |
///\code
|
ggab90@1130
|
592 |
/// for(auto a: p.arcs())
|
ggab90@1130
|
593 |
/// doSomething(a);
|
ggab90@1130
|
594 |
///\endcode
|
ggab90@1130
|
595 |
LemonRangeWrapper1<ArcIt, ListPath> arcs() const {
|
ggab90@1130
|
596 |
return LemonRangeWrapper1<ArcIt, ListPath>(*this);
|
ggab90@1130
|
597 |
}
|
ggab90@1130
|
598 |
|
ggab90@1130
|
599 |
|
kpeter@920
|
600 |
/// \brief The n-th arc.
|
alpar@96
|
601 |
///
|
kpeter@920
|
602 |
/// This function looks for the n-th arc in O(n) time.
|
kpeter@1201
|
603 |
/// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
|
alpar@96
|
604 |
const Arc& nth(int n) const {
|
alpar@96
|
605 |
Node *node = first;
|
alpar@96
|
606 |
for (int i = 0; i < n; ++i) {
|
alpar@96
|
607 |
node = node->next;
|
alpar@96
|
608 |
}
|
alpar@96
|
609 |
return node->arc;
|
alpar@96
|
610 |
}
|
alpar@96
|
611 |
|
kpeter@920
|
612 |
/// \brief Initializes arc iterator to point to the n-th arc.
|
alpar@96
|
613 |
ArcIt nthIt(int n) const {
|
alpar@96
|
614 |
Node *node = first;
|
alpar@96
|
615 |
for (int i = 0; i < n; ++i) {
|
alpar@96
|
616 |
node = node->next;
|
alpar@96
|
617 |
}
|
alpar@96
|
618 |
return ArcIt(*this, node);
|
alpar@96
|
619 |
}
|
alpar@96
|
620 |
|
alpar@96
|
621 |
/// \brief Length of the path.
|
alpar@96
|
622 |
int length() const {
|
alpar@96
|
623 |
int len = 0;
|
alpar@96
|
624 |
Node *node = first;
|
alpar@96
|
625 |
while (node != 0) {
|
alpar@96
|
626 |
node = node->next;
|
alpar@96
|
627 |
++len;
|
alpar@96
|
628 |
}
|
alpar@96
|
629 |
return len;
|
alpar@96
|
630 |
}
|
alpar@96
|
631 |
|
alpar@97
|
632 |
/// \brief Return true if the path is empty.
|
alpar@96
|
633 |
bool empty() const { return first == 0; }
|
alpar@96
|
634 |
|
alpar@97
|
635 |
/// \brief Reset the path to an empty one.
|
alpar@96
|
636 |
void clear() {
|
alpar@96
|
637 |
while (first != 0) {
|
alpar@96
|
638 |
last = first->next;
|
alpar@96
|
639 |
alloc.destroy(first);
|
alpar@96
|
640 |
alloc.deallocate(first, 1);
|
alpar@96
|
641 |
first = last;
|
alpar@96
|
642 |
}
|
alpar@96
|
643 |
}
|
alpar@96
|
644 |
|
alpar@97
|
645 |
/// \brief The first arc of the path
|
alpar@96
|
646 |
const Arc& front() const {
|
alpar@96
|
647 |
return first->arc;
|
alpar@96
|
648 |
}
|
alpar@96
|
649 |
|
alpar@96
|
650 |
/// \brief Add a new arc before the current path
|
alpar@96
|
651 |
void addFront(const Arc& arc) {
|
alpar@96
|
652 |
Node *node = alloc.allocate(1);
|
alpar@96
|
653 |
alloc.construct(node, Node());
|
alpar@96
|
654 |
node->prev = 0;
|
alpar@96
|
655 |
node->next = first;
|
alpar@96
|
656 |
node->arc = arc;
|
alpar@96
|
657 |
if (first) {
|
alpar@96
|
658 |
first->prev = node;
|
alpar@96
|
659 |
first = node;
|
alpar@96
|
660 |
} else {
|
alpar@96
|
661 |
first = last = node;
|
alpar@96
|
662 |
}
|
alpar@96
|
663 |
}
|
alpar@96
|
664 |
|
alpar@96
|
665 |
/// \brief Erase the first arc of the path
|
alpar@96
|
666 |
void eraseFront() {
|
alpar@96
|
667 |
Node *node = first;
|
alpar@96
|
668 |
first = first->next;
|
alpar@96
|
669 |
if (first) {
|
alpar@96
|
670 |
first->prev = 0;
|
alpar@96
|
671 |
} else {
|
alpar@96
|
672 |
last = 0;
|
alpar@96
|
673 |
}
|
alpar@96
|
674 |
alloc.destroy(node);
|
alpar@96
|
675 |
alloc.deallocate(node, 1);
|
alpar@96
|
676 |
}
|
alpar@96
|
677 |
|
alpar@97
|
678 |
/// \brief The last arc of the path.
|
alpar@96
|
679 |
const Arc& back() const {
|
alpar@96
|
680 |
return last->arc;
|
alpar@96
|
681 |
}
|
alpar@96
|
682 |
|
alpar@96
|
683 |
/// \brief Add a new arc behind the current path.
|
alpar@96
|
684 |
void addBack(const Arc& arc) {
|
alpar@96
|
685 |
Node *node = alloc.allocate(1);
|
alpar@96
|
686 |
alloc.construct(node, Node());
|
alpar@96
|
687 |
node->next = 0;
|
alpar@96
|
688 |
node->prev = last;
|
alpar@96
|
689 |
node->arc = arc;
|
alpar@96
|
690 |
if (last) {
|
alpar@96
|
691 |
last->next = node;
|
alpar@96
|
692 |
last = node;
|
alpar@96
|
693 |
} else {
|
alpar@96
|
694 |
last = first = node;
|
alpar@96
|
695 |
}
|
alpar@96
|
696 |
}
|
alpar@96
|
697 |
|
alpar@96
|
698 |
/// \brief Erase the last arc of the path
|
alpar@96
|
699 |
void eraseBack() {
|
alpar@96
|
700 |
Node *node = last;
|
alpar@96
|
701 |
last = last->prev;
|
alpar@96
|
702 |
if (last) {
|
alpar@96
|
703 |
last->next = 0;
|
alpar@96
|
704 |
} else {
|
alpar@96
|
705 |
first = 0;
|
alpar@96
|
706 |
}
|
alpar@96
|
707 |
alloc.destroy(node);
|
alpar@96
|
708 |
alloc.deallocate(node, 1);
|
alpar@96
|
709 |
}
|
alpar@96
|
710 |
|
alpar@97
|
711 |
/// \brief Splice a path to the back of the current path.
|
alpar@96
|
712 |
///
|
alpar@97
|
713 |
/// It splices \c tpath to the back of the current path and \c
|
alpar@96
|
714 |
/// tpath becomes empty. The time complexity of this function is
|
alpar@96
|
715 |
/// O(1).
|
alpar@96
|
716 |
void spliceBack(ListPath& tpath) {
|
alpar@96
|
717 |
if (first) {
|
alpar@96
|
718 |
if (tpath.first) {
|
alpar@96
|
719 |
last->next = tpath.first;
|
alpar@96
|
720 |
tpath.first->prev = last;
|
alpar@96
|
721 |
last = tpath.last;
|
alpar@96
|
722 |
}
|
alpar@96
|
723 |
} else {
|
alpar@96
|
724 |
first = tpath.first;
|
alpar@96
|
725 |
last = tpath.last;
|
alpar@96
|
726 |
}
|
alpar@96
|
727 |
tpath.first = tpath.last = 0;
|
alpar@96
|
728 |
}
|
alpar@96
|
729 |
|
alpar@97
|
730 |
/// \brief Splice a path to the front of the current path.
|
alpar@96
|
731 |
///
|
alpar@97
|
732 |
/// It splices \c tpath before the current path and \c tpath
|
alpar@96
|
733 |
/// becomes empty. The time complexity of this function
|
alpar@96
|
734 |
/// is O(1).
|
alpar@96
|
735 |
void spliceFront(ListPath& tpath) {
|
alpar@96
|
736 |
if (first) {
|
alpar@96
|
737 |
if (tpath.first) {
|
alpar@96
|
738 |
first->prev = tpath.last;
|
alpar@96
|
739 |
tpath.last->next = first;
|
alpar@96
|
740 |
first = tpath.first;
|
alpar@96
|
741 |
}
|
alpar@96
|
742 |
} else {
|
alpar@96
|
743 |
first = tpath.first;
|
alpar@96
|
744 |
last = tpath.last;
|
alpar@96
|
745 |
}
|
alpar@96
|
746 |
tpath.first = tpath.last = 0;
|
alpar@96
|
747 |
}
|
alpar@96
|
748 |
|
alpar@97
|
749 |
/// \brief Splice a path into the current path.
|
alpar@96
|
750 |
///
|
alpar@96
|
751 |
/// It splices the \c tpath into the current path before the
|
alpar@96
|
752 |
/// position of \c it iterator and \c tpath becomes empty. The
|
alpar@97
|
753 |
/// time complexity of this function is O(1). If the \c it is
|
alpar@97
|
754 |
/// \c INVALID then it will splice behind the current path.
|
alpar@96
|
755 |
void splice(ArcIt it, ListPath& tpath) {
|
alpar@96
|
756 |
if (it.node) {
|
alpar@96
|
757 |
if (tpath.first) {
|
alpar@96
|
758 |
tpath.first->prev = it.node->prev;
|
alpar@96
|
759 |
if (it.node->prev) {
|
alpar@96
|
760 |
it.node->prev->next = tpath.first;
|
alpar@96
|
761 |
} else {
|
alpar@96
|
762 |
first = tpath.first;
|
alpar@96
|
763 |
}
|
alpar@96
|
764 |
it.node->prev = tpath.last;
|
alpar@96
|
765 |
tpath.last->next = it.node;
|
alpar@96
|
766 |
}
|
alpar@96
|
767 |
} else {
|
alpar@96
|
768 |
if (first) {
|
alpar@96
|
769 |
if (tpath.first) {
|
alpar@96
|
770 |
last->next = tpath.first;
|
alpar@96
|
771 |
tpath.first->prev = last;
|
alpar@96
|
772 |
last = tpath.last;
|
alpar@96
|
773 |
}
|
alpar@96
|
774 |
} else {
|
alpar@96
|
775 |
first = tpath.first;
|
alpar@96
|
776 |
last = tpath.last;
|
alpar@96
|
777 |
}
|
alpar@96
|
778 |
}
|
alpar@96
|
779 |
tpath.first = tpath.last = 0;
|
alpar@96
|
780 |
}
|
alpar@96
|
781 |
|
alpar@97
|
782 |
/// \brief Split the current path.
|
alpar@96
|
783 |
///
|
alpar@97
|
784 |
/// It splits the current path into two parts. The part before
|
alpar@97
|
785 |
/// the iterator \c it will remain in the current path and the part
|
alpar@97
|
786 |
/// starting with
|
alpar@97
|
787 |
/// \c it will put into \c tpath. If \c tpath have arcs
|
alpar@97
|
788 |
/// before the operation they are removed first. The time
|
kpeter@1201
|
789 |
/// complexity of this function is O(1) plus the time of emtying
|
alpar@97
|
790 |
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath
|
alpar@96
|
791 |
void split(ArcIt it, ListPath& tpath) {
|
alpar@96
|
792 |
tpath.clear();
|
alpar@96
|
793 |
if (it.node) {
|
alpar@96
|
794 |
tpath.first = it.node;
|
alpar@96
|
795 |
tpath.last = last;
|
alpar@96
|
796 |
if (it.node->prev) {
|
alpar@96
|
797 |
last = it.node->prev;
|
alpar@96
|
798 |
last->next = 0;
|
alpar@96
|
799 |
} else {
|
alpar@96
|
800 |
first = last = 0;
|
alpar@96
|
801 |
}
|
alpar@96
|
802 |
it.node->prev = 0;
|
alpar@96
|
803 |
}
|
alpar@96
|
804 |
}
|
alpar@96
|
805 |
|
alpar@96
|
806 |
|
alpar@96
|
807 |
typedef True BuildTag;
|
alpar@96
|
808 |
|
alpar@96
|
809 |
template <typename CPath>
|
alpar@96
|
810 |
void build(const CPath& path) {
|
alpar@96
|
811 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
812 |
addBack(it);
|
alpar@96
|
813 |
}
|
alpar@96
|
814 |
}
|
alpar@96
|
815 |
|
alpar@96
|
816 |
template <typename CPath>
|
alpar@96
|
817 |
void buildRev(const CPath& path) {
|
alpar@96
|
818 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
819 |
addFront(it);
|
alpar@96
|
820 |
}
|
alpar@96
|
821 |
}
|
alpar@96
|
822 |
|
alpar@96
|
823 |
};
|
alpar@96
|
824 |
|
alpar@96
|
825 |
/// \brief A structure for representing directed paths in a digraph.
|
alpar@96
|
826 |
///
|
alpar@96
|
827 |
/// A structure for representing directed path in a digraph.
|
kpeter@559
|
828 |
/// \tparam GR The digraph type in which the path is.
|
alpar@96
|
829 |
///
|
kpeter@1201
|
830 |
/// In a sense, a path can be treated as a list of arcs. The
|
kpeter@1201
|
831 |
/// LEMON path type simply stores this list. As a consequence, it
|
kpeter@1201
|
832 |
/// cannot enumerate the nodes in the path, and the source node of
|
kpeter@1201
|
833 |
/// a zero-length path is undefined.
|
alpar@96
|
834 |
///
|
alpar@97
|
835 |
/// This implementation is completly static, i.e. it can be copy constucted
|
alpar@97
|
836 |
/// or copy assigned from another path, but otherwise it cannot be
|
alpar@97
|
837 |
/// modified.
|
alpar@97
|
838 |
///
|
kpeter@1201
|
839 |
/// Being the most memory-efficient path type in LEMON, it is
|
kpeter@1201
|
840 |
/// intented to be used when you want to store a large number of paths.
|
kpeter@559
|
841 |
template <typename GR>
|
alpar@96
|
842 |
class StaticPath {
|
alpar@96
|
843 |
public:
|
alpar@96
|
844 |
|
kpeter@559
|
845 |
typedef GR Digraph;
|
alpar@96
|
846 |
typedef typename Digraph::Arc Arc;
|
alpar@96
|
847 |
|
alpar@96
|
848 |
/// \brief Default constructor
|
alpar@96
|
849 |
///
|
alpar@96
|
850 |
/// Default constructor
|
ggab90@1130
|
851 |
StaticPath() : len(0), _arcs(0) {}
|
alpar@209
|
852 |
|
alpar@990
|
853 |
/// \brief Copy constructor
|
alpar@990
|
854 |
///
|
ggab90@1130
|
855 |
StaticPath(const StaticPath& cpath) : _arcs(0) {
|
alpar@990
|
856 |
pathCopy(cpath, *this);
|
alpar@990
|
857 |
}
|
alpar@990
|
858 |
|
alpar@96
|
859 |
/// \brief Template copy constructor
|
alpar@96
|
860 |
///
|
alpar@97
|
861 |
/// This path can be initialized from any other path type.
|
alpar@96
|
862 |
template <typename CPath>
|
ggab90@1130
|
863 |
StaticPath(const CPath& cpath) : _arcs(0) {
|
kpeter@516
|
864 |
pathCopy(cpath, *this);
|
alpar@96
|
865 |
}
|
alpar@96
|
866 |
|
alpar@96
|
867 |
/// \brief Destructor of the path
|
alpar@96
|
868 |
///
|
alpar@96
|
869 |
/// Destructor of the path
|
alpar@96
|
870 |
~StaticPath() {
|
ggab90@1130
|
871 |
if (_arcs) delete[] _arcs;
|
alpar@96
|
872 |
}
|
alpar@96
|
873 |
|
alpar@990
|
874 |
/// \brief Copy assignment
|
alpar@990
|
875 |
///
|
alpar@990
|
876 |
StaticPath& operator=(const StaticPath& cpath) {
|
alpar@990
|
877 |
pathCopy(cpath, *this);
|
alpar@990
|
878 |
return *this;
|
alpar@990
|
879 |
}
|
alpar@990
|
880 |
|
alpar@96
|
881 |
/// \brief Template copy assignment
|
alpar@96
|
882 |
///
|
alpar@97
|
883 |
/// This path can be made equal to any other path type. It simply
|
alpar@96
|
884 |
/// makes a copy of the given path.
|
alpar@96
|
885 |
template <typename CPath>
|
alpar@96
|
886 |
StaticPath& operator=(const CPath& cpath) {
|
kpeter@516
|
887 |
pathCopy(cpath, *this);
|
alpar@96
|
888 |
return *this;
|
alpar@96
|
889 |
}
|
alpar@96
|
890 |
|
alpar@96
|
891 |
/// \brief Iterator class to iterate on the arcs of the paths
|
alpar@96
|
892 |
///
|
alpar@96
|
893 |
/// This class is used to iterate on the arcs of the paths
|
alpar@96
|
894 |
///
|
alpar@96
|
895 |
/// Of course it converts to Digraph::Arc
|
alpar@96
|
896 |
class ArcIt {
|
alpar@96
|
897 |
friend class StaticPath;
|
alpar@96
|
898 |
public:
|
alpar@96
|
899 |
/// Default constructor
|
alpar@96
|
900 |
ArcIt() {}
|
alpar@96
|
901 |
/// Invalid constructor
|
alpar@96
|
902 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
alpar@96
|
903 |
/// Initializate the constructor to the first arc of path
|
alpar@209
|
904 |
ArcIt(const StaticPath &_path)
|
alpar@96
|
905 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
alpar@96
|
906 |
|
alpar@96
|
907 |
private:
|
alpar@96
|
908 |
|
alpar@96
|
909 |
/// Constructor with starting point
|
alpar@209
|
910 |
ArcIt(const StaticPath &_path, int _idx)
|
alpar@96
|
911 |
: idx(_idx), path(&_path) {}
|
alpar@96
|
912 |
|
alpar@96
|
913 |
public:
|
alpar@96
|
914 |
|
alpar@96
|
915 |
///Conversion to Digraph::Arc
|
alpar@96
|
916 |
operator const Arc&() const {
|
alpar@96
|
917 |
return path->nth(idx);
|
alpar@96
|
918 |
}
|
alpar@96
|
919 |
|
alpar@96
|
920 |
/// Next arc
|
alpar@209
|
921 |
ArcIt& operator++() {
|
alpar@96
|
922 |
++idx;
|
alpar@209
|
923 |
if (idx >= path->length()) idx = -1;
|
alpar@209
|
924 |
return *this;
|
alpar@96
|
925 |
}
|
alpar@96
|
926 |
|
alpar@96
|
927 |
/// Comparison operator
|
alpar@96
|
928 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
alpar@96
|
929 |
/// Comparison operator
|
alpar@96
|
930 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
alpar@96
|
931 |
/// Comparison operator
|
alpar@96
|
932 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
alpar@96
|
933 |
|
alpar@96
|
934 |
private:
|
alpar@96
|
935 |
const StaticPath *path;
|
alpar@96
|
936 |
int idx;
|
alpar@96
|
937 |
};
|
ggab90@1130
|
938 |
|
ggab90@1130
|
939 |
/// \brief Gets the collection of the arcs of the path.
|
ggab90@1130
|
940 |
///
|
ggab90@1130
|
941 |
/// This function can be used for iterating on the
|
ggab90@1130
|
942 |
/// arcs of the path. It returns a wrapped
|
ggab90@1130
|
943 |
/// ArcIt, which looks like an STL container
|
ggab90@1130
|
944 |
/// (by having begin() and end()) which you can use in range-based
|
ggab90@1130
|
945 |
/// for loops, STL algorithms, etc.
|
ggab90@1130
|
946 |
/// For example you can write:
|
ggab90@1130
|
947 |
///\code
|
ggab90@1130
|
948 |
/// for(auto a: p.arcs())
|
ggab90@1130
|
949 |
/// doSomething(a);
|
ggab90@1130
|
950 |
///\endcode
|
ggab90@1130
|
951 |
LemonRangeWrapper1<ArcIt, StaticPath> arcs() const {
|
ggab90@1130
|
952 |
return LemonRangeWrapper1<ArcIt, StaticPath>(*this);
|
ggab90@1130
|
953 |
}
|
ggab90@1130
|
954 |
|
alpar@96
|
955 |
|
kpeter@920
|
956 |
/// \brief The n-th arc.
|
alpar@96
|
957 |
///
|
kpeter@1201
|
958 |
/// Gives back the n-th arc. This function runs in O(1) time.
|
kpeter@1201
|
959 |
/// \pre \c n is in the range <tt>[0..length() - 1]</tt>.
|
alpar@96
|
960 |
const Arc& nth(int n) const {
|
ggab90@1130
|
961 |
return _arcs[n];
|
alpar@96
|
962 |
}
|
alpar@96
|
963 |
|
kpeter@920
|
964 |
/// \brief The arc iterator pointing to the n-th arc.
|
alpar@96
|
965 |
ArcIt nthIt(int n) const {
|
alpar@96
|
966 |
return ArcIt(*this, n);
|
alpar@96
|
967 |
}
|
alpar@96
|
968 |
|
alpar@97
|
969 |
/// \brief The length of the path.
|
alpar@96
|
970 |
int length() const { return len; }
|
alpar@96
|
971 |
|
alpar@97
|
972 |
/// \brief Return true when the path is empty.
|
alpar@96
|
973 |
int empty() const { return len == 0; }
|
alpar@96
|
974 |
|
kpeter@1201
|
975 |
/// \brief Reset the path to an empty one.
|
alpar@96
|
976 |
void clear() {
|
alpar@96
|
977 |
len = 0;
|
ggab90@1130
|
978 |
if (_arcs) delete[] _arcs;
|
ggab90@1130
|
979 |
_arcs = 0;
|
alpar@96
|
980 |
}
|
alpar@96
|
981 |
|
alpar@97
|
982 |
/// \brief The first arc of the path.
|
alpar@96
|
983 |
const Arc& front() const {
|
ggab90@1130
|
984 |
return _arcs[0];
|
alpar@96
|
985 |
}
|
alpar@96
|
986 |
|
alpar@97
|
987 |
/// \brief The last arc of the path.
|
alpar@96
|
988 |
const Arc& back() const {
|
ggab90@1130
|
989 |
return _arcs[len - 1];
|
alpar@96
|
990 |
}
|
alpar@96
|
991 |
|
alpar@96
|
992 |
|
alpar@96
|
993 |
typedef True BuildTag;
|
alpar@96
|
994 |
|
alpar@96
|
995 |
template <typename CPath>
|
alpar@96
|
996 |
void build(const CPath& path) {
|
alpar@96
|
997 |
len = path.length();
|
ggab90@1130
|
998 |
_arcs = new Arc[len];
|
alpar@96
|
999 |
int index = 0;
|
alpar@96
|
1000 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
ggab90@1130
|
1001 |
_arcs[index] = it;
|
alpar@96
|
1002 |
++index;
|
alpar@96
|
1003 |
}
|
alpar@96
|
1004 |
}
|
alpar@96
|
1005 |
|
alpar@96
|
1006 |
template <typename CPath>
|
alpar@96
|
1007 |
void buildRev(const CPath& path) {
|
alpar@96
|
1008 |
len = path.length();
|
ggab90@1130
|
1009 |
_arcs = new Arc[len];
|
alpar@96
|
1010 |
int index = len;
|
alpar@96
|
1011 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
alpar@96
|
1012 |
--index;
|
ggab90@1130
|
1013 |
_arcs[index] = it;
|
alpar@96
|
1014 |
}
|
alpar@96
|
1015 |
}
|
alpar@96
|
1016 |
|
alpar@96
|
1017 |
private:
|
alpar@96
|
1018 |
int len;
|
ggab90@1130
|
1019 |
Arc* _arcs;
|
alpar@96
|
1020 |
};
|
alpar@96
|
1021 |
|
alpar@98
|
1022 |
///////////////////////////////////////////////////////////////////////
|
alpar@98
|
1023 |
// Additional utilities
|
alpar@98
|
1024 |
///////////////////////////////////////////////////////////////////////
|
alpar@98
|
1025 |
|
alpar@98
|
1026 |
namespace _path_bits {
|
alpar@98
|
1027 |
|
alpar@98
|
1028 |
template <typename Path, typename Enable = void>
|
deba@144
|
1029 |
struct RevPathTagIndicator {
|
alpar@98
|
1030 |
static const bool value = false;
|
alpar@98
|
1031 |
};
|
alpar@98
|
1032 |
|
deba@144
|
1033 |
template <typename Path>
|
deba@144
|
1034 |
struct RevPathTagIndicator<
|
alpar@209
|
1035 |
Path,
|
deba@144
|
1036 |
typename enable_if<typename Path::RevPathTag, void>::type
|
deba@144
|
1037 |
> {
|
deba@144
|
1038 |
static const bool value = true;
|
deba@144
|
1039 |
};
|
deba@144
|
1040 |
|
deba@144
|
1041 |
template <typename Path, typename Enable = void>
|
deba@144
|
1042 |
struct BuildTagIndicator {
|
deba@144
|
1043 |
static const bool value = false;
|
deba@144
|
1044 |
};
|
deba@144
|
1045 |
|
deba@144
|
1046 |
template <typename Path>
|
deba@144
|
1047 |
struct BuildTagIndicator<
|
alpar@209
|
1048 |
Path,
|
deba@144
|
1049 |
typename enable_if<typename Path::BuildTag, void>::type
|
alpar@98
|
1050 |
> {
|
alpar@98
|
1051 |
static const bool value = true;
|
alpar@98
|
1052 |
};
|
alpar@98
|
1053 |
|
kpeter@516
|
1054 |
template <typename From, typename To,
|
kpeter@516
|
1055 |
bool buildEnable = BuildTagIndicator<To>::value>
|
kpeter@498
|
1056 |
struct PathCopySelectorForward {
|
kpeter@516
|
1057 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1058 |
to.clear();
|
kpeter@516
|
1059 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
kpeter@516
|
1060 |
to.addBack(it);
|
alpar@98
|
1061 |
}
|
alpar@98
|
1062 |
}
|
alpar@98
|
1063 |
};
|
alpar@98
|
1064 |
|
kpeter@516
|
1065 |
template <typename From, typename To>
|
kpeter@516
|
1066 |
struct PathCopySelectorForward<From, To, true> {
|
kpeter@516
|
1067 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1068 |
to.clear();
|
kpeter@516
|
1069 |
to.build(from);
|
kpeter@498
|
1070 |
}
|
kpeter@498
|
1071 |
};
|
kpeter@498
|
1072 |
|
kpeter@516
|
1073 |
template <typename From, typename To,
|
kpeter@516
|
1074 |
bool buildEnable = BuildTagIndicator<To>::value>
|
kpeter@498
|
1075 |
struct PathCopySelectorBackward {
|
kpeter@516
|
1076 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1077 |
to.clear();
|
kpeter@516
|
1078 |
for (typename From::RevArcIt it(from); it != INVALID; ++it) {
|
kpeter@516
|
1079 |
to.addFront(it);
|
alpar@98
|
1080 |
}
|
alpar@98
|
1081 |
}
|
alpar@98
|
1082 |
};
|
alpar@98
|
1083 |
|
kpeter@516
|
1084 |
template <typename From, typename To>
|
kpeter@516
|
1085 |
struct PathCopySelectorBackward<From, To, true> {
|
kpeter@516
|
1086 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1087 |
to.clear();
|
kpeter@516
|
1088 |
to.buildRev(from);
|
alpar@98
|
1089 |
}
|
alpar@98
|
1090 |
};
|
alpar@98
|
1091 |
|
alpar@877
|
1092 |
|
kpeter@516
|
1093 |
template <typename From, typename To,
|
kpeter@516
|
1094 |
bool revEnable = RevPathTagIndicator<From>::value>
|
kpeter@498
|
1095 |
struct PathCopySelector {
|
kpeter@516
|
1096 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1097 |
PathCopySelectorForward<From, To>::copy(from, to);
|
alpar@877
|
1098 |
}
|
kpeter@498
|
1099 |
};
|
kpeter@498
|
1100 |
|
kpeter@516
|
1101 |
template <typename From, typename To>
|
kpeter@516
|
1102 |
struct PathCopySelector<From, To, true> {
|
kpeter@516
|
1103 |
static void copy(const From& from, To& to) {
|
kpeter@516
|
1104 |
PathCopySelectorBackward<From, To>::copy(from, to);
|
alpar@877
|
1105 |
}
|
kpeter@498
|
1106 |
};
|
kpeter@498
|
1107 |
|
alpar@98
|
1108 |
}
|
alpar@98
|
1109 |
|
alpar@98
|
1110 |
|
alpar@98
|
1111 |
/// \brief Make a copy of a path.
|
alpar@98
|
1112 |
///
|
kpeter@516
|
1113 |
/// This function makes a copy of a path.
|
kpeter@516
|
1114 |
template <typename From, typename To>
|
kpeter@516
|
1115 |
void pathCopy(const From& from, To& to) {
|
kpeter@516
|
1116 |
checkConcept<concepts::PathDumper<typename From::Digraph>, From>();
|
kpeter@516
|
1117 |
_path_bits::PathCopySelector<From, To>::copy(from, to);
|
kpeter@516
|
1118 |
}
|
kpeter@516
|
1119 |
|
kpeter@516
|
1120 |
/// \brief Deprecated version of \ref pathCopy().
|
kpeter@516
|
1121 |
///
|
kpeter@516
|
1122 |
/// Deprecated version of \ref pathCopy() (only for reverse compatibility).
|
kpeter@516
|
1123 |
template <typename To, typename From>
|
kpeter@516
|
1124 |
void copyPath(To& to, const From& from) {
|
kpeter@516
|
1125 |
pathCopy(from, to);
|
alpar@98
|
1126 |
}
|
alpar@98
|
1127 |
|
alpar@98
|
1128 |
/// \brief Check the consistency of a path.
|
alpar@98
|
1129 |
///
|
alpar@98
|
1130 |
/// This function checks that the target of each arc is the same
|
alpar@209
|
1131 |
/// as the source of the next one.
|
alpar@209
|
1132 |
///
|
alpar@98
|
1133 |
template <typename Digraph, typename Path>
|
alpar@98
|
1134 |
bool checkPath(const Digraph& digraph, const Path& path) {
|
alpar@98
|
1135 |
typename Path::ArcIt it(path);
|
alpar@98
|
1136 |
if (it == INVALID) return true;
|
alpar@98
|
1137 |
typename Digraph::Node node = digraph.target(it);
|
alpar@98
|
1138 |
++it;
|
alpar@98
|
1139 |
while (it != INVALID) {
|
alpar@98
|
1140 |
if (digraph.source(it) != node) return false;
|
alpar@98
|
1141 |
node = digraph.target(it);
|
alpar@98
|
1142 |
++it;
|
alpar@98
|
1143 |
}
|
alpar@98
|
1144 |
return true;
|
alpar@98
|
1145 |
}
|
alpar@98
|
1146 |
|
alpar@98
|
1147 |
/// \brief The source of a path
|
alpar@98
|
1148 |
///
|
kpeter@514
|
1149 |
/// This function returns the source node of the given path.
|
kpeter@514
|
1150 |
/// If the path is empty, then it returns \c INVALID.
|
alpar@98
|
1151 |
template <typename Digraph, typename Path>
|
alpar@98
|
1152 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
|
kpeter@514
|
1153 |
return path.empty() ? INVALID : digraph.source(path.front());
|
alpar@98
|
1154 |
}
|
alpar@98
|
1155 |
|
alpar@98
|
1156 |
/// \brief The target of a path
|
alpar@98
|
1157 |
///
|
kpeter@514
|
1158 |
/// This function returns the target node of the given path.
|
kpeter@514
|
1159 |
/// If the path is empty, then it returns \c INVALID.
|
alpar@98
|
1160 |
template <typename Digraph, typename Path>
|
alpar@98
|
1161 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
|
kpeter@514
|
1162 |
return path.empty() ? INVALID : digraph.target(path.back());
|
alpar@98
|
1163 |
}
|
alpar@98
|
1164 |
|
kpeter@1201
|
1165 |
/// \brief Class for iterating through the nodes of a path
|
alpar@98
|
1166 |
///
|
kpeter@1201
|
1167 |
/// Class for iterating through the nodes of a path.
|
alpar@98
|
1168 |
///
|
kpeter@1201
|
1169 |
/// In a sense, a path can be treated as a list of arcs. The
|
kpeter@1201
|
1170 |
/// LEMON path type simply stores this list. As a consequence, it
|
kpeter@1201
|
1171 |
/// cannot enumerate the nodes in the path, and the source node of
|
kpeter@1201
|
1172 |
/// a zero-length path is undefined.
|
kpeter@1201
|
1173 |
///
|
kpeter@1201
|
1174 |
/// However, this class implements a node iterator for path structures.
|
kpeter@1201
|
1175 |
/// To provide this feature, the underlying digraph should be passed to
|
alpar@98
|
1176 |
/// the constructor of the iterator.
|
alpar@98
|
1177 |
template <typename Path>
|
alpar@98
|
1178 |
class PathNodeIt {
|
alpar@98
|
1179 |
private:
|
alpar@98
|
1180 |
const typename Path::Digraph *_digraph;
|
alpar@98
|
1181 |
typename Path::ArcIt _it;
|
alpar@98
|
1182 |
typename Path::Digraph::Node _nd;
|
alpar@98
|
1183 |
|
alpar@98
|
1184 |
public:
|
alpar@98
|
1185 |
|
alpar@98
|
1186 |
typedef typename Path::Digraph Digraph;
|
alpar@98
|
1187 |
typedef typename Digraph::Node Node;
|
alpar@209
|
1188 |
|
alpar@98
|
1189 |
/// Default constructor
|
alpar@98
|
1190 |
PathNodeIt() {}
|
alpar@98
|
1191 |
/// Invalid constructor
|
alpar@209
|
1192 |
PathNodeIt(Invalid)
|
alpar@98
|
1193 |
: _digraph(0), _it(INVALID), _nd(INVALID) {}
|
alpar@98
|
1194 |
/// Constructor
|
alpar@209
|
1195 |
PathNodeIt(const Digraph& digraph, const Path& path)
|
alpar@98
|
1196 |
: _digraph(&digraph), _it(path) {
|
alpar@98
|
1197 |
_nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
|
alpar@98
|
1198 |
}
|
alpar@98
|
1199 |
/// Constructor
|
alpar@209
|
1200 |
PathNodeIt(const Digraph& digraph, const Path& path, const Node& src)
|
alpar@98
|
1201 |
: _digraph(&digraph), _it(path), _nd(src) {}
|
alpar@98
|
1202 |
|
alpar@98
|
1203 |
///Conversion to Digraph::Node
|
alpar@98
|
1204 |
operator Node() const {
|
alpar@98
|
1205 |
return _nd;
|
alpar@98
|
1206 |
}
|
alpar@98
|
1207 |
|
alpar@98
|
1208 |
/// Next node
|
alpar@98
|
1209 |
PathNodeIt& operator++() {
|
alpar@98
|
1210 |
if (_it == INVALID) _nd = INVALID;
|
alpar@98
|
1211 |
else {
|
alpar@209
|
1212 |
_nd = _digraph->target(_it);
|
alpar@209
|
1213 |
++_it;
|
alpar@98
|
1214 |
}
|
alpar@98
|
1215 |
return *this;
|
alpar@98
|
1216 |
}
|
alpar@98
|
1217 |
|
alpar@98
|
1218 |
/// Comparison operator
|
alpar@209
|
1219 |
bool operator==(const PathNodeIt& n) const {
|
alpar@209
|
1220 |
return _it == n._it && _nd == n._nd;
|
alpar@98
|
1221 |
}
|
alpar@98
|
1222 |
/// Comparison operator
|
alpar@209
|
1223 |
bool operator!=(const PathNodeIt& n) const {
|
alpar@209
|
1224 |
return _it != n._it || _nd != n._nd;
|
alpar@98
|
1225 |
}
|
alpar@98
|
1226 |
/// Comparison operator
|
alpar@209
|
1227 |
bool operator<(const PathNodeIt& n) const {
|
alpar@98
|
1228 |
return (_it < n._it && _nd != INVALID);
|
alpar@98
|
1229 |
}
|
alpar@209
|
1230 |
|
alpar@98
|
1231 |
};
|
alpar@209
|
1232 |
|
ggab90@1130
|
1233 |
/// \brief Gets the collection of the nodes of the path.
|
ggab90@1130
|
1234 |
///
|
ggab90@1130
|
1235 |
/// This function can be used for iterating on the
|
ggab90@1130
|
1236 |
/// nodes of the path. It returns a wrapped
|
ggab90@1130
|
1237 |
/// PathNodeIt, which looks like an STL container
|
ggab90@1130
|
1238 |
/// (by having begin() and end()) which you can use in range-based
|
ggab90@1130
|
1239 |
/// for loops, STL algorithms, etc.
|
ggab90@1130
|
1240 |
/// For example you can write:
|
ggab90@1130
|
1241 |
///\code
|
ggab90@1130
|
1242 |
/// for(auto u: pathNodes(g,p))
|
ggab90@1130
|
1243 |
/// doSomething(u);
|
ggab90@1130
|
1244 |
///\endcode
|
ggab90@1130
|
1245 |
template<typename Path>
|
ggab90@1130
|
1246 |
LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>
|
ggab90@1130
|
1247 |
pathNodes(const typename Path::Digraph &g, const Path &p) {
|
ggab90@1130
|
1248 |
return
|
ggab90@1130
|
1249 |
LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>(g,p);
|
ggab90@1130
|
1250 |
}
|
ggab90@1130
|
1251 |
|
alpar@96
|
1252 |
///@}
|
alpar@96
|
1253 |
|
alpar@96
|
1254 |
} // namespace lemon
|
alpar@96
|
1255 |
|
alpar@96
|
1256 |
#endif // LEMON_PATH_H
|