lemon/lgf_writer.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 01 Oct 2008 13:56:40 +0200
changeset 294 cbe3ec2d59d2
parent 293 47fbc814aa31
parent 291 d901321d6555
child 295 7c796c1cf1b0
permissions -rw-r--r--
Merge
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
 *
deba@127
     5
 * Copyright (C) 2003-2008
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" writer.
deba@127
    22
deba@127
    23
deba@127
    24
#ifndef LEMON_LGF_WRITER_H
deba@127
    25
#define LEMON_LGF_WRITER_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 <algorithm>
deba@127
    32
deba@127
    33
#include <vector>
deba@127
    34
#include <functional>
deba@127
    35
deba@127
    36
#include <lemon/assert.h>
deba@220
    37
#include <lemon/core.h>
deba@220
    38
#include <lemon/maps.h>
deba@127
    39
deba@248
    40
#include <lemon/concept_check.h>
deba@248
    41
#include <lemon/concepts/maps.h>
deba@248
    42
deba@127
    43
namespace lemon {
deba@127
    44
deba@127
    45
  namespace _writer_bits {
deba@127
    46
deba@127
    47
    template <typename Value>
deba@127
    48
    struct DefaultConverter {
deba@127
    49
      std::string operator()(const Value& value) {
alpar@209
    50
        std::ostringstream os;
alpar@209
    51
        os << value;
alpar@209
    52
        return os.str();
deba@127
    53
      }
deba@127
    54
    };
deba@127
    55
deba@127
    56
    template <typename T>
deba@127
    57
    bool operator<(const T&, const T&) {
deba@290
    58
      throw FormatError("Label map is not comparable");
deba@127
    59
    }
deba@127
    60
deba@127
    61
    template <typename _Map>
deba@127
    62
    class MapLess {
deba@127
    63
    public:
deba@127
    64
      typedef _Map Map;
deba@127
    65
      typedef typename Map::Key Item;
deba@127
    66
deba@127
    67
    private:
deba@127
    68
      const Map& _map;
alpar@209
    69
deba@127
    70
    public:
deba@127
    71
      MapLess(const Map& map) : _map(map) {}
deba@127
    72
deba@127
    73
      bool operator()(const Item& left, const Item& right) {
alpar@209
    74
        return _map[left] < _map[right];
deba@127
    75
      }
deba@127
    76
    };
deba@127
    77
deba@165
    78
    template <typename _Graph, bool _dir, typename _Map>
deba@165
    79
    class GraphArcMapLess {
deba@165
    80
    public:
deba@165
    81
      typedef _Map Map;
deba@165
    82
      typedef _Graph Graph;
deba@165
    83
      typedef typename Graph::Edge Item;
deba@165
    84
deba@165
    85
    private:
deba@165
    86
      const Graph& _graph;
deba@165
    87
      const Map& _map;
alpar@209
    88
deba@165
    89
    public:
alpar@209
    90
      GraphArcMapLess(const Graph& graph, const Map& map)
alpar@209
    91
        : _graph(graph), _map(map) {}
deba@165
    92
deba@165
    93
      bool operator()(const Item& left, const Item& right) {
alpar@209
    94
        return _map[_graph.direct(left, _dir)] <
alpar@209
    95
          _map[_graph.direct(right, _dir)];
deba@165
    96
      }
deba@165
    97
    };
deba@165
    98
alpar@209
    99
    template <typename _Item>
deba@127
   100
    class MapStorageBase {
deba@127
   101
    public:
deba@127
   102
      typedef _Item Item;
deba@127
   103
deba@127
   104
    public:
deba@127
   105
      MapStorageBase() {}
deba@127
   106
      virtual ~MapStorageBase() {}
deba@127
   107
deba@127
   108
      virtual std::string get(const Item& item) = 0;
deba@127
   109
      virtual void sort(std::vector<Item>&) = 0;
deba@127
   110
    };
deba@127
   111
alpar@209
   112
    template <typename _Item, typename _Map,
alpar@209
   113
              typename _Converter = DefaultConverter<typename _Map::Value> >
deba@127
   114
    class MapStorage : public MapStorageBase<_Item> {
deba@127
   115
    public:
deba@127
   116
      typedef _Map Map;
deba@127
   117
      typedef _Converter Converter;
deba@127
   118
      typedef _Item Item;
alpar@209
   119
deba@127
   120
    private:
deba@127
   121
      const Map& _map;
deba@127
   122
      Converter _converter;
deba@127
   123
deba@127
   124
    public:
alpar@209
   125
      MapStorage(const Map& map, const Converter& converter = Converter())
alpar@209
   126
        : _map(map), _converter(converter) {}
deba@127
   127
      virtual ~MapStorage() {}
deba@127
   128
deba@127
   129
      virtual std::string get(const Item& item) {
alpar@209
   130
        return _converter(_map[item]);
deba@127
   131
      }
deba@127
   132
      virtual void sort(std::vector<Item>& items) {
alpar@209
   133
        MapLess<Map> less(_map);
alpar@209
   134
        std::sort(items.begin(), items.end(), less);
deba@127
   135
      }
deba@127
   136
    };
deba@127
   137
alpar@209
   138
    template <typename _Graph, bool _dir, typename _Map,
alpar@209
   139
              typename _Converter = DefaultConverter<typename _Map::Value> >
deba@165
   140
    class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
deba@165
   141
    public:
deba@165
   142
      typedef _Map Map;
deba@165
   143
      typedef _Converter Converter;
deba@165
   144
      typedef _Graph Graph;
deba@165
   145
      typedef typename Graph::Edge Item;
deba@165
   146
      static const bool dir = _dir;
alpar@209
   147
deba@165
   148
    private:
deba@165
   149
      const Graph& _graph;
deba@165
   150
      const Map& _map;
deba@165
   151
      Converter _converter;
deba@165
   152
deba@165
   153
    public:
alpar@209
   154
      GraphArcMapStorage(const Graph& graph, const Map& map,
alpar@209
   155
                         const Converter& converter = Converter())
alpar@209
   156
        : _graph(graph), _map(map), _converter(converter) {}
deba@165
   157
      virtual ~GraphArcMapStorage() {}
deba@165
   158
deba@165
   159
      virtual std::string get(const Item& item) {
alpar@209
   160
        return _converter(_map[_graph.direct(item, dir)]);
deba@165
   161
      }
deba@165
   162
      virtual void sort(std::vector<Item>& items) {
alpar@209
   163
        GraphArcMapLess<Graph, dir, Map> less(_graph, _map);
alpar@209
   164
        std::sort(items.begin(), items.end(), less);
deba@165
   165
      }
deba@165
   166
    };
deba@165
   167
deba@127
   168
    class ValueStorageBase {
deba@127
   169
    public:
deba@127
   170
      ValueStorageBase() {}
deba@127
   171
      virtual ~ValueStorageBase() {}
deba@127
   172
alpar@209
   173
      virtual std::string get() = 0;
deba@127
   174
    };
deba@127
   175
deba@127
   176
    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
deba@127
   177
    class ValueStorage : public ValueStorageBase {
deba@127
   178
    public:
deba@127
   179
      typedef _Value Value;
deba@127
   180
      typedef _Converter Converter;
deba@127
   181
deba@127
   182
    private:
deba@127
   183
      const Value& _value;
deba@127
   184
      Converter _converter;
deba@127
   185
deba@127
   186
    public:
deba@127
   187
      ValueStorage(const Value& value, const Converter& converter = Converter())
kpeter@212
   188
        : _value(value), _converter(converter) {}
deba@127
   189
deba@127
   190
      virtual std::string get() {
alpar@209
   191
        return _converter(_value);
deba@127
   192
      }
deba@127
   193
    };
deba@127
   194
deba@127
   195
    template <typename Value>
deba@127
   196
    struct MapLookUpConverter {
deba@127
   197
      const std::map<Value, std::string>& _map;
alpar@209
   198
alpar@209
   199
      MapLookUpConverter(const std::map<Value, std::string>& map)
alpar@209
   200
        : _map(map) {}
alpar@209
   201
deba@127
   202
      std::string operator()(const Value& str) {
alpar@209
   203
        typename std::map<Value, std::string>::const_iterator it =
alpar@209
   204
          _map.find(str);
alpar@209
   205
        if (it == _map.end()) {
deba@290
   206
          throw FormatError("Item not found");
alpar@209
   207
        }
alpar@209
   208
        return it->second;
deba@127
   209
      }
deba@127
   210
    };
deba@127
   211
deba@165
   212
    template <typename Graph>
deba@165
   213
    struct GraphArcLookUpConverter {
deba@165
   214
      const Graph& _graph;
deba@165
   215
      const std::map<typename Graph::Edge, std::string>& _map;
alpar@209
   216
alpar@209
   217
      GraphArcLookUpConverter(const Graph& graph,
alpar@209
   218
                              const std::map<typename Graph::Edge,
alpar@209
   219
                                             std::string>& map)
alpar@209
   220
        : _graph(graph), _map(map) {}
alpar@209
   221
deba@165
   222
      std::string operator()(const typename Graph::Arc& val) {
alpar@209
   223
        typename std::map<typename Graph::Edge, std::string>
alpar@209
   224
          ::const_iterator it = _map.find(val);
alpar@209
   225
        if (it == _map.end()) {
deba@290
   226
          throw FormatError("Item not found");
alpar@209
   227
        }
alpar@209
   228
        return (_graph.direction(val) ? '+' : '-') + it->second;
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
    }
deba@127
   236
deba@197
   237
    inline bool isEscaped(char c) {
alpar@209
   238
      return c == '\\' || c == '\"' || c == '\'' ||
alpar@209
   239
        c == '\a' || c == '\b';
deba@127
   240
    }
deba@127
   241
deba@197
   242
    inline static void writeEscape(std::ostream& os, char c) {
deba@127
   243
      switch (c) {
deba@127
   244
      case '\\':
alpar@209
   245
        os << "\\\\";
alpar@209
   246
        return;
deba@127
   247
      case '\"':
alpar@209
   248
        os << "\\\"";
alpar@209
   249
        return;
deba@127
   250
      case '\a':
alpar@209
   251
        os << "\\a";
alpar@209
   252
        return;
deba@127
   253
      case '\b':
alpar@209
   254
        os << "\\b";
alpar@209
   255
        return;
deba@127
   256
      case '\f':
alpar@209
   257
        os << "\\f";
alpar@209
   258
        return;
deba@127
   259
      case '\r':
alpar@209
   260
        os << "\\r";
alpar@209
   261
        return;
deba@127
   262
      case '\n':
alpar@209
   263
        os << "\\n";
alpar@209
   264
        return;
deba@127
   265
      case '\t':
alpar@209
   266
        os << "\\t";
alpar@209
   267
        return;
deba@127
   268
      case '\v':
alpar@209
   269
        os << "\\v";
alpar@209
   270
        return;
deba@127
   271
      default:
alpar@209
   272
        if (c < 0x20) {
alpar@209
   273
          std::ios::fmtflags flags = os.flags();
alpar@209
   274
          os << '\\' << std::oct << static_cast<int>(c);
alpar@209
   275
          os.flags(flags);
alpar@209
   276
        } else {
alpar@209
   277
          os << c;
alpar@209
   278
        }
alpar@209
   279
        return;
alpar@209
   280
      }
deba@127
   281
    }
deba@127
   282
deba@197
   283
    inline bool requireEscape(const std::string& str) {
alpar@156
   284
      if (str.empty() || str[0] == '@') return true;
deba@127
   285
      std::istringstream is(str);
deba@127
   286
      char c;
deba@127
   287
      while (is.get(c)) {
alpar@209
   288
        if (isWhiteSpace(c) || isEscaped(c)) {
alpar@209
   289
          return true;
alpar@209
   290
        }
deba@127
   291
      }
deba@127
   292
      return false;
deba@127
   293
    }
alpar@209
   294
deba@197
   295
    inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
deba@127
   296
deba@127
   297
      if (requireEscape(str)) {
alpar@209
   298
        os << '\"';
alpar@209
   299
        for (std::string::const_iterator it = str.begin();
alpar@209
   300
             it != str.end(); ++it) {
alpar@209
   301
          writeEscape(os, *it);
alpar@209
   302
        }
alpar@209
   303
        os << '\"';
deba@127
   304
      } else {
alpar@209
   305
        os << str;
deba@127
   306
      }
deba@127
   307
      return os;
deba@127
   308
    }
deba@127
   309
deba@248
   310
    class Section {
deba@248
   311
    public:
deba@248
   312
      virtual ~Section() {}
deba@248
   313
      virtual void process(std::ostream& os) = 0;
deba@248
   314
    };
deba@248
   315
deba@248
   316
    template <typename Functor>
deba@248
   317
    class LineSection : public Section {
deba@248
   318
    private:
deba@248
   319
deba@248
   320
      Functor _functor;
deba@248
   321
deba@248
   322
    public:
deba@248
   323
deba@248
   324
      LineSection(const Functor& functor) : _functor(functor) {}
deba@248
   325
      virtual ~LineSection() {}
deba@248
   326
deba@248
   327
      virtual void process(std::ostream& os) {
deba@248
   328
        std::string line;
deba@248
   329
        while (!(line = _functor()).empty()) os << line << std::endl;
deba@248
   330
      }
deba@248
   331
    };
deba@248
   332
deba@248
   333
    template <typename Functor>
deba@248
   334
    class StreamSection : public Section {
deba@248
   335
    private:
deba@248
   336
deba@248
   337
      Functor _functor;
deba@248
   338
deba@248
   339
    public:
deba@248
   340
deba@248
   341
      StreamSection(const Functor& functor) : _functor(functor) {}
deba@248
   342
      virtual ~StreamSection() {}
deba@248
   343
deba@248
   344
      virtual void process(std::ostream& os) {
deba@248
   345
        _functor(os);
deba@248
   346
      }
deba@248
   347
    };
deba@248
   348
deba@127
   349
  }
deba@190
   350
deba@190
   351
  template <typename Digraph>
deba@190
   352
  class DigraphWriter;
deba@190
   353
deba@190
   354
  template <typename Digraph>
kpeter@293
   355
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   356
                                       std::ostream& os = std::cout);
deba@190
   357
deba@190
   358
  template <typename Digraph>
kpeter@293
   359
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   360
                                       const std::string& fn);
deba@190
   361
deba@190
   362
  template <typename Digraph>
kpeter@293
   363
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   364
                                       const char *fn);
alpar@209
   365
alpar@156
   366
  /// \ingroup lemon_io
alpar@209
   367
  ///
kpeter@192
   368
  /// \brief \ref lgf-format "LGF" writer for directed graphs
alpar@156
   369
  ///
alpar@156
   370
  /// This utility writes an \ref lgf-format "LGF" file.
alpar@156
   371
  ///
alpar@156
   372
  /// The writing method does a batch processing. The user creates a
alpar@156
   373
  /// writer object, then various writing rules can be added to the
alpar@156
   374
  /// writer, and eventually the writing is executed with the \c run()
alpar@156
   375
  /// member function. A map writing rule can be added to the writer
alpar@156
   376
  /// with the \c nodeMap() or \c arcMap() members. An optional
deba@163
   377
  /// converter parameter can also be added as a standard functor
kpeter@192
   378
  /// converting from the value type of the map to \c std::string. If it
kpeter@192
   379
  /// is set, it will determine how the value type of the map is written to
deba@163
   380
  /// the output stream. If the functor is not set, then a default
deba@163
   381
  /// conversion will be used. The \c attribute(), \c node() and \c
deba@163
   382
  /// arc() functions are used to add attribute writing rules.
alpar@156
   383
  ///
alpar@156
   384
  ///\code
kpeter@293
   385
  /// DigraphWriter<Digraph>(digraph, std::cout).
kpeter@192
   386
  ///   nodeMap("coordinates", coord_map).
kpeter@192
   387
  ///   nodeMap("size", size).
kpeter@192
   388
  ///   nodeMap("title", title).
kpeter@192
   389
  ///   arcMap("capacity", cap_map).
kpeter@192
   390
  ///   node("source", src).
kpeter@192
   391
  ///   node("target", trg).
kpeter@192
   392
  ///   attribute("caption", caption).
kpeter@192
   393
  ///   run();
alpar@156
   394
  ///\endcode
alpar@156
   395
  ///
alpar@156
   396
  ///
alpar@156
   397
  /// By default, the writer does not write additional captions to the
alpar@156
   398
  /// sections, but they can be give as an optional parameter of
alpar@156
   399
  /// the \c nodes(), \c arcs() or \c
alpar@156
   400
  /// attributes() functions.
alpar@156
   401
  ///
alpar@156
   402
  /// The \c skipNodes() and \c skipArcs() functions forbid the
deba@163
   403
  /// writing of the sections. If two arc sections should be written
deba@163
   404
  /// to the output, it can be done in two passes, the first pass
deba@163
   405
  /// writes the node section and the first arc section, then the
deba@163
   406
  /// second pass skips the node section and writes just the arc
deba@163
   407
  /// section to the stream. The output stream can be retrieved with
deba@163
   408
  /// the \c ostream() function, hence the second pass can append its
deba@163
   409
  /// output to the output of the first pass.
deba@127
   410
  template <typename _Digraph>
deba@127
   411
  class DigraphWriter {
deba@127
   412
  public:
deba@127
   413
deba@127
   414
    typedef _Digraph Digraph;
deba@148
   415
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@209
   416
deba@127
   417
  private:
deba@127
   418
deba@127
   419
deba@127
   420
    std::ostream* _os;
deba@127
   421
    bool local_os;
deba@127
   422
deba@190
   423
    const Digraph& _digraph;
deba@127
   424
deba@127
   425
    std::string _nodes_caption;
deba@127
   426
    std::string _arcs_caption;
deba@127
   427
    std::string _attributes_caption;
alpar@209
   428
deba@127
   429
    typedef std::map<Node, std::string> NodeIndex;
deba@127
   430
    NodeIndex _node_index;
deba@127
   431
    typedef std::map<Arc, std::string> ArcIndex;
deba@127
   432
    ArcIndex _arc_index;
deba@127
   433
alpar@209
   434
    typedef std::vector<std::pair<std::string,
alpar@209
   435
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
alpar@209
   436
    NodeMaps _node_maps;
deba@127
   437
alpar@209
   438
    typedef std::vector<std::pair<std::string,
deba@127
   439
      _writer_bits::MapStorageBase<Arc>* > >ArcMaps;
deba@127
   440
    ArcMaps _arc_maps;
deba@127
   441
alpar@209
   442
    typedef std::vector<std::pair<std::string,
deba@127
   443
      _writer_bits::ValueStorageBase*> > Attributes;
deba@127
   444
    Attributes _attributes;
deba@127
   445
deba@127
   446
    bool _skip_nodes;
deba@127
   447
    bool _skip_arcs;
deba@127
   448
deba@127
   449
  public:
deba@127
   450
alpar@156
   451
    /// \brief Constructor
alpar@156
   452
    ///
alpar@156
   453
    /// Construct a directed graph writer, which writes to the given
alpar@156
   454
    /// output stream.
kpeter@293
   455
    DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout)
kpeter@293
   456
      : _os(&os), local_os(false), _digraph(digraph),
alpar@209
   457
        _skip_nodes(false), _skip_arcs(false) {}
deba@127
   458
alpar@156
   459
    /// \brief Constructor
alpar@156
   460
    ///
alpar@156
   461
    /// Construct a directed graph writer, which writes to the given
alpar@156
   462
    /// output file.
kpeter@293
   463
    DigraphWriter(const Digraph& digraph, const std::string& fn)
deba@127
   464
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
deba@290
   465
        _skip_nodes(false), _skip_arcs(false) {
kpeter@291
   466
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
   467
    }
deba@127
   468
alpar@156
   469
    /// \brief Constructor
alpar@156
   470
    ///
alpar@156
   471
    /// Construct a directed graph writer, which writes to the given
alpar@156
   472
    /// output file.
kpeter@293
   473
    DigraphWriter(const Digraph& digraph, const char* fn)
deba@127
   474
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
deba@290
   475
        _skip_nodes(false), _skip_arcs(false) {
kpeter@291
   476
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
   477
    }
deba@127
   478
alpar@156
   479
    /// \brief Destructor
deba@127
   480
    ~DigraphWriter() {
alpar@209
   481
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   482
           it != _node_maps.end(); ++it) {
alpar@209
   483
        delete it->second;
deba@127
   484
      }
deba@127
   485
alpar@209
   486
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   487
           it != _arc_maps.end(); ++it) {
alpar@209
   488
        delete it->second;
deba@127
   489
      }
deba@127
   490
alpar@209
   491
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
   492
           it != _attributes.end(); ++it) {
alpar@209
   493
        delete it->second;
deba@127
   494
      }
deba@127
   495
deba@127
   496
      if (local_os) {
alpar@209
   497
        delete _os;
deba@127
   498
      }
deba@127
   499
    }
deba@127
   500
deba@127
   501
  private:
deba@190
   502
kpeter@293
   503
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   504
                                                  std::ostream& os);
kpeter@293
   505
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   506
                                                  const std::string& fn);
kpeter@293
   507
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   508
                                                  const char *fn);
deba@190
   509
alpar@209
   510
    DigraphWriter(DigraphWriter& other)
deba@190
   511
      : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
alpar@209
   512
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
deba@190
   513
deba@190
   514
      other._os = 0;
deba@190
   515
      other.local_os = false;
deba@190
   516
deba@190
   517
      _node_index.swap(other._node_index);
deba@190
   518
      _arc_index.swap(other._arc_index);
deba@190
   519
deba@190
   520
      _node_maps.swap(other._node_maps);
deba@190
   521
      _arc_maps.swap(other._arc_maps);
deba@190
   522
      _attributes.swap(other._attributes);
deba@190
   523
deba@190
   524
      _nodes_caption = other._nodes_caption;
deba@190
   525
      _arcs_caption = other._arcs_caption;
deba@190
   526
      _attributes_caption = other._attributes_caption;
deba@190
   527
    }
alpar@209
   528
deba@127
   529
    DigraphWriter& operator=(const DigraphWriter&);
deba@127
   530
deba@127
   531
  public:
deba@127
   532
alpar@156
   533
    /// \name Writing rules
alpar@156
   534
    /// @{
alpar@209
   535
kpeter@192
   536
    /// \brief Node map writing rule
alpar@156
   537
    ///
kpeter@192
   538
    /// Add a node map writing rule to the writer.
deba@127
   539
    template <typename Map>
deba@127
   540
    DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
deba@127
   541
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   542
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
   543
        new _writer_bits::MapStorage<Node, Map>(map);
deba@127
   544
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   545
      return *this;
deba@127
   546
    }
deba@127
   547
alpar@156
   548
    /// \brief Node map writing rule
alpar@156
   549
    ///
alpar@156
   550
    /// Add a node map writing rule with specialized converter to the
alpar@156
   551
    /// writer.
deba@127
   552
    template <typename Map, typename Converter>
alpar@209
   553
    DigraphWriter& nodeMap(const std::string& caption, const Map& map,
alpar@209
   554
                           const Converter& converter = Converter()) {
deba@127
   555
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   556
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
   557
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@127
   558
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   559
      return *this;
deba@127
   560
    }
deba@127
   561
alpar@156
   562
    /// \brief Arc map writing rule
alpar@156
   563
    ///
alpar@156
   564
    /// Add an arc map writing rule to the writer.
deba@127
   565
    template <typename Map>
deba@127
   566
    DigraphWriter& arcMap(const std::string& caption, const Map& map) {
deba@127
   567
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
   568
      _writer_bits::MapStorageBase<Arc>* storage =
alpar@209
   569
        new _writer_bits::MapStorage<Arc, Map>(map);
deba@127
   570
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   571
      return *this;
deba@127
   572
    }
deba@127
   573
alpar@156
   574
    /// \brief Arc map writing rule
alpar@156
   575
    ///
alpar@156
   576
    /// Add an arc map writing rule with specialized converter to the
alpar@156
   577
    /// writer.
deba@127
   578
    template <typename Map, typename Converter>
alpar@209
   579
    DigraphWriter& arcMap(const std::string& caption, const Map& map,
alpar@209
   580
                          const Converter& converter = Converter()) {
deba@127
   581
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
   582
      _writer_bits::MapStorageBase<Arc>* storage =
alpar@209
   583
        new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
deba@127
   584
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   585
      return *this;
deba@127
   586
    }
deba@127
   587
alpar@156
   588
    /// \brief Attribute writing rule
alpar@156
   589
    ///
alpar@156
   590
    /// Add an attribute writing rule to the writer.
deba@127
   591
    template <typename Value>
deba@127
   592
    DigraphWriter& attribute(const std::string& caption, const Value& value) {
alpar@209
   593
      _writer_bits::ValueStorageBase* storage =
alpar@209
   594
        new _writer_bits::ValueStorage<Value>(value);
deba@127
   595
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   596
      return *this;
deba@127
   597
    }
deba@127
   598
alpar@156
   599
    /// \brief Attribute writing rule
alpar@156
   600
    ///
alpar@156
   601
    /// Add an attribute writing rule with specialized converter to the
alpar@156
   602
    /// writer.
deba@127
   603
    template <typename Value, typename Converter>
alpar@209
   604
    DigraphWriter& attribute(const std::string& caption, const Value& value,
alpar@209
   605
                             const Converter& converter = Converter()) {
alpar@209
   606
      _writer_bits::ValueStorageBase* storage =
alpar@209
   607
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
deba@127
   608
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   609
      return *this;
deba@127
   610
    }
deba@127
   611
alpar@156
   612
    /// \brief Node writing rule
alpar@156
   613
    ///
alpar@156
   614
    /// Add a node writing rule to the writer.
deba@127
   615
    DigraphWriter& node(const std::string& caption, const Node& node) {
deba@127
   616
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
deba@127
   617
      Converter converter(_node_index);
alpar@209
   618
      _writer_bits::ValueStorageBase* storage =
alpar@209
   619
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
deba@127
   620
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   621
      return *this;
deba@127
   622
    }
deba@127
   623
alpar@156
   624
    /// \brief Arc writing rule
alpar@156
   625
    ///
alpar@156
   626
    /// Add an arc writing rule to writer.
deba@127
   627
    DigraphWriter& arc(const std::string& caption, const Arc& arc) {
deba@127
   628
      typedef _writer_bits::MapLookUpConverter<Arc> Converter;
deba@127
   629
      Converter converter(_arc_index);
alpar@209
   630
      _writer_bits::ValueStorageBase* storage =
alpar@209
   631
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@127
   632
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   633
      return *this;
deba@127
   634
    }
deba@127
   635
kpeter@192
   636
    /// \name Section captions
alpar@156
   637
    /// @{
alpar@156
   638
kpeter@192
   639
    /// \brief Add an additional caption to the \c \@nodes section
alpar@156
   640
    ///
kpeter@192
   641
    /// Add an additional caption to the \c \@nodes section.
deba@127
   642
    DigraphWriter& nodes(const std::string& caption) {
deba@127
   643
      _nodes_caption = caption;
deba@127
   644
      return *this;
deba@127
   645
    }
deba@127
   646
kpeter@192
   647
    /// \brief Add an additional caption to the \c \@arcs section
alpar@156
   648
    ///
kpeter@192
   649
    /// Add an additional caption to the \c \@arcs section.
deba@127
   650
    DigraphWriter& arcs(const std::string& caption) {
deba@127
   651
      _arcs_caption = caption;
deba@127
   652
      return *this;
deba@127
   653
    }
deba@127
   654
kpeter@192
   655
    /// \brief Add an additional caption to the \c \@attributes section
alpar@156
   656
    ///
kpeter@192
   657
    /// Add an additional caption to the \c \@attributes section.
deba@127
   658
    DigraphWriter& attributes(const std::string& caption) {
deba@127
   659
      _attributes_caption = caption;
deba@127
   660
      return *this;
deba@127
   661
    }
deba@127
   662
alpar@156
   663
    /// \name Skipping section
alpar@156
   664
    /// @{
alpar@156
   665
alpar@156
   666
    /// \brief Skip writing the node set
alpar@156
   667
    ///
kpeter@192
   668
    /// The \c \@nodes section will not be written to the stream.
deba@127
   669
    DigraphWriter& skipNodes() {
deba@127
   670
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
deba@185
   671
      _skip_nodes = true;
deba@127
   672
      return *this;
deba@127
   673
    }
deba@127
   674
alpar@156
   675
    /// \brief Skip writing arc set
alpar@156
   676
    ///
kpeter@192
   677
    /// The \c \@arcs section will not be written to the stream.
deba@127
   678
    DigraphWriter& skipArcs() {
deba@127
   679
      LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
deba@185
   680
      _skip_arcs = true;
deba@127
   681
      return *this;
deba@127
   682
    }
deba@127
   683
alpar@156
   684
    /// @}
alpar@156
   685
deba@127
   686
  private:
deba@127
   687
deba@127
   688
    void writeNodes() {
deba@127
   689
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@127
   690
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   691
           it != _node_maps.end(); ++it) {
deba@127
   692
        if (it->first == "label") {
alpar@209
   693
          label = it->second;
alpar@209
   694
          break;
alpar@209
   695
        }
deba@127
   696
      }
deba@127
   697
deba@127
   698
      *_os << "@nodes";
deba@127
   699
      if (!_nodes_caption.empty()) {
alpar@209
   700
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
deba@127
   701
      }
deba@127
   702
      *_os << std::endl;
deba@127
   703
deba@127
   704
      if (label == 0) {
alpar@209
   705
        *_os << "label" << '\t';
deba@127
   706
      }
deba@127
   707
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   708
           it != _node_maps.end(); ++it) {
alpar@209
   709
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@127
   710
      }
deba@127
   711
      *_os << std::endl;
deba@127
   712
deba@127
   713
      std::vector<Node> nodes;
deba@127
   714
      for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   715
        nodes.push_back(n);
deba@127
   716
      }
alpar@209
   717
deba@127
   718
      if (label == 0) {
alpar@209
   719
        IdMap<Digraph, Node> id_map(_digraph);
alpar@209
   720
        _writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map);
alpar@209
   721
        std::sort(nodes.begin(), nodes.end(), id_less);
deba@127
   722
      } else {
alpar@209
   723
        label->sort(nodes);
deba@127
   724
      }
deba@127
   725
deba@127
   726
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
alpar@209
   727
        Node n = nodes[i];
alpar@209
   728
        if (label == 0) {
alpar@209
   729
          std::ostringstream os;
alpar@209
   730
          os << _digraph.id(n);
alpar@209
   731
          _writer_bits::writeToken(*_os, os.str());
alpar@209
   732
          *_os << '\t';
alpar@209
   733
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
   734
        }
alpar@209
   735
        for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   736
             it != _node_maps.end(); ++it) {
alpar@209
   737
          std::string value = it->second->get(n);
alpar@209
   738
          _writer_bits::writeToken(*_os, value);
alpar@209
   739
          if (it->first == "label") {
alpar@209
   740
            _node_index.insert(std::make_pair(n, value));
alpar@209
   741
          }
alpar@209
   742
          *_os << '\t';
alpar@209
   743
        }
alpar@209
   744
        *_os << std::endl;
deba@127
   745
      }
deba@127
   746
    }
deba@127
   747
deba@185
   748
    void createNodeIndex() {
deba@185
   749
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@185
   750
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   751
           it != _node_maps.end(); ++it) {
deba@185
   752
        if (it->first == "label") {
alpar@209
   753
          label = it->second;
alpar@209
   754
          break;
alpar@209
   755
        }
deba@185
   756
      }
deba@185
   757
deba@185
   758
      if (label == 0) {
alpar@209
   759
        for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   760
          std::ostringstream os;
alpar@209
   761
          os << _digraph.id(n);
alpar@209
   762
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
   763
        }
deba@185
   764
      } else {
alpar@209
   765
        for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   766
          std::string value = label->get(n);
alpar@209
   767
          _node_index.insert(std::make_pair(n, value));
alpar@209
   768
        }
deba@185
   769
      }
deba@185
   770
    }
deba@185
   771
deba@127
   772
    void writeArcs() {
deba@127
   773
      _writer_bits::MapStorageBase<Arc>* label = 0;
deba@127
   774
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   775
           it != _arc_maps.end(); ++it) {
deba@127
   776
        if (it->first == "label") {
alpar@209
   777
          label = it->second;
alpar@209
   778
          break;
alpar@209
   779
        }
deba@127
   780
      }
deba@127
   781
deba@127
   782
      *_os << "@arcs";
deba@127
   783
      if (!_arcs_caption.empty()) {
alpar@209
   784
        _writer_bits::writeToken(*_os << ' ', _arcs_caption);
deba@127
   785
      }
deba@127
   786
      *_os << std::endl;
deba@127
   787
deba@127
   788
      *_os << '\t' << '\t';
deba@127
   789
      if (label == 0) {
alpar@209
   790
        *_os << "label" << '\t';
deba@127
   791
      }
deba@127
   792
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   793
           it != _arc_maps.end(); ++it) {
alpar@209
   794
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@127
   795
      }
deba@127
   796
      *_os << std::endl;
deba@127
   797
deba@127
   798
      std::vector<Arc> arcs;
deba@127
   799
      for (ArcIt n(_digraph); n != INVALID; ++n) {
alpar@209
   800
        arcs.push_back(n);
deba@127
   801
      }
alpar@209
   802
deba@127
   803
      if (label == 0) {
alpar@209
   804
        IdMap<Digraph, Arc> id_map(_digraph);
alpar@209
   805
        _writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map);
alpar@209
   806
        std::sort(arcs.begin(), arcs.end(), id_less);
deba@127
   807
      } else {
alpar@209
   808
        label->sort(arcs);
deba@127
   809
      }
deba@127
   810
deba@127
   811
      for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
alpar@209
   812
        Arc a = arcs[i];
alpar@209
   813
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
   814
                                 find(_digraph.source(a))->second);
alpar@209
   815
        *_os << '\t';
alpar@209
   816
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
   817
                                 find(_digraph.target(a))->second);
alpar@209
   818
        *_os << '\t';
alpar@209
   819
        if (label == 0) {
alpar@209
   820
          std::ostringstream os;
alpar@209
   821
          os << _digraph.id(a);
alpar@209
   822
          _writer_bits::writeToken(*_os, os.str());
alpar@209
   823
          *_os << '\t';
alpar@209
   824
          _arc_index.insert(std::make_pair(a, os.str()));
alpar@209
   825
        }
alpar@209
   826
        for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   827
             it != _arc_maps.end(); ++it) {
alpar@209
   828
          std::string value = it->second->get(a);
alpar@209
   829
          _writer_bits::writeToken(*_os, value);
alpar@209
   830
          if (it->first == "label") {
alpar@209
   831
            _arc_index.insert(std::make_pair(a, value));
alpar@209
   832
          }
alpar@209
   833
          *_os << '\t';
alpar@209
   834
        }
alpar@209
   835
        *_os << std::endl;
deba@127
   836
      }
deba@127
   837
    }
deba@127
   838
deba@185
   839
    void createArcIndex() {
deba@185
   840
      _writer_bits::MapStorageBase<Arc>* label = 0;
deba@185
   841
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   842
           it != _arc_maps.end(); ++it) {
deba@185
   843
        if (it->first == "label") {
alpar@209
   844
          label = it->second;
alpar@209
   845
          break;
alpar@209
   846
        }
deba@185
   847
      }
deba@185
   848
deba@185
   849
      if (label == 0) {
alpar@209
   850
        for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   851
          std::ostringstream os;
alpar@209
   852
          os << _digraph.id(a);
alpar@209
   853
          _arc_index.insert(std::make_pair(a, os.str()));
alpar@209
   854
        }
deba@185
   855
      } else {
alpar@209
   856
        for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   857
          std::string value = label->get(a);
alpar@209
   858
          _arc_index.insert(std::make_pair(a, value));
alpar@209
   859
        }
deba@185
   860
      }
deba@185
   861
    }
deba@185
   862
deba@127
   863
    void writeAttributes() {
deba@127
   864
      if (_attributes.empty()) return;
deba@127
   865
      *_os << "@attributes";
deba@127
   866
      if (!_attributes_caption.empty()) {
alpar@209
   867
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
deba@127
   868
      }
deba@127
   869
      *_os << std::endl;
deba@127
   870
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
   871
           it != _attributes.end(); ++it) {
alpar@209
   872
        _writer_bits::writeToken(*_os, it->first) << ' ';
alpar@209
   873
        _writer_bits::writeToken(*_os, it->second->get());
alpar@209
   874
        *_os << std::endl;
deba@127
   875
      }
deba@127
   876
    }
alpar@209
   877
deba@127
   878
  public:
alpar@209
   879
alpar@209
   880
    /// \name Execution of the writer
alpar@156
   881
    /// @{
alpar@156
   882
alpar@156
   883
    /// \brief Start the batch processing
alpar@156
   884
    ///
kpeter@192
   885
    /// This function starts the batch processing.
deba@127
   886
    void run() {
deba@127
   887
      if (!_skip_nodes) {
alpar@209
   888
        writeNodes();
deba@185
   889
      } else {
alpar@209
   890
        createNodeIndex();
deba@127
   891
      }
alpar@209
   892
      if (!_skip_arcs) {
alpar@209
   893
        writeArcs();
deba@185
   894
      } else {
alpar@209
   895
        createArcIndex();
deba@127
   896
      }
deba@127
   897
      writeAttributes();
deba@127
   898
    }
deba@127
   899
kpeter@192
   900
    /// \brief Give back the stream of the writer
alpar@156
   901
    ///
kpeter@192
   902
    /// Give back the stream of the writer.
alpar@156
   903
    std::ostream& ostream() {
deba@127
   904
      return *_os;
deba@127
   905
    }
alpar@156
   906
alpar@156
   907
    /// @}
deba@127
   908
  };
deba@127
   909
kpeter@192
   910
  /// \brief Return a \ref DigraphWriter class
alpar@209
   911
  ///
kpeter@192
   912
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   913
  /// \relates DigraphWriter
deba@127
   914
  template <typename Digraph>
kpeter@293
   915
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   916
                                       std::ostream& os = std::cout) {
kpeter@293
   917
    DigraphWriter<Digraph> tmp(digraph, os);
deba@163
   918
    return tmp;
deba@127
   919
  }
deba@127
   920
kpeter@192
   921
  /// \brief Return a \ref DigraphWriter class
alpar@209
   922
  ///
kpeter@192
   923
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   924
  /// \relates DigraphWriter
deba@127
   925
  template <typename Digraph>
kpeter@293
   926
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   927
                                       const std::string& fn) {
kpeter@293
   928
    DigraphWriter<Digraph> tmp(digraph, fn);
deba@163
   929
    return tmp;
deba@127
   930
  }
deba@127
   931
kpeter@192
   932
  /// \brief Return a \ref DigraphWriter class
alpar@209
   933
  ///
kpeter@192
   934
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   935
  /// \relates DigraphWriter
deba@127
   936
  template <typename Digraph>
kpeter@293
   937
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   938
                                       const char* fn) {
kpeter@293
   939
    DigraphWriter<Digraph> tmp(digraph, fn);
deba@163
   940
    return tmp;
deba@127
   941
  }
deba@165
   942
deba@190
   943
  template <typename Graph>
deba@190
   944
  class GraphWriter;
deba@190
   945
deba@190
   946
  template <typename Graph>
kpeter@293
   947
  GraphWriter<Graph> graphWriter(const Graph& graph,
kpeter@293
   948
                                 std::ostream& os = std::cout);
deba@190
   949
deba@190
   950
  template <typename Graph>
kpeter@293
   951
  GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn);
deba@190
   952
deba@190
   953
  template <typename Graph>
kpeter@293
   954
  GraphWriter<Graph> graphWriter(const Graph& graph, const char *fn);
deba@190
   955
deba@165
   956
  /// \ingroup lemon_io
alpar@209
   957
  ///
kpeter@192
   958
  /// \brief \ref lgf-format "LGF" writer for directed graphs
deba@165
   959
  ///
deba@165
   960
  /// This utility writes an \ref lgf-format "LGF" file.
kpeter@192
   961
  ///
kpeter@192
   962
  /// It can be used almost the same way as \c DigraphWriter.
kpeter@192
   963
  /// The only difference is that this class can handle edges and
kpeter@192
   964
  /// edge maps as well as arcs and arc maps.
deba@201
   965
  ///
deba@201
   966
  /// The arc maps are written into the file as two columns, the
deba@201
   967
  /// caption of the columns are the name of the map prefixed with \c
deba@201
   968
  /// '+' and \c '-'. The arcs are written into the \c \@attributes
deba@201
   969
  /// section as a \c '+' or a \c '-' prefix (depends on the direction
deba@201
   970
  /// of the arc) and the label of corresponding edge.
deba@165
   971
  template <typename _Graph>
deba@165
   972
  class GraphWriter {
deba@165
   973
  public:
deba@165
   974
deba@165
   975
    typedef _Graph Graph;
deba@165
   976
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
alpar@209
   977
deba@165
   978
  private:
deba@165
   979
deba@165
   980
deba@165
   981
    std::ostream* _os;
deba@165
   982
    bool local_os;
deba@165
   983
deba@237
   984
    const Graph& _graph;
deba@165
   985
deba@165
   986
    std::string _nodes_caption;
deba@165
   987
    std::string _edges_caption;
deba@165
   988
    std::string _attributes_caption;
alpar@209
   989
deba@165
   990
    typedef std::map<Node, std::string> NodeIndex;
deba@165
   991
    NodeIndex _node_index;
deba@165
   992
    typedef std::map<Edge, std::string> EdgeIndex;
deba@165
   993
    EdgeIndex _edge_index;
deba@165
   994
alpar@209
   995
    typedef std::vector<std::pair<std::string,
alpar@209
   996
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
alpar@209
   997
    NodeMaps _node_maps;
deba@165
   998
alpar@209
   999
    typedef std::vector<std::pair<std::string,
deba@165
  1000
      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
deba@165
  1001
    EdgeMaps _edge_maps;
deba@165
  1002
alpar@209
  1003
    typedef std::vector<std::pair<std::string,
deba@165
  1004
      _writer_bits::ValueStorageBase*> > Attributes;
deba@165
  1005
    Attributes _attributes;
deba@165
  1006
deba@165
  1007
    bool _skip_nodes;
deba@165
  1008
    bool _skip_edges;
deba@165
  1009
deba@165
  1010
  public:
deba@165
  1011
deba@165
  1012
    /// \brief Constructor
deba@165
  1013
    ///
deba@165
  1014
    /// Construct a directed graph writer, which writes to the given
deba@165
  1015
    /// output stream.
kpeter@293
  1016
    GraphWriter(const Graph& graph, std::ostream& os = std::cout)
kpeter@293
  1017
      : _os(&os), local_os(false), _graph(graph),
alpar@209
  1018
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1019
deba@165
  1020
    /// \brief Constructor
deba@165
  1021
    ///
deba@165
  1022
    /// Construct a directed graph writer, which writes to the given
deba@165
  1023
    /// output file.
kpeter@293
  1024
    GraphWriter(const Graph& graph, const std::string& fn)
deba@165
  1025
      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
deba@290
  1026
        _skip_nodes(false), _skip_edges(false) {
kpeter@291
  1027
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
  1028
    }
deba@165
  1029
deba@165
  1030
    /// \brief Constructor
deba@165
  1031
    ///
deba@165
  1032
    /// Construct a directed graph writer, which writes to the given
deba@165
  1033
    /// output file.
kpeter@293
  1034
    GraphWriter(const Graph& graph, const char* fn)
deba@165
  1035
      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
deba@290
  1036
        _skip_nodes(false), _skip_edges(false) {
kpeter@291
  1037
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
  1038
    }
deba@165
  1039
deba@165
  1040
    /// \brief Destructor
deba@165
  1041
    ~GraphWriter() {
alpar@209
  1042
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1043
           it != _node_maps.end(); ++it) {
alpar@209
  1044
        delete it->second;
deba@165
  1045
      }
deba@165
  1046
alpar@209
  1047
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1048
           it != _edge_maps.end(); ++it) {
alpar@209
  1049
        delete it->second;
deba@165
  1050
      }
deba@165
  1051
alpar@209
  1052
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1053
           it != _attributes.end(); ++it) {
alpar@209
  1054
        delete it->second;
deba@165
  1055
      }
deba@165
  1056
deba@165
  1057
      if (local_os) {
alpar@209
  1058
        delete _os;
deba@165
  1059
      }
deba@165
  1060
    }
alpar@209
  1061
deba@190
  1062
  private:
deba@165
  1063
kpeter@293
  1064
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1065
                                            std::ostream& os);
kpeter@293
  1066
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1067
                                            const std::string& fn);
kpeter@293
  1068
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1069
                                            const char *fn);
deba@190
  1070
alpar@209
  1071
    GraphWriter(GraphWriter& other)
deba@190
  1072
      : _os(other._os), local_os(other.local_os), _graph(other._graph),
alpar@209
  1073
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
deba@190
  1074
deba@190
  1075
      other._os = 0;
deba@190
  1076
      other.local_os = false;
deba@190
  1077
deba@190
  1078
      _node_index.swap(other._node_index);
deba@190
  1079
      _edge_index.swap(other._edge_index);
deba@190
  1080
deba@190
  1081
      _node_maps.swap(other._node_maps);
deba@190
  1082
      _edge_maps.swap(other._edge_maps);
deba@190
  1083
      _attributes.swap(other._attributes);
deba@190
  1084
deba@190
  1085
      _nodes_caption = other._nodes_caption;
deba@190
  1086
      _edges_caption = other._edges_caption;
deba@190
  1087
      _attributes_caption = other._attributes_caption;
deba@190
  1088
    }
deba@190
  1089
deba@165
  1090
    GraphWriter& operator=(const GraphWriter&);
deba@165
  1091
deba@165
  1092
  public:
deba@165
  1093
deba@165
  1094
    /// \name Writing rules
deba@165
  1095
    /// @{
alpar@209
  1096
kpeter@192
  1097
    /// \brief Node map writing rule
deba@165
  1098
    ///
kpeter@192
  1099
    /// Add a node map writing rule to the writer.
deba@165
  1100
    template <typename Map>
deba@165
  1101
    GraphWriter& nodeMap(const std::string& caption, const Map& map) {
deba@165
  1102
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1103
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1104
        new _writer_bits::MapStorage<Node, Map>(map);
deba@165
  1105
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1106
      return *this;
deba@165
  1107
    }
deba@165
  1108
deba@165
  1109
    /// \brief Node map writing rule
deba@165
  1110
    ///
deba@165
  1111
    /// Add a node map writing rule with specialized converter to the
deba@165
  1112
    /// writer.
deba@165
  1113
    template <typename Map, typename Converter>
alpar@209
  1114
    GraphWriter& nodeMap(const std::string& caption, const Map& map,
alpar@209
  1115
                           const Converter& converter = Converter()) {
deba@165
  1116
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1117
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1118
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@165
  1119
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1120
      return *this;
deba@165
  1121
    }
deba@165
  1122
deba@165
  1123
    /// \brief Edge map writing rule
deba@165
  1124
    ///
deba@165
  1125
    /// Add an edge map writing rule to the writer.
deba@165
  1126
    template <typename Map>
deba@165
  1127
    GraphWriter& edgeMap(const std::string& caption, const Map& map) {
deba@165
  1128
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1129
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1130
        new _writer_bits::MapStorage<Edge, Map>(map);
deba@165
  1131
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1132
      return *this;
deba@165
  1133
    }
deba@165
  1134
deba@165
  1135
    /// \brief Edge map writing rule
deba@165
  1136
    ///
deba@165
  1137
    /// Add an edge map writing rule with specialized converter to the
deba@165
  1138
    /// writer.
deba@165
  1139
    template <typename Map, typename Converter>
alpar@209
  1140
    GraphWriter& edgeMap(const std::string& caption, const Map& map,
alpar@209
  1141
                          const Converter& converter = Converter()) {
deba@165
  1142
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1143
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1144
        new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@165
  1145
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1146
      return *this;
deba@165
  1147
    }
deba@165
  1148
deba@165
  1149
    /// \brief Arc map writing rule
deba@165
  1150
    ///
deba@165
  1151
    /// Add an arc map writing rule to the writer.
deba@165
  1152
    template <typename Map>
deba@165
  1153
    GraphWriter& arcMap(const std::string& caption, const Map& map) {
deba@165
  1154
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1155
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1156
        new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@165
  1157
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1158
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1159
        new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
deba@165
  1160
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1161
      return *this;
deba@165
  1162
    }
deba@165
  1163
deba@165
  1164
    /// \brief Arc map writing rule
deba@165
  1165
    ///
deba@165
  1166
    /// Add an arc map writing rule with specialized converter to the
deba@165
  1167
    /// writer.
deba@165
  1168
    template <typename Map, typename Converter>
alpar@209
  1169
    GraphWriter& arcMap(const std::string& caption, const Map& map,
alpar@209
  1170
                          const Converter& converter = Converter()) {
deba@165
  1171
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1172
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1173
        new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter>
alpar@209
  1174
        (_graph, map, converter);
deba@165
  1175
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1176
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1177
        new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter>
alpar@209
  1178
        (_graph, map, converter);
deba@165
  1179
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1180
      return *this;
deba@165
  1181
    }
deba@165
  1182
deba@165
  1183
    /// \brief Attribute writing rule
deba@165
  1184
    ///
deba@165
  1185
    /// Add an attribute writing rule to the writer.
deba@165
  1186
    template <typename Value>
deba@165
  1187
    GraphWriter& attribute(const std::string& caption, const Value& value) {
alpar@209
  1188
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1189
        new _writer_bits::ValueStorage<Value>(value);
deba@165
  1190
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1191
      return *this;
deba@165
  1192
    }
deba@165
  1193
deba@165
  1194
    /// \brief Attribute writing rule
deba@165
  1195
    ///
deba@165
  1196
    /// Add an attribute writing rule with specialized converter to the
deba@165
  1197
    /// writer.
deba@165
  1198
    template <typename Value, typename Converter>
alpar@209
  1199
    GraphWriter& attribute(const std::string& caption, const Value& value,
alpar@209
  1200
                             const Converter& converter = Converter()) {
alpar@209
  1201
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1202
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
deba@165
  1203
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1204
      return *this;
deba@165
  1205
    }
deba@165
  1206
deba@165
  1207
    /// \brief Node writing rule
deba@165
  1208
    ///
deba@165
  1209
    /// Add a node writing rule to the writer.
deba@165
  1210
    GraphWriter& node(const std::string& caption, const Node& node) {
deba@165
  1211
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
deba@165
  1212
      Converter converter(_node_index);
alpar@209
  1213
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1214
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
deba@165
  1215
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1216
      return *this;
deba@165
  1217
    }
deba@165
  1218
deba@165
  1219
    /// \brief Edge writing rule
deba@165
  1220
    ///
deba@165
  1221
    /// Add an edge writing rule to writer.
deba@165
  1222
    GraphWriter& edge(const std::string& caption, const Edge& edge) {
deba@165
  1223
      typedef _writer_bits::MapLookUpConverter<Edge> Converter;
deba@165
  1224
      Converter converter(_edge_index);
alpar@209
  1225
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1226
        new _writer_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@165
  1227
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1228
      return *this;
deba@165
  1229
    }
deba@165
  1230
deba@165
  1231
    /// \brief Arc writing rule
deba@165
  1232
    ///
deba@165
  1233
    /// Add an arc writing rule to writer.
deba@165
  1234
    GraphWriter& arc(const std::string& caption, const Arc& arc) {
deba@165
  1235
      typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter;
deba@165
  1236
      Converter converter(_graph, _edge_index);
alpar@209
  1237
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1238
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@165
  1239
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1240
      return *this;
deba@165
  1241
    }
deba@165
  1242
kpeter@192
  1243
    /// \name Section captions
deba@165
  1244
    /// @{
deba@165
  1245
kpeter@192
  1246
    /// \brief Add an additional caption to the \c \@nodes section
deba@165
  1247
    ///
kpeter@192
  1248
    /// Add an additional caption to the \c \@nodes section.
deba@165
  1249
    GraphWriter& nodes(const std::string& caption) {
deba@165
  1250
      _nodes_caption = caption;
deba@165
  1251
      return *this;
deba@165
  1252
    }
deba@165
  1253
kpeter@192
  1254
    /// \brief Add an additional caption to the \c \@arcs section
deba@165
  1255
    ///
kpeter@192
  1256
    /// Add an additional caption to the \c \@arcs section.
deba@165
  1257
    GraphWriter& edges(const std::string& caption) {
deba@165
  1258
      _edges_caption = caption;
deba@165
  1259
      return *this;
deba@165
  1260
    }
deba@165
  1261
kpeter@192
  1262
    /// \brief Add an additional caption to the \c \@attributes section
deba@165
  1263
    ///
kpeter@192
  1264
    /// Add an additional caption to the \c \@attributes section.
deba@165
  1265
    GraphWriter& attributes(const std::string& caption) {
deba@165
  1266
      _attributes_caption = caption;
deba@165
  1267
      return *this;
deba@165
  1268
    }
deba@165
  1269
deba@165
  1270
    /// \name Skipping section
deba@165
  1271
    /// @{
deba@165
  1272
deba@165
  1273
    /// \brief Skip writing the node set
deba@165
  1274
    ///
kpeter@192
  1275
    /// The \c \@nodes section will not be written to the stream.
deba@165
  1276
    GraphWriter& skipNodes() {
deba@165
  1277
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
deba@185
  1278
      _skip_nodes = true;
deba@165
  1279
      return *this;
deba@165
  1280
    }
deba@165
  1281
deba@165
  1282
    /// \brief Skip writing edge set
deba@165
  1283
    ///
kpeter@192
  1284
    /// The \c \@edges section will not be written to the stream.
deba@165
  1285
    GraphWriter& skipEdges() {
deba@165
  1286
      LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
deba@185
  1287
      _skip_edges = true;
deba@165
  1288
      return *this;
deba@165
  1289
    }
deba@165
  1290
deba@165
  1291
    /// @}
deba@165
  1292
deba@165
  1293
  private:
deba@165
  1294
deba@165
  1295
    void writeNodes() {
deba@165
  1296
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@165
  1297
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1298
           it != _node_maps.end(); ++it) {
deba@165
  1299
        if (it->first == "label") {
alpar@209
  1300
          label = it->second;
alpar@209
  1301
          break;
alpar@209
  1302
        }
deba@165
  1303
      }
deba@165
  1304
deba@165
  1305
      *_os << "@nodes";
deba@165
  1306
      if (!_nodes_caption.empty()) {
alpar@209
  1307
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
deba@165
  1308
      }
deba@165
  1309
      *_os << std::endl;
deba@165
  1310
deba@165
  1311
      if (label == 0) {
alpar@209
  1312
        *_os << "label" << '\t';
deba@165
  1313
      }
deba@165
  1314
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1315
           it != _node_maps.end(); ++it) {
alpar@209
  1316
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1317
      }
deba@165
  1318
      *_os << std::endl;
deba@165
  1319
deba@165
  1320
      std::vector<Node> nodes;
deba@165
  1321
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1322
        nodes.push_back(n);
deba@165
  1323
      }
alpar@209
  1324
deba@165
  1325
      if (label == 0) {
alpar@209
  1326
        IdMap<Graph, Node> id_map(_graph);
alpar@209
  1327
        _writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map);
alpar@209
  1328
        std::sort(nodes.begin(), nodes.end(), id_less);
deba@165
  1329
      } else {
alpar@209
  1330
        label->sort(nodes);
deba@165
  1331
      }
deba@165
  1332
deba@165
  1333
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
alpar@209
  1334
        Node n = nodes[i];
alpar@209
  1335
        if (label == 0) {
alpar@209
  1336
          std::ostringstream os;
alpar@209
  1337
          os << _graph.id(n);
alpar@209
  1338
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1339
          *_os << '\t';
alpar@209
  1340
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1341
        }
alpar@209
  1342
        for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1343
             it != _node_maps.end(); ++it) {
alpar@209
  1344
          std::string value = it->second->get(n);
alpar@209
  1345
          _writer_bits::writeToken(*_os, value);
alpar@209
  1346
          if (it->first == "label") {
alpar@209
  1347
            _node_index.insert(std::make_pair(n, value));
alpar@209
  1348
          }
alpar@209
  1349
          *_os << '\t';
alpar@209
  1350
        }
alpar@209
  1351
        *_os << std::endl;
deba@165
  1352
      }
deba@165
  1353
    }
deba@165
  1354
deba@185
  1355
    void createNodeIndex() {
deba@185
  1356
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@185
  1357
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1358
           it != _node_maps.end(); ++it) {
deba@185
  1359
        if (it->first == "label") {
alpar@209
  1360
          label = it->second;
alpar@209
  1361
          break;
alpar@209
  1362
        }
deba@185
  1363
      }
deba@185
  1364
deba@185
  1365
      if (label == 0) {
alpar@209
  1366
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1367
          std::ostringstream os;
alpar@209
  1368
          os << _graph.id(n);
alpar@209
  1369
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1370
        }
deba@185
  1371
      } else {
alpar@209
  1372
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1373
          std::string value = label->get(n);
alpar@209
  1374
          _node_index.insert(std::make_pair(n, value));
alpar@209
  1375
        }
deba@185
  1376
      }
deba@185
  1377
    }
deba@185
  1378
deba@165
  1379
    void writeEdges() {
deba@165
  1380
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@165
  1381
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1382
           it != _edge_maps.end(); ++it) {
deba@165
  1383
        if (it->first == "label") {
alpar@209
  1384
          label = it->second;
alpar@209
  1385
          break;
alpar@209
  1386
        }
deba@165
  1387
      }
deba@165
  1388
deba@165
  1389
      *_os << "@edges";
deba@165
  1390
      if (!_edges_caption.empty()) {
alpar@209
  1391
        _writer_bits::writeToken(*_os << ' ', _edges_caption);
deba@165
  1392
      }
deba@165
  1393
      *_os << std::endl;
deba@165
  1394
deba@165
  1395
      *_os << '\t' << '\t';
deba@165
  1396
      if (label == 0) {
alpar@209
  1397
        *_os << "label" << '\t';
deba@165
  1398
      }
deba@165
  1399
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1400
           it != _edge_maps.end(); ++it) {
alpar@209
  1401
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1402
      }
deba@165
  1403
      *_os << std::endl;
deba@165
  1404
deba@165
  1405
      std::vector<Edge> edges;
deba@165
  1406
      for (EdgeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1407
        edges.push_back(n);
deba@165
  1408
      }
alpar@209
  1409
deba@165
  1410
      if (label == 0) {
alpar@209
  1411
        IdMap<Graph, Edge> id_map(_graph);
alpar@209
  1412
        _writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map);
alpar@209
  1413
        std::sort(edges.begin(), edges.end(), id_less);
deba@165
  1414
      } else {
alpar@209
  1415
        label->sort(edges);
deba@165
  1416
      }
deba@165
  1417
deba@165
  1418
      for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
alpar@209
  1419
        Edge e = edges[i];
alpar@209
  1420
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1421
                                 find(_graph.u(e))->second);
alpar@209
  1422
        *_os << '\t';
alpar@209
  1423
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1424
                                 find(_graph.v(e))->second);
alpar@209
  1425
        *_os << '\t';
alpar@209
  1426
        if (label == 0) {
alpar@209
  1427
          std::ostringstream os;
alpar@209
  1428
          os << _graph.id(e);
alpar@209
  1429
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1430
          *_os << '\t';
alpar@209
  1431
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1432
        }
alpar@209
  1433
        for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1434
             it != _edge_maps.end(); ++it) {
alpar@209
  1435
          std::string value = it->second->get(e);
alpar@209
  1436
          _writer_bits::writeToken(*_os, value);
alpar@209
  1437
          if (it->first == "label") {
alpar@209
  1438
            _edge_index.insert(std::make_pair(e, value));
alpar@209
  1439
          }
alpar@209
  1440
          *_os << '\t';
alpar@209
  1441
        }
alpar@209
  1442
        *_os << std::endl;
deba@165
  1443
      }
deba@165
  1444
    }
deba@165
  1445
deba@185
  1446
    void createEdgeIndex() {
deba@185
  1447
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@185
  1448
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1449
           it != _edge_maps.end(); ++it) {
deba@185
  1450
        if (it->first == "label") {
alpar@209
  1451
          label = it->second;
alpar@209
  1452
          break;
alpar@209
  1453
        }
deba@185
  1454
      }
deba@185
  1455
deba@185
  1456
      if (label == 0) {
alpar@209
  1457
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1458
          std::ostringstream os;
alpar@209
  1459
          os << _graph.id(e);
alpar@209
  1460
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1461
        }
deba@185
  1462
      } else {
alpar@209
  1463
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1464
          std::string value = label->get(e);
alpar@209
  1465
          _edge_index.insert(std::make_pair(e, value));
alpar@209
  1466
        }
deba@185
  1467
      }
deba@185
  1468
    }
deba@185
  1469
deba@165
  1470
    void writeAttributes() {
deba@165
  1471
      if (_attributes.empty()) return;
deba@165
  1472
      *_os << "@attributes";
deba@165
  1473
      if (!_attributes_caption.empty()) {
alpar@209
  1474
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
deba@165
  1475
      }
deba@165
  1476
      *_os << std::endl;
deba@165
  1477
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1478
           it != _attributes.end(); ++it) {
alpar@209
  1479
        _writer_bits::writeToken(*_os, it->first) << ' ';
alpar@209
  1480
        _writer_bits::writeToken(*_os, it->second->get());
alpar@209
  1481
        *_os << std::endl;
deba@165
  1482
      }
deba@165
  1483
    }
alpar@209
  1484
deba@165
  1485
  public:
alpar@209
  1486
alpar@209
  1487
    /// \name Execution of the writer
deba@165
  1488
    /// @{
deba@165
  1489
deba@165
  1490
    /// \brief Start the batch processing
deba@165
  1491
    ///
kpeter@192
  1492
    /// This function starts the batch processing.
deba@165
  1493
    void run() {
deba@165
  1494
      if (!_skip_nodes) {
alpar@209
  1495
        writeNodes();
deba@185
  1496
      } else {
alpar@209
  1497
        createNodeIndex();
deba@165
  1498
      }
alpar@209
  1499
      if (!_skip_edges) {
alpar@209
  1500
        writeEdges();
deba@185
  1501
      } else {
alpar@209
  1502
        createEdgeIndex();
deba@165
  1503
      }
deba@165
  1504
      writeAttributes();
deba@165
  1505
    }
deba@165
  1506
kpeter@192
  1507
    /// \brief Give back the stream of the writer
deba@165
  1508
    ///
kpeter@192
  1509
    /// Give back the stream of the writer
deba@165
  1510
    std::ostream& ostream() {
deba@165
  1511
      return *_os;
deba@165
  1512
    }
deba@165
  1513
deba@165
  1514
    /// @}
deba@165
  1515
  };
deba@165
  1516
kpeter@192
  1517
  /// \brief Return a \ref GraphWriter class
alpar@209
  1518
  ///
kpeter@192
  1519
  /// This function just returns a \ref GraphWriter class.
deba@165
  1520
  /// \relates GraphWriter
deba@165
  1521
  template <typename Graph>
kpeter@293
  1522
  GraphWriter<Graph> graphWriter(const Graph& graph,
kpeter@293
  1523
                                 std::ostream& os = std::cout) {
kpeter@293
  1524
    GraphWriter<Graph> tmp(graph, os);
deba@165
  1525
    return tmp;
deba@165
  1526
  }
deba@165
  1527
kpeter@192
  1528
  /// \brief Return a \ref GraphWriter class
alpar@209
  1529
  ///
kpeter@192
  1530
  /// This function just returns a \ref GraphWriter class.
deba@165
  1531
  /// \relates GraphWriter
deba@165
  1532
  template <typename Graph>
kpeter@293
  1533
  GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
kpeter@293
  1534
    GraphWriter<Graph> tmp(graph, fn);
deba@165
  1535
    return tmp;
deba@165
  1536
  }
deba@165
  1537
kpeter@192
  1538
  /// \brief Return a \ref GraphWriter class
alpar@209
  1539
  ///
kpeter@192
  1540
  /// This function just returns a \ref GraphWriter class.
deba@165
  1541
  /// \relates GraphWriter
deba@165
  1542
  template <typename Graph>
kpeter@293
  1543
  GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
kpeter@293
  1544
    GraphWriter<Graph> tmp(graph, fn);
deba@165
  1545
    return tmp;
deba@165
  1546
  }
deba@248
  1547
deba@248
  1548
  class SectionWriter;
deba@248
  1549
deba@248
  1550
  SectionWriter sectionWriter(std::istream& is);
deba@248
  1551
  SectionWriter sectionWriter(const std::string& fn);
deba@248
  1552
  SectionWriter sectionWriter(const char* fn);
deba@248
  1553
deba@248
  1554
  /// \ingroup lemon_io
deba@248
  1555
  ///
deba@248
  1556
  /// \brief Section writer class
deba@248
  1557
  ///
deba@248
  1558
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
deba@248
  1559
  /// which contain any data in arbitrary format. Such sections can be
deba@248
  1560
  /// written with this class. A writing rule can be added to the
deba@248
  1561
  /// class with two different functions. With the \c sectionLines()
deba@248
  1562
  /// function a generator can write the section line-by-line, while
deba@248
  1563
  /// with the \c sectionStream() member the section can be written to
deba@248
  1564
  /// an output stream.
deba@248
  1565
  class SectionWriter {
deba@248
  1566
  private:
deba@248
  1567
deba@248
  1568
    std::ostream* _os;
deba@248
  1569
    bool local_os;
deba@248
  1570
deba@248
  1571
    typedef std::vector<std::pair<std::string, _writer_bits::Section*> >
deba@248
  1572
    Sections;
deba@248
  1573
deba@248
  1574
    Sections _sections;
deba@248
  1575
deba@248
  1576
  public:
deba@248
  1577
deba@248
  1578
    /// \brief Constructor
deba@248
  1579
    ///
deba@248
  1580
    /// Construct a section writer, which writes to the given output
deba@248
  1581
    /// stream.
deba@248
  1582
    SectionWriter(std::ostream& os)
deba@248
  1583
      : _os(&os), local_os(false) {}
deba@248
  1584
deba@248
  1585
    /// \brief Constructor
deba@248
  1586
    ///
deba@248
  1587
    /// Construct a section writer, which writes into the given file.
deba@248
  1588
    SectionWriter(const std::string& fn)
deba@290
  1589
      : _os(new std::ofstream(fn.c_str())), local_os(true) {
kpeter@291
  1590
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
  1591
    }
deba@248
  1592
deba@248
  1593
    /// \brief Constructor
deba@248
  1594
    ///
deba@248
  1595
    /// Construct a section writer, which writes into the given file.
deba@248
  1596
    SectionWriter(const char* fn)
deba@290
  1597
      : _os(new std::ofstream(fn)), local_os(true) {
kpeter@291
  1598
      if (!(*_os)) throw IoError("Cannot write file", fn);
deba@290
  1599
    }
deba@248
  1600
deba@248
  1601
    /// \brief Destructor
deba@248
  1602
    ~SectionWriter() {
deba@248
  1603
      for (Sections::iterator it = _sections.begin();
deba@248
  1604
           it != _sections.end(); ++it) {
deba@248
  1605
        delete it->second;
deba@248
  1606
      }
deba@248
  1607
deba@248
  1608
      if (local_os) {
deba@248
  1609
        delete _os;
deba@248
  1610
      }
deba@248
  1611
deba@248
  1612
    }
deba@248
  1613
deba@248
  1614
  private:
deba@248
  1615
deba@248
  1616
    friend SectionWriter sectionWriter(std::ostream& os);
deba@248
  1617
    friend SectionWriter sectionWriter(const std::string& fn);
deba@248
  1618
    friend SectionWriter sectionWriter(const char* fn);
deba@248
  1619
deba@248
  1620
    SectionWriter(SectionWriter& other)
deba@248
  1621
      : _os(other._os), local_os(other.local_os) {
deba@248
  1622
deba@248
  1623
      other._os = 0;
deba@248
  1624
      other.local_os = false;
deba@248
  1625
deba@248
  1626
      _sections.swap(other._sections);
deba@248
  1627
    }
deba@248
  1628
deba@248
  1629
    SectionWriter& operator=(const SectionWriter&);
deba@248
  1630
deba@248
  1631
  public:
deba@248
  1632
deba@248
  1633
    /// \name Section writers
deba@248
  1634
    /// @{
deba@248
  1635
deba@248
  1636
    /// \brief Add a section writer with line oriented writing
deba@248
  1637
    ///
deba@248
  1638
    /// The first parameter is the type descriptor of the section, the
deba@248
  1639
    /// second is a generator with std::string values. At the writing
deba@248
  1640
    /// process, the returned \c std::string will be written into the
deba@248
  1641
    /// output file until it is an empty string.
deba@248
  1642
    ///
deba@248
  1643
    /// For example, an integer vector is written into a section.
deba@248
  1644
    ///\code
deba@248
  1645
    ///  @numbers
deba@248
  1646
    ///  12 45 23 78
deba@248
  1647
    ///  4 28 38 28
deba@248
  1648
    ///  23 6 16
deba@248
  1649
    ///\endcode
deba@248
  1650
    ///
deba@248
  1651
    /// The generator is implemented as a struct.
deba@248
  1652
    ///\code
deba@248
  1653
    ///  struct NumberSection {
deba@248
  1654
    ///    std::vector<int>::const_iterator _it, _end;
deba@248
  1655
    ///    NumberSection(const std::vector<int>& data)
deba@248
  1656
    ///      : _it(data.begin()), _end(data.end()) {}
deba@248
  1657
    ///    std::string operator()() {
deba@248
  1658
    ///      int rem_in_line = 4;
deba@248
  1659
    ///      std::ostringstream ls;
deba@248
  1660
    ///      while (rem_in_line > 0 && _it != _end) {
deba@248
  1661
    ///        ls << *(_it++) << ' ';
deba@248
  1662
    ///        --rem_in_line;
deba@248
  1663
    ///      }
deba@248
  1664
    ///      return ls.str();
deba@248
  1665
    ///    }
deba@248
  1666
    ///  };
deba@248
  1667
    ///
deba@248
  1668
    ///  // ...
deba@248
  1669
    ///
deba@248
  1670
    ///  writer.sectionLines("numbers", NumberSection(vec));
deba@248
  1671
    ///\endcode
deba@248
  1672
    template <typename Functor>
deba@248
  1673
    SectionWriter& sectionLines(const std::string& type, Functor functor) {
deba@248
  1674
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1675
      _sections.push_back(std::make_pair(type,
deba@248
  1676
        new _writer_bits::LineSection<Functor>(functor)));
deba@248
  1677
      return *this;
deba@248
  1678
    }
deba@248
  1679
deba@248
  1680
deba@248
  1681
    /// \brief Add a section writer with stream oriented writing
deba@248
  1682
    ///
deba@248
  1683
    /// The first parameter is the type of the section, the second is
deba@248
  1684
    /// a functor, which takes a \c std::ostream& parameter. The
deba@248
  1685
    /// functor writes the section to the output stream.
deba@248
  1686
    /// \warning The last line must be closed with end-line character.
deba@248
  1687
    template <typename Functor>
deba@248
  1688
    SectionWriter& sectionStream(const std::string& type, Functor functor) {
deba@248
  1689
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1690
      _sections.push_back(std::make_pair(type,
deba@248
  1691
         new _writer_bits::StreamSection<Functor>(functor)));
deba@248
  1692
      return *this;
deba@248
  1693
    }
deba@248
  1694
deba@248
  1695
    /// @}
deba@248
  1696
deba@248
  1697
  public:
deba@248
  1698
deba@248
  1699
deba@248
  1700
    /// \name Execution of the writer
deba@248
  1701
    /// @{
deba@248
  1702
deba@248
  1703
    /// \brief Start the batch processing
deba@248
  1704
    ///
deba@248
  1705
    /// This function starts the batch processing.
deba@248
  1706
    void run() {
deba@248
  1707
deba@248
  1708
      LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer");
deba@248
  1709
deba@248
  1710
      for (Sections::iterator it = _sections.begin();
deba@248
  1711
           it != _sections.end(); ++it) {
deba@248
  1712
        (*_os) << '@' << it->first << std::endl;
deba@248
  1713
        it->second->process(*_os);
deba@248
  1714
      }
deba@248
  1715
    }
deba@248
  1716
deba@248
  1717
    /// \brief Give back the stream of the writer
deba@248
  1718
    ///
deba@248
  1719
    /// Returns the stream of the writer
deba@248
  1720
    std::ostream& ostream() {
deba@248
  1721
      return *_os;
deba@248
  1722
    }
deba@248
  1723
deba@248
  1724
    /// @}
deba@248
  1725
deba@248
  1726
  };
deba@248
  1727
deba@248
  1728
  /// \brief Return a \ref SectionWriter class
deba@248
  1729
  ///
deba@248
  1730
  /// This function just returns a \ref SectionWriter class.
deba@248
  1731
  /// \relates SectionWriter
deba@248
  1732
  inline SectionWriter sectionWriter(std::ostream& os) {
deba@248
  1733
    SectionWriter tmp(os);
deba@248
  1734
    return tmp;
deba@248
  1735
  }
deba@248
  1736
deba@248
  1737
  /// \brief Return a \ref SectionWriter class
deba@248
  1738
  ///
deba@248
  1739
  /// This function just returns a \ref SectionWriter class.
deba@248
  1740
  /// \relates SectionWriter
deba@248
  1741
  inline SectionWriter sectionWriter(const std::string& fn) {
deba@248
  1742
    SectionWriter tmp(fn);
deba@248
  1743
    return tmp;
deba@248
  1744
  }
deba@248
  1745
deba@248
  1746
  /// \brief Return a \ref SectionWriter class
deba@248
  1747
  ///
deba@248
  1748
  /// This function just returns a \ref SectionWriter class.
deba@248
  1749
  /// \relates SectionWriter
deba@248
  1750
  inline SectionWriter sectionWriter(const char* fn) {
deba@248
  1751
    SectionWriter tmp(fn);
deba@248
  1752
    return tmp;
deba@248
  1753
  }
deba@127
  1754
}
deba@127
  1755
deba@127
  1756
#endif