src/lemon/graph_reader.h
author ladanyi
Tue, 05 Apr 2005 22:37:19 +0000
changeset 1308 0274efa2222f
parent 1250 30f540067a80
child 1311 b810a07248a0
permissions -rw-r--r--
Applied the changes which somehow vanished during my last merge. Thanks goes
to Marci for noticing this. In detail:
- added amsmath and amssymb latex packages for latex documentation
- src/demo is also scanned for doxygen input files
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/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
deba@1137
     5
 * (Egervary Combinatorial Optimization Research Group, 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
#include <sstream>
deba@1137
    26
deba@1137
    27
#include <map>
deba@1137
    28
#include <vector>
deba@1137
    29
deba@1137
    30
#include <memory>
deba@1137
    31
deba@1137
    32
#include <lemon/error.h>
deba@1137
    33
deba@1137
    34
deba@1137
    35
namespace lemon {
deba@1137
    36
deba@1137
    37
deba@1137
    38
  /// \brief Standard ReaderTraits for the GraphReader class.
deba@1137
    39
  ///
deba@1137
    40
  /// Standard ReaderTraits for the GraphReader class.
deba@1137
    41
  /// It defines standard reading method for all type of value. 
deba@1137
    42
  struct DefaultReaderTraits {
deba@1137
    43
alpar@1287
    44
   /// \brief Template class for reading an value.
deba@1137
    45
    ///
deba@1137
    46
    /// Template class for reading an value.
deba@1137
    47
    template <typename _Value>
deba@1137
    48
    struct Reader {
deba@1137
    49
      /// The value type.
deba@1137
    50
      typedef _Value Value;
deba@1137
    51
      /// \brief Reads a value from the given stream.
deba@1137
    52
      ///
deba@1137
    53
      /// Reads a value from the given stream.
deba@1137
    54
      void read(std::istream& is, Value& value) {
deba@1137
    55
	if (!(is >> value)) 
deba@1208
    56
	  throw DataFormatError("Default reader format exception");
deba@1137
    57
      }
deba@1137
    58
    };
deba@1137
    59
deba@1137
    60
    /// The reader class for the not needed maps.
deba@1137
    61
    typedef Reader<std::string> DefaultReader;
deba@1137
    62
deba@1137
    63
  };
deba@1137
    64
deba@1137
    65
  /// \brief Reader class for quoted strings.
deba@1137
    66
  ///
deba@1137
    67
  /// Reader class for quoted strings. It can process the escape
deba@1137
    68
  /// sequences in the string.
deba@1137
    69
  class QuotedStringReader {
deba@1137
    70
  public:
deba@1137
    71
    typedef std::string Value;
deba@1137
    72
    
deba@1137
    73
    /// \brief Constructor for the reader.
deba@1137
    74
    ///
deba@1137
    75
    /// Constructor for the reader. If the given parameter is true
deba@1137
    76
    /// the reader processes the escape sequences.
deba@1137
    77
    QuotedStringReader(bool _escaped = true) : escaped(_escaped) {}
deba@1137
    78
    
deba@1137
    79
    /// \brief Reads a quoted string from the given stream.
deba@1137
    80
    ///
deba@1137
    81
    /// Reads a quoted string from the given stream.
deba@1137
    82
    void read(std::istream& is, std::string& value) {
deba@1137
    83
      char c;
deba@1137
    84
      value.clear();
deba@1137
    85
      is >> ws;
deba@1137
    86
      if (!is.get(c) || c != '\"') 
deba@1208
    87
	throw DataFormatError("Quoted string format error");
deba@1137
    88
      while (is.get(c) && c != '\"') {
deba@1137
    89
	if (escaped && c == '\\') {
deba@1137
    90
	  value += readEscape(is);
deba@1137
    91
	} else {
deba@1137
    92
	  value += c;
deba@1137
    93
	}
deba@1137
    94
      }
deba@1208
    95
      if (!is) throw DataFormatError("Quoted string format error");
deba@1137
    96
    }
deba@1137
    97
deba@1137
    98
  private:
deba@1137
    99
    
deba@1137
   100
    static char readEscape(std::istream& is) {
deba@1137
   101
      char c;
deba@1137
   102
      switch (is.get(c), c) {
deba@1137
   103
      case '\\':
deba@1137
   104
	return '\\';
deba@1137
   105
      case '\"':
deba@1137
   106
	return '\"';
deba@1137
   107
      case '\'':
deba@1137
   108
	return '\'';
deba@1137
   109
      case '\?':
deba@1137
   110
	return '\?';
deba@1137
   111
      case 'a':
deba@1137
   112
	return '\a';
deba@1137
   113
      case 'b':
deba@1137
   114
	return '\b';
deba@1137
   115
      case 'f':
deba@1137
   116
	return '\f';
deba@1137
   117
      case 'n':
deba@1137
   118
	return '\n';
deba@1137
   119
      case 'r':
deba@1137
   120
	return '\r';
deba@1137
   121
      case 't':
deba@1137
   122
	return '\t';
deba@1137
   123
      case 'v':
deba@1137
   124
	return '\v';
deba@1137
   125
      case 'x':
deba@1137
   126
	{
deba@1137
   127
	  int code;
deba@1137
   128
	  if (!is.get(c) || !isHex(c)) 
deba@1208
   129
	    throw DataFormatError("Escape format error");
deba@1137
   130
	  else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
deba@1137
   131
	  else code = code * 16 + valueHex(c);
deba@1137
   132
	  return code;
deba@1137
   133
	}
deba@1137
   134
      default:
deba@1137
   135
	{
deba@1137
   136
	  int code;
deba@1137
   137
	  if (!isOct(c)) 
deba@1208
   138
	    throw DataFormatError("Escape format error");
deba@1137
   139
	  else if (code = valueOct(c), !is.get(c) || !isOct(c)) 
deba@1137
   140
	    is.putback(c);
deba@1137
   141
	  else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 
deba@1137
   142
	    is.putback(c);
deba@1137
   143
	  else code = code * 8 + valueOct(c);
deba@1137
   144
	  return code;
deba@1137
   145
	}	      
deba@1137
   146
      } 
deba@1137
   147
    }
deba@1137
   148
deba@1137
   149
    static bool isOct(char c) {
deba@1137
   150
      return '0' <= c && c <='7'; 
deba@1137
   151
    }
deba@1137
   152
    
deba@1137
   153
    static int valueOct(char c) {
deba@1137
   154
      return c - '0';
deba@1137
   155
    }
deba@1137
   156
deba@1137
   157
   static bool isHex(char c) {
deba@1137
   158
      return ('0' <= c && c <= '9') || 
deba@1137
   159
	('a' <= c && c <= 'z') || 
deba@1137
   160
	('A' <= c && c <= 'Z'); 
deba@1137
   161
    }
deba@1137
   162
    
deba@1137
   163
    static int valueHex(char c) {
deba@1137
   164
      if ('0' <= c && c <= '9') return c - '0';
deba@1137
   165
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
deba@1137
   166
      return c - 'A' + 10;
deba@1137
   167
    }
deba@1137
   168
deba@1137
   169
    bool escaped;
deba@1137
   170
  };
deba@1137
   171
deba@1137
   172
  /// \brief The graph reader class.
deba@1137
   173
  ///
alpar@1287
   174
  /// \ingroup io_group
deba@1137
   175
  /// The reader class for the graph input.
alpar@1287
   176
  /// \see DefaultReaderTraits
alpar@1287
   177
  /// \see QuotedStringReader
alpar@1138
   178
  /// \see \ref GraphWriter
alpar@1138
   179
  /// \see \ref graph-io-page
deba@1137
   180
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1137
   181
  class GraphReader {
deba@1137
   182
  public:
deba@1137
   183
    
deba@1137
   184
    typedef _Graph Graph;
deba@1137
   185
    typedef typename Graph::Node Node;
deba@1137
   186
    typedef typename Graph::Edge Edge;
deba@1137
   187
deba@1137
   188
    typedef _ReaderTraits ReaderTraits;
deba@1137
   189
    typedef typename ReaderTraits::DefaultReader DefaultReader;
deba@1137
   190
deba@1137
   191
    /// \brief Construct a new GraphReader.
deba@1137
   192
    ///
deba@1208
   193
    /// Construct a new GraphReader. It reads into the given graph
deba@1208
   194
    /// and it use the given reader as the default skipper.
deba@1137
   195
    GraphReader(std::istream& _is, Graph& _graph, 
deba@1137
   196
		const DefaultReader& _reader = DefaultReader()) 
deba@1137
   197
      : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
deba@1137
   198
deba@1137
   199
    /// \brief Destruct the graph reader.
deba@1137
   200
    ///
deba@1137
   201
    /// Destruct the graph reader.
deba@1137
   202
    ~GraphReader() {
deba@1137
   203
deba@1137
   204
      for (typename NodeMapReaders::iterator it = node_map_readers.begin(); 
deba@1137
   205
	   it != node_map_readers.end(); ++it) {
deba@1137
   206
	delete it->second;
deba@1137
   207
      }
deba@1137
   208
deba@1137
   209
      for (typename EdgeMapReaders::iterator it = edge_map_readers.begin(); 
deba@1137
   210
	   it != edge_map_readers.end(); ++it) {
deba@1137
   211
	delete it->second;
deba@1137
   212
      }
deba@1137
   213
deba@1137
   214
    }
deba@1137
   215
deba@1137
   216
    /// \brief Add a new node map reader command for the reader.
deba@1137
   217
    ///
deba@1137
   218
    /// Add a new node map reader command for the reader.
deba@1137
   219
    template <typename Map>
deba@1137
   220
    GraphReader& addNodeMap(std::string name, Map& map) {
deba@1137
   221
      return addNodeMap<typename ReaderTraits::template 
deba@1137
   222
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   223
    }
deba@1137
   224
deba@1137
   225
    /// \brief Add a new node map reader command for the reader.
deba@1137
   226
    ///
deba@1137
   227
    /// Add a new node map reader command for the reader.
deba@1137
   228
    template <typename Reader, typename Map>
deba@1137
   229
    GraphReader& addNodeMap(std::string name, Map& map, 
deba@1137
   230
			     const Reader& reader = Reader()) {
deba@1137
   231
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1208
   232
	ErrorMessage msg;
deba@1208
   233
	msg << "Multiple read rule for node map: " << name;
deba@1214
   234
	throw IOParameterError(msg.message());
deba@1137
   235
      }
deba@1137
   236
      node_map_readers.insert(
deba@1137
   237
        make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
deba@1137
   238
      return *this;
deba@1137
   239
    }
deba@1137
   240
deba@1137
   241
    /// \brief Add a new node map skipper command for the reader.
deba@1137
   242
    ///
deba@1137
   243
    /// Add a new node map skipper command for the reader.
deba@1137
   244
    template <typename Reader>
deba@1137
   245
    GraphReader& skipNodeMap(std::string name, 
deba@1137
   246
			     const Reader& reader = Reader()) {
deba@1137
   247
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1208
   248
	ErrorMessage msg;
deba@1208
   249
	msg << "Multiple read rule for node map: " << name;
deba@1214
   250
	throw IOParameterError(msg.message());
deba@1137
   251
      }
deba@1137
   252
      node_map_readers.insert(
deba@1137
   253
        make_pair(name, new SkipReader<Node, Reader>(reader)));
deba@1137
   254
      return *this;
deba@1137
   255
    }
deba@1137
   256
deba@1137
   257
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   258
    ///
deba@1137
   259
    /// Add a new edge map reader command for the reader.
deba@1137
   260
    template <typename Map>
deba@1137
   261
    GraphReader& addEdgeMap(std::string name, Map& map) { 
deba@1137
   262
      return addEdgeMap<typename ReaderTraits::template
deba@1137
   263
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   264
    }
deba@1137
   265
deba@1137
   266
deba@1137
   267
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   268
    ///
deba@1137
   269
    /// Add a new edge map reader command for the reader.
deba@1137
   270
    template <typename Reader, typename Map>
deba@1137
   271
    GraphReader& addEdgeMap(std::string name, Map& map,
deba@1137
   272
			     const Reader& reader = Reader()) {
deba@1137
   273
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1208
   274
	ErrorMessage msg;
deba@1208
   275
	msg << "Multiple read rule for edge map: " << name;
deba@1214
   276
	throw IOParameterError(msg.message());
deba@1137
   277
      }
deba@1137
   278
      edge_map_readers.insert(
deba@1137
   279
        make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
deba@1137
   280
      return *this;
deba@1137
   281
    }
deba@1137
   282
deba@1137
   283
    /// \brief Add a new edge map skipper command for the reader.
deba@1137
   284
    ///
deba@1137
   285
    /// Add a new edge map skipper command for the reader.
deba@1137
   286
    template <typename Reader>
deba@1137
   287
    GraphReader& skipEdgeMap(std::string name,
deba@1137
   288
			     const Reader& reader = Reader()) {
deba@1137
   289
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1208
   290
	ErrorMessage msg;
deba@1208
   291
	msg << "Multiple read rule for edge map: " << name;
deba@1214
   292
	throw IOParameterError(msg.message());
deba@1137
   293
      }
deba@1137
   294
      edge_map_readers.insert(
deba@1137
   295
        make_pair(name, new SkipReader<Edge, Reader>(reader)));
deba@1137
   296
      return *this;
deba@1137
   297
    }
deba@1137
   298
deba@1137
   299
    /// \brief Add a new labeled node reader for the reader.
deba@1137
   300
    ///
deba@1137
   301
    /// Add a new labeled node reader for the reader.
deba@1137
   302
    GraphReader& addNode(std::string name, Node& node) {
deba@1137
   303
      if (node_readers.find(name) != node_readers.end()) {
deba@1208
   304
	ErrorMessage msg;
deba@1208
   305
	msg << "Multiple read rule for node: " << name;
deba@1214
   306
	throw IOParameterError(msg.message());
deba@1137
   307
      }
deba@1137
   308
      node_readers.insert(make_pair(name, &node));
deba@1137
   309
      return *this;
deba@1137
   310
    }
deba@1137
   311
deba@1137
   312
    /// \brief Add a new labeled edge reader for the reader.
deba@1137
   313
    ///
deba@1137
   314
    /// Add a new labeled edge reader for the reader.
deba@1137
   315
    GraphReader& addEdge(std::string name, Edge& edge) {
deba@1137
   316
      if (edge_readers.find(name) != edge_readers.end()) {
deba@1208
   317
	ErrorMessage msg;
deba@1208
   318
	msg << "Multiple read rule for edge: " << name;
deba@1214
   319
	throw IOParameterError(msg.message());
deba@1137
   320
      }
deba@1137
   321
      edge_readers.insert(make_pair(name, &edge));
deba@1137
   322
      return *this;
deba@1137
   323
    }
deba@1137
   324
deba@1137
   325
    /// \brief Executes the reader commands.
deba@1137
   326
    ///
deba@1137
   327
    /// Executes the reader commands.
deba@1137
   328
    void run() {
deba@1137
   329
      int line_num = 0;
deba@1137
   330
      std::auto_ptr<InverterBase<Node> > nodeInverter;
deba@1137
   331
      std::auto_ptr<InverterBase<Edge> > edgeInverter;
deba@1137
   332
      try {
deba@1137
   333
	std::string line = readNotEmptyLine(is, line_num);
deba@1137
   334
	if (line.find("@nodeset") == 0) {
deba@1137
   335
	  line = readNodeSet(line_num, nodeInverter);
deba@1137
   336
	} 
deba@1137
   337
	if (line.find("@edgeset") == 0) {
deba@1137
   338
	  line = readEdgeSet(line_num, edgeInverter, nodeInverter);
deba@1137
   339
	}
deba@1137
   340
	if (line.find("@nodes") == 0) {
deba@1137
   341
	  line = readNodes(line_num, nodeInverter);
deba@1137
   342
	}
deba@1137
   343
	if (line.find("@edges") == 0) {
deba@1137
   344
	  line = readEdges(line_num, edgeInverter);
deba@1137
   345
	}
deba@1137
   346
	if (line.find("@end") != 0) {
deba@1208
   347
	  throw DataFormatError("Invalid control sequence error");
deba@1137
   348
	}
deba@1208
   349
      } catch (DataFormatError e) {
deba@1208
   350
	e.line(line_num);
deba@1208
   351
	throw e;
deba@1137
   352
      }
deba@1137
   353
    }
deba@1137
   354
deba@1137
   355
  private:
deba@1137
   356
deba@1137
   357
    template <typename Item> class InverterBase;
deba@1137
   358
deba@1137
   359
    std::string readNodeSet(int& line_num, 
deba@1137
   360
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   361
      std::vector<ReaderBase<Node>* > index;
deba@1137
   362
      {
deba@1137
   363
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   364
	std::string id;
deba@1137
   365
	std::istringstream ls(line);	
deba@1137
   366
	while (ls >> id) {
deba@1137
   367
	  if (id[0] == '#') break;
deba@1137
   368
	  typename NodeMapReaders::iterator it = node_map_readers.find(id);
deba@1137
   369
	  if (it != node_map_readers.end()) {
deba@1137
   370
	    index.push_back(it->second);
deba@1137
   371
	    node_map_readers.erase(it);
deba@1137
   372
	  } else {
deba@1137
   373
	    index.push_back(&nodeSkipper);
deba@1137
   374
	  }
deba@1137
   375
	}
deba@1137
   376
      }
deba@1137
   377
deba@1137
   378
      if (index.size() == 0) {
deba@1208
   379
	throw DataFormatError("Cannot find node id map");
deba@1137
   380
      }
deba@1137
   381
deba@1137
   382
      nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
deba@1137
   383
      std::string line;
deba@1137
   384
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   385
	Node node = graph.addNode();
deba@1137
   386
	std::istringstream ls(line);
deba@1137
   387
	nodeInverter->read(ls, node);
deba@1137
   388
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   389
	  index[i]->read(ls, node);
deba@1137
   390
	}
deba@1137
   391
      }
deba@1137
   392
      return line;
deba@1137
   393
    }
deba@1137
   394
deba@1137
   395
    std::string readEdgeSet(int& line_num, 
deba@1137
   396
			    auto_ptr<InverterBase<Edge> > & edgeInverter, 
deba@1137
   397
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   398
      std::vector<ReaderBase<Edge>*> index;
deba@1137
   399
      {
deba@1137
   400
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   401
	std::string id;
deba@1137
   402
	std::istringstream ls(line);	
deba@1137
   403
	while (ls >> id) {
deba@1137
   404
	  if (id[0] == '#') break;
deba@1137
   405
	  typename EdgeMapReaders::iterator it = edge_map_readers.find(id);
deba@1137
   406
	  if (it != edge_map_readers.end()) {
deba@1137
   407
	    index.push_back(it->second);
deba@1137
   408
	    edge_map_readers.erase(it);
deba@1137
   409
	  } else {
deba@1137
   410
	    index.push_back(&edgeSkipper);
deba@1137
   411
	  }
deba@1137
   412
	}
deba@1137
   413
      }
deba@1137
   414
deba@1137
   415
      if (index.size() == 0) {
deba@1208
   416
	throw DataFormatError("Cannot find edge id map");
deba@1137
   417
      }
deba@1137
   418
deba@1137
   419
      edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
deba@1137
   420
      std::string line;
deba@1137
   421
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {	
deba@1137
   422
	std::istringstream ls(line);
deba@1137
   423
	Node source = nodeInverter->read(ls);
deba@1137
   424
	Node target = nodeInverter->read(ls);
deba@1137
   425
	Edge edge = graph.addEdge(source, target);
deba@1137
   426
	edgeInverter->read(ls, edge);
deba@1137
   427
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   428
	  index[i]->read(ls, edge);
deba@1137
   429
	}
deba@1137
   430
      }      
deba@1137
   431
      return line;
deba@1137
   432
    }
deba@1137
   433
deba@1137
   434
    std::string readNodes(int& line_num, 
deba@1137
   435
			  auto_ptr<InverterBase<Node> >& nodeInverter) {
deba@1137
   436
      std::string line;
deba@1137
   437
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   438
	std::istringstream ls(line);
deba@1137
   439
	std::string name;
deba@1137
   440
	ls >> name;
deba@1137
   441
	typename NodeReaders::iterator it = node_readers.find(name);
deba@1137
   442
	if (it != node_readers.end()) {
deba@1137
   443
	  *(it -> second) = nodeInverter->read(ls);
deba@1137
   444
	} 
deba@1137
   445
      }        
deba@1137
   446
      return line;
deba@1137
   447
    }
deba@1137
   448
deba@1137
   449
    std::string readEdges(int& line_num, 
deba@1137
   450
			  auto_ptr<InverterBase<Edge> >& edgeInverter) {
deba@1137
   451
      std::string line;
deba@1137
   452
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   453
	std::istringstream ls(line);
deba@1137
   454
	std::string name;
deba@1137
   455
	ls >> name;
deba@1137
   456
	typename EdgeReaders::iterator it = edge_readers.find(name);
deba@1137
   457
	if (it != edge_readers.end()) {
deba@1137
   458
	  *(it -> second) = edgeInverter->read(ls);
deba@1137
   459
	} 
deba@1137
   460
      }        
deba@1137
   461
      return line;    
deba@1137
   462
    }
deba@1137
   463
deba@1137
   464
    std::string readNotEmptyLine(std::istream& is, int& line_num) {
deba@1137
   465
      std::string line;
deba@1137
   466
      while (++line_num, getline(is, line)) {	
deba@1137
   467
	int vi = line.find_first_not_of(" \t");
deba@1137
   468
	if (vi != (int)string::npos && line[vi] != '#') {
deba@1137
   469
	  return line.substr(vi);
deba@1137
   470
	}
deba@1137
   471
      }
deba@1208
   472
      throw DataFormatError("End of stream error");
deba@1137
   473
    }
deba@1137
   474
    
deba@1137
   475
    // Inverters store and give back the Item from the id,
deba@1137
   476
    // and may put the ids into a map.
deba@1137
   477
    
deba@1137
   478
    template <typename _Item>
deba@1137
   479
    class InverterBase {
deba@1137
   480
    public:
deba@1137
   481
      typedef _Item Item;
deba@1137
   482
      virtual void read(std::istream&, const Item&) = 0;
deba@1137
   483
      virtual Item read(std::istream&) = 0;
deba@1137
   484
    };
deba@1137
   485
deba@1137
   486
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   487
    class MapReaderInverter : public InverterBase<_Item> {
deba@1137
   488
    public:
deba@1137
   489
      typedef _Item Item;
deba@1137
   490
      typedef _Reader Reader;
deba@1137
   491
      typedef typename Reader::Value Value;
deba@1137
   492
      typedef _Map Map;
deba@1137
   493
      typedef std::map<Value, Item> Inverse;
deba@1137
   494
deba@1137
   495
      Map& map;
deba@1137
   496
      Reader reader;
deba@1137
   497
      Inverse inverse;
deba@1137
   498
deba@1137
   499
      MapReaderInverter(Map& _map, const Reader& _reader) 
deba@1137
   500
	: map(_map), reader(_reader) {}
deba@1137
   501
deba@1137
   502
      virtual ~MapReaderInverter() {}
deba@1137
   503
deba@1137
   504
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   505
	Value value;
deba@1137
   506
	reader.read(is, value);
deba@1137
   507
	map.set(item, value);
deba@1137
   508
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   509
	if (it == inverse.end()) {
deba@1137
   510
	  inverse.insert(make_pair(value, item));
deba@1137
   511
	} else {
deba@1208
   512
	  throw DataFormatError("Multiple ID occurence");
deba@1137
   513
	}
deba@1137
   514
      }
deba@1137
   515
deba@1137
   516
      virtual Item read(std::istream& is) {
deba@1137
   517
	Value value;
deba@1137
   518
	reader.read(is, value);	
deba@1137
   519
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   520
	if (it != inverse.end()) {
deba@1137
   521
	  return it->second;
deba@1137
   522
	} else {
deba@1208
   523
	  throw DataFormatError("Invalid ID error");
deba@1137
   524
	}
deba@1137
   525
      }      
deba@1137
   526
    };
deba@1137
   527
deba@1137
   528
    template <typename _Item, typename _Reader>
deba@1137
   529
    class SkipReaderInverter : public InverterBase<_Item> {
deba@1137
   530
    public:
deba@1137
   531
      typedef _Item Item;
deba@1137
   532
      typedef _Reader Reader;
deba@1137
   533
      typedef typename Reader::Value Value;
deba@1137
   534
      typedef std::map<Value, Item> Inverse;
deba@1137
   535
deba@1137
   536
      Reader reader;
deba@1137
   537
deba@1137
   538
      SkipReaderInverter(const Reader& _reader) 
deba@1137
   539
	: reader(_reader) {}
deba@1137
   540
deba@1137
   541
      virtual ~SkipReaderInverter() {}
deba@1137
   542
deba@1137
   543
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   544
	Value value;
deba@1137
   545
	reader.read(is, value);
deba@1137
   546
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   547
	if (it == inverse.end()) {
deba@1137
   548
	  inverse.insert(make_pair(value, item));
deba@1137
   549
	} else {
deba@1208
   550
	  throw DataFormatError("Multiple ID occurence error");
deba@1137
   551
	}
deba@1137
   552
      }
deba@1137
   553
deba@1137
   554
      virtual Item read(std::istream& is) {
deba@1137
   555
	Value value;
deba@1137
   556
	reader.read(is, value);	
deba@1137
   557
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   558
	if (it != inverse.end()) {
deba@1137
   559
	  return it->second;
deba@1137
   560
	} else {
deba@1208
   561
	  throw DataFormatError("Invalid ID error");
deba@1137
   562
	}
deba@1137
   563
      }      
deba@1137
   564
    private:
deba@1137
   565
      Inverse inverse;
deba@1137
   566
    };
deba@1137
   567
deba@1137
   568
    // Readers
deba@1137
   569
deba@1137
   570
    template <typename _Item>    
deba@1137
   571
    class ReaderBase {
deba@1137
   572
    public:
deba@1137
   573
      typedef _Item Item;
deba@1137
   574
deba@1137
   575
      //      virtual ~ReaderBase() {}
deba@1137
   576
deba@1137
   577
      virtual void read(std::istream& is, const Item& item) = 0;
deba@1137
   578
      virtual InverterBase<_Item>* getInverter() = 0;
deba@1137
   579
    };
deba@1137
   580
deba@1137
   581
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   582
    class MapReader : public ReaderBase<_Item> {
deba@1137
   583
    public:
deba@1137
   584
      typedef _Map Map;
deba@1137
   585
      typedef _Reader Reader;
deba@1137
   586
      typedef typename Reader::Value Value;
deba@1137
   587
      typedef _Item Item;
deba@1137
   588
      
deba@1137
   589
      Map& map;
deba@1137
   590
      Reader reader;
deba@1137
   591
deba@1137
   592
      MapReader(Map& _map, const Reader& _reader) 
deba@1137
   593
	: map(_map), reader(_reader) {}
deba@1137
   594
deba@1137
   595
      virtual ~MapReader() {}
deba@1137
   596
deba@1137
   597
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   598
	Value value;
deba@1137
   599
	reader.read(is, value);
deba@1137
   600
	map.set(item, value);
deba@1137
   601
      }
deba@1137
   602
deba@1137
   603
      virtual InverterBase<_Item>* getInverter() {
deba@1137
   604
	return new MapReaderInverter<Item, Map, Reader>(map, reader);
deba@1137
   605
      }
deba@1137
   606
    };
deba@1137
   607
deba@1137
   608
deba@1137
   609
    template <typename _Item, typename _Reader>
deba@1137
   610
    class SkipReader : public ReaderBase<_Item> {
deba@1137
   611
    public:
deba@1137
   612
      typedef _Reader Reader;
deba@1137
   613
      typedef typename Reader::Value Value;
deba@1137
   614
      typedef _Item Item;
deba@1137
   615
deba@1137
   616
      Reader reader;
deba@1137
   617
      SkipReader(const Reader& _reader) : reader(_reader) {}
deba@1137
   618
deba@1137
   619
      virtual ~SkipReader() {}
deba@1137
   620
alpar@1250
   621
      virtual void read(std::istream& is, const Item&) {
deba@1137
   622
	Value value;
deba@1137
   623
	reader.read(is, value);
deba@1137
   624
      }      
deba@1137
   625
deba@1137
   626
      virtual InverterBase<Item>* getInverter() {
deba@1137
   627
	return new SkipReaderInverter<Item, Reader>(reader);
deba@1137
   628
      }
deba@1137
   629
    };
deba@1137
   630
deba@1137
   631
deba@1137
   632
    typedef std::map<std::string, ReaderBase<Node>*> NodeMapReaders;
deba@1137
   633
    NodeMapReaders node_map_readers;
deba@1137
   634
deba@1137
   635
    typedef std::map<std::string, ReaderBase<Edge>*> EdgeMapReaders;
deba@1137
   636
    EdgeMapReaders edge_map_readers;
deba@1137
   637
deba@1137
   638
    typedef std::map<std::string, Node*> NodeReaders;
deba@1137
   639
    NodeReaders node_readers;
deba@1137
   640
deba@1137
   641
    typedef std::map<std::string, Edge*> EdgeReaders;
deba@1137
   642
    EdgeReaders edge_readers;
deba@1137
   643
deba@1137
   644
    std::istream& is;
deba@1137
   645
    Graph& graph;
deba@1137
   646
deba@1137
   647
    SkipReader<Node, DefaultReader> nodeSkipper;
deba@1137
   648
    SkipReader<Edge, DefaultReader> edgeSkipper;
deba@1137
   649
deba@1137
   650
  };
deba@1137
   651
deba@1208
   652
  /// Ready to use reader function.  
deba@1208
   653
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   654
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   655
		  typename Graph::Node &s, typename Graph::Node &t, 
deba@1208
   656
		  CostMap& cost) {
deba@1208
   657
    GraphReader<Graph> reader(is, g);
deba@1208
   658
    reader.addEdgeMap("capacity", capacity);
deba@1208
   659
    reader.addEdgeMap("cost", cost);
deba@1208
   660
    reader.addNode("source", s);
deba@1208
   661
    reader.addNode("target", t);
deba@1208
   662
    reader.run();
deba@1208
   663
  }
deba@1208
   664
deba@1208
   665
  template<typename Graph, typename CapacityMap>
deba@1208
   666
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   667
		  typename Graph::Node &s, typename Graph::Node &t) {
deba@1208
   668
    GraphReader<Graph> reader(is, g);
deba@1208
   669
    reader.addEdgeMap("capacity", capacity);
deba@1208
   670
    reader.addNode("source", s);
deba@1208
   671
    reader.addNode("target", t);
deba@1208
   672
    reader.run();
deba@1208
   673
  }
deba@1208
   674
deba@1208
   675
  template<typename Graph, typename CapacityMap>
deba@1208
   676
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   677
		  typename Graph::Node &s) {
deba@1208
   678
    GraphReader<Graph> reader(is, g);
deba@1208
   679
    reader.addEdgeMap("capacity", capacity);
deba@1208
   680
    reader.addNode("source", s);
deba@1208
   681
    reader.run();
deba@1208
   682
  }
deba@1208
   683
deba@1208
   684
  template<typename Graph, typename CapacityMap>
deba@1208
   685
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1208
   686
    GraphReader<Graph> reader(is, g);
deba@1208
   687
    reader.addEdgeMap("capacity", capacity);
deba@1208
   688
    reader.run();
deba@1208
   689
  }
deba@1208
   690
deba@1208
   691
  template<typename Graph>
deba@1208
   692
  void readGraph(std::istream& is, Graph &g) {
deba@1208
   693
    GraphReader<Graph> reader(is, g);
deba@1208
   694
    reader.run();
deba@1208
   695
  }
deba@1208
   696
deba@1137
   697
}
deba@1214
   698
deba@1214
   699
#endif