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