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