0
24
0
2
2
1
1
1
1
1
1
4
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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
\dir demo |
| 21 | 21 |
\brief A collection of demo applications. |
| 22 | 22 |
|
| 23 | 23 |
This directory contains several simple demo applications, mainly |
| 24 | 24 |
for educational purposes. |
| 25 | 25 |
*/ |
| 26 | 26 |
|
| 27 | 27 |
/** |
| 28 | 28 |
\dir doc |
| 29 | 29 |
\brief Auxiliary (and the whole generated) documentation. |
| 30 | 30 |
|
| 31 | 31 |
This directory contains some auxiliary pages and the whole generated |
| 32 | 32 |
documentation. |
| 33 | 33 |
*/ |
| 34 | 34 |
|
| 35 | 35 |
/** |
| 36 | 36 |
\dir test |
| 37 | 37 |
\brief Test programs. |
| 38 | 38 |
|
| 39 | 39 |
This directory contains several test programs that check the consistency |
| 40 | 40 |
of the code. |
| 41 | 41 |
*/ |
| 42 | 42 |
|
| 43 | 43 |
/** |
| 44 | 44 |
\dir tools |
| 45 | 45 |
\brief Some useful executables. |
| 46 | 46 |
|
| 47 | 47 |
This directory contains the sources of some useful complete executables. |
| 48 | 48 |
*/ |
| 49 | 49 |
|
| 50 | 50 |
/** |
| 51 | 51 |
\dir lemon |
| 52 | 52 |
\brief Base include directory of LEMON. |
| 53 | 53 |
|
| 54 | 54 |
This is the base directory of LEMON includes, so each include file must be |
| 55 | 55 |
prefixed with this, e.g. |
| 56 | 56 |
\code |
| 57 | 57 |
#include<lemon/list_graph.h> |
| 58 | 58 |
#include<lemon/dijkstra.h> |
| 59 | 59 |
\endcode |
| 60 | 60 |
*/ |
| 61 | 61 |
|
| 62 | 62 |
/** |
| 63 | 63 |
\dir concepts |
| 64 | 64 |
\brief Concept descriptors and checking classes. |
| 65 | 65 |
|
| 66 | 66 |
This directory contains the concept descriptors and concept checking tools. |
| 67 | 67 |
For more information see the \ref concept "Concepts" module. |
| 68 | 68 |
*/ |
| 69 | 69 |
|
| 70 | 70 |
/** |
| 71 | 71 |
\dir bits |
| 72 | 72 |
\brief Auxiliary tools for implementation. |
| 73 | 73 |
|
| 74 |
This directory contains some auxiliary classes for implementing graphs, |
|
| 74 |
This directory contains some auxiliary classes for implementing graphs, |
|
| 75 | 75 |
maps and some other classes. |
| 76 | 76 |
As a user you typically don't have to deal with these files. |
| 77 | 77 |
*/ |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
You are free to use the graph structure that fit your requirements |
| 44 | 44 |
the best, most graph algorithms and auxiliary data structures can be used |
| 45 | 45 |
with any graph structure. |
| 46 | 46 |
|
| 47 | 47 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
| 48 | 48 |
*/ |
| 49 | 49 |
|
| 50 | 50 |
/** |
| 51 | 51 |
@defgroup maps Maps |
| 52 | 52 |
@ingroup datas |
| 53 | 53 |
\brief Map structures implemented in LEMON. |
| 54 | 54 |
|
| 55 | 55 |
This group describes the map structures implemented in LEMON. |
| 56 | 56 |
|
| 57 | 57 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
| 58 | 58 |
new maps from existing ones. |
| 59 | 59 |
|
| 60 | 60 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
| 61 | 61 |
*/ |
| 62 | 62 |
|
| 63 | 63 |
/** |
| 64 | 64 |
@defgroup graph_maps Graph Maps |
| 65 | 65 |
@ingroup maps |
| 66 | 66 |
\brief Special graph-related maps. |
| 67 | 67 |
|
| 68 | 68 |
This group describes maps that are specifically designed to assign |
| 69 | 69 |
values to the nodes and arcs of graphs. |
| 70 | 70 |
*/ |
| 71 | 71 |
|
| 72 | 72 |
/** |
| 73 | 73 |
\defgroup map_adaptors Map Adaptors |
| 74 | 74 |
\ingroup maps |
| 75 | 75 |
\brief Tools to create new maps from existing ones |
| 76 | 76 |
|
| 77 | 77 |
This group describes map adaptors that are used to create "implicit" |
| 78 | 78 |
maps from other maps. |
| 79 | 79 |
|
| 80 | 80 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
| 81 | 81 |
They can make arithmetic and logical operations between one or two maps |
| 82 | 82 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 83 | 83 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 84 | 84 |
|
| 85 | 85 |
The typical usage of this classes is passing implicit maps to |
| 86 | 86 |
algorithms. If a function type algorithm is called then the function |
| 87 | 87 |
type map adaptors can be used comfortable. For example let's see the |
| 88 | 88 |
usage of map adaptors with the \c graphToEps() function. |
| 89 | 89 |
\code |
| 90 | 90 |
Color nodeColor(int deg) {
|
| 91 | 91 |
if (deg >= 2) {
|
| 92 | 92 |
return Color(0.5, 0.0, 0.5); |
| 93 | 93 |
} else if (deg == 1) {
|
| 94 | 94 |
return Color(1.0, 0.5, 1.0); |
| 95 | 95 |
} else {
|
| 96 | 96 |
return Color(0.0, 0.0, 0.0); |
| 97 | 97 |
} |
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 | 100 |
Digraph::NodeMap<int> degree_map(graph); |
| 101 | 101 |
|
| 102 | 102 |
graphToEps(graph, "graph.eps") |
| 103 | 103 |
.coords(coords).scaleToA4().undirected() |
| 104 | 104 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
| 105 | 105 |
.run(); |
| 106 | 106 |
\endcode |
| 107 | 107 |
The \c functorToMap() function makes an \c int to \c Color map from the |
| 108 | 108 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
| 109 | 109 |
and the previously created map. The composed map is a proper function to |
| 110 | 110 |
get the color of each node. |
| 111 | 111 |
|
| 112 | 112 |
The usage with class type algorithms is little bit harder. In this |
| 113 | 113 |
case the function type map adaptors can not be used, because the |
| 114 | 114 |
function map adaptors give back temporary objects. |
| 115 | 115 |
\code |
| 116 | 116 |
Digraph graph; |
| 117 | 117 |
|
| 118 | 118 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
| 119 | 119 |
DoubleArcMap length(graph); |
| 120 | 120 |
DoubleArcMap speed(graph); |
| 121 | 121 |
|
| 122 | 122 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
| 123 | 123 |
TimeMap time(length, speed); |
| 124 | 124 |
|
| 125 | 125 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
| 126 | 126 |
dijkstra.run(source, target); |
| 127 | 127 |
\endcode |
| 128 | 128 |
We have a length map and a maximum speed map on the arcs of a digraph. |
| 129 | 129 |
The minimum time to pass the arc can be calculated as the division of |
| 130 | 130 |
the two maps which can be done implicitly with the \c DivMap template |
| 131 | 131 |
class. We use the implicit minimum time map as the length map of the |
| 132 | 132 |
\c Dijkstra algorithm. |
| 133 | 133 |
*/ |
| 134 | 134 |
|
| 135 | 135 |
/** |
| 136 | 136 |
@defgroup paths Path Structures |
| 137 | 137 |
@ingroup datas |
| 138 | 138 |
\brief %Path structures implemented in LEMON. |
| 139 | 139 |
|
| 140 | 140 |
This group describes the path structures implemented in LEMON. |
| 141 | 141 |
|
| 142 | 142 |
LEMON provides flexible data structures to work with paths. |
| 143 | 143 |
All of them have similar interfaces and they can be copied easily with |
| 144 | 144 |
assignment operators and copy constructors. This makes it easy and |
| 145 | 145 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
| 146 | 146 |
any kind of path structure. |
| 147 | 147 |
|
| 148 | 148 |
\sa lemon::concepts::Path |
| 149 | 149 |
*/ |
| 150 | 150 |
|
| 151 | 151 |
/** |
| 152 | 152 |
@defgroup auxdat Auxiliary Data Structures |
| 153 | 153 |
@ingroup datas |
| 154 | 154 |
\brief Auxiliary data structures implemented in LEMON. |
| 155 | 155 |
|
| 156 | 156 |
This group describes some data structures implemented in LEMON in |
| 157 | 157 |
order to make it easier to implement combinatorial algorithms. |
| 158 | 158 |
*/ |
| 159 | 159 |
|
| 160 | 160 |
/** |
| 161 | 161 |
@defgroup algs Algorithms |
| 162 | 162 |
\brief This group describes the several algorithms |
| 163 | 163 |
implemented in LEMON. |
| 164 | 164 |
|
| 165 | 165 |
This group describes the several algorithms |
| 166 | 166 |
implemented in LEMON. |
| 167 | 167 |
*/ |
| 168 | 168 |
|
| 169 | 169 |
/** |
| 170 | 170 |
@defgroup search Graph Search |
| 171 | 171 |
@ingroup algs |
| 172 | 172 |
\brief Common graph search algorithms. |
| 173 | 173 |
|
| 174 | 174 |
This group describes the common graph search algorithms like |
| 175 | 175 |
Breadth-First Search (BFS) and Depth-First Search (DFS). |
| 176 | 176 |
*/ |
| 177 | 177 |
|
| 178 | 178 |
/** |
| 179 | 179 |
@defgroup shortest_path Shortest Path Algorithms |
| 180 | 180 |
@ingroup algs |
| 181 | 181 |
\brief Algorithms for finding shortest paths. |
| 182 | 182 |
|
| 183 | 183 |
This group describes the algorithms for finding shortest paths in graphs. |
| 184 | 184 |
*/ |
| 185 | 185 |
|
| 186 | 186 |
/** |
| 187 | 187 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 188 | 188 |
@ingroup algs |
| 189 | 189 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
| 190 | 190 |
|
| 191 | 191 |
This group describes the algorithms for finding a minimum cost spanning |
| 192 | 192 |
tree in a graph |
| 193 | 193 |
*/ |
| 194 | 194 |
|
| 195 | 195 |
/** |
| 196 | 196 |
@defgroup utils Tools and Utilities |
| 197 | 197 |
\brief Tools and utilities for programming in LEMON |
| 198 | 198 |
|
| 199 | 199 |
Tools and utilities for programming in LEMON. |
| 200 | 200 |
*/ |
| 201 | 201 |
|
| 202 | 202 |
/** |
| 203 | 203 |
@defgroup gutils Basic Graph Utilities |
| 204 | 204 |
@ingroup utils |
| 205 | 205 |
\brief Simple basic graph utilities. |
| 206 | 206 |
|
| 207 | 207 |
This group describes some simple basic graph utilities. |
| 208 | 208 |
*/ |
| 209 | 209 |
|
| 210 | 210 |
/** |
| 211 | 211 |
@defgroup misc Miscellaneous Tools |
| 212 | 212 |
@ingroup utils |
| 213 | 213 |
\brief Tools for development, debugging and testing. |
| 214 | 214 |
|
| 215 | 215 |
This group describes several useful tools for development, |
| 216 | 216 |
debugging and testing. |
| 217 | 217 |
*/ |
| 218 | 218 |
|
| 219 | 219 |
/** |
| 220 | 220 |
@defgroup timecount Time Measuring and Counting |
| 221 | 221 |
@ingroup misc |
| 222 | 222 |
\brief Simple tools for measuring the performance of algorithms. |
| 223 | 223 |
|
| 224 | 224 |
This group describes simple tools for measuring the performance |
| 225 | 225 |
of algorithms. |
| 226 | 226 |
*/ |
| 227 | 227 |
|
| 228 | 228 |
/** |
| 229 | 229 |
@defgroup exceptions Exceptions |
| 230 | 230 |
@ingroup utils |
| 231 | 231 |
\brief Exceptions defined in LEMON. |
| 232 | 232 |
|
| 233 | 233 |
This group describes the exceptions defined in LEMON. |
| 234 | 234 |
*/ |
| 235 | 235 |
|
| 236 | 236 |
/** |
| 237 | 237 |
@defgroup io_group Input-Output |
| 238 | 238 |
\brief Graph Input-Output methods |
| 239 | 239 |
|
| 240 | 240 |
This group describes the tools for importing and exporting graphs |
| 241 | 241 |
and graph related data. Now it supports the LEMON format |
| 242 | 242 |
and the encapsulated postscript (EPS) format. |
| 243 | 243 |
postscript (EPS) format. |
| 244 | 244 |
*/ |
| 245 | 245 |
|
| 246 | 246 |
/** |
| 247 | 247 |
@defgroup lemon_io LEMON Input-Output |
| 248 | 248 |
@ingroup io_group |
| 249 | 249 |
\brief Reading and writing LEMON Graph Format. |
| 250 | 250 |
|
| 251 | 251 |
This group describes methods for reading and writing |
| 252 | 252 |
\ref lgf-format "LEMON Graph Format". |
| 253 | 253 |
*/ |
| 254 | 254 |
|
| 255 | 255 |
/** |
| 256 | 256 |
@defgroup eps_io Postscript Exporting |
| 257 | 257 |
@ingroup io_group |
| 258 | 258 |
\brief General \c EPS drawer and graph exporter |
| 259 | 259 |
|
| 260 | 260 |
This group describes general \c EPS drawing methods and special |
| 261 | 261 |
graph exporting tools. |
| 262 | 262 |
*/ |
| 263 | 263 |
|
| 264 | 264 |
/** |
| 265 | 265 |
@defgroup concept Concepts |
| 266 | 266 |
\brief Skeleton classes and concept checking classes |
| 267 | 267 |
|
| 268 | 268 |
This group describes the data/algorithm skeletons and concept checking |
| 269 | 269 |
classes implemented in LEMON. |
| 270 | 270 |
|
| 271 | 271 |
The purpose of the classes in this group is fourfold. |
| 272 | 272 |
|
| 273 | 273 |
- These classes contain the documentations of the %concepts. In order |
| 274 | 274 |
to avoid document multiplications, an implementation of a concept |
| 275 | 275 |
simply refers to the corresponding concept class. |
| 276 | 276 |
|
| 277 | 277 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 278 | 278 |
implementation of the %concepts should provide, however completely |
| 279 | 279 |
without implementations and real data structures behind the |
| 280 | 280 |
interface. On the other hand they should provide nothing else. All |
| 281 | 281 |
the algorithms working on a data structure meeting a certain concept |
| 282 | 282 |
should compile with these classes. (Though it will not run properly, |
| 283 | 283 |
of course.) In this way it is easily to check if an algorithm |
| 284 | 284 |
doesn't use any extra feature of a certain implementation. |
| 285 | 285 |
|
| 286 | 286 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 287 | 287 |
that makes it possible to check whether a certain implementation of a |
| 288 | 288 |
concept indeed provides all the required features. |
| 289 | 289 |
|
| 290 | 290 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 291 | 291 |
*/ |
| 292 | 292 |
|
| 293 | 293 |
/** |
| 294 | 294 |
@defgroup graph_concepts Graph Structure Concepts |
| 295 | 295 |
@ingroup concept |
| 296 | 296 |
\brief Skeleton and concept checking classes for graph structures |
| 297 | 297 |
|
| 298 | 298 |
This group describes the skeletons and concept checking classes of LEMON's |
| 299 | 299 |
graph structures and helper classes used to implement these. |
| 300 | 300 |
*/ |
| 301 | 301 |
|
| 302 | 302 |
/** |
| 303 | 303 |
@defgroup map_concepts Map Concepts |
| 304 | 304 |
@ingroup concept |
| 305 | 305 |
\brief Skeleton and concept checking classes for maps |
| 306 |
|
|
| 306 |
|
|
| 307 | 307 |
This group describes the skeletons and concept checking classes of maps. |
| 308 | 308 |
*/ |
| 309 | 309 |
|
| 310 | 310 |
/** |
| 311 | 311 |
\anchor demoprograms |
| 312 | 312 |
|
| 313 | 313 |
@defgroup demos Demo programs |
| 314 | 314 |
|
| 315 | 315 |
Some demo programs are listed here. Their full source codes can be found in |
| 316 | 316 |
the \c demo subdirectory of the source tree. |
| 317 | 317 |
|
| 318 | 318 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
| 319 | 319 |
build the library. |
| 320 | 320 |
*/ |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
|
| 23 | 23 |
|
| 24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
| 25 | 25 |
|
| 26 | 26 |
The \e LGF is a <em>column oriented</em> |
| 27 | 27 |
file format for storing graphs and associated data like |
| 28 | 28 |
node and edge maps. |
| 29 | 29 |
|
| 30 | 30 |
Each line with \c '#' first non-whitespace |
| 31 | 31 |
character is considered as a comment line. |
| 32 | 32 |
|
| 33 | 33 |
Otherwise the file consists of sections starting with |
| 34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
| 35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
| 36 | 36 |
\@arcs and \c \@edges |
| 37 | 37 |
and \@attributes. Each header line may also have an optional |
| 38 | 38 |
\e name, which can be use to distinguish the sections of the same |
| 39 | 39 |
type. |
| 40 | 40 |
|
| 41 | 41 |
The standard sections are column oriented, each line consists of |
| 42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
| 43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
| 44 | 44 |
while a quoted token is a |
| 45 | 45 |
character sequence surrounded by double quotes, and it can also |
| 46 | 46 |
contain whitespaces and escape sequences. |
| 47 | 47 |
|
| 48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
| 49 | 49 |
maps. The first is a header line, its columns are the names of the |
| 50 | 50 |
maps appearing in the following lines. |
| 51 | 51 |
One of the maps must be called \c |
| 52 | 52 |
"label", which plays special role in the file. |
| 53 | 53 |
The following |
| 54 | 54 |
non-empty lines until the next section describes nodes of the |
| 55 | 55 |
graph. Each line contains the values of the node maps |
| 56 | 56 |
associated to the current node. |
| 57 | 57 |
|
| 58 | 58 |
\code |
| 59 | 59 |
@nodes |
| 60 | 60 |
label coordinates size title |
| 61 | 61 |
1 (10,20) 10 "First node" |
| 62 | 62 |
2 (80,80) 8 "Second node" |
| 63 | 63 |
3 (40,10) 10 "Third node" |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, it |
| 67 | 67 |
again starts with a header line describing the names of the maps, but |
| 68 | 68 |
the \c "label" map is not obligatory here. The following lines |
| 69 | 69 |
describe the arcs. The first two tokens of each line are the source |
| 70 | 70 |
and the target node of the arc, respectively, then come the map |
| 71 | 71 |
values. The source and target tokens must be node labels. |
| 72 | 72 |
|
| 73 | 73 |
\code |
| 74 | 74 |
@arcs |
| 75 | 75 |
capacity |
| 76 | 76 |
1 2 16 |
| 77 | 77 |
1 3 12 |
| 78 | 78 |
2 3 18 |
| 79 | 79 |
\endcode |
| 80 | 80 |
|
| 81 | 81 |
If there is no map in the \c \@arcs section at all, then it must be |
| 82 | 82 |
indicated by a sole '-' sign in the first line. |
| 83 | 83 |
|
| 84 | 84 |
\code |
| 85 | 85 |
@arcs |
| 86 | 86 |
- |
| 87 | 87 |
1 2 |
| 88 | 88 |
1 3 |
| 89 | 89 |
2 3 |
| 90 | 90 |
\endcode |
| 91 | 91 |
|
| 92 | 92 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
| 93 | 93 |
also store the edge set of an undirected graph. In such case there is |
| 94 | 94 |
a conventional method for store arc maps in the file, if two columns |
| 95 | 95 |
have the same caption with \c '+' and \c '-' prefix, then these columns |
| 96 | 96 |
can be regarded as the values of an arc map. |
| 97 | 97 |
|
| 98 | 98 |
The \c \@attributes section contains key-value pairs, each line |
| 99 | 99 |
consists of two tokens, an attribute name, and then an attribute |
| 100 | 100 |
value. The value of the attribute could be also a label value of a |
| 101 | 101 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
| 102 | 102 |
which regards to the forward or backward directed arc of the |
| 103 | 103 |
corresponding edge. |
| 104 | 104 |
|
| 105 | 105 |
\code |
| 106 | 106 |
@attributes |
| 107 | 107 |
source 1 |
| 108 | 108 |
target 3 |
| 109 | 109 |
caption "LEMON test digraph" |
| 110 | 110 |
\endcode |
| 111 | 111 |
|
| 112 | 112 |
The \e LGF can contain extra sections, but there is no restriction on |
| 113 | 113 |
the format of such sections. |
| 114 | 114 |
|
| 115 | 115 |
*/ |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
// LocalWords: whitespace whitespaces |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
///\file |
| 20 | 20 |
///\brief Some basic non-inline functions and static global data. |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/tolerance.h> |
| 23 | 23 |
#include<lemon/core.h> |
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
float Tolerance<float>::def_epsilon = static_cast<float>(1e-4); |
| 27 | 27 |
double Tolerance<double>::def_epsilon = 1e-10; |
| 28 | 28 |
long double Tolerance<long double>::def_epsilon = 1e-14; |
| 29 | 29 |
|
| 30 | 30 |
#ifndef LEMON_ONLY_TEMPLATES |
| 31 | 31 |
const Invalid INVALID = Invalid(); |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
} //namespace lemon |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 | 22 |
#include <lemon/config.h> |
| 23 | 23 |
#include <lemon/bits/array_map.h> |
| 24 | 24 |
#include <lemon/bits/vector_map.h> |
| 25 | 25 |
//#include <lemon/bits/debug_map.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup graphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Graph maps that construct and destruct their elements dynamically. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
|
| 34 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
| 35 | 35 |
|
| 36 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
| 37 | 37 |
struct DefaultMapSelector {
|
| 38 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
| 39 | 39 |
}; |
| 40 | 40 |
|
| 41 | 41 |
// bool |
| 42 | 42 |
template <typename _Graph, typename _Item> |
| 43 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> {
|
| 44 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
| 45 | 45 |
}; |
| 46 | 46 |
|
| 47 | 47 |
// char |
| 48 | 48 |
template <typename _Graph, typename _Item> |
| 49 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> {
|
| 50 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
template <typename _Graph, typename _Item> |
| 54 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> {
|
| 55 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
| 56 | 56 |
}; |
| 57 | 57 |
|
| 58 | 58 |
template <typename _Graph, typename _Item> |
| 59 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> {
|
| 60 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
| 61 | 61 |
}; |
| 62 | 62 |
|
| 63 | 63 |
|
| 64 | 64 |
// int |
| 65 | 65 |
template <typename _Graph, typename _Item> |
| 66 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> {
|
| 67 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
| 68 | 68 |
}; |
| 69 | 69 |
|
| 70 | 70 |
template <typename _Graph, typename _Item> |
| 71 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> {
|
| 72 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
| 73 | 73 |
}; |
| 74 | 74 |
|
| 75 | 75 |
|
| 76 | 76 |
// short |
| 77 | 77 |
template <typename _Graph, typename _Item> |
| 78 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> {
|
| 79 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
| 80 | 80 |
}; |
| 81 | 81 |
|
| 82 | 82 |
template <typename _Graph, typename _Item> |
| 83 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> {
|
| 84 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
| 85 | 85 |
}; |
| 86 | 86 |
|
| 87 | 87 |
|
| 88 | 88 |
// long |
| 89 | 89 |
template <typename _Graph, typename _Item> |
| 90 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> {
|
| 91 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
| 92 | 92 |
}; |
| 93 | 93 |
|
| 94 | 94 |
template <typename _Graph, typename _Item> |
| 95 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> {
|
| 96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
| 97 | 97 |
}; |
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
#if defined LEMON_HAVE_LONG_LONG |
| 101 | 101 |
|
| 102 | 102 |
// long long |
| 103 | 103 |
template <typename _Graph, typename _Item> |
| 104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> {
|
| 105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
| 106 | 106 |
}; |
| 107 | 107 |
|
| 108 | 108 |
template <typename _Graph, typename _Item> |
| 109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
|
| 110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
#endif |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
// float |
| 117 | 117 |
template <typename _Graph, typename _Item> |
| 118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> {
|
| 119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
| 120 | 120 |
}; |
| 121 | 121 |
|
| 122 | 122 |
|
| 123 | 123 |
// double |
| 124 | 124 |
template <typename _Graph, typename _Item> |
| 125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> {
|
| 126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
|
| 130 | 130 |
// long double |
| 131 | 131 |
template <typename _Graph, typename _Item> |
| 132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> {
|
| 133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
| 134 | 134 |
}; |
| 135 | 135 |
|
| 136 | 136 |
|
| 137 | 137 |
// pointer |
| 138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
| 139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
|
| 140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
| 141 | 141 |
}; |
| 142 | 142 |
|
| 143 | 143 |
// #else |
| 144 | 144 |
|
| 145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
| 146 | 146 |
// struct DefaultMapSelector {
|
| 147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
| 148 | 148 |
// }; |
| 149 | 149 |
|
| 150 | 150 |
// #endif |
| 151 | 151 |
|
| 152 | 152 |
// DefaultMap class |
| 153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
| 154 | 154 |
class DefaultMap |
| 155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map {
|
| 156 | 156 |
public: |
| 157 | 157 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
| 158 | 158 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 159 | 159 |
|
| 160 | 160 |
typedef typename Parent::Graph Graph; |
| 161 | 161 |
typedef typename Parent::Value Value; |
| 162 | 162 |
|
| 163 | 163 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {}
|
| 164 | 164 |
DefaultMap(const Graph& graph, const Value& value) |
| 165 | 165 |
: Parent(graph, value) {}
|
| 166 | 166 |
|
| 167 | 167 |
DefaultMap& operator=(const DefaultMap& cmap) {
|
| 168 | 168 |
return operator=<DefaultMap>(cmap); |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
template <typename CMap> |
| 172 | 172 |
DefaultMap& operator=(const CMap& cmap) {
|
| 173 | 173 |
Parent::operator=(cmap); |
| 174 | 174 |
return *this; |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
}; |
| 178 | 178 |
|
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
#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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 | 29 |
//\file |
| 30 | 30 |
//\brief Extenders for iterable maps. |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
// \ingroup graphbits |
| 35 | 35 |
// |
| 36 | 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() : map(NULL) {}
|
| 84 | 84 |
|
| 85 | 85 |
MapIt(Invalid i) : Parent(i), map(NULL) {}
|
| 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() : map(NULL) {}
|
| 124 | 124 |
|
| 125 | 125 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) {}
|
| 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() : map(NULL) {}
|
| 153 | 153 |
|
| 154 | 154 |
ItemIt(Invalid i) : Parent(i), map(NULL) {}
|
| 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 | 174 |
// \ingroup graphbits |
| 175 | 175 |
// |
| 176 | 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() : map(NULL) {}
|
| 228 | 228 |
|
| 229 | 229 |
MapIt(Invalid i) : Parent(i), map(NULL) { }
|
| 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() : map(NULL) {}
|
| 268 | 268 |
|
| 269 | 269 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) { }
|
| 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() : map(NULL) {}
|
| 297 | 297 |
|
| 298 | 298 |
ItemIt(Invalid i) : Parent(i), map(NULL) { }
|
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_PRED_MAP_PATH_H |
| 20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
| 21 | 21 |
|
| 22 | 22 |
namespace lemon {
|
| 23 | 23 |
|
| 24 | 24 |
template <typename _Digraph, typename _PredMap> |
| 25 | 25 |
class PredMapPath {
|
| 26 | 26 |
public: |
| 27 | 27 |
typedef True RevPathTag; |
| 28 | 28 |
|
| 29 | 29 |
typedef _Digraph Digraph; |
| 30 | 30 |
typedef typename Digraph::Arc Arc; |
| 31 | 31 |
typedef _PredMap PredMap; |
| 32 | 32 |
|
| 33 | 33 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
| 34 | 34 |
typename Digraph::Node _target) |
| 35 | 35 |
: digraph(_digraph), predMap(_predMap), target(_target) {}
|
| 36 | 36 |
|
| 37 | 37 |
int length() const {
|
| 38 | 38 |
int len = 0; |
| 39 | 39 |
typename Digraph::Node node = target; |
| 40 | 40 |
typename Digraph::Arc arc; |
| 41 | 41 |
while ((arc = predMap[node]) != INVALID) {
|
| 42 | 42 |
node = digraph.source(arc); |
| 43 | 43 |
++len; |
| 44 | 44 |
} |
| 45 | 45 |
return len; |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 | 48 |
bool empty() const {
|
| 49 | 49 |
return predMap[target] == INVALID; |
| 50 | 50 |
} |
| 51 | 51 |
|
| 52 | 52 |
class RevArcIt {
|
| 53 | 53 |
public: |
| 54 | 54 |
RevArcIt() {}
|
| 55 | 55 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 56 | 56 |
RevArcIt(const PredMapPath& _path) |
| 57 | 57 |
: path(&_path), current(_path.target) {
|
| 58 | 58 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 | 61 |
operator const typename Digraph::Arc() const {
|
| 62 | 62 |
return path->predMap[current]; |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
RevArcIt& operator++() {
|
| 66 | 66 |
current = path->digraph.source(path->predMap[current]); |
| 67 | 67 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 68 | 68 |
return *this; |
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 | 71 |
bool operator==(const RevArcIt& e) const {
|
| 72 | 72 |
return current == e.current; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
bool operator!=(const RevArcIt& e) const {
|
| 76 | 76 |
return current != e.current; |
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 | 79 |
bool operator<(const RevArcIt& e) const {
|
| 80 | 80 |
return current < e.current; |
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
private: |
| 84 | 84 |
const PredMapPath* path; |
| 85 | 85 |
typename Digraph::Node current; |
| 86 | 86 |
}; |
| 87 | 87 |
|
| 88 | 88 |
private: |
| 89 | 89 |
const Digraph& digraph; |
| 90 | 90 |
const PredMap& predMap; |
| 91 | 91 |
typename Digraph::Node target; |
| 92 | 92 |
}; |
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
template <typename _Digraph, typename _PredMatrixMap> |
| 96 | 96 |
class PredMatrixMapPath {
|
| 97 | 97 |
public: |
| 98 | 98 |
typedef True RevPathTag; |
| 99 | 99 |
|
| 100 | 100 |
typedef _Digraph Digraph; |
| 101 | 101 |
typedef typename Digraph::Arc Arc; |
| 102 | 102 |
typedef _PredMatrixMap PredMatrixMap; |
| 103 | 103 |
|
| 104 | 104 |
PredMatrixMapPath(const Digraph& _digraph, |
| 105 | 105 |
const PredMatrixMap& _predMatrixMap, |
| 106 | 106 |
typename Digraph::Node _source, |
| 107 | 107 |
typename Digraph::Node _target) |
| 108 | 108 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
| 109 | 109 |
source(_source), target(_target) {}
|
| 110 | 110 |
|
| 111 | 111 |
int length() const {
|
| 112 | 112 |
int len = 0; |
| 113 | 113 |
typename Digraph::Node node = target; |
| 114 | 114 |
typename Digraph::Arc arc; |
| 115 | 115 |
while ((arc = predMatrixMap(source, node)) != INVALID) {
|
| 116 | 116 |
node = digraph.source(arc); |
| 117 | 117 |
++len; |
| 118 | 118 |
} |
| 119 | 119 |
return len; |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 | 122 |
bool empty() const {
|
| 123 | 123 |
return predMatrixMap(source, target) == INVALID; |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
class RevArcIt {
|
| 127 | 127 |
public: |
| 128 | 128 |
RevArcIt() {}
|
| 129 | 129 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 130 | 130 |
RevArcIt(const PredMatrixMapPath& _path) |
| 131 | 131 |
: path(&_path), current(_path.target) {
|
| 132 | 132 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 133 | 133 |
current = INVALID; |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
operator const typename Digraph::Arc() const {
|
| 137 | 137 |
return path->predMatrixMap(path->source, current); |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
RevArcIt& operator++() {
|
| 141 | 141 |
current = |
| 142 | 142 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
| 143 | 143 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 144 | 144 |
current = INVALID; |
| 145 | 145 |
return *this; |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
bool operator==(const RevArcIt& e) const {
|
| 149 | 149 |
return current == e.current; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
bool operator!=(const RevArcIt& e) const {
|
| 153 | 153 |
return current != e.current; |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 | 156 |
bool operator<(const RevArcIt& e) const {
|
| 157 | 157 |
return current < e.current; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
private: |
| 161 | 161 |
const PredMatrixMapPath* path; |
| 162 | 162 |
typename Digraph::Node current; |
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
private: |
| 166 | 166 |
const Digraph& digraph; |
| 167 | 167 |
const PredMatrixMap& predMatrixMap; |
| 168 | 168 |
typename Digraph::Node source; |
| 169 | 169 |
typename Digraph::Node target; |
| 170 | 170 |
}; |
| 171 | 171 |
|
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
#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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
///\file |
| 20 | 20 |
///\brief Some basic non-inline functions and static global data. |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/bits/windows.h> |
| 23 | 23 |
|
| 24 | 24 |
#ifdef WIN32 |
| 25 | 25 |
#ifndef WIN32_LEAN_AND_MEAN |
| 26 | 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 | 27 |
#endif |
| 28 | 28 |
#ifndef NOMINMAX |
| 29 | 29 |
#define NOMINMAX |
| 30 | 30 |
#endif |
| 31 | 31 |
#ifdef UNICODE |
| 32 | 32 |
#undef UNICODE |
| 33 | 33 |
#endif |
| 34 | 34 |
#include <windows.h> |
| 35 | 35 |
#ifdef LOCALE_INVARIANT |
| 36 | 36 |
#define MY_LOCALE LOCALE_INVARIANT |
| 37 | 37 |
#else |
| 38 | 38 |
#define MY_LOCALE LOCALE_NEUTRAL |
| 39 | 39 |
#endif |
| 40 | 40 |
#else |
| 41 | 41 |
#include <unistd.h> |
| 42 | 42 |
#include <ctime> |
| 43 | 43 |
#include <sys/times.h> |
| 44 | 44 |
#include <sys/time.h> |
| 45 | 45 |
#endif |
| 46 | 46 |
|
| 47 | 47 |
#include <cmath> |
| 48 | 48 |
#include <sstream> |
| 49 | 49 |
|
| 50 | 50 |
namespace lemon {
|
| 51 | 51 |
namespace bits {
|
| 52 | 52 |
void getWinProcTimes(double &rtime, |
| 53 | 53 |
double &utime, double &stime, |
| 54 | 54 |
double &cutime, double &cstime) |
| 55 | 55 |
{
|
| 56 | 56 |
#ifdef WIN32 |
| 57 | 57 |
static const double ch = 4294967296.0e-7; |
| 58 | 58 |
static const double cl = 1.0e-7; |
| 59 | 59 |
|
| 60 | 60 |
FILETIME system; |
| 61 | 61 |
GetSystemTimeAsFileTime(&system); |
| 62 | 62 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
| 63 | 63 |
|
| 64 | 64 |
FILETIME create, exit, kernel, user; |
| 65 | 65 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
|
| 66 | 66 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
| 67 | 67 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
| 68 | 68 |
cutime = 0; |
| 69 | 69 |
cstime = 0; |
| 70 | 70 |
} else {
|
| 71 | 71 |
rtime = 0; |
| 72 | 72 |
utime = 0; |
| 73 | 73 |
stime = 0; |
| 74 | 74 |
cutime = 0; |
| 75 | 75 |
cstime = 0; |
| 76 | 76 |
} |
| 77 | 77 |
#else |
| 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 |
#endif |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
std::string getWinFormattedDate() |
| 93 | 93 |
{
|
| 94 | 94 |
std::ostringstream os; |
| 95 | 95 |
#ifdef WIN32 |
| 96 | 96 |
SYSTEMTIME time; |
| 97 | 97 |
GetSystemTime(&time); |
| 98 | 98 |
char buf1[11], buf2[9], buf3[5]; |
| 99 |
|
|
| 99 |
if (GetDateFormat(MY_LOCALE, 0, &time, |
|
| 100 | 100 |
("ddd MMM dd"), buf1, 11) &&
|
| 101 | 101 |
GetTimeFormat(MY_LOCALE, 0, &time, |
| 102 | 102 |
("HH':'mm':'ss"), buf2, 9) &&
|
| 103 | 103 |
GetDateFormat(MY_LOCALE, 0, &time, |
| 104 | 104 |
("yyyy"), buf3, 5)) {
|
| 105 | 105 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
| 106 | 106 |
} |
| 107 | 107 |
else os << "unknown"; |
| 108 | 108 |
#else |
| 109 | 109 |
timeval tv; |
| 110 | 110 |
gettimeofday(&tv, 0); |
| 111 | 111 |
|
| 112 | 112 |
char cbuf[26]; |
| 113 | 113 |
ctime_r(&tv.tv_sec,cbuf); |
| 114 | 114 |
os << cbuf; |
| 115 | 115 |
#endif |
| 116 | 116 |
return os.str(); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
int getWinRndSeed() |
| 120 | 120 |
{
|
| 121 | 121 |
#ifdef WIN32 |
| 122 | 122 |
FILETIME time; |
| 123 | 123 |
GetSystemTimeAsFileTime(&time); |
| 124 | 124 |
return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; |
| 125 | 125 |
#else |
| 126 | 126 |
timeval tv; |
| 127 | 127 |
gettimeofday(&tv, 0); |
| 128 | 128 |
return getpid() + tv.tv_sec + tv.tv_usec; |
| 129 | 129 |
#endif |
| 130 | 130 |
} |
| 131 | 131 |
} |
| 132 | 132 |
} |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_CORE_H |
| 20 | 20 |
#define LEMON_CORE_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/config.h> |
| 26 | 26 |
#include <lemon/bits/enable_if.h> |
| 27 | 27 |
#include <lemon/bits/traits.h> |
| 28 | 28 |
#include <lemon/assert.h> |
| 29 | 29 |
|
| 30 | 30 |
///\file |
| 31 | 31 |
///\brief LEMON core utilities. |
| 32 | 32 |
/// |
| 33 | 33 |
///This header file contains core utilities for LEMON. |
| 34 | 34 |
///It is automatically included by all graph types, therefore it usually |
| 35 | 35 |
///do not have to be included directly. |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief Dummy type to make it easier to create invalid iterators. |
| 40 | 40 |
/// |
| 41 | 41 |
/// Dummy type to make it easier to create invalid iterators. |
| 42 | 42 |
/// See \ref INVALID for the usage. |
| 43 | 43 |
struct Invalid {
|
| 44 | 44 |
public: |
| 45 | 45 |
bool operator==(Invalid) { return true; }
|
| 46 | 46 |
bool operator!=(Invalid) { return false; }
|
| 47 | 47 |
bool operator< (Invalid) { return false; }
|
| 48 | 48 |
}; |
| 49 | 49 |
|
| 50 | 50 |
/// \brief Invalid iterators. |
| 51 | 51 |
/// |
| 52 | 52 |
/// \ref Invalid is a global type that converts to each iterator |
| 53 | 53 |
/// in such a way that the value of the target iterator will be invalid. |
| 54 | 54 |
#ifdef LEMON_ONLY_TEMPLATES |
| 55 | 55 |
const Invalid INVALID = Invalid(); |
| 56 | 56 |
#else |
| 57 | 57 |
extern const Invalid INVALID; |
| 58 | 58 |
#endif |
| 59 | 59 |
|
| 60 | 60 |
/// \addtogroup gutils |
| 61 | 61 |
/// @{
|
| 62 | 62 |
|
| 63 | 63 |
///Create convenience typedefs for the digraph types and iterators |
| 64 | 64 |
|
| 65 | 65 |
///This \c \#define creates convenient type definitions for the following |
| 66 | 66 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 67 | 67 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 68 | 68 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 69 | 69 |
/// |
| 70 | 70 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 71 | 71 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
| 72 | 72 |
///macro. |
| 73 | 73 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 74 | 74 |
typedef Digraph::Node Node; \ |
| 75 | 75 |
typedef Digraph::NodeIt NodeIt; \ |
| 76 | 76 |
typedef Digraph::Arc Arc; \ |
| 77 | 77 |
typedef Digraph::ArcIt ArcIt; \ |
| 78 | 78 |
typedef Digraph::InArcIt InArcIt; \ |
| 79 | 79 |
typedef Digraph::OutArcIt OutArcIt; \ |
| 80 | 80 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
| 81 | 81 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
| 82 | 82 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
| 83 | 83 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
| 84 | 84 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
| 85 | 85 |
typedef Digraph::ArcMap<double> DoubleArcMap |
| 86 | 86 |
|
| 87 | 87 |
///Create convenience typedefs for the digraph types and iterators |
| 88 | 88 |
|
| 89 | 89 |
///\see DIGRAPH_TYPEDEFS |
| 90 | 90 |
/// |
| 91 | 91 |
///\note Use this macro, if the graph type is a dependent type, |
| 92 | 92 |
///ie. the graph type depend on a template parameter. |
| 93 | 93 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 94 | 94 |
typedef typename Digraph::Node Node; \ |
| 95 | 95 |
typedef typename Digraph::NodeIt NodeIt; \ |
| 96 | 96 |
typedef typename Digraph::Arc Arc; \ |
| 97 | 97 |
typedef typename Digraph::ArcIt ArcIt; \ |
| 98 | 98 |
typedef typename Digraph::InArcIt InArcIt; \ |
| 99 | 99 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
| 100 | 100 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
| 101 | 101 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
| 102 | 102 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
| 103 | 103 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
| 104 | 104 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
| 105 | 105 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
| 106 | 106 |
|
| 107 | 107 |
///Create convenience typedefs for the graph types and iterators |
| 108 | 108 |
|
| 109 | 109 |
///This \c \#define creates the same convenient type definitions as defined |
| 110 | 110 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 111 | 111 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 112 | 112 |
///\c DoubleEdgeMap. |
| 113 | 113 |
/// |
| 114 | 114 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 115 | 115 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
| 116 | 116 |
///macro. |
| 117 | 117 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 118 | 118 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 119 | 119 |
typedef Graph::Edge Edge; \ |
| 120 | 120 |
typedef Graph::EdgeIt EdgeIt; \ |
| 121 | 121 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
| 122 | 122 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
| 123 | 123 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
| 124 | 124 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
| 125 | 125 |
|
| 126 | 126 |
///Create convenience typedefs for the graph types and iterators |
| 127 | 127 |
|
| 128 | 128 |
///\see GRAPH_TYPEDEFS |
| 129 | 129 |
/// |
| 130 | 130 |
///\note Use this macro, if the graph type is a dependent type, |
| 131 | 131 |
///ie. the graph type depend on a template parameter. |
| 132 | 132 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
| 133 | 133 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
| 134 | 134 |
typedef typename Graph::Edge Edge; \ |
| 135 | 135 |
typedef typename Graph::EdgeIt EdgeIt; \ |
| 136 | 136 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
| 137 | 137 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
| 138 | 138 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
| 139 | 139 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
| 140 | 140 |
|
| 141 | 141 |
/// \brief Function to count the items in a graph. |
| 142 | 142 |
/// |
| 143 | 143 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
| 144 | 144 |
/// The complexity of the function is linear because |
| 145 | 145 |
/// it iterates on all of the items. |
| 146 | 146 |
template <typename Graph, typename Item> |
| 147 | 147 |
inline int countItems(const Graph& g) {
|
| 148 | 148 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 149 | 149 |
int num = 0; |
| 150 | 150 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 151 | 151 |
++num; |
| 152 | 152 |
} |
| 153 | 153 |
return num; |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 | 156 |
// Node counting: |
| 157 | 157 |
|
| 158 | 158 |
namespace _core_bits {
|
| 159 | 159 |
|
| 160 | 160 |
template <typename Graph, typename Enable = void> |
| 161 | 161 |
struct CountNodesSelector {
|
| 162 | 162 |
static int count(const Graph &g) {
|
| 163 | 163 |
return countItems<Graph, typename Graph::Node>(g); |
| 164 | 164 |
} |
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
template <typename Graph> |
| 168 | 168 |
struct CountNodesSelector< |
| 169 | 169 |
Graph, typename |
| 170 | 170 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 171 | 171 |
{
|
| 172 | 172 |
static int count(const Graph &g) {
|
| 173 | 173 |
return g.nodeNum(); |
| 174 | 174 |
} |
| 175 | 175 |
}; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
/// \brief Function to count the nodes in the graph. |
| 179 | 179 |
/// |
| 180 | 180 |
/// This function counts the nodes in the graph. |
| 181 | 181 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
| 182 | 182 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 183 | 183 |
/// |
| 184 | 184 |
/// \note If the graph contains a \c nodeNum() member function and a |
| 185 | 185 |
/// \c NodeNumTag tag then this function calls directly the member |
| 186 | 186 |
/// function to query the cardinality of the node set. |
| 187 | 187 |
template <typename Graph> |
| 188 | 188 |
inline int countNodes(const Graph& g) {
|
| 189 | 189 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
// Arc counting: |
| 193 | 193 |
|
| 194 | 194 |
namespace _core_bits {
|
| 195 | 195 |
|
| 196 | 196 |
template <typename Graph, typename Enable = void> |
| 197 | 197 |
struct CountArcsSelector {
|
| 198 | 198 |
static int count(const Graph &g) {
|
| 199 | 199 |
return countItems<Graph, typename Graph::Arc>(g); |
| 200 | 200 |
} |
| 201 | 201 |
}; |
| 202 | 202 |
|
| 203 | 203 |
template <typename Graph> |
| 204 | 204 |
struct CountArcsSelector< |
| 205 | 205 |
Graph, |
| 206 | 206 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
| 207 | 207 |
{
|
| 208 | 208 |
static int count(const Graph &g) {
|
| 209 | 209 |
return g.arcNum(); |
| 210 | 210 |
} |
| 211 | 211 |
}; |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 | 214 |
/// \brief Function to count the arcs in the graph. |
| 215 | 215 |
/// |
| 216 | 216 |
/// This function counts the arcs in the graph. |
| 217 | 217 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
| 218 | 218 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 219 | 219 |
/// |
| 220 | 220 |
/// \note If the graph contains a \c arcNum() member function and a |
| 221 | 221 |
/// \c ArcNumTag tag then this function calls directly the member |
| 222 | 222 |
/// function to query the cardinality of the arc set. |
| 223 | 223 |
template <typename Graph> |
| 224 | 224 |
inline int countArcs(const Graph& g) {
|
| 225 | 225 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
// Edge counting: |
| 229 | 229 |
|
| 230 | 230 |
namespace _core_bits {
|
| 231 | 231 |
|
| 232 | 232 |
template <typename Graph, typename Enable = void> |
| 233 | 233 |
struct CountEdgesSelector {
|
| 234 | 234 |
static int count(const Graph &g) {
|
| 235 | 235 |
return countItems<Graph, typename Graph::Edge>(g); |
| 236 | 236 |
} |
| 237 | 237 |
}; |
| 238 | 238 |
|
| 239 | 239 |
template <typename Graph> |
| 240 | 240 |
struct CountEdgesSelector< |
| 241 | 241 |
Graph, |
| 242 | 242 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
| 243 | 243 |
{
|
| 244 | 244 |
static int count(const Graph &g) {
|
| 245 | 245 |
return g.edgeNum(); |
| 246 | 246 |
} |
| 247 | 247 |
}; |
| 248 | 248 |
} |
| 249 | 249 |
|
| 250 | 250 |
/// \brief Function to count the edges in the graph. |
| 251 | 251 |
/// |
| 252 | 252 |
/// This function counts the edges in the graph. |
| 253 | 253 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
| 254 | 254 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 255 | 255 |
/// |
| 256 | 256 |
/// \note If the graph contains a \c edgeNum() member function and a |
| 257 | 257 |
/// \c EdgeNumTag tag then this function calls directly the member |
| 258 | 258 |
/// function to query the cardinality of the edge set. |
| 259 | 259 |
template <typename Graph> |
| 260 | 260 |
inline int countEdges(const Graph& g) {
|
| 261 | 261 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
| 262 | 262 |
|
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
|
| 266 | 266 |
template <typename Graph, typename DegIt> |
| 267 | 267 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 268 | 268 |
int num = 0; |
| 269 | 269 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 270 | 270 |
++num; |
| 271 | 271 |
} |
| 272 | 272 |
return num; |
| 273 | 273 |
} |
| 274 | 274 |
|
| 275 | 275 |
/// \brief Function to count the number of the out-arcs from node \c n. |
| 276 | 276 |
/// |
| 277 | 277 |
/// This function counts the number of the out-arcs from node \c n |
| 278 | 278 |
/// in the graph \c g. |
| 279 | 279 |
template <typename Graph> |
| 280 | 280 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
|
| 281 | 281 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
| 282 | 282 |
} |
| 283 | 283 |
|
| 284 | 284 |
/// \brief Function to count the number of the in-arcs to node \c n. |
| 285 | 285 |
/// |
| 286 | 286 |
/// This function counts the number of the in-arcs to node \c n |
| 287 | 287 |
/// in the graph \c g. |
| 288 | 288 |
template <typename Graph> |
| 289 | 289 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
|
| 290 | 290 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
| 291 | 291 |
} |
| 292 | 292 |
|
| 293 | 293 |
/// \brief Function to count the number of the inc-edges to node \c n. |
| 294 | 294 |
/// |
| 295 | 295 |
/// This function counts the number of the inc-edges to node \c n |
| 296 | 296 |
/// in the undirected graph \c g. |
| 297 | 297 |
template <typename Graph> |
| 298 | 298 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
|
| 299 | 299 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
| 300 | 300 |
} |
| 301 | 301 |
|
| 302 | 302 |
namespace _core_bits {
|
| 303 | 303 |
|
| 304 | 304 |
template <typename Digraph, typename Item, typename RefMap> |
| 305 | 305 |
class MapCopyBase {
|
| 306 | 306 |
public: |
| 307 | 307 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
| 308 | 308 |
|
| 309 | 309 |
virtual ~MapCopyBase() {}
|
| 310 | 310 |
}; |
| 311 | 311 |
|
| 312 | 312 |
template <typename Digraph, typename Item, typename RefMap, |
| 313 | 313 |
typename FromMap, typename ToMap> |
| 314 | 314 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 315 | 315 |
public: |
| 316 | 316 |
|
| 317 | 317 |
MapCopy(const FromMap& map, ToMap& tmap) |
| 318 | 318 |
: _map(map), _tmap(tmap) {}
|
| 319 | 319 |
|
| 320 | 320 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 321 | 321 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 322 | 322 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 323 | 323 |
_tmap.set(refMap[it], _map[it]); |
| 324 | 324 |
} |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 | 327 |
private: |
| 328 | 328 |
const FromMap& _map; |
| 329 | 329 |
ToMap& _tmap; |
| 330 | 330 |
}; |
| 331 | 331 |
|
| 332 | 332 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
| 333 | 333 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 334 | 334 |
public: |
| 335 | 335 |
|
| 336 | 336 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
|
| 337 | 337 |
|
| 338 | 338 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 339 | 339 |
_it = refMap[_item]; |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
private: |
| 343 | 343 |
Item _item; |
| 344 | 344 |
It& _it; |
| 345 | 345 |
}; |
| 346 | 346 |
|
| 347 | 347 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
| 348 | 348 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 349 | 349 |
public: |
| 350 | 350 |
|
| 351 | 351 |
RefCopy(Ref& map) : _map(map) {}
|
| 352 | 352 |
|
| 353 | 353 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 354 | 354 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 355 | 355 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 356 | 356 |
_map.set(it, refMap[it]); |
| 357 | 357 |
} |
| 358 | 358 |
} |
| 359 | 359 |
|
| 360 | 360 |
private: |
| 361 | 361 |
Ref& _map; |
| 362 | 362 |
}; |
| 363 | 363 |
|
| 364 | 364 |
template <typename Digraph, typename Item, typename RefMap, |
| 365 | 365 |
typename CrossRef> |
| 366 | 366 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 367 | 367 |
public: |
| 368 | 368 |
|
| 369 | 369 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 370 | 370 |
|
| 371 | 371 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 372 | 372 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 373 | 373 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 374 | 374 |
_cmap.set(refMap[it], it); |
| 375 | 375 |
} |
| 376 | 376 |
} |
| 377 | 377 |
|
| 378 | 378 |
private: |
| 379 | 379 |
CrossRef& _cmap; |
| 380 | 380 |
}; |
| 381 | 381 |
|
| 382 | 382 |
template <typename Digraph, typename Enable = void> |
| 383 | 383 |
struct DigraphCopySelector {
|
| 384 | 384 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 385 | 385 |
static void copy(const From& from, Digraph &to, |
| 386 | 386 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 387 | 387 |
to.clear(); |
| 388 | 388 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 389 | 389 |
nodeRefMap[it] = to.addNode(); |
| 390 | 390 |
} |
| 391 | 391 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 392 | 392 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 393 | 393 |
nodeRefMap[from.target(it)]); |
| 394 | 394 |
} |
| 395 | 395 |
} |
| 396 | 396 |
}; |
| 397 | 397 |
|
| 398 | 398 |
template <typename Digraph> |
| 399 | 399 |
struct DigraphCopySelector< |
| 400 | 400 |
Digraph, |
| 401 | 401 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
| 402 | 402 |
{
|
| 403 | 403 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 404 | 404 |
static void copy(const From& from, Digraph &to, |
| 405 | 405 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 406 | 406 |
to.build(from, nodeRefMap, arcRefMap); |
| 407 | 407 |
} |
| 408 | 408 |
}; |
| 409 | 409 |
|
| 410 | 410 |
template <typename Graph, typename Enable = void> |
| 411 | 411 |
struct GraphCopySelector {
|
| 412 | 412 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 413 | 413 |
static void copy(const From& from, Graph &to, |
| 414 | 414 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 415 | 415 |
to.clear(); |
| 416 | 416 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 417 | 417 |
nodeRefMap[it] = to.addNode(); |
| 418 | 418 |
} |
| 419 | 419 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 420 | 420 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
| 421 | 421 |
nodeRefMap[from.v(it)]); |
| 422 | 422 |
} |
| 423 | 423 |
} |
| 424 | 424 |
}; |
| 425 | 425 |
|
| 426 | 426 |
template <typename Graph> |
| 427 | 427 |
struct GraphCopySelector< |
| 428 | 428 |
Graph, |
| 429 | 429 |
typename enable_if<typename Graph::BuildTag, void>::type> |
| 430 | 430 |
{
|
| 431 | 431 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 432 | 432 |
static void copy(const From& from, Graph &to, |
| 433 | 433 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 434 | 434 |
to.build(from, nodeRefMap, edgeRefMap); |
| 435 | 435 |
} |
| 436 | 436 |
}; |
| 437 | 437 |
|
| 438 | 438 |
} |
| 439 | 439 |
|
| 440 | 440 |
/// \brief Class to copy a digraph. |
| 441 | 441 |
/// |
| 442 | 442 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
| 443 | 443 |
/// simplest way of using it is through the \c digraphCopy() function. |
| 444 | 444 |
/// |
| 445 | 445 |
/// This class not only make a copy of a digraph, but it can create |
| 446 | 446 |
/// references and cross references between the nodes and arcs of |
| 447 | 447 |
/// the two digraphs, and it can copy maps to use with the newly created |
| 448 | 448 |
/// digraph. |
| 449 | 449 |
/// |
| 450 | 450 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
| 451 | 451 |
/// should be created, then the data belongs to the digraph should |
| 452 | 452 |
/// assigned to copy. In the end, the \c run() member should be |
| 453 | 453 |
/// called. |
| 454 | 454 |
/// |
| 455 | 455 |
/// The next code copies a digraph with several data: |
| 456 | 456 |
///\code |
| 457 | 457 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
| 458 | 458 |
/// // Create references for the nodes |
| 459 | 459 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 460 | 460 |
/// cg.nodeRef(nr); |
| 461 | 461 |
/// // Create cross references (inverse) for the arcs |
| 462 | 462 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
| 463 | 463 |
/// cg.arcCrossRef(acr); |
| 464 | 464 |
/// // Copy an arc map |
| 465 | 465 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
| 466 | 466 |
/// NewGraph::ArcMap<double> namap(new_graph); |
| 467 | 467 |
/// cg.arcMap(oamap, namap); |
| 468 | 468 |
/// // Copy a node |
| 469 | 469 |
/// OrigGraph::Node on; |
| 470 | 470 |
/// NewGraph::Node nn; |
| 471 | 471 |
/// cg.node(on, nn); |
| 472 | 472 |
/// // Execute copying |
| 473 | 473 |
/// cg.run(); |
| 474 | 474 |
///\endcode |
| 475 | 475 |
template <typename From, typename To> |
| 476 | 476 |
class DigraphCopy {
|
| 477 | 477 |
private: |
| 478 | 478 |
|
| 479 | 479 |
typedef typename From::Node Node; |
| 480 | 480 |
typedef typename From::NodeIt NodeIt; |
| 481 | 481 |
typedef typename From::Arc Arc; |
| 482 | 482 |
typedef typename From::ArcIt ArcIt; |
| 483 | 483 |
|
| 484 | 484 |
typedef typename To::Node TNode; |
| 485 | 485 |
typedef typename To::Arc TArc; |
| 486 | 486 |
|
| 487 | 487 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 488 | 488 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
| 489 | 489 |
|
| 490 | 490 |
public: |
| 491 | 491 |
|
| 492 | 492 |
/// \brief Constructor of DigraphCopy. |
| 493 | 493 |
/// |
| 494 | 494 |
/// Constructor of DigraphCopy for copying the content of the |
| 495 | 495 |
/// \c from digraph into the \c to digraph. |
| 496 | 496 |
DigraphCopy(const From& from, To& to) |
| 497 | 497 |
: _from(from), _to(to) {}
|
| 498 | 498 |
|
| 499 | 499 |
/// \brief Destructor of DigraphCopy |
| 500 | 500 |
/// |
| 501 | 501 |
/// Destructor of DigraphCopy. |
| 502 | 502 |
~DigraphCopy() {
|
| 503 | 503 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 504 | 504 |
delete _node_maps[i]; |
| 505 | 505 |
} |
| 506 | 506 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 507 | 507 |
delete _arc_maps[i]; |
| 508 | 508 |
} |
| 509 | 509 |
|
| 510 | 510 |
} |
| 511 | 511 |
|
| 512 | 512 |
/// \brief Copy the node references into the given map. |
| 513 | 513 |
/// |
| 514 | 514 |
/// This function copies the node references into the given map. |
| 515 | 515 |
/// The parameter should be a map, whose key type is the Node type of |
| 516 | 516 |
/// the source digraph, while the value type is the Node type of the |
| 517 | 517 |
/// destination digraph. |
| 518 | 518 |
template <typename NodeRef> |
| 519 | 519 |
DigraphCopy& nodeRef(NodeRef& map) {
|
| 520 | 520 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
| 521 | 521 |
NodeRefMap, NodeRef>(map)); |
| 522 | 522 |
return *this; |
| 523 | 523 |
} |
| 524 | 524 |
|
| 525 | 525 |
/// \brief Copy the node cross references into the given map. |
| 526 | 526 |
/// |
| 527 | 527 |
/// This function copies the node cross references (reverse references) |
| 528 | 528 |
/// into the given map. The parameter should be a map, whose key type |
| 529 | 529 |
/// is the Node type of the destination digraph, while the value type is |
| 530 | 530 |
/// the Node type of the source digraph. |
| 531 | 531 |
template <typename NodeCrossRef> |
| 532 | 532 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 533 | 533 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
| 534 | 534 |
NodeRefMap, NodeCrossRef>(map)); |
| 535 | 535 |
return *this; |
| 536 | 536 |
} |
| 537 | 537 |
|
| 538 | 538 |
/// \brief Make a copy of the given node map. |
| 539 | 539 |
/// |
| 540 | 540 |
/// This function makes a copy of the given node map for the newly |
| 541 | 541 |
/// created digraph. |
| 542 | 542 |
/// The key type of the new map \c tmap should be the Node type of the |
| 543 | 543 |
/// destination digraph, and the key type of the original map \c map |
| 544 | 544 |
/// should be the Node type of the source digraph. |
| 545 | 545 |
template <typename FromMap, typename ToMap> |
| 546 | 546 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 547 | 547 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
| 548 | 548 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
| 549 | 549 |
return *this; |
| 550 | 550 |
} |
| 551 | 551 |
|
| 552 | 552 |
/// \brief Make a copy of the given node. |
| 553 | 553 |
/// |
| 554 | 554 |
/// This function makes a copy of the given node. |
| 555 | 555 |
DigraphCopy& node(const Node& node, TNode& tnode) {
|
| 556 | 556 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
| 557 | 557 |
NodeRefMap, TNode>(node, tnode)); |
| 558 | 558 |
return *this; |
| 559 | 559 |
} |
| 560 | 560 |
|
| 561 | 561 |
/// \brief Copy the arc references into the given map. |
| 562 | 562 |
/// |
| 563 | 563 |
/// This function copies the arc references into the given map. |
| 564 | 564 |
/// The parameter should be a map, whose key type is the Arc type of |
| 565 | 565 |
/// the source digraph, while the value type is the Arc type of the |
| 566 | 566 |
/// destination digraph. |
| 567 | 567 |
template <typename ArcRef> |
| 568 | 568 |
DigraphCopy& arcRef(ArcRef& map) {
|
| 569 | 569 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
| 570 | 570 |
ArcRefMap, ArcRef>(map)); |
| 571 | 571 |
return *this; |
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
/// \brief Copy the arc cross references into the given map. |
| 575 | 575 |
/// |
| 576 | 576 |
/// This function copies the arc cross references (reverse references) |
| 577 | 577 |
/// into the given map. The parameter should be a map, whose key type |
| 578 | 578 |
/// is the Arc type of the destination digraph, while the value type is |
| 579 | 579 |
/// the Arc type of the source digraph. |
| 580 | 580 |
template <typename ArcCrossRef> |
| 581 | 581 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 582 | 582 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
| 583 | 583 |
ArcRefMap, ArcCrossRef>(map)); |
| 584 | 584 |
return *this; |
| 585 | 585 |
} |
| 586 | 586 |
|
| 587 | 587 |
/// \brief Make a copy of the given arc map. |
| 588 | 588 |
/// |
| 589 | 589 |
/// This function makes a copy of the given arc map for the newly |
| 590 | 590 |
/// created digraph. |
| 591 | 591 |
/// The key type of the new map \c tmap should be the Arc type of the |
| 592 | 592 |
/// destination digraph, and the key type of the original map \c map |
| 593 | 593 |
/// should be the Arc type of the source digraph. |
| 594 | 594 |
template <typename FromMap, typename ToMap> |
| 595 | 595 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 596 | 596 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
| 597 | 597 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
| 598 | 598 |
return *this; |
| 599 | 599 |
} |
| 600 | 600 |
|
| 601 | 601 |
/// \brief Make a copy of the given arc. |
| 602 | 602 |
/// |
| 603 | 603 |
/// This function makes a copy of the given arc. |
| 604 | 604 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 605 | 605 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
| 606 | 606 |
ArcRefMap, TArc>(arc, tarc)); |
| 607 | 607 |
return *this; |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 | 610 |
/// \brief Execute copying. |
| 611 | 611 |
/// |
| 612 | 612 |
/// This function executes the copying of the digraph along with the |
| 613 | 613 |
/// copying of the assigned data. |
| 614 | 614 |
void run() {
|
| 615 | 615 |
NodeRefMap nodeRefMap(_from); |
| 616 | 616 |
ArcRefMap arcRefMap(_from); |
| 617 | 617 |
_core_bits::DigraphCopySelector<To>:: |
| 618 | 618 |
copy(_from, _to, nodeRefMap, arcRefMap); |
| 619 | 619 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 620 | 620 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 621 | 621 |
} |
| 622 | 622 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 623 | 623 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 624 | 624 |
} |
| 625 | 625 |
} |
| 626 | 626 |
|
| 627 | 627 |
protected: |
| 628 | 628 |
|
| 629 | 629 |
const From& _from; |
| 630 | 630 |
To& _to; |
| 631 | 631 |
|
| 632 | 632 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 633 | 633 |
_node_maps; |
| 634 | 634 |
|
| 635 | 635 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 636 | 636 |
_arc_maps; |
| 637 | 637 |
|
| 638 | 638 |
}; |
| 639 | 639 |
|
| 640 | 640 |
/// \brief Copy a digraph to another digraph. |
| 641 | 641 |
/// |
| 642 | 642 |
/// This function copies a digraph to another digraph. |
| 643 | 643 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
| 644 | 644 |
/// a short example shows a basic work: |
| 645 | 645 |
///\code |
| 646 | 646 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
| 647 | 647 |
///\endcode |
| 648 | 648 |
/// |
| 649 | 649 |
/// After the copy the \c nr map will contain the mapping from the |
| 650 | 650 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
| 651 | 651 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
| 652 | 652 |
/// to the arcs of the \c from digraph. |
| 653 | 653 |
/// |
| 654 | 654 |
/// \see DigraphCopy |
| 655 | 655 |
template <typename From, typename To> |
| 656 | 656 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
|
| 657 | 657 |
return DigraphCopy<From, To>(from, to); |
| 658 | 658 |
} |
| 659 | 659 |
|
| 660 | 660 |
/// \brief Class to copy a graph. |
| 661 | 661 |
/// |
| 662 | 662 |
/// Class to copy a graph to another graph (duplicate a graph). The |
| 663 | 663 |
/// simplest way of using it is through the \c graphCopy() function. |
| 664 | 664 |
/// |
| 665 | 665 |
/// This class not only make a copy of a graph, but it can create |
| 666 | 666 |
/// references and cross references between the nodes, edges and arcs of |
| 667 | 667 |
/// the two graphs, and it can copy maps for using with the newly created |
| 668 | 668 |
/// graph. |
| 669 | 669 |
/// |
| 670 | 670 |
/// To make a copy from a graph, first an instance of GraphCopy |
| 671 | 671 |
/// should be created, then the data belongs to the graph should |
| 672 | 672 |
/// assigned to copy. In the end, the \c run() member should be |
| 673 | 673 |
/// called. |
| 674 | 674 |
/// |
| 675 | 675 |
/// The next code copies a graph with several data: |
| 676 | 676 |
///\code |
| 677 | 677 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
| 678 | 678 |
/// // Create references for the nodes |
| 679 | 679 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 680 | 680 |
/// cg.nodeRef(nr); |
| 681 | 681 |
/// // Create cross references (inverse) for the edges |
| 682 | 682 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
| 683 | 683 |
/// cg.edgeCrossRef(ecr); |
| 684 | 684 |
/// // Copy an edge map |
| 685 | 685 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
| 686 | 686 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
| 687 | 687 |
/// cg.edgeMap(oemap, nemap); |
| 688 | 688 |
/// // Copy a node |
| 689 | 689 |
/// OrigGraph::Node on; |
| 690 | 690 |
/// NewGraph::Node nn; |
| 691 | 691 |
/// cg.node(on, nn); |
| 692 | 692 |
/// // Execute copying |
| 693 | 693 |
/// cg.run(); |
| 694 | 694 |
///\endcode |
| 695 | 695 |
template <typename From, typename To> |
| 696 | 696 |
class GraphCopy {
|
| 697 | 697 |
private: |
| 698 | 698 |
|
| 699 | 699 |
typedef typename From::Node Node; |
| 700 | 700 |
typedef typename From::NodeIt NodeIt; |
| 701 | 701 |
typedef typename From::Arc Arc; |
| 702 | 702 |
typedef typename From::ArcIt ArcIt; |
| 703 | 703 |
typedef typename From::Edge Edge; |
| 704 | 704 |
typedef typename From::EdgeIt EdgeIt; |
| 705 | 705 |
|
| 706 | 706 |
typedef typename To::Node TNode; |
| 707 | 707 |
typedef typename To::Arc TArc; |
| 708 | 708 |
typedef typename To::Edge TEdge; |
| 709 | 709 |
|
| 710 | 710 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 711 | 711 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
| 712 | 712 |
|
| 713 | 713 |
struct ArcRefMap {
|
| 714 | 714 |
ArcRefMap(const From& from, const To& to, |
| 715 | 715 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
| 716 | 716 |
: _from(from), _to(to), |
| 717 | 717 |
_edge_ref(edge_ref), _node_ref(node_ref) {}
|
| 718 | 718 |
|
| 719 | 719 |
typedef typename From::Arc Key; |
| 720 | 720 |
typedef typename To::Arc Value; |
| 721 | 721 |
|
| 722 | 722 |
Value operator[](const Key& key) const {
|
| 723 | 723 |
bool forward = _from.u(key) != _from.v(key) ? |
| 724 | 724 |
_node_ref[_from.source(key)] == |
| 725 | 725 |
_to.source(_to.direct(_edge_ref[key], true)) : |
| 726 | 726 |
_from.direction(key); |
| 727 | 727 |
return _to.direct(_edge_ref[key], forward); |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
const From& _from; |
| 731 | 731 |
const To& _to; |
| 732 | 732 |
const EdgeRefMap& _edge_ref; |
| 733 | 733 |
const NodeRefMap& _node_ref; |
| 734 | 734 |
}; |
| 735 | 735 |
|
| 736 | 736 |
public: |
| 737 | 737 |
|
| 738 | 738 |
/// \brief Constructor of GraphCopy. |
| 739 | 739 |
/// |
| 740 | 740 |
/// Constructor of GraphCopy for copying the content of the |
| 741 | 741 |
/// \c from graph into the \c to graph. |
| 742 | 742 |
GraphCopy(const From& from, To& to) |
| 743 | 743 |
: _from(from), _to(to) {}
|
| 744 | 744 |
|
| 745 | 745 |
/// \brief Destructor of GraphCopy |
| 746 | 746 |
/// |
| 747 | 747 |
/// Destructor of GraphCopy. |
| 748 | 748 |
~GraphCopy() {
|
| 749 | 749 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 750 | 750 |
delete _node_maps[i]; |
| 751 | 751 |
} |
| 752 | 752 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 753 | 753 |
delete _arc_maps[i]; |
| 754 | 754 |
} |
| 755 | 755 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 756 | 756 |
delete _edge_maps[i]; |
| 757 | 757 |
} |
| 758 | 758 |
} |
| 759 | 759 |
|
| 760 | 760 |
/// \brief Copy the node references into the given map. |
| 761 | 761 |
/// |
| 762 | 762 |
/// This function copies the node references into the given map. |
| 763 | 763 |
/// The parameter should be a map, whose key type is the Node type of |
| 764 | 764 |
/// the source graph, while the value type is the Node type of the |
| 765 | 765 |
/// destination graph. |
| 766 | 766 |
template <typename NodeRef> |
| 767 | 767 |
GraphCopy& nodeRef(NodeRef& map) {
|
| 768 | 768 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
| 769 | 769 |
NodeRefMap, NodeRef>(map)); |
| 770 | 770 |
return *this; |
| 771 | 771 |
} |
| 772 | 772 |
|
| 773 | 773 |
/// \brief Copy the node cross references into the given map. |
| 774 | 774 |
/// |
| 775 | 775 |
/// This function copies the node cross references (reverse references) |
| 776 | 776 |
/// into the given map. The parameter should be a map, whose key type |
| 777 | 777 |
/// is the Node type of the destination graph, while the value type is |
| 778 | 778 |
/// the Node type of the source graph. |
| 779 | 779 |
template <typename NodeCrossRef> |
| 780 | 780 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 781 | 781 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
| 782 | 782 |
NodeRefMap, NodeCrossRef>(map)); |
| 783 | 783 |
return *this; |
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
/// \brief Make a copy of the given node map. |
| 787 | 787 |
/// |
| 788 | 788 |
/// This function makes a copy of the given node map for the newly |
| 789 | 789 |
/// created graph. |
| 790 | 790 |
/// The key type of the new map \c tmap should be the Node type of the |
| 791 | 791 |
/// destination graph, and the key type of the original map \c map |
| 792 | 792 |
/// should be the Node type of the source graph. |
| 793 | 793 |
template <typename FromMap, typename ToMap> |
| 794 | 794 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 795 | 795 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
| 796 | 796 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
| 797 | 797 |
return *this; |
| 798 | 798 |
} |
| 799 | 799 |
|
| 800 | 800 |
/// \brief Make a copy of the given node. |
| 801 | 801 |
/// |
| 802 | 802 |
/// This function makes a copy of the given node. |
| 803 | 803 |
GraphCopy& node(const Node& node, TNode& tnode) {
|
| 804 | 804 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
| 805 | 805 |
NodeRefMap, TNode>(node, tnode)); |
| 806 | 806 |
return *this; |
| 807 | 807 |
} |
| 808 | 808 |
|
| 809 | 809 |
/// \brief Copy the arc references into the given map. |
| 810 | 810 |
/// |
| 811 | 811 |
/// This function copies the arc references into the given map. |
| 812 | 812 |
/// The parameter should be a map, whose key type is the Arc type of |
| 813 | 813 |
/// the source graph, while the value type is the Arc type of the |
| 814 | 814 |
/// destination graph. |
| 815 | 815 |
template <typename ArcRef> |
| 816 | 816 |
GraphCopy& arcRef(ArcRef& map) {
|
| 817 | 817 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
| 818 | 818 |
ArcRefMap, ArcRef>(map)); |
| 819 | 819 |
return *this; |
| 820 | 820 |
} |
| 821 | 821 |
|
| 822 | 822 |
/// \brief Copy the arc cross references into the given map. |
| 823 | 823 |
/// |
| 824 | 824 |
/// This function copies the arc cross references (reverse references) |
| 825 | 825 |
/// into the given map. The parameter should be a map, whose key type |
| 826 | 826 |
/// is the Arc type of the destination graph, while the value type is |
| 827 | 827 |
/// the Arc type of the source graph. |
| 828 | 828 |
template <typename ArcCrossRef> |
| 829 | 829 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 830 | 830 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
| 831 | 831 |
ArcRefMap, ArcCrossRef>(map)); |
| 832 | 832 |
return *this; |
| 833 | 833 |
} |
| 834 | 834 |
|
| 835 | 835 |
/// \brief Make a copy of the given arc map. |
| 836 | 836 |
/// |
| 837 | 837 |
/// This function makes a copy of the given arc map for the newly |
| 838 | 838 |
/// created graph. |
| 839 | 839 |
/// The key type of the new map \c tmap should be the Arc type of the |
| 840 | 840 |
/// destination graph, and the key type of the original map \c map |
| 841 | 841 |
/// should be the Arc type of the source graph. |
| 842 | 842 |
template <typename FromMap, typename ToMap> |
| 843 | 843 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 844 | 844 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
| 845 | 845 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
| 846 | 846 |
return *this; |
| 847 | 847 |
} |
| 848 | 848 |
|
| 849 | 849 |
/// \brief Make a copy of the given arc. |
| 850 | 850 |
/// |
| 851 | 851 |
/// This function makes a copy of the given arc. |
| 852 | 852 |
GraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 853 | 853 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
| 854 | 854 |
ArcRefMap, TArc>(arc, tarc)); |
| 855 | 855 |
return *this; |
| 856 | 856 |
} |
| 857 | 857 |
|
| 858 | 858 |
/// \brief Copy the edge references into the given map. |
| 859 | 859 |
/// |
| 860 | 860 |
/// This function copies the edge references into the given map. |
| 861 | 861 |
/// The parameter should be a map, whose key type is the Edge type of |
| 862 | 862 |
/// the source graph, while the value type is the Edge type of the |
| 863 | 863 |
/// destination graph. |
| 864 | 864 |
template <typename EdgeRef> |
| 865 | 865 |
GraphCopy& edgeRef(EdgeRef& map) {
|
| 866 | 866 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
| 867 | 867 |
EdgeRefMap, EdgeRef>(map)); |
| 868 | 868 |
return *this; |
| 869 | 869 |
} |
| 870 | 870 |
|
| 871 | 871 |
/// \brief Copy the edge cross references into the given map. |
| 872 | 872 |
/// |
| 873 | 873 |
/// This function copies the edge cross references (reverse references) |
| 874 | 874 |
/// into the given map. The parameter should be a map, whose key type |
| 875 | 875 |
/// is the Edge type of the destination graph, while the value type is |
| 876 | 876 |
/// the Edge type of the source graph. |
| 877 | 877 |
template <typename EdgeCrossRef> |
| 878 | 878 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
| 879 | 879 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
| 880 | 880 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
| 881 | 881 |
return *this; |
| 882 | 882 |
} |
| 883 | 883 |
|
| 884 | 884 |
/// \brief Make a copy of the given edge map. |
| 885 | 885 |
/// |
| 886 | 886 |
/// This function makes a copy of the given edge map for the newly |
| 887 | 887 |
/// created graph. |
| 888 | 888 |
/// The key type of the new map \c tmap should be the Edge type of the |
| 889 | 889 |
/// destination graph, and the key type of the original map \c map |
| 890 | 890 |
/// should be the Edge type of the source graph. |
| 891 | 891 |
template <typename FromMap, typename ToMap> |
| 892 | 892 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
|
| 893 | 893 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
| 894 | 894 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
| 895 | 895 |
return *this; |
| 896 | 896 |
} |
| 897 | 897 |
|
| 898 | 898 |
/// \brief Make a copy of the given edge. |
| 899 | 899 |
/// |
| 900 | 900 |
/// This function makes a copy of the given edge. |
| 901 | 901 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) {
|
| 902 | 902 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
| 903 | 903 |
EdgeRefMap, TEdge>(edge, tedge)); |
| 904 | 904 |
return *this; |
| 905 | 905 |
} |
| 906 | 906 |
|
| 907 | 907 |
/// \brief Execute copying. |
| 908 | 908 |
/// |
| 909 | 909 |
/// This function executes the copying of the graph along with the |
| 910 | 910 |
/// copying of the assigned data. |
| 911 | 911 |
void run() {
|
| 912 | 912 |
NodeRefMap nodeRefMap(_from); |
| 913 | 913 |
EdgeRefMap edgeRefMap(_from); |
| 914 | 914 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
| 915 | 915 |
_core_bits::GraphCopySelector<To>:: |
| 916 | 916 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
| 917 | 917 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 918 | 918 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 919 | 919 |
} |
| 920 | 920 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 921 | 921 |
_edge_maps[i]->copy(_from, edgeRefMap); |
| 922 | 922 |
} |
| 923 | 923 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 924 | 924 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 925 | 925 |
} |
| 926 | 926 |
} |
| 927 | 927 |
|
| 928 | 928 |
private: |
| 929 | 929 |
|
| 930 | 930 |
const From& _from; |
| 931 | 931 |
To& _to; |
| 932 | 932 |
|
| 933 | 933 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 934 | 934 |
_node_maps; |
| 935 | 935 |
|
| 936 | 936 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 937 | 937 |
_arc_maps; |
| 938 | 938 |
|
| 939 | 939 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
| 940 | 940 |
_edge_maps; |
| 941 | 941 |
|
| 942 | 942 |
}; |
| 943 | 943 |
|
| 944 | 944 |
/// \brief Copy a graph to another graph. |
| 945 | 945 |
/// |
| 946 | 946 |
/// This function copies a graph to another graph. |
| 947 | 947 |
/// The complete usage of it is detailed in the GraphCopy class, |
| 948 | 948 |
/// but a short example shows a basic work: |
| 949 | 949 |
///\code |
| 950 | 950 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
| 951 | 951 |
///\endcode |
| 952 | 952 |
/// |
| 953 | 953 |
/// After the copy the \c nr map will contain the mapping from the |
| 954 | 954 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
| 955 | 955 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
| 956 | 956 |
/// to the edges of the \c from graph. |
| 957 | 957 |
/// |
| 958 | 958 |
/// \see GraphCopy |
| 959 | 959 |
template <typename From, typename To> |
| 960 | 960 |
GraphCopy<From, To> |
| 961 | 961 |
graphCopy(const From& from, To& to) {
|
| 962 | 962 |
return GraphCopy<From, To>(from, to); |
| 963 | 963 |
} |
| 964 | 964 |
|
| 965 | 965 |
namespace _core_bits {
|
| 966 | 966 |
|
| 967 | 967 |
template <typename Graph, typename Enable = void> |
| 968 | 968 |
struct FindArcSelector {
|
| 969 | 969 |
typedef typename Graph::Node Node; |
| 970 | 970 |
typedef typename Graph::Arc Arc; |
| 971 | 971 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 972 | 972 |
if (e == INVALID) {
|
| 973 | 973 |
g.firstOut(e, u); |
| 974 | 974 |
} else {
|
| 975 | 975 |
g.nextOut(e); |
| 976 | 976 |
} |
| 977 | 977 |
while (e != INVALID && g.target(e) != v) {
|
| 978 | 978 |
g.nextOut(e); |
| 979 | 979 |
} |
| 980 | 980 |
return e; |
| 981 | 981 |
} |
| 982 | 982 |
}; |
| 983 | 983 |
|
| 984 | 984 |
template <typename Graph> |
| 985 | 985 |
struct FindArcSelector< |
| 986 | 986 |
Graph, |
| 987 | 987 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
| 988 | 988 |
{
|
| 989 | 989 |
typedef typename Graph::Node Node; |
| 990 | 990 |
typedef typename Graph::Arc Arc; |
| 991 | 991 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 992 | 992 |
return g.findArc(u, v, prev); |
| 993 | 993 |
} |
| 994 | 994 |
}; |
| 995 | 995 |
} |
| 996 | 996 |
|
| 997 | 997 |
/// \brief Find an arc between two nodes of a digraph. |
| 998 | 998 |
/// |
| 999 | 999 |
/// This function finds an arc from node \c u to node \c v in the |
| 1000 | 1000 |
/// digraph \c g. |
| 1001 | 1001 |
/// |
| 1002 | 1002 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 1003 | 1003 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 1004 | 1004 |
/// the next arc from \c u to \c v after \c prev. |
| 1005 | 1005 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 1006 | 1006 |
/// |
| 1007 | 1007 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 1008 | 1008 |
///\code |
| 1009 | 1009 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
|
| 1010 | 1010 |
/// ... |
| 1011 | 1011 |
/// } |
| 1012 | 1012 |
///\endcode |
| 1013 | 1013 |
/// |
| 1014 | 1014 |
/// \note \ref ConArcIt provides iterator interface for the same |
| 1015 | 1015 |
/// functionality. |
| 1016 | 1016 |
/// |
| 1017 | 1017 |
///\sa ConArcIt |
| 1018 | 1018 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
| 1019 | 1019 |
template <typename Graph> |
| 1020 | 1020 |
inline typename Graph::Arc |
| 1021 | 1021 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 1022 | 1022 |
typename Graph::Arc prev = INVALID) {
|
| 1023 | 1023 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
| 1024 | 1024 |
} |
| 1025 | 1025 |
|
| 1026 | 1026 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
| 1027 | 1027 |
/// |
| 1028 | 1028 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
| 1029 | 1029 |
/// a higher level interface for the \ref findArc() function. You can |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Dfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Dfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct DfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the %DFS paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the %DFS paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///PredMap. |
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 | 67 |
///Instantiates a ProcessedMap. |
| 68 | 68 |
|
| 69 | 69 |
///This function instantiates a ProcessedMap. |
| 70 | 70 |
///\param g is the digraph, to which |
| 71 | 71 |
///we would like to define the ProcessedMap |
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 | 86 |
///Instantiates a ReachedMap. |
| 87 | 87 |
|
| 88 | 88 |
///This function instantiates a ReachedMap. |
| 89 | 89 |
///\param g is the digraph, to which |
| 90 | 90 |
///we would like to define the ReachedMap. |
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 | 101 |
///Instantiates a DistMap. |
| 102 | 102 |
|
| 103 | 103 |
///This function instantiates a DistMap. |
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 | 105 |
///DistMap. |
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%DFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
| 123 | 123 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
| 124 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
| 125 | 125 |
///The default traits class is |
| 126 | 126 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
| 127 | 127 |
///See \ref DfsDefaultTraits for the documentation of |
| 128 | 128 |
///a Dfs traits class. |
| 129 | 129 |
#ifdef DOXYGEN |
| 130 | 130 |
template <typename GR, |
| 131 | 131 |
typename TR> |
| 132 | 132 |
#else |
| 133 | 133 |
template <typename GR=ListDigraph, |
| 134 | 134 |
typename TR=DfsDefaultTraits<GR> > |
| 135 | 135 |
#endif |
| 136 | 136 |
class Dfs {
|
| 137 | 137 |
public: |
| 138 | 138 |
|
| 139 | 139 |
///The type of the digraph the algorithm runs on. |
| 140 | 140 |
typedef typename TR::Digraph Digraph; |
| 141 | 141 |
|
| 142 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
| 143 | 143 |
///DFS paths. |
| 144 | 144 |
typedef typename TR::PredMap PredMap; |
| 145 | 145 |
///The type of the map that stores the distances of the nodes. |
| 146 | 146 |
typedef typename TR::DistMap DistMap; |
| 147 | 147 |
///The type of the map that indicates which nodes are reached. |
| 148 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
| 149 | 149 |
///The type of the map that indicates which nodes are processed. |
| 150 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 151 | 151 |
///The type of the paths. |
| 152 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 153 | 153 |
|
| 154 | 154 |
///The traits class. |
| 155 | 155 |
typedef TR Traits; |
| 156 | 156 |
|
| 157 | 157 |
private: |
| 158 | 158 |
|
| 159 | 159 |
typedef typename Digraph::Node Node; |
| 160 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
| 161 | 161 |
typedef typename Digraph::Arc Arc; |
| 162 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 163 | 163 |
|
| 164 | 164 |
//Pointer to the underlying digraph. |
| 165 | 165 |
const Digraph *G; |
| 166 | 166 |
//Pointer to the map of predecessor arcs. |
| 167 | 167 |
PredMap *_pred; |
| 168 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
| 169 | 169 |
bool local_pred; |
| 170 | 170 |
//Pointer to the map of distances. |
| 171 | 171 |
DistMap *_dist; |
| 172 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
| 173 | 173 |
bool local_dist; |
| 174 | 174 |
//Pointer to the map of reached status of the nodes. |
| 175 | 175 |
ReachedMap *_reached; |
| 176 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
| 177 | 177 |
bool local_reached; |
| 178 | 178 |
//Pointer to the map of processed status of the nodes. |
| 179 | 179 |
ProcessedMap *_processed; |
| 180 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
| 181 | 181 |
bool local_processed; |
| 182 | 182 |
|
| 183 | 183 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 184 | 184 |
int _stack_head; |
| 185 | 185 |
|
| 186 | 186 |
//Creates the maps if necessary. |
| 187 | 187 |
void create_maps() |
| 188 | 188 |
{
|
| 189 | 189 |
if(!_pred) {
|
| 190 | 190 |
local_pred = true; |
| 191 | 191 |
_pred = Traits::createPredMap(*G); |
| 192 | 192 |
} |
| 193 | 193 |
if(!_dist) {
|
| 194 | 194 |
local_dist = true; |
| 195 | 195 |
_dist = Traits::createDistMap(*G); |
| 196 | 196 |
} |
| 197 | 197 |
if(!_reached) {
|
| 198 | 198 |
local_reached = true; |
| 199 | 199 |
_reached = Traits::createReachedMap(*G); |
| 200 | 200 |
} |
| 201 | 201 |
if(!_processed) {
|
| 202 | 202 |
local_processed = true; |
| 203 | 203 |
_processed = Traits::createProcessedMap(*G); |
| 204 | 204 |
} |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
protected: |
| 208 | 208 |
|
| 209 | 209 |
Dfs() {}
|
| 210 | 210 |
|
| 211 | 211 |
public: |
| 212 | 212 |
|
| 213 | 213 |
typedef Dfs Create; |
| 214 | 214 |
|
| 215 | 215 |
///\name Named template parameters |
| 216 | 216 |
|
| 217 | 217 |
///@{
|
| 218 | 218 |
|
| 219 | 219 |
template <class T> |
| 220 | 220 |
struct SetPredMapTraits : public Traits {
|
| 221 | 221 |
typedef T PredMap; |
| 222 | 222 |
static PredMap *createPredMap(const Digraph &) |
| 223 | 223 |
{
|
| 224 | 224 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 225 | 225 |
return 0; // ignore warnings |
| 226 | 226 |
} |
| 227 | 227 |
}; |
| 228 | 228 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 229 | 229 |
///PredMap type. |
| 230 | 230 |
/// |
| 231 | 231 |
///\ref named-templ-param "Named parameter" for setting |
| 232 | 232 |
///PredMap type. |
| 233 | 233 |
template <class T> |
| 234 | 234 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
|
| 235 | 235 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
template <class T> |
| 239 | 239 |
struct SetDistMapTraits : public Traits {
|
| 240 | 240 |
typedef T DistMap; |
| 241 | 241 |
static DistMap *createDistMap(const Digraph &) |
| 242 | 242 |
{
|
| 243 | 243 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 244 | 244 |
return 0; // ignore warnings |
| 245 | 245 |
} |
| 246 | 246 |
}; |
| 247 | 247 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 248 | 248 |
///DistMap type. |
| 249 | 249 |
/// |
| 250 | 250 |
///\ref named-templ-param "Named parameter" for setting |
| 251 | 251 |
///DistMap type. |
| 252 | 252 |
template <class T> |
| 253 | 253 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
|
| 254 | 254 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
| 255 | 255 |
}; |
| 256 | 256 |
|
| 257 | 257 |
template <class T> |
| 258 | 258 |
struct SetReachedMapTraits : public Traits {
|
| 259 | 259 |
typedef T ReachedMap; |
| 260 | 260 |
static ReachedMap *createReachedMap(const Digraph &) |
| 261 | 261 |
{
|
| 262 | 262 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 263 | 263 |
return 0; // ignore warnings |
| 264 | 264 |
} |
| 265 | 265 |
}; |
| 266 | 266 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 267 | 267 |
///ReachedMap type. |
| 268 | 268 |
/// |
| 269 | 269 |
///\ref named-templ-param "Named parameter" for setting |
| 270 | 270 |
///ReachedMap type. |
| 271 | 271 |
template <class T> |
| 272 | 272 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
|
| 273 | 273 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
| 274 | 274 |
}; |
| 275 | 275 |
|
| 276 | 276 |
template <class T> |
| 277 | 277 |
struct SetProcessedMapTraits : public Traits {
|
| 278 | 278 |
typedef T ProcessedMap; |
| 279 | 279 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 280 | 280 |
{
|
| 281 | 281 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 282 | 282 |
return 0; // ignore warnings |
| 283 | 283 |
} |
| 284 | 284 |
}; |
| 285 | 285 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 286 | 286 |
///ProcessedMap type. |
| 287 | 287 |
/// |
| 288 | 288 |
///\ref named-templ-param "Named parameter" for setting |
| 289 | 289 |
///ProcessedMap type. |
| 290 | 290 |
template <class T> |
| 291 | 291 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
|
| 292 | 292 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 293 | 293 |
}; |
| 294 | 294 |
|
| 295 | 295 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 296 | 296 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 297 | 297 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 298 | 298 |
{
|
| 299 | 299 |
return new ProcessedMap(g); |
| 300 | 300 |
} |
| 301 | 301 |
}; |
| 302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 303 | 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 304 | 304 |
/// |
| 305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
| 306 | 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 307 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
| 308 | 308 |
struct SetStandardProcessedMap : |
| 309 | 309 |
public Dfs< Digraph, SetStandardProcessedMapTraits > {
|
| 310 | 310 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 311 | 311 |
}; |
| 312 | 312 |
|
| 313 | 313 |
///@} |
| 314 | 314 |
|
| 315 | 315 |
public: |
| 316 | 316 |
|
| 317 | 317 |
///Constructor. |
| 318 | 318 |
|
| 319 | 319 |
///Constructor. |
| 320 | 320 |
///\param g The digraph the algorithm runs on. |
| 321 | 321 |
Dfs(const Digraph &g) : |
| 322 | 322 |
G(&g), |
| 323 | 323 |
_pred(NULL), local_pred(false), |
| 324 | 324 |
_dist(NULL), local_dist(false), |
| 325 | 325 |
_reached(NULL), local_reached(false), |
| 326 | 326 |
_processed(NULL), local_processed(false) |
| 327 | 327 |
{ }
|
| 328 | 328 |
|
| 329 | 329 |
///Destructor. |
| 330 | 330 |
~Dfs() |
| 331 | 331 |
{
|
| 332 | 332 |
if(local_pred) delete _pred; |
| 333 | 333 |
if(local_dist) delete _dist; |
| 334 | 334 |
if(local_reached) delete _reached; |
| 335 | 335 |
if(local_processed) delete _processed; |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
|
| 340 | 340 |
///Sets the map that stores the predecessor arcs. |
| 341 | 341 |
///If you don't use this function before calling \ref run(), |
| 342 | 342 |
///it will allocate one. The destructor deallocates this |
| 343 | 343 |
///automatically allocated map, of course. |
| 344 | 344 |
///\return <tt> (*this) </tt> |
| 345 | 345 |
Dfs &predMap(PredMap &m) |
| 346 | 346 |
{
|
| 347 | 347 |
if(local_pred) {
|
| 348 | 348 |
delete _pred; |
| 349 | 349 |
local_pred=false; |
| 350 | 350 |
} |
| 351 | 351 |
_pred = &m; |
| 352 | 352 |
return *this; |
| 353 | 353 |
} |
| 354 | 354 |
|
| 355 | 355 |
///Sets the map that indicates which nodes are reached. |
| 356 | 356 |
|
| 357 | 357 |
///Sets the map that indicates which nodes are reached. |
| 358 | 358 |
///If you don't use this function before calling \ref run(), |
| 359 | 359 |
///it will allocate one. The destructor deallocates this |
| 360 | 360 |
///automatically allocated map, of course. |
| 361 | 361 |
///\return <tt> (*this) </tt> |
| 362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
| 363 | 363 |
{
|
| 364 | 364 |
if(local_reached) {
|
| 365 | 365 |
delete _reached; |
| 366 | 366 |
local_reached=false; |
| 367 | 367 |
} |
| 368 | 368 |
_reached = &m; |
| 369 | 369 |
return *this; |
| 370 | 370 |
} |
| 371 | 371 |
|
| 372 | 372 |
///Sets the map that indicates which nodes are processed. |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
///If you don't use this function before calling \ref run(), |
| 376 | 376 |
///it will allocate one. The destructor deallocates this |
| 377 | 377 |
///automatically allocated map, of course. |
| 378 | 378 |
///\return <tt> (*this) </tt> |
| 379 | 379 |
Dfs &processedMap(ProcessedMap &m) |
| 380 | 380 |
{
|
| 381 | 381 |
if(local_processed) {
|
| 382 | 382 |
delete _processed; |
| 383 | 383 |
local_processed=false; |
| 384 | 384 |
} |
| 385 | 385 |
_processed = &m; |
| 386 | 386 |
return *this; |
| 387 | 387 |
} |
| 388 | 388 |
|
| 389 | 389 |
///Sets the map that stores the distances of the nodes. |
| 390 | 390 |
|
| 391 | 391 |
///Sets the map that stores the distances of the nodes calculated by |
| 392 | 392 |
///the algorithm. |
| 393 | 393 |
///If you don't use this function before calling \ref run(), |
| 394 | 394 |
///it will allocate one. The destructor deallocates this |
| 395 | 395 |
///automatically allocated map, of course. |
| 396 | 396 |
///\return <tt> (*this) </tt> |
| 397 | 397 |
Dfs &distMap(DistMap &m) |
| 398 | 398 |
{
|
| 399 | 399 |
if(local_dist) {
|
| 400 | 400 |
delete _dist; |
| 401 | 401 |
local_dist=false; |
| 402 | 402 |
} |
| 403 | 403 |
_dist = &m; |
| 404 | 404 |
return *this; |
| 405 | 405 |
} |
| 406 | 406 |
|
| 407 | 407 |
public: |
| 408 | 408 |
|
| 409 | 409 |
///\name Execution control |
| 410 | 410 |
///The simplest way to execute the algorithm is to use |
| 411 | 411 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
| 412 | 412 |
///\n |
| 413 | 413 |
///If you need more control on the execution, first you must call |
| 414 | 414 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
| 415 | 415 |
///with \ref lemon::Dfs::addSource() "addSource()". |
| 416 | 416 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
| 417 | 417 |
///actual path computation. |
| 418 | 418 |
|
| 419 | 419 |
///@{
|
| 420 | 420 |
|
| 421 | 421 |
///Initializes the internal data structures. |
| 422 | 422 |
|
| 423 | 423 |
///Initializes the internal data structures. |
| 424 | 424 |
/// |
| 425 | 425 |
void init() |
| 426 | 426 |
{
|
| 427 | 427 |
create_maps(); |
| 428 | 428 |
_stack.resize(countNodes(*G)); |
| 429 | 429 |
_stack_head=-1; |
| 430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 431 | 431 |
_pred->set(u,INVALID); |
| 432 | 432 |
_reached->set(u,false); |
| 433 | 433 |
_processed->set(u,false); |
| 434 | 434 |
} |
| 435 | 435 |
} |
| 436 | 436 |
|
| 437 | 437 |
///Adds a new source node. |
| 438 | 438 |
|
| 439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
| 440 | 440 |
/// |
| 441 | 441 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
| 442 | 442 |
///false results.) |
| 443 | 443 |
/// |
| 444 | 444 |
///\warning Distances will be wrong (or at least strange) in case of |
| 445 | 445 |
///multiple sources. |
| 446 | 446 |
void addSource(Node s) |
| 447 | 447 |
{
|
| 448 | 448 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
| 449 | 449 |
if(!(*_reached)[s]) |
| 450 | 450 |
{
|
| 451 | 451 |
_reached->set(s,true); |
| 452 | 452 |
_pred->set(s,INVALID); |
| 453 | 453 |
OutArcIt e(*G,s); |
| 454 | 454 |
if(e!=INVALID) {
|
| 455 | 455 |
_stack[++_stack_head]=e; |
| 456 | 456 |
_dist->set(s,_stack_head); |
| 457 | 457 |
} |
| 458 | 458 |
else {
|
| 459 | 459 |
_processed->set(s,true); |
| 460 | 460 |
_dist->set(s,0); |
| 461 | 461 |
} |
| 462 | 462 |
} |
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
///Processes the next arc. |
| 466 | 466 |
|
| 467 | 467 |
///Processes the next arc. |
| 468 | 468 |
/// |
| 469 | 469 |
///\return The processed arc. |
| 470 | 470 |
/// |
| 471 | 471 |
///\pre The stack must not be empty. |
| 472 | 472 |
Arc processNextArc() |
| 473 | 473 |
{
|
| 474 | 474 |
Node m; |
| 475 | 475 |
Arc e=_stack[_stack_head]; |
| 476 | 476 |
if(!(*_reached)[m=G->target(e)]) {
|
| 477 | 477 |
_pred->set(m,e); |
| 478 | 478 |
_reached->set(m,true); |
| 479 | 479 |
++_stack_head; |
| 480 | 480 |
_stack[_stack_head] = OutArcIt(*G, m); |
| 481 | 481 |
_dist->set(m,_stack_head); |
| 482 | 482 |
} |
| 483 | 483 |
else {
|
| 484 | 484 |
m=G->source(e); |
| 485 | 485 |
++_stack[_stack_head]; |
| 486 | 486 |
} |
| 487 | 487 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
|
| 488 | 488 |
_processed->set(m,true); |
| 489 | 489 |
--_stack_head; |
| 490 | 490 |
if(_stack_head>=0) {
|
| 491 | 491 |
m=G->source(_stack[_stack_head]); |
| 492 | 492 |
++_stack[_stack_head]; |
| 493 | 493 |
} |
| 494 | 494 |
} |
| 495 | 495 |
return e; |
| 496 | 496 |
} |
| 497 | 497 |
|
| 498 | 498 |
///Next arc to be processed. |
| 499 | 499 |
|
| 500 | 500 |
///Next arc to be processed. |
| 501 | 501 |
/// |
| 502 | 502 |
///\return The next arc to be processed or \c INVALID if the stack |
| 503 | 503 |
///is empty. |
| 504 | 504 |
OutArcIt nextArc() const |
| 505 | 505 |
{
|
| 506 | 506 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
| 507 | 507 |
} |
| 508 | 508 |
|
| 509 | 509 |
///\brief Returns \c false if there are nodes |
| 510 | 510 |
///to be processed. |
| 511 | 511 |
/// |
| 512 | 512 |
///Returns \c false if there are nodes |
| 513 | 513 |
///to be processed in the queue (stack). |
| 514 | 514 |
bool emptyQueue() const { return _stack_head<0; }
|
| 515 | 515 |
|
| 516 | 516 |
///Returns the number of the nodes to be processed. |
| 517 | 517 |
|
| 518 | 518 |
///Returns the number of the nodes to be processed in the queue (stack). |
| 519 | 519 |
int queueSize() const { return _stack_head+1; }
|
| 520 | 520 |
|
| 521 | 521 |
///Executes the algorithm. |
| 522 | 522 |
|
| 523 | 523 |
///Executes the algorithm. |
| 524 | 524 |
/// |
| 525 | 525 |
///This method runs the %DFS algorithm from the root node |
| 526 | 526 |
///in order to compute the DFS path to each node. |
| 527 | 527 |
/// |
| 528 | 528 |
/// The algorithm computes |
| 529 | 529 |
///- the %DFS tree, |
| 530 | 530 |
///- the distance of each node from the root in the %DFS tree. |
| 531 | 531 |
/// |
| 532 | 532 |
///\pre init() must be called and a root node should be |
| 533 | 533 |
///added with addSource() before using this function. |
| 534 | 534 |
/// |
| 535 | 535 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
| 536 | 536 |
///\code |
| 537 | 537 |
/// while ( !d.emptyQueue() ) {
|
| 538 | 538 |
/// d.processNextArc(); |
| 539 | 539 |
/// } |
| 540 | 540 |
///\endcode |
| 541 | 541 |
void start() |
| 542 | 542 |
{
|
| 543 | 543 |
while ( !emptyQueue() ) processNextArc(); |
| 544 | 544 |
} |
| 545 | 545 |
|
| 546 | 546 |
///Executes the algorithm until the given target node is reached. |
| 547 | 547 |
|
| 548 | 548 |
///Executes the algorithm until the given target node is reached. |
| 549 | 549 |
/// |
| 550 | 550 |
///This method runs the %DFS algorithm from the root node |
| 551 | 551 |
///in order to compute the DFS path to \c t. |
| 552 | 552 |
/// |
| 553 | 553 |
///The algorithm computes |
| 554 | 554 |
///- the %DFS path to \c t, |
| 555 | 555 |
///- the distance of \c t from the root in the %DFS tree. |
| 556 | 556 |
/// |
| 557 | 557 |
///\pre init() must be called and a root node should be |
| 558 | 558 |
///added with addSource() before using this function. |
| 559 | 559 |
void start(Node t) |
| 560 | 560 |
{
|
| 561 | 561 |
while ( !emptyQueue() && !(*_reached)[t] ) |
| 562 | 562 |
processNextArc(); |
| 563 | 563 |
} |
| 564 | 564 |
|
| 565 | 565 |
///Executes the algorithm until a condition is met. |
| 566 | 566 |
|
| 567 | 567 |
///Executes the algorithm until a condition is met. |
| 568 | 568 |
/// |
| 569 | 569 |
///This method runs the %DFS algorithm from the root node |
| 570 | 570 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
| 571 | 571 |
/// |
| 572 | 572 |
///\param am A \c bool (or convertible) arc map. The algorithm |
| 573 | 573 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
| 574 | 574 |
/// |
| 575 | 575 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
| 576 | 576 |
///\c INVALID if no such arc was found. |
| 577 | 577 |
/// |
| 578 | 578 |
///\pre init() must be called and a root node should be |
| 579 | 579 |
///added with addSource() before using this function. |
| 580 | 580 |
/// |
| 581 | 581 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
| 582 | 582 |
///not a node map. |
| 583 | 583 |
template<class ArcBoolMap> |
| 584 | 584 |
Arc start(const ArcBoolMap &am) |
| 585 | 585 |
{
|
| 586 | 586 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
| 587 | 587 |
processNextArc(); |
| 588 | 588 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
| 589 | 589 |
} |
| 590 | 590 |
|
| 591 | 591 |
///Runs the algorithm from the given source node. |
| 592 | 592 |
|
| 593 | 593 |
///This method runs the %DFS algorithm from node \c s |
| 594 | 594 |
///in order to compute the DFS path to each node. |
| 595 | 595 |
/// |
| 596 | 596 |
///The algorithm computes |
| 597 | 597 |
///- the %DFS tree, |
| 598 | 598 |
///- the distance of each node from the root in the %DFS tree. |
| 599 | 599 |
/// |
| 600 | 600 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
| 601 | 601 |
///\code |
| 602 | 602 |
/// d.init(); |
| 603 | 603 |
/// d.addSource(s); |
| 604 | 604 |
/// d.start(); |
| 605 | 605 |
///\endcode |
| 606 | 606 |
void run(Node s) {
|
| 607 | 607 |
init(); |
| 608 | 608 |
addSource(s); |
| 609 | 609 |
start(); |
| 610 | 610 |
} |
| 611 | 611 |
|
| 612 | 612 |
///Finds the %DFS path between \c s and \c t. |
| 613 | 613 |
|
| 614 | 614 |
///This method runs the %DFS algorithm from node \c s |
| 615 | 615 |
///in order to compute the DFS path to node \c t |
| 616 | 616 |
///(it stops searching when \c t is processed) |
| 617 | 617 |
/// |
| 618 | 618 |
///\return \c true if \c t is reachable form \c s. |
| 619 | 619 |
/// |
| 620 | 620 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
| 621 | 621 |
///just a shortcut of the following code. |
| 622 | 622 |
///\code |
| 623 | 623 |
/// d.init(); |
| 624 | 624 |
/// d.addSource(s); |
| 625 | 625 |
/// d.start(t); |
| 626 | 626 |
///\endcode |
| 627 | 627 |
bool run(Node s,Node t) {
|
| 628 | 628 |
init(); |
| 629 | 629 |
addSource(s); |
| 630 | 630 |
start(t); |
| 631 | 631 |
return reached(t); |
| 632 | 632 |
} |
| 633 | 633 |
|
| 634 | 634 |
///Runs the algorithm to visit all nodes in the digraph. |
| 635 | 635 |
|
| 636 | 636 |
///This method runs the %DFS algorithm in order to compute the |
| 637 | 637 |
///%DFS path to each node. |
| 638 | 638 |
/// |
| 639 | 639 |
///The algorithm computes |
| 640 | 640 |
///- the %DFS tree, |
| 641 | 641 |
///- the distance of each node from the root in the %DFS tree. |
| 642 | 642 |
/// |
| 643 | 643 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
| 644 | 644 |
///\code |
| 645 | 645 |
/// d.init(); |
| 646 | 646 |
/// for (NodeIt n(digraph); n != INVALID; ++n) {
|
| 647 | 647 |
/// if (!d.reached(n)) {
|
| 648 | 648 |
/// d.addSource(n); |
| 649 | 649 |
/// d.start(); |
| 650 | 650 |
/// } |
| 651 | 651 |
/// } |
| 652 | 652 |
///\endcode |
| 653 | 653 |
void run() {
|
| 654 | 654 |
init(); |
| 655 | 655 |
for (NodeIt it(*G); it != INVALID; ++it) {
|
| 656 | 656 |
if (!reached(it)) {
|
| 657 | 657 |
addSource(it); |
| 658 | 658 |
start(); |
| 659 | 659 |
} |
| 660 | 660 |
} |
| 661 | 661 |
} |
| 662 | 662 |
|
| 663 | 663 |
///@} |
| 664 | 664 |
|
| 665 | 665 |
///\name Query Functions |
| 666 | 666 |
///The result of the %DFS algorithm can be obtained using these |
| 667 | 667 |
///functions.\n |
| 668 | 668 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
| 669 | 669 |
///"start()" must be called before using them. |
| 670 | 670 |
|
| 671 | 671 |
///@{
|
| 672 | 672 |
|
| 673 | 673 |
///The DFS path to a node. |
| 674 | 674 |
|
| 675 | 675 |
///Returns the DFS path to a node. |
| 676 | 676 |
/// |
| 677 | 677 |
///\warning \c t should be reachable from the root. |
| 678 | 678 |
/// |
| 679 | 679 |
///\pre Either \ref run() or \ref start() must be called before |
| 680 | 680 |
///using this function. |
| 681 | 681 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 682 | 682 |
|
| 683 | 683 |
///The distance of a node from the root. |
| 684 | 684 |
|
| 685 | 685 |
///Returns the distance of a node from the root. |
| 686 | 686 |
/// |
| 687 | 687 |
///\warning If node \c v is not reachable from the root, then |
| 688 | 688 |
///the return value of this function is undefined. |
| 689 | 689 |
/// |
| 690 | 690 |
///\pre Either \ref run() or \ref start() must be called before |
| 691 | 691 |
///using this function. |
| 692 | 692 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 693 | 693 |
|
| 694 | 694 |
///Returns the 'previous arc' of the %DFS tree for a node. |
| 695 | 695 |
|
| 696 | 696 |
///This function returns the 'previous arc' of the %DFS tree for the |
| 697 | 697 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
| 698 | 698 |
///root to \c v. It is \c INVALID |
| 699 | 699 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 700 | 700 |
/// |
| 701 | 701 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 702 | 702 |
///\ref predNode(). |
| 703 | 703 |
/// |
| 704 | 704 |
///\pre Either \ref run() or \ref start() must be called before using |
| 705 | 705 |
///this function. |
| 706 | 706 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 707 | 707 |
|
| 708 | 708 |
///Returns the 'previous node' of the %DFS tree. |
| 709 | 709 |
|
| 710 | 710 |
///This function returns the 'previous node' of the %DFS |
| 711 | 711 |
///tree for the node \c v, i.e. it returns the last but one node |
| 712 | 712 |
///from a %DFS path from the root to \c v. It is \c INVALID |
| 713 | 713 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 714 | 714 |
/// |
| 715 | 715 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 716 | 716 |
///\ref predArc(). |
| 717 | 717 |
/// |
| 718 | 718 |
///\pre Either \ref run() or \ref start() must be called before |
| 719 | 719 |
///using this function. |
| 720 | 720 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
| 721 | 721 |
G->source((*_pred)[v]); } |
| 722 | 722 |
|
| 723 | 723 |
///\brief Returns a const reference to the node map that stores the |
| 724 | 724 |
///distances of the nodes. |
| 725 | 725 |
/// |
| 726 | 726 |
///Returns a const reference to the node map that stores the |
| 727 | 727 |
///distances of the nodes calculated by the algorithm. |
| 728 | 728 |
/// |
| 729 | 729 |
///\pre Either \ref run() or \ref init() |
| 730 | 730 |
///must be called before using this function. |
| 731 | 731 |
const DistMap &distMap() const { return *_dist;}
|
| 732 | 732 |
|
| 733 | 733 |
///\brief Returns a const reference to the node map that stores the |
| 734 | 734 |
///predecessor arcs. |
| 735 | 735 |
/// |
| 736 | 736 |
///Returns a const reference to the node map that stores the predecessor |
| 737 | 737 |
///arcs, which form the DFS tree. |
| 738 | 738 |
/// |
| 739 | 739 |
///\pre Either \ref run() or \ref init() |
| 740 | 740 |
///must be called before using this function. |
| 741 | 741 |
const PredMap &predMap() const { return *_pred;}
|
| 742 | 742 |
|
| 743 | 743 |
///Checks if a node is reachable from the root(s). |
| 744 | 744 |
|
| 745 | 745 |
///Returns \c true if \c v is reachable from the root(s). |
| 746 | 746 |
///\pre Either \ref run() or \ref start() |
| 747 | 747 |
///must be called before using this function. |
| 748 | 748 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 749 | 749 |
|
| 750 | 750 |
///@} |
| 751 | 751 |
}; |
| 752 | 752 |
|
| 753 | 753 |
///Default traits class of dfs() function. |
| 754 | 754 |
|
| 755 | 755 |
///Default traits class of dfs() function. |
| 756 | 756 |
///\tparam GR Digraph type. |
| 757 | 757 |
template<class GR> |
| 758 | 758 |
struct DfsWizardDefaultTraits |
| 759 | 759 |
{
|
| 760 | 760 |
///The type of the digraph the algorithm runs on. |
| 761 | 761 |
typedef GR Digraph; |
| 762 | 762 |
|
| 763 | 763 |
///\brief The type of the map that stores the predecessor |
| 764 | 764 |
///arcs of the %DFS paths. |
| 765 | 765 |
/// |
| 766 | 766 |
///The type of the map that stores the predecessor |
| 767 | 767 |
///arcs of the %DFS paths. |
| 768 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 769 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 770 | 770 |
///Instantiates a PredMap. |
| 771 | 771 |
|
| 772 | 772 |
///This function instantiates a PredMap. |
| 773 | 773 |
///\param g is the digraph, to which we would like to define the |
| 774 | 774 |
///PredMap. |
| 775 | 775 |
static PredMap *createPredMap(const Digraph &g) |
| 776 | 776 |
{
|
| 777 | 777 |
return new PredMap(g); |
| 778 | 778 |
} |
| 779 | 779 |
|
| 780 | 780 |
///The type of the map that indicates which nodes are processed. |
| 781 | 781 |
|
| 782 | 782 |
///The type of the map that indicates which nodes are processed. |
| 783 | 783 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 784 | 784 |
///By default it is a NullMap. |
| 785 | 785 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 786 | 786 |
///Instantiates a ProcessedMap. |
| 787 | 787 |
|
| 788 | 788 |
///This function instantiates a ProcessedMap. |
| 789 | 789 |
///\param g is the digraph, to which |
| 790 | 790 |
///we would like to define the ProcessedMap. |
| 791 | 791 |
#ifdef DOXYGEN |
| 792 | 792 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 793 | 793 |
#else |
| 794 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 795 | 795 |
#endif |
| 796 | 796 |
{
|
| 797 | 797 |
return new ProcessedMap(); |
| 798 | 798 |
} |
| 799 | 799 |
|
| 800 | 800 |
///The type of the map that indicates which nodes are reached. |
| 801 | 801 |
|
| 802 | 802 |
///The type of the map that indicates which nodes are reached. |
| 803 | 803 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 804 | 804 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 805 | 805 |
///Instantiates a ReachedMap. |
| 806 | 806 |
|
| 807 | 807 |
///This function instantiates a ReachedMap. |
| 808 | 808 |
///\param g is the digraph, to which |
| 809 | 809 |
///we would like to define the ReachedMap. |
| 810 | 810 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 811 | 811 |
{
|
| 812 | 812 |
return new ReachedMap(g); |
| 813 | 813 |
} |
| 814 | 814 |
|
| 815 | 815 |
///The type of the map that stores the distances of the nodes. |
| 816 | 816 |
|
| 817 | 817 |
///The type of the map that stores the distances of the nodes. |
| 818 | 818 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 819 | 819 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 820 | 820 |
///Instantiates a DistMap. |
| 821 | 821 |
|
| 822 | 822 |
///This function instantiates a DistMap. |
| 823 | 823 |
///\param g is the digraph, to which we would like to define |
| 824 | 824 |
///the DistMap |
| 825 | 825 |
static DistMap *createDistMap(const Digraph &g) |
| 826 | 826 |
{
|
| 827 | 827 |
return new DistMap(g); |
| 828 | 828 |
} |
| 829 | 829 |
|
| 830 | 830 |
///The type of the DFS paths. |
| 831 | 831 |
|
| 832 | 832 |
///The type of the DFS paths. |
| 833 | 833 |
///It must meet the \ref concepts::Path "Path" concept. |
| 834 | 834 |
typedef lemon::Path<Digraph> Path; |
| 835 | 835 |
}; |
| 836 | 836 |
|
| 837 | 837 |
/// Default traits class used by DfsWizard |
| 838 | 838 |
|
| 839 | 839 |
/// To make it easier to use Dfs algorithm |
| 840 | 840 |
/// we have created a wizard class. |
| 841 | 841 |
/// This \ref DfsWizard class needs default traits, |
| 842 | 842 |
/// as well as the \ref Dfs class. |
| 843 | 843 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
| 844 | 844 |
/// \ref DfsWizard class. |
| 845 | 845 |
template<class GR> |
| 846 | 846 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
| 847 | 847 |
{
|
| 848 | 848 |
|
| 849 | 849 |
typedef DfsWizardDefaultTraits<GR> Base; |
| 850 | 850 |
protected: |
| 851 | 851 |
//The type of the nodes in the digraph. |
| 852 | 852 |
typedef typename Base::Digraph::Node Node; |
| 853 | 853 |
|
| 854 | 854 |
//Pointer to the digraph the algorithm runs on. |
| 855 | 855 |
void *_g; |
| 856 | 856 |
//Pointer to the map of reached nodes. |
| 857 | 857 |
void *_reached; |
| 858 | 858 |
//Pointer to the map of processed nodes. |
| 859 | 859 |
void *_processed; |
| 860 | 860 |
//Pointer to the map of predecessors arcs. |
| 861 | 861 |
void *_pred; |
| 862 | 862 |
//Pointer to the map of distances. |
| 863 | 863 |
void *_dist; |
| 864 | 864 |
//Pointer to the DFS path to the target node. |
| 865 | 865 |
void *_path; |
| 866 | 866 |
//Pointer to the distance of the target node. |
| 867 | 867 |
int *_di; |
| 868 | 868 |
|
| 869 | 869 |
public: |
| 870 | 870 |
/// Constructor. |
| 871 | 871 |
|
| 872 | 872 |
/// This constructor does not require parameters, therefore it initiates |
| 873 | 873 |
/// all of the attributes to \c 0. |
| 874 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 875 | 875 |
_dist(0), _path(0), _di(0) {}
|
| 876 | 876 |
|
| 877 | 877 |
/// Constructor. |
| 878 | 878 |
|
| 879 | 879 |
/// This constructor requires one parameter, |
| 880 | 880 |
/// others are initiated to \c 0. |
| 881 | 881 |
/// \param g The digraph the algorithm runs on. |
| 882 | 882 |
DfsWizardBase(const GR &g) : |
| 883 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 884 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 885 | 885 |
|
| 886 | 886 |
}; |
| 887 | 887 |
|
| 888 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
| 889 | 889 |
|
| 890 | 890 |
/// This auxiliary class is created to implement the |
| 891 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
| 892 | 892 |
/// It does not have own \ref run() method, it uses the functions |
| 893 | 893 |
/// and features of the plain \ref Dfs. |
| 894 | 894 |
/// |
| 895 | 895 |
/// This class should only be used through the \ref dfs() function, |
| 896 | 896 |
/// which makes it easier to use the algorithm. |
| 897 | 897 |
template<class TR> |
| 898 | 898 |
class DfsWizard : public TR |
| 899 | 899 |
{
|
| 900 | 900 |
typedef TR Base; |
| 901 | 901 |
|
| 902 | 902 |
///The type of the digraph the algorithm runs on. |
| 903 | 903 |
typedef typename TR::Digraph Digraph; |
| 904 | 904 |
|
| 905 | 905 |
typedef typename Digraph::Node Node; |
| 906 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
| 907 | 907 |
typedef typename Digraph::Arc Arc; |
| 908 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 909 | 909 |
|
| 910 | 910 |
///\brief The type of the map that stores the predecessor |
| 911 | 911 |
///arcs of the DFS paths. |
| 912 | 912 |
typedef typename TR::PredMap PredMap; |
| 913 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
| 914 | 914 |
typedef typename TR::DistMap DistMap; |
| 915 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
| 916 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
| 917 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
| 918 | 918 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 919 | 919 |
///The type of the DFS paths |
| 920 | 920 |
typedef typename TR::Path Path; |
| 921 | 921 |
|
| 922 | 922 |
public: |
| 923 | 923 |
|
| 924 | 924 |
/// Constructor. |
| 925 | 925 |
DfsWizard() : TR() {}
|
| 926 | 926 |
|
| 927 | 927 |
/// Constructor that requires parameters. |
| 928 | 928 |
|
| 929 | 929 |
/// Constructor that requires parameters. |
| 930 | 930 |
/// These parameters will be the default values for the traits class. |
| 931 | 931 |
/// \param g The digraph the algorithm runs on. |
| 932 | 932 |
DfsWizard(const Digraph &g) : |
| 933 | 933 |
TR(g) {}
|
| 934 | 934 |
|
| 935 | 935 |
///Copy constructor |
| 936 | 936 |
DfsWizard(const TR &b) : TR(b) {}
|
| 937 | 937 |
|
| 938 | 938 |
~DfsWizard() {}
|
| 939 | 939 |
|
| 940 | 940 |
///Runs DFS algorithm from the given source node. |
| 941 | 941 |
|
| 942 | 942 |
///This method runs DFS algorithm from node \c s |
| 943 | 943 |
///in order to compute the DFS path to each node. |
| 944 | 944 |
void run(Node s) |
| 945 | 945 |
{
|
| 946 | 946 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 947 | 947 |
if (Base::_pred) |
| 948 | 948 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 949 | 949 |
if (Base::_dist) |
| 950 | 950 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 951 | 951 |
if (Base::_reached) |
| 952 | 952 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 953 | 953 |
if (Base::_processed) |
| 954 | 954 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 955 | 955 |
if (s!=INVALID) |
| 956 | 956 |
alg.run(s); |
| 957 | 957 |
else |
| 958 | 958 |
alg.run(); |
| 959 | 959 |
} |
| 960 | 960 |
|
| 961 | 961 |
///Finds the DFS path between \c s and \c t. |
| 962 | 962 |
|
| 963 | 963 |
///This method runs DFS algorithm from node \c s |
| 964 | 964 |
///in order to compute the DFS path to node \c t |
| 965 | 965 |
///(it stops searching when \c t is processed). |
| 966 | 966 |
/// |
| 967 | 967 |
///\return \c true if \c t is reachable form \c s. |
| 968 | 968 |
bool run(Node s, Node t) |
| 969 | 969 |
{
|
| 970 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 971 | 971 |
if (Base::_pred) |
| 972 | 972 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 973 | 973 |
if (Base::_dist) |
| 974 | 974 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 975 | 975 |
if (Base::_reached) |
| 976 | 976 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 977 | 977 |
if (Base::_processed) |
| 978 | 978 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 979 | 979 |
alg.run(s,t); |
| 980 | 980 |
if (Base::_path) |
| 981 | 981 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 982 | 982 |
if (Base::_di) |
| 983 | 983 |
*Base::_di = alg.dist(t); |
| 984 | 984 |
return alg.reached(t); |
| 985 | 985 |
} |
| 986 | 986 |
|
| 987 | 987 |
///Runs DFS algorithm to visit all nodes in the digraph. |
| 988 | 988 |
|
| 989 | 989 |
///This method runs DFS algorithm in order to compute |
| 990 | 990 |
///the DFS path to each node. |
| 991 | 991 |
void run() |
| 992 | 992 |
{
|
| 993 | 993 |
run(INVALID); |
| 994 | 994 |
} |
| 995 | 995 |
|
| 996 | 996 |
template<class T> |
| 997 | 997 |
struct SetPredMapBase : public Base {
|
| 998 | 998 |
typedef T PredMap; |
| 999 | 999 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1000 | 1000 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1001 | 1001 |
}; |
| 1002 | 1002 |
///\brief \ref named-func-param "Named parameter" |
| 1003 | 1003 |
///for setting PredMap object. |
| 1004 | 1004 |
/// |
| 1005 | 1005 |
///\ref named-func-param "Named parameter" |
| 1006 | 1006 |
///for setting PredMap object. |
| 1007 | 1007 |
template<class T> |
| 1008 | 1008 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1009 | 1009 |
{
|
| 1010 | 1010 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1011 | 1011 |
return DfsWizard<SetPredMapBase<T> >(*this); |
| 1012 | 1012 |
} |
| 1013 | 1013 |
|
| 1014 | 1014 |
template<class T> |
| 1015 | 1015 |
struct SetReachedMapBase : public Base {
|
| 1016 | 1016 |
typedef T ReachedMap; |
| 1017 | 1017 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1018 | 1018 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1019 | 1019 |
}; |
| 1020 | 1020 |
///\brief \ref named-func-param "Named parameter" |
| 1021 | 1021 |
///for setting ReachedMap object. |
| 1022 | 1022 |
/// |
| 1023 | 1023 |
/// \ref named-func-param "Named parameter" |
| 1024 | 1024 |
///for setting ReachedMap object. |
| 1025 | 1025 |
template<class T> |
| 1026 | 1026 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1027 | 1027 |
{
|
| 1028 | 1028 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1029 | 1029 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
| 20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include<iostream> |
| 23 | 23 |
#include<fstream> |
| 24 | 24 |
#include<sstream> |
| 25 | 25 |
#include<algorithm> |
| 26 | 26 |
#include<vector> |
| 27 | 27 |
|
| 28 | 28 |
#ifndef WIN32 |
| 29 | 29 |
#include<sys/time.h> |
| 30 | 30 |
#include<ctime> |
| 31 | 31 |
#else |
| 32 | 32 |
#include<lemon/bits/windows.h> |
| 33 | 33 |
#endif |
| 34 | 34 |
|
| 35 | 35 |
#include<lemon/math.h> |
| 36 | 36 |
#include<lemon/core.h> |
| 37 | 37 |
#include<lemon/dim2.h> |
| 38 | 38 |
#include<lemon/maps.h> |
| 39 | 39 |
#include<lemon/color.h> |
| 40 | 40 |
#include<lemon/bits/bezier.h> |
| 41 | 41 |
#include<lemon/error.h> |
| 42 | 42 |
|
| 43 | 43 |
|
| 44 | 44 |
///\ingroup eps_io |
| 45 | 45 |
///\file |
| 46 | 46 |
///\brief A well configurable tool for visualizing graphs |
| 47 | 47 |
|
| 48 | 48 |
namespace lemon {
|
| 49 | 49 |
|
| 50 | 50 |
namespace _graph_to_eps_bits {
|
| 51 | 51 |
template<class MT> |
| 52 | 52 |
class _NegY {
|
| 53 | 53 |
public: |
| 54 | 54 |
typedef typename MT::Key Key; |
| 55 | 55 |
typedef typename MT::Value Value; |
| 56 | 56 |
const MT ↦ |
| 57 | 57 |
int yscale; |
| 58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
|
| 59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
|
| 60 | 60 |
}; |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 | 63 |
///Default traits class of GraphToEps |
| 64 | 64 |
|
| 65 | 65 |
///Default traits class of \ref GraphToEps. |
| 66 | 66 |
/// |
| 67 | 67 |
///\c G is the type of the underlying graph. |
| 68 | 68 |
template<class G> |
| 69 | 69 |
struct DefaultGraphToEpsTraits |
| 70 | 70 |
{
|
| 71 | 71 |
typedef G Graph; |
| 72 | 72 |
typedef typename Graph::Node Node; |
| 73 | 73 |
typedef typename Graph::NodeIt NodeIt; |
| 74 | 74 |
typedef typename Graph::Arc Arc; |
| 75 | 75 |
typedef typename Graph::ArcIt ArcIt; |
| 76 | 76 |
typedef typename Graph::InArcIt InArcIt; |
| 77 | 77 |
typedef typename Graph::OutArcIt OutArcIt; |
| 78 | 78 |
|
| 79 | 79 |
|
| 80 | 80 |
const Graph &g; |
| 81 | 81 |
|
| 82 | 82 |
std::ostream& os; |
| 83 | 83 |
|
| 84 | 84 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
| 85 | 85 |
CoordsMapType _coords; |
| 86 | 86 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
| 87 | 87 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
| 88 | 88 |
|
| 89 | 89 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
| 90 | 90 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
| 91 | 91 |
|
| 92 | 92 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
| 93 | 93 |
|
| 94 | 94 |
double _arcWidthScale; |
| 95 | 95 |
|
| 96 | 96 |
double _nodeScale; |
| 97 | 97 |
double _xBorder, _yBorder; |
| 98 | 98 |
double _scale; |
| 99 | 99 |
double _nodeBorderQuotient; |
| 100 | 100 |
|
| 101 | 101 |
bool _drawArrows; |
| 102 | 102 |
double _arrowLength, _arrowWidth; |
| 103 | 103 |
|
| 104 | 104 |
bool _showNodes, _showArcs; |
| 105 | 105 |
|
| 106 | 106 |
bool _enableParallel; |
| 107 | 107 |
double _parArcDist; |
| 108 | 108 |
|
| 109 | 109 |
bool _showNodeText; |
| 110 | 110 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
| 111 | 111 |
double _nodeTextSize; |
| 112 | 112 |
|
| 113 | 113 |
bool _showNodePsText; |
| 114 | 114 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
| 115 | 115 |
char *_nodePsTextsPreamble; |
| 116 | 116 |
|
| 117 | 117 |
bool _undirected; |
| 118 | 118 |
|
| 119 | 119 |
bool _pleaseRemoveOsStream; |
| 120 | 120 |
|
| 121 | 121 |
bool _scaleToA4; |
| 122 | 122 |
|
| 123 | 123 |
std::string _title; |
| 124 | 124 |
std::string _copyright; |
| 125 | 125 |
|
| 126 | 126 |
enum NodeTextColorType |
| 127 | 127 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
|
| 128 | 128 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
| 129 | 129 |
|
| 130 | 130 |
bool _autoNodeScale; |
| 131 | 131 |
bool _autoArcWidthScale; |
| 132 | 132 |
|
| 133 | 133 |
bool _absoluteNodeSizes; |
| 134 | 134 |
bool _absoluteArcWidths; |
| 135 | 135 |
|
| 136 | 136 |
bool _negY; |
| 137 | 137 |
|
| 138 | 138 |
bool _preScale; |
| 139 | 139 |
///Constructor |
| 140 | 140 |
|
| 141 | 141 |
///Constructor |
| 142 | 142 |
///\param _g Reference to the graph to be printed. |
| 143 | 143 |
///\param _os Reference to the output stream. |
| 144 | 144 |
///\param _os Reference to the output stream. |
| 145 | 145 |
///By default it is <tt>std::cout</tt>. |
| 146 | 146 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
| 147 | 147 |
///will be explicitly deallocated by the destructor. |
| 148 | 148 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
| 149 | 149 |
bool _pros=false) : |
| 150 | 150 |
g(_g), os(_os), |
| 151 | 151 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
| 152 | 152 |
_nodeColors(WHITE), _arcColors(BLACK), |
| 153 | 153 |
_arcWidths(1.0), _arcWidthScale(0.003), |
| 154 | 154 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
| 155 | 155 |
_nodeBorderQuotient(.1), |
| 156 | 156 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
| 157 | 157 |
_showNodes(true), _showArcs(true), |
| 158 | 158 |
_enableParallel(false), _parArcDist(1), |
| 159 | 159 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
| 160 | 160 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
| 161 | 161 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
| 162 | 162 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
| 163 | 163 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
| 164 | 164 |
_autoNodeScale(false), |
| 165 | 165 |
_autoArcWidthScale(false), |
| 166 | 166 |
_absoluteNodeSizes(false), |
| 167 | 167 |
_absoluteArcWidths(false), |
| 168 | 168 |
_negY(false), |
| 169 | 169 |
_preScale(true) |
| 170 | 170 |
{}
|
| 171 | 171 |
}; |
| 172 | 172 |
|
| 173 | 173 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
| 174 | 174 |
|
| 175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
| 176 | 176 |
/// |
| 177 | 177 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
| 178 | 178 |
template<class T> class GraphToEps : public T |
| 179 | 179 |
{
|
| 180 | 180 |
// Can't believe it is required by the C++ standard |
| 181 | 181 |
using T::g; |
| 182 | 182 |
using T::os; |
| 183 | 183 |
|
| 184 | 184 |
using T::_coords; |
| 185 | 185 |
using T::_nodeSizes; |
| 186 | 186 |
using T::_nodeShapes; |
| 187 | 187 |
using T::_nodeColors; |
| 188 | 188 |
using T::_arcColors; |
| 189 | 189 |
using T::_arcWidths; |
| 190 | 190 |
|
| 191 | 191 |
using T::_arcWidthScale; |
| 192 | 192 |
using T::_nodeScale; |
| 193 | 193 |
using T::_xBorder; |
| 194 | 194 |
using T::_yBorder; |
| 195 | 195 |
using T::_scale; |
| 196 | 196 |
using T::_nodeBorderQuotient; |
| 197 | 197 |
|
| 198 | 198 |
using T::_drawArrows; |
| 199 | 199 |
using T::_arrowLength; |
| 200 | 200 |
using T::_arrowWidth; |
| 201 | 201 |
|
| 202 | 202 |
using T::_showNodes; |
| 203 | 203 |
using T::_showArcs; |
| 204 | 204 |
|
| 205 | 205 |
using T::_enableParallel; |
| 206 | 206 |
using T::_parArcDist; |
| 207 | 207 |
|
| 208 | 208 |
using T::_showNodeText; |
| 209 | 209 |
using T::_nodeTexts; |
| 210 | 210 |
using T::_nodeTextSize; |
| 211 | 211 |
|
| 212 | 212 |
using T::_showNodePsText; |
| 213 | 213 |
using T::_nodePsTexts; |
| 214 | 214 |
using T::_nodePsTextsPreamble; |
| 215 | 215 |
|
| 216 | 216 |
using T::_undirected; |
| 217 | 217 |
|
| 218 | 218 |
using T::_pleaseRemoveOsStream; |
| 219 | 219 |
|
| 220 | 220 |
using T::_scaleToA4; |
| 221 | 221 |
|
| 222 | 222 |
using T::_title; |
| 223 | 223 |
using T::_copyright; |
| 224 | 224 |
|
| 225 | 225 |
using T::NodeTextColorType; |
| 226 | 226 |
using T::CUST_COL; |
| 227 | 227 |
using T::DIST_COL; |
| 228 | 228 |
using T::DIST_BW; |
| 229 | 229 |
using T::_nodeTextColorType; |
| 230 | 230 |
using T::_nodeTextColors; |
| 231 | 231 |
|
| 232 | 232 |
using T::_autoNodeScale; |
| 233 | 233 |
using T::_autoArcWidthScale; |
| 234 | 234 |
|
| 235 | 235 |
using T::_absoluteNodeSizes; |
| 236 | 236 |
using T::_absoluteArcWidths; |
| 237 | 237 |
|
| 238 | 238 |
|
| 239 | 239 |
using T::_negY; |
| 240 | 240 |
using T::_preScale; |
| 241 | 241 |
|
| 242 | 242 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
| 243 | 243 |
|
| 244 | 244 |
typedef typename T::Graph Graph; |
| 245 | 245 |
typedef typename Graph::Node Node; |
| 246 | 246 |
typedef typename Graph::NodeIt NodeIt; |
| 247 | 247 |
typedef typename Graph::Arc Arc; |
| 248 | 248 |
typedef typename Graph::ArcIt ArcIt; |
| 249 | 249 |
typedef typename Graph::InArcIt InArcIt; |
| 250 | 250 |
typedef typename Graph::OutArcIt OutArcIt; |
| 251 | 251 |
|
| 252 | 252 |
static const int INTERPOL_PREC; |
| 253 | 253 |
static const double A4HEIGHT; |
| 254 | 254 |
static const double A4WIDTH; |
| 255 | 255 |
static const double A4BORDER; |
| 256 | 256 |
|
| 257 | 257 |
bool dontPrint; |
| 258 | 258 |
|
| 259 | 259 |
public: |
| 260 | 260 |
///Node shapes |
| 261 | 261 |
|
| 262 | 262 |
///Node shapes. |
| 263 | 263 |
/// |
| 264 | 264 |
enum NodeShapes {
|
| 265 | 265 |
/// = 0 |
| 266 | 266 |
///\image html nodeshape_0.png |
| 267 | 267 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
| 268 | 268 |
CIRCLE=0, |
| 269 | 269 |
/// = 1 |
| 270 | 270 |
///\image html nodeshape_1.png |
| 271 | 271 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
| 272 | 272 |
/// |
| 273 | 273 |
SQUARE=1, |
| 274 | 274 |
/// = 2 |
| 275 | 275 |
///\image html nodeshape_2.png |
| 276 | 276 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
| 277 | 277 |
/// |
| 278 | 278 |
DIAMOND=2, |
| 279 | 279 |
/// = 3 |
| 280 | 280 |
///\image html nodeshape_3.png |
| 281 | 281 |
///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
| 282 | 282 |
/// |
| 283 | 283 |
MALE=3, |
| 284 | 284 |
/// = 4 |
| 285 | 285 |
///\image html nodeshape_4.png |
| 286 | 286 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
| 287 | 287 |
/// |
| 288 | 288 |
FEMALE=4 |
| 289 | 289 |
}; |
| 290 | 290 |
|
| 291 | 291 |
private: |
| 292 | 292 |
class arcLess {
|
| 293 | 293 |
const Graph &g; |
| 294 | 294 |
public: |
| 295 | 295 |
arcLess(const Graph &_g) : g(_g) {}
|
| 296 | 296 |
bool operator()(Arc a,Arc b) const |
| 297 | 297 |
{
|
| 298 | 298 |
Node ai=std::min(g.source(a),g.target(a)); |
| 299 | 299 |
Node aa=std::max(g.source(a),g.target(a)); |
| 300 | 300 |
Node bi=std::min(g.source(b),g.target(b)); |
| 301 | 301 |
Node ba=std::max(g.source(b),g.target(b)); |
| 302 | 302 |
return ai<bi || |
| 303 | 303 |
(ai==bi && (aa < ba || |
| 304 | 304 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
| 305 | 305 |
} |
| 306 | 306 |
}; |
| 307 | 307 |
bool isParallel(Arc e,Arc f) const |
| 308 | 308 |
{
|
| 309 | 309 |
return (g.source(e)==g.source(f)&& |
| 310 | 310 |
g.target(e)==g.target(f)) || |
| 311 | 311 |
(g.source(e)==g.target(f)&& |
| 312 | 312 |
g.target(e)==g.source(f)); |
| 313 | 313 |
} |
| 314 | 314 |
template<class TT> |
| 315 | 315 |
static std::string psOut(const dim2::Point<TT> &p) |
| 316 | 316 |
{
|
| 317 | 317 |
std::ostringstream os; |
| 318 | 318 |
os << p.x << ' ' << p.y; |
| 319 | 319 |
return os.str(); |
| 320 | 320 |
} |
| 321 | 321 |
static std::string psOut(const Color &c) |
| 322 | 322 |
{
|
| 323 | 323 |
std::ostringstream os; |
| 324 | 324 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
| 325 | 325 |
return os.str(); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
public: |
| 329 | 329 |
GraphToEps(const T &t) : T(t), dontPrint(false) {};
|
| 330 | 330 |
|
| 331 | 331 |
template<class X> struct CoordsTraits : public T {
|
| 332 | 332 |
typedef X CoordsMapType; |
| 333 | 333 |
const X &_coords; |
| 334 | 334 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
|
| 335 | 335 |
}; |
| 336 | 336 |
///Sets the map of the node coordinates |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map of the node coordinates. |
| 339 | 339 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
| 340 | 340 |
///\ref dim2::Point "dim2::Point<int>" values. |
| 341 | 341 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
|
| 342 | 342 |
dontPrint=true; |
| 343 | 343 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
| 344 | 344 |
} |
| 345 | 345 |
template<class X> struct NodeSizesTraits : public T {
|
| 346 | 346 |
const X &_nodeSizes; |
| 347 | 347 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
|
| 348 | 348 |
}; |
| 349 | 349 |
///Sets the map of the node sizes |
| 350 | 350 |
|
| 351 | 351 |
///Sets the map of the node sizes. |
| 352 | 352 |
///\param x must be a node map with \c double (or convertible) values. |
| 353 | 353 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
| 354 | 354 |
{
|
| 355 | 355 |
dontPrint=true; |
| 356 | 356 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
| 357 | 357 |
} |
| 358 | 358 |
template<class X> struct NodeShapesTraits : public T {
|
| 359 | 359 |
const X &_nodeShapes; |
| 360 | 360 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
|
| 361 | 361 |
}; |
| 362 | 362 |
///Sets the map of the node shapes |
| 363 | 363 |
|
| 364 | 364 |
///Sets the map of the node shapes. |
| 365 | 365 |
///The available shape values |
| 366 | 366 |
///can be found in \ref NodeShapes "enum NodeShapes". |
| 367 | 367 |
///\param x must be a node map with \c int (or convertible) values. |
| 368 | 368 |
///\sa NodeShapes |
| 369 | 369 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
| 370 | 370 |
{
|
| 371 | 371 |
dontPrint=true; |
| 372 | 372 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
| 373 | 373 |
} |
| 374 | 374 |
template<class X> struct NodeTextsTraits : public T {
|
| 375 | 375 |
const X &_nodeTexts; |
| 376 | 376 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
|
| 377 | 377 |
}; |
| 378 | 378 |
///Sets the text printed on the nodes |
| 379 | 379 |
|
| 380 | 380 |
///Sets the text printed on the nodes. |
| 381 | 381 |
///\param x must be a node map with type that can be pushed to a standard |
| 382 | 382 |
///\c ostream. |
| 383 | 383 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
| 384 | 384 |
{
|
| 385 | 385 |
dontPrint=true; |
| 386 | 386 |
_showNodeText=true; |
| 387 | 387 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
| 388 | 388 |
} |
| 389 | 389 |
template<class X> struct NodePsTextsTraits : public T {
|
| 390 | 390 |
const X &_nodePsTexts; |
| 391 | 391 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
|
| 392 | 392 |
}; |
| 393 | 393 |
///Inserts a PostScript block to the nodes |
| 394 | 394 |
|
| 395 | 395 |
///With this command it is possible to insert a verbatim PostScript |
| 396 | 396 |
///block to the nodes. |
| 397 | 397 |
///The PS current point will be moved to the center of the node before |
| 398 | 398 |
///the PostScript block inserted. |
| 399 | 399 |
/// |
| 400 | 400 |
///Before and after the block a newline character is inserted so you |
| 401 | 401 |
///don't have to bother with the separators. |
| 402 | 402 |
/// |
| 403 | 403 |
///\param x must be a node map with type that can be pushed to a standard |
| 404 | 404 |
///\c ostream. |
| 405 | 405 |
/// |
| 406 | 406 |
///\sa nodePsTextsPreamble() |
| 407 | 407 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
| 408 | 408 |
{
|
| 409 | 409 |
dontPrint=true; |
| 410 | 410 |
_showNodePsText=true; |
| 411 | 411 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
| 412 | 412 |
} |
| 413 | 413 |
template<class X> struct ArcWidthsTraits : public T {
|
| 414 | 414 |
const X &_arcWidths; |
| 415 | 415 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
|
| 416 | 416 |
}; |
| 417 | 417 |
///Sets the map of the arc widths |
| 418 | 418 |
|
| 419 | 419 |
///Sets the map of the arc widths. |
| 420 | 420 |
///\param x must be an arc map with \c double (or convertible) values. |
| 421 | 421 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
| 422 | 422 |
{
|
| 423 | 423 |
dontPrint=true; |
| 424 | 424 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
| 425 | 425 |
} |
| 426 | 426 |
|
| 427 | 427 |
template<class X> struct NodeColorsTraits : public T {
|
| 428 | 428 |
const X &_nodeColors; |
| 429 | 429 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
|
| 430 | 430 |
}; |
| 431 | 431 |
///Sets the map of the node colors |
| 432 | 432 |
|
| 433 | 433 |
///Sets the map of the node colors. |
| 434 | 434 |
///\param x must be a node map with \ref Color values. |
| 435 | 435 |
/// |
| 436 | 436 |
///\sa Palette |
| 437 | 437 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
| 438 | 438 |
nodeColors(const X &x) |
| 439 | 439 |
{
|
| 440 | 440 |
dontPrint=true; |
| 441 | 441 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
| 442 | 442 |
} |
| 443 | 443 |
template<class X> struct NodeTextColorsTraits : public T {
|
| 444 | 444 |
const X &_nodeTextColors; |
| 445 | 445 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
|
| 446 | 446 |
}; |
| 447 | 447 |
///Sets the map of the node text colors |
| 448 | 448 |
|
| 449 | 449 |
///Sets the map of the node text colors. |
| 450 | 450 |
///\param x must be a node map with \ref Color values. |
| 451 | 451 |
/// |
| 452 | 452 |
///\sa Palette |
| 453 | 453 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
| 454 | 454 |
nodeTextColors(const X &x) |
| 455 | 455 |
{
|
| 456 | 456 |
dontPrint=true; |
| 457 | 457 |
_nodeTextColorType=CUST_COL; |
| 458 | 458 |
return GraphToEps<NodeTextColorsTraits<X> > |
| 459 | 459 |
(NodeTextColorsTraits<X>(*this,x)); |
| 460 | 460 |
} |
| 461 | 461 |
template<class X> struct ArcColorsTraits : public T {
|
| 462 | 462 |
const X &_arcColors; |
| 463 | 463 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
|
| 464 | 464 |
}; |
| 465 | 465 |
///Sets the map of the arc colors |
| 466 | 466 |
|
| 467 | 467 |
///Sets the map of the arc colors. |
| 468 | 468 |
///\param x must be an arc map with \ref Color values. |
| 469 | 469 |
/// |
| 470 | 470 |
///\sa Palette |
| 471 | 471 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
| 472 | 472 |
arcColors(const X &x) |
| 473 | 473 |
{
|
| 474 | 474 |
dontPrint=true; |
| 475 | 475 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
| 476 | 476 |
} |
| 477 | 477 |
///Sets a global scale factor for node sizes |
| 478 | 478 |
|
| 479 | 479 |
///Sets a global scale factor for node sizes. |
| 480 | 480 |
/// |
| 481 | 481 |
/// If nodeSizes() is not given, this function simply sets the node |
| 482 | 482 |
/// sizes to \c d. If nodeSizes() is given, but |
| 483 | 483 |
/// autoNodeScale() is not, then the node size given by |
| 484 | 484 |
/// nodeSizes() will be multiplied by the value \c d. |
| 485 | 485 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
| 486 | 486 |
/// node sizes will be scaled in such a way that the greatest size will be |
| 487 | 487 |
/// equal to \c d. |
| 488 | 488 |
/// \sa nodeSizes() |
| 489 | 489 |
/// \sa autoNodeScale() |
| 490 | 490 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
|
| 491 | 491 |
///Turns on/off the automatic node size scaling. |
| 492 | 492 |
|
| 493 | 493 |
///Turns on/off the automatic node size scaling. |
| 494 | 494 |
/// |
| 495 | 495 |
///\sa nodeScale() |
| 496 | 496 |
/// |
| 497 | 497 |
GraphToEps<T> &autoNodeScale(bool b=true) {
|
| 498 | 498 |
_autoNodeScale=b;return *this; |
| 499 | 499 |
} |
| 500 | 500 |
|
| 501 | 501 |
///Turns on/off the absolutematic node size scaling. |
| 502 | 502 |
|
| 503 | 503 |
///Turns on/off the absolutematic node size scaling. |
| 504 | 504 |
/// |
| 505 | 505 |
///\sa nodeScale() |
| 506 | 506 |
/// |
| 507 | 507 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) {
|
| 508 | 508 |
_absoluteNodeSizes=b;return *this; |
| 509 | 509 |
} |
| 510 | 510 |
|
| 511 | 511 |
///Negates the Y coordinates. |
| 512 | 512 |
GraphToEps<T> &negateY(bool b=true) {
|
| 513 | 513 |
_negY=b;return *this; |
| 514 | 514 |
} |
| 515 | 515 |
|
| 516 | 516 |
///Turn on/off pre-scaling |
| 517 | 517 |
|
| 518 | 518 |
///By default graphToEps() rescales the whole image in order to avoid |
| 519 | 519 |
///very big or very small bounding boxes. |
| 520 | 520 |
/// |
| 521 | 521 |
///This (p)rescaling can be turned off with this function. |
| 522 | 522 |
/// |
| 523 | 523 |
GraphToEps<T> &preScale(bool b=true) {
|
| 524 | 524 |
_preScale=b;return *this; |
| 525 | 525 |
} |
| 526 | 526 |
|
| 527 | 527 |
///Sets a global scale factor for arc widths |
| 528 | 528 |
|
| 529 | 529 |
/// Sets a global scale factor for arc widths. |
| 530 | 530 |
/// |
| 531 | 531 |
/// If arcWidths() is not given, this function simply sets the arc |
| 532 | 532 |
/// widths to \c d. If arcWidths() is given, but |
| 533 | 533 |
/// autoArcWidthScale() is not, then the arc withs given by |
| 534 | 534 |
/// arcWidths() will be multiplied by the value \c d. |
| 535 | 535 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
| 536 | 536 |
/// arc withs will be scaled in such a way that the greatest width will be |
| 537 | 537 |
/// equal to \c d. |
| 538 | 538 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
|
| 539 | 539 |
///Turns on/off the automatic arc width scaling. |
| 540 | 540 |
|
| 541 | 541 |
///Turns on/off the automatic arc width scaling. |
| 542 | 542 |
/// |
| 543 | 543 |
///\sa arcWidthScale() |
| 544 | 544 |
/// |
| 545 | 545 |
GraphToEps<T> &autoArcWidthScale(bool b=true) {
|
| 546 | 546 |
_autoArcWidthScale=b;return *this; |
| 547 | 547 |
} |
| 548 | 548 |
///Turns on/off the absolutematic arc width scaling. |
| 549 | 549 |
|
| 550 | 550 |
///Turns on/off the absolutematic arc width scaling. |
| 551 | 551 |
/// |
| 552 | 552 |
///\sa arcWidthScale() |
| 553 | 553 |
/// |
| 554 | 554 |
GraphToEps<T> &absoluteArcWidths(bool b=true) {
|
| 555 | 555 |
_absoluteArcWidths=b;return *this; |
| 556 | 556 |
} |
| 557 | 557 |
///Sets a global scale factor for the whole picture |
| 558 | 558 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;}
|
| 559 | 559 |
///Sets the width of the border around the picture |
| 560 | 560 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
|
| 561 | 561 |
///Sets the width of the border around the picture |
| 562 | 562 |
GraphToEps<T> &border(double x, double y) {
|
| 563 | 563 |
_xBorder=x;_yBorder=y;return *this; |
| 564 | 564 |
} |
| 565 | 565 |
///Sets whether to draw arrows |
| 566 | 566 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
|
| 567 | 567 |
///Sets the length of the arrowheads |
| 568 | 568 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
|
| 569 | 569 |
///Sets the width of the arrowheads |
| 570 | 570 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
|
| 571 | 571 |
|
| 572 | 572 |
///Scales the drawing to fit to A4 page |
| 573 | 573 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
|
| 574 | 574 |
|
| 575 | 575 |
///Enables parallel arcs |
| 576 | 576 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
|
| 577 | 577 |
|
| 578 | 578 |
///Sets the distance between parallel arcs |
| 579 | 579 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
|
| 580 | 580 |
|
| 581 | 581 |
///Hides the arcs |
| 582 | 582 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
|
| 583 | 583 |
///Hides the nodes |
| 584 | 584 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
|
| 585 | 585 |
|
| 586 | 586 |
///Sets the size of the node texts |
| 587 | 587 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
|
| 588 | 588 |
|
| 589 | 589 |
///Sets the color of the node texts to be different from the node color |
| 590 | 590 |
|
| 591 | 591 |
///Sets the color of the node texts to be as different from the node color |
| 592 | 592 |
///as it is possible. |
| 593 | 593 |
GraphToEps<T> &distantColorNodeTexts() |
| 594 | 594 |
{_nodeTextColorType=DIST_COL;return *this;}
|
| 595 | 595 |
///Sets the color of the node texts to be black or white and always visible. |
| 596 | 596 |
|
| 597 | 597 |
///Sets the color of the node texts to be black or white according to |
| 598 | 598 |
///which is more different from the node color. |
| 599 | 599 |
GraphToEps<T> &distantBWNodeTexts() |
| 600 | 600 |
{_nodeTextColorType=DIST_BW;return *this;}
|
| 601 | 601 |
|
| 602 | 602 |
///Gives a preamble block for node Postscript block. |
| 603 | 603 |
|
| 604 | 604 |
///Gives a preamble block for node Postscript block. |
| 605 | 605 |
/// |
| 606 | 606 |
///\sa nodePsTexts() |
| 607 | 607 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) {
|
| 608 | 608 |
_nodePsTextsPreamble=str ;return *this; |
| 609 | 609 |
} |
| 610 | 610 |
///Sets whether the graph is undirected |
| 611 | 611 |
|
| 612 | 612 |
///Sets whether the graph is undirected. |
| 613 | 613 |
/// |
| 614 | 614 |
///This setting is the default for undirected graphs. |
| 615 | 615 |
/// |
| 616 | 616 |
///\sa directed() |
| 617 | 617 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
|
| 618 | 618 |
|
| 619 | 619 |
///Sets whether the graph is directed |
| 620 | 620 |
|
| 621 | 621 |
///Sets whether the graph is directed. |
| 622 | 622 |
///Use it to show the edges as a pair of directed ones. |
| 623 | 623 |
/// |
| 624 | 624 |
///This setting is the default for digraphs. |
| 625 | 625 |
/// |
| 626 | 626 |
///\sa undirected() |
| 627 | 627 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
|
| 628 | 628 |
|
| 629 | 629 |
///Sets the title. |
| 630 | 630 |
|
| 631 | 631 |
///Sets the title of the generated image, |
| 632 | 632 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
| 633 | 633 |
///the EPS file. |
| 634 | 634 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
|
| 635 | 635 |
///Sets the copyright statement. |
| 636 | 636 |
|
| 637 | 637 |
///Sets the copyright statement of the generated image, |
| 638 | 638 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
| 639 | 639 |
///the EPS file. |
| 640 | 640 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
|
| 641 | 641 |
|
| 642 | 642 |
protected: |
| 643 | 643 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
| 644 | 644 |
{
|
| 645 | 645 |
switch(t) {
|
| 646 | 646 |
case CIRCLE: |
| 647 | 647 |
case MALE: |
| 648 | 648 |
case FEMALE: |
| 649 | 649 |
return p.normSquare()<=r*r; |
| 650 | 650 |
case SQUARE: |
| 651 | 651 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
| 652 | 652 |
case DIAMOND: |
| 653 | 653 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
| 654 | 654 |
} |
| 655 | 655 |
return false; |
| 656 | 656 |
} |
| 657 | 657 |
|
| 658 | 658 |
public: |
| 659 | 659 |
~GraphToEps() { }
|
| 660 | 660 |
|
| 661 | 661 |
///Draws the graph. |
| 662 | 662 |
|
| 663 | 663 |
///Like other functions using |
| 664 | 664 |
///\ref named-templ-func-param "named template parameters", |
| 665 | 665 |
///this function calls the algorithm itself, i.e. in this case |
| 666 | 666 |
///it draws the graph. |
| 667 | 667 |
void run() {
|
| 668 | 668 |
const double EPSILON=1e-9; |
| 669 | 669 |
if(dontPrint) return; |
| 670 | 670 |
|
| 671 | 671 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
| 672 | 672 |
mycoords(_coords,_negY); |
| 673 | 673 |
|
| 674 | 674 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
| 675 | 675 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
| 676 | 676 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
| 677 | 677 |
os << "%%Creator: LEMON, graphToEps()\n"; |
| 678 | 678 |
|
| 679 | 679 |
{
|
| 680 | 680 |
os << "%%CreationDate: "; |
| 681 | 681 |
#ifndef WIN32 |
| 682 | 682 |
timeval tv; |
| 683 | 683 |
gettimeofday(&tv, 0); |
| 684 | 684 |
|
| 685 | 685 |
char cbuf[26]; |
| 686 | 686 |
ctime_r(&tv.tv_sec,cbuf); |
| 687 | 687 |
os << cbuf; |
| 688 | 688 |
#else |
| 689 | 689 |
os << bits::getWinFormattedDate(); |
| 690 | 690 |
os << std::endl; |
| 691 | 691 |
#endif |
| 692 | 692 |
} |
| 693 | 693 |
|
| 694 | 694 |
if (_autoArcWidthScale) {
|
| 695 | 695 |
double max_w=0; |
| 696 | 696 |
for(ArcIt e(g);e!=INVALID;++e) |
| 697 | 697 |
max_w=std::max(double(_arcWidths[e]),max_w); |
| 698 | 698 |
if(max_w>EPSILON) {
|
| 699 | 699 |
_arcWidthScale/=max_w; |
| 700 | 700 |
} |
| 701 | 701 |
} |
| 702 | 702 |
|
| 703 | 703 |
if (_autoNodeScale) {
|
| 704 | 704 |
double max_s=0; |
| 705 | 705 |
for(NodeIt n(g);n!=INVALID;++n) |
| 706 | 706 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
| 707 | 707 |
if(max_s>EPSILON) {
|
| 708 | 708 |
_nodeScale/=max_s; |
| 709 | 709 |
} |
| 710 | 710 |
} |
| 711 | 711 |
|
| 712 | 712 |
double diag_len = 1; |
| 713 | 713 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
|
| 714 | 714 |
dim2::Box<double> bb; |
| 715 | 715 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
| 716 | 716 |
if (bb.empty()) {
|
| 717 | 717 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
| 718 | 718 |
} |
| 719 | 719 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
| 720 | 720 |
if(diag_len<EPSILON) diag_len = 1; |
| 721 | 721 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
| 722 | 722 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
| 723 | 723 |
} |
| 724 | 724 |
|
| 725 | 725 |
dim2::Box<double> bb; |
| 726 | 726 |
for(NodeIt n(g);n!=INVALID;++n) {
|
| 727 | 727 |
double ns=_nodeSizes[n]*_nodeScale; |
| 728 | 728 |
dim2::Point<double> p(ns,ns); |
| 729 | 729 |
switch(_nodeShapes[n]) {
|
| 730 | 730 |
case CIRCLE: |
| 731 | 731 |
case SQUARE: |
| 732 | 732 |
case DIAMOND: |
| 733 | 733 |
bb.add(p+mycoords[n]); |
| 734 | 734 |
bb.add(-p+mycoords[n]); |
| 735 | 735 |
break; |
| 736 | 736 |
case MALE: |
| 737 | 737 |
bb.add(-p+mycoords[n]); |
| 738 | 738 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
| 739 | 739 |
break; |
| 740 | 740 |
case FEMALE: |
| 741 | 741 |
bb.add(p+mycoords[n]); |
| 742 | 742 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
| 743 | 743 |
break; |
| 744 | 744 |
} |
| 745 | 745 |
} |
| 746 | 746 |
if (bb.empty()) {
|
| 747 | 747 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
| 748 | 748 |
} |
| 749 | 749 |
|
| 750 | 750 |
if(_scaleToA4) |
| 751 | 751 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
| 752 | 752 |
else {
|
| 753 | 753 |
if(_preScale) {
|
| 754 | 754 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
| 755 | 755 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
| 756 | 756 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
| 757 | 757 |
} |
| 758 | 758 |
|
| 759 | 759 |
os << "%%BoundingBox: " |
| 760 | 760 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
| 761 | 761 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
| 762 | 762 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
| 763 | 763 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
| 764 | 764 |
} |
| 765 | 765 |
|
| 766 | 766 |
os << "%%EndComments\n"; |
| 767 | 767 |
|
| 768 | 768 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
| 769 | 769 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
|
| 770 | 770 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
| 771 | 771 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
|
| 772 | 772 |
<< " bind def\n"; |
| 773 | 773 |
//x y r |
| 774 | 774 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
|
| 775 | 775 |
<< " bind def\n"; |
| 776 | 776 |
//x y r |
| 777 | 777 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
|
| 778 | 778 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
| 779 | 779 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
| 780 | 780 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
| 781 | 781 |
<< " closepath pop pop pop} bind def\n"; |
| 782 | 782 |
//x y r |
| 783 | 783 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n"
|
| 784 | 784 |
<< " 2 index 2 index 2 index add lineto\n" |
| 785 | 785 |
<< " 2 index 1 index sub 2 index lineto\n" |
| 786 | 786 |
<< " 2 index 2 index 2 index sub lineto\n" |
| 787 | 787 |
<< " closepath pop pop pop} bind def\n"; |
| 788 | 788 |
// x y r cr cg cb |
| 789 | 789 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
|
| 790 | 790 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
| 791 | 791 |
<< " } bind def\n"; |
| 792 | 792 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
|
| 793 | 793 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
| 794 | 794 |
<< " } bind def\n"; |
| 795 | 795 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
|
| 796 | 796 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
| 797 | 797 |
<< " } bind def\n"; |
| 798 | 798 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index "
|
| 799 | 799 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
| 800 | 800 |
<< " 1.5 mul mul setlinewidth\n" |
| 801 | 801 |
<< " newpath 5 index 5 index moveto " |
| 802 | 802 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
| 803 | 803 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
| 804 | 804 |
<< " moveto\n" |
| 805 | 805 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
| 806 | 806 |
<< "stroke\n" |
| 807 | 807 |
<< " 5 index 5 index 5 index c fill\n" |
| 808 | 808 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
| 809 | 809 |
<< " } bind def\n"; |
| 810 | 810 |
os << "/nmale {\n"
|
| 811 | 811 |
<< " 0 0 0 setrgbcolor 3 index " |
| 812 | 812 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
| 813 | 813 |
<<" 1.5 mul mul setlinewidth\n" |
| 814 | 814 |
<< " newpath 5 index 5 index moveto\n" |
| 815 | 815 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
| 816 | 816 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
| 817 | 817 |
<< " 1 index 1 index lineto\n" |
| 818 | 818 |
<< " 1 index 1 index 7 index sub moveto\n" |
| 819 | 819 |
<< " 1 index 1 index lineto\n" |
| 820 | 820 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
| 821 | 821 |
<< " lineto\n" |
| 822 | 822 |
<< " stroke\n" |
| 823 | 823 |
<< " 5 index 5 index 5 index c fill\n" |
| 824 | 824 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
| 825 | 825 |
<< " } bind def\n"; |
| 826 | 826 |
|
| 827 | 827 |
|
| 828 | 828 |
os << "/arrl " << _arrowLength << " def\n"; |
| 829 | 829 |
os << "/arrw " << _arrowWidth << " def\n"; |
| 830 | 830 |
// l dx_norm dy_norm |
| 831 | 831 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
|
| 832 | 832 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
| 833 | 833 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
|
| 834 | 834 |
<< "exch def\n" |
| 835 | 835 |
<< " /w exch def /len exch def\n" |
| 836 | 836 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
| 837 | 837 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
| 838 | 838 |
<< " len w sub arrl sub dx dy lrl\n" |
| 839 | 839 |
<< " arrw dy dx neg lrl\n" |
| 840 | 840 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
| 841 | 841 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
| 842 | 842 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
| 843 | 843 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
| 844 | 844 |
<< " arrw dy dx neg lrl\n" |
| 845 | 845 |
<< " len w sub arrl sub neg dx dy lrl\n" |
| 846 | 846 |
<< " closepath fill } bind def\n"; |
| 847 | 847 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
|
| 848 | 848 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
| 849 | 849 |
|
| 850 | 850 |
os << "\ngsave\n"; |
| 851 | 851 |
if(_scaleToA4) |
| 852 | 852 |
if(bb.height()>bb.width()) {
|
| 853 | 853 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
| 854 | 854 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
| 855 | 855 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
| 856 | 856 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
| 857 | 857 |
<< " translate\n" |
| 858 | 858 |
<< sc << " dup scale\n" |
| 859 | 859 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
| 860 | 860 |
} |
| 861 | 861 |
else {
|
| 862 | 862 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
| 863 | 863 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
| 864 | 864 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
| 865 | 865 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
| 866 | 866 |
<< " translate\n" |
| 867 | 867 |
<< sc << " dup scale\n90 rotate\n" |
| 868 | 868 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
| 869 | 869 |
} |
| 870 | 870 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
| 871 | 871 |
|
| 872 | 872 |
if(_showArcs) {
|
| 873 | 873 |
os << "%Arcs:\ngsave\n"; |
| 874 | 874 |
if(_enableParallel) {
|
| 875 | 875 |
std::vector<Arc> el; |
| 876 | 876 |
for(ArcIt e(g);e!=INVALID;++e) |
| 877 | 877 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
| 878 | 878 |
&&g.source(e)!=g.target(e)) |
| 879 | 879 |
el.push_back(e); |
| 880 | 880 |
std::sort(el.begin(),el.end(),arcLess(g)); |
| 881 | 881 |
|
| 882 | 882 |
typename std::vector<Arc>::iterator j; |
| 883 | 883 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
|
| 884 | 884 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
| 885 | 885 |
|
| 886 | 886 |
double sw=0; |
| 887 | 887 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
| 888 | 888 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
| 889 | 889 |
sw-=_parArcDist; |
| 890 | 890 |
sw/=-2.0; |
| 891 | 891 |
dim2::Point<double> |
| 892 | 892 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
| 893 | 893 |
double l=std::sqrt(dvec.normSquare()); |
| 894 | 894 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
| 895 | 895 |
dim2::Point<double> m; |
| 896 | 896 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
| 897 | 897 |
// mycoords[g.source(*i)])/2.0; |
| 898 | 898 |
|
| 899 | 899 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
| 900 | 900 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
| 901 | 901 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
| 902 | 902 |
|
| 903 | 903 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
| 904 | 904 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
| 905 | 905 |
|
| 906 | 906 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
|
| 907 | 907 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
| 908 | 908 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
| 909 | 909 |
if(_drawArrows) {
|
| 910 | 910 |
int node_shape; |
| 911 | 911 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
| 912 | 912 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
| 913 | 913 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
| 914 | 914 |
node_shape=_nodeShapes[g.target(*e)]; |
| 915 | 915 |
dim2::Bezier3 bez(s,mm,mm,t); |
| 916 | 916 |
double t1=0,t2=1; |
| 917 | 917 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
| 918 | 918 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
| 919 | 919 |
else t1=(t1+t2)/2; |
| 920 | 920 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
| 921 | 921 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
| 922 | 922 |
rn*=rn; |
| 923 | 923 |
t2=(t1+t2)/2;t1=0; |
| 924 | 924 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
| 925 | 925 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
| 926 | 926 |
else t2=(t1+t2)/2; |
| 927 | 927 |
dim2::Point<double> linend=bez((t1+t2)/2); |
| 928 | 928 |
bez=bez.before((t1+t2)/2); |
| 929 | 929 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
| 930 | 930 |
// node_shape=_nodeShapes[g.source(*e)]; |
| 931 | 931 |
// t1=0;t2=1; |
| 932 | 932 |
// for(int i=0;i<INTERPOL_PREC;++i) |
| 933 | 933 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
| 934 | 934 |
// t1=(t1+t2)/2; |
| 935 | 935 |
// else t2=(t1+t2)/2; |
| 936 | 936 |
// bez=bez.after((t1+t2)/2); |
| 937 | 937 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
| 938 | 938 |
<< _arcColors[*e].red() << ' ' |
| 939 | 939 |
<< _arcColors[*e].green() << ' ' |
| 940 | 940 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
| 941 | 941 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
| 942 | 942 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
| 943 | 943 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
| 944 | 944 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
| 945 | 945 |
dim2::Point<double> dd(rot90(linend-apoint)); |
| 946 | 946 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
| 947 | 947 |
std::sqrt(dd.normSquare()); |
| 948 | 948 |
os << "newpath " << psOut(apoint) << " moveto " |
| 949 | 949 |
<< psOut(linend+dd) << " lineto " |
| 950 | 950 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
| 951 | 951 |
} |
| 952 | 952 |
else {
|
| 953 | 953 |
os << mycoords[g.source(*e)].x << ' ' |
| 954 | 954 |
<< mycoords[g.source(*e)].y << ' ' |
| 955 | 955 |
<< mm.x << ' ' << mm.y << ' ' |
| 956 | 956 |
<< mycoords[g.target(*e)].x << ' ' |
| 957 | 957 |
<< mycoords[g.target(*e)].y << ' ' |
| 958 | 958 |
<< _arcColors[*e].red() << ' ' |
| 959 | 959 |
<< _arcColors[*e].green() << ' ' |
| 960 | 960 |
<< _arcColors[*e].blue() << ' ' |
| 961 | 961 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
| 962 | 962 |
} |
| 963 | 963 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
| 964 | 964 |
} |
| 965 | 965 |
} |
| 966 | 966 |
} |
| 967 | 967 |
else for(ArcIt e(g);e!=INVALID;++e) |
| 968 | 968 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
| 969 | 969 |
&&g.source(e)!=g.target(e)) {
|
| 970 | 970 |
if(_drawArrows) {
|
| 971 | 971 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
| 972 | 972 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
| 973 | 973 |
int node_shape=_nodeShapes[g.target(e)]; |
| 974 | 974 |
double t1=0,t2=1; |
| 975 | 975 |
for(int i=0;i<INTERPOL_PREC;++i) |
| 976 | 976 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
| 977 | 977 |
else t2=(t1+t2)/2; |
| 978 | 978 |
double l=std::sqrt(d.normSquare()); |
| 979 | 979 |
d/=l; |
| 980 | 980 |
|
| 981 | 981 |
os << l*(1-(t1+t2)/2) << ' ' |
| 982 | 982 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
| 983 | 983 |
<< d.x << ' ' << d.y << ' ' |
| 984 | 984 |
<< mycoords[g.source(e)].x << ' ' |
| 985 | 985 |
<< mycoords[g.source(e)].y << ' ' |
| 986 | 986 |
<< _arcColors[e].red() << ' ' |
| 987 | 987 |
<< _arcColors[e].green() << ' ' |
| 988 | 988 |
<< _arcColors[e].blue() << " arr\n"; |
| 989 | 989 |
} |
| 990 | 990 |
else os << mycoords[g.source(e)].x << ' ' |
| 991 | 991 |
<< mycoords[g.source(e)].y << ' ' |
| 992 | 992 |
<< mycoords[g.target(e)].x << ' ' |
| 993 | 993 |
<< mycoords[g.target(e)].y << ' ' |
| 994 | 994 |
<< _arcColors[e].red() << ' ' |
| 995 | 995 |
<< _arcColors[e].green() << ' ' |
| 996 | 996 |
<< _arcColors[e].blue() << ' ' |
| 997 | 997 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
| 998 | 998 |
} |
| 999 | 999 |
os << "grestore\n"; |
| 1000 | 1000 |
} |
| 1001 | 1001 |
if(_showNodes) {
|
| 1002 | 1002 |
os << "%Nodes:\ngsave\n"; |
| 1003 | 1003 |
for(NodeIt n(g);n!=INVALID;++n) {
|
| 1004 | 1004 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
| 1005 | 1005 |
<< _nodeSizes[n]*_nodeScale << ' ' |
| 1006 | 1006 |
<< _nodeColors[n].red() << ' ' |
| 1007 | 1007 |
<< _nodeColors[n].green() << ' ' |
| 1008 | 1008 |
<< _nodeColors[n].blue() << ' '; |
| 1009 | 1009 |
switch(_nodeShapes[n]) {
|
| 1010 | 1010 |
case CIRCLE: |
| 1011 | 1011 |
os<< "nc";break; |
| 1012 | 1012 |
case SQUARE: |
| 1013 | 1013 |
os<< "nsq";break; |
| 1014 | 1014 |
case DIAMOND: |
| 1015 | 1015 |
os<< "ndi";break; |
| 1016 | 1016 |
case MALE: |
| 1017 | 1017 |
os<< "nmale";break; |
| 1018 | 1018 |
case FEMALE: |
| 1019 | 1019 |
os<< "nfemale";break; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
os<<'\n'; |
| 1022 | 1022 |
} |
| 1023 | 1023 |
os << "grestore\n"; |
| 1024 | 1024 |
} |
| 1025 | 1025 |
if(_showNodeText) {
|
| 1026 | 1026 |
os << "%Node texts:\ngsave\n"; |
| 1027 | 1027 |
os << "/fosi " << _nodeTextSize << " def\n"; |
| 1028 | 1028 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
| 1029 | 1029 |
for(NodeIt n(g);n!=INVALID;++n) {
|
| 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-2011 |
| 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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" reader. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_READER_H |
| 25 | 25 |
#define LEMON_LGF_READER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <set> |
| 32 | 32 |
#include <map> |
| 33 | 33 |
|
| 34 | 34 |
#include <lemon/core.h> |
| 35 | 35 |
|
| 36 | 36 |
#include <lemon/lgf_writer.h> |
| 37 | 37 |
|
| 38 | 38 |
#include <lemon/concept_check.h> |
| 39 | 39 |
#include <lemon/concepts/maps.h> |
| 40 | 40 |
|
| 41 | 41 |
namespace lemon {
|
| 42 | 42 |
|
| 43 | 43 |
namespace _reader_bits {
|
| 44 | 44 |
|
| 45 | 45 |
template <typename Value> |
| 46 | 46 |
struct DefaultConverter {
|
| 47 | 47 |
Value operator()(const std::string& str) {
|
| 48 | 48 |
std::istringstream is(str); |
| 49 | 49 |
Value value; |
| 50 | 50 |
if (!(is >> value)) {
|
| 51 | 51 |
throw FormatError("Cannot read token");
|
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
char c; |
| 55 | 55 |
if (is >> std::ws >> c) {
|
| 56 | 56 |
throw FormatError("Remaining characters in token");
|
| 57 | 57 |
} |
| 58 | 58 |
return value; |
| 59 | 59 |
} |
| 60 | 60 |
}; |
| 61 | 61 |
|
| 62 | 62 |
template <> |
| 63 | 63 |
struct DefaultConverter<std::string> {
|
| 64 | 64 |
std::string operator()(const std::string& str) {
|
| 65 | 65 |
return str; |
| 66 | 66 |
} |
| 67 | 67 |
}; |
| 68 | 68 |
|
| 69 | 69 |
template <typename _Item> |
| 70 | 70 |
class MapStorageBase {
|
| 71 | 71 |
public: |
| 72 | 72 |
typedef _Item Item; |
| 73 | 73 |
|
| 74 | 74 |
public: |
| 75 | 75 |
MapStorageBase() {}
|
| 76 | 76 |
virtual ~MapStorageBase() {}
|
| 77 | 77 |
|
| 78 | 78 |
virtual void set(const Item& item, const std::string& value) = 0; |
| 79 | 79 |
|
| 80 | 80 |
}; |
| 81 | 81 |
|
| 82 | 82 |
template <typename _Item, typename _Map, |
| 83 | 83 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 84 | 84 |
class MapStorage : public MapStorageBase<_Item> {
|
| 85 | 85 |
public: |
| 86 | 86 |
typedef _Map Map; |
| 87 | 87 |
typedef _Converter Converter; |
| 88 | 88 |
typedef _Item Item; |
| 89 | 89 |
|
| 90 | 90 |
private: |
| 91 | 91 |
Map& _map; |
| 92 | 92 |
Converter _converter; |
| 93 | 93 |
|
| 94 | 94 |
public: |
| 95 | 95 |
MapStorage(Map& map, const Converter& converter = Converter()) |
| 96 | 96 |
: _map(map), _converter(converter) {}
|
| 97 | 97 |
virtual ~MapStorage() {}
|
| 98 | 98 |
|
| 99 | 99 |
virtual void set(const Item& item ,const std::string& value) {
|
| 100 | 100 |
_map.set(item, _converter(value)); |
| 101 | 101 |
} |
| 102 | 102 |
}; |
| 103 | 103 |
|
| 104 | 104 |
template <typename _Graph, bool _dir, typename _Map, |
| 105 | 105 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 106 | 106 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 107 | 107 |
public: |
| 108 | 108 |
typedef _Map Map; |
| 109 | 109 |
typedef _Converter Converter; |
| 110 | 110 |
typedef _Graph Graph; |
| 111 | 111 |
typedef typename Graph::Edge Item; |
| 112 | 112 |
static const bool dir = _dir; |
| 113 | 113 |
|
| 114 | 114 |
private: |
| 115 | 115 |
const Graph& _graph; |
| 116 | 116 |
Map& _map; |
| 117 | 117 |
Converter _converter; |
| 118 | 118 |
|
| 119 | 119 |
public: |
| 120 | 120 |
GraphArcMapStorage(const Graph& graph, Map& map, |
| 121 | 121 |
const Converter& converter = Converter()) |
| 122 | 122 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 123 | 123 |
virtual ~GraphArcMapStorage() {}
|
| 124 | 124 |
|
| 125 | 125 |
virtual void set(const Item& item ,const std::string& value) {
|
| 126 | 126 |
_map.set(_graph.direct(item, dir), _converter(value)); |
| 127 | 127 |
} |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
class ValueStorageBase {
|
| 131 | 131 |
public: |
| 132 | 132 |
ValueStorageBase() {}
|
| 133 | 133 |
virtual ~ValueStorageBase() {}
|
| 134 | 134 |
|
| 135 | 135 |
virtual void set(const std::string&) = 0; |
| 136 | 136 |
}; |
| 137 | 137 |
|
| 138 | 138 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 139 | 139 |
class ValueStorage : public ValueStorageBase {
|
| 140 | 140 |
public: |
| 141 | 141 |
typedef _Value Value; |
| 142 | 142 |
typedef _Converter Converter; |
| 143 | 143 |
|
| 144 | 144 |
private: |
| 145 | 145 |
Value& _value; |
| 146 | 146 |
Converter _converter; |
| 147 | 147 |
|
| 148 | 148 |
public: |
| 149 | 149 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
| 150 | 150 |
: _value(value), _converter(converter) {}
|
| 151 | 151 |
|
| 152 | 152 |
virtual void set(const std::string& value) {
|
| 153 | 153 |
_value = _converter(value); |
| 154 | 154 |
} |
| 155 | 155 |
}; |
| 156 | 156 |
|
| 157 | 157 |
template <typename Value> |
| 158 | 158 |
struct MapLookUpConverter {
|
| 159 | 159 |
const std::map<std::string, Value>& _map; |
| 160 | 160 |
|
| 161 | 161 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
| 162 | 162 |
: _map(map) {}
|
| 163 | 163 |
|
| 164 | 164 |
Value operator()(const std::string& str) {
|
| 165 | 165 |
typename std::map<std::string, Value>::const_iterator it = |
| 166 | 166 |
_map.find(str); |
| 167 | 167 |
if (it == _map.end()) {
|
| 168 | 168 |
std::ostringstream msg; |
| 169 | 169 |
msg << "Item not found: " << str; |
| 170 | 170 |
throw FormatError(msg.str()); |
| 171 | 171 |
} |
| 172 | 172 |
return it->second; |
| 173 | 173 |
} |
| 174 | 174 |
}; |
| 175 | 175 |
|
| 176 | 176 |
template <typename Graph> |
| 177 | 177 |
struct GraphArcLookUpConverter {
|
| 178 | 178 |
const Graph& _graph; |
| 179 | 179 |
const std::map<std::string, typename Graph::Edge>& _map; |
| 180 | 180 |
|
| 181 | 181 |
GraphArcLookUpConverter(const Graph& graph, |
| 182 | 182 |
const std::map<std::string, |
| 183 | 183 |
typename Graph::Edge>& map) |
| 184 | 184 |
: _graph(graph), _map(map) {}
|
| 185 | 185 |
|
| 186 | 186 |
typename Graph::Arc operator()(const std::string& str) {
|
| 187 | 187 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) {
|
| 188 | 188 |
throw FormatError("Item must start with '+' or '-'");
|
| 189 | 189 |
} |
| 190 | 190 |
typename std::map<std::string, typename Graph::Edge> |
| 191 | 191 |
::const_iterator it = _map.find(str.substr(1)); |
| 192 | 192 |
if (it == _map.end()) {
|
| 193 | 193 |
throw FormatError("Item not found");
|
| 194 | 194 |
} |
| 195 | 195 |
return _graph.direct(it->second, str[0] == '+'); |
| 196 | 196 |
} |
| 197 | 197 |
}; |
| 198 | 198 |
|
| 199 | 199 |
inline bool isWhiteSpace(char c) {
|
| 200 | 200 |
return c == ' ' || c == '\t' || c == '\v' || |
| 201 | 201 |
c == '\n' || c == '\r' || c == '\f'; |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
inline bool isOct(char c) {
|
| 205 | 205 |
return '0' <= c && c <='7'; |
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
inline int valueOct(char c) {
|
| 209 | 209 |
LEMON_ASSERT(isOct(c), "The character is not octal."); |
| 210 | 210 |
return c - '0'; |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
inline bool isHex(char c) {
|
| 214 | 214 |
return ('0' <= c && c <= '9') ||
|
| 215 | 215 |
('a' <= c && c <= 'z') ||
|
| 216 | 216 |
('A' <= c && c <= 'Z');
|
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
inline int valueHex(char c) {
|
| 220 | 220 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
| 221 | 221 |
if ('0' <= c && c <= '9') return c - '0';
|
| 222 | 222 |
if ('a' <= c && c <= 'z') return c - 'a' + 10;
|
| 223 | 223 |
return c - 'A' + 10; |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
inline bool isIdentifierFirstChar(char c) {
|
| 227 | 227 |
return ('a' <= c && c <= 'z') ||
|
| 228 | 228 |
('A' <= c && c <= 'Z') || c == '_';
|
| 229 | 229 |
} |
| 230 | 230 |
|
| 231 | 231 |
inline bool isIdentifierChar(char c) {
|
| 232 | 232 |
return isIdentifierFirstChar(c) || |
| 233 | 233 |
('0' <= c && c <= '9');
|
| 234 | 234 |
} |
| 235 | 235 |
|
| 236 | 236 |
inline char readEscape(std::istream& is) {
|
| 237 | 237 |
char c; |
| 238 | 238 |
if (!is.get(c)) |
| 239 | 239 |
throw FormatError("Escape format error");
|
| 240 | 240 |
|
| 241 | 241 |
switch (c) {
|
| 242 | 242 |
case '\\': |
| 243 | 243 |
return '\\'; |
| 244 | 244 |
case '\"': |
| 245 | 245 |
return '\"'; |
| 246 | 246 |
case '\'': |
| 247 | 247 |
return '\''; |
| 248 | 248 |
case '\?': |
| 249 | 249 |
return '\?'; |
| 250 | 250 |
case 'a': |
| 251 | 251 |
return '\a'; |
| 252 | 252 |
case 'b': |
| 253 | 253 |
return '\b'; |
| 254 | 254 |
case 'f': |
| 255 | 255 |
return '\f'; |
| 256 | 256 |
case 'n': |
| 257 | 257 |
return '\n'; |
| 258 | 258 |
case 'r': |
| 259 | 259 |
return '\r'; |
| 260 | 260 |
case 't': |
| 261 | 261 |
return '\t'; |
| 262 | 262 |
case 'v': |
| 263 | 263 |
return '\v'; |
| 264 | 264 |
case 'x': |
| 265 | 265 |
{
|
| 266 | 266 |
int code; |
| 267 | 267 |
if (!is.get(c) || !isHex(c)) |
| 268 | 268 |
throw FormatError("Escape format error");
|
| 269 | 269 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
| 270 | 270 |
else code = code * 16 + valueHex(c); |
| 271 | 271 |
return code; |
| 272 | 272 |
} |
| 273 | 273 |
default: |
| 274 | 274 |
{
|
| 275 | 275 |
int code; |
| 276 | 276 |
if (!isOct(c)) |
| 277 | 277 |
throw FormatError("Escape format error");
|
| 278 | 278 |
else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
| 279 | 279 |
is.putback(c); |
| 280 | 280 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
| 281 | 281 |
is.putback(c); |
| 282 | 282 |
else code = code * 8 + valueOct(c); |
| 283 | 283 |
return code; |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
} |
| 287 | 287 |
|
| 288 | 288 |
inline std::istream& readToken(std::istream& is, std::string& str) {
|
| 289 | 289 |
std::ostringstream os; |
| 290 | 290 |
|
| 291 | 291 |
char c; |
| 292 | 292 |
is >> std::ws; |
| 293 | 293 |
|
| 294 | 294 |
if (!is.get(c)) |
| 295 | 295 |
return is; |
| 296 | 296 |
|
| 297 | 297 |
if (c == '\"') {
|
| 298 | 298 |
while (is.get(c) && c != '\"') {
|
| 299 | 299 |
if (c == '\\') |
| 300 | 300 |
c = readEscape(is); |
| 301 | 301 |
os << c; |
| 302 | 302 |
} |
| 303 | 303 |
if (!is) |
| 304 | 304 |
throw FormatError("Quoted format error");
|
| 305 | 305 |
} else {
|
| 306 | 306 |
is.putback(c); |
| 307 | 307 |
while (is.get(c) && !isWhiteSpace(c)) {
|
| 308 | 308 |
if (c == '\\') |
| 309 | 309 |
c = readEscape(is); |
| 310 | 310 |
os << c; |
| 311 | 311 |
} |
| 312 | 312 |
if (!is) {
|
| 313 | 313 |
is.clear(); |
| 314 | 314 |
} else {
|
| 315 | 315 |
is.putback(c); |
| 316 | 316 |
} |
| 317 | 317 |
} |
| 318 | 318 |
str = os.str(); |
| 319 | 319 |
return is; |
| 320 | 320 |
} |
| 321 | 321 |
|
| 322 | 322 |
class Section {
|
| 323 | 323 |
public: |
| 324 | 324 |
virtual ~Section() {}
|
| 325 | 325 |
virtual void process(std::istream& is, int& line_num) = 0; |
| 326 | 326 |
}; |
| 327 | 327 |
|
| 328 | 328 |
template <typename Functor> |
| 329 | 329 |
class LineSection : public Section {
|
| 330 | 330 |
private: |
| 331 | 331 |
|
| 332 | 332 |
Functor _functor; |
| 333 | 333 |
|
| 334 | 334 |
public: |
| 335 | 335 |
|
| 336 | 336 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 337 | 337 |
virtual ~LineSection() {}
|
| 338 | 338 |
|
| 339 | 339 |
virtual void process(std::istream& is, int& line_num) {
|
| 340 | 340 |
char c; |
| 341 | 341 |
std::string line; |
| 342 | 342 |
while (is.get(c) && c != '@') {
|
| 343 | 343 |
if (c == '\n') {
|
| 344 | 344 |
++line_num; |
| 345 | 345 |
} else if (c == '#') {
|
| 346 | 346 |
getline(is, line); |
| 347 | 347 |
++line_num; |
| 348 | 348 |
} else if (!isWhiteSpace(c)) {
|
| 349 | 349 |
is.putback(c); |
| 350 | 350 |
getline(is, line); |
| 351 | 351 |
_functor(line); |
| 352 | 352 |
++line_num; |
| 353 | 353 |
} |
| 354 | 354 |
} |
| 355 | 355 |
if (is) is.putback(c); |
| 356 | 356 |
else if (is.eof()) is.clear(); |
| 357 | 357 |
} |
| 358 | 358 |
}; |
| 359 | 359 |
|
| 360 | 360 |
template <typename Functor> |
| 361 | 361 |
class StreamSection : public Section {
|
| 362 | 362 |
private: |
| 363 | 363 |
|
| 364 | 364 |
Functor _functor; |
| 365 | 365 |
|
| 366 | 366 |
public: |
| 367 | 367 |
|
| 368 | 368 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 369 | 369 |
virtual ~StreamSection() {}
|
| 370 | 370 |
|
| 371 | 371 |
virtual void process(std::istream& is, int& line_num) {
|
| 372 | 372 |
_functor(is, line_num); |
| 373 | 373 |
char c; |
| 374 | 374 |
std::string line; |
| 375 | 375 |
while (is.get(c) && c != '@') {
|
| 376 | 376 |
if (c == '\n') {
|
| 377 | 377 |
++line_num; |
| 378 | 378 |
} else if (!isWhiteSpace(c)) {
|
| 379 | 379 |
getline(is, line); |
| 380 | 380 |
++line_num; |
| 381 | 381 |
} |
| 382 | 382 |
} |
| 383 | 383 |
if (is) is.putback(c); |
| 384 | 384 |
else if (is.eof()) is.clear(); |
| 385 | 385 |
} |
| 386 | 386 |
}; |
| 387 | 387 |
|
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
template <typename Digraph> |
| 391 | 391 |
class DigraphReader; |
| 392 | 392 |
|
| 393 | 393 |
template <typename Digraph> |
| 394 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 394 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 395 | 395 |
std::istream& is = std::cin); |
| 396 | 396 |
template <typename Digraph> |
| 397 | 397 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const std::string& fn); |
| 398 | 398 |
template <typename Digraph> |
| 399 | 399 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char *fn); |
| 400 | 400 |
|
| 401 | 401 |
/// \ingroup lemon_io |
| 402 | 402 |
/// |
| 403 | 403 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
| 404 | 404 |
/// |
| 405 | 405 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 406 | 406 |
/// |
| 407 | 407 |
/// The reading method does a batch processing. The user creates a |
| 408 | 408 |
/// reader object, then various reading rules can be added to the |
| 409 | 409 |
/// reader, and eventually the reading is executed with the \c run() |
| 410 | 410 |
/// member function. A map reading rule can be added to the reader |
| 411 | 411 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 412 | 412 |
/// converter parameter can also be added as a standard functor |
| 413 | 413 |
/// converting from \c std::string to the value type of the map. If it |
| 414 | 414 |
/// is set, it will determine how the tokens in the file should be |
| 415 | 415 |
/// converted to the value type of the map. If the functor is not set, |
| 416 | 416 |
/// then a default conversion will be used. One map can be read into |
| 417 | 417 |
/// multiple map objects at the same time. The \c attribute(), \c |
| 418 | 418 |
/// node() and \c arc() functions are used to add attribute reading |
| 419 | 419 |
/// rules. |
| 420 | 420 |
/// |
| 421 | 421 |
///\code |
| 422 | 422 |
/// DigraphReader<Digraph>(digraph, std::cin). |
| 423 | 423 |
/// nodeMap("coordinates", coord_map).
|
| 424 | 424 |
/// arcMap("capacity", cap_map).
|
| 425 | 425 |
/// node("source", src).
|
| 426 | 426 |
/// node("target", trg).
|
| 427 | 427 |
/// attribute("caption", caption).
|
| 428 | 428 |
/// run(); |
| 429 | 429 |
///\endcode |
| 430 | 430 |
/// |
| 431 | 431 |
/// By default the reader uses the first section in the file of the |
| 432 | 432 |
/// proper type. If a section has an optional name, then it can be |
| 433 | 433 |
/// selected for reading by giving an optional name parameter to the |
| 434 | 434 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
| 435 | 435 |
/// |
| 436 | 436 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
| 437 | 437 |
/// that the nodes or arcs should not be constructed (added to the |
| 438 | 438 |
/// graph) during the reading, but instead the label map of the items |
| 439 | 439 |
/// are given as a parameter of these functions. An |
| 440 | 440 |
/// application of these functions is multipass reading, which is |
| 441 | 441 |
/// important if two \c \@arcs sections must be read from the |
| 442 | 442 |
/// file. In this case the first phase would read the node set and one |
| 443 | 443 |
/// of the arc sets, while the second phase would read the second arc |
| 444 | 444 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
| 445 | 445 |
/// The previously read label node map should be passed to the \c |
| 446 | 446 |
/// useNodes() functions. Another application of multipass reading when |
| 447 | 447 |
/// paths are given as a node map or an arc map. |
| 448 | 448 |
/// It is impossible to read this in |
| 449 | 449 |
/// a single pass, because the arcs are not constructed when the node |
| 450 | 450 |
/// maps are read. |
| 451 | 451 |
template <typename _Digraph> |
| 452 | 452 |
class DigraphReader {
|
| 453 | 453 |
public: |
| 454 | 454 |
|
| 455 | 455 |
typedef _Digraph Digraph; |
| 456 | 456 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 457 | 457 |
|
| 458 | 458 |
private: |
| 459 | 459 |
|
| 460 | 460 |
|
| 461 | 461 |
std::istream* _is; |
| 462 | 462 |
bool local_is; |
| 463 | 463 |
std::string _filename; |
| 464 | 464 |
|
| 465 | 465 |
Digraph& _digraph; |
| 466 | 466 |
|
| 467 | 467 |
std::string _nodes_caption; |
| 468 | 468 |
std::string _arcs_caption; |
| 469 | 469 |
std::string _attributes_caption; |
| 470 | 470 |
|
| 471 | 471 |
typedef std::map<std::string, Node> NodeIndex; |
| 472 | 472 |
NodeIndex _node_index; |
| 473 | 473 |
typedef std::map<std::string, Arc> ArcIndex; |
| 474 | 474 |
ArcIndex _arc_index; |
| 475 | 475 |
|
| 476 | 476 |
typedef std::vector<std::pair<std::string, |
| 477 | 477 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 478 | 478 |
NodeMaps _node_maps; |
| 479 | 479 |
|
| 480 | 480 |
typedef std::vector<std::pair<std::string, |
| 481 | 481 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
| 482 | 482 |
ArcMaps _arc_maps; |
| 483 | 483 |
|
| 484 | 484 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 485 | 485 |
Attributes; |
| 486 | 486 |
Attributes _attributes; |
| 487 | 487 |
|
| 488 | 488 |
bool _use_nodes; |
| 489 | 489 |
bool _use_arcs; |
| 490 | 490 |
|
| 491 | 491 |
bool _skip_nodes; |
| 492 | 492 |
bool _skip_arcs; |
| 493 | 493 |
|
| 494 | 494 |
int line_num; |
| 495 | 495 |
std::istringstream line; |
| 496 | 496 |
|
| 497 | 497 |
public: |
| 498 | 498 |
|
| 499 | 499 |
/// \brief Constructor |
| 500 | 500 |
/// |
| 501 | 501 |
/// Construct a directed graph reader, which reads from the given |
| 502 | 502 |
/// input stream. |
| 503 | 503 |
DigraphReader(Digraph& digraph, std::istream& is = std::cin) |
| 504 | 504 |
: _is(&is), local_is(false), _digraph(digraph), |
| 505 | 505 |
_use_nodes(false), _use_arcs(false), |
| 506 | 506 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 507 | 507 |
|
| 508 | 508 |
/// \brief Constructor |
| 509 | 509 |
/// |
| 510 | 510 |
/// Construct a directed graph reader, which reads from the given |
| 511 | 511 |
/// file. |
| 512 | 512 |
DigraphReader(Digraph& digraph, const std::string& fn) |
| 513 | 513 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 514 | 514 |
_filename(fn), _digraph(digraph), |
| 515 | 515 |
_use_nodes(false), _use_arcs(false), |
| 516 | 516 |
_skip_nodes(false), _skip_arcs(false) {
|
| 517 | 517 |
if (!(*_is)) {
|
| 518 | 518 |
delete _is; |
| 519 | 519 |
throw IoError("Cannot open file", fn);
|
| 520 | 520 |
} |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 | 523 |
/// \brief Constructor |
| 524 | 524 |
/// |
| 525 | 525 |
/// Construct a directed graph reader, which reads from the given |
| 526 | 526 |
/// file. |
| 527 | 527 |
DigraphReader(Digraph& digraph, const char* fn) |
| 528 | 528 |
: _is(new std::ifstream(fn)), local_is(true), |
| 529 | 529 |
_filename(fn), _digraph(digraph), |
| 530 | 530 |
_use_nodes(false), _use_arcs(false), |
| 531 | 531 |
_skip_nodes(false), _skip_arcs(false) {
|
| 532 | 532 |
if (!(*_is)) {
|
| 533 | 533 |
delete _is; |
| 534 | 534 |
throw IoError("Cannot open file", fn);
|
| 535 | 535 |
} |
| 536 | 536 |
} |
| 537 | 537 |
|
| 538 | 538 |
/// \brief Destructor |
| 539 | 539 |
~DigraphReader() {
|
| 540 | 540 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 541 | 541 |
it != _node_maps.end(); ++it) {
|
| 542 | 542 |
delete it->second; |
| 543 | 543 |
} |
| 544 | 544 |
|
| 545 | 545 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 546 | 546 |
it != _arc_maps.end(); ++it) {
|
| 547 | 547 |
delete it->second; |
| 548 | 548 |
} |
| 549 | 549 |
|
| 550 | 550 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 551 | 551 |
it != _attributes.end(); ++it) {
|
| 552 | 552 |
delete it->second; |
| 553 | 553 |
} |
| 554 | 554 |
|
| 555 | 555 |
if (local_is) {
|
| 556 | 556 |
delete _is; |
| 557 | 557 |
} |
| 558 | 558 |
|
| 559 | 559 |
} |
| 560 | 560 |
|
| 561 | 561 |
private: |
| 562 | 562 |
|
| 563 | 563 |
template <typename DGR> |
| 564 | 564 |
friend DigraphReader<DGR> digraphReader(DGR& digraph, std::istream& is); |
| 565 | 565 |
template <typename DGR> |
| 566 |
friend DigraphReader<DGR> digraphReader(DGR& digraph, |
|
| 566 |
friend DigraphReader<DGR> digraphReader(DGR& digraph, |
|
| 567 | 567 |
const std::string& fn); |
| 568 | 568 |
template <typename DGR> |
| 569 | 569 |
friend DigraphReader<DGR> digraphReader(DGR& digraph, const char *fn); |
| 570 | 570 |
|
| 571 | 571 |
DigraphReader(DigraphReader& other) |
| 572 | 572 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
| 573 | 573 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
| 574 | 574 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 575 | 575 |
|
| 576 | 576 |
other._is = 0; |
| 577 | 577 |
other.local_is = false; |
| 578 | 578 |
|
| 579 | 579 |
_node_index.swap(other._node_index); |
| 580 | 580 |
_arc_index.swap(other._arc_index); |
| 581 | 581 |
|
| 582 | 582 |
_node_maps.swap(other._node_maps); |
| 583 | 583 |
_arc_maps.swap(other._arc_maps); |
| 584 | 584 |
_attributes.swap(other._attributes); |
| 585 | 585 |
|
| 586 | 586 |
_nodes_caption = other._nodes_caption; |
| 587 | 587 |
_arcs_caption = other._arcs_caption; |
| 588 | 588 |
_attributes_caption = other._attributes_caption; |
| 589 | 589 |
|
| 590 | 590 |
} |
| 591 | 591 |
|
| 592 | 592 |
DigraphReader& operator=(const DigraphReader&); |
| 593 | 593 |
|
| 594 | 594 |
public: |
| 595 | 595 |
|
| 596 | 596 |
/// \name Reading rules |
| 597 | 597 |
/// @{
|
| 598 | 598 |
|
| 599 | 599 |
/// \brief Node map reading rule |
| 600 | 600 |
/// |
| 601 | 601 |
/// Add a node map reading rule to the reader. |
| 602 | 602 |
template <typename Map> |
| 603 | 603 |
DigraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 604 | 604 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 605 | 605 |
_reader_bits::MapStorageBase<Node>* storage = |
| 606 | 606 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 607 | 607 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 608 | 608 |
return *this; |
| 609 | 609 |
} |
| 610 | 610 |
|
| 611 | 611 |
/// \brief Node map reading rule |
| 612 | 612 |
/// |
| 613 | 613 |
/// Add a node map reading rule with specialized converter to the |
| 614 | 614 |
/// reader. |
| 615 | 615 |
template <typename Map, typename Converter> |
| 616 | 616 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
| 617 | 617 |
const Converter& converter = Converter()) {
|
| 618 | 618 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 619 | 619 |
_reader_bits::MapStorageBase<Node>* storage = |
| 620 | 620 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 621 | 621 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 622 | 622 |
return *this; |
| 623 | 623 |
} |
| 624 | 624 |
|
| 625 | 625 |
/// \brief Arc map reading rule |
| 626 | 626 |
/// |
| 627 | 627 |
/// Add an arc map reading rule to the reader. |
| 628 | 628 |
template <typename Map> |
| 629 | 629 |
DigraphReader& arcMap(const std::string& caption, Map& map) {
|
| 630 | 630 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 631 | 631 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 632 | 632 |
new _reader_bits::MapStorage<Arc, Map>(map); |
| 633 | 633 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 634 | 634 |
return *this; |
| 635 | 635 |
} |
| 636 | 636 |
|
| 637 | 637 |
/// \brief Arc map reading rule |
| 638 | 638 |
/// |
| 639 | 639 |
/// Add an arc map reading rule with specialized converter to the |
| 640 | 640 |
/// reader. |
| 641 | 641 |
template <typename Map, typename Converter> |
| 642 | 642 |
DigraphReader& arcMap(const std::string& caption, Map& map, |
| 643 | 643 |
const Converter& converter = Converter()) {
|
| 644 | 644 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 645 | 645 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 646 | 646 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 647 | 647 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 648 | 648 |
return *this; |
| 649 | 649 |
} |
| 650 | 650 |
|
| 651 | 651 |
/// \brief Attribute reading rule |
| 652 | 652 |
/// |
| 653 | 653 |
/// Add an attribute reading rule to the reader. |
| 654 | 654 |
template <typename Value> |
| 655 | 655 |
DigraphReader& attribute(const std::string& caption, Value& value) {
|
| 656 | 656 |
_reader_bits::ValueStorageBase* storage = |
| 657 | 657 |
new _reader_bits::ValueStorage<Value>(value); |
| 658 | 658 |
_attributes.insert(std::make_pair(caption, storage)); |
| 659 | 659 |
return *this; |
| 660 | 660 |
} |
| 661 | 661 |
|
| 662 | 662 |
/// \brief Attribute reading rule |
| 663 | 663 |
/// |
| 664 | 664 |
/// Add an attribute reading rule with specialized converter to the |
| 665 | 665 |
/// reader. |
| 666 | 666 |
template <typename Value, typename Converter> |
| 667 | 667 |
DigraphReader& attribute(const std::string& caption, Value& value, |
| 668 | 668 |
const Converter& converter = Converter()) {
|
| 669 | 669 |
_reader_bits::ValueStorageBase* storage = |
| 670 | 670 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 671 | 671 |
_attributes.insert(std::make_pair(caption, storage)); |
| 672 | 672 |
return *this; |
| 673 | 673 |
} |
| 674 | 674 |
|
| 675 | 675 |
/// \brief Node reading rule |
| 676 | 676 |
/// |
| 677 | 677 |
/// Add a node reading rule to reader. |
| 678 | 678 |
DigraphReader& node(const std::string& caption, Node& node) {
|
| 679 | 679 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 680 | 680 |
Converter converter(_node_index); |
| 681 | 681 |
_reader_bits::ValueStorageBase* storage = |
| 682 | 682 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 683 | 683 |
_attributes.insert(std::make_pair(caption, storage)); |
| 684 | 684 |
return *this; |
| 685 | 685 |
} |
| 686 | 686 |
|
| 687 | 687 |
/// \brief Arc reading rule |
| 688 | 688 |
/// |
| 689 | 689 |
/// Add an arc reading rule to reader. |
| 690 | 690 |
DigraphReader& arc(const std::string& caption, Arc& arc) {
|
| 691 | 691 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
| 692 | 692 |
Converter converter(_arc_index); |
| 693 | 693 |
_reader_bits::ValueStorageBase* storage = |
| 694 | 694 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 695 | 695 |
_attributes.insert(std::make_pair(caption, storage)); |
| 696 | 696 |
return *this; |
| 697 | 697 |
} |
| 698 | 698 |
|
| 699 | 699 |
/// @} |
| 700 | 700 |
|
| 701 | 701 |
/// \name Select section by name |
| 702 | 702 |
/// @{
|
| 703 | 703 |
|
| 704 | 704 |
/// \brief Set \c \@nodes section to be read |
| 705 | 705 |
/// |
| 706 | 706 |
/// Set \c \@nodes section to be read |
| 707 | 707 |
DigraphReader& nodes(const std::string& caption) {
|
| 708 | 708 |
_nodes_caption = caption; |
| 709 | 709 |
return *this; |
| 710 | 710 |
} |
| 711 | 711 |
|
| 712 | 712 |
/// \brief Set \c \@arcs section to be read |
| 713 | 713 |
/// |
| 714 | 714 |
/// Set \c \@arcs section to be read |
| 715 | 715 |
DigraphReader& arcs(const std::string& caption) {
|
| 716 | 716 |
_arcs_caption = caption; |
| 717 | 717 |
return *this; |
| 718 | 718 |
} |
| 719 | 719 |
|
| 720 | 720 |
/// \brief Set \c \@attributes section to be read |
| 721 | 721 |
/// |
| 722 | 722 |
/// Set \c \@attributes section to be read |
| 723 | 723 |
DigraphReader& attributes(const std::string& caption) {
|
| 724 | 724 |
_attributes_caption = caption; |
| 725 | 725 |
return *this; |
| 726 | 726 |
} |
| 727 | 727 |
|
| 728 | 728 |
/// @} |
| 729 | 729 |
|
| 730 | 730 |
/// \name Using previously constructed node or arc set |
| 731 | 731 |
/// @{
|
| 732 | 732 |
|
| 733 | 733 |
/// \brief Use previously constructed node set |
| 734 | 734 |
/// |
| 735 | 735 |
/// Use previously constructed node set, and specify the node |
| 736 | 736 |
/// label map. |
| 737 | 737 |
template <typename Map> |
| 738 | 738 |
DigraphReader& useNodes(const Map& map) {
|
| 739 | 739 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 740 | 740 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 741 | 741 |
_use_nodes = true; |
| 742 | 742 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 743 | 743 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 744 | 744 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 745 | 745 |
} |
| 746 | 746 |
return *this; |
| 747 | 747 |
} |
| 748 | 748 |
|
| 749 | 749 |
/// \brief Use previously constructed node set |
| 750 | 750 |
/// |
| 751 | 751 |
/// Use previously constructed node set, and specify the node |
| 752 | 752 |
/// label map and a functor which converts the label map values to |
| 753 | 753 |
/// \c std::string. |
| 754 | 754 |
template <typename Map, typename Converter> |
| 755 | 755 |
DigraphReader& useNodes(const Map& map, |
| 756 | 756 |
const Converter& converter = Converter()) {
|
| 757 | 757 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 758 | 758 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 759 | 759 |
_use_nodes = true; |
| 760 | 760 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 761 | 761 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 762 | 762 |
} |
| 763 | 763 |
return *this; |
| 764 | 764 |
} |
| 765 | 765 |
|
| 766 | 766 |
/// \brief Use previously constructed arc set |
| 767 | 767 |
/// |
| 768 | 768 |
/// Use previously constructed arc set, and specify the arc |
| 769 | 769 |
/// label map. |
| 770 | 770 |
template <typename Map> |
| 771 | 771 |
DigraphReader& useArcs(const Map& map) {
|
| 772 | 772 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 773 | 773 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 774 | 774 |
_use_arcs = true; |
| 775 | 775 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 776 | 776 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 777 | 777 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 778 | 778 |
} |
| 779 | 779 |
return *this; |
| 780 | 780 |
} |
| 781 | 781 |
|
| 782 | 782 |
/// \brief Use previously constructed arc set |
| 783 | 783 |
/// |
| 784 | 784 |
/// Use previously constructed arc set, and specify the arc |
| 785 | 785 |
/// label map and a functor which converts the label map values to |
| 786 | 786 |
/// \c std::string. |
| 787 | 787 |
template <typename Map, typename Converter> |
| 788 | 788 |
DigraphReader& useArcs(const Map& map, |
| 789 | 789 |
const Converter& converter = Converter()) {
|
| 790 | 790 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 791 | 791 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 792 | 792 |
_use_arcs = true; |
| 793 | 793 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 794 | 794 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 795 | 795 |
} |
| 796 | 796 |
return *this; |
| 797 | 797 |
} |
| 798 | 798 |
|
| 799 | 799 |
/// \brief Skips the reading of node section |
| 800 | 800 |
/// |
| 801 | 801 |
/// Omit the reading of the node section. This implies that each node |
| 802 | 802 |
/// map reading rule will be abandoned, and the nodes of the graph |
| 803 | 803 |
/// will not be constructed, which usually cause that the arc set |
| 804 | 804 |
/// could not be read due to lack of node name resolving. |
| 805 | 805 |
/// Therefore \c skipArcs() function should also be used, or |
| 806 | 806 |
/// \c useNodes() should be used to specify the label of the nodes. |
| 807 | 807 |
DigraphReader& skipNodes() {
|
| 808 | 808 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
| 809 | 809 |
_skip_nodes = true; |
| 810 | 810 |
return *this; |
| 811 | 811 |
} |
| 812 | 812 |
|
| 813 | 813 |
/// \brief Skips the reading of arc section |
| 814 | 814 |
/// |
| 815 | 815 |
/// Omit the reading of the arc section. This implies that each arc |
| 816 | 816 |
/// map reading rule will be abandoned, and the arcs of the graph |
| 817 | 817 |
/// will not be constructed. |
| 818 | 818 |
DigraphReader& skipArcs() {
|
| 819 | 819 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
| 820 | 820 |
_skip_arcs = true; |
| 821 | 821 |
return *this; |
| 822 | 822 |
} |
| 823 | 823 |
|
| 824 | 824 |
/// @} |
| 825 | 825 |
|
| 826 | 826 |
private: |
| 827 | 827 |
|
| 828 | 828 |
bool readLine() {
|
| 829 | 829 |
std::string str; |
| 830 | 830 |
while(++line_num, std::getline(*_is, str)) {
|
| 831 | 831 |
line.clear(); line.str(str); |
| 832 | 832 |
char c; |
| 833 | 833 |
if (line >> std::ws >> c && c != '#') {
|
| 834 | 834 |
line.putback(c); |
| 835 | 835 |
return true; |
| 836 | 836 |
} |
| 837 | 837 |
} |
| 838 | 838 |
return false; |
| 839 | 839 |
} |
| 840 | 840 |
|
| 841 | 841 |
bool readSuccess() {
|
| 842 | 842 |
return static_cast<bool>(*_is); |
| 843 | 843 |
} |
| 844 | 844 |
|
| 845 | 845 |
void skipSection() {
|
| 846 | 846 |
char c; |
| 847 | 847 |
while (readSuccess() && line >> c && c != '@') {
|
| 848 | 848 |
readLine(); |
| 849 | 849 |
} |
| 850 | 850 |
if (readSuccess()) {
|
| 851 | 851 |
line.putback(c); |
| 852 | 852 |
} |
| 853 | 853 |
} |
| 854 | 854 |
|
| 855 | 855 |
void readNodes() {
|
| 856 | 856 |
|
| 857 | 857 |
std::vector<int> map_index(_node_maps.size()); |
| 858 | 858 |
int map_num, label_index; |
| 859 | 859 |
|
| 860 | 860 |
char c; |
| 861 | 861 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 862 | 862 |
if (readSuccess() && line) line.putback(c); |
| 863 | 863 |
if (!_node_maps.empty()) |
| 864 | 864 |
throw FormatError("Cannot find map names");
|
| 865 | 865 |
return; |
| 866 | 866 |
} |
| 867 | 867 |
line.putback(c); |
| 868 | 868 |
|
| 869 | 869 |
{
|
| 870 | 870 |
std::map<std::string, int> maps; |
| 871 | 871 |
|
| 872 | 872 |
std::string map; |
| 873 | 873 |
int index = 0; |
| 874 | 874 |
while (_reader_bits::readToken(line, map)) {
|
| 875 | 875 |
if (maps.find(map) != maps.end()) {
|
| 876 | 876 |
std::ostringstream msg; |
| 877 | 877 |
msg << "Multiple occurence of node map: " << map; |
| 878 | 878 |
throw FormatError(msg.str()); |
| 879 | 879 |
} |
| 880 | 880 |
maps.insert(std::make_pair(map, index)); |
| 881 | 881 |
++index; |
| 882 | 882 |
} |
| 883 | 883 |
|
| 884 | 884 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 885 | 885 |
std::map<std::string, int>::iterator jt = |
| 886 | 886 |
maps.find(_node_maps[i].first); |
| 887 | 887 |
if (jt == maps.end()) {
|
| 888 | 888 |
std::ostringstream msg; |
| 889 | 889 |
msg << "Map not found: " << _node_maps[i].first; |
| 890 | 890 |
throw FormatError(msg.str()); |
| 891 | 891 |
} |
| 892 | 892 |
map_index[i] = jt->second; |
| 893 | 893 |
} |
| 894 | 894 |
|
| 895 | 895 |
{
|
| 896 | 896 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 897 | 897 |
if (jt != maps.end()) {
|
| 898 | 898 |
label_index = jt->second; |
| 899 | 899 |
} else {
|
| 900 | 900 |
label_index = -1; |
| 901 | 901 |
} |
| 902 | 902 |
} |
| 903 | 903 |
map_num = maps.size(); |
| 904 | 904 |
} |
| 905 | 905 |
|
| 906 | 906 |
while (readLine() && line >> c && c != '@') {
|
| 907 | 907 |
line.putback(c); |
| 908 | 908 |
|
| 909 | 909 |
std::vector<std::string> tokens(map_num); |
| 910 | 910 |
for (int i = 0; i < map_num; ++i) {
|
| 911 | 911 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 912 | 912 |
std::ostringstream msg; |
| 913 | 913 |
msg << "Column not found (" << i + 1 << ")";
|
| 914 | 914 |
throw FormatError(msg.str()); |
| 915 | 915 |
} |
| 916 | 916 |
} |
| 917 | 917 |
if (line >> std::ws >> c) |
| 918 | 918 |
throw FormatError("Extra character at the end of line");
|
| 919 | 919 |
|
| 920 | 920 |
Node n; |
| 921 | 921 |
if (!_use_nodes) {
|
| 922 | 922 |
n = _digraph.addNode(); |
| 923 | 923 |
if (label_index != -1) |
| 924 | 924 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
| 925 | 925 |
} else {
|
| 926 | 926 |
if (label_index == -1) |
| 927 | 927 |
throw FormatError("Label map not found");
|
| 928 | 928 |
typename std::map<std::string, Node>::iterator it = |
| 929 | 929 |
_node_index.find(tokens[label_index]); |
| 930 | 930 |
if (it == _node_index.end()) {
|
| 931 | 931 |
std::ostringstream msg; |
| 932 | 932 |
msg << "Node with label not found: " << tokens[label_index]; |
| 933 | 933 |
throw FormatError(msg.str()); |
| 934 | 934 |
} |
| 935 | 935 |
n = it->second; |
| 936 | 936 |
} |
| 937 | 937 |
|
| 938 | 938 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 939 | 939 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
| 940 | 940 |
} |
| 941 | 941 |
|
| 942 | 942 |
} |
| 943 | 943 |
if (readSuccess()) {
|
| 944 | 944 |
line.putback(c); |
| 945 | 945 |
} |
| 946 | 946 |
} |
| 947 | 947 |
|
| 948 | 948 |
void readArcs() {
|
| 949 | 949 |
|
| 950 | 950 |
std::vector<int> map_index(_arc_maps.size()); |
| 951 | 951 |
int map_num, label_index; |
| 952 | 952 |
|
| 953 | 953 |
char c; |
| 954 | 954 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 955 | 955 |
if (readSuccess() && line) line.putback(c); |
| 956 | 956 |
if (!_arc_maps.empty()) |
| 957 | 957 |
throw FormatError("Cannot find map names");
|
| 958 | 958 |
return; |
| 959 | 959 |
} |
| 960 | 960 |
line.putback(c); |
| 961 | 961 |
|
| 962 | 962 |
{
|
| 963 | 963 |
std::map<std::string, int> maps; |
| 964 | 964 |
|
| 965 | 965 |
std::string map; |
| 966 | 966 |
int index = 0; |
| 967 | 967 |
while (_reader_bits::readToken(line, map)) {
|
| 968 | 968 |
if(map == "-") {
|
| 969 | 969 |
if(index!=0) |
| 970 | 970 |
throw FormatError("'-' is not allowed as a map name");
|
| 971 | 971 |
else if (line >> std::ws >> c) |
| 972 | 972 |
throw FormatError("Extra character at the end of line");
|
| 973 | 973 |
else break; |
| 974 | 974 |
} |
| 975 | 975 |
if (maps.find(map) != maps.end()) {
|
| 976 | 976 |
std::ostringstream msg; |
| 977 | 977 |
msg << "Multiple occurence of arc map: " << map; |
| 978 | 978 |
throw FormatError(msg.str()); |
| 979 | 979 |
} |
| 980 | 980 |
maps.insert(std::make_pair(map, index)); |
| 981 | 981 |
++index; |
| 982 | 982 |
} |
| 983 | 983 |
|
| 984 | 984 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
| 985 | 985 |
std::map<std::string, int>::iterator jt = |
| 986 | 986 |
maps.find(_arc_maps[i].first); |
| 987 | 987 |
if (jt == maps.end()) {
|
| 988 | 988 |
std::ostringstream msg; |
| 989 | 989 |
msg << "Map not found: " << _arc_maps[i].first; |
| 990 | 990 |
throw FormatError(msg.str()); |
| 991 | 991 |
} |
| 992 | 992 |
map_index[i] = jt->second; |
| 993 | 993 |
} |
| 994 | 994 |
|
| 995 | 995 |
{
|
| 996 | 996 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 997 | 997 |
if (jt != maps.end()) {
|
| 998 | 998 |
label_index = jt->second; |
| 999 | 999 |
} else {
|
| 1000 | 1000 |
label_index = -1; |
| 1001 | 1001 |
} |
| 1002 | 1002 |
} |
| 1003 | 1003 |
map_num = maps.size(); |
| 1004 | 1004 |
} |
| 1005 | 1005 |
|
| 1006 | 1006 |
while (readLine() && line >> c && c != '@') {
|
| 1007 | 1007 |
line.putback(c); |
| 1008 | 1008 |
|
| 1009 | 1009 |
std::string source_token; |
| 1010 | 1010 |
std::string target_token; |
| 1011 | 1011 |
|
| 1012 | 1012 |
if (!_reader_bits::readToken(line, source_token)) |
| 1013 | 1013 |
throw FormatError("Source not found");
|
| 1014 | 1014 |
|
| 1015 | 1015 |
if (!_reader_bits::readToken(line, target_token)) |
| 1016 | 1016 |
throw FormatError("Target not found");
|
| 1017 | 1017 |
|
| 1018 | 1018 |
std::vector<std::string> tokens(map_num); |
| 1019 | 1019 |
for (int i = 0; i < map_num; ++i) {
|
| 1020 | 1020 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1021 | 1021 |
std::ostringstream msg; |
| 1022 | 1022 |
msg << "Column not found (" << i + 1 << ")";
|
| 1023 | 1023 |
throw FormatError(msg.str()); |
| 1024 | 1024 |
} |
| 1025 | 1025 |
} |
| 1026 | 1026 |
if (line >> std::ws >> c) |
| 1027 | 1027 |
throw FormatError("Extra character at the end of line");
|
| 1028 | 1028 |
|
| 1029 | 1029 |
Arc a; |
| 1030 | 1030 |
if (!_use_arcs) {
|
| 1031 | 1031 |
|
| 1032 | 1032 |
typename NodeIndex::iterator it; |
| 1033 | 1033 |
|
| 1034 | 1034 |
it = _node_index.find(source_token); |
| 1035 | 1035 |
if (it == _node_index.end()) {
|
| 1036 | 1036 |
std::ostringstream msg; |
| 1037 | 1037 |
msg << "Item not found: " << source_token; |
| 1038 | 1038 |
throw FormatError(msg.str()); |
| 1039 | 1039 |
} |
| 1040 | 1040 |
Node source = it->second; |
| 1041 | 1041 |
|
| 1042 | 1042 |
it = _node_index.find(target_token); |
| 1043 | 1043 |
if (it == _node_index.end()) {
|
| 1044 | 1044 |
std::ostringstream msg; |
| 1045 | 1045 |
msg << "Item not found: " << target_token; |
| 1046 | 1046 |
throw FormatError(msg.str()); |
| 1047 | 1047 |
} |
| 1048 | 1048 |
Node target = it->second; |
| 1049 | 1049 |
|
| 1050 | 1050 |
a = _digraph.addArc(source, target); |
| 1051 | 1051 |
if (label_index != -1) |
| 1052 | 1052 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
| 1053 | 1053 |
} else {
|
| 1054 | 1054 |
if (label_index == -1) |
| 1055 | 1055 |
throw FormatError("Label map not found");
|
| 1056 | 1056 |
typename std::map<std::string, Arc>::iterator it = |
| 1057 | 1057 |
_arc_index.find(tokens[label_index]); |
| 1058 | 1058 |
if (it == _arc_index.end()) {
|
| 1059 | 1059 |
std::ostringstream msg; |
| 1060 | 1060 |
msg << "Arc with label not found: " << tokens[label_index]; |
| 1061 | 1061 |
throw FormatError(msg.str()); |
| 1062 | 1062 |
} |
| 1063 | 1063 |
a = it->second; |
| 1064 | 1064 |
} |
| 1065 | 1065 |
|
| 1066 | 1066 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
| 1067 | 1067 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
| 1068 | 1068 |
} |
| 1069 | 1069 |
|
| 1070 | 1070 |
} |
| 1071 | 1071 |
if (readSuccess()) {
|
| 1072 | 1072 |
line.putback(c); |
| 1073 | 1073 |
} |
| 1074 | 1074 |
} |
| 1075 | 1075 |
|
| 1076 | 1076 |
void readAttributes() {
|
| 1077 | 1077 |
|
| 1078 | 1078 |
std::set<std::string> read_attr; |
| 1079 | 1079 |
|
| 1080 | 1080 |
char c; |
| 1081 | 1081 |
while (readLine() && line >> c && c != '@') {
|
| 1082 | 1082 |
line.putback(c); |
| 1083 | 1083 |
|
| 1084 | 1084 |
std::string attr, token; |
| 1085 | 1085 |
if (!_reader_bits::readToken(line, attr)) |
| 1086 | 1086 |
throw FormatError("Attribute name not found");
|
| 1087 | 1087 |
if (!_reader_bits::readToken(line, token)) |
| 1088 | 1088 |
throw FormatError("Attribute value not found");
|
| 1089 | 1089 |
if (line >> c) |
| 1090 | 1090 |
throw FormatError("Extra character at the end of line");
|
| 1091 | 1091 |
|
| 1092 | 1092 |
{
|
| 1093 | 1093 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1094 | 1094 |
if (it != read_attr.end()) {
|
| 1095 | 1095 |
std::ostringstream msg; |
| 1096 | 1096 |
msg << "Multiple occurence of attribute: " << attr; |
| 1097 | 1097 |
throw FormatError(msg.str()); |
| 1098 | 1098 |
} |
| 1099 | 1099 |
read_attr.insert(attr); |
| 1100 | 1100 |
} |
| 1101 | 1101 |
|
| 1102 | 1102 |
{
|
| 1103 | 1103 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1104 | 1104 |
while (it != _attributes.end() && it->first == attr) {
|
| 1105 | 1105 |
it->second->set(token); |
| 1106 | 1106 |
++it; |
| 1107 | 1107 |
} |
| 1108 | 1108 |
} |
| 1109 | 1109 |
|
| 1110 | 1110 |
} |
| 1111 | 1111 |
if (readSuccess()) {
|
| 1112 | 1112 |
line.putback(c); |
| 1113 | 1113 |
} |
| 1114 | 1114 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1115 | 1115 |
it != _attributes.end(); ++it) {
|
| 1116 | 1116 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1117 | 1117 |
std::ostringstream msg; |
| 1118 | 1118 |
msg << "Attribute not found: " << it->first; |
| 1119 | 1119 |
throw FormatError(msg.str()); |
| 1120 | 1120 |
} |
| 1121 | 1121 |
} |
| 1122 | 1122 |
} |
| 1123 | 1123 |
|
| 1124 | 1124 |
public: |
| 1125 | 1125 |
|
| 1126 | 1126 |
/// \name Execution of the reader |
| 1127 | 1127 |
/// @{
|
| 1128 | 1128 |
|
| 1129 | 1129 |
/// \brief Start the batch processing |
| 1130 | 1130 |
/// |
| 1131 | 1131 |
/// This function starts the batch processing |
| 1132 | 1132 |
void run() {
|
| 1133 | 1133 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1134 | 1134 |
|
| 1135 | 1135 |
bool nodes_done = _skip_nodes; |
| 1136 | 1136 |
bool arcs_done = _skip_arcs; |
| 1137 | 1137 |
bool attributes_done = false; |
| 1138 | 1138 |
|
| 1139 | 1139 |
line_num = 0; |
| 1140 | 1140 |
readLine(); |
| 1141 | 1141 |
skipSection(); |
| 1142 | 1142 |
|
| 1143 | 1143 |
while (readSuccess()) {
|
| 1144 | 1144 |
try {
|
| 1145 | 1145 |
char c; |
| 1146 | 1146 |
std::string section, caption; |
| 1147 | 1147 |
line >> c; |
| 1148 | 1148 |
_reader_bits::readToken(line, section); |
| 1149 | 1149 |
_reader_bits::readToken(line, caption); |
| 1150 | 1150 |
|
| 1151 | 1151 |
if (line >> c) |
| 1152 | 1152 |
throw FormatError("Extra character at the end of line");
|
| 1153 | 1153 |
|
| 1154 | 1154 |
if (section == "nodes" && !nodes_done) {
|
| 1155 | 1155 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1156 | 1156 |
readNodes(); |
| 1157 | 1157 |
nodes_done = true; |
| 1158 | 1158 |
} |
| 1159 | 1159 |
} else if ((section == "arcs" || section == "edges") && |
| 1160 | 1160 |
!arcs_done) {
|
| 1161 | 1161 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
| 1162 | 1162 |
readArcs(); |
| 1163 | 1163 |
arcs_done = true; |
| 1164 | 1164 |
} |
| 1165 | 1165 |
} else if (section == "attributes" && !attributes_done) {
|
| 1166 | 1166 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1167 | 1167 |
readAttributes(); |
| 1168 | 1168 |
attributes_done = true; |
| 1169 | 1169 |
} |
| 1170 | 1170 |
} else {
|
| 1171 | 1171 |
readLine(); |
| 1172 | 1172 |
skipSection(); |
| 1173 | 1173 |
} |
| 1174 | 1174 |
} catch (FormatError& error) {
|
| 1175 | 1175 |
error.line(line_num); |
| 1176 | 1176 |
error.file(_filename); |
| 1177 | 1177 |
throw; |
| 1178 | 1178 |
} |
| 1179 | 1179 |
} |
| 1180 | 1180 |
|
| 1181 | 1181 |
if (!nodes_done) {
|
| 1182 | 1182 |
throw FormatError("Section @nodes not found");
|
| 1183 | 1183 |
} |
| 1184 | 1184 |
|
| 1185 | 1185 |
if (!arcs_done) {
|
| 1186 | 1186 |
throw FormatError("Section @arcs not found");
|
| 1187 | 1187 |
} |
| 1188 | 1188 |
|
| 1189 | 1189 |
if (!attributes_done && !_attributes.empty()) {
|
| 1190 | 1190 |
throw FormatError("Section @attributes not found");
|
| 1191 | 1191 |
} |
| 1192 | 1192 |
|
| 1193 | 1193 |
} |
| 1194 | 1194 |
|
| 1195 | 1195 |
/// @} |
| 1196 | 1196 |
|
| 1197 | 1197 |
}; |
| 1198 | 1198 |
|
| 1199 | 1199 |
/// \brief Return a \ref DigraphReader class |
| 1200 | 1200 |
/// |
| 1201 | 1201 |
/// This function just returns a \ref DigraphReader class. |
| 1202 | 1202 |
/// \relates DigraphReader |
| 1203 | 1203 |
template <typename Digraph> |
| 1204 | 1204 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, std::istream& is) {
|
| 1205 | 1205 |
DigraphReader<Digraph> tmp(digraph, is); |
| 1206 | 1206 |
return tmp; |
| 1207 | 1207 |
} |
| 1208 | 1208 |
|
| 1209 | 1209 |
/// \brief Return a \ref DigraphReader class |
| 1210 | 1210 |
/// |
| 1211 | 1211 |
/// This function just returns a \ref DigraphReader class. |
| 1212 | 1212 |
/// \relates DigraphReader |
| 1213 | 1213 |
template <typename Digraph> |
| 1214 | 1214 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
| 1215 | 1215 |
const std::string& fn) {
|
| 1216 | 1216 |
DigraphReader<Digraph> tmp(digraph, fn); |
| 1217 | 1217 |
return tmp; |
| 1218 | 1218 |
} |
| 1219 | 1219 |
|
| 1220 | 1220 |
/// \brief Return a \ref DigraphReader class |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
/// This function just returns a \ref DigraphReader class. |
| 1223 | 1223 |
/// \relates DigraphReader |
| 1224 | 1224 |
template <typename Digraph> |
| 1225 | 1225 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
|
| 1226 | 1226 |
DigraphReader<Digraph> tmp(digraph, fn); |
| 1227 | 1227 |
return tmp; |
| 1228 | 1228 |
} |
| 1229 | 1229 |
|
| 1230 | 1230 |
template <typename Graph> |
| 1231 | 1231 |
class GraphReader; |
| 1232 |
|
|
| 1232 |
|
|
| 1233 | 1233 |
template <typename Graph> |
| 1234 |
GraphReader<Graph> graphReader(Graph& graph, |
|
| 1234 |
GraphReader<Graph> graphReader(Graph& graph, |
|
| 1235 | 1235 |
std::istream& is = std::cin); |
| 1236 | 1236 |
template <typename Graph> |
| 1237 | 1237 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); |
| 1238 | 1238 |
template <typename Graph> |
| 1239 | 1239 |
GraphReader<Graph> graphReader(Graph& graph, const char *fn); |
| 1240 | 1240 |
|
| 1241 | 1241 |
/// \ingroup lemon_io |
| 1242 | 1242 |
/// |
| 1243 | 1243 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
| 1244 | 1244 |
/// |
| 1245 | 1245 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1246 | 1246 |
/// |
| 1247 | 1247 |
/// It can be used almost the same way as \c DigraphReader. |
| 1248 | 1248 |
/// The only difference is that this class can handle edges and |
| 1249 | 1249 |
/// edge maps as well as arcs and arc maps. |
| 1250 | 1250 |
/// |
| 1251 | 1251 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
| 1252 | 1252 |
/// edge maps. However, if there are two maps with the same name |
| 1253 | 1253 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
| 1254 | 1254 |
/// arc map. Similarly, an attribute can be read into an arc, if |
| 1255 | 1255 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
| 1256 | 1256 |
template <typename _Graph> |
| 1257 | 1257 |
class GraphReader {
|
| 1258 | 1258 |
public: |
| 1259 | 1259 |
|
| 1260 | 1260 |
typedef _Graph Graph; |
| 1261 | 1261 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 1262 | 1262 |
|
| 1263 | 1263 |
private: |
| 1264 | 1264 |
|
| 1265 | 1265 |
std::istream* _is; |
| 1266 | 1266 |
bool local_is; |
| 1267 | 1267 |
std::string _filename; |
| 1268 | 1268 |
|
| 1269 | 1269 |
Graph& _graph; |
| 1270 | 1270 |
|
| 1271 | 1271 |
std::string _nodes_caption; |
| 1272 | 1272 |
std::string _edges_caption; |
| 1273 | 1273 |
std::string _attributes_caption; |
| 1274 | 1274 |
|
| 1275 | 1275 |
typedef std::map<std::string, Node> NodeIndex; |
| 1276 | 1276 |
NodeIndex _node_index; |
| 1277 | 1277 |
typedef std::map<std::string, Edge> EdgeIndex; |
| 1278 | 1278 |
EdgeIndex _edge_index; |
| 1279 | 1279 |
|
| 1280 | 1280 |
typedef std::vector<std::pair<std::string, |
| 1281 | 1281 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 1282 | 1282 |
NodeMaps _node_maps; |
| 1283 | 1283 |
|
| 1284 | 1284 |
typedef std::vector<std::pair<std::string, |
| 1285 | 1285 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 1286 | 1286 |
EdgeMaps _edge_maps; |
| 1287 | 1287 |
|
| 1288 | 1288 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 1289 | 1289 |
Attributes; |
| 1290 | 1290 |
Attributes _attributes; |
| 1291 | 1291 |
|
| 1292 | 1292 |
bool _use_nodes; |
| 1293 | 1293 |
bool _use_edges; |
| 1294 | 1294 |
|
| 1295 | 1295 |
bool _skip_nodes; |
| 1296 | 1296 |
bool _skip_edges; |
| 1297 | 1297 |
|
| 1298 | 1298 |
int line_num; |
| 1299 | 1299 |
std::istringstream line; |
| 1300 | 1300 |
|
| 1301 | 1301 |
public: |
| 1302 | 1302 |
|
| 1303 | 1303 |
/// \brief Constructor |
| 1304 | 1304 |
/// |
| 1305 | 1305 |
/// Construct an undirected graph reader, which reads from the given |
| 1306 | 1306 |
/// input stream. |
| 1307 | 1307 |
GraphReader(Graph& graph, std::istream& is = std::cin) |
| 1308 | 1308 |
: _is(&is), local_is(false), _graph(graph), |
| 1309 | 1309 |
_use_nodes(false), _use_edges(false), |
| 1310 | 1310 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1311 | 1311 |
|
| 1312 | 1312 |
/// \brief Constructor |
| 1313 | 1313 |
/// |
| 1314 | 1314 |
/// Construct an undirected graph reader, which reads from the given |
| 1315 | 1315 |
/// file. |
| 1316 | 1316 |
GraphReader(Graph& graph, const std::string& fn) |
| 1317 | 1317 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 1318 | 1318 |
_filename(fn), _graph(graph), |
| 1319 | 1319 |
_use_nodes(false), _use_edges(false), |
| 1320 | 1320 |
_skip_nodes(false), _skip_edges(false) {
|
| 1321 | 1321 |
if (!(*_is)) {
|
| 1322 | 1322 |
delete _is; |
| 1323 | 1323 |
throw IoError("Cannot open file", fn);
|
| 1324 | 1324 |
} |
| 1325 | 1325 |
} |
| 1326 | 1326 |
|
| 1327 | 1327 |
/// \brief Constructor |
| 1328 | 1328 |
/// |
| 1329 | 1329 |
/// Construct an undirected graph reader, which reads from the given |
| 1330 | 1330 |
/// file. |
| 1331 | 1331 |
GraphReader(Graph& graph, const char* fn) |
| 1332 | 1332 |
: _is(new std::ifstream(fn)), local_is(true), |
| 1333 | 1333 |
_filename(fn), _graph(graph), |
| 1334 | 1334 |
_use_nodes(false), _use_edges(false), |
| 1335 | 1335 |
_skip_nodes(false), _skip_edges(false) {
|
| 1336 | 1336 |
if (!(*_is)) {
|
| 1337 | 1337 |
delete _is; |
| 1338 | 1338 |
throw IoError("Cannot open file", fn);
|
| 1339 | 1339 |
} |
| 1340 | 1340 |
} |
| 1341 | 1341 |
|
| 1342 | 1342 |
/// \brief Destructor |
| 1343 | 1343 |
~GraphReader() {
|
| 1344 | 1344 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1345 | 1345 |
it != _node_maps.end(); ++it) {
|
| 1346 | 1346 |
delete it->second; |
| 1347 | 1347 |
} |
| 1348 | 1348 |
|
| 1349 | 1349 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1350 | 1350 |
it != _edge_maps.end(); ++it) {
|
| 1351 | 1351 |
delete it->second; |
| 1352 | 1352 |
} |
| 1353 | 1353 |
|
| 1354 | 1354 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1355 | 1355 |
it != _attributes.end(); ++it) {
|
| 1356 | 1356 |
delete it->second; |
| 1357 | 1357 |
} |
| 1358 | 1358 |
|
| 1359 | 1359 |
if (local_is) {
|
| 1360 | 1360 |
delete _is; |
| 1361 | 1361 |
} |
| 1362 | 1362 |
|
| 1363 | 1363 |
} |
| 1364 | 1364 |
|
| 1365 | 1365 |
private: |
| 1366 | 1366 |
template <typename GR> |
| 1367 | 1367 |
friend GraphReader<GR> graphReader(GR& graph, std::istream& is); |
| 1368 | 1368 |
template <typename GR> |
| 1369 |
friend GraphReader<GR> graphReader(GR& graph, const std::string& fn); |
|
| 1369 |
friend GraphReader<GR> graphReader(GR& graph, const std::string& fn); |
|
| 1370 | 1370 |
template <typename GR> |
| 1371 | 1371 |
friend GraphReader<GR> graphReader(GR& graph, const char *fn); |
| 1372 | 1372 |
|
| 1373 | 1373 |
GraphReader(GraphReader& other) |
| 1374 | 1374 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 1375 | 1375 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
| 1376 | 1376 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1377 | 1377 |
|
| 1378 | 1378 |
other._is = 0; |
| 1379 | 1379 |
other.local_is = false; |
| 1380 | 1380 |
|
| 1381 | 1381 |
_node_index.swap(other._node_index); |
| 1382 | 1382 |
_edge_index.swap(other._edge_index); |
| 1383 | 1383 |
|
| 1384 | 1384 |
_node_maps.swap(other._node_maps); |
| 1385 | 1385 |
_edge_maps.swap(other._edge_maps); |
| 1386 | 1386 |
_attributes.swap(other._attributes); |
| 1387 | 1387 |
|
| 1388 | 1388 |
_nodes_caption = other._nodes_caption; |
| 1389 | 1389 |
_edges_caption = other._edges_caption; |
| 1390 | 1390 |
_attributes_caption = other._attributes_caption; |
| 1391 | 1391 |
|
| 1392 | 1392 |
} |
| 1393 | 1393 |
|
| 1394 | 1394 |
GraphReader& operator=(const GraphReader&); |
| 1395 | 1395 |
|
| 1396 | 1396 |
public: |
| 1397 | 1397 |
|
| 1398 | 1398 |
/// \name Reading rules |
| 1399 | 1399 |
/// @{
|
| 1400 | 1400 |
|
| 1401 | 1401 |
/// \brief Node map reading rule |
| 1402 | 1402 |
/// |
| 1403 | 1403 |
/// Add a node map reading rule to the reader. |
| 1404 | 1404 |
template <typename Map> |
| 1405 | 1405 |
GraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 1406 | 1406 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1407 | 1407 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1408 | 1408 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 1409 | 1409 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1410 | 1410 |
return *this; |
| 1411 | 1411 |
} |
| 1412 | 1412 |
|
| 1413 | 1413 |
/// \brief Node map reading rule |
| 1414 | 1414 |
/// |
| 1415 | 1415 |
/// Add a node map reading rule with specialized converter to the |
| 1416 | 1416 |
/// reader. |
| 1417 | 1417 |
template <typename Map, typename Converter> |
| 1418 | 1418 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
| 1419 | 1419 |
const Converter& converter = Converter()) {
|
| 1420 | 1420 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1421 | 1421 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1422 | 1422 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1423 | 1423 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1424 | 1424 |
return *this; |
| 1425 | 1425 |
} |
| 1426 | 1426 |
|
| 1427 | 1427 |
/// \brief Edge map reading rule |
| 1428 | 1428 |
/// |
| 1429 | 1429 |
/// Add an edge map reading rule to the reader. |
| 1430 | 1430 |
template <typename Map> |
| 1431 | 1431 |
GraphReader& edgeMap(const std::string& caption, Map& map) {
|
| 1432 | 1432 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1433 | 1433 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1434 | 1434 |
new _reader_bits::MapStorage<Edge, Map>(map); |
| 1435 | 1435 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1436 | 1436 |
return *this; |
| 1437 | 1437 |
} |
| 1438 | 1438 |
|
| 1439 | 1439 |
/// \brief Edge map reading rule |
| 1440 | 1440 |
/// |
| 1441 | 1441 |
/// Add an edge map reading rule with specialized converter to the |
| 1442 | 1442 |
/// reader. |
| 1443 | 1443 |
template <typename Map, typename Converter> |
| 1444 | 1444 |
GraphReader& edgeMap(const std::string& caption, Map& map, |
| 1445 | 1445 |
const Converter& converter = Converter()) {
|
| 1446 | 1446 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1447 | 1447 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1448 | 1448 |
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1449 | 1449 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1450 | 1450 |
return *this; |
| 1451 | 1451 |
} |
| 1452 | 1452 |
|
| 1453 | 1453 |
/// \brief Arc map reading rule |
| 1454 | 1454 |
/// |
| 1455 | 1455 |
/// Add an arc map reading rule to the reader. |
| 1456 | 1456 |
template <typename Map> |
| 1457 | 1457 |
GraphReader& arcMap(const std::string& caption, Map& map) {
|
| 1458 | 1458 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1459 | 1459 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1460 | 1460 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1461 | 1461 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1462 | 1462 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1463 | 1463 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1464 | 1464 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1465 | 1465 |
return *this; |
| 1466 | 1466 |
} |
| 1467 | 1467 |
|
| 1468 | 1468 |
/// \brief Arc map reading rule |
| 1469 | 1469 |
/// |
| 1470 | 1470 |
/// Add an arc map reading rule with specialized converter to the |
| 1471 | 1471 |
/// reader. |
| 1472 | 1472 |
template <typename Map, typename Converter> |
| 1473 | 1473 |
GraphReader& arcMap(const std::string& caption, Map& map, |
| 1474 | 1474 |
const Converter& converter = Converter()) {
|
| 1475 | 1475 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1476 | 1476 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1477 | 1477 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1478 | 1478 |
(_graph, map, converter); |
| 1479 | 1479 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1480 | 1480 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1481 | 1481 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1482 | 1482 |
(_graph, map, converter); |
| 1483 | 1483 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1484 | 1484 |
return *this; |
| 1485 | 1485 |
} |
| 1486 | 1486 |
|
| 1487 | 1487 |
/// \brief Attribute reading rule |
| 1488 | 1488 |
/// |
| 1489 | 1489 |
/// Add an attribute reading rule to the reader. |
| 1490 | 1490 |
template <typename Value> |
| 1491 | 1491 |
GraphReader& attribute(const std::string& caption, Value& value) {
|
| 1492 | 1492 |
_reader_bits::ValueStorageBase* storage = |
| 1493 | 1493 |
new _reader_bits::ValueStorage<Value>(value); |
| 1494 | 1494 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1495 | 1495 |
return *this; |
| 1496 | 1496 |
} |
| 1497 | 1497 |
|
| 1498 | 1498 |
/// \brief Attribute reading rule |
| 1499 | 1499 |
/// |
| 1500 | 1500 |
/// Add an attribute reading rule with specialized converter to the |
| 1501 | 1501 |
/// reader. |
| 1502 | 1502 |
template <typename Value, typename Converter> |
| 1503 | 1503 |
GraphReader& attribute(const std::string& caption, Value& value, |
| 1504 | 1504 |
const Converter& converter = Converter()) {
|
| 1505 | 1505 |
_reader_bits::ValueStorageBase* storage = |
| 1506 | 1506 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 1507 | 1507 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1508 | 1508 |
return *this; |
| 1509 | 1509 |
} |
| 1510 | 1510 |
|
| 1511 | 1511 |
/// \brief Node reading rule |
| 1512 | 1512 |
/// |
| 1513 | 1513 |
/// Add a node reading rule to reader. |
| 1514 | 1514 |
GraphReader& node(const std::string& caption, Node& node) {
|
| 1515 | 1515 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 1516 | 1516 |
Converter converter(_node_index); |
| 1517 | 1517 |
_reader_bits::ValueStorageBase* storage = |
| 1518 | 1518 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 1519 | 1519 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1520 | 1520 |
return *this; |
| 1521 | 1521 |
} |
| 1522 | 1522 |
|
| 1523 | 1523 |
/// \brief Edge reading rule |
| 1524 | 1524 |
/// |
| 1525 | 1525 |
/// Add an edge reading rule to reader. |
| 1526 | 1526 |
GraphReader& edge(const std::string& caption, Edge& edge) {
|
| 1527 | 1527 |
typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
| 1528 | 1528 |
Converter converter(_edge_index); |
| 1529 | 1529 |
_reader_bits::ValueStorageBase* storage = |
| 1530 | 1530 |
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1531 | 1531 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1532 | 1532 |
return *this; |
| 1533 | 1533 |
} |
| 1534 | 1534 |
|
| 1535 | 1535 |
/// \brief Arc reading rule |
| 1536 | 1536 |
/// |
| 1537 | 1537 |
/// Add an arc reading rule to reader. |
| 1538 | 1538 |
GraphReader& arc(const std::string& caption, Arc& arc) {
|
| 1539 | 1539 |
typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1540 | 1540 |
Converter converter(_graph, _edge_index); |
| 1541 | 1541 |
_reader_bits::ValueStorageBase* storage = |
| 1542 | 1542 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1543 | 1543 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1544 | 1544 |
return *this; |
| 1545 | 1545 |
} |
| 1546 | 1546 |
|
| 1547 | 1547 |
/// @} |
| 1548 | 1548 |
|
| 1549 | 1549 |
/// \name Select section by name |
| 1550 | 1550 |
/// @{
|
| 1551 | 1551 |
|
| 1552 | 1552 |
/// \brief Set \c \@nodes section to be read |
| 1553 | 1553 |
/// |
| 1554 | 1554 |
/// Set \c \@nodes section to be read. |
| 1555 | 1555 |
GraphReader& nodes(const std::string& caption) {
|
| 1556 | 1556 |
_nodes_caption = caption; |
| 1557 | 1557 |
return *this; |
| 1558 | 1558 |
} |
| 1559 | 1559 |
|
| 1560 | 1560 |
/// \brief Set \c \@edges section to be read |
| 1561 | 1561 |
/// |
| 1562 | 1562 |
/// Set \c \@edges section to be read. |
| 1563 | 1563 |
GraphReader& edges(const std::string& caption) {
|
| 1564 | 1564 |
_edges_caption = caption; |
| 1565 | 1565 |
return *this; |
| 1566 | 1566 |
} |
| 1567 | 1567 |
|
| 1568 | 1568 |
/// \brief Set \c \@attributes section to be read |
| 1569 | 1569 |
/// |
| 1570 | 1570 |
/// Set \c \@attributes section to be read. |
| 1571 | 1571 |
GraphReader& attributes(const std::string& caption) {
|
| 1572 | 1572 |
_attributes_caption = caption; |
| 1573 | 1573 |
return *this; |
| 1574 | 1574 |
} |
| 1575 | 1575 |
|
| 1576 | 1576 |
/// @} |
| 1577 | 1577 |
|
| 1578 | 1578 |
/// \name Using previously constructed node or edge set |
| 1579 | 1579 |
/// @{
|
| 1580 | 1580 |
|
| 1581 | 1581 |
/// \brief Use previously constructed node set |
| 1582 | 1582 |
/// |
| 1583 | 1583 |
/// Use previously constructed node set, and specify the node |
| 1584 | 1584 |
/// label map. |
| 1585 | 1585 |
template <typename Map> |
| 1586 | 1586 |
GraphReader& useNodes(const Map& map) {
|
| 1587 | 1587 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1588 | 1588 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 1589 | 1589 |
_use_nodes = true; |
| 1590 | 1590 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 1591 | 1591 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1592 | 1592 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 1593 | 1593 |
} |
| 1594 | 1594 |
return *this; |
| 1595 | 1595 |
} |
| 1596 | 1596 |
|
| 1597 | 1597 |
/// \brief Use previously constructed node set |
| 1598 | 1598 |
/// |
| 1599 | 1599 |
/// Use previously constructed node set, and specify the node |
| 1600 | 1600 |
/// label map and a functor which converts the label map values to |
| 1601 | 1601 |
/// \c std::string. |
| 1602 | 1602 |
template <typename Map, typename Converter> |
| 1603 | 1603 |
GraphReader& useNodes(const Map& map, |
| 1604 | 1604 |
const Converter& converter = Converter()) {
|
| 1605 | 1605 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1606 | 1606 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 1607 | 1607 |
_use_nodes = true; |
| 1608 | 1608 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1609 | 1609 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 1610 | 1610 |
} |
| 1611 | 1611 |
return *this; |
| 1612 | 1612 |
} |
| 1613 | 1613 |
|
| 1614 | 1614 |
/// \brief Use previously constructed edge set |
| 1615 | 1615 |
/// |
| 1616 | 1616 |
/// Use previously constructed edge set, and specify the edge |
| 1617 | 1617 |
/// label map. |
| 1618 | 1618 |
template <typename Map> |
| 1619 | 1619 |
GraphReader& useEdges(const Map& map) {
|
| 1620 | 1620 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1621 | 1621 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1622 | 1622 |
_use_edges = true; |
| 1623 | 1623 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 1624 | 1624 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1625 | 1625 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1626 | 1626 |
} |
| 1627 | 1627 |
return *this; |
| 1628 | 1628 |
} |
| 1629 | 1629 |
|
| 1630 | 1630 |
/// \brief Use previously constructed edge set |
| 1631 | 1631 |
/// |
| 1632 | 1632 |
/// Use previously constructed edge set, and specify the edge |
| 1633 | 1633 |
/// label map and a functor which converts the label map values to |
| 1634 | 1634 |
/// \c std::string. |
| 1635 | 1635 |
template <typename Map, typename Converter> |
| 1636 | 1636 |
GraphReader& useEdges(const Map& map, |
| 1637 | 1637 |
const Converter& converter = Converter()) {
|
| 1638 | 1638 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1639 | 1639 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1640 | 1640 |
_use_edges = true; |
| 1641 | 1641 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1642 | 1642 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1643 | 1643 |
} |
| 1644 | 1644 |
return *this; |
| 1645 | 1645 |
} |
| 1646 | 1646 |
|
| 1647 | 1647 |
/// \brief Skip the reading of node section |
| 1648 | 1648 |
/// |
| 1649 | 1649 |
/// Omit the reading of the node section. This implies that each node |
| 1650 | 1650 |
/// map reading rule will be abandoned, and the nodes of the graph |
| 1651 | 1651 |
/// will not be constructed, which usually cause that the edge set |
| 1652 | 1652 |
/// could not be read due to lack of node name |
| 1653 | 1653 |
/// could not be read due to lack of node name resolving. |
| 1654 | 1654 |
/// Therefore \c skipEdges() function should also be used, or |
| 1655 | 1655 |
/// \c useNodes() should be used to specify the label of the nodes. |
| 1656 | 1656 |
GraphReader& skipNodes() {
|
| 1657 | 1657 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
| 1658 | 1658 |
_skip_nodes = true; |
| 1659 | 1659 |
return *this; |
| 1660 | 1660 |
} |
| 1661 | 1661 |
|
| 1662 | 1662 |
/// \brief Skip the reading of edge section |
| 1663 | 1663 |
/// |
| 1664 | 1664 |
/// Omit the reading of the edge section. This implies that each edge |
| 1665 | 1665 |
/// map reading rule will be abandoned, and the edges of the graph |
| 1666 | 1666 |
/// will not be constructed. |
| 1667 | 1667 |
GraphReader& skipEdges() {
|
| 1668 | 1668 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
| 1669 | 1669 |
_skip_edges = true; |
| 1670 | 1670 |
return *this; |
| 1671 | 1671 |
} |
| 1672 | 1672 |
|
| 1673 | 1673 |
/// @} |
| 1674 | 1674 |
|
| 1675 | 1675 |
private: |
| 1676 | 1676 |
|
| 1677 | 1677 |
bool readLine() {
|
| 1678 | 1678 |
std::string str; |
| 1679 | 1679 |
while(++line_num, std::getline(*_is, str)) {
|
| 1680 | 1680 |
line.clear(); line.str(str); |
| 1681 | 1681 |
char c; |
| 1682 | 1682 |
if (line >> std::ws >> c && c != '#') {
|
| 1683 | 1683 |
line.putback(c); |
| 1684 | 1684 |
return true; |
| 1685 | 1685 |
} |
| 1686 | 1686 |
} |
| 1687 | 1687 |
return false; |
| 1688 | 1688 |
} |
| 1689 | 1689 |
|
| 1690 | 1690 |
bool readSuccess() {
|
| 1691 | 1691 |
return static_cast<bool>(*_is); |
| 1692 | 1692 |
} |
| 1693 | 1693 |
|
| 1694 | 1694 |
void skipSection() {
|
| 1695 | 1695 |
char c; |
| 1696 | 1696 |
while (readSuccess() && line >> c && c != '@') {
|
| 1697 | 1697 |
readLine(); |
| 1698 | 1698 |
} |
| 1699 | 1699 |
if (readSuccess()) {
|
| 1700 | 1700 |
line.putback(c); |
| 1701 | 1701 |
} |
| 1702 | 1702 |
} |
| 1703 | 1703 |
|
| 1704 | 1704 |
void readNodes() {
|
| 1705 | 1705 |
|
| 1706 | 1706 |
std::vector<int> map_index(_node_maps.size()); |
| 1707 | 1707 |
int map_num, label_index; |
| 1708 | 1708 |
|
| 1709 | 1709 |
char c; |
| 1710 | 1710 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 1711 | 1711 |
if (readSuccess() && line) line.putback(c); |
| 1712 | 1712 |
if (!_node_maps.empty()) |
| 1713 | 1713 |
throw FormatError("Cannot find map names");
|
| 1714 | 1714 |
return; |
| 1715 | 1715 |
} |
| 1716 | 1716 |
line.putback(c); |
| 1717 | 1717 |
|
| 1718 | 1718 |
{
|
| 1719 | 1719 |
std::map<std::string, int> maps; |
| 1720 | 1720 |
|
| 1721 | 1721 |
std::string map; |
| 1722 | 1722 |
int index = 0; |
| 1723 | 1723 |
while (_reader_bits::readToken(line, map)) {
|
| 1724 | 1724 |
if (maps.find(map) != maps.end()) {
|
| 1725 | 1725 |
std::ostringstream msg; |
| 1726 | 1726 |
msg << "Multiple occurence of node map: " << map; |
| 1727 | 1727 |
throw FormatError(msg.str()); |
| 1728 | 1728 |
} |
| 1729 | 1729 |
maps.insert(std::make_pair(map, index)); |
| 1730 | 1730 |
++index; |
| 1731 | 1731 |
} |
| 1732 | 1732 |
|
| 1733 | 1733 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 1734 | 1734 |
std::map<std::string, int>::iterator jt = |
| 1735 | 1735 |
maps.find(_node_maps[i].first); |
| 1736 | 1736 |
if (jt == maps.end()) {
|
| 1737 | 1737 |
std::ostringstream msg; |
| 1738 | 1738 |
msg << "Map not found: " << _node_maps[i].first; |
| 1739 | 1739 |
throw FormatError(msg.str()); |
| 1740 | 1740 |
} |
| 1741 | 1741 |
map_index[i] = jt->second; |
| 1742 | 1742 |
} |
| 1743 | 1743 |
|
| 1744 | 1744 |
{
|
| 1745 | 1745 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 1746 | 1746 |
if (jt != maps.end()) {
|
| 1747 | 1747 |
label_index = jt->second; |
| 1748 | 1748 |
} else {
|
| 1749 | 1749 |
label_index = -1; |
| 1750 | 1750 |
} |
| 1751 | 1751 |
} |
| 1752 | 1752 |
map_num = maps.size(); |
| 1753 | 1753 |
} |
| 1754 | 1754 |
|
| 1755 | 1755 |
while (readLine() && line >> c && c != '@') {
|
| 1756 | 1756 |
line.putback(c); |
| 1757 | 1757 |
|
| 1758 | 1758 |
std::vector<std::string> tokens(map_num); |
| 1759 | 1759 |
for (int i = 0; i < map_num; ++i) {
|
| 1760 | 1760 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1761 | 1761 |
std::ostringstream msg; |
| 1762 | 1762 |
msg << "Column not found (" << i + 1 << ")";
|
| 1763 | 1763 |
throw FormatError(msg.str()); |
| 1764 | 1764 |
} |
| 1765 | 1765 |
} |
| 1766 | 1766 |
if (line >> std::ws >> c) |
| 1767 | 1767 |
throw FormatError("Extra character at the end of line");
|
| 1768 | 1768 |
|
| 1769 | 1769 |
Node n; |
| 1770 | 1770 |
if (!_use_nodes) {
|
| 1771 | 1771 |
n = _graph.addNode(); |
| 1772 | 1772 |
if (label_index != -1) |
| 1773 | 1773 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
| 1774 | 1774 |
} else {
|
| 1775 | 1775 |
if (label_index == -1) |
| 1776 | 1776 |
throw FormatError("Label map not found");
|
| 1777 | 1777 |
typename std::map<std::string, Node>::iterator it = |
| 1778 | 1778 |
_node_index.find(tokens[label_index]); |
| 1779 | 1779 |
if (it == _node_index.end()) {
|
| 1780 | 1780 |
std::ostringstream msg; |
| 1781 | 1781 |
msg << "Node with label not found: " << tokens[label_index]; |
| 1782 | 1782 |
throw FormatError(msg.str()); |
| 1783 | 1783 |
} |
| 1784 | 1784 |
n = it->second; |
| 1785 | 1785 |
} |
| 1786 | 1786 |
|
| 1787 | 1787 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 1788 | 1788 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
| 1789 | 1789 |
} |
| 1790 | 1790 |
|
| 1791 | 1791 |
} |
| 1792 | 1792 |
if (readSuccess()) {
|
| 1793 | 1793 |
line.putback(c); |
| 1794 | 1794 |
} |
| 1795 | 1795 |
} |
| 1796 | 1796 |
|
| 1797 | 1797 |
void readEdges() {
|
| 1798 | 1798 |
|
| 1799 | 1799 |
std::vector<int> map_index(_edge_maps.size()); |
| 1800 | 1800 |
int map_num, label_index; |
| 1801 | 1801 |
|
| 1802 | 1802 |
char c; |
| 1803 | 1803 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 1804 | 1804 |
if (readSuccess() && line) line.putback(c); |
| 1805 | 1805 |
if (!_edge_maps.empty()) |
| 1806 | 1806 |
throw FormatError("Cannot find map names");
|
| 1807 | 1807 |
return; |
| 1808 | 1808 |
} |
| 1809 | 1809 |
line.putback(c); |
| 1810 | 1810 |
|
| 1811 | 1811 |
{
|
| 1812 | 1812 |
std::map<std::string, int> maps; |
| 1813 | 1813 |
|
| 1814 | 1814 |
std::string map; |
| 1815 | 1815 |
int index = 0; |
| 1816 | 1816 |
while (_reader_bits::readToken(line, map)) {
|
| 1817 | 1817 |
if(map == "-") {
|
| 1818 | 1818 |
if(index!=0) |
| 1819 | 1819 |
throw FormatError("'-' is not allowed as a map name");
|
| 1820 | 1820 |
else if (line >> std::ws >> c) |
| 1821 | 1821 |
throw FormatError("Extra character at the end of line");
|
| 1822 | 1822 |
else break; |
| 1823 | 1823 |
} |
| 1824 | 1824 |
if (maps.find(map) != maps.end()) {
|
| 1825 | 1825 |
std::ostringstream msg; |
| 1826 | 1826 |
msg << "Multiple occurence of edge map: " << map; |
| 1827 | 1827 |
throw FormatError(msg.str()); |
| 1828 | 1828 |
} |
| 1829 | 1829 |
maps.insert(std::make_pair(map, index)); |
| 1830 | 1830 |
++index; |
| 1831 | 1831 |
} |
| 1832 | 1832 |
|
| 1833 | 1833 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
| 1834 | 1834 |
std::map<std::string, int>::iterator jt = |
| 1835 | 1835 |
maps.find(_edge_maps[i].first); |
| 1836 | 1836 |
if (jt == maps.end()) {
|
| 1837 | 1837 |
std::ostringstream msg; |
| 1838 | 1838 |
msg << "Map not found: " << _edge_maps[i].first; |
| 1839 | 1839 |
throw FormatError(msg.str()); |
| 1840 | 1840 |
} |
| 1841 | 1841 |
map_index[i] = jt->second; |
| 1842 | 1842 |
} |
| 1843 | 1843 |
|
| 1844 | 1844 |
{
|
| 1845 | 1845 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 1846 | 1846 |
if (jt != maps.end()) {
|
| 1847 | 1847 |
label_index = jt->second; |
| 1848 | 1848 |
} else {
|
| 1849 | 1849 |
label_index = -1; |
| 1850 | 1850 |
} |
| 1851 | 1851 |
} |
| 1852 | 1852 |
map_num = maps.size(); |
| 1853 | 1853 |
} |
| 1854 | 1854 |
|
| 1855 | 1855 |
while (readLine() && line >> c && c != '@') {
|
| 1856 | 1856 |
line.putback(c); |
| 1857 | 1857 |
|
| 1858 | 1858 |
std::string source_token; |
| 1859 | 1859 |
std::string target_token; |
| 1860 | 1860 |
|
| 1861 | 1861 |
if (!_reader_bits::readToken(line, source_token)) |
| 1862 | 1862 |
throw FormatError("Node u not found");
|
| 1863 | 1863 |
|
| 1864 | 1864 |
if (!_reader_bits::readToken(line, target_token)) |
| 1865 | 1865 |
throw FormatError("Node v not found");
|
| 1866 | 1866 |
|
| 1867 | 1867 |
std::vector<std::string> tokens(map_num); |
| 1868 | 1868 |
for (int i = 0; i < map_num; ++i) {
|
| 1869 | 1869 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1870 | 1870 |
std::ostringstream msg; |
| 1871 | 1871 |
msg << "Column not found (" << i + 1 << ")";
|
| 1872 | 1872 |
throw FormatError(msg.str()); |
| 1873 | 1873 |
} |
| 1874 | 1874 |
} |
| 1875 | 1875 |
if (line >> std::ws >> c) |
| 1876 | 1876 |
throw FormatError("Extra character at the end of line");
|
| 1877 | 1877 |
|
| 1878 | 1878 |
Edge e; |
| 1879 | 1879 |
if (!_use_edges) {
|
| 1880 | 1880 |
|
| 1881 | 1881 |
typename NodeIndex::iterator it; |
| 1882 | 1882 |
|
| 1883 | 1883 |
it = _node_index.find(source_token); |
| 1884 | 1884 |
if (it == _node_index.end()) {
|
| 1885 | 1885 |
std::ostringstream msg; |
| 1886 | 1886 |
msg << "Item not found: " << source_token; |
| 1887 | 1887 |
throw FormatError(msg.str()); |
| 1888 | 1888 |
} |
| 1889 | 1889 |
Node source = it->second; |
| 1890 | 1890 |
|
| 1891 | 1891 |
it = _node_index.find(target_token); |
| 1892 | 1892 |
if (it == _node_index.end()) {
|
| 1893 | 1893 |
std::ostringstream msg; |
| 1894 | 1894 |
msg << "Item not found: " << target_token; |
| 1895 | 1895 |
throw FormatError(msg.str()); |
| 1896 | 1896 |
} |
| 1897 | 1897 |
Node target = it->second; |
| 1898 | 1898 |
|
| 1899 | 1899 |
e = _graph.addEdge(source, target); |
| 1900 | 1900 |
if (label_index != -1) |
| 1901 | 1901 |
_edge_index.insert(std::make_pair(tokens[label_index], e)); |
| 1902 | 1902 |
} else {
|
| 1903 | 1903 |
if (label_index == -1) |
| 1904 | 1904 |
throw FormatError("Label map not found");
|
| 1905 | 1905 |
typename std::map<std::string, Edge>::iterator it = |
| 1906 | 1906 |
_edge_index.find(tokens[label_index]); |
| 1907 | 1907 |
if (it == _edge_index.end()) {
|
| 1908 | 1908 |
std::ostringstream msg; |
| 1909 | 1909 |
msg << "Edge with label not found: " << tokens[label_index]; |
| 1910 | 1910 |
throw FormatError(msg.str()); |
| 1911 | 1911 |
} |
| 1912 | 1912 |
e = it->second; |
| 1913 | 1913 |
} |
| 1914 | 1914 |
|
| 1915 | 1915 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
| 1916 | 1916 |
_edge_maps[i].second->set(e, tokens[map_index[i]]); |
| 1917 | 1917 |
} |
| 1918 | 1918 |
|
| 1919 | 1919 |
} |
| 1920 | 1920 |
if (readSuccess()) {
|
| 1921 | 1921 |
line.putback(c); |
| 1922 | 1922 |
} |
| 1923 | 1923 |
} |
| 1924 | 1924 |
|
| 1925 | 1925 |
void readAttributes() {
|
| 1926 | 1926 |
|
| 1927 | 1927 |
std::set<std::string> read_attr; |
| 1928 | 1928 |
|
| 1929 | 1929 |
char c; |
| 1930 | 1930 |
while (readLine() && line >> c && c != '@') {
|
| 1931 | 1931 |
line.putback(c); |
| 1932 | 1932 |
|
| 1933 | 1933 |
std::string attr, token; |
| 1934 | 1934 |
if (!_reader_bits::readToken(line, attr)) |
| 1935 | 1935 |
throw FormatError("Attribute name not found");
|
| 1936 | 1936 |
if (!_reader_bits::readToken(line, token)) |
| 1937 | 1937 |
throw FormatError("Attribute value not found");
|
| 1938 | 1938 |
if (line >> c) |
| 1939 | 1939 |
throw FormatError("Extra character at the end of line");
|
| 1940 | 1940 |
|
| 1941 | 1941 |
{
|
| 1942 | 1942 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1943 | 1943 |
if (it != read_attr.end()) {
|
| 1944 | 1944 |
std::ostringstream msg; |
| 1945 | 1945 |
msg << "Multiple occurence of attribute: " << attr; |
| 1946 | 1946 |
throw FormatError(msg.str()); |
| 1947 | 1947 |
} |
| 1948 | 1948 |
read_attr.insert(attr); |
| 1949 | 1949 |
} |
| 1950 | 1950 |
|
| 1951 | 1951 |
{
|
| 1952 | 1952 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1953 | 1953 |
while (it != _attributes.end() && it->first == attr) {
|
| 1954 | 1954 |
it->second->set(token); |
| 1955 | 1955 |
++it; |
| 1956 | 1956 |
} |
| 1957 | 1957 |
} |
| 1958 | 1958 |
|
| 1959 | 1959 |
} |
| 1960 | 1960 |
if (readSuccess()) {
|
| 1961 | 1961 |
line.putback(c); |
| 1962 | 1962 |
} |
| 1963 | 1963 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1964 | 1964 |
it != _attributes.end(); ++it) {
|
| 1965 | 1965 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1966 | 1966 |
std::ostringstream msg; |
| 1967 | 1967 |
msg << "Attribute not found: " << it->first; |
| 1968 | 1968 |
throw FormatError(msg.str()); |
| 1969 | 1969 |
} |
| 1970 | 1970 |
} |
| 1971 | 1971 |
} |
| 1972 | 1972 |
|
| 1973 | 1973 |
public: |
| 1974 | 1974 |
|
| 1975 | 1975 |
/// \name Execution of the reader |
| 1976 | 1976 |
/// @{
|
| 1977 | 1977 |
|
| 1978 | 1978 |
/// \brief Start the batch processing |
| 1979 | 1979 |
/// |
| 1980 | 1980 |
/// This function starts the batch processing |
| 1981 | 1981 |
void run() {
|
| 1982 | 1982 |
|
| 1983 | 1983 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1984 | 1984 |
|
| 1985 | 1985 |
bool nodes_done = _skip_nodes; |
| 1986 | 1986 |
bool edges_done = _skip_edges; |
| 1987 | 1987 |
bool attributes_done = false; |
| 1988 | 1988 |
|
| 1989 | 1989 |
line_num = 0; |
| 1990 | 1990 |
readLine(); |
| 1991 | 1991 |
skipSection(); |
| 1992 | 1992 |
|
| 1993 | 1993 |
while (readSuccess()) {
|
| 1994 | 1994 |
try {
|
| 1995 | 1995 |
char c; |
| 1996 | 1996 |
std::string section, caption; |
| 1997 | 1997 |
line >> c; |
| 1998 | 1998 |
_reader_bits::readToken(line, section); |
| 1999 | 1999 |
_reader_bits::readToken(line, caption); |
| 2000 | 2000 |
|
| 2001 | 2001 |
if (line >> c) |
| 2002 | 2002 |
throw FormatError("Extra character at the end of line");
|
| 2003 | 2003 |
|
| 2004 | 2004 |
if (section == "nodes" && !nodes_done) {
|
| 2005 | 2005 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 2006 | 2006 |
readNodes(); |
| 2007 | 2007 |
nodes_done = true; |
| 2008 | 2008 |
} |
| 2009 | 2009 |
} else if ((section == "edges" || section == "arcs") && |
| 2010 | 2010 |
!edges_done) {
|
| 2011 | 2011 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
| 2012 | 2012 |
readEdges(); |
| 2013 | 2013 |
edges_done = true; |
| 2014 | 2014 |
} |
| 2015 | 2015 |
} else if (section == "attributes" && !attributes_done) {
|
| 2016 | 2016 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 2017 | 2017 |
readAttributes(); |
| 2018 | 2018 |
attributes_done = true; |
| 2019 | 2019 |
} |
| 2020 | 2020 |
} else {
|
| 2021 | 2021 |
readLine(); |
| 2022 | 2022 |
skipSection(); |
| 2023 | 2023 |
} |
| 2024 | 2024 |
} catch (FormatError& error) {
|
| 2025 | 2025 |
error.line(line_num); |
| 2026 | 2026 |
error.file(_filename); |
| 2027 | 2027 |
throw; |
| 2028 | 2028 |
} |
| 2029 | 2029 |
} |
| 2030 | 2030 |
|
| 2031 | 2031 |
if (!nodes_done) {
|
| 2032 | 2032 |
throw FormatError("Section @nodes not found");
|
| 2033 | 2033 |
} |
| 2034 | 2034 |
|
| 2035 | 2035 |
if (!edges_done) {
|
| 2036 | 2036 |
throw FormatError("Section @edges not found");
|
| 2037 | 2037 |
} |
| 2038 | 2038 |
|
| 2039 | 2039 |
if (!attributes_done && !_attributes.empty()) {
|
| 2040 | 2040 |
throw FormatError("Section @attributes not found");
|
| 2041 | 2041 |
} |
| 2042 | 2042 |
|
| 2043 | 2043 |
} |
| 2044 | 2044 |
|
| 2045 | 2045 |
/// @} |
| 2046 | 2046 |
|
| 2047 | 2047 |
}; |
| 2048 | 2048 |
|
| 2049 | 2049 |
/// \brief Return a \ref GraphReader class |
| 2050 | 2050 |
/// |
| 2051 | 2051 |
/// This function just returns a \ref GraphReader class. |
| 2052 | 2052 |
/// \relates GraphReader |
| 2053 | 2053 |
template <typename Graph> |
| 2054 | 2054 |
GraphReader<Graph> graphReader(Graph& graph, std::istream& is) {
|
| 2055 | 2055 |
GraphReader<Graph> tmp(graph, is); |
| 2056 | 2056 |
return tmp; |
| 2057 | 2057 |
} |
| 2058 | 2058 |
|
| 2059 | 2059 |
/// \brief Return a \ref GraphReader class |
| 2060 | 2060 |
/// |
| 2061 | 2061 |
/// This function just returns a \ref GraphReader class. |
| 2062 | 2062 |
/// \relates GraphReader |
| 2063 | 2063 |
template <typename Graph> |
| 2064 | 2064 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
|
| 2065 | 2065 |
GraphReader<Graph> tmp(graph, fn); |
| 2066 | 2066 |
return tmp; |
| 2067 | 2067 |
} |
| 2068 | 2068 |
|
| 2069 | 2069 |
/// \brief Return a \ref GraphReader class |
| 2070 | 2070 |
/// |
| 2071 | 2071 |
/// This function just returns a \ref GraphReader class. |
| 2072 | 2072 |
/// \relates GraphReader |
| 2073 | 2073 |
template <typename Graph> |
| 2074 | 2074 |
GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
|
| 2075 | 2075 |
GraphReader<Graph> tmp(graph, fn); |
| 2076 | 2076 |
return tmp; |
| 2077 | 2077 |
} |
| 2078 | 2078 |
|
| 2079 | 2079 |
class SectionReader; |
| 2080 | 2080 |
|
| 2081 | 2081 |
SectionReader sectionReader(std::istream& is); |
| 2082 | 2082 |
SectionReader sectionReader(const std::string& fn); |
| 2083 | 2083 |
SectionReader sectionReader(const char* fn); |
| 2084 | 2084 |
|
| 2085 | 2085 |
/// \ingroup lemon_io |
| 2086 | 2086 |
/// |
| 2087 | 2087 |
/// \brief Section reader class |
| 2088 | 2088 |
/// |
| 2089 | 2089 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 2090 | 2090 |
/// which contain any data in arbitrary format. Such sections can be |
| 2091 | 2091 |
/// read with this class. A reading rule can be added to the class |
| 2092 | 2092 |
/// with two different functions. With the \c sectionLines() function a |
| 2093 | 2093 |
/// functor can process the section line-by-line, while with the \c |
| 2094 | 2094 |
/// sectionStream() member the section can be read from an input |
| 2095 | 2095 |
/// stream. |
| 2096 | 2096 |
class SectionReader {
|
| 2097 | 2097 |
private: |
| 2098 | 2098 |
|
| 2099 | 2099 |
std::istream* _is; |
| 2100 | 2100 |
bool local_is; |
| 2101 | 2101 |
std::string _filename; |
| 2102 | 2102 |
|
| 2103 | 2103 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 2104 | 2104 |
Sections _sections; |
| 2105 | 2105 |
|
| 2106 | 2106 |
int line_num; |
| 2107 | 2107 |
std::istringstream line; |
| 2108 | 2108 |
|
| 2109 | 2109 |
public: |
| 2110 | 2110 |
|
| 2111 | 2111 |
/// \brief Constructor |
| 2112 | 2112 |
/// |
| 2113 | 2113 |
/// Construct a section reader, which reads from the given input |
| 2114 | 2114 |
/// stream. |
| 2115 | 2115 |
SectionReader(std::istream& is) |
| 2116 | 2116 |
: _is(&is), local_is(false) {}
|
| 2117 | 2117 |
|
| 2118 | 2118 |
/// \brief Constructor |
| 2119 | 2119 |
/// |
| 2120 | 2120 |
/// Construct a section reader, which reads from the given file. |
| 2121 | 2121 |
SectionReader(const std::string& fn) |
| 2122 | 2122 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 2123 | 2123 |
_filename(fn) {
|
| 2124 | 2124 |
if (!(*_is)) {
|
| 2125 | 2125 |
delete _is; |
| 2126 | 2126 |
throw IoError("Cannot open file", fn);
|
| 2127 | 2127 |
} |
| 2128 | 2128 |
} |
| 2129 | 2129 |
|
| 2130 | 2130 |
/// \brief Constructor |
| 2131 | 2131 |
/// |
| 2132 | 2132 |
/// Construct a section reader, which reads from the given file. |
| 2133 | 2133 |
SectionReader(const char* fn) |
| 2134 | 2134 |
: _is(new std::ifstream(fn)), local_is(true), |
| 2135 | 2135 |
_filename(fn) {
|
| 2136 | 2136 |
if (!(*_is)) {
|
| 2137 | 2137 |
delete _is; |
| 2138 | 2138 |
throw IoError("Cannot open file", fn);
|
| 2139 | 2139 |
} |
| 2140 | 2140 |
} |
| 2141 | 2141 |
|
| 2142 | 2142 |
/// \brief Destructor |
| 2143 | 2143 |
~SectionReader() {
|
| 2144 | 2144 |
for (Sections::iterator it = _sections.begin(); |
| 2145 | 2145 |
it != _sections.end(); ++it) {
|
| 2146 | 2146 |
delete it->second; |
| 2147 | 2147 |
} |
| 2148 | 2148 |
|
| 2149 | 2149 |
if (local_is) {
|
| 2150 | 2150 |
delete _is; |
| 2151 | 2151 |
} |
| 2152 | 2152 |
|
| 2153 | 2153 |
} |
| 2154 | 2154 |
|
| 2155 | 2155 |
private: |
| 2156 | 2156 |
|
| 2157 | 2157 |
friend SectionReader sectionReader(std::istream& is); |
| 2158 | 2158 |
friend SectionReader sectionReader(const std::string& fn); |
| 2159 | 2159 |
friend SectionReader sectionReader(const char* fn); |
| 2160 | 2160 |
|
| 2161 | 2161 |
SectionReader(SectionReader& other) |
| 2162 | 2162 |
: _is(other._is), local_is(other.local_is) {
|
| 2163 | 2163 |
|
| 2164 | 2164 |
other._is = 0; |
| 2165 | 2165 |
other.local_is = false; |
| 2166 | 2166 |
|
| 2167 | 2167 |
_sections.swap(other._sections); |
| 2168 | 2168 |
} |
| 2169 | 2169 |
|
| 2170 | 2170 |
SectionReader& operator=(const SectionReader&); |
| 2171 | 2171 |
|
| 2172 | 2172 |
public: |
| 2173 | 2173 |
|
| 2174 | 2174 |
/// \name Section readers |
| 2175 | 2175 |
/// @{
|
| 2176 | 2176 |
|
| 2177 | 2177 |
/// \brief Add a section processor with line oriented reading |
| 2178 | 2178 |
/// |
| 2179 | 2179 |
/// The first parameter is the type descriptor of the section, the |
| 2180 | 2180 |
/// second is a functor, which takes just one \c std::string |
| 2181 | 2181 |
/// parameter. At the reading process, each line of the section |
| 2182 | 2182 |
/// will be given to the functor object. However, the empty lines |
| 2183 | 2183 |
/// and the comment lines are filtered out, and the leading |
| 2184 | 2184 |
/// whitespaces are trimmed from each processed string. |
| 2185 | 2185 |
/// |
| 2186 | 2186 |
/// For example let's see a section, which contain several |
| 2187 | 2187 |
/// integers, which should be inserted into a vector. |
| 2188 | 2188 |
///\code |
| 2189 | 2189 |
/// @numbers |
| 2190 | 2190 |
/// 12 45 23 |
| 2191 | 2191 |
/// 4 |
| 2192 | 2192 |
/// 23 6 |
| 2193 | 2193 |
///\endcode |
| 2194 | 2194 |
/// |
| 2195 | 2195 |
/// The functor is implemented as a struct: |
| 2196 | 2196 |
///\code |
| 2197 | 2197 |
/// struct NumberSection {
|
| 2198 | 2198 |
/// std::vector<int>& _data; |
| 2199 | 2199 |
/// NumberSection(std::vector<int>& data) : _data(data) {}
|
| 2200 | 2200 |
/// void operator()(const std::string& line) {
|
| 2201 | 2201 |
/// std::istringstream ls(line); |
| 2202 | 2202 |
/// int value; |
| 2203 | 2203 |
/// while (ls >> value) _data.push_back(value); |
| 2204 | 2204 |
/// } |
| 2205 | 2205 |
/// }; |
| 2206 | 2206 |
/// |
| 2207 | 2207 |
/// // ... |
| 2208 | 2208 |
/// |
| 2209 | 2209 |
/// reader.sectionLines("numbers", NumberSection(vec));
|
| 2210 | 2210 |
///\endcode |
| 2211 | 2211 |
template <typename Functor> |
| 2212 | 2212 |
SectionReader& sectionLines(const std::string& type, Functor functor) {
|
| 2213 | 2213 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2214 | 2214 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2215 | 2215 |
"Multiple reading of section."); |
| 2216 | 2216 |
_sections.insert(std::make_pair(type, |
| 2217 | 2217 |
new _reader_bits::LineSection<Functor>(functor))); |
| 2218 | 2218 |
return *this; |
| 2219 | 2219 |
} |
| 2220 | 2220 |
|
| 2221 | 2221 |
|
| 2222 | 2222 |
/// \brief Add a section processor with stream oriented reading |
| 2223 | 2223 |
/// |
| 2224 | 2224 |
/// The first parameter is the type of the section, the second is |
| 2225 | 2225 |
/// a functor, which takes an \c std::istream& and an \c int& |
| 2226 | 2226 |
/// parameter, the latter regard to the line number of stream. The |
| 2227 | 2227 |
/// functor can read the input while the section go on, and the |
| 2228 | 2228 |
/// line number should be modified accordingly. |
| 2229 | 2229 |
template <typename Functor> |
| 2230 | 2230 |
SectionReader& sectionStream(const std::string& type, Functor functor) {
|
| 2231 | 2231 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2232 | 2232 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2233 | 2233 |
"Multiple reading of section."); |
| 2234 | 2234 |
_sections.insert(std::make_pair(type, |
| 2235 | 2235 |
new _reader_bits::StreamSection<Functor>(functor))); |
| 2236 | 2236 |
return *this; |
| 2237 | 2237 |
} |
| 2238 | 2238 |
|
| 2239 | 2239 |
/// @} |
| 2240 | 2240 |
|
| 2241 | 2241 |
private: |
| 2242 | 2242 |
|
| 2243 | 2243 |
bool readLine() {
|
| 2244 | 2244 |
std::string str; |
| 2245 | 2245 |
while(++line_num, std::getline(*_is, str)) {
|
| 2246 | 2246 |
line.clear(); line.str(str); |
| 2247 | 2247 |
char c; |
| 2248 | 2248 |
if (line >> std::ws >> c && c != '#') {
|
| 2249 | 2249 |
line.putback(c); |
| 2250 | 2250 |
return true; |
| 2251 | 2251 |
} |
| 2252 | 2252 |
} |
| 2253 | 2253 |
return false; |
| 2254 | 2254 |
} |
| 2255 | 2255 |
|
| 2256 | 2256 |
bool readSuccess() {
|
| 2257 | 2257 |
return static_cast<bool>(*_is); |
| 2258 | 2258 |
} |
| 2259 | 2259 |
|
| 2260 | 2260 |
void skipSection() {
|
| 2261 | 2261 |
char c; |
| 2262 | 2262 |
while (readSuccess() && line >> c && c != '@') {
|
| 2263 | 2263 |
readLine(); |
| 2264 | 2264 |
} |
| 2265 | 2265 |
if (readSuccess()) {
|
| 2266 | 2266 |
line.putback(c); |
| 2267 | 2267 |
} |
| 2268 | 2268 |
} |
| 2269 | 2269 |
|
| 2270 | 2270 |
public: |
| 2271 | 2271 |
|
| 2272 | 2272 |
|
| 2273 | 2273 |
/// \name Execution of the reader |
| 2274 | 2274 |
/// @{
|
| 2275 | 2275 |
|
| 2276 | 2276 |
/// \brief Start the batch processing |
| 2277 | 2277 |
/// |
| 2278 | 2278 |
/// This function starts the batch processing. |
| 2279 | 2279 |
void run() {
|
| 2280 | 2280 |
|
| 2281 | 2281 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 2282 | 2282 |
|
| 2283 | 2283 |
std::set<std::string> extra_sections; |
| 2284 | 2284 |
|
| 2285 | 2285 |
line_num = 0; |
| 2286 | 2286 |
readLine(); |
| 2287 | 2287 |
skipSection(); |
| 2288 | 2288 |
|
| 2289 | 2289 |
while (readSuccess()) {
|
| 2290 | 2290 |
try {
|
| 2291 | 2291 |
char c; |
| 2292 | 2292 |
std::string section, caption; |
| 2293 | 2293 |
line >> c; |
| 2294 | 2294 |
_reader_bits::readToken(line, section); |
| 2295 | 2295 |
_reader_bits::readToken(line, caption); |
| 2296 | 2296 |
|
| 2297 | 2297 |
if (line >> c) |
| 2298 | 2298 |
throw FormatError("Extra character at the end of line");
|
| 2299 | 2299 |
|
| 2300 | 2300 |
if (extra_sections.find(section) != extra_sections.end()) {
|
| 2301 | 2301 |
std::ostringstream msg; |
| 2302 | 2302 |
msg << "Multiple occurence of section: " << section; |
| 2303 | 2303 |
throw FormatError(msg.str()); |
| 2304 | 2304 |
} |
| 2305 | 2305 |
Sections::iterator it = _sections.find(section); |
| 2306 | 2306 |
if (it != _sections.end()) {
|
| 2307 | 2307 |
extra_sections.insert(section); |
| 2308 | 2308 |
it->second->process(*_is, line_num); |
| 2309 | 2309 |
} |
| 2310 | 2310 |
readLine(); |
| 2311 | 2311 |
skipSection(); |
| 2312 | 2312 |
} catch (FormatError& error) {
|
| 2313 | 2313 |
error.line(line_num); |
| 2314 | 2314 |
error.file(_filename); |
| 2315 | 2315 |
throw; |
| 2316 | 2316 |
} |
| 2317 | 2317 |
} |
| 2318 | 2318 |
for (Sections::iterator it = _sections.begin(); |
| 2319 | 2319 |
it != _sections.end(); ++it) {
|
| 2320 | 2320 |
if (extra_sections.find(it->first) == extra_sections.end()) {
|
| 2321 | 2321 |
std::ostringstream os; |
| 2322 | 2322 |
os << "Cannot find section: " << it->first; |
| 2323 | 2323 |
throw FormatError(os.str()); |
| 2324 | 2324 |
} |
| 2325 | 2325 |
} |
| 2326 | 2326 |
} |
| 2327 | 2327 |
|
| 2328 | 2328 |
/// @} |
| 2329 | 2329 |
|
| 2330 | 2330 |
}; |
| 2331 | 2331 |
|
| 2332 | 2332 |
/// \brief Return a \ref SectionReader class |
| 2333 | 2333 |
/// |
| 2334 | 2334 |
/// This function just returns a \ref SectionReader class. |
| 2335 | 2335 |
/// \relates SectionReader |
| 2336 | 2336 |
inline SectionReader sectionReader(std::istream& is) {
|
| 2337 | 2337 |
SectionReader tmp(is); |
| 2338 | 2338 |
return tmp; |
| 2339 | 2339 |
} |
| 2340 | 2340 |
|
| 2341 | 2341 |
/// \brief Return a \ref SectionReader class |
| 2342 | 2342 |
/// |
| 2343 | 2343 |
/// This function just returns a \ref SectionReader class. |
| 2344 | 2344 |
/// \relates SectionReader |
| 2345 | 2345 |
inline SectionReader sectionReader(const std::string& fn) {
|
| 2346 | 2346 |
SectionReader tmp(fn); |
| 2347 | 2347 |
return tmp; |
| 2348 | 2348 |
} |
| 2349 | 2349 |
|
| 2350 | 2350 |
/// \brief Return a \ref SectionReader class |
| 2351 | 2351 |
/// |
| 2352 | 2352 |
/// This function just returns a \ref SectionReader class. |
| 2353 | 2353 |
/// \relates SectionReader |
| 2354 | 2354 |
inline SectionReader sectionReader(const char* fn) {
|
| 2355 | 2355 |
SectionReader tmp(fn); |
| 2356 | 2356 |
return tmp; |
| 2357 | 2357 |
} |
| 2358 | 2358 |
|
| 2359 | 2359 |
/// \ingroup lemon_io |
| 2360 | 2360 |
/// |
| 2361 | 2361 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file |
| 2362 | 2362 |
/// |
| 2363 | 2363 |
/// This class can be used to read the sections, the map names and |
| 2364 | 2364 |
/// the attributes from a file. Usually, the LEMON programs know |
| 2365 | 2365 |
/// that, which type of graph, which maps and which attributes |
| 2366 | 2366 |
/// should be read from a file, but in general tools (like glemon) |
| 2367 | 2367 |
/// the contents of an LGF file should be guessed somehow. This class |
| 2368 | 2368 |
/// reads the graph and stores the appropriate information for |
| 2369 | 2369 |
/// reading the graph. |
| 2370 | 2370 |
/// |
| 2371 | 2371 |
///\code |
| 2372 | 2372 |
/// LgfContents contents("graph.lgf");
|
| 2373 | 2373 |
/// contents.run(); |
| 2374 | 2374 |
/// |
| 2375 | 2375 |
/// // Does it contain any node section and arc section? |
| 2376 | 2376 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
|
| 2377 | 2377 |
/// std::cerr << "Failure, cannot find graph." << std::endl; |
| 2378 | 2378 |
/// return -1; |
| 2379 | 2379 |
/// } |
| 2380 | 2380 |
/// std::cout << "The name of the default node section: " |
| 2381 | 2381 |
/// << contents.nodeSection(0) << std::endl; |
| 2382 | 2382 |
/// std::cout << "The number of the arc maps: " |
| 2383 | 2383 |
/// << contents.arcMaps(0).size() << std::endl; |
| 2384 | 2384 |
/// std::cout << "The name of second arc map: " |
| 2385 | 2385 |
/// << contents.arcMaps(0)[1] << std::endl; |
| 2386 | 2386 |
///\endcode |
| 2387 | 2387 |
class LgfContents {
|
| 2388 | 2388 |
private: |
| 2389 | 2389 |
|
| 2390 | 2390 |
std::istream* _is; |
| 2391 | 2391 |
bool local_is; |
| 2392 | 2392 |
|
| 2393 | 2393 |
std::vector<std::string> _node_sections; |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" writer. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
| 25 | 25 |
#define LEMON_LGF_WRITER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <algorithm> |
| 32 | 32 |
|
| 33 | 33 |
#include <vector> |
| 34 | 34 |
#include <functional> |
| 35 | 35 |
|
| 36 | 36 |
#include <lemon/core.h> |
| 37 | 37 |
#include <lemon/maps.h> |
| 38 | 38 |
|
| 39 | 39 |
#include <lemon/concept_check.h> |
| 40 | 40 |
#include <lemon/concepts/maps.h> |
| 41 | 41 |
|
| 42 | 42 |
namespace lemon {
|
| 43 | 43 |
|
| 44 | 44 |
namespace _writer_bits {
|
| 45 | 45 |
|
| 46 | 46 |
template <typename Value> |
| 47 | 47 |
struct DefaultConverter {
|
| 48 | 48 |
std::string operator()(const Value& value) {
|
| 49 | 49 |
std::ostringstream os; |
| 50 | 50 |
os << value; |
| 51 | 51 |
return os.str(); |
| 52 | 52 |
} |
| 53 | 53 |
}; |
| 54 | 54 |
|
| 55 | 55 |
template <typename T> |
| 56 | 56 |
bool operator<(const T&, const T&) {
|
| 57 | 57 |
throw FormatError("Label map is not comparable");
|
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
template <typename _Map> |
| 61 | 61 |
class MapLess {
|
| 62 | 62 |
public: |
| 63 | 63 |
typedef _Map Map; |
| 64 | 64 |
typedef typename Map::Key Item; |
| 65 | 65 |
|
| 66 | 66 |
private: |
| 67 | 67 |
const Map& _map; |
| 68 | 68 |
|
| 69 | 69 |
public: |
| 70 | 70 |
MapLess(const Map& map) : _map(map) {}
|
| 71 | 71 |
|
| 72 | 72 |
bool operator()(const Item& left, const Item& right) {
|
| 73 | 73 |
return _map[left] < _map[right]; |
| 74 | 74 |
} |
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
template <typename _Graph, bool _dir, typename _Map> |
| 78 | 78 |
class GraphArcMapLess {
|
| 79 | 79 |
public: |
| 80 | 80 |
typedef _Map Map; |
| 81 | 81 |
typedef _Graph Graph; |
| 82 | 82 |
typedef typename Graph::Edge Item; |
| 83 | 83 |
|
| 84 | 84 |
private: |
| 85 | 85 |
const Graph& _graph; |
| 86 | 86 |
const Map& _map; |
| 87 | 87 |
|
| 88 | 88 |
public: |
| 89 | 89 |
GraphArcMapLess(const Graph& graph, const Map& map) |
| 90 | 90 |
: _graph(graph), _map(map) {}
|
| 91 | 91 |
|
| 92 | 92 |
bool operator()(const Item& left, const Item& right) {
|
| 93 | 93 |
return _map[_graph.direct(left, _dir)] < |
| 94 | 94 |
_map[_graph.direct(right, _dir)]; |
| 95 | 95 |
} |
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
template <typename _Item> |
| 99 | 99 |
class MapStorageBase {
|
| 100 | 100 |
public: |
| 101 | 101 |
typedef _Item Item; |
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
MapStorageBase() {}
|
| 105 | 105 |
virtual ~MapStorageBase() {}
|
| 106 | 106 |
|
| 107 | 107 |
virtual std::string get(const Item& item) = 0; |
| 108 | 108 |
virtual void sort(std::vector<Item>&) = 0; |
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
template <typename _Item, typename _Map, |
| 112 | 112 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 113 | 113 |
class MapStorage : public MapStorageBase<_Item> {
|
| 114 | 114 |
public: |
| 115 | 115 |
typedef _Map Map; |
| 116 | 116 |
typedef _Converter Converter; |
| 117 | 117 |
typedef _Item Item; |
| 118 | 118 |
|
| 119 | 119 |
private: |
| 120 | 120 |
const Map& _map; |
| 121 | 121 |
Converter _converter; |
| 122 | 122 |
|
| 123 | 123 |
public: |
| 124 | 124 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
| 125 | 125 |
: _map(map), _converter(converter) {}
|
| 126 | 126 |
virtual ~MapStorage() {}
|
| 127 | 127 |
|
| 128 | 128 |
virtual std::string get(const Item& item) {
|
| 129 | 129 |
return _converter(_map[item]); |
| 130 | 130 |
} |
| 131 | 131 |
virtual void sort(std::vector<Item>& items) {
|
| 132 | 132 |
MapLess<Map> less(_map); |
| 133 | 133 |
std::sort(items.begin(), items.end(), less); |
| 134 | 134 |
} |
| 135 | 135 |
}; |
| 136 | 136 |
|
| 137 | 137 |
template <typename _Graph, bool _dir, typename _Map, |
| 138 | 138 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 139 | 139 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 140 | 140 |
public: |
| 141 | 141 |
typedef _Map Map; |
| 142 | 142 |
typedef _Converter Converter; |
| 143 | 143 |
typedef _Graph Graph; |
| 144 | 144 |
typedef typename Graph::Edge Item; |
| 145 | 145 |
static const bool dir = _dir; |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
const Graph& _graph; |
| 149 | 149 |
const Map& _map; |
| 150 | 150 |
Converter _converter; |
| 151 | 151 |
|
| 152 | 152 |
public: |
| 153 | 153 |
GraphArcMapStorage(const Graph& graph, const Map& map, |
| 154 | 154 |
const Converter& converter = Converter()) |
| 155 | 155 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 156 | 156 |
virtual ~GraphArcMapStorage() {}
|
| 157 | 157 |
|
| 158 | 158 |
virtual std::string get(const Item& item) {
|
| 159 | 159 |
return _converter(_map[_graph.direct(item, dir)]); |
| 160 | 160 |
} |
| 161 | 161 |
virtual void sort(std::vector<Item>& items) {
|
| 162 | 162 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
| 163 | 163 |
std::sort(items.begin(), items.end(), less); |
| 164 | 164 |
} |
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
class ValueStorageBase {
|
| 168 | 168 |
public: |
| 169 | 169 |
ValueStorageBase() {}
|
| 170 | 170 |
virtual ~ValueStorageBase() {}
|
| 171 | 171 |
|
| 172 | 172 |
virtual std::string get() = 0; |
| 173 | 173 |
}; |
| 174 | 174 |
|
| 175 | 175 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 176 | 176 |
class ValueStorage : public ValueStorageBase {
|
| 177 | 177 |
public: |
| 178 | 178 |
typedef _Value Value; |
| 179 | 179 |
typedef _Converter Converter; |
| 180 | 180 |
|
| 181 | 181 |
private: |
| 182 | 182 |
const Value& _value; |
| 183 | 183 |
Converter _converter; |
| 184 | 184 |
|
| 185 | 185 |
public: |
| 186 | 186 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
| 187 | 187 |
: _value(value), _converter(converter) {}
|
| 188 | 188 |
|
| 189 | 189 |
virtual std::string get() {
|
| 190 | 190 |
return _converter(_value); |
| 191 | 191 |
} |
| 192 | 192 |
}; |
| 193 | 193 |
|
| 194 | 194 |
template <typename Value> |
| 195 | 195 |
struct MapLookUpConverter {
|
| 196 | 196 |
const std::map<Value, std::string>& _map; |
| 197 | 197 |
|
| 198 | 198 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
| 199 | 199 |
: _map(map) {}
|
| 200 | 200 |
|
| 201 | 201 |
std::string operator()(const Value& str) {
|
| 202 | 202 |
typename std::map<Value, std::string>::const_iterator it = |
| 203 | 203 |
_map.find(str); |
| 204 | 204 |
if (it == _map.end()) {
|
| 205 | 205 |
throw FormatError("Item not found");
|
| 206 | 206 |
} |
| 207 | 207 |
return it->second; |
| 208 | 208 |
} |
| 209 | 209 |
}; |
| 210 | 210 |
|
| 211 | 211 |
template <typename Graph> |
| 212 | 212 |
struct GraphArcLookUpConverter {
|
| 213 | 213 |
const Graph& _graph; |
| 214 | 214 |
const std::map<typename Graph::Edge, std::string>& _map; |
| 215 | 215 |
|
| 216 | 216 |
GraphArcLookUpConverter(const Graph& graph, |
| 217 | 217 |
const std::map<typename Graph::Edge, |
| 218 | 218 |
std::string>& map) |
| 219 | 219 |
: _graph(graph), _map(map) {}
|
| 220 | 220 |
|
| 221 | 221 |
std::string operator()(const typename Graph::Arc& val) {
|
| 222 | 222 |
typename std::map<typename Graph::Edge, std::string> |
| 223 | 223 |
::const_iterator it = _map.find(val); |
| 224 | 224 |
if (it == _map.end()) {
|
| 225 | 225 |
throw FormatError("Item not found");
|
| 226 | 226 |
} |
| 227 | 227 |
return (_graph.direction(val) ? '+' : '-') + it->second; |
| 228 | 228 |
} |
| 229 | 229 |
}; |
| 230 | 230 |
|
| 231 | 231 |
inline bool isWhiteSpace(char c) {
|
| 232 | 232 |
return c == ' ' || c == '\t' || c == '\v' || |
| 233 | 233 |
c == '\n' || c == '\r' || c == '\f'; |
| 234 | 234 |
} |
| 235 | 235 |
|
| 236 | 236 |
inline bool isEscaped(char c) {
|
| 237 | 237 |
return c == '\\' || c == '\"' || c == '\'' || |
| 238 | 238 |
c == '\a' || c == '\b'; |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
inline static void writeEscape(std::ostream& os, char c) {
|
| 242 | 242 |
switch (c) {
|
| 243 | 243 |
case '\\': |
| 244 | 244 |
os << "\\\\"; |
| 245 | 245 |
return; |
| 246 | 246 |
case '\"': |
| 247 | 247 |
os << "\\\""; |
| 248 | 248 |
return; |
| 249 | 249 |
case '\a': |
| 250 | 250 |
os << "\\a"; |
| 251 | 251 |
return; |
| 252 | 252 |
case '\b': |
| 253 | 253 |
os << "\\b"; |
| 254 | 254 |
return; |
| 255 | 255 |
case '\f': |
| 256 | 256 |
os << "\\f"; |
| 257 | 257 |
return; |
| 258 | 258 |
case '\r': |
| 259 | 259 |
os << "\\r"; |
| 260 | 260 |
return; |
| 261 | 261 |
case '\n': |
| 262 | 262 |
os << "\\n"; |
| 263 | 263 |
return; |
| 264 | 264 |
case '\t': |
| 265 | 265 |
os << "\\t"; |
| 266 | 266 |
return; |
| 267 | 267 |
case '\v': |
| 268 | 268 |
os << "\\v"; |
| 269 | 269 |
return; |
| 270 | 270 |
default: |
| 271 | 271 |
if (c < 0x20) {
|
| 272 | 272 |
std::ios::fmtflags flags = os.flags(); |
| 273 | 273 |
os << '\\' << std::oct << static_cast<int>(c); |
| 274 | 274 |
os.flags(flags); |
| 275 | 275 |
} else {
|
| 276 | 276 |
os << c; |
| 277 | 277 |
} |
| 278 | 278 |
return; |
| 279 | 279 |
} |
| 280 | 280 |
} |
| 281 | 281 |
|
| 282 | 282 |
inline bool requireEscape(const std::string& str) {
|
| 283 | 283 |
if (str.empty() || str[0] == '@') return true; |
| 284 | 284 |
std::istringstream is(str); |
| 285 | 285 |
char c; |
| 286 | 286 |
while (is.get(c)) {
|
| 287 | 287 |
if (isWhiteSpace(c) || isEscaped(c)) {
|
| 288 | 288 |
return true; |
| 289 | 289 |
} |
| 290 | 290 |
} |
| 291 | 291 |
return false; |
| 292 | 292 |
} |
| 293 | 293 |
|
| 294 | 294 |
inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
|
| 295 | 295 |
|
| 296 | 296 |
if (requireEscape(str)) {
|
| 297 | 297 |
os << '\"'; |
| 298 | 298 |
for (std::string::const_iterator it = str.begin(); |
| 299 | 299 |
it != str.end(); ++it) {
|
| 300 | 300 |
writeEscape(os, *it); |
| 301 | 301 |
} |
| 302 | 302 |
os << '\"'; |
| 303 | 303 |
} else {
|
| 304 | 304 |
os << str; |
| 305 | 305 |
} |
| 306 | 306 |
return os; |
| 307 | 307 |
} |
| 308 | 308 |
|
| 309 | 309 |
class Section {
|
| 310 | 310 |
public: |
| 311 | 311 |
virtual ~Section() {}
|
| 312 | 312 |
virtual void process(std::ostream& os) = 0; |
| 313 | 313 |
}; |
| 314 | 314 |
|
| 315 | 315 |
template <typename Functor> |
| 316 | 316 |
class LineSection : public Section {
|
| 317 | 317 |
private: |
| 318 | 318 |
|
| 319 | 319 |
Functor _functor; |
| 320 | 320 |
|
| 321 | 321 |
public: |
| 322 | 322 |
|
| 323 | 323 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 324 | 324 |
virtual ~LineSection() {}
|
| 325 | 325 |
|
| 326 | 326 |
virtual void process(std::ostream& os) {
|
| 327 | 327 |
std::string line; |
| 328 | 328 |
while (!(line = _functor()).empty()) os << line << std::endl; |
| 329 | 329 |
} |
| 330 | 330 |
}; |
| 331 | 331 |
|
| 332 | 332 |
template <typename Functor> |
| 333 | 333 |
class StreamSection : public Section {
|
| 334 | 334 |
private: |
| 335 | 335 |
|
| 336 | 336 |
Functor _functor; |
| 337 | 337 |
|
| 338 | 338 |
public: |
| 339 | 339 |
|
| 340 | 340 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 341 | 341 |
virtual ~StreamSection() {}
|
| 342 | 342 |
|
| 343 | 343 |
virtual void process(std::ostream& os) {
|
| 344 | 344 |
_functor(os); |
| 345 | 345 |
} |
| 346 | 346 |
}; |
| 347 | 347 |
|
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
template <typename Digraph> |
| 351 | 351 |
class DigraphWriter; |
| 352 | 352 |
|
| 353 | 353 |
template <typename Digraph> |
| 354 | 354 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 355 | 355 |
std::ostream& os = std::cout); |
| 356 | 356 |
template <typename Digraph> |
| 357 | 357 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 358 | 358 |
const std::string& fn); |
| 359 | 359 |
|
| 360 | 360 |
template <typename Digraph> |
| 361 | 361 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 362 | 362 |
const char* fn); |
| 363 | 363 |
|
| 364 | 364 |
|
| 365 | 365 |
/// \ingroup lemon_io |
| 366 | 366 |
/// |
| 367 | 367 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 368 | 368 |
/// |
| 369 | 369 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 370 | 370 |
/// |
| 371 | 371 |
/// The writing method does a batch processing. The user creates a |
| 372 | 372 |
/// writer object, then various writing rules can be added to the |
| 373 | 373 |
/// writer, and eventually the writing is executed with the \c run() |
| 374 | 374 |
/// member function. A map writing rule can be added to the writer |
| 375 | 375 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 376 | 376 |
/// converter parameter can also be added as a standard functor |
| 377 | 377 |
/// converting from the value type of the map to \c std::string. If it |
| 378 | 378 |
/// is set, it will determine how the value type of the map is written to |
| 379 | 379 |
/// the output stream. If the functor is not set, then a default |
| 380 | 380 |
/// conversion will be used. The \c attribute(), \c node() and \c |
| 381 | 381 |
/// arc() functions are used to add attribute writing rules. |
| 382 | 382 |
/// |
| 383 | 383 |
///\code |
| 384 | 384 |
/// DigraphWriter<Digraph>(digraph, std::cout). |
| 385 | 385 |
/// nodeMap("coordinates", coord_map).
|
| 386 | 386 |
/// nodeMap("size", size).
|
| 387 | 387 |
/// nodeMap("title", title).
|
| 388 | 388 |
/// arcMap("capacity", cap_map).
|
| 389 | 389 |
/// node("source", src).
|
| 390 | 390 |
/// node("target", trg).
|
| 391 | 391 |
/// attribute("caption", caption).
|
| 392 | 392 |
/// run(); |
| 393 | 393 |
///\endcode |
| 394 | 394 |
/// |
| 395 | 395 |
/// |
| 396 | 396 |
/// By default, the writer does not write additional captions to the |
| 397 | 397 |
/// sections, but they can be give as an optional parameter of |
| 398 | 398 |
/// the \c nodes(), \c arcs() or \c |
| 399 | 399 |
/// attributes() functions. |
| 400 | 400 |
/// |
| 401 | 401 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
| 402 | 402 |
/// writing of the sections. If two arc sections should be written |
| 403 | 403 |
/// to the output, it can be done in two passes, the first pass |
| 404 | 404 |
/// writes the node section and the first arc section, then the |
| 405 | 405 |
/// second pass skips the node section and writes just the arc |
| 406 | 406 |
/// section to the stream. The output stream can be retrieved with |
| 407 | 407 |
/// the \c ostream() function, hence the second pass can append its |
| 408 | 408 |
/// output to the output of the first pass. |
| 409 | 409 |
template <typename _Digraph> |
| 410 | 410 |
class DigraphWriter {
|
| 411 | 411 |
public: |
| 412 | 412 |
|
| 413 | 413 |
typedef _Digraph Digraph; |
| 414 | 414 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 415 | 415 |
|
| 416 | 416 |
private: |
| 417 | 417 |
|
| 418 | 418 |
|
| 419 | 419 |
std::ostream* _os; |
| 420 | 420 |
bool local_os; |
| 421 | 421 |
|
| 422 | 422 |
const Digraph& _digraph; |
| 423 | 423 |
|
| 424 | 424 |
std::string _nodes_caption; |
| 425 | 425 |
std::string _arcs_caption; |
| 426 | 426 |
std::string _attributes_caption; |
| 427 | 427 |
|
| 428 | 428 |
typedef std::map<Node, std::string> NodeIndex; |
| 429 | 429 |
NodeIndex _node_index; |
| 430 | 430 |
typedef std::map<Arc, std::string> ArcIndex; |
| 431 | 431 |
ArcIndex _arc_index; |
| 432 | 432 |
|
| 433 | 433 |
typedef std::vector<std::pair<std::string, |
| 434 | 434 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 435 | 435 |
NodeMaps _node_maps; |
| 436 | 436 |
|
| 437 | 437 |
typedef std::vector<std::pair<std::string, |
| 438 | 438 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
| 439 | 439 |
ArcMaps _arc_maps; |
| 440 | 440 |
|
| 441 | 441 |
typedef std::vector<std::pair<std::string, |
| 442 | 442 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 443 | 443 |
Attributes _attributes; |
| 444 | 444 |
|
| 445 | 445 |
bool _skip_nodes; |
| 446 | 446 |
bool _skip_arcs; |
| 447 | 447 |
|
| 448 | 448 |
public: |
| 449 | 449 |
|
| 450 | 450 |
/// \brief Constructor |
| 451 | 451 |
/// |
| 452 | 452 |
/// Construct a directed graph writer, which writes to the given |
| 453 | 453 |
/// output stream. |
| 454 | 454 |
DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) |
| 455 | 455 |
: _os(&os), local_os(false), _digraph(digraph), |
| 456 | 456 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 457 | 457 |
|
| 458 | 458 |
/// \brief Constructor |
| 459 | 459 |
/// |
| 460 | 460 |
/// Construct a directed graph writer, which writes to the given |
| 461 | 461 |
/// output file. |
| 462 | 462 |
DigraphWriter(const Digraph& digraph, const std::string& fn) |
| 463 | 463 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), |
| 464 | 464 |
_skip_nodes(false), _skip_arcs(false) {
|
| 465 | 465 |
if (!(*_os)) {
|
| 466 | 466 |
delete _os; |
| 467 | 467 |
throw IoError("Cannot write file", fn);
|
| 468 | 468 |
} |
| 469 | 469 |
} |
| 470 | 470 |
|
| 471 | 471 |
/// \brief Constructor |
| 472 | 472 |
/// |
| 473 | 473 |
/// Construct a directed graph writer, which writes to the given |
| 474 | 474 |
/// output file. |
| 475 | 475 |
DigraphWriter(const Digraph& digraph, const char* fn) |
| 476 | 476 |
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), |
| 477 | 477 |
_skip_nodes(false), _skip_arcs(false) {
|
| 478 | 478 |
if (!(*_os)) {
|
| 479 | 479 |
delete _os; |
| 480 | 480 |
throw IoError("Cannot write file", fn);
|
| 481 | 481 |
} |
| 482 | 482 |
} |
| 483 | 483 |
|
| 484 | 484 |
/// \brief Destructor |
| 485 | 485 |
~DigraphWriter() {
|
| 486 | 486 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 487 | 487 |
it != _node_maps.end(); ++it) {
|
| 488 | 488 |
delete it->second; |
| 489 | 489 |
} |
| 490 | 490 |
|
| 491 | 491 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 492 | 492 |
it != _arc_maps.end(); ++it) {
|
| 493 | 493 |
delete it->second; |
| 494 | 494 |
} |
| 495 | 495 |
|
| 496 | 496 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 497 | 497 |
it != _attributes.end(); ++it) {
|
| 498 | 498 |
delete it->second; |
| 499 | 499 |
} |
| 500 | 500 |
|
| 501 | 501 |
if (local_os) {
|
| 502 | 502 |
delete _os; |
| 503 | 503 |
} |
| 504 | 504 |
} |
| 505 | 505 |
|
| 506 | 506 |
private: |
| 507 | 507 |
|
| 508 | 508 |
template <typename DGR> |
| 509 |
friend DigraphWriter<DGR> digraphWriter(const DGR& digraph, |
|
| 509 |
friend DigraphWriter<DGR> digraphWriter(const DGR& digraph, |
|
| 510 | 510 |
std::ostream& os); |
| 511 | 511 |
template <typename DGR> |
| 512 | 512 |
friend DigraphWriter<DGR> digraphWriter(const DGR& digraph, |
| 513 | 513 |
const std::string& fn); |
| 514 | 514 |
template <typename DGR> |
| 515 | 515 |
friend DigraphWriter<DGR> digraphWriter(const DGR& digraph, |
| 516 | 516 |
const char *fn); |
| 517 | 517 |
|
| 518 | 518 |
DigraphWriter(DigraphWriter& other) |
| 519 | 519 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
| 520 | 520 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 521 | 521 |
|
| 522 | 522 |
other._os = 0; |
| 523 | 523 |
other.local_os = false; |
| 524 | 524 |
|
| 525 | 525 |
_node_index.swap(other._node_index); |
| 526 | 526 |
_arc_index.swap(other._arc_index); |
| 527 | 527 |
|
| 528 | 528 |
_node_maps.swap(other._node_maps); |
| 529 | 529 |
_arc_maps.swap(other._arc_maps); |
| 530 | 530 |
_attributes.swap(other._attributes); |
| 531 | 531 |
|
| 532 | 532 |
_nodes_caption = other._nodes_caption; |
| 533 | 533 |
_arcs_caption = other._arcs_caption; |
| 534 | 534 |
_attributes_caption = other._attributes_caption; |
| 535 | 535 |
} |
| 536 | 536 |
|
| 537 | 537 |
DigraphWriter& operator=(const DigraphWriter&); |
| 538 | 538 |
|
| 539 | 539 |
public: |
| 540 | 540 |
|
| 541 | 541 |
/// \name Writing rules |
| 542 | 542 |
/// @{
|
| 543 | 543 |
|
| 544 | 544 |
/// \brief Node map writing rule |
| 545 | 545 |
/// |
| 546 | 546 |
/// Add a node map writing rule to the writer. |
| 547 | 547 |
template <typename Map> |
| 548 | 548 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 549 | 549 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 550 | 550 |
_writer_bits::MapStorageBase<Node>* storage = |
| 551 | 551 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 552 | 552 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 553 | 553 |
return *this; |
| 554 | 554 |
} |
| 555 | 555 |
|
| 556 | 556 |
/// \brief Node map writing rule |
| 557 | 557 |
/// |
| 558 | 558 |
/// Add a node map writing rule with specialized converter to the |
| 559 | 559 |
/// writer. |
| 560 | 560 |
template <typename Map, typename Converter> |
| 561 | 561 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 562 | 562 |
const Converter& converter = Converter()) {
|
| 563 | 563 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 564 | 564 |
_writer_bits::MapStorageBase<Node>* storage = |
| 565 | 565 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 566 | 566 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 567 | 567 |
return *this; |
| 568 | 568 |
} |
| 569 | 569 |
|
| 570 | 570 |
/// \brief Arc map writing rule |
| 571 | 571 |
/// |
| 572 | 572 |
/// Add an arc map writing rule to the writer. |
| 573 | 573 |
template <typename Map> |
| 574 | 574 |
DigraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 575 | 575 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 576 | 576 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 577 | 577 |
new _writer_bits::MapStorage<Arc, Map>(map); |
| 578 | 578 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 579 | 579 |
return *this; |
| 580 | 580 |
} |
| 581 | 581 |
|
| 582 | 582 |
/// \brief Arc map writing rule |
| 583 | 583 |
/// |
| 584 | 584 |
/// Add an arc map writing rule with specialized converter to the |
| 585 | 585 |
/// writer. |
| 586 | 586 |
template <typename Map, typename Converter> |
| 587 | 587 |
DigraphWriter& arcMap(const std::string& caption, const Map& map, |
| 588 | 588 |
const Converter& converter = Converter()) {
|
| 589 | 589 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 590 | 590 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 591 | 591 |
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 592 | 592 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 593 | 593 |
return *this; |
| 594 | 594 |
} |
| 595 | 595 |
|
| 596 | 596 |
/// \brief Attribute writing rule |
| 597 | 597 |
/// |
| 598 | 598 |
/// Add an attribute writing rule to the writer. |
| 599 | 599 |
template <typename Value> |
| 600 | 600 |
DigraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 601 | 601 |
_writer_bits::ValueStorageBase* storage = |
| 602 | 602 |
new _writer_bits::ValueStorage<Value>(value); |
| 603 | 603 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 604 | 604 |
return *this; |
| 605 | 605 |
} |
| 606 | 606 |
|
| 607 | 607 |
/// \brief Attribute writing rule |
| 608 | 608 |
/// |
| 609 | 609 |
/// Add an attribute writing rule with specialized converter to the |
| 610 | 610 |
/// writer. |
| 611 | 611 |
template <typename Value, typename Converter> |
| 612 | 612 |
DigraphWriter& attribute(const std::string& caption, const Value& value, |
| 613 | 613 |
const Converter& converter = Converter()) {
|
| 614 | 614 |
_writer_bits::ValueStorageBase* storage = |
| 615 | 615 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 616 | 616 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 617 | 617 |
return *this; |
| 618 | 618 |
} |
| 619 | 619 |
|
| 620 | 620 |
/// \brief Node writing rule |
| 621 | 621 |
/// |
| 622 | 622 |
/// Add a node writing rule to the writer. |
| 623 | 623 |
DigraphWriter& node(const std::string& caption, const Node& node) {
|
| 624 | 624 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 625 | 625 |
Converter converter(_node_index); |
| 626 | 626 |
_writer_bits::ValueStorageBase* storage = |
| 627 | 627 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 628 | 628 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 629 | 629 |
return *this; |
| 630 | 630 |
} |
| 631 | 631 |
|
| 632 | 632 |
/// \brief Arc writing rule |
| 633 | 633 |
/// |
| 634 | 634 |
/// Add an arc writing rule to writer. |
| 635 | 635 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 636 | 636 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
| 637 | 637 |
Converter converter(_arc_index); |
| 638 | 638 |
_writer_bits::ValueStorageBase* storage = |
| 639 | 639 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 640 | 640 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 641 | 641 |
return *this; |
| 642 | 642 |
} |
| 643 | 643 |
|
| 644 | 644 |
/// \name Section captions |
| 645 | 645 |
/// @{
|
| 646 | 646 |
|
| 647 | 647 |
/// \brief Add an additional caption to the \c \@nodes section |
| 648 | 648 |
/// |
| 649 | 649 |
/// Add an additional caption to the \c \@nodes section. |
| 650 | 650 |
DigraphWriter& nodes(const std::string& caption) {
|
| 651 | 651 |
_nodes_caption = caption; |
| 652 | 652 |
return *this; |
| 653 | 653 |
} |
| 654 | 654 |
|
| 655 | 655 |
/// \brief Add an additional caption to the \c \@arcs section |
| 656 | 656 |
/// |
| 657 | 657 |
/// Add an additional caption to the \c \@arcs section. |
| 658 | 658 |
DigraphWriter& arcs(const std::string& caption) {
|
| 659 | 659 |
_arcs_caption = caption; |
| 660 | 660 |
return *this; |
| 661 | 661 |
} |
| 662 | 662 |
|
| 663 | 663 |
/// \brief Add an additional caption to the \c \@attributes section |
| 664 | 664 |
/// |
| 665 | 665 |
/// Add an additional caption to the \c \@attributes section. |
| 666 | 666 |
DigraphWriter& attributes(const std::string& caption) {
|
| 667 | 667 |
_attributes_caption = caption; |
| 668 | 668 |
return *this; |
| 669 | 669 |
} |
| 670 | 670 |
|
| 671 | 671 |
/// \name Skipping section |
| 672 | 672 |
/// @{
|
| 673 | 673 |
|
| 674 | 674 |
/// \brief Skip writing the node set |
| 675 | 675 |
/// |
| 676 | 676 |
/// The \c \@nodes section will not be written to the stream. |
| 677 | 677 |
DigraphWriter& skipNodes() {
|
| 678 | 678 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
| 679 | 679 |
_skip_nodes = true; |
| 680 | 680 |
return *this; |
| 681 | 681 |
} |
| 682 | 682 |
|
| 683 | 683 |
/// \brief Skip writing arc set |
| 684 | 684 |
/// |
| 685 | 685 |
/// The \c \@arcs section will not be written to the stream. |
| 686 | 686 |
DigraphWriter& skipArcs() {
|
| 687 | 687 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
| 688 | 688 |
_skip_arcs = true; |
| 689 | 689 |
return *this; |
| 690 | 690 |
} |
| 691 | 691 |
|
| 692 | 692 |
/// @} |
| 693 | 693 |
|
| 694 | 694 |
private: |
| 695 | 695 |
|
| 696 | 696 |
void writeNodes() {
|
| 697 | 697 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 698 | 698 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 699 | 699 |
it != _node_maps.end(); ++it) {
|
| 700 | 700 |
if (it->first == "label") {
|
| 701 | 701 |
label = it->second; |
| 702 | 702 |
break; |
| 703 | 703 |
} |
| 704 | 704 |
} |
| 705 | 705 |
|
| 706 | 706 |
*_os << "@nodes"; |
| 707 | 707 |
if (!_nodes_caption.empty()) {
|
| 708 | 708 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
| 709 | 709 |
} |
| 710 | 710 |
*_os << std::endl; |
| 711 | 711 |
|
| 712 | 712 |
if (label == 0) {
|
| 713 | 713 |
*_os << "label" << '\t'; |
| 714 | 714 |
} |
| 715 | 715 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 716 | 716 |
it != _node_maps.end(); ++it) {
|
| 717 | 717 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 718 | 718 |
} |
| 719 | 719 |
*_os << std::endl; |
| 720 | 720 |
|
| 721 | 721 |
std::vector<Node> nodes; |
| 722 | 722 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 723 | 723 |
nodes.push_back(n); |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
if (label == 0) {
|
| 727 | 727 |
IdMap<Digraph, Node> id_map(_digraph); |
| 728 | 728 |
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map); |
| 729 | 729 |
std::sort(nodes.begin(), nodes.end(), id_less); |
| 730 | 730 |
} else {
|
| 731 | 731 |
label->sort(nodes); |
| 732 | 732 |
} |
| 733 | 733 |
|
| 734 | 734 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 735 | 735 |
Node n = nodes[i]; |
| 736 | 736 |
if (label == 0) {
|
| 737 | 737 |
std::ostringstream os; |
| 738 | 738 |
os << _digraph.id(n); |
| 739 | 739 |
_writer_bits::writeToken(*_os, os.str()); |
| 740 | 740 |
*_os << '\t'; |
| 741 | 741 |
_node_index.insert(std::make_pair(n, os.str())); |
| 742 | 742 |
} |
| 743 | 743 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 744 | 744 |
it != _node_maps.end(); ++it) {
|
| 745 | 745 |
std::string value = it->second->get(n); |
| 746 | 746 |
_writer_bits::writeToken(*_os, value); |
| 747 | 747 |
if (it->first == "label") {
|
| 748 | 748 |
_node_index.insert(std::make_pair(n, value)); |
| 749 | 749 |
} |
| 750 | 750 |
*_os << '\t'; |
| 751 | 751 |
} |
| 752 | 752 |
*_os << std::endl; |
| 753 | 753 |
} |
| 754 | 754 |
} |
| 755 | 755 |
|
| 756 | 756 |
void createNodeIndex() {
|
| 757 | 757 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 758 | 758 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 759 | 759 |
it != _node_maps.end(); ++it) {
|
| 760 | 760 |
if (it->first == "label") {
|
| 761 | 761 |
label = it->second; |
| 762 | 762 |
break; |
| 763 | 763 |
} |
| 764 | 764 |
} |
| 765 | 765 |
|
| 766 | 766 |
if (label == 0) {
|
| 767 | 767 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 768 | 768 |
std::ostringstream os; |
| 769 | 769 |
os << _digraph.id(n); |
| 770 | 770 |
_node_index.insert(std::make_pair(n, os.str())); |
| 771 | 771 |
} |
| 772 | 772 |
} else {
|
| 773 | 773 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 774 | 774 |
std::string value = label->get(n); |
| 775 | 775 |
_node_index.insert(std::make_pair(n, value)); |
| 776 | 776 |
} |
| 777 | 777 |
} |
| 778 | 778 |
} |
| 779 | 779 |
|
| 780 | 780 |
void writeArcs() {
|
| 781 | 781 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 782 | 782 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 783 | 783 |
it != _arc_maps.end(); ++it) {
|
| 784 | 784 |
if (it->first == "label") {
|
| 785 | 785 |
label = it->second; |
| 786 | 786 |
break; |
| 787 | 787 |
} |
| 788 | 788 |
} |
| 789 | 789 |
|
| 790 | 790 |
*_os << "@arcs"; |
| 791 | 791 |
if (!_arcs_caption.empty()) {
|
| 792 | 792 |
_writer_bits::writeToken(*_os << ' ', _arcs_caption); |
| 793 | 793 |
} |
| 794 | 794 |
*_os << std::endl; |
| 795 | 795 |
|
| 796 | 796 |
*_os << '\t' << '\t'; |
| 797 | 797 |
if (label == 0) {
|
| 798 | 798 |
*_os << "label" << '\t'; |
| 799 | 799 |
} |
| 800 | 800 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 801 | 801 |
it != _arc_maps.end(); ++it) {
|
| 802 | 802 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 803 | 803 |
} |
| 804 | 804 |
*_os << std::endl; |
| 805 | 805 |
|
| 806 | 806 |
std::vector<Arc> arcs; |
| 807 | 807 |
for (ArcIt n(_digraph); n != INVALID; ++n) {
|
| 808 | 808 |
arcs.push_back(n); |
| 809 | 809 |
} |
| 810 | 810 |
|
| 811 | 811 |
if (label == 0) {
|
| 812 | 812 |
IdMap<Digraph, Arc> id_map(_digraph); |
| 813 | 813 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map); |
| 814 | 814 |
std::sort(arcs.begin(), arcs.end(), id_less); |
| 815 | 815 |
} else {
|
| 816 | 816 |
label->sort(arcs); |
| 817 | 817 |
} |
| 818 | 818 |
|
| 819 | 819 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
|
| 820 | 820 |
Arc a = arcs[i]; |
| 821 | 821 |
_writer_bits::writeToken(*_os, _node_index. |
| 822 | 822 |
find(_digraph.source(a))->second); |
| 823 | 823 |
*_os << '\t'; |
| 824 | 824 |
_writer_bits::writeToken(*_os, _node_index. |
| 825 | 825 |
find(_digraph.target(a))->second); |
| 826 | 826 |
*_os << '\t'; |
| 827 | 827 |
if (label == 0) {
|
| 828 | 828 |
std::ostringstream os; |
| 829 | 829 |
os << _digraph.id(a); |
| 830 | 830 |
_writer_bits::writeToken(*_os, os.str()); |
| 831 | 831 |
*_os << '\t'; |
| 832 | 832 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 833 | 833 |
} |
| 834 | 834 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 835 | 835 |
it != _arc_maps.end(); ++it) {
|
| 836 | 836 |
std::string value = it->second->get(a); |
| 837 | 837 |
_writer_bits::writeToken(*_os, value); |
| 838 | 838 |
if (it->first == "label") {
|
| 839 | 839 |
_arc_index.insert(std::make_pair(a, value)); |
| 840 | 840 |
} |
| 841 | 841 |
*_os << '\t'; |
| 842 | 842 |
} |
| 843 | 843 |
*_os << std::endl; |
| 844 | 844 |
} |
| 845 | 845 |
} |
| 846 | 846 |
|
| 847 | 847 |
void createArcIndex() {
|
| 848 | 848 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 849 | 849 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 850 | 850 |
it != _arc_maps.end(); ++it) {
|
| 851 | 851 |
if (it->first == "label") {
|
| 852 | 852 |
label = it->second; |
| 853 | 853 |
break; |
| 854 | 854 |
} |
| 855 | 855 |
} |
| 856 | 856 |
|
| 857 | 857 |
if (label == 0) {
|
| 858 | 858 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 859 | 859 |
std::ostringstream os; |
| 860 | 860 |
os << _digraph.id(a); |
| 861 | 861 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 862 | 862 |
} |
| 863 | 863 |
} else {
|
| 864 | 864 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 865 | 865 |
std::string value = label->get(a); |
| 866 | 866 |
_arc_index.insert(std::make_pair(a, value)); |
| 867 | 867 |
} |
| 868 | 868 |
} |
| 869 | 869 |
} |
| 870 | 870 |
|
| 871 | 871 |
void writeAttributes() {
|
| 872 | 872 |
if (_attributes.empty()) return; |
| 873 | 873 |
*_os << "@attributes"; |
| 874 | 874 |
if (!_attributes_caption.empty()) {
|
| 875 | 875 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 876 | 876 |
} |
| 877 | 877 |
*_os << std::endl; |
| 878 | 878 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 879 | 879 |
it != _attributes.end(); ++it) {
|
| 880 | 880 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 881 | 881 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 882 | 882 |
*_os << std::endl; |
| 883 | 883 |
} |
| 884 | 884 |
} |
| 885 | 885 |
|
| 886 | 886 |
public: |
| 887 | 887 |
|
| 888 | 888 |
/// \name Execution of the writer |
| 889 | 889 |
/// @{
|
| 890 | 890 |
|
| 891 | 891 |
/// \brief Start the batch processing |
| 892 | 892 |
/// |
| 893 | 893 |
/// This function starts the batch processing. |
| 894 | 894 |
void run() {
|
| 895 | 895 |
if (!_skip_nodes) {
|
| 896 | 896 |
writeNodes(); |
| 897 | 897 |
} else {
|
| 898 | 898 |
createNodeIndex(); |
| 899 | 899 |
} |
| 900 | 900 |
if (!_skip_arcs) {
|
| 901 | 901 |
writeArcs(); |
| 902 | 902 |
} else {
|
| 903 | 903 |
createArcIndex(); |
| 904 | 904 |
} |
| 905 | 905 |
writeAttributes(); |
| 906 | 906 |
} |
| 907 | 907 |
|
| 908 | 908 |
/// \brief Give back the stream of the writer |
| 909 | 909 |
/// |
| 910 | 910 |
/// Give back the stream of the writer. |
| 911 | 911 |
std::ostream& ostream() {
|
| 912 | 912 |
return *_os; |
| 913 | 913 |
} |
| 914 | 914 |
|
| 915 | 915 |
/// @} |
| 916 | 916 |
}; |
| 917 | 917 |
|
| 918 | 918 |
/// \brief Return a \ref DigraphWriter class |
| 919 | 919 |
/// |
| 920 | 920 |
/// This function just returns a \ref DigraphWriter class. |
| 921 | 921 |
/// \relates DigraphWriter |
| 922 | 922 |
template <typename Digraph> |
| 923 | 923 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 924 | 924 |
std::ostream& os) {
|
| 925 | 925 |
DigraphWriter<Digraph> tmp(digraph, os); |
| 926 | 926 |
return tmp; |
| 927 | 927 |
} |
| 928 | 928 |
|
| 929 | 929 |
/// \brief Return a \ref DigraphWriter class |
| 930 | 930 |
/// |
| 931 | 931 |
/// This function just returns a \ref DigraphWriter class. |
| 932 | 932 |
/// \relates DigraphWriter |
| 933 | 933 |
template <typename Digraph> |
| 934 | 934 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 935 | 935 |
const std::string& fn) {
|
| 936 | 936 |
DigraphWriter<Digraph> tmp(digraph, fn); |
| 937 | 937 |
return tmp; |
| 938 | 938 |
} |
| 939 | 939 |
|
| 940 | 940 |
/// \brief Return a \ref DigraphWriter class |
| 941 | 941 |
/// |
| 942 | 942 |
/// This function just returns a \ref DigraphWriter class. |
| 943 | 943 |
/// \relates DigraphWriter |
| 944 | 944 |
template <typename Digraph> |
| 945 | 945 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 946 | 946 |
const char* fn) {
|
| 947 | 947 |
DigraphWriter<Digraph> tmp(digraph, fn); |
| 948 | 948 |
return tmp; |
| 949 | 949 |
} |
| 950 | 950 |
|
| 951 | 951 |
template <typename Graph> |
| 952 | 952 |
class GraphWriter; |
| 953 | 953 |
|
| 954 | 954 |
template <typename Graph> |
| 955 | 955 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
| 956 | 956 |
std::ostream& os = std::cout); |
| 957 | 957 |
template <typename Graph> |
| 958 | 958 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn); |
| 959 | 959 |
template <typename Graph> |
| 960 | 960 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn); |
| 961 | 961 |
|
| 962 | 962 |
/// \ingroup lemon_io |
| 963 | 963 |
/// |
| 964 | 964 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 965 | 965 |
/// |
| 966 | 966 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 967 | 967 |
/// |
| 968 | 968 |
/// It can be used almost the same way as \c DigraphWriter. |
| 969 | 969 |
/// The only difference is that this class can handle edges and |
| 970 | 970 |
/// edge maps as well as arcs and arc maps. |
| 971 | 971 |
/// |
| 972 | 972 |
/// The arc maps are written into the file as two columns, the |
| 973 | 973 |
/// caption of the columns are the name of the map prefixed with \c |
| 974 | 974 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
| 975 | 975 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
| 976 | 976 |
/// of the arc) and the label of corresponding edge. |
| 977 | 977 |
template <typename _Graph> |
| 978 | 978 |
class GraphWriter {
|
| 979 | 979 |
public: |
| 980 | 980 |
|
| 981 | 981 |
typedef _Graph Graph; |
| 982 | 982 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 983 | 983 |
|
| 984 | 984 |
private: |
| 985 | 985 |
|
| 986 | 986 |
|
| 987 | 987 |
std::ostream* _os; |
| 988 | 988 |
bool local_os; |
| 989 | 989 |
|
| 990 | 990 |
const Graph& _graph; |
| 991 | 991 |
|
| 992 | 992 |
std::string _nodes_caption; |
| 993 | 993 |
std::string _edges_caption; |
| 994 | 994 |
std::string _attributes_caption; |
| 995 | 995 |
|
| 996 | 996 |
typedef std::map<Node, std::string> NodeIndex; |
| 997 | 997 |
NodeIndex _node_index; |
| 998 | 998 |
typedef std::map<Edge, std::string> EdgeIndex; |
| 999 | 999 |
EdgeIndex _edge_index; |
| 1000 | 1000 |
|
| 1001 | 1001 |
typedef std::vector<std::pair<std::string, |
| 1002 | 1002 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 1003 | 1003 |
NodeMaps _node_maps; |
| 1004 | 1004 |
|
| 1005 | 1005 |
typedef std::vector<std::pair<std::string, |
| 1006 | 1006 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
| 1007 | 1007 |
EdgeMaps _edge_maps; |
| 1008 | 1008 |
|
| 1009 | 1009 |
typedef std::vector<std::pair<std::string, |
| 1010 | 1010 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 1011 | 1011 |
Attributes _attributes; |
| 1012 | 1012 |
|
| 1013 | 1013 |
bool _skip_nodes; |
| 1014 | 1014 |
bool _skip_edges; |
| 1015 | 1015 |
|
| 1016 | 1016 |
public: |
| 1017 | 1017 |
|
| 1018 | 1018 |
/// \brief Constructor |
| 1019 | 1019 |
/// |
| 1020 | 1020 |
/// Construct a directed graph writer, which writes to the given |
| 1021 | 1021 |
/// output stream. |
| 1022 | 1022 |
GraphWriter(const Graph& graph, std::ostream& os = std::cout) |
| 1023 | 1023 |
: _os(&os), local_os(false), _graph(graph), |
| 1024 | 1024 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1025 | 1025 |
|
| 1026 | 1026 |
/// \brief Constructor |
| 1027 | 1027 |
/// |
| 1028 | 1028 |
/// Construct a directed graph writer, which writes to the given |
| 1029 | 1029 |
/// output file. |
| 1030 | 1030 |
GraphWriter(const Graph& graph, const std::string& fn) |
| 1031 | 1031 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
| 1032 | 1032 |
_skip_nodes(false), _skip_edges(false) {
|
| 1033 | 1033 |
if (!(*_os)) {
|
| 1034 | 1034 |
delete _os; |
| 1035 | 1035 |
throw IoError("Cannot write file", fn);
|
| 1036 | 1036 |
} |
| 1037 | 1037 |
} |
| 1038 | 1038 |
|
| 1039 | 1039 |
/// \brief Constructor |
| 1040 | 1040 |
/// |
| 1041 | 1041 |
/// Construct a directed graph writer, which writes to the given |
| 1042 | 1042 |
/// output file. |
| 1043 | 1043 |
GraphWriter(const Graph& graph, const char* fn) |
| 1044 | 1044 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
| 1045 | 1045 |
_skip_nodes(false), _skip_edges(false) {
|
| 1046 | 1046 |
if (!(*_os)) {
|
| 1047 | 1047 |
delete _os; |
| 1048 | 1048 |
throw IoError("Cannot write file", fn);
|
| 1049 | 1049 |
} |
| 1050 | 1050 |
} |
| 1051 | 1051 |
|
| 1052 | 1052 |
/// \brief Destructor |
| 1053 | 1053 |
~GraphWriter() {
|
| 1054 | 1054 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1055 | 1055 |
it != _node_maps.end(); ++it) {
|
| 1056 | 1056 |
delete it->second; |
| 1057 | 1057 |
} |
| 1058 | 1058 |
|
| 1059 | 1059 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1060 | 1060 |
it != _edge_maps.end(); ++it) {
|
| 1061 | 1061 |
delete it->second; |
| 1062 | 1062 |
} |
| 1063 | 1063 |
|
| 1064 | 1064 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1065 | 1065 |
it != _attributes.end(); ++it) {
|
| 1066 | 1066 |
delete it->second; |
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
if (local_os) {
|
| 1070 | 1070 |
delete _os; |
| 1071 | 1071 |
} |
| 1072 | 1072 |
} |
| 1073 | 1073 |
|
| 1074 | 1074 |
private: |
| 1075 | 1075 |
|
| 1076 | 1076 |
template <typename GR> |
| 1077 | 1077 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
| 1078 | 1078 |
std::ostream& os); |
| 1079 | 1079 |
template <typename GR> |
| 1080 | 1080 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
| 1081 | 1081 |
const std::string& fn); |
| 1082 | 1082 |
template <typename GR> |
| 1083 | 1083 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
| 1084 | 1084 |
const char *fn); |
| 1085 |
|
|
| 1085 |
|
|
| 1086 | 1086 |
GraphWriter(GraphWriter& other) |
| 1087 | 1087 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
| 1088 | 1088 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1089 | 1089 |
|
| 1090 | 1090 |
other._os = 0; |
| 1091 | 1091 |
other.local_os = false; |
| 1092 | 1092 |
|
| 1093 | 1093 |
_node_index.swap(other._node_index); |
| 1094 | 1094 |
_edge_index.swap(other._edge_index); |
| 1095 | 1095 |
|
| 1096 | 1096 |
_node_maps.swap(other._node_maps); |
| 1097 | 1097 |
_edge_maps.swap(other._edge_maps); |
| 1098 | 1098 |
_attributes.swap(other._attributes); |
| 1099 | 1099 |
|
| 1100 | 1100 |
_nodes_caption = other._nodes_caption; |
| 1101 | 1101 |
_edges_caption = other._edges_caption; |
| 1102 | 1102 |
_attributes_caption = other._attributes_caption; |
| 1103 | 1103 |
} |
| 1104 | 1104 |
|
| 1105 | 1105 |
GraphWriter& operator=(const GraphWriter&); |
| 1106 | 1106 |
|
| 1107 | 1107 |
public: |
| 1108 | 1108 |
|
| 1109 | 1109 |
/// \name Writing rules |
| 1110 | 1110 |
/// @{
|
| 1111 | 1111 |
|
| 1112 | 1112 |
/// \brief Node map writing rule |
| 1113 | 1113 |
/// |
| 1114 | 1114 |
/// Add a node map writing rule to the writer. |
| 1115 | 1115 |
template <typename Map> |
| 1116 | 1116 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 1117 | 1117 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1118 | 1118 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1119 | 1119 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 1120 | 1120 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1121 | 1121 |
return *this; |
| 1122 | 1122 |
} |
| 1123 | 1123 |
|
| 1124 | 1124 |
/// \brief Node map writing rule |
| 1125 | 1125 |
/// |
| 1126 | 1126 |
/// Add a node map writing rule with specialized converter to the |
| 1127 | 1127 |
/// writer. |
| 1128 | 1128 |
template <typename Map, typename Converter> |
| 1129 | 1129 |
GraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 1130 | 1130 |
const Converter& converter = Converter()) {
|
| 1131 | 1131 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1132 | 1132 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1133 | 1133 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1134 | 1134 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1135 | 1135 |
return *this; |
| 1136 | 1136 |
} |
| 1137 | 1137 |
|
| 1138 | 1138 |
/// \brief Edge map writing rule |
| 1139 | 1139 |
/// |
| 1140 | 1140 |
/// Add an edge map writing rule to the writer. |
| 1141 | 1141 |
template <typename Map> |
| 1142 | 1142 |
GraphWriter& edgeMap(const std::string& caption, const Map& map) {
|
| 1143 | 1143 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1144 | 1144 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1145 | 1145 |
new _writer_bits::MapStorage<Edge, Map>(map); |
| 1146 | 1146 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1147 | 1147 |
return *this; |
| 1148 | 1148 |
} |
| 1149 | 1149 |
|
| 1150 | 1150 |
/// \brief Edge map writing rule |
| 1151 | 1151 |
/// |
| 1152 | 1152 |
/// Add an edge map writing rule with specialized converter to the |
| 1153 | 1153 |
/// writer. |
| 1154 | 1154 |
template <typename Map, typename Converter> |
| 1155 | 1155 |
GraphWriter& edgeMap(const std::string& caption, const Map& map, |
| 1156 | 1156 |
const Converter& converter = Converter()) {
|
| 1157 | 1157 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1158 | 1158 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1159 | 1159 |
new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1160 | 1160 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1161 | 1161 |
return *this; |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
/// \brief Arc map writing rule |
| 1165 | 1165 |
/// |
| 1166 | 1166 |
/// Add an arc map writing rule to the writer. |
| 1167 | 1167 |
template <typename Map> |
| 1168 | 1168 |
GraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 1169 | 1169 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1170 | 1170 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1171 | 1171 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1172 | 1172 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1173 | 1173 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1174 | 1174 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1175 | 1175 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1176 | 1176 |
return *this; |
| 1177 | 1177 |
} |
| 1178 | 1178 |
|
| 1179 | 1179 |
/// \brief Arc map writing rule |
| 1180 | 1180 |
/// |
| 1181 | 1181 |
/// Add an arc map writing rule with specialized converter to the |
| 1182 | 1182 |
/// writer. |
| 1183 | 1183 |
template <typename Map, typename Converter> |
| 1184 | 1184 |
GraphWriter& arcMap(const std::string& caption, const Map& map, |
| 1185 | 1185 |
const Converter& converter = Converter()) {
|
| 1186 | 1186 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1187 | 1187 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1188 | 1188 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1189 | 1189 |
(_graph, map, converter); |
| 1190 | 1190 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1191 | 1191 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1192 | 1192 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1193 | 1193 |
(_graph, map, converter); |
| 1194 | 1194 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1195 | 1195 |
return *this; |
| 1196 | 1196 |
} |
| 1197 | 1197 |
|
| 1198 | 1198 |
/// \brief Attribute writing rule |
| 1199 | 1199 |
/// |
| 1200 | 1200 |
/// Add an attribute writing rule to the writer. |
| 1201 | 1201 |
template <typename Value> |
| 1202 | 1202 |
GraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 1203 | 1203 |
_writer_bits::ValueStorageBase* storage = |
| 1204 | 1204 |
new _writer_bits::ValueStorage<Value>(value); |
| 1205 | 1205 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1206 | 1206 |
return *this; |
| 1207 | 1207 |
} |
| 1208 | 1208 |
|
| 1209 | 1209 |
/// \brief Attribute writing rule |
| 1210 | 1210 |
/// |
| 1211 | 1211 |
/// Add an attribute writing rule with specialized converter to the |
| 1212 | 1212 |
/// writer. |
| 1213 | 1213 |
template <typename Value, typename Converter> |
| 1214 | 1214 |
GraphWriter& attribute(const std::string& caption, const Value& value, |
| 1215 | 1215 |
const Converter& converter = Converter()) {
|
| 1216 | 1216 |
_writer_bits::ValueStorageBase* storage = |
| 1217 | 1217 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 1218 | 1218 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1219 | 1219 |
return *this; |
| 1220 | 1220 |
} |
| 1221 | 1221 |
|
| 1222 | 1222 |
/// \brief Node writing rule |
| 1223 | 1223 |
/// |
| 1224 | 1224 |
/// Add a node writing rule to the writer. |
| 1225 | 1225 |
GraphWriter& node(const std::string& caption, const Node& node) {
|
| 1226 | 1226 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 1227 | 1227 |
Converter converter(_node_index); |
| 1228 | 1228 |
_writer_bits::ValueStorageBase* storage = |
| 1229 | 1229 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 1230 | 1230 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1231 | 1231 |
return *this; |
| 1232 | 1232 |
} |
| 1233 | 1233 |
|
| 1234 | 1234 |
/// \brief Edge writing rule |
| 1235 | 1235 |
/// |
| 1236 | 1236 |
/// Add an edge writing rule to writer. |
| 1237 | 1237 |
GraphWriter& edge(const std::string& caption, const Edge& edge) {
|
| 1238 | 1238 |
typedef _writer_bits::MapLookUpConverter<Edge> Converter; |
| 1239 | 1239 |
Converter converter(_edge_index); |
| 1240 | 1240 |
_writer_bits::ValueStorageBase* storage = |
| 1241 | 1241 |
new _writer_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1242 | 1242 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1243 | 1243 |
return *this; |
| 1244 | 1244 |
} |
| 1245 | 1245 |
|
| 1246 | 1246 |
/// \brief Arc writing rule |
| 1247 | 1247 |
/// |
| 1248 | 1248 |
/// Add an arc writing rule to writer. |
| 1249 | 1249 |
GraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 1250 | 1250 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1251 | 1251 |
Converter converter(_graph, _edge_index); |
| 1252 | 1252 |
_writer_bits::ValueStorageBase* storage = |
| 1253 | 1253 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1254 | 1254 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1255 | 1255 |
return *this; |
| 1256 | 1256 |
} |
| 1257 | 1257 |
|
| 1258 | 1258 |
/// \name Section captions |
| 1259 | 1259 |
/// @{
|
| 1260 | 1260 |
|
| 1261 | 1261 |
/// \brief Add an additional caption to the \c \@nodes section |
| 1262 | 1262 |
/// |
| 1263 | 1263 |
/// Add an additional caption to the \c \@nodes section. |
| 1264 | 1264 |
GraphWriter& nodes(const std::string& caption) {
|
| 1265 | 1265 |
_nodes_caption = caption; |
| 1266 | 1266 |
return *this; |
| 1267 | 1267 |
} |
| 1268 | 1268 |
|
| 1269 | 1269 |
/// \brief Add an additional caption to the \c \@arcs section |
| 1270 | 1270 |
/// |
| 1271 | 1271 |
/// Add an additional caption to the \c \@arcs section. |
| 1272 | 1272 |
GraphWriter& edges(const std::string& caption) {
|
| 1273 | 1273 |
_edges_caption = caption; |
| 1274 | 1274 |
return *this; |
| 1275 | 1275 |
} |
| 1276 | 1276 |
|
| 1277 | 1277 |
/// \brief Add an additional caption to the \c \@attributes section |
| 1278 | 1278 |
/// |
| 1279 | 1279 |
/// Add an additional caption to the \c \@attributes section. |
| 1280 | 1280 |
GraphWriter& attributes(const std::string& caption) {
|
| 1281 | 1281 |
_attributes_caption = caption; |
| 1282 | 1282 |
return *this; |
| 1283 | 1283 |
} |
| 1284 | 1284 |
|
| 1285 | 1285 |
/// \name Skipping section |
| 1286 | 1286 |
/// @{
|
| 1287 | 1287 |
|
| 1288 | 1288 |
/// \brief Skip writing the node set |
| 1289 | 1289 |
/// |
| 1290 | 1290 |
/// The \c \@nodes section will not be written to the stream. |
| 1291 | 1291 |
GraphWriter& skipNodes() {
|
| 1292 | 1292 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
| 1293 | 1293 |
_skip_nodes = true; |
| 1294 | 1294 |
return *this; |
| 1295 | 1295 |
} |
| 1296 | 1296 |
|
| 1297 | 1297 |
/// \brief Skip writing edge set |
| 1298 | 1298 |
/// |
| 1299 | 1299 |
/// The \c \@edges section will not be written to the stream. |
| 1300 | 1300 |
GraphWriter& skipEdges() {
|
| 1301 | 1301 |
LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member"); |
| 1302 | 1302 |
_skip_edges = true; |
| 1303 | 1303 |
return *this; |
| 1304 | 1304 |
} |
| 1305 | 1305 |
|
| 1306 | 1306 |
/// @} |
| 1307 | 1307 |
|
| 1308 | 1308 |
private: |
| 1309 | 1309 |
|
| 1310 | 1310 |
void writeNodes() {
|
| 1311 | 1311 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 1312 | 1312 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1313 | 1313 |
it != _node_maps.end(); ++it) {
|
| 1314 | 1314 |
if (it->first == "label") {
|
| 1315 | 1315 |
label = it->second; |
| 1316 | 1316 |
break; |
| 1317 | 1317 |
} |
| 1318 | 1318 |
} |
| 1319 | 1319 |
|
| 1320 | 1320 |
*_os << "@nodes"; |
| 1321 | 1321 |
if (!_nodes_caption.empty()) {
|
| 1322 | 1322 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
| 1323 | 1323 |
} |
| 1324 | 1324 |
*_os << std::endl; |
| 1325 | 1325 |
|
| 1326 | 1326 |
if (label == 0) {
|
| 1327 | 1327 |
*_os << "label" << '\t'; |
| 1328 | 1328 |
} |
| 1329 | 1329 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1330 | 1330 |
it != _node_maps.end(); ++it) {
|
| 1331 | 1331 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 1332 | 1332 |
} |
| 1333 | 1333 |
*_os << std::endl; |
| 1334 | 1334 |
|
| 1335 | 1335 |
std::vector<Node> nodes; |
| 1336 | 1336 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1337 | 1337 |
nodes.push_back(n); |
| 1338 | 1338 |
} |
| 1339 | 1339 |
|
| 1340 | 1340 |
if (label == 0) {
|
| 1341 | 1341 |
IdMap<Graph, Node> id_map(_graph); |
| 1342 | 1342 |
_writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map); |
| 1343 | 1343 |
std::sort(nodes.begin(), nodes.end(), id_less); |
| 1344 | 1344 |
} else {
|
| 1345 | 1345 |
label->sort(nodes); |
| 1346 | 1346 |
} |
| 1347 | 1347 |
|
| 1348 | 1348 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 1349 | 1349 |
Node n = nodes[i]; |
| 1350 | 1350 |
if (label == 0) {
|
| 1351 | 1351 |
std::ostringstream os; |
| 1352 | 1352 |
os << _graph.id(n); |
| 1353 | 1353 |
_writer_bits::writeToken(*_os, os.str()); |
| 1354 | 1354 |
*_os << '\t'; |
| 1355 | 1355 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1358 | 1358 |
it != _node_maps.end(); ++it) {
|
| 1359 | 1359 |
std::string value = it->second->get(n); |
| 1360 | 1360 |
_writer_bits::writeToken(*_os, value); |
| 1361 | 1361 |
if (it->first == "label") {
|
| 1362 | 1362 |
_node_index.insert(std::make_pair(n, value)); |
| 1363 | 1363 |
} |
| 1364 | 1364 |
*_os << '\t'; |
| 1365 | 1365 |
} |
| 1366 | 1366 |
*_os << std::endl; |
| 1367 | 1367 |
} |
| 1368 | 1368 |
} |
| 1369 | 1369 |
|
| 1370 | 1370 |
void createNodeIndex() {
|
| 1371 | 1371 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 1372 | 1372 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1373 | 1373 |
it != _node_maps.end(); ++it) {
|
| 1374 | 1374 |
if (it->first == "label") {
|
| 1375 | 1375 |
label = it->second; |
| 1376 | 1376 |
break; |
| 1377 | 1377 |
} |
| 1378 | 1378 |
} |
| 1379 | 1379 |
|
| 1380 | 1380 |
if (label == 0) {
|
| 1381 | 1381 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1382 | 1382 |
std::ostringstream os; |
| 1383 | 1383 |
os << _graph.id(n); |
| 1384 | 1384 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1385 | 1385 |
} |
| 1386 | 1386 |
} else {
|
| 1387 | 1387 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1388 | 1388 |
std::string value = label->get(n); |
| 1389 | 1389 |
_node_index.insert(std::make_pair(n, value)); |
| 1390 | 1390 |
} |
| 1391 | 1391 |
} |
| 1392 | 1392 |
} |
| 1393 | 1393 |
|
| 1394 | 1394 |
void writeEdges() {
|
| 1395 | 1395 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1396 | 1396 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1397 | 1397 |
it != _edge_maps.end(); ++it) {
|
| 1398 | 1398 |
if (it->first == "label") {
|
| 1399 | 1399 |
label = it->second; |
| 1400 | 1400 |
break; |
| 1401 | 1401 |
} |
| 1402 | 1402 |
} |
| 1403 | 1403 |
|
| 1404 | 1404 |
*_os << "@edges"; |
| 1405 | 1405 |
if (!_edges_caption.empty()) {
|
| 1406 | 1406 |
_writer_bits::writeToken(*_os << ' ', _edges_caption); |
| 1407 | 1407 |
} |
| 1408 | 1408 |
*_os << std::endl; |
| 1409 | 1409 |
|
| 1410 | 1410 |
*_os << '\t' << '\t'; |
| 1411 | 1411 |
if (label == 0) {
|
| 1412 | 1412 |
*_os << "label" << '\t'; |
| 1413 | 1413 |
} |
| 1414 | 1414 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1415 | 1415 |
it != _edge_maps.end(); ++it) {
|
| 1416 | 1416 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 1417 | 1417 |
} |
| 1418 | 1418 |
*_os << std::endl; |
| 1419 | 1419 |
|
| 1420 | 1420 |
std::vector<Edge> edges; |
| 1421 | 1421 |
for (EdgeIt n(_graph); n != INVALID; ++n) {
|
| 1422 | 1422 |
edges.push_back(n); |
| 1423 | 1423 |
} |
| 1424 | 1424 |
|
| 1425 | 1425 |
if (label == 0) {
|
| 1426 | 1426 |
IdMap<Graph, Edge> id_map(_graph); |
| 1427 | 1427 |
_writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map); |
| 1428 | 1428 |
std::sort(edges.begin(), edges.end(), id_less); |
| 1429 | 1429 |
} else {
|
| 1430 | 1430 |
label->sort(edges); |
| 1431 | 1431 |
} |
| 1432 | 1432 |
|
| 1433 | 1433 |
for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
|
| 1434 | 1434 |
Edge e = edges[i]; |
| 1435 | 1435 |
_writer_bits::writeToken(*_os, _node_index. |
| 1436 | 1436 |
find(_graph.u(e))->second); |
| 1437 | 1437 |
*_os << '\t'; |
| 1438 | 1438 |
_writer_bits::writeToken(*_os, _node_index. |
| 1439 | 1439 |
find(_graph.v(e))->second); |
| 1440 | 1440 |
*_os << '\t'; |
| 1441 | 1441 |
if (label == 0) {
|
| 1442 | 1442 |
std::ostringstream os; |
| 1443 | 1443 |
os << _graph.id(e); |
| 1444 | 1444 |
_writer_bits::writeToken(*_os, os.str()); |
| 1445 | 1445 |
*_os << '\t'; |
| 1446 | 1446 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1447 | 1447 |
} |
| 1448 | 1448 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1449 | 1449 |
it != _edge_maps.end(); ++it) {
|
| 1450 | 1450 |
std::string value = it->second->get(e); |
| 1451 | 1451 |
_writer_bits::writeToken(*_os, value); |
| 1452 | 1452 |
if (it->first == "label") {
|
| 1453 | 1453 |
_edge_index.insert(std::make_pair(e, value)); |
| 1454 | 1454 |
} |
| 1455 | 1455 |
*_os << '\t'; |
| 1456 | 1456 |
} |
| 1457 | 1457 |
*_os << std::endl; |
| 1458 | 1458 |
} |
| 1459 | 1459 |
} |
| 1460 | 1460 |
|
| 1461 | 1461 |
void createEdgeIndex() {
|
| 1462 | 1462 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1463 | 1463 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1464 | 1464 |
it != _edge_maps.end(); ++it) {
|
| 1465 | 1465 |
if (it->first == "label") {
|
| 1466 | 1466 |
label = it->second; |
| 1467 | 1467 |
break; |
| 1468 | 1468 |
} |
| 1469 | 1469 |
} |
| 1470 | 1470 |
|
| 1471 | 1471 |
if (label == 0) {
|
| 1472 | 1472 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1473 | 1473 |
std::ostringstream os; |
| 1474 | 1474 |
os << _graph.id(e); |
| 1475 | 1475 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1476 | 1476 |
} |
| 1477 | 1477 |
} else {
|
| 1478 | 1478 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1479 | 1479 |
std::string value = label->get(e); |
| 1480 | 1480 |
_edge_index.insert(std::make_pair(e, value)); |
| 1481 | 1481 |
} |
| 1482 | 1482 |
} |
| 1483 | 1483 |
} |
| 1484 | 1484 |
|
| 1485 | 1485 |
void writeAttributes() {
|
| 1486 | 1486 |
if (_attributes.empty()) return; |
| 1487 | 1487 |
*_os << "@attributes"; |
| 1488 | 1488 |
if (!_attributes_caption.empty()) {
|
| 1489 | 1489 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 1490 | 1490 |
} |
| 1491 | 1491 |
*_os << std::endl; |
| 1492 | 1492 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1493 | 1493 |
it != _attributes.end(); ++it) {
|
| 1494 | 1494 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 1495 | 1495 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 1496 | 1496 |
*_os << std::endl; |
| 1497 | 1497 |
} |
| 1498 | 1498 |
} |
| 1499 | 1499 |
|
| 1500 | 1500 |
public: |
| 1501 | 1501 |
|
| 1502 | 1502 |
/// \name Execution of the writer |
| 1503 | 1503 |
/// @{
|
| 1504 | 1504 |
|
| 1505 | 1505 |
/// \brief Start the batch processing |
| 1506 | 1506 |
/// |
| 1507 | 1507 |
/// This function starts the batch processing. |
| 1508 | 1508 |
void run() {
|
| 1509 | 1509 |
if (!_skip_nodes) {
|
| 1510 | 1510 |
writeNodes(); |
| 1511 | 1511 |
} else {
|
| 1512 | 1512 |
createNodeIndex(); |
| 1513 | 1513 |
} |
| 1514 | 1514 |
if (!_skip_edges) {
|
| 1515 | 1515 |
writeEdges(); |
| 1516 | 1516 |
} else {
|
| 1517 | 1517 |
createEdgeIndex(); |
| 1518 | 1518 |
} |
| 1519 | 1519 |
writeAttributes(); |
| 1520 | 1520 |
} |
| 1521 | 1521 |
|
| 1522 | 1522 |
/// \brief Give back the stream of the writer |
| 1523 | 1523 |
/// |
| 1524 | 1524 |
/// Give back the stream of the writer |
| 1525 | 1525 |
std::ostream& ostream() {
|
| 1526 | 1526 |
return *_os; |
| 1527 | 1527 |
} |
| 1528 | 1528 |
|
| 1529 | 1529 |
/// @} |
| 1530 | 1530 |
}; |
| 1531 | 1531 |
|
| 1532 | 1532 |
/// \brief Return a \ref GraphWriter class |
| 1533 | 1533 |
/// |
| 1534 | 1534 |
/// This function just returns a \ref GraphWriter class. |
| 1535 | 1535 |
/// \relates GraphWriter |
| 1536 | 1536 |
template <typename Graph> |
| 1537 | 1537 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
| 1538 | 1538 |
std::ostream& os) {
|
| 1539 | 1539 |
GraphWriter<Graph> tmp(graph, os); |
| 1540 | 1540 |
return tmp; |
| 1541 | 1541 |
} |
| 1542 | 1542 |
|
| 1543 | 1543 |
/// \brief Return a \ref GraphWriter class |
| 1544 | 1544 |
/// |
| 1545 | 1545 |
/// This function just returns a \ref GraphWriter class. |
| 1546 | 1546 |
/// \relates GraphWriter |
| 1547 | 1547 |
template <typename Graph> |
| 1548 | 1548 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
|
| 1549 | 1549 |
GraphWriter<Graph> tmp(graph, fn); |
| 1550 | 1550 |
return tmp; |
| 1551 | 1551 |
} |
| 1552 | 1552 |
|
| 1553 | 1553 |
/// \brief Return a \ref GraphWriter class |
| 1554 | 1554 |
/// |
| 1555 | 1555 |
/// This function just returns a \ref GraphWriter class. |
| 1556 | 1556 |
/// \relates GraphWriter |
| 1557 | 1557 |
template <typename Graph> |
| 1558 | 1558 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
|
| 1559 | 1559 |
GraphWriter<Graph> tmp(graph, fn); |
| 1560 | 1560 |
return tmp; |
| 1561 | 1561 |
} |
| 1562 | 1562 |
|
| 1563 | 1563 |
class SectionWriter; |
| 1564 | 1564 |
|
| 1565 | 1565 |
SectionWriter sectionWriter(std::istream& is); |
| 1566 | 1566 |
SectionWriter sectionWriter(const std::string& fn); |
| 1567 | 1567 |
SectionWriter sectionWriter(const char* fn); |
| 1568 | 1568 |
|
| 1569 | 1569 |
/// \ingroup lemon_io |
| 1570 | 1570 |
/// |
| 1571 | 1571 |
/// \brief Section writer class |
| 1572 | 1572 |
/// |
| 1573 | 1573 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 1574 | 1574 |
/// which contain any data in arbitrary format. Such sections can be |
| 1575 | 1575 |
/// written with this class. A writing rule can be added to the |
| 1576 | 1576 |
/// class with two different functions. With the \c sectionLines() |
| 1577 | 1577 |
/// function a generator can write the section line-by-line, while |
| 1578 | 1578 |
/// with the \c sectionStream() member the section can be written to |
| 1579 | 1579 |
/// an output stream. |
| 1580 | 1580 |
class SectionWriter {
|
| 1581 | 1581 |
private: |
| 1582 | 1582 |
|
| 1583 | 1583 |
std::ostream* _os; |
| 1584 | 1584 |
bool local_os; |
| 1585 | 1585 |
|
| 1586 | 1586 |
typedef std::vector<std::pair<std::string, _writer_bits::Section*> > |
| 1587 | 1587 |
Sections; |
| 1588 | 1588 |
|
| 1589 | 1589 |
Sections _sections; |
| 1590 | 1590 |
|
| 1591 | 1591 |
public: |
| 1592 | 1592 |
|
| 1593 | 1593 |
/// \brief Constructor |
| 1594 | 1594 |
/// |
| 1595 | 1595 |
/// Construct a section writer, which writes to the given output |
| 1596 | 1596 |
/// stream. |
| 1597 | 1597 |
SectionWriter(std::ostream& os) |
| 1598 | 1598 |
: _os(&os), local_os(false) {}
|
| 1599 | 1599 |
|
| 1600 | 1600 |
/// \brief Constructor |
| 1601 | 1601 |
/// |
| 1602 | 1602 |
/// Construct a section writer, which writes into the given file. |
| 1603 | 1603 |
SectionWriter(const std::string& fn) |
| 1604 | 1604 |
: _os(new std::ofstream(fn.c_str())), local_os(true) {
|
| 1605 | 1605 |
if (!(*_os)) {
|
| 1606 | 1606 |
delete _os; |
| 1607 | 1607 |
throw IoError("Cannot write file", fn);
|
| 1608 | 1608 |
} |
| 1609 | 1609 |
} |
| 1610 | 1610 |
|
| 1611 | 1611 |
/// \brief Constructor |
| 1612 | 1612 |
/// |
| 1613 | 1613 |
/// Construct a section writer, which writes into the given file. |
| 1614 | 1614 |
SectionWriter(const char* fn) |
| 1615 | 1615 |
: _os(new std::ofstream(fn)), local_os(true) {
|
| 1616 | 1616 |
if (!(*_os)) {
|
| 1617 | 1617 |
delete _os; |
| 1618 | 1618 |
throw IoError("Cannot write file", fn);
|
| 1619 | 1619 |
} |
| 1620 | 1620 |
} |
| 1621 | 1621 |
|
| 1622 | 1622 |
/// \brief Destructor |
| 1623 | 1623 |
~SectionWriter() {
|
| 1624 | 1624 |
for (Sections::iterator it = _sections.begin(); |
| 1625 | 1625 |
it != _sections.end(); ++it) {
|
| 1626 | 1626 |
delete it->second; |
| 1627 | 1627 |
} |
| 1628 | 1628 |
|
| 1629 | 1629 |
if (local_os) {
|
| 1630 | 1630 |
delete _os; |
| 1631 | 1631 |
} |
| 1632 | 1632 |
|
| 1633 | 1633 |
} |
| 1634 | 1634 |
|
| 1635 | 1635 |
private: |
| 1636 | 1636 |
|
| 1637 | 1637 |
friend SectionWriter sectionWriter(std::ostream& os); |
| 1638 | 1638 |
friend SectionWriter sectionWriter(const std::string& fn); |
| 1639 | 1639 |
friend SectionWriter sectionWriter(const char* fn); |
| 1640 | 1640 |
|
| 1641 | 1641 |
SectionWriter(SectionWriter& other) |
| 1642 | 1642 |
: _os(other._os), local_os(other.local_os) {
|
| 1643 | 1643 |
|
| 1644 | 1644 |
other._os = 0; |
| 1645 | 1645 |
other.local_os = false; |
| 1646 | 1646 |
|
| 1647 | 1647 |
_sections.swap(other._sections); |
| 1648 | 1648 |
} |
| 1649 | 1649 |
|
| 1650 | 1650 |
SectionWriter& operator=(const SectionWriter&); |
| 1651 | 1651 |
|
| 1652 | 1652 |
public: |
| 1653 | 1653 |
|
| 1654 | 1654 |
/// \name Section writers |
| 1655 | 1655 |
/// @{
|
| 1656 | 1656 |
|
| 1657 | 1657 |
/// \brief Add a section writer with line oriented writing |
| 1658 | 1658 |
/// |
| 1659 | 1659 |
/// The first parameter is the type descriptor of the section, the |
| 1660 | 1660 |
/// second is a generator with std::string values. At the writing |
| 1661 | 1661 |
/// process, the returned \c std::string will be written into the |
| 1662 | 1662 |
/// output file until it is an empty string. |
| 1663 | 1663 |
/// |
| 1664 | 1664 |
/// For example, an integer vector is written into a section. |
| 1665 | 1665 |
///\code |
| 1666 | 1666 |
/// @numbers |
| 1667 | 1667 |
/// 12 45 23 78 |
| 1668 | 1668 |
/// 4 28 38 28 |
| 1669 | 1669 |
/// 23 6 16 |
| 1670 | 1670 |
///\endcode |
| 1671 | 1671 |
/// |
| 1672 | 1672 |
/// The generator is implemented as a struct. |
| 1673 | 1673 |
///\code |
| 1674 | 1674 |
/// struct NumberSection {
|
| 1675 | 1675 |
/// std::vector<int>::const_iterator _it, _end; |
| 1676 | 1676 |
/// NumberSection(const std::vector<int>& data) |
| 1677 | 1677 |
/// : _it(data.begin()), _end(data.end()) {}
|
| 1678 | 1678 |
/// std::string operator()() {
|
| 1679 | 1679 |
/// int rem_in_line = 4; |
| 1680 | 1680 |
/// std::ostringstream ls; |
| 1681 | 1681 |
/// while (rem_in_line > 0 && _it != _end) {
|
| 1682 | 1682 |
/// ls << *(_it++) << ' '; |
| 1683 | 1683 |
/// --rem_in_line; |
| 1684 | 1684 |
/// } |
| 1685 | 1685 |
/// return ls.str(); |
| 1686 | 1686 |
/// } |
| 1687 | 1687 |
/// }; |
| 1688 | 1688 |
/// |
| 1689 | 1689 |
/// // ... |
| 1690 | 1690 |
/// |
| 1691 | 1691 |
/// writer.sectionLines("numbers", NumberSection(vec));
|
| 1692 | 1692 |
///\endcode |
| 1693 | 1693 |
template <typename Functor> |
| 1694 | 1694 |
SectionWriter& sectionLines(const std::string& type, Functor functor) {
|
| 1695 | 1695 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1696 | 1696 |
_sections.push_back(std::make_pair(type, |
| 1697 | 1697 |
new _writer_bits::LineSection<Functor>(functor))); |
| 1698 | 1698 |
return *this; |
| 1699 | 1699 |
} |
| 1700 | 1700 |
|
| 1701 | 1701 |
|
| 1702 | 1702 |
/// \brief Add a section writer with stream oriented writing |
| 1703 | 1703 |
/// |
| 1704 | 1704 |
/// The first parameter is the type of the section, the second is |
| 1705 | 1705 |
/// a functor, which takes a \c std::ostream& parameter. The |
| 1706 | 1706 |
/// functor writes the section to the output stream. |
| 1707 | 1707 |
/// \warning The last line must be closed with end-line character. |
| 1708 | 1708 |
template <typename Functor> |
| 1709 | 1709 |
SectionWriter& sectionStream(const std::string& type, Functor functor) {
|
| 1710 | 1710 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1711 | 1711 |
_sections.push_back(std::make_pair(type, |
| 1712 | 1712 |
new _writer_bits::StreamSection<Functor>(functor))); |
| 1713 | 1713 |
return *this; |
| 1714 | 1714 |
} |
| 1715 | 1715 |
|
| 1716 | 1716 |
/// @} |
| 1717 | 1717 |
|
| 1718 | 1718 |
public: |
| 1719 | 1719 |
|
| 1720 | 1720 |
|
| 1721 | 1721 |
/// \name Execution of the writer |
| 1722 | 1722 |
/// @{
|
| 1723 | 1723 |
|
| 1724 | 1724 |
/// \brief Start the batch processing |
| 1725 | 1725 |
/// |
| 1726 | 1726 |
/// This function starts the batch processing. |
| 1727 | 1727 |
void run() {
|
| 1728 | 1728 |
|
| 1729 | 1729 |
LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer"); |
| 1730 | 1730 |
|
| 1731 | 1731 |
for (Sections::iterator it = _sections.begin(); |
| 1732 | 1732 |
it != _sections.end(); ++it) {
|
| 1733 | 1733 |
(*_os) << '@' << it->first << std::endl; |
| 1734 | 1734 |
it->second->process(*_os); |
| 1735 | 1735 |
} |
| 1736 | 1736 |
} |
| 1737 | 1737 |
|
| 1738 | 1738 |
/// \brief Give back the stream of the writer |
| 1739 | 1739 |
/// |
| 1740 | 1740 |
/// Returns the stream of the writer |
| 1741 | 1741 |
std::ostream& ostream() {
|
| 1742 | 1742 |
return *_os; |
| 1743 | 1743 |
} |
| 1744 | 1744 |
|
| 1745 | 1745 |
/// @} |
| 1746 | 1746 |
|
| 1747 | 1747 |
}; |
| 1748 | 1748 |
|
| 1749 | 1749 |
/// \brief Return a \ref SectionWriter class |
| 1750 | 1750 |
/// |
| 1751 | 1751 |
/// This function just returns a \ref SectionWriter class. |
| 1752 | 1752 |
/// \relates SectionWriter |
| 1753 | 1753 |
inline SectionWriter sectionWriter(std::ostream& os) {
|
| 1754 | 1754 |
SectionWriter tmp(os); |
| 1755 | 1755 |
return tmp; |
| 1756 | 1756 |
} |
| 1757 | 1757 |
|
| 1758 | 1758 |
/// \brief Return a \ref SectionWriter class |
| 1759 | 1759 |
/// |
| 1760 | 1760 |
/// This function just returns a \ref SectionWriter class. |
| 1761 | 1761 |
/// \relates SectionWriter |
| 1762 | 1762 |
inline SectionWriter sectionWriter(const std::string& fn) {
|
| 1763 | 1763 |
SectionWriter tmp(fn); |
| 1764 | 1764 |
return tmp; |
| 1765 | 1765 |
} |
| 1766 | 1766 |
|
| 1767 | 1767 |
/// \brief Return a \ref SectionWriter class |
| 1768 | 1768 |
/// |
| 1769 | 1769 |
/// This function just returns a \ref SectionWriter class. |
| 1770 | 1770 |
/// \relates SectionWriter |
| 1771 | 1771 |
inline SectionWriter sectionWriter(const char* fn) {
|
| 1772 | 1772 |
SectionWriter tmp(fn); |
| 1773 | 1773 |
return tmp; |
| 1774 | 1774 |
} |
| 1775 | 1775 |
} |
| 1776 | 1776 |
|
| 1777 | 1777 |
#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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_LIST_GRAPH_H |
| 20 | 20 |
#define LEMON_LIST_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief ListDigraph, ListGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
#include <lemon/bits/graph_extender.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <vector> |
| 31 | 31 |
#include <list> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
class ListDigraphBase {
|
| 36 | 36 |
|
| 37 | 37 |
protected: |
| 38 | 38 |
struct NodeT {
|
| 39 | 39 |
int first_in, first_out; |
| 40 | 40 |
int prev, next; |
| 41 | 41 |
}; |
| 42 | 42 |
|
| 43 | 43 |
struct ArcT {
|
| 44 | 44 |
int target, source; |
| 45 | 45 |
int prev_in, prev_out; |
| 46 | 46 |
int next_in, next_out; |
| 47 | 47 |
}; |
| 48 | 48 |
|
| 49 | 49 |
std::vector<NodeT> nodes; |
| 50 | 50 |
|
| 51 | 51 |
int first_node; |
| 52 | 52 |
|
| 53 | 53 |
int first_free_node; |
| 54 | 54 |
|
| 55 | 55 |
std::vector<ArcT> arcs; |
| 56 | 56 |
|
| 57 | 57 |
int first_free_arc; |
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
|
| 61 | 61 |
typedef ListDigraphBase Digraph; |
| 62 | 62 |
|
| 63 | 63 |
class Node {
|
| 64 | 64 |
friend class ListDigraphBase; |
| 65 | 65 |
protected: |
| 66 | 66 |
|
| 67 | 67 |
int id; |
| 68 | 68 |
explicit Node(int pid) { id = pid;}
|
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
Node() {}
|
| 72 | 72 |
Node (Invalid) { id = -1; }
|
| 73 | 73 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 74 | 74 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 75 | 75 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
class Arc {
|
| 79 | 79 |
friend class ListDigraphBase; |
| 80 | 80 |
protected: |
| 81 | 81 |
|
| 82 | 82 |
int id; |
| 83 | 83 |
explicit Arc(int pid) { id = pid;}
|
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
Arc() {}
|
| 87 | 87 |
Arc (Invalid) { id = -1; }
|
| 88 | 88 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 89 | 89 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 90 | 90 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
ListDigraphBase() |
| 96 | 96 |
: nodes(), first_node(-1), |
| 97 | 97 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
int maxNodeId() const { return nodes.size()-1; }
|
| 101 | 101 |
int maxArcId() const { return arcs.size()-1; }
|
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc e) const { return Node(arcs[e.id].source); }
|
| 104 | 104 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
void first(Node& node) const {
|
| 108 | 108 |
node.id = first_node; |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
void next(Node& node) const {
|
| 112 | 112 |
node.id = nodes[node.id].next; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
void first(Arc& arc) const {
|
| 117 | 117 |
int n; |
| 118 | 118 |
for(n = first_node; |
| 119 | 119 |
n!=-1 && nodes[n].first_in == -1; |
| 120 | 120 |
n = nodes[n].next) {}
|
| 121 | 121 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
void next(Arc& arc) const {
|
| 125 | 125 |
if (arcs[arc.id].next_in != -1) {
|
| 126 | 126 |
arc.id = arcs[arc.id].next_in; |
| 127 | 127 |
} else {
|
| 128 | 128 |
int n; |
| 129 | 129 |
for(n = nodes[arcs[arc.id].target].next; |
| 130 | 130 |
n!=-1 && nodes[n].first_in == -1; |
| 131 | 131 |
n = nodes[n].next) {}
|
| 132 | 132 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstOut(Arc &e, const Node& v) const {
|
| 137 | 137 |
e.id = nodes[v.id].first_out; |
| 138 | 138 |
} |
| 139 | 139 |
void nextOut(Arc &e) const {
|
| 140 | 140 |
e.id=arcs[e.id].next_out; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
void firstIn(Arc &e, const Node& v) const {
|
| 144 | 144 |
e.id = nodes[v.id].first_in; |
| 145 | 145 |
} |
| 146 | 146 |
void nextIn(Arc &e) const {
|
| 147 | 147 |
e.id=arcs[e.id].next_in; |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
static int id(Node v) { return v.id; }
|
| 152 | 152 |
static int id(Arc e) { return e.id; }
|
| 153 | 153 |
|
| 154 | 154 |
static Node nodeFromId(int id) { return Node(id);}
|
| 155 | 155 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 156 | 156 |
|
| 157 | 157 |
bool valid(Node n) const {
|
| 158 | 158 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 159 | 159 |
nodes[n.id].prev != -2; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
bool valid(Arc a) const {
|
| 163 | 163 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 164 | 164 |
arcs[a.id].prev_in != -2; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Node addNode() {
|
| 168 | 168 |
int n; |
| 169 | 169 |
|
| 170 | 170 |
if(first_free_node==-1) {
|
| 171 | 171 |
n = nodes.size(); |
| 172 | 172 |
nodes.push_back(NodeT()); |
| 173 | 173 |
} else {
|
| 174 | 174 |
n = first_free_node; |
| 175 | 175 |
first_free_node = nodes[n].next; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
nodes[n].next = first_node; |
| 179 | 179 |
if(first_node != -1) nodes[first_node].prev = n; |
| 180 | 180 |
first_node = n; |
| 181 | 181 |
nodes[n].prev = -1; |
| 182 | 182 |
|
| 183 | 183 |
nodes[n].first_in = nodes[n].first_out = -1; |
| 184 | 184 |
|
| 185 | 185 |
return Node(n); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
Arc addArc(Node u, Node v) {
|
| 189 | 189 |
int n; |
| 190 | 190 |
|
| 191 | 191 |
if (first_free_arc == -1) {
|
| 192 | 192 |
n = arcs.size(); |
| 193 | 193 |
arcs.push_back(ArcT()); |
| 194 | 194 |
} else {
|
| 195 | 195 |
n = first_free_arc; |
| 196 | 196 |
first_free_arc = arcs[n].next_in; |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 | 199 |
arcs[n].source = u.id; |
| 200 | 200 |
arcs[n].target = v.id; |
| 201 | 201 |
|
| 202 | 202 |
arcs[n].next_out = nodes[u.id].first_out; |
| 203 | 203 |
if(nodes[u.id].first_out != -1) {
|
| 204 | 204 |
arcs[nodes[u.id].first_out].prev_out = n; |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
arcs[n].next_in = nodes[v.id].first_in; |
| 208 | 208 |
if(nodes[v.id].first_in != -1) {
|
| 209 | 209 |
arcs[nodes[v.id].first_in].prev_in = n; |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 | 212 |
arcs[n].prev_in = arcs[n].prev_out = -1; |
| 213 | 213 |
|
| 214 | 214 |
nodes[u.id].first_out = nodes[v.id].first_in = n; |
| 215 | 215 |
|
| 216 | 216 |
return Arc(n); |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
void erase(const Node& node) {
|
| 220 | 220 |
int n = node.id; |
| 221 | 221 |
|
| 222 | 222 |
if(nodes[n].next != -1) {
|
| 223 | 223 |
nodes[nodes[n].next].prev = nodes[n].prev; |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
if(nodes[n].prev != -1) {
|
| 227 | 227 |
nodes[nodes[n].prev].next = nodes[n].next; |
| 228 | 228 |
} else {
|
| 229 | 229 |
first_node = nodes[n].next; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
nodes[n].next = first_free_node; |
| 233 | 233 |
first_free_node = n; |
| 234 | 234 |
nodes[n].prev = -2; |
| 235 | 235 |
|
| 236 | 236 |
} |
| 237 | 237 |
|
| 238 | 238 |
void erase(const Arc& arc) {
|
| 239 | 239 |
int n = arc.id; |
| 240 | 240 |
|
| 241 | 241 |
if(arcs[n].next_in!=-1) {
|
| 242 | 242 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
if(arcs[n].prev_in!=-1) {
|
| 246 | 246 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
| 247 | 247 |
} else {
|
| 248 | 248 |
nodes[arcs[n].target].first_in = arcs[n].next_in; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
|
| 252 | 252 |
if(arcs[n].next_out!=-1) {
|
| 253 | 253 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 254 | 254 |
} |
| 255 | 255 |
|
| 256 | 256 |
if(arcs[n].prev_out!=-1) {
|
| 257 | 257 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 258 | 258 |
} else {
|
| 259 | 259 |
nodes[arcs[n].source].first_out = arcs[n].next_out; |
| 260 | 260 |
} |
| 261 | 261 |
|
| 262 | 262 |
arcs[n].next_in = first_free_arc; |
| 263 | 263 |
first_free_arc = n; |
| 264 | 264 |
arcs[n].prev_in = -2; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
void clear() {
|
| 268 | 268 |
arcs.clear(); |
| 269 | 269 |
nodes.clear(); |
| 270 | 270 |
first_node = first_free_node = first_free_arc = -1; |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
protected: |
| 274 | 274 |
void changeTarget(Arc e, Node n) |
| 275 | 275 |
{
|
| 276 | 276 |
if(arcs[e.id].next_in != -1) |
| 277 | 277 |
arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
| 278 | 278 |
if(arcs[e.id].prev_in != -1) |
| 279 | 279 |
arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
| 280 | 280 |
else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
| 281 | 281 |
if (nodes[n.id].first_in != -1) {
|
| 282 | 282 |
arcs[nodes[n.id].first_in].prev_in = e.id; |
| 283 | 283 |
} |
| 284 | 284 |
arcs[e.id].target = n.id; |
| 285 | 285 |
arcs[e.id].prev_in = -1; |
| 286 | 286 |
arcs[e.id].next_in = nodes[n.id].first_in; |
| 287 | 287 |
nodes[n.id].first_in = e.id; |
| 288 | 288 |
} |
| 289 | 289 |
void changeSource(Arc e, Node n) |
| 290 | 290 |
{
|
| 291 | 291 |
if(arcs[e.id].next_out != -1) |
| 292 | 292 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
| 293 | 293 |
if(arcs[e.id].prev_out != -1) |
| 294 | 294 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
| 295 | 295 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
| 296 | 296 |
if (nodes[n.id].first_out != -1) {
|
| 297 | 297 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
| 298 | 298 |
} |
| 299 | 299 |
arcs[e.id].source = n.id; |
| 300 | 300 |
arcs[e.id].prev_out = -1; |
| 301 | 301 |
arcs[e.id].next_out = nodes[n.id].first_out; |
| 302 | 302 |
nodes[n.id].first_out = e.id; |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 | 305 |
}; |
| 306 | 306 |
|
| 307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
| 308 | 308 |
|
| 309 | 309 |
/// \addtogroup graphs |
| 310 | 310 |
/// @{
|
| 311 | 311 |
|
| 312 | 312 |
///A general directed graph structure. |
| 313 | 313 |
|
| 314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
| 315 | 315 |
///implementation based on static linked lists that are stored in |
| 316 | 316 |
///\c std::vector structures. |
| 317 | 317 |
/// |
| 318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
| 319 | 319 |
///also provides several useful additional functionalities. |
| 320 | 320 |
///Most of the member functions and nested classes are documented |
| 321 | 321 |
///only in the concept class. |
| 322 | 322 |
/// |
| 323 | 323 |
///An important extra feature of this digraph implementation is that |
| 324 | 324 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
| 325 | 325 |
/// |
| 326 | 326 |
///\sa concepts::Digraph |
| 327 | 327 |
|
| 328 | 328 |
class ListDigraph : public ExtendedListDigraphBase {
|
| 329 | 329 |
private: |
| 330 | 330 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
| 331 | 331 |
|
| 332 | 332 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
| 333 | 333 |
/// |
| 334 | 334 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
|
| 335 | 335 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
| 336 | 336 |
///Use copyDigraph() instead. |
| 337 | 337 |
|
| 338 | 338 |
///Assignment of ListDigraph to another one is \e not allowed. |
| 339 | 339 |
///Use copyDigraph() instead. |
| 340 | 340 |
void operator=(const ListDigraph &) {}
|
| 341 | 341 |
public: |
| 342 | 342 |
|
| 343 | 343 |
typedef ExtendedListDigraphBase Parent; |
| 344 | 344 |
|
| 345 | 345 |
/// Constructor |
| 346 | 346 |
|
| 347 | 347 |
/// Constructor. |
| 348 | 348 |
/// |
| 349 | 349 |
ListDigraph() {}
|
| 350 | 350 |
|
| 351 | 351 |
///Add a new node to the digraph. |
| 352 | 352 |
|
| 353 | 353 |
///Add a new node to the digraph. |
| 354 | 354 |
///\return the new node. |
| 355 | 355 |
Node addNode() { return Parent::addNode(); }
|
| 356 | 356 |
|
| 357 | 357 |
///Add a new arc to the digraph. |
| 358 | 358 |
|
| 359 | 359 |
///Add a new arc to the digraph with source node \c s |
| 360 | 360 |
///and target node \c t. |
| 361 | 361 |
///\return the new arc. |
| 362 | 362 |
Arc addArc(const Node& s, const Node& t) {
|
| 363 | 363 |
return Parent::addArc(s, t); |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 | 366 |
///\brief Erase a node from the digraph. |
| 367 | 367 |
/// |
| 368 | 368 |
///Erase a node from the digraph. |
| 369 | 369 |
/// |
| 370 | 370 |
void erase(const Node& n) { Parent::erase(n); }
|
| 371 | 371 |
|
| 372 | 372 |
///\brief Erase an arc from the digraph. |
| 373 | 373 |
/// |
| 374 | 374 |
///Erase an arc from the digraph. |
| 375 | 375 |
/// |
| 376 | 376 |
void erase(const Arc& a) { Parent::erase(a); }
|
| 377 | 377 |
|
| 378 | 378 |
/// Node validity check |
| 379 | 379 |
|
| 380 | 380 |
/// This function gives back true if the given node is valid, |
| 381 | 381 |
/// ie. it is a real node of the graph. |
| 382 | 382 |
/// |
| 383 | 383 |
/// \warning A Node pointing to a removed item |
| 384 | 384 |
/// could become valid again later if new nodes are |
| 385 | 385 |
/// added to the graph. |
| 386 | 386 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 387 | 387 |
|
| 388 | 388 |
/// Arc validity check |
| 389 | 389 |
|
| 390 | 390 |
/// This function gives back true if the given arc is valid, |
| 391 | 391 |
/// ie. it is a real arc of the graph. |
| 392 | 392 |
/// |
| 393 | 393 |
/// \warning An Arc pointing to a removed item |
| 394 | 394 |
/// could become valid again later if new nodes are |
| 395 | 395 |
/// added to the graph. |
| 396 | 396 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 397 | 397 |
|
| 398 | 398 |
/// Change the target of \c a to \c n |
| 399 | 399 |
|
| 400 | 400 |
/// Change the target of \c a to \c n |
| 401 | 401 |
/// |
| 402 | 402 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
| 403 | 403 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
| 404 | 404 |
///invalidated. |
| 405 | 405 |
/// |
| 406 | 406 |
///\warning This functionality cannot be used together with the Snapshot |
| 407 | 407 |
///feature. |
| 408 | 408 |
void changeTarget(Arc a, Node n) {
|
| 409 | 409 |
Parent::changeTarget(a,n); |
| 410 | 410 |
} |
| 411 | 411 |
/// Change the source of \c a to \c n |
| 412 | 412 |
|
| 413 | 413 |
/// Change the source of \c a to \c n |
| 414 | 414 |
/// |
| 415 | 415 |
///\note The <tt>InArcIt</tt>s referencing the changed arc remain |
| 416 | 416 |
///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are |
| 417 | 417 |
///invalidated. |
| 418 | 418 |
/// |
| 419 | 419 |
///\warning This functionality cannot be used together with the Snapshot |
| 420 | 420 |
///feature. |
| 421 | 421 |
void changeSource(Arc a, Node n) {
|
| 422 | 422 |
Parent::changeSource(a,n); |
| 423 | 423 |
} |
| 424 | 424 |
|
| 425 | 425 |
/// Invert the direction of an arc. |
| 426 | 426 |
|
| 427 | 427 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
| 428 | 428 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
| 429 | 429 |
///invalidated. |
| 430 | 430 |
/// |
| 431 | 431 |
///\warning This functionality cannot be used together with the Snapshot |
| 432 | 432 |
///feature. |
| 433 | 433 |
void reverseArc(Arc e) {
|
| 434 | 434 |
Node t=target(e); |
| 435 | 435 |
changeTarget(e,source(e)); |
| 436 | 436 |
changeSource(e,t); |
| 437 | 437 |
} |
| 438 | 438 |
|
| 439 | 439 |
/// Reserve memory for nodes. |
| 440 | 440 |
|
| 441 | 441 |
/// Using this function it is possible to avoid the superfluous memory |
| 442 | 442 |
/// allocation: if you know that the digraph you want to build will |
| 443 | 443 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 444 | 444 |
/// then it is worth reserving space for this amount before starting |
| 445 | 445 |
/// to build the digraph. |
| 446 | 446 |
/// \sa reserveArc |
| 447 | 447 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 448 | 448 |
|
| 449 | 449 |
/// Reserve memory for arcs. |
| 450 | 450 |
|
| 451 | 451 |
/// Using this function it is possible to avoid the superfluous memory |
| 452 | 452 |
/// allocation: if you know that the digraph you want to build will |
| 453 | 453 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 454 | 454 |
/// then it is worth reserving space for this amount before starting |
| 455 | 455 |
/// to build the digraph. |
| 456 | 456 |
/// \sa reserveNode |
| 457 | 457 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 458 | 458 |
|
| 459 | 459 |
///Contract two nodes. |
| 460 | 460 |
|
| 461 | 461 |
///This function contracts two nodes. |
| 462 | 462 |
///Node \p b will be removed but instead of deleting |
| 463 | 463 |
///incident arcs, they will be joined to \p a. |
| 464 | 464 |
///The last parameter \p r controls whether to remove loops. \c true |
| 465 | 465 |
///means that loops will be removed. |
| 466 | 466 |
/// |
| 467 | 467 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 468 | 468 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
| 469 | 469 |
///may be invalidated. |
| 470 | 470 |
/// |
| 471 | 471 |
///\warning This functionality cannot be used together with the Snapshot |
| 472 | 472 |
///feature. |
| 473 | 473 |
void contract(Node a, Node b, bool r = true) |
| 474 | 474 |
{
|
| 475 | 475 |
for(OutArcIt e(*this,b);e!=INVALID;) {
|
| 476 | 476 |
OutArcIt f=e; |
| 477 | 477 |
++f; |
| 478 | 478 |
if(r && target(e)==a) erase(e); |
| 479 | 479 |
else changeSource(e,a); |
| 480 | 480 |
e=f; |
| 481 | 481 |
} |
| 482 | 482 |
for(InArcIt e(*this,b);e!=INVALID;) {
|
| 483 | 483 |
InArcIt f=e; |
| 484 | 484 |
++f; |
| 485 | 485 |
if(r && source(e)==a) erase(e); |
| 486 | 486 |
else changeTarget(e,a); |
| 487 | 487 |
e=f; |
| 488 | 488 |
} |
| 489 | 489 |
erase(b); |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
///Split a node. |
| 493 | 493 |
|
| 494 | 494 |
///This function splits a node. First a new node is added to the digraph, |
| 495 | 495 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 496 | 496 |
///If \c connect is \c true (this is the default value), then a new arc |
| 497 | 497 |
///from \c n to the newly created node is also added. |
| 498 | 498 |
///\return The newly created node. |
| 499 | 499 |
/// |
| 500 | 500 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 501 | 501 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
| 502 | 502 |
///be invalidated. |
| 503 | 503 |
/// |
| 504 | 504 |
///\warning This functionality cannot be used in conjunction with the |
| 505 | 505 |
///Snapshot feature. |
| 506 | 506 |
Node split(Node n, bool connect = true) {
|
| 507 | 507 |
Node b = addNode(); |
| 508 | 508 |
for(OutArcIt e(*this,n);e!=INVALID;) {
|
| 509 | 509 |
OutArcIt f=e; |
| 510 | 510 |
++f; |
| 511 | 511 |
changeSource(e,b); |
| 512 | 512 |
e=f; |
| 513 | 513 |
} |
| 514 | 514 |
if (connect) addArc(n,b); |
| 515 | 515 |
return b; |
| 516 | 516 |
} |
| 517 | 517 |
|
| 518 | 518 |
///Split an arc. |
| 519 | 519 |
|
| 520 | 520 |
///This function splits an arc. First a new node \c b is added to |
| 521 | 521 |
///the digraph, then the original arc is re-targeted to \c |
| 522 | 522 |
///b. Finally an arc from \c b to the original target is added. |
| 523 | 523 |
/// |
| 524 | 524 |
///\return The newly created node. |
| 525 | 525 |
/// |
| 526 | 526 |
///\warning This functionality cannot be used together with the |
| 527 | 527 |
///Snapshot feature. |
| 528 | 528 |
Node split(Arc e) {
|
| 529 | 529 |
Node b = addNode(); |
| 530 | 530 |
addArc(b,target(e)); |
| 531 | 531 |
changeTarget(e,b); |
| 532 | 532 |
return b; |
| 533 | 533 |
} |
| 534 | 534 |
|
| 535 | 535 |
/// \brief Class to make a snapshot of the digraph and restore |
| 536 | 536 |
/// it later. |
| 537 | 537 |
/// |
| 538 | 538 |
/// Class to make a snapshot of the digraph and restore it later. |
| 539 | 539 |
/// |
| 540 | 540 |
/// The newly added nodes and arcs can be removed using the |
| 541 | 541 |
/// restore() function. |
| 542 | 542 |
/// |
| 543 | 543 |
/// \warning Arc and node deletions and other modifications (e.g. |
| 544 | 544 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
| 545 | 545 |
/// restored. These events invalidate the snapshot. |
| 546 | 546 |
class Snapshot {
|
| 547 | 547 |
protected: |
| 548 | 548 |
|
| 549 | 549 |
typedef Parent::NodeNotifier NodeNotifier; |
| 550 | 550 |
|
| 551 | 551 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 552 | 552 |
public: |
| 553 | 553 |
|
| 554 | 554 |
NodeObserverProxy(Snapshot& _snapshot) |
| 555 | 555 |
: snapshot(_snapshot) {}
|
| 556 | 556 |
|
| 557 | 557 |
using NodeNotifier::ObserverBase::attach; |
| 558 | 558 |
using NodeNotifier::ObserverBase::detach; |
| 559 | 559 |
using NodeNotifier::ObserverBase::attached; |
| 560 | 560 |
|
| 561 | 561 |
protected: |
| 562 | 562 |
|
| 563 | 563 |
virtual void add(const Node& node) {
|
| 564 | 564 |
snapshot.addNode(node); |
| 565 | 565 |
} |
| 566 | 566 |
virtual void add(const std::vector<Node>& nodes) {
|
| 567 | 567 |
for (int i = nodes.size() - 1; i >= 0; ++i) {
|
| 568 | 568 |
snapshot.addNode(nodes[i]); |
| 569 | 569 |
} |
| 570 | 570 |
} |
| 571 | 571 |
virtual void erase(const Node& node) {
|
| 572 | 572 |
snapshot.eraseNode(node); |
| 573 | 573 |
} |
| 574 | 574 |
virtual void erase(const std::vector<Node>& nodes) {
|
| 575 | 575 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 576 | 576 |
snapshot.eraseNode(nodes[i]); |
| 577 | 577 |
} |
| 578 | 578 |
} |
| 579 | 579 |
virtual void build() {
|
| 580 | 580 |
Node node; |
| 581 | 581 |
std::vector<Node> nodes; |
| 582 | 582 |
for (notifier()->first(node); node != INVALID; |
| 583 | 583 |
notifier()->next(node)) {
|
| 584 | 584 |
nodes.push_back(node); |
| 585 | 585 |
} |
| 586 | 586 |
for (int i = nodes.size() - 1; i >= 0; --i) {
|
| 587 | 587 |
snapshot.addNode(nodes[i]); |
| 588 | 588 |
} |
| 589 | 589 |
} |
| 590 | 590 |
virtual void clear() {
|
| 591 | 591 |
Node node; |
| 592 | 592 |
for (notifier()->first(node); node != INVALID; |
| 593 | 593 |
notifier()->next(node)) {
|
| 594 | 594 |
snapshot.eraseNode(node); |
| 595 | 595 |
} |
| 596 | 596 |
} |
| 597 | 597 |
|
| 598 | 598 |
Snapshot& snapshot; |
| 599 | 599 |
}; |
| 600 | 600 |
|
| 601 | 601 |
class ArcObserverProxy : public ArcNotifier::ObserverBase {
|
| 602 | 602 |
public: |
| 603 | 603 |
|
| 604 | 604 |
ArcObserverProxy(Snapshot& _snapshot) |
| 605 | 605 |
: snapshot(_snapshot) {}
|
| 606 | 606 |
|
| 607 | 607 |
using ArcNotifier::ObserverBase::attach; |
| 608 | 608 |
using ArcNotifier::ObserverBase::detach; |
| 609 | 609 |
using ArcNotifier::ObserverBase::attached; |
| 610 | 610 |
|
| 611 | 611 |
protected: |
| 612 | 612 |
|
| 613 | 613 |
virtual void add(const Arc& arc) {
|
| 614 | 614 |
snapshot.addArc(arc); |
| 615 | 615 |
} |
| 616 | 616 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 617 | 617 |
for (int i = arcs.size() - 1; i >= 0; ++i) {
|
| 618 | 618 |
snapshot.addArc(arcs[i]); |
| 619 | 619 |
} |
| 620 | 620 |
} |
| 621 | 621 |
virtual void erase(const Arc& arc) {
|
| 622 | 622 |
snapshot.eraseArc(arc); |
| 623 | 623 |
} |
| 624 | 624 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 625 | 625 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 626 | 626 |
snapshot.eraseArc(arcs[i]); |
| 627 | 627 |
} |
| 628 | 628 |
} |
| 629 | 629 |
virtual void build() {
|
| 630 | 630 |
Arc arc; |
| 631 | 631 |
std::vector<Arc> arcs; |
| 632 | 632 |
for (notifier()->first(arc); arc != INVALID; |
| 633 | 633 |
notifier()->next(arc)) {
|
| 634 | 634 |
arcs.push_back(arc); |
| 635 | 635 |
} |
| 636 | 636 |
for (int i = arcs.size() - 1; i >= 0; --i) {
|
| 637 | 637 |
snapshot.addArc(arcs[i]); |
| 638 | 638 |
} |
| 639 | 639 |
} |
| 640 | 640 |
virtual void clear() {
|
| 641 | 641 |
Arc arc; |
| 642 | 642 |
for (notifier()->first(arc); arc != INVALID; |
| 643 | 643 |
notifier()->next(arc)) {
|
| 644 | 644 |
snapshot.eraseArc(arc); |
| 645 | 645 |
} |
| 646 | 646 |
} |
| 647 | 647 |
|
| 648 | 648 |
Snapshot& snapshot; |
| 649 | 649 |
}; |
| 650 | 650 |
|
| 651 | 651 |
ListDigraph *digraph; |
| 652 | 652 |
|
| 653 | 653 |
NodeObserverProxy node_observer_proxy; |
| 654 | 654 |
ArcObserverProxy arc_observer_proxy; |
| 655 | 655 |
|
| 656 | 656 |
std::list<Node> added_nodes; |
| 657 | 657 |
std::list<Arc> added_arcs; |
| 658 | 658 |
|
| 659 | 659 |
|
| 660 | 660 |
void addNode(const Node& node) {
|
| 661 | 661 |
added_nodes.push_front(node); |
| 662 | 662 |
} |
| 663 | 663 |
void eraseNode(const Node& node) {
|
| 664 | 664 |
std::list<Node>::iterator it = |
| 665 | 665 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
| 666 | 666 |
if (it == added_nodes.end()) {
|
| 667 | 667 |
clear(); |
| 668 | 668 |
arc_observer_proxy.detach(); |
| 669 | 669 |
throw NodeNotifier::ImmediateDetach(); |
| 670 | 670 |
} else {
|
| 671 | 671 |
added_nodes.erase(it); |
| 672 | 672 |
} |
| 673 | 673 |
} |
| 674 | 674 |
|
| 675 | 675 |
void addArc(const Arc& arc) {
|
| 676 | 676 |
added_arcs.push_front(arc); |
| 677 | 677 |
} |
| 678 | 678 |
void eraseArc(const Arc& arc) {
|
| 679 | 679 |
std::list<Arc>::iterator it = |
| 680 | 680 |
std::find(added_arcs.begin(), added_arcs.end(), arc); |
| 681 | 681 |
if (it == added_arcs.end()) {
|
| 682 | 682 |
clear(); |
| 683 | 683 |
node_observer_proxy.detach(); |
| 684 | 684 |
throw ArcNotifier::ImmediateDetach(); |
| 685 | 685 |
} else {
|
| 686 | 686 |
added_arcs.erase(it); |
| 687 | 687 |
} |
| 688 | 688 |
} |
| 689 | 689 |
|
| 690 | 690 |
void attach(ListDigraph &_digraph) {
|
| 691 | 691 |
digraph = &_digraph; |
| 692 | 692 |
node_observer_proxy.attach(digraph->notifier(Node())); |
| 693 | 693 |
arc_observer_proxy.attach(digraph->notifier(Arc())); |
| 694 | 694 |
} |
| 695 | 695 |
|
| 696 | 696 |
void detach() {
|
| 697 | 697 |
node_observer_proxy.detach(); |
| 698 | 698 |
arc_observer_proxy.detach(); |
| 699 | 699 |
} |
| 700 | 700 |
|
| 701 | 701 |
bool attached() const {
|
| 702 | 702 |
return node_observer_proxy.attached(); |
| 703 | 703 |
} |
| 704 | 704 |
|
| 705 | 705 |
void clear() {
|
| 706 | 706 |
added_nodes.clear(); |
| 707 | 707 |
added_arcs.clear(); |
| 708 | 708 |
} |
| 709 | 709 |
|
| 710 | 710 |
public: |
| 711 | 711 |
|
| 712 | 712 |
/// \brief Default constructor. |
| 713 | 713 |
/// |
| 714 | 714 |
/// Default constructor. |
| 715 | 715 |
/// To actually make a snapshot you must call save(). |
| 716 | 716 |
Snapshot() |
| 717 | 717 |
: digraph(0), node_observer_proxy(*this), |
| 718 | 718 |
arc_observer_proxy(*this) {}
|
| 719 | 719 |
|
| 720 | 720 |
/// \brief Constructor that immediately makes a snapshot. |
| 721 | 721 |
/// |
| 722 | 722 |
/// This constructor immediately makes a snapshot of the digraph. |
| 723 | 723 |
/// \param _digraph The digraph we make a snapshot of. |
| 724 | 724 |
Snapshot(ListDigraph &_digraph) |
| 725 | 725 |
: node_observer_proxy(*this), |
| 726 | 726 |
arc_observer_proxy(*this) {
|
| 727 | 727 |
attach(_digraph); |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
/// \brief Make a snapshot. |
| 731 | 731 |
/// |
| 732 | 732 |
/// Make a snapshot of the digraph. |
| 733 | 733 |
/// |
| 734 | 734 |
/// This function can be called more than once. In case of a repeated |
| 735 | 735 |
/// call, the previous snapshot gets lost. |
| 736 | 736 |
/// \param _digraph The digraph we make the snapshot of. |
| 737 | 737 |
void save(ListDigraph &_digraph) {
|
| 738 | 738 |
if (attached()) {
|
| 739 | 739 |
detach(); |
| 740 | 740 |
clear(); |
| 741 | 741 |
} |
| 742 | 742 |
attach(_digraph); |
| 743 | 743 |
} |
| 744 | 744 |
|
| 745 | 745 |
/// \brief Undo the changes until the last snapshot. |
| 746 | 746 |
// |
| 747 | 747 |
/// Undo the changes until the last snapshot created by save(). |
| 748 | 748 |
void restore() {
|
| 749 | 749 |
detach(); |
| 750 | 750 |
for(std::list<Arc>::iterator it = added_arcs.begin(); |
| 751 | 751 |
it != added_arcs.end(); ++it) {
|
| 752 | 752 |
digraph->erase(*it); |
| 753 | 753 |
} |
| 754 | 754 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
| 755 | 755 |
it != added_nodes.end(); ++it) {
|
| 756 | 756 |
digraph->erase(*it); |
| 757 | 757 |
} |
| 758 | 758 |
clear(); |
| 759 | 759 |
} |
| 760 | 760 |
|
| 761 | 761 |
/// \brief Gives back true when the snapshot is valid. |
| 762 | 762 |
/// |
| 763 | 763 |
/// Gives back true when the snapshot is valid. |
| 764 | 764 |
bool valid() const {
|
| 765 | 765 |
return attached(); |
| 766 | 766 |
} |
| 767 | 767 |
}; |
| 768 | 768 |
|
| 769 | 769 |
}; |
| 770 | 770 |
|
| 771 | 771 |
///@} |
| 772 | 772 |
|
| 773 | 773 |
class ListGraphBase {
|
| 774 | 774 |
|
| 775 | 775 |
protected: |
| 776 | 776 |
|
| 777 | 777 |
struct NodeT {
|
| 778 | 778 |
int first_out; |
| 779 | 779 |
int prev, next; |
| 780 | 780 |
}; |
| 781 | 781 |
|
| 782 | 782 |
struct ArcT {
|
| 783 | 783 |
int target; |
| 784 | 784 |
int prev_out, next_out; |
| 785 | 785 |
}; |
| 786 | 786 |
|
| 787 | 787 |
std::vector<NodeT> nodes; |
| 788 | 788 |
|
| 789 | 789 |
int first_node; |
| 790 | 790 |
|
| 791 | 791 |
int first_free_node; |
| 792 | 792 |
|
| 793 | 793 |
std::vector<ArcT> arcs; |
| 794 | 794 |
|
| 795 | 795 |
int first_free_arc; |
| 796 | 796 |
|
| 797 | 797 |
public: |
| 798 | 798 |
|
| 799 | 799 |
typedef ListGraphBase Digraph; |
| 800 | 800 |
|
| 801 | 801 |
class Node; |
| 802 | 802 |
class Arc; |
| 803 | 803 |
class Edge; |
| 804 | 804 |
|
| 805 | 805 |
class Node {
|
| 806 | 806 |
friend class ListGraphBase; |
| 807 | 807 |
protected: |
| 808 | 808 |
|
| 809 | 809 |
int id; |
| 810 | 810 |
explicit Node(int pid) { id = pid;}
|
| 811 | 811 |
|
| 812 | 812 |
public: |
| 813 | 813 |
Node() {}
|
| 814 | 814 |
Node (Invalid) { id = -1; }
|
| 815 | 815 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 816 | 816 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 817 | 817 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 818 | 818 |
}; |
| 819 | 819 |
|
| 820 | 820 |
class Edge {
|
| 821 | 821 |
friend class ListGraphBase; |
| 822 | 822 |
protected: |
| 823 | 823 |
|
| 824 | 824 |
int id; |
| 825 | 825 |
explicit Edge(int pid) { id = pid;}
|
| 826 | 826 |
|
| 827 | 827 |
public: |
| 828 | 828 |
Edge() {}
|
| 829 | 829 |
Edge (Invalid) { id = -1; }
|
| 830 | 830 |
bool operator==(const Edge& edge) const {return id == edge.id;}
|
| 831 | 831 |
bool operator!=(const Edge& edge) const {return id != edge.id;}
|
| 832 | 832 |
bool operator<(const Edge& edge) const {return id < edge.id;}
|
| 833 | 833 |
}; |
| 834 | 834 |
|
| 835 | 835 |
class Arc {
|
| 836 | 836 |
friend class ListGraphBase; |
| 837 | 837 |
protected: |
| 838 | 838 |
|
| 839 | 839 |
int id; |
| 840 | 840 |
explicit Arc(int pid) { id = pid;}
|
| 841 | 841 |
|
| 842 | 842 |
public: |
| 843 |
operator Edge() const {
|
|
| 844 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
|
| 843 |
operator Edge() const {
|
|
| 844 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
|
| 845 | 845 |
} |
| 846 | 846 |
|
| 847 | 847 |
Arc() {}
|
| 848 | 848 |
Arc (Invalid) { id = -1; }
|
| 849 | 849 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 850 | 850 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 851 | 851 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 852 | 852 |
}; |
| 853 | 853 |
|
| 854 | 854 |
|
| 855 | 855 |
|
| 856 | 856 |
ListGraphBase() |
| 857 | 857 |
: nodes(), first_node(-1), |
| 858 | 858 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 859 | 859 |
|
| 860 | 860 |
|
| 861 | 861 |
int maxNodeId() const { return nodes.size()-1; }
|
| 862 | 862 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 863 | 863 |
int maxArcId() const { return arcs.size()-1; }
|
| 864 | 864 |
|
| 865 | 865 |
Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
|
| 866 | 866 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 867 | 867 |
|
| 868 | 868 |
Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
|
| 869 | 869 |
Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
|
| 870 | 870 |
|
| 871 | 871 |
static bool direction(Arc e) {
|
| 872 | 872 |
return (e.id & 1) == 1; |
| 873 | 873 |
} |
| 874 | 874 |
|
| 875 | 875 |
static Arc direct(Edge e, bool d) {
|
| 876 | 876 |
return Arc(e.id * 2 + (d ? 1 : 0)); |
| 877 | 877 |
} |
| 878 | 878 |
|
| 879 | 879 |
void first(Node& node) const {
|
| 880 | 880 |
node.id = first_node; |
| 881 | 881 |
} |
| 882 | 882 |
|
| 883 | 883 |
void next(Node& node) const {
|
| 884 | 884 |
node.id = nodes[node.id].next; |
| 885 | 885 |
} |
| 886 | 886 |
|
| 887 | 887 |
void first(Arc& e) const {
|
| 888 | 888 |
int n = first_node; |
| 889 | 889 |
while (n != -1 && nodes[n].first_out == -1) {
|
| 890 | 890 |
n = nodes[n].next; |
| 891 | 891 |
} |
| 892 | 892 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 893 | 893 |
} |
| 894 | 894 |
|
| 895 | 895 |
void next(Arc& e) const {
|
| 896 | 896 |
if (arcs[e.id].next_out != -1) {
|
| 897 | 897 |
e.id = arcs[e.id].next_out; |
| 898 | 898 |
} else {
|
| 899 | 899 |
int n = nodes[arcs[e.id ^ 1].target].next; |
| 900 | 900 |
while(n != -1 && nodes[n].first_out == -1) {
|
| 901 | 901 |
n = nodes[n].next; |
| 902 | 902 |
} |
| 903 | 903 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 904 | 904 |
} |
| 905 | 905 |
} |
| 906 | 906 |
|
| 907 | 907 |
void first(Edge& e) const {
|
| 908 | 908 |
int n = first_node; |
| 909 | 909 |
while (n != -1) {
|
| 910 | 910 |
e.id = nodes[n].first_out; |
| 911 | 911 |
while ((e.id & 1) != 1) {
|
| 912 | 912 |
e.id = arcs[e.id].next_out; |
| 913 | 913 |
} |
| 914 | 914 |
if (e.id != -1) {
|
| 915 | 915 |
e.id /= 2; |
| 916 | 916 |
return; |
| 917 | 917 |
} |
| 918 | 918 |
n = nodes[n].next; |
| 919 | 919 |
} |
| 920 | 920 |
e.id = -1; |
| 921 | 921 |
} |
| 922 | 922 |
|
| 923 | 923 |
void next(Edge& e) const {
|
| 924 | 924 |
int n = arcs[e.id * 2].target; |
| 925 | 925 |
e.id = arcs[(e.id * 2) | 1].next_out; |
| 926 | 926 |
while ((e.id & 1) != 1) {
|
| 927 | 927 |
e.id = arcs[e.id].next_out; |
| 928 | 928 |
} |
| 929 | 929 |
if (e.id != -1) {
|
| 930 | 930 |
e.id /= 2; |
| 931 | 931 |
return; |
| 932 | 932 |
} |
| 933 | 933 |
n = nodes[n].next; |
| 934 | 934 |
while (n != -1) {
|
| 935 | 935 |
e.id = nodes[n].first_out; |
| 936 | 936 |
while ((e.id & 1) != 1) {
|
| 937 | 937 |
e.id = arcs[e.id].next_out; |
| 938 | 938 |
} |
| 939 | 939 |
if (e.id != -1) {
|
| 940 | 940 |
e.id /= 2; |
| 941 | 941 |
return; |
| 942 | 942 |
} |
| 943 | 943 |
n = nodes[n].next; |
| 944 | 944 |
} |
| 945 | 945 |
e.id = -1; |
| 946 | 946 |
} |
| 947 | 947 |
|
| 948 | 948 |
void firstOut(Arc &e, const Node& v) const {
|
| 949 | 949 |
e.id = nodes[v.id].first_out; |
| 950 | 950 |
} |
| 951 | 951 |
void nextOut(Arc &e) const {
|
| 952 | 952 |
e.id = arcs[e.id].next_out; |
| 953 | 953 |
} |
| 954 | 954 |
|
| 955 | 955 |
void firstIn(Arc &e, const Node& v) const {
|
| 956 | 956 |
e.id = ((nodes[v.id].first_out) ^ 1); |
| 957 | 957 |
if (e.id == -2) e.id = -1; |
| 958 | 958 |
} |
| 959 | 959 |
void nextIn(Arc &e) const {
|
| 960 | 960 |
e.id = ((arcs[e.id ^ 1].next_out) ^ 1); |
| 961 | 961 |
if (e.id == -2) e.id = -1; |
| 962 | 962 |
} |
| 963 | 963 |
|
| 964 | 964 |
void firstInc(Edge &e, bool& d, const Node& v) const {
|
| 965 | 965 |
int a = nodes[v.id].first_out; |
| 966 | 966 |
if (a != -1 ) {
|
| 967 | 967 |
e.id = a / 2; |
| 968 | 968 |
d = ((a & 1) == 1); |
| 969 | 969 |
} else {
|
| 970 | 970 |
e.id = -1; |
| 971 | 971 |
d = true; |
| 972 | 972 |
} |
| 973 | 973 |
} |
| 974 | 974 |
void nextInc(Edge &e, bool& d) const {
|
| 975 | 975 |
int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
| 976 | 976 |
if (a != -1 ) {
|
| 977 | 977 |
e.id = a / 2; |
| 978 | 978 |
d = ((a & 1) == 1); |
| 979 | 979 |
} else {
|
| 980 | 980 |
e.id = -1; |
| 981 | 981 |
d = true; |
| 982 | 982 |
} |
| 983 | 983 |
} |
| 984 | 984 |
|
| 985 | 985 |
static int id(Node v) { return v.id; }
|
| 986 | 986 |
static int id(Arc e) { return e.id; }
|
| 987 | 987 |
static int id(Edge e) { return e.id; }
|
| 988 | 988 |
|
| 989 | 989 |
static Node nodeFromId(int id) { return Node(id);}
|
| 990 | 990 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 991 | 991 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 992 | 992 |
|
| 993 | 993 |
bool valid(Node n) const {
|
| 994 | 994 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 995 | 995 |
nodes[n.id].prev != -2; |
| 996 | 996 |
} |
| 997 | 997 |
|
| 998 | 998 |
bool valid(Arc a) const {
|
| 999 | 999 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 1000 | 1000 |
arcs[a.id].prev_out != -2; |
| 1001 | 1001 |
} |
| 1002 | 1002 |
|
| 1003 | 1003 |
bool valid(Edge e) const {
|
| 1004 | 1004 |
return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) && |
| 1005 | 1005 |
arcs[2 * e.id].prev_out != -2; |
| 1006 | 1006 |
} |
| 1007 | 1007 |
|
| 1008 | 1008 |
Node addNode() {
|
| 1009 | 1009 |
int n; |
| 1010 | 1010 |
|
| 1011 | 1011 |
if(first_free_node==-1) {
|
| 1012 | 1012 |
n = nodes.size(); |
| 1013 | 1013 |
nodes.push_back(NodeT()); |
| 1014 | 1014 |
} else {
|
| 1015 | 1015 |
n = first_free_node; |
| 1016 | 1016 |
first_free_node = nodes[n].next; |
| 1017 | 1017 |
} |
| 1018 | 1018 |
|
| 1019 | 1019 |
nodes[n].next = first_node; |
| 1020 | 1020 |
if (first_node != -1) nodes[first_node].prev = n; |
| 1021 | 1021 |
first_node = n; |
| 1022 | 1022 |
nodes[n].prev = -1; |
| 1023 | 1023 |
|
| 1024 | 1024 |
nodes[n].first_out = -1; |
| 1025 | 1025 |
|
| 1026 | 1026 |
return Node(n); |
| 1027 | 1027 |
} |
| 1028 | 1028 |
|
| 1029 | 1029 |
Edge addEdge(Node u, Node v) {
|
| 1030 | 1030 |
int n; |
| 1031 | 1031 |
|
| 1032 | 1032 |
if (first_free_arc == -1) {
|
| 1033 | 1033 |
n = arcs.size(); |
| 1034 | 1034 |
arcs.push_back(ArcT()); |
| 1035 | 1035 |
arcs.push_back(ArcT()); |
| 1036 | 1036 |
} else {
|
| 1037 | 1037 |
n = first_free_arc; |
| 1038 | 1038 |
first_free_arc = arcs[n].next_out; |
| 1039 | 1039 |
} |
| 1040 | 1040 |
|
| 1041 | 1041 |
arcs[n].target = u.id; |
| 1042 | 1042 |
arcs[n | 1].target = v.id; |
| 1043 | 1043 |
|
| 1044 | 1044 |
arcs[n].next_out = nodes[v.id].first_out; |
| 1045 | 1045 |
if (nodes[v.id].first_out != -1) {
|
| 1046 | 1046 |
arcs[nodes[v.id].first_out].prev_out = n; |
| 1047 | 1047 |
} |
| 1048 | 1048 |
arcs[n].prev_out = -1; |
| 1049 | 1049 |
nodes[v.id].first_out = n; |
| 1050 | 1050 |
|
| 1051 | 1051 |
arcs[n | 1].next_out = nodes[u.id].first_out; |
| 1052 | 1052 |
if (nodes[u.id].first_out != -1) {
|
| 1053 | 1053 |
arcs[nodes[u.id].first_out].prev_out = (n | 1); |
| 1054 | 1054 |
} |
| 1055 | 1055 |
arcs[n | 1].prev_out = -1; |
| 1056 | 1056 |
nodes[u.id].first_out = (n | 1); |
| 1057 | 1057 |
|
| 1058 | 1058 |
return Edge(n / 2); |
| 1059 | 1059 |
} |
| 1060 | 1060 |
|
| 1061 | 1061 |
void erase(const Node& node) {
|
| 1062 | 1062 |
int n = node.id; |
| 1063 | 1063 |
|
| 1064 | 1064 |
if(nodes[n].next != -1) {
|
| 1065 | 1065 |
nodes[nodes[n].next].prev = nodes[n].prev; |
| 1066 | 1066 |
} |
| 1067 | 1067 |
|
| 1068 | 1068 |
if(nodes[n].prev != -1) {
|
| 1069 | 1069 |
nodes[nodes[n].prev].next = nodes[n].next; |
| 1070 | 1070 |
} else {
|
| 1071 | 1071 |
first_node = nodes[n].next; |
| 1072 | 1072 |
} |
| 1073 | 1073 |
|
| 1074 | 1074 |
nodes[n].next = first_free_node; |
| 1075 | 1075 |
first_free_node = n; |
| 1076 | 1076 |
nodes[n].prev = -2; |
| 1077 | 1077 |
} |
| 1078 | 1078 |
|
| 1079 | 1079 |
void erase(const Edge& edge) {
|
| 1080 | 1080 |
int n = edge.id * 2; |
| 1081 | 1081 |
|
| 1082 | 1082 |
if (arcs[n].next_out != -1) {
|
| 1083 | 1083 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 1084 | 1084 |
} |
| 1085 | 1085 |
|
| 1086 | 1086 |
if (arcs[n].prev_out != -1) {
|
| 1087 | 1087 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 1088 | 1088 |
} else {
|
| 1089 | 1089 |
nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
| 1090 | 1090 |
} |
| 1091 | 1091 |
|
| 1092 | 1092 |
if (arcs[n | 1].next_out != -1) {
|
| 1093 | 1093 |
arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
| 1094 | 1094 |
} |
| 1095 | 1095 |
|
| 1096 | 1096 |
if (arcs[n | 1].prev_out != -1) {
|
| 1097 | 1097 |
arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
| 1098 | 1098 |
} else {
|
| 1099 | 1099 |
nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
| 1100 | 1100 |
} |
| 1101 | 1101 |
|
| 1102 | 1102 |
arcs[n].next_out = first_free_arc; |
| 1103 | 1103 |
first_free_arc = n; |
| 1104 | 1104 |
arcs[n].prev_out = -2; |
| 1105 | 1105 |
arcs[n | 1].prev_out = -2; |
| 1106 | 1106 |
|
| 1107 | 1107 |
} |
| 1108 | 1108 |
|
| 1109 | 1109 |
void clear() {
|
| 1110 | 1110 |
arcs.clear(); |
| 1111 | 1111 |
nodes.clear(); |
| 1112 | 1112 |
first_node = first_free_node = first_free_arc = -1; |
| 1113 | 1113 |
} |
| 1114 | 1114 |
|
| 1115 | 1115 |
protected: |
| 1116 | 1116 |
|
| 1117 | 1117 |
void changeV(Edge e, Node n) {
|
| 1118 | 1118 |
if(arcs[2 * e.id].next_out != -1) {
|
| 1119 | 1119 |
arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
| 1120 | 1120 |
} |
| 1121 | 1121 |
if(arcs[2 * e.id].prev_out != -1) {
|
| 1122 | 1122 |
arcs[arcs[2 * e.id].prev_out].next_out = |
| 1123 | 1123 |
arcs[2 * e.id].next_out; |
| 1124 | 1124 |
} else {
|
| 1125 | 1125 |
nodes[arcs[(2 * e.id) | 1].target].first_out = |
| 1126 | 1126 |
arcs[2 * e.id].next_out; |
| 1127 | 1127 |
} |
| 1128 | 1128 |
|
| 1129 | 1129 |
if (nodes[n.id].first_out != -1) {
|
| 1130 | 1130 |
arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
| 1131 | 1131 |
} |
| 1132 | 1132 |
arcs[(2 * e.id) | 1].target = n.id; |
| 1133 | 1133 |
arcs[2 * e.id].prev_out = -1; |
| 1134 | 1134 |
arcs[2 * e.id].next_out = nodes[n.id].first_out; |
| 1135 | 1135 |
nodes[n.id].first_out = 2 * e.id; |
| 1136 | 1136 |
} |
| 1137 | 1137 |
|
| 1138 | 1138 |
void changeU(Edge e, Node n) {
|
| 1139 | 1139 |
if(arcs[(2 * e.id) | 1].next_out != -1) {
|
| 1140 | 1140 |
arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
| 1141 | 1141 |
arcs[(2 * e.id) | 1].prev_out; |
| 1142 | 1142 |
} |
| 1143 | 1143 |
if(arcs[(2 * e.id) | 1].prev_out != -1) {
|
| 1144 | 1144 |
arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
| 1145 | 1145 |
arcs[(2 * e.id) | 1].next_out; |
| 1146 | 1146 |
} else {
|
| 1147 | 1147 |
nodes[arcs[2 * e.id].target].first_out = |
| 1148 | 1148 |
arcs[(2 * e.id) | 1].next_out; |
| 1149 | 1149 |
} |
| 1150 | 1150 |
|
| 1151 | 1151 |
if (nodes[n.id].first_out != -1) {
|
| 1152 | 1152 |
arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
| 1153 | 1153 |
} |
| 1154 | 1154 |
arcs[2 * e.id].target = n.id; |
| 1155 | 1155 |
arcs[(2 * e.id) | 1].prev_out = -1; |
| 1156 | 1156 |
arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
| 1157 | 1157 |
nodes[n.id].first_out = ((2 * e.id) | 1); |
| 1158 | 1158 |
} |
| 1159 | 1159 |
|
| 1160 | 1160 |
}; |
| 1161 | 1161 |
|
| 1162 | 1162 |
typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
| 1163 | 1163 |
|
| 1164 | 1164 |
|
| 1165 | 1165 |
/// \addtogroup graphs |
| 1166 | 1166 |
/// @{
|
| 1167 | 1167 |
|
| 1168 | 1168 |
///A general undirected graph structure. |
| 1169 | 1169 |
|
| 1170 | 1170 |
///\ref ListGraph is a simple and fast <em>undirected graph</em> |
| 1171 | 1171 |
///implementation based on static linked lists that are stored in |
| 1172 | 1172 |
///\c std::vector structures. |
| 1173 | 1173 |
/// |
| 1174 | 1174 |
///It conforms to the \ref concepts::Graph "Graph concept" and it |
| 1175 | 1175 |
///also provides several useful additional functionalities. |
| 1176 | 1176 |
///Most of the member functions and nested classes are documented |
| 1177 | 1177 |
///only in the concept class. |
| 1178 | 1178 |
/// |
| 1179 | 1179 |
///An important extra feature of this graph implementation is that |
| 1180 | 1180 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
| 1181 | 1181 |
/// |
| 1182 | 1182 |
///\sa concepts::Graph |
| 1183 | 1183 |
|
| 1184 | 1184 |
class ListGraph : public ExtendedListGraphBase {
|
| 1185 | 1185 |
private: |
| 1186 | 1186 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
| 1187 | 1187 |
|
| 1188 | 1188 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
| 1189 | 1189 |
/// |
| 1190 | 1190 |
ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
|
| 1191 | 1191 |
///\brief Assignment of ListGraph to another one is \e not allowed. |
| 1192 | 1192 |
///Use copyGraph() instead. |
| 1193 | 1193 |
|
| 1194 | 1194 |
///Assignment of ListGraph to another one is \e not allowed. |
| 1195 | 1195 |
///Use copyGraph() instead. |
| 1196 | 1196 |
void operator=(const ListGraph &) {}
|
| 1197 | 1197 |
public: |
| 1198 | 1198 |
/// Constructor |
| 1199 | 1199 |
|
| 1200 | 1200 |
/// Constructor. |
| 1201 | 1201 |
/// |
| 1202 | 1202 |
ListGraph() {}
|
| 1203 | 1203 |
|
| 1204 | 1204 |
typedef ExtendedListGraphBase Parent; |
| 1205 | 1205 |
|
| 1206 | 1206 |
typedef Parent::OutArcIt IncEdgeIt; |
| 1207 | 1207 |
|
| 1208 | 1208 |
/// \brief Add a new node to the graph. |
| 1209 | 1209 |
/// |
| 1210 | 1210 |
/// Add a new node to the graph. |
| 1211 | 1211 |
/// \return the new node. |
| 1212 | 1212 |
Node addNode() { return Parent::addNode(); }
|
| 1213 | 1213 |
|
| 1214 | 1214 |
/// \brief Add a new edge to the graph. |
| 1215 | 1215 |
/// |
| 1216 | 1216 |
/// Add a new edge to the graph with source node \c s |
| 1217 | 1217 |
/// and target node \c t. |
| 1218 | 1218 |
/// \return the new edge. |
| 1219 | 1219 |
Edge addEdge(const Node& s, const Node& t) {
|
| 1220 | 1220 |
return Parent::addEdge(s, t); |
| 1221 | 1221 |
} |
| 1222 | 1222 |
|
| 1223 | 1223 |
/// \brief Erase a node from the graph. |
| 1224 | 1224 |
/// |
| 1225 | 1225 |
/// Erase a node from the graph. |
| 1226 | 1226 |
/// |
| 1227 | 1227 |
void erase(const Node& n) { Parent::erase(n); }
|
| 1228 | 1228 |
|
| 1229 | 1229 |
/// \brief Erase an edge from the graph. |
| 1230 | 1230 |
/// |
| 1231 | 1231 |
/// Erase an edge from the graph. |
| 1232 | 1232 |
/// |
| 1233 | 1233 |
void erase(const Edge& e) { Parent::erase(e); }
|
| 1234 | 1234 |
/// Node validity check |
| 1235 | 1235 |
|
| 1236 | 1236 |
/// This function gives back true if the given node is valid, |
| 1237 | 1237 |
/// ie. it is a real node of the graph. |
| 1238 | 1238 |
/// |
| 1239 | 1239 |
/// \warning A Node pointing to a removed item |
| 1240 | 1240 |
/// could become valid again later if new nodes are |
| 1241 | 1241 |
/// added to the graph. |
| 1242 | 1242 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 1243 | 1243 |
/// Arc validity check |
| 1244 | 1244 |
|
| 1245 | 1245 |
/// This function gives back true if the given arc is valid, |
| 1246 | 1246 |
/// ie. it is a real arc of the graph. |
| 1247 | 1247 |
/// |
| 1248 | 1248 |
/// \warning An Arc pointing to a removed item |
| 1249 | 1249 |
/// could become valid again later if new edges are |
| 1250 | 1250 |
/// added to the graph. |
| 1251 | 1251 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 1252 | 1252 |
/// Edge validity check |
| 1253 | 1253 |
|
| 1254 | 1254 |
/// This function gives back true if the given edge is valid, |
| 1255 | 1255 |
/// ie. it is a real arc of the graph. |
| 1256 | 1256 |
/// |
| 1257 | 1257 |
/// \warning A Edge pointing to a removed item |
| 1258 | 1258 |
/// could become valid again later if new edges are |
| 1259 | 1259 |
/// added to the graph. |
| 1260 | 1260 |
bool valid(Edge e) const { return Parent::valid(e); }
|
| 1261 | 1261 |
/// \brief Change the end \c u of \c e to \c n |
| 1262 | 1262 |
/// |
| 1263 | 1263 |
/// This function changes the end \c u of \c e to node \c n. |
| 1264 | 1264 |
/// |
| 1265 | 1265 |
///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the |
| 1266 | 1266 |
///changed edge are invalidated and if the changed node is the |
| 1267 | 1267 |
///base node of an iterator then this iterator is also |
| 1268 | 1268 |
///invalidated. |
| 1269 | 1269 |
/// |
| 1270 | 1270 |
///\warning This functionality cannot be used together with the |
| 1271 | 1271 |
///Snapshot feature. |
| 1272 | 1272 |
void changeU(Edge e, Node n) {
|
| 1273 | 1273 |
Parent::changeU(e,n); |
| 1274 | 1274 |
} |
| 1275 | 1275 |
/// \brief Change the end \c v of \c e to \c n |
| 1276 | 1276 |
/// |
| 1277 | 1277 |
/// This function changes the end \c v of \c e to \c n. |
| 1278 | 1278 |
/// |
| 1279 | 1279 |
///\note The <tt>EdgeIt</tt>s referencing the changed edge remain |
| 1280 | 1280 |
///valid, however <tt>ArcIt</tt>s and if the changed node is the |
| 1281 | 1281 |
///base node of an iterator then this iterator is invalidated. |
| 1282 | 1282 |
/// |
| 1283 | 1283 |
///\warning This functionality cannot be used together with the |
| 1284 | 1284 |
///Snapshot feature. |
| 1285 | 1285 |
void changeV(Edge e, Node n) {
|
| 1286 | 1286 |
Parent::changeV(e,n); |
| 1287 | 1287 |
} |
| 1288 | 1288 |
/// \brief Contract two nodes. |
| 1289 | 1289 |
/// |
| 1290 | 1290 |
/// This function contracts two nodes. |
| 1291 | 1291 |
/// Node \p b will be removed but instead of deleting |
| 1292 | 1292 |
/// its neighboring arcs, they will be joined to \p a. |
| 1293 | 1293 |
/// The last parameter \p r controls whether to remove loops. \c true |
| 1294 | 1294 |
/// means that loops will be removed. |
| 1295 | 1295 |
/// |
| 1296 | 1296 |
/// \note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 1297 | 1297 |
/// valid. |
| 1298 | 1298 |
/// |
| 1299 | 1299 |
///\warning This functionality cannot be used together with the |
| 1300 | 1300 |
///Snapshot feature. |
| 1301 | 1301 |
void contract(Node a, Node b, bool r = true) {
|
| 1302 | 1302 |
for(IncEdgeIt e(*this, b); e!=INVALID;) {
|
| 1303 | 1303 |
IncEdgeIt f = e; ++f; |
| 1304 | 1304 |
if (r && runningNode(e) == a) {
|
| 1305 | 1305 |
erase(e); |
| 1306 | 1306 |
} else if (u(e) == b) {
|
| 1307 | 1307 |
changeU(e, a); |
| 1308 | 1308 |
} else {
|
| 1309 | 1309 |
changeV(e, a); |
| 1310 | 1310 |
} |
| 1311 | 1311 |
e = f; |
| 1312 | 1312 |
} |
| 1313 | 1313 |
erase(b); |
| 1314 | 1314 |
} |
| 1315 | 1315 |
|
| 1316 | 1316 |
|
| 1317 | 1317 |
/// \brief Class to make a snapshot of the graph and restore |
| 1318 | 1318 |
/// it later. |
| 1319 | 1319 |
/// |
| 1320 | 1320 |
/// Class to make a snapshot of the graph and restore it later. |
| 1321 | 1321 |
/// |
| 1322 | 1322 |
/// The newly added nodes and edges can be removed |
| 1323 | 1323 |
/// using the restore() function. |
| 1324 | 1324 |
/// |
| 1325 | 1325 |
/// \warning Edge and node deletions and other modifications |
| 1326 | 1326 |
/// (e.g. changing nodes of edges, contracting nodes) cannot be |
| 1327 | 1327 |
/// restored. These events invalidate the snapshot. |
| 1328 | 1328 |
class Snapshot {
|
| 1329 | 1329 |
protected: |
| 1330 | 1330 |
|
| 1331 | 1331 |
typedef Parent::NodeNotifier NodeNotifier; |
| 1332 | 1332 |
|
| 1333 | 1333 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 1334 | 1334 |
public: |
| 1335 | 1335 |
|
| 1336 | 1336 |
NodeObserverProxy(Snapshot& _snapshot) |
| 1337 | 1337 |
: snapshot(_snapshot) {}
|
| 1338 | 1338 |
|
| 1339 | 1339 |
using NodeNotifier::ObserverBase::attach; |
| 1340 | 1340 |
using NodeNotifier::ObserverBase::detach; |
| 1341 | 1341 |
using NodeNotifier::ObserverBase::attached; |
| 1342 | 1342 |
|
| 1343 | 1343 |
protected: |
| 1344 | 1344 |
|
| 1345 | 1345 |
virtual void add(const Node& node) {
|
| 1346 | 1346 |
snapshot.addNode(node); |
| 1347 | 1347 |
} |
| 1348 | 1348 |
virtual void add(const std::vector<Node>& nodes) {
|
| 1349 | 1349 |
for (int i = nodes.size() - 1; i >= 0; ++i) {
|
| 1350 | 1350 |
snapshot.addNode(nodes[i]); |
| 1351 | 1351 |
} |
| 1352 | 1352 |
} |
| 1353 | 1353 |
virtual void erase(const Node& node) {
|
| 1354 | 1354 |
snapshot.eraseNode(node); |
| 1355 | 1355 |
} |
| 1356 | 1356 |
virtual void erase(const std::vector<Node>& nodes) {
|
| 1357 | 1357 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1358 | 1358 |
snapshot.eraseNode(nodes[i]); |
| 1359 | 1359 |
} |
| 1360 | 1360 |
} |
| 1361 | 1361 |
virtual void build() {
|
| 1362 | 1362 |
Node node; |
| 1363 | 1363 |
std::vector<Node> nodes; |
| 1364 | 1364 |
for (notifier()->first(node); node != INVALID; |
| 1365 | 1365 |
notifier()->next(node)) {
|
| 1366 | 1366 |
nodes.push_back(node); |
| 1367 | 1367 |
} |
| 1368 | 1368 |
for (int i = nodes.size() - 1; i >= 0; --i) {
|
| 1369 | 1369 |
snapshot.addNode(nodes[i]); |
| 1370 | 1370 |
} |
| 1371 | 1371 |
} |
| 1372 | 1372 |
virtual void clear() {
|
| 1373 | 1373 |
Node node; |
| 1374 | 1374 |
for (notifier()->first(node); node != INVALID; |
| 1375 | 1375 |
notifier()->next(node)) {
|
| 1376 | 1376 |
snapshot.eraseNode(node); |
| 1377 | 1377 |
} |
| 1378 | 1378 |
} |
| 1379 | 1379 |
|
| 1380 | 1380 |
Snapshot& snapshot; |
| 1381 | 1381 |
}; |
| 1382 | 1382 |
|
| 1383 | 1383 |
class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
|
| 1384 | 1384 |
public: |
| 1385 | 1385 |
|
| 1386 | 1386 |
EdgeObserverProxy(Snapshot& _snapshot) |
| 1387 | 1387 |
: snapshot(_snapshot) {}
|
| 1388 | 1388 |
|
| 1389 | 1389 |
using EdgeNotifier::ObserverBase::attach; |
| 1390 | 1390 |
using EdgeNotifier::ObserverBase::detach; |
| 1391 | 1391 |
using EdgeNotifier::ObserverBase::attached; |
| 1392 | 1392 |
|
| 1393 | 1393 |
protected: |
| 1394 | 1394 |
|
| 1395 | 1395 |
virtual void add(const Edge& edge) {
|
| 1396 | 1396 |
snapshot.addEdge(edge); |
| 1397 | 1397 |
} |
| 1398 | 1398 |
virtual void add(const std::vector<Edge>& edges) {
|
| 1399 | 1399 |
for (int i = edges.size() - 1; i >= 0; ++i) {
|
| 1400 | 1400 |
snapshot.addEdge(edges[i]); |
| 1401 | 1401 |
} |
| 1402 | 1402 |
} |
| 1403 | 1403 |
virtual void erase(const Edge& edge) {
|
| 1404 | 1404 |
snapshot.eraseEdge(edge); |
| 1405 | 1405 |
} |
| 1406 | 1406 |
virtual void erase(const std::vector<Edge>& edges) {
|
| 1407 | 1407 |
for (int i = 0; i < int(edges.size()); ++i) {
|
| 1408 | 1408 |
snapshot.eraseEdge(edges[i]); |
| 1409 | 1409 |
} |
| 1410 | 1410 |
} |
| 1411 | 1411 |
virtual void build() {
|
| 1412 | 1412 |
Edge edge; |
| 1413 | 1413 |
std::vector<Edge> edges; |
| 1414 | 1414 |
for (notifier()->first(edge); edge != INVALID; |
| 1415 | 1415 |
notifier()->next(edge)) {
|
| 1416 | 1416 |
edges.push_back(edge); |
| 1417 | 1417 |
} |
| 1418 | 1418 |
for (int i = edges.size() - 1; i >= 0; --i) {
|
| 1419 | 1419 |
snapshot.addEdge(edges[i]); |
| 1420 | 1420 |
} |
| 1421 | 1421 |
} |
| 1422 | 1422 |
virtual void clear() {
|
| 1423 | 1423 |
Edge edge; |
| 1424 | 1424 |
for (notifier()->first(edge); edge != INVALID; |
| 1425 | 1425 |
notifier()->next(edge)) {
|
| 1426 | 1426 |
snapshot.eraseEdge(edge); |
| 1427 | 1427 |
} |
| 1428 | 1428 |
} |
| 1429 | 1429 |
|
| 1430 | 1430 |
Snapshot& snapshot; |
| 1431 | 1431 |
}; |
| 1432 | 1432 |
|
| 1433 | 1433 |
ListGraph *graph; |
| 1434 | 1434 |
|
| 1435 | 1435 |
NodeObserverProxy node_observer_proxy; |
| 1436 | 1436 |
EdgeObserverProxy edge_observer_proxy; |
| 1437 | 1437 |
|
| 1438 | 1438 |
std::list<Node> added_nodes; |
| 1439 | 1439 |
std::list<Edge> added_edges; |
| 1440 | 1440 |
|
| 1441 | 1441 |
|
| 1442 | 1442 |
void addNode(const Node& node) {
|
| 1443 | 1443 |
added_nodes.push_front(node); |
| 1444 | 1444 |
} |
| 1445 | 1445 |
void eraseNode(const Node& node) {
|
| 1446 | 1446 |
std::list<Node>::iterator it = |
| 1447 | 1447 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
| 1448 | 1448 |
if (it == added_nodes.end()) {
|
| 1449 | 1449 |
clear(); |
| 1450 | 1450 |
edge_observer_proxy.detach(); |
| 1451 | 1451 |
throw NodeNotifier::ImmediateDetach(); |
| 1452 | 1452 |
} else {
|
| 1453 | 1453 |
added_nodes.erase(it); |
| 1454 | 1454 |
} |
| 1455 | 1455 |
} |
| 1456 | 1456 |
|
| 1457 | 1457 |
void addEdge(const Edge& edge) {
|
| 1458 | 1458 |
added_edges.push_front(edge); |
| 1459 | 1459 |
} |
| 1460 | 1460 |
void eraseEdge(const Edge& edge) {
|
| 1461 | 1461 |
std::list<Edge>::iterator it = |
| 1462 | 1462 |
std::find(added_edges.begin(), added_edges.end(), edge); |
| 1463 | 1463 |
if (it == added_edges.end()) {
|
| 1464 | 1464 |
clear(); |
| 1465 | 1465 |
node_observer_proxy.detach(); |
| 1466 | 1466 |
throw EdgeNotifier::ImmediateDetach(); |
| 1467 | 1467 |
} else {
|
| 1468 | 1468 |
added_edges.erase(it); |
| 1469 | 1469 |
} |
| 1470 | 1470 |
} |
| 1471 | 1471 |
|
| 1472 | 1472 |
void attach(ListGraph &_graph) {
|
| 1473 | 1473 |
graph = &_graph; |
| 1474 | 1474 |
node_observer_proxy.attach(graph->notifier(Node())); |
| 1475 | 1475 |
edge_observer_proxy.attach(graph->notifier(Edge())); |
| 1476 | 1476 |
} |
| 1477 | 1477 |
|
| 1478 | 1478 |
void detach() {
|
| 1479 | 1479 |
node_observer_proxy.detach(); |
| 1480 | 1480 |
edge_observer_proxy.detach(); |
| 1481 | 1481 |
} |
| 1482 | 1482 |
|
| 1483 | 1483 |
bool attached() const {
|
| 1484 | 1484 |
return node_observer_proxy.attached(); |
| 1485 | 1485 |
} |
| 1486 | 1486 |
|
| 1487 | 1487 |
void clear() {
|
| 1488 | 1488 |
added_nodes.clear(); |
| 1489 | 1489 |
added_edges.clear(); |
| 1490 | 1490 |
} |
| 1491 | 1491 |
|
| 1492 | 1492 |
public: |
| 1493 | 1493 |
|
| 1494 | 1494 |
/// \brief Default constructor. |
| 1495 | 1495 |
/// |
| 1496 | 1496 |
/// Default constructor. |
| 1497 | 1497 |
/// To actually make a snapshot you must call save(). |
| 1498 | 1498 |
Snapshot() |
| 1499 | 1499 |
: graph(0), node_observer_proxy(*this), |
| 1500 | 1500 |
edge_observer_proxy(*this) {}
|
| 1501 | 1501 |
|
| 1502 | 1502 |
/// \brief Constructor that immediately makes a snapshot. |
| 1503 | 1503 |
/// |
| 1504 | 1504 |
/// This constructor immediately makes a snapshot of the graph. |
| 1505 | 1505 |
/// \param _graph The graph we make a snapshot of. |
| 1506 | 1506 |
Snapshot(ListGraph &_graph) |
| 1507 | 1507 |
: node_observer_proxy(*this), |
| 1508 | 1508 |
edge_observer_proxy(*this) {
|
| 1509 | 1509 |
attach(_graph); |
| 1510 | 1510 |
} |
| 1511 | 1511 |
|
| 1512 | 1512 |
/// \brief Make a snapshot. |
| 1513 | 1513 |
/// |
| 1514 | 1514 |
/// Make a snapshot of the graph. |
| 1515 | 1515 |
/// |
| 1516 | 1516 |
/// This function can be called more than once. In case of a repeated |
| 1517 | 1517 |
/// call, the previous snapshot gets lost. |
| 1518 | 1518 |
/// \param _graph The graph we make the snapshot of. |
| 1519 | 1519 |
void save(ListGraph &_graph) {
|
| 1520 | 1520 |
if (attached()) {
|
| 1521 | 1521 |
detach(); |
| 1522 | 1522 |
clear(); |
| 1523 | 1523 |
} |
| 1524 | 1524 |
attach(_graph); |
| 1525 | 1525 |
} |
| 1526 | 1526 |
|
| 1527 | 1527 |
/// \brief Undo the changes until the last snapshot. |
| 1528 | 1528 |
// |
| 1529 | 1529 |
/// Undo the changes until the last snapshot created by save(). |
| 1530 | 1530 |
void restore() {
|
| 1531 | 1531 |
detach(); |
| 1532 | 1532 |
for(std::list<Edge>::iterator it = added_edges.begin(); |
| 1533 | 1533 |
it != added_edges.end(); ++it) {
|
| 1534 | 1534 |
graph->erase(*it); |
| 1535 | 1535 |
} |
| 1536 | 1536 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
| 1537 | 1537 |
it != added_nodes.end(); ++it) {
|
| 1538 | 1538 |
graph->erase(*it); |
| 1539 | 1539 |
} |
| 1540 | 1540 |
clear(); |
| 1541 | 1541 |
} |
| 1542 | 1542 |
|
| 1543 | 1543 |
/// \brief Gives back true when the snapshot is valid. |
| 1544 | 1544 |
/// |
| 1545 | 1545 |
/// Gives back true when the snapshot is valid. |
| 1546 | 1546 |
bool valid() const {
|
| 1547 | 1547 |
return attached(); |
| 1548 | 1548 |
} |
| 1549 | 1549 |
}; |
| 1550 | 1550 |
}; |
| 1551 | 1551 |
|
| 1552 | 1552 |
/// @} |
| 1553 | 1553 |
} //namespace lemon |
| 1554 | 1554 |
|
| 1555 | 1555 |
|
| 1556 | 1556 |
#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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
///\ingroup paths |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Classes for representing paths in digraphs. |
| 22 | 22 |
/// |
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_PATH_H |
| 25 | 25 |
#define LEMON_PATH_H |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/error.h> |
| 31 | 31 |
#include <lemon/core.h> |
| 32 | 32 |
#include <lemon/concepts/path.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \addtogroup paths |
| 37 | 37 |
/// @{
|
| 38 | 38 |
|
| 39 | 39 |
|
| 40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
| 41 | 41 |
/// |
| 42 | 42 |
/// A structure for representing directed path in a digraph. |
| 43 | 43 |
/// \tparam _Digraph The digraph type in which the path is. |
| 44 | 44 |
/// |
| 45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
| 46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
| 47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
| 48 | 48 |
/// a zero length path is undefined. |
| 49 | 49 |
/// |
| 50 | 50 |
/// This implementation is a back and front insertable and erasable |
| 51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
| 52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
| 53 | 53 |
/// implementation uses two vectors for storing the front and back |
| 54 | 54 |
/// insertions. |
| 55 | 55 |
template <typename _Digraph> |
| 56 | 56 |
class Path {
|
| 57 | 57 |
public: |
| 58 | 58 |
|
| 59 | 59 |
typedef _Digraph Digraph; |
| 60 | 60 |
typedef typename Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief Default constructor |
| 63 | 63 |
/// |
| 64 | 64 |
/// Default constructor |
| 65 | 65 |
Path() {}
|
| 66 | 66 |
|
| 67 | 67 |
/// \brief Template copy constructor |
| 68 | 68 |
/// |
| 69 | 69 |
/// This constuctor initializes the path from any other path type. |
| 70 | 70 |
/// It simply makes a copy of the given path. |
| 71 | 71 |
template <typename CPath> |
| 72 | 72 |
Path(const CPath& cpath) {
|
| 73 | 73 |
pathCopy(cpath, *this); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
/// \brief Template copy assignment |
| 77 | 77 |
/// |
| 78 | 78 |
/// This operator makes a copy of a path of any other type. |
| 79 | 79 |
template <typename CPath> |
| 80 | 80 |
Path& operator=(const CPath& cpath) {
|
| 81 | 81 |
pathCopy(cpath, *this); |
| 82 | 82 |
return *this; |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
/// \brief LEMON style iterator for path arcs |
| 86 | 86 |
/// |
| 87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
| 88 | 88 |
class ArcIt {
|
| 89 | 89 |
friend class Path; |
| 90 | 90 |
public: |
| 91 | 91 |
/// \brief Default constructor |
| 92 | 92 |
ArcIt() {}
|
| 93 | 93 |
/// \brief Invalid constructor |
| 94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
| 96 | 96 |
ArcIt(const Path &_path) |
| 97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 98 | 98 |
|
| 99 | 99 |
private: |
| 100 | 100 |
|
| 101 | 101 |
ArcIt(const Path &_path, int _idx) |
| 102 | 102 |
: path(&_path), idx(_idx) {}
|
| 103 | 103 |
|
| 104 | 104 |
public: |
| 105 | 105 |
|
| 106 | 106 |
/// \brief Conversion to Arc |
| 107 | 107 |
operator const Arc&() const {
|
| 108 | 108 |
return path->nth(idx); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
/// \brief Next arc |
| 112 | 112 |
ArcIt& operator++() {
|
| 113 | 113 |
++idx; |
| 114 | 114 |
if (idx >= path->length()) idx = -1; |
| 115 | 115 |
return *this; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Comparison operator |
| 119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 120 | 120 |
/// \brief Comparison operator |
| 121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 122 | 122 |
/// \brief Comparison operator |
| 123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 124 | 124 |
|
| 125 | 125 |
private: |
| 126 | 126 |
const Path *path; |
| 127 | 127 |
int idx; |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Length of the path. |
| 131 | 131 |
int length() const { return head.size() + tail.size(); }
|
| 132 | 132 |
/// \brief Return whether the path is empty. |
| 133 | 133 |
bool empty() const { return head.empty() && tail.empty(); }
|
| 134 | 134 |
|
| 135 | 135 |
/// \brief Reset the path to an empty one. |
| 136 | 136 |
void clear() { head.clear(); tail.clear(); }
|
| 137 | 137 |
|
| 138 | 138 |
/// \brief The nth arc. |
| 139 | 139 |
/// |
| 140 | 140 |
/// \pre n is in the [0..length() - 1] range |
| 141 | 141 |
const Arc& nth(int n) const {
|
| 142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
| 143 | 143 |
*(tail.begin() + (n - head.size())); |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
/// \brief Initialize arc iterator to point to the nth arc |
| 147 | 147 |
/// |
| 148 | 148 |
/// \pre n is in the [0..length() - 1] range |
| 149 | 149 |
ArcIt nthIt(int n) const {
|
| 150 | 150 |
return ArcIt(*this, n); |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
/// \brief The first arc of the path |
| 154 | 154 |
const Arc& front() const {
|
| 155 | 155 |
return head.empty() ? tail.front() : head.back(); |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
/// \brief Add a new arc before the current path |
| 159 | 159 |
void addFront(const Arc& arc) {
|
| 160 | 160 |
head.push_back(arc); |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
/// \brief Erase the first arc of the path |
| 164 | 164 |
void eraseFront() {
|
| 165 | 165 |
if (!head.empty()) {
|
| 166 | 166 |
head.pop_back(); |
| 167 | 167 |
} else {
|
| 168 | 168 |
head.clear(); |
| 169 | 169 |
int halfsize = tail.size() / 2; |
| 170 | 170 |
head.resize(halfsize); |
| 171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
| 172 | 172 |
head.rbegin()); |
| 173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
| 174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
| 175 | 175 |
} |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
/// \brief The last arc of the path |
| 179 | 179 |
const Arc& back() const {
|
| 180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
/// \brief Add a new arc behind the current path |
| 184 | 184 |
void addBack(const Arc& arc) {
|
| 185 | 185 |
tail.push_back(arc); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Erase the last arc of the path |
| 189 | 189 |
void eraseBack() {
|
| 190 | 190 |
if (!tail.empty()) {
|
| 191 | 191 |
tail.pop_back(); |
| 192 | 192 |
} else {
|
| 193 | 193 |
int halfsize = head.size() / 2; |
| 194 | 194 |
tail.resize(halfsize); |
| 195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
| 196 | 196 |
tail.rbegin()); |
| 197 | 197 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
| 198 | 198 |
head.resize(head.size() - halfsize - 1); |
| 199 | 199 |
} |
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
typedef True BuildTag; |
| 203 | 203 |
|
| 204 | 204 |
template <typename CPath> |
| 205 | 205 |
void build(const CPath& path) {
|
| 206 | 206 |
int len = path.length(); |
| 207 | 207 |
tail.reserve(len); |
| 208 | 208 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 209 | 209 |
tail.push_back(it); |
| 210 | 210 |
} |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
template <typename CPath> |
| 214 | 214 |
void buildRev(const CPath& path) {
|
| 215 | 215 |
int len = path.length(); |
| 216 | 216 |
head.reserve(len); |
| 217 | 217 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 218 | 218 |
head.push_back(it); |
| 219 | 219 |
} |
| 220 | 220 |
} |
| 221 | 221 |
|
| 222 | 222 |
protected: |
| 223 | 223 |
typedef std::vector<Arc> Container; |
| 224 | 224 |
Container head, tail; |
| 225 | 225 |
|
| 226 | 226 |
}; |
| 227 | 227 |
|
| 228 | 228 |
/// \brief A structure for representing directed paths in a digraph. |
| 229 | 229 |
/// |
| 230 | 230 |
/// A structure for representing directed path in a digraph. |
| 231 | 231 |
/// \tparam _Digraph The digraph type in which the path is. |
| 232 | 232 |
/// |
| 233 | 233 |
/// In a sense, the path can be treated as a list of arcs. The |
| 234 | 234 |
/// lemon path type stores just this list. As a consequence it |
| 235 | 235 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 236 | 236 |
/// cannot store the source. |
| 237 | 237 |
/// |
| 238 | 238 |
/// This implementation is a just back insertable and erasable path |
| 239 | 239 |
/// type. It can be indexed in O(1) time. The back insertion and |
| 240 | 240 |
/// erasure is amortized O(1) time. This implementation is faster |
| 241 | 241 |
/// then the \c Path type because it use just one vector for the |
| 242 | 242 |
/// arcs. |
| 243 | 243 |
template <typename _Digraph> |
| 244 | 244 |
class SimplePath {
|
| 245 | 245 |
public: |
| 246 | 246 |
|
| 247 | 247 |
typedef _Digraph Digraph; |
| 248 | 248 |
typedef typename Digraph::Arc Arc; |
| 249 | 249 |
|
| 250 | 250 |
/// \brief Default constructor |
| 251 | 251 |
/// |
| 252 | 252 |
/// Default constructor |
| 253 | 253 |
SimplePath() {}
|
| 254 | 254 |
|
| 255 | 255 |
/// \brief Template copy constructor |
| 256 | 256 |
/// |
| 257 | 257 |
/// This path can be initialized with any other path type. It just |
| 258 | 258 |
/// makes a copy of the given path. |
| 259 | 259 |
template <typename CPath> |
| 260 | 260 |
SimplePath(const CPath& cpath) {
|
| 261 | 261 |
pathCopy(cpath, *this); |
| 262 | 262 |
} |
| 263 | 263 |
|
| 264 | 264 |
/// \brief Template copy assignment |
| 265 | 265 |
/// |
| 266 | 266 |
/// This path can be initialized with any other path type. It just |
| 267 | 267 |
/// makes a copy of the given path. |
| 268 | 268 |
template <typename CPath> |
| 269 | 269 |
SimplePath& operator=(const CPath& cpath) {
|
| 270 | 270 |
pathCopy(cpath, *this); |
| 271 | 271 |
return *this; |
| 272 | 272 |
} |
| 273 | 273 |
|
| 274 | 274 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 275 | 275 |
/// |
| 276 | 276 |
/// This class is used to iterate on the arcs of the paths |
| 277 | 277 |
/// |
| 278 | 278 |
/// Of course it converts to Digraph::Arc |
| 279 | 279 |
class ArcIt {
|
| 280 | 280 |
friend class SimplePath; |
| 281 | 281 |
public: |
| 282 | 282 |
/// Default constructor |
| 283 | 283 |
ArcIt() {}
|
| 284 | 284 |
/// Invalid constructor |
| 285 | 285 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 286 | 286 |
/// \brief Initializate the constructor to the first arc of path |
| 287 | 287 |
ArcIt(const SimplePath &_path) |
| 288 | 288 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 289 | 289 |
|
| 290 | 290 |
private: |
| 291 | 291 |
|
| 292 | 292 |
/// Constructor with starting point |
| 293 | 293 |
ArcIt(const SimplePath &_path, int _idx) |
| 294 | 294 |
: idx(_idx), path(&_path) {}
|
| 295 | 295 |
|
| 296 | 296 |
public: |
| 297 | 297 |
|
| 298 | 298 |
///Conversion to Digraph::Arc |
| 299 | 299 |
operator const Arc&() const {
|
| 300 | 300 |
return path->nth(idx); |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
/// Next arc |
| 304 | 304 |
ArcIt& operator++() {
|
| 305 | 305 |
++idx; |
| 306 | 306 |
if (idx >= path->length()) idx = -1; |
| 307 | 307 |
return *this; |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
/// Comparison operator |
| 311 | 311 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 312 | 312 |
/// Comparison operator |
| 313 | 313 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 314 | 314 |
/// Comparison operator |
| 315 | 315 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 316 | 316 |
|
| 317 | 317 |
private: |
| 318 | 318 |
const SimplePath *path; |
| 319 | 319 |
int idx; |
| 320 | 320 |
}; |
| 321 | 321 |
|
| 322 | 322 |
/// \brief Length of the path. |
| 323 | 323 |
int length() const { return data.size(); }
|
| 324 | 324 |
/// \brief Return true if the path is empty. |
| 325 | 325 |
bool empty() const { return data.empty(); }
|
| 326 | 326 |
|
| 327 | 327 |
/// \brief Reset the path to an empty one. |
| 328 | 328 |
void clear() { data.clear(); }
|
| 329 | 329 |
|
| 330 | 330 |
/// \brief The nth arc. |
| 331 | 331 |
/// |
| 332 | 332 |
/// \pre n is in the [0..length() - 1] range |
| 333 | 333 |
const Arc& nth(int n) const {
|
| 334 | 334 |
return data[n]; |
| 335 | 335 |
} |
| 336 | 336 |
|
| 337 | 337 |
/// \brief Initializes arc iterator to point to the nth arc. |
| 338 | 338 |
ArcIt nthIt(int n) const {
|
| 339 | 339 |
return ArcIt(*this, n); |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
/// \brief The first arc of the path. |
| 343 | 343 |
const Arc& front() const {
|
| 344 | 344 |
return data.front(); |
| 345 | 345 |
} |
| 346 | 346 |
|
| 347 | 347 |
/// \brief The last arc of the path. |
| 348 | 348 |
const Arc& back() const {
|
| 349 | 349 |
return data.back(); |
| 350 | 350 |
} |
| 351 | 351 |
|
| 352 | 352 |
/// \brief Add a new arc behind the current path. |
| 353 | 353 |
void addBack(const Arc& arc) {
|
| 354 | 354 |
data.push_back(arc); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
/// \brief Erase the last arc of the path |
| 358 | 358 |
void eraseBack() {
|
| 359 | 359 |
data.pop_back(); |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 | 362 |
typedef True BuildTag; |
| 363 | 363 |
|
| 364 | 364 |
template <typename CPath> |
| 365 | 365 |
void build(const CPath& path) {
|
| 366 | 366 |
int len = path.length(); |
| 367 | 367 |
data.resize(len); |
| 368 | 368 |
int index = 0; |
| 369 | 369 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 370 | 370 |
data[index] = it;; |
| 371 | 371 |
++index; |
| 372 | 372 |
} |
| 373 | 373 |
} |
| 374 | 374 |
|
| 375 | 375 |
template <typename CPath> |
| 376 | 376 |
void buildRev(const CPath& path) {
|
| 377 | 377 |
int len = path.length(); |
| 378 | 378 |
data.resize(len); |
| 379 | 379 |
int index = len; |
| 380 | 380 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 381 | 381 |
--index; |
| 382 | 382 |
data[index] = it;; |
| 383 | 383 |
} |
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 | 386 |
protected: |
| 387 | 387 |
typedef std::vector<Arc> Container; |
| 388 | 388 |
Container data; |
| 389 | 389 |
|
| 390 | 390 |
}; |
| 391 | 391 |
|
| 392 | 392 |
/// \brief A structure for representing directed paths in a digraph. |
| 393 | 393 |
/// |
| 394 | 394 |
/// A structure for representing directed path in a digraph. |
| 395 | 395 |
/// \tparam _Digraph The digraph type in which the path is. |
| 396 | 396 |
/// |
| 397 | 397 |
/// In a sense, the path can be treated as a list of arcs. The |
| 398 | 398 |
/// lemon path type stores just this list. As a consequence it |
| 399 | 399 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 400 | 400 |
/// cannot store the source. |
| 401 | 401 |
/// |
| 402 | 402 |
/// This implementation is a back and front insertable and erasable |
| 403 | 403 |
/// path type. It can be indexed in O(k) time, where k is the rank |
| 404 | 404 |
/// of the arc in the path. The length can be computed in O(n) |
| 405 | 405 |
/// time. The front and back insertion and erasure is O(1) time |
| 406 | 406 |
/// and it can be splited and spliced in O(1) time. |
| 407 | 407 |
template <typename _Digraph> |
| 408 | 408 |
class ListPath {
|
| 409 | 409 |
public: |
| 410 | 410 |
|
| 411 | 411 |
typedef _Digraph Digraph; |
| 412 | 412 |
typedef typename Digraph::Arc Arc; |
| 413 | 413 |
|
| 414 | 414 |
protected: |
| 415 | 415 |
|
| 416 | 416 |
// the std::list<> is incompatible |
| 417 | 417 |
// hard to create invalid iterator |
| 418 | 418 |
struct Node {
|
| 419 | 419 |
Arc arc; |
| 420 | 420 |
Node *next, *prev; |
| 421 | 421 |
}; |
| 422 | 422 |
|
| 423 | 423 |
Node *first, *last; |
| 424 | 424 |
|
| 425 | 425 |
std::allocator<Node> alloc; |
| 426 | 426 |
|
| 427 | 427 |
public: |
| 428 | 428 |
|
| 429 | 429 |
/// \brief Default constructor |
| 430 | 430 |
/// |
| 431 | 431 |
/// Default constructor |
| 432 | 432 |
ListPath() : first(0), last(0) {}
|
| 433 | 433 |
|
| 434 | 434 |
/// \brief Template copy constructor |
| 435 | 435 |
/// |
| 436 | 436 |
/// This path can be initialized with any other path type. It just |
| 437 | 437 |
/// makes a copy of the given path. |
| 438 | 438 |
template <typename CPath> |
| 439 | 439 |
ListPath(const CPath& cpath) : first(0), last(0) {
|
| 440 | 440 |
pathCopy(cpath, *this); |
| 441 | 441 |
} |
| 442 | 442 |
|
| 443 | 443 |
/// \brief Destructor of the path |
| 444 | 444 |
/// |
| 445 | 445 |
/// Destructor of the path |
| 446 | 446 |
~ListPath() {
|
| 447 | 447 |
clear(); |
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
/// \brief Template copy assignment |
| 451 | 451 |
/// |
| 452 | 452 |
/// This path can be initialized with any other path type. It just |
| 453 | 453 |
/// makes a copy of the given path. |
| 454 | 454 |
template <typename CPath> |
| 455 | 455 |
ListPath& operator=(const CPath& cpath) {
|
| 456 | 456 |
pathCopy(cpath, *this); |
| 457 | 457 |
return *this; |
| 458 | 458 |
} |
| 459 | 459 |
|
| 460 | 460 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 461 | 461 |
/// |
| 462 | 462 |
/// This class is used to iterate on the arcs of the paths |
| 463 | 463 |
/// |
| 464 | 464 |
/// Of course it converts to Digraph::Arc |
| 465 | 465 |
class ArcIt {
|
| 466 | 466 |
friend class ListPath; |
| 467 | 467 |
public: |
| 468 | 468 |
/// Default constructor |
| 469 | 469 |
ArcIt() {}
|
| 470 | 470 |
/// Invalid constructor |
| 471 | 471 |
ArcIt(Invalid) : path(0), node(0) {}
|
| 472 | 472 |
/// \brief Initializate the constructor to the first arc of path |
| 473 | 473 |
ArcIt(const ListPath &_path) |
| 474 | 474 |
: path(&_path), node(_path.first) {}
|
| 475 | 475 |
|
| 476 | 476 |
protected: |
| 477 | 477 |
|
| 478 | 478 |
ArcIt(const ListPath &_path, Node *_node) |
| 479 | 479 |
: path(&_path), node(_node) {}
|
| 480 | 480 |
|
| 481 | 481 |
|
| 482 | 482 |
public: |
| 483 | 483 |
|
| 484 | 484 |
///Conversion to Digraph::Arc |
| 485 | 485 |
operator const Arc&() const {
|
| 486 | 486 |
return node->arc; |
| 487 | 487 |
} |
| 488 | 488 |
|
| 489 | 489 |
/// Next arc |
| 490 | 490 |
ArcIt& operator++() {
|
| 491 | 491 |
node = node->next; |
| 492 | 492 |
return *this; |
| 493 | 493 |
} |
| 494 | 494 |
|
| 495 | 495 |
/// Comparison operator |
| 496 | 496 |
bool operator==(const ArcIt& e) const { return node==e.node; }
|
| 497 | 497 |
/// Comparison operator |
| 498 | 498 |
bool operator!=(const ArcIt& e) const { return node!=e.node; }
|
| 499 | 499 |
/// Comparison operator |
| 500 | 500 |
bool operator<(const ArcIt& e) const { return node<e.node; }
|
| 501 | 501 |
|
| 502 | 502 |
private: |
| 503 | 503 |
const ListPath *path; |
| 504 | 504 |
Node *node; |
| 505 | 505 |
}; |
| 506 | 506 |
|
| 507 | 507 |
/// \brief The nth arc. |
| 508 | 508 |
/// |
| 509 | 509 |
/// This function looks for the nth arc in O(n) time. |
| 510 | 510 |
/// \pre n is in the [0..length() - 1] range |
| 511 | 511 |
const Arc& nth(int n) const {
|
| 512 | 512 |
Node *node = first; |
| 513 | 513 |
for (int i = 0; i < n; ++i) {
|
| 514 | 514 |
node = node->next; |
| 515 | 515 |
} |
| 516 | 516 |
return node->arc; |
| 517 | 517 |
} |
| 518 | 518 |
|
| 519 | 519 |
/// \brief Initializes arc iterator to point to the nth arc. |
| 520 | 520 |
ArcIt nthIt(int n) const {
|
| 521 | 521 |
Node *node = first; |
| 522 | 522 |
for (int i = 0; i < n; ++i) {
|
| 523 | 523 |
node = node->next; |
| 524 | 524 |
} |
| 525 | 525 |
return ArcIt(*this, node); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
/// \brief Length of the path. |
| 529 | 529 |
int length() const {
|
| 530 | 530 |
int len = 0; |
| 531 | 531 |
Node *node = first; |
| 532 | 532 |
while (node != 0) {
|
| 533 | 533 |
node = node->next; |
| 534 | 534 |
++len; |
| 535 | 535 |
} |
| 536 | 536 |
return len; |
| 537 | 537 |
} |
| 538 | 538 |
|
| 539 | 539 |
/// \brief Return true if the path is empty. |
| 540 | 540 |
bool empty() const { return first == 0; }
|
| 541 | 541 |
|
| 542 | 542 |
/// \brief Reset the path to an empty one. |
| 543 | 543 |
void clear() {
|
| 544 | 544 |
while (first != 0) {
|
| 545 | 545 |
last = first->next; |
| 546 | 546 |
alloc.destroy(first); |
| 547 | 547 |
alloc.deallocate(first, 1); |
| 548 | 548 |
first = last; |
| 549 | 549 |
} |
| 550 | 550 |
} |
| 551 | 551 |
|
| 552 | 552 |
/// \brief The first arc of the path |
| 553 | 553 |
const Arc& front() const {
|
| 554 | 554 |
return first->arc; |
| 555 | 555 |
} |
| 556 | 556 |
|
| 557 | 557 |
/// \brief Add a new arc before the current path |
| 558 | 558 |
void addFront(const Arc& arc) {
|
| 559 | 559 |
Node *node = alloc.allocate(1); |
| 560 | 560 |
alloc.construct(node, Node()); |
| 561 | 561 |
node->prev = 0; |
| 562 | 562 |
node->next = first; |
| 563 | 563 |
node->arc = arc; |
| 564 | 564 |
if (first) {
|
| 565 | 565 |
first->prev = node; |
| 566 | 566 |
first = node; |
| 567 | 567 |
} else {
|
| 568 | 568 |
first = last = node; |
| 569 | 569 |
} |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 | 572 |
/// \brief Erase the first arc of the path |
| 573 | 573 |
void eraseFront() {
|
| 574 | 574 |
Node *node = first; |
| 575 | 575 |
first = first->next; |
| 576 | 576 |
if (first) {
|
| 577 | 577 |
first->prev = 0; |
| 578 | 578 |
} else {
|
| 579 | 579 |
last = 0; |
| 580 | 580 |
} |
| 581 | 581 |
alloc.destroy(node); |
| 582 | 582 |
alloc.deallocate(node, 1); |
| 583 | 583 |
} |
| 584 | 584 |
|
| 585 | 585 |
/// \brief The last arc of the path. |
| 586 | 586 |
const Arc& back() const {
|
| 587 | 587 |
return last->arc; |
| 588 | 588 |
} |
| 589 | 589 |
|
| 590 | 590 |
/// \brief Add a new arc behind the current path. |
| 591 | 591 |
void addBack(const Arc& arc) {
|
| 592 | 592 |
Node *node = alloc.allocate(1); |
| 593 | 593 |
alloc.construct(node, Node()); |
| 594 | 594 |
node->next = 0; |
| 595 | 595 |
node->prev = last; |
| 596 | 596 |
node->arc = arc; |
| 597 | 597 |
if (last) {
|
| 598 | 598 |
last->next = node; |
| 599 | 599 |
last = node; |
| 600 | 600 |
} else {
|
| 601 | 601 |
last = first = node; |
| 602 | 602 |
} |
| 603 | 603 |
} |
| 604 | 604 |
|
| 605 | 605 |
/// \brief Erase the last arc of the path |
| 606 | 606 |
void eraseBack() {
|
| 607 | 607 |
Node *node = last; |
| 608 | 608 |
last = last->prev; |
| 609 | 609 |
if (last) {
|
| 610 | 610 |
last->next = 0; |
| 611 | 611 |
} else {
|
| 612 | 612 |
first = 0; |
| 613 | 613 |
} |
| 614 | 614 |
alloc.destroy(node); |
| 615 | 615 |
alloc.deallocate(node, 1); |
| 616 | 616 |
} |
| 617 | 617 |
|
| 618 | 618 |
/// \brief Splice a path to the back of the current path. |
| 619 | 619 |
/// |
| 620 | 620 |
/// It splices \c tpath to the back of the current path and \c |
| 621 | 621 |
/// tpath becomes empty. The time complexity of this function is |
| 622 | 622 |
/// O(1). |
| 623 | 623 |
void spliceBack(ListPath& tpath) {
|
| 624 | 624 |
if (first) {
|
| 625 | 625 |
if (tpath.first) {
|
| 626 | 626 |
last->next = tpath.first; |
| 627 | 627 |
tpath.first->prev = last; |
| 628 | 628 |
last = tpath.last; |
| 629 | 629 |
} |
| 630 | 630 |
} else {
|
| 631 | 631 |
first = tpath.first; |
| 632 | 632 |
last = tpath.last; |
| 633 | 633 |
} |
| 634 | 634 |
tpath.first = tpath.last = 0; |
| 635 | 635 |
} |
| 636 | 636 |
|
| 637 | 637 |
/// \brief Splice a path to the front of the current path. |
| 638 | 638 |
/// |
| 639 | 639 |
/// It splices \c tpath before the current path and \c tpath |
| 640 | 640 |
/// becomes empty. The time complexity of this function |
| 641 | 641 |
/// is O(1). |
| 642 | 642 |
void spliceFront(ListPath& tpath) {
|
| 643 | 643 |
if (first) {
|
| 644 | 644 |
if (tpath.first) {
|
| 645 | 645 |
first->prev = tpath.last; |
| 646 | 646 |
tpath.last->next = first; |
| 647 | 647 |
first = tpath.first; |
| 648 | 648 |
} |
| 649 | 649 |
} else {
|
| 650 | 650 |
first = tpath.first; |
| 651 | 651 |
last = tpath.last; |
| 652 | 652 |
} |
| 653 | 653 |
tpath.first = tpath.last = 0; |
| 654 | 654 |
} |
| 655 | 655 |
|
| 656 | 656 |
/// \brief Splice a path into the current path. |
| 657 | 657 |
/// |
| 658 | 658 |
/// It splices the \c tpath into the current path before the |
| 659 | 659 |
/// position of \c it iterator and \c tpath becomes empty. The |
| 660 | 660 |
/// time complexity of this function is O(1). If the \c it is |
| 661 | 661 |
/// \c INVALID then it will splice behind the current path. |
| 662 | 662 |
void splice(ArcIt it, ListPath& tpath) {
|
| 663 | 663 |
if (it.node) {
|
| 664 | 664 |
if (tpath.first) {
|
| 665 | 665 |
tpath.first->prev = it.node->prev; |
| 666 | 666 |
if (it.node->prev) {
|
| 667 | 667 |
it.node->prev->next = tpath.first; |
| 668 | 668 |
} else {
|
| 669 | 669 |
first = tpath.first; |
| 670 | 670 |
} |
| 671 | 671 |
it.node->prev = tpath.last; |
| 672 | 672 |
tpath.last->next = it.node; |
| 673 | 673 |
} |
| 674 | 674 |
} else {
|
| 675 | 675 |
if (first) {
|
| 676 | 676 |
if (tpath.first) {
|
| 677 | 677 |
last->next = tpath.first; |
| 678 | 678 |
tpath.first->prev = last; |
| 679 | 679 |
last = tpath.last; |
| 680 | 680 |
} |
| 681 | 681 |
} else {
|
| 682 | 682 |
first = tpath.first; |
| 683 | 683 |
last = tpath.last; |
| 684 | 684 |
} |
| 685 | 685 |
} |
| 686 | 686 |
tpath.first = tpath.last = 0; |
| 687 | 687 |
} |
| 688 | 688 |
|
| 689 | 689 |
/// \brief Split the current path. |
| 690 | 690 |
/// |
| 691 | 691 |
/// It splits the current path into two parts. The part before |
| 692 | 692 |
/// the iterator \c it will remain in the current path and the part |
| 693 | 693 |
/// starting with |
| 694 | 694 |
/// \c it will put into \c tpath. If \c tpath have arcs |
| 695 | 695 |
/// before the operation they are removed first. The time |
| 696 | 696 |
/// complexity of this function is O(1) plus the the time of emtying |
| 697 | 697 |
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
| 698 | 698 |
void split(ArcIt it, ListPath& tpath) {
|
| 699 | 699 |
tpath.clear(); |
| 700 | 700 |
if (it.node) {
|
| 701 | 701 |
tpath.first = it.node; |
| 702 | 702 |
tpath.last = last; |
| 703 | 703 |
if (it.node->prev) {
|
| 704 | 704 |
last = it.node->prev; |
| 705 | 705 |
last->next = 0; |
| 706 | 706 |
} else {
|
| 707 | 707 |
first = last = 0; |
| 708 | 708 |
} |
| 709 | 709 |
it.node->prev = 0; |
| 710 | 710 |
} |
| 711 | 711 |
} |
| 712 | 712 |
|
| 713 | 713 |
|
| 714 | 714 |
typedef True BuildTag; |
| 715 | 715 |
|
| 716 | 716 |
template <typename CPath> |
| 717 | 717 |
void build(const CPath& path) {
|
| 718 | 718 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 719 | 719 |
addBack(it); |
| 720 | 720 |
} |
| 721 | 721 |
} |
| 722 | 722 |
|
| 723 | 723 |
template <typename CPath> |
| 724 | 724 |
void buildRev(const CPath& path) {
|
| 725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 726 | 726 |
addFront(it); |
| 727 | 727 |
} |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
}; |
| 731 | 731 |
|
| 732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
| 733 | 733 |
/// |
| 734 | 734 |
/// A structure for representing directed path in a digraph. |
| 735 | 735 |
/// \tparam _Digraph The digraph type in which the path is. |
| 736 | 736 |
/// |
| 737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
| 738 | 738 |
/// lemon path type stores just this list. As a consequence it |
| 739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
| 740 | 740 |
/// a zero length path is undefined. |
| 741 | 741 |
/// |
| 742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
| 743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
| 744 | 744 |
/// modified. |
| 745 | 745 |
/// |
| 746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
| 747 | 747 |
/// it is intented to be |
| 748 | 748 |
/// used when you want to store a large number of paths. |
| 749 | 749 |
template <typename _Digraph> |
| 750 | 750 |
class StaticPath {
|
| 751 | 751 |
public: |
| 752 | 752 |
|
| 753 | 753 |
typedef _Digraph Digraph; |
| 754 | 754 |
typedef typename Digraph::Arc Arc; |
| 755 | 755 |
|
| 756 | 756 |
/// \brief Default constructor |
| 757 | 757 |
/// |
| 758 | 758 |
/// Default constructor |
| 759 | 759 |
StaticPath() : len(0), arcs(0) {}
|
| 760 | 760 |
|
| 761 | 761 |
/// \brief Template copy constructor |
| 762 | 762 |
/// |
| 763 | 763 |
/// This path can be initialized from any other path type. |
| 764 | 764 |
template <typename CPath> |
| 765 | 765 |
StaticPath(const CPath& cpath) : arcs(0) {
|
| 766 | 766 |
pathCopy(cpath, *this); |
| 767 | 767 |
} |
| 768 | 768 |
|
| 769 | 769 |
/// \brief Destructor of the path |
| 770 | 770 |
/// |
| 771 | 771 |
/// Destructor of the path |
| 772 | 772 |
~StaticPath() {
|
| 773 | 773 |
if (arcs) delete[] arcs; |
| 774 | 774 |
} |
| 775 | 775 |
|
| 776 | 776 |
/// \brief Template copy assignment |
| 777 | 777 |
/// |
| 778 | 778 |
/// This path can be made equal to any other path type. It simply |
| 779 | 779 |
/// makes a copy of the given path. |
| 780 | 780 |
template <typename CPath> |
| 781 | 781 |
StaticPath& operator=(const CPath& cpath) {
|
| 782 | 782 |
pathCopy(cpath, *this); |
| 783 | 783 |
return *this; |
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
/// \brief Iterator class to iterate on the arcs of the paths |
| 787 | 787 |
/// |
| 788 | 788 |
/// This class is used to iterate on the arcs of the paths |
| 789 | 789 |
/// |
| 790 | 790 |
/// Of course it converts to Digraph::Arc |
| 791 | 791 |
class ArcIt {
|
| 792 | 792 |
friend class StaticPath; |
| 793 | 793 |
public: |
| 794 | 794 |
/// Default constructor |
| 795 | 795 |
ArcIt() {}
|
| 796 | 796 |
/// Invalid constructor |
| 797 | 797 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 798 | 798 |
/// Initializate the constructor to the first arc of path |
| 799 | 799 |
ArcIt(const StaticPath &_path) |
| 800 | 800 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 801 | 801 |
|
| 802 | 802 |
private: |
| 803 | 803 |
|
| 804 | 804 |
/// Constructor with starting point |
| 805 | 805 |
ArcIt(const StaticPath &_path, int _idx) |
| 806 | 806 |
: idx(_idx), path(&_path) {}
|
| 807 | 807 |
|
| 808 | 808 |
public: |
| 809 | 809 |
|
| 810 | 810 |
///Conversion to Digraph::Arc |
| 811 | 811 |
operator const Arc&() const {
|
| 812 | 812 |
return path->nth(idx); |
| 813 | 813 |
} |
| 814 | 814 |
|
| 815 | 815 |
/// Next arc |
| 816 | 816 |
ArcIt& operator++() {
|
| 817 | 817 |
++idx; |
| 818 | 818 |
if (idx >= path->length()) idx = -1; |
| 819 | 819 |
return *this; |
| 820 | 820 |
} |
| 821 | 821 |
|
| 822 | 822 |
/// Comparison operator |
| 823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 824 | 824 |
/// Comparison operator |
| 825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 826 | 826 |
/// Comparison operator |
| 827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 828 | 828 |
|
| 829 | 829 |
private: |
| 830 | 830 |
const StaticPath *path; |
| 831 | 831 |
int idx; |
| 832 | 832 |
}; |
| 833 | 833 |
|
| 834 | 834 |
/// \brief The nth arc. |
| 835 | 835 |
/// |
| 836 | 836 |
/// \pre n is in the [0..length() - 1] range |
| 837 | 837 |
const Arc& nth(int n) const {
|
| 838 | 838 |
return arcs[n]; |
| 839 | 839 |
} |
| 840 | 840 |
|
| 841 | 841 |
/// \brief The arc iterator pointing to the nth arc. |
| 842 | 842 |
ArcIt nthIt(int n) const {
|
| 843 | 843 |
return ArcIt(*this, n); |
| 844 | 844 |
} |
| 845 | 845 |
|
| 846 | 846 |
/// \brief The length of the path. |
| 847 | 847 |
int length() const { return len; }
|
| 848 | 848 |
|
| 849 | 849 |
/// \brief Return true when the path is empty. |
| 850 | 850 |
int empty() const { return len == 0; }
|
| 851 | 851 |
|
| 852 | 852 |
/// \brief Erase all arcs in the digraph. |
| 853 | 853 |
void clear() {
|
| 854 | 854 |
len = 0; |
| 855 | 855 |
if (arcs) delete[] arcs; |
| 856 | 856 |
arcs = 0; |
| 857 | 857 |
} |
| 858 | 858 |
|
| 859 | 859 |
/// \brief The first arc of the path. |
| 860 | 860 |
const Arc& front() const {
|
| 861 | 861 |
return arcs[0]; |
| 862 | 862 |
} |
| 863 | 863 |
|
| 864 | 864 |
/// \brief The last arc of the path. |
| 865 | 865 |
const Arc& back() const {
|
| 866 | 866 |
return arcs[len - 1]; |
| 867 | 867 |
} |
| 868 | 868 |
|
| 869 | 869 |
|
| 870 | 870 |
typedef True BuildTag; |
| 871 | 871 |
|
| 872 | 872 |
template <typename CPath> |
| 873 | 873 |
void build(const CPath& path) {
|
| 874 | 874 |
len = path.length(); |
| 875 | 875 |
arcs = new Arc[len]; |
| 876 | 876 |
int index = 0; |
| 877 | 877 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
|
| 878 | 878 |
arcs[index] = it; |
| 879 | 879 |
++index; |
| 880 | 880 |
} |
| 881 | 881 |
} |
| 882 | 882 |
|
| 883 | 883 |
template <typename CPath> |
| 884 | 884 |
void buildRev(const CPath& path) {
|
| 885 | 885 |
len = path.length(); |
| 886 | 886 |
arcs = new Arc[len]; |
| 887 | 887 |
int index = len; |
| 888 | 888 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
|
| 889 | 889 |
--index; |
| 890 | 890 |
arcs[index] = it; |
| 891 | 891 |
} |
| 892 | 892 |
} |
| 893 | 893 |
|
| 894 | 894 |
private: |
| 895 | 895 |
int len; |
| 896 | 896 |
Arc* arcs; |
| 897 | 897 |
}; |
| 898 | 898 |
|
| 899 | 899 |
/////////////////////////////////////////////////////////////////////// |
| 900 | 900 |
// Additional utilities |
| 901 | 901 |
/////////////////////////////////////////////////////////////////////// |
| 902 | 902 |
|
| 903 | 903 |
namespace _path_bits {
|
| 904 | 904 |
|
| 905 | 905 |
template <typename Path, typename Enable = void> |
| 906 | 906 |
struct RevPathTagIndicator {
|
| 907 | 907 |
static const bool value = false; |
| 908 | 908 |
}; |
| 909 | 909 |
|
| 910 | 910 |
template <typename Path> |
| 911 | 911 |
struct RevPathTagIndicator< |
| 912 | 912 |
Path, |
| 913 | 913 |
typename enable_if<typename Path::RevPathTag, void>::type |
| 914 | 914 |
> {
|
| 915 | 915 |
static const bool value = true; |
| 916 | 916 |
}; |
| 917 | 917 |
|
| 918 | 918 |
template <typename Path, typename Enable = void> |
| 919 | 919 |
struct BuildTagIndicator {
|
| 920 | 920 |
static const bool value = false; |
| 921 | 921 |
}; |
| 922 | 922 |
|
| 923 | 923 |
template <typename Path> |
| 924 | 924 |
struct BuildTagIndicator< |
| 925 | 925 |
Path, |
| 926 | 926 |
typename enable_if<typename Path::BuildTag, void>::type |
| 927 | 927 |
> {
|
| 928 | 928 |
static const bool value = true; |
| 929 | 929 |
}; |
| 930 | 930 |
|
| 931 | 931 |
template <typename From, typename To, |
| 932 | 932 |
bool buildEnable = BuildTagIndicator<To>::value> |
| 933 | 933 |
struct PathCopySelectorForward {
|
| 934 | 934 |
static void copy(const From& from, To& to) {
|
| 935 | 935 |
to.clear(); |
| 936 | 936 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 937 | 937 |
to.addBack(it); |
| 938 | 938 |
} |
| 939 | 939 |
} |
| 940 | 940 |
}; |
| 941 | 941 |
|
| 942 | 942 |
template <typename From, typename To> |
| 943 | 943 |
struct PathCopySelectorForward<From, To, true> {
|
| 944 | 944 |
static void copy(const From& from, To& to) {
|
| 945 | 945 |
to.clear(); |
| 946 | 946 |
to.build(from); |
| 947 | 947 |
} |
| 948 | 948 |
}; |
| 949 | 949 |
|
| 950 | 950 |
template <typename From, typename To, |
| 951 | 951 |
bool buildEnable = BuildTagIndicator<To>::value> |
| 952 | 952 |
struct PathCopySelectorBackward {
|
| 953 | 953 |
static void copy(const From& from, To& to) {
|
| 954 | 954 |
to.clear(); |
| 955 | 955 |
for (typename From::RevArcIt it(from); it != INVALID; ++it) {
|
| 956 | 956 |
to.addFront(it); |
| 957 | 957 |
} |
| 958 | 958 |
} |
| 959 | 959 |
}; |
| 960 | 960 |
|
| 961 | 961 |
template <typename From, typename To> |
| 962 | 962 |
struct PathCopySelectorBackward<From, To, true> {
|
| 963 | 963 |
static void copy(const From& from, To& to) {
|
| 964 | 964 |
to.clear(); |
| 965 | 965 |
to.buildRev(from); |
| 966 | 966 |
} |
| 967 | 967 |
}; |
| 968 | 968 |
|
| 969 |
|
|
| 969 |
|
|
| 970 | 970 |
template <typename From, typename To, |
| 971 | 971 |
bool revEnable = RevPathTagIndicator<From>::value> |
| 972 | 972 |
struct PathCopySelector {
|
| 973 | 973 |
static void copy(const From& from, To& to) {
|
| 974 | 974 |
PathCopySelectorForward<From, To>::copy(from, to); |
| 975 |
} |
|
| 975 |
} |
|
| 976 | 976 |
}; |
| 977 | 977 |
|
| 978 | 978 |
template <typename From, typename To> |
| 979 | 979 |
struct PathCopySelector<From, To, true> {
|
| 980 | 980 |
static void copy(const From& from, To& to) {
|
| 981 | 981 |
PathCopySelectorBackward<From, To>::copy(from, to); |
| 982 |
} |
|
| 982 |
} |
|
| 983 | 983 |
}; |
| 984 | 984 |
|
| 985 | 985 |
} |
| 986 | 986 |
|
| 987 | 987 |
|
| 988 | 988 |
/// \brief Make a copy of a path. |
| 989 | 989 |
/// |
| 990 | 990 |
/// This function makes a copy of a path. |
| 991 | 991 |
template <typename From, typename To> |
| 992 | 992 |
void pathCopy(const From& from, To& to) {
|
| 993 | 993 |
checkConcept<concepts::PathDumper<typename From::Digraph>, From>(); |
| 994 | 994 |
_path_bits::PathCopySelector<From, To>::copy(from, to); |
| 995 | 995 |
} |
| 996 | 996 |
|
| 997 | 997 |
/// \brief Deprecated version of \ref pathCopy(). |
| 998 | 998 |
/// |
| 999 | 999 |
/// Deprecated version of \ref pathCopy() (only for reverse compatibility). |
| 1000 | 1000 |
template <typename To, typename From> |
| 1001 | 1001 |
void copyPath(To& to, const From& from) {
|
| 1002 | 1002 |
pathCopy(from, to); |
| 1003 | 1003 |
} |
| 1004 | 1004 |
|
| 1005 | 1005 |
/// \brief Check the consistency of a path. |
| 1006 | 1006 |
/// |
| 1007 | 1007 |
/// This function checks that the target of each arc is the same |
| 1008 | 1008 |
/// as the source of the next one. |
| 1009 | 1009 |
/// |
| 1010 | 1010 |
template <typename Digraph, typename Path> |
| 1011 | 1011 |
bool checkPath(const Digraph& digraph, const Path& path) {
|
| 1012 | 1012 |
typename Path::ArcIt it(path); |
| 1013 | 1013 |
if (it == INVALID) return true; |
| 1014 | 1014 |
typename Digraph::Node node = digraph.target(it); |
| 1015 | 1015 |
++it; |
| 1016 | 1016 |
while (it != INVALID) {
|
| 1017 | 1017 |
if (digraph.source(it) != node) return false; |
| 1018 | 1018 |
node = digraph.target(it); |
| 1019 | 1019 |
++it; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
return true; |
| 1022 | 1022 |
} |
| 1023 | 1023 |
|
| 1024 | 1024 |
/// \brief The source of a path |
| 1025 | 1025 |
/// |
| 1026 | 1026 |
/// This function returns the source node of the given path. |
| 1027 | 1027 |
/// If the path is empty, then it returns \c INVALID. |
| 1028 | 1028 |
template <typename Digraph, typename Path> |
| 1029 | 1029 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
|
| 1030 | 1030 |
return path.empty() ? INVALID : digraph.source(path.front()); |
| 1031 | 1031 |
} |
| 1032 | 1032 |
|
| 1033 | 1033 |
/// \brief The target of a path |
| 1034 | 1034 |
/// |
| 1035 | 1035 |
/// This function returns the target node of the given path. |
| 1036 | 1036 |
/// If the path is empty, then it returns \c INVALID. |
| 1037 | 1037 |
template <typename Digraph, typename Path> |
| 1038 | 1038 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
|
| 1039 | 1039 |
return path.empty() ? INVALID : digraph.target(path.back()); |
| 1040 | 1040 |
} |
| 1041 | 1041 |
|
| 1042 | 1042 |
/// \brief Class which helps to iterate through the nodes of a path |
| 1043 | 1043 |
/// |
| 1044 | 1044 |
/// In a sense, the path can be treated as a list of arcs. The |
| 1045 | 1045 |
/// lemon path type stores only this list. As a consequence, it |
| 1046 | 1046 |
/// cannot enumerate the nodes in the path and the zero length paths |
| 1047 | 1047 |
/// cannot have a source node. |
| 1048 | 1048 |
/// |
| 1049 | 1049 |
/// This class implements the node iterator of a path structure. To |
| 1050 | 1050 |
/// provide this feature, the underlying digraph should be passed to |
| 1051 | 1051 |
/// the constructor of the iterator. |
| 1052 | 1052 |
template <typename Path> |
| 1053 | 1053 |
class PathNodeIt {
|
| 1054 | 1054 |
private: |
| 1055 | 1055 |
const typename Path::Digraph *_digraph; |
| 1056 | 1056 |
typename Path::ArcIt _it; |
| 1057 | 1057 |
typename Path::Digraph::Node _nd; |
| 1058 | 1058 |
|
| 1059 | 1059 |
public: |
| 1060 | 1060 |
|
| 1061 | 1061 |
typedef typename Path::Digraph Digraph; |
| 1062 | 1062 |
typedef typename Digraph::Node Node; |
| 1063 | 1063 |
|
| 1064 | 1064 |
/// Default constructor |
| 1065 | 1065 |
PathNodeIt() {}
|
| 1066 | 1066 |
/// Invalid constructor |
| 1067 | 1067 |
PathNodeIt(Invalid) |
| 1068 | 1068 |
: _digraph(0), _it(INVALID), _nd(INVALID) {}
|
| 1069 | 1069 |
/// Constructor |
| 1070 | 1070 |
PathNodeIt(const Digraph& digraph, const Path& path) |
| 1071 | 1071 |
: _digraph(&digraph), _it(path) {
|
| 1072 | 1072 |
_nd = (_it != INVALID ? _digraph->source(_it) : INVALID); |
| 1073 | 1073 |
} |
| 1074 | 1074 |
/// Constructor |
| 1075 | 1075 |
PathNodeIt(const Digraph& digraph, const Path& path, const Node& src) |
| 1076 | 1076 |
: _digraph(&digraph), _it(path), _nd(src) {}
|
| 1077 | 1077 |
|
| 1078 | 1078 |
///Conversion to Digraph::Node |
| 1079 | 1079 |
operator Node() const {
|
| 1080 | 1080 |
return _nd; |
| 1081 | 1081 |
} |
| 1082 | 1082 |
|
| 1083 | 1083 |
/// Next node |
| 1084 | 1084 |
PathNodeIt& operator++() {
|
| 1085 | 1085 |
if (_it == INVALID) _nd = INVALID; |
| 1086 | 1086 |
else {
|
| 1087 | 1087 |
_nd = _digraph->target(_it); |
| 1088 | 1088 |
++_it; |
| 1089 | 1089 |
} |
| 1090 | 1090 |
return *this; |
| 1091 | 1091 |
} |
| 1092 | 1092 |
|
| 1093 | 1093 |
/// Comparison operator |
| 1094 | 1094 |
bool operator==(const PathNodeIt& n) const {
|
| 1095 | 1095 |
return _it == n._it && _nd == n._nd; |
| 1096 | 1096 |
} |
| 1097 | 1097 |
/// Comparison operator |
| 1098 | 1098 |
bool operator!=(const PathNodeIt& n) const {
|
| 1099 | 1099 |
return _it != n._it || _nd != n._nd; |
| 1100 | 1100 |
} |
| 1101 | 1101 |
/// Comparison operator |
| 1102 | 1102 |
bool operator<(const PathNodeIt& n) const {
|
| 1103 | 1103 |
return (_it < n._it && _nd != INVALID); |
| 1104 | 1104 |
} |
| 1105 | 1105 |
|
| 1106 | 1106 |
}; |
| 1107 | 1107 |
|
| 1108 | 1108 |
///@} |
| 1109 | 1109 |
|
| 1110 | 1110 |
} // namespace lemon |
| 1111 | 1111 |
|
| 1112 | 1112 |
#endif // LEMON_PATH_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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
* This file contains the reimplemented version of the Mersenne Twister |
| 21 | 21 |
* Generator of Matsumoto and Nishimura. |
| 22 | 22 |
* |
| 23 | 23 |
* See the appropriate copyright notice below. |
| 24 | 24 |
* |
| 25 | 25 |
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
| 26 | 26 |
* All rights reserved. |
| 27 | 27 |
* |
| 28 | 28 |
* Redistribution and use in source and binary forms, with or without |
| 29 | 29 |
* modification, are permitted provided that the following conditions |
| 30 | 30 |
* are met: |
| 31 | 31 |
* |
| 32 | 32 |
* 1. Redistributions of source code must retain the above copyright |
| 33 | 33 |
* notice, this list of conditions and the following disclaimer. |
| 34 | 34 |
* |
| 35 | 35 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 36 | 36 |
* notice, this list of conditions and the following disclaimer in the |
| 37 | 37 |
* documentation and/or other materials provided with the distribution. |
| 38 | 38 |
* |
| 39 | 39 |
* 3. The names of its contributors may not be used to endorse or promote |
| 40 | 40 |
* products derived from this software without specific prior written |
| 41 | 41 |
* permission. |
| 42 | 42 |
* |
| 43 | 43 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 44 | 44 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 45 | 45 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 46 | 46 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 47 | 47 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 48 | 48 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 49 | 49 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 50 | 50 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 51 | 51 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 52 | 52 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 53 | 53 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 54 | 54 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 55 | 55 |
* |
| 56 | 56 |
* |
| 57 | 57 |
* Any feedback is very welcome. |
| 58 | 58 |
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
| 59 | 59 |
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
| 60 | 60 |
*/ |
| 61 | 61 |
|
| 62 | 62 |
#ifndef LEMON_RANDOM_H |
| 63 | 63 |
#define LEMON_RANDOM_H |
| 64 | 64 |
|
| 65 | 65 |
#include <algorithm> |
| 66 | 66 |
#include <iterator> |
| 67 | 67 |
#include <vector> |
| 68 | 68 |
#include <limits> |
| 69 | 69 |
#include <fstream> |
| 70 | 70 |
|
| 71 | 71 |
#include <lemon/math.h> |
| 72 | 72 |
#include <lemon/dim2.h> |
| 73 | 73 |
|
| 74 | 74 |
#ifndef WIN32 |
| 75 | 75 |
#include <sys/time.h> |
| 76 | 76 |
#include <ctime> |
| 77 | 77 |
#include <sys/types.h> |
| 78 | 78 |
#include <unistd.h> |
| 79 | 79 |
#else |
| 80 | 80 |
#include <lemon/bits/windows.h> |
| 81 | 81 |
#endif |
| 82 | 82 |
|
| 83 | 83 |
///\ingroup misc |
| 84 | 84 |
///\file |
| 85 | 85 |
///\brief Mersenne Twister random number generator |
| 86 | 86 |
|
| 87 | 87 |
namespace lemon {
|
| 88 | 88 |
|
| 89 | 89 |
namespace _random_bits {
|
| 90 | 90 |
|
| 91 | 91 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits> |
| 92 | 92 |
struct RandomTraits {};
|
| 93 | 93 |
|
| 94 | 94 |
template <typename _Word> |
| 95 | 95 |
struct RandomTraits<_Word, 32> {
|
| 96 | 96 |
|
| 97 | 97 |
typedef _Word Word; |
| 98 | 98 |
static const int bits = 32; |
| 99 | 99 |
|
| 100 | 100 |
static const int length = 624; |
| 101 | 101 |
static const int shift = 397; |
| 102 | 102 |
|
| 103 | 103 |
static const Word mul = 0x6c078965u; |
| 104 | 104 |
static const Word arrayInit = 0x012BD6AAu; |
| 105 | 105 |
static const Word arrayMul1 = 0x0019660Du; |
| 106 | 106 |
static const Word arrayMul2 = 0x5D588B65u; |
| 107 | 107 |
|
| 108 | 108 |
static const Word mask = 0x9908B0DFu; |
| 109 | 109 |
static const Word loMask = (1u << 31) - 1; |
| 110 | 110 |
static const Word hiMask = ~loMask; |
| 111 | 111 |
|
| 112 | 112 |
|
| 113 | 113 |
static Word tempering(Word rnd) {
|
| 114 | 114 |
rnd ^= (rnd >> 11); |
| 115 | 115 |
rnd ^= (rnd << 7) & 0x9D2C5680u; |
| 116 | 116 |
rnd ^= (rnd << 15) & 0xEFC60000u; |
| 117 | 117 |
rnd ^= (rnd >> 18); |
| 118 | 118 |
return rnd; |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
}; |
| 122 | 122 |
|
| 123 | 123 |
template <typename _Word> |
| 124 | 124 |
struct RandomTraits<_Word, 64> {
|
| 125 | 125 |
|
| 126 | 126 |
typedef _Word Word; |
| 127 | 127 |
static const int bits = 64; |
| 128 | 128 |
|
| 129 | 129 |
static const int length = 312; |
| 130 | 130 |
static const int shift = 156; |
| 131 | 131 |
|
| 132 | 132 |
static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du); |
| 133 | 133 |
static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu); |
| 134 | 134 |
static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u); |
| 135 | 135 |
static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu); |
| 136 | 136 |
|
| 137 | 137 |
static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u); |
| 138 | 138 |
static const Word loMask = (Word(1u) << 31) - 1; |
| 139 | 139 |
static const Word hiMask = ~loMask; |
| 140 | 140 |
|
| 141 | 141 |
static Word tempering(Word rnd) {
|
| 142 | 142 |
rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u)); |
| 143 | 143 |
rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u)); |
| 144 | 144 |
rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u)); |
| 145 | 145 |
rnd ^= (rnd >> 43); |
| 146 | 146 |
return rnd; |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
}; |
| 150 | 150 |
|
| 151 | 151 |
template <typename _Word> |
| 152 | 152 |
class RandomCore {
|
| 153 | 153 |
public: |
| 154 | 154 |
|
| 155 | 155 |
typedef _Word Word; |
| 156 | 156 |
|
| 157 | 157 |
private: |
| 158 | 158 |
|
| 159 | 159 |
static const int bits = RandomTraits<Word>::bits; |
| 160 | 160 |
|
| 161 | 161 |
static const int length = RandomTraits<Word>::length; |
| 162 | 162 |
static const int shift = RandomTraits<Word>::shift; |
| 163 | 163 |
|
| 164 | 164 |
public: |
| 165 | 165 |
|
| 166 | 166 |
void initState() {
|
| 167 | 167 |
static const Word seedArray[4] = {
|
| 168 | 168 |
0x12345u, 0x23456u, 0x34567u, 0x45678u |
| 169 | 169 |
}; |
| 170 | 170 |
|
| 171 | 171 |
initState(seedArray, seedArray + 4); |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void initState(Word seed) {
|
| 175 | 175 |
|
| 176 | 176 |
static const Word mul = RandomTraits<Word>::mul; |
| 177 | 177 |
|
| 178 | 178 |
current = state; |
| 179 | 179 |
|
| 180 | 180 |
Word *curr = state + length - 1; |
| 181 | 181 |
curr[0] = seed; --curr; |
| 182 | 182 |
for (int i = 1; i < length; ++i) {
|
| 183 | 183 |
curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i); |
| 184 | 184 |
--curr; |
| 185 | 185 |
} |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
template <typename Iterator> |
| 189 | 189 |
void initState(Iterator begin, Iterator end) {
|
| 190 | 190 |
|
| 191 | 191 |
static const Word init = RandomTraits<Word>::arrayInit; |
| 192 | 192 |
static const Word mul1 = RandomTraits<Word>::arrayMul1; |
| 193 | 193 |
static const Word mul2 = RandomTraits<Word>::arrayMul2; |
| 194 | 194 |
|
| 195 | 195 |
|
| 196 | 196 |
Word *curr = state + length - 1; --curr; |
| 197 | 197 |
Iterator it = begin; int cnt = 0; |
| 198 | 198 |
int num; |
| 199 | 199 |
|
| 200 | 200 |
initState(init); |
| 201 | 201 |
|
| 202 | 202 |
num = length > end - begin ? length : end - begin; |
| 203 | 203 |
while (num--) {
|
| 204 | 204 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) |
| 205 | 205 |
+ *it + cnt; |
| 206 | 206 |
++it; ++cnt; |
| 207 | 207 |
if (it == end) {
|
| 208 | 208 |
it = begin; cnt = 0; |
| 209 | 209 |
} |
| 210 | 210 |
if (curr == state) {
|
| 211 | 211 |
curr = state + length - 1; curr[0] = state[0]; |
| 212 | 212 |
} |
| 213 | 213 |
--curr; |
| 214 | 214 |
} |
| 215 | 215 |
|
| 216 | 216 |
num = length - 1; cnt = length - (curr - state) - 1; |
| 217 | 217 |
while (num--) {
|
| 218 | 218 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2)) |
| 219 | 219 |
- cnt; |
| 220 | 220 |
--curr; ++cnt; |
| 221 | 221 |
if (curr == state) {
|
| 222 | 222 |
curr = state + length - 1; curr[0] = state[0]; --curr; |
| 223 | 223 |
cnt = 1; |
| 224 | 224 |
} |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
state[length - 1] = Word(1) << (bits - 1); |
| 228 | 228 |
} |
| 229 | 229 |
|
| 230 | 230 |
void copyState(const RandomCore& other) {
|
| 231 | 231 |
std::copy(other.state, other.state + length, state); |
| 232 | 232 |
current = state + (other.current - other.state); |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 | 235 |
Word operator()() {
|
| 236 | 236 |
if (current == state) fillState(); |
| 237 | 237 |
--current; |
| 238 | 238 |
Word rnd = *current; |
| 239 | 239 |
return RandomTraits<Word>::tempering(rnd); |
| 240 | 240 |
} |
| 241 | 241 |
|
| 242 | 242 |
private: |
| 243 | 243 |
|
| 244 | 244 |
|
| 245 | 245 |
void fillState() {
|
| 246 | 246 |
static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
|
| 247 | 247 |
static const Word loMask = RandomTraits<Word>::loMask; |
| 248 | 248 |
static const Word hiMask = RandomTraits<Word>::hiMask; |
| 249 | 249 |
|
| 250 | 250 |
current = state + length; |
| 251 | 251 |
|
| 252 | 252 |
register Word *curr = state + length - 1; |
| 253 | 253 |
register long num; |
| 254 | 254 |
|
| 255 | 255 |
num = length - shift; |
| 256 | 256 |
while (num--) {
|
| 257 | 257 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^ |
| 258 | 258 |
curr[- shift] ^ mask[curr[-1] & 1ul]; |
| 259 | 259 |
--curr; |
| 260 | 260 |
} |
| 261 | 261 |
num = shift - 1; |
| 262 | 262 |
while (num--) {
|
| 263 | 263 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^ |
| 264 | 264 |
curr[length - shift] ^ mask[curr[-1] & 1ul]; |
| 265 | 265 |
--curr; |
| 266 | 266 |
} |
| 267 | 267 |
state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^ |
| 268 | 268 |
curr[length - shift] ^ mask[curr[length - 1] & 1ul]; |
| 269 | 269 |
|
| 270 | 270 |
} |
| 271 | 271 |
|
| 272 | 272 |
|
| 273 | 273 |
Word *current; |
| 274 | 274 |
Word state[length]; |
| 275 | 275 |
|
| 276 | 276 |
}; |
| 277 | 277 |
|
| 278 | 278 |
|
| 279 | 279 |
template <typename Result, |
| 280 | 280 |
int shift = (std::numeric_limits<Result>::digits + 1) / 2> |
| 281 | 281 |
struct Masker {
|
| 282 | 282 |
static Result mask(const Result& result) {
|
| 283 | 283 |
return Masker<Result, (shift + 1) / 2>:: |
| 284 | 284 |
mask(static_cast<Result>(result | (result >> shift))); |
| 285 | 285 |
} |
| 286 | 286 |
}; |
| 287 | 287 |
|
| 288 | 288 |
template <typename Result> |
| 289 | 289 |
struct Masker<Result, 1> {
|
| 290 | 290 |
static Result mask(const Result& result) {
|
| 291 | 291 |
return static_cast<Result>(result | (result >> 1)); |
| 292 | 292 |
} |
| 293 | 293 |
}; |
| 294 | 294 |
|
| 295 | 295 |
template <typename Result, typename Word, |
| 296 | 296 |
int rest = std::numeric_limits<Result>::digits, int shift = 0, |
| 297 | 297 |
bool last = rest <= std::numeric_limits<Word>::digits> |
| 298 | 298 |
struct IntConversion {
|
| 299 | 299 |
static const int bits = std::numeric_limits<Word>::digits; |
| 300 | 300 |
|
| 301 | 301 |
static Result convert(RandomCore<Word>& rnd) {
|
| 302 | 302 |
return static_cast<Result>(rnd() >> (bits - rest)) << shift; |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 | 305 |
}; |
| 306 | 306 |
|
| 307 | 307 |
template <typename Result, typename Word, int rest, int shift> |
| 308 | 308 |
struct IntConversion<Result, Word, rest, shift, false> {
|
| 309 | 309 |
static const int bits = std::numeric_limits<Word>::digits; |
| 310 | 310 |
|
| 311 | 311 |
static Result convert(RandomCore<Word>& rnd) {
|
| 312 | 312 |
return (static_cast<Result>(rnd()) << shift) | |
| 313 | 313 |
IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd); |
| 314 | 314 |
} |
| 315 | 315 |
}; |
| 316 | 316 |
|
| 317 | 317 |
|
| 318 | 318 |
template <typename Result, typename Word, |
| 319 | 319 |
bool one_word = (std::numeric_limits<Word>::digits < |
| 320 | 320 |
std::numeric_limits<Result>::digits) > |
| 321 | 321 |
struct Mapping {
|
| 322 | 322 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
| 323 | 323 |
Word max = Word(bound - 1); |
| 324 | 324 |
Result mask = Masker<Result>::mask(bound - 1); |
| 325 | 325 |
Result num; |
| 326 | 326 |
do {
|
| 327 | 327 |
num = IntConversion<Result, Word>::convert(rnd) & mask; |
| 328 | 328 |
} while (num > max); |
| 329 | 329 |
return num; |
| 330 | 330 |
} |
| 331 | 331 |
}; |
| 332 | 332 |
|
| 333 | 333 |
template <typename Result, typename Word> |
| 334 | 334 |
struct Mapping<Result, Word, false> {
|
| 335 | 335 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
| 336 | 336 |
Word max = Word(bound - 1); |
| 337 | 337 |
Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2> |
| 338 | 338 |
::mask(max); |
| 339 | 339 |
Word num; |
| 340 | 340 |
do {
|
| 341 | 341 |
num = rnd() & mask; |
| 342 | 342 |
} while (num > max); |
| 343 | 343 |
return num; |
| 344 | 344 |
} |
| 345 | 345 |
}; |
| 346 | 346 |
|
| 347 | 347 |
template <typename Result, int exp> |
| 348 | 348 |
struct ShiftMultiplier {
|
| 349 | 349 |
static const Result multiplier() {
|
| 350 | 350 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); |
| 351 | 351 |
res *= res; |
| 352 | 352 |
if ((exp & 1) == 1) res *= static_cast<Result>(0.5); |
| 353 | 353 |
return res; |
| 354 | 354 |
} |
| 355 | 355 |
}; |
| 356 | 356 |
|
| 357 | 357 |
template <typename Result> |
| 358 | 358 |
struct ShiftMultiplier<Result, 0> {
|
| 359 | 359 |
static const Result multiplier() {
|
| 360 | 360 |
return static_cast<Result>(1.0); |
| 361 | 361 |
} |
| 362 | 362 |
}; |
| 363 | 363 |
|
| 364 | 364 |
template <typename Result> |
| 365 | 365 |
struct ShiftMultiplier<Result, 20> {
|
| 366 | 366 |
static const Result multiplier() {
|
| 367 | 367 |
return static_cast<Result>(1.0/1048576.0); |
| 368 | 368 |
} |
| 369 | 369 |
}; |
| 370 | 370 |
|
| 371 | 371 |
template <typename Result> |
| 372 | 372 |
struct ShiftMultiplier<Result, 32> {
|
| 373 | 373 |
static const Result multiplier() {
|
| 374 | 374 |
return static_cast<Result>(1.0/4294967296.0); |
| 375 | 375 |
} |
| 376 | 376 |
}; |
| 377 | 377 |
|
| 378 | 378 |
template <typename Result> |
| 379 | 379 |
struct ShiftMultiplier<Result, 53> {
|
| 380 | 380 |
static const Result multiplier() {
|
| 381 | 381 |
return static_cast<Result>(1.0/9007199254740992.0); |
| 382 | 382 |
} |
| 383 | 383 |
}; |
| 384 | 384 |
|
| 385 | 385 |
template <typename Result> |
| 386 | 386 |
struct ShiftMultiplier<Result, 64> {
|
| 387 | 387 |
static const Result multiplier() {
|
| 388 | 388 |
return static_cast<Result>(1.0/18446744073709551616.0); |
| 389 | 389 |
} |
| 390 | 390 |
}; |
| 391 | 391 |
|
| 392 | 392 |
template <typename Result, int exp> |
| 393 | 393 |
struct Shifting {
|
| 394 | 394 |
static Result shift(const Result& result) {
|
| 395 | 395 |
return result * ShiftMultiplier<Result, exp>::multiplier(); |
| 396 | 396 |
} |
| 397 | 397 |
}; |
| 398 | 398 |
|
| 399 | 399 |
template <typename Result, typename Word, |
| 400 | 400 |
int rest = std::numeric_limits<Result>::digits, int shift = 0, |
| 401 | 401 |
bool last = rest <= std::numeric_limits<Word>::digits> |
| 402 | 402 |
struct RealConversion{
|
| 403 | 403 |
static const int bits = std::numeric_limits<Word>::digits; |
| 404 | 404 |
|
| 405 | 405 |
static Result convert(RandomCore<Word>& rnd) {
|
| 406 | 406 |
return Shifting<Result, shift + rest>:: |
| 407 | 407 |
shift(static_cast<Result>(rnd() >> (bits - rest))); |
| 408 | 408 |
} |
| 409 | 409 |
}; |
| 410 | 410 |
|
| 411 | 411 |
template <typename Result, typename Word, int rest, int shift> |
| 412 | 412 |
struct RealConversion<Result, Word, rest, shift, false> {
|
| 413 | 413 |
static const int bits = std::numeric_limits<Word>::digits; |
| 414 | 414 |
|
| 415 | 415 |
static Result convert(RandomCore<Word>& rnd) {
|
| 416 | 416 |
return Shifting<Result, shift + bits>:: |
| 417 | 417 |
shift(static_cast<Result>(rnd())) + |
| 418 | 418 |
RealConversion<Result, Word, rest-bits, shift + bits>:: |
| 419 | 419 |
convert(rnd); |
| 420 | 420 |
} |
| 421 | 421 |
}; |
| 422 | 422 |
|
| 423 | 423 |
template <typename Result, typename Word> |
| 424 | 424 |
struct Initializer {
|
| 425 | 425 |
|
| 426 | 426 |
template <typename Iterator> |
| 427 | 427 |
static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
|
| 428 | 428 |
std::vector<Word> ws; |
| 429 | 429 |
for (Iterator it = begin; it != end; ++it) {
|
| 430 | 430 |
ws.push_back(Word(*it)); |
| 431 | 431 |
} |
| 432 | 432 |
rnd.initState(ws.begin(), ws.end()); |
| 433 | 433 |
} |
| 434 | 434 |
|
| 435 | 435 |
static void init(RandomCore<Word>& rnd, Result seed) {
|
| 436 | 436 |
rnd.initState(seed); |
| 437 | 437 |
} |
| 438 | 438 |
}; |
| 439 | 439 |
|
| 440 | 440 |
template <typename Word> |
| 441 | 441 |
struct BoolConversion {
|
| 442 | 442 |
static bool convert(RandomCore<Word>& rnd) {
|
| 443 | 443 |
return (rnd() & 1) == 1; |
| 444 | 444 |
} |
| 445 | 445 |
}; |
| 446 | 446 |
|
| 447 | 447 |
template <typename Word> |
| 448 | 448 |
struct BoolProducer {
|
| 449 | 449 |
Word buffer; |
| 450 | 450 |
int num; |
| 451 | 451 |
|
| 452 | 452 |
BoolProducer() : num(0) {}
|
| 453 | 453 |
|
| 454 | 454 |
bool convert(RandomCore<Word>& rnd) {
|
| 455 | 455 |
if (num == 0) {
|
| 456 | 456 |
buffer = rnd(); |
| 457 | 457 |
num = RandomTraits<Word>::bits; |
| 458 | 458 |
} |
| 459 | 459 |
bool r = (buffer & 1); |
| 460 | 460 |
buffer >>= 1; |
| 461 | 461 |
--num; |
| 462 | 462 |
return r; |
| 463 | 463 |
} |
| 464 | 464 |
}; |
| 465 | 465 |
|
| 466 | 466 |
} |
| 467 | 467 |
|
| 468 | 468 |
/// \ingroup misc |
| 469 | 469 |
/// |
| 470 | 470 |
/// \brief Mersenne Twister random number generator |
| 471 | 471 |
/// |
| 472 | 472 |
/// The Mersenne Twister is a twisted generalized feedback |
| 473 | 473 |
/// shift-register generator of Matsumoto and Nishimura. The period |
| 474 | 474 |
/// of this generator is \f$ 2^{19937} - 1 \f$ and it is
|
| 475 | 475 |
/// equi-distributed in 623 dimensions for 32-bit numbers. The time |
| 476 | 476 |
/// performance of this generator is comparable to the commonly used |
| 477 | 477 |
/// generators. |
| 478 | 478 |
/// |
| 479 | 479 |
/// This implementation is specialized for both 32-bit and 64-bit |
| 480 | 480 |
/// architectures. The generators differ sligthly in the |
| 481 | 481 |
/// initialization and generation phase so they produce two |
| 482 | 482 |
/// completly different sequences. |
| 483 | 483 |
/// |
| 484 | 484 |
/// The generator gives back random numbers of serveral types. To |
| 485 | 485 |
/// get a random number from a range of a floating point type you |
| 486 | 486 |
/// can use one form of the \c operator() or the \c real() member |
| 487 | 487 |
/// function. If you want to get random number from the {0, 1, ...,
|
| 488 | 488 |
/// n-1} integer range use the \c operator[] or the \c integer() |
| 489 | 489 |
/// method. And to get random number from the whole range of an |
| 490 | 490 |
/// integer type you can use the argumentless \c integer() or \c |
| 491 | 491 |
/// uinteger() functions. After all you can get random bool with |
| 492 | 492 |
/// equal chance of true and false or given probability of true |
| 493 | 493 |
/// result with the \c boolean() member functions. |
| 494 | 494 |
/// |
| 495 | 495 |
///\code |
| 496 | 496 |
/// // The commented code is identical to the other |
| 497 | 497 |
/// double a = rnd(); // [0.0, 1.0) |
| 498 | 498 |
/// // double a = rnd.real(); // [0.0, 1.0) |
| 499 | 499 |
/// double b = rnd(100.0); // [0.0, 100.0) |
| 500 | 500 |
/// // double b = rnd.real(100.0); // [0.0, 100.0) |
| 501 | 501 |
/// double c = rnd(1.0, 2.0); // [1.0, 2.0) |
| 502 | 502 |
/// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0) |
| 503 | 503 |
/// int d = rnd[100000]; // 0..99999 |
| 504 | 504 |
/// // int d = rnd.integer(100000); // 0..99999 |
| 505 | 505 |
/// int e = rnd[6] + 1; // 1..6 |
| 506 | 506 |
/// // int e = rnd.integer(1, 1 + 6); // 1..6 |
| 507 | 507 |
/// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 |
| 508 | 508 |
/// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1 |
| 509 | 509 |
/// bool g = rnd.boolean(); // P(g = true) = 0.5 |
| 510 | 510 |
/// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 |
| 511 | 511 |
///\endcode |
| 512 | 512 |
/// |
| 513 | 513 |
/// LEMON provides a global instance of the random number |
| 514 | 514 |
/// generator which name is \ref lemon::rnd "rnd". Usually it is a |
| 515 | 515 |
/// good programming convenience to use this global generator to get |
| 516 | 516 |
/// random numbers. |
| 517 | 517 |
class Random {
|
| 518 | 518 |
private: |
| 519 | 519 |
|
| 520 | 520 |
// Architecture word |
| 521 | 521 |
typedef unsigned long Word; |
| 522 | 522 |
|
| 523 | 523 |
_random_bits::RandomCore<Word> core; |
| 524 | 524 |
_random_bits::BoolProducer<Word> bool_producer; |
| 525 | 525 |
|
| 526 | 526 |
|
| 527 | 527 |
public: |
| 528 | 528 |
|
| 529 | 529 |
///\name Initialization |
| 530 | 530 |
/// |
| 531 | 531 |
/// @{
|
| 532 | 532 |
|
| 533 | 533 |
///\name Initialization |
| 534 | 534 |
/// |
| 535 | 535 |
/// @{
|
| 536 | 536 |
|
| 537 | 537 |
/// \brief Default constructor |
| 538 | 538 |
/// |
| 539 | 539 |
/// Constructor with constant seeding. |
| 540 | 540 |
Random() { core.initState(); }
|
| 541 | 541 |
|
| 542 | 542 |
/// \brief Constructor with seed |
| 543 | 543 |
/// |
| 544 | 544 |
/// Constructor with seed. The current number type will be converted |
| 545 | 545 |
/// to the architecture word type. |
| 546 | 546 |
template <typename Number> |
| 547 | 547 |
Random(Number seed) {
|
| 548 | 548 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
| 549 | 549 |
} |
| 550 | 550 |
|
| 551 | 551 |
/// \brief Constructor with array seeding |
| 552 | 552 |
/// |
| 553 | 553 |
/// Constructor with array seeding. The given range should contain |
| 554 | 554 |
/// any number type and the numbers will be converted to the |
| 555 | 555 |
/// architecture word type. |
| 556 | 556 |
template <typename Iterator> |
| 557 | 557 |
Random(Iterator begin, Iterator end) {
|
| 558 | 558 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
| 559 | 559 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
| 560 | 560 |
} |
| 561 | 561 |
|
| 562 | 562 |
/// \brief Copy constructor |
| 563 | 563 |
/// |
| 564 | 564 |
/// Copy constructor. The generated sequence will be identical to |
| 565 | 565 |
/// the other sequence. It can be used to save the current state |
| 566 | 566 |
/// of the generator and later use it to generate the same |
| 567 | 567 |
/// sequence. |
| 568 | 568 |
Random(const Random& other) {
|
| 569 | 569 |
core.copyState(other.core); |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 | 572 |
/// \brief Assign operator |
| 573 | 573 |
/// |
| 574 | 574 |
/// Assign operator. The generated sequence will be identical to |
| 575 | 575 |
/// the other sequence. It can be used to save the current state |
| 576 | 576 |
/// of the generator and later use it to generate the same |
| 577 | 577 |
/// sequence. |
| 578 | 578 |
Random& operator=(const Random& other) {
|
| 579 | 579 |
if (&other != this) {
|
| 580 | 580 |
core.copyState(other.core); |
| 581 | 581 |
} |
| 582 | 582 |
return *this; |
| 583 | 583 |
} |
| 584 | 584 |
|
| 585 | 585 |
/// \brief Seeding random sequence |
| 586 | 586 |
/// |
| 587 | 587 |
/// Seeding the random sequence. The current number type will be |
| 588 | 588 |
/// converted to the architecture word type. |
| 589 | 589 |
template <typename Number> |
| 590 | 590 |
void seed(Number seed) {
|
| 591 | 591 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
| 592 | 592 |
} |
| 593 | 593 |
|
| 594 | 594 |
/// \brief Seeding random sequence |
| 595 | 595 |
/// |
| 596 | 596 |
/// Seeding the random sequence. The given range should contain |
| 597 | 597 |
/// any number type and the numbers will be converted to the |
| 598 | 598 |
/// architecture word type. |
| 599 | 599 |
template <typename Iterator> |
| 600 | 600 |
void seed(Iterator begin, Iterator end) {
|
| 601 | 601 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
| 602 | 602 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
| 603 | 603 |
} |
| 604 | 604 |
|
| 605 | 605 |
/// \brief Seeding from file or from process id and time |
| 606 | 606 |
/// |
| 607 | 607 |
/// By default, this function calls the \c seedFromFile() member |
| 608 | 608 |
/// function with the <tt>/dev/urandom</tt> file. If it does not success, |
| 609 | 609 |
/// it uses the \c seedFromTime(). |
| 610 | 610 |
/// \return Currently always true. |
| 611 | 611 |
bool seed() {
|
| 612 | 612 |
#ifndef WIN32 |
| 613 | 613 |
if (seedFromFile("/dev/urandom", 0)) return true;
|
| 614 | 614 |
#endif |
| 615 | 615 |
if (seedFromTime()) return true; |
| 616 | 616 |
return false; |
| 617 | 617 |
} |
| 618 | 618 |
|
| 619 | 619 |
/// \brief Seeding from file |
| 620 | 620 |
/// |
| 621 | 621 |
/// Seeding the random sequence from file. The linux kernel has two |
| 622 | 622 |
/// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which |
| 623 | 623 |
/// could give good seed values for pseudo random generators (The |
| 624 | 624 |
/// difference between two devices is that the <tt>random</tt> may |
| 625 | 625 |
/// block the reading operation while the kernel can give good |
| 626 | 626 |
/// source of randomness, while the <tt>urandom</tt> does not |
| 627 | 627 |
/// block the input, but it could give back bytes with worse |
| 628 | 628 |
/// entropy). |
| 629 | 629 |
/// \param file The source file |
| 630 | 630 |
/// \param offset The offset, from the file read. |
| 631 | 631 |
/// \return True when the seeding successes. |
| 632 | 632 |
#ifndef WIN32 |
| 633 | 633 |
bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) |
| 634 | 634 |
#else |
| 635 | 635 |
bool seedFromFile(const std::string& file = "", int offset = 0) |
| 636 | 636 |
#endif |
| 637 | 637 |
{
|
| 638 | 638 |
std::ifstream rs(file.c_str()); |
| 639 | 639 |
const int size = 4; |
| 640 | 640 |
Word buf[size]; |
| 641 | 641 |
if (offset != 0 && !rs.seekg(offset)) return false; |
| 642 | 642 |
if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false; |
| 643 | 643 |
seed(buf, buf + size); |
| 644 | 644 |
return true; |
| 645 | 645 |
} |
| 646 | 646 |
|
| 647 | 647 |
/// \brief Seding from process id and time |
| 648 | 648 |
/// |
| 649 | 649 |
/// Seding from process id and time. This function uses the |
| 650 | 650 |
/// current process id and the current time for initialize the |
| 651 | 651 |
/// random sequence. |
| 652 | 652 |
/// \return Currently always true. |
| 653 | 653 |
bool seedFromTime() {
|
| 654 | 654 |
#ifndef WIN32 |
| 655 | 655 |
timeval tv; |
| 656 | 656 |
gettimeofday(&tv, 0); |
| 657 | 657 |
seed(getpid() + tv.tv_sec + tv.tv_usec); |
| 658 | 658 |
#else |
| 659 | 659 |
seed(bits::getWinRndSeed()); |
| 660 | 660 |
#endif |
| 661 | 661 |
return true; |
| 662 | 662 |
} |
| 663 | 663 |
|
| 664 | 664 |
/// @} |
| 665 | 665 |
|
| 666 | 666 |
///\name Uniform distributions |
| 667 | 667 |
/// |
| 668 | 668 |
/// @{
|
| 669 | 669 |
|
| 670 | 670 |
/// \brief Returns a random real number from the range [0, 1) |
| 671 | 671 |
/// |
| 672 | 672 |
/// It returns a random real number from the range [0, 1). The |
| 673 | 673 |
/// default Number type is \c double. |
| 674 | 674 |
template <typename Number> |
| 675 | 675 |
Number real() {
|
| 676 | 676 |
return _random_bits::RealConversion<Number, Word>::convert(core); |
| 677 | 677 |
} |
| 678 | 678 |
|
| 679 | 679 |
double real() {
|
| 680 | 680 |
return real<double>(); |
| 681 | 681 |
} |
| 682 | 682 |
|
| 683 | 683 |
/// @} |
| 684 | 684 |
|
| 685 | 685 |
///\name Uniform distributions |
| 686 | 686 |
/// |
| 687 | 687 |
/// @{
|
| 688 | 688 |
|
| 689 | 689 |
/// \brief Returns a random real number from the range [0, 1) |
| 690 | 690 |
/// |
| 691 | 691 |
/// It returns a random double from the range [0, 1). |
| 692 | 692 |
double operator()() {
|
| 693 | 693 |
return real<double>(); |
| 694 | 694 |
} |
| 695 | 695 |
|
| 696 | 696 |
/// \brief Returns a random real number from the range [0, b) |
| 697 | 697 |
/// |
| 698 | 698 |
/// It returns a random real number from the range [0, b). |
| 699 | 699 |
double operator()(double b) {
|
| 700 | 700 |
return real<double>() * b; |
| 701 | 701 |
} |
| 702 | 702 |
|
| 703 | 703 |
/// \brief Returns a random real number from the range [a, b) |
| 704 | 704 |
/// |
| 705 | 705 |
/// It returns a random real number from the range [a, b). |
| 706 | 706 |
double operator()(double a, double b) {
|
| 707 | 707 |
return real<double>() * (b - a) + a; |
| 708 | 708 |
} |
| 709 | 709 |
|
| 710 | 710 |
/// \brief Returns a random integer from a range |
| 711 | 711 |
/// |
| 712 | 712 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
| 713 | 713 |
template <typename Number> |
| 714 | 714 |
Number integer(Number b) {
|
| 715 | 715 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
| 716 | 716 |
} |
| 717 | 717 |
|
| 718 | 718 |
/// \brief Returns a random integer from a range |
| 719 | 719 |
/// |
| 720 | 720 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}.
|
| 721 | 721 |
template <typename Number> |
| 722 | 722 |
Number integer(Number a, Number b) {
|
| 723 | 723 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a; |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
/// \brief Returns a random integer from a range |
| 727 | 727 |
/// |
| 728 | 728 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
| 729 | 729 |
template <typename Number> |
| 730 | 730 |
Number operator[](Number b) {
|
| 731 | 731 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
| 732 | 732 |
} |
| 733 | 733 |
|
| 734 | 734 |
/// \brief Returns a random non-negative integer |
| 735 | 735 |
/// |
| 736 | 736 |
/// It returns a random non-negative integer uniformly from the |
| 737 | 737 |
/// whole range of the current \c Number type. The default result |
| 738 | 738 |
/// type of this function is <tt>unsigned int</tt>. |
| 739 | 739 |
template <typename Number> |
| 740 | 740 |
Number uinteger() {
|
| 741 | 741 |
return _random_bits::IntConversion<Number, Word>::convert(core); |
| 742 | 742 |
} |
| 743 | 743 |
|
| 744 | 744 |
/// @} |
| 745 | 745 |
|
| 746 | 746 |
unsigned int uinteger() {
|
| 747 | 747 |
return uinteger<unsigned int>(); |
| 748 | 748 |
} |
| 749 | 749 |
|
| 750 | 750 |
/// \brief Returns a random integer |
| 751 | 751 |
/// |
| 752 | 752 |
/// It returns a random integer uniformly from the whole range of |
| 753 | 753 |
/// the current \c Number type. The default result type of this |
| 754 | 754 |
/// function is \c int. |
| 755 | 755 |
template <typename Number> |
| 756 | 756 |
Number integer() {
|
| 757 | 757 |
static const int nb = std::numeric_limits<Number>::digits + |
| 758 | 758 |
(std::numeric_limits<Number>::is_signed ? 1 : 0); |
| 759 | 759 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core); |
| 760 | 760 |
} |
| 761 | 761 |
|
| 762 | 762 |
int integer() {
|
| 763 | 763 |
return integer<int>(); |
| 764 | 764 |
} |
| 765 | 765 |
|
| 766 | 766 |
/// \brief Returns a random bool |
| 767 | 767 |
/// |
| 768 | 768 |
/// It returns a random bool. The generator holds a buffer for |
| 769 | 769 |
/// random bits. Every time when it become empty the generator makes |
| 770 | 770 |
/// a new random word and fill the buffer up. |
| 771 | 771 |
bool boolean() {
|
| 772 | 772 |
return bool_producer.convert(core); |
| 773 | 773 |
} |
| 774 | 774 |
|
| 775 | 775 |
/// @} |
| 776 | 776 |
|
| 777 | 777 |
///\name Non-uniform distributions |
| 778 | 778 |
/// |
| 779 | 779 |
|
| 780 | 780 |
///@{
|
| 781 | 781 |
|
| 782 | 782 |
/// \brief Returns a random bool |
| 783 | 783 |
/// |
| 784 | 784 |
/// It returns a random bool with given probability of true result. |
| 785 | 785 |
bool boolean(double p) {
|
| 786 | 786 |
return operator()() < p; |
| 787 | 787 |
} |
| 788 | 788 |
|
| 789 | 789 |
/// Standard Gauss distribution |
| 790 | 790 |
|
| 791 | 791 |
/// Standard Gauss distribution. |
| 792 | 792 |
/// \note The Cartesian form of the Box-Muller |
| 793 | 793 |
/// transformation is used to generate a random normal distribution. |
| 794 | 794 |
double gauss() |
| 795 | 795 |
{
|
| 796 | 796 |
double V1,V2,S; |
| 797 | 797 |
do {
|
| 798 | 798 |
V1=2*real<double>()-1; |
| 799 | 799 |
V2=2*real<double>()-1; |
| 800 | 800 |
S=V1*V1+V2*V2; |
| 801 | 801 |
} while(S>=1); |
| 802 | 802 |
return std::sqrt(-2*std::log(S)/S)*V1; |
| 803 | 803 |
} |
| 804 | 804 |
/// Gauss distribution with given mean and standard deviation |
| 805 | 805 |
|
| 806 | 806 |
/// Gauss distribution with given mean and standard deviation. |
| 807 | 807 |
/// \sa gauss() |
| 808 | 808 |
double gauss(double mean,double std_dev) |
| 809 | 809 |
{
|
| 810 | 810 |
return gauss()*std_dev+mean; |
| 811 | 811 |
} |
| 812 | 812 |
|
| 813 | 813 |
/// Exponential distribution with given mean |
| 814 | 814 |
|
| 815 | 815 |
/// This function generates an exponential distribution random number |
| 816 | 816 |
/// with mean <tt>1/lambda</tt>. |
| 817 | 817 |
/// |
| 818 | 818 |
double exponential(double lambda=1.0) |
| 819 | 819 |
{
|
| 820 | 820 |
return -std::log(1.0-real<double>())/lambda; |
| 821 | 821 |
} |
| 822 | 822 |
|
| 823 | 823 |
/// Gamma distribution with given integer shape |
| 824 | 824 |
|
| 825 | 825 |
/// This function generates a gamma distribution random number. |
| 826 | 826 |
/// |
| 827 | 827 |
///\param k shape parameter (<tt>k>0</tt> integer) |
| 828 | 828 |
double gamma(int k) |
| 829 | 829 |
{
|
| 830 | 830 |
double s = 0; |
| 831 | 831 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
| 832 | 832 |
return s; |
| 833 | 833 |
} |
| 834 | 834 |
|
| 835 | 835 |
/// Gamma distribution with given shape and scale parameter |
| 836 | 836 |
|
| 837 | 837 |
/// This function generates a gamma distribution random number. |
| 838 | 838 |
/// |
| 839 | 839 |
///\param k shape parameter (<tt>k>0</tt>) |
| 840 | 840 |
///\param theta scale parameter |
| 841 | 841 |
/// |
| 842 | 842 |
double gamma(double k,double theta=1.0) |
| 843 | 843 |
{
|
| 844 | 844 |
double xi,nu; |
| 845 | 845 |
const double delta = k-std::floor(k); |
| 846 | 846 |
const double v0=E/(E-delta); |
| 847 | 847 |
do {
|
| 848 | 848 |
double V0=1.0-real<double>(); |
| 849 | 849 |
double V1=1.0-real<double>(); |
| 850 | 850 |
double V2=1.0-real<double>(); |
| 851 | 851 |
if(V2<=v0) |
| 852 | 852 |
{
|
| 853 | 853 |
xi=std::pow(V1,1.0/delta); |
| 854 | 854 |
nu=V0*std::pow(xi,delta-1.0); |
| 855 | 855 |
} |
| 856 | 856 |
else |
| 857 | 857 |
{
|
| 858 | 858 |
xi=1.0-std::log(V1); |
| 859 | 859 |
nu=V0*std::exp(-xi); |
| 860 | 860 |
} |
| 861 | 861 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi)); |
| 862 | 862 |
return theta*(xi+gamma(int(std::floor(k)))); |
| 863 | 863 |
} |
| 864 | 864 |
|
| 865 | 865 |
/// Weibull distribution |
| 866 | 866 |
|
| 867 | 867 |
/// This function generates a Weibull distribution random number. |
| 868 | 868 |
/// |
| 869 | 869 |
///\param k shape parameter (<tt>k>0</tt>) |
| 870 | 870 |
///\param lambda scale parameter (<tt>lambda>0</tt>) |
| 871 | 871 |
/// |
| 872 | 872 |
double weibull(double k,double lambda) |
| 873 | 873 |
{
|
| 874 | 874 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k); |
| 875 | 875 |
} |
| 876 | 876 |
|
| 877 | 877 |
/// Pareto distribution |
| 878 | 878 |
|
| 879 | 879 |
/// This function generates a Pareto distribution random number. |
| 880 | 880 |
/// |
| 881 | 881 |
///\param k shape parameter (<tt>k>0</tt>) |
| 882 | 882 |
///\param x_min location parameter (<tt>x_min>0</tt>) |
| 883 | 883 |
/// |
| 884 | 884 |
double pareto(double k,double x_min) |
| 885 | 885 |
{
|
| 886 | 886 |
return exponential(gamma(k,1.0/x_min))+x_min; |
| 887 | 887 |
} |
| 888 | 888 |
|
| 889 | 889 |
/// Poisson distribution |
| 890 | 890 |
|
| 891 | 891 |
/// This function generates a Poisson distribution random number with |
| 892 | 892 |
/// parameter \c lambda. |
| 893 | 893 |
/// |
| 894 | 894 |
/// The probability mass function of this distribusion is |
| 895 | 895 |
/// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
|
| 896 | 896 |
/// \note The algorithm is taken from the book of Donald E. Knuth titled |
| 897 | 897 |
/// ''Seminumerical Algorithms'' (1969). Its running time is linear in the |
| 898 | 898 |
/// return value. |
| 899 | 899 |
|
| 900 | 900 |
int poisson(double lambda) |
| 901 | 901 |
{
|
| 902 | 902 |
const double l = std::exp(-lambda); |
| 903 | 903 |
int k=0; |
| 904 | 904 |
double p = 1.0; |
| 905 | 905 |
do {
|
| 906 | 906 |
k++; |
| 907 | 907 |
p*=real<double>(); |
| 908 | 908 |
} while (p>=l); |
| 909 | 909 |
return k-1; |
| 910 | 910 |
} |
| 911 | 911 |
|
| 912 | 912 |
///@} |
| 913 | 913 |
|
| 914 | 914 |
///\name Two dimensional distributions |
| 915 | 915 |
/// |
| 916 | 916 |
|
| 917 | 917 |
///@{
|
| 918 | 918 |
|
| 919 | 919 |
/// Uniform distribution on the full unit circle |
| 920 | 920 |
|
| 921 | 921 |
/// Uniform distribution on the full unit circle. |
| 922 | 922 |
/// |
| 923 | 923 |
dim2::Point<double> disc() |
| 924 | 924 |
{
|
| 925 | 925 |
double V1,V2; |
| 926 | 926 |
do {
|
| 927 | 927 |
V1=2*real<double>()-1; |
| 928 | 928 |
V2=2*real<double>()-1; |
| 929 | 929 |
|
| 930 | 930 |
} while(V1*V1+V2*V2>=1); |
| 931 | 931 |
return dim2::Point<double>(V1,V2); |
| 932 | 932 |
} |
| 933 | 933 |
/// A kind of two dimensional Gauss distribution |
| 934 | 934 |
|
| 935 | 935 |
/// This function provides a turning symmetric two-dimensional distribution. |
| 936 | 936 |
/// Both coordinates are of standard normal distribution, but they are not |
| 937 | 937 |
/// independent. |
| 938 | 938 |
/// |
| 939 | 939 |
/// \note The coordinates are the two random variables provided by |
| 940 | 940 |
/// the Box-Muller method. |
| 941 | 941 |
dim2::Point<double> gauss2() |
| 942 | 942 |
{
|
| 943 | 943 |
double V1,V2,S; |
| 944 | 944 |
do {
|
| 945 | 945 |
V1=2*real<double>()-1; |
| 946 | 946 |
V2=2*real<double>()-1; |
| 947 | 947 |
S=V1*V1+V2*V2; |
| 948 | 948 |
} while(S>=1); |
| 949 | 949 |
double W=std::sqrt(-2*std::log(S)/S); |
| 950 | 950 |
return dim2::Point<double>(W*V1,W*V2); |
| 951 | 951 |
} |
| 952 | 952 |
/// A kind of two dimensional exponential distribution |
| 953 | 953 |
|
| 954 | 954 |
/// This function provides a turning symmetric two-dimensional distribution. |
| 955 | 955 |
/// The x-coordinate is of conditionally exponential distribution |
| 956 | 956 |
/// with the condition that x is positive and y=0. If x is negative and |
| 957 | 957 |
/// y=0 then, -x is of exponential distribution. The same is true for the |
| 958 | 958 |
/// y-coordinate. |
| 959 | 959 |
dim2::Point<double> exponential2() |
| 960 | 960 |
{
|
| 961 | 961 |
double V1,V2,S; |
| 962 | 962 |
do {
|
| 963 | 963 |
V1=2*real<double>()-1; |
| 964 | 964 |
V2=2*real<double>()-1; |
| 965 | 965 |
S=V1*V1+V2*V2; |
| 966 | 966 |
} while(S>=1); |
| 967 | 967 |
double W=-std::log(S)/S; |
| 968 | 968 |
return dim2::Point<double>(W*V1,W*V2); |
| 969 | 969 |
} |
| 970 | 970 |
|
| 971 | 971 |
///@} |
| 972 | 972 |
}; |
| 973 | 973 |
|
| 974 | 974 |
|
| 975 | 975 |
extern Random rnd; |
| 976 | 976 |
|
| 977 | 977 |
} |
| 978 | 978 |
|
| 979 | 979 |
#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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_SMART_GRAPH_H |
| 20 | 20 |
#define LEMON_SMART_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/bits/graph_extender.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
class SmartDigraph; |
| 35 | 35 |
///Base of SmartDigraph |
| 36 | 36 |
|
| 37 | 37 |
///Base of SmartDigraph |
| 38 | 38 |
/// |
| 39 | 39 |
class SmartDigraphBase {
|
| 40 | 40 |
protected: |
| 41 | 41 |
|
| 42 | 42 |
struct NodeT |
| 43 | 43 |
{
|
| 44 | 44 |
int first_in, first_out; |
| 45 | 45 |
NodeT() {}
|
| 46 | 46 |
}; |
| 47 | 47 |
struct ArcT |
| 48 | 48 |
{
|
| 49 | 49 |
int target, source, next_in, next_out; |
| 50 | 50 |
ArcT() {}
|
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
std::vector<NodeT> nodes; |
| 54 | 54 |
std::vector<ArcT> arcs; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef SmartDigraphBase Graph; |
| 59 | 59 |
|
| 60 | 60 |
class Node; |
| 61 | 61 |
class Arc; |
| 62 | 62 |
|
| 63 | 63 |
public: |
| 64 | 64 |
|
| 65 | 65 |
SmartDigraphBase() : nodes(), arcs() { }
|
| 66 | 66 |
SmartDigraphBase(const SmartDigraphBase &_g) |
| 67 | 67 |
: nodes(_g.nodes), arcs(_g.arcs) { }
|
| 68 | 68 |
|
| 69 | 69 |
typedef True NodeNumTag; |
| 70 | 70 |
typedef True EdgeNumTag; |
| 71 | 71 |
|
| 72 | 72 |
int nodeNum() const { return nodes.size(); }
|
| 73 | 73 |
int arcNum() const { return arcs.size(); }
|
| 74 | 74 |
|
| 75 | 75 |
int maxNodeId() const { return nodes.size()-1; }
|
| 76 | 76 |
int maxArcId() const { return arcs.size()-1; }
|
| 77 | 77 |
|
| 78 | 78 |
Node addNode() {
|
| 79 | 79 |
int n = nodes.size(); |
| 80 | 80 |
nodes.push_back(NodeT()); |
| 81 | 81 |
nodes[n].first_in = -1; |
| 82 | 82 |
nodes[n].first_out = -1; |
| 83 | 83 |
return Node(n); |
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
Arc addArc(Node u, Node v) {
|
| 87 | 87 |
int n = arcs.size(); |
| 88 | 88 |
arcs.push_back(ArcT()); |
| 89 | 89 |
arcs[n].source = u._id; |
| 90 | 90 |
arcs[n].target = v._id; |
| 91 | 91 |
arcs[n].next_out = nodes[u._id].first_out; |
| 92 | 92 |
arcs[n].next_in = nodes[v._id].first_in; |
| 93 | 93 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
| 94 | 94 |
|
| 95 | 95 |
return Arc(n); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
void clear() {
|
| 99 | 99 |
arcs.clear(); |
| 100 | 100 |
nodes.clear(); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc a) const { return Node(arcs[a._id].source); }
|
| 104 | 104 |
Node target(Arc a) const { return Node(arcs[a._id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
static int id(Node v) { return v._id; }
|
| 107 | 107 |
static int id(Arc a) { return a._id; }
|
| 108 | 108 |
|
| 109 | 109 |
static Node nodeFromId(int id) { return Node(id);}
|
| 110 | 110 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 111 | 111 |
|
| 112 | 112 |
bool valid(Node n) const {
|
| 113 | 113 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 114 | 114 |
} |
| 115 | 115 |
bool valid(Arc a) const {
|
| 116 | 116 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class SmartDigraphBase; |
| 121 | 121 |
friend class SmartDigraph; |
| 122 | 122 |
|
| 123 | 123 |
protected: |
| 124 | 124 |
int _id; |
| 125 | 125 |
explicit Node(int id) : _id(id) {}
|
| 126 | 126 |
public: |
| 127 | 127 |
Node() {}
|
| 128 | 128 |
Node (Invalid) : _id(-1) {}
|
| 129 | 129 |
bool operator==(const Node i) const {return _id == i._id;}
|
| 130 | 130 |
bool operator!=(const Node i) const {return _id != i._id;}
|
| 131 | 131 |
bool operator<(const Node i) const {return _id < i._id;}
|
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
|
| 135 | 135 |
class Arc {
|
| 136 | 136 |
friend class SmartDigraphBase; |
| 137 | 137 |
friend class SmartDigraph; |
| 138 | 138 |
|
| 139 | 139 |
protected: |
| 140 | 140 |
int _id; |
| 141 | 141 |
explicit Arc(int id) : _id(id) {}
|
| 142 | 142 |
public: |
| 143 | 143 |
Arc() { }
|
| 144 | 144 |
Arc (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Arc i) const {return _id == i._id;}
|
| 146 | 146 |
bool operator!=(const Arc i) const {return _id != i._id;}
|
| 147 | 147 |
bool operator<(const Arc i) const {return _id < i._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
void first(Node& node) const {
|
| 151 | 151 |
node._id = nodes.size() - 1; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
static void next(Node& node) {
|
| 155 | 155 |
--node._id; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
void first(Arc& arc) const {
|
| 159 | 159 |
arc._id = arcs.size() - 1; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
static void next(Arc& arc) {
|
| 163 | 163 |
--arc._id; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
void firstOut(Arc& arc, const Node& node) const {
|
| 167 | 167 |
arc._id = nodes[node._id].first_out; |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
void nextOut(Arc& arc) const {
|
| 171 | 171 |
arc._id = arcs[arc._id].next_out; |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void firstIn(Arc& arc, const Node& node) const {
|
| 175 | 175 |
arc._id = nodes[node._id].first_in; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
void nextIn(Arc& arc) const {
|
| 179 | 179 |
arc._id = arcs[arc._id].next_in; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
}; |
| 183 | 183 |
|
| 184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
| 185 | 185 |
|
| 186 | 186 |
///\ingroup graphs |
| 187 | 187 |
/// |
| 188 | 188 |
///\brief A smart directed graph class. |
| 189 | 189 |
/// |
| 190 | 190 |
///This is a simple and fast digraph implementation. |
| 191 | 191 |
///It is also quite memory efficient, but at the price |
| 192 | 192 |
///that <b> it does support only limited (only stack-like) |
| 193 | 193 |
///node and arc deletions</b>. |
| 194 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
| 195 | 195 |
///an important extra feature that its maps are real \ref |
| 196 | 196 |
///concepts::ReferenceMap "reference map"s. |
| 197 | 197 |
/// |
| 198 | 198 |
///\sa concepts::Digraph. |
| 199 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase {
|
| 200 | 200 |
public: |
| 201 | 201 |
|
| 202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
| 203 | 203 |
|
| 204 | 204 |
private: |
| 205 | 205 |
|
| 206 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 207 | 207 |
|
| 208 | 208 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 209 | 209 |
/// |
| 210 | 210 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
|
| 211 | 211 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
| 212 | 212 |
///Use DigraphCopy() instead. |
| 213 | 213 |
|
| 214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
| 215 | 215 |
///Use DigraphCopy() instead. |
| 216 | 216 |
void operator=(const SmartDigraph &) {}
|
| 217 | 217 |
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
/// Constructor |
| 221 | 221 |
|
| 222 | 222 |
/// Constructor. |
| 223 | 223 |
/// |
| 224 | 224 |
SmartDigraph() {};
|
| 225 | 225 |
|
| 226 | 226 |
///Add a new node to the digraph. |
| 227 | 227 |
|
| 228 | 228 |
/// \return the new node. |
| 229 | 229 |
/// |
| 230 | 230 |
Node addNode() { return Parent::addNode(); }
|
| 231 | 231 |
|
| 232 | 232 |
///Add a new arc to the digraph. |
| 233 | 233 |
|
| 234 | 234 |
///Add a new arc to the digraph with source node \c s |
| 235 | 235 |
///and target node \c t. |
| 236 | 236 |
///\return the new arc. |
| 237 | 237 |
Arc addArc(const Node& s, const Node& t) {
|
| 238 | 238 |
return Parent::addArc(s, t); |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 242 | 242 |
/// allocation. |
| 243 | 243 |
|
| 244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
| 245 | 245 |
/// allocation: if you know that the digraph you want to build will |
| 246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 247 | 247 |
/// then it is worth reserving space for this amount before starting |
| 248 | 248 |
/// to build the digraph. |
| 249 | 249 |
/// \sa reserveArc |
| 250 | 250 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 251 | 251 |
|
| 252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 253 | 253 |
/// allocation. |
| 254 | 254 |
|
| 255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
| 256 | 256 |
/// allocation: if you know that the digraph you want to build will |
| 257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 258 | 258 |
/// then it is worth reserving space for this amount before starting |
| 259 | 259 |
/// to build the digraph. |
| 260 | 260 |
/// \sa reserveNode |
| 261 | 261 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 262 | 262 |
|
| 263 | 263 |
/// \brief Node validity check |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function gives back true if the given node is valid, |
| 266 | 266 |
/// ie. it is a real node of the graph. |
| 267 | 267 |
/// |
| 268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
| 269 | 269 |
/// when new nodes are added to the graph. |
| 270 | 270 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 271 | 271 |
|
| 272 | 272 |
/// \brief Arc validity check |
| 273 | 273 |
/// |
| 274 | 274 |
/// This function gives back true if the given arc is valid, |
| 275 | 275 |
/// ie. it is a real arc of the graph. |
| 276 | 276 |
/// |
| 277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 278 | 278 |
/// when new arcs are added to the graph. |
| 279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 280 | 280 |
|
| 281 | 281 |
///Clear the digraph. |
| 282 | 282 |
|
| 283 | 283 |
///Erase all the nodes and arcs from the digraph. |
| 284 | 284 |
/// |
| 285 | 285 |
void clear() {
|
| 286 | 286 |
Parent::clear(); |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
///Split a node. |
| 290 | 290 |
|
| 291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
| 292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
| 294 | 294 |
///from \c n to the newly created node is also added. |
| 295 | 295 |
///\return The newly created node. |
| 296 | 296 |
/// |
| 297 | 297 |
///\note The <tt>Arc</tt>s |
| 298 | 298 |
///referencing a moved arc remain |
| 299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
| 300 | 300 |
///may be invalidated. |
| 301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
| 302 | 302 |
///feature. |
| 303 | 303 |
Node split(Node n, bool connect = true) |
| 304 | 304 |
{
|
| 305 | 305 |
Node b = addNode(); |
| 306 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
| 307 | 307 |
nodes[n._id].first_out=-1; |
| 308 | 308 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
|
| 309 | 309 |
arcs[i].source=b._id; |
| 310 | 310 |
} |
| 311 | 311 |
if(connect) addArc(n,b); |
| 312 | 312 |
return b; |
| 313 | 313 |
} |
| 314 | 314 |
|
| 315 | 315 |
public: |
| 316 | 316 |
|
| 317 | 317 |
class Snapshot; |
| 318 | 318 |
|
| 319 | 319 |
protected: |
| 320 | 320 |
|
| 321 | 321 |
void restoreSnapshot(const Snapshot &s) |
| 322 | 322 |
{
|
| 323 | 323 |
while(s.arc_num<arcs.size()) {
|
| 324 | 324 |
Arc arc = arcFromId(arcs.size()-1); |
| 325 | 325 |
Parent::notifier(Arc()).erase(arc); |
| 326 | 326 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 327 | 327 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 328 | 328 |
arcs.pop_back(); |
| 329 | 329 |
} |
| 330 | 330 |
while(s.node_num<nodes.size()) {
|
| 331 | 331 |
Node node = nodeFromId(nodes.size()-1); |
| 332 | 332 |
Parent::notifier(Node()).erase(node); |
| 333 | 333 |
nodes.pop_back(); |
| 334 | 334 |
} |
| 335 | 335 |
} |
| 336 | 336 |
|
| 337 | 337 |
public: |
| 338 | 338 |
|
| 339 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 340 | 340 |
|
| 341 | 341 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 342 | 342 |
/// |
| 343 | 343 |
///The newly added nodes and arcs can be removed using the |
| 344 | 344 |
///restore() function. |
| 345 | 345 |
///\note After you restore a state, you cannot restore |
| 346 | 346 |
///a later state, in other word you cannot add again the arcs deleted |
| 347 | 347 |
///by restore() using another one Snapshot instance. |
| 348 | 348 |
/// |
| 349 | 349 |
///\warning If you do not use correctly the snapshot that can cause |
| 350 | 350 |
///either broken program, invalid state of the digraph, valid but |
| 351 | 351 |
///not the restored digraph or no change. Because the runtime performance |
| 352 | 352 |
///the validity of the snapshot is not stored. |
| 353 | 353 |
class Snapshot |
| 354 | 354 |
{
|
| 355 | 355 |
SmartDigraph *_graph; |
| 356 | 356 |
protected: |
| 357 | 357 |
friend class SmartDigraph; |
| 358 | 358 |
unsigned int node_num; |
| 359 | 359 |
unsigned int arc_num; |
| 360 | 360 |
public: |
| 361 | 361 |
///Default constructor. |
| 362 | 362 |
|
| 363 | 363 |
///Default constructor. |
| 364 | 364 |
///To actually make a snapshot you must call save(). |
| 365 | 365 |
/// |
| 366 | 366 |
Snapshot() : _graph(0) {}
|
| 367 | 367 |
///Constructor that immediately makes a snapshot |
| 368 | 368 |
|
| 369 | 369 |
///This constructor immediately makes a snapshot of the digraph. |
| 370 | 370 |
///\param graph The digraph we make a snapshot of. |
| 371 | 371 |
Snapshot(SmartDigraph &graph) : _graph(&graph) {
|
| 372 | 372 |
node_num=_graph->nodes.size(); |
| 373 | 373 |
arc_num=_graph->arcs.size(); |
| 374 | 374 |
} |
| 375 | 375 |
|
| 376 | 376 |
///Make a snapshot. |
| 377 | 377 |
|
| 378 | 378 |
///Make a snapshot of the digraph. |
| 379 | 379 |
/// |
| 380 | 380 |
///This function can be called more than once. In case of a repeated |
| 381 | 381 |
///call, the previous snapshot gets lost. |
| 382 | 382 |
///\param graph The digraph we make the snapshot of. |
| 383 | 383 |
void save(SmartDigraph &graph) |
| 384 | 384 |
{
|
| 385 | 385 |
_graph=&graph; |
| 386 | 386 |
node_num=_graph->nodes.size(); |
| 387 | 387 |
arc_num=_graph->arcs.size(); |
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
///Undo the changes until a snapshot. |
| 391 | 391 |
|
| 392 | 392 |
///Undo the changes until a snapshot created by save(). |
| 393 | 393 |
/// |
| 394 | 394 |
///\note After you restored a state, you cannot restore |
| 395 | 395 |
///a later state, in other word you cannot add again the arcs deleted |
| 396 | 396 |
///by restore(). |
| 397 | 397 |
void restore() |
| 398 | 398 |
{
|
| 399 | 399 |
_graph->restoreSnapshot(*this); |
| 400 | 400 |
} |
| 401 | 401 |
}; |
| 402 | 402 |
}; |
| 403 | 403 |
|
| 404 | 404 |
|
| 405 | 405 |
class SmartGraphBase {
|
| 406 | 406 |
|
| 407 | 407 |
protected: |
| 408 | 408 |
|
| 409 | 409 |
struct NodeT {
|
| 410 | 410 |
int first_out; |
| 411 | 411 |
}; |
| 412 | 412 |
|
| 413 | 413 |
struct ArcT {
|
| 414 | 414 |
int target; |
| 415 | 415 |
int next_out; |
| 416 | 416 |
}; |
| 417 | 417 |
|
| 418 | 418 |
std::vector<NodeT> nodes; |
| 419 | 419 |
std::vector<ArcT> arcs; |
| 420 | 420 |
|
| 421 | 421 |
int first_free_arc; |
| 422 | 422 |
|
| 423 | 423 |
public: |
| 424 | 424 |
|
| 425 | 425 |
typedef SmartGraphBase Digraph; |
| 426 | 426 |
|
| 427 | 427 |
class Node; |
| 428 | 428 |
class Arc; |
| 429 | 429 |
class Edge; |
| 430 | 430 |
|
| 431 | 431 |
class Node {
|
| 432 | 432 |
friend class SmartGraphBase; |
| 433 | 433 |
protected: |
| 434 | 434 |
|
| 435 | 435 |
int _id; |
| 436 | 436 |
explicit Node(int id) { _id = id;}
|
| 437 | 437 |
|
| 438 | 438 |
public: |
| 439 | 439 |
Node() {}
|
| 440 | 440 |
Node (Invalid) { _id = -1; }
|
| 441 | 441 |
bool operator==(const Node& node) const {return _id == node._id;}
|
| 442 | 442 |
bool operator!=(const Node& node) const {return _id != node._id;}
|
| 443 | 443 |
bool operator<(const Node& node) const {return _id < node._id;}
|
| 444 | 444 |
}; |
| 445 | 445 |
|
| 446 | 446 |
class Edge {
|
| 447 | 447 |
friend class SmartGraphBase; |
| 448 | 448 |
protected: |
| 449 | 449 |
|
| 450 | 450 |
int _id; |
| 451 | 451 |
explicit Edge(int id) { _id = id;}
|
| 452 | 452 |
|
| 453 | 453 |
public: |
| 454 | 454 |
Edge() {}
|
| 455 | 455 |
Edge (Invalid) { _id = -1; }
|
| 456 | 456 |
bool operator==(const Edge& arc) const {return _id == arc._id;}
|
| 457 | 457 |
bool operator!=(const Edge& arc) const {return _id != arc._id;}
|
| 458 | 458 |
bool operator<(const Edge& arc) const {return _id < arc._id;}
|
| 459 | 459 |
}; |
| 460 | 460 |
|
| 461 | 461 |
class Arc {
|
| 462 | 462 |
friend class SmartGraphBase; |
| 463 | 463 |
protected: |
| 464 | 464 |
|
| 465 | 465 |
int _id; |
| 466 | 466 |
explicit Arc(int id) { _id = id;}
|
| 467 | 467 |
|
| 468 | 468 |
public: |
| 469 |
operator Edge() const {
|
|
| 470 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
|
| 469 |
operator Edge() const {
|
|
| 470 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
|
| 471 | 471 |
} |
| 472 | 472 |
|
| 473 | 473 |
Arc() {}
|
| 474 | 474 |
Arc (Invalid) { _id = -1; }
|
| 475 | 475 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 476 | 476 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 477 | 477 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 478 | 478 |
}; |
| 479 | 479 |
|
| 480 | 480 |
|
| 481 | 481 |
|
| 482 | 482 |
SmartGraphBase() |
| 483 | 483 |
: nodes(), arcs() {}
|
| 484 | 484 |
|
| 485 | 485 |
|
| 486 | 486 |
int maxNodeId() const { return nodes.size()-1; }
|
| 487 | 487 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 488 | 488 |
int maxArcId() const { return arcs.size()-1; }
|
| 489 | 489 |
|
| 490 | 490 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 491 | 491 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 492 | 492 |
|
| 493 | 493 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 494 | 494 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 495 | 495 |
|
| 496 | 496 |
static bool direction(Arc e) {
|
| 497 | 497 |
return (e._id & 1) == 1; |
| 498 | 498 |
} |
| 499 | 499 |
|
| 500 | 500 |
static Arc direct(Edge e, bool d) {
|
| 501 | 501 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
| 502 | 502 |
} |
| 503 | 503 |
|
| 504 | 504 |
void first(Node& node) const {
|
| 505 | 505 |
node._id = nodes.size() - 1; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
void next(Node& node) const {
|
| 509 | 509 |
--node._id; |
| 510 | 510 |
} |
| 511 | 511 |
|
| 512 | 512 |
void first(Arc& arc) const {
|
| 513 | 513 |
arc._id = arcs.size() - 1; |
| 514 | 514 |
} |
| 515 | 515 |
|
| 516 | 516 |
void next(Arc& arc) const {
|
| 517 | 517 |
--arc._id; |
| 518 | 518 |
} |
| 519 | 519 |
|
| 520 | 520 |
void first(Edge& arc) const {
|
| 521 | 521 |
arc._id = arcs.size() / 2 - 1; |
| 522 | 522 |
} |
| 523 | 523 |
|
| 524 | 524 |
void next(Edge& arc) const {
|
| 525 | 525 |
--arc._id; |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
void firstOut(Arc &arc, const Node& v) const {
|
| 529 | 529 |
arc._id = nodes[v._id].first_out; |
| 530 | 530 |
} |
| 531 | 531 |
void nextOut(Arc &arc) const {
|
| 532 | 532 |
arc._id = arcs[arc._id].next_out; |
| 533 | 533 |
} |
| 534 | 534 |
|
| 535 | 535 |
void firstIn(Arc &arc, const Node& v) const {
|
| 536 | 536 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
| 537 | 537 |
if (arc._id == -2) arc._id = -1; |
| 538 | 538 |
} |
| 539 | 539 |
void nextIn(Arc &arc) const {
|
| 540 | 540 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
| 541 | 541 |
if (arc._id == -2) arc._id = -1; |
| 542 | 542 |
} |
| 543 | 543 |
|
| 544 | 544 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 545 | 545 |
int de = nodes[v._id].first_out; |
| 546 | 546 |
if (de != -1) {
|
| 547 | 547 |
arc._id = de / 2; |
| 548 | 548 |
d = ((de & 1) == 1); |
| 549 | 549 |
} else {
|
| 550 | 550 |
arc._id = -1; |
| 551 | 551 |
d = true; |
| 552 | 552 |
} |
| 553 | 553 |
} |
| 554 | 554 |
void nextInc(Edge &arc, bool& d) const {
|
| 555 | 555 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 556 | 556 |
if (de != -1) {
|
| 557 | 557 |
arc._id = de / 2; |
| 558 | 558 |
d = ((de & 1) == 1); |
| 559 | 559 |
} else {
|
| 560 | 560 |
arc._id = -1; |
| 561 | 561 |
d = true; |
| 562 | 562 |
} |
| 563 | 563 |
} |
| 564 | 564 |
|
| 565 | 565 |
static int id(Node v) { return v._id; }
|
| 566 | 566 |
static int id(Arc e) { return e._id; }
|
| 567 | 567 |
static int id(Edge e) { return e._id; }
|
| 568 | 568 |
|
| 569 | 569 |
static Node nodeFromId(int id) { return Node(id);}
|
| 570 | 570 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 571 | 571 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 572 | 572 |
|
| 573 | 573 |
bool valid(Node n) const {
|
| 574 | 574 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 575 | 575 |
} |
| 576 | 576 |
bool valid(Arc a) const {
|
| 577 | 577 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 578 | 578 |
} |
| 579 | 579 |
bool valid(Edge e) const {
|
| 580 | 580 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
| 581 | 581 |
} |
| 582 | 582 |
|
| 583 | 583 |
Node addNode() {
|
| 584 | 584 |
int n = nodes.size(); |
| 585 | 585 |
nodes.push_back(NodeT()); |
| 586 | 586 |
nodes[n].first_out = -1; |
| 587 | 587 |
|
| 588 | 588 |
return Node(n); |
| 589 | 589 |
} |
| 590 | 590 |
|
| 591 | 591 |
Edge addEdge(Node u, Node v) {
|
| 592 | 592 |
int n = arcs.size(); |
| 593 | 593 |
arcs.push_back(ArcT()); |
| 594 | 594 |
arcs.push_back(ArcT()); |
| 595 | 595 |
|
| 596 | 596 |
arcs[n].target = u._id; |
| 597 | 597 |
arcs[n | 1].target = v._id; |
| 598 | 598 |
|
| 599 | 599 |
arcs[n].next_out = nodes[v._id].first_out; |
| 600 | 600 |
nodes[v._id].first_out = n; |
| 601 | 601 |
|
| 602 | 602 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
| 603 | 603 |
nodes[u._id].first_out = (n | 1); |
| 604 | 604 |
|
| 605 | 605 |
return Edge(n / 2); |
| 606 | 606 |
} |
| 607 | 607 |
|
| 608 | 608 |
void clear() {
|
| 609 | 609 |
arcs.clear(); |
| 610 | 610 |
nodes.clear(); |
| 611 | 611 |
} |
| 612 | 612 |
|
| 613 | 613 |
}; |
| 614 | 614 |
|
| 615 | 615 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
| 616 | 616 |
|
| 617 | 617 |
/// \ingroup graphs |
| 618 | 618 |
/// |
| 619 | 619 |
/// \brief A smart undirected graph class. |
| 620 | 620 |
/// |
| 621 | 621 |
/// This is a simple and fast graph implementation. |
| 622 | 622 |
/// It is also quite memory efficient, but at the price |
| 623 | 623 |
/// that <b> it does support only limited (only stack-like) |
| 624 | 624 |
/// node and arc deletions</b>. |
| 625 | 625 |
/// Except from this it conforms to |
| 626 | 626 |
/// the \ref concepts::Graph "Graph concept". |
| 627 | 627 |
/// |
| 628 | 628 |
/// It also has an |
| 629 | 629 |
/// important extra feature that |
| 630 | 630 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
| 631 | 631 |
/// |
| 632 | 632 |
/// \sa concepts::Graph. |
| 633 | 633 |
/// |
| 634 | 634 |
class SmartGraph : public ExtendedSmartGraphBase {
|
| 635 | 635 |
private: |
| 636 | 636 |
|
| 637 | 637 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 638 | 638 |
|
| 639 | 639 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 640 | 640 |
/// |
| 641 | 641 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
|
| 642 | 642 |
|
| 643 | 643 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
| 644 | 644 |
///Use GraphCopy() instead. |
| 645 | 645 |
|
| 646 | 646 |
///Assignment of SmartGraph to another one is \e not allowed. |
| 647 | 647 |
///Use GraphCopy() instead. |
| 648 | 648 |
void operator=(const SmartGraph &) {}
|
| 649 | 649 |
|
| 650 | 650 |
public: |
| 651 | 651 |
|
| 652 | 652 |
typedef ExtendedSmartGraphBase Parent; |
| 653 | 653 |
|
| 654 | 654 |
/// Constructor |
| 655 | 655 |
|
| 656 | 656 |
/// Constructor. |
| 657 | 657 |
/// |
| 658 | 658 |
SmartGraph() {}
|
| 659 | 659 |
|
| 660 | 660 |
///Add a new node to the graph. |
| 661 | 661 |
|
| 662 | 662 |
/// \return the new node. |
| 663 | 663 |
/// |
| 664 | 664 |
Node addNode() { return Parent::addNode(); }
|
| 665 | 665 |
|
| 666 | 666 |
///Add a new edge to the graph. |
| 667 | 667 |
|
| 668 | 668 |
///Add a new edge to the graph with node \c s |
| 669 | 669 |
///and \c t. |
| 670 | 670 |
///\return the new edge. |
| 671 | 671 |
Edge addEdge(const Node& s, const Node& t) {
|
| 672 | 672 |
return Parent::addEdge(s, t); |
| 673 | 673 |
} |
| 674 | 674 |
|
| 675 | 675 |
/// \brief Node validity check |
| 676 | 676 |
/// |
| 677 | 677 |
/// This function gives back true if the given node is valid, |
| 678 | 678 |
/// ie. it is a real node of the graph. |
| 679 | 679 |
/// |
| 680 | 680 |
/// \warning A removed node (using Snapshot) could become valid again |
| 681 | 681 |
/// when new nodes are added to the graph. |
| 682 | 682 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 683 | 683 |
|
| 684 | 684 |
/// \brief Arc validity check |
| 685 | 685 |
/// |
| 686 | 686 |
/// This function gives back true if the given arc is valid, |
| 687 | 687 |
/// ie. it is a real arc of the graph. |
| 688 | 688 |
/// |
| 689 | 689 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 690 | 690 |
/// when new edges are added to the graph. |
| 691 | 691 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 692 | 692 |
|
| 693 | 693 |
/// \brief Edge validity check |
| 694 | 694 |
/// |
| 695 | 695 |
/// This function gives back true if the given edge is valid, |
| 696 | 696 |
/// ie. it is a real edge of the graph. |
| 697 | 697 |
/// |
| 698 | 698 |
/// \warning A removed edge (using Snapshot) could become valid again |
| 699 | 699 |
/// when new edges are added to the graph. |
| 700 | 700 |
bool valid(Edge e) const { return Parent::valid(e); }
|
| 701 | 701 |
|
| 702 | 702 |
///Clear the graph. |
| 703 | 703 |
|
| 704 | 704 |
///Erase all the nodes and edges from the graph. |
| 705 | 705 |
/// |
| 706 | 706 |
void clear() {
|
| 707 | 707 |
Parent::clear(); |
| 708 | 708 |
} |
| 709 | 709 |
|
| 710 | 710 |
public: |
| 711 | 711 |
|
| 712 | 712 |
class Snapshot; |
| 713 | 713 |
|
| 714 | 714 |
protected: |
| 715 | 715 |
|
| 716 | 716 |
void saveSnapshot(Snapshot &s) |
| 717 | 717 |
{
|
| 718 | 718 |
s._graph = this; |
| 719 | 719 |
s.node_num = nodes.size(); |
| 720 | 720 |
s.arc_num = arcs.size(); |
| 721 | 721 |
} |
| 722 | 722 |
|
| 723 | 723 |
void restoreSnapshot(const Snapshot &s) |
| 724 | 724 |
{
|
| 725 | 725 |
while(s.arc_num<arcs.size()) {
|
| 726 | 726 |
int n=arcs.size()-1; |
| 727 | 727 |
Edge arc=edgeFromId(n/2); |
| 728 | 728 |
Parent::notifier(Edge()).erase(arc); |
| 729 | 729 |
std::vector<Arc> dir; |
| 730 | 730 |
dir.push_back(arcFromId(n)); |
| 731 | 731 |
dir.push_back(arcFromId(n-1)); |
| 732 | 732 |
Parent::notifier(Arc()).erase(dir); |
| 733 | 733 |
nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
| 734 | 734 |
nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
| 735 | 735 |
arcs.pop_back(); |
| 736 | 736 |
arcs.pop_back(); |
| 737 | 737 |
} |
| 738 | 738 |
while(s.node_num<nodes.size()) {
|
| 739 | 739 |
int n=nodes.size()-1; |
| 740 | 740 |
Node node = nodeFromId(n); |
| 741 | 741 |
Parent::notifier(Node()).erase(node); |
| 742 | 742 |
nodes.pop_back(); |
| 743 | 743 |
} |
| 744 | 744 |
} |
| 745 | 745 |
|
| 746 | 746 |
public: |
| 747 | 747 |
|
| 748 | 748 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 749 | 749 |
|
| 750 | 750 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 751 | 751 |
/// |
| 752 | 752 |
///The newly added nodes and arcs can be removed using the |
| 753 | 753 |
///restore() function. |
| 754 | 754 |
/// |
| 755 | 755 |
///\note After you restore a state, you cannot restore |
| 756 | 756 |
///a later state, in other word you cannot add again the arcs deleted |
| 757 | 757 |
///by restore() using another one Snapshot instance. |
| 758 | 758 |
/// |
| 759 | 759 |
///\warning If you do not use correctly the snapshot that can cause |
| 760 | 760 |
///either broken program, invalid state of the digraph, valid but |
| 761 | 761 |
///not the restored digraph or no change. Because the runtime performance |
| 762 | 762 |
///the validity of the snapshot is not stored. |
| 763 | 763 |
class Snapshot |
| 764 | 764 |
{
|
| 765 | 765 |
SmartGraph *_graph; |
| 766 | 766 |
protected: |
| 767 | 767 |
friend class SmartGraph; |
| 768 | 768 |
unsigned int node_num; |
| 769 | 769 |
unsigned int arc_num; |
| 770 | 770 |
public: |
| 771 | 771 |
///Default constructor. |
| 772 | 772 |
|
| 773 | 773 |
///Default constructor. |
| 774 | 774 |
///To actually make a snapshot you must call save(). |
| 775 | 775 |
/// |
| 776 | 776 |
Snapshot() : _graph(0) {}
|
| 777 | 777 |
///Constructor that immediately makes a snapshot |
| 778 | 778 |
|
| 779 | 779 |
///This constructor immediately makes a snapshot of the digraph. |
| 780 | 780 |
///\param graph The digraph we make a snapshot of. |
| 781 | 781 |
Snapshot(SmartGraph &graph) {
|
| 782 | 782 |
graph.saveSnapshot(*this); |
| 783 | 783 |
} |
| 784 | 784 |
|
| 785 | 785 |
///Make a snapshot. |
| 786 | 786 |
|
| 787 | 787 |
///Make a snapshot of the graph. |
| 788 | 788 |
/// |
| 789 | 789 |
///This function can be called more than once. In case of a repeated |
| 790 | 790 |
///call, the previous snapshot gets lost. |
| 791 | 791 |
///\param graph The digraph we make the snapshot of. |
| 792 | 792 |
void save(SmartGraph &graph) |
| 793 | 793 |
{
|
| 794 | 794 |
graph.saveSnapshot(*this); |
| 795 | 795 |
} |
| 796 | 796 |
|
| 797 | 797 |
///Undo the changes until a snapshot. |
| 798 | 798 |
|
| 799 | 799 |
///Undo the changes until a snapshot created by save(). |
| 800 | 800 |
/// |
| 801 | 801 |
///\note After you restored a state, you cannot restore |
| 802 | 802 |
///a later state, in other word you cannot add again the arcs deleted |
| 803 | 803 |
///by restore(). |
| 804 | 804 |
void restore() |
| 805 | 805 |
{
|
| 806 | 806 |
_graph->restoreSnapshot(*this); |
| 807 | 807 |
} |
| 808 | 808 |
}; |
| 809 | 809 |
}; |
| 810 | 810 |
|
| 811 | 811 |
} //namespace lemon |
| 812 | 812 |
|
| 813 | 813 |
|
| 814 | 814 |
#endif //LEMON_SMART_GRAPH_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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
#include <lemon/bits/windows.h> |
| 28 | 28 |
#else |
| 29 | 29 |
#include <unistd.h> |
| 30 | 30 |
#include <sys/times.h> |
| 31 | 31 |
#include <sys/time.h> |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
#include <string> |
| 35 | 35 |
#include <fstream> |
| 36 | 36 |
#include <iostream> |
| 37 | 37 |
|
| 38 | 38 |
namespace lemon {
|
| 39 | 39 |
|
| 40 | 40 |
/// \addtogroup timecount |
| 41 | 41 |
/// @{
|
| 42 | 42 |
|
| 43 | 43 |
/// A class to store (cpu)time instances. |
| 44 | 44 |
|
| 45 | 45 |
/// This class stores five time values. |
| 46 | 46 |
/// - a real time |
| 47 | 47 |
/// - a user cpu time |
| 48 | 48 |
/// - a system cpu time |
| 49 | 49 |
/// - a user cpu time of children |
| 50 | 50 |
/// - a system cpu time of children |
| 51 | 51 |
/// |
| 52 | 52 |
/// TimeStamp's can be added to or substracted from each other and |
| 53 | 53 |
/// they can be pushed to a stream. |
| 54 | 54 |
/// |
| 55 | 55 |
/// In most cases, perhaps the \ref Timer or the \ref TimeReport |
| 56 | 56 |
/// class is what you want to use instead. |
| 57 | 57 |
|
| 58 | 58 |
class TimeStamp |
| 59 | 59 |
{
|
| 60 | 60 |
double utime; |
| 61 | 61 |
double stime; |
| 62 | 62 |
double cutime; |
| 63 | 63 |
double cstime; |
| 64 | 64 |
double rtime; |
| 65 | 65 |
|
| 66 | 66 |
void _reset() {
|
| 67 | 67 |
utime = stime = cutime = cstime = rtime = 0; |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
|
| 72 | 72 |
///Read the current time values of the process |
| 73 | 73 |
void stamp() |
| 74 | 74 |
{
|
| 75 | 75 |
#ifndef WIN32 |
| 76 | 76 |
timeval tv; |
| 77 | 77 |
gettimeofday(&tv, 0); |
| 78 | 78 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
| 79 | 79 |
|
| 80 | 80 |
tms ts; |
| 81 | 81 |
double tck=sysconf(_SC_CLK_TCK); |
| 82 | 82 |
times(&ts); |
| 83 | 83 |
utime=ts.tms_utime/tck; |
| 84 | 84 |
stime=ts.tms_stime/tck; |
| 85 | 85 |
cutime=ts.tms_cutime/tck; |
| 86 | 86 |
cstime=ts.tms_cstime/tck; |
| 87 | 87 |
#else |
| 88 | 88 |
bits::getWinProcTimes(rtime, utime, stime, cutime, cstime); |
| 89 | 89 |
#endif |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
/// Constructor initializing with zero |
| 93 | 93 |
TimeStamp() |
| 94 | 94 |
{ _reset(); }
|
| 95 | 95 |
///Constructor initializing with the current time values of the process |
| 96 | 96 |
TimeStamp(void *) { stamp();}
|
| 97 | 97 |
|
| 98 | 98 |
///Set every time value to zero |
| 99 | 99 |
TimeStamp &reset() {_reset();return *this;}
|
| 100 | 100 |
|
| 101 | 101 |
///\e |
| 102 | 102 |
TimeStamp &operator+=(const TimeStamp &b) |
| 103 | 103 |
{
|
| 104 | 104 |
utime+=b.utime; |
| 105 | 105 |
stime+=b.stime; |
| 106 | 106 |
cutime+=b.cutime; |
| 107 | 107 |
cstime+=b.cstime; |
| 108 | 108 |
rtime+=b.rtime; |
| 109 | 109 |
return *this; |
| 110 | 110 |
} |
| 111 | 111 |
///\e |
| 112 | 112 |
TimeStamp operator+(const TimeStamp &b) const |
| 113 | 113 |
{
|
| 114 | 114 |
TimeStamp t(*this); |
| 115 | 115 |
return t+=b; |
| 116 | 116 |
} |
| 117 | 117 |
///\e |
| 118 | 118 |
TimeStamp &operator-=(const TimeStamp &b) |
| 119 | 119 |
{
|
| 120 | 120 |
utime-=b.utime; |
| 121 | 121 |
stime-=b.stime; |
| 122 | 122 |
cutime-=b.cutime; |
| 123 | 123 |
cstime-=b.cstime; |
| 124 | 124 |
rtime-=b.rtime; |
| 125 | 125 |
return *this; |
| 126 | 126 |
} |
| 127 | 127 |
///\e |
| 128 | 128 |
TimeStamp operator-(const TimeStamp &b) const |
| 129 | 129 |
{
|
| 130 | 130 |
TimeStamp t(*this); |
| 131 | 131 |
return t-=b; |
| 132 | 132 |
} |
| 133 | 133 |
///\e |
| 134 | 134 |
TimeStamp &operator*=(double b) |
| 135 | 135 |
{
|
| 136 | 136 |
utime*=b; |
| 137 | 137 |
stime*=b; |
| 138 | 138 |
cutime*=b; |
| 139 | 139 |
cstime*=b; |
| 140 | 140 |
rtime*=b; |
| 141 | 141 |
return *this; |
| 142 | 142 |
} |
| 143 | 143 |
///\e |
| 144 | 144 |
TimeStamp operator*(double b) const |
| 145 | 145 |
{
|
| 146 | 146 |
TimeStamp t(*this); |
| 147 | 147 |
return t*=b; |
| 148 | 148 |
} |
| 149 | 149 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
| 150 | 150 |
///\e |
| 151 | 151 |
TimeStamp &operator/=(double b) |
| 152 | 152 |
{
|
| 153 | 153 |
utime/=b; |
| 154 | 154 |
stime/=b; |
| 155 | 155 |
cutime/=b; |
| 156 | 156 |
cstime/=b; |
| 157 | 157 |
rtime/=b; |
| 158 | 158 |
return *this; |
| 159 | 159 |
} |
| 160 | 160 |
///\e |
| 161 | 161 |
TimeStamp operator/(double b) const |
| 162 | 162 |
{
|
| 163 | 163 |
TimeStamp t(*this); |
| 164 | 164 |
return t/=b; |
| 165 | 165 |
} |
| 166 | 166 |
///The time ellapsed since the last call of stamp() |
| 167 | 167 |
TimeStamp ellapsed() const |
| 168 | 168 |
{
|
| 169 | 169 |
TimeStamp t(NULL); |
| 170 | 170 |
return t-*this; |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
| 174 | 174 |
|
| 175 | 175 |
///Gives back the user time of the process |
| 176 | 176 |
double userTime() const |
| 177 | 177 |
{
|
| 178 | 178 |
return utime; |
| 179 | 179 |
} |
| 180 | 180 |
///Gives back the system time of the process |
| 181 | 181 |
double systemTime() const |
| 182 | 182 |
{
|
| 183 | 183 |
return stime; |
| 184 | 184 |
} |
| 185 | 185 |
///Gives back the user time of the process' children |
| 186 | 186 |
|
| 187 | 187 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 188 | 188 |
/// |
| 189 | 189 |
double cUserTime() const |
| 190 | 190 |
{
|
| 191 | 191 |
return cutime; |
| 192 | 192 |
} |
| 193 | 193 |
///Gives back the user time of the process' children |
| 194 | 194 |
|
| 195 | 195 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 196 | 196 |
/// |
| 197 | 197 |
double cSystemTime() const |
| 198 | 198 |
{
|
| 199 | 199 |
return cstime; |
| 200 | 200 |
} |
| 201 | 201 |
///Gives back the real time |
| 202 | 202 |
double realTime() const {return rtime;}
|
| 203 | 203 |
}; |
| 204 | 204 |
|
| 205 | 205 |
inline TimeStamp operator*(double b,const TimeStamp &t) |
| 206 | 206 |
{
|
| 207 | 207 |
return t*b; |
| 208 | 208 |
} |
| 209 | 209 |
|
| 210 | 210 |
///Prints the time counters |
| 211 | 211 |
|
| 212 | 212 |
///Prints the time counters in the following form: |
| 213 | 213 |
/// |
| 214 | 214 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
| 215 | 215 |
/// |
| 216 | 216 |
/// where the values are the |
| 217 | 217 |
/// \li \c u: user cpu time, |
| 218 | 218 |
/// \li \c s: system cpu time, |
| 219 | 219 |
/// \li \c cu: user cpu time of children, |
| 220 | 220 |
/// \li \c cs: system cpu time of children, |
| 221 | 221 |
/// \li \c real: real time. |
| 222 | 222 |
/// \relates TimeStamp |
| 223 | 223 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
| 224 | 224 |
/// calculated. |
| 225 | 225 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
| 226 | 226 |
{
|
| 227 | 227 |
os << "u: " << t.userTime() << |
| 228 | 228 |
"s, s: " << t.systemTime() << |
| 229 | 229 |
"s, cu: " << t.cUserTime() << |
| 230 | 230 |
"s, cs: " << t.cSystemTime() << |
| 231 | 231 |
"s, real: " << t.realTime() << "s"; |
| 232 | 232 |
return os; |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 | 235 |
///Class for measuring the cpu time and real time usage of the process |
| 236 | 236 |
|
| 237 | 237 |
///Class for measuring the cpu time and real time usage of the process. |
| 238 | 238 |
///It is quite easy-to-use, here is a short example. |
| 239 | 239 |
///\code |
| 240 | 240 |
/// #include<lemon/time_measure.h> |
| 241 | 241 |
/// #include<iostream> |
| 242 | 242 |
/// |
| 243 | 243 |
/// int main() |
| 244 | 244 |
/// {
|
| 245 | 245 |
/// |
| 246 | 246 |
/// ... |
| 247 | 247 |
/// |
| 248 | 248 |
/// Timer t; |
| 249 | 249 |
/// doSomething(); |
| 250 | 250 |
/// std::cout << t << '\n'; |
| 251 | 251 |
/// t.restart(); |
| 252 | 252 |
/// doSomethingElse(); |
| 253 | 253 |
/// std::cout << t << '\n'; |
| 254 | 254 |
/// |
| 255 | 255 |
/// ... |
| 256 | 256 |
/// |
| 257 | 257 |
/// } |
| 258 | 258 |
///\endcode |
| 259 | 259 |
/// |
| 260 | 260 |
///The \ref Timer can also be \ref stop() "stopped" and |
| 261 | 261 |
///\ref start() "started" again, so it is possible to compute collected |
| 262 | 262 |
///running times. |
| 263 | 263 |
/// |
| 264 | 264 |
///\warning Depending on the operation system and its actual configuration |
| 265 | 265 |
///the time counters have a certain (10ms on a typical Linux system) |
| 266 | 266 |
///granularity. |
| 267 | 267 |
///Therefore this tool is not appropriate to measure very short times. |
| 268 | 268 |
///Also, if you start and stop the timer very frequently, it could lead to |
| 269 | 269 |
///distorted results. |
| 270 | 270 |
/// |
| 271 | 271 |
///\note If you want to measure the running time of the execution of a certain |
| 272 | 272 |
///function, consider the usage of \ref TimeReport instead. |
| 273 | 273 |
/// |
| 274 | 274 |
///\sa TimeReport |
| 275 | 275 |
class Timer |
| 276 | 276 |
{
|
| 277 | 277 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
| 278 | 278 |
TimeStamp start_time; //This is the relativ start-time if the timer |
| 279 | 279 |
//is _running, the collected _running time otherwise. |
| 280 | 280 |
|
| 281 | 281 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
|
| 282 | 282 |
|
| 283 | 283 |
public: |
| 284 | 284 |
///Constructor. |
| 285 | 285 |
|
| 286 | 286 |
///\param run indicates whether or not the timer starts immediately. |
| 287 | 287 |
/// |
| 288 | 288 |
Timer(bool run=true) :_running(run) {_reset();}
|
| 289 | 289 |
|
| 290 | 290 |
///\name Control the state of the timer |
| 291 | 291 |
///Basically a Timer can be either running or stopped, |
| 292 | 292 |
///but it provides a bit finer control on the execution. |
| 293 | 293 |
///The \ref lemon::Timer "Timer" also counts the number of |
| 294 | 294 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
| 295 | 295 |
///only after the same amount (or more) \ref lemon::Timer::stop() |
| 296 | 296 |
///"stop()"s. This can be useful e.g. to compute the running time |
| 297 | 297 |
///of recursive functions. |
| 298 | 298 |
|
| 299 | 299 |
///@{
|
| 300 | 300 |
|
| 301 | 301 |
///Reset and stop the time counters |
| 302 | 302 |
|
| 303 | 303 |
///This function resets and stops the time counters |
| 304 | 304 |
///\sa restart() |
| 305 | 305 |
void reset() |
| 306 | 306 |
{
|
| 307 | 307 |
_running=0; |
| 308 | 308 |
_reset(); |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
///Start the time counters |
| 312 | 312 |
|
| 313 | 313 |
///This function starts the time counters. |
| 314 | 314 |
/// |
| 315 | 315 |
///If the timer is started more than ones, it will remain running |
| 316 | 316 |
///until the same amount of \ref stop() is called. |
| 317 | 317 |
///\sa stop() |
| 318 | 318 |
void start() |
| 319 | 319 |
{
|
| 320 | 320 |
if(_running) _running++; |
| 321 | 321 |
else {
|
| 322 | 322 |
_running=1; |
| 323 | 323 |
TimeStamp t; |
| 324 | 324 |
t.stamp(); |
| 325 | 325 |
start_time=t-start_time; |
| 326 | 326 |
} |
| 327 | 327 |
} |
| 328 | 328 |
|
| 329 | 329 |
|
| 330 | 330 |
///Stop the time counters |
| 331 | 331 |
|
| 332 | 332 |
///This function stops the time counters. If start() was executed more than |
| 333 | 333 |
///once, then the same number of stop() execution is necessary the really |
| 334 | 334 |
///stop the timer. |
| 335 | 335 |
/// |
| 336 | 336 |
///\sa halt() |
| 337 | 337 |
///\sa start() |
| 338 | 338 |
///\sa restart() |
| 339 | 339 |
///\sa reset() |
| 340 | 340 |
|
| 341 | 341 |
void stop() |
| 342 | 342 |
{
|
| 343 | 343 |
if(_running && !--_running) {
|
| 344 | 344 |
TimeStamp t; |
| 345 | 345 |
t.stamp(); |
| 346 | 346 |
start_time=t-start_time; |
| 347 | 347 |
} |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
///Halt (i.e stop immediately) the time counters |
| 351 | 351 |
|
| 352 | 352 |
///This function stops immediately the time counters, i.e. <tt>t.halt()</tt> |
| 353 | 353 |
///is a faster |
| 354 | 354 |
///equivalent of the following. |
| 355 | 355 |
///\code |
| 356 | 356 |
/// while(t.running()) t.stop() |
| 357 | 357 |
///\endcode |
| 358 | 358 |
/// |
| 359 | 359 |
/// |
| 360 | 360 |
///\sa stop() |
| 361 | 361 |
///\sa restart() |
| 362 | 362 |
///\sa reset() |
| 363 | 363 |
|
| 364 | 364 |
void halt() |
| 365 | 365 |
{
|
| 366 | 366 |
if(_running) {
|
| 367 | 367 |
_running=0; |
| 368 | 368 |
TimeStamp t; |
| 369 | 369 |
t.stamp(); |
| 370 | 370 |
start_time=t-start_time; |
| 371 | 371 |
} |
| 372 | 372 |
} |
| 373 | 373 |
|
| 374 | 374 |
///Returns the running state of the timer |
| 375 | 375 |
|
| 376 | 376 |
///This function returns the number of stop() exections that is |
| 377 | 377 |
///necessary to really stop the timer. |
| 378 | 378 |
///For example the timer |
| 379 | 379 |
///is running if and only if the return value is \c true |
| 380 | 380 |
///(i.e. greater than |
| 381 | 381 |
///zero). |
| 382 | 382 |
int running() { return _running; }
|
| 383 | 383 |
|
| 384 | 384 |
|
| 385 | 385 |
///Restart the time counters |
| 386 | 386 |
|
| 387 | 387 |
///This function is a shorthand for |
| 388 | 388 |
///a reset() and a start() calls. |
| 389 | 389 |
/// |
| 390 | 390 |
void restart() |
| 391 | 391 |
{
|
| 392 | 392 |
reset(); |
| 393 | 393 |
start(); |
| 394 | 394 |
} |
| 395 | 395 |
|
| 396 | 396 |
///@} |
| 397 | 397 |
|
| 398 | 398 |
///\name Query Functions for the ellapsed time |
| 399 | 399 |
|
| 400 | 400 |
///@{
|
| 401 | 401 |
|
| 402 | 402 |
///Gives back the ellapsed user time of the process |
| 403 | 403 |
double userTime() const |
| 404 | 404 |
{
|
| 405 | 405 |
return operator TimeStamp().userTime(); |
| 406 | 406 |
} |
| 407 | 407 |
///Gives back the ellapsed system time of the process |
| 408 | 408 |
double systemTime() const |
| 409 | 409 |
{
|
| 410 | 410 |
return operator TimeStamp().systemTime(); |
| 411 | 411 |
} |
| 412 | 412 |
///Gives back the ellapsed user time of the process' children |
| 413 | 413 |
|
| 414 | 414 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 415 | 415 |
/// |
| 416 | 416 |
double cUserTime() const |
| 417 | 417 |
{
|
| 418 | 418 |
return operator TimeStamp().cUserTime(); |
| 419 | 419 |
} |
| 420 | 420 |
///Gives back the ellapsed user time of the process' children |
| 421 | 421 |
|
| 422 | 422 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 423 | 423 |
/// |
| 424 | 424 |
double cSystemTime() const |
| 425 | 425 |
{
|
| 426 | 426 |
return operator TimeStamp().cSystemTime(); |
| 427 | 427 |
} |
| 428 | 428 |
///Gives back the ellapsed real time |
| 429 | 429 |
double realTime() const |
| 430 | 430 |
{
|
| 431 | 431 |
return operator TimeStamp().realTime(); |
| 432 | 432 |
} |
| 433 | 433 |
///Computes the ellapsed time |
| 434 | 434 |
|
| 435 | 435 |
///This conversion computes the ellapsed time, therefore you can print |
| 436 | 436 |
///the ellapsed time like this. |
| 437 | 437 |
///\code |
| 438 | 438 |
/// Timer t; |
| 439 | 439 |
/// doSomething(); |
| 440 | 440 |
/// std::cout << t << '\n'; |
| 441 | 441 |
///\endcode |
| 442 | 442 |
operator TimeStamp () const |
| 443 | 443 |
{
|
| 444 | 444 |
TimeStamp t; |
| 445 | 445 |
t.stamp(); |
| 446 | 446 |
return _running?t-start_time:start_time; |
| 447 | 447 |
} |
| 448 | 448 |
|
| 449 | 449 |
|
| 450 | 450 |
///@} |
| 451 | 451 |
}; |
| 452 | 452 |
|
| 453 | 453 |
///Same as Timer but prints a report on destruction. |
| 454 | 454 |
|
| 455 | 455 |
///Same as \ref Timer but prints a report on destruction. |
| 456 | 456 |
///This example shows its usage. |
| 457 | 457 |
///\code |
| 458 | 458 |
/// void myAlg(ListGraph &g,int n) |
| 459 | 459 |
/// {
|
| 460 | 460 |
/// TimeReport tr("Running time of myAlg: ");
|
| 461 | 461 |
/// ... //Here comes the algorithm |
| 462 | 462 |
/// } |
| 463 | 463 |
///\endcode |
| 464 | 464 |
/// |
| 465 | 465 |
///\sa Timer |
| 466 | 466 |
///\sa NoTimeReport |
| 467 | 467 |
class TimeReport : public Timer |
| 468 | 468 |
{
|
| 469 | 469 |
std::string _title; |
| 470 | 470 |
std::ostream &_os; |
| 471 | 471 |
public: |
| 472 | 472 |
///Constructor |
| 473 | 473 |
|
| 474 | 474 |
///Constructor. |
| 475 | 475 |
///\param title This text will be printed before the ellapsed time. |
| 476 | 476 |
///\param os The stream to print the report to. |
| 477 | 477 |
///\param run Sets whether the timer should start immediately. |
| 478 | 478 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
| 479 | 479 |
: Timer(run), _title(title), _os(os){}
|
| 480 | 480 |
///Destructor that prints the ellapsed time |
| 481 | 481 |
~TimeReport() |
| 482 | 482 |
{
|
| 483 | 483 |
_os << _title << *this << std::endl; |
| 484 | 484 |
} |
| 485 | 485 |
}; |
| 486 | 486 |
|
| 487 | 487 |
///'Do nothing' version of TimeReport |
| 488 | 488 |
|
| 489 | 489 |
///\sa TimeReport |
| 490 | 490 |
/// |
| 491 | 491 |
class NoTimeReport |
| 492 | 492 |
{
|
| 493 | 493 |
public: |
| 494 | 494 |
///\e |
| 495 | 495 |
NoTimeReport(std::string,std::ostream &,bool) {}
|
| 496 | 496 |
///\e |
| 497 | 497 |
NoTimeReport(std::string,std::ostream &) {}
|
| 498 | 498 |
///\e |
| 499 | 499 |
NoTimeReport(std::string) {}
|
| 500 | 500 |
///\e Do nothing. |
| 501 | 501 |
~NoTimeReport() {}
|
| 502 | 502 |
|
| 503 | 503 |
operator TimeStamp () const { return TimeStamp(); }
|
| 504 | 504 |
void reset() {}
|
| 505 | 505 |
void start() {}
|
| 506 | 506 |
void stop() {}
|
| 507 | 507 |
void halt() {}
|
| 508 | 508 |
int running() { return 0; }
|
| 509 | 509 |
void restart() {}
|
| 510 | 510 |
double userTime() const { return 0; }
|
| 511 | 511 |
double systemTime() const { return 0; }
|
| 512 | 512 |
double cUserTime() const { return 0; }
|
| 513 | 513 |
double cSystemTime() const { return 0; }
|
| 514 | 514 |
double realTime() const { return 0; }
|
| 515 | 515 |
}; |
| 516 | 516 |
|
| 517 | 517 |
///Tool to measure the running time more exactly. |
| 518 | 518 |
|
| 519 | 519 |
///This function calls \c f several times and returns the average |
| 520 | 520 |
///running time. The number of the executions will be choosen in such a way |
| 521 | 521 |
///that the full real running time will be roughly between \c min_time |
| 522 | 522 |
///and <tt>2*min_time</tt>. |
| 523 | 523 |
///\param f the function object to be measured. |
| 524 | 524 |
///\param min_time the minimum total running time. |
| 525 | 525 |
///\retval num if it is not \c NULL, then the actual |
| 526 | 526 |
/// number of execution of \c f will be written into <tt>*num</tt>. |
| 527 | 527 |
///\retval full_time if it is not \c NULL, then the actual |
| 528 | 528 |
/// total running time will be written into <tt>*full_time</tt>. |
| 529 | 529 |
///\return The average running time of \c f. |
| 530 | 530 |
|
| 531 | 531 |
template<class F> |
| 532 | 532 |
TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
| 533 | 533 |
TimeStamp *full_time=NULL) |
| 534 | 534 |
{
|
| 535 | 535 |
TimeStamp full; |
| 536 | 536 |
unsigned int total=0; |
| 537 | 537 |
Timer t; |
| 538 | 538 |
for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
|
| 539 | 539 |
for(;total<tn;total++) f(); |
| 540 | 540 |
full=t; |
| 541 | 541 |
} |
| 542 | 542 |
if(num) *num=total; |
| 543 | 543 |
if(full_time) *full_time=full; |
| 544 | 544 |
return full/total; |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 | 547 |
/// @} |
| 548 | 548 |
|
| 549 | 549 |
|
| 550 | 550 |
} //namespace lemon |
| 551 | 551 |
|
| 552 | 552 |
#endif //LEMON_TIME_MEASURE_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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_TOLERANCE_H |
| 20 | 20 |
#define LEMON_TOLERANCE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief A basic tool to handle the anomalies of calculation with |
| 25 | 25 |
///floating point numbers. |
| 26 | 26 |
/// |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
/// \addtogroup misc |
| 31 | 31 |
/// @{
|
| 32 | 32 |
|
| 33 | 33 |
///\brief A class to provide a basic way to |
| 34 | 34 |
///handle the comparison of numbers that are obtained |
| 35 | 35 |
///as a result of a probably inexact computation. |
| 36 | 36 |
/// |
| 37 | 37 |
///\ref Tolerance is a class to provide a basic way to |
| 38 | 38 |
///handle the comparison of numbers that are obtained |
| 39 | 39 |
///as a result of a probably inexact computation. |
| 40 | 40 |
/// |
| 41 | 41 |
///The general implementation is suitable only if the data type is exact, |
| 42 | 42 |
///like the integer types, otherwise a specialized version must be |
| 43 | 43 |
///implemented. These specialized classes like |
| 44 | 44 |
///Tolerance<double> may offer additional tuning parameters. |
| 45 | 45 |
/// |
| 46 | 46 |
///\sa Tolerance<float> |
| 47 | 47 |
///\sa Tolerance<double> |
| 48 | 48 |
///\sa Tolerance<long double> |
| 49 | 49 |
|
| 50 | 50 |
template<class T> |
| 51 | 51 |
class Tolerance |
| 52 | 52 |
{
|
| 53 | 53 |
public: |
| 54 | 54 |
typedef T Value; |
| 55 | 55 |
|
| 56 | 56 |
///\name Comparisons |
| 57 | 57 |
///The concept is that these bool functions return \c true only if |
| 58 | 58 |
///the related comparisons hold even if some numerical error appeared |
| 59 | 59 |
///during the computations. |
| 60 | 60 |
|
| 61 | 61 |
///@{
|
| 62 | 62 |
|
| 63 | 63 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 64 | 64 |
static bool less(Value a,Value b) {return a<b;}
|
| 65 | 65 |
///Returns \c true if \c a is \e surely different from \c b |
| 66 | 66 |
static bool different(Value a,Value b) {return a!=b;}
|
| 67 | 67 |
///Returns \c true if \c a is \e surely positive |
| 68 | 68 |
static bool positive(Value a) {return static_cast<Value>(0) < a;}
|
| 69 | 69 |
///Returns \c true if \c a is \e surely negative |
| 70 | 70 |
static bool negative(Value a) {return a < static_cast<Value>(0);}
|
| 71 | 71 |
///Returns \c true if \c a is \e surely non-zero |
| 72 | 72 |
static bool nonZero(Value a) {return a != static_cast<Value>(0);}
|
| 73 | 73 |
|
| 74 | 74 |
///@} |
| 75 | 75 |
|
| 76 | 76 |
///Returns the zero value. |
| 77 | 77 |
static Value zero() {return static_cast<Value>(0);}
|
| 78 | 78 |
|
| 79 | 79 |
// static bool finite(Value a) {}
|
| 80 | 80 |
// static Value big() {}
|
| 81 | 81 |
// static Value negativeBig() {}
|
| 82 | 82 |
}; |
| 83 | 83 |
|
| 84 | 84 |
|
| 85 | 85 |
///Float specialization of Tolerance. |
| 86 | 86 |
|
| 87 | 87 |
///Float specialization of Tolerance. |
| 88 | 88 |
///\sa Tolerance |
| 89 | 89 |
///\relates Tolerance |
| 90 | 90 |
template<> |
| 91 | 91 |
class Tolerance<float> |
| 92 | 92 |
{
|
| 93 | 93 |
static float def_epsilon; |
| 94 | 94 |
float _epsilon; |
| 95 | 95 |
public: |
| 96 | 96 |
///\e |
| 97 | 97 |
typedef float Value; |
| 98 | 98 |
|
| 99 | 99 |
///Constructor setting the epsilon tolerance to the default value. |
| 100 | 100 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 101 | 101 |
///Constructor setting the epsilon tolerance to the given value. |
| 102 | 102 |
Tolerance(float e) : _epsilon(e) {}
|
| 103 | 103 |
|
| 104 | 104 |
///Returns the epsilon value. |
| 105 | 105 |
Value epsilon() const {return _epsilon;}
|
| 106 | 106 |
///Sets the epsilon value. |
| 107 | 107 |
void epsilon(Value e) {_epsilon=e;}
|
| 108 | 108 |
|
| 109 | 109 |
///Returns the default epsilon value. |
| 110 | 110 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 111 | 111 |
///Sets the default epsilon value. |
| 112 | 112 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 113 | 113 |
|
| 114 | 114 |
///\name Comparisons |
| 115 | 115 |
///See \ref lemon::Tolerance "Tolerance" for more details. |
| 116 | 116 |
|
| 117 | 117 |
///@{
|
| 118 | 118 |
|
| 119 | 119 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 120 | 120 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 121 | 121 |
///Returns \c true if \c a is \e surely different from \c b |
| 122 | 122 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 123 | 123 |
///Returns \c true if \c a is \e surely positive |
| 124 | 124 |
bool positive(Value a) const { return _epsilon<a; }
|
| 125 | 125 |
///Returns \c true if \c a is \e surely negative |
| 126 | 126 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 127 | 127 |
///Returns \c true if \c a is \e surely non-zero |
| 128 | 128 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 129 | 129 |
|
| 130 | 130 |
///@} |
| 131 | 131 |
|
| 132 | 132 |
///Returns zero |
| 133 | 133 |
static Value zero() {return 0;}
|
| 134 | 134 |
}; |
| 135 | 135 |
|
| 136 | 136 |
///Double specialization of Tolerance. |
| 137 | 137 |
|
| 138 | 138 |
///Double specialization of Tolerance. |
| 139 | 139 |
///\sa Tolerance |
| 140 | 140 |
///\relates Tolerance |
| 141 | 141 |
template<> |
| 142 | 142 |
class Tolerance<double> |
| 143 | 143 |
{
|
| 144 | 144 |
static double def_epsilon; |
| 145 | 145 |
double _epsilon; |
| 146 | 146 |
public: |
| 147 | 147 |
///\e |
| 148 | 148 |
typedef double Value; |
| 149 | 149 |
|
| 150 | 150 |
///Constructor setting the epsilon tolerance to the default value. |
| 151 | 151 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 152 | 152 |
///Constructor setting the epsilon tolerance to the given value. |
| 153 | 153 |
Tolerance(double e) : _epsilon(e) {}
|
| 154 | 154 |
|
| 155 | 155 |
///Returns the epsilon value. |
| 156 | 156 |
Value epsilon() const {return _epsilon;}
|
| 157 | 157 |
///Sets the epsilon value. |
| 158 | 158 |
void epsilon(Value e) {_epsilon=e;}
|
| 159 | 159 |
|
| 160 | 160 |
///Returns the default epsilon value. |
| 161 | 161 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 162 | 162 |
///Sets the default epsilon value. |
| 163 | 163 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 164 | 164 |
|
| 165 | 165 |
///\name Comparisons |
| 166 | 166 |
///See \ref lemon::Tolerance "Tolerance" for more details. |
| 167 | 167 |
|
| 168 | 168 |
///@{
|
| 169 | 169 |
|
| 170 | 170 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 171 | 171 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 172 | 172 |
///Returns \c true if \c a is \e surely different from \c b |
| 173 | 173 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 174 | 174 |
///Returns \c true if \c a is \e surely positive |
| 175 | 175 |
bool positive(Value a) const { return _epsilon<a; }
|
| 176 | 176 |
///Returns \c true if \c a is \e surely negative |
| 177 | 177 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 178 | 178 |
///Returns \c true if \c a is \e surely non-zero |
| 179 | 179 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 180 | 180 |
|
| 181 | 181 |
///@} |
| 182 | 182 |
|
| 183 | 183 |
///Returns zero |
| 184 | 184 |
static Value zero() {return 0;}
|
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
///Long double specialization of Tolerance. |
| 188 | 188 |
|
| 189 | 189 |
///Long double specialization of Tolerance. |
| 190 | 190 |
///\sa Tolerance |
| 191 | 191 |
///\relates Tolerance |
| 192 | 192 |
template<> |
| 193 | 193 |
class Tolerance<long double> |
| 194 | 194 |
{
|
| 195 | 195 |
static long double def_epsilon; |
| 196 | 196 |
long double _epsilon; |
| 197 | 197 |
public: |
| 198 | 198 |
///\e |
| 199 | 199 |
typedef long double Value; |
| 200 | 200 |
|
| 201 | 201 |
///Constructor setting the epsilon tolerance to the default value. |
| 202 | 202 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 203 | 203 |
///Constructor setting the epsilon tolerance to the given value. |
| 204 | 204 |
Tolerance(long double e) : _epsilon(e) {}
|
| 205 | 205 |
|
| 206 | 206 |
///Returns the epsilon value. |
| 207 | 207 |
Value epsilon() const {return _epsilon;}
|
| 208 | 208 |
///Sets the epsilon value. |
| 209 | 209 |
void epsilon(Value e) {_epsilon=e;}
|
| 210 | 210 |
|
| 211 | 211 |
///Returns the default epsilon value. |
| 212 | 212 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 213 | 213 |
///Sets the default epsilon value. |
| 214 | 214 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 215 | 215 |
|
| 216 | 216 |
///\name Comparisons |
| 217 | 217 |
///See \ref lemon::Tolerance "Tolerance" for more details. |
| 218 | 218 |
|
| 219 | 219 |
///@{
|
| 220 | 220 |
|
| 221 | 221 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 222 | 222 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 223 | 223 |
///Returns \c true if \c a is \e surely different from \c b |
| 224 | 224 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 225 | 225 |
///Returns \c true if \c a is \e surely positive |
| 226 | 226 |
bool positive(Value a) const { return _epsilon<a; }
|
| 227 | 227 |
///Returns \c true if \c a is \e surely negative |
| 228 | 228 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 229 | 229 |
///Returns \c true if \c a is \e surely non-zero |
| 230 | 230 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 231 | 231 |
|
| 232 | 232 |
///@} |
| 233 | 233 |
|
| 234 | 234 |
///Returns zero |
| 235 | 235 |
static Value zero() {return 0;}
|
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
/// @} |
| 239 | 239 |
|
| 240 | 240 |
} //namespace lemon |
| 241 | 241 |
|
| 242 | 242 |
#endif //LEMON_TOLERANCE_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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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_UNION_FIND_H |
| 20 | 20 |
#define LEMON_UNION_FIND_H |
| 21 | 21 |
|
| 22 | 22 |
//!\ingroup auxdat |
| 23 | 23 |
//!\file |
| 24 | 24 |
//!\brief Union-Find data structures. |
| 25 | 25 |
//! |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <list> |
| 29 | 29 |
#include <utility> |
| 30 | 30 |
#include <algorithm> |
| 31 | 31 |
#include <functional> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/core.h> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \ingroup auxdat |
| 38 | 38 |
/// |
| 39 | 39 |
/// \brief A \e Union-Find data structure implementation |
| 40 | 40 |
/// |
| 41 | 41 |
/// The class implements the \e Union-Find data structure. |
| 42 | 42 |
/// The union operation uses rank heuristic, while |
| 43 | 43 |
/// the find operation uses path compression. |
| 44 | 44 |
/// This is a very simple but efficient implementation, providing |
| 45 | 45 |
/// only four methods: join (union), find, insert and size. |
| 46 | 46 |
/// For more features see the \ref UnionFindEnum class. |
| 47 | 47 |
/// |
| 48 | 48 |
/// It is primarily used in Kruskal algorithm for finding minimal |
| 49 | 49 |
/// cost spanning tree in a graph. |
| 50 | 50 |
/// \sa kruskal() |
| 51 | 51 |
/// |
| 52 | 52 |
/// \pre You need to add all the elements by the \ref insert() |
| 53 | 53 |
/// method. |
| 54 | 54 |
template <typename _ItemIntMap> |
| 55 | 55 |
class UnionFind {
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef _ItemIntMap ItemIntMap; |
| 59 | 59 |
typedef typename ItemIntMap::Key Item; |
| 60 | 60 |
|
| 61 | 61 |
private: |
| 62 | 62 |
// If the items vector stores negative value for an item then |
| 63 | 63 |
// that item is root item and it has -items[it] component size. |
| 64 | 64 |
// Else the items[it] contains the index of the parent. |
| 65 | 65 |
std::vector<int> items; |
| 66 | 66 |
ItemIntMap& index; |
| 67 | 67 |
|
| 68 | 68 |
bool rep(int idx) const {
|
| 69 | 69 |
return items[idx] < 0; |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
int repIndex(int idx) const {
|
| 73 | 73 |
int k = idx; |
| 74 | 74 |
while (!rep(k)) {
|
| 75 | 75 |
k = items[k] ; |
| 76 | 76 |
} |
| 77 | 77 |
while (idx != k) {
|
| 78 | 78 |
int next = items[idx]; |
| 79 | 79 |
const_cast<int&>(items[idx]) = k; |
| 80 | 80 |
idx = next; |
| 81 | 81 |
} |
| 82 | 82 |
return k; |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
|
| 87 | 87 |
/// \brief Constructor |
| 88 | 88 |
/// |
| 89 | 89 |
/// Constructor of the UnionFind class. You should give an item to |
| 90 | 90 |
/// integer map which will be used from the data structure. If you |
| 91 | 91 |
/// modify directly this map that may cause segmentation fault, |
| 92 | 92 |
/// invalid data structure, or infinite loop when you use again |
| 93 | 93 |
/// the union-find. |
| 94 | 94 |
UnionFind(ItemIntMap& m) : index(m) {}
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Returns the index of the element's component. |
| 97 | 97 |
/// |
| 98 | 98 |
/// The method returns the index of the element's component. |
| 99 | 99 |
/// This is an integer between zero and the number of inserted elements. |
| 100 | 100 |
/// |
| 101 | 101 |
int find(const Item& a) {
|
| 102 | 102 |
return repIndex(index[a]); |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
/// \brief Clears the union-find data structure |
| 106 | 106 |
/// |
| 107 | 107 |
/// Erase each item from the data structure. |
| 108 | 108 |
void clear() {
|
| 109 | 109 |
items.clear(); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
/// \brief Inserts a new element into the structure. |
| 113 | 113 |
/// |
| 114 | 114 |
/// This method inserts a new element into the data structure. |
| 115 | 115 |
/// |
| 116 | 116 |
/// The method returns the index of the new component. |
| 117 | 117 |
int insert(const Item& a) {
|
| 118 | 118 |
int n = items.size(); |
| 119 | 119 |
items.push_back(-1); |
| 120 | 120 |
index.set(a,n); |
| 121 | 121 |
return n; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
/// \brief Joining the components of element \e a and element \e b. |
| 125 | 125 |
/// |
| 126 | 126 |
/// This is the \e union operation of the Union-Find structure. |
| 127 | 127 |
/// Joins the component of element \e a and component of |
| 128 | 128 |
/// element \e b. If \e a and \e b are in the same component then |
| 129 | 129 |
/// it returns false otherwise it returns true. |
| 130 | 130 |
bool join(const Item& a, const Item& b) {
|
| 131 | 131 |
int ka = repIndex(index[a]); |
| 132 | 132 |
int kb = repIndex(index[b]); |
| 133 | 133 |
|
| 134 | 134 |
if ( ka == kb ) |
| 135 | 135 |
return false; |
| 136 | 136 |
|
| 137 | 137 |
if (items[ka] < items[kb]) {
|
| 138 | 138 |
items[ka] += items[kb]; |
| 139 | 139 |
items[kb] = ka; |
| 140 | 140 |
} else {
|
| 141 | 141 |
items[kb] += items[ka]; |
| 142 | 142 |
items[ka] = kb; |
| 143 | 143 |
} |
| 144 | 144 |
return true; |
| 145 | 145 |
} |
| 146 | 146 |
|
| 147 | 147 |
/// \brief Returns the size of the component of element \e a. |
| 148 | 148 |
/// |
| 149 | 149 |
/// Returns the size of the component of element \e a. |
| 150 | 150 |
int size(const Item& a) {
|
| 151 | 151 |
int k = repIndex(index[a]); |
| 152 | 152 |
return - items[k]; |
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
}; |
| 156 | 156 |
|
| 157 | 157 |
/// \ingroup auxdat |
| 158 | 158 |
/// |
| 159 | 159 |
/// \brief A \e Union-Find data structure implementation which |
| 160 | 160 |
/// is able to enumerate the components. |
| 161 | 161 |
/// |
| 162 | 162 |
/// The class implements a \e Union-Find data structure |
| 163 | 163 |
/// which is able to enumerate the components and the items in |
| 164 | 164 |
/// a component. If you don't need this feature then perhaps it's |
| 165 | 165 |
/// better to use the \ref UnionFind class which is more efficient. |
| 166 | 166 |
/// |
| 167 | 167 |
/// The union operation uses rank heuristic, while |
| 168 | 168 |
/// the find operation uses path compression. |
| 169 | 169 |
/// |
| 170 | 170 |
/// \pre You need to add all the elements by the \ref insert() |
| 171 | 171 |
/// method. |
| 172 | 172 |
/// |
| 173 | 173 |
template <typename _ItemIntMap> |
| 174 | 174 |
class UnionFindEnum {
|
| 175 | 175 |
public: |
| 176 | 176 |
|
| 177 | 177 |
typedef _ItemIntMap ItemIntMap; |
| 178 | 178 |
typedef typename ItemIntMap::Key Item; |
| 179 | 179 |
|
| 180 | 180 |
private: |
| 181 | 181 |
|
| 182 | 182 |
ItemIntMap& index; |
| 183 | 183 |
|
| 184 | 184 |
// If the parent stores negative value for an item then that item |
| 185 | 185 |
// is root item and it has ~(items[it].parent) component id. Else |
| 186 | 186 |
// the items[it].parent contains the index of the parent. |
| 187 | 187 |
// |
| 188 | 188 |
// The \c next and \c prev provides the double-linked |
| 189 | 189 |
// cyclic list of one component's items. |
| 190 | 190 |
struct ItemT {
|
| 191 | 191 |
int parent; |
| 192 | 192 |
Item item; |
| 193 | 193 |
|
| 194 | 194 |
int next, prev; |
| 195 | 195 |
}; |
| 196 | 196 |
|
| 197 | 197 |
std::vector<ItemT> items; |
| 198 | 198 |
int firstFreeItem; |
| 199 | 199 |
|
| 200 | 200 |
struct ClassT {
|
| 201 | 201 |
int size; |
| 202 | 202 |
int firstItem; |
| 203 | 203 |
int next, prev; |
| 204 | 204 |
}; |
| 205 | 205 |
|
| 206 | 206 |
std::vector<ClassT> classes; |
| 207 | 207 |
int firstClass, firstFreeClass; |
| 208 | 208 |
|
| 209 | 209 |
int newClass() {
|
| 210 | 210 |
if (firstFreeClass == -1) {
|
| 211 | 211 |
int cdx = classes.size(); |
| 212 | 212 |
classes.push_back(ClassT()); |
| 213 | 213 |
return cdx; |
| 214 | 214 |
} else {
|
| 215 | 215 |
int cdx = firstFreeClass; |
| 216 | 216 |
firstFreeClass = classes[firstFreeClass].next; |
| 217 | 217 |
return cdx; |
| 218 | 218 |
} |
| 219 | 219 |
} |
| 220 | 220 |
|
| 221 | 221 |
int newItem() {
|
| 222 | 222 |
if (firstFreeItem == -1) {
|
| 223 | 223 |
int idx = items.size(); |
| 224 | 224 |
items.push_back(ItemT()); |
| 225 | 225 |
return idx; |
| 226 | 226 |
} else {
|
| 227 | 227 |
int idx = firstFreeItem; |
| 228 | 228 |
firstFreeItem = items[firstFreeItem].next; |
| 229 | 229 |
return idx; |
| 230 | 230 |
} |
| 231 | 231 |
} |
| 232 | 232 |
|
| 233 | 233 |
|
| 234 | 234 |
bool rep(int idx) const {
|
| 235 | 235 |
return items[idx].parent < 0; |
| 236 | 236 |
} |
| 237 | 237 |
|
| 238 | 238 |
int repIndex(int idx) const {
|
| 239 | 239 |
int k = idx; |
| 240 | 240 |
while (!rep(k)) {
|
| 241 | 241 |
k = items[k].parent; |
| 242 | 242 |
} |
| 243 | 243 |
while (idx != k) {
|
| 244 | 244 |
int next = items[idx].parent; |
| 245 | 245 |
const_cast<int&>(items[idx].parent) = k; |
| 246 | 246 |
idx = next; |
| 247 | 247 |
} |
| 248 | 248 |
return k; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
int classIndex(int idx) const {
|
| 252 | 252 |
return ~(items[repIndex(idx)].parent); |
| 253 | 253 |
} |
| 254 | 254 |
|
| 255 | 255 |
void singletonItem(int idx) {
|
| 256 | 256 |
items[idx].next = idx; |
| 257 | 257 |
items[idx].prev = idx; |
| 258 | 258 |
} |
| 259 | 259 |
|
| 260 | 260 |
void laceItem(int idx, int rdx) {
|
| 261 | 261 |
items[idx].prev = rdx; |
| 262 | 262 |
items[idx].next = items[rdx].next; |
| 263 | 263 |
items[items[rdx].next].prev = idx; |
| 264 | 264 |
items[rdx].next = idx; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
void unlaceItem(int idx) {
|
| 268 | 268 |
items[items[idx].prev].next = items[idx].next; |
| 269 | 269 |
items[items[idx].next].prev = items[idx].prev; |
| 270 | 270 |
|
| 271 | 271 |
items[idx].next = firstFreeItem; |
| 272 | 272 |
firstFreeItem = idx; |
| 273 | 273 |
} |
| 274 | 274 |
|
| 275 | 275 |
void spliceItems(int ak, int bk) {
|
| 276 | 276 |
items[items[ak].prev].next = bk; |
| 277 | 277 |
items[items[bk].prev].next = ak; |
| 278 | 278 |
int tmp = items[ak].prev; |
| 279 | 279 |
items[ak].prev = items[bk].prev; |
| 280 | 280 |
items[bk].prev = tmp; |
| 281 | 281 |
|
| 282 | 282 |
} |
| 283 | 283 |
|
| 284 | 284 |
void laceClass(int cls) {
|
| 285 | 285 |
if (firstClass != -1) {
|
| 286 | 286 |
classes[firstClass].prev = cls; |
| 287 | 287 |
} |
| 288 | 288 |
classes[cls].next = firstClass; |
| 289 | 289 |
classes[cls].prev = -1; |
| 290 | 290 |
firstClass = cls; |
| 291 | 291 |
} |
| 292 | 292 |
|
| 293 | 293 |
void unlaceClass(int cls) {
|
| 294 | 294 |
if (classes[cls].prev != -1) {
|
| 295 | 295 |
classes[classes[cls].prev].next = classes[cls].next; |
| 296 | 296 |
} else {
|
| 297 | 297 |
firstClass = classes[cls].next; |
| 298 | 298 |
} |
| 299 | 299 |
if (classes[cls].next != -1) {
|
| 300 | 300 |
classes[classes[cls].next].prev = classes[cls].prev; |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
classes[cls].next = firstFreeClass; |
| 304 | 304 |
firstFreeClass = cls; |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 | 307 |
public: |
| 308 | 308 |
|
| 309 | 309 |
UnionFindEnum(ItemIntMap& _index) |
| 310 | 310 |
: index(_index), items(), firstFreeItem(-1), |
| 311 | 311 |
firstClass(-1), firstFreeClass(-1) {}
|
| 312 | 312 |
|
| 313 | 313 |
/// \brief Inserts the given element into a new component. |
| 314 | 314 |
/// |
| 315 | 315 |
/// This method creates a new component consisting only of the |
| 316 | 316 |
/// given element. |
| 317 | 317 |
/// |
| 318 | 318 |
int insert(const Item& item) {
|
| 319 | 319 |
int idx = newItem(); |
| 320 | 320 |
|
| 321 | 321 |
index.set(item, idx); |
| 322 | 322 |
|
| 323 | 323 |
singletonItem(idx); |
| 324 | 324 |
items[idx].item = item; |
| 325 | 325 |
|
| 326 | 326 |
int cdx = newClass(); |
| 327 | 327 |
|
| 328 | 328 |
items[idx].parent = ~cdx; |
| 329 | 329 |
|
| 330 | 330 |
laceClass(cdx); |
| 331 | 331 |
classes[cdx].size = 1; |
| 332 | 332 |
classes[cdx].firstItem = idx; |
| 333 | 333 |
|
| 334 | 334 |
firstClass = cdx; |
| 335 | 335 |
|
| 336 | 336 |
return cdx; |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 | 339 |
/// \brief Inserts the given element into the component of the others. |
| 340 | 340 |
/// |
| 341 | 341 |
/// This methods inserts the element \e a into the component of the |
| 342 | 342 |
/// element \e comp. |
| 343 | 343 |
void insert(const Item& item, int cls) {
|
| 344 | 344 |
int rdx = classes[cls].firstItem; |
| 345 | 345 |
int idx = newItem(); |
| 346 | 346 |
|
| 347 | 347 |
index.set(item, idx); |
| 348 | 348 |
|
| 349 | 349 |
laceItem(idx, rdx); |
| 350 | 350 |
|
| 351 | 351 |
items[idx].item = item; |
| 352 | 352 |
items[idx].parent = rdx; |
| 353 | 353 |
|
| 354 | 354 |
++classes[~(items[rdx].parent)].size; |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
/// \brief Clears the union-find data structure |
| 358 | 358 |
/// |
| 359 | 359 |
/// Erase each item from the data structure. |
| 360 | 360 |
void clear() {
|
| 361 | 361 |
items.clear(); |
| 362 | 362 |
firstClass = -1; |
| 363 | 363 |
firstFreeItem = -1; |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 | 366 |
/// \brief Finds the component of the given element. |
| 367 | 367 |
/// |
| 368 | 368 |
/// The method returns the component id of the given element. |
| 369 | 369 |
int find(const Item &item) const {
|
| 370 | 370 |
return ~(items[repIndex(index[item])].parent); |
| 371 | 371 |
} |
| 372 | 372 |
|
| 373 | 373 |
/// \brief Joining the component of element \e a and element \e b. |
| 374 | 374 |
/// |
| 375 | 375 |
/// This is the \e union operation of the Union-Find structure. |
| 376 | 376 |
/// Joins the component of element \e a and component of |
| 377 | 377 |
/// element \e b. If \e a and \e b are in the same component then |
| 378 | 378 |
/// returns -1 else returns the remaining class. |
| 379 | 379 |
int join(const Item& a, const Item& b) {
|
| 380 | 380 |
|
| 381 | 381 |
int ak = repIndex(index[a]); |
| 382 | 382 |
int bk = repIndex(index[b]); |
| 383 | 383 |
|
| 384 | 384 |
if (ak == bk) {
|
| 385 | 385 |
return -1; |
| 386 | 386 |
} |
| 387 | 387 |
|
| 388 | 388 |
int acx = ~(items[ak].parent); |
| 389 | 389 |
int bcx = ~(items[bk].parent); |
| 390 | 390 |
|
| 391 | 391 |
int rcx; |
| 392 | 392 |
|
| 393 | 393 |
if (classes[acx].size > classes[bcx].size) {
|
| 394 | 394 |
classes[acx].size += classes[bcx].size; |
| 395 | 395 |
items[bk].parent = ak; |
| 396 | 396 |
unlaceClass(bcx); |
| 397 | 397 |
rcx = acx; |
| 398 | 398 |
} else {
|
| 399 | 399 |
classes[bcx].size += classes[acx].size; |
| 400 | 400 |
items[ak].parent = bk; |
| 401 | 401 |
unlaceClass(acx); |
| 402 | 402 |
rcx = bcx; |
| 403 | 403 |
} |
| 404 | 404 |
spliceItems(ak, bk); |
| 405 | 405 |
|
| 406 | 406 |
return rcx; |
| 407 | 407 |
} |
| 408 | 408 |
|
| 409 | 409 |
/// \brief Returns the size of the class. |
| 410 | 410 |
/// |
| 411 | 411 |
/// Returns the size of the class. |
| 412 | 412 |
int size(int cls) const {
|
| 413 | 413 |
return classes[cls].size; |
| 414 | 414 |
} |
| 415 | 415 |
|
| 416 | 416 |
/// \brief Splits up the component. |
| 417 | 417 |
/// |
| 418 | 418 |
/// Splitting the component into singleton components (component |
| 419 | 419 |
/// of size one). |
| 420 | 420 |
void split(int cls) {
|
| 421 | 421 |
int fdx = classes[cls].firstItem; |
| 422 | 422 |
int idx = items[fdx].next; |
| 423 | 423 |
while (idx != fdx) {
|
| 424 | 424 |
int next = items[idx].next; |
| 425 | 425 |
|
| 426 | 426 |
singletonItem(idx); |
| 427 | 427 |
|
| 428 | 428 |
int cdx = newClass(); |
| 429 | 429 |
items[idx].parent = ~cdx; |
| 430 | 430 |
|
| 431 | 431 |
laceClass(cdx); |
| 432 | 432 |
classes[cdx].size = 1; |
| 433 | 433 |
classes[cdx].firstItem = idx; |
| 434 | 434 |
|
| 435 | 435 |
idx = next; |
| 436 | 436 |
} |
| 437 | 437 |
|
| 438 | 438 |
items[idx].prev = idx; |
| 439 | 439 |
items[idx].next = idx; |
| 440 | 440 |
|
| 441 | 441 |
classes[~(items[idx].parent)].size = 1; |
| 442 | 442 |
|
| 443 | 443 |
} |
| 444 | 444 |
|
| 445 | 445 |
/// \brief Removes the given element from the structure. |
| 446 | 446 |
/// |
| 447 | 447 |
/// Removes the element from its component and if the component becomes |
| 448 | 448 |
/// empty then removes that component from the component list. |
| 449 | 449 |
/// |
| 450 | 450 |
/// \warning It is an error to remove an element which is not in |
| 451 | 451 |
/// the structure. |
| 452 | 452 |
/// \warning This running time of this operation is proportional to the |
| 453 | 453 |
/// number of the items in this class. |
| 454 | 454 |
void erase(const Item& item) {
|
| 455 | 455 |
int idx = index[item]; |
| 456 | 456 |
int fdx = items[idx].next; |
| 457 | 457 |
|
| 458 | 458 |
int cdx = classIndex(idx); |
| 459 | 459 |
if (idx == fdx) {
|
| 460 | 460 |
unlaceClass(cdx); |
| 461 | 461 |
items[idx].next = firstFreeItem; |
| 462 | 462 |
firstFreeItem = idx; |
| 463 | 463 |
return; |
| 464 | 464 |
} else {
|
| 465 | 465 |
classes[cdx].firstItem = fdx; |
| 466 | 466 |
--classes[cdx].size; |
| 467 | 467 |
items[fdx].parent = ~cdx; |
| 468 | 468 |
|
| 469 | 469 |
unlaceItem(idx); |
| 470 | 470 |
idx = items[fdx].next; |
| 471 | 471 |
while (idx != fdx) {
|
| 472 | 472 |
items[idx].parent = fdx; |
| 473 | 473 |
idx = items[idx].next; |
| 474 | 474 |
} |
| 475 | 475 |
|
| 476 | 476 |
} |
| 477 | 477 |
|
| 478 | 478 |
} |
| 479 | 479 |
|
| 480 | 480 |
/// \brief Gives back a representant item of the component. |
| 481 | 481 |
/// |
| 482 | 482 |
/// Gives back a representant item of the component. |
| 483 | 483 |
Item item(int cls) const {
|
| 484 | 484 |
return items[classes[cls].firstItem].item; |
| 485 | 485 |
} |
| 486 | 486 |
|
| 487 | 487 |
/// \brief Removes the component of the given element from the structure. |
| 488 | 488 |
/// |
| 489 | 489 |
/// Removes the component of the given element from the structure. |
| 490 | 490 |
/// |
| 491 | 491 |
/// \warning It is an error to give an element which is not in the |
| 492 | 492 |
/// structure. |
| 493 | 493 |
void eraseClass(int cls) {
|
| 494 | 494 |
int fdx = classes[cls].firstItem; |
| 495 | 495 |
unlaceClass(cls); |
| 496 | 496 |
items[items[fdx].prev].next = firstFreeItem; |
| 497 | 497 |
firstFreeItem = fdx; |
| 498 | 498 |
} |
| 499 | 499 |
|
| 500 | 500 |
/// \brief LEMON style iterator for the representant items. |
| 501 | 501 |
/// |
| 502 | 502 |
/// ClassIt is a lemon style iterator for the components. It iterates |
| 503 | 503 |
/// on the ids of the classes. |
| 504 | 504 |
class ClassIt {
|
| 505 | 505 |
public: |
| 506 | 506 |
/// \brief Constructor of the iterator |
| 507 | 507 |
/// |
| 508 | 508 |
/// Constructor of the iterator |
| 509 | 509 |
ClassIt(const UnionFindEnum& ufe) : unionFind(&ufe) {
|
| 510 | 510 |
cdx = unionFind->firstClass; |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 | 513 |
/// \brief Constructor to get invalid iterator |
| 514 | 514 |
/// |
| 515 | 515 |
/// Constructor to get invalid iterator |
| 516 | 516 |
ClassIt(Invalid) : unionFind(0), cdx(-1) {}
|
| 517 | 517 |
|
| 518 | 518 |
/// \brief Increment operator |
| 519 | 519 |
/// |
| 520 | 520 |
/// It steps to the next representant item. |
| 521 | 521 |
ClassIt& operator++() {
|
| 522 | 522 |
cdx = unionFind->classes[cdx].next; |
| 523 | 523 |
return *this; |
| 524 | 524 |
} |
| 525 | 525 |
|
| 526 | 526 |
/// \brief Conversion operator |
| 527 | 527 |
/// |
| 528 | 528 |
/// It converts the iterator to the current representant item. |
| 529 | 529 |
operator int() const {
|
| 530 | 530 |
return cdx; |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
/// \brief Equality operator |
| 534 | 534 |
/// |
| 535 | 535 |
/// Equality operator |
| 536 | 536 |
bool operator==(const ClassIt& i) {
|
| 537 | 537 |
return i.cdx == cdx; |
| 538 | 538 |
} |
| 539 | 539 |
|
| 540 | 540 |
/// \brief Inequality operator |
| 541 | 541 |
/// |
| 542 | 542 |
/// Inequality operator |
| 543 | 543 |
bool operator!=(const ClassIt& i) {
|
| 544 | 544 |
return i.cdx != cdx; |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 | 547 |
private: |
| 548 | 548 |
const UnionFindEnum* unionFind; |
| 549 | 549 |
int cdx; |
| 550 | 550 |
}; |
| 551 | 551 |
|
| 552 | 552 |
/// \brief LEMON style iterator for the items of a component. |
| 553 | 553 |
/// |
| 554 | 554 |
/// ClassIt is a lemon style iterator for the components. It iterates |
| 555 | 555 |
/// on the items of a class. By example if you want to iterate on |
| 556 | 556 |
/// each items of each classes then you may write the next code. |
| 557 | 557 |
///\code |
| 558 | 558 |
/// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
|
| 559 | 559 |
/// std::cout << "Class: "; |
| 560 | 560 |
/// for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
|
| 561 | 561 |
/// std::cout << toString(iit) << ' ' << std::endl; |
| 562 | 562 |
/// } |
| 563 | 563 |
/// std::cout << std::endl; |
| 564 | 564 |
/// } |
| 565 | 565 |
///\endcode |
| 566 | 566 |
class ItemIt {
|
| 567 | 567 |
public: |
| 568 | 568 |
/// \brief Constructor of the iterator |
| 569 | 569 |
/// |
| 570 | 570 |
/// Constructor of the iterator. The iterator iterates |
| 571 | 571 |
/// on the class of the \c item. |
| 572 | 572 |
ItemIt(const UnionFindEnum& ufe, int cls) : unionFind(&ufe) {
|
| 573 | 573 |
fdx = idx = unionFind->classes[cls].firstItem; |
| 574 | 574 |
} |
| 575 | 575 |
|
| 576 | 576 |
/// \brief Constructor to get invalid iterator |
| 577 | 577 |
/// |
| 578 | 578 |
/// Constructor to get invalid iterator |
| 579 | 579 |
ItemIt(Invalid) : unionFind(0), idx(-1) {}
|
| 580 | 580 |
|
| 581 | 581 |
/// \brief Increment operator |
| 582 | 582 |
/// |
| 583 | 583 |
/// It steps to the next item in the class. |
| 584 | 584 |
ItemIt& operator++() {
|
| 585 | 585 |
idx = unionFind->items[idx].next; |
| 586 | 586 |
if (idx == fdx) idx = -1; |
| 587 | 587 |
return *this; |
| 588 | 588 |
} |
| 589 | 589 |
|
| 590 | 590 |
/// \brief Conversion operator |
| 591 | 591 |
/// |
| 592 | 592 |
/// It converts the iterator to the current item. |
| 593 | 593 |
operator const Item&() const {
|
| 594 | 594 |
return unionFind->items[idx].item; |
| 595 | 595 |
} |
| 596 | 596 |
|
| 597 | 597 |
/// \brief Equality operator |
| 598 | 598 |
/// |
| 599 | 599 |
/// Equality operator |
| 600 | 600 |
bool operator==(const ItemIt& i) {
|
| 601 | 601 |
return i.idx == idx; |
| 602 | 602 |
} |
| 603 | 603 |
|
| 604 | 604 |
/// \brief Inequality operator |
| 605 | 605 |
/// |
| 606 | 606 |
/// Inequality operator |
| 607 | 607 |
bool operator!=(const ItemIt& i) {
|
| 608 | 608 |
return i.idx != idx; |
| 609 | 609 |
} |
| 610 | 610 |
|
| 611 | 611 |
private: |
| 612 | 612 |
const UnionFindEnum* unionFind; |
| 613 | 613 |
int idx, fdx; |
| 614 | 614 |
}; |
| 615 | 615 |
|
| 616 | 616 |
}; |
| 617 | 617 |
|
| 618 | 618 |
/// \ingroup auxdat |
| 619 | 619 |
/// |
| 620 | 620 |
/// \brief A \e Extend-Find data structure implementation which |
| 621 | 621 |
/// is able to enumerate the components. |
| 622 | 622 |
/// |
| 623 | 623 |
/// The class implements an \e Extend-Find data structure which is |
| 624 | 624 |
/// able to enumerate the components and the items in a |
| 625 | 625 |
/// component. The data structure is a simplification of the |
| 626 | 626 |
/// Union-Find structure, and it does not allow to merge two components. |
| 627 | 627 |
/// |
| 628 | 628 |
/// \pre You need to add all the elements by the \ref insert() |
| 629 | 629 |
/// method. |
| 630 | 630 |
template <typename _ItemIntMap> |
| 631 | 631 |
class ExtendFindEnum {
|
| 632 | 632 |
public: |
| 633 | 633 |
|
| 634 | 634 |
typedef _ItemIntMap ItemIntMap; |
| 635 | 635 |
typedef typename ItemIntMap::Key Item; |
| 636 | 636 |
|
| 637 | 637 |
private: |
| 638 | 638 |
|
| 639 | 639 |
ItemIntMap& index; |
| 640 | 640 |
|
| 641 | 641 |
struct ItemT {
|
| 642 | 642 |
int cls; |
| 643 | 643 |
Item item; |
| 644 | 644 |
int next, prev; |
| 645 | 645 |
}; |
| 646 | 646 |
|
| 647 | 647 |
std::vector<ItemT> items; |
| 648 | 648 |
int firstFreeItem; |
| 649 | 649 |
|
| 650 | 650 |
struct ClassT {
|
| 651 | 651 |
int firstItem; |
| 652 | 652 |
int next, prev; |
| 653 | 653 |
}; |
| 654 | 654 |
|
| 655 | 655 |
std::vector<ClassT> classes; |
| 656 | 656 |
|
| 657 | 657 |
int firstClass, firstFreeClass; |
| 658 | 658 |
|
| 659 | 659 |
int newClass() {
|
| 660 | 660 |
if (firstFreeClass != -1) {
|
| 661 | 661 |
int cdx = firstFreeClass; |
| 662 | 662 |
firstFreeClass = classes[cdx].next; |
| 663 | 663 |
return cdx; |
| 664 | 664 |
} else {
|
| 665 | 665 |
classes.push_back(ClassT()); |
| 666 | 666 |
return classes.size() - 1; |
| 667 | 667 |
} |
| 668 | 668 |
} |
| 669 | 669 |
|
| 670 | 670 |
int newItem() {
|
| 671 | 671 |
if (firstFreeItem != -1) {
|
| 672 | 672 |
int idx = firstFreeItem; |
| 673 | 673 |
firstFreeItem = items[idx].next; |
| 674 | 674 |
return idx; |
| 675 | 675 |
} else {
|
| 676 | 676 |
items.push_back(ItemT()); |
| 677 | 677 |
return items.size() - 1; |
| 678 | 678 |
} |
| 679 | 679 |
} |
| 680 | 680 |
|
| 681 | 681 |
public: |
| 682 | 682 |
|
| 683 | 683 |
/// \brief Constructor |
| 684 | 684 |
ExtendFindEnum(ItemIntMap& _index) |
| 685 | 685 |
: index(_index), items(), firstFreeItem(-1), |
| 686 | 686 |
classes(), firstClass(-1), firstFreeClass(-1) {}
|
| 687 | 687 |
|
| 688 | 688 |
/// \brief Inserts the given element into a new component. |
| 689 | 689 |
/// |
| 690 | 690 |
/// This method creates a new component consisting only of the |
| 691 | 691 |
/// given element. |
| 692 | 692 |
int insert(const Item& item) {
|
| 693 | 693 |
int cdx = newClass(); |
| 694 | 694 |
classes[cdx].prev = -1; |
| 695 | 695 |
classes[cdx].next = firstClass; |
| 696 | 696 |
if (firstClass != -1) {
|
| 697 | 697 |
classes[firstClass].prev = cdx; |
| 698 | 698 |
} |
| 699 | 699 |
firstClass = cdx; |
| 700 | 700 |
|
| 701 | 701 |
int idx = newItem(); |
| 702 | 702 |
items[idx].item = item; |
| 703 | 703 |
items[idx].cls = cdx; |
| 704 | 704 |
items[idx].prev = idx; |
| 705 | 705 |
items[idx].next = idx; |
| 706 | 706 |
|
| 707 | 707 |
classes[cdx].firstItem = idx; |
| 708 | 708 |
|
| 709 | 709 |
index.set(item, idx); |
| 710 | 710 |
|
| 711 | 711 |
return cdx; |
| 712 | 712 |
} |
| 713 | 713 |
|
| 714 | 714 |
/// \brief Inserts the given element into the given component. |
| 715 | 715 |
/// |
| 716 | 716 |
/// This methods inserts the element \e item a into the \e cls class. |
| 717 | 717 |
void insert(const Item& item, int cls) {
|
| 718 | 718 |
int idx = newItem(); |
| 719 | 719 |
int rdx = classes[cls].firstItem; |
| 720 | 720 |
items[idx].item = item; |
| 721 | 721 |
items[idx].cls = cls; |
| 722 | 722 |
|
| 723 | 723 |
items[idx].prev = rdx; |
| 724 | 724 |
items[idx].next = items[rdx].next; |
| 725 | 725 |
items[items[rdx].next].prev = idx; |
| 726 | 726 |
items[rdx].next = idx; |
| 727 | 727 |
|
| 728 | 728 |
index.set(item, idx); |
| 729 | 729 |
} |
| 730 | 730 |
|
| 731 | 731 |
/// \brief Clears the union-find data structure |
| 732 | 732 |
/// |
| 733 | 733 |
/// Erase each item from the data structure. |
| 734 | 734 |
void clear() {
|
| 735 | 735 |
items.clear(); |
| 736 | 736 |
classes.clear(); |
| 737 | 737 |
firstClass = firstFreeClass = firstFreeItem = -1; |
| 738 | 738 |
} |
| 739 | 739 |
|
| 740 | 740 |
/// \brief Gives back the class of the \e item. |
| 741 | 741 |
/// |
| 742 | 742 |
/// Gives back the class of the \e item. |
| 743 | 743 |
int find(const Item &item) const {
|
| 744 | 744 |
return items[index[item]].cls; |
| 745 | 745 |
} |
| 746 | 746 |
|
| 747 | 747 |
/// \brief Gives back a representant item of the component. |
| 748 | 748 |
/// |
| 749 | 749 |
/// Gives back a representant item of the component. |
| 750 | 750 |
Item item(int cls) const {
|
| 751 | 751 |
return items[classes[cls].firstItem].item; |
| 752 | 752 |
} |
| 753 | 753 |
|
| 754 | 754 |
/// \brief Removes the given element from the structure. |
| 755 | 755 |
/// |
| 756 | 756 |
/// Removes the element from its component and if the component becomes |
| 757 | 757 |
/// empty then removes that component from the component list. |
| 758 | 758 |
/// |
| 759 | 759 |
/// \warning It is an error to remove an element which is not in |
| 760 | 760 |
/// the structure. |
| 761 | 761 |
void erase(const Item &item) {
|
| 762 | 762 |
int idx = index[item]; |
| 763 | 763 |
int cdx = items[idx].cls; |
| 764 | 764 |
|
| 765 | 765 |
if (idx == items[idx].next) {
|
| 766 | 766 |
if (classes[cdx].prev != -1) {
|
| 767 | 767 |
classes[classes[cdx].prev].next = classes[cdx].next; |
| 768 | 768 |
} else {
|
| 769 | 769 |
firstClass = classes[cdx].next; |
| 770 | 770 |
} |
| 771 | 771 |
if (classes[cdx].next != -1) {
|
| 772 | 772 |
classes[classes[cdx].next].prev = classes[cdx].prev; |
| 773 | 773 |
} |
| 774 | 774 |
classes[cdx].next = firstFreeClass; |
| 775 | 775 |
firstFreeClass = cdx; |
| 776 | 776 |
} else {
|
| 777 | 777 |
classes[cdx].firstItem = items[idx].next; |
| 778 | 778 |
items[items[idx].next].prev = items[idx].prev; |
| 779 | 779 |
items[items[idx].prev].next = items[idx].next; |
| 780 | 780 |
} |
| 781 | 781 |
items[idx].next = firstFreeItem; |
| 782 | 782 |
firstFreeItem = idx; |
| 783 | 783 |
|
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
|
| 787 | 787 |
/// \brief Removes the component of the given element from the structure. |
| 788 | 788 |
/// |
| 789 | 789 |
/// Removes the component of the given element from the structure. |
| 790 | 790 |
/// |
| 791 | 791 |
/// \warning It is an error to give an element which is not in the |
| 792 | 792 |
/// structure. |
| 793 | 793 |
void eraseClass(int cdx) {
|
| 794 | 794 |
int idx = classes[cdx].firstItem; |
| 795 | 795 |
items[items[idx].prev].next = firstFreeItem; |
| 796 | 796 |
firstFreeItem = idx; |
| 797 | 797 |
|
| 798 | 798 |
if (classes[cdx].prev != -1) {
|
| 799 | 799 |
classes[classes[cdx].prev].next = classes[cdx].next; |
| 800 | 800 |
} else {
|
| 801 | 801 |
firstClass = classes[cdx].next; |
| 802 | 802 |
} |
| 803 | 803 |
if (classes[cdx].next != -1) {
|
| 804 | 804 |
classes[classes[cdx].next].prev = classes[cdx].prev; |
| 805 | 805 |
} |
| 806 | 806 |
classes[cdx].next = firstFreeClass; |
| 807 | 807 |
firstFreeClass = cdx; |
| 808 | 808 |
} |
| 809 | 809 |
|
| 810 | 810 |
/// \brief LEMON style iterator for the classes. |
| 811 | 811 |
/// |
| 812 | 812 |
/// ClassIt is a lemon style iterator for the components. It iterates |
| 813 | 813 |
/// on the ids of classes. |
| 814 | 814 |
class ClassIt {
|
| 815 | 815 |
public: |
| 816 | 816 |
/// \brief Constructor of the iterator |
| 817 | 817 |
/// |
| 818 | 818 |
/// Constructor of the iterator |
| 819 | 819 |
ClassIt(const ExtendFindEnum& ufe) : extendFind(&ufe) {
|
| 820 | 820 |
cdx = extendFind->firstClass; |
| 821 | 821 |
} |
| 822 | 822 |
|
| 823 | 823 |
/// \brief Constructor to get invalid iterator |
| 824 | 824 |
/// |
| 825 | 825 |
/// Constructor to get invalid iterator |
| 826 | 826 |
ClassIt(Invalid) : extendFind(0), cdx(-1) {}
|
| 827 | 827 |
|
| 828 | 828 |
/// \brief Increment operator |
| 829 | 829 |
/// |
| 830 | 830 |
/// It steps to the next representant item. |
| 831 | 831 |
ClassIt& operator++() {
|
| 832 | 832 |
cdx = extendFind->classes[cdx].next; |
| 833 | 833 |
return *this; |
| 834 | 834 |
} |
| 835 | 835 |
|
| 836 | 836 |
/// \brief Conversion operator |
| 837 | 837 |
/// |
| 838 | 838 |
/// It converts the iterator to the current class id. |
| 839 | 839 |
operator int() const {
|
| 840 | 840 |
return cdx; |
| 841 | 841 |
} |
| 842 | 842 |
|
| 843 | 843 |
/// \brief Equality operator |
| 844 | 844 |
/// |
| 845 | 845 |
/// Equality operator |
| 846 | 846 |
bool operator==(const ClassIt& i) {
|
| 847 | 847 |
return i.cdx == cdx; |
| 848 | 848 |
} |
| 849 | 849 |
|
| 850 | 850 |
/// \brief Inequality operator |
| 851 | 851 |
/// |
| 852 | 852 |
/// Inequality operator |
| 853 | 853 |
bool operator!=(const ClassIt& i) {
|
| 854 | 854 |
return i.cdx != cdx; |
| 855 | 855 |
} |
| 856 | 856 |
|
| 857 | 857 |
private: |
| 858 | 858 |
const ExtendFindEnum* extendFind; |
| 859 | 859 |
int cdx; |
| 860 | 860 |
}; |
| 861 | 861 |
|
| 862 | 862 |
/// \brief LEMON style iterator for the items of a component. |
| 863 | 863 |
/// |
| 864 | 864 |
/// ClassIt is a lemon style iterator for the components. It iterates |
| 865 | 865 |
/// on the items of a class. By example if you want to iterate on |
| 866 | 866 |
/// each items of each classes then you may write the next code. |
| 867 | 867 |
///\code |
| 868 | 868 |
/// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
|
| 869 | 869 |
/// std::cout << "Class: "; |
| 870 | 870 |
/// for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
|
| 871 | 871 |
/// std::cout << toString(iit) << ' ' << std::endl; |
| 872 | 872 |
/// } |
| 873 | 873 |
/// std::cout << std::endl; |
| 874 | 874 |
/// } |
| 875 | 875 |
///\endcode |
| 876 | 876 |
class ItemIt {
|
| 877 | 877 |
public: |
| 878 | 878 |
/// \brief Constructor of the iterator |
| 879 | 879 |
/// |
| 880 | 880 |
/// Constructor of the iterator. The iterator iterates |
| 881 | 881 |
/// on the class of the \c item. |
| 882 | 882 |
ItemIt(const ExtendFindEnum& ufe, int cls) : extendFind(&ufe) {
|
| 883 | 883 |
fdx = idx = extendFind->classes[cls].firstItem; |
| 884 | 884 |
} |
| 885 | 885 |
|
| 886 | 886 |
/// \brief Constructor to get invalid iterator |
| 887 | 887 |
/// |
| 888 | 888 |
/// Constructor to get invalid iterator |
| 889 | 889 |
ItemIt(Invalid) : extendFind(0), idx(-1) {}
|
| 890 | 890 |
|
| 891 | 891 |
/// \brief Increment operator |
| 892 | 892 |
/// |
| 893 | 893 |
/// It steps to the next item in the class. |
| 894 | 894 |
ItemIt& operator++() {
|
| 895 | 895 |
idx = extendFind->items[idx].next; |
| 896 | 896 |
if (fdx == idx) idx = -1; |
| 897 | 897 |
return *this; |
| 898 | 898 |
} |
| 899 | 899 |
|
| 900 | 900 |
/// \brief Conversion operator |
| 901 | 901 |
/// |
| 902 | 902 |
/// It converts the iterator to the current item. |
| 903 | 903 |
operator const Item&() const {
|
| 904 | 904 |
return extendFind->items[idx].item; |
| 905 | 905 |
} |
| 906 | 906 |
|
| 907 | 907 |
/// \brief Equality operator |
| 908 | 908 |
/// |
| 909 | 909 |
/// Equality operator |
| 910 | 910 |
bool operator==(const ItemIt& i) {
|
| 911 | 911 |
return i.idx == idx; |
| 912 | 912 |
} |
| 913 | 913 |
|
| 914 | 914 |
/// \brief Inequality operator |
| 915 | 915 |
/// |
| 916 | 916 |
/// Inequality operator |
| 917 | 917 |
bool operator!=(const ItemIt& i) {
|
| 918 | 918 |
return i.idx != idx; |
| 919 | 919 |
} |
| 920 | 920 |
|
| 921 | 921 |
private: |
| 922 | 922 |
const ExtendFindEnum* extendFind; |
| 923 | 923 |
int idx, fdx; |
| 924 | 924 |
}; |
| 925 | 925 |
|
| 926 | 926 |
}; |
| 927 | 927 |
|
| 928 | 928 |
/// \ingroup auxdat |
| 929 | 929 |
/// |
| 930 | 930 |
/// \brief A \e Union-Find data structure implementation which |
| 931 | 931 |
/// is able to store a priority for each item and retrieve the minimum of |
| 932 | 932 |
/// each class. |
| 933 | 933 |
/// |
| 934 | 934 |
/// A \e Union-Find data structure implementation which is able to |
| 935 | 935 |
/// store a priority for each item and retrieve the minimum of each |
| 936 | 936 |
/// class. In addition, it supports the joining and splitting the |
| 937 | 937 |
/// components. If you don't need this feature then you makes |
| 938 | 938 |
/// better to use the \ref UnionFind class which is more efficient. |
| 939 | 939 |
/// |
| 940 | 940 |
/// The union-find data strcuture based on a (2, 16)-tree with a |
| 941 | 941 |
/// tournament minimum selection on the internal nodes. The insert |
| 942 | 942 |
/// operation takes O(1), the find, set, decrease and increase takes |
| 943 | 943 |
/// O(log(n)), where n is the number of nodes in the current |
| 944 | 944 |
/// component. The complexity of join and split is O(log(n)*k), |
| 945 | 945 |
/// where n is the sum of the number of the nodes and k is the |
| 946 | 946 |
/// number of joined components or the number of the components |
| 947 | 947 |
/// after the split. |
| 948 | 948 |
/// |
| 949 | 949 |
/// \pre You need to add all the elements by the \ref insert() |
| 950 | 950 |
/// method. |
| 951 | 951 |
/// |
| 952 | 952 |
template <typename _Value, typename _ItemIntMap, |
| 953 | 953 |
typename _Comp = std::less<_Value> > |
| 954 | 954 |
class HeapUnionFind {
|
| 955 | 955 |
public: |
| 956 | 956 |
|
| 957 | 957 |
typedef _Value Value; |
| 958 | 958 |
typedef typename _ItemIntMap::Key Item; |
| 959 | 959 |
|
| 960 | 960 |
typedef _ItemIntMap ItemIntMap; |
| 961 | 961 |
|
| 962 | 962 |
typedef _Comp Comp; |
| 963 | 963 |
|
| 964 | 964 |
private: |
| 965 | 965 |
|
| 966 | 966 |
static const int cmax = 16; |
| 967 | 967 |
|
| 968 | 968 |
ItemIntMap& index; |
| 969 | 969 |
|
| 970 | 970 |
struct ClassNode {
|
| 971 | 971 |
int parent; |
| 972 | 972 |
int depth; |
| 973 | 973 |
|
| 974 | 974 |
int left, right; |
| 975 | 975 |
int next, prev; |
| 976 | 976 |
}; |
| 977 | 977 |
|
| 978 | 978 |
int first_class; |
| 979 | 979 |
int first_free_class; |
| 980 | 980 |
std::vector<ClassNode> classes; |
| 981 | 981 |
|
| 982 | 982 |
int newClass() {
|
| 983 | 983 |
if (first_free_class < 0) {
|
| 984 | 984 |
int id = classes.size(); |
| 985 | 985 |
classes.push_back(ClassNode()); |
| 986 | 986 |
return id; |
| 987 | 987 |
} else {
|
| 988 | 988 |
int id = first_free_class; |
| 989 | 989 |
first_free_class = classes[id].next; |
| 990 | 990 |
return id; |
| 991 | 991 |
} |
| 992 | 992 |
} |
| 993 | 993 |
|
| 994 | 994 |
void deleteClass(int id) {
|
| 995 | 995 |
classes[id].next = first_free_class; |
| 996 | 996 |
first_free_class = id; |
| 997 | 997 |
} |
| 998 | 998 |
|
| 999 | 999 |
struct ItemNode {
|
| 1000 | 1000 |
int parent; |
| 1001 | 1001 |
Item item; |
| 1002 | 1002 |
Value prio; |
| 1003 | 1003 |
int next, prev; |
| 1004 | 1004 |
int left, right; |
| 1005 | 1005 |
int size; |
| 1006 | 1006 |
}; |
| 1007 | 1007 |
|
| 1008 | 1008 |
int first_free_node; |
| 1009 | 1009 |
std::vector<ItemNode> nodes; |
| 1010 | 1010 |
|
| 1011 | 1011 |
int newNode() {
|
| 1012 | 1012 |
if (first_free_node < 0) {
|
| 1013 | 1013 |
int id = nodes.size(); |
| 1014 | 1014 |
nodes.push_back(ItemNode()); |
| 1015 | 1015 |
return id; |
| 1016 | 1016 |
} else {
|
| 1017 | 1017 |
int id = first_free_node; |
| 1018 | 1018 |
first_free_node = nodes[id].next; |
| 1019 | 1019 |
return id; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
} |
| 1022 | 1022 |
|
| 1023 | 1023 |
void deleteNode(int id) {
|
| 1024 | 1024 |
nodes[id].next = first_free_node; |
| 1025 | 1025 |
first_free_node = id; |
| 1026 | 1026 |
} |
| 1027 | 1027 |
|
| 1028 | 1028 |
Comp comp; |
| 1029 | 1029 |
|
| 1030 | 1030 |
int findClass(int id) const {
|
| 1031 | 1031 |
int kd = id; |
| 1032 | 1032 |
while (kd >= 0) {
|
| 1033 | 1033 |
kd = nodes[kd].parent; |
| 1034 | 1034 |
} |
| 1035 | 1035 |
return ~kd; |
| 1036 | 1036 |
} |
| 1037 | 1037 |
|
| 1038 | 1038 |
int leftNode(int id) const {
|
| 1039 | 1039 |
int kd = ~(classes[id].parent); |
| 1040 | 1040 |
for (int i = 0; i < classes[id].depth; ++i) {
|
| 1041 | 1041 |
kd = nodes[kd].left; |
| 1042 | 1042 |
} |
| 1043 | 1043 |
return kd; |
| 1044 | 1044 |
} |
| 1045 | 1045 |
|
| 1046 | 1046 |
int nextNode(int id) const {
|
| 1047 | 1047 |
int depth = 0; |
| 1048 | 1048 |
while (id >= 0 && nodes[id].next == -1) {
|
| 1049 | 1049 |
id = nodes[id].parent; |
| 1050 | 1050 |
++depth; |
| 1051 | 1051 |
} |
| 1052 | 1052 |
if (id < 0) {
|
| 1053 | 1053 |
return -1; |
| 1054 | 1054 |
} |
| 1055 | 1055 |
id = nodes[id].next; |
| 1056 | 1056 |
while (depth--) {
|
| 1057 | 1057 |
id = nodes[id].left; |
| 1058 | 1058 |
} |
| 1059 | 1059 |
return id; |
| 1060 | 1060 |
} |
| 1061 | 1061 |
|
| 1062 | 1062 |
|
| 1063 | 1063 |
void setPrio(int id) {
|
| 1064 | 1064 |
int jd = nodes[id].left; |
| 1065 | 1065 |
nodes[id].prio = nodes[jd].prio; |
| 1066 | 1066 |
nodes[id].item = nodes[jd].item; |
| 1067 | 1067 |
jd = nodes[jd].next; |
| 1068 | 1068 |
while (jd != -1) {
|
| 1069 | 1069 |
if (comp(nodes[jd].prio, nodes[id].prio)) {
|
| 1070 | 1070 |
nodes[id].prio = nodes[jd].prio; |
| 1071 | 1071 |
nodes[id].item = nodes[jd].item; |
| 1072 | 1072 |
} |
| 1073 | 1073 |
jd = nodes[jd].next; |
| 1074 | 1074 |
} |
| 1075 | 1075 |
} |
| 1076 | 1076 |
|
| 1077 | 1077 |
void push(int id, int jd) {
|
| 1078 | 1078 |
nodes[id].size = 1; |
| 1079 | 1079 |
nodes[id].left = nodes[id].right = jd; |
| 1080 | 1080 |
nodes[jd].next = nodes[jd].prev = -1; |
| 1081 | 1081 |
nodes[jd].parent = id; |
| 1082 | 1082 |
} |
| 1083 | 1083 |
|
| 1084 | 1084 |
void pushAfter(int id, int jd) {
|
| 1085 | 1085 |
int kd = nodes[id].parent; |
| 1086 | 1086 |
if (nodes[id].next != -1) {
|
| 1087 | 1087 |
nodes[nodes[id].next].prev = jd; |
| 1088 | 1088 |
if (kd >= 0) {
|
| 1089 | 1089 |
nodes[kd].size += 1; |
| 1090 | 1090 |
} |
| 1091 | 1091 |
} else {
|
| 1092 | 1092 |
if (kd >= 0) {
|
| 1093 | 1093 |
nodes[kd].right = jd; |
| 1094 | 1094 |
nodes[kd].size += 1; |
| 1095 | 1095 |
} |
| 1096 | 1096 |
} |
| 1097 | 1097 |
nodes[jd].next = nodes[id].next; |
| 1098 | 1098 |
nodes[jd].prev = id; |
| 1099 | 1099 |
nodes[id].next = jd; |
| 1100 | 1100 |
nodes[jd].parent = kd; |
| 1101 | 1101 |
} |
| 1102 | 1102 |
|
| 1103 | 1103 |
void pushRight(int id, int jd) {
|
| 1104 | 1104 |
nodes[id].size += 1; |
| 1105 | 1105 |
nodes[jd].prev = nodes[id].right; |
| 1106 | 1106 |
nodes[jd].next = -1; |
| 1107 | 1107 |
nodes[nodes[id].right].next = jd; |
| 1108 | 1108 |
nodes[id].right = jd; |
| 1109 | 1109 |
nodes[jd].parent = id; |
| 1110 | 1110 |
} |
| 1111 | 1111 |
|
| 1112 | 1112 |
void popRight(int id) {
|
| 1113 | 1113 |
nodes[id].size -= 1; |
| 1114 | 1114 |
int jd = nodes[id].right; |
| 1115 | 1115 |
nodes[nodes[jd].prev].next = -1; |
| 1116 | 1116 |
nodes[id].right = nodes[jd].prev; |
| 1117 | 1117 |
} |
| 1118 | 1118 |
|
| 1119 | 1119 |
void splice(int id, int jd) {
|
| 1120 | 1120 |
nodes[id].size += nodes[jd].size; |
| 1121 | 1121 |
nodes[nodes[id].right].next = nodes[jd].left; |
| 1122 | 1122 |
nodes[nodes[jd].left].prev = nodes[id].right; |
| 1123 | 1123 |
int kd = nodes[jd].left; |
| 1124 | 1124 |
while (kd != -1) {
|
| 1125 | 1125 |
nodes[kd].parent = id; |
| 1126 | 1126 |
kd = nodes[kd].next; |
| 1127 | 1127 |
} |
| 1128 | 1128 |
nodes[id].right = nodes[jd].right; |
| 1129 | 1129 |
} |
| 1130 | 1130 |
|
| 1131 | 1131 |
void split(int id, int jd) {
|
| 1132 | 1132 |
int kd = nodes[id].parent; |
| 1133 | 1133 |
nodes[kd].right = nodes[id].prev; |
| 1134 | 1134 |
nodes[nodes[id].prev].next = -1; |
| 1135 | 1135 |
|
| 1136 | 1136 |
nodes[jd].left = id; |
| 1137 | 1137 |
nodes[id].prev = -1; |
| 1138 | 1138 |
int num = 0; |
| 1139 | 1139 |
while (id != -1) {
|
| 1140 | 1140 |
nodes[id].parent = jd; |
| 1141 | 1141 |
nodes[jd].right = id; |
| 1142 | 1142 |
id = nodes[id].next; |
| 1143 | 1143 |
++num; |
| 1144 | 1144 |
} |
| 1145 | 1145 |
nodes[kd].size -= num; |
| 1146 | 1146 |
nodes[jd].size = num; |
| 1147 | 1147 |
} |
| 1148 | 1148 |
|
| 1149 | 1149 |
void pushLeft(int id, int jd) {
|
| 1150 | 1150 |
nodes[id].size += 1; |
| 1151 | 1151 |
nodes[jd].next = nodes[id].left; |
| 1152 | 1152 |
nodes[jd].prev = -1; |
| 1153 | 1153 |
nodes[nodes[id].left].prev = jd; |
| 1154 | 1154 |
nodes[id].left = jd; |
| 1155 | 1155 |
nodes[jd].parent = id; |
| 1156 | 1156 |
} |
| 1157 | 1157 |
|
| 1158 | 1158 |
void popLeft(int id) {
|
| 1159 | 1159 |
nodes[id].size -= 1; |
| 1160 | 1160 |
int jd = nodes[id].left; |
| 1161 | 1161 |
nodes[nodes[jd].next].prev = -1; |
| 1162 | 1162 |
nodes[id].left = nodes[jd].next; |
| 1163 | 1163 |
} |
| 1164 | 1164 |
|
| 1165 | 1165 |
void repairLeft(int id) {
|
| 1166 | 1166 |
int jd = ~(classes[id].parent); |
| 1167 | 1167 |
while (nodes[jd].left != -1) {
|
| 1168 | 1168 |
int kd = nodes[jd].left; |
| 1169 | 1169 |
if (nodes[jd].size == 1) {
|
| 1170 | 1170 |
if (nodes[jd].parent < 0) {
|
| 1171 | 1171 |
classes[id].parent = ~kd; |
| 1172 | 1172 |
classes[id].depth -= 1; |
| 1173 | 1173 |
nodes[kd].parent = ~id; |
| 1174 | 1174 |
deleteNode(jd); |
| 1175 | 1175 |
jd = kd; |
| 1176 | 1176 |
} else {
|
| 1177 | 1177 |
int pd = nodes[jd].parent; |
| 1178 | 1178 |
if (nodes[nodes[jd].next].size < cmax) {
|
| 1179 | 1179 |
pushLeft(nodes[jd].next, nodes[jd].left); |
| 1180 | 1180 |
if (less(jd, nodes[jd].next) || |
| 1181 | 1181 |
nodes[jd].item == nodes[pd].item) {
|
| 1182 | 1182 |
nodes[nodes[jd].next].prio = nodes[jd].prio; |
| 1183 | 1183 |
nodes[nodes[jd].next].item = nodes[jd].item; |
| 1184 | 1184 |
} |
| 1185 | 1185 |
popLeft(pd); |
| 1186 | 1186 |
deleteNode(jd); |
| 1187 | 1187 |
jd = pd; |
| 1188 | 1188 |
} else {
|
| 1189 | 1189 |
int ld = nodes[nodes[jd].next].left; |
| 1190 | 1190 |
popLeft(nodes[jd].next); |
| 1191 | 1191 |
pushRight(jd, ld); |
| 1192 |
if (less(ld, nodes[jd].left) || |
|
| 1192 |
if (less(ld, nodes[jd].left) || |
|
| 1193 | 1193 |
nodes[ld].item == nodes[pd].item) {
|
| 1194 | 1194 |
nodes[jd].item = nodes[ld].item; |
| 1195 | 1195 |
nodes[jd].prio = nodes[ld].prio; |
| 1196 | 1196 |
} |
| 1197 | 1197 |
if (nodes[nodes[jd].next].item == nodes[ld].item) {
|
| 1198 | 1198 |
setPrio(nodes[jd].next); |
| 1199 | 1199 |
} |
| 1200 | 1200 |
jd = nodes[jd].left; |
| 1201 | 1201 |
} |
| 1202 | 1202 |
} |
| 1203 | 1203 |
} else {
|
| 1204 | 1204 |
jd = nodes[jd].left; |
| 1205 | 1205 |
} |
| 1206 | 1206 |
} |
| 1207 | 1207 |
} |
| 1208 | 1208 |
|
| 1209 | 1209 |
void repairRight(int id) {
|
| 1210 | 1210 |
int jd = ~(classes[id].parent); |
| 1211 | 1211 |
while (nodes[jd].right != -1) {
|
| 1212 | 1212 |
int kd = nodes[jd].right; |
| 1213 | 1213 |
if (nodes[jd].size == 1) {
|
| 1214 | 1214 |
if (nodes[jd].parent < 0) {
|
| 1215 | 1215 |
classes[id].parent = ~kd; |
| 1216 | 1216 |
classes[id].depth -= 1; |
| 1217 | 1217 |
nodes[kd].parent = ~id; |
| 1218 | 1218 |
deleteNode(jd); |
| 1219 | 1219 |
jd = kd; |
| 1220 | 1220 |
} else {
|
| 1221 | 1221 |
int pd = nodes[jd].parent; |
| 1222 | 1222 |
if (nodes[nodes[jd].prev].size < cmax) {
|
| 1223 | 1223 |
pushRight(nodes[jd].prev, nodes[jd].right); |
| 1224 | 1224 |
if (less(jd, nodes[jd].prev) || |
| 1225 | 1225 |
nodes[jd].item == nodes[pd].item) {
|
| 1226 | 1226 |
nodes[nodes[jd].prev].prio = nodes[jd].prio; |
| 1227 | 1227 |
nodes[nodes[jd].prev].item = nodes[jd].item; |
| 1228 | 1228 |
} |
| 1229 | 1229 |
popRight(pd); |
| 1230 | 1230 |
deleteNode(jd); |
| 1231 | 1231 |
jd = pd; |
| 1232 | 1232 |
} else {
|
| 1233 | 1233 |
int ld = nodes[nodes[jd].prev].right; |
| 1234 | 1234 |
popRight(nodes[jd].prev); |
| 1235 | 1235 |
pushLeft(jd, ld); |
| 1236 | 1236 |
if (less(ld, nodes[jd].right) || |
| 1237 | 1237 |
nodes[ld].item == nodes[pd].item) {
|
| 1238 | 1238 |
nodes[jd].item = nodes[ld].item; |
| 1239 | 1239 |
nodes[jd].prio = nodes[ld].prio; |
| 1240 | 1240 |
} |
| 1241 | 1241 |
if (nodes[nodes[jd].prev].item == nodes[ld].item) {
|
| 1242 | 1242 |
setPrio(nodes[jd].prev); |
| 1243 | 1243 |
} |
| 1244 | 1244 |
jd = nodes[jd].right; |
| 1245 | 1245 |
} |
| 1246 | 1246 |
} |
| 1247 | 1247 |
} else {
|
| 1248 | 1248 |
jd = nodes[jd].right; |
| 1249 | 1249 |
} |
| 1250 | 1250 |
} |
| 1251 | 1251 |
} |
| 1252 | 1252 |
|
| 1253 | 1253 |
|
| 1254 | 1254 |
bool less(int id, int jd) const {
|
| 1255 | 1255 |
return comp(nodes[id].prio, nodes[jd].prio); |
| 1256 | 1256 |
} |
| 1257 | 1257 |
|
| 1258 | 1258 |
public: |
| 1259 | 1259 |
|
| 1260 | 1260 |
/// \brief Returns true when the given class is alive. |
| 1261 | 1261 |
/// |
| 1262 | 1262 |
/// Returns true when the given class is alive, ie. the class is |
| 1263 | 1263 |
/// not nested into other class. |
| 1264 | 1264 |
bool alive(int cls) const {
|
| 1265 | 1265 |
return classes[cls].parent < 0; |
| 1266 | 1266 |
} |
| 1267 | 1267 |
|
| 1268 | 1268 |
/// \brief Returns true when the given class is trivial. |
| 1269 | 1269 |
/// |
| 1270 | 1270 |
/// Returns true when the given class is trivial, ie. the class |
| 1271 | 1271 |
/// contains just one item directly. |
| 1272 | 1272 |
bool trivial(int cls) const {
|
| 1273 | 1273 |
return classes[cls].left == -1; |
| 1274 | 1274 |
} |
| 1275 | 1275 |
|
| 1276 | 1276 |
/// \brief Constructs the union-find. |
| 1277 | 1277 |
/// |
| 1278 | 1278 |
/// Constructs the union-find. |
| 1279 | 1279 |
/// \brief _index The index map of the union-find. The data |
| 1280 | 1280 |
/// structure uses internally for store references. |
| 1281 | 1281 |
HeapUnionFind(ItemIntMap& _index) |
| 1282 | 1282 |
: index(_index), first_class(-1), |
| 1283 | 1283 |
first_free_class(-1), first_free_node(-1) {}
|
| 1284 | 1284 |
|
| 1285 | 1285 |
/// \brief Insert a new node into a new component. |
| 1286 | 1286 |
/// |
| 1287 | 1287 |
/// Insert a new node into a new component. |
| 1288 | 1288 |
/// \param item The item of the new node. |
| 1289 | 1289 |
/// \param prio The priority of the new node. |
| 1290 | 1290 |
/// \return The class id of the one-item-heap. |
| 1291 | 1291 |
int insert(const Item& item, const Value& prio) {
|
| 1292 | 1292 |
int id = newNode(); |
| 1293 | 1293 |
nodes[id].item = item; |
| 1294 | 1294 |
nodes[id].prio = prio; |
| 1295 | 1295 |
nodes[id].size = 0; |
| 1296 | 1296 |
|
| 1297 | 1297 |
nodes[id].prev = -1; |
| 1298 | 1298 |
nodes[id].next = -1; |
| 1299 | 1299 |
|
| 1300 | 1300 |
nodes[id].left = -1; |
| 1301 | 1301 |
nodes[id].right = -1; |
| 1302 | 1302 |
|
| 1303 | 1303 |
nodes[id].item = item; |
| 1304 | 1304 |
index[item] = id; |
| 1305 | 1305 |
|
| 1306 | 1306 |
int class_id = newClass(); |
| 1307 | 1307 |
classes[class_id].parent = ~id; |
| 1308 | 1308 |
classes[class_id].depth = 0; |
| 1309 | 1309 |
|
| 1310 | 1310 |
classes[class_id].left = -1; |
| 1311 | 1311 |
classes[class_id].right = -1; |
| 1312 | 1312 |
|
| 1313 | 1313 |
if (first_class != -1) {
|
| 1314 | 1314 |
classes[first_class].prev = class_id; |
| 1315 | 1315 |
} |
| 1316 | 1316 |
classes[class_id].next = first_class; |
| 1317 | 1317 |
classes[class_id].prev = -1; |
| 1318 | 1318 |
first_class = class_id; |
| 1319 | 1319 |
|
| 1320 | 1320 |
nodes[id].parent = ~class_id; |
| 1321 | 1321 |
|
| 1322 | 1322 |
return class_id; |
| 1323 | 1323 |
} |
| 1324 | 1324 |
|
| 1325 | 1325 |
/// \brief The class of the item. |
| 1326 | 1326 |
/// |
| 1327 | 1327 |
/// \return The alive class id of the item, which is not nested into |
| 1328 | 1328 |
/// other classes. |
| 1329 | 1329 |
/// |
| 1330 | 1330 |
/// The time complexity is O(log(n)). |
| 1331 | 1331 |
int find(const Item& item) const {
|
| 1332 | 1332 |
return findClass(index[item]); |
| 1333 | 1333 |
} |
| 1334 | 1334 |
|
| 1335 | 1335 |
/// \brief Joins the classes. |
| 1336 | 1336 |
/// |
| 1337 | 1337 |
/// The current function joins the given classes. The parameter is |
| 1338 | 1338 |
/// an STL range which should be contains valid class ids. The |
| 1339 | 1339 |
/// time complexity is O(log(n)*k) where n is the overall number |
| 1340 | 1340 |
/// of the joined nodes and k is the number of classes. |
| 1341 | 1341 |
/// \return The class of the joined classes. |
| 1342 | 1342 |
/// \pre The range should contain at least two class ids. |
| 1343 | 1343 |
template <typename Iterator> |
| 1344 | 1344 |
int join(Iterator begin, Iterator end) {
|
| 1345 | 1345 |
std::vector<int> cs; |
| 1346 | 1346 |
for (Iterator it = begin; it != end; ++it) {
|
| 1347 | 1347 |
cs.push_back(*it); |
| 1348 | 1348 |
} |
| 1349 | 1349 |
|
| 1350 | 1350 |
int class_id = newClass(); |
| 1351 | 1351 |
{ // creation union-find
|
| 1352 | 1352 |
|
| 1353 | 1353 |
if (first_class != -1) {
|
| 1354 | 1354 |
classes[first_class].prev = class_id; |
| 1355 | 1355 |
} |
| 1356 | 1356 |
classes[class_id].next = first_class; |
| 1357 | 1357 |
classes[class_id].prev = -1; |
| 1358 | 1358 |
first_class = class_id; |
| 1359 | 1359 |
|
| 1360 | 1360 |
classes[class_id].depth = classes[cs[0]].depth; |
| 1361 | 1361 |
classes[class_id].parent = classes[cs[0]].parent; |
| 1362 | 1362 |
nodes[~(classes[class_id].parent)].parent = ~class_id; |
| 1363 | 1363 |
|
| 1364 | 1364 |
int l = cs[0]; |
| 1365 | 1365 |
|
| 1366 | 1366 |
classes[class_id].left = l; |
| 1367 | 1367 |
classes[class_id].right = l; |
| 1368 | 1368 |
|
| 1369 | 1369 |
if (classes[l].next != -1) {
|
| 1370 | 1370 |
classes[classes[l].next].prev = classes[l].prev; |
| 1371 | 1371 |
} |
| 1372 | 1372 |
classes[classes[l].prev].next = classes[l].next; |
| 1373 | 1373 |
|
| 1374 | 1374 |
classes[l].prev = -1; |
| 1375 | 1375 |
classes[l].next = -1; |
| 1376 | 1376 |
|
| 1377 | 1377 |
classes[l].depth = leftNode(l); |
| 1378 | 1378 |
classes[l].parent = class_id; |
| 1379 | 1379 |
|
| 1380 | 1380 |
} |
| 1381 | 1381 |
|
| 1382 | 1382 |
{ // merging of heap
|
| 1383 | 1383 |
int l = class_id; |
| 1384 | 1384 |
for (int ci = 1; ci < int(cs.size()); ++ci) {
|
| 1385 | 1385 |
int r = cs[ci]; |
| 1386 | 1386 |
int rln = leftNode(r); |
| 1387 | 1387 |
if (classes[l].depth > classes[r].depth) {
|
| 1388 | 1388 |
int id = ~(classes[l].parent); |
| 1389 | 1389 |
for (int i = classes[r].depth + 1; i < classes[l].depth; ++i) {
|
| 1390 | 1390 |
id = nodes[id].right; |
| 1391 | 1391 |
} |
| 1392 | 1392 |
while (id >= 0 && nodes[id].size == cmax) {
|
| 1393 | 1393 |
int new_id = newNode(); |
| 1394 | 1394 |
int right_id = nodes[id].right; |
| 1395 | 1395 |
|
| 1396 | 1396 |
popRight(id); |
| 1397 | 1397 |
if (nodes[id].item == nodes[right_id].item) {
|
| 1398 | 1398 |
setPrio(id); |
| 1399 | 1399 |
} |
| 1400 | 1400 |
push(new_id, right_id); |
| 1401 | 1401 |
pushRight(new_id, ~(classes[r].parent)); |
| 1402 | 1402 |
|
| 1403 | 1403 |
if (less(~classes[r].parent, right_id)) {
|
| 1404 | 1404 |
nodes[new_id].item = nodes[~classes[r].parent].item; |
| 1405 | 1405 |
nodes[new_id].prio = nodes[~classes[r].parent].prio; |
| 1406 | 1406 |
} else {
|
| 1407 | 1407 |
nodes[new_id].item = nodes[right_id].item; |
| 1408 | 1408 |
nodes[new_id].prio = nodes[right_id].prio; |
| 1409 | 1409 |
} |
| 1410 | 1410 |
|
| 1411 | 1411 |
id = nodes[id].parent; |
| 1412 | 1412 |
classes[r].parent = ~new_id; |
| 1413 | 1413 |
} |
| 1414 | 1414 |
if (id < 0) {
|
| 1415 | 1415 |
int new_parent = newNode(); |
| 1416 | 1416 |
nodes[new_parent].next = -1; |
| 1417 | 1417 |
nodes[new_parent].prev = -1; |
| 1418 | 1418 |
nodes[new_parent].parent = ~l; |
| 1419 | 1419 |
|
| 1420 | 1420 |
push(new_parent, ~(classes[l].parent)); |
| 1421 | 1421 |
pushRight(new_parent, ~(classes[r].parent)); |
| 1422 | 1422 |
setPrio(new_parent); |
| 1423 | 1423 |
|
| 1424 | 1424 |
classes[l].parent = ~new_parent; |
| 1425 | 1425 |
classes[l].depth += 1; |
| 1426 | 1426 |
} else {
|
| 1427 | 1427 |
pushRight(id, ~(classes[r].parent)); |
| 1428 | 1428 |
while (id >= 0 && less(~(classes[r].parent), id)) {
|
| 1429 | 1429 |
nodes[id].prio = nodes[~(classes[r].parent)].prio; |
| 1430 | 1430 |
nodes[id].item = nodes[~(classes[r].parent)].item; |
| 1431 | 1431 |
id = nodes[id].parent; |
| 1432 | 1432 |
} |
| 1433 | 1433 |
} |
| 1434 | 1434 |
} else if (classes[r].depth > classes[l].depth) {
|
| 1435 | 1435 |
int id = ~(classes[r].parent); |
| 1436 | 1436 |
for (int i = classes[l].depth + 1; i < classes[r].depth; ++i) {
|
| 1437 | 1437 |
id = nodes[id].left; |
| 1438 | 1438 |
} |
| 1439 | 1439 |
while (id >= 0 && nodes[id].size == cmax) {
|
| 1440 | 1440 |
int new_id = newNode(); |
| 1441 | 1441 |
int left_id = nodes[id].left; |
| 1442 | 1442 |
|
| 1443 | 1443 |
popLeft(id); |
| 1444 | 1444 |
if (nodes[id].prio == nodes[left_id].prio) {
|
| 1445 | 1445 |
setPrio(id); |
| 1446 | 1446 |
} |
| 1447 | 1447 |
push(new_id, left_id); |
| 1448 | 1448 |
pushLeft(new_id, ~(classes[l].parent)); |
| 1449 | 1449 |
|
| 1450 | 1450 |
if (less(~classes[l].parent, left_id)) {
|
| 1451 | 1451 |
nodes[new_id].item = nodes[~classes[l].parent].item; |
| 1452 | 1452 |
nodes[new_id].prio = nodes[~classes[l].parent].prio; |
| 1453 | 1453 |
} else {
|
| 1454 | 1454 |
nodes[new_id].item = nodes[left_id].item; |
| 1455 | 1455 |
nodes[new_id].prio = nodes[left_id].prio; |
| 1456 | 1456 |
} |
| 1457 | 1457 |
|
| 1458 | 1458 |
id = nodes[id].parent; |
| 1459 | 1459 |
classes[l].parent = ~new_id; |
| 1460 | 1460 |
|
| 1461 | 1461 |
} |
| 1462 | 1462 |
if (id < 0) {
|
| 1463 | 1463 |
int new_parent = newNode(); |
| 1464 | 1464 |
nodes[new_parent].next = -1; |
| 1465 | 1465 |
nodes[new_parent].prev = -1; |
| 1466 | 1466 |
nodes[new_parent].parent = ~l; |
| 1467 | 1467 |
|
| 1468 | 1468 |
push(new_parent, ~(classes[r].parent)); |
| 1469 | 1469 |
pushLeft(new_parent, ~(classes[l].parent)); |
| 1470 | 1470 |
setPrio(new_parent); |
| 1471 | 1471 |
|
| 1472 | 1472 |
classes[r].parent = ~new_parent; |
| 1473 | 1473 |
classes[r].depth += 1; |
| 1474 | 1474 |
} else {
|
| 1475 | 1475 |
pushLeft(id, ~(classes[l].parent)); |
| 1476 | 1476 |
while (id >= 0 && less(~(classes[l].parent), id)) {
|
| 1477 | 1477 |
nodes[id].prio = nodes[~(classes[l].parent)].prio; |
| 1478 | 1478 |
nodes[id].item = nodes[~(classes[l].parent)].item; |
| 1479 | 1479 |
id = nodes[id].parent; |
| 1480 | 1480 |
} |
| 1481 | 1481 |
} |
| 1482 | 1482 |
nodes[~(classes[r].parent)].parent = ~l; |
| 1483 | 1483 |
classes[l].parent = classes[r].parent; |
| 1484 | 1484 |
classes[l].depth = classes[r].depth; |
| 1485 | 1485 |
} else {
|
| 1486 | 1486 |
if (classes[l].depth != 0 && |
| 1487 | 1487 |
nodes[~(classes[l].parent)].size + |
| 1488 | 1488 |
nodes[~(classes[r].parent)].size <= cmax) {
|
| 1489 | 1489 |
splice(~(classes[l].parent), ~(classes[r].parent)); |
| 1490 | 1490 |
deleteNode(~(classes[r].parent)); |
| 1491 | 1491 |
if (less(~(classes[r].parent), ~(classes[l].parent))) {
|
| 1492 | 1492 |
nodes[~(classes[l].parent)].prio = |
| 1493 | 1493 |
nodes[~(classes[r].parent)].prio; |
| 1494 | 1494 |
nodes[~(classes[l].parent)].item = |
| 1495 | 1495 |
nodes[~(classes[r].parent)].item; |
| 1496 | 1496 |
} |
| 1497 | 1497 |
} else {
|
| 1498 | 1498 |
int new_parent = newNode(); |
| 1499 | 1499 |
nodes[new_parent].next = nodes[new_parent].prev = -1; |
| 1500 | 1500 |
push(new_parent, ~(classes[l].parent)); |
| 1501 | 1501 |
pushRight(new_parent, ~(classes[r].parent)); |
| 1502 | 1502 |
setPrio(new_parent); |
| 1503 | 1503 |
|
| 1504 | 1504 |
classes[l].parent = ~new_parent; |
| 1505 | 1505 |
classes[l].depth += 1; |
| 1506 | 1506 |
nodes[new_parent].parent = ~l; |
| 1507 | 1507 |
} |
| 1508 | 1508 |
} |
| 1509 | 1509 |
if (classes[r].next != -1) {
|
| 1510 | 1510 |
classes[classes[r].next].prev = classes[r].prev; |
| 1511 | 1511 |
} |
| 1512 | 1512 |
classes[classes[r].prev].next = classes[r].next; |
| 1513 | 1513 |
|
| 1514 | 1514 |
classes[r].prev = classes[l].right; |
| 1515 | 1515 |
classes[classes[l].right].next = r; |
| 1516 | 1516 |
classes[l].right = r; |
| 1517 | 1517 |
classes[r].parent = l; |
| 1518 | 1518 |
|
| 1519 | 1519 |
classes[r].next = -1; |
| 1520 | 1520 |
classes[r].depth = rln; |
| 1521 | 1521 |
} |
| 1522 | 1522 |
} |
| 1523 | 1523 |
return class_id; |
| 1524 | 1524 |
} |
| 1525 | 1525 |
|
| 1526 | 1526 |
/// \brief Split the class to subclasses. |
| 1527 | 1527 |
/// |
| 1528 | 1528 |
/// The current function splits the given class. The join, which |
| 1529 | 1529 |
/// made the current class, stored a reference to the |
| 1530 | 1530 |
/// subclasses. The \c splitClass() member restores the classes |
| 1531 | 1531 |
/// and creates the heaps. The parameter is an STL output iterator |
| 1532 | 1532 |
/// which will be filled with the subclass ids. The time |
| 1533 | 1533 |
/// complexity is O(log(n)*k) where n is the overall number of |
| 1534 | 1534 |
/// nodes in the splitted classes and k is the number of the |
| 1535 | 1535 |
/// classes. |
| 1536 | 1536 |
template <typename Iterator> |
| 1537 | 1537 |
void split(int cls, Iterator out) {
|
| 1538 | 1538 |
std::vector<int> cs; |
| 1539 | 1539 |
{ // splitting union-find
|
| 1540 | 1540 |
int id = cls; |
| 1541 | 1541 |
int l = classes[id].left; |
| 1542 | 1542 |
|
| 1543 | 1543 |
classes[l].parent = classes[id].parent; |
| 1544 | 1544 |
classes[l].depth = classes[id].depth; |
| 1545 | 1545 |
|
| 1546 | 1546 |
nodes[~(classes[l].parent)].parent = ~l; |
| 1547 | 1547 |
|
| 1548 | 1548 |
*out++ = l; |
| 1549 | 1549 |
|
| 1550 | 1550 |
while (l != -1) {
|
| 1551 | 1551 |
cs.push_back(l); |
| 1552 | 1552 |
l = classes[l].next; |
| 1553 | 1553 |
} |
| 1554 | 1554 |
|
| 1555 | 1555 |
classes[classes[id].right].next = first_class; |
| 1556 | 1556 |
classes[first_class].prev = classes[id].right; |
| 1557 | 1557 |
first_class = classes[id].left; |
| 1558 | 1558 |
|
| 1559 | 1559 |
if (classes[id].next != -1) {
|
| 1560 | 1560 |
classes[classes[id].next].prev = classes[id].prev; |
| 1561 | 1561 |
} |
| 1562 | 1562 |
classes[classes[id].prev].next = classes[id].next; |
| 1563 | 1563 |
|
| 1564 | 1564 |
deleteClass(id); |
| 1565 | 1565 |
} |
| 1566 | 1566 |
|
| 1567 | 1567 |
{
|
| 1568 | 1568 |
for (int i = 1; i < int(cs.size()); ++i) {
|
| 1569 | 1569 |
int l = classes[cs[i]].depth; |
| 1570 | 1570 |
while (nodes[nodes[l].parent].left == l) {
|
| 1571 | 1571 |
l = nodes[l].parent; |
| 1572 | 1572 |
} |
| 1573 | 1573 |
int r = l; |
| 1574 | 1574 |
while (nodes[l].parent >= 0) {
|
| 1575 | 1575 |
l = nodes[l].parent; |
| 1576 | 1576 |
int new_node = newNode(); |
| 1577 | 1577 |
|
| 1578 | 1578 |
nodes[new_node].prev = -1; |
| 1579 | 1579 |
nodes[new_node].next = -1; |
| 1580 | 1580 |
|
| 1581 | 1581 |
split(r, new_node); |
| 1582 | 1582 |
pushAfter(l, new_node); |
| 1583 | 1583 |
setPrio(l); |
| 1584 | 1584 |
setPrio(new_node); |
| 1585 | 1585 |
r = new_node; |
| 1586 | 1586 |
} |
| 1587 | 1587 |
classes[cs[i]].parent = ~r; |
| 1588 | 1588 |
classes[cs[i]].depth = classes[~(nodes[l].parent)].depth; |
| 1589 | 1589 |
nodes[r].parent = ~cs[i]; |
| 1590 | 1590 |
|
| 1591 | 1591 |
nodes[l].next = -1; |
| 1592 | 1592 |
nodes[r].prev = -1; |
| 1593 | 1593 |
|
| 1594 | 1594 |
repairRight(~(nodes[l].parent)); |
| 1595 | 1595 |
repairLeft(cs[i]); |
| 1596 | 1596 |
|
| 1597 | 1597 |
*out++ = cs[i]; |
| 1598 | 1598 |
} |
| 1599 | 1599 |
} |
| 1600 | 1600 |
} |
| 1601 | 1601 |
|
| 1602 | 1602 |
/// \brief Gives back the priority of the current item. |
| 1603 | 1603 |
/// |
| 1604 | 1604 |
/// \return Gives back the priority of the current item. |
| 1605 | 1605 |
const Value& operator[](const Item& item) const {
|
| 1606 | 1606 |
return nodes[index[item]].prio; |
| 1607 | 1607 |
} |
| 1608 | 1608 |
|
| 1609 | 1609 |
/// \brief Sets the priority of the current item. |
| 1610 | 1610 |
/// |
| 1611 | 1611 |
/// Sets the priority of the current item. |
| 1612 | 1612 |
void set(const Item& item, const Value& prio) {
|
| 1613 | 1613 |
if (comp(prio, nodes[index[item]].prio)) {
|
| 1614 | 1614 |
decrease(item, prio); |
| 1615 | 1615 |
} else if (!comp(prio, nodes[index[item]].prio)) {
|
| 1616 | 1616 |
increase(item, prio); |
| 1617 | 1617 |
} |
| 1618 | 1618 |
} |
| 1619 | 1619 |
|
| 1620 | 1620 |
/// \brief Increase the priority of the current item. |
| 1621 | 1621 |
/// |
| 1622 | 1622 |
/// Increase the priority of the current item. |
| 1623 | 1623 |
void increase(const Item& item, const Value& prio) {
|
| 1624 | 1624 |
int id = index[item]; |
| 1625 | 1625 |
int kd = nodes[id].parent; |
| 1626 | 1626 |
nodes[id].prio = prio; |
| 1627 | 1627 |
while (kd >= 0 && nodes[kd].item == item) {
|
| 1628 | 1628 |
setPrio(kd); |
| 1629 | 1629 |
kd = nodes[kd].parent; |
| 1630 | 1630 |
} |
| 1631 | 1631 |
} |
| 1632 | 1632 |
|
| 1633 | 1633 |
/// \brief Increase the priority of the current item. |
| 1634 | 1634 |
/// |
| 1635 | 1635 |
/// Increase the priority of the current item. |
| 1636 | 1636 |
void decrease(const Item& item, const Value& prio) {
|
| 1637 | 1637 |
int id = index[item]; |
| 1638 | 1638 |
int kd = nodes[id].parent; |
| 1639 | 1639 |
nodes[id].prio = prio; |
| 1640 | 1640 |
while (kd >= 0 && less(id, kd)) {
|
| 1641 | 1641 |
nodes[kd].prio = prio; |
| 1642 | 1642 |
nodes[kd].item = item; |
| 1643 | 1643 |
kd = nodes[kd].parent; |
| 1644 | 1644 |
} |
| 1645 | 1645 |
} |
| 1646 | 1646 |
|
| 1647 | 1647 |
/// \brief Gives back the minimum priority of the class. |
| 1648 | 1648 |
/// |
| 1649 | 1649 |
/// \return Gives back the minimum priority of the class. |
| 1650 | 1650 |
const Value& classPrio(int cls) const {
|
| 1651 | 1651 |
return nodes[~(classes[cls].parent)].prio; |
| 1652 | 1652 |
} |
| 1653 | 1653 |
|
| 1654 | 1654 |
/// \brief Gives back the minimum priority item of the class. |
| 1655 | 1655 |
/// |
| 1656 | 1656 |
/// \return Gives back the minimum priority item of the class. |
| 1657 | 1657 |
const Item& classTop(int cls) const {
|
| 1658 | 1658 |
return nodes[~(classes[cls].parent)].item; |
| 1659 | 1659 |
} |
| 1660 | 1660 |
|
| 1661 | 1661 |
/// \brief Gives back a representant item of the class. |
| 1662 | 1662 |
/// |
| 1663 | 1663 |
/// The representant is indpendent from the priorities of the |
| 1664 | 1664 |
/// items. |
| 1665 | 1665 |
/// \return Gives back a representant item of the class. |
| 1666 | 1666 |
const Item& classRep(int id) const {
|
| 1667 | 1667 |
int parent = classes[id].parent; |
| 1668 | 1668 |
return nodes[parent >= 0 ? classes[id].depth : leftNode(id)].item; |
| 1669 | 1669 |
} |
| 1670 | 1670 |
|
| 1671 | 1671 |
/// \brief LEMON style iterator for the items of a class. |
| 1672 | 1672 |
/// |
| 1673 | 1673 |
/// ClassIt is a lemon style iterator for the components. It iterates |
| 1674 | 1674 |
/// on the items of a class. By example if you want to iterate on |
| 1675 | 1675 |
/// each items of each classes then you may write the next code. |
| 1676 | 1676 |
///\code |
| 1677 | 1677 |
/// for (ClassIt cit(huf); cit != INVALID; ++cit) {
|
| 1678 | 1678 |
/// std::cout << "Class: "; |
| 1679 | 1679 |
/// for (ItemIt iit(huf, cit); iit != INVALID; ++iit) {
|
| 1680 | 1680 |
/// std::cout << toString(iit) << ' ' << std::endl; |
| 1681 | 1681 |
/// } |
| 1682 | 1682 |
/// std::cout << std::endl; |
| 1683 | 1683 |
/// } |
| 1684 | 1684 |
///\endcode |
| 1685 | 1685 |
class ItemIt {
|
| 1686 | 1686 |
private: |
| 1687 | 1687 |
|
| 1688 | 1688 |
const HeapUnionFind* _huf; |
| 1689 | 1689 |
int _id, _lid; |
| 1690 | 1690 |
|
| 1691 | 1691 |
public: |
| 1692 | 1692 |
|
| 1693 | 1693 |
/// \brief Default constructor |
| 1694 | 1694 |
/// |
| 1695 | 1695 |
/// Default constructor |
| 1696 | 1696 |
ItemIt() {}
|
| 1697 | 1697 |
|
| 1698 | 1698 |
ItemIt(const HeapUnionFind& huf, int cls) : _huf(&huf) {
|
| 1699 | 1699 |
int id = cls; |
| 1700 | 1700 |
int parent = _huf->classes[id].parent; |
| 1701 | 1701 |
if (parent >= 0) {
|
| 1702 | 1702 |
_id = _huf->classes[id].depth; |
| 1703 | 1703 |
if (_huf->classes[id].next != -1) {
|
| 1704 | 1704 |
_lid = _huf->classes[_huf->classes[id].next].depth; |
| 1705 | 1705 |
} else {
|
| 1706 | 1706 |
_lid = -1; |
| 1707 | 1707 |
} |
| 1708 | 1708 |
} else {
|
| 1709 | 1709 |
_id = _huf->leftNode(id); |
| 1710 | 1710 |
_lid = -1; |
| 1711 | 1711 |
} |
| 1712 | 1712 |
} |
| 1713 | 1713 |
|
| 1714 | 1714 |
/// \brief Increment operator |
| 1715 | 1715 |
/// |
| 1716 | 1716 |
/// It steps to the next item in the class. |
| 1717 | 1717 |
ItemIt& operator++() {
|
| 1718 | 1718 |
_id = _huf->nextNode(_id); |
| 1719 | 1719 |
return *this; |
| 1720 | 1720 |
} |
| 1721 | 1721 |
|
| 1722 | 1722 |
/// \brief Conversion operator |
| 1723 | 1723 |
/// |
| 1724 | 1724 |
/// It converts the iterator to the current item. |
| 1725 | 1725 |
operator const Item&() const {
|
| 1726 | 1726 |
return _huf->nodes[_id].item; |
| 1727 | 1727 |
} |
| 1728 | 1728 |
|
| 1729 | 1729 |
/// \brief Equality operator |
| 1730 | 1730 |
/// |
| 1731 | 1731 |
/// Equality operator |
| 1732 | 1732 |
bool operator==(const ItemIt& i) {
|
| 1733 | 1733 |
return i._id == _id; |
| 1734 | 1734 |
} |
| 1735 | 1735 |
|
| 1736 | 1736 |
/// \brief Inequality operator |
| 1737 | 1737 |
/// |
| 1738 | 1738 |
/// Inequality operator |
| 1739 | 1739 |
bool operator!=(const ItemIt& i) {
|
| 1740 | 1740 |
return i._id != _id; |
| 1741 | 1741 |
} |
| 1742 | 1742 |
|
| 1743 | 1743 |
/// \brief Equality operator |
| 1744 | 1744 |
/// |
| 1745 | 1745 |
/// Equality operator |
| 1746 | 1746 |
bool operator==(Invalid) {
|
| 1747 | 1747 |
return _id == _lid; |
| 1748 | 1748 |
} |
| 1749 | 1749 |
|
| 1750 | 1750 |
/// \brief Inequality operator |
| 1751 | 1751 |
/// |
| 1752 | 1752 |
/// Inequality operator |
| 1753 | 1753 |
bool operator!=(Invalid) {
|
| 1754 | 1754 |
return _id != _lid; |
| 1755 | 1755 |
} |
| 1756 | 1756 |
|
| 1757 | 1757 |
}; |
| 1758 | 1758 |
|
| 1759 | 1759 |
/// \brief Class iterator |
| 1760 | 1760 |
/// |
| 1761 | 1761 |
/// The iterator stores |
| 1762 | 1762 |
class ClassIt {
|
| 1763 | 1763 |
private: |
| 1764 | 1764 |
|
| 1765 | 1765 |
const HeapUnionFind* _huf; |
| 1766 | 1766 |
int _id; |
| 1767 | 1767 |
|
| 1768 | 1768 |
public: |
| 1769 | 1769 |
|
| 1770 | 1770 |
ClassIt(const HeapUnionFind& huf) |
| 1771 | 1771 |
: _huf(&huf), _id(huf.first_class) {}
|
| 1772 | 1772 |
|
| 1773 | 1773 |
ClassIt(const HeapUnionFind& huf, int cls) |
| 1774 | 1774 |
: _huf(&huf), _id(huf.classes[cls].left) {}
|
| 1775 | 1775 |
|
| 1776 | 1776 |
ClassIt(Invalid) : _huf(0), _id(-1) {}
|
| 1777 | 1777 |
|
| 1778 | 1778 |
const ClassIt& operator++() {
|
| 1779 | 1779 |
_id = _huf->classes[_id].next; |
| 1780 | 1780 |
return *this; |
| 1781 | 1781 |
} |
| 1782 | 1782 |
|
| 1783 | 1783 |
/// \brief Equality operator |
| 1784 | 1784 |
/// |
| 1785 | 1785 |
/// Equality operator |
| 1786 | 1786 |
bool operator==(const ClassIt& i) {
|
| 1787 | 1787 |
return i._id == _id; |
| 1788 | 1788 |
} |
| 1789 | 1789 |
|
| 1790 | 1790 |
/// \brief Inequality operator |
| 1791 | 1791 |
/// |
| 1792 | 1792 |
/// Inequality operator |
| 1793 | 1793 |
bool operator!=(const ClassIt& i) {
|
| 1794 | 1794 |
return i._id != _id; |
| 1795 | 1795 |
} |
| 1796 | 1796 |
|
| 1797 | 1797 |
operator int() const {
|
| 1798 | 1798 |
return _id; |
| 1799 | 1799 |
} |
| 1800 | 1800 |
|
| 1801 | 1801 |
}; |
| 1802 | 1802 |
|
| 1803 | 1803 |
}; |
| 1804 | 1804 |
|
| 1805 | 1805 |
//! @} |
| 1806 | 1806 |
|
| 1807 | 1807 |
} //namespace lemon |
| 1808 | 1808 |
|
| 1809 | 1809 |
#endif //LEMON_UNION_FIND_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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"6\n" |
| 41 | 41 |
"@arcs\n" |
| 42 | 42 |
" label\n" |
| 43 | 43 |
"0 1 0\n" |
| 44 | 44 |
"1 2 1\n" |
| 45 | 45 |
"2 3 2\n" |
| 46 | 46 |
"1 4 3\n" |
| 47 | 47 |
"4 2 4\n" |
| 48 | 48 |
"4 5 5\n" |
| 49 | 49 |
"5 0 6\n" |
| 50 | 50 |
"6 3 7\n" |
| 51 | 51 |
"@attributes\n" |
| 52 | 52 |
"source 0\n" |
| 53 | 53 |
"target 5\n" |
| 54 | 54 |
"source1 6\n" |
| 55 | 55 |
"target1 3\n"; |
| 56 | 56 |
|
| 57 | 57 |
|
| 58 | 58 |
void checkDfsCompile() |
| 59 | 59 |
{
|
| 60 | 60 |
typedef concepts::Digraph Digraph; |
| 61 | 61 |
typedef Dfs<Digraph> DType; |
| 62 | 62 |
typedef Digraph::Node Node; |
| 63 | 63 |
typedef Digraph::Arc Arc; |
| 64 | 64 |
|
| 65 | 65 |
Digraph G; |
| 66 | 66 |
Node s, t; |
| 67 | 67 |
Arc e; |
| 68 | 68 |
int l; |
| 69 | 69 |
bool b; |
| 70 | 70 |
DType::DistMap d(G); |
| 71 | 71 |
DType::PredMap p(G); |
| 72 | 72 |
Path<Digraph> pp; |
| 73 | 73 |
|
| 74 | 74 |
{
|
| 75 | 75 |
DType dfs_test(G); |
| 76 | 76 |
|
| 77 | 77 |
dfs_test.run(s); |
| 78 | 78 |
dfs_test.run(s,t); |
| 79 | 79 |
dfs_test.run(); |
| 80 | 80 |
|
| 81 | 81 |
l = dfs_test.dist(t); |
| 82 | 82 |
e = dfs_test.predArc(t); |
| 83 | 83 |
s = dfs_test.predNode(t); |
| 84 | 84 |
b = dfs_test.reached(t); |
| 85 | 85 |
d = dfs_test.distMap(); |
| 86 | 86 |
p = dfs_test.predMap(); |
| 87 | 87 |
pp = dfs_test.path(t); |
| 88 | 88 |
} |
| 89 | 89 |
{
|
| 90 | 90 |
DType |
| 91 | 91 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 92 | 92 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 93 | 93 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 94 | 94 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 95 | 95 |
::SetStandardProcessedMap |
| 96 | 96 |
::Create dfs_test(G); |
| 97 | 97 |
|
| 98 | 98 |
dfs_test.run(s); |
| 99 | 99 |
dfs_test.run(s,t); |
| 100 | 100 |
dfs_test.run(); |
| 101 | 101 |
|
| 102 | 102 |
l = dfs_test.dist(t); |
| 103 | 103 |
e = dfs_test.predArc(t); |
| 104 | 104 |
s = dfs_test.predNode(t); |
| 105 | 105 |
b = dfs_test.reached(t); |
| 106 | 106 |
pp = dfs_test.path(t); |
| 107 | 107 |
} |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
void checkDfsFunctionCompile() |
| 111 | 111 |
{
|
| 112 | 112 |
typedef int VType; |
| 113 | 113 |
typedef concepts::Digraph Digraph; |
| 114 | 114 |
typedef Digraph::Arc Arc; |
| 115 | 115 |
typedef Digraph::Node Node; |
| 116 | 116 |
|
| 117 | 117 |
Digraph g; |
| 118 | 118 |
bool b; |
| 119 | 119 |
dfs(g).run(Node()); |
| 120 | 120 |
b=dfs(g).run(Node(),Node()); |
| 121 | 121 |
dfs(g).run(); |
| 122 | 122 |
dfs(g) |
| 123 | 123 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 124 | 124 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 125 | 125 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 126 | 126 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 127 | 127 |
.run(Node()); |
| 128 | 128 |
b=dfs(g) |
| 129 | 129 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 130 | 130 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 131 | 131 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 132 | 132 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 133 | 133 |
.path(concepts::Path<Digraph>()) |
| 134 | 134 |
.dist(VType()) |
| 135 | 135 |
.run(Node(),Node()); |
| 136 | 136 |
dfs(g) |
| 137 | 137 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 138 | 138 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 139 | 139 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 140 | 140 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 141 | 141 |
.run(); |
| 142 | 142 |
} |
| 143 | 143 |
|
| 144 | 144 |
template <class Digraph> |
| 145 | 145 |
void checkDfs() {
|
| 146 | 146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 147 |
|
| 148 | 148 |
Digraph G; |
| 149 | 149 |
Node s, t; |
| 150 | 150 |
Node s1, t1; |
| 151 | 151 |
|
| 152 | 152 |
std::istringstream input(test_lgf); |
| 153 | 153 |
digraphReader(G, input). |
| 154 | 154 |
node("source", s).
|
| 155 | 155 |
node("target", t).
|
| 156 | 156 |
node("source1", s1).
|
| 157 | 157 |
node("target1", t1).
|
| 158 | 158 |
run(); |
| 159 | 159 |
|
| 160 | 160 |
Dfs<Digraph> dfs_test(G); |
| 161 | 161 |
dfs_test.run(s); |
| 162 | 162 |
|
| 163 | 163 |
Path<Digraph> p = dfs_test.path(t); |
| 164 | 164 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
| 165 | 165 |
check(checkPath(G, p),"path() found a wrong path."); |
| 166 | 166 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 167 | 167 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 168 | 168 |
|
| 169 | 169 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 170 | 170 |
if (dfs_test.reached(v)) {
|
| 171 | 171 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
| 172 | 172 |
if (dfs_test.predArc(v)!=INVALID ) {
|
| 173 | 173 |
Arc e=dfs_test.predArc(v); |
| 174 | 174 |
Node u=G.source(e); |
| 175 | 175 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
| 176 | 176 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
| 177 | 177 |
"Wrong distance. (" << dfs_test.dist(u) << "->"
|
| 178 | 178 |
<< dfs_test.dist(v) << ")"); |
| 179 | 179 |
} |
| 180 | 180 |
} |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
{
|
| 184 | 184 |
Dfs<Digraph> dfs(G); |
| 185 | 185 |
check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); |
| 186 | 186 |
} |
| 187 |
|
|
| 187 |
|
|
| 188 | 188 |
{
|
| 189 | 189 |
NullMap<Node,Arc> myPredMap; |
| 190 | 190 |
dfs(G).predMap(myPredMap).run(s); |
| 191 | 191 |
} |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
int main() |
| 195 | 195 |
{
|
| 196 | 196 |
checkDfs<ListDigraph>(); |
| 197 | 197 |
checkDfs<SmartDigraph>(); |
| 198 | 198 |
return 0; |
| 199 | 199 |
} |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
#include <lemon/smart_graph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/lgf_reader.h> |
| 22 | 22 |
#include <lemon/error.h> |
| 23 | 23 |
|
| 24 | 24 |
#include "test_tools.h" |
| 25 | 25 |
|
| 26 | 26 |
using namespace std; |
| 27 | 27 |
using namespace lemon; |
| 28 | 28 |
|
| 29 | 29 |
void digraph_copy_test() {
|
| 30 | 30 |
const int nn = 10; |
| 31 | 31 |
|
| 32 | 32 |
// Build a digraph |
| 33 | 33 |
SmartDigraph from; |
| 34 | 34 |
SmartDigraph::NodeMap<int> fnm(from); |
| 35 | 35 |
SmartDigraph::ArcMap<int> fam(from); |
| 36 | 36 |
SmartDigraph::Node fn = INVALID; |
| 37 | 37 |
SmartDigraph::Arc fa = INVALID; |
| 38 | 38 |
|
| 39 | 39 |
std::vector<SmartDigraph::Node> fnv; |
| 40 | 40 |
for (int i = 0; i < nn; ++i) {
|
| 41 | 41 |
SmartDigraph::Node node = from.addNode(); |
| 42 | 42 |
fnv.push_back(node); |
| 43 | 43 |
fnm[node] = i * i; |
| 44 | 44 |
if (i == 0) fn = node; |
| 45 | 45 |
} |
| 46 | 46 |
|
| 47 | 47 |
for (int i = 0; i < nn; ++i) {
|
| 48 | 48 |
for (int j = 0; j < nn; ++j) {
|
| 49 | 49 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
| 50 | 50 |
fam[arc] = i + j * j; |
| 51 | 51 |
if (i == 0 && j == 0) fa = arc; |
| 52 | 52 |
} |
| 53 | 53 |
} |
| 54 | 54 |
|
| 55 | 55 |
// Test digraph copy |
| 56 | 56 |
ListDigraph to; |
| 57 | 57 |
ListDigraph::NodeMap<int> tnm(to); |
| 58 | 58 |
ListDigraph::ArcMap<int> tam(to); |
| 59 | 59 |
ListDigraph::Node tn; |
| 60 | 60 |
ListDigraph::Arc ta; |
| 61 | 61 |
|
| 62 | 62 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
| 63 | 63 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
| 64 | 64 |
|
| 65 | 65 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
| 66 | 66 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
| 67 | 67 |
|
| 68 | 68 |
digraphCopy(from, to). |
| 69 | 69 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
| 70 | 70 |
nodeRef(nr).arcRef(er). |
| 71 | 71 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
| 72 | 72 |
node(fn, tn).arc(fa, ta).run(); |
| 73 |
|
|
| 73 |
|
|
| 74 | 74 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
| 75 | 75 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
| 76 | 76 |
|
| 77 | 77 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
|
| 78 | 78 |
check(ncr[nr[it]] == it, "Wrong copy."); |
| 79 | 79 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
|
| 83 | 83 |
check(ecr[er[it]] == it, "Wrong copy."); |
| 84 | 84 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
| 85 | 85 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
| 86 | 86 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
|
| 90 | 90 |
check(nr[ncr[it]] == it, "Wrong copy."); |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 | 93 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
|
| 94 | 94 |
check(er[ecr[it]] == it, "Wrong copy."); |
| 95 | 95 |
} |
| 96 | 96 |
check(tn == nr[fn], "Wrong copy."); |
| 97 | 97 |
check(ta == er[fa], "Wrong copy."); |
| 98 | 98 |
|
| 99 | 99 |
// Test repeated copy |
| 100 | 100 |
digraphCopy(from, to).run(); |
| 101 |
|
|
| 101 |
|
|
| 102 | 102 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
| 103 | 103 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
void graph_copy_test() {
|
| 107 | 107 |
const int nn = 10; |
| 108 | 108 |
|
| 109 | 109 |
// Build a graph |
| 110 | 110 |
SmartGraph from; |
| 111 | 111 |
SmartGraph::NodeMap<int> fnm(from); |
| 112 | 112 |
SmartGraph::ArcMap<int> fam(from); |
| 113 | 113 |
SmartGraph::EdgeMap<int> fem(from); |
| 114 | 114 |
SmartGraph::Node fn = INVALID; |
| 115 | 115 |
SmartGraph::Arc fa = INVALID; |
| 116 | 116 |
SmartGraph::Edge fe = INVALID; |
| 117 | 117 |
|
| 118 | 118 |
std::vector<SmartGraph::Node> fnv; |
| 119 | 119 |
for (int i = 0; i < nn; ++i) {
|
| 120 | 120 |
SmartGraph::Node node = from.addNode(); |
| 121 | 121 |
fnv.push_back(node); |
| 122 | 122 |
fnm[node] = i * i; |
| 123 | 123 |
if (i == 0) fn = node; |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
for (int i = 0; i < nn; ++i) {
|
| 127 | 127 |
for (int j = 0; j < nn; ++j) {
|
| 128 | 128 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
| 129 | 129 |
fem[edge] = i * i + j * j; |
| 130 | 130 |
fam[from.direct(edge, true)] = i + j * j; |
| 131 | 131 |
fam[from.direct(edge, false)] = i * i + j; |
| 132 | 132 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
| 133 | 133 |
if (i == 0 && j == 0) fe = edge; |
| 134 | 134 |
} |
| 135 | 135 |
} |
| 136 | 136 |
|
| 137 | 137 |
// Test graph copy |
| 138 | 138 |
ListGraph to; |
| 139 | 139 |
ListGraph::NodeMap<int> tnm(to); |
| 140 | 140 |
ListGraph::ArcMap<int> tam(to); |
| 141 | 141 |
ListGraph::EdgeMap<int> tem(to); |
| 142 | 142 |
ListGraph::Node tn; |
| 143 | 143 |
ListGraph::Arc ta; |
| 144 | 144 |
ListGraph::Edge te; |
| 145 | 145 |
|
| 146 | 146 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
| 147 | 147 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
| 148 | 148 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
| 149 | 149 |
|
| 150 | 150 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
| 151 | 151 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
| 152 | 152 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
| 153 | 153 |
|
| 154 | 154 |
graphCopy(from, to). |
| 155 | 155 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
| 156 | 156 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
| 157 | 157 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
| 158 | 158 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
| 159 | 159 |
|
| 160 | 160 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
| 161 | 161 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
| 162 | 162 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
| 163 | 163 |
|
| 164 | 164 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
|
| 165 | 165 |
check(ncr[nr[it]] == it, "Wrong copy."); |
| 166 | 166 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
| 167 | 167 |
} |
| 168 | 168 |
|
| 169 | 169 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
|
| 170 | 170 |
check(acr[ar[it]] == it, "Wrong copy."); |
| 171 | 171 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
| 172 | 172 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
| 173 | 173 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
|
| 177 | 177 |
check(ecr[er[it]] == it, "Wrong copy."); |
| 178 | 178 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
| 179 | 179 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
| 180 | 180 |
"Wrong copy."); |
| 181 | 181 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
| 182 | 182 |
"Wrong copy."); |
| 183 | 183 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
| 184 | 184 |
"Wrong copy."); |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
|
| 188 | 188 |
check(nr[ncr[it]] == it, "Wrong copy."); |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
|
| 192 | 192 |
check(ar[acr[it]] == it, "Wrong copy."); |
| 193 | 193 |
} |
| 194 | 194 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
|
| 195 | 195 |
check(er[ecr[it]] == it, "Wrong copy."); |
| 196 | 196 |
} |
| 197 | 197 |
check(tn == nr[fn], "Wrong copy."); |
| 198 | 198 |
check(ta == ar[fa], "Wrong copy."); |
| 199 | 199 |
check(te == er[fe], "Wrong copy."); |
| 200 | 200 |
|
| 201 | 201 |
// Test repeated copy |
| 202 | 202 |
graphCopy(from, to).run(); |
| 203 |
|
|
| 203 |
|
|
| 204 | 204 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
| 205 | 205 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
| 206 | 206 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
| 207 | 207 |
} |
| 208 | 208 |
|
| 209 | 209 |
|
| 210 | 210 |
int main() {
|
| 211 | 211 |
digraph_copy_test(); |
| 212 | 212 |
graph_copy_test(); |
| 213 | 213 |
|
| 214 | 214 |
return 0; |
| 215 | 215 |
} |
| 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-2011 |
| 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 |
#include <lemon/list_graph.h> |
| 20 | 20 |
#include <lemon/lgf_reader.h> |
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
|
| 23 | 23 |
using namespace lemon; |
| 24 | 24 |
|
| 25 | 25 |
char test_lgf[] = |
| 26 | 26 |
"@nodes\n" |
| 27 | 27 |
"label\n" |
| 28 | 28 |
"0\n" |
| 29 | 29 |
"1\n" |
| 30 | 30 |
"@arcs\n" |
| 31 | 31 |
" label\n" |
| 32 | 32 |
"0 1 0\n" |
| 33 | 33 |
"1 0 1\n" |
| 34 | 34 |
"@attributes\n" |
| 35 | 35 |
"source 0\n" |
| 36 | 36 |
"target 1\n"; |
| 37 | 37 |
|
| 38 | 38 |
char test_lgf_nomap[] = |
| 39 | 39 |
"@nodes\n" |
| 40 | 40 |
"label\n" |
| 41 | 41 |
"0\n" |
| 42 | 42 |
"1\n" |
| 43 | 43 |
"@arcs\n" |
| 44 | 44 |
" -\n" |
| 45 | 45 |
"0 1\n"; |
| 46 | 46 |
|
| 47 | 47 |
char test_lgf_bad1[] = |
| 48 | 48 |
"@nodes\n" |
| 49 | 49 |
"label\n" |
| 50 | 50 |
"0\n" |
| 51 | 51 |
"1\n" |
| 52 | 52 |
"@arcs\n" |
| 53 | 53 |
" - another\n" |
| 54 | 54 |
"0 1\n"; |
| 55 | 55 |
|
| 56 | 56 |
char test_lgf_bad2[] = |
| 57 | 57 |
"@nodes\n" |
| 58 | 58 |
"label\n" |
| 59 | 59 |
"0\n" |
| 60 | 60 |
"1\n" |
| 61 | 61 |
"@arcs\n" |
| 62 | 62 |
" label -\n" |
| 63 | 63 |
"0 1\n"; |
| 64 | 64 |
|
| 65 | 65 |
|
| 66 |
int main() |
|
| 66 |
int main() |
|
| 67 | 67 |
{
|
| 68 | 68 |
{
|
| 69 |
ListDigraph d; |
|
| 69 |
ListDigraph d; |
|
| 70 | 70 |
ListDigraph::Node s,t; |
| 71 | 71 |
ListDigraph::ArcMap<int> label(d); |
| 72 | 72 |
std::istringstream input(test_lgf); |
| 73 | 73 |
digraphReader(d, input). |
| 74 | 74 |
node("source", s).
|
| 75 | 75 |
node("target", t).
|
| 76 | 76 |
arcMap("label", label).
|
| 77 | 77 |
run(); |
| 78 | 78 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
| 79 | 79 |
check(countArcs(d) == 2,"There should be 2 arcs"); |
| 80 | 80 |
} |
| 81 | 81 |
{
|
| 82 | 82 |
ListGraph g; |
| 83 | 83 |
ListGraph::Node s,t; |
| 84 | 84 |
ListGraph::EdgeMap<int> label(g); |
| 85 | 85 |
std::istringstream input(test_lgf); |
| 86 | 86 |
graphReader(g, input). |
| 87 | 87 |
node("source", s).
|
| 88 | 88 |
node("target", t).
|
| 89 | 89 |
edgeMap("label", label).
|
| 90 | 90 |
run(); |
| 91 | 91 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
| 92 | 92 |
check(countEdges(g) == 2,"There should be 2 arcs"); |
| 93 | 93 |
} |
| 94 | 94 |
|
| 95 | 95 |
{
|
| 96 |
ListDigraph d; |
|
| 96 |
ListDigraph d; |
|
| 97 | 97 |
std::istringstream input(test_lgf_nomap); |
| 98 | 98 |
digraphReader(d, input). |
| 99 | 99 |
run(); |
| 100 | 100 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
| 101 | 101 |
check(countArcs(d) == 1,"There should be 1 arc"); |
| 102 | 102 |
} |
| 103 | 103 |
{
|
| 104 | 104 |
ListGraph g; |
| 105 | 105 |
std::istringstream input(test_lgf_nomap); |
| 106 | 106 |
graphReader(g, input). |
| 107 | 107 |
run(); |
| 108 | 108 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
| 109 | 109 |
check(countEdges(g) == 1,"There should be 1 edge"); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
{
|
| 113 |
ListDigraph d; |
|
| 113 |
ListDigraph d; |
|
| 114 | 114 |
std::istringstream input(test_lgf_bad1); |
| 115 | 115 |
bool ok=false; |
| 116 | 116 |
try {
|
| 117 | 117 |
digraphReader(d, input). |
| 118 | 118 |
run(); |
| 119 | 119 |
} |
| 120 |
catch (FormatError& error) |
|
| 120 |
catch (FormatError& error) |
|
| 121 | 121 |
{
|
| 122 | 122 |
ok = true; |
| 123 | 123 |
} |
| 124 | 124 |
check(ok,"FormatError exception should have occured"); |
| 125 | 125 |
} |
| 126 | 126 |
{
|
| 127 | 127 |
ListGraph g; |
| 128 | 128 |
std::istringstream input(test_lgf_bad1); |
| 129 | 129 |
bool ok=false; |
| 130 | 130 |
try {
|
| 131 | 131 |
graphReader(g, input). |
| 132 | 132 |
run(); |
| 133 | 133 |
} |
| 134 | 134 |
catch (FormatError& error) |
| 135 | 135 |
{
|
| 136 | 136 |
ok = true; |
| 137 | 137 |
} |
| 138 | 138 |
check(ok,"FormatError exception should have occured"); |
| 139 | 139 |
} |
| 140 | 140 |
|
| 141 | 141 |
{
|
| 142 |
ListDigraph d; |
|
| 142 |
ListDigraph d; |
|
| 143 | 143 |
std::istringstream input(test_lgf_bad2); |
| 144 | 144 |
bool ok=false; |
| 145 | 145 |
try {
|
| 146 | 146 |
digraphReader(d, input). |
| 147 | 147 |
run(); |
| 148 | 148 |
} |
| 149 | 149 |
catch (FormatError& error) |
| 150 | 150 |
{
|
| 151 | 151 |
ok = true; |
| 152 | 152 |
} |
| 153 | 153 |
check(ok,"FormatError exception should have occured"); |
| 154 | 154 |
} |
| 155 | 155 |
{
|
| 156 | 156 |
ListGraph g; |
| 157 | 157 |
std::istringstream input(test_lgf_bad2); |
| 158 | 158 |
bool ok=false; |
| 159 | 159 |
try {
|
| 160 | 160 |
graphReader(g, input). |
| 161 | 161 |
run(); |
| 162 | 162 |
} |
| 163 | 163 |
catch (FormatError& error) |
| 164 | 164 |
{
|
| 165 | 165 |
ok = true; |
| 166 | 166 |
} |
| 167 | 167 |
check(ok,"FormatError exception should have occured"); |
| 168 | 168 |
} |
| 169 | 169 |
} |
| 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 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2011 |
|
| 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 |
#include <deque> |
| 20 | 20 |
#include <set> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concept_check.h> |
| 23 | 23 |
#include <lemon/concepts/maps.h> |
| 24 | 24 |
#include <lemon/maps.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
using namespace lemon::concepts; |
| 30 | 30 |
|
| 31 | 31 |
struct A {};
|
| 32 | 32 |
inline bool operator<(A, A) { return true; }
|
| 33 | 33 |
struct B {};
|
| 34 | 34 |
|
| 35 | 35 |
class C {
|
| 36 | 36 |
int x; |
| 37 | 37 |
public: |
| 38 | 38 |
C(int _x) : x(_x) {}
|
| 39 | 39 |
}; |
| 40 | 40 |
|
| 41 | 41 |
class F {
|
| 42 | 42 |
public: |
| 43 | 43 |
typedef A argument_type; |
| 44 | 44 |
typedef B result_type; |
| 45 | 45 |
|
| 46 | 46 |
B operator()(const A&) const { return B(); }
|
| 47 | 47 |
private: |
| 48 | 48 |
F& operator=(const F&); |
| 49 | 49 |
}; |
| 50 | 50 |
|
| 51 | 51 |
int func(A) { return 3; }
|
| 52 | 52 |
|
| 53 | 53 |
int binc(int a, B) { return a+1; }
|
| 54 | 54 |
|
| 55 | 55 |
typedef ReadMap<A, double> DoubleMap; |
| 56 | 56 |
typedef ReadWriteMap<A, double> DoubleWriteMap; |
| 57 | 57 |
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
| 58 | 58 |
|
| 59 | 59 |
typedef ReadMap<A, bool> BoolMap; |
| 60 | 60 |
typedef ReadWriteMap<A, bool> BoolWriteMap; |
| 61 | 61 |
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
| 62 | 62 |
|
| 63 | 63 |
int main() |
| 64 | 64 |
{
|
| 65 | 65 |
// Map concepts |
| 66 | 66 |
checkConcept<ReadMap<A,B>, ReadMap<A,B> >(); |
| 67 | 67 |
checkConcept<ReadMap<A,C>, ReadMap<A,C> >(); |
| 68 | 68 |
checkConcept<WriteMap<A,B>, WriteMap<A,B> >(); |
| 69 | 69 |
checkConcept<WriteMap<A,C>, WriteMap<A,C> >(); |
| 70 | 70 |
checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >(); |
| 71 | 71 |
checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >(); |
| 72 |
checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >(); |
|
| 73 |
checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >(); |
|
| 72 |
checkConcept<ReferenceMap<A,B,B&,const B&>, |
|
| 73 |
ReferenceMap<A,B,B&,const B&> >(); |
|
| 74 |
checkConcept<ReferenceMap<A,C,C&,const C&>, |
|
| 75 |
ReferenceMap<A,C,C&,const C&> >(); |
|
| 74 | 76 |
|
| 75 | 77 |
// NullMap |
| 76 | 78 |
{
|
| 77 | 79 |
checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
| 78 | 80 |
NullMap<A,B> map1; |
| 79 | 81 |
NullMap<A,B> map2 = map1; |
| 80 | 82 |
map1 = nullMap<A,B>(); |
| 81 | 83 |
} |
| 82 | 84 |
|
| 83 | 85 |
// ConstMap |
| 84 | 86 |
{
|
| 85 | 87 |
checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
| 86 | 88 |
checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
| 87 | 89 |
ConstMap<A,B> map1; |
| 88 | 90 |
ConstMap<A,B> map2 = B(); |
| 89 | 91 |
ConstMap<A,B> map3 = map1; |
| 90 | 92 |
map1 = constMap<A>(B()); |
| 91 | 93 |
map1 = constMap<A,B>(); |
| 92 | 94 |
map1.setAll(B()); |
| 93 | 95 |
ConstMap<A,C> map4(C(1)); |
| 94 | 96 |
ConstMap<A,C> map5 = map4; |
| 95 | 97 |
map4 = constMap<A>(C(2)); |
| 96 | 98 |
map4.setAll(C(3)); |
| 97 | 99 |
|
| 98 | 100 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >(); |
| 99 | 101 |
check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap"); |
| 100 | 102 |
|
| 101 | 103 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >(); |
| 102 | 104 |
ConstMap<A,Const<int,10> > map6; |
| 103 | 105 |
ConstMap<A,Const<int,10> > map7 = map6; |
| 104 | 106 |
map6 = constMap<A,int,10>(); |
| 105 | 107 |
map7 = constMap<A,Const<int,10> >(); |
| 106 | 108 |
check(map6[A()] == 10 && map7[A()] == 10, |
| 107 | 109 |
"Something is wrong with ConstMap"); |
| 108 | 110 |
} |
| 109 | 111 |
|
| 110 | 112 |
// IdentityMap |
| 111 | 113 |
{
|
| 112 | 114 |
checkConcept<ReadMap<A,A>, IdentityMap<A> >(); |
| 113 | 115 |
IdentityMap<A> map1; |
| 114 | 116 |
IdentityMap<A> map2 = map1; |
| 115 | 117 |
map1 = identityMap<A>(); |
| 116 | 118 |
|
| 117 | 119 |
checkConcept<ReadMap<double,double>, IdentityMap<double> >(); |
| 118 | 120 |
check(identityMap<double>()[1.0] == 1.0 && |
| 119 | 121 |
identityMap<double>()[3.14] == 3.14, |
| 120 | 122 |
"Something is wrong with IdentityMap"); |
| 121 | 123 |
} |
| 122 | 124 |
|
| 123 | 125 |
// RangeMap |
| 124 | 126 |
{
|
| 125 | 127 |
checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >(); |
| 126 | 128 |
RangeMap<B> map1; |
| 127 | 129 |
RangeMap<B> map2(10); |
| 128 | 130 |
RangeMap<B> map3(10,B()); |
| 129 | 131 |
RangeMap<B> map4 = map1; |
| 130 | 132 |
RangeMap<B> map5 = rangeMap<B>(); |
| 131 | 133 |
RangeMap<B> map6 = rangeMap<B>(10); |
| 132 | 134 |
RangeMap<B> map7 = rangeMap(10,B()); |
| 133 | 135 |
|
| 134 | 136 |
checkConcept< ReferenceMap<int, double, double&, const double&>, |
| 135 | 137 |
RangeMap<double> >(); |
| 136 | 138 |
std::vector<double> v(10, 0); |
| 137 | 139 |
v[5] = 100; |
| 138 | 140 |
RangeMap<double> map8(v); |
| 139 | 141 |
RangeMap<double> map9 = rangeMap(v); |
| 140 | 142 |
check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, |
| 141 | 143 |
"Something is wrong with RangeMap"); |
| 142 | 144 |
} |
| 143 | 145 |
|
| 144 | 146 |
// SparseMap |
| 145 | 147 |
{
|
| 146 | 148 |
checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
| 147 | 149 |
SparseMap<A,B> map1; |
| 148 | 150 |
SparseMap<A,B> map2 = B(); |
| 149 | 151 |
SparseMap<A,B> map3 = sparseMap<A,B>(); |
| 150 | 152 |
SparseMap<A,B> map4 = sparseMap<A>(B()); |
| 151 | 153 |
|
| 152 | 154 |
checkConcept< ReferenceMap<double, int, int&, const int&>, |
| 153 | 155 |
SparseMap<double, int> >(); |
| 154 | 156 |
std::map<double, int> m; |
| 155 | 157 |
SparseMap<double, int> map5(m); |
| 156 | 158 |
SparseMap<double, int> map6(m,10); |
| 157 | 159 |
SparseMap<double, int> map7 = sparseMap(m); |
| 158 | 160 |
SparseMap<double, int> map8 = sparseMap(m,10); |
| 159 | 161 |
|
| 160 | 162 |
check(map5[1.0] == 0 && map5[3.14] == 0 && |
| 161 | 163 |
map6[1.0] == 10 && map6[3.14] == 10, |
| 162 | 164 |
"Something is wrong with SparseMap"); |
| 163 | 165 |
map5[1.0] = map6[3.14] = 100; |
| 164 | 166 |
check(map5[1.0] == 100 && map5[3.14] == 0 && |
| 165 | 167 |
map6[1.0] == 10 && map6[3.14] == 100, |
| 166 | 168 |
"Something is wrong with SparseMap"); |
| 167 | 169 |
} |
| 168 | 170 |
|
| 169 | 171 |
// ComposeMap |
| 170 | 172 |
{
|
| 171 | 173 |
typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap; |
| 172 | 174 |
checkConcept<ReadMap<B,double>, CompMap>(); |
| 173 | 175 |
CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>()); |
| 174 | 176 |
CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>()); |
| 175 | 177 |
|
| 176 | 178 |
SparseMap<double, bool> m1(false); m1[3.14] = true; |
| 177 | 179 |
RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14; |
| 178 | 180 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], |
| 179 | 181 |
"Something is wrong with ComposeMap") |
| 180 | 182 |
} |
| 181 | 183 |
|
| 182 | 184 |
// CombineMap |
| 183 | 185 |
{
|
| 184 | 186 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
| 185 | 187 |
checkConcept<ReadMap<A,double>, CombMap>(); |
| 186 | 188 |
CombMap map1 = CombMap(DoubleMap(), DoubleMap()); |
| 187 | 189 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
| 188 | 190 |
|
| 189 | 191 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
| 190 | 192 |
"Something is wrong with CombineMap"); |
| 191 | 193 |
} |
| 192 | 194 |
|
| 193 | 195 |
// FunctorToMap, MapToFunctor |
| 194 | 196 |
{
|
| 195 | 197 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
| 196 | 198 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
| 197 | 199 |
FunctorToMap<F> map1; |
| 198 | 200 |
FunctorToMap<F> map2 = FunctorToMap<F>(F()); |
| 199 | 201 |
B b = functorToMap(F())[A()]; |
| 200 | 202 |
|
| 201 | 203 |
checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
| 202 |
MapToFunctor<ReadMap<A,B> > map = |
|
| 204 |
MapToFunctor<ReadMap<A,B> > map = |
|
| 205 |
MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>()); |
|
| 203 | 206 |
|
| 204 | 207 |
check(functorToMap(&func)[A()] == 3, |
| 205 | 208 |
"Something is wrong with FunctorToMap"); |
| 206 | 209 |
check(mapToFunctor(constMap<A,int>(2))(A()) == 2, |
| 207 | 210 |
"Something is wrong with MapToFunctor"); |
| 208 | 211 |
check(mapToFunctor(functorToMap(&func))(A()) == 3 && |
| 209 | 212 |
mapToFunctor(functorToMap(&func))[A()] == 3, |
| 210 | 213 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
| 211 | 214 |
check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2, |
| 212 | 215 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
| 213 | 216 |
} |
| 214 | 217 |
|
| 215 | 218 |
// ConvertMap |
| 216 | 219 |
{
|
| 217 | 220 |
checkConcept<ReadMap<double,double>, |
| 218 | 221 |
ConvertMap<ReadMap<double, int>, double> >(); |
| 219 | 222 |
ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true)); |
| 220 | 223 |
ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false)); |
| 221 | 224 |
} |
| 222 | 225 |
|
| 223 | 226 |
// ForkMap |
| 224 | 227 |
{
|
| 225 | 228 |
checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
| 226 | 229 |
|
| 227 | 230 |
typedef RangeMap<double> RM; |
| 228 | 231 |
typedef SparseMap<int, double> SM; |
| 229 | 232 |
RM m1(10, -1); |
| 230 | 233 |
SM m2(-1); |
| 231 | 234 |
checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >(); |
| 232 | 235 |
checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >(); |
| 233 | 236 |
ForkMap<RM, SM> map1(m1,m2); |
| 234 | 237 |
ForkMap<SM, RM> map2 = forkMap(m2,m1); |
| 235 | 238 |
map2.set(5, 10); |
| 236 | 239 |
check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && |
| 237 | 240 |
m2[5] == 10 && map2[1] == -1 && map2[5] == 10, |
| 238 | 241 |
"Something is wrong with ForkMap"); |
| 239 | 242 |
} |
| 240 | 243 |
|
| 241 | 244 |
// Arithmetic maps: |
| 242 | 245 |
// - AddMap, SubMap, MulMap, DivMap |
| 243 | 246 |
// - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap |
| 244 | 247 |
// - NegMap, NegWriteMap, AbsMap |
| 245 | 248 |
{
|
| 246 | 249 |
checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >(); |
| 247 | 250 |
checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >(); |
| 248 | 251 |
checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >(); |
| 249 | 252 |
checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >(); |
| 250 | 253 |
|
| 251 | 254 |
ConstMap<int, double> c1(1.0), c2(3.14); |
| 252 | 255 |
IdentityMap<int> im; |
| 253 | 256 |
ConvertMap<IdentityMap<int>, double> id(im); |
| 254 | 257 |
check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0, |
| 255 | 258 |
"Something is wrong with AddMap"); |
| 256 | 259 |
check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0, |
| 257 | 260 |
"Something is wrong with SubMap"); |
| 258 | 261 |
check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28, |
| 259 | 262 |
"Something is wrong with MulMap"); |
| 260 | 263 |
check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57, |
| 261 | 264 |
"Something is wrong with DivMap"); |
| 262 | 265 |
|
| 263 | 266 |
checkConcept<DoubleMap, ShiftMap<DoubleMap> >(); |
| 264 | 267 |
checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >(); |
| 265 | 268 |
checkConcept<DoubleMap, ScaleMap<DoubleMap> >(); |
| 266 | 269 |
checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >(); |
| 267 | 270 |
checkConcept<DoubleMap, NegMap<DoubleMap> >(); |
| 268 | 271 |
checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >(); |
| 269 | 272 |
checkConcept<DoubleMap, AbsMap<DoubleMap> >(); |
| 270 | 273 |
|
| 271 | 274 |
check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0, |
| 272 | 275 |
"Something is wrong with ShiftMap"); |
| 273 | 276 |
check(shiftWriteMap(id, 2.0)[1] == 3.0 && |
| 274 | 277 |
shiftWriteMap(id, 2.0)[10] == 12.0, |
| 275 | 278 |
"Something is wrong with ShiftWriteMap"); |
| 276 | 279 |
check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0, |
| 277 | 280 |
"Something is wrong with ScaleMap"); |
| 278 | 281 |
check(scaleWriteMap(id, 2.0)[1] == 2.0 && |
| 279 | 282 |
scaleWriteMap(id, 2.0)[10] == 20.0, |
| 280 | 283 |
"Something is wrong with ScaleWriteMap"); |
| 281 | 284 |
check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0, |
| 282 | 285 |
"Something is wrong with NegMap"); |
| 283 | 286 |
check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0, |
| 284 | 287 |
"Something is wrong with NegWriteMap"); |
| 285 | 288 |
check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0, |
| 286 | 289 |
"Something is wrong with AbsMap"); |
| 287 | 290 |
} |
| 288 | 291 |
|
| 289 | 292 |
// Logical maps: |
| 290 | 293 |
// - TrueMap, FalseMap |
| 291 | 294 |
// - AndMap, OrMap |
| 292 | 295 |
// - NotMap, NotWriteMap |
| 293 | 296 |
// - EqualMap, LessMap |
| 294 | 297 |
{
|
| 295 | 298 |
checkConcept<BoolMap, TrueMap<A> >(); |
| 296 | 299 |
checkConcept<BoolMap, FalseMap<A> >(); |
| 297 | 300 |
checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >(); |
| 298 | 301 |
checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >(); |
| 299 | 302 |
checkConcept<BoolMap, NotMap<BoolMap> >(); |
| 300 | 303 |
checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >(); |
| 301 | 304 |
checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >(); |
| 302 | 305 |
checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >(); |
| 303 | 306 |
|
| 304 | 307 |
TrueMap<int> tm; |
| 305 | 308 |
FalseMap<int> fm; |
| 306 | 309 |
RangeMap<bool> rm(2); |
| 307 | 310 |
rm[0] = true; rm[1] = false; |
| 308 | 311 |
check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && |
| 309 | 312 |
!andMap(fm,rm)[0] && !andMap(fm,rm)[1], |
| 310 | 313 |
"Something is wrong with AndMap"); |
| 311 | 314 |
check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && |
| 312 | 315 |
orMap(fm,rm)[0] && !orMap(fm,rm)[1], |
| 313 | 316 |
"Something is wrong with OrMap"); |
| 314 | 317 |
check(!notMap(rm)[0] && notMap(rm)[1], |
| 315 | 318 |
"Something is wrong with NotMap"); |
| 316 | 319 |
check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], |
| 317 | 320 |
"Something is wrong with NotWriteMap"); |
| 318 | 321 |
|
| 319 | 322 |
ConstMap<int, double> cm(2.0); |
| 320 | 323 |
IdentityMap<int> im; |
| 321 | 324 |
ConvertMap<IdentityMap<int>, double> id(im); |
| 322 | 325 |
check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3], |
| 323 | 326 |
"Something is wrong with LessMap"); |
| 324 | 327 |
check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3], |
| 325 | 328 |
"Something is wrong with EqualMap"); |
| 326 | 329 |
} |
| 327 | 330 |
|
| 328 | 331 |
// LoggerBoolMap |
| 329 | 332 |
{
|
| 330 | 333 |
typedef std::vector<int> vec; |
| 331 | 334 |
vec v1; |
| 332 | 335 |
vec v2(10); |
| 333 | 336 |
LoggerBoolMap<std::back_insert_iterator<vec> > |
| 334 | 337 |
map1(std::back_inserter(v1)); |
| 335 | 338 |
LoggerBoolMap<vec::iterator> map2(v2.begin()); |
| 336 | 339 |
map1.set(10, false); |
| 337 | 340 |
map1.set(20, true); map2.set(20, true); |
| 338 | 341 |
map1.set(30, false); map2.set(40, false); |
| 339 | 342 |
map1.set(50, true); map2.set(50, true); |
| 340 | 343 |
map1.set(60, true); map2.set(60, true); |
| 341 | 344 |
check(v1.size() == 3 && v2.size() == 10 && |
| 342 | 345 |
v1[0]==20 && v1[1]==50 && v1[2]==60 && |
| 343 | 346 |
v2[0]==20 && v2[1]==50 && v2[2]==60, |
| 344 | 347 |
"Something is wrong with LoggerBoolMap"); |
| 345 | 348 |
|
| 346 | 349 |
int i = 0; |
| 347 | 350 |
for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin(); |
| 348 | 351 |
it != map2.end(); ++it ) |
| 349 | 352 |
check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); |
| 350 | 353 |
} |
| 351 | 354 |
|
| 352 | 355 |
return 0; |
| 353 | 356 |
} |
0 comments (0 inline)