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@1956
|
5 |
* Copyright (C) 2003-2006
|
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>
|
klao@946
|
26 |
|
deba@1993
|
27 |
#include <lemon/bits/invalid.h>
|
deba@1993
|
28 |
#include <lemon/bits/utility.h>
|
deba@1413
|
29 |
#include <lemon/maps.h>
|
deba@1993
|
30 |
#include <lemon/bits/traits.h>
|
deba@1990
|
31 |
|
alpar@1459
|
32 |
#include <lemon/bits/alteration_notifier.h>
|
deba@1990
|
33 |
#include <lemon/bits/default_map.h>
|
klao@946
|
34 |
|
alpar@947
|
35 |
///\ingroup gutils
|
klao@946
|
36 |
///\file
|
alpar@947
|
37 |
///\brief Graph utilities.
|
klao@946
|
38 |
///
|
alpar@964
|
39 |
///
|
klao@946
|
40 |
|
klao@946
|
41 |
|
klao@946
|
42 |
namespace lemon {
|
klao@946
|
43 |
|
deba@1267
|
44 |
/// \addtogroup gutils
|
deba@1267
|
45 |
/// @{
|
alpar@947
|
46 |
|
alpar@1756
|
47 |
///Creates convenience typedefs for the graph types and iterators
|
alpar@1756
|
48 |
|
alpar@1756
|
49 |
///This \c \#define creates convenience typedefs for the following types
|
alpar@1756
|
50 |
///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
|
deba@2031
|
51 |
///\c OutEdgeIt
|
alpar@1756
|
52 |
///\note If \c G it a template parameter, it should be used in this way.
|
alpar@1756
|
53 |
///\code
|
alpar@1756
|
54 |
/// GRAPH_TYPEDEFS(typename G)
|
alpar@1756
|
55 |
///\endcode
|
alpar@1756
|
56 |
///
|
alpar@1756
|
57 |
///\warning There are no typedefs for the graph maps because of the lack of
|
alpar@1756
|
58 |
///template typedefs in C++.
|
alpar@1804
|
59 |
#define GRAPH_TYPEDEFS(Graph) \
|
alpar@1804
|
60 |
typedef Graph:: Node Node; \
|
alpar@1804
|
61 |
typedef Graph:: NodeIt NodeIt; \
|
alpar@1804
|
62 |
typedef Graph:: Edge Edge; \
|
alpar@1804
|
63 |
typedef Graph:: EdgeIt EdgeIt; \
|
alpar@1804
|
64 |
typedef Graph:: InEdgeIt InEdgeIt; \
|
alpar@1811
|
65 |
typedef Graph::OutEdgeIt OutEdgeIt;
|
deba@2031
|
66 |
|
alpar@1756
|
67 |
///Creates convenience typedefs for the undirected graph types and iterators
|
alpar@1756
|
68 |
|
alpar@1756
|
69 |
///This \c \#define creates the same convenience typedefs as defined by
|
alpar@1756
|
70 |
///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
|
klao@1909
|
71 |
///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
|
alpar@1756
|
72 |
///
|
alpar@1756
|
73 |
///\note If \c G it a template parameter, it should be used in this way.
|
alpar@1756
|
74 |
///\code
|
deba@1992
|
75 |
/// UGRAPH_TYPEDEFS(typename G)
|
alpar@1756
|
76 |
///\endcode
|
alpar@1756
|
77 |
///
|
alpar@1756
|
78 |
///\warning There are no typedefs for the graph maps because of the lack of
|
alpar@1756
|
79 |
///template typedefs in C++.
|
deba@1992
|
80 |
#define UGRAPH_TYPEDEFS(Graph) \
|
alpar@1804
|
81 |
GRAPH_TYPEDEFS(Graph) \
|
klao@1909
|
82 |
typedef Graph:: UEdge UEdge; \
|
klao@1909
|
83 |
typedef Graph:: UEdgeIt UEdgeIt; \
|
alpar@1811
|
84 |
typedef Graph:: IncEdgeIt IncEdgeIt;
|
klao@1909
|
85 |
// typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
|
klao@1909
|
86 |
// typedef Graph::template UEdgeMap<int> IntUEdgeMap;
|
klao@1909
|
87 |
// typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
|
alpar@1756
|
88 |
|
deba@2031
|
89 |
///\brief Creates convenience typedefs for the bipartite undirected graph
|
deba@2031
|
90 |
///types and iterators
|
deba@2031
|
91 |
|
deba@2031
|
92 |
///This \c \#define creates the same convenience typedefs as defined by
|
deba@2031
|
93 |
///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
|
deba@2031
|
94 |
///\c ANodeIt, \c BNodeIt,
|
deba@2031
|
95 |
///
|
deba@2031
|
96 |
///\note If \c G it a template parameter, it should be used in this way.
|
deba@2031
|
97 |
///\code
|
deba@2031
|
98 |
/// BPUGRAPH_TYPEDEFS(typename G)
|
deba@2031
|
99 |
///\endcode
|
deba@2031
|
100 |
///
|
deba@2031
|
101 |
///\warning There are no typedefs for the graph maps because of the lack of
|
deba@2031
|
102 |
///template typedefs in C++.
|
deba@2031
|
103 |
#define BPUGRAPH_TYPEDEFS(Graph) \
|
deba@2031
|
104 |
UGRAPH_TYPEDEFS(Graph) \
|
deba@2031
|
105 |
typedef Graph::ANodeIt ANodeIt; \
|
deba@2031
|
106 |
typedef Graph::BNodeIt BNodeIt;
|
alpar@1756
|
107 |
|
klao@946
|
108 |
/// \brief Function to count the items in the graph.
|
klao@946
|
109 |
///
|
athos@1540
|
110 |
/// This function counts the items (nodes, edges etc) in the graph.
|
klao@946
|
111 |
/// The complexity of the function is O(n) because
|
klao@946
|
112 |
/// it iterates on all of the items.
|
klao@946
|
113 |
|
deba@2020
|
114 |
template <typename Graph, typename Item>
|
klao@977
|
115 |
inline int countItems(const Graph& g) {
|
deba@2020
|
116 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
klao@946
|
117 |
int num = 0;
|
klao@977
|
118 |
for (ItemIt it(g); it != INVALID; ++it) {
|
klao@946
|
119 |
++num;
|
klao@946
|
120 |
}
|
klao@946
|
121 |
return num;
|
klao@946
|
122 |
}
|
klao@946
|
123 |
|
klao@977
|
124 |
// Node counting:
|
klao@977
|
125 |
|
deba@2020
|
126 |
namespace _graph_utils_bits {
|
deba@2020
|
127 |
|
deba@2020
|
128 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
129 |
struct CountNodesSelector {
|
deba@2020
|
130 |
static int count(const Graph &g) {
|
deba@2020
|
131 |
return countItems<Graph, typename Graph::Node>(g);
|
deba@2020
|
132 |
}
|
deba@2020
|
133 |
};
|
klao@977
|
134 |
|
deba@2020
|
135 |
template <typename Graph>
|
deba@2020
|
136 |
struct CountNodesSelector<
|
deba@2020
|
137 |
Graph, typename
|
deba@2020
|
138 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2020
|
139 |
{
|
deba@2020
|
140 |
static int count(const Graph &g) {
|
deba@2020
|
141 |
return g.nodeNum();
|
deba@2020
|
142 |
}
|
deba@2020
|
143 |
};
|
klao@977
|
144 |
}
|
klao@977
|
145 |
|
klao@946
|
146 |
/// \brief Function to count the nodes in the graph.
|
klao@946
|
147 |
///
|
klao@946
|
148 |
/// This function counts the nodes in the graph.
|
klao@946
|
149 |
/// The complexity of the function is O(n) but for some
|
athos@1526
|
150 |
/// graph structures it is specialized to run in O(1).
|
klao@977
|
151 |
///
|
klao@977
|
152 |
/// \todo refer how to specialize it
|
klao@946
|
153 |
|
klao@946
|
154 |
template <typename Graph>
|
klao@977
|
155 |
inline int countNodes(const Graph& g) {
|
deba@2020
|
156 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
|
klao@977
|
157 |
}
|
klao@977
|
158 |
|
deba@2029
|
159 |
namespace _graph_utils_bits {
|
deba@2029
|
160 |
|
deba@2029
|
161 |
template <typename Graph, typename Enable = void>
|
deba@2029
|
162 |
struct CountANodesSelector {
|
deba@2029
|
163 |
static int count(const Graph &g) {
|
deba@2029
|
164 |
return countItems<Graph, typename Graph::ANode>(g);
|
deba@2029
|
165 |
}
|
deba@2029
|
166 |
};
|
deba@2029
|
167 |
|
deba@2029
|
168 |
template <typename Graph>
|
deba@2029
|
169 |
struct CountANodesSelector<
|
deba@2029
|
170 |
Graph, typename
|
deba@2029
|
171 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2029
|
172 |
{
|
deba@2029
|
173 |
static int count(const Graph &g) {
|
deba@2186
|
174 |
return g.aNodeNum();
|
deba@2029
|
175 |
}
|
deba@2029
|
176 |
};
|
deba@2029
|
177 |
}
|
deba@2029
|
178 |
|
deba@2029
|
179 |
/// \brief Function to count the anodes in the graph.
|
deba@2029
|
180 |
///
|
deba@2029
|
181 |
/// This function counts the anodes in the graph.
|
deba@2029
|
182 |
/// The complexity of the function is O(an) but for some
|
deba@2029
|
183 |
/// graph structures it is specialized to run in O(1).
|
deba@2029
|
184 |
///
|
deba@2029
|
185 |
/// \todo refer how to specialize it
|
deba@2029
|
186 |
|
deba@2029
|
187 |
template <typename Graph>
|
deba@2029
|
188 |
inline int countANodes(const Graph& g) {
|
deba@2029
|
189 |
return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
|
deba@2029
|
190 |
}
|
deba@2029
|
191 |
|
deba@2029
|
192 |
namespace _graph_utils_bits {
|
deba@2029
|
193 |
|
deba@2029
|
194 |
template <typename Graph, typename Enable = void>
|
deba@2029
|
195 |
struct CountBNodesSelector {
|
deba@2029
|
196 |
static int count(const Graph &g) {
|
deba@2029
|
197 |
return countItems<Graph, typename Graph::BNode>(g);
|
deba@2029
|
198 |
}
|
deba@2029
|
199 |
};
|
deba@2029
|
200 |
|
deba@2029
|
201 |
template <typename Graph>
|
deba@2029
|
202 |
struct CountBNodesSelector<
|
deba@2029
|
203 |
Graph, typename
|
deba@2029
|
204 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
deba@2029
|
205 |
{
|
deba@2029
|
206 |
static int count(const Graph &g) {
|
deba@2186
|
207 |
return g.bNodeNum();
|
deba@2029
|
208 |
}
|
deba@2029
|
209 |
};
|
deba@2029
|
210 |
}
|
deba@2029
|
211 |
|
deba@2029
|
212 |
/// \brief Function to count the bnodes in the graph.
|
deba@2029
|
213 |
///
|
deba@2029
|
214 |
/// This function counts the bnodes in the graph.
|
deba@2029
|
215 |
/// The complexity of the function is O(bn) but for some
|
deba@2029
|
216 |
/// graph structures it is specialized to run in O(1).
|
deba@2029
|
217 |
///
|
deba@2029
|
218 |
/// \todo refer how to specialize it
|
deba@2029
|
219 |
|
deba@2029
|
220 |
template <typename Graph>
|
deba@2029
|
221 |
inline int countBNodes(const Graph& g) {
|
deba@2029
|
222 |
return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
|
deba@2029
|
223 |
}
|
deba@2029
|
224 |
|
deba@2020
|
225 |
|
klao@977
|
226 |
// Edge counting:
|
klao@977
|
227 |
|
deba@2020
|
228 |
namespace _graph_utils_bits {
|
deba@2020
|
229 |
|
deba@2020
|
230 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
231 |
struct CountEdgesSelector {
|
deba@2020
|
232 |
static int count(const Graph &g) {
|
deba@2020
|
233 |
return countItems<Graph, typename Graph::Edge>(g);
|
deba@2020
|
234 |
}
|
deba@2020
|
235 |
};
|
klao@977
|
236 |
|
deba@2020
|
237 |
template <typename Graph>
|
deba@2020
|
238 |
struct CountEdgesSelector<
|
deba@2020
|
239 |
Graph,
|
deba@2020
|
240 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
deba@2020
|
241 |
{
|
deba@2020
|
242 |
static int count(const Graph &g) {
|
deba@2020
|
243 |
return g.edgeNum();
|
deba@2020
|
244 |
}
|
deba@2020
|
245 |
};
|
klao@946
|
246 |
}
|
klao@946
|
247 |
|
klao@946
|
248 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
249 |
///
|
klao@946
|
250 |
/// This function counts the edges in the graph.
|
klao@946
|
251 |
/// The complexity of the function is O(e) but for some
|
athos@1526
|
252 |
/// graph structures it is specialized to run in O(1).
|
klao@977
|
253 |
|
klao@946
|
254 |
template <typename Graph>
|
klao@977
|
255 |
inline int countEdges(const Graph& g) {
|
deba@2020
|
256 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
|
klao@946
|
257 |
}
|
klao@946
|
258 |
|
klao@1053
|
259 |
// Undirected edge counting:
|
deba@2020
|
260 |
namespace _graph_utils_bits {
|
deba@2020
|
261 |
|
deba@2020
|
262 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
263 |
struct CountUEdgesSelector {
|
deba@2020
|
264 |
static int count(const Graph &g) {
|
deba@2020
|
265 |
return countItems<Graph, typename Graph::UEdge>(g);
|
deba@2020
|
266 |
}
|
deba@2020
|
267 |
};
|
klao@1053
|
268 |
|
deba@2020
|
269 |
template <typename Graph>
|
deba@2020
|
270 |
struct CountUEdgesSelector<
|
deba@2020
|
271 |
Graph,
|
deba@2020
|
272 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
deba@2020
|
273 |
{
|
deba@2020
|
274 |
static int count(const Graph &g) {
|
deba@2020
|
275 |
return g.uEdgeNum();
|
deba@2020
|
276 |
}
|
deba@2020
|
277 |
};
|
klao@1053
|
278 |
}
|
klao@1053
|
279 |
|
athos@1526
|
280 |
/// \brief Function to count the undirected edges in the graph.
|
klao@946
|
281 |
///
|
athos@1526
|
282 |
/// This function counts the undirected edges in the graph.
|
klao@946
|
283 |
/// The complexity of the function is O(e) but for some
|
athos@1540
|
284 |
/// graph structures it is specialized to run in O(1).
|
klao@1053
|
285 |
|
klao@946
|
286 |
template <typename Graph>
|
klao@1909
|
287 |
inline int countUEdges(const Graph& g) {
|
deba@2020
|
288 |
return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
|
deba@2020
|
289 |
|
klao@946
|
290 |
}
|
klao@946
|
291 |
|
klao@977
|
292 |
|
klao@946
|
293 |
template <typename Graph, typename DegIt>
|
klao@946
|
294 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
295 |
int num = 0;
|
klao@946
|
296 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
klao@946
|
297 |
++num;
|
klao@946
|
298 |
}
|
klao@946
|
299 |
return num;
|
klao@946
|
300 |
}
|
alpar@967
|
301 |
|
deba@1531
|
302 |
/// \brief Function to count the number of the out-edges from node \c n.
|
deba@1531
|
303 |
///
|
deba@1531
|
304 |
/// This function counts the number of the out-edges from node \c n
|
deba@1531
|
305 |
/// in the graph.
|
deba@1531
|
306 |
template <typename Graph>
|
deba@1531
|
307 |
inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1531
|
308 |
return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
|
deba@1531
|
309 |
}
|
deba@1531
|
310 |
|
deba@1531
|
311 |
/// \brief Function to count the number of the in-edges to node \c n.
|
deba@1531
|
312 |
///
|
deba@1531
|
313 |
/// This function counts the number of the in-edges to node \c n
|
deba@1531
|
314 |
/// in the graph.
|
deba@1531
|
315 |
template <typename Graph>
|
deba@1531
|
316 |
inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1531
|
317 |
return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
|
deba@1531
|
318 |
}
|
deba@1531
|
319 |
|
deba@1704
|
320 |
/// \brief Function to count the number of the inc-edges to node \c n.
|
deba@1679
|
321 |
///
|
deba@1704
|
322 |
/// This function counts the number of the inc-edges to node \c n
|
deba@1679
|
323 |
/// in the graph.
|
deba@1679
|
324 |
template <typename Graph>
|
deba@1679
|
325 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
|
deba@1679
|
326 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
|
deba@1679
|
327 |
}
|
deba@1679
|
328 |
|
deba@2020
|
329 |
namespace _graph_utils_bits {
|
deba@2020
|
330 |
|
deba@2020
|
331 |
template <typename Graph, typename Enable = void>
|
deba@2020
|
332 |
struct FindEdgeSelector {
|
deba@2020
|
333 |
typedef typename Graph::Node Node;
|
deba@2020
|
334 |
typedef typename Graph::Edge Edge;
|
deba@2020
|
335 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
deba@2020
|
336 |
if (e == INVALID) {
|
deba@2020
|
337 |
g.firstOut(e, u);
|
deba@2020
|
338 |
} else {
|
deba@2020
|
339 |
g.nextOut(e);
|
deba@2020
|
340 |
}
|
deba@2020
|
341 |
while (e != INVALID && g.target(e) != v) {
|
deba@2020
|
342 |
g.nextOut(e);
|
deba@2020
|
343 |
}
|
deba@2020
|
344 |
return e;
|
deba@2020
|
345 |
}
|
deba@2020
|
346 |
};
|
deba@1531
|
347 |
|
deba@2020
|
348 |
template <typename Graph>
|
deba@2020
|
349 |
struct FindEdgeSelector<
|
deba@2020
|
350 |
Graph,
|
deba@2020
|
351 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
deba@2020
|
352 |
{
|
deba@2020
|
353 |
typedef typename Graph::Node Node;
|
deba@2020
|
354 |
typedef typename Graph::Edge Edge;
|
deba@2020
|
355 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
deba@2020
|
356 |
return g.findEdge(u, v, prev);
|
deba@2020
|
357 |
}
|
deba@2020
|
358 |
};
|
deba@1565
|
359 |
}
|
deba@1565
|
360 |
|
deba@1565
|
361 |
/// \brief Finds an edge between two nodes of a graph.
|
deba@1565
|
362 |
///
|
alpar@967
|
363 |
/// Finds an edge from node \c u to node \c v in graph \c g.
|
alpar@967
|
364 |
///
|
alpar@967
|
365 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@967
|
366 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@967
|
367 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@967
|
368 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
alpar@967
|
369 |
///
|
alpar@967
|
370 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@1946
|
371 |
///\code
|
alpar@967
|
372 |
/// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
|
alpar@967
|
373 |
/// ...
|
alpar@967
|
374 |
/// }
|
alpar@1946
|
375 |
///\endcode
|
alpar@2155
|
376 |
///
|
alpar@2155
|
377 |
///\sa ConEdgeIt
|
alpar@967
|
378 |
template <typename Graph>
|
deba@1565
|
379 |
inline typename Graph::Edge findEdge(const Graph &g,
|
deba@1565
|
380 |
typename Graph::Node u,
|
deba@1565
|
381 |
typename Graph::Node v,
|
deba@1565
|
382 |
typename Graph::Edge prev = INVALID) {
|
deba@2020
|
383 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
|
alpar@967
|
384 |
}
|
deba@1531
|
385 |
|
deba@1565
|
386 |
/// \brief Iterator for iterating on edges connected the same nodes.
|
deba@1565
|
387 |
///
|
deba@1565
|
388 |
/// Iterator for iterating on edges connected the same nodes. It is
|
deba@1565
|
389 |
/// higher level interface for the findEdge() function. You can
|
alpar@1591
|
390 |
/// use it the following way:
|
alpar@1946
|
391 |
///\code
|
deba@1565
|
392 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
deba@1565
|
393 |
/// ...
|
deba@1565
|
394 |
/// }
|
alpar@1946
|
395 |
///\endcode
|
alpar@2155
|
396 |
///
|
alpar@2155
|
397 |
///\sa findEdge()
|
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@2031
|
504 |
inline typename Graph::UEdge findUEdge(const Graph &g,
|
deba@2031
|
505 |
typename Graph::Node u,
|
deba@2031
|
506 |
typename Graph::Node v,
|
deba@2031
|
507 |
typename Graph::UEdge p = INVALID) {
|
deba@2031
|
508 |
return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
|
deba@1704
|
509 |
}
|
deba@1704
|
510 |
|
klao@1909
|
511 |
/// \brief Iterator for iterating on uedges connected the same nodes.
|
deba@1704
|
512 |
///
|
klao@1909
|
513 |
/// Iterator for iterating on uedges connected the same nodes. It is
|
klao@1909
|
514 |
/// higher level interface for the findUEdge() function. You can
|
deba@1704
|
515 |
/// use it the following way:
|
alpar@1946
|
516 |
///\code
|
klao@1909
|
517 |
/// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
deba@1704
|
518 |
/// ...
|
deba@1704
|
519 |
/// }
|
alpar@1946
|
520 |
///\endcode
|
deba@1704
|
521 |
///
|
alpar@2155
|
522 |
///\sa findUEdge()
|
alpar@2155
|
523 |
///
|
deba@1704
|
524 |
/// \author Balazs Dezso
|
deba@1704
|
525 |
template <typename _Graph>
|
klao@1909
|
526 |
class ConUEdgeIt : public _Graph::UEdge {
|
deba@1704
|
527 |
public:
|
deba@1704
|
528 |
|
deba@1704
|
529 |
typedef _Graph Graph;
|
klao@1909
|
530 |
typedef typename Graph::UEdge Parent;
|
deba@1704
|
531 |
|
klao@1909
|
532 |
typedef typename Graph::UEdge UEdge;
|
deba@1704
|
533 |
typedef typename Graph::Node Node;
|
deba@1704
|
534 |
|
deba@1704
|
535 |
/// \brief Constructor.
|
deba@1704
|
536 |
///
|
klao@1909
|
537 |
/// Construct a new ConUEdgeIt iterating on the edges which
|
deba@1704
|
538 |
/// connects the \c u and \c v node.
|
klao@1909
|
539 |
ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
|
klao@1909
|
540 |
Parent::operator=(findUEdge(graph, u, v));
|
deba@1704
|
541 |
}
|
deba@1704
|
542 |
|
deba@1704
|
543 |
/// \brief Constructor.
|
deba@1704
|
544 |
///
|
klao@1909
|
545 |
/// Construct a new ConUEdgeIt which continues the iterating from
|
deba@1704
|
546 |
/// the \c e edge.
|
klao@1909
|
547 |
ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
|
deba@1704
|
548 |
|
deba@1704
|
549 |
/// \brief Increment operator.
|
deba@1704
|
550 |
///
|
deba@1704
|
551 |
/// It increments the iterator and gives back the next edge.
|
klao@1909
|
552 |
ConUEdgeIt& operator++() {
|
klao@1909
|
553 |
Parent::operator=(findUEdge(graph, graph.source(*this),
|
deba@1829
|
554 |
graph.target(*this), *this));
|
deba@1704
|
555 |
return *this;
|
deba@1704
|
556 |
}
|
deba@1704
|
557 |
private:
|
deba@1704
|
558 |
const Graph& graph;
|
deba@1704
|
559 |
};
|
deba@1704
|
560 |
|
athos@1540
|
561 |
/// \brief Copy a map.
|
alpar@964
|
562 |
///
|
alpar@1547
|
563 |
/// This function copies the \c source map to the \c target map. It uses the
|
athos@1540
|
564 |
/// given iterator to iterate on the data structure and it uses the \c ref
|
athos@1540
|
565 |
/// mapping to convert the source's keys to the target's keys.
|
deba@1531
|
566 |
template <typename Target, typename Source,
|
deba@1531
|
567 |
typename ItemIt, typename Ref>
|
deba@1531
|
568 |
void copyMap(Target& target, const Source& source,
|
deba@1531
|
569 |
ItemIt it, const Ref& ref) {
|
deba@1531
|
570 |
for (; it != INVALID; ++it) {
|
deba@1531
|
571 |
target[ref[it]] = source[it];
|
klao@946
|
572 |
}
|
klao@946
|
573 |
}
|
klao@946
|
574 |
|
deba@1531
|
575 |
/// \brief Copy the source map to the target map.
|
deba@1531
|
576 |
///
|
deba@1531
|
577 |
/// Copy the \c source map to the \c target map. It uses the given iterator
|
deba@1531
|
578 |
/// to iterate on the data structure.
|
deba@1830
|
579 |
template <typename Target, typename Source, typename ItemIt>
|
deba@1531
|
580 |
void copyMap(Target& target, const Source& source, ItemIt it) {
|
deba@1531
|
581 |
for (; it != INVALID; ++it) {
|
deba@1531
|
582 |
target[it] = source[it];
|
klao@946
|
583 |
}
|
klao@946
|
584 |
}
|
klao@946
|
585 |
|
athos@1540
|
586 |
/// \brief Class to copy a graph.
|
deba@1531
|
587 |
///
|
alpar@2006
|
588 |
/// Class to copy a graph to another graph (duplicate a graph). The
|
athos@1540
|
589 |
/// simplest way of using it is through the \c copyGraph() function.
|
deba@1531
|
590 |
template <typename Target, typename Source>
|
deba@1267
|
591 |
class GraphCopy {
|
deba@1531
|
592 |
public:
|
deba@1531
|
593 |
typedef typename Source::Node Node;
|
deba@1531
|
594 |
typedef typename Source::NodeIt NodeIt;
|
deba@1531
|
595 |
typedef typename Source::Edge Edge;
|
deba@1531
|
596 |
typedef typename Source::EdgeIt EdgeIt;
|
klao@946
|
597 |
|
deba@1531
|
598 |
typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
|
deba@1531
|
599 |
typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
|
klao@946
|
600 |
|
deba@1531
|
601 |
/// \brief Constructor for the GraphCopy.
|
deba@1531
|
602 |
///
|
deba@1531
|
603 |
/// It copies the content of the \c _source graph into the
|
deba@1531
|
604 |
/// \c _target graph. It creates also two references, one beetween
|
deba@1531
|
605 |
/// the two nodeset and one beetween the two edgesets.
|
deba@1531
|
606 |
GraphCopy(Target& _target, const Source& _source)
|
deba@1531
|
607 |
: source(_source), target(_target),
|
deba@1531
|
608 |
nodeRefMap(_source), edgeRefMap(_source) {
|
deba@1531
|
609 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
610 |
nodeRefMap[it] = target.addNode();
|
deba@1531
|
611 |
}
|
deba@1531
|
612 |
for (EdgeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
613 |
edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
|
deba@1531
|
614 |
nodeRefMap[source.target(it)]);
|
deba@1531
|
615 |
}
|
deba@1267
|
616 |
}
|
klao@946
|
617 |
|
deba@1531
|
618 |
/// \brief Copies the node references into the given map.
|
deba@1531
|
619 |
///
|
deba@1531
|
620 |
/// Copies the node references into the given map.
|
deba@1531
|
621 |
template <typename NodeRef>
|
deba@1531
|
622 |
const GraphCopy& nodeRef(NodeRef& map) const {
|
deba@1531
|
623 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
624 |
map.set(it, nodeRefMap[it]);
|
deba@1531
|
625 |
}
|
deba@1531
|
626 |
return *this;
|
deba@1267
|
627 |
}
|
deba@1531
|
628 |
|
deba@1531
|
629 |
/// \brief Reverse and copies the node references into the given map.
|
deba@1531
|
630 |
///
|
deba@1531
|
631 |
/// Reverse and copies the node references into the given map.
|
deba@1531
|
632 |
template <typename NodeRef>
|
deba@1531
|
633 |
const GraphCopy& nodeCrossRef(NodeRef& map) const {
|
deba@1531
|
634 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
635 |
map.set(nodeRefMap[it], it);
|
deba@1531
|
636 |
}
|
deba@1531
|
637 |
return *this;
|
deba@1531
|
638 |
}
|
deba@1531
|
639 |
|
deba@1531
|
640 |
/// \brief Copies the edge references into the given map.
|
deba@1531
|
641 |
///
|
deba@1531
|
642 |
/// Copies the edge references into the given map.
|
deba@1531
|
643 |
template <typename EdgeRef>
|
deba@1531
|
644 |
const GraphCopy& edgeRef(EdgeRef& map) const {
|
deba@1531
|
645 |
for (EdgeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
646 |
map.set(it, edgeRefMap[it]);
|
deba@1531
|
647 |
}
|
deba@1531
|
648 |
return *this;
|
deba@1531
|
649 |
}
|
deba@1531
|
650 |
|
deba@1531
|
651 |
/// \brief Reverse and copies the edge references into the given map.
|
deba@1531
|
652 |
///
|
deba@1531
|
653 |
/// Reverse and copies the edge references into the given map.
|
deba@1531
|
654 |
template <typename EdgeRef>
|
deba@1531
|
655 |
const GraphCopy& edgeCrossRef(EdgeRef& map) const {
|
deba@1531
|
656 |
for (EdgeIt it(source); it != INVALID; ++it) {
|
deba@1531
|
657 |
map.set(edgeRefMap[it], it);
|
deba@1531
|
658 |
}
|
deba@1531
|
659 |
return *this;
|
deba@1531
|
660 |
}
|
deba@1531
|
661 |
|
deba@1531
|
662 |
/// \brief Make copy of the given map.
|
deba@1531
|
663 |
///
|
deba@1531
|
664 |
/// Makes copy of the given map for the newly created graph.
|
deba@1531
|
665 |
/// The new map's key type is the target graph's node type,
|
deba@1531
|
666 |
/// and the copied map's key type is the source graph's node
|
deba@1531
|
667 |
/// type.
|
deba@1531
|
668 |
template <typename TargetMap, typename SourceMap>
|
deba@1531
|
669 |
const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
|
deba@1531
|
670 |
copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
|
deba@1531
|
671 |
return *this;
|
deba@1531
|
672 |
}
|
deba@1531
|
673 |
|
deba@1531
|
674 |
/// \brief Make copy of the given map.
|
deba@1531
|
675 |
///
|
deba@1531
|
676 |
/// Makes copy of the given map for the newly created graph.
|
deba@1531
|
677 |
/// The new map's key type is the target graph's edge type,
|
deba@1531
|
678 |
/// and the copied map's key type is the source graph's edge
|
deba@1531
|
679 |
/// type.
|
deba@1531
|
680 |
template <typename TargetMap, typename SourceMap>
|
deba@1531
|
681 |
const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
|
deba@1531
|
682 |
copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
|
deba@1531
|
683 |
return *this;
|
deba@1531
|
684 |
}
|
deba@1531
|
685 |
|
deba@1531
|
686 |
/// \brief Gives back the stored node references.
|
deba@1531
|
687 |
///
|
deba@1531
|
688 |
/// Gives back the stored node references.
|
deba@1531
|
689 |
const NodeRefMap& nodeRef() const {
|
deba@1531
|
690 |
return nodeRefMap;
|
deba@1531
|
691 |
}
|
deba@1531
|
692 |
|
deba@1531
|
693 |
/// \brief Gives back the stored edge references.
|
deba@1531
|
694 |
///
|
deba@1531
|
695 |
/// Gives back the stored edge references.
|
deba@1531
|
696 |
const EdgeRefMap& edgeRef() const {
|
deba@1531
|
697 |
return edgeRefMap;
|
deba@1531
|
698 |
}
|
deba@1531
|
699 |
|
deba@1981
|
700 |
void run() const {}
|
deba@1720
|
701 |
|
deba@1531
|
702 |
private:
|
deba@1531
|
703 |
|
deba@1531
|
704 |
const Source& source;
|
deba@1531
|
705 |
Target& target;
|
deba@1531
|
706 |
|
deba@1531
|
707 |
NodeRefMap nodeRefMap;
|
deba@1531
|
708 |
EdgeRefMap edgeRefMap;
|
deba@1267
|
709 |
};
|
klao@946
|
710 |
|
alpar@2006
|
711 |
/// \brief Copy a graph to another graph.
|
deba@1531
|
712 |
///
|
alpar@2006
|
713 |
/// Copy a graph to another graph.
|
deba@1531
|
714 |
/// The usage of the function:
|
deba@1531
|
715 |
///
|
alpar@1946
|
716 |
///\code
|
deba@1531
|
717 |
/// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
|
alpar@1946
|
718 |
///\endcode
|
deba@1531
|
719 |
///
|
deba@1531
|
720 |
/// After the copy the \c nr map will contain the mapping from the
|
deba@1531
|
721 |
/// source graph's nodes to the target graph's nodes and the \c ecr will
|
athos@1540
|
722 |
/// contain the mapping from the target graph's edges to the source's
|
deba@1531
|
723 |
/// edges.
|
deba@1531
|
724 |
template <typename Target, typename Source>
|
deba@1531
|
725 |
GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
|
deba@1531
|
726 |
return GraphCopy<Target, Source>(target, source);
|
deba@1531
|
727 |
}
|
klao@946
|
728 |
|
deba@1720
|
729 |
/// \brief Class to copy an undirected graph.
|
deba@1720
|
730 |
///
|
alpar@2006
|
731 |
/// Class to copy an undirected graph to another graph (duplicate a graph).
|
klao@1909
|
732 |
/// The simplest way of using it is through the \c copyUGraph() function.
|
deba@1720
|
733 |
template <typename Target, typename Source>
|
klao@1909
|
734 |
class UGraphCopy {
|
deba@1720
|
735 |
public:
|
deba@1720
|
736 |
typedef typename Source::Node Node;
|
deba@1720
|
737 |
typedef typename Source::NodeIt NodeIt;
|
deba@1720
|
738 |
typedef typename Source::Edge Edge;
|
deba@1720
|
739 |
typedef typename Source::EdgeIt EdgeIt;
|
klao@1909
|
740 |
typedef typename Source::UEdge UEdge;
|
klao@1909
|
741 |
typedef typename Source::UEdgeIt UEdgeIt;
|
deba@1720
|
742 |
|
deba@1720
|
743 |
typedef typename Source::
|
deba@1720
|
744 |
template NodeMap<typename Target::Node> NodeRefMap;
|
deba@1720
|
745 |
|
deba@1720
|
746 |
typedef typename Source::
|
klao@1909
|
747 |
template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
|
deba@1720
|
748 |
|
deba@1720
|
749 |
private:
|
deba@1720
|
750 |
|
deba@1720
|
751 |
struct EdgeRefMap {
|
klao@1909
|
752 |
EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
|
deba@1720
|
753 |
typedef typename Source::Edge Key;
|
deba@1720
|
754 |
typedef typename Target::Edge Value;
|
deba@1720
|
755 |
|
deba@1720
|
756 |
Value operator[](const Key& key) {
|
klao@1909
|
757 |
return gc.target.direct(gc.uEdgeRef[key],
|
deba@1720
|
758 |
gc.target.direction(key));
|
deba@1720
|
759 |
}
|
deba@1720
|
760 |
|
klao@1909
|
761 |
UGraphCopy& gc;
|
deba@1720
|
762 |
};
|
deba@1720
|
763 |
|
deba@1192
|
764 |
public:
|
deba@1720
|
765 |
|
klao@1909
|
766 |
/// \brief Constructor for the UGraphCopy.
|
deba@1720
|
767 |
///
|
deba@1720
|
768 |
/// It copies the content of the \c _source graph into the
|
deba@1720
|
769 |
/// \c _target graph. It creates also two references, one beetween
|
deba@1720
|
770 |
/// the two nodeset and one beetween the two edgesets.
|
klao@1909
|
771 |
UGraphCopy(Target& _target, const Source& _source)
|
deba@1720
|
772 |
: source(_source), target(_target),
|
klao@1909
|
773 |
nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
|
deba@1720
|
774 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1720
|
775 |
nodeRefMap[it] = target.addNode();
|
deba@1720
|
776 |
}
|
klao@1909
|
777 |
for (UEdgeIt it(source); it != INVALID; ++it) {
|
klao@1909
|
778 |
uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
|
deba@1720
|
779 |
nodeRefMap[source.target(it)]);
|
deba@1720
|
780 |
}
|
deba@1720
|
781 |
}
|
deba@1720
|
782 |
|
deba@1720
|
783 |
/// \brief Copies the node references into the given map.
|
deba@1720
|
784 |
///
|
deba@1720
|
785 |
/// Copies the node references into the given map.
|
deba@1720
|
786 |
template <typename NodeRef>
|
klao@1909
|
787 |
const UGraphCopy& nodeRef(NodeRef& map) const {
|
deba@1720
|
788 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1720
|
789 |
map.set(it, nodeRefMap[it]);
|
deba@1720
|
790 |
}
|
deba@1720
|
791 |
return *this;
|
deba@1720
|
792 |
}
|
deba@1720
|
793 |
|
deba@1720
|
794 |
/// \brief Reverse and copies the node references into the given map.
|
deba@1720
|
795 |
///
|
deba@1720
|
796 |
/// Reverse and copies the node references into the given map.
|
deba@1720
|
797 |
template <typename NodeRef>
|
klao@1909
|
798 |
const UGraphCopy& nodeCrossRef(NodeRef& map) const {
|
deba@1720
|
799 |
for (NodeIt it(source); it != INVALID; ++it) {
|
deba@1720
|
800 |
map.set(nodeRefMap[it], it);
|
deba@1720
|
801 |
}
|
deba@1720
|
802 |
return *this;
|
deba@1720
|
803 |
}
|
deba@1720
|
804 |
|
deba@1720
|
805 |
/// \brief Copies the edge references into the given map.
|
deba@1720
|
806 |
///
|
deba@1720
|
807 |
/// Copies the edge references into the given map.
|
deba@1720
|
808 |
template <typename EdgeRef>
|
klao@1909
|
809 |
const UGraphCopy& edgeRef(EdgeRef& map) const {
|
deba@1720
|
810 |
for (EdgeIt it(source); it != INVALID; ++it) {
|
deba@1720
|
811 |
map.set(edgeRefMap[it], it);
|
deba@1720
|
812 |
}
|
deba@1720
|
813 |
return *this;
|
deba@1720
|
814 |
}
|
deba@1720
|
815 |
|
deba@1720
|
816 |
/// \brief Reverse and copies the undirected edge references into the
|
deba@1720
|
817 |
/// given map.
|
deba@1720
|
818 |
///
|
deba@1720
|
819 |
/// Reverse and copies the undirected edge references into the given map.
|
deba@1720
|
820 |
template <typename EdgeRef>
|
klao@1909
|
821 |
const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
|
deba@1720
|
822 |
for (EdgeIt it(source); it != INVALID; ++it) {
|
deba@1720
|
823 |
map.set(it, edgeRefMap[it]);
|
deba@1720
|
824 |
}
|
deba@1720
|
825 |
return *this;
|
deba@1720
|
826 |
}
|
deba@1720
|
827 |
|
deba@1720
|
828 |
/// \brief Copies the undirected edge references into the given map.
|
deba@1720
|
829 |
///
|
deba@1720
|
830 |
/// Copies the undirected edge references into the given map.
|
deba@1720
|
831 |
template <typename EdgeRef>
|
klao@1909
|
832 |
const UGraphCopy& uEdgeRef(EdgeRef& map) const {
|
klao@1909
|
833 |
for (UEdgeIt it(source); it != INVALID; ++it) {
|
klao@1909
|
834 |
map.set(it, uEdgeRefMap[it]);
|
deba@1720
|
835 |
}
|
deba@1720
|
836 |
return *this;
|
deba@1720
|
837 |
}
|
deba@1720
|
838 |
|
deba@1720
|
839 |
/// \brief Reverse and copies the undirected edge references into the
|
deba@1720
|
840 |
/// given map.
|
deba@1720
|
841 |
///
|
deba@1720
|
842 |
/// Reverse and copies the undirected edge references into the given map.
|
deba@1720
|
843 |
template <typename EdgeRef>
|
klao@1909
|
844 |
const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
|
klao@1909
|
845 |
for (UEdgeIt it(source); it != INVALID; ++it) {
|
klao@1909
|
846 |
map.set(uEdgeRefMap[it], it);
|
deba@1720
|
847 |
}
|
deba@1720
|
848 |
return *this;
|
deba@1720
|
849 |
}
|
deba@1720
|
850 |
|
deba@1720
|
851 |
/// \brief Make copy of the given map.
|
deba@1720
|
852 |
///
|
deba@1720
|
853 |
/// Makes copy of the given map for the newly created graph.
|
deba@1720
|
854 |
/// The new map's key type is the target graph's node type,
|
deba@1720
|
855 |
/// and the copied map's key type is the source graph's node
|
deba@1720
|
856 |
/// type.
|
deba@1720
|
857 |
template <typename TargetMap, typename SourceMap>
|
klao@1909
|
858 |
const UGraphCopy& nodeMap(TargetMap& tMap,
|
deba@1720
|
859 |
const SourceMap& sMap) const {
|
deba@1720
|
860 |
copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
|
deba@1720
|
861 |
return *this;
|
deba@1720
|
862 |
}
|
deba@1720
|
863 |
|
deba@1720
|
864 |
/// \brief Make copy of the given map.
|
deba@1720
|
865 |
///
|
deba@1720
|
866 |
/// Makes copy of the given map for the newly created graph.
|
deba@1720
|
867 |
/// The new map's key type is the target graph's edge type,
|
deba@1720
|
868 |
/// and the copied map's key type is the source graph's edge
|
deba@1720
|
869 |
/// type.
|
deba@1720
|
870 |
template <typename TargetMap, typename SourceMap>
|
klao@1909
|
871 |
const UGraphCopy& edgeMap(TargetMap& tMap,
|
deba@1720
|
872 |
const SourceMap& sMap) const {
|
deba@1720
|
873 |
copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
|
deba@1720
|
874 |
return *this;
|
deba@1720
|
875 |
}
|
deba@1720
|
876 |
|
deba@1720
|
877 |
/// \brief Make copy of the given map.
|
deba@1720
|
878 |
///
|
deba@1720
|
879 |
/// Makes copy of the given map for the newly created graph.
|
deba@1720
|
880 |
/// The new map's key type is the target graph's edge type,
|
deba@1720
|
881 |
/// and the copied map's key type is the source graph's edge
|
deba@1720
|
882 |
/// type.
|
deba@1720
|
883 |
template <typename TargetMap, typename SourceMap>
|
klao@1909
|
884 |
const UGraphCopy& uEdgeMap(TargetMap& tMap,
|
deba@1720
|
885 |
const SourceMap& sMap) const {
|
klao@1909
|
886 |
copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
|
deba@1720
|
887 |
return *this;
|
deba@1720
|
888 |
}
|
deba@1720
|
889 |
|
deba@1720
|
890 |
/// \brief Gives back the stored node references.
|
deba@1720
|
891 |
///
|
deba@1720
|
892 |
/// Gives back the stored node references.
|
deba@1720
|
893 |
const NodeRefMap& nodeRef() const {
|
deba@1720
|
894 |
return nodeRefMap;
|
deba@1720
|
895 |
}
|
deba@1720
|
896 |
|
deba@1720
|
897 |
/// \brief Gives back the stored edge references.
|
deba@1720
|
898 |
///
|
deba@1720
|
899 |
/// Gives back the stored edge references.
|
deba@1720
|
900 |
const EdgeRefMap& edgeRef() const {
|
deba@1720
|
901 |
return edgeRefMap;
|
deba@1720
|
902 |
}
|
deba@1720
|
903 |
|
klao@1909
|
904 |
/// \brief Gives back the stored uedge references.
|
deba@1720
|
905 |
///
|
klao@1909
|
906 |
/// Gives back the stored uedge references.
|
klao@1909
|
907 |
const UEdgeRefMap& uEdgeRef() const {
|
klao@1909
|
908 |
return uEdgeRefMap;
|
deba@1720
|
909 |
}
|
deba@1720
|
910 |
|
deba@1981
|
911 |
void run() const {}
|
deba@1720
|
912 |
|
deba@1720
|
913 |
private:
|
deba@1192
|
914 |
|
deba@1720
|
915 |
const Source& source;
|
deba@1720
|
916 |
Target& target;
|
alpar@947
|
917 |
|
deba@1720
|
918 |
NodeRefMap nodeRefMap;
|
deba@1720
|
919 |
EdgeRefMap edgeRefMap;
|
klao@1909
|
920 |
UEdgeRefMap uEdgeRefMap;
|
deba@1192
|
921 |
};
|
deba@1192
|
922 |
|
alpar@2006
|
923 |
/// \brief Copy a graph to another graph.
|
deba@1720
|
924 |
///
|
alpar@2006
|
925 |
/// Copy a graph to another graph.
|
deba@1720
|
926 |
/// The usage of the function:
|
deba@1720
|
927 |
///
|
alpar@1946
|
928 |
///\code
|
alpar@2022
|
929 |
/// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
|
alpar@1946
|
930 |
///\endcode
|
deba@1720
|
931 |
///
|
deba@1720
|
932 |
/// After the copy the \c nr map will contain the mapping from the
|
deba@1720
|
933 |
/// source graph's nodes to the target graph's nodes and the \c ecr will
|
deba@1720
|
934 |
/// contain the mapping from the target graph's edges to the source's
|
deba@1720
|
935 |
/// edges.
|
deba@1720
|
936 |
template <typename Target, typename Source>
|
klao@1909
|
937 |
UGraphCopy<Target, Source>
|
klao@1909
|
938 |
copyUGraph(Target& target, const Source& source) {
|
klao@1909
|
939 |
return UGraphCopy<Target, Source>(target, source);
|
deba@1720
|
940 |
}
|
deba@1192
|
941 |
|
deba@1192
|
942 |
|
deba@1192
|
943 |
/// @}
|
alpar@1402
|
944 |
|
alpar@1402
|
945 |
/// \addtogroup graph_maps
|
alpar@1402
|
946 |
/// @{
|
alpar@1402
|
947 |
|
deba@1413
|
948 |
/// Provides an immutable and unique id for each item in the graph.
|
deba@1413
|
949 |
|
athos@1540
|
950 |
/// The IdMap class provides a unique and immutable id for each item of the
|
athos@1540
|
951 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
|
athos@1540
|
952 |
/// different items (nodes) get different ids <li>\b immutable: the id of an
|
athos@1540
|
953 |
/// item (node) does not change (even if you delete other nodes). </ul>
|
athos@1540
|
954 |
/// Through this map you get access (i.e. can read) the inner id values of
|
athos@1540
|
955 |
/// the items stored in the graph. This map can be inverted with its member
|
athos@1540
|
956 |
/// class \c InverseMap.
|
deba@1413
|
957 |
///
|
deba@1413
|
958 |
template <typename _Graph, typename _Item>
|
deba@1413
|
959 |
class IdMap {
|
deba@1413
|
960 |
public:
|
deba@1413
|
961 |
typedef _Graph Graph;
|
deba@1413
|
962 |
typedef int Value;
|
deba@1413
|
963 |
typedef _Item Item;
|
deba@1413
|
964 |
typedef _Item Key;
|
deba@1413
|
965 |
|
deba@1413
|
966 |
/// \brief Constructor.
|
deba@1413
|
967 |
///
|
deba@1413
|
968 |
/// Constructor for creating id map.
|
deba@1413
|
969 |
IdMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
970 |
|
deba@1413
|
971 |
/// \brief Gives back the \e id of the item.
|
deba@1413
|
972 |
///
|
deba@1413
|
973 |
/// Gives back the immutable and unique \e id of the map.
|
deba@1413
|
974 |
int operator[](const Item& item) const { return graph->id(item);}
|
deba@1413
|
975 |
|
deba@1413
|
976 |
|
deba@1413
|
977 |
private:
|
deba@1413
|
978 |
const Graph* graph;
|
deba@1413
|
979 |
|
deba@1413
|
980 |
public:
|
deba@1413
|
981 |
|
athos@1540
|
982 |
/// \brief The class represents the inverse of its owner (IdMap).
|
deba@1413
|
983 |
///
|
athos@1540
|
984 |
/// The class represents the inverse of its owner (IdMap).
|
deba@1413
|
985 |
/// \see inverse()
|
deba@1413
|
986 |
class InverseMap {
|
deba@1413
|
987 |
public:
|
deba@1419
|
988 |
|
deba@1413
|
989 |
/// \brief Constructor.
|
deba@1413
|
990 |
///
|
deba@1413
|
991 |
/// Constructor for creating an id-to-item map.
|
deba@1413
|
992 |
InverseMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
993 |
|
deba@1413
|
994 |
/// \brief Constructor.
|
deba@1413
|
995 |
///
|
deba@1413
|
996 |
/// Constructor for creating an id-to-item map.
|
deba@1413
|
997 |
InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
|
deba@1413
|
998 |
|
deba@1413
|
999 |
/// \brief Gives back the given item from its id.
|
deba@1413
|
1000 |
///
|
deba@1413
|
1001 |
/// Gives back the given item from its id.
|
deba@1413
|
1002 |
///
|
deba@1413
|
1003 |
Item operator[](int id) const { return graph->fromId(id, Item());}
|
deba@1413
|
1004 |
private:
|
deba@1413
|
1005 |
const Graph* graph;
|
deba@1413
|
1006 |
};
|
deba@1413
|
1007 |
|
deba@1413
|
1008 |
/// \brief Gives back the inverse of the map.
|
deba@1413
|
1009 |
///
|
athos@1540
|
1010 |
/// Gives back the inverse of the IdMap.
|
deba@1413
|
1011 |
InverseMap inverse() const { return InverseMap(*graph);}
|
deba@1413
|
1012 |
|
deba@1413
|
1013 |
};
|
deba@1413
|
1014 |
|
deba@1413
|
1015 |
|
athos@1526
|
1016 |
/// \brief General invertable graph-map type.
|
alpar@1402
|
1017 |
|
athos@1540
|
1018 |
/// This type provides simple invertable graph-maps.
|
athos@1526
|
1019 |
/// The InvertableMap wraps an arbitrary ReadWriteMap
|
athos@1526
|
1020 |
/// and if a key is set to a new value then store it
|
alpar@1402
|
1021 |
/// in the inverse map.
|
deba@1931
|
1022 |
///
|
deba@1931
|
1023 |
/// The values of the map can be accessed
|
deba@1931
|
1024 |
/// with stl compatible forward iterator.
|
deba@1931
|
1025 |
///
|
alpar@1402
|
1026 |
/// \param _Graph The graph type.
|
deba@1830
|
1027 |
/// \param _Item The item type of the graph.
|
deba@1830
|
1028 |
/// \param _Value The value type of the map.
|
deba@1931
|
1029 |
///
|
deba@1931
|
1030 |
/// \see IterableValueMap
|
deba@1830
|
1031 |
#ifndef DOXYGEN
|
deba@1830
|
1032 |
/// \param _Map A ReadWriteMap mapping from the item type to integer.
|
alpar@1402
|
1033 |
template <
|
deba@1990
|
1034 |
typename _Graph, typename _Item, typename _Value,
|
deba@1990
|
1035 |
typename _Map = DefaultMap<_Graph, _Item, _Value>
|
alpar@1402
|
1036 |
>
|
deba@1830
|
1037 |
#else
|
deba@1830
|
1038 |
template <typename _Graph, typename _Item, typename _Value>
|
deba@1830
|
1039 |
#endif
|
deba@1413
|
1040 |
class InvertableMap : protected _Map {
|
alpar@1402
|
1041 |
public:
|
deba@1413
|
1042 |
|
klao@1909
|
1043 |
/// The key type of InvertableMap (Node, Edge, UEdge).
|
alpar@1402
|
1044 |
typedef typename _Map::Key Key;
|
deba@1413
|
1045 |
/// The value type of the InvertableMap.
|
alpar@1402
|
1046 |
typedef typename _Map::Value Value;
|
alpar@1402
|
1047 |
|
deba@1931
|
1048 |
private:
|
deba@1931
|
1049 |
|
deba@1931
|
1050 |
typedef _Map Map;
|
deba@1931
|
1051 |
typedef _Graph Graph;
|
deba@1931
|
1052 |
|
deba@1931
|
1053 |
typedef std::map<Value, Key> Container;
|
deba@1931
|
1054 |
Container invMap;
|
deba@1931
|
1055 |
|
deba@1931
|
1056 |
public:
|
deba@1931
|
1057 |
|
deba@1931
|
1058 |
|
deba@1931
|
1059 |
|
alpar@1402
|
1060 |
/// \brief Constructor.
|
alpar@1402
|
1061 |
///
|
deba@1413
|
1062 |
/// Construct a new InvertableMap for the graph.
|
alpar@1402
|
1063 |
///
|
deba@1413
|
1064 |
InvertableMap(const Graph& graph) : Map(graph) {}
|
deba@1931
|
1065 |
|
deba@1931
|
1066 |
/// \brief Forward iterator for values.
|
deba@1931
|
1067 |
///
|
deba@1931
|
1068 |
/// This iterator is an stl compatible forward
|
deba@1931
|
1069 |
/// iterator on the values of the map. The values can
|
deba@1931
|
1070 |
/// be accessed in the [beginValue, endValue) range.
|
deba@1931
|
1071 |
///
|
deba@1931
|
1072 |
class ValueIterator
|
deba@1931
|
1073 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
deba@1931
|
1074 |
friend class InvertableMap;
|
deba@1931
|
1075 |
private:
|
deba@1931
|
1076 |
ValueIterator(typename Container::const_iterator _it)
|
deba@1931
|
1077 |
: it(_it) {}
|
deba@1931
|
1078 |
public:
|
deba@1931
|
1079 |
|
deba@1931
|
1080 |
ValueIterator() {}
|
deba@1931
|
1081 |
|
deba@1931
|
1082 |
ValueIterator& operator++() { ++it; return *this; }
|
deba@1931
|
1083 |
ValueIterator operator++(int) {
|
deba@1931
|
1084 |
ValueIterator tmp(*this);
|
deba@1931
|
1085 |
operator++();
|
deba@1931
|
1086 |
return tmp;
|
deba@1931
|
1087 |
}
|
deba@1931
|
1088 |
|
deba@1931
|
1089 |
const Value& operator*() const { return it->first; }
|
deba@1931
|
1090 |
const Value* operator->() const { return &(it->first); }
|
deba@1931
|
1091 |
|
deba@1931
|
1092 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
deba@1931
|
1093 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
deba@1931
|
1094 |
|
deba@1931
|
1095 |
private:
|
deba@1931
|
1096 |
typename Container::const_iterator it;
|
deba@1931
|
1097 |
};
|
deba@1931
|
1098 |
|
deba@1931
|
1099 |
/// \brief Returns an iterator to the first value.
|
deba@1931
|
1100 |
///
|
deba@1931
|
1101 |
/// Returns an stl compatible iterator to the
|
deba@1931
|
1102 |
/// first value of the map. The values of the
|
deba@1931
|
1103 |
/// map can be accessed in the [beginValue, endValue)
|
deba@1931
|
1104 |
/// range.
|
deba@1931
|
1105 |
ValueIterator beginValue() const {
|
deba@1931
|
1106 |
return ValueIterator(invMap.begin());
|
deba@1931
|
1107 |
}
|
deba@1931
|
1108 |
|
deba@1931
|
1109 |
/// \brief Returns an iterator after the last value.
|
deba@1931
|
1110 |
///
|
deba@1931
|
1111 |
/// Returns an stl compatible iterator after the
|
deba@1931
|
1112 |
/// last value of the map. The values of the
|
deba@1931
|
1113 |
/// map can be accessed in the [beginValue, endValue)
|
deba@1931
|
1114 |
/// range.
|
deba@1931
|
1115 |
ValueIterator endValue() const {
|
deba@1931
|
1116 |
return ValueIterator(invMap.end());
|
deba@1931
|
1117 |
}
|
alpar@1402
|
1118 |
|
alpar@1402
|
1119 |
/// \brief The setter function of the map.
|
alpar@1402
|
1120 |
///
|
deba@1413
|
1121 |
/// Sets the mapped value.
|
alpar@1402
|
1122 |
void set(const Key& key, const Value& val) {
|
alpar@1402
|
1123 |
Value oldval = Map::operator[](key);
|
deba@1413
|
1124 |
typename Container::iterator it = invMap.find(oldval);
|
alpar@1402
|
1125 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
1126 |
invMap.erase(it);
|
alpar@1402
|
1127 |
}
|
alpar@1402
|
1128 |
invMap.insert(make_pair(val, key));
|
alpar@1402
|
1129 |
Map::set(key, val);
|
alpar@1402
|
1130 |
}
|
alpar@1402
|
1131 |
|
alpar@1402
|
1132 |
/// \brief The getter function of the map.
|
alpar@1402
|
1133 |
///
|
alpar@1402
|
1134 |
/// It gives back the value associated with the key.
|
deba@1931
|
1135 |
typename MapTraits<Map>::ConstReturnValue
|
deba@1931
|
1136 |
operator[](const Key& key) const {
|
alpar@1402
|
1137 |
return Map::operator[](key);
|
alpar@1402
|
1138 |
}
|
alpar@1402
|
1139 |
|
deba@1515
|
1140 |
protected:
|
deba@1515
|
1141 |
|
alpar@1402
|
1142 |
/// \brief Erase the key from the map.
|
alpar@1402
|
1143 |
///
|
alpar@1402
|
1144 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
1145 |
/// \c AlterationNotifier.
|
alpar@1402
|
1146 |
virtual void erase(const Key& key) {
|
alpar@1402
|
1147 |
Value val = Map::operator[](key);
|
deba@1413
|
1148 |
typename Container::iterator it = invMap.find(val);
|
alpar@1402
|
1149 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
1150 |
invMap.erase(it);
|
alpar@1402
|
1151 |
}
|
alpar@1402
|
1152 |
Map::erase(key);
|
alpar@1402
|
1153 |
}
|
alpar@1402
|
1154 |
|
deba@1829
|
1155 |
/// \brief Erase more keys from the map.
|
deba@1829
|
1156 |
///
|
deba@1829
|
1157 |
/// Erase more keys from the map. It is called by the
|
deba@1829
|
1158 |
/// \c AlterationNotifier.
|
deba@1829
|
1159 |
virtual void erase(const std::vector<Key>& keys) {
|
deba@1829
|
1160 |
for (int i = 0; i < (int)keys.size(); ++i) {
|
deba@1829
|
1161 |
Value val = Map::operator[](keys[i]);
|
deba@1829
|
1162 |
typename Container::iterator it = invMap.find(val);
|
deba@1829
|
1163 |
if (it != invMap.end() && it->second == keys[i]) {
|
deba@1829
|
1164 |
invMap.erase(it);
|
deba@1829
|
1165 |
}
|
deba@1829
|
1166 |
}
|
deba@1829
|
1167 |
Map::erase(keys);
|
deba@1829
|
1168 |
}
|
deba@1829
|
1169 |
|
alpar@1402
|
1170 |
/// \brief Clear the keys from the map and inverse map.
|
alpar@1402
|
1171 |
///
|
alpar@1402
|
1172 |
/// Clear the keys from the map and inverse map. It is called by the
|
alpar@1402
|
1173 |
/// \c AlterationNotifier.
|
alpar@1402
|
1174 |
virtual void clear() {
|
alpar@1402
|
1175 |
invMap.clear();
|
alpar@1402
|
1176 |
Map::clear();
|
alpar@1402
|
1177 |
}
|
alpar@1402
|
1178 |
|
deba@1413
|
1179 |
public:
|
deba@1413
|
1180 |
|
deba@1413
|
1181 |
/// \brief The inverse map type.
|
deba@1413
|
1182 |
///
|
deba@1413
|
1183 |
/// The inverse of this map. The subscript operator of the map
|
deba@1413
|
1184 |
/// gives back always the item what was last assigned to the value.
|
deba@1413
|
1185 |
class InverseMap {
|
deba@1413
|
1186 |
public:
|
deba@1413
|
1187 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
1188 |
///
|
deba@1413
|
1189 |
/// Constructor of the InverseMap.
|
deba@1413
|
1190 |
InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
|
deba@1413
|
1191 |
|
deba@1413
|
1192 |
/// The value type of the InverseMap.
|
deba@1413
|
1193 |
typedef typename InvertableMap::Key Value;
|
deba@1413
|
1194 |
/// The key type of the InverseMap.
|
deba@1413
|
1195 |
typedef typename InvertableMap::Value Key;
|
deba@1413
|
1196 |
|
deba@1413
|
1197 |
/// \brief Subscript operator.
|
deba@1413
|
1198 |
///
|
deba@1413
|
1199 |
/// Subscript operator. It gives back always the item
|
deba@1413
|
1200 |
/// what was last assigned to the value.
|
deba@1413
|
1201 |
Value operator[](const Key& key) const {
|
deba@1413
|
1202 |
typename Container::const_iterator it = inverted.invMap.find(key);
|
deba@1413
|
1203 |
return it->second;
|
deba@1413
|
1204 |
}
|
deba@1413
|
1205 |
|
deba@1413
|
1206 |
private:
|
deba@1413
|
1207 |
const InvertableMap& inverted;
|
deba@1413
|
1208 |
};
|
deba@1413
|
1209 |
|
alpar@2094
|
1210 |
/// \brief It gives back the just readable inverse map.
|
alpar@1402
|
1211 |
///
|
alpar@2094
|
1212 |
/// It gives back the just readable inverse map.
|
deba@1413
|
1213 |
InverseMap inverse() const {
|
deba@1413
|
1214 |
return InverseMap(*this);
|
alpar@1402
|
1215 |
}
|
alpar@1402
|
1216 |
|
alpar@1402
|
1217 |
|
deba@1413
|
1218 |
|
alpar@1402
|
1219 |
};
|
alpar@1402
|
1220 |
|
alpar@1402
|
1221 |
/// \brief Provides a mutable, continuous and unique descriptor for each
|
alpar@1402
|
1222 |
/// item in the graph.
|
alpar@1402
|
1223 |
///
|
athos@1540
|
1224 |
/// The DescriptorMap class provides a unique and continuous (but mutable)
|
athos@1540
|
1225 |
/// descriptor (id) for each item of the same type (e.g. node) in the
|
athos@1540
|
1226 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get
|
athos@1540
|
1227 |
/// different ids <li>\b continuous: the range of the ids is the set of
|
athos@1540
|
1228 |
/// integers between 0 and \c n-1, where \c n is the number of the items of
|
athos@1540
|
1229 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an
|
athos@1540
|
1230 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted
|
athos@1540
|
1231 |
/// with its member class \c InverseMap.
|
alpar@1402
|
1232 |
///
|
alpar@1402
|
1233 |
/// \param _Graph The graph class the \c DescriptorMap belongs to.
|
alpar@1402
|
1234 |
/// \param _Item The Item is the Key of the Map. It may be Node, Edge or
|
klao@1909
|
1235 |
/// UEdge.
|
deba@1830
|
1236 |
#ifndef DOXYGEN
|
alpar@1402
|
1237 |
/// \param _Map A ReadWriteMap mapping from the item type to integer.
|
alpar@1402
|
1238 |
template <
|
deba@1990
|
1239 |
typename _Graph, typename _Item,
|
deba@1990
|
1240 |
typename _Map = DefaultMap<_Graph, _Item, int>
|
alpar@1402
|
1241 |
>
|
deba@1830
|
1242 |
#else
|
deba@1830
|
1243 |
template <typename _Graph, typename _Item>
|
deba@1830
|
1244 |
#endif
|
alpar@1402
|
1245 |
class DescriptorMap : protected _Map {
|
alpar@1402
|
1246 |
|
alpar@1402
|
1247 |
typedef _Item Item;
|
alpar@1402
|
1248 |
typedef _Map Map;
|
alpar@1402
|
1249 |
|
alpar@1402
|
1250 |
public:
|
alpar@1402
|
1251 |
/// The graph class of DescriptorMap.
|
alpar@1402
|
1252 |
typedef _Graph Graph;
|
alpar@1402
|
1253 |
|
klao@1909
|
1254 |
/// The key type of DescriptorMap (Node, Edge, UEdge).
|
alpar@1402
|
1255 |
typedef typename _Map::Key Key;
|
alpar@1402
|
1256 |
/// The value type of DescriptorMap.
|
alpar@1402
|
1257 |
typedef typename _Map::Value Value;
|
alpar@1402
|
1258 |
|
alpar@1402
|
1259 |
/// \brief Constructor.
|
alpar@1402
|
1260 |
///
|
deba@1413
|
1261 |
/// Constructor for descriptor map.
|
alpar@1402
|
1262 |
DescriptorMap(const Graph& _graph) : Map(_graph) {
|
deba@2201
|
1263 |
Item it;
|
deba@2201
|
1264 |
const typename Map::Notifier* notifier = Map::getNotifier();
|
deba@2201
|
1265 |
for (notifier->first(it); it != INVALID; notifier->next(it)) {
|
deba@2201
|
1266 |
Map::set(it, invMap.size());
|
deba@2201
|
1267 |
invMap.push_back(it);
|
deba@2201
|
1268 |
}
|
alpar@1402
|
1269 |
}
|
alpar@1402
|
1270 |
|
deba@1515
|
1271 |
protected:
|
deba@1515
|
1272 |
|
alpar@1402
|
1273 |
/// \brief Add a new key to the map.
|
alpar@1402
|
1274 |
///
|
alpar@1402
|
1275 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
1276 |
/// \c AlterationNotifier.
|
alpar@1402
|
1277 |
virtual void add(const Item& item) {
|
alpar@1402
|
1278 |
Map::add(item);
|
alpar@1402
|
1279 |
Map::set(item, invMap.size());
|
alpar@1402
|
1280 |
invMap.push_back(item);
|
alpar@1402
|
1281 |
}
|
alpar@1402
|
1282 |
|
deba@1829
|
1283 |
/// \brief Add more new keys to the map.
|
deba@1829
|
1284 |
///
|
deba@1829
|
1285 |
/// Add more new keys to the map. It is called by the
|
deba@1829
|
1286 |
/// \c AlterationNotifier.
|
deba@1829
|
1287 |
virtual void add(const std::vector<Item>& items) {
|
deba@1829
|
1288 |
Map::add(items);
|
deba@1829
|
1289 |
for (int i = 0; i < (int)items.size(); ++i) {
|
deba@1829
|
1290 |
Map::set(items[i], invMap.size());
|
deba@1829
|
1291 |
invMap.push_back(items[i]);
|
deba@1829
|
1292 |
}
|
deba@1829
|
1293 |
}
|
deba@1829
|
1294 |
|
alpar@1402
|
1295 |
/// \brief Erase the key from the map.
|
alpar@1402
|
1296 |
///
|
deba@1829
|
1297 |
/// Erase the key from the map. It is called by the
|
alpar@1402
|
1298 |
/// \c AlterationNotifier.
|
alpar@1402
|
1299 |
virtual void erase(const Item& item) {
|
alpar@1402
|
1300 |
Map::set(invMap.back(), Map::operator[](item));
|
alpar@1402
|
1301 |
invMap[Map::operator[](item)] = invMap.back();
|
deba@1413
|
1302 |
invMap.pop_back();
|
alpar@1402
|
1303 |
Map::erase(item);
|
alpar@1402
|
1304 |
}
|
alpar@1402
|
1305 |
|
deba@1829
|
1306 |
/// \brief Erase more keys from the map.
|
deba@1829
|
1307 |
///
|
deba@1829
|
1308 |
/// Erase more keys from the map. It is called by the
|
deba@1829
|
1309 |
/// \c AlterationNotifier.
|
deba@1829
|
1310 |
virtual void erase(const std::vector<Item>& items) {
|
deba@1829
|
1311 |
for (int i = 0; i < (int)items.size(); ++i) {
|
deba@1829
|
1312 |
Map::set(invMap.back(), Map::operator[](items[i]));
|
deba@1829
|
1313 |
invMap[Map::operator[](items[i])] = invMap.back();
|
deba@1829
|
1314 |
invMap.pop_back();
|
deba@1829
|
1315 |
}
|
deba@1829
|
1316 |
Map::erase(items);
|
deba@1829
|
1317 |
}
|
deba@1829
|
1318 |
|
alpar@1402
|
1319 |
/// \brief Build the unique map.
|
alpar@1402
|
1320 |
///
|
alpar@1402
|
1321 |
/// Build the unique map. It is called by the
|
alpar@1402
|
1322 |
/// \c AlterationNotifier.
|
alpar@1402
|
1323 |
virtual void build() {
|
alpar@1402
|
1324 |
Map::build();
|
alpar@1402
|
1325 |
Item it;
|
deba@1999
|
1326 |
const typename Map::Notifier* notifier = Map::getNotifier();
|
deba@1999
|
1327 |
for (notifier->first(it); it != INVALID; notifier->next(it)) {
|
alpar@1402
|
1328 |
Map::set(it, invMap.size());
|
alpar@1402
|
1329 |
invMap.push_back(it);
|
alpar@1402
|
1330 |
}
|
alpar@1402
|
1331 |
}
|
alpar@1402
|
1332 |
|
alpar@1402
|
1333 |
/// \brief Clear the keys from the map.
|
alpar@1402
|
1334 |
///
|
alpar@1402
|
1335 |
/// Clear the keys from the map. It is called by the
|
alpar@1402
|
1336 |
/// \c AlterationNotifier.
|
alpar@1402
|
1337 |
virtual void clear() {
|
alpar@1402
|
1338 |
invMap.clear();
|
alpar@1402
|
1339 |
Map::clear();
|
alpar@1402
|
1340 |
}
|
alpar@1402
|
1341 |
|
deba@1538
|
1342 |
public:
|
deba@1538
|
1343 |
|
deba@1931
|
1344 |
/// \brief Returns the maximal value plus one.
|
deba@1931
|
1345 |
///
|
deba@1931
|
1346 |
/// Returns the maximal value plus one in the map.
|
deba@1931
|
1347 |
unsigned int size() const {
|
deba@1931
|
1348 |
return invMap.size();
|
deba@1931
|
1349 |
}
|
deba@1931
|
1350 |
|
deba@1552
|
1351 |
/// \brief Swaps the position of the two items in the map.
|
deba@1552
|
1352 |
///
|
deba@1552
|
1353 |
/// Swaps the position of the two items in the map.
|
deba@1552
|
1354 |
void swap(const Item& p, const Item& q) {
|
deba@1552
|
1355 |
int pi = Map::operator[](p);
|
deba@1552
|
1356 |
int qi = Map::operator[](q);
|
deba@1552
|
1357 |
Map::set(p, qi);
|
deba@1552
|
1358 |
invMap[qi] = p;
|
deba@1552
|
1359 |
Map::set(q, pi);
|
deba@1552
|
1360 |
invMap[pi] = q;
|
deba@1552
|
1361 |
}
|
deba@1552
|
1362 |
|
alpar@1402
|
1363 |
/// \brief Gives back the \e descriptor of the item.
|
alpar@1402
|
1364 |
///
|
alpar@1402
|
1365 |
/// Gives back the mutable and unique \e descriptor of the map.
|
alpar@1402
|
1366 |
int operator[](const Item& item) const {
|
alpar@1402
|
1367 |
return Map::operator[](item);
|
alpar@1402
|
1368 |
}
|
alpar@1402
|
1369 |
|
deba@1413
|
1370 |
private:
|
deba@1413
|
1371 |
|
deba@1413
|
1372 |
typedef std::vector<Item> Container;
|
deba@1413
|
1373 |
Container invMap;
|
deba@1413
|
1374 |
|
deba@1413
|
1375 |
public:
|
athos@1540
|
1376 |
/// \brief The inverse map type of DescriptorMap.
|
deba@1413
|
1377 |
///
|
athos@1540
|
1378 |
/// The inverse map type of DescriptorMap.
|
deba@1413
|
1379 |
class InverseMap {
|
deba@1413
|
1380 |
public:
|
deba@1413
|
1381 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
1382 |
///
|
deba@1413
|
1383 |
/// Constructor of the InverseMap.
|
deba@1413
|
1384 |
InverseMap(const DescriptorMap& _inverted)
|
deba@1413
|
1385 |
: inverted(_inverted) {}
|
deba@1413
|
1386 |
|
deba@1413
|
1387 |
|
deba@1413
|
1388 |
/// The value type of the InverseMap.
|
deba@1413
|
1389 |
typedef typename DescriptorMap::Key Value;
|
deba@1413
|
1390 |
/// The key type of the InverseMap.
|
deba@1413
|
1391 |
typedef typename DescriptorMap::Value Key;
|
deba@1413
|
1392 |
|
deba@1413
|
1393 |
/// \brief Subscript operator.
|
deba@1413
|
1394 |
///
|
deba@1413
|
1395 |
/// Subscript operator. It gives back the item
|
deba@1413
|
1396 |
/// that the descriptor belongs to currently.
|
deba@1413
|
1397 |
Value operator[](const Key& key) const {
|
deba@1413
|
1398 |
return inverted.invMap[key];
|
deba@1413
|
1399 |
}
|
deba@1470
|
1400 |
|
deba@1470
|
1401 |
/// \brief Size of the map.
|
deba@1470
|
1402 |
///
|
deba@1470
|
1403 |
/// Returns the size of the map.
|
deba@1931
|
1404 |
unsigned int size() const {
|
deba@1470
|
1405 |
return inverted.invMap.size();
|
deba@1470
|
1406 |
}
|
deba@1413
|
1407 |
|
deba@1413
|
1408 |
private:
|
deba@1413
|
1409 |
const DescriptorMap& inverted;
|
deba@1413
|
1410 |
};
|
deba@1413
|
1411 |
|
alpar@1402
|
1412 |
/// \brief Gives back the inverse of the map.
|
alpar@1402
|
1413 |
///
|
alpar@1402
|
1414 |
/// Gives back the inverse of the map.
|
alpar@1402
|
1415 |
const InverseMap inverse() const {
|
deba@1413
|
1416 |
return InverseMap(*this);
|
alpar@1402
|
1417 |
}
|
alpar@1402
|
1418 |
};
|
alpar@1402
|
1419 |
|
alpar@1402
|
1420 |
/// \brief Returns the source of the given edge.
|
alpar@1402
|
1421 |
///
|
alpar@1402
|
1422 |
/// The SourceMap gives back the source Node of the given edge.
|
alpar@1402
|
1423 |
/// \author Balazs Dezso
|
alpar@1402
|
1424 |
template <typename Graph>
|
alpar@1402
|
1425 |
class SourceMap {
|
alpar@1402
|
1426 |
public:
|
deba@1419
|
1427 |
|
alpar@1402
|
1428 |
typedef typename Graph::Node Value;
|
alpar@1402
|
1429 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
1430 |
|
alpar@1402
|
1431 |
/// \brief Constructor
|
alpar@1402
|
1432 |
///
|
alpar@1402
|
1433 |
/// Constructor
|
alpar@1402
|
1434 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
1435 |
SourceMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
1436 |
|
alpar@1402
|
1437 |
/// \brief The subscript operator.
|
alpar@1402
|
1438 |
///
|
alpar@1402
|
1439 |
/// The subscript operator.
|
alpar@1402
|
1440 |
/// \param edge The edge
|
alpar@1402
|
1441 |
/// \return The source of the edge
|
deba@1679
|
1442 |
Value operator[](const Key& edge) const {
|
alpar@1402
|
1443 |
return graph.source(edge);
|
alpar@1402
|
1444 |
}
|
alpar@1402
|
1445 |
|
alpar@1402
|
1446 |
private:
|
alpar@1402
|
1447 |
const Graph& graph;
|
alpar@1402
|
1448 |
};
|
alpar@1402
|
1449 |
|
alpar@1402
|
1450 |
/// \brief Returns a \ref SourceMap class
|
alpar@1402
|
1451 |
///
|
alpar@1402
|
1452 |
/// This function just returns an \ref SourceMap class.
|
alpar@1402
|
1453 |
/// \relates SourceMap
|
alpar@1402
|
1454 |
template <typename Graph>
|
alpar@1402
|
1455 |
inline SourceMap<Graph> sourceMap(const Graph& graph) {
|
alpar@1402
|
1456 |
return SourceMap<Graph>(graph);
|
alpar@1402
|
1457 |
}
|
alpar@1402
|
1458 |
|
alpar@1402
|
1459 |
/// \brief Returns the target of the given edge.
|
alpar@1402
|
1460 |
///
|
alpar@1402
|
1461 |
/// The TargetMap gives back the target Node of the given edge.
|
alpar@1402
|
1462 |
/// \author Balazs Dezso
|
alpar@1402
|
1463 |
template <typename Graph>
|
alpar@1402
|
1464 |
class TargetMap {
|
alpar@1402
|
1465 |
public:
|
deba@1419
|
1466 |
|
alpar@1402
|
1467 |
typedef typename Graph::Node Value;
|
alpar@1402
|
1468 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
1469 |
|
alpar@1402
|
1470 |
/// \brief Constructor
|
alpar@1402
|
1471 |
///
|
alpar@1402
|
1472 |
/// Constructor
|
alpar@1402
|
1473 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
1474 |
TargetMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
1475 |
|
alpar@1402
|
1476 |
/// \brief The subscript operator.
|
alpar@1402
|
1477 |
///
|
alpar@1402
|
1478 |
/// The subscript operator.
|
alpar@1536
|
1479 |
/// \param e The edge
|
alpar@1402
|
1480 |
/// \return The target of the edge
|
deba@1679
|
1481 |
Value operator[](const Key& e) const {
|
alpar@1536
|
1482 |
return graph.target(e);
|
alpar@1402
|
1483 |
}
|
alpar@1402
|
1484 |
|
alpar@1402
|
1485 |
private:
|
alpar@1402
|
1486 |
const Graph& graph;
|
alpar@1402
|
1487 |
};
|
alpar@1402
|
1488 |
|
alpar@1402
|
1489 |
/// \brief Returns a \ref TargetMap class
|
deba@1515
|
1490 |
///
|
athos@1540
|
1491 |
/// This function just returns a \ref TargetMap class.
|
alpar@1402
|
1492 |
/// \relates TargetMap
|
alpar@1402
|
1493 |
template <typename Graph>
|
alpar@1402
|
1494 |
inline TargetMap<Graph> targetMap(const Graph& graph) {
|
alpar@1402
|
1495 |
return TargetMap<Graph>(graph);
|
alpar@1402
|
1496 |
}
|
alpar@1402
|
1497 |
|
athos@1540
|
1498 |
/// \brief Returns the "forward" directed edge view of an undirected edge.
|
deba@1419
|
1499 |
///
|
athos@1540
|
1500 |
/// Returns the "forward" directed edge view of an undirected edge.
|
deba@1419
|
1501 |
/// \author Balazs Dezso
|
deba@1419
|
1502 |
template <typename Graph>
|
deba@1419
|
1503 |
class ForwardMap {
|
deba@1419
|
1504 |
public:
|
deba@1419
|
1505 |
|
deba@1419
|
1506 |
typedef typename Graph::Edge Value;
|
klao@1909
|
1507 |
typedef typename Graph::UEdge Key;
|
deba@1419
|
1508 |
|
deba@1419
|
1509 |
/// \brief Constructor
|
deba@1419
|
1510 |
///
|
deba@1419
|
1511 |
/// Constructor
|
deba@1419
|
1512 |
/// \param _graph The graph that the map belongs to.
|
deba@1419
|
1513 |
ForwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
1514 |
|
deba@1419
|
1515 |
/// \brief The subscript operator.
|
deba@1419
|
1516 |
///
|
deba@1419
|
1517 |
/// The subscript operator.
|
deba@1419
|
1518 |
/// \param key An undirected edge
|
deba@1419
|
1519 |
/// \return The "forward" directed edge view of undirected edge
|
deba@1419
|
1520 |
Value operator[](const Key& key) const {
|
deba@1627
|
1521 |
return graph.direct(key, true);
|
deba@1419
|
1522 |
}
|
deba@1419
|
1523 |
|
deba@1419
|
1524 |
private:
|
deba@1419
|
1525 |
const Graph& graph;
|
deba@1419
|
1526 |
};
|
deba@1419
|
1527 |
|
deba@1419
|
1528 |
/// \brief Returns a \ref ForwardMap class
|
deba@1515
|
1529 |
///
|
deba@1419
|
1530 |
/// This function just returns an \ref ForwardMap class.
|
deba@1419
|
1531 |
/// \relates ForwardMap
|
deba@1419
|
1532 |
template <typename Graph>
|
deba@1419
|
1533 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
deba@1419
|
1534 |
return ForwardMap<Graph>(graph);
|
deba@1419
|
1535 |
}
|
deba@1419
|
1536 |
|
athos@1540
|
1537 |
/// \brief Returns the "backward" directed edge view of an undirected edge.
|
deba@1419
|
1538 |
///
|
athos@1540
|
1539 |
/// Returns the "backward" directed edge view of an undirected edge.
|
deba@1419
|
1540 |
/// \author Balazs Dezso
|
deba@1419
|
1541 |
template <typename Graph>
|
deba@1419
|
1542 |
class BackwardMap {
|
deba@1419
|
1543 |
public:
|
deba@1419
|
1544 |
|
deba@1419
|
1545 |
typedef typename Graph::Edge Value;
|
klao@1909
|
1546 |
typedef typename Graph::UEdge Key;
|
deba@1419
|
1547 |
|
deba@1419
|
1548 |
/// \brief Constructor
|
deba@1419
|
1549 |
///
|
deba@1419
|
1550 |
/// Constructor
|
deba@1419
|
1551 |
/// \param _graph The graph that the map belongs to.
|
deba@1419
|
1552 |
BackwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
1553 |
|
deba@1419
|
1554 |
/// \brief The subscript operator.
|
deba@1419
|
1555 |
///
|
deba@1419
|
1556 |
/// The subscript operator.
|
deba@1419
|
1557 |
/// \param key An undirected edge
|
deba@1419
|
1558 |
/// \return The "backward" directed edge view of undirected edge
|
deba@1419
|
1559 |
Value operator[](const Key& key) const {
|
deba@1627
|
1560 |
return graph.direct(key, false);
|
deba@1419
|
1561 |
}
|
deba@1419
|
1562 |
|
deba@1419
|
1563 |
private:
|
deba@1419
|
1564 |
const Graph& graph;
|
deba@1419
|
1565 |
};
|
deba@1419
|
1566 |
|
deba@1419
|
1567 |
/// \brief Returns a \ref BackwardMap class
|
deba@1419
|
1568 |
|
athos@1540
|
1569 |
/// This function just returns a \ref BackwardMap class.
|
deba@1419
|
1570 |
/// \relates BackwardMap
|
deba@1419
|
1571 |
template <typename Graph>
|
deba@1419
|
1572 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
deba@1419
|
1573 |
return BackwardMap<Graph>(graph);
|
deba@1419
|
1574 |
}
|
deba@1419
|
1575 |
|
deba@1695
|
1576 |
/// \brief Potential difference map
|
deba@1695
|
1577 |
///
|
deba@1695
|
1578 |
/// If there is an potential map on the nodes then we
|
deba@1695
|
1579 |
/// can get an edge map as we get the substraction of the
|
deba@1695
|
1580 |
/// values of the target and source.
|
deba@1695
|
1581 |
template <typename Graph, typename NodeMap>
|
deba@1695
|
1582 |
class PotentialDifferenceMap {
|
deba@1515
|
1583 |
public:
|
deba@1695
|
1584 |
typedef typename Graph::Edge Key;
|
deba@1695
|
1585 |
typedef typename NodeMap::Value Value;
|
deba@1695
|
1586 |
|
deba@1695
|
1587 |
/// \brief Constructor
|
deba@1695
|
1588 |
///
|
deba@1695
|
1589 |
/// Contructor of the map
|
deba@1695
|
1590 |
PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
|
deba@1695
|
1591 |
: graph(_graph), potential(_potential) {}
|
deba@1695
|
1592 |
|
deba@1695
|
1593 |
/// \brief Const subscription operator
|
deba@1695
|
1594 |
///
|
deba@1695
|
1595 |
/// Const subscription operator
|
deba@1695
|
1596 |
Value operator[](const Key& edge) const {
|
deba@1695
|
1597 |
return potential[graph.target(edge)] - potential[graph.source(edge)];
|
deba@1695
|
1598 |
}
|
deba@1695
|
1599 |
|
deba@1695
|
1600 |
private:
|
deba@1695
|
1601 |
const Graph& graph;
|
deba@1695
|
1602 |
const NodeMap& potential;
|
deba@1695
|
1603 |
};
|
deba@1695
|
1604 |
|
deba@1695
|
1605 |
/// \brief Just returns a PotentialDifferenceMap
|
deba@1695
|
1606 |
///
|
deba@1695
|
1607 |
/// Just returns a PotentialDifferenceMap
|
deba@1695
|
1608 |
/// \relates PotentialDifferenceMap
|
deba@1695
|
1609 |
template <typename Graph, typename NodeMap>
|
deba@1695
|
1610 |
PotentialDifferenceMap<Graph, NodeMap>
|
deba@1695
|
1611 |
potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
|
deba@1695
|
1612 |
return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
|
deba@1695
|
1613 |
}
|
deba@1695
|
1614 |
|
deba@1515
|
1615 |
/// \brief Map of the node in-degrees.
|
alpar@1453
|
1616 |
///
|
athos@1540
|
1617 |
/// This map returns the in-degree of a node. Once it is constructed,
|
deba@1515
|
1618 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
athos@1540
|
1619 |
/// in constant time. On the other hand, the values are updated automatically
|
deba@1515
|
1620 |
/// whenever the graph changes.
|
deba@1515
|
1621 |
///
|
deba@1729
|
1622 |
/// \warning Besides addNode() and addEdge(), a graph structure may provide
|
deba@1730
|
1623 |
/// alternative ways to modify the graph. The correct behavior of InDegMap
|
deba@1829
|
1624 |
/// is not guarantied if these additional features are used. For example
|
deba@1829
|
1625 |
/// the functions \ref ListGraph::changeSource() "changeSource()",
|
deba@1729
|
1626 |
/// \ref ListGraph::changeTarget() "changeTarget()" and
|
deba@1729
|
1627 |
/// \ref ListGraph::reverseEdge() "reverseEdge()"
|
deba@1729
|
1628 |
/// of \ref ListGraph will \e not update the degree values correctly.
|
deba@1729
|
1629 |
///
|
deba@1515
|
1630 |
/// \sa OutDegMap
|
deba@1515
|
1631 |
|
alpar@1453
|
1632 |
template <typename _Graph>
|
deba@1515
|
1633 |
class InDegMap
|
deba@1999
|
1634 |
: protected ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
1635 |
::ItemNotifier::ObserverBase {
|
deba@1515
|
1636 |
|
alpar@1453
|
1637 |
public:
|
deba@1515
|
1638 |
|
deba@1515
|
1639 |
typedef _Graph Graph;
|
alpar@1453
|
1640 |
typedef int Value;
|
deba@1515
|
1641 |
typedef typename Graph::Node Key;
|
deba@1515
|
1642 |
|
deba@1999
|
1643 |
typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
1644 |
::ItemNotifier::ObserverBase Parent;
|
deba@1999
|
1645 |
|
deba@1515
|
1646 |
private:
|
deba@1515
|
1647 |
|
deba@1990
|
1648 |
class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
|
deba@1515
|
1649 |
public:
|
deba@1515
|
1650 |
|
deba@1990
|
1651 |
typedef DefaultMap<_Graph, Key, int> Parent;
|
deba@2002
|
1652 |
typedef typename Parent::Graph Graph;
|
deba@1515
|
1653 |
|
deba@1515
|
1654 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
1655 |
|
deba@1829
|
1656 |
virtual void add(const Key& key) {
|
deba@1515
|
1657 |
Parent::add(key);
|
deba@1515
|
1658 |
Parent::set(key, 0);
|
deba@1515
|
1659 |
}
|
deba@1931
|
1660 |
|
deba@1829
|
1661 |
virtual void add(const std::vector<Key>& keys) {
|
deba@1829
|
1662 |
Parent::add(keys);
|
deba@1829
|
1663 |
for (int i = 0; i < (int)keys.size(); ++i) {
|
deba@1829
|
1664 |
Parent::set(keys[i], 0);
|
deba@1829
|
1665 |
}
|
deba@1829
|
1666 |
}
|
deba@1515
|
1667 |
};
|
deba@1515
|
1668 |
|
deba@1515
|
1669 |
public:
|
alpar@1453
|
1670 |
|
alpar@1453
|
1671 |
/// \brief Constructor.
|
alpar@1453
|
1672 |
///
|
alpar@1453
|
1673 |
/// Constructor for creating in-degree map.
|
deba@1515
|
1674 |
InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
deba@1999
|
1675 |
Parent::attach(graph.getNotifier(typename _Graph::Edge()));
|
deba@1515
|
1676 |
|
deba@1515
|
1677 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1678 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
1679 |
}
|
alpar@1453
|
1680 |
}
|
alpar@1453
|
1681 |
|
alpar@1459
|
1682 |
/// Gives back the in-degree of a Node.
|
deba@1515
|
1683 |
int operator[](const Key& key) const {
|
deba@1515
|
1684 |
return deg[key];
|
alpar@1459
|
1685 |
}
|
alpar@1453
|
1686 |
|
alpar@1453
|
1687 |
protected:
|
deba@1515
|
1688 |
|
deba@1515
|
1689 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
1690 |
|
deba@1515
|
1691 |
virtual void add(const Edge& edge) {
|
deba@1515
|
1692 |
++deg[graph.target(edge)];
|
alpar@1453
|
1693 |
}
|
alpar@1453
|
1694 |
|
deba@1931
|
1695 |
virtual void add(const std::vector<Edge>& edges) {
|
deba@1931
|
1696 |
for (int i = 0; i < (int)edges.size(); ++i) {
|
deba@1931
|
1697 |
++deg[graph.target(edges[i])];
|
deba@1931
|
1698 |
}
|
deba@1931
|
1699 |
}
|
deba@1931
|
1700 |
|
deba@1515
|
1701 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
1702 |
--deg[graph.target(edge)];
|
deba@1515
|
1703 |
}
|
deba@1515
|
1704 |
|
deba@1931
|
1705 |
virtual void erase(const std::vector<Edge>& edges) {
|
deba@1931
|
1706 |
for (int i = 0; i < (int)edges.size(); ++i) {
|
deba@1931
|
1707 |
--deg[graph.target(edges[i])];
|
deba@1931
|
1708 |
}
|
deba@1931
|
1709 |
}
|
deba@1931
|
1710 |
|
deba@1515
|
1711 |
virtual void build() {
|
deba@1515
|
1712 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1713 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
1714 |
}
|
deba@1515
|
1715 |
}
|
deba@1515
|
1716 |
|
deba@1515
|
1717 |
virtual void clear() {
|
deba@1515
|
1718 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1719 |
deg[it] = 0;
|
deba@1515
|
1720 |
}
|
deba@1515
|
1721 |
}
|
deba@1515
|
1722 |
private:
|
alpar@1506
|
1723 |
|
deba@1515
|
1724 |
const _Graph& graph;
|
deba@1515
|
1725 |
AutoNodeMap deg;
|
alpar@1459
|
1726 |
};
|
alpar@1459
|
1727 |
|
deba@1515
|
1728 |
/// \brief Map of the node out-degrees.
|
deba@1515
|
1729 |
///
|
athos@1540
|
1730 |
/// This map returns the out-degree of a node. Once it is constructed,
|
deba@1515
|
1731 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
athos@1540
|
1732 |
/// in constant time. On the other hand, the values are updated automatically
|
deba@1515
|
1733 |
/// whenever the graph changes.
|
deba@1515
|
1734 |
///
|
deba@1729
|
1735 |
/// \warning Besides addNode() and addEdge(), a graph structure may provide
|
deba@1730
|
1736 |
/// alternative ways to modify the graph. The correct behavior of OutDegMap
|
deba@1829
|
1737 |
/// is not guarantied if these additional features are used. For example
|
deba@1829
|
1738 |
/// the functions \ref ListGraph::changeSource() "changeSource()",
|
deba@1729
|
1739 |
/// \ref ListGraph::changeTarget() "changeTarget()" and
|
deba@1729
|
1740 |
/// \ref ListGraph::reverseEdge() "reverseEdge()"
|
deba@1729
|
1741 |
/// of \ref ListGraph will \e not update the degree values correctly.
|
deba@1729
|
1742 |
///
|
alpar@1555
|
1743 |
/// \sa InDegMap
|
alpar@1459
|
1744 |
|
alpar@1459
|
1745 |
template <typename _Graph>
|
deba@1515
|
1746 |
class OutDegMap
|
deba@1999
|
1747 |
: protected ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
1748 |
::ItemNotifier::ObserverBase {
|
deba@1515
|
1749 |
|
alpar@1459
|
1750 |
public:
|
deba@1999
|
1751 |
|
deba@1999
|
1752 |
typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
|
deba@1999
|
1753 |
::ItemNotifier::ObserverBase Parent;
|
deba@1515
|
1754 |
|
deba@1515
|
1755 |
typedef _Graph Graph;
|
alpar@1459
|
1756 |
typedef int Value;
|
deba@1515
|
1757 |
typedef typename Graph::Node Key;
|
deba@1515
|
1758 |
|
deba@1515
|
1759 |
private:
|
deba@1515
|
1760 |
|
deba@1990
|
1761 |
class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
|
deba@1515
|
1762 |
public:
|
deba@1515
|
1763 |
|
deba@1990
|
1764 |
typedef DefaultMap<_Graph, Key, int> Parent;
|
deba@2002
|
1765 |
typedef typename Parent::Graph Graph;
|
deba@1515
|
1766 |
|
deba@1515
|
1767 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
1768 |
|
deba@1829
|
1769 |
virtual void add(const Key& key) {
|
deba@1515
|
1770 |
Parent::add(key);
|
deba@1515
|
1771 |
Parent::set(key, 0);
|
deba@1515
|
1772 |
}
|
deba@1829
|
1773 |
virtual void add(const std::vector<Key>& keys) {
|
deba@1829
|
1774 |
Parent::add(keys);
|
deba@1829
|
1775 |
for (int i = 0; i < (int)keys.size(); ++i) {
|
deba@1829
|
1776 |
Parent::set(keys[i], 0);
|
deba@1829
|
1777 |
}
|
deba@1829
|
1778 |
}
|
deba@1515
|
1779 |
};
|
deba@1515
|
1780 |
|
deba@1515
|
1781 |
public:
|
alpar@1459
|
1782 |
|
alpar@1459
|
1783 |
/// \brief Constructor.
|
alpar@1459
|
1784 |
///
|
alpar@1459
|
1785 |
/// Constructor for creating out-degree map.
|
deba@1515
|
1786 |
OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
deba@1999
|
1787 |
Parent::attach(graph.getNotifier(typename _Graph::Edge()));
|
deba@1515
|
1788 |
|
deba@1515
|
1789 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1790 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
1791 |
}
|
alpar@1459
|
1792 |
}
|
alpar@1459
|
1793 |
|
deba@1990
|
1794 |
/// Gives back the out-degree of a Node.
|
deba@1515
|
1795 |
int operator[](const Key& key) const {
|
deba@1515
|
1796 |
return deg[key];
|
alpar@1459
|
1797 |
}
|
alpar@1459
|
1798 |
|
alpar@1459
|
1799 |
protected:
|
deba@1515
|
1800 |
|
deba@1515
|
1801 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
1802 |
|
deba@1515
|
1803 |
virtual void add(const Edge& edge) {
|
deba@1515
|
1804 |
++deg[graph.source(edge)];
|
alpar@1459
|
1805 |
}
|
alpar@1459
|
1806 |
|
deba@1931
|
1807 |
virtual void add(const std::vector<Edge>& edges) {
|
deba@1931
|
1808 |
for (int i = 0; i < (int)edges.size(); ++i) {
|
deba@1931
|
1809 |
++deg[graph.source(edges[i])];
|
deba@1931
|
1810 |
}
|
deba@1931
|
1811 |
}
|
deba@1931
|
1812 |
|
deba@1515
|
1813 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
1814 |
--deg[graph.source(edge)];
|
deba@1515
|
1815 |
}
|
deba@1515
|
1816 |
|
deba@1931
|
1817 |
virtual void erase(const std::vector<Edge>& edges) {
|
deba@1931
|
1818 |
for (int i = 0; i < (int)edges.size(); ++i) {
|
deba@1931
|
1819 |
--deg[graph.source(edges[i])];
|
deba@1931
|
1820 |
}
|
deba@1931
|
1821 |
}
|
deba@1931
|
1822 |
|
deba@1515
|
1823 |
virtual void build() {
|
deba@1515
|
1824 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1825 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
1826 |
}
|
deba@1515
|
1827 |
}
|
deba@1515
|
1828 |
|
deba@1515
|
1829 |
virtual void clear() {
|
deba@1515
|
1830 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1831 |
deg[it] = 0;
|
deba@1515
|
1832 |
}
|
deba@1515
|
1833 |
}
|
deba@1515
|
1834 |
private:
|
alpar@1506
|
1835 |
|
deba@1515
|
1836 |
const _Graph& graph;
|
deba@1515
|
1837 |
AutoNodeMap deg;
|
alpar@1453
|
1838 |
};
|
alpar@1453
|
1839 |
|
deba@1695
|
1840 |
|
alpar@1402
|
1841 |
/// @}
|
alpar@1402
|
1842 |
|
alpar@947
|
1843 |
} //END OF NAMESPACE LEMON
|
klao@946
|
1844 |
|
klao@946
|
1845 |
#endif
|