lemon/graph_reader.h
author athos
Thu, 09 Jun 2005 15:22:40 +0000
changeset 1464 718207d6a1e6
parent 1429 4283998fb2be
child 1476 182da222fceb
permissions -rw-r--r--
Bugfix.
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@1408
   264
    }
deba@1408
   265
deba@1408
   266
    /// \brief Add a new attribute reader command.
deba@1408
   267
    ///
deba@1408
   268
    ///  Add a new attribute reader command.
deba@1408
   269
    template <typename Value>
deba@1408
   270
    GraphReader& readAttribute(std::string name, Value& value) {
deba@1408
   271
      attribute_reader.readAttribute(name, value);
deba@1137
   272
      return *this;
deba@1137
   273
    }
deba@1408
   274
    
deba@1408
   275
    /// \brief Add a new attribute reader command.
deba@1408
   276
    ///
deba@1408
   277
    ///  Add a new attribute reader command.
deba@1408
   278
    template <typename Reader, typename Value>
deba@1408
   279
    GraphReader& readAttribute(std::string name, Value& value, 
deba@1408
   280
			       const Reader& reader) {
deba@1408
   281
      attribute_reader.readAttribute<Reader>(name, value, reader);
deba@1408
   282
      return *this;
deba@1408
   283
    }
deba@1408
   284
deba@1408
   285
    /// \brief Conversion operator to LemonReader.
deba@1408
   286
    ///
deba@1408
   287
    /// Conversion operator to LemonReader. It make possible
deba@1408
   288
    /// to access the encapsulated \e LemonReader, this way
deba@1408
   289
    /// you can attach to this reader new instances of 
deba@1408
   290
    /// \e LemonReader::SectionReader.
deba@1408
   291
    operator LemonReader&() {
deba@1408
   292
      return *reader;
deba@1408
   293
    }
deba@1137
   294
deba@1137
   295
    /// \brief Executes the reader commands.
deba@1137
   296
    ///
deba@1137
   297
    /// Executes the reader commands.
deba@1137
   298
    void run() {
deba@1408
   299
      reader->run();
deba@1396
   300
    }
deba@1396
   301
deba@1429
   302
    /// \brief Gives back the node by its id.
deba@1429
   303
    ///
deba@1429
   304
    /// It reads an id from the stream and gives back which node belongs to
deba@1429
   305
    /// it. It is possible only if there was read an "id" named node map.
deba@1429
   306
    Node readId(std::istream& is, Node) const {
deba@1429
   307
      return nodeset_reader.readId(is, Node());
deba@1429
   308
    } 
deba@1429
   309
deba@1429
   310
    /// \brief Gives back the edge by its id.
deba@1429
   311
    ///
deba@1429
   312
    /// It reads an id from the stream and gives back which edge belongs to
deba@1429
   313
    /// it. It is possible only if there was read an "id" named edge map.
deba@1429
   314
    Edge readId(std::istream& is, Edge) const {
deba@1429
   315
      return edgeset_reader.readId(is, Edge());
deba@1429
   316
    } 
deba@1429
   317
deba@1137
   318
  private:
deba@1137
   319
deba@1408
   320
    LemonReader* reader;
deba@1408
   321
    bool own_reader;
deba@1137
   322
deba@1408
   323
    DefaultSkipper skipper;
deba@1137
   324
deba@1408
   325
    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
deba@1408
   326
    EdgeSetReader<Graph, ReaderTraits> edgeset_reader;
deba@1408
   327
deba@1408
   328
    NodeReader<Graph> node_reader;
deba@1408
   329
    EdgeReader<Graph> edge_reader;
deba@1408
   330
    
deba@1408
   331
    AttributeReader<ReaderTraits> attribute_reader;
deba@1137
   332
  };
deba@1137
   333
deba@1333
   334
  /// \brief Read a graph from the input.
deba@1333
   335
  ///
deba@1333
   336
  /// Read a graph from the input.
deba@1333
   337
  /// \param is The input stream.
deba@1333
   338
  /// \param g The graph.
deba@1333
   339
  /// \param capacity The capacity map.
deba@1333
   340
  /// \param s The source node.
deba@1333
   341
  /// \param t The target node.
deba@1333
   342
  /// \param cost The cost map.
deba@1208
   343
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   344
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   345
		  typename Graph::Node &s, typename Graph::Node &t, 
deba@1208
   346
		  CostMap& cost) {
deba@1208
   347
    GraphReader<Graph> reader(is, g);
deba@1394
   348
    reader.readEdgeMap("capacity", capacity);
deba@1394
   349
    reader.readEdgeMap("cost", cost);
deba@1394
   350
    reader.readNode("source", s);
deba@1394
   351
    reader.readNode("target", t);
deba@1208
   352
    reader.run();
deba@1208
   353
  }
deba@1208
   354
deba@1333
   355
  /// \brief Read a graph from the input.
deba@1333
   356
  ///
deba@1333
   357
  /// Read a graph from the input.
deba@1333
   358
  /// \param is The input stream.
deba@1333
   359
  /// \param g The graph.
deba@1333
   360
  /// \param capacity The capacity map.
deba@1333
   361
  /// \param s The source node.
deba@1333
   362
  /// \param t The target node.
deba@1208
   363
  template<typename Graph, typename CapacityMap>
deba@1208
   364
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   365
		  typename Graph::Node &s, typename Graph::Node &t) {
deba@1208
   366
    GraphReader<Graph> reader(is, g);
deba@1394
   367
    reader.readEdgeMap("capacity", capacity);
deba@1394
   368
    reader.readNode("source", s);
deba@1394
   369
    reader.readNode("target", t);
deba@1208
   370
    reader.run();
deba@1208
   371
  }
deba@1208
   372
deba@1333
   373
  /// \brief Read a graph from the input.
deba@1333
   374
  ///
deba@1333
   375
  /// Read a graph from the input.
deba@1333
   376
  /// \param is The input stream.
deba@1333
   377
  /// \param g The graph.
deba@1333
   378
  /// \param capacity The capacity map.
deba@1333
   379
  /// \param s The source node.
deba@1208
   380
  template<typename Graph, typename CapacityMap>
deba@1208
   381
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   382
		  typename Graph::Node &s) {
deba@1208
   383
    GraphReader<Graph> reader(is, g);
deba@1394
   384
    reader.readEdgeMap("capacity", capacity);
deba@1394
   385
    reader.readNode("source", s);
deba@1208
   386
    reader.run();
deba@1208
   387
  }
deba@1208
   388
deba@1333
   389
  /// \brief Read a graph from the input.
deba@1333
   390
  ///
deba@1333
   391
  /// Read a graph from the input.
deba@1333
   392
  /// \param is The input stream.
deba@1333
   393
  /// \param g The graph.
deba@1333
   394
  /// \param capacity The capacity map.
deba@1208
   395
  template<typename Graph, typename CapacityMap>
deba@1208
   396
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1208
   397
    GraphReader<Graph> reader(is, g);
deba@1394
   398
    reader.readEdgeMap("capacity", capacity);
deba@1208
   399
    reader.run();
deba@1208
   400
  }
deba@1208
   401
deba@1333
   402
  /// \brief Read a graph from the input.
deba@1333
   403
  ///
deba@1333
   404
  /// Read a graph from the input.
deba@1333
   405
  /// \param is The input stream.
deba@1333
   406
  /// \param g The graph.
deba@1208
   407
  template<typename Graph>
deba@1208
   408
  void readGraph(std::istream& is, Graph &g) {
deba@1208
   409
    GraphReader<Graph> reader(is, g);
deba@1208
   410
    reader.run();
deba@1208
   411
  }
deba@1208
   412
deba@1421
   413
  /// \brief The undir graph reader class.
deba@1421
   414
  ///
deba@1421
   415
  /// The given file format may contain several maps and labeled nodes or 
deba@1421
   416
  /// edges.
deba@1421
   417
  ///
deba@1421
   418
  /// If you read a graph you need not read all the maps and items just those
deba@1421
   419
  /// that you need. The interface of the \c GraphReader is very similar to
deba@1421
   420
  /// the GraphWriter but the reading method does not depend on the order the
deba@1421
   421
  /// given commands.
deba@1421
   422
  ///
deba@1421
   423
  /// The reader object suppose that each not readed value does not contain 
deba@1421
   424
  /// whitespaces, therefore it has some extra possibilities to control how
deba@1421
   425
  /// it should skip the values when the string representation contains spaces.
deba@1421
   426
  ///
deba@1421
   427
  /// \code
deba@1421
   428
  /// UndirGraphReader<UndirListGraph> reader(std::cin, graph);
deba@1421
   429
  /// \endcode
deba@1421
   430
  ///
deba@1421
   431
  /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
deba@1421
   432
  /// If there is a map that you do not want to read from the file and there is
deba@1421
   433
  /// whitespace in the string represenation of the values then you should
deba@1421
   434
  /// call the \c skipNodeMap() template member function with proper 
deba@1421
   435
  /// parameters.
deba@1421
   436
  ///
deba@1421
   437
  /// \code
deba@1421
   438
  /// reader.readNodeMap("coords", coords);
deba@1421
   439
  ///
deba@1421
   440
  /// reader.readNodeMap<QuotedStringReader>("label", labelMap);
deba@1421
   441
  /// reader.skipNodeMap<QuotedStringReader>("description");
deba@1421
   442
  ///
deba@1421
   443
  /// reader.readNodeMap("color", colorMap);
deba@1421
   444
  /// \endcode
deba@1421
   445
  ///
deba@1421
   446
  /// With the \c readUndirEdgeMap() member function you can give an 
deba@1421
   447
  /// undir edge map reading command similar to the NodeMaps. 
deba@1421
   448
  ///
deba@1421
   449
  /// \code
deba@1421
   450
  /// reader.readUndirEdgeMap("capacity", capacityMap);
deba@1421
   451
  /// \endcode
deba@1421
   452
  ///
deba@1421
   453
  /// The reading of the directed edge maps is just a syntactical sugar.
deba@1421
   454
  /// It reads two undirected edgemaps into a directed edge map. The 
deba@1421
   455
  /// undirected edge maps' name should be start with the \c '+' and the
deba@1421
   456
  /// \c '-' character and the same.
deba@1421
   457
  ///
deba@1421
   458
  /// \code
deba@1421
   459
  /// reader.readEdgeMap("flow", flowMap);
deba@1421
   460
  /// \endcode 
deba@1421
   461
  ///
deba@1421
   462
  /// With \c readNode() and \c readUndirEdge() functions you can read 
deba@1421
   463
  /// labeled Nodes and UndirEdges.
deba@1421
   464
  ///
deba@1421
   465
  /// \code
deba@1421
   466
  /// reader.readNode("source", sourceNode);
deba@1421
   467
  /// reader.readNode("target", targetNode);
deba@1421
   468
  ///
deba@1421
   469
  /// reader.readUndirEdge("observed", undirEdge);
deba@1421
   470
  /// \endcode
deba@1421
   471
  ///
deba@1421
   472
  /// With the \c readAttribute() functions you can read an attribute
deba@1421
   473
  /// in a variable. You can specify the reader for the attribute as
deba@1421
   474
  /// the nodemaps.
deba@1421
   475
  ///
deba@1421
   476
  /// After you give all read commands you must call the \c run() member
deba@1421
   477
  /// function, which execute all the commands.
deba@1421
   478
  ///
deba@1421
   479
  /// \code
deba@1421
   480
  /// reader.run();
deba@1421
   481
  /// \endcode
deba@1421
   482
  ///
deba@1421
   483
  /// \see GraphReader
deba@1421
   484
  /// \see DefaultReaderTraits
deba@1421
   485
  /// \see \ref UndirGraphWriter
deba@1421
   486
  /// \see \ref graph-io-page
deba@1421
   487
  ///
deba@1421
   488
  /// \author Balazs Dezso
deba@1421
   489
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1421
   490
  class UndirGraphReader {
deba@1421
   491
  public:
deba@1421
   492
    
deba@1421
   493
    typedef _Graph Graph;
deba@1421
   494
    typedef typename Graph::Node Node;
deba@1421
   495
    typedef typename Graph::Edge Edge;
deba@1421
   496
    typedef typename Graph::UndirEdge UndirEdge;
deba@1421
   497
deba@1421
   498
    typedef _ReaderTraits ReaderTraits;
deba@1421
   499
    typedef typename ReaderTraits::Skipper DefaultSkipper;
deba@1421
   500
deba@1421
   501
    /// \brief Construct a new UndirGraphReader.
deba@1421
   502
    ///
deba@1421
   503
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   504
    /// and it use the given reader as the default skipper.
deba@1421
   505
    UndirGraphReader(std::istream& _is, Graph& _graph, 
deba@1421
   506
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   507
      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
deba@1421
   508
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   509
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   510
			     std::string(), skipper),
deba@1421
   511
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   512
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   513
	attribute_reader(*reader, std::string()) {}
deba@1421
   514
deba@1421
   515
    /// \brief Construct a new UndirGraphReader.
deba@1421
   516
    ///
deba@1421
   517
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   518
    /// and it use the given reader as the default skipper.
deba@1421
   519
    UndirGraphReader(const std::string& _filename, Graph& _graph, 
deba@1421
   520
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   521
      : reader(new LemonReader(_filename)), own_reader(true), 
deba@1421
   522
	skipper(_skipper),
deba@1421
   523
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   524
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   525
			     std::string(), skipper),
deba@1421
   526
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   527
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   528
	attribute_reader(*reader, std::string()) {}
deba@1421
   529
deba@1421
   530
    /// \brief Construct a new UndirGraphReader.
deba@1421
   531
    ///
deba@1421
   532
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   533
    /// and it use the given reader as the default skipper.
deba@1421
   534
    UndirGraphReader(LemonReader& _reader, Graph& _graph, 
deba@1421
   535
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   536
      : reader(_reader), own_reader(false), skipper(_skipper),
deba@1421
   537
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   538
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   539
			     std::string(), skipper),
deba@1421
   540
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   541
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   542
	attribute_reader(*reader, std::string()) {}
deba@1421
   543
deba@1421
   544
    /// \brief Destruct the graph reader.
deba@1421
   545
    ///
deba@1421
   546
    /// Destruct the graph reader.
deba@1421
   547
    ~UndirGraphReader() {
deba@1421
   548
      if (own_reader) 
deba@1421
   549
	delete reader;
deba@1421
   550
    }
deba@1421
   551
deba@1421
   552
    /// \brief Add a new node map reader command for the reader.
deba@1421
   553
    ///
deba@1421
   554
    /// Add a new node map reader command for the reader.
deba@1421
   555
    template <typename Map>
deba@1421
   556
    UndirGraphReader& readNodeMap(std::string name, Map& map) {
deba@1421
   557
      nodeset_reader.readNodeMap(name, map);
deba@1421
   558
      return *this;
deba@1421
   559
    }
deba@1421
   560
deba@1421
   561
    template <typename Map>
deba@1421
   562
    UndirGraphReader& readNodeMap(std::string name, const Map& map) {
deba@1421
   563
      nodeset_reader.readNodeMap(name, map);
deba@1421
   564
      return *this;
deba@1421
   565
    }
deba@1421
   566
deba@1421
   567
    /// \brief Add a new node map reader command for the reader.
deba@1421
   568
    ///
deba@1421
   569
    /// Add a new node map reader command for the reader.
deba@1421
   570
    template <typename Reader, typename Map>
deba@1421
   571
    UndirGraphReader& readNodeMap(std::string name, Map& map, 
deba@1421
   572
				  const Reader& reader = Reader()) {
deba@1421
   573
      nodeset_reader.readNodeMap(name, map, reader);
deba@1421
   574
      return *this;
deba@1421
   575
    }
deba@1421
   576
deba@1421
   577
    template <typename Reader, typename Map>
deba@1421
   578
    UndirGraphReader& readNodeMap(std::string name, const Map& map, 
deba@1421
   579
				  const Reader& reader = Reader()) {
deba@1421
   580
      nodeset_reader.readNodeMap(name, map, reader);
deba@1421
   581
      return *this;
deba@1421
   582
    }
deba@1421
   583
deba@1421
   584
    /// \brief Add a new node map skipper command for the reader.
deba@1421
   585
    ///
deba@1421
   586
    /// Add a new node map skipper command for the reader.
deba@1421
   587
    template <typename Reader>
deba@1421
   588
    UndirGraphReader& skipNodeMap(std::string name, 
deba@1421
   589
			     const Reader& reader = Reader()) {
deba@1421
   590
      nodeset_reader.skipNodeMap(name, reader);
deba@1421
   591
      return *this;
deba@1421
   592
    }
deba@1421
   593
deba@1421
   594
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
   595
    ///
deba@1421
   596
    /// Add a new undirected edge map reader command for the reader.
deba@1421
   597
    template <typename Map>
deba@1421
   598
    UndirGraphReader& readUndirEdgeMap(std::string name, Map& map) { 
deba@1421
   599
      undir_edgeset_reader.readUndirEdgeMap(name, map);
deba@1421
   600
      return *this;
deba@1421
   601
    }
deba@1421
   602
deba@1421
   603
    template <typename Map>
deba@1421
   604
    UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map) { 
deba@1421
   605
      undir_edgeset_reader.readUndirEdgeMap(name, map);
deba@1421
   606
      return *this;
deba@1421
   607
    }
deba@1421
   608
deba@1421
   609
deba@1421
   610
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
   611
    ///
deba@1421
   612
    /// Add a new undirected edge map reader command for the reader.
deba@1421
   613
    template <typename Reader, typename Map>
deba@1421
   614
    UndirGraphReader& readUndirEdgeMap(std::string name, Map& map,
deba@1421
   615
				       const Reader& reader = Reader()) {
deba@1421
   616
      undir_edgeset_reader.readUndirEdgeMap(name, map, reader);
deba@1421
   617
      return *this;
deba@1421
   618
    }
deba@1421
   619
deba@1421
   620
    template <typename Reader, typename Map>
deba@1421
   621
    UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map,
deba@1421
   622
				       const Reader& reader = Reader()) {
deba@1421
   623
      undir_edgeset_reader.readUndirEdgeMap(name, map, reader);
deba@1421
   624
      return *this;
deba@1421
   625
    }
deba@1421
   626
deba@1421
   627
    /// \brief Add a new undirected edge map skipper command for the reader.
deba@1421
   628
    ///
deba@1421
   629
    /// Add a new undirected edge map skipper command for the reader.
deba@1421
   630
    template <typename Reader>
deba@1421
   631
    UndirGraphReader& skipUndirEdgeMap(std::string name,
deba@1421
   632
				       const Reader& reader = Reader()) {
deba@1421
   633
      undir_edgeset_reader.skipUndirMap(name, reader);
deba@1421
   634
      return *this;
deba@1421
   635
    }
deba@1421
   636
deba@1421
   637
deba@1421
   638
    /// \brief Add a new edge map reader command for the reader.
deba@1421
   639
    ///
deba@1421
   640
    /// Add a new edge map reader command for the reader.
deba@1421
   641
    template <typename Map>
deba@1421
   642
    UndirGraphReader& readEdgeMap(std::string name, Map& map) { 
deba@1421
   643
      undir_edgeset_reader.readEdgeMap(name, map);
deba@1421
   644
      return *this;
deba@1421
   645
    }
deba@1421
   646
deba@1421
   647
    template <typename Map>
deba@1421
   648
    UndirGraphReader& readEdgeMap(std::string name, const Map& map) { 
deba@1421
   649
      undir_edgeset_reader.readEdgeMap(name, map);
deba@1421
   650
      return *this;
deba@1421
   651
    }
deba@1421
   652
deba@1421
   653
deba@1421
   654
    /// \brief Add a new edge map reader command for the reader.
deba@1421
   655
    ///
deba@1421
   656
    /// Add a new edge map reader command for the reader.
deba@1421
   657
    template <typename Reader, typename Map>
deba@1421
   658
    UndirGraphReader& readEdgeMap(std::string name, Map& map,
deba@1421
   659
				       const Reader& reader = Reader()) {
deba@1421
   660
      undir_edgeset_reader.readEdgeMap(name, map, reader);
deba@1421
   661
      return *this;
deba@1421
   662
    }
deba@1421
   663
deba@1421
   664
    template <typename Reader, typename Map>
deba@1421
   665
    UndirGraphReader& readEdgeMap(std::string name, const Map& map,
deba@1421
   666
				       const Reader& reader = Reader()) {
deba@1421
   667
      undir_edgeset_reader.readEdgeMap(name, map, reader);
deba@1421
   668
      return *this;
deba@1421
   669
    }
deba@1421
   670
deba@1421
   671
    /// \brief Add a new edge map skipper command for the reader.
deba@1421
   672
    ///
deba@1421
   673
    /// Add a new edge map skipper command for the reader.
deba@1421
   674
    template <typename Reader>
deba@1421
   675
    UndirGraphReader& skipEdgeMap(std::string name,
deba@1421
   676
				       const Reader& reader = Reader()) {
deba@1421
   677
      undir_edgeset_reader.skipEdgeMap(name, reader);
deba@1421
   678
      return *this;
deba@1421
   679
    }
deba@1421
   680
deba@1421
   681
    /// \brief Add a new labeled node reader for the reader.
deba@1421
   682
    ///
deba@1421
   683
    /// Add a new labeled node reader for the reader.
deba@1421
   684
    UndirGraphReader& readNode(std::string name, Node& node) {
deba@1421
   685
      node_reader.readNode(name, node);
deba@1421
   686
      return *this;
deba@1421
   687
    }
deba@1421
   688
deba@1421
   689
    /// \brief Add a new labeled edge reader for the reader.
deba@1421
   690
    ///
deba@1421
   691
    /// Add a new labeled edge reader for the reader.
deba@1429
   692
    UndirGraphReader& readEdge(std::string name, Edge& edge) {
deba@1429
   693
      undir_edge_reader.readEdge(name, edge);
deba@1429
   694
    }
deba@1429
   695
deba@1429
   696
    /// \brief Add a new labeled undirected edge reader for the reader.
deba@1429
   697
    ///
deba@1429
   698
    /// Add a new labeled undirected edge reader for the reader.
deba@1421
   699
    UndirGraphReader& readUndirEdge(std::string name, UndirEdge& edge) {
deba@1421
   700
      undir_edge_reader.readUndirEdge(name, edge);
deba@1421
   701
    }
deba@1421
   702
deba@1421
   703
    /// \brief Add a new attribute reader command.
deba@1421
   704
    ///
deba@1421
   705
    ///  Add a new attribute reader command.
deba@1421
   706
    template <typename Value>
deba@1421
   707
    UndirGraphReader& readAttribute(std::string name, Value& value) {
deba@1421
   708
      attribute_reader.readAttribute(name, value);
deba@1421
   709
      return *this;
deba@1421
   710
    }
deba@1421
   711
    
deba@1421
   712
    /// \brief Add a new attribute reader command.
deba@1421
   713
    ///
deba@1421
   714
    ///  Add a new attribute reader command.
deba@1421
   715
    template <typename Reader, typename Value>
deba@1421
   716
    UndirGraphReader& readAttribute(std::string name, Value& value, 
deba@1421
   717
			       const Reader& reader) {
deba@1421
   718
      attribute_reader.readAttribute<Reader>(name, value, reader);
deba@1421
   719
      return *this;
deba@1421
   720
    }
deba@1421
   721
deba@1421
   722
    /// \brief Conversion operator to LemonReader.
deba@1421
   723
    ///
deba@1421
   724
    /// Conversion operator to LemonReader. It make possible
deba@1421
   725
    /// to access the encapsulated \e LemonReader, this way
deba@1421
   726
    /// you can attach to this reader new instances of 
deba@1421
   727
    /// \e LemonReader::SectionReader.
deba@1421
   728
    operator LemonReader&() {
deba@1421
   729
      return *reader;
deba@1421
   730
    }
deba@1421
   731
deba@1421
   732
    /// \brief Executes the reader commands.
deba@1421
   733
    ///
deba@1421
   734
    /// Executes the reader commands.
deba@1421
   735
    void run() {
deba@1421
   736
      reader->run();
deba@1421
   737
    }
deba@1421
   738
deba@1429
   739
    /// \brief Gives back the node by its id.
deba@1429
   740
    ///
deba@1429
   741
    /// It reads an id from the stream and gives back which node belongs to
deba@1429
   742
    /// it. It is possible only if there was read an "id" named node map.
deba@1429
   743
    Node readId(std::istream& is, Node) const {
deba@1429
   744
      return nodeset_reader.readId(is, Node());
deba@1429
   745
    } 
deba@1429
   746
deba@1429
   747
    /// \brief Gives back the edge by its id.
deba@1429
   748
    ///
deba@1429
   749
    /// It reads an id from the stream and gives back which edge belongs to
deba@1429
   750
    /// it. It is possible only if there was read an "id" named edge map.
deba@1429
   751
    Edge readId(std::istream& is, Edge) const {
deba@1429
   752
      return undir_edgeset_reader.readId(is, Edge());
deba@1429
   753
    } 
deba@1429
   754
deba@1429
   755
    /// \brief Gives back the undirected edge by its id.
deba@1429
   756
    ///
deba@1429
   757
    /// It reads an id from the stream and gives back which undirected edge 
deba@1429
   758
    /// belongs to it. It is possible only if there was read an "id" named 
deba@1429
   759
    /// edge map.
deba@1429
   760
    UndirEdge readId(std::istream& is, UndirEdge) const {
deba@1429
   761
      return undir_edgeset_reader.readId(is, UndirEdge());
deba@1429
   762
    } 
deba@1429
   763
    
deba@1429
   764
deba@1421
   765
  private:
deba@1421
   766
deba@1421
   767
    LemonReader* reader;
deba@1421
   768
    bool own_reader;
deba@1421
   769
deba@1421
   770
    DefaultSkipper skipper;
deba@1421
   771
deba@1421
   772
    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
deba@1421
   773
    UndirEdgeSetReader<Graph, ReaderTraits> undir_edgeset_reader;
deba@1421
   774
deba@1421
   775
    NodeReader<Graph> node_reader;
deba@1421
   776
    UndirEdgeReader<Graph> undir_edge_reader;
deba@1421
   777
    
deba@1421
   778
    AttributeReader<ReaderTraits> attribute_reader;
deba@1421
   779
  };
deba@1421
   780
deba@1421
   781
  /// \brief Read an undir graph from the input.
deba@1421
   782
  ///
deba@1421
   783
  /// Read an undir graph from the input.
deba@1421
   784
  /// \param is The input stream.
deba@1421
   785
  /// \param g The graph.
deba@1421
   786
  /// \param capacity The capacity map.
deba@1421
   787
  template<typename Graph, typename CapacityMap>
deba@1421
   788
  void readUndirGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1421
   789
    UndirGraphReader<Graph> reader(is, g);
deba@1421
   790
    reader.readUndirEdgeMap("capacity", capacity);
deba@1421
   791
    reader.run();
deba@1421
   792
  }
deba@1421
   793
deba@1421
   794
  /// \brief Read an undir graph from the input.
deba@1421
   795
  ///
deba@1421
   796
  /// Read an undir graph from the input.
deba@1421
   797
  /// \param is The input stream.
deba@1421
   798
  /// \param g The graph.
deba@1421
   799
  template<typename Graph>
deba@1421
   800
  void readUndirGraph(std::istream& is, Graph &g) {
deba@1421
   801
    UndirGraphReader<Graph> reader(is, g);
deba@1421
   802
    reader.run();
deba@1421
   803
  }
deba@1421
   804
deba@1333
   805
  /// @}
deba@1137
   806
}
deba@1214
   807
deba@1214
   808
#endif