[2391] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2553] | 5 | * Copyright (C) 2003-2008 |
---|
[2391] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1118] | 19 | namespace lemon { |
---|
[1114] | 20 | /*! |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | \page graph-io-page Graph Input-Output |
---|
| 24 | |
---|
[1540] | 25 | The standard graph IO enables one to store graphs and additional maps |
---|
| 26 | (i.e. functions on the nodes or edges) in a flexible and efficient way. |
---|
| 27 | Before you read this page you should be familiar with LEMON |
---|
| 28 | \ref graphs "graphs" and \ref maps-page "maps". |
---|
[1114] | 29 | |
---|
| 30 | \section format The general file format |
---|
| 31 | |
---|
[1532] | 32 | The file contains sections in the following order: |
---|
[1114] | 33 | |
---|
| 34 | \li nodeset |
---|
| 35 | \li edgeset |
---|
| 36 | \li nodes |
---|
| 37 | \li edges |
---|
[1532] | 38 | \li attributes |
---|
[1114] | 39 | |
---|
[1540] | 40 | Some of these sections can be omitted, but you will basicly need the nodeset |
---|
| 41 | section (unless your graph has no nodes at all) and the edgeset section |
---|
| 42 | (unless your graph has no edges at all). |
---|
| 43 | |
---|
| 44 | The nodeset section describes the nodes of your graph: it identifies the nodes |
---|
| 45 | and gives the maps defined on them, if any. It starts with the |
---|
| 46 | following line: |
---|
[1522] | 47 | |
---|
| 48 | <tt>\@nodeset</tt> |
---|
| 49 | |
---|
| 50 | The next line contains the names of the nodemaps, separated by whitespaces. Each |
---|
| 51 | following line describes a node in the graph: it contains the values of the |
---|
[1901] | 52 | maps in the right order. The map named "label" should contain unique values |
---|
| 53 | because it is regarded as a label map. These labels need not be numbers but they |
---|
[1540] | 54 | must identify the nodes uniquely for later reference. For example: |
---|
[1114] | 55 | |
---|
| 56 | \code |
---|
| 57 | @nodeset |
---|
[1901] | 58 | label x-coord y-coord color |
---|
[1114] | 59 | 3 1.0 4.0 blue |
---|
| 60 | 5 2.3 5.7 red |
---|
| 61 | 12 7.8 2.3 green |
---|
| 62 | \endcode |
---|
| 63 | |
---|
| 64 | The edgeset section is very similar to the nodeset section, it has |
---|
[1522] | 65 | the same coloumn oriented structure. It starts with the line |
---|
| 66 | |
---|
| 67 | <tt>\@edgeset</tt> |
---|
| 68 | |
---|
[1540] | 69 | The next line contains the whitespace separated list of names of the edge |
---|
| 70 | maps. Each of the next lines describes one edge. The first two elements in |
---|
[1901] | 71 | the line are the labels of the source and target (or tail and head) nodes of the |
---|
| 72 | edge as they occur in the label node map of the nodeset section. You can also |
---|
| 73 | have an optional label map on the edges for later reference (which has to be |
---|
[1540] | 74 | unique in this case). |
---|
[1114] | 75 | |
---|
| 76 | \code |
---|
| 77 | @edgeset |
---|
[1901] | 78 | label weight note |
---|
| 79 | 3 5 a 4.3 a-edge |
---|
| 80 | 5 12 c 2.6 c-edge |
---|
| 81 | 3 12 g 3.4 g-edge |
---|
[1114] | 82 | \endcode |
---|
| 83 | |
---|
[1540] | 84 | The \e nodes section contains <em>labeled (distinguished) nodes</em> |
---|
| 85 | (i.e. nodes having a special |
---|
[1118] | 86 | label on them). The section starts with |
---|
[1522] | 87 | |
---|
| 88 | <tt> \@nodes </tt> |
---|
| 89 | |
---|
| 90 | Each of the next lines contains a label for a node in the graph |
---|
[1901] | 91 | and then the label as described in the \e nodeset section. |
---|
[1114] | 92 | |
---|
| 93 | \code |
---|
| 94 | @nodes |
---|
| 95 | source 3 |
---|
| 96 | target 12 |
---|
| 97 | \endcode |
---|
| 98 | |
---|
[1540] | 99 | The last section describes the <em>labeled (distinguished) edges</em> |
---|
[1333] | 100 | (i.e. edges having a special label on them). It starts with \c \@edges |
---|
[1901] | 101 | and then each line contains the name of the edge and the label. |
---|
[1114] | 102 | |
---|
| 103 | \code |
---|
[1540] | 104 | @edges |
---|
[1114] | 105 | observed c |
---|
| 106 | \endcode |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | The file may contain empty lines and comment lines. The comment lines |
---|
| 110 | start with an \c # character. |
---|
| 111 | |
---|
[1532] | 112 | The attributes section can handle some information about the graph. It |
---|
[1540] | 113 | contains key-value pairs in each line (a key and the mapped value to key). The |
---|
| 114 | key should be a string without whitespaces, the value can be of various types. |
---|
[1532] | 115 | |
---|
| 116 | \code |
---|
| 117 | @attributes |
---|
[1959] | 118 | title "Four colored planar graph" |
---|
[1532] | 119 | author "Balazs DEZSO" |
---|
| 120 | copyright "Lemon Library" |
---|
| 121 | version 12 |
---|
| 122 | \endcode |
---|
| 123 | |
---|
[1901] | 124 | Finally, the file should be closed with \c \@end line. |
---|
[1522] | 125 | |
---|
[1114] | 126 | |
---|
| 127 | \section use Using graph input-output |
---|
[1540] | 128 | |
---|
| 129 | |
---|
| 130 | The graph input and output is based on <em> reading and writing |
---|
| 131 | commands</em>. The user gives reading and writing commands to the reader or |
---|
| 132 | writer class, then he calls the \c run() method that executes all the given |
---|
| 133 | commands. |
---|
[1114] | 134 | |
---|
| 135 | \subsection write Writing a graph |
---|
| 136 | |
---|
[1631] | 137 | The \ref lemon::GraphWriter "GraphWriter" template class |
---|
| 138 | provides the graph output. To write a graph |
---|
[1526] | 139 | you should first give writing commands to the writer. You can declare |
---|
[1540] | 140 | writing command as \c NodeMap or \c EdgeMap writing and labeled Node and |
---|
[1114] | 141 | Edge writing. |
---|
| 142 | |
---|
| 143 | \code |
---|
[1333] | 144 | GraphWriter<ListGraph> writer(std::cout, graph); |
---|
[1114] | 145 | \endcode |
---|
| 146 | |
---|
[1631] | 147 | The \ref lemon::GraphWriter::writeNodeMap() "writeNodeMap()" |
---|
| 148 | function declares a \c NodeMap writing command in the |
---|
| 149 | \ref lemon::GraphWriter "GraphWriter". |
---|
| 150 | You should give a name to the map and the map |
---|
[1901] | 151 | object as parameters. The NodeMap writing command with name "label" should write a |
---|
| 152 | unique map because it will be regarded as a label map. |
---|
[1114] | 153 | |
---|
| 154 | \see IdMap, DescriptorMap |
---|
| 155 | |
---|
| 156 | \code |
---|
[1901] | 157 | IdMap<ListGraph, Node> nodeLabelMap; |
---|
| 158 | writer.writeNodeMap("label", nodeLabelMap); |
---|
[1114] | 159 | |
---|
[1394] | 160 | writer.writeNodeMap("x-coord", xCoordMap); |
---|
| 161 | writer.writeNodeMap("y-coord", yCoordMap); |
---|
| 162 | writer.writeNodeMap("color", colorMap); |
---|
[1114] | 163 | \endcode |
---|
| 164 | |
---|
[1631] | 165 | With the \ref lemon::GraphWriter::writeEdgeMap() "writeEdgeMap()" |
---|
| 166 | member function you can give an edge map |
---|
[1333] | 167 | writing command similar to the NodeMaps. |
---|
[1114] | 168 | |
---|
| 169 | \see IdMap, DescriptorMap |
---|
[1522] | 170 | |
---|
[1114] | 171 | \code |
---|
| 172 | DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > edgeDescMap(graph); |
---|
[1394] | 173 | writer.writeEdgeMap("descriptor", edgeDescMap); |
---|
[1114] | 174 | |
---|
[1394] | 175 | writer.writeEdgeMap("weight", weightMap); |
---|
[1901] | 176 | writer.writeEdgeMap("note", noteMap); |
---|
[1114] | 177 | \endcode |
---|
| 178 | |
---|
[1631] | 179 | With \ref lemon::GraphWriter::writeNode() "writeNode()" |
---|
| 180 | and \ref lemon::GraphWriter::writeEdge() "writeEdge()" |
---|
| 181 | functions you can designate Nodes and |
---|
[1522] | 182 | Edges in the graph. For example, you can write out the source and target node |
---|
| 183 | of a maximum flow instance. |
---|
[1114] | 184 | |
---|
| 185 | \code |
---|
[1394] | 186 | writer.writeNode("source", sourceNode); |
---|
| 187 | writer.writeNode("target", targetNode); |
---|
[1114] | 188 | |
---|
[1394] | 189 | writer.writeEdge("observed", edge); |
---|
[1114] | 190 | \endcode |
---|
| 191 | |
---|
[1631] | 192 | With \ref lemon::GraphWriter::writeAttribute() "writeAttribute()" |
---|
| 193 | function you can write an attribute to the file. |
---|
[1532] | 194 | |
---|
| 195 | \code |
---|
| 196 | writer.writeAttribute("author", "Balazs DEZSO"); |
---|
| 197 | writer.writeAttribute("version", 12); |
---|
| 198 | \endcode |
---|
| 199 | |
---|
[1631] | 200 | After you give all write commands you must call the |
---|
| 201 | \ref lemon::GraphWriter::run() "run()" member |
---|
[1522] | 202 | function, which executes all the writing commands. |
---|
[1114] | 203 | |
---|
| 204 | \code |
---|
| 205 | writer.run(); |
---|
| 206 | \endcode |
---|
| 207 | |
---|
| 208 | \subsection reading Reading a graph |
---|
| 209 | |
---|
[1540] | 210 | The file to be read may contain several maps and labeled nodes or edges. |
---|
[1114] | 211 | If you read a graph you need not read all the maps and items just those |
---|
[1631] | 212 | that you need. The interface of the \ref lemon::GraphReader "GraphReader" |
---|
| 213 | is very similar to |
---|
| 214 | the \ref lemon::GraphWriter "GraphWriter" |
---|
| 215 | but the reading method does not depend on the order of the |
---|
[1114] | 216 | given commands. |
---|
| 217 | |
---|
[2100] | 218 | The reader object assumes that each not read value does not contain |
---|
[1118] | 219 | whitespaces, therefore it has some extra possibilities to control how |
---|
| 220 | it should skip the values when the string representation contains spaces. |
---|
[1114] | 221 | |
---|
| 222 | \code |
---|
[1333] | 223 | GraphReader<ListGraph> reader(std::cin, graph); |
---|
[1114] | 224 | \endcode |
---|
| 225 | |
---|
[1631] | 226 | The \ref lemon::GraphReader::readNodeMap() "readNodeMap()" |
---|
| 227 | function reads a map from the \c nodeset section. |
---|
[1522] | 228 | If there is a map that you do not want to read from the file and there are |
---|
| 229 | whitespaces in the string represenation of the values then you should |
---|
[1631] | 230 | call the \ref lemon::GraphReader::skipNodeMap() "skipNodeMap()" |
---|
| 231 | template member function with proper parameters. |
---|
[1114] | 232 | |
---|
| 233 | \see QuotedStringReader |
---|
[1522] | 234 | |
---|
[1114] | 235 | \code |
---|
[1394] | 236 | reader.readNodeMap("x-coord", xCoordMap); |
---|
| 237 | reader.readNodeMap("y-coord", yCoordMap); |
---|
[1114] | 238 | |
---|
[1394] | 239 | reader.readNodeMap<QuotedStringReader>("label", labelMap); |
---|
[1114] | 240 | reader.skipNodeMap<QuotedStringReader>("description"); |
---|
| 241 | |
---|
[1394] | 242 | reader.readNodeMap("color", colorMap); |
---|
[1114] | 243 | \endcode |
---|
| 244 | |
---|
[1631] | 245 | With the \ref lemon::GraphReader::readEdgeMap() "readEdgeMap()" |
---|
| 246 | member function you can give an edge map |
---|
[1114] | 247 | reading command similar to the NodeMaps. |
---|
| 248 | |
---|
| 249 | \code |
---|
[1394] | 250 | reader.readEdgeMap("weight", weightMap); |
---|
| 251 | reader.readEdgeMap("label", labelMap); |
---|
[1114] | 252 | \endcode |
---|
| 253 | |
---|
[1631] | 254 | With \ref lemon::GraphReader::readNode() "readNode()" |
---|
| 255 | and \ref lemon::GraphReader::readEdge() "readEdge()" |
---|
| 256 | functions you can read labeled Nodes and |
---|
[1114] | 257 | Edges. |
---|
| 258 | |
---|
| 259 | \code |
---|
[1394] | 260 | reader.readNode("source", sourceNode); |
---|
| 261 | reader.readNode("target", targetNode); |
---|
[1114] | 262 | |
---|
[1394] | 263 | reader.readEdge("observed", edge); |
---|
[1114] | 264 | \endcode |
---|
| 265 | |
---|
[1631] | 266 | With \ref lemon::GraphReader::readAttribute() "readAttribute()" |
---|
| 267 | function you can read an attribute from the file. |
---|
[1532] | 268 | |
---|
| 269 | \code |
---|
| 270 | std::string author; |
---|
| 271 | writer.readAttribute("author", author); |
---|
| 272 | int version; |
---|
| 273 | writer.writeAttribute("version", version); |
---|
| 274 | \endcode |
---|
| 275 | |
---|
[1631] | 276 | After you give all read commands you must call the |
---|
| 277 | \ref lemon::GraphReader::run() "run()" member |
---|
[1522] | 278 | function, which executes all the commands. |
---|
[1114] | 279 | |
---|
| 280 | \code |
---|
| 281 | reader.run(); |
---|
| 282 | \endcode |
---|
| 283 | |
---|
[1540] | 284 | \anchor rwbackground |
---|
[1527] | 285 | \section types Background of Reading and Writing |
---|
[1540] | 286 | |
---|
| 287 | |
---|
[1527] | 288 | To read a map (on the nodes or edges) |
---|
[1631] | 289 | the \ref lemon::GraphReader "GraphReader" |
---|
| 290 | should know how to read a Value from the given map. |
---|
[1114] | 291 | By the default implementation the input operator reads a value from |
---|
[2100] | 292 | the stream and the type of the read value is the value type of the given map. |
---|
[1114] | 293 | When the reader should skip a value in the stream, because you do not |
---|
[1527] | 294 | want to store it in a map, the reader skips a character sequence without |
---|
[1540] | 295 | whitespaces. |
---|
[1114] | 296 | |
---|
| 297 | If you want to change the functionality of the reader, you can use |
---|
| 298 | template parameters to specialize it. When you give a reading |
---|
| 299 | command for a map you can give a Reader type as template parameter. |
---|
[1333] | 300 | With this template parameter you can control how the Reader reads |
---|
[1114] | 301 | a value from the stream. |
---|
| 302 | |
---|
| 303 | The reader has the next structure: |
---|
| 304 | \code |
---|
| 305 | struct TypeReader { |
---|
| 306 | typedef TypeName Value; |
---|
| 307 | |
---|
| 308 | void read(std::istream& is, Value& value); |
---|
| 309 | }; |
---|
| 310 | \endcode |
---|
| 311 | |
---|
[1527] | 312 | For example, the \c "strings" nodemap contains strings and you do not need |
---|
[1540] | 313 | the value of the string just the length. Then you can implement an own Reader |
---|
[1114] | 314 | struct. |
---|
| 315 | |
---|
| 316 | \code |
---|
| 317 | struct LengthReader { |
---|
| 318 | typedef int Value; |
---|
| 319 | |
---|
| 320 | void read(std::istream& is, Value& value) { |
---|
| 321 | std::string tmp; |
---|
| 322 | is >> tmp; |
---|
| 323 | value = tmp.length(); |
---|
| 324 | } |
---|
| 325 | }; |
---|
| 326 | ... |
---|
[1394] | 327 | reader.readNodeMap<LengthReader>("strings", lengthMap); |
---|
[1114] | 328 | \endcode |
---|
| 329 | |
---|
| 330 | The global functionality of the reader class can be changed by giving a |
---|
[1526] | 331 | special template parameter to the GraphReader class. By default, the |
---|
[1118] | 332 | template parameter is \c DefaultReaderTraits. A reader traits class |
---|
[1901] | 333 | should provide a nested template class Reader for each type, and a |
---|
[1114] | 334 | DefaultReader for skipping a value. |
---|
| 335 | |
---|
[1901] | 336 | The specialization of writing is very similar to that of reading. |
---|
[1114] | 337 | |
---|
[2502] | 338 | \section undir Undirected and Bipartite graphs |
---|
[1532] | 339 | |
---|
[1909] | 340 | In a file describing an undirected graph (ugraph, for short) you find an |
---|
| 341 | \c uedgeset section instead of the \c edgeset section. The first line of |
---|
[1540] | 342 | the section describes the names of the maps on the undirected egdes and all |
---|
| 343 | next lines describe one undirected edge with the the incident nodes and the |
---|
| 344 | values of the map. |
---|
[1532] | 345 | |
---|
[2502] | 346 | The format could store directed edge maps, if there are two maps with |
---|
| 347 | names being the same with a \c '+' and a \c '-' prefix then this could |
---|
| 348 | be read as such a map. |
---|
[1532] | 349 | |
---|
| 350 | \code |
---|
[1909] | 351 | @uedgeset |
---|
[1901] | 352 | label capacity +flow -flow |
---|
| 353 | 32 2 1 4.3 2.0 0.0 |
---|
| 354 | 21 21 5 2.6 0.0 2.6 |
---|
| 355 | 21 12 8 3.4 0.0 0.0 |
---|
[1532] | 356 | \endcode |
---|
| 357 | |
---|
[1909] | 358 | The \c edges section is changed to \c uedges section. This section |
---|
[1532] | 359 | describes labeled edges and undirected edges. The directed edge label |
---|
[1540] | 360 | should start with a \c '+' or a \c '-' prefix to decide the direction |
---|
[1532] | 361 | of the edge. |
---|
| 362 | |
---|
| 363 | \code |
---|
[1909] | 364 | @uedges |
---|
| 365 | uedge 1 |
---|
[1532] | 366 | +edge 5 |
---|
| 367 | -back 5 |
---|
| 368 | \endcode |
---|
| 369 | |
---|
[1631] | 370 | There are similar classes to the \ref lemon::GraphReader "GraphReader" and |
---|
| 371 | \ref lemon::GraphWriter "GraphWriter" which |
---|
| 372 | handle the undirected graphs. These classes are |
---|
[1909] | 373 | the \ref lemon::UGraphReader "UGraphReader" |
---|
| 374 | and \ref lemon::UGraphWriter "UGraphWriter". |
---|
[1532] | 375 | |
---|
[1909] | 376 | The \ref lemon::UGraphReader::readUEdgeMap() "readUEdgeMap()" |
---|
[1631] | 377 | function reads an undirected map and the |
---|
[1909] | 378 | \ref lemon::UGraphReader::readUEdge() "readUEdge()" |
---|
[1631] | 379 | reads an undirected edge from the file, |
---|
[1532] | 380 | |
---|
| 381 | \code |
---|
[1909] | 382 | reader.readUEdgeMap("capacity", capacityMap); |
---|
[1532] | 383 | reader.readEdgeMap("flow", flowMap); |
---|
| 384 | ... |
---|
[1909] | 385 | reader.readUEdge("u_edge", u_edge); |
---|
[1532] | 386 | reader.readEdge("edge", edge); |
---|
| 387 | \endcode |
---|
| 388 | |
---|
[2502] | 389 | The undirected bipartite graphs could be read with the \c BpUGraph |
---|
| 390 | class and it has specialized nodeset section, which should be start |
---|
| 391 | with \c "@bpnodeset". This section is separated to two |
---|
| 392 | subsections. The header line of these sections start with "&anodeset" |
---|
| 393 | or "&bnodeset" and after that the line contains the names of the |
---|
| 394 | regular and A-node or B-node maps accordingly. The lines of each |
---|
| 395 | section contains the mapped values. The labels of the graph should be |
---|
| 396 | unique overall both subsections. |
---|
| 397 | |
---|
| 398 | \code |
---|
| 399 | @bpnodeset |
---|
| 400 | &anodeset label coords radius |
---|
| 401 | 0 (0, 0) 14.0 |
---|
| 402 | 1 (0, 1) 12.0 |
---|
| 403 | &bnodeset label coords |
---|
| 404 | 2 (1, 0) |
---|
| 405 | 3 (1, 1) |
---|
| 406 | \endcode |
---|
| 407 | |
---|
| 408 | The reading can be done with \ref lemon::BpUGraphReader::readANodeMap() |
---|
| 409 | "readANodeMap()", \ref lemon::BpUGraphReader::readBNodeMap() |
---|
| 410 | "readBNodeMap()" or \ref lemon::BpUGraphReader::readNodeMap() |
---|
| 411 | "readNodeMap()" members. |
---|
| 412 | |
---|
| 413 | \code |
---|
| 414 | reader.readNodeMap("coords", coords); |
---|
| 415 | reader.readAnodeMap("radius", radius); |
---|
| 416 | \endcode |
---|
| 417 | |
---|
[1532] | 418 | \section advanced Advanced features |
---|
| 419 | |
---|
[1540] | 420 | The graph reader and writer classes give an easy way to read and write |
---|
| 421 | graphs. But sometimes we want more advanced features. In this case we can |
---|
| 422 | use the more general <tt>lemon reader and writer</tt> interface. |
---|
[1532] | 423 | |
---|
[1540] | 424 | The LEMON file format is a section oriented file format. It contains one or |
---|
| 425 | more sections, each starting with a line identifying its type |
---|
| 426 | (the word starting with the \c \@ character). |
---|
[1532] | 427 | The content of the section this way cannot contain line with \c \@ first |
---|
| 428 | character. The file may contains comment lines with \c # first character. |
---|
| 429 | |
---|
[1631] | 430 | The \ref lemon::LemonReader "LemonReader" |
---|
| 431 | and \ref lemon::LemonWriter "LemonWriter" |
---|
| 432 | gives a framework to read and |
---|
[1532] | 433 | write sections. There are various section reader and section writer |
---|
[1631] | 434 | classes which can be attached to a \ref lemon::LemonReader "LemonReader" |
---|
| 435 | or a \ref lemon::LemonWriter "LemonWriter". |
---|
[1532] | 436 | |
---|
| 437 | There are default section readers and writers for reading and writing |
---|
[1540] | 438 | item sets, and labeled items in the graph. These read and write |
---|
[1532] | 439 | the format described above. Other type of data can be handled with own |
---|
| 440 | section reader and writer classes which are inherited from the |
---|
[1631] | 441 | \c LemonReader::SectionReader or the |
---|
| 442 | \ref lemon::LemonWriter::SectionWriter "LemonWriter::SectionWriter" |
---|
| 443 | classes. |
---|
[1532] | 444 | |
---|
| 445 | The next example defines a special section reader which reads the |
---|
| 446 | \c \@description sections into a string: |
---|
| 447 | |
---|
| 448 | \code |
---|
| 449 | class DescriptionReader : LemonReader::SectionReader { |
---|
| 450 | protected: |
---|
| 451 | virtual bool header(const std::string& line) { |
---|
| 452 | std::istringstream ls(line); |
---|
| 453 | std::string head; |
---|
| 454 | ls >> head; |
---|
| 455 | return head == "@description"; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | virtual void read(std::istream& is) { |
---|
| 459 | std::string line; |
---|
| 460 | while (getline(is, line)) { |
---|
| 461 | desc += line; |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | public: |
---|
| 465 | |
---|
| 466 | typedef LemonReader::SectionReader Parent; |
---|
| 467 | |
---|
| 468 | DescriptionReader(LemonReader& reader) : Parent(reader) {} |
---|
| 469 | |
---|
| 470 | const std::string& description() const { |
---|
| 471 | return description; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | private: |
---|
| 475 | std::string desc; |
---|
| 476 | }; |
---|
| 477 | \endcode |
---|
| 478 | |
---|
| 479 | The other advanced stuff of the generalized file format is that |
---|
| 480 | multiple edgesets can be stored to the same nodeset. It can be used |
---|
[1540] | 481 | for example as a network traffic matrix. |
---|
[1532] | 482 | |
---|
[1540] | 483 | In our example there is a network with symmetric links and there are assymetric |
---|
[1532] | 484 | traffic request on the network. This construction can be stored in an |
---|
[1842] | 485 | undirected graph and in a directed \c ListEdgeSet class. The example |
---|
[1631] | 486 | shows the input with the \ref lemon::LemonReader "LemonReader" class: |
---|
[1532] | 487 | |
---|
| 488 | \code |
---|
[1909] | 489 | ListUGraph network; |
---|
| 490 | ListUGraph::UEdgeMap<double> capacity; |
---|
| 491 | ListEdgeSet<ListUGraph> traffic(network); |
---|
| 492 | ListEdgeSet<ListUGraph>::EdgeMap<double> request(network); |
---|
[1532] | 493 | |
---|
| 494 | LemonReader reader(std::cin); |
---|
[1909] | 495 | NodeSetReader<ListUGraph> nodesetReader(reader, network); |
---|
| 496 | UEdgeSetReader<ListUGraph> |
---|
| 497 | uEdgesetReader(reader, network, nodesetReader); |
---|
| 498 | uEdgesetReader.readEdgeMap("capacity", capacity); |
---|
| 499 | EdgeSetReader<ListEdgeSet<ListUGraph> > |
---|
[1848] | 500 | edgesetReader(reader, traffic, nodesetReader, "traffic"); |
---|
[1532] | 501 | edgesetReader.readEdgeMap("request", request); |
---|
| 502 | |
---|
| 503 | reader.run(); |
---|
| 504 | \endcode |
---|
| 505 | |
---|
[1631] | 506 | Because both the \ref lemon::GraphReader "GraphReader" |
---|
[1909] | 507 | and the \ref lemon::UGraphReader "UGraphReader" can be converted |
---|
[1631] | 508 | to \ref lemon::LemonReader "LemonReader" |
---|
[1901] | 509 | and it can resolve the label's of the items, the previous |
---|
[1909] | 510 | result can be achived with the \ref lemon::UGraphReader "UGraphReader" |
---|
[1631] | 511 | class, too. |
---|
[1532] | 512 | |
---|
| 513 | |
---|
| 514 | \code |
---|
[1909] | 515 | ListUGraph network; |
---|
| 516 | ListUGraph::UEdgeSet<double> capacity; |
---|
| 517 | ListEdgeSet<ListUGraph> traffic(network); |
---|
| 518 | ListEdgeSet<ListUGraph>::EdgeMap<double> request(network); |
---|
[1532] | 519 | |
---|
[1909] | 520 | UGraphReader<ListUGraph> reader(std::cin, network); |
---|
[1532] | 521 | reader.readEdgeMap("capacity", capacity); |
---|
[1909] | 522 | EdgeSetReader<ListEdgeSet<ListUGraph> > |
---|
[1848] | 523 | edgesetReader(reader, traffic, reader, "traffic"); |
---|
[1532] | 524 | edgesetReader.readEdgeMap("request", request); |
---|
| 525 | |
---|
| 526 | reader.run(); |
---|
| 527 | \endcode |
---|
| 528 | |
---|
[1333] | 529 | \author Balazs Dezso |
---|
[1114] | 530 | */ |
---|
[2391] | 531 | } |
---|