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