0
17
0
35
48
4
15
10
4
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
/** |
20 | 20 |
@defgroup datas Data Structures |
21 | 21 |
This group describes the several data structures implemented in LEMON. |
22 | 22 |
*/ |
23 | 23 |
|
24 | 24 |
/** |
25 | 25 |
@defgroup graphs Graph Structures |
26 | 26 |
@ingroup datas |
27 | 27 |
\brief Graph structures implemented in LEMON. |
28 | 28 |
|
29 | 29 |
The implementation of combinatorial algorithms heavily relies on |
30 | 30 |
efficient graph implementations. LEMON offers data structures which are |
31 | 31 |
planned to be easily used in an experimental phase of implementation studies, |
32 | 32 |
and thereafter the program code can be made efficient by small modifications. |
33 | 33 |
|
34 | 34 |
The most efficient implementation of diverse applications require the |
35 | 35 |
usage of different physical graph implementations. These differences |
36 | 36 |
appear in the size of graph we require to handle, memory or time usage |
37 | 37 |
limitations or in the set of operations through which the graph can be |
38 | 38 |
accessed. LEMON provides several physical graph structures to meet |
39 | 39 |
the diverging requirements of the possible users. In order to save on |
40 | 40 |
running time or on memory usage, some structures may fail to provide |
41 | 41 |
some graph features like arc/edge or node deletion. |
42 | 42 |
|
43 | 43 |
Alteration of standard containers need a very limited number of |
44 | 44 |
operations, these together satisfy the everyday requirements. |
45 | 45 |
In the case of graph structures, different operations are needed which do |
46 | 46 |
not alter the physical graph, but gives another view. If some nodes or |
47 | 47 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
48 | 48 |
this is the case. It also may happen that in a flow implementation |
49 | 49 |
the residual graph can be accessed by another algorithm, or a node-set |
50 | 50 |
is to be shrunk for another algorithm. |
51 | 51 |
LEMON also provides a variety of graphs for these requirements called |
52 | 52 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
53 | 53 |
in conjunction with other graph representations. |
54 | 54 |
|
55 | 55 |
You are free to use the graph structure that fit your requirements |
56 | 56 |
the best, most graph algorithms and auxiliary data structures can be used |
57 |
with any graph |
|
57 |
with any graph structure. |
|
58 |
|
|
59 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
|
58 | 60 |
*/ |
59 | 61 |
|
60 | 62 |
/** |
61 | 63 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
62 | 64 |
@ingroup graphs |
63 | 65 |
\brief Graph types between real graphs and graph adaptors. |
64 | 66 |
|
65 | 67 |
This group describes some graph types between real graphs and graph adaptors. |
66 | 68 |
These classes wrap graphs to give new functionality as the adaptors do it. |
67 | 69 |
On the other hand they are not light-weight structures as the adaptors. |
68 | 70 |
*/ |
69 | 71 |
|
70 | 72 |
/** |
71 | 73 |
@defgroup maps Maps |
72 | 74 |
@ingroup datas |
73 | 75 |
\brief Map structures implemented in LEMON. |
74 | 76 |
|
75 | 77 |
This group describes the map structures implemented in LEMON. |
76 | 78 |
|
77 |
LEMON provides several special purpose maps that e.g. combine |
|
79 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
|
78 | 80 |
new maps from existing ones. |
81 |
|
|
82 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
|
79 | 83 |
*/ |
80 | 84 |
|
81 | 85 |
/** |
82 | 86 |
@defgroup graph_maps Graph Maps |
83 | 87 |
@ingroup maps |
84 | 88 |
\brief Special graph-related maps. |
85 | 89 |
|
86 | 90 |
This group describes maps that are specifically designed to assign |
87 | 91 |
values to the nodes and arcs of graphs. |
88 | 92 |
*/ |
89 | 93 |
|
90 |
|
|
91 | 94 |
/** |
92 | 95 |
\defgroup map_adaptors Map Adaptors |
93 | 96 |
\ingroup maps |
94 | 97 |
\brief Tools to create new maps from existing ones |
95 | 98 |
|
96 | 99 |
This group describes map adaptors that are used to create "implicit" |
97 | 100 |
maps from other maps. |
98 | 101 |
|
99 | 102 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
100 | 103 |
They can make arithmetic and logical operations between one or two maps |
101 | 104 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
102 | 105 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
103 | 106 |
|
104 | 107 |
The typical usage of this classes is passing implicit maps to |
105 | 108 |
algorithms. If a function type algorithm is called then the function |
106 | 109 |
type map adaptors can be used comfortable. For example let's see the |
107 |
usage of map adaptors with the \c |
|
110 |
usage of map adaptors with the \c graphToEps() function. |
|
108 | 111 |
\code |
109 | 112 |
Color nodeColor(int deg) { |
110 | 113 |
if (deg >= 2) { |
111 | 114 |
return Color(0.5, 0.0, 0.5); |
112 | 115 |
} else if (deg == 1) { |
113 | 116 |
return Color(1.0, 0.5, 1.0); |
114 | 117 |
} else { |
115 | 118 |
return Color(0.0, 0.0, 0.0); |
116 | 119 |
} |
117 | 120 |
} |
118 | 121 |
|
119 | 122 |
Digraph::NodeMap<int> degree_map(graph); |
120 | 123 |
|
121 |
|
|
124 |
graphToEps(graph, "graph.eps") |
|
122 | 125 |
.coords(coords).scaleToA4().undirected() |
123 | 126 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
124 | 127 |
.run(); |
125 | 128 |
\endcode |
126 | 129 |
The \c functorToMap() function makes an \c int to \c Color map from the |
127 |
\ |
|
130 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
|
128 | 131 |
and the previously created map. The composed map is a proper function to |
129 | 132 |
get the color of each node. |
130 | 133 |
|
131 | 134 |
The usage with class type algorithms is little bit harder. In this |
132 | 135 |
case the function type map adaptors can not be used, because the |
133 | 136 |
function map adaptors give back temporary objects. |
134 | 137 |
\code |
135 | 138 |
Digraph graph; |
136 | 139 |
|
137 | 140 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
138 | 141 |
DoubleArcMap length(graph); |
139 | 142 |
DoubleArcMap speed(graph); |
140 | 143 |
|
141 | 144 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
142 | 145 |
TimeMap time(length, speed); |
143 | 146 |
|
144 | 147 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
145 | 148 |
dijkstra.run(source, target); |
146 | 149 |
\endcode |
147 | 150 |
We have a length map and a maximum speed map on the arcs of a digraph. |
148 | 151 |
The minimum time to pass the arc can be calculated as the division of |
149 | 152 |
the two maps which can be done implicitly with the \c DivMap template |
150 | 153 |
class. We use the implicit minimum time map as the length map of the |
151 | 154 |
\c Dijkstra algorithm. |
152 | 155 |
*/ |
153 | 156 |
|
154 | 157 |
/** |
155 | 158 |
@defgroup matrices Matrices |
156 | 159 |
@ingroup datas |
157 | 160 |
\brief Two dimensional data storages implemented in LEMON. |
158 | 161 |
|
159 | 162 |
This group describes two dimensional data storages implemented in LEMON. |
160 | 163 |
*/ |
161 | 164 |
|
162 | 165 |
/** |
163 | 166 |
@defgroup paths Path Structures |
164 | 167 |
@ingroup datas |
165 | 168 |
\brief Path structures implemented in LEMON. |
166 | 169 |
|
167 | 170 |
This group describes the path structures implemented in LEMON. |
168 | 171 |
|
169 | 172 |
LEMON provides flexible data structures to work with paths. |
170 | 173 |
All of them have similar interfaces and they can be copied easily with |
171 | 174 |
assignment operators and copy constructors. This makes it easy and |
172 | 175 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
173 | 176 |
any kind of path structure. |
174 | 177 |
|
175 | 178 |
\sa lemon::concepts::Path |
176 |
|
|
177 | 179 |
*/ |
178 | 180 |
|
179 | 181 |
/** |
180 | 182 |
@defgroup auxdat Auxiliary Data Structures |
181 | 183 |
@ingroup datas |
182 | 184 |
\brief Auxiliary data structures implemented in LEMON. |
183 | 185 |
|
184 | 186 |
This group describes some data structures implemented in LEMON in |
185 | 187 |
order to make it easier to implement combinatorial algorithms. |
186 | 188 |
*/ |
187 | 189 |
|
188 |
|
|
189 | 190 |
/** |
190 | 191 |
@defgroup algs Algorithms |
191 | 192 |
\brief This group describes the several algorithms |
192 | 193 |
implemented in LEMON. |
193 | 194 |
|
194 | 195 |
This group describes the several algorithms |
195 | 196 |
implemented in LEMON. |
196 | 197 |
*/ |
197 | 198 |
|
198 | 199 |
/** |
199 | 200 |
@defgroup search Graph Search |
200 | 201 |
@ingroup algs |
201 | 202 |
\brief Common graph search algorithms. |
202 | 203 |
|
203 | 204 |
This group describes the common graph search algorithms like |
204 |
Breadth- |
|
205 |
Breadth-First Search (BFS) and Depth-First Search (DFS). |
|
205 | 206 |
*/ |
206 | 207 |
|
207 | 208 |
/** |
208 |
@defgroup shortest_path Shortest Path |
|
209 |
@defgroup shortest_path Shortest Path Algorithms |
|
209 | 210 |
@ingroup algs |
210 | 211 |
\brief Algorithms for finding shortest paths. |
211 | 212 |
|
212 | 213 |
This group describes the algorithms for finding shortest paths in graphs. |
213 | 214 |
*/ |
214 | 215 |
|
215 | 216 |
/** |
216 |
@defgroup max_flow Maximum Flow |
|
217 |
@defgroup max_flow Maximum Flow Algorithms |
|
217 | 218 |
@ingroup algs |
218 | 219 |
\brief Algorithms for finding maximum flows. |
219 | 220 |
|
220 | 221 |
This group describes the algorithms for finding maximum flows and |
221 | 222 |
feasible circulations. |
222 | 223 |
|
223 | 224 |
The maximum flow problem is to find a flow between a single source and |
224 | 225 |
a single target that is maximum. Formally, there is a \f$G=(V,A)\f$ |
225 | 226 |
directed graph, an \f$c_a:A\rightarrow\mathbf{R}^+_0\f$ capacity |
226 | 227 |
function and given \f$s, t \in V\f$ source and target node. The |
227 | 228 |
maximum flow is the \f$f_a\f$ solution of the next optimization problem: |
228 | 229 |
|
229 | 230 |
\f[ 0 \le f_a \le c_a \f] |
230 | 231 |
\f[ \sum_{v\in\delta^{-}(u)}f_{vu}=\sum_{v\in\delta^{+}(u)}f_{uv} |
231 | 232 |
\qquad \forall u \in V \setminus \{s,t\}\f] |
232 | 233 |
\f[ \max \sum_{v\in\delta^{+}(s)}f_{uv} - \sum_{v\in\delta^{-}(s)}f_{vu}\f] |
233 | 234 |
|
234 | 235 |
LEMON contains several algorithms for solving maximum flow problems: |
235 | 236 |
- \ref lemon::EdmondsKarp "Edmonds-Karp" |
236 | 237 |
- \ref lemon::Preflow "Goldberg's Preflow algorithm" |
237 | 238 |
- \ref lemon::DinitzSleatorTarjan "Dinitz's blocking flow algorithm with dynamic trees" |
238 | 239 |
- \ref lemon::GoldbergTarjan "Preflow algorithm with dynamic trees" |
239 | 240 |
|
240 | 241 |
In most cases the \ref lemon::Preflow "Preflow" algorithm provides the |
241 | 242 |
fastest method to compute the maximum flow. All impelementations |
242 | 243 |
provides functions to query the minimum cut, which is the dual linear |
243 | 244 |
programming problem of the maximum flow. |
244 |
|
|
245 | 245 |
*/ |
246 | 246 |
|
247 | 247 |
/** |
248 |
@defgroup min_cost_flow Minimum Cost Flow |
|
248 |
@defgroup min_cost_flow Minimum Cost Flow Algorithms |
|
249 | 249 |
@ingroup algs |
250 | 250 |
|
251 | 251 |
\brief Algorithms for finding minimum cost flows and circulations. |
252 | 252 |
|
253 | 253 |
This group describes the algorithms for finding minimum cost flows and |
254 | 254 |
circulations. |
255 | 255 |
*/ |
256 | 256 |
|
257 | 257 |
/** |
258 |
@defgroup min_cut Minimum Cut |
|
258 |
@defgroup min_cut Minimum Cut Algorithms |
|
259 | 259 |
@ingroup algs |
260 | 260 |
|
261 | 261 |
\brief Algorithms for finding minimum cut in graphs. |
262 | 262 |
|
263 | 263 |
This group describes the algorithms for finding minimum cut in graphs. |
264 | 264 |
|
265 | 265 |
The minimum cut problem is to find a non-empty and non-complete |
266 | 266 |
\f$X\f$ subset of the vertices with minimum overall capacity on |
267 | 267 |
outgoing arcs. Formally, there is \f$G=(V,A)\f$ directed graph, an |
268 | 268 |
\f$c_a:A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
269 | 269 |
cut is the \f$X\f$ solution of the next optimization problem: |
270 | 270 |
|
271 | 271 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
272 | 272 |
\sum_{uv\in A, u\in X, v\not\in X}c_{uv}\f] |
273 | 273 |
|
274 | 274 |
LEMON contains several algorithms related to minimum cut problems: |
275 | 275 |
|
276 | 276 |
- \ref lemon::HaoOrlin "Hao-Orlin algorithm" to calculate minimum cut |
277 | 277 |
in directed graphs |
278 | 278 |
- \ref lemon::NagamochiIbaraki "Nagamochi-Ibaraki algorithm" to |
279 | 279 |
calculate minimum cut in undirected graphs |
280 | 280 |
- \ref lemon::GomoryHuTree "Gomory-Hu tree computation" to calculate all |
281 | 281 |
pairs minimum cut in undirected graphs |
282 | 282 |
|
283 | 283 |
If you want to find minimum cut just between two distinict nodes, |
284 | 284 |
please see the \ref max_flow "Maximum Flow page". |
285 |
|
|
286 | 285 |
*/ |
287 | 286 |
|
288 | 287 |
/** |
289 |
@defgroup graph_prop Connectivity and |
|
288 |
@defgroup graph_prop Connectivity and Other Graph Properties |
|
290 | 289 |
@ingroup algs |
291 | 290 |
\brief Algorithms for discovering the graph properties |
292 | 291 |
|
293 | 292 |
This group describes the algorithms for discovering the graph properties |
294 | 293 |
like connectivity, bipartiteness, euler property, simplicity etc. |
295 | 294 |
|
296 | 295 |
\image html edge_biconnected_components.png |
297 | 296 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
298 | 297 |
*/ |
299 | 298 |
|
300 | 299 |
/** |
301 |
@defgroup planar Planarity |
|
300 |
@defgroup planar Planarity Embedding and Drawing |
|
302 | 301 |
@ingroup algs |
303 | 302 |
\brief Algorithms for planarity checking, embedding and drawing |
304 | 303 |
|
305 | 304 |
This group describes the algorithms for planarity checking, |
306 | 305 |
embedding and drawing. |
307 | 306 |
|
308 | 307 |
\image html planar.png |
309 | 308 |
\image latex planar.eps "Plane graph" width=\textwidth |
310 | 309 |
*/ |
311 | 310 |
|
312 | 311 |
/** |
313 |
@defgroup matching Matching |
|
312 |
@defgroup matching Matching Algorithms |
|
314 | 313 |
@ingroup algs |
315 | 314 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
316 | 315 |
|
317 | 316 |
This group contains algorithm objects and functions to calculate |
318 | 317 |
matchings in graphs and bipartite graphs. The general matching problem is |
319 | 318 |
finding a subset of the arcs which does not shares common endpoints. |
320 | 319 |
|
321 | 320 |
There are several different algorithms for calculate matchings in |
322 | 321 |
graphs. The matching problems in bipartite graphs are generally |
323 | 322 |
easier than in general graphs. The goal of the matching optimization |
324 | 323 |
can be the finding maximum cardinality, maximum weight or minimum cost |
325 | 324 |
matching. The search can be constrained to find perfect or |
326 | 325 |
maximum cardinality matching. |
327 | 326 |
|
328 | 327 |
LEMON contains the next algorithms: |
329 | 328 |
- \ref lemon::MaxBipartiteMatching "MaxBipartiteMatching" Hopcroft-Karp |
330 | 329 |
augmenting path algorithm for calculate maximum cardinality matching in |
331 | 330 |
bipartite graphs |
332 | 331 |
- \ref lemon::PrBipartiteMatching "PrBipartiteMatching" Push-Relabel |
333 | 332 |
algorithm for calculate maximum cardinality matching in bipartite graphs |
334 | 333 |
- \ref lemon::MaxWeightedBipartiteMatching "MaxWeightedBipartiteMatching" |
335 | 334 |
Successive shortest path algorithm for calculate maximum weighted matching |
336 | 335 |
and maximum weighted bipartite matching in bipartite graph |
337 | 336 |
- \ref lemon::MinCostMaxBipartiteMatching "MinCostMaxBipartiteMatching" |
338 | 337 |
Successive shortest path algorithm for calculate minimum cost maximum |
339 | 338 |
matching in bipartite graph |
340 | 339 |
- \ref lemon::MaxMatching "MaxMatching" Edmond's blossom shrinking algorithm |
341 | 340 |
for calculate maximum cardinality matching in general graph |
342 | 341 |
- \ref lemon::MaxWeightedMatching "MaxWeightedMatching" Edmond's blossom |
343 | 342 |
shrinking algorithm for calculate maximum weighted matching in general |
344 | 343 |
graph |
345 | 344 |
- \ref lemon::MaxWeightedPerfectMatching "MaxWeightedPerfectMatching" |
346 | 345 |
Edmond's blossom shrinking algorithm for calculate maximum weighted |
347 | 346 |
perfect matching in general graph |
348 | 347 |
|
349 | 348 |
\image html bipartite_matching.png |
350 | 349 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
351 |
|
|
352 | 350 |
*/ |
353 | 351 |
|
354 | 352 |
/** |
355 |
@defgroup spantree Minimum Spanning Tree |
|
353 |
@defgroup spantree Minimum Spanning Tree Algorithms |
|
356 | 354 |
@ingroup algs |
357 | 355 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
358 | 356 |
|
359 | 357 |
This group describes the algorithms for finding a minimum cost spanning |
360 | 358 |
tree in a graph |
361 | 359 |
*/ |
362 | 360 |
|
363 |
|
|
364 | 361 |
/** |
365 |
@defgroup auxalg Auxiliary |
|
362 |
@defgroup auxalg Auxiliary Algorithms |
|
366 | 363 |
@ingroup algs |
367 | 364 |
\brief Auxiliary algorithms implemented in LEMON. |
368 | 365 |
|
369 | 366 |
This group describes some algorithms implemented in LEMON |
370 | 367 |
in order to make it easier to implement complex algorithms. |
371 | 368 |
*/ |
372 | 369 |
|
373 | 370 |
/** |
374 |
@defgroup approx Approximation |
|
371 |
@defgroup approx Approximation Algorithms |
|
372 |
@ingroup algs |
|
375 | 373 |
\brief Approximation algorithms. |
376 | 374 |
|
377 | 375 |
This group describes the approximation and heuristic algorithms |
378 | 376 |
implemented in LEMON. |
379 | 377 |
*/ |
380 | 378 |
|
381 | 379 |
/** |
382 | 380 |
@defgroup gen_opt_group General Optimization Tools |
383 | 381 |
\brief This group describes some general optimization frameworks |
384 | 382 |
implemented in LEMON. |
385 | 383 |
|
386 | 384 |
This group describes some general optimization frameworks |
387 | 385 |
implemented in LEMON. |
388 |
|
|
389 | 386 |
*/ |
390 | 387 |
|
391 | 388 |
/** |
392 |
@defgroup lp_group Lp and Mip |
|
389 |
@defgroup lp_group Lp and Mip Solvers |
|
393 | 390 |
@ingroup gen_opt_group |
394 | 391 |
\brief Lp and Mip solver interfaces for LEMON. |
395 | 392 |
|
396 | 393 |
This group describes Lp and Mip solver interfaces for LEMON. The |
397 | 394 |
various LP solvers could be used in the same manner with this |
398 | 395 |
interface. |
399 |
|
|
400 | 396 |
*/ |
401 | 397 |
|
402 | 398 |
/** |
403 |
@defgroup lp_utils Tools for Lp and Mip |
|
399 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
|
404 | 400 |
@ingroup lp_group |
405 | 401 |
\brief Helper tools to the Lp and Mip solvers. |
406 | 402 |
|
407 | 403 |
This group adds some helper tools to general optimization framework |
408 | 404 |
implemented in LEMON. |
409 | 405 |
*/ |
410 | 406 |
|
411 | 407 |
/** |
412 | 408 |
@defgroup metah Metaheuristics |
413 | 409 |
@ingroup gen_opt_group |
414 | 410 |
\brief Metaheuristics for LEMON library. |
415 | 411 |
|
416 | 412 |
This group describes some metaheuristic optimization tools. |
417 | 413 |
*/ |
418 | 414 |
|
419 | 415 |
/** |
420 | 416 |
@defgroup utils Tools and Utilities |
421 | 417 |
\brief Tools and utilities for programming in LEMON |
422 | 418 |
|
423 | 419 |
Tools and utilities for programming in LEMON. |
424 | 420 |
*/ |
425 | 421 |
|
426 | 422 |
/** |
427 | 423 |
@defgroup gutils Basic Graph Utilities |
428 | 424 |
@ingroup utils |
429 | 425 |
\brief Simple basic graph utilities. |
430 | 426 |
|
431 | 427 |
This group describes some simple basic graph utilities. |
432 | 428 |
*/ |
433 | 429 |
|
434 | 430 |
/** |
435 | 431 |
@defgroup misc Miscellaneous Tools |
436 | 432 |
@ingroup utils |
437 | 433 |
\brief Tools for development, debugging and testing. |
438 | 434 |
|
439 | 435 |
This group describes several useful tools for development, |
440 | 436 |
debugging and testing. |
441 | 437 |
*/ |
442 | 438 |
|
443 | 439 |
/** |
444 |
@defgroup timecount Time |
|
440 |
@defgroup timecount Time Measuring and Counting |
|
445 | 441 |
@ingroup misc |
446 | 442 |
\brief Simple tools for measuring the performance of algorithms. |
447 | 443 |
|
448 | 444 |
This group describes simple tools for measuring the performance |
449 | 445 |
of algorithms. |
450 | 446 |
*/ |
451 | 447 |
|
452 | 448 |
/** |
453 |
@defgroup graphbits Tools for Graph Implementation |
|
454 |
@ingroup utils |
|
455 |
\brief Tools to make it easier to create graphs. |
|
456 |
|
|
457 |
This group describes the tools that makes it easier to create graphs and |
|
458 |
the maps that dynamically update with the graph changes. |
|
459 |
*/ |
|
460 |
|
|
461 |
/** |
|
462 | 449 |
@defgroup exceptions Exceptions |
463 | 450 |
@ingroup utils |
464 | 451 |
\brief Exceptions defined in LEMON. |
465 | 452 |
|
466 | 453 |
This group describes the exceptions defined in LEMON. |
467 | 454 |
*/ |
468 | 455 |
|
469 | 456 |
/** |
470 | 457 |
@defgroup io_group Input-Output |
471 | 458 |
\brief Graph Input-Output methods |
472 | 459 |
|
473 | 460 |
This group describes the tools for importing and exporting graphs |
474 |
and graph related data. Now it supports the LEMON format, the |
|
475 |
\c DIMACS format and the encapsulated postscript (EPS) format. |
|
461 |
and graph related data. Now it supports the \ref lgf-format |
|
462 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
|
463 |
postscript (EPS) format. |
|
476 | 464 |
*/ |
477 | 465 |
|
478 | 466 |
/** |
479 | 467 |
@defgroup lemon_io LEMON Input-Output |
480 | 468 |
@ingroup io_group |
481 |
\brief Reading and writing |
|
469 |
\brief Reading and writing LEMON Graph Format. |
|
482 | 470 |
|
483 | 471 |
This group describes methods for reading and writing |
484 | 472 |
\ref lgf-format "LEMON Graph Format". |
485 | 473 |
*/ |
486 | 474 |
|
487 | 475 |
/** |
488 |
@defgroup eps_io Postscript |
|
476 |
@defgroup eps_io Postscript Exporting |
|
489 | 477 |
@ingroup io_group |
490 | 478 |
\brief General \c EPS drawer and graph exporter |
491 | 479 |
|
492 | 480 |
This group describes general \c EPS drawing methods and special |
493 | 481 |
graph exporting tools. |
494 | 482 |
*/ |
495 | 483 |
|
496 |
|
|
497 | 484 |
/** |
498 | 485 |
@defgroup concept Concepts |
499 | 486 |
\brief Skeleton classes and concept checking classes |
500 | 487 |
|
501 | 488 |
This group describes the data/algorithm skeletons and concept checking |
502 | 489 |
classes implemented in LEMON. |
503 | 490 |
|
504 | 491 |
The purpose of the classes in this group is fourfold. |
505 | 492 |
|
506 | 493 |
- These classes contain the documentations of the concepts. In order |
507 | 494 |
to avoid document multiplications, an implementation of a concept |
508 | 495 |
simply refers to the corresponding concept class. |
509 | 496 |
|
510 | 497 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
511 | 498 |
implementation of the concepts should provide, however completely |
512 | 499 |
without implementations and real data structures behind the |
513 | 500 |
interface. On the other hand they should provide nothing else. All |
514 | 501 |
the algorithms working on a data structure meeting a certain concept |
515 | 502 |
should compile with these classes. (Though it will not run properly, |
516 | 503 |
of course.) In this way it is easily to check if an algorithm |
517 | 504 |
doesn't use any extra feature of a certain implementation. |
518 | 505 |
|
519 | 506 |
- The concept descriptor classes also provide a <em>checker class</em> |
520 | 507 |
that makes it possible to check whether a certain implementation of a |
521 | 508 |
concept indeed provides all the required features. |
522 | 509 |
|
523 | 510 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
524 |
|
|
525 | 511 |
*/ |
526 | 512 |
|
527 |
|
|
528 | 513 |
/** |
529 | 514 |
@defgroup graph_concepts Graph Structure Concepts |
530 | 515 |
@ingroup concept |
531 | 516 |
\brief Skeleton and concept checking classes for graph structures |
532 | 517 |
|
533 | 518 |
This group describes the skeletons and concept checking classes of LEMON's |
534 | 519 |
graph structures and helper classes used to implement these. |
535 | 520 |
*/ |
536 | 521 |
|
537 |
/* --- Unused group |
|
538 |
@defgroup experimental Experimental Structures and Algorithms |
|
539 |
This group describes some Experimental structures and algorithms. |
|
540 |
The stuff here is subject to change. |
|
522 |
/** |
|
523 |
@defgroup map_concepts Map Concepts |
|
524 |
@ingroup concept |
|
525 |
\brief Skeleton and concept checking classes for maps |
|
526 |
|
|
527 |
This group describes the skeletons and concept checking classes of maps. |
|
541 | 528 |
*/ |
542 | 529 |
|
543 | 530 |
/** |
544 | 531 |
\anchor demoprograms |
545 | 532 |
|
546 | 533 |
@defgroup demos Demo programs |
547 | 534 |
|
548 | 535 |
Some demo programs are listed here. Their full source codes can be found in |
549 | 536 |
the \c demo subdirectory of the source tree. |
550 | 537 |
|
551 | 538 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
552 | 539 |
build the library. |
553 | 540 |
*/ |
554 | 541 |
|
555 | 542 |
/** |
556 | 543 |
@defgroup tools Standalone utility applications |
557 | 544 |
|
558 | 545 |
Some utility applications are listed here. |
559 | 546 |
|
560 | 547 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
561 | 548 |
them, as well. |
562 | 549 |
*/ |
563 | 550 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
/** |
20 | 20 |
\mainpage LEMON Documentation |
21 | 21 |
|
22 | 22 |
\section intro Introduction |
23 | 23 |
|
24 | 24 |
\subsection whatis What is LEMON |
25 | 25 |
|
26 | 26 |
LEMON stands for |
27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
29 | 29 |
It is a C++ template |
30 | 30 |
library aimed at combinatorial optimization tasks which |
31 | 31 |
often involve in working |
32 | 32 |
with graphs. |
33 | 33 |
|
34 | 34 |
<b> |
35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
36 | 36 |
project. |
37 | 37 |
You are free to use it in your commercial or |
38 | 38 |
non-commercial applications under very permissive |
39 | 39 |
\ref license "license terms". |
40 | 40 |
</b> |
41 | 41 |
|
42 | 42 |
\subsection howtoread How to read the documentation |
43 | 43 |
|
44 | 44 |
If you want to get a quick start and see the most important features then |
45 | 45 |
take a look at our \ref quicktour |
46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
47 | 47 |
|
48 | 48 |
If you already feel like using our library, see the page that tells you |
49 | 49 |
\ref getstart "How to start using LEMON". |
50 | 50 |
|
51 | 51 |
If you |
52 | 52 |
want to see how LEMON works, see |
53 |
some \ref demoprograms "demo programs" |
|
53 |
some \ref demoprograms "demo programs". |
|
54 | 54 |
|
55 | 55 |
If you know what you are looking for then try to find it under the |
56 | 56 |
<a class="el" href="modules.html">Modules</a> |
57 | 57 |
section. |
58 | 58 |
|
59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
|
59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
|
60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
|
60 | 61 |
*/ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
namespace lemon { |
20 | 20 |
/*! |
21 | 21 |
|
22 | 22 |
\page migration Migration from the 0.x Series |
23 | 23 |
|
24 | 24 |
This guide gives an in depth description on what has changed compared |
25 | 25 |
to the 0.x release series. |
26 | 26 |
|
27 | 27 |
Many of these changes adjusted automatically by the |
28 | 28 |
<tt>script/lemon-0.x-to-1.x.sh</tt> tool. Those requiring manual |
29 | 29 |
update are typeset <b>boldface</b>. |
30 | 30 |
|
31 | 31 |
\section migration-graph Graph Related Name Changes |
32 | 32 |
|
33 | 33 |
- \ref concepts::Digraph "Directed graphs" are called \c Digraph and |
34 | 34 |
they have <tt>Arc</tt>s (instead of <tt>Edge</tt>s), while |
35 | 35 |
\ref concepts::Graph "undirected graphs" are called \c Graph |
36 | 36 |
(instead of \c UGraph) and they have <tt>Edge</tt>s (instead of |
37 | 37 |
<tt>UEdge</tt>s). These changes reflected thoroughly everywhere in |
38 | 38 |
the library. Namely, |
39 | 39 |
- \c Graph -> \c Digraph |
40 | 40 |
- \c %ListGraph -> \c ListDigraph, \c %SmartGraph -> \c SmartDigraph etc. |
41 | 41 |
- \c UGraph -> \c Graph |
42 | 42 |
- \c ListUGraph -> \c ListGraph, \c SmartUGraph -> \c SmartGraph etc. |
43 | 43 |
- \c Edge -> \c Arc, \c UEdge -> \c Edge |
44 | 44 |
- \c EdgeMap -> \c ArcMap, \c UEdgeMap -> \c EdgeMap |
45 | 45 |
- \c EdgeIt -> \c ArcIt, \c UEdgeIt -> \c EdgeIt |
46 | 46 |
- Class names and function names containing the words \c graph, |
47 | 47 |
\c ugraph, \e edge or \e arc should also be updated. |
48 | 48 |
- <b>The two endpoints of an (\e undirected) \c Edge can be obtained by the |
49 | 49 |
<tt>u()</tt> and <tt>v()</tt> member function of the graph |
50 | 50 |
(instead of <tt>source()</tt> and <tt>target()</tt>). This change |
51 | 51 |
must be done by hand.</b> |
52 | 52 |
\n Of course, you can still use <tt>source()</tt> and <tt>target()</tt> |
53 | 53 |
for <tt>Arc</tt>s (directed edges). |
54 | 54 |
|
55 | 55 |
\warning |
56 | 56 |
<b>The <tt>script/lemon-0.x-to-1.x.sh</tt> tool replaces all instances of |
57 | 57 |
the words \c graph, \c digraph, \c edge and \c arc, so it replaces them |
58 | 58 |
in strings, comments etc. as well as in all identifiers.</b> |
59 | 59 |
|
60 |
\section migration-lgf LGF tools |
|
60 |
\section migration-lgf LGF tools |
|
61 | 61 |
- The \ref lgf-format "LGF file format" has changed, |
62 | 62 |
<tt>\@nodeset</tt> has changed to <tt>\@nodes</tt>, |
63 | 63 |
<tt>\@edgeset</tt> and <tt>\@uedgeset</tt> to <tt>\@arcs</tt> or |
64 | 64 |
<tt>\@edges</tt>, which become completely equivalents. The |
65 | 65 |
<tt>\@nodes</tt>, <tt>\@edges</tt> and <tt>\@uedges</tt> sections are |
66 | 66 |
removed from the format, the content of them should be |
67 | 67 |
the part of <tt>\@attributes</tt> section. The data fields in |
68 | 68 |
the sections must follow a strict format, they must be either character |
69 | 69 |
sequences without whitespaces or quoted strings. |
70 | 70 |
- The <tt>LemonReader</tt> and <tt>LemonWriter</tt> core interfaces |
71 | 71 |
are no longer available. |
72 | 72 |
- The implementation of the general section readers and writers has changed |
73 | 73 |
they are simple functors now. Beside the old |
74 | 74 |
stream based section handling, currently line oriented section |
75 | 75 |
reading and writing are also supported. In the |
76 | 76 |
section readers the lines must be counted manually. The sections |
77 | 77 |
should be read and written with the SectionWriter and SectionReader |
78 | 78 |
classes. |
79 | 79 |
- Instead of the item readers and writers, item converters should be |
80 | 80 |
used. The converters are functors, which map the type to |
81 | 81 |
std::string or std::string to the type. The converters for standard |
82 | 82 |
containers hasn't yet been implemented in the new LEMON. The converters |
83 | 83 |
can return strings in any format, because if it is necessary, the LGF |
84 | 84 |
writer and reader will quote and unquote the given value. |
85 | 85 |
- The DigraphReader and DigraphWriter can used similarly to the |
86 | 86 |
0.x series, however the <tt>read</tt> or <tt>write</tt> prefix of |
87 | 87 |
the member functions are removed. |
88 | 88 |
- The new LEMON supports the function like interface, the \c |
89 | 89 |
digraphReader and \c digraphWriter functions are more convenient than |
90 | 90 |
using the classes directly. |
91 | 91 |
|
92 | 92 |
\section migration-search BFS, DFS and Dijkstra |
93 | 93 |
- <b>Using the function interface of BFS, DFS and %Dijkstra both source and |
94 | 94 |
target nodes can be given as parameters of the <tt>run()</tt> function |
95 | 95 |
(instead of \c bfs(), \c dfs() or \c dijkstra() itself).</b> |
96 | 96 |
- \ref named-templ-param "Named class template parameters" of \c Bfs, |
97 | 97 |
\c Dfs, \c Dijkstra, \c BfsVisit, \c DfsVisit are renamed to start |
98 | 98 |
with "Set" instead of "Def". Namely, |
99 | 99 |
- \c DefPredMap -> \c SetPredMap |
100 | 100 |
- \c DefDistMap -> \c SetDistMap |
101 | 101 |
- \c DefReachedMap -> \c SetReachedMap |
102 | 102 |
- \c DefProcessedMap -> \c SetProcessedMap |
103 | 103 |
- \c DefHeap -> \c SetHeap |
104 | 104 |
- \c DefStandardHeap -> \c SetStandardHeap |
105 | 105 |
- \c DefOperationTraits -> \c SetOperationTraits |
106 | 106 |
- \c DefProcessedMapToBeDefaultMap -> \c SetStandardProcessedMap |
107 | 107 |
|
108 | 108 |
\section migration-error Exceptions and Debug tools |
109 | 109 |
|
110 | 110 |
<b>The class hierarchy of exceptions has largely been simplified. Now, |
111 | 111 |
only the i/o related tools may throw exceptions. All other exceptions |
112 | 112 |
have been replaced with either the \c LEMON_ASSERT or the \c LEMON_DEBUG |
113 | 113 |
macros.</b> |
114 | 114 |
|
115 | 115 |
<b>On the other hand, the parameter order of constructors of the |
116 | 116 |
exceptions has been changed. See \ref IoError and \ref FormatError for |
117 | 117 |
more details.</b> |
118 | 118 |
|
119 | 119 |
\section migration-other Others |
120 | 120 |
- <b>The contents of <tt>graph_utils.h</tt> are moved to <tt>core.h</tt> |
121 | 121 |
and <tt>maps.h</tt>. <tt>core.h</tt> is included by all graph types, |
122 | 122 |
therefore it usually do not have to be included directly.</b> |
123 | 123 |
- <b><tt>path_utils.h</tt> is merged to \c path.h.</b> |
124 | 124 |
- <b>The semantic of the assignment operations and copy constructors of maps |
125 | 125 |
are still under discussion. So, you must copy them by hand (i.e. copy |
126 | 126 |
each entry one-by-one)</b> |
127 | 127 |
- <b>The parameters of the graph copying tools (i.e. \c GraphCopy, |
128 | 128 |
\c DigraphCopy) have to be given in the from-to order.</b> |
129 | 129 |
- \c copyDigraph() and \c copyGraph() are renamed to \c digraphCopy() |
130 | 130 |
and \c graphCopy(), respectively. |
131 | 131 |
- <b>The interface of \ref DynArcLookUp has changed. It is now the same as |
132 | 132 |
of \ref ArcLookUp and \ref AllArcLookUp</b> |
133 | 133 |
- Some map types should also been renamed. Namely, |
134 | 134 |
- \c IntegerMap -> \c RangeMap |
135 | 135 |
- \c StdMap -> \c SparseMap |
136 | 136 |
- \c FunctorMap -> \c FunctorToMap |
137 | 137 |
- \c MapFunctor -> \c MapToFunctor |
138 | 138 |
- \c ForkWriteMap -> \c ForkMap |
139 | 139 |
- \c StoreBoolMap -> \c LoggerBoolMap |
140 | 140 |
- \c dim2::BoundingBox -> \c dim2::Box |
141 | 141 |
|
142 | 142 |
*/ |
143 | 143 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_ALTERATION_NOTIFIER_H |
20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
|
27 |
///\ingroup graphbits |
|
28 |
///\file |
|
29 |
|
|
27 |
//\ingroup graphbits |
|
28 |
//\file |
|
29 |
//\brief Observer notifier for graph alteration observers. |
|
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 |
/// \ingroup graphbits |
|
34 |
/// |
|
35 |
/// \brief Notifier class to notify observes about alterations in |
|
36 |
/// a container. |
|
37 |
/// |
|
38 |
/// The simple graph's can be refered as two containers, one node container |
|
39 |
/// and one edge container. But they are not standard containers they |
|
40 |
/// does not store values directly they are just key continars for more |
|
41 |
/// value containers which are the node and edge maps. |
|
42 |
/// |
|
43 |
/// The graph's node and edge sets can be changed as we add or erase |
|
44 |
/// nodes and edges in the graph. LEMON would like to handle easily |
|
45 |
/// that the node and edge maps should contain values for all nodes or |
|
46 |
/// edges. If we want to check on every indicing if the map contains |
|
47 |
/// the current indicing key that cause a drawback in the performance |
|
48 |
/// in the library. We use another solution we notify all maps about |
|
49 |
/// an alteration in the graph, which cause only drawback on the |
|
50 |
/// alteration of the graph. |
|
51 |
/// |
|
52 |
/// This class provides an interface to the container. The \e first() and \e |
|
53 |
/// next() member functions make possible to iterate on the keys of the |
|
54 |
/// container. The \e id() function returns an integer id for each key. |
|
55 |
/// The \e maxId() function gives back an upper bound of the ids. |
|
56 |
/// |
|
57 |
/// For the proper functonality of this class, we should notify it |
|
58 |
/// about each alteration in the container. The alterations have four type |
|
59 |
/// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
|
60 |
/// \e erase() signals that only one or few items added or erased to or |
|
61 |
/// from the graph. If all items are erased from the graph or from an empty |
|
62 |
/// graph a new graph is builded then it can be signaled with the |
|
63 |
/// clear() and build() members. Important rule that if we erase items |
|
64 |
/// from graph we should first signal the alteration and after that erase |
|
65 |
/// them from the container, on the other way on item addition we should |
|
66 |
/// first extend the container and just after that signal the alteration. |
|
67 |
/// |
|
68 |
/// The alteration can be observed with a class inherited from the |
|
69 |
/// \e ObserverBase nested class. The signals can be handled with |
|
70 |
/// overriding the virtual functions defined in the base class. The |
|
71 |
/// observer base can be attached to the notifier with the |
|
72 |
/// \e attach() member and can be detached with detach() function. The |
|
73 |
/// alteration handlers should not call any function which signals |
|
74 |
/// an other alteration in the same notifier and should not |
|
75 |
/// detach any observer from the notifier. |
|
76 |
/// |
|
77 |
/// Alteration observers try to be exception safe. If an \e add() or |
|
78 |
/// a \e clear() function throws an exception then the remaining |
|
79 |
/// observeres will not be notified and the fulfilled additions will |
|
80 |
/// be rolled back by calling the \e erase() or \e clear() |
|
81 |
/// functions. Thence the \e erase() and \e clear() should not throw |
|
82 |
/// exception. Actullay, it can be throw only \ref ImmediateDetach |
|
83 |
/// exception which detach the observer from the notifier. |
|
84 |
/// |
|
85 |
/// There are some place when the alteration observing is not completly |
|
86 |
/// reliable. If we want to carry out the node degree in the graph |
|
87 |
/// as in the \ref InDegMap and we use the reverseEdge that cause |
|
88 |
/// unreliable functionality. Because the alteration observing signals |
|
89 |
/// only erasing and adding but not the reversing it will stores bad |
|
90 |
/// degrees. The sub graph adaptors cannot signal the alterations because |
|
91 |
/// just a setting in the filter map can modify the graph and this cannot |
|
92 |
/// be watched in any way. |
|
93 |
/// |
|
94 |
/// \param _Container The container which is observed. |
|
95 |
// |
|
33 |
// \ingroup graphbits |
|
34 |
// |
|
35 |
// \brief Notifier class to notify observes about alterations in |
|
36 |
// a container. |
|
37 |
// |
|
38 |
// The simple graph's can be refered as two containers, one node container |
|
39 |
// and one edge container. But they are not standard containers they |
|
40 |
// does not store values directly they are just key continars for more |
|
41 |
// value containers which are the node and edge maps. |
|
42 |
// |
|
43 |
// The graph's node and edge sets can be changed as we add or erase |
|
44 |
// nodes and edges in the graph. LEMON would like to handle easily |
|
45 |
// that the node and edge maps should contain values for all nodes or |
|
46 |
// edges. If we want to check on every indicing if the map contains |
|
47 |
// the current indicing key that cause a drawback in the performance |
|
48 |
// in the library. We use another solution we notify all maps about |
|
49 |
// an alteration in the graph, which cause only drawback on the |
|
50 |
// alteration of the graph. |
|
51 |
// |
|
52 |
// This class provides an interface to the container. The \e first() and \e |
|
53 |
// next() member functions make possible to iterate on the keys of the |
|
54 |
// container. The \e id() function returns an integer id for each key. |
|
55 |
// The \e maxId() function gives back an upper bound of the ids. |
|
56 |
// |
|
57 |
// For the proper functonality of this class, we should notify it |
|
58 |
// about each alteration in the container. The alterations have four type |
|
59 |
// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
|
60 |
// \e erase() signals that only one or few items added or erased to or |
|
61 |
// from the graph. If all items are erased from the graph or from an empty |
|
62 |
// graph a new graph is builded then it can be signaled with the |
|
63 |
// clear() and build() members. Important rule that if we erase items |
|
64 |
// from graph we should first signal the alteration and after that erase |
|
65 |
// them from the container, on the other way on item addition we should |
|
66 |
// first extend the container and just after that signal the alteration. |
|
67 |
// |
|
68 |
// The alteration can be observed with a class inherited from the |
|
69 |
// \e ObserverBase nested class. The signals can be handled with |
|
70 |
// overriding the virtual functions defined in the base class. The |
|
71 |
// observer base can be attached to the notifier with the |
|
72 |
// \e attach() member and can be detached with detach() function. The |
|
73 |
// alteration handlers should not call any function which signals |
|
74 |
// an other alteration in the same notifier and should not |
|
75 |
// detach any observer from the notifier. |
|
76 |
// |
|
77 |
// Alteration observers try to be exception safe. If an \e add() or |
|
78 |
// a \e clear() function throws an exception then the remaining |
|
79 |
// observeres will not be notified and the fulfilled additions will |
|
80 |
// be rolled back by calling the \e erase() or \e clear() |
|
81 |
// functions. Thence the \e erase() and \e clear() should not throw |
|
82 |
// exception. Actullay, it can be throw only \ref ImmediateDetach |
|
83 |
// exception which detach the observer from the notifier. |
|
84 |
// |
|
85 |
// There are some place when the alteration observing is not completly |
|
86 |
// reliable. If we want to carry out the node degree in the graph |
|
87 |
// as in the \ref InDegMap and we use the reverseEdge that cause |
|
88 |
// unreliable functionality. Because the alteration observing signals |
|
89 |
// only erasing and adding but not the reversing it will stores bad |
|
90 |
// degrees. The sub graph adaptors cannot signal the alterations because |
|
91 |
// just a setting in the filter map can modify the graph and this cannot |
|
92 |
// be watched in any way. |
|
93 |
// |
|
94 |
// \param _Container The container which is observed. |
|
95 |
// \param _Item The item type which is obserbved. |
|
96 | 96 |
|
97 | 97 |
template <typename _Container, typename _Item> |
98 | 98 |
class AlterationNotifier { |
99 | 99 |
public: |
100 | 100 |
|
101 | 101 |
typedef True Notifier; |
102 | 102 |
|
103 | 103 |
typedef _Container Container; |
104 | 104 |
typedef _Item Item; |
105 | 105 |
|
106 |
/// \brief Exception which can be called from \e clear() and |
|
107 |
/// \e erase(). |
|
108 |
/// |
|
109 |
/// From the \e clear() and \e erase() function only this |
|
110 |
/// exception is allowed to throw. The exception immediatly |
|
111 |
/// detaches the current observer from the notifier. Because the |
|
112 |
/// \e clear() and \e erase() should not throw other exceptions |
|
113 |
/// it can be used to invalidate the observer. |
|
106 |
// \brief Exception which can be called from \e clear() and |
|
107 |
// \e erase(). |
|
108 |
// |
|
109 |
// From the \e clear() and \e erase() function only this |
|
110 |
// exception is allowed to throw. The exception immediatly |
|
111 |
// detaches the current observer from the notifier. Because the |
|
112 |
// \e clear() and \e erase() should not throw other exceptions |
|
113 |
// it can be used to invalidate the observer. |
|
114 | 114 |
struct ImmediateDetach {}; |
115 | 115 |
|
116 |
/// \brief ObserverBase is the base class for the observers. |
|
117 |
/// |
|
118 |
/// ObserverBase is the abstract base class for the observers. |
|
119 |
/// It will be notified about an item was inserted into or |
|
120 |
/// erased from the graph. |
|
121 |
/// |
|
122 |
/// The observer interface contains some pure virtual functions |
|
123 |
/// to override. The add() and erase() functions are |
|
124 |
/// to notify the oberver when one item is added or |
|
125 |
/// erased. |
|
126 |
/// |
|
127 |
/// The build() and clear() members are to notify the observer |
|
128 |
/// about the container is built from an empty container or |
|
129 |
/// is cleared to an empty container. |
|
130 |
|
|
116 |
// \brief ObserverBase is the base class for the observers. |
|
117 |
// |
|
118 |
// ObserverBase is the abstract base class for the observers. |
|
119 |
// It will be notified about an item was inserted into or |
|
120 |
// erased from the graph. |
|
121 |
// |
|
122 |
// The observer interface contains some pure virtual functions |
|
123 |
// to override. The add() and erase() functions are |
|
124 |
// to notify the oberver when one item is added or |
|
125 |
// erased. |
|
126 |
// |
|
127 |
// The build() and clear() members are to notify the observer |
|
128 |
// about the container is built from an empty container or |
|
129 |
// is cleared to an empty container. |
|
131 | 130 |
class ObserverBase { |
132 | 131 |
protected: |
133 | 132 |
typedef AlterationNotifier Notifier; |
134 | 133 |
|
135 | 134 |
friend class AlterationNotifier; |
136 | 135 |
|
137 |
/// \brief Default constructor. |
|
138 |
/// |
|
139 |
/// Default constructor for ObserverBase. |
|
140 |
/// |
|
136 |
// \brief Default constructor. |
|
137 |
// |
|
138 |
// Default constructor for ObserverBase. |
|
141 | 139 |
ObserverBase() : _notifier(0) {} |
142 | 140 |
|
143 |
/// \brief Constructor which attach the observer into notifier. |
|
144 |
/// |
|
145 |
// |
|
141 |
// \brief Constructor which attach the observer into notifier. |
|
142 |
// |
|
143 |
// Constructor which attach the observer into notifier. |
|
146 | 144 |
ObserverBase(AlterationNotifier& nf) { |
147 | 145 |
attach(nf); |
148 | 146 |
} |
149 | 147 |
|
150 |
/// \brief Constructor which attach the obserever to the same notifier. |
|
151 |
/// |
|
152 |
/// Constructor which attach the obserever to the same notifier as |
|
153 |
/// the other observer is attached to. |
|
148 |
// \brief Constructor which attach the obserever to the same notifier. |
|
149 |
// |
|
150 |
// Constructor which attach the obserever to the same notifier as |
|
151 |
// the other observer is attached to. |
|
154 | 152 |
ObserverBase(const ObserverBase& copy) { |
155 | 153 |
if (copy.attached()) { |
156 | 154 |
attach(*copy.notifier()); |
157 | 155 |
} |
158 | 156 |
} |
159 | 157 |
|
160 |
// |
|
158 |
// \brief Destructor |
|
161 | 159 |
virtual ~ObserverBase() { |
162 | 160 |
if (attached()) { |
163 | 161 |
detach(); |
164 | 162 |
} |
165 | 163 |
} |
166 | 164 |
|
167 |
/// \brief Attaches the observer into an AlterationNotifier. |
|
168 |
/// |
|
169 |
/// This member attaches the observer into an AlterationNotifier. |
|
170 |
/// |
|
165 |
// \brief Attaches the observer into an AlterationNotifier. |
|
166 |
// |
|
167 |
// This member attaches the observer into an AlterationNotifier. |
|
171 | 168 |
void attach(AlterationNotifier& nf) { |
172 | 169 |
nf.attach(*this); |
173 | 170 |
} |
174 | 171 |
|
175 |
/// \brief Detaches the observer into an AlterationNotifier. |
|
176 |
/// |
|
177 |
/// This member detaches the observer from an AlterationNotifier. |
|
178 |
/// |
|
172 |
// \brief Detaches the observer into an AlterationNotifier. |
|
173 |
// |
|
174 |
// This member detaches the observer from an AlterationNotifier. |
|
179 | 175 |
void detach() { |
180 | 176 |
_notifier->detach(*this); |
181 | 177 |
} |
182 | 178 |
|
183 |
/// \brief Gives back a pointer to the notifier which the map |
|
184 |
/// attached into. |
|
185 |
/// |
|
186 |
/// This function gives back a pointer to the notifier which the map |
|
187 |
/// attached into. |
|
188 |
/// |
|
179 |
// \brief Gives back a pointer to the notifier which the map |
|
180 |
// attached into. |
|
181 |
// |
|
182 |
// This function gives back a pointer to the notifier which the map |
|
183 |
// attached into. |
|
189 | 184 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
190 | 185 |
|
191 |
|
|
186 |
// Gives back true when the observer is attached into a notifier. |
|
192 | 187 |
bool attached() const { return _notifier != 0; } |
193 | 188 |
|
194 | 189 |
private: |
195 | 190 |
|
196 | 191 |
ObserverBase& operator=(const ObserverBase& copy); |
197 | 192 |
|
198 | 193 |
protected: |
199 | 194 |
|
200 | 195 |
Notifier* _notifier; |
201 | 196 |
typename std::list<ObserverBase*>::iterator _index; |
202 | 197 |
|
203 |
/// \brief The member function to notificate the observer about an |
|
204 |
/// item is added to the container. |
|
205 |
/// |
|
206 |
/// The add() member function notificates the observer about an item |
|
207 |
/// is added to the container. It have to be overrided in the |
|
208 |
/// subclasses. |
|
198 |
// \brief The member function to notificate the observer about an |
|
199 |
// item is added to the container. |
|
200 |
// |
|
201 |
// The add() member function notificates the observer about an item |
|
202 |
// is added to the container. It have to be overrided in the |
|
203 |
// subclasses. |
|
209 | 204 |
virtual void add(const Item&) = 0; |
210 | 205 |
|
211 |
/// \brief The member function to notificate the observer about |
|
212 |
/// more item is added to the container. |
|
213 |
/// |
|
214 |
/// The add() member function notificates the observer about more item |
|
215 |
/// is added to the container. It have to be overrided in the |
|
216 |
/// subclasses. |
|
206 |
// \brief The member function to notificate the observer about |
|
207 |
// more item is added to the container. |
|
208 |
// |
|
209 |
// The add() member function notificates the observer about more item |
|
210 |
// is added to the container. It have to be overrided in the |
|
211 |
// subclasses. |
|
217 | 212 |
virtual void add(const std::vector<Item>& items) = 0; |
218 | 213 |
|
219 |
/// \brief The member function to notificate the observer about an |
|
220 |
/// item is erased from the container. |
|
221 |
/// |
|
222 |
/// The erase() member function notificates the observer about an |
|
223 |
/// item is erased from the container. It have to be overrided in |
|
224 |
/// the subclasses. |
|
214 |
// \brief The member function to notificate the observer about an |
|
215 |
// item is erased from the container. |
|
216 |
// |
|
217 |
// The erase() member function notificates the observer about an |
|
218 |
// item is erased from the container. It have to be overrided in |
|
219 |
// the subclasses. |
|
225 | 220 |
virtual void erase(const Item&) = 0; |
226 | 221 |
|
227 |
/// \brief The member function to notificate the observer about |
|
228 |
/// more item is erased from the container. |
|
229 |
/// |
|
230 |
/// The erase() member function notificates the observer about more item |
|
231 |
/// is erased from the container. It have to be overrided in the |
|
232 |
/// subclasses. |
|
222 |
// \brief The member function to notificate the observer about |
|
223 |
// more item is erased from the container. |
|
224 |
// |
|
225 |
// The erase() member function notificates the observer about more item |
|
226 |
// is erased from the container. It have to be overrided in the |
|
227 |
// subclasses. |
|
233 | 228 |
virtual void erase(const std::vector<Item>& items) = 0; |
234 | 229 |
|
235 |
/// \brief The member function to notificate the observer about the |
|
236 |
/// container is built. |
|
237 |
/// |
|
238 |
/// The build() member function notificates the observer about the |
|
239 |
/// container is built from an empty container. It have to be |
|
240 |
/// overrided in the subclasses. |
|
241 |
|
|
230 |
// \brief The member function to notificate the observer about the |
|
231 |
// container is built. |
|
232 |
// |
|
233 |
// The build() member function notificates the observer about the |
|
234 |
// container is built from an empty container. It have to be |
|
235 |
// overrided in the subclasses. |
|
242 | 236 |
virtual void build() = 0; |
243 | 237 |
|
244 |
/// \brief The member function to notificate the observer about all |
|
245 |
/// items are erased from the container. |
|
246 |
/// |
|
247 |
/// The clear() member function notificates the observer about all |
|
248 |
/// items are erased from the container. It have to be overrided in |
|
249 |
/// the subclasses. |
|
238 |
// \brief The member function to notificate the observer about all |
|
239 |
// items are erased from the container. |
|
240 |
// |
|
241 |
// The clear() member function notificates the observer about all |
|
242 |
// items are erased from the container. It have to be overrided in |
|
243 |
// the subclasses. |
|
250 | 244 |
virtual void clear() = 0; |
251 | 245 |
|
252 | 246 |
}; |
253 | 247 |
|
254 | 248 |
protected: |
255 | 249 |
|
256 | 250 |
const Container* container; |
257 | 251 |
|
258 | 252 |
typedef std::list<ObserverBase*> Observers; |
259 | 253 |
Observers _observers; |
260 | 254 |
|
261 | 255 |
|
262 | 256 |
public: |
263 | 257 |
|
264 |
/// \brief Default constructor. |
|
265 |
/// |
|
266 |
/// The default constructor of the AlterationNotifier. |
|
267 |
/// It creates an empty notifier. |
|
258 |
// \brief Default constructor. |
|
259 |
// |
|
260 |
// The default constructor of the AlterationNotifier. |
|
261 |
// It creates an empty notifier. |
|
268 | 262 |
AlterationNotifier() |
269 | 263 |
: container(0) {} |
270 | 264 |
|
271 |
/// \brief Constructor. |
|
272 |
/// |
|
273 |
// |
|
265 |
// \brief Constructor. |
|
266 |
// |
|
267 |
// Constructor with the observed container parameter. |
|
274 | 268 |
AlterationNotifier(const Container& _container) |
275 | 269 |
: container(&_container) {} |
276 | 270 |
|
277 |
/// \brief Copy Constructor of the AlterationNotifier. |
|
278 |
/// |
|
279 |
/// Copy constructor of the AlterationNotifier. |
|
280 |
/// It creates only an empty notifier because the copiable |
|
281 |
// |
|
271 |
// \brief Copy Constructor of the AlterationNotifier. |
|
272 |
// |
|
273 |
// Copy constructor of the AlterationNotifier. |
|
274 |
// It creates only an empty notifier because the copiable |
|
275 |
// notifier's observers have to be registered still into that notifier. |
|
282 | 276 |
AlterationNotifier(const AlterationNotifier& _notifier) |
283 | 277 |
: container(_notifier.container) {} |
284 | 278 |
|
285 |
/// \brief Destructor. |
|
286 |
/// |
|
287 |
/// Destructor of the AlterationNotifier. |
|
288 |
/// |
|
279 |
// \brief Destructor. |
|
280 |
// |
|
281 |
// Destructor of the AlterationNotifier. |
|
289 | 282 |
~AlterationNotifier() { |
290 | 283 |
typename Observers::iterator it; |
291 | 284 |
for (it = _observers.begin(); it != _observers.end(); ++it) { |
292 | 285 |
(*it)->_notifier = 0; |
293 | 286 |
} |
294 | 287 |
} |
295 | 288 |
|
296 |
/// \brief Sets the container. |
|
297 |
/// |
|
298 |
// |
|
289 |
// \brief Sets the container. |
|
290 |
// |
|
291 |
// Sets the container. |
|
299 | 292 |
void setContainer(const Container& _container) { |
300 | 293 |
container = &_container; |
301 | 294 |
} |
302 | 295 |
|
303 | 296 |
protected: |
304 | 297 |
|
305 | 298 |
AlterationNotifier& operator=(const AlterationNotifier&); |
306 | 299 |
|
307 | 300 |
public: |
308 | 301 |
|
309 |
|
|
310 |
|
|
311 |
/// \brief First item in the container. |
|
312 |
/// |
|
313 |
/// Returns the first item in the container. It is |
|
314 |
/// for start the iteration on the container. |
|
302 |
// \brief First item in the container. |
|
303 |
// |
|
304 |
// Returns the first item in the container. It is |
|
305 |
// for start the iteration on the container. |
|
315 | 306 |
void first(Item& item) const { |
316 | 307 |
container->first(item); |
317 | 308 |
} |
318 | 309 |
|
319 |
/// \brief Next item in the container. |
|
320 |
/// |
|
321 |
/// Returns the next item in the container. It is |
|
322 |
/// for iterate on the container. |
|
310 |
// \brief Next item in the container. |
|
311 |
// |
|
312 |
// Returns the next item in the container. It is |
|
313 |
// for iterate on the container. |
|
323 | 314 |
void next(Item& item) const { |
324 | 315 |
container->next(item); |
325 | 316 |
} |
326 | 317 |
|
327 |
/// \brief Returns the id of the item. |
|
328 |
/// |
|
329 |
// |
|
318 |
// \brief Returns the id of the item. |
|
319 |
// |
|
320 |
// Returns the id of the item provided by the container. |
|
330 | 321 |
int id(const Item& item) const { |
331 | 322 |
return container->id(item); |
332 | 323 |
} |
333 | 324 |
|
334 |
/// \brief Returns the maximum id of the container. |
|
335 |
/// |
|
336 |
// |
|
325 |
// \brief Returns the maximum id of the container. |
|
326 |
// |
|
327 |
// Returns the maximum id of the container. |
|
337 | 328 |
int maxId() const { |
338 | 329 |
return container->maxId(Item()); |
339 | 330 |
} |
340 | 331 |
|
341 | 332 |
protected: |
342 | 333 |
|
343 | 334 |
void attach(ObserverBase& observer) { |
344 | 335 |
observer._index = _observers.insert(_observers.begin(), &observer); |
345 | 336 |
observer._notifier = this; |
346 | 337 |
} |
347 | 338 |
|
348 | 339 |
void detach(ObserverBase& observer) { |
349 | 340 |
_observers.erase(observer._index); |
350 | 341 |
observer._index = _observers.end(); |
351 | 342 |
observer._notifier = 0; |
352 | 343 |
} |
353 | 344 |
|
354 | 345 |
public: |
355 | 346 |
|
356 |
/// \brief Notifies all the registed observers about an item added to |
|
357 |
/// the container. |
|
358 |
/// |
|
359 |
/// It notifies all the registed observers about an item added to |
|
360 |
/// the container. |
|
361 |
/// |
|
347 |
// \brief Notifies all the registed observers about an item added to |
|
348 |
// the container. |
|
349 |
// |
|
350 |
// It notifies all the registed observers about an item added to |
|
351 |
// the container. |
|
362 | 352 |
void add(const Item& item) { |
363 | 353 |
typename Observers::reverse_iterator it; |
364 | 354 |
try { |
365 | 355 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
366 | 356 |
(*it)->add(item); |
367 | 357 |
} |
368 | 358 |
} catch (...) { |
369 | 359 |
typename Observers::iterator jt; |
370 | 360 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
371 | 361 |
(*jt)->erase(item); |
372 | 362 |
} |
373 | 363 |
throw; |
374 | 364 |
} |
375 | 365 |
} |
376 | 366 |
|
377 |
/// \brief Notifies all the registed observers about more item added to |
|
378 |
/// the container. |
|
379 |
/// |
|
380 |
/// It notifies all the registed observers about more item added to |
|
381 |
/// the container. |
|
382 |
/// |
|
367 |
// \brief Notifies all the registed observers about more item added to |
|
368 |
// the container. |
|
369 |
// |
|
370 |
// It notifies all the registed observers about more item added to |
|
371 |
// the container. |
|
383 | 372 |
void add(const std::vector<Item>& items) { |
384 | 373 |
typename Observers::reverse_iterator it; |
385 | 374 |
try { |
386 | 375 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
387 | 376 |
(*it)->add(items); |
388 | 377 |
} |
389 | 378 |
} catch (...) { |
390 | 379 |
typename Observers::iterator jt; |
391 | 380 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
392 | 381 |
(*jt)->erase(items); |
393 | 382 |
} |
394 | 383 |
throw; |
395 | 384 |
} |
396 | 385 |
} |
397 | 386 |
|
398 |
/// \brief Notifies all the registed observers about an item erased from |
|
399 |
/// the container. |
|
400 |
/// |
|
401 |
/// It notifies all the registed observers about an item erased from |
|
402 |
/// the container. |
|
403 |
/// |
|
387 |
// \brief Notifies all the registed observers about an item erased from |
|
388 |
// the container. |
|
389 |
// |
|
390 |
// It notifies all the registed observers about an item erased from |
|
391 |
// the container. |
|
404 | 392 |
void erase(const Item& item) throw() { |
405 | 393 |
typename Observers::iterator it = _observers.begin(); |
406 | 394 |
while (it != _observers.end()) { |
407 | 395 |
try { |
408 | 396 |
(*it)->erase(item); |
409 | 397 |
++it; |
410 | 398 |
} catch (const ImmediateDetach&) { |
411 | 399 |
(*it)->_index = _observers.end(); |
412 | 400 |
(*it)->_notifier = 0; |
413 | 401 |
it = _observers.erase(it); |
414 | 402 |
} |
415 | 403 |
} |
416 | 404 |
} |
417 | 405 |
|
418 |
/// \brief Notifies all the registed observers about more item erased |
|
419 |
/// from the container. |
|
420 |
/// |
|
421 |
/// It notifies all the registed observers about more item erased from |
|
422 |
/// the container. |
|
423 |
/// |
|
406 |
// \brief Notifies all the registed observers about more item erased |
|
407 |
// from the container. |
|
408 |
// |
|
409 |
// It notifies all the registed observers about more item erased from |
|
410 |
// the container. |
|
424 | 411 |
void erase(const std::vector<Item>& items) { |
425 | 412 |
typename Observers::iterator it = _observers.begin(); |
426 | 413 |
while (it != _observers.end()) { |
427 | 414 |
try { |
428 | 415 |
(*it)->erase(items); |
429 | 416 |
++it; |
430 | 417 |
} catch (const ImmediateDetach&) { |
431 | 418 |
(*it)->_index = _observers.end(); |
432 | 419 |
(*it)->_notifier = 0; |
433 | 420 |
it = _observers.erase(it); |
434 | 421 |
} |
435 | 422 |
} |
436 | 423 |
} |
437 | 424 |
|
438 |
/// \brief Notifies all the registed observers about the container is |
|
439 |
/// built. |
|
440 |
/// |
|
441 |
/// Notifies all the registed observers about the container is built |
|
442 |
// |
|
425 |
// \brief Notifies all the registed observers about the container is |
|
426 |
// built. |
|
427 |
// |
|
428 |
// Notifies all the registed observers about the container is built |
|
429 |
// from an empty container. |
|
443 | 430 |
void build() { |
444 | 431 |
typename Observers::reverse_iterator it; |
445 | 432 |
try { |
446 | 433 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
447 | 434 |
(*it)->build(); |
448 | 435 |
} |
449 | 436 |
} catch (...) { |
450 | 437 |
typename Observers::iterator jt; |
451 | 438 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
452 | 439 |
(*jt)->clear(); |
453 | 440 |
} |
454 | 441 |
throw; |
455 | 442 |
} |
456 | 443 |
} |
457 | 444 |
|
458 |
/// \brief Notifies all the registed observers about all items are |
|
459 |
/// erased. |
|
460 |
/// |
|
461 |
/// Notifies all the registed observers about all items are erased |
|
462 |
// |
|
445 |
// \brief Notifies all the registed observers about all items are |
|
446 |
// erased. |
|
447 |
// |
|
448 |
// Notifies all the registed observers about all items are erased |
|
449 |
// from the container. |
|
463 | 450 |
void clear() { |
464 | 451 |
typename Observers::iterator it = _observers.begin(); |
465 | 452 |
while (it != _observers.end()) { |
466 | 453 |
try { |
467 | 454 |
(*it)->clear(); |
468 | 455 |
++it; |
469 | 456 |
} catch (const ImmediateDetach&) { |
470 | 457 |
(*it)->_index = _observers.end(); |
471 | 458 |
(*it)->_notifier = 0; |
472 | 459 |
it = _observers.erase(it); |
473 | 460 |
} |
474 | 461 |
} |
475 | 462 |
} |
476 | 463 |
}; |
477 | 464 |
|
478 | 465 |
} |
479 | 466 |
|
480 | 467 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_ARRAY_MAP_H |
20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
21 | 21 |
|
22 | 22 |
#include <memory> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 |
/// \ingroup graphbits |
|
30 |
/// \file |
|
31 |
|
|
29 |
// \ingroup graphbits |
|
30 |
// \file |
|
31 |
// \brief Graph map based on the array storage. |
|
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 |
/// \ingroup graphbits |
|
36 |
/// |
|
37 |
/// \brief Graph map based on the array storage. |
|
38 |
/// |
|
39 |
/// The ArrayMap template class is graph map structure what |
|
40 |
/// automatically updates the map when a key is added to or erased from |
|
41 |
/// the map. This map uses the allocators to implement |
|
42 |
/// the container functionality. |
|
43 |
/// |
|
44 |
/// The template parameters are the Graph the current Item type and |
|
45 |
// |
|
35 |
// \ingroup graphbits |
|
36 |
// |
|
37 |
// \brief Graph map based on the array storage. |
|
38 |
// |
|
39 |
// The ArrayMap template class is graph map structure what |
|
40 |
// automatically updates the map when a key is added to or erased from |
|
41 |
// the map. This map uses the allocators to implement |
|
42 |
// the container functionality. |
|
43 |
// |
|
44 |
// The template parameters are the Graph the current Item type and |
|
45 |
// the Value type of the map. |
|
46 | 46 |
template <typename _Graph, typename _Item, typename _Value> |
47 | 47 |
class ArrayMap |
48 | 48 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
49 | 49 |
public: |
50 |
|
|
50 |
// The graph type of the maps. |
|
51 | 51 |
typedef _Graph Graph; |
52 |
|
|
52 |
// The item type of the map. |
|
53 | 53 |
typedef _Item Item; |
54 |
|
|
54 |
// The reference map tag. |
|
55 | 55 |
typedef True ReferenceMapTag; |
56 | 56 |
|
57 |
|
|
57 |
// The key type of the maps. |
|
58 | 58 |
typedef _Item Key; |
59 |
|
|
59 |
// The value type of the map. |
|
60 | 60 |
typedef _Value Value; |
61 | 61 |
|
62 |
|
|
62 |
// The const reference type of the map. |
|
63 | 63 |
typedef const _Value& ConstReference; |
64 |
|
|
64 |
// The reference type of the map. |
|
65 | 65 |
typedef _Value& Reference; |
66 | 66 |
|
67 |
|
|
67 |
// The notifier type. |
|
68 | 68 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
69 | 69 |
|
70 |
|
|
70 |
// The MapBase of the Map which imlements the core regisitry function. |
|
71 | 71 |
typedef typename Notifier::ObserverBase Parent; |
72 | 72 |
|
73 | 73 |
private: |
74 | 74 |
typedef std::allocator<Value> Allocator; |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
|
78 |
/// \brief Graph initialized map constructor. |
|
79 |
/// |
|
80 |
// |
|
78 |
// \brief Graph initialized map constructor. |
|
79 |
// |
|
80 |
// Graph initialized map constructor. |
|
81 | 81 |
explicit ArrayMap(const Graph& graph) { |
82 | 82 |
Parent::attach(graph.notifier(Item())); |
83 | 83 |
allocate_memory(); |
84 | 84 |
Notifier* nf = Parent::notifier(); |
85 | 85 |
Item it; |
86 | 86 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
87 | 87 |
int id = nf->id(it);; |
88 | 88 |
allocator.construct(&(values[id]), Value()); |
89 | 89 |
} |
90 | 90 |
} |
91 | 91 |
|
92 |
/// \brief Constructor to use default value to initialize the map. |
|
93 |
/// |
|
94 |
// |
|
92 |
// \brief Constructor to use default value to initialize the map. |
|
93 |
// |
|
94 |
// It constructs a map and initialize all of the the map. |
|
95 | 95 |
ArrayMap(const Graph& graph, const Value& value) { |
96 | 96 |
Parent::attach(graph.notifier(Item())); |
97 | 97 |
allocate_memory(); |
98 | 98 |
Notifier* nf = Parent::notifier(); |
99 | 99 |
Item it; |
100 | 100 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
101 | 101 |
int id = nf->id(it);; |
102 | 102 |
allocator.construct(&(values[id]), value); |
103 | 103 |
} |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
private: |
107 |
/// \brief Constructor to copy a map of the same map type. |
|
108 |
/// |
|
109 |
// |
|
107 |
// \brief Constructor to copy a map of the same map type. |
|
108 |
// |
|
109 |
// Constructor to copy a map of the same map type. |
|
110 | 110 |
ArrayMap(const ArrayMap& copy) : Parent() { |
111 | 111 |
if (copy.attached()) { |
112 | 112 |
attach(*copy.notifier()); |
113 | 113 |
} |
114 | 114 |
capacity = copy.capacity; |
115 | 115 |
if (capacity == 0) return; |
116 | 116 |
values = allocator.allocate(capacity); |
117 | 117 |
Notifier* nf = Parent::notifier(); |
118 | 118 |
Item it; |
119 | 119 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
120 | 120 |
int id = nf->id(it);; |
121 | 121 |
allocator.construct(&(values[id]), copy.values[id]); |
122 | 122 |
} |
123 | 123 |
} |
124 | 124 |
|
125 |
/// \brief Assign operator. |
|
126 |
/// |
|
127 |
/// This operator assigns for each item in the map the |
|
128 |
/// value mapped to the same item in the copied map. |
|
129 |
/// The parameter map should be indiced with the same |
|
130 |
/// itemset because this assign operator does not change |
|
131 |
// |
|
125 |
// \brief Assign operator. |
|
126 |
// |
|
127 |
// This operator assigns for each item in the map the |
|
128 |
// value mapped to the same item in the copied map. |
|
129 |
// The parameter map should be indiced with the same |
|
130 |
// itemset because this assign operator does not change |
|
131 |
// the container of the map. |
|
132 | 132 |
ArrayMap& operator=(const ArrayMap& cmap) { |
133 | 133 |
return operator=<ArrayMap>(cmap); |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
|
137 |
/// \brief Template assign operator. |
|
138 |
/// |
|
139 |
/// The given parameter should be conform to the ReadMap |
|
140 |
/// concecpt and could be indiced by the current item set of |
|
141 |
/// the NodeMap. In this case the value for each item |
|
142 |
/// is assigned by the value of the given ReadMap. |
|
137 |
// \brief Template assign operator. |
|
138 |
// |
|
139 |
// The given parameter should be conform to the ReadMap |
|
140 |
// concecpt and could be indiced by the current item set of |
|
141 |
// the NodeMap. In this case the value for each item |
|
142 |
// is assigned by the value of the given ReadMap. |
|
143 | 143 |
template <typename CMap> |
144 | 144 |
ArrayMap& operator=(const CMap& cmap) { |
145 | 145 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
146 | 146 |
const typename Parent::Notifier* nf = Parent::notifier(); |
147 | 147 |
Item it; |
148 | 148 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
149 | 149 |
set(it, cmap[it]); |
150 | 150 |
} |
151 | 151 |
return *this; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
public: |
155 |
/// \brief The destructor of the map. |
|
156 |
/// |
|
157 |
// |
|
155 |
// \brief The destructor of the map. |
|
156 |
// |
|
157 |
// The destructor of the map. |
|
158 | 158 |
virtual ~ArrayMap() { |
159 | 159 |
if (attached()) { |
160 | 160 |
clear(); |
161 | 161 |
detach(); |
162 | 162 |
} |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
protected: |
166 | 166 |
|
167 | 167 |
using Parent::attach; |
168 | 168 |
using Parent::detach; |
169 | 169 |
using Parent::attached; |
170 | 170 |
|
171 | 171 |
public: |
172 | 172 |
|
173 |
/// \brief The subscript operator. |
|
174 |
/// |
|
175 |
/// The subscript operator. The map can be subscripted by the |
|
176 |
/// actual keys of the graph. |
|
173 |
// \brief The subscript operator. |
|
174 |
// |
|
175 |
// The subscript operator. The map can be subscripted by the |
|
176 |
// actual keys of the graph. |
|
177 | 177 |
Value& operator[](const Key& key) { |
178 | 178 |
int id = Parent::notifier()->id(key); |
179 | 179 |
return values[id]; |
180 | 180 |
} |
181 | 181 |
|
182 |
/// \brief The const subscript operator. |
|
183 |
/// |
|
184 |
/// The const subscript operator. The map can be subscripted by the |
|
185 |
/// actual keys of the graph. |
|
182 |
// \brief The const subscript operator. |
|
183 |
// |
|
184 |
// The const subscript operator. The map can be subscripted by the |
|
185 |
// actual keys of the graph. |
|
186 | 186 |
const Value& operator[](const Key& key) const { |
187 | 187 |
int id = Parent::notifier()->id(key); |
188 | 188 |
return values[id]; |
189 | 189 |
} |
190 | 190 |
|
191 |
/// \brief Setter function of the map. |
|
192 |
/// |
|
193 |
/// Setter function of the map. Equivalent with map[key] = val. |
|
194 |
/// This is a compatibility feature with the not dereferable maps. |
|
191 |
// \brief Setter function of the map. |
|
192 |
// |
|
193 |
// Setter function of the map. Equivalent with map[key] = val. |
|
194 |
// This is a compatibility feature with the not dereferable maps. |
|
195 | 195 |
void set(const Key& key, const Value& val) { |
196 | 196 |
(*this)[key] = val; |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
protected: |
200 | 200 |
|
201 |
/// \brief Adds a new key to the map. |
|
202 |
/// |
|
203 |
/// It adds a new key to the map. It called by the observer notifier |
|
204 |
/// and it overrides the add() member function of the observer base. |
|
201 |
// \brief Adds a new key to the map. |
|
202 |
// |
|
203 |
// It adds a new key to the map. It called by the observer notifier |
|
204 |
// and it overrides the add() member function of the observer base. |
|
205 | 205 |
virtual void add(const Key& key) { |
206 | 206 |
Notifier* nf = Parent::notifier(); |
207 | 207 |
int id = nf->id(key); |
208 | 208 |
if (id >= capacity) { |
209 | 209 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
210 | 210 |
while (new_capacity <= id) { |
211 | 211 |
new_capacity <<= 1; |
212 | 212 |
} |
213 | 213 |
Value* new_values = allocator.allocate(new_capacity); |
214 | 214 |
Item it; |
215 | 215 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
216 | 216 |
int jd = nf->id(it);; |
217 | 217 |
if (id != jd) { |
218 | 218 |
allocator.construct(&(new_values[jd]), values[jd]); |
219 | 219 |
allocator.destroy(&(values[jd])); |
220 | 220 |
} |
221 | 221 |
} |
222 | 222 |
if (capacity != 0) allocator.deallocate(values, capacity); |
223 | 223 |
values = new_values; |
224 | 224 |
capacity = new_capacity; |
225 | 225 |
} |
226 | 226 |
allocator.construct(&(values[id]), Value()); |
227 | 227 |
} |
228 | 228 |
|
229 |
/// \brief Adds more new keys to the map. |
|
230 |
/// |
|
231 |
/// It adds more new keys to the map. It called by the observer notifier |
|
232 |
/// and it overrides the add() member function of the observer base. |
|
229 |
// \brief Adds more new keys to the map. |
|
230 |
// |
|
231 |
// It adds more new keys to the map. It called by the observer notifier |
|
232 |
// and it overrides the add() member function of the observer base. |
|
233 | 233 |
virtual void add(const std::vector<Key>& keys) { |
234 | 234 |
Notifier* nf = Parent::notifier(); |
235 | 235 |
int max_id = -1; |
236 | 236 |
for (int i = 0; i < int(keys.size()); ++i) { |
237 | 237 |
int id = nf->id(keys[i]); |
238 | 238 |
if (id > max_id) { |
239 | 239 |
max_id = id; |
240 | 240 |
} |
241 | 241 |
} |
242 | 242 |
if (max_id >= capacity) { |
243 | 243 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
244 | 244 |
while (new_capacity <= max_id) { |
245 | 245 |
new_capacity <<= 1; |
246 | 246 |
} |
247 | 247 |
Value* new_values = allocator.allocate(new_capacity); |
248 | 248 |
Item it; |
249 | 249 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
250 | 250 |
int id = nf->id(it); |
251 | 251 |
bool found = false; |
252 | 252 |
for (int i = 0; i < int(keys.size()); ++i) { |
253 | 253 |
int jd = nf->id(keys[i]); |
254 | 254 |
if (id == jd) { |
255 | 255 |
found = true; |
256 | 256 |
break; |
257 | 257 |
} |
258 | 258 |
} |
259 | 259 |
if (found) continue; |
260 | 260 |
allocator.construct(&(new_values[id]), values[id]); |
261 | 261 |
allocator.destroy(&(values[id])); |
262 | 262 |
} |
263 | 263 |
if (capacity != 0) allocator.deallocate(values, capacity); |
264 | 264 |
values = new_values; |
265 | 265 |
capacity = new_capacity; |
266 | 266 |
} |
267 | 267 |
for (int i = 0; i < int(keys.size()); ++i) { |
268 | 268 |
int id = nf->id(keys[i]); |
269 | 269 |
allocator.construct(&(values[id]), Value()); |
270 | 270 |
} |
271 | 271 |
} |
272 | 272 |
|
273 |
/// \brief Erase a key from the map. |
|
274 |
/// |
|
275 |
/// Erase a key from the map. It called by the observer notifier |
|
276 |
/// and it overrides the erase() member function of the observer base. |
|
273 |
// \brief Erase a key from the map. |
|
274 |
// |
|
275 |
// Erase a key from the map. It called by the observer notifier |
|
276 |
// and it overrides the erase() member function of the observer base. |
|
277 | 277 |
virtual void erase(const Key& key) { |
278 | 278 |
int id = Parent::notifier()->id(key); |
279 | 279 |
allocator.destroy(&(values[id])); |
280 | 280 |
} |
281 | 281 |
|
282 |
/// \brief Erase more keys from the map. |
|
283 |
/// |
|
284 |
/// Erase more keys from the map. It called by the observer notifier |
|
285 |
/// and it overrides the erase() member function of the observer base. |
|
282 |
// \brief Erase more keys from the map. |
|
283 |
// |
|
284 |
// Erase more keys from the map. It called by the observer notifier |
|
285 |
// and it overrides the erase() member function of the observer base. |
|
286 | 286 |
virtual void erase(const std::vector<Key>& keys) { |
287 | 287 |
for (int i = 0; i < int(keys.size()); ++i) { |
288 | 288 |
int id = Parent::notifier()->id(keys[i]); |
289 | 289 |
allocator.destroy(&(values[id])); |
290 | 290 |
} |
291 | 291 |
} |
292 | 292 |
|
293 |
/// \brief Buildes the map. |
|
294 |
/// |
|
295 |
/// It buildes the map. It called by the observer notifier |
|
296 |
/// and it overrides the build() member function of the observer base. |
|
293 |
// \brief Buildes the map. |
|
294 |
// |
|
295 |
// It buildes the map. It called by the observer notifier |
|
296 |
// and it overrides the build() member function of the observer base. |
|
297 | 297 |
virtual void build() { |
298 | 298 |
Notifier* nf = Parent::notifier(); |
299 | 299 |
allocate_memory(); |
300 | 300 |
Item it; |
301 | 301 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
302 | 302 |
int id = nf->id(it);; |
303 | 303 |
allocator.construct(&(values[id]), Value()); |
304 | 304 |
} |
305 | 305 |
} |
306 | 306 |
|
307 |
/// \brief Clear the map. |
|
308 |
/// |
|
309 |
/// It erase all items from the map. It called by the observer notifier |
|
310 |
/// and it overrides the clear() member function of the observer base. |
|
307 |
// \brief Clear the map. |
|
308 |
// |
|
309 |
// It erase all items from the map. It called by the observer notifier |
|
310 |
// and it overrides the clear() member function of the observer base. |
|
311 | 311 |
virtual void clear() { |
312 | 312 |
Notifier* nf = Parent::notifier(); |
313 | 313 |
if (capacity != 0) { |
314 | 314 |
Item it; |
315 | 315 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
316 | 316 |
int id = nf->id(it); |
317 | 317 |
allocator.destroy(&(values[id])); |
318 | 318 |
} |
319 | 319 |
allocator.deallocate(values, capacity); |
320 | 320 |
capacity = 0; |
321 | 321 |
} |
322 | 322 |
} |
323 | 323 |
|
324 | 324 |
private: |
325 | 325 |
|
326 | 326 |
void allocate_memory() { |
327 | 327 |
int max_id = Parent::notifier()->maxId(); |
328 | 328 |
if (max_id == -1) { |
329 | 329 |
capacity = 0; |
330 | 330 |
values = 0; |
331 | 331 |
return; |
332 | 332 |
} |
333 | 333 |
capacity = 1; |
334 | 334 |
while (capacity <= max_id) { |
335 | 335 |
capacity <<= 1; |
336 | 336 |
} |
337 | 337 |
values = allocator.allocate(capacity); |
338 | 338 |
} |
339 | 339 |
|
340 | 340 |
int capacity; |
341 | 341 |
Value* values; |
342 | 342 |
Allocator allocator; |
343 | 343 |
|
344 | 344 |
}; |
345 | 345 |
|
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
#include <lemon/bits/default_map.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 |
///\ingroup digraphbits |
|
32 |
///\file |
|
33 |
|
|
31 |
//\ingroup digraphbits |
|
32 |
//\file |
|
33 |
//\brief Extenders for the digraph types |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 |
/// \ingroup digraphbits |
|
37 |
/// |
|
38 |
// |
|
36 |
// \ingroup digraphbits |
|
37 |
// |
|
38 |
// \brief BaseDigraph to BaseGraph extender |
|
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 |
|
|
77 |
// First node of the edge |
|
78 | 78 |
Node u(const Edge &e) const { |
79 | 79 |
return Parent::source(e); |
80 | 80 |
} |
81 | 81 |
|
82 |
|
|
82 |
// Source of the given arc |
|
83 | 83 |
Node source(const Arc &e) const { |
84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
85 | 85 |
} |
86 | 86 |
|
87 |
|
|
87 |
// Second node of the edge |
|
88 | 88 |
Node v(const Edge &e) const { |
89 | 89 |
return Parent::target(e); |
90 | 90 |
} |
91 | 91 |
|
92 |
|
|
92 |
// Target of the given arc |
|
93 | 93 |
Node target(const Arc &e) const { |
94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
95 | 95 |
} |
96 | 96 |
|
97 |
/// \brief Directed arc from an edge. |
|
98 |
/// |
|
99 |
/// Returns a directed arc corresponding to the specified edge. |
|
100 |
/// If the given bool is true, the first node of the given edge and |
|
101 |
// |
|
97 |
// \brief Directed arc from an edge. |
|
98 |
// |
|
99 |
// Returns a directed arc corresponding to the specified edge. |
|
100 |
// If the given bool is true, the first node of the given edge and |
|
101 |
// the source node of the returned arc are the same. |
|
102 | 102 |
static Arc direct(const Edge &e, bool d) { |
103 | 103 |
return Arc(e, d); |
104 | 104 |
} |
105 | 105 |
|
106 |
/// Returns whether the given directed arc has the same orientation |
|
107 |
/// as the corresponding edge. |
|
106 |
// Returns whether the given directed arc has the same orientation |
|
107 |
// as the corresponding edge. |
|
108 | 108 |
static bool direction(const Arc &a) { return a.forward; } |
109 | 109 |
|
110 | 110 |
using Parent::first; |
111 | 111 |
using Parent::next; |
112 | 112 |
|
113 | 113 |
void first(Arc &e) const { |
114 | 114 |
Parent::first(e); |
115 | 115 |
e.forward=true; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
void next(Arc &e) const { |
119 | 119 |
if( e.forward ) { |
120 | 120 |
e.forward = false; |
121 | 121 |
} |
122 | 122 |
else { |
123 | 123 |
Parent::next(e); |
124 | 124 |
e.forward = true; |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
void firstOut(Arc &e, const Node &n) const { |
129 | 129 |
Parent::firstIn(e,n); |
130 | 130 |
if( Edge(e) != INVALID ) { |
131 | 131 |
e.forward = false; |
132 | 132 |
} |
133 | 133 |
else { |
134 | 134 |
Parent::firstOut(e,n); |
135 | 135 |
e.forward = true; |
136 | 136 |
} |
137 | 137 |
} |
138 | 138 |
void nextOut(Arc &e) const { |
139 | 139 |
if( ! e.forward ) { |
140 | 140 |
Node n = Parent::target(e); |
141 | 141 |
Parent::nextIn(e); |
142 | 142 |
if( Edge(e) == INVALID ) { |
143 | 143 |
Parent::firstOut(e, n); |
144 | 144 |
e.forward = true; |
145 | 145 |
} |
146 | 146 |
} |
147 | 147 |
else { |
148 | 148 |
Parent::nextOut(e); |
149 | 149 |
} |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
void firstIn(Arc &e, const Node &n) const { |
153 | 153 |
Parent::firstOut(e,n); |
154 | 154 |
if( Edge(e) != INVALID ) { |
155 | 155 |
e.forward = false; |
156 | 156 |
} |
157 | 157 |
else { |
158 | 158 |
Parent::firstIn(e,n); |
159 | 159 |
e.forward = true; |
160 | 160 |
} |
161 | 161 |
} |
162 | 162 |
void nextIn(Arc &e) const { |
163 | 163 |
if( ! e.forward ) { |
164 | 164 |
Node n = Parent::source(e); |
165 | 165 |
Parent::nextOut(e); |
166 | 166 |
if( Edge(e) == INVALID ) { |
167 | 167 |
Parent::firstIn(e, n); |
168 | 168 |
e.forward = true; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
else { |
172 | 172 |
Parent::nextIn(e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
177 | 177 |
d = true; |
178 | 178 |
Parent::firstOut(e, n); |
179 | 179 |
if (e != INVALID) return; |
180 | 180 |
d = false; |
181 | 181 |
Parent::firstIn(e, n); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
void nextInc(Edge &e, bool &d) const { |
185 | 185 |
if (d) { |
186 | 186 |
Node s = Parent::source(e); |
187 | 187 |
Parent::nextOut(e); |
188 | 188 |
if (e != INVALID) return; |
189 | 189 |
d = false; |
190 | 190 |
Parent::firstIn(e, s); |
191 | 191 |
} else { |
192 | 192 |
Parent::nextIn(e); |
193 | 193 |
} |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
Node nodeFromId(int ix) const { |
197 | 197 |
return Parent::nodeFromId(ix); |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
Arc arcFromId(int ix) const { |
201 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
Edge edgeFromId(int ix) const { |
205 | 205 |
return Parent::arcFromId(ix); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
int id(const Node &n) const { |
209 | 209 |
return Parent::id(n); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
int id(const Edge &e) const { |
213 | 213 |
return Parent::id(e); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
int id(const Arc &e) const { |
217 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
int maxNodeId() const { |
221 | 221 |
return Parent::maxNodeId(); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
int maxArcId() const { |
225 | 225 |
return 2 * Parent::maxArcId() + 1; |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
int maxEdgeId() const { |
229 | 229 |
return Parent::maxArcId(); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
int arcNum() const { |
233 | 233 |
return 2 * Parent::arcNum(); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
int edgeNum() const { |
237 | 237 |
return Parent::arcNum(); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
241 | 241 |
if (p == INVALID) { |
242 | 242 |
Edge arc = Parent::findArc(s, t); |
243 | 243 |
if (arc != INVALID) return direct(arc, true); |
244 | 244 |
arc = Parent::findArc(t, s); |
245 | 245 |
if (arc != INVALID) return direct(arc, false); |
246 | 246 |
} else if (direction(p)) { |
247 | 247 |
Edge arc = Parent::findArc(s, t, p); |
248 | 248 |
if (arc != INVALID) return direct(arc, true); |
249 | 249 |
arc = Parent::findArc(t, s); |
250 | 250 |
if (arc != INVALID) return direct(arc, false); |
251 | 251 |
} else { |
252 | 252 |
Edge arc = Parent::findArc(t, s, p); |
253 | 253 |
if (arc != INVALID) return direct(arc, false); |
254 | 254 |
} |
255 | 255 |
return INVALID; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
259 | 259 |
if (s != t) { |
260 | 260 |
if (p == INVALID) { |
261 | 261 |
Edge arc = Parent::findArc(s, t); |
262 | 262 |
if (arc != INVALID) return arc; |
263 | 263 |
arc = Parent::findArc(t, s); |
264 | 264 |
if (arc != INVALID) return arc; |
265 | 265 |
} else if (Parent::s(p) == s) { |
266 | 266 |
Edge arc = Parent::findArc(s, t, p); |
267 | 267 |
if (arc != INVALID) return arc; |
268 | 268 |
arc = Parent::findArc(t, s); |
269 | 269 |
if (arc != INVALID) return arc; |
270 | 270 |
} else { |
271 | 271 |
Edge arc = Parent::findArc(t, s, p); |
272 | 272 |
if (arc != INVALID) return arc; |
273 | 273 |
} |
274 | 274 |
} else { |
275 | 275 |
return Parent::findArc(s, t, p); |
276 | 276 |
} |
277 | 277 |
return INVALID; |
278 | 278 |
} |
279 | 279 |
}; |
280 | 280 |
|
281 | 281 |
template <typename Base> |
282 | 282 |
class BidirBpGraphExtender : public Base { |
283 | 283 |
public: |
284 | 284 |
typedef Base Parent; |
285 | 285 |
typedef BidirBpGraphExtender Digraph; |
286 | 286 |
|
287 | 287 |
typedef typename Parent::Node Node; |
288 | 288 |
typedef typename Parent::Edge Edge; |
289 | 289 |
|
290 | 290 |
|
291 | 291 |
using Parent::first; |
292 | 292 |
using Parent::next; |
293 | 293 |
|
294 | 294 |
using Parent::id; |
295 | 295 |
|
296 | 296 |
class Red : public Node { |
297 | 297 |
friend class BidirBpGraphExtender; |
298 | 298 |
public: |
299 | 299 |
Red() {} |
300 | 300 |
Red(const Node& node) : Node(node) { |
301 | 301 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
302 | 302 |
typename Parent::NodeSetError()); |
303 | 303 |
} |
304 | 304 |
Red& operator=(const Node& node) { |
305 | 305 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
306 | 306 |
typename Parent::NodeSetError()); |
307 | 307 |
Node::operator=(node); |
308 | 308 |
return *this; |
309 | 309 |
} |
310 | 310 |
Red(Invalid) : Node(INVALID) {} |
311 | 311 |
Red& operator=(Invalid) { |
312 | 312 |
Node::operator=(INVALID); |
313 | 313 |
return *this; |
314 | 314 |
} |
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
void first(Red& node) const { |
318 | 318 |
Parent::firstRed(static_cast<Node&>(node)); |
319 | 319 |
} |
320 | 320 |
void next(Red& node) const { |
321 | 321 |
Parent::nextRed(static_cast<Node&>(node)); |
322 | 322 |
} |
323 | 323 |
|
324 | 324 |
int id(const Red& node) const { |
325 | 325 |
return Parent::redId(node); |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
class Blue : public Node { |
329 | 329 |
friend class BidirBpGraphExtender; |
330 | 330 |
public: |
331 | 331 |
Blue() {} |
332 | 332 |
Blue(const Node& node) : Node(node) { |
333 | 333 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
334 | 334 |
typename Parent::NodeSetError()); |
335 | 335 |
} |
336 | 336 |
Blue& operator=(const Node& node) { |
337 | 337 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
338 | 338 |
typename Parent::NodeSetError()); |
339 | 339 |
Node::operator=(node); |
340 | 340 |
return *this; |
341 | 341 |
} |
342 | 342 |
Blue(Invalid) : Node(INVALID) {} |
343 | 343 |
Blue& operator=(Invalid) { |
344 | 344 |
Node::operator=(INVALID); |
345 | 345 |
return *this; |
346 | 346 |
} |
347 | 347 |
}; |
348 | 348 |
|
349 | 349 |
void first(Blue& node) const { |
350 | 350 |
Parent::firstBlue(static_cast<Node&>(node)); |
351 | 351 |
} |
352 | 352 |
void next(Blue& node) const { |
353 | 353 |
Parent::nextBlue(static_cast<Node&>(node)); |
354 | 354 |
} |
355 | 355 |
|
356 | 356 |
int id(const Blue& node) const { |
357 | 357 |
return Parent::redId(node); |
358 | 358 |
} |
359 | 359 |
|
360 | 360 |
Node source(const Edge& arc) const { |
361 | 361 |
return red(arc); |
362 | 362 |
} |
363 | 363 |
Node target(const Edge& arc) const { |
364 | 364 |
return blue(arc); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
368 | 368 |
if (Parent::red(node)) { |
369 | 369 |
Parent::firstFromRed(arc, node); |
370 | 370 |
dir = true; |
371 | 371 |
} else { |
372 | 372 |
Parent::firstFromBlue(arc, node); |
373 | 373 |
dir = static_cast<Edge&>(arc) == INVALID; |
374 | 374 |
} |
375 | 375 |
} |
376 | 376 |
void nextInc(Edge& arc, bool& dir) const { |
377 | 377 |
if (dir) { |
378 | 378 |
Parent::nextFromRed(arc); |
379 | 379 |
} else { |
380 | 380 |
Parent::nextFromBlue(arc); |
381 | 381 |
if (arc == INVALID) dir = true; |
382 | 382 |
} |
383 | 383 |
} |
384 | 384 |
|
385 | 385 |
class Arc : public Edge { |
386 | 386 |
friend class BidirBpGraphExtender; |
387 | 387 |
protected: |
388 | 388 |
bool forward; |
389 | 389 |
|
390 | 390 |
Arc(const Edge& arc, bool _forward) |
391 | 391 |
: Edge(arc), forward(_forward) {} |
392 | 392 |
|
393 | 393 |
public: |
394 | 394 |
Arc() {} |
395 | 395 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
396 | 396 |
bool operator==(const Arc& i) const { |
397 | 397 |
return Edge::operator==(i) && forward == i.forward; |
398 | 398 |
} |
399 | 399 |
bool operator!=(const Arc& i) const { |
400 | 400 |
return Edge::operator!=(i) || forward != i.forward; |
401 | 401 |
} |
402 | 402 |
bool operator<(const Arc& i) const { |
403 | 403 |
return Edge::operator<(i) || |
404 | 404 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
405 | 405 |
} |
406 | 406 |
}; |
407 | 407 |
|
408 | 408 |
void first(Arc& arc) const { |
409 | 409 |
Parent::first(static_cast<Edge&>(arc)); |
410 | 410 |
arc.forward = true; |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
void next(Arc& arc) const { |
414 | 414 |
if (!arc.forward) { |
415 | 415 |
Parent::next(static_cast<Edge&>(arc)); |
416 | 416 |
} |
417 | 417 |
arc.forward = !arc.forward; |
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
void firstOut(Arc& arc, const Node& node) const { |
421 | 421 |
if (Parent::red(node)) { |
422 | 422 |
Parent::firstFromRed(arc, node); |
423 | 423 |
arc.forward = true; |
424 | 424 |
} else { |
425 | 425 |
Parent::firstFromBlue(arc, node); |
426 | 426 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
427 | 427 |
} |
428 | 428 |
} |
429 | 429 |
void nextOut(Arc& arc) const { |
430 | 430 |
if (arc.forward) { |
431 | 431 |
Parent::nextFromRed(arc); |
432 | 432 |
} else { |
433 | 433 |
Parent::nextFromBlue(arc); |
434 | 434 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
435 | 435 |
} |
436 | 436 |
} |
437 | 437 |
|
438 | 438 |
void firstIn(Arc& arc, const Node& node) const { |
439 | 439 |
if (Parent::blue(node)) { |
440 | 440 |
Parent::firstFromBlue(arc, node); |
441 | 441 |
arc.forward = true; |
442 | 442 |
} else { |
443 | 443 |
Parent::firstFromRed(arc, node); |
444 | 444 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
445 | 445 |
} |
446 | 446 |
} |
447 | 447 |
void nextIn(Arc& arc) const { |
448 | 448 |
if (arc.forward) { |
449 | 449 |
Parent::nextFromBlue(arc); |
450 | 450 |
} else { |
451 | 451 |
Parent::nextFromRed(arc); |
452 | 452 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
453 | 453 |
} |
454 | 454 |
} |
455 | 455 |
|
456 | 456 |
Node source(const Arc& arc) const { |
457 | 457 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
458 | 458 |
} |
459 | 459 |
Node target(const Arc& arc) const { |
460 | 460 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
461 | 461 |
} |
462 | 462 |
|
463 | 463 |
int id(const Arc& arc) const { |
464 | 464 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
465 | 465 |
(arc.forward ? 0 : 1); |
466 | 466 |
} |
467 | 467 |
Arc arcFromId(int ix) const { |
468 | 468 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
469 | 469 |
} |
470 | 470 |
int maxArcId() const { |
471 | 471 |
return (Parent::maxEdgeId() << 1) + 1; |
472 | 472 |
} |
473 | 473 |
|
474 | 474 |
bool direction(const Arc& arc) const { |
475 | 475 |
return arc.forward; |
476 | 476 |
} |
477 | 477 |
|
478 | 478 |
Arc direct(const Edge& arc, bool dir) const { |
479 | 479 |
return Arc(arc, dir); |
480 | 480 |
} |
481 | 481 |
|
482 | 482 |
int arcNum() const { |
483 | 483 |
return 2 * Parent::edgeNum(); |
484 | 484 |
} |
485 | 485 |
|
486 | 486 |
int edgeNum() const { |
487 | 487 |
return Parent::edgeNum(); |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
|
491 | 491 |
}; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BEZIER_H |
20 | 20 |
#define LEMON_BEZIER_H |
21 | 21 |
|
22 |
///\ingroup misc |
|
23 |
///\file |
|
24 |
///\brief Classes to compute with Bezier curves. |
|
25 |
/// |
|
26 |
// |
|
22 |
//\ingroup misc |
|
23 |
//\file |
|
24 |
//\brief Classes to compute with Bezier curves. |
|
25 |
// |
|
26 |
//Up to now this file is used internally by \ref graph_to_eps.h |
|
27 | 27 |
|
28 | 28 |
#include<lemon/dim2.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace dim2 { |
32 | 32 |
|
33 | 33 |
class BezierBase { |
34 | 34 |
public: |
35 | 35 |
typedef lemon::dim2::Point<double> Point; |
36 | 36 |
protected: |
37 | 37 |
static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;} |
38 | 38 |
}; |
39 | 39 |
|
40 | 40 |
class Bezier1 : public BezierBase |
41 | 41 |
{ |
42 | 42 |
public: |
43 | 43 |
Point p1,p2; |
44 | 44 |
|
45 | 45 |
Bezier1() {} |
46 | 46 |
Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {} |
47 | 47 |
|
48 | 48 |
Point operator()(double t) const |
49 | 49 |
{ |
50 | 50 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
51 | 51 |
return conv(p1,p2,t); |
52 | 52 |
} |
53 | 53 |
Bezier1 before(double t) const |
54 | 54 |
{ |
55 | 55 |
return Bezier1(p1,conv(p1,p2,t)); |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
Bezier1 after(double t) const |
59 | 59 |
{ |
60 | 60 |
return Bezier1(conv(p1,p2,t),p2); |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
Bezier1 revert() const { return Bezier1(p2,p1);} |
64 | 64 |
Bezier1 operator()(double a,double b) const { return before(b).after(a/b); } |
65 | 65 |
Point grad() const { return p2-p1; } |
66 | 66 |
Point norm() const { return rot90(p2-p1); } |
67 | 67 |
Point grad(double) const { return grad(); } |
68 | 68 |
Point norm(double t) const { return rot90(grad(t)); } |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
class Bezier2 : public BezierBase |
72 | 72 |
{ |
73 | 73 |
public: |
74 | 74 |
Point p1,p2,p3; |
75 | 75 |
|
76 | 76 |
Bezier2() {} |
77 | 77 |
Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {} |
78 | 78 |
Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {} |
79 | 79 |
Point operator()(double t) const |
80 | 80 |
{ |
81 | 81 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
82 | 82 |
return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
83 | 83 |
} |
84 | 84 |
Bezier2 before(double t) const |
85 | 85 |
{ |
86 | 86 |
Point q(conv(p1,p2,t)); |
87 | 87 |
Point r(conv(p2,p3,t)); |
88 | 88 |
return Bezier2(p1,q,conv(q,r,t)); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
Bezier2 after(double t) const |
92 | 92 |
{ |
93 | 93 |
Point q(conv(p1,p2,t)); |
94 | 94 |
Point r(conv(p2,p3,t)); |
95 | 95 |
return Bezier2(conv(q,r,t),r,p3); |
96 | 96 |
} |
97 | 97 |
Bezier2 revert() const { return Bezier2(p3,p2,p1);} |
98 | 98 |
Bezier2 operator()(double a,double b) const { return before(b).after(a/b); } |
99 | 99 |
Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); } |
100 | 100 |
Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); } |
101 | 101 |
Point grad(double t) const { return grad()(t); } |
102 | 102 |
Point norm(double t) const { return rot90(grad(t)); } |
103 | 103 |
}; |
104 | 104 |
|
105 | 105 |
class Bezier3 : public BezierBase |
106 | 106 |
{ |
107 | 107 |
public: |
108 | 108 |
Point p1,p2,p3,p4; |
109 | 109 |
|
110 | 110 |
Bezier3() {} |
111 | 111 |
Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) |
112 | 112 |
: p1(_p1), p2(_p2), p3(_p3), p4(_p4) {} |
113 | 113 |
Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
114 | 114 |
p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {} |
115 | 115 |
Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
116 | 116 |
p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {} |
117 | 117 |
|
118 | 118 |
Point operator()(double t) const |
119 | 119 |
{ |
120 | 120 |
// return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
121 | 121 |
return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
122 | 122 |
(3*t*t*(1-t))*p3+(t*t*t)*p4; |
123 | 123 |
} |
124 | 124 |
Bezier3 before(double t) const |
125 | 125 |
{ |
126 | 126 |
Point p(conv(p1,p2,t)); |
127 | 127 |
Point q(conv(p2,p3,t)); |
128 | 128 |
Point r(conv(p3,p4,t)); |
129 | 129 |
Point a(conv(p,q,t)); |
130 | 130 |
Point b(conv(q,r,t)); |
131 | 131 |
Point c(conv(a,b,t)); |
132 | 132 |
return Bezier3(p1,p,a,c); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
Bezier3 after(double t) const |
136 | 136 |
{ |
137 | 137 |
Point p(conv(p1,p2,t)); |
138 | 138 |
Point q(conv(p2,p3,t)); |
139 | 139 |
Point r(conv(p3,p4,t)); |
140 | 140 |
Point a(conv(p,q,t)); |
141 | 141 |
Point b(conv(q,r,t)); |
142 | 142 |
Point c(conv(a,b,t)); |
143 | 143 |
return Bezier3(c,b,r,p4); |
144 | 144 |
} |
145 | 145 |
Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);} |
146 | 146 |
Bezier3 operator()(double a,double b) const { return before(b).after(a/b); } |
147 | 147 |
Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); } |
148 | 148 |
Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1), |
149 | 149 |
3.0*rot90(p3-p2), |
150 | 150 |
3.0*rot90(p4-p3)); } |
151 | 151 |
Point grad(double t) const { return grad()(t); } |
152 | 152 |
Point norm(double t) const { return rot90(grad(t)); } |
153 | 153 |
|
154 | 154 |
template<class R,class F,class S,class D> |
155 | 155 |
R recSplit(F &_f,const S &_s,D _d) const |
156 | 156 |
{ |
157 | 157 |
const Point a=(p1+p2)/2; |
158 | 158 |
const Point b=(p2+p3)/2; |
159 | 159 |
const Point c=(p3+p4)/2; |
160 | 160 |
const Point d=(a+b)/2; |
161 | 161 |
const Point e=(b+c)/2; |
162 | 162 |
const Point f=(d+e)/2; |
163 | 163 |
R f1=_f(Bezier3(p1,a,d,e),_d); |
164 | 164 |
R f2=_f(Bezier3(e,d,c,p4),_d); |
165 | 165 |
return _s(f1,f2); |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
}; |
169 | 169 |
|
170 | 170 |
|
171 | 171 |
} //END OF NAMESPACE dim2 |
172 | 172 |
} //END OF NAMESPACE lemon |
173 | 173 |
|
174 | 174 |
#endif // LEMON_BEZIER_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_DEFAULT_MAP_H |
20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
21 | 21 |
|
22 |
|
|
23 | 22 |
#include <lemon/bits/array_map.h> |
24 | 23 |
#include <lemon/bits/vector_map.h> |
25 | 24 |
//#include <lemon/bits/debug_map.h> |
26 | 25 |
|
27 |
///\ingroup graphbits |
|
28 |
///\file |
|
29 |
|
|
26 |
//\ingroup graphbits |
|
27 |
//\file |
|
28 |
//\brief Graph maps that construct and destruct their elements dynamically. |
|
30 | 29 |
|
31 | 30 |
namespace lemon { |
32 | 31 |
|
33 | 32 |
|
34 | 33 |
//#ifndef LEMON_USE_DEBUG_MAP |
35 | 34 |
|
36 | 35 |
template <typename _Graph, typename _Item, typename _Value> |
37 | 36 |
struct DefaultMapSelector { |
38 | 37 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
39 | 38 |
}; |
40 | 39 |
|
41 | 40 |
// bool |
42 | 41 |
template <typename _Graph, typename _Item> |
43 | 42 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
44 | 43 |
typedef VectorMap<_Graph, _Item, bool> Map; |
45 | 44 |
}; |
46 | 45 |
|
47 | 46 |
// char |
48 | 47 |
template <typename _Graph, typename _Item> |
49 | 48 |
struct DefaultMapSelector<_Graph, _Item, char> { |
50 | 49 |
typedef VectorMap<_Graph, _Item, char> Map; |
51 | 50 |
}; |
52 | 51 |
|
53 | 52 |
template <typename _Graph, typename _Item> |
54 | 53 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
55 | 54 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
56 | 55 |
}; |
57 | 56 |
|
58 | 57 |
template <typename _Graph, typename _Item> |
59 | 58 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
60 | 59 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
61 | 60 |
}; |
62 | 61 |
|
63 | 62 |
|
64 | 63 |
// int |
65 | 64 |
template <typename _Graph, typename _Item> |
66 | 65 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
67 | 66 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
68 | 67 |
}; |
69 | 68 |
|
70 | 69 |
template <typename _Graph, typename _Item> |
71 | 70 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
72 | 71 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
73 | 72 |
}; |
74 | 73 |
|
75 | 74 |
|
76 | 75 |
// short |
77 | 76 |
template <typename _Graph, typename _Item> |
78 | 77 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
79 | 78 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
80 | 79 |
}; |
81 | 80 |
|
82 | 81 |
template <typename _Graph, typename _Item> |
83 | 82 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
84 | 83 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
85 | 84 |
}; |
86 | 85 |
|
87 | 86 |
|
88 | 87 |
// long |
89 | 88 |
template <typename _Graph, typename _Item> |
90 | 89 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
91 | 90 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
92 | 91 |
}; |
93 | 92 |
|
94 | 93 |
template <typename _Graph, typename _Item> |
95 | 94 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
96 | 95 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 96 |
}; |
98 | 97 |
|
99 | 98 |
|
100 | 99 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
101 | 100 |
|
102 | 101 |
// long long |
103 | 102 |
template <typename _Graph, typename _Item> |
104 | 103 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
105 | 104 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
106 | 105 |
}; |
107 | 106 |
|
108 | 107 |
template <typename _Graph, typename _Item> |
109 | 108 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
110 | 109 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
111 | 110 |
}; |
112 | 111 |
|
113 | 112 |
#endif |
114 | 113 |
|
115 | 114 |
|
116 | 115 |
// float |
117 | 116 |
template <typename _Graph, typename _Item> |
118 | 117 |
struct DefaultMapSelector<_Graph, _Item, float> { |
119 | 118 |
typedef VectorMap<_Graph, _Item, float> Map; |
120 | 119 |
}; |
121 | 120 |
|
122 | 121 |
|
123 | 122 |
// double |
124 | 123 |
template <typename _Graph, typename _Item> |
125 | 124 |
struct DefaultMapSelector<_Graph, _Item, double> { |
126 | 125 |
typedef VectorMap<_Graph, _Item, double> Map; |
127 | 126 |
}; |
128 | 127 |
|
129 | 128 |
|
130 | 129 |
// long double |
131 | 130 |
template <typename _Graph, typename _Item> |
132 | 131 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
133 | 132 |
typedef VectorMap<_Graph, _Item, long double> Map; |
134 | 133 |
}; |
135 | 134 |
|
136 | 135 |
|
137 | 136 |
// pointer |
138 | 137 |
template <typename _Graph, typename _Item, typename _Ptr> |
139 | 138 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
140 | 139 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
141 | 140 |
}; |
142 | 141 |
|
143 | 142 |
// #else |
144 | 143 |
|
145 | 144 |
// template <typename _Graph, typename _Item, typename _Value> |
146 | 145 |
// struct DefaultMapSelector { |
147 | 146 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
148 | 147 |
// }; |
149 | 148 |
|
150 | 149 |
// #endif |
151 | 150 |
|
152 |
|
|
151 |
// DefaultMap class |
|
153 | 152 |
template <typename _Graph, typename _Item, typename _Value> |
154 | 153 |
class DefaultMap |
155 | 154 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
156 | 155 |
public: |
157 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
158 | 157 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
159 | 158 |
|
160 | 159 |
typedef typename Parent::Graph Graph; |
161 | 160 |
typedef typename Parent::Value Value; |
162 | 161 |
|
163 | 162 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
164 | 163 |
DefaultMap(const Graph& graph, const Value& value) |
165 | 164 |
: Parent(graph, value) {} |
166 | 165 |
|
167 | 166 |
DefaultMap& operator=(const DefaultMap& cmap) { |
168 | 167 |
return operator=<DefaultMap>(cmap); |
169 | 168 |
} |
170 | 169 |
|
171 | 170 |
template <typename CMap> |
172 | 171 |
DefaultMap& operator=(const CMap& cmap) { |
173 | 172 |
Parent::operator=(cmap); |
174 | 173 |
return *this; |
175 | 174 |
} |
176 | 175 |
|
177 | 176 |
}; |
178 | 177 |
|
179 | 178 |
} |
180 | 179 |
|
181 | 180 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
// This file contains a modified version of the enable_if library from BOOST. |
20 | 20 |
// See the appropriate copyright notice below. |
21 | 21 |
|
22 | 22 |
// Boost enable_if library |
23 | 23 |
|
24 | 24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
25 | 25 |
|
26 | 26 |
// Use, modification, and distribution is subject to the Boost Software |
27 | 27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
28 | 28 |
// http://www.boost.org/LICENSE_1_0.txt) |
29 | 29 |
|
30 | 30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
31 | 31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
32 | 32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
36 | 36 |
#define LEMON_BITS_ENABLE_IF_H |
37 | 37 |
|
38 |
///\file |
|
39 |
///\brief Miscellaneous basic utilities |
|
38 |
//\file |
|
39 |
//\brief Miscellaneous basic utilities |
|
40 | 40 |
|
41 | 41 |
namespace lemon |
42 | 42 |
{ |
43 | 43 |
|
44 |
|
|
44 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
45 | 45 |
|
46 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
47 |
/// |
|
48 |
// |
|
46 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
47 |
// |
|
48 |
//\sa False |
|
49 | 49 |
struct True { |
50 |
// |
|
50 |
//\e |
|
51 | 51 |
static const bool value = true; |
52 | 52 |
}; |
53 | 53 |
|
54 |
|
|
54 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
55 | 55 |
|
56 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
57 |
/// |
|
58 |
// |
|
56 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
57 |
// |
|
58 |
//\sa True |
|
59 | 59 |
struct False { |
60 |
// |
|
60 |
//\e |
|
61 | 61 |
static const bool value = false; |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
|
66 | 66 |
template <typename T> |
67 | 67 |
struct Wrap { |
68 | 68 |
const T &value; |
69 | 69 |
Wrap(const T &t) : value(t) {} |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
/**************** dummy class to avoid ambiguity ****************/ |
73 | 73 |
|
74 | 74 |
template<int T> struct dummy { dummy(int) {} }; |
75 | 75 |
|
76 | 76 |
/**************** enable_if from BOOST ****************/ |
77 | 77 |
|
78 | 78 |
template <typename Type, typename T = void> |
79 | 79 |
struct exists { |
80 | 80 |
typedef T type; |
81 | 81 |
}; |
82 | 82 |
|
83 | 83 |
|
84 | 84 |
template <bool B, class T = void> |
85 | 85 |
struct enable_if_c { |
86 | 86 |
typedef T type; |
87 | 87 |
}; |
88 | 88 |
|
89 | 89 |
template <class T> |
90 | 90 |
struct enable_if_c<false, T> {}; |
91 | 91 |
|
92 | 92 |
template <class Cond, class T = void> |
93 | 93 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
94 | 94 |
|
95 | 95 |
template <bool B, class T> |
96 | 96 |
struct lazy_enable_if_c { |
97 | 97 |
typedef typename T::type type; |
98 | 98 |
}; |
99 | 99 |
|
100 | 100 |
template <class T> |
101 | 101 |
struct lazy_enable_if_c<false, T> {}; |
102 | 102 |
|
103 | 103 |
template <class Cond, class T> |
104 | 104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
template <bool B, class T = void> |
108 | 108 |
struct disable_if_c { |
109 | 109 |
typedef T type; |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
template <class T> |
113 | 113 |
struct disable_if_c<true, T> {}; |
114 | 114 |
|
115 | 115 |
template <class Cond, class T = void> |
116 | 116 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
117 | 117 |
|
118 | 118 |
template <bool B, class T> |
119 | 119 |
struct lazy_disable_if_c { |
120 | 120 |
typedef typename T::type type; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <class T> |
124 | 124 |
struct lazy_disable_if_c<true, T> {}; |
125 | 125 |
|
126 | 126 |
template <class Cond, class T> |
127 | 127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
128 | 128 |
|
129 | 129 |
} // namespace lemon |
130 | 130 |
|
131 | 131 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/map_extender.h> |
25 | 25 |
#include <lemon/bits/default_map.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
|
30 |
///\ingroup graphbits |
|
31 |
///\file |
|
32 |
|
|
30 |
//\ingroup graphbits |
|
31 |
//\file |
|
32 |
//\brief Extenders for the digraph types |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 |
/// \ingroup graphbits |
|
36 |
/// |
|
37 |
// |
|
35 |
// \ingroup graphbits |
|
36 |
// |
|
37 |
// \brief Extender for the Digraphs |
|
38 | 38 |
template <typename Base> |
39 | 39 |
class DigraphExtender : public Base { |
40 | 40 |
public: |
41 | 41 |
|
42 | 42 |
typedef Base Parent; |
43 | 43 |
typedef DigraphExtender Digraph; |
44 | 44 |
|
45 | 45 |
// Base extensions |
46 | 46 |
|
47 | 47 |
typedef typename Parent::Node Node; |
48 | 48 |
typedef typename Parent::Arc Arc; |
49 | 49 |
|
50 | 50 |
int maxId(Node) const { |
51 | 51 |
return Parent::maxNodeId(); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
int maxId(Arc) const { |
55 | 55 |
return Parent::maxArcId(); |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
Node fromId(int id, Node) const { |
59 | 59 |
return Parent::nodeFromId(id); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
Arc fromId(int id, Arc) const { |
63 | 63 |
return Parent::arcFromId(id); |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
67 | 67 |
if (node == Parent::source(arc)) |
68 | 68 |
return Parent::target(arc); |
69 | 69 |
else if(node == Parent::target(arc)) |
70 | 70 |
return Parent::source(arc); |
71 | 71 |
else |
72 | 72 |
return INVALID; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
// Alterable extension |
76 | 76 |
|
77 | 77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
78 | 78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
protected: |
82 | 82 |
|
83 | 83 |
mutable NodeNotifier node_notifier; |
84 | 84 |
mutable ArcNotifier arc_notifier; |
85 | 85 |
|
86 | 86 |
public: |
87 | 87 |
|
88 | 88 |
NodeNotifier& notifier(Node) const { |
89 | 89 |
return node_notifier; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
ArcNotifier& notifier(Arc) const { |
93 | 93 |
return arc_notifier; |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
class NodeIt : public Node { |
97 | 97 |
const Digraph* _digraph; |
98 | 98 |
public: |
99 | 99 |
|
100 | 100 |
NodeIt() {} |
101 | 101 |
|
102 | 102 |
NodeIt(Invalid i) : Node(i) { } |
103 | 103 |
|
104 | 104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
105 | 105 |
_digraph->first(static_cast<Node&>(*this)); |
106 | 106 |
} |
107 | 107 |
|
108 | 108 |
NodeIt(const Digraph& digraph, const Node& node) |
109 | 109 |
: Node(node), _digraph(&digraph) {} |
110 | 110 |
|
111 | 111 |
NodeIt& operator++() { |
112 | 112 |
_digraph->next(*this); |
113 | 113 |
return *this; |
114 | 114 |
} |
115 | 115 |
|
116 | 116 |
}; |
117 | 117 |
|
118 | 118 |
|
119 | 119 |
class ArcIt : public Arc { |
120 | 120 |
const Digraph* _digraph; |
121 | 121 |
public: |
122 | 122 |
|
123 | 123 |
ArcIt() { } |
124 | 124 |
|
125 | 125 |
ArcIt(Invalid i) : Arc(i) { } |
126 | 126 |
|
127 | 127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) { |
128 | 128 |
_digraph->first(static_cast<Arc&>(*this)); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
132 | 132 |
Arc(arc), _digraph(&digraph) { } |
133 | 133 |
|
134 | 134 |
ArcIt& operator++() { |
135 | 135 |
_digraph->next(*this); |
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
}; |
140 | 140 |
|
141 | 141 |
|
142 | 142 |
class OutArcIt : public Arc { |
143 | 143 |
const Digraph* _digraph; |
144 | 144 |
public: |
145 | 145 |
|
146 | 146 |
OutArcIt() { } |
147 | 147 |
|
148 | 148 |
OutArcIt(Invalid i) : Arc(i) { } |
149 | 149 |
|
150 | 150 |
OutArcIt(const Digraph& digraph, const Node& node) |
151 | 151 |
: _digraph(&digraph) { |
152 | 152 |
_digraph->firstOut(*this, node); |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
156 | 156 |
: Arc(arc), _digraph(&digraph) {} |
157 | 157 |
|
158 | 158 |
OutArcIt& operator++() { |
159 | 159 |
_digraph->nextOut(*this); |
160 | 160 |
return *this; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
|
166 | 166 |
class InArcIt : public Arc { |
167 | 167 |
const Digraph* _digraph; |
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
InArcIt() { } |
171 | 171 |
|
172 | 172 |
InArcIt(Invalid i) : Arc(i) { } |
173 | 173 |
|
174 | 174 |
InArcIt(const Digraph& digraph, const Node& node) |
175 | 175 |
: _digraph(&digraph) { |
176 | 176 |
_digraph->firstIn(*this, node); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
180 | 180 |
Arc(arc), _digraph(&digraph) {} |
181 | 181 |
|
182 | 182 |
InArcIt& operator++() { |
183 | 183 |
_digraph->nextIn(*this); |
184 | 184 |
return *this; |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
}; |
188 | 188 |
|
189 |
/// \brief Base node of the iterator |
|
190 |
/// |
|
191 |
// |
|
189 |
// \brief Base node of the iterator |
|
190 |
// |
|
191 |
// Returns the base node (i.e. the source in this case) of the iterator |
|
192 | 192 |
Node baseNode(const OutArcIt &arc) const { |
193 | 193 |
return Parent::source(arc); |
194 | 194 |
} |
195 |
/// \brief Running node of the iterator |
|
196 |
/// |
|
197 |
/// Returns the running node (i.e. the target in this case) of the |
|
198 |
/// iterator |
|
195 |
// \brief Running node of the iterator |
|
196 |
// |
|
197 |
// Returns the running node (i.e. the target in this case) of the |
|
198 |
// iterator |
|
199 | 199 |
Node runningNode(const OutArcIt &arc) const { |
200 | 200 |
return Parent::target(arc); |
201 | 201 |
} |
202 | 202 |
|
203 |
/// \brief Base node of the iterator |
|
204 |
/// |
|
205 |
// |
|
203 |
// \brief Base node of the iterator |
|
204 |
// |
|
205 |
// Returns the base node (i.e. the target in this case) of the iterator |
|
206 | 206 |
Node baseNode(const InArcIt &arc) const { |
207 | 207 |
return Parent::target(arc); |
208 | 208 |
} |
209 |
/// \brief Running node of the iterator |
|
210 |
/// |
|
211 |
/// Returns the running node (i.e. the source in this case) of the |
|
212 |
/// iterator |
|
209 |
// \brief Running node of the iterator |
|
210 |
// |
|
211 |
// Returns the running node (i.e. the source in this case) of the |
|
212 |
// iterator |
|
213 | 213 |
Node runningNode(const InArcIt &arc) const { |
214 | 214 |
return Parent::source(arc); |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
|
218 | 218 |
template <typename _Value> |
219 | 219 |
class NodeMap |
220 | 220 |
: public MapExtender<DefaultMap<Digraph, Node, _Value> > { |
221 | 221 |
public: |
222 | 222 |
typedef DigraphExtender Digraph; |
223 | 223 |
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent; |
224 | 224 |
|
225 | 225 |
explicit NodeMap(const Digraph& digraph) |
226 | 226 |
: Parent(digraph) {} |
227 | 227 |
NodeMap(const Digraph& digraph, const _Value& value) |
228 | 228 |
: Parent(digraph, value) {} |
229 | 229 |
|
230 | 230 |
private: |
231 | 231 |
NodeMap& operator=(const NodeMap& cmap) { |
232 | 232 |
return operator=<NodeMap>(cmap); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
template <typename CMap> |
236 | 236 |
NodeMap& operator=(const CMap& cmap) { |
237 | 237 |
Parent::operator=(cmap); |
238 | 238 |
return *this; |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
}; |
242 | 242 |
|
243 | 243 |
template <typename _Value> |
244 | 244 |
class ArcMap |
245 | 245 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
246 | 246 |
public: |
247 | 247 |
typedef DigraphExtender Digraph; |
248 | 248 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
249 | 249 |
|
250 | 250 |
explicit ArcMap(const Digraph& digraph) |
251 | 251 |
: Parent(digraph) {} |
252 | 252 |
ArcMap(const Digraph& digraph, const _Value& value) |
253 | 253 |
: Parent(digraph, value) {} |
254 | 254 |
|
255 | 255 |
private: |
256 | 256 |
ArcMap& operator=(const ArcMap& cmap) { |
257 | 257 |
return operator=<ArcMap>(cmap); |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
template <typename CMap> |
261 | 261 |
ArcMap& operator=(const CMap& cmap) { |
262 | 262 |
Parent::operator=(cmap); |
263 | 263 |
return *this; |
264 | 264 |
} |
265 | 265 |
}; |
266 | 266 |
|
267 | 267 |
|
268 | 268 |
Node addNode() { |
269 | 269 |
Node node = Parent::addNode(); |
270 | 270 |
notifier(Node()).add(node); |
271 | 271 |
return node; |
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
Arc addArc(const Node& from, const Node& to) { |
275 | 275 |
Arc arc = Parent::addArc(from, to); |
276 | 276 |
notifier(Arc()).add(arc); |
277 | 277 |
return arc; |
278 | 278 |
} |
279 | 279 |
|
280 | 280 |
void clear() { |
281 | 281 |
notifier(Arc()).clear(); |
282 | 282 |
notifier(Node()).clear(); |
283 | 283 |
Parent::clear(); |
284 | 284 |
} |
285 | 285 |
|
286 | 286 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
287 | 287 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) { |
288 | 288 |
Parent::build(digraph, nodeRef, arcRef); |
289 | 289 |
notifier(Node()).build(); |
290 | 290 |
notifier(Arc()).build(); |
291 | 291 |
} |
292 | 292 |
|
293 | 293 |
void erase(const Node& node) { |
294 | 294 |
Arc arc; |
295 | 295 |
Parent::firstOut(arc, node); |
296 | 296 |
while (arc != INVALID ) { |
297 | 297 |
erase(arc); |
298 | 298 |
Parent::firstOut(arc, node); |
299 | 299 |
} |
300 | 300 |
|
301 | 301 |
Parent::firstIn(arc, node); |
302 | 302 |
while (arc != INVALID ) { |
303 | 303 |
erase(arc); |
304 | 304 |
Parent::firstIn(arc, node); |
305 | 305 |
} |
306 | 306 |
|
307 | 307 |
notifier(Node()).erase(node); |
308 | 308 |
Parent::erase(node); |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
void erase(const Arc& arc) { |
312 | 312 |
notifier(Arc()).erase(arc); |
313 | 313 |
Parent::erase(arc); |
314 | 314 |
} |
315 | 315 |
|
316 | 316 |
DigraphExtender() { |
317 | 317 |
node_notifier.setContainer(*this); |
318 | 318 |
arc_notifier.setContainer(*this); |
319 | 319 |
} |
320 | 320 |
|
321 | 321 |
|
322 | 322 |
~DigraphExtender() { |
323 | 323 |
arc_notifier.clear(); |
324 | 324 |
node_notifier.clear(); |
325 | 325 |
} |
326 | 326 |
}; |
327 | 327 |
|
328 |
/// \ingroup _graphbits |
|
329 |
/// |
|
330 |
// |
|
328 |
// \ingroup _graphbits |
|
329 |
// |
|
330 |
// \brief Extender for the Graphs |
|
331 | 331 |
template <typename Base> |
332 | 332 |
class GraphExtender : public Base { |
333 | 333 |
public: |
334 | 334 |
|
335 | 335 |
typedef Base Parent; |
336 | 336 |
typedef GraphExtender Graph; |
337 | 337 |
|
338 | 338 |
typedef True UndirectedTag; |
339 | 339 |
|
340 | 340 |
typedef typename Parent::Node Node; |
341 | 341 |
typedef typename Parent::Arc Arc; |
342 | 342 |
typedef typename Parent::Edge Edge; |
343 | 343 |
|
344 | 344 |
// Graph extension |
345 | 345 |
|
346 | 346 |
int maxId(Node) const { |
347 | 347 |
return Parent::maxNodeId(); |
348 | 348 |
} |
349 | 349 |
|
350 | 350 |
int maxId(Arc) const { |
351 | 351 |
return Parent::maxArcId(); |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
int maxId(Edge) const { |
355 | 355 |
return Parent::maxEdgeId(); |
356 | 356 |
} |
357 | 357 |
|
358 | 358 |
Node fromId(int id, Node) const { |
359 | 359 |
return Parent::nodeFromId(id); |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
Arc fromId(int id, Arc) const { |
363 | 363 |
return Parent::arcFromId(id); |
364 | 364 |
} |
365 | 365 |
|
366 | 366 |
Edge fromId(int id, Edge) const { |
367 | 367 |
return Parent::edgeFromId(id); |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
Node oppositeNode(const Node &n, const Edge &e) const { |
371 | 371 |
if( n == Parent::u(e)) |
372 | 372 |
return Parent::v(e); |
373 | 373 |
else if( n == Parent::v(e)) |
374 | 374 |
return Parent::u(e); |
375 | 375 |
else |
376 | 376 |
return INVALID; |
377 | 377 |
} |
378 | 378 |
|
379 | 379 |
Arc oppositeArc(const Arc &arc) const { |
380 | 380 |
return Parent::direct(arc, !Parent::direction(arc)); |
381 | 381 |
} |
382 | 382 |
|
383 | 383 |
using Parent::direct; |
384 | 384 |
Arc direct(const Edge &edge, const Node &node) const { |
385 | 385 |
return Parent::direct(edge, Parent::u(edge) == node); |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
// Alterable extension |
389 | 389 |
|
390 | 390 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
391 | 391 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
392 | 392 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
393 | 393 |
|
394 | 394 |
|
395 | 395 |
protected: |
396 | 396 |
|
397 | 397 |
mutable NodeNotifier node_notifier; |
398 | 398 |
mutable ArcNotifier arc_notifier; |
399 | 399 |
mutable EdgeNotifier edge_notifier; |
400 | 400 |
|
401 | 401 |
public: |
402 | 402 |
|
403 | 403 |
NodeNotifier& notifier(Node) const { |
404 | 404 |
return node_notifier; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
ArcNotifier& notifier(Arc) const { |
408 | 408 |
return arc_notifier; |
409 | 409 |
} |
410 | 410 |
|
411 | 411 |
EdgeNotifier& notifier(Edge) const { |
412 | 412 |
return edge_notifier; |
413 | 413 |
} |
414 | 414 |
|
415 | 415 |
|
416 | 416 |
|
417 | 417 |
class NodeIt : public Node { |
418 | 418 |
const Graph* _graph; |
419 | 419 |
public: |
420 | 420 |
|
421 | 421 |
NodeIt() {} |
422 | 422 |
|
423 | 423 |
NodeIt(Invalid i) : Node(i) { } |
424 | 424 |
|
425 | 425 |
explicit NodeIt(const Graph& graph) : _graph(&graph) { |
426 | 426 |
_graph->first(static_cast<Node&>(*this)); |
427 | 427 |
} |
428 | 428 |
|
429 | 429 |
NodeIt(const Graph& graph, const Node& node) |
430 | 430 |
: Node(node), _graph(&graph) {} |
431 | 431 |
|
432 | 432 |
NodeIt& operator++() { |
433 | 433 |
_graph->next(*this); |
434 | 434 |
return *this; |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
}; |
438 | 438 |
|
439 | 439 |
|
440 | 440 |
class ArcIt : public Arc { |
441 | 441 |
const Graph* _graph; |
442 | 442 |
public: |
443 | 443 |
|
444 | 444 |
ArcIt() { } |
445 | 445 |
|
446 | 446 |
ArcIt(Invalid i) : Arc(i) { } |
447 | 447 |
|
448 | 448 |
explicit ArcIt(const Graph& graph) : _graph(&graph) { |
449 | 449 |
_graph->first(static_cast<Arc&>(*this)); |
450 | 450 |
} |
451 | 451 |
|
452 | 452 |
ArcIt(const Graph& graph, const Arc& arc) : |
453 | 453 |
Arc(arc), _graph(&graph) { } |
454 | 454 |
|
455 | 455 |
ArcIt& operator++() { |
456 | 456 |
_graph->next(*this); |
457 | 457 |
return *this; |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
}; |
461 | 461 |
|
462 | 462 |
|
463 | 463 |
class OutArcIt : public Arc { |
464 | 464 |
const Graph* _graph; |
465 | 465 |
public: |
466 | 466 |
|
467 | 467 |
OutArcIt() { } |
468 | 468 |
|
469 | 469 |
OutArcIt(Invalid i) : Arc(i) { } |
470 | 470 |
|
471 | 471 |
OutArcIt(const Graph& graph, const Node& node) |
472 | 472 |
: _graph(&graph) { |
473 | 473 |
_graph->firstOut(*this, node); |
474 | 474 |
} |
475 | 475 |
|
476 | 476 |
OutArcIt(const Graph& graph, const Arc& arc) |
477 | 477 |
: Arc(arc), _graph(&graph) {} |
478 | 478 |
|
479 | 479 |
OutArcIt& operator++() { |
480 | 480 |
_graph->nextOut(*this); |
481 | 481 |
return *this; |
482 | 482 |
} |
483 | 483 |
|
484 | 484 |
}; |
485 | 485 |
|
486 | 486 |
|
487 | 487 |
class InArcIt : public Arc { |
488 | 488 |
const Graph* _graph; |
489 | 489 |
public: |
490 | 490 |
|
491 | 491 |
InArcIt() { } |
492 | 492 |
|
493 | 493 |
InArcIt(Invalid i) : Arc(i) { } |
494 | 494 |
|
495 | 495 |
InArcIt(const Graph& graph, const Node& node) |
496 | 496 |
: _graph(&graph) { |
497 | 497 |
_graph->firstIn(*this, node); |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
InArcIt(const Graph& graph, const Arc& arc) : |
501 | 501 |
Arc(arc), _graph(&graph) {} |
502 | 502 |
|
503 | 503 |
InArcIt& operator++() { |
504 | 504 |
_graph->nextIn(*this); |
505 | 505 |
return *this; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
}; |
509 | 509 |
|
510 | 510 |
|
511 | 511 |
class EdgeIt : public Parent::Edge { |
512 | 512 |
const Graph* _graph; |
513 | 513 |
public: |
514 | 514 |
|
515 | 515 |
EdgeIt() { } |
516 | 516 |
|
517 | 517 |
EdgeIt(Invalid i) : Edge(i) { } |
518 | 518 |
|
519 | 519 |
explicit EdgeIt(const Graph& graph) : _graph(&graph) { |
520 | 520 |
_graph->first(static_cast<Edge&>(*this)); |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
EdgeIt(const Graph& graph, const Edge& edge) : |
524 | 524 |
Edge(edge), _graph(&graph) { } |
525 | 525 |
|
526 | 526 |
EdgeIt& operator++() { |
527 | 527 |
_graph->next(*this); |
528 | 528 |
return *this; |
529 | 529 |
} |
530 | 530 |
|
531 | 531 |
}; |
532 | 532 |
|
533 | 533 |
class IncEdgeIt : public Parent::Edge { |
534 | 534 |
friend class GraphExtender; |
535 | 535 |
const Graph* _graph; |
536 | 536 |
bool _direction; |
537 | 537 |
public: |
538 | 538 |
|
539 | 539 |
IncEdgeIt() { } |
540 | 540 |
|
541 | 541 |
IncEdgeIt(Invalid i) : Edge(i), _direction(false) { } |
542 | 542 |
|
543 | 543 |
IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) { |
544 | 544 |
_graph->firstInc(*this, _direction, node); |
545 | 545 |
} |
546 | 546 |
|
547 | 547 |
IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node) |
548 | 548 |
: _graph(&graph), Edge(edge) { |
549 | 549 |
_direction = (_graph->source(edge) == node); |
550 | 550 |
} |
551 | 551 |
|
552 | 552 |
IncEdgeIt& operator++() { |
553 | 553 |
_graph->nextInc(*this, _direction); |
554 | 554 |
return *this; |
555 | 555 |
} |
556 | 556 |
}; |
557 | 557 |
|
558 |
/// \brief Base node of the iterator |
|
559 |
/// |
|
560 |
// |
|
558 |
// \brief Base node of the iterator |
|
559 |
// |
|
560 |
// Returns the base node (ie. the source in this case) of the iterator |
|
561 | 561 |
Node baseNode(const OutArcIt &arc) const { |
562 | 562 |
return Parent::source(static_cast<const Arc&>(arc)); |
563 | 563 |
} |
564 |
/// \brief Running node of the iterator |
|
565 |
/// |
|
566 |
/// Returns the running node (ie. the target in this case) of the |
|
567 |
/// iterator |
|
564 |
// \brief Running node of the iterator |
|
565 |
// |
|
566 |
// Returns the running node (ie. the target in this case) of the |
|
567 |
// iterator |
|
568 | 568 |
Node runningNode(const OutArcIt &arc) const { |
569 | 569 |
return Parent::target(static_cast<const Arc&>(arc)); |
570 | 570 |
} |
571 | 571 |
|
572 |
/// \brief Base node of the iterator |
|
573 |
/// |
|
574 |
// |
|
572 |
// \brief Base node of the iterator |
|
573 |
// |
|
574 |
// Returns the base node (ie. the target in this case) of the iterator |
|
575 | 575 |
Node baseNode(const InArcIt &arc) const { |
576 | 576 |
return Parent::target(static_cast<const Arc&>(arc)); |
577 | 577 |
} |
578 |
/// \brief Running node of the iterator |
|
579 |
/// |
|
580 |
/// Returns the running node (ie. the source in this case) of the |
|
581 |
/// iterator |
|
578 |
// \brief Running node of the iterator |
|
579 |
// |
|
580 |
// Returns the running node (ie. the source in this case) of the |
|
581 |
// iterator |
|
582 | 582 |
Node runningNode(const InArcIt &arc) const { |
583 | 583 |
return Parent::source(static_cast<const Arc&>(arc)); |
584 | 584 |
} |
585 | 585 |
|
586 |
/// Base node of the iterator |
|
587 |
/// |
|
588 |
// |
|
586 |
// Base node of the iterator |
|
587 |
// |
|
588 |
// Returns the base node of the iterator |
|
589 | 589 |
Node baseNode(const IncEdgeIt &edge) const { |
590 | 590 |
return edge._direction ? u(edge) : v(edge); |
591 | 591 |
} |
592 |
/// Running node of the iterator |
|
593 |
/// |
|
594 |
// |
|
592 |
// Running node of the iterator |
|
593 |
// |
|
594 |
// Returns the running node of the iterator |
|
595 | 595 |
Node runningNode(const IncEdgeIt &edge) const { |
596 | 596 |
return edge._direction ? v(edge) : u(edge); |
597 | 597 |
} |
598 | 598 |
|
599 | 599 |
// Mappable extension |
600 | 600 |
|
601 | 601 |
template <typename _Value> |
602 | 602 |
class NodeMap |
603 | 603 |
: public MapExtender<DefaultMap<Graph, Node, _Value> > { |
604 | 604 |
public: |
605 | 605 |
typedef GraphExtender Graph; |
606 | 606 |
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
607 | 607 |
|
608 | 608 |
NodeMap(const Graph& graph) |
609 | 609 |
: Parent(graph) {} |
610 | 610 |
NodeMap(const Graph& graph, const _Value& value) |
611 | 611 |
: Parent(graph, value) {} |
612 | 612 |
|
613 | 613 |
private: |
614 | 614 |
NodeMap& operator=(const NodeMap& cmap) { |
615 | 615 |
return operator=<NodeMap>(cmap); |
616 | 616 |
} |
617 | 617 |
|
618 | 618 |
template <typename CMap> |
619 | 619 |
NodeMap& operator=(const CMap& cmap) { |
620 | 620 |
Parent::operator=(cmap); |
621 | 621 |
return *this; |
622 | 622 |
} |
623 | 623 |
|
624 | 624 |
}; |
625 | 625 |
|
626 | 626 |
template <typename _Value> |
627 | 627 |
class ArcMap |
628 | 628 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
629 | 629 |
public: |
630 | 630 |
typedef GraphExtender Graph; |
631 | 631 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
632 | 632 |
|
633 | 633 |
ArcMap(const Graph& graph) |
634 | 634 |
: Parent(graph) {} |
635 | 635 |
ArcMap(const Graph& graph, const _Value& value) |
636 | 636 |
: Parent(graph, value) {} |
637 | 637 |
|
638 | 638 |
private: |
639 | 639 |
ArcMap& operator=(const ArcMap& cmap) { |
640 | 640 |
return operator=<ArcMap>(cmap); |
641 | 641 |
} |
642 | 642 |
|
643 | 643 |
template <typename CMap> |
644 | 644 |
ArcMap& operator=(const CMap& cmap) { |
645 | 645 |
Parent::operator=(cmap); |
646 | 646 |
return *this; |
647 | 647 |
} |
648 | 648 |
}; |
649 | 649 |
|
650 | 650 |
|
651 | 651 |
template <typename _Value> |
652 | 652 |
class EdgeMap |
653 | 653 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
654 | 654 |
public: |
655 | 655 |
typedef GraphExtender Graph; |
656 | 656 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
657 | 657 |
|
658 | 658 |
EdgeMap(const Graph& graph) |
659 | 659 |
: Parent(graph) {} |
660 | 660 |
|
661 | 661 |
EdgeMap(const Graph& graph, const _Value& value) |
662 | 662 |
: Parent(graph, value) {} |
663 | 663 |
|
664 | 664 |
private: |
665 | 665 |
EdgeMap& operator=(const EdgeMap& cmap) { |
666 | 666 |
return operator=<EdgeMap>(cmap); |
667 | 667 |
} |
668 | 668 |
|
669 | 669 |
template <typename CMap> |
670 | 670 |
EdgeMap& operator=(const CMap& cmap) { |
671 | 671 |
Parent::operator=(cmap); |
672 | 672 |
return *this; |
673 | 673 |
} |
674 | 674 |
|
675 | 675 |
}; |
676 | 676 |
|
677 | 677 |
// Alteration extension |
678 | 678 |
|
679 | 679 |
Node addNode() { |
680 | 680 |
Node node = Parent::addNode(); |
681 | 681 |
notifier(Node()).add(node); |
682 | 682 |
return node; |
683 | 683 |
} |
684 | 684 |
|
685 | 685 |
Edge addEdge(const Node& from, const Node& to) { |
686 | 686 |
Edge edge = Parent::addEdge(from, to); |
687 | 687 |
notifier(Edge()).add(edge); |
688 | 688 |
std::vector<Arc> ev; |
689 | 689 |
ev.push_back(Parent::direct(edge, true)); |
690 | 690 |
ev.push_back(Parent::direct(edge, false)); |
691 | 691 |
notifier(Arc()).add(ev); |
692 | 692 |
return edge; |
693 | 693 |
} |
694 | 694 |
|
695 | 695 |
void clear() { |
696 | 696 |
notifier(Arc()).clear(); |
697 | 697 |
notifier(Edge()).clear(); |
698 | 698 |
notifier(Node()).clear(); |
699 | 699 |
Parent::clear(); |
700 | 700 |
} |
701 | 701 |
|
702 | 702 |
template <typename Graph, typename NodeRefMap, typename EdgeRefMap> |
703 | 703 |
void build(const Graph& graph, NodeRefMap& nodeRef, |
704 | 704 |
EdgeRefMap& edgeRef) { |
705 | 705 |
Parent::build(graph, nodeRef, edgeRef); |
706 | 706 |
notifier(Node()).build(); |
707 | 707 |
notifier(Edge()).build(); |
708 | 708 |
notifier(Arc()).build(); |
709 | 709 |
} |
710 | 710 |
|
711 | 711 |
void erase(const Node& node) { |
712 | 712 |
Arc arc; |
713 | 713 |
Parent::firstOut(arc, node); |
714 | 714 |
while (arc != INVALID ) { |
715 | 715 |
erase(arc); |
716 | 716 |
Parent::firstOut(arc, node); |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
Parent::firstIn(arc, node); |
720 | 720 |
while (arc != INVALID ) { |
721 | 721 |
erase(arc); |
722 | 722 |
Parent::firstIn(arc, node); |
723 | 723 |
} |
724 | 724 |
|
725 | 725 |
notifier(Node()).erase(node); |
726 | 726 |
Parent::erase(node); |
727 | 727 |
} |
728 | 728 |
|
729 | 729 |
void erase(const Edge& edge) { |
730 | 730 |
std::vector<Arc> av; |
731 | 731 |
av.push_back(Parent::direct(edge, true)); |
732 | 732 |
av.push_back(Parent::direct(edge, false)); |
733 | 733 |
notifier(Arc()).erase(av); |
734 | 734 |
notifier(Edge()).erase(edge); |
735 | 735 |
Parent::erase(edge); |
736 | 736 |
} |
737 | 737 |
|
738 | 738 |
GraphExtender() { |
739 | 739 |
node_notifier.setContainer(*this); |
740 | 740 |
arc_notifier.setContainer(*this); |
741 | 741 |
edge_notifier.setContainer(*this); |
742 | 742 |
} |
743 | 743 |
|
744 | 744 |
~GraphExtender() { |
745 | 745 |
edge_notifier.clear(); |
746 | 746 |
arc_notifier.clear(); |
747 | 747 |
node_notifier.clear(); |
748 | 748 |
} |
749 | 749 |
|
750 | 750 |
}; |
751 | 751 |
|
752 | 752 |
} |
753 | 753 |
|
754 | 754 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_MAP_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
|
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 |
///\file |
|
30 |
///\brief Extenders for iterable maps. |
|
29 |
//\file |
|
30 |
//\brief Extenders for iterable maps. |
|
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 |
/// \ingroup graphbits |
|
35 |
/// |
|
36 |
// |
|
34 |
// \ingroup graphbits |
|
35 |
// |
|
36 |
// \brief Extender for maps |
|
37 | 37 |
template <typename _Map> |
38 | 38 |
class MapExtender : public _Map { |
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
typedef _Map Parent; |
42 | 42 |
typedef MapExtender Map; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
typedef typename Parent::Graph Graph; |
46 | 46 |
typedef typename Parent::Key Item; |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Key Key; |
49 | 49 |
typedef typename Parent::Value Value; |
50 | 50 |
|
51 | 51 |
class MapIt; |
52 | 52 |
class ConstMapIt; |
53 | 53 |
|
54 | 54 |
friend class MapIt; |
55 | 55 |
friend class ConstMapIt; |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
MapExtender(const Graph& graph) |
60 | 60 |
: Parent(graph) {} |
61 | 61 |
|
62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
63 | 63 |
: Parent(graph, value) {} |
64 | 64 |
|
65 | 65 |
private: |
66 | 66 |
MapExtender& operator=(const MapExtender& cmap) { |
67 | 67 |
return operator=<MapExtender>(cmap); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
template <typename CMap> |
71 | 71 |
MapExtender& operator=(const CMap& cmap) { |
72 | 72 |
Parent::operator=(cmap); |
73 | 73 |
return *this; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
class MapIt : public Item { |
78 | 78 |
public: |
79 | 79 |
|
80 | 80 |
typedef Item Parent; |
81 | 81 |
typedef typename Map::Value Value; |
82 | 82 |
|
83 | 83 |
MapIt() {} |
84 | 84 |
|
85 | 85 |
MapIt(Invalid i) : Parent(i) { } |
86 | 86 |
|
87 | 87 |
explicit MapIt(Map& _map) : map(_map) { |
88 | 88 |
map.notifier()->first(*this); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
MapIt(const Map& _map, const Item& item) |
92 | 92 |
: Parent(item), map(_map) {} |
93 | 93 |
|
94 | 94 |
MapIt& operator++() { |
95 | 95 |
map.notifier()->next(*this); |
96 | 96 |
return *this; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
100 | 100 |
return map[*this]; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() { |
104 | 104 |
return map[*this]; |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
void set(const Value& value) { |
108 | 108 |
map.set(*this, value); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
protected: |
112 | 112 |
Map& map; |
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
class ConstMapIt : public Item { |
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
typedef Item Parent; |
120 | 120 |
|
121 | 121 |
typedef typename Map::Value Value; |
122 | 122 |
|
123 | 123 |
ConstMapIt() {} |
124 | 124 |
|
125 | 125 |
ConstMapIt(Invalid i) : Parent(i) { } |
126 | 126 |
|
127 | 127 |
explicit ConstMapIt(Map& _map) : map(_map) { |
128 | 128 |
map.notifier()->first(*this); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
132 | 132 |
: Parent(item), map(_map) {} |
133 | 133 |
|
134 | 134 |
ConstMapIt& operator++() { |
135 | 135 |
map.notifier()->next(*this); |
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
140 | 140 |
return map[*this]; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
protected: |
144 | 144 |
const Map& map; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
class ItemIt : public Item { |
148 | 148 |
public: |
149 | 149 |
|
150 | 150 |
typedef Item Parent; |
151 | 151 |
|
152 | 152 |
ItemIt() {} |
153 | 153 |
|
154 | 154 |
ItemIt(Invalid i) : Parent(i) { } |
155 | 155 |
|
156 | 156 |
explicit ItemIt(Map& _map) : map(_map) { |
157 | 157 |
map.notifier()->first(*this); |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
ItemIt(const Map& _map, const Item& item) |
161 | 161 |
: Parent(item), map(_map) {} |
162 | 162 |
|
163 | 163 |
ItemIt& operator++() { |
164 | 164 |
map.notifier()->next(*this); |
165 | 165 |
return *this; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
const Map& map; |
170 | 170 |
|
171 | 171 |
}; |
172 | 172 |
}; |
173 | 173 |
|
174 |
/// \ingroup graphbits |
|
175 |
/// |
|
176 |
// |
|
174 |
// \ingroup graphbits |
|
175 |
// |
|
176 |
// \brief Extender for maps which use a subset of the items. |
|
177 | 177 |
template <typename _Graph, typename _Map> |
178 | 178 |
class SubMapExtender : public _Map { |
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Map Parent; |
182 | 182 |
typedef SubMapExtender Map; |
183 | 183 |
|
184 | 184 |
typedef _Graph Graph; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Key Item; |
187 | 187 |
|
188 | 188 |
typedef typename Parent::Key Key; |
189 | 189 |
typedef typename Parent::Value Value; |
190 | 190 |
|
191 | 191 |
class MapIt; |
192 | 192 |
class ConstMapIt; |
193 | 193 |
|
194 | 194 |
friend class MapIt; |
195 | 195 |
friend class ConstMapIt; |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
SubMapExtender(const Graph& _graph) |
200 | 200 |
: Parent(_graph), graph(_graph) {} |
201 | 201 |
|
202 | 202 |
SubMapExtender(const Graph& _graph, const Value& _value) |
203 | 203 |
: Parent(_graph, _value), graph(_graph) {} |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
207 | 207 |
return operator=<MapExtender>(cmap); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
template <typename CMap> |
211 | 211 |
SubMapExtender& operator=(const CMap& cmap) { |
212 | 212 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
213 | 213 |
Item it; |
214 | 214 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
215 | 215 |
Parent::set(it, cmap[it]); |
216 | 216 |
} |
217 | 217 |
return *this; |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
public: |
221 | 221 |
class MapIt : public Item { |
222 | 222 |
public: |
223 | 223 |
|
224 | 224 |
typedef Item Parent; |
225 | 225 |
typedef typename Map::Value Value; |
226 | 226 |
|
227 | 227 |
MapIt() {} |
228 | 228 |
|
229 | 229 |
MapIt(Invalid i) : Parent(i) { } |
230 | 230 |
|
231 | 231 |
explicit MapIt(Map& _map) : map(_map) { |
232 | 232 |
map.graph.first(*this); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
MapIt(const Map& _map, const Item& item) |
236 | 236 |
: Parent(item), map(_map) {} |
237 | 237 |
|
238 | 238 |
MapIt& operator++() { |
239 | 239 |
map.graph.next(*this); |
240 | 240 |
return *this; |
241 | 241 |
} |
242 | 242 |
|
243 | 243 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
244 | 244 |
return map[*this]; |
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
typename MapTraits<Map>::ReturnValue operator*() { |
248 | 248 |
return map[*this]; |
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
void set(const Value& value) { |
252 | 252 |
map.set(*this, value); |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
protected: |
256 | 256 |
Map& map; |
257 | 257 |
|
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
class ConstMapIt : public Item { |
261 | 261 |
public: |
262 | 262 |
|
263 | 263 |
typedef Item Parent; |
264 | 264 |
|
265 | 265 |
typedef typename Map::Value Value; |
266 | 266 |
|
267 | 267 |
ConstMapIt() {} |
268 | 268 |
|
269 | 269 |
ConstMapIt(Invalid i) : Parent(i) { } |
270 | 270 |
|
271 | 271 |
explicit ConstMapIt(Map& _map) : map(_map) { |
272 | 272 |
map.graph.first(*this); |
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
ConstMapIt(const Map& _map, const Item& item) |
276 | 276 |
: Parent(item), map(_map) {} |
277 | 277 |
|
278 | 278 |
ConstMapIt& operator++() { |
279 | 279 |
map.graph.next(*this); |
280 | 280 |
return *this; |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
284 | 284 |
return map[*this]; |
285 | 285 |
} |
286 | 286 |
|
287 | 287 |
protected: |
288 | 288 |
const Map& map; |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
class ItemIt : public Item { |
292 | 292 |
public: |
293 | 293 |
|
294 | 294 |
typedef Item Parent; |
295 | 295 |
|
296 | 296 |
ItemIt() {} |
297 | 297 |
|
298 | 298 |
ItemIt(Invalid i) : Parent(i) { } |
299 | 299 |
|
300 | 300 |
explicit ItemIt(Map& _map) : map(_map) { |
301 | 301 |
map.graph.first(*this); |
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
ItemIt(const Map& _map, const Item& item) |
305 | 305 |
: Parent(item), map(_map) {} |
306 | 306 |
|
307 | 307 |
ItemIt& operator++() { |
308 | 308 |
map.graph.next(*this); |
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
protected: |
313 | 313 |
const Map& map; |
314 | 314 |
|
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
|
319 | 319 |
const Graph& graph; |
320 | 320 |
|
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_TRAITS_H |
20 | 20 |
#define LEMON_BITS_TRAITS_H |
21 | 21 |
|
22 |
///\file |
|
23 |
///\brief Traits for graphs and maps |
|
24 |
// |
|
22 |
//\file |
|
23 |
//\brief Traits for graphs and maps |
|
24 |
// |
|
25 | 25 |
|
26 | 26 |
#include <lemon/bits/enable_if.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
struct InvalidType {}; |
31 | 31 |
|
32 | 32 |
template <typename _Graph, typename _Item> |
33 | 33 |
class ItemSetTraits {}; |
34 | 34 |
|
35 | 35 |
|
36 | 36 |
template <typename Graph, typename Enable = void> |
37 | 37 |
struct NodeNotifierIndicator { |
38 | 38 |
typedef InvalidType Type; |
39 | 39 |
}; |
40 | 40 |
template <typename Graph> |
41 | 41 |
struct NodeNotifierIndicator< |
42 | 42 |
Graph, |
43 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
44 | 44 |
> { |
45 | 45 |
typedef typename Graph::NodeNotifier Type; |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
template <typename _Graph> |
49 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
typedef _Graph Graph; |
53 | 53 |
|
54 | 54 |
typedef typename Graph::Node Item; |
55 | 55 |
typedef typename Graph::NodeIt ItemIt; |
56 | 56 |
|
57 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
58 | 58 |
|
59 | 59 |
template <typename _Value> |
60 | 60 |
class Map : public Graph::template NodeMap<_Value> { |
61 | 61 |
public: |
62 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
63 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
64 | 64 |
typedef typename Parent::Value Value; |
65 | 65 |
|
66 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
67 | 67 |
Map(const Graph& _digraph, const Value& _value) |
68 | 68 |
: Parent(_digraph, _value) {} |
69 | 69 |
|
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
template <typename Graph, typename Enable = void> |
75 | 75 |
struct ArcNotifierIndicator { |
76 | 76 |
typedef InvalidType Type; |
77 | 77 |
}; |
78 | 78 |
template <typename Graph> |
79 | 79 |
struct ArcNotifierIndicator< |
80 | 80 |
Graph, |
81 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
82 | 82 |
> { |
83 | 83 |
typedef typename Graph::ArcNotifier Type; |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
template <typename _Graph> |
87 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
88 | 88 |
public: |
89 | 89 |
|
90 | 90 |
typedef _Graph Graph; |
91 | 91 |
|
92 | 92 |
typedef typename Graph::Arc Item; |
93 | 93 |
typedef typename Graph::ArcIt ItemIt; |
94 | 94 |
|
95 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
96 | 96 |
|
97 | 97 |
template <typename _Value> |
98 | 98 |
class Map : public Graph::template ArcMap<_Value> { |
99 | 99 |
public: |
100 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
101 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
102 | 102 |
typedef typename Parent::Value Value; |
103 | 103 |
|
104 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
105 | 105 |
Map(const Graph& _digraph, const Value& _value) |
106 | 106 |
: Parent(_digraph, _value) {} |
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename Graph, typename Enable = void> |
112 | 112 |
struct EdgeNotifierIndicator { |
113 | 113 |
typedef InvalidType Type; |
114 | 114 |
}; |
115 | 115 |
template <typename Graph> |
116 | 116 |
struct EdgeNotifierIndicator< |
117 | 117 |
Graph, |
118 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
119 | 119 |
> { |
120 | 120 |
typedef typename Graph::EdgeNotifier Type; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <typename _Graph> |
124 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
125 | 125 |
public: |
126 | 126 |
|
127 | 127 |
typedef _Graph Graph; |
128 | 128 |
|
129 | 129 |
typedef typename Graph::Edge Item; |
130 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
131 | 131 |
|
132 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
133 | 133 |
|
134 | 134 |
template <typename _Value> |
135 | 135 |
class Map : public Graph::template EdgeMap<_Value> { |
136 | 136 |
public: |
137 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
138 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
139 | 139 |
typedef typename Parent::Value Value; |
140 | 140 |
|
141 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
142 | 142 |
Map(const Graph& _digraph, const Value& _value) |
143 | 143 |
: Parent(_digraph, _value) {} |
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
}; |
147 | 147 |
|
148 | 148 |
template <typename Map, typename Enable = void> |
149 | 149 |
struct MapTraits { |
150 | 150 |
typedef False ReferenceMapTag; |
151 | 151 |
|
152 | 152 |
typedef typename Map::Key Key; |
153 | 153 |
typedef typename Map::Value Value; |
154 | 154 |
|
155 | 155 |
typedef Value ConstReturnValue; |
156 | 156 |
typedef Value ReturnValue; |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
template <typename Map> |
160 | 160 |
struct MapTraits< |
161 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
162 | 162 |
{ |
163 | 163 |
typedef True ReferenceMapTag; |
164 | 164 |
|
165 | 165 |
typedef typename Map::Key Key; |
166 | 166 |
typedef typename Map::Value Value; |
167 | 167 |
|
168 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
169 | 169 |
typedef typename Map::Reference ReturnValue; |
170 | 170 |
|
171 | 171 |
typedef typename Map::ConstReference ConstReference; |
172 | 172 |
typedef typename Map::Reference Reference; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename MatrixMap, typename Enable = void> |
176 | 176 |
struct MatrixMapTraits { |
177 | 177 |
typedef False ReferenceMapTag; |
178 | 178 |
|
179 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
180 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
181 | 181 |
typedef typename MatrixMap::Value Value; |
182 | 182 |
|
183 | 183 |
typedef Value ConstReturnValue; |
184 | 184 |
typedef Value ReturnValue; |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
template <typename MatrixMap> |
188 | 188 |
struct MatrixMapTraits< |
189 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
190 | 190 |
void>::type > |
191 | 191 |
{ |
192 | 192 |
typedef True ReferenceMapTag; |
193 | 193 |
|
194 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
195 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
196 | 196 |
typedef typename MatrixMap::Value Value; |
197 | 197 |
|
198 | 198 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
199 | 199 |
typedef typename MatrixMap::Reference ReturnValue; |
200 | 200 |
|
201 | 201 |
typedef typename MatrixMap::ConstReference ConstReference; |
202 | 202 |
typedef typename MatrixMap::Reference Reference; |
203 | 203 |
}; |
204 | 204 |
|
205 | 205 |
// Indicators for the tags |
206 | 206 |
|
207 | 207 |
template <typename Graph, typename Enable = void> |
208 | 208 |
struct NodeNumTagIndicator { |
209 | 209 |
static const bool value = false; |
210 | 210 |
}; |
211 | 211 |
|
212 | 212 |
template <typename Graph> |
213 | 213 |
struct NodeNumTagIndicator< |
214 | 214 |
Graph, |
215 | 215 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
216 | 216 |
> { |
217 | 217 |
static const bool value = true; |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename Graph, typename Enable = void> |
221 | 221 |
struct EdgeNumTagIndicator { |
222 | 222 |
static const bool value = false; |
223 | 223 |
}; |
224 | 224 |
|
225 | 225 |
template <typename Graph> |
226 | 226 |
struct EdgeNumTagIndicator< |
227 | 227 |
Graph, |
228 | 228 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
229 | 229 |
> { |
230 | 230 |
static const bool value = true; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <typename Graph, typename Enable = void> |
234 | 234 |
struct FindEdgeTagIndicator { |
235 | 235 |
static const bool value = false; |
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
template <typename Graph> |
239 | 239 |
struct FindEdgeTagIndicator< |
240 | 240 |
Graph, |
241 | 241 |
typename enable_if<typename Graph::FindEdgeTag, void>::type |
242 | 242 |
> { |
243 | 243 |
static const bool value = true; |
244 | 244 |
}; |
245 | 245 |
|
246 | 246 |
template <typename Graph, typename Enable = void> |
247 | 247 |
struct UndirectedTagIndicator { |
248 | 248 |
static const bool value = false; |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
template <typename Graph> |
252 | 252 |
struct UndirectedTagIndicator< |
253 | 253 |
Graph, |
254 | 254 |
typename enable_if<typename Graph::UndirectedTag, void>::type |
255 | 255 |
> { |
256 | 256 |
static const bool value = true; |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
template <typename Graph, typename Enable = void> |
260 | 260 |
struct BuildTagIndicator { |
261 | 261 |
static const bool value = false; |
262 | 262 |
}; |
263 | 263 |
|
264 | 264 |
template <typename Graph> |
265 | 265 |
struct BuildTagIndicator< |
266 | 266 |
Graph, |
267 | 267 |
typename enable_if<typename Graph::BuildTag, void>::type |
268 | 268 |
> { |
269 | 269 |
static const bool value = true; |
270 | 270 |
}; |
271 | 271 |
|
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 |
///\ingroup graphbits |
|
32 |
/// |
|
33 |
///\file |
|
34 |
///\brief Vector based graph maps. |
|
31 |
//\ingroup graphbits |
|
32 |
// |
|
33 |
//\file |
|
34 |
//\brief Vector based graph maps. |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 |
/// \ingroup graphbits |
|
38 |
/// |
|
39 |
/// \brief Graph map based on the std::vector storage. |
|
40 |
/// |
|
41 |
/// The VectorMap template class is graph map structure what |
|
42 |
/// automatically updates the map when a key is added to or erased from |
|
43 |
/// the map. This map type uses the std::vector to store the values. |
|
44 |
/// |
|
45 |
/// \tparam _Graph The graph this map is attached to. |
|
46 |
/// \tparam _Item The item type of the graph items. |
|
47 |
// |
|
37 |
// \ingroup graphbits |
|
38 |
// |
|
39 |
// \brief Graph map based on the std::vector storage. |
|
40 |
// |
|
41 |
// The VectorMap template class is graph map structure what |
|
42 |
// automatically updates the map when a key is added to or erased from |
|
43 |
// the map. This map type uses the std::vector to store the values. |
|
44 |
// |
|
45 |
// \tparam _Graph The graph this map is attached to. |
|
46 |
// \tparam _Item The item type of the graph items. |
|
47 |
// \tparam _Value The value type of the map. |
|
48 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
49 | 49 |
class VectorMap |
50 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
51 | 51 |
private: |
52 | 52 |
|
53 |
|
|
53 |
// The container type of the map. |
|
54 | 54 |
typedef std::vector<_Value> Container; |
55 | 55 |
|
56 | 56 |
public: |
57 | 57 |
|
58 |
|
|
58 |
// The graph type of the map. |
|
59 | 59 |
typedef _Graph Graph; |
60 |
|
|
60 |
// The item type of the map. |
|
61 | 61 |
typedef _Item Item; |
62 |
|
|
62 |
// The reference map tag. |
|
63 | 63 |
typedef True ReferenceMapTag; |
64 | 64 |
|
65 |
|
|
65 |
// The key type of the map. |
|
66 | 66 |
typedef _Item Key; |
67 |
|
|
67 |
// The value type of the map. |
|
68 | 68 |
typedef _Value Value; |
69 | 69 |
|
70 |
|
|
70 |
// The notifier type. |
|
71 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
72 | 72 |
|
73 |
|
|
73 |
// The map type. |
|
74 | 74 |
typedef VectorMap Map; |
75 |
|
|
75 |
// The base class of the map. |
|
76 | 76 |
typedef typename Notifier::ObserverBase Parent; |
77 | 77 |
|
78 |
|
|
78 |
// The reference type of the map; |
|
79 | 79 |
typedef typename Container::reference Reference; |
80 |
|
|
80 |
// The const reference type of the map; |
|
81 | 81 |
typedef typename Container::const_reference ConstReference; |
82 | 82 |
|
83 | 83 |
|
84 |
/// \brief Constructor to attach the new map into the notifier. |
|
85 |
/// |
|
86 |
/// It constructs a map and attachs it into the notifier. |
|
87 |
/// It adds all the items of the graph to the map. |
|
84 |
// \brief Constructor to attach the new map into the notifier. |
|
85 |
// |
|
86 |
// It constructs a map and attachs it into the notifier. |
|
87 |
// It adds all the items of the graph to the map. |
|
88 | 88 |
VectorMap(const Graph& graph) { |
89 | 89 |
Parent::attach(graph.notifier(Item())); |
90 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
91 | 91 |
} |
92 | 92 |
|
93 |
/// \brief Constructor uses given value to initialize the map. |
|
94 |
/// |
|
95 |
/// It constructs a map uses a given value to initialize the map. |
|
96 |
/// It adds all the items of the graph to the map. |
|
93 |
// \brief Constructor uses given value to initialize the map. |
|
94 |
// |
|
95 |
// It constructs a map uses a given value to initialize the map. |
|
96 |
// It adds all the items of the graph to the map. |
|
97 | 97 |
VectorMap(const Graph& graph, const Value& value) { |
98 | 98 |
Parent::attach(graph.notifier(Item())); |
99 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
private: |
103 |
/// \brief Copy constructor |
|
104 |
/// |
|
105 |
// |
|
103 |
// \brief Copy constructor |
|
104 |
// |
|
105 |
// Copy constructor. |
|
106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
107 | 107 |
if (_copy.attached()) { |
108 | 108 |
Parent::attach(*_copy.notifier()); |
109 | 109 |
container = _copy.container; |
110 | 110 |
} |
111 | 111 |
} |
112 | 112 |
|
113 |
/// \brief Assign operator. |
|
114 |
/// |
|
115 |
/// This operator assigns for each item in the map the |
|
116 |
/// value mapped to the same item in the copied map. |
|
117 |
/// The parameter map should be indiced with the same |
|
118 |
/// itemset because this assign operator does not change |
|
119 |
// |
|
113 |
// \brief Assign operator. |
|
114 |
// |
|
115 |
// This operator assigns for each item in the map the |
|
116 |
// value mapped to the same item in the copied map. |
|
117 |
// The parameter map should be indiced with the same |
|
118 |
// itemset because this assign operator does not change |
|
119 |
// the container of the map. |
|
120 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
121 | 121 |
return operator=<VectorMap>(cmap); |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
|
125 |
/// \brief Template assign operator. |
|
126 |
/// |
|
127 |
/// The given parameter should be conform to the ReadMap |
|
128 |
/// concecpt and could be indiced by the current item set of |
|
129 |
/// the NodeMap. In this case the value for each item |
|
130 |
/// is assigned by the value of the given ReadMap. |
|
125 |
// \brief Template assign operator. |
|
126 |
// |
|
127 |
// The given parameter should be conform to the ReadMap |
|
128 |
// concecpt and could be indiced by the current item set of |
|
129 |
// the NodeMap. In this case the value for each item |
|
130 |
// is assigned by the value of the given ReadMap. |
|
131 | 131 |
template <typename CMap> |
132 | 132 |
VectorMap& operator=(const CMap& cmap) { |
133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
135 | 135 |
Item it; |
136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
137 | 137 |
set(it, cmap[it]); |
138 | 138 |
} |
139 | 139 |
return *this; |
140 | 140 |
} |
141 | 141 |
|
142 | 142 |
public: |
143 | 143 |
|
144 |
/// \brief The subcript operator. |
|
145 |
/// |
|
146 |
/// The subscript operator. The map can be subscripted by the |
|
147 |
/// actual items of the graph. |
|
144 |
// \brief The subcript operator. |
|
145 |
// |
|
146 |
// The subscript operator. The map can be subscripted by the |
|
147 |
// actual items of the graph. |
|
148 | 148 |
Reference operator[](const Key& key) { |
149 | 149 |
return container[Parent::notifier()->id(key)]; |
150 | 150 |
} |
151 | 151 |
|
152 |
/// \brief The const subcript operator. |
|
153 |
/// |
|
154 |
/// The const subscript operator. The map can be subscripted by the |
|
155 |
/// actual items of the graph. |
|
152 |
// \brief The const subcript operator. |
|
153 |
// |
|
154 |
// The const subscript operator. The map can be subscripted by the |
|
155 |
// actual items of the graph. |
|
156 | 156 |
ConstReference operator[](const Key& key) const { |
157 | 157 |
return container[Parent::notifier()->id(key)]; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
|
161 |
/// \brief The setter function of the map. |
|
162 |
/// |
|
163 |
// |
|
161 |
// \brief The setter function of the map. |
|
162 |
// |
|
163 |
// It the same as operator[](key) = value expression. |
|
164 | 164 |
void set(const Key& key, const Value& value) { |
165 | 165 |
(*this)[key] = value; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
|
170 |
/// \brief Adds a new key to the map. |
|
171 |
/// |
|
172 |
/// It adds a new key to the map. It called by the observer notifier |
|
173 |
/// and it overrides the add() member function of the observer base. |
|
170 |
// \brief Adds a new key to the map. |
|
171 |
// |
|
172 |
// It adds a new key to the map. It called by the observer notifier |
|
173 |
// and it overrides the add() member function of the observer base. |
|
174 | 174 |
virtual void add(const Key& key) { |
175 | 175 |
int id = Parent::notifier()->id(key); |
176 | 176 |
if (id >= int(container.size())) { |
177 | 177 |
container.resize(id + 1); |
178 | 178 |
} |
179 | 179 |
} |
180 | 180 |
|
181 |
/// \brief Adds more new keys to the map. |
|
182 |
/// |
|
183 |
/// It adds more new keys to the map. It called by the observer notifier |
|
184 |
/// and it overrides the add() member function of the observer base. |
|
181 |
// \brief Adds more new keys to the map. |
|
182 |
// |
|
183 |
// It adds more new keys to the map. It called by the observer notifier |
|
184 |
// and it overrides the add() member function of the observer base. |
|
185 | 185 |
virtual void add(const std::vector<Key>& keys) { |
186 | 186 |
int max = container.size() - 1; |
187 | 187 |
for (int i = 0; i < int(keys.size()); ++i) { |
188 | 188 |
int id = Parent::notifier()->id(keys[i]); |
189 | 189 |
if (id >= max) { |
190 | 190 |
max = id; |
191 | 191 |
} |
192 | 192 |
} |
193 | 193 |
container.resize(max + 1); |
194 | 194 |
} |
195 | 195 |
|
196 |
/// \brief Erase a key from the map. |
|
197 |
/// |
|
198 |
/// Erase a key from the map. It called by the observer notifier |
|
199 |
/// and it overrides the erase() member function of the observer base. |
|
196 |
// \brief Erase a key from the map. |
|
197 |
// |
|
198 |
// Erase a key from the map. It called by the observer notifier |
|
199 |
// and it overrides the erase() member function of the observer base. |
|
200 | 200 |
virtual void erase(const Key& key) { |
201 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
202 | 202 |
} |
203 | 203 |
|
204 |
/// \brief Erase more keys from the map. |
|
205 |
/// |
|
206 |
/// Erase more keys from the map. It called by the observer notifier |
|
207 |
/// and it overrides the erase() member function of the observer base. |
|
204 |
// \brief Erase more keys from the map. |
|
205 |
// |
|
206 |
// Erase more keys from the map. It called by the observer notifier |
|
207 |
// and it overrides the erase() member function of the observer base. |
|
208 | 208 |
virtual void erase(const std::vector<Key>& keys) { |
209 | 209 |
for (int i = 0; i < int(keys.size()); ++i) { |
210 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
211 | 211 |
} |
212 | 212 |
} |
213 | 213 |
|
214 |
/// \brief Buildes the map. |
|
215 |
/// |
|
216 |
/// It buildes the map. It called by the observer notifier |
|
217 |
/// and it overrides the build() member function of the observer base. |
|
214 |
// \brief Buildes the map. |
|
215 |
// |
|
216 |
// It buildes the map. It called by the observer notifier |
|
217 |
// and it overrides the build() member function of the observer base. |
|
218 | 218 |
virtual void build() { |
219 | 219 |
int size = Parent::notifier()->maxId() + 1; |
220 | 220 |
container.reserve(size); |
221 | 221 |
container.resize(size); |
222 | 222 |
} |
223 | 223 |
|
224 |
/// \brief Clear the map. |
|
225 |
/// |
|
226 |
/// It erase all items from the map. It called by the observer notifier |
|
227 |
/// and it overrides the clear() member function of the observer base. |
|
224 |
// \brief Clear the map. |
|
225 |
// |
|
226 |
// It erase all items from the map. It called by the observer notifier |
|
227 |
// and it overrides the clear() member function of the observer base. |
|
228 | 228 |
virtual void clear() { |
229 | 229 |
container.clear(); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
|
234 | 234 |
Container container; |
235 | 235 |
|
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_CONCEPT_MAPS_H |
20 | 20 |
#define LEMON_CONCEPT_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 |
///\ingroup |
|
25 |
///\ingroup map_concepts |
|
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 |
/// \addtogroup |
|
33 |
/// \addtogroup map_concepts |
|
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 | 185 |
void constraints() { |
186 | 186 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
187 | 187 |
ref = m[key]; |
188 | 188 |
m[key] = val; |
189 | 189 |
m[key] = ref; |
190 | 190 |
m[key] = cref; |
191 | 191 |
own_ref = m[own_key]; |
192 | 192 |
m[own_key] = own_val; |
193 | 193 |
m[own_key] = own_ref; |
194 | 194 |
m[own_key] = own_cref; |
195 | 195 |
m[key] = m[own_key]; |
196 | 196 |
m[own_key] = m[key]; |
197 | 197 |
} |
198 | 198 |
const Key& key; |
199 | 199 |
Value& val; |
200 | 200 |
Reference ref; |
201 | 201 |
ConstReference cref; |
202 | 202 |
const typename _ReferenceMap::Key& own_key; |
203 | 203 |
typename _ReferenceMap::Value& own_val; |
204 | 204 |
typename _ReferenceMap::Reference own_ref; |
205 | 205 |
typename _ReferenceMap::ConstReference own_cref; |
206 | 206 |
_ReferenceMap& m; |
207 | 207 |
}; |
208 | 208 |
}; |
209 | 209 |
|
210 | 210 |
// @} |
211 | 211 |
|
212 | 212 |
} //namespace concepts |
213 | 213 |
|
214 | 214 |
} //namespace lemon |
215 | 215 |
|
216 | 216 |
#endif // LEMON_CONCEPT_MAPS_H |
... | ... |
@@ -195,549 +195,538 @@ |
195 | 195 |
if (is >> c) { |
196 | 196 |
if (c != '(') is.putback(c); |
197 | 197 |
} else { |
198 | 198 |
is.clear(); |
199 | 199 |
} |
200 | 200 |
if (!(is >> z.x)) return is; |
201 | 201 |
if (is >> c) { |
202 | 202 |
if (c != ',') is.putback(c); |
203 | 203 |
} else { |
204 | 204 |
is.clear(); |
205 | 205 |
} |
206 | 206 |
if (!(is >> z.y)) return is; |
207 | 207 |
if (is >> c) { |
208 | 208 |
if (c != ')') is.putback(c); |
209 | 209 |
} else { |
210 | 210 |
is.clear(); |
211 | 211 |
} |
212 | 212 |
return is; |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
///Write a plain vector to a stream |
216 | 216 |
|
217 | 217 |
///Write a plain vector to a stream. |
218 | 218 |
///\relates Point |
219 | 219 |
/// |
220 | 220 |
template<typename T> |
221 | 221 |
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z) |
222 | 222 |
{ |
223 | 223 |
os << "(" << z.x << "," << z.y << ")"; |
224 | 224 |
return os; |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
///Rotate by 90 degrees |
228 | 228 |
|
229 | 229 |
///Returns the parameter rotated by 90 degrees in positive direction. |
230 | 230 |
///\relates Point |
231 | 231 |
/// |
232 | 232 |
template<typename T> |
233 | 233 |
inline Point<T> rot90(const Point<T> &z) |
234 | 234 |
{ |
235 | 235 |
return Point<T>(-z.y,z.x); |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
///Rotate by 180 degrees |
239 | 239 |
|
240 | 240 |
///Returns the parameter rotated by 180 degrees. |
241 | 241 |
///\relates Point |
242 | 242 |
/// |
243 | 243 |
template<typename T> |
244 | 244 |
inline Point<T> rot180(const Point<T> &z) |
245 | 245 |
{ |
246 | 246 |
return Point<T>(-z.x,-z.y); |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
///Rotate by 270 degrees |
250 | 250 |
|
251 | 251 |
///Returns the parameter rotated by 90 degrees in negative direction. |
252 | 252 |
///\relates Point |
253 | 253 |
/// |
254 | 254 |
template<typename T> |
255 | 255 |
inline Point<T> rot270(const Point<T> &z) |
256 | 256 |
{ |
257 | 257 |
return Point<T>(z.y,-z.x); |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
|
261 | 261 |
|
262 | 262 |
/// Bounding box of plain vectors (points). |
263 | 263 |
|
264 | 264 |
/// A class to calculate or store the bounding box of plain vectors |
265 | 265 |
/// (\ref Point "points"). |
266 | 266 |
template<typename T> |
267 | 267 |
class Box { |
268 | 268 |
Point<T> _bottom_left, _top_right; |
269 | 269 |
bool _empty; |
270 | 270 |
public: |
271 | 271 |
|
272 | 272 |
///Default constructor: creates an empty box |
273 | 273 |
Box() { _empty = true; } |
274 | 274 |
|
275 | 275 |
///Construct a box from one point |
276 | 276 |
Box(Point<T> a) { |
277 | 277 |
_bottom_left = _top_right = a; |
278 | 278 |
_empty = false; |
279 | 279 |
} |
280 | 280 |
|
281 | 281 |
///Construct a box from two points |
282 | 282 |
|
283 | 283 |
///Construct a box from two points. |
284 | 284 |
///\param a The bottom left corner. |
285 | 285 |
///\param b The top right corner. |
286 | 286 |
///\warning The coordinates of the bottom left corner must be no more |
287 | 287 |
///than those of the top right one. |
288 | 288 |
Box(Point<T> a,Point<T> b) |
289 | 289 |
{ |
290 | 290 |
_bottom_left = a; |
291 | 291 |
_top_right = b; |
292 | 292 |
_empty = false; |
293 | 293 |
} |
294 | 294 |
|
295 | 295 |
///Construct a box from four numbers |
296 | 296 |
|
297 | 297 |
///Construct a box from four numbers. |
298 | 298 |
///\param l The left side of the box. |
299 | 299 |
///\param b The bottom of the box. |
300 | 300 |
///\param r The right side of the box. |
301 | 301 |
///\param t The top of the box. |
302 | 302 |
///\warning The left side must be no more than the right side and |
303 | 303 |
///bottom must be no more than the top. |
304 | 304 |
Box(T l,T b,T r,T t) |
305 | 305 |
{ |
306 | 306 |
_bottom_left=Point<T>(l,b); |
307 | 307 |
_top_right=Point<T>(r,t); |
308 | 308 |
_empty = false; |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
///Return \c true if the box is empty. |
312 | 312 |
|
313 | 313 |
///Return \c true if the box is empty (i.e. return \c false |
314 | 314 |
///if at least one point was added to the box or the coordinates of |
315 | 315 |
///the box were set). |
316 | 316 |
/// |
317 | 317 |
///The coordinates of an empty box are not defined. |
318 | 318 |
bool empty() const { |
319 | 319 |
return _empty; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
///Make the box empty |
323 | 323 |
void clear() { |
324 | 324 |
_empty = true; |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
///Give back the bottom left corner of the box |
328 | 328 |
|
329 | 329 |
///Give back the bottom left corner of the box. |
330 | 330 |
///If the box is empty, then the return value is not defined. |
331 | 331 |
Point<T> bottomLeft() const { |
332 | 332 |
return _bottom_left; |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
///Set the bottom left corner of the box |
336 | 336 |
|
337 | 337 |
///Set the bottom left corner of the box. |
338 | 338 |
///\pre The box must not be empty. |
339 | 339 |
void bottomLeft(Point<T> p) { |
340 | 340 |
_bottom_left = p; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
///Give back the top right corner of the box |
344 | 344 |
|
345 | 345 |
///Give back the top right corner of the box. |
346 | 346 |
///If the box is empty, then the return value is not defined. |
347 | 347 |
Point<T> topRight() const { |
348 | 348 |
return _top_right; |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
///Set the top right corner of the box |
352 | 352 |
|
353 | 353 |
///Set the top right corner of the box. |
354 | 354 |
///\pre The box must not be empty. |
355 | 355 |
void topRight(Point<T> p) { |
356 | 356 |
_top_right = p; |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
///Give back the bottom right corner of the box |
360 | 360 |
|
361 | 361 |
///Give back the bottom right corner of the box. |
362 | 362 |
///If the box is empty, then the return value is not defined. |
363 | 363 |
Point<T> bottomRight() const { |
364 | 364 |
return Point<T>(_top_right.x,_bottom_left.y); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
///Set the bottom right corner of the box |
368 | 368 |
|
369 | 369 |
///Set the bottom right corner of the box. |
370 | 370 |
///\pre The box must not be empty. |
371 | 371 |
void bottomRight(Point<T> p) { |
372 | 372 |
_top_right.x = p.x; |
373 | 373 |
_bottom_left.y = p.y; |
374 | 374 |
} |
375 | 375 |
|
376 | 376 |
///Give back the top left corner of the box |
377 | 377 |
|
378 | 378 |
///Give back the top left corner of the box. |
379 | 379 |
///If the box is empty, then the return value is not defined. |
380 | 380 |
Point<T> topLeft() const { |
381 | 381 |
return Point<T>(_bottom_left.x,_top_right.y); |
382 | 382 |
} |
383 | 383 |
|
384 | 384 |
///Set the top left corner of the box |
385 | 385 |
|
386 | 386 |
///Set the top left corner of the box. |
387 | 387 |
///\pre The box must not be empty. |
388 | 388 |
void topLeft(Point<T> p) { |
389 | 389 |
_top_right.y = p.y; |
390 | 390 |
_bottom_left.x = p.x; |
391 | 391 |
} |
392 | 392 |
|
393 | 393 |
///Give back the bottom of the box |
394 | 394 |
|
395 | 395 |
///Give back the bottom of the box. |
396 | 396 |
///If the box is empty, then the return value is not defined. |
397 | 397 |
T bottom() const { |
398 | 398 |
return _bottom_left.y; |
399 | 399 |
} |
400 | 400 |
|
401 | 401 |
///Set the bottom of the box |
402 | 402 |
|
403 | 403 |
///Set the bottom of the box. |
404 | 404 |
///\pre The box must not be empty. |
405 | 405 |
void bottom(T t) { |
406 | 406 |
_bottom_left.y = t; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
///Give back the top of the box |
410 | 410 |
|
411 | 411 |
///Give back the top of the box. |
412 | 412 |
///If the box is empty, then the return value is not defined. |
413 | 413 |
T top() const { |
414 | 414 |
return _top_right.y; |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
///Set the top of the box |
418 | 418 |
|
419 | 419 |
///Set the top of the box. |
420 | 420 |
///\pre The box must not be empty. |
421 | 421 |
void top(T t) { |
422 | 422 |
_top_right.y = t; |
423 | 423 |
} |
424 | 424 |
|
425 | 425 |
///Give back the left side of the box |
426 | 426 |
|
427 | 427 |
///Give back the left side of the box. |
428 | 428 |
///If the box is empty, then the return value is not defined. |
429 | 429 |
T left() const { |
430 | 430 |
return _bottom_left.x; |
431 | 431 |
} |
432 | 432 |
|
433 | 433 |
///Set the left side of the box |
434 | 434 |
|
435 | 435 |
///Set the left side of the box. |
436 | 436 |
///\pre The box must not be empty. |
437 | 437 |
void left(T t) { |
438 | 438 |
_bottom_left.x = t; |
439 | 439 |
} |
440 | 440 |
|
441 | 441 |
/// Give back the right side of the box |
442 | 442 |
|
443 | 443 |
/// Give back the right side of the box. |
444 | 444 |
///If the box is empty, then the return value is not defined. |
445 | 445 |
T right() const { |
446 | 446 |
return _top_right.x; |
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
///Set the right side of the box |
450 | 450 |
|
451 | 451 |
///Set the right side of the box. |
452 | 452 |
///\pre The box must not be empty. |
453 | 453 |
void right(T t) { |
454 | 454 |
_top_right.x = t; |
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
///Give back the height of the box |
458 | 458 |
|
459 | 459 |
///Give back the height of the box. |
460 | 460 |
///If the box is empty, then the return value is not defined. |
461 | 461 |
T height() const { |
462 | 462 |
return _top_right.y-_bottom_left.y; |
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
///Give back the width of the box |
466 | 466 |
|
467 | 467 |
///Give back the width of the box. |
468 | 468 |
///If the box is empty, then the return value is not defined. |
469 | 469 |
T width() const { |
470 | 470 |
return _top_right.x-_bottom_left.x; |
471 | 471 |
} |
472 | 472 |
|
473 | 473 |
///Checks whether a point is inside the box |
474 | 474 |
bool inside(const Point<T>& u) const { |
475 | 475 |
if (_empty) |
476 | 476 |
return false; |
477 | 477 |
else { |
478 | 478 |
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 && |
479 | 479 |
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 ); |
480 | 480 |
} |
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
///Increments the box with a point |
484 | 484 |
|
485 | 485 |
///Increments the box with a point. |
486 | 486 |
/// |
487 | 487 |
Box& add(const Point<T>& u){ |
488 | 488 |
if (_empty) { |
489 | 489 |
_bottom_left = _top_right = u; |
490 | 490 |
_empty = false; |
491 | 491 |
} |
492 | 492 |
else { |
493 | 493 |
if (_bottom_left.x > u.x) _bottom_left.x = u.x; |
494 | 494 |
if (_bottom_left.y > u.y) _bottom_left.y = u.y; |
495 | 495 |
if (_top_right.x < u.x) _top_right.x = u.x; |
496 | 496 |
if (_top_right.y < u.y) _top_right.y = u.y; |
497 | 497 |
} |
498 | 498 |
return *this; |
499 | 499 |
} |
500 | 500 |
|
501 | 501 |
///Increments the box to contain another box |
502 | 502 |
|
503 | 503 |
///Increments the box to contain another box. |
504 | 504 |
/// |
505 | 505 |
Box& add(const Box &u){ |
506 | 506 |
if ( !u.empty() ){ |
507 | 507 |
add(u._bottom_left); |
508 | 508 |
add(u._top_right); |
509 | 509 |
} |
510 | 510 |
return *this; |
511 | 511 |
} |
512 | 512 |
|
513 | 513 |
///Intersection of two boxes |
514 | 514 |
|
515 | 515 |
///Intersection of two boxes. |
516 | 516 |
/// |
517 | 517 |
Box operator&(const Box& u) const { |
518 | 518 |
Box b; |
519 | 519 |
if (_empty || u._empty) { |
520 | 520 |
b._empty = true; |
521 | 521 |
} else { |
522 | 522 |
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x); |
523 | 523 |
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y); |
524 | 524 |
b._top_right.x = std::min(_top_right.x, u._top_right.x); |
525 | 525 |
b._top_right.y = std::min(_top_right.y, u._top_right.y); |
526 | 526 |
b._empty = b._bottom_left.x > b._top_right.x || |
527 | 527 |
b._bottom_left.y > b._top_right.y; |
528 | 528 |
} |
529 | 529 |
return b; |
530 | 530 |
} |
531 | 531 |
|
532 | 532 |
};//class Box |
533 | 533 |
|
534 | 534 |
|
535 | 535 |
///Read a box from a stream |
536 | 536 |
|
537 | 537 |
///Read a box from a stream. |
538 | 538 |
///\relates Box |
539 | 539 |
template<typename T> |
540 | 540 |
inline std::istream& operator>>(std::istream &is, Box<T>& b) { |
541 | 541 |
char c; |
542 | 542 |
Point<T> p; |
543 | 543 |
if (is >> c) { |
544 | 544 |
if (c != '(') is.putback(c); |
545 | 545 |
} else { |
546 | 546 |
is.clear(); |
547 | 547 |
} |
548 | 548 |
if (!(is >> p)) return is; |
549 | 549 |
b.bottomLeft(p); |
550 | 550 |
if (is >> c) { |
551 | 551 |
if (c != ',') is.putback(c); |
552 | 552 |
} else { |
553 | 553 |
is.clear(); |
554 | 554 |
} |
555 | 555 |
if (!(is >> p)) return is; |
556 | 556 |
b.topRight(p); |
557 | 557 |
if (is >> c) { |
558 | 558 |
if (c != ')') is.putback(c); |
559 | 559 |
} else { |
560 | 560 |
is.clear(); |
561 | 561 |
} |
562 | 562 |
return is; |
563 | 563 |
} |
564 | 564 |
|
565 | 565 |
///Write a box to a stream |
566 | 566 |
|
567 | 567 |
///Write a box to a stream. |
568 | 568 |
///\relates Box |
569 | 569 |
template<typename T> |
570 | 570 |
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b) |
571 | 571 |
{ |
572 | 572 |
os << "(" << b.bottomLeft() << "," << b.topRight() << ")"; |
573 | 573 |
return os; |
574 | 574 |
} |
575 | 575 |
|
576 | 576 |
///Map of x-coordinates of a <tt>Point</tt>-map |
577 | 577 |
|
578 | 578 |
///Map of x-coordinates of a \ref Point "Point"-map. |
579 |
/// |
|
579 |
/// |
|
580 | 580 |
template<class M> |
581 | 581 |
class XMap |
582 | 582 |
{ |
583 | 583 |
M& _map; |
584 | 584 |
public: |
585 | 585 |
|
586 | 586 |
typedef typename M::Value::Value Value; |
587 | 587 |
typedef typename M::Key Key; |
588 | 588 |
///\e |
589 | 589 |
XMap(M& map) : _map(map) {} |
590 | 590 |
Value operator[](Key k) const {return _map[k].x;} |
591 | 591 |
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
592 | 592 |
}; |
593 | 593 |
|
594 | 594 |
///Returns an XMap class |
595 | 595 |
|
596 | 596 |
///This function just returns an XMap class. |
597 |
/// |
|
598 |
///\ingroup maps |
|
599 | 597 |
///\relates XMap |
600 | 598 |
template<class M> |
601 | 599 |
inline XMap<M> xMap(M &m) |
602 | 600 |
{ |
603 | 601 |
return XMap<M>(m); |
604 | 602 |
} |
605 | 603 |
|
606 | 604 |
template<class M> |
607 | 605 |
inline XMap<M> xMap(const M &m) |
608 | 606 |
{ |
609 | 607 |
return XMap<M>(m); |
610 | 608 |
} |
611 | 609 |
|
612 | 610 |
///Constant (read only) version of XMap |
613 | 611 |
|
614 | 612 |
///Constant (read only) version of XMap. |
615 |
/// |
|
613 |
/// |
|
616 | 614 |
template<class M> |
617 | 615 |
class ConstXMap |
618 | 616 |
{ |
619 | 617 |
const M& _map; |
620 | 618 |
public: |
621 | 619 |
|
622 | 620 |
typedef typename M::Value::Value Value; |
623 | 621 |
typedef typename M::Key Key; |
624 | 622 |
///\e |
625 | 623 |
ConstXMap(const M &map) : _map(map) {} |
626 | 624 |
Value operator[](Key k) const {return _map[k].x;} |
627 | 625 |
}; |
628 | 626 |
|
629 | 627 |
///Returns a ConstXMap class |
630 | 628 |
|
631 | 629 |
///This function just returns a ConstXMap class. |
632 |
/// |
|
633 |
///\ingroup maps |
|
634 | 630 |
///\relates ConstXMap |
635 | 631 |
template<class M> |
636 | 632 |
inline ConstXMap<M> xMap(const M &m) |
637 | 633 |
{ |
638 | 634 |
return ConstXMap<M>(m); |
639 | 635 |
} |
640 | 636 |
|
641 | 637 |
///Map of y-coordinates of a <tt>Point</tt>-map |
642 | 638 |
|
643 | 639 |
///Map of y-coordinates of a \ref Point "Point"-map. |
644 |
/// |
|
640 |
/// |
|
645 | 641 |
template<class M> |
646 | 642 |
class YMap |
647 | 643 |
{ |
648 | 644 |
M& _map; |
649 | 645 |
public: |
650 | 646 |
|
651 | 647 |
typedef typename M::Value::Value Value; |
652 | 648 |
typedef typename M::Key Key; |
653 | 649 |
///\e |
654 | 650 |
YMap(M& map) : _map(map) {} |
655 | 651 |
Value operator[](Key k) const {return _map[k].y;} |
656 | 652 |
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
657 | 653 |
}; |
658 | 654 |
|
659 | 655 |
///Returns a YMap class |
660 | 656 |
|
661 | 657 |
///This function just returns a YMap class. |
662 |
/// |
|
663 |
///\ingroup maps |
|
664 | 658 |
///\relates YMap |
665 | 659 |
template<class M> |
666 | 660 |
inline YMap<M> yMap(M &m) |
667 | 661 |
{ |
668 | 662 |
return YMap<M>(m); |
669 | 663 |
} |
670 | 664 |
|
671 | 665 |
template<class M> |
672 | 666 |
inline YMap<M> yMap(const M &m) |
673 | 667 |
{ |
674 | 668 |
return YMap<M>(m); |
675 | 669 |
} |
676 | 670 |
|
677 | 671 |
///Constant (read only) version of YMap |
678 | 672 |
|
679 | 673 |
///Constant (read only) version of YMap. |
680 |
/// |
|
674 |
/// |
|
681 | 675 |
template<class M> |
682 | 676 |
class ConstYMap |
683 | 677 |
{ |
684 | 678 |
const M& _map; |
685 | 679 |
public: |
686 | 680 |
|
687 | 681 |
typedef typename M::Value::Value Value; |
688 | 682 |
typedef typename M::Key Key; |
689 | 683 |
///\e |
690 | 684 |
ConstYMap(const M &map) : _map(map) {} |
691 | 685 |
Value operator[](Key k) const {return _map[k].y;} |
692 | 686 |
}; |
693 | 687 |
|
694 | 688 |
///Returns a ConstYMap class |
695 | 689 |
|
696 | 690 |
///This function just returns a ConstYMap class. |
697 |
/// |
|
698 |
///\ingroup maps |
|
699 | 691 |
///\relates ConstYMap |
700 | 692 |
template<class M> |
701 | 693 |
inline ConstYMap<M> yMap(const M &m) |
702 | 694 |
{ |
703 | 695 |
return ConstYMap<M>(m); |
704 | 696 |
} |
705 | 697 |
|
706 | 698 |
|
707 | 699 |
///\brief Map of the normSquare() of a <tt>Point</tt>-map |
708 | 700 |
/// |
709 | 701 |
///Map of the \ref Point::normSquare() "normSquare()" |
710 | 702 |
///of a \ref Point "Point"-map. |
711 |
///\ingroup maps |
|
712 | 703 |
template<class M> |
713 | 704 |
class NormSquareMap |
714 | 705 |
{ |
715 | 706 |
const M& _map; |
716 | 707 |
public: |
717 | 708 |
|
718 | 709 |
typedef typename M::Value::Value Value; |
719 | 710 |
typedef typename M::Key Key; |
720 | 711 |
///\e |
721 | 712 |
NormSquareMap(const M &map) : _map(map) {} |
722 | 713 |
Value operator[](Key k) const {return _map[k].normSquare();} |
723 | 714 |
}; |
724 | 715 |
|
725 | 716 |
///Returns a NormSquareMap class |
726 | 717 |
|
727 | 718 |
///This function just returns a NormSquareMap class. |
728 |
/// |
|
729 |
///\ingroup maps |
|
730 | 719 |
///\relates NormSquareMap |
731 | 720 |
template<class M> |
732 | 721 |
inline NormSquareMap<M> normSquareMap(const M &m) |
733 | 722 |
{ |
734 | 723 |
return NormSquareMap<M>(m); |
735 | 724 |
} |
736 | 725 |
|
737 | 726 |
/// @} |
738 | 727 |
|
739 | 728 |
} //namespce dim2 |
740 | 729 |
|
741 | 730 |
} //namespace lemon |
742 | 731 |
|
743 | 732 |
#endif //LEMON_DIM2_H |
... | ... |
@@ -1302,1136 +1302,1142 @@ |
1302 | 1302 |
/// |
1303 | 1303 |
/// For example, if \c m is a map with \c double values, then |
1304 | 1304 |
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
1305 | 1305 |
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
1306 | 1306 |
/// negative. |
1307 | 1307 |
/// |
1308 | 1308 |
/// \relates AbsMap |
1309 | 1309 |
template<typename M> |
1310 | 1310 |
inline AbsMap<M> absMap(const M &m) { |
1311 | 1311 |
return AbsMap<M>(m); |
1312 | 1312 |
} |
1313 | 1313 |
|
1314 | 1314 |
/// @} |
1315 | 1315 |
|
1316 | 1316 |
// Logical maps and map adaptors: |
1317 | 1317 |
|
1318 | 1318 |
/// \addtogroup maps |
1319 | 1319 |
/// @{ |
1320 | 1320 |
|
1321 | 1321 |
/// Constant \c true map. |
1322 | 1322 |
|
1323 | 1323 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1324 | 1324 |
/// each key. |
1325 | 1325 |
/// |
1326 | 1326 |
/// Note that |
1327 | 1327 |
/// \code |
1328 | 1328 |
/// TrueMap<K> tm; |
1329 | 1329 |
/// \endcode |
1330 | 1330 |
/// is equivalent to |
1331 | 1331 |
/// \code |
1332 | 1332 |
/// ConstMap<K,bool> tm(true); |
1333 | 1333 |
/// \endcode |
1334 | 1334 |
/// |
1335 | 1335 |
/// \sa FalseMap |
1336 | 1336 |
/// \sa ConstMap |
1337 | 1337 |
template <typename K> |
1338 | 1338 |
class TrueMap : public MapBase<K, bool> { |
1339 | 1339 |
public: |
1340 | 1340 |
typedef MapBase<K, bool> Parent; |
1341 | 1341 |
typedef typename Parent::Key Key; |
1342 | 1342 |
typedef typename Parent::Value Value; |
1343 | 1343 |
|
1344 | 1344 |
/// Gives back \c true. |
1345 | 1345 |
Value operator[](const Key&) const { return true; } |
1346 | 1346 |
}; |
1347 | 1347 |
|
1348 | 1348 |
/// Returns a \c TrueMap class |
1349 | 1349 |
|
1350 | 1350 |
/// This function just returns a \c TrueMap class. |
1351 | 1351 |
/// \relates TrueMap |
1352 | 1352 |
template<typename K> |
1353 | 1353 |
inline TrueMap<K> trueMap() { |
1354 | 1354 |
return TrueMap<K>(); |
1355 | 1355 |
} |
1356 | 1356 |
|
1357 | 1357 |
|
1358 | 1358 |
/// Constant \c false map. |
1359 | 1359 |
|
1360 | 1360 |
/// This \ref concepts::ReadMap "read-only map" assigns \c false to |
1361 | 1361 |
/// each key. |
1362 | 1362 |
/// |
1363 | 1363 |
/// Note that |
1364 | 1364 |
/// \code |
1365 | 1365 |
/// FalseMap<K> fm; |
1366 | 1366 |
/// \endcode |
1367 | 1367 |
/// is equivalent to |
1368 | 1368 |
/// \code |
1369 | 1369 |
/// ConstMap<K,bool> fm(false); |
1370 | 1370 |
/// \endcode |
1371 | 1371 |
/// |
1372 | 1372 |
/// \sa TrueMap |
1373 | 1373 |
/// \sa ConstMap |
1374 | 1374 |
template <typename K> |
1375 | 1375 |
class FalseMap : public MapBase<K, bool> { |
1376 | 1376 |
public: |
1377 | 1377 |
typedef MapBase<K, bool> Parent; |
1378 | 1378 |
typedef typename Parent::Key Key; |
1379 | 1379 |
typedef typename Parent::Value Value; |
1380 | 1380 |
|
1381 | 1381 |
/// Gives back \c false. |
1382 | 1382 |
Value operator[](const Key&) const { return false; } |
1383 | 1383 |
}; |
1384 | 1384 |
|
1385 | 1385 |
/// Returns a \c FalseMap class |
1386 | 1386 |
|
1387 | 1387 |
/// This function just returns a \c FalseMap class. |
1388 | 1388 |
/// \relates FalseMap |
1389 | 1389 |
template<typename K> |
1390 | 1390 |
inline FalseMap<K> falseMap() { |
1391 | 1391 |
return FalseMap<K>(); |
1392 | 1392 |
} |
1393 | 1393 |
|
1394 | 1394 |
/// @} |
1395 | 1395 |
|
1396 | 1396 |
/// \addtogroup map_adaptors |
1397 | 1397 |
/// @{ |
1398 | 1398 |
|
1399 | 1399 |
/// Logical 'and' of two maps |
1400 | 1400 |
|
1401 | 1401 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1402 | 1402 |
/// 'and' of the values of the two given maps. |
1403 | 1403 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1404 | 1404 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1405 | 1405 |
/// |
1406 | 1406 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1407 | 1407 |
/// \code |
1408 | 1408 |
/// AndMap<M1,M2> am(m1,m2); |
1409 | 1409 |
/// \endcode |
1410 | 1410 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>. |
1411 | 1411 |
/// |
1412 | 1412 |
/// The simplest way of using this map is through the andMap() |
1413 | 1413 |
/// function. |
1414 | 1414 |
/// |
1415 | 1415 |
/// \sa OrMap |
1416 | 1416 |
/// \sa NotMap, NotWriteMap |
1417 | 1417 |
template<typename M1, typename M2> |
1418 | 1418 |
class AndMap : public MapBase<typename M1::Key, bool> { |
1419 | 1419 |
const M1 &_m1; |
1420 | 1420 |
const M2 &_m2; |
1421 | 1421 |
public: |
1422 | 1422 |
typedef MapBase<typename M1::Key, bool> Parent; |
1423 | 1423 |
typedef typename Parent::Key Key; |
1424 | 1424 |
typedef typename Parent::Value Value; |
1425 | 1425 |
|
1426 | 1426 |
/// Constructor |
1427 | 1427 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1428 | 1428 |
/// \e |
1429 | 1429 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; } |
1430 | 1430 |
}; |
1431 | 1431 |
|
1432 | 1432 |
/// Returns an \c AndMap class |
1433 | 1433 |
|
1434 | 1434 |
/// This function just returns an \c AndMap class. |
1435 | 1435 |
/// |
1436 | 1436 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
1437 | 1437 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to |
1438 | 1438 |
/// <tt>m1[x]&&m2[x]</tt>. |
1439 | 1439 |
/// |
1440 | 1440 |
/// \relates AndMap |
1441 | 1441 |
template<typename M1, typename M2> |
1442 | 1442 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) { |
1443 | 1443 |
return AndMap<M1, M2>(m1,m2); |
1444 | 1444 |
} |
1445 | 1445 |
|
1446 | 1446 |
|
1447 | 1447 |
/// Logical 'or' of two maps |
1448 | 1448 |
|
1449 | 1449 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1450 | 1450 |
/// 'or' of the values of the two given maps. |
1451 | 1451 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1452 | 1452 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1453 | 1453 |
/// |
1454 | 1454 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1455 | 1455 |
/// \code |
1456 | 1456 |
/// OrMap<M1,M2> om(m1,m2); |
1457 | 1457 |
/// \endcode |
1458 | 1458 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>. |
1459 | 1459 |
/// |
1460 | 1460 |
/// The simplest way of using this map is through the orMap() |
1461 | 1461 |
/// function. |
1462 | 1462 |
/// |
1463 | 1463 |
/// \sa AndMap |
1464 | 1464 |
/// \sa NotMap, NotWriteMap |
1465 | 1465 |
template<typename M1, typename M2> |
1466 | 1466 |
class OrMap : public MapBase<typename M1::Key, bool> { |
1467 | 1467 |
const M1 &_m1; |
1468 | 1468 |
const M2 &_m2; |
1469 | 1469 |
public: |
1470 | 1470 |
typedef MapBase<typename M1::Key, bool> Parent; |
1471 | 1471 |
typedef typename Parent::Key Key; |
1472 | 1472 |
typedef typename Parent::Value Value; |
1473 | 1473 |
|
1474 | 1474 |
/// Constructor |
1475 | 1475 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1476 | 1476 |
/// \e |
1477 | 1477 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; } |
1478 | 1478 |
}; |
1479 | 1479 |
|
1480 | 1480 |
/// Returns an \c OrMap class |
1481 | 1481 |
|
1482 | 1482 |
/// This function just returns an \c OrMap class. |
1483 | 1483 |
/// |
1484 | 1484 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
1485 | 1485 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to |
1486 | 1486 |
/// <tt>m1[x]||m2[x]</tt>. |
1487 | 1487 |
/// |
1488 | 1488 |
/// \relates OrMap |
1489 | 1489 |
template<typename M1, typename M2> |
1490 | 1490 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) { |
1491 | 1491 |
return OrMap<M1, M2>(m1,m2); |
1492 | 1492 |
} |
1493 | 1493 |
|
1494 | 1494 |
|
1495 | 1495 |
/// Logical 'not' of a map |
1496 | 1496 |
|
1497 | 1497 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1498 | 1498 |
/// negation of the values of the given map. |
1499 | 1499 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
1500 | 1500 |
/// |
1501 | 1501 |
/// The simplest way of using this map is through the notMap() |
1502 | 1502 |
/// function. |
1503 | 1503 |
/// |
1504 | 1504 |
/// \sa NotWriteMap |
1505 | 1505 |
template <typename M> |
1506 | 1506 |
class NotMap : public MapBase<typename M::Key, bool> { |
1507 | 1507 |
const M &_m; |
1508 | 1508 |
public: |
1509 | 1509 |
typedef MapBase<typename M::Key, bool> Parent; |
1510 | 1510 |
typedef typename Parent::Key Key; |
1511 | 1511 |
typedef typename Parent::Value Value; |
1512 | 1512 |
|
1513 | 1513 |
/// Constructor |
1514 | 1514 |
NotMap(const M &m) : _m(m) {} |
1515 | 1515 |
/// \e |
1516 | 1516 |
Value operator[](const Key &k) const { return !_m[k]; } |
1517 | 1517 |
}; |
1518 | 1518 |
|
1519 | 1519 |
/// Logical 'not' of a map (read-write version) |
1520 | 1520 |
|
1521 | 1521 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
1522 | 1522 |
/// logical negation of the values of the given map. |
1523 | 1523 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
1524 | 1524 |
/// It makes also possible to write the map. When a value is set, |
1525 | 1525 |
/// the opposite value is set to the original map. |
1526 | 1526 |
/// |
1527 | 1527 |
/// The simplest way of using this map is through the notWriteMap() |
1528 | 1528 |
/// function. |
1529 | 1529 |
/// |
1530 | 1530 |
/// \sa NotMap |
1531 | 1531 |
template <typename M> |
1532 | 1532 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1533 | 1533 |
M &_m; |
1534 | 1534 |
public: |
1535 | 1535 |
typedef MapBase<typename M::Key, bool> Parent; |
1536 | 1536 |
typedef typename Parent::Key Key; |
1537 | 1537 |
typedef typename Parent::Value Value; |
1538 | 1538 |
|
1539 | 1539 |
/// Constructor |
1540 | 1540 |
NotWriteMap(M &m) : _m(m) {} |
1541 | 1541 |
/// \e |
1542 | 1542 |
Value operator[](const Key &k) const { return !_m[k]; } |
1543 | 1543 |
/// \e |
1544 | 1544 |
void set(const Key &k, bool v) { _m.set(k, !v); } |
1545 | 1545 |
}; |
1546 | 1546 |
|
1547 | 1547 |
/// Returns a \c NotMap class |
1548 | 1548 |
|
1549 | 1549 |
/// This function just returns a \c NotMap class. |
1550 | 1550 |
/// |
1551 | 1551 |
/// For example, if \c m is a map with \c bool values, then |
1552 | 1552 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
1553 | 1553 |
/// |
1554 | 1554 |
/// \relates NotMap |
1555 | 1555 |
template <typename M> |
1556 | 1556 |
inline NotMap<M> notMap(const M &m) { |
1557 | 1557 |
return NotMap<M>(m); |
1558 | 1558 |
} |
1559 | 1559 |
|
1560 | 1560 |
/// Returns a \c NotWriteMap class |
1561 | 1561 |
|
1562 | 1562 |
/// This function just returns a \c NotWriteMap class. |
1563 | 1563 |
/// |
1564 | 1564 |
/// For example, if \c m is a map with \c bool values, then |
1565 | 1565 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
1566 | 1566 |
/// Moreover it makes also possible to write the map. |
1567 | 1567 |
/// |
1568 | 1568 |
/// \relates NotWriteMap |
1569 | 1569 |
template <typename M> |
1570 | 1570 |
inline NotWriteMap<M> notWriteMap(M &m) { |
1571 | 1571 |
return NotWriteMap<M>(m); |
1572 | 1572 |
} |
1573 | 1573 |
|
1574 | 1574 |
|
1575 | 1575 |
/// Combination of two maps using the \c == operator |
1576 | 1576 |
|
1577 | 1577 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1578 | 1578 |
/// the keys for which the corresponding values of the two maps are |
1579 | 1579 |
/// equal. |
1580 | 1580 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1581 | 1581 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1582 | 1582 |
/// |
1583 | 1583 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1584 | 1584 |
/// \code |
1585 | 1585 |
/// EqualMap<M1,M2> em(m1,m2); |
1586 | 1586 |
/// \endcode |
1587 | 1587 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
1588 | 1588 |
/// |
1589 | 1589 |
/// The simplest way of using this map is through the equalMap() |
1590 | 1590 |
/// function. |
1591 | 1591 |
/// |
1592 | 1592 |
/// \sa LessMap |
1593 | 1593 |
template<typename M1, typename M2> |
1594 | 1594 |
class EqualMap : public MapBase<typename M1::Key, bool> { |
1595 | 1595 |
const M1 &_m1; |
1596 | 1596 |
const M2 &_m2; |
1597 | 1597 |
public: |
1598 | 1598 |
typedef MapBase<typename M1::Key, bool> Parent; |
1599 | 1599 |
typedef typename Parent::Key Key; |
1600 | 1600 |
typedef typename Parent::Value Value; |
1601 | 1601 |
|
1602 | 1602 |
/// Constructor |
1603 | 1603 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1604 | 1604 |
/// \e |
1605 | 1605 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } |
1606 | 1606 |
}; |
1607 | 1607 |
|
1608 | 1608 |
/// Returns an \c EqualMap class |
1609 | 1609 |
|
1610 | 1610 |
/// This function just returns an \c EqualMap class. |
1611 | 1611 |
/// |
1612 | 1612 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1613 | 1613 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
1614 | 1614 |
/// <tt>m1[x]==m2[x]</tt>. |
1615 | 1615 |
/// |
1616 | 1616 |
/// \relates EqualMap |
1617 | 1617 |
template<typename M1, typename M2> |
1618 | 1618 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) { |
1619 | 1619 |
return EqualMap<M1, M2>(m1,m2); |
1620 | 1620 |
} |
1621 | 1621 |
|
1622 | 1622 |
|
1623 | 1623 |
/// Combination of two maps using the \c < operator |
1624 | 1624 |
|
1625 | 1625 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1626 | 1626 |
/// the keys for which the corresponding value of the first map is |
1627 | 1627 |
/// less then the value of the second map. |
1628 | 1628 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1629 | 1629 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1630 | 1630 |
/// |
1631 | 1631 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1632 | 1632 |
/// \code |
1633 | 1633 |
/// LessMap<M1,M2> lm(m1,m2); |
1634 | 1634 |
/// \endcode |
1635 | 1635 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
1636 | 1636 |
/// |
1637 | 1637 |
/// The simplest way of using this map is through the lessMap() |
1638 | 1638 |
/// function. |
1639 | 1639 |
/// |
1640 | 1640 |
/// \sa EqualMap |
1641 | 1641 |
template<typename M1, typename M2> |
1642 | 1642 |
class LessMap : public MapBase<typename M1::Key, bool> { |
1643 | 1643 |
const M1 &_m1; |
1644 | 1644 |
const M2 &_m2; |
1645 | 1645 |
public: |
1646 | 1646 |
typedef MapBase<typename M1::Key, bool> Parent; |
1647 | 1647 |
typedef typename Parent::Key Key; |
1648 | 1648 |
typedef typename Parent::Value Value; |
1649 | 1649 |
|
1650 | 1650 |
/// Constructor |
1651 | 1651 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1652 | 1652 |
/// \e |
1653 | 1653 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } |
1654 | 1654 |
}; |
1655 | 1655 |
|
1656 | 1656 |
/// Returns an \c LessMap class |
1657 | 1657 |
|
1658 | 1658 |
/// This function just returns an \c LessMap class. |
1659 | 1659 |
/// |
1660 | 1660 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1661 | 1661 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
1662 | 1662 |
/// <tt>m1[x]<m2[x]</tt>. |
1663 | 1663 |
/// |
1664 | 1664 |
/// \relates LessMap |
1665 | 1665 |
template<typename M1, typename M2> |
1666 | 1666 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) { |
1667 | 1667 |
return LessMap<M1, M2>(m1,m2); |
1668 | 1668 |
} |
1669 | 1669 |
|
1670 | 1670 |
namespace _maps_bits { |
1671 | 1671 |
|
1672 | 1672 |
template <typename _Iterator, typename Enable = void> |
1673 | 1673 |
struct IteratorTraits { |
1674 | 1674 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1675 | 1675 |
}; |
1676 | 1676 |
|
1677 | 1677 |
template <typename _Iterator> |
1678 | 1678 |
struct IteratorTraits<_Iterator, |
1679 | 1679 |
typename exists<typename _Iterator::container_type>::type> |
1680 | 1680 |
{ |
1681 | 1681 |
typedef typename _Iterator::container_type::value_type Value; |
1682 | 1682 |
}; |
1683 | 1683 |
|
1684 | 1684 |
} |
1685 | 1685 |
|
1686 |
/// @} |
|
1687 |
|
|
1688 |
/// \addtogroup maps |
|
1689 |
/// @{ |
|
1690 |
|
|
1686 | 1691 |
/// \brief Writable bool map for logging each \c true assigned element |
1687 | 1692 |
/// |
1688 | 1693 |
/// A \ref concepts::WriteMap "writable" bool map for logging |
1689 | 1694 |
/// each \c true assigned element, i.e it copies subsequently each |
1690 | 1695 |
/// keys set to \c true to the given iterator. |
1691 | 1696 |
/// The most important usage of it is storing certain nodes or arcs |
1692 | 1697 |
/// that were marked \c true by an algorithm. |
1693 | 1698 |
/// |
1694 | 1699 |
/// There are several algorithms that provide solutions through bool |
1695 | 1700 |
/// maps and most of them assign \c true at most once for each key. |
1696 | 1701 |
/// In these cases it is a natural request to store each \c true |
1697 | 1702 |
/// assigned elements (in order of the assignment), which can be |
1698 | 1703 |
/// easily done with LoggerBoolMap. |
1699 | 1704 |
/// |
1700 | 1705 |
/// The simplest way of using this map is through the loggerBoolMap() |
1701 | 1706 |
/// function. |
1702 | 1707 |
/// |
1703 | 1708 |
/// \tparam It The type of the iterator. |
1704 | 1709 |
/// \tparam Ke The key type of the map. The default value set |
1705 | 1710 |
/// according to the iterator type should work in most cases. |
1706 | 1711 |
/// |
1707 | 1712 |
/// \note The container of the iterator must contain enough space |
1708 | 1713 |
/// for the elements or the iterator should be an inserter iterator. |
1709 | 1714 |
#ifdef DOXYGEN |
1710 | 1715 |
template <typename It, typename Ke> |
1711 | 1716 |
#else |
1712 | 1717 |
template <typename It, |
1713 | 1718 |
typename Ke=typename _maps_bits::IteratorTraits<It>::Value> |
1714 | 1719 |
#endif |
1715 | 1720 |
class LoggerBoolMap { |
1716 | 1721 |
public: |
1717 | 1722 |
typedef It Iterator; |
1718 | 1723 |
|
1719 | 1724 |
typedef Ke Key; |
1720 | 1725 |
typedef bool Value; |
1721 | 1726 |
|
1722 | 1727 |
/// Constructor |
1723 | 1728 |
LoggerBoolMap(Iterator it) |
1724 | 1729 |
: _begin(it), _end(it) {} |
1725 | 1730 |
|
1726 | 1731 |
/// Gives back the given iterator set for the first key |
1727 | 1732 |
Iterator begin() const { |
1728 | 1733 |
return _begin; |
1729 | 1734 |
} |
1730 | 1735 |
|
1731 | 1736 |
/// Gives back the the 'after the last' iterator |
1732 | 1737 |
Iterator end() const { |
1733 | 1738 |
return _end; |
1734 | 1739 |
} |
1735 | 1740 |
|
1736 | 1741 |
/// The set function of the map |
1737 | 1742 |
void set(const Key& key, Value value) { |
1738 | 1743 |
if (value) { |
1739 | 1744 |
*_end++ = key; |
1740 | 1745 |
} |
1741 | 1746 |
} |
1742 | 1747 |
|
1743 | 1748 |
private: |
1744 | 1749 |
Iterator _begin; |
1745 | 1750 |
Iterator _end; |
1746 | 1751 |
}; |
1747 | 1752 |
|
1748 | 1753 |
/// Returns a \c LoggerBoolMap class |
1749 | 1754 |
|
1750 | 1755 |
/// This function just returns a \c LoggerBoolMap class. |
1751 | 1756 |
/// |
1752 | 1757 |
/// The most important usage of it is storing certain nodes or arcs |
1753 | 1758 |
/// that were marked \c true by an algorithm. |
1754 | 1759 |
/// For example it makes easier to store the nodes in the processing |
1755 | 1760 |
/// order of Dfs algorithm, as the following examples show. |
1756 | 1761 |
/// \code |
1757 | 1762 |
/// std::vector<Node> v; |
1758 | 1763 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
1759 | 1764 |
/// \endcode |
1760 | 1765 |
/// \code |
1761 | 1766 |
/// std::vector<Node> v(countNodes(g)); |
1762 | 1767 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
1763 | 1768 |
/// \endcode |
1764 | 1769 |
/// |
1765 | 1770 |
/// \note The container of the iterator must contain enough space |
1766 | 1771 |
/// for the elements or the iterator should be an inserter iterator. |
1767 | 1772 |
/// |
1768 | 1773 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
1769 | 1774 |
/// it cannot be used when a readable map is needed, for example as |
1770 | 1775 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
1771 | 1776 |
/// |
1772 | 1777 |
/// \relates LoggerBoolMap |
1773 | 1778 |
template<typename Iterator> |
1774 | 1779 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) { |
1775 | 1780 |
return LoggerBoolMap<Iterator>(it); |
1776 | 1781 |
} |
1777 | 1782 |
|
1783 |
/// @} |
|
1784 |
|
|
1785 |
/// \addtogroup graph_maps |
|
1786 |
/// @{ |
|
1787 |
|
|
1778 | 1788 |
/// Provides an immutable and unique id for each item in the graph. |
1779 | 1789 |
|
1780 | 1790 |
/// The IdMap class provides a unique and immutable id for each item of the |
1781 | 1791 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
1782 | 1792 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
1783 | 1793 |
/// item (node) does not change (even if you delete other nodes). </ul> |
1784 | 1794 |
/// Through this map you get access (i.e. can read) the inner id values of |
1785 | 1795 |
/// the items stored in the graph. This map can be inverted with its member |
1786 | 1796 |
/// class \c InverseMap or with the \c operator() member. |
1787 | 1797 |
/// |
1788 | 1798 |
template <typename _Graph, typename _Item> |
1789 | 1799 |
class IdMap { |
1790 | 1800 |
public: |
1791 | 1801 |
typedef _Graph Graph; |
1792 | 1802 |
typedef int Value; |
1793 | 1803 |
typedef _Item Item; |
1794 | 1804 |
typedef _Item Key; |
1795 | 1805 |
|
1796 | 1806 |
/// \brief Constructor. |
1797 | 1807 |
/// |
1798 | 1808 |
/// Constructor of the map. |
1799 | 1809 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
1800 | 1810 |
|
1801 | 1811 |
/// \brief Gives back the \e id of the item. |
1802 | 1812 |
/// |
1803 | 1813 |
/// Gives back the immutable and unique \e id of the item. |
1804 | 1814 |
int operator[](const Item& item) const { return _graph->id(item);} |
1805 | 1815 |
|
1806 | 1816 |
/// \brief Gives back the item by its id. |
1807 | 1817 |
/// |
1808 | 1818 |
/// Gives back the item by its id. |
1809 | 1819 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
1810 | 1820 |
|
1811 | 1821 |
private: |
1812 | 1822 |
const Graph* _graph; |
1813 | 1823 |
|
1814 | 1824 |
public: |
1815 | 1825 |
|
1816 | 1826 |
/// \brief The class represents the inverse of its owner (IdMap). |
1817 | 1827 |
/// |
1818 | 1828 |
/// The class represents the inverse of its owner (IdMap). |
1819 | 1829 |
/// \see inverse() |
1820 | 1830 |
class InverseMap { |
1821 | 1831 |
public: |
1822 | 1832 |
|
1823 | 1833 |
/// \brief Constructor. |
1824 | 1834 |
/// |
1825 | 1835 |
/// Constructor for creating an id-to-item map. |
1826 | 1836 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
1827 | 1837 |
|
1828 | 1838 |
/// \brief Constructor. |
1829 | 1839 |
/// |
1830 | 1840 |
/// Constructor for creating an id-to-item map. |
1831 | 1841 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
1832 | 1842 |
|
1833 | 1843 |
/// \brief Gives back the given item from its id. |
1834 | 1844 |
/// |
1835 | 1845 |
/// Gives back the given item from its id. |
1836 | 1846 |
/// |
1837 | 1847 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
1838 | 1848 |
|
1839 | 1849 |
private: |
1840 | 1850 |
const Graph* _graph; |
1841 | 1851 |
}; |
1842 | 1852 |
|
1843 | 1853 |
/// \brief Gives back the inverse of the map. |
1844 | 1854 |
/// |
1845 | 1855 |
/// Gives back the inverse of the IdMap. |
1846 | 1856 |
InverseMap inverse() const { return InverseMap(*_graph);} |
1847 | 1857 |
|
1848 | 1858 |
}; |
1849 | 1859 |
|
1850 | 1860 |
|
1851 | 1861 |
/// \brief General invertable graph-map type. |
1852 | 1862 |
|
1853 | 1863 |
/// This type provides simple invertable graph-maps. |
1854 | 1864 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
1855 | 1865 |
/// and if a key is set to a new value then store it |
1856 | 1866 |
/// in the inverse map. |
1857 | 1867 |
/// |
1858 | 1868 |
/// The values of the map can be accessed |
1859 | 1869 |
/// with stl compatible forward iterator. |
1860 | 1870 |
/// |
1861 | 1871 |
/// \tparam _Graph The graph type. |
1862 | 1872 |
/// \tparam _Item The item type of the graph. |
1863 | 1873 |
/// \tparam _Value The value type of the map. |
1864 | 1874 |
/// |
1865 | 1875 |
/// \see IterableValueMap |
1866 | 1876 |
template <typename _Graph, typename _Item, typename _Value> |
1867 | 1877 |
class InvertableMap |
1868 | 1878 |
: protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type { |
1869 | 1879 |
private: |
1870 | 1880 |
|
1871 | 1881 |
typedef typename ItemSetTraits<_Graph, _Item>:: |
1872 | 1882 |
template Map<_Value>::Type Map; |
1873 | 1883 |
typedef _Graph Graph; |
1874 | 1884 |
|
1875 | 1885 |
typedef std::map<_Value, _Item> Container; |
1876 | 1886 |
Container _inv_map; |
1877 | 1887 |
|
1878 | 1888 |
public: |
1879 | 1889 |
|
1880 | 1890 |
/// The key type of InvertableMap (Node, Arc, Edge). |
1881 | 1891 |
typedef typename Map::Key Key; |
1882 | 1892 |
/// The value type of the InvertableMap. |
1883 | 1893 |
typedef typename Map::Value Value; |
1884 | 1894 |
|
1885 |
|
|
1886 |
|
|
1887 | 1895 |
/// \brief Constructor. |
1888 | 1896 |
/// |
1889 | 1897 |
/// Construct a new InvertableMap for the graph. |
1890 | 1898 |
/// |
1891 | 1899 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
1892 | 1900 |
|
1893 | 1901 |
/// \brief Forward iterator for values. |
1894 | 1902 |
/// |
1895 | 1903 |
/// This iterator is an stl compatible forward |
1896 | 1904 |
/// iterator on the values of the map. The values can |
1897 | 1905 |
/// be accessed in the [beginValue, endValue) range. |
1898 | 1906 |
/// |
1899 | 1907 |
class ValueIterator |
1900 | 1908 |
: public std::iterator<std::forward_iterator_tag, Value> { |
1901 | 1909 |
friend class InvertableMap; |
1902 | 1910 |
private: |
1903 | 1911 |
ValueIterator(typename Container::const_iterator _it) |
1904 | 1912 |
: it(_it) {} |
1905 | 1913 |
public: |
1906 | 1914 |
|
1907 | 1915 |
ValueIterator() {} |
1908 | 1916 |
|
1909 | 1917 |
ValueIterator& operator++() { ++it; return *this; } |
1910 | 1918 |
ValueIterator operator++(int) { |
1911 | 1919 |
ValueIterator tmp(*this); |
1912 | 1920 |
operator++(); |
1913 | 1921 |
return tmp; |
1914 | 1922 |
} |
1915 | 1923 |
|
1916 | 1924 |
const Value& operator*() const { return it->first; } |
1917 | 1925 |
const Value* operator->() const { return &(it->first); } |
1918 | 1926 |
|
1919 | 1927 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
1920 | 1928 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
1921 | 1929 |
|
1922 | 1930 |
private: |
1923 | 1931 |
typename Container::const_iterator it; |
1924 | 1932 |
}; |
1925 | 1933 |
|
1926 | 1934 |
/// \brief Returns an iterator to the first value. |
1927 | 1935 |
/// |
1928 | 1936 |
/// Returns an stl compatible iterator to the |
1929 | 1937 |
/// first value of the map. The values of the |
1930 | 1938 |
/// map can be accessed in the [beginValue, endValue) |
1931 | 1939 |
/// range. |
1932 | 1940 |
ValueIterator beginValue() const { |
1933 | 1941 |
return ValueIterator(_inv_map.begin()); |
1934 | 1942 |
} |
1935 | 1943 |
|
1936 | 1944 |
/// \brief Returns an iterator after the last value. |
1937 | 1945 |
/// |
1938 | 1946 |
/// Returns an stl compatible iterator after the |
1939 | 1947 |
/// last value of the map. The values of the |
1940 | 1948 |
/// map can be accessed in the [beginValue, endValue) |
1941 | 1949 |
/// range. |
1942 | 1950 |
ValueIterator endValue() const { |
1943 | 1951 |
return ValueIterator(_inv_map.end()); |
1944 | 1952 |
} |
1945 | 1953 |
|
1946 | 1954 |
/// \brief The setter function of the map. |
1947 | 1955 |
/// |
1948 | 1956 |
/// Sets the mapped value. |
1949 | 1957 |
void set(const Key& key, const Value& val) { |
1950 | 1958 |
Value oldval = Map::operator[](key); |
1951 | 1959 |
typename Container::iterator it = _inv_map.find(oldval); |
1952 | 1960 |
if (it != _inv_map.end() && it->second == key) { |
1953 | 1961 |
_inv_map.erase(it); |
1954 | 1962 |
} |
1955 | 1963 |
_inv_map.insert(make_pair(val, key)); |
1956 | 1964 |
Map::set(key, val); |
1957 | 1965 |
} |
1958 | 1966 |
|
1959 | 1967 |
/// \brief The getter function of the map. |
1960 | 1968 |
/// |
1961 | 1969 |
/// It gives back the value associated with the key. |
1962 | 1970 |
typename MapTraits<Map>::ConstReturnValue |
1963 | 1971 |
operator[](const Key& key) const { |
1964 | 1972 |
return Map::operator[](key); |
1965 | 1973 |
} |
1966 | 1974 |
|
1967 | 1975 |
/// \brief Gives back the item by its value. |
1968 | 1976 |
/// |
1969 | 1977 |
/// Gives back the item by its value. |
1970 | 1978 |
Key operator()(const Value& key) const { |
1971 | 1979 |
typename Container::const_iterator it = _inv_map.find(key); |
1972 | 1980 |
return it != _inv_map.end() ? it->second : INVALID; |
1973 | 1981 |
} |
1974 | 1982 |
|
1975 | 1983 |
protected: |
1976 | 1984 |
|
1977 | 1985 |
/// \brief Erase the key from the map. |
1978 | 1986 |
/// |
1979 | 1987 |
/// Erase the key to the map. It is called by the |
1980 | 1988 |
/// \c AlterationNotifier. |
1981 | 1989 |
virtual void erase(const Key& key) { |
1982 | 1990 |
Value val = Map::operator[](key); |
1983 | 1991 |
typename Container::iterator it = _inv_map.find(val); |
1984 | 1992 |
if (it != _inv_map.end() && it->second == key) { |
1985 | 1993 |
_inv_map.erase(it); |
1986 | 1994 |
} |
1987 | 1995 |
Map::erase(key); |
1988 | 1996 |
} |
1989 | 1997 |
|
1990 | 1998 |
/// \brief Erase more keys from the map. |
1991 | 1999 |
/// |
1992 | 2000 |
/// Erase more keys from the map. It is called by the |
1993 | 2001 |
/// \c AlterationNotifier. |
1994 | 2002 |
virtual void erase(const std::vector<Key>& keys) { |
1995 | 2003 |
for (int i = 0; i < int(keys.size()); ++i) { |
1996 | 2004 |
Value val = Map::operator[](keys[i]); |
1997 | 2005 |
typename Container::iterator it = _inv_map.find(val); |
1998 | 2006 |
if (it != _inv_map.end() && it->second == keys[i]) { |
1999 | 2007 |
_inv_map.erase(it); |
2000 | 2008 |
} |
2001 | 2009 |
} |
2002 | 2010 |
Map::erase(keys); |
2003 | 2011 |
} |
2004 | 2012 |
|
2005 | 2013 |
/// \brief Clear the keys from the map and inverse map. |
2006 | 2014 |
/// |
2007 | 2015 |
/// Clear the keys from the map and inverse map. It is called by the |
2008 | 2016 |
/// \c AlterationNotifier. |
2009 | 2017 |
virtual void clear() { |
2010 | 2018 |
_inv_map.clear(); |
2011 | 2019 |
Map::clear(); |
2012 | 2020 |
} |
2013 | 2021 |
|
2014 | 2022 |
public: |
2015 | 2023 |
|
2016 | 2024 |
/// \brief The inverse map type. |
2017 | 2025 |
/// |
2018 | 2026 |
/// The inverse of this map. The subscript operator of the map |
2019 | 2027 |
/// gives back always the item what was last assigned to the value. |
2020 | 2028 |
class InverseMap { |
2021 | 2029 |
public: |
2022 | 2030 |
/// \brief Constructor of the InverseMap. |
2023 | 2031 |
/// |
2024 | 2032 |
/// Constructor of the InverseMap. |
2025 | 2033 |
explicit InverseMap(const InvertableMap& inverted) |
2026 | 2034 |
: _inverted(inverted) {} |
2027 | 2035 |
|
2028 | 2036 |
/// The value type of the InverseMap. |
2029 | 2037 |
typedef typename InvertableMap::Key Value; |
2030 | 2038 |
/// The key type of the InverseMap. |
2031 | 2039 |
typedef typename InvertableMap::Value Key; |
2032 | 2040 |
|
2033 | 2041 |
/// \brief Subscript operator. |
2034 | 2042 |
/// |
2035 | 2043 |
/// Subscript operator. It gives back always the item |
2036 | 2044 |
/// what was last assigned to the value. |
2037 | 2045 |
Value operator[](const Key& key) const { |
2038 | 2046 |
return _inverted(key); |
2039 | 2047 |
} |
2040 | 2048 |
|
2041 | 2049 |
private: |
2042 | 2050 |
const InvertableMap& _inverted; |
2043 | 2051 |
}; |
2044 | 2052 |
|
2045 | 2053 |
/// \brief It gives back the just readable inverse map. |
2046 | 2054 |
/// |
2047 | 2055 |
/// It gives back the just readable inverse map. |
2048 | 2056 |
InverseMap inverse() const { |
2049 | 2057 |
return InverseMap(*this); |
2050 | 2058 |
} |
2051 | 2059 |
|
2052 |
|
|
2053 |
|
|
2054 | 2060 |
}; |
2055 | 2061 |
|
2056 | 2062 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
2057 | 2063 |
/// item in the graph. |
2058 | 2064 |
/// |
2059 | 2065 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
2060 | 2066 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
2061 | 2067 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
2062 | 2068 |
/// different ids <li>\b continuous: the range of the ids is the set of |
2063 | 2069 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
2064 | 2070 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
2065 | 2071 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
2066 | 2072 |
/// with its member class \c InverseMap, or with the \c operator() member. |
2067 | 2073 |
/// |
2068 | 2074 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
2069 | 2075 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
2070 | 2076 |
/// Edge. |
2071 | 2077 |
template <typename _Graph, typename _Item> |
2072 | 2078 |
class DescriptorMap |
2073 | 2079 |
: protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type { |
2074 | 2080 |
|
2075 | 2081 |
typedef _Item Item; |
2076 | 2082 |
typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map; |
2077 | 2083 |
|
2078 | 2084 |
public: |
2079 | 2085 |
/// The graph class of DescriptorMap. |
2080 | 2086 |
typedef _Graph Graph; |
2081 | 2087 |
|
2082 | 2088 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
2083 | 2089 |
typedef typename Map::Key Key; |
2084 | 2090 |
/// The value type of DescriptorMap. |
2085 | 2091 |
typedef typename Map::Value Value; |
2086 | 2092 |
|
2087 | 2093 |
/// \brief Constructor. |
2088 | 2094 |
/// |
2089 | 2095 |
/// Constructor for descriptor map. |
2090 | 2096 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
2091 | 2097 |
Item it; |
2092 | 2098 |
const typename Map::Notifier* nf = Map::notifier(); |
2093 | 2099 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2094 | 2100 |
Map::set(it, _inv_map.size()); |
2095 | 2101 |
_inv_map.push_back(it); |
2096 | 2102 |
} |
2097 | 2103 |
} |
2098 | 2104 |
|
2099 | 2105 |
protected: |
2100 | 2106 |
|
2101 | 2107 |
/// \brief Add a new key to the map. |
2102 | 2108 |
/// |
2103 | 2109 |
/// Add a new key to the map. It is called by the |
2104 | 2110 |
/// \c AlterationNotifier. |
2105 | 2111 |
virtual void add(const Item& item) { |
2106 | 2112 |
Map::add(item); |
2107 | 2113 |
Map::set(item, _inv_map.size()); |
2108 | 2114 |
_inv_map.push_back(item); |
2109 | 2115 |
} |
2110 | 2116 |
|
2111 | 2117 |
/// \brief Add more new keys to the map. |
2112 | 2118 |
/// |
2113 | 2119 |
/// Add more new keys to the map. It is called by the |
2114 | 2120 |
/// \c AlterationNotifier. |
2115 | 2121 |
virtual void add(const std::vector<Item>& items) { |
2116 | 2122 |
Map::add(items); |
2117 | 2123 |
for (int i = 0; i < int(items.size()); ++i) { |
2118 | 2124 |
Map::set(items[i], _inv_map.size()); |
2119 | 2125 |
_inv_map.push_back(items[i]); |
2120 | 2126 |
} |
2121 | 2127 |
} |
2122 | 2128 |
|
2123 | 2129 |
/// \brief Erase the key from the map. |
2124 | 2130 |
/// |
2125 | 2131 |
/// Erase the key from the map. It is called by the |
2126 | 2132 |
/// \c AlterationNotifier. |
2127 | 2133 |
virtual void erase(const Item& item) { |
2128 | 2134 |
Map::set(_inv_map.back(), Map::operator[](item)); |
2129 | 2135 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
2130 | 2136 |
_inv_map.pop_back(); |
2131 | 2137 |
Map::erase(item); |
2132 | 2138 |
} |
2133 | 2139 |
|
2134 | 2140 |
/// \brief Erase more keys from the map. |
2135 | 2141 |
/// |
2136 | 2142 |
/// Erase more keys from the map. It is called by the |
2137 | 2143 |
/// \c AlterationNotifier. |
2138 | 2144 |
virtual void erase(const std::vector<Item>& items) { |
2139 | 2145 |
for (int i = 0; i < int(items.size()); ++i) { |
2140 | 2146 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
2141 | 2147 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
2142 | 2148 |
_inv_map.pop_back(); |
2143 | 2149 |
} |
2144 | 2150 |
Map::erase(items); |
2145 | 2151 |
} |
2146 | 2152 |
|
2147 | 2153 |
/// \brief Build the unique map. |
2148 | 2154 |
/// |
2149 | 2155 |
/// Build the unique map. It is called by the |
2150 | 2156 |
/// \c AlterationNotifier. |
2151 | 2157 |
virtual void build() { |
2152 | 2158 |
Map::build(); |
2153 | 2159 |
Item it; |
2154 | 2160 |
const typename Map::Notifier* nf = Map::notifier(); |
2155 | 2161 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2156 | 2162 |
Map::set(it, _inv_map.size()); |
2157 | 2163 |
_inv_map.push_back(it); |
2158 | 2164 |
} |
2159 | 2165 |
} |
2160 | 2166 |
|
2161 | 2167 |
/// \brief Clear the keys from the map. |
2162 | 2168 |
/// |
2163 | 2169 |
/// Clear the keys from the map. It is called by the |
2164 | 2170 |
/// \c AlterationNotifier. |
2165 | 2171 |
virtual void clear() { |
2166 | 2172 |
_inv_map.clear(); |
2167 | 2173 |
Map::clear(); |
2168 | 2174 |
} |
2169 | 2175 |
|
2170 | 2176 |
public: |
2171 | 2177 |
|
2172 | 2178 |
/// \brief Returns the maximal value plus one. |
2173 | 2179 |
/// |
2174 | 2180 |
/// Returns the maximal value plus one in the map. |
2175 | 2181 |
unsigned int size() const { |
2176 | 2182 |
return _inv_map.size(); |
2177 | 2183 |
} |
2178 | 2184 |
|
2179 | 2185 |
/// \brief Swaps the position of the two items in the map. |
2180 | 2186 |
/// |
2181 | 2187 |
/// Swaps the position of the two items in the map. |
2182 | 2188 |
void swap(const Item& p, const Item& q) { |
2183 | 2189 |
int pi = Map::operator[](p); |
2184 | 2190 |
int qi = Map::operator[](q); |
2185 | 2191 |
Map::set(p, qi); |
2186 | 2192 |
_inv_map[qi] = p; |
2187 | 2193 |
Map::set(q, pi); |
2188 | 2194 |
_inv_map[pi] = q; |
2189 | 2195 |
} |
2190 | 2196 |
|
2191 | 2197 |
/// \brief Gives back the \e descriptor of the item. |
2192 | 2198 |
/// |
2193 | 2199 |
/// Gives back the mutable and unique \e descriptor of the map. |
2194 | 2200 |
int operator[](const Item& item) const { |
2195 | 2201 |
return Map::operator[](item); |
2196 | 2202 |
} |
2197 | 2203 |
|
2198 | 2204 |
/// \brief Gives back the item by its descriptor. |
2199 | 2205 |
/// |
2200 | 2206 |
/// Gives back th item by its descriptor. |
2201 | 2207 |
Item operator()(int id) const { |
2202 | 2208 |
return _inv_map[id]; |
2203 | 2209 |
} |
2204 | 2210 |
|
2205 | 2211 |
private: |
2206 | 2212 |
|
2207 | 2213 |
typedef std::vector<Item> Container; |
2208 | 2214 |
Container _inv_map; |
2209 | 2215 |
|
2210 | 2216 |
public: |
2211 | 2217 |
/// \brief The inverse map type of DescriptorMap. |
2212 | 2218 |
/// |
2213 | 2219 |
/// The inverse map type of DescriptorMap. |
2214 | 2220 |
class InverseMap { |
2215 | 2221 |
public: |
2216 | 2222 |
/// \brief Constructor of the InverseMap. |
2217 | 2223 |
/// |
2218 | 2224 |
/// Constructor of the InverseMap. |
2219 | 2225 |
explicit InverseMap(const DescriptorMap& inverted) |
2220 | 2226 |
: _inverted(inverted) {} |
2221 | 2227 |
|
2222 | 2228 |
|
2223 | 2229 |
/// The value type of the InverseMap. |
2224 | 2230 |
typedef typename DescriptorMap::Key Value; |
2225 | 2231 |
/// The key type of the InverseMap. |
2226 | 2232 |
typedef typename DescriptorMap::Value Key; |
2227 | 2233 |
|
2228 | 2234 |
/// \brief Subscript operator. |
2229 | 2235 |
/// |
2230 | 2236 |
/// Subscript operator. It gives back the item |
2231 | 2237 |
/// that the descriptor belongs to currently. |
2232 | 2238 |
Value operator[](const Key& key) const { |
2233 | 2239 |
return _inverted(key); |
2234 | 2240 |
} |
2235 | 2241 |
|
2236 | 2242 |
/// \brief Size of the map. |
2237 | 2243 |
/// |
2238 | 2244 |
/// Returns the size of the map. |
2239 | 2245 |
unsigned int size() const { |
2240 | 2246 |
return _inverted.size(); |
2241 | 2247 |
} |
2242 | 2248 |
|
2243 | 2249 |
private: |
2244 | 2250 |
const DescriptorMap& _inverted; |
2245 | 2251 |
}; |
2246 | 2252 |
|
2247 | 2253 |
/// \brief Gives back the inverse of the map. |
2248 | 2254 |
/// |
2249 | 2255 |
/// Gives back the inverse of the map. |
2250 | 2256 |
const InverseMap inverse() const { |
2251 | 2257 |
return InverseMap(*this); |
2252 | 2258 |
} |
2253 | 2259 |
}; |
2254 | 2260 |
|
2255 | 2261 |
/// \brief Returns the source of the given arc. |
2256 | 2262 |
/// |
2257 | 2263 |
/// The SourceMap gives back the source Node of the given arc. |
2258 | 2264 |
/// \see TargetMap |
2259 | 2265 |
template <typename Digraph> |
2260 | 2266 |
class SourceMap { |
2261 | 2267 |
public: |
2262 | 2268 |
|
2263 | 2269 |
typedef typename Digraph::Node Value; |
2264 | 2270 |
typedef typename Digraph::Arc Key; |
2265 | 2271 |
|
2266 | 2272 |
/// \brief Constructor |
2267 | 2273 |
/// |
2268 | 2274 |
/// Constructor |
2269 | 2275 |
/// \param digraph The digraph that the map belongs to. |
2270 | 2276 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
2271 | 2277 |
|
2272 | 2278 |
/// \brief The subscript operator. |
2273 | 2279 |
/// |
2274 | 2280 |
/// The subscript operator. |
2275 | 2281 |
/// \param arc The arc |
2276 | 2282 |
/// \return The source of the arc |
2277 | 2283 |
Value operator[](const Key& arc) const { |
2278 | 2284 |
return _digraph.source(arc); |
2279 | 2285 |
} |
2280 | 2286 |
|
2281 | 2287 |
private: |
2282 | 2288 |
const Digraph& _digraph; |
2283 | 2289 |
}; |
2284 | 2290 |
|
2285 | 2291 |
/// \brief Returns a \c SourceMap class. |
2286 | 2292 |
/// |
2287 | 2293 |
/// This function just returns an \c SourceMap class. |
2288 | 2294 |
/// \relates SourceMap |
2289 | 2295 |
template <typename Digraph> |
2290 | 2296 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
2291 | 2297 |
return SourceMap<Digraph>(digraph); |
2292 | 2298 |
} |
2293 | 2299 |
|
2294 | 2300 |
/// \brief Returns the target of the given arc. |
2295 | 2301 |
/// |
2296 | 2302 |
/// The TargetMap gives back the target Node of the given arc. |
2297 | 2303 |
/// \see SourceMap |
2298 | 2304 |
template <typename Digraph> |
2299 | 2305 |
class TargetMap { |
2300 | 2306 |
public: |
2301 | 2307 |
|
2302 | 2308 |
typedef typename Digraph::Node Value; |
2303 | 2309 |
typedef typename Digraph::Arc Key; |
2304 | 2310 |
|
2305 | 2311 |
/// \brief Constructor |
2306 | 2312 |
/// |
2307 | 2313 |
/// Constructor |
2308 | 2314 |
/// \param digraph The digraph that the map belongs to. |
2309 | 2315 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
2310 | 2316 |
|
2311 | 2317 |
/// \brief The subscript operator. |
2312 | 2318 |
/// |
2313 | 2319 |
/// The subscript operator. |
2314 | 2320 |
/// \param e The arc |
2315 | 2321 |
/// \return The target of the arc |
2316 | 2322 |
Value operator[](const Key& e) const { |
2317 | 2323 |
return _digraph.target(e); |
2318 | 2324 |
} |
2319 | 2325 |
|
2320 | 2326 |
private: |
2321 | 2327 |
const Digraph& _digraph; |
2322 | 2328 |
}; |
2323 | 2329 |
|
2324 | 2330 |
/// \brief Returns a \c TargetMap class. |
2325 | 2331 |
/// |
2326 | 2332 |
/// This function just returns a \c TargetMap class. |
2327 | 2333 |
/// \relates TargetMap |
2328 | 2334 |
template <typename Digraph> |
2329 | 2335 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
2330 | 2336 |
return TargetMap<Digraph>(digraph); |
2331 | 2337 |
} |
2332 | 2338 |
|
2333 | 2339 |
/// \brief Returns the "forward" directed arc view of an edge. |
2334 | 2340 |
/// |
2335 | 2341 |
/// Returns the "forward" directed arc view of an edge. |
2336 | 2342 |
/// \see BackwardMap |
2337 | 2343 |
template <typename Graph> |
2338 | 2344 |
class ForwardMap { |
2339 | 2345 |
public: |
2340 | 2346 |
|
2341 | 2347 |
typedef typename Graph::Arc Value; |
2342 | 2348 |
typedef typename Graph::Edge Key; |
2343 | 2349 |
|
2344 | 2350 |
/// \brief Constructor |
2345 | 2351 |
/// |
2346 | 2352 |
/// Constructor |
2347 | 2353 |
/// \param graph The graph that the map belongs to. |
2348 | 2354 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
2349 | 2355 |
|
2350 | 2356 |
/// \brief The subscript operator. |
2351 | 2357 |
/// |
2352 | 2358 |
/// The subscript operator. |
2353 | 2359 |
/// \param key An edge |
2354 | 2360 |
/// \return The "forward" directed arc view of edge |
2355 | 2361 |
Value operator[](const Key& key) const { |
2356 | 2362 |
return _graph.direct(key, true); |
2357 | 2363 |
} |
2358 | 2364 |
|
2359 | 2365 |
private: |
2360 | 2366 |
const Graph& _graph; |
2361 | 2367 |
}; |
2362 | 2368 |
|
2363 | 2369 |
/// \brief Returns a \c ForwardMap class. |
2364 | 2370 |
/// |
2365 | 2371 |
/// This function just returns an \c ForwardMap class. |
2366 | 2372 |
/// \relates ForwardMap |
2367 | 2373 |
template <typename Graph> |
2368 | 2374 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
2369 | 2375 |
return ForwardMap<Graph>(graph); |
2370 | 2376 |
} |
2371 | 2377 |
|
2372 | 2378 |
/// \brief Returns the "backward" directed arc view of an edge. |
2373 | 2379 |
/// |
2374 | 2380 |
/// Returns the "backward" directed arc view of an edge. |
2375 | 2381 |
/// \see ForwardMap |
2376 | 2382 |
template <typename Graph> |
2377 | 2383 |
class BackwardMap { |
2378 | 2384 |
public: |
2379 | 2385 |
|
2380 | 2386 |
typedef typename Graph::Arc Value; |
2381 | 2387 |
typedef typename Graph::Edge Key; |
2382 | 2388 |
|
2383 | 2389 |
/// \brief Constructor |
2384 | 2390 |
/// |
2385 | 2391 |
/// Constructor |
2386 | 2392 |
/// \param graph The graph that the map belongs to. |
2387 | 2393 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
2388 | 2394 |
|
2389 | 2395 |
/// \brief The subscript operator. |
2390 | 2396 |
/// |
2391 | 2397 |
/// The subscript operator. |
2392 | 2398 |
/// \param key An edge |
2393 | 2399 |
/// \return The "backward" directed arc view of edge |
2394 | 2400 |
Value operator[](const Key& key) const { |
2395 | 2401 |
return _graph.direct(key, false); |
2396 | 2402 |
} |
2397 | 2403 |
|
2398 | 2404 |
private: |
2399 | 2405 |
const Graph& _graph; |
2400 | 2406 |
}; |
2401 | 2407 |
|
2402 | 2408 |
/// \brief Returns a \c BackwardMap class |
2403 | 2409 |
|
2404 | 2410 |
/// This function just returns a \c BackwardMap class. |
2405 | 2411 |
/// \relates BackwardMap |
2406 | 2412 |
template <typename Graph> |
2407 | 2413 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
2408 | 2414 |
return BackwardMap<Graph>(graph); |
2409 | 2415 |
} |
2410 | 2416 |
|
2411 | 2417 |
/// \brief Potential difference map |
2412 | 2418 |
/// |
2413 | 2419 |
/// If there is an potential map on the nodes then we |
2414 | 2420 |
/// can get an arc map as we get the substraction of the |
2415 | 2421 |
/// values of the target and source. |
2416 | 2422 |
template <typename Digraph, typename NodeMap> |
2417 | 2423 |
class PotentialDifferenceMap { |
2418 | 2424 |
public: |
2419 | 2425 |
typedef typename Digraph::Arc Key; |
2420 | 2426 |
typedef typename NodeMap::Value Value; |
2421 | 2427 |
|
2422 | 2428 |
/// \brief Constructor |
2423 | 2429 |
/// |
2424 | 2430 |
/// Contructor of the map |
2425 | 2431 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
2426 | 2432 |
const NodeMap& potential) |
2427 | 2433 |
: _digraph(digraph), _potential(potential) {} |
2428 | 2434 |
|
2429 | 2435 |
/// \brief Const subscription operator |
2430 | 2436 |
/// |
2431 | 2437 |
/// Const subscription operator |
2432 | 2438 |
Value operator[](const Key& arc) const { |
2433 | 2439 |
return _potential[_digraph.target(arc)] - |
2434 | 2440 |
_potential[_digraph.source(arc)]; |
2435 | 2441 |
} |
2436 | 2442 |
|
2437 | 2443 |
private: |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_TIME_MEASURE_H |
20 | 20 |
#define LEMON_TIME_MEASURE_H |
21 | 21 |
|
22 | 22 |
///\ingroup timecount |
23 | 23 |
///\file |
24 | 24 |
///\brief Tools for measuring cpu usage |
25 | 25 |
|
26 | 26 |
#ifdef WIN32 |
27 | 27 |
#define WIN32_LEAN_AND_MEAN |
28 | 28 |
#define NOMINMAX |
29 | 29 |
#include <windows.h> |
30 | 30 |
#include <cmath> |
31 | 31 |
#else |
32 | 32 |
#include <sys/times.h> |
33 | 33 |
#include <sys/time.h> |
34 | 34 |
#endif |
35 | 35 |
|
36 | 36 |
#include <string> |
37 | 37 |
#include <fstream> |
38 | 38 |
#include <iostream> |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup timecount |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
/// A class to store (cpu)time instances. |
46 | 46 |
|
47 | 47 |
/// This class stores five time values. |
48 | 48 |
/// - a real time |
49 | 49 |
/// - a user cpu time |
50 | 50 |
/// - a system cpu time |
51 | 51 |
/// - a user cpu time of children |
52 | 52 |
/// - a system cpu time of children |
53 | 53 |
/// |
54 | 54 |
/// TimeStamp's can be added to or substracted from each other and |
55 | 55 |
/// they can be pushed to a stream. |
56 | 56 |
/// |
57 | 57 |
/// In most cases, perhaps the \ref Timer or the \ref TimeReport |
58 | 58 |
/// class is what you want to use instead. |
59 | 59 |
|
60 | 60 |
class TimeStamp |
61 | 61 |
{ |
62 | 62 |
double utime; |
63 | 63 |
double stime; |
64 | 64 |
double cutime; |
65 | 65 |
double cstime; |
66 | 66 |
double rtime; |
67 | 67 |
|
68 | 68 |
void _reset() { |
69 | 69 |
utime = stime = cutime = cstime = rtime = 0; |
70 | 70 |
} |
71 | 71 |
|
72 | 72 |
public: |
73 | 73 |
|
74 | 74 |
///Read the current time values of the process |
75 | 75 |
void stamp() |
76 | 76 |
{ |
77 | 77 |
#ifndef WIN32 |
78 | 78 |
timeval tv; |
79 | 79 |
gettimeofday(&tv, 0); |
80 | 80 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
81 | 81 |
|
82 | 82 |
tms ts; |
83 | 83 |
double tck=sysconf(_SC_CLK_TCK); |
84 | 84 |
times(&ts); |
85 | 85 |
utime=ts.tms_utime/tck; |
86 | 86 |
stime=ts.tms_stime/tck; |
87 | 87 |
cutime=ts.tms_cutime/tck; |
88 | 88 |
cstime=ts.tms_cstime/tck; |
89 | 89 |
#else |
90 | 90 |
static const double ch = 4294967296.0e-7; |
91 | 91 |
static const double cl = 1.0e-7; |
92 | 92 |
|
93 | 93 |
FILETIME system; |
94 | 94 |
GetSystemTimeAsFileTime(&system); |
95 | 95 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
96 | 96 |
|
97 | 97 |
FILETIME create, exit, kernel, user; |
98 | 98 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
99 | 99 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
100 | 100 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
101 | 101 |
cutime = 0; |
102 | 102 |
cstime = 0; |
103 | 103 |
} else { |
104 | 104 |
rtime = 0; |
105 | 105 |
utime = 0; |
106 | 106 |
stime = 0; |
107 | 107 |
cutime = 0; |
108 | 108 |
cstime = 0; |
109 | 109 |
} |
110 | 110 |
#endif |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
/// Constructor initializing with zero |
114 | 114 |
TimeStamp() |
115 | 115 |
{ _reset(); } |
116 | 116 |
///Constructor initializing with the current time values of the process |
117 | 117 |
TimeStamp(void *) { stamp();} |
118 | 118 |
|
119 | 119 |
///Set every time value to zero |
120 | 120 |
TimeStamp &reset() {_reset();return *this;} |
121 | 121 |
|
122 | 122 |
///\e |
123 | 123 |
TimeStamp &operator+=(const TimeStamp &b) |
124 | 124 |
{ |
125 | 125 |
utime+=b.utime; |
126 | 126 |
stime+=b.stime; |
127 | 127 |
cutime+=b.cutime; |
128 | 128 |
cstime+=b.cstime; |
129 | 129 |
rtime+=b.rtime; |
130 | 130 |
return *this; |
131 | 131 |
} |
132 | 132 |
///\e |
133 | 133 |
TimeStamp operator+(const TimeStamp &b) const |
134 | 134 |
{ |
135 | 135 |
TimeStamp t(*this); |
136 | 136 |
return t+=b; |
137 | 137 |
} |
138 | 138 |
///\e |
139 | 139 |
TimeStamp &operator-=(const TimeStamp &b) |
140 | 140 |
{ |
141 | 141 |
utime-=b.utime; |
142 | 142 |
stime-=b.stime; |
143 | 143 |
cutime-=b.cutime; |
144 | 144 |
cstime-=b.cstime; |
145 | 145 |
rtime-=b.rtime; |
146 | 146 |
return *this; |
147 | 147 |
} |
148 | 148 |
///\e |
149 | 149 |
TimeStamp operator-(const TimeStamp &b) const |
150 | 150 |
{ |
151 | 151 |
TimeStamp t(*this); |
152 | 152 |
return t-=b; |
153 | 153 |
} |
154 | 154 |
///\e |
155 | 155 |
TimeStamp &operator*=(double b) |
156 | 156 |
{ |
157 | 157 |
utime*=b; |
158 | 158 |
stime*=b; |
159 | 159 |
cutime*=b; |
160 | 160 |
cstime*=b; |
161 | 161 |
rtime*=b; |
162 | 162 |
return *this; |
163 | 163 |
} |
164 | 164 |
///\e |
165 | 165 |
TimeStamp operator*(double b) const |
166 | 166 |
{ |
167 | 167 |
TimeStamp t(*this); |
168 | 168 |
return t*=b; |
169 | 169 |
} |
170 | 170 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
171 | 171 |
///\e |
172 | 172 |
TimeStamp &operator/=(double b) |
173 | 173 |
{ |
174 | 174 |
utime/=b; |
175 | 175 |
stime/=b; |
176 | 176 |
cutime/=b; |
177 | 177 |
cstime/=b; |
178 | 178 |
rtime/=b; |
179 | 179 |
return *this; |
180 | 180 |
} |
181 | 181 |
///\e |
182 | 182 |
TimeStamp operator/(double b) const |
183 | 183 |
{ |
184 | 184 |
TimeStamp t(*this); |
185 | 185 |
return t/=b; |
186 | 186 |
} |
187 | 187 |
///The time ellapsed since the last call of stamp() |
188 | 188 |
TimeStamp ellapsed() const |
189 | 189 |
{ |
190 | 190 |
TimeStamp t(NULL); |
191 | 191 |
return t-*this; |
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
195 | 195 |
|
196 | 196 |
///Gives back the user time of the process |
197 | 197 |
double userTime() const |
198 | 198 |
{ |
199 | 199 |
return utime; |
200 | 200 |
} |
201 | 201 |
///Gives back the system time of the process |
202 | 202 |
double systemTime() const |
203 | 203 |
{ |
204 | 204 |
return stime; |
205 | 205 |
} |
206 | 206 |
///Gives back the user time of the process' children |
207 | 207 |
|
208 | 208 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
209 | 209 |
/// |
210 | 210 |
double cUserTime() const |
211 | 211 |
{ |
212 | 212 |
return cutime; |
213 | 213 |
} |
214 | 214 |
///Gives back the user time of the process' children |
215 | 215 |
|
216 | 216 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
217 | 217 |
/// |
218 | 218 |
double cSystemTime() const |
219 | 219 |
{ |
220 | 220 |
return cstime; |
221 | 221 |
} |
222 | 222 |
///Gives back the real time |
223 | 223 |
double realTime() const {return rtime;} |
224 | 224 |
}; |
225 | 225 |
|
226 | 226 |
TimeStamp operator*(double b,const TimeStamp &t) |
227 | 227 |
{ |
228 | 228 |
return t*b; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
///Prints the time counters |
232 | 232 |
|
233 | 233 |
///Prints the time counters in the following form: |
234 | 234 |
/// |
235 | 235 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
236 | 236 |
/// |
237 | 237 |
/// where the values are the |
238 | 238 |
/// \li \c u: user cpu time, |
239 | 239 |
/// \li \c s: system cpu time, |
240 | 240 |
/// \li \c cu: user cpu time of children, |
241 | 241 |
/// \li \c cs: system cpu time of children, |
242 | 242 |
/// \li \c real: real time. |
243 | 243 |
/// \relates TimeStamp |
244 | 244 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
245 | 245 |
/// calculated. |
246 | 246 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
247 | 247 |
{ |
248 | 248 |
os << "u: " << t.userTime() << |
249 | 249 |
"s, s: " << t.systemTime() << |
250 | 250 |
"s, cu: " << t.cUserTime() << |
251 | 251 |
"s, cs: " << t.cSystemTime() << |
252 | 252 |
"s, real: " << t.realTime() << "s"; |
253 | 253 |
return os; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
///Class for measuring the cpu time and real time usage of the process |
257 | 257 |
|
258 | 258 |
///Class for measuring the cpu time and real time usage of the process. |
259 | 259 |
///It is quite easy-to-use, here is a short example. |
260 | 260 |
///\code |
261 | 261 |
/// #include<lemon/time_measure.h> |
262 | 262 |
/// #include<iostream> |
263 | 263 |
/// |
264 | 264 |
/// int main() |
265 | 265 |
/// { |
266 | 266 |
/// |
267 | 267 |
/// ... |
268 | 268 |
/// |
269 | 269 |
/// Timer t; |
270 | 270 |
/// doSomething(); |
271 | 271 |
/// std::cout << t << '\n'; |
272 | 272 |
/// t.restart(); |
273 | 273 |
/// doSomethingElse(); |
274 | 274 |
/// std::cout << t << '\n'; |
275 | 275 |
/// |
276 | 276 |
/// ... |
277 | 277 |
/// |
278 | 278 |
/// } |
279 | 279 |
///\endcode |
280 | 280 |
/// |
281 | 281 |
///The \ref Timer can also be \ref stop() "stopped" and |
282 | 282 |
///\ref start() "started" again, so it is possible to compute collected |
283 | 283 |
///running times. |
284 | 284 |
/// |
285 | 285 |
///\warning Depending on the operation system and its actual configuration |
286 | 286 |
///the time counters have a certain (10ms on a typical Linux system) |
287 | 287 |
///granularity. |
288 | 288 |
///Therefore this tool is not appropriate to measure very short times. |
289 | 289 |
///Also, if you start and stop the timer very frequently, it could lead to |
290 | 290 |
///distorted results. |
291 | 291 |
/// |
292 | 292 |
///\note If you want to measure the running time of the execution of a certain |
293 | 293 |
///function, consider the usage of \ref TimeReport instead. |
294 | 294 |
/// |
295 | 295 |
///\sa TimeReport |
296 | 296 |
class Timer |
297 | 297 |
{ |
298 | 298 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
299 | 299 |
TimeStamp start_time; //This is the relativ start-time if the timer |
300 | 300 |
//is _running, the collected _running time otherwise. |
301 | 301 |
|
302 | 302 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
303 | 303 |
|
304 | 304 |
public: |
305 | 305 |
///Constructor. |
306 | 306 |
|
307 | 307 |
///\param run indicates whether or not the timer starts immediately. |
308 | 308 |
/// |
309 | 309 |
Timer(bool run=true) :_running(run) {_reset();} |
310 | 310 |
|
311 | 311 |
///\name Control the state of the timer |
312 | 312 |
///Basically a Timer can be either running or stopped, |
313 | 313 |
///but it provides a bit finer control on the execution. |
314 |
///The \ref lemon::Timer "Timer" also counts the number of |
|
315 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
|
314 |
///The \ref lemon::Timer "Timer" also counts the number of |
|
315 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
|
316 | 316 |
///only after the same amount (or more) \ref lemon::Timer::stop() |
317 | 317 |
///"stop()"s. This can be useful e.g. to compute the running time |
318 | 318 |
///of recursive functions. |
319 | 319 |
|
320 | 320 |
///@{ |
321 | 321 |
|
322 | 322 |
///Reset and stop the time counters |
323 | 323 |
|
324 | 324 |
///This function resets and stops the time counters |
325 | 325 |
///\sa restart() |
326 | 326 |
void reset() |
327 | 327 |
{ |
328 | 328 |
_running=0; |
329 | 329 |
_reset(); |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
///Start the time counters |
333 | 333 |
|
334 | 334 |
///This function starts the time counters. |
335 | 335 |
/// |
336 | 336 |
///If the timer is started more than ones, it will remain running |
337 | 337 |
///until the same amount of \ref stop() is called. |
338 | 338 |
///\sa stop() |
339 | 339 |
void start() |
340 | 340 |
{ |
341 | 341 |
if(_running) _running++; |
342 | 342 |
else { |
343 | 343 |
_running=1; |
344 | 344 |
TimeStamp t; |
345 | 345 |
t.stamp(); |
346 | 346 |
start_time=t-start_time; |
347 | 347 |
} |
348 | 348 |
} |
349 | 349 |
|
350 | 350 |
|
351 | 351 |
///Stop the time counters |
352 | 352 |
|
353 | 353 |
///This function stops the time counters. If start() was executed more than |
354 | 354 |
///once, then the same number of stop() execution is necessary the really |
355 | 355 |
///stop the timer. |
356 | 356 |
/// |
357 | 357 |
///\sa halt() |
358 | 358 |
///\sa start() |
359 | 359 |
///\sa restart() |
360 | 360 |
///\sa reset() |
361 | 361 |
|
362 | 362 |
void stop() |
363 | 363 |
{ |
364 | 364 |
if(_running && !--_running) { |
365 | 365 |
TimeStamp t; |
366 | 366 |
t.stamp(); |
367 | 367 |
start_time=t-start_time; |
368 | 368 |
} |
369 | 369 |
} |
370 | 370 |
|
371 | 371 |
///Halt (i.e stop immediately) the time counters |
372 | 372 |
|
373 | 373 |
///This function stops immediately the time counters, i.e. <tt>t.halt()</tt> |
374 | 374 |
///is a faster |
375 | 375 |
///equivalent of the following. |
376 | 376 |
///\code |
377 | 377 |
/// while(t.running()) t.stop() |
378 | 378 |
///\endcode |
379 | 379 |
/// |
380 | 380 |
/// |
381 | 381 |
///\sa stop() |
382 | 382 |
///\sa restart() |
383 | 383 |
///\sa reset() |
384 | 384 |
|
385 | 385 |
void halt() |
386 | 386 |
{ |
387 | 387 |
if(_running) { |
388 | 388 |
_running=0; |
389 | 389 |
TimeStamp t; |
390 | 390 |
t.stamp(); |
391 | 391 |
start_time=t-start_time; |
392 | 392 |
} |
393 | 393 |
} |
394 | 394 |
|
395 | 395 |
///Returns the running state of the timer |
396 | 396 |
|
397 | 397 |
///This function returns the number of stop() exections that is |
398 | 398 |
///necessary to really stop the timer. |
399 | 399 |
///For example the timer |
400 | 400 |
///is running if and only if the return value is \c true |
401 | 401 |
///(i.e. greater than |
402 | 402 |
///zero). |
403 | 403 |
int running() { return _running; } |
404 | 404 |
|
405 | 405 |
|
406 | 406 |
///Restart the time counters |
407 | 407 |
|
408 | 408 |
///This function is a shorthand for |
409 | 409 |
///a reset() and a start() calls. |
410 | 410 |
/// |
411 | 411 |
void restart() |
412 | 412 |
{ |
413 | 413 |
reset(); |
414 | 414 |
start(); |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
///@} |
418 | 418 |
|
419 | 419 |
///\name Query Functions for the ellapsed time |
420 | 420 |
|
421 | 421 |
///@{ |
422 | 422 |
|
423 | 423 |
///Gives back the ellapsed user time of the process |
424 | 424 |
double userTime() const |
425 | 425 |
{ |
426 | 426 |
return operator TimeStamp().userTime(); |
427 | 427 |
} |
428 | 428 |
///Gives back the ellapsed system time of the process |
429 | 429 |
double systemTime() const |
430 | 430 |
{ |
431 | 431 |
return operator TimeStamp().systemTime(); |
432 | 432 |
} |
433 | 433 |
///Gives back the ellapsed user time of the process' children |
434 | 434 |
|
435 | 435 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
436 | 436 |
/// |
437 | 437 |
double cUserTime() const |
438 | 438 |
{ |
439 | 439 |
return operator TimeStamp().cUserTime(); |
440 | 440 |
} |
441 | 441 |
///Gives back the ellapsed user time of the process' children |
442 | 442 |
|
443 | 443 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
444 | 444 |
/// |
445 | 445 |
double cSystemTime() const |
446 | 446 |
{ |
447 | 447 |
return operator TimeStamp().cSystemTime(); |
448 | 448 |
} |
449 | 449 |
///Gives back the ellapsed real time |
450 | 450 |
double realTime() const |
451 | 451 |
{ |
452 | 452 |
return operator TimeStamp().realTime(); |
453 | 453 |
} |
454 | 454 |
///Computes the ellapsed time |
455 | 455 |
|
456 | 456 |
///This conversion computes the ellapsed time, therefore you can print |
457 | 457 |
///the ellapsed time like this. |
458 | 458 |
///\code |
459 | 459 |
/// Timer t; |
460 | 460 |
/// doSomething(); |
461 | 461 |
/// std::cout << t << '\n'; |
462 | 462 |
///\endcode |
463 | 463 |
operator TimeStamp () const |
464 | 464 |
{ |
465 | 465 |
TimeStamp t; |
466 | 466 |
t.stamp(); |
467 | 467 |
return _running?t-start_time:start_time; |
468 | 468 |
} |
469 | 469 |
|
470 | 470 |
|
471 | 471 |
///@} |
472 | 472 |
}; |
473 | 473 |
|
474 | 474 |
///Same as Timer but prints a report on destruction. |
475 | 475 |
|
476 | 476 |
///Same as \ref Timer but prints a report on destruction. |
477 | 477 |
///This example shows its usage. |
478 | 478 |
///\code |
479 | 479 |
/// void myAlg(ListGraph &g,int n) |
480 | 480 |
/// { |
481 | 481 |
/// TimeReport tr("Running time of myAlg: "); |
482 | 482 |
/// ... //Here comes the algorithm |
483 | 483 |
/// } |
484 | 484 |
///\endcode |
485 | 485 |
/// |
486 | 486 |
///\sa Timer |
487 | 487 |
///\sa NoTimeReport |
488 | 488 |
class TimeReport : public Timer |
489 | 489 |
{ |
490 | 490 |
std::string _title; |
491 | 491 |
std::ostream &_os; |
492 | 492 |
public: |
493 | 493 |
///Constructor |
494 | 494 |
|
495 | 495 |
///Constructor. |
496 | 496 |
///\param title This text will be printed before the ellapsed time. |
497 | 497 |
///\param os The stream to print the report to. |
498 | 498 |
///\param run Sets whether the timer should start immediately. |
499 | 499 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
500 | 500 |
: Timer(run), _title(title), _os(os){} |
501 | 501 |
///Destructor that prints the ellapsed time |
502 | 502 |
~TimeReport() |
503 | 503 |
{ |
504 | 504 |
_os << _title << *this << std::endl; |
505 | 505 |
} |
506 | 506 |
}; |
507 | 507 |
|
508 | 508 |
///'Do nothing' version of TimeReport |
509 | 509 |
|
510 | 510 |
///\sa TimeReport |
511 | 511 |
/// |
512 | 512 |
class NoTimeReport |
513 | 513 |
{ |
514 | 514 |
public: |
515 | 515 |
///\e |
516 | 516 |
NoTimeReport(std::string,std::ostream &,bool) {} |
517 | 517 |
///\e |
518 | 518 |
NoTimeReport(std::string,std::ostream &) {} |
519 | 519 |
///\e |
520 | 520 |
NoTimeReport(std::string) {} |
521 | 521 |
///\e Do nothing. |
522 | 522 |
~NoTimeReport() {} |
523 | 523 |
|
524 | 524 |
operator TimeStamp () const { return TimeStamp(); } |
525 | 525 |
void reset() {} |
526 | 526 |
void start() {} |
527 | 527 |
void stop() {} |
528 | 528 |
void halt() {} |
529 | 529 |
int running() { return 0; } |
530 | 530 |
void restart() {} |
531 | 531 |
double userTime() const { return 0; } |
532 | 532 |
double systemTime() const { return 0; } |
533 | 533 |
double cUserTime() const { return 0; } |
534 | 534 |
double cSystemTime() const { return 0; } |
535 | 535 |
double realTime() const { return 0; } |
536 | 536 |
}; |
537 | 537 |
|
538 | 538 |
///Tool to measure the running time more exactly. |
539 | 539 |
|
540 | 540 |
///This function calls \c f several times and returns the average |
541 | 541 |
///running time. The number of the executions will be choosen in such a way |
542 | 542 |
///that the full real running time will be roughly between \c min_time |
543 | 543 |
///and <tt>2*min_time</tt>. |
544 | 544 |
///\param f the function object to be measured. |
545 | 545 |
///\param min_time the minimum total running time. |
546 | 546 |
///\retval num if it is not \c NULL, then the actual |
547 | 547 |
/// number of execution of \c f will be written into <tt>*num</tt>. |
548 | 548 |
///\retval full_time if it is not \c NULL, then the actual |
549 | 549 |
/// total running time will be written into <tt>*full_time</tt>. |
550 | 550 |
///\return The average running time of \c f. |
551 | 551 |
|
552 | 552 |
template<class F> |
553 | 553 |
TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
554 | 554 |
TimeStamp *full_time=NULL) |
555 | 555 |
{ |
556 | 556 |
TimeStamp full; |
557 | 557 |
unsigned int total=0; |
558 | 558 |
Timer t; |
559 | 559 |
for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) { |
560 | 560 |
for(;total<tn;total++) f(); |
561 | 561 |
full=t; |
562 | 562 |
} |
563 | 563 |
if(num) *num=total; |
564 | 564 |
if(full_time) *full_time=full; |
565 | 565 |
return full/total; |
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
/// @} |
569 | 569 |
|
570 | 570 |
|
571 | 571 |
} //namespace lemon |
572 | 572 |
|
573 | 573 |
#endif //LEMON_TIME_MEASURE_H |
0 comments (0 inline)