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-2008
|
6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
8 |
8 |
*
|
9 |
9 |
* Permission to use, modify and distribute this software is granted
|
10 |
10 |
* provided that this copyright notice appears in all copies. For
|
11 |
11 |
* precise terms see the accompanying LICENSE file.
|
12 |
12 |
*
|
13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
15 |
15 |
* purpose.
|
16 |
16 |
*
|
17 |
17 |
*/
|
18 |
18 |
|
19 |
19 |
#ifndef LEMON_CORE_H
|
20 |
20 |
#define LEMON_CORE_H
|
21 |
21 |
|
22 |
22 |
#include <vector>
|
23 |
23 |
#include <algorithm>
|
24 |
24 |
|
25 |
|
#include <lemon/core.h>
|
|
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 |
///\file
|
31 |
31 |
///\brief LEMON core utilities.
|
32 |
32 |
///
|
33 |
33 |
///This header file contains core utilities for LEMON.
|
34 |
34 |
///It is automatically included by all graph types, therefore it usually
|
35 |
35 |
///do not have to be included directly.
|
36 |
36 |
|
37 |
37 |
namespace lemon {
|
38 |
38 |
|
39 |
39 |
/// \brief Dummy type to make it easier to create invalid iterators.
|
40 |
40 |
///
|
41 |
41 |
/// Dummy type to make it easier to create invalid iterators.
|
42 |
42 |
/// See \ref INVALID for the usage.
|
43 |
43 |
struct Invalid {
|
44 |
44 |
public:
|
45 |
45 |
bool operator==(Invalid) { return true; }
|
46 |
46 |
bool operator!=(Invalid) { return false; }
|
47 |
47 |
bool operator< (Invalid) { return false; }
|
48 |
48 |
};
|
49 |
49 |
|
50 |
50 |
/// \brief Invalid iterators.
|
51 |
51 |
///
|
52 |
52 |
/// \ref Invalid is a global type that converts to each iterator
|
53 |
53 |
/// in such a way that the value of the target iterator will be invalid.
|
54 |
54 |
#ifdef LEMON_ONLY_TEMPLATES
|
55 |
55 |
const Invalid INVALID = Invalid();
|
56 |
56 |
#else
|
57 |
57 |
extern const Invalid INVALID;
|
58 |
58 |
#endif
|
59 |
59 |
|
60 |
60 |
/// \addtogroup gutils
|
61 |
61 |
/// @{
|
62 |
62 |
|
63 |
63 |
///Create convenience typedefs for the digraph types and iterators
|
64 |
64 |
|
65 |
65 |
///This \c \#define creates convenient type definitions for the following
|
66 |
66 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
|
67 |
67 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
|
68 |
68 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
|
69 |
69 |
///
|
70 |
70 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
71 |
71 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
|
72 |
72 |
///macro.
|
73 |
73 |
#define DIGRAPH_TYPEDEFS(Digraph) \
|
74 |
74 |
typedef Digraph::Node Node; \
|
75 |
75 |
typedef Digraph::NodeIt NodeIt; \
|
76 |
76 |
typedef Digraph::Arc Arc; \
|
77 |
77 |
typedef Digraph::ArcIt ArcIt; \
|
78 |
78 |
typedef Digraph::InArcIt InArcIt; \
|
79 |
79 |
typedef Digraph::OutArcIt OutArcIt; \
|
80 |
80 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \
|
81 |
81 |
typedef Digraph::NodeMap<int> IntNodeMap; \
|
82 |
82 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \
|
83 |
83 |
typedef Digraph::ArcMap<bool> BoolArcMap; \
|
84 |
84 |
typedef Digraph::ArcMap<int> IntArcMap; \
|
85 |
85 |
typedef Digraph::ArcMap<double> DoubleArcMap
|
86 |
86 |
|
87 |
87 |
///Create convenience typedefs for the digraph types and iterators
|
88 |
88 |
|
89 |
89 |
///\see DIGRAPH_TYPEDEFS
|
90 |
90 |
///
|
91 |
91 |
///\note Use this macro, if the graph type is a dependent type,
|
92 |
92 |
///ie. the graph type depend on a template parameter.
|
93 |
93 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
|
94 |
94 |
typedef typename Digraph::Node Node; \
|
95 |
95 |
typedef typename Digraph::NodeIt NodeIt; \
|
96 |
96 |
typedef typename Digraph::Arc Arc; \
|
97 |
97 |
typedef typename Digraph::ArcIt ArcIt; \
|
98 |
98 |
typedef typename Digraph::InArcIt InArcIt; \
|
99 |
99 |
typedef typename Digraph::OutArcIt OutArcIt; \
|
100 |
100 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
|
101 |
101 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \
|
102 |
102 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
|
103 |
103 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
|
104 |
104 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \
|
105 |
105 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap
|
106 |
106 |
|
107 |
107 |
///Create convenience typedefs for the graph types and iterators
|
108 |
108 |
|
109 |
109 |
///This \c \#define creates the same convenient type definitions as defined
|
110 |
110 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
|
111 |
111 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
|
112 |
112 |
///\c DoubleEdgeMap.
|
113 |
113 |
///
|
114 |
114 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
115 |
115 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
|
116 |
116 |
///macro.
|
117 |
117 |
#define GRAPH_TYPEDEFS(Graph) \
|
118 |
118 |
DIGRAPH_TYPEDEFS(Graph); \
|
119 |
119 |
typedef Graph::Edge Edge; \
|
120 |
120 |
typedef Graph::EdgeIt EdgeIt; \
|
121 |
121 |
typedef Graph::IncEdgeIt IncEdgeIt; \
|
122 |
122 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \
|
123 |
123 |
typedef Graph::EdgeMap<int> IntEdgeMap; \
|
124 |
124 |
typedef Graph::EdgeMap<double> DoubleEdgeMap
|
125 |
125 |
|
126 |
126 |
///Create convenience typedefs for the graph types and iterators
|
127 |
127 |
|
128 |
128 |
///\see GRAPH_TYPEDEFS
|
129 |
129 |
///
|
130 |
130 |
///\note Use this macro, if the graph type is a dependent type,
|
131 |
131 |
///ie. the graph type depend on a template parameter.
|
132 |
132 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
|
133 |
133 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
|
134 |
134 |
typedef typename Graph::Edge Edge; \
|
135 |
135 |
typedef typename Graph::EdgeIt EdgeIt; \
|
136 |
136 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \
|
137 |
137 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
|
138 |
138 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
|
139 |
139 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
|
140 |
140 |
|
141 |
141 |
/// \brief Function to count the items in a graph.
|
142 |
142 |
///
|
143 |
143 |
/// This function counts the items (nodes, arcs etc.) in a graph.
|
144 |
144 |
/// The complexity of the function is linear because
|
145 |
145 |
/// it iterates on all of the items.
|
146 |
146 |
template <typename Graph, typename Item>
|
147 |
147 |
inline int countItems(const Graph& g) {
|
148 |
148 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
149 |
149 |
int num = 0;
|
150 |
150 |
for (ItemIt it(g); it != INVALID; ++it) {
|
151 |
151 |
++num;
|
152 |
152 |
}
|
153 |
153 |
return num;
|
154 |
154 |
}
|
155 |
155 |
|
156 |
156 |
// Node counting:
|
157 |
157 |
|
158 |
158 |
namespace _core_bits {
|
159 |
159 |
|
160 |
160 |
template <typename Graph, typename Enable = void>
|
161 |
161 |
struct CountNodesSelector {
|
162 |
162 |
static int count(const Graph &g) {
|
163 |
163 |
return countItems<Graph, typename Graph::Node>(g);
|
164 |
164 |
}
|
165 |
165 |
};
|
166 |
166 |
|
167 |
167 |
template <typename Graph>
|
168 |
168 |
struct CountNodesSelector<
|
169 |
169 |
Graph, typename
|
170 |
170 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
171 |
171 |
{
|
172 |
172 |
static int count(const Graph &g) {
|
173 |
173 |
return g.nodeNum();
|
174 |
174 |
}
|
175 |
175 |
};
|
176 |
176 |
}
|
177 |
177 |
|
178 |
178 |
/// \brief Function to count the nodes in the graph.
|
179 |
179 |
///
|
180 |
180 |
/// This function counts the nodes in the graph.
|
181 |
181 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some
|
182 |
182 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
183 |
183 |
///
|
184 |
184 |
/// \note If the graph contains a \c nodeNum() member function and a
|
185 |
185 |
/// \c NodeNumTag tag then this function calls directly the member
|
186 |
186 |
/// function to query the cardinality of the node set.
|
187 |
187 |
template <typename Graph>
|
188 |
188 |
inline int countNodes(const Graph& g) {
|
189 |
189 |
return _core_bits::CountNodesSelector<Graph>::count(g);
|
190 |
190 |
}
|
191 |
191 |
|
192 |
192 |
// Arc counting:
|
193 |
193 |
|
194 |
194 |
namespace _core_bits {
|
195 |
195 |
|
196 |
196 |
template <typename Graph, typename Enable = void>
|
197 |
197 |
struct CountArcsSelector {
|
198 |
198 |
static int count(const Graph &g) {
|
199 |
199 |
return countItems<Graph, typename Graph::Arc>(g);
|
200 |
200 |
}
|
201 |
201 |
};
|
202 |
202 |
|
203 |
203 |
template <typename Graph>
|
204 |
204 |
struct CountArcsSelector<
|
205 |
205 |
Graph,
|
206 |
206 |
typename enable_if<typename Graph::ArcNumTag, void>::type>
|
207 |
207 |
{
|
208 |
208 |
static int count(const Graph &g) {
|
209 |
209 |
return g.arcNum();
|
210 |
210 |
}
|
211 |
211 |
};
|
212 |
212 |
}
|
213 |
213 |
|
214 |
214 |
/// \brief Function to count the arcs in the graph.
|
215 |
215 |
///
|
216 |
216 |
/// This function counts the arcs in the graph.
|
217 |
217 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
218 |
218 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
219 |
219 |
///
|
220 |
220 |
/// \note If the graph contains a \c arcNum() member function and a
|
221 |
221 |
/// \c ArcNumTag tag then this function calls directly the member
|
222 |
222 |
/// function to query the cardinality of the arc set.
|
223 |
223 |
template <typename Graph>
|
224 |
224 |
inline int countArcs(const Graph& g) {
|
225 |
225 |
return _core_bits::CountArcsSelector<Graph>::count(g);
|
226 |
226 |
}
|
227 |
227 |
|
228 |
228 |
// Edge counting:
|
229 |
229 |
|
230 |
230 |
namespace _core_bits {
|
231 |
231 |
|
232 |
232 |
template <typename Graph, typename Enable = void>
|
233 |
233 |
struct CountEdgesSelector {
|
234 |
234 |
static int count(const Graph &g) {
|
235 |
235 |
return countItems<Graph, typename Graph::Edge>(g);
|
236 |
236 |
}
|
237 |
237 |
};
|
238 |
238 |
|
239 |
239 |
template <typename Graph>
|
240 |
240 |
struct CountEdgesSelector<
|
241 |
241 |
Graph,
|
242 |
242 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
243 |
243 |
{
|
244 |
244 |
static int count(const Graph &g) {
|
245 |
245 |
return g.edgeNum();
|
246 |
246 |
}
|
247 |
247 |
};
|
248 |
248 |
}
|
249 |
249 |
|
250 |
250 |
/// \brief Function to count the edges in the graph.
|
251 |
251 |
///
|
252 |
252 |
/// This function counts the edges in the graph.
|
253 |
253 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
254 |
254 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
255 |
255 |
///
|
256 |
256 |
/// \note If the graph contains a \c edgeNum() member function and a
|
257 |
257 |
/// \c EdgeNumTag tag then this function calls directly the member
|
258 |
258 |
/// function to query the cardinality of the edge set.
|
259 |
259 |
template <typename Graph>
|
260 |
260 |
inline int countEdges(const Graph& g) {
|
261 |
261 |
return _core_bits::CountEdgesSelector<Graph>::count(g);
|
262 |
262 |
|
263 |
263 |
}
|
264 |
264 |
|
265 |
265 |
|
266 |
266 |
template <typename Graph, typename DegIt>
|
267 |
267 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
268 |
268 |
int num = 0;
|
269 |
269 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
270 |
270 |
++num;
|
271 |
271 |
}
|
272 |
272 |
return num;
|
273 |
273 |
}
|
274 |
274 |
|
275 |
275 |
/// \brief Function to count the number of the out-arcs from node \c n.
|
276 |
276 |
///
|
277 |
277 |
/// This function counts the number of the out-arcs from node \c n
|
278 |
278 |
/// in the graph \c g.
|
279 |
279 |
template <typename Graph>
|
280 |
280 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
|
281 |
281 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
|
282 |
282 |
}
|
283 |
283 |
|
284 |
284 |
/// \brief Function to count the number of the in-arcs to node \c n.
|
285 |
285 |
///
|
286 |
286 |
/// This function counts the number of the in-arcs to node \c n
|
287 |
287 |
/// in the graph \c g.
|
288 |
288 |
template <typename Graph>
|
289 |
289 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
|
290 |
290 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
|
291 |
291 |
}
|
292 |
292 |
|
293 |
293 |
/// \brief Function to count the number of the inc-edges to node \c n.
|
294 |
294 |
///
|
295 |
295 |
/// This function counts the number of the inc-edges to node \c n
|
296 |
296 |
/// in the undirected graph \c g.
|
297 |
297 |
template <typename Graph>
|
298 |
298 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
|
299 |
299 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
|
300 |
300 |
}
|
301 |
301 |
|
302 |
302 |
namespace _core_bits {
|
303 |
303 |
|
304 |
304 |
template <typename Digraph, typename Item, typename RefMap>
|
305 |
305 |
class MapCopyBase {
|
306 |
306 |
public:
|
307 |
307 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
|
308 |
308 |
|
309 |
309 |
virtual ~MapCopyBase() {}
|
310 |
310 |
};
|
311 |
311 |
|
312 |
312 |
template <typename Digraph, typename Item, typename RefMap,
|
313 |
313 |
typename FromMap, typename ToMap>
|
314 |
314 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
315 |
315 |
public:
|
316 |
316 |
|
317 |
317 |
MapCopy(const FromMap& map, ToMap& tmap)
|
318 |
318 |
: _map(map), _tmap(tmap) {}
|
319 |
319 |
|
320 |
320 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
321 |
321 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
322 |
322 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
323 |
323 |
_tmap.set(refMap[it], _map[it]);
|
324 |
324 |
}
|
325 |
325 |
}
|
326 |
326 |
|
327 |
327 |
private:
|
328 |
328 |
const FromMap& _map;
|
329 |
329 |
ToMap& _tmap;
|
330 |
330 |
};
|
331 |
331 |
|
332 |
332 |
template <typename Digraph, typename Item, typename RefMap, typename It>
|
333 |
333 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
334 |
334 |
public:
|
335 |
335 |
|
336 |
336 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
|
337 |
337 |
|
338 |
338 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
339 |
339 |
_it = refMap[_item];
|
340 |
340 |
}
|
341 |
341 |
|
342 |
342 |
private:
|
343 |
343 |
Item _item;
|
344 |
344 |
It& _it;
|
345 |
345 |
};
|
346 |
346 |
|
347 |
347 |
template <typename Digraph, typename Item, typename RefMap, typename Ref>
|
348 |
348 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
349 |
349 |
public:
|
350 |
350 |
|
351 |
351 |
RefCopy(Ref& map) : _map(map) {}
|
352 |
352 |
|
353 |
353 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
354 |
354 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
355 |
355 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
356 |
356 |
_map.set(it, refMap[it]);
|
357 |
357 |
}
|
358 |
358 |
}
|
359 |
359 |
|
360 |
360 |
private:
|
361 |
361 |
Ref& _map;
|
362 |
362 |
};
|
363 |
363 |
|
364 |
364 |
template <typename Digraph, typename Item, typename RefMap,
|
365 |
365 |
typename CrossRef>
|
366 |
366 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
367 |
367 |
public:
|
368 |
368 |
|
369 |
369 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
370 |
370 |
|
371 |
371 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
372 |
372 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
373 |
373 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
374 |
374 |
_cmap.set(refMap[it], it);
|
375 |
375 |
}
|
376 |
376 |
}
|
377 |
377 |
|
378 |
378 |
private:
|
379 |
379 |
CrossRef& _cmap;
|
380 |
380 |
};
|
381 |
381 |
|
382 |
382 |
template <typename Digraph, typename Enable = void>
|
383 |
383 |
struct DigraphCopySelector {
|
384 |
384 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
385 |
385 |
static void copy(const From& from, Digraph &to,
|
386 |
386 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
387 |
387 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
388 |
388 |
nodeRefMap[it] = to.addNode();
|
389 |
389 |
}
|
390 |
390 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
391 |
391 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
|
392 |
392 |
nodeRefMap[from.target(it)]);
|
393 |
393 |
}
|
394 |
394 |
}
|
395 |
395 |
};
|
396 |
396 |
|
397 |
397 |
template <typename Digraph>
|
398 |
398 |
struct DigraphCopySelector<
|
399 |
399 |
Digraph,
|
400 |
400 |
typename enable_if<typename Digraph::BuildTag, void>::type>
|
401 |
401 |
{
|
402 |
402 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
403 |
403 |
static void copy(const From& from, Digraph &to,
|
404 |
404 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
405 |
405 |
to.build(from, nodeRefMap, arcRefMap);
|
406 |
406 |
}
|
407 |
407 |
};
|
408 |
408 |
|
409 |
409 |
template <typename Graph, typename Enable = void>
|
410 |
410 |
struct GraphCopySelector {
|
411 |
411 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
412 |
412 |
static void copy(const From& from, Graph &to,
|
413 |
413 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
414 |
414 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
415 |
415 |
nodeRefMap[it] = to.addNode();
|
416 |
416 |
}
|
417 |
417 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
418 |
418 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
|
419 |
419 |
nodeRefMap[from.v(it)]);
|
420 |
420 |
}
|
421 |
421 |
}
|
422 |
422 |
};
|
423 |
423 |
|
424 |
424 |
template <typename Graph>
|
425 |
425 |
struct GraphCopySelector<
|
426 |
426 |
Graph,
|
427 |
427 |
typename enable_if<typename Graph::BuildTag, void>::type>
|
428 |
428 |
{
|
429 |
429 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
430 |
430 |
static void copy(const From& from, Graph &to,
|
431 |
431 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
432 |
432 |
to.build(from, nodeRefMap, edgeRefMap);
|
433 |
433 |
}
|
434 |
434 |
};
|
435 |
435 |
|
436 |
436 |
}
|
437 |
437 |
|
438 |
438 |
/// \brief Class to copy a digraph.
|
439 |
439 |
///
|
440 |
440 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The
|
441 |
441 |
/// simplest way of using it is through the \c digraphCopy() function.
|
442 |
442 |
///
|
443 |
443 |
/// This class not only make a copy of a digraph, but it can create
|
444 |
444 |
/// references and cross references between the nodes and arcs of
|
445 |
445 |
/// the two digraphs, and it can copy maps to use with the newly created
|
446 |
446 |
/// digraph.
|
447 |
447 |
///
|
448 |
448 |
/// To make a copy from a digraph, first an instance of DigraphCopy
|
449 |
449 |
/// should be created, then the data belongs to the digraph should
|
450 |
450 |
/// assigned to copy. In the end, the \c run() member should be
|
451 |
451 |
/// called.
|
452 |
452 |
///
|
453 |
453 |
/// The next code copies a digraph with several data:
|
454 |
454 |
///\code
|
455 |
455 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
|
456 |
456 |
/// // Create references for the nodes
|
457 |
457 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
458 |
458 |
/// cg.nodeRef(nr);
|
459 |
459 |
/// // Create cross references (inverse) for the arcs
|
460 |
460 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
|
461 |
461 |
/// cg.arcCrossRef(acr);
|
462 |
462 |
/// // Copy an arc map
|
463 |
463 |
/// OrigGraph::ArcMap<double> oamap(orig_graph);
|
464 |
464 |
/// NewGraph::ArcMap<double> namap(new_graph);
|
465 |
465 |
/// cg.arcMap(oamap, namap);
|
466 |
466 |
/// // Copy a node
|
467 |
467 |
/// OrigGraph::Node on;
|
468 |
468 |
/// NewGraph::Node nn;
|
469 |
469 |
/// cg.node(on, nn);
|
470 |
470 |
/// // Execute copying
|
471 |
471 |
/// cg.run();
|
472 |
472 |
///\endcode
|
473 |
473 |
template <typename From, typename To>
|
474 |
474 |
class DigraphCopy {
|
475 |
475 |
private:
|
476 |
476 |
|
477 |
477 |
typedef typename From::Node Node;
|
478 |
478 |
typedef typename From::NodeIt NodeIt;
|
479 |
479 |
typedef typename From::Arc Arc;
|
480 |
480 |
typedef typename From::ArcIt ArcIt;
|
481 |
481 |
|
482 |
482 |
typedef typename To::Node TNode;
|
483 |
483 |
typedef typename To::Arc TArc;
|
484 |
484 |
|
485 |
485 |
typedef typename From::template NodeMap<TNode> NodeRefMap;
|
486 |
486 |
typedef typename From::template ArcMap<TArc> ArcRefMap;
|
487 |
487 |
|
488 |
488 |
public:
|
489 |
489 |
|
490 |
490 |
/// \brief Constructor of DigraphCopy.
|
491 |
491 |
///
|
492 |
492 |
/// Constructor of DigraphCopy for copying the content of the
|
493 |
493 |
/// \c from digraph into the \c to digraph.
|
494 |
494 |
DigraphCopy(const From& from, To& to)
|
495 |
495 |
: _from(from), _to(to) {}
|
496 |
496 |
|
497 |
497 |
/// \brief Destructor of DigraphCopy
|
498 |
498 |
///
|
499 |
499 |
/// Destructor of DigraphCopy.
|
500 |
500 |
~DigraphCopy() {
|
501 |
501 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
502 |
502 |
delete _node_maps[i];
|
503 |
503 |
}
|
504 |
504 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
505 |
505 |
delete _arc_maps[i];
|
506 |
506 |
}
|
507 |
507 |
|
508 |
508 |
}
|
509 |
509 |
|
510 |
510 |
/// \brief Copy the node references into the given map.
|
511 |
511 |
///
|
512 |
512 |
/// This function copies the node references into the given map.
|
513 |
513 |
/// The parameter should be a map, whose key type is the Node type of
|
514 |
514 |
/// the source digraph, while the value type is the Node type of the
|
515 |
515 |
/// destination digraph.
|
516 |
516 |
template <typename NodeRef>
|
517 |
517 |
DigraphCopy& nodeRef(NodeRef& map) {
|
518 |
518 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node,
|
519 |
519 |
NodeRefMap, NodeRef>(map));
|
520 |
520 |
return *this;
|
521 |
521 |
}
|
522 |
522 |
|
523 |
523 |
/// \brief Copy the node cross references into the given map.
|
524 |
524 |
///
|
525 |
525 |
/// This function copies the node cross references (reverse references)
|
526 |
526 |
/// into the given map. The parameter should be a map, whose key type
|
527 |
527 |
/// is the Node type of the destination digraph, while the value type is
|
528 |
528 |
/// the Node type of the source digraph.
|
529 |
529 |
template <typename NodeCrossRef>
|
530 |
530 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
531 |
531 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
|
532 |
532 |
NodeRefMap, NodeCrossRef>(map));
|
533 |
533 |
return *this;
|
534 |
534 |
}
|
535 |
535 |
|
536 |
536 |
/// \brief Make a copy of the given node map.
|
537 |
537 |
///
|
538 |
538 |
/// This function makes a copy of the given node map for the newly
|
539 |
539 |
/// created digraph.
|
540 |
540 |
/// The key type of the new map \c tmap should be the Node type of the
|
541 |
541 |
/// destination digraph, and the key type of the original map \c map
|
542 |
542 |
/// should be the Node type of the source digraph.
|
543 |
543 |
template <typename FromMap, typename ToMap>
|
544 |
544 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
545 |
545 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node,
|
546 |
546 |
NodeRefMap, FromMap, ToMap>(map, tmap));
|
547 |
547 |
return *this;
|
548 |
548 |
}
|
549 |
549 |
|
550 |
550 |
/// \brief Make a copy of the given node.
|
551 |
551 |
///
|
552 |
552 |
/// This function makes a copy of the given node.
|
553 |
553 |
DigraphCopy& node(const Node& node, TNode& tnode) {
|
554 |
554 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node,
|
555 |
555 |
NodeRefMap, TNode>(node, tnode));
|
556 |
556 |
return *this;
|
557 |
557 |
}
|
558 |
558 |
|
559 |
559 |
/// \brief Copy the arc references into the given map.
|
560 |
560 |
///
|
561 |
561 |
/// This function copies the arc references into the given map.
|
562 |
562 |
/// The parameter should be a map, whose key type is the Arc type of
|
563 |
563 |
/// the source digraph, while the value type is the Arc type of the
|
564 |
564 |
/// destination digraph.
|
565 |
565 |
template <typename ArcRef>
|
566 |
566 |
DigraphCopy& arcRef(ArcRef& map) {
|
567 |
567 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
|
568 |
568 |
ArcRefMap, ArcRef>(map));
|
569 |
569 |
return *this;
|
570 |
570 |
}
|
571 |
571 |
|
572 |
572 |
/// \brief Copy the arc cross references into the given map.
|
573 |
573 |
///
|
574 |
574 |
/// This function copies the arc cross references (reverse references)
|
575 |
575 |
/// into the given map. The parameter should be a map, whose key type
|
576 |
576 |
/// is the Arc type of the destination digraph, while the value type is
|
577 |
577 |
/// the Arc type of the source digraph.
|
578 |
578 |
template <typename ArcCrossRef>
|
579 |
579 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
580 |
580 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
|
581 |
581 |
ArcRefMap, ArcCrossRef>(map));
|
582 |
582 |
return *this;
|
583 |
583 |
}
|
584 |
584 |
|
585 |
585 |
/// \brief Make a copy of the given arc map.
|
586 |
586 |
///
|
587 |
587 |
/// This function makes a copy of the given arc map for the newly
|
588 |
588 |
/// created digraph.
|
589 |
589 |
/// The key type of the new map \c tmap should be the Arc type of the
|
590 |
590 |
/// destination digraph, and the key type of the original map \c map
|
591 |
591 |
/// should be the Arc type of the source digraph.
|
592 |
592 |
template <typename FromMap, typename ToMap>
|
593 |
593 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
594 |
594 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
|
595 |
595 |
ArcRefMap, FromMap, ToMap>(map, tmap));
|
596 |
596 |
return *this;
|
597 |
597 |
}
|
598 |
598 |
|
599 |
599 |
/// \brief Make a copy of the given arc.
|
600 |
600 |
///
|
601 |
601 |
/// This function makes a copy of the given arc.
|
602 |
602 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) {
|
603 |
603 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
|
604 |
604 |
ArcRefMap, TArc>(arc, tarc));
|
605 |
605 |
return *this;
|
606 |
606 |
}
|
607 |
607 |
|
608 |
608 |
/// \brief Execute copying.
|
609 |
609 |
///
|
610 |
610 |
/// This function executes the copying of the digraph along with the
|
611 |
611 |
/// copying of the assigned data.
|
612 |
612 |
void run() {
|
613 |
613 |
NodeRefMap nodeRefMap(_from);
|
614 |
614 |
ArcRefMap arcRefMap(_from);
|
615 |
615 |
_core_bits::DigraphCopySelector<To>::
|
616 |
616 |
copy(_from, _to, nodeRefMap, arcRefMap);
|
617 |
617 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
618 |
618 |
_node_maps[i]->copy(_from, nodeRefMap);
|
619 |
619 |
}
|
620 |
620 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
621 |
621 |
_arc_maps[i]->copy(_from, arcRefMap);
|
622 |
622 |
}
|
623 |
623 |
}
|
624 |
624 |
|
625 |
625 |
protected:
|
626 |
626 |
|
627 |
627 |
const From& _from;
|
628 |
628 |
To& _to;
|
629 |
629 |
|
630 |
630 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
|
631 |
631 |
_node_maps;
|
632 |
632 |
|
633 |
633 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
|
634 |
634 |
_arc_maps;
|
635 |
635 |
|
636 |
636 |
};
|
637 |
637 |
|
638 |
638 |
/// \brief Copy a digraph to another digraph.
|
639 |
639 |
///
|
640 |
640 |
/// This function copies a digraph to another digraph.
|
641 |
641 |
/// The complete usage of it is detailed in the DigraphCopy class, but
|
642 |
642 |
/// a short example shows a basic work:
|
643 |
643 |
///\code
|
644 |
644 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
|
645 |
645 |
///\endcode
|
646 |
646 |
///
|
647 |
647 |
/// After the copy the \c nr map will contain the mapping from the
|
648 |
648 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and
|
649 |
649 |
/// \c acr will contain the mapping from the arcs of the \c to digraph
|
650 |
650 |
/// to the arcs of the \c from digraph.
|
651 |
651 |
///
|
652 |
652 |
/// \see DigraphCopy
|
653 |
653 |
template <typename From, typename To>
|
654 |
654 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
|
655 |
655 |
return DigraphCopy<From, To>(from, to);
|
656 |
656 |
}
|
657 |
657 |
|
658 |
658 |
/// \brief Class to copy a graph.
|
659 |
659 |
///
|
660 |
660 |
/// Class to copy a graph to another graph (duplicate a graph). The
|
661 |
661 |
/// simplest way of using it is through the \c graphCopy() function.
|
662 |
662 |
///
|
663 |
663 |
/// This class not only make a copy of a graph, but it can create
|
664 |
664 |
/// references and cross references between the nodes, edges and arcs of
|
665 |
665 |
/// the two graphs, and it can copy maps for using with the newly created
|
666 |
666 |
/// graph.
|
667 |
667 |
///
|
668 |
668 |
/// To make a copy from a graph, first an instance of GraphCopy
|
669 |
669 |
/// should be created, then the data belongs to the graph should
|
670 |
670 |
/// assigned to copy. In the end, the \c run() member should be
|
671 |
671 |
/// called.
|
672 |
672 |
///
|
673 |
673 |
/// The next code copies a graph with several data:
|
674 |
674 |
///\code
|
675 |
675 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
|
676 |
676 |
/// // Create references for the nodes
|
677 |
677 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
678 |
678 |
/// cg.nodeRef(nr);
|
679 |
679 |
/// // Create cross references (inverse) for the edges
|
680 |
680 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
|
681 |
681 |
/// cg.edgeCrossRef(ecr);
|
682 |
682 |
/// // Copy an edge map
|
683 |
683 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph);
|
684 |
684 |
/// NewGraph::EdgeMap<double> nemap(new_graph);
|
685 |
685 |
/// cg.edgeMap(oemap, nemap);
|
686 |
686 |
/// // Copy a node
|
687 |
687 |
/// OrigGraph::Node on;
|
688 |
688 |
/// NewGraph::Node nn;
|
689 |
689 |
/// cg.node(on, nn);
|
690 |
690 |
/// // Execute copying
|
691 |
691 |
/// cg.run();
|
692 |
692 |
///\endcode
|
693 |
693 |
template <typename From, typename To>
|
694 |
694 |
class GraphCopy {
|
695 |
695 |
private:
|
696 |
696 |
|
697 |
697 |
typedef typename From::Node Node;
|
698 |
698 |
typedef typename From::NodeIt NodeIt;
|
699 |
699 |
typedef typename From::Arc Arc;
|
700 |
700 |
typedef typename From::ArcIt ArcIt;
|
701 |
701 |
typedef typename From::Edge Edge;
|
702 |
702 |
typedef typename From::EdgeIt EdgeIt;
|
703 |
703 |
|
704 |
704 |
typedef typename To::Node TNode;
|
705 |
705 |
typedef typename To::Arc TArc;
|
706 |
706 |
typedef typename To::Edge TEdge;
|
707 |
707 |
|
708 |
708 |
typedef typename From::template NodeMap<TNode> NodeRefMap;
|
709 |
709 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
|
710 |
710 |
|
711 |
711 |
struct ArcRefMap {
|
712 |
712 |
ArcRefMap(const From& from, const To& to,
|
713 |
713 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
|
714 |
714 |
: _from(from), _to(to),
|
715 |
715 |
_edge_ref(edge_ref), _node_ref(node_ref) {}
|
716 |
716 |
|
717 |
717 |
typedef typename From::Arc Key;
|
718 |
718 |
typedef typename To::Arc Value;
|
719 |
719 |
|
720 |
720 |
Value operator[](const Key& key) const {
|
721 |
721 |
bool forward = _from.u(key) != _from.v(key) ?
|
722 |
722 |
_node_ref[_from.source(key)] ==
|
723 |
723 |
_to.source(_to.direct(_edge_ref[key], true)) :
|
724 |
724 |
_from.direction(key);
|
725 |
725 |
return _to.direct(_edge_ref[key], forward);
|
726 |
726 |
}
|
727 |
727 |
|
728 |
728 |
const From& _from;
|
729 |
729 |
const To& _to;
|
730 |
730 |
const EdgeRefMap& _edge_ref;
|
731 |
731 |
const NodeRefMap& _node_ref;
|
732 |
732 |
};
|
733 |
733 |
|
734 |
734 |
public:
|
735 |
735 |
|
736 |
736 |
/// \brief Constructor of GraphCopy.
|
737 |
737 |
///
|
738 |
738 |
/// Constructor of GraphCopy for copying the content of the
|
739 |
739 |
/// \c from graph into the \c to graph.
|
740 |
740 |
GraphCopy(const From& from, To& to)
|
741 |
741 |
: _from(from), _to(to) {}
|
742 |
742 |
|
743 |
743 |
/// \brief Destructor of GraphCopy
|
744 |
744 |
///
|
745 |
745 |
/// Destructor of GraphCopy.
|
746 |
746 |
~GraphCopy() {
|
747 |
747 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
748 |
748 |
delete _node_maps[i];
|
749 |
749 |
}
|
750 |
750 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
751 |
751 |
delete _arc_maps[i];
|
752 |
752 |
}
|
753 |
753 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
754 |
754 |
delete _edge_maps[i];
|
755 |
755 |
}
|
756 |
756 |
}
|
757 |
757 |
|
758 |
758 |
/// \brief Copy the node references into the given map.
|
759 |
759 |
///
|
760 |
760 |
/// This function copies the node references into the given map.
|
761 |
761 |
/// The parameter should be a map, whose key type is the Node type of
|
762 |
762 |
/// the source graph, while the value type is the Node type of the
|
763 |
763 |
/// destination graph.
|
764 |
764 |
template <typename NodeRef>
|
765 |
765 |
GraphCopy& nodeRef(NodeRef& map) {
|
766 |
766 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node,
|
767 |
767 |
NodeRefMap, NodeRef>(map));
|
768 |
768 |
return *this;
|
769 |
769 |
}
|
770 |
770 |
|
771 |
771 |
/// \brief Copy the node cross references into the given map.
|
772 |
772 |
///
|
773 |
773 |
/// This function copies the node cross references (reverse references)
|
774 |
774 |
/// into the given map. The parameter should be a map, whose key type
|
775 |
775 |
/// is the Node type of the destination graph, while the value type is
|
776 |
776 |
/// the Node type of the source graph.
|
777 |
777 |
template <typename NodeCrossRef>
|
778 |
778 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
779 |
779 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
|
780 |
780 |
NodeRefMap, NodeCrossRef>(map));
|
781 |
781 |
return *this;
|
782 |
782 |
}
|
783 |
783 |
|
784 |
784 |
/// \brief Make a copy of the given node map.
|
785 |
785 |
///
|
786 |
786 |
/// This function makes a copy of the given node map for the newly
|
787 |
787 |
/// created graph.
|
788 |
788 |
/// The key type of the new map \c tmap should be the Node type of the
|
789 |
789 |
/// destination graph, and the key type of the original map \c map
|
790 |
790 |
/// should be the Node type of the source graph.
|
791 |
791 |
template <typename FromMap, typename ToMap>
|
792 |
792 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
793 |
793 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node,
|
794 |
794 |
NodeRefMap, FromMap, ToMap>(map, tmap));
|
795 |
795 |
return *this;
|
796 |
796 |
}
|
797 |
797 |
|
798 |
798 |
/// \brief Make a copy of the given node.
|
799 |
799 |
///
|
800 |
800 |
/// This function makes a copy of the given node.
|
801 |
801 |
GraphCopy& node(const Node& node, TNode& tnode) {
|
802 |
802 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node,
|
803 |
803 |
NodeRefMap, TNode>(node, tnode));
|
804 |
804 |
return *this;
|
805 |
805 |
}
|
806 |
806 |
|
807 |
807 |
/// \brief Copy the arc references into the given map.
|
808 |
808 |
///
|
809 |
809 |
/// This function copies the arc references into the given map.
|
810 |
810 |
/// The parameter should be a map, whose key type is the Arc type of
|
811 |
811 |
/// the source graph, while the value type is the Arc type of the
|
812 |
812 |
/// destination graph.
|
813 |
813 |
template <typename ArcRef>
|
814 |
814 |
GraphCopy& arcRef(ArcRef& map) {
|
815 |
815 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
|
816 |
816 |
ArcRefMap, ArcRef>(map));
|
817 |
817 |
return *this;
|
818 |
818 |
}
|
819 |
819 |
|
820 |
820 |
/// \brief Copy the arc cross references into the given map.
|
821 |
821 |
///
|
822 |
822 |
/// This function copies the arc cross references (reverse references)
|
823 |
823 |
/// into the given map. The parameter should be a map, whose key type
|
824 |
824 |
/// is the Arc type of the destination graph, while the value type is
|
825 |
825 |
/// the Arc type of the source graph.
|
826 |
826 |
template <typename ArcCrossRef>
|
827 |
827 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
828 |
828 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
|
829 |
829 |
ArcRefMap, ArcCrossRef>(map));
|
830 |
830 |
return *this;
|
831 |
831 |
}
|
832 |
832 |
|
833 |
833 |
/// \brief Make a copy of the given arc map.
|
834 |
834 |
///
|
835 |
835 |
/// This function makes a copy of the given arc map for the newly
|
836 |
836 |
/// created graph.
|
837 |
837 |
/// The key type of the new map \c tmap should be the Arc type of the
|
838 |
838 |
/// destination graph, and the key type of the original map \c map
|
839 |
839 |
/// should be the Arc type of the source graph.
|
840 |
840 |
template <typename FromMap, typename ToMap>
|
841 |
841 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
842 |
842 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
|
843 |
843 |
ArcRefMap, FromMap, ToMap>(map, tmap));
|
844 |
844 |
return *this;
|
845 |
845 |
}
|
846 |
846 |
|
847 |
847 |
/// \brief Make a copy of the given arc.
|
848 |
848 |
///
|
849 |
849 |
/// This function makes a copy of the given arc.
|
850 |
850 |
GraphCopy& arc(const Arc& arc, TArc& tarc) {
|
851 |
851 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
|
852 |
852 |
ArcRefMap, TArc>(arc, tarc));
|
853 |
853 |
return *this;
|
854 |
854 |
}
|
855 |
855 |
|
856 |
856 |
/// \brief Copy the edge references into the given map.
|
857 |
857 |
///
|
858 |
858 |
/// This function copies the edge references into the given map.
|
859 |
859 |
/// The parameter should be a map, whose key type is the Edge type of
|
860 |
860 |
/// the source graph, while the value type is the Edge type of the
|
861 |
861 |
/// destination graph.
|
862 |
862 |
template <typename EdgeRef>
|
863 |
863 |
GraphCopy& edgeRef(EdgeRef& map) {
|
864 |
864 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
|
865 |
865 |
EdgeRefMap, EdgeRef>(map));
|
866 |
866 |
return *this;
|
867 |
867 |
}
|
868 |
868 |
|
869 |
869 |
/// \brief Copy the edge cross references into the given map.
|
870 |
870 |
///
|
871 |
871 |
/// This function copies the edge cross references (reverse references)
|
872 |
872 |
/// into the given map. The parameter should be a map, whose key type
|
873 |
873 |
/// is the Edge type of the destination graph, while the value type is
|
874 |
874 |
/// the Edge type of the source graph.
|
875 |
875 |
template <typename EdgeCrossRef>
|
876 |
876 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
877 |
877 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From,
|
878 |
878 |
Edge, EdgeRefMap, EdgeCrossRef>(map));
|
879 |
879 |
return *this;
|
880 |
880 |
}
|
881 |
881 |
|
882 |
882 |
/// \brief Make a copy of the given edge map.
|
883 |
883 |
///
|
884 |
884 |
/// This function makes a copy of the given edge map for the newly
|
885 |
885 |
/// created graph.
|
886 |
886 |
/// The key type of the new map \c tmap should be the Edge type of the
|
887 |
887 |
/// destination graph, and the key type of the original map \c map
|
888 |
888 |
/// should be the Edge type of the source graph.
|
889 |
889 |
template <typename FromMap, typename ToMap>
|
890 |
890 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
|
891 |
891 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
|
892 |
892 |
EdgeRefMap, FromMap, ToMap>(map, tmap));
|
893 |
893 |
return *this;
|
894 |
894 |
}
|
895 |
895 |
|
896 |
896 |
/// \brief Make a copy of the given edge.
|
897 |
897 |
///
|
898 |
898 |
/// This function makes a copy of the given edge.
|
899 |
899 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) {
|
900 |
900 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
|
901 |
901 |
EdgeRefMap, TEdge>(edge, tedge));
|
902 |
902 |
return *this;
|
903 |
903 |
}
|
904 |
904 |
|
905 |
905 |
/// \brief Execute copying.
|
906 |
906 |
///
|
907 |
907 |
/// This function executes the copying of the graph along with the
|
908 |
908 |
/// copying of the assigned data.
|
909 |
909 |
void run() {
|
910 |
910 |
NodeRefMap nodeRefMap(_from);
|
911 |
911 |
EdgeRefMap edgeRefMap(_from);
|
912 |
912 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
|
913 |
913 |
_core_bits::GraphCopySelector<To>::
|
914 |
914 |
copy(_from, _to, nodeRefMap, edgeRefMap);
|
915 |
915 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
916 |
916 |
_node_maps[i]->copy(_from, nodeRefMap);
|
917 |
917 |
}
|
918 |
918 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
919 |
919 |
_edge_maps[i]->copy(_from, edgeRefMap);
|
920 |
920 |
}
|
921 |
921 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
922 |
922 |
_arc_maps[i]->copy(_from, arcRefMap);
|
923 |
923 |
}
|
924 |
924 |
}
|
925 |
925 |
|
926 |
926 |
private:
|
927 |
927 |
|
928 |
928 |
const From& _from;
|
929 |
929 |
To& _to;
|
930 |
930 |
|
931 |
931 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
|
932 |
932 |
_node_maps;
|
933 |
933 |
|
934 |
934 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
|
935 |
935 |
_arc_maps;
|
936 |
936 |
|
937 |
937 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
|
938 |
938 |
_edge_maps;
|
939 |
939 |
|
940 |
940 |
};
|
941 |
941 |
|
942 |
942 |
/// \brief Copy a graph to another graph.
|
943 |
943 |
///
|
944 |
944 |
/// This function copies a graph to another graph.
|
945 |
945 |
/// The complete usage of it is detailed in the GraphCopy class,
|
946 |
946 |
/// but a short example shows a basic work:
|
947 |
947 |
///\code
|
948 |
948 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
|
949 |
949 |
///\endcode
|
950 |
950 |
///
|
951 |
951 |
/// After the copy the \c nr map will contain the mapping from the
|
952 |
952 |
/// nodes of the \c from graph to the nodes of the \c to graph and
|
953 |
953 |
/// \c ecr will contain the mapping from the edges of the \c to graph
|
954 |
954 |
/// to the edges of the \c from graph.
|
955 |
955 |
///
|
956 |
956 |
/// \see GraphCopy
|
957 |
957 |
template <typename From, typename To>
|
958 |
958 |
GraphCopy<From, To>
|
959 |
959 |
graphCopy(const From& from, To& to) {
|
960 |
960 |
return GraphCopy<From, To>(from, to);
|
961 |
961 |
}
|
962 |
962 |
|
963 |
963 |
namespace _core_bits {
|
964 |
964 |
|
965 |
965 |
template <typename Graph, typename Enable = void>
|
966 |
966 |
struct FindArcSelector {
|
967 |
967 |
typedef typename Graph::Node Node;
|
968 |
968 |
typedef typename Graph::Arc Arc;
|
969 |
969 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
970 |
970 |
if (e == INVALID) {
|
971 |
971 |
g.firstOut(e, u);
|
972 |
972 |
} else {
|
973 |
973 |
g.nextOut(e);
|
974 |
974 |
}
|
975 |
975 |
while (e != INVALID && g.target(e) != v) {
|
976 |
976 |
g.nextOut(e);
|
977 |
977 |
}
|
978 |
978 |
return e;
|
979 |
979 |
}
|
980 |
980 |
};
|
981 |
981 |
|
982 |
982 |
template <typename Graph>
|
983 |
983 |
struct FindArcSelector<
|
984 |
984 |
Graph,
|
985 |
985 |
typename enable_if<typename Graph::FindArcTag, void>::type>
|
986 |
986 |
{
|
987 |
987 |
typedef typename Graph::Node Node;
|
988 |
988 |
typedef typename Graph::Arc Arc;
|
989 |
989 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
990 |
990 |
return g.findArc(u, v, prev);
|
991 |
991 |
}
|
992 |
992 |
};
|
993 |
993 |
}
|
994 |
994 |
|
995 |
995 |
/// \brief Find an arc between two nodes of a digraph.
|
996 |
996 |
///
|
997 |
997 |
/// This function finds an arc from node \c u to node \c v in the
|
998 |
998 |
/// digraph \c g.
|
999 |
999 |
///
|
1000 |
1000 |
/// If \c prev is \ref INVALID (this is the default value), then
|
1001 |
1001 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for
|
1002 |
1002 |
/// the next arc from \c u to \c v after \c prev.
|
1003 |
1003 |
/// \return The found arc or \ref INVALID if there is no such an arc.
|
1004 |
1004 |
///
|
1005 |
1005 |
/// Thus you can iterate through each arc from \c u to \c v as it follows.
|
1006 |
1006 |
///\code
|
1007 |
1007 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
|
1008 |
1008 |
/// ...
|
1009 |
1009 |
/// }
|
1010 |
1010 |
///\endcode
|
1011 |
1011 |
///
|
1012 |
1012 |
/// \note \ref ConArcIt provides iterator interface for the same
|
1013 |
1013 |
/// functionality.
|
1014 |
1014 |
///
|
1015 |
1015 |
///\sa ConArcIt
|
1016 |
1016 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
|
1017 |
1017 |
template <typename Graph>
|
1018 |
1018 |
inline typename Graph::Arc
|
1019 |
1019 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
1020 |
1020 |
typename Graph::Arc prev = INVALID) {
|
1021 |
1021 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
|
1022 |
1022 |
}
|
1023 |
1023 |
|
1024 |
1024 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes.
|
1025 |
1025 |
///
|
1026 |
1026 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is
|
1027 |
1027 |
/// a higher level interface for the \ref findArc() function. You can
|
1028 |
1028 |
/// use it the following way:
|
1029 |
1029 |
///\code
|
1030 |
1030 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
1031 |
1031 |
/// ...
|
1032 |
1032 |
/// }
|
1033 |
1033 |
///\endcode
|
1034 |
1034 |
///
|
1035 |
1035 |
///\sa findArc()
|
1036 |
1036 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
|
1037 |
1037 |
template <typename _Graph>
|
1038 |
1038 |
class ConArcIt : public _Graph::Arc {
|
1039 |
1039 |
public:
|
1040 |
1040 |
|
1041 |
1041 |
typedef _Graph Graph;
|
1042 |
1042 |
typedef typename Graph::Arc Parent;
|
1043 |
1043 |
|
1044 |
1044 |
typedef typename Graph::Arc Arc;
|
1045 |
1045 |
typedef typename Graph::Node Node;
|
1046 |
1046 |
|
1047 |
1047 |
/// \brief Constructor.
|
1048 |
1048 |
///
|
1049 |
1049 |
/// Construct a new ConArcIt iterating on the arcs that
|