lemon/lgf_reader.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 08 Mar 2013 01:12:05 +0100
changeset 1045 4e8787627db3
parent 1029 374a9519986b
child 1076 97d978243703
permissions -rw-r--r--
Improve images of the Connectivity section (#411)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@127
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@127
     4
 *
alpar@949
     5
 * Copyright (C) 2003-2011
deba@127
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@127
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@127
     8
 *
deba@127
     9
 * Permission to use, modify and distribute this software is granted
deba@127
    10
 * provided that this copyright notice appears in all copies. For
deba@127
    11
 * precise terms see the accompanying LICENSE file.
deba@127
    12
 *
deba@127
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@127
    14
 * express or implied, and with no claim as to its suitability for any
deba@127
    15
 * purpose.
deba@127
    16
 *
deba@127
    17
 */
deba@127
    18
deba@127
    19
///\ingroup lemon_io
deba@127
    20
///\file
ladanyi@236
    21
///\brief \ref lgf-format "LEMON Graph Format" reader.
deba@127
    22
deba@127
    23
deba@127
    24
#ifndef LEMON_LGF_READER_H
deba@127
    25
#define LEMON_LGF_READER_H
deba@127
    26
deba@127
    27
#include <iostream>
deba@127
    28
#include <fstream>
deba@127
    29
#include <sstream>
deba@127
    30
deba@127
    31
#include <set>
deba@127
    32
#include <map>
deba@127
    33
deba@220
    34
#include <lemon/core.h>
deba@127
    35
deba@127
    36
#include <lemon/lgf_writer.h>
deba@127
    37
deba@127
    38
#include <lemon/concept_check.h>
deba@127
    39
#include <lemon/concepts/maps.h>
deba@127
    40
deba@127
    41
namespace lemon {
deba@127
    42
deba@127
    43
  namespace _reader_bits {
deba@127
    44
deba@127
    45
    template <typename Value>
deba@127
    46
    struct DefaultConverter {
deba@127
    47
      Value operator()(const std::string& str) {
alpar@209
    48
        std::istringstream is(str);
alpar@209
    49
        Value value;
deba@290
    50
        if (!(is >> value)) {
deba@290
    51
          throw FormatError("Cannot read token");
deba@290
    52
        }
alpar@209
    53
alpar@209
    54
        char c;
alpar@209
    55
        if (is >> std::ws >> c) {
deba@290
    56
          throw FormatError("Remaining characters in token");
alpar@209
    57
        }
alpar@209
    58
        return value;
deba@127
    59
      }
deba@127
    60
    };
deba@127
    61
deba@127
    62
    template <>
deba@127
    63
    struct DefaultConverter<std::string> {
deba@127
    64
      std::string operator()(const std::string& str) {
alpar@209
    65
        return str;
deba@127
    66
      }
deba@127
    67
    };
deba@127
    68
alpar@209
    69
    template <typename _Item>
deba@127
    70
    class MapStorageBase {
deba@127
    71
    public:
deba@127
    72
      typedef _Item Item;
deba@127
    73
deba@127
    74
    public:
deba@127
    75
      MapStorageBase() {}
deba@127
    76
      virtual ~MapStorageBase() {}
deba@127
    77
deba@127
    78
      virtual void set(const Item& item, const std::string& value) = 0;
deba@127
    79
deba@127
    80
    };
deba@127
    81
alpar@209
    82
    template <typename _Item, typename _Map,
alpar@209
    83
              typename _Converter = DefaultConverter<typename _Map::Value> >
deba@127
    84
    class MapStorage : public MapStorageBase<_Item> {
deba@127
    85
    public:
deba@127
    86
      typedef _Map Map;
deba@127
    87
      typedef _Converter Converter;
deba@127
    88
      typedef _Item Item;
alpar@209
    89
deba@127
    90
    private:
deba@127
    91
      Map& _map;
deba@127
    92
      Converter _converter;
deba@127
    93
deba@127
    94
    public:
alpar@209
    95
      MapStorage(Map& map, const Converter& converter = Converter())
alpar@209
    96
        : _map(map), _converter(converter) {}
deba@127
    97
      virtual ~MapStorage() {}
deba@127
    98
deba@127
    99
      virtual void set(const Item& item ,const std::string& value) {
alpar@209
   100
        _map.set(item, _converter(value));
deba@127
   101
      }
deba@127
   102
    };
deba@127
   103
deba@598
   104
    template <typename _GR, bool _dir, typename _Map,
alpar@209
   105
              typename _Converter = DefaultConverter<typename _Map::Value> >
deba@598
   106
    class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> {
deba@165
   107
    public:
deba@165
   108
      typedef _Map Map;
deba@165
   109
      typedef _Converter Converter;
deba@598
   110
      typedef _GR GR;
deba@598
   111
      typedef typename GR::Edge Item;
deba@165
   112
      static const bool dir = _dir;
alpar@209
   113
deba@165
   114
    private:
deba@598
   115
      const GR& _graph;
deba@165
   116
      Map& _map;
deba@165
   117
      Converter _converter;
deba@165
   118
deba@165
   119
    public:
deba@598
   120
      GraphArcMapStorage(const GR& graph, Map& map,
alpar@209
   121
                         const Converter& converter = Converter())
alpar@209
   122
        : _graph(graph), _map(map), _converter(converter) {}
deba@165
   123
      virtual ~GraphArcMapStorage() {}
deba@165
   124
deba@165
   125
      virtual void set(const Item& item ,const std::string& value) {
alpar@209
   126
        _map.set(_graph.direct(item, dir), _converter(value));
deba@165
   127
      }
deba@165
   128
    };
deba@165
   129
deba@127
   130
    class ValueStorageBase {
deba@127
   131
    public:
deba@127
   132
      ValueStorageBase() {}
deba@127
   133
      virtual ~ValueStorageBase() {}
deba@127
   134
deba@127
   135
      virtual void set(const std::string&) = 0;
deba@127
   136
    };
deba@127
   137
deba@127
   138
    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
deba@127
   139
    class ValueStorage : public ValueStorageBase {
deba@127
   140
    public:
deba@127
   141
      typedef _Value Value;
deba@127
   142
      typedef _Converter Converter;
deba@127
   143
deba@127
   144
    private:
deba@127
   145
      Value& _value;
deba@127
   146
      Converter _converter;
deba@127
   147
deba@127
   148
    public:
deba@127
   149
      ValueStorage(Value& value, const Converter& converter = Converter())
kpeter@212
   150
        : _value(value), _converter(converter) {}
deba@127
   151
deba@127
   152
      virtual void set(const std::string& value) {
alpar@209
   153
        _value = _converter(value);
deba@127
   154
      }
deba@127
   155
    };
deba@127
   156
deba@1030
   157
    template <typename Value,
deba@1030
   158
              typename Map = std::map<std::string, Value> >
deba@127
   159
    struct MapLookUpConverter {
deba@1030
   160
      const Map& _map;
deba@1030
   161
deba@1030
   162
      MapLookUpConverter(const Map& map)
deba@127
   163
        : _map(map) {}
deba@127
   164
deba@127
   165
      Value operator()(const std::string& str) {
deba@1030
   166
        typename Map::const_iterator it = _map.find(str);
deba@127
   167
        if (it == _map.end()) {
deba@127
   168
          std::ostringstream msg;
deba@127
   169
          msg << "Item not found: " << str;
deba@290
   170
          throw FormatError(msg.str());
deba@127
   171
        }
deba@127
   172
        return it->second;
deba@127
   173
      }
deba@127
   174
    };
deba@127
   175
deba@1030
   176
    template <typename Value,
deba@1030
   177
              typename Map1 = std::map<std::string, Value>,
deba@1030
   178
              typename Map2 = std::map<std::string, Value> >
deba@1030
   179
    struct DoubleMapLookUpConverter {
deba@1030
   180
      const Map1& _map1;
deba@1030
   181
      const Map2& _map2;
deba@1030
   182
deba@1030
   183
      DoubleMapLookUpConverter(const Map1& map1, const Map2& map2)
deba@1030
   184
        : _map1(map1), _map2(map2) {}
deba@1030
   185
deba@1030
   186
      Value operator()(const std::string& str) {
deba@1030
   187
        typename Map1::const_iterator it1 = _map1.find(str);
deba@1030
   188
        typename Map2::const_iterator it2 = _map2.find(str);
deba@1030
   189
        if (it1 == _map1.end()) {
deba@1030
   190
          if (it2 == _map2.end()) {
deba@1030
   191
            std::ostringstream msg;
deba@1030
   192
            msg << "Item not found: " << str;
deba@1030
   193
            throw FormatError(msg.str());
deba@1030
   194
          } else {
deba@1030
   195
            return it2->second;
deba@1030
   196
          }
deba@1030
   197
        } else {
deba@1030
   198
          if (it2 == _map2.end()) {
deba@1030
   199
            return it1->second;
deba@1030
   200
          } else {
deba@1030
   201
            std::ostringstream msg;
deba@1030
   202
            msg << "Item is ambigous: " << str;
deba@1030
   203
            throw FormatError(msg.str());
deba@1030
   204
          }
deba@1030
   205
        }
deba@1030
   206
      }
deba@1030
   207
    };
deba@1030
   208
deba@598
   209
    template <typename GR>
deba@165
   210
    struct GraphArcLookUpConverter {
deba@598
   211
      const GR& _graph;
deba@598
   212
      const std::map<std::string, typename GR::Edge>& _map;
deba@598
   213
deba@598
   214
      GraphArcLookUpConverter(const GR& graph,
alpar@209
   215
                              const std::map<std::string,
deba@598
   216
                                             typename GR::Edge>& map)
alpar@209
   217
        : _graph(graph), _map(map) {}
alpar@209
   218
deba@598
   219
      typename GR::Arc operator()(const std::string& str) {
alpar@209
   220
        if (str.empty() || (str[0] != '+' && str[0] != '-')) {
deba@290
   221
          throw FormatError("Item must start with '+' or '-'");
alpar@209
   222
        }
deba@598
   223
        typename std::map<std::string, typename GR::Edge>
alpar@209
   224
          ::const_iterator it = _map.find(str.substr(1));
alpar@209
   225
        if (it == _map.end()) {
deba@290
   226
          throw FormatError("Item not found");
alpar@209
   227
        }
alpar@209
   228
        return _graph.direct(it->second, str[0] == '+');
deba@165
   229
      }
deba@165
   230
    };
deba@165
   231
deba@197
   232
    inline bool isWhiteSpace(char c) {
alpar@209
   233
      return c == ' ' || c == '\t' || c == '\v' ||
alpar@209
   234
        c == '\n' || c == '\r' || c == '\f';
deba@127
   235
    }
alpar@209
   236
deba@197
   237
    inline bool isOct(char c) {
alpar@209
   238
      return '0' <= c && c <='7';
deba@127
   239
    }
alpar@209
   240
deba@197
   241
    inline int valueOct(char c) {
deba@127
   242
      LEMON_ASSERT(isOct(c), "The character is not octal.");
deba@127
   243
      return c - '0';
deba@127
   244
    }
deba@127
   245
deba@197
   246
    inline bool isHex(char c) {
alpar@209
   247
      return ('0' <= c && c <= '9') ||
alpar@209
   248
        ('a' <= c && c <= 'z') ||
alpar@209
   249
        ('A' <= c && c <= 'Z');
deba@127
   250
    }
alpar@209
   251
deba@197
   252
    inline int valueHex(char c) {
deba@127
   253
      LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
deba@127
   254
      if ('0' <= c && c <= '9') return c - '0';
deba@127
   255
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
deba@127
   256
      return c - 'A' + 10;
deba@127
   257
    }
deba@127
   258
deba@197
   259
    inline bool isIdentifierFirstChar(char c) {
deba@127
   260
      return ('a' <= c && c <= 'z') ||
alpar@209
   261
        ('A' <= c && c <= 'Z') || c == '_';
deba@127
   262
    }
deba@127
   263
deba@197
   264
    inline bool isIdentifierChar(char c) {
deba@127
   265
      return isIdentifierFirstChar(c) ||
alpar@209
   266
        ('0' <= c && c <= '9');
deba@127
   267
    }
deba@127
   268
deba@197
   269
    inline char readEscape(std::istream& is) {
deba@127
   270
      char c;
deba@127
   271
      if (!is.get(c))
deba@290
   272
        throw FormatError("Escape format error");
deba@127
   273
deba@127
   274
      switch (c) {
deba@127
   275
      case '\\':
alpar@209
   276
        return '\\';
deba@127
   277
      case '\"':
alpar@209
   278
        return '\"';
deba@127
   279
      case '\'':
alpar@209
   280
        return '\'';
deba@127
   281
      case '\?':
alpar@209
   282
        return '\?';
deba@127
   283
      case 'a':
alpar@209
   284
        return '\a';
deba@127
   285
      case 'b':
alpar@209
   286
        return '\b';
deba@127
   287
      case 'f':
alpar@209
   288
        return '\f';
deba@127
   289
      case 'n':
alpar@209
   290
        return '\n';
deba@127
   291
      case 'r':
alpar@209
   292
        return '\r';
deba@127
   293
      case 't':
alpar@209
   294
        return '\t';
deba@127
   295
      case 'v':
alpar@209
   296
        return '\v';
deba@127
   297
      case 'x':
alpar@209
   298
        {
alpar@209
   299
          int code;
alpar@209
   300
          if (!is.get(c) || !isHex(c))
deba@290
   301
            throw FormatError("Escape format error");
alpar@209
   302
          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
alpar@209
   303
          else code = code * 16 + valueHex(c);
alpar@209
   304
          return code;
alpar@209
   305
        }
deba@127
   306
      default:
alpar@209
   307
        {
alpar@209
   308
          int code;
alpar@209
   309
          if (!isOct(c))
deba@290
   310
            throw FormatError("Escape format error");
alpar@209
   311
          else if (code = valueOct(c), !is.get(c) || !isOct(c))
alpar@209
   312
            is.putback(c);
alpar@209
   313
          else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
alpar@209
   314
            is.putback(c);
alpar@209
   315
          else code = code * 8 + valueOct(c);
alpar@209
   316
          return code;
alpar@209
   317
        }
alpar@209
   318
      }
deba@127
   319
    }
alpar@209
   320
deba@197
   321
    inline std::istream& readToken(std::istream& is, std::string& str) {
deba@127
   322
      std::ostringstream os;
deba@127
   323
deba@127
   324
      char c;
deba@127
   325
      is >> std::ws;
alpar@209
   326
alpar@209
   327
      if (!is.get(c))
alpar@209
   328
        return is;
deba@127
   329
deba@127
   330
      if (c == '\"') {
alpar@209
   331
        while (is.get(c) && c != '\"') {
alpar@209
   332
          if (c == '\\')
alpar@209
   333
            c = readEscape(is);
alpar@209
   334
          os << c;
alpar@209
   335
        }
alpar@209
   336
        if (!is)
deba@290
   337
          throw FormatError("Quoted format error");
deba@127
   338
      } else {
alpar@209
   339
        is.putback(c);
alpar@209
   340
        while (is.get(c) && !isWhiteSpace(c)) {
alpar@209
   341
          if (c == '\\')
alpar@209
   342
            c = readEscape(is);
alpar@209
   343
          os << c;
alpar@209
   344
        }
alpar@209
   345
        if (!is) {
alpar@209
   346
          is.clear();
alpar@209
   347
        } else {
alpar@209
   348
          is.putback(c);
alpar@209
   349
        }
deba@127
   350
      }
deba@127
   351
      str = os.str();
deba@127
   352
      return is;
deba@127
   353
    }
deba@162
   354
deba@162
   355
    class Section {
deba@162
   356
    public:
deba@162
   357
      virtual ~Section() {}
deba@162
   358
      virtual void process(std::istream& is, int& line_num) = 0;
deba@162
   359
    };
deba@162
   360
deba@162
   361
    template <typename Functor>
deba@162
   362
    class LineSection : public Section {
deba@162
   363
    private:
deba@162
   364
deba@162
   365
      Functor _functor;
deba@162
   366
deba@162
   367
    public:
alpar@209
   368
deba@162
   369
      LineSection(const Functor& functor) : _functor(functor) {}
deba@162
   370
      virtual ~LineSection() {}
deba@162
   371
deba@162
   372
      virtual void process(std::istream& is, int& line_num) {
alpar@209
   373
        char c;
alpar@209
   374
        std::string line;
alpar@209
   375
        while (is.get(c) && c != '@') {
alpar@209
   376
          if (c == '\n') {
alpar@209
   377
            ++line_num;
alpar@209
   378
          } else if (c == '#') {
alpar@209
   379
            getline(is, line);
alpar@209
   380
            ++line_num;
alpar@209
   381
          } else if (!isWhiteSpace(c)) {
alpar@209
   382
            is.putback(c);
alpar@209
   383
            getline(is, line);
alpar@209
   384
            _functor(line);
alpar@209
   385
            ++line_num;
alpar@209
   386
          }
alpar@209
   387
        }
alpar@209
   388
        if (is) is.putback(c);
alpar@209
   389
        else if (is.eof()) is.clear();
deba@162
   390
      }
deba@162
   391
    };
deba@162
   392
deba@162
   393
    template <typename Functor>
deba@162
   394
    class StreamSection : public Section {
deba@162
   395
    private:
deba@162
   396
deba@162
   397
      Functor _functor;
deba@162
   398
deba@162
   399
    public:
alpar@209
   400
deba@162
   401
      StreamSection(const Functor& functor) : _functor(functor) {}
alpar@209
   402
      virtual ~StreamSection() {}
deba@162
   403
deba@162
   404
      virtual void process(std::istream& is, int& line_num) {
alpar@209
   405
        _functor(is, line_num);
alpar@209
   406
        char c;
alpar@209
   407
        std::string line;
alpar@209
   408
        while (is.get(c) && c != '@') {
alpar@209
   409
          if (c == '\n') {
alpar@209
   410
            ++line_num;
alpar@209
   411
          } else if (!isWhiteSpace(c)) {
alpar@209
   412
            getline(is, line);
alpar@209
   413
            ++line_num;
alpar@209
   414
          }
alpar@209
   415
        }
alpar@209
   416
        if (is) is.putback(c);
alpar@209
   417
        else if (is.eof()) is.clear();
deba@162
   418
      }
deba@162
   419
    };
alpar@209
   420
deba@127
   421
  }
alpar@156
   422
deba@598
   423
  template <typename DGR>
deba@190
   424
  class DigraphReader;
deba@190
   425
deba@598
   426
  template <typename TDGR>
deba@598
   427
  DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin);
deba@598
   428
  template <typename TDGR>
deba@598
   429
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn);
deba@598
   430
  template <typename TDGR>
deba@598
   431
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
deba@190
   432
alpar@156
   433
  /// \ingroup lemon_io
alpar@209
   434
  ///
kpeter@192
   435
  /// \brief \ref lgf-format "LGF" reader for directed graphs
alpar@156
   436
  ///
alpar@156
   437
  /// This utility reads an \ref lgf-format "LGF" file.
alpar@156
   438
  ///
alpar@156
   439
  /// The reading method does a batch processing. The user creates a
alpar@156
   440
  /// reader object, then various reading rules can be added to the
alpar@156
   441
  /// reader, and eventually the reading is executed with the \c run()
alpar@156
   442
  /// member function. A map reading rule can be added to the reader
alpar@156
   443
  /// with the \c nodeMap() or \c arcMap() members. An optional
deba@162
   444
  /// converter parameter can also be added as a standard functor
kpeter@192
   445
  /// converting from \c std::string to the value type of the map. If it
deba@162
   446
  /// is set, it will determine how the tokens in the file should be
kpeter@192
   447
  /// converted to the value type of the map. If the functor is not set,
deba@162
   448
  /// then a default conversion will be used. One map can be read into
deba@162
   449
  /// multiple map objects at the same time. The \c attribute(), \c
deba@162
   450
  /// node() and \c arc() functions are used to add attribute reading
deba@162
   451
  /// rules.
alpar@156
   452
  ///
alpar@156
   453
  ///\code
deba@598
   454
  /// DigraphReader<DGR>(digraph, std::cin).
kpeter@192
   455
  ///   nodeMap("coordinates", coord_map).
kpeter@192
   456
  ///   arcMap("capacity", cap_map).
kpeter@192
   457
  ///   node("source", src).
kpeter@192
   458
  ///   node("target", trg).
kpeter@192
   459
  ///   attribute("caption", caption).
kpeter@192
   460
  ///   run();
alpar@156
   461
  ///\endcode
alpar@156
   462
  ///
kpeter@786
   463
  /// By default, the reader uses the first section in the file of the
alpar@156
   464
  /// proper type. If a section has an optional name, then it can be
deba@162
   465
  /// selected for reading by giving an optional name parameter to the
deba@189
   466
  /// \c nodes(), \c arcs() or \c attributes() functions.
alpar@156
   467
  ///
alpar@156
   468
  /// The \c useNodes() and \c useArcs() functions are used to tell the reader
alpar@156
   469
  /// that the nodes or arcs should not be constructed (added to the
alpar@156
   470
  /// graph) during the reading, but instead the label map of the items
alpar@156
   471
  /// are given as a parameter of these functions. An
kpeter@192
   472
  /// application of these functions is multipass reading, which is
kpeter@192
   473
  /// important if two \c \@arcs sections must be read from the
kpeter@192
   474
  /// file. In this case the first phase would read the node set and one
alpar@156
   475
  /// of the arc sets, while the second phase would read the second arc
alpar@156
   476
  /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
alpar@156
   477
  /// The previously read label node map should be passed to the \c
alpar@156
   478
  /// useNodes() functions. Another application of multipass reading when
alpar@210
   479
  /// paths are given as a node map or an arc map.
alpar@210
   480
  /// It is impossible to read this in
alpar@156
   481
  /// a single pass, because the arcs are not constructed when the node
alpar@156
   482
  /// maps are read.
deba@598
   483
  template <typename DGR>
deba@127
   484
  class DigraphReader {
deba@127
   485
  public:
deba@127
   486
deba@598
   487
    typedef DGR Digraph;
kpeter@559
   488
kpeter@559
   489
  private:
kpeter@559
   490
deba@598
   491
    TEMPLATE_DIGRAPH_TYPEDEFS(DGR);
alpar@209
   492
deba@127
   493
    std::istream* _is;
deba@127
   494
    bool local_is;
deba@290
   495
    std::string _filename;
deba@127
   496
deba@598
   497
    DGR& _digraph;
deba@127
   498
deba@127
   499
    std::string _nodes_caption;
deba@127
   500
    std::string _arcs_caption;
deba@127
   501
    std::string _attributes_caption;
deba@127
   502
deba@127
   503
    typedef std::map<std::string, Node> NodeIndex;
deba@127
   504
    NodeIndex _node_index;
deba@127
   505
    typedef std::map<std::string, Arc> ArcIndex;
deba@127
   506
    ArcIndex _arc_index;
alpar@209
   507
alpar@209
   508
    typedef std::vector<std::pair<std::string,
alpar@209
   509
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
alpar@209
   510
    NodeMaps _node_maps;
deba@127
   511
deba@127
   512
    typedef std::vector<std::pair<std::string,
deba@127
   513
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
deba@127
   514
    ArcMaps _arc_maps;
deba@127
   515
alpar@209
   516
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
deba@127
   517
      Attributes;
deba@127
   518
    Attributes _attributes;
deba@127
   519
deba@127
   520
    bool _use_nodes;
deba@127
   521
    bool _use_arcs;
deba@127
   522
deba@188
   523
    bool _skip_nodes;
deba@188
   524
    bool _skip_arcs;
deba@188
   525
deba@127
   526
    int line_num;
deba@127
   527
    std::istringstream line;
deba@127
   528
deba@127
   529
  public:
deba@127
   530
alpar@156
   531
    /// \brief Constructor
alpar@156
   532
    ///
alpar@156
   533
    /// Construct a directed graph reader, which reads from the given
alpar@156
   534
    /// input stream.
deba@598
   535
    DigraphReader(DGR& digraph, std::istream& is = std::cin)
deba@127
   536
      : _is(&is), local_is(false), _digraph(digraph),
alpar@209
   537
        _use_nodes(false), _use_arcs(false),
alpar@209
   538
        _skip_nodes(false), _skip_arcs(false) {}
deba@127
   539
alpar@156
   540
    /// \brief Constructor
alpar@156
   541
    ///
alpar@156
   542
    /// Construct a directed graph reader, which reads from the given
alpar@156
   543
    /// file.
deba@598
   544
    DigraphReader(DGR& digraph, const std::string& fn)
deba@290
   545
      : _is(new std::ifstream(fn.c_str())), local_is(true),
deba@290
   546
        _filename(fn), _digraph(digraph),
kpeter@212
   547
        _use_nodes(false), _use_arcs(false),
deba@290
   548
        _skip_nodes(false), _skip_arcs(false) {
deba@295
   549
      if (!(*_is)) {
deba@295
   550
        delete _is;
deba@295
   551
        throw IoError("Cannot open file", fn);
deba@295
   552
      }
deba@290
   553
    }
alpar@209
   554
alpar@156
   555
    /// \brief Constructor
alpar@156
   556
    ///
alpar@156
   557
    /// Construct a directed graph reader, which reads from the given
alpar@156
   558
    /// file.
deba@598
   559
    DigraphReader(DGR& digraph, const char* fn)
deba@290
   560
      : _is(new std::ifstream(fn)), local_is(true),
deba@290
   561
        _filename(fn), _digraph(digraph),
kpeter@212
   562
        _use_nodes(false), _use_arcs(false),
deba@290
   563
        _skip_nodes(false), _skip_arcs(false) {
deba@295
   564
      if (!(*_is)) {
deba@295
   565
        delete _is;
deba@295
   566
        throw IoError("Cannot open file", fn);
deba@295
   567
      }
deba@290
   568
    }
deba@127
   569
alpar@156
   570
    /// \brief Destructor
deba@127
   571
    ~DigraphReader() {
alpar@209
   572
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   573
           it != _node_maps.end(); ++it) {
alpar@209
   574
        delete it->second;
deba@127
   575
      }
deba@127
   576
alpar@209
   577
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   578
           it != _arc_maps.end(); ++it) {
alpar@209
   579
        delete it->second;
deba@127
   580
      }
deba@127
   581
alpar@209
   582
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
   583
           it != _attributes.end(); ++it) {
alpar@209
   584
        delete it->second;
deba@127
   585
      }
deba@127
   586
deba@127
   587
      if (local_is) {
alpar@209
   588
        delete _is;
deba@127
   589
      }
deba@127
   590
deba@127
   591
    }
deba@127
   592
deba@127
   593
  private:
deba@190
   594
deba@598
   595
    template <typename TDGR>
deba@598
   596
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is);
deba@598
   597
    template <typename TDGR>
alpar@877
   598
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph,
deba@598
   599
                                             const std::string& fn);
deba@598
   600
    template <typename TDGR>
deba@598
   601
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
alpar@209
   602
alpar@209
   603
    DigraphReader(DigraphReader& other)
deba@190
   604
      : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
alpar@209
   605
        _use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
alpar@209
   606
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
deba@190
   607
deba@190
   608
      other._is = 0;
deba@190
   609
      other.local_is = false;
alpar@209
   610
deba@190
   611
      _node_index.swap(other._node_index);
deba@190
   612
      _arc_index.swap(other._arc_index);
deba@190
   613
deba@190
   614
      _node_maps.swap(other._node_maps);
deba@190
   615
      _arc_maps.swap(other._arc_maps);
deba@190
   616
      _attributes.swap(other._attributes);
deba@190
   617
deba@190
   618
      _nodes_caption = other._nodes_caption;
deba@190
   619
      _arcs_caption = other._arcs_caption;
deba@190
   620
      _attributes_caption = other._attributes_caption;
deba@190
   621
deba@190
   622
    }
deba@190
   623
deba@127
   624
    DigraphReader& operator=(const DigraphReader&);
deba@127
   625
deba@127
   626
  public:
deba@127
   627
kpeter@584
   628
    /// \name Reading Rules
alpar@156
   629
    /// @{
alpar@209
   630
alpar@156
   631
    /// \brief Node map reading rule
alpar@156
   632
    ///
alpar@156
   633
    /// Add a node map reading rule to the reader.
deba@127
   634
    template <typename Map>
deba@127
   635
    DigraphReader& nodeMap(const std::string& caption, Map& map) {
deba@127
   636
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
alpar@209
   637
      _reader_bits::MapStorageBase<Node>* storage =
alpar@209
   638
        new _reader_bits::MapStorage<Node, Map>(map);
deba@127
   639
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   640
      return *this;
deba@127
   641
    }
deba@127
   642
alpar@156
   643
    /// \brief Node map reading rule
alpar@156
   644
    ///
alpar@156
   645
    /// Add a node map reading rule with specialized converter to the
alpar@156
   646
    /// reader.
deba@127
   647
    template <typename Map, typename Converter>
alpar@209
   648
    DigraphReader& nodeMap(const std::string& caption, Map& map,
alpar@209
   649
                           const Converter& converter = Converter()) {
deba@127
   650
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
alpar@209
   651
      _reader_bits::MapStorageBase<Node>* storage =
alpar@209
   652
        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@127
   653
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   654
      return *this;
deba@127
   655
    }
deba@127
   656
alpar@156
   657
    /// \brief Arc map reading rule
alpar@156
   658
    ///
alpar@156
   659
    /// Add an arc map reading rule to the reader.
deba@127
   660
    template <typename Map>
deba@127
   661
    DigraphReader& arcMap(const std::string& caption, Map& map) {
deba@127
   662
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
alpar@209
   663
      _reader_bits::MapStorageBase<Arc>* storage =
alpar@209
   664
        new _reader_bits::MapStorage<Arc, Map>(map);
deba@127
   665
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   666
      return *this;
deba@127
   667
    }
deba@127
   668
alpar@156
   669
    /// \brief Arc map reading rule
alpar@156
   670
    ///
alpar@156
   671
    /// Add an arc map reading rule with specialized converter to the
alpar@156
   672
    /// reader.
deba@127
   673
    template <typename Map, typename Converter>
alpar@209
   674
    DigraphReader& arcMap(const std::string& caption, Map& map,
alpar@209
   675
                          const Converter& converter = Converter()) {
deba@127
   676
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
alpar@209
   677
      _reader_bits::MapStorageBase<Arc>* storage =
alpar@209
   678
        new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
deba@127
   679
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   680
      return *this;
deba@127
   681
    }
deba@127
   682
alpar@156
   683
    /// \brief Attribute reading rule
alpar@156
   684
    ///
alpar@156
   685
    /// Add an attribute reading rule to the reader.
deba@127
   686
    template <typename Value>
deba@127
   687
    DigraphReader& attribute(const std::string& caption, Value& value) {
alpar@209
   688
      _reader_bits::ValueStorageBase* storage =
alpar@209
   689
        new _reader_bits::ValueStorage<Value>(value);
deba@127
   690
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   691
      return *this;
deba@127
   692
    }
deba@127
   693
alpar@156
   694
    /// \brief Attribute reading rule
alpar@156
   695
    ///
alpar@156
   696
    /// Add an attribute reading rule with specialized converter to the
alpar@156
   697
    /// reader.
deba@127
   698
    template <typename Value, typename Converter>
alpar@209
   699
    DigraphReader& attribute(const std::string& caption, Value& value,
alpar@209
   700
                             const Converter& converter = Converter()) {
alpar@209
   701
      _reader_bits::ValueStorageBase* storage =
alpar@209
   702
        new _reader_bits::ValueStorage<Value, Converter>(value, converter);
deba@127
   703
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   704
      return *this;
deba@127
   705
    }
deba@127
   706
alpar@156
   707
    /// \brief Node reading rule
alpar@156
   708
    ///
alpar@156
   709
    /// Add a node reading rule to reader.
deba@127
   710
    DigraphReader& node(const std::string& caption, Node& node) {
deba@127
   711
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
deba@127
   712
      Converter converter(_node_index);
alpar@209
   713
      _reader_bits::ValueStorageBase* storage =
alpar@209
   714
        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
deba@127
   715
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   716
      return *this;
deba@127
   717
    }
deba@127
   718
alpar@156
   719
    /// \brief Arc reading rule
alpar@156
   720
    ///
alpar@156
   721
    /// Add an arc reading rule to reader.
deba@127
   722
    DigraphReader& arc(const std::string& caption, Arc& arc) {
deba@127
   723
      typedef _reader_bits::MapLookUpConverter<Arc> Converter;
deba@127
   724
      Converter converter(_arc_index);
alpar@209
   725
      _reader_bits::ValueStorageBase* storage =
alpar@209
   726
        new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@127
   727
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   728
      return *this;
deba@127
   729
    }
deba@127
   730
alpar@156
   731
    /// @}
alpar@156
   732
kpeter@584
   733
    /// \name Select Section by Name
alpar@156
   734
    /// @{
alpar@156
   735
alpar@156
   736
    /// \brief Set \c \@nodes section to be read
alpar@156
   737
    ///
alpar@156
   738
    /// Set \c \@nodes section to be read
deba@127
   739
    DigraphReader& nodes(const std::string& caption) {
deba@127
   740
      _nodes_caption = caption;
deba@127
   741
      return *this;
deba@127
   742
    }
deba@127
   743
alpar@156
   744
    /// \brief Set \c \@arcs section to be read
alpar@156
   745
    ///
alpar@156
   746
    /// Set \c \@arcs section to be read
deba@127
   747
    DigraphReader& arcs(const std::string& caption) {
deba@127
   748
      _arcs_caption = caption;
deba@127
   749
      return *this;
deba@127
   750
    }
deba@127
   751
alpar@156
   752
    /// \brief Set \c \@attributes section to be read
alpar@156
   753
    ///
alpar@156
   754
    /// Set \c \@attributes section to be read
deba@127
   755
    DigraphReader& attributes(const std::string& caption) {
deba@127
   756
      _attributes_caption = caption;
deba@127
   757
      return *this;
deba@127
   758
    }
deba@127
   759
alpar@156
   760
    /// @}
alpar@156
   761
kpeter@584
   762
    /// \name Using Previously Constructed Node or Arc Set
alpar@156
   763
    /// @{
alpar@156
   764
alpar@156
   765
    /// \brief Use previously constructed node set
alpar@156
   766
    ///
alpar@156
   767
    /// Use previously constructed node set, and specify the node
alpar@156
   768
    /// label map.
deba@127
   769
    template <typename Map>
deba@127
   770
    DigraphReader& useNodes(const Map& map) {
deba@127
   771
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   772
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@127
   773
      _use_nodes = true;
deba@127
   774
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@127
   775
      for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   776
        _node_index.insert(std::make_pair(converter(map[n]), n));
deba@127
   777
      }
deba@127
   778
      return *this;
deba@127
   779
    }
deba@127
   780
alpar@156
   781
    /// \brief Use previously constructed node set
alpar@156
   782
    ///
alpar@156
   783
    /// Use previously constructed node set, and specify the node
alpar@156
   784
    /// label map and a functor which converts the label map values to
kpeter@192
   785
    /// \c std::string.
deba@127
   786
    template <typename Map, typename Converter>
alpar@209
   787
    DigraphReader& useNodes(const Map& map,
alpar@209
   788
                            const Converter& converter = Converter()) {
deba@127
   789
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   790
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@127
   791
      _use_nodes = true;
deba@127
   792
      for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   793
        _node_index.insert(std::make_pair(converter(map[n]), n));
deba@127
   794
      }
deba@127
   795
      return *this;
deba@127
   796
    }
deba@127
   797
alpar@156
   798
    /// \brief Use previously constructed arc set
alpar@156
   799
    ///
alpar@156
   800
    /// Use previously constructed arc set, and specify the arc
alpar@156
   801
    /// label map.
deba@127
   802
    template <typename Map>
deba@127
   803
    DigraphReader& useArcs(const Map& map) {
deba@127
   804
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
deba@127
   805
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
deba@127
   806
      _use_arcs = true;
deba@127
   807
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@127
   808
      for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   809
        _arc_index.insert(std::make_pair(converter(map[a]), a));
deba@127
   810
      }
deba@127
   811
      return *this;
deba@127
   812
    }
deba@127
   813
alpar@156
   814
    /// \brief Use previously constructed arc set
alpar@156
   815
    ///
alpar@156
   816
    /// Use previously constructed arc set, and specify the arc
alpar@156
   817
    /// label map and a functor which converts the label map values to
kpeter@192
   818
    /// \c std::string.
deba@127
   819
    template <typename Map, typename Converter>
alpar@209
   820
    DigraphReader& useArcs(const Map& map,
alpar@209
   821
                           const Converter& converter = Converter()) {
deba@127
   822
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
   823
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
deba@127
   824
      _use_arcs = true;
deba@127
   825
      for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   826
        _arc_index.insert(std::make_pair(converter(map[a]), a));
deba@127
   827
      }
deba@127
   828
      return *this;
deba@127
   829
    }
deba@127
   830
deba@188
   831
    /// \brief Skips the reading of node section
deba@188
   832
    ///
deba@188
   833
    /// Omit the reading of the node section. This implies that each node
kpeter@192
   834
    /// map reading rule will be abandoned, and the nodes of the graph
deba@188
   835
    /// will not be constructed, which usually cause that the arc set
kpeter@192
   836
    /// could not be read due to lack of node name resolving.
kpeter@192
   837
    /// Therefore \c skipArcs() function should also be used, or
kpeter@192
   838
    /// \c useNodes() should be used to specify the label of the nodes.
deba@188
   839
    DigraphReader& skipNodes() {
alpar@209
   840
      LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
deba@188
   841
      _skip_nodes = true;
deba@188
   842
      return *this;
deba@188
   843
    }
deba@188
   844
deba@188
   845
    /// \brief Skips the reading of arc section
deba@188
   846
    ///
deba@188
   847
    /// Omit the reading of the arc section. This implies that each arc
kpeter@192
   848
    /// map reading rule will be abandoned, and the arcs of the graph
deba@188
   849
    /// will not be constructed.
deba@188
   850
    DigraphReader& skipArcs() {
alpar@209
   851
      LEMON_ASSERT(!_skip_arcs, "Skip arcs already set");
deba@188
   852
      _skip_arcs = true;
deba@188
   853
      return *this;
deba@188
   854
    }
deba@188
   855
alpar@156
   856
    /// @}
alpar@156
   857
deba@127
   858
  private:
deba@127
   859
deba@127
   860
    bool readLine() {
deba@127
   861
      std::string str;
deba@127
   862
      while(++line_num, std::getline(*_is, str)) {
alpar@209
   863
        line.clear(); line.str(str);
alpar@209
   864
        char c;
alpar@209
   865
        if (line >> std::ws >> c && c != '#') {
alpar@209
   866
          line.putback(c);
alpar@209
   867
          return true;
alpar@209
   868
        }
deba@127
   869
      }
deba@127
   870
      return false;
deba@127
   871
    }
deba@127
   872
deba@127
   873
    bool readSuccess() {
deba@127
   874
      return static_cast<bool>(*_is);
deba@127
   875
    }
alpar@209
   876
deba@127
   877
    void skipSection() {
deba@127
   878
      char c;
deba@127
   879
      while (readSuccess() && line >> c && c != '@') {
alpar@209
   880
        readLine();
deba@127
   881
      }
deba@427
   882
      if (readSuccess()) {
deba@427
   883
        line.putback(c);
deba@427
   884
      }
deba@127
   885
    }
deba@127
   886
deba@127
   887
    void readNodes() {
deba@127
   888
deba@127
   889
      std::vector<int> map_index(_node_maps.size());
deba@127
   890
      int map_num, label_index;
deba@127
   891
deba@186
   892
      char c;
deba@186
   893
      if (!readLine() || !(line >> c) || c == '@') {
alpar@209
   894
        if (readSuccess() && line) line.putback(c);
alpar@209
   895
        if (!_node_maps.empty())
deba@290
   896
          throw FormatError("Cannot find map names");
alpar@209
   897
        return;
deba@186
   898
      }
deba@186
   899
      line.putback(c);
deba@186
   900
deba@127
   901
      {
alpar@209
   902
        std::map<std::string, int> maps;
alpar@209
   903
alpar@209
   904
        std::string map;
alpar@209
   905
        int index = 0;
alpar@209
   906
        while (_reader_bits::readToken(line, map)) {
alpar@209
   907
          if (maps.find(map) != maps.end()) {
alpar@209
   908
            std::ostringstream msg;
alpar@209
   909
            msg << "Multiple occurence of node map: " << map;
deba@290
   910
            throw FormatError(msg.str());
alpar@209
   911
          }
alpar@209
   912
          maps.insert(std::make_pair(map, index));
alpar@209
   913
          ++index;
alpar@209
   914
        }
alpar@209
   915
alpar@209
   916
        for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
alpar@209
   917
          std::map<std::string, int>::iterator jt =
alpar@209
   918
            maps.find(_node_maps[i].first);
alpar@209
   919
          if (jt == maps.end()) {
alpar@209
   920
            std::ostringstream msg;
kpeter@291
   921
            msg << "Map not found: " << _node_maps[i].first;
deba@290
   922
            throw FormatError(msg.str());
alpar@209
   923
          }
alpar@209
   924
          map_index[i] = jt->second;
alpar@209
   925
        }
alpar@209
   926
alpar@209
   927
        {
alpar@209
   928
          std::map<std::string, int>::iterator jt = maps.find("label");
alpar@209
   929
          if (jt != maps.end()) {
alpar@209
   930
            label_index = jt->second;
alpar@209
   931
          } else {
alpar@209
   932
            label_index = -1;
alpar@209
   933
          }
alpar@209
   934
        }
alpar@209
   935
        map_num = maps.size();
deba@127
   936
      }
deba@127
   937
deba@127
   938
      while (readLine() && line >> c && c != '@') {
alpar@209
   939
        line.putback(c);
alpar@209
   940
alpar@209
   941
        std::vector<std::string> tokens(map_num);
alpar@209
   942
        for (int i = 0; i < map_num; ++i) {
alpar@209
   943
          if (!_reader_bits::readToken(line, tokens[i])) {
alpar@209
   944
            std::ostringstream msg;
alpar@209
   945
            msg << "Column not found (" << i + 1 << ")";
deba@290
   946
            throw FormatError(msg.str());
alpar@209
   947
          }
alpar@209
   948
        }
alpar@209
   949
        if (line >> std::ws >> c)
kpeter@291
   950
          throw FormatError("Extra character at the end of line");
alpar@209
   951
alpar@209
   952
        Node n;
alpar@209
   953
        if (!_use_nodes) {
alpar@209
   954
          n = _digraph.addNode();
alpar@209
   955
          if (label_index != -1)
alpar@209
   956
            _node_index.insert(std::make_pair(tokens[label_index], n));
alpar@209
   957
        } else {
alpar@209
   958
          if (label_index == -1)
kpeter@291
   959
            throw FormatError("Label map not found");
alpar@209
   960
          typename std::map<std::string, Node>::iterator it =
alpar@209
   961
            _node_index.find(tokens[label_index]);
alpar@209
   962
          if (it == _node_index.end()) {
alpar@209
   963
            std::ostringstream msg;
alpar@209
   964
            msg << "Node with label not found: " << tokens[label_index];
deba@290
   965
            throw FormatError(msg.str());
alpar@209
   966
          }
alpar@209
   967
          n = it->second;
alpar@209
   968
        }
alpar@209
   969
alpar@209
   970
        for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
alpar@209
   971
          _node_maps[i].second->set(n, tokens[map_index[i]]);
alpar@209
   972
        }
deba@127
   973
deba@127
   974
      }
deba@127
   975
      if (readSuccess()) {
alpar@209
   976
        line.putback(c);
deba@127
   977
      }
deba@127
   978
    }
deba@127
   979
deba@127
   980
    void readArcs() {
deba@127
   981
deba@127
   982
      std::vector<int> map_index(_arc_maps.size());
deba@127
   983
      int map_num, label_index;
deba@127
   984
deba@186
   985
      char c;
deba@186
   986
      if (!readLine() || !(line >> c) || c == '@') {
alpar@209
   987
        if (readSuccess() && line) line.putback(c);
alpar@209
   988
        if (!_arc_maps.empty())
deba@290
   989
          throw FormatError("Cannot find map names");
alpar@209
   990
        return;
deba@186
   991
      }
deba@186
   992
      line.putback(c);
alpar@209
   993
deba@127
   994
      {
alpar@209
   995
        std::map<std::string, int> maps;
alpar@209
   996
alpar@209
   997
        std::string map;
alpar@209
   998
        int index = 0;
alpar@209
   999
        while (_reader_bits::readToken(line, map)) {
alpar@949
  1000
          if(map == "-") {
alpar@949
  1001
              if(index!=0)
alpar@949
  1002
                throw FormatError("'-' is not allowed as a map name");
alpar@949
  1003
              else if (line >> std::ws >> c)
alpar@949
  1004
                throw FormatError("Extra character at the end of line");
alpar@949
  1005
              else break;
alpar@949
  1006
            }
alpar@209
  1007
          if (maps.find(map) != maps.end()) {
alpar@209
  1008
            std::ostringstream msg;
alpar@209
  1009
            msg << "Multiple occurence of arc map: " << map;
deba@290
  1010
            throw FormatError(msg.str());
alpar@209
  1011
          }
alpar@209
  1012
          maps.insert(std::make_pair(map, index));
alpar@209
  1013
          ++index;
alpar@209
  1014
        }
alpar@209
  1015
alpar@209
  1016
        for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
alpar@209
  1017
          std::map<std::string, int>::iterator jt =
alpar@209
  1018
            maps.find(_arc_maps[i].first);
alpar@209
  1019
          if (jt == maps.end()) {
alpar@209
  1020
            std::ostringstream msg;
kpeter@291
  1021
            msg << "Map not found: " << _arc_maps[i].first;
deba@290
  1022
            throw FormatError(msg.str());
alpar@209
  1023
          }
alpar@209
  1024
          map_index[i] = jt->second;
alpar@209
  1025
        }
alpar@209
  1026
alpar@209
  1027
        {
alpar@209
  1028
          std::map<std::string, int>::iterator jt = maps.find("label");
alpar@209
  1029
          if (jt != maps.end()) {
alpar@209
  1030
            label_index = jt->second;
alpar@209
  1031
          } else {
alpar@209
  1032
            label_index = -1;
alpar@209
  1033
          }
alpar@209
  1034
        }
alpar@209
  1035
        map_num = maps.size();
deba@127
  1036
      }
deba@127
  1037
deba@127
  1038
      while (readLine() && line >> c && c != '@') {
alpar@209
  1039
        line.putback(c);
alpar@209
  1040
alpar@209
  1041
        std::string source_token;
alpar@209
  1042
        std::string target_token;
alpar@209
  1043
alpar@209
  1044
        if (!_reader_bits::readToken(line, source_token))
deba@290
  1045
          throw FormatError("Source not found");
alpar@209
  1046
alpar@209
  1047
        if (!_reader_bits::readToken(line, target_token))
deba@290
  1048
          throw FormatError("Target not found");
alpar@209
  1049
alpar@209
  1050
        std::vector<std::string> tokens(map_num);
alpar@209
  1051
        for (int i = 0; i < map_num; ++i) {
alpar@209
  1052
          if (!_reader_bits::readToken(line, tokens[i])) {
alpar@209
  1053
            std::ostringstream msg;
alpar@209
  1054
            msg << "Column not found (" << i + 1 << ")";
deba@290
  1055
            throw FormatError(msg.str());
alpar@209
  1056
          }
alpar@209
  1057
        }
alpar@209
  1058
        if (line >> std::ws >> c)
kpeter@291
  1059
          throw FormatError("Extra character at the end of line");
alpar@209
  1060
alpar@209
  1061
        Arc a;
alpar@209
  1062
        if (!_use_arcs) {
deba@127
  1063
deba@127
  1064
          typename NodeIndex::iterator it;
alpar@209
  1065
deba@127
  1066
          it = _node_index.find(source_token);
deba@127
  1067
          if (it == _node_index.end()) {
deba@127
  1068
            std::ostringstream msg;
deba@127
  1069
            msg << "Item not found: " << source_token;
deba@290
  1070
            throw FormatError(msg.str());
deba@127
  1071
          }
deba@127
  1072
          Node source = it->second;
deba@127
  1073
deba@127
  1074
          it = _node_index.find(target_token);
alpar@209
  1075
          if (it == _node_index.end()) {
alpar@209
  1076
            std::ostringstream msg;
deba@127
  1077
            msg << "Item not found: " << target_token;
deba@290
  1078
            throw FormatError(msg.str());
alpar@209
  1079
          }
alpar@209
  1080
          Node target = it->second;
alpar@209
  1081
alpar@209
  1082
          a = _digraph.addArc(source, target);
alpar@209
  1083
          if (label_index != -1)
alpar@209
  1084
            _arc_index.insert(std::make_pair(tokens[label_index], a));
alpar@209
  1085
        } else {
alpar@209
  1086
          if (label_index == -1)
kpeter@291
  1087
            throw FormatError("Label map not found");
alpar@209
  1088
          typename std::map<std::string, Arc>::iterator it =
alpar@209
  1089
            _arc_index.find(tokens[label_index]);
alpar@209
  1090
          if (it == _arc_index.end()) {
alpar@209
  1091
            std::ostringstream msg;
alpar@209
  1092
            msg << "Arc with label not found: " << tokens[label_index];
deba@290
  1093
            throw FormatError(msg.str());
alpar@209
  1094
          }
alpar@209
  1095
          a = it->second;
alpar@209
  1096
        }
alpar@209
  1097
alpar@209
  1098
        for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
alpar@209
  1099
          _arc_maps[i].second->set(a, tokens[map_index[i]]);
alpar@209
  1100
        }
deba@127
  1101
deba@127
  1102
      }
deba@127
  1103
      if (readSuccess()) {
alpar@209
  1104
        line.putback(c);
deba@127
  1105
      }
deba@127
  1106
    }
deba@127
  1107
deba@127
  1108
    void readAttributes() {
deba@127
  1109
deba@127
  1110
      std::set<std::string> read_attr;
deba@127
  1111
deba@127
  1112
      char c;
deba@127
  1113
      while (readLine() && line >> c && c != '@') {
alpar@209
  1114
        line.putback(c);
alpar@209
  1115
alpar@209
  1116
        std::string attr, token;
alpar@209
  1117
        if (!_reader_bits::readToken(line, attr))
deba@290
  1118
          throw FormatError("Attribute name not found");
alpar@209
  1119
        if (!_reader_bits::readToken(line, token))
deba@290
  1120
          throw FormatError("Attribute value not found");
alpar@209
  1121
        if (line >> c)
kpeter@291
  1122
          throw FormatError("Extra character at the end of line");
alpar@209
  1123
alpar@209
  1124
        {
alpar@209
  1125
          std::set<std::string>::iterator it = read_attr.find(attr);
alpar@209
  1126
          if (it != read_attr.end()) {
alpar@209
  1127
            std::ostringstream msg;
kpeter@291
  1128
            msg << "Multiple occurence of attribute: " << attr;
deba@290
  1129
            throw FormatError(msg.str());
alpar@209
  1130
          }
alpar@209
  1131
          read_attr.insert(attr);
alpar@209
  1132
        }
alpar@209
  1133
alpar@209
  1134
        {
alpar@209
  1135
          typename Attributes::iterator it = _attributes.lower_bound(attr);
alpar@209
  1136
          while (it != _attributes.end() && it->first == attr) {
alpar@209
  1137
            it->second->set(token);
alpar@209
  1138
            ++it;
alpar@209
  1139
          }
alpar@209
  1140
        }
deba@127
  1141
deba@127
  1142
      }
deba@127
  1143
      if (readSuccess()) {
alpar@209
  1144
        line.putback(c);
deba@127
  1145
      }
deba@127
  1146
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1147
           it != _attributes.end(); ++it) {
alpar@209
  1148
        if (read_attr.find(it->first) == read_attr.end()) {
alpar@209
  1149
          std::ostringstream msg;
kpeter@291
  1150
          msg << "Attribute not found: " << it->first;
deba@290
  1151
          throw FormatError(msg.str());
alpar@209
  1152
        }
deba@127
  1153
      }
deba@127
  1154
    }
deba@127
  1155
deba@127
  1156
  public:
alpar@156
  1157
kpeter@584
  1158
    /// \name Execution of the Reader
alpar@156
  1159
    /// @{
alpar@156
  1160
alpar@156
  1161
    /// \brief Start the batch processing
alpar@156
  1162
    ///
alpar@156
  1163
    /// This function starts the batch processing
deba@127
  1164
    void run() {
deba@127
  1165
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
alpar@209
  1166
deba@188
  1167
      bool nodes_done = _skip_nodes;
deba@188
  1168
      bool arcs_done = _skip_arcs;
deba@127
  1169
      bool attributes_done = false;
deba@127
  1170
alpar@209
  1171
      line_num = 0;
deba@127
  1172
      readLine();
deba@172
  1173
      skipSection();
deba@127
  1174
deba@127
  1175
      while (readSuccess()) {
alpar@209
  1176
        try {
alpar@209
  1177
          char c;
alpar@209
  1178
          std::string section, caption;
alpar@209
  1179
          line >> c;
alpar@209
  1180
          _reader_bits::readToken(line, section);
alpar@209
  1181
          _reader_bits::readToken(line, caption);
alpar@209
  1182
alpar@209
  1183
          if (line >> c)
kpeter@291
  1184
            throw FormatError("Extra character at the end of line");
alpar@209
  1185
alpar@209
  1186
          if (section == "nodes" && !nodes_done) {
alpar@209
  1187
            if (_nodes_caption.empty() || _nodes_caption == caption) {
alpar@209
  1188
              readNodes();
alpar@209
  1189
              nodes_done = true;
alpar@209
  1190
            }
alpar@209
  1191
          } else if ((section == "arcs" || section == "edges") &&
alpar@209
  1192
                     !arcs_done) {
alpar@209
  1193
            if (_arcs_caption.empty() || _arcs_caption == caption) {
alpar@209
  1194
              readArcs();
alpar@209
  1195
              arcs_done = true;
alpar@209
  1196
            }
alpar@209
  1197
          } else if (section == "attributes" && !attributes_done) {
alpar@209
  1198
            if (_attributes_caption.empty() || _attributes_caption == caption) {
alpar@209
  1199
              readAttributes();
alpar@209
  1200
              attributes_done = true;
alpar@209
  1201
            }
alpar@209
  1202
          } else {
alpar@209
  1203
            readLine();
alpar@209
  1204
            skipSection();
alpar@209
  1205
          }
deba@290
  1206
        } catch (FormatError& error) {
alpar@209
  1207
          error.line(line_num);
deba@290
  1208
          error.file(_filename);
alpar@209
  1209
          throw;
alpar@209
  1210
        }
deba@127
  1211
      }
deba@127
  1212
deba@127
  1213
      if (!nodes_done) {
deba@290
  1214
        throw FormatError("Section @nodes not found");
deba@127
  1215
      }
deba@127
  1216
deba@127
  1217
      if (!arcs_done) {
deba@290
  1218
        throw FormatError("Section @arcs not found");
deba@127
  1219
      }
deba@127
  1220
deba@127
  1221
      if (!attributes_done && !_attributes.empty()) {
deba@290
  1222
        throw FormatError("Section @attributes not found");
deba@127
  1223
      }
deba@127
  1224
deba@127
  1225
    }
alpar@156
  1226
alpar@156
  1227
    /// @}
alpar@209
  1228
deba@127
  1229
  };
alpar@877
  1230
deba@598
  1231
  /// \ingroup lemon_io
deba@598
  1232
  ///
deba@598
  1233
  /// \brief Return a \ref DigraphReader class
deba@598
  1234
  ///
deba@598
  1235
  /// This function just returns a \ref DigraphReader class.
deba@598
  1236
  ///
alpar@877
  1237
  /// With this function a digraph can be read from an
deba@598
  1238
  /// \ref lgf-format "LGF" file or input stream with several maps and
deba@598
  1239
  /// attributes. For example, there is network flow problem on a
deba@598
  1240
  /// digraph, i.e. a digraph with a \e capacity map on the arcs and
deba@598
  1241
  /// \e source and \e target nodes. This digraph can be read with the
deba@598
  1242
  /// following code:
deba@598
  1243
  ///
deba@598
  1244
  ///\code
deba@598
  1245
  ///ListDigraph digraph;
deba@598
  1246
  ///ListDigraph::ArcMap<int> cm(digraph);
deba@598
  1247
  ///ListDigraph::Node src, trg;
deba@598
  1248
  ///digraphReader(digraph, std::cin).
deba@598
  1249
  ///  arcMap("capacity", cap).
deba@598
  1250
  ///  node("source", src).
deba@598
  1251
  ///  node("target", trg).
deba@598
  1252
  ///  run();
deba@598
  1253
  ///\endcode
deba@598
  1254
  ///
deba@598
  1255
  /// For a complete documentation, please see the \ref DigraphReader
deba@598
  1256
  /// class documentation.
deba@598
  1257
  /// \warning Don't forget to put the \ref DigraphReader::run() "run()"
deba@598
  1258
  /// to the end of the parameter list.
deba@598
  1259
  /// \relates DigraphReader
deba@598
  1260
  /// \sa digraphReader(TDGR& digraph, const std::string& fn)
deba@598
  1261
  /// \sa digraphReader(TDGR& digraph, const char* fn)
deba@598
  1262
  template <typename TDGR>
deba@598
  1263
  DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) {
deba@598
  1264
    DigraphReader<TDGR> tmp(digraph, is);
deba@598
  1265
    return tmp;
deba@598
  1266
  }
deba@127
  1267
kpeter@487
  1268
  /// \brief Return a \ref DigraphReader class
kpeter@487
  1269
  ///
kpeter@487
  1270
  /// This function just returns a \ref DigraphReader class.
kpeter@487
  1271
  /// \relates DigraphReader
deba@598
  1272
  /// \sa digraphReader(TDGR& digraph, std::istream& is)
deba@598
  1273
  template <typename TDGR>
deba@598
  1274
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) {
deba@598
  1275
    DigraphReader<TDGR> tmp(digraph, fn);
kpeter@487
  1276
    return tmp;
kpeter@487
  1277
  }
kpeter@487
  1278
kpeter@487
  1279
  /// \brief Return a \ref DigraphReader class
kpeter@487
  1280
  ///
kpeter@487
  1281
  /// This function just returns a \ref DigraphReader class.
kpeter@487
  1282
  /// \relates DigraphReader
deba@598
  1283
  /// \sa digraphReader(TDGR& digraph, std::istream& is)
deba@598
  1284
  template <typename TDGR>
deba@598
  1285
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) {
deba@598
  1286
    DigraphReader<TDGR> tmp(digraph, fn);
kpeter@487
  1287
    return tmp;
kpeter@487
  1288
  }
kpeter@487
  1289
deba@598
  1290
  template <typename GR>
ladanyi@303
  1291
  class GraphReader;
alpar@877
  1292
deba@598
  1293
  template <typename TGR>
deba@598
  1294
  GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin);
deba@598
  1295
  template <typename TGR>
deba@598
  1296
  GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
deba@598
  1297
  template <typename TGR>
deba@598
  1298
  GraphReader<TGR> graphReader(TGR& graph, const char *fn);
deba@165
  1299
deba@165
  1300
  /// \ingroup lemon_io
alpar@209
  1301
  ///
kpeter@192
  1302
  /// \brief \ref lgf-format "LGF" reader for undirected graphs
deba@165
  1303
  ///
deba@165
  1304
  /// This utility reads an \ref lgf-format "LGF" file.
kpeter@192
  1305
  ///
kpeter@192
  1306
  /// It can be used almost the same way as \c DigraphReader.
kpeter@192
  1307
  /// The only difference is that this class can handle edges and
kpeter@192
  1308
  /// edge maps as well as arcs and arc maps.
deba@201
  1309
  ///
deba@201
  1310
  /// The columns in the \c \@edges (or \c \@arcs) section are the
deba@201
  1311
  /// edge maps. However, if there are two maps with the same name
deba@201
  1312
  /// prefixed with \c '+' and \c '-', then these can be read into an
deba@201
  1313
  /// arc map.  Similarly, an attribute can be read into an arc, if
deba@201
  1314
  /// it's value is an edge label prefixed with \c '+' or \c '-'.
kpeter@559
  1315
  template <typename GR>
deba@165
  1316
  class GraphReader {
deba@165
  1317
  public:
deba@165
  1318
kpeter@559
  1319
    typedef GR Graph;
kpeter@559
  1320
kpeter@559
  1321
  private:
kpeter@559
  1322
deba@598
  1323
    TEMPLATE_GRAPH_TYPEDEFS(GR);
alpar@209
  1324
deba@165
  1325
    std::istream* _is;
deba@165
  1326
    bool local_is;
deba@290
  1327
    std::string _filename;
deba@165
  1328
deba@598
  1329
    GR& _graph;
deba@165
  1330
deba@165
  1331
    std::string _nodes_caption;
deba@165
  1332
    std::string _edges_caption;
deba@165
  1333
    std::string _attributes_caption;
deba@165
  1334
deba@165
  1335
    typedef std::map<std::string, Node> NodeIndex;
deba@165
  1336
    NodeIndex _node_index;
deba@165
  1337
    typedef std::map<std::string, Edge> EdgeIndex;
deba@165
  1338
    EdgeIndex _edge_index;
alpar@209
  1339
alpar@209
  1340
    typedef std::vector<std::pair<std::string,
alpar@209
  1341
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
alpar@209
  1342
    NodeMaps _node_maps;
deba@165
  1343
deba@165
  1344
    typedef std::vector<std::pair<std::string,
deba@165
  1345
      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
deba@165
  1346
    EdgeMaps _edge_maps;
deba@165
  1347
alpar@209
  1348
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
deba@165
  1349
      Attributes;
deba@165
  1350
    Attributes _attributes;
deba@165
  1351
deba@165
  1352
    bool _use_nodes;
deba@165
  1353
    bool _use_edges;
deba@165
  1354
deba@188
  1355
    bool _skip_nodes;
deba@188
  1356
    bool _skip_edges;
deba@188
  1357
deba@165
  1358
    int line_num;
deba@165
  1359
    std::istringstream line;
deba@165
  1360
deba@165
  1361
  public:
deba@165
  1362
deba@165
  1363
    /// \brief Constructor
deba@165
  1364
    ///
kpeter@192
  1365
    /// Construct an undirected graph reader, which reads from the given
deba@165
  1366
    /// input stream.
deba@598
  1367
    GraphReader(GR& graph, std::istream& is = std::cin)
deba@165
  1368
      : _is(&is), local_is(false), _graph(graph),
alpar@209
  1369
        _use_nodes(false), _use_edges(false),
alpar@209
  1370
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1371
deba@165
  1372
    /// \brief Constructor
deba@165
  1373
    ///
kpeter@192
  1374
    /// Construct an undirected graph reader, which reads from the given
deba@165
  1375
    /// file.
deba@598
  1376
    GraphReader(GR& graph, const std::string& fn)
deba@290
  1377
      : _is(new std::ifstream(fn.c_str())), local_is(true),
deba@290
  1378
        _filename(fn), _graph(graph),
kpeter@212
  1379
        _use_nodes(false), _use_edges(false),
deba@290
  1380
        _skip_nodes(false), _skip_edges(false) {
deba@295
  1381
      if (!(*_is)) {
deba@295
  1382
        delete _is;
deba@295
  1383
        throw IoError("Cannot open file", fn);
deba@295
  1384
      }
deba@290
  1385
    }
alpar@209
  1386
deba@165
  1387
    /// \brief Constructor
deba@165
  1388
    ///
kpeter@192
  1389
    /// Construct an undirected graph reader, which reads from the given
deba@165
  1390
    /// file.
deba@598
  1391
    GraphReader(GR& graph, const char* fn)
deba@290
  1392
      : _is(new std::ifstream(fn)), local_is(true),
deba@290
  1393
        _filename(fn), _graph(graph),
kpeter@212
  1394
        _use_nodes(false), _use_edges(false),
deba@290
  1395
        _skip_nodes(false), _skip_edges(false) {
deba@295
  1396
      if (!(*_is)) {
deba@295
  1397
        delete _is;
deba@295
  1398
        throw IoError("Cannot open file", fn);
deba@295
  1399
      }
deba@290
  1400
    }
deba@165
  1401
deba@165
  1402
    /// \brief Destructor
deba@165
  1403
    ~GraphReader() {
alpar@209
  1404
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1405
           it != _node_maps.end(); ++it) {
alpar@209
  1406
        delete it->second;
deba@165
  1407
      }
deba@165
  1408
alpar@209
  1409
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1410
           it != _edge_maps.end(); ++it) {
alpar@209
  1411
        delete it->second;
deba@165
  1412
      }
deba@165
  1413
alpar@209
  1414
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1415
           it != _attributes.end(); ++it) {
alpar@209
  1416
        delete it->second;
deba@165
  1417
      }
deba@165
  1418
deba@165
  1419
      if (local_is) {
alpar@209
  1420
        delete _is;
deba@165
  1421
      }
deba@165
  1422
deba@165
  1423
    }
deba@165
  1424
deba@165
  1425
  private:
deba@598
  1426
    template <typename TGR>
deba@598
  1427
    friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is);
deba@598
  1428
    template <typename TGR>
alpar@877
  1429
    friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
deba@598
  1430
    template <typename TGR>
deba@598
  1431
    friend GraphReader<TGR> graphReader(TGR& graph, const char *fn);
alpar@209
  1432
alpar@209
  1433
    GraphReader(GraphReader& other)
deba@190
  1434
      : _is(other._is), local_is(other.local_is), _graph(other._graph),
alpar@209
  1435
        _use_nodes(other._use_nodes), _use_edges(other._use_edges),
alpar@209
  1436
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
deba@190
  1437
deba@190
  1438
      other._is = 0;
deba@190
  1439
      other.local_is = false;
alpar@209
  1440
deba@190
  1441
      _node_index.swap(other._node_index);
deba@190
  1442
      _edge_index.swap(other._edge_index);
deba@190
  1443
deba@190
  1444
      _node_maps.swap(other._node_maps);
deba@190
  1445
      _edge_maps.swap(other._edge_maps);
deba@190
  1446
      _attributes.swap(other._attributes);
deba@190
  1447
deba@190
  1448
      _nodes_caption = other._nodes_caption;
deba@190
  1449
      _edges_caption = other._edges_caption;
deba@190
  1450
      _attributes_caption = other._attributes_caption;
deba@190
  1451
deba@190
  1452
    }
deba@190
  1453
deba@165
  1454
    GraphReader& operator=(const GraphReader&);
deba@165
  1455
deba@165
  1456
  public:
deba@165
  1457
kpeter@584
  1458
    /// \name Reading Rules
deba@165
  1459
    /// @{
alpar@209
  1460
deba@165
  1461
    /// \brief Node map reading rule
deba@165
  1462
    ///
deba@165
  1463
    /// Add a node map reading rule to the reader.
deba@165
  1464
    template <typename Map>
deba@165
  1465
    GraphReader& nodeMap(const std::string& caption, Map& map) {
deba@165
  1466
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
alpar@209
  1467
      _reader_bits::MapStorageBase<Node>* storage =
alpar@209
  1468
        new _reader_bits::MapStorage<Node, Map>(map);
deba@165
  1469
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1470
      return *this;
deba@165
  1471
    }
deba@165
  1472
deba@165
  1473
    /// \brief Node map reading rule
deba@165
  1474
    ///
deba@165
  1475
    /// Add a node map reading rule with specialized converter to the
deba@165
  1476
    /// reader.
deba@165
  1477
    template <typename Map, typename Converter>
alpar@209
  1478
    GraphReader& nodeMap(const std::string& caption, Map& map,
alpar@209
  1479
                           const Converter& converter = Converter()) {
deba@165
  1480
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
alpar@209
  1481
      _reader_bits::MapStorageBase<Node>* storage =
alpar@209
  1482
        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@165
  1483
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1484
      return *this;
deba@165
  1485
    }
deba@165
  1486
deba@165
  1487
    /// \brief Edge map reading rule
deba@165
  1488
    ///
deba@165
  1489
    /// Add an edge map reading rule to the reader.
deba@165
  1490
    template <typename Map>
deba@165
  1491
    GraphReader& edgeMap(const std::string& caption, Map& map) {
deba@165
  1492
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
alpar@209
  1493
      _reader_bits::MapStorageBase<Edge>* storage =
alpar@209
  1494
        new _reader_bits::MapStorage<Edge, Map>(map);
deba@165
  1495
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1496
      return *this;
deba@165
  1497
    }
deba@165
  1498
deba@165
  1499
    /// \brief Edge map reading rule
deba@165
  1500
    ///
deba@165
  1501
    /// Add an edge map reading rule with specialized converter to the
deba@165
  1502
    /// reader.
deba@165
  1503
    template <typename Map, typename Converter>
alpar@209
  1504
    GraphReader& edgeMap(const std::string& caption, Map& map,
alpar@209
  1505
                          const Converter& converter = Converter()) {
deba@165
  1506
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
alpar@209
  1507
      _reader_bits::MapStorageBase<Edge>* storage =
alpar@209
  1508
        new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@165
  1509
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1510
      return *this;
deba@165
  1511
    }
deba@165
  1512
deba@165
  1513
    /// \brief Arc map reading rule
deba@165
  1514
    ///
deba@165
  1515
    /// Add an arc map reading rule to the reader.
deba@165
  1516
    template <typename Map>
deba@165
  1517
    GraphReader& arcMap(const std::string& caption, Map& map) {
deba@165
  1518
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
alpar@209
  1519
      _reader_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1520
        new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@165
  1521
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1522
      _reader_bits::MapStorageBase<Edge>* backward_storage =
deba@598
  1523
        new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map);
deba@165
  1524
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1525
      return *this;
deba@165
  1526
    }
deba@165
  1527
deba@165
  1528
    /// \brief Arc map reading rule
deba@165
  1529
    ///
deba@165
  1530
    /// Add an arc map reading rule with specialized converter to the
deba@165
  1531
    /// reader.
deba@165
  1532
    template <typename Map, typename Converter>
alpar@209
  1533
    GraphReader& arcMap(const std::string& caption, Map& map,
alpar@209
  1534
                          const Converter& converter = Converter()) {
deba@165
  1535
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
alpar@209
  1536
      _reader_bits::MapStorageBase<Edge>* forward_storage =
deba@598
  1537
        new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter>
alpar@209
  1538
        (_graph, map, converter);
deba@165
  1539
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1540
      _reader_bits::MapStorageBase<Edge>* backward_storage =
deba@598
  1541
        new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter>
alpar@209
  1542
        (_graph, map, converter);
deba@165
  1543
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1544
      return *this;
deba@165
  1545
    }
deba@165
  1546
deba@165
  1547
    /// \brief Attribute reading rule
deba@165
  1548
    ///
deba@165
  1549
    /// Add an attribute reading rule to the reader.
deba@165
  1550
    template <typename Value>
deba@165
  1551
    GraphReader& attribute(const std::string& caption, Value& value) {
alpar@209
  1552
      _reader_bits::ValueStorageBase* storage =
alpar@209
  1553
        new _reader_bits::ValueStorage<Value>(value);
deba@165
  1554
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1555
      return *this;
deba@165
  1556
    }
deba@165
  1557
deba@165
  1558
    /// \brief Attribute reading rule
deba@165
  1559
    ///
deba@165
  1560
    /// Add an attribute reading rule with specialized converter to the
deba@165
  1561
    /// reader.
deba@165
  1562
    template <typename Value, typename Converter>
alpar@209
  1563
    GraphReader& attribute(const std::string& caption, Value& value,
alpar@209
  1564
                             const Converter& converter = Converter()) {
alpar@209
  1565
      _reader_bits::ValueStorageBase* storage =
alpar@209
  1566
        new _reader_bits::ValueStorage<Value, Converter>(value, converter);
deba@165
  1567
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1568
      return *this;
deba@165
  1569
    }
deba@165
  1570
deba@165
  1571
    /// \brief Node reading rule
deba@165
  1572
    ///
deba@165
  1573
    /// Add a node reading rule to reader.
deba@165
  1574
    GraphReader& node(const std::string& caption, Node& node) {
deba@165
  1575
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
deba@165
  1576
      Converter converter(_node_index);
alpar@209
  1577
      _reader_bits::ValueStorageBase* storage =
alpar@209
  1578
        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
deba@165
  1579
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1580
      return *this;
deba@165
  1581
    }
deba@165
  1582
deba@165
  1583
    /// \brief Edge reading rule
deba@165
  1584
    ///
deba@165
  1585
    /// Add an edge reading rule to reader.
deba@165
  1586
    GraphReader& edge(const std::string& caption, Edge& edge) {
deba@165
  1587
      typedef _reader_bits::MapLookUpConverter<Edge> Converter;
deba@165
  1588
      Converter converter(_edge_index);
alpar@209
  1589
      _reader_bits::ValueStorageBase* storage =
alpar@209
  1590
        new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@165
  1591
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1592
      return *this;
deba@165
  1593
    }
deba@165
  1594
deba@165
  1595
    /// \brief Arc reading rule
deba@165
  1596
    ///
deba@165
  1597
    /// Add an arc reading rule to reader.
deba@165
  1598
    GraphReader& arc(const std::string& caption, Arc& arc) {
deba@598
  1599
      typedef _reader_bits::GraphArcLookUpConverter<GR> Converter;
deba@165
  1600
      Converter converter(_graph, _edge_index);
alpar@209
  1601
      _reader_bits::ValueStorageBase* storage =
alpar@209
  1602
        new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@165
  1603
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1604
      return *this;
deba@165
  1605
    }
deba@165
  1606
deba@165
  1607
    /// @}
deba@165
  1608
kpeter@584
  1609
    /// \name Select Section by Name
deba@165
  1610
    /// @{
deba@165
  1611
deba@165
  1612
    /// \brief Set \c \@nodes section to be read
deba@165
  1613
    ///
kpeter@192
  1614
    /// Set \c \@nodes section to be read.
deba@165
  1615
    GraphReader& nodes(const std::string& caption) {
deba@165
  1616
      _nodes_caption = caption;
deba@165
  1617
      return *this;
deba@165
  1618
    }
deba@165
  1619
deba@165
  1620
    /// \brief Set \c \@edges section to be read
deba@165
  1621
    ///
kpeter@192
  1622
    /// Set \c \@edges section to be read.
deba@165
  1623
    GraphReader& edges(const std::string& caption) {
deba@165
  1624
      _edges_caption = caption;
deba@165
  1625
      return *this;
deba@165
  1626
    }
deba@165
  1627
deba@165
  1628
    /// \brief Set \c \@attributes section to be read
deba@165
  1629
    ///
kpeter@192
  1630
    /// Set \c \@attributes section to be read.
deba@165
  1631
    GraphReader& attributes(const std::string& caption) {
deba@165
  1632
      _attributes_caption = caption;
deba@165
  1633
      return *this;
deba@165
  1634
    }
deba@165
  1635
deba@165
  1636
    /// @}
deba@165
  1637
kpeter@584
  1638
    /// \name Using Previously Constructed Node or Edge Set
deba@165
  1639
    /// @{
deba@165
  1640
deba@165
  1641
    /// \brief Use previously constructed node set
deba@165
  1642
    ///
deba@165
  1643
    /// Use previously constructed node set, and specify the node
deba@165
  1644
    /// label map.
deba@165
  1645
    template <typename Map>
deba@165
  1646
    GraphReader& useNodes(const Map& map) {
deba@165
  1647
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1648
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@165
  1649
      _use_nodes = true;
deba@165
  1650
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@165
  1651
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1652
        _node_index.insert(std::make_pair(converter(map[n]), n));
deba@165
  1653
      }
deba@165
  1654
      return *this;
deba@165
  1655
    }
deba@165
  1656
deba@165
  1657
    /// \brief Use previously constructed node set
deba@165
  1658
    ///
deba@165
  1659
    /// Use previously constructed node set, and specify the node
deba@165
  1660
    /// label map and a functor which converts the label map values to
kpeter@192
  1661
    /// \c std::string.
deba@165
  1662
    template <typename Map, typename Converter>
alpar@209
  1663
    GraphReader& useNodes(const Map& map,
alpar@209
  1664
                            const Converter& converter = Converter()) {
deba@165
  1665
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1666
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@165
  1667
      _use_nodes = true;
deba@165
  1668
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1669
        _node_index.insert(std::make_pair(converter(map[n]), n));
deba@165
  1670
      }
deba@165
  1671
      return *this;
deba@165
  1672
    }
deba@165
  1673
deba@165
  1674
    /// \brief Use previously constructed edge set
deba@165
  1675
    ///
deba@165
  1676
    /// Use previously constructed edge set, and specify the edge
deba@165
  1677
    /// label map.
deba@165
  1678
    template <typename Map>
deba@165
  1679
    GraphReader& useEdges(const Map& map) {
deba@165
  1680
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@165
  1681
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
deba@165
  1682
      _use_edges = true;
deba@165
  1683
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@165
  1684
      for (EdgeIt a(_graph); a != INVALID; ++a) {
alpar@209
  1685
        _edge_index.insert(std::make_pair(converter(map[a]), a));
deba@165
  1686
      }
deba@165
  1687
      return *this;
deba@165
  1688
    }
deba@165
  1689
deba@165
  1690
    /// \brief Use previously constructed edge set
deba@165
  1691
    ///
deba@165
  1692
    /// Use previously constructed edge set, and specify the edge
deba@165
  1693
    /// label map and a functor which converts the label map values to
kpeter@192
  1694
    /// \c std::string.
deba@165
  1695
    template <typename Map, typename Converter>
alpar@209
  1696
    GraphReader& useEdges(const Map& map,
alpar@209
  1697
                            const Converter& converter = Converter()) {
deba@165
  1698
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1699
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
deba@165
  1700
      _use_edges = true;
deba@165
  1701
      for (EdgeIt a(_graph); a != INVALID; ++a) {
alpar@209
  1702
        _edge_index.insert(std::make_pair(converter(map[a]), a));
deba@165
  1703
      }
deba@165
  1704
      return *this;
deba@165
  1705
    }
deba@165
  1706
kpeter@192
  1707
    /// \brief Skip the reading of node section
deba@188
  1708
    ///
deba@188
  1709
    /// Omit the reading of the node section. This implies that each node
kpeter@192
  1710
    /// map reading rule will be abandoned, and the nodes of the graph
deba@188
  1711
    /// will not be constructed, which usually cause that the edge set
deba@188
  1712
    /// could not be read due to lack of node name
kpeter@192
  1713
    /// could not be read due to lack of node name resolving.
kpeter@192
  1714
    /// Therefore \c skipEdges() function should also be used, or
kpeter@192
  1715
    /// \c useNodes() should be used to specify the label of the nodes.
deba@188
  1716
    GraphReader& skipNodes() {
alpar@209
  1717
      LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
deba@188
  1718
      _skip_nodes = true;
deba@188
  1719
      return *this;
deba@188
  1720
    }
deba@188
  1721
kpeter@192
  1722
    /// \brief Skip the reading of edge section
deba@188
  1723
    ///
deba@188
  1724
    /// Omit the reading of the edge section. This implies that each edge
kpeter@192
  1725
    /// map reading rule will be abandoned, and the edges of the graph
deba@188
  1726
    /// will not be constructed.
deba@188
  1727
    GraphReader& skipEdges() {
alpar@209
  1728
      LEMON_ASSERT(!_skip_edges, "Skip edges already set");
deba@188
  1729
      _skip_edges = true;
deba@188
  1730
      return *this;
deba@188
  1731
    }
deba@188
  1732
deba@165
  1733
    /// @}
deba@165
  1734
deba@165
  1735
  private:
deba@165
  1736
deba@165
  1737
    bool readLine() {
deba@165
  1738
      std::string str;
deba@165
  1739
      while(++line_num, std::getline(*_is, str)) {
alpar@209
  1740
        line.clear(); line.str(str);
alpar@209
  1741
        char c;
alpar@209
  1742
        if (line >> std::ws >> c && c != '#') {
alpar@209
  1743
          line.putback(c);
alpar@209
  1744
          return true;
alpar@209
  1745
        }
deba@165
  1746
      }
deba@165
  1747
      return false;
deba@165
  1748
    }
deba@165
  1749
deba@165
  1750
    bool readSuccess() {
deba@165
  1751
      return static_cast<bool>(*_is);
deba@165
  1752
    }
alpar@209
  1753
deba@165
  1754
    void skipSection() {
deba@165
  1755
      char c;
deba@165
  1756
      while (readSuccess() && line >> c && c != '@') {
alpar@209
  1757
        readLine();
deba@165
  1758
      }
deba@427
  1759
      if (readSuccess()) {
deba@427
  1760
        line.putback(c);
deba@427
  1761
      }
deba@165
  1762
    }
deba@165
  1763
deba@165
  1764
    void readNodes() {
deba@165
  1765
deba@165
  1766
      std::vector<int> map_index(_node_maps.size());
deba@165
  1767
      int map_num, label_index;
deba@165
  1768
deba@186
  1769
      char c;
deba@186
  1770
      if (!readLine() || !(line >> c) || c == '@') {
alpar@209
  1771
        if (readSuccess() && line) line.putback(c);
alpar@209
  1772
        if (!_node_maps.empty())
deba@290
  1773
          throw FormatError("Cannot find map names");
alpar@209
  1774
        return;
deba@186
  1775
      }
deba@186
  1776
      line.putback(c);
alpar@209
  1777
deba@165
  1778
      {
alpar@209
  1779
        std::map<std::string, int> maps;
alpar@209
  1780
alpar@209
  1781
        std::string map;
alpar@209
  1782
        int index = 0;
alpar@209
  1783
        while (_reader_bits::readToken(line, map)) {
alpar@209
  1784
          if (maps.find(map) != maps.end()) {
alpar@209
  1785
            std::ostringstream msg;
alpar@209
  1786
            msg << "Multiple occurence of node map: " << map;
deba@290
  1787
            throw FormatError(msg.str());
alpar@209
  1788
          }
alpar@209
  1789
          maps.insert(std::make_pair(map, index));
alpar@209
  1790
          ++index;
alpar@209
  1791
        }
alpar@209
  1792
alpar@209
  1793
        for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
alpar@209
  1794
          std::map<std::string, int>::iterator jt =
alpar@209
  1795
            maps.find(_node_maps[i].first);
alpar@209
  1796
          if (jt == maps.end()) {
alpar@209
  1797
            std::ostringstream msg;
kpeter@291
  1798
            msg << "Map not found: " << _node_maps[i].first;
deba@290
  1799
            throw FormatError(msg.str());
alpar@209
  1800
          }
alpar@209
  1801
          map_index[i] = jt->second;
alpar@209
  1802
        }
alpar@209
  1803
alpar@209
  1804
        {
alpar@209
  1805
          std::map<std::string, int>::iterator jt = maps.find("label");
alpar@209
  1806
          if (jt != maps.end()) {
alpar@209
  1807
            label_index = jt->second;
alpar@209
  1808
          } else {
alpar@209
  1809
            label_index = -1;
alpar@209
  1810
          }
alpar@209
  1811
        }
alpar@209
  1812
        map_num = maps.size();
deba@165
  1813
      }
deba@165
  1814
deba@165
  1815
      while (readLine() && line >> c && c != '@') {
alpar@209
  1816
        line.putback(c);
alpar@209
  1817
alpar@209
  1818
        std::vector<std::string> tokens(map_num);
alpar@209
  1819
        for (int i = 0; i < map_num; ++i) {
alpar@209
  1820
          if (!_reader_bits::readToken(line, tokens[i])) {
alpar@209
  1821
            std::ostringstream msg;
alpar@209
  1822
            msg << "Column not found (" << i + 1 << ")";
deba@290
  1823
            throw FormatError(msg.str());
alpar@209
  1824
          }
alpar@209
  1825
        }
alpar@209
  1826
        if (line >> std::ws >> c)
kpeter@291
  1827
          throw FormatError("Extra character at the end of line");
alpar@209
  1828
alpar@209
  1829
        Node n;
alpar@209
  1830
        if (!_use_nodes) {
alpar@209
  1831
          n = _graph.addNode();
alpar@209
  1832
          if (label_index != -1)
alpar@209
  1833
            _node_index.insert(std::make_pair(tokens[label_index], n));
alpar@209
  1834
        } else {
alpar@209
  1835
          if (label_index == -1)
kpeter@291
  1836
            throw FormatError("Label map not found");
alpar@209
  1837
          typename std::map<std::string, Node>::iterator it =
alpar@209
  1838
            _node_index.find(tokens[label_index]);
alpar@209
  1839
          if (it == _node_index.end()) {
alpar@209
  1840
            std::ostringstream msg;
alpar@209
  1841
            msg << "Node with label not found: " << tokens[label_index];
deba@290
  1842
            throw FormatError(msg.str());
alpar@209
  1843
          }
alpar@209
  1844
          n = it->second;
alpar@209
  1845
        }
alpar@209
  1846
alpar@209
  1847
        for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
alpar@209
  1848
          _node_maps[i].second->set(n, tokens[map_index[i]]);
alpar@209
  1849
        }
deba@165
  1850
deba@165
  1851
      }
deba@165
  1852
      if (readSuccess()) {
alpar@209
  1853
        line.putback(c);
deba@165
  1854
      }
deba@165
  1855
    }
deba@165
  1856
deba@165
  1857
    void readEdges() {
deba@165
  1858
deba@165
  1859
      std::vector<int> map_index(_edge_maps.size());
deba@165
  1860
      int map_num, label_index;
deba@165
  1861
deba@186
  1862
      char c;
deba@186
  1863
      if (!readLine() || !(line >> c) || c == '@') {
alpar@209
  1864
        if (readSuccess() && line) line.putback(c);
alpar@209
  1865
        if (!_edge_maps.empty())
deba@290
  1866
          throw FormatError("Cannot find map names");
alpar@209
  1867
        return;
deba@186
  1868
      }
deba@186
  1869
      line.putback(c);
alpar@209
  1870
deba@165
  1871
      {
alpar@209
  1872
        std::map<std::string, int> maps;
alpar@209
  1873
alpar@209
  1874
        std::string map;
alpar@209
  1875
        int index = 0;
alpar@209
  1876
        while (_reader_bits::readToken(line, map)) {
alpar@949
  1877
          if(map == "-") {
alpar@949
  1878
              if(index!=0)
alpar@949
  1879
                throw FormatError("'-' is not allowed as a map name");
alpar@949
  1880
              else if (line >> std::ws >> c)
alpar@949
  1881
                throw FormatError("Extra character at the end of line");
alpar@949
  1882
              else break;
alpar@949
  1883
            }
alpar@209
  1884
          if (maps.find(map) != maps.end()) {
alpar@209
  1885
            std::ostringstream msg;
alpar@209
  1886
            msg << "Multiple occurence of edge map: " << map;
deba@290
  1887
            throw FormatError(msg.str());
alpar@209
  1888
          }
alpar@209
  1889
          maps.insert(std::make_pair(map, index));
alpar@209
  1890
          ++index;
alpar@209
  1891
        }
alpar@209
  1892
alpar@209
  1893
        for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
alpar@209
  1894
          std::map<std::string, int>::iterator jt =
alpar@209
  1895
            maps.find(_edge_maps[i].first);
alpar@209
  1896
          if (jt == maps.end()) {
alpar@209
  1897
            std::ostringstream msg;
kpeter@291
  1898
            msg << "Map not found: " << _edge_maps[i].first;
deba@290
  1899
            throw FormatError(msg.str());
alpar@209
  1900
          }
alpar@209
  1901
          map_index[i] = jt->second;
alpar@209
  1902
        }
alpar@209
  1903
alpar@209
  1904
        {
alpar@209
  1905
          std::map<std::string, int>::iterator jt = maps.find("label");
alpar@209
  1906
          if (jt != maps.end()) {
alpar@209
  1907
            label_index = jt->second;
alpar@209
  1908
          } else {
alpar@209
  1909
            label_index = -1;
alpar@209
  1910
          }
alpar@209
  1911
        }
alpar@209
  1912
        map_num = maps.size();
deba@165
  1913
      }
deba@165
  1914
deba@165
  1915
      while (readLine() && line >> c && c != '@') {
alpar@209
  1916
        line.putback(c);
alpar@209
  1917
alpar@209
  1918
        std::string source_token;
alpar@209
  1919
        std::string target_token;
alpar@209
  1920
alpar@209
  1921
        if (!_reader_bits::readToken(line, source_token))
deba@290
  1922
          throw FormatError("Node u not found");
alpar@209
  1923
alpar@209
  1924
        if (!_reader_bits::readToken(line, target_token))
deba@290
  1925
          throw FormatError("Node v not found");
alpar@209
  1926
alpar@209
  1927
        std::vector<std::string> tokens(map_num);
alpar@209
  1928
        for (int i = 0; i < map_num; ++i) {
alpar@209
  1929
          if (!_reader_bits::readToken(line, tokens[i])) {
alpar@209
  1930
            std::ostringstream msg;
alpar@209
  1931
            msg << "Column not found (" << i + 1 << ")";
deba@290
  1932
            throw FormatError(msg.str());
alpar@209
  1933
          }
alpar@209
  1934
        }
alpar@209
  1935
        if (line >> std::ws >> c)
kpeter@291
  1936
          throw FormatError("Extra character at the end of line");
alpar@209
  1937
alpar@209
  1938
        Edge e;
alpar@209
  1939
        if (!_use_edges) {
deba@165
  1940
deba@165
  1941
          typename NodeIndex::iterator it;
alpar@209
  1942
deba@165
  1943
          it = _node_index.find(source_token);
deba@165
  1944
          if (it == _node_index.end()) {
deba@165
  1945
            std::ostringstream msg;
deba@165
  1946
            msg << "Item not found: " << source_token;
deba@290
  1947
            throw FormatError(msg.str());
deba@165
  1948
          }
deba@165
  1949
          Node source = it->second;
deba@165
  1950
deba@165
  1951
          it = _node_index.find(target_token);
alpar@209
  1952
          if (it == _node_index.end()) {
alpar@209
  1953
            std::ostringstream msg;
deba@165
  1954
            msg << "Item not found: " << target_token;
deba@290
  1955
            throw FormatError(msg.str());
alpar@209
  1956
          }
alpar@209
  1957
          Node target = it->second;
alpar@209
  1958
alpar@209
  1959
          e = _graph.addEdge(source, target);
alpar@209
  1960
          if (label_index != -1)
alpar@209
  1961
            _edge_index.insert(std::make_pair(tokens[label_index], e));
alpar@209
  1962
        } else {
alpar@209
  1963
          if (label_index == -1)
kpeter@291
  1964
            throw FormatError("Label map not found");
alpar@209
  1965
          typename std::map<std::string, Edge>::iterator it =
alpar@209
  1966
            _edge_index.find(tokens[label_index]);
alpar@209
  1967
          if (it == _edge_index.end()) {
alpar@209
  1968
            std::ostringstream msg;
alpar@209
  1969
            msg << "Edge with label not found: " << tokens[label_index];
deba@290
  1970
            throw FormatError(msg.str());
alpar@209
  1971
          }
alpar@209
  1972
          e = it->second;
alpar@209
  1973
        }
alpar@209
  1974
alpar@209
  1975
        for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
alpar@209
  1976
          _edge_maps[i].second->set(e, tokens[map_index[i]]);
alpar@209
  1977
        }
deba@165
  1978
deba@165
  1979
      }
deba@165
  1980
      if (readSuccess()) {
alpar@209
  1981
        line.putback(c);
deba@165
  1982
      }
deba@165
  1983
    }
deba@165
  1984
deba@165
  1985
    void readAttributes() {
deba@165
  1986
deba@165
  1987
      std::set<std::string> read_attr;
deba@165
  1988
deba@165
  1989
      char c;
deba@165
  1990
      while (readLine() && line >> c && c != '@') {
alpar@209
  1991
        line.putback(c);
alpar@209
  1992
alpar@209
  1993
        std::string attr, token;
alpar@209
  1994
        if (!_reader_bits::readToken(line, attr))
deba@290
  1995
          throw FormatError("Attribute name not found");
alpar@209
  1996
        if (!_reader_bits::readToken(line, token))
deba@290
  1997
          throw FormatError("Attribute value not found");
alpar@209
  1998
        if (line >> c)
kpeter@291
  1999
          throw FormatError("Extra character at the end of line");
alpar@209
  2000
alpar@209
  2001
        {
alpar@209
  2002
          std::set<std::string>::iterator it = read_attr.find(attr);
alpar@209
  2003
          if (it != read_attr.end()) {
alpar@209
  2004
            std::ostringstream msg;
kpeter@291
  2005
            msg << "Multiple occurence of attribute: " << attr;
deba@290
  2006
            throw FormatError(msg.str());
alpar@209
  2007
          }
alpar@209
  2008
          read_attr.insert(attr);
alpar@209
  2009
        }
alpar@209
  2010
alpar@209
  2011
        {
alpar@209
  2012
          typename Attributes::iterator it = _attributes.lower_bound(attr);
alpar@209
  2013
          while (it != _attributes.end() && it->first == attr) {
alpar@209
  2014
            it->second->set(token);
alpar@209
  2015
            ++it;
alpar@209
  2016
          }
alpar@209
  2017
        }
deba@165
  2018
deba@165
  2019
      }
deba@165
  2020
      if (readSuccess()) {
alpar@209
  2021
        line.putback(c);
deba@165
  2022
      }
deba@165
  2023
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  2024
           it != _attributes.end(); ++it) {
alpar@209
  2025
        if (read_attr.find(it->first) == read_attr.end()) {
alpar@209
  2026
          std::ostringstream msg;
kpeter@291
  2027
          msg << "Attribute not found: " << it->first;
deba@290
  2028
          throw FormatError(msg.str());
alpar@209
  2029
        }
deba@165
  2030
      }
deba@165
  2031
    }
deba@165
  2032
deba@165
  2033
  public:
deba@165
  2034
kpeter@584
  2035
    /// \name Execution of the Reader
deba@165
  2036
    /// @{
deba@165
  2037
deba@165
  2038
    /// \brief Start the batch processing
deba@165
  2039
    ///
deba@165
  2040
    /// This function starts the batch processing
deba@165
  2041
    void run() {
alpar@209
  2042
deba@165
  2043
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
alpar@209
  2044
deba@188
  2045
      bool nodes_done = _skip_nodes;
deba@188
  2046
      bool edges_done = _skip_edges;
deba@165
  2047
      bool attributes_done = false;
deba@165
  2048
alpar@209
  2049
      line_num = 0;
deba@165
  2050
      readLine();
deba@172
  2051
      skipSection();
deba@165
  2052
deba@165
  2053
      while (readSuccess()) {
alpar@209
  2054
        try {
alpar@209
  2055
          char c;
alpar@209
  2056
          std::string section, caption;
alpar@209
  2057
          line >> c;
alpar@209
  2058
          _reader_bits::readToken(line, section);
alpar@209
  2059
          _reader_bits::readToken(line, caption);
alpar@209
  2060
alpar@209
  2061
          if (line >> c)
kpeter@291
  2062
            throw FormatError("Extra character at the end of line");
alpar@209
  2063
alpar@209
  2064
          if (section == "nodes" && !nodes_done) {
alpar@209
  2065
            if (_nodes_caption.empty() || _nodes_caption == caption) {
alpar@209
  2066
              readNodes();
alpar@209
  2067
              nodes_done = true;
alpar@209
  2068
            }
alpar@209
  2069
          } else if ((section == "edges" || section == "arcs") &&
alpar@209
  2070
                     !edges_done) {
alpar@209
  2071
            if (_edges_caption.empty() || _edges_caption == caption) {
alpar@209
  2072
              readEdges();
alpar@209
  2073
              edges_done = true;
alpar@209
  2074
            }
alpar@209
  2075
          } else if (section == "attributes" && !attributes_done) {
alpar@209
  2076
            if (_attributes_caption.empty() || _attributes_caption == caption) {
alpar@209
  2077
              readAttributes();
alpar@209
  2078
              attributes_done = true;
alpar@209
  2079
            }
alpar@209
  2080
          } else {
alpar@209
  2081
            readLine();
alpar@209
  2082
            skipSection();
alpar@209
  2083
          }
deba@290
  2084
        } catch (FormatError& error) {
alpar@209
  2085
          error.line(line_num);
deba@290
  2086
          error.file(_filename);
alpar@209
  2087
          throw;
alpar@209
  2088
        }
deba@165
  2089
      }
deba@165
  2090
deba@165
  2091
      if (!nodes_done) {
deba@290
  2092
        throw FormatError("Section @nodes not found");
deba@165
  2093
      }
deba@165
  2094
deba@165
  2095
      if (!edges_done) {
deba@290
  2096
        throw FormatError("Section @edges not found");
deba@165
  2097
      }
deba@165
  2098
deba@165
  2099
      if (!attributes_done && !_attributes.empty()) {
deba@290
  2100
        throw FormatError("Section @attributes not found");
deba@165
  2101
      }
deba@165
  2102
deba@165
  2103
    }
deba@165
  2104
deba@165
  2105
    /// @}
alpar@209
  2106
deba@165
  2107
  };
deba@165
  2108
deba@598
  2109
  /// \ingroup lemon_io
deba@598
  2110
  ///
deba@598
  2111
  /// \brief Return a \ref GraphReader class
deba@598
  2112
  ///
alpar@877
  2113
  /// This function just returns a \ref GraphReader class.
deba@598
  2114
  ///
alpar@877
  2115
  /// With this function a graph can be read from an
deba@598
  2116
  /// \ref lgf-format "LGF" file or input stream with several maps and
deba@598
  2117
  /// attributes. For example, there is weighted matching problem on a
deba@598
  2118
  /// graph, i.e. a graph with a \e weight map on the edges. This
deba@598
  2119
  /// graph can be read with the following code:
deba@598
  2120
  ///
deba@598
  2121
  ///\code
deba@598
  2122
  ///ListGraph graph;
deba@598
  2123
  ///ListGraph::EdgeMap<int> weight(graph);
deba@598
  2124
  ///graphReader(graph, std::cin).
deba@598
  2125
  ///  edgeMap("weight", weight).
deba@598
  2126
  ///  run();
deba@598
  2127
  ///\endcode
deba@598
  2128
  ///
deba@598
  2129
  /// For a complete documentation, please see the \ref GraphReader
deba@598
  2130
  /// class documentation.
deba@598
  2131
  /// \warning Don't forget to put the \ref GraphReader::run() "run()"
deba@598
  2132
  /// to the end of the parameter list.
deba@598
  2133
  /// \relates GraphReader
deba@598
  2134
  /// \sa graphReader(TGR& graph, const std::string& fn)
deba@598
  2135
  /// \sa graphReader(TGR& graph, const char* fn)
deba@598
  2136
  template <typename TGR>
deba@598
  2137
  GraphReader<TGR> graphReader(TGR& graph, std::istream& is) {
deba@598
  2138
    GraphReader<TGR> tmp(graph, is);
deba@598
  2139
    return tmp;
deba@598
  2140
  }
deba@598
  2141
kpeter@487
  2142
  /// \brief Return a \ref GraphReader class
kpeter@487
  2143
  ///
kpeter@487
  2144
  /// This function just returns a \ref GraphReader class.
kpeter@487
  2145
  /// \relates GraphReader
deba@598
  2146
  /// \sa graphReader(TGR& graph, std::istream& is)
deba@598
  2147
  template <typename TGR>
deba@598
  2148
  GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) {
deba@598
  2149
    GraphReader<TGR> tmp(graph, fn);
kpeter@487
  2150
    return tmp;
kpeter@487
  2151
  }
kpeter@487
  2152
kpeter@487
  2153
  /// \brief Return a \ref GraphReader class
kpeter@487
  2154
  ///
kpeter@487
  2155
  /// This function just returns a \ref GraphReader class.
kpeter@487
  2156
  /// \relates GraphReader
deba@598
  2157
  /// \sa graphReader(TGR& graph, std::istream& is)
deba@598
  2158
  template <typename TGR>
deba@598
  2159
  GraphReader<TGR> graphReader(TGR& graph, const char* fn) {
deba@598
  2160
    GraphReader<TGR> tmp(graph, fn);
kpeter@487
  2161
    return tmp;
kpeter@487
  2162
  }
kpeter@487
  2163
deba@1024
  2164
  template <typename BGR>
deba@1024
  2165
  class BpGraphReader;
deba@1024
  2166
deba@1024
  2167
  template <typename TBGR>
deba@1024
  2168
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is = std::cin);
deba@1024
  2169
  template <typename TBGR>
deba@1024
  2170
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn);
deba@1024
  2171
  template <typename TBGR>
deba@1024
  2172
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn);
deba@1024
  2173
deba@1024
  2174
  /// \ingroup lemon_io
deba@1024
  2175
  ///
deba@1024
  2176
  /// \brief \ref lgf-format "LGF" reader for bipartite graphs
deba@1024
  2177
  ///
deba@1024
  2178
  /// This utility reads an \ref lgf-format "LGF" file.
deba@1024
  2179
  ///
deba@1024
  2180
  /// It can be used almost the same way as \c GraphReader, but it
deba@1024
  2181
  /// reads the red and blue nodes from separate sections, and these
deba@1024
  2182
  /// sections can contain different set of maps.
deba@1024
  2183
  ///
deba@1026
  2184
  /// The red and blue node maps are read from the corresponding
deba@1024
  2185
  /// sections. If a map is defined with the same name in both of
deba@1024
  2186
  /// these sections, then it can be read as a node map.
deba@1024
  2187
  template <typename BGR>
deba@1024
  2188
  class BpGraphReader {
deba@1024
  2189
  public:
deba@1024
  2190
deba@1024
  2191
    typedef BGR Graph;
deba@1024
  2192
deba@1024
  2193
  private:
deba@1024
  2194
deba@1024
  2195
    TEMPLATE_BPGRAPH_TYPEDEFS(BGR);
deba@1024
  2196
deba@1024
  2197
    std::istream* _is;
deba@1024
  2198
    bool local_is;
deba@1024
  2199
    std::string _filename;
deba@1024
  2200
deba@1024
  2201
    BGR& _graph;
deba@1024
  2202
deba@1024
  2203
    std::string _nodes_caption;
deba@1024
  2204
    std::string _edges_caption;
deba@1024
  2205
    std::string _attributes_caption;
deba@1024
  2206
deba@1030
  2207
    typedef std::map<std::string, RedNode> RedNodeIndex;
deba@1030
  2208
    RedNodeIndex _red_node_index;
deba@1030
  2209
    typedef std::map<std::string, BlueNode> BlueNodeIndex;
deba@1030
  2210
    BlueNodeIndex _blue_node_index;
deba@1024
  2211
    typedef std::map<std::string, Edge> EdgeIndex;
deba@1024
  2212
    EdgeIndex _edge_index;
deba@1024
  2213
deba@1024
  2214
    typedef std::vector<std::pair<std::string,
deba@1030
  2215
      _reader_bits::MapStorageBase<RedNode>*> > RedNodeMaps;
deba@1030
  2216
    RedNodeMaps _red_node_maps;
deba@1030
  2217
    typedef std::vector<std::pair<std::string,
deba@1030
  2218
      _reader_bits::MapStorageBase<BlueNode>*> > BlueNodeMaps;
deba@1030
  2219
    BlueNodeMaps _blue_node_maps;
deba@1024
  2220
deba@1024
  2221
    typedef std::vector<std::pair<std::string,
deba@1024
  2222
      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
deba@1024
  2223
    EdgeMaps _edge_maps;
deba@1024
  2224
deba@1024
  2225
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
deba@1024
  2226
      Attributes;
deba@1024
  2227
    Attributes _attributes;
deba@1024
  2228
deba@1024
  2229
    bool _use_nodes;
deba@1024
  2230
    bool _use_edges;
deba@1024
  2231
deba@1024
  2232
    bool _skip_nodes;
deba@1024
  2233
    bool _skip_edges;
deba@1024
  2234
deba@1024
  2235
    int line_num;
deba@1024
  2236
    std::istringstream line;
deba@1024
  2237
deba@1024
  2238
  public:
deba@1024
  2239
deba@1024
  2240
    /// \brief Constructor
deba@1024
  2241
    ///
deba@1024
  2242
    /// Construct an undirected graph reader, which reads from the given
deba@1024
  2243
    /// input stream.
deba@1024
  2244
    BpGraphReader(BGR& graph, std::istream& is = std::cin)
deba@1024
  2245
      : _is(&is), local_is(false), _graph(graph),
deba@1024
  2246
        _use_nodes(false), _use_edges(false),
deba@1024
  2247
        _skip_nodes(false), _skip_edges(false) {}
deba@1024
  2248
deba@1024
  2249
    /// \brief Constructor
deba@1024
  2250
    ///
deba@1024
  2251
    /// Construct an undirected graph reader, which reads from the given
deba@1024
  2252
    /// file.
deba@1024
  2253
    BpGraphReader(BGR& graph, const std::string& fn)
deba@1024
  2254
      : _is(new std::ifstream(fn.c_str())), local_is(true),
deba@1024
  2255
        _filename(fn), _graph(graph),
deba@1024
  2256
        _use_nodes(false), _use_edges(false),
deba@1024
  2257
        _skip_nodes(false), _skip_edges(false) {
deba@1024
  2258
      if (!(*_is)) {
deba@1024
  2259
        delete _is;
deba@1024
  2260
        throw IoError("Cannot open file", fn);
deba@1024
  2261
      }
deba@1024
  2262
    }
deba@1024
  2263
deba@1024
  2264
    /// \brief Constructor
deba@1024
  2265
    ///
deba@1024
  2266
    /// Construct an undirected graph reader, which reads from the given
deba@1024
  2267
    /// file.
deba@1024
  2268
    BpGraphReader(BGR& graph, const char* fn)
deba@1024
  2269
      : _is(new std::ifstream(fn)), local_is(true),
deba@1024
  2270
        _filename(fn), _graph(graph),
deba@1024
  2271
        _use_nodes(false), _use_edges(false),
deba@1024
  2272
        _skip_nodes(false), _skip_edges(false) {
deba@1024
  2273
      if (!(*_is)) {
deba@1024
  2274
        delete _is;
deba@1024
  2275
        throw IoError("Cannot open file", fn);
deba@1024
  2276
      }
deba@1024
  2277
    }
deba@1024
  2278
deba@1024
  2279
    /// \brief Destructor
deba@1024
  2280
    ~BpGraphReader() {
deba@1030
  2281
      for (typename RedNodeMaps::iterator it = _red_node_maps.begin();
deba@1030
  2282
           it != _red_node_maps.end(); ++it) {
deba@1024
  2283
        delete it->second;
deba@1024
  2284
      }
deba@1024
  2285
deba@1030
  2286
      for (typename BlueNodeMaps::iterator it = _blue_node_maps.begin();
deba@1030
  2287
           it != _blue_node_maps.end(); ++it) {
deba@1024
  2288
        delete it->second;
deba@1024
  2289
      }
deba@1024
  2290
deba@1024
  2291
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
deba@1024
  2292
           it != _edge_maps.end(); ++it) {
deba@1024
  2293
        delete it->second;
deba@1024
  2294
      }
deba@1024
  2295
deba@1024
  2296
      for (typename Attributes::iterator it = _attributes.begin();
deba@1024
  2297
           it != _attributes.end(); ++it) {
deba@1024
  2298
        delete it->second;
deba@1024
  2299
      }
deba@1024
  2300
deba@1024
  2301
      if (local_is) {
deba@1024
  2302
        delete _is;
deba@1024
  2303
      }
deba@1024
  2304
deba@1024
  2305
    }
deba@1024
  2306
deba@1024
  2307
  private:
deba@1024
  2308
    template <typename TBGR>
deba@1024
  2309
    friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is);
deba@1024
  2310
    template <typename TBGR>
deba@1024
  2311
    friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph,
deba@1024
  2312
                                             const std::string& fn);
deba@1024
  2313
    template <typename TBGR>
deba@1024
  2314
    friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn);
deba@1024
  2315
deba@1024
  2316
    BpGraphReader(BpGraphReader& other)
deba@1024
  2317
      : _is(other._is), local_is(other.local_is), _graph(other._graph),
deba@1024
  2318
        _use_nodes(other._use_nodes), _use_edges(other._use_edges),
deba@1024
  2319
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
deba@1024
  2320
deba@1024
  2321
      other._is = 0;
deba@1024
  2322
      other.local_is = false;
deba@1024
  2323
deba@1030
  2324
      _red_node_index.swap(other._red_node_index);
deba@1030
  2325
      _blue_node_index.swap(other._blue_node_index);
deba@1024
  2326
      _edge_index.swap(other._edge_index);
deba@1024
  2327
deba@1030
  2328
      _red_node_maps.swap(other._red_node_maps);
deba@1030
  2329
      _blue_node_maps.swap(other._blue_node_maps);
deba@1024
  2330
      _edge_maps.swap(other._edge_maps);
deba@1024
  2331
      _attributes.swap(other._attributes);
deba@1024
  2332
deba@1024
  2333
      _nodes_caption = other._nodes_caption;
deba@1024
  2334
      _edges_caption = other._edges_caption;
deba@1024
  2335
      _attributes_caption = other._attributes_caption;
deba@1024
  2336
deba@1024
  2337
    }
deba@1024
  2338
deba@1024
  2339
    BpGraphReader& operator=(const BpGraphReader&);
deba@1024
  2340
deba@1024
  2341
  public:
deba@1024
  2342
deba@1024
  2343
    /// \name Reading Rules
deba@1024
  2344
    /// @{
deba@1024
  2345
deba@1024
  2346
    /// \brief Node map reading rule
deba@1024
  2347
    ///
deba@1024
  2348
    /// Add a node map reading rule to the reader.
deba@1024
  2349
    template <typename Map>
deba@1024
  2350
    BpGraphReader& nodeMap(const std::string& caption, Map& map) {
deba@1024
  2351
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@1030
  2352
      _reader_bits::MapStorageBase<RedNode>* red_storage =
deba@1030
  2353
        new _reader_bits::MapStorage<RedNode, Map>(map);
deba@1030
  2354
      _red_node_maps.push_back(std::make_pair(caption, red_storage));
deba@1030
  2355
      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
deba@1030
  2356
        new _reader_bits::MapStorage<BlueNode, Map>(map);
deba@1030
  2357
      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
deba@1024
  2358
      return *this;
deba@1024
  2359
    }
deba@1024
  2360
deba@1024
  2361
    /// \brief Node map reading rule
deba@1024
  2362
    ///
deba@1024
  2363
    /// Add a node map reading rule with specialized converter to the
deba@1024
  2364
    /// reader.
deba@1024
  2365
    template <typename Map, typename Converter>
deba@1024
  2366
    BpGraphReader& nodeMap(const std::string& caption, Map& map,
deba@1024
  2367
                           const Converter& converter = Converter()) {
deba@1024
  2368
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@1030
  2369
      _reader_bits::MapStorageBase<RedNode>* red_storage =
deba@1030
  2370
        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
deba@1030
  2371
      _red_node_maps.push_back(std::make_pair(caption, red_storage));
deba@1030
  2372
      _reader_bits::MapStorageBase<BlueNode>* blue_storage =
deba@1030
  2373
        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
deba@1030
  2374
      _blue_node_maps.push_back(std::make_pair(caption, blue_storage));
deba@1024
  2375
      return *this;
deba@1024
  2376
    }
deba@1024
  2377
deba@1026
  2378
    /// Add a red node map reading rule to the reader.
deba@1024
  2379
    template <typename Map>
deba@1026
  2380
    BpGraphReader& redNodeMap(const std::string& caption, Map& map) {
deba@1030
  2381
      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
deba@1030
  2382
      _reader_bits::MapStorageBase<RedNode>* storage =
deba@1030
  2383
        new _reader_bits::MapStorage<RedNode, Map>(map);
deba@1030
  2384
      _red_node_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2385
      return *this;
deba@1024
  2386
    }
deba@1024
  2387
deba@1026
  2388
    /// \brief Red node map reading rule
deba@1024
  2389
    ///
deba@1026
  2390
    /// Add a red node map node reading rule with specialized converter to
deba@1026
  2391
    /// the reader.
deba@1024
  2392
    template <typename Map, typename Converter>
deba@1026
  2393
    BpGraphReader& redNodeMap(const std::string& caption, Map& map,
deba@1026
  2394
                              const Converter& converter = Converter()) {
deba@1030
  2395
      checkConcept<concepts::WriteMap<RedNode, typename Map::Value>, Map>();
deba@1030
  2396
      _reader_bits::MapStorageBase<RedNode>* storage =
deba@1030
  2397
        new _reader_bits::MapStorage<RedNode, Map, Converter>(map, converter);
deba@1030
  2398
      _red_node_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2399
      return *this;
deba@1024
  2400
    }
deba@1024
  2401
deba@1026
  2402
    /// Add a blue node map reading rule to the reader.
deba@1024
  2403
    template <typename Map>
deba@1026
  2404
    BpGraphReader& blueNodeMap(const std::string& caption, Map& map) {
deba@1030
  2405
      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
deba@1030
  2406
      _reader_bits::MapStorageBase<BlueNode>* storage =
deba@1030
  2407
        new _reader_bits::MapStorage<BlueNode, Map>(map);
deba@1030
  2408
      _blue_node_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2409
      return *this;
deba@1024
  2410
    }
deba@1024
  2411
deba@1026
  2412
    /// \brief Blue node map reading rule
deba@1024
  2413
    ///
deba@1026
  2414
    /// Add a blue node map reading rule with specialized converter to
deba@1026
  2415
    /// the reader.
deba@1024
  2416
    template <typename Map, typename Converter>
deba@1026
  2417
    BpGraphReader& blueNodeMap(const std::string& caption, Map& map,
deba@1026
  2418
                               const Converter& converter = Converter()) {
deba@1030
  2419
      checkConcept<concepts::WriteMap<BlueNode, typename Map::Value>, Map>();
deba@1030
  2420
      _reader_bits::MapStorageBase<BlueNode>* storage =
deba@1030
  2421
        new _reader_bits::MapStorage<BlueNode, Map, Converter>(map, converter);
deba@1030
  2422
      _blue_node_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2423
      return *this;
deba@1024
  2424
    }
deba@1024
  2425
deba@1024
  2426
    /// \brief Edge map reading rule
deba@1024
  2427
    ///
deba@1024
  2428
    /// Add an edge map reading rule to the reader.
deba@1024
  2429
    template <typename Map>
deba@1024
  2430
    BpGraphReader& edgeMap(const std::string& caption, Map& map) {
deba@1024
  2431
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@1024
  2432
      _reader_bits::MapStorageBase<Edge>* storage =
deba@1024
  2433
        new _reader_bits::MapStorage<Edge, Map>(map);
deba@1024
  2434
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2435
      return *this;
deba@1024
  2436
    }
deba@1024
  2437
deba@1024
  2438
    /// \brief Edge map reading rule
deba@1024
  2439
    ///
deba@1024
  2440
    /// Add an edge map reading rule with specialized converter to the
deba@1024
  2441
    /// reader.
deba@1024
  2442
    template <typename Map, typename Converter>
deba@1024
  2443
    BpGraphReader& edgeMap(const std::string& caption, Map& map,
deba@1024
  2444
                          const Converter& converter = Converter()) {
deba@1024
  2445
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@1024
  2446
      _reader_bits::MapStorageBase<Edge>* storage =
deba@1024
  2447
        new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@1024
  2448
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@1024
  2449
      return *this;
deba@1024
  2450
    }
deba@1024
  2451
deba@1024
  2452
    /// \brief Arc map reading rule
deba@1024
  2453
    ///
deba@1024
  2454
    /// Add an arc map reading rule to the reader.
deba@1024
  2455
    template <typename Map>
deba@1024
  2456
    BpGraphReader& arcMap(const std::string& caption, Map& map) {
deba@1024
  2457
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@1024
  2458
      _reader_bits::MapStorageBase<Edge>* forward_storage =
deba@1024
  2459
        new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@1024
  2460
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
deba@1024
  2461
      _reader_bits::MapStorageBase<Edge>* backward_storage =
deba@1024
  2462
        new _reader_bits::GraphArcMapStorage<BGR, false, Map>(_graph, map);
deba@1024
  2463
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@1024
  2464
      return *this;
deba@1024
  2465
    }
deba@1024
  2466
deba@1024
  2467
    /// \brief Arc map reading rule
deba@1024
  2468
    ///
deba@1024
  2469
    /// Add an arc map reading rule with specialized converter to the
deba@1024
  2470
    /// reader.
deba@1024
  2471
    template <typename Map, typename Converter>
deba@1024
  2472
    BpGraphReader& arcMap(const std::string& caption, Map& map,
deba@1024
  2473
                          const Converter& converter = Converter()) {
deba@1024
  2474
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@1024
  2475
      _reader_bits::MapStorageBase<Edge>* forward_storage =
deba@1024
  2476
        new _reader_bits::GraphArcMapStorage<BGR, true, Map, Converter>
deba@1024
  2477
        (_graph, map, converter);
deba@1024
  2478
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
deba@1024
  2479
      _reader_bits::MapStorageBase<Edge>* backward_storage =
deba@1024
  2480
        new _reader_bits::GraphArcMapStorage<BGR, false, Map, Converter>
deba@1024
  2481
        (_graph, map, converter);
deba@1024
  2482
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@1024
  2483
      return *this;
deba@1024
  2484
    }
deba@1024
  2485
deba@1024
  2486
    /// \brief Attribute reading rule
deba@1024
  2487
    ///
deba@1024
  2488
    /// Add an attribute reading rule to the reader.
deba@1024
  2489
    template <typename Value>
deba@1024
  2490
    BpGraphReader& attribute(const std::string& caption, Value& value) {
deba@1024
  2491
      _reader_bits::ValueStorageBase* storage =
deba@1024
  2492
        new _reader_bits::ValueStorage<Value>(value);
deba@1024
  2493
      _attributes.insert(std::make_pair(caption, storage));
deba@1024
  2494
      return *this;
deba@1024
  2495
    }
deba@1024
  2496
deba@1024
  2497
    /// \brief Attribute reading rule
deba@1024
  2498
    ///
deba@1024
  2499
    /// Add an attribute reading rule with specialized converter to the
deba@1024
  2500
    /// reader.
deba@1024
  2501
    template <typename Value, typename Converter>
deba@1024
  2502
    BpGraphReader& attribute(const std::string& caption, Value& value,
deba@1024
  2503
                             const Converter& converter = Converter()) {
deba@1024
  2504
      _reader_bits::ValueStorageBase* storage =
deba@1024
  2505
        new _reader_bits::ValueStorage<Value, Converter>(value, converter);
deba@1024
  2506
      _attributes.insert(std::make_pair(caption, storage));
deba@1024
  2507
      return *this;
deba@1024
  2508
    }
deba@1024
  2509
deba@1024
  2510
    /// \brief Node reading rule
deba@1024
  2511
    ///
deba@1024
  2512
    /// Add a node reading rule to reader.
deba@1024
  2513
    BpGraphReader& node(const std::string& caption, Node& node) {
deba@1030
  2514
      typedef _reader_bits::DoubleMapLookUpConverter<
deba@1030
  2515
        Node, RedNodeIndex, BlueNodeIndex> Converter;
deba@1030
  2516
      Converter converter(_red_node_index, _blue_node_index);
deba@1024
  2517
      _reader_bits::ValueStorageBase* storage =
deba@1024
  2518
        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
deba@1024
  2519
      _attributes.insert(std::make_pair(caption, storage));
deba@1024
  2520
      return *this;
deba@1024
  2521
    }
deba@1024
  2522
deba@1030
  2523
    /// \brief Red node reading rule
deba@1030
  2524
    ///
deba@1030
  2525
    /// Add a red node reading rule to reader.
deba@1030
  2526
    BpGraphReader& redNode(const std::string& caption, RedNode& node) {
deba@1030
  2527
      typedef _reader_bits::MapLookUpConverter<RedNode> Converter;
deba@1030
  2528
      Converter converter(_red_node_index);
deba@1030
  2529
      _reader_bits::ValueStorageBase* storage =
deba@1030
  2530
        new _reader_bits::ValueStorage<RedNode, Converter>(node, converter);
deba@1030
  2531
      _attributes.insert(std::make_pair(caption, storage));
deba@1030
  2532
      return *this;
deba@1030
  2533
    }
deba@1030
  2534
deba@1030
  2535
    /// \brief Blue node reading rule
deba@1030
  2536
    ///
deba@1030
  2537
    /// Add a blue node reading rule to reader.
deba@1030
  2538
    BpGraphReader& blueNode(const std::string& caption, BlueNode& node) {
deba@1030
  2539
      typedef _reader_bits::MapLookUpConverter<BlueNode> Converter;
deba@1030
  2540
      Converter converter(_blue_node_index);
deba@1030
  2541
      _reader_bits::ValueStorageBase* storage =
deba@1030
  2542
        new _reader_bits::ValueStorage<BlueNode, Converter>(node, converter);
deba@1030
  2543
      _attributes.insert(std::make_pair(caption, storage));
deba@1030
  2544
      return *this;
deba@1030
  2545
    }
deba@1030
  2546
deba@1024
  2547
    /// \brief Edge reading rule
deba@1024
  2548
    ///
deba@1024
  2549
    /// Add an edge reading rule to reader.
deba@1024
  2550
    BpGraphReader& edge(const std::string& caption, Edge& edge) {
deba@1024
  2551
      typedef _reader_bits::MapLookUpConverter<Edge> Converter;
deba@1024
  2552
      Converter converter(_edge_index);
deba@1024
  2553
      _reader_bits::ValueStorageBase* storage =
deba@1024
  2554
        new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@1024
  2555
      _attributes.insert(std::make_pair(caption, storage));
deba@1024
  2556
      return *this;
deba@1024
  2557
    }
deba@1024
  2558
deba@1024
  2559
    /// \brief Arc reading rule
deba@1024
  2560
    ///
deba@1024
  2561
    /// Add an arc reading rule to reader.
deba@1024
  2562
    BpGraphReader& arc(const std::string& caption, Arc& arc) {
deba@1024
  2563
      typedef _reader_bits::GraphArcLookUpConverter<BGR> Converter;
deba@1024
  2564
      Converter converter(_graph, _edge_index);
deba@1024
  2565
      _reader_bits::ValueStorageBase* storage =
deba@1024
  2566
        new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@1024
  2567
      _attributes.insert(std::make_pair(caption, storage));
deba@1024
  2568
      return *this;
deba@1024
  2569
    }
deba@1024
  2570
deba@1024
  2571
    /// @}
deba@1024
  2572
deba@1024
  2573
    /// \name Select Section by Name
deba@1024
  2574
    /// @{
deba@1024
  2575
deba@1024
  2576
    /// \brief Set \c \@nodes section to be read
deba@1024
  2577
    ///
deba@1024
  2578
    /// Set \c \@nodes section to be read.
deba@1024
  2579
    BpGraphReader& nodes(const std::string& caption) {
deba@1024
  2580
      _nodes_caption = caption;
deba@1024
  2581
      return *this;
deba@1024
  2582
    }
deba@1024
  2583
deba@1024
  2584
    /// \brief Set \c \@edges section to be read
deba@1024
  2585
    ///
deba@1024
  2586
    /// Set \c \@edges section to be read.
deba@1024
  2587
    BpGraphReader& edges(const std::string& caption) {
deba@1024
  2588
      _edges_caption = caption;
deba@1024
  2589
      return *this;
deba@1024
  2590
    }
deba@1024
  2591
deba@1024
  2592
    /// \brief Set \c \@attributes section to be read
deba@1024
  2593
    ///
deba@1024
  2594
    /// Set \c \@attributes section to be read.
deba@1024
  2595
    BpGraphReader& attributes(const std::string& caption) {
deba@1024
  2596
      _attributes_caption = caption;
deba@1024
  2597
      return *this;
deba@1024
  2598
    }
deba@1024
  2599
deba@1024
  2600
    /// @}
deba@1024
  2601
deba@1024
  2602
    /// \name Using Previously Constructed Node or Edge Set
deba@1024
  2603
    /// @{
deba@1024
  2604
deba@1024
  2605
    /// \brief Use previously constructed node set
deba@1024
  2606
    ///
deba@1024
  2607
    /// Use previously constructed node set, and specify the node
deba@1024
  2608
    /// label map.
deba@1024
  2609
    template <typename Map>
deba@1024
  2610
    BpGraphReader& useNodes(const Map& map) {
deba@1024
  2611
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@1024
  2612
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@1024
  2613
      _use_nodes = true;
deba@1024
  2614
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@1030
  2615
      for (RedNodeIt n(_graph); n != INVALID; ++n) {
deba@1030
  2616
        _red_node_index.insert(std::make_pair(converter(map[n]), n));
deba@1030
  2617
      }
deba@1030
  2618
      for (BlueNodeIt n(_graph); n != INVALID; ++n) {
deba@1030
  2619
        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
deba@1024
  2620
      }
deba@1024
  2621
      return *this;
deba@1024
  2622
    }
deba@1024
  2623
deba@1024
  2624
    /// \brief Use previously constructed node set
deba@1024
  2625
    ///
deba@1024
  2626
    /// Use previously constructed node set, and specify the node
deba@1024
  2627
    /// label map and a functor which converts the label map values to
deba@1024
  2628
    /// \c std::string.
deba@1024
  2629
    template <typename Map, typename Converter>
deba@1024
  2630
    BpGraphReader& useNodes(const Map& map,
deba@1024
  2631
                            const Converter& converter = Converter()) {
deba@1024
  2632
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@1024
  2633
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
deba@1024
  2634
      _use_nodes = true;
deba@1030
  2635
      for (RedNodeIt n(_graph); n != INVALID; ++n) {
deba@1030
  2636
        _red_node_index.insert(std::make_pair(converter(map[n]), n));
deba@1030
  2637
      }
deba@1030
  2638
      for (BlueNodeIt n(_graph); n != INVALID; ++n) {      
deba@1030
  2639
        _blue_node_index.insert(std::make_pair(converter(map[n]), n));
deba@1024
  2640
      }
deba@1024
  2641
      return *this;
deba@1024
  2642
    }
deba@1024
  2643
deba@1024
  2644
    /// \brief Use previously constructed edge set
deba@1024
  2645
    ///
deba@1024
  2646
    /// Use previously constructed edge set, and specify the edge
deba@1024
  2647
    /// label map.
deba@1024
  2648
    template <typename Map>
deba@1024
  2649
    BpGraphReader& useEdges(const Map& map) {
deba@1024
  2650
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@1024
  2651
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
deba@1024
  2652
      _use_edges = true;
deba@1024
  2653
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@1024
  2654
      for (EdgeIt a(_graph); a != INVALID; ++a) {
deba@1024
  2655
        _edge_index.insert(std::make_pair(converter(map[a]), a));
deba@1024
  2656
      }
deba@1024
  2657
      return *this;
deba@1024
  2658
    }
deba@1024
  2659
deba@1024
  2660
    /// \brief Use previously constructed edge set
deba@1024
  2661
    ///
deba@1024
  2662
    /// Use previously constructed edge set, and specify the edge
deba@1024
  2663
    /// label map and a functor which converts the label map values to
deba@1024
  2664
    /// \c std::string.
deba@1024
  2665
    template <typename Map, typename Converter>
deba@1024
  2666
    BpGraphReader& useEdges(const Map& map,
deba@1024
  2667
                            const Converter& converter = Converter()) {
deba@1024
  2668
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@1024
  2669
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
deba@1024
  2670
      _use_edges = true;
deba@1024
  2671
      for (EdgeIt a(_graph); a != INVALID; ++a) {
deba@1024
  2672
        _edge_index.insert(std::make_pair(converter(map[a]), a));
deba@1024
  2673
      }
deba@1024
  2674
      return *this;
deba@1024
  2675
    }
deba@1024
  2676
deba@1024
  2677
    /// \brief Skip the reading of node section
deba@1024
  2678
    ///
deba@1024
  2679
    /// Omit the reading of the node section. This implies that each node
deba@1024
  2680
    /// map reading rule will be abandoned, and the nodes of the graph
deba@1024
  2681
    /// will not be constructed, which usually cause that the edge set
deba@1024
  2682
    /// could not be read due to lack of node name
deba@1024
  2683
    /// could not be read due to lack of node name resolving.
deba@1024
  2684
    /// Therefore \c skipEdges() function should also be used, or
deba@1024
  2685
    /// \c useNodes() should be used to specify the label of the nodes.
deba@1024
  2686
    BpGraphReader& skipNodes() {
deba@1024
  2687
      LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
deba@1024
  2688
      _skip_nodes = true;
deba@1024
  2689
      return *this;
deba@1024
  2690
    }
deba@1024
  2691
deba@1024
  2692
    /// \brief Skip the reading of edge section
deba@1024
  2693
    ///
deba@1024
  2694
    /// Omit the reading of the edge section. This implies that each edge
deba@1024
  2695
    /// map reading rule will be abandoned, and the edges of the graph
deba@1024
  2696
    /// will not be constructed.
deba@1024
  2697
    BpGraphReader& skipEdges() {
deba@1024
  2698
      LEMON_ASSERT(!_skip_edges, "Skip edges already set");
deba@1024
  2699
      _skip_edges = true;
deba@1024
  2700
      return *this;
deba@1024
  2701
    }
deba@1024
  2702
deba@1024
  2703
    /// @}
deba@1024
  2704
deba@1024
  2705
  private:
deba@1024
  2706
deba@1024
  2707
    bool readLine() {
deba@1024
  2708
      std::string str;
deba@1024
  2709
      while(++line_num, std::getline(*_is, str)) {
deba@1024
  2710
        line.clear(); line.str(str);
deba@1024
  2711
        char c;
deba@1024
  2712
        if (line >> std::ws >> c && c != '#') {
deba@1024
  2713
          line.putback(c);
deba@1024
  2714
          return true;
deba@1024
  2715
        }
deba@1024
  2716
      }
deba@1024
  2717
      return false;
deba@1024
  2718
    }
deba@1024
  2719
deba@1024
  2720
    bool readSuccess() {
deba@1024
  2721
      return static_cast<bool>(*_is);
deba@1024
  2722
    }
deba@1024
  2723
deba@1024
  2724
    void skipSection() {
deba@1024
  2725
      char c;
deba@1024
  2726
      while (readSuccess() && line >> c && c != '@') {
deba@1024
  2727
        readLine();
deba@1024
  2728
      }
deba@1024
  2729
      if (readSuccess()) {
deba@1024
  2730
        line.putback(c);
deba@1024
  2731
      }
deba@1024
  2732
    }
deba@1024
  2733
deba@1024
  2734
    void readRedNodes() {
deba@1024
  2735
deba@1030
  2736
      std::vector<int> map_index(_red_node_maps.size());
deba@1024
  2737
      int map_num, label_index;
deba@1024
  2738
deba@1024
  2739
      char c;
deba@1024
  2740
      if (!readLine() || !(line >> c) || c == '@') {
deba@1024
  2741
        if (readSuccess() && line) line.putback(c);
deba@1030
  2742
        if (!_red_node_maps.empty())
deba@1024
  2743
          throw FormatError("Cannot find map names");
deba@1024
  2744
        return;
deba@1024
  2745
      }
deba@1024
  2746
      line.putback(c);
deba@1024
  2747
deba@1024
  2748
      {
deba@1024
  2749
        std::map<std::string, int> maps;
deba@1024
  2750
deba@1024
  2751
        std::string map;
deba@1024
  2752
        int index = 0;
deba@1024
  2753
        while (_reader_bits::readToken(line, map)) {
deba@1024
  2754
          if (maps.find(map) != maps.end()) {
deba@1024
  2755
            std::ostringstream msg;
deba@1026
  2756
            msg << "Multiple occurence of red node map: " << map;
deba@1024
  2757
            throw FormatError(msg.str());
deba@1024
  2758
          }
deba@1024
  2759
          maps.insert(std::make_pair(map, index));
deba@1024
  2760
          ++index;
deba@1024
  2761
        }
deba@1024
  2762
deba@1030
  2763
        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
deba@1024
  2764
          std::map<std::string, int>::iterator jt =
deba@1030
  2765
            maps.find(_red_node_maps[i].first);
deba@1024
  2766
          if (jt == maps.end()) {
deba@1024
  2767
            std::ostringstream msg;
deba@1030
  2768
            msg << "Map not found: " << _red_node_maps[i].first;
deba@1024
  2769
            throw FormatError(msg.str());
deba@1024
  2770
          }
deba@1024
  2771
          map_index[i] = jt->second;
deba@1024
  2772
        }
deba@1024
  2773
deba@1024
  2774
        {
deba@1024
  2775
          std::map<std::string, int>::iterator jt = maps.find("label");
deba@1024
  2776
          if (jt != maps.end()) {
deba@1024
  2777
            label_index = jt->second;
deba@1024
  2778
          } else {
deba@1024
  2779
            label_index = -1;
deba@1024
  2780
          }
deba@1024
  2781
        }
deba@1024
  2782
        map_num = maps.size();
deba@1024
  2783
      }
deba@1024
  2784
deba@1024
  2785
      while (readLine() && line >> c && c != '@') {
deba@1024
  2786
        line.putback(c);
deba@1024
  2787
deba@1024
  2788
        std::vector<std::string> tokens(map_num);
deba@1024
  2789
        for (int i = 0; i < map_num; ++i) {
deba@1024
  2790
          if (!_reader_bits::readToken(line, tokens[i])) {
deba@1024
  2791
            std::ostringstream msg;
deba@1024
  2792
            msg << "Column not found (" << i + 1 << ")";
deba@1024
  2793
            throw FormatError(msg.str());
deba@1024
  2794
          }
deba@1024
  2795
        }
deba@1024
  2796
        if (line >> std::ws >> c)
deba@1024
  2797
          throw FormatError("Extra character at the end of line");
deba@1024
  2798
deba@1030
  2799
        RedNode n;
deba@1024
  2800
        if (!_use_nodes) {
deba@1024
  2801
          n = _graph.addRedNode();
deba@1024
  2802
          if (label_index != -1)
deba@1030
  2803
            _red_node_index.insert(std::make_pair(tokens[label_index], n));
deba@1024
  2804
        } else {
deba@1024
  2805
          if (label_index == -1)
deba@1024
  2806
            throw FormatError("Label map not found");
deba@1030
  2807
          typename std::map<std::string, RedNode>::iterator it =
deba@1030
  2808
            _red_node_index.find(tokens[label_index]);
deba@1030
  2809
          if (it == _red_node_index.end()) {
deba@1024
  2810
            std::ostringstream msg;
deba@1024
  2811
            msg << "Node with label not found: " << tokens[label_index];
deba@1024
  2812
            throw FormatError(msg.str());
deba@1024
  2813
          }
deba@1024
  2814
          n = it->second;
deba@1024
  2815
        }
deba@1024
  2816
deba@1030
  2817
        for (int i = 0; i < static_cast<int>(_red_node_maps.size()); ++i) {
deba@1030
  2818
          _red_node_maps[i].second->set(n, tokens[map_index[i]]);
deba@1024
  2819
        }
deba@1024
  2820
deba@1024
  2821
      }
deba@1024
  2822
      if (readSuccess()) {
deba@1024
  2823
        line.putback(c);
deba@1024
  2824
      }
deba@1024
  2825
    }
deba@1024
  2826
deba@1024
  2827
    void readBlueNodes() {
deba@1024
  2828
deba@1030
  2829
      std::vector<int> map_index(_blue_node_maps.size());
deba@1024
  2830
      int map_num, label_index;
deba@1024
  2831
deba@1024
  2832
      char c;
deba@1024
  2833
      if (!readLine() || !(line >> c) || c == '@') {
deba@1024
  2834
        if (readSuccess() && line) line.putback(c);
deba@1030
  2835
        if (!_blue_node_maps.empty())
deba@1024
  2836
          throw FormatError("Cannot find map names");
deba@1024
  2837
        return;
deba@1024
  2838
      }
deba@1024
  2839
      line.putback(c);
deba@1024
  2840
deba@1024
  2841
      {
deba@1024
  2842
        std::map<std::string, int> maps;
deba@1024
  2843
deba@1024
  2844
        std::string map;
deba@1024
  2845
        int index = 0;
deba@1024
  2846
        while (_reader_bits::readToken(line, map)) {
deba@1024
  2847
          if (maps.find(map) != maps.end()) {
deba@1024
  2848
            std::ostringstream msg;
deba@1026
  2849
            msg << "Multiple occurence of blue node map: " << map;
deba@1024
  2850
            throw FormatError(msg.str());
deba@1024
  2851
          }
deba@1024
  2852
          maps.insert(std::make_pair(map, index));
deba@1024
  2853
          ++index;
deba@1024
  2854
        }
deba@1024
  2855
deba@1030
  2856
        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
deba@1024
  2857
          std::map<std::string, int>::iterator jt =
deba@1030
  2858
            maps.find(_blue_node_maps[i].first);
deba@1024
  2859
          if (jt == maps.end()) {
deba@1024
  2860
            std::ostringstream msg;
deba@1030
  2861
            msg << "Map not found: " << _blue_node_maps[i].first;
deba@1024
  2862
            throw FormatError(msg.str());
deba@1024
  2863
          }
deba@1024
  2864
          map_index[i] = jt->second;
deba@1024
  2865
        }
deba@1024
  2866
deba@1024
  2867
        {
deba@1024
  2868
          std::map<std::string, int>::iterator jt = maps.find("label");
deba@1024
  2869
          if (jt != maps.end()) {
deba@1024
  2870
            label_index = jt->second;
deba@1024
  2871
          } else {
deba@1024
  2872
            label_index = -1;
deba@1024
  2873
          }
deba@1024
  2874
        }
deba@1024
  2875
        map_num = maps.size();
deba@1024
  2876
      }
deba@1024
  2877
deba@1024
  2878
      while (readLine() && line >> c && c != '@') {
deba@1024
  2879
        line.putback(c);
deba@1024
  2880
deba@1024
  2881
        std::vector<std::string> tokens(map_num);
deba@1024
  2882
        for (int i = 0; i < map_num; ++i) {
deba@1024
  2883
          if (!_reader_bits::readToken(line, tokens[i])) {
deba@1024
  2884
            std::ostringstream msg;
deba@1024
  2885
            msg << "Column not found (" << i + 1 << ")";
deba@1024
  2886
            throw FormatError(msg.str());
deba@1024
  2887
          }
deba@1024
  2888
        }
deba@1024
  2889
        if (line >> std::ws >> c)
deba@1024
  2890
          throw FormatError("Extra character at the end of line");
deba@1024
  2891
deba@1030
  2892
        BlueNode n;
deba@1024
  2893
        if (!_use_nodes) {
deba@1024
  2894
          n = _graph.addBlueNode();
deba@1024
  2895
          if (label_index != -1)
deba@1030
  2896
            _blue_node_index.insert(std::make_pair(tokens[label_index], n));
deba@1024
  2897
        } else {
deba@1024
  2898
          if (label_index == -1)
deba@1024
  2899
            throw FormatError("Label map not found");
deba@1030
  2900
          typename std::map<std::string, BlueNode>::iterator it =
deba@1030
  2901
            _blue_node_index.find(tokens[label_index]);
deba@1030
  2902
          if (it == _blue_node_index.end()) {
deba@1024
  2903
            std::ostringstream msg;
deba@1024
  2904
            msg << "Node with label not found: " << tokens[label_index];
deba@1024
  2905
            throw FormatError(msg.str());
deba@1024
  2906
          }
deba@1024
  2907
          n = it->second;
deba@1024
  2908
        }
deba@1024
  2909
deba@1030
  2910
        for (int i = 0; i < static_cast<int>(_blue_node_maps.size()); ++i) {
deba@1030
  2911
          _blue_node_maps[i].second->set(n, tokens[map_index[i]]);
deba@1024
  2912
        }
deba@1024
  2913
deba@1024
  2914
      }
deba@1024
  2915
      if (readSuccess()) {
deba@1024
  2916
        line.putback(c);
deba@1024
  2917
      }
deba@1024
  2918
    }
deba@1024
  2919
deba@1024
  2920
    void readEdges() {
deba@1024
  2921
deba@1024
  2922
      std::vector<int> map_index(_edge_maps.size());
deba@1024
  2923
      int map_num, label_index;
deba@1024
  2924
deba@1024
  2925
      char c;
deba@1024
  2926
      if (!readLine() || !(line >> c) || c == '@') {
deba@1024
  2927
        if (readSuccess() && line) line.putback(c);
deba@1024
  2928
        if (!_edge_maps.empty())
deba@1024
  2929
          throw FormatError("Cannot find map names");
deba@1024
  2930
        return;
deba@1024
  2931
      }
deba@1024
  2932
      line.putback(c);
deba@1024
  2933
deba@1024
  2934
      {
deba@1024
  2935
        std::map<std::string, int> maps;
deba@1024
  2936
deba@1024
  2937
        std::string map;
deba@1024
  2938
        int index = 0;
deba@1024
  2939
        while (_reader_bits::readToken(line, map)) {
deba@1024
  2940
          if (maps.find(map) != maps.end()) {
deba@1024
  2941
            std::ostringstream msg;
deba@1024
  2942
            msg << "Multiple occurence of edge map: " << map;
deba@1024
  2943
            throw FormatError(msg.str());
deba@1024
  2944
          }
deba@1024
  2945
          maps.insert(std::make_pair(map, index));
deba@1024
  2946
          ++index;
deba@1024
  2947
        }
deba@1024
  2948
deba@1024
  2949
        for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
deba@1024
  2950
          std::map<std::string, int>::iterator jt =
deba@1024
  2951
            maps.find(_edge_maps[i].first);
deba@1024
  2952
          if (jt == maps.end()) {
deba@1024
  2953
            std::ostringstream msg;
deba@1024
  2954
            msg << "Map not found: " << _edge_maps[i].first;
deba@1024
  2955
            throw FormatError(msg.str());
deba@1024
  2956
          }
deba@1024
  2957
          map_index[i] = jt->second;
deba@1024
  2958
        }
deba@1024
  2959
deba@1024
  2960
        {
deba@1024
  2961
          std::map<std::string, int>::iterator jt = maps.find("label");
deba@1024
  2962
          if (jt != maps.end()) {
deba@1024
  2963
            label_index = jt->second;
deba@1024
  2964
          } else {
deba@1024
  2965
            label_index = -1;
deba@1024
  2966
          }
deba@1024
  2967
        }
deba@1024
  2968
        map_num = maps.size();
deba@1024
  2969
      }
deba@1024
  2970
deba@1024
  2971
      while (readLine() && line >> c && c != '@') {
deba@1024
  2972
        line.putback(c);
deba@1024
  2973
deba@1024
  2974
        std::string source_token;
deba@1024
  2975
        std::string target_token;
deba@1024
  2976
deba@1024
  2977
        if (!_reader_bits::readToken(line, source_token))
deba@1024
  2978
          throw FormatError("Red node not found");
deba@1024
  2979
deba@1024
  2980
        if (!_reader_bits::readToken(line, target_token))
deba@1024
  2981
          throw FormatError("Blue node not found");
deba@1024
  2982
deba@1024
  2983
        std::vector<std::string> tokens(map_num);
deba@1024
  2984
        for (int i = 0; i < map_num; ++i) {
deba@1024
  2985
          if (!_reader_bits::readToken(line, tokens[i])) {
deba@1024
  2986
            std::ostringstream msg;
deba@1024
  2987
            msg << "Column not found (" << i + 1 << ")";
deba@1024
  2988
            throw FormatError(msg.str());
deba@1024
  2989
          }
deba@1024
  2990
        }
deba@1024
  2991
        if (line >> std::ws >> c)
deba@1024
  2992
          throw FormatError("Extra character at the end of line");
deba@1024
  2993
deba@1024
  2994
        Edge e;
deba@1024
  2995
        if (!_use_edges) {
deba@1030
  2996
          typename RedNodeIndex::iterator rit =
deba@1030
  2997
            _red_node_index.find(source_token);
deba@1030
  2998
          if (rit == _red_node_index.end()) {
deba@1024
  2999
            std::ostringstream msg;
deba@1024
  3000
            msg << "Item not found: " << source_token;
deba@1024
  3001
            throw FormatError(msg.str());
deba@1024
  3002
          }
deba@1030
  3003
          RedNode source = rit->second;
deba@1030
  3004
          typename BlueNodeIndex::iterator it =
deba@1030
  3005
            _blue_node_index.find(target_token);
deba@1030
  3006
          if (it == _blue_node_index.end()) {
deba@1024
  3007
            std::ostringstream msg;
deba@1024
  3008
            msg << "Item not found: " << target_token;
deba@1024
  3009
            throw FormatError(msg.str());
deba@1024
  3010
          }
deba@1030
  3011
          BlueNode target = it->second;
deba@1024
  3012
poroszd@1029
  3013
          // It is checked that source is red and
poroszd@1029
  3014
          // target is blue, so this should be safe:
deba@1030
  3015
          e = _graph.addEdge(source, target);
deba@1024
  3016
          if (label_index != -1)
deba@1024
  3017
            _edge_index.insert(std::make_pair(tokens[label_index], e));
deba@1024
  3018
        } else {
deba@1024
  3019
          if (label_index == -1)
deba@1024
  3020
            throw FormatError("Label map not found");
deba@1024
  3021
          typename std::map<std::string, Edge>::iterator it =
deba@1024
  3022
            _edge_index.find(tokens[label_index]);
deba@1024
  3023
          if (it == _edge_index.end()) {
deba@1024
  3024
            std::ostringstream msg;
deba@1024
  3025
            msg << "Edge with label not found: " << tokens[label_index];
deba@1024
  3026
            throw FormatError(msg.str());
deba@1024
  3027
          }
deba@1024
  3028
          e = it->second;
deba@1024
  3029
        }
deba@1024
  3030
deba@1024
  3031
        for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
deba@1024
  3032
          _edge_maps[i].second->set(e, tokens[map_index[i]]);
deba@1024
  3033
        }
deba@1024
  3034
deba@1024
  3035
      }
deba@1024
  3036
      if (readSuccess()) {
deba@1024
  3037
        line.putback(c);
deba@1024
  3038
      }
deba@1024
  3039
    }
deba@1024
  3040
deba@1024
  3041
    void readAttributes() {
deba@1024
  3042
deba@1024
  3043
      std::set<std::string> read_attr;
deba@1024
  3044
deba@1024
  3045
      char c;
deba@1024
  3046
      while (readLine() && line >> c && c != '@') {
deba@1024
  3047
        line.putback(c);
deba@1024
  3048
deba@1024
  3049
        std::string attr, token;
deba@1024
  3050
        if (!_reader_bits::readToken(line, attr))
deba@1024
  3051
          throw FormatError("Attribute name not found");
deba@1024
  3052
        if (!_reader_bits::readToken(line, token))
deba@1024
  3053
          throw FormatError("Attribute value not found");
deba@1024
  3054
        if (line >> c)
deba@1024
  3055
          throw FormatError("Extra character at the end of line");
deba@1024
  3056
deba@1024
  3057
        {
deba@1024
  3058
          std::set<std::string>::iterator it = read_attr.find(attr);
deba@1024
  3059
          if (it != read_attr.end()) {
deba@1024
  3060
            std::ostringstream msg;
deba@1024
  3061
            msg << "Multiple occurence of attribute: " << attr;
deba@1024
  3062
            throw FormatError(msg.str());
deba@1024
  3063
          }
deba@1024
  3064
          read_attr.insert(attr);
deba@1024
  3065
        }
deba@1024
  3066
deba@1024
  3067
        {
deba@1024
  3068
          typename Attributes::iterator it = _attributes.lower_bound(attr);
deba@1024
  3069
          while (it != _attributes.end() && it->first == attr) {
deba@1024
  3070
            it->second->set(token);
deba@1024
  3071
            ++it;
deba@1024
  3072
          }
deba@1024
  3073
        }
deba@1024
  3074
deba@1024
  3075
      }
deba@1024
  3076
      if (readSuccess()) {
deba@1024
  3077
        line.putback(c);
deba@1024
  3078
      }
deba@1024
  3079
      for (typename Attributes::iterator it = _attributes.begin();
deba@1024
  3080
           it != _attributes.end(); ++it) {
deba@1024
  3081
        if (read_attr.find(it->first) == read_attr.end()) {
deba@1024
  3082
          std::ostringstream msg;
deba@1024
  3083
          msg << "Attribute not found: " << it->first;
deba@1024
  3084
          throw FormatError(msg.str());
deba@1024
  3085
        }
deba@1024
  3086
      }
deba@1024
  3087
    }
deba@1024
  3088
deba@1024
  3089
  public:
deba@1024
  3090
deba@1024
  3091
    /// \name Execution of the Reader
deba@1024
  3092
    /// @{
deba@1024
  3093
deba@1024
  3094
    /// \brief Start the batch processing
deba@1024
  3095
    ///
deba@1024
  3096
    /// This function starts the batch processing
deba@1024
  3097
    void run() {
deba@1024
  3098
deba@1024
  3099
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
deba@1024
  3100
deba@1024
  3101
      bool red_nodes_done = _skip_nodes;
deba@1024
  3102
      bool blue_nodes_done = _skip_nodes;
deba@1024
  3103
      bool edges_done = _skip_edges;
deba@1024
  3104
      bool attributes_done = false;
deba@1024
  3105
deba@1024
  3106
      line_num = 0;
deba@1024
  3107
      readLine();
deba@1024
  3108
      skipSection();
deba@1024
  3109
deba@1024
  3110
      while (readSuccess()) {
deba@1024
  3111
        try {
deba@1024
  3112
          char c;
deba@1024
  3113
          std::string section, caption;
deba@1024
  3114
          line >> c;
deba@1024
  3115
          _reader_bits::readToken(line, section);
deba@1024
  3116
          _reader_bits::readToken(line, caption);
deba@1024
  3117
deba@1024
  3118
          if (line >> c)
deba@1024
  3119
            throw FormatError("Extra character at the end of line");
deba@1024
  3120
deba@1024
  3121
          if (section == "red_nodes" && !red_nodes_done) {
deba@1024
  3122
            if (_nodes_caption.empty() || _nodes_caption == caption) {
deba@1024
  3123
              readRedNodes();
deba@1024
  3124
              red_nodes_done = true;
deba@1024
  3125
            }
deba@1024
  3126
          } else if (section == "blue_nodes" && !blue_nodes_done) {
deba@1024
  3127
            if (_nodes_caption.empty() || _nodes_caption == caption) {
deba@1024
  3128
              readBlueNodes();
deba@1024
  3129
              blue_nodes_done = true;
deba@1024
  3130
            }
deba@1024
  3131
          } else if ((section == "edges" || section == "arcs") &&
deba@1024
  3132
                     !edges_done) {
deba@1024
  3133
            if (_edges_caption.empty() || _edges_caption == caption) {
deba@1024
  3134
              readEdges();
deba@1024
  3135
              edges_done = true;
deba@1024
  3136
            }
deba@1024
  3137
          } else if (section == "attributes" && !attributes_done) {
deba@1024
  3138
            if (_attributes_caption.empty() || _attributes_caption == caption) {
deba@1024
  3139
              readAttributes();
deba@1024
  3140
              attributes_done = true;
deba@1024
  3141
            }
deba@1024
  3142
          } else {
deba@1024
  3143
            readLine();
deba@1024
  3144
            skipSection();
deba@1024
  3145
          }
deba@1024
  3146
        } catch (FormatError& error) {
deba@1024
  3147
          error.line(line_num);
deba@1024
  3148
          error.file(_filename);
deba@1024
  3149
          throw;
deba@1024
  3150
        }
deba@1024
  3151
      }
deba@1024
  3152
deba@1024
  3153
      if (!red_nodes_done) {
deba@1024
  3154
        throw FormatError("Section @red_nodes not found");
deba@1024
  3155
      }
deba@1024
  3156
deba@1024
  3157
      if (!blue_nodes_done) {
deba@1024
  3158
        throw FormatError("Section @blue_nodes not found");
deba@1024
  3159
      }
deba@1024
  3160
deba@1024
  3161
      if (!edges_done) {
deba@1024
  3162
        throw FormatError("Section @edges not found");
deba@1024
  3163
      }
deba@1024
  3164
deba@1024
  3165
      if (!attributes_done && !_attributes.empty()) {
deba@1024
  3166
        throw FormatError("Section @attributes not found");
deba@1024
  3167
      }
deba@1024
  3168
deba@1024
  3169
    }
deba@1024
  3170
deba@1024
  3171
    /// @}
deba@1024
  3172
deba@1024
  3173
  };
deba@1024
  3174
deba@1024
  3175
  /// \ingroup lemon_io
deba@1024
  3176
  ///
deba@1024
  3177
  /// \brief Return a \ref BpGraphReader class
deba@1024
  3178
  ///
deba@1024
  3179
  /// This function just returns a \ref BpGraphReader class.
deba@1024
  3180
  ///
deba@1024
  3181
  /// With this function a graph can be read from an
deba@1024
  3182
  /// \ref lgf-format "LGF" file or input stream with several maps and
deba@1024
  3183
  /// attributes. For example, there is bipartite weighted matching problem
deba@1024
  3184
  /// on a graph, i.e. a graph with a \e weight map on the edges. This
deba@1024
  3185
  /// graph can be read with the following code:
deba@1024
  3186
  ///
deba@1024
  3187
  ///\code
deba@1024
  3188
  ///ListBpGraph graph;
deba@1024
  3189
  ///ListBpGraph::EdgeMap<int> weight(graph);
deba@1024
  3190
  ///bpGraphReader(graph, std::cin).
deba@1024
  3191
  ///  edgeMap("weight", weight).
deba@1024
  3192
  ///  run();
deba@1024
  3193
  ///\endcode
deba@1024
  3194
  ///
deba@1024
  3195
  /// For a complete documentation, please see the \ref BpGraphReader
deba@1024
  3196
  /// class documentation.
deba@1024
  3197
  /// \warning Don't forget to put the \ref BpGraphReader::run() "run()"
deba@1024
  3198
  /// to the end of the parameter list.
deba@1024
  3199
  /// \relates BpGraphReader
deba@1024
  3200
  /// \sa bpGraphReader(TBGR& graph, const std::string& fn)
deba@1024
  3201
  /// \sa bpGraphReader(TBGR& graph, const char* fn)
deba@1024
  3202
  template <typename TBGR>
deba@1024
  3203
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is) {
deba@1024
  3204
    BpGraphReader<TBGR> tmp(graph, is);
deba@1024
  3205
    return tmp;
deba@1024
  3206
  }
deba@1024
  3207
deba@1024
  3208
  /// \brief Return a \ref BpGraphReader class
deba@1024
  3209
  ///
deba@1024
  3210
  /// This function just returns a \ref BpGraphReader class.
deba@1024
  3211
  /// \relates BpGraphReader
deba@1024
  3212
  /// \sa bpGraphReader(TBGR& graph, std::istream& is)
deba@1024
  3213
  template <typename TBGR>
deba@1024
  3214
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn) {
deba@1024
  3215
    BpGraphReader<TBGR> tmp(graph, fn);
deba@1024
  3216
    return tmp;
deba@1024
  3217
  }
deba@1024
  3218
deba@1024
  3219
  /// \brief Return a \ref BpGraphReader class
deba@1024
  3220
  ///
deba@1024
  3221
  /// This function just returns a \ref BpGraphReader class.
deba@1024
  3222
  /// \relates BpGraphReader
deba@1024
  3223
  /// \sa bpGraphReader(TBGR& graph, std::istream& is)
deba@1024
  3224
  template <typename TBGR>
deba@1024
  3225
  BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char* fn) {
deba@1024
  3226
    BpGraphReader<TBGR> tmp(graph, fn);
deba@1024
  3227
    return tmp;
deba@1024
  3228
  }
deba@1024
  3229
deba@190
  3230
  class SectionReader;
deba@190
  3231
deba@190
  3232
  SectionReader sectionReader(std::istream& is);
deba@190
  3233
  SectionReader sectionReader(const std::string& fn);
deba@190
  3234
  SectionReader sectionReader(const char* fn);
alpar@209
  3235
kpeter@192
  3236
  /// \ingroup lemon_io
kpeter@192
  3237
  ///
deba@189
  3238
  /// \brief Section reader class
deba@189
  3239
  ///
alpar@209
  3240
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
kpeter@192
  3241
  /// which contain any data in arbitrary format. Such sections can be
alpar@209
  3242
  /// read with this class. A reading rule can be added to the class
kpeter@192
  3243
  /// with two different functions. With the \c sectionLines() function a
kpeter@192
  3244
  /// functor can process the section line-by-line, while with the \c
deba@189
  3245
  /// sectionStream() member the section can be read from an input
deba@189
  3246
  /// stream.
deba@189
  3247
  class SectionReader {
deba@189
  3248
  private:
alpar@209
  3249
deba@189
  3250
    std::istream* _is;
deba@189
  3251
    bool local_is;
deba@290
  3252
    std::string _filename;
deba@189
  3253
deba@189
  3254
    typedef std::map<std::string, _reader_bits::Section*> Sections;
deba@189
  3255
    Sections _sections;
deba@189
  3256
deba@189
  3257
    int line_num;
deba@189
  3258
    std::istringstream line;
deba@189
  3259
deba@189
  3260
  public:
deba@189
  3261
deba@189
  3262
    /// \brief Constructor
deba@189
  3263
    ///
deba@189
  3264
    /// Construct a section reader, which reads from the given input
deba@189
  3265
    /// stream.
alpar@209
  3266
    SectionReader(std::istream& is)
deba@189
  3267
      : _is(&is), local_is(false) {}
deba@189
  3268
deba@189
  3269
    /// \brief Constructor
deba@189
  3270
    ///
deba@189
  3271
    /// Construct a section reader, which reads from the given file.
alpar@209
  3272
    SectionReader(const std::string& fn)
deba@290
  3273
      : _is(new std::ifstream(fn.c_str())), local_is(true),
deba@290
  3274
        _filename(fn) {
deba@295
  3275
      if (!(*_is)) {
deba@295
  3276
        delete _is;
deba@295
  3277
        throw IoError("Cannot open file", fn);
deba@295
  3278
      }
deba@290
  3279
    }
alpar@209
  3280
deba@189
  3281
    /// \brief Constructor
deba@189
  3282
    ///
deba@189
  3283
    /// Construct a section reader, which reads from the given file.
alpar@209
  3284
    SectionReader(const char* fn)
deba@290
  3285
      : _is(new std::ifstream(fn)), local_is(true),
deba@290
  3286
        _filename(fn) {
deba@295
  3287
      if (!(*_is)) {
deba@295
  3288
        delete _is;
deba@295
  3289
        throw IoError("Cannot open file", fn);
deba@295
  3290
      }
deba@290
  3291
    }
deba@189
  3292
deba@189
  3293
    /// \brief Destructor
deba@189
  3294
    ~SectionReader() {
alpar@209
  3295
      for (Sections::iterator it = _sections.begin();
alpar@209
  3296
           it != _sections.end(); ++it) {
alpar@209
  3297
        delete it->second;
deba@189
  3298
      }
deba@189
  3299
deba@189
  3300
      if (local_is) {
alpar@209
  3301
        delete _is;
deba@189
  3302
      }
deba@189
  3303
deba@189
  3304
    }
deba@189
  3305
deba@189
  3306
  private:
deba@190
  3307
deba@190
  3308
    friend SectionReader sectionReader(std::istream& is);
deba@190
  3309
    friend SectionReader sectionReader(const std::string& fn);
deba@190
  3310
    friend SectionReader sectionReader(const char* fn);
deba@190
  3311
alpar@209
  3312
    SectionReader(SectionReader& other)
deba@190
  3313
      : _is(other._is), local_is(other.local_is) {
deba@190
  3314
deba@190
  3315
      other._is = 0;
deba@190
  3316
      other.local_is = false;
alpar@209
  3317
deba@190
  3318
      _sections.swap(other._sections);
deba@190
  3319
    }
alpar@209
  3320
deba@189
  3321
    SectionReader& operator=(const SectionReader&);
deba@189
  3322
deba@189
  3323
  public:
deba@189
  3324
kpeter@584
  3325
    /// \name Section Readers
deba@189
  3326
    /// @{
deba@189
  3327
deba@189
  3328
    /// \brief Add a section processor with line oriented reading
deba@189
  3329
    ///
deba@189
  3330
    /// The first parameter is the type descriptor of the section, the
deba@189
  3331
    /// second is a functor, which takes just one \c std::string
deba@189
  3332
    /// parameter. At the reading process, each line of the section
deba@189
  3333
    /// will be given to the functor object. However, the empty lines
deba@189
  3334
    /// and the comment lines are filtered out, and the leading
deba@189
  3335
    /// whitespaces are trimmed from each processed string.
deba@189
  3336
    ///
kpeter@786
  3337
    /// For example, let's see a section, which contain several
deba@189
  3338
    /// integers, which should be inserted into a vector.
deba@189
  3339
    ///\code
deba@189
  3340
    ///  @numbers
deba@189
  3341
    ///  12 45 23
deba@189
  3342
    ///  4
deba@189
  3343
    ///  23 6
deba@189
  3344
    ///\endcode
deba@189
  3345
    ///
kpeter@192
  3346
    /// The functor is implemented as a struct:
deba@189
  3347
    ///\code
deba@189
  3348
    ///  struct NumberSection {
deba@189
  3349
    ///    std::vector<int>& _data;
deba@189
  3350
    ///    NumberSection(std::vector<int>& data) : _data(data) {}
deba@189
  3351
    ///    void operator()(const std::string& line) {
deba@189
  3352
    ///      std::istringstream ls(line);
deba@189
  3353
    ///      int value;
deba@189
  3354
    ///      while (ls >> value) _data.push_back(value);
deba@189
  3355
    ///    }
deba@189
  3356
    ///  };
deba@189
  3357
    ///
deba@189
  3358
    ///  // ...
deba@189
  3359
    ///
alpar@209
  3360
    ///  reader.sectionLines("numbers", NumberSection(vec));
deba@189
  3361
    ///\endcode
deba@189
  3362
    template <typename Functor>
deba@189
  3363
    SectionReader& sectionLines(const std::string& type, Functor functor) {
kpeter@192
  3364
      LEMON_ASSERT(!type.empty(), "Type is empty.");
alpar@209
  3365
      LEMON_ASSERT(_sections.find(type) == _sections.end(),
alpar@209
  3366
                   "Multiple reading of section.");
alpar@209
  3367
      _sections.insert(std::make_pair(type,
deba@189
  3368
        new _reader_bits::LineSection<Functor>(functor)));
deba@189
  3369
      return *this;
deba@189
  3370
    }
deba@189
  3371
deba@189
  3372
deba@189
  3373
    /// \brief Add a section processor with stream oriented reading
deba@189
  3374
    ///
deba@189
  3375
    /// The first parameter is the type of the section, the second is
kpeter@192
  3376
    /// a functor, which takes an \c std::istream& and an \c int&
deba@189
  3377
    /// parameter, the latter regard to the line number of stream. The
deba@189
  3378
    /// functor can read the input while the section go on, and the
deba@189
  3379
    /// line number should be modified accordingly.
deba@189
  3380
    template <typename Functor>
deba@189
  3381
    SectionReader& sectionStream(const std::string& type, Functor functor) {
kpeter@192
  3382
      LEMON_ASSERT(!type.empty(), "Type is empty.");
alpar@209
  3383
      LEMON_ASSERT(_sections.find(type) == _sections.end(),
alpar@209
  3384
                   "Multiple reading of section.");
alpar@209
  3385
      _sections.insert(std::make_pair(type,
alpar@209
  3386
         new _reader_bits::StreamSection<Functor>(functor)));
deba@189
  3387
      return *this;
alpar@209
  3388
    }
alpar@209
  3389
deba@189
  3390
    /// @}
deba@189
  3391
deba@189
  3392
  private:
deba@189
  3393
deba@189
  3394
    bool readLine() {
deba@189
  3395
      std::string str;
deba@189
  3396
      while(++line_num, std::getline(*_is, str)) {
alpar@209
  3397
        line.clear(); line.str(str);
alpar@209
  3398
        char c;
alpar@209
  3399
        if (line >> std::ws >> c && c != '#') {
alpar@209
  3400
          line.putback(c);
alpar@209
  3401
          return true;
alpar@209
  3402
        }
deba@189
  3403
      }
deba@189
  3404
      return false;
deba@189
  3405
    }
deba@189
  3406
deba@189
  3407
    bool readSuccess() {
deba@189
  3408
      return static_cast<bool>(*_is);
deba@189
  3409
    }
alpar@209
  3410
deba@189
  3411
    void skipSection() {
deba@189
  3412
      char c;
deba@189
  3413
      while (readSuccess() && line >> c && c != '@') {
alpar@209
  3414
        readLine();
deba@189
  3415
      }
deba@427
  3416
      if (readSuccess()) {
deba@427
  3417
        line.putback(c);
deba@427
  3418
      }
deba@189
  3419
    }
deba@189
  3420
deba@189
  3421
  public:
deba@189
  3422
deba@189
  3423
kpeter@584
  3424
    /// \name Execution of the Reader
deba@189
  3425
    /// @{
deba@189
  3426
deba@189
  3427
    /// \brief Start the batch processing
deba@189
  3428
    ///
kpeter@192
  3429
    /// This function starts the batch processing.
deba@189
  3430
    void run() {
alpar@209
  3431
deba@189
  3432
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
alpar@209
  3433
deba@189
  3434
      std::set<std::string> extra_sections;
deba@189
  3435
alpar@209
  3436
      line_num = 0;
deba@189
  3437
      readLine();
deba@189
  3438
      skipSection();
deba@189
  3439
deba@189
  3440
      while (readSuccess()) {
alpar@209
  3441
        try {
alpar@209
  3442
          char c;
alpar@209
  3443
          std::string section, caption;
alpar@209
  3444
          line >> c;
alpar@209
  3445
          _reader_bits::readToken(line, section);
alpar@209
  3446
          _reader_bits::readToken(line, caption);
alpar@209
  3447
alpar@209
  3448
          if (line >> c)
kpeter@291
  3449
            throw FormatError("Extra character at the end of line");
alpar@209
  3450
alpar@209
  3451
          if (extra_sections.find(section) != extra_sections.end()) {
alpar@209
  3452
            std::ostringstream msg;
kpeter@291
  3453
            msg << "Multiple occurence of section: " << section;
deba@290
  3454
            throw FormatError(msg.str());
alpar@209
  3455
          }
alpar@209
  3456
          Sections::iterator it = _sections.find(section);
alpar@209
  3457
          if (it != _sections.end()) {
alpar@209
  3458
            extra_sections.insert(section);
alpar@209
  3459
            it->second->process(*_is, line_num);
alpar@209
  3460
          }
alpar@209
  3461
          readLine();
alpar@209
  3462
          skipSection();
deba@290
  3463
        } catch (FormatError& error) {
alpar@209
  3464
          error.line(line_num);
deba@290
  3465
          error.file(_filename);
alpar@209
  3466
          throw;
alpar@209
  3467
        }
deba@189
  3468
      }
deba@189
  3469
      for (Sections::iterator it = _sections.begin();
alpar@209
  3470
           it != _sections.end(); ++it) {
alpar@209
  3471
        if (extra_sections.find(it->first) == extra_sections.end()) {
alpar@209
  3472
          std::ostringstream os;
alpar@209
  3473
          os << "Cannot find section: " << it->first;
deba@290
  3474
          throw FormatError(os.str());
alpar@209
  3475
        }
deba@189
  3476
      }
deba@189
  3477
    }
deba@189
  3478
deba@189
  3479
    /// @}
alpar@209
  3480
deba@189
  3481
  };
deba@189
  3482
deba@598
  3483
  /// \ingroup lemon_io
deba@598
  3484
  ///
deba@598
  3485
  /// \brief Return a \ref SectionReader class
deba@598
  3486
  ///
deba@598
  3487
  /// This function just returns a \ref SectionReader class.
deba@598
  3488
  ///
deba@598
  3489
  /// Please see SectionReader documentation about the custom section
deba@598
  3490
  /// input.
deba@598
  3491
  ///
deba@598
  3492
  /// \relates SectionReader
deba@598
  3493
  /// \sa sectionReader(const std::string& fn)
deba@598
  3494
  /// \sa sectionReader(const char *fn)
deba@598
  3495
  inline SectionReader sectionReader(std::istream& is) {
deba@598
  3496
    SectionReader tmp(is);
deba@598
  3497
    return tmp;
deba@598
  3498
  }
deba@598
  3499
kpeter@192
  3500
  /// \brief Return a \ref SectionReader class
alpar@209
  3501
  ///
kpeter@192
  3502
  /// This function just returns a \ref SectionReader class.
deba@189
  3503
  /// \relates SectionReader
deba@598
  3504
  /// \sa sectionReader(std::istream& is)
deba@598
  3505
  inline SectionReader sectionReader(const std::string& fn) {
deba@598
  3506
    SectionReader tmp(fn);
deba@189
  3507
    return tmp;
deba@189
  3508
  }
deba@189
  3509
kpeter@192
  3510
  /// \brief Return a \ref SectionReader class
alpar@209
  3511
  ///
kpeter@192
  3512
  /// This function just returns a \ref SectionReader class.
deba@189
  3513
  /// \relates SectionReader
deba@598
  3514
  /// \sa sectionReader(std::istream& is)
deba@189
  3515
  inline SectionReader sectionReader(const char* fn) {
deba@189
  3516
    SectionReader tmp(fn);
deba@189
  3517
    return tmp;
deba@189
  3518
  }
deba@189
  3519
deba@173
  3520
  /// \ingroup lemon_io
deba@173
  3521
  ///
alpar@209
  3522
  /// \brief Reader for the contents of the \ref lgf-format "LGF" file
deba@173
  3523
  ///
deba@173
  3524
  /// This class can be used to read the sections, the map names and
ladanyi@236
  3525
  /// the attributes from a file. Usually, the LEMON programs know
deba@173
  3526
  /// that, which type of graph, which maps and which attributes
deba@173
  3527
  /// should be read from a file, but in general tools (like glemon)
alpar@179
  3528
  /// the contents of an LGF file should be guessed somehow. This class
deba@173
  3529
  /// reads the graph and stores the appropriate information for
deba@173
  3530
  /// reading the graph.
deba@173
  3531
  ///
alpar@209
  3532
  ///\code
alpar@209
  3533
  /// LgfContents contents("graph.lgf");
alpar@179
  3534
  /// contents.run();
deba@173
  3535
  ///
kpeter@192
  3536
  /// // Does it contain any node section and arc section?
alpar@179
  3537
  /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
kpeter@192
  3538
  ///   std::cerr << "Failure, cannot find graph." << std::endl;
deba@173
  3539
  ///   return -1;
deba@173
  3540
  /// }
alpar@209
  3541
  /// std::cout << "The name of the default node section: "
alpar@179
  3542
  ///           << contents.nodeSection(0) << std::endl;
alpar@209
  3543
  /// std::cout << "The number of the arc maps: "
alpar@179
  3544
  ///           << contents.arcMaps(0).size() << std::endl;
alpar@209
  3545
  /// std::cout << "The name of second arc map: "
alpar@179
  3546
  ///           << contents.arcMaps(0)[1] << std::endl;
deba@173
  3547
  ///\endcode
alpar@209
  3548
  class LgfContents {
deba@173
  3549
  private:
deba@173
  3550
deba@173
  3551
    std::istream* _is;
deba@173
  3552
    bool local_is;
deba@173
  3553
deba@173
  3554
    std::vector<std::string> _node_sections;
deba@173
  3555
    std::vector<std::string> _edge_sections;
deba@173
  3556
    std::vector<std::string> _attribute_sections;
deba@173
  3557
    std::vector<std::string> _extra_sections;
deba@173
  3558
deba@173
  3559
    std::vector<bool> _arc_sections;
deba@173
  3560
deba@173
  3561
    std::vector<std::vector<std::string> > _node_maps;
deba@173
  3562
    std::vector<std::vector<std::string> > _edge_maps;
deba@173
  3563
deba@173
  3564
    std::vector<std::vector<std::string> > _attributes;
deba@173
  3565
deba@173
  3566
deba@173
  3567
    int line_num;
deba@173
  3568
    std::istringstream line;
alpar@209
  3569
deba@173
  3570
  public:
deba@173
  3571
deba@173
  3572
    /// \brief Constructor
deba@173
  3573
    ///
alpar@179
  3574
    /// Construct an \e LGF contents reader, which reads from the given
deba@173
  3575
    /// input stream.
alpar@209
  3576
    LgfContents(std::istream& is)
deba@173
  3577
      : _is(&is), local_is(false) {}
deba@173
  3578
deba@173
  3579
    /// \brief Constructor
deba@173
  3580
    ///
alpar@179
  3581
    /// Construct an \e LGF contents reader, which reads from the given
deba@173
  3582
    /// file.
alpar@209
  3583
    LgfContents(const std::string& fn)
deba@290
  3584
      : _is(new std::ifstream(fn.c_str())), local_is(true) {
deba@295
  3585
      if (!(*_is)) {
deba@295
  3586
        delete _is;
deba@295
  3587
        throw IoError("Cannot open file", fn);
deba@295
  3588
      }
deba@290
  3589
    }
deba@173
  3590
deba@173
  3591
    /// \brief Constructor
deba@173
  3592
    ///
alpar@179
  3593
    /// Construct an \e LGF contents reader, which reads from the given
deba@173
  3594
    /// file.
alpar@179
  3595
    LgfContents(const char* fn)
deba@290
  3596
      : _is(new std::ifstream(fn)), local_is(true) {
deba@295
  3597
      if (!(*_is)) {
deba@295
  3598
        delete _is;
deba@295
  3599
        throw IoError("Cannot open file", fn);
deba@295
  3600
      }
deba@290
  3601
    }
alpar@209
  3602
deba@173
  3603
    /// \brief Destructor
alpar@179
  3604
    ~LgfContents() {
deba@173
  3605
      if (local_is) delete _is;
deba@173
  3606
    }
deba@173
  3607
deba@190
  3608
  private:
alpar@209
  3609
deba@190
  3610
    LgfContents(const LgfContents&);
deba@190
  3611
    LgfContents& operator=(const LgfContents&);
deba@190
  3612
deba@190
  3613
  public:
deba@190
  3614
deba@173
  3615
kpeter@584
  3616
    /// \name Node Sections
deba@173
  3617
    /// @{
deba@173
  3618
deba@173
  3619
    /// \brief Gives back the number of node sections in the file.
deba@173
  3620
    ///
deba@173
  3621
    /// Gives back the number of node sections in the file.
deba@173
  3622
    int nodeSectionNum() const {
deba@173
  3623
      return _node_sections.size();
deba@173
  3624
    }
deba@173
  3625
alpar@209
  3626
    /// \brief Returns the node section name at the given position.
deba@173
  3627
    ///
alpar@209
  3628
    /// Returns the node section name at the given position.
deba@173
  3629
    const std::string& nodeSection(int i) const {
deba@173
  3630
      return _node_sections[i];
deba@173
  3631
    }
deba@173
  3632
deba@173
  3633
    /// \brief Gives back the node maps for the given section.
deba@173
  3634
    ///
deba@173
  3635
    /// Gives back the node maps for the given section.
alpar@182
  3636
    const std::vector<std::string>& nodeMapNames(int i) const {
deba@173
  3637
      return _node_maps[i];
deba@173
  3638
    }
deba@173
  3639
deba@173
  3640
    /// @}
deba@173
  3641
kpeter@584
  3642
    /// \name Arc/Edge Sections
deba@173
  3643
    /// @{
deba@173
  3644
alpar@181
  3645
    /// \brief Gives back the number of arc/edge sections in the file.
deba@173
  3646
    ///
alpar@181
  3647
    /// Gives back the number of arc/edge sections in the file.
alpar@181
  3648
    /// \note It is synonym of \c edgeSectionNum().
deba@173
  3649
    int arcSectionNum() const {
deba@173
  3650
      return _edge_sections.size();
deba@173
  3651
    }
deba@173
  3652
alpar@209
  3653
    /// \brief Returns the arc/edge section name at the given position.
deba@173
  3654
    ///
alpar@209
  3655
    /// Returns the arc/edge section name at the given position.
alpar@181
  3656
    /// \note It is synonym of \c edgeSection().
deba@173
  3657
    const std::string& arcSection(int i) const {
deba@173
  3658
      return _edge_sections[i];
deba@173
  3659
    }
deba@173
  3660
alpar@181
  3661
    /// \brief Gives back the arc/edge maps for the given section.
deba@173
  3662
    ///
alpar@181
  3663
    /// Gives back the arc/edge maps for the given section.
alpar@182
  3664
    /// \note It is synonym of \c edgeMapNames().
alpar@182
  3665
    const std::vector<std::string>& arcMapNames(int i) const {
deba@173
  3666
      return _edge_maps[i];
deba@173
  3667
    }
deba@173
  3668
deba@173
  3669
    /// @}
deba@173
  3670
alpar@181
  3671
    /// \name Synonyms
deba@173
  3672
    /// @{
deba@173
  3673
alpar@181
  3674
    /// \brief Gives back the number of arc/edge sections in the file.
deba@173
  3675
    ///
alpar@181
  3676
    /// Gives back the number of arc/edge sections in the file.
alpar@181
  3677
    /// \note It is synonym of \c arcSectionNum().
deba@173
  3678
    int edgeSectionNum() const {
deba@173
  3679
      return _edge_sections.size();
deba@173
  3680
    }
deba@173
  3681
alpar@209
  3682
    /// \brief Returns the section name at the given position.
deba@173
  3683
    ///
alpar@209
  3684
    /// Returns the section name at the given position.
alpar@181
  3685
    /// \note It is synonym of \c arcSection().
deba@173
  3686
    const std::string& edgeSection(int i) const {
deba@173
  3687
      return _edge_sections[i];
deba@173
  3688
    }
deba@173
  3689
deba@173
  3690
    /// \brief Gives back the edge maps for the given section.
deba@173
  3691
    ///
deba@173
  3692
    /// Gives back the edge maps for the given section.
alpar@182
  3693
    /// \note It is synonym of \c arcMapNames().
alpar@182
  3694
    const std::vector<std::string>& edgeMapNames(int i) const {
deba@173
  3695
      return _edge_maps[i];
deba@173
  3696
    }
deba@173
  3697
deba@173
  3698
    /// @}
deba@173
  3699
kpeter@584
  3700
    /// \name Attribute Sections
deba@173
  3701
    /// @{
deba@173
  3702
deba@173
  3703
    /// \brief Gives back the number of attribute sections in the file.
deba@173
  3704
    ///
deba@173
  3705
    /// Gives back the number of attribute sections in the file.
deba@173
  3706
    int attributeSectionNum() const {
deba@173
  3707
      return _attribute_sections.size();
deba@173
  3708
    }
deba@173
  3709
alpar@209
  3710
    /// \brief Returns the attribute section name at the given position.
deba@173
  3711
    ///
alpar@209
  3712
    /// Returns the attribute section name at the given position.
alpar@182
  3713
    const std::string& attributeSectionNames(int i) const {
deba@173
  3714
      return _attribute_sections[i];
deba@173
  3715
    }
deba@173
  3716
deba@173
  3717
    /// \brief Gives back the attributes for the given section.
deba@173
  3718
    ///
deba@173
  3719
    /// Gives back the attributes for the given section.
deba@173
  3720
    const std::vector<std::string>& attributes(int i) const {
deba@173
  3721
      return _attributes[i];
deba@173
  3722
    }
deba@173
  3723
deba@173
  3724
    /// @}
deba@173
  3725
kpeter@584
  3726
    /// \name Extra Sections
deba@173
  3727
    /// @{
deba@173
  3728
deba@173
  3729
    /// \brief Gives back the number of extra sections in the file.
deba@173
  3730
    ///
deba@173
  3731
    /// Gives back the number of extra sections in the file.
deba@173
  3732
    int extraSectionNum() const {
deba@173
  3733
      return _extra_sections.size();
deba@173
  3734
    }
deba@173
  3735
alpar@209
  3736
    /// \brief Returns the extra section type at the given position.
deba@173
  3737
    ///
alpar@209
  3738
    /// Returns the section type at the given position.
deba@173
  3739
    const std::string& extraSection(int i) const {
deba@173
  3740
      return _extra_sections[i];
deba@173
  3741
    }
deba@173
  3742
deba@173
  3743
    /// @}
deba@173
  3744
deba@173
  3745
  private:
deba@173
  3746
deba@173
  3747
    bool readLine() {
deba@173
  3748
      std::string str;
deba@173
  3749
      while(++line_num, std::getline(*_is, str)) {
alpar@209
  3750
        line.clear(); line.str(str);
alpar@209
  3751
        char c;
alpar@209
  3752
        if (line >> std::ws >> c && c != '#') {
alpar@209
  3753
          line.putback(c);
alpar@209
  3754
          return true;
alpar@209
  3755
        }
deba@173
  3756
      }
deba@173
  3757
      return false;
deba@173
  3758
    }
deba@173
  3759
deba@173
  3760
    bool readSuccess() {
deba@173
  3761
      return static_cast<bool>(*_is);
deba@173
  3762
    }
deba@173
  3763
deba@173
  3764
    void skipSection() {
deba@173
  3765
      char c;
deba@173
  3766
      while (readSuccess() && line >> c && c != '@') {
alpar@209
  3767
        readLine();
deba@173
  3768
      }
deba@427
  3769
      if (readSuccess()) {
deba@427
  3770
        line.putback(c);
deba@427
  3771
      }
deba@173
  3772
    }
deba@173
  3773
deba@173
  3774
    void readMaps(std::vector<std::string>& maps) {
deba@186
  3775
      char c;
deba@186
  3776
      if (!readLine() || !(line >> c) || c == '@') {
alpar@209
  3777
        if (readSuccess() && line) line.putback(c);
alpar@209
  3778
        return;
deba@186
  3779
      }
deba@186
  3780
      line.putback(c);
deba@173
  3781
      std::string map;
deba@173
  3782
      while (_reader_bits::readToken(line, map)) {
alpar@209
  3783
        maps.push_back(map);
deba@173
  3784
      }
deba@173
  3785
    }
deba@173
  3786
deba@173
  3787
    void readAttributes(std::vector<std::string>& attrs) {
deba@173
  3788
      readLine();
deba@173
  3789
      char c;
deba@173
  3790
      while (readSuccess() && line >> c && c != '@') {
alpar@209
  3791
        line.putback(c);
alpar@209
  3792
        std::string attr;
alpar@209
  3793
        _reader_bits::readToken(line, attr);
alpar@209
  3794
        attrs.push_back(attr);
alpar@209
  3795
        readLine();
deba@173
  3796
      }
deba@173
  3797
      line.putback(c);
deba@173
  3798
    }
deba@173
  3799
deba@173
  3800
  public:
deba@173
  3801
kpeter@584
  3802
    /// \name Execution of the Contents Reader
deba@173
  3803
    /// @{
deba@173
  3804
kpeter@192
  3805
    /// \brief Starts the reading
deba@173
  3806
    ///
kpeter@192
  3807
    /// This function starts the reading.
deba@173
  3808
    void run() {
deba@173
  3809
deba@173
  3810
      readLine();
deba@173
  3811
      skipSection();
deba@173
  3812
deba@173
  3813
      while (readSuccess()) {
deba@173
  3814
alpar@209
  3815
        char c;
alpar@209
  3816
        line >> c;
alpar@209
  3817
alpar@209
  3818
        std::string section, caption;
alpar@209
  3819
        _reader_bits::readToken(line, section);
alpar@209
  3820
        _reader_bits::readToken(line, caption);
alpar@209
  3821
alpar@209
  3822
        if (section == "nodes") {
alpar@209
  3823
          _node_sections.push_back(caption);
alpar@209
  3824
          _node_maps.push_back(std::vector<std::string>());
alpar@209
  3825
          readMaps(_node_maps.back());
alpar@209
  3826
          readLine(); skipSection();
alpar@209
  3827
        } else if (section == "arcs" || section == "edges") {
alpar@209
  3828
          _edge_sections.push_back(caption);
alpar@209
  3829
          _arc_sections.push_back(section == "arcs");
alpar@209
  3830
          _edge_maps.push_back(std::vector<std::string>());
alpar@209
  3831
          readMaps(_edge_maps.back());
alpar@209
  3832
          readLine(); skipSection();
alpar@209
  3833
        } else if (section == "attributes") {
alpar@209
  3834
          _attribute_sections.push_back(caption);
alpar@209
  3835
          _attributes.push_back(std::vector<std::string>());
alpar@209
  3836
          readAttributes(_attributes.back());
alpar@209
  3837
        } else {
alpar@209
  3838
          _extra_sections.push_back(section);
alpar@209
  3839
          readLine(); skipSection();
alpar@209
  3840
        }
deba@173
  3841
      }
deba@173
  3842
    }
deba@173
  3843
deba@173
  3844
    /// @}
alpar@209
  3845
deba@173
  3846
  };
deba@127
  3847
}
deba@127
  3848
deba@127
  3849
#endif