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