lemon/lgf_writer.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 18 Aug 2008 20:33:11 +0200
changeset 249 f0b89f242745
parent 240 bea328c5a8d3
child 290 f6899946c1ac
child 293 47fbc814aa31
permissions -rw-r--r--
Ignore the downloaded tag files
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@127
    58
      throw DataFormatError("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()) {
alpar@209
   206
          throw DataFormatError("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()) {
alpar@209
   226
          throw DataFormatError("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>
alpar@209
   355
  DigraphWriter<Digraph> digraphWriter(std::ostream& os,
alpar@209
   356
                                       const Digraph& digraph);
deba@190
   357
deba@190
   358
  template <typename Digraph>
alpar@209
   359
  DigraphWriter<Digraph> digraphWriter(const std::string& fn,
alpar@209
   360
                                       const Digraph& digraph);
deba@190
   361
deba@190
   362
  template <typename Digraph>
alpar@209
   363
  DigraphWriter<Digraph> digraphWriter(const char *fn,
alpar@209
   364
                                       const Digraph& digraph);
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@192
   385
  /// DigraphWriter<Digraph>(std::cout, digraph).
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.
alpar@209
   455
    DigraphWriter(std::ostream& is, const Digraph& digraph)
deba@127
   456
      : _os(&is), 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.
alpar@209
   463
    DigraphWriter(const std::string& fn, const Digraph& digraph)
deba@127
   464
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
alpar@209
   465
        _skip_nodes(false), _skip_arcs(false) {}
deba@127
   466
alpar@156
   467
    /// \brief Constructor
alpar@156
   468
    ///
alpar@156
   469
    /// Construct a directed graph writer, which writes to the given
alpar@156
   470
    /// output file.
alpar@209
   471
    DigraphWriter(const char* fn, const Digraph& digraph)
deba@127
   472
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
alpar@209
   473
        _skip_nodes(false), _skip_arcs(false) {}
deba@127
   474
alpar@156
   475
    /// \brief Destructor
deba@127
   476
    ~DigraphWriter() {
alpar@209
   477
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   478
           it != _node_maps.end(); ++it) {
alpar@209
   479
        delete it->second;
deba@127
   480
      }
deba@127
   481
alpar@209
   482
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   483
           it != _arc_maps.end(); ++it) {
alpar@209
   484
        delete it->second;
deba@127
   485
      }
deba@127
   486
alpar@209
   487
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
   488
           it != _attributes.end(); ++it) {
alpar@209
   489
        delete it->second;
deba@127
   490
      }
deba@127
   491
deba@127
   492
      if (local_os) {
alpar@209
   493
        delete _os;
deba@127
   494
      }
deba@127
   495
    }
deba@127
   496
deba@127
   497
  private:
deba@190
   498
alpar@209
   499
    friend DigraphWriter<Digraph> digraphWriter<>(std::ostream& os,
alpar@209
   500
                                                  const Digraph& digraph);
alpar@209
   501
    friend DigraphWriter<Digraph> digraphWriter<>(const std::string& fn,
alpar@209
   502
                                                  const Digraph& digraph);
alpar@209
   503
    friend DigraphWriter<Digraph> digraphWriter<>(const char *fn,
alpar@209
   504
                                                  const Digraph& digraph);
deba@190
   505
alpar@209
   506
    DigraphWriter(DigraphWriter& other)
deba@190
   507
      : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
alpar@209
   508
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
deba@190
   509
deba@190
   510
      other._os = 0;
deba@190
   511
      other.local_os = false;
deba@190
   512
deba@190
   513
      _node_index.swap(other._node_index);
deba@190
   514
      _arc_index.swap(other._arc_index);
deba@190
   515
deba@190
   516
      _node_maps.swap(other._node_maps);
deba@190
   517
      _arc_maps.swap(other._arc_maps);
deba@190
   518
      _attributes.swap(other._attributes);
deba@190
   519
deba@190
   520
      _nodes_caption = other._nodes_caption;
deba@190
   521
      _arcs_caption = other._arcs_caption;
deba@190
   522
      _attributes_caption = other._attributes_caption;
deba@190
   523
    }
alpar@209
   524
deba@127
   525
    DigraphWriter& operator=(const DigraphWriter&);
deba@127
   526
deba@127
   527
  public:
deba@127
   528
alpar@156
   529
    /// \name Writing rules
alpar@156
   530
    /// @{
alpar@209
   531
kpeter@192
   532
    /// \brief Node map writing rule
alpar@156
   533
    ///
kpeter@192
   534
    /// Add a node map writing rule to the writer.
deba@127
   535
    template <typename Map>
deba@127
   536
    DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
deba@127
   537
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   538
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
   539
        new _writer_bits::MapStorage<Node, Map>(map);
deba@127
   540
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   541
      return *this;
deba@127
   542
    }
deba@127
   543
alpar@156
   544
    /// \brief Node map writing rule
alpar@156
   545
    ///
alpar@156
   546
    /// Add a node map writing rule with specialized converter to the
alpar@156
   547
    /// writer.
deba@127
   548
    template <typename Map, typename Converter>
alpar@209
   549
    DigraphWriter& nodeMap(const std::string& caption, const Map& map,
alpar@209
   550
                           const Converter& converter = Converter()) {
deba@127
   551
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
   552
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
   553
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@127
   554
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   555
      return *this;
deba@127
   556
    }
deba@127
   557
alpar@156
   558
    /// \brief Arc map writing rule
alpar@156
   559
    ///
alpar@156
   560
    /// Add an arc map writing rule to the writer.
deba@127
   561
    template <typename Map>
deba@127
   562
    DigraphWriter& arcMap(const std::string& caption, const Map& map) {
deba@127
   563
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
   564
      _writer_bits::MapStorageBase<Arc>* storage =
alpar@209
   565
        new _writer_bits::MapStorage<Arc, Map>(map);
deba@127
   566
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   567
      return *this;
deba@127
   568
    }
deba@127
   569
alpar@156
   570
    /// \brief Arc map writing rule
alpar@156
   571
    ///
alpar@156
   572
    /// Add an arc map writing rule with specialized converter to the
alpar@156
   573
    /// writer.
deba@127
   574
    template <typename Map, typename Converter>
alpar@209
   575
    DigraphWriter& arcMap(const std::string& caption, const Map& map,
alpar@209
   576
                          const Converter& converter = Converter()) {
deba@127
   577
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
   578
      _writer_bits::MapStorageBase<Arc>* storage =
alpar@209
   579
        new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
deba@127
   580
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   581
      return *this;
deba@127
   582
    }
deba@127
   583
alpar@156
   584
    /// \brief Attribute writing rule
alpar@156
   585
    ///
alpar@156
   586
    /// Add an attribute writing rule to the writer.
deba@127
   587
    template <typename Value>
deba@127
   588
    DigraphWriter& attribute(const std::string& caption, const Value& value) {
alpar@209
   589
      _writer_bits::ValueStorageBase* storage =
alpar@209
   590
        new _writer_bits::ValueStorage<Value>(value);
deba@127
   591
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   592
      return *this;
deba@127
   593
    }
deba@127
   594
alpar@156
   595
    /// \brief Attribute writing rule
alpar@156
   596
    ///
alpar@156
   597
    /// Add an attribute writing rule with specialized converter to the
alpar@156
   598
    /// writer.
deba@127
   599
    template <typename Value, typename Converter>
alpar@209
   600
    DigraphWriter& attribute(const std::string& caption, const Value& value,
alpar@209
   601
                             const Converter& converter = Converter()) {
alpar@209
   602
      _writer_bits::ValueStorageBase* storage =
alpar@209
   603
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
deba@127
   604
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   605
      return *this;
deba@127
   606
    }
deba@127
   607
alpar@156
   608
    /// \brief Node writing rule
alpar@156
   609
    ///
alpar@156
   610
    /// Add a node writing rule to the writer.
deba@127
   611
    DigraphWriter& node(const std::string& caption, const Node& node) {
deba@127
   612
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
deba@127
   613
      Converter converter(_node_index);
alpar@209
   614
      _writer_bits::ValueStorageBase* storage =
alpar@209
   615
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
deba@127
   616
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   617
      return *this;
deba@127
   618
    }
deba@127
   619
alpar@156
   620
    /// \brief Arc writing rule
alpar@156
   621
    ///
alpar@156
   622
    /// Add an arc writing rule to writer.
deba@127
   623
    DigraphWriter& arc(const std::string& caption, const Arc& arc) {
deba@127
   624
      typedef _writer_bits::MapLookUpConverter<Arc> Converter;
deba@127
   625
      Converter converter(_arc_index);
alpar@209
   626
      _writer_bits::ValueStorageBase* storage =
alpar@209
   627
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@127
   628
      _attributes.push_back(std::make_pair(caption, storage));
deba@127
   629
      return *this;
deba@127
   630
    }
deba@127
   631
kpeter@192
   632
    /// \name Section captions
alpar@156
   633
    /// @{
alpar@156
   634
kpeter@192
   635
    /// \brief Add an additional caption to the \c \@nodes section
alpar@156
   636
    ///
kpeter@192
   637
    /// Add an additional caption to the \c \@nodes section.
deba@127
   638
    DigraphWriter& nodes(const std::string& caption) {
deba@127
   639
      _nodes_caption = caption;
deba@127
   640
      return *this;
deba@127
   641
    }
deba@127
   642
kpeter@192
   643
    /// \brief Add an additional caption to the \c \@arcs section
alpar@156
   644
    ///
kpeter@192
   645
    /// Add an additional caption to the \c \@arcs section.
deba@127
   646
    DigraphWriter& arcs(const std::string& caption) {
deba@127
   647
      _arcs_caption = caption;
deba@127
   648
      return *this;
deba@127
   649
    }
deba@127
   650
kpeter@192
   651
    /// \brief Add an additional caption to the \c \@attributes section
alpar@156
   652
    ///
kpeter@192
   653
    /// Add an additional caption to the \c \@attributes section.
deba@127
   654
    DigraphWriter& attributes(const std::string& caption) {
deba@127
   655
      _attributes_caption = caption;
deba@127
   656
      return *this;
deba@127
   657
    }
deba@127
   658
alpar@156
   659
    /// \name Skipping section
alpar@156
   660
    /// @{
alpar@156
   661
alpar@156
   662
    /// \brief Skip writing the node set
alpar@156
   663
    ///
kpeter@192
   664
    /// The \c \@nodes section will not be written to the stream.
deba@127
   665
    DigraphWriter& skipNodes() {
deba@127
   666
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
deba@185
   667
      _skip_nodes = true;
deba@127
   668
      return *this;
deba@127
   669
    }
deba@127
   670
alpar@156
   671
    /// \brief Skip writing arc set
alpar@156
   672
    ///
kpeter@192
   673
    /// The \c \@arcs section will not be written to the stream.
deba@127
   674
    DigraphWriter& skipArcs() {
deba@127
   675
      LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
deba@185
   676
      _skip_arcs = true;
deba@127
   677
      return *this;
deba@127
   678
    }
deba@127
   679
alpar@156
   680
    /// @}
alpar@156
   681
deba@127
   682
  private:
deba@127
   683
deba@127
   684
    void writeNodes() {
deba@127
   685
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@127
   686
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   687
           it != _node_maps.end(); ++it) {
deba@127
   688
        if (it->first == "label") {
alpar@209
   689
          label = it->second;
alpar@209
   690
          break;
alpar@209
   691
        }
deba@127
   692
      }
deba@127
   693
deba@127
   694
      *_os << "@nodes";
deba@127
   695
      if (!_nodes_caption.empty()) {
alpar@209
   696
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
deba@127
   697
      }
deba@127
   698
      *_os << std::endl;
deba@127
   699
deba@127
   700
      if (label == 0) {
alpar@209
   701
        *_os << "label" << '\t';
deba@127
   702
      }
deba@127
   703
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   704
           it != _node_maps.end(); ++it) {
alpar@209
   705
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@127
   706
      }
deba@127
   707
      *_os << std::endl;
deba@127
   708
deba@127
   709
      std::vector<Node> nodes;
deba@127
   710
      for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   711
        nodes.push_back(n);
deba@127
   712
      }
alpar@209
   713
deba@127
   714
      if (label == 0) {
alpar@209
   715
        IdMap<Digraph, Node> id_map(_digraph);
alpar@209
   716
        _writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map);
alpar@209
   717
        std::sort(nodes.begin(), nodes.end(), id_less);
deba@127
   718
      } else {
alpar@209
   719
        label->sort(nodes);
deba@127
   720
      }
deba@127
   721
deba@127
   722
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
alpar@209
   723
        Node n = nodes[i];
alpar@209
   724
        if (label == 0) {
alpar@209
   725
          std::ostringstream os;
alpar@209
   726
          os << _digraph.id(n);
alpar@209
   727
          _writer_bits::writeToken(*_os, os.str());
alpar@209
   728
          *_os << '\t';
alpar@209
   729
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
   730
        }
alpar@209
   731
        for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   732
             it != _node_maps.end(); ++it) {
alpar@209
   733
          std::string value = it->second->get(n);
alpar@209
   734
          _writer_bits::writeToken(*_os, value);
alpar@209
   735
          if (it->first == "label") {
alpar@209
   736
            _node_index.insert(std::make_pair(n, value));
alpar@209
   737
          }
alpar@209
   738
          *_os << '\t';
alpar@209
   739
        }
alpar@209
   740
        *_os << std::endl;
deba@127
   741
      }
deba@127
   742
    }
deba@127
   743
deba@185
   744
    void createNodeIndex() {
deba@185
   745
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@185
   746
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
   747
           it != _node_maps.end(); ++it) {
deba@185
   748
        if (it->first == "label") {
alpar@209
   749
          label = it->second;
alpar@209
   750
          break;
alpar@209
   751
        }
deba@185
   752
      }
deba@185
   753
deba@185
   754
      if (label == 0) {
alpar@209
   755
        for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   756
          std::ostringstream os;
alpar@209
   757
          os << _digraph.id(n);
alpar@209
   758
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
   759
        }
deba@185
   760
      } else {
alpar@209
   761
        for (NodeIt n(_digraph); n != INVALID; ++n) {
alpar@209
   762
          std::string value = label->get(n);
alpar@209
   763
          _node_index.insert(std::make_pair(n, value));
alpar@209
   764
        }
deba@185
   765
      }
deba@185
   766
    }
deba@185
   767
deba@127
   768
    void writeArcs() {
deba@127
   769
      _writer_bits::MapStorageBase<Arc>* label = 0;
deba@127
   770
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   771
           it != _arc_maps.end(); ++it) {
deba@127
   772
        if (it->first == "label") {
alpar@209
   773
          label = it->second;
alpar@209
   774
          break;
alpar@209
   775
        }
deba@127
   776
      }
deba@127
   777
deba@127
   778
      *_os << "@arcs";
deba@127
   779
      if (!_arcs_caption.empty()) {
alpar@209
   780
        _writer_bits::writeToken(*_os << ' ', _arcs_caption);
deba@127
   781
      }
deba@127
   782
      *_os << std::endl;
deba@127
   783
deba@127
   784
      *_os << '\t' << '\t';
deba@127
   785
      if (label == 0) {
alpar@209
   786
        *_os << "label" << '\t';
deba@127
   787
      }
deba@127
   788
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   789
           it != _arc_maps.end(); ++it) {
alpar@209
   790
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@127
   791
      }
deba@127
   792
      *_os << std::endl;
deba@127
   793
deba@127
   794
      std::vector<Arc> arcs;
deba@127
   795
      for (ArcIt n(_digraph); n != INVALID; ++n) {
alpar@209
   796
        arcs.push_back(n);
deba@127
   797
      }
alpar@209
   798
deba@127
   799
      if (label == 0) {
alpar@209
   800
        IdMap<Digraph, Arc> id_map(_digraph);
alpar@209
   801
        _writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map);
alpar@209
   802
        std::sort(arcs.begin(), arcs.end(), id_less);
deba@127
   803
      } else {
alpar@209
   804
        label->sort(arcs);
deba@127
   805
      }
deba@127
   806
deba@127
   807
      for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
alpar@209
   808
        Arc a = arcs[i];
alpar@209
   809
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
   810
                                 find(_digraph.source(a))->second);
alpar@209
   811
        *_os << '\t';
alpar@209
   812
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
   813
                                 find(_digraph.target(a))->second);
alpar@209
   814
        *_os << '\t';
alpar@209
   815
        if (label == 0) {
alpar@209
   816
          std::ostringstream os;
alpar@209
   817
          os << _digraph.id(a);
alpar@209
   818
          _writer_bits::writeToken(*_os, os.str());
alpar@209
   819
          *_os << '\t';
alpar@209
   820
          _arc_index.insert(std::make_pair(a, os.str()));
alpar@209
   821
        }
alpar@209
   822
        for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   823
             it != _arc_maps.end(); ++it) {
alpar@209
   824
          std::string value = it->second->get(a);
alpar@209
   825
          _writer_bits::writeToken(*_os, value);
alpar@209
   826
          if (it->first == "label") {
alpar@209
   827
            _arc_index.insert(std::make_pair(a, value));
alpar@209
   828
          }
alpar@209
   829
          *_os << '\t';
alpar@209
   830
        }
alpar@209
   831
        *_os << std::endl;
deba@127
   832
      }
deba@127
   833
    }
deba@127
   834
deba@185
   835
    void createArcIndex() {
deba@185
   836
      _writer_bits::MapStorageBase<Arc>* label = 0;
deba@185
   837
      for (typename ArcMaps::iterator it = _arc_maps.begin();
alpar@209
   838
           it != _arc_maps.end(); ++it) {
deba@185
   839
        if (it->first == "label") {
alpar@209
   840
          label = it->second;
alpar@209
   841
          break;
alpar@209
   842
        }
deba@185
   843
      }
deba@185
   844
deba@185
   845
      if (label == 0) {
alpar@209
   846
        for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   847
          std::ostringstream os;
alpar@209
   848
          os << _digraph.id(a);
alpar@209
   849
          _arc_index.insert(std::make_pair(a, os.str()));
alpar@209
   850
        }
deba@185
   851
      } else {
alpar@209
   852
        for (ArcIt a(_digraph); a != INVALID; ++a) {
alpar@209
   853
          std::string value = label->get(a);
alpar@209
   854
          _arc_index.insert(std::make_pair(a, value));
alpar@209
   855
        }
deba@185
   856
      }
deba@185
   857
    }
deba@185
   858
deba@127
   859
    void writeAttributes() {
deba@127
   860
      if (_attributes.empty()) return;
deba@127
   861
      *_os << "@attributes";
deba@127
   862
      if (!_attributes_caption.empty()) {
alpar@209
   863
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
deba@127
   864
      }
deba@127
   865
      *_os << std::endl;
deba@127
   866
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
   867
           it != _attributes.end(); ++it) {
alpar@209
   868
        _writer_bits::writeToken(*_os, it->first) << ' ';
alpar@209
   869
        _writer_bits::writeToken(*_os, it->second->get());
alpar@209
   870
        *_os << std::endl;
deba@127
   871
      }
deba@127
   872
    }
alpar@209
   873
deba@127
   874
  public:
alpar@209
   875
alpar@209
   876
    /// \name Execution of the writer
alpar@156
   877
    /// @{
alpar@156
   878
alpar@156
   879
    /// \brief Start the batch processing
alpar@156
   880
    ///
kpeter@192
   881
    /// This function starts the batch processing.
deba@127
   882
    void run() {
deba@127
   883
      if (!_skip_nodes) {
alpar@209
   884
        writeNodes();
deba@185
   885
      } else {
alpar@209
   886
        createNodeIndex();
deba@127
   887
      }
alpar@209
   888
      if (!_skip_arcs) {
alpar@209
   889
        writeArcs();
deba@185
   890
      } else {
alpar@209
   891
        createArcIndex();
deba@127
   892
      }
deba@127
   893
      writeAttributes();
deba@127
   894
    }
deba@127
   895
kpeter@192
   896
    /// \brief Give back the stream of the writer
alpar@156
   897
    ///
kpeter@192
   898
    /// Give back the stream of the writer.
alpar@156
   899
    std::ostream& ostream() {
deba@127
   900
      return *_os;
deba@127
   901
    }
alpar@156
   902
alpar@156
   903
    /// @}
deba@127
   904
  };
deba@127
   905
kpeter@192
   906
  /// \brief Return a \ref DigraphWriter class
alpar@209
   907
  ///
kpeter@192
   908
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   909
  /// \relates DigraphWriter
deba@127
   910
  template <typename Digraph>
alpar@209
   911
  DigraphWriter<Digraph> digraphWriter(std::ostream& os,
alpar@209
   912
                                       const Digraph& digraph) {
deba@163
   913
    DigraphWriter<Digraph> tmp(os, digraph);
deba@163
   914
    return tmp;
deba@127
   915
  }
deba@127
   916
kpeter@192
   917
  /// \brief Return a \ref DigraphWriter class
alpar@209
   918
  ///
kpeter@192
   919
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   920
  /// \relates DigraphWriter
deba@127
   921
  template <typename Digraph>
alpar@209
   922
  DigraphWriter<Digraph> digraphWriter(const std::string& fn,
alpar@209
   923
                                       const Digraph& digraph) {
deba@163
   924
    DigraphWriter<Digraph> tmp(fn, digraph);
deba@163
   925
    return tmp;
deba@127
   926
  }
deba@127
   927
kpeter@192
   928
  /// \brief Return a \ref DigraphWriter class
alpar@209
   929
  ///
kpeter@192
   930
  /// This function just returns a \ref DigraphWriter class.
alpar@156
   931
  /// \relates DigraphWriter
deba@127
   932
  template <typename Digraph>
alpar@209
   933
  DigraphWriter<Digraph> digraphWriter(const char* fn,
alpar@209
   934
                                       const Digraph& digraph) {
deba@163
   935
    DigraphWriter<Digraph> tmp(fn, digraph);
deba@163
   936
    return tmp;
deba@127
   937
  }
deba@165
   938
deba@190
   939
  template <typename Graph>
deba@190
   940
  class GraphWriter;
deba@190
   941
deba@190
   942
  template <typename Graph>
alpar@209
   943
  GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph);
deba@190
   944
deba@190
   945
  template <typename Graph>
alpar@209
   946
  GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph);
deba@190
   947
deba@190
   948
  template <typename Graph>
alpar@209
   949
  GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);
deba@190
   950
deba@165
   951
  /// \ingroup lemon_io
alpar@209
   952
  ///
kpeter@192
   953
  /// \brief \ref lgf-format "LGF" writer for directed graphs
deba@165
   954
  ///
deba@165
   955
  /// This utility writes an \ref lgf-format "LGF" file.
kpeter@192
   956
  ///
kpeter@192
   957
  /// It can be used almost the same way as \c DigraphWriter.
kpeter@192
   958
  /// The only difference is that this class can handle edges and
kpeter@192
   959
  /// edge maps as well as arcs and arc maps.
deba@201
   960
  ///
deba@201
   961
  /// The arc maps are written into the file as two columns, the
deba@201
   962
  /// caption of the columns are the name of the map prefixed with \c
deba@201
   963
  /// '+' and \c '-'. The arcs are written into the \c \@attributes
deba@201
   964
  /// section as a \c '+' or a \c '-' prefix (depends on the direction
deba@201
   965
  /// of the arc) and the label of corresponding edge.
deba@165
   966
  template <typename _Graph>
deba@165
   967
  class GraphWriter {
deba@165
   968
  public:
deba@165
   969
deba@165
   970
    typedef _Graph Graph;
deba@165
   971
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
alpar@209
   972
deba@165
   973
  private:
deba@165
   974
deba@165
   975
deba@165
   976
    std::ostream* _os;
deba@165
   977
    bool local_os;
deba@165
   978
deba@237
   979
    const Graph& _graph;
deba@165
   980
deba@165
   981
    std::string _nodes_caption;
deba@165
   982
    std::string _edges_caption;
deba@165
   983
    std::string _attributes_caption;
alpar@209
   984
deba@165
   985
    typedef std::map<Node, std::string> NodeIndex;
deba@165
   986
    NodeIndex _node_index;
deba@165
   987
    typedef std::map<Edge, std::string> EdgeIndex;
deba@165
   988
    EdgeIndex _edge_index;
deba@165
   989
alpar@209
   990
    typedef std::vector<std::pair<std::string,
alpar@209
   991
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
alpar@209
   992
    NodeMaps _node_maps;
deba@165
   993
alpar@209
   994
    typedef std::vector<std::pair<std::string,
deba@165
   995
      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
deba@165
   996
    EdgeMaps _edge_maps;
deba@165
   997
alpar@209
   998
    typedef std::vector<std::pair<std::string,
deba@165
   999
      _writer_bits::ValueStorageBase*> > Attributes;
deba@165
  1000
    Attributes _attributes;
deba@165
  1001
deba@165
  1002
    bool _skip_nodes;
deba@165
  1003
    bool _skip_edges;
deba@165
  1004
deba@165
  1005
  public:
deba@165
  1006
deba@165
  1007
    /// \brief Constructor
deba@165
  1008
    ///
deba@165
  1009
    /// Construct a directed graph writer, which writes to the given
deba@165
  1010
    /// output stream.
alpar@209
  1011
    GraphWriter(std::ostream& is, const Graph& graph)
deba@165
  1012
      : _os(&is), local_os(false), _graph(graph),
alpar@209
  1013
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1014
deba@165
  1015
    /// \brief Constructor
deba@165
  1016
    ///
deba@165
  1017
    /// Construct a directed graph writer, which writes to the given
deba@165
  1018
    /// output file.
alpar@209
  1019
    GraphWriter(const std::string& fn, const Graph& graph)
deba@165
  1020
      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
alpar@209
  1021
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1022
deba@165
  1023
    /// \brief Constructor
deba@165
  1024
    ///
deba@165
  1025
    /// Construct a directed graph writer, which writes to the given
deba@165
  1026
    /// output file.
alpar@209
  1027
    GraphWriter(const char* fn, const Graph& graph)
deba@165
  1028
      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
alpar@209
  1029
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1030
deba@165
  1031
    /// \brief Destructor
deba@165
  1032
    ~GraphWriter() {
alpar@209
  1033
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1034
           it != _node_maps.end(); ++it) {
alpar@209
  1035
        delete it->second;
deba@165
  1036
      }
deba@165
  1037
alpar@209
  1038
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1039
           it != _edge_maps.end(); ++it) {
alpar@209
  1040
        delete it->second;
deba@165
  1041
      }
deba@165
  1042
alpar@209
  1043
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1044
           it != _attributes.end(); ++it) {
alpar@209
  1045
        delete it->second;
deba@165
  1046
      }
deba@165
  1047
deba@165
  1048
      if (local_os) {
alpar@209
  1049
        delete _os;
deba@165
  1050
      }
deba@165
  1051
    }
alpar@209
  1052
deba@190
  1053
  private:
deba@165
  1054
alpar@209
  1055
    friend GraphWriter<Graph> graphWriter<>(std::ostream& os,
alpar@209
  1056
                                            const Graph& graph);
alpar@209
  1057
    friend GraphWriter<Graph> graphWriter<>(const std::string& fn,
alpar@209
  1058
                                            const Graph& graph);
alpar@209
  1059
    friend GraphWriter<Graph> graphWriter<>(const char *fn,
alpar@209
  1060
                                            const Graph& graph);
deba@190
  1061
alpar@209
  1062
    GraphWriter(GraphWriter& other)
deba@190
  1063
      : _os(other._os), local_os(other.local_os), _graph(other._graph),
alpar@209
  1064
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
deba@190
  1065
deba@190
  1066
      other._os = 0;
deba@190
  1067
      other.local_os = false;
deba@190
  1068
deba@190
  1069
      _node_index.swap(other._node_index);
deba@190
  1070
      _edge_index.swap(other._edge_index);
deba@190
  1071
deba@190
  1072
      _node_maps.swap(other._node_maps);
deba@190
  1073
      _edge_maps.swap(other._edge_maps);
deba@190
  1074
      _attributes.swap(other._attributes);
deba@190
  1075
deba@190
  1076
      _nodes_caption = other._nodes_caption;
deba@190
  1077
      _edges_caption = other._edges_caption;
deba@190
  1078
      _attributes_caption = other._attributes_caption;
deba@190
  1079
    }
deba@190
  1080
deba@165
  1081
    GraphWriter& operator=(const GraphWriter&);
deba@165
  1082
deba@165
  1083
  public:
deba@165
  1084
deba@165
  1085
    /// \name Writing rules
deba@165
  1086
    /// @{
alpar@209
  1087
kpeter@192
  1088
    /// \brief Node map writing rule
deba@165
  1089
    ///
kpeter@192
  1090
    /// Add a node map writing rule to the writer.
deba@165
  1091
    template <typename Map>
deba@165
  1092
    GraphWriter& nodeMap(const std::string& caption, const Map& map) {
deba@165
  1093
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1094
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1095
        new _writer_bits::MapStorage<Node, Map>(map);
deba@165
  1096
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1097
      return *this;
deba@165
  1098
    }
deba@165
  1099
deba@165
  1100
    /// \brief Node map writing rule
deba@165
  1101
    ///
deba@165
  1102
    /// Add a node map writing rule with specialized converter to the
deba@165
  1103
    /// writer.
deba@165
  1104
    template <typename Map, typename Converter>
alpar@209
  1105
    GraphWriter& nodeMap(const std::string& caption, const Map& map,
alpar@209
  1106
                           const Converter& converter = Converter()) {
deba@165
  1107
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1108
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1109
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@165
  1110
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1111
      return *this;
deba@165
  1112
    }
deba@165
  1113
deba@165
  1114
    /// \brief Edge map writing rule
deba@165
  1115
    ///
deba@165
  1116
    /// Add an edge map writing rule to the writer.
deba@165
  1117
    template <typename Map>
deba@165
  1118
    GraphWriter& edgeMap(const std::string& caption, const Map& map) {
deba@165
  1119
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1120
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1121
        new _writer_bits::MapStorage<Edge, Map>(map);
deba@165
  1122
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1123
      return *this;
deba@165
  1124
    }
deba@165
  1125
deba@165
  1126
    /// \brief Edge map writing rule
deba@165
  1127
    ///
deba@165
  1128
    /// Add an edge map writing rule with specialized converter to the
deba@165
  1129
    /// writer.
deba@165
  1130
    template <typename Map, typename Converter>
alpar@209
  1131
    GraphWriter& edgeMap(const std::string& caption, const Map& map,
alpar@209
  1132
                          const Converter& converter = Converter()) {
deba@165
  1133
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1134
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1135
        new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@165
  1136
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1137
      return *this;
deba@165
  1138
    }
deba@165
  1139
deba@165
  1140
    /// \brief Arc map writing rule
deba@165
  1141
    ///
deba@165
  1142
    /// Add an arc map writing rule to the writer.
deba@165
  1143
    template <typename Map>
deba@165
  1144
    GraphWriter& arcMap(const std::string& caption, const Map& map) {
deba@165
  1145
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1146
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1147
        new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@165
  1148
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1149
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1150
        new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
deba@165
  1151
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1152
      return *this;
deba@165
  1153
    }
deba@165
  1154
deba@165
  1155
    /// \brief Arc map writing rule
deba@165
  1156
    ///
deba@165
  1157
    /// Add an arc map writing rule with specialized converter to the
deba@165
  1158
    /// writer.
deba@165
  1159
    template <typename Map, typename Converter>
alpar@209
  1160
    GraphWriter& arcMap(const std::string& caption, const Map& map,
alpar@209
  1161
                          const Converter& converter = Converter()) {
deba@165
  1162
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1163
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1164
        new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter>
alpar@209
  1165
        (_graph, map, converter);
deba@165
  1166
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1167
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1168
        new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter>
alpar@209
  1169
        (_graph, map, converter);
deba@165
  1170
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1171
      return *this;
deba@165
  1172
    }
deba@165
  1173
deba@165
  1174
    /// \brief Attribute writing rule
deba@165
  1175
    ///
deba@165
  1176
    /// Add an attribute writing rule to the writer.
deba@165
  1177
    template <typename Value>
deba@165
  1178
    GraphWriter& attribute(const std::string& caption, const Value& value) {
alpar@209
  1179
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1180
        new _writer_bits::ValueStorage<Value>(value);
deba@165
  1181
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1182
      return *this;
deba@165
  1183
    }
deba@165
  1184
deba@165
  1185
    /// \brief Attribute writing rule
deba@165
  1186
    ///
deba@165
  1187
    /// Add an attribute writing rule with specialized converter to the
deba@165
  1188
    /// writer.
deba@165
  1189
    template <typename Value, typename Converter>
alpar@209
  1190
    GraphWriter& attribute(const std::string& caption, const Value& value,
alpar@209
  1191
                             const Converter& converter = Converter()) {
alpar@209
  1192
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1193
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
deba@165
  1194
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1195
      return *this;
deba@165
  1196
    }
deba@165
  1197
deba@165
  1198
    /// \brief Node writing rule
deba@165
  1199
    ///
deba@165
  1200
    /// Add a node writing rule to the writer.
deba@165
  1201
    GraphWriter& node(const std::string& caption, const Node& node) {
deba@165
  1202
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
deba@165
  1203
      Converter converter(_node_index);
alpar@209
  1204
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1205
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
deba@165
  1206
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1207
      return *this;
deba@165
  1208
    }
deba@165
  1209
deba@165
  1210
    /// \brief Edge writing rule
deba@165
  1211
    ///
deba@165
  1212
    /// Add an edge writing rule to writer.
deba@165
  1213
    GraphWriter& edge(const std::string& caption, const Edge& edge) {
deba@165
  1214
      typedef _writer_bits::MapLookUpConverter<Edge> Converter;
deba@165
  1215
      Converter converter(_edge_index);
alpar@209
  1216
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1217
        new _writer_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@165
  1218
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1219
      return *this;
deba@165
  1220
    }
deba@165
  1221
deba@165
  1222
    /// \brief Arc writing rule
deba@165
  1223
    ///
deba@165
  1224
    /// Add an arc writing rule to writer.
deba@165
  1225
    GraphWriter& arc(const std::string& caption, const Arc& arc) {
deba@165
  1226
      typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter;
deba@165
  1227
      Converter converter(_graph, _edge_index);
alpar@209
  1228
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1229
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@165
  1230
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1231
      return *this;
deba@165
  1232
    }
deba@165
  1233
kpeter@192
  1234
    /// \name Section captions
deba@165
  1235
    /// @{
deba@165
  1236
kpeter@192
  1237
    /// \brief Add an additional caption to the \c \@nodes section
deba@165
  1238
    ///
kpeter@192
  1239
    /// Add an additional caption to the \c \@nodes section.
deba@165
  1240
    GraphWriter& nodes(const std::string& caption) {
deba@165
  1241
      _nodes_caption = caption;
deba@165
  1242
      return *this;
deba@165
  1243
    }
deba@165
  1244
kpeter@192
  1245
    /// \brief Add an additional caption to the \c \@arcs section
deba@165
  1246
    ///
kpeter@192
  1247
    /// Add an additional caption to the \c \@arcs section.
deba@165
  1248
    GraphWriter& edges(const std::string& caption) {
deba@165
  1249
      _edges_caption = caption;
deba@165
  1250
      return *this;
deba@165
  1251
    }
deba@165
  1252
kpeter@192
  1253
    /// \brief Add an additional caption to the \c \@attributes section
deba@165
  1254
    ///
kpeter@192
  1255
    /// Add an additional caption to the \c \@attributes section.
deba@165
  1256
    GraphWriter& attributes(const std::string& caption) {
deba@165
  1257
      _attributes_caption = caption;
deba@165
  1258
      return *this;
deba@165
  1259
    }
deba@165
  1260
deba@165
  1261
    /// \name Skipping section
deba@165
  1262
    /// @{
deba@165
  1263
deba@165
  1264
    /// \brief Skip writing the node set
deba@165
  1265
    ///
kpeter@192
  1266
    /// The \c \@nodes section will not be written to the stream.
deba@165
  1267
    GraphWriter& skipNodes() {
deba@165
  1268
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
deba@185
  1269
      _skip_nodes = true;
deba@165
  1270
      return *this;
deba@165
  1271
    }
deba@165
  1272
deba@165
  1273
    /// \brief Skip writing edge set
deba@165
  1274
    ///
kpeter@192
  1275
    /// The \c \@edges section will not be written to the stream.
deba@165
  1276
    GraphWriter& skipEdges() {
deba@165
  1277
      LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
deba@185
  1278
      _skip_edges = true;
deba@165
  1279
      return *this;
deba@165
  1280
    }
deba@165
  1281
deba@165
  1282
    /// @}
deba@165
  1283
deba@165
  1284
  private:
deba@165
  1285
deba@165
  1286
    void writeNodes() {
deba@165
  1287
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@165
  1288
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1289
           it != _node_maps.end(); ++it) {
deba@165
  1290
        if (it->first == "label") {
alpar@209
  1291
          label = it->second;
alpar@209
  1292
          break;
alpar@209
  1293
        }
deba@165
  1294
      }
deba@165
  1295
deba@165
  1296
      *_os << "@nodes";
deba@165
  1297
      if (!_nodes_caption.empty()) {
alpar@209
  1298
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
deba@165
  1299
      }
deba@165
  1300
      *_os << std::endl;
deba@165
  1301
deba@165
  1302
      if (label == 0) {
alpar@209
  1303
        *_os << "label" << '\t';
deba@165
  1304
      }
deba@165
  1305
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1306
           it != _node_maps.end(); ++it) {
alpar@209
  1307
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1308
      }
deba@165
  1309
      *_os << std::endl;
deba@165
  1310
deba@165
  1311
      std::vector<Node> nodes;
deba@165
  1312
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1313
        nodes.push_back(n);
deba@165
  1314
      }
alpar@209
  1315
deba@165
  1316
      if (label == 0) {
alpar@209
  1317
        IdMap<Graph, Node> id_map(_graph);
alpar@209
  1318
        _writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map);
alpar@209
  1319
        std::sort(nodes.begin(), nodes.end(), id_less);
deba@165
  1320
      } else {
alpar@209
  1321
        label->sort(nodes);
deba@165
  1322
      }
deba@165
  1323
deba@165
  1324
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
alpar@209
  1325
        Node n = nodes[i];
alpar@209
  1326
        if (label == 0) {
alpar@209
  1327
          std::ostringstream os;
alpar@209
  1328
          os << _graph.id(n);
alpar@209
  1329
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1330
          *_os << '\t';
alpar@209
  1331
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1332
        }
alpar@209
  1333
        for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1334
             it != _node_maps.end(); ++it) {
alpar@209
  1335
          std::string value = it->second->get(n);
alpar@209
  1336
          _writer_bits::writeToken(*_os, value);
alpar@209
  1337
          if (it->first == "label") {
alpar@209
  1338
            _node_index.insert(std::make_pair(n, value));
alpar@209
  1339
          }
alpar@209
  1340
          *_os << '\t';
alpar@209
  1341
        }
alpar@209
  1342
        *_os << std::endl;
deba@165
  1343
      }
deba@165
  1344
    }
deba@165
  1345
deba@185
  1346
    void createNodeIndex() {
deba@185
  1347
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@185
  1348
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1349
           it != _node_maps.end(); ++it) {
deba@185
  1350
        if (it->first == "label") {
alpar@209
  1351
          label = it->second;
alpar@209
  1352
          break;
alpar@209
  1353
        }
deba@185
  1354
      }
deba@185
  1355
deba@185
  1356
      if (label == 0) {
alpar@209
  1357
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1358
          std::ostringstream os;
alpar@209
  1359
          os << _graph.id(n);
alpar@209
  1360
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1361
        }
deba@185
  1362
      } else {
alpar@209
  1363
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1364
          std::string value = label->get(n);
alpar@209
  1365
          _node_index.insert(std::make_pair(n, value));
alpar@209
  1366
        }
deba@185
  1367
      }
deba@185
  1368
    }
deba@185
  1369
deba@165
  1370
    void writeEdges() {
deba@165
  1371
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@165
  1372
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1373
           it != _edge_maps.end(); ++it) {
deba@165
  1374
        if (it->first == "label") {
alpar@209
  1375
          label = it->second;
alpar@209
  1376
          break;
alpar@209
  1377
        }
deba@165
  1378
      }
deba@165
  1379
deba@165
  1380
      *_os << "@edges";
deba@165
  1381
      if (!_edges_caption.empty()) {
alpar@209
  1382
        _writer_bits::writeToken(*_os << ' ', _edges_caption);
deba@165
  1383
      }
deba@165
  1384
      *_os << std::endl;
deba@165
  1385
deba@165
  1386
      *_os << '\t' << '\t';
deba@165
  1387
      if (label == 0) {
alpar@209
  1388
        *_os << "label" << '\t';
deba@165
  1389
      }
deba@165
  1390
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1391
           it != _edge_maps.end(); ++it) {
alpar@209
  1392
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1393
      }
deba@165
  1394
      *_os << std::endl;
deba@165
  1395
deba@165
  1396
      std::vector<Edge> edges;
deba@165
  1397
      for (EdgeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1398
        edges.push_back(n);
deba@165
  1399
      }
alpar@209
  1400
deba@165
  1401
      if (label == 0) {
alpar@209
  1402
        IdMap<Graph, Edge> id_map(_graph);
alpar@209
  1403
        _writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map);
alpar@209
  1404
        std::sort(edges.begin(), edges.end(), id_less);
deba@165
  1405
      } else {
alpar@209
  1406
        label->sort(edges);
deba@165
  1407
      }
deba@165
  1408
deba@165
  1409
      for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
alpar@209
  1410
        Edge e = edges[i];
alpar@209
  1411
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1412
                                 find(_graph.u(e))->second);
alpar@209
  1413
        *_os << '\t';
alpar@209
  1414
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1415
                                 find(_graph.v(e))->second);
alpar@209
  1416
        *_os << '\t';
alpar@209
  1417
        if (label == 0) {
alpar@209
  1418
          std::ostringstream os;
alpar@209
  1419
          os << _graph.id(e);
alpar@209
  1420
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1421
          *_os << '\t';
alpar@209
  1422
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1423
        }
alpar@209
  1424
        for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1425
             it != _edge_maps.end(); ++it) {
alpar@209
  1426
          std::string value = it->second->get(e);
alpar@209
  1427
          _writer_bits::writeToken(*_os, value);
alpar@209
  1428
          if (it->first == "label") {
alpar@209
  1429
            _edge_index.insert(std::make_pair(e, value));
alpar@209
  1430
          }
alpar@209
  1431
          *_os << '\t';
alpar@209
  1432
        }
alpar@209
  1433
        *_os << std::endl;
deba@165
  1434
      }
deba@165
  1435
    }
deba@165
  1436
deba@185
  1437
    void createEdgeIndex() {
deba@185
  1438
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@185
  1439
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1440
           it != _edge_maps.end(); ++it) {
deba@185
  1441
        if (it->first == "label") {
alpar@209
  1442
          label = it->second;
alpar@209
  1443
          break;
alpar@209
  1444
        }
deba@185
  1445
      }
deba@185
  1446
deba@185
  1447
      if (label == 0) {
alpar@209
  1448
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1449
          std::ostringstream os;
alpar@209
  1450
          os << _graph.id(e);
alpar@209
  1451
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1452
        }
deba@185
  1453
      } else {
alpar@209
  1454
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1455
          std::string value = label->get(e);
alpar@209
  1456
          _edge_index.insert(std::make_pair(e, value));
alpar@209
  1457
        }
deba@185
  1458
      }
deba@185
  1459
    }
deba@185
  1460
deba@165
  1461
    void writeAttributes() {
deba@165
  1462
      if (_attributes.empty()) return;
deba@165
  1463
      *_os << "@attributes";
deba@165
  1464
      if (!_attributes_caption.empty()) {
alpar@209
  1465
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
deba@165
  1466
      }
deba@165
  1467
      *_os << std::endl;
deba@165
  1468
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1469
           it != _attributes.end(); ++it) {
alpar@209
  1470
        _writer_bits::writeToken(*_os, it->first) << ' ';
alpar@209
  1471
        _writer_bits::writeToken(*_os, it->second->get());
alpar@209
  1472
        *_os << std::endl;
deba@165
  1473
      }
deba@165
  1474
    }
alpar@209
  1475
deba@165
  1476
  public:
alpar@209
  1477
alpar@209
  1478
    /// \name Execution of the writer
deba@165
  1479
    /// @{
deba@165
  1480
deba@165
  1481
    /// \brief Start the batch processing
deba@165
  1482
    ///
kpeter@192
  1483
    /// This function starts the batch processing.
deba@165
  1484
    void run() {
deba@165
  1485
      if (!_skip_nodes) {
alpar@209
  1486
        writeNodes();
deba@185
  1487
      } else {
alpar@209
  1488
        createNodeIndex();
deba@165
  1489
      }
alpar@209
  1490
      if (!_skip_edges) {
alpar@209
  1491
        writeEdges();
deba@185
  1492
      } else {
alpar@209
  1493
        createEdgeIndex();
deba@165
  1494
      }
deba@165
  1495
      writeAttributes();
deba@165
  1496
    }
deba@165
  1497
kpeter@192
  1498
    /// \brief Give back the stream of the writer
deba@165
  1499
    ///
kpeter@192
  1500
    /// Give back the stream of the writer
deba@165
  1501
    std::ostream& ostream() {
deba@165
  1502
      return *_os;
deba@165
  1503
    }
deba@165
  1504
deba@165
  1505
    /// @}
deba@165
  1506
  };
deba@165
  1507
kpeter@192
  1508
  /// \brief Return a \ref GraphWriter class
alpar@209
  1509
  ///
kpeter@192
  1510
  /// This function just returns a \ref GraphWriter class.
deba@165
  1511
  /// \relates GraphWriter
deba@165
  1512
  template <typename Graph>
deba@190
  1513
  GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
deba@165
  1514
    GraphWriter<Graph> tmp(os, graph);
deba@165
  1515
    return tmp;
deba@165
  1516
  }
deba@165
  1517
kpeter@192
  1518
  /// \brief Return a \ref GraphWriter class
alpar@209
  1519
  ///
kpeter@192
  1520
  /// This function just returns a \ref GraphWriter class.
deba@165
  1521
  /// \relates GraphWriter
deba@165
  1522
  template <typename Graph>
deba@190
  1523
  GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
deba@165
  1524
    GraphWriter<Graph> tmp(fn, graph);
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>
deba@190
  1533
  GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
deba@165
  1534
    GraphWriter<Graph> tmp(fn, graph);
deba@165
  1535
    return tmp;
deba@165
  1536
  }
deba@248
  1537
deba@248
  1538
  class SectionWriter;
deba@248
  1539
deba@248
  1540
  SectionWriter sectionWriter(std::istream& is);
deba@248
  1541
  SectionWriter sectionWriter(const std::string& fn);
deba@248
  1542
  SectionWriter sectionWriter(const char* fn);
deba@248
  1543
deba@248
  1544
  /// \ingroup lemon_io
deba@248
  1545
  ///
deba@248
  1546
  /// \brief Section writer class
deba@248
  1547
  ///
deba@248
  1548
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
deba@248
  1549
  /// which contain any data in arbitrary format. Such sections can be
deba@248
  1550
  /// written with this class. A writing rule can be added to the
deba@248
  1551
  /// class with two different functions. With the \c sectionLines()
deba@248
  1552
  /// function a generator can write the section line-by-line, while
deba@248
  1553
  /// with the \c sectionStream() member the section can be written to
deba@248
  1554
  /// an output stream.
deba@248
  1555
  class SectionWriter {
deba@248
  1556
  private:
deba@248
  1557
deba@248
  1558
    std::ostream* _os;
deba@248
  1559
    bool local_os;
deba@248
  1560
deba@248
  1561
    typedef std::vector<std::pair<std::string, _writer_bits::Section*> >
deba@248
  1562
    Sections;
deba@248
  1563
deba@248
  1564
    Sections _sections;
deba@248
  1565
deba@248
  1566
  public:
deba@248
  1567
deba@248
  1568
    /// \brief Constructor
deba@248
  1569
    ///
deba@248
  1570
    /// Construct a section writer, which writes to the given output
deba@248
  1571
    /// stream.
deba@248
  1572
    SectionWriter(std::ostream& os)
deba@248
  1573
      : _os(&os), local_os(false) {}
deba@248
  1574
deba@248
  1575
    /// \brief Constructor
deba@248
  1576
    ///
deba@248
  1577
    /// Construct a section writer, which writes into the given file.
deba@248
  1578
    SectionWriter(const std::string& fn)
deba@248
  1579
      : _os(new std::ofstream(fn.c_str())), local_os(true) {}
deba@248
  1580
deba@248
  1581
    /// \brief Constructor
deba@248
  1582
    ///
deba@248
  1583
    /// Construct a section writer, which writes into the given file.
deba@248
  1584
    SectionWriter(const char* fn)
deba@248
  1585
      : _os(new std::ofstream(fn)), local_os(true) {}
deba@248
  1586
deba@248
  1587
    /// \brief Destructor
deba@248
  1588
    ~SectionWriter() {
deba@248
  1589
      for (Sections::iterator it = _sections.begin();
deba@248
  1590
           it != _sections.end(); ++it) {
deba@248
  1591
        delete it->second;
deba@248
  1592
      }
deba@248
  1593
deba@248
  1594
      if (local_os) {
deba@248
  1595
        delete _os;
deba@248
  1596
      }
deba@248
  1597
deba@248
  1598
    }
deba@248
  1599
deba@248
  1600
  private:
deba@248
  1601
deba@248
  1602
    friend SectionWriter sectionWriter(std::ostream& os);
deba@248
  1603
    friend SectionWriter sectionWriter(const std::string& fn);
deba@248
  1604
    friend SectionWriter sectionWriter(const char* fn);
deba@248
  1605
deba@248
  1606
    SectionWriter(SectionWriter& other)
deba@248
  1607
      : _os(other._os), local_os(other.local_os) {
deba@248
  1608
deba@248
  1609
      other._os = 0;
deba@248
  1610
      other.local_os = false;
deba@248
  1611
deba@248
  1612
      _sections.swap(other._sections);
deba@248
  1613
    }
deba@248
  1614
deba@248
  1615
    SectionWriter& operator=(const SectionWriter&);
deba@248
  1616
deba@248
  1617
  public:
deba@248
  1618
deba@248
  1619
    /// \name Section writers
deba@248
  1620
    /// @{
deba@248
  1621
deba@248
  1622
    /// \brief Add a section writer with line oriented writing
deba@248
  1623
    ///
deba@248
  1624
    /// The first parameter is the type descriptor of the section, the
deba@248
  1625
    /// second is a generator with std::string values. At the writing
deba@248
  1626
    /// process, the returned \c std::string will be written into the
deba@248
  1627
    /// output file until it is an empty string.
deba@248
  1628
    ///
deba@248
  1629
    /// For example, an integer vector is written into a section.
deba@248
  1630
    ///\code
deba@248
  1631
    ///  @numbers
deba@248
  1632
    ///  12 45 23 78
deba@248
  1633
    ///  4 28 38 28
deba@248
  1634
    ///  23 6 16
deba@248
  1635
    ///\endcode
deba@248
  1636
    ///
deba@248
  1637
    /// The generator is implemented as a struct.
deba@248
  1638
    ///\code
deba@248
  1639
    ///  struct NumberSection {
deba@248
  1640
    ///    std::vector<int>::const_iterator _it, _end;
deba@248
  1641
    ///    NumberSection(const std::vector<int>& data)
deba@248
  1642
    ///      : _it(data.begin()), _end(data.end()) {}
deba@248
  1643
    ///    std::string operator()() {
deba@248
  1644
    ///      int rem_in_line = 4;
deba@248
  1645
    ///      std::ostringstream ls;
deba@248
  1646
    ///      while (rem_in_line > 0 && _it != _end) {
deba@248
  1647
    ///        ls << *(_it++) << ' ';
deba@248
  1648
    ///        --rem_in_line;
deba@248
  1649
    ///      }
deba@248
  1650
    ///      return ls.str();
deba@248
  1651
    ///    }
deba@248
  1652
    ///  };
deba@248
  1653
    ///
deba@248
  1654
    ///  // ...
deba@248
  1655
    ///
deba@248
  1656
    ///  writer.sectionLines("numbers", NumberSection(vec));
deba@248
  1657
    ///\endcode
deba@248
  1658
    template <typename Functor>
deba@248
  1659
    SectionWriter& sectionLines(const std::string& type, Functor functor) {
deba@248
  1660
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1661
      _sections.push_back(std::make_pair(type,
deba@248
  1662
        new _writer_bits::LineSection<Functor>(functor)));
deba@248
  1663
      return *this;
deba@248
  1664
    }
deba@248
  1665
deba@248
  1666
deba@248
  1667
    /// \brief Add a section writer with stream oriented writing
deba@248
  1668
    ///
deba@248
  1669
    /// The first parameter is the type of the section, the second is
deba@248
  1670
    /// a functor, which takes a \c std::ostream& parameter. The
deba@248
  1671
    /// functor writes the section to the output stream.
deba@248
  1672
    /// \warning The last line must be closed with end-line character.
deba@248
  1673
    template <typename Functor>
deba@248
  1674
    SectionWriter& sectionStream(const std::string& type, Functor functor) {
deba@248
  1675
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1676
      _sections.push_back(std::make_pair(type,
deba@248
  1677
         new _writer_bits::StreamSection<Functor>(functor)));
deba@248
  1678
      return *this;
deba@248
  1679
    }
deba@248
  1680
deba@248
  1681
    /// @}
deba@248
  1682
deba@248
  1683
  public:
deba@248
  1684
deba@248
  1685
deba@248
  1686
    /// \name Execution of the writer
deba@248
  1687
    /// @{
deba@248
  1688
deba@248
  1689
    /// \brief Start the batch processing
deba@248
  1690
    ///
deba@248
  1691
    /// This function starts the batch processing.
deba@248
  1692
    void run() {
deba@248
  1693
deba@248
  1694
      LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer");
deba@248
  1695
deba@248
  1696
      for (Sections::iterator it = _sections.begin();
deba@248
  1697
           it != _sections.end(); ++it) {
deba@248
  1698
        (*_os) << '@' << it->first << std::endl;
deba@248
  1699
        it->second->process(*_os);
deba@248
  1700
      }
deba@248
  1701
    }
deba@248
  1702
deba@248
  1703
    /// \brief Give back the stream of the writer
deba@248
  1704
    ///
deba@248
  1705
    /// Returns the stream of the writer
deba@248
  1706
    std::ostream& ostream() {
deba@248
  1707
      return *_os;
deba@248
  1708
    }
deba@248
  1709
deba@248
  1710
    /// @}
deba@248
  1711
deba@248
  1712
  };
deba@248
  1713
deba@248
  1714
  /// \brief Return a \ref SectionWriter class
deba@248
  1715
  ///
deba@248
  1716
  /// This function just returns a \ref SectionWriter class.
deba@248
  1717
  /// \relates SectionWriter
deba@248
  1718
  inline SectionWriter sectionWriter(std::ostream& os) {
deba@248
  1719
    SectionWriter tmp(os);
deba@248
  1720
    return tmp;
deba@248
  1721
  }
deba@248
  1722
deba@248
  1723
  /// \brief Return a \ref SectionWriter class
deba@248
  1724
  ///
deba@248
  1725
  /// This function just returns a \ref SectionWriter class.
deba@248
  1726
  /// \relates SectionWriter
deba@248
  1727
  inline SectionWriter sectionWriter(const std::string& fn) {
deba@248
  1728
    SectionWriter tmp(fn);
deba@248
  1729
    return tmp;
deba@248
  1730
  }
deba@248
  1731
deba@248
  1732
  /// \brief Return a \ref SectionWriter class
deba@248
  1733
  ///
deba@248
  1734
  /// This function just returns a \ref SectionWriter class.
deba@248
  1735
  /// \relates SectionWriter
deba@248
  1736
  inline SectionWriter sectionWriter(const char* fn) {
deba@248
  1737
    SectionWriter tmp(fn);
deba@248
  1738
    return tmp;
deba@248
  1739
  }
deba@127
  1740
}
deba@127
  1741
deba@127
  1742
#endif