src/lemon/graph_reader.h
author zsuzska
Fri, 25 Mar 2005 22:05:54 +0000
changeset 1265 47c60ae9c26a
parent 1214 39993ada11c7
child 1287 984723507b86
permissions -rw-r--r--
zsuzska's own work directory
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
deba@1137
    17
///\ingroup gio
deba@1137
    18
///\file
deba@1137
    19
///\brief Graph 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
deba@1137
    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
  ///
deba@1137
   174
  /// The reader class for the graph input.
alpar@1138
   175
  /// \see \ref GraphWriter
alpar@1138
   176
  /// \see \ref graph-io-page
deba@1137
   177
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1137
   178
  class GraphReader {
deba@1137
   179
  public:
deba@1137
   180
    
deba@1137
   181
    typedef _Graph Graph;
deba@1137
   182
    typedef typename Graph::Node Node;
deba@1137
   183
    typedef typename Graph::Edge Edge;
deba@1137
   184
deba@1137
   185
    typedef _ReaderTraits ReaderTraits;
deba@1137
   186
    typedef typename ReaderTraits::DefaultReader DefaultReader;
deba@1137
   187
deba@1137
   188
    /// \brief Construct a new GraphReader.
deba@1137
   189
    ///
deba@1208
   190
    /// Construct a new GraphReader. It reads into the given graph
deba@1208
   191
    /// and it use the given reader as the default skipper.
deba@1137
   192
    GraphReader(std::istream& _is, Graph& _graph, 
deba@1137
   193
		const DefaultReader& _reader = DefaultReader()) 
deba@1137
   194
      : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
deba@1137
   195
deba@1137
   196
    /// \brief Destruct the graph reader.
deba@1137
   197
    ///
deba@1137
   198
    /// Destruct the graph reader.
deba@1137
   199
    ~GraphReader() {
deba@1137
   200
deba@1137
   201
      for (typename NodeMapReaders::iterator it = node_map_readers.begin(); 
deba@1137
   202
	   it != node_map_readers.end(); ++it) {
deba@1137
   203
	delete it->second;
deba@1137
   204
      }
deba@1137
   205
deba@1137
   206
      for (typename EdgeMapReaders::iterator it = edge_map_readers.begin(); 
deba@1137
   207
	   it != edge_map_readers.end(); ++it) {
deba@1137
   208
	delete it->second;
deba@1137
   209
      }
deba@1137
   210
deba@1137
   211
    }
deba@1137
   212
deba@1137
   213
    /// \brief Add a new node map reader command for the reader.
deba@1137
   214
    ///
deba@1137
   215
    /// Add a new node map reader command for the reader.
deba@1137
   216
    template <typename Map>
deba@1137
   217
    GraphReader& addNodeMap(std::string name, Map& map) {
deba@1137
   218
      return addNodeMap<typename ReaderTraits::template 
deba@1137
   219
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   220
    }
deba@1137
   221
deba@1137
   222
    /// \brief Add a new node map reader command for the reader.
deba@1137
   223
    ///
deba@1137
   224
    /// Add a new node map reader command for the reader.
deba@1137
   225
    template <typename Reader, typename Map>
deba@1137
   226
    GraphReader& addNodeMap(std::string name, Map& map, 
deba@1137
   227
			     const Reader& reader = Reader()) {
deba@1137
   228
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1208
   229
	ErrorMessage msg;
deba@1208
   230
	msg << "Multiple read rule for node map: " << name;
deba@1214
   231
	throw IOParameterError(msg.message());
deba@1137
   232
      }
deba@1137
   233
      node_map_readers.insert(
deba@1137
   234
        make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
deba@1137
   235
      return *this;
deba@1137
   236
    }
deba@1137
   237
deba@1137
   238
    /// \brief Add a new node map skipper command for the reader.
deba@1137
   239
    ///
deba@1137
   240
    /// Add a new node map skipper command for the reader.
deba@1137
   241
    template <typename Reader>
deba@1137
   242
    GraphReader& skipNodeMap(std::string name, 
deba@1137
   243
			     const Reader& reader = Reader()) {
deba@1137
   244
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1208
   245
	ErrorMessage msg;
deba@1208
   246
	msg << "Multiple read rule for node map: " << name;
deba@1214
   247
	throw IOParameterError(msg.message());
deba@1137
   248
      }
deba@1137
   249
      node_map_readers.insert(
deba@1137
   250
        make_pair(name, new SkipReader<Node, Reader>(reader)));
deba@1137
   251
      return *this;
deba@1137
   252
    }
deba@1137
   253
deba@1137
   254
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   255
    ///
deba@1137
   256
    /// Add a new edge map reader command for the reader.
deba@1137
   257
    template <typename Map>
deba@1137
   258
    GraphReader& addEdgeMap(std::string name, Map& map) { 
deba@1137
   259
      return addEdgeMap<typename ReaderTraits::template
deba@1137
   260
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   261
    }
deba@1137
   262
deba@1137
   263
deba@1137
   264
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   265
    ///
deba@1137
   266
    /// Add a new edge map reader command for the reader.
deba@1137
   267
    template <typename Reader, typename Map>
deba@1137
   268
    GraphReader& addEdgeMap(std::string name, Map& map,
deba@1137
   269
			     const Reader& reader = Reader()) {
deba@1137
   270
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1208
   271
	ErrorMessage msg;
deba@1208
   272
	msg << "Multiple read rule for edge map: " << name;
deba@1214
   273
	throw IOParameterError(msg.message());
deba@1137
   274
      }
deba@1137
   275
      edge_map_readers.insert(
deba@1137
   276
        make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
deba@1137
   277
      return *this;
deba@1137
   278
    }
deba@1137
   279
deba@1137
   280
    /// \brief Add a new edge map skipper command for the reader.
deba@1137
   281
    ///
deba@1137
   282
    /// Add a new edge map skipper command for the reader.
deba@1137
   283
    template <typename Reader>
deba@1137
   284
    GraphReader& skipEdgeMap(std::string name,
deba@1137
   285
			     const Reader& reader = Reader()) {
deba@1137
   286
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1208
   287
	ErrorMessage msg;
deba@1208
   288
	msg << "Multiple read rule for edge map: " << name;
deba@1214
   289
	throw IOParameterError(msg.message());
deba@1137
   290
      }
deba@1137
   291
      edge_map_readers.insert(
deba@1137
   292
        make_pair(name, new SkipReader<Edge, Reader>(reader)));
deba@1137
   293
      return *this;
deba@1137
   294
    }
deba@1137
   295
deba@1137
   296
    /// \brief Add a new labeled node reader for the reader.
deba@1137
   297
    ///
deba@1137
   298
    /// Add a new labeled node reader for the reader.
deba@1137
   299
    GraphReader& addNode(std::string name, Node& node) {
deba@1137
   300
      if (node_readers.find(name) != node_readers.end()) {
deba@1208
   301
	ErrorMessage msg;
deba@1208
   302
	msg << "Multiple read rule for node: " << name;
deba@1214
   303
	throw IOParameterError(msg.message());
deba@1137
   304
      }
deba@1137
   305
      node_readers.insert(make_pair(name, &node));
deba@1137
   306
      return *this;
deba@1137
   307
    }
deba@1137
   308
deba@1137
   309
    /// \brief Add a new labeled edge reader for the reader.
deba@1137
   310
    ///
deba@1137
   311
    /// Add a new labeled edge reader for the reader.
deba@1137
   312
    GraphReader& addEdge(std::string name, Edge& edge) {
deba@1137
   313
      if (edge_readers.find(name) != edge_readers.end()) {
deba@1208
   314
	ErrorMessage msg;
deba@1208
   315
	msg << "Multiple read rule for edge: " << name;
deba@1214
   316
	throw IOParameterError(msg.message());
deba@1137
   317
      }
deba@1137
   318
      edge_readers.insert(make_pair(name, &edge));
deba@1137
   319
      return *this;
deba@1137
   320
    }
deba@1137
   321
deba@1137
   322
    /// \brief Executes the reader commands.
deba@1137
   323
    ///
deba@1137
   324
    /// Executes the reader commands.
deba@1137
   325
    void run() {
deba@1137
   326
      int line_num = 0;
deba@1137
   327
      std::auto_ptr<InverterBase<Node> > nodeInverter;
deba@1137
   328
      std::auto_ptr<InverterBase<Edge> > edgeInverter;
deba@1137
   329
      try {
deba@1137
   330
	std::string line = readNotEmptyLine(is, line_num);
deba@1137
   331
	if (line.find("@nodeset") == 0) {
deba@1137
   332
	  line = readNodeSet(line_num, nodeInverter);
deba@1137
   333
	} 
deba@1137
   334
	if (line.find("@edgeset") == 0) {
deba@1137
   335
	  line = readEdgeSet(line_num, edgeInverter, nodeInverter);
deba@1137
   336
	}
deba@1137
   337
	if (line.find("@nodes") == 0) {
deba@1137
   338
	  line = readNodes(line_num, nodeInverter);
deba@1137
   339
	}
deba@1137
   340
	if (line.find("@edges") == 0) {
deba@1137
   341
	  line = readEdges(line_num, edgeInverter);
deba@1137
   342
	}
deba@1137
   343
	if (line.find("@end") != 0) {
deba@1208
   344
	  throw DataFormatError("Invalid control sequence error");
deba@1137
   345
	}
deba@1208
   346
      } catch (DataFormatError e) {
deba@1208
   347
	e.line(line_num);
deba@1208
   348
	throw e;
deba@1137
   349
      }
deba@1137
   350
    }
deba@1137
   351
deba@1137
   352
  private:
deba@1137
   353
deba@1137
   354
    template <typename Item> class InverterBase;
deba@1137
   355
deba@1137
   356
    std::string readNodeSet(int& line_num, 
deba@1137
   357
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   358
      std::vector<ReaderBase<Node>* > index;
deba@1137
   359
      {
deba@1137
   360
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   361
	std::string id;
deba@1137
   362
	std::istringstream ls(line);	
deba@1137
   363
	while (ls >> id) {
deba@1137
   364
	  if (id[0] == '#') break;
deba@1137
   365
	  typename NodeMapReaders::iterator it = node_map_readers.find(id);
deba@1137
   366
	  if (it != node_map_readers.end()) {
deba@1137
   367
	    index.push_back(it->second);
deba@1137
   368
	    node_map_readers.erase(it);
deba@1137
   369
	  } else {
deba@1137
   370
	    index.push_back(&nodeSkipper);
deba@1137
   371
	  }
deba@1137
   372
	}
deba@1137
   373
      }
deba@1137
   374
deba@1137
   375
      if (index.size() == 0) {
deba@1208
   376
	throw DataFormatError("Cannot find node id map");
deba@1137
   377
      }
deba@1137
   378
deba@1137
   379
      nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
deba@1137
   380
      std::string line;
deba@1137
   381
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   382
	Node node = graph.addNode();
deba@1137
   383
	std::istringstream ls(line);
deba@1137
   384
	nodeInverter->read(ls, node);
deba@1137
   385
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   386
	  index[i]->read(ls, node);
deba@1137
   387
	}
deba@1137
   388
      }
deba@1137
   389
      return line;
deba@1137
   390
    }
deba@1137
   391
deba@1137
   392
    std::string readEdgeSet(int& line_num, 
deba@1137
   393
			    auto_ptr<InverterBase<Edge> > & edgeInverter, 
deba@1137
   394
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   395
      std::vector<ReaderBase<Edge>*> index;
deba@1137
   396
      {
deba@1137
   397
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   398
	std::string id;
deba@1137
   399
	std::istringstream ls(line);	
deba@1137
   400
	while (ls >> id) {
deba@1137
   401
	  if (id[0] == '#') break;
deba@1137
   402
	  typename EdgeMapReaders::iterator it = edge_map_readers.find(id);
deba@1137
   403
	  if (it != edge_map_readers.end()) {
deba@1137
   404
	    index.push_back(it->second);
deba@1137
   405
	    edge_map_readers.erase(it);
deba@1137
   406
	  } else {
deba@1137
   407
	    index.push_back(&edgeSkipper);
deba@1137
   408
	  }
deba@1137
   409
	}
deba@1137
   410
      }
deba@1137
   411
deba@1137
   412
      if (index.size() == 0) {
deba@1208
   413
	throw DataFormatError("Cannot find edge id map");
deba@1137
   414
      }
deba@1137
   415
deba@1137
   416
      edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
deba@1137
   417
      std::string line;
deba@1137
   418
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {	
deba@1137
   419
	std::istringstream ls(line);
deba@1137
   420
	Node source = nodeInverter->read(ls);
deba@1137
   421
	Node target = nodeInverter->read(ls);
deba@1137
   422
	Edge edge = graph.addEdge(source, target);
deba@1137
   423
	edgeInverter->read(ls, edge);
deba@1137
   424
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   425
	  index[i]->read(ls, edge);
deba@1137
   426
	}
deba@1137
   427
      }      
deba@1137
   428
      return line;
deba@1137
   429
    }
deba@1137
   430
deba@1137
   431
    std::string readNodes(int& line_num, 
deba@1137
   432
			  auto_ptr<InverterBase<Node> >& nodeInverter) {
deba@1137
   433
      std::string line;
deba@1137
   434
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   435
	std::istringstream ls(line);
deba@1137
   436
	std::string name;
deba@1137
   437
	ls >> name;
deba@1137
   438
	typename NodeReaders::iterator it = node_readers.find(name);
deba@1137
   439
	if (it != node_readers.end()) {
deba@1137
   440
	  *(it -> second) = nodeInverter->read(ls);
deba@1137
   441
	} 
deba@1137
   442
      }        
deba@1137
   443
      return line;
deba@1137
   444
    }
deba@1137
   445
deba@1137
   446
    std::string readEdges(int& line_num, 
deba@1137
   447
			  auto_ptr<InverterBase<Edge> >& edgeInverter) {
deba@1137
   448
      std::string line;
deba@1137
   449
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   450
	std::istringstream ls(line);
deba@1137
   451
	std::string name;
deba@1137
   452
	ls >> name;
deba@1137
   453
	typename EdgeReaders::iterator it = edge_readers.find(name);
deba@1137
   454
	if (it != edge_readers.end()) {
deba@1137
   455
	  *(it -> second) = edgeInverter->read(ls);
deba@1137
   456
	} 
deba@1137
   457
      }        
deba@1137
   458
      return line;    
deba@1137
   459
    }
deba@1137
   460
deba@1137
   461
    std::string readNotEmptyLine(std::istream& is, int& line_num) {
deba@1137
   462
      std::string line;
deba@1137
   463
      while (++line_num, getline(is, line)) {	
deba@1137
   464
	int vi = line.find_first_not_of(" \t");
deba@1137
   465
	if (vi != (int)string::npos && line[vi] != '#') {
deba@1137
   466
	  return line.substr(vi);
deba@1137
   467
	}
deba@1137
   468
      }
deba@1208
   469
      throw DataFormatError("End of stream error");
deba@1137
   470
    }
deba@1137
   471
    
deba@1137
   472
    // Inverters store and give back the Item from the id,
deba@1137
   473
    // and may put the ids into a map.
deba@1137
   474
    
deba@1137
   475
    template <typename _Item>
deba@1137
   476
    class InverterBase {
deba@1137
   477
    public:
deba@1137
   478
      typedef _Item Item;
deba@1137
   479
      virtual void read(std::istream&, const Item&) = 0;
deba@1137
   480
      virtual Item read(std::istream&) = 0;
deba@1137
   481
    };
deba@1137
   482
deba@1137
   483
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   484
    class MapReaderInverter : public InverterBase<_Item> {
deba@1137
   485
    public:
deba@1137
   486
      typedef _Item Item;
deba@1137
   487
      typedef _Reader Reader;
deba@1137
   488
      typedef typename Reader::Value Value;
deba@1137
   489
      typedef _Map Map;
deba@1137
   490
      typedef std::map<Value, Item> Inverse;
deba@1137
   491
deba@1137
   492
      Map& map;
deba@1137
   493
      Reader reader;
deba@1137
   494
      Inverse inverse;
deba@1137
   495
deba@1137
   496
      MapReaderInverter(Map& _map, const Reader& _reader) 
deba@1137
   497
	: map(_map), reader(_reader) {}
deba@1137
   498
deba@1137
   499
      virtual ~MapReaderInverter() {}
deba@1137
   500
deba@1137
   501
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   502
	Value value;
deba@1137
   503
	reader.read(is, value);
deba@1137
   504
	map.set(item, value);
deba@1137
   505
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   506
	if (it == inverse.end()) {
deba@1137
   507
	  inverse.insert(make_pair(value, item));
deba@1137
   508
	} else {
deba@1208
   509
	  throw DataFormatError("Multiple ID occurence");
deba@1137
   510
	}
deba@1137
   511
      }
deba@1137
   512
deba@1137
   513
      virtual Item read(std::istream& is) {
deba@1137
   514
	Value value;
deba@1137
   515
	reader.read(is, value);	
deba@1137
   516
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   517
	if (it != inverse.end()) {
deba@1137
   518
	  return it->second;
deba@1137
   519
	} else {
deba@1208
   520
	  throw DataFormatError("Invalid ID error");
deba@1137
   521
	}
deba@1137
   522
      }      
deba@1137
   523
    };
deba@1137
   524
deba@1137
   525
    template <typename _Item, typename _Reader>
deba@1137
   526
    class SkipReaderInverter : public InverterBase<_Item> {
deba@1137
   527
    public:
deba@1137
   528
      typedef _Item Item;
deba@1137
   529
      typedef _Reader Reader;
deba@1137
   530
      typedef typename Reader::Value Value;
deba@1137
   531
      typedef std::map<Value, Item> Inverse;
deba@1137
   532
deba@1137
   533
      Reader reader;
deba@1137
   534
deba@1137
   535
      SkipReaderInverter(const Reader& _reader) 
deba@1137
   536
	: reader(_reader) {}
deba@1137
   537
deba@1137
   538
      virtual ~SkipReaderInverter() {}
deba@1137
   539
deba@1137
   540
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   541
	Value value;
deba@1137
   542
	reader.read(is, value);
deba@1137
   543
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   544
	if (it == inverse.end()) {
deba@1137
   545
	  inverse.insert(make_pair(value, item));
deba@1137
   546
	} else {
deba@1208
   547
	  throw DataFormatError("Multiple ID occurence error");
deba@1137
   548
	}
deba@1137
   549
      }
deba@1137
   550
deba@1137
   551
      virtual Item read(std::istream& is) {
deba@1137
   552
	Value value;
deba@1137
   553
	reader.read(is, value);	
deba@1137
   554
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   555
	if (it != inverse.end()) {
deba@1137
   556
	  return it->second;
deba@1137
   557
	} else {
deba@1208
   558
	  throw DataFormatError("Invalid ID error");
deba@1137
   559
	}
deba@1137
   560
      }      
deba@1137
   561
    private:
deba@1137
   562
      Inverse inverse;
deba@1137
   563
    };
deba@1137
   564
deba@1137
   565
    // Readers
deba@1137
   566
deba@1137
   567
    template <typename _Item>    
deba@1137
   568
    class ReaderBase {
deba@1137
   569
    public:
deba@1137
   570
      typedef _Item Item;
deba@1137
   571
deba@1137
   572
      //      virtual ~ReaderBase() {}
deba@1137
   573
deba@1137
   574
      virtual void read(std::istream& is, const Item& item) = 0;
deba@1137
   575
      virtual InverterBase<_Item>* getInverter() = 0;
deba@1137
   576
    };
deba@1137
   577
deba@1137
   578
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   579
    class MapReader : public ReaderBase<_Item> {
deba@1137
   580
    public:
deba@1137
   581
      typedef _Map Map;
deba@1137
   582
      typedef _Reader Reader;
deba@1137
   583
      typedef typename Reader::Value Value;
deba@1137
   584
      typedef _Item Item;
deba@1137
   585
      
deba@1137
   586
      Map& map;
deba@1137
   587
      Reader reader;
deba@1137
   588
deba@1137
   589
      MapReader(Map& _map, const Reader& _reader) 
deba@1137
   590
	: map(_map), reader(_reader) {}
deba@1137
   591
deba@1137
   592
      virtual ~MapReader() {}
deba@1137
   593
deba@1137
   594
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   595
	Value value;
deba@1137
   596
	reader.read(is, value);
deba@1137
   597
	map.set(item, value);
deba@1137
   598
      }
deba@1137
   599
deba@1137
   600
      virtual InverterBase<_Item>* getInverter() {
deba@1137
   601
	return new MapReaderInverter<Item, Map, Reader>(map, reader);
deba@1137
   602
      }
deba@1137
   603
    };
deba@1137
   604
deba@1137
   605
deba@1137
   606
    template <typename _Item, typename _Reader>
deba@1137
   607
    class SkipReader : public ReaderBase<_Item> {
deba@1137
   608
    public:
deba@1137
   609
      typedef _Reader Reader;
deba@1137
   610
      typedef typename Reader::Value Value;
deba@1137
   611
      typedef _Item Item;
deba@1137
   612
deba@1137
   613
      Reader reader;
deba@1137
   614
      SkipReader(const Reader& _reader) : reader(_reader) {}
deba@1137
   615
deba@1137
   616
      virtual ~SkipReader() {}
deba@1137
   617
alpar@1250
   618
      virtual void read(std::istream& is, const Item&) {
deba@1137
   619
	Value value;
deba@1137
   620
	reader.read(is, value);
deba@1137
   621
      }      
deba@1137
   622
deba@1137
   623
      virtual InverterBase<Item>* getInverter() {
deba@1137
   624
	return new SkipReaderInverter<Item, Reader>(reader);
deba@1137
   625
      }
deba@1137
   626
    };
deba@1137
   627
deba@1137
   628
deba@1137
   629
    typedef std::map<std::string, ReaderBase<Node>*> NodeMapReaders;
deba@1137
   630
    NodeMapReaders node_map_readers;
deba@1137
   631
deba@1137
   632
    typedef std::map<std::string, ReaderBase<Edge>*> EdgeMapReaders;
deba@1137
   633
    EdgeMapReaders edge_map_readers;
deba@1137
   634
deba@1137
   635
    typedef std::map<std::string, Node*> NodeReaders;
deba@1137
   636
    NodeReaders node_readers;
deba@1137
   637
deba@1137
   638
    typedef std::map<std::string, Edge*> EdgeReaders;
deba@1137
   639
    EdgeReaders edge_readers;
deba@1137
   640
deba@1137
   641
    std::istream& is;
deba@1137
   642
    Graph& graph;
deba@1137
   643
deba@1137
   644
    SkipReader<Node, DefaultReader> nodeSkipper;
deba@1137
   645
    SkipReader<Edge, DefaultReader> edgeSkipper;
deba@1137
   646
deba@1137
   647
  };
deba@1137
   648
deba@1208
   649
  /// Ready to use reader function.  
deba@1208
   650
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   651
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   652
		  typename Graph::Node &s, typename Graph::Node &t, 
deba@1208
   653
		  CostMap& cost) {
deba@1208
   654
    GraphReader<Graph> reader(is, g);
deba@1208
   655
    reader.addEdgeMap("capacity", capacity);
deba@1208
   656
    reader.addEdgeMap("cost", cost);
deba@1208
   657
    reader.addNode("source", s);
deba@1208
   658
    reader.addNode("target", t);
deba@1208
   659
    reader.run();
deba@1208
   660
  }
deba@1208
   661
deba@1208
   662
  template<typename Graph, typename CapacityMap>
deba@1208
   663
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   664
		  typename Graph::Node &s, typename Graph::Node &t) {
deba@1208
   665
    GraphReader<Graph> reader(is, g);
deba@1208
   666
    reader.addEdgeMap("capacity", capacity);
deba@1208
   667
    reader.addNode("source", s);
deba@1208
   668
    reader.addNode("target", t);
deba@1208
   669
    reader.run();
deba@1208
   670
  }
deba@1208
   671
deba@1208
   672
  template<typename Graph, typename CapacityMap>
deba@1208
   673
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   674
		  typename Graph::Node &s) {
deba@1208
   675
    GraphReader<Graph> reader(is, g);
deba@1208
   676
    reader.addEdgeMap("capacity", capacity);
deba@1208
   677
    reader.addNode("source", s);
deba@1208
   678
    reader.run();
deba@1208
   679
  }
deba@1208
   680
deba@1208
   681
  template<typename Graph, typename CapacityMap>
deba@1208
   682
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1208
   683
    GraphReader<Graph> reader(is, g);
deba@1208
   684
    reader.addEdgeMap("capacity", capacity);
deba@1208
   685
    reader.run();
deba@1208
   686
  }
deba@1208
   687
deba@1208
   688
  template<typename Graph>
deba@1208
   689
  void readGraph(std::istream& is, Graph &g) {
deba@1208
   690
    GraphReader<Graph> reader(is, g);
deba@1208
   691
    reader.run();
deba@1208
   692
  }
deba@1208
   693
deba@1137
   694
}
deba@1214
   695
deba@1214
   696
#endif