klao@946
|
1 |
/* -*- C++ -*-
|
klao@946
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2391
|
5 |
* Copyright (C) 2003-2007
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
klao@946
|
8 |
*
|
klao@946
|
9 |
* Permission to use, modify and distribute this software is granted
|
klao@946
|
10 |
* provided that this copyright notice appears in all copies. For
|
klao@946
|
11 |
* precise terms see the accompanying LICENSE file.
|
klao@946
|
12 |
*
|
klao@946
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
klao@946
|
14 |
* express or implied, and with no claim as to its suitability for any
|
klao@946
|
15 |
* purpose.
|
klao@946
|
16 |
*
|
klao@946
|
17 |
*/
|
klao@946
|
18 |
|
klao@946
|
19 |
#ifndef LEMON_GRAPH_UTILS_H
|
klao@946
|
20 |
#define LEMON_GRAPH_UTILS_H
|
klao@946
|
21 |
|
klao@946
|
22 |
#include <iterator>
|
deba@1419
|
23 |
#include <vector>
|
alpar@1402
|
24 |
#include <map>
|
deba@1695
|
25 |
#include <cmath>
|
alpar@2235
|
26 |
#include <algorithm>
|
klao@946
|
27 |
|
deba@1993
|
28 |
#include <lemon/bits/invalid.h>
|
deba@1993
|
29 |
#include <lemon/bits/utility.h>
|
deba@1413
|
30 |
#include <lemon/maps.h>
|
deba@1993
|
31 |
#include <lemon/bits/traits.h>
|
deba@1990
|
32 |
|
alpar@1459
|
33 |
#include <lemon/bits/alteration_notifier.h>
|
deba@1990
|
34 |
#include <lemon/bits/default_map.h>
|
klao@946
|
35 |
|
alpar@947
|
36 |
///\ingroup gutils
|
klao@946
|
37 |
///\file
|
alpar@947
|
38 |
///\brief Graph utilities.
|
klao@946
|
39 |
|
klao@946
|
40 |
namespace lemon {
|
klao@946
|
41 |
|
deba@1267
|
42 |
/// \addtogroup gutils
|
deba@1267
|
43 |
/// @{
|
alpar@947
|
44 |
|
alpar@1756
|
45 |
///Creates convenience typedefs for the graph types and iterators
|
alpar@1756
|
46 |
|
alpar@1756
|
47 |
///This \c \#define creates convenience typedefs for the following types
|
alpar@1756
|
48 |
///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
|
deba@2031
|
49 |
///\c OutEdgeIt
|
alpar@1756
|
50 |
///\note If \c G it a template parameter, it should be used in this way.
|
alpar@1756
|
51 |
///\code
|
alpar@1756
|
52 |
/// GRAPH_TYPEDEFS(typename G)
|
alpar@1756
|
53 |
///\endcode
|
alpar@1756
|
54 |
///
|
alpar@1756
|
55 |
///\warning There are no typedefs for the graph maps because of the lack of
|
alpar@1756
|
56 |
///template typedefs in C++.
|
alpar@1804
|
57 |
#define GRAPH_TYPEDEFS(Graph) \
|
alpar@1804
|
58 |
typedef Graph:: Node Node; \
|
alpar@1804
|
59 |
typedef Graph:: NodeIt NodeIt; \
|
alpar@1804
|
60 |
typedef Graph:: Edge Edge; \
|
alpar@1804
|
61 |
typedef Graph:: EdgeIt EdgeIt; \
|
alpar@1804
|
62 |
typedef Graph:: InEdgeIt InEdgeIt; \
|
alpar@1811
|
63 |
typedef Graph::OutEdgeIt OutEdgeIt;
|
deba@2031
|
64 |
|
alpar@1756
|
65 |
///Creates convenience typedefs for the undirected graph types and iterators
|
alpar@1756
|
66 |
|
alpar@1756
|
67 |
///This \c \#define creates the same convenience typedefs as defined by
|
alpar@1756
|
68 |
///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
|
klao@1909
|
69 |
///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
|
alpar@1756
|
70 |
///
|
alpar@1756
|
71 |
///\note If \c G it a template parameter, it should be used in this way.
|
alpar@1756
|
72 |
///\code
|
deba@1992
|
73 |
/// UGRAPH_TYPEDEFS(typename G)
|
alpar@1756
|
74 |
///\endcode
|
alpar@1756
|
75 |
///
|
alpar@1756
|
76 |
///\warning There are no typedefs for the graph maps because of the lack of
|
alpar@1756
|
77 |
///template typedefs in C++.
|
deba@1992
|
78 |
#define UGRAPH_TYPEDEFS(Graph) \
|
alpar@1804
|
79 |
GRAPH_TYPEDEFS(Graph) \
|
klao@1909
|
80 |
typedef Graph:: UEdge UEdge; \
|
klao@1909
|
81 |
typedef Graph:: UEdgeIt UEdgeIt; \
|
alpar@1811
|
82 |
typedef Graph:: IncEdgeIt IncEdgeIt;
|
alpar@1756
|
83 |
|
deba@2031
|
84 |
///\brief Creates convenience typedefs for the bipartite undirected graph
|
deba@2031
|
85 |
///types and iterators
|
deba@2031
|
86 |
|
deba@2031
|
87 |
///This \c \#define creates the same convenience typedefs as defined by
|
deba@2031
|
88 |
///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
|
deba@2031
|
89 |
///\c ANodeIt, \c BNodeIt,
|
deba@2031
|
90 |
///
|
deba@2031
|
91 |
///\note If \c G it a template parameter, it should be used in this way.
|
deba@2031
|
92 |
///\code
|
deba@2031
|
93 |
/// BPUGRAPH_TYPEDEFS(typename G)
|
deba@2031
|
94 |
///\endcode
|
deba@2031
|
95 |
///
|
deba@2031
|
96 |
///\warning There are no typedefs for the graph maps because of the lack of
|
deba@2031
|
97 |
///template typedefs in C++.
|
deba@2031
|
98 |
#define BPUGRAPH_TYPEDEFS(Graph) \
|
deba@2031
|
99 |
UGRAPH_TYPEDEFS(Graph) \
|
deba@2286
|
100 |
typedef Graph::ANode ANode; \
|
deba@2286
|
101 |
typedef Graph::BNode BNode; \
|
deba@2031
|
102 |
typedef Graph::ANodeIt ANodeIt; \
|
deba@2031
|
103 |
typedef Graph::BNodeIt BNodeIt;
|
alpar@1756
|
104 |
|
klao@946
|
105 |
/// \brief Function to count the items in the graph.
|
klao@946
|
106 |
///
|
athos@1540
|
107 |
/// This function counts the items (nodes, edges etc) in the graph.
|
klao@946
|
108 |
/// The complexity of the function is O(n) because
|
klao@946
|
109 |
/// it iterates on all of the items.
|
klao@946
|
110 |
|
deba@2020
|
111 |
template <typename Graph, typename Item>
|
klao@977
|
112 |
inline int countItems(const Graph& g) {
|
deba@2020
|
113 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
klao@946
|
114 |
int num = 0;
|
klao@977
|
115 |
for (ItemIt it(g); it != INVALID; ++it) {
|
klao@946
|
116 |
++num;
|
klao@946
|
117 |
}
|
klao@946
|
118 |
return num;
|
klao@946
|
119 |
}
|
klao@946
|
120 |
|
klao@977
|
121 |
// Node counting:
|
klao@977
|
122 |
|
deba@2020
|
123 |
namespace _graph_utils_bits {
|
deba@2020
|
124 |
|
deba@2020
|
125 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
126 |
struct CountNodesSelector {
|
deba@2020
|
127 |
static int count(const Graph &g) {
|
deba@2020
|
128 |
return countItems<Graph, typename Graph::Node>(g);
|
deba@2020
|
129 |
}
|
deba@2020
|
130 |
};
|
klao@977
|
131 |
|
deba@2020
|
132 |
template <typename Graph>
|
deba@2020
|
133 |
struct CountNodesSelector<
|
deba@2020
|
134 |
Graph, typename
|
deba@2020
|
135 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2020
|
136 |
{
|
deba@2020
|
137 |
static int count(const Graph &g) {
|
deba@2020
|
138 |
return g.nodeNum();
|
deba@2020
|
139 |
}
|
deba@2020
|
140 |
};
|
klao@977
|
141 |
}
|
klao@977
|
142 |
|
klao@946
|
143 |
/// \brief Function to count the nodes in the graph.
|
klao@946
|
144 |
///
|
klao@946
|
145 |
/// This function counts the nodes in the graph.
|
klao@946
|
146 |
/// The complexity of the function is O(n) but for some
|
athos@1526
|
147 |
/// graph structures it is specialized to run in O(1).
|
klao@977
|
148 |
///
|
kpeter@2474
|
149 |
/// \todo Refer how to specialize it.
|
klao@946
|
150 |
|
klao@946
|
151 |
template <typename Graph>
|
klao@977
|
152 |
inline int countNodes(const Graph& g) {
|
deba@2020
|
153 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
|
klao@977
|
154 |
}
|
klao@977
|
155 |
|
deba@2029
|
156 |
namespace _graph_utils_bits {
|
deba@2029
|
157 |
|
deba@2029
|
158 |
template <typename Graph, typename Enable = void>
|
deba@2029
|
159 |
struct CountANodesSelector {
|
deba@2029
|
160 |
static int count(const Graph &g) {
|
deba@2029
|
161 |
return countItems<Graph, typename Graph::ANode>(g);
|
deba@2029
|
162 |
}
|
deba@2029
|
163 |
};
|
deba@2029
|
164 |
|
deba@2029
|
165 |
template <typename Graph>
|
deba@2029
|
166 |
struct CountANodesSelector<
|
deba@2029
|
167 |
Graph, typename
|
deba@2029
|
168 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2029
|
169 |
{
|
deba@2029
|
170 |
static int count(const Graph &g) {
|
deba@2186
|
171 |
return g.aNodeNum();
|
deba@2029
|
172 |
}
|
deba@2029
|
173 |
};
|
deba@2029
|
174 |
}
|
deba@2029
|
175 |
|
deba@2029
|
176 |
/// \brief Function to count the anodes in the graph.
|
deba@2029
|
177 |
///
|
deba@2029
|
178 |
/// This function counts the anodes in the graph.
|
deba@2029
|
179 |
/// The complexity of the function is O(an) but for some
|
deba@2029
|
180 |
/// graph structures it is specialized to run in O(1).
|
deba@2029
|
181 |
///
|
kpeter@2474
|
182 |
/// \todo Refer how to specialize it.
|
deba@2029
|
183 |
|
deba@2029
|
184 |
template <typename Graph>
|
deba@2029
|
185 |
inline int countANodes(const Graph& g) {
|
deba@2029
|
186 |
return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
|
deba@2029
|
187 |
}
|
deba@2029
|
188 |
|
deba@2029
|
189 |
namespace _graph_utils_bits {
|
deba@2029
|
190 |
|
deba@2029
|
191 |
template <typename Graph, typename Enable = void>
|
deba@2029
|
192 |
struct CountBNodesSelector {
|
deba@2029
|
193 |
static int count(const Graph &g) {
|
deba@2029
|
194 |
return countItems<Graph, typename Graph::BNode>(g);
|
deba@2029
|
195 |
}
|
deba@2029
|
196 |
};
|
deba@2029
|
197 |
|
deba@2029
|
198 |
template <typename Graph>
|
deba@2029
|
199 |
struct CountBNodesSelector<
|
deba@2029
|
200 |
Graph, typename
|
deba@2029
|
201 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2029
|
202 |
{
|
deba@2029
|
203 |
static int count(const Graph &g) {
|
deba@2186
|
204 |
return g.bNodeNum();
|
deba@2029
|
205 |
}
|
deba@2029
|
206 |
};
|
deba@2029
|
207 |
}
|
deba@2029
|
208 |
|
deba@2029
|
209 |
/// \brief Function to count the bnodes in the graph.
|
deba@2029
|
210 |
///
|
deba@2029
|
211 |
/// This function counts the bnodes in the graph.
|
deba@2029
|
212 |
/// The complexity of the function is O(bn) but for some
|
deba@2029
|
213 |
/// graph structures it is specialized to run in O(1).
|
deba@2029
|
214 |
///
|
kpeter@2474
|
215 |
/// \todo Refer how to specialize it.
|
deba@2029
|
216 |
|
deba@2029
|
217 |
template <typename Graph>
|
deba@2029
|
218 |
inline int countBNodes(const Graph& g) {
|
deba@2029
|
219 |
return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
|
deba@2029
|
220 |
}
|
deba@2029
|
221 |
|
deba@2020
|
222 |
|
klao@977
|
223 |
// Edge counting:
|
klao@977
|
224 |
|
deba@2020
|
225 |
namespace _graph_utils_bits {
|
deba@2020
|
226 |
|
deba@2020
|
227 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
228 |
struct CountEdgesSelector {
|
deba@2020
|
229 |
static int count(const Graph &g) {
|
deba@2020
|
230 |
return countItems<Graph, typename Graph::Edge>(g);
|
deba@2020
|
231 |
}
|
deba@2020
|
232 |
};
|
klao@977
|
233 |
|
deba@2020
|
234 |
template <typename Graph>
|
deba@2020
|
235 |
struct CountEdgesSelector<
|
deba@2020
|
236 |
Graph,
|
deba@2020
|
237 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
deba@2020
|
238 |
{
|
deba@2020
|
239 |
static int count(const Graph &g) {
|
deba@2020
|
240 |
return g.edgeNum();
|
deba@2020
|
241 |
}
|
deba@2020
|
242 |
};
|
klao@946
|
243 |
}
|
klao@946
|
244 |
|
klao@946
|
245 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
246 |
///
|
klao@946
|
247 |
/// This function counts the edges in the graph.
|
klao@946
|
248 |
/// The complexity of the function is O(e) but for some
|
athos@1526
|
249 |
/// graph structures it is specialized to run in O(1).
|
klao@977
|
250 |
|
klao@946
|
251 |
template <typename Graph>
|
klao@977
|
252 |
inline int countEdges(const Graph& g) {
|
deba@2020
|
253 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
|
klao@946
|
254 |
}
|
klao@946
|
255 |
|
klao@1053
|
256 |
// Undirected edge counting:
|
deba@2020
|
257 |
namespace _graph_utils_bits {
|
deba@2020
|
258 |
|
deba@2020
|
259 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
260 |
struct CountUEdgesSelector {
|
deba@2020
|
261 |
static int count(const Graph &g) {
|
deba@2020
|
262 |
return countItems<Graph, typename Graph::UEdge>(g);
|
deba@2020
|
263 |
}
|
deba@2020
|
264 |
};
|
klao@1053
|
265 |
|
deba@2020
|
266 |
template <typename Graph>
|
deba@2020
|
267 |
struct CountUEdgesSelector<
|
deba@2020
|
268 |
Graph,
|
deba@2020
|
269 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
deba@2020
|
270 |
{
|
deba@2020
|
271 |
static int count(const Graph &g) {
|
deba@2020
|
272 |
return g.uEdgeNum();
|
deba@2020
|
273 |
}
|
deba@2020
|
274 |
};
|
klao@1053
|
275 |
}
|
klao@1053
|
276 |
|
athos@1526
|
277 |
/// \brief Function to count the undirected edges in the graph.
|
klao@946
|
278 |
///
|
athos@1526
|
279 |
/// This function counts the undirected edges in the graph.
|
klao@946
|
280 |
/// The complexity of the function is O(e) but for some
|
athos@1540
|
281 |
/// graph structures it is specialized to run in O(1).
|
klao@1053
|
282 |
|
klao@946
|
283 |
template <typename Graph>
|
klao@1909
|
284 |
inline int countUEdges(const Graph& g) {
|
deba@2020
|
285 |
return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
|
deba@2020
|
286 |
|
klao@946
|
287 |
}
|
klao@946
|
288 |
|
klao@977
|
289 |
|
klao@946
|
290 |
template <typename Graph, typename DegIt>
|
klao@946
|
291 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
292 |
int num = 0;
|
klao@946
|
293 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
klao@946
|
294 |
++num;
|
klao@946
|
295 |
}
|
klao@946
|
296 |
return num;
|
klao@946
|
297 |
}
|
alpar@967
|
298 |
|
deba@1531
|
299 |
/// \brief Function to count the number of the out-edges from node \c n.
|
deba@1531
|
300 |
///
|
deba@1531
|
301 |
/// This function counts the number of the out-edges from node \c n
|
deba@1531
|
302 |
/// in the graph.
|
deba@1531
|
303 |
template <typename Graph>
|
deba@1531
|
304 |
inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1531
|
305 |
return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
|
deba@1531
|
306 |
}
|
deba@1531
|
307 |
|
deba@1531
|
308 |
/// \brief Function to count the number of the in-edges to node \c n.
|
deba@1531
|
309 |
///
|
deba@1531
|
310 |
/// This function counts the number of the in-edges to node \c n
|
deba@1531
|
311 |
/// in the graph.
|
deba@1531
|
312 |
template <typename Graph>
|
deba@1531
|
313 |
inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1531
|
314 |
return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
|
deba@1531
|
315 |
}
|
deba@1531
|
316 |
|
deba@1704
|
317 |
/// \brief Function to count the number of the inc-edges to node \c n.
|
deba@1679
|
318 |
///
|
deba@1704
|
319 |
/// This function counts the number of the inc-edges to node \c n
|
deba@1679
|
320 |
/// in the graph.
|
deba@1679
|
321 |
template <typename Graph>
|
deba@1679
|
322 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1679
|
323 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
|
deba@1679
|
324 |
}
|
deba@1679
|
325 |
|
deba@2020
|
326 |
namespace _graph_utils_bits {
|
deba@2020
|
327 |
|
deba@2020
|
328 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
329 |
struct FindEdgeSelector {
|
deba@2020
|
330 |
typedef typename Graph::Node Node;
|
deba@2020
|
331 |
typedef typename Graph::Edge Edge;
|
deba@2020
|
332 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
deba@2020
|
333 |
if (e == INVALID) {
|
deba@2020
|
334 |
g.firstOut(e, u);
|
deba@2020
|
335 |
} else {
|
deba@2020
|
336 |
g.nextOut(e);
|
deba@2020
|
337 |
}
|
deba@2020
|
338 |
while (e != INVALID && g.target(e) != v) {
|
deba@2020
|
339 |
g.nextOut(e);
|
deba@2020
|
340 |
}
|
deba@2020
|
341 |
return e;
|
deba@2020
|
342 |
}
|
deba@2020
|
343 |
};
|
deba@1531
|
344 |
|
deba@2020
|
345 |
template <typename Graph>
|
deba@2020
|
346 |
struct FindEdgeSelector<
|
deba@2020
|
347 |
Graph,
|
deba@2020
|
348 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
deba@2020
|
349 |
{
|
deba@2020
|
350 |
typedef typename Graph::Node Node;
|
deba@2020
|
351 |
typedef typename Graph::Edge Edge;
|
deba@2020
|
352 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
deba@2020
|
353 |
return g.findEdge(u, v, prev);
|
deba@2020
|
354 |
}
|
deba@2020
|
355 |
};
|
deba@1565
|
356 |
}
|
deba@1565
|
357 |
|
deba@1565
|
358 |
/// \brief Finds an edge between two nodes of a graph.
|
deba@1565
|
359 |
///
|
alpar@967
|
360 |
/// Finds an edge from node \c u to node \c v in graph \c g.
|
alpar@967
|
361 |
///
|
alpar@967
|
362 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@967
|
363 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@967
|
364 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@967
|
365 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
alpar@967
|
366 |
///
|
alpar@967
|
367 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@1946
|
368 |
///\code
|
alpar@967
|
369 |
/// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
|
alpar@967
|
370 |
/// ...
|
alpar@967
|
371 |
/// }
|
alpar@1946
|
372 |
///\endcode
|
alpar@2155
|
373 |
///
|
alpar@2235
|
374 |
///\sa EdgeLookUp
|
kpeter@2476
|
375 |
///\sa AllEdgeLookUp
|
alpar@2155
|
376 |
///\sa ConEdgeIt
|
alpar@967
|
377 |
template <typename Graph>
|
deba@2286
|
378 |
inline typename Graph::Edge
|
deba@2286
|
379 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
deba@2286
|
380 |
typename Graph::Edge prev = INVALID) {
|
deba@2020
|
381 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
|
alpar@967
|
382 |
}
|
deba@1531
|
383 |
|
deba@1565
|
384 |
/// \brief Iterator for iterating on edges connected the same nodes.
|
deba@1565
|
385 |
///
|
deba@1565
|
386 |
/// Iterator for iterating on edges connected the same nodes. It is
|
deba@1565
|
387 |
/// higher level interface for the findEdge() function. You can
|
alpar@1591
|
388 |
/// use it the following way:
|
alpar@1946
|
389 |
///\code
|
deba@1565
|
390 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
deba@1565
|
391 |
/// ...
|
deba@1565
|
392 |
/// }
|
alpar@1946
|
393 |
///\endcode
|
alpar@2155
|
394 |
///
|
alpar@2155
|
395 |
///\sa findEdge()
|
alpar@2235
|
396 |
///\sa EdgeLookUp
|
kpeter@2474
|
397 |
///\sa AllEdgeLookUp
|
deba@1565
|
398 |
///
|
deba@1565
|
399 |
/// \author Balazs Dezso
|
deba@1565
|
400 |
template <typename _Graph>
|
deba@1565
|
401 |
class ConEdgeIt : public _Graph::Edge {
|
deba@1565
|
402 |
public:
|
deba@1565
|
403 |
|
deba@1565
|
404 |
typedef _Graph Graph;
|
deba@1565
|
405 |
typedef typename Graph::Edge Parent;
|
deba@1565
|
406 |
|
deba@1565
|
407 |
typedef typename Graph::Edge Edge;
|
deba@1565
|
408 |
typedef typename Graph::Node Node;
|
deba@1565
|
409 |
|
deba@1565
|
410 |
/// \brief Constructor.
|
deba@1565
|
411 |
///
|
deba@1565
|
412 |
/// Construct a new ConEdgeIt iterating on the edges which
|
deba@1565
|
413 |
/// connects the \c u and \c v node.
|
deba@1565
|
414 |
ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
|
deba@1565
|
415 |
Parent::operator=(findEdge(graph, u, v));
|
deba@1565
|
416 |
}
|
deba@1565
|
417 |
|
deba@1565
|
418 |
/// \brief Constructor.
|
deba@1565
|
419 |
///
|
deba@1565
|
420 |
/// Construct a new ConEdgeIt which continues the iterating from
|
deba@1565
|
421 |
/// the \c e edge.
|
deba@1565
|
422 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
|
deba@1565
|
423 |
|
deba@1565
|
424 |
/// \brief Increment operator.
|
deba@1565
|
425 |
///
|
deba@1565
|
426 |
/// It increments the iterator and gives back the next edge.
|
deba@1565
|
427 |
ConEdgeIt& operator++() {
|
deba@1565
|
428 |
Parent::operator=(findEdge(graph, graph.source(*this),
|
deba@1565
|
429 |
graph.target(*this), *this));
|
deba@1565
|
430 |
return *this;
|
deba@1565
|
431 |
}
|
deba@1565
|
432 |
private:
|
deba@1565
|
433 |
const Graph& graph;
|
deba@1565
|
434 |
};
|
deba@1565
|
435 |
|
deba@2020
|
436 |
namespace _graph_utils_bits {
|
deba@2020
|
437 |
|
deba@2020
|
438 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
439 |
struct FindUEdgeSelector {
|
deba@2020
|
440 |
typedef typename Graph::Node Node;
|
deba@2020
|
441 |
typedef typename Graph::UEdge UEdge;
|
deba@2020
|
442 |
static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
|
deba@2020
|
443 |
bool b;
|
deba@2020
|
444 |
if (u != v) {
|
deba@2020
|
445 |
if (e == INVALID) {
|
deba@2031
|
446 |
g.firstInc(e, b, u);
|
deba@2020
|
447 |
} else {
|
deba@2020
|
448 |
b = g.source(e) == u;
|
deba@2020
|
449 |
g.nextInc(e, b);
|
deba@2020
|
450 |
}
|
deba@2064
|
451 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
|
deba@2020
|
452 |
g.nextInc(e, b);
|
deba@2020
|
453 |
}
|
deba@2020
|
454 |
} else {
|
deba@2020
|
455 |
if (e == INVALID) {
|
deba@2031
|
456 |
g.firstInc(e, b, u);
|
deba@2020
|
457 |
} else {
|
deba@2020
|
458 |
b = true;
|
deba@2020
|
459 |
g.nextInc(e, b);
|
deba@2020
|
460 |
}
|
deba@2020
|
461 |
while (e != INVALID && (!b || g.target(e) != v)) {
|
deba@2020
|
462 |
g.nextInc(e, b);
|
deba@2020
|
463 |
}
|
deba@2020
|
464 |
}
|
deba@2020
|
465 |
return e;
|
deba@2020
|
466 |
}
|
deba@2020
|
467 |
};
|
deba@1704
|
468 |
|
deba@2020
|
469 |
template <typename Graph>
|
deba@2020
|
470 |
struct FindUEdgeSelector<
|
deba@2020
|
471 |
Graph,
|
deba@2020
|
472 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
deba@2020
|
473 |
{
|
deba@2020
|
474 |
typedef typename Graph::Node Node;
|
deba@2020
|
475 |
typedef typename Graph::UEdge UEdge;
|
deba@2020
|
476 |
static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
|
deba@2020
|
477 |
return g.findUEdge(u, v, prev);
|
deba@2020
|
478 |
}
|
deba@2020
|
479 |
};
|
deba@1704
|
480 |
}
|
deba@1704
|
481 |
|
klao@1909
|
482 |
/// \brief Finds an uedge between two nodes of a graph.
|
deba@1704
|
483 |
///
|
klao@1909
|
484 |
/// Finds an uedge from node \c u to node \c v in graph \c g.
|
deba@2020
|
485 |
/// If the node \c u and node \c v is equal then each loop edge
|
deba@2020
|
486 |
/// will be enumerated.
|
deba@1704
|
487 |
///
|
deba@1704
|
488 |
/// If \c prev is \ref INVALID (this is the default value), then
|
deba@1704
|
489 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
deba@1704
|
490 |
/// the next edge from \c u to \c v after \c prev.
|
deba@1704
|
491 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
deba@1704
|
492 |
///
|
deba@1704
|
493 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@1946
|
494 |
///\code
|
klao@1909
|
495 |
/// for(UEdge e = findUEdge(g,u,v); e != INVALID;
|
klao@1909
|
496 |
/// e = findUEdge(g,u,v,e)) {
|
deba@1704
|
497 |
/// ...
|
deba@1704
|
498 |
/// }
|
alpar@1946
|
499 |
///\endcode
|
alpar@2155
|
500 |
///
|
alpar@2155
|
501 |
///\sa ConEdgeIt
|
alpar@2155
|
502 |
|
deba@1704
|
503 |
template <typename Graph>
|
deba@2286
|
504 |
inline typename Graph::UEdge
|
deba@2286
|
505 |
findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
deba@2286
|
506 |
typename Graph::UEdge p = INVALID) {
|
deba@2031
|
507 |
return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
|
deba@1704
|
508 |
}
|
deba@1704
|
509 |
|
klao@1909
|
510 |
/// \brief Iterator for iterating on uedges connected the same nodes.
|
deba@1704
|
511 |
///
|
klao@1909
|
512 |
/// Iterator for iterating on uedges connected the same nodes. It is
|
klao@1909
|
513 |
/// higher level interface for the findUEdge() function. You can
|
deba@1704
|
514 |
/// use it the following way:
|
alpar@1946
|
515 |
///\code
|
klao@1909
|
516 |
/// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
deba@1704
|
517 |
/// ...
|
deba@1704
|
518 |
/// }
|
alpar@1946
|
519 |
///\endcode
|
deba@1704
|
520 |
///
|
alpar@2155
|
521 |
///\sa findUEdge()
|
alpar@2155
|
522 |
///
|
deba@1704
|
523 |
/// \author Balazs Dezso
|
deba@1704
|
524 |
template <typename _Graph>
|
klao@1909
|
525 |
class ConUEdgeIt : public _Graph::UEdge {
|
deba@1704
|
526 |
public:
|
deba@1704
|
527 |
|
deba@1704
|
528 |
typedef _Graph Graph;
|
klao@1909
|
529 |
typedef typename Graph::UEdge Parent;
|
deba@1704
|
530 |
|
klao@1909
|
531 |
typedef typename Graph::UEdge UEdge;
|
deba@1704
|
532 |
typedef typename Graph::Node Node;
|
deba@1704
|
533 |
|
deba@1704
|
534 |
/// \brief Constructor.
|
deba@1704
|
535 |
///
|
klao@1909
|
536 |
/// Construct a new ConUEdgeIt iterating on the edges which
|
deba@1704
|
537 |
/// connects the \c u and \c v node.
|
klao@1909
|
538 |
ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
|
klao@1909
|
539 |
Parent::operator=(findUEdge(graph, u, v));
|
deba@1704
|
540 |
}
|
deba@1704
|
541 |
|
deba@1704
|
542 |
/// \brief Constructor.
|
deba@1704
|
543 |
///
|
klao@1909
|
544 |
/// Construct a new ConUEdgeIt which continues the iterating from
|
deba@1704
|
545 |
/// the \c e edge.
|
klao@1909
|
546 |
ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
|
deba@1704
|
547 |
|
deba@1704
|
548 |
/// \brief Increment operator.
|
deba@1704
|
549 |
///
|
deba@1704
|
550 |
/// It increments the iterator and gives back the next edge.
|
klao@1909
|
551 |
ConUEdgeIt& operator++() {
|
klao@1909
|
552 |
Parent::operator=(findUEdge(graph, graph.source(*this),
|
deba@1829
|
553 |
graph.target(*this), *this));
|
deba@1704
|
554 |
return *this;
|
deba@1704
|
555 |
}
|
deba@1704
|
556 |
private:
|
deba@1704
|
557 |
const Graph& graph;
|
deba@1704
|
558 |
};
|
deba@1704
|
559 |
|
athos@1540
|
560 |
/// \brief Copy a map.
|
alpar@964
|
561 |
///
|
alpar@1547
|
562 |
/// This function copies the \c source map to the \c target map. It uses the
|
athos@1540
|
563 |
/// given iterator to iterate on the data structure and it uses the \c ref
|
athos@1540
|
564 |
/// mapping to convert the source's keys to the target's keys.
|
deba@1531
|
565 |
template <typename Target, typename Source,
|
deba@1531
|
566 |
typename ItemIt, typename Ref>
|
deba@1531
|
567 |
void copyMap(Target& target, const Source& source,
|
deba@1531
|
568 |
ItemIt it, const Ref& ref) {
|
deba@1531
|
569 |
for (; it != INVALID; ++it) {
|
deba@1531
|
570 |
target[ref[it]] = source[it];
|
klao@946
|
571 |
}
|
klao@946
|
572 |
}
|
klao@946
|
573 |
|
deba@1531
|
574 |
/// \brief Copy the source map to the target map.
|
deba@1531
|
575 |
///
|
deba@1531
|
576 |
/// Copy the \c source map to the \c target map. It uses the given iterator
|
deba@1531
|
577 |
/// to iterate on the data structure.
|
deba@1830
|
578 |
template <typename Target, typename Source, typename ItemIt>
|
deba@1531
|
579 |
void copyMap(Target& target, const Source& source, ItemIt it) {
|
deba@1531
|
580 |
for (; it != INVALID; ++it) {
|
deba@1531
|
581 |
target[it] = source[it];
|
klao@946
|
582 |
}
|
klao@946
|
583 |
}
|
klao@946
|
584 |
|
deba@2286
|
585 |
namespace _graph_utils_bits {
|
deba@2286
|
586 |
|
deba@2286
|
587 |
template <typename Graph, typename Item, typename RefMap>
|
deba@2286
|
588 |
class MapCopyBase {
|
deba@2286
|
589 |
public:
|
deba@2286
|
590 |
virtual void copy(const Graph& source, const RefMap& refMap) = 0;
|
deba@2286
|
591 |
|
deba@2286
|
592 |
virtual ~MapCopyBase() {}
|
deba@2286
|
593 |
};
|
deba@2286
|
594 |
|
deba@2286
|
595 |
template <typename Graph, typename Item, typename RefMap,
|
deba@2286
|
596 |
typename TargetMap, typename SourceMap>
|
deba@2286
|
597 |
class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
|
deba@2286
|
598 |
public:
|
deba@2286
|
599 |
|
deba@2286
|
600 |
MapCopy(TargetMap& tmap, const SourceMap& map)
|
deba@2286
|
601 |
: _tmap(tmap), _map(map) {}
|
deba@2286
|
602 |
|
deba@2286
|
603 |
virtual void copy(const Graph& graph, const RefMap& refMap) {
|
deba@2286
|
604 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
deba@2286
|
605 |
for (ItemIt it(graph); it != INVALID; ++it) {
|
deba@2286
|
606 |
_tmap.set(refMap[it], _map[it]);
|
deba@2286
|
607 |
}
|
deba@2286
|
608 |
}
|
deba@2286
|
609 |
|
deba@2286
|
610 |
private:
|
deba@2286
|
611 |
TargetMap& _tmap;
|
deba@2286
|
612 |
const SourceMap& _map;
|
deba@2286
|
613 |
};
|
deba@2286
|
614 |
|
deba@2290
|
615 |
template <typename Graph, typename Item, typename RefMap, typename It>
|
deba@2290
|
616 |
class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
|
deba@2290
|
617 |
public:
|
deba@2290
|
618 |
|
deba@2290
|
619 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
|
deba@2290
|
620 |
|
deba@2290
|
621 |
virtual void copy(const Graph&, const RefMap& refMap) {
|
deba@2290
|
622 |
_it = refMap[_item];
|
deba@2290
|
623 |
}
|
deba@2290
|
624 |
|
deba@2290
|
625 |
private:
|
deba@2290
|
626 |
It& _it;
|
deba@2290
|
627 |
Item _item;
|
deba@2290
|
628 |
};
|
deba@2290
|
629 |
|
deba@2286
|
630 |
template <typename Graph, typename Item, typename RefMap, typename Ref>
|
deba@2286
|
631 |
class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
|
deba@2286
|
632 |
public:
|
deba@2286
|
633 |
|
deba@2286
|
634 |
RefCopy(Ref& map) : _map(map) {}
|
deba@2286
|
635 |
|
deba@2286
|
636 |
virtual void copy(const Graph& graph, const RefMap& refMap) {
|
deba@2286
|
637 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
deba@2286
|
638 |
for (ItemIt it(graph); it != INVALID; ++it) {
|
deba@2286
|
639 |
_map.set(it, refMap[it]);
|
deba@2286
|
640 |
}
|
deba@2286
|
641 |
}
|
deba@2286
|
642 |
|
deba@2286
|
643 |
private:
|
deba@2286
|
644 |
Ref& _map;
|
deba@2286
|
645 |
};
|
deba@2286
|
646 |
|
deba@2286
|
647 |
template <typename Graph, typename Item, typename RefMap,
|
deba@2286
|
648 |
typename CrossRef>
|
deba@2286
|
649 |
class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
|
deba@2286
|
650 |
public:
|
deba@2286
|
651 |
|
deba@2286
|
652 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
deba@2286
|
653 |
|
deba@2286
|
654 |
virtual void copy(const Graph& graph, const RefMap& refMap) {
|
deba@2286
|
655 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
deba@2286
|
656 |
for (ItemIt it(graph); it != INVALID; ++it) {
|
deba@2286
|
657 |
_cmap.set(refMap[it], it);
|
deba@2286
|
658 |
}
|
deba@2286
|
659 |
}
|
deba@2286
|
660 |
|
deba@2286
|
661 |
private:
|
deba@2286
|
662 |
CrossRef& _cmap;
|
deba@2286
|
663 |
};
|
deba@2286
|
664 |
|
deba@2290
|
665 |
template <typename Graph, typename Enable = void>
|
deba@2290
|
666 |
struct GraphCopySelector {
|
deba@2290
|
667 |
template <typename Source, typename NodeRefMap, typename EdgeRefMap>
|
deba@2290
|
668 |
static void copy(Graph &target, const Source& source,
|
deba@2290
|
669 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
deba@2290
|
670 |
for (typename Source::NodeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
671 |
nodeRefMap[it] = target.addNode();
|
deba@2290
|
672 |
}
|
deba@2290
|
673 |
for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
674 |
edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
|
deba@2290
|
675 |
nodeRefMap[source.target(it)]);
|
deba@2290
|
676 |
}
|
deba@2290
|
677 |
}
|
deba@2290
|
678 |
};
|
deba@2290
|
679 |
|
deba@2290
|
680 |
template <typename Graph>
|
deba@2290
|
681 |
struct GraphCopySelector<
|
deba@2290
|
682 |
Graph,
|
deba@2329
|
683 |
typename enable_if<typename Graph::BuildTag, void>::type>
|
deba@2290
|
684 |
{
|
deba@2290
|
685 |
template <typename Source, typename NodeRefMap, typename EdgeRefMap>
|
deba@2290
|
686 |
static void copy(Graph &target, const Source& source,
|
deba@2290
|
687 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
deba@2329
|
688 |
target.build(source, nodeRefMap, edgeRefMap);
|
deba@2290
|
689 |
}
|
deba@2290
|
690 |
};
|
deba@2290
|
691 |
|
deba@2290
|
692 |
template <typename UGraph, typename Enable = void>
|
deba@2290
|
693 |
struct UGraphCopySelector {
|
deba@2290
|
694 |
template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
|
deba@2290
|
695 |
static void copy(UGraph &target, const Source& source,
|
deba@2290
|
696 |
NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
|
deba@2290
|
697 |
for (typename Source::NodeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
698 |
nodeRefMap[it] = target.addNode();
|
deba@2290
|
699 |
}
|
deba@2290
|
700 |
for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
701 |
uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
|
deba@2290
|
702 |
nodeRefMap[source.target(it)]);
|
deba@2290
|
703 |
}
|
deba@2290
|
704 |
}
|
deba@2290
|
705 |
};
|
deba@2290
|
706 |
|
deba@2290
|
707 |
template <typename UGraph>
|
deba@2290
|
708 |
struct UGraphCopySelector<
|
deba@2290
|
709 |
UGraph,
|
deba@2329
|
710 |
typename enable_if<typename UGraph::BuildTag, void>::type>
|
deba@2290
|
711 |
{
|
deba@2290
|
712 |
template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
|
deba@2290
|
713 |
static void copy(UGraph &target, const Source& source,
|
deba@2290
|
714 |
NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
|
deba@2329
|
715 |
target.build(source, nodeRefMap, uEdgeRefMap);
|
deba@2290
|
716 |
}
|
deba@2290
|
717 |
};
|
deba@2290
|
718 |
|
deba@2290
|
719 |
template <typename BpUGraph, typename Enable = void>
|
deba@2290
|
720 |
struct BpUGraphCopySelector {
|
deba@2290
|
721 |
template <typename Source, typename ANodeRefMap,
|
deba@2290
|
722 |
typename BNodeRefMap, typename UEdgeRefMap>
|
deba@2290
|
723 |
static void copy(BpUGraph &target, const Source& source,
|
deba@2290
|
724 |
ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
|
deba@2290
|
725 |
UEdgeRefMap& uEdgeRefMap) {
|
deba@2290
|
726 |
for (typename Source::ANodeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
727 |
aNodeRefMap[it] = target.addANode();
|
deba@2290
|
728 |
}
|
deba@2290
|
729 |
for (typename Source::BNodeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
730 |
bNodeRefMap[it] = target.addBNode();
|
deba@2290
|
731 |
}
|
deba@2290
|
732 |
for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
|
deba@2290
|
733 |
uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)],
|
deba@2290
|
734 |
bNodeRefMap[source.bNode(it)]);
|
deba@2290
|
735 |
}
|
deba@2290
|
736 |
}
|
deba@2290
|
737 |
};
|
deba@2290
|
738 |
|
deba@2290
|
739 |
template <typename BpUGraph>
|
deba@2290
|
740 |
struct BpUGraphCopySelector<
|
deba@2290
|
741 |
BpUGraph,
|
deba@2329
|
742 |
typename enable_if<typename BpUGraph::BuildTag, void>::type>
|
deba@2290
|
743 |
{
|
deba@2290
|
744 |
template <typename Source, typename ANodeRefMap,
|
deba@2290
|
745 |
typename BNodeRefMap, typename UEdgeRefMap>
|
deba@2290
|
746 |
static void copy(BpUGraph &target, const Source& source,
|
deba@2290
|
747 |
ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
|
deba@2290
|
748 |
UEdgeRefMap& uEdgeRefMap) {
|
deba@2329
|
749 |
target.build(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
|
deba@2290
|
750 |
}
|
deba@2290
|
751 |
};
|
deba@2290
|
752 |
|
deba@2290
|
753 |
|
deba@2286
|
754 |
}
|
deba@2286
|
755 |
|
athos@1540
|
756 |
/// \brief Class to copy a graph.
|
deba@1531
|
757 |
///
|
alpar@2006
|
758 |
/// Class to copy a graph to another graph (duplicate a graph). The
|
athos@1540
|
759 |
/// simplest way of using it is through the \c copyGraph() function.
|
deba@1531
|
760 |
template <typename Target, typename Source>
|
deba@1267
|
761 |
class GraphCopy {
|
deba@2286
|
762 |
private:
|
deba@2286
|
763 |
|
deba@1531
|
764 |
typedef typename Source::Node Node;
|
deba@1531
|
765 |
typedef typename Source::NodeIt NodeIt;
|
deba@1531
|
766 |
typedef typename Source::Edge Edge;
|
deba@1531
|
767 |
typedef typename Source::EdgeIt EdgeIt;
|
klao@946
|
768 |
|
deba@2286
|
769 |
typedef typename Target::Node TNode;
|
deba@2286
|
770 |
typedef typename Target::Edge TEdge;
|
deba@2286
|
771 |
|
deba@2286
|
772 |
typedef typename Source::template NodeMap<TNode> NodeRefMap;
|
deba@2286
|
773 |
typedef typename Source::template EdgeMap<TEdge> EdgeRefMap;
|
deba@2286
|
774 |
|
deba@2286
|
775 |
|
deba@2286
|
776 |
public:
|
deba@2286
|
777 |
|
klao@946
|
778 |
|
deba@1531
|
779 |
/// \brief Constructor for the GraphCopy.
|
deba@1531
|
780 |
///
|
deba@1531
|
781 |
/// It copies the content of the \c _source graph into the
|
deba@2286
|
782 |
/// \c _target graph.
|
deba@1531
|
783 |
GraphCopy(Target& _target, const Source& _source)
|
deba@2286
|
784 |
: source(_source), target(_target) {}
|
deba@2286
|
785 |
|
deba@2286
|
786 |
/// \brief Destructor of the GraphCopy
|
deba@2286
|
787 |
///
|
deba@2286
|
788 |
/// Destructor of the GraphCopy
|
deba@2286
|
789 |
~GraphCopy() {
|
deba@2386
|
790 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2286
|
791 |
delete nodeMapCopies[i];
|
deba@1531
|
792 |
}
|
deba@2386
|
793 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2286
|
794 |
delete edgeMapCopies[i];
|
deba@1531
|
795 |
}
|
deba@2286
|
796 |
|
deba@1267
|
797 |
}
|
klao@946
|
798 |
|
deba@1531
|
799 |
/// \brief Copies the node references into the given map.
|
deba@1531
|
800 |
///
|
deba@1531
|
801 |
/// Copies the node references into the given map.
|
deba@1531
|
802 |
template <typename NodeRef>
|
deba@2286
|
803 |
GraphCopy& nodeRef(NodeRef& map) {
|
deba@2286
|
804 |
nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
|
deba@2286
|
805 |
NodeRefMap, NodeRef>(map));
|
deba@1531
|
806 |
return *this;
|
deba@1267
|
807 |
}
|
deba@1531
|
808 |
|
deba@2290
|
809 |
/// \brief Copies the node cross references into the given map.
|
deba@1531
|
810 |
///
|
deba@2290
|
811 |
/// Copies the node cross references (reverse references) into
|
deba@2290
|
812 |
/// the given map.
|
deba@2286
|
813 |
template <typename NodeCrossRef>
|
deba@2286
|
814 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
deba@2286
|
815 |
nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
|
deba@2286
|
816 |
NodeRefMap, NodeCrossRef>(map));
|
deba@1531
|
817 |
return *this;
|
deba@1531
|
818 |
}
|
deba@1531
|
819 |
|
deba@1531
|
820 |
/// \brief Make copy of the given map.
|
deba@1531
|
821 |
///
|
deba@1531
|
822 |
/// Makes copy of the given map for the newly created graph.
|
deba@1531
|
823 |
/// The new map's key type is the target graph's node type,
|
deba@1531
|
824 |
/// and the copied map's key type is the source graph's node
|
deba@1531
|
825 |
/// type.
|
deba@1531
|
826 |
template <typename TargetMap, typename SourceMap>
|
deba@2286
|
827 |
GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2286
|
828 |
nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
|
deba@2286
|
829 |
NodeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2286
|
830 |
return *this;
|
deba@2286
|
831 |
}
|
deba@2286
|
832 |
|
deba@2290
|
833 |
/// \brief Make a copy of the given node.
|
deba@2290
|
834 |
///
|
deba@2290
|
835 |
/// Make a copy of the given node.
|
deba@2386
|
836 |
GraphCopy& node(TNode& tnode, const Node& snode) {
|
deba@2290
|
837 |
nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
|
deba@2386
|
838 |
NodeRefMap, TNode>(tnode, snode));
|
deba@2290
|
839 |
return *this;
|
deba@2290
|
840 |
}
|
deba@2290
|
841 |
|
deba@2286
|
842 |
/// \brief Copies the edge references into the given map.
|
deba@2286
|
843 |
///
|
deba@2286
|
844 |
/// Copies the edge references into the given map.
|
deba@2286
|
845 |
template <typename EdgeRef>
|
deba@2286
|
846 |
GraphCopy& edgeRef(EdgeRef& map) {
|
deba@2286
|
847 |
edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
|
deba@2286
|
848 |
EdgeRefMap, EdgeRef>(map));
|
deba@2286
|
849 |
return *this;
|
deba@2286
|
850 |
}
|
deba@2286
|
851 |
|
deba@2290
|
852 |
/// \brief Copies the edge cross references into the given map.
|
deba@2286
|
853 |
///
|
deba@2290
|
854 |
/// Copies the edge cross references (reverse references) into
|
deba@2290
|
855 |
/// the given map.
|
deba@2286
|
856 |
template <typename EdgeCrossRef>
|
deba@2286
|
857 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
deba@2286
|
858 |
edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
|
deba@2286
|
859 |
EdgeRefMap, EdgeCrossRef>(map));
|
deba@1531
|
860 |
return *this;
|
deba@1531
|
861 |
}
|
deba@1531
|
862 |
|
deba@1531
|
863 |
/// \brief Make copy of the given map.
|
deba@1531
|
864 |
///
|
deba@1531
|
865 |
/// Makes copy of the given map for the newly created graph.
|
deba@1531
|
866 |
/// The new map's key type is the target graph's edge type,
|
deba@1531
|
867 |
/// and the copied map's key type is the source graph's edge
|
deba@1531
|
868 |
/// type.
|
deba@1531
|
869 |
template <typename TargetMap, typename SourceMap>
|
deba@2286
|
870 |
GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2286
|
871 |
edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
|
deba@2286
|
872 |
EdgeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@1531
|
873 |
return *this;
|
deba@1531
|
874 |
}
|
deba@1531
|
875 |
|
deba@2290
|
876 |
/// \brief Make a copy of the given edge.
|
deba@2290
|
877 |
///
|
deba@2290
|
878 |
/// Make a copy of the given edge.
|
deba@2386
|
879 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
deba@2290
|
880 |
edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
|
deba@2386
|
881 |
EdgeRefMap, TEdge>(tedge, sedge));
|
deba@2290
|
882 |
return *this;
|
deba@2290
|
883 |
}
|
deba@2290
|
884 |
|
deba@2286
|
885 |
/// \brief Executes the copies.
|
deba@1531
|
886 |
///
|
deba@2286
|
887 |
/// Executes the copies.
|
deba@2286
|
888 |
void run() {
|
deba@2286
|
889 |
NodeRefMap nodeRefMap(source);
|
deba@2290
|
890 |
EdgeRefMap edgeRefMap(source);
|
deba@2290
|
891 |
_graph_utils_bits::GraphCopySelector<Target>::
|
deba@2290
|
892 |
copy(target, source, nodeRefMap, edgeRefMap);
|
deba@2386
|
893 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2286
|
894 |
nodeMapCopies[i]->copy(source, nodeRefMap);
|
deba@2286
|
895 |
}
|
deba@2386
|
896 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2286
|
897 |
edgeMapCopies[i]->copy(source, edgeRefMap);
|
deba@2290
|
898 |
}
|
deba@1531
|
899 |
}
|
deba@1531
|
900 |
|
deba@2290
|
901 |
protected:
|
deba@2290
|
902 |
|
deba@2290
|
903 |
|
deba@1531
|
904 |
const Source& source;
|
deba@1531
|
905 |
Target& target;
|
deba@1531
|
906 |
|
deba@2286
|
907 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
|
deba@2286
|
908 |
nodeMapCopies;
|
deba@2286
|
909 |
|
deba@2286
|
910 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
|
deba@2286
|
911 |
edgeMapCopies;
|
deba@2286
|
912 |
|
deba@1267
|
913 |
};
|
klao@946
|
914 |
|
alpar@2006
|
915 |
/// \brief Copy a graph to another graph.
|
deba@1531
|
916 |
///
|
alpar@2006
|
917 |
/// Copy a graph to another graph.
|
deba@1531
|
918 |
/// The usage of the function:
|
deba@1531
|
919 |
///
|
alpar@1946
|
920 |
///\code
|
deba@2286
|
921 |
/// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
|
alpar@1946
|
922 |
///\endcode
|
deba@1531
|
923 |
///
|
deba@1531
|
924 |
/// After the copy the \c nr map will contain the mapping from the
|
deba@1531
|
925 |
/// source graph's nodes to the target graph's nodes and the \c ecr will
|
athos@1540
|
926 |
/// contain the mapping from the target graph's edges to the source's
|
deba@1531
|
927 |
/// edges.
|
deba@2290
|
928 |
///
|
deba@2290
|
929 |
/// \see GraphCopy
|
deba@1531
|
930 |
template <typename Target, typename Source>
|
deba@1531
|
931 |
GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
|
deba@1531
|
932 |
return GraphCopy<Target, Source>(target, source);
|
deba@1531
|
933 |
}
|
klao@946
|
934 |
|
deba@1720
|
935 |
/// \brief Class to copy an undirected graph.
|
deba@1720
|
936 |
///
|
alpar@2006
|
937 |
/// Class to copy an undirected graph to another graph (duplicate a graph).
|
klao@1909
|
938 |
/// The simplest way of using it is through the \c copyUGraph() function.
|
deba@1720
|
939 |
template <typename Target, typename Source>
|
klao@1909
|
940 |
class UGraphCopy {
|
deba@2286
|
941 |
private:
|
deba@2286
|
942 |
|
deba@1720
|
943 |
typedef typename Source::Node Node;
|
deba@1720
|
944 |
typedef typename Source::NodeIt NodeIt;
|
deba@1720
|
945 |
typedef typename Source::Edge Edge;
|
deba@1720
|
946 |
typedef typename Source::EdgeIt EdgeIt;
|
klao@1909
|
947 |
typedef typename Source::UEdge UEdge;
|
klao@1909
|
948 |
typedef typename Source::UEdgeIt UEdgeIt;
|
deba@1720
|
949 |
|
deba@2286
|
950 |
typedef typename Target::Node TNode;
|
deba@2286
|
951 |
typedef typename Target::Edge TEdge;
|
deba@2286
|
952 |
typedef typename Target::UEdge TUEdge;
|
deba@1720
|
953 |
|
deba@2286
|
954 |
typedef typename Source::template NodeMap<TNode> NodeRefMap;
|
deba@2286
|
955 |
typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
|
deba@1720
|
956 |
|
deba@1720
|
957 |
struct EdgeRefMap {
|
deba@2286
|
958 |
EdgeRefMap(const Target& _target, const Source& _source,
|
deba@2286
|
959 |
const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
|
deba@2286
|
960 |
: target(_target), source(_source),
|
deba@2286
|
961 |
uedge_ref(_uedge_ref), node_ref(_node_ref) {}
|
deba@2286
|
962 |
|
deba@1720
|
963 |
typedef typename Source::Edge Key;
|
deba@1720
|
964 |
typedef typename Target::Edge Value;
|
deba@1720
|
965 |
|
deba@2286
|
966 |
Value operator[](const Key& key) const {
|
deba@2386
|
967 |
bool forward =
|
deba@2386
|
968 |
(source.direction(key) ==
|
deba@2386
|
969 |
(node_ref[source.source(static_cast<const UEdge&>(key))] ==
|
deba@2386
|
970 |
target.source(uedge_ref[static_cast<const UEdge&>(key)])));
|
deba@2286
|
971 |
return target.direct(uedge_ref[key], forward);
|
deba@1720
|
972 |
}
|
deba@1720
|
973 |
|
deba@2286
|
974 |
const Target& target;
|
deba@2286
|
975 |
const Source& source;
|
deba@2286
|
976 |
const UEdgeRefMap& uedge_ref;
|
deba@2286
|
977 |
const NodeRefMap& node_ref;
|
deba@1720
|
978 |
};
|
deba@2286
|
979 |
|
deba@1720
|
980 |
|
deba@2286
|
981 |
public:
|
deba@1720
|
982 |
|
deba@2286
|
983 |
|
deba@2286
|
984 |
/// \brief Constructor for the GraphCopy.
|
deba@1720
|
985 |
///
|
deba@1720
|
986 |
/// It copies the content of the \c _source graph into the
|
deba@2286
|
987 |
/// \c _target graph.
|
klao@1909
|
988 |
UGraphCopy(Target& _target, const Source& _source)
|
deba@2286
|
989 |
: source(_source), target(_target) {}
|
deba@2286
|
990 |
|
deba@2286
|
991 |
/// \brief Destructor of the GraphCopy
|
deba@2286
|
992 |
///
|
deba@2286
|
993 |
/// Destructor of the GraphCopy
|
deba@2286
|
994 |
~UGraphCopy() {
|
deba@2386
|
995 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2286
|
996 |
delete nodeMapCopies[i];
|
deba@1720
|
997 |
}
|
deba@2386
|
998 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2286
|
999 |
delete edgeMapCopies[i];
|
deba@1720
|
1000 |
}
|
deba@2386
|
1001 |
for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
|
deba@2286
|
1002 |
delete uEdgeMapCopies[i];
|
deba@2286
|
1003 |
}
|
deba@2286
|
1004 |
|
deba@1720
|
1005 |
}
|
deba@1720
|
1006 |
|
deba@1720
|
1007 |
/// \brief Copies the node references into the given map.
|
deba@1720
|
1008 |
///
|
deba@1720
|
1009 |
/// Copies the node references into the given map.
|
deba@1720
|
1010 |
template <typename NodeRef>
|
deba@2286
|
1011 |
UGraphCopy& nodeRef(NodeRef& map) {
|
deba@2286
|
1012 |
nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
|
deba@2286
|
1013 |
NodeRefMap, NodeRef>(map));
|
deba@1720
|
1014 |
return *this;
|
deba@1720
|
1015 |
}
|
deba@1720
|
1016 |
|
deba@2290
|
1017 |
/// \brief Copies the node cross references into the given map.
|
deba@1720
|
1018 |
///
|
deba@2290
|
1019 |
/// Copies the node cross references (reverse references) into
|
deba@2290
|
1020 |
/// the given map.
|
deba@2286
|
1021 |
template <typename NodeCrossRef>
|
deba@2286
|
1022 |
UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
deba@2286
|
1023 |
nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
|
deba@2286
|
1024 |
NodeRefMap, NodeCrossRef>(map));
|
deba@1720
|
1025 |
return *this;
|
deba@1720
|
1026 |
}
|
deba@1720
|
1027 |
|
deba@1720
|
1028 |
/// \brief Make copy of the given map.
|
deba@1720
|
1029 |
///
|
deba@1720
|
1030 |
/// Makes copy of the given map for the newly created graph.
|
deba@1720
|
1031 |
/// The new map's key type is the target graph's node type,
|
deba@1720
|
1032 |
/// and the copied map's key type is the source graph's node
|
deba@1720
|
1033 |
/// type.
|
deba@1720
|
1034 |
template <typename TargetMap, typename SourceMap>
|
deba@2286
|
1035 |
UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2286
|
1036 |
nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
|
deba@2286
|
1037 |
NodeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2286
|
1038 |
return *this;
|
deba@2286
|
1039 |
}
|
deba@2286
|
1040 |
|
deba@2290
|
1041 |
/// \brief Make a copy of the given node.
|
deba@2290
|
1042 |
///
|
deba@2290
|
1043 |
/// Make a copy of the given node.
|
deba@2386
|
1044 |
UGraphCopy& node(TNode& tnode, const Node& snode) {
|
deba@2290
|
1045 |
nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
|
deba@2386
|
1046 |
NodeRefMap, TNode>(tnode, snode));
|
deba@2290
|
1047 |
return *this;
|
deba@2290
|
1048 |
}
|
deba@2290
|
1049 |
|
deba@2286
|
1050 |
/// \brief Copies the edge references into the given map.
|
deba@2286
|
1051 |
///
|
deba@2286
|
1052 |
/// Copies the edge references into the given map.
|
deba@2286
|
1053 |
template <typename EdgeRef>
|
deba@2286
|
1054 |
UGraphCopy& edgeRef(EdgeRef& map) {
|
deba@2286
|
1055 |
edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
|
deba@2286
|
1056 |
EdgeRefMap, EdgeRef>(map));
|
deba@2286
|
1057 |
return *this;
|
deba@2286
|
1058 |
}
|
deba@2286
|
1059 |
|
deba@2290
|
1060 |
/// \brief Copies the edge cross references into the given map.
|
deba@2286
|
1061 |
///
|
deba@2290
|
1062 |
/// Copies the edge cross references (reverse references) into
|
deba@2290
|
1063 |
/// the given map.
|
deba@2286
|
1064 |
template <typename EdgeCrossRef>
|
deba@2286
|
1065 |
UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
deba@2286
|
1066 |
edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
|
deba@2286
|
1067 |
EdgeRefMap, EdgeCrossRef>(map));
|
deba@1720
|
1068 |
return *this;
|
deba@1720
|
1069 |
}
|
deba@1720
|
1070 |
|
deba@1720
|
1071 |
/// \brief Make copy of the given map.
|
deba@1720
|
1072 |
///
|
deba@1720
|
1073 |
/// Makes copy of the given map for the newly created graph.
|
deba@1720
|
1074 |
/// The new map's key type is the target graph's edge type,
|
deba@1720
|
1075 |
/// and the copied map's key type is the source graph's edge
|
deba@1720
|
1076 |
/// type.
|
deba@1720
|
1077 |
template <typename TargetMap, typename SourceMap>
|
deba@2286
|
1078 |
UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2286
|
1079 |
edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
|
deba@2286
|
1080 |
EdgeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2286
|
1081 |
return *this;
|
deba@2286
|
1082 |
}
|
deba@2286
|
1083 |
|
deba@2290
|
1084 |
/// \brief Make a copy of the given edge.
|
deba@2286
|
1085 |
///
|
deba@2290
|
1086 |
/// Make a copy of the given edge.
|
deba@2386
|
1087 |
UGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
deba@2290
|
1088 |
edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
|
deba@2386
|
1089 |
EdgeRefMap, TEdge>(tedge, sedge));
|
deba@2290
|
1090 |
return *this;
|
deba@2290
|
1091 |
}
|
deba@2290
|
1092 |
|
deba@2290
|
1093 |
/// \brief Copies the undirected edge references into the given map.
|
deba@2290
|
1094 |
///
|
deba@2290
|
1095 |
/// Copies the undirected edge references into the given map.
|
deba@2286
|
1096 |
template <typename UEdgeRef>
|
deba@2286
|
1097 |
UGraphCopy& uEdgeRef(UEdgeRef& map) {
|
deba@2286
|
1098 |
uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
|
deba@2286
|
1099 |
UEdgeRefMap, UEdgeRef>(map));
|
deba@2286
|
1100 |
return *this;
|
deba@2286
|
1101 |
}
|
deba@2286
|
1102 |
|
deba@2290
|
1103 |
/// \brief Copies the undirected edge cross references into the given map.
|
deba@2286
|
1104 |
///
|
deba@2290
|
1105 |
/// Copies the undirected edge cross references (reverse
|
deba@2290
|
1106 |
/// references) into the given map.
|
deba@2286
|
1107 |
template <typename UEdgeCrossRef>
|
deba@2286
|
1108 |
UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
|
deba@2286
|
1109 |
uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
|
deba@2286
|
1110 |
UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
|
deba@1720
|
1111 |
return *this;
|
deba@1720
|
1112 |
}
|
deba@1720
|
1113 |
|
deba@1720
|
1114 |
/// \brief Make copy of the given map.
|
deba@1720
|
1115 |
///
|
deba@1720
|
1116 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1117 |
/// The new map's key type is the target graph's undirected edge type,
|
deba@2290
|
1118 |
/// and the copied map's key type is the source graph's undirected edge
|
deba@1720
|
1119 |
/// type.
|
deba@1720
|
1120 |
template <typename TargetMap, typename SourceMap>
|
deba@2286
|
1121 |
UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2286
|
1122 |
uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
|
deba@2286
|
1123 |
UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@1720
|
1124 |
return *this;
|
deba@1720
|
1125 |
}
|
deba@1720
|
1126 |
|
deba@2290
|
1127 |
/// \brief Make a copy of the given undirected edge.
|
deba@2290
|
1128 |
///
|
deba@2290
|
1129 |
/// Make a copy of the given undirected edge.
|
deba@2386
|
1130 |
UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
|
deba@2290
|
1131 |
uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
|
deba@2386
|
1132 |
UEdgeRefMap, TUEdge>(tuedge, suedge));
|
deba@2290
|
1133 |
return *this;
|
deba@2290
|
1134 |
}
|
deba@2290
|
1135 |
|
deba@2286
|
1136 |
/// \brief Executes the copies.
|
deba@1720
|
1137 |
///
|
deba@2286
|
1138 |
/// Executes the copies.
|
deba@2286
|
1139 |
void run() {
|
deba@2286
|
1140 |
NodeRefMap nodeRefMap(source);
|
deba@2290
|
1141 |
UEdgeRefMap uEdgeRefMap(source);
|
deba@2290
|
1142 |
EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
|
deba@2290
|
1143 |
_graph_utils_bits::UGraphCopySelector<Target>::
|
deba@2290
|
1144 |
copy(target, source, nodeRefMap, uEdgeRefMap);
|
deba@2386
|
1145 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2286
|
1146 |
nodeMapCopies[i]->copy(source, nodeRefMap);
|
deba@2286
|
1147 |
}
|
deba@2386
|
1148 |
for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
|
deba@2286
|
1149 |
uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
|
deba@2286
|
1150 |
}
|
deba@2386
|
1151 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2286
|
1152 |
edgeMapCopies[i]->copy(source, edgeRefMap);
|
deba@2286
|
1153 |
}
|
deba@1720
|
1154 |
}
|
deba@1720
|
1155 |
|
deba@1720
|
1156 |
private:
|
deba@1192
|
1157 |
|
deba@1720
|
1158 |
const Source& source;
|
deba@1720
|
1159 |
Target& target;
|
alpar@947
|
1160 |
|
deba@2286
|
1161 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
|
deba@2286
|
1162 |
nodeMapCopies;
|
deba@2286
|
1163 |
|
deba@2286
|
1164 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
|
deba@2286
|
1165 |
edgeMapCopies;
|
deba@2286
|
1166 |
|
deba@2286
|
1167 |
std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
|
deba@2286
|
1168 |
uEdgeMapCopies;
|
deba@2286
|
1169 |
|
deba@1192
|
1170 |
};
|
deba@1192
|
1171 |
|
deba@2290
|
1172 |
/// \brief Copy an undirected graph to another graph.
|
deba@1720
|
1173 |
///
|
deba@2290
|
1174 |
/// Copy an undirected graph to another graph.
|
deba@1720
|
1175 |
/// The usage of the function:
|
deba@1720
|
1176 |
///
|
alpar@1946
|
1177 |
///\code
|
deba@2286
|
1178 |
/// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
|
alpar@1946
|
1179 |
///\endcode
|
deba@1720
|
1180 |
///
|
deba@1720
|
1181 |
/// After the copy the \c nr map will contain the mapping from the
|
deba@1720
|
1182 |
/// source graph's nodes to the target graph's nodes and the \c ecr will
|
deba@1720
|
1183 |
/// contain the mapping from the target graph's edges to the source's
|
deba@1720
|
1184 |
/// edges.
|
deba@2290
|
1185 |
///
|
deba@2290
|
1186 |
/// \see UGraphCopy
|
deba@1720
|
1187 |
template <typename Target, typename Source>
|
klao@1909
|
1188 |
UGraphCopy<Target, Source>
|
klao@1909
|
1189 |
copyUGraph(Target& target, const Source& source) {
|
klao@1909
|
1190 |
return UGraphCopy<Target, Source>(target, source);
|
deba@1720
|
1191 |
}
|
deba@1192
|
1192 |
|
deba@2290
|
1193 |
/// \brief Class to copy a bipartite undirected graph.
|
deba@2290
|
1194 |
///
|
deba@2290
|
1195 |
/// Class to copy a bipartite undirected graph to another graph
|
deba@2290
|
1196 |
/// (duplicate a graph). The simplest way of using it is through
|
deba@2290
|
1197 |
/// the \c copyBpUGraph() function.
|
deba@2290
|
1198 |
template <typename Target, typename Source>
|
deba@2290
|
1199 |
class BpUGraphCopy {
|
deba@2290
|
1200 |
private:
|
deba@2290
|
1201 |
|
deba@2290
|
1202 |
typedef typename Source::Node Node;
|
deba@2290
|
1203 |
typedef typename Source::ANode ANode;
|
deba@2290
|
1204 |
typedef typename Source::BNode BNode;
|
deba@2290
|
1205 |
typedef typename Source::NodeIt NodeIt;
|
deba@2290
|
1206 |
typedef typename Source::Edge Edge;
|
deba@2290
|
1207 |
typedef typename Source::EdgeIt EdgeIt;
|
deba@2290
|
1208 |
typedef typename Source::UEdge UEdge;
|
deba@2290
|
1209 |
typedef typename Source::UEdgeIt UEdgeIt;
|
deba@2290
|
1210 |
|
deba@2290
|
1211 |
typedef typename Target::Node TNode;
|
deba@2290
|
1212 |
typedef typename Target::Edge TEdge;
|
deba@2290
|
1213 |
typedef typename Target::UEdge TUEdge;
|
deba@2290
|
1214 |
|
deba@2290
|
1215 |
typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
|
deba@2290
|
1216 |
typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
|
deba@2290
|
1217 |
typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
|
deba@2290
|
1218 |
|
deba@2290
|
1219 |
struct NodeRefMap {
|
deba@2290
|
1220 |
NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
|
deba@2290
|
1221 |
const BNodeRefMap& _bnode_ref)
|
deba@2290
|
1222 |
: source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
|
deba@2290
|
1223 |
|
deba@2290
|
1224 |
typedef typename Source::Node Key;
|
deba@2290
|
1225 |
typedef typename Target::Node Value;
|
deba@2290
|
1226 |
|
deba@2290
|
1227 |
Value operator[](const Key& key) const {
|
deba@2290
|
1228 |
return source.aNode(key) ? anode_ref[key] : bnode_ref[key];
|
deba@2290
|
1229 |
}
|
deba@2290
|
1230 |
|
deba@2290
|
1231 |
const Source& source;
|
deba@2290
|
1232 |
const ANodeRefMap& anode_ref;
|
deba@2290
|
1233 |
const BNodeRefMap& bnode_ref;
|
deba@2290
|
1234 |
};
|
deba@2290
|
1235 |
|
deba@2290
|
1236 |
struct EdgeRefMap {
|
deba@2290
|
1237 |
EdgeRefMap(const Target& _target, const Source& _source,
|
deba@2290
|
1238 |
const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
|
deba@2290
|
1239 |
: target(_target), source(_source),
|
deba@2290
|
1240 |
uedge_ref(_uedge_ref), node_ref(_node_ref) {}
|
deba@2290
|
1241 |
|
deba@2290
|
1242 |
typedef typename Source::Edge Key;
|
deba@2290
|
1243 |
typedef typename Target::Edge Value;
|
deba@2290
|
1244 |
|
deba@2290
|
1245 |
Value operator[](const Key& key) const {
|
deba@2386
|
1246 |
bool forward =
|
deba@2386
|
1247 |
(source.direction(key) ==
|
deba@2386
|
1248 |
(node_ref[source.source(static_cast<const UEdge&>(key))] ==
|
deba@2386
|
1249 |
target.source(uedge_ref[static_cast<const UEdge&>(key)])));
|
deba@2290
|
1250 |
return target.direct(uedge_ref[key], forward);
|
deba@2290
|
1251 |
}
|
deba@2290
|
1252 |
|
deba@2290
|
1253 |
const Target& target;
|
deba@2290
|
1254 |
const Source& source;
|
deba@2290
|
1255 |
const UEdgeRefMap& uedge_ref;
|
deba@2290
|
1256 |
const NodeRefMap& node_ref;
|
deba@2290
|
1257 |
};
|
deba@2290
|
1258 |
|
deba@2290
|
1259 |
public:
|
deba@2290
|
1260 |
|
deba@2290
|
1261 |
|
deba@2290
|
1262 |
/// \brief Constructor for the GraphCopy.
|
deba@2290
|
1263 |
///
|
deba@2290
|
1264 |
/// It copies the content of the \c _source graph into the
|
deba@2290
|
1265 |
/// \c _target graph.
|
deba@2290
|
1266 |
BpUGraphCopy(Target& _target, const Source& _source)
|
deba@2290
|
1267 |
: source(_source), target(_target) {}
|
deba@2290
|
1268 |
|
deba@2290
|
1269 |
/// \brief Destructor of the GraphCopy
|
deba@2290
|
1270 |
///
|
deba@2290
|
1271 |
/// Destructor of the GraphCopy
|
deba@2290
|
1272 |
~BpUGraphCopy() {
|
deba@2386
|
1273 |
for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
|
deba@2290
|
1274 |
delete aNodeMapCopies[i];
|
deba@2290
|
1275 |
}
|
deba@2386
|
1276 |
for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
|
deba@2290
|
1277 |
delete bNodeMapCopies[i];
|
deba@2290
|
1278 |
}
|
deba@2386
|
1279 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2290
|
1280 |
delete nodeMapCopies[i];
|
deba@2290
|
1281 |
}
|
deba@2386
|
1282 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2290
|
1283 |
delete edgeMapCopies[i];
|
deba@2290
|
1284 |
}
|
deba@2386
|
1285 |
for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
|
deba@2290
|
1286 |
delete uEdgeMapCopies[i];
|
deba@2290
|
1287 |
}
|
deba@2290
|
1288 |
|
deba@2290
|
1289 |
}
|
deba@2290
|
1290 |
|
deba@2290
|
1291 |
/// \brief Copies the A-node references into the given map.
|
deba@2290
|
1292 |
///
|
deba@2290
|
1293 |
/// Copies the A-node references into the given map.
|
deba@2290
|
1294 |
template <typename ANodeRef>
|
deba@2290
|
1295 |
BpUGraphCopy& aNodeRef(ANodeRef& map) {
|
deba@2290
|
1296 |
aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode,
|
deba@2290
|
1297 |
ANodeRefMap, ANodeRef>(map));
|
deba@2290
|
1298 |
return *this;
|
deba@2290
|
1299 |
}
|
deba@2290
|
1300 |
|
deba@2290
|
1301 |
/// \brief Copies the A-node cross references into the given map.
|
deba@2290
|
1302 |
///
|
deba@2290
|
1303 |
/// Copies the A-node cross references (reverse references) into
|
deba@2290
|
1304 |
/// the given map.
|
deba@2290
|
1305 |
template <typename ANodeCrossRef>
|
deba@2290
|
1306 |
BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
|
deba@2290
|
1307 |
aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
|
deba@2290
|
1308 |
ANode, ANodeRefMap, ANodeCrossRef>(map));
|
deba@2290
|
1309 |
return *this;
|
deba@2290
|
1310 |
}
|
deba@2290
|
1311 |
|
deba@2290
|
1312 |
/// \brief Make copy of the given A-node map.
|
deba@2290
|
1313 |
///
|
deba@2290
|
1314 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1315 |
/// The new map's key type is the target graph's node type,
|
deba@2290
|
1316 |
/// and the copied map's key type is the source graph's node
|
deba@2290
|
1317 |
/// type.
|
deba@2290
|
1318 |
template <typename TargetMap, typename SourceMap>
|
deba@2290
|
1319 |
BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2290
|
1320 |
aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode,
|
deba@2290
|
1321 |
ANodeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2290
|
1322 |
return *this;
|
deba@2290
|
1323 |
}
|
deba@2290
|
1324 |
|
deba@2290
|
1325 |
/// \brief Copies the B-node references into the given map.
|
deba@2290
|
1326 |
///
|
deba@2290
|
1327 |
/// Copies the B-node references into the given map.
|
deba@2290
|
1328 |
template <typename BNodeRef>
|
deba@2290
|
1329 |
BpUGraphCopy& bNodeRef(BNodeRef& map) {
|
deba@2290
|
1330 |
bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode,
|
deba@2290
|
1331 |
BNodeRefMap, BNodeRef>(map));
|
deba@2290
|
1332 |
return *this;
|
deba@2290
|
1333 |
}
|
deba@2290
|
1334 |
|
deba@2290
|
1335 |
/// \brief Copies the B-node cross references into the given map.
|
deba@2290
|
1336 |
///
|
deba@2290
|
1337 |
/// Copies the B-node cross references (reverse references) into
|
deba@2290
|
1338 |
/// the given map.
|
deba@2290
|
1339 |
template <typename BNodeCrossRef>
|
deba@2290
|
1340 |
BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
|
deba@2290
|
1341 |
bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
|
deba@2290
|
1342 |
BNode, BNodeRefMap, BNodeCrossRef>(map));
|
deba@2290
|
1343 |
return *this;
|
deba@2290
|
1344 |
}
|
deba@2290
|
1345 |
|
deba@2290
|
1346 |
/// \brief Make copy of the given B-node map.
|
deba@2290
|
1347 |
///
|
deba@2290
|
1348 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1349 |
/// The new map's key type is the target graph's node type,
|
deba@2290
|
1350 |
/// and the copied map's key type is the source graph's node
|
deba@2290
|
1351 |
/// type.
|
deba@2290
|
1352 |
template <typename TargetMap, typename SourceMap>
|
deba@2290
|
1353 |
BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2290
|
1354 |
bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode,
|
deba@2290
|
1355 |
BNodeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2290
|
1356 |
return *this;
|
deba@2290
|
1357 |
}
|
deba@2290
|
1358 |
/// \brief Copies the node references into the given map.
|
deba@2290
|
1359 |
///
|
deba@2290
|
1360 |
/// Copies the node references into the given map.
|
deba@2290
|
1361 |
template <typename NodeRef>
|
deba@2290
|
1362 |
BpUGraphCopy& nodeRef(NodeRef& map) {
|
deba@2290
|
1363 |
nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
|
deba@2290
|
1364 |
NodeRefMap, NodeRef>(map));
|
deba@2290
|
1365 |
return *this;
|
deba@2290
|
1366 |
}
|
deba@2290
|
1367 |
|
deba@2290
|
1368 |
/// \brief Copies the node cross references into the given map.
|
deba@2290
|
1369 |
///
|
deba@2290
|
1370 |
/// Copies the node cross references (reverse references) into
|
deba@2290
|
1371 |
/// the given map.
|
deba@2290
|
1372 |
template <typename NodeCrossRef>
|
deba@2290
|
1373 |
BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
deba@2290
|
1374 |
nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
|
deba@2290
|
1375 |
NodeRefMap, NodeCrossRef>(map));
|
deba@2290
|
1376 |
return *this;
|
deba@2290
|
1377 |
}
|
deba@2290
|
1378 |
|
deba@2290
|
1379 |
/// \brief Make copy of the given map.
|
deba@2290
|
1380 |
///
|
deba@2290
|
1381 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1382 |
/// The new map's key type is the target graph's node type,
|
deba@2290
|
1383 |
/// and the copied map's key type is the source graph's node
|
deba@2290
|
1384 |
/// type.
|
deba@2290
|
1385 |
template <typename TargetMap, typename SourceMap>
|
deba@2290
|
1386 |
BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2290
|
1387 |
nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
|
deba@2290
|
1388 |
NodeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2290
|
1389 |
return *this;
|
deba@2290
|
1390 |
}
|
deba@2290
|
1391 |
|
deba@2290
|
1392 |
/// \brief Make a copy of the given node.
|
deba@2290
|
1393 |
///
|
deba@2290
|
1394 |
/// Make a copy of the given node.
|
deba@2386
|
1395 |
BpUGraphCopy& node(TNode& tnode, const Node& snode) {
|
deba@2290
|
1396 |
nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
|
deba@2386
|
1397 |
NodeRefMap, TNode>(tnode, snode));
|
deba@2290
|
1398 |
return *this;
|
deba@2290
|
1399 |
}
|
deba@2290
|
1400 |
|
deba@2290
|
1401 |
/// \brief Copies the edge references into the given map.
|
deba@2290
|
1402 |
///
|
deba@2290
|
1403 |
/// Copies the edge references into the given map.
|
deba@2290
|
1404 |
template <typename EdgeRef>
|
deba@2290
|
1405 |
BpUGraphCopy& edgeRef(EdgeRef& map) {
|
deba@2290
|
1406 |
edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
|
deba@2290
|
1407 |
EdgeRefMap, EdgeRef>(map));
|
deba@2290
|
1408 |
return *this;
|
deba@2290
|
1409 |
}
|
deba@2290
|
1410 |
|
deba@2290
|
1411 |
/// \brief Copies the edge cross references into the given map.
|
deba@2290
|
1412 |
///
|
deba@2290
|
1413 |
/// Copies the edge cross references (reverse references) into
|
deba@2290
|
1414 |
/// the given map.
|
deba@2290
|
1415 |
template <typename EdgeCrossRef>
|
deba@2290
|
1416 |
BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
deba@2290
|
1417 |
edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
|
deba@2290
|
1418 |
EdgeRefMap, EdgeCrossRef>(map));
|
deba@2290
|
1419 |
return *this;
|
deba@2290
|
1420 |
}
|
deba@2290
|
1421 |
|
deba@2290
|
1422 |
/// \brief Make copy of the given map.
|
deba@2290
|
1423 |
///
|
deba@2290
|
1424 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1425 |
/// The new map's key type is the target graph's edge type,
|
deba@2290
|
1426 |
/// and the copied map's key type is the source graph's edge
|
deba@2290
|
1427 |
/// type.
|
deba@2290
|
1428 |
template <typename TargetMap, typename SourceMap>
|
deba@2290
|
1429 |
BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2290
|
1430 |
edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
|
deba@2290
|
1431 |
EdgeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2290
|
1432 |
return *this;
|
deba@2290
|
1433 |
}
|
deba@2290
|
1434 |
|
deba@2290
|
1435 |
/// \brief Make a copy of the given edge.
|
deba@2290
|
1436 |
///
|
deba@2290
|
1437 |
/// Make a copy of the given edge.
|
deba@2386
|
1438 |
BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
deba@2290
|
1439 |
edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
|
deba@2386
|
1440 |
EdgeRefMap, TEdge>(tedge, sedge));
|
deba@2290
|
1441 |
return *this;
|
deba@2290
|
1442 |
}
|
deba@2290
|
1443 |
|
deba@2290
|
1444 |
/// \brief Copies the undirected edge references into the given map.
|
deba@2290
|
1445 |
///
|
deba@2290
|
1446 |
/// Copies the undirected edge references into the given map.
|
deba@2290
|
1447 |
template <typename UEdgeRef>
|
deba@2290
|
1448 |
BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
|
deba@2290
|
1449 |
uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
|
deba@2290
|
1450 |
UEdgeRefMap, UEdgeRef>(map));
|
deba@2290
|
1451 |
return *this;
|
deba@2290
|
1452 |
}
|
deba@2290
|
1453 |
|
deba@2290
|
1454 |
/// \brief Copies the undirected edge cross references into the given map.
|
deba@2290
|
1455 |
///
|
deba@2290
|
1456 |
/// Copies the undirected edge cross references (reverse
|
deba@2290
|
1457 |
/// references) into the given map.
|
deba@2290
|
1458 |
template <typename UEdgeCrossRef>
|
deba@2290
|
1459 |
BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
|
deba@2290
|
1460 |
uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
|
deba@2290
|
1461 |
UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
|
deba@2290
|
1462 |
return *this;
|
deba@2290
|
1463 |
}
|
deba@2290
|
1464 |
|
deba@2290
|
1465 |
/// \brief Make copy of the given map.
|
deba@2290
|
1466 |
///
|
deba@2290
|
1467 |
/// Makes copy of the given map for the newly created graph.
|
deba@2290
|
1468 |
/// The new map's key type is the target graph's undirected edge type,
|
deba@2290
|
1469 |
/// and the copied map's key type is the source graph's undirected edge
|
deba@2290
|
1470 |
/// type.
|
deba@2290
|
1471 |
template <typename TargetMap, typename SourceMap>
|
deba@2290
|
1472 |
BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
|
deba@2290
|
1473 |
uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
|
deba@2290
|
1474 |
UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
|
deba@2290
|
1475 |
return *this;
|
deba@2290
|
1476 |
}
|
deba@2290
|
1477 |
|
deba@2290
|
1478 |
/// \brief Make a copy of the given undirected edge.
|
deba@2290
|
1479 |
///
|
deba@2290
|
1480 |
/// Make a copy of the given undirected edge.
|
deba@2386
|
1481 |
BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
|
deba@2290
|
1482 |
uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
|
deba@2386
|
1483 |
UEdgeRefMap, TUEdge>(tuedge, suedge));
|
deba@2290
|
1484 |
return *this;
|
deba@2290
|
1485 |
}
|
deba@2290
|
1486 |
|
deba@2290
|
1487 |
/// \brief Executes the copies.
|
deba@2290
|
1488 |
///
|
deba@2290
|
1489 |
/// Executes the copies.
|
deba@2290
|
1490 |
void run() {
|
deba@2290
|
1491 |
ANodeRefMap aNodeRefMap(source);
|
deba@2290
|
1492 |
BNodeRefMap bNodeRefMap(source);
|
deba@2290
|
1493 |
NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
|
deba@2290
|
1494 |
UEdgeRefMap uEdgeRefMap(source);
|
deba@2290
|
1495 |
EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
|
deba@2290
|
1496 |
_graph_utils_bits::BpUGraphCopySelector<Target>::
|
deba@2290
|
1497 |
copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
|
deba@2386
|
1498 |
for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
|
deba@2290
|
1499 |
aNodeMapCopies[i]->copy(source, aNodeRefMap);
|
deba@2290
|
1500 |
}
|
deba@2386
|
1501 |
for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
|
deba@2290
|
1502 |
bNodeMapCopies[i]->copy(source, bNodeRefMap);
|
deba@2290
|
1503 |
}
|
deba@2386
|
1504 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
deba@2290
|
1505 |
nodeMapCopies[i]->copy(source, nodeRefMap);
|
deba@2290
|
1506 |
}
|
deba@2386
|
1507 |
for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
|
deba@2290
|
1508 |
uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
|
deba@2290
|
1509 |
}
|
deba@2386
|
1510 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
deba@2290
|
1511 |
edgeMapCopies[i]->copy(source, edgeRefMap);
|
deba@2290
|
1512 |
}
|
deba@2290
|
1513 |
}
|
deba@2290
|
1514 |
|
deba@2290
|
1515 |
private:
|
deba@2290
|
1516 |
|
deba@2290
|
1517 |
const Source& source;
|
deba@2290
|
1518 |
Target& target;
|
deba@2290
|
1519 |
|
deba@2290
|
1520 |
std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* >
|
deba@2290
|
1521 |
aNodeMapCopies;
|
deba@2290
|
1522 |
|
deba@2290
|
1523 |
std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* >
|
deba@2290
|
1524 |
bNodeMapCopies;
|
deba@2290
|
1525 |
|
deba@2290
|
1526 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
|
deba@2290
|
1527 |
nodeMapCopies;
|
deba@2290
|
1528 |
|
deba@2290
|
1529 |
std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
|
deba@2290
|
1530 |
edgeMapCopies;
|
deba@2290
|
1531 |
|
deba@2290
|
1532 |
std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
|
deba@2290
|
1533 |
uEdgeMapCopies;
|
deba@2290
|
1534 |
|
deba@2290
|
1535 |
};
|
deba@2290
|
1536 |
|
deba@2290
|
1537 |
/// \brief Copy a bipartite undirected graph to another graph.
|
deba@2290
|
1538 |
///
|
deba@2290
|
1539 |
/// Copy a bipartite undirected graph to another graph.
|
deba@2290
|
1540 |
/// The usage of the function:
|
deba@2290
|
1541 |
///
|
deba@2290
|
1542 |
///\code
|
deba@2290
|
1543 |
/// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
|
deba@2290
|
1544 |
///\endcode
|
deba@2290
|
1545 |
///
|
deba@2290
|
1546 |
/// After the copy the \c nr map will contain the mapping from the
|
deba@2290
|
1547 |
/// source graph's nodes to the target graph's nodes and the \c ecr will
|
deba@2290
|
1548 |
/// contain the mapping from the target graph's edges to the source's
|
deba@2290
|
1549 |
/// edges.
|
deba@2290
|
1550 |
///
|
deba@2290
|
1551 |
/// \see BpUGraphCopy
|
deba@2290
|
1552 |
template <typename Target, typename Source>
|
deba@2290
|
1553 |
BpUGraphCopy<Target, Source>
|
deba@2290
|
1554 |
copyBpUGraph(Target& target, const Source& source) {
|
deba@2290
|
1555 |
return BpUGraphCopy<Target, Source>(target, source);
|
deba@2290
|
1556 |
}
|
deba@2290
|
1557 |
|
deba@1192
|
1558 |
|
deba@1192
|
1559 |
/// @}
|
alpar@1402
|
1560 |
|
alpar@1402
|
1561 |
/// \addtogroup graph_maps
|
alpar@1402
|
1562 |
/// @{
|
alpar@1402
|
1563 |
|
deba@1413
|
1564 |
/// Provides an immutable and unique id for each item in the graph.
|
deba@1413
|
1565 |
|
athos@1540
|
1566 |
/// The IdMap class provides a unique and immutable id for each item of the
|
athos@1540
|
1567 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
|
athos@1540
|
1568 |
/// different items (nodes) get different ids <li>\b immutable: the id of an
|
athos@1540
|
1569 |
/// item (node) does not change (even if you delete other nodes). </ul>
|
athos@1540
|
1570 |
/// Through this map you get access (i.e. can read) the inner id values of
|
athos@1540
|
1571 |
/// the items stored in the graph. This map can be inverted with its member
|
athos@1540
|
1572 |
/// class \c InverseMap.
|
deba@1413
|
1573 |
///
|
deba@1413
|
1574 |
template <typename _Graph, typename _Item>
|
deba@1413
|
1575 |
class IdMap {
|
deba@1413
|
1576 |
public:
|
deba@1413
|
1577 |
typedef _Graph Graph;
|
deba@1413
|
1578 |
typedef int Value;
|
deba@1413
|
1579 |
typedef _Item Item;
|
deba@1413
|
1580 |
typedef _Item Key;
|
deba@1413
|
1581 |
|
deba@1413
|
1582 |
/// \brief Constructor.
|
deba@1413
|
1583 |
///
|
deba@2331
|
1584 |
/// Constructor of the map.
|
deba@2286
|
1585 |
explicit IdMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
1586 |
|
deba@1413
|
1587 |
/// \brief Gives back the \e id of the item.
|
deba@1413
|
1588 |
///
|
deba@2331
|
1589 |
/// Gives back the immutable and unique \e id of the item.
|
deba@1413
|
1590 |
int operator[](const Item& item) const { return graph->id(item);}
|
deba@1413
|
1591 |
|
deba@2331
|
1592 |
/// \brief Gives back the item by its id.
|
deba@2331
|
1593 |
///
|
deba@2331
|
1594 |
/// Gives back the item by its id.
|
deba@2331
|
1595 |
Item operator()(int id) { return graph->fromId(id, Item()); }
|
deba@1413
|
1596 |
|
deba@1413
|
1597 |
private:
|
deba@1413
|
1598 |
const Graph* graph;
|
deba@1413
|
1599 |
|
deba@1413
|
1600 |
public:
|
deba@1413
|
1601 |
|
athos@1540
|
1602 |
/// \brief The class represents the inverse of its owner (IdMap).
|
deba@1413
|
1603 |
///
|
athos@1540
|
1604 |
/// The class represents the inverse of its owner (IdMap).
|
deba@1413
|
1605 |
/// \see inverse()
|
deba@1413
|
1606 |
class InverseMap {
|
deba@1413
|
1607 |
public:
|
deba@1419
|
1608 |
|
deba@1413
|
1609 |
/// \brief Constructor.
|
deba@1413
|
1610 |
///
|
deba@1413
|
1611 |
/// Constructor for creating an id-to-item map.
|
deba@2286
|
1612 |
explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
1613 |
|
deba@1413
|
1614 |
/// \brief Constructor.
|
deba@1413
|
1615 |
///
|
deba@1413
|
1616 |
/// Constructor for creating an id-to-item map.
|
deba@2286
|
1617 |
explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
|
deba@1413
|
1618 |
|
deba@1413
|
1619 |
/// \brief Gives back the given item from its id.
|
deba@1413
|
1620 |
///
|
deba@1413
|
1621 |
/// Gives back the given item from its id.
|
deba@1413
|
1622 |
///
|
deba@1413
|
1623 |
Item operator[](int id) const { return graph->fromId(id, Item());}
|
deba@2331
|
1624 |
|
deba@1413
|
1625 |
private:
|
deba@1413
|
1626 |
const Graph* graph;
|
deba@1413
|
1627 |
};
|
deba@1413
|
1628 |
|
deba@1413
|
1629 |
/// \brief Gives back the inverse of the map.
|
deba@1413
|
1630 |
///
|
athos@1540
|
1631 |
/// Gives back the inverse of the IdMap.
|
deba@1413
|
1632 |
InverseMap inverse() const { return InverseMap(*graph);}
|
deba@1413
|
1633 |
|
deba@1413
|
1634 |
};
|
deba@1413
|
1635 |
|
deba@1413
|
1636 |
|
athos@1526
|
1637 |
/// \brief General invertable graph-map type.
|
alpar@1402
|
1638 |
|
athos@1540
|
1639 |
/// This type provides simple invertable graph-maps.
|
athos@1526
|
1640 |
/// The InvertableMap wraps an arbitrary ReadWriteMap
|
athos@1526
|
1641 |
/// and if a key is set to a new value then store it
|
alpar@1402
|
1642 |
/// in the inverse map.
|
deba@1931
|
1643 |
///
|
deba@1931
|
1644 |
/// The values of the map can be accessed
|
deba@1931
|
1645 |
/// with stl compatible forward iterator.
|
deba@1931
|
1646 |
///
|
alpar@1402
|
1647 |
/// \param _Graph The graph type.
|
deba@1830
|
1648 |
/// \param _Item The item type of the graph.
|
deba@1830
|
1649 |
/// \param _Value The value type of the map.
|
deba@1931
|
1650 |
///
|
deba@1931
|
1651 |
/// \see IterableValueMap
|
deba@1830
|
1652 |
template <typename _Graph, typename _Item, typename _Value>
|
deba@2287
|
1653 |
class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
|
deba@1931
|
1654 |
private:
|
deba@1931
|
1655 |
|
deba@2287
|
1656 |
typedef DefaultMap<_Graph, _Item, _Value> Map;
|
deba@1931
|
1657 |
typedef _Graph Graph;
|
deba@1931
|
1658 |
|
deba@2287
|
1659 |
typedef std::map<_Value, _Item> Container;
|
deba@1931
|
1660 |
Container invMap;
|
deba@1931
|
1661 |
|
deba@1931
|
1662 |
public:
|
deba@1931
|
1663 |
|
deba@2287
|
1664 |
/// The key type of InvertableMap (Node, Edge, UEdge).
|
deba@2287
|
1665 |
typedef typename Map::Key Key;
|
deba@2287
|
1666 |
/// The value type of the InvertableMap.
|
deba@2287
|
1667 |
typedef typename Map::Value Value;
|
deba@2287
|
1668 |
|
deba@1931
|
1669 |
|
deba@1931
|
1670 |
|
alpar@1402
|
1671 |
/// \brief Constructor.
|
alpar@1402
|
1672 |
///
|
deba@1413
|
1673 |
/// Construct a new InvertableMap for the graph.
|
alpar@1402
|
1674 |
///
|
deba@2286
|
1675 |
explicit InvertableMap(const Graph& graph) : Map(graph) {}
|
deba@1931
|
1676 |
|
deba@1931
|
1677 |
/// \brief Forward iterator for values.
|
deba@1931
|
1678 |
///
|
deba@1931
|
1679 |
/// This iterator is an stl compatible forward
|
deba@1931
|
1680 |
/// iterator on the values of the map. The values can
|
deba@1931
|
1681 |
/// be accessed in the [beginValue, endValue) range.
|
deba@1931
|
1682 |
///
|
deba@1931
|
1683 |
class ValueIterator
|
deba@1931
|
1684 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
deba@1931
|
1685 |
friend class InvertableMap;
|
deba@1931
|
1686 |
private:
|
deba@1931
|
1687 |
ValueIterator(typename Container::const_iterator _it)
|
deba@1931
|
1688 |
: it(_it) {}
|
deba@1931
|
1689 |
public:
|
deba@1931
|
1690 |
|
deba@1931
|
1691 |
ValueIterator() {}
|
deba@1931
|
1692 |
|
deba@1931
|
1693 |
ValueIterator& operator++() { ++it; return *this; }
|
deba@1931
|
1694 |
ValueIterator operator++(int) {
|
deba@1931
|
1695 |
ValueIterator tmp(*this);
|
deba@1931
|
1696 |
operator++();
|
deba@1931
|
1697 |
return tmp;
|
deba@1931
|
1698 |
}
|
deba@1931
|
1699 |
|
deba@1931
|
1700 |
const Value& operator*() const { return it->first; }
|
deba@1931
|
1701 |
const Value* operator->() const { return &(it->first); }
|
deba@1931
|
1702 |
|
deba@1931
|
1703 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
deba@1931
|
1704 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
deba@1931
|
1705 |
|
deba@1931
|
1706 |
private:
|
deba@1931
|
1707 |
typename Container::const_iterator it;
|
deba@1931
|
1708 |
};
|
deba@1931
|
1709 |
|
deba@1931
|
1710 |
/// \brief Returns an iterator to the first value.
|
deba@1931
|
1711 |
///
|
deba@1931
|
1712 |
/// Returns an stl compatible iterator to the
|
deba@1931
|
1713 |
/// first value of the map. The values of the
|
deba@1931
|
1714 |
/// map can be accessed in the [beginValue, endValue)
|
deba@1931
|
1715 |
/// range.
|
deba@1931
|
1716 |
ValueIterator beginValue() const {
|
deba@1931
|
1717 |
return ValueIterator(invMap.begin());
|
deba@1931
|
1718 |
}
|
deba@1931
|
1719 |
|
deba@1931
|
1720 |
/// \brief Returns an iterator after the last value.
|
deba@1931
|
1721 |
///
|
deba@1931
|
1722 |
/// Returns an stl compatible iterator after the
|
deba@1931
|
1723 |
/// last value of the map. The values of the
|
deba@1931
|
1724 |
/// map can be accessed in the [beginValue, endValue)
|
deba@1931
|
1725 |
/// range.
|
deba@1931
|
1726 |
ValueIterator endValue() const {
|
deba@1931
|
1727 |
return ValueIterator(invMap.end());
|
deba@1931
|
1728 |
}
|
alpar@1402
|
1729 |
|
alpar@1402
|
1730 |
/// \brief The setter function of the map.
|
alpar@1402
|
1731 |
///
|
deba@1413
|
1732 |
/// Sets the mapped value.
|
alpar@1402
|
1733 |
void set(const Key& key, const Value& val) {
|
alpar@1402
|
1734 |
Value oldval = Map::operator[](key);
|
deba@1413
|
1735 |
typename Container::iterator it = invMap.find(oldval);
|
alpar@1402
|
1736 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
1737 |
invMap.erase(it);
|
alpar@1402
|
1738 |
}
|
alpar@1402
|
1739 |
invMap.insert(make_pair(val, key));
|
alpar@1402
|
1740 |
Map::set(key, val);
|
alpar@1402
|
1741 |
}
|
alpar@1402
|
1742 |
|
alpar@1402
|
1743 |
/// \brief The getter function of the map.
|
alpar@1402
|
1744 |
///
|
alpar@1402
|
1745 |
/// It gives back the value associated with the key.
|
deba@1931
|
1746 |
typename MapTraits<Map>::ConstReturnValue
|
deba@1931
|
1747 |
operator[](const Key& key) const {
|
alpar@1402
|
1748 |
return Map::operator[](key);
|
alpar@1402
|
1749 |
}
|
alpar@1402
|
1750 |
|
deba@2331
|
1751 |
/// \brief Gives back the item by its value.
|
deba@2331
|
1752 |
///
|
deba@2331
|
1753 |
/// Gives back the item by its value.
|
deba@2331
|
1754 |
Key operator()(const Value& key) const {
|
deba@2331
|
1755 |
typename Container::const_iterator it = invMap.find(key);
|
deba@2331
|
1756 |
return it != invMap.end() ? it->second : INVALID;
|
deba@2331
|
1757 |
}
|
deba@2331
|
1758 |
|
deba@1515
|
1759 |
protected:
|
deba@1515
|
1760 |
|
alpar@1402
|
1761 |
/// \brief Erase the key from the map.
|
alpar@1402
|
1762 |
///
|
alpar@1402
|
1763 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
1764 |
/// \c AlterationNotifier.
|
alpar@1402
|
1765 |
virtual void erase(const Key& key) {
|
alpar@1402
|
1766 |
Value val = Map::operator[](key);
|
deba@1413
|
1767 |
typename Container::iterator it = invMap.find(val);
|
alpar@1402
|
1768 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
1769 |
invMap.erase(it);
|
alpar@1402
|
1770 |
}
|
alpar@1402
|
1771 |
Map::erase(key);
|
alpar@1402
|
1772 |
}
|
alpar@1402
|
1773 |
|
deba@1829
|
1774 |
/// \brief Erase more keys from the map.
|
deba@1829
|
1775 |
///
|
deba@1829
|
1776 |
/// Erase more keys from the map. It is called by the
|
deba@1829
|
1777 |
/// \c AlterationNotifier.
|
deba@1829
|
1778 |
virtual void erase(const std::vector<Key>& keys) {
|
deba@2386
|
1779 |
for (int i = 0; i < int(keys.size()); ++i) {
|
deba@1829
|
1780 |
Value val = Map::operator[](keys[i]);
|
deba@1829
|
1781 |
typename Container::iterator it = invMap.find(val);
|
deba@1829
|
1782 |
if (it != invMap.end() && it->second == keys[i]) {
|
deba@1829
|
1783 |
invMap.erase(it);
|
deba@1829
|
1784 |
}
|
deba@1829
|
1785 |
}
|
deba@1829
|
1786 |
Map::erase(keys);
|
deba@1829
|
1787 |
}
|
deba@1829
|
1788 |
|
alpar@1402
|
1789 |
/// \brief Clear the keys from the map and inverse map.
|
alpar@1402
|
1790 |
///
|
alpar@1402
|
1791 |
/// Clear the keys from the map and inverse map. It is called by the
|
alpar@1402
|
1792 |
/// \c AlterationNotifier.
|
alpar@1402
|
1793 |
virtual void clear() {
|
alpar@1402
|
1794 |
invMap.clear();
|
alpar@1402
|
1795 |
Map::clear();
|
alpar@1402
|
1796 |
}
|
alpar@1402
|
1797 |
|
deba@1413
|
1798 |
public:
|
deba@1413
|
1799 |
|
deba@1413
|
1800 |
/// \brief The inverse map type.
|
deba@1413
|
1801 |
///
|
deba@1413
|
1802 |
/// The inverse of this map. The subscript operator of the map
|
deba@1413
|
1803 |
/// gives back always the item what was last assigned to the value.
|
deba@1413
|
1804 |
class InverseMap {
|
deba@1413
|
1805 |
public:
|
deba@1413
|
1806 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
1807 |
///
|
deba@1413
|
1808 |
/// Constructor of the InverseMap.
|
deba@2286
|
1809 |
explicit InverseMap(const InvertableMap& _inverted)
|
deba@2286
|
1810 |
: inverted(_inverted) {}
|
deba@1413
|
1811 |
|
deba@1413
|
1812 |
/// The value type of the InverseMap.
|
deba@1413
|
1813 |
typedef typename InvertableMap::Key Value;
|
deba@1413
|
1814 |
/// The key type of the InverseMap.
|
deba@1413
|
1815 |
typedef typename InvertableMap::Value Key;
|
deba@1413
|
1816 |
|
deba@1413
|
1817 |
/// \brief Subscript operator.
|
deba@1413
|
1818 |
///
|
deba@1413
|
1819 |
/// Subscript operator. It gives back always the item
|
deba@1413
|
1820 |
/// what was last assigned to the value.
|
deba@1413
|
1821 |
Value operator[](const Key& key) const {
|
deba@2331
|
1822 |
return inverted(key);
|
deba@1413
|
1823 |
}
|
deba@1413
|
1824 |
|
deba@1413
|
1825 |
private:
|
deba@1413
|
1826 |
const InvertableMap& inverted;
|
deba@1413
|
1827 |
};
|
deba@1413
|
1828 |
|
alpar@2094
|
1829 |
/// \brief It gives back the just readable inverse map.
|
alpar@1402
|
1830 |
///
|
alpar@2094
|
1831 |
/// It gives back the just readable inverse map.
|
deba@1413
|
1832 |
InverseMap inverse() const {
|
deba@1413
|
1833 |
return InverseMap(*this);
|
alpar@1402
|
1834 |
}
|
alpar@1402
|
1835 |
|
alpar@1402
|
1836 |
|
deba@1413
|
1837 |
|
alpar@1402
|
1838 |
};
|
alpar@1402
|
1839 |
|
alpar@1402
|
1840 |
/// \brief Provides a mutable, continuous and unique descriptor for each
|
alpar@1402
|
1841 |
/// item in the graph.
|
alpar@1402
|
1842 |
///
|
athos@1540
|
1843 |
/// The DescriptorMap class provides a unique and continuous (but mutable)
|
athos@1540
|
1844 |
/// descriptor (id) for each item of the same type (e.g. node) in the
|
athos@1540
|
1845 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get
|
athos@1540
|
1846 |
/// different ids <li>\b continuous: the range of the ids is the set of
|
athos@1540
|
1847 |
/// integers between 0 and \c n-1, where \c n is the number of the items of
|
athos@1540
|
1848 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an
|
athos@1540
|
1849 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted
|
athos@1540
|
1850 |
/// with its member class \c InverseMap.
|
alpar@1402
|
1851 |
///
|
alpar@1402
|
1852 |
/// \param _Graph The graph class the \c DescriptorMap belongs to.
|
alpar@1402
|
1853 |
/// \param _Item The Item is the Key of the Map. It may be Node, Edge or
|
klao@1909
|
1854 |
/// UEdge.
|
deba@1830
|
1855 |
template <typename _Graph, typename _Item>
|
deba@2287
|
1856 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
|
alpar@1402
|
1857 |
|
alpar@1402
|
1858 |
typedef _Item Item;
|
deba@2287
|
1859 |
typedef DefaultMap<_Graph, _Item, int> Map;
|
alpar@1402
|
1860 |
|
alpar@1402
|
1861 |
public:
|
alpar@1402
|
1862 |
/// The graph class of DescriptorMap.
|
alpar@1402
|
1863 |
typedef _Graph Graph;
|
alpar@1402
|
1864 |
|
klao@1909
|
1865 |
/// The key type of DescriptorMap (Node, Edge, UEdge).
|
deba@2287
|
1866 |
typedef typename Map::Key Key;
|
alpar@1402
|
1867 |
/// The value type of DescriptorMap.
|
deba@2287
|
1868 |
typedef typename Map::Value Value;
|
alpar@1402
|
1869 |
|
alpar@1402
|
1870 |
/// \brief Constructor.
|
alpar@1402
|
1871 |
///
|
deba@1413
|
1872 |
/// Constructor for descriptor map.
|
deba@2286
|
1873 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
|
deba@2201
|
1874 |
Item it;
|
deba@2386
|
1875 |
const typename Map::Notifier* nf = Map::notifier();
|
deba@2386
|
1876 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
deba@2201
|
1877 |
Map::set(it, invMap.size());
|
deba@2201
|
1878 |
invMap.push_back(it);
|
deba@2201
|
1879 |
}
|
alpar@1402
|
1880 |
}
|
alpar@1402
|
1881 |
|
deba@1515
|
1882 |
protected:
|
deba@1515
|
1883 |
|
alpar@1402
|
1884 |
/// \brief Add a new key to the map.
|
alpar@1402
|
1885 |
///
|
alpar@1402
|
1886 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
1887 |
/// \c AlterationNotifier.
|
alpar@1402
|
1888 |
virtual void add(const Item& item) {
|
alpar@1402
|
1889 |
Map::add(item);
|
alpar@1402
|
1890 |
Map::set(item, invMap.size());
|
alpar@1402
|
1891 |
invMap.push_back(item);
|
alpar@1402
|
1892 |
}
|
alpar@1402
|
1893 |
|
deba@1829
|
1894 |
/// \brief Add more new keys to the map.
|
deba@1829
|
1895 |
///
|
deba@1829
|
1896 |
/// Add more new keys to the map. It is called by the
|
deba@1829
|
1897 |
/// \c AlterationNotifier.
|
deba@1829
|
1898 |
virtual void add(const std::vector<Item>& items) {
|
deba@1829
|
1899 |
Map::add(items);
|
deba@2386
|
1900 |
for (int i = 0; i < int(items.size()); ++i) {
|
deba@1829
|
1901 |
Map::set(items[i], invMap.size());
|
deba@1829
|
1902 |
invMap.push_back(items[i]);
|
deba@1829
|
1903 |
}
|
deba@1829
|
1904 |
}
|
deba@1829
|
1905 |
|
alpar@1402
|
1906 |
/// \brief Erase the key from the map.
|
alpar@1402
|
1907 |
///
|
deba@1829
|
1908 |
/// Erase the key from the map. It is called by the
|
alpar@1402
|
1909 |
/// \c AlterationNotifier.
|
alpar@1402
|
1910 |
virtual void erase(const Item& item) {
|
alpar@1402
|
1911 |
Map::set(invMap.back(), Map::operator[](item));
|
alpar@1402
|
1912 |
invMap[Map::operator[](item)] = invMap.back();
|
deba@1413
|
1913 |
invMap.pop_back();
|
alpar@1402
|
1914 |
Map::erase(item);
|
alpar@1402
|
1915 |
}
|
alpar@1402
|
1916 |
|
deba@1829
|
1917 |
/// \brief Erase more keys from the map.
|
deba@1829
|
1918 |
///
|
deba@1829
|
1919 |
/// Erase more keys from the map. It is called by the
|
deba@1829
|
1920 |
/// \c AlterationNotifier.
|
deba@1829
|
1921 |
virtual void erase(const std::vector<Item>& items) {
|
deba@2386
|
1922 |
for (int i = 0; i < int(items.size()); ++i) {
|
deba@1829
|
1923 |
Map::set(invMap.back(), Map::operator[](items[i]));
|
deba@1829
|
1924 |
invMap[Map::operator[](items[i])] = invMap.back();
|
deba@1829
|
1925 |
invMap.pop_back();
|
deba@1829
|
1926 |
}
|
deba@1829
|
1927 |
Map::erase(items);
|
deba@1829
|
1928 |
}
|
deba@1829
|
1929 |
|
alpar@1402
|
1930 |
/// \brief Build the unique map.
|
alpar@1402
|
1931 |
///
|
alpar@1402
|
1932 |
/// Build the unique map. It is called by the
|
alpar@1402
|
1933 |
/// \c AlterationNotifier.
|
alpar@1402
|
1934 |
virtual void build() {
|
alpar@1402
|
1935 |
Map::build();
|
alpar@1402
|
1936 |
Item it;
|
deba@2386
|
1937 |
const typename Map::Notifier* nf = Map::notifier();
|
deba@2386
|
1938 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
alpar@1402
|
1939 |
Map::set(it, invMap.size());
|
alpar@1402
|
1940 |
invMap.push_back(it);
|
alpar@1402
|
1941 |
}
|
alpar@1402
|
1942 |
}
|
alpar@1402
|
1943 |
|
alpar@1402
|
1944 |
/// \brief Clear the keys from the map.
|
alpar@1402
|
1945 |
///
|
alpar@1402
|
1946 |
/// Clear the keys from the map. It is called by the
|
alpar@1402
|
1947 |
/// \c AlterationNotifier.
|
alpar@1402
|
1948 |
virtual void clear() {
|
alpar@1402
|
1949 |
invMap.clear();
|
alpar@1402
|
1950 |
Map::clear();
|
alpar@1402
|
1951 |
}
|
alpar@1402
|
1952 |
|
deba@1538
|
1953 |
public:
|
deba@1538
|
1954 |
|
deba@1931
|
1955 |
/// \brief Returns the maximal value plus one.
|
deba@1931
|
1956 |
///
|
deba@1931
|
1957 |
/// Returns the maximal value plus one in the map.
|
deba@1931
|
1958 |
unsigned int size() const {
|
deba@1931
|
1959 |
return invMap.size();
|
deba@1931
|
1960 |
}
|
deba@1931
|
1961 |
|
deba@1552
|
1962 |
/// \brief Swaps the position of the two items in the map.
|
deba@1552
|
1963 |
///
|
deba@1552
|
1964 |
/// Swaps the position of the two items in the map.
|
deba@1552
|
1965 |
void swap(const Item& p, const Item& q) {
|
deba@1552
|
1966 |
int pi = Map::operator[](p);
|
deba@1552
|
1967 |
int qi = Map::operator[](q);
|
deba@1552
|
1968 |
Map::set(p, qi);
|
deba@1552
|
1969 |
invMap[qi] = p;
|
deba@1552
|
1970 |
Map::set(q, pi);
|
deba@1552
|
1971 |
invMap[pi] = q;
|
deba@1552
|
1972 |
}
|
deba@1552
|
1973 |
|
alpar@1402
|
1974 |
/// \brief Gives back the \e descriptor of the item.
|
alpar@1402
|
1975 |
///
|
alpar@1402
|
1976 |
/// Gives back the mutable and unique \e descriptor of the map.
|
alpar@1402
|
1977 |
int operator[](const Item& item) const {
|
alpar@1402
|
1978 |
return Map::operator[](item);
|
alpar@1402
|
1979 |
}
|
deba@2331
|
1980 |
|
deba@2331
|
1981 |
/// \brief Gives back the item by its descriptor.
|
deba@2331
|
1982 |
///
|
deba@2331
|
1983 |
/// Gives back th item by its descriptor.
|
deba@2331
|
1984 |
Item operator()(int id) const {
|
deba@2331
|
1985 |
return invMap[id];
|
deba@2331
|
1986 |
}
|
alpar@1402
|
1987 |
|
deba@1413
|
1988 |
private:
|
deba@1413
|
1989 |
|
deba@1413
|
1990 |
typedef std::vector<Item> Container;
|
deba@1413
|
1991 |
Container invMap;
|
deba@1413
|
1992 |
|
deba@1413
|
1993 |
public:
|
athos@1540
|
1994 |
/// \brief The inverse map type of DescriptorMap.
|
deba@1413
|
1995 |
///
|
athos@1540
|
1996 |
/// The inverse map type of DescriptorMap.
|
deba@1413
|
1997 |
class InverseMap {
|
deba@1413
|
1998 |
public:
|
deba@1413
|
1999 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
2000 |
///
|
deba@1413
|
2001 |
/// Constructor of the InverseMap.
|
deba@2286
|
2002 |
explicit InverseMap(const DescriptorMap& _inverted)
|
deba@1413
|
2003 |
: inverted(_inverted) {}
|
deba@1413
|
2004 |
|
deba@1413
|
2005 |
|
deba@1413
|
2006 |
/// The value type of the InverseMap.
|
deba@1413
|
2007 |
typedef typename DescriptorMap::Key Value;
|
deba@1413
|
2008 |
/// The key type of the InverseMap.
|
deba@1413
|
2009 |
typedef typename DescriptorMap::Value Key;
|
deba@1413
|
2010 |
|
deba@1413
|
2011 |
/// \brief Subscript operator.
|
deba@1413
|
2012 |
///
|
deba@1413
|
2013 |
/// Subscript operator. It gives back the item
|
deba@1413
|
2014 |
/// that the descriptor belongs to currently.
|
deba@1413
|
2015 |
Value operator[](const Key& key) const {
|
deba@2331
|
2016 |
return inverted(key);
|
deba@1413
|
2017 |
}
|
deba@1470
|
2018 |
|
deba@1470
|
2019 |
/// \brief Size of the map.
|
deba@1470
|
2020 |
///
|
deba@1470
|
2021 |
/// Returns the size of the map.
|
deba@1931
|
2022 |
unsigned int size() const {
|
deba@2331
|
2023 |
return inverted.size();
|
deba@1470
|
2024 |
}
|
deba@1413
|
2025 |
|
deba@1413
|
2026 |
private:
|
deba@1413
|
2027 |
const DescriptorMap& inverted;
|
deba@1413
|
2028 |
};
|
deba@1413
|
2029 |
|
alpar@1402
|
2030 |
/// \brief Gives back the inverse of the map.
|
alpar@1402
|
2031 |
///
|
alpar@1402
|
2032 |
/// Gives back the inverse of the map.
|
alpar@1402
|
2033 |
const InverseMap inverse() const {
|
deba@1413
|
2034 |
return InverseMap(*this);
|
alpar@1402
|
2035 |
}
|
alpar@1402
|
2036 |
};
|
alpar@1402
|
2037 |
|
alpar@1402
|
2038 |
/// \brief Returns the source of the given edge.
|
alpar@1402
|
2039 |
///
|
alpar@1402
|
2040 |
/// The SourceMap gives back the source Node of the given edge.
|
kpeter@2474
|
2041 |
/// \see TargetMap
|
alpar@1402
|
2042 |
/// \author Balazs Dezso
|
alpar@1402
|
2043 |
template <typename Graph>
|
alpar@1402
|
2044 |
class SourceMap {
|
alpar@1402
|
2045 |
public:
|
deba@1419
|
2046 |
|
alpar@1402
|
2047 |
typedef typename Graph::Node Value;
|
alpar@1402
|
2048 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
2049 |
|
alpar@1402
|
2050 |
/// \brief Constructor
|
alpar@1402
|
2051 |
///
|
alpar@1402
|
2052 |
/// Constructor
|
alpar@1402
|
2053 |
/// \param _graph The graph that the map belongs to.
|
deba@2286
|
2054 |
explicit SourceMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
2055 |
|
alpar@1402
|
2056 |
/// \brief The subscript operator.
|
alpar@1402
|
2057 |
///
|
alpar@1402
|
2058 |
/// The subscript operator.
|
alpar@1402
|
2059 |
/// \param edge The edge
|
alpar@1402
|
2060 |
/// \return The source of the edge
|
deba@1679
|
2061 |
Value operator[](const Key& edge) const {
|
alpar@1402
|
2062 |
return graph.source(edge);
|
alpar@1402
|
2063 |
}
|
alpar@1402
|
2064 |
|
alpar@1402
|
2065 |
private:
|
alpar@1402
|
2066 |
const Graph& graph;
|
alpar@1402
|
2067 |
};
|
alpar@1402
|
2068 |
|
kpeter@2474
|
2069 |
/// \brief Returns a \ref SourceMap class.
|
alpar@1402
|
2070 |
///
|
alpar@1402
|
2071 |
/// This function just returns an \ref SourceMap class.
|
alpar@1402
|
2072 |
/// \relates SourceMap
|
alpar@1402
|
2073 |
template <typename Graph>
|
alpar@1402
|
2074 |
inline SourceMap<Graph> sourceMap(const Graph& graph) {
|
alpar@1402
|
2075 |
return SourceMap<Graph>(graph);
|
alpar@1402
|
2076 |
}
|
alpar@1402
|
2077 |
|
alpar@1402
|
2078 |
/// \brief Returns the target of the given edge.
|
alpar@1402
|
2079 |
///
|
alpar@1402
|
2080 |
/// The TargetMap gives back the target Node of the given edge.
|
kpeter@2474
|
2081 |
/// \see SourceMap
|
alpar@1402
|
2082 |
/// \author Balazs Dezso
|
alpar@1402
|
2083 |
template <typename Graph>
|
alpar@1402
|
2084 |
class TargetMap {
|
alpar@1402
|
2085 |
public:
|
deba@1419
|
2086 |
|
alpar@1402
|
2087 |
typedef typename Graph::Node Value;
|
alpar@1402
|
2088 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
2089 |
|
alpar@1402
|
2090 |
/// \brief Constructor
|
alpar@1402
|
2091 |
///
|
alpar@1402
|
2092 |
/// Constructor
|
alpar@1402
|
2093 |
/// \param _graph The graph that the map belongs to.
|
deba@2286
|
2094 |
explicit TargetMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
2095 |
|
alpar@1402
|
2096 |
/// \brief The subscript operator.
|
alpar@1402
|
2097 |
///
|
alpar@1402
|
2098 |
/// The subscript operator.
|
alpar@1536
|
2099 |
/// \param e The edge
|
alpar@1402
|
2100 |
/// \return The target of the edge
|
deba@1679
|
2101 |
Value operator[](const Key& e) const {
|
alpar@1536
|
2102 |
return graph.target(e);
|
alpar@1402
|
2103 |
}
|
alpar@1402
|
2104 |
|
alpar@1402
|
2105 |
private:
|
alpar@1402
|
2106 |
const Graph& graph;
|
alpar@1402
|
2107 |
};
|
alpar@1402
|
2108 |
|
kpeter@2474
|
2109 |
/// \brief Returns a \ref TargetMap class.
|
deba@1515
|
2110 |
///
|
athos@1540
|
2111 |
/// This function just returns a \ref TargetMap class.
|
alpar@1402
|
2112 |
/// \relates TargetMap
|
alpar@1402
|
2113 |
template <typename Graph>
|
alpar@1402
|
2114 |
inline TargetMap<Graph> targetMap(const Graph& graph) {
|
alpar@1402
|
2115 |
return TargetMap<Graph>(graph);
|
alpar@1402
|
2116 |
}
|
alpar@1402
|
2117 |
|
athos@1540
|
2118 |
/// \brief Returns the "forward" directed edge view of an undirected edge.
|
deba@1419
|
2119 |
///
|
athos@1540
|
2120 |
/// Returns the "forward" directed edge view of an undirected edge.
|
kpeter@2474
|
2121 |
/// \see BackwardMap
|
deba@1419
|
2122 |
/// \author Balazs Dezso
|
deba@1419
|
2123 |
template <typename Graph>
|
deba@1419
|
2124 |
class ForwardMap {
|
deba@1419
|
2125 |
public:
|
deba@1419
|
2126 |
|
deba@1419
|
2127 |
typedef typename Graph::Edge Value;
|
klao@1909
|
2128 |
typedef typename Graph::UEdge Key;
|
deba@1419
|
2129 |
|
deba@1419
|
2130 |
/// \brief Constructor
|
deba@1419
|
2131 |
///
|
deba@1419
|
2132 |
/// Constructor
|
deba@1419
|
2133 |
/// \param _graph The graph that the map belongs to.
|
deba@2286
|
2134 |
explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
2135 |
|
deba@1419
|
2136 |
/// \brief The subscript operator.
|
deba@1419
|
2137 |
///
|
deba@1419
|
2138 |
/// The subscript operator.
|
deba@1419
|
2139 |
/// \param key An undirected edge
|
deba@1419
|
2140 |
/// \return The "forward" directed edge view of undirected edge
|
deba@1419
|
2141 |
Value operator[](const Key& key) const {
|
deba@1627
|
2142 |
return graph.direct(key, true);
|
deba@1419
|
2143 |
}
|
deba@1419
|
2144 |
|
deba@1419
|
2145 |
private:
|
deba@1419
|
2146 |
const Graph& graph;
|
deba@1419
|
2147 |
};
|
deba@1419
|
2148 |
|
kpeter@2474
|
2149 |
/// \brief Returns a \ref ForwardMap class.
|
deba@1515
|
2150 |
///
|
deba@1419
|
2151 |
/// This function just returns an \ref ForwardMap class.
|
deba@1419
|
2152 |
/// \relates ForwardMap
|
deba@1419
|
2153 |
template <typename Graph>
|
deba@1419
|
2154 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
deba@1419
|
2155 |
return ForwardMap<Graph>(graph);
|
deba@1419
|
2156 |
}
|
deba@1419
|
2157 |
|
athos@1540
|
2158 |
/// \brief Returns the "backward" directed edge view of an undirected edge.
|
deba@1419
|
2159 |
///
|
athos@1540
|
2160 |
/// Returns the "backward" directed edge view of an undirected edge.
|
kpeter@2474
|
2161 |
/// \see ForwardMap
|
deba@1419
|
2162 |
/// \author Balazs Dezso
|
deba@1419
|
2163 |
template <typename Graph>
|
deba@1419
|
2164 |
class BackwardMap {
|
deba@1419
|
2165 |
public:
|
deba@1419
|
2166 |
|
deba@1419
|
2167 |
typedef typename Graph::Edge Value;
|
klao@1909
|
2168 |
typedef typename Graph::UEdge Key;
|
deba@1419
|
2169 |
|
deba@1419
|
2170 |
/// \brief Constructor
|
deba@1419
|
2171 |
///
|
deba@1419
|
2172 |
/// Constructor
|
deba@1419
|
2173 |
/// \param _graph The graph that the map belongs to.
|
deba@2286
|
2174 |
explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
2175 |
|
deba@1419
|
2176 |
/// \brief The subscript operator.
|
deba@1419
|
2177 |
///
|
deba@1419
|
2178 |
/// The subscript operator.
|
deba@1419
|
2179 |
/// \param key An undirected edge
|
deba@1419
|
2180 |
/// \return The "backward" directed edge view of undirected edge
|
deba@1419
|
2181 |
Value operator[](const Key& key) const {
|
deba@1627
|
2182 |
return graph.direct(key, false);
|
deba@1419
|
2183 |
}
|
deba@1419
|
2184 |
|
deba@1419
|
2185 |
private:
|
deba@1419
|
2186 |
const Graph& graph;
|
deba@1419
|
2187 |
};
|
deba@1419
|
2188 |
|
deba@1419
|
2189 |
/// \brief Returns a \ref BackwardMap class
|
deba@1419
|
2190 |
|
athos@1540
|
2191 |
/// This function just returns a \ref BackwardMap class.
|
deba@1419
|
2192 |
/// \relates BackwardMap
|
deba@1419
|
2193 |
template <typename Graph>
|
deba@1419
|
2194 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
deba@1419
|
2195 |
return BackwardMap<Graph>(graph);
|
deba@1419
|
2196 |
}
|
deba@1419
|
2197 |
|
deba@1695
|
2198 |
/// \brief Potential difference map
|
deba@1695
|
2199 |
///
|
deba@1695
|
2200 |
/// If there is an potential map on the nodes then we
|
deba@1695
|
2201 |
/// can get an edge map as we get the substraction of the
|
deba@1695
|
2202 |
/// values of the target and source.
|
deba@1695
|
2203 |
template <typename Graph, typename NodeMap>
|
deba@1695
|
2204 |
class PotentialDifferenceMap {
|
deba@1515
|
2205 |
public:
|
deba@1695
|
2206 |
typedef typename Graph::Edge Key;
|
deba@1695
|
2207 |
typedef typename NodeMap::Value Value;
|
deba@1695
|
2208 |
|
deba@1695
|
2209 |
/// \brief Constructor
|
deba@1695
|
2210 |
///
|
deba@1695
|
2211 |
/// Contructor of the map
|
deba@2286
|
2212 |
explicit PotentialDifferenceMap(const Graph& _graph,
|
deba@2286
|
2213 |
const NodeMap& _potential)
|
deba@1695
|
2214 |
: graph(_graph), potential(_potential) {}
|
deba@1695
|
2215 |
|
deba@1695
|
2216 |
/// \brief Const subscription operator
|
deba@1695
|
2217 |
///
|
deba@1695
|
2218 |
/// Const subscription operator
|
deba@1695
|
2219 |
Value operator[](const Key& edge) const {
|
deba@1695
|
2220 |
return potential[graph.target(edge)] - potential[graph.source(edge)];
|
deba@1695
|
2221 |
}
|
deba@1695
|
2222 |
|
deba@1695
|
2223 |
private:
|
deba@1695
|
2224 |
const Graph& graph;
|
deba@1695
|
2225 |
const NodeMap& potential;
|
deba@1695
|
2226 |
};
|
deba@1695
|
2227 |
|
kpeter@2474
|
2228 |
/// \brief Returns a PotentialDifferenceMap.
|
deba@1695
|
2229 |
///
|
kpeter@2474
|
2230 |
/// This function just returns a PotentialDifferenceMap.
|
deba@1695
|
2231 |
/// \relates PotentialDifferenceMap
|
deba@1695
|
2232 |
template <typename Graph, typename NodeMap>
|
deba@1695
|
2233 |
PotentialDifferenceMap<Graph, NodeMap>
|
deba@1695
|
2234 |
potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
|
deba@1695
|
2235 |
return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
|
deba@1695
|
2236 |
}
|
deba@1695
|
2237 |
|
deba@1515
|
2238 |
/// \brief Map of the node in-degrees.
|
alpar@1453
|
2239 |
///
|
athos@1540
|
2240 |
/// This map returns the in-degree of a node. Once it is constructed,
|
deba@1515
|
2241 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
athos@1540
|
2242 |
/// in constant time. On the other hand, the values are updated automatically
|
deba@1515
|
2243 |
/// whenever the graph changes.
|
deba@1515
|
2244 |
///
|
deba@1729
|
2245 |
/// \warning Besides addNode() and addEdge(), a graph structure may provide
|
deba@1730
|
2246 |
/// alternative ways to modify the graph. The correct behavior of InDegMap
|
deba@1829
|
2247 |
/// is not guarantied if these additional features are used. For example
|
deba@1829
|
2248 |
/// the functions \ref ListGraph::changeSource() "changeSource()",
|
deba@1729
|
2249 |
/// \ref ListGraph::changeTarget() "changeTarget()" and
|
deba@1729
|
2250 |
/// \ref ListGraph::reverseEdge() "reverseEdge()"
|
deba@1729
|
2251 |
/// of \ref ListGraph will \e not update the degree values correctly.
|
deba@1729
|
2252 |
///
|
deba@1515
|
2253 |
/// \sa OutDegMap
|
deba@1515
|
2254 |
|
alpar@1453
|
2255 |
template <typename _Graph>
|
deba@1515
|
2256 |
class InDegMap
|
deba@1999
|
2257 |
: protected ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
2258 |
::ItemNotifier::ObserverBase {
|
deba@1515
|
2259 |
|
alpar@1453
|
2260 |
public:
|
deba@1515
|
2261 |
|
deba@1515
|
2262 |
typedef _Graph Graph;
|
alpar@1453
|
2263 |
typedef int Value;
|
deba@1515
|
2264 |
typedef typename Graph::Node Key;
|
deba@1515
|
2265 |
|
deba@1999
|
2266 |
typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
2267 |
::ItemNotifier::ObserverBase Parent;
|
deba@1999
|
2268 |
|
deba@1515
|
2269 |
private:
|
deba@1515
|
2270 |
|
deba@1990
|
2271 |
class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
|
deba@1515
|
2272 |
public:
|
deba@1515
|
2273 |
|
deba@1990
|
2274 |
typedef DefaultMap<_Graph, Key, int> Parent;
|
deba@2002
|
2275 |
typedef typename Parent::Graph Graph;
|
deba@1515
|
2276 |
|
deba@1515
|
2277 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
2278 |
|
deba@1829
|
2279 |
virtual void add(const Key& key) {
|
deba@1515
|
2280 |
Parent::add(key);
|
deba@1515
|
2281 |
Parent::set(key, 0);
|
deba@1515
|
2282 |
}
|
deba@1931
|
2283 |
|
deba@1829
|
2284 |
virtual void add(const std::vector<Key>& keys) {
|
deba@1829
|
2285 |
Parent::add(keys);
|
deba@2386
|
2286 |
for (int i = 0; i < int(keys.size()); ++i) {
|
deba@1829
|
2287 |
Parent::set(keys[i], 0);
|
deba@1829
|
2288 |
}
|
deba@1829
|
2289 |
}
|
deba@1515
|
2290 |
};
|
deba@1515
|
2291 |
|
deba@1515
|
2292 |
public:
|
alpar@1453
|
2293 |
|
alpar@1453
|
2294 |
/// \brief Constructor.
|
alpar@1453
|
2295 |
///
|
alpar@1453
|
2296 |
/// Constructor for creating in-degree map.
|
deba@2286
|
2297 |
explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
deba@2384
|
2298 |
Parent::attach(graph.notifier(typename _Graph::Edge()));
|
deba@1515
|
2299 |
|
deba@1515
|
2300 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2301 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
2302 |
}
|
alpar@1453
|
2303 |
}
|
alpar@1453
|
2304 |
|
alpar@1459
|
2305 |
/// Gives back the in-degree of a Node.
|
deba@1515
|
2306 |
int operator[](const Key& key) const {
|
deba@1515
|
2307 |
return deg[key];
|
alpar@1459
|
2308 |
}
|
alpar@1453
|
2309 |
|
alpar@1453
|
2310 |
protected:
|
deba@1515
|
2311 |
|
deba@1515
|
2312 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
2313 |
|
deba@1515
|
2314 |
virtual void add(const Edge& edge) {
|
deba@1515
|
2315 |
++deg[graph.target(edge)];
|
alpar@1453
|
2316 |
}
|
alpar@1453
|
2317 |
|
deba@1931
|
2318 |
virtual void add(const std::vector<Edge>& edges) {
|
deba@2386
|
2319 |
for (int i = 0; i < int(edges.size()); ++i) {
|
deba@1931
|
2320 |
++deg[graph.target(edges[i])];
|
deba@1931
|
2321 |
}
|
deba@1931
|
2322 |
}
|
deba@1931
|
2323 |
|
deba@1515
|
2324 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
2325 |
--deg[graph.target(edge)];
|
deba@1515
|
2326 |
}
|
deba@1515
|
2327 |
|
deba@1931
|
2328 |
virtual void erase(const std::vector<Edge>& edges) {
|
deba@2386
|
2329 |
for (int i = 0; i < int(edges.size()); ++i) {
|
deba@1931
|
2330 |
--deg[graph.target(edges[i])];
|
deba@1931
|
2331 |
}
|
deba@1931
|
2332 |
}
|
deba@1931
|
2333 |
|
deba@1515
|
2334 |
virtual void build() {
|
deba@1515
|
2335 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2336 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
2337 |
}
|
deba@1515
|
2338 |
}
|
deba@1515
|
2339 |
|
deba@1515
|
2340 |
virtual void clear() {
|
deba@1515
|
2341 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2342 |
deg[it] = 0;
|
deba@1515
|
2343 |
}
|
deba@1515
|
2344 |
}
|
deba@1515
|
2345 |
private:
|
alpar@1506
|
2346 |
|
deba@1515
|
2347 |
const _Graph& graph;
|
deba@1515
|
2348 |
AutoNodeMap deg;
|
alpar@1459
|
2349 |
};
|
alpar@1459
|
2350 |
|
deba@1515
|
2351 |
/// \brief Map of the node out-degrees.
|
deba@1515
|
2352 |
///
|
athos@1540
|
2353 |
/// This map returns the out-degree of a node. Once it is constructed,
|
deba@1515
|
2354 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
athos@1540
|
2355 |
/// in constant time. On the other hand, the values are updated automatically
|
deba@1515
|
2356 |
/// whenever the graph changes.
|
deba@1515
|
2357 |
///
|
deba@1729
|
2358 |
/// \warning Besides addNode() and addEdge(), a graph structure may provide
|
deba@1730
|
2359 |
/// alternative ways to modify the graph. The correct behavior of OutDegMap
|
deba@1829
|
2360 |
/// is not guarantied if these additional features are used. For example
|
deba@1829
|
2361 |
/// the functions \ref ListGraph::changeSource() "changeSource()",
|
deba@1729
|
2362 |
/// \ref ListGraph::changeTarget() "changeTarget()" and
|
deba@1729
|
2363 |
/// \ref ListGraph::reverseEdge() "reverseEdge()"
|
deba@1729
|
2364 |
/// of \ref ListGraph will \e not update the degree values correctly.
|
deba@1729
|
2365 |
///
|
alpar@1555
|
2366 |
/// \sa InDegMap
|
alpar@1459
|
2367 |
|
alpar@1459
|
2368 |
template <typename _Graph>
|
deba@1515
|
2369 |
class OutDegMap
|
deba@1999
|
2370 |
: protected ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
2371 |
::ItemNotifier::ObserverBase {
|
deba@1515
|
2372 |
|
alpar@1459
|
2373 |
public:
|
deba@1999
|
2374 |
|
deba@1999
|
2375 |
typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
2376 |
::ItemNotifier::ObserverBase Parent;
|
deba@1515
|
2377 |
|
deba@1515
|
2378 |
typedef _Graph Graph;
|
alpar@1459
|
2379 |
typedef int Value;
|
deba@1515
|
2380 |
typedef typename Graph::Node Key;
|
deba@1515
|
2381 |
|
deba@1515
|
2382 |
private:
|
deba@1515
|
2383 |
|
deba@1990
|
2384 |
class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
|
deba@1515
|
2385 |
public:
|
deba@1515
|
2386 |
|
deba@1990
|
2387 |
typedef DefaultMap<_Graph, Key, int> Parent;
|
deba@2002
|
2388 |
typedef typename Parent::Graph Graph;
|
deba@1515
|
2389 |
|
deba@1515
|
2390 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
2391 |
|
deba@1829
|
2392 |
virtual void add(const Key& key) {
|
deba@1515
|
2393 |
Parent::add(key);
|
deba@1515
|
2394 |
Parent::set(key, 0);
|
deba@1515
|
2395 |
}
|
deba@1829
|
2396 |
virtual void add(const std::vector<Key>& keys) {
|
deba@1829
|
2397 |
Parent::add(keys);
|
deba@2386
|
2398 |
for (int i = 0; i < int(keys.size()); ++i) {
|
deba@1829
|
2399 |
Parent::set(keys[i], 0);
|
deba@1829
|
2400 |
}
|
deba@1829
|
2401 |
}
|
deba@1515
|
2402 |
};
|
deba@1515
|
2403 |
|
deba@1515
|
2404 |
public:
|
alpar@1459
|
2405 |
|
alpar@1459
|
2406 |
/// \brief Constructor.
|
alpar@1459
|
2407 |
///
|
alpar@1459
|
2408 |
/// Constructor for creating out-degree map.
|
deba@2286
|
2409 |
explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
deba@2384
|
2410 |
Parent::attach(graph.notifier(typename _Graph::Edge()));
|
deba@1515
|
2411 |
|
deba@1515
|
2412 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2413 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
2414 |
}
|
alpar@1459
|
2415 |
}
|
alpar@1459
|
2416 |
|
deba@1990
|
2417 |
/// Gives back the out-degree of a Node.
|
deba@1515
|
2418 |
int operator[](const Key& key) const {
|
deba@1515
|
2419 |
return deg[key];
|
alpar@1459
|
2420 |
}
|
alpar@1459
|
2421 |
|
alpar@1459
|
2422 |
protected:
|
deba@1515
|
2423 |
|
deba@1515
|
2424 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
2425 |
|
deba@1515
|
2426 |
virtual void add(const Edge& edge) {
|
deba@1515
|
2427 |
++deg[graph.source(edge)];
|
alpar@1459
|
2428 |
}
|
alpar@1459
|
2429 |
|
deba@1931
|
2430 |
virtual void add(const std::vector<Edge>& edges) {
|
deba@2386
|
2431 |
for (int i = 0; i < int(edges.size()); ++i) {
|
deba@1931
|
2432 |
++deg[graph.source(edges[i])];
|
deba@1931
|
2433 |
}
|
deba@1931
|
2434 |
}
|
deba@1931
|
2435 |
|
deba@1515
|
2436 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
2437 |
--deg[graph.source(edge)];
|
deba@1515
|
2438 |
}
|
deba@1515
|
2439 |
|
deba@1931
|
2440 |
virtual void erase(const std::vector<Edge>& edges) {
|
deba@2386
|
2441 |
for (int i = 0; i < int(edges.size()); ++i) {
|
deba@1931
|
2442 |
--deg[graph.source(edges[i])];
|
deba@1931
|
2443 |
}
|
deba@1931
|
2444 |
}
|
deba@1931
|
2445 |
|
deba@1515
|
2446 |
virtual void build() {
|
deba@1515
|
2447 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2448 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
2449 |
}
|
deba@1515
|
2450 |
}
|
deba@1515
|
2451 |
|
deba@1515
|
2452 |
virtual void clear() {
|
deba@1515
|
2453 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
2454 |
deg[it] = 0;
|
deba@1515
|
2455 |
}
|
deba@1515
|
2456 |
}
|
deba@1515
|
2457 |
private:
|
alpar@1506
|
2458 |
|
deba@1515
|
2459 |
const _Graph& graph;
|
deba@1515
|
2460 |
AutoNodeMap deg;
|
alpar@1453
|
2461 |
};
|
alpar@1453
|
2462 |
|
deba@1695
|
2463 |
|
alpar@2235
|
2464 |
///Fast edge look up between given endpoints.
|
alpar@2235
|
2465 |
|
alpar@2235
|
2466 |
///\ingroup gutils
|
alpar@2235
|
2467 |
///Using this class, you can find an edge in a graph from a given
|
alpar@2235
|
2468 |
///source to a given target in time <em>O(log d)</em>,
|
alpar@2235
|
2469 |
///where <em>d</em> is the out-degree of the source node.
|
alpar@2235
|
2470 |
///
|
alpar@2235
|
2471 |
///It is not possible to find \e all parallel edges between two nodes.
|
alpar@2235
|
2472 |
///Use \ref AllEdgeLookUp for this purpose.
|
alpar@2235
|
2473 |
///
|
alpar@2235
|
2474 |
///\warning This class is static, so you should refresh() (or at least
|
alpar@2235
|
2475 |
///refresh(Node)) this data structure
|
alpar@2235
|
2476 |
///whenever the graph changes. This is a time consuming (superlinearly
|
alpar@2235
|
2477 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
|
alpar@2235
|
2478 |
///
|
alpar@2235
|
2479 |
///\param G The type of the underlying graph.
|
alpar@2235
|
2480 |
///
|
alpar@2235
|
2481 |
///\sa AllEdgeLookUp
|
alpar@2235
|
2482 |
template<class G>
|
alpar@2235
|
2483 |
class EdgeLookUp
|
alpar@2235
|
2484 |
{
|
alpar@2235
|
2485 |
public:
|
alpar@2235
|
2486 |
GRAPH_TYPEDEFS(typename G)
|
alpar@2235
|
2487 |
typedef G Graph;
|
alpar@2235
|
2488 |
|
alpar@2235
|
2489 |
protected:
|
alpar@2235
|
2490 |
const Graph &_g;
|
alpar@2235
|
2491 |
typename Graph::template NodeMap<Edge> _head;
|
alpar@2235
|
2492 |
typename Graph::template EdgeMap<Edge> _left;
|
alpar@2235
|
2493 |
typename Graph::template EdgeMap<Edge> _right;
|
alpar@2235
|
2494 |
|
alpar@2235
|
2495 |
class EdgeLess {
|
alpar@2235
|
2496 |
const Graph &g;
|
alpar@2235
|
2497 |
public:
|
alpar@2235
|
2498 |
EdgeLess(const Graph &_g) : g(_g) {}
|
alpar@2235
|
2499 |
bool operator()(Edge a,Edge b) const
|
alpar@2235
|
2500 |
{
|
alpar@2235
|
2501 |
return g.target(a)<g.target(b);
|
alpar@2235
|
2502 |
}
|
alpar@2235
|
2503 |
};
|
alpar@2235
|
2504 |
|
alpar@2235
|
2505 |
public:
|
alpar@2235
|
2506 |
|
alpar@2235
|
2507 |
///Constructor
|
alpar@2235
|
2508 |
|
alpar@2235
|
2509 |
///Constructor.
|
alpar@2235
|
2510 |
///
|
alpar@2235
|
2511 |
///It builds up the search database, which remains valid until the graph
|
alpar@2235
|
2512 |
///changes.
|
alpar@2235
|
2513 |
EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
alpar@2235
|
2514 |
|
alpar@2235
|
2515 |
private:
|
alpar@2235
|
2516 |
Edge refresh_rec(std::vector<Edge> &v,int a,int b)
|
alpar@2235
|
2517 |
{
|
alpar@2235
|
2518 |
int m=(a+b)/2;
|
alpar@2235
|
2519 |
Edge me=v[m];
|
alpar@2235
|
2520 |
_left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
|
alpar@2235
|
2521 |
_right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
|
alpar@2235
|
2522 |
return me;
|
alpar@2235
|
2523 |
}
|
alpar@2235
|
2524 |
public:
|
alpar@2235
|
2525 |
///Refresh the data structure at a node.
|
alpar@2235
|
2526 |
|
alpar@2235
|
2527 |
///Build up the search database of node \c n.
|
alpar@2235
|
2528 |
///
|
alpar@2235
|
2529 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
|
alpar@2235
|
2530 |
///the number of the outgoing edges of \c n.
|
alpar@2235
|
2531 |
void refresh(Node n)
|
alpar@2235
|
2532 |
{
|
alpar@2235
|
2533 |
std::vector<Edge> v;
|
alpar@2235
|
2534 |
for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
|
alpar@2235
|
2535 |
if(v.size()) {
|
alpar@2235
|
2536 |
std::sort(v.begin(),v.end(),EdgeLess(_g));
|
alpar@2235
|
2537 |
_head[n]=refresh_rec(v,0,v.size()-1);
|
alpar@2235
|
2538 |
}
|
alpar@2235
|
2539 |
else _head[n]=INVALID;
|
alpar@2235
|
2540 |
}
|
alpar@2235
|
2541 |
///Refresh the full data structure.
|
alpar@2235
|
2542 |
|
alpar@2235
|
2543 |
///Build up the full search database. In fact, it simply calls
|
alpar@2235
|
2544 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
alpar@2235
|
2545 |
///
|
alpar@2235
|
2546 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
|
alpar@2235
|
2547 |
///the number of the edges of \c n and <em>D</em> is the maximum
|
alpar@2235
|
2548 |
///out-degree of the graph.
|
alpar@2235
|
2549 |
|
alpar@2235
|
2550 |
void refresh()
|
alpar@2235
|
2551 |
{
|
alpar@2235
|
2552 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
|
alpar@2235
|
2553 |
}
|
alpar@2235
|
2554 |
|
alpar@2235
|
2555 |
///Find an edge between two nodes.
|
alpar@2235
|
2556 |
|
alpar@2235
|
2557 |
///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
|
alpar@2235
|
2558 |
/// <em>d</em> is the number of outgoing edges of \c s.
|
alpar@2235
|
2559 |
///\param s The source node
|
alpar@2235
|
2560 |
///\param t The target node
|
alpar@2235
|
2561 |
///\return An edge from \c s to \c t if there exists,
|
alpar@2235
|
2562 |
///\ref INVALID otherwise.
|
alpar@2235
|
2563 |
///
|
alpar@2235
|
2564 |
///\warning If you change the graph, refresh() must be called before using
|
alpar@2235
|
2565 |
///this operator. If you change the outgoing edges of
|
alpar@2235
|
2566 |
///a single node \c n, then
|
alpar@2235
|
2567 |
///\ref refresh(Node) "refresh(n)" is enough.
|
alpar@2235
|
2568 |
///
|
alpar@2235
|
2569 |
Edge operator()(Node s, Node t) const
|
alpar@2235
|
2570 |
{
|
alpar@2235
|
2571 |
Edge e;
|
alpar@2235
|
2572 |
for(e=_head[s];
|
alpar@2235
|
2573 |
e!=INVALID&&_g.target(e)!=t;
|
alpar@2235
|
2574 |
e = t < _g.target(e)?_left[e]:_right[e]) ;
|
alpar@2235
|
2575 |
return e;
|
alpar@2235
|
2576 |
}
|
alpar@2235
|
2577 |
|
alpar@2235
|
2578 |
};
|
alpar@2235
|
2579 |
|
alpar@2235
|
2580 |
///Fast look up of all edges between given endpoints.
|
alpar@2235
|
2581 |
|
alpar@2235
|
2582 |
///\ingroup gutils
|
alpar@2235
|
2583 |
///This class is the same as \ref EdgeLookUp, with the addition
|
alpar@2235
|
2584 |
///that it makes it possible to find all edges between given endpoints.
|
alpar@2235
|
2585 |
///
|
alpar@2235
|
2586 |
///\warning This class is static, so you should refresh() (or at least
|
alpar@2235
|
2587 |
///refresh(Node)) this data structure
|
alpar@2235
|
2588 |
///whenever the graph changes. This is a time consuming (superlinearly
|
alpar@2235
|
2589 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
|
alpar@2235
|
2590 |
///
|
alpar@2235
|
2591 |
///\param G The type of the underlying graph.
|
alpar@2235
|
2592 |
///
|
alpar@2235
|
2593 |
///\sa EdgeLookUp
|
alpar@2235
|
2594 |
template<class G>
|
alpar@2235
|
2595 |
class AllEdgeLookUp : public EdgeLookUp<G>
|
alpar@2235
|
2596 |
{
|
alpar@2235
|
2597 |
using EdgeLookUp<G>::_g;
|
alpar@2235
|
2598 |
using EdgeLookUp<G>::_right;
|
alpar@2235
|
2599 |
using EdgeLookUp<G>::_left;
|
alpar@2235
|
2600 |
using EdgeLookUp<G>::_head;
|
alpar@2235
|
2601 |
|
alpar@2235
|
2602 |
GRAPH_TYPEDEFS(typename G)
|
alpar@2235
|
2603 |
typedef G Graph;
|
alpar@2235
|
2604 |
|
alpar@2235
|
2605 |
typename Graph::template EdgeMap<Edge> _next;
|
alpar@2235
|
2606 |
|
alpar@2235
|
2607 |
Edge refreshNext(Edge head,Edge next=INVALID)
|
alpar@2235
|
2608 |
{
|
alpar@2235
|
2609 |
if(head==INVALID) return next;
|
alpar@2235
|
2610 |
else {
|
alpar@2235
|
2611 |
next=refreshNext(_right[head],next);
|
alpar@2235
|
2612 |
// _next[head]=next;
|
alpar@2235
|
2613 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
|
alpar@2235
|
2614 |
? next : INVALID;
|
alpar@2235
|
2615 |
return refreshNext(_left[head],head);
|
alpar@2235
|
2616 |
}
|
alpar@2235
|
2617 |
}
|
alpar@2235
|
2618 |
|
alpar@2235
|
2619 |
void refreshNext()
|
alpar@2235
|
2620 |
{
|
alpar@2235
|
2621 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
|
alpar@2235
|
2622 |
}
|
alpar@2235
|
2623 |
|
alpar@2235
|
2624 |
public:
|
alpar@2235
|
2625 |
///Constructor
|
alpar@2235
|
2626 |
|
alpar@2235
|
2627 |
///Constructor.
|
alpar@2235
|
2628 |
///
|
alpar@2235
|
2629 |
///It builds up the search database, which remains valid until the graph
|
alpar@2235
|
2630 |
///changes.
|
alpar@2235
|
2631 |
AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
|
alpar@2235
|
2632 |
|
alpar@2235
|
2633 |
///Refresh the data structure at a node.
|
alpar@2235
|
2634 |
|
alpar@2235
|
2635 |
///Build up the search database of node \c n.
|
alpar@2235
|
2636 |
///
|
alpar@2235
|
2637 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
|
alpar@2235
|
2638 |
///the number of the outgoing edges of \c n.
|
alpar@2235
|
2639 |
|
alpar@2235
|
2640 |
void refresh(Node n)
|
alpar@2235
|
2641 |
{
|
alpar@2235
|
2642 |
EdgeLookUp<G>::refresh(n);
|
alpar@2235
|
2643 |
refreshNext(_head[n]);
|
alpar@2235
|
2644 |
}
|
alpar@2235
|
2645 |
|
alpar@2235
|
2646 |
///Refresh the full data structure.
|
alpar@2235
|
2647 |
|
alpar@2235
|
2648 |
///Build up the full search database. In fact, it simply calls
|
alpar@2235
|
2649 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
alpar@2235
|
2650 |
///
|
alpar@2235
|
2651 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
|
alpar@2235
|
2652 |
///the number of the edges of \c n and <em>D</em> is the maximum
|
alpar@2235
|
2653 |
///out-degree of the graph.
|
alpar@2235
|
2654 |
|
alpar@2235
|
2655 |
void refresh()
|
alpar@2235
|
2656 |
{
|
alpar@2235
|
2657 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
|
alpar@2235
|
2658 |
}
|
alpar@2235
|
2659 |
|
alpar@2235
|
2660 |
///Find an edge between two nodes.
|
alpar@2235
|
2661 |
|
alpar@2235
|
2662 |
///Find an edge between two nodes.
|
alpar@2235
|
2663 |
///\param s The source node
|
alpar@2235
|
2664 |
///\param t The target node
|
alpar@2235
|
2665 |
///\param prev The previous edge between \c s and \c t. It it is INVALID or
|
alpar@2235
|
2666 |
///not given, the operator finds the first appropriate edge.
|
alpar@2350
|
2667 |
///\return An edge from \c s to \c t after \c prev or
|
alpar@2235
|
2668 |
///\ref INVALID if there is no more.
|
alpar@2235
|
2669 |
///
|
alpar@2235
|
2670 |
///For example, you can count the number of edges from \c u to \c v in the
|
alpar@2235
|
2671 |
///following way.
|
alpar@2235
|
2672 |
///\code
|
alpar@2235
|
2673 |
///AllEdgeLookUp<ListGraph> ae(g);
|
alpar@2235
|
2674 |
///...
|
alpar@2235
|
2675 |
///int n=0;
|
alpar@2235
|
2676 |
///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
|
alpar@2235
|
2677 |
///\endcode
|
alpar@2235
|
2678 |
///
|
alpar@2235
|
2679 |
///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
|
alpar@2235
|
2680 |
/// <em>d</em> is the number of outgoing edges of \c s. Then, the
|
alpar@2235
|
2681 |
///consecutive edges are found in constant time.
|
alpar@2235
|
2682 |
///
|
alpar@2235
|
2683 |
///\warning If you change the graph, refresh() must be called before using
|
alpar@2235
|
2684 |
///this operator. If you change the outgoing edges of
|
alpar@2235
|
2685 |
///a single node \c n, then
|
alpar@2235
|
2686 |
///\ref refresh(Node) "refresh(n)" is enough.
|
alpar@2235
|
2687 |
///
|
alpar@2235
|
2688 |
#ifdef DOXYGEN
|
alpar@2235
|
2689 |
Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
|
alpar@2235
|
2690 |
#else
|
alpar@2235
|
2691 |
using EdgeLookUp<G>::operator() ;
|
alpar@2235
|
2692 |
Edge operator()(Node s, Node t, Edge prev) const
|
alpar@2235
|
2693 |
{
|
alpar@2235
|
2694 |
return prev==INVALID?(*this)(s,t):_next[prev];
|
alpar@2235
|
2695 |
}
|
alpar@2235
|
2696 |
#endif
|
alpar@2235
|
2697 |
|
alpar@2235
|
2698 |
};
|
alpar@2235
|
2699 |
|
alpar@1402
|
2700 |
/// @}
|
alpar@1402
|
2701 |
|
alpar@947
|
2702 |
} //END OF NAMESPACE LEMON
|
klao@946
|
2703 |
|
klao@946
|
2704 |
#endif
|