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