lemon/lgf_writer.h
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 01 Oct 2008 12:44:16 +0200
changeset 293 47fbc814aa31
parent 248 8fada33fc60a
child 294 cbe3ec2d59d2
permissions -rw-r--r--
Change the parameter order in LGF reader and writer tools
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>
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),
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.
kpeter@293
   471
    DigraphWriter(const Digraph& digraph, const char* fn)
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
kpeter@293
   499
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   500
                                                  std::ostream& os);
kpeter@293
   501
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   502
                                                  const std::string& fn);
kpeter@293
   503
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
kpeter@293
   504
                                                  const char *fn);
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>
kpeter@293
   911
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   912
                                       std::ostream& os = std::cout) {
kpeter@293
   913
    DigraphWriter<Digraph> tmp(digraph, os);
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>
kpeter@293
   922
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   923
                                       const std::string& fn) {
kpeter@293
   924
    DigraphWriter<Digraph> tmp(digraph, fn);
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>
kpeter@293
   933
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
kpeter@293
   934
                                       const char* fn) {
kpeter@293
   935
    DigraphWriter<Digraph> tmp(digraph, fn);
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>
kpeter@293
   943
  GraphWriter<Graph> graphWriter(const Graph& graph,
kpeter@293
   944
                                 std::ostream& os = std::cout);
deba@190
   945
deba@190
   946
  template <typename Graph>
kpeter@293
   947
  GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn);
deba@190
   948
deba@190
   949
  template <typename Graph>
kpeter@293
   950
  GraphWriter<Graph> graphWriter(const Graph& graph, const char *fn);
deba@190
   951
deba@165
   952
  /// \ingroup lemon_io
alpar@209
   953
  ///
kpeter@192
   954
  /// \brief \ref lgf-format "LGF" writer for directed graphs
deba@165
   955
  ///
deba@165
   956
  /// This utility writes an \ref lgf-format "LGF" file.
kpeter@192
   957
  ///
kpeter@192
   958
  /// It can be used almost the same way as \c DigraphWriter.
kpeter@192
   959
  /// The only difference is that this class can handle edges and
kpeter@192
   960
  /// edge maps as well as arcs and arc maps.
deba@201
   961
  ///
deba@201
   962
  /// The arc maps are written into the file as two columns, the
deba@201
   963
  /// caption of the columns are the name of the map prefixed with \c
deba@201
   964
  /// '+' and \c '-'. The arcs are written into the \c \@attributes
deba@201
   965
  /// section as a \c '+' or a \c '-' prefix (depends on the direction
deba@201
   966
  /// of the arc) and the label of corresponding edge.
deba@165
   967
  template <typename _Graph>
deba@165
   968
  class GraphWriter {
deba@165
   969
  public:
deba@165
   970
deba@165
   971
    typedef _Graph Graph;
deba@165
   972
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
alpar@209
   973
deba@165
   974
  private:
deba@165
   975
deba@165
   976
deba@165
   977
    std::ostream* _os;
deba@165
   978
    bool local_os;
deba@165
   979
deba@237
   980
    const Graph& _graph;
deba@165
   981
deba@165
   982
    std::string _nodes_caption;
deba@165
   983
    std::string _edges_caption;
deba@165
   984
    std::string _attributes_caption;
alpar@209
   985
deba@165
   986
    typedef std::map<Node, std::string> NodeIndex;
deba@165
   987
    NodeIndex _node_index;
deba@165
   988
    typedef std::map<Edge, std::string> EdgeIndex;
deba@165
   989
    EdgeIndex _edge_index;
deba@165
   990
alpar@209
   991
    typedef std::vector<std::pair<std::string,
alpar@209
   992
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
alpar@209
   993
    NodeMaps _node_maps;
deba@165
   994
alpar@209
   995
    typedef std::vector<std::pair<std::string,
deba@165
   996
      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
deba@165
   997
    EdgeMaps _edge_maps;
deba@165
   998
alpar@209
   999
    typedef std::vector<std::pair<std::string,
deba@165
  1000
      _writer_bits::ValueStorageBase*> > Attributes;
deba@165
  1001
    Attributes _attributes;
deba@165
  1002
deba@165
  1003
    bool _skip_nodes;
deba@165
  1004
    bool _skip_edges;
deba@165
  1005
deba@165
  1006
  public:
deba@165
  1007
deba@165
  1008
    /// \brief Constructor
deba@165
  1009
    ///
deba@165
  1010
    /// Construct a directed graph writer, which writes to the given
deba@165
  1011
    /// output stream.
kpeter@293
  1012
    GraphWriter(const Graph& graph, std::ostream& os = std::cout)
kpeter@293
  1013
      : _os(&os), local_os(false), _graph(graph),
alpar@209
  1014
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1015
deba@165
  1016
    /// \brief Constructor
deba@165
  1017
    ///
deba@165
  1018
    /// Construct a directed graph writer, which writes to the given
deba@165
  1019
    /// output file.
kpeter@293
  1020
    GraphWriter(const Graph& graph, const std::string& fn)
deba@165
  1021
      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
alpar@209
  1022
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1023
deba@165
  1024
    /// \brief Constructor
deba@165
  1025
    ///
deba@165
  1026
    /// Construct a directed graph writer, which writes to the given
deba@165
  1027
    /// output file.
kpeter@293
  1028
    GraphWriter(const Graph& graph, const char* fn)
deba@165
  1029
      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
alpar@209
  1030
        _skip_nodes(false), _skip_edges(false) {}
deba@165
  1031
deba@165
  1032
    /// \brief Destructor
deba@165
  1033
    ~GraphWriter() {
alpar@209
  1034
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1035
           it != _node_maps.end(); ++it) {
alpar@209
  1036
        delete it->second;
deba@165
  1037
      }
deba@165
  1038
alpar@209
  1039
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1040
           it != _edge_maps.end(); ++it) {
alpar@209
  1041
        delete it->second;
deba@165
  1042
      }
deba@165
  1043
alpar@209
  1044
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1045
           it != _attributes.end(); ++it) {
alpar@209
  1046
        delete it->second;
deba@165
  1047
      }
deba@165
  1048
deba@165
  1049
      if (local_os) {
alpar@209
  1050
        delete _os;
deba@165
  1051
      }
deba@165
  1052
    }
alpar@209
  1053
deba@190
  1054
  private:
deba@165
  1055
kpeter@293
  1056
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1057
                                            std::ostream& os);
kpeter@293
  1058
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1059
                                            const std::string& fn);
kpeter@293
  1060
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
kpeter@293
  1061
                                            const char *fn);
deba@190
  1062
alpar@209
  1063
    GraphWriter(GraphWriter& other)
deba@190
  1064
      : _os(other._os), local_os(other.local_os), _graph(other._graph),
alpar@209
  1065
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
deba@190
  1066
deba@190
  1067
      other._os = 0;
deba@190
  1068
      other.local_os = false;
deba@190
  1069
deba@190
  1070
      _node_index.swap(other._node_index);
deba@190
  1071
      _edge_index.swap(other._edge_index);
deba@190
  1072
deba@190
  1073
      _node_maps.swap(other._node_maps);
deba@190
  1074
      _edge_maps.swap(other._edge_maps);
deba@190
  1075
      _attributes.swap(other._attributes);
deba@190
  1076
deba@190
  1077
      _nodes_caption = other._nodes_caption;
deba@190
  1078
      _edges_caption = other._edges_caption;
deba@190
  1079
      _attributes_caption = other._attributes_caption;
deba@190
  1080
    }
deba@190
  1081
deba@165
  1082
    GraphWriter& operator=(const GraphWriter&);
deba@165
  1083
deba@165
  1084
  public:
deba@165
  1085
deba@165
  1086
    /// \name Writing rules
deba@165
  1087
    /// @{
alpar@209
  1088
kpeter@192
  1089
    /// \brief Node map writing rule
deba@165
  1090
    ///
kpeter@192
  1091
    /// Add a node map writing rule to the writer.
deba@165
  1092
    template <typename Map>
deba@165
  1093
    GraphWriter& nodeMap(const std::string& caption, const Map& map) {
deba@165
  1094
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1095
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1096
        new _writer_bits::MapStorage<Node, Map>(map);
deba@165
  1097
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1098
      return *this;
deba@165
  1099
    }
deba@165
  1100
deba@165
  1101
    /// \brief Node map writing rule
deba@165
  1102
    ///
deba@165
  1103
    /// Add a node map writing rule with specialized converter to the
deba@165
  1104
    /// writer.
deba@165
  1105
    template <typename Map, typename Converter>
alpar@209
  1106
    GraphWriter& nodeMap(const std::string& caption, const Map& map,
alpar@209
  1107
                           const Converter& converter = Converter()) {
deba@165
  1108
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
alpar@209
  1109
      _writer_bits::MapStorageBase<Node>* storage =
alpar@209
  1110
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@165
  1111
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1112
      return *this;
deba@165
  1113
    }
deba@165
  1114
deba@165
  1115
    /// \brief Edge map writing rule
deba@165
  1116
    ///
deba@165
  1117
    /// Add an edge map writing rule to the writer.
deba@165
  1118
    template <typename Map>
deba@165
  1119
    GraphWriter& edgeMap(const std::string& caption, const Map& map) {
deba@165
  1120
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1121
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1122
        new _writer_bits::MapStorage<Edge, Map>(map);
deba@165
  1123
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1124
      return *this;
deba@165
  1125
    }
deba@165
  1126
deba@165
  1127
    /// \brief Edge map writing rule
deba@165
  1128
    ///
deba@165
  1129
    /// Add an edge map writing rule with specialized converter to the
deba@165
  1130
    /// writer.
deba@165
  1131
    template <typename Map, typename Converter>
alpar@209
  1132
    GraphWriter& edgeMap(const std::string& caption, const Map& map,
alpar@209
  1133
                          const Converter& converter = Converter()) {
deba@165
  1134
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
alpar@209
  1135
      _writer_bits::MapStorageBase<Edge>* storage =
alpar@209
  1136
        new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@165
  1137
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1138
      return *this;
deba@165
  1139
    }
deba@165
  1140
deba@165
  1141
    /// \brief Arc map writing rule
deba@165
  1142
    ///
deba@165
  1143
    /// Add an arc map writing rule to the writer.
deba@165
  1144
    template <typename Map>
deba@165
  1145
    GraphWriter& arcMap(const std::string& caption, const Map& map) {
deba@165
  1146
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1147
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1148
        new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@165
  1149
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1150
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1151
        new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
deba@165
  1152
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1153
      return *this;
deba@165
  1154
    }
deba@165
  1155
deba@165
  1156
    /// \brief Arc map writing rule
deba@165
  1157
    ///
deba@165
  1158
    /// Add an arc map writing rule with specialized converter to the
deba@165
  1159
    /// writer.
deba@165
  1160
    template <typename Map, typename Converter>
alpar@209
  1161
    GraphWriter& arcMap(const std::string& caption, const Map& map,
alpar@209
  1162
                          const Converter& converter = Converter()) {
deba@165
  1163
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
alpar@209
  1164
      _writer_bits::MapStorageBase<Edge>* forward_storage =
alpar@209
  1165
        new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter>
alpar@209
  1166
        (_graph, map, converter);
deba@165
  1167
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
alpar@209
  1168
      _writer_bits::MapStorageBase<Edge>* backward_storage =
alpar@209
  1169
        new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter>
alpar@209
  1170
        (_graph, map, converter);
deba@165
  1171
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1172
      return *this;
deba@165
  1173
    }
deba@165
  1174
deba@165
  1175
    /// \brief Attribute writing rule
deba@165
  1176
    ///
deba@165
  1177
    /// Add an attribute writing rule to the writer.
deba@165
  1178
    template <typename Value>
deba@165
  1179
    GraphWriter& attribute(const std::string& caption, const Value& value) {
alpar@209
  1180
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1181
        new _writer_bits::ValueStorage<Value>(value);
deba@165
  1182
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1183
      return *this;
deba@165
  1184
    }
deba@165
  1185
deba@165
  1186
    /// \brief Attribute writing rule
deba@165
  1187
    ///
deba@165
  1188
    /// Add an attribute writing rule with specialized converter to the
deba@165
  1189
    /// writer.
deba@165
  1190
    template <typename Value, typename Converter>
alpar@209
  1191
    GraphWriter& attribute(const std::string& caption, const Value& value,
alpar@209
  1192
                             const Converter& converter = Converter()) {
alpar@209
  1193
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1194
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
deba@165
  1195
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1196
      return *this;
deba@165
  1197
    }
deba@165
  1198
deba@165
  1199
    /// \brief Node writing rule
deba@165
  1200
    ///
deba@165
  1201
    /// Add a node writing rule to the writer.
deba@165
  1202
    GraphWriter& node(const std::string& caption, const Node& node) {
deba@165
  1203
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
deba@165
  1204
      Converter converter(_node_index);
alpar@209
  1205
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1206
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
deba@165
  1207
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1208
      return *this;
deba@165
  1209
    }
deba@165
  1210
deba@165
  1211
    /// \brief Edge writing rule
deba@165
  1212
    ///
deba@165
  1213
    /// Add an edge writing rule to writer.
deba@165
  1214
    GraphWriter& edge(const std::string& caption, const Edge& edge) {
deba@165
  1215
      typedef _writer_bits::MapLookUpConverter<Edge> Converter;
deba@165
  1216
      Converter converter(_edge_index);
alpar@209
  1217
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1218
        new _writer_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@165
  1219
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1220
      return *this;
deba@165
  1221
    }
deba@165
  1222
deba@165
  1223
    /// \brief Arc writing rule
deba@165
  1224
    ///
deba@165
  1225
    /// Add an arc writing rule to writer.
deba@165
  1226
    GraphWriter& arc(const std::string& caption, const Arc& arc) {
deba@165
  1227
      typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter;
deba@165
  1228
      Converter converter(_graph, _edge_index);
alpar@209
  1229
      _writer_bits::ValueStorageBase* storage =
alpar@209
  1230
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@165
  1231
      _attributes.push_back(std::make_pair(caption, storage));
deba@165
  1232
      return *this;
deba@165
  1233
    }
deba@165
  1234
kpeter@192
  1235
    /// \name Section captions
deba@165
  1236
    /// @{
deba@165
  1237
kpeter@192
  1238
    /// \brief Add an additional caption to the \c \@nodes section
deba@165
  1239
    ///
kpeter@192
  1240
    /// Add an additional caption to the \c \@nodes section.
deba@165
  1241
    GraphWriter& nodes(const std::string& caption) {
deba@165
  1242
      _nodes_caption = caption;
deba@165
  1243
      return *this;
deba@165
  1244
    }
deba@165
  1245
kpeter@192
  1246
    /// \brief Add an additional caption to the \c \@arcs section
deba@165
  1247
    ///
kpeter@192
  1248
    /// Add an additional caption to the \c \@arcs section.
deba@165
  1249
    GraphWriter& edges(const std::string& caption) {
deba@165
  1250
      _edges_caption = caption;
deba@165
  1251
      return *this;
deba@165
  1252
    }
deba@165
  1253
kpeter@192
  1254
    /// \brief Add an additional caption to the \c \@attributes section
deba@165
  1255
    ///
kpeter@192
  1256
    /// Add an additional caption to the \c \@attributes section.
deba@165
  1257
    GraphWriter& attributes(const std::string& caption) {
deba@165
  1258
      _attributes_caption = caption;
deba@165
  1259
      return *this;
deba@165
  1260
    }
deba@165
  1261
deba@165
  1262
    /// \name Skipping section
deba@165
  1263
    /// @{
deba@165
  1264
deba@165
  1265
    /// \brief Skip writing the node set
deba@165
  1266
    ///
kpeter@192
  1267
    /// The \c \@nodes section will not be written to the stream.
deba@165
  1268
    GraphWriter& skipNodes() {
deba@165
  1269
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
deba@185
  1270
      _skip_nodes = true;
deba@165
  1271
      return *this;
deba@165
  1272
    }
deba@165
  1273
deba@165
  1274
    /// \brief Skip writing edge set
deba@165
  1275
    ///
kpeter@192
  1276
    /// The \c \@edges section will not be written to the stream.
deba@165
  1277
    GraphWriter& skipEdges() {
deba@165
  1278
      LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
deba@185
  1279
      _skip_edges = true;
deba@165
  1280
      return *this;
deba@165
  1281
    }
deba@165
  1282
deba@165
  1283
    /// @}
deba@165
  1284
deba@165
  1285
  private:
deba@165
  1286
deba@165
  1287
    void writeNodes() {
deba@165
  1288
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@165
  1289
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1290
           it != _node_maps.end(); ++it) {
deba@165
  1291
        if (it->first == "label") {
alpar@209
  1292
          label = it->second;
alpar@209
  1293
          break;
alpar@209
  1294
        }
deba@165
  1295
      }
deba@165
  1296
deba@165
  1297
      *_os << "@nodes";
deba@165
  1298
      if (!_nodes_caption.empty()) {
alpar@209
  1299
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
deba@165
  1300
      }
deba@165
  1301
      *_os << std::endl;
deba@165
  1302
deba@165
  1303
      if (label == 0) {
alpar@209
  1304
        *_os << "label" << '\t';
deba@165
  1305
      }
deba@165
  1306
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1307
           it != _node_maps.end(); ++it) {
alpar@209
  1308
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1309
      }
deba@165
  1310
      *_os << std::endl;
deba@165
  1311
deba@165
  1312
      std::vector<Node> nodes;
deba@165
  1313
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1314
        nodes.push_back(n);
deba@165
  1315
      }
alpar@209
  1316
deba@165
  1317
      if (label == 0) {
alpar@209
  1318
        IdMap<Graph, Node> id_map(_graph);
alpar@209
  1319
        _writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map);
alpar@209
  1320
        std::sort(nodes.begin(), nodes.end(), id_less);
deba@165
  1321
      } else {
alpar@209
  1322
        label->sort(nodes);
deba@165
  1323
      }
deba@165
  1324
deba@165
  1325
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
alpar@209
  1326
        Node n = nodes[i];
alpar@209
  1327
        if (label == 0) {
alpar@209
  1328
          std::ostringstream os;
alpar@209
  1329
          os << _graph.id(n);
alpar@209
  1330
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1331
          *_os << '\t';
alpar@209
  1332
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1333
        }
alpar@209
  1334
        for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1335
             it != _node_maps.end(); ++it) {
alpar@209
  1336
          std::string value = it->second->get(n);
alpar@209
  1337
          _writer_bits::writeToken(*_os, value);
alpar@209
  1338
          if (it->first == "label") {
alpar@209
  1339
            _node_index.insert(std::make_pair(n, value));
alpar@209
  1340
          }
alpar@209
  1341
          *_os << '\t';
alpar@209
  1342
        }
alpar@209
  1343
        *_os << std::endl;
deba@165
  1344
      }
deba@165
  1345
    }
deba@165
  1346
deba@185
  1347
    void createNodeIndex() {
deba@185
  1348
      _writer_bits::MapStorageBase<Node>* label = 0;
deba@185
  1349
      for (typename NodeMaps::iterator it = _node_maps.begin();
alpar@209
  1350
           it != _node_maps.end(); ++it) {
deba@185
  1351
        if (it->first == "label") {
alpar@209
  1352
          label = it->second;
alpar@209
  1353
          break;
alpar@209
  1354
        }
deba@185
  1355
      }
deba@185
  1356
deba@185
  1357
      if (label == 0) {
alpar@209
  1358
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1359
          std::ostringstream os;
alpar@209
  1360
          os << _graph.id(n);
alpar@209
  1361
          _node_index.insert(std::make_pair(n, os.str()));
alpar@209
  1362
        }
deba@185
  1363
      } else {
alpar@209
  1364
        for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1365
          std::string value = label->get(n);
alpar@209
  1366
          _node_index.insert(std::make_pair(n, value));
alpar@209
  1367
        }
deba@185
  1368
      }
deba@185
  1369
    }
deba@185
  1370
deba@165
  1371
    void writeEdges() {
deba@165
  1372
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@165
  1373
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1374
           it != _edge_maps.end(); ++it) {
deba@165
  1375
        if (it->first == "label") {
alpar@209
  1376
          label = it->second;
alpar@209
  1377
          break;
alpar@209
  1378
        }
deba@165
  1379
      }
deba@165
  1380
deba@165
  1381
      *_os << "@edges";
deba@165
  1382
      if (!_edges_caption.empty()) {
alpar@209
  1383
        _writer_bits::writeToken(*_os << ' ', _edges_caption);
deba@165
  1384
      }
deba@165
  1385
      *_os << std::endl;
deba@165
  1386
deba@165
  1387
      *_os << '\t' << '\t';
deba@165
  1388
      if (label == 0) {
alpar@209
  1389
        *_os << "label" << '\t';
deba@165
  1390
      }
deba@165
  1391
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1392
           it != _edge_maps.end(); ++it) {
alpar@209
  1393
        _writer_bits::writeToken(*_os, it->first) << '\t';
deba@165
  1394
      }
deba@165
  1395
      *_os << std::endl;
deba@165
  1396
deba@165
  1397
      std::vector<Edge> edges;
deba@165
  1398
      for (EdgeIt n(_graph); n != INVALID; ++n) {
alpar@209
  1399
        edges.push_back(n);
deba@165
  1400
      }
alpar@209
  1401
deba@165
  1402
      if (label == 0) {
alpar@209
  1403
        IdMap<Graph, Edge> id_map(_graph);
alpar@209
  1404
        _writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map);
alpar@209
  1405
        std::sort(edges.begin(), edges.end(), id_less);
deba@165
  1406
      } else {
alpar@209
  1407
        label->sort(edges);
deba@165
  1408
      }
deba@165
  1409
deba@165
  1410
      for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
alpar@209
  1411
        Edge e = edges[i];
alpar@209
  1412
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1413
                                 find(_graph.u(e))->second);
alpar@209
  1414
        *_os << '\t';
alpar@209
  1415
        _writer_bits::writeToken(*_os, _node_index.
alpar@209
  1416
                                 find(_graph.v(e))->second);
alpar@209
  1417
        *_os << '\t';
alpar@209
  1418
        if (label == 0) {
alpar@209
  1419
          std::ostringstream os;
alpar@209
  1420
          os << _graph.id(e);
alpar@209
  1421
          _writer_bits::writeToken(*_os, os.str());
alpar@209
  1422
          *_os << '\t';
alpar@209
  1423
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1424
        }
alpar@209
  1425
        for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1426
             it != _edge_maps.end(); ++it) {
alpar@209
  1427
          std::string value = it->second->get(e);
alpar@209
  1428
          _writer_bits::writeToken(*_os, value);
alpar@209
  1429
          if (it->first == "label") {
alpar@209
  1430
            _edge_index.insert(std::make_pair(e, value));
alpar@209
  1431
          }
alpar@209
  1432
          *_os << '\t';
alpar@209
  1433
        }
alpar@209
  1434
        *_os << std::endl;
deba@165
  1435
      }
deba@165
  1436
    }
deba@165
  1437
deba@185
  1438
    void createEdgeIndex() {
deba@185
  1439
      _writer_bits::MapStorageBase<Edge>* label = 0;
deba@185
  1440
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
alpar@209
  1441
           it != _edge_maps.end(); ++it) {
deba@185
  1442
        if (it->first == "label") {
alpar@209
  1443
          label = it->second;
alpar@209
  1444
          break;
alpar@209
  1445
        }
deba@185
  1446
      }
deba@185
  1447
deba@185
  1448
      if (label == 0) {
alpar@209
  1449
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1450
          std::ostringstream os;
alpar@209
  1451
          os << _graph.id(e);
alpar@209
  1452
          _edge_index.insert(std::make_pair(e, os.str()));
alpar@209
  1453
        }
deba@185
  1454
      } else {
alpar@209
  1455
        for (EdgeIt e(_graph); e != INVALID; ++e) {
alpar@209
  1456
          std::string value = label->get(e);
alpar@209
  1457
          _edge_index.insert(std::make_pair(e, value));
alpar@209
  1458
        }
deba@185
  1459
      }
deba@185
  1460
    }
deba@185
  1461
deba@165
  1462
    void writeAttributes() {
deba@165
  1463
      if (_attributes.empty()) return;
deba@165
  1464
      *_os << "@attributes";
deba@165
  1465
      if (!_attributes_caption.empty()) {
alpar@209
  1466
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
deba@165
  1467
      }
deba@165
  1468
      *_os << std::endl;
deba@165
  1469
      for (typename Attributes::iterator it = _attributes.begin();
alpar@209
  1470
           it != _attributes.end(); ++it) {
alpar@209
  1471
        _writer_bits::writeToken(*_os, it->first) << ' ';
alpar@209
  1472
        _writer_bits::writeToken(*_os, it->second->get());
alpar@209
  1473
        *_os << std::endl;
deba@165
  1474
      }
deba@165
  1475
    }
alpar@209
  1476
deba@165
  1477
  public:
alpar@209
  1478
alpar@209
  1479
    /// \name Execution of the writer
deba@165
  1480
    /// @{
deba@165
  1481
deba@165
  1482
    /// \brief Start the batch processing
deba@165
  1483
    ///
kpeter@192
  1484
    /// This function starts the batch processing.
deba@165
  1485
    void run() {
deba@165
  1486
      if (!_skip_nodes) {
alpar@209
  1487
        writeNodes();
deba@185
  1488
      } else {
alpar@209
  1489
        createNodeIndex();
deba@165
  1490
      }
alpar@209
  1491
      if (!_skip_edges) {
alpar@209
  1492
        writeEdges();
deba@185
  1493
      } else {
alpar@209
  1494
        createEdgeIndex();
deba@165
  1495
      }
deba@165
  1496
      writeAttributes();
deba@165
  1497
    }
deba@165
  1498
kpeter@192
  1499
    /// \brief Give back the stream of the writer
deba@165
  1500
    ///
kpeter@192
  1501
    /// Give back the stream of the writer
deba@165
  1502
    std::ostream& ostream() {
deba@165
  1503
      return *_os;
deba@165
  1504
    }
deba@165
  1505
deba@165
  1506
    /// @}
deba@165
  1507
  };
deba@165
  1508
kpeter@192
  1509
  /// \brief Return a \ref GraphWriter class
alpar@209
  1510
  ///
kpeter@192
  1511
  /// This function just returns a \ref GraphWriter class.
deba@165
  1512
  /// \relates GraphWriter
deba@165
  1513
  template <typename Graph>
kpeter@293
  1514
  GraphWriter<Graph> graphWriter(const Graph& graph,
kpeter@293
  1515
                                 std::ostream& os = std::cout) {
kpeter@293
  1516
    GraphWriter<Graph> tmp(graph, os);
deba@165
  1517
    return tmp;
deba@165
  1518
  }
deba@165
  1519
kpeter@192
  1520
  /// \brief Return a \ref GraphWriter class
alpar@209
  1521
  ///
kpeter@192
  1522
  /// This function just returns a \ref GraphWriter class.
deba@165
  1523
  /// \relates GraphWriter
deba@165
  1524
  template <typename Graph>
kpeter@293
  1525
  GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
kpeter@293
  1526
    GraphWriter<Graph> tmp(graph, fn);
deba@165
  1527
    return tmp;
deba@165
  1528
  }
deba@165
  1529
kpeter@192
  1530
  /// \brief Return a \ref GraphWriter class
alpar@209
  1531
  ///
kpeter@192
  1532
  /// This function just returns a \ref GraphWriter class.
deba@165
  1533
  /// \relates GraphWriter
deba@165
  1534
  template <typename Graph>
kpeter@293
  1535
  GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
kpeter@293
  1536
    GraphWriter<Graph> tmp(graph, fn);
deba@165
  1537
    return tmp;
deba@165
  1538
  }
deba@248
  1539
deba@248
  1540
  class SectionWriter;
deba@248
  1541
deba@248
  1542
  SectionWriter sectionWriter(std::istream& is);
deba@248
  1543
  SectionWriter sectionWriter(const std::string& fn);
deba@248
  1544
  SectionWriter sectionWriter(const char* fn);
deba@248
  1545
deba@248
  1546
  /// \ingroup lemon_io
deba@248
  1547
  ///
deba@248
  1548
  /// \brief Section writer class
deba@248
  1549
  ///
deba@248
  1550
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
deba@248
  1551
  /// which contain any data in arbitrary format. Such sections can be
deba@248
  1552
  /// written with this class. A writing rule can be added to the
deba@248
  1553
  /// class with two different functions. With the \c sectionLines()
deba@248
  1554
  /// function a generator can write the section line-by-line, while
deba@248
  1555
  /// with the \c sectionStream() member the section can be written to
deba@248
  1556
  /// an output stream.
deba@248
  1557
  class SectionWriter {
deba@248
  1558
  private:
deba@248
  1559
deba@248
  1560
    std::ostream* _os;
deba@248
  1561
    bool local_os;
deba@248
  1562
deba@248
  1563
    typedef std::vector<std::pair<std::string, _writer_bits::Section*> >
deba@248
  1564
    Sections;
deba@248
  1565
deba@248
  1566
    Sections _sections;
deba@248
  1567
deba@248
  1568
  public:
deba@248
  1569
deba@248
  1570
    /// \brief Constructor
deba@248
  1571
    ///
deba@248
  1572
    /// Construct a section writer, which writes to the given output
deba@248
  1573
    /// stream.
deba@248
  1574
    SectionWriter(std::ostream& os)
deba@248
  1575
      : _os(&os), local_os(false) {}
deba@248
  1576
deba@248
  1577
    /// \brief Constructor
deba@248
  1578
    ///
deba@248
  1579
    /// Construct a section writer, which writes into the given file.
deba@248
  1580
    SectionWriter(const std::string& fn)
deba@248
  1581
      : _os(new std::ofstream(fn.c_str())), local_os(true) {}
deba@248
  1582
deba@248
  1583
    /// \brief Constructor
deba@248
  1584
    ///
deba@248
  1585
    /// Construct a section writer, which writes into the given file.
deba@248
  1586
    SectionWriter(const char* fn)
deba@248
  1587
      : _os(new std::ofstream(fn)), local_os(true) {}
deba@248
  1588
deba@248
  1589
    /// \brief Destructor
deba@248
  1590
    ~SectionWriter() {
deba@248
  1591
      for (Sections::iterator it = _sections.begin();
deba@248
  1592
           it != _sections.end(); ++it) {
deba@248
  1593
        delete it->second;
deba@248
  1594
      }
deba@248
  1595
deba@248
  1596
      if (local_os) {
deba@248
  1597
        delete _os;
deba@248
  1598
      }
deba@248
  1599
deba@248
  1600
    }
deba@248
  1601
deba@248
  1602
  private:
deba@248
  1603
deba@248
  1604
    friend SectionWriter sectionWriter(std::ostream& os);
deba@248
  1605
    friend SectionWriter sectionWriter(const std::string& fn);
deba@248
  1606
    friend SectionWriter sectionWriter(const char* fn);
deba@248
  1607
deba@248
  1608
    SectionWriter(SectionWriter& other)
deba@248
  1609
      : _os(other._os), local_os(other.local_os) {
deba@248
  1610
deba@248
  1611
      other._os = 0;
deba@248
  1612
      other.local_os = false;
deba@248
  1613
deba@248
  1614
      _sections.swap(other._sections);
deba@248
  1615
    }
deba@248
  1616
deba@248
  1617
    SectionWriter& operator=(const SectionWriter&);
deba@248
  1618
deba@248
  1619
  public:
deba@248
  1620
deba@248
  1621
    /// \name Section writers
deba@248
  1622
    /// @{
deba@248
  1623
deba@248
  1624
    /// \brief Add a section writer with line oriented writing
deba@248
  1625
    ///
deba@248
  1626
    /// The first parameter is the type descriptor of the section, the
deba@248
  1627
    /// second is a generator with std::string values. At the writing
deba@248
  1628
    /// process, the returned \c std::string will be written into the
deba@248
  1629
    /// output file until it is an empty string.
deba@248
  1630
    ///
deba@248
  1631
    /// For example, an integer vector is written into a section.
deba@248
  1632
    ///\code
deba@248
  1633
    ///  @numbers
deba@248
  1634
    ///  12 45 23 78
deba@248
  1635
    ///  4 28 38 28
deba@248
  1636
    ///  23 6 16
deba@248
  1637
    ///\endcode
deba@248
  1638
    ///
deba@248
  1639
    /// The generator is implemented as a struct.
deba@248
  1640
    ///\code
deba@248
  1641
    ///  struct NumberSection {
deba@248
  1642
    ///    std::vector<int>::const_iterator _it, _end;
deba@248
  1643
    ///    NumberSection(const std::vector<int>& data)
deba@248
  1644
    ///      : _it(data.begin()), _end(data.end()) {}
deba@248
  1645
    ///    std::string operator()() {
deba@248
  1646
    ///      int rem_in_line = 4;
deba@248
  1647
    ///      std::ostringstream ls;
deba@248
  1648
    ///      while (rem_in_line > 0 && _it != _end) {
deba@248
  1649
    ///        ls << *(_it++) << ' ';
deba@248
  1650
    ///        --rem_in_line;
deba@248
  1651
    ///      }
deba@248
  1652
    ///      return ls.str();
deba@248
  1653
    ///    }
deba@248
  1654
    ///  };
deba@248
  1655
    ///
deba@248
  1656
    ///  // ...
deba@248
  1657
    ///
deba@248
  1658
    ///  writer.sectionLines("numbers", NumberSection(vec));
deba@248
  1659
    ///\endcode
deba@248
  1660
    template <typename Functor>
deba@248
  1661
    SectionWriter& sectionLines(const std::string& type, Functor functor) {
deba@248
  1662
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1663
      _sections.push_back(std::make_pair(type,
deba@248
  1664
        new _writer_bits::LineSection<Functor>(functor)));
deba@248
  1665
      return *this;
deba@248
  1666
    }
deba@248
  1667
deba@248
  1668
deba@248
  1669
    /// \brief Add a section writer with stream oriented writing
deba@248
  1670
    ///
deba@248
  1671
    /// The first parameter is the type of the section, the second is
deba@248
  1672
    /// a functor, which takes a \c std::ostream& parameter. The
deba@248
  1673
    /// functor writes the section to the output stream.
deba@248
  1674
    /// \warning The last line must be closed with end-line character.
deba@248
  1675
    template <typename Functor>
deba@248
  1676
    SectionWriter& sectionStream(const std::string& type, Functor functor) {
deba@248
  1677
      LEMON_ASSERT(!type.empty(), "Type is empty.");
deba@248
  1678
      _sections.push_back(std::make_pair(type,
deba@248
  1679
         new _writer_bits::StreamSection<Functor>(functor)));
deba@248
  1680
      return *this;
deba@248
  1681
    }
deba@248
  1682
deba@248
  1683
    /// @}
deba@248
  1684
deba@248
  1685
  public:
deba@248
  1686
deba@248
  1687
deba@248
  1688
    /// \name Execution of the writer
deba@248
  1689
    /// @{
deba@248
  1690
deba@248
  1691
    /// \brief Start the batch processing
deba@248
  1692
    ///
deba@248
  1693
    /// This function starts the batch processing.
deba@248
  1694
    void run() {
deba@248
  1695
deba@248
  1696
      LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer");
deba@248
  1697
deba@248
  1698
      for (Sections::iterator it = _sections.begin();
deba@248
  1699
           it != _sections.end(); ++it) {
deba@248
  1700
        (*_os) << '@' << it->first << std::endl;
deba@248
  1701
        it->second->process(*_os);
deba@248
  1702
      }
deba@248
  1703
    }
deba@248
  1704
deba@248
  1705
    /// \brief Give back the stream of the writer
deba@248
  1706
    ///
deba@248
  1707
    /// Returns the stream of the writer
deba@248
  1708
    std::ostream& ostream() {
deba@248
  1709
      return *_os;
deba@248
  1710
    }
deba@248
  1711
deba@248
  1712
    /// @}
deba@248
  1713
deba@248
  1714
  };
deba@248
  1715
deba@248
  1716
  /// \brief Return a \ref SectionWriter class
deba@248
  1717
  ///
deba@248
  1718
  /// This function just returns a \ref SectionWriter class.
deba@248
  1719
  /// \relates SectionWriter
deba@248
  1720
  inline SectionWriter sectionWriter(std::ostream& os) {
deba@248
  1721
    SectionWriter tmp(os);
deba@248
  1722
    return tmp;
deba@248
  1723
  }
deba@248
  1724
deba@248
  1725
  /// \brief Return a \ref SectionWriter class
deba@248
  1726
  ///
deba@248
  1727
  /// This function just returns a \ref SectionWriter class.
deba@248
  1728
  /// \relates SectionWriter
deba@248
  1729
  inline SectionWriter sectionWriter(const std::string& fn) {
deba@248
  1730
    SectionWriter tmp(fn);
deba@248
  1731
    return tmp;
deba@248
  1732
  }
deba@248
  1733
deba@248
  1734
  /// \brief Return a \ref SectionWriter class
deba@248
  1735
  ///
deba@248
  1736
  /// This function just returns a \ref SectionWriter class.
deba@248
  1737
  /// \relates SectionWriter
deba@248
  1738
  inline SectionWriter sectionWriter(const char* fn) {
deba@248
  1739
    SectionWriter tmp(fn);
deba@248
  1740
    return tmp;
deba@248
  1741
  }
deba@127
  1742
}
deba@127
  1743
deba@127
  1744
#endif