[1137] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library |
---|
[1137] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1137] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[1287] | 17 | ///\ingroup io_group |
---|
[1137] | 18 | ///\file |
---|
[1287] | 19 | ///\brief Lemon Graph Format reader. |
---|
[1137] | 20 | |
---|
[1214] | 21 | #ifndef LEMON_GRAPH_READER_H |
---|
| 22 | #define LEMON_GRAPH_READER_H |
---|
| 23 | |
---|
[1137] | 24 | #include <iostream> |
---|
| 25 | |
---|
| 26 | #include <lemon/error.h> |
---|
[1408] | 27 | #include <lemon/lemon_reader.h> |
---|
[1137] | 28 | |
---|
| 29 | namespace lemon { |
---|
| 30 | |
---|
[1333] | 31 | /// \addtogroup io_group |
---|
| 32 | /// @{ |
---|
[1137] | 33 | |
---|
| 34 | /// \brief The graph reader class. |
---|
| 35 | /// |
---|
[1534] | 36 | /// The \c GraphReader class provides the graph input. |
---|
| 37 | /// Before you read this documentation it might be useful to read the general |
---|
| 38 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
[1540] | 39 | /// |
---|
[1534] | 40 | /// If you don't need very sophisticated |
---|
| 41 | /// behaviour then you can use the versions of the public function |
---|
| 42 | /// \ref readGraph() to read a graph (or a max flow instance etc). |
---|
| 43 | /// |
---|
[1540] | 44 | /// The file to be read may contain several maps and labeled nodes or |
---|
[1333] | 45 | /// edges. |
---|
| 46 | /// |
---|
| 47 | /// If you read a graph you need not read all the maps and items just those |
---|
| 48 | /// that you need. The interface of the \c GraphReader is very similar to |
---|
| 49 | /// the GraphWriter but the reading method does not depend on the order the |
---|
[1540] | 50 | /// given commands (i.e. you don't have to insist on the order in which the |
---|
| 51 | /// maps are given in the file). |
---|
[1333] | 52 | /// |
---|
[1540] | 53 | /// The reader object assumes that not readed values do not contain |
---|
[1333] | 54 | /// whitespaces, therefore it has some extra possibilities to control how |
---|
| 55 | /// it should skip the values when the string representation contains spaces. |
---|
| 56 | /// |
---|
| 57 | /// \code |
---|
| 58 | /// GraphReader<ListGraph> reader(std::cin, graph); |
---|
| 59 | /// \endcode |
---|
| 60 | /// |
---|
[1394] | 61 | /// The \c readNodeMap() function reads a map from the \c \@nodeset section. |
---|
[1333] | 62 | /// If there is a map that you do not want to read from the file and there is |
---|
| 63 | /// whitespace in the string represenation of the values then you should |
---|
| 64 | /// call the \c skipNodeMap() template member function with proper |
---|
| 65 | /// parameters. |
---|
| 66 | /// |
---|
| 67 | /// \code |
---|
[1421] | 68 | /// reader.readNodeMap("coords", coords); |
---|
[1333] | 69 | /// |
---|
[1394] | 70 | /// reader.readNodeMap<QuotedStringReader>("label", labelMap); |
---|
[1333] | 71 | /// reader.skipNodeMap<QuotedStringReader>("description"); |
---|
| 72 | /// |
---|
[1394] | 73 | /// reader.readNodeMap("color", colorMap); |
---|
[1333] | 74 | /// \endcode |
---|
| 75 | /// |
---|
[1394] | 76 | /// With the \c readEdgeMap() member function you can give an edge map |
---|
[1333] | 77 | /// reading command similar to the NodeMaps. |
---|
| 78 | /// |
---|
| 79 | /// \code |
---|
[1394] | 80 | /// reader.readEdgeMap("weight", weightMap); |
---|
| 81 | /// reader.readEdgeMap("label", labelMap); |
---|
[1333] | 82 | /// \endcode |
---|
| 83 | /// |
---|
[1408] | 84 | /// With \c readNode() and \c readEdge() functions you can read |
---|
| 85 | /// labeled Nodes and Edges. |
---|
[1333] | 86 | /// |
---|
| 87 | /// \code |
---|
[1394] | 88 | /// reader.readNode("source", sourceNode); |
---|
| 89 | /// reader.readNode("target", targetNode); |
---|
[1333] | 90 | /// |
---|
[1394] | 91 | /// reader.readEdge("observed", edge); |
---|
[1333] | 92 | /// \endcode |
---|
| 93 | /// |
---|
[1408] | 94 | /// With the \c readAttribute() functions you can read an attribute |
---|
[1540] | 95 | /// into a variable. You can specify the reader for the attribute as |
---|
[1408] | 96 | /// the nodemaps. |
---|
| 97 | /// |
---|
[1333] | 98 | /// After you give all read commands you must call the \c run() member |
---|
[1540] | 99 | /// function, which executes all the commands. |
---|
[1333] | 100 | /// |
---|
| 101 | /// \code |
---|
| 102 | /// reader.run(); |
---|
| 103 | /// \endcode |
---|
| 104 | /// |
---|
[1287] | 105 | /// \see DefaultReaderTraits |
---|
| 106 | /// \see QuotedStringReader |
---|
[1138] | 107 | /// \see \ref GraphWriter |
---|
| 108 | /// \see \ref graph-io-page |
---|
[1333] | 109 | /// \author Balazs Dezso |
---|
[1137] | 110 | template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> |
---|
| 111 | class GraphReader { |
---|
| 112 | public: |
---|
| 113 | |
---|
| 114 | typedef _Graph Graph; |
---|
| 115 | typedef typename Graph::Node Node; |
---|
| 116 | typedef typename Graph::Edge Edge; |
---|
| 117 | |
---|
| 118 | typedef _ReaderTraits ReaderTraits; |
---|
[1408] | 119 | typedef typename ReaderTraits::Skipper DefaultSkipper; |
---|
[1137] | 120 | |
---|
| 121 | /// \brief Construct a new GraphReader. |
---|
| 122 | /// |
---|
[1208] | 123 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 124 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 125 | GraphReader(std::istream& _is, Graph& _graph, |
---|
[1408] | 126 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
[1421] | 127 | : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper), |
---|
| 128 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 129 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 130 | std::string(), skipper), |
---|
[1408] | 131 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 132 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 133 | attribute_reader(*reader, std::string()) {} |
---|
| 134 | |
---|
| 135 | /// \brief Construct a new GraphReader. |
---|
| 136 | /// |
---|
| 137 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 138 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 139 | GraphReader(const std::string& _filename, Graph& _graph, |
---|
[1408] | 140 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 141 | : reader(new LemonReader(_filename)), own_reader(true), |
---|
[1421] | 142 | skipper(_skipper), |
---|
| 143 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 144 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 145 | std::string(), skipper), |
---|
[1408] | 146 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 147 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 148 | attribute_reader(*reader, std::string()) {} |
---|
| 149 | |
---|
| 150 | /// \brief Construct a new GraphReader. |
---|
| 151 | /// |
---|
| 152 | /// Construct a new GraphReader. It reads into the given graph |
---|
[1540] | 153 | /// and it uses the given reader as the default skipper. |
---|
[1705] | 154 | GraphReader(LemonReader& _reader, Graph& _graph, |
---|
[1408] | 155 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
[1421] | 156 | : reader(_reader), own_reader(false), skipper(_skipper), |
---|
| 157 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 158 | edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 159 | std::string(), skipper), |
---|
[1408] | 160 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 161 | edge_reader(*reader, edgeset_reader, std::string()), |
---|
| 162 | attribute_reader(*reader, std::string()) {} |
---|
[1137] | 163 | |
---|
| 164 | /// \brief Destruct the graph reader. |
---|
| 165 | /// |
---|
| 166 | /// Destruct the graph reader. |
---|
| 167 | ~GraphReader() { |
---|
[1408] | 168 | if (own_reader) |
---|
| 169 | delete reader; |
---|
[1137] | 170 | } |
---|
| 171 | |
---|
[1540] | 172 | /// \brief Give a new node map reading command to the reader. |
---|
[1137] | 173 | /// |
---|
[1540] | 174 | /// Give a new node map reading command to the reader. |
---|
[1137] | 175 | template <typename Map> |
---|
[1394] | 176 | GraphReader& readNodeMap(std::string name, Map& map) { |
---|
[1421] | 177 | nodeset_reader.readNodeMap(name, map); |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | template <typename Map> |
---|
| 182 | GraphReader& readNodeMap(std::string name, const Map& map) { |
---|
| 183 | nodeset_reader.readNodeMap(name, map); |
---|
[1408] | 184 | return *this; |
---|
[1137] | 185 | } |
---|
| 186 | |
---|
[1540] | 187 | /// \brief Give a new node map reading command to the reader. |
---|
[1137] | 188 | /// |
---|
[1540] | 189 | /// Give a new node map reading command to the reader. |
---|
[1137] | 190 | template <typename Reader, typename Map> |
---|
[1394] | 191 | GraphReader& readNodeMap(std::string name, Map& map, |
---|
[1137] | 192 | const Reader& reader = Reader()) { |
---|
[1421] | 193 | nodeset_reader.readNodeMap(name, map, reader); |
---|
| 194 | return *this; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | template <typename Reader, typename Map> |
---|
| 198 | GraphReader& readNodeMap(std::string name, const Map& map, |
---|
| 199 | const Reader& reader = Reader()) { |
---|
| 200 | nodeset_reader.readNodeMap(name, map, reader); |
---|
[1137] | 201 | return *this; |
---|
| 202 | } |
---|
| 203 | |
---|
[1540] | 204 | /// \brief Give a new node map skipping command to the reader. |
---|
[1137] | 205 | /// |
---|
[1540] | 206 | /// Give a new node map skipping command to the reader. |
---|
[1137] | 207 | template <typename Reader> |
---|
| 208 | GraphReader& skipNodeMap(std::string name, |
---|
| 209 | const Reader& reader = Reader()) { |
---|
[1421] | 210 | nodeset_reader.skipNodeMap(name, reader); |
---|
[1137] | 211 | return *this; |
---|
| 212 | } |
---|
| 213 | |
---|
[1540] | 214 | /// \brief Give a new edge map reading command to the reader. |
---|
[1137] | 215 | /// |
---|
[1540] | 216 | /// Give a new edge map reading command to the reader. |
---|
[1137] | 217 | template <typename Map> |
---|
[1394] | 218 | GraphReader& readEdgeMap(std::string name, Map& map) { |
---|
[1421] | 219 | edgeset_reader.readEdgeMap(name, map); |
---|
| 220 | return *this; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | template <typename Map> |
---|
| 224 | GraphReader& readEdgeMap(std::string name, const Map& map) { |
---|
| 225 | edgeset_reader.readEdgeMap(name, map); |
---|
[1408] | 226 | return *this; |
---|
[1137] | 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
[1540] | 230 | /// \brief Give a new edge map reading command to the reader. |
---|
[1137] | 231 | /// |
---|
[1540] | 232 | /// Give a new edge map reading command to the reader. |
---|
[1137] | 233 | template <typename Reader, typename Map> |
---|
[1394] | 234 | GraphReader& readEdgeMap(std::string name, Map& map, |
---|
[1137] | 235 | const Reader& reader = Reader()) { |
---|
[1421] | 236 | edgeset_reader.readEdgeMap(name, map, reader); |
---|
| 237 | return *this; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | template <typename Reader, typename Map> |
---|
| 241 | GraphReader& readEdgeMap(std::string name, const Map& map, |
---|
| 242 | const Reader& reader = Reader()) { |
---|
| 243 | edgeset_reader.readEdgeMap(name, map, reader); |
---|
[1137] | 244 | return *this; |
---|
| 245 | } |
---|
| 246 | |
---|
[1540] | 247 | /// \brief Give a new edge map skipping command to the reader. |
---|
[1137] | 248 | /// |
---|
[1540] | 249 | /// Give a new edge map skipping command to the reader. |
---|
[1137] | 250 | template <typename Reader> |
---|
[1421] | 251 | GraphReader& skipEdgeMap(std::string name, |
---|
[1137] | 252 | const Reader& reader = Reader()) { |
---|
[1421] | 253 | edgeset_reader.skipEdgeMap(name, reader); |
---|
[1137] | 254 | return *this; |
---|
| 255 | } |
---|
| 256 | |
---|
[1540] | 257 | /// \brief Give a new labeled node reading command to the reader. |
---|
[1137] | 258 | /// |
---|
[1540] | 259 | /// Give a new labeled node reading command to the reader. |
---|
[1394] | 260 | GraphReader& readNode(std::string name, Node& node) { |
---|
[1408] | 261 | node_reader.readNode(name, node); |
---|
[1137] | 262 | return *this; |
---|
| 263 | } |
---|
| 264 | |
---|
[1540] | 265 | /// \brief Give a new labeled edge reading command to the reader. |
---|
[1137] | 266 | /// |
---|
[1540] | 267 | /// Give a new labeled edge reading command to the reader. |
---|
[1394] | 268 | GraphReader& readEdge(std::string name, Edge& edge) { |
---|
[1408] | 269 | edge_reader.readEdge(name, edge); |
---|
[1476] | 270 | return *this; |
---|
[1408] | 271 | } |
---|
| 272 | |
---|
[1540] | 273 | /// \brief Give a new attribute reading command. |
---|
[1408] | 274 | /// |
---|
[1540] | 275 | /// Give a new attribute reading command. |
---|
[1408] | 276 | template <typename Value> |
---|
| 277 | GraphReader& readAttribute(std::string name, Value& value) { |
---|
| 278 | attribute_reader.readAttribute(name, value); |
---|
[1137] | 279 | return *this; |
---|
| 280 | } |
---|
[1408] | 281 | |
---|
[1540] | 282 | /// \brief Give a new attribute reading command. |
---|
[1408] | 283 | /// |
---|
[1540] | 284 | /// Give a new attribute reading command. |
---|
[1408] | 285 | template <typename Reader, typename Value> |
---|
| 286 | GraphReader& readAttribute(std::string name, Value& value, |
---|
| 287 | const Reader& reader) { |
---|
| 288 | attribute_reader.readAttribute<Reader>(name, value, reader); |
---|
| 289 | return *this; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /// \brief Conversion operator to LemonReader. |
---|
| 293 | /// |
---|
[1540] | 294 | /// Conversion operator to LemonReader. It makes possible to access the |
---|
| 295 | /// encapsulated \e LemonReader, this way you can attach to this reader |
---|
| 296 | /// new instances of \e LemonReader::SectionReader. For more details see |
---|
| 297 | /// the \ref rwbackground "Background of Reading and Writing". |
---|
[1408] | 298 | operator LemonReader&() { |
---|
| 299 | return *reader; |
---|
| 300 | } |
---|
[1137] | 301 | |
---|
[1540] | 302 | /// \brief Executes the reading commands. |
---|
[1137] | 303 | /// |
---|
[1540] | 304 | /// Executes the reading commands. |
---|
[1137] | 305 | void run() { |
---|
[1408] | 306 | reader->run(); |
---|
[1396] | 307 | } |
---|
| 308 | |
---|
[1429] | 309 | /// \brief Gives back the node by its id. |
---|
| 310 | /// |
---|
| 311 | /// It reads an id from the stream and gives back which node belongs to |
---|
| 312 | /// it. It is possible only if there was read an "id" named node map. |
---|
| 313 | Node readId(std::istream& is, Node) const { |
---|
| 314 | return nodeset_reader.readId(is, Node()); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /// \brief Gives back the edge by its id. |
---|
| 318 | /// |
---|
| 319 | /// It reads an id from the stream and gives back which edge belongs to |
---|
| 320 | /// it. It is possible only if there was read an "id" named edge map. |
---|
| 321 | Edge readId(std::istream& is, Edge) const { |
---|
| 322 | return edgeset_reader.readId(is, Edge()); |
---|
| 323 | } |
---|
| 324 | |
---|
[1137] | 325 | private: |
---|
| 326 | |
---|
[1408] | 327 | LemonReader* reader; |
---|
| 328 | bool own_reader; |
---|
[1137] | 329 | |
---|
[1408] | 330 | DefaultSkipper skipper; |
---|
[1137] | 331 | |
---|
[1408] | 332 | NodeSetReader<Graph, ReaderTraits> nodeset_reader; |
---|
| 333 | EdgeSetReader<Graph, ReaderTraits> edgeset_reader; |
---|
| 334 | |
---|
| 335 | NodeReader<Graph> node_reader; |
---|
| 336 | EdgeReader<Graph> edge_reader; |
---|
| 337 | |
---|
| 338 | AttributeReader<ReaderTraits> attribute_reader; |
---|
[1137] | 339 | }; |
---|
| 340 | |
---|
[1534] | 341 | |
---|
[1744] | 342 | /// \brief Read a graph from the input. |
---|
[1333] | 343 | /// |
---|
[1744] | 344 | /// It is a helper function to read a graph from the given input |
---|
| 345 | /// stream. It gives back an GraphReader object and this object |
---|
| 346 | /// can read more maps, labeled nodes, edges and attributes. |
---|
[1534] | 347 | /// |
---|
[1744] | 348 | /// \warning Do not forget to call the \c run() function. |
---|
| 349 | /// |
---|
[1534] | 350 | /// \param is The input stream. |
---|
| 351 | /// \param g The graph. |
---|
| 352 | template<typename Graph> |
---|
[1744] | 353 | GraphReader<Graph> graphReader(std::istream& is, Graph &g) { |
---|
| 354 | return GraphReader<Graph>(is, g); |
---|
[1534] | 355 | } |
---|
| 356 | |
---|
[1744] | 357 | /// \brief Read a graph from the input. |
---|
| 358 | /// |
---|
| 359 | /// It is a helper function to read a graph from the given input |
---|
| 360 | /// file. It gives back an GraphReader object and this object |
---|
| 361 | /// can read more maps, labeled nodes, edges and attributes. |
---|
| 362 | /// |
---|
| 363 | /// \warning Do not forget to call the \c run() function. |
---|
| 364 | /// |
---|
| 365 | /// \param fn The input filename. |
---|
[1534] | 366 | /// \param g The graph. |
---|
[1744] | 367 | template<typename Graph> |
---|
| 368 | GraphReader<Graph> graphReader(const std::string& fn, Graph &g) { |
---|
| 369 | return GraphReader<Graph>(fn, g); |
---|
[1534] | 370 | } |
---|
| 371 | |
---|
[1744] | 372 | /// \brief The undirected graph reader class. |
---|
[1421] | 373 | /// |
---|
[1540] | 374 | /// The \c UndirGraphReader class provides the graph input. |
---|
| 375 | /// Before you read this documentation it might be useful to read the general |
---|
| 376 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
| 377 | /// |
---|
| 378 | /// If you don't need very sophisticated |
---|
| 379 | /// behaviour then you can use the versions of the public function |
---|
| 380 | /// \ref readGraph() to read a graph (or a max flow instance etc). |
---|
| 381 | /// |
---|
[1421] | 382 | /// The given file format may contain several maps and labeled nodes or |
---|
| 383 | /// edges. |
---|
| 384 | /// |
---|
| 385 | /// If you read a graph you need not read all the maps and items just those |
---|
[1540] | 386 | /// that you need. The interface of the \c UndirGraphReader is very similar |
---|
| 387 | /// to the UndirGraphWriter but the reading method does not depend on the |
---|
| 388 | /// order of the given commands. |
---|
[1421] | 389 | /// |
---|
| 390 | /// The reader object suppose that each not readed value does not contain |
---|
| 391 | /// whitespaces, therefore it has some extra possibilities to control how |
---|
| 392 | /// it should skip the values when the string representation contains spaces. |
---|
| 393 | /// |
---|
| 394 | /// \code |
---|
| 395 | /// UndirGraphReader<UndirListGraph> reader(std::cin, graph); |
---|
| 396 | /// \endcode |
---|
| 397 | /// |
---|
| 398 | /// The \c readNodeMap() function reads a map from the \c \@nodeset section. |
---|
| 399 | /// If there is a map that you do not want to read from the file and there is |
---|
| 400 | /// whitespace in the string represenation of the values then you should |
---|
| 401 | /// call the \c skipNodeMap() template member function with proper |
---|
| 402 | /// parameters. |
---|
| 403 | /// |
---|
| 404 | /// \code |
---|
| 405 | /// reader.readNodeMap("coords", coords); |
---|
| 406 | /// |
---|
| 407 | /// reader.readNodeMap<QuotedStringReader>("label", labelMap); |
---|
| 408 | /// reader.skipNodeMap<QuotedStringReader>("description"); |
---|
| 409 | /// |
---|
| 410 | /// reader.readNodeMap("color", colorMap); |
---|
| 411 | /// \endcode |
---|
| 412 | /// |
---|
| 413 | /// With the \c readUndirEdgeMap() member function you can give an |
---|
| 414 | /// undir edge map reading command similar to the NodeMaps. |
---|
| 415 | /// |
---|
| 416 | /// \code |
---|
| 417 | /// reader.readUndirEdgeMap("capacity", capacityMap); |
---|
| 418 | /// \endcode |
---|
| 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 | /// |
---|
| 425 | /// \code |
---|
| 426 | /// reader.readEdgeMap("flow", flowMap); |
---|
| 427 | /// \endcode |
---|
| 428 | /// |
---|
| 429 | /// With \c readNode() and \c readUndirEdge() functions you can read |
---|
| 430 | /// labeled Nodes and UndirEdges. |
---|
| 431 | /// |
---|
| 432 | /// \code |
---|
| 433 | /// reader.readNode("source", sourceNode); |
---|
| 434 | /// reader.readNode("target", targetNode); |
---|
| 435 | /// |
---|
| 436 | /// reader.readUndirEdge("observed", undirEdge); |
---|
| 437 | /// \endcode |
---|
| 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 | /// |
---|
| 446 | /// \code |
---|
| 447 | /// reader.run(); |
---|
| 448 | /// \endcode |
---|
| 449 | /// |
---|
| 450 | /// \see GraphReader |
---|
| 451 | /// \see DefaultReaderTraits |
---|
| 452 | /// \see \ref UndirGraphWriter |
---|
| 453 | /// \see \ref graph-io-page |
---|
| 454 | /// |
---|
| 455 | /// \author Balazs Dezso |
---|
| 456 | template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> |
---|
| 457 | class UndirGraphReader { |
---|
| 458 | public: |
---|
| 459 | |
---|
| 460 | typedef _Graph Graph; |
---|
| 461 | typedef typename Graph::Node Node; |
---|
| 462 | typedef typename Graph::Edge Edge; |
---|
| 463 | typedef typename Graph::UndirEdge UndirEdge; |
---|
| 464 | |
---|
| 465 | typedef _ReaderTraits ReaderTraits; |
---|
| 466 | typedef typename ReaderTraits::Skipper DefaultSkipper; |
---|
| 467 | |
---|
| 468 | /// \brief Construct a new UndirGraphReader. |
---|
| 469 | /// |
---|
| 470 | /// Construct a new UndirGraphReader. It reads into the given graph |
---|
| 471 | /// and it use the given reader as the default skipper. |
---|
| 472 | UndirGraphReader(std::istream& _is, Graph& _graph, |
---|
| 473 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 474 | : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper), |
---|
| 475 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 476 | undir_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 477 | std::string(), skipper), |
---|
| 478 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 479 | undir_edge_reader(*reader, undir_edgeset_reader, std::string()), |
---|
| 480 | attribute_reader(*reader, std::string()) {} |
---|
| 481 | |
---|
| 482 | /// \brief Construct a new UndirGraphReader. |
---|
| 483 | /// |
---|
| 484 | /// Construct a new UndirGraphReader. It reads into the given graph |
---|
| 485 | /// and it use the given reader as the default skipper. |
---|
| 486 | UndirGraphReader(const std::string& _filename, Graph& _graph, |
---|
| 487 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 488 | : reader(new LemonReader(_filename)), own_reader(true), |
---|
| 489 | skipper(_skipper), |
---|
| 490 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 491 | undir_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 492 | std::string(), skipper), |
---|
| 493 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 494 | undir_edge_reader(*reader, undir_edgeset_reader, std::string()), |
---|
| 495 | attribute_reader(*reader, std::string()) {} |
---|
| 496 | |
---|
| 497 | /// \brief Construct a new UndirGraphReader. |
---|
| 498 | /// |
---|
| 499 | /// Construct a new UndirGraphReader. It reads into the given graph |
---|
| 500 | /// and it use the given reader as the default skipper. |
---|
| 501 | UndirGraphReader(LemonReader& _reader, Graph& _graph, |
---|
| 502 | const DefaultSkipper& _skipper = DefaultSkipper()) |
---|
| 503 | : reader(_reader), own_reader(false), skipper(_skipper), |
---|
| 504 | nodeset_reader(*reader, _graph, std::string(), skipper), |
---|
| 505 | undir_edgeset_reader(*reader, _graph, nodeset_reader, |
---|
| 506 | std::string(), skipper), |
---|
| 507 | node_reader(*reader, nodeset_reader, std::string()), |
---|
| 508 | undir_edge_reader(*reader, undir_edgeset_reader, std::string()), |
---|
| 509 | attribute_reader(*reader, std::string()) {} |
---|
| 510 | |
---|
| 511 | /// \brief Destruct the graph reader. |
---|
| 512 | /// |
---|
| 513 | /// Destruct the graph reader. |
---|
| 514 | ~UndirGraphReader() { |
---|
| 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> |
---|
| 523 | UndirGraphReader& readNodeMap(std::string name, Map& map) { |
---|
| 524 | nodeset_reader.readNodeMap(name, map); |
---|
| 525 | return *this; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | template <typename Map> |
---|
| 529 | UndirGraphReader& readNodeMap(std::string name, const Map& map) { |
---|
| 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> |
---|
| 538 | UndirGraphReader& readNodeMap(std::string name, Map& map, |
---|
| 539 | const Reader& reader = Reader()) { |
---|
| 540 | nodeset_reader.readNodeMap(name, map, reader); |
---|
| 541 | return *this; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | template <typename Reader, typename Map> |
---|
| 545 | UndirGraphReader& readNodeMap(std::string name, const Map& map, |
---|
| 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> |
---|
| 555 | UndirGraphReader& skipNodeMap(std::string name, |
---|
| 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> |
---|
| 565 | UndirGraphReader& readUndirEdgeMap(std::string name, Map& map) { |
---|
| 566 | undir_edgeset_reader.readUndirEdgeMap(name, map); |
---|
| 567 | return *this; |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | template <typename Map> |
---|
| 571 | UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map) { |
---|
| 572 | undir_edgeset_reader.readUndirEdgeMap(name, map); |
---|
| 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> |
---|
| 581 | UndirGraphReader& readUndirEdgeMap(std::string name, Map& map, |
---|
| 582 | const Reader& reader = Reader()) { |
---|
| 583 | undir_edgeset_reader.readUndirEdgeMap(name, map, reader); |
---|
| 584 | return *this; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | template <typename Reader, typename Map> |
---|
| 588 | UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map, |
---|
| 589 | const Reader& reader = Reader()) { |
---|
| 590 | undir_edgeset_reader.readUndirEdgeMap(name, map, reader); |
---|
| 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> |
---|
| 598 | UndirGraphReader& skipUndirEdgeMap(std::string name, |
---|
| 599 | const Reader& reader = Reader()) { |
---|
| 600 | undir_edgeset_reader.skipUndirMap(name, reader); |
---|
| 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> |
---|
| 609 | UndirGraphReader& readEdgeMap(std::string name, Map& map) { |
---|
| 610 | undir_edgeset_reader.readEdgeMap(name, map); |
---|
| 611 | return *this; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | template <typename Map> |
---|
| 615 | UndirGraphReader& readEdgeMap(std::string name, const Map& map) { |
---|
| 616 | undir_edgeset_reader.readEdgeMap(name, map); |
---|
| 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> |
---|
| 625 | UndirGraphReader& readEdgeMap(std::string name, Map& map, |
---|
| 626 | const Reader& reader = Reader()) { |
---|
| 627 | undir_edgeset_reader.readEdgeMap(name, map, reader); |
---|
| 628 | return *this; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | template <typename Reader, typename Map> |
---|
| 632 | UndirGraphReader& readEdgeMap(std::string name, const Map& map, |
---|
| 633 | const Reader& reader = Reader()) { |
---|
| 634 | undir_edgeset_reader.readEdgeMap(name, map, reader); |
---|
| 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> |
---|
| 642 | UndirGraphReader& skipEdgeMap(std::string name, |
---|
| 643 | const Reader& reader = Reader()) { |
---|
| 644 | undir_edgeset_reader.skipEdgeMap(name, reader); |
---|
| 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. |
---|
[1421] | 651 | UndirGraphReader& readNode(std::string name, Node& node) { |
---|
| 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. |
---|
[1429] | 659 | UndirGraphReader& readEdge(std::string name, Edge& edge) { |
---|
| 660 | undir_edge_reader.readEdge(name, edge); |
---|
| 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. |
---|
[1421] | 667 | UndirGraphReader& readUndirEdge(std::string name, UndirEdge& edge) { |
---|
| 668 | undir_edge_reader.readUndirEdge(name, edge); |
---|
| 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> |
---|
| 675 | UndirGraphReader& readAttribute(std::string name, Value& value) { |
---|
| 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> |
---|
| 684 | UndirGraphReader& readAttribute(std::string name, Value& value, |
---|
| 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 | |
---|
[1429] | 707 | /// \brief Gives back the node by its id. |
---|
| 708 | /// |
---|
| 709 | /// It reads an id from the stream and gives back which node belongs to |
---|
| 710 | /// it. It is possible only if there was read an "id" named node map. |
---|
| 711 | Node readId(std::istream& is, Node) const { |
---|
| 712 | return nodeset_reader.readId(is, Node()); |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | /// \brief Gives back the edge by its id. |
---|
| 716 | /// |
---|
| 717 | /// It reads an id from the stream and gives back which edge belongs to |
---|
| 718 | /// it. It is possible only if there was read an "id" named edge map. |
---|
| 719 | Edge readId(std::istream& is, Edge) const { |
---|
| 720 | return undir_edgeset_reader.readId(is, Edge()); |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | /// \brief Gives back the undirected edge by its id. |
---|
| 724 | /// |
---|
| 725 | /// It reads an id from the stream and gives back which undirected edge |
---|
| 726 | /// belongs to it. It is possible only if there was read an "id" named |
---|
| 727 | /// edge map. |
---|
| 728 | UndirEdge readId(std::istream& is, UndirEdge) const { |
---|
| 729 | return undir_edgeset_reader.readId(is, UndirEdge()); |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | |
---|
[1421] | 733 | private: |
---|
| 734 | |
---|
| 735 | LemonReader* reader; |
---|
| 736 | bool own_reader; |
---|
| 737 | |
---|
| 738 | DefaultSkipper skipper; |
---|
| 739 | |
---|
| 740 | NodeSetReader<Graph, ReaderTraits> nodeset_reader; |
---|
| 741 | UndirEdgeSetReader<Graph, ReaderTraits> undir_edgeset_reader; |
---|
| 742 | |
---|
| 743 | NodeReader<Graph> node_reader; |
---|
| 744 | UndirEdgeReader<Graph> undir_edge_reader; |
---|
| 745 | |
---|
| 746 | AttributeReader<ReaderTraits> attribute_reader; |
---|
| 747 | }; |
---|
| 748 | |
---|
[1744] | 749 | /// \brief Read an undirected graph from the input. |
---|
[1421] | 750 | /// |
---|
[1744] | 751 | /// It is a helper function to read an undirected graph from the given input |
---|
| 752 | /// stream. It gives back an UndirGraphReader object and this object |
---|
| 753 | /// can read more maps, labeled nodes, edges, undirected edges and |
---|
| 754 | /// attributes. |
---|
| 755 | /// |
---|
| 756 | /// \warning Do not forget to call the \c run() function. |
---|
| 757 | /// |
---|
[1534] | 758 | /// \param is The input stream. |
---|
| 759 | /// \param g The graph. |
---|
| 760 | template<typename Graph> |
---|
[1744] | 761 | UndirGraphReader<Graph> undirGraphReader(std::istream& is, Graph &g) { |
---|
| 762 | return GraphReader<Graph>(is, g); |
---|
[1534] | 763 | } |
---|
| 764 | |
---|
[1744] | 765 | /// \brief Read an undirected graph from the input. |
---|
[1534] | 766 | /// |
---|
[1744] | 767 | /// It is a helper function to read an undirected graph from the given input |
---|
| 768 | /// file. It gives back an UndirGraphReader object and this object |
---|
| 769 | /// can read more maps, labeled nodes, edges, undirected edges and |
---|
| 770 | /// attributes. |
---|
| 771 | /// |
---|
| 772 | /// \warning Do not forget to call the \c run() function. |
---|
| 773 | /// |
---|
| 774 | /// \param fn The input filename. |
---|
[1421] | 775 | /// \param g The graph. |
---|
[1744] | 776 | template<typename Graph> |
---|
| 777 | UndirGraphReader<Graph> undirGraphReader(const std::string& fn, Graph &g) { |
---|
| 778 | return GraphReader<Graph>(fn, g); |
---|
[1421] | 779 | } |
---|
| 780 | |
---|
[1333] | 781 | /// @} |
---|
[1137] | 782 | } |
---|
[1214] | 783 | |
---|
| 784 | #endif |
---|