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