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