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