[1137] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1137] | 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 | |
---|
[1287] | 19 | ///\ingroup io_group |
---|
[1137] | 20 | ///\file |
---|
[1287] | 21 | ///\brief Lemon Graph Format reader. |
---|
[1137] | 22 | |
---|
[1214] | 23 | #ifndef LEMON_GRAPH_READER_H |
---|
| 24 | #define LEMON_GRAPH_READER_H |
---|
| 25 | |
---|
[1137] | 26 | #include <iostream> |
---|
| 27 | |
---|
| 28 | #include <lemon/error.h> |
---|
[1408] | 29 | #include <lemon/lemon_reader.h> |
---|
[1137] | 30 | |
---|
| 31 | namespace lemon { |
---|
| 32 | |
---|
[1333] | 33 | /// \addtogroup io_group |
---|
| 34 | /// @{ |
---|
[1137] | 35 | |
---|
| 36 | /// \brief The graph reader class. |
---|
| 37 | /// |
---|
[1534] | 38 | /// The \c GraphReader class provides the graph input. |
---|
| 39 | /// Before you read this documentation it might be useful to read the general |
---|
| 40 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
[1540] | 41 | /// |
---|
[1534] | 42 | /// If you don't need very sophisticated |
---|
| 43 | /// behaviour then you can use the versions of the public function |
---|
| 44 | /// \ref readGraph() to read a graph (or a max flow instance etc). |
---|
| 45 | /// |
---|
[1540] | 46 | /// The file to be read may contain several maps and labeled nodes or |
---|
[1333] | 47 | /// edges. |
---|
| 48 | /// |
---|
| 49 | /// If you read a graph you need not read all the maps and items just those |
---|
| 50 | /// that you need. The interface of the \c GraphReader is very similar to |
---|
| 51 | /// the GraphWriter but the reading method does not depend on the order the |
---|
[1540] | 52 | /// given commands (i.e. you don't have to insist on the order in which the |
---|
| 53 | /// maps are given in the file). |
---|
[1333] | 54 | /// |
---|
[1540] | 55 | /// The reader object assumes that not readed values do not contain |
---|
[1333] | 56 | /// whitespaces, therefore it has some extra possibilities to control how |
---|
| 57 | /// it should skip the values when the string representation contains spaces. |
---|
| 58 | /// |
---|
[1946] | 59 | ///\code |
---|
[1333] | 60 | /// GraphReader<ListGraph> reader(std::cin, graph); |
---|
[1946] | 61 | ///\endcode |
---|
[1333] | 62 | /// |
---|
[1394] | 63 | /// The \c readNodeMap() function reads a map from the \c \@nodeset section. |
---|
[1333] | 64 | /// If there is a map that you do not want to read from the file and there is |
---|
| 65 | /// whitespace in the string represenation of the values then you should |
---|
| 66 | /// call the \c skipNodeMap() template member function with proper |
---|
| 67 | /// parameters. |
---|
| 68 | /// |
---|
[1946] | 69 | ///\code |
---|
[1421] | 70 | /// reader.readNodeMap("coords", coords); |
---|
[1333] | 71 | /// |
---|
[1901] | 72 | /// reader.skipNodeMap("description", desc); |
---|
[1333] | 73 | /// |
---|
[1394] | 74 | /// reader.readNodeMap("color", colorMap); |
---|
[1946] | 75 | ///\endcode |
---|
[1333] | 76 | /// |
---|
[1394] | 77 | /// With the \c readEdgeMap() member function you can give an edge map |
---|
[1333] | 78 | /// reading command similar to the NodeMaps. |
---|
| 79 | /// |
---|
[1946] | 80 | ///\code |
---|
[1394] | 81 | /// reader.readEdgeMap("weight", weightMap); |
---|
| 82 | /// reader.readEdgeMap("label", labelMap); |
---|
[1946] | 83 | ///\endcode |
---|
[1333] | 84 | /// |
---|
[1408] | 85 | /// With \c readNode() and \c readEdge() functions you can read |
---|
| 86 | /// labeled Nodes and Edges. |
---|
[1333] | 87 | /// |
---|
[1946] | 88 | ///\code |
---|
[1394] | 89 | /// reader.readNode("source", sourceNode); |
---|
| 90 | /// reader.readNode("target", targetNode); |
---|
[1333] | 91 | /// |
---|
[1394] | 92 | /// reader.readEdge("observed", edge); |
---|
[1946] | 93 | ///\endcode |
---|
[1333] | 94 | /// |
---|
[1408] | 95 | /// With the \c readAttribute() functions you can read an attribute |
---|
[1540] | 96 | /// into a variable. You can specify the reader for the attribute as |
---|
[1408] | 97 | /// the nodemaps. |
---|
| 98 | /// |
---|
[1333] | 99 | /// After you give all read commands you must call the \c run() member |
---|
[1540] | 100 | /// function, which executes all the commands. |
---|
[1333] | 101 | /// |
---|
[1946] | 102 | ///\code |
---|
[1333] | 103 | /// reader.run(); |
---|
[1946] | 104 | ///\endcode |
---|
[1333] | 105 | /// |
---|
[1287] | 106 | /// \see DefaultReaderTraits |
---|
| 107 | /// \see QuotedStringReader |
---|
[1138] | 108 | /// \see \ref GraphWriter |
---|
| 109 | /// \see \ref graph-io-page |
---|
[1333] | 110 | /// \author Balazs Dezso |
---|
[1137] | 111 | template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> |
---|
| 112 | class GraphReader { |
---|
| 113 | public: |
---|
| 114 | |
---|
| 115 | typedef _Graph Graph; |
---|
| 116 | typedef typename Graph::Node Node; |
---|
| 117 | typedef typename Graph::Edge Edge; |
---|
| 118 | |
---|
| 119 | typedef _ReaderTraits ReaderTraits; |
---|
[1408] | 120 | typedef typename ReaderTraits::Skipper DefaultSkipper; |
---|
[1137] | 121 | |
---|
| 122 | /// \brief Construct a new GraphReader. |
---|
| 123 | /// |
---|
[1208] | 124 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 125 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 126 | GraphReader(std::istream& _is, Graph& _graph, |
---|
[1408] | 127 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
[1421] | 128 | : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper), |
---|
| 129 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 130 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 131 | std::string(), skipper), |
---|
[1408] | 132 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 133 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 134 | attribute_reader(*reader, std::string()) {} |
---|
| 135 | |
---|
| 136 | /// \brief Construct a new GraphReader. |
---|
| 137 | /// |
---|
| 138 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 139 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 140 | GraphReader(const std::string& _filename, Graph& _graph, |
---|
[1408] | 141 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 142 | : reader(new LemonReader(_filename)), own_reader(true), |
---|
[1421] | 143 | skipper(_skipper), |
---|
| 144 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 145 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 146 | std::string(), skipper), |
---|
[1408] | 147 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 148 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 149 | attribute_reader(*reader, std::string()) {} |
---|
| 150 | |
---|
| 151 | /// \brief Construct a new GraphReader. |
---|
| 152 | /// |
---|
| 153 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 154 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 155 | GraphReader(LemonReader& _reader, Graph& _graph, |
---|
[1408] | 156 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
[1421] | 157 | : reader(_reader), own_reader(false), skipper(_skipper), |
---|
| 158 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 159 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 160 | std::string(), skipper), |
---|
[1408] | 161 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 162 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 163 | attribute_reader(*reader, std::string()) {} |
---|
[1137] | 164 | |
---|
| 165 | /// \brief Destruct the graph reader. |
---|
| 166 | /// |
---|
| 167 | /// Destruct the graph reader. |
---|
| 168 | ~GraphReader() { |
---|
[1408] | 169 | if (own_reader) |
---|
| 170 | delete reader; |
---|
[1137] | 171 | } |
---|
| 172 | |
---|
[1540] | 173 | /// \brief Give a new node map reading command to the reader. |
---|
[1137] | 174 | /// |
---|
[1540] | 175 | /// Give a new node map reading command to the reader. |
---|
[1137] | 176 | template <typename Map> |
---|
[1394] | 177 | GraphReader& readNodeMap(std::string name, Map& map) { |
---|
[1421] | 178 | nodeset_reader.readNodeMap(name, map); |
---|
| 179 | return *this; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | template <typename Map> |
---|
| 183 | GraphReader& readNodeMap(std::string name, const Map& map) { |
---|
| 184 | nodeset_reader.readNodeMap(name, map); |
---|
[1408] | 185 | return *this; |
---|
[1137] | 186 | } |
---|
| 187 | |
---|
[1540] | 188 | /// \brief Give a new node map reading command to the reader. |
---|
[1137] | 189 | /// |
---|
[1540] | 190 | /// Give a new node map reading command to the reader. |
---|
[1137] | 191 | template <typename Reader, typename Map> |
---|
[1394] | 192 | GraphReader& readNodeMap(std::string name, Map& map, |
---|
[1137] | 193 | const Reader& reader = Reader()) { |
---|
[1421] | 194 | nodeset_reader.readNodeMap(name, map, reader); |
---|
| 195 | return *this; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | template <typename Reader, typename Map> |
---|
| 199 | GraphReader& readNodeMap(std::string name, const Map& map, |
---|
| 200 | const Reader& reader = Reader()) { |
---|
| 201 | nodeset_reader.readNodeMap(name, map, reader); |
---|
[1137] | 202 | return *this; |
---|
| 203 | } |
---|
| 204 | |
---|
[1540] | 205 | /// \brief Give a new node map skipping command to the reader. |
---|
[1137] | 206 | /// |
---|
[1540] | 207 | /// Give a new node map skipping command to the reader. |
---|
[1137] | 208 | template <typename Reader> |
---|
| 209 | GraphReader& skipNodeMap(std::string name, |
---|
| 210 | const Reader& reader = Reader()) { |
---|
[1421] | 211 | nodeset_reader.skipNodeMap(name, reader); |
---|
[1137] | 212 | return *this; |
---|
| 213 | } |
---|
| 214 | |
---|
[1540] | 215 | /// \brief Give a new edge map reading command to the reader. |
---|
[1137] | 216 | /// |
---|
[1540] | 217 | /// Give a new edge map reading command to the reader. |
---|
[1137] | 218 | template <typename Map> |
---|
[1394] | 219 | GraphReader& readEdgeMap(std::string name, Map& map) { |
---|
[1421] | 220 | edgeset_reader.readEdgeMap(name, map); |
---|
| 221 | return *this; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | template <typename Map> |
---|
| 225 | GraphReader& readEdgeMap(std::string name, const Map& map) { |
---|
| 226 | edgeset_reader.readEdgeMap(name, map); |
---|
[1408] | 227 | return *this; |
---|
[1137] | 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
[1540] | 231 | /// \brief Give a new edge map reading command to the reader. |
---|
[1137] | 232 | /// |
---|
[1540] | 233 | /// Give a new edge map reading command to the reader. |
---|
[1137] | 234 | template <typename Reader, typename Map> |
---|
[1394] | 235 | GraphReader& readEdgeMap(std::string name, Map& map, |
---|
[1137] | 236 | const Reader& reader = Reader()) { |
---|
[1421] | 237 | edgeset_reader.readEdgeMap(name, map, reader); |
---|
| 238 | return *this; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | template <typename Reader, typename Map> |
---|
| 242 | GraphReader& readEdgeMap(std::string name, const Map& map, |
---|
| 243 | const Reader& reader = Reader()) { |
---|
| 244 | edgeset_reader.readEdgeMap(name, map, reader); |
---|
[1137] | 245 | return *this; |
---|
| 246 | } |
---|
| 247 | |
---|
[1540] | 248 | /// \brief Give a new edge map skipping command to the reader. |
---|
[1137] | 249 | /// |
---|
[1540] | 250 | /// Give a new edge map skipping command to the reader. |
---|
[1137] | 251 | template <typename Reader> |
---|
[1421] | 252 | GraphReader& skipEdgeMap(std::string name, |
---|
[1137] | 253 | const Reader& reader = Reader()) { |
---|
[1421] | 254 | edgeset_reader.skipEdgeMap(name, reader); |
---|
[1137] | 255 | return *this; |
---|
| 256 | } |
---|
| 257 | |
---|
[1540] | 258 | /// \brief Give a new labeled node reading command to the reader. |
---|
[1137] | 259 | /// |
---|
[1540] | 260 | /// Give a new labeled node reading command to the reader. |
---|
[1394] | 261 | GraphReader& readNode(std::string name, Node& node) { |
---|
[1408] | 262 | node_reader.readNode(name, node); |
---|
[1137] | 263 | return *this; |
---|
| 264 | } |
---|
| 265 | |
---|
[1540] | 266 | /// \brief Give a new labeled edge reading command to the reader. |
---|
[1137] | 267 | /// |
---|
[1540] | 268 | /// Give a new labeled edge reading command to the reader. |
---|
[1394] | 269 | GraphReader& readEdge(std::string name, Edge& edge) { |
---|
[1408] | 270 | edge_reader.readEdge(name, edge); |
---|
[1476] | 271 | return *this; |
---|
[1408] | 272 | } |
---|
| 273 | |
---|
[1540] | 274 | /// \brief Give a new attribute reading command. |
---|
[1408] | 275 | /// |
---|
[1540] | 276 | /// Give a new attribute reading command. |
---|
[1408] | 277 | template <typename Value> |
---|
| 278 | GraphReader& readAttribute(std::string name, Value& value) { |
---|
| 279 | attribute_reader.readAttribute(name, value); |
---|
[1137] | 280 | return *this; |
---|
| 281 | } |
---|
[1408] | 282 | |
---|
[1540] | 283 | /// \brief Give a new attribute reading command. |
---|
[1408] | 284 | /// |
---|
[1540] | 285 | /// Give a new attribute reading command. |
---|
[1408] | 286 | template <typename Reader, typename Value> |
---|
| 287 | GraphReader& readAttribute(std::string name, Value& value, |
---|
| 288 | const Reader& reader) { |
---|
| 289 | attribute_reader.readAttribute<Reader>(name, value, reader); |
---|
| 290 | return *this; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /// \brief Conversion operator to LemonReader. |
---|
| 294 | /// |
---|
[1540] | 295 | /// Conversion operator to LemonReader. It makes possible to access the |
---|
| 296 | /// encapsulated \e LemonReader, this way you can attach to this reader |
---|
| 297 | /// new instances of \e LemonReader::SectionReader. For more details see |
---|
| 298 | /// the \ref rwbackground "Background of Reading and Writing". |
---|
[1408] | 299 | operator LemonReader&() { |
---|
| 300 | return *reader; |
---|
| 301 | } |
---|
[1137] | 302 | |
---|
[1540] | 303 | /// \brief Executes the reading commands. |
---|
[1137] | 304 | /// |
---|
[1540] | 305 | /// Executes the reading commands. |
---|
[1137] | 306 | void run() { |
---|
[1408] | 307 | reader->run(); |
---|
[1396] | 308 | } |
---|
| 309 | |
---|
[1901] | 310 | |
---|
| 311 | /// \brief Returns true if the reader can give back the items by its label. |
---|
[1429] | 312 | /// |
---|
[1901] | 313 | /// \brief Returns true if the reader can give back the items by its label. |
---|
| 314 | bool isLabelReader() const { |
---|
| 315 | return nodeset_reader.isLabelReader() && edgeset_reader.isLabelReader(); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /// \brief Gives back the node by its label. |
---|
| 319 | /// |
---|
| 320 | /// It reads an label from the stream and gives back which node belongs to |
---|
[1935] | 321 | /// it. It is possible only if there was read a "label" named node map. |
---|
[1901] | 322 | void readLabel(std::istream& is, Node& node) const { |
---|
| 323 | nodeset_reader.readLabel(is, node); |
---|
[1429] | 324 | } |
---|
| 325 | |
---|
[1901] | 326 | /// \brief Gives back the edge by its label. |
---|
[1429] | 327 | /// |
---|
[1901] | 328 | /// It reads an label from the stream and gives back which edge belongs to |
---|
[1935] | 329 | /// it. It is possible only if there was read a "label" named edge map. |
---|
[1901] | 330 | void readLabel(std::istream& is, Edge& edge) const { |
---|
| 331 | return edgeset_reader.readLabel(is, edge); |
---|
[1429] | 332 | } |
---|
| 333 | |
---|
[1137] | 334 | private: |
---|
| 335 | |
---|
[1408] | 336 | LemonReader* reader; |
---|
| 337 | bool own_reader; |
---|
[1137] | 338 | |
---|
[1408] | 339 | DefaultSkipper skipper; |
---|
[1137] | 340 | |
---|
[1408] | 341 | NodeSetReader<Graph, ReaderTraits> nodeset_reader; |
---|
| 342 | EdgeSetReader<Graph, ReaderTraits> edgeset_reader; |
---|
| 343 | |
---|
| 344 | NodeReader<Graph> node_reader; |
---|
| 345 | EdgeReader<Graph> edge_reader; |
---|
| 346 | |
---|
| 347 | AttributeReader<ReaderTraits> attribute_reader; |
---|
[1137] | 348 | }; |
---|
| 349 | |
---|
[1534] | 350 | |
---|
[1744] | 351 | /// \brief Read a graph from the input. |
---|
[1333] | 352 | /// |
---|
[1744] | 353 | /// It is a helper function to read a graph from the given input |
---|
| 354 | /// stream. It gives back an GraphReader object and this object |
---|
| 355 | /// can read more maps, labeled nodes, edges and attributes. |
---|
[1534] | 356 | /// |
---|
[1744] | 357 | /// \warning Do not forget to call the \c run() function. |
---|
| 358 | /// |
---|
[1534] | 359 | /// \param is The input stream. |
---|
| 360 | /// \param g The graph. |
---|
| 361 | template<typename Graph> |
---|
[1744] | 362 | GraphReader<Graph> graphReader(std::istream& is, Graph &g) { |
---|
| 363 | return GraphReader<Graph>(is, g); |
---|
[1534] | 364 | } |
---|
| 365 | |
---|
[1744] | 366 | /// \brief Read a graph from the input. |
---|
| 367 | /// |
---|
| 368 | /// It is a helper function to read a graph from the given input |
---|
| 369 | /// file. It gives back an GraphReader object and this object |
---|
| 370 | /// can read more maps, labeled nodes, edges and attributes. |
---|
| 371 | /// |
---|
| 372 | /// \warning Do not forget to call the \c run() function. |
---|
| 373 | /// |
---|
| 374 | /// \param fn The input filename. |
---|
[1534] | 375 | /// \param g The graph. |
---|
[1744] | 376 | template<typename Graph> |
---|
| 377 | GraphReader<Graph> graphReader(const std::string& fn, Graph &g) { |
---|
| 378 | return GraphReader<Graph>(fn, g); |
---|
[1534] | 379 | } |
---|
| 380 | |
---|
[1744] | 381 | /// \brief The undirected graph reader class. |
---|
[1421] | 382 | /// |
---|
[1909] | 383 | /// The \c UGraphReader class provides the graph input. |
---|
[1540] | 384 | /// Before you read this documentation it might be useful to read the general |
---|
| 385 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
| 386 | /// |
---|
| 387 | /// If you don't need very sophisticated |
---|
| 388 | /// behaviour then you can use the versions of the public function |
---|
| 389 | /// \ref readGraph() to read a graph (or a max flow instance etc). |
---|
| 390 | /// |
---|
[1421] | 391 | /// The given file format may contain several maps and labeled nodes or |
---|
| 392 | /// edges. |
---|
| 393 | /// |
---|
| 394 | /// If you read a graph you need not read all the maps and items just those |
---|
[1909] | 395 | /// that you need. The interface of the \c UGraphReader is very similar |
---|
| 396 | /// to the UGraphWriter but the reading method does not depend on the |
---|
[1540] | 397 | /// order of the given commands. |
---|
[1421] | 398 | /// |
---|
| 399 | /// The reader object suppose that each not readed value does not contain |
---|
| 400 | /// whitespaces, therefore it has some extra possibilities to control how |
---|
| 401 | /// it should skip the values when the string representation contains spaces. |
---|
| 402 | /// |
---|
[1946] | 403 | ///\code |
---|
[1909] | 404 | /// UGraphReader<ListUGraph> reader(std::cin, graph); |
---|
[1946] | 405 | ///\endcode |
---|
[1421] | 406 | /// |
---|
| 407 | /// The \c readNodeMap() function reads a map from the \c \@nodeset section. |
---|
| 408 | /// If there is a map that you do not want to read from the file and there is |
---|
| 409 | /// whitespace in the string represenation of the values then you should |
---|
| 410 | /// call the \c skipNodeMap() template member function with proper |
---|
| 411 | /// parameters. |
---|
| 412 | /// |
---|
[1946] | 413 | ///\code |
---|
[1421] | 414 | /// reader.readNodeMap("coords", coords); |
---|
| 415 | /// |
---|
[1901] | 416 | /// reader.skipNodeMap("description", desc); |
---|
[1421] | 417 | /// |
---|
| 418 | /// reader.readNodeMap("color", colorMap); |
---|
[1946] | 419 | ///\endcode |
---|
[1421] | 420 | /// |
---|
[1909] | 421 | /// With the \c readUEdgeMap() member function you can give an |
---|
| 422 | /// uedge map reading command similar to the NodeMaps. |
---|
[1421] | 423 | /// |
---|
[1946] | 424 | ///\code |
---|
[1909] | 425 | /// reader.readUEdgeMap("capacity", capacityMap); |
---|
[1946] | 426 | ///\endcode |
---|
[1421] | 427 | /// |
---|
| 428 | /// The reading of the directed edge maps is just a syntactical sugar. |
---|
| 429 | /// It reads two undirected edgemaps into a directed edge map. The |
---|
| 430 | /// undirected edge maps' name should be start with the \c '+' and the |
---|
| 431 | /// \c '-' character and the same. |
---|
| 432 | /// |
---|
[1946] | 433 | ///\code |
---|
[1421] | 434 | /// reader.readEdgeMap("flow", flowMap); |
---|
[1946] | 435 | ///\endcode |
---|
[1421] | 436 | /// |
---|
[1909] | 437 | /// With \c readNode() and \c readUEdge() functions you can read |
---|
| 438 | /// labeled Nodes and UEdges. |
---|
[1421] | 439 | /// |
---|
[1946] | 440 | ///\code |
---|
[1421] | 441 | /// reader.readNode("source", sourceNode); |
---|
| 442 | /// reader.readNode("target", targetNode); |
---|
| 443 | /// |
---|
[1909] | 444 | /// reader.readUEdge("observed", uEdge); |
---|
[1946] | 445 | ///\endcode |
---|
[1421] | 446 | /// |
---|
| 447 | /// With the \c readAttribute() functions you can read an attribute |
---|
| 448 | /// in a variable. You can specify the reader for the attribute as |
---|
| 449 | /// the nodemaps. |
---|
| 450 | /// |
---|
| 451 | /// After you give all read commands you must call the \c run() member |
---|
| 452 | /// function, which execute all the commands. |
---|
| 453 | /// |
---|
[1946] | 454 | ///\code |
---|
[1421] | 455 | /// reader.run(); |
---|
[1946] | 456 | ///\endcode |
---|
[1421] | 457 | /// |
---|
| 458 | /// \see GraphReader |
---|
| 459 | /// \see DefaultReaderTraits |
---|
[1909] | 460 | /// \see \ref UGraphWriter |
---|
[1421] | 461 | /// \see \ref graph-io-page |
---|
| 462 | /// |
---|
| 463 | /// \author Balazs Dezso |
---|
| 464 | template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> |
---|
[1909] | 465 | class UGraphReader { |
---|
[1421] | 466 | public: |
---|
| 467 | |
---|
| 468 | typedef _Graph Graph; |
---|
| 469 | typedef typename Graph::Node Node; |
---|
| 470 | typedef typename Graph::Edge Edge; |
---|
[1909] | 471 | typedef typename Graph::UEdge UEdge; |
---|
[1421] | 472 | |
---|
| 473 | typedef _ReaderTraits ReaderTraits; |
---|
| 474 | typedef typename ReaderTraits::Skipper DefaultSkipper; |
---|
| 475 | |
---|
[1909] | 476 | /// \brief Construct a new UGraphReader. |
---|
[1421] | 477 | /// |
---|
[1909] | 478 | /// Construct a new UGraphReader. It reads into the given graph |
---|
[1421] | 479 | /// and it use the given reader as the default skipper. |
---|
[1909] | 480 | UGraphReader(std::istream& _is, Graph& _graph, |
---|
[1421] | 481 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 482 | : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper), |
---|
| 483 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
[1909] | 484 | u_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
[1421] | 485 | std::string(), skipper), |
---|
| 486 | node_reader(*reader, nodeset_reader, std::string()), |
---|
[1909] | 487 | u_edge_reader(*reader, u_edgeset_reader, std::string()), |
---|
[1421] | 488 | attribute_reader(*reader, std::string()) {} |
---|
| 489 | |
---|
[1909] | 490 | /// \brief Construct a new UGraphReader. |
---|
[1421] | 491 | /// |
---|
[1909] | 492 | /// Construct a new UGraphReader. It reads into the given graph |
---|
[1421] | 493 | /// and it use the given reader as the default skipper. |
---|
[1909] | 494 | UGraphReader(const std::string& _filename, Graph& _graph, |
---|
[1421] | 495 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 496 | : reader(new LemonReader(_filename)), own_reader(true), |
---|
| 497 | skipper(_skipper), |
---|
| 498 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
[1909] | 499 | u_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
[1421] | 500 | std::string(), skipper), |
---|
| 501 | node_reader(*reader, nodeset_reader, std::string()), |
---|
[1909] | 502 | u_edge_reader(*reader, u_edgeset_reader, std::string()), |
---|
[1421] | 503 | attribute_reader(*reader, std::string()) {} |
---|
| 504 | |
---|
[1909] | 505 | /// \brief Construct a new UGraphReader. |
---|
[1421] | 506 | /// |
---|
[1909] | 507 | /// Construct a new UGraphReader. It reads into the given graph |
---|
[1421] | 508 | /// and it use the given reader as the default skipper. |
---|
[1909] | 509 | UGraphReader(LemonReader& _reader, Graph& _graph, |
---|
[1421] | 510 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 511 | : reader(_reader), own_reader(false), skipper(_skipper), |
---|
| 512 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
[1909] | 513 | u_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
[1421] | 514 | std::string(), skipper), |
---|
| 515 | node_reader(*reader, nodeset_reader, std::string()), |
---|
[1909] | 516 | u_edge_reader(*reader, u_edgeset_reader, std::string()), |
---|
[1421] | 517 | attribute_reader(*reader, std::string()) {} |
---|
| 518 | |
---|
| 519 | /// \brief Destruct the graph reader. |
---|
| 520 | /// |
---|
| 521 | /// Destruct the graph reader. |
---|
[1909] | 522 | ~UGraphReader() { |
---|
[1421] | 523 | if (own_reader) |
---|
| 524 | delete reader; |
---|
| 525 | } |
---|
| 526 | |
---|
[1540] | 527 | /// \brief Give a new node map reading command to the reader. |
---|
[1421] | 528 | /// |
---|
[1540] | 529 | /// Give a new node map reading command to the reader. |
---|
[1421] | 530 | template <typename Map> |
---|
[1909] | 531 | UGraphReader& readNodeMap(std::string name, Map& map) { |
---|
[1421] | 532 | nodeset_reader.readNodeMap(name, map); |
---|
| 533 | return *this; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | template <typename Map> |
---|
[1909] | 537 | UGraphReader& readNodeMap(std::string name, const Map& map) { |
---|
[1421] | 538 | nodeset_reader.readNodeMap(name, map); |
---|
| 539 | return *this; |
---|
| 540 | } |
---|
| 541 | |
---|
[1540] | 542 | /// \brief Give a new node map reading command to the reader. |
---|
[1421] | 543 | /// |
---|
[1540] | 544 | /// Give a new node map reading command to the reader. |
---|
[1421] | 545 | template <typename Reader, typename Map> |
---|
[1909] | 546 | UGraphReader& readNodeMap(std::string name, Map& map, |
---|
[1421] | 547 | const Reader& reader = Reader()) { |
---|
| 548 | nodeset_reader.readNodeMap(name, map, reader); |
---|
| 549 | return *this; |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | template <typename Reader, typename Map> |
---|
[1909] | 553 | UGraphReader& readNodeMap(std::string name, const Map& map, |
---|
[1421] | 554 | const Reader& reader = Reader()) { |
---|
| 555 | nodeset_reader.readNodeMap(name, map, reader); |
---|
| 556 | return *this; |
---|
| 557 | } |
---|
| 558 | |
---|
[1540] | 559 | /// \brief Give a new node map skipping command to the reader. |
---|
[1421] | 560 | /// |
---|
[1540] | 561 | /// Give a new node map skipping command to the reader. |
---|
[1421] | 562 | template <typename Reader> |
---|
[1909] | 563 | UGraphReader& skipNodeMap(std::string name, |
---|
[1421] | 564 | const Reader& reader = Reader()) { |
---|
| 565 | nodeset_reader.skipNodeMap(name, reader); |
---|
| 566 | return *this; |
---|
| 567 | } |
---|
| 568 | |
---|
[1540] | 569 | /// \brief Give a new undirected edge map reading command to the reader. |
---|
[1421] | 570 | /// |
---|
[1540] | 571 | /// Give a new undirected edge map reading command to the reader. |
---|
[1421] | 572 | template <typename Map> |
---|
[1909] | 573 | UGraphReader& readUEdgeMap(std::string name, Map& map) { |
---|
| 574 | u_edgeset_reader.readUEdgeMap(name, map); |
---|
[1421] | 575 | return *this; |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | template <typename Map> |
---|
[1909] | 579 | UGraphReader& readUEdgeMap(std::string name, const Map& map) { |
---|
| 580 | u_edgeset_reader.readUEdgeMap(name, map); |
---|
[1421] | 581 | return *this; |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | |
---|
[1540] | 585 | /// \brief Give a new undirected edge map reading command to the reader. |
---|
[1421] | 586 | /// |
---|
[1540] | 587 | /// Give a new undirected edge map reading command to the reader. |
---|
[1421] | 588 | template <typename Reader, typename Map> |
---|
[1909] | 589 | UGraphReader& readUEdgeMap(std::string name, Map& map, |
---|
[1421] | 590 | const Reader& reader = Reader()) { |
---|
[1909] | 591 | u_edgeset_reader.readUEdgeMap(name, map, reader); |
---|
[1421] | 592 | return *this; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | template <typename Reader, typename Map> |
---|
[1909] | 596 | UGraphReader& readUEdgeMap(std::string name, const Map& map, |
---|
[1421] | 597 | const Reader& reader = Reader()) { |
---|
[1909] | 598 | u_edgeset_reader.readUEdgeMap(name, map, reader); |
---|
[1421] | 599 | return *this; |
---|
| 600 | } |
---|
| 601 | |
---|
[1540] | 602 | /// \brief Give a new undirected edge map skipping command to the reader. |
---|
[1421] | 603 | /// |
---|
[1540] | 604 | /// Give a new undirected edge map skipping command to the reader. |
---|
[1421] | 605 | template <typename Reader> |
---|
[1909] | 606 | UGraphReader& skipUEdgeMap(std::string name, |
---|
[1421] | 607 | const Reader& reader = Reader()) { |
---|
[1909] | 608 | u_edgeset_reader.skipUMap(name, reader); |
---|
[1421] | 609 | return *this; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | |
---|
[1540] | 613 | /// \brief Give a new edge map reading command to the reader. |
---|
[1421] | 614 | /// |
---|
[1540] | 615 | /// Give a new edge map reading command to the reader. |
---|
[1421] | 616 | template <typename Map> |
---|
[1909] | 617 | UGraphReader& readEdgeMap(std::string name, Map& map) { |
---|
| 618 | u_edgeset_reader.readEdgeMap(name, map); |
---|
[1421] | 619 | return *this; |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | template <typename Map> |
---|
[1909] | 623 | UGraphReader& readEdgeMap(std::string name, const Map& map) { |
---|
| 624 | u_edgeset_reader.readEdgeMap(name, map); |
---|
[1421] | 625 | return *this; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | |
---|
[1540] | 629 | /// \brief Give a new edge map reading command to the reader. |
---|
[1421] | 630 | /// |
---|
[1540] | 631 | /// Give a new edge map reading command to the reader. |
---|
[1421] | 632 | template <typename Reader, typename Map> |
---|
[1909] | 633 | UGraphReader& readEdgeMap(std::string name, Map& map, |
---|
[1421] | 634 | const Reader& reader = Reader()) { |
---|
[1909] | 635 | u_edgeset_reader.readEdgeMap(name, map, reader); |
---|
[1421] | 636 | return *this; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | template <typename Reader, typename Map> |
---|
[1909] | 640 | UGraphReader& readEdgeMap(std::string name, const Map& map, |
---|
[1421] | 641 | const Reader& reader = Reader()) { |
---|
[1909] | 642 | u_edgeset_reader.readEdgeMap(name, map, reader); |
---|
[1421] | 643 | return *this; |
---|
| 644 | } |
---|
| 645 | |
---|
[1540] | 646 | /// \brief Give a new edge map skipping command to the reader. |
---|
[1421] | 647 | /// |
---|
[1540] | 648 | /// Give a new edge map skipping command to the reader. |
---|
[1421] | 649 | template <typename Reader> |
---|
[1909] | 650 | UGraphReader& skipEdgeMap(std::string name, |
---|
[1421] | 651 | const Reader& reader = Reader()) { |
---|
[1909] | 652 | u_edgeset_reader.skipEdgeMap(name, reader); |
---|
[1421] | 653 | return *this; |
---|
| 654 | } |
---|
| 655 | |
---|
[1540] | 656 | /// \brief Give a new labeled node reading command to the reader. |
---|
[1421] | 657 | /// |
---|
[1540] | 658 | /// Give a new labeled node reading command to the reader. |
---|
[1909] | 659 | UGraphReader& readNode(std::string name, Node& node) { |
---|
[1421] | 660 | node_reader.readNode(name, node); |
---|
| 661 | return *this; |
---|
| 662 | } |
---|
| 663 | |
---|
[1540] | 664 | /// \brief Give a new labeled edge reading command to the reader. |
---|
[1421] | 665 | /// |
---|
[1540] | 666 | /// Give a new labeled edge reading command to the reader. |
---|
[1909] | 667 | UGraphReader& readEdge(std::string name, Edge& edge) { |
---|
| 668 | u_edge_reader.readEdge(name, edge); |
---|
[1429] | 669 | } |
---|
| 670 | |
---|
[1540] | 671 | /// \brief Give a new labeled undirected edge reading command to the |
---|
| 672 | /// reader. |
---|
[1429] | 673 | /// |
---|
[1540] | 674 | /// Give a new labeled undirected edge reading command to the reader. |
---|
[1909] | 675 | UGraphReader& readUEdge(std::string name, UEdge& edge) { |
---|
| 676 | u_edge_reader.readUEdge(name, edge); |
---|
[1421] | 677 | } |
---|
| 678 | |
---|
[1540] | 679 | /// \brief Give a new attribute reading command. |
---|
[1421] | 680 | /// |
---|
[1540] | 681 | /// Give a new attribute reading command. |
---|
[1421] | 682 | template <typename Value> |
---|
[1909] | 683 | UGraphReader& readAttribute(std::string name, Value& value) { |
---|
[1421] | 684 | attribute_reader.readAttribute(name, value); |
---|
| 685 | return *this; |
---|
| 686 | } |
---|
| 687 | |
---|
[1540] | 688 | /// \brief Give a new attribute reading command. |
---|
[1421] | 689 | /// |
---|
[1540] | 690 | /// Give a new attribute reading command. |
---|
[1421] | 691 | template <typename Reader, typename Value> |
---|
[1909] | 692 | UGraphReader& readAttribute(std::string name, Value& value, |
---|
[1421] | 693 | const Reader& reader) { |
---|
| 694 | attribute_reader.readAttribute<Reader>(name, value, reader); |
---|
| 695 | return *this; |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | /// \brief Conversion operator to LemonReader. |
---|
| 699 | /// |
---|
| 700 | /// Conversion operator to LemonReader. It make possible |
---|
| 701 | /// to access the encapsulated \e LemonReader, this way |
---|
| 702 | /// you can attach to this reader new instances of |
---|
| 703 | /// \e LemonReader::SectionReader. |
---|
| 704 | operator LemonReader&() { |
---|
| 705 | return *reader; |
---|
| 706 | } |
---|
| 707 | |
---|
[1540] | 708 | /// \brief Executes the reading commands. |
---|
[1421] | 709 | /// |
---|
[1540] | 710 | /// Executes the reading commands. |
---|
[1421] | 711 | void run() { |
---|
| 712 | reader->run(); |
---|
| 713 | } |
---|
| 714 | |
---|
[1901] | 715 | |
---|
| 716 | /// \brief Returns true if the reader can give back the items by its label. |
---|
[1429] | 717 | /// |
---|
[1901] | 718 | /// \brief Returns true if the reader can give back the items by its label. |
---|
| 719 | bool isLabelReader() const { |
---|
| 720 | return nodeset_reader.isLabelReader() && |
---|
[1909] | 721 | u_edgeset_reader.isLabelReader(); |
---|
[1901] | 722 | } |
---|
| 723 | |
---|
| 724 | /// \brief Gives back the node by its label. |
---|
| 725 | /// |
---|
| 726 | /// It reads an label from the stream and gives back which node belongs to |
---|
[1935] | 727 | /// it. It is possible only if there was read a "label" named node map. |
---|
[1901] | 728 | void readLabel(std::istream& is, Node& node) const { |
---|
| 729 | return nodeset_reader.readLabel(is, node); |
---|
[1429] | 730 | } |
---|
| 731 | |
---|
[1935] | 732 | /// \brief Gives back the edge by its label |
---|
[1429] | 733 | /// |
---|
[1901] | 734 | /// It reads an label from the stream and gives back which edge belongs to |
---|
[1935] | 735 | /// it. It is possible only if there was read a "label" named edge map. |
---|
[1901] | 736 | void readLabel(std::istream& is, Edge& edge) const { |
---|
[1909] | 737 | return u_edgeset_reader.readLabel(is, edge); |
---|
[1429] | 738 | } |
---|
| 739 | |
---|
[1901] | 740 | /// \brief Gives back the undirected edge by its label. |
---|
[1429] | 741 | /// |
---|
[1901] | 742 | /// It reads an label from the stream and gives back which undirected edge |
---|
[1935] | 743 | /// belongs to it. It is possible only if there was read a "label" named |
---|
[1429] | 744 | /// edge map. |
---|
[1909] | 745 | void readLabel(std::istream& is, UEdge& uedge) const { |
---|
| 746 | return u_edgeset_reader.readLabel(is, uedge); |
---|
[1429] | 747 | } |
---|
| 748 | |
---|
| 749 | |
---|
[1421] | 750 | private: |
---|
| 751 | |
---|
| 752 | LemonReader* reader; |
---|
| 753 | bool own_reader; |
---|
| 754 | |
---|
| 755 | DefaultSkipper skipper; |
---|
| 756 | |
---|
| 757 | NodeSetReader<Graph, ReaderTraits> nodeset_reader; |
---|
[1909] | 758 | UEdgeSetReader<Graph, ReaderTraits> u_edgeset_reader; |
---|
[1421] | 759 | |
---|
| 760 | NodeReader<Graph> node_reader; |
---|
[1909] | 761 | UEdgeReader<Graph> u_edge_reader; |
---|
[1421] | 762 | |
---|
| 763 | AttributeReader<ReaderTraits> attribute_reader; |
---|
| 764 | }; |
---|
| 765 | |
---|
[1744] | 766 | /// \brief Read an undirected graph from the input. |
---|
[1421] | 767 | /// |
---|
[1744] | 768 | /// It is a helper function to read an undirected graph from the given input |
---|
[1909] | 769 | /// stream. It gives back an UGraphReader object and this object |
---|
[1744] | 770 | /// can read more maps, labeled nodes, edges, undirected edges and |
---|
| 771 | /// attributes. |
---|
| 772 | /// |
---|
| 773 | /// \warning Do not forget to call the \c run() function. |
---|
| 774 | /// |
---|
[1534] | 775 | /// \param is The input stream. |
---|
| 776 | /// \param g The graph. |
---|
| 777 | template<typename Graph> |
---|
[1909] | 778 | UGraphReader<Graph> uGraphReader(std::istream& is, Graph &g) { |
---|
[1744] | 779 | return GraphReader<Graph>(is, g); |
---|
[1534] | 780 | } |
---|
| 781 | |
---|
[1744] | 782 | /// \brief Read an undirected graph from the input. |
---|
[1534] | 783 | /// |
---|
[1744] | 784 | /// It is a helper function to read an undirected graph from the given input |
---|
[1909] | 785 | /// file. It gives back an UGraphReader object and this object |
---|
[1744] | 786 | /// can read more maps, labeled nodes, edges, undirected edges and |
---|
| 787 | /// attributes. |
---|
| 788 | /// |
---|
| 789 | /// \warning Do not forget to call the \c run() function. |
---|
| 790 | /// |
---|
| 791 | /// \param fn The input filename. |
---|
[1421] | 792 | /// \param g The graph. |
---|
[1744] | 793 | template<typename Graph> |
---|
[1909] | 794 | UGraphReader<Graph> uGraphReader(const std::string& fn, Graph &g) { |
---|
[1744] | 795 | return GraphReader<Graph>(fn, g); |
---|
[1421] | 796 | } |
---|
| 797 | |
---|
[1333] | 798 | /// @} |
---|
[1137] | 799 | } |
---|
[1214] | 800 | |
---|
| 801 | #endif |
---|