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