lemon/lemon_reader.h
author alpar
Sat, 03 Mar 2007 12:05:05 +0000
changeset 2389 df6a32249b46
parent 2368 6b2e8b734ae7
child 2391 14a343be7a5a
permissions -rw-r--r--
arg_parser.h: A command line argument parser.
dist_log.h: A tool for measuring one and two dimensional distributions.
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@2386
   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@2386
   712
	      std::istream ss(&buffer);
deba@2386
   713
	      it->first->read(ss);
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@2386
   800
    NodeSetReader& readNodeMap(std::string label, Map& map) {
deba@1421
   801
      return _readMap<
deba@1421
   802
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
   803
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
   804
    }
deba@1421
   805
deba@1421
   806
    template <typename Map>
deba@2386
   807
    NodeSetReader& readNodeMap(std::string label, const Map& map) {
deba@1421
   808
      return _readMap<
deba@1421
   809
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
   810
	typename _reader_bits::Arg<Map>::Type>(label, 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@2386
   816
    template <typename ItemReader, typename Map>
deba@2386
   817
    NodeSetReader& readNodeMap(std::string label, Map& map, 
deba@2386
   818
			       const ItemReader& ir = ItemReader()) {
deba@2386
   819
      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
deba@2386
   820
	(label, map, ir);
deba@1421
   821
    }
deba@1421
   822
deba@2386
   823
    template <typename ItemReader, typename Map>
deba@2386
   824
    NodeSetReader& readNodeMap(std::string label, const Map& map, 
deba@2386
   825
			       const ItemReader& ir = ItemReader()) {
deba@2386
   826
      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
deba@2386
   827
	(label, map, ir);
deba@1421
   828
    }
deba@1421
   829
deba@1421
   830
  private:
deba@1421
   831
deba@2386
   832
    template <typename ItemReader, typename Map, typename MapParameter>
deba@2386
   833
    NodeSetReader& _readMap(std::string label, MapParameter map, 
deba@2386
   834
			    const ItemReader& ir = ItemReader()) {
alpar@2260
   835
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@2386
   836
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
deba@2386
   837
      if (readers.find(label) != readers.end()) {
deba@1408
   838
	ErrorMessage msg;
deba@2386
   839
	msg << "Multiple read rule for node map: " << label;
alpar@2153
   840
	throw IoParameterError(msg.message());
deba@1845
   841
      }      
deba@1408
   842
      readers.insert(
deba@2386
   843
        make_pair(label, new _reader_bits::
deba@2386
   844
		  MapReader<Node, Map, ItemReader>(map, ir)));
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@2386
   853
    template <typename ItemReader>
deba@2386
   854
    NodeSetReader& skipNodeMap(std::string label, 
deba@2386
   855
                               const ItemReader& ir = ItemReader()) {
deba@2386
   856
      if (readers.find(label) != readers.end()) {
deba@1408
   857
	ErrorMessage msg;
deba@2386
   858
	msg << "Multiple read rule for node map: " << label;
alpar@2153
   859
	throw IoParameterError(msg.message());
deba@1408
   860
      }
deba@2386
   861
      readers.insert(make_pair(label, new _reader_bits::
deba@2386
   862
			       SkipReader<Node, ItemReader>(ir)));
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@2386
   888
      {
deba@2386
   889
        getline(is, line);
deba@2386
   890
        std::istringstream ls(line);
deba@2386
   891
        std::string id;
deba@2386
   892
        while (ls >> id) {
deba@2386
   893
          typename MapReaders::iterator it = readers.find(id);
deba@2386
   894
          if (it != readers.end()) {
deba@2386
   895
            it->second->touch();
deba@2386
   896
            index.push_back(it->second);
deba@2386
   897
          } else {
deba@2386
   898
            index.push_back(&skipper);
deba@2386
   899
          }
deba@2386
   900
          if (id == "label") {
deba@2386
   901
            inverter.reset(index.back()->getInverter());
deba@2386
   902
            index.back() = inverter.get();
deba@2386
   903
          }
deba@2386
   904
        }
deba@1408
   905
      }
deba@1845
   906
      for (typename MapReaders::iterator it = readers.begin();
deba@1845
   907
	   it != readers.end(); ++it) {
deba@1845
   908
	if (!it->second->touched()) {
deba@1845
   909
	  ErrorMessage msg;
deba@1845
   910
	  msg << "Map not found in file: " << it->first;
alpar@2153
   911
	  throw IoParameterError(msg.message());
deba@1845
   912
	}
deba@1845
   913
      }
deba@1408
   914
      while (getline(is, line)) {	
deba@1429
   915
	Node node = graph.addNode();
deba@1408
   916
	std::istringstream ls(line);
deba@2386
   917
	for (int i = 0; i < int(index.size()); ++i) {
deba@1408
   918
	  index[i]->read(ls, node);
deba@1408
   919
	}
deba@1408
   920
      }
deba@1408
   921
    }
deba@1408
   922
deba@2000
   923
    virtual void missing() {
deba@2000
   924
      if (readers.empty()) return;
deba@2000
   925
      ErrorMessage msg;
deba@2001
   926
      msg << "NodeSet section not found in file: @nodeset " << name;
alpar@2153
   927
      throw IoParameterError(msg.message());
deba@2000
   928
    }
deba@2000
   929
deba@1409
   930
  public:
deba@1409
   931
deba@1901
   932
    /// \brief Returns true if the nodeset can give back the node by its label.
deba@1409
   933
    ///
deba@1901
   934
    /// Returns true if the nodeset can give back the node by its label.
deba@1901
   935
    /// It is possible only if an "label" named map was read.
deba@1901
   936
    bool isLabelReader() const {
deba@1408
   937
      return inverter.get() != 0;
deba@1408
   938
    }
deba@1408
   939
deba@1901
   940
    /// \brief Gives back the node by its label.
deba@1409
   941
    ///
deba@1409
   942
    /// It reads an id from the stream and gives back which node belongs to
deba@1901
   943
    /// it. It is possible only if there was read an "label" named map.
deba@1901
   944
    void readLabel(std::istream& is, Node& node) const {
deba@1492
   945
      node = inverter->read(is);
deba@1408
   946
    } 
deba@1408
   947
deba@1408
   948
  private:
deba@1408
   949
deba@1845
   950
    typedef std::map<std::string, _reader_bits::MapReaderBase<Node>*> MapReaders;
deba@1408
   951
    MapReaders readers;
deba@1408
   952
   
deba@1705
   953
    Graph& graph;   
deba@1901
   954
    std::string name;
deba@1845
   955
    _reader_bits::SkipReader<Node, DefaultSkipper> skipper;
deba@1408
   956
deba@1845
   957
    std::auto_ptr<_reader_bits::MapInverterBase<Node> > inverter;
deba@1408
   958
  };
deba@1408
   959
deba@2016
   960
  /// \ingroup section_io
deba@1409
   961
  /// \brief SectionReader for reading a graph's edgeset.
deba@1409
   962
  ///
deba@1409
   963
  /// The lemon format can store multiple graph edgesets with several maps.
deba@1901
   964
  /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but the
deba@1901
   965
  /// \c edgeset_name may be empty.
deba@1409
   966
  ///
deba@1409
   967
  /// The first line of the section contains the names of the maps separated
deba@1421
   968
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
   969
  /// line contains the source and the target nodes' id and the mapped 
deba@1421
   970
  /// values for each map.
deba@1409
   971
  ///
deba@1901
   972
  /// If the edgeset contains an \c "label" named map then it will be regarded
deba@1409
   973
  /// as id map. This map should contain only unique values and when the 
deba@1901
   974
  /// \c readLabel() member will read a value from the given stream it will
deba@1409
   975
  /// give back that edge which is mapped to this value.
deba@1409
   976
  ///
deba@1409
   977
  /// The edgeset reader needs a node id reader to identify which nodes
deba@1901
   978
  /// have to be connected. If a NodeSetReader reads an "label" named map,
deba@1409
   979
  /// it will be able to resolve the nodes by ids.
deba@1409
   980
  ///
deba@1409
   981
  /// \relates LemonReader
deba@1408
   982
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1845
   983
  class EdgeSetReader : public LemonReader::SectionReader {
deba@1845
   984
    typedef LemonReader::SectionReader Parent;
deba@1408
   985
  public:
deba@1408
   986
deba@1408
   987
    typedef _Graph Graph;
deba@1408
   988
    typedef _Traits Traits;
deba@1429
   989
    typedef typename Graph::Node Node;
deba@1429
   990
    typedef typename Graph::Edge Edge;
deba@1408
   991
    typedef typename Traits::Skipper DefaultSkipper;
deba@1408
   992
deba@1409
   993
    /// \brief Constructor.
deba@1409
   994
    ///
deba@1409
   995
    /// Constructor for EdgeSetReader. It creates the EdgeSetReader and
deba@1409
   996
    /// attach it into the given LemonReader. The edgeset reader will
deba@2100
   997
    /// add the read edges to the given Graph. It will use the given
deba@1409
   998
    /// node id reader to read the source and target nodes of the edges.
deba@1901
   999
    /// The reader will read the section only if the \c _name and the 
deba@1901
  1000
    /// \c edgset_name are the same. 
deba@1901
  1001
    template <typename NodeLabelReader>
deba@1421
  1002
    EdgeSetReader(LemonReader& _reader, 
deba@1705
  1003
		  Graph& _graph, 
deba@1901
  1004
		  const NodeLabelReader& _nodeLabelReader, 
deba@1901
  1005
		  const std::string& _name = std::string(),
deba@1409
  1006
		  const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1901
  1007
      : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
deba@1901
  1008
      checkConcept<_reader_bits::ItemLabelReader<Node>, NodeLabelReader>();
deba@1901
  1009
      nodeLabelReader.reset(new _reader_bits::
deba@1901
  1010
			 LabelReader<Node, NodeLabelReader>(_nodeLabelReader));
deba@1476
  1011
    }
deba@1409
  1012
    /// \brief Destructor.
deba@1409
  1013
    ///
deba@1409
  1014
    /// Destructor for EdgeSetReader.
deba@1408
  1015
    virtual ~EdgeSetReader() {
deba@1408
  1016
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1408
  1017
	   it != readers.end(); ++it) {
deba@1408
  1018
	delete it->second;
deba@1408
  1019
      }
deba@1408
  1020
    }
deba@1408
  1021
deba@1408
  1022
  private:
deba@1408
  1023
    EdgeSetReader(const EdgeSetReader&);
deba@1408
  1024
    void operator=(const EdgeSetReader&);
deba@1408
  1025
deba@1408
  1026
  public:
deba@1408
  1027
deba@1409
  1028
    /// \brief Add a new edge map reader command for the reader.
deba@1408
  1029
    ///
deba@1409
  1030
    /// Add a new edge map reader command for the reader.
deba@1408
  1031
    template <typename Map>
deba@2386
  1032
    EdgeSetReader& readEdgeMap(std::string label, Map& map) {
deba@1421
  1033
      return _readMap<
deba@1421
  1034
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
  1035
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
  1036
    }
deba@1421
  1037
deba@1421
  1038
    template <typename Map>
deba@2386
  1039
    EdgeSetReader& readEdgeMap(std::string label, const Map& map) {
deba@1421
  1040
      return _readMap<
deba@1421
  1041
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
  1042
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1408
  1043
    }
deba@1408
  1044
deba@1409
  1045
    /// \brief Add a new edge map reader command for the reader.
deba@1408
  1046
    ///
deba@1409
  1047
    /// Add a new edge map reader command for the reader.
deba@2386
  1048
    template <typename ItemReader, typename Map>
deba@2386
  1049
    EdgeSetReader& readEdgeMap(std::string label, Map& map, 
deba@2386
  1050
                               const ItemReader& ir = ItemReader()) {
deba@2386
  1051
      return _readMap<ItemReader, Map,
deba@2386
  1052
	typename _reader_bits::Arg<Map>::Type>(label, map, ir);
deba@1421
  1053
    }
deba@1421
  1054
deba@2386
  1055
    template <typename ItemReader, typename Map>
deba@2386
  1056
    EdgeSetReader& readEdgeMap(std::string label, const Map& map, 
deba@2386
  1057
			       const ItemReader& ir = ItemReader()) {
deba@2386
  1058
      return _readMap<ItemReader, Map,
deba@2386
  1059
	typename _reader_bits::Arg<Map>::Type>(label, map, ir);
deba@1421
  1060
    }
deba@1421
  1061
deba@1421
  1062
  private:
deba@1421
  1063
deba@2386
  1064
    template <typename ItemReader, typename Map, typename MapParameter>
deba@2386
  1065
    EdgeSetReader& _readMap(std::string label, MapParameter map, 
deba@2386
  1066
			    const ItemReader& ir = ItemReader()) {
alpar@2260
  1067
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@2386
  1068
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
deba@2386
  1069
      if (readers.find(label) != readers.end()) {
deba@1408
  1070
	ErrorMessage msg;
deba@2386
  1071
	msg << "Multiple read rule for edge map: " << label;
alpar@2153
  1072
	throw IoParameterError(msg.message());
deba@1408
  1073
      }
deba@1408
  1074
      readers.insert(
deba@2386
  1075
	make_pair(label, new _reader_bits::
deba@2386
  1076
		  MapReader<Edge, Map, ItemReader>(map, ir)));
deba@1408
  1077
      return *this;
deba@1408
  1078
    }
deba@1408
  1079
deba@1421
  1080
  public:
deba@1421
  1081
deba@1409
  1082
    /// \brief Add a new edge map skipper command for the reader.
deba@1408
  1083
    ///
deba@1409
  1084
    /// Add a new edge map skipper command for the reader.
deba@2386
  1085
    template <typename ItemReader>
deba@2386
  1086
    EdgeSetReader& skipEdgeMap(std::string label, 
deba@2386
  1087
			       const ItemReader& ir = ItemReader()) {
deba@2386
  1088
      if (readers.find(label) != readers.end()) {
deba@1408
  1089
	ErrorMessage msg;
deba@2386
  1090
	msg << "Multiple read rule for edge map: " << label;
alpar@2153
  1091
	throw IoParameterError(msg.message());
deba@1408
  1092
      }
deba@2386
  1093
      readers.insert(make_pair(label, new _reader_bits::
deba@2386
  1094
			       SkipReader<Edge, ItemReader>(ir)));
deba@1408
  1095
      return *this;
deba@1408
  1096
    }
deba@1408
  1097
deba@1409
  1098
  protected:
deba@1409
  1099
deba@1409
  1100
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1101
    /// the section with the given header line.
deba@1409
  1102
    ///
deba@1421
  1103
    /// It gives back true when the header line starts with \c \@edgeset,
deba@1901
  1104
    /// and the header line's name and the edgeset's name are the same.
deba@1408
  1105
    virtual bool header(const std::string& line) {
deba@1408
  1106
      std::istringstream ls(line);
deba@1408
  1107
      std::string command;
deba@1901
  1108
      std::string id;
klao@1997
  1109
      ls >> command >> id;
deba@1408
  1110
      return command == "@edgeset" && name == id;
deba@1408
  1111
    }
deba@1408
  1112
deba@1409
  1113
    /// \brief Reader function of the section.
deba@1409
  1114
    ///
deba@1409
  1115
    /// It reads the content of the section.
deba@1408
  1116
    virtual void read(std::istream& is) {
deba@1901
  1117
      if (!nodeLabelReader->isLabelReader()) {
deba@1901
  1118
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1119
      }
deba@1845
  1120
      std::vector<_reader_bits::MapReaderBase<Edge>* > index;
deba@1408
  1121
      std::string line;
deba@1408
  1122
deba@2386
  1123
      {
deba@2386
  1124
        getline(is, line);
deba@2386
  1125
        std::istringstream ls(line);	
deba@2386
  1126
        std::string id;
deba@2386
  1127
        while (ls >> id) {
deba@2386
  1128
          typename MapReaders::iterator it = readers.find(id);
deba@2386
  1129
          if (it != readers.end()) {
deba@2386
  1130
            index.push_back(it->second);
deba@2386
  1131
            it->second->touch();
deba@2386
  1132
          } else {
deba@2386
  1133
            index.push_back(&skipper);
deba@2386
  1134
          }
deba@2386
  1135
          if (id == "label") {
deba@2386
  1136
            inverter.reset(index.back()->getInverter());
deba@2386
  1137
            index.back() = inverter.get();
deba@2386
  1138
          }
deba@2386
  1139
        }
deba@1408
  1140
      }
deba@1845
  1141
      for (typename MapReaders::iterator it = readers.begin();
deba@1845
  1142
	   it != readers.end(); ++it) {
deba@1845
  1143
	if (!it->second->touched()) {
deba@1845
  1144
	  ErrorMessage msg;
deba@1845
  1145
	  msg << "Map not found in file: " << it->first;
alpar@2153
  1146
	  throw IoParameterError(msg.message());
deba@1845
  1147
	}
deba@1845
  1148
      }
deba@1408
  1149
      while (getline(is, line)) {	
deba@1408
  1150
	std::istringstream ls(line);
deba@1901
  1151
	Node from = nodeLabelReader->read(ls);
deba@1901
  1152
	Node to = nodeLabelReader->read(ls);
deba@1429
  1153
	Edge edge = graph.addEdge(from, to);
deba@2386
  1154
	for (int i = 0; i < int(index.size()); ++i) {
deba@1408
  1155
	  index[i]->read(ls, edge);
deba@1408
  1156
	}
deba@1408
  1157
      }
deba@1408
  1158
    }
deba@1408
  1159
deba@2000
  1160
    virtual void missing() {
deba@2000
  1161
      if (readers.empty()) return;
deba@2000
  1162
      ErrorMessage msg;
deba@2001
  1163
      msg << "EdgeSet section not found in file: @edgeset " << name;
alpar@2153
  1164
      throw IoParameterError(msg.message());
deba@2000
  1165
    }
deba@2000
  1166
deba@1409
  1167
  public:
deba@1409
  1168
deba@1901
  1169
    /// \brief Returns true if the edgeset can give back the edge by its label.
deba@1409
  1170
    ///
deba@1901
  1171
    /// Returns true if the edgeset can give back the edge by its label.
deba@1901
  1172
    /// It is possible only if an "label" named map was read.
deba@1901
  1173
    bool isLabelReader() const {
deba@1408
  1174
      return inverter.get() != 0;
deba@1408
  1175
    }
deba@1408
  1176
deba@1901
  1177
    /// \brief Gives back the edge by its label.
deba@1409
  1178
    ///
deba@1409
  1179
    /// It reads an id from the stream and gives back which edge belongs to
deba@1901
  1180
    /// it. It is possible only if there was read an "label" named map.
deba@1901
  1181
    void readLabel(std::istream& is, Edge& edge) const {
deba@1492
  1182
      edge = inverter->read(is);
deba@1408
  1183
    } 
deba@1408
  1184
deba@1408
  1185
  private:
deba@1408
  1186
deba@2001
  1187
    typedef std::map<std::string, _reader_bits::MapReaderBase<Edge>*> 
deba@2001
  1188
    MapReaders;
deba@2001
  1189
    
deba@1408
  1190
    MapReaders readers;
deba@1408
  1191
   
deba@1705
  1192
    Graph& graph;   
deba@1901
  1193
    std::string name;
deba@1845
  1194
    _reader_bits::SkipReader<Edge, DefaultSkipper> skipper;
deba@1421
  1195
deba@1845
  1196
    std::auto_ptr<_reader_bits::MapInverterBase<Edge> > inverter;
deba@1901
  1197
    std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader;
deba@1421
  1198
  };
deba@1421
  1199
deba@2016
  1200
  /// \ingroup section_io
deba@1421
  1201
  /// \brief SectionReader for reading a undirected graph's edgeset.
deba@1421
  1202
  ///
deba@1421
  1203
  /// The lemon format can store multiple undirected edgesets with several 
klao@1909
  1204
  /// maps. The undirected edgeset section's header line is \c \@uedgeset 
klao@1909
  1205
  /// \c uedgeset_name, but the \c uedgeset_name may be empty.
deba@1421
  1206
  ///
deba@1421
  1207
  /// The first line of the section contains the names of the maps separated
deba@1421
  1208
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
  1209
  /// line contains the connected nodes' id and the mapped values for each map.
deba@1421
  1210
  ///
deba@1421
  1211
  /// The section can handle the directed as a syntactical sugar. Two
deba@1421
  1212
  /// undirected edge map describes one directed edge map. This two maps
deba@1421
  1213
  /// are the forward map and the backward map and the names of this map
deba@1421
  1214
  /// is near the same just with a prefix \c '+' or \c '-' character 
deba@1421
  1215
  /// difference.
deba@1421
  1216
  ///
deba@1901
  1217
  /// If the edgeset contains an \c "label" named map then it will be regarded
deba@1421
  1218
  /// as id map. This map should contain only unique values and when the 
deba@1901
  1219
  /// \c readLabel() member will read a value from the given stream it will
klao@1909
  1220
  /// give back that uicted edge which is mapped to this value.
deba@1421
  1221
  ///
deba@1421
  1222
  /// The undirected edgeset reader needs a node id reader to identify which 
deba@2001
  1223
  /// nodes have to be connected. If a NodeSetReader reads an "label" named 
deba@2001
  1224
  /// map, it will be able to resolve the nodes by ids.
deba@1421
  1225
  ///
deba@1421
  1226
  /// \relates LemonReader
deba@1421
  1227
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
klao@1909
  1228
  class UEdgeSetReader : public LemonReader::SectionReader {
deba@1845
  1229
    typedef LemonReader::SectionReader Parent;
deba@1421
  1230
  public:
deba@1421
  1231
deba@1421
  1232
    typedef _Graph Graph;
deba@1421
  1233
    typedef _Traits Traits;
deba@1429
  1234
    typedef typename Graph::Node Node;
deba@1429
  1235
    typedef typename Graph::Edge Edge;
klao@1909
  1236
    typedef typename Graph::UEdge UEdge;
deba@1421
  1237
    typedef typename Traits::Skipper DefaultSkipper;
deba@1421
  1238
deba@1421
  1239
    /// \brief Constructor.
deba@1421
  1240
    ///
klao@1909
  1241
    /// Constructor for UEdgeSetReader. It creates the UEdgeSetReader 
deba@1421
  1242
    /// and attach it into the given LemonReader. The undirected edgeset 
deba@2100
  1243
    /// reader will add the read undirected edges to the given Graph. It 
deba@1421
  1244
    /// will use the given node id reader to read the source and target 
deba@1421
  1245
    /// nodes of the edges. The reader will read the section only if the 
klao@1909
  1246
    /// \c _name and the \c uedgset_name are the same. 
deba@1901
  1247
    template <typename NodeLabelReader>
klao@1909
  1248
    UEdgeSetReader(LemonReader& _reader, 
deba@1705
  1249
		       Graph& _graph, 
deba@1901
  1250
		       const NodeLabelReader& _nodeLabelReader, 
deba@1901
  1251
		       const std::string& _name = std::string(),
deba@1421
  1252
		       const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1901
  1253
      : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
deba@1901
  1254
      checkConcept<_reader_bits::ItemLabelReader<Node>, NodeLabelReader>();
deba@1901
  1255
      nodeLabelReader.reset(new _reader_bits::
deba@1901
  1256
			 LabelReader<Node, NodeLabelReader>(_nodeLabelReader));
deba@1476
  1257
    }
deba@1421
  1258
    /// \brief Destructor.
deba@1421
  1259
    ///
klao@1909
  1260
    /// Destructor for UEdgeSetReader.
klao@1909
  1261
    virtual ~UEdgeSetReader() {
deba@1421
  1262
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1421
  1263
	   it != readers.end(); ++it) {
deba@1421
  1264
	delete it->second;
deba@1421
  1265
      }
deba@1421
  1266
    }
deba@1421
  1267
deba@1421
  1268
  private:
klao@1909
  1269
    UEdgeSetReader(const UEdgeSetReader&);
klao@1909
  1270
    void operator=(const UEdgeSetReader&);
deba@1421
  1271
deba@1421
  1272
  public:
deba@1421
  1273
deba@1421
  1274
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1275
    ///
deba@1421
  1276
    /// Add a new edge undirected map reader command for the reader.
deba@1421
  1277
    template <typename Map>
deba@2386
  1278
    UEdgeSetReader& readUEdgeMap(std::string label, Map& map) {
deba@1421
  1279
      return _readMap<
deba@1421
  1280
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@2386
  1281
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
  1282
    }
deba@1421
  1283
deba@1421
  1284
    template <typename Map>
deba@2386
  1285
    UEdgeSetReader& readUEdgeMap(std::string label, const Map& map) {
deba@1421
  1286
      return _readMap<
deba@1421
  1287
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@2386
  1288
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
  1289
    }
deba@1421
  1290
deba@1421
  1291
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1292
    ///
deba@1421
  1293
    /// Add a new edge undirected map reader command for the reader.
deba@2386
  1294
    template <typename ItemReader, typename Map>
deba@2386
  1295
    UEdgeSetReader& readUEdgeMap(std::string label, Map& map, 
deba@2386
  1296
                                 const ItemReader& ir = ItemReader()) {
deba@2386
  1297
      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
deba@2386
  1298
	(label, map, ir);
deba@1421
  1299
    }
deba@1421
  1300
deba@2386
  1301
    template <typename ItemReader, typename Map>
deba@2386
  1302
    UEdgeSetReader& readUEdgeMap(std::string label, const Map& map, 
deba@2386
  1303
                                 const ItemReader& ir = ItemReader()) {
deba@2386
  1304
      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type >
deba@2386
  1305
	(label, map, ir);
deba@1421
  1306
    }
deba@1421
  1307
deba@1421
  1308
  private:
deba@1421
  1309
deba@2386
  1310
    template <typename ItemReader, typename Map, typename MapParameter>
deba@2386
  1311
    UEdgeSetReader& _readMap(std::string label, MapParameter map,
deba@2386
  1312
                             const ItemReader& ir = ItemReader()) {
alpar@2260
  1313
      checkConcept<concepts::WriteMap<UEdge, typename Map::Value>, Map>();
deba@2386
  1314
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
deba@2386
  1315
      if (readers.find(label) != readers.end()) {
deba@1421
  1316
	ErrorMessage msg;
deba@2386
  1317
	msg << "Multiple read rule for edge map: " << label;
alpar@2153
  1318
	throw IoParameterError(msg.message());
deba@1421
  1319
      }
deba@1421
  1320
      readers.insert(
deba@2386
  1321
	make_pair(label, new _reader_bits::
deba@2386
  1322
		  MapReader<UEdge, Map, ItemReader>(map, ir)));
deba@1421
  1323
      return *this;
deba@1421
  1324
    }
deba@1421
  1325
deba@1421
  1326
  public:
deba@1421
  1327
deba@1421
  1328
    /// \brief Add a new undirected edge map skipper command for the reader.
deba@1421
  1329
    ///
deba@1421
  1330
    /// Add a new undirected edge map skipper command for the reader.
deba@2386
  1331
    template <typename ItemReader>
deba@2386
  1332
    UEdgeSetReader& skipUEdgeMap(std::string label, 
deba@2386
  1333
                                 const ItemReader& ir = ItemReader()) {
deba@2386
  1334
      if (readers.find(label) != readers.end()) {
deba@1421
  1335
	ErrorMessage msg;
deba@2386
  1336
	msg << "Multiple read rule for node map: " << label;
alpar@2153
  1337
	throw IoParameterError(msg.message());
deba@1421
  1338
      }
deba@2386
  1339
      readers.insert(make_pair(label, new _reader_bits::
deba@2386
  1340
			       SkipReader<UEdge, ItemReader>(ir)));
deba@1421
  1341
      return *this;
deba@1421
  1342
    }
deba@1421
  1343
deba@1421
  1344
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1345
    ///
deba@1421
  1346
    /// Add a new directed edge map reader command for the reader.
deba@1421
  1347
    template <typename Map>
deba@2386
  1348
    UEdgeSetReader& readEdgeMap(std::string label, Map& map) {
deba@1421
  1349
      return _readDirMap<
deba@1421
  1350
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
  1351
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
  1352
    }
deba@1421
  1353
deba@1421
  1354
    template <typename Map>
deba@2386
  1355
    UEdgeSetReader& readEdgeMap(std::string label, const Map& map) {
deba@1421
  1356
      return _readDirMap<
deba@1421
  1357
	typename Traits::template Reader<typename Map::Value>, Map,
deba@2386
  1358
	typename _reader_bits::Arg<Map>::Type>(label, map);
deba@1421
  1359
    }
deba@1421
  1360
deba@1421
  1361
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1362
    ///
deba@1421
  1363
    /// Add a new directed edge map reader command for the reader.
deba@2386
  1364
    template <typename ItemReader, typename Map>
deba@2386
  1365
    UEdgeSetReader& readEdgeMap(std::string label, Map& map, 
deba@2386
  1366
				    const ItemReader& ir = ItemReader()) {
deba@2386
  1367
      return _readDirMap<ItemReader, Map, 
deba@2386
  1368
        typename _reader_bits::Arg<Map>::Type>(label, map, ir);
deba@1421
  1369
    }
deba@1421
  1370
deba@2386
  1371
    template <typename ItemReader, typename Map>
deba@2386
  1372
    UEdgeSetReader& readEdgeMap(std::string label, const Map& map, 
deba@2386
  1373
				    const ItemReader& ir = ItemReader()) {
deba@2386
  1374
      return _readDirMap<ItemReader, Map, 
deba@2386
  1375
        typename _reader_bits::Arg<Map>::Type>(label, map, ir);
deba@1421
  1376
    }
deba@1421
  1377
deba@1421
  1378
  private:
deba@1421
  1379
deba@2386
  1380
    template <typename ItemReader, typename Map, typename MapParameter>
deba@2386
  1381
    UEdgeSetReader& _readDirMap(std::string label, MapParameter map,
deba@2386
  1382
				    const ItemReader& ir = ItemReader()) { 
deba@2386
  1383
      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
alpar@2260
  1384
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@2386
  1385
      readUEdgeMap("+" + label, 
deba@2386
  1386
                   _reader_bits::forwardComposeMap(graph, map), ir);
deba@2386
  1387
      readUEdgeMap("-" + label, 
deba@2386
  1388
                   _reader_bits::backwardComposeMap(graph, map), ir);
deba@1421
  1389
      return *this;      
deba@1421
  1390
    }
deba@1421
  1391
deba@1421
  1392
  public:
deba@1421
  1393
deba@1421
  1394
    /// \brief Add a new directed edge map skipper command for the reader.
deba@1421
  1395
    ///
deba@1421
  1396
    /// Add a new directed edge map skipper command for the reader.
deba@2386
  1397
    template <typename ItemReader>
deba@2386
  1398
    UEdgeSetReader& skipEdgeMap(std::string label, 
deba@2386
  1399
                                const ItemReader& ir = ItemReader()) {
deba@2386
  1400
      skipUEdgeMap("+" + label, ir);
deba@2386
  1401
      skipUEdgeMap("-" + label, ir);
deba@1421
  1402
      return *this;
deba@1421
  1403
    }
deba@1421
  1404
deba@1421
  1405
  protected:
deba@1421
  1406
deba@1421
  1407
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1408
    /// the section with the given header line.
deba@1421
  1409
    ///
klao@1909
  1410
    /// It gives back true when the header line starts with \c \@uedgeset,
deba@1901
  1411
    /// and the header line's name and the edgeset's name are the same.
deba@1421
  1412
    virtual bool header(const std::string& line) {
deba@1421
  1413
      std::istringstream ls(line);
deba@1421
  1414
      std::string command;
deba@1901
  1415
      std::string id;
klao@1997
  1416
      ls >> command >> id;
klao@1909
  1417
      return command == "@uedgeset" && name == id;
deba@1421
  1418
    }
deba@1421
  1419
deba@1421
  1420
    /// \brief Reader function of the section.
deba@1421
  1421
    ///
deba@1421
  1422
    /// It reads the content of the section.
deba@1421
  1423
    virtual void read(std::istream& is) {
deba@1901
  1424
      if (!nodeLabelReader->isLabelReader()) {
deba@1901
  1425
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1426
      }
klao@1909
  1427
      std::vector<_reader_bits::MapReaderBase<UEdge>* > index;
deba@1421
  1428
      std::string line;
deba@1421
  1429
deba@2386
  1430
      {
deba@2386
  1431
        getline(is, line);
deba@2386
  1432
        std::istringstream ls(line);	
deba@2386
  1433
        std::string id;
deba@2386
  1434
        while (ls >> id) {
deba@2386
  1435
          typename MapReaders::iterator it = readers.find(id);
deba@2386
  1436
          if (it != readers.end()) {
deba@2386
  1437
            index.push_back(it->second);
deba@2386
  1438
            it->second->touch();
deba@2386
  1439
          } else {
deba@2386
  1440
            index.push_back(&skipper);
deba@2386
  1441
          }
deba@2386
  1442
          if (id == "label") {
deba@2386
  1443
            inverter.reset(index.back()->getInverter());
deba@2386
  1444
            index.back() = inverter.get();
deba@2386
  1445
          }
deba@2386
  1446
        }
deba@2386
  1447
        for (typename MapReaders::iterator it = readers.begin();
deba@2386
  1448
             it != readers.end(); ++it) {
deba@2386
  1449
          if (!it->second->touched()) {
deba@2386
  1450
            ErrorMessage msg;
deba@2386
  1451
            msg << "Map not found in file: " << it->first;
deba@2386
  1452
            throw IoParameterError(msg.message());
deba@2386
  1453
          }
deba@2386
  1454
        }
deba@1845
  1455
      }
deba@1421
  1456
      while (getline(is, line)) {	
deba@1421
  1457
	std::istringstream ls(line);
deba@1901
  1458
	Node from = nodeLabelReader->read(ls);
deba@1901
  1459
	Node to = nodeLabelReader->read(ls);
klao@1909
  1460
	UEdge edge = graph.addEdge(from, to);
deba@2386
  1461
	for (int i = 0; i < int(index.size()); ++i) {
deba@1421
  1462
	  index[i]->read(ls, edge);
deba@1421
  1463
	}
deba@1421
  1464
      }
deba@1421
  1465
    }
deba@1421
  1466
deba@2000
  1467
    virtual void missing() {
deba@2000
  1468
      if (readers.empty()) return;
deba@2000
  1469
      ErrorMessage msg;
deba@2001
  1470
      msg << "UEdgeSet section not found in file: @uedgeset " << name;
alpar@2153
  1471
      throw IoParameterError(msg.message());
deba@2000
  1472
    }
deba@2000
  1473
deba@1421
  1474
  public:
deba@1421
  1475
deba@1901
  1476
    /// \brief Returns true if the edgeset can give back the edge by its label.
deba@1421
  1477
    ///
deba@1421
  1478
    /// Returns true if the edgeset can give back the undirected edge by its 
deba@1901
  1479
    /// id. It is possible only if an "label" named map was read.
deba@1901
  1480
    bool isLabelReader() const {
deba@1421
  1481
      return inverter.get() != 0;
deba@1421
  1482
    }
deba@1421
  1483
deba@1901
  1484
    /// \brief Gives back the undirected edge by its label.
deba@1421
  1485
    ///
deba@1421
  1486
    /// It reads an id from the stream and gives back which undirected edge 
deba@1901
  1487
    /// belongs to it. It is possible only if there was read an "label" named map.
klao@1909
  1488
    void readLabel(std::istream& is, UEdge& uEdge) const {
klao@1909
  1489
      uEdge = inverter->read(is);
deba@1421
  1490
    } 
deba@1421
  1491
deba@1901
  1492
    /// \brief Gives back the directed edge by its label.
deba@1429
  1493
    ///
deba@1429
  1494
    /// It reads an id from the stream and gives back which directed edge 
deba@1429
  1495
    /// belongs to it. The directed edge id is the \c '+' or \c '-' character
deba@1429
  1496
    /// and the undirected edge id. It is possible only if there was read 
deba@1901
  1497
    /// an "label" named map.
deba@1901
  1498
    void readLabel(std::istream& is, Edge& edge) const {
deba@1429
  1499
      char c;
deba@1429
  1500
      is >> c;
klao@1909
  1501
      UEdge uEdge = inverter->read(is);
deba@1429
  1502
      if (c == '+') {
klao@1909
  1503
	edge = graph.direct(uEdge, true);
deba@1429
  1504
      } else if (c == '-') {
klao@1909
  1505
        edge = graph.direct(uEdge, false);
deba@1429
  1506
      } else {
deba@1429
  1507
	throw DataFormatError("Wrong id format for edge "
deba@1429
  1508
			      "in undirected edgeset");
deba@1429
  1509
      }
deba@1429
  1510
    } 
deba@1429
  1511
deba@1421
  1512
  private:
deba@1421
  1513
deba@1845
  1514
    typedef std::map<std::string, 
klao@1909
  1515
		     _reader_bits::MapReaderBase<UEdge>*> MapReaders;
deba@1421
  1516
    MapReaders readers;
deba@1421
  1517
   
deba@1705
  1518
    Graph& graph;   
deba@1901
  1519
    std::string name;
klao@1909
  1520
    _reader_bits::SkipReader<UEdge, DefaultSkipper> skipper;
deba@1408
  1521
klao@1909
  1522
    std::auto_ptr<_reader_bits::MapInverterBase<UEdge> > inverter;
deba@1901
  1523
    std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader;
deba@1408
  1524
  };
deba@1408
  1525
deba@2016
  1526
  /// \ingroup section_io
deba@1409
  1527
  /// \brief SectionReader for reading labeled nodes.
deba@1409
  1528
  ///
deba@1901
  1529
  /// The nodes section's header line is \c \@nodes \c nodes_name, but the
deba@1901
  1530
  /// \c nodes_name may be empty.
deba@1409
  1531
  ///
deba@1409
  1532
  /// Each line in the section contains the name of the node 
deba@1409
  1533
  /// and then the node id. 
deba@1409
  1534
  ///
deba@1409
  1535
  /// \relates LemonReader
deba@1409
  1536
  template <typename _Graph>
deba@1845
  1537
  class NodeReader : public LemonReader::SectionReader {
deba@1845
  1538
    typedef LemonReader::SectionReader Parent;
deba@1409
  1539
    typedef _Graph Graph;
deba@1429
  1540
    typedef typename Graph::Node Node;
deba@1409
  1541
  public:
deba@1409
  1542
    
deba@1409
  1543
    /// \brief Constructor.
deba@1409
  1544
    ///
deba@1409
  1545
    /// Constructor for NodeReader. It creates the NodeReader and
deba@1409
  1546
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1547
    /// node id reader to give back the nodes. The reader will read the 
deba@1901
  1548
    /// section only if the \c _name and the \c nodes_name are the same. 
deba@1901
  1549
    template <typename _LabelReader>
deba@1901
  1550
    NodeReader(LemonReader& _reader, const _LabelReader& _labelReader, 
deba@1901
  1551
	       const std::string& _name = std::string()) 
deba@1901
  1552
      : Parent(_reader), name(_name) {
deba@1901
  1553
      checkConcept<_reader_bits::ItemLabelReader<Node>, _LabelReader>();
deba@1901
  1554
      nodeLabelReader.reset(new _reader_bits::
deba@1901
  1555
			 LabelReader<Node, _LabelReader>(_labelReader));
deba@1476
  1556
    }
deba@1408
  1557
deba@1409
  1558
    /// \brief Destructor.
deba@1409
  1559
    ///
deba@1409
  1560
    /// Destructor for NodeReader.
deba@1409
  1561
    virtual ~NodeReader() {}
deba@1409
  1562
deba@1409
  1563
  private:
deba@1409
  1564
    NodeReader(const NodeReader&);
deba@1409
  1565
    void operator=(const NodeReader&);
deba@1409
  1566
deba@1409
  1567
  public:
deba@1409
  1568
deba@1409
  1569
    /// \brief Add a node reader command for the NodeReader.
deba@1409
  1570
    ///
deba@1409
  1571
    /// Add a node reader command for the NodeReader.
deba@2386
  1572
    void readNode(std::string label, Node& item) {
deba@2386
  1573
      if (readers.find(label) != readers.end()) {
deba@1409
  1574
	ErrorMessage msg;
deba@2386
  1575
	msg << "Multiple read rule for node: " << label;
alpar@2153
  1576
	throw IoParameterError(msg.message());
deba@1409
  1577
      }
deba@2386
  1578
      readers.insert(make_pair(label, _reader_bits::ItemStore<Node>(item)));
deba@1409
  1579
    }
deba@1409
  1580
deba@1409
  1581
  protected:
deba@1409
  1582
deba@1409
  1583
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1584
    /// the section with the given header line.
deba@1409
  1585
    ///
deba@1421
  1586
    /// It gives back true when the header line start with \c \@nodes,
deba@1901
  1587
    /// and the header line's name and the reader's name are the same.
deba@1409
  1588
    virtual bool header(const std::string& line) {
deba@1409
  1589
      std::istringstream ls(line);
deba@1409
  1590
      std::string command;
deba@1901
  1591
      std::string id;
klao@1997
  1592
      ls >> command >> id;
deba@1409
  1593
      return command == "@nodes" && name == id;
deba@1409
  1594
    }
deba@1409
  1595
deba@1409
  1596
    /// \brief Reader function of the section.
deba@1409
  1597
    ///
deba@1409
  1598
    /// It reads the content of the section.
deba@1409
  1599
    virtual void read(std::istream& is) {
deba@1901
  1600
      if (!nodeLabelReader->isLabelReader()) {
deba@1901
  1601
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1602
      }
deba@1409
  1603
      std::string line;
deba@1409
  1604
      while (getline(is, line)) {
deba@1409
  1605
	std::istringstream ls(line);
deba@1409
  1606
	std::string id;
deba@1409
  1607
	ls >> id;
deba@1429
  1608
	typename NodeReaders::iterator it = readers.find(id);
deba@1409
  1609
	if (it != readers.end()) {
deba@1901
  1610
	  it->second.read(nodeLabelReader->read(ls));
deba@1845
  1611
	  it->second.touch();
deba@1409
  1612
	}	
deba@1409
  1613
      }
deba@1845
  1614
      for (typename NodeReaders::iterator it = readers.begin();
deba@1845
  1615
	   it != readers.end(); ++it) {
deba@1845
  1616
	if (!it->second.touched()) {
deba@1845
  1617
	  ErrorMessage msg;
deba@1845
  1618
	  msg << "Node not found in file: " << it->first;
alpar@2153
  1619
	  throw IoParameterError(msg.message());
deba@1845
  1620
	}
deba@1845
  1621
      }
deba@1409
  1622
    }
deba@2000
  1623
deba@2000
  1624
    virtual void missing() {
deba@2000
  1625
      if (readers.empty()) return;
deba@2000
  1626
      ErrorMessage msg;
deba@2001
  1627
      msg << "Nodes section not found in file: @nodes " << name;
alpar@2153
  1628
      throw IoParameterError(msg.message());
deba@2000
  1629
    }
deba@1409
  1630
    
deba@1409
  1631
  private:
deba@1409
  1632
deba@1901
  1633
    std::string name;
deba@1409
  1634
deba@1845
  1635
    typedef std::map<std::string, _reader_bits::ItemStore<Node> > NodeReaders;
deba@1429
  1636
    NodeReaders readers;
deba@1901
  1637
    std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader;
deba@1409
  1638
  };
deba@1409
  1639
deba@2016
  1640
  /// \ingroup section_io
deba@1409
  1641
  /// \brief SectionReader for reading labeled edges.
deba@1409
  1642
  ///
deba@1901
  1643
  /// The edges section's header line is \c \@edges \c edges_name, but the
deba@1901
  1644
  /// \c edges_name may be empty.
deba@1409
  1645
  ///
deba@1409
  1646
  /// Each line in the section contains the name of the edge 
deba@1409
  1647
  /// and then the edge id. 
deba@1409
  1648
  ///
deba@1409
  1649
  /// \relates LemonReader
deba@1409
  1650
  template <typename _Graph>
deba@1845
  1651
  class EdgeReader : public LemonReader::SectionReader {
deba@1845
  1652
    typedef LemonReader::SectionReader Parent;
deba@1409
  1653
    typedef _Graph Graph;
deba@1429
  1654
    typedef typename Graph::Edge Edge;
deba@1409
  1655
  public:
deba@1409
  1656
    
deba@1409
  1657
    /// \brief Constructor.
deba@1409
  1658
    ///
deba@1409
  1659
    /// Constructor for EdgeReader. It creates the EdgeReader and
deba@1409
  1660
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1661
    /// edge id reader to give back the edges. The reader will read the 
deba@1901
  1662
    /// section only if the \c _name and the \c edges_name are the same. 
deba@1901
  1663
    template <typename _LabelReader>
deba@1901
  1664
    EdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 
deba@1901
  1665
	       const std::string& _name = std::string()) 
deba@1901
  1666
      : Parent(_reader), name(_name) {
deba@1901
  1667
      checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>();
deba@1901
  1668
      edgeLabelReader.reset(new _reader_bits::
deba@1901
  1669
			 LabelReader<Edge, _LabelReader>(_labelReader));
deba@1476
  1670
    }
deba@1409
  1671
deba@1409
  1672
    /// \brief Destructor.
deba@1409
  1673
    ///
deba@1409
  1674
    /// Destructor for EdgeReader.
deba@1409
  1675
    virtual ~EdgeReader() {}
deba@1409
  1676
  private:
deba@1409
  1677
    EdgeReader(const EdgeReader&);
deba@1409
  1678
    void operator=(const EdgeReader&);
deba@1409
  1679
deba@1409
  1680
  public:
deba@1409
  1681
deba@1409
  1682
    /// \brief Add an edge reader command for the EdgeReader.
deba@1409
  1683
    ///
deba@1409
  1684
    /// Add an edge reader command for the EdgeReader.
deba@2386
  1685
    void readEdge(std::string label, Edge& item) {
deba@2386
  1686
      if (readers.find(label) != readers.end()) {
deba@1409
  1687
	ErrorMessage msg;
deba@2386
  1688
	msg << "Multiple read rule for edge: " << label;
alpar@2153
  1689
	throw IoParameterError(msg.message());
deba@1409
  1690
      }
deba@2386
  1691
      readers.insert(make_pair(label, _reader_bits::ItemStore<Edge>(item)));
deba@1409
  1692
    }
deba@1409
  1693
deba@1409
  1694
  protected:
deba@1409
  1695
deba@1409
  1696
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1697
    /// the section with the given header line.
deba@1409
  1698
    ///
deba@1421
  1699
    /// It gives back true when the header line start with \c \@edges,
deba@1901
  1700
    /// and the header line's name and the reader's name are the same.
deba@1421
  1701
    virtual bool header(const std::string& line) {
deba@1421
  1702
      std::istringstream ls(line);
deba@1421
  1703
      std::string command;
deba@1901
  1704
      std::string id;
klao@1997
  1705
      ls >> command >> id;
deba@1421
  1706
      return command == "@edges" && name == id;
deba@1421
  1707
    }
deba@1421
  1708
deba@1421
  1709
    /// \brief Reader function of the section.
deba@1421
  1710
    ///
deba@1421
  1711
    /// It reads the content of the section.
deba@1421
  1712
    virtual void read(std::istream& is) {
deba@1901
  1713
      if (!edgeLabelReader->isLabelReader()) {
deba@1901
  1714
	throw DataFormatError("Cannot find edgeset or label map");
deba@1476
  1715
      }
deba@1421
  1716
      std::string line;
deba@1421
  1717
      while (getline(is, line)) {
deba@1421
  1718
	std::istringstream ls(line);
deba@1421
  1719
	std::string id;
deba@1421
  1720
	ls >> id;
deba@1429
  1721
	typename EdgeReaders::iterator it = readers.find(id);
deba@1421
  1722
	if (it != readers.end()) {
deba@1901
  1723
	  it->second.read(edgeLabelReader->read(ls));
deba@1845
  1724
	  it->second.touch();
deba@1421
  1725
	}	
deba@1421
  1726
      }
deba@1845
  1727
      for (typename EdgeReaders::iterator it = readers.begin();
deba@1845
  1728
	   it != readers.end(); ++it) {
deba@1845
  1729
	if (!it->second.touched()) {
deba@1845
  1730
	  ErrorMessage msg;
deba@1845
  1731
	  msg << "Edge not found in file: " << it->first;
alpar@2153
  1732
	  throw IoParameterError(msg.message());
deba@1845
  1733
	}
deba@1845
  1734
      }
deba@1421
  1735
    }
deba@2000
  1736
deba@2000
  1737
    virtual void missing() {
deba@2000
  1738
      if (readers.empty()) return;
deba@2000
  1739
      ErrorMessage msg;
deba@2001
  1740
      msg << "Edges section not found in file: @edges " << name;
alpar@2153
  1741
      throw IoParameterError(msg.message());
deba@2000
  1742
    }
deba@1421
  1743
    
deba@1421
  1744
  private:
deba@1421
  1745
deba@1901
  1746
    std::string name;
deba@1421
  1747
deba@1845
  1748
    typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders;
deba@1429
  1749
    EdgeReaders readers;
deba@1901
  1750
    std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader;
deba@1421
  1751
  };
deba@1421
  1752
deba@2016
  1753
  /// \ingroup section_io
deba@1421
  1754
  /// \brief SectionReader for reading labeled undirected edges.
deba@1421
  1755
  ///
klao@1909
  1756
  /// The undirected edges section's header line is \c \@uedges 
klao@1909
  1757
  /// \c uedges_name, but the \c uedges_name may be empty.
deba@1421
  1758
  ///
deba@1421
  1759
  /// Each line in the section contains the name of the undirected edge 
deba@1421
  1760
  /// and then the undirected edge id. 
deba@1421
  1761
  ///
deba@1421
  1762
  /// \relates LemonReader
deba@1421
  1763
  template <typename _Graph>
klao@1909
  1764
  class UEdgeReader : public LemonReader::SectionReader {
deba@1845
  1765
    typedef LemonReader::SectionReader Parent;
deba@1421
  1766
    typedef _Graph Graph;
deba@1429
  1767
    typedef typename Graph::Edge Edge;
klao@1909
  1768
    typedef typename Graph::UEdge UEdge;
deba@1421
  1769
  public:
deba@1421
  1770
    
deba@1421
  1771
    /// \brief Constructor.
deba@1421
  1772
    ///
klao@1909
  1773
    /// Constructor for UEdgeReader. It creates the UEdgeReader and
deba@1421
  1774
    /// attach it into the given LemonReader. It will use the given
deba@1421
  1775
    /// undirected edge id reader to give back the edges. The reader will 
klao@1909
  1776
    /// read the section only if the \c _name and the \c uedges_name are 
deba@1421
  1777
    /// the same. 
deba@1901
  1778
    template <typename _LabelReader>
klao@1909
  1779
    UEdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 
deba@1901
  1780
	       const std::string& _name = std::string()) 
deba@1901
  1781
      : Parent(_reader), name(_name) {
klao@1909
  1782
      checkConcept<_reader_bits::ItemLabelReader<UEdge>, _LabelReader>();
deba@1901
  1783
      checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>();
klao@1909
  1784
      uEdgeLabelReader.reset(new _reader_bits::
klao@1909
  1785
			      LabelReader<UEdge, _LabelReader>(_labelReader));
deba@1901
  1786
      edgeLabelReader.reset(new _reader_bits::
deba@1901
  1787
			 LabelReader<Edge, _LabelReader>(_labelReader));
deba@1476
  1788
    }
deba@1421
  1789
deba@1421
  1790
    /// \brief Destructor.
deba@1421
  1791
    ///
klao@1909
  1792
    /// Destructor for UEdgeReader.
klao@1909
  1793
    virtual ~UEdgeReader() {}
deba@1421
  1794
  private:
klao@1909
  1795
    UEdgeReader(const UEdgeReader&);
klao@1909
  1796
    void operator=(const UEdgeReader&);
deba@1421
  1797
deba@1421
  1798
  public:
deba@1421
  1799
klao@1909
  1800
    /// \brief Add an undirected edge reader command for the UEdgeReader.
deba@1421
  1801
    ///
klao@1909
  1802
    /// Add an undirected edge reader command for the UEdgeReader.
deba@2386
  1803
    void readUEdge(std::string label, UEdge& item) {
deba@2386
  1804
      if (uEdgeReaders.find(label) != uEdgeReaders.end()) {
deba@1429
  1805
	ErrorMessage msg;
deba@2386
  1806
	msg << "Multiple read rule for undirected edge: " << label;
alpar@2153
  1807
	throw IoParameterError(msg.message());
deba@1429
  1808
      }
deba@2386
  1809
      uEdgeReaders.insert(make_pair(label, _reader_bits::
klao@1909
  1810
					ItemStore<UEdge>(item)));
deba@1429
  1811
    }
deba@1429
  1812
klao@1909
  1813
    /// \brief Add an edge reader command for the UEdgeReader.
deba@1429
  1814
    ///
klao@1909
  1815
    /// Add an edge reader command for the UEdgeReader.
deba@2386
  1816
    void readEdge(std::string label, Edge& item) {
deba@2386
  1817
      if (edgeReaders.find(label) != edgeReaders.end()) {
deba@1421
  1818
	ErrorMessage msg;
deba@2386
  1819
	msg << "Multiple read rule for edge: " << label;
alpar@2153
  1820
	throw IoParameterError(msg.message());
deba@1421
  1821
      }
deba@2386
  1822
      edgeReaders.insert(make_pair(label, _reader_bits::ItemStore<Edge>(item)));
deba@1421
  1823
    }
deba@1421
  1824
deba@1421
  1825
  protected:
deba@1421
  1826
deba@1421
  1827
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1828
    /// the section with the given header line.
deba@1421
  1829
    ///
deba@1421
  1830
    /// It gives back true when the header line start with \c \@edges,
deba@1901
  1831
    /// and the header line's name and the reader's name are the same.
deba@1409
  1832
    virtual bool header(const std::string& line) {
deba@1409
  1833
      std::istringstream ls(line);
deba@1409
  1834
      std::string command;
deba@1901
  1835
      std::string id;
klao@1997
  1836
      ls >> command >> id;
klao@1909
  1837
      return command == "@uedges" && name == id;
deba@1409
  1838
    }
deba@1409
  1839
deba@1409
  1840
    /// \brief Reader function of the section.
deba@1409
  1841
    ///
deba@1409
  1842
    /// It reads the content of the section.
deba@1409
  1843
    virtual void read(std::istream& is) {
deba@1901
  1844
      if (!edgeLabelReader->isLabelReader()) {
deba@1901
  1845
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1846
      }
klao@1909
  1847
      if (!uEdgeLabelReader->isLabelReader()) {
deba@1901
  1848
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1849
      }
deba@1409
  1850
      std::string line;
deba@1409
  1851
      while (getline(is, line)) {
deba@1409
  1852
	std::istringstream ls(line);
deba@1409
  1853
	std::string id;
deba@1409
  1854
	ls >> id;
deba@1429
  1855
	{
klao@1909
  1856
	  typename UEdgeReaders::iterator it = uEdgeReaders.find(id);
klao@1909
  1857
	  if (it != uEdgeReaders.end()) {
klao@1909
  1858
	    it->second.read(uEdgeLabelReader->read(ls));
deba@1845
  1859
	    it->second.touch();
deba@1845
  1860
	    continue;
deba@1429
  1861
	  }	
deba@1429
  1862
	} {
deba@1429
  1863
	  typename EdgeReaders::iterator it = edgeReaders.find(id);
deba@1429
  1864
	  if (it != edgeReaders.end()) {
deba@1901
  1865
	    it->second.read(edgeLabelReader->read(ls));
deba@1845
  1866
	    it->second.touch();
deba@1845
  1867
	    continue;
deba@1429
  1868
	  }	
deba@1429
  1869
	}
deba@1409
  1870
      }
deba@1845
  1871
      for (typename EdgeReaders::iterator it = edgeReaders.begin();
deba@1845
  1872
	   it != edgeReaders.end(); ++it) {
deba@1845
  1873
	if (!it->second.touched()) {
deba@1845
  1874
	  ErrorMessage msg;
deba@1845
  1875
	  msg << "Edge not found in file: " << it->first;
alpar@2153
  1876
	  throw IoParameterError(msg.message());
deba@1845
  1877
	}
deba@1845
  1878
      }
klao@1909
  1879
      for (typename UEdgeReaders::iterator it = uEdgeReaders.begin();
klao@1909
  1880
	   it != uEdgeReaders.end(); ++it) {
deba@1845
  1881
	if (!it->second.touched()) {
deba@1845
  1882
	  ErrorMessage msg;
klao@1909
  1883
	  msg << "UEdge not found in file: " << it->first;
alpar@2153
  1884
	  throw IoParameterError(msg.message());
deba@1845
  1885
	}
deba@1845
  1886
      }
deba@1409
  1887
    }
deba@2000
  1888
deba@2000
  1889
    virtual void missing() {
deba@2000
  1890
      if (edgeReaders.empty() && uEdgeReaders.empty()) return;
deba@2000
  1891
      ErrorMessage msg;
deba@2001
  1892
      msg << "UEdges section not found in file: @uedges " << name;
alpar@2153
  1893
      throw IoParameterError(msg.message());
deba@2000
  1894
    }
deba@1409
  1895
    
deba@1409
  1896
  private:
deba@1409
  1897
deba@1901
  1898
    std::string name;
deba@1409
  1899
deba@1845
  1900
    typedef std::map<std::string, 
klao@1909
  1901
		     _reader_bits::ItemStore<UEdge> > UEdgeReaders;
klao@1909
  1902
    UEdgeReaders uEdgeReaders;
klao@1909
  1903
    std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > uEdgeLabelReader;
deba@1429
  1904
deba@1845
  1905
    typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders;
deba@1429
  1906
    EdgeReaders edgeReaders;
deba@1901
  1907
    std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader;
deba@1409
  1908
  };
deba@1409
  1909
deba@2016
  1910
  /// \ingroup section_io
deba@1409
  1911
  /// \brief SectionReader for attributes.
deba@1409
  1912
  ///
deba@1409
  1913
  /// The lemon format can store multiple attribute set. Each set has
deba@1901
  1914
  /// the header line \c \@attributes \c attributeset_name, but the 
deba@1901
  1915
  /// attributeset_name may be empty.
deba@1409
  1916
  ///
deba@1409
  1917
  /// The attributeset section contains several lines. Each of them starts
deba@1409
  1918
  /// with an attribute and then a the value for the id.
deba@1409
  1919
  ///
deba@1409
  1920
  /// \relates LemonReader
deba@1408
  1921
  template <typename _Traits = DefaultReaderTraits>
deba@1845
  1922
  class AttributeReader : public LemonReader::SectionReader {
deba@1845
  1923
    typedef LemonReader::SectionReader Parent;
deba@1408
  1924
    typedef _Traits Traits; 
deba@1408
  1925
  public:
deba@1409
  1926
    /// \brief Constructor.
deba@1409
  1927
    ///
deba@1409
  1928
    /// Constructor for AttributeReader. It creates the AttributeReader and
deba@1409
  1929
    /// attach it into the given LemonReader. The reader process a section
deba@1901
  1930
    /// only if the \c section_name and the \c _name are the same.
deba@1408
  1931
    AttributeReader(LemonReader& _reader, 
deba@1901
  1932
		    const std::string& _name = std::string()) 
deba@1901
  1933
      : Parent(_reader), name(_name) {}
deba@1408
  1934
deba@1409
  1935
    /// \brief Destructor.
deba@1409
  1936
    ///
deba@1409
  1937
    /// Destructor for AttributeReader.
deba@1408
  1938
    virtual ~AttributeReader() {
deba@1408
  1939
      for (typename Readers::iterator it = readers.begin(); 
deba@1408
  1940
	   it != readers.end(); ++it) {
deba@1408
  1941
	delete it->second;
deba@1408
  1942
      }
deba@1408
  1943
    }
deba@1408
  1944
deba@1408
  1945
  private:
deba@1408
  1946
    AttributeReader(const AttributeReader&);
deba@1408
  1947
    void operator=(AttributeReader&);
deba@1408
  1948
deba@1408
  1949
  public:
deba@1409
  1950
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1951
    ///
deba@1409
  1952
    /// Add an attribute reader command for the reader.
deba@1408
  1953
    template <typename Value>
deba@1408
  1954
    AttributeReader& readAttribute(const std::string& id, Value& value) {
deba@1408
  1955
      return readAttribute<typename Traits::template Reader<Value> >
deba@1408
  1956
	(id, value);
deba@1408
  1957
    }
deba@1408
  1958
deba@1409
  1959
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1960
    ///
deba@1409
  1961
    /// Add an attribute reader command for the reader.
deba@2386
  1962
    template <typename ItemReader, typename Value>
deba@2386
  1963
    AttributeReader& readAttribute(std::string label, Value& value,
deba@2386
  1964
				   const ItemReader& ir = ItemReader()) {
deba@2386
  1965
      checkConcept<_reader_bits::ItemReader<Value>, ItemReader>();
deba@2386
  1966
      if (readers.find(label) != readers.end()) {
deba@1408
  1967
	ErrorMessage msg;
deba@2386
  1968
	msg << "Multiple read rule for attribute: " << label;
alpar@2153
  1969
	throw IoParameterError(msg.message());
deba@1408
  1970
      }
deba@2386
  1971
      readers.insert(make_pair(label, new _reader_bits::
deba@2386
  1972
			       ValueReader<Value, ItemReader>(value, ir)));
deba@1408
  1973
      return *this;
deba@1408
  1974
    }
deba@1408
  1975
deba@1409
  1976
  protected:
deba@1409
  1977
deba@1409
  1978
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1979
    /// the section with the given header line.
deba@1409
  1980
    ///
deba@1421
  1981
    /// It gives back true when the header line start with \c \@attributes,
deba@1409
  1982
    /// and the header line's id and the attributeset's id are the same.
deba@1408
  1983
    bool header(const std::string& line) {
deba@1408
  1984
      std::istringstream ls(line);
deba@1408
  1985
      std::string command;
deba@1901
  1986
      std::string id;
klao@1997
  1987
      ls >> command >> id;
deba@1408
  1988
      return command == "@attributes" && name == id;
deba@1408
  1989
    }
deba@1408
  1990
deba@1409
  1991
    /// \brief Reader function of the section.
deba@1409
  1992
    ///
deba@1409
  1993
    /// It reads the content of the section.
deba@1408
  1994
    void read(std::istream& is) {
deba@1408
  1995
      std::string line;
deba@1408
  1996
      while (getline(is, line)) {
deba@1408
  1997
	std::istringstream ls(line);
deba@1408
  1998
	std::string id;
deba@1408
  1999
	ls >> id;
deba@1408
  2000
	typename Readers::iterator it = readers.find(id);
deba@1408
  2001
	if (it != readers.end()) {
deba@1408
  2002
	  it->second->read(ls);
deba@1901
  2003
 	  it->second->touch();
deba@1408
  2004
	}
deba@1408
  2005
      }
deba@1846
  2006
      for (typename Readers::iterator it = readers.begin();
deba@1846
  2007
	   it != readers.end(); ++it) {
deba@1846
  2008
	if (!it->second->touched()) {
deba@1846
  2009
	  ErrorMessage msg;
deba@1846
  2010
	  msg << "Attribute not found in file: " << it->first;
alpar@2153
  2011
	  throw IoParameterError(msg.message());
deba@1846
  2012
	}	
deba@1846
  2013
      }
deba@1408
  2014
    }    
deba@1408
  2015
deba@2000
  2016
    virtual void missing() {
deba@2000
  2017
      if (readers.empty()) return;
deba@2000
  2018
      ErrorMessage msg;
deba@2001
  2019
      msg << "Attribute section not found in file: @attributes " << name;
alpar@2153
  2020
      throw IoParameterError(msg.message());
deba@2000
  2021
    }
deba@2000
  2022
deba@1408
  2023
  private:
deba@1901
  2024
    std::string name;
deba@1408
  2025
deba@1845
  2026
    typedef std::map<std::string, _reader_bits::ValueReaderBase*> Readers;
deba@1409
  2027
    Readers readers;  
deba@1408
  2028
  };
deba@1408
  2029
deba@2016
  2030
  /// \ingroup section_io
deba@1423
  2031
  /// \brief SectionReader for retrieve what is in the file.
deba@1423
  2032
  ///
deba@1423
  2033
  /// SectionReader for retrieve what is in the file. If you want
deba@1423
  2034
  /// to know which sections, maps and items are in the file
deba@1423
  2035
  /// use the next code:
alpar@1946
  2036
  ///\code
deba@1423
  2037
  /// LemonReader reader("input.lgf");
deba@1423
  2038
  /// ContentReader content(reader);
deba@1423
  2039
  /// reader.run();
alpar@1946
  2040
  ///\endcode
deba@1423
  2041
  class ContentReader : public LemonReader::SectionReader {
deba@1423
  2042
    typedef LemonReader::SectionReader Parent;
deba@1423
  2043
  public:
deba@1423
  2044
    /// \brief Constructor.
deba@1423
  2045
    ///
deba@1423
  2046
    /// Constructor for
deba@1423
  2047
    ContentReader(LemonReader& _reader) : Parent(_reader) {}
deba@1423
  2048
deba@1423
  2049
    /// \brief Desctructor.
deba@1423
  2050
    ///
deba@1423
  2051
    /// Desctructor.
deba@1423
  2052
    virtual ~ContentReader() {}
deba@1423
  2053
deba@1423
  2054
    /// \brief Gives back how many nodesets are in the file.
deba@1423
  2055
    ///
deba@1423
  2056
    /// Gives back how many nodesets are in the file.
deba@1423
  2057
    int nodeSetNum() const {
deba@1423
  2058
      return nodesets.size();
deba@1423
  2059
    }
deba@1423
  2060
deba@1423
  2061
    /// \brief Gives back the name of nodeset on the indiced position.
deba@1423
  2062
    ///
deba@1423
  2063
    /// Gives back the name of nodeset on the indiced position.
deba@1423
  2064
    std::string nodeSetName(int index) const {
deba@1423
  2065
      return nodesets[index].name;
deba@1423
  2066
    }
deba@1423
  2067
deba@1423
  2068
    /// \brief Gives back the map names of nodeset on the indiced position.
deba@1423
  2069
    ///
deba@1423
  2070
    /// Gives back the map names of nodeset on the indiced position.
deba@1423
  2071
    const std::vector<std::string>& nodeSetMaps(int index) const {
deba@1423
  2072
      return nodesets[index].items;
deba@1423
  2073
    }
deba@1423
  2074
deba@1423
  2075
    /// \brief Gives back how many edgesets are in the file.
deba@1423
  2076
    ///
deba@1423
  2077
    /// Gives back how many edgesets are in the file.
deba@1423
  2078
    int edgeSetNum() const {
deba@1423
  2079
      return edgesets.size();
deba@1423
  2080
    }
deba@1423
  2081
deba@1423
  2082
    /// \brief Gives back the name of edgeset on the indiced position.
deba@1423
  2083
    ///
deba@1423
  2084
    /// Gives back the name of edgeset on the indiced position.
deba@1423
  2085
    std::string edgeSetName(int index) const {
deba@1423
  2086
      return edgesets[index].name;
deba@1423
  2087
    }
deba@1423
  2088
deba@1423
  2089
    /// \brief Gives back the map names of edgeset on the indiced position.
deba@1423
  2090
    ///
deba@1423
  2091
    /// Gives back the map names of edgeset on the indiced position.
deba@1423
  2092
    const std::vector<std::string>& edgeSetMaps(int index) const {
deba@1423
  2093
      return edgesets[index].items;
deba@1423
  2094
    }
deba@1423
  2095
deba@1423
  2096
    /// \brief Gives back how many undirected edgesets are in the file.
deba@1423
  2097
    ///
deba@1423
  2098
    /// Gives back how many undirected edgesets are in the file.
klao@1909
  2099
    int uEdgeSetNum() const {
klao@1909
  2100
      return uedgesets.size();
deba@1423
  2101
    }
deba@1423
  2102
deba@1423
  2103
    /// \brief Gives back the name of undirected edgeset on the indiced 
deba@1423
  2104
    /// position.
deba@1423
  2105
    ///
deba@1423
  2106
    /// Gives back the name of undirected edgeset on the indiced position.
klao@1909
  2107
    std::string uEdgeSetName(int index) const {
klao@1909
  2108
      return uedgesets[index].name;
deba@1423
  2109
    }
deba@1423
  2110
deba@1423
  2111
    /// \brief Gives back the map names of undirected edgeset on the indiced 
deba@1423
  2112
    /// position.
deba@1423
  2113
    ///
deba@1423
  2114
    /// Gives back the map names of undirected edgeset on the indiced position.
klao@1909
  2115
    const std::vector<std::string>& uEdgeSetMaps(int index) const {
klao@1909
  2116
      return uedgesets[index].items;
deba@1423
  2117
    }
deba@1423
  2118
deba@1423
  2119
    /// \brief Gives back how many labeled nodes section are in the file.
deba@1423
  2120
    ///
deba@1423
  2121
    /// Gives back how many labeled nodes section are in the file.
deba@1423
  2122
    int nodesNum() const {
deba@1423
  2123
      return nodes.size();
deba@1423
  2124
    }
deba@1423
  2125
deba@1423
  2126
    /// \brief Gives back the name of labeled nodes section on the indiced 
deba@1423
  2127
    /// position.
deba@1423
  2128
    ///
deba@1423
  2129
    /// Gives back the name of labeled nodes section on the indiced position.
deba@1423
  2130
    std::string nodesName(int index) const {
deba@1423
  2131
      return nodes[index].name;
deba@1423
  2132
    }
deba@1423
  2133
deba@1423
  2134
    /// \brief Gives back the names of the labeled nodes in the indiced 
deba@1423
  2135
    /// section.
deba@1423
  2136
    ///
deba@1423
  2137
    /// Gives back the names of the labeled nodes in the indiced section.
deba@1423
  2138
    const std::vector<std::string>& nodesItems(int index) const {
deba@1423
  2139
      return nodes[index].items;
deba@1423
  2140
    }
deba@1423
  2141
deba@1423
  2142
    /// \brief Gives back how many labeled edges section are in the file.
deba@1423
  2143
    ///
deba@1423
  2144
    /// Gives back how many labeled edges section are in the file.
deba@1423
  2145
    int edgesNum() const {
deba@1423
  2146
      return edges.size();
deba@1423
  2147
    }
deba@1423
  2148
deba@1423
  2149
    /// \brief Gives back the name of labeled edges section on the indiced 
deba@1423
  2150
    /// position.
deba@1423
  2151
    ///
deba@1423
  2152
    /// Gives back the name of labeled edges section on the indiced position.
deba@1423
  2153
    std::string edgesName(int index) const {
deba@1423
  2154
      return edges[index].name;
deba@1423
  2155
    }
deba@1423
  2156
deba@1423
  2157
    /// \brief Gives back the names of the labeled edges in the indiced 
deba@1423
  2158
    /// section.
deba@1423
  2159
    ///
deba@1423
  2160
    /// Gives back the names of the labeled edges in the indiced section.
deba@1423
  2161
    const std::vector<std::string>& edgesItems(int index) const {
deba@1423
  2162
      return edges[index].items;
deba@1423
  2163
    }
deba@1423
  2164
 
deba@1423
  2165
    /// \brief Gives back how many labeled undirected edges section are 
deba@1423
  2166
    /// in the file.
deba@1423
  2167
    ///
deba@1423
  2168
    /// Gives back how many labeled undirected edges section are in the file.
klao@1909
  2169
    int uEdgesNum() const {
klao@1909
  2170
      return uedges.size();
deba@1423
  2171
    }
deba@1423
  2172
deba@1423
  2173
    /// \brief Gives back the name of labeled undirected edges section 
deba@1423
  2174
    /// on the indiced position.
deba@1423
  2175
    ///
deba@1423
  2176
    /// Gives back the name of labeled undirected edges section on the 
deba@1423
  2177
    /// indiced position.
klao@1909
  2178
    std::string uEdgesName(int index) const {
klao@1909
  2179
      return uedges[index].name;
deba@1423
  2180
    }
deba@1423
  2181
deba@1423
  2182
    /// \brief Gives back the names of the labeled undirected edges in 
deba@1423
  2183
    /// the indiced section.
deba@1423
  2184
    ///
deba@1423
  2185
    /// Gives back the names of the labeled undirected edges in the 
deba@1423
  2186
    /// indiced section.
klao@1909
  2187
    const std::vector<std::string>& uEdgesItems(int index) const {
klao@1909
  2188
      return uedges[index].items;
deba@1423
  2189
    }
deba@1423
  2190
deba@1423
  2191
 
deba@1423
  2192
    /// \brief Gives back how many attributes section are in the file.
deba@1423
  2193
    ///
deba@1423
  2194
    /// Gives back how many attributes section are in the file.
deba@1423
  2195
    int attributesNum() const {
deba@1423
  2196
      return attributes.size();
deba@1423
  2197
    }
deba@1423
  2198
deba@1423
  2199
    /// \brief Gives back the name of attributes section on the indiced 
deba@1423
  2200
    /// position.
deba@1423
  2201
    ///
deba@1423
  2202
    /// Gives back the name of attributes section on the indiced position.
deba@1423
  2203
    std::string attributesName(int index) const {
deba@1423
  2204
      return attributes[index].name;
deba@1423
  2205
    }
deba@1423
  2206
deba@1423
  2207
    /// \brief Gives back the names of the attributes in the indiced section.
deba@1423
  2208
    ///
deba@1423
  2209
    /// Gives back the names of the attributes in the indiced section.
deba@1423
  2210
    const std::vector<std::string>& attributesItems(int index) const {
deba@1423
  2211
      return attributes[index].items;
deba@1423
  2212
    }
deba@1423
  2213
deba@1423
  2214
    const std::vector<std::string>& otherSections() const {
deba@1423
  2215
      return sections;
deba@1423
  2216
    }
deba@1423
  2217
deba@1423
  2218
  protected:
deba@1423
  2219
    
deba@1423
  2220
    /// \brief Gives back true when the SectionReader can process 
deba@1423
  2221
    /// the section with the given header line.
deba@1423
  2222
    ///
deba@1423
  2223
    /// It gives back true when the section is common section.
deba@1423
  2224
    bool header(const std::string& line) {
deba@1423
  2225
      std::istringstream ls(line);
deba@1423
  2226
      std::string command, name;
deba@1423
  2227
      ls >> command >> name;
deba@1423
  2228
      if (command == "@nodeset") {
deba@1423
  2229
	current = command;
deba@1423
  2230
	nodesets.push_back(SectionInfo(name));
deba@1423
  2231
      } else if (command == "@edgeset") {
deba@1423
  2232
	current = command;
deba@1423
  2233
	edgesets.push_back(SectionInfo(name));
klao@1909
  2234
      } else if (command == "@uedgeset") {
deba@1423
  2235
	current = command;
klao@1909
  2236
	uedgesets.push_back(SectionInfo(name));
deba@1423
  2237
      } else if (command == "@nodes") {
deba@1423
  2238
	current = command;
deba@1423
  2239
	nodes.push_back(SectionInfo(name));
deba@1423
  2240
      } else if (command == "@edges") {
deba@1423
  2241
	current = command;
deba@1423
  2242
	edges.push_back(SectionInfo(name));
klao@1909
  2243
      } else if (command == "@uedges") {
deba@1423
  2244
	current = command;
klao@1909
  2245
	uedges.push_back(SectionInfo(name));
deba@1423
  2246
      } else if (command == "@attributes") {
deba@1423
  2247
	current = command;
deba@1423
  2248
	attributes.push_back(SectionInfo(name));
deba@1423
  2249
      } else {
deba@1423
  2250
	sections.push_back(line);
deba@1423
  2251
	return false;
deba@1423
  2252
      }
deba@1423
  2253
      return true;
deba@1423
  2254
    }
deba@1423
  2255
deba@1423
  2256
    /// \brief Retrieve the items from various sections.
deba@1423
  2257
    ///
deba@1423
  2258
    /// Retrieve the items from various sections.
deba@1423
  2259
    void read(std::istream& is) {
deba@1423
  2260
      if (current == "@nodeset") {
deba@1423
  2261
	readMapNames(is, nodesets.back().items);
deba@1423
  2262
      } else if (current == "@edgeset") {
deba@1423
  2263
	readMapNames(is, edgesets.back().items);
klao@1909
  2264
      } else if (current == "@uedgeset") {
klao@1909
  2265
	readMapNames(is, uedgesets.back().items);
deba@1423
  2266
      } else if (current == "@nodes") {
deba@1423
  2267
	readItemNames(is, nodes.back().items);
deba@1423
  2268
      } else if (current == "@edges") {
deba@1423
  2269
	readItemNames(is, edges.back().items);
klao@1909
  2270
      } else if (current == "@uedges") {
klao@1909
  2271
	readItemNames(is, uedges.back().items);
deba@1423
  2272
      } else if (current == "@attributes") {
deba@1423
  2273
	readItemNames(is, attributes.back().items);
deba@1423
  2274
      }
deba@1423
  2275
    }    
deba@1423
  2276
deba@1423
  2277
  private:
deba@1423
  2278
deba@1423
  2279
    void readMapNames(std::istream& is, std::vector<std::string>& maps) {
deba@1901
  2280
      std::string line, name;
deba@1423
  2281
      std::getline(is, line);
deba@1423
  2282
      std::istringstream ls(line);
deba@1901
  2283
      while (ls >> name) {
deba@1901
  2284
	maps.push_back(name);
deba@1423
  2285
      }
deba@1423
  2286
      while (getline(is, line));
deba@1423
  2287
    }
deba@1423
  2288
deba@1423
  2289
    void readItemNames(std::istream& is, std::vector<std::string>& maps) {
deba@1901
  2290
      std::string line, name;
deba@1423
  2291
      while (std::getline(is, line)) {
deba@1423
  2292
	std::istringstream ls(line);
deba@1901
  2293
	ls >> name;
deba@1901
  2294
	maps.push_back(name);
deba@1423
  2295
      }
deba@1423
  2296
    }
deba@1423
  2297
deba@1423
  2298
    struct SectionInfo {
deba@1423
  2299
      std::string name;
deba@1423
  2300
      std::vector<std::string> items;
deba@1423
  2301
deba@1423
  2302
      SectionInfo(const std::string& _name) : name(_name) {}
deba@1423
  2303
    };
deba@1423
  2304
deba@1423
  2305
    std::vector<SectionInfo> nodesets;
deba@1423
  2306
    std::vector<SectionInfo> edgesets;
klao@1909
  2307
    std::vector<SectionInfo> uedgesets;
deba@1423
  2308
deba@1423
  2309
    std::vector<SectionInfo> nodes;
deba@1423
  2310
    std::vector<SectionInfo> edges;
klao@1909
  2311
    std::vector<SectionInfo> uedges;
deba@1423
  2312
deba@1423
  2313
    std::vector<SectionInfo> attributes;
deba@1423
  2314
deba@1423
  2315
    std::vector<std::string> sections;
deba@1423
  2316
deba@1423
  2317
    std::string current;
deba@1423
  2318
deba@1423
  2319
  };
deba@1423
  2320
deba@1408
  2321
}
deba@1408
  2322
#endif