| 1 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
| 2 |
2 |
*
|
| 3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
| 4 |
4 |
*
|
| 5 |
5 |
* Copyright (C) 2003-2009
|
| 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 |
#ifndef LEMON_CORE_H
|
| 20 |
20 |
#define LEMON_CORE_H
|
| 21 |
21 |
|
| 22 |
22 |
#include <vector>
|
| 23 |
23 |
#include <algorithm>
|
| 24 |
24 |
|
| 25 |
25 |
#include <lemon/config.h>
|
| 26 |
26 |
#include <lemon/bits/enable_if.h>
|
| 27 |
27 |
#include <lemon/bits/traits.h>
|
| 28 |
28 |
#include <lemon/assert.h>
|
| 29 |
29 |
|
| 30 |
30 |
// Disable the following warnings when compiling with MSVC:
|
| 31 |
31 |
// C4250: 'class1' : inherits 'class2::member' via dominance
|
| 32 |
32 |
// C4355: 'this' : used in base member initializer list
|
| 33 |
33 |
// C4503: 'function' : decorated name length exceeded, name was truncated
|
| 34 |
34 |
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
|
| 35 |
35 |
// C4996: 'function': was declared deprecated
|
| 36 |
36 |
#ifdef _MSC_VER
|
| 37 |
37 |
#pragma warning( disable : 4250 4355 4503 4800 4996 )
|
| 38 |
38 |
#endif
|
| 39 |
39 |
|
| 40 |
40 |
///\file
|
| 41 |
41 |
///\brief LEMON core utilities.
|
| 42 |
42 |
///
|
| 43 |
43 |
///This header file contains core utilities for LEMON.
|
| 44 |
44 |
///It is automatically included by all graph types, therefore it usually
|
| 45 |
45 |
///do not have to be included directly.
|
| 46 |
46 |
|
| 47 |
47 |
namespace lemon {
|
| 48 |
48 |
|
| 49 |
49 |
/// \brief Dummy type to make it easier to create invalid iterators.
|
| 50 |
50 |
///
|
| 51 |
51 |
/// Dummy type to make it easier to create invalid iterators.
|
| 52 |
52 |
/// See \ref INVALID for the usage.
|
| 53 |
53 |
struct Invalid {
|
| 54 |
54 |
public:
|
| 55 |
55 |
bool operator==(Invalid) { return true; }
|
| 56 |
56 |
bool operator!=(Invalid) { return false; }
|
| 57 |
57 |
bool operator< (Invalid) { return false; }
|
| 58 |
58 |
};
|
| 59 |
59 |
|
| 60 |
60 |
/// \brief Invalid iterators.
|
| 61 |
61 |
///
|
| 62 |
62 |
/// \ref Invalid is a global type that converts to each iterator
|
| 63 |
63 |
/// in such a way that the value of the target iterator will be invalid.
|
| 64 |
64 |
#ifdef LEMON_ONLY_TEMPLATES
|
| 65 |
65 |
const Invalid INVALID = Invalid();
|
| 66 |
66 |
#else
|
| 67 |
67 |
extern const Invalid INVALID;
|
| 68 |
68 |
#endif
|
| 69 |
69 |
|
| 70 |
70 |
/// \addtogroup gutils
|
| 71 |
71 |
/// @{
|
| 72 |
72 |
|
| 73 |
73 |
///Create convenience typedefs for the digraph types and iterators
|
| 74 |
74 |
|
| 75 |
75 |
///This \c \#define creates convenient type definitions for the following
|
| 76 |
76 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
|
| 77 |
77 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
|
| 78 |
78 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
|
| 79 |
79 |
///
|
| 80 |
80 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
| 81 |
81 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
|
| 82 |
82 |
///macro.
|
| 83 |
83 |
#define DIGRAPH_TYPEDEFS(Digraph) \
|
| 84 |
84 |
typedef Digraph::Node Node; \
|
| 85 |
85 |
typedef Digraph::NodeIt NodeIt; \
|
| 86 |
86 |
typedef Digraph::Arc Arc; \
|
| 87 |
87 |
typedef Digraph::ArcIt ArcIt; \
|
| 88 |
88 |
typedef Digraph::InArcIt InArcIt; \
|
| 89 |
89 |
typedef Digraph::OutArcIt OutArcIt; \
|
| 90 |
90 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \
|
| 91 |
91 |
typedef Digraph::NodeMap<int> IntNodeMap; \
|
| 92 |
92 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \
|
| 93 |
93 |
typedef Digraph::ArcMap<bool> BoolArcMap; \
|
| 94 |
94 |
typedef Digraph::ArcMap<int> IntArcMap; \
|
| 95 |
95 |
typedef Digraph::ArcMap<double> DoubleArcMap
|
| 96 |
96 |
|
| 97 |
97 |
///Create convenience typedefs for the digraph types and iterators
|
| 98 |
98 |
|
| 99 |
99 |
///\see DIGRAPH_TYPEDEFS
|
| 100 |
100 |
///
|
| 101 |
101 |
///\note Use this macro, if the graph type is a dependent type,
|
| 102 |
102 |
///ie. the graph type depend on a template parameter.
|
| 103 |
103 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
|
| 104 |
104 |
typedef typename Digraph::Node Node; \
|
| 105 |
105 |
typedef typename Digraph::NodeIt NodeIt; \
|
| 106 |
106 |
typedef typename Digraph::Arc Arc; \
|
| 107 |
107 |
typedef typename Digraph::ArcIt ArcIt; \
|
| 108 |
108 |
typedef typename Digraph::InArcIt InArcIt; \
|
| 109 |
109 |
typedef typename Digraph::OutArcIt OutArcIt; \
|
| 110 |
110 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
|
| 111 |
111 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \
|
| 112 |
112 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
|
| 113 |
113 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
|
| 114 |
114 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \
|
| 115 |
115 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap
|
| 116 |
116 |
|
| 117 |
117 |
///Create convenience typedefs for the graph types and iterators
|
| 118 |
118 |
|
| 119 |
119 |
///This \c \#define creates the same convenient type definitions as defined
|
| 120 |
120 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
|
| 121 |
121 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
|
| 122 |
122 |
///\c DoubleEdgeMap.
|
| 123 |
123 |
///
|
| 124 |
124 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
| 125 |
125 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
|
| 126 |
126 |
///macro.
|
| 127 |
127 |
#define GRAPH_TYPEDEFS(Graph) \
|
| 128 |
128 |
DIGRAPH_TYPEDEFS(Graph); \
|
| 129 |
129 |
typedef Graph::Edge Edge; \
|
| 130 |
130 |
typedef Graph::EdgeIt EdgeIt; \
|
| 131 |
131 |
typedef Graph::IncEdgeIt IncEdgeIt; \
|
| 132 |
132 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \
|
| 133 |
133 |
typedef Graph::EdgeMap<int> IntEdgeMap; \
|
| 134 |
134 |
typedef Graph::EdgeMap<double> DoubleEdgeMap
|
| 135 |
135 |
|
| 136 |
136 |
///Create convenience typedefs for the graph types and iterators
|
| 137 |
137 |
|
| 138 |
138 |
///\see GRAPH_TYPEDEFS
|
| 139 |
139 |
///
|
| 140 |
140 |
///\note Use this macro, if the graph type is a dependent type,
|
| 141 |
141 |
///ie. the graph type depend on a template parameter.
|
| 142 |
142 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
|
| 143 |
143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
|
| 144 |
144 |
typedef typename Graph::Edge Edge; \
|
| 145 |
145 |
typedef typename Graph::EdgeIt EdgeIt; \
|
| 146 |
146 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \
|
| 147 |
147 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
|
| 148 |
148 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
|
| 149 |
149 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
|
| 150 |
150 |
|
| 151 |
151 |
/// \brief Function to count the items in a graph.
|
| 152 |
152 |
///
|
| 153 |
153 |
/// This function counts the items (nodes, arcs etc.) in a graph.
|
| 154 |
154 |
/// The complexity of the function is linear because
|
| 155 |
155 |
/// it iterates on all of the items.
|
| 156 |
156 |
template <typename Graph, typename Item>
|
| 157 |
157 |
inline int countItems(const Graph& g) {
|
| 158 |
158 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
| 159 |
159 |
int num = 0;
|
| 160 |
160 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 161 |
161 |
++num;
|
| 162 |
162 |
}
|
| 163 |
163 |
return num;
|
| 164 |
164 |
}
|
| 165 |
165 |
|
| 166 |
166 |
// Node counting:
|
| 167 |
167 |
|
| 168 |
168 |
namespace _core_bits {
|
| 169 |
169 |
|
| 170 |
170 |
template <typename Graph, typename Enable = void>
|
| 171 |
171 |
struct CountNodesSelector {
|
| 172 |
172 |
static int count(const Graph &g) {
|
| 173 |
173 |
return countItems<Graph, typename Graph::Node>(g);
|
| 174 |
174 |
}
|
| 175 |
175 |
};
|
| 176 |
176 |
|
| 177 |
177 |
template <typename Graph>
|
| 178 |
178 |
struct CountNodesSelector<
|
| 179 |
179 |
Graph, typename
|
| 180 |
180 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
| 181 |
181 |
{
|
| 182 |
182 |
static int count(const Graph &g) {
|
| 183 |
183 |
return g.nodeNum();
|
| 184 |
184 |
}
|
| 185 |
185 |
};
|
| 186 |
186 |
}
|
| 187 |
187 |
|
| 188 |
188 |
/// \brief Function to count the nodes in the graph.
|
| 189 |
189 |
///
|
| 190 |
190 |
/// This function counts the nodes in the graph.
|
| 191 |
191 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some
|
| 192 |
192 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
| 193 |
193 |
///
|
| 194 |
194 |
/// \note If the graph contains a \c nodeNum() member function and a
|
| 195 |
195 |
/// \c NodeNumTag tag then this function calls directly the member
|
| 196 |
196 |
/// function to query the cardinality of the node set.
|
| 197 |
197 |
template <typename Graph>
|
| 198 |
198 |
inline int countNodes(const Graph& g) {
|
| 199 |
199 |
return _core_bits::CountNodesSelector<Graph>::count(g);
|
| 200 |
200 |
}
|
| 201 |
201 |
|
| 202 |
202 |
// Arc counting:
|
| 203 |
203 |
|
| 204 |
204 |
namespace _core_bits {
|
| 205 |
205 |
|
| 206 |
206 |
template <typename Graph, typename Enable = void>
|
| 207 |
207 |
struct CountArcsSelector {
|
| 208 |
208 |
static int count(const Graph &g) {
|
| 209 |
209 |
return countItems<Graph, typename Graph::Arc>(g);
|
| 210 |
210 |
}
|
| 211 |
211 |
};
|
| 212 |
212 |
|
| 213 |
213 |
template <typename Graph>
|
| 214 |
214 |
struct CountArcsSelector<
|
| 215 |
215 |
Graph,
|
| 216 |
216 |
typename enable_if<typename Graph::ArcNumTag, void>::type>
|
| 217 |
217 |
{
|
| 218 |
218 |
static int count(const Graph &g) {
|
| 219 |
219 |
return g.arcNum();
|
| 220 |
220 |
}
|
| 221 |
221 |
};
|
| 222 |
222 |
}
|
| 223 |
223 |
|
| 224 |
224 |
/// \brief Function to count the arcs in the graph.
|
| 225 |
225 |
///
|
| 226 |
226 |
/// This function counts the arcs in the graph.
|
| 227 |
227 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
| 228 |
228 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
| 229 |
229 |
///
|
| 230 |
230 |
/// \note If the graph contains a \c arcNum() member function and a
|
| 231 |
231 |
/// \c ArcNumTag tag then this function calls directly the member
|
| 232 |
232 |
/// function to query the cardinality of the arc set.
|
| 233 |
233 |
template <typename Graph>
|
| 234 |
234 |
inline int countArcs(const Graph& g) {
|
| 235 |
235 |
return _core_bits::CountArcsSelector<Graph>::count(g);
|
| 236 |
236 |
}
|
| 237 |
237 |
|
| 238 |
238 |
// Edge counting:
|
| 239 |
239 |
|
| 240 |
240 |
namespace _core_bits {
|
| 241 |
241 |
|
| 242 |
242 |
template <typename Graph, typename Enable = void>
|
| 243 |
243 |
struct CountEdgesSelector {
|
| 244 |
244 |
static int count(const Graph &g) {
|
| 245 |
245 |
return countItems<Graph, typename Graph::Edge>(g);
|
| 246 |
246 |
}
|
| 247 |
247 |
};
|
| 248 |
248 |
|
| 249 |
249 |
template <typename Graph>
|
| 250 |
250 |
struct CountEdgesSelector<
|
| 251 |
251 |
Graph,
|
| 252 |
252 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
| 253 |
253 |
{
|
| 254 |
254 |
static int count(const Graph &g) {
|
| 255 |
255 |
return g.edgeNum();
|
| 256 |
256 |
}
|
| 257 |
257 |
};
|
| 258 |
258 |
}
|
| 259 |
259 |
|
| 260 |
260 |
/// \brief Function to count the edges in the graph.
|
| 261 |
261 |
///
|
| 262 |
262 |
/// This function counts the edges in the graph.
|
| 263 |
263 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
| 264 |
264 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
| 265 |
265 |
///
|
| 266 |
266 |
/// \note If the graph contains a \c edgeNum() member function and a
|
| 267 |
267 |
/// \c EdgeNumTag tag then this function calls directly the member
|
| 268 |
268 |
/// function to query the cardinality of the edge set.
|
| 269 |
269 |
template <typename Graph>
|
| 270 |
270 |
inline int countEdges(const Graph& g) {
|
| 271 |
271 |
return _core_bits::CountEdgesSelector<Graph>::count(g);
|
| 272 |
272 |
|
| 273 |
273 |
}
|
| 274 |
274 |
|
| 275 |
275 |
|
| 276 |
276 |
template <typename Graph, typename DegIt>
|
| 277 |
277 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 278 |
278 |
int num = 0;
|
| 279 |
279 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 280 |
280 |
++num;
|
| 281 |
281 |
}
|
| 282 |
282 |
return num;
|
| 283 |
283 |
}
|
| 284 |
284 |
|
| 285 |
285 |
/// \brief Function to count the number of the out-arcs from node \c n.
|
| 286 |
286 |
///
|
| 287 |
287 |
/// This function counts the number of the out-arcs from node \c n
|
| 288 |
288 |
/// in the graph \c g.
|
| 289 |
289 |
template <typename Graph>
|
| 290 |
290 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
|
| 291 |
291 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
|
| 292 |
292 |
}
|
| 293 |
293 |
|
| 294 |
294 |
/// \brief Function to count the number of the in-arcs to node \c n.
|
| 295 |
295 |
///
|
| 296 |
296 |
/// This function counts the number of the in-arcs to node \c n
|
| 297 |
297 |
/// in the graph \c g.
|
| 298 |
298 |
template <typename Graph>
|
| 299 |
299 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
|
| 300 |
300 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
|
| 301 |
301 |
}
|
| 302 |
302 |
|
| 303 |
303 |
/// \brief Function to count the number of the inc-edges to node \c n.
|
| 304 |
304 |
///
|
| 305 |
305 |
/// This function counts the number of the inc-edges to node \c n
|
| 306 |
306 |
/// in the undirected graph \c g.
|
| 307 |
307 |
template <typename Graph>
|
| 308 |
308 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
|
| 309 |
309 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
|
| 310 |
310 |
}
|
| 311 |
311 |
|
| 312 |
312 |
namespace _core_bits {
|
| 313 |
313 |
|
| 314 |
314 |
template <typename Digraph, typename Item, typename RefMap>
|
| 315 |
315 |
class MapCopyBase {
|
| 316 |
316 |
public:
|
| 317 |
317 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
|
| 318 |
318 |
|
| 319 |
319 |
virtual ~MapCopyBase() {}
|
| 320 |
320 |
};
|
| 321 |
321 |
|
| 322 |
322 |
template <typename Digraph, typename Item, typename RefMap,
|
| 323 |
323 |
typename FromMap, typename ToMap>
|
| 324 |
324 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 325 |
325 |
public:
|
| 326 |
326 |
|
| 327 |
327 |
MapCopy(const FromMap& map, ToMap& tmap)
|
| 328 |
328 |
: _map(map), _tmap(tmap) {}
|
| 329 |
329 |
|
| 330 |
330 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 331 |
331 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 332 |
332 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 333 |
333 |
_tmap.set(refMap[it], _map[it]);
|
| 334 |
334 |
}
|
| 335 |
335 |
}
|
| 336 |
336 |
|
| 337 |
337 |
private:
|
| 338 |
338 |
const FromMap& _map;
|
| 339 |
339 |
ToMap& _tmap;
|
| 340 |
340 |
};
|
| 341 |
341 |
|
| 342 |
342 |
template <typename Digraph, typename Item, typename RefMap, typename It>
|
| 343 |
343 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 344 |
344 |
public:
|
| 345 |
345 |
|
| 346 |
346 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
|
| 347 |
347 |
|
| 348 |
348 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 349 |
349 |
_it = refMap[_item];
|
| 350 |
350 |
}
|
| 351 |
351 |
|
| 352 |
352 |
private:
|
| 353 |
353 |
Item _item;
|
| 354 |
354 |
It& _it;
|
| 355 |
355 |
};
|
| 356 |
356 |
|
| 357 |
357 |
template <typename Digraph, typename Item, typename RefMap, typename Ref>
|
| 358 |
358 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 359 |
359 |
public:
|
| 360 |
360 |
|
| 361 |
361 |
RefCopy(Ref& map) : _map(map) {}
|
| 362 |
362 |
|
| 363 |
363 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 364 |
364 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 365 |
365 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 366 |
366 |
_map.set(it, refMap[it]);
|
| 367 |
367 |
}
|
| 368 |
368 |
}
|
| 369 |
369 |
|
| 370 |
370 |
private:
|
| 371 |
371 |
Ref& _map;
|
| 372 |
372 |
};
|
| 373 |
373 |
|
| 374 |
374 |
template <typename Digraph, typename Item, typename RefMap,
|
| 375 |
375 |
typename CrossRef>
|
| 376 |
376 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 377 |
377 |
public:
|
| 378 |
378 |
|
| 379 |
379 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 380 |
380 |
|
| 381 |
381 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 382 |
382 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 383 |
383 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 384 |
384 |
_cmap.set(refMap[it], it);
|
| 385 |
385 |
}
|
| 386 |
386 |
}
|
| 387 |
387 |
|
| 388 |
388 |
private:
|
| 389 |
389 |
CrossRef& _cmap;
|
| 390 |
390 |
};
|
| 391 |
391 |
|
| 392 |
392 |
template <typename Digraph, typename Enable = void>
|
| 393 |
393 |
struct DigraphCopySelector {
|
| 394 |
394 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
| 395 |
395 |
static void copy(const From& from, Digraph &to,
|
| 396 |
396 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
|
397 |
to.clear();
|
| 397 |
398 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 398 |
399 |
nodeRefMap[it] = to.addNode();
|
| 399 |
400 |
}
|
| 400 |
401 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 401 |
402 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
|
| 402 |
403 |
nodeRefMap[from.target(it)]);
|
| 403 |
404 |
}
|
| 404 |
405 |
}
|
| 405 |
406 |
};
|
| 406 |
407 |
|
| 407 |
408 |
template <typename Digraph>
|
| 408 |
409 |
struct DigraphCopySelector<
|
| 409 |
410 |
Digraph,
|
| 410 |
411 |
typename enable_if<typename Digraph::BuildTag, void>::type>
|
| 411 |
412 |
{
|
| 412 |
413 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
| 413 |
414 |
static void copy(const From& from, Digraph &to,
|
| 414 |
415 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 415 |
416 |
to.build(from, nodeRefMap, arcRefMap);
|
| 416 |
417 |
}
|
| 417 |
418 |
};
|
| 418 |
419 |
|
| 419 |
420 |
template <typename Graph, typename Enable = void>
|
| 420 |
421 |
struct GraphCopySelector {
|
| 421 |
422 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
| 422 |
423 |
static void copy(const From& from, Graph &to,
|
| 423 |
424 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
|
425 |
to.clear();
|
| 424 |
426 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 425 |
427 |
nodeRefMap[it] = to.addNode();
|
| 426 |
428 |
}
|
| 427 |
429 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 428 |
430 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
|
| 429 |
431 |
nodeRefMap[from.v(it)]);
|
| 430 |
432 |
}
|
| 431 |
433 |
}
|
| 432 |
434 |
};
|
| 433 |
435 |
|
| 434 |
436 |
template <typename Graph>
|
| 435 |
437 |
struct GraphCopySelector<
|
| 436 |
438 |
Graph,
|
| 437 |
439 |
typename enable_if<typename Graph::BuildTag, void>::type>
|
| 438 |
440 |
{
|
| 439 |
441 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
| 440 |
442 |
static void copy(const From& from, Graph &to,
|
| 441 |
443 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 442 |
444 |
to.build(from, nodeRefMap, edgeRefMap);
|
| 443 |
445 |
}
|
| 444 |
446 |
};
|
| 445 |
447 |
|
| 446 |
448 |
}
|
| 447 |
449 |
|
| 448 |
450 |
/// \brief Class to copy a digraph.
|
| 449 |
451 |
///
|
| 450 |
452 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The
|
| 451 |
453 |
/// simplest way of using it is through the \c digraphCopy() function.
|
| 452 |
454 |
///
|
| 453 |
455 |
/// This class not only make a copy of a digraph, but it can create
|
| 454 |
456 |
/// references and cross references between the nodes and arcs of
|
| 455 |
457 |
/// the two digraphs, and it can copy maps to use with the newly created
|
| 456 |
458 |
/// digraph.
|
| 457 |
459 |
///
|
| 458 |
460 |
/// To make a copy from a digraph, first an instance of DigraphCopy
|
| 459 |
461 |
/// should be created, then the data belongs to the digraph should
|
| 460 |
462 |
/// assigned to copy. In the end, the \c run() member should be
|
| 461 |
463 |
/// called.
|
| 462 |
464 |
///
|
| 463 |
465 |
/// The next code copies a digraph with several data:
|
| 464 |
466 |
///\code
|
| 465 |
467 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
|
| 466 |
468 |
/// // Create references for the nodes
|
| 467 |
469 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
| 468 |
470 |
/// cg.nodeRef(nr);
|
| 469 |
471 |
/// // Create cross references (inverse) for the arcs
|
| 470 |
472 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
|
| 471 |
473 |
/// cg.arcCrossRef(acr);
|
| 472 |
474 |
/// // Copy an arc map
|
| 473 |
475 |
/// OrigGraph::ArcMap<double> oamap(orig_graph);
|
| 474 |
476 |
/// NewGraph::ArcMap<double> namap(new_graph);
|
| 475 |
477 |
/// cg.arcMap(oamap, namap);
|
| 476 |
478 |
/// // Copy a node
|
| 477 |
479 |
/// OrigGraph::Node on;
|
| 478 |
480 |
/// NewGraph::Node nn;
|
| 479 |
481 |
/// cg.node(on, nn);
|
| 480 |
482 |
/// // Execute copying
|
| 481 |
483 |
/// cg.run();
|
| 482 |
484 |
///\endcode
|
| 483 |
485 |
template <typename From, typename To>
|
| 484 |
486 |
class DigraphCopy {
|
| 485 |
487 |
private:
|
| 486 |
488 |
|
| 487 |
489 |
typedef typename From::Node Node;
|
| 488 |
490 |
typedef typename From::NodeIt NodeIt;
|
| 489 |
491 |
typedef typename From::Arc Arc;
|
| 490 |
492 |
typedef typename From::ArcIt ArcIt;
|
| 491 |
493 |
|
| 492 |
494 |
typedef typename To::Node TNode;
|
| 493 |
495 |
typedef typename To::Arc TArc;
|
| 494 |
496 |
|
| 495 |
497 |
typedef typename From::template NodeMap<TNode> NodeRefMap;
|
| 496 |
498 |
typedef typename From::template ArcMap<TArc> ArcRefMap;
|
| 497 |
499 |
|
| 498 |
500 |
public:
|
| 499 |
501 |
|
| 500 |
502 |
/// \brief Constructor of DigraphCopy.
|
| 501 |
503 |
///
|
| 502 |
504 |
/// Constructor of DigraphCopy for copying the content of the
|
| 503 |
505 |
/// \c from digraph into the \c to digraph.
|
| 504 |
506 |
DigraphCopy(const From& from, To& to)
|
| 505 |
507 |
: _from(from), _to(to) {}
|
| 506 |
508 |
|
| 507 |
509 |
/// \brief Destructor of DigraphCopy
|
| 508 |
510 |
///
|
| 509 |
511 |
/// Destructor of DigraphCopy.
|
| 510 |
512 |
~DigraphCopy() {
|
| 511 |
513 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 512 |
514 |
delete _node_maps[i];
|
| 513 |
515 |
}
|
| 514 |
516 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 515 |
517 |
delete _arc_maps[i];
|
| 516 |
518 |
}
|
| 517 |
519 |
|
| 518 |
520 |
}
|
| 519 |
521 |
|
| 520 |
522 |
/// \brief Copy the node references into the given map.
|
| 521 |
523 |
///
|
| 522 |
524 |
/// This function copies the node references into the given map.
|
| 523 |
525 |
/// The parameter should be a map, whose key type is the Node type of
|
| 524 |
526 |
/// the source digraph, while the value type is the Node type of the
|
| 525 |
527 |
/// destination digraph.
|
| 526 |
528 |
template <typename NodeRef>
|
| 527 |
529 |
DigraphCopy& nodeRef(NodeRef& map) {
|
| 528 |
530 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node,
|
| 529 |
531 |
NodeRefMap, NodeRef>(map));
|
| 530 |
532 |
return *this;
|
| 531 |
533 |
}
|
| 532 |
534 |
|
| 533 |
535 |
/// \brief Copy the node cross references into the given map.
|
| 534 |
536 |
///
|
| 535 |
537 |
/// This function copies the node cross references (reverse references)
|
| 536 |
538 |
/// into the given map. The parameter should be a map, whose key type
|
| 537 |
539 |
/// is the Node type of the destination digraph, while the value type is
|
| 538 |
540 |
/// the Node type of the source digraph.
|
| 539 |
541 |
template <typename NodeCrossRef>
|
| 540 |
542 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 541 |
543 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
|
| 542 |
544 |
NodeRefMap, NodeCrossRef>(map));
|
| 543 |
545 |
return *this;
|
| 544 |
546 |
}
|
| 545 |
547 |
|
| 546 |
548 |
/// \brief Make a copy of the given node map.
|
| 547 |
549 |
///
|
| 548 |
550 |
/// This function makes a copy of the given node map for the newly
|
| 549 |
551 |
/// created digraph.
|
| 550 |
552 |
/// The key type of the new map \c tmap should be the Node type of the
|
| 551 |
553 |
/// destination digraph, and the key type of the original map \c map
|
| 552 |
554 |
/// should be the Node type of the source digraph.
|
| 553 |
555 |
template <typename FromMap, typename ToMap>
|
| 554 |
556 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 555 |
557 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node,
|
| 556 |
558 |
NodeRefMap, FromMap, ToMap>(map, tmap));
|
| 557 |
559 |
return *this;
|
| 558 |
560 |
}
|
| 559 |
561 |
|
| 560 |
562 |
/// \brief Make a copy of the given node.
|
| 561 |
563 |
///
|
| 562 |
564 |
/// This function makes a copy of the given node.
|
| 563 |
565 |
DigraphCopy& node(const Node& node, TNode& tnode) {
|
| 564 |
566 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node,
|
| 565 |
567 |
NodeRefMap, TNode>(node, tnode));
|
| 566 |
568 |
return *this;
|
| 567 |
569 |
}
|
| 568 |
570 |
|
| 569 |
571 |
/// \brief Copy the arc references into the given map.
|
| 570 |
572 |
///
|
| 571 |
573 |
/// This function copies the arc references into the given map.
|
| 572 |
574 |
/// The parameter should be a map, whose key type is the Arc type of
|
| 573 |
575 |
/// the source digraph, while the value type is the Arc type of the
|
| 574 |
576 |
/// destination digraph.
|
| 575 |
577 |
template <typename ArcRef>
|
| 576 |
578 |
DigraphCopy& arcRef(ArcRef& map) {
|
| 577 |
579 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
|
| 578 |
580 |
ArcRefMap, ArcRef>(map));
|
| 579 |
581 |
return *this;
|
| 580 |
582 |
}
|
| 581 |
583 |
|
| 582 |
584 |
/// \brief Copy the arc cross references into the given map.
|
| 583 |
585 |
///
|
| 584 |
586 |
/// This function copies the arc cross references (reverse references)
|
| 585 |
587 |
/// into the given map. The parameter should be a map, whose key type
|
| 586 |
588 |
/// is the Arc type of the destination digraph, while the value type is
|
| 587 |
589 |
/// the Arc type of the source digraph.
|
| 588 |
590 |
template <typename ArcCrossRef>
|
| 589 |
591 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 590 |
592 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
|
| 591 |
593 |
ArcRefMap, ArcCrossRef>(map));
|
| 592 |
594 |
return *this;
|
| 593 |
595 |
}
|
| 594 |
596 |
|
| 595 |
597 |
/// \brief Make a copy of the given arc map.
|
| 596 |
598 |
///
|
| 597 |
599 |
/// This function makes a copy of the given arc map for the newly
|
| 598 |
600 |
/// created digraph.
|
| 599 |
601 |
/// The key type of the new map \c tmap should be the Arc type of the
|
| 600 |
602 |
/// destination digraph, and the key type of the original map \c map
|
| 601 |
603 |
/// should be the Arc type of the source digraph.
|
| 602 |
604 |
template <typename FromMap, typename ToMap>
|
| 603 |
605 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 604 |
606 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
|
| 605 |
607 |
ArcRefMap, FromMap, ToMap>(map, tmap));
|
| 606 |
608 |
return *this;
|
| 607 |
609 |
}
|
| 608 |
610 |
|
| 609 |
611 |
/// \brief Make a copy of the given arc.
|
| 610 |
612 |
///
|
| 611 |
613 |
/// This function makes a copy of the given arc.
|
| 612 |
614 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 613 |
615 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
|
| 614 |
616 |
ArcRefMap, TArc>(arc, tarc));
|
| 615 |
617 |
return *this;
|
| 616 |
618 |
}
|
| 617 |
619 |
|
| 618 |
620 |
/// \brief Execute copying.
|
| 619 |
621 |
///
|
| 620 |
622 |
/// This function executes the copying of the digraph along with the
|
| 621 |
623 |
/// copying of the assigned data.
|
| 622 |
624 |
void run() {
|
| 623 |
625 |
NodeRefMap nodeRefMap(_from);
|
| 624 |
626 |
ArcRefMap arcRefMap(_from);
|
| 625 |
627 |
_core_bits::DigraphCopySelector<To>::
|
| 626 |
628 |
copy(_from, _to, nodeRefMap, arcRefMap);
|
| 627 |
629 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 628 |
630 |
_node_maps[i]->copy(_from, nodeRefMap);
|
| 629 |
631 |
}
|
| 630 |
632 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 631 |
633 |
_arc_maps[i]->copy(_from, arcRefMap);
|
| 632 |
634 |
}
|
| 633 |
635 |
}
|
| 634 |
636 |
|
| 635 |
637 |
protected:
|
| 636 |
638 |
|
| 637 |
639 |
const From& _from;
|
| 638 |
640 |
To& _to;
|
| 639 |
641 |
|
| 640 |
642 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
|
| 641 |
643 |
_node_maps;
|
| 642 |
644 |
|
| 643 |
645 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
|
| 644 |
646 |
_arc_maps;
|
| 645 |
647 |
|
| 646 |
648 |
};
|
| 647 |
649 |
|
| 648 |
650 |
/// \brief Copy a digraph to another digraph.
|
| 649 |
651 |
///
|
| 650 |
652 |
/// This function copies a digraph to another digraph.
|
| 651 |
653 |
/// The complete usage of it is detailed in the DigraphCopy class, but
|
| 652 |
654 |
/// a short example shows a basic work:
|
| 653 |
655 |
///\code
|
| 654 |
656 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
|
| 655 |
657 |
///\endcode
|
| 656 |
658 |
///
|
| 657 |
659 |
/// After the copy the \c nr map will contain the mapping from the
|
| 658 |
660 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and
|
| 659 |
661 |
/// \c acr will contain the mapping from the arcs of the \c to digraph
|
| 660 |
662 |
/// to the arcs of the \c from digraph.
|
| 661 |
663 |
///
|
| 662 |
664 |
/// \see DigraphCopy
|
| 663 |
665 |
template <typename From, typename To>
|
| 664 |
666 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
|
| 665 |
667 |
return DigraphCopy<From, To>(from, to);
|
| 666 |
668 |
}
|
| 667 |
669 |
|
| 668 |
670 |
/// \brief Class to copy a graph.
|
| 669 |
671 |
///
|
| 670 |
672 |
/// Class to copy a graph to another graph (duplicate a graph). The
|
| 671 |
673 |
/// simplest way of using it is through the \c graphCopy() function.
|
| 672 |
674 |
///
|
| 673 |
675 |
/// This class not only make a copy of a graph, but it can create
|
| 674 |
676 |
/// references and cross references between the nodes, edges and arcs of
|
| 675 |
677 |
/// the two graphs, and it can copy maps for using with the newly created
|
| 676 |
678 |
/// graph.
|
| 677 |
679 |
///
|
| 678 |
680 |
/// To make a copy from a graph, first an instance of GraphCopy
|
| 679 |
681 |
/// should be created, then the data belongs to the graph should
|
| 680 |
682 |
/// assigned to copy. In the end, the \c run() member should be
|
| 681 |
683 |
/// called.
|
| 682 |
684 |
///
|
| 683 |
685 |
/// The next code copies a graph with several data:
|
| 684 |
686 |
///\code
|
| 685 |
687 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
|
| 686 |
688 |
/// // Create references for the nodes
|
| 687 |
689 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
| 688 |
690 |
/// cg.nodeRef(nr);
|
| 689 |
691 |
/// // Create cross references (inverse) for the edges
|
| 690 |
692 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
|
| 691 |
693 |
/// cg.edgeCrossRef(ecr);
|
| 692 |
694 |
/// // Copy an edge map
|
| 693 |
695 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph);
|
| 694 |
696 |
/// NewGraph::EdgeMap<double> nemap(new_graph);
|
| 695 |
697 |
/// cg.edgeMap(oemap, nemap);
|
| 696 |
698 |
/// // Copy a node
|
| 697 |
699 |
/// OrigGraph::Node on;
|
| 698 |
700 |
/// NewGraph::Node nn;
|
| 699 |
701 |
/// cg.node(on, nn);
|
| 700 |
702 |
/// // Execute copying
|
| 701 |
703 |
/// cg.run();
|
| 702 |
704 |
///\endcode
|
| 703 |
705 |
template <typename From, typename To>
|
| 704 |
706 |
class GraphCopy {
|
| 705 |
707 |
private:
|
| 706 |
708 |
|
| 707 |
709 |
typedef typename From::Node Node;
|
| 708 |
710 |
typedef typename From::NodeIt NodeIt;
|
| 709 |
711 |
typedef typename From::Arc Arc;
|
| 710 |
712 |
typedef typename From::ArcIt ArcIt;
|
| 711 |
713 |
typedef typename From::Edge Edge;
|
| 712 |
714 |
typedef typename From::EdgeIt EdgeIt;
|
| 713 |
715 |
|
| 714 |
716 |
typedef typename To::Node TNode;
|
| 715 |
717 |
typedef typename To::Arc TArc;
|
| 716 |
718 |
typedef typename To::Edge TEdge;
|
| 717 |
719 |
|
| 718 |
720 |
typedef typename From::template NodeMap<TNode> NodeRefMap;
|
| 719 |
721 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
|
| 720 |
722 |
|
| 721 |
723 |
struct ArcRefMap {
|
| 722 |
724 |
ArcRefMap(const From& from, const To& to,
|
| 723 |
725 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
|
| 724 |
726 |
: _from(from), _to(to),
|
| 725 |
727 |
_edge_ref(edge_ref), _node_ref(node_ref) {}
|
| 726 |
728 |
|
| 727 |
729 |
typedef typename From::Arc Key;
|
| 728 |
730 |
typedef typename To::Arc Value;
|
| 729 |
731 |
|
| 730 |
732 |
Value operator[](const Key& key) const {
|
| 731 |
733 |
bool forward = _from.u(key) != _from.v(key) ?
|
| 732 |
734 |
_node_ref[_from.source(key)] ==
|
| 733 |
735 |
_to.source(_to.direct(_edge_ref[key], true)) :
|
| 734 |
736 |
_from.direction(key);
|
| 735 |
737 |
return _to.direct(_edge_ref[key], forward);
|
| 736 |
738 |
}
|
| 737 |
739 |
|
| 738 |
740 |
const From& _from;
|
| 739 |
741 |
const To& _to;
|
| 740 |
742 |
const EdgeRefMap& _edge_ref;
|
| 741 |
743 |
const NodeRefMap& _node_ref;
|
| 742 |
744 |
};
|
| 743 |
745 |
|
| 744 |
746 |
public:
|
| 745 |
747 |
|
| 746 |
748 |
/// \brief Constructor of GraphCopy.
|
| 747 |
749 |
///
|
| 748 |
750 |
/// Constructor of GraphCopy for copying the content of the
|
| 749 |
751 |
/// \c from graph into the \c to graph.
|
| 750 |
752 |
GraphCopy(const From& from, To& to)
|
| 751 |
753 |
: _from(from), _to(to) {}
|
| 752 |
754 |
|
| 753 |
755 |
/// \brief Destructor of GraphCopy
|
| 754 |
756 |
///
|
| 755 |
757 |
/// Destructor of GraphCopy.
|
| 756 |
758 |
~GraphCopy() {
|
| 757 |
759 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 758 |
760 |
delete _node_maps[i];
|
| 759 |
761 |
}
|
| 760 |
762 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 761 |
763 |
delete _arc_maps[i];
|
| 762 |
764 |
}
|
| 763 |
765 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 764 |
766 |
delete _edge_maps[i];
|
| 765 |
767 |
}
|
| 766 |
768 |
}
|
| 767 |
769 |
|
| 768 |
770 |
/// \brief Copy the node references into the given map.
|
| 769 |
771 |
///
|
| 770 |
772 |
/// This function copies the node references into the given map.
|
| 771 |
773 |
/// The parameter should be a map, whose key type is the Node type of
|
| 772 |
774 |
/// the source graph, while the value type is the Node type of the
|
| 773 |
775 |
/// destination graph.
|
| 774 |
776 |
template <typename NodeRef>
|
| 775 |
777 |
GraphCopy& nodeRef(NodeRef& map) {
|
| 776 |
778 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node,
|
| 777 |
779 |
NodeRefMap, NodeRef>(map));
|
| 778 |
780 |
return *this;
|
| 779 |
781 |
}
|
| 780 |
782 |
|
| 781 |
783 |
/// \brief Copy the node cross references into the given map.
|
| 782 |
784 |
///
|
| 783 |
785 |
/// This function copies the node cross references (reverse references)
|
| 784 |
786 |
/// into the given map. The parameter should be a map, whose key type
|
| 785 |
787 |
/// is the Node type of the destination graph, while the value type is
|
| 786 |
788 |
/// the Node type of the source graph.
|
| 787 |
789 |
template <typename NodeCrossRef>
|
| 788 |
790 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 789 |
791 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
|
| 790 |
792 |
NodeRefMap, NodeCrossRef>(map));
|
| 791 |
793 |
return *this;
|
| 792 |
794 |
}
|
| 793 |
795 |
|
| 794 |
796 |
/// \brief Make a copy of the given node map.
|
| 795 |
797 |
///
|
| 796 |
798 |
/// This function makes a copy of the given node map for the newly
|
| 797 |
799 |
/// created graph.
|
| 798 |
800 |
/// The key type of the new map \c tmap should be the Node type of the
|
| 799 |
801 |
/// destination graph, and the key type of the original map \c map
|
| 800 |
802 |
/// should be the Node type of the source graph.
|
| 801 |
803 |
template <typename FromMap, typename ToMap>
|
| 802 |
804 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 803 |
805 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node,
|
| 804 |
806 |
NodeRefMap, FromMap, ToMap>(map, tmap));
|
| 805 |
807 |
return *this;
|
| 806 |
808 |
}
|
| 807 |
809 |
|
| 808 |
810 |
/// \brief Make a copy of the given node.
|
| 809 |
811 |
///
|
| 810 |
812 |
/// This function makes a copy of the given node.
|
| 811 |
813 |
GraphCopy& node(const Node& node, TNode& tnode) {
|
| 812 |
814 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node,
|
| 813 |
815 |
NodeRefMap, TNode>(node, tnode));
|
| 814 |
816 |
return *this;
|
| 815 |
817 |
}
|
| 816 |
818 |
|
| 817 |
819 |
/// \brief Copy the arc references into the given map.
|
| 818 |
820 |
///
|
| 819 |
821 |
/// This function copies the arc references into the given map.
|
| 820 |
822 |
/// The parameter should be a map, whose key type is the Arc type of
|
| 821 |
823 |
/// the source graph, while the value type is the Arc type of the
|
| 822 |
824 |
/// destination graph.
|
| 823 |
825 |
template <typename ArcRef>
|
| 824 |
826 |
GraphCopy& arcRef(ArcRef& map) {
|
| 825 |
827 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
|
| 826 |
828 |
ArcRefMap, ArcRef>(map));
|
| 827 |
829 |
return *this;
|
| 828 |
830 |
}
|
| 829 |
831 |
|
| 830 |
832 |
/// \brief Copy the arc cross references into the given map.
|
| 831 |
833 |
///
|
| 832 |
834 |
/// This function copies the arc cross references (reverse references)
|
| 833 |
835 |
/// into the given map. The parameter should be a map, whose key type
|
| 834 |
836 |
/// is the Arc type of the destination graph, while the value type is
|
| 835 |
837 |
/// the Arc type of the source graph.
|
| 836 |
838 |
template <typename ArcCrossRef>
|
| 837 |
839 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 838 |
840 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
|
| 839 |
841 |
ArcRefMap, ArcCrossRef>(map));
|
| 840 |
842 |
return *this;
|
| 841 |
843 |
}
|
| 842 |
844 |
|
| 843 |
845 |
/// \brief Make a copy of the given arc map.
|
| 844 |
846 |
///
|
| 845 |
847 |
/// This function makes a copy of the given arc map for the newly
|
| 846 |
848 |
/// created graph.
|
| 847 |
849 |
/// The key type of the new map \c tmap should be the Arc type of the
|
| 848 |
850 |
/// destination graph, and the key type of the original map \c map
|
| 849 |
851 |
/// should be the Arc type of the source graph.
|
| 850 |
852 |
template <typename FromMap, typename ToMap>
|
| 851 |
853 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 852 |
854 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
|
| 853 |
855 |
ArcRefMap, FromMap, ToMap>(map, tmap));
|
| 854 |
856 |
return *this;
|
| 855 |
857 |
}
|
| 856 |
858 |
|
| 857 |
859 |
/// \brief Make a copy of the given arc.
|
| 858 |
860 |
///
|
| 859 |
861 |
/// This function makes a copy of the given arc.
|
| 860 |
862 |
GraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 861 |
863 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
|
| 862 |
864 |
ArcRefMap, TArc>(arc, tarc));
|
| 863 |
865 |
return *this;
|
| 864 |
866 |
}
|
| 865 |
867 |
|
| 866 |
868 |
/// \brief Copy the edge references into the given map.
|
| 867 |
869 |
///
|
| 868 |
870 |
/// This function copies the edge references into the given map.
|
| 869 |
871 |
/// The parameter should be a map, whose key type is the Edge type of
|
| 870 |
872 |
/// the source graph, while the value type is the Edge type of the
|
| 871 |
873 |
/// destination graph.
|
| 872 |
874 |
template <typename EdgeRef>
|
| 873 |
875 |
GraphCopy& edgeRef(EdgeRef& map) {
|
| 874 |
876 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
|
| 875 |
877 |
EdgeRefMap, EdgeRef>(map));
|
| 876 |
878 |
return *this;
|
| 877 |
879 |
}
|
| 878 |
880 |
|
| 879 |
881 |
/// \brief Copy the edge cross references into the given map.
|
| 880 |
882 |
///
|
| 881 |
883 |
/// This function copies the edge cross references (reverse references)
|
| 882 |
884 |
/// into the given map. The parameter should be a map, whose key type
|
| 883 |
885 |
/// is the Edge type of the destination graph, while the value type is
|
| 884 |
886 |
/// the Edge type of the source graph.
|
| 885 |
887 |
template <typename EdgeCrossRef>
|
| 886 |
888 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
| 887 |
889 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From,
|
| 888 |
890 |
Edge, EdgeRefMap, EdgeCrossRef>(map));
|
| 889 |
891 |
return *this;
|
| 890 |
892 |
}
|
| 891 |
893 |
|
| 892 |
894 |
/// \brief Make a copy of the given edge map.
|
| 893 |
895 |
///
|
| 894 |
896 |
/// This function makes a copy of the given edge map for the newly
|
| 895 |
897 |
/// created graph.
|
| 896 |
898 |
/// The key type of the new map \c tmap should be the Edge type of the
|
| 897 |
899 |
/// destination graph, and the key type of the original map \c map
|
| 898 |
900 |
/// should be the Edge type of the source graph.
|
| 899 |
901 |
template <typename FromMap, typename ToMap>
|
| 900 |
902 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
|
| 901 |
903 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
|
| 902 |
904 |
EdgeRefMap, FromMap, ToMap>(map, tmap));
|
| 903 |
905 |
return *this;
|
| 904 |
906 |
}
|
| 905 |
907 |
|
| 906 |
908 |
/// \brief Make a copy of the given edge.
|
| 907 |
909 |
///
|
| 908 |
910 |
/// This function makes a copy of the given edge.
|
| 909 |
911 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) {
|
| 910 |
912 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
|
| 911 |
913 |
EdgeRefMap, TEdge>(edge, tedge));
|
| 912 |
914 |
return *this;
|
| 913 |
915 |
}
|
| 914 |
916 |
|
| 915 |
917 |
/// \brief Execute copying.
|
| 916 |
918 |
///
|
| 917 |
919 |
/// This function executes the copying of the graph along with the
|
| 918 |
920 |
/// copying of the assigned data.
|
| 919 |
921 |
void run() {
|
| 920 |
922 |
NodeRefMap nodeRefMap(_from);
|
| 921 |
923 |
EdgeRefMap edgeRefMap(_from);
|
| 922 |
924 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
|
| 923 |
925 |
_core_bits::GraphCopySelector<To>::
|
| 924 |
926 |
copy(_from, _to, nodeRefMap, edgeRefMap);
|
| 925 |
927 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 926 |
928 |
_node_maps[i]->copy(_from, nodeRefMap);
|
| 927 |
929 |
}
|
| 928 |
930 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 929 |
931 |
_edge_maps[i]->copy(_from, edgeRefMap);
|
| 930 |
932 |
}
|
| 931 |
933 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 932 |
934 |
_arc_maps[i]->copy(_from, arcRefMap);
|
| 933 |
935 |
}
|
| 934 |
936 |
}
|
| 935 |
937 |
|
| 936 |
938 |
private:
|
| 937 |
939 |
|
| 938 |
940 |
const From& _from;
|
| 939 |
941 |
To& _to;
|
| 940 |
942 |
|
| 941 |
943 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
|
| 942 |
944 |
_node_maps;
|
| 943 |
945 |
|
| 944 |
946 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
|
| 945 |
947 |
_arc_maps;
|
| 946 |
948 |
|
| 947 |
949 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
|
| 948 |
950 |
_edge_maps;
|
| 949 |
951 |
|
| 950 |
952 |
};
|
| 951 |
953 |
|
| 952 |
954 |
/// \brief Copy a graph to another graph.
|
| 953 |
955 |
///
|
| 954 |
956 |
/// This function copies a graph to another graph.
|
| 955 |
957 |
/// The complete usage of it is detailed in the GraphCopy class,
|
| 956 |
958 |
/// but a short example shows a basic work:
|
| 957 |
959 |
///\code
|
| 958 |
960 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
|
| 959 |
961 |
///\endcode
|
| 960 |
962 |
///
|
| 961 |
963 |
/// After the copy the \c nr map will contain the mapping from the
|
| 962 |
964 |
/// nodes of the \c from graph to the nodes of the \c to graph and
|
| 963 |
965 |
/// \c ecr will contain the mapping from the edges of the \c to graph
|
| 964 |
966 |
/// to the edges of the \c from graph.
|
| 965 |
967 |
///
|
| 966 |
968 |
/// \see GraphCopy
|
| 967 |
969 |
template <typename From, typename To>
|
| 968 |
970 |
GraphCopy<From, To>
|
| 969 |
971 |
graphCopy(const From& from, To& to) {
|
| 970 |
972 |
return GraphCopy<From, To>(from, to);
|
| 971 |
973 |
}
|
| 972 |
974 |
|
| 973 |
975 |
namespace _core_bits {
|
| 974 |
976 |
|
| 975 |
977 |
template <typename Graph, typename Enable = void>
|
| 976 |
978 |
struct FindArcSelector {
|
| 977 |
979 |
typedef typename Graph::Node Node;
|
| 978 |
980 |
typedef typename Graph::Arc Arc;
|
| 979 |
981 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 980 |
982 |
if (e == INVALID) {
|
| 981 |
983 |
g.firstOut(e, u);
|
| 982 |
984 |
} else {
|
| 983 |
985 |
g.nextOut(e);
|
| 984 |
986 |
}
|
| 985 |
987 |
while (e != INVALID && g.target(e) != v) {
|
| 986 |
988 |
g.nextOut(e);
|
| 987 |
989 |
}
|
| 988 |
990 |
return e;
|
| 989 |
991 |
}
|
| 990 |
992 |
};
|
| 991 |
993 |
|
| 992 |
994 |
template <typename Graph>
|
| 993 |
995 |
struct FindArcSelector<
|
| 994 |
996 |
Graph,
|
| 995 |
997 |
typename enable_if<typename Graph::FindArcTag, void>::type>
|
| 996 |
998 |
{
|
| 997 |
999 |
typedef typename Graph::Node Node;
|
| 998 |
1000 |
typedef typename Graph::Arc Arc;
|
| 999 |
1001 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 1000 |
1002 |
return g.findArc(u, v, prev);
|
| 1001 |
1003 |
}
|
| 1002 |
1004 |
};
|
| 1003 |
1005 |
}
|
| 1004 |
1006 |
|
| 1005 |
1007 |
/// \brief Find an arc between two nodes of a digraph.
|
| 1006 |
1008 |
///
|
| 1007 |
1009 |
/// This function finds an arc from node \c u to node \c v in the
|
| 1008 |
1010 |
/// digraph \c g.
|
| 1009 |
1011 |
///
|
| 1010 |
1012 |
/// If \c prev is \ref INVALID (this is the default value), then
|
| 1011 |
1013 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for
|
| 1012 |
1014 |
/// the next arc from \c u to \c v after \c prev.
|
| 1013 |
1015 |
/// \return The found arc or \ref INVALID if there is no such an arc.
|
| 1014 |
1016 |
///
|
| 1015 |
1017 |
/// Thus you can iterate through each arc from \c u to \c v as it follows.
|
| 1016 |
1018 |
///\code
|
| 1017 |
1019 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
|
| 1018 |
1020 |
/// ...
|
| 1019 |
1021 |
/// }
|
| 1020 |
1022 |
///\endcode
|
| 1021 |
1023 |
///
|
| 1022 |
1024 |
/// \note \ref ConArcIt provides iterator interface for the same
|
| 1023 |
1025 |
/// functionality.
|
| 1024 |
1026 |
///
|
| 1025 |
1027 |
///\sa ConArcIt
|
| 1026 |
1028 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
|
| 1027 |
1029 |
template <typename Graph>
|
| 1028 |
1030 |
inline typename Graph::Arc
|
| 1029 |
1031 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
| 1030 |
1032 |
typename Graph::Arc prev = INVALID) {
|
| 1031 |
1033 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
|
| 1032 |
1034 |
}
|
| 1033 |
1035 |
|
| 1034 |
1036 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes.
|
| 1035 |
1037 |
///
|
| 1036 |
1038 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is
|
| 1037 |
1039 |
/// a higher level interface for the \ref findArc() function. You can
|
| 1038 |
1040 |
/// use it the following way:
|
| 1039 |
1041 |
///\code
|
| 1040 |
1042 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 1041 |
1043 |
/// ...
|
| 1042 |
1044 |
/// }
|
| 1043 |
1045 |
///\endcode
|
| 1044 |
1046 |
///
|
| 1045 |
1047 |
///\sa findArc()
|
| 1046 |
1048 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
|
| 1047 |
1049 |
template <typename GR>
|
| 1048 |
1050 |
class ConArcIt : public GR::Arc {
|
| 1049 |
1051 |
typedef typename GR::Arc Parent;
|
| 1050 |
1052 |
|
| 1051 |
1053 |
public:
|
| 1052 |
1054 |
|
| 1053 |
1055 |
typedef typename GR::Arc Arc;
|
| 1054 |
1056 |
typedef typename GR::Node Node;
|
| 1055 |
1057 |
|
| 1056 |
1058 |
/// \brief Constructor.
|
| 1057 |
1059 |
///
|
| 1058 |
1060 |
/// Construct a new ConArcIt iterating on the arcs that
|
| 1059 |
1061 |
/// connects nodes \c u and \c v.
|
| 1060 |
1062 |
ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
|
| 1061 |
1063 |
Parent::operator=(findArc(_graph, u, v));
|
| 1062 |
1064 |
}
|
| 1063 |
1065 |
|
| 1064 |
1066 |
/// \brief Constructor.
|
| 1065 |
1067 |
///
|
| 1066 |
1068 |
/// Construct a new ConArcIt that continues the iterating from arc \c a.
|
| 1067 |
1069 |
ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
|
| 1068 |
1070 |
|
| 1069 |
1071 |
/// \brief Increment operator.
|
| 1070 |
1072 |
///
|
| 1071 |
1073 |
/// It increments the iterator and gives back the next arc.
|
| 1072 |
1074 |
ConArcIt& operator++() {
|
| 1073 |
1075 |
Parent::operator=(findArc(_graph, _graph.source(*this),
|
| 1074 |
1076 |
_graph.target(*this), *this));
|
| 1075 |
1077 |
return *this;
|
| 1076 |
1078 |
}
|
| 1077 |
1079 |
private:
|
| 1078 |
1080 |
const GR& _graph;
|
| 1079 |
1081 |
};
|
| 1080 |
1082 |
|
| 1081 |
1083 |
namespace _core_bits {
|
| 1082 |
1084 |
|
| 1083 |
1085 |
template <typename Graph, typename Enable = void>
|
| 1084 |
1086 |
struct FindEdgeSelector {
|
| 1085 |
1087 |
typedef typename Graph::Node Node;
|
| 1086 |
1088 |
typedef typename Graph::Edge Edge;
|
| 1087 |
1089 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 1088 |
1090 |
bool b;
|
| 1089 |
1091 |
if (u != v) {
|
| 1090 |
1092 |
if (e == INVALID) {
|
| 1091 |
1093 |
g.firstInc(e, b, u);
|
| 1092 |
1094 |
} else {
|
| 1093 |
1095 |
b = g.u(e) == u;
|
| 1094 |
1096 |
g.nextInc(e, b);
|
| 1095 |
1097 |
}
|
| 1096 |
1098 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
|
| 1097 |
1099 |
g.nextInc(e, b);
|
| 1098 |
1100 |
}
|
| 1099 |
1101 |
} else {
|
| 1100 |
1102 |
if (e == INVALID) {
|
| 1101 |
1103 |
g.firstInc(e, b, u);
|
| 1102 |
1104 |
} else {
|
| 1103 |
1105 |
b = true;
|
| 1104 |
1106 |
g.nextInc(e, b);
|
| 1105 |
1107 |
}
|
| 1106 |
1108 |
while (e != INVALID && (!b || g.v(e) != v)) {
|
| 1107 |
1109 |
g.nextInc(e, b);
|
| 1108 |
1110 |
}
|
| 1109 |
1111 |
}
|
| 1110 |
1112 |
return e;
|
| 1111 |
1113 |
}
|
| 1112 |
1114 |
};
|
| 1113 |
1115 |
|
| 1114 |
1116 |
template <typename Graph>
|
| 1115 |
1117 |
struct FindEdgeSelector<
|
| 1116 |
1118 |
Graph,
|
| 1117 |
1119 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
| 1118 |
1120 |
{
|
| 1119 |
1121 |
typedef typename Graph::Node Node;
|
| 1120 |
1122 |
typedef typename Graph::Edge Edge;
|
| 1121 |
1123 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 1122 |
1124 |
return g.findEdge(u, v, prev);
|
| 1123 |
1125 |
}
|
| 1124 |
1126 |
};
|
| 1125 |
1127 |
}
|
| 1126 |
1128 |
|
| 1127 |
1129 |
/// \brief Find an edge between two nodes of a graph.
|
| 1128 |
1130 |
///
|
| 1129 |
1131 |
/// This function finds an edge from node \c u to node \c v in graph \c g.
|
| 1130 |
1132 |
/// If node \c u and node \c v is equal then each loop edge
|
| 1131 |
1133 |
/// will be enumerated once.
|
| 1132 |
1134 |
///
|
| 1133 |
1135 |
/// If \c prev is \ref INVALID (this is the default value), then
|
| 1134 |
1136 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
| 1135 |
1137 |
/// the next edge from \c u to \c v after \c prev.
|
| 1136 |
1138 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
| 1137 |
1139 |
///
|
| 1138 |
1140 |
/// Thus you can iterate through each edge between \c u and \c v
|
| 1139 |
1141 |
/// as it follows.
|
| 1140 |
1142 |
///\code
|
| 1141 |
1143 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
|
| 1142 |
1144 |
/// ...
|
| 1143 |
1145 |
/// }
|
| 1144 |
1146 |
///\endcode
|
| 1145 |
1147 |
///
|
| 1146 |
1148 |
/// \note \ref ConEdgeIt provides iterator interface for the same
|
| 1147 |
1149 |
/// functionality.
|
| 1148 |
1150 |
///
|
| 1149 |
1151 |
///\sa ConEdgeIt
|
| 1150 |
1152 |
template <typename Graph>
|
| 1151 |
1153 |
inline typename Graph::Edge
|
| 1152 |
1154 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
| 1153 |
1155 |
typename Graph::Edge p = INVALID) {
|
| 1154 |
1156 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
|
| 1155 |
1157 |
}
|
| 1156 |
1158 |
|
| 1157 |
1159 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes.
|
| 1158 |
1160 |
///
|
| 1159 |
1161 |
/// Iterator for iterating on parallel edges connecting the same nodes.
|
| 1160 |
1162 |
/// It is a higher level interface for the findEdge() function. You can
|
| 1161 |
1163 |
/// use it the following way:
|
| 1162 |
1164 |
///\code
|
| 1163 |
1165 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
|
| 1164 |
1166 |
/// ...
|
| 1165 |
1167 |
/// }
|
| 1166 |
1168 |
///\endcode
|
| 1167 |
1169 |
///
|
| 1168 |
1170 |
///\sa findEdge()
|
| 1169 |
1171 |
template <typename GR>
|
| 1170 |
1172 |
class ConEdgeIt : public GR::Edge {
|
| 1171 |
1173 |
typedef typename GR::Edge Parent;
|
| 1172 |
1174 |
|
| 1173 |
1175 |
public:
|
| 1174 |
1176 |
|
| 1175 |
1177 |
typedef typename GR::Edge Edge;
|
| 1176 |
1178 |
typedef typename GR::Node Node;
|
| 1177 |
1179 |
|
| 1178 |
1180 |
/// \brief Constructor.
|
| 1179 |
1181 |
///
|
| 1180 |
1182 |
/// Construct a new ConEdgeIt iterating on the edges that
|
| 1181 |
1183 |
/// connects nodes \c u and \c v.
|
| 1182 |
1184 |
ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
|
| 1183 |
1185 |
Parent::operator=(findEdge(_graph, _u, _v));
|
| 1184 |
1186 |
}
|
| 1185 |
1187 |
|
| 1186 |
1188 |
/// \brief Constructor.
|
| 1187 |
1189 |
///
|
| 1188 |
1190 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e.
|
| 1189 |
1191 |
ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
|
| 1190 |
1192 |
|
| 1191 |
1193 |
/// \brief Increment operator.
|
| 1192 |
1194 |
///
|
| 1193 |
1195 |
/// It increments the iterator and gives back the next edge.
|
| 1194 |
1196 |
ConEdgeIt& operator++() {
|
| 1195 |
1197 |
Parent::operator=(findEdge(_graph, _u, _v, *this));
|
| 1196 |
1198 |
return *this;
|
| 1197 |
1199 |
}
|
| 1198 |
1200 |
private:
|
| 1199 |
1201 |
const GR& _graph;
|
| 1200 |
1202 |
Node _u, _v;
|
| 1201 |
1203 |
};
|
| 1202 |
1204 |
|
| 1203 |
1205 |
|
| 1204 |
1206 |
///Dynamic arc look-up between given endpoints.
|
| 1205 |
1207 |
|
| 1206 |
1208 |
///Using this class, you can find an arc in a digraph from a given
|
| 1207 |
1209 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>),
|
| 1208 |
1210 |
///where <em>d</em> is the out-degree of the source node.
|
| 1209 |
1211 |
///
|
| 1210 |
1212 |
///It is possible to find \e all parallel arcs between two nodes with
|
| 1211 |
1213 |
///the \c operator() member.
|
| 1212 |
1214 |
///
|
| 1213 |
1215 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or
|
| 1214 |
1216 |
///\ref AllArcLookUp if your digraph is not changed so frequently.
|
| 1215 |
1217 |
///
|
| 1216 |
1218 |
///This class uses a self-adjusting binary search tree, the Splay tree
|
| 1217 |
1219 |
///of Sleator and Tarjan to guarantee the logarithmic amortized
|
| 1218 |
1220 |
///time bound for arc look-ups. This class also guarantees the
|
| 1219 |
1221 |
///optimal time bound in a constant factor for any distribution of
|
| 1220 |
1222 |
///queries.
|
| 1221 |
1223 |
///
|
| 1222 |
1224 |
///\tparam GR The type of the underlying digraph.
|
| 1223 |
1225 |
///
|
| 1224 |
1226 |
///\sa ArcLookUp
|
| 1225 |
1227 |
///\sa AllArcLookUp
|
| 1226 |
1228 |
template <typename GR>
|
| 1227 |
1229 |
class DynArcLookUp
|
| 1228 |
1230 |
: protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
|
| 1229 |
1231 |
{
|
| 1230 |
1232 |
typedef typename ItemSetTraits<GR, typename GR::Arc>
|
| 1231 |
1233 |
::ItemNotifier::ObserverBase Parent;
|
| 1232 |
1234 |
|
| 1233 |
1235 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
| 1234 |
1236 |
|
| 1235 |
1237 |
public:
|
| 1236 |
1238 |
|
| 1237 |
1239 |
/// The Digraph type
|
| 1238 |
1240 |
typedef GR Digraph;
|
| 1239 |
1241 |
|
| 1240 |
1242 |
protected:
|
| 1241 |
1243 |
|
| 1242 |
1244 |
class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
|
| 1243 |
1245 |
typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
|
| 1244 |
1246 |
|
| 1245 |
1247 |
public:
|
| 1246 |
1248 |
|
| 1247 |
1249 |
AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
|
| 1248 |
1250 |
|
| 1249 |
1251 |
virtual void add(const Node& node) {
|
| 1250 |
1252 |
Parent::add(node);
|
| 1251 |
1253 |
Parent::set(node, INVALID);
|
| 1252 |
1254 |
}
|
| 1253 |
1255 |
|
| 1254 |
1256 |
virtual void add(const std::vector<Node>& nodes) {
|
| 1255 |
1257 |
Parent::add(nodes);
|
| 1256 |
1258 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1257 |
1259 |
Parent::set(nodes[i], INVALID);
|
| 1258 |
1260 |
}
|
| 1259 |
1261 |
}
|
| 1260 |
1262 |
|
| 1261 |
1263 |
virtual void build() {
|
| 1262 |
1264 |
Parent::build();
|
| 1263 |
1265 |
Node it;
|
| 1264 |
1266 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 1265 |
1267 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1266 |
1268 |
Parent::set(it, INVALID);
|
| 1267 |
1269 |
}
|
| 1268 |
1270 |
}
|
| 1269 |
1271 |
};
|
| 1270 |
1272 |
|
| 1271 |
1273 |
class ArcLess {
|
| 1272 |
1274 |
const Digraph &g;
|
| 1273 |
1275 |
public:
|
| 1274 |
1276 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1275 |
1277 |
bool operator()(Arc a,Arc b) const
|
| 1276 |
1278 |
{
|
| 1277 |
1279 |
return g.target(a)<g.target(b);
|
| 1278 |
1280 |
}
|
| 1279 |
1281 |
};
|
| 1280 |
1282 |
|
| 1281 |
1283 |
protected:
|
| 1282 |
1284 |
|
| 1283 |
1285 |
const Digraph &_g;
|
| 1284 |
1286 |
AutoNodeMap _head;
|
| 1285 |
1287 |
typename Digraph::template ArcMap<Arc> _parent;
|
| 1286 |
1288 |
typename Digraph::template ArcMap<Arc> _left;
|
| 1287 |
1289 |
typename Digraph::template ArcMap<Arc> _right;
|
| 1288 |
1290 |
|
| 1289 |
1291 |
public:
|
| 1290 |
1292 |
|
| 1291 |
1293 |
///Constructor
|
| 1292 |
1294 |
|
| 1293 |
1295 |
///Constructor.
|
| 1294 |
1296 |
///
|
| 1295 |
1297 |
///It builds up the search database.
|
| 1296 |
1298 |
DynArcLookUp(const Digraph &g)
|
| 1297 |
1299 |
: _g(g),_head(g),_parent(g),_left(g),_right(g)
|
| 1298 |
1300 |
{
|
| 1299 |
1301 |
Parent::attach(_g.notifier(typename Digraph::Arc()));
|
| 1300 |
1302 |
refresh();
|
| 1301 |
1303 |
}
|
| 1302 |
1304 |
|
| 1303 |
1305 |
protected:
|
| 1304 |
1306 |
|
| 1305 |
1307 |
virtual void add(const Arc& arc) {
|
| 1306 |
1308 |
insert(arc);
|
| 1307 |
1309 |
}
|
| 1308 |
1310 |
|
| 1309 |
1311 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1310 |
1312 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1311 |
1313 |
insert(arcs[i]);
|
| 1312 |
1314 |
}
|
| 1313 |
1315 |
}
|
| 1314 |
1316 |
|
| 1315 |
1317 |
virtual void erase(const Arc& arc) {
|
| 1316 |
1318 |
remove(arc);
|
| 1317 |
1319 |
}
|
| 1318 |
1320 |
|
| 1319 |
1321 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1320 |
1322 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1321 |
1323 |
remove(arcs[i]);
|
| 1322 |
1324 |
}
|
| 1323 |
1325 |
}
|
| 1324 |
1326 |
|
| 1325 |
1327 |
virtual void build() {
|
| 1326 |
1328 |
refresh();
|
| 1327 |
1329 |
}
|
| 1328 |
1330 |
|
| 1329 |
1331 |
virtual void clear() {
|
| 1330 |
1332 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1331 |
1333 |
_head[n] = INVALID;
|
| 1332 |
1334 |
}
|
| 1333 |
1335 |
}
|
| 1334 |
1336 |
|
| 1335 |
1337 |
void insert(Arc arc) {
|
| 1336 |
1338 |
Node s = _g.source(arc);
|
| 1337 |
1339 |
Node t = _g.target(arc);
|
| 1338 |
1340 |
_left[arc] = INVALID;
|
| 1339 |
1341 |
_right[arc] = INVALID;
|
| 1340 |
1342 |
|
| 1341 |
1343 |
Arc e = _head[s];
|
| 1342 |
1344 |
if (e == INVALID) {
|
| 1343 |
1345 |
_head[s] = arc;
|
| 1344 |
1346 |
_parent[arc] = INVALID;
|
| 1345 |
1347 |
return;
|
| 1346 |
1348 |
}
|
| 1347 |
1349 |
while (true) {
|
| 1348 |
1350 |
if (t < _g.target(e)) {
|
| 1349 |
1351 |
if (_left[e] == INVALID) {
|
| 1350 |
1352 |
_left[e] = arc;
|
| 1351 |
1353 |
_parent[arc] = e;
|
| 1352 |
1354 |
splay(arc);
|
| 1353 |
1355 |
return;
|
| 1354 |
1356 |
} else {
|
| 1355 |
1357 |
e = _left[e];
|
| 1356 |
1358 |
}
|
| 1357 |
1359 |
} else {
|
| 1358 |
1360 |
if (_right[e] == INVALID) {
|
| 1359 |
1361 |
_right[e] = arc;
|
| 1360 |
1362 |
_parent[arc] = e;
|
| 1361 |
1363 |
splay(arc);
|
| 1362 |
1364 |
return;
|
| 1363 |
1365 |
} else {
|
| 1364 |
1366 |
e = _right[e];
|
| 1365 |
1367 |
}
|
| 1366 |
1368 |
}
|
| 1367 |
1369 |
}
|
| 1368 |
1370 |
}
|
| 1369 |
1371 |
|
| 1370 |
1372 |
void remove(Arc arc) {
|
| 1371 |
1373 |
if (_left[arc] == INVALID) {
|
| 1372 |
1374 |
if (_right[arc] != INVALID) {
|
| 1373 |
1375 |
_parent[_right[arc]] = _parent[arc];
|
| 1374 |
1376 |
}
|
| 1375 |
1377 |
if (_parent[arc] != INVALID) {
|
| 1376 |
1378 |
if (_left[_parent[arc]] == arc) {
|
| 1377 |
1379 |
_left[_parent[arc]] = _right[arc];
|
| 1378 |
1380 |
} else {
|
| 1379 |
1381 |
_right[_parent[arc]] = _right[arc];
|
| 1380 |
1382 |
}
|
| 1381 |
1383 |
} else {
|
| 1382 |
1384 |
_head[_g.source(arc)] = _right[arc];
|
| 1383 |
1385 |
}
|
| 1384 |
1386 |
} else if (_right[arc] == INVALID) {
|
| 1385 |
1387 |
_parent[_left[arc]] = _parent[arc];
|
| 1386 |
1388 |
if (_parent[arc] != INVALID) {
|
| 1387 |
1389 |
if (_left[_parent[arc]] == arc) {
|
| 1388 |
1390 |
_left[_parent[arc]] = _left[arc];
|
| 1389 |
1391 |
} else {
|
| 1390 |
1392 |
_right[_parent[arc]] = _left[arc];
|
| 1391 |
1393 |
}
|
| 1392 |
1394 |
} else {
|
| 1393 |
1395 |
_head[_g.source(arc)] = _left[arc];
|
| 1394 |
1396 |
}
|
| 1395 |
1397 |
} else {
|
| 1396 |
1398 |
Arc e = _left[arc];
|
| 1397 |
1399 |
if (_right[e] != INVALID) {
|
| 1398 |
1400 |
e = _right[e];
|
| 1399 |
1401 |
while (_right[e] != INVALID) {
|
| 1400 |
1402 |
e = _right[e];
|
| 1401 |
1403 |
}
|
| 1402 |
1404 |
Arc s = _parent[e];
|
| 1403 |
1405 |
_right[_parent[e]] = _left[e];
|
| 1404 |
1406 |
if (_left[e] != INVALID) {
|
| 1405 |
1407 |
_parent[_left[e]] = _parent[e];
|
| 1406 |
1408 |
}
|
| 1407 |
1409 |
|
| 1408 |
1410 |
_left[e] = _left[arc];
|
| 1409 |
1411 |
_parent[_left[arc]] = e;
|
| 1410 |
1412 |
_right[e] = _right[arc];
|
| 1411 |
1413 |
_parent[_right[arc]] = e;
|
| 1412 |
1414 |
|
| 1413 |
1415 |
_parent[e] = _parent[arc];
|
| 1414 |
1416 |
if (_parent[arc] != INVALID) {
|
| 1415 |
1417 |
if (_left[_parent[arc]] == arc) {
|
| 1416 |
1418 |
_left[_parent[arc]] = e;
|
| 1417 |
1419 |
} else {
|
| 1418 |
1420 |
_right[_parent[arc]] = e;
|
| 1419 |
1421 |
}
|
| 1420 |
1422 |
}
|
| 1421 |
1423 |
splay(s);
|
| 1422 |
1424 |
} else {
|
| 1423 |
1425 |
_right[e] = _right[arc];
|
| 1424 |
1426 |
_parent[_right[arc]] = e;
|
| 1425 |
1427 |
_parent[e] = _parent[arc];
|
| 1426 |
1428 |
|
| 1427 |
1429 |
if (_parent[arc] != INVALID) {
|
| 1428 |
1430 |
if (_left[_parent[arc]] == arc) {
|
| 1429 |
1431 |
_left[_parent[arc]] = e;
|
| 1430 |
1432 |
} else {
|
| 1431 |
1433 |
_right[_parent[arc]] = e;
|
| 1432 |
1434 |
}
|
| 1433 |
1435 |
} else {
|
| 1434 |
1436 |
_head[_g.source(arc)] = e;
|
| 1435 |
1437 |
}
|
| 1436 |
1438 |
}
|
| 1437 |
1439 |
}
|
| 1438 |
1440 |
}
|
| 1439 |
1441 |
|
| 1440 |
1442 |
Arc refreshRec(std::vector<Arc> &v,int a,int b)
|
| 1441 |
1443 |
{
|
| 1442 |
1444 |
int m=(a+b)/2;
|
| 1443 |
1445 |
Arc me=v[m];
|
| 1444 |
1446 |
if (a < m) {
|
| 1445 |
1447 |
Arc left = refreshRec(v,a,m-1);
|
| 1446 |
1448 |
_left[me] = left;
|
| 1447 |
1449 |
_parent[left] = me;
|
| 1448 |
1450 |
} else {
|
| 1449 |
1451 |
_left[me] = INVALID;
|
| 1450 |
1452 |
}
|
| 1451 |
1453 |
if (m < b) {
|
| 1452 |
1454 |
Arc right = refreshRec(v,m+1,b);
|
| 1453 |
1455 |
_right[me] = right;
|
| 1454 |
1456 |
_parent[right] = me;
|
| 1455 |
1457 |
} else {
|
| 1456 |
1458 |
_right[me] = INVALID;
|
| 1457 |
1459 |
}
|
| 1458 |
1460 |
return me;
|
| 1459 |
1461 |
}
|
| 1460 |
1462 |
|
| 1461 |
1463 |
void refresh() {
|
| 1462 |
1464 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1463 |
1465 |
std::vector<Arc> v;
|
| 1464 |
1466 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
|
| 1465 |
1467 |
if (!v.empty()) {
|
| 1466 |
1468 |
std::sort(v.begin(),v.end(),ArcLess(_g));
|
| 1467 |
1469 |
Arc head = refreshRec(v,0,v.size()-1);
|
| 1468 |
1470 |
_head[n] = head;
|
| 1469 |
1471 |
_parent[head] = INVALID;
|
| 1470 |
1472 |
}
|
| 1471 |
1473 |
else _head[n] = INVALID;
|
| 1472 |
1474 |
}
|
| 1473 |
1475 |
}
|
| 1474 |
1476 |
|
| 1475 |
1477 |
void zig(Arc v) {
|
| 1476 |
1478 |
Arc w = _parent[v];
|
| 1477 |
1479 |
_parent[v] = _parent[w];
|
| 1478 |
1480 |
_parent[w] = v;
|
| 1479 |
1481 |
_left[w] = _right[v];
|
| 1480 |
1482 |
_right[v] = w;
|
| 1481 |
1483 |
if (_parent[v] != INVALID) {
|
| 1482 |
1484 |
if (_right[_parent[v]] == w) {
|
| 1483 |
1485 |
_right[_parent[v]] = v;
|
| 1484 |
1486 |
} else {
|
| 1485 |
1487 |
_left[_parent[v]] = v;
|
| 1486 |
1488 |
}
|
| 1487 |
1489 |
}
|
| 1488 |
1490 |
if (_left[w] != INVALID){
|
| 1489 |
1491 |
_parent[_left[w]] = w;
|
| 1490 |
1492 |
}
|
| 1491 |
1493 |
}
|
| 1492 |
1494 |
|
| 1493 |
1495 |
void zag(Arc v) {
|
| 1494 |
1496 |
Arc w = _parent[v];
|
| 1495 |
1497 |
_parent[v] = _parent[w];
|
| 1496 |
1498 |
_parent[w] = v;
|
| 1497 |
1499 |
_right[w] = _left[v];
|
| 1498 |
1500 |
_left[v] = w;
|
| 1499 |
1501 |
if (_parent[v] != INVALID){
|
| 1500 |
1502 |
if (_left[_parent[v]] == w) {
|
| 1501 |
1503 |
_left[_parent[v]] = v;
|
| 1502 |
1504 |
} else {
|
| 1503 |
1505 |
_right[_parent[v]] = v;
|
| 1504 |
1506 |
}
|
| 1505 |
1507 |
}
|
| 1506 |
1508 |
if (_right[w] != INVALID){
|
| 1507 |
1509 |
_parent[_right[w]] = w;
|
| 1508 |
1510 |
}
|
| 1509 |
1511 |
}
|
| 1510 |
1512 |
|
| 1511 |
1513 |
void splay(Arc v) {
|
| 1512 |
1514 |
while (_parent[v] != INVALID) {
|
| 1513 |
1515 |
if (v == _left[_parent[v]]) {
|
| 1514 |
1516 |
if (_parent[_parent[v]] == INVALID) {
|
| 1515 |
1517 |
zig(v);
|
| 1516 |
1518 |
} else {
|
| 1517 |
1519 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1518 |
1520 |
zig(_parent[v]);
|
| 1519 |
1521 |
zig(v);
|
| 1520 |
1522 |
} else {
|
| 1521 |
1523 |
zig(v);
|
| 1522 |
1524 |
zag(v);
|
| 1523 |
1525 |
}
|
| 1524 |
1526 |
}
|
| 1525 |
1527 |
} else {
|
| 1526 |
1528 |
if (_parent[_parent[v]] == INVALID) {
|
| 1527 |
1529 |
zag(v);
|
| 1528 |
1530 |
} else {
|
| 1529 |
1531 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1530 |
1532 |
zag(v);
|
| 1531 |
1533 |
zig(v);
|
| 1532 |
1534 |
} else {
|
| 1533 |
1535 |
zag(_parent[v]);
|
| 1534 |
1536 |
zag(v);
|
| 1535 |
1537 |
}
|
| 1536 |
1538 |
}
|
| 1537 |
1539 |
}
|
| 1538 |
1540 |
}
|
| 1539 |
1541 |
_head[_g.source(v)] = v;
|
| 1540 |
1542 |
}
|
| 1541 |
1543 |
|
| 1542 |
1544 |
|
| 1543 |
1545 |
public:
|
| 1544 |
1546 |
|
| 1545 |
1547 |
///Find an arc between two nodes.
|
| 1546 |
1548 |
|
| 1547 |
1549 |
///Find an arc between two nodes.
|
| 1548 |
1550 |
///\param s The source node.
|
| 1549 |
1551 |
///\param t The target node.
|
| 1550 |
1552 |
///\param p The previous arc between \c s and \c t. It it is INVALID or
|
| 1551 |
1553 |
///not given, the operator finds the first appropriate arc.
|
| 1552 |
1554 |
///\return An arc from \c s to \c t after \c p or
|
| 1553 |
1555 |
///\ref INVALID if there is no more.
|
| 1554 |
1556 |
///
|
| 1555 |
1557 |
///For example, you can count the number of arcs from \c u to \c v in the
|
| 1556 |
1558 |
///following way.
|
| 1557 |
1559 |
///\code
|
| 1558 |
1560 |
///DynArcLookUp<ListDigraph> ae(g);
|
| 1559 |
1561 |
///...
|
| 1560 |
1562 |
///int n = 0;
|
| 1561 |
1563 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
|
| 1562 |
1564 |
///\endcode
|
| 1563 |
1565 |
///
|
| 1564 |
1566 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>)
|
| 1565 |
1567 |
///amortized time, specifically, the time complexity of the lookups
|
| 1566 |
1568 |
///is equal to the optimal search tree implementation for the
|
| 1567 |
1569 |
///current query distribution in a constant factor.
|
| 1568 |
1570 |
///
|
| 1569 |
1571 |
///\note This is a dynamic data structure, therefore the data
|
| 1570 |
1572 |
///structure is updated after each graph alteration. Thus although
|
| 1571 |
1573 |
///this data structure is theoretically faster than \ref ArcLookUp
|
| 1572 |
1574 |
///and \ref AllArcLookUp, it often provides worse performance than
|
| 1573 |
1575 |
///them.
|
| 1574 |
1576 |
Arc operator()(Node s, Node t, Arc p = INVALID) const {
|
| 1575 |
1577 |
if (p == INVALID) {
|
| 1576 |
1578 |
Arc a = _head[s];
|
| 1577 |
1579 |
if (a == INVALID) return INVALID;
|
| 1578 |
1580 |
Arc r = INVALID;
|
| 1579 |
1581 |
while (true) {
|
| 1580 |
1582 |
if (_g.target(a) < t) {
|
| 1581 |
1583 |
if (_right[a] == INVALID) {
|
| 1582 |
1584 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 1583 |
1585 |
return r;
|
| 1584 |
1586 |
} else {
|
| 1585 |
1587 |
a = _right[a];
|
| 1586 |
1588 |
}
|
| 1587 |
1589 |
} else {
|
| 1588 |
1590 |
if (_g.target(a) == t) {
|
| 1589 |
1591 |
r = a;
|
| 1590 |
1592 |
}
|
| 1591 |
1593 |
if (_left[a] == INVALID) {
|
| 1592 |
1594 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 1593 |
1595 |
return r;
|
| 1594 |
1596 |
} else {
|
| 1595 |
1597 |
a = _left[a];
|
| 1596 |
1598 |
}
|
| 1597 |
1599 |
}
|
| 1598 |
1600 |
}
|
| 1599 |
1601 |
} else {
|
| 1600 |
1602 |
Arc a = p;
|
| 1601 |
1603 |
if (_right[a] != INVALID) {
|
| 1602 |
1604 |
a = _right[a];
|
| 1603 |
1605 |
while (_left[a] != INVALID) {
|
| 1604 |
1606 |
a = _left[a];
|
| 1605 |
1607 |
}
|
| 1606 |
1608 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 1607 |
1609 |
} else {
|
| 1608 |
1610 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) {
|
| 1609 |
1611 |
a = _parent[a];
|
| 1610 |
1612 |
}
|
| 1611 |
1613 |
if (_parent[a] == INVALID) {
|
| 1612 |
1614 |
return INVALID;
|
| 1613 |
1615 |
} else {
|
| 1614 |
1616 |
a = _parent[a];
|
| 1615 |
1617 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 1616 |
1618 |
}
|
| 1617 |
1619 |
}
|
| 1618 |
1620 |
if (_g.target(a) == t) return a;
|
| 1619 |
1621 |
else return INVALID;
|
| 1620 |
1622 |
}
|
| 1621 |
1623 |
}
|
| 1622 |
1624 |
|
| 1623 |
1625 |
};
|
| 1624 |
1626 |
|
| 1625 |
1627 |
///Fast arc look-up between given endpoints.
|
| 1626 |
1628 |
|
| 1627 |
1629 |
///Using this class, you can find an arc in a digraph from a given
|
| 1628 |
1630 |
///source to a given target in time <em>O</em>(log<em>d</em>),
|
| 1629 |
1631 |
///where <em>d</em> is the out-degree of the source node.
|
| 1630 |
1632 |
///
|
| 1631 |
1633 |
///It is not possible to find \e all parallel arcs between two nodes.
|
| 1632 |
1634 |
///Use \ref AllArcLookUp for this purpose.
|
| 1633 |
1635 |
///
|
| 1634 |
1636 |
///\warning This class is static, so you should call refresh() (or at
|
| 1635 |
1637 |
///least refresh(Node)) to refresh this data structure whenever the
|
| 1636 |
1638 |
///digraph changes. This is a time consuming (superlinearly proportional
|
| 1637 |
1639 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
|
| 1638 |
1640 |
///
|
| 1639 |
1641 |
///\tparam GR The type of the underlying digraph.
|
| 1640 |
1642 |
///
|
| 1641 |
1643 |
///\sa DynArcLookUp
|
| 1642 |
1644 |
///\sa AllArcLookUp
|
| 1643 |
1645 |
template<class GR>
|
| 1644 |
1646 |
class ArcLookUp
|
| 1645 |
1647 |
{
|
| 1646 |
1648 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
| 1647 |
1649 |
|
| 1648 |
1650 |
public:
|
| 1649 |
1651 |
|
| 1650 |
1652 |
/// The Digraph type
|
| 1651 |
1653 |
typedef GR Digraph;
|
| 1652 |
1654 |
|
| 1653 |
1655 |
protected:
|
| 1654 |
1656 |
const Digraph &_g;
|
| 1655 |
1657 |
typename Digraph::template NodeMap<Arc> _head;
|
| 1656 |
1658 |
typename Digraph::template ArcMap<Arc> _left;
|
| 1657 |
1659 |
typename Digraph::template ArcMap<Arc> _right;
|
| 1658 |
1660 |
|
| 1659 |
1661 |
class ArcLess {
|
| 1660 |
1662 |
const Digraph &g;
|
| 1661 |
1663 |
public:
|
| 1662 |
1664 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1663 |
1665 |
bool operator()(Arc a,Arc b) const
|
| 1664 |
1666 |
{
|
| 1665 |
1667 |
return g.target(a)<g.target(b);
|
| 1666 |
1668 |
}
|
| 1667 |
1669 |
};
|
| 1668 |
1670 |
|
| 1669 |
1671 |
public:
|
| 1670 |
1672 |
|
| 1671 |
1673 |
///Constructor
|
| 1672 |
1674 |
|
| 1673 |
1675 |
///Constructor.
|
| 1674 |
1676 |
///
|
| 1675 |
1677 |
///It builds up the search database, which remains valid until the digraph
|
| 1676 |
1678 |
///changes.
|
| 1677 |
1679 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
| 1678 |
1680 |
|
| 1679 |
1681 |
private:
|
| 1680 |
1682 |
Arc refreshRec(std::vector<Arc> &v,int a,int b)
|
| 1681 |
1683 |
{
|
| 1682 |
1684 |
int m=(a+b)/2;
|
| 1683 |
1685 |
Arc me=v[m];
|
| 1684 |
1686 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID;
|
| 1685 |
1687 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID;
|
| 1686 |
1688 |
return me;
|
| 1687 |
1689 |
}
|
| 1688 |
1690 |
public:
|
| 1689 |
1691 |
///Refresh the search data structure at a node.
|
| 1690 |
1692 |
|
| 1691 |
1693 |
///Build up the search database of node \c n.
|
| 1692 |
1694 |
///
|
| 1693 |
1695 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
|
| 1694 |
1696 |
///is the number of the outgoing arcs of \c n.
|
| 1695 |
1697 |
void refresh(Node n)
|
| 1696 |
1698 |
{
|
| 1697 |
1699 |
std::vector<Arc> v;
|
| 1698 |
1700 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
|
| 1699 |
1701 |
if(v.size()) {
|
| 1700 |
1702 |
std::sort(v.begin(),v.end(),ArcLess(_g));
|
| 1701 |
1703 |
_head[n]=refreshRec(v,0,v.size()-1);
|
| 1702 |
1704 |
}
|
| 1703 |
1705 |
else _head[n]=INVALID;
|
| 1704 |
1706 |
}
|
| 1705 |
1707 |
///Refresh the full data structure.
|
| 1706 |
1708 |
|
| 1707 |
1709 |
///Build up the full search database. In fact, it simply calls
|
| 1708 |
1710 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
| 1709 |
1711 |
///
|
| 1710 |
1712 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
|
| 1711 |
1713 |
///the number of the arcs in the digraph and <em>D</em> is the maximum
|
| 1712 |
1714 |
///out-degree of the digraph.
|
| 1713 |
1715 |
void refresh()
|
| 1714 |
1716 |
{
|
| 1715 |
1717 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
|
| 1716 |
1718 |
}
|
| 1717 |
1719 |
|
| 1718 |
1720 |
///Find an arc between two nodes.
|
| 1719 |
1721 |
|
| 1720 |
1722 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
|
| 1721 |
1723 |
///where <em>d</em> is the number of outgoing arcs of \c s.
|
| 1722 |
1724 |
///\param s The source node.
|
| 1723 |
1725 |
///\param t The target node.
|
| 1724 |
1726 |
///\return An arc from \c s to \c t if there exists,
|
| 1725 |
1727 |
///\ref INVALID otherwise.
|
| 1726 |
1728 |
///
|
| 1727 |
1729 |
///\warning If you change the digraph, refresh() must be called before using
|
| 1728 |
1730 |
///this operator. If you change the outgoing arcs of
|
| 1729 |
1731 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
|
| 1730 |
1732 |
Arc operator()(Node s, Node t) const
|
| 1731 |
1733 |
{
|
| 1732 |
1734 |
Arc e;
|
| 1733 |
1735 |
for(e=_head[s];
|
| 1734 |
1736 |
e!=INVALID&&_g.target(e)!=t;
|
| 1735 |
1737 |
e = t < _g.target(e)?_left[e]:_right[e]) ;
|
| 1736 |
1738 |
return e;
|
| 1737 |
1739 |
}
|
| 1738 |
1740 |
|
| 1739 |
1741 |
};
|
| 1740 |
1742 |
|
| 1741 |
1743 |
///Fast look-up of all arcs between given endpoints.
|
| 1742 |
1744 |
|
| 1743 |
1745 |
///This class is the same as \ref ArcLookUp, with the addition
|
| 1744 |
1746 |
///that it makes it possible to find all parallel arcs between given
|
| 1745 |
1747 |
///endpoints.
|
| 1746 |
1748 |
///
|
| 1747 |
1749 |
///\warning This class is static, so you should call refresh() (or at
|
| 1748 |
1750 |
///least refresh(Node)) to refresh this data structure whenever the
|
| 1749 |
1751 |
///digraph changes. This is a time consuming (superlinearly proportional
|
| 1750 |
1752 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
|
| 1751 |
1753 |
///
|
| 1752 |
1754 |
///\tparam GR The type of the underlying digraph.
|
| 1753 |
1755 |
///
|
| 1754 |
1756 |
///\sa DynArcLookUp
|
| 1755 |
1757 |
///\sa ArcLookUp
|
| 1756 |
1758 |
template<class GR>
|
| 1757 |
1759 |
class AllArcLookUp : public ArcLookUp<GR>
|
| 1758 |
1760 |
{
|
| 1759 |
1761 |
using ArcLookUp<GR>::_g;
|
| 1760 |
1762 |
using ArcLookUp<GR>::_right;
|
| 1761 |
1763 |
using ArcLookUp<GR>::_left;
|
| 1762 |
1764 |
using ArcLookUp<GR>::_head;
|
| 1763 |
1765 |
|
| 1764 |
1766 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
| 1765 |
1767 |
|
| 1766 |
1768 |
typename GR::template ArcMap<Arc> _next;
|
| 1767 |
1769 |
|
| 1768 |
1770 |
Arc refreshNext(Arc head,Arc next=INVALID)
|
| 1769 |
1771 |
{
|
| 1770 |
1772 |
if(head==INVALID) return next;
|
| 1771 |
1773 |
else {
|
| 1772 |
1774 |
next=refreshNext(_right[head],next);
|
| 1773 |
1775 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
|
| 1774 |
1776 |
? next : INVALID;
|
| 1775 |
1777 |
return refreshNext(_left[head],head);
|
| 1776 |
1778 |
}
|
| 1777 |
1779 |
}
|
| 1778 |
1780 |
|
| 1779 |
1781 |
void refreshNext()
|
| 1780 |
1782 |
{
|
| 1781 |
1783 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
|
| 1782 |
1784 |
}
|
| 1783 |
1785 |
|
| 1784 |
1786 |
public:
|
| 1785 |
1787 |
|
| 1786 |
1788 |
/// The Digraph type
|
| 1787 |
1789 |
typedef GR Digraph;
|
| 1788 |
1790 |
|
| 1789 |
1791 |
///Constructor
|
| 1790 |
1792 |
|
| 1791 |
1793 |
///Constructor.
|
| 1792 |
1794 |
///
|
| 1793 |
1795 |
///It builds up the search database, which remains valid until the digraph
|
| 1794 |
1796 |
///changes.
|
| 1795 |
1797 |
AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
|
| 1796 |
1798 |
|
| 1797 |
1799 |
///Refresh the data structure at a node.
|
| 1798 |
1800 |
|
| 1799 |
1801 |
///Build up the search database of node \c n.
|
| 1800 |
1802 |
///
|
| 1801 |
1803 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
|
| 1802 |
1804 |
///the number of the outgoing arcs of \c n.
|
| 1803 |
1805 |
void refresh(Node n)
|
| 1804 |
1806 |
{
|
| 1805 |
1807 |
ArcLookUp<GR>::refresh(n);
|
| 1806 |
1808 |
refreshNext(_head[n]);
|
| 1807 |
1809 |
}
|
| 1808 |
1810 |
|
| 1809 |
1811 |
///Refresh the full data structure.
|
| 1810 |
1812 |
|
| 1811 |
1813 |
///Build up the full search database. In fact, it simply calls
|
| 1812 |
1814 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
| 1813 |
1815 |
///
|
| 1814 |
1816 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
|
| 1815 |
1817 |
///the number of the arcs in the digraph and <em>D</em> is the maximum
|
| 1816 |
1818 |
///out-degree of the digraph.
|
| 1817 |
1819 |
void refresh()
|
| 1818 |
1820 |
{
|
| 1819 |
1821 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
|
| 1820 |
1822 |
}
|
| 1821 |
1823 |
|
| 1822 |
1824 |
///Find an arc between two nodes.
|
| 1823 |
1825 |
|
| 1824 |
1826 |
///Find an arc between two nodes.
|
| 1825 |
1827 |
///\param s The source node.
|
| 1826 |
1828 |
///\param t The target node.
|
| 1827 |
1829 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or
|
| 1828 |
1830 |
///not given, the operator finds the first appropriate arc.
|
| 1829 |
1831 |
///\return An arc from \c s to \c t after \c prev or
|
| 1830 |
1832 |
///\ref INVALID if there is no more.
|
| 1831 |
1833 |
///
|
| 1832 |
1834 |
///For example, you can count the number of arcs from \c u to \c v in the
|
| 1833 |
1835 |
///following way.
|
| 1834 |
1836 |
///\code
|
| 1835 |
1837 |
///AllArcLookUp<ListDigraph> ae(g);
|
| 1836 |
1838 |
///...
|
| 1837 |
1839 |
///int n = 0;
|
| 1838 |
1840 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
|
| 1839 |
1841 |
///\endcode
|
| 1840 |
1842 |
///
|
| 1841 |
1843 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time,
|
| 1842 |
1844 |
///where <em>d</em> is the number of outgoing arcs of \c s. Then the
|
| 1843 |
1845 |
///consecutive arcs are found in constant time.
|
| 1844 |
1846 |
///
|
| 1845 |
1847 |
///\warning If you change the digraph, refresh() must be called before using
|
| 1846 |
1848 |
///this operator. If you change the outgoing arcs of
|
| 1847 |
1849 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
|
| 1848 |
1850 |
///
|
| 1849 |
1851 |
#ifdef DOXYGEN
|
| 1850 |
1852 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
|
| 1851 |
1853 |
#else
|
| 1852 |
1854 |
using ArcLookUp<GR>::operator() ;
|
| 1853 |
1855 |
Arc operator()(Node s, Node t, Arc prev) const
|
| 1854 |
1856 |
{
|
| 1855 |
1857 |
return prev==INVALID?(*this)(s,t):_next[prev];
|
| 1856 |
1858 |
}
|
| 1857 |
1859 |
#endif
|
| 1858 |
1860 |
|
| 1859 |
1861 |
};
|
| 1860 |
1862 |
|
| 1861 |
1863 |
/// @}
|
| 1862 |
1864 |
|
| 1863 |
1865 |
} //namespace lemon
|
| 1864 |
1866 |
|
| 1865 |
1867 |
#endif
|