klao@946
|
1 |
/* -*- C++ -*-
|
klao@946
|
2 |
* src/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>
|
alpar@1402
|
21 |
#include <map>
|
klao@946
|
22 |
|
klao@946
|
23 |
#include <lemon/invalid.h>
|
klao@977
|
24 |
#include <lemon/utility.h>
|
deba@1413
|
25 |
#include <lemon/maps.h>
|
klao@946
|
26 |
|
alpar@947
|
27 |
///\ingroup gutils
|
klao@946
|
28 |
///\file
|
alpar@947
|
29 |
///\brief Graph utilities.
|
klao@946
|
30 |
///
|
alpar@964
|
31 |
///\todo Please
|
alpar@964
|
32 |
///revise the documentation.
|
alpar@964
|
33 |
///
|
klao@946
|
34 |
|
klao@946
|
35 |
|
klao@946
|
36 |
namespace lemon {
|
klao@946
|
37 |
|
deba@1267
|
38 |
/// \addtogroup gutils
|
deba@1267
|
39 |
/// @{
|
alpar@947
|
40 |
|
klao@946
|
41 |
/// \brief Function to count the items in the graph.
|
klao@946
|
42 |
///
|
klao@946
|
43 |
/// This function counts the items in the graph.
|
klao@946
|
44 |
/// The complexity of the function is O(n) because
|
klao@946
|
45 |
/// it iterates on all of the items.
|
klao@946
|
46 |
|
klao@946
|
47 |
template <typename Graph, typename ItemIt>
|
klao@977
|
48 |
inline int countItems(const Graph& g) {
|
klao@946
|
49 |
int num = 0;
|
klao@977
|
50 |
for (ItemIt it(g); it != INVALID; ++it) {
|
klao@946
|
51 |
++num;
|
klao@946
|
52 |
}
|
klao@946
|
53 |
return num;
|
klao@946
|
54 |
}
|
klao@946
|
55 |
|
klao@977
|
56 |
// Node counting:
|
klao@977
|
57 |
|
klao@977
|
58 |
template <typename Graph>
|
klao@977
|
59 |
inline
|
klao@977
|
60 |
typename enable_if<typename Graph::NodeNumTag, int>::type
|
klao@977
|
61 |
_countNodes(const Graph &g) {
|
klao@977
|
62 |
return g.nodeNum();
|
klao@977
|
63 |
}
|
klao@977
|
64 |
|
klao@977
|
65 |
template <typename Graph>
|
klao@977
|
66 |
inline int _countNodes(Wrap<Graph> w) {
|
klao@977
|
67 |
return countItems<Graph, typename Graph::NodeIt>(w.value);
|
klao@977
|
68 |
}
|
klao@977
|
69 |
|
klao@946
|
70 |
/// \brief Function to count the nodes in the graph.
|
klao@946
|
71 |
///
|
klao@946
|
72 |
/// This function counts the nodes in the graph.
|
klao@946
|
73 |
/// The complexity of the function is O(n) but for some
|
alpar@964
|
74 |
/// graph structure it is specialized to run in O(1).
|
klao@977
|
75 |
///
|
klao@977
|
76 |
/// \todo refer how to specialize it
|
klao@946
|
77 |
|
klao@946
|
78 |
template <typename Graph>
|
klao@977
|
79 |
inline int countNodes(const Graph& g) {
|
klao@977
|
80 |
return _countNodes<Graph>(g);
|
klao@977
|
81 |
}
|
klao@977
|
82 |
|
klao@977
|
83 |
// Edge counting:
|
klao@977
|
84 |
|
klao@977
|
85 |
template <typename Graph>
|
klao@977
|
86 |
inline
|
klao@977
|
87 |
typename enable_if<typename Graph::EdgeNumTag, int>::type
|
klao@977
|
88 |
_countEdges(const Graph &g) {
|
klao@977
|
89 |
return g.edgeNum();
|
klao@977
|
90 |
}
|
klao@977
|
91 |
|
klao@977
|
92 |
template <typename Graph>
|
klao@977
|
93 |
inline int _countEdges(Wrap<Graph> w) {
|
klao@977
|
94 |
return countItems<Graph, typename Graph::EdgeIt>(w.value);
|
klao@946
|
95 |
}
|
klao@946
|
96 |
|
klao@946
|
97 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
98 |
///
|
klao@946
|
99 |
/// This function counts the edges in the graph.
|
klao@946
|
100 |
/// The complexity of the function is O(e) but for some
|
alpar@964
|
101 |
/// graph structure it is specialized to run in O(1).
|
klao@977
|
102 |
|
klao@946
|
103 |
template <typename Graph>
|
klao@977
|
104 |
inline int countEdges(const Graph& g) {
|
klao@977
|
105 |
return _countEdges<Graph>(g);
|
klao@946
|
106 |
}
|
klao@946
|
107 |
|
klao@1053
|
108 |
// Undirected edge counting:
|
klao@1053
|
109 |
|
klao@1053
|
110 |
template <typename Graph>
|
klao@1053
|
111 |
inline
|
klao@1053
|
112 |
typename enable_if<typename Graph::EdgeNumTag, int>::type
|
klao@1053
|
113 |
_countUndirEdges(const Graph &g) {
|
klao@1053
|
114 |
return g.undirEdgeNum();
|
klao@1053
|
115 |
}
|
klao@1053
|
116 |
|
klao@1053
|
117 |
template <typename Graph>
|
klao@1053
|
118 |
inline int _countUndirEdges(Wrap<Graph> w) {
|
klao@1053
|
119 |
return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
|
klao@1053
|
120 |
}
|
klao@1053
|
121 |
|
klao@1053
|
122 |
/// \brief Function to count the edges in the graph.
|
klao@946
|
123 |
///
|
klao@1053
|
124 |
/// This function counts the edges in the graph.
|
klao@946
|
125 |
/// The complexity of the function is O(e) but for some
|
alpar@964
|
126 |
/// graph structure it is specialized to run in O(1).
|
klao@1053
|
127 |
|
klao@946
|
128 |
template <typename Graph>
|
klao@1053
|
129 |
inline int countUndirEdges(const Graph& g) {
|
klao@1053
|
130 |
return _countUndirEdges<Graph>(g);
|
klao@946
|
131 |
}
|
klao@946
|
132 |
|
klao@977
|
133 |
|
klao@1053
|
134 |
|
klao@946
|
135 |
template <typename Graph, typename DegIt>
|
klao@946
|
136 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
137 |
int num = 0;
|
klao@946
|
138 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
klao@946
|
139 |
++num;
|
klao@946
|
140 |
}
|
klao@946
|
141 |
return num;
|
klao@946
|
142 |
}
|
alpar@967
|
143 |
|
alpar@967
|
144 |
/// Finds an edge between two nodes of a graph.
|
alpar@967
|
145 |
|
alpar@967
|
146 |
/// Finds an edge from node \c u to node \c v in graph \c g.
|
alpar@967
|
147 |
///
|
alpar@967
|
148 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@967
|
149 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@967
|
150 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@967
|
151 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
alpar@967
|
152 |
///
|
alpar@967
|
153 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@967
|
154 |
/// \code
|
alpar@967
|
155 |
/// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
|
alpar@967
|
156 |
/// ...
|
alpar@967
|
157 |
/// }
|
alpar@967
|
158 |
/// \endcode
|
alpar@967
|
159 |
/// \todo We may want to use the \ref concept::GraphBase "GraphBase"
|
alpar@967
|
160 |
/// interface here...
|
alpar@967
|
161 |
/// \bug Untested ...
|
alpar@967
|
162 |
template <typename Graph>
|
alpar@967
|
163 |
typename Graph::Edge findEdge(const Graph &g,
|
deba@1267
|
164 |
typename Graph::Node u, typename Graph::Node v,
|
deba@1267
|
165 |
typename Graph::Edge prev = INVALID)
|
alpar@967
|
166 |
{
|
alpar@967
|
167 |
typename Graph::OutEdgeIt e(g,prev);
|
alpar@1079
|
168 |
// if(prev==INVALID) g.first(e,u);
|
alpar@1079
|
169 |
if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
|
alpar@967
|
170 |
else ++e;
|
alpar@1079
|
171 |
while(e!=INVALID && g.target(e)!=v) ++e;
|
alpar@967
|
172 |
return e;
|
alpar@967
|
173 |
}
|
alpar@964
|
174 |
|
alpar@964
|
175 |
///\e
|
klao@946
|
176 |
|
alpar@964
|
177 |
///\todo Please document.
|
alpar@964
|
178 |
///
|
klao@946
|
179 |
template <typename Graph>
|
klao@946
|
180 |
inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
181 |
return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
|
klao@946
|
182 |
}
|
klao@946
|
183 |
|
alpar@964
|
184 |
///\e
|
alpar@964
|
185 |
|
alpar@964
|
186 |
///\todo Please document.
|
alpar@964
|
187 |
///
|
klao@946
|
188 |
template <typename Graph>
|
klao@946
|
189 |
inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
|
klao@946
|
190 |
return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
|
klao@946
|
191 |
}
|
klao@946
|
192 |
|
klao@946
|
193 |
// graph copy
|
klao@946
|
194 |
|
klao@946
|
195 |
template <
|
klao@946
|
196 |
typename DestinationGraph,
|
klao@946
|
197 |
typename SourceGraph,
|
klao@946
|
198 |
typename NodeBijection>
|
klao@946
|
199 |
void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
200 |
NodeBijection& _nb) {
|
klao@946
|
201 |
for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
|
klao@946
|
202 |
_nb[it] = _d.addNode();
|
klao@946
|
203 |
}
|
klao@946
|
204 |
}
|
klao@946
|
205 |
|
klao@946
|
206 |
template <
|
klao@946
|
207 |
typename DestinationGraph,
|
klao@946
|
208 |
typename SourceGraph,
|
klao@946
|
209 |
typename NodeBijection,
|
klao@946
|
210 |
typename EdgeBijection>
|
klao@946
|
211 |
void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
212 |
const NodeBijection& _nb, EdgeBijection& _eb) {
|
klao@946
|
213 |
for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
|
alpar@986
|
214 |
_eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
|
klao@946
|
215 |
}
|
klao@946
|
216 |
}
|
klao@946
|
217 |
|
klao@946
|
218 |
template <
|
klao@946
|
219 |
typename DestinationGraph,
|
klao@946
|
220 |
typename SourceGraph,
|
klao@946
|
221 |
typename NodeBijection,
|
klao@946
|
222 |
typename EdgeBijection>
|
klao@946
|
223 |
void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
|
klao@946
|
224 |
NodeBijection& _nb, EdgeBijection& _eb) {
|
klao@946
|
225 |
nodeCopy(_d, _s, _nb);
|
klao@946
|
226 |
edgeCopy(_d, _s, _nb, _eb);
|
klao@946
|
227 |
}
|
klao@946
|
228 |
|
deba@1267
|
229 |
template <
|
klao@946
|
230 |
typename _DestinationGraph,
|
klao@946
|
231 |
typename _SourceGraph,
|
klao@946
|
232 |
typename _NodeBijection
|
klao@946
|
233 |
=typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
|
klao@946
|
234 |
typename _EdgeBijection
|
deba@1267
|
235 |
= typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
|
deba@1267
|
236 |
>
|
deba@1267
|
237 |
class GraphCopy {
|
deba@1267
|
238 |
public:
|
deba@1267
|
239 |
|
deba@1267
|
240 |
typedef _DestinationGraph DestinationGraph;
|
deba@1267
|
241 |
typedef _SourceGraph SourceGraph;
|
klao@946
|
242 |
|
deba@1267
|
243 |
typedef _NodeBijection NodeBijection;
|
deba@1267
|
244 |
typedef _EdgeBijection EdgeBijection;
|
deba@1267
|
245 |
|
deba@1267
|
246 |
protected:
|
deba@1267
|
247 |
|
deba@1267
|
248 |
NodeBijection node_bijection;
|
deba@1267
|
249 |
EdgeBijection edge_bijection;
|
klao@946
|
250 |
|
deba@1267
|
251 |
public:
|
deba@1267
|
252 |
|
deba@1267
|
253 |
GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
|
deba@1267
|
254 |
copyGraph(_d, _s, node_bijection, edge_bijection);
|
deba@1267
|
255 |
}
|
deba@1267
|
256 |
|
deba@1267
|
257 |
const NodeBijection& getNodeBijection() const {
|
deba@1267
|
258 |
return node_bijection;
|
deba@1267
|
259 |
}
|
klao@946
|
260 |
|
deba@1267
|
261 |
const EdgeBijection& getEdgeBijection() const {
|
deba@1267
|
262 |
return edge_bijection;
|
deba@1267
|
263 |
}
|
deba@1267
|
264 |
|
deba@1267
|
265 |
};
|
klao@946
|
266 |
|
klao@946
|
267 |
|
deba@1267
|
268 |
template <typename _Graph, typename _Item>
|
deba@1267
|
269 |
class ItemSetTraits {
|
deba@1267
|
270 |
};
|
deba@1192
|
271 |
|
deba@1192
|
272 |
template <typename _Graph>
|
deba@1267
|
273 |
class ItemSetTraits<_Graph, typename _Graph::Node> {
|
deba@1192
|
274 |
public:
|
deba@1192
|
275 |
|
deba@1192
|
276 |
typedef _Graph Graph;
|
alpar@947
|
277 |
|
deba@1192
|
278 |
typedef typename Graph::Node Item;
|
deba@1192
|
279 |
typedef typename Graph::NodeIt ItemIt;
|
deba@1192
|
280 |
|
deba@1192
|
281 |
template <typename _Value>
|
deba@1192
|
282 |
class Map : public Graph::template NodeMap<_Value> {
|
deba@1192
|
283 |
public:
|
deba@1192
|
284 |
typedef typename Graph::template NodeMap<_Value> Parent;
|
deba@1192
|
285 |
typedef typename Parent::Value Value;
|
deba@1192
|
286 |
|
deba@1192
|
287 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1192
|
288 |
Map(const Graph& _graph, const Value& _value)
|
deba@1192
|
289 |
: Parent(_graph, _value) {}
|
deba@1192
|
290 |
};
|
deba@1192
|
291 |
|
deba@1192
|
292 |
};
|
deba@1192
|
293 |
|
deba@1192
|
294 |
template <typename _Graph>
|
deba@1267
|
295 |
class ItemSetTraits<_Graph, typename _Graph::Edge> {
|
deba@1192
|
296 |
public:
|
deba@1192
|
297 |
|
deba@1192
|
298 |
typedef _Graph Graph;
|
deba@1192
|
299 |
|
deba@1192
|
300 |
typedef typename Graph::Edge Item;
|
deba@1192
|
301 |
typedef typename Graph::EdgeIt ItemIt;
|
deba@1192
|
302 |
|
deba@1192
|
303 |
template <typename _Value>
|
deba@1192
|
304 |
class Map : public Graph::template EdgeMap<_Value> {
|
deba@1192
|
305 |
public:
|
deba@1192
|
306 |
typedef typename Graph::template EdgeMap<_Value> Parent;
|
deba@1192
|
307 |
typedef typename Parent::Value Value;
|
deba@1192
|
308 |
|
deba@1192
|
309 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1192
|
310 |
Map(const Graph& _graph, const Value& _value)
|
deba@1192
|
311 |
: Parent(_graph, _value) {}
|
deba@1192
|
312 |
};
|
deba@1192
|
313 |
|
deba@1192
|
314 |
};
|
deba@1192
|
315 |
|
deba@1267
|
316 |
template <typename _Graph>
|
deba@1267
|
317 |
class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
|
deba@1267
|
318 |
public:
|
deba@1267
|
319 |
|
deba@1267
|
320 |
typedef _Graph Graph;
|
deba@1267
|
321 |
|
deba@1267
|
322 |
typedef typename Graph::UndirEdge Item;
|
deba@1267
|
323 |
typedef typename Graph::UndirEdgeIt ItemIt;
|
deba@1267
|
324 |
|
deba@1267
|
325 |
template <typename _Value>
|
deba@1267
|
326 |
class Map : public Graph::template UndirEdgeMap<_Value> {
|
deba@1267
|
327 |
public:
|
deba@1267
|
328 |
typedef typename Graph::template UndirEdgeMap<_Value> Parent;
|
deba@1267
|
329 |
typedef typename Parent::Value Value;
|
deba@1267
|
330 |
|
deba@1267
|
331 |
Map(const Graph& _graph) : Parent(_graph) {}
|
deba@1267
|
332 |
Map(const Graph& _graph, const Value& _value)
|
deba@1267
|
333 |
: Parent(_graph, _value) {}
|
deba@1267
|
334 |
};
|
deba@1267
|
335 |
|
deba@1267
|
336 |
};
|
deba@1192
|
337 |
|
deba@1192
|
338 |
/// @}
|
alpar@1402
|
339 |
|
alpar@1402
|
340 |
/// \addtogroup graph_maps
|
alpar@1402
|
341 |
/// @{
|
alpar@1402
|
342 |
|
alpar@1402
|
343 |
template <typename Map, typename Enable = void>
|
alpar@1402
|
344 |
struct ReferenceMapTraits {
|
alpar@1402
|
345 |
typedef typename Map::Value Value;
|
alpar@1402
|
346 |
typedef typename Map::Value& Reference;
|
alpar@1402
|
347 |
typedef const typename Map::Value& ConstReference;
|
alpar@1402
|
348 |
typedef typename Map::Value* Pointer;
|
alpar@1402
|
349 |
typedef const typename Map::Value* ConstPointer;
|
alpar@1402
|
350 |
};
|
alpar@1402
|
351 |
|
alpar@1402
|
352 |
template <typename Map>
|
alpar@1402
|
353 |
struct ReferenceMapTraits<
|
alpar@1402
|
354 |
Map,
|
alpar@1402
|
355 |
typename enable_if<typename Map::FullTypeTag, void>::type
|
alpar@1402
|
356 |
> {
|
alpar@1402
|
357 |
typedef typename Map::Value Value;
|
alpar@1402
|
358 |
typedef typename Map::Reference Reference;
|
alpar@1402
|
359 |
typedef typename Map::ConstReference ConstReference;
|
alpar@1402
|
360 |
typedef typename Map::Pointer Pointer;
|
alpar@1402
|
361 |
typedef typename Map::ConstPointer ConstPointer;
|
alpar@1402
|
362 |
};
|
alpar@1402
|
363 |
|
deba@1413
|
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@1413
|
378 |
/// \brief Constructor.
|
deba@1413
|
379 |
///
|
deba@1413
|
380 |
/// Constructor for creating id map.
|
deba@1413
|
381 |
IdMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
382 |
|
deba@1413
|
383 |
/// \brief Gives back the \e id of the item.
|
deba@1413
|
384 |
///
|
deba@1413
|
385 |
/// Gives back the immutable and unique \e id of the map.
|
deba@1413
|
386 |
int operator[](const Item& item) const { return graph->id(item);}
|
deba@1413
|
387 |
|
deba@1413
|
388 |
|
deba@1413
|
389 |
private:
|
deba@1413
|
390 |
const Graph* graph;
|
deba@1413
|
391 |
|
deba@1413
|
392 |
public:
|
deba@1413
|
393 |
|
deba@1413
|
394 |
/// \brief The class represents the inverse of the map.
|
deba@1413
|
395 |
///
|
deba@1413
|
396 |
/// The class represents the inverse of the map.
|
deba@1413
|
397 |
/// \see inverse()
|
deba@1413
|
398 |
class InverseMap {
|
deba@1413
|
399 |
public:
|
deba@1413
|
400 |
/// \brief Constructor.
|
deba@1413
|
401 |
///
|
deba@1413
|
402 |
/// Constructor for creating an id-to-item map.
|
deba@1413
|
403 |
InverseMap(const Graph& _graph) : graph(&_graph) {}
|
deba@1413
|
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 IdMap& idMap) : graph(idMap.graph) {}
|
deba@1413
|
409 |
|
deba@1413
|
410 |
/// \brief Gives back the given item from its id.
|
deba@1413
|
411 |
///
|
deba@1413
|
412 |
/// Gives back the given item from its id.
|
deba@1413
|
413 |
///
|
deba@1413
|
414 |
Item operator[](int id) const { return graph->fromId(id, Item());}
|
deba@1413
|
415 |
private:
|
deba@1413
|
416 |
const Graph* graph;
|
deba@1413
|
417 |
};
|
deba@1413
|
418 |
|
deba@1413
|
419 |
/// \brief Gives back the inverse of the map.
|
deba@1413
|
420 |
///
|
deba@1413
|
421 |
/// Gives back the inverse of the map.
|
deba@1413
|
422 |
InverseMap inverse() const { return InverseMap(*graph);}
|
deba@1413
|
423 |
|
deba@1413
|
424 |
};
|
deba@1413
|
425 |
|
deba@1413
|
426 |
|
alpar@1402
|
427 |
/// \brief General inversable graph-map type.
|
alpar@1402
|
428 |
|
alpar@1402
|
429 |
/// This type provides simple inversable map functions.
|
alpar@1402
|
430 |
/// The InversableMap wraps an arbitrary ReadWriteMap
|
alpar@1402
|
431 |
/// and if a key is setted to a new value then store it
|
alpar@1402
|
432 |
/// in the inverse map.
|
alpar@1402
|
433 |
/// \param _Graph The graph type.
|
alpar@1402
|
434 |
/// \param _Map The map to extend with inversable functionality.
|
alpar@1402
|
435 |
template <
|
alpar@1402
|
436 |
typename _Graph,
|
alpar@1402
|
437 |
typename _Item,
|
alpar@1402
|
438 |
typename _Value,
|
alpar@1402
|
439 |
typename _Map
|
deba@1413
|
440 |
= typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
|
alpar@1402
|
441 |
>
|
deba@1413
|
442 |
class InvertableMap : protected _Map {
|
alpar@1402
|
443 |
|
alpar@1402
|
444 |
public:
|
alpar@1402
|
445 |
|
alpar@1402
|
446 |
typedef _Map Map;
|
alpar@1402
|
447 |
typedef _Graph Graph;
|
deba@1413
|
448 |
|
deba@1413
|
449 |
/// The key type of InvertableMap (Node, Edge, UndirEdge).
|
alpar@1402
|
450 |
typedef typename _Map::Key Key;
|
deba@1413
|
451 |
/// The value type of the InvertableMap.
|
alpar@1402
|
452 |
typedef typename _Map::Value Value;
|
alpar@1402
|
453 |
|
alpar@1402
|
454 |
/// \brief Constructor.
|
alpar@1402
|
455 |
///
|
deba@1413
|
456 |
/// Construct a new InvertableMap for the graph.
|
alpar@1402
|
457 |
///
|
deba@1413
|
458 |
InvertableMap(const Graph& graph) : Map(graph) {}
|
alpar@1402
|
459 |
|
alpar@1402
|
460 |
/// \brief The setter function of the map.
|
alpar@1402
|
461 |
///
|
deba@1413
|
462 |
/// Sets the mapped value.
|
alpar@1402
|
463 |
void set(const Key& key, const Value& val) {
|
alpar@1402
|
464 |
Value oldval = Map::operator[](key);
|
deba@1413
|
465 |
typename Container::iterator it = invMap.find(oldval);
|
alpar@1402
|
466 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
467 |
invMap.erase(it);
|
alpar@1402
|
468 |
}
|
alpar@1402
|
469 |
invMap.insert(make_pair(val, key));
|
alpar@1402
|
470 |
Map::set(key, val);
|
alpar@1402
|
471 |
}
|
alpar@1402
|
472 |
|
alpar@1402
|
473 |
/// \brief The getter function of the map.
|
alpar@1402
|
474 |
///
|
alpar@1402
|
475 |
/// It gives back the value associated with the key.
|
deba@1413
|
476 |
const Value operator[](const Key& key) const {
|
alpar@1402
|
477 |
return Map::operator[](key);
|
alpar@1402
|
478 |
}
|
alpar@1402
|
479 |
|
alpar@1402
|
480 |
/// \brief Add a new key to the map.
|
alpar@1402
|
481 |
///
|
alpar@1402
|
482 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
483 |
/// \c AlterationNotifier.
|
alpar@1402
|
484 |
virtual void add(const Key& key) {
|
alpar@1402
|
485 |
Map::add(key);
|
alpar@1402
|
486 |
}
|
alpar@1402
|
487 |
|
alpar@1402
|
488 |
/// \brief Erase the key from the map.
|
alpar@1402
|
489 |
///
|
alpar@1402
|
490 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
491 |
/// \c AlterationNotifier.
|
alpar@1402
|
492 |
virtual void erase(const Key& key) {
|
alpar@1402
|
493 |
Value val = Map::operator[](key);
|
deba@1413
|
494 |
typename Container::iterator it = invMap.find(val);
|
alpar@1402
|
495 |
if (it != invMap.end() && it->second == key) {
|
alpar@1402
|
496 |
invMap.erase(it);
|
alpar@1402
|
497 |
}
|
alpar@1402
|
498 |
Map::erase(key);
|
alpar@1402
|
499 |
}
|
alpar@1402
|
500 |
|
alpar@1402
|
501 |
/// \brief Clear the keys from the map and inverse map.
|
alpar@1402
|
502 |
///
|
alpar@1402
|
503 |
/// Clear the keys from the map and inverse map. It is called by the
|
alpar@1402
|
504 |
/// \c AlterationNotifier.
|
alpar@1402
|
505 |
virtual void clear() {
|
alpar@1402
|
506 |
invMap.clear();
|
alpar@1402
|
507 |
Map::clear();
|
alpar@1402
|
508 |
}
|
alpar@1402
|
509 |
|
deba@1413
|
510 |
private:
|
deba@1413
|
511 |
|
deba@1413
|
512 |
typedef std::map<Value, Key> Container;
|
deba@1413
|
513 |
Container invMap;
|
deba@1413
|
514 |
|
deba@1413
|
515 |
public:
|
deba@1413
|
516 |
|
deba@1413
|
517 |
/// \brief The inverse map type.
|
deba@1413
|
518 |
///
|
deba@1413
|
519 |
/// The inverse of this map. The subscript operator of the map
|
deba@1413
|
520 |
/// gives back always the item what was last assigned to the value.
|
deba@1413
|
521 |
class InverseMap {
|
deba@1413
|
522 |
public:
|
deba@1413
|
523 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
524 |
///
|
deba@1413
|
525 |
/// Constructor of the InverseMap.
|
deba@1413
|
526 |
InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
|
deba@1413
|
527 |
|
deba@1413
|
528 |
/// The value type of the InverseMap.
|
deba@1413
|
529 |
typedef typename InvertableMap::Key Value;
|
deba@1413
|
530 |
/// The key type of the InverseMap.
|
deba@1413
|
531 |
typedef typename InvertableMap::Value Key;
|
deba@1413
|
532 |
|
deba@1413
|
533 |
/// \brief Subscript operator.
|
deba@1413
|
534 |
///
|
deba@1413
|
535 |
/// Subscript operator. It gives back always the item
|
deba@1413
|
536 |
/// what was last assigned to the value.
|
deba@1413
|
537 |
Value operator[](const Key& key) const {
|
deba@1413
|
538 |
typename Container::const_iterator it = inverted.invMap.find(key);
|
deba@1413
|
539 |
return it->second;
|
deba@1413
|
540 |
}
|
deba@1413
|
541 |
|
deba@1413
|
542 |
private:
|
deba@1413
|
543 |
const InvertableMap& inverted;
|
deba@1413
|
544 |
};
|
deba@1413
|
545 |
|
alpar@1402
|
546 |
/// \brief It gives back the just readeable inverse map.
|
alpar@1402
|
547 |
///
|
alpar@1402
|
548 |
/// It gives back the just readeable inverse map.
|
deba@1413
|
549 |
InverseMap inverse() const {
|
deba@1413
|
550 |
return InverseMap(*this);
|
alpar@1402
|
551 |
}
|
alpar@1402
|
552 |
|
alpar@1402
|
553 |
|
deba@1413
|
554 |
|
alpar@1402
|
555 |
};
|
alpar@1402
|
556 |
|
alpar@1402
|
557 |
/// \brief Provides a mutable, continuous and unique descriptor for each
|
alpar@1402
|
558 |
/// item in the graph.
|
alpar@1402
|
559 |
///
|
alpar@1402
|
560 |
/// The DescriptorMap class provides a mutable, continuous and immutable
|
deba@1413
|
561 |
/// mapping for each item in the graph. The value for an item may mutated
|
deba@1413
|
562 |
/// on each operation when the an item erased or added to graph.
|
alpar@1402
|
563 |
///
|
alpar@1402
|
564 |
/// \param _Graph The graph class the \c DescriptorMap belongs to.
|
alpar@1402
|
565 |
/// \param _Item The Item is the Key of the Map. It may be Node, Edge or
|
alpar@1402
|
566 |
/// UndirEdge.
|
alpar@1402
|
567 |
/// \param _Map A ReadWriteMap mapping from the item type to integer.
|
alpar@1402
|
568 |
template <
|
alpar@1402
|
569 |
typename _Graph,
|
alpar@1402
|
570 |
typename _Item,
|
deba@1413
|
571 |
typename _Map
|
deba@1413
|
572 |
= typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
|
alpar@1402
|
573 |
>
|
alpar@1402
|
574 |
class DescriptorMap : protected _Map {
|
alpar@1402
|
575 |
|
alpar@1402
|
576 |
typedef _Item Item;
|
alpar@1402
|
577 |
typedef _Map Map;
|
alpar@1402
|
578 |
|
alpar@1402
|
579 |
public:
|
alpar@1402
|
580 |
/// The graph class of DescriptorMap.
|
alpar@1402
|
581 |
typedef _Graph Graph;
|
alpar@1402
|
582 |
|
alpar@1402
|
583 |
/// The key type of DescriptorMap (Node, Edge, UndirEdge).
|
alpar@1402
|
584 |
typedef typename _Map::Key Key;
|
alpar@1402
|
585 |
/// The value type of DescriptorMap.
|
alpar@1402
|
586 |
typedef typename _Map::Value Value;
|
alpar@1402
|
587 |
|
alpar@1402
|
588 |
/// \brief Constructor.
|
alpar@1402
|
589 |
///
|
deba@1413
|
590 |
/// Constructor for descriptor map.
|
alpar@1402
|
591 |
DescriptorMap(const Graph& _graph) : Map(_graph) {
|
alpar@1402
|
592 |
build();
|
alpar@1402
|
593 |
}
|
alpar@1402
|
594 |
|
alpar@1402
|
595 |
/// \brief Add a new key to the map.
|
alpar@1402
|
596 |
///
|
alpar@1402
|
597 |
/// Add a new key to the map. It is called by the
|
alpar@1402
|
598 |
/// \c AlterationNotifier.
|
alpar@1402
|
599 |
virtual void add(const Item& item) {
|
alpar@1402
|
600 |
Map::add(item);
|
alpar@1402
|
601 |
Map::set(item, invMap.size());
|
alpar@1402
|
602 |
invMap.push_back(item);
|
alpar@1402
|
603 |
}
|
alpar@1402
|
604 |
|
alpar@1402
|
605 |
/// \brief Erase the key from the map.
|
alpar@1402
|
606 |
///
|
alpar@1402
|
607 |
/// Erase the key to the map. It is called by the
|
alpar@1402
|
608 |
/// \c AlterationNotifier.
|
alpar@1402
|
609 |
virtual void erase(const Item& item) {
|
alpar@1402
|
610 |
Map::set(invMap.back(), Map::operator[](item));
|
alpar@1402
|
611 |
invMap[Map::operator[](item)] = invMap.back();
|
deba@1413
|
612 |
invMap.pop_back();
|
alpar@1402
|
613 |
Map::erase(item);
|
alpar@1402
|
614 |
}
|
alpar@1402
|
615 |
|
alpar@1402
|
616 |
/// \brief Build the unique map.
|
alpar@1402
|
617 |
///
|
alpar@1402
|
618 |
/// Build the unique map. It is called by the
|
alpar@1402
|
619 |
/// \c AlterationNotifier.
|
alpar@1402
|
620 |
virtual void build() {
|
alpar@1402
|
621 |
Map::build();
|
alpar@1402
|
622 |
Item it;
|
alpar@1402
|
623 |
const typename Map::Graph* graph = Map::getGraph();
|
alpar@1402
|
624 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
alpar@1402
|
625 |
Map::set(it, invMap.size());
|
alpar@1402
|
626 |
invMap.push_back(it);
|
alpar@1402
|
627 |
}
|
alpar@1402
|
628 |
}
|
alpar@1402
|
629 |
|
alpar@1402
|
630 |
/// \brief Clear the keys from the map.
|
alpar@1402
|
631 |
///
|
alpar@1402
|
632 |
/// Clear the keys from the map. It is called by the
|
alpar@1402
|
633 |
/// \c AlterationNotifier.
|
alpar@1402
|
634 |
virtual void clear() {
|
alpar@1402
|
635 |
invMap.clear();
|
alpar@1402
|
636 |
Map::clear();
|
alpar@1402
|
637 |
}
|
alpar@1402
|
638 |
|
alpar@1402
|
639 |
/// \brief Gives back the \e descriptor of the item.
|
alpar@1402
|
640 |
///
|
alpar@1402
|
641 |
/// Gives back the mutable and unique \e descriptor of the map.
|
alpar@1402
|
642 |
int operator[](const Item& item) const {
|
alpar@1402
|
643 |
return Map::operator[](item);
|
alpar@1402
|
644 |
}
|
alpar@1402
|
645 |
|
deba@1413
|
646 |
private:
|
deba@1413
|
647 |
|
deba@1413
|
648 |
typedef std::vector<Item> Container;
|
deba@1413
|
649 |
Container invMap;
|
deba@1413
|
650 |
|
deba@1413
|
651 |
public:
|
deba@1413
|
652 |
/// \brief The inverse map type.
|
deba@1413
|
653 |
///
|
deba@1413
|
654 |
/// The inverse map type.
|
deba@1413
|
655 |
class InverseMap {
|
deba@1413
|
656 |
public:
|
deba@1413
|
657 |
/// \brief Constructor of the InverseMap.
|
deba@1413
|
658 |
///
|
deba@1413
|
659 |
/// Constructor of the InverseMap.
|
deba@1413
|
660 |
InverseMap(const DescriptorMap& _inverted)
|
deba@1413
|
661 |
: inverted(_inverted) {}
|
deba@1413
|
662 |
|
deba@1413
|
663 |
|
deba@1413
|
664 |
/// The value type of the InverseMap.
|
deba@1413
|
665 |
typedef typename DescriptorMap::Key Value;
|
deba@1413
|
666 |
/// The key type of the InverseMap.
|
deba@1413
|
667 |
typedef typename DescriptorMap::Value Key;
|
deba@1413
|
668 |
|
deba@1413
|
669 |
/// \brief Subscript operator.
|
deba@1413
|
670 |
///
|
deba@1413
|
671 |
/// Subscript operator. It gives back the item
|
deba@1413
|
672 |
/// that the descriptor belongs to currently.
|
deba@1413
|
673 |
Value operator[](const Key& key) const {
|
deba@1413
|
674 |
return inverted.invMap[key];
|
deba@1413
|
675 |
}
|
deba@1413
|
676 |
|
deba@1413
|
677 |
private:
|
deba@1413
|
678 |
const DescriptorMap& inverted;
|
deba@1413
|
679 |
};
|
deba@1413
|
680 |
|
alpar@1402
|
681 |
/// \brief Gives back the inverse of the map.
|
alpar@1402
|
682 |
///
|
alpar@1402
|
683 |
/// Gives back the inverse of the map.
|
alpar@1402
|
684 |
const InverseMap inverse() const {
|
deba@1413
|
685 |
return InverseMap(*this);
|
alpar@1402
|
686 |
}
|
alpar@1402
|
687 |
};
|
alpar@1402
|
688 |
|
alpar@1402
|
689 |
/// \brief Returns the source of the given edge.
|
alpar@1402
|
690 |
///
|
alpar@1402
|
691 |
/// The SourceMap gives back the source Node of the given edge.
|
alpar@1402
|
692 |
/// \author Balazs Dezso
|
alpar@1402
|
693 |
template <typename Graph>
|
alpar@1402
|
694 |
class SourceMap {
|
alpar@1402
|
695 |
public:
|
alpar@1402
|
696 |
typedef typename Graph::Node Value;
|
alpar@1402
|
697 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
698 |
|
alpar@1402
|
699 |
/// \brief Constructor
|
alpar@1402
|
700 |
///
|
alpar@1402
|
701 |
/// Constructor
|
alpar@1402
|
702 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
703 |
SourceMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
704 |
|
alpar@1402
|
705 |
/// \brief The subscript operator.
|
alpar@1402
|
706 |
///
|
alpar@1402
|
707 |
/// The subscript operator.
|
alpar@1402
|
708 |
/// \param edge The edge
|
alpar@1402
|
709 |
/// \return The source of the edge
|
alpar@1402
|
710 |
Value operator[](const Key& edge) {
|
alpar@1402
|
711 |
return graph.source(edge);
|
alpar@1402
|
712 |
}
|
alpar@1402
|
713 |
|
alpar@1402
|
714 |
private:
|
alpar@1402
|
715 |
const Graph& graph;
|
alpar@1402
|
716 |
};
|
alpar@1402
|
717 |
|
alpar@1402
|
718 |
/// \brief Returns a \ref SourceMap class
|
alpar@1402
|
719 |
///
|
alpar@1402
|
720 |
/// This function just returns an \ref SourceMap class.
|
alpar@1402
|
721 |
/// \relates SourceMap
|
alpar@1402
|
722 |
template <typename Graph>
|
alpar@1402
|
723 |
inline SourceMap<Graph> sourceMap(const Graph& graph) {
|
alpar@1402
|
724 |
return SourceMap<Graph>(graph);
|
alpar@1402
|
725 |
}
|
alpar@1402
|
726 |
|
alpar@1402
|
727 |
/// \brief Returns the target of the given edge.
|
alpar@1402
|
728 |
///
|
alpar@1402
|
729 |
/// The TargetMap gives back the target Node of the given edge.
|
alpar@1402
|
730 |
/// \author Balazs Dezso
|
alpar@1402
|
731 |
template <typename Graph>
|
alpar@1402
|
732 |
class TargetMap {
|
alpar@1402
|
733 |
public:
|
alpar@1402
|
734 |
typedef typename Graph::Node Value;
|
alpar@1402
|
735 |
typedef typename Graph::Edge Key;
|
alpar@1402
|
736 |
|
alpar@1402
|
737 |
/// \brief Constructor
|
alpar@1402
|
738 |
///
|
alpar@1402
|
739 |
/// Constructor
|
alpar@1402
|
740 |
/// \param _graph The graph that the map belongs to.
|
alpar@1402
|
741 |
TargetMap(const Graph& _graph) : graph(_graph) {}
|
alpar@1402
|
742 |
|
alpar@1402
|
743 |
/// \brief The subscript operator.
|
alpar@1402
|
744 |
///
|
alpar@1402
|
745 |
/// The subscript operator.
|
alpar@1402
|
746 |
/// \param edge The edge
|
alpar@1402
|
747 |
/// \return The target of the edge
|
alpar@1402
|
748 |
Value operator[](const Key& key) {
|
alpar@1402
|
749 |
return graph.target(key);
|
alpar@1402
|
750 |
}
|
alpar@1402
|
751 |
|
alpar@1402
|
752 |
private:
|
alpar@1402
|
753 |
const Graph& graph;
|
alpar@1402
|
754 |
};
|
alpar@1402
|
755 |
|
alpar@1402
|
756 |
/// \brief Returns a \ref TargetMap class
|
alpar@1402
|
757 |
|
alpar@1402
|
758 |
/// This function just returns an \ref TargetMap class.
|
alpar@1402
|
759 |
/// \relates TargetMap
|
alpar@1402
|
760 |
template <typename Graph>
|
alpar@1402
|
761 |
inline TargetMap<Graph> targetMap(const Graph& graph) {
|
alpar@1402
|
762 |
return TargetMap<Graph>(graph);
|
alpar@1402
|
763 |
}
|
alpar@1402
|
764 |
|
alpar@1402
|
765 |
|
alpar@1402
|
766 |
/// @}
|
alpar@1402
|
767 |
|
alpar@947
|
768 |
} //END OF NAMESPACE LEMON
|
klao@946
|
769 |
|
klao@946
|
770 |
#endif
|