3
31
2
1851
1
2
1
2
1
1
921
2
1
1
2760
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
// This file contains a modified version of the enable_if library from BOOST. |
|
20 |
// See the appropriate copyright notice below. |
|
21 |
|
|
22 |
// Boost enable_if library |
|
23 |
|
|
24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
|
25 |
|
|
26 |
// Use, modification, and distribution is subject to the Boost Software |
|
27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|
28 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
29 |
|
|
30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
|
31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
|
32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
|
33 |
|
|
34 |
|
|
35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
|
36 |
#define LEMON_BITS_ENABLE_IF_H |
|
37 |
|
|
38 |
///\file |
|
39 |
///\brief Miscellaneous basic utilities |
|
40 |
|
|
41 |
namespace lemon |
|
42 |
{ |
|
43 |
|
|
44 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
45 |
|
|
46 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
47 |
/// |
|
48 |
///\sa False |
|
49 |
struct True { |
|
50 |
///\e |
|
51 |
static const bool value = true; |
|
52 |
}; |
|
53 |
|
|
54 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
55 |
|
|
56 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
57 |
/// |
|
58 |
///\sa True |
|
59 |
struct False { |
|
60 |
///\e |
|
61 |
static const bool value = false; |
|
62 |
}; |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
template <typename T> |
|
67 |
struct Wrap { |
|
68 |
const T &value; |
|
69 |
Wrap(const T &t) : value(t) {} |
|
70 |
}; |
|
71 |
|
|
72 |
/**************** dummy class to avoid ambiguity ****************/ |
|
73 |
|
|
74 |
template<int T> struct dummy { dummy(int) {} }; |
|
75 |
|
|
76 |
/**************** enable_if from BOOST ****************/ |
|
77 |
|
|
78 |
template <typename Type, typename T = void> |
|
79 |
struct exists { |
|
80 |
typedef T type; |
|
81 |
}; |
|
82 |
|
|
83 |
|
|
84 |
template <bool B, class T = void> |
|
85 |
struct enable_if_c { |
|
86 |
typedef T type; |
|
87 |
}; |
|
88 |
|
|
89 |
template <class T> |
|
90 |
struct enable_if_c<false, T> {}; |
|
91 |
|
|
92 |
template <class Cond, class T = void> |
|
93 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
|
94 |
|
|
95 |
template <bool B, class T> |
|
96 |
struct lazy_enable_if_c { |
|
97 |
typedef typename T::type type; |
|
98 |
}; |
|
99 |
|
|
100 |
template <class T> |
|
101 |
struct lazy_enable_if_c<false, T> {}; |
|
102 |
|
|
103 |
template <class Cond, class T> |
|
104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
|
105 |
|
|
106 |
|
|
107 |
template <bool B, class T = void> |
|
108 |
struct disable_if_c { |
|
109 |
typedef T type; |
|
110 |
}; |
|
111 |
|
|
112 |
template <class T> |
|
113 |
struct disable_if_c<true, T> {}; |
|
114 |
|
|
115 |
template <class Cond, class T = void> |
|
116 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
|
117 |
|
|
118 |
template <bool B, class T> |
|
119 |
struct lazy_disable_if_c { |
|
120 |
typedef typename T::type type; |
|
121 |
}; |
|
122 |
|
|
123 |
template <class T> |
|
124 |
struct lazy_disable_if_c<true, T> {}; |
|
125 |
|
|
126 |
template <class Cond, class T> |
|
127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
|
128 |
|
|
129 |
} // namespace lemon |
|
130 |
|
|
131 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_CORE_H |
|
20 |
#define LEMON_CORE_H |
|
21 |
|
|
22 |
#include <vector> |
|
23 |
#include <algorithm> |
|
24 |
|
|
25 |
#include <lemon/bits/enable_if.h> |
|
26 |
#include <lemon/bits/traits.h> |
|
27 |
|
|
28 |
///\file |
|
29 |
///\brief LEMON core utilities. |
|
30 |
|
|
31 |
namespace lemon { |
|
32 |
|
|
33 |
/// \brief Dummy type to make it easier to create invalid iterators. |
|
34 |
/// |
|
35 |
/// Dummy type to make it easier to create invalid iterators. |
|
36 |
/// See \ref INVALID for the usage. |
|
37 |
struct Invalid { |
|
38 |
public: |
|
39 |
bool operator==(Invalid) { return true; } |
|
40 |
bool operator!=(Invalid) { return false; } |
|
41 |
bool operator< (Invalid) { return false; } |
|
42 |
}; |
|
43 |
|
|
44 |
/// \brief Invalid iterators. |
|
45 |
/// |
|
46 |
/// \ref Invalid is a global type that converts to each iterator |
|
47 |
/// in such a way that the value of the target iterator will be invalid. |
|
48 |
#ifdef LEMON_ONLY_TEMPLATES |
|
49 |
const Invalid INVALID = Invalid(); |
|
50 |
#else |
|
51 |
extern const Invalid INVALID; |
|
52 |
#endif |
|
53 |
|
|
54 |
/// \addtogroup gutils |
|
55 |
/// @{ |
|
56 |
|
|
57 |
///Creates convenience typedefs for the digraph types and iterators |
|
58 |
|
|
59 |
///This \c \#define creates convenience typedefs for the following types |
|
60 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
61 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
|
62 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
|
63 |
/// |
|
64 |
///\note If the graph type is a dependent type, ie. the graph type depend |
|
65 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
|
66 |
///macro. |
|
67 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
|
68 |
typedef Digraph::Node Node; \ |
|
69 |
typedef Digraph::NodeIt NodeIt; \ |
|
70 |
typedef Digraph::Arc Arc; \ |
|
71 |
typedef Digraph::ArcIt ArcIt; \ |
|
72 |
typedef Digraph::InArcIt InArcIt; \ |
|
73 |
typedef Digraph::OutArcIt OutArcIt; \ |
|
74 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
|
75 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
|
76 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
|
77 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
|
78 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
|
79 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
80 |
|
|
81 |
///Creates convenience typedefs for the digraph types and iterators |
|
82 |
|
|
83 |
///\see DIGRAPH_TYPEDEFS |
|
84 |
/// |
|
85 |
///\note Use this macro, if the graph type is a dependent type, |
|
86 |
///ie. the graph type depend on a template parameter. |
|
87 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
|
88 |
typedef typename Digraph::Node Node; \ |
|
89 |
typedef typename Digraph::NodeIt NodeIt; \ |
|
90 |
typedef typename Digraph::Arc Arc; \ |
|
91 |
typedef typename Digraph::ArcIt ArcIt; \ |
|
92 |
typedef typename Digraph::InArcIt InArcIt; \ |
|
93 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
|
94 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
|
95 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
|
96 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
|
97 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
|
98 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
|
99 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
100 |
|
|
101 |
///Creates convenience typedefs for the graph types and iterators |
|
102 |
|
|
103 |
///This \c \#define creates the same convenience typedefs as defined |
|
104 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
|
105 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
|
106 |
///\c DoubleEdgeMap. |
|
107 |
/// |
|
108 |
///\note If the graph type is a dependent type, ie. the graph type depend |
|
109 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
|
110 |
///macro. |
|
111 |
#define GRAPH_TYPEDEFS(Graph) \ |
|
112 |
DIGRAPH_TYPEDEFS(Graph); \ |
|
113 |
typedef Graph::Edge Edge; \ |
|
114 |
typedef Graph::EdgeIt EdgeIt; \ |
|
115 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
|
116 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
|
117 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
|
118 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
119 |
|
|
120 |
///Creates convenience typedefs for the graph types and iterators |
|
121 |
|
|
122 |
///\see GRAPH_TYPEDEFS |
|
123 |
/// |
|
124 |
///\note Use this macro, if the graph type is a dependent type, |
|
125 |
///ie. the graph type depend on a template parameter. |
|
126 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
|
127 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
|
128 |
typedef typename Graph::Edge Edge; \ |
|
129 |
typedef typename Graph::EdgeIt EdgeIt; \ |
|
130 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
|
131 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
|
132 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
|
133 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
134 |
|
|
135 |
/// \brief Function to count the items in the graph. |
|
136 |
/// |
|
137 |
/// This function counts the items (nodes, arcs etc) in the graph. |
|
138 |
/// The complexity of the function is O(n) because |
|
139 |
/// it iterates on all of the items. |
|
140 |
template <typename Graph, typename Item> |
|
141 |
inline int countItems(const Graph& g) { |
|
142 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
|
143 |
int num = 0; |
|
144 |
for (ItemIt it(g); it != INVALID; ++it) { |
|
145 |
++num; |
|
146 |
} |
|
147 |
return num; |
|
148 |
} |
|
149 |
|
|
150 |
// Node counting: |
|
151 |
|
|
152 |
namespace _core_bits { |
|
153 |
|
|
154 |
template <typename Graph, typename Enable = void> |
|
155 |
struct CountNodesSelector { |
|
156 |
static int count(const Graph &g) { |
|
157 |
return countItems<Graph, typename Graph::Node>(g); |
|
158 |
} |
|
159 |
}; |
|
160 |
|
|
161 |
template <typename Graph> |
|
162 |
struct CountNodesSelector< |
|
163 |
Graph, typename |
|
164 |
enable_if<typename Graph::NodeNumTag, void>::type> |
|
165 |
{ |
|
166 |
static int count(const Graph &g) { |
|
167 |
return g.nodeNum(); |
|
168 |
} |
|
169 |
}; |
|
170 |
} |
|
171 |
|
|
172 |
/// \brief Function to count the nodes in the graph. |
|
173 |
/// |
|
174 |
/// This function counts the nodes in the graph. |
|
175 |
/// The complexity of the function is O(n) but for some |
|
176 |
/// graph structures it is specialized to run in O(1). |
|
177 |
/// |
|
178 |
/// If the graph contains a \e nodeNum() member function and a |
|
179 |
/// \e NodeNumTag tag then this function calls directly the member |
|
180 |
/// function to query the cardinality of the node set. |
|
181 |
template <typename Graph> |
|
182 |
inline int countNodes(const Graph& g) { |
|
183 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
|
184 |
} |
|
185 |
|
|
186 |
// Arc counting: |
|
187 |
|
|
188 |
namespace _core_bits { |
|
189 |
|
|
190 |
template <typename Graph, typename Enable = void> |
|
191 |
struct CountArcsSelector { |
|
192 |
static int count(const Graph &g) { |
|
193 |
return countItems<Graph, typename Graph::Arc>(g); |
|
194 |
} |
|
195 |
}; |
|
196 |
|
|
197 |
template <typename Graph> |
|
198 |
struct CountArcsSelector< |
|
199 |
Graph, |
|
200 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
|
201 |
{ |
|
202 |
static int count(const Graph &g) { |
|
203 |
return g.arcNum(); |
|
204 |
} |
|
205 |
}; |
|
206 |
} |
|
207 |
|
|
208 |
/// \brief Function to count the arcs in the graph. |
|
209 |
/// |
|
210 |
/// This function counts the arcs in the graph. |
|
211 |
/// The complexity of the function is O(e) but for some |
|
212 |
/// graph structures it is specialized to run in O(1). |
|
213 |
/// |
|
214 |
/// If the graph contains a \e arcNum() member function and a |
|
215 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
216 |
/// function to query the cardinality of the arc set. |
|
217 |
template <typename Graph> |
|
218 |
inline int countArcs(const Graph& g) { |
|
219 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
|
220 |
} |
|
221 |
|
|
222 |
// Edge counting: |
|
223 |
namespace _core_bits { |
|
224 |
|
|
225 |
template <typename Graph, typename Enable = void> |
|
226 |
struct CountEdgesSelector { |
|
227 |
static int count(const Graph &g) { |
|
228 |
return countItems<Graph, typename Graph::Edge>(g); |
|
229 |
} |
|
230 |
}; |
|
231 |
|
|
232 |
template <typename Graph> |
|
233 |
struct CountEdgesSelector< |
|
234 |
Graph, |
|
235 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
|
236 |
{ |
|
237 |
static int count(const Graph &g) { |
|
238 |
return g.edgeNum(); |
|
239 |
} |
|
240 |
}; |
|
241 |
} |
|
242 |
|
|
243 |
/// \brief Function to count the edges in the graph. |
|
244 |
/// |
|
245 |
/// This function counts the edges in the graph. |
|
246 |
/// The complexity of the function is O(m) but for some |
|
247 |
/// graph structures it is specialized to run in O(1). |
|
248 |
/// |
|
249 |
/// If the graph contains a \e edgeNum() member function and a |
|
250 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
251 |
/// function to query the cardinality of the edge set. |
|
252 |
template <typename Graph> |
|
253 |
inline int countEdges(const Graph& g) { |
|
254 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
|
255 |
|
|
256 |
} |
|
257 |
|
|
258 |
|
|
259 |
template <typename Graph, typename DegIt> |
|
260 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
|
261 |
int num = 0; |
|
262 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
|
263 |
++num; |
|
264 |
} |
|
265 |
return num; |
|
266 |
} |
|
267 |
|
|
268 |
/// \brief Function to count the number of the out-arcs from node \c n. |
|
269 |
/// |
|
270 |
/// This function counts the number of the out-arcs from node \c n |
|
271 |
/// in the graph. |
|
272 |
template <typename Graph> |
|
273 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
274 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
|
275 |
} |
|
276 |
|
|
277 |
/// \brief Function to count the number of the in-arcs to node \c n. |
|
278 |
/// |
|
279 |
/// This function counts the number of the in-arcs to node \c n |
|
280 |
/// in the graph. |
|
281 |
template <typename Graph> |
|
282 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
283 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
|
284 |
} |
|
285 |
|
|
286 |
/// \brief Function to count the number of the inc-edges to node \c n. |
|
287 |
/// |
|
288 |
/// This function counts the number of the inc-edges to node \c n |
|
289 |
/// in the graph. |
|
290 |
template <typename Graph> |
|
291 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
|
292 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
|
293 |
} |
|
294 |
|
|
295 |
namespace _core_bits { |
|
296 |
|
|
297 |
template <typename Digraph, typename Item, typename RefMap> |
|
298 |
class MapCopyBase { |
|
299 |
public: |
|
300 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
|
301 |
|
|
302 |
virtual ~MapCopyBase() {} |
|
303 |
}; |
|
304 |
|
|
305 |
template <typename Digraph, typename Item, typename RefMap, |
|
306 |
typename ToMap, typename FromMap> |
|
307 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
308 |
public: |
|
309 |
|
|
310 |
MapCopy(ToMap& tmap, const FromMap& map) |
|
311 |
: _tmap(tmap), _map(map) {} |
|
312 |
|
|
313 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
314 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
315 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
316 |
_tmap.set(refMap[it], _map[it]); |
|
317 |
} |
|
318 |
} |
|
319 |
|
|
320 |
private: |
|
321 |
ToMap& _tmap; |
|
322 |
const FromMap& _map; |
|
323 |
}; |
|
324 |
|
|
325 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
|
326 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
327 |
public: |
|
328 |
|
|
329 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
|
330 |
|
|
331 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
|
332 |
_it = refMap[_item]; |
|
333 |
} |
|
334 |
|
|
335 |
private: |
|
336 |
It& _it; |
|
337 |
Item _item; |
|
338 |
}; |
|
339 |
|
|
340 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
|
341 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
342 |
public: |
|
343 |
|
|
344 |
RefCopy(Ref& map) : _map(map) {} |
|
345 |
|
|
346 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
347 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
348 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
349 |
_map.set(it, refMap[it]); |
|
350 |
} |
|
351 |
} |
|
352 |
|
|
353 |
private: |
|
354 |
Ref& _map; |
|
355 |
}; |
|
356 |
|
|
357 |
template <typename Digraph, typename Item, typename RefMap, |
|
358 |
typename CrossRef> |
|
359 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
360 |
public: |
|
361 |
|
|
362 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
|
363 |
|
|
364 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
365 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
366 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
367 |
_cmap.set(refMap[it], it); |
|
368 |
} |
|
369 |
} |
|
370 |
|
|
371 |
private: |
|
372 |
CrossRef& _cmap; |
|
373 |
}; |
|
374 |
|
|
375 |
template <typename Digraph, typename Enable = void> |
|
376 |
struct DigraphCopySelector { |
|
377 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
378 |
static void copy(Digraph &to, const From& from, |
|
379 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
|
380 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
|
381 |
nodeRefMap[it] = to.addNode(); |
|
382 |
} |
|
383 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
|
384 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
|
385 |
nodeRefMap[from.target(it)]); |
|
386 |
} |
|
387 |
} |
|
388 |
}; |
|
389 |
|
|
390 |
template <typename Digraph> |
|
391 |
struct DigraphCopySelector< |
|
392 |
Digraph, |
|
393 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
|
394 |
{ |
|
395 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
396 |
static void copy(Digraph &to, const From& from, |
|
397 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
|
398 |
to.build(from, nodeRefMap, arcRefMap); |
|
399 |
} |
|
400 |
}; |
|
401 |
|
|
402 |
template <typename Graph, typename Enable = void> |
|
403 |
struct GraphCopySelector { |
|
404 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
405 |
static void copy(Graph &to, const From& from, |
|
406 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
|
407 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
|
408 |
nodeRefMap[it] = to.addNode(); |
|
409 |
} |
|
410 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
|
411 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
|
412 |
nodeRefMap[from.v(it)]); |
|
413 |
} |
|
414 |
} |
|
415 |
}; |
|
416 |
|
|
417 |
template <typename Graph> |
|
418 |
struct GraphCopySelector< |
|
419 |
Graph, |
|
420 |
typename enable_if<typename Graph::BuildTag, void>::type> |
|
421 |
{ |
|
422 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
423 |
static void copy(Graph &to, const From& from, |
|
424 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
|
425 |
to.build(from, nodeRefMap, edgeRefMap); |
|
426 |
} |
|
427 |
}; |
|
428 |
|
|
429 |
} |
|
430 |
|
|
431 |
/// \brief Class to copy a digraph. |
|
432 |
/// |
|
433 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
|
434 |
/// simplest way of using it is through the \c copyDigraph() function. |
|
435 |
/// |
|
436 |
/// This class not just make a copy of a graph, but it can create |
|
437 |
/// references and cross references between the nodes and arcs of |
|
438 |
/// the two graphs, it can copy maps for use with the newly created |
|
439 |
/// graph and copy nodes and arcs. |
|
440 |
/// |
|
441 |
/// To make a copy from a graph, first an instance of DigraphCopy |
|
442 |
/// should be created, then the data belongs to the graph should |
|
443 |
/// assigned to copy. In the end, the \c run() member should be |
|
444 |
/// called. |
|
445 |
/// |
|
446 |
/// The next code copies a graph with several data: |
|
447 |
///\code |
|
448 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
449 |
/// // create a reference for the nodes |
|
450 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
451 |
/// dc.nodeRef(nr); |
|
452 |
/// // create a cross reference (inverse) for the arcs |
|
453 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
|
454 |
/// dc.arcCrossRef(acr); |
|
455 |
/// // copy an arc map |
|
456 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
457 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
458 |
/// dc.arcMap(namap, oamap); |
|
459 |
/// // copy a node |
|
460 |
/// OrigGraph::Node on; |
|
461 |
/// NewGraph::Node nn; |
|
462 |
/// dc.node(nn, on); |
|
463 |
/// // Executions of copy |
|
464 |
/// dc.run(); |
|
465 |
///\endcode |
|
466 |
template <typename To, typename From> |
|
467 |
class DigraphCopy { |
|
468 |
private: |
|
469 |
|
|
470 |
typedef typename From::Node Node; |
|
471 |
typedef typename From::NodeIt NodeIt; |
|
472 |
typedef typename From::Arc Arc; |
|
473 |
typedef typename From::ArcIt ArcIt; |
|
474 |
|
|
475 |
typedef typename To::Node TNode; |
|
476 |
typedef typename To::Arc TArc; |
|
477 |
|
|
478 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
479 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
|
480 |
|
|
481 |
|
|
482 |
public: |
|
483 |
|
|
484 |
|
|
485 |
/// \brief Constructor for the DigraphCopy. |
|
486 |
/// |
|
487 |
/// It copies the content of the \c _from digraph into the |
|
488 |
/// \c _to digraph. |
|
489 |
DigraphCopy(To& to, const From& from) |
|
490 |
: _from(from), _to(to) {} |
|
491 |
|
|
492 |
/// \brief Destructor of the DigraphCopy |
|
493 |
/// |
|
494 |
/// Destructor of the DigraphCopy |
|
495 |
~DigraphCopy() { |
|
496 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
497 |
delete _node_maps[i]; |
|
498 |
} |
|
499 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
500 |
delete _arc_maps[i]; |
|
501 |
} |
|
502 |
|
|
503 |
} |
|
504 |
|
|
505 |
/// \brief Copies the node references into the given map. |
|
506 |
/// |
|
507 |
/// Copies the node references into the given map. The parameter |
|
508 |
/// should be a map, which key type is the Node type of the source |
|
509 |
/// graph, while the value type is the Node type of the |
|
510 |
/// destination graph. |
|
511 |
template <typename NodeRef> |
|
512 |
DigraphCopy& nodeRef(NodeRef& map) { |
|
513 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
|
514 |
NodeRefMap, NodeRef>(map)); |
|
515 |
return *this; |
|
516 |
} |
|
517 |
|
|
518 |
/// \brief Copies the node cross references into the given map. |
|
519 |
/// |
|
520 |
/// Copies the node cross references (reverse references) into |
|
521 |
/// the given map. The parameter should be a map, which key type |
|
522 |
/// is the Node type of the destination graph, while the value type is |
|
523 |
/// the Node type of the source graph. |
|
524 |
template <typename NodeCrossRef> |
|
525 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
|
526 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
|
527 |
NodeRefMap, NodeCrossRef>(map)); |
|
528 |
return *this; |
|
529 |
} |
|
530 |
|
|
531 |
/// \brief Make copy of the given map. |
|
532 |
/// |
|
533 |
/// Makes copy of the given map for the newly created digraph. |
|
534 |
/// The new map's key type is the destination graph's node type, |
|
535 |
/// and the copied map's key type is the source graph's node type. |
|
536 |
template <typename ToMap, typename FromMap> |
|
537 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
538 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
|
539 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
540 |
return *this; |
|
541 |
} |
|
542 |
|
|
543 |
/// \brief Make a copy of the given node. |
|
544 |
/// |
|
545 |
/// Make a copy of the given node. |
|
546 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
|
547 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
|
548 |
NodeRefMap, TNode>(tnode, snode)); |
|
549 |
return *this; |
|
550 |
} |
|
551 |
|
|
552 |
/// \brief Copies the arc references into the given map. |
|
553 |
/// |
|
554 |
/// Copies the arc references into the given map. |
|
555 |
template <typename ArcRef> |
|
556 |
DigraphCopy& arcRef(ArcRef& map) { |
|
557 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
|
558 |
ArcRefMap, ArcRef>(map)); |
|
559 |
return *this; |
|
560 |
} |
|
561 |
|
|
562 |
/// \brief Copies the arc cross references into the given map. |
|
563 |
/// |
|
564 |
/// Copies the arc cross references (reverse references) into |
|
565 |
/// the given map. |
|
566 |
template <typename ArcCrossRef> |
|
567 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
|
568 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
|
569 |
ArcRefMap, ArcCrossRef>(map)); |
|
570 |
return *this; |
|
571 |
} |
|
572 |
|
|
573 |
/// \brief Make copy of the given map. |
|
574 |
/// |
|
575 |
/// Makes copy of the given map for the newly created digraph. |
|
576 |
/// The new map's key type is the to digraph's arc type, |
|
577 |
/// and the copied map's key type is the from digraph's arc |
|
578 |
/// type. |
|
579 |
template <typename ToMap, typename FromMap> |
|
580 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
581 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
|
582 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
583 |
return *this; |
|
584 |
} |
|
585 |
|
|
586 |
/// \brief Make a copy of the given arc. |
|
587 |
/// |
|
588 |
/// Make a copy of the given arc. |
|
589 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
590 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
|
591 |
ArcRefMap, TArc>(tarc, sarc)); |
|
592 |
return *this; |
|
593 |
} |
|
594 |
|
|
595 |
/// \brief Executes the copies. |
|
596 |
/// |
|
597 |
/// Executes the copies. |
|
598 |
void run() { |
|
599 |
NodeRefMap nodeRefMap(_from); |
|
600 |
ArcRefMap arcRefMap(_from); |
|
601 |
_core_bits::DigraphCopySelector<To>:: |
|
602 |
copy(_to, _from, nodeRefMap, arcRefMap); |
|
603 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
604 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
605 |
} |
|
606 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
607 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
608 |
} |
|
609 |
} |
|
610 |
|
|
611 |
protected: |
|
612 |
|
|
613 |
|
|
614 |
const From& _from; |
|
615 |
To& _to; |
|
616 |
|
|
617 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
618 |
_node_maps; |
|
619 |
|
|
620 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
621 |
_arc_maps; |
|
622 |
|
|
623 |
}; |
|
624 |
|
|
625 |
/// \brief Copy a digraph to another digraph. |
|
626 |
/// |
|
627 |
/// Copy a digraph to another digraph. The complete usage of the |
|
628 |
/// function is detailed in the DigraphCopy class, but a short |
|
629 |
/// example shows a basic work: |
|
630 |
///\code |
|
631 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
632 |
///\endcode |
|
633 |
/// |
|
634 |
/// After the copy the \c nr map will contain the mapping from the |
|
635 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
636 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
637 |
/// to the arcs of the \c from digraph. |
|
638 |
/// |
|
639 |
/// \see DigraphCopy |
|
640 |
template <typename To, typename From> |
|
641 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
|
642 |
return DigraphCopy<To, From>(to, from); |
|
643 |
} |
|
644 |
|
|
645 |
/// \brief Class to copy a graph. |
|
646 |
/// |
|
647 |
/// Class to copy a graph to another graph (duplicate a graph). The |
|
648 |
/// simplest way of using it is through the \c copyGraph() function. |
|
649 |
/// |
|
650 |
/// This class not just make a copy of a graph, but it can create |
|
651 |
/// references and cross references between the nodes, edges and arcs of |
|
652 |
/// the two graphs, it can copy maps for use with the newly created |
|
653 |
/// graph and copy nodes, edges and arcs. |
|
654 |
/// |
|
655 |
/// To make a copy from a graph, first an instance of GraphCopy |
|
656 |
/// should be created, then the data belongs to the graph should |
|
657 |
/// assigned to copy. In the end, the \c run() member should be |
|
658 |
/// called. |
|
659 |
/// |
|
660 |
/// The next code copies a graph with several data: |
|
661 |
///\code |
|
662 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
663 |
/// // create a reference for the nodes |
|
664 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
665 |
/// dc.nodeRef(nr); |
|
666 |
/// // create a cross reference (inverse) for the edges |
|
667 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
|
668 |
/// dc.edgeCrossRef(ecr); |
|
669 |
/// // copy an arc map |
|
670 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
671 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
672 |
/// dc.arcMap(namap, oamap); |
|
673 |
/// // copy a node |
|
674 |
/// OrigGraph::Node on; |
|
675 |
/// NewGraph::Node nn; |
|
676 |
/// dc.node(nn, on); |
|
677 |
/// // Executions of copy |
|
678 |
/// dc.run(); |
|
679 |
///\endcode |
|
680 |
template <typename To, typename From> |
|
681 |
class GraphCopy { |
|
682 |
private: |
|
683 |
|
|
684 |
typedef typename From::Node Node; |
|
685 |
typedef typename From::NodeIt NodeIt; |
|
686 |
typedef typename From::Arc Arc; |
|
687 |
typedef typename From::ArcIt ArcIt; |
|
688 |
typedef typename From::Edge Edge; |
|
689 |
typedef typename From::EdgeIt EdgeIt; |
|
690 |
|
|
691 |
typedef typename To::Node TNode; |
|
692 |
typedef typename To::Arc TArc; |
|
693 |
typedef typename To::Edge TEdge; |
|
694 |
|
|
695 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
696 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
|
697 |
|
|
698 |
struct ArcRefMap { |
|
699 |
ArcRefMap(const To& to, const From& from, |
|
700 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
|
701 |
: _to(to), _from(from), |
|
702 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
|
703 |
|
|
704 |
typedef typename From::Arc Key; |
|
705 |
typedef typename To::Arc Value; |
|
706 |
|
|
707 |
Value operator[](const Key& key) const { |
|
708 |
bool forward = _from.u(key) != _from.v(key) ? |
|
709 |
_node_ref[_from.source(key)] == |
|
710 |
_to.source(_to.direct(_edge_ref[key], true)) : |
|
711 |
_from.direction(key); |
|
712 |
return _to.direct(_edge_ref[key], forward); |
|
713 |
} |
|
714 |
|
|
715 |
const To& _to; |
|
716 |
const From& _from; |
|
717 |
const EdgeRefMap& _edge_ref; |
|
718 |
const NodeRefMap& _node_ref; |
|
719 |
}; |
|
720 |
|
|
721 |
|
|
722 |
public: |
|
723 |
|
|
724 |
|
|
725 |
/// \brief Constructor for the GraphCopy. |
|
726 |
/// |
|
727 |
/// It copies the content of the \c _from graph into the |
|
728 |
/// \c _to graph. |
|
729 |
GraphCopy(To& to, const From& from) |
|
730 |
: _from(from), _to(to) {} |
|
731 |
|
|
732 |
/// \brief Destructor of the GraphCopy |
|
733 |
/// |
|
734 |
/// Destructor of the GraphCopy |
|
735 |
~GraphCopy() { |
|
736 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
737 |
delete _node_maps[i]; |
|
738 |
} |
|
739 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
740 |
delete _arc_maps[i]; |
|
741 |
} |
|
742 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
743 |
delete _edge_maps[i]; |
|
744 |
} |
|
745 |
|
|
746 |
} |
|
747 |
|
|
748 |
/// \brief Copies the node references into the given map. |
|
749 |
/// |
|
750 |
/// Copies the node references into the given map. |
|
751 |
template <typename NodeRef> |
|
752 |
GraphCopy& nodeRef(NodeRef& map) { |
|
753 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
|
754 |
NodeRefMap, NodeRef>(map)); |
|
755 |
return *this; |
|
756 |
} |
|
757 |
|
|
758 |
/// \brief Copies the node cross references into the given map. |
|
759 |
/// |
|
760 |
/// Copies the node cross references (reverse references) into |
|
761 |
/// the given map. |
|
762 |
template <typename NodeCrossRef> |
|
763 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
|
764 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
|
765 |
NodeRefMap, NodeCrossRef>(map)); |
|
766 |
return *this; |
|
767 |
} |
|
768 |
|
|
769 |
/// \brief Make copy of the given map. |
|
770 |
/// |
|
771 |
/// Makes copy of the given map for the newly created graph. |
|
772 |
/// The new map's key type is the to graph's node type, |
|
773 |
/// and the copied map's key type is the from graph's node |
|
774 |
/// type. |
|
775 |
template <typename ToMap, typename FromMap> |
|
776 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
777 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
|
778 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
779 |
return *this; |
|
780 |
} |
|
781 |
|
|
782 |
/// \brief Make a copy of the given node. |
|
783 |
/// |
|
784 |
/// Make a copy of the given node. |
|
785 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
|
786 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
|
787 |
NodeRefMap, TNode>(tnode, snode)); |
|
788 |
return *this; |
|
789 |
} |
|
790 |
|
|
791 |
/// \brief Copies the arc references into the given map. |
|
792 |
/// |
|
793 |
/// Copies the arc references into the given map. |
|
794 |
template <typename ArcRef> |
|
795 |
GraphCopy& arcRef(ArcRef& map) { |
|
796 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
|
797 |
ArcRefMap, ArcRef>(map)); |
|
798 |
return *this; |
|
799 |
} |
|
800 |
|
|
801 |
/// \brief Copies the arc cross references into the given map. |
|
802 |
/// |
|
803 |
/// Copies the arc cross references (reverse references) into |
|
804 |
/// the given map. |
|
805 |
template <typename ArcCrossRef> |
|
806 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
|
807 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
|
808 |
ArcRefMap, ArcCrossRef>(map)); |
|
809 |
return *this; |
|
810 |
} |
|
811 |
|
|
812 |
/// \brief Make copy of the given map. |
|
813 |
/// |
|
814 |
/// Makes copy of the given map for the newly created graph. |
|
815 |
/// The new map's key type is the to graph's arc type, |
|
816 |
/// and the copied map's key type is the from graph's arc |
|
817 |
/// type. |
|
818 |
template <typename ToMap, typename FromMap> |
|
819 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
820 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
|
821 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
822 |
return *this; |
|
823 |
} |
|
824 |
|
|
825 |
/// \brief Make a copy of the given arc. |
|
826 |
/// |
|
827 |
/// Make a copy of the given arc. |
|
828 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
829 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
|
830 |
ArcRefMap, TArc>(tarc, sarc)); |
|
831 |
return *this; |
|
832 |
} |
|
833 |
|
|
834 |
/// \brief Copies the edge references into the given map. |
|
835 |
/// |
|
836 |
/// Copies the edge references into the given map. |
|
837 |
template <typename EdgeRef> |
|
838 |
GraphCopy& edgeRef(EdgeRef& map) { |
|
839 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
|
840 |
EdgeRefMap, EdgeRef>(map)); |
|
841 |
return *this; |
|
842 |
} |
|
843 |
|
|
844 |
/// \brief Copies the edge cross references into the given map. |
|
845 |
/// |
|
846 |
/// Copies the edge cross references (reverse |
|
847 |
/// references) into the given map. |
|
848 |
template <typename EdgeCrossRef> |
|
849 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
|
850 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
|
851 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
852 |
return *this; |
|
853 |
} |
|
854 |
|
|
855 |
/// \brief Make copy of the given map. |
|
856 |
/// |
|
857 |
/// Makes copy of the given map for the newly created graph. |
|
858 |
/// The new map's key type is the to graph's edge type, |
|
859 |
/// and the copied map's key type is the from graph's edge |
|
860 |
/// type. |
|
861 |
template <typename ToMap, typename FromMap> |
|
862 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
|
863 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
|
864 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
865 |
return *this; |
|
866 |
} |
|
867 |
|
|
868 |
/// \brief Make a copy of the given edge. |
|
869 |
/// |
|
870 |
/// Make a copy of the given edge. |
|
871 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
|
872 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
|
873 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
874 |
return *this; |
|
875 |
} |
|
876 |
|
|
877 |
/// \brief Executes the copies. |
|
878 |
/// |
|
879 |
/// Executes the copies. |
|
880 |
void run() { |
|
881 |
NodeRefMap nodeRefMap(_from); |
|
882 |
EdgeRefMap edgeRefMap(_from); |
|
883 |
ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
|
884 |
_core_bits::GraphCopySelector<To>:: |
|
885 |
copy(_to, _from, nodeRefMap, edgeRefMap); |
|
886 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
887 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
888 |
} |
|
889 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
890 |
_edge_maps[i]->copy(_from, edgeRefMap); |
|
891 |
} |
|
892 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
893 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
894 |
} |
|
895 |
} |
|
896 |
|
|
897 |
private: |
|
898 |
|
|
899 |
const From& _from; |
|
900 |
To& _to; |
|
901 |
|
|
902 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
903 |
_node_maps; |
|
904 |
|
|
905 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
906 |
_arc_maps; |
|
907 |
|
|
908 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
909 |
_edge_maps; |
|
910 |
|
|
911 |
}; |
|
912 |
|
|
913 |
/// \brief Copy a graph to another graph. |
|
914 |
/// |
|
915 |
/// Copy a graph to another graph. The complete usage of the |
|
916 |
/// function is detailed in the GraphCopy class, but a short |
|
917 |
/// example shows a basic work: |
|
918 |
///\code |
|
919 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
920 |
///\endcode |
|
921 |
/// |
|
922 |
/// After the copy the \c nr map will contain the mapping from the |
|
923 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
|
924 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
|
925 |
/// to the arcs of the \c from graph. |
|
926 |
/// |
|
927 |
/// \see GraphCopy |
|
928 |
template <typename To, typename From> |
|
929 |
GraphCopy<To, From> |
|
930 |
copyGraph(To& to, const From& from) { |
|
931 |
return GraphCopy<To, From>(to, from); |
|
932 |
} |
|
933 |
|
|
934 |
namespace _core_bits { |
|
935 |
|
|
936 |
template <typename Graph, typename Enable = void> |
|
937 |
struct FindArcSelector { |
|
938 |
typedef typename Graph::Node Node; |
|
939 |
typedef typename Graph::Arc Arc; |
|
940 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
|
941 |
if (e == INVALID) { |
|
942 |
g.firstOut(e, u); |
|
943 |
} else { |
|
944 |
g.nextOut(e); |
|
945 |
} |
|
946 |
while (e != INVALID && g.target(e) != v) { |
|
947 |
g.nextOut(e); |
|
948 |
} |
|
949 |
return e; |
|
950 |
} |
|
951 |
}; |
|
952 |
|
|
953 |
template <typename Graph> |
|
954 |
struct FindArcSelector< |
|
955 |
Graph, |
|
956 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
957 |
{ |
|
958 |
typedef typename Graph::Node Node; |
|
959 |
typedef typename Graph::Arc Arc; |
|
960 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
|
961 |
return g.findArc(u, v, prev); |
|
962 |
} |
|
963 |
}; |
|
964 |
} |
|
965 |
|
|
966 |
/// \brief Finds an arc between two nodes of a graph. |
|
967 |
/// |
|
968 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
|
969 |
/// |
|
970 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
971 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
972 |
/// the next arc from \c u to \c v after \c prev. |
|
973 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
974 |
/// |
|
975 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
976 |
///\code |
|
977 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
|
978 |
/// ... |
|
979 |
/// } |
|
980 |
///\endcode |
|
981 |
/// |
|
982 |
///\sa ArcLookUp |
|
983 |
///\sa AllArcLookUp |
|
984 |
///\sa DynArcLookUp |
|
985 |
///\sa ConArcIt |
|
986 |
template <typename Graph> |
|
987 |
inline typename Graph::Arc |
|
988 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
989 |
typename Graph::Arc prev = INVALID) { |
|
990 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
|
991 |
} |
|
992 |
|
|
993 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
|
994 |
/// |
|
995 |
/// Iterator for iterating on arcs connected the same nodes. It is |
|
996 |
/// higher level interface for the findArc() function. You can |
|
997 |
/// use it the following way: |
|
998 |
///\code |
|
999 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
1000 |
/// ... |
|
1001 |
/// } |
|
1002 |
///\endcode |
|
1003 |
/// |
|
1004 |
///\sa findArc() |
|
1005 |
///\sa ArcLookUp |
|
1006 |
///\sa AllArcLookUp |
|
1007 |
///\sa DynArcLookUp |
|
1008 |
template <typename _Graph> |
|
1009 |
class ConArcIt : public _Graph::Arc { |
|
1010 |
public: |
|
1011 |
|
|
1012 |
typedef _Graph Graph; |
|
1013 |
typedef typename Graph::Arc Parent; |
|
1014 |
|
|
1015 |
typedef typename Graph::Arc Arc; |
|
1016 |
typedef typename Graph::Node Node; |
|
1017 |
|
|
1018 |
/// \brief Constructor. |
|
1019 |
/// |
|
1020 |
/// Construct a new ConArcIt iterating on the arcs which |
|
1021 |
/// connects the \c u and \c v node. |
|
1022 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
1023 |
Parent::operator=(findArc(_graph, u, v)); |
|
1024 |
} |
|
1025 |
|
|
1026 |
/// \brief Constructor. |
|
1027 |
/// |
|
1028 |
/// Construct a new ConArcIt which continues the iterating from |
|
1029 |
/// the \c e arc. |
|
1030 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
|
1031 |
|
|
1032 |
/// \brief Increment operator. |
|
1033 |
/// |
|
1034 |
/// It increments the iterator and gives back the next arc. |
|
1035 |
ConArcIt& operator++() { |
|
1036 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
|
1037 |
_graph.target(*this), *this)); |
|
1038 |
return *this; |
|
1039 |
} |
|
1040 |
private: |
|
1041 |
const Graph& _graph; |
|
1042 |
}; |
|
1043 |
|
|
1044 |
namespace _core_bits { |
|
1045 |
|
|
1046 |
template <typename Graph, typename Enable = void> |
|
1047 |
struct FindEdgeSelector { |
|
1048 |
typedef typename Graph::Node Node; |
|
1049 |
typedef typename Graph::Edge Edge; |
|
1050 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
|
1051 |
bool b; |
|
1052 |
if (u != v) { |
|
1053 |
if (e == INVALID) { |
|
1054 |
g.firstInc(e, b, u); |
|
1055 |
} else { |
|
1056 |
b = g.u(e) == u; |
|
1057 |
g.nextInc(e, b); |
|
1058 |
} |
|
1059 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
|
1060 |
g.nextInc(e, b); |
|
1061 |
} |
|
1062 |
} else { |
|
1063 |
if (e == INVALID) { |
|
1064 |
g.firstInc(e, b, u); |
|
1065 |
} else { |
|
1066 |
b = true; |
|
1067 |
g.nextInc(e, b); |
|
1068 |
} |
|
1069 |
while (e != INVALID && (!b || g.v(e) != v)) { |
|
1070 |
g.nextInc(e, b); |
|
1071 |
} |
|
1072 |
} |
|
1073 |
return e; |
|
1074 |
} |
|
1075 |
}; |
|
1076 |
|
|
1077 |
template <typename Graph> |
|
1078 |
struct FindEdgeSelector< |
|
1079 |
Graph, |
|
1080 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
1081 |
{ |
|
1082 |
typedef typename Graph::Node Node; |
|
1083 |
typedef typename Graph::Edge Edge; |
|
1084 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
|
1085 |
return g.findEdge(u, v, prev); |
|
1086 |
} |
|
1087 |
}; |
|
1088 |
} |
|
1089 |
|
|
1090 |
/// \brief Finds an edge between two nodes of a graph. |
|
1091 |
/// |
|
1092 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
|
1093 |
/// If the node \c u and node \c v is equal then each loop edge |
|
1094 |
/// will be enumerated once. |
|
1095 |
/// |
|
1096 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
1097 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
1098 |
/// the next arc from \c u to \c v after \c prev. |
|
1099 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
1100 |
/// |
|
1101 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
1102 |
///\code |
|
1103 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
|
1104 |
/// e = findEdge(g,u,v,e)) { |
|
1105 |
/// ... |
|
1106 |
/// } |
|
1107 |
///\endcode |
|
1108 |
/// |
|
1109 |
///\sa ConEdgeIt |
|
1110 |
|
|
1111 |
template <typename Graph> |
|
1112 |
inline typename Graph::Edge |
|
1113 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
1114 |
typename Graph::Edge p = INVALID) { |
|
1115 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
|
1116 |
} |
|
1117 |
|
|
1118 |
/// \brief Iterator for iterating on edges connected the same nodes. |
|
1119 |
/// |
|
1120 |
/// Iterator for iterating on edges connected the same nodes. It is |
|
1121 |
/// higher level interface for the findEdge() function. You can |
|
1122 |
/// use it the following way: |
|
1123 |
///\code |
|
1124 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
1125 |
/// ... |
|
1126 |
/// } |
|
1127 |
///\endcode |
|
1128 |
/// |
|
1129 |
///\sa findEdge() |
|
1130 |
template <typename _Graph> |
|
1131 |
class ConEdgeIt : public _Graph::Edge { |
|
1132 |
public: |
|
1133 |
|
|
1134 |
typedef _Graph Graph; |
|
1135 |
typedef typename Graph::Edge Parent; |
|
1136 |
|
|
1137 |
typedef typename Graph::Edge Edge; |
|
1138 |
typedef typename Graph::Node Node; |
|
1139 |
|
|
1140 |
/// \brief Constructor. |
|
1141 |
/// |
|
1142 |
/// Construct a new ConEdgeIt iterating on the edges which |
|
1143 |
/// connects the \c u and \c v node. |
|
1144 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
1145 |
Parent::operator=(findEdge(_graph, u, v)); |
|
1146 |
} |
|
1147 |
|
|
1148 |
/// \brief Constructor. |
|
1149 |
/// |
|
1150 |
/// Construct a new ConEdgeIt which continues the iterating from |
|
1151 |
/// the \c e edge. |
|
1152 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
|
1153 |
|
|
1154 |
/// \brief Increment operator. |
|
1155 |
/// |
|
1156 |
/// It increments the iterator and gives back the next edge. |
|
1157 |
ConEdgeIt& operator++() { |
|
1158 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
|
1159 |
_graph.v(*this), *this)); |
|
1160 |
return *this; |
|
1161 |
} |
|
1162 |
private: |
|
1163 |
const Graph& _graph; |
|
1164 |
}; |
|
1165 |
|
|
1166 |
|
|
1167 |
///Dynamic arc look up between given endpoints. |
|
1168 |
|
|
1169 |
///Using this class, you can find an arc in a digraph from a given |
|
1170 |
///source to a given target in amortized time <em>O(log d)</em>, |
|
1171 |
///where <em>d</em> is the out-degree of the source node. |
|
1172 |
/// |
|
1173 |
///It is possible to find \e all parallel arcs between two nodes with |
|
1174 |
///the \c findFirst() and \c findNext() members. |
|
1175 |
/// |
|
1176 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
|
1177 |
///digraph is not changed so frequently. |
|
1178 |
/// |
|
1179 |
///This class uses a self-adjusting binary search tree, Sleator's |
|
1180 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
|
1181 |
///time bound for arc lookups. This class also guarantees the |
|
1182 |
///optimal time bound in a constant factor for any distribution of |
|
1183 |
///queries. |
|
1184 |
/// |
|
1185 |
///\tparam G The type of the underlying digraph. |
|
1186 |
/// |
|
1187 |
///\sa ArcLookUp |
|
1188 |
///\sa AllArcLookUp |
|
1189 |
template<class G> |
|
1190 |
class DynArcLookUp |
|
1191 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
|
1192 |
{ |
|
1193 |
public: |
|
1194 |
typedef typename ItemSetTraits<G, typename G::Arc> |
|
1195 |
::ItemNotifier::ObserverBase Parent; |
|
1196 |
|
|
1197 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1198 |
typedef G Digraph; |
|
1199 |
|
|
1200 |
protected: |
|
1201 |
|
|
1202 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type { |
|
1203 |
public: |
|
1204 |
|
|
1205 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
|
1206 |
|
|
1207 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
|
1208 |
|
|
1209 |
virtual void add(const Node& node) { |
|
1210 |
Parent::add(node); |
|
1211 |
Parent::set(node, INVALID); |
|
1212 |
} |
|
1213 |
|
|
1214 |
virtual void add(const std::vector<Node>& nodes) { |
|
1215 |
Parent::add(nodes); |
|
1216 |
for (int i = 0; i < int(nodes.size()); ++i) { |
|
1217 |
Parent::set(nodes[i], INVALID); |
|
1218 |
} |
|
1219 |
} |
|
1220 |
|
|
1221 |
virtual void build() { |
|
1222 |
Parent::build(); |
|
1223 |
Node it; |
|
1224 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
1225 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
1226 |
Parent::set(it, INVALID); |
|
1227 |
} |
|
1228 |
} |
|
1229 |
}; |
|
1230 |
|
|
1231 |
const Digraph &_g; |
|
1232 |
AutoNodeMap _head; |
|
1233 |
typename Digraph::template ArcMap<Arc> _parent; |
|
1234 |
typename Digraph::template ArcMap<Arc> _left; |
|
1235 |
typename Digraph::template ArcMap<Arc> _right; |
|
1236 |
|
|
1237 |
class ArcLess { |
|
1238 |
const Digraph &g; |
|
1239 |
public: |
|
1240 |
ArcLess(const Digraph &_g) : g(_g) {} |
|
1241 |
bool operator()(Arc a,Arc b) const |
|
1242 |
{ |
|
1243 |
return g.target(a)<g.target(b); |
|
1244 |
} |
|
1245 |
}; |
|
1246 |
|
|
1247 |
public: |
|
1248 |
|
|
1249 |
///Constructor |
|
1250 |
|
|
1251 |
///Constructor. |
|
1252 |
/// |
|
1253 |
///It builds up the search database. |
|
1254 |
DynArcLookUp(const Digraph &g) |
|
1255 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
|
1256 |
{ |
|
1257 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
|
1258 |
refresh(); |
|
1259 |
} |
|
1260 |
|
|
1261 |
protected: |
|
1262 |
|
|
1263 |
virtual void add(const Arc& arc) { |
|
1264 |
insert(arc); |
|
1265 |
} |
|
1266 |
|
|
1267 |
virtual void add(const std::vector<Arc>& arcs) { |
|
1268 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
1269 |
insert(arcs[i]); |
|
1270 |
} |
|
1271 |
} |
|
1272 |
|
|
1273 |
virtual void erase(const Arc& arc) { |
|
1274 |
remove(arc); |
|
1275 |
} |
|
1276 |
|
|
1277 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
1278 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
1279 |
remove(arcs[i]); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
|
|
1283 |
virtual void build() { |
|
1284 |
refresh(); |
|
1285 |
} |
|
1286 |
|
|
1287 |
virtual void clear() { |
|
1288 |
for(NodeIt n(_g);n!=INVALID;++n) { |
|
1289 |
_head.set(n, INVALID); |
|
1290 |
} |
|
1291 |
} |
|
1292 |
|
|
1293 |
void insert(Arc arc) { |
|
1294 |
Node s = _g.source(arc); |
|
1295 |
Node t = _g.target(arc); |
|
1296 |
_left.set(arc, INVALID); |
|
1297 |
_right.set(arc, INVALID); |
|
1298 |
|
|
1299 |
Arc e = _head[s]; |
|
1300 |
if (e == INVALID) { |
|
1301 |
_head.set(s, arc); |
|
1302 |
_parent.set(arc, INVALID); |
|
1303 |
return; |
|
1304 |
} |
|
1305 |
while (true) { |
|
1306 |
if (t < _g.target(e)) { |
|
1307 |
if (_left[e] == INVALID) { |
|
1308 |
_left.set(e, arc); |
|
1309 |
_parent.set(arc, e); |
|
1310 |
splay(arc); |
|
1311 |
return; |
|
1312 |
} else { |
|
1313 |
e = _left[e]; |
|
1314 |
} |
|
1315 |
} else { |
|
1316 |
if (_right[e] == INVALID) { |
|
1317 |
_right.set(e, arc); |
|
1318 |
_parent.set(arc, e); |
|
1319 |
splay(arc); |
|
1320 |
return; |
|
1321 |
} else { |
|
1322 |
e = _right[e]; |
|
1323 |
} |
|
1324 |
} |
|
1325 |
} |
|
1326 |
} |
|
1327 |
|
|
1328 |
void remove(Arc arc) { |
|
1329 |
if (_left[arc] == INVALID) { |
|
1330 |
if (_right[arc] != INVALID) { |
|
1331 |
_parent.set(_right[arc], _parent[arc]); |
|
1332 |
} |
|
1333 |
if (_parent[arc] != INVALID) { |
|
1334 |
if (_left[_parent[arc]] == arc) { |
|
1335 |
_left.set(_parent[arc], _right[arc]); |
|
1336 |
} else { |
|
1337 |
_right.set(_parent[arc], _right[arc]); |
|
1338 |
} |
|
1339 |
} else { |
|
1340 |
_head.set(_g.source(arc), _right[arc]); |
|
1341 |
} |
|
1342 |
} else if (_right[arc] == INVALID) { |
|
1343 |
_parent.set(_left[arc], _parent[arc]); |
|
1344 |
if (_parent[arc] != INVALID) { |
|
1345 |
if (_left[_parent[arc]] == arc) { |
|
1346 |
_left.set(_parent[arc], _left[arc]); |
|
1347 |
} else { |
|
1348 |
_right.set(_parent[arc], _left[arc]); |
|
1349 |
} |
|
1350 |
} else { |
|
1351 |
_head.set(_g.source(arc), _left[arc]); |
|
1352 |
} |
|
1353 |
} else { |
|
1354 |
Arc e = _left[arc]; |
|
1355 |
if (_right[e] != INVALID) { |
|
1356 |
e = _right[e]; |
|
1357 |
while (_right[e] != INVALID) { |
|
1358 |
e = _right[e]; |
|
1359 |
} |
|
1360 |
Arc s = _parent[e]; |
|
1361 |
_right.set(_parent[e], _left[e]); |
|
1362 |
if (_left[e] != INVALID) { |
|
1363 |
_parent.set(_left[e], _parent[e]); |
|
1364 |
} |
|
1365 |
|
|
1366 |
_left.set(e, _left[arc]); |
|
1367 |
_parent.set(_left[arc], e); |
|
1368 |
_right.set(e, _right[arc]); |
|
1369 |
_parent.set(_right[arc], e); |
|
1370 |
|
|
1371 |
_parent.set(e, _parent[arc]); |
|
1372 |
if (_parent[arc] != INVALID) { |
|
1373 |
if (_left[_parent[arc]] == arc) { |
|
1374 |
_left.set(_parent[arc], e); |
|
1375 |
} else { |
|
1376 |
_right.set(_parent[arc], e); |
|
1377 |
} |
|
1378 |
} |
|
1379 |
splay(s); |
|
1380 |
} else { |
|
1381 |
_right.set(e, _right[arc]); |
|
1382 |
_parent.set(_right[arc], e); |
|
1383 |
|
|
1384 |
if (_parent[arc] != INVALID) { |
|
1385 |
if (_left[_parent[arc]] == arc) { |
|
1386 |
_left.set(_parent[arc], e); |
|
1387 |
} else { |
|
1388 |
_right.set(_parent[arc], e); |
|
1389 |
} |
|
1390 |
} else { |
|
1391 |
_head.set(_g.source(arc), e); |
|
1392 |
} |
|
1393 |
} |
|
1394 |
} |
|
1395 |
} |
|
1396 |
|
|
1397 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
1398 |
{ |
|
1399 |
int m=(a+b)/2; |
|
1400 |
Arc me=v[m]; |
|
1401 |
if (a < m) { |
|
1402 |
Arc left = refreshRec(v,a,m-1); |
|
1403 |
_left.set(me, left); |
|
1404 |
_parent.set(left, me); |
|
1405 |
} else { |
|
1406 |
_left.set(me, INVALID); |
|
1407 |
} |
|
1408 |
if (m < b) { |
|
1409 |
Arc right = refreshRec(v,m+1,b); |
|
1410 |
_right.set(me, right); |
|
1411 |
_parent.set(right, me); |
|
1412 |
} else { |
|
1413 |
_right.set(me, INVALID); |
|
1414 |
} |
|
1415 |
return me; |
|
1416 |
} |
|
1417 |
|
|
1418 |
void refresh() { |
|
1419 |
for(NodeIt n(_g);n!=INVALID;++n) { |
|
1420 |
std::vector<Arc> v; |
|
1421 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
1422 |
if(v.size()) { |
|
1423 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
1424 |
Arc head = refreshRec(v,0,v.size()-1); |
|
1425 |
_head.set(n, head); |
|
1426 |
_parent.set(head, INVALID); |
|
1427 |
} |
|
1428 |
else _head.set(n, INVALID); |
|
1429 |
} |
|
1430 |
} |
|
1431 |
|
|
1432 |
void zig(Arc v) { |
|
1433 |
Arc w = _parent[v]; |
|
1434 |
_parent.set(v, _parent[w]); |
|
1435 |
_parent.set(w, v); |
|
1436 |
_left.set(w, _right[v]); |
|
1437 |
_right.set(v, w); |
|
1438 |
if (_parent[v] != INVALID) { |
|
1439 |
if (_right[_parent[v]] == w) { |
|
1440 |
_right.set(_parent[v], v); |
|
1441 |
} else { |
|
1442 |
_left.set(_parent[v], v); |
|
1443 |
} |
|
1444 |
} |
|
1445 |
if (_left[w] != INVALID){ |
|
1446 |
_parent.set(_left[w], w); |
|
1447 |
} |
|
1448 |
} |
|
1449 |
|
|
1450 |
void zag(Arc v) { |
|
1451 |
Arc w = _parent[v]; |
|
1452 |
_parent.set(v, _parent[w]); |
|
1453 |
_parent.set(w, v); |
|
1454 |
_right.set(w, _left[v]); |
|
1455 |
_left.set(v, w); |
|
1456 |
if (_parent[v] != INVALID){ |
|
1457 |
if (_left[_parent[v]] == w) { |
|
1458 |
_left.set(_parent[v], v); |
|
1459 |
} else { |
|
1460 |
_right.set(_parent[v], v); |
|
1461 |
} |
|
1462 |
} |
|
1463 |
if (_right[w] != INVALID){ |
|
1464 |
_parent.set(_right[w], w); |
|
1465 |
} |
|
1466 |
} |
|
1467 |
|
|
1468 |
void splay(Arc v) { |
|
1469 |
while (_parent[v] != INVALID) { |
|
1470 |
if (v == _left[_parent[v]]) { |
|
1471 |
if (_parent[_parent[v]] == INVALID) { |
|
1472 |
zig(v); |
|
1473 |
} else { |
|
1474 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
|
1475 |
zig(_parent[v]); |
|
1476 |
zig(v); |
|
1477 |
} else { |
|
1478 |
zig(v); |
|
1479 |
zag(v); |
|
1480 |
} |
|
1481 |
} |
|
1482 |
} else { |
|
1483 |
if (_parent[_parent[v]] == INVALID) { |
|
1484 |
zag(v); |
|
1485 |
} else { |
|
1486 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
|
1487 |
zag(v); |
|
1488 |
zig(v); |
|
1489 |
} else { |
|
1490 |
zag(_parent[v]); |
|
1491 |
zag(v); |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} |
|
1495 |
} |
|
1496 |
_head[_g.source(v)] = v; |
|
1497 |
} |
|
1498 |
|
|
1499 |
|
|
1500 |
public: |
|
1501 |
|
|
1502 |
///Find an arc between two nodes. |
|
1503 |
|
|
1504 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
1505 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
1506 |
///\param s The source node |
|
1507 |
///\param t The target node |
|
1508 |
///\return An arc from \c s to \c t if there exists, |
|
1509 |
///\ref INVALID otherwise. |
|
1510 |
Arc operator()(Node s, Node t) const |
|
1511 |
{ |
|
1512 |
Arc a = _head[s]; |
|
1513 |
while (true) { |
|
1514 |
if (_g.target(a) == t) { |
|
1515 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1516 |
return a; |
|
1517 |
} else if (t < _g.target(a)) { |
|
1518 |
if (_left[a] == INVALID) { |
|
1519 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1520 |
return INVALID; |
|
1521 |
} else { |
|
1522 |
a = _left[a]; |
|
1523 |
} |
|
1524 |
} else { |
|
1525 |
if (_right[a] == INVALID) { |
|
1526 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1527 |
return INVALID; |
|
1528 |
} else { |
|
1529 |
a = _right[a]; |
|
1530 |
} |
|
1531 |
} |
|
1532 |
} |
|
1533 |
} |
|
1534 |
|
|
1535 |
///Find the first arc between two nodes. |
|
1536 |
|
|
1537 |
///Find the first arc between two nodes in time |
|
1538 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
1539 |
/// outgoing arcs of \c s. |
|
1540 |
///\param s The source node |
|
1541 |
///\param t The target node |
|
1542 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
1543 |
/// otherwise. |
|
1544 |
Arc findFirst(Node s, Node t) const |
|
1545 |
{ |
|
1546 |
Arc a = _head[s]; |
|
1547 |
Arc r = INVALID; |
|
1548 |
while (true) { |
|
1549 |
if (_g.target(a) < t) { |
|
1550 |
if (_right[a] == INVALID) { |
|
1551 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1552 |
return r; |
|
1553 |
} else { |
|
1554 |
a = _right[a]; |
|
1555 |
} |
|
1556 |
} else { |
|
1557 |
if (_g.target(a) == t) { |
|
1558 |
r = a; |
|
1559 |
} |
|
1560 |
if (_left[a] == INVALID) { |
|
1561 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1562 |
return r; |
|
1563 |
} else { |
|
1564 |
a = _left[a]; |
|
1565 |
} |
|
1566 |
} |
|
1567 |
} |
|
1568 |
} |
|
1569 |
|
|
1570 |
///Find the next arc between two nodes. |
|
1571 |
|
|
1572 |
///Find the next arc between two nodes in time |
|
1573 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
1574 |
/// outgoing arcs of \c s. |
|
1575 |
///\param s The source node |
|
1576 |
///\param t The target node |
|
1577 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
1578 |
/// otherwise. |
|
1579 |
|
|
1580 |
///\note If \c e is not the result of the previous \c findFirst() |
|
1581 |
///operation then the amorized time bound can not be guaranteed. |
|
1582 |
#ifdef DOXYGEN |
|
1583 |
Arc findNext(Node s, Node t, Arc a) const |
|
1584 |
#else |
|
1585 |
Arc findNext(Node, Node t, Arc a) const |
|
1586 |
#endif |
|
1587 |
{ |
|
1588 |
if (_right[a] != INVALID) { |
|
1589 |
a = _right[a]; |
|
1590 |
while (_left[a] != INVALID) { |
|
1591 |
a = _left[a]; |
|
1592 |
} |
|
1593 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1594 |
} else { |
|
1595 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
|
1596 |
a = _parent[a]; |
|
1597 |
} |
|
1598 |
if (_parent[a] == INVALID) { |
|
1599 |
return INVALID; |
|
1600 |
} else { |
|
1601 |
a = _parent[a]; |
|
1602 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1603 |
} |
|
1604 |
} |
|
1605 |
if (_g.target(a) == t) return a; |
|
1606 |
else return INVALID; |
|
1607 |
} |
|
1608 |
|
|
1609 |
}; |
|
1610 |
|
|
1611 |
///Fast arc look up between given endpoints. |
|
1612 |
|
|
1613 |
///Using this class, you can find an arc in a digraph from a given |
|
1614 |
///source to a given target in time <em>O(log d)</em>, |
|
1615 |
///where <em>d</em> is the out-degree of the source node. |
|
1616 |
/// |
|
1617 |
///It is not possible to find \e all parallel arcs between two nodes. |
|
1618 |
///Use \ref AllArcLookUp for this purpose. |
|
1619 |
/// |
|
1620 |
///\warning This class is static, so you should refresh() (or at least |
|
1621 |
///refresh(Node)) this data structure |
|
1622 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1623 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1624 |
/// |
|
1625 |
///\tparam G The type of the underlying digraph. |
|
1626 |
/// |
|
1627 |
///\sa DynArcLookUp |
|
1628 |
///\sa AllArcLookUp |
|
1629 |
template<class G> |
|
1630 |
class ArcLookUp |
|
1631 |
{ |
|
1632 |
public: |
|
1633 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1634 |
typedef G Digraph; |
|
1635 |
|
|
1636 |
protected: |
|
1637 |
const Digraph &_g; |
|
1638 |
typename Digraph::template NodeMap<Arc> _head; |
|
1639 |
typename Digraph::template ArcMap<Arc> _left; |
|
1640 |
typename Digraph::template ArcMap<Arc> _right; |
|
1641 |
|
|
1642 |
class ArcLess { |
|
1643 |
const Digraph &g; |
|
1644 |
public: |
|
1645 |
ArcLess(const Digraph &_g) : g(_g) {} |
|
1646 |
bool operator()(Arc a,Arc b) const |
|
1647 |
{ |
|
1648 |
return g.target(a)<g.target(b); |
|
1649 |
} |
|
1650 |
}; |
|
1651 |
|
|
1652 |
public: |
|
1653 |
|
|
1654 |
///Constructor |
|
1655 |
|
|
1656 |
///Constructor. |
|
1657 |
/// |
|
1658 |
///It builds up the search database, which remains valid until the digraph |
|
1659 |
///changes. |
|
1660 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
|
1661 |
|
|
1662 |
private: |
|
1663 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
1664 |
{ |
|
1665 |
int m=(a+b)/2; |
|
1666 |
Arc me=v[m]; |
|
1667 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
|
1668 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
|
1669 |
return me; |
|
1670 |
} |
|
1671 |
public: |
|
1672 |
///Refresh the data structure at a node. |
|
1673 |
|
|
1674 |
///Build up the search database of node \c n. |
|
1675 |
/// |
|
1676 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
1677 |
///the number of the outgoing arcs of \c n. |
|
1678 |
void refresh(Node n) |
|
1679 |
{ |
|
1680 |
std::vector<Arc> v; |
|
1681 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
1682 |
if(v.size()) { |
|
1683 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
1684 |
_head[n]=refreshRec(v,0,v.size()-1); |
|
1685 |
} |
|
1686 |
else _head[n]=INVALID; |
|
1687 |
} |
|
1688 |
///Refresh the full data structure. |
|
1689 |
|
|
1690 |
///Build up the full search database. In fact, it simply calls |
|
1691 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
1692 |
/// |
|
1693 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1694 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1695 |
///out-degree of the digraph. |
|
1696 |
|
|
1697 |
void refresh() |
|
1698 |
{ |
|
1699 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
|
1700 |
} |
|
1701 |
|
|
1702 |
///Find an arc between two nodes. |
|
1703 |
|
|
1704 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
1705 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
1706 |
///\param s The source node |
|
1707 |
///\param t The target node |
|
1708 |
///\return An arc from \c s to \c t if there exists, |
|
1709 |
///\ref INVALID otherwise. |
|
1710 |
/// |
|
1711 |
///\warning If you change the digraph, refresh() must be called before using |
|
1712 |
///this operator. If you change the outgoing arcs of |
|
1713 |
///a single node \c n, then |
|
1714 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1715 |
/// |
|
1716 |
Arc operator()(Node s, Node t) const |
|
1717 |
{ |
|
1718 |
Arc e; |
|
1719 |
for(e=_head[s]; |
|
1720 |
e!=INVALID&&_g.target(e)!=t; |
|
1721 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
|
1722 |
return e; |
|
1723 |
} |
|
1724 |
|
|
1725 |
}; |
|
1726 |
|
|
1727 |
///Fast look up of all arcs between given endpoints. |
|
1728 |
|
|
1729 |
///This class is the same as \ref ArcLookUp, with the addition |
|
1730 |
///that it makes it possible to find all arcs between given endpoints. |
|
1731 |
/// |
|
1732 |
///\warning This class is static, so you should refresh() (or at least |
|
1733 |
///refresh(Node)) this data structure |
|
1734 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1735 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1736 |
/// |
|
1737 |
///\tparam G The type of the underlying digraph. |
|
1738 |
/// |
|
1739 |
///\sa DynArcLookUp |
|
1740 |
///\sa ArcLookUp |
|
1741 |
template<class G> |
|
1742 |
class AllArcLookUp : public ArcLookUp<G> |
|
1743 |
{ |
|
1744 |
using ArcLookUp<G>::_g; |
|
1745 |
using ArcLookUp<G>::_right; |
|
1746 |
using ArcLookUp<G>::_left; |
|
1747 |
using ArcLookUp<G>::_head; |
|
1748 |
|
|
1749 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1750 |
typedef G Digraph; |
|
1751 |
|
|
1752 |
typename Digraph::template ArcMap<Arc> _next; |
|
1753 |
|
|
1754 |
Arc refreshNext(Arc head,Arc next=INVALID) |
|
1755 |
{ |
|
1756 |
if(head==INVALID) return next; |
|
1757 |
else { |
|
1758 |
next=refreshNext(_right[head],next); |
|
1759 |
// _next[head]=next; |
|
1760 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
|
1761 |
? next : INVALID; |
|
1762 |
return refreshNext(_left[head],head); |
|
1763 |
} |
|
1764 |
} |
|
1765 |
|
|
1766 |
void refreshNext() |
|
1767 |
{ |
|
1768 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
|
1769 |
} |
|
1770 |
|
|
1771 |
public: |
|
1772 |
///Constructor |
|
1773 |
|
|
1774 |
///Constructor. |
|
1775 |
/// |
|
1776 |
///It builds up the search database, which remains valid until the digraph |
|
1777 |
///changes. |
|
1778 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
|
1779 |
|
|
1780 |
///Refresh the data structure at a node. |
|
1781 |
|
|
1782 |
///Build up the search database of node \c n. |
|
1783 |
/// |
|
1784 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
1785 |
///the number of the outgoing arcs of \c n. |
|
1786 |
|
|
1787 |
void refresh(Node n) |
|
1788 |
{ |
|
1789 |
ArcLookUp<G>::refresh(n); |
|
1790 |
refreshNext(_head[n]); |
|
1791 |
} |
|
1792 |
|
|
1793 |
///Refresh the full data structure. |
|
1794 |
|
|
1795 |
///Build up the full search database. In fact, it simply calls |
|
1796 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
1797 |
/// |
|
1798 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1799 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1800 |
///out-degree of the digraph. |
|
1801 |
|
|
1802 |
void refresh() |
|
1803 |
{ |
|
1804 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
|
1805 |
} |
|
1806 |
|
|
1807 |
///Find an arc between two nodes. |
|
1808 |
|
|
1809 |
///Find an arc between two nodes. |
|
1810 |
///\param s The source node |
|
1811 |
///\param t The target node |
|
1812 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
|
1813 |
///not given, the operator finds the first appropriate arc. |
|
1814 |
///\return An arc from \c s to \c t after \c prev or |
|
1815 |
///\ref INVALID if there is no more. |
|
1816 |
/// |
|
1817 |
///For example, you can count the number of arcs from \c u to \c v in the |
|
1818 |
///following way. |
|
1819 |
///\code |
|
1820 |
///AllArcLookUp<ListDigraph> ae(g); |
|
1821 |
///... |
|
1822 |
///int n=0; |
|
1823 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1824 |
///\endcode |
|
1825 |
/// |
|
1826 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
|
1827 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1828 |
///consecutive arcs are found in constant time. |
|
1829 |
/// |
|
1830 |
///\warning If you change the digraph, refresh() must be called before using |
|
1831 |
///this operator. If you change the outgoing arcs of |
|
1832 |
///a single node \c n, then |
|
1833 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1834 |
/// |
|
1835 |
#ifdef DOXYGEN |
|
1836 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
|
1837 |
#else |
|
1838 |
using ArcLookUp<G>::operator() ; |
|
1839 |
Arc operator()(Node s, Node t, Arc prev) const |
|
1840 |
{ |
|
1841 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
|
1842 |
} |
|
1843 |
#endif |
|
1844 |
|
|
1845 |
}; |
|
1846 |
|
|
1847 |
/// @} |
|
1848 |
|
|
1849 |
} //namespace lemon |
|
1850 |
|
|
1851 |
#endif |
... | ... |
@@ -24,12 +24,12 @@ |
24 | 24 |
lemon/color.h \ |
25 | 25 |
lemon/concept_check.h \ |
26 | 26 |
lemon/counter.h \ |
27 |
lemon/core.h \ |
|
27 | 28 |
lemon/dfs.h \ |
28 | 29 |
lemon/dijkstra.h \ |
29 | 30 |
lemon/dim2.h \ |
30 | 31 |
lemon/error.h \ |
31 | 32 |
lemon/graph_to_eps.h \ |
32 |
lemon/graph_utils.h \ |
|
33 | 33 |
lemon/kruskal.h \ |
34 | 34 |
lemon/lgf_reader.h \ |
35 | 35 |
lemon/lgf_writer.h \ |
... | ... |
@@ -49,12 +49,11 @@ |
49 | 49 |
lemon/bits/base_extender.h \ |
50 | 50 |
lemon/bits/bezier.h \ |
51 | 51 |
lemon/bits/default_map.h \ |
52 |
lemon/bits/enable_if.h \ |
|
52 | 53 |
lemon/bits/graph_extender.h \ |
53 |
lemon/bits/invalid.h \ |
|
54 | 54 |
lemon/bits/map_extender.h \ |
55 | 55 |
lemon/bits/path_dump.h \ |
56 | 56 |
lemon/bits/traits.h \ |
57 |
lemon/bits/utility.h \ |
|
58 | 57 |
lemon/bits/vector_map.h |
59 | 58 |
|
60 | 59 |
concept_HEADERS += \ |
... | ... |
@@ -24,9 +24,8 @@ |
24 | 24 |
///\brief Bfs algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 |
#include <lemon/graph_utils.h> |
|
28 | 27 |
#include <lemon/bits/path_dump.h> |
29 |
#include <lemon/ |
|
28 |
#include <lemon/core.h> |
|
30 | 29 |
#include <lemon/error.h> |
31 | 30 |
#include <lemon/maps.h> |
32 | 31 |
... | ... |
@@ -19,8 +19,7 @@ |
19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 |
#include <lemon/bits/invalid.h> |
|
23 |
#include <lemon/bits/utility.h> |
|
22 |
#include <lemon/core.h> |
|
24 | 23 |
|
25 | 24 |
#include <lemon/bits/map_extender.h> |
26 | 25 |
#include <lemon/bits/default_map.h> |
... | ... |
@@ -19,13 +19,16 @@ |
19 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
20 | 20 |
#define LEMON_BITS_TRAITS_H |
21 | 21 |
|
22 |
#include <lemon/bits/utility.h> |
|
23 |
|
|
24 | 22 |
///\file |
25 | 23 |
///\brief Traits for graphs and maps |
26 | 24 |
/// |
27 | 25 |
|
26 |
#include <lemon/bits/enable_if.h> |
|
27 |
|
|
28 | 28 |
namespace lemon { |
29 |
|
|
30 |
struct InvalidType {}; |
|
31 |
|
|
29 | 32 |
template <typename _Graph, typename _Item> |
30 | 33 |
class ItemSetTraits {}; |
31 | 34 |
... | ... |
@@ -23,8 +23,7 @@ |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of directed graphs. |
25 | 25 |
|
26 |
#include <lemon/bits/invalid.h> |
|
27 |
#include <lemon/bits/utility.h> |
|
26 |
#include <lemon/core.h> |
|
28 | 27 |
#include <lemon/concepts/maps.h> |
29 | 28 |
#include <lemon/concept_check.h> |
30 | 29 |
#include <lemon/concepts/graph_components.h> |
... | ... |
@@ -24,9 +24,8 @@ |
24 | 24 |
///\brief Dfs algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 |
#include <lemon/graph_utils.h> |
|
28 | 27 |
#include <lemon/bits/path_dump.h> |
29 |
#include <lemon/ |
|
28 |
#include <lemon/core.h> |
|
30 | 29 |
#include <lemon/error.h> |
31 | 30 |
#include <lemon/maps.h> |
32 | 31 |
... | ... |
@@ -22,12 +22,9 @@ |
22 | 22 |
#include <algorithm> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <lemon/unionfind.h> |
25 |
// #include <lemon/graph_utils.h> |
|
26 | 25 |
#include <lemon/maps.h> |
27 | 26 |
|
28 |
// #include <lemon/radix_sort.h> |
|
29 |
|
|
30 |
#include <lemon/ |
|
27 |
#include <lemon/core.h> |
|
31 | 28 |
#include <lemon/bits/traits.h> |
32 | 29 |
|
33 | 30 |
///\ingroup spantree |
... | ... |
@@ -300,7 +297,7 @@ |
300 | 297 |
/// |
301 | 298 |
/// \return The total cost of the found spanning tree. |
302 | 299 |
/// |
303 |
/// \note If the input graph is not (weakly) connected, a spanning |
|
300 |
/// \note If the input graph is not (weakly) connected, a spanning |
|
304 | 301 |
/// forest is calculated instead of a spanning tree. |
305 | 302 |
|
306 | 303 |
#ifdef DOXYGEN |
... | ... |
@@ -23,8 +23,7 @@ |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 |
#include <lemon/bits/utility.h> |
|
27 |
#include <lemon/bits/traits.h> |
|
26 |
#include <lemon/core.h> |
|
28 | 27 |
|
29 | 28 |
///\file |
30 | 29 |
///\ingroup maps |
... | ... |
@@ -1780,6 +1779,926 @@ |
1780 | 1779 |
return LoggerBoolMap<Iterator>(it); |
1781 | 1780 |
} |
1782 | 1781 |
|
1782 |
/// Provides an immutable and unique id for each item in the graph. |
|
1783 |
|
|
1784 |
/// The IdMap class provides a unique and immutable id for each item of the |
|
1785 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
|
1786 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
|
1787 |
/// item (node) does not change (even if you delete other nodes). </ul> |
|
1788 |
/// Through this map you get access (i.e. can read) the inner id values of |
|
1789 |
/// the items stored in the graph. This map can be inverted with its member |
|
1790 |
/// class \c InverseMap or with the \c operator() member. |
|
1791 |
/// |
|
1792 |
template <typename _Graph, typename _Item> |
|
1793 |
class IdMap { |
|
1794 |
public: |
|
1795 |
typedef _Graph Graph; |
|
1796 |
typedef int Value; |
|
1797 |
typedef _Item Item; |
|
1798 |
typedef _Item Key; |
|
1799 |
|
|
1800 |
/// \brief Constructor. |
|
1801 |
/// |
|
1802 |
/// Constructor of the map. |
|
1803 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
|
1804 |
|
|
1805 |
/// \brief Gives back the \e id of the item. |
|
1806 |
/// |
|
1807 |
/// Gives back the immutable and unique \e id of the item. |
|
1808 |
int operator[](const Item& item) const { return _graph->id(item);} |
|
1809 |
|
|
1810 |
/// \brief Gives back the item by its id. |
|
1811 |
/// |
|
1812 |
/// Gives back the item by its id. |
|
1813 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
|
1814 |
|
|
1815 |
private: |
|
1816 |
const Graph* _graph; |
|
1817 |
|
|
1818 |
public: |
|
1819 |
|
|
1820 |
/// \brief The class represents the inverse of its owner (IdMap). |
|
1821 |
/// |
|
1822 |
/// The class represents the inverse of its owner (IdMap). |
|
1823 |
/// \see inverse() |
|
1824 |
class InverseMap { |
|
1825 |
public: |
|
1826 |
|
|
1827 |
/// \brief Constructor. |
|
1828 |
/// |
|
1829 |
/// Constructor for creating an id-to-item map. |
|
1830 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
|
1831 |
|
|
1832 |
/// \brief Constructor. |
|
1833 |
/// |
|
1834 |
/// Constructor for creating an id-to-item map. |
|
1835 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
|
1836 |
|
|
1837 |
/// \brief Gives back the given item from its id. |
|
1838 |
/// |
|
1839 |
/// Gives back the given item from its id. |
|
1840 |
/// |
|
1841 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
|
1842 |
|
|
1843 |
private: |
|
1844 |
const Graph* _graph; |
|
1845 |
}; |
|
1846 |
|
|
1847 |
/// \brief Gives back the inverse of the map. |
|
1848 |
/// |
|
1849 |
/// Gives back the inverse of the IdMap. |
|
1850 |
InverseMap inverse() const { return InverseMap(*_graph);} |
|
1851 |
|
|
1852 |
}; |
|
1853 |
|
|
1854 |
|
|
1855 |
/// \brief General invertable graph-map type. |
|
1856 |
|
|
1857 |
/// This type provides simple invertable graph-maps. |
|
1858 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
|
1859 |
/// and if a key is set to a new value then store it |
|
1860 |
/// in the inverse map. |
|
1861 |
/// |
|
1862 |
/// The values of the map can be accessed |
|
1863 |
/// with stl compatible forward iterator. |
|
1864 |
/// |
|
1865 |
/// \tparam _Graph The graph type. |
|
1866 |
/// \tparam _Item The item type of the graph. |
|
1867 |
/// \tparam _Value The value type of the map. |
|
1868 |
/// |
|
1869 |
/// \see IterableValueMap |
|
1870 |
template <typename _Graph, typename _Item, typename _Value> |
|
1871 |
class InvertableMap |
|
1872 |
: protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type { |
|
1873 |
private: |
|
1874 |
|
|
1875 |
typedef typename ItemSetTraits<_Graph, _Item>:: |
|
1876 |
template Map<_Value>::Type Map; |
|
1877 |
typedef _Graph Graph; |
|
1878 |
|
|
1879 |
typedef std::map<_Value, _Item> Container; |
|
1880 |
Container _inv_map; |
|
1881 |
|
|
1882 |
public: |
|
1883 |
|
|
1884 |
/// The key type of InvertableMap (Node, Arc, Edge). |
|
1885 |
typedef typename Map::Key Key; |
|
1886 |
/// The value type of the InvertableMap. |
|
1887 |
typedef typename Map::Value Value; |
|
1888 |
|
|
1889 |
|
|
1890 |
|
|
1891 |
/// \brief Constructor. |
|
1892 |
/// |
|
1893 |
/// Construct a new InvertableMap for the graph. |
|
1894 |
/// |
|
1895 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
|
1896 |
|
|
1897 |
/// \brief Forward iterator for values. |
|
1898 |
/// |
|
1899 |
/// This iterator is an stl compatible forward |
|
1900 |
/// iterator on the values of the map. The values can |
|
1901 |
/// be accessed in the [beginValue, endValue) range. |
|
1902 |
/// |
|
1903 |
class ValueIterator |
|
1904 |
: public std::iterator<std::forward_iterator_tag, Value> { |
|
1905 |
friend class InvertableMap; |
|
1906 |
private: |
|
1907 |
ValueIterator(typename Container::const_iterator _it) |
|
1908 |
: it(_it) {} |
|
1909 |
public: |
|
1910 |
|
|
1911 |
ValueIterator() {} |
|
1912 |
|
|
1913 |
ValueIterator& operator++() { ++it; return *this; } |
|
1914 |
ValueIterator operator++(int) { |
|
1915 |
ValueIterator tmp(*this); |
|
1916 |
operator++(); |
|
1917 |
return tmp; |
|
1918 |
} |
|
1919 |
|
|
1920 |
const Value& operator*() const { return it->first; } |
|
1921 |
const Value* operator->() const { return &(it->first); } |
|
1922 |
|
|
1923 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
|
1924 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
|
1925 |
|
|
1926 |
private: |
|
1927 |
typename Container::const_iterator it; |
|
1928 |
}; |
|
1929 |
|
|
1930 |
/// \brief Returns an iterator to the first value. |
|
1931 |
/// |
|
1932 |
/// Returns an stl compatible iterator to the |
|
1933 |
/// first value of the map. The values of the |
|
1934 |
/// map can be accessed in the [beginValue, endValue) |
|
1935 |
/// range. |
|
1936 |
ValueIterator beginValue() const { |
|
1937 |
return ValueIterator(_inv_map.begin()); |
|
1938 |
} |
|
1939 |
|
|
1940 |
/// \brief Returns an iterator after the last value. |
|
1941 |
/// |
|
1942 |
/// Returns an stl compatible iterator after the |
|
1943 |
/// last value of the map. The values of the |
|
1944 |
/// map can be accessed in the [beginValue, endValue) |
|
1945 |
/// range. |
|
1946 |
ValueIterator endValue() const { |
|
1947 |
return ValueIterator(_inv_map.end()); |
|
1948 |
} |
|
1949 |
|
|
1950 |
/// \brief The setter function of the map. |
|
1951 |
/// |
|
1952 |
/// Sets the mapped value. |
|
1953 |
void set(const Key& key, const Value& val) { |
|
1954 |
Value oldval = Map::operator[](key); |
|
1955 |
typename Container::iterator it = _inv_map.find(oldval); |
|
1956 |
if (it != _inv_map.end() && it->second == key) { |
|
1957 |
_inv_map.erase(it); |
|
1958 |
} |
|
1959 |
_inv_map.insert(make_pair(val, key)); |
|
1960 |
Map::set(key, val); |
|
1961 |
} |
|
1962 |
|
|
1963 |
/// \brief The getter function of the map. |
|
1964 |
/// |
|
1965 |
/// It gives back the value associated with the key. |
|
1966 |
typename MapTraits<Map>::ConstReturnValue |
|
1967 |
operator[](const Key& key) const { |
|
1968 |
return Map::operator[](key); |
|
1969 |
} |
|
1970 |
|
|
1971 |
/// \brief Gives back the item by its value. |
|
1972 |
/// |
|
1973 |
/// Gives back the item by its value. |
|
1974 |
Key operator()(const Value& key) const { |
|
1975 |
typename Container::const_iterator it = _inv_map.find(key); |
|
1976 |
return it != _inv_map.end() ? it->second : INVALID; |
|
1977 |
} |
|
1978 |
|
|
1979 |
protected: |
|
1980 |
|
|
1981 |
/// \brief Erase the key from the map. |
|
1982 |
/// |
|
1983 |
/// Erase the key to the map. It is called by the |
|
1984 |
/// \c AlterationNotifier. |
|
1985 |
virtual void erase(const Key& key) { |
|
1986 |
Value val = Map::operator[](key); |
|
1987 |
typename Container::iterator it = _inv_map.find(val); |
|
1988 |
if (it != _inv_map.end() && it->second == key) { |
|
1989 |
_inv_map.erase(it); |
|
1990 |
} |
|
1991 |
Map::erase(key); |
|
1992 |
} |
|
1993 |
|
|
1994 |
/// \brief Erase more keys from the map. |
|
1995 |
/// |
|
1996 |
/// Erase more keys from the map. It is called by the |
|
1997 |
/// \c AlterationNotifier. |
|
1998 |
virtual void erase(const std::vector<Key>& keys) { |
|
1999 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
2000 |
Value val = Map::operator[](keys[i]); |
|
2001 |
typename Container::iterator it = _inv_map.find(val); |
|
2002 |
if (it != _inv_map.end() && it->second == keys[i]) { |
|
2003 |
_inv_map.erase(it); |
|
2004 |
} |
|
2005 |
} |
|
2006 |
Map::erase(keys); |
|
2007 |
} |
|
2008 |
|
|
2009 |
/// \brief Clear the keys from the map and inverse map. |
|
2010 |
/// |
|
2011 |
/// Clear the keys from the map and inverse map. It is called by the |
|
2012 |
/// \c AlterationNotifier. |
|
2013 |
virtual void clear() { |
|
2014 |
_inv_map.clear(); |
|
2015 |
Map::clear(); |
|
2016 |
} |
|
2017 |
|
|
2018 |
public: |
|
2019 |
|
|
2020 |
/// \brief The inverse map type. |
|
2021 |
/// |
|
2022 |
/// The inverse of this map. The subscript operator of the map |
|
2023 |
/// gives back always the item what was last assigned to the value. |
|
2024 |
class InverseMap { |
|
2025 |
public: |
|
2026 |
/// \brief Constructor of the InverseMap. |
|
2027 |
/// |
|
2028 |
/// Constructor of the InverseMap. |
|
2029 |
explicit InverseMap(const InvertableMap& inverted) |
|
2030 |
: _inverted(inverted) {} |
|
2031 |
|
|
2032 |
/// The value type of the InverseMap. |
|
2033 |
typedef typename InvertableMap::Key Value; |
|
2034 |
/// The key type of the InverseMap. |
|
2035 |
typedef typename InvertableMap::Value Key; |
|
2036 |
|
|
2037 |
/// \brief Subscript operator. |
|
2038 |
/// |
|
2039 |
/// Subscript operator. It gives back always the item |
|
2040 |
/// what was last assigned to the value. |
|
2041 |
Value operator[](const Key& key) const { |
|
2042 |
return _inverted(key); |
|
2043 |
} |
|
2044 |
|
|
2045 |
private: |
|
2046 |
const InvertableMap& _inverted; |
|
2047 |
}; |
|
2048 |
|
|
2049 |
/// \brief It gives back the just readable inverse map. |
|
2050 |
/// |
|
2051 |
/// It gives back the just readable inverse map. |
|
2052 |
InverseMap inverse() const { |
|
2053 |
return InverseMap(*this); |
|
2054 |
} |
|
2055 |
|
|
2056 |
|
|
2057 |
|
|
2058 |
}; |
|
2059 |
|
|
2060 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
|
2061 |
/// item in the graph. |
|
2062 |
/// |
|
2063 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
|
2064 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
|
2065 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
|
2066 |
/// different ids <li>\b continuous: the range of the ids is the set of |
|
2067 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
|
2068 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
|
2069 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
|
2070 |
/// with its member class \c InverseMap, or with the \c operator() member. |
|
2071 |
/// |
|
2072 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
|
2073 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
|
2074 |
/// Edge. |
|
2075 |
template <typename _Graph, typename _Item> |
|
2076 |
class DescriptorMap |
|
2077 |
: protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type { |
|
2078 |
|
|
2079 |
typedef _Item Item; |
|
2080 |
typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map; |
|
2081 |
|
|
2082 |
public: |
|
2083 |
/// The graph class of DescriptorMap. |
|
2084 |
typedef _Graph Graph; |
|
2085 |
|
|
2086 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
|
2087 |
typedef typename Map::Key Key; |
|
2088 |
/// The value type of DescriptorMap. |
|
2089 |
typedef typename Map::Value Value; |
|
2090 |
|
|
2091 |
/// \brief Constructor. |
|
2092 |
/// |
|
2093 |
/// Constructor for descriptor map. |
|
2094 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
|
2095 |
Item it; |
|
2096 |
const typename Map::Notifier* nf = Map::notifier(); |
|
2097 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2098 |
Map::set(it, _inv_map.size()); |
|
2099 |
_inv_map.push_back(it); |
|
2100 |
} |
|
2101 |
} |
|
2102 |
|
|
2103 |
protected: |
|
2104 |
|
|
2105 |
/// \brief Add a new key to the map. |
|
2106 |
/// |
|
2107 |
/// Add a new key to the map. It is called by the |
|
2108 |
/// \c AlterationNotifier. |
|
2109 |
virtual void add(const Item& item) { |
|
2110 |
Map::add(item); |
|
2111 |
Map::set(item, _inv_map.size()); |
|
2112 |
_inv_map.push_back(item); |
|
2113 |
} |
|
2114 |
|
|
2115 |
/// \brief Add more new keys to the map. |
|
2116 |
/// |
|
2117 |
/// Add more new keys to the map. It is called by the |
|
2118 |
/// \c AlterationNotifier. |
|
2119 |
virtual void add(const std::vector<Item>& items) { |
|
2120 |
Map::add(items); |
|
2121 |
for (int i = 0; i < int(items.size()); ++i) { |
|
2122 |
Map::set(items[i], _inv_map.size()); |
|
2123 |
_inv_map.push_back(items[i]); |
|
2124 |
} |
|
2125 |
} |
|
2126 |
|
|
2127 |
/// \brief Erase the key from the map. |
|
2128 |
/// |
|
2129 |
/// Erase the key from the map. It is called by the |
|
2130 |
/// \c AlterationNotifier. |
|
2131 |
virtual void erase(const Item& item) { |
|
2132 |
Map::set(_inv_map.back(), Map::operator[](item)); |
|
2133 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
|
2134 |
_inv_map.pop_back(); |
|
2135 |
Map::erase(item); |
|
2136 |
} |
|
2137 |
|
|
2138 |
/// \brief Erase more keys from the map. |
|
2139 |
/// |
|
2140 |
/// Erase more keys from the map. It is called by the |
|
2141 |
/// \c AlterationNotifier. |
|
2142 |
virtual void erase(const std::vector<Item>& items) { |
|
2143 |
for (int i = 0; i < int(items.size()); ++i) { |
|
2144 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
|
2145 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
|
2146 |
_inv_map.pop_back(); |
|
2147 |
} |
|
2148 |
Map::erase(items); |
|
2149 |
} |
|
2150 |
|
|
2151 |
/// \brief Build the unique map. |
|
2152 |
/// |
|
2153 |
/// Build the unique map. It is called by the |
|
2154 |
/// \c AlterationNotifier. |
|
2155 |
virtual void build() { |
|
2156 |
Map::build(); |
|
2157 |
Item it; |
|
2158 |
const typename Map::Notifier* nf = Map::notifier(); |
|
2159 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2160 |
Map::set(it, _inv_map.size()); |
|
2161 |
_inv_map.push_back(it); |
|
2162 |
} |
|
2163 |
} |
|
2164 |
|
|
2165 |
/// \brief Clear the keys from the map. |
|
2166 |
/// |
|
2167 |
/// Clear the keys from the map. It is called by the |
|
2168 |
/// \c AlterationNotifier. |
|
2169 |
virtual void clear() { |
|
2170 |
_inv_map.clear(); |
|
2171 |
Map::clear(); |
|
2172 |
} |
|
2173 |
|
|
2174 |
public: |
|
2175 |
|
|
2176 |
/// \brief Returns the maximal value plus one. |
|
2177 |
/// |
|
2178 |
/// Returns the maximal value plus one in the map. |
|
2179 |
unsigned int size() const { |
|
2180 |
return _inv_map.size(); |
|
2181 |
} |
|
2182 |
|
|
2183 |
/// \brief Swaps the position of the two items in the map. |
|
2184 |
/// |
|
2185 |
/// Swaps the position of the two items in the map. |
|
2186 |
void swap(const Item& p, const Item& q) { |
|
2187 |
int pi = Map::operator[](p); |
|
2188 |
int qi = Map::operator[](q); |
|
2189 |
Map::set(p, qi); |
|
2190 |
_inv_map[qi] = p; |
|
2191 |
Map::set(q, pi); |
|
2192 |
_inv_map[pi] = q; |
|
2193 |
} |
|
2194 |
|
|
2195 |
/// \brief Gives back the \e descriptor of the item. |
|
2196 |
/// |
|
2197 |
/// Gives back the mutable and unique \e descriptor of the map. |
|
2198 |
int operator[](const Item& item) const { |
|
2199 |
return Map::operator[](item); |
|
2200 |
} |
|
2201 |
|
|
2202 |
/// \brief Gives back the item by its descriptor. |
|
2203 |
/// |
|
2204 |
/// Gives back th item by its descriptor. |
|
2205 |
Item operator()(int id) const { |
|
2206 |
return _inv_map[id]; |
|
2207 |
} |
|
2208 |
|
|
2209 |
private: |
|
2210 |
|
|
2211 |
typedef std::vector<Item> Container; |
|
2212 |
Container _inv_map; |
|
2213 |
|
|
2214 |
public: |
|
2215 |
/// \brief The inverse map type of DescriptorMap. |
|
2216 |
/// |
|
2217 |
/// The inverse map type of DescriptorMap. |
|
2218 |
class InverseMap { |
|
2219 |
public: |
|
2220 |
/// \brief Constructor of the InverseMap. |
|
2221 |
/// |
|
2222 |
/// Constructor of the InverseMap. |
|
2223 |
explicit InverseMap(const DescriptorMap& inverted) |
|
2224 |
: _inverted(inverted) {} |
|
2225 |
|
|
2226 |
|
|
2227 |
/// The value type of the InverseMap. |
|
2228 |
typedef typename DescriptorMap::Key Value; |
|
2229 |
/// The key type of the InverseMap. |
|
2230 |
typedef typename DescriptorMap::Value Key; |
|
2231 |
|
|
2232 |
/// \brief Subscript operator. |
|
2233 |
/// |
|
2234 |
/// Subscript operator. It gives back the item |
|
2235 |
/// that the descriptor belongs to currently. |
|
2236 |
Value operator[](const Key& key) const { |
|
2237 |
return _inverted(key); |
|
2238 |
} |
|
2239 |
|
|
2240 |
/// \brief Size of the map. |
|
2241 |
/// |
|
2242 |
/// Returns the size of the map. |
|
2243 |
unsigned int size() const { |
|
2244 |
return _inverted.size(); |
|
2245 |
} |
|
2246 |
|
|
2247 |
private: |
|
2248 |
const DescriptorMap& _inverted; |
|
2249 |
}; |
|
2250 |
|
|
2251 |
/// \brief Gives back the inverse of the map. |
|
2252 |
/// |
|
2253 |
/// Gives back the inverse of the map. |
|
2254 |
const InverseMap inverse() const { |
|
2255 |
return InverseMap(*this); |
|
2256 |
} |
|
2257 |
}; |
|
2258 |
|
|
2259 |
/// \brief Returns the source of the given arc. |
|
2260 |
/// |
|
2261 |
/// The SourceMap gives back the source Node of the given arc. |
|
2262 |
/// \see TargetMap |
|
2263 |
template <typename Digraph> |
|
2264 |
class SourceMap { |
|
2265 |
public: |
|
2266 |
|
|
2267 |
typedef typename Digraph::Node Value; |
|
2268 |
typedef typename Digraph::Arc Key; |
|
2269 |
|
|
2270 |
/// \brief Constructor |
|
2271 |
/// |
|
2272 |
/// Constructor |
|
2273 |
/// \param _digraph The digraph that the map belongs to. |
|
2274 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2275 |
|
|
2276 |
/// \brief The subscript operator. |
|
2277 |
/// |
|
2278 |
/// The subscript operator. |
|
2279 |
/// \param arc The arc |
|
2280 |
/// \return The source of the arc |
|
2281 |
Value operator[](const Key& arc) const { |
|
2282 |
return _digraph.source(arc); |
|
2283 |
} |
|
2284 |
|
|
2285 |
private: |
|
2286 |
const Digraph& _digraph; |
|
2287 |
}; |
|
2288 |
|
|
2289 |
/// \brief Returns a \ref SourceMap class. |
|
2290 |
/// |
|
2291 |
/// This function just returns an \ref SourceMap class. |
|
2292 |
/// \relates SourceMap |
|
2293 |
template <typename Digraph> |
|
2294 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
|
2295 |
return SourceMap<Digraph>(digraph); |
|
2296 |
} |
|
2297 |
|
|
2298 |
/// \brief Returns the target of the given arc. |
|
2299 |
/// |
|
2300 |
/// The TargetMap gives back the target Node of the given arc. |
|
2301 |
/// \see SourceMap |
|
2302 |
template <typename Digraph> |
|
2303 |
class TargetMap { |
|
2304 |
public: |
|
2305 |
|
|
2306 |
typedef typename Digraph::Node Value; |
|
2307 |
typedef typename Digraph::Arc Key; |
|
2308 |
|
|
2309 |
/// \brief Constructor |
|
2310 |
/// |
|
2311 |
/// Constructor |
|
2312 |
/// \param _digraph The digraph that the map belongs to. |
|
2313 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2314 |
|
|
2315 |
/// \brief The subscript operator. |
|
2316 |
/// |
|
2317 |
/// The subscript operator. |
|
2318 |
/// \param e The arc |
|
2319 |
/// \return The target of the arc |
|
2320 |
Value operator[](const Key& e) const { |
|
2321 |
return _digraph.target(e); |
|
2322 |
} |
|
2323 |
|
|
2324 |
private: |
|
2325 |
const Digraph& _digraph; |
|
2326 |
}; |
|
2327 |
|
|
2328 |
/// \brief Returns a \ref TargetMap class. |
|
2329 |
/// |
|
2330 |
/// This function just returns a \ref TargetMap class. |
|
2331 |
/// \relates TargetMap |
|
2332 |
template <typename Digraph> |
|
2333 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
|
2334 |
return TargetMap<Digraph>(digraph); |
|
2335 |
} |
|
2336 |
|
|
2337 |
/// \brief Returns the "forward" directed arc view of an edge. |
|
2338 |
/// |
|
2339 |
/// Returns the "forward" directed arc view of an edge. |
|
2340 |
/// \see BackwardMap |
|
2341 |
template <typename Graph> |
|
2342 |
class ForwardMap { |
|
2343 |
public: |
|
2344 |
|
|
2345 |
typedef typename Graph::Arc Value; |
|
2346 |
typedef typename Graph::Edge Key; |
|
2347 |
|
|
2348 |
/// \brief Constructor |
|
2349 |
/// |
|
2350 |
/// Constructor |
|
2351 |
/// \param _graph The graph that the map belongs to. |
|
2352 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
|
2353 |
|
|
2354 |
/// \brief The subscript operator. |
|
2355 |
/// |
|
2356 |
/// The subscript operator. |
|
2357 |
/// \param key An edge |
|
2358 |
/// \return The "forward" directed arc view of edge |
|
2359 |
Value operator[](const Key& key) const { |
|
2360 |
return _graph.direct(key, true); |
|
2361 |
} |
|
2362 |
|
|
2363 |
private: |
|
2364 |
const Graph& _graph; |
|
2365 |
}; |
|
2366 |
|
|
2367 |
/// \brief Returns a \ref ForwardMap class. |
|
2368 |
/// |
|
2369 |
/// This function just returns an \ref ForwardMap class. |
|
2370 |
/// \relates ForwardMap |
|
2371 |
template <typename Graph> |
|
2372 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
|
2373 |
return ForwardMap<Graph>(graph); |
|
2374 |
} |
|
2375 |
|
|
2376 |
/// \brief Returns the "backward" directed arc view of an edge. |
|
2377 |
/// |
|
2378 |
/// Returns the "backward" directed arc view of an edge. |
|
2379 |
/// \see ForwardMap |
|
2380 |
template <typename Graph> |
|
2381 |
class BackwardMap { |
|
2382 |
public: |
|
2383 |
|
|
2384 |
typedef typename Graph::Arc Value; |
|
2385 |
typedef typename Graph::Edge Key; |
|
2386 |
|
|
2387 |
/// \brief Constructor |
|
2388 |
/// |
|
2389 |
/// Constructor |
|
2390 |
/// \param _graph The graph that the map belongs to. |
|
2391 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
|
2392 |
|
|
2393 |
/// \brief The subscript operator. |
|
2394 |
/// |
|
2395 |
/// The subscript operator. |
|
2396 |
/// \param key An edge |
|
2397 |
/// \return The "backward" directed arc view of edge |
|
2398 |
Value operator[](const Key& key) const { |
|
2399 |
return _graph.direct(key, false); |
|
2400 |
} |
|
2401 |
|
|
2402 |
private: |
|
2403 |
const Graph& _graph; |
|
2404 |
}; |
|
2405 |
|
|
2406 |
/// \brief Returns a \ref BackwardMap class |
|
2407 |
|
|
2408 |
/// This function just returns a \ref BackwardMap class. |
|
2409 |
/// \relates BackwardMap |
|
2410 |
template <typename Graph> |
|
2411 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
|
2412 |
return BackwardMap<Graph>(graph); |
|
2413 |
} |
|
2414 |
|
|
2415 |
/// \brief Potential difference map |
|
2416 |
/// |
|
2417 |
/// If there is an potential map on the nodes then we |
|
2418 |
/// can get an arc map as we get the substraction of the |
|
2419 |
/// values of the target and source. |
|
2420 |
template <typename Digraph, typename NodeMap> |
|
2421 |
class PotentialDifferenceMap { |
|
2422 |
public: |
|
2423 |
typedef typename Digraph::Arc Key; |
|
2424 |
typedef typename NodeMap::Value Value; |
|
2425 |
|
|
2426 |
/// \brief Constructor |
|
2427 |
/// |
|
2428 |
/// Contructor of the map |
|
2429 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
|
2430 |
const NodeMap& potential) |
|
2431 |
: _digraph(digraph), _potential(potential) {} |
|
2432 |
|
|
2433 |
/// \brief Const subscription operator |
|
2434 |
/// |
|
2435 |
/// Const subscription operator |
|
2436 |
Value operator[](const Key& arc) const { |
|
2437 |
return _potential[_digraph.target(arc)] - |
|
2438 |
_potential[_digraph.source(arc)]; |
|
2439 |
} |
|
2440 |
|
|
2441 |
private: |
|
2442 |
const Digraph& _digraph; |
|
2443 |
const NodeMap& _potential; |
|
2444 |
}; |
|
2445 |
|
|
2446 |
/// \brief Returns a PotentialDifferenceMap. |
|
2447 |
/// |
|
2448 |
/// This function just returns a PotentialDifferenceMap. |
|
2449 |
/// \relates PotentialDifferenceMap |
|
2450 |
template <typename Digraph, typename NodeMap> |
|
2451 |
PotentialDifferenceMap<Digraph, NodeMap> |
|
2452 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
|
2453 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
|
2454 |
} |
|
2455 |
|
|
2456 |
/// \brief Map of the node in-degrees. |
|
2457 |
/// |
|
2458 |
/// This map returns the in-degree of a node. Once it is constructed, |
|
2459 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
2460 |
/// in constant time. On the other hand, the values are updated automatically |
|
2461 |
/// whenever the digraph changes. |
|
2462 |
/// |
|
2463 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
2464 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
|
2465 |
/// is not guarantied if these additional features are used. For example |
|
2466 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
2467 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
2468 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
2469 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
2470 |
/// |
|
2471 |
/// \sa OutDegMap |
|
2472 |
|
|
2473 |
template <typename _Digraph> |
|
2474 |
class InDegMap |
|
2475 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
2476 |
::ItemNotifier::ObserverBase { |
|
2477 |
|
|
2478 |
public: |
|
2479 |
|
|
2480 |
typedef _Digraph Digraph; |
|
2481 |
typedef int Value; |
|
2482 |
typedef typename Digraph::Node Key; |
|
2483 |
|
|
2484 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
2485 |
::ItemNotifier::ObserverBase Parent; |
|
2486 |
|
|
2487 |
private: |
|
2488 |
|
|
2489 |
class AutoNodeMap |
|
2490 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
|
2491 |
public: |
|
2492 |
|
|
2493 |
typedef typename ItemSetTraits<Digraph, Key>:: |
|
2494 |
template Map<int>::Type Parent; |
|
2495 |
|
|
2496 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
|
2497 |
|
|
2498 |
virtual void add(const Key& key) { |
|
2499 |
Parent::add(key); |
|
2500 |
Parent::set(key, 0); |
|
2501 |
} |
|
2502 |
|
|
2503 |
virtual void add(const std::vector<Key>& keys) { |
|
2504 |
Parent::add(keys); |
|
2505 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
2506 |
Parent::set(keys[i], 0); |
|
2507 |
} |
|
2508 |
} |
|
2509 |
|
|
2510 |
virtual void build() { |
|
2511 |
Parent::build(); |
|
2512 |
Key it; |
|
2513 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
2514 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2515 |
Parent::set(it, 0); |
|
2516 |
} |
|
2517 |
} |
|
2518 |
}; |
|
2519 |
|
|
2520 |
public: |
|
2521 |
|
|
2522 |
/// \brief Constructor. |
|
2523 |
/// |
|
2524 |
/// Constructor for creating in-degree map. |
|
2525 |
explicit InDegMap(const Digraph& digraph) |
|
2526 |
: _digraph(digraph), _deg(digraph) { |
|
2527 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
2528 |
|
|
2529 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2530 |
_deg[it] = countInArcs(_digraph, it); |
|
2531 |
} |
|
2532 |
} |
|
2533 |
|
|
2534 |
/// Gives back the in-degree of a Node. |
|
2535 |
int operator[](const Key& key) const { |
|
2536 |
return _deg[key]; |
|
2537 |
} |
|
2538 |
|
|
2539 |
protected: |
|
2540 |
|
|
2541 |
typedef typename Digraph::Arc Arc; |
|
2542 |
|
|
2543 |
virtual void add(const Arc& arc) { |
|
2544 |
++_deg[_digraph.target(arc)]; |
|
2545 |
} |
|
2546 |
|
|
2547 |
virtual void add(const std::vector<Arc>& arcs) { |
|
2548 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2549 |
++_deg[_digraph.target(arcs[i])]; |
|
2550 |
} |
|
2551 |
} |
|
2552 |
|
|
2553 |
virtual void erase(const Arc& arc) { |
|
2554 |
--_deg[_digraph.target(arc)]; |
|
2555 |
} |
|
2556 |
|
|
2557 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
2558 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2559 |
--_deg[_digraph.target(arcs[i])]; |
|
2560 |
} |
|
2561 |
} |
|
2562 |
|
|
2563 |
virtual void build() { |
|
2564 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2565 |
_deg[it] = countInArcs(_digraph, it); |
|
2566 |
} |
|
2567 |
} |
|
2568 |
|
|
2569 |
virtual void clear() { |
|
2570 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2571 |
_deg[it] = 0; |
|
2572 |
} |
|
2573 |
} |
|
2574 |
private: |
|
2575 |
|
|
2576 |
const Digraph& _digraph; |
|
2577 |
AutoNodeMap _deg; |
|
2578 |
}; |
|
2579 |
|
|
2580 |
/// \brief Map of the node out-degrees. |
|
2581 |
/// |
|
2582 |
/// This map returns the out-degree of a node. Once it is constructed, |
|
2583 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
2584 |
/// in constant time. On the other hand, the values are updated automatically |
|
2585 |
/// whenever the digraph changes. |
|
2586 |
/// |
|
2587 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
2588 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
|
2589 |
/// is not guarantied if these additional features are used. For example |
|
2590 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
2591 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
2592 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
2593 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
2594 |
/// |
|
2595 |
/// \sa InDegMap |
|
2596 |
|
|
2597 |
template <typename _Digraph> |
|
2598 |
class OutDegMap |
|
2599 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
2600 |
::ItemNotifier::ObserverBase { |
|
2601 |
|
|
2602 |
public: |
|
2603 |
|
|
2604 |
typedef _Digraph Digraph; |
|
2605 |
typedef int Value; |
|
2606 |
typedef typename Digraph::Node Key; |
|
2607 |
|
|
2608 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
2609 |
::ItemNotifier::ObserverBase Parent; |
|
2610 |
|
|
2611 |
private: |
|
2612 |
|
|
2613 |
class AutoNodeMap |
|
2614 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
|
2615 |
public: |
|
2616 |
|
|
2617 |
typedef typename ItemSetTraits<Digraph, Key>:: |
|
2618 |
template Map<int>::Type Parent; |
|
2619 |
|
|
2620 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
|
2621 |
|
|
2622 |
virtual void add(const Key& key) { |
|
2623 |
Parent::add(key); |
|
2624 |
Parent::set(key, 0); |
|
2625 |
} |
|
2626 |
virtual void add(const std::vector<Key>& keys) { |
|
2627 |
Parent::add(keys); |
|
2628 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
2629 |
Parent::set(keys[i], 0); |
|
2630 |
} |
|
2631 |
} |
|
2632 |
virtual void build() { |
|
2633 |
Parent::build(); |
|
2634 |
Key it; |
|
2635 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
2636 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2637 |
Parent::set(it, 0); |
|
2638 |
} |
|
2639 |
} |
|
2640 |
}; |
|
2641 |
|
|
2642 |
public: |
|
2643 |
|
|
2644 |
/// \brief Constructor. |
|
2645 |
/// |
|
2646 |
/// Constructor for creating out-degree map. |
|
2647 |
explicit OutDegMap(const Digraph& digraph) |
|
2648 |
: _digraph(digraph), _deg(digraph) { |
|
2649 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
2650 |
|
|
2651 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2652 |
_deg[it] = countOutArcs(_digraph, it); |
|
2653 |
} |
|
2654 |
} |
|
2655 |
|
|
2656 |
/// Gives back the out-degree of a Node. |
|
2657 |
int operator[](const Key& key) const { |
|
2658 |
return _deg[key]; |
|
2659 |
} |
|
2660 |
|
|
2661 |
protected: |
|
2662 |
|
|
2663 |
typedef typename Digraph::Arc Arc; |
|
2664 |
|
|
2665 |
virtual void add(const Arc& arc) { |
|
2666 |
++_deg[_digraph.source(arc)]; |
|
2667 |
} |
|
2668 |
|
|
2669 |
virtual void add(const std::vector<Arc>& arcs) { |
|
2670 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2671 |
++_deg[_digraph.source(arcs[i])]; |
|
2672 |
} |
|
2673 |
} |
|
2674 |
|
|
2675 |
virtual void erase(const Arc& arc) { |
|
2676 |
--_deg[_digraph.source(arc)]; |
|
2677 |
} |
|
2678 |
|
|
2679 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
2680 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2681 |
--_deg[_digraph.source(arcs[i])]; |
|
2682 |
} |
|
2683 |
} |
|
2684 |
|
|
2685 |
virtual void build() { |
|
2686 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2687 |
_deg[it] = countOutArcs(_digraph, it); |
|
2688 |
} |
|
2689 |
} |
|
2690 |
|
|
2691 |
virtual void clear() { |
|
2692 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2693 |
_deg[it] = 0; |
|
2694 |
} |
|
2695 |
} |
|
2696 |
private: |
|
2697 |
|
|
2698 |
const Digraph& _digraph; |
|
2699 |
AutoNodeMap _deg; |
|
2700 |
}; |
|
2701 |
|
|
1783 | 2702 |
/// @} |
1784 | 2703 |
} |
1785 | 2704 |
... | ... |
@@ -25,14 +25,8 @@ |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 |
#include <lemon/bits/invalid.h> |
|
29 |
|
|
30 |
#include <lemon/bits/base_extender.h> |
|
31 |
#include <lemon/bits/graph_extender.h> |
|
32 |
|
|
33 |
#include <lemon/bits/utility.h> |
|
28 |
#include <lemon/core.h> |
|
34 | 29 |
#include <lemon/error.h> |
35 |
|
|
36 | 30 |
#include <lemon/bits/graph_extender.h> |
37 | 31 |
|
38 | 32 |
namespace lemon { |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_INVALID_H |
|
20 |
#define LEMON_BITS_INVALID_H |
|
21 |
|
|
22 |
///\file |
|
23 |
///\brief Definition of INVALID. |
|
24 |
|
|
25 |
namespace lemon { |
|
26 |
|
|
27 |
/// \brief Dummy type to make it easier to create invalid iterators. |
|
28 |
/// |
|
29 |
/// Dummy type to make it easier to create invalid iterators. |
|
30 |
/// See \ref INVALID for the usage. |
|
31 |
struct Invalid { |
|
32 |
public: |
|
33 |
bool operator==(Invalid) { return true; } |
|
34 |
bool operator!=(Invalid) { return false; } |
|
35 |
bool operator< (Invalid) { return false; } |
|
36 |
}; |
|
37 |
|
|
38 |
/// \brief Invalid iterators. |
|
39 |
/// |
|
40 |
/// \ref Invalid is a global type that converts to each iterator |
|
41 |
/// in such a way that the value of the target iterator will be invalid. |
|
42 |
|
|
43 |
//Some people didn't like this: |
|
44 |
//const Invalid &INVALID = *(Invalid *)0; |
|
45 |
|
|
46 |
#ifdef LEMON_ONLY_TEMPLATES |
|
47 |
const Invalid INVALID = Invalid(); |
|
48 |
#else |
|
49 |
extern const Invalid INVALID; |
|
50 |
#endif |
|
51 |
|
|
52 |
} //namespace lemon |
|
53 |
|
|
54 |
#endif |
|
55 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
// This file contains a modified version of the enable_if library from BOOST. |
|
20 |
// See the appropriate copyright notice below. |
|
21 |
|
|
22 |
// Boost enable_if library |
|
23 |
|
|
24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
|
25 |
|
|
26 |
// Use, modification, and distribution is subject to the Boost Software |
|
27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|
28 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
29 |
|
|
30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
|
31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
|
32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
|
33 |
|
|
34 |
|
|
35 |
#ifndef LEMON_BITS_UTILITY_H |
|
36 |
#define LEMON_BITS_UTILITY_H |
|
37 |
|
|
38 |
///\file |
|
39 |
///\brief Miscellaneous basic utilities |
|
40 |
/// |
|
41 |
///\todo Please rethink the organisation of the basic files like this. |
|
42 |
///E.g. this file might be merged with invalid.h. |
|
43 |
|
|
44 |
|
|
45 |
namespace lemon |
|
46 |
{ |
|
47 |
|
|
48 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
49 |
|
|
50 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
51 |
/// |
|
52 |
///\sa False |
|
53 |
/// |
|
54 |
/// \todo This should go to a separate "basic_types.h" (or something) |
|
55 |
/// file. |
|
56 |
struct True { |
|
57 |
///\e |
|
58 |
static const bool value = true; |
|
59 |
}; |
|
60 |
|
|
61 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
62 |
|
|
63 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
64 |
/// |
|
65 |
///\sa True |
|
66 |
struct False { |
|
67 |
///\e |
|
68 |
static const bool value = false; |
|
69 |
}; |
|
70 |
|
|
71 |
|
|
72 |
struct InvalidType { |
|
73 |
}; |
|
74 |
|
|
75 |
template <typename T> |
|
76 |
struct Wrap { |
|
77 |
const T &value; |
|
78 |
Wrap(const T &t) : value(t) {} |
|
79 |
}; |
|
80 |
|
|
81 |
/**************** dummy class to avoid ambiguity ****************/ |
|
82 |
|
|
83 |
template<int T> struct dummy { dummy(int) {} }; |
|
84 |
|
|
85 |
/**************** enable_if from BOOST ****************/ |
|
86 |
|
|
87 |
template <typename Type, typename T = void> |
|
88 |
struct exists { |
|
89 |
typedef T type; |
|
90 |
}; |
|
91 |
|
|
92 |
|
|
93 |
template <bool B, class T = void> |
|
94 |
struct enable_if_c { |
|
95 |
typedef T type; |
|
96 |
}; |
|
97 |
|
|
98 |
template <class T> |
|
99 |
struct enable_if_c<false, T> {}; |
|
100 |
|
|
101 |
template <class Cond, class T = void> |
|
102 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
|
103 |
|
|
104 |
template <bool B, class T> |
|
105 |
struct lazy_enable_if_c { |
|
106 |
typedef typename T::type type; |
|
107 |
}; |
|
108 |
|
|
109 |
template <class T> |
|
110 |
struct lazy_enable_if_c<false, T> {}; |
|
111 |
|
|
112 |
template <class Cond, class T> |
|
113 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
|
114 |
|
|
115 |
|
|
116 |
template <bool B, class T = void> |
|
117 |
struct disable_if_c { |
|
118 |
typedef T type; |
|
119 |
}; |
|
120 |
|
|
121 |
template <class T> |
|
122 |
struct disable_if_c<true, T> {}; |
|
123 |
|
|
124 |
template <class Cond, class T = void> |
|
125 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
|
126 |
|
|
127 |
template <bool B, class T> |
|
128 |
struct lazy_disable_if_c { |
|
129 |
typedef typename T::type type; |
|
130 |
}; |
|
131 |
|
|
132 |
template <class T> |
|
133 |
struct lazy_disable_if_c<true, T> {}; |
|
134 |
|
|
135 |
template <class Cond, class T> |
|
136 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
|
137 |
|
|
138 |
} // namespace lemon |
|
139 |
|
|
140 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_GRAPH_UTILS_H |
|
20 |
#define LEMON_GRAPH_UTILS_H |
|
21 |
|
|
22 |
#include <iterator> |
|
23 |
#include <vector> |
|
24 |
#include <map> |
|
25 |
#include <cmath> |
|
26 |
#include <algorithm> |
|
27 |
|
|
28 |
#include <lemon/bits/invalid.h> |
|
29 |
#include <lemon/bits/utility.h> |
|
30 |
#include <lemon/maps.h> |
|
31 |
#include <lemon/bits/traits.h> |
|
32 |
|
|
33 |
#include <lemon/bits/alteration_notifier.h> |
|
34 |
#include <lemon/bits/default_map.h> |
|
35 |
|
|
36 |
///\ingroup gutils |
|
37 |
///\file |
|
38 |
///\brief Graph utilities. |
|
39 |
|
|
40 |
namespace lemon { |
|
41 |
|
|
42 |
/// \addtogroup gutils |
|
43 |
/// @{ |
|
44 |
|
|
45 |
///Creates convenience typedefs for the digraph types and iterators |
|
46 |
|
|
47 |
///This \c \#define creates convenience typedefs for the following types |
|
48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
49 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
|
50 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
|
51 |
/// |
|
52 |
///\note If the graph type is a dependent type, ie. the graph type depend |
|
53 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
|
54 |
///macro. |
|
55 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
|
56 |
typedef Digraph::Node Node; \ |
|
57 |
typedef Digraph::NodeIt NodeIt; \ |
|
58 |
typedef Digraph::Arc Arc; \ |
|
59 |
typedef Digraph::ArcIt ArcIt; \ |
|
60 |
typedef Digraph::InArcIt InArcIt; \ |
|
61 |
typedef Digraph::OutArcIt OutArcIt; \ |
|
62 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
|
63 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
|
64 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
|
65 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
|
66 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
|
67 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
68 |
|
|
69 |
///Creates convenience typedefs for the digraph types and iterators |
|
70 |
|
|
71 |
///\see DIGRAPH_TYPEDEFS |
|
72 |
/// |
|
73 |
///\note Use this macro, if the graph type is a dependent type, |
|
74 |
///ie. the graph type depend on a template parameter. |
|
75 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
|
76 |
typedef typename Digraph::Node Node; \ |
|
77 |
typedef typename Digraph::NodeIt NodeIt; \ |
|
78 |
typedef typename Digraph::Arc Arc; \ |
|
79 |
typedef typename Digraph::ArcIt ArcIt; \ |
|
80 |
typedef typename Digraph::InArcIt InArcIt; \ |
|
81 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
|
82 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
|
83 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
|
84 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
|
85 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
|
86 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
|
87 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
88 |
|
|
89 |
///Creates convenience typedefs for the graph types and iterators |
|
90 |
|
|
91 |
///This \c \#define creates the same convenience typedefs as defined |
|
92 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
|
93 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
|
94 |
///\c DoubleEdgeMap. |
|
95 |
/// |
|
96 |
///\note If the graph type is a dependent type, ie. the graph type depend |
|
97 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
|
98 |
///macro. |
|
99 |
#define GRAPH_TYPEDEFS(Graph) \ |
|
100 |
DIGRAPH_TYPEDEFS(Graph); \ |
|
101 |
typedef Graph::Edge Edge; \ |
|
102 |
typedef Graph::EdgeIt EdgeIt; \ |
|
103 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
|
104 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
|
105 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
|
106 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
107 |
|
|
108 |
///Creates convenience typedefs for the graph types and iterators |
|
109 |
|
|
110 |
///\see GRAPH_TYPEDEFS |
|
111 |
/// |
|
112 |
///\note Use this macro, if the graph type is a dependent type, |
|
113 |
///ie. the graph type depend on a template parameter. |
|
114 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
|
115 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
|
116 |
typedef typename Graph::Edge Edge; \ |
|
117 |
typedef typename Graph::EdgeIt EdgeIt; \ |
|
118 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
|
119 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
|
120 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
|
121 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
122 |
|
|
123 |
/// \brief Function to count the items in the graph. |
|
124 |
/// |
|
125 |
/// This function counts the items (nodes, arcs etc) in the graph. |
|
126 |
/// The complexity of the function is O(n) because |
|
127 |
/// it iterates on all of the items. |
|
128 |
template <typename Graph, typename Item> |
|
129 |
inline int countItems(const Graph& g) { |
|
130 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
|
131 |
int num = 0; |
|
132 |
for (ItemIt it(g); it != INVALID; ++it) { |
|
133 |
++num; |
|
134 |
} |
|
135 |
return num; |
|
136 |
} |
|
137 |
|
|
138 |
// Node counting: |
|
139 |
|
|
140 |
namespace _graph_utils_bits { |
|
141 |
|
|
142 |
template <typename Graph, typename Enable = void> |
|
143 |
struct CountNodesSelector { |
|
144 |
static int count(const Graph &g) { |
|
145 |
return countItems<Graph, typename Graph::Node>(g); |
|
146 |
} |
|
147 |
}; |
|
148 |
|
|
149 |
template <typename Graph> |
|
150 |
struct CountNodesSelector< |
|
151 |
Graph, typename |
|
152 |
enable_if<typename Graph::NodeNumTag, void>::type> |
|
153 |
{ |
|
154 |
static int count(const Graph &g) { |
|
155 |
return g.nodeNum(); |
|
156 |
} |
|
157 |
}; |
|
158 |
} |
|
159 |
|
|
160 |
/// \brief Function to count the nodes in the graph. |
|
161 |
/// |
|
162 |
/// This function counts the nodes in the graph. |
|
163 |
/// The complexity of the function is O(n) but for some |
|
164 |
/// graph structures it is specialized to run in O(1). |
|
165 |
/// |
|
166 |
/// If the graph contains a \e nodeNum() member function and a |
|
167 |
/// \e NodeNumTag tag then this function calls directly the member |
|
168 |
/// function to query the cardinality of the node set. |
|
169 |
template <typename Graph> |
|
170 |
inline int countNodes(const Graph& g) { |
|
171 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
|
172 |
} |
|
173 |
|
|
174 |
// Arc counting: |
|
175 |
|
|
176 |
namespace _graph_utils_bits { |
|
177 |
|
|
178 |
template <typename Graph, typename Enable = void> |
|
179 |
struct CountArcsSelector { |
|
180 |
static int count(const Graph &g) { |
|
181 |
return countItems<Graph, typename Graph::Arc>(g); |
|
182 |
} |
|
183 |
}; |
|
184 |
|
|
185 |
template <typename Graph> |
|
186 |
struct CountArcsSelector< |
|
187 |
Graph, |
|
188 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
|
189 |
{ |
|
190 |
static int count(const Graph &g) { |
|
191 |
return g.arcNum(); |
|
192 |
} |
|
193 |
}; |
|
194 |
} |
|
195 |
|
|
196 |
/// \brief Function to count the arcs in the graph. |
|
197 |
/// |
|
198 |
/// This function counts the arcs in the graph. |
|
199 |
/// The complexity of the function is O(e) but for some |
|
200 |
/// graph structures it is specialized to run in O(1). |
|
201 |
/// |
|
202 |
/// If the graph contains a \e arcNum() member function and a |
|
203 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
204 |
/// function to query the cardinality of the arc set. |
|
205 |
template <typename Graph> |
|
206 |
inline int countArcs(const Graph& g) { |
|
207 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
|
208 |
} |
|
209 |
|
|
210 |
// Edge counting: |
|
211 |
namespace _graph_utils_bits { |
|
212 |
|
|
213 |
template <typename Graph, typename Enable = void> |
|
214 |
struct CountEdgesSelector { |
|
215 |
static int count(const Graph &g) { |
|
216 |
return countItems<Graph, typename Graph::Edge>(g); |
|
217 |
} |
|
218 |
}; |
|
219 |
|
|
220 |
template <typename Graph> |
|
221 |
struct CountEdgesSelector< |
|
222 |
Graph, |
|
223 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
|
224 |
{ |
|
225 |
static int count(const Graph &g) { |
|
226 |
return g.edgeNum(); |
|
227 |
} |
|
228 |
}; |
|
229 |
} |
|
230 |
|
|
231 |
/// \brief Function to count the edges in the graph. |
|
232 |
/// |
|
233 |
/// This function counts the edges in the graph. |
|
234 |
/// The complexity of the function is O(m) but for some |
|
235 |
/// graph structures it is specialized to run in O(1). |
|
236 |
/// |
|
237 |
/// If the graph contains a \e edgeNum() member function and a |
|
238 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
239 |
/// function to query the cardinality of the edge set. |
|
240 |
template <typename Graph> |
|
241 |
inline int countEdges(const Graph& g) { |
|
242 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
|
243 |
|
|
244 |
} |
|
245 |
|
|
246 |
|
|
247 |
template <typename Graph, typename DegIt> |
|
248 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
|
249 |
int num = 0; |
|
250 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
|
251 |
++num; |
|
252 |
} |
|
253 |
return num; |
|
254 |
} |
|
255 |
|
|
256 |
/// \brief Function to count the number of the out-arcs from node \c n. |
|
257 |
/// |
|
258 |
/// This function counts the number of the out-arcs from node \c n |
|
259 |
/// in the graph. |
|
260 |
template <typename Graph> |
|
261 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
262 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
|
263 |
} |
|
264 |
|
|
265 |
/// \brief Function to count the number of the in-arcs to node \c n. |
|
266 |
/// |
|
267 |
/// This function counts the number of the in-arcs to node \c n |
|
268 |
/// in the graph. |
|
269 |
template <typename Graph> |
|
270 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
271 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
|
272 |
} |
|
273 |
|
|
274 |
/// \brief Function to count the number of the inc-edges to node \c n. |
|
275 |
/// |
|
276 |
/// This function counts the number of the inc-edges to node \c n |
|
277 |
/// in the graph. |
|
278 |
template <typename Graph> |
|
279 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
|
280 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
|
281 |
} |
|
282 |
|
|
283 |
namespace _graph_utils_bits { |
|
284 |
|
|
285 |
template <typename Graph, typename Enable = void> |
|
286 |
struct FindArcSelector { |
|
287 |
typedef typename Graph::Node Node; |
|
288 |
typedef typename Graph::Arc Arc; |
|
289 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
|
290 |
if (e == INVALID) { |
|
291 |
g.firstOut(e, u); |
|
292 |
} else { |
|
293 |
g.nextOut(e); |
|
294 |
} |
|
295 |
while (e != INVALID && g.target(e) != v) { |
|
296 |
g.nextOut(e); |
|
297 |
} |
|
298 |
return e; |
|
299 |
} |
|
300 |
}; |
|
301 |
|
|
302 |
template <typename Graph> |
|
303 |
struct FindArcSelector< |
|
304 |
Graph, |
|
305 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
306 |
{ |
|
307 |
typedef typename Graph::Node Node; |
|
308 |
typedef typename Graph::Arc Arc; |
|
309 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
|
310 |
return g.findArc(u, v, prev); |
|
311 |
} |
|
312 |
}; |
|
313 |
} |
|
314 |
|
|
315 |
/// \brief Finds an arc between two nodes of a graph. |
|
316 |
/// |
|
317 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
|
318 |
/// |
|
319 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
320 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
321 |
/// the next arc from \c u to \c v after \c prev. |
|
322 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
323 |
/// |
|
324 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
325 |
///\code |
|
326 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
|
327 |
/// ... |
|
328 |
/// } |
|
329 |
///\endcode |
|
330 |
/// |
|
331 |
///\sa ArcLookUp |
|
332 |
///\sa AllArcLookUp |
|
333 |
///\sa DynArcLookUp |
|
334 |
///\sa ConArcIt |
|
335 |
template <typename Graph> |
|
336 |
inline typename Graph::Arc |
|
337 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
338 |
typename Graph::Arc prev = INVALID) { |
|
339 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
|
340 |
} |
|
341 |
|
|
342 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
|
343 |
/// |
|
344 |
/// Iterator for iterating on arcs connected the same nodes. It is |
|
345 |
/// higher level interface for the findArc() function. You can |
|
346 |
/// use it the following way: |
|
347 |
///\code |
|
348 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
349 |
/// ... |
|
350 |
/// } |
|
351 |
///\endcode |
|
352 |
/// |
|
353 |
///\sa findArc() |
|
354 |
///\sa ArcLookUp |
|
355 |
///\sa AllArcLookUp |
|
356 |
///\sa DynArcLookUp |
|
357 |
template <typename _Graph> |
|
358 |
class ConArcIt : public _Graph::Arc { |
|
359 |
public: |
|
360 |
|
|
361 |
typedef _Graph Graph; |
|
362 |
typedef typename Graph::Arc Parent; |
|
363 |
|
|
364 |
typedef typename Graph::Arc Arc; |
|
365 |
typedef typename Graph::Node Node; |
|
366 |
|
|
367 |
/// \brief Constructor. |
|
368 |
/// |
|
369 |
/// Construct a new ConArcIt iterating on the arcs which |
|
370 |
/// connects the \c u and \c v node. |
|
371 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
372 |
Parent::operator=(findArc(_graph, u, v)); |
|
373 |
} |
|
374 |
|
|
375 |
/// \brief Constructor. |
|
376 |
/// |
|
377 |
/// Construct a new ConArcIt which continues the iterating from |
|
378 |
/// the \c e arc. |
|
379 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
|
380 |
|
|
381 |
/// \brief Increment operator. |
|
382 |
/// |
|
383 |
/// It increments the iterator and gives back the next arc. |
|
384 |
ConArcIt& operator++() { |
|
385 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
|
386 |
_graph.target(*this), *this)); |
|
387 |
return *this; |
|
388 |
} |
|
389 |
private: |
|
390 |
const Graph& _graph; |
|
391 |
}; |
|
392 |
|
|
393 |
namespace _graph_utils_bits { |
|
394 |
|
|
395 |
template <typename Graph, typename Enable = void> |
|
396 |
struct FindEdgeSelector { |
|
397 |
typedef typename Graph::Node Node; |
|
398 |
typedef typename Graph::Edge Edge; |
|
399 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
|
400 |
bool b; |
|
401 |
if (u != v) { |
|
402 |
if (e == INVALID) { |
|
403 |
g.firstInc(e, b, u); |
|
404 |
} else { |
|
405 |
b = g.u(e) == u; |
|
406 |
g.nextInc(e, b); |
|
407 |
} |
|
408 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
|
409 |
g.nextInc(e, b); |
|
410 |
} |
|
411 |
} else { |
|
412 |
if (e == INVALID) { |
|
413 |
g.firstInc(e, b, u); |
|
414 |
} else { |
|
415 |
b = true; |
|
416 |
g.nextInc(e, b); |
|
417 |
} |
|
418 |
while (e != INVALID && (!b || g.v(e) != v)) { |
|
419 |
g.nextInc(e, b); |
|
420 |
} |
|
421 |
} |
|
422 |
return e; |
|
423 |
} |
|
424 |
}; |
|
425 |
|
|
426 |
template <typename Graph> |
|
427 |
struct FindEdgeSelector< |
|
428 |
Graph, |
|
429 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
430 |
{ |
|
431 |
typedef typename Graph::Node Node; |
|
432 |
typedef typename Graph::Edge Edge; |
|
433 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
|
434 |
return g.findEdge(u, v, prev); |
|
435 |
} |
|
436 |
}; |
|
437 |
} |
|
438 |
|
|
439 |
/// \brief Finds an edge between two nodes of a graph. |
|
440 |
/// |
|
441 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
|
442 |
/// If the node \c u and node \c v is equal then each loop edge |
|
443 |
/// will be enumerated once. |
|
444 |
/// |
|
445 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
446 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
447 |
/// the next arc from \c u to \c v after \c prev. |
|
448 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
449 |
/// |
|
450 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
451 |
///\code |
|
452 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
|
453 |
/// e = findEdge(g,u,v,e)) { |
|
454 |
/// ... |
|
455 |
/// } |
|
456 |
///\endcode |
|
457 |
/// |
|
458 |
///\sa ConEdgeIt |
|
459 |
|
|
460 |
template <typename Graph> |
|
461 |
inline typename Graph::Edge |
|
462 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
463 |
typename Graph::Edge p = INVALID) { |
|
464 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
|
465 |
} |
|
466 |
|
|
467 |
/// \brief Iterator for iterating on edges connected the same nodes. |
|
468 |
/// |
|
469 |
/// Iterator for iterating on edges connected the same nodes. It is |
|
470 |
/// higher level interface for the findEdge() function. You can |
|
471 |
/// use it the following way: |
|
472 |
///\code |
|
473 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
474 |
/// ... |
|
475 |
/// } |
|
476 |
///\endcode |
|
477 |
/// |
|
478 |
///\sa findEdge() |
|
479 |
template <typename _Graph> |
|
480 |
class ConEdgeIt : public _Graph::Edge { |
|
481 |
public: |
|
482 |
|
|
483 |
typedef _Graph Graph; |
|
484 |
typedef typename Graph::Edge Parent; |
|
485 |
|
|
486 |
typedef typename Graph::Edge Edge; |
|
487 |
typedef typename Graph::Node Node; |
|
488 |
|
|
489 |
/// \brief Constructor. |
|
490 |
/// |
|
491 |
/// Construct a new ConEdgeIt iterating on the edges which |
|
492 |
/// connects the \c u and \c v node. |
|
493 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
494 |
Parent::operator=(findEdge(_graph, u, v)); |
|
495 |
} |
|
496 |
|
|
497 |
/// \brief Constructor. |
|
498 |
/// |
|
499 |
/// Construct a new ConEdgeIt which continues the iterating from |
|
500 |
/// the \c e edge. |
|
501 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
|
502 |
|
|
503 |
/// \brief Increment operator. |
|
504 |
/// |
|
505 |
/// It increments the iterator and gives back the next edge. |
|
506 |
ConEdgeIt& operator++() { |
|
507 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
|
508 |
_graph.v(*this), *this)); |
|
509 |
return *this; |
|
510 |
} |
|
511 |
private: |
|
512 |
const Graph& _graph; |
|
513 |
}; |
|
514 |
|
|
515 |
namespace _graph_utils_bits { |
|
516 |
|
|
517 |
template <typename Digraph, typename Item, typename RefMap> |
|
518 |
class MapCopyBase { |
|
519 |
public: |
|
520 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
|
521 |
|
|
522 |
virtual ~MapCopyBase() {} |
|
523 |
}; |
|
524 |
|
|
525 |
template <typename Digraph, typename Item, typename RefMap, |
|
526 |
typename ToMap, typename FromMap> |
|
527 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
528 |
public: |
|
529 |
|
|
530 |
MapCopy(ToMap& tmap, const FromMap& map) |
|
531 |
: _tmap(tmap), _map(map) {} |
|
532 |
|
|
533 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
534 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
535 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
536 |
_tmap.set(refMap[it], _map[it]); |
|
537 |
} |
|
538 |
} |
|
539 |
|
|
540 |
private: |
|
541 |
ToMap& _tmap; |
|
542 |
const FromMap& _map; |
|
543 |
}; |
|
544 |
|
|
545 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
|
546 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
547 |
public: |
|
548 |
|
|
549 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
|
550 |
|
|
551 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
|
552 |
_it = refMap[_item]; |
|
553 |
} |
|
554 |
|
|
555 |
private: |
|
556 |
It& _it; |
|
557 |
Item _item; |
|
558 |
}; |
|
559 |
|
|
560 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
|
561 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
562 |
public: |
|
563 |
|
|
564 |
RefCopy(Ref& map) : _map(map) {} |
|
565 |
|
|
566 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
567 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
568 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
569 |
_map.set(it, refMap[it]); |
|
570 |
} |
|
571 |
} |
|
572 |
|
|
573 |
private: |
|
574 |
Ref& _map; |
|
575 |
}; |
|
576 |
|
|
577 |
template <typename Digraph, typename Item, typename RefMap, |
|
578 |
typename CrossRef> |
|
579 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
|
580 |
public: |
|
581 |
|
|
582 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
|
583 |
|
|
584 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
|
585 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
586 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
|
587 |
_cmap.set(refMap[it], it); |
|
588 |
} |
|
589 |
} |
|
590 |
|
|
591 |
private: |
|
592 |
CrossRef& _cmap; |
|
593 |
}; |
|
594 |
|
|
595 |
template <typename Digraph, typename Enable = void> |
|
596 |
struct DigraphCopySelector { |
|
597 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
598 |
static void copy(Digraph &to, const From& from, |
|
599 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
|
600 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
|
601 |
nodeRefMap[it] = to.addNode(); |
|
602 |
} |
|
603 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
|
604 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
|
605 |
nodeRefMap[from.target(it)]); |
|
606 |
} |
|
607 |
} |
|
608 |
}; |
|
609 |
|
|
610 |
template <typename Digraph> |
|
611 |
struct DigraphCopySelector< |
|
612 |
Digraph, |
|
613 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
|
614 |
{ |
|
615 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
616 |
static void copy(Digraph &to, const From& from, |
|
617 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
|
618 |
to.build(from, nodeRefMap, arcRefMap); |
|
619 |
} |
|
620 |
}; |
|
621 |
|
|
622 |
template <typename Graph, typename Enable = void> |
|
623 |
struct GraphCopySelector { |
|
624 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
625 |
static void copy(Graph &to, const From& from, |
|
626 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
|
627 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
|
628 |
nodeRefMap[it] = to.addNode(); |
|
629 |
} |
|
630 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
|
631 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
|
632 |
nodeRefMap[from.v(it)]); |
|
633 |
} |
|
634 |
} |
|
635 |
}; |
|
636 |
|
|
637 |
template <typename Graph> |
|
638 |
struct GraphCopySelector< |
|
639 |
Graph, |
|
640 |
typename enable_if<typename Graph::BuildTag, void>::type> |
|
641 |
{ |
|
642 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
643 |
static void copy(Graph &to, const From& from, |
|
644 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
|
645 |
to.build(from, nodeRefMap, edgeRefMap); |
|
646 |
} |
|
647 |
}; |
|
648 |
|
|
649 |
} |
|
650 |
|
|
651 |
/// \brief Class to copy a digraph. |
|
652 |
/// |
|
653 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
|
654 |
/// simplest way of using it is through the \c copyDigraph() function. |
|
655 |
/// |
|
656 |
/// This class not just make a copy of a graph, but it can create |
|
657 |
/// references and cross references between the nodes and arcs of |
|
658 |
/// the two graphs, it can copy maps for use with the newly created |
|
659 |
/// graph and copy nodes and arcs. |
|
660 |
/// |
|
661 |
/// To make a copy from a graph, first an instance of DigraphCopy |
|
662 |
/// should be created, then the data belongs to the graph should |
|
663 |
/// assigned to copy. In the end, the \c run() member should be |
|
664 |
/// called. |
|
665 |
/// |
|
666 |
/// The next code copies a graph with several data: |
|
667 |
///\code |
|
668 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
669 |
/// // create a reference for the nodes |
|
670 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
671 |
/// dc.nodeRef(nr); |
|
672 |
/// // create a cross reference (inverse) for the arcs |
|
673 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
|
674 |
/// dc.arcCrossRef(acr); |
|
675 |
/// // copy an arc map |
|
676 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
677 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
678 |
/// dc.arcMap(namap, oamap); |
|
679 |
/// // copy a node |
|
680 |
/// OrigGraph::Node on; |
|
681 |
/// NewGraph::Node nn; |
|
682 |
/// dc.node(nn, on); |
|
683 |
/// // Executions of copy |
|
684 |
/// dc.run(); |
|
685 |
///\endcode |
|
686 |
template <typename To, typename From> |
|
687 |
class DigraphCopy { |
|
688 |
private: |
|
689 |
|
|
690 |
typedef typename From::Node Node; |
|
691 |
typedef typename From::NodeIt NodeIt; |
|
692 |
typedef typename From::Arc Arc; |
|
693 |
typedef typename From::ArcIt ArcIt; |
|
694 |
|
|
695 |
typedef typename To::Node TNode; |
|
696 |
typedef typename To::Arc TArc; |
|
697 |
|
|
698 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
699 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
|
700 |
|
|
701 |
|
|
702 |
public: |
|
703 |
|
|
704 |
|
|
705 |
/// \brief Constructor for the DigraphCopy. |
|
706 |
/// |
|
707 |
/// It copies the content of the \c _from digraph into the |
|
708 |
/// \c _to digraph. |
|
709 |
DigraphCopy(To& to, const From& from) |
|
710 |
: _from(from), _to(to) {} |
|
711 |
|
|
712 |
/// \brief Destructor of the DigraphCopy |
|
713 |
/// |
|
714 |
/// Destructor of the DigraphCopy |
|
715 |
~DigraphCopy() { |
|
716 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
717 |
delete _node_maps[i]; |
|
718 |
} |
|
719 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
720 |
delete _arc_maps[i]; |
|
721 |
} |
|
722 |
|
|
723 |
} |
|
724 |
|
|
725 |
/// \brief Copies the node references into the given map. |
|
726 |
/// |
|
727 |
/// Copies the node references into the given map. The parameter |
|
728 |
/// should be a map, which key type is the Node type of the source |
|
729 |
/// graph, while the value type is the Node type of the |
|
730 |
/// destination graph. |
|
731 |
template <typename NodeRef> |
|
732 |
DigraphCopy& nodeRef(NodeRef& map) { |
|
733 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
|
734 |
NodeRefMap, NodeRef>(map)); |
|
735 |
return *this; |
|
736 |
} |
|
737 |
|
|
738 |
/// \brief Copies the node cross references into the given map. |
|
739 |
/// |
|
740 |
/// Copies the node cross references (reverse references) into |
|
741 |
/// the given map. The parameter should be a map, which key type |
|
742 |
/// is the Node type of the destination graph, while the value type is |
|
743 |
/// the Node type of the source graph. |
|
744 |
template <typename NodeCrossRef> |
|
745 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
|
746 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
|
747 |
NodeRefMap, NodeCrossRef>(map)); |
|
748 |
return *this; |
|
749 |
} |
|
750 |
|
|
751 |
/// \brief Make copy of the given map. |
|
752 |
/// |
|
753 |
/// Makes copy of the given map for the newly created digraph. |
|
754 |
/// The new map's key type is the destination graph's node type, |
|
755 |
/// and the copied map's key type is the source graph's node type. |
|
756 |
template <typename ToMap, typename FromMap> |
|
757 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
758 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
|
759 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
760 |
return *this; |
|
761 |
} |
|
762 |
|
|
763 |
/// \brief Make a copy of the given node. |
|
764 |
/// |
|
765 |
/// Make a copy of the given node. |
|
766 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
|
767 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
|
768 |
NodeRefMap, TNode>(tnode, snode)); |
|
769 |
return *this; |
|
770 |
} |
|
771 |
|
|
772 |
/// \brief Copies the arc references into the given map. |
|
773 |
/// |
|
774 |
/// Copies the arc references into the given map. |
|
775 |
template <typename ArcRef> |
|
776 |
DigraphCopy& arcRef(ArcRef& map) { |
|
777 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
|
778 |
ArcRefMap, ArcRef>(map)); |
|
779 |
return *this; |
|
780 |
} |
|
781 |
|
|
782 |
/// \brief Copies the arc cross references into the given map. |
|
783 |
/// |
|
784 |
/// Copies the arc cross references (reverse references) into |
|
785 |
/// the given map. |
|
786 |
template <typename ArcCrossRef> |
|
787 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
|
788 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
|
789 |
ArcRefMap, ArcCrossRef>(map)); |
|
790 |
return *this; |
|
791 |
} |
|
792 |
|
|
793 |
/// \brief Make copy of the given map. |
|
794 |
/// |
|
795 |
/// Makes copy of the given map for the newly created digraph. |
|
796 |
/// The new map's key type is the to digraph's arc type, |
|
797 |
/// and the copied map's key type is the from digraph's arc |
|
798 |
/// type. |
|
799 |
template <typename ToMap, typename FromMap> |
|
800 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
801 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
|
802 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
803 |
return *this; |
|
804 |
} |
|
805 |
|
|
806 |
/// \brief Make a copy of the given arc. |
|
807 |
/// |
|
808 |
/// Make a copy of the given arc. |
|
809 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
810 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
|
811 |
ArcRefMap, TArc>(tarc, sarc)); |
|
812 |
return *this; |
|
813 |
} |
|
814 |
|
|
815 |
/// \brief Executes the copies. |
|
816 |
/// |
|
817 |
/// Executes the copies. |
|
818 |
void run() { |
|
819 |
NodeRefMap nodeRefMap(_from); |
|
820 |
ArcRefMap arcRefMap(_from); |
|
821 |
_graph_utils_bits::DigraphCopySelector<To>:: |
|
822 |
copy(_to, _from, nodeRefMap, arcRefMap); |
|
823 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
824 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
825 |
} |
|
826 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
827 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
828 |
} |
|
829 |
} |
|
830 |
|
|
831 |
protected: |
|
832 |
|
|
833 |
|
|
834 |
const From& _from; |
|
835 |
To& _to; |
|
836 |
|
|
837 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
838 |
_node_maps; |
|
839 |
|
|
840 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
841 |
_arc_maps; |
|
842 |
|
|
843 |
}; |
|
844 |
|
|
845 |
/// \brief Copy a digraph to another digraph. |
|
846 |
/// |
|
847 |
/// Copy a digraph to another digraph. The complete usage of the |
|
848 |
/// function is detailed in the DigraphCopy class, but a short |
|
849 |
/// example shows a basic work: |
|
850 |
///\code |
|
851 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
852 |
///\endcode |
|
853 |
/// |
|
854 |
/// After the copy the \c nr map will contain the mapping from the |
|
855 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
856 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
857 |
/// to the arcs of the \c from digraph. |
|
858 |
/// |
|
859 |
/// \see DigraphCopy |
|
860 |
template <typename To, typename From> |
|
861 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
|
862 |
return DigraphCopy<To, From>(to, from); |
|
863 |
} |
|
864 |
|
|
865 |
/// \brief Class to copy a graph. |
|
866 |
/// |
|
867 |
/// Class to copy a graph to another graph (duplicate a graph). The |
|
868 |
/// simplest way of using it is through the \c copyGraph() function. |
|
869 |
/// |
|
870 |
/// This class not just make a copy of a graph, but it can create |
|
871 |
/// references and cross references between the nodes, edges and arcs of |
|
872 |
/// the two graphs, it can copy maps for use with the newly created |
|
873 |
/// graph and copy nodes, edges and arcs. |
|
874 |
/// |
|
875 |
/// To make a copy from a graph, first an instance of GraphCopy |
|
876 |
/// should be created, then the data belongs to the graph should |
|
877 |
/// assigned to copy. In the end, the \c run() member should be |
|
878 |
/// called. |
|
879 |
/// |
|
880 |
/// The next code copies a graph with several data: |
|
881 |
///\code |
|
882 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
883 |
/// // create a reference for the nodes |
|
884 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
885 |
/// dc.nodeRef(nr); |
|
886 |
/// // create a cross reference (inverse) for the edges |
|
887 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
|
888 |
/// dc.edgeCrossRef(ecr); |
|
889 |
/// // copy an arc map |
|
890 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
891 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
892 |
/// dc.arcMap(namap, oamap); |
|
893 |
/// // copy a node |
|
894 |
/// OrigGraph::Node on; |
|
895 |
/// NewGraph::Node nn; |
|
896 |
/// dc.node(nn, on); |
|
897 |
/// // Executions of copy |
|
898 |
/// dc.run(); |
|
899 |
///\endcode |
|
900 |
template <typename To, typename From> |
|
901 |
class GraphCopy { |
|
902 |
private: |
|
903 |
|
|
904 |
typedef typename From::Node Node; |
|
905 |
typedef typename From::NodeIt NodeIt; |
|
906 |
typedef typename From::Arc Arc; |
|
907 |
typedef typename From::ArcIt ArcIt; |
|
908 |
typedef typename From::Edge Edge; |
|
909 |
typedef typename From::EdgeIt EdgeIt; |
|
910 |
|
|
911 |
typedef typename To::Node TNode; |
|
912 |
typedef typename To::Arc TArc; |
|
913 |
typedef typename To::Edge TEdge; |
|
914 |
|
|
915 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
916 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
|
917 |
|
|
918 |
struct ArcRefMap { |
|
919 |
ArcRefMap(const To& to, const From& from, |
|
920 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
|
921 |
: _to(to), _from(from), |
|
922 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
|
923 |
|
|
924 |
typedef typename From::Arc Key; |
|
925 |
typedef typename To::Arc Value; |
|
926 |
|
|
927 |
Value operator[](const Key& key) const { |
|
928 |
bool forward = _from.u(key) != _from.v(key) ? |
|
929 |
_node_ref[_from.source(key)] == |
|
930 |
_to.source(_to.direct(_edge_ref[key], true)) : |
|
931 |
_from.direction(key); |
|
932 |
return _to.direct(_edge_ref[key], forward); |
|
933 |
} |
|
934 |
|
|
935 |
const To& _to; |
|
936 |
const From& _from; |
|
937 |
const EdgeRefMap& _edge_ref; |
|
938 |
const NodeRefMap& _node_ref; |
|
939 |
}; |
|
940 |
|
|
941 |
|
|
942 |
public: |
|
943 |
|
|
944 |
|
|
945 |
/// \brief Constructor for the GraphCopy. |
|
946 |
/// |
|
947 |
/// It copies the content of the \c _from graph into the |
|
948 |
/// \c _to graph. |
|
949 |
GraphCopy(To& to, const From& from) |
|
950 |
: _from(from), _to(to) {} |
|
951 |
|
|
952 |
/// \brief Destructor of the GraphCopy |
|
953 |
/// |
|
954 |
/// Destructor of the GraphCopy |
|
955 |
~GraphCopy() { |
|
956 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
957 |
delete _node_maps[i]; |
|
958 |
} |
|
959 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
960 |
delete _arc_maps[i]; |
|
961 |
} |
|
962 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
963 |
delete _edge_maps[i]; |
|
964 |
} |
|
965 |
|
|
966 |
} |
|
967 |
|
|
968 |
/// \brief Copies the node references into the given map. |
|
969 |
/// |
|
970 |
/// Copies the node references into the given map. |
|
971 |
template <typename NodeRef> |
|
972 |
GraphCopy& nodeRef(NodeRef& map) { |
|
973 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
|
974 |
NodeRefMap, NodeRef>(map)); |
|
975 |
return *this; |
|
976 |
} |
|
977 |
|
|
978 |
/// \brief Copies the node cross references into the given map. |
|
979 |
/// |
|
980 |
/// Copies the node cross references (reverse references) into |
|
981 |
/// the given map. |
|
982 |
template <typename NodeCrossRef> |
|
983 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
|
984 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
|
985 |
NodeRefMap, NodeCrossRef>(map)); |
|
986 |
return *this; |
|
987 |
} |
|
988 |
|
|
989 |
/// \brief Make copy of the given map. |
|
990 |
/// |
|
991 |
/// Makes copy of the given map for the newly created graph. |
|
992 |
/// The new map's key type is the to graph's node type, |
|
993 |
/// and the copied map's key type is the from graph's node |
|
994 |
/// type. |
|
995 |
template <typename ToMap, typename FromMap> |
|
996 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
997 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
|
998 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
999 |
return *this; |
|
1000 |
} |
|
1001 |
|
|
1002 |
/// \brief Make a copy of the given node. |
|
1003 |
/// |
|
1004 |
/// Make a copy of the given node. |
|
1005 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
|
1006 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
|
1007 |
NodeRefMap, TNode>(tnode, snode)); |
|
1008 |
return *this; |
|
1009 |
} |
|
1010 |
|
|
1011 |
/// \brief Copies the arc references into the given map. |
|
1012 |
/// |
|
1013 |
/// Copies the arc references into the given map. |
|
1014 |
template <typename ArcRef> |
|
1015 |
GraphCopy& arcRef(ArcRef& map) { |
|
1016 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
|
1017 |
ArcRefMap, ArcRef>(map)); |
|
1018 |
return *this; |
|
1019 |
} |
|
1020 |
|
|
1021 |
/// \brief Copies the arc cross references into the given map. |
|
1022 |
/// |
|
1023 |
/// Copies the arc cross references (reverse references) into |
|
1024 |
/// the given map. |
|
1025 |
template <typename ArcCrossRef> |
|
1026 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
|
1027 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
|
1028 |
ArcRefMap, ArcCrossRef>(map)); |
|
1029 |
return *this; |
|
1030 |
} |
|
1031 |
|
|
1032 |
/// \brief Make copy of the given map. |
|
1033 |
/// |
|
1034 |
/// Makes copy of the given map for the newly created graph. |
|
1035 |
/// The new map's key type is the to graph's arc type, |
|
1036 |
/// and the copied map's key type is the from graph's arc |
|
1037 |
/// type. |
|
1038 |
template <typename ToMap, typename FromMap> |
|
1039 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
1040 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
|
1041 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
1042 |
return *this; |
|
1043 |
} |
|
1044 |
|
|
1045 |
/// \brief Make a copy of the given arc. |
|
1046 |
/// |
|
1047 |
/// Make a copy of the given arc. |
|
1048 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
1049 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
|
1050 |
ArcRefMap, TArc>(tarc, sarc)); |
|
1051 |
return *this; |
|
1052 |
} |
|
1053 |
|
|
1054 |
/// \brief Copies the edge references into the given map. |
|
1055 |
/// |
|
1056 |
/// Copies the edge references into the given map. |
|
1057 |
template <typename EdgeRef> |
|
1058 |
GraphCopy& edgeRef(EdgeRef& map) { |
|
1059 |
_edge_maps.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
|
1060 |
EdgeRefMap, EdgeRef>(map)); |
|
1061 |
return *this; |
|
1062 |
} |
|
1063 |
|
|
1064 |
/// \brief Copies the edge cross references into the given map. |
|
1065 |
/// |
|
1066 |
/// Copies the edge cross references (reverse |
|
1067 |
/// references) into the given map. |
|
1068 |
template <typename EdgeCrossRef> |
|
1069 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
|
1070 |
_edge_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, |
|
1071 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
1072 |
return *this; |
|
1073 |
} |
|
1074 |
|
|
1075 |
/// \brief Make copy of the given map. |
|
1076 |
/// |
|
1077 |
/// Makes copy of the given map for the newly created graph. |
|
1078 |
/// The new map's key type is the to graph's edge type, |
|
1079 |
/// and the copied map's key type is the from graph's edge |
|
1080 |
/// type. |
|
1081 |
template <typename ToMap, typename FromMap> |
|
1082 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
|
1083 |
_edge_maps.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
|
1084 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
1085 |
return *this; |
|
1086 |
} |
|
1087 |
|
|
1088 |
/// \brief Make a copy of the given edge. |
|
1089 |
/// |
|
1090 |
/// Make a copy of the given edge. |
|
1091 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
|
1092 |
_edge_maps.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
|
1093 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
1094 |
return *this; |
|
1095 |
} |
|
1096 |
|
|
1097 |
/// \brief Executes the copies. |
|
1098 |
/// |
|
1099 |
/// Executes the copies. |
|
1100 |
void run() { |
|
1101 |
NodeRefMap nodeRefMap(_from); |
|
1102 |
EdgeRefMap edgeRefMap(_from); |
|
1103 |
ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
|
1104 |
_graph_utils_bits::GraphCopySelector<To>:: |
|
1105 |
copy(_to, _from, nodeRefMap, edgeRefMap); |
|
1106 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
1107 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
1108 |
} |
|
1109 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
1110 |
_edge_maps[i]->copy(_from, edgeRefMap); |
|
1111 |
} |
|
1112 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
1113 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
1114 |
} |
|
1115 |
} |
|
1116 |
|
|
1117 |
private: |
|
1118 |
|
|
1119 |
const From& _from; |
|
1120 |
To& _to; |
|
1121 |
|
|
1122 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
1123 |
_node_maps; |
|
1124 |
|
|
1125 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
1126 |
_arc_maps; |
|
1127 |
|
|
1128 |
std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
1129 |
_edge_maps; |
|
1130 |
|
|
1131 |
}; |
|
1132 |
|
|
1133 |
/// \brief Copy a graph to another graph. |
|
1134 |
/// |
|
1135 |
/// Copy a graph to another graph. The complete usage of the |
|
1136 |
/// function is detailed in the GraphCopy class, but a short |
|
1137 |
/// example shows a basic work: |
|
1138 |
///\code |
|
1139 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
1140 |
///\endcode |
|
1141 |
/// |
|
1142 |
/// After the copy the \c nr map will contain the mapping from the |
|
1143 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
|
1144 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
|
1145 |
/// to the arcs of the \c from graph. |
|
1146 |
/// |
|
1147 |
/// \see GraphCopy |
|
1148 |
template <typename To, typename From> |
|
1149 |
GraphCopy<To, From> |
|
1150 |
copyGraph(To& to, const From& from) { |
|
1151 |
return GraphCopy<To, From>(to, from); |
|
1152 |
} |
|
1153 |
|
|
1154 |
/// @} |
|
1155 |
|
|
1156 |
/// \addtogroup graph_maps |
|
1157 |
/// @{ |
|
1158 |
|
|
1159 |
/// Provides an immutable and unique id for each item in the graph. |
|
1160 |
|
|
1161 |
/// The IdMap class provides a unique and immutable id for each item of the |
|
1162 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
|
1163 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
|
1164 |
/// item (node) does not change (even if you delete other nodes). </ul> |
|
1165 |
/// Through this map you get access (i.e. can read) the inner id values of |
|
1166 |
/// the items stored in the graph. This map can be inverted with its member |
|
1167 |
/// class \c InverseMap or with the \c operator() member. |
|
1168 |
/// |
|
1169 |
template <typename _Graph, typename _Item> |
|
1170 |
class IdMap { |
|
1171 |
public: |
|
1172 |
typedef _Graph Graph; |
|
1173 |
typedef int Value; |
|
1174 |
typedef _Item Item; |
|
1175 |
typedef _Item Key; |
|
1176 |
|
|
1177 |
/// \brief Constructor. |
|
1178 |
/// |
|
1179 |
/// Constructor of the map. |
|
1180 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
|
1181 |
|
|
1182 |
/// \brief Gives back the \e id of the item. |
|
1183 |
/// |
|
1184 |
/// Gives back the immutable and unique \e id of the item. |
|
1185 |
int operator[](const Item& item) const { return _graph->id(item);} |
|
1186 |
|
|
1187 |
/// \brief Gives back the item by its id. |
|
1188 |
/// |
|
1189 |
/// Gives back the item by its id. |
|
1190 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
|
1191 |
|
|
1192 |
private: |
|
1193 |
const Graph* _graph; |
|
1194 |
|
|
1195 |
public: |
|
1196 |
|
|
1197 |
/// \brief The class represents the inverse of its owner (IdMap). |
|
1198 |
/// |
|
1199 |
/// The class represents the inverse of its owner (IdMap). |
|
1200 |
/// \see inverse() |
|
1201 |
class InverseMap { |
|
1202 |
public: |
|
1203 |
|
|
1204 |
/// \brief Constructor. |
|
1205 |
/// |
|
1206 |
/// Constructor for creating an id-to-item map. |
|
1207 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
|
1208 |
|
|
1209 |
/// \brief Constructor. |
|
1210 |
/// |
|
1211 |
/// Constructor for creating an id-to-item map. |
|
1212 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
|
1213 |
|
|
1214 |
/// \brief Gives back the given item from its id. |
|
1215 |
/// |
|
1216 |
/// Gives back the given item from its id. |
|
1217 |
/// |
|
1218 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
|
1219 |
|
|
1220 |
private: |
|
1221 |
const Graph* _graph; |
|
1222 |
}; |
|
1223 |
|
|
1224 |
/// \brief Gives back the inverse of the map. |
|
1225 |
/// |
|
1226 |
/// Gives back the inverse of the IdMap. |
|
1227 |
InverseMap inverse() const { return InverseMap(*_graph);} |
|
1228 |
|
|
1229 |
}; |
|
1230 |
|
|
1231 |
|
|
1232 |
/// \brief General invertable graph-map type. |
|
1233 |
|
|
1234 |
/// This type provides simple invertable graph-maps. |
|
1235 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
|
1236 |
/// and if a key is set to a new value then store it |
|
1237 |
/// in the inverse map. |
|
1238 |
/// |
|
1239 |
/// The values of the map can be accessed |
|
1240 |
/// with stl compatible forward iterator. |
|
1241 |
/// |
|
1242 |
/// \tparam _Graph The graph type. |
|
1243 |
/// \tparam _Item The item type of the graph. |
|
1244 |
/// \tparam _Value The value type of the map. |
|
1245 |
/// |
|
1246 |
/// \see IterableValueMap |
|
1247 |
template <typename _Graph, typename _Item, typename _Value> |
|
1248 |
class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> { |
|
1249 |
private: |
|
1250 |
|
|
1251 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
|
1252 |
typedef _Graph Graph; |
|
1253 |
|
|
1254 |
typedef std::map<_Value, _Item> Container; |
|
1255 |
Container _inv_map; |
|
1256 |
|
|
1257 |
public: |
|
1258 |
|
|
1259 |
/// The key type of InvertableMap (Node, Arc, Edge). |
|
1260 |
typedef typename Map::Key Key; |
|
1261 |
/// The value type of the InvertableMap. |
|
1262 |
typedef typename Map::Value Value; |
|
1263 |
|
|
1264 |
|
|
1265 |
|
|
1266 |
/// \brief Constructor. |
|
1267 |
/// |
|
1268 |
/// Construct a new InvertableMap for the graph. |
|
1269 |
/// |
|
1270 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
|
1271 |
|
|
1272 |
/// \brief Forward iterator for values. |
|
1273 |
/// |
|
1274 |
/// This iterator is an stl compatible forward |
|
1275 |
/// iterator on the values of the map. The values can |
|
1276 |
/// be accessed in the [beginValue, endValue) range. |
|
1277 |
/// |
|
1278 |
class ValueIterator |
|
1279 |
: public std::iterator<std::forward_iterator_tag, Value> { |
|
1280 |
friend class InvertableMap; |
|
1281 |
private: |
|
1282 |
ValueIterator(typename Container::const_iterator _it) |
|
1283 |
: it(_it) {} |
|
1284 |
public: |
|
1285 |
|
|
1286 |
ValueIterator() {} |
|
1287 |
|
|
1288 |
ValueIterator& operator++() { ++it; return *this; } |
|
1289 |
ValueIterator operator++(int) { |
|
1290 |
ValueIterator tmp(*this); |
|
1291 |
operator++(); |
|
1292 |
return tmp; |
|
1293 |
} |
|
1294 |
|
|
1295 |
const Value& operator*() const { return it->first; } |
|
1296 |
const Value* operator->() const { return &(it->first); } |
|
1297 |
|
|
1298 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
|
1299 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
|
1300 |
|
|
1301 |
private: |
|
1302 |
typename Container::const_iterator it; |
|
1303 |
}; |
|
1304 |
|
|
1305 |
/// \brief Returns an iterator to the first value. |
|
1306 |
/// |
|
1307 |
/// Returns an stl compatible iterator to the |
|
1308 |
/// first value of the map. The values of the |
|
1309 |
/// map can be accessed in the [beginValue, endValue) |
|
1310 |
/// range. |
|
1311 |
ValueIterator beginValue() const { |
|
1312 |
return ValueIterator(_inv_map.begin()); |
|
1313 |
} |
|
1314 |
|
|
1315 |
/// \brief Returns an iterator after the last value. |
|
1316 |
/// |
|
1317 |
/// Returns an stl compatible iterator after the |
|
1318 |
/// last value of the map. The values of the |
|
1319 |
/// map can be accessed in the [beginValue, endValue) |
|
1320 |
/// range. |
|
1321 |
ValueIterator endValue() const { |
|
1322 |
return ValueIterator(_inv_map.end()); |
|
1323 |
} |
|
1324 |
|
|
1325 |
/// \brief The setter function of the map. |
|
1326 |
/// |
|
1327 |
/// Sets the mapped value. |
|
1328 |
void set(const Key& key, const Value& val) { |
|
1329 |
Value oldval = Map::operator[](key); |
|
1330 |
typename Container::iterator it = _inv_map.find(oldval); |
|
1331 |
if (it != _inv_map.end() && it->second == key) { |
|
1332 |
_inv_map.erase(it); |
|
1333 |
} |
|
1334 |
_inv_map.insert(make_pair(val, key)); |
|
1335 |
Map::set(key, val); |
|
1336 |
} |
|
1337 |
|
|
1338 |
/// \brief The getter function of the map. |
|
1339 |
/// |
|
1340 |
/// It gives back the value associated with the key. |
|
1341 |
typename MapTraits<Map>::ConstReturnValue |
|
1342 |
operator[](const Key& key) const { |
|
1343 |
return Map::operator[](key); |
|
1344 |
} |
|
1345 |
|
|
1346 |
/// \brief Gives back the item by its value. |
|
1347 |
/// |
|
1348 |
/// Gives back the item by its value. |
|
1349 |
Key operator()(const Value& key) const { |
|
1350 |
typename Container::const_iterator it = _inv_map.find(key); |
|
1351 |
return it != _inv_map.end() ? it->second : INVALID; |
|
1352 |
} |
|
1353 |
|
|
1354 |
protected: |
|
1355 |
|
|
1356 |
/// \brief Erase the key from the map. |
|
1357 |
/// |
|
1358 |
/// Erase the key to the map. It is called by the |
|
1359 |
/// \c AlterationNotifier. |
|
1360 |
virtual void erase(const Key& key) { |
|
1361 |
Value val = Map::operator[](key); |
|
1362 |
typename Container::iterator it = _inv_map.find(val); |
|
1363 |
if (it != _inv_map.end() && it->second == key) { |
|
1364 |
_inv_map.erase(it); |
|
1365 |
} |
|
1366 |
Map::erase(key); |
|
1367 |
} |
|
1368 |
|
|
1369 |
/// \brief Erase more keys from the map. |
|
1370 |
/// |
|
1371 |
/// Erase more keys from the map. It is called by the |
|
1372 |
/// \c AlterationNotifier. |
|
1373 |
virtual void erase(const std::vector<Key>& keys) { |
|
1374 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
1375 |
Value val = Map::operator[](keys[i]); |
|
1376 |
typename Container::iterator it = _inv_map.find(val); |
|
1377 |
if (it != _inv_map.end() && it->second == keys[i]) { |
|
1378 |
_inv_map.erase(it); |
|
1379 |
} |
|
1380 |
} |
|
1381 |
Map::erase(keys); |
|
1382 |
} |
|
1383 |
|
|
1384 |
/// \brief Clear the keys from the map and inverse map. |
|
1385 |
/// |
|
1386 |
/// Clear the keys from the map and inverse map. It is called by the |
|
1387 |
/// \c AlterationNotifier. |
|
1388 |
virtual void clear() { |
|
1389 |
_inv_map.clear(); |
|
1390 |
Map::clear(); |
|
1391 |
} |
|
1392 |
|
|
1393 |
public: |
|
1394 |
|
|
1395 |
/// \brief The inverse map type. |
|
1396 |
/// |
|
1397 |
/// The inverse of this map. The subscript operator of the map |
|
1398 |
/// gives back always the item what was last assigned to the value. |
|
1399 |
class InverseMap { |
|
1400 |
public: |
|
1401 |
/// \brief Constructor of the InverseMap. |
|
1402 |
/// |
|
1403 |
/// Constructor of the InverseMap. |
|
1404 |
explicit InverseMap(const InvertableMap& inverted) |
|
1405 |
: _inverted(inverted) {} |
|
1406 |
|
|
1407 |
/// The value type of the InverseMap. |
|
1408 |
typedef typename InvertableMap::Key Value; |
|
1409 |
/// The key type of the InverseMap. |
|
1410 |
typedef typename InvertableMap::Value Key; |
|
1411 |
|
|
1412 |
/// \brief Subscript operator. |
|
1413 |
/// |
|
1414 |
/// Subscript operator. It gives back always the item |
|
1415 |
/// what was last assigned to the value. |
|
1416 |
Value operator[](const Key& key) const { |
|
1417 |
return _inverted(key); |
|
1418 |
} |
|
1419 |
|
|
1420 |
private: |
|
1421 |
const InvertableMap& _inverted; |
|
1422 |
}; |
|
1423 |
|
|
1424 |
/// \brief It gives back the just readable inverse map. |
|
1425 |
/// |
|
1426 |
/// It gives back the just readable inverse map. |
|
1427 |
InverseMap inverse() const { |
|
1428 |
return InverseMap(*this); |
|
1429 |
} |
|
1430 |
|
|
1431 |
|
|
1432 |
|
|
1433 |
}; |
|
1434 |
|
|
1435 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
|
1436 |
/// item in the graph. |
|
1437 |
/// |
|
1438 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
|
1439 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
|
1440 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
|
1441 |
/// different ids <li>\b continuous: the range of the ids is the set of |
|
1442 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
|
1443 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
|
1444 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
|
1445 |
/// with its member class \c InverseMap, or with the \c operator() member. |
|
1446 |
/// |
|
1447 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
|
1448 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
|
1449 |
/// Edge. |
|
1450 |
template <typename _Graph, typename _Item> |
|
1451 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> { |
|
1452 |
|
|
1453 |
typedef _Item Item; |
|
1454 |
typedef DefaultMap<_Graph, _Item, int> Map; |
|
1455 |
|
|
1456 |
public: |
|
1457 |
/// The graph class of DescriptorMap. |
|
1458 |
typedef _Graph Graph; |
|
1459 |
|
|
1460 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
|
1461 |
typedef typename Map::Key Key; |
|
1462 |
/// The value type of DescriptorMap. |
|
1463 |
typedef typename Map::Value Value; |
|
1464 |
|
|
1465 |
/// \brief Constructor. |
|
1466 |
/// |
|
1467 |
/// Constructor for descriptor map. |
|
1468 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
|
1469 |
Item it; |
|
1470 |
const typename Map::Notifier* nf = Map::notifier(); |
|
1471 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
1472 |
Map::set(it, _inv_map.size()); |
|
1473 |
_inv_map.push_back(it); |
|
1474 |
} |
|
1475 |
} |
|
1476 |
|
|
1477 |
protected: |
|
1478 |
|
|
1479 |
/// \brief Add a new key to the map. |
|
1480 |
/// |
|
1481 |
/// Add a new key to the map. It is called by the |
|
1482 |
/// \c AlterationNotifier. |
|
1483 |
virtual void add(const Item& item) { |
|
1484 |
Map::add(item); |
|
1485 |
Map::set(item, _inv_map.size()); |
|
1486 |
_inv_map.push_back(item); |
|
1487 |
} |
|
1488 |
|
|
1489 |
/// \brief Add more new keys to the map. |
|
1490 |
/// |
|
1491 |
/// Add more new keys to the map. It is called by the |
|
1492 |
/// \c AlterationNotifier. |
|
1493 |
virtual void add(const std::vector<Item>& items) { |
|
1494 |
Map::add(items); |
|
1495 |
for (int i = 0; i < int(items.size()); ++i) { |
|
1496 |
Map::set(items[i], _inv_map.size()); |
|
1497 |
_inv_map.push_back(items[i]); |
|
1498 |
} |
|
1499 |
} |
|
1500 |
|
|
1501 |
/// \brief Erase the key from the map. |
|
1502 |
/// |
|
1503 |
/// Erase the key from the map. It is called by the |
|
1504 |
/// \c AlterationNotifier. |
|
1505 |
virtual void erase(const Item& item) { |
|
1506 |
Map::set(_inv_map.back(), Map::operator[](item)); |
|
1507 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
|
1508 |
_inv_map.pop_back(); |
|
1509 |
Map::erase(item); |
|
1510 |
} |
|
1511 |
|
|
1512 |
/// \brief Erase more keys from the map. |
|
1513 |
/// |
|
1514 |
/// Erase more keys from the map. It is called by the |
|
1515 |
/// \c AlterationNotifier. |
|
1516 |
virtual void erase(const std::vector<Item>& items) { |
|
1517 |
for (int i = 0; i < int(items.size()); ++i) { |
|
1518 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
|
1519 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
|
1520 |
_inv_map.pop_back(); |
|
1521 |
} |
|
1522 |
Map::erase(items); |
|
1523 |
} |
|
1524 |
|
|
1525 |
/// \brief Build the unique map. |
|
1526 |
/// |
|
1527 |
/// Build the unique map. It is called by the |
|
1528 |
/// \c AlterationNotifier. |
|
1529 |
virtual void build() { |
|
1530 |
Map::build(); |
|
1531 |
Item it; |
|
1532 |
const typename Map::Notifier* nf = Map::notifier(); |
|
1533 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
1534 |
Map::set(it, _inv_map.size()); |
|
1535 |
_inv_map.push_back(it); |
|
1536 |
} |
|
1537 |
} |
|
1538 |
|
|
1539 |
/// \brief Clear the keys from the map. |
|
1540 |
/// |
|
1541 |
/// Clear the keys from the map. It is called by the |
|
1542 |
/// \c AlterationNotifier. |
|
1543 |
virtual void clear() { |
|
1544 |
_inv_map.clear(); |
|
1545 |
Map::clear(); |
|
1546 |
} |
|
1547 |
|
|
1548 |
public: |
|
1549 |
|
|
1550 |
/// \brief Returns the maximal value plus one. |
|
1551 |
/// |
|
1552 |
/// Returns the maximal value plus one in the map. |
|
1553 |
unsigned int size() const { |
|
1554 |
return _inv_map.size(); |
|
1555 |
} |
|
1556 |
|
|
1557 |
/// \brief Swaps the position of the two items in the map. |
|
1558 |
/// |
|
1559 |
/// Swaps the position of the two items in the map. |
|
1560 |
void swap(const Item& p, const Item& q) { |
|
1561 |
int pi = Map::operator[](p); |
|
1562 |
int qi = Map::operator[](q); |
|
1563 |
Map::set(p, qi); |
|
1564 |
_inv_map[qi] = p; |
|
1565 |
Map::set(q, pi); |
|
1566 |
_inv_map[pi] = q; |
|
1567 |
} |
|
1568 |
|
|
1569 |
/// \brief Gives back the \e descriptor of the item. |
|
1570 |
/// |
|
1571 |
/// Gives back the mutable and unique \e descriptor of the map. |
|
1572 |
int operator[](const Item& item) const { |
|
1573 |
return Map::operator[](item); |
|
1574 |
} |
|
1575 |
|
|
1576 |
/// \brief Gives back the item by its descriptor. |
|
1577 |
/// |
|
1578 |
/// Gives back th item by its descriptor. |
|
1579 |
Item operator()(int id) const { |
|
1580 |
return _inv_map[id]; |
|
1581 |
} |
|
1582 |
|
|
1583 |
private: |
|
1584 |
|
|
1585 |
typedef std::vector<Item> Container; |
|
1586 |
Container _inv_map; |
|
1587 |
|
|
1588 |
public: |
|
1589 |
/// \brief The inverse map type of DescriptorMap. |
|
1590 |
/// |
|
1591 |
/// The inverse map type of DescriptorMap. |
|
1592 |
class InverseMap { |
|
1593 |
public: |
|
1594 |
/// \brief Constructor of the InverseMap. |
|
1595 |
/// |
|
1596 |
/// Constructor of the InverseMap. |
|
1597 |
explicit InverseMap(const DescriptorMap& inverted) |
|
1598 |
: _inverted(inverted) {} |
|
1599 |
|
|
1600 |
|
|
1601 |
/// The value type of the InverseMap. |
|
1602 |
typedef typename DescriptorMap::Key Value; |
|
1603 |
/// The key type of the InverseMap. |
|
1604 |
typedef typename DescriptorMap::Value Key; |
|
1605 |
|
|
1606 |
/// \brief Subscript operator. |
|
1607 |
/// |
|
1608 |
/// Subscript operator. It gives back the item |
|
1609 |
/// that the descriptor belongs to currently. |
|
1610 |
Value operator[](const Key& key) const { |
|
1611 |
return _inverted(key); |
|
1612 |
} |
|
1613 |
|
|
1614 |
/// \brief Size of the map. |
|
1615 |
/// |
|
1616 |
/// Returns the size of the map. |
|
1617 |
unsigned int size() const { |
|
1618 |
return _inverted.size(); |
|
1619 |
} |
|
1620 |
|
|
1621 |
private: |
|
1622 |
const DescriptorMap& _inverted; |
|
1623 |
}; |
|
1624 |
|
|
1625 |
/// \brief Gives back the inverse of the map. |
|
1626 |
/// |
|
1627 |
/// Gives back the inverse of the map. |
|
1628 |
const InverseMap inverse() const { |
|
1629 |
return InverseMap(*this); |
|
1630 |
} |
|
1631 |
}; |
|
1632 |
|
|
1633 |
/// \brief Returns the source of the given arc. |
|
1634 |
/// |
|
1635 |
/// The SourceMap gives back the source Node of the given arc. |
|
1636 |
/// \see TargetMap |
|
1637 |
template <typename Digraph> |
|
1638 |
class SourceMap { |
|
1639 |
public: |
|
1640 |
|
|
1641 |
typedef typename Digraph::Node Value; |
|
1642 |
typedef typename Digraph::Arc Key; |
|
1643 |
|
|
1644 |
/// \brief Constructor |
|
1645 |
/// |
|
1646 |
/// Constructor |
|
1647 |
/// \param _digraph The digraph that the map belongs to. |
|
1648 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
|
1649 |
|
|
1650 |
/// \brief The subscript operator. |
|
1651 |
/// |
|
1652 |
/// The subscript operator. |
|
1653 |
/// \param arc The arc |
|
1654 |
/// \return The source of the arc |
|
1655 |
Value operator[](const Key& arc) const { |
|
1656 |
return _digraph.source(arc); |
|
1657 |
} |
|
1658 |
|
|
1659 |
private: |
|
1660 |
const Digraph& _digraph; |
|
1661 |
}; |
|
1662 |
|
|
1663 |
/// \brief Returns a \ref SourceMap class. |
|
1664 |
/// |
|
1665 |
/// This function just returns an \ref SourceMap class. |
|
1666 |
/// \relates SourceMap |
|
1667 |
template <typename Digraph> |
|
1668 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
|
1669 |
return SourceMap<Digraph>(digraph); |
|
1670 |
} |
|
1671 |
|
|
1672 |
/// \brief Returns the target of the given arc. |
|
1673 |
/// |
|
1674 |
/// The TargetMap gives back the target Node of the given arc. |
|
1675 |
/// \see SourceMap |
|
1676 |
template <typename Digraph> |
|
1677 |
class TargetMap { |
|
1678 |
public: |
|
1679 |
|
|
1680 |
typedef typename Digraph::Node Value; |
|
1681 |
typedef typename Digraph::Arc Key; |
|
1682 |
|
|
1683 |
/// \brief Constructor |
|
1684 |
/// |
|
1685 |
/// Constructor |
|
1686 |
/// \param _digraph The digraph that the map belongs to. |
|
1687 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
|
1688 |
|
|
1689 |
/// \brief The subscript operator. |
|
1690 |
/// |
|
1691 |
/// The subscript operator. |
|
1692 |
/// \param e The arc |
|
1693 |
/// \return The target of the arc |
|
1694 |
Value operator[](const Key& e) const { |
|
1695 |
return _digraph.target(e); |
|
1696 |
} |
|
1697 |
|
|
1698 |
private: |
|
1699 |
const Digraph& _digraph; |
|
1700 |
}; |
|
1701 |
|
|
1702 |
/// \brief Returns a \ref TargetMap class. |
|
1703 |
/// |
|
1704 |
/// This function just returns a \ref TargetMap class. |
|
1705 |
/// \relates TargetMap |
|
1706 |
template <typename Digraph> |
|
1707 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
|
1708 |
return TargetMap<Digraph>(digraph); |
|
1709 |
} |
|
1710 |
|
|
1711 |
/// \brief Returns the "forward" directed arc view of an edge. |
|
1712 |
/// |
|
1713 |
/// Returns the "forward" directed arc view of an edge. |
|
1714 |
/// \see BackwardMap |
|
1715 |
template <typename Graph> |
|
1716 |
class ForwardMap { |
|
1717 |
public: |
|
1718 |
|
|
1719 |
typedef typename Graph::Arc Value; |
|
1720 |
typedef typename Graph::Edge Key; |
|
1721 |
|
|
1722 |
/// \brief Constructor |
|
1723 |
/// |
|
1724 |
/// Constructor |
|
1725 |
/// \param _graph The graph that the map belongs to. |
|
1726 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
|
1727 |
|
|
1728 |
/// \brief The subscript operator. |
|
1729 |
/// |
|
1730 |
/// The subscript operator. |
|
1731 |
/// \param key An edge |
|
1732 |
/// \return The "forward" directed arc view of edge |
|
1733 |
Value operator[](const Key& key) const { |
|
1734 |
return _graph.direct(key, true); |
|
1735 |
} |
|
1736 |
|
|
1737 |
private: |
|
1738 |
const Graph& _graph; |
|
1739 |
}; |
|
1740 |
|
|
1741 |
/// \brief Returns a \ref ForwardMap class. |
|
1742 |
/// |
|
1743 |
/// This function just returns an \ref ForwardMap class. |
|
1744 |
/// \relates ForwardMap |
|
1745 |
template <typename Graph> |
|
1746 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
|
1747 |
return ForwardMap<Graph>(graph); |
|
1748 |
} |
|
1749 |
|
|
1750 |
/// \brief Returns the "backward" directed arc view of an edge. |
|
1751 |
/// |
|
1752 |
/// Returns the "backward" directed arc view of an edge. |
|
1753 |
/// \see ForwardMap |
|
1754 |
template <typename Graph> |
|
1755 |
class BackwardMap { |
|
1756 |
public: |
|
1757 |
|
|
1758 |
typedef typename Graph::Arc Value; |
|
1759 |
typedef typename Graph::Edge Key; |
|
1760 |
|
|
1761 |
/// \brief Constructor |
|
1762 |
/// |
|
1763 |
/// Constructor |
|
1764 |
/// \param _graph The graph that the map belongs to. |
|
1765 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
|
1766 |
|
|
1767 |
/// \brief The subscript operator. |
|
1768 |
/// |
|
1769 |
/// The subscript operator. |
|
1770 |
/// \param key An edge |
|
1771 |
/// \return The "backward" directed arc view of edge |
|
1772 |
Value operator[](const Key& key) const { |
|
1773 |
return _graph.direct(key, false); |
|
1774 |
} |
|
1775 |
|
|
1776 |
private: |
|
1777 |
const Graph& _graph; |
|
1778 |
}; |
|
1779 |
|
|
1780 |
/// \brief Returns a \ref BackwardMap class |
|
1781 |
|
|
1782 |
/// This function just returns a \ref BackwardMap class. |
|
1783 |
/// \relates BackwardMap |
|
1784 |
template <typename Graph> |
|
1785 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
|
1786 |
return BackwardMap<Graph>(graph); |
|
1787 |
} |
|
1788 |
|
|
1789 |
/// \brief Potential difference map |
|
1790 |
/// |
|
1791 |
/// If there is an potential map on the nodes then we |
|
1792 |
/// can get an arc map as we get the substraction of the |
|
1793 |
/// values of the target and source. |
|
1794 |
template <typename Digraph, typename NodeMap> |
|
1795 |
class PotentialDifferenceMap { |
|
1796 |
public: |
|
1797 |
typedef typename Digraph::Arc Key; |
|
1798 |
typedef typename NodeMap::Value Value; |
|
1799 |
|
|
1800 |
/// \brief Constructor |
|
1801 |
/// |
|
1802 |
/// Contructor of the map |
|
1803 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
|
1804 |
const NodeMap& potential) |
|
1805 |
: _digraph(digraph), _potential(potential) {} |
|
1806 |
|
|
1807 |
/// \brief Const subscription operator |
|
1808 |
/// |
|
1809 |
/// Const subscription operator |
|
1810 |
Value operator[](const Key& arc) const { |
|
1811 |
return _potential[_digraph.target(arc)] - |
|
1812 |
_potential[_digraph.source(arc)]; |
|
1813 |
} |
|
1814 |
|
|
1815 |
private: |
|
1816 |
const Digraph& _digraph; |
|
1817 |
const NodeMap& _potential; |
|
1818 |
}; |
|
1819 |
|
|
1820 |
/// \brief Returns a PotentialDifferenceMap. |
|
1821 |
/// |
|
1822 |
/// This function just returns a PotentialDifferenceMap. |
|
1823 |
/// \relates PotentialDifferenceMap |
|
1824 |
template <typename Digraph, typename NodeMap> |
|
1825 |
PotentialDifferenceMap<Digraph, NodeMap> |
|
1826 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
|
1827 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
|
1828 |
} |
|
1829 |
|
|
1830 |
/// \brief Map of the node in-degrees. |
|
1831 |
/// |
|
1832 |
/// This map returns the in-degree of a node. Once it is constructed, |
|
1833 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
1834 |
/// in constant time. On the other hand, the values are updated automatically |
|
1835 |
/// whenever the digraph changes. |
|
1836 |
/// |
|
1837 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
1838 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
|
1839 |
/// is not guarantied if these additional features are used. For example |
|
1840 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
1841 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
1842 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
1843 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
1844 |
/// |
|
1845 |
/// \sa OutDegMap |
|
1846 |
|
|
1847 |
template <typename _Digraph> |
|
1848 |
class InDegMap |
|
1849 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
1850 |
::ItemNotifier::ObserverBase { |
|
1851 |
|
|
1852 |
public: |
|
1853 |
|
|
1854 |
typedef _Digraph Digraph; |
|
1855 |
typedef int Value; |
|
1856 |
typedef typename Digraph::Node Key; |
|
1857 |
|
|
1858 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
1859 |
::ItemNotifier::ObserverBase Parent; |
|
1860 |
|
|
1861 |
private: |
|
1862 |
|
|
1863 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
|
1864 |
public: |
|
1865 |
|
|
1866 |
typedef DefaultMap<Digraph, Key, int> Parent; |
|
1867 |
|
|
1868 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
|
1869 |
|
|
1870 |
virtual void add(const Key& key) { |
|
1871 |
Parent::add(key); |
|
1872 |
Parent::set(key, 0); |
|
1873 |
} |
|
1874 |
|
|
1875 |
virtual void add(const std::vector<Key>& keys) { |
|
1876 |
Parent::add(keys); |
|
1877 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
1878 |
Parent::set(keys[i], 0); |
|
1879 |
} |
|
1880 |
} |
|
1881 |
|
|
1882 |
virtual void build() { |
|
1883 |
Parent::build(); |
|
1884 |
Key it; |
|
1885 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
1886 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
1887 |
Parent::set(it, 0); |
|
1888 |
} |
|
1889 |
} |
|
1890 |
}; |
|
1891 |
|
|
1892 |
public: |
|
1893 |
|
|
1894 |
/// \brief Constructor. |
|
1895 |
/// |
|
1896 |
/// Constructor for creating in-degree map. |
|
1897 |
explicit InDegMap(const Digraph& digraph) |
|
1898 |
: _digraph(digraph), _deg(digraph) { |
|
1899 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
1900 |
|
|
1901 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1902 |
_deg[it] = countInArcs(_digraph, it); |
|
1903 |
} |
|
1904 |
} |
|
1905 |
|
|
1906 |
/// Gives back the in-degree of a Node. |
|
1907 |
int operator[](const Key& key) const { |
|
1908 |
return _deg[key]; |
|
1909 |
} |
|
1910 |
|
|
1911 |
protected: |
|
1912 |
|
|
1913 |
typedef typename Digraph::Arc Arc; |
|
1914 |
|
|
1915 |
virtual void add(const Arc& arc) { |
|
1916 |
++_deg[_digraph.target(arc)]; |
|
1917 |
} |
|
1918 |
|
|
1919 |
virtual void add(const std::vector<Arc>& arcs) { |
|
1920 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
1921 |
++_deg[_digraph.target(arcs[i])]; |
|
1922 |
} |
|
1923 |
} |
|
1924 |
|
|
1925 |
virtual void erase(const Arc& arc) { |
|
1926 |
--_deg[_digraph.target(arc)]; |
|
1927 |
} |
|
1928 |
|
|
1929 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
1930 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
1931 |
--_deg[_digraph.target(arcs[i])]; |
|
1932 |
} |
|
1933 |
} |
|
1934 |
|
|
1935 |
virtual void build() { |
|
1936 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1937 |
_deg[it] = countInArcs(_digraph, it); |
|
1938 |
} |
|
1939 |
} |
|
1940 |
|
|
1941 |
virtual void clear() { |
|
1942 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1943 |
_deg[it] = 0; |
|
1944 |
} |
|
1945 |
} |
|
1946 |
private: |
|
1947 |
|
|
1948 |
const Digraph& _digraph; |
|
1949 |
AutoNodeMap _deg; |
|
1950 |
}; |
|
1951 |
|
|
1952 |
/// \brief Map of the node out-degrees. |
|
1953 |
/// |
|
1954 |
/// This map returns the out-degree of a node. Once it is constructed, |
|
1955 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
1956 |
/// in constant time. On the other hand, the values are updated automatically |
|
1957 |
/// whenever the digraph changes. |
|
1958 |
/// |
|
1959 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
1960 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
|
1961 |
/// is not guarantied if these additional features are used. For example |
|
1962 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
1963 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
1964 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
1965 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
1966 |
/// |
|
1967 |
/// \sa InDegMap |
|
1968 |
|
|
1969 |
template <typename _Digraph> |
|
1970 |
class OutDegMap |
|
1971 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
1972 |
::ItemNotifier::ObserverBase { |
|
1973 |
|
|
1974 |
public: |
|
1975 |
|
|
1976 |
typedef _Digraph Digraph; |
|
1977 |
typedef int Value; |
|
1978 |
typedef typename Digraph::Node Key; |
|
1979 |
|
|
1980 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
1981 |
::ItemNotifier::ObserverBase Parent; |
|
1982 |
|
|
1983 |
private: |
|
1984 |
|
|
1985 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
|
1986 |
public: |
|
1987 |
|
|
1988 |
typedef DefaultMap<Digraph, Key, int> Parent; |
|
1989 |
|
|
1990 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
|
1991 |
|
|
1992 |
virtual void add(const Key& key) { |
|
1993 |
Parent::add(key); |
|
1994 |
Parent::set(key, 0); |
|
1995 |
} |
|
1996 |
virtual void add(const std::vector<Key>& keys) { |
|
1997 |
Parent::add(keys); |
|
1998 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
1999 |
Parent::set(keys[i], 0); |
|
2000 |
} |
|
2001 |
} |
|
2002 |
virtual void build() { |
|
2003 |
Parent::build(); |
|
2004 |
Key it; |
|
2005 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
2006 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2007 |
Parent::set(it, 0); |
|
2008 |
} |
|
2009 |
} |
|
2010 |
}; |
|
2011 |
|
|
2012 |
public: |
|
2013 |
|
|
2014 |
/// \brief Constructor. |
|
2015 |
/// |
|
2016 |
/// Constructor for creating out-degree map. |
|
2017 |
explicit OutDegMap(const Digraph& digraph) |
|
2018 |
: _digraph(digraph), _deg(digraph) { |
|
2019 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
2020 |
|
|
2021 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2022 |
_deg[it] = countOutArcs(_digraph, it); |
|
2023 |
} |
|
2024 |
} |
|
2025 |
|
|
2026 |
/// Gives back the out-degree of a Node. |
|
2027 |
int operator[](const Key& key) const { |
|
2028 |
return _deg[key]; |
|
2029 |
} |
|
2030 |
|
|
2031 |
protected: |
|
2032 |
|
|
2033 |
typedef typename Digraph::Arc Arc; |
|
2034 |
|
|
2035 |
virtual void add(const Arc& arc) { |
|
2036 |
++_deg[_digraph.source(arc)]; |
|
2037 |
} |
|
2038 |
|
|
2039 |
virtual void add(const std::vector<Arc>& arcs) { |
|
2040 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2041 |
++_deg[_digraph.source(arcs[i])]; |
|
2042 |
} |
|
2043 |
} |
|
2044 |
|
|
2045 |
virtual void erase(const Arc& arc) { |
|
2046 |
--_deg[_digraph.source(arc)]; |
|
2047 |
} |
|
2048 |
|
|
2049 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
2050 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2051 |
--_deg[_digraph.source(arcs[i])]; |
|
2052 |
} |
|
2053 |
} |
|
2054 |
|
|
2055 |
virtual void build() { |
|
2056 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2057 |
_deg[it] = countOutArcs(_digraph, it); |
|
2058 |
} |
|
2059 |
} |
|
2060 |
|
|
2061 |
virtual void clear() { |
|
2062 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2063 |
_deg[it] = 0; |
|
2064 |
} |
|
2065 |
} |
|
2066 |
private: |
|
2067 |
|
|
2068 |
const Digraph& _digraph; |
|
2069 |
AutoNodeMap _deg; |
|
2070 |
}; |
|
2071 |
|
|
2072 |
|
|
2073 |
///Dynamic arc look up between given endpoints. |
|
2074 |
|
|
2075 |
///\ingroup gutils |
|
2076 |
///Using this class, you can find an arc in a digraph from a given |
|
2077 |
///source to a given target in amortized time <em>O(log d)</em>, |
|
2078 |
///where <em>d</em> is the out-degree of the source node. |
|
2079 |
/// |
|
2080 |
///It is possible to find \e all parallel arcs between two nodes with |
|
2081 |
///the \c findFirst() and \c findNext() members. |
|
2082 |
/// |
|
2083 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
|
2084 |
///digraph is not changed so frequently. |
|
2085 |
/// |
|
2086 |
///This class uses a self-adjusting binary search tree, Sleator's |
|
2087 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
|
2088 |
///time bound for arc lookups. This class also guarantees the |
|
2089 |
///optimal time bound in a constant factor for any distribution of |
|
2090 |
///queries. |
|
2091 |
/// |
|
2092 |
///\tparam G The type of the underlying digraph. |
|
2093 |
/// |
|
2094 |
///\sa ArcLookUp |
|
2095 |
///\sa AllArcLookUp |
|
2096 |
template<class G> |
|
2097 |
class DynArcLookUp |
|
2098 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
|
2099 |
{ |
|
2100 |
public: |
|
2101 |
typedef typename ItemSetTraits<G, typename G::Arc> |
|
2102 |
::ItemNotifier::ObserverBase Parent; |
|
2103 |
|
|
2104 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
2105 |
typedef G Digraph; |
|
2106 |
|
|
2107 |
protected: |
|
2108 |
|
|
2109 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> { |
|
2110 |
public: |
|
2111 |
|
|
2112 |
typedef DefaultMap<G, Node, Arc> Parent; |
|
2113 |
|
|
2114 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
|
2115 |
|
|
2116 |
virtual void add(const Node& node) { |
|
2117 |
Parent::add(node); |
|
2118 |
Parent::set(node, INVALID); |
|
2119 |
} |
|
2120 |
|
|
2121 |
virtual void add(const std::vector<Node>& nodes) { |
|
2122 |
Parent::add(nodes); |
|
2123 |
for (int i = 0; i < int(nodes.size()); ++i) { |
|
2124 |
Parent::set(nodes[i], INVALID); |
|
2125 |
} |
|
2126 |
} |
|
2127 |
|
|
2128 |
virtual void build() { |
|
2129 |
Parent::build(); |
|
2130 |
Node it; |
|
2131 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
2132 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2133 |
Parent::set(it, INVALID); |
|
2134 |
} |
|
2135 |
} |
|
2136 |
}; |
|
2137 |
|
|
2138 |
const Digraph &_g; |
|
2139 |
AutoNodeMap _head; |
|
2140 |
typename Digraph::template ArcMap<Arc> _parent; |
|
2141 |
typename Digraph::template ArcMap<Arc> _left; |
|
2142 |
typename Digraph::template ArcMap<Arc> _right; |
|
2143 |
|
|
2144 |
class ArcLess { |
|
2145 |
const Digraph &g; |
|
2146 |
public: |
|
2147 |
ArcLess(const Digraph &_g) : g(_g) {} |
|
2148 |
bool operator()(Arc a,Arc b) const |
|
2149 |
{ |
|
2150 |
return g.target(a)<g.target(b); |
|
2151 |
} |
|
2152 |
}; |
|
2153 |
|
|
2154 |
public: |
|
2155 |
|
|
2156 |
///Constructor |
|
2157 |
|
|
2158 |
///Constructor. |
|
2159 |
/// |
|
2160 |
///It builds up the search database. |
|
2161 |
DynArcLookUp(const Digraph &g) |
|
2162 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
|
2163 |
{ |
|
2164 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
|
2165 |
refresh(); |
|
2166 |
} |
|
2167 |
|
|
2168 |
protected: |
|
2169 |
|
|
2170 |
virtual void add(const Arc& arc) { |
|
2171 |
insert(arc); |
|
2172 |
} |
|
2173 |
|
|
2174 |
virtual void add(const std::vector<Arc>& arcs) { |
|
2175 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2176 |
insert(arcs[i]); |
|
2177 |
} |
|
2178 |
} |
|
2179 |
|
|
2180 |
virtual void erase(const Arc& arc) { |
|
2181 |
remove(arc); |
|
2182 |
} |
|
2183 |
|
|
2184 |
virtual void erase(const std::vector<Arc>& arcs) { |
|
2185 |
for (int i = 0; i < int(arcs.size()); ++i) { |
|
2186 |
remove(arcs[i]); |
|
2187 |
} |
|
2188 |
} |
|
2189 |
|
|
2190 |
virtual void build() { |
|
2191 |
refresh(); |
|
2192 |
} |
|
2193 |
|
|
2194 |
virtual void clear() { |
|
2195 |
for(NodeIt n(_g);n!=INVALID;++n) { |
|
2196 |
_head.set(n, INVALID); |
|
2197 |
} |
|
2198 |
} |
|
2199 |
|
|
2200 |
void insert(Arc arc) { |
|
2201 |
Node s = _g.source(arc); |
|
2202 |
Node t = _g.target(arc); |
|
2203 |
_left.set(arc, INVALID); |
|
2204 |
_right.set(arc, INVALID); |
|
2205 |
|
|
2206 |
Arc e = _head[s]; |
|
2207 |
if (e == INVALID) { |
|
2208 |
_head.set(s, arc); |
|
2209 |
_parent.set(arc, INVALID); |
|
2210 |
return; |
|
2211 |
} |
|
2212 |
while (true) { |
|
2213 |
if (t < _g.target(e)) { |
|
2214 |
if (_left[e] == INVALID) { |
|
2215 |
_left.set(e, arc); |
|
2216 |
_parent.set(arc, e); |
|
2217 |
splay(arc); |
|
2218 |
return; |
|
2219 |
} else { |
|
2220 |
e = _left[e]; |
|
2221 |
} |
|
2222 |
} else { |
|
2223 |
if (_right[e] == INVALID) { |
|
2224 |
_right.set(e, arc); |
|
2225 |
_parent.set(arc, e); |
|
2226 |
splay(arc); |
|
2227 |
return; |
|
2228 |
} else { |
|
2229 |
e = _right[e]; |
|
2230 |
} |
|
2231 |
} |
|
2232 |
} |
|
2233 |
} |
|
2234 |
|
|
2235 |
void remove(Arc arc) { |
|
2236 |
if (_left[arc] == INVALID) { |
|
2237 |
if (_right[arc] != INVALID) { |
|
2238 |
_parent.set(_right[arc], _parent[arc]); |
|
2239 |
} |
|
2240 |
if (_parent[arc] != INVALID) { |
|
2241 |
if (_left[_parent[arc]] == arc) { |
|
2242 |
_left.set(_parent[arc], _right[arc]); |
|
2243 |
} else { |
|
2244 |
_right.set(_parent[arc], _right[arc]); |
|
2245 |
} |
|
2246 |
} else { |
|
2247 |
_head.set(_g.source(arc), _right[arc]); |
|
2248 |
} |
|
2249 |
} else if (_right[arc] == INVALID) { |
|
2250 |
_parent.set(_left[arc], _parent[arc]); |
|
2251 |
if (_parent[arc] != INVALID) { |
|
2252 |
if (_left[_parent[arc]] == arc) { |
|
2253 |
_left.set(_parent[arc], _left[arc]); |
|
2254 |
} else { |
|
2255 |
_right.set(_parent[arc], _left[arc]); |
|
2256 |
} |
|
2257 |
} else { |
|
2258 |
_head.set(_g.source(arc), _left[arc]); |
|
2259 |
} |
|
2260 |
} else { |
|
2261 |
Arc e = _left[arc]; |
|
2262 |
if (_right[e] != INVALID) { |
|
2263 |
e = _right[e]; |
|
2264 |
while (_right[e] != INVALID) { |
|
2265 |
e = _right[e]; |
|
2266 |
} |
|
2267 |
Arc s = _parent[e]; |
|
2268 |
_right.set(_parent[e], _left[e]); |
|
2269 |
if (_left[e] != INVALID) { |
|
2270 |
_parent.set(_left[e], _parent[e]); |
|
2271 |
} |
|
2272 |
|
|
2273 |
_left.set(e, _left[arc]); |
|
2274 |
_parent.set(_left[arc], e); |
|
2275 |
_right.set(e, _right[arc]); |
|
2276 |
_parent.set(_right[arc], e); |
|
2277 |
|
|
2278 |
_parent.set(e, _parent[arc]); |
|
2279 |
if (_parent[arc] != INVALID) { |
|
2280 |
if (_left[_parent[arc]] == arc) { |
|
2281 |
_left.set(_parent[arc], e); |
|
2282 |
} else { |
|
2283 |
_right.set(_parent[arc], e); |
|
2284 |
} |
|
2285 |
} |
|
2286 |
splay(s); |
|
2287 |
} else { |
|
2288 |
_right.set(e, _right[arc]); |
|
2289 |
_parent.set(_right[arc], e); |
|
2290 |
|
|
2291 |
if (_parent[arc] != INVALID) { |
|
2292 |
if (_left[_parent[arc]] == arc) { |
|
2293 |
_left.set(_parent[arc], e); |
|
2294 |
} else { |
|
2295 |
_right.set(_parent[arc], e); |
|
2296 |
} |
|
2297 |
} else { |
|
2298 |
_head.set(_g.source(arc), e); |
|
2299 |
} |
|
2300 |
} |
|
2301 |
} |
|
2302 |
} |
|
2303 |
|
|
2304 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
2305 |
{ |
|
2306 |
int m=(a+b)/2; |
|
2307 |
Arc me=v[m]; |
|
2308 |
if (a < m) { |
|
2309 |
Arc left = refreshRec(v,a,m-1); |
|
2310 |
_left.set(me, left); |
|
2311 |
_parent.set(left, me); |
|
2312 |
} else { |
|
2313 |
_left.set(me, INVALID); |
|
2314 |
} |
|
2315 |
if (m < b) { |
|
2316 |
Arc right = refreshRec(v,m+1,b); |
|
2317 |
_right.set(me, right); |
|
2318 |
_parent.set(right, me); |
|
2319 |
} else { |
|
2320 |
_right.set(me, INVALID); |
|
2321 |
} |
|
2322 |
return me; |
|
2323 |
} |
|
2324 |
|
|
2325 |
void refresh() { |
|
2326 |
for(NodeIt n(_g);n!=INVALID;++n) { |
|
2327 |
std::vector<Arc> v; |
|
2328 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
2329 |
if(v.size()) { |
|
2330 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
2331 |
Arc head = refreshRec(v,0,v.size()-1); |
|
2332 |
_head.set(n, head); |
|
2333 |
_parent.set(head, INVALID); |
|
2334 |
} |
|
2335 |
else _head.set(n, INVALID); |
|
2336 |
} |
|
2337 |
} |
|
2338 |
|
|
2339 |
void zig(Arc v) { |
|
2340 |
Arc w = _parent[v]; |
|
2341 |
_parent.set(v, _parent[w]); |
|
2342 |
_parent.set(w, v); |
|
2343 |
_left.set(w, _right[v]); |
|
2344 |
_right.set(v, w); |
|
2345 |
if (_parent[v] != INVALID) { |
|
2346 |
if (_right[_parent[v]] == w) { |
|
2347 |
_right.set(_parent[v], v); |
|
2348 |
} else { |
|
2349 |
_left.set(_parent[v], v); |
|
2350 |
} |
|
2351 |
} |
|
2352 |
if (_left[w] != INVALID){ |
|
2353 |
_parent.set(_left[w], w); |
|
2354 |
} |
|
2355 |
} |
|
2356 |
|
|
2357 |
void zag(Arc v) { |
|
2358 |
Arc w = _parent[v]; |
|
2359 |
_parent.set(v, _parent[w]); |
|
2360 |
_parent.set(w, v); |
|
2361 |
_right.set(w, _left[v]); |
|
2362 |
_left.set(v, w); |
|
2363 |
if (_parent[v] != INVALID){ |
|
2364 |
if (_left[_parent[v]] == w) { |
|
2365 |
_left.set(_parent[v], v); |
|
2366 |
} else { |
|
2367 |
_right.set(_parent[v], v); |
|
2368 |
} |
|
2369 |
} |
|
2370 |
if (_right[w] != INVALID){ |
|
2371 |
_parent.set(_right[w], w); |
|
2372 |
} |
|
2373 |
} |
|
2374 |
|
|
2375 |
void splay(Arc v) { |
|
2376 |
while (_parent[v] != INVALID) { |
|
2377 |
if (v == _left[_parent[v]]) { |
|
2378 |
if (_parent[_parent[v]] == INVALID) { |
|
2379 |
zig(v); |
|
2380 |
} else { |
|
2381 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
|
2382 |
zig(_parent[v]); |
|
2383 |
zig(v); |
|
2384 |
} else { |
|
2385 |
zig(v); |
|
2386 |
zag(v); |
|
2387 |
} |
|
2388 |
} |
|
2389 |
} else { |
|
2390 |
if (_parent[_parent[v]] == INVALID) { |
|
2391 |
zag(v); |
|
2392 |
} else { |
|
2393 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
|
2394 |
zag(v); |
|
2395 |
zig(v); |
|
2396 |
} else { |
|
2397 |
zag(_parent[v]); |
|
2398 |
zag(v); |
|
2399 |
} |
|
2400 |
} |
|
2401 |
} |
|
2402 |
} |
|
2403 |
_head[_g.source(v)] = v; |
|
2404 |
} |
|
2405 |
|
|
2406 |
|
|
2407 |
public: |
|
2408 |
|
|
2409 |
///Find an arc between two nodes. |
|
2410 |
|
|
2411 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
2412 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
2413 |
///\param s The source node |
|
2414 |
///\param t The target node |
|
2415 |
///\return An arc from \c s to \c t if there exists, |
|
2416 |
///\ref INVALID otherwise. |
|
2417 |
Arc operator()(Node s, Node t) const |
|
2418 |
{ |
|
2419 |
Arc a = _head[s]; |
|
2420 |
while (true) { |
|
2421 |
if (_g.target(a) == t) { |
|
2422 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2423 |
return a; |
|
2424 |
} else if (t < _g.target(a)) { |
|
2425 |
if (_left[a] == INVALID) { |
|
2426 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2427 |
return INVALID; |
|
2428 |
} else { |
|
2429 |
a = _left[a]; |
|
2430 |
} |
|
2431 |
} else { |
|
2432 |
if (_right[a] == INVALID) { |
|
2433 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2434 |
return INVALID; |
|
2435 |
} else { |
|
2436 |
a = _right[a]; |
|
2437 |
} |
|
2438 |
} |
|
2439 |
} |
|
2440 |
} |
|
2441 |
|
|
2442 |
///Find the first arc between two nodes. |
|
2443 |
|
|
2444 |
///Find the first arc between two nodes in time |
|
2445 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
2446 |
/// outgoing arcs of \c s. |
|
2447 |
///\param s The source node |
|
2448 |
///\param t The target node |
|
2449 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
2450 |
/// otherwise. |
|
2451 |
Arc findFirst(Node s, Node t) const |
|
2452 |
{ |
|
2453 |
Arc a = _head[s]; |
|
2454 |
Arc r = INVALID; |
|
2455 |
while (true) { |
|
2456 |
if (_g.target(a) < t) { |
|
2457 |
if (_right[a] == INVALID) { |
|
2458 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2459 |
return r; |
|
2460 |
} else { |
|
2461 |
a = _right[a]; |
|
2462 |
} |
|
2463 |
} else { |
|
2464 |
if (_g.target(a) == t) { |
|
2465 |
r = a; |
|
2466 |
} |
|
2467 |
if (_left[a] == INVALID) { |
|
2468 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2469 |
return r; |
|
2470 |
} else { |
|
2471 |
a = _left[a]; |
|
2472 |
} |
|
2473 |
} |
|
2474 |
} |
|
2475 |
} |
|
2476 |
|
|
2477 |
///Find the next arc between two nodes. |
|
2478 |
|
|
2479 |
///Find the next arc between two nodes in time |
|
2480 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
2481 |
/// outgoing arcs of \c s. |
|
2482 |
///\param s The source node |
|
2483 |
///\param t The target node |
|
2484 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
2485 |
/// otherwise. |
|
2486 |
|
|
2487 |
///\note If \c e is not the result of the previous \c findFirst() |
|
2488 |
///operation then the amorized time bound can not be guaranteed. |
|
2489 |
#ifdef DOXYGEN |
|
2490 |
Arc findNext(Node s, Node t, Arc a) const |
|
2491 |
#else |
|
2492 |
Arc findNext(Node, Node t, Arc a) const |
|
2493 |
#endif |
|
2494 |
{ |
|
2495 |
if (_right[a] != INVALID) { |
|
2496 |
a = _right[a]; |
|
2497 |
while (_left[a] != INVALID) { |
|
2498 |
a = _left[a]; |
|
2499 |
} |
|
2500 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2501 |
} else { |
|
2502 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
|
2503 |
a = _parent[a]; |
|
2504 |
} |
|
2505 |
if (_parent[a] == INVALID) { |
|
2506 |
return INVALID; |
|
2507 |
} else { |
|
2508 |
a = _parent[a]; |
|
2509 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2510 |
} |
|
2511 |
} |
|
2512 |
if (_g.target(a) == t) return a; |
|
2513 |
else return INVALID; |
|
2514 |
} |
|
2515 |
|
|
2516 |
}; |
|
2517 |
|
|
2518 |
///Fast arc look up between given endpoints. |
|
2519 |
|
|
2520 |
///\ingroup gutils |
|
2521 |
///Using this class, you can find an arc in a digraph from a given |
|
2522 |
///source to a given target in time <em>O(log d)</em>, |
|
2523 |
///where <em>d</em> is the out-degree of the source node. |
|
2524 |
/// |
|
2525 |
///It is not possible to find \e all parallel arcs between two nodes. |
|
2526 |
///Use \ref AllArcLookUp for this purpose. |
|
2527 |
/// |
|
2528 |
///\warning This class is static, so you should refresh() (or at least |
|
2529 |
///refresh(Node)) this data structure |
|
2530 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
2531 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
2532 |
/// |
|
2533 |
///\tparam G The type of the underlying digraph. |
|
2534 |
/// |
|
2535 |
///\sa DynArcLookUp |
|
2536 |
///\sa AllArcLookUp |
|
2537 |
template<class G> |
|
2538 |
class ArcLookUp |
|
2539 |
{ |
|
2540 |
public: |
|
2541 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
2542 |
typedef G Digraph; |
|
2543 |
|
|
2544 |
protected: |
|
2545 |
const Digraph &_g; |
|
2546 |
typename Digraph::template NodeMap<Arc> _head; |
|
2547 |
typename Digraph::template ArcMap<Arc> _left; |
|
2548 |
typename Digraph::template ArcMap<Arc> _right; |
|
2549 |
|
|
2550 |
class ArcLess { |
|
2551 |
const Digraph &g; |
|
2552 |
public: |
|
2553 |
ArcLess(const Digraph &_g) : g(_g) {} |
|
2554 |
bool operator()(Arc a,Arc b) const |
|
2555 |
{ |
|
2556 |
return g.target(a)<g.target(b); |
|
2557 |
} |
|
2558 |
}; |
|
2559 |
|
|
2560 |
public: |
|
2561 |
|
|
2562 |
///Constructor |
|
2563 |
|
|
2564 |
///Constructor. |
|
2565 |
/// |
|
2566 |
///It builds up the search database, which remains valid until the digraph |
|
2567 |
///changes. |
|
2568 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
|
2569 |
|
|
2570 |
private: |
|
2571 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
2572 |
{ |
|
2573 |
int m=(a+b)/2; |
|
2574 |
Arc me=v[m]; |
|
2575 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
|
2576 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
|
2577 |
return me; |
|
2578 |
} |
|
2579 |
public: |
|
2580 |
///Refresh the data structure at a node. |
|
2581 |
|
|
2582 |
///Build up the search database of node \c n. |
|
2583 |
/// |
|
2584 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
2585 |
///the number of the outgoing arcs of \c n. |
|
2586 |
void refresh(Node n) |
|
2587 |
{ |
|
2588 |
std::vector<Arc> v; |
|
2589 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
2590 |
if(v.size()) { |
|
2591 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
2592 |
_head[n]=refreshRec(v,0,v.size()-1); |
|
2593 |
} |
|
2594 |
else _head[n]=INVALID; |
|
2595 |
} |
|
2596 |
///Refresh the full data structure. |
|
2597 |
|
|
2598 |
///Build up the full search database. In fact, it simply calls |
|
2599 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
2600 |
/// |
|
2601 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
2602 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
2603 |
///out-degree of the digraph. |
|
2604 |
|
|
2605 |
void refresh() |
|
2606 |
{ |
|
2607 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
|
2608 |
} |
|
2609 |
|
|
2610 |
///Find an arc between two nodes. |
|
2611 |
|
|
2612 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
2613 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
2614 |
///\param s The source node |
|
2615 |
///\param t The target node |
|
2616 |
///\return An arc from \c s to \c t if there exists, |
|
2617 |
///\ref INVALID otherwise. |
|
2618 |
/// |
|
2619 |
///\warning If you change the digraph, refresh() must be called before using |
|
2620 |
///this operator. If you change the outgoing arcs of |
|
2621 |
///a single node \c n, then |
|
2622 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
2623 |
/// |
|
2624 |
Arc operator()(Node s, Node t) const |
|
2625 |
{ |
|
2626 |
Arc e; |
|
2627 |
for(e=_head[s]; |
|
2628 |
e!=INVALID&&_g.target(e)!=t; |
|
2629 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
|
2630 |
return e; |
|
2631 |
} |
|
2632 |
|
|
2633 |
}; |
|
2634 |
|
|
2635 |
///Fast look up of all arcs between given endpoints. |
|
2636 |
|
|
2637 |
///\ingroup gutils |
|
2638 |
///This class is the same as \ref ArcLookUp, with the addition |
|
2639 |
///that it makes it possible to find all arcs between given endpoints. |
|
2640 |
/// |
|
2641 |
///\warning This class is static, so you should refresh() (or at least |
|
2642 |
///refresh(Node)) this data structure |
|
2643 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
2644 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
2645 |
/// |
|
2646 |
///\tparam G The type of the underlying digraph. |
|
2647 |
/// |
|
2648 |
///\sa DynArcLookUp |
|
2649 |
///\sa ArcLookUp |
|
2650 |
template<class G> |
|
2651 |
class AllArcLookUp : public ArcLookUp<G> |
|
2652 |
{ |
|
2653 |
using ArcLookUp<G>::_g; |
|
2654 |
using ArcLookUp<G>::_right; |
|
2655 |
using ArcLookUp<G>::_left; |
|
2656 |
using ArcLookUp<G>::_head; |
|
2657 |
|
|
2658 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
2659 |
typedef G Digraph; |
|
2660 |
|
|
2661 |
typename Digraph::template ArcMap<Arc> _next; |
|
2662 |
|
|
2663 |
Arc refreshNext(Arc head,Arc next=INVALID) |
|
2664 |
{ |
|
2665 |
if(head==INVALID) return next; |
|
2666 |
else { |
|
2667 |
next=refreshNext(_right[head],next); |
|
2668 |
// _next[head]=next; |
|
2669 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
|
2670 |
? next : INVALID; |
|
2671 |
return refreshNext(_left[head],head); |
|
2672 |
} |
|
2673 |
} |
|
2674 |
|
|
2675 |
void refreshNext() |
|
2676 |
{ |
|
2677 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
|
2678 |
} |
|
2679 |
|
|
2680 |
public: |
|
2681 |
///Constructor |
|
2682 |
|
|
2683 |
///Constructor. |
|
2684 |
/// |
|
2685 |
///It builds up the search database, which remains valid until the digraph |
|
2686 |
///changes. |
|
2687 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
|
2688 |
|
|
2689 |
///Refresh the data structure at a node. |
|
2690 |
|
|
2691 |
///Build up the search database of node \c n. |
|
2692 |
/// |
|
2693 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
2694 |
///the number of the outgoing arcs of \c n. |
|
2695 |
|
|
2696 |
void refresh(Node n) |
|
2697 |
{ |
|
2698 |
ArcLookUp<G>::refresh(n); |
|
2699 |
refreshNext(_head[n]); |
|
2700 |
} |
|
2701 |
|
|
2702 |
///Refresh the full data structure. |
|
2703 |
|
|
2704 |
///Build up the full search database. In fact, it simply calls |
|
2705 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
2706 |
/// |
|
2707 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
2708 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
2709 |
///out-degree of the digraph. |
|
2710 |
|
|
2711 |
void refresh() |
|
2712 |
{ |
|
2713 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
|
2714 |
} |
|
2715 |
|
|
2716 |
///Find an arc between two nodes. |
|
2717 |
|
|
2718 |
///Find an arc between two nodes. |
|
2719 |
///\param s The source node |
|
2720 |
///\param t The target node |
|
2721 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
|
2722 |
///not given, the operator finds the first appropriate arc. |
|
2723 |
///\return An arc from \c s to \c t after \c prev or |
|
2724 |
///\ref INVALID if there is no more. |
|
2725 |
/// |
|
2726 |
///For example, you can count the number of arcs from \c u to \c v in the |
|
2727 |
///following way. |
|
2728 |
///\code |
|
2729 |
///AllArcLookUp<ListDigraph> ae(g); |
|
2730 |
///... |
|
2731 |
///int n=0; |
|
2732 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
2733 |
///\endcode |
|
2734 |
/// |
|
2735 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
|
2736 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
2737 |
///consecutive arcs are found in constant time. |
|
2738 |
/// |
|
2739 |
///\warning If you change the digraph, refresh() must be called before using |
|
2740 |
///this operator. If you change the outgoing arcs of |
|
2741 |
///a single node \c n, then |
|
2742 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
2743 |
/// |
|
2744 |
#ifdef DOXYGEN |
|
2745 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
|
2746 |
#else |
|
2747 |
using ArcLookUp<G>::operator() ; |
|
2748 |
Arc operator()(Node s, Node t, Arc prev) const |
|
2749 |
{ |
|
2750 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
|
2751 |
} |
|
2752 |
#endif |
|
2753 |
|
|
2754 |
}; |
|
2755 |
|
|
2756 |
/// @} |
|
2757 |
|
|
2758 |
} //END OF NAMESPACE LEMON |
|
2759 |
|
|
2760 |
#endif |
0 comments (0 inline)