klao@946
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
|
klao@946
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
klao@946
|
6 |
*
|
klao@946
|
7 |
* Permission to use, modify and distribute this software is granted
|
klao@946
|
8 |
* provided that this copyright notice appears in all copies. For
|
klao@946
|
9 |
* precise terms see the accompanying LICENSE file.
|
klao@946
|
10 |
*
|
klao@946
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
klao@946
|
12 |
* express or implied, and with no claim as to its suitability for any
|
klao@946
|
13 |
* purpose.
|
klao@946
|
14 |
*
|
klao@946
|
15 |
*/
|
klao@946
|
16 |
|
klao@946
|
17 |
#ifndef LEMON_GRAPH_UTILS_H
|
klao@946
|
18 |
#define LEMON_GRAPH_UTILS_H
|
klao@946
|
19 |
|
klao@946
|
20 |
#include <iterator>
|
deba@1419
|
21 |
#include <vector>
|
alpar@1402
|
22 |
#include <map>
|
klao@946
|
23 |
|
klao@946
|
24 |
#include <lemon/invalid.h>
|
klao@977
|
25 |
#include <lemon/utility.h>
|
deba@1413
|
26 |
#include <lemon/maps.h>
|
alpar@1459
|
27 |
#include <lemon/bits/alteration_notifier.h>
|
klao@946
|
28 |
|
alpar@947
|
29 |
///\ingroup gutils
|
klao@946
|
30 |
///\file
|
alpar@947
|
31 |
///\brief Graph utilities.
|
klao@946
|
32 |
///
|
alpar@964
|
33 |
///\todo Please
|
alpar@964
|
34 |
///revise the documentation.
|
alpar@964
|
35 |
///
|
klao@946
|
36 |
|
klao@946
|
37 |
|
klao@946
|
38 |
namespace lemon {
|
klao@946
|
39 |
|
deba@1267
|
40 |
/// \addtogroup gutils
|
deba@1267
|
41 |
/// @{
|
alpar@947
|
42 |
|
klao@946
|
43 |
/// \brief Function to count the items in the graph.
|
klao@946
|
44 |
///
|
klao@946
|
45 |
/// This function counts the items in the graph.
|
klao@946
|
46 |
/// The complexity of the function is O(n) because
|
klao@946
|
47 |
/// it iterates on all of the items.
|
klao@946
|
48 |
|
klao@946
|
49 |
template <typename Graph, typename ItemIt>
|
klao@977
|
50 |
inline int countItems(const Graph& g) {
|
klao@946
|
51 |
int num = 0;
|
klao@977
|
52 |
for (ItemIt it(g); it != INVALID; ++it) {
|
klao@946
|
53 |
++num;
|
klao@946
|
54 |
}
|
klao@946
|
55 |
return num;
|
klao@946
|
56 |
}
|
klao@946
|
57 |
|
klao@977
|
58 |
// Node counting:
|
klao@977
|
59 |
|
klao@977
|
60 |
template <typename Graph>
|
klao@977
|
61 |
inline
|
klao@977
|
62 |
typename enable_if<typename Graph::NodeNumTag, int>::type
|
klao@977
|
63 |
_countNodes(const Graph &g) {
|
klao@977
|
64 |
return g.nodeNum();
|
klao@977
|
65 |
}
|
klao@977
|
66 |
|
klao@977
|
67 |
template <typename Graph>
|
klao@977
|
68 |
inline int _countNodes(Wrap<Graph> w) {
|
klao@977
|
69 |
return countItems<Graph, typename Graph::NodeIt>(w.value);
|
klao@977
|
70 |
}
|
klao@977
|
71 |
|
klao@946
|
72 |
/// \brief Function to count the nodes in the graph.
|
klao@946
|
73 |
///
|
klao@946
|
74 |
/// This function counts the nodes in the graph.
|
klao@946
|
75 |
/// The complexity of the function is O(n) but for some
|
alpar@964
|
76 |
/// graph structure it is specialized to run in O(1).
|
klao@977
|
77 |
///
|
klao@977
|
78 |
/// \todo refer how to specialize it
|
klao@946
|
79 |
|
klao@946
|
80 |
template <typename Graph>
|
klao@977
|
81 |
inline int countNodes(const Graph& g) {
|
klao@977
|
82 |
return _countNodes<Graph>(g);
|
klao@977
|
83 |
}
|
klao@977
|
84 |
|
klao@977
|
85 |
// Edge counting:
|
klao@977
|
86 |
|
klao@977
|
87 |
template <typename Graph>
|
klao@977
|
88 |
inline
|
klao@977
|
89 |
typename enable_if<typename Graph::EdgeNumTag, int>::type
|
klao@977
|
90 |
_countEdges(const Graph &g) {
|
klao@977
|
91 |
return g.edgeNum();
|
klao@977
|
92 |
}
|
klao@977
|
93 |
|
klao@977
|
94 |
template <typename Graph>
|
klao@977
|
95 |
inline int _countEdges(Wrap<Graph> w) {
|
klao@977
|
96 |
return countItems<Graph, typename Graph::EdgeIt>(w.value);
|
klao@946
|
97 |
}
|
klao@946
|
98 |
|
klao@946
|
99 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
100 |
///
|
klao@946
|
101 |
/// This function counts the edges in the graph.
|
klao@946
|
102 |
/// The complexity of the function is O(e) but for some
|
alpar@964
|
103 |
/// graph structure it is specialized to run in O(1).
|
klao@977
|
104 |
|
klao@946
|
105 |
template <typename Graph>
|
klao@977
|
106 |
inline int countEdges(const Graph& g) {
|
klao@977
|
107 |
return _countEdges<Graph>(g);
|
klao@946
|
108 |
}
|
klao@946
|
109 |
|
klao@1053
|
110 |
// Undirected edge counting:
|
klao@1053
|
111 |
|
klao@1053
|
112 |
template <typename Graph>
|
klao@1053
|
113 |
inline
|
klao@1053
|
114 |
typename enable_if<typename Graph::EdgeNumTag, int>::type
|
klao@1053
|
115 |
_countUndirEdges(const Graph &g) {
|
klao@1053
|
116 |
return g.undirEdgeNum();
|
klao@1053
|
117 |
}
|
klao@1053
|
118 |
|
klao@1053
|
119 |
template <typename Graph>
|
klao@1053
|
120 |
inline int _countUndirEdges(Wrap<Graph> w) {
|
klao@1053
|
121 |
return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
|
klao@1053
|
122 |
}
|
klao@1053
|
123 |
|
klao@1053
|
124 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
125 |
///
|
klao@1053
|
126 |
/// This function counts the edges in the graph.
|
klao@946
|
127 |
/// The complexity of the function is O(e) but for some
|
alpar@964
|
128 |
/// graph structure it is specialized to run in O(1).
|
klao@1053
|
129 |
|
klao@946
|
130 |
template <typename Graph>
|
klao@1053
|
131 |
inline int countUndirEdges(const Graph& g) {
|
klao@1053
|
132 |
return _countUndirEdges<Graph>(g);
|
klao@946
|
133 |
}
|
klao@946
|
134 |
|
klao@977
|
135 |
|
klao@1053
|
136 |
|
klao@946
|
137 |
template <typename Graph, typename DegIt>
|
klao@946
|
138 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
139 |
int num = 0;
|
klao@946
|
140 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
klao@946
|
141 |
++num;
|
klao@946
|
142 |
}
|
klao@946
|
143 |
return num;
|
klao@946
|
144 |
}
|
alpar@967
|
145 |
|
alpar@967
|
146 |
/// Finds an edge between two nodes of a graph.
|
alpar@967
|
147 |
|
alpar@967
|
148 |
/// Finds an edge from node \c u to node \c v in graph \c g.
|
alpar@967
|
149 |
///
|
alpar@967
|
150 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@967
|
151 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@967
|
152 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@967
|
153 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
alpar@967
|
154 |
///
|
alpar@967
|
155 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@967
|
156 |
/// \code
|
alpar@967
|
157 |
/// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
|
alpar@967
|
158 |
/// ...
|
alpar@967
|
159 |
/// }
|
alpar@967
|
160 |
/// \endcode
|
alpar@967
|
161 |
/// \todo We may want to use the \ref concept::GraphBase "GraphBase"
|
alpar@967
|
162 |
/// interface here...
|
alpar@967
|
163 |
/// \bug Untested ...
|
alpar@967
|
164 |
template <typename Graph>
|
alpar@967
|
165 |
typename Graph::Edge findEdge(const Graph &g,
|
deba@1267
|
166 |
typename Graph::Node u, typename Graph::Node v,
|
deba@1267
|
167 |
typename Graph::Edge prev = INVALID)
|
alpar@967
|
168 |
{
|
alpar@967
|
169 |
typename Graph::OutEdgeIt e(g,prev);
|
alpar@1079
|
170 |
// if(prev==INVALID) g.first(e,u);
|
alpar@1079
|
171 |
if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
|
alpar@967
|
172 |
else ++e;
|
alpar@1079
|
173 |
while(e!=INVALID && g.target(e)!=v) ++e;
|
alpar@967
|
174 |
return e;
|
alpar@967
|
175 |
}
|
alpar@964
|
176 |
|
alpar@964
|
177 |
///\e
|
klao@946
|
178 |
|
alpar@964
|
179 |
///\todo Please document.
|
alpar@964
|
180 |
///
|
klao@946
|
181 |
template <typename Graph>
|
klao@946
|
182 |
inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
183 |
return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
|
klao@946
|
184 |
}
|
klao@946
|
185 |
|
alpar@964
|
186 |
///\e
|
alpar@964
|
187 |
|
alpar@964
|
188 |
///\todo Please document.
|
alpar@964
|
189 |
///
|
klao@946
|
190 |
template <typename Graph>
|
klao@946
|
191 |
inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
192 |
return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
|
klao@946
|
193 |
}
|
klao@946
|
194 |
|
klao@946
|
195 |
// graph copy
|
klao@946
|
196 |
|
klao@946
|
197 |
template <
|
klao@946
|
198 |
typename DestinationGraph,
|
klao@946
|
199 |
typename SourceGraph,
|
klao@946
|
200 |
typename NodeBijection>
|
klao@946
|
201 |
void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
202 |
NodeBijection& _nb) {
|
klao@946
|
203 |
for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
|
klao@946
|
204 |
_nb[it] = _d.addNode();
|
klao@946
|
205 |
}
|
klao@946
|
206 |
}
|
klao@946
|
207 |
|
klao@946
|
208 |
template <
|
klao@946
|
209 |
typename DestinationGraph,
|
klao@946
|
210 |
typename SourceGraph,
|
klao@946
|
211 |
typename NodeBijection,
|
klao@946
|
212 |
typename EdgeBijection>
|
klao@946
|
213 |
void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
214 |
const NodeBijection& _nb, EdgeBijection& _eb) {
|
klao@946
|
215 |
for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
|
alpar@986
|
216 |
_eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
|
klao@946
|
217 |
}
|
klao@946
|
218 |
}
|
klao@946
|
219 |
|
klao@946
|
220 |
template <
|
klao@946
|
221 |
typename DestinationGraph,
|
klao@946
|
222 |
typename SourceGraph,
|
klao@946
|
223 |
typename NodeBijection,
|
klao@946
|
224 |
typename EdgeBijection>
|
klao@946
|
225 |
void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
226 |
NodeBijection& _nb, EdgeBijection& _eb) {
|
klao@946
|
227 |
nodeCopy(_d, _s, _nb);
|
klao@946
|
228 |
edgeCopy(_d, _s, _nb, _eb);
|
klao@946
|
229 |
}
|
klao@946
|
230 |
|
deba@1267
|
231 |
template <
|
klao@946
|
232 |
typename _DestinationGraph,
|
klao@946
|
233 |
typename _SourceGraph,
|
klao@946
|
234 |
typename _NodeBijection
|
klao@946
|
235 |
=typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
|
klao@946
|
236 |
typename _EdgeBijection
|
deba@1267
|
237 |
= typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
|
deba@1267
|
238 |
>
|
deba@1267
|
239 |
class GraphCopy {
|
deba@1267
|
240 |
public:
|
deba@1267
|
241 |
|
deba@1267
|
242 |
typedef _DestinationGraph DestinationGraph;
|
deba@1267
|
243 |
typedef _SourceGraph SourceGraph;
|
klao@946
|
244 |
|
deba@1267
|
245 |
typedef _NodeBijection NodeBijection;
|
deba@1267
|
246 |
typedef _EdgeBijection EdgeBijection;
|
deba@1267
|
247 |
|
deba@1267
|
248 |
protected:
|
deba@1267
|
249 |
|
deba@1267
|
250 |
NodeBijection node_bijection;
|
deba@1267
|
251 |
EdgeBijection edge_bijection;
|
klao@946
|
252 |
|
deba@1267
|
253 |
public:
|
deba@1267
|
254 |
|
deba@1267
|
255 |
GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
|
deba@1267
|
256 |
copyGraph(_d, _s, node_bijection, edge_bijection);
|
deba@1267
|
257 |
}
|
deba@1267
|
258 |
|
deba@1267
|
259 |
const NodeBijection& getNodeBijection() const {
|
deba@1267
|
260 |
return node_bijection;
|
deba@1267
|
261 |
}
|
klao@946
|
262 |
|
deba@1267
|
263 |
const EdgeBijection& getEdgeBijection() const {
|
deba@1267
|
264 |
return edge_bijection;
|
deba@1267
|
265 |
}
|
deba@1267
|
266 |
|
deba@1267
|
267 |
};
|
klao@946
|
268 |
|
klao@946
|
269 |
|
deba@1267
|
270 |
template <typename _Graph, typename _Item>
|
deba@1419
|
271 |
class ItemSetTraits {};
|
deba@1192
|
272 |
|
deba@1192
|
273 |
template <typename _Graph>
|
deba@1267
|
274 |
class ItemSetTraits<_Graph, typename _Graph::Node> {
|
deba@1192
|
275 |
public:
|
deba@1192
|
276 |
|
deba@1192
|
277 |
typedef _Graph Graph;
|
alpar@947
|
278 |
|
deba@1192
|
279 |
typedef typename Graph::Node Item;
|
deba@1192
|
280 |
typedef typename Graph::NodeIt ItemIt;
|
deba@1192
|
281 |
|
deba@1192
|
282 |
template <typename _Value>
|
deba@1192
|
283 |
class Map : public Graph::template NodeMap<_Value> {
|
deba@1192
|
284 |
public:
|
deba@1192
|
285 |
typedef typename Graph::template NodeMap<_Value> Parent;
|
deba@1192
|
286 |
typedef typename Parent::Value Value;
|
deba@1192
|
287 |
|
deba@1192
|
288 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1192
|
289 |
Map(const Graph& _graph, const Value& _value)
|
deba@1192
|
290 |
: Parent(_graph, _value) {}
|
deba@1192
|
291 |
};
|
deba@1192
|
292 |
|
deba@1192
|
293 |
};
|
deba@1192
|
294 |
|
deba@1192
|
295 |
template <typename _Graph>
|
deba@1267
|
296 |
class ItemSetTraits<_Graph, typename _Graph::Edge> {
|
deba@1192
|
297 |
public:
|
deba@1192
|
298 |
|
deba@1192
|
299 |
typedef _Graph Graph;
|
deba@1192
|
300 |
|
deba@1192
|
301 |
typedef typename Graph::Edge Item;
|
deba@1192
|
302 |
typedef typename Graph::EdgeIt ItemIt;
|
deba@1192
|
303 |
|
deba@1192
|
304 |
template <typename _Value>
|
deba@1192
|
305 |
class Map : public Graph::template EdgeMap<_Value> {
|
deba@1192
|
306 |
public:
|
deba@1192
|
307 |
typedef typename Graph::template EdgeMap<_Value> Parent;
|
deba@1192
|
308 |
typedef typename Parent::Value Value;
|
deba@1192
|
309 |
|
deba@1192
|
310 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1192
|
311 |
Map(const Graph& _graph, const Value& _value)
|
deba@1192
|
312 |
: Parent(_graph, _value) {}
|
deba@1192
|
313 |
};
|
deba@1192
|
314 |
|
deba@1192
|
315 |
};
|
deba@1192
|
316 |
|
deba@1267
|
317 |
template <typename _Graph>
|
deba@1267
|
318 |
class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
|
deba@1267
|
319 |
public:
|
deba@1267
|
320 |
|
deba@1267
|
321 |
typedef _Graph Graph;
|
deba@1267
|
322 |
|
deba@1267
|
323 |
typedef typename Graph::UndirEdge Item;
|
deba@1267
|
324 |
typedef typename Graph::UndirEdgeIt ItemIt;
|
deba@1267
|
325 |
|
deba@1267
|
326 |
template <typename _Value>
|
deba@1267
|
327 |
class Map : public Graph::template UndirEdgeMap<_Value> {
|
deba@1267
|
328 |
public:
|
deba@1267
|
329 |
typedef typename Graph::template UndirEdgeMap<_Value> Parent;
|
deba@1267
|
330 |
typedef typename Parent::Value Value;
|
deba@1267
|
331 |
|
deba@1267
|
332 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1267
|
333 |
Map(const Graph& _graph, const Value& _value)
|
deba@1267
|
334 |
: Parent(_graph, _value) {}
|
deba@1267
|
335 |
};
|
deba@1267
|
336 |
|
deba@1267
|
337 |
};
|
deba@1192
|
338 |
|
deba@1192
|
339 |
/// @}
|
alpar@1402
|
340 |
|
alpar@1402
|
341 |
/// \addtogroup graph_maps
|
alpar@1402
|
342 |
/// @{
|
alpar@1402
|
343 |
|
alpar@1402
|
344 |
template <typename Map, typename Enable = void>
|
alpar@1402
|
345 |
struct ReferenceMapTraits {
|
alpar@1402
|
346 |
typedef typename Map::Value Value;
|
alpar@1402
|
347 |
typedef typename Map::Value& Reference;
|
alpar@1402
|
348 |
typedef const typename Map::Value& ConstReference;
|
alpar@1402
|
349 |
typedef typename Map::Value* Pointer;
|
alpar@1402
|
350 |
typedef const typename Map::Value* ConstPointer;
|
alpar@1402
|
351 |
};
|
alpar@1402
|
352 |
|
alpar@1402
|
353 |
template <typename Map>
|
alpar@1402
|
354 |
struct ReferenceMapTraits<
|
alpar@1402
|
355 |
Map,
|
alpar@1402
|
356 |
typename enable_if<typename Map::FullTypeTag, void>::type
|
alpar@1402
|
357 |
> {
|
alpar@1402
|
358 |
typedef typename Map::Value Value;
|
alpar@1402
|
359 |
typedef typename Map::Reference Reference;
|
alpar@1402
|
360 |
typedef typename Map::ConstReference ConstReference;
|
alpar@1402
|
361 |
typedef typename Map::Pointer Pointer;
|
alpar@1402
|
362 |
typedef typename Map::ConstPointer ConstPointer;
|
alpar@1402
|
363 |
};
|
alpar@1402
|
364 |
|
deba@1413
|
365 |
/// Provides an immutable and unique id for each item in the graph.
|
deba@1413
|
366 |
|
deba@1413
|
367 |
/// The IdMap class provides an unique and immutable mapping for each item
|
deba@1413
|
368 |
/// in the graph.
|
deba@1413
|
369 |
///
|
deba@1413
|
370 |
template <typename _Graph, typename _Item>
|
deba@1413
|
371 |
class IdMap {
|
deba@1413
|
372 |
public:
|
deba@1413
|
373 |
typedef _Graph Graph;
|
deba@1413
|
374 |
typedef int Value;
|
deba@1413
|
375 |
typedef _Item Item;
|
deba@1413
|
376 |
typedef _Item Key;
|
deba@1413
|
377 |
|
deba@1419
|
378 |
typedef True NeedCopy;
|
deba@1419
|
379 |
|
deba@1413
|
380 |
/// \brief Constructor.
|
deba@1413
|
381 |
///
|
deba@1413
|
382 |
/// Constructor for creating id map.
|
deba@1413
|
383 |
IdMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
384 |
|
deba@1413
|
385 |
/// \brief Gives back the \e id of the item.
|
deba@1413
|
386 |
///
|
deba@1413
|
387 |
/// Gives back the immutable and unique \e id of the map.
|
deba@1413
|
388 |
int operator[](const Item& item) const { return graph->id(item);}
|
deba@1413
|
389 |
|
deba@1413
|
390 |
|
deba@1413
|
391 |
private:
|
deba@1413
|
392 |
const Graph* graph;
|
deba@1413
|
393 |
|
deba@1413
|
394 |
public:
|
deba@1413
|
395 |
|
deba@1413
|
396 |
/// \brief The class represents the inverse of the map.
|
deba@1413
|
397 |
///
|
deba@1413
|
398 |
/// The class represents the inverse of the map.
|
deba@1413
|
399 |
/// \see inverse()
|
deba@1413
|
400 |
class InverseMap {
|
deba@1413
|
401 |
public:
|
deba@1419
|
402 |
|
deba@1419
|
403 |
typedef True NeedCopy;
|
deba@1419
|
404 |
|
deba@1413
|
405 |
/// \brief Constructor.
|
deba@1413
|
406 |
///
|
deba@1413
|
407 |
/// Constructor for creating an id-to-item map.
|
deba@1413
|
408 |
InverseMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
409 |
|
deba@1413
|
410 |
/// \brief Constructor.
|
deba@1413
|
411 |
///
|
deba@1413
|
412 |
/// Constructor for creating an id-to-item map.
|
deba@1413
|
413 |
InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
|
deba@1413
|
414 |
|
deba@1413
|
415 |
/// \brief Gives back the given item from its id.
|
deba@1413
|
416 |
///
|
deba@1413
|
417 |
/// Gives back the given item from its id.
|
deba@1413
|
418 |
///
|
deba@1413
|
419 |
Item operator[](int id) const { return graph->fromId(id, Item());}
|
deba@1413
|
420 |
private:
|
deba@1413
|
421 |
const Graph* graph;
|
deba@1413
|
422 |
};
|
deba@1413
|
423 |
|
deba@1413
|
424 |
/// \brief Gives back the inverse of the map.
|
deba@1413
|
425 |
///
|
deba@1413
|
426 |
/// Gives back the inverse of the map.
|
deba@1413
|
427 |
InverseMap inverse() const { return InverseMap(*graph);}
|
deba@1413
|
428 |
|
deba@1413
|
429 |
};
|
deba@1413
|
430 |
|
deba@1413
|
431 |
|
alpar@1402
|
432 |
/// \brief General inversable graph-map type.
|
alpar@1402
|
433 |
|
alpar@1402
|
434 |
/// This type provides simple inversable map functions.
|
alpar@1402
|
435 |
/// The InversableMap wraps an arbitrary ReadWriteMap
|
alpar@1402
|
436 |
/// and if a key is setted to a new value then store it
|
alpar@1402
|
437 |
/// in the inverse map.
|
alpar@1402
|
438 |
/// \param _Graph The graph type.
|
alpar@1402
|
439 |
/// \param _Map The map to extend with inversable functionality.
|
alpar@1402
|
440 |
template <
|
alpar@1402
|
441 |
typename _Graph,
|
alpar@1402
|
442 |
typename _Item,
|
alpar@1402
|
443 |
typename _Value,
|
alpar@1402
|
444 |
typename _Map
|
deba@1413
|
445 |
= typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
|
alpar@1402
|
446 |
>
|
deba@1413
|
447 |
class InvertableMap : protected _Map {
|
alpar@1402
|
448 |
|
alpar@1402
|
449 |
public:
|
alpar@1402
|
450 |
|
alpar@1402
|
451 |
typedef _Map Map;
|
alpar@1402
|
452 |
typedef _Graph Graph;
|
deba@1413
|
453 |
|
deba@1413
|
454 |
/// The key type of InvertableMap (Node, Edge, UndirEdge).
|
alpar@1402
|
455 |
typedef typename _Map::Key Key;
|
deba@1413
|
456 |
/// The value type of the InvertableMap.
|
alpar@1402
|
457 |
typedef typename _Map::Value Value;
|
alpar@1402
|
458 |
|
alpar@1402
|
459 |
/// \brief Constructor.
|
alpar@1402
|
460 |
///
|
deba@1413
|
461 |
/// Construct a new InvertableMap for the graph.
|
alpar@1402
|
462 |
///
|
deba@1413
|
463 |
InvertableMap(const Graph& graph) : Map(graph) {}
|
alpar@1402
|
464 |
|
alpar@1402
|
465 |
/// \brief The setter function of the map.
|
alpar@1402
|
466 |
///
|
deba@1413
|
467 |
/// Sets the mapped value.
|
alpar@1402
|
468 |
void set(const Key& key, const Value& val) {
|
alpar@1402
|
469 |
Value oldval = Map::operator[](key);
|
deba@1413
|
470 |
typename Container::iterator it = invMap.find(oldval);
|
alpar@1402
|
471 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
472 |
invMap.erase(it);
|
alpar@1402
|
473 |
}
|
alpar@1402
|
474 |
invMap.insert(make_pair(val, key));
|
alpar@1402
|
475 |
Map::set(key, val);
|
alpar@1402
|
476 |
}
|
alpar@1402
|
477 |
|
alpar@1402
|
478 |
/// \brief The getter function of the map.
|
alpar@1402
|
479 |
///
|
alpar@1402
|
480 |
/// It gives back the value associated with the key.
|
deba@1413
|
481 |
const Value operator[](const Key& key) const {
|
alpar@1402
|
482 |
return Map::operator[](key);
|
alpar@1402
|
483 |
}
|
alpar@1402
|
484 |
|
deba@1515
|
485 |
protected:
|
deba@1515
|
486 |
|
alpar@1402
|
487 |
/// \brief Add a new key to the map.
|
alpar@1402
|
488 |
///
|
alpar@1402
|
489 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
490 |
/// \c AlterationNotifier.
|
alpar@1402
|
491 |
virtual void add(const Key& key) {
|
alpar@1402
|
492 |
Map::add(key);
|
alpar@1402
|
493 |
}
|
alpar@1402
|
494 |
|
alpar@1402
|
495 |
/// \brief Erase the key from the map.
|
alpar@1402
|
496 |
///
|
alpar@1402
|
497 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
498 |
/// \c AlterationNotifier.
|
alpar@1402
|
499 |
virtual void erase(const Key& key) {
|
alpar@1402
|
500 |
Value val = Map::operator[](key);
|
deba@1413
|
501 |
typename Container::iterator it = invMap.find(val);
|
alpar@1402
|
502 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
503 |
invMap.erase(it);
|
alpar@1402
|
504 |
}
|
alpar@1402
|
505 |
Map::erase(key);
|
alpar@1402
|
506 |
}
|
alpar@1402
|
507 |
|
alpar@1402
|
508 |
/// \brief Clear the keys from the map and inverse map.
|
alpar@1402
|
509 |
///
|
alpar@1402
|
510 |
/// Clear the keys from the map and inverse map. It is called by the
|
alpar@1402
|
511 |
/// \c AlterationNotifier.
|
alpar@1402
|
512 |
virtual void clear() {
|
alpar@1402
|
513 |
invMap.clear();
|
alpar@1402
|
514 |
Map::clear();
|
alpar@1402
|
515 |
}
|
alpar@1402
|
516 |
|
deba@1413
|
517 |
private:
|
deba@1413
|
518 |
|
deba@1413
|
519 |
typedef std::map<Value, Key> Container;
|
deba@1413
|
520 |
Container invMap;
|
deba@1413
|
521 |
|
deba@1413
|
522 |
public:
|
deba@1413
|
523 |
|
deba@1413
|
524 |
/// \brief The inverse map type.
|
deba@1413
|
525 |
///
|
deba@1413
|
526 |
/// The inverse of this map. The subscript operator of the map
|
deba@1413
|
527 |
/// gives back always the item what was last assigned to the value.
|
deba@1413
|
528 |
class InverseMap {
|
deba@1413
|
529 |
public:
|
deba@1413
|
530 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
531 |
///
|
deba@1413
|
532 |
/// Constructor of the InverseMap.
|
deba@1413
|
533 |
InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
|
deba@1413
|
534 |
|
deba@1413
|
535 |
/// The value type of the InverseMap.
|
deba@1413
|
536 |
typedef typename InvertableMap::Key Value;
|
deba@1413
|
537 |
/// The key type of the InverseMap.
|
deba@1413
|
538 |
typedef typename InvertableMap::Value Key;
|
deba@1413
|
539 |
|
deba@1413
|
540 |
/// \brief Subscript operator.
|
deba@1413
|
541 |
///
|
deba@1413
|
542 |
/// Subscript operator. It gives back always the item
|
deba@1413
|
543 |
/// what was last assigned to the value.
|
deba@1413
|
544 |
Value operator[](const Key& key) const {
|
deba@1413
|
545 |
typename Container::const_iterator it = inverted.invMap.find(key);
|
deba@1413
|
546 |
return it->second;
|
deba@1413
|
547 |
}
|
deba@1413
|
548 |
|
deba@1413
|
549 |
private:
|
deba@1413
|
550 |
const InvertableMap& inverted;
|
deba@1413
|
551 |
};
|
deba@1413
|
552 |
|
alpar@1402
|
553 |
/// \brief It gives back the just readeable inverse map.
|
alpar@1402
|
554 |
///
|
alpar@1402
|
555 |
/// It gives back the just readeable inverse map.
|
deba@1413
|
556 |
InverseMap inverse() const {
|
deba@1413
|
557 |
return InverseMap(*this);
|
alpar@1402
|
558 |
}
|
alpar@1402
|
559 |
|
alpar@1402
|
560 |
|
deba@1413
|
561 |
|
alpar@1402
|
562 |
};
|
alpar@1402
|
563 |
|
alpar@1402
|
564 |
/// \brief Provides a mutable, continuous and unique descriptor for each
|
alpar@1402
|
565 |
/// item in the graph.
|
alpar@1402
|
566 |
///
|
alpar@1402
|
567 |
/// The DescriptorMap class provides a mutable, continuous and immutable
|
deba@1413
|
568 |
/// mapping for each item in the graph. The value for an item may mutated
|
deba@1413
|
569 |
/// on each operation when the an item erased or added to graph.
|
alpar@1402
|
570 |
///
|
alpar@1402
|
571 |
/// \param _Graph The graph class the \c DescriptorMap belongs to.
|
alpar@1402
|
572 |
/// \param _Item The Item is the Key of the Map. It may be Node, Edge or
|
alpar@1402
|
573 |
/// UndirEdge.
|
alpar@1402
|
574 |
/// \param _Map A ReadWriteMap mapping from the item type to integer.
|
alpar@1402
|
575 |
template <
|
alpar@1402
|
576 |
typename _Graph,
|
alpar@1402
|
577 |
typename _Item,
|
deba@1413
|
578 |
typename _Map
|
deba@1413
|
579 |
= typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
|
alpar@1402
|
580 |
>
|
alpar@1402
|
581 |
class DescriptorMap : protected _Map {
|
alpar@1402
|
582 |
|
alpar@1402
|
583 |
typedef _Item Item;
|
alpar@1402
|
584 |
typedef _Map Map;
|
alpar@1402
|
585 |
|
alpar@1402
|
586 |
public:
|
alpar@1402
|
587 |
/// The graph class of DescriptorMap.
|
alpar@1402
|
588 |
typedef _Graph Graph;
|
alpar@1402
|
589 |
|
alpar@1402
|
590 |
/// The key type of DescriptorMap (Node, Edge, UndirEdge).
|
alpar@1402
|
591 |
typedef typename _Map::Key Key;
|
alpar@1402
|
592 |
/// The value type of DescriptorMap.
|
alpar@1402
|
593 |
typedef typename _Map::Value Value;
|
alpar@1402
|
594 |
|
alpar@1402
|
595 |
/// \brief Constructor.
|
alpar@1402
|
596 |
///
|
deba@1413
|
597 |
/// Constructor for descriptor map.
|
alpar@1402
|
598 |
DescriptorMap(const Graph& _graph) : Map(_graph) {
|
alpar@1402
|
599 |
build();
|
alpar@1402
|
600 |
}
|
alpar@1402
|
601 |
|
deba@1515
|
602 |
protected:
|
deba@1515
|
603 |
|
alpar@1402
|
604 |
/// \brief Add a new key to the map.
|
alpar@1402
|
605 |
///
|
alpar@1402
|
606 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
607 |
/// \c AlterationNotifier.
|
alpar@1402
|
608 |
virtual void add(const Item& item) {
|
alpar@1402
|
609 |
Map::add(item);
|
alpar@1402
|
610 |
Map::set(item, invMap.size());
|
alpar@1402
|
611 |
invMap.push_back(item);
|
alpar@1402
|
612 |
}
|
alpar@1402
|
613 |
|
alpar@1402
|
614 |
/// \brief Erase the key from the map.
|
alpar@1402
|
615 |
///
|
alpar@1402
|
616 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
617 |
/// \c AlterationNotifier.
|
alpar@1402
|
618 |
virtual void erase(const Item& item) {
|
alpar@1402
|
619 |
Map::set(invMap.back(), Map::operator[](item));
|
alpar@1402
|
620 |
invMap[Map::operator[](item)] = invMap.back();
|
deba@1413
|
621 |
invMap.pop_back();
|
alpar@1402
|
622 |
Map::erase(item);
|
alpar@1402
|
623 |
}
|
alpar@1402
|
624 |
|
alpar@1402
|
625 |
/// \brief Build the unique map.
|
alpar@1402
|
626 |
///
|
alpar@1402
|
627 |
/// Build the unique map. It is called by the
|
alpar@1402
|
628 |
/// \c AlterationNotifier.
|
alpar@1402
|
629 |
virtual void build() {
|
alpar@1402
|
630 |
Map::build();
|
alpar@1402
|
631 |
Item it;
|
alpar@1402
|
632 |
const typename Map::Graph* graph = Map::getGraph();
|
alpar@1402
|
633 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
alpar@1402
|
634 |
Map::set(it, invMap.size());
|
alpar@1402
|
635 |
invMap.push_back(it);
|
alpar@1402
|
636 |
}
|
alpar@1402
|
637 |
}
|
alpar@1402
|
638 |
|
alpar@1402
|
639 |
/// \brief Clear the keys from the map.
|
alpar@1402
|
640 |
///
|
alpar@1402
|
641 |
/// Clear the keys from the map. It is called by the
|
alpar@1402
|
642 |
/// \c AlterationNotifier.
|
alpar@1402
|
643 |
virtual void clear() {
|
alpar@1402
|
644 |
invMap.clear();
|
alpar@1402
|
645 |
Map::clear();
|
alpar@1402
|
646 |
}
|
alpar@1402
|
647 |
|
alpar@1402
|
648 |
/// \brief Gives back the \e descriptor of the item.
|
alpar@1402
|
649 |
///
|
alpar@1402
|
650 |
/// Gives back the mutable and unique \e descriptor of the map.
|
alpar@1402
|
651 |
int operator[](const Item& item) const {
|
alpar@1402
|
652 |
return Map::operator[](item);
|
alpar@1402
|
653 |
}
|
alpar@1402
|
654 |
|
deba@1413
|
655 |
private:
|
deba@1413
|
656 |
|
deba@1413
|
657 |
typedef std::vector<Item> Container;
|
deba@1413
|
658 |
Container invMap;
|
deba@1413
|
659 |
|
deba@1413
|
660 |
public:
|
deba@1413
|
661 |
/// \brief The inverse map type.
|
deba@1413
|
662 |
///
|
deba@1413
|
663 |
/// The inverse map type.
|
deba@1413
|
664 |
class InverseMap {
|
deba@1413
|
665 |
public:
|
deba@1413
|
666 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
667 |
///
|
deba@1413
|
668 |
/// Constructor of the InverseMap.
|
deba@1413
|
669 |
InverseMap(const DescriptorMap& _inverted)
|
deba@1413
|
670 |
: inverted(_inverted) {}
|
deba@1413
|
671 |
|
deba@1413
|
672 |
|
deba@1413
|
673 |
/// The value type of the InverseMap.
|
deba@1413
|
674 |
typedef typename DescriptorMap::Key Value;
|
deba@1413
|
675 |
/// The key type of the InverseMap.
|
deba@1413
|
676 |
typedef typename DescriptorMap::Value Key;
|
deba@1413
|
677 |
|
deba@1413
|
678 |
/// \brief Subscript operator.
|
deba@1413
|
679 |
///
|
deba@1413
|
680 |
/// Subscript operator. It gives back the item
|
deba@1413
|
681 |
/// that the descriptor belongs to currently.
|
deba@1413
|
682 |
Value operator[](const Key& key) const {
|
deba@1413
|
683 |
return inverted.invMap[key];
|
deba@1413
|
684 |
}
|
deba@1470
|
685 |
|
deba@1470
|
686 |
/// \brief Size of the map.
|
deba@1470
|
687 |
///
|
deba@1470
|
688 |
/// Returns the size of the map.
|
deba@1470
|
689 |
unsigned size() const {
|
deba@1470
|
690 |
return inverted.invMap.size();
|
deba@1470
|
691 |
}
|
deba@1413
|
692 |
|
deba@1413
|
693 |
private:
|
deba@1413
|
694 |
const DescriptorMap& inverted;
|
deba@1413
|
695 |
};
|
deba@1413
|
696 |
|
alpar@1402
|
697 |
/// \brief Gives back the inverse of the map.
|
alpar@1402
|
698 |
///
|
alpar@1402
|
699 |
/// Gives back the inverse of the map.
|
alpar@1402
|
700 |
const InverseMap inverse() const {
|
deba@1413
|
701 |
return InverseMap(*this);
|
alpar@1402
|
702 |
}
|
alpar@1402
|
703 |
};
|
alpar@1402
|
704 |
|
alpar@1402
|
705 |
/// \brief Returns the source of the given edge.
|
alpar@1402
|
706 |
///
|
alpar@1402
|
707 |
/// The SourceMap gives back the source Node of the given edge.
|
alpar@1402
|
708 |
/// \author Balazs Dezso
|
alpar@1402
|
709 |
template <typename Graph>
|
alpar@1402
|
710 |
class SourceMap {
|
alpar@1402
|
711 |
public:
|
deba@1419
|
712 |
|
deba@1419
|
713 |
typedef True NeedCopy;
|
deba@1419
|
714 |
|
alpar@1402
|
715 |
typedef typename Graph::Node Value;
|
alpar@1402
|
716 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
717 |
|
alpar@1402
|
718 |
/// \brief Constructor
|
alpar@1402
|
719 |
///
|
alpar@1402
|
720 |
/// Constructor
|
alpar@1402
|
721 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
722 |
SourceMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
723 |
|
alpar@1402
|
724 |
/// \brief The subscript operator.
|
alpar@1402
|
725 |
///
|
alpar@1402
|
726 |
/// The subscript operator.
|
alpar@1402
|
727 |
/// \param edge The edge
|
alpar@1402
|
728 |
/// \return The source of the edge
|
alpar@1402
|
729 |
Value operator[](const Key& edge) {
|
alpar@1402
|
730 |
return graph.source(edge);
|
alpar@1402
|
731 |
}
|
alpar@1402
|
732 |
|
alpar@1402
|
733 |
private:
|
alpar@1402
|
734 |
const Graph& graph;
|
alpar@1402
|
735 |
};
|
alpar@1402
|
736 |
|
alpar@1402
|
737 |
/// \brief Returns a \ref SourceMap class
|
alpar@1402
|
738 |
///
|
alpar@1402
|
739 |
/// This function just returns an \ref SourceMap class.
|
alpar@1402
|
740 |
/// \relates SourceMap
|
alpar@1402
|
741 |
template <typename Graph>
|
alpar@1402
|
742 |
inline SourceMap<Graph> sourceMap(const Graph& graph) {
|
alpar@1402
|
743 |
return SourceMap<Graph>(graph);
|
alpar@1402
|
744 |
}
|
alpar@1402
|
745 |
|
alpar@1402
|
746 |
/// \brief Returns the target of the given edge.
|
alpar@1402
|
747 |
///
|
alpar@1402
|
748 |
/// The TargetMap gives back the target Node of the given edge.
|
alpar@1402
|
749 |
/// \author Balazs Dezso
|
alpar@1402
|
750 |
template <typename Graph>
|
alpar@1402
|
751 |
class TargetMap {
|
alpar@1402
|
752 |
public:
|
deba@1419
|
753 |
|
deba@1419
|
754 |
typedef True NeedCopy;
|
deba@1419
|
755 |
|
alpar@1402
|
756 |
typedef typename Graph::Node Value;
|
alpar@1402
|
757 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
758 |
|
alpar@1402
|
759 |
/// \brief Constructor
|
alpar@1402
|
760 |
///
|
alpar@1402
|
761 |
/// Constructor
|
alpar@1402
|
762 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
763 |
TargetMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
764 |
|
alpar@1402
|
765 |
/// \brief The subscript operator.
|
alpar@1402
|
766 |
///
|
alpar@1402
|
767 |
/// The subscript operator.
|
alpar@1402
|
768 |
/// \param edge The edge
|
alpar@1402
|
769 |
/// \return The target of the edge
|
alpar@1402
|
770 |
Value operator[](const Key& key) {
|
alpar@1402
|
771 |
return graph.target(key);
|
alpar@1402
|
772 |
}
|
alpar@1402
|
773 |
|
alpar@1402
|
774 |
private:
|
alpar@1402
|
775 |
const Graph& graph;
|
alpar@1402
|
776 |
};
|
alpar@1402
|
777 |
|
alpar@1402
|
778 |
/// \brief Returns a \ref TargetMap class
|
deba@1515
|
779 |
///
|
alpar@1402
|
780 |
/// This function just returns an \ref TargetMap class.
|
alpar@1402
|
781 |
/// \relates TargetMap
|
alpar@1402
|
782 |
template <typename Graph>
|
alpar@1402
|
783 |
inline TargetMap<Graph> targetMap(const Graph& graph) {
|
alpar@1402
|
784 |
return TargetMap<Graph>(graph);
|
alpar@1402
|
785 |
}
|
alpar@1402
|
786 |
|
deba@1419
|
787 |
/// \brief Returns the "forward" directed edge view of undirected edge.
|
deba@1419
|
788 |
///
|
deba@1419
|
789 |
/// Returns the "forward" directed edge view of undirected edge.
|
deba@1419
|
790 |
/// \author Balazs Dezso
|
deba@1419
|
791 |
template <typename Graph>
|
deba@1419
|
792 |
class ForwardMap {
|
deba@1419
|
793 |
public:
|
deba@1419
|
794 |
|
deba@1419
|
795 |
typedef True NeedCopy;
|
deba@1419
|
796 |
|
deba@1419
|
797 |
typedef typename Graph::Edge Value;
|
deba@1419
|
798 |
typedef typename Graph::UndirEdge Key;
|
deba@1419
|
799 |
|
deba@1419
|
800 |
/// \brief Constructor
|
deba@1419
|
801 |
///
|
deba@1419
|
802 |
/// Constructor
|
deba@1419
|
803 |
/// \param _graph The graph that the map belongs to.
|
deba@1419
|
804 |
ForwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
805 |
|
deba@1419
|
806 |
/// \brief The subscript operator.
|
deba@1419
|
807 |
///
|
deba@1419
|
808 |
/// The subscript operator.
|
deba@1419
|
809 |
/// \param key An undirected edge
|
deba@1419
|
810 |
/// \return The "forward" directed edge view of undirected edge
|
deba@1419
|
811 |
Value operator[](const Key& key) const {
|
deba@1419
|
812 |
return graph.edgeWithSource(key, graph.source(key));
|
deba@1419
|
813 |
}
|
deba@1419
|
814 |
|
deba@1419
|
815 |
private:
|
deba@1419
|
816 |
const Graph& graph;
|
deba@1419
|
817 |
};
|
deba@1419
|
818 |
|
deba@1419
|
819 |
/// \brief Returns a \ref ForwardMap class
|
deba@1515
|
820 |
///
|
deba@1419
|
821 |
/// This function just returns an \ref ForwardMap class.
|
deba@1419
|
822 |
/// \relates ForwardMap
|
deba@1419
|
823 |
template <typename Graph>
|
deba@1419
|
824 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
deba@1419
|
825 |
return ForwardMap<Graph>(graph);
|
deba@1419
|
826 |
}
|
deba@1419
|
827 |
|
deba@1419
|
828 |
/// \brief Returns the "backward" directed edge view of undirected edge.
|
deba@1419
|
829 |
///
|
deba@1419
|
830 |
/// Returns the "backward" directed edge view of undirected edge.
|
deba@1419
|
831 |
/// \author Balazs Dezso
|
deba@1419
|
832 |
template <typename Graph>
|
deba@1419
|
833 |
class BackwardMap {
|
deba@1419
|
834 |
public:
|
deba@1419
|
835 |
typedef True NeedCopy;
|
deba@1419
|
836 |
|
deba@1419
|
837 |
typedef typename Graph::Edge Value;
|
deba@1419
|
838 |
typedef typename Graph::UndirEdge Key;
|
deba@1419
|
839 |
|
deba@1419
|
840 |
/// \brief Constructor
|
deba@1419
|
841 |
///
|
deba@1419
|
842 |
/// Constructor
|
deba@1419
|
843 |
/// \param _graph The graph that the map belongs to.
|
deba@1419
|
844 |
BackwardMap(const Graph& _graph) : graph(_graph) {}
|
deba@1419
|
845 |
|
deba@1419
|
846 |
/// \brief The subscript operator.
|
deba@1419
|
847 |
///
|
deba@1419
|
848 |
/// The subscript operator.
|
deba@1419
|
849 |
/// \param key An undirected edge
|
deba@1419
|
850 |
/// \return The "backward" directed edge view of undirected edge
|
deba@1419
|
851 |
Value operator[](const Key& key) const {
|
deba@1419
|
852 |
return graph.edgeWithSource(key, graph.target(key));
|
deba@1419
|
853 |
}
|
deba@1419
|
854 |
|
deba@1419
|
855 |
private:
|
deba@1419
|
856 |
const Graph& graph;
|
deba@1419
|
857 |
};
|
deba@1419
|
858 |
|
deba@1419
|
859 |
/// \brief Returns a \ref BackwardMap class
|
deba@1419
|
860 |
|
deba@1419
|
861 |
/// This function just returns an \ref BackwardMap class.
|
deba@1419
|
862 |
/// \relates BackwardMap
|
deba@1419
|
863 |
template <typename Graph>
|
deba@1419
|
864 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
deba@1419
|
865 |
return BackwardMap<Graph>(graph);
|
deba@1419
|
866 |
}
|
deba@1419
|
867 |
|
deba@1515
|
868 |
template <typename _Graph>
|
deba@1515
|
869 |
class DegMapBase {
|
deba@1515
|
870 |
public:
|
deba@1515
|
871 |
|
deba@1515
|
872 |
typedef _Graph Graph;
|
alpar@1402
|
873 |
|
deba@1515
|
874 |
protected:
|
alpar@1453
|
875 |
|
deba@1515
|
876 |
typedef typename Graph::template NodeMap<int> IntNodeMap;
|
deba@1515
|
877 |
|
deba@1515
|
878 |
};
|
alpar@1453
|
879 |
|
deba@1515
|
880 |
/// \brief Map of the node in-degrees.
|
alpar@1453
|
881 |
///
|
deba@1515
|
882 |
/// This map returns the in-degree of a node. Ones it is constructed,
|
deba@1515
|
883 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
deba@1515
|
884 |
/// in constant time. On the other hand, the values updates automatically
|
deba@1515
|
885 |
/// whenever the graph changes.
|
deba@1515
|
886 |
///
|
deba@1515
|
887 |
/// \sa OutDegMap
|
deba@1515
|
888 |
|
alpar@1453
|
889 |
template <typename _Graph>
|
deba@1515
|
890 |
class InDegMap
|
deba@1515
|
891 |
: protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
|
deba@1515
|
892 |
|
alpar@1453
|
893 |
public:
|
deba@1515
|
894 |
|
deba@1515
|
895 |
typedef _Graph Graph;
|
alpar@1453
|
896 |
typedef int Value;
|
deba@1515
|
897 |
typedef typename Graph::Node Key;
|
deba@1515
|
898 |
|
deba@1515
|
899 |
private:
|
deba@1515
|
900 |
|
deba@1515
|
901 |
class AutoNodeMap : public Graph::template NodeMap<int> {
|
deba@1515
|
902 |
public:
|
deba@1515
|
903 |
|
deba@1515
|
904 |
typedef typename Graph::template NodeMap<int> Parent;
|
deba@1515
|
905 |
|
deba@1515
|
906 |
typedef typename Parent::Key Key;
|
deba@1515
|
907 |
typedef typename Parent::Value Value;
|
deba@1515
|
908 |
|
deba@1515
|
909 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
910 |
|
deba@1515
|
911 |
void add(const Key& key) {
|
deba@1515
|
912 |
Parent::add(key);
|
deba@1515
|
913 |
Parent::set(key, 0);
|
deba@1515
|
914 |
}
|
deba@1515
|
915 |
};
|
deba@1515
|
916 |
|
deba@1515
|
917 |
public:
|
alpar@1453
|
918 |
|
alpar@1453
|
919 |
/// \brief Constructor.
|
alpar@1453
|
920 |
///
|
alpar@1453
|
921 |
/// Constructor for creating in-degree map.
|
deba@1515
|
922 |
InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
alpar@1459
|
923 |
AlterationNotifier<typename _Graph::Edge>
|
alpar@1459
|
924 |
::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
|
deba@1515
|
925 |
|
deba@1515
|
926 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
927 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
928 |
}
|
alpar@1453
|
929 |
}
|
alpar@1453
|
930 |
|
deba@1515
|
931 |
virtual ~InDegMap() {
|
alpar@1459
|
932 |
AlterationNotifier<typename _Graph::Edge>::
|
alpar@1453
|
933 |
ObserverBase::detach();
|
alpar@1453
|
934 |
}
|
alpar@1453
|
935 |
|
alpar@1459
|
936 |
/// Gives back the in-degree of a Node.
|
deba@1515
|
937 |
int operator[](const Key& key) const {
|
deba@1515
|
938 |
return deg[key];
|
alpar@1459
|
939 |
}
|
alpar@1453
|
940 |
|
alpar@1453
|
941 |
protected:
|
deba@1515
|
942 |
|
deba@1515
|
943 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
944 |
|
deba@1515
|
945 |
virtual void add(const Edge& edge) {
|
deba@1515
|
946 |
++deg[graph.target(edge)];
|
alpar@1453
|
947 |
}
|
alpar@1453
|
948 |
|
deba@1515
|
949 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
950 |
--deg[graph.target(edge)];
|
deba@1515
|
951 |
}
|
deba@1515
|
952 |
|
deba@1515
|
953 |
|
deba@1515
|
954 |
virtual void build() {
|
deba@1515
|
955 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
956 |
deg[it] = countInEdges(graph, it);
|
deba@1515
|
957 |
}
|
deba@1515
|
958 |
}
|
deba@1515
|
959 |
|
deba@1515
|
960 |
virtual void clear() {
|
deba@1515
|
961 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
962 |
deg[it] = 0;
|
deba@1515
|
963 |
}
|
deba@1515
|
964 |
}
|
deba@1515
|
965 |
private:
|
alpar@1506
|
966 |
|
deba@1515
|
967 |
const _Graph& graph;
|
deba@1515
|
968 |
AutoNodeMap deg;
|
alpar@1459
|
969 |
};
|
alpar@1459
|
970 |
|
alpar@1459
|
971 |
|
deba@1515
|
972 |
/// \brief Map of the node out-degrees.
|
deba@1515
|
973 |
///
|
deba@1515
|
974 |
/// This map returns the out-degree of a node. One it is constructed,
|
deba@1515
|
975 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
deba@1515
|
976 |
/// in constant time. On the other hand, the values updates automatically
|
deba@1515
|
977 |
/// whenever the graph changes.
|
deba@1515
|
978 |
///
|
deba@1515
|
979 |
/// \sa OutDegMap
|
alpar@1459
|
980 |
|
alpar@1459
|
981 |
template <typename _Graph>
|
deba@1515
|
982 |
class OutDegMap
|
deba@1515
|
983 |
: protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
|
deba@1515
|
984 |
|
alpar@1459
|
985 |
public:
|
deba@1515
|
986 |
|
deba@1515
|
987 |
typedef _Graph Graph;
|
alpar@1459
|
988 |
typedef int Value;
|
deba@1515
|
989 |
typedef typename Graph::Node Key;
|
deba@1515
|
990 |
|
deba@1515
|
991 |
private:
|
deba@1515
|
992 |
|
deba@1515
|
993 |
class AutoNodeMap : public Graph::template NodeMap<int> {
|
deba@1515
|
994 |
public:
|
deba@1515
|
995 |
|
deba@1515
|
996 |
typedef typename Graph::template NodeMap<int> Parent;
|
deba@1515
|
997 |
|
deba@1515
|
998 |
typedef typename Parent::Key Key;
|
deba@1515
|
999 |
typedef typename Parent::Value Value;
|
deba@1515
|
1000 |
|
deba@1515
|
1001 |
AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
|
deba@1515
|
1002 |
|
deba@1515
|
1003 |
void add(const Key& key) {
|
deba@1515
|
1004 |
Parent::add(key);
|
deba@1515
|
1005 |
Parent::set(key, 0);
|
deba@1515
|
1006 |
}
|
deba@1515
|
1007 |
};
|
deba@1515
|
1008 |
|
deba@1515
|
1009 |
public:
|
alpar@1459
|
1010 |
|
alpar@1459
|
1011 |
/// \brief Constructor.
|
alpar@1459
|
1012 |
///
|
alpar@1459
|
1013 |
/// Constructor for creating out-degree map.
|
deba@1515
|
1014 |
OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
|
alpar@1459
|
1015 |
AlterationNotifier<typename _Graph::Edge>
|
alpar@1459
|
1016 |
::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
|
deba@1515
|
1017 |
|
deba@1515
|
1018 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1019 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
1020 |
}
|
alpar@1459
|
1021 |
}
|
alpar@1459
|
1022 |
|
deba@1515
|
1023 |
virtual ~OutDegMap() {
|
alpar@1459
|
1024 |
AlterationNotifier<typename _Graph::Edge>::
|
alpar@1459
|
1025 |
ObserverBase::detach();
|
alpar@1459
|
1026 |
}
|
alpar@1459
|
1027 |
|
alpar@1459
|
1028 |
/// Gives back the in-degree of a Node.
|
deba@1515
|
1029 |
int operator[](const Key& key) const {
|
deba@1515
|
1030 |
return deg[key];
|
alpar@1459
|
1031 |
}
|
alpar@1459
|
1032 |
|
alpar@1459
|
1033 |
protected:
|
deba@1515
|
1034 |
|
deba@1515
|
1035 |
typedef typename Graph::Edge Edge;
|
deba@1515
|
1036 |
|
deba@1515
|
1037 |
virtual void add(const Edge& edge) {
|
deba@1515
|
1038 |
++deg[graph.source(edge)];
|
alpar@1459
|
1039 |
}
|
alpar@1459
|
1040 |
|
deba@1515
|
1041 |
virtual void erase(const Edge& edge) {
|
deba@1515
|
1042 |
--deg[graph.source(edge)];
|
deba@1515
|
1043 |
}
|
deba@1515
|
1044 |
|
deba@1515
|
1045 |
|
deba@1515
|
1046 |
virtual void build() {
|
deba@1515
|
1047 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1048 |
deg[it] = countOutEdges(graph, it);
|
deba@1515
|
1049 |
}
|
deba@1515
|
1050 |
}
|
deba@1515
|
1051 |
|
deba@1515
|
1052 |
virtual void clear() {
|
deba@1515
|
1053 |
for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
|
deba@1515
|
1054 |
deg[it] = 0;
|
deba@1515
|
1055 |
}
|
deba@1515
|
1056 |
}
|
deba@1515
|
1057 |
private:
|
alpar@1506
|
1058 |
|
deba@1515
|
1059 |
const _Graph& graph;
|
deba@1515
|
1060 |
AutoNodeMap deg;
|
alpar@1453
|
1061 |
};
|
alpar@1453
|
1062 |
|
alpar@1402
|
1063 |
/// @}
|
alpar@1402
|
1064 |
|
alpar@947
|
1065 |
} //END OF NAMESPACE LEMON
|
klao@946
|
1066 |
|
klao@946
|
1067 |
#endif
|