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