lemon/lemon_reader.h
author deba
Fri, 14 Oct 2005 10:49:51 +0000
changeset 1720 578d8b2b76c6
parent 1627 3fd1ba6e9872
child 1722 2acb5f9bfa72
permissions -rw-r--r--
Matrixmaps moved to own file
deba@1408
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/lemon_reader.h - Part of LEMON, a generic C++ optimization library
deba@1408
     3
 *
deba@1408
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1408
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1408
     6
 *
deba@1408
     7
 * Permission to use, modify and distribute this software is granted
deba@1408
     8
 * provided that this copyright notice appears in all copies. For
deba@1408
     9
 * precise terms see the accompanying LICENSE file.
deba@1408
    10
 *
deba@1408
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1408
    12
 * express or implied, and with no claim as to its suitability for any
deba@1408
    13
 * purpose.
deba@1408
    14
 *
deba@1408
    15
 */
deba@1408
    16
deba@1408
    17
///\ingroup io_group
deba@1408
    18
///\file
deba@1408
    19
///\brief Lemon Format reader.
deba@1408
    20
deba@1421
    21
deba@1408
    22
#ifndef LEMON_LEMON_READER_H
deba@1408
    23
#define LEMON_LEMON_READER_H
deba@1408
    24
deba@1421
    25
deba@1408
    26
#include <iostream>
deba@1408
    27
#include <fstream>
deba@1408
    28
#include <string>
deba@1408
    29
#include <vector>
deba@1408
    30
#include <algorithm>
deba@1408
    31
#include <map>
deba@1408
    32
#include <memory>
deba@1408
    33
deba@1408
    34
#include <lemon/error.h>
deba@1421
    35
#include <lemon/graph_utils.h>
deba@1421
    36
#include <lemon/utility.h>
deba@1409
    37
#include <lemon/bits/item_reader.h>
deba@1408
    38
deba@1705
    39
#include <lemon/xy.h>
deba@1705
    40
deba@1476
    41
#include <lemon/concept_check.h>
deba@1476
    42
#include <lemon/concept/maps.h>
deba@1408
    43
deba@1408
    44
namespace lemon {
deba@1408
    45
deba@1421
    46
  namespace _reader_bits {
deba@1476
    47
deba@1476
    48
    template <typename Item>
deba@1476
    49
    class ItemIdReader {
deba@1476
    50
    public:
deba@1476
    51
deba@1476
    52
      bool isIdReader() { return true; }
deba@1476
    53
deba@1492
    54
      void readId(std::istream&, Item&) {}
deba@1476
    55
      
deba@1476
    56
      template <class _ItemIdReader>
deba@1476
    57
      struct Constraints {
deba@1476
    58
	void constraints() {
deba@1476
    59
	  bool b = reader.isIdReader();
deba@1476
    60
	  ignore_unused_variable_warning(b);
deba@1492
    61
	  Item item;
deba@1492
    62
	  reader.readId(is, item);
deba@1476
    63
	}
deba@1476
    64
	_ItemIdReader& reader;
deba@1476
    65
	std::istream& is;
deba@1476
    66
      };
deba@1476
    67
deba@1476
    68
    };
deba@1492
    69
deba@1492
    70
    template <typename Item>
deba@1492
    71
    class ItemReader {
deba@1492
    72
    public:
deba@1492
    73
      void read(std::istream&, Item&) {}
deba@1492
    74
      
deba@1492
    75
      template <class _ItemReader>
deba@1492
    76
      struct Constraints {
deba@1492
    77
	void constraints() {
deba@1492
    78
	  Item item;
deba@1492
    79
	  reader.read(is, item);
deba@1492
    80
	}
deba@1492
    81
	_ItemReader& reader;
deba@1492
    82
	std::istream& is;
deba@1492
    83
      };
deba@1492
    84
deba@1492
    85
    };
deba@1421
    86
  
deba@1421
    87
    template <typename T>
deba@1421
    88
    bool operator<(T, T) {
deba@1421
    89
      throw DataFormatError("Id is not comparable");
deba@1421
    90
    }
deba@1421
    91
deba@1421
    92
    template <typename T>
deba@1421
    93
    struct Less {
deba@1421
    94
      bool operator()(const T& p, const T& q) const {
deba@1421
    95
	return p < q;
deba@1421
    96
      }
deba@1421
    97
    };
deba@1421
    98
deba@1705
    99
    template <typename Map>
deba@1705
   100
    struct Ref { typedef Map& Type; };
deba@1705
   101
    template <typename Map>
deba@1705
   102
    struct Arg { typedef Map& Type; };
deba@1705
   103
deba@1705
   104
    template <typename Graph, typename Map>
deba@1705
   105
    class ForwardComposeMap {
deba@1421
   106
    public:
deba@1705
   107
      typedef typename Graph::UndirEdge Key;
deba@1705
   108
      typedef typename Map::Value Value;
deba@1705
   109
deba@1705
   110
      ForwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
deba@1705
   111
	: graph(_graph), map(_map) {}
deba@1421
   112
      
deba@1705
   113
      void set(const Key& key, const Value& val) {
deba@1705
   114
	map.set(graph.direct(key, true), val);
deba@1421
   115
      }
deba@1421
   116
deba@1421
   117
    private:
deba@1705
   118
      typename Ref<Map>::Type map;
deba@1705
   119
      const Graph& graph;
deba@1421
   120
    };
deba@1421
   121
deba@1705
   122
    template <typename Graph, typename Map>
deba@1705
   123
    ForwardComposeMap<Graph, Map>
deba@1705
   124
    forwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   125
      return ForwardComposeMap<Graph, Map>(graph, map);
deba@1421
   126
    }
deba@1421
   127
deba@1705
   128
    template <typename Graph, typename Map>
deba@1705
   129
    ForwardComposeMap<Graph, Map>
deba@1705
   130
    forwardComposeMap(const Graph& graph, Map& map) {
deba@1705
   131
      return ForwardComposeMap<Graph, Map>(graph, map);
deba@1421
   132
    }
deba@1705
   133
deba@1705
   134
    template <typename Graph, typename Map>
deba@1705
   135
    class BackwardComposeMap {
deba@1705
   136
    public:
deba@1705
   137
      typedef typename Graph::UndirEdge Key;
deba@1705
   138
      typedef typename Map::Value Value;
deba@1705
   139
deba@1705
   140
      BackwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
deba@1705
   141
	: graph(_graph), map(_map) {}
deba@1705
   142
      
deba@1705
   143
      void set(const Key& key, const Value& val) {
deba@1705
   144
	map.set(graph.direct(key, false), val);
deba@1705
   145
      }
deba@1705
   146
deba@1705
   147
    private:
deba@1705
   148
      typename Ref<Map>::Type map;
deba@1705
   149
      const Graph& graph;
deba@1705
   150
    };
deba@1705
   151
deba@1705
   152
deba@1705
   153
    template <typename Graph, typename Map>
deba@1705
   154
    BackwardComposeMap<Graph, Map>
deba@1705
   155
    backwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   156
      return BackwardComposeMap<Graph, Map>(graph, map);
deba@1705
   157
    }
deba@1705
   158
deba@1705
   159
    template <typename Graph, typename Map>
deba@1705
   160
    BackwardComposeMap<Graph, Map>
deba@1705
   161
    backwardComposeMap(const Graph& graph, Map& map) {
deba@1705
   162
      return BackwardComposeMap<Graph, Map>(graph, map);
deba@1705
   163
    }
deba@1705
   164
deba@1705
   165
    template <typename Graph, typename Map>
deba@1705
   166
    struct Ref<ForwardComposeMap<Graph, Map> > { 
deba@1705
   167
      typedef ForwardComposeMap<Graph, Map> Type;
deba@1705
   168
    };
deba@1705
   169
    template <typename Graph, typename Map>
deba@1705
   170
    struct Arg<ForwardComposeMap<Graph, Map> > { 
deba@1705
   171
      typedef const ForwardComposeMap<Graph, Map>& Type;
deba@1705
   172
    };
deba@1705
   173
deba@1705
   174
    template <typename Graph, typename Map>
deba@1705
   175
    struct Ref<BackwardComposeMap<Graph, Map> > { 
deba@1705
   176
      typedef BackwardComposeMap<Graph, Map> Type; 
deba@1705
   177
    };
deba@1705
   178
    template <typename Graph, typename Map>
deba@1705
   179
    struct Arg<BackwardComposeMap<Graph, Map> > { 
deba@1705
   180
      typedef const BackwardComposeMap<Graph, Map>& Type; 
deba@1705
   181
    };
deba@1705
   182
deba@1705
   183
    template <typename Map>
deba@1705
   184
    struct Ref<XMap<Map> > { 
deba@1705
   185
      typedef XMap<Map> Type;
deba@1705
   186
    };
deba@1705
   187
    template <typename Map>
deba@1705
   188
    struct Arg<XMap<Map> > { 
deba@1705
   189
      typedef const XMap<Map>& Type;
deba@1705
   190
    };
deba@1705
   191
deba@1705
   192
    template <typename Map>
deba@1705
   193
    struct Ref<YMap<Map> > { 
deba@1705
   194
      typedef YMap<Map> Type;
deba@1705
   195
    };
deba@1705
   196
    template <typename Map>
deba@1705
   197
    struct Arg<YMap<Map> > { 
deba@1705
   198
      typedef const YMap<Map>& Type;
deba@1705
   199
    };
deba@1705
   200
deba@1421
   201
  }
deba@1421
   202
deba@1409
   203
  /// \ingroup io_group
deba@1408
   204
  /// \brief Lemon Format reader class.
deba@1408
   205
  /// 
deba@1409
   206
  /// The Lemon Format contains several sections. We do not want to
deba@1409
   207
  /// determine what sections are in a lemon file we give only a framework
deba@1409
   208
  /// to read a section oriented format.
deba@1409
   209
  ///
deba@1409
   210
  /// In the Lemon Format each section starts with a line contains a \c \@
deba@1409
   211
  /// character on the first not white space position. This line is the
deba@1409
   212
  /// header line of the section. Each next lines belong to this section
deba@1409
   213
  /// while it does not starts with \c \@ character. This line can start a 
deba@1409
   214
  /// new section or if it can close the file with the \c \@end line.
deba@1427
   215
  /// The file format ignore the empty and comment lines. The line is
deba@1427
   216
  /// comment line if it starts with a \c # character. 
deba@1409
   217
  ///
deba@1409
   218
  /// The framework provides an abstract LemonReader::SectionReader class
deba@1409
   219
  /// what defines the interface of a SectionReader. The SectionReader
deba@1409
   220
  /// has the \c header() member function what get a header line string and
deba@1409
   221
  /// decides if it want to process the next section. Several SectionReaders
deba@1409
   222
  /// can be attached to an LemonReader and the first attached what can
deba@1409
   223
  /// process the section will be used. Its \c read() member will called
deba@1427
   224
  /// with a stream contains the section. From this stream the empty and
deba@1427
   225
  /// comment lines are filtered out.
deba@1409
   226
  ///
deba@1409
   227
  /// \relates GraphReader
deba@1409
   228
  /// \relates NodeSetReader
deba@1409
   229
  /// \relates EdgeSetReader
deba@1409
   230
  /// \relates NodesReader
deba@1409
   231
  /// \relates EdgesReader
deba@1409
   232
  /// \relates AttributeReader
deba@1408
   233
  class LemonReader {
deba@1408
   234
  private:
deba@1408
   235
    
deba@1408
   236
    class FilterStreamBuf : public std::streambuf {
deba@1408
   237
    public:
deba@1408
   238
deba@1408
   239
      typedef std::streambuf Parent;
deba@1408
   240
      typedef Parent::char_type char_type;
deba@1408
   241
      FilterStreamBuf(std::istream& is, int& num) 
deba@1408
   242
	: _is(is), _base(0), _eptr(0), 
deba@1408
   243
	  _num(num), skip_state(after_endl) {}
deba@1408
   244
deba@1408
   245
    protected:
deba@1408
   246
deba@1408
   247
      enum skip_state_type {
deba@1408
   248
	no_skip,
deba@1408
   249
	after_endl,
deba@1427
   250
	comment_line
deba@1408
   251
      };
deba@1408
   252
deba@1408
   253
      char_type small_buf[1];
deba@1408
   254
deba@1408
   255
deba@1408
   256
      std::istream& _is;
deba@1408
   257
deba@1408
   258
      char_type* _base;
deba@1408
   259
      char_type* _eptr;
deba@1408
   260
deba@1408
   261
      int& _num;
deba@1408
   262
deba@1408
   263
      skip_state_type skip_state;
deba@1408
   264
deba@1408
   265
deba@1408
   266
      char_type* base() { return _base; }
deba@1408
   267
deba@1408
   268
      char_type* eptr() { return _eptr; }
deba@1408
   269
deba@1408
   270
      int blen() { return _eptr - _base; }
deba@1408
   271
deba@1408
   272
      void setb(char_type* buf, int len) {
deba@1408
   273
	_base = buf;
deba@1408
   274
	_eptr = buf + len;
deba@1408
   275
      }
deba@1408
   276
  
deba@1408
   277
      virtual std::streambuf* setbuf(char *buf, int len) {
deba@1408
   278
	if (base()) return 0;
deba@1408
   279
	if (buf != 0 && len >= (int)sizeof(small_buf)) {
deba@1408
   280
	  setb(buf, len);
deba@1408
   281
	} else {
deba@1408
   282
	  setb(small_buf, sizeof(small_buf));
deba@1408
   283
	}
deba@1408
   284
	setg(0, 0, 0);
deba@1408
   285
	return this;
deba@1408
   286
      }
deba@1408
   287
deba@1408
   288
      bool put_char(char c) {
deba@1408
   289
	switch (skip_state) {
deba@1408
   290
	case no_skip:
deba@1408
   291
	  switch (c) {
deba@1408
   292
	  case '\n': 
deba@1408
   293
	    skip_state = after_endl;
deba@1408
   294
	    return true;
deba@1408
   295
	  default:
deba@1408
   296
	    return true;
deba@1408
   297
	  }
deba@1408
   298
	case after_endl:
deba@1408
   299
	  switch (c) {
deba@1408
   300
	  case '@':
deba@1408
   301
	    return false;
deba@1408
   302
	  case '\n': 
deba@1408
   303
	    return false;
deba@1408
   304
	  case '#':
deba@1427
   305
	    skip_state = comment_line;
deba@1408
   306
	    return false;
deba@1408
   307
	  default:
deba@1408
   308
	    if (!isspace(c)) {
deba@1408
   309
	      skip_state = no_skip;
deba@1408
   310
	      return true;
deba@1408
   311
	    } else {
deba@1408
   312
	      return false;
deba@1408
   313
	    }
deba@1408
   314
	  }
deba@1408
   315
	  break;
deba@1427
   316
	case comment_line:
deba@1408
   317
	  switch (c) {
deba@1408
   318
	  case '\n': 
deba@1408
   319
	    skip_state = after_endl;
deba@1408
   320
	    return false;
deba@1408
   321
	  default:
deba@1408
   322
	    return false;
deba@1408
   323
	  }
deba@1408
   324
	}
deba@1408
   325
	return false;
deba@1408
   326
      }
deba@1408
   327
deba@1408
   328
      virtual int underflow() {
deba@1408
   329
	char c;
deba@1408
   330
	if (_is.read(&c, 1)) {
deba@1408
   331
	  _is.putback(c);
deba@1408
   332
	  if (c == '@') {
deba@1408
   333
	    return EOF;
deba@1408
   334
	  }
deba@1408
   335
	} else {
deba@1408
   336
	  return EOF;
deba@1408
   337
	}
deba@1408
   338
	char_type *ptr;
deba@1408
   339
	for (ptr = base(); ptr != eptr(); ++ptr) {
deba@1408
   340
	  if (_is.read(&c, 1)) {
deba@1408
   341
	    if (c == '\n') ++_num;
deba@1408
   342
	    if (put_char(c)) {
deba@1408
   343
	      *ptr = c;
deba@1408
   344
	    } else {
deba@1408
   345
	      if (skip_state == after_endl && c == '@') {
deba@1408
   346
		_is.putback('@');
deba@1408
   347
		break;
deba@1408
   348
	      }
deba@1408
   349
	      --ptr;
deba@1408
   350
	    }
deba@1408
   351
	  } else {
deba@1408
   352
	    break;
deba@1408
   353
	  }
deba@1408
   354
	}
deba@1408
   355
	setg(base(), base(), ptr);
deba@1408
   356
	return *base();
deba@1408
   357
      }
deba@1408
   358
deba@1408
   359
      virtual int sync() {
deba@1408
   360
	return EOF;
deba@1408
   361
      }
deba@1408
   362
    };
deba@1408
   363
deba@1408
   364
  public:
deba@1408
   365
deba@1409
   366
    /// \brief Abstract base class for reading a section.
deba@1409
   367
    ///
deba@1409
   368
    /// This class has an \c header() member function what get a 
deba@1409
   369
    /// header line string and decides if it want to process the next 
deba@1409
   370
    /// section. Several SectionReaders can be attached to an LemonReader 
deba@1409
   371
    /// and the first attached what can process the section will be used. 
deba@1409
   372
    /// Its \c read() member will called with a stream contains the section. 
deba@1409
   373
    /// From this stream the empty lines and comments are filtered out.
deba@1408
   374
    class SectionReader {
deba@1409
   375
      friend class LemonReader;
deba@1409
   376
    protected:
deba@1409
   377
      /// \brief Constructor for SectionReader.
deba@1409
   378
      ///
deba@1409
   379
      /// Constructor for SectionReader. It attach this reader to
deba@1409
   380
      /// the given LemonReader.
deba@1409
   381
      SectionReader(LemonReader& reader) {
deba@1409
   382
	reader.attach(*this);
deba@1409
   383
      }
deba@1409
   384
alpar@1494
   385
      virtual ~SectionReader() {}
alpar@1494
   386
deba@1409
   387
      /// \brief Gives back true when the SectionReader can process 
deba@1409
   388
      /// the section with the given header line.
deba@1409
   389
      ///
deba@1409
   390
      /// It gives back true when the SectionReader can process
deba@1409
   391
      /// the section with the given header line.
deba@1408
   392
      virtual bool header(const std::string& line) = 0;
deba@1409
   393
deba@1409
   394
      /// \brief Reader function of the section.
deba@1409
   395
      ///
deba@1409
   396
      /// It reads the content of the section.
deba@1408
   397
      virtual void read(std::istream& is) = 0;
deba@1408
   398
    };
deba@1408
   399
deba@1409
   400
    /// \brief Constructor for LemonReader.
deba@1409
   401
    ///
deba@1409
   402
    /// Constructor for LemonReader which reads from the given stream.
deba@1408
   403
    LemonReader(std::istream& _is) 
deba@1408
   404
      : is(&_is), own_is(false) {}
deba@1408
   405
deba@1409
   406
    /// \brief Constructor for LemonReader.
deba@1409
   407
    ///
deba@1409
   408
    /// Constructor for LemonReader which reads from the given file.
deba@1408
   409
    LemonReader(const std::string& filename) 
deba@1408
   410
      : is(0), own_is(true) {
deba@1408
   411
      is = new std::ifstream(filename.c_str());
deba@1408
   412
    }
deba@1408
   413
deba@1409
   414
    /// \brief Desctructor for LemonReader.
deba@1409
   415
    ///
deba@1409
   416
    /// Desctructor for LemonReader.
deba@1408
   417
    ~LemonReader() {
deba@1408
   418
      if (own_is) {
deba@1408
   419
	delete is;
deba@1408
   420
      }
deba@1408
   421
    }
deba@1408
   422
deba@1408
   423
  private:
deba@1408
   424
    LemonReader(const LemonReader&);
deba@1408
   425
    void operator=(const LemonReader&);
deba@1408
   426
deba@1408
   427
    void attach(SectionReader& reader) {
deba@1408
   428
      readers.push_back(&reader);
deba@1408
   429
    }
deba@1408
   430
deba@1409
   431
  public:
deba@1409
   432
    /// \brief Executes the LemonReader.
deba@1409
   433
    /// 
deba@1409
   434
    /// It executes the LemonReader.
deba@1408
   435
    void run() {
deba@1408
   436
      int line_num = 0;
deba@1408
   437
      std::string line;
deba@1408
   438
      try {
deba@1408
   439
	while ((++line_num, getline(*is, line)) && line.find("@end") != 0) {
deba@1408
   440
	  SectionReaders::iterator it;
deba@1408
   441
	  for (it = readers.begin(); it != readers.end(); ++it) {
deba@1408
   442
	    if ((*it)->header(line)) {
deba@1408
   443
	      char buf[2048];
deba@1408
   444
	      FilterStreamBuf buffer(*is, line_num);
deba@1408
   445
	      buffer.pubsetbuf(buf, sizeof(buf));
deba@1408
   446
	      std::istream is(&buffer);
deba@1408
   447
	      (*it)->read(is);
deba@1408
   448
	      break;
deba@1408
   449
	    }
deba@1408
   450
	  }
deba@1408
   451
	}
deba@1408
   452
      } catch (DataFormatError& error) {
deba@1408
   453
	error.line(line_num);
deba@1408
   454
	throw error;
deba@1408
   455
      }	
deba@1408
   456
    }
deba@1408
   457
deba@1408
   458
deba@1408
   459
  private:
deba@1408
   460
deba@1408
   461
    std::istream* is;
deba@1408
   462
    bool own_is;
deba@1408
   463
deba@1408
   464
    typedef std::vector<SectionReader*> SectionReaders;
deba@1408
   465
    SectionReaders readers;
deba@1408
   466
deba@1408
   467
  };
deba@1408
   468
deba@1409
   469
  /// \brief Helper class for implementing the common SectionReaders.
deba@1409
   470
  ///
deba@1409
   471
  /// Helper class for implementing the common SectionReaders.
deba@1409
   472
  class CommonSectionReaderBase : public LemonReader::SectionReader {
deba@1409
   473
    typedef LemonReader::SectionReader Parent;
deba@1409
   474
  protected:
deba@1409
   475
    
deba@1409
   476
    /// \brief Constructor for CommonSectionReaderBase.
deba@1409
   477
    ///
deba@1409
   478
    /// Constructor for CommonSectionReaderBase. It attach this reader to
deba@1409
   479
    /// the given LemonReader.
deba@1409
   480
    CommonSectionReaderBase(LemonReader& _reader) 
deba@1409
   481
      : Parent(_reader) {}
deba@1408
   482
deba@1408
   483
    template <typename _Item>
deba@1408
   484
    class ReaderBase;
deba@1408
   485
    
deba@1408
   486
    template <typename _Item>
deba@1408
   487
    class InverterBase : public ReaderBase<_Item> {
deba@1408
   488
    public:
deba@1408
   489
      typedef _Item Item;
deba@1408
   490
      virtual void read(std::istream&, const Item&) = 0;
deba@1408
   491
      virtual Item read(std::istream&) const = 0;
deba@1408
   492
deba@1408
   493
      virtual InverterBase<_Item>* getInverter() {
deba@1408
   494
	return this;
deba@1408
   495
      }
deba@1408
   496
    };
deba@1408
   497
deba@1408
   498
    template <typename _Item, typename _Map, typename _Reader>
deba@1408
   499
    class MapReaderInverter : public InverterBase<_Item> {
deba@1408
   500
    public:
deba@1408
   501
      typedef _Item Item;
deba@1408
   502
      typedef _Reader Reader;
deba@1408
   503
      typedef typename Reader::Value Value;
deba@1408
   504
      typedef _Map Map;
deba@1424
   505
      typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
deba@1408
   506
deba@1705
   507
      typename _reader_bits::Ref<Map>::Type map;
deba@1408
   508
      Reader reader;
deba@1408
   509
      Inverse inverse;
deba@1408
   510
deba@1705
   511
      MapReaderInverter(typename _reader_bits::Arg<Map>::Type _map,
deba@1421
   512
			const Reader& _reader) 
deba@1408
   513
	: map(_map), reader(_reader) {}
deba@1408
   514
deba@1408
   515
      virtual ~MapReaderInverter() {}
deba@1408
   516
deba@1408
   517
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   518
	Value value;
deba@1408
   519
	reader.read(is, value);
deba@1408
   520
	map.set(item, value);
deba@1408
   521
	typename Inverse::iterator it = inverse.find(value);
deba@1408
   522
	if (it == inverse.end()) {
deba@1408
   523
	  inverse.insert(std::make_pair(value, item));
deba@1408
   524
	} else {
deba@1408
   525
	  throw DataFormatError("Multiple ID occurence");
deba@1408
   526
	}
deba@1408
   527
      }
deba@1408
   528
deba@1408
   529
      virtual Item read(std::istream& is) const {
deba@1408
   530
	Value value;
deba@1408
   531
	reader.read(is, value);	
deba@1408
   532
	typename Inverse::const_iterator it = inverse.find(value);
deba@1408
   533
	if (it != inverse.end()) {
deba@1408
   534
	  return it->second;
deba@1408
   535
	} else {
deba@1408
   536
	  throw DataFormatError("Invalid ID error");
deba@1408
   537
	}
deba@1408
   538
      }      
deba@1408
   539
    };
deba@1408
   540
deba@1408
   541
    template <typename _Item, typename _Reader>
deba@1408
   542
    class SkipReaderInverter : public InverterBase<_Item> {
deba@1408
   543
    public:
deba@1408
   544
      typedef _Item Item;
deba@1408
   545
      typedef _Reader Reader;
deba@1408
   546
      typedef typename Reader::Value Value;
deba@1424
   547
      typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
deba@1408
   548
deba@1408
   549
      Reader reader;
deba@1408
   550
deba@1408
   551
      SkipReaderInverter(const Reader& _reader) 
deba@1408
   552
	: reader(_reader) {}
deba@1408
   553
deba@1408
   554
      virtual ~SkipReaderInverter() {}
deba@1408
   555
deba@1408
   556
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   557
	Value value;
deba@1408
   558
	reader.read(is, value);
deba@1408
   559
	typename Inverse::iterator it = inverse.find(value);
deba@1408
   560
	if (it == inverse.end()) {
deba@1408
   561
	  inverse.insert(std::make_pair(value, item));
deba@1408
   562
	} else {
deba@1408
   563
	  throw DataFormatError("Multiple ID occurence error");
deba@1408
   564
	}
deba@1408
   565
      }
deba@1408
   566
deba@1408
   567
      virtual Item read(std::istream& is) const {
deba@1408
   568
	Value value;
deba@1408
   569
	reader.read(is, value);	
deba@1408
   570
	typename Inverse::const_iterator it = inverse.find(value);
deba@1408
   571
	if (it != inverse.end()) {
deba@1408
   572
	  return it->second;
deba@1408
   573
	} else {
deba@1408
   574
	  throw DataFormatError("Invalid ID error");
deba@1408
   575
	}
deba@1408
   576
      }
deba@1408
   577
deba@1408
   578
    private:
deba@1408
   579
      Inverse inverse;
deba@1408
   580
    };
deba@1408
   581
deba@1408
   582
    template <typename _Item>    
deba@1408
   583
    class ReaderBase {
deba@1408
   584
    public:
deba@1408
   585
      typedef _Item Item;
deba@1408
   586
deba@1408
   587
      virtual ~ReaderBase() {}
deba@1408
   588
deba@1408
   589
      virtual void read(std::istream& is, const Item& item) = 0;
deba@1408
   590
      virtual InverterBase<_Item>* getInverter() = 0;
deba@1408
   591
    };
deba@1408
   592
deba@1408
   593
    template <typename _Item, typename _Map, typename _Reader>
deba@1408
   594
    class MapReader : public ReaderBase<_Item> {
deba@1408
   595
    public:
deba@1408
   596
      typedef _Map Map;
deba@1408
   597
      typedef _Reader Reader;
deba@1408
   598
      typedef typename Reader::Value Value;
deba@1408
   599
      typedef _Item Item;
deba@1408
   600
      
deba@1705
   601
      typename _reader_bits::Ref<Map>::Type map;
deba@1408
   602
      Reader reader;
deba@1408
   603
deba@1705
   604
      MapReader(typename _reader_bits::Arg<Map>::Type _map, 
deba@1421
   605
		const Reader& _reader) 
deba@1408
   606
	: map(_map), reader(_reader) {}
deba@1408
   607
deba@1408
   608
      virtual ~MapReader() {}
deba@1408
   609
deba@1408
   610
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   611
	Value value;
deba@1408
   612
	reader.read(is, value);
deba@1408
   613
	map.set(item, value);
deba@1408
   614
      }
deba@1408
   615
deba@1408
   616
      virtual InverterBase<_Item>* getInverter() {
deba@1408
   617
	return new MapReaderInverter<Item, Map, Reader>(map, reader);
deba@1408
   618
      }
deba@1408
   619
    };
deba@1408
   620
deba@1408
   621
deba@1408
   622
    template <typename _Item, typename _Reader>
deba@1408
   623
    class SkipReader : public ReaderBase<_Item> {
deba@1408
   624
    public:
deba@1408
   625
      typedef _Reader Reader;
deba@1408
   626
      typedef typename Reader::Value Value;
deba@1408
   627
      typedef _Item Item;
deba@1408
   628
deba@1408
   629
      Reader reader;
deba@1408
   630
      SkipReader(const Reader& _reader) : reader(_reader) {}
deba@1408
   631
deba@1408
   632
      virtual ~SkipReader() {}
deba@1408
   633
deba@1408
   634
      virtual void read(std::istream& is, const Item&) {
deba@1408
   635
	Value value;
deba@1408
   636
	reader.read(is, value);
deba@1408
   637
      }      
deba@1408
   638
deba@1408
   639
      virtual InverterBase<Item>* getInverter() {
deba@1408
   640
	return new SkipReaderInverter<Item, Reader>(reader);
deba@1408
   641
      }
deba@1408
   642
    };
deba@1408
   643
deba@1408
   644
    template <typename _Item>
deba@1409
   645
    class IdReaderBase {
deba@1408
   646
    public:
deba@1408
   647
      typedef _Item Item;
alpar@1494
   648
      virtual ~IdReaderBase() {}
deba@1409
   649
      virtual Item read(std::istream& is) const = 0;
deba@1476
   650
      virtual bool isIdReader() const = 0;
deba@1408
   651
    };
deba@1408
   652
deba@1409
   653
    template <typename _Item, typename _BoxedIdReader>
deba@1409
   654
    class IdReader : public IdReaderBase<_Item> {
deba@1408
   655
    public:
deba@1408
   656
      typedef _Item Item;
deba@1409
   657
      typedef _BoxedIdReader BoxedIdReader;
deba@1409
   658
      
deba@1409
   659
      const BoxedIdReader& boxedIdReader;
deba@1408
   660
deba@1409
   661
      IdReader(const BoxedIdReader& _boxedIdReader) 
deba@1409
   662
	: boxedIdReader(_boxedIdReader) {}
deba@1408
   663
deba@1409
   664
      virtual Item read(std::istream& is) const {
deba@1492
   665
	Item item;
deba@1492
   666
	boxedIdReader.readId(is, item);
deba@1492
   667
	return item;
deba@1408
   668
      }
deba@1476
   669
deba@1476
   670
      virtual bool isIdReader() const {
deba@1476
   671
	return boxedIdReader.isIdReader();
deba@1476
   672
      }
deba@1408
   673
    };
deba@1408
   674
deba@1408
   675
    class ValueReaderBase {
deba@1408
   676
    public:
deba@1408
   677
      virtual void read(std::istream&) {};
alpar@1494
   678
      virtual ~ValueReaderBase() {}
deba@1408
   679
    };
deba@1408
   680
deba@1408
   681
    template <typename _Value, typename _Reader>
deba@1408
   682
    class ValueReader : public ValueReaderBase {
deba@1408
   683
    public:
deba@1408
   684
      typedef _Value Value;
deba@1408
   685
      typedef _Reader Reader;
deba@1408
   686
deba@1408
   687
      ValueReader(Value& _value, const Reader& _reader)
deba@1408
   688
 	: value(_value), reader(_reader) {}
deba@1408
   689
deba@1408
   690
      virtual void read(std::istream& is) {
deba@1408
   691
	reader.read(is, value);
deba@1408
   692
      }
deba@1408
   693
    private:
deba@1408
   694
      Value& value;
deba@1408
   695
      Reader reader;
deba@1408
   696
    };
deba@1408
   697
    
deba@1408
   698
  };
deba@1408
   699
deba@1409
   700
  /// \ingroup io_group
deba@1409
   701
  /// \brief SectionReader for reading a graph's nodeset.
deba@1409
   702
  ///
deba@1409
   703
  /// The lemon format can store multiple graph nodesets with several maps.
deba@1409
   704
  /// The nodeset section's header line is \c \@nodeset \c nodeset_id, but the
deba@1409
   705
  /// \c nodeset_id may be empty.
deba@1409
   706
  ///
deba@1409
   707
  /// The first line of the section contains the names of the maps separated
deba@1409
   708
  /// with white spaces. Each next lines describes a node in the nodeset, and
deba@1409
   709
  /// contains the mapped values for each map.
deba@1409
   710
  ///
deba@1409
   711
  /// If the nodeset contains an \c "id" named map then it will be regarded
deba@1409
   712
  /// as id map. This map should contain only unique values and when the 
deba@1409
   713
  /// \c readId() member will read a value from the given stream it will
deba@1409
   714
  /// give back that node which is mapped to this value.
deba@1409
   715
  ///
deba@1409
   716
  /// \relates LemonReader
deba@1408
   717
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1408
   718
  class NodeSetReader : public CommonSectionReaderBase {
deba@1408
   719
    typedef CommonSectionReaderBase Parent;
deba@1408
   720
  public:
deba@1408
   721
deba@1408
   722
    typedef _Graph Graph;
deba@1408
   723
    typedef _Traits Traits;
deba@1429
   724
    typedef typename Graph::Node Node;
deba@1408
   725
    typedef typename Traits::Skipper DefaultSkipper;
deba@1408
   726
deba@1409
   727
    /// \brief Constructor.
deba@1409
   728
    ///
deba@1409
   729
    /// Constructor for NodeSetReader. It creates the NodeSetReader and
deba@1409
   730
    /// attach it into the given LemonReader. The nodeset reader will
deba@1409
   731
    /// add the readed nodes to the given Graph. The reader will read
deba@1409
   732
    /// the section when the \c section_id and the \c _id are the same. 
deba@1421
   733
    NodeSetReader(LemonReader& _reader, 
deba@1705
   734
		  Graph& _graph, 
deba@1408
   735
		  const std::string& _id = std::string(),
deba@1409
   736
		  const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1409
   737
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {} 
deba@1408
   738
deba@1409
   739
deba@1409
   740
    /// \brief Destructor.
deba@1409
   741
    ///
deba@1409
   742
    /// Destructor for NodeSetReader.
deba@1408
   743
    virtual ~NodeSetReader() {
deba@1408
   744
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1408
   745
	   it != readers.end(); ++it) {
deba@1408
   746
	delete it->second;
deba@1408
   747
      }
deba@1408
   748
    }
deba@1408
   749
deba@1408
   750
  private:
deba@1408
   751
    NodeSetReader(const NodeSetReader&);
deba@1408
   752
    void operator=(const NodeSetReader&);
deba@1408
   753
  
deba@1408
   754
  public:
deba@1408
   755
deba@1408
   756
    /// \brief Add a new node map reader command for the reader.
deba@1408
   757
    ///
deba@1408
   758
    /// Add a new node map reader command for the reader.
deba@1408
   759
    template <typename Map>
deba@1421
   760
    NodeSetReader& readNodeMap(std::string name, Map& map) {
deba@1421
   761
      return _readMap<
deba@1421
   762
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
   763
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
   764
    }
deba@1421
   765
deba@1421
   766
    template <typename Map>
deba@1421
   767
    NodeSetReader& readNodeMap(std::string name, const Map& map) {
deba@1421
   768
      return _readMap<
deba@1421
   769
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
   770
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1408
   771
    }
deba@1408
   772
deba@1408
   773
    /// \brief Add a new node map reader command for the reader.
deba@1408
   774
    ///
deba@1408
   775
    /// Add a new node map reader command for the reader.
deba@1408
   776
    template <typename Reader, typename Map>
deba@1421
   777
    NodeSetReader& readNodeMap(std::string name, Map& map, 
deba@1421
   778
			       const Reader& reader = Reader()) {
deba@1705
   779
      return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
deba@1429
   780
	(name, map, reader);
deba@1421
   781
    }
deba@1421
   782
deba@1421
   783
    template <typename Reader, typename Map>
deba@1421
   784
    NodeSetReader& readNodeMap(std::string name, const Map& map, 
deba@1421
   785
			       const Reader& reader = Reader()) {
deba@1705
   786
      return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
deba@1429
   787
	(name, map, reader);
deba@1421
   788
    }
deba@1421
   789
deba@1421
   790
  private:
deba@1421
   791
deba@1421
   792
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
   793
    NodeSetReader& _readMap(std::string name, MapParameter map, 
deba@1421
   794
			    const Reader& reader = Reader()) {
deba@1492
   795
      checkConcept<concept::WriteMap<Node, typename Map::Value>, Map>();
deba@1492
   796
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
deba@1408
   797
      if (readers.find(name) != readers.end()) {
deba@1408
   798
	ErrorMessage msg;
deba@1408
   799
	msg << "Multiple read rule for node map: " << name;
deba@1408
   800
	throw IOParameterError(msg.message());
deba@1408
   801
      }
deba@1408
   802
      readers.insert(
deba@1429
   803
	make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
deba@1408
   804
      return *this;
deba@1408
   805
    }
deba@1408
   806
deba@1421
   807
  public:
deba@1421
   808
deba@1408
   809
    /// \brief Add a new node map skipper command for the reader.
deba@1408
   810
    ///
deba@1408
   811
    /// Add a new node map skipper command for the reader.
deba@1408
   812
    template <typename Reader>
deba@1421
   813
    NodeSetReader& skipNodeMap(std::string name, 
deba@1408
   814
			   const Reader& reader = Reader()) {
deba@1408
   815
      if (readers.find(name) != readers.end()) {
deba@1408
   816
	ErrorMessage msg;
deba@1408
   817
	msg << "Multiple read rule for node map: " << name;
deba@1408
   818
	throw IOParameterError(msg.message());
deba@1408
   819
      }
deba@1429
   820
      readers.insert(make_pair(name, new SkipReader<Node, Reader>(reader)));
deba@1408
   821
      return *this;
deba@1408
   822
    }
deba@1408
   823
deba@1409
   824
  protected:
deba@1409
   825
deba@1409
   826
    /// \brief Gives back true when the SectionReader can process 
deba@1409
   827
    /// the section with the given header line.
deba@1409
   828
    ///
deba@1421
   829
    /// It gives back true when the header line starts with \c \@nodeset,
deba@1409
   830
    /// and the header line's id and the nodeset's id are the same.
deba@1408
   831
    virtual bool header(const std::string& line) {
deba@1408
   832
      std::istringstream ls(line);
deba@1408
   833
      std::string command;
deba@1408
   834
      std::string name;
deba@1408
   835
      ls >> command >> name;
deba@1408
   836
      return command == "@nodeset" && name == id;
deba@1408
   837
    }
deba@1408
   838
deba@1409
   839
    /// \brief Reader function of the section.
deba@1409
   840
    ///
deba@1409
   841
    /// It reads the content of the section.
deba@1408
   842
    virtual void read(std::istream& is) {
deba@1429
   843
      std::vector<ReaderBase<Node>* > index;
deba@1408
   844
      std::string line;
deba@1408
   845
deba@1408
   846
      getline(is, line);
deba@1408
   847
      std::istringstream ls(line);	
deba@1408
   848
      while (ls >> id) {
deba@1408
   849
	typename MapReaders::iterator it = readers.find(id);
deba@1408
   850
	if (it != readers.end()) {
deba@1408
   851
	  index.push_back(it->second);
deba@1408
   852
	} else {
deba@1408
   853
	  index.push_back(&skipper);
deba@1408
   854
	}
deba@1408
   855
	if (id == "id" && inverter.get() == 0) {
deba@1408
   856
	  inverter.reset(index.back()->getInverter());
deba@1408
   857
	  index.back() = inverter.get();
deba@1408
   858
	}
deba@1408
   859
      }
deba@1408
   860
      while (getline(is, line)) {	
deba@1429
   861
	Node node = graph.addNode();
deba@1408
   862
	std::istringstream ls(line);
deba@1408
   863
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1408
   864
	  index[i]->read(ls, node);
deba@1408
   865
	}
deba@1408
   866
      }
deba@1408
   867
    }
deba@1408
   868
deba@1409
   869
  public:
deba@1409
   870
deba@1409
   871
    /// \brief Returns true if the nodeset can give back the node by its id.
deba@1409
   872
    ///
deba@1409
   873
    /// Returns true if the nodeset can give back the node by its id.
deba@1409
   874
    /// It is possible only if an "id" named map was read.
deba@1409
   875
    bool isIdReader() const {
deba@1408
   876
      return inverter.get() != 0;
deba@1408
   877
    }
deba@1408
   878
deba@1409
   879
    /// \brief Gives back the node by its id.
deba@1409
   880
    ///
deba@1409
   881
    /// It reads an id from the stream and gives back which node belongs to
deba@1409
   882
    /// it. It is possible only if there was read an "id" named map.
deba@1492
   883
    void readId(std::istream& is, Node& node) const {
deba@1492
   884
      node = inverter->read(is);
deba@1408
   885
    } 
deba@1408
   886
deba@1408
   887
  private:
deba@1408
   888
deba@1429
   889
    typedef std::map<std::string, ReaderBase<Node>*> MapReaders;
deba@1408
   890
    MapReaders readers;
deba@1408
   891
   
deba@1705
   892
    Graph& graph;   
deba@1408
   893
    std::string id;
deba@1429
   894
    SkipReader<Node, DefaultSkipper> skipper;
deba@1408
   895
deba@1429
   896
    std::auto_ptr<InverterBase<Node> > inverter;
deba@1408
   897
  };
deba@1408
   898
deba@1409
   899
  /// \ingroup io_group
deba@1409
   900
  /// \brief SectionReader for reading a graph's edgeset.
deba@1409
   901
  ///
deba@1409
   902
  /// The lemon format can store multiple graph edgesets with several maps.
deba@1409
   903
  /// The edgeset section's header line is \c \@edgeset \c edgeset_id, but the
deba@1409
   904
  /// \c edgeset_id may be empty.
deba@1409
   905
  ///
deba@1409
   906
  /// The first line of the section contains the names of the maps separated
deba@1421
   907
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
   908
  /// line contains the source and the target nodes' id and the mapped 
deba@1421
   909
  /// values for each map.
deba@1409
   910
  ///
deba@1409
   911
  /// If the edgeset contains an \c "id" named map then it will be regarded
deba@1409
   912
  /// as id map. This map should contain only unique values and when the 
deba@1409
   913
  /// \c readId() member will read a value from the given stream it will
deba@1409
   914
  /// give back that edge which is mapped to this value.
deba@1409
   915
  ///
deba@1409
   916
  /// The edgeset reader needs a node id reader to identify which nodes
deba@1409
   917
  /// have to be connected. If a NodeSetReader reads an "id" named map,
deba@1409
   918
  /// it will be able to resolve the nodes by ids.
deba@1409
   919
  ///
deba@1409
   920
  /// \relates LemonReader
deba@1408
   921
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1408
   922
  class EdgeSetReader : public CommonSectionReaderBase {
deba@1408
   923
    typedef CommonSectionReaderBase Parent;
deba@1408
   924
  public:
deba@1408
   925
deba@1408
   926
    typedef _Graph Graph;
deba@1408
   927
    typedef _Traits Traits;
deba@1429
   928
    typedef typename Graph::Node Node;
deba@1429
   929
    typedef typename Graph::Edge Edge;
deba@1408
   930
    typedef typename Traits::Skipper DefaultSkipper;
deba@1408
   931
deba@1409
   932
    /// \brief Constructor.
deba@1409
   933
    ///
deba@1409
   934
    /// Constructor for EdgeSetReader. It creates the EdgeSetReader and
deba@1409
   935
    /// attach it into the given LemonReader. The edgeset reader will
deba@1409
   936
    /// add the readed edges to the given Graph. It will use the given
deba@1409
   937
    /// node id reader to read the source and target nodes of the edges.
deba@1409
   938
    /// The reader will read the section only if the \c _id and the 
deba@1409
   939
    /// \c edgset_id are the same. 
deba@1409
   940
    template <typename NodeIdReader>
deba@1421
   941
    EdgeSetReader(LemonReader& _reader, 
deba@1705
   942
		  Graph& _graph, 
deba@1409
   943
		  const NodeIdReader& _nodeIdReader, 
deba@1408
   944
		  const std::string& _id = std::string(),
deba@1409
   945
		  const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1476
   946
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
deba@1476
   947
      checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
deba@1476
   948
      nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
deba@1476
   949
    }
deba@1409
   950
    /// \brief Destructor.
deba@1409
   951
    ///
deba@1409
   952
    /// Destructor for EdgeSetReader.
deba@1408
   953
    virtual ~EdgeSetReader() {
deba@1408
   954
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1408
   955
	   it != readers.end(); ++it) {
deba@1408
   956
	delete it->second;
deba@1408
   957
      }
deba@1408
   958
    }
deba@1408
   959
deba@1408
   960
  private:
deba@1408
   961
    EdgeSetReader(const EdgeSetReader&);
deba@1408
   962
    void operator=(const EdgeSetReader&);
deba@1408
   963
deba@1408
   964
  public:
deba@1408
   965
deba@1409
   966
    /// \brief Add a new edge map reader command for the reader.
deba@1408
   967
    ///
deba@1409
   968
    /// Add a new edge map reader command for the reader.
deba@1408
   969
    template <typename Map>
deba@1421
   970
    EdgeSetReader& readEdgeMap(std::string name, Map& map) {
deba@1421
   971
      return _readMap<
deba@1421
   972
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
   973
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
   974
    }
deba@1421
   975
deba@1421
   976
    template <typename Map>
deba@1421
   977
    EdgeSetReader& readEdgeMap(std::string name, const Map& map) {
deba@1421
   978
      return _readMap<
deba@1421
   979
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
   980
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1408
   981
    }
deba@1408
   982
deba@1409
   983
    /// \brief Add a new edge map reader command for the reader.
deba@1408
   984
    ///
deba@1409
   985
    /// Add a new edge map reader command for the reader.
deba@1408
   986
    template <typename Reader, typename Map>
deba@1421
   987
    EdgeSetReader& readEdgeMap(std::string name, Map& map, 
deba@1421
   988
			   const Reader& reader = Reader()) {
deba@1429
   989
      return _readMap<Reader, Map,
deba@1705
   990
	typename _reader_bits::Arg<Map>::Type>(name, map, reader);
deba@1421
   991
    }
deba@1421
   992
deba@1421
   993
    template <typename Reader, typename Map>
deba@1421
   994
    EdgeSetReader& readEdgeMap(std::string name, const Map& map, 
deba@1421
   995
			       const Reader& reader = Reader()) {
deba@1429
   996
      return _readMap<Reader, Map,
deba@1705
   997
	typename _reader_bits::Arg<Map>::Type>(name, map, reader);
deba@1421
   998
    }
deba@1421
   999
deba@1421
  1000
  private:
deba@1421
  1001
deba@1421
  1002
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
  1003
    EdgeSetReader& _readMap(std::string name, MapParameter map, 
deba@1421
  1004
			    const Reader& reader = Reader()) {
deba@1492
  1005
      checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
deba@1492
  1006
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
deba@1408
  1007
      if (readers.find(name) != readers.end()) {
deba@1408
  1008
	ErrorMessage msg;
deba@1408
  1009
	msg << "Multiple read rule for edge map: " << name;
deba@1408
  1010
	throw IOParameterError(msg.message());
deba@1408
  1011
      }
deba@1408
  1012
      readers.insert(
deba@1429
  1013
	make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
deba@1408
  1014
      return *this;
deba@1408
  1015
    }
deba@1408
  1016
deba@1421
  1017
  public:
deba@1421
  1018
deba@1409
  1019
    /// \brief Add a new edge map skipper command for the reader.
deba@1408
  1020
    ///
deba@1409
  1021
    /// Add a new edge map skipper command for the reader.
deba@1408
  1022
    template <typename Reader>
deba@1421
  1023
    EdgeSetReader& skipEdgeMap(std::string name, 
deba@1421
  1024
			       const Reader& reader = Reader()) {
deba@1408
  1025
      if (readers.find(name) != readers.end()) {
deba@1408
  1026
	ErrorMessage msg;
deba@1421
  1027
	msg << "Multiple read rule for edge map: " << name;
deba@1408
  1028
	throw IOParameterError(msg.message());
deba@1408
  1029
      }
deba@1429
  1030
      readers.insert(make_pair(name, new SkipReader<Edge, Reader>(reader)));
deba@1408
  1031
      return *this;
deba@1408
  1032
    }
deba@1408
  1033
deba@1409
  1034
  protected:
deba@1409
  1035
deba@1409
  1036
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1037
    /// the section with the given header line.
deba@1409
  1038
    ///
deba@1421
  1039
    /// It gives back true when the header line starts with \c \@edgeset,
deba@1409
  1040
    /// and the header line's id and the edgeset's id are the same.
deba@1408
  1041
    virtual bool header(const std::string& line) {
deba@1408
  1042
      std::istringstream ls(line);
deba@1408
  1043
      std::string command;
deba@1408
  1044
      std::string name;
deba@1408
  1045
      ls >> command >> name;
deba@1408
  1046
      return command == "@edgeset" && name == id;
deba@1408
  1047
    }
deba@1408
  1048
deba@1409
  1049
    /// \brief Reader function of the section.
deba@1409
  1050
    ///
deba@1409
  1051
    /// It reads the content of the section.
deba@1408
  1052
    virtual void read(std::istream& is) {
deba@1476
  1053
      if (!nodeIdReader->isIdReader()) {
deba@1476
  1054
	throw DataFormatError("Cannot find nodeset or ID map");
deba@1476
  1055
      }
deba@1429
  1056
      std::vector<ReaderBase<Edge>* > index;
deba@1408
  1057
      std::string line;
deba@1408
  1058
deba@1408
  1059
      getline(is, line);
deba@1408
  1060
      std::istringstream ls(line);	
deba@1408
  1061
      while (ls >> id) {
deba@1408
  1062
	typename MapReaders::iterator it = readers.find(id);
deba@1408
  1063
	if (it != readers.end()) {
deba@1408
  1064
	  index.push_back(it->second);
deba@1408
  1065
	} else {
deba@1408
  1066
	  index.push_back(&skipper);
deba@1408
  1067
	}
deba@1408
  1068
	if (id == "id" && inverter.get() == 0) {
deba@1408
  1069
	  inverter.reset(index.back()->getInverter());
deba@1408
  1070
	  index.back() = inverter.get();
deba@1408
  1071
	}
deba@1408
  1072
      }
deba@1408
  1073
      while (getline(is, line)) {	
deba@1408
  1074
	std::istringstream ls(line);
deba@1429
  1075
	Node from = nodeIdReader->read(ls);
deba@1429
  1076
	Node to = nodeIdReader->read(ls);
deba@1429
  1077
	Edge edge = graph.addEdge(from, to);
deba@1408
  1078
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1408
  1079
	  index[i]->read(ls, edge);
deba@1408
  1080
	}
deba@1408
  1081
      }
deba@1408
  1082
    }
deba@1408
  1083
deba@1409
  1084
  public:
deba@1409
  1085
deba@1409
  1086
    /// \brief Returns true if the edgeset can give back the edge by its id.
deba@1409
  1087
    ///
deba@1409
  1088
    /// Returns true if the edgeset can give back the edge by its id.
deba@1409
  1089
    /// It is possible only if an "id" named map was read.
deba@1409
  1090
    bool isIdReader() const {
deba@1408
  1091
      return inverter.get() != 0;
deba@1408
  1092
    }
deba@1408
  1093
deba@1409
  1094
    /// \brief Gives back the edge by its id.
deba@1409
  1095
    ///
deba@1409
  1096
    /// It reads an id from the stream and gives back which edge belongs to
deba@1409
  1097
    /// it. It is possible only if there was read an "id" named map.
deba@1492
  1098
    void readId(std::istream& is, Edge& edge) const {
deba@1492
  1099
      edge = inverter->read(is);
deba@1408
  1100
    } 
deba@1408
  1101
deba@1408
  1102
  private:
deba@1408
  1103
deba@1429
  1104
    typedef std::map<std::string, ReaderBase<Edge>*> MapReaders;
deba@1408
  1105
    MapReaders readers;
deba@1408
  1106
   
deba@1705
  1107
    Graph& graph;   
deba@1421
  1108
    std::string id;
deba@1429
  1109
    SkipReader<Edge, DefaultSkipper> skipper;
deba@1421
  1110
deba@1429
  1111
    std::auto_ptr<InverterBase<Edge> > inverter;
deba@1429
  1112
    std::auto_ptr<IdReaderBase<Node> > nodeIdReader;
deba@1421
  1113
  };
deba@1421
  1114
deba@1421
  1115
  /// \ingroup io_group
deba@1421
  1116
  /// \brief SectionReader for reading a undirected graph's edgeset.
deba@1421
  1117
  ///
deba@1421
  1118
  /// The lemon format can store multiple undirected edgesets with several 
deba@1421
  1119
  /// maps. The undirected edgeset section's header line is \c \@undiredgeset 
deba@1421
  1120
  /// \c undiredgeset_id, but the \c undiredgeset_id may be empty.
deba@1421
  1121
  ///
deba@1421
  1122
  /// The first line of the section contains the names of the maps separated
deba@1421
  1123
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
  1124
  /// line contains the connected nodes' id and the mapped values for each map.
deba@1421
  1125
  ///
deba@1421
  1126
  /// The section can handle the directed as a syntactical sugar. Two
deba@1421
  1127
  /// undirected edge map describes one directed edge map. This two maps
deba@1421
  1128
  /// are the forward map and the backward map and the names of this map
deba@1421
  1129
  /// is near the same just with a prefix \c '+' or \c '-' character 
deba@1421
  1130
  /// difference.
deba@1421
  1131
  ///
deba@1421
  1132
  /// If the edgeset contains an \c "id" named map then it will be regarded
deba@1421
  1133
  /// as id map. This map should contain only unique values and when the 
deba@1421
  1134
  /// \c readId() member will read a value from the given stream it will
deba@1421
  1135
  /// give back that undiricted edge which is mapped to this value.
deba@1421
  1136
  ///
deba@1421
  1137
  /// The undirected edgeset reader needs a node id reader to identify which 
deba@1421
  1138
  /// nodes have to be connected. If a NodeSetReader reads an "id" named map,
deba@1421
  1139
  /// it will be able to resolve the nodes by ids.
deba@1421
  1140
  ///
deba@1421
  1141
  /// \relates LemonReader
deba@1421
  1142
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1421
  1143
  class UndirEdgeSetReader : public CommonSectionReaderBase {
deba@1421
  1144
    typedef CommonSectionReaderBase Parent;
deba@1421
  1145
  public:
deba@1421
  1146
deba@1421
  1147
    typedef _Graph Graph;
deba@1421
  1148
    typedef _Traits Traits;
deba@1429
  1149
    typedef typename Graph::Node Node;
deba@1429
  1150
    typedef typename Graph::Edge Edge;
deba@1429
  1151
    typedef typename Graph::UndirEdge UndirEdge;
deba@1421
  1152
    typedef typename Traits::Skipper DefaultSkipper;
deba@1421
  1153
deba@1421
  1154
    /// \brief Constructor.
deba@1421
  1155
    ///
deba@1421
  1156
    /// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader 
deba@1421
  1157
    /// and attach it into the given LemonReader. The undirected edgeset 
deba@1421
  1158
    /// reader will add the readed undirected edges to the given Graph. It 
deba@1421
  1159
    /// will use the given node id reader to read the source and target 
deba@1421
  1160
    /// nodes of the edges. The reader will read the section only if the 
deba@1421
  1161
    /// \c _id and the \c undiredgset_id are the same. 
deba@1421
  1162
    template <typename NodeIdReader>
deba@1421
  1163
    UndirEdgeSetReader(LemonReader& _reader, 
deba@1705
  1164
		       Graph& _graph, 
deba@1421
  1165
		       const NodeIdReader& _nodeIdReader, 
deba@1421
  1166
		       const std::string& _id = std::string(),
deba@1421
  1167
		       const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1476
  1168
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
deba@1476
  1169
      checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
deba@1476
  1170
      nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
deba@1476
  1171
    }
deba@1421
  1172
    /// \brief Destructor.
deba@1421
  1173
    ///
deba@1421
  1174
    /// Destructor for UndirEdgeSetReader.
deba@1421
  1175
    virtual ~UndirEdgeSetReader() {
deba@1421
  1176
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1421
  1177
	   it != readers.end(); ++it) {
deba@1421
  1178
	delete it->second;
deba@1421
  1179
      }
deba@1421
  1180
    }
deba@1421
  1181
deba@1421
  1182
  private:
deba@1421
  1183
    UndirEdgeSetReader(const UndirEdgeSetReader&);
deba@1421
  1184
    void operator=(const UndirEdgeSetReader&);
deba@1421
  1185
deba@1421
  1186
  public:
deba@1421
  1187
deba@1421
  1188
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1189
    ///
deba@1421
  1190
    /// Add a new edge undirected map reader command for the reader.
deba@1421
  1191
    template <typename Map>
deba@1421
  1192
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) {
deba@1421
  1193
      return _readMap<
deba@1421
  1194
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@1705
  1195
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
  1196
    }
deba@1421
  1197
deba@1421
  1198
    template <typename Map>
deba@1421
  1199
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) {
deba@1421
  1200
      return _readMap<
deba@1421
  1201
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@1705
  1202
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
  1203
    }
deba@1421
  1204
deba@1421
  1205
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1206
    ///
deba@1421
  1207
    /// Add a new edge undirected map reader command for the reader.
deba@1421
  1208
    template <typename Reader, typename Map>
deba@1421
  1209
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map, 
deba@1421
  1210
					 const Reader& reader = Reader()) {
deba@1705
  1211
      return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
deba@1421
  1212
	(name, map, reader);
deba@1421
  1213
    }
deba@1421
  1214
deba@1421
  1215
    template <typename Reader, typename Map>
deba@1421
  1216
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map, 
deba@1421
  1217
					 const Reader& reader = Reader()) {
deba@1705
  1218
      return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type >
deba@1421
  1219
	(name, map, reader);
deba@1421
  1220
    }
deba@1421
  1221
deba@1421
  1222
  private:
deba@1421
  1223
deba@1421
  1224
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
  1225
    UndirEdgeSetReader& _readMap(std::string name, MapParameter map,
deba@1421
  1226
				 const Reader& reader = Reader()) {
deba@1492
  1227
      checkConcept<concept::WriteMap<UndirEdge, typename Map::Value>, Map>();
deba@1492
  1228
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
deba@1421
  1229
      if (readers.find(name) != readers.end()) {
deba@1421
  1230
	ErrorMessage msg;
deba@1421
  1231
	msg << "Multiple read rule for edge map: " << name;
deba@1421
  1232
	throw IOParameterError(msg.message());
deba@1421
  1233
      }
deba@1421
  1234
      readers.insert(
deba@1429
  1235
	make_pair(name, new MapReader<UndirEdge, Map, Reader>(map, reader)));
deba@1421
  1236
      return *this;
deba@1421
  1237
    }
deba@1421
  1238
deba@1421
  1239
  public:
deba@1421
  1240
deba@1421
  1241
    /// \brief Add a new undirected edge map skipper command for the reader.
deba@1421
  1242
    ///
deba@1421
  1243
    /// Add a new undirected edge map skipper command for the reader.
deba@1421
  1244
    template <typename Reader>
deba@1421
  1245
    UndirEdgeSetReader& skipUndirEdgeMap(std::string name, 
deba@1421
  1246
					 const Reader& reader = Reader()) {
deba@1421
  1247
      if (readers.find(name) != readers.end()) {
deba@1421
  1248
	ErrorMessage msg;
deba@1421
  1249
	msg << "Multiple read rule for node map: " << name;
deba@1421
  1250
	throw IOParameterError(msg.message());
deba@1421
  1251
      }
deba@1429
  1252
      readers.insert(make_pair(name, 
deba@1429
  1253
			       new SkipReader<UndirEdge, Reader>(reader)));
deba@1421
  1254
      return *this;
deba@1421
  1255
    }
deba@1421
  1256
deba@1421
  1257
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1258
    ///
deba@1421
  1259
    /// Add a new directed edge map reader command for the reader.
deba@1421
  1260
    template <typename Map>
deba@1421
  1261
    UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) {
deba@1421
  1262
      return _readDirMap<
deba@1421
  1263
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
  1264
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
  1265
    }
deba@1421
  1266
deba@1421
  1267
    template <typename Map>
deba@1421
  1268
    UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) {
deba@1421
  1269
      return _readDirMap<
deba@1421
  1270
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1705
  1271
	typename _reader_bits::Arg<Map>::Type>(name, map);
deba@1421
  1272
    }
deba@1421
  1273
deba@1421
  1274
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1275
    ///
deba@1421
  1276
    /// Add a new directed edge map reader command for the reader.
deba@1421
  1277
    template <typename Reader, typename Map>
deba@1421
  1278
    UndirEdgeSetReader& readEdgeMap(std::string name, Map& map, 
deba@1421
  1279
				    const Reader& reader = Reader()) {
deba@1705
  1280
      return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
deba@1421
  1281
	(name, map, reader);
deba@1421
  1282
    }
deba@1421
  1283
deba@1421
  1284
    template <typename Reader, typename Map>
deba@1421
  1285
    UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map, 
deba@1421
  1286
				    const Reader& reader = Reader()) {
deba@1705
  1287
      return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
deba@1421
  1288
	(name, map, reader);
deba@1421
  1289
    }
deba@1421
  1290
deba@1421
  1291
  private:
deba@1421
  1292
deba@1421
  1293
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
  1294
    UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map,
deba@1492
  1295
				    const Reader& reader = Reader()) { 
deba@1492
  1296
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
deba@1492
  1297
      checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
deba@1421
  1298
      readMap("+" + name, 
deba@1705
  1299
	      _reader_bits::forwardComposeMap(graph, map), reader);
deba@1421
  1300
      readMap("-" + name, 
deba@1705
  1301
	      _reader_bits::backwardComposeMap(graph, map), reader);
deba@1421
  1302
      return *this;      
deba@1421
  1303
    }
deba@1421
  1304
deba@1421
  1305
  public:
deba@1421
  1306
deba@1421
  1307
    /// \brief Add a new directed edge map skipper command for the reader.
deba@1421
  1308
    ///
deba@1421
  1309
    /// Add a new directed edge map skipper command for the reader.
deba@1421
  1310
    template <typename Reader>
deba@1421
  1311
    UndirEdgeSetReader& skipEdgeMap(std::string name, 
deba@1421
  1312
				    const Reader& reader = Reader()) {
deba@1421
  1313
      skipMap("+" + name, reader);
deba@1421
  1314
      skipMap("-" + name, reader);
deba@1421
  1315
      return *this;
deba@1421
  1316
    }
deba@1421
  1317
deba@1421
  1318
  protected:
deba@1421
  1319
deba@1421
  1320
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1321
    /// the section with the given header line.
deba@1421
  1322
    ///
deba@1421
  1323
    /// It gives back true when the header line starts with \c \@undiredgeset,
deba@1421
  1324
    /// and the header line's id and the edgeset's id are the same.
deba@1421
  1325
    virtual bool header(const std::string& line) {
deba@1421
  1326
      std::istringstream ls(line);
deba@1421
  1327
      std::string command;
deba@1421
  1328
      std::string name;
deba@1421
  1329
      ls >> command >> name;
deba@1421
  1330
      return command == "@undiredgeset" && name == id;
deba@1421
  1331
    }
deba@1421
  1332
deba@1421
  1333
    /// \brief Reader function of the section.
deba@1421
  1334
    ///
deba@1421
  1335
    /// It reads the content of the section.
deba@1421
  1336
    virtual void read(std::istream& is) {
deba@1476
  1337
      if (!nodeIdReader->isIdReader()) {
deba@1476
  1338
	throw DataFormatError("Cannot find nodeset or ID map");
deba@1476
  1339
      }
deba@1429
  1340
      std::vector<ReaderBase<UndirEdge>* > index;
deba@1421
  1341
      std::string line;
deba@1421
  1342
deba@1421
  1343
      getline(is, line);
deba@1421
  1344
      std::istringstream ls(line);	
deba@1421
  1345
      while (ls >> id) {
deba@1421
  1346
	typename MapReaders::iterator it = readers.find(id);
deba@1421
  1347
	if (it != readers.end()) {
deba@1421
  1348
	  index.push_back(it->second);
deba@1421
  1349
	} else {
deba@1421
  1350
	  index.push_back(&skipper);
deba@1421
  1351
	}
deba@1421
  1352
	if (id == "id" && inverter.get() == 0) {
deba@1421
  1353
	  inverter.reset(index.back()->getInverter());
deba@1421
  1354
	  index.back() = inverter.get();
deba@1421
  1355
	}
deba@1421
  1356
      }
deba@1421
  1357
      while (getline(is, line)) {	
deba@1421
  1358
	std::istringstream ls(line);
deba@1429
  1359
	Node from = nodeIdReader->read(ls);
deba@1429
  1360
	Node to = nodeIdReader->read(ls);
deba@1429
  1361
	UndirEdge edge = graph.addEdge(from, to);
deba@1421
  1362
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1421
  1363
	  index[i]->read(ls, edge);
deba@1421
  1364
	}
deba@1421
  1365
      }
deba@1421
  1366
    }
deba@1421
  1367
deba@1421
  1368
  public:
deba@1421
  1369
deba@1421
  1370
    /// \brief Returns true if the edgeset can give back the edge by its id.
deba@1421
  1371
    ///
deba@1421
  1372
    /// Returns true if the edgeset can give back the undirected edge by its 
deba@1421
  1373
    /// id. It is possible only if an "id" named map was read.
deba@1421
  1374
    bool isIdReader() const {
deba@1421
  1375
      return inverter.get() != 0;
deba@1421
  1376
    }
deba@1421
  1377
deba@1421
  1378
    /// \brief Gives back the undirected edge by its id.
deba@1421
  1379
    ///
deba@1421
  1380
    /// It reads an id from the stream and gives back which undirected edge 
deba@1421
  1381
    /// belongs to it. It is possible only if there was read an "id" named map.
deba@1492
  1382
    void readId(std::istream& is, UndirEdge& undirEdge) const {
deba@1492
  1383
      undirEdge = inverter->read(is);
deba@1421
  1384
    } 
deba@1421
  1385
deba@1429
  1386
    /// \brief Gives back the directed edge by its id.
deba@1429
  1387
    ///
deba@1429
  1388
    /// It reads an id from the stream and gives back which directed edge 
deba@1429
  1389
    /// belongs to it. The directed edge id is the \c '+' or \c '-' character
deba@1429
  1390
    /// and the undirected edge id. It is possible only if there was read 
deba@1429
  1391
    /// an "id" named map.
deba@1492
  1392
    void readId(std::istream& is, Edge& edge) const {
deba@1429
  1393
      char c;
deba@1429
  1394
      is >> c;
deba@1429
  1395
      UndirEdge undirEdge = inverter->read(is);
deba@1429
  1396
      if (c == '+') {
deba@1627
  1397
	edge = graph.direct(undirEdge, true);
deba@1429
  1398
      } else if (c == '-') {
deba@1627
  1399
        edge = graph.direct(undirEdge, false);
deba@1429
  1400
      } else {
deba@1429
  1401
	throw DataFormatError("Wrong id format for edge "
deba@1429
  1402
			      "in undirected edgeset");
deba@1429
  1403
      }
deba@1429
  1404
    } 
deba@1429
  1405
deba@1421
  1406
  private:
deba@1421
  1407
deba@1429
  1408
    typedef std::map<std::string, ReaderBase<UndirEdge>*> MapReaders;
deba@1421
  1409
    MapReaders readers;
deba@1421
  1410
   
deba@1705
  1411
    Graph& graph;   
deba@1408
  1412
    std::string id;
deba@1429
  1413
    SkipReader<UndirEdge, DefaultSkipper> skipper;
deba@1408
  1414
deba@1429
  1415
    std::auto_ptr<InverterBase<UndirEdge> > inverter;
deba@1429
  1416
    std::auto_ptr<IdReaderBase<Node> > nodeIdReader;
deba@1408
  1417
  };
deba@1408
  1418
deba@1409
  1419
  /// \ingroup io_group
deba@1409
  1420
  /// \brief SectionReader for reading labeled nodes.
deba@1409
  1421
  ///
deba@1409
  1422
  /// The nodes section's header line is \c \@nodes \c nodes_id, but the
deba@1409
  1423
  /// \c nodes_id may be empty.
deba@1409
  1424
  ///
deba@1409
  1425
  /// Each line in the section contains the name of the node 
deba@1409
  1426
  /// and then the node id. 
deba@1409
  1427
  ///
deba@1409
  1428
  /// \relates LemonReader
deba@1409
  1429
  template <typename _Graph>
deba@1409
  1430
  class NodeReader : public CommonSectionReaderBase {
deba@1409
  1431
    typedef CommonSectionReaderBase Parent;
deba@1409
  1432
    typedef _Graph Graph;
deba@1429
  1433
    typedef typename Graph::Node Node;
deba@1409
  1434
  public:
deba@1409
  1435
    
deba@1409
  1436
    /// \brief Constructor.
deba@1409
  1437
    ///
deba@1409
  1438
    /// Constructor for NodeReader. It creates the NodeReader and
deba@1409
  1439
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1440
    /// node id reader to give back the nodes. The reader will read the 
deba@1409
  1441
    /// section only if the \c _id and the \c nodes_id are the same. 
deba@1409
  1442
    template <typename _IdReader>
deba@1409
  1443
    NodeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1409
  1444
	       const std::string& _id = std::string()) 
deba@1476
  1445
      : Parent(_reader), id(_id) {
deba@1476
  1446
      checkConcept<_reader_bits::ItemIdReader<Node>, _IdReader>();
deba@1492
  1447
      nodeIdReader.reset(new IdReader<Node, _IdReader>(_idReader));
deba@1476
  1448
    }
deba@1408
  1449
deba@1409
  1450
    /// \brief Destructor.
deba@1409
  1451
    ///
deba@1409
  1452
    /// Destructor for NodeReader.
deba@1409
  1453
    virtual ~NodeReader() {}
deba@1409
  1454
deba@1409
  1455
  private:
deba@1409
  1456
    NodeReader(const NodeReader&);
deba@1409
  1457
    void operator=(const NodeReader&);
deba@1409
  1458
deba@1409
  1459
  public:
deba@1409
  1460
deba@1409
  1461
    /// \brief Add a node reader command for the NodeReader.
deba@1409
  1462
    ///
deba@1409
  1463
    /// Add a node reader command for the NodeReader.
deba@1429
  1464
    void readNode(const std::string& name, Node& item) {
deba@1409
  1465
      if (readers.find(name) != readers.end()) {
deba@1409
  1466
	ErrorMessage msg;
deba@1409
  1467
	msg << "Multiple read rule for node: " << name;
deba@1409
  1468
	throw IOParameterError(msg.message());
deba@1409
  1469
      }
deba@1409
  1470
      readers.insert(make_pair(name, &item));
deba@1409
  1471
    }
deba@1409
  1472
deba@1409
  1473
  protected:
deba@1409
  1474
deba@1409
  1475
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1476
    /// the section with the given header line.
deba@1409
  1477
    ///
deba@1421
  1478
    /// It gives back true when the header line start with \c \@nodes,
deba@1409
  1479
    /// and the header line's id and the reader's id are the same.
deba@1409
  1480
    virtual bool header(const std::string& line) {
deba@1409
  1481
      std::istringstream ls(line);
deba@1409
  1482
      std::string command;
deba@1409
  1483
      std::string name;
deba@1409
  1484
      ls >> command >> name;
deba@1409
  1485
      return command == "@nodes" && name == id;
deba@1409
  1486
    }
deba@1409
  1487
deba@1409
  1488
    /// \brief Reader function of the section.
deba@1409
  1489
    ///
deba@1409
  1490
    /// It reads the content of the section.
deba@1409
  1491
    virtual void read(std::istream& is) {
deba@1492
  1492
      if (!nodeIdReader->isIdReader()) {
deba@1476
  1493
	throw DataFormatError("Cannot find nodeset or ID map");
deba@1476
  1494
      }
deba@1409
  1495
      std::string line;
deba@1409
  1496
      while (getline(is, line)) {
deba@1409
  1497
	std::istringstream ls(line);
deba@1409
  1498
	std::string id;
deba@1409
  1499
	ls >> id;
deba@1429
  1500
	typename NodeReaders::iterator it = readers.find(id);
deba@1409
  1501
	if (it != readers.end()) {
deba@1492
  1502
	  *(it->second) = nodeIdReader->read(ls); 
deba@1409
  1503
	}	
deba@1409
  1504
      }
deba@1409
  1505
    }
deba@1409
  1506
    
deba@1409
  1507
  private:
deba@1409
  1508
deba@1409
  1509
    std::string id;
deba@1409
  1510
deba@1429
  1511
    typedef std::map<std::string, Node*> NodeReaders;
deba@1429
  1512
    NodeReaders readers;
deba@1492
  1513
    std::auto_ptr<IdReaderBase<Node> > nodeIdReader;
deba@1409
  1514
  };
deba@1409
  1515
deba@1409
  1516
  /// \ingroup io_group
deba@1409
  1517
  /// \brief SectionReader for reading labeled edges.
deba@1409
  1518
  ///
deba@1409
  1519
  /// The edges section's header line is \c \@edges \c edges_id, but the
deba@1409
  1520
  /// \c edges_id may be empty.
deba@1409
  1521
  ///
deba@1409
  1522
  /// Each line in the section contains the name of the edge 
deba@1409
  1523
  /// and then the edge id. 
deba@1409
  1524
  ///
deba@1409
  1525
  /// \relates LemonReader
deba@1409
  1526
  template <typename _Graph>
deba@1409
  1527
  class EdgeReader : public CommonSectionReaderBase {
deba@1409
  1528
    typedef CommonSectionReaderBase Parent;
deba@1409
  1529
    typedef _Graph Graph;
deba@1429
  1530
    typedef typename Graph::Edge Edge;
deba@1409
  1531
  public:
deba@1409
  1532
    
deba@1409
  1533
    /// \brief Constructor.
deba@1409
  1534
    ///
deba@1409
  1535
    /// Constructor for EdgeReader. It creates the EdgeReader and
deba@1409
  1536
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1537
    /// edge id reader to give back the edges. The reader will read the 
deba@1421
  1538
    /// section only if the \c _id and the \c edges_id are the same. 
deba@1409
  1539
    template <typename _IdReader>
deba@1409
  1540
    EdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1409
  1541
	       const std::string& _id = std::string()) 
deba@1476
  1542
      : Parent(_reader), id(_id) {
deba@1476
  1543
      checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
deba@1492
  1544
      edgeIdReader.reset(new IdReader<Edge, _IdReader>(_idReader));
deba@1476
  1545
    }
deba@1409
  1546
deba@1409
  1547
    /// \brief Destructor.
deba@1409
  1548
    ///
deba@1409
  1549
    /// Destructor for EdgeReader.
deba@1409
  1550
    virtual ~EdgeReader() {}
deba@1409
  1551
  private:
deba@1409
  1552
    EdgeReader(const EdgeReader&);
deba@1409
  1553
    void operator=(const EdgeReader&);
deba@1409
  1554
deba@1409
  1555
  public:
deba@1409
  1556
deba@1409
  1557
    /// \brief Add an edge reader command for the EdgeReader.
deba@1409
  1558
    ///
deba@1409
  1559
    /// Add an edge reader command for the EdgeReader.
deba@1429
  1560
    void readEdge(const std::string& name, Edge& item) {
deba@1409
  1561
      if (readers.find(name) != readers.end()) {
deba@1409
  1562
	ErrorMessage msg;
deba@1409
  1563
	msg << "Multiple read rule for edge: " << name;
deba@1409
  1564
	throw IOParameterError(msg.message());
deba@1409
  1565
      }
deba@1409
  1566
      readers.insert(make_pair(name, &item));
deba@1409
  1567
    }
deba@1409
  1568
deba@1409
  1569
  protected:
deba@1409
  1570
deba@1409
  1571
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1572
    /// the section with the given header line.
deba@1409
  1573
    ///
deba@1421
  1574
    /// It gives back true when the header line start with \c \@edges,
deba@1421
  1575
    /// and the header line's id and the reader's id are the same.
deba@1421
  1576
    virtual bool header(const std::string& line) {
deba@1421
  1577
      std::istringstream ls(line);
deba@1421
  1578
      std::string command;
deba@1421
  1579
      std::string name;
deba@1421
  1580
      ls >> command >> name;
deba@1421
  1581
      return command == "@edges" && name == id;
deba@1421
  1582
    }
deba@1421
  1583
deba@1421
  1584
    /// \brief Reader function of the section.
deba@1421
  1585
    ///
deba@1421
  1586
    /// It reads the content of the section.
deba@1421
  1587
    virtual void read(std::istream& is) {
deba@1492
  1588
      if (!edgeIdReader->isIdReader()) {
deba@1476
  1589
	throw DataFormatError("Cannot find edgeset or ID map");
deba@1476
  1590
      }
deba@1421
  1591
      std::string line;
deba@1421
  1592
      while (getline(is, line)) {
deba@1421
  1593
	std::istringstream ls(line);
deba@1421
  1594
	std::string id;
deba@1421
  1595
	ls >> id;
deba@1429
  1596
	typename EdgeReaders::iterator it = readers.find(id);
deba@1421
  1597
	if (it != readers.end()) {
deba@1492
  1598
	  *(it->second) = edgeIdReader->read(ls); 
deba@1421
  1599
	}	
deba@1421
  1600
      }
deba@1421
  1601
    }
deba@1421
  1602
    
deba@1421
  1603
  private:
deba@1421
  1604
deba@1421
  1605
    std::string id;
deba@1421
  1606
deba@1429
  1607
    typedef std::map<std::string, Edge*> EdgeReaders;
deba@1429
  1608
    EdgeReaders readers;
deba@1492
  1609
    std::auto_ptr<IdReaderBase<Edge> > edgeIdReader;
deba@1421
  1610
  };
deba@1421
  1611
deba@1421
  1612
  /// \ingroup io_group
deba@1421
  1613
  /// \brief SectionReader for reading labeled undirected edges.
deba@1421
  1614
  ///
deba@1421
  1615
  /// The undirected edges section's header line is \c \@undiredges 
deba@1421
  1616
  /// \c undiredges_id, but the \c undiredges_id may be empty.
deba@1421
  1617
  ///
deba@1421
  1618
  /// Each line in the section contains the name of the undirected edge 
deba@1421
  1619
  /// and then the undirected edge id. 
deba@1421
  1620
  ///
deba@1421
  1621
  /// \relates LemonReader
deba@1421
  1622
  template <typename _Graph>
deba@1421
  1623
  class UndirEdgeReader : public CommonSectionReaderBase {
deba@1421
  1624
    typedef CommonSectionReaderBase Parent;
deba@1421
  1625
    typedef _Graph Graph;
deba@1429
  1626
    typedef typename Graph::Edge Edge;
deba@1429
  1627
    typedef typename Graph::UndirEdge UndirEdge;
deba@1421
  1628
  public:
deba@1421
  1629
    
deba@1421
  1630
    /// \brief Constructor.
deba@1421
  1631
    ///
deba@1421
  1632
    /// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and
deba@1421
  1633
    /// attach it into the given LemonReader. It will use the given
deba@1421
  1634
    /// undirected edge id reader to give back the edges. The reader will 
deba@1421
  1635
    /// read the section only if the \c _id and the \c undiredges_id are 
deba@1421
  1636
    /// the same. 
deba@1421
  1637
    template <typename _IdReader>
deba@1421
  1638
    UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1421
  1639
	       const std::string& _id = std::string()) 
deba@1476
  1640
      : Parent(_reader), id(_id) {
deba@1476
  1641
      checkConcept<_reader_bits::ItemIdReader<UndirEdge>, _IdReader>();
deba@1476
  1642
      checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
deba@1476
  1643
      undirEdgeIdReader.reset(new IdReader<UndirEdge, _IdReader>(_idReader));
deba@1477
  1644
      edgeIdReader.reset(new IdReader<Edge, _IdReader>(_idReader));
deba@1476
  1645
    }
deba@1421
  1646
deba@1421
  1647
    /// \brief Destructor.
deba@1421
  1648
    ///
deba@1421
  1649
    /// Destructor for UndirEdgeReader.
deba@1421
  1650
    virtual ~UndirEdgeReader() {}
deba@1421
  1651
  private:
deba@1421
  1652
    UndirEdgeReader(const UndirEdgeReader&);
deba@1421
  1653
    void operator=(const UndirEdgeReader&);
deba@1421
  1654
deba@1421
  1655
  public:
deba@1421
  1656
deba@1421
  1657
    /// \brief Add an undirected edge reader command for the UndirEdgeReader.
deba@1421
  1658
    ///
deba@1421
  1659
    /// Add an undirected edge reader command for the UndirEdgeReader.
deba@1429
  1660
    void readUndirEdge(const std::string& name, UndirEdge& item) {
deba@1429
  1661
      if (undirEdgeReaders.find(name) != undirEdgeReaders.end()) {
deba@1429
  1662
	ErrorMessage msg;
deba@1429
  1663
	msg << "Multiple read rule for undirected edge: " << name;
deba@1429
  1664
	throw IOParameterError(msg.message());
deba@1429
  1665
      }
deba@1429
  1666
      undirEdgeReaders.insert(make_pair(name, &item));
deba@1429
  1667
    }
deba@1429
  1668
deba@1429
  1669
    /// \brief Add an edge reader command for the UndirEdgeReader.
deba@1429
  1670
    ///
deba@1429
  1671
    /// Add an edge reader command for the UndirEdgeReader.
deba@1429
  1672
    void readEdge(const std::string& name, Edge& item) {
deba@1429
  1673
      if (edgeReaders.find(name) != edgeReaders.end()) {
deba@1421
  1674
	ErrorMessage msg;
deba@1421
  1675
	msg << "Multiple read rule for edge: " << name;
deba@1421
  1676
	throw IOParameterError(msg.message());
deba@1421
  1677
      }
deba@1429
  1678
      edgeReaders.insert(make_pair(name, &item));
deba@1421
  1679
    }
deba@1421
  1680
deba@1421
  1681
  protected:
deba@1421
  1682
deba@1421
  1683
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1684
    /// the section with the given header line.
deba@1421
  1685
    ///
deba@1421
  1686
    /// It gives back true when the header line start with \c \@edges,
deba@1409
  1687
    /// and the header line's id and the reader's id are the same.
deba@1409
  1688
    virtual bool header(const std::string& line) {
deba@1409
  1689
      std::istringstream ls(line);
deba@1409
  1690
      std::string command;
deba@1409
  1691
      std::string name;
deba@1409
  1692
      ls >> command >> name;
deba@1429
  1693
      return command == "@undiredges" && name == id;
deba@1409
  1694
    }
deba@1409
  1695
deba@1409
  1696
    /// \brief Reader function of the section.
deba@1409
  1697
    ///
deba@1409
  1698
    /// It reads the content of the section.
deba@1409
  1699
    virtual void read(std::istream& is) {
deba@1476
  1700
      if (!edgeIdReader->isIdReader()) {
deba@1476
  1701
	throw DataFormatError("Cannot find undirected edgeset or ID map");
deba@1476
  1702
      }
deba@1476
  1703
      if (!undirEdgeIdReader->isIdReader()) {
deba@1476
  1704
	throw DataFormatError("Cannot find undirected edgeset or ID map");
deba@1476
  1705
      }
deba@1409
  1706
      std::string line;
deba@1409
  1707
      while (getline(is, line)) {
deba@1409
  1708
	std::istringstream ls(line);
deba@1409
  1709
	std::string id;
deba@1409
  1710
	ls >> id;
deba@1429
  1711
	{
deba@1429
  1712
	  typename UndirEdgeReaders::iterator it = undirEdgeReaders.find(id);
deba@1429
  1713
	  if (it != undirEdgeReaders.end()) {
deba@1429
  1714
	    *(it->second) = undirEdgeIdReader->read(ls); 
deba@1429
  1715
	    break;
deba@1429
  1716
	  }	
deba@1429
  1717
	} {
deba@1429
  1718
	  typename EdgeReaders::iterator it = edgeReaders.find(id);
deba@1429
  1719
	  if (it != edgeReaders.end()) {
deba@1429
  1720
	    *(it->second) = edgeIdReader->read(ls); 
deba@1429
  1721
	    break;
deba@1429
  1722
	  }	
deba@1429
  1723
	}
deba@1409
  1724
      }
deba@1409
  1725
    }
deba@1409
  1726
    
deba@1409
  1727
  private:
deba@1409
  1728
deba@1409
  1729
    std::string id;
deba@1409
  1730
deba@1429
  1731
    typedef std::map<std::string, UndirEdge*> UndirEdgeReaders;
deba@1429
  1732
    UndirEdgeReaders undirEdgeReaders;
deba@1429
  1733
    std::auto_ptr<IdReaderBase<UndirEdge> > undirEdgeIdReader;
deba@1429
  1734
deba@1429
  1735
    typedef std::map<std::string, Edge*> EdgeReaders;
deba@1429
  1736
    EdgeReaders edgeReaders;
deba@1429
  1737
    std::auto_ptr<IdReaderBase<Edge> > edgeIdReader;
deba@1409
  1738
  };
deba@1409
  1739
deba@1409
  1740
  /// \ingroup io_group
deba@1409
  1741
  /// \brief SectionReader for attributes.
deba@1409
  1742
  ///
deba@1409
  1743
  /// The lemon format can store multiple attribute set. Each set has
deba@1409
  1744
  /// the header line \c \@attributes \c attributeset_id, but the 
deba@1409
  1745
  /// attributeset_id may be empty.
deba@1409
  1746
  ///
deba@1409
  1747
  /// The attributeset section contains several lines. Each of them starts
deba@1409
  1748
  /// with an attribute and then a the value for the id.
deba@1409
  1749
  ///
deba@1409
  1750
  /// \relates LemonReader
deba@1408
  1751
  template <typename _Traits = DefaultReaderTraits>
deba@1408
  1752
  class AttributeReader : public CommonSectionReaderBase {
deba@1408
  1753
    typedef CommonSectionReaderBase Parent;
deba@1408
  1754
    typedef _Traits Traits; 
deba@1408
  1755
  public:
deba@1409
  1756
    /// \brief Constructor.
deba@1409
  1757
    ///
deba@1409
  1758
    /// Constructor for AttributeReader. It creates the AttributeReader and
deba@1409
  1759
    /// attach it into the given LemonReader. The reader process a section
deba@1409
  1760
    /// only if the \c section_id and the \c _id are the same.
deba@1408
  1761
    AttributeReader(LemonReader& _reader, 
deba@1409
  1762
		    const std::string& _id = std::string()) 
deba@1409
  1763
      : Parent(_reader), id(_id) {}
deba@1408
  1764
deba@1409
  1765
    /// \brief Destructor.
deba@1409
  1766
    ///
deba@1409
  1767
    /// Destructor for AttributeReader.
deba@1408
  1768
    virtual ~AttributeReader() {
deba@1408
  1769
      for (typename Readers::iterator it = readers.begin(); 
deba@1408
  1770
	   it != readers.end(); ++it) {
deba@1408
  1771
	delete it->second;
deba@1408
  1772
      }
deba@1408
  1773
    }
deba@1408
  1774
deba@1408
  1775
  private:
deba@1408
  1776
    AttributeReader(const AttributeReader&);
deba@1408
  1777
    void operator=(AttributeReader&);
deba@1408
  1778
deba@1408
  1779
  public:
deba@1409
  1780
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1781
    ///
deba@1409
  1782
    /// Add an attribute reader command for the reader.
deba@1408
  1783
    template <typename Value>
deba@1408
  1784
    AttributeReader& readAttribute(const std::string& id, Value& value) {
deba@1408
  1785
      return readAttribute<typename Traits::template Reader<Value> >
deba@1408
  1786
	(id, value);
deba@1408
  1787
    }
deba@1408
  1788
deba@1409
  1789
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1790
    ///
deba@1409
  1791
    /// Add an attribute reader command for the reader.
deba@1408
  1792
    template <typename Reader, typename Value>
deba@1408
  1793
    AttributeReader& readAttribute(const std::string& name, Value& value,
deba@1408
  1794
				   const Reader& reader = Reader()) {
deba@1492
  1795
      checkConcept<_reader_bits::ItemReader<Value>, Reader>();
deba@1408
  1796
      if (readers.find(name) != readers.end()) {
deba@1408
  1797
	ErrorMessage msg;
deba@1408
  1798
	msg << "Multiple read rule for attribute: " << name;
deba@1408
  1799
	throw IOParameterError(msg.message());
deba@1408
  1800
      }
deba@1408
  1801
      readers.insert(make_pair(name, new ValueReader<Value, Reader>
deba@1408
  1802
      			       (value, reader)));
deba@1408
  1803
      return *this;
deba@1408
  1804
    }
deba@1408
  1805
deba@1409
  1806
  protected:
deba@1409
  1807
deba@1409
  1808
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1809
    /// the section with the given header line.
deba@1409
  1810
    ///
deba@1421
  1811
    /// It gives back true when the header line start with \c \@attributes,
deba@1409
  1812
    /// and the header line's id and the attributeset's id are the same.
deba@1408
  1813
    bool header(const std::string& line) {
deba@1408
  1814
      std::istringstream ls(line);
deba@1408
  1815
      std::string command;
deba@1408
  1816
      std::string name;
deba@1408
  1817
      ls >> command >> name;
deba@1408
  1818
      return command == "@attributes" && name == id;
deba@1408
  1819
    }
deba@1408
  1820
deba@1409
  1821
    /// \brief Reader function of the section.
deba@1409
  1822
    ///
deba@1409
  1823
    /// It reads the content of the section.
deba@1408
  1824
    void read(std::istream& is) {
deba@1408
  1825
      std::string line;
deba@1408
  1826
      while (getline(is, line)) {
deba@1408
  1827
	std::istringstream ls(line);
deba@1408
  1828
	std::string id;
deba@1408
  1829
	ls >> id;
deba@1408
  1830
	typename Readers::iterator it = readers.find(id);
deba@1408
  1831
	if (it != readers.end()) {
deba@1408
  1832
	  it->second->read(ls);
deba@1408
  1833
	}
deba@1408
  1834
      }
deba@1408
  1835
    }    
deba@1408
  1836
deba@1408
  1837
  private:
deba@1408
  1838
    std::string id;
deba@1408
  1839
deba@1408
  1840
    typedef std::map<std::string, ValueReaderBase*> Readers;
deba@1409
  1841
    Readers readers;  
deba@1408
  1842
  };
deba@1408
  1843
deba@1423
  1844
  /// \ingroup io_group
deba@1423
  1845
  /// \brief SectionReader for retrieve what is in the file.
deba@1423
  1846
  ///
deba@1423
  1847
  /// SectionReader for retrieve what is in the file. If you want
deba@1423
  1848
  /// to know which sections, maps and items are in the file
deba@1423
  1849
  /// use the next code:
deba@1423
  1850
  /// \code
deba@1423
  1851
  /// LemonReader reader("input.lgf");
deba@1423
  1852
  /// ContentReader content(reader);
deba@1423
  1853
  /// reader.run();
deba@1423
  1854
  /// \endcode
deba@1423
  1855
  class ContentReader : public LemonReader::SectionReader {
deba@1423
  1856
    typedef LemonReader::SectionReader Parent;
deba@1423
  1857
  public:
deba@1423
  1858
    /// \brief Constructor.
deba@1423
  1859
    ///
deba@1423
  1860
    /// Constructor for
deba@1423
  1861
    ContentReader(LemonReader& _reader) : Parent(_reader) {}
deba@1423
  1862
deba@1423
  1863
    /// \brief Desctructor.
deba@1423
  1864
    ///
deba@1423
  1865
    /// Desctructor.
deba@1423
  1866
    virtual ~ContentReader() {}
deba@1423
  1867
deba@1423
  1868
    /// \brief Gives back how many nodesets are in the file.
deba@1423
  1869
    ///
deba@1423
  1870
    /// Gives back how many nodesets are in the file.
deba@1423
  1871
    int nodeSetNum() const {
deba@1423
  1872
      return nodesets.size();
deba@1423
  1873
    }
deba@1423
  1874
deba@1423
  1875
    /// \brief Gives back the name of nodeset on the indiced position.
deba@1423
  1876
    ///
deba@1423
  1877
    /// Gives back the name of nodeset on the indiced position.
deba@1423
  1878
    std::string nodeSetName(int index) const {
deba@1423
  1879
      return nodesets[index].name;
deba@1423
  1880
    }
deba@1423
  1881
deba@1423
  1882
    /// \brief Gives back the map names of nodeset on the indiced position.
deba@1423
  1883
    ///
deba@1423
  1884
    /// Gives back the map names of nodeset on the indiced position.
deba@1423
  1885
    const std::vector<std::string>& nodeSetMaps(int index) const {
deba@1423
  1886
      return nodesets[index].items;
deba@1423
  1887
    }
deba@1423
  1888
deba@1423
  1889
    /// \brief Gives back how many edgesets are in the file.
deba@1423
  1890
    ///
deba@1423
  1891
    /// Gives back how many edgesets are in the file.
deba@1423
  1892
    int edgeSetNum() const {
deba@1423
  1893
      return edgesets.size();
deba@1423
  1894
    }
deba@1423
  1895
deba@1423
  1896
    /// \brief Gives back the name of edgeset on the indiced position.
deba@1423
  1897
    ///
deba@1423
  1898
    /// Gives back the name of edgeset on the indiced position.
deba@1423
  1899
    std::string edgeSetName(int index) const {
deba@1423
  1900
      return edgesets[index].name;
deba@1423
  1901
    }
deba@1423
  1902
deba@1423
  1903
    /// \brief Gives back the map names of edgeset on the indiced position.
deba@1423
  1904
    ///
deba@1423
  1905
    /// Gives back the map names of edgeset on the indiced position.
deba@1423
  1906
    const std::vector<std::string>& edgeSetMaps(int index) const {
deba@1423
  1907
      return edgesets[index].items;
deba@1423
  1908
    }
deba@1423
  1909
deba@1423
  1910
    /// \brief Gives back how many undirected edgesets are in the file.
deba@1423
  1911
    ///
deba@1423
  1912
    /// Gives back how many undirected edgesets are in the file.
deba@1423
  1913
    int undirEdgeSetNum() const {
deba@1423
  1914
      return undiredgesets.size();
deba@1423
  1915
    }
deba@1423
  1916
deba@1423
  1917
    /// \brief Gives back the name of undirected edgeset on the indiced 
deba@1423
  1918
    /// position.
deba@1423
  1919
    ///
deba@1423
  1920
    /// Gives back the name of undirected edgeset on the indiced position.
deba@1423
  1921
    std::string undirEdgeSetName(int index) const {
deba@1423
  1922
      return undiredgesets[index].name;
deba@1423
  1923
    }
deba@1423
  1924
deba@1423
  1925
    /// \brief Gives back the map names of undirected edgeset on the indiced 
deba@1423
  1926
    /// position.
deba@1423
  1927
    ///
deba@1423
  1928
    /// Gives back the map names of undirected edgeset on the indiced position.
deba@1423
  1929
    const std::vector<std::string>& undirEdgeSetMaps(int index) const {
deba@1423
  1930
      return undiredgesets[index].items;
deba@1423
  1931
    }
deba@1423
  1932
deba@1423
  1933
    /// \brief Gives back how many labeled nodes section are in the file.
deba@1423
  1934
    ///
deba@1423
  1935
    /// Gives back how many labeled nodes section are in the file.
deba@1423
  1936
    int nodesNum() const {
deba@1423
  1937
      return nodes.size();
deba@1423
  1938
    }
deba@1423
  1939
deba@1423
  1940
    /// \brief Gives back the name of labeled nodes section on the indiced 
deba@1423
  1941
    /// position.
deba@1423
  1942
    ///
deba@1423
  1943
    /// Gives back the name of labeled nodes section on the indiced position.
deba@1423
  1944
    std::string nodesName(int index) const {
deba@1423
  1945
      return nodes[index].name;
deba@1423
  1946
    }
deba@1423
  1947
deba@1423
  1948
    /// \brief Gives back the names of the labeled nodes in the indiced 
deba@1423
  1949
    /// section.
deba@1423
  1950
    ///
deba@1423
  1951
    /// Gives back the names of the labeled nodes in the indiced section.
deba@1423
  1952
    const std::vector<std::string>& nodesItems(int index) const {
deba@1423
  1953
      return nodes[index].items;
deba@1423
  1954
    }
deba@1423
  1955
deba@1423
  1956
    /// \brief Gives back how many labeled edges section are in the file.
deba@1423
  1957
    ///
deba@1423
  1958
    /// Gives back how many labeled edges section are in the file.
deba@1423
  1959
    int edgesNum() const {
deba@1423
  1960
      return edges.size();
deba@1423
  1961
    }
deba@1423
  1962
deba@1423
  1963
    /// \brief Gives back the name of labeled edges section on the indiced 
deba@1423
  1964
    /// position.
deba@1423
  1965
    ///
deba@1423
  1966
    /// Gives back the name of labeled edges section on the indiced position.
deba@1423
  1967
    std::string edgesName(int index) const {
deba@1423
  1968
      return edges[index].name;
deba@1423
  1969
    }
deba@1423
  1970
deba@1423
  1971
    /// \brief Gives back the names of the labeled edges in the indiced 
deba@1423
  1972
    /// section.
deba@1423
  1973
    ///
deba@1423
  1974
    /// Gives back the names of the labeled edges in the indiced section.
deba@1423
  1975
    const std::vector<std::string>& edgesItems(int index) const {
deba@1423
  1976
      return edges[index].items;
deba@1423
  1977
    }
deba@1423
  1978
 
deba@1423
  1979
    /// \brief Gives back how many labeled undirected edges section are 
deba@1423
  1980
    /// in the file.
deba@1423
  1981
    ///
deba@1423
  1982
    /// Gives back how many labeled undirected edges section are in the file.
deba@1423
  1983
    int undirEdgesNum() const {
deba@1423
  1984
      return undiredges.size();
deba@1423
  1985
    }
deba@1423
  1986
deba@1423
  1987
    /// \brief Gives back the name of labeled undirected edges section 
deba@1423
  1988
    /// on the indiced position.
deba@1423
  1989
    ///
deba@1423
  1990
    /// Gives back the name of labeled undirected edges section on the 
deba@1423
  1991
    /// indiced position.
deba@1423
  1992
    std::string undirEdgesName(int index) const {
deba@1423
  1993
      return undiredges[index].name;
deba@1423
  1994
    }
deba@1423
  1995
deba@1423
  1996
    /// \brief Gives back the names of the labeled undirected edges in 
deba@1423
  1997
    /// the indiced section.
deba@1423
  1998
    ///
deba@1423
  1999
    /// Gives back the names of the labeled undirected edges in the 
deba@1423
  2000
    /// indiced section.
deba@1423
  2001
    const std::vector<std::string>& undirEdgesItems(int index) const {
deba@1423
  2002
      return undiredges[index].items;
deba@1423
  2003
    }
deba@1423
  2004
deba@1423
  2005
 
deba@1423
  2006
    /// \brief Gives back how many attributes section are in the file.
deba@1423
  2007
    ///
deba@1423
  2008
    /// Gives back how many attributes section are in the file.
deba@1423
  2009
    int attributesNum() const {
deba@1423
  2010
      return attributes.size();
deba@1423
  2011
    }
deba@1423
  2012
deba@1423
  2013
    /// \brief Gives back the name of attributes section on the indiced 
deba@1423
  2014
    /// position.
deba@1423
  2015
    ///
deba@1423
  2016
    /// Gives back the name of attributes section on the indiced position.
deba@1423
  2017
    std::string attributesName(int index) const {
deba@1423
  2018
      return attributes[index].name;
deba@1423
  2019
    }
deba@1423
  2020
deba@1423
  2021
    /// \brief Gives back the names of the attributes in the indiced section.
deba@1423
  2022
    ///
deba@1423
  2023
    /// Gives back the names of the attributes in the indiced section.
deba@1423
  2024
    const std::vector<std::string>& attributesItems(int index) const {
deba@1423
  2025
      return attributes[index].items;
deba@1423
  2026
    }
deba@1423
  2027
deba@1423
  2028
    const std::vector<std::string>& otherSections() const {
deba@1423
  2029
      return sections;
deba@1423
  2030
    }
deba@1423
  2031
deba@1423
  2032
  protected:
deba@1423
  2033
    
deba@1423
  2034
    /// \brief Gives back true when the SectionReader can process 
deba@1423
  2035
    /// the section with the given header line.
deba@1423
  2036
    ///
deba@1423
  2037
    /// It gives back true when the section is common section.
deba@1423
  2038
    bool header(const std::string& line) {
deba@1423
  2039
      std::istringstream ls(line);
deba@1423
  2040
      std::string command, name;
deba@1423
  2041
      ls >> command >> name;
deba@1423
  2042
      if (command == "@nodeset") {
deba@1423
  2043
	current = command;
deba@1423
  2044
	nodesets.push_back(SectionInfo(name));
deba@1423
  2045
      } else if (command == "@edgeset") {
deba@1423
  2046
	current = command;
deba@1423
  2047
	edgesets.push_back(SectionInfo(name));
deba@1423
  2048
      } else if (command == "@undiredgeset") {
deba@1423
  2049
	current = command;
deba@1423
  2050
	undiredgesets.push_back(SectionInfo(name));
deba@1423
  2051
      } else if (command == "@nodes") {
deba@1423
  2052
	current = command;
deba@1423
  2053
	nodes.push_back(SectionInfo(name));
deba@1423
  2054
      } else if (command == "@edges") {
deba@1423
  2055
	current = command;
deba@1423
  2056
	edges.push_back(SectionInfo(name));
deba@1423
  2057
      } else if (command == "@undiredges") {
deba@1423
  2058
	current = command;
deba@1423
  2059
	undiredges.push_back(SectionInfo(name));
deba@1423
  2060
      } else if (command == "@attributes") {
deba@1423
  2061
	current = command;
deba@1423
  2062
	attributes.push_back(SectionInfo(name));
deba@1423
  2063
      } else {
deba@1423
  2064
	sections.push_back(line);
deba@1423
  2065
	return false;
deba@1423
  2066
      }
deba@1423
  2067
      return true;
deba@1423
  2068
    }
deba@1423
  2069
deba@1423
  2070
    /// \brief Retrieve the items from various sections.
deba@1423
  2071
    ///
deba@1423
  2072
    /// Retrieve the items from various sections.
deba@1423
  2073
    void read(std::istream& is) {
deba@1423
  2074
      if (current == "@nodeset") {
deba@1423
  2075
	readMapNames(is, nodesets.back().items);
deba@1423
  2076
      } else if (current == "@edgeset") {
deba@1423
  2077
	readMapNames(is, edgesets.back().items);
deba@1423
  2078
      } else if (current == "@undiredgeset") {
deba@1423
  2079
	readMapNames(is, undiredgesets.back().items);
deba@1423
  2080
      } else if (current == "@nodes") {
deba@1423
  2081
	readItemNames(is, nodes.back().items);
deba@1423
  2082
      } else if (current == "@edges") {
deba@1423
  2083
	readItemNames(is, edges.back().items);
deba@1423
  2084
      } else if (current == "@undiredges") {
deba@1423
  2085
	readItemNames(is, undiredges.back().items);
deba@1423
  2086
      } else if (current == "@attributes") {
deba@1423
  2087
	readItemNames(is, attributes.back().items);
deba@1423
  2088
      }
deba@1423
  2089
    }    
deba@1423
  2090
deba@1423
  2091
  private:
deba@1423
  2092
deba@1423
  2093
    void readMapNames(std::istream& is, std::vector<std::string>& maps) {
deba@1423
  2094
      std::string line, id;
deba@1423
  2095
      std::getline(is, line);
deba@1423
  2096
      std::istringstream ls(line);
deba@1423
  2097
      while (ls >> id) {
deba@1423
  2098
	maps.push_back(id);
deba@1423
  2099
      }
deba@1423
  2100
      while (getline(is, line));
deba@1423
  2101
    }
deba@1423
  2102
deba@1423
  2103
    void readItemNames(std::istream& is, std::vector<std::string>& maps) {
deba@1423
  2104
      std::string line, id;
deba@1423
  2105
      while (std::getline(is, line)) {
deba@1423
  2106
	std::istringstream ls(line);
deba@1423
  2107
	ls >> id;
deba@1423
  2108
	maps.push_back(id);
deba@1423
  2109
      }
deba@1423
  2110
    }
deba@1423
  2111
deba@1423
  2112
    struct SectionInfo {
deba@1423
  2113
      std::string name;
deba@1423
  2114
      std::vector<std::string> items;
deba@1423
  2115
deba@1423
  2116
      SectionInfo(const std::string& _name) : name(_name) {}
deba@1423
  2117
    };
deba@1423
  2118
deba@1423
  2119
    std::vector<SectionInfo> nodesets;
deba@1423
  2120
    std::vector<SectionInfo> edgesets;
deba@1423
  2121
    std::vector<SectionInfo> undiredgesets;
deba@1423
  2122
deba@1423
  2123
    std::vector<SectionInfo> nodes;
deba@1423
  2124
    std::vector<SectionInfo> edges;
deba@1423
  2125
    std::vector<SectionInfo> undiredges;
deba@1423
  2126
deba@1423
  2127
    std::vector<SectionInfo> attributes;
deba@1423
  2128
deba@1423
  2129
    std::vector<std::string> sections;
deba@1423
  2130
deba@1423
  2131
    std::string current;
deba@1423
  2132
deba@1423
  2133
  };
deba@1423
  2134
deba@1408
  2135
}
deba@1408
  2136
#endif