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