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