lemon/lemon_writer.h
author deba
Tue, 28 Aug 2007 14:13:40 +0000
changeset 2468 16615642ac7b
parent 2414 9e80927b7921
child 2502 9c23c3762bc5
permissions -rw-r--r--
More simple interface for PathDumper
deba@1409
     1
/* -*- C++ -*-
deba@1409
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1409
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1409
     8
 *
deba@1409
     9
 * Permission to use, modify and distribute this software is granted
deba@1409
    10
 * provided that this copyright notice appears in all copies. For
deba@1409
    11
 * precise terms see the accompanying LICENSE file.
deba@1409
    12
 *
deba@1409
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1409
    14
 * express or implied, and with no claim as to its suitability for any
deba@1409
    15
 * purpose.
deba@1409
    16
 *
deba@1409
    17
 */
deba@1409
    18
deba@2084
    19
///\ingroup lemon_io
deba@1409
    20
///\file
deba@1409
    21
///\brief Lemon Format writer.
deba@1409
    22
deba@1409
    23
#ifndef LEMON_LEMON_WRITER_H
deba@1409
    24
#define LEMON_LEMON_WRITER_H
deba@1409
    25
deba@1409
    26
#include <iostream>
deba@1409
    27
#include <fstream>
deba@1409
    28
#include <string>
deba@1409
    29
#include <vector>
deba@1409
    30
#include <algorithm>
deba@1409
    31
#include <map>
deba@1409
    32
#include <memory>
deba@1409
    33
deba@1409
    34
#include <lemon/error.h>
deba@1993
    35
#include <lemon/bits/invalid.h>
deba@1421
    36
#include <lemon/graph_utils.h>
deba@1409
    37
#include <lemon/bits/item_writer.h>
deba@1993
    38
#include <lemon/bits/utility.h>
deba@1421
    39
#include <lemon/maps.h>
alpar@2207
    40
#include <lemon/dim2.h>
deba@1409
    41
deba@1476
    42
#include <lemon/concept_check.h>
alpar@2260
    43
#include <lemon/concepts/maps.h>
deba@1476
    44
deba@1409
    45
deba@1409
    46
namespace lemon {
deba@1409
    47
deba@1476
    48
  namespace _writer_bits {
deba@1476
    49
    
deba@2101
    50
    template <typename T>
deba@2101
    51
    bool operator<(T, T) {
deba@2101
    52
      throw DataFormatError("Label is not comparable");
deba@2101
    53
    }
deba@2101
    54
deba@2101
    55
    template <typename T>
deba@2101
    56
    struct Less {
deba@2101
    57
      bool operator()(const T& p, const T& q) const {
deba@2101
    58
	return p < q;
deba@2101
    59
      }
deba@2101
    60
    };
deba@2101
    61
deba@2101
    62
    template <typename Map>
deba@2101
    63
    struct ComposeLess {
deba@2101
    64
      ComposeLess(const Map& _map) : map(_map), less() {}
deba@2101
    65
deba@2101
    66
      bool operator()(const typename Map::Key& p, 
deba@2101
    67
                      const typename Map::Key& q) const {
deba@2101
    68
	return less(map[p], map[q]);
deba@2101
    69
      }
deba@2101
    70
      const Map& map;
deba@2101
    71
      Less<typename Map::Value> less;
deba@2101
    72
    };
deba@2101
    73
deba@2467
    74
    template <typename UGraph, typename Map>
deba@2467
    75
    struct UEdgeComposeLess {
deba@2467
    76
      UEdgeComposeLess(const UGraph& _ugraph, const Map& _map) 
deba@2467
    77
	: ugraph(_ugraph), map(_map), less() {}
deba@2467
    78
deba@2467
    79
      bool operator()(const typename UGraph::Edge& p, 
deba@2467
    80
                      const typename UGraph::Edge& q) const {
deba@2467
    81
	return p != q ? less(map[p], map[q]) : 
deba@2467
    82
	  (!ugraph.direction(p) && ugraph.direction(q));
deba@2467
    83
      }
deba@2467
    84
deba@2467
    85
      const UGraph& ugraph;
deba@2467
    86
      const Map& map;
deba@2467
    87
      Less<typename Map::Value> less;
deba@2467
    88
    };
deba@2467
    89
deba@1476
    90
    template <typename Item>
deba@1901
    91
    class ItemLabelWriter {
deba@1476
    92
    public:
deba@1476
    93
deba@1901
    94
      bool isLabelWriter() { return true; }
deba@1476
    95
deba@1901
    96
      void writeLabel(std::ostream&, const Item&) {}
deba@1476
    97
      
deba@1901
    98
      template <class _ItemLabelWriter>
deba@1476
    99
      struct Constraints {
deba@1476
   100
	void constraints() {
deba@1901
   101
	  bool b = writer.isLabelWriter();
deba@1476
   102
	  ignore_unused_variable_warning(b);
deba@1901
   103
	  writer.writeLabel(os, item);
deba@1476
   104
	}
deba@1901
   105
	_ItemLabelWriter& writer;
deba@1476
   106
	std::ostream& os;
deba@1492
   107
	const Item& item;
deba@1492
   108
      };
deba@1492
   109
deba@1492
   110
    };
deba@1492
   111
deba@1492
   112
    template <typename Item>
deba@1492
   113
    class ItemWriter {
deba@1492
   114
    public:
deba@1492
   115
deba@1492
   116
      void write(std::ostream&, const Item&) {}
deba@1492
   117
      
deba@1492
   118
      template <class _ItemWriter>
deba@1492
   119
      struct Constraints {
deba@1492
   120
	void constraints() {
deba@1492
   121
	  writer.write(os, item);
deba@1492
   122
	}
deba@1492
   123
	_ItemWriter& writer;
deba@1492
   124
	std::ostream& os;
deba@1492
   125
	const Item& item;
deba@1476
   126
      };
deba@1476
   127
deba@1476
   128
    };
deba@1476
   129
deba@1705
   130
    template <typename Map>
deba@1705
   131
    struct Ref { typedef const Map& Type; };
deba@1705
   132
deba@1705
   133
    template <typename Graph, typename Map>
deba@1705
   134
    class ForwardComposeMap {
deba@1705
   135
    public:
klao@1909
   136
      typedef typename Graph::UEdge Key;
deba@1705
   137
      typedef typename Map::Value Value;
deba@1705
   138
deba@1705
   139
      ForwardComposeMap(const Graph& _graph, const Map& _map) 
deba@1705
   140
	: graph(_graph), map(_map) {}
deba@1705
   141
      
deba@2467
   142
      Value operator[](const Key& key) const {
deba@2467
   143
	return map[graph.direct(key, true)];
deba@1705
   144
      }
deba@1705
   145
deba@1705
   146
    private:
deba@1705
   147
      typename Ref<Map>::Type map;
deba@1705
   148
      const Graph& graph;
deba@1705
   149
    };
deba@1705
   150
deba@1705
   151
    template <typename Graph, typename Map>
deba@1705
   152
    ForwardComposeMap<Graph, Map>
deba@1705
   153
    forwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   154
      return ForwardComposeMap<Graph, Map>(graph, map);
deba@1705
   155
    }
deba@1705
   156
deba@1705
   157
    template <typename Graph, typename Map>
deba@1705
   158
    class BackwardComposeMap {
deba@1705
   159
    public:
klao@1909
   160
      typedef typename Graph::UEdge Key;
deba@1705
   161
      typedef typename Map::Value Value;
deba@1705
   162
deba@1705
   163
      BackwardComposeMap(const Graph& _graph, const Map& _map) 
deba@1705
   164
	: graph(_graph), map(_map) {}
deba@1705
   165
      
deba@2467
   166
      Value operator[](const Key& key) const {
deba@1705
   167
	return map[graph.direct(key, false)];
deba@1705
   168
      }
deba@1705
   169
deba@1705
   170
    private:
deba@1705
   171
      typename Ref<Map>::Type map;
deba@1705
   172
      const Graph& graph;
deba@1705
   173
    };
deba@1705
   174
deba@1705
   175
    template <typename Graph, typename Map>
deba@1705
   176
    BackwardComposeMap<Graph, Map>
deba@1705
   177
    backwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   178
      return BackwardComposeMap<Graph, Map>(graph, map);
deba@1705
   179
    }
deba@1705
   180
deba@1705
   181
    template <typename Graph, typename Map>
deba@1705
   182
    struct Ref<ForwardComposeMap<Graph, Map> > { 
deba@1705
   183
      typedef ForwardComposeMap<Graph, Map> Type;
deba@1705
   184
    };
deba@1705
   185
deba@1705
   186
    template <typename Graph, typename Map>
deba@1705
   187
    struct Ref<BackwardComposeMap<Graph, Map> > { 
deba@1705
   188
      typedef BackwardComposeMap<Graph, Map> Type; 
deba@1705
   189
    };
deba@1705
   190
deba@1705
   191
    template <typename Map>
alpar@2207
   192
    struct Ref<dim2::XMap<Map> > { 
alpar@2207
   193
      typedef dim2::XMap<Map> Type;
deba@1705
   194
    };
deba@1705
   195
    template <typename Map>
alpar@2207
   196
    struct Ref<dim2::ConstXMap<Map> > { 
alpar@2207
   197
      typedef dim2::ConstXMap<Map> Type;
deba@1705
   198
    };
deba@1705
   199
deba@1705
   200
    template <typename Map>
alpar@2207
   201
    struct Ref<dim2::YMap<Map> > { 
alpar@2207
   202
      typedef dim2::YMap<Map> Type;
deba@1705
   203
    };
deba@1705
   204
    template <typename Map>
alpar@2207
   205
    struct Ref<dim2::ConstYMap<Map> > { 
alpar@2207
   206
      typedef dim2::ConstYMap<Map> Type;
deba@1705
   207
    };
deba@1705
   208
deba@1845
   209
deba@1845
   210
    template <typename _Item>    
deba@1845
   211
    class MapWriterBase {
deba@1845
   212
    public:
deba@1845
   213
      typedef _Item Item;
deba@1845
   214
deba@1845
   215
      virtual ~MapWriterBase() {}
deba@1845
   216
deba@1852
   217
      virtual void write(std::ostream& os, const Item& item) const = 0;
deba@2467
   218
      virtual void sort(std::vector<Item>&) const = 0;
deba@1845
   219
    };
deba@1845
   220
deba@1845
   221
deba@1845
   222
    template <typename _Item, typename _Map, typename _Writer>
deba@1845
   223
    class MapWriter : public MapWriterBase<_Item> {
deba@1845
   224
    public:
deba@1845
   225
      typedef _Map Map;
deba@1845
   226
      typedef _Writer Writer;
deba@1845
   227
      typedef typename Writer::Value Value;
deba@1845
   228
      typedef _Item Item;
deba@1845
   229
      
deba@1845
   230
      typename _writer_bits::Ref<Map>::Type map;
deba@1845
   231
      Writer writer;
deba@1845
   232
deba@1845
   233
      MapWriter(const Map& _map, const Writer& _writer) 
deba@1845
   234
	: map(_map), writer(_writer) {}
deba@1845
   235
deba@1845
   236
      virtual ~MapWriter() {}
deba@1845
   237
deba@1852
   238
      virtual void write(std::ostream& os, const Item& item) const {
deba@1845
   239
	Value value = map[item];
deba@1845
   240
	writer.write(os, value);
deba@1845
   241
      }
deba@1845
   242
deba@2467
   243
      virtual void sort(std::vector<Item>& items) const {
deba@2467
   244
        ComposeLess<Map> less(map);
deba@2467
   245
        std::sort(items.begin(), items.end(), less);
deba@2467
   246
      }
deba@2467
   247
deba@2467
   248
    };
deba@2467
   249
deba@2467
   250
    template <typename _UGraph>    
deba@2467
   251
    class UEdgeMapWriterBase {
deba@2467
   252
    public:
deba@2467
   253
      typedef typename _UGraph::Edge Edge;
deba@2467
   254
      typedef typename _UGraph::UEdge UEdge;
deba@2467
   255
deba@2467
   256
      typedef UEdge Item;
deba@2467
   257
deba@2467
   258
      virtual ~UEdgeMapWriterBase() {}
deba@2467
   259
deba@2467
   260
      virtual void write(std::ostream& os, const Item& item) const = 0;
deba@2467
   261
      virtual void sort(const _UGraph&, std::vector<Edge>&) const = 0;
deba@2467
   262
      virtual void sort(std::vector<UEdge>&) const = 0;
deba@2467
   263
    };
deba@2467
   264
deba@2467
   265
deba@2467
   266
    template <typename _UGraph, typename _Map, typename _Writer>
deba@2467
   267
    class UEdgeMapWriter : public UEdgeMapWriterBase<_UGraph> {
deba@2467
   268
    public:
deba@2467
   269
      typedef _Map Map;
deba@2467
   270
      typedef _Writer Writer;
deba@2467
   271
      typedef typename Writer::Value Value;
deba@2467
   272
deba@2467
   273
      typedef typename _UGraph::Edge Edge;
deba@2467
   274
      typedef typename _UGraph::UEdge UEdge;
deba@2467
   275
      typedef UEdge Item;
deba@2467
   276
      
deba@2467
   277
      typename _writer_bits::Ref<Map>::Type map;
deba@2467
   278
      Writer writer;
deba@2467
   279
deba@2467
   280
      UEdgeMapWriter(const Map& _map, const Writer& _writer) 
deba@2467
   281
	: map(_map), writer(_writer) {}
deba@2467
   282
deba@2467
   283
      virtual ~UEdgeMapWriter() {}
deba@2467
   284
deba@2467
   285
      virtual void write(std::ostream& os, const Item& item) const {
deba@2467
   286
	Value value = map[item];
deba@2467
   287
	writer.write(os, value);
deba@2467
   288
      }
deba@2467
   289
deba@2467
   290
      virtual void sort(const _UGraph& ugraph, std::vector<Edge>& items) const {
deba@2467
   291
        UEdgeComposeLess<_UGraph, Map> less(ugraph, map);
deba@2467
   292
        std::sort(items.begin(), items.end(), less);
deba@2467
   293
      }
deba@2467
   294
deba@2467
   295
      virtual void sort(std::vector<UEdge>& items) const {
deba@2101
   296
        ComposeLess<Map> less(map);
deba@2101
   297
        std::sort(items.begin(), items.end(), less);
deba@2101
   298
      }
deba@2101
   299
deba@1845
   300
    };
deba@1845
   301
deba@1845
   302
deba@1845
   303
    class ValueWriterBase {
deba@1845
   304
    public:
deba@1845
   305
      virtual ~ValueWriterBase() {}
deba@1845
   306
      virtual void write(std::ostream&) = 0;
deba@1845
   307
    };
deba@1845
   308
deba@1845
   309
    template <typename _Value, typename _Writer>
deba@1845
   310
    class ValueWriter : public ValueWriterBase {
deba@1845
   311
    public:
deba@1845
   312
      typedef _Value Value;
deba@1845
   313
      typedef _Writer Writer;
deba@1845
   314
deba@1845
   315
      ValueWriter(const Value& _value, const Writer& _writer)
deba@1845
   316
 	: value(_value), writer(_writer) {}
deba@1845
   317
deba@1845
   318
      virtual void write(std::ostream& os) {
deba@1845
   319
	writer.write(os, value);
deba@1845
   320
      }
deba@1845
   321
    private:
deba@1845
   322
      const Value& value;
deba@1845
   323
      Writer writer;
deba@1845
   324
    };
deba@1845
   325
    
deba@1845
   326
deba@1845
   327
    template <typename _Item>
deba@1901
   328
    class LabelWriterBase {
deba@1845
   329
    public:
deba@1845
   330
      typedef _Item Item;
deba@1901
   331
      virtual ~LabelWriterBase() {}
deba@1845
   332
      virtual void write(std::ostream&, const Item&) const = 0;
deba@2467
   333
      virtual void sort(std::vector<Item>&) const = 0;
deba@1901
   334
      virtual bool isLabelWriter() const = 0;
deba@2467
   335
      virtual LabelWriterBase* clone() const = 0;
deba@1845
   336
    };
deba@1845
   337
deba@1901
   338
    template <typename _Item, typename _BoxedLabelWriter>
deba@1901
   339
    class LabelWriter : public LabelWriterBase<_Item> {
deba@1845
   340
    public:
deba@1845
   341
      typedef _Item Item;
deba@1901
   342
      typedef _BoxedLabelWriter BoxedLabelWriter;
deba@1845
   343
deba@1901
   344
      const BoxedLabelWriter& labelWriter;
deba@1845
   345
deba@1901
   346
      LabelWriter(const BoxedLabelWriter& _labelWriter) 
deba@1901
   347
	: labelWriter(_labelWriter) {}
deba@1845
   348
deba@1845
   349
      virtual void write(std::ostream& os, const Item& item) const {
deba@1901
   350
	labelWriter.writeLabel(os, item);
deba@1845
   351
      }
deba@2467
   352
      virtual void sort(std::vector<Item>& items) const {
deba@2467
   353
	labelWriter.sortByLabel(items);
deba@2467
   354
      }
deba@1845
   355
deba@1901
   356
      virtual bool isLabelWriter() const {
deba@1901
   357
	return labelWriter.isLabelWriter();
deba@1845
   358
      }
deba@2467
   359
deba@2467
   360
      virtual LabelWriter* clone() const {
deba@2467
   361
	return new LabelWriter(labelWriter);
deba@2467
   362
      }
deba@1845
   363
    };
deba@1845
   364
deba@1476
   365
  }
deba@1476
   366
deba@2084
   367
  /// \ingroup lemon_io
deba@1409
   368
  /// \brief Lemon Format writer class.
deba@1409
   369
  /// 
deba@1409
   370
  /// The Lemon Format contains several sections. We do not want to
deba@1409
   371
  /// determine what sections are in a lemon file we give only a framework
deba@1409
   372
  /// to write a section oriented format.
deba@1409
   373
  ///
deba@1409
   374
  /// In the Lemon Format each section starts with a line contains a \c \@
deba@1409
   375
  /// character on the first not white space position. This line is the
deba@1409
   376
  /// header line of the section. Each next lines belong to this section
deba@1409
   377
  /// while it does not starts with \c \@ character. This line can start a 
deba@1409
   378
  /// new section or if it can close the file with the \c \@end line.
deba@1409
   379
  /// The file format ignore the empty lines and it may contain comments
deba@1409
   380
  /// started with a \c # character to the end of the line. 
deba@1409
   381
  ///
deba@1409
   382
  /// The framework provides an abstract LemonWriter::SectionWriter class
deba@1409
   383
  /// what defines the interface of a SectionWriter. The SectionWriter
deba@1409
   384
  /// has the \c header() member function what gives back the header of the
deba@1409
   385
  /// section. After that it will be called the \c write() member which
deba@1409
   386
  /// should write the content of the section.
deba@1409
   387
  ///
deba@1409
   388
  /// \relates GraphWriter
deba@1409
   389
  /// \relates NodeSetWriter
deba@1409
   390
  /// \relates EdgeSetWriter
deba@1409
   391
  /// \relates NodesWriter
deba@1409
   392
  /// \relates EdgesWriter
deba@1409
   393
  /// \relates AttributeWriter
deba@1409
   394
  class LemonWriter {
deba@1409
   395
  public:
deba@1409
   396
deba@1409
   397
    /// \brief Abstract base class for writing a section.
deba@1409
   398
    ///
deba@1409
   399
    /// This class has an \c header() member function what gives back
deba@1409
   400
    /// the header line of the section. The \c write() member should
deba@1409
   401
    /// write the content of the section to the stream.
deba@1409
   402
    class SectionWriter {
deba@1409
   403
      friend class LemonWriter;
deba@1409
   404
    protected:
deba@1409
   405
      /// \brief Constructor for SectionWriter.
deba@1409
   406
      ///
deba@1409
   407
      /// Constructor for SectionWriter. It attach this writer to
deba@1409
   408
      /// the given LemonWriter.
deba@1409
   409
      SectionWriter(LemonWriter& writer) {
deba@1409
   410
	writer.attach(*this);
deba@1409
   411
      }
alpar@1494
   412
      
alpar@1494
   413
      virtual ~SectionWriter() {}
deba@1409
   414
deba@1409
   415
      /// \brief The header of section.
deba@1409
   416
      ///
deba@1409
   417
      /// It gives back the header of the section.
deba@1409
   418
      virtual std::string header() = 0;
deba@1409
   419
deba@2084
   420
      /// \brief Writer function of the section.
deba@1409
   421
      ///
deba@1409
   422
      /// Write the content of the section.
deba@1409
   423
      virtual void write(std::ostream& os) = 0;
deba@2084
   424
      
deba@2084
   425
      /// \brief Gives back true when the section should be written.
deba@2084
   426
      ///
deba@2084
   427
      /// Gives back true when the section should be written.
deba@2084
   428
      virtual bool valid() { return true; }
deba@1409
   429
    };
deba@1409
   430
deba@1409
   431
    /// \brief Constructor for LemonWriter.
deba@1409
   432
    ///
deba@1409
   433
    /// Constructor for LemonWriter which writes to the given stream.
deba@1409
   434
    LemonWriter(std::ostream& _os) 
deba@1409
   435
      : os(&_os), own_os(false) {}
deba@1409
   436
deba@1409
   437
    /// \brief Constructor for LemonWriter.
deba@1409
   438
    ///
deba@1409
   439
    /// Constructor for LemonWriter which writes to the given file.
deba@1409
   440
    LemonWriter(const std::string& filename) 
deba@1409
   441
      : os(0), own_os(true) {
deba@1409
   442
      os = new std::ofstream(filename.c_str());
deba@1409
   443
    }
deba@1409
   444
deba@1409
   445
    /// \brief Desctructor for LemonWriter.
deba@1409
   446
    ///
deba@1409
   447
    /// Desctructor for LemonWriter.
deba@1409
   448
    ~LemonWriter() {
deba@1409
   449
      if (own_os) {
deba@1409
   450
	delete os;
deba@1409
   451
      }
deba@1409
   452
    }
deba@1409
   453
deba@1409
   454
  private:
deba@1409
   455
    LemonWriter(const LemonWriter&);
deba@1409
   456
    void operator=(const LemonWriter&);
deba@1409
   457
deba@1409
   458
    void attach(SectionWriter& writer) {
deba@1409
   459
      writers.push_back(&writer);
deba@1409
   460
    }
deba@1409
   461
deba@1409
   462
  public:
deba@1409
   463
deba@1409
   464
    /// \brief Executes the LemonWriter.
deba@1409
   465
    /// 
deba@1409
   466
    /// It executes the LemonWriter.
deba@1409
   467
    void run() {
deba@1409
   468
      SectionWriters::iterator it;
deba@1409
   469
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@2084
   470
        if ((*it)->valid()) {
deba@2084
   471
          *os << (*it)->header() << std::endl;
deba@2084
   472
          (*it)->write(*os);
deba@2084
   473
        }
deba@1409
   474
      }
deba@1409
   475
      *os << "@end" << std::endl;
deba@1409
   476
    }
deba@1409
   477
deba@1409
   478
deba@1409
   479
  private:
deba@1409
   480
deba@1409
   481
    std::ostream* os;
deba@1409
   482
    bool own_os;
deba@1409
   483
deba@1409
   484
    typedef std::vector<SectionWriter*> SectionWriters;
deba@1409
   485
    SectionWriters writers;
deba@1409
   486
deba@1409
   487
  };
deba@1409
   488
deba@2016
   489
  /// \ingroup section_io
deba@1409
   490
  /// \brief SectionWriter for writing a graph's nodeset.
deba@1409
   491
  ///
deba@1409
   492
  /// The lemon format can store multiple graph nodesets with several maps.
deba@1901
   493
  /// The nodeset section's header line is \c \@nodeset \c nodeset_name, but 
deba@1901
   494
  /// the \c nodeset_name may be empty.
deba@1409
   495
  ///
deba@1409
   496
  /// The first line of the section contains the names of the maps separated
deba@1409
   497
  /// with white spaces. Each next lines describes a node in the nodeset, and
deba@1409
   498
  /// contains the mapped values for each map.
deba@1409
   499
  ///
deba@1901
   500
  /// If the nodeset contains an \c "label" named map then it will be regarded
deba@1901
   501
  /// as label map. This map should contain only unique values and when the 
deba@1901
   502
  /// \c writeLabel() member will be called with a node it will write it's 
deba@1901
   503
  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 
deba@2101
   504
  /// then the label map will be the id in the graph. In addition if the
deba@2467
   505
  /// the \c _forceSort is true then the writer will write the edges
deba@2101
   506
  /// sorted by the labels.
deba@1409
   507
  ///
deba@1409
   508
  /// \relates LemonWriter
deba@1409
   509
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@1845
   510
  class NodeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   511
    typedef LemonWriter::SectionWriter Parent;
deba@1409
   512
  public:
deba@1409
   513
deba@1409
   514
    typedef _Graph Graph;
deba@1409
   515
    typedef _Traits Traits;
deba@1429
   516
    typedef typename Graph::Node Node;
deba@1409
   517
deba@1409
   518
    /// \brief Constructor.
deba@1409
   519
    ///
deba@1409
   520
    /// Constructor for NodeSetWriter. It creates the NodeSetWriter and
deba@1901
   521
    /// attach it into the given LemonWriter. If the \c _forceLabelMap
deba@1901
   522
    /// parameter is true then the writer will write own label map when
deba@2101
   523
    /// the user does not give "label" named map. In addition if the
deba@2467
   524
    /// the \c _forceSort is true then the writer will write the edges
deba@2101
   525
    /// sorted by the labels.
deba@1409
   526
    NodeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   527
		  const std::string& _name = std::string(), 
deba@2467
   528
		  bool _forceLabelMap = true, bool _forceSort = true) 
deba@1901
   529
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 
deba@2467
   530
	forceSort(_forceSort), graph(_graph), name(_name) {}
deba@1409
   531
deba@1409
   532
    /// \brief Destructor.
deba@1409
   533
    ///
deba@1409
   534
    /// Destructor for NodeSetWriter.
deba@1409
   535
    virtual ~NodeSetWriter() {
deba@1409
   536
      typename MapWriters::iterator it;
deba@1409
   537
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
   538
	delete it->second;
deba@1409
   539
      }
deba@1409
   540
    }
deba@1409
   541
deba@1409
   542
  private:
deba@1409
   543
    NodeSetWriter(const NodeSetWriter&);
deba@1409
   544
    void operator=(const NodeSetWriter&);
deba@1409
   545
  
deba@1409
   546
  public:
deba@1409
   547
deba@1409
   548
    /// \brief Add a new node map writer command for the writer.
deba@1409
   549
    ///
deba@1409
   550
    /// Add a new node map writer command for the writer.
deba@1409
   551
    template <typename Map>
deba@2386
   552
    NodeSetWriter& writeNodeMap(std::string label, const Map& map) {
deba@1421
   553
      return writeNodeMap<typename Traits::
deba@2386
   554
	template Writer<typename Map::Value>, Map>(label, map);
deba@1409
   555
    }
deba@1409
   556
deba@1409
   557
    /// \brief Add a new node map writer command for the writer.
deba@1409
   558
    ///
deba@1409
   559
    /// Add a new node map writer command for the writer.
deba@2386
   560
    template <typename ItemWriter, typename Map>
deba@2386
   561
    NodeSetWriter& writeNodeMap(std::string label, const Map& map, 
deba@2386
   562
			    const ItemWriter& iw = ItemWriter()) {
alpar@2260
   563
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@2386
   564
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
deba@1409
   565
      writers.push_back(
deba@2386
   566
	make_pair(label, new _writer_bits::
deba@2386
   567
		  MapWriter<Node, Map, ItemWriter>(map, iw)));
deba@1409
   568
      return *this;
deba@1409
   569
    }
deba@1409
   570
deba@1409
   571
  protected:
deba@1409
   572
deba@1409
   573
    /// \brief The header of the section.
deba@1409
   574
    ///
deba@1409
   575
    /// It gives back the header of the section.
deba@1409
   576
    virtual std::string header() {
deba@1901
   577
      return "@nodeset " + name;
deba@1409
   578
    }
deba@1409
   579
deba@1409
   580
    /// \brief  Writer function of the section.
deba@1409
   581
    ///
deba@1409
   582
    /// Write the content of the section.
deba@1409
   583
    virtual void write(std::ostream& os) {
deba@2386
   584
      for (int i = 0; i < int(writers.size()); ++i) {
deba@2084
   585
	if (writers[i].first == "label") {
deba@1901
   586
	  labelMap = writers[i].second;
deba@1901
   587
	  forceLabelMap = false;
deba@1409
   588
	  break;
deba@1409
   589
	}
deba@1409
   590
      }
deba@2101
   591
      std::vector<Node> items;
deba@2101
   592
      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@2101
   593
        items.push_back(it);
deba@2101
   594
      }
deba@2467
   595
      if (forceSort) {
deba@2101
   596
        if (labelMap) {
deba@2467
   597
          labelMap->sort(items);
deba@2101
   598
        } else {
deba@2101
   599
          typedef IdMap<Graph, Node> Map;
deba@2101
   600
          Map map(graph);
deba@2101
   601
          _writer_bits::ComposeLess<Map> less(map);
deba@2101
   602
          std::sort(items.begin(), items.end(), less);
deba@2101
   603
        }
deba@2101
   604
      }
deba@1901
   605
      if (forceLabelMap) {
deba@1901
   606
	os << "label\t";
deba@1409
   607
      }
deba@2386
   608
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1409
   609
	os << writers[i].first << '\t';
deba@1409
   610
      }
deba@1409
   611
      os << std::endl;
deba@2101
   612
      for (typename std::vector<Node>::iterator it = items.begin();
deba@2101
   613
           it != items.end(); ++it) {
deba@1901
   614
	if (forceLabelMap) {
deba@2101
   615
	  os << graph.id(*it) << '\t';
deba@1409
   616
	}
deba@2386
   617
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2101
   618
	  writers[i].second->write(os, *it);
deba@1409
   619
	  os << '\t';
deba@1409
   620
	}
deba@1409
   621
	os << std::endl;
deba@1409
   622
      }
deba@1409
   623
    }
deba@1409
   624
deba@1409
   625
  public:
deba@1409
   626
deba@1901
   627
    /// \brief Returns true if the nodeset can write the labels of the nodes.
deba@1409
   628
    ///
deba@1901
   629
    /// Returns true if the nodeset can write the labels of the nodes.
deba@2467
   630
    /// It is possible only if a "label" named map was written or the 
deba@1901
   631
    /// \c _forceLabelMap constructor parameter was true.
deba@1901
   632
    bool isLabelWriter() const {
deba@1901
   633
      return labelMap != 0 || forceLabelMap;
deba@1409
   634
    }
deba@1409
   635
deba@1901
   636
    /// \brief Write the label of the given node.
deba@1409
   637
    ///
deba@2467
   638
    /// It writes the label of the given node. If there was written a "label"
deba@1409
   639
    /// named map then it will write the map value belongs to the node.
deba@1901
   640
    /// Otherwise if the \c forceLabel parameter was true it will write
deba@1901
   641
    /// its label in the graph. 
deba@1901
   642
    void writeLabel(std::ostream& os, const Node& item) const {
deba@1901
   643
      if (forceLabelMap) {
deba@1409
   644
	os << graph.id(item);
deba@1409
   645
      } else {
deba@1901
   646
	labelMap->write(os, item);
deba@1409
   647
      }
deba@1409
   648
    }
deba@1409
   649
deba@2467
   650
    /// \brief Sorts the given node vector by label.
deba@2467
   651
    ///
deba@2467
   652
    /// Sorts the given node vector by label. If there was written an
deba@2467
   653
    /// "label" named map then the vector will be sorted by the values
deba@2467
   654
    /// of this map. Otherwise if the \c forceLabel parameter was true
deba@2467
   655
    /// it will be sorted by its id in the graph.
deba@2467
   656
    void sortByLabel(std::vector<Node>& nodes) const {
deba@2467
   657
      if (labelMap) {
deba@2467
   658
	labelMap->sort(nodes);
deba@2467
   659
      } else {
deba@2467
   660
	typedef IdMap<Graph, Node> Map;
deba@2467
   661
	Map map(graph);
deba@2467
   662
	_writer_bits::ComposeLess<Map> less(map);
deba@2467
   663
	std::sort(nodes.begin(), nodes.end(), less);
deba@2467
   664
      }
deba@2467
   665
    }
deba@2467
   666
deba@1409
   667
  private:
deba@1409
   668
deba@1845
   669
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@1845
   670
				  MapWriterBase<Node>*> > MapWriters;
deba@1409
   671
    MapWriters writers;
deba@1409
   672
deba@1901
   673
    _writer_bits::MapWriterBase<Node>* labelMap;
deba@1901
   674
    bool forceLabelMap;
deba@2467
   675
    bool forceSort;
deba@1409
   676
   
deba@1705
   677
    const Graph& graph;   
deba@1901
   678
    std::string name;
deba@1409
   679
deba@1409
   680
  };
deba@1409
   681
deba@2016
   682
  /// \ingroup section_io
deba@1421
   683
  /// \brief SectionWriter for writing a graph's edgesets.
deba@1409
   684
  ///
deba@1421
   685
  /// The lemon format can store multiple graph edgesets with several maps. 
deba@1901
   686
  /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but 
deba@1901
   687
  /// the \c edgeset_name may be empty.
deba@1409
   688
  ///
deba@1409
   689
  /// The first line of the section contains the names of the maps separated
deba@1409
   690
  /// with white spaces. Each next lines describes a edge in the edgeset. The
deba@1901
   691
  /// line contains the source and the target nodes' label and the mapped 
deba@1409
   692
  /// values for each map.
deba@1409
   693
  ///
deba@1901
   694
  /// If the edgeset contains an \c "label" named map then it will be regarded
deba@1901
   695
  /// as label map. This map should contain only unique values and when the 
deba@1901
   696
  /// \c writeLabel() member will be called with an edge it will write it's 
deba@1901
   697
  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 
deba@2101
   698
  /// then the label map will be the id in the graph. In addition if the
deba@2467
   699
  /// the \c _forceSort is true then the writer will write the edges
deba@2101
   700
  /// sorted by the labels.
deba@1409
   701
  ///
deba@1901
   702
  /// The edgeset writer needs a node label writer to identify which nodes
deba@1901
   703
  /// have to be connected. If a NodeSetWriter can write the nodes' label,
deba@1409
   704
  /// it will be able to use with this class.
deba@1409
   705
  ///
deba@1409
   706
  /// \relates LemonWriter
deba@1409
   707
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@1845
   708
  class EdgeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   709
    typedef LemonWriter::SectionWriter Parent;
deba@1409
   710
  public:
deba@1409
   711
deba@1409
   712
    typedef _Graph Graph;
deba@1409
   713
    typedef _Traits Traits;
deba@1429
   714
    typedef typename Graph::Node Node;
deba@1429
   715
    typedef typename Graph::Edge Edge;
deba@1409
   716
deba@1409
   717
    /// \brief Constructor.
deba@1409
   718
    ///
deba@2101
   719
    /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter
deba@2101
   720
    /// and attach it into the given LemonWriter. It will write node
deba@2101
   721
    /// labels by the \c _nodeLabelWriter. If the \c _forceLabelMap
deba@2101
   722
    /// parameter is true then the writer will write own label map if
deba@2101
   723
    /// the user does not give "label" named map. In addition if the
deba@2467
   724
    /// the \c _forceSort is true then the writer will write the
deba@2101
   725
    /// edges sorted by the labels.
deba@1901
   726
    template <typename NodeLabelWriter>
deba@1409
   727
    EdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   728
		  const NodeLabelWriter& _nodeLabelWriter, 
deba@1901
   729
		  const std::string& _name = std::string(),
deba@2467
   730
		  bool _forceLabelMap = true, bool _forceSort = true)
deba@1901
   731
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
deba@2467
   732
	forceSort(_forceSort), graph(_graph), name(_name) {
deba@1901
   733
      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
deba@1901
   734
      nodeLabelWriter.reset(new _writer_bits::
deba@1901
   735
			 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
deba@1476
   736
    } 
deba@1409
   737
deba@1409
   738
    /// \brief Destructor.
deba@1409
   739
    ///
deba@1409
   740
    /// Destructor for EdgeSetWriter.
deba@1409
   741
    virtual ~EdgeSetWriter() {
deba@1409
   742
      typename MapWriters::iterator it;
deba@1409
   743
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
   744
	delete it->second;
deba@1409
   745
      }
deba@1409
   746
    }
deba@1409
   747
deba@1409
   748
  private:
deba@1409
   749
    EdgeSetWriter(const EdgeSetWriter&);
deba@1409
   750
    void operator=(const EdgeSetWriter&);
deba@1409
   751
deba@1409
   752
  public:
deba@1409
   753
deba@1421
   754
    /// \brief Add a new edge map writer command for the writer.
deba@1409
   755
    ///
deba@1421
   756
    /// Add a new edge map writer command for the writer.
deba@1409
   757
    template <typename Map>
deba@2386
   758
    EdgeSetWriter& writeEdgeMap(std::string label, const Map& map) {
deba@1421
   759
      return writeEdgeMap<typename Traits::
deba@2386
   760
	template Writer<typename Map::Value>, Map>(label, map);
deba@1409
   761
    }
deba@1409
   762
deba@1421
   763
    /// \brief Add a new edge map writer command for the writer.
deba@1409
   764
    ///
deba@1421
   765
    /// Add a new edge map writer command for the writer.
deba@2386
   766
    template <typename ItemWriter, typename Map>
deba@2386
   767
    EdgeSetWriter& writeEdgeMap(std::string label, const Map& map, 
deba@2386
   768
			    const ItemWriter& iw = ItemWriter()) {
alpar@2260
   769
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@2386
   770
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
deba@1409
   771
      writers.push_back(
deba@2386
   772
	make_pair(label, new _writer_bits::
deba@2386
   773
		  MapWriter<Edge, Map, ItemWriter>(map, iw)));
deba@1409
   774
      return *this;
deba@1409
   775
    }
deba@1409
   776
deba@1409
   777
  protected:
deba@1409
   778
deba@1409
   779
    /// \brief The header of the section.
deba@1409
   780
    ///
deba@1409
   781
    /// It gives back the header of the section.
deba@1409
   782
    virtual std::string header() {
deba@1901
   783
      return "@edgeset " + name;
deba@1409
   784
    }
deba@1409
   785
deba@1409
   786
    /// \brief  Writer function of the section.
deba@1409
   787
    ///
deba@1409
   788
    /// Write the content of the section.
deba@1409
   789
    virtual void write(std::ostream& os) {
deba@1901
   790
      if (!nodeLabelWriter->isLabelWriter()) {
deba@1901
   791
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
   792
      }
deba@2386
   793
      for (int i = 0; i < int(writers.size()); ++i) {
deba@2084
   794
	if (writers[i].first == "label") {
deba@1901
   795
	  labelMap = writers[i].second;
deba@1901
   796
	  forceLabelMap = false;
deba@1409
   797
	  break;
deba@1409
   798
	}
deba@1409
   799
      }
deba@2101
   800
      std::vector<Edge> items;
deba@2101
   801
      for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) {
deba@2101
   802
        items.push_back(it);
deba@2101
   803
      }
deba@2467
   804
      if (forceSort) {
deba@2101
   805
        if (labelMap) {
deba@2467
   806
          labelMap->sort(items);
deba@2101
   807
        } else {
deba@2101
   808
          typedef IdMap<Graph, Edge> Map;
deba@2101
   809
          Map map(graph);
deba@2101
   810
          _writer_bits::ComposeLess<Map> less(map);
deba@2101
   811
          std::sort(items.begin(), items.end(), less);
deba@2101
   812
        }
deba@2101
   813
      }
deba@1409
   814
      os << "\t\t";
deba@1901
   815
      if (forceLabelMap) {
deba@1901
   816
	os << "label\t";
deba@1409
   817
      }
deba@2386
   818
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1409
   819
	os << writers[i].first << '\t';
deba@1409
   820
      }
deba@1409
   821
      os << std::endl;
deba@2101
   822
      for (typename std::vector<Edge>::iterator it = items.begin();
deba@2101
   823
           it != items.end(); ++it) {
deba@2101
   824
	nodeLabelWriter->write(os, graph.source(*it));
deba@1409
   825
	os << '\t';
deba@2101
   826
	nodeLabelWriter->write(os, graph.target(*it));
deba@1409
   827
	os << '\t';
deba@1901
   828
	if (forceLabelMap) {
deba@2101
   829
	  os << graph.id(*it) << '\t';
deba@1409
   830
	}
deba@2386
   831
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2101
   832
	  writers[i].second->write(os, *it);
deba@1409
   833
	  os << '\t';
deba@1409
   834
	}
deba@1409
   835
	os << std::endl;
deba@1409
   836
      }
deba@1409
   837
    }
deba@1409
   838
deba@1409
   839
  public:
deba@1409
   840
deba@1901
   841
    /// \brief Returns true if the edgeset can write the labels of the edges.
deba@1409
   842
    ///
deba@1901
   843
    /// Returns true if the edgeset can write the labels of the edges.
deba@2467
   844
    /// It is possible only if a "label" named map was written or the 
deba@1901
   845
    /// \c _forceLabelMap constructor parameter was true.
deba@1901
   846
    bool isLabelWriter() const {
deba@1901
   847
      return forceLabelMap || labelMap != 0;
deba@1409
   848
    }
deba@1409
   849
deba@1901
   850
    /// \brief Write the label of the given edge.
deba@1409
   851
    ///
deba@2467
   852
    /// It writes the label of the given edge. If there was written a "label"
deba@1409
   853
    /// named map then it will write the map value belongs to the edge.
deba@1901
   854
    /// Otherwise if the \c forceLabel parameter was true it will write
deba@1901
   855
    /// its label in the graph. 
deba@1901
   856
    void writeLabel(std::ostream& os, const Edge& item) const {
deba@1901
   857
      if (forceLabelMap) {
deba@1409
   858
	os << graph.id(item);
deba@1409
   859
      } else {
deba@1901
   860
	labelMap->write(os, item);
deba@1409
   861
      }
deba@1409
   862
    } 
deba@1409
   863
deba@2467
   864
    /// \brief Sorts the given edge vector by label.
deba@2467
   865
    ///
deba@2467
   866
    /// Sorts the given edge vector by label. If there was written an
deba@2467
   867
    /// "label" named map then the vector will be sorted by the values
deba@2467
   868
    /// of this map. Otherwise if the \c forceLabel parameter was true
deba@2467
   869
    /// it will be sorted by its id in the graph.
deba@2467
   870
    void sortByLabel(std::vector<Edge>& edges) const {
deba@2467
   871
      if (labelMap) {
deba@2467
   872
	labelMap->sort(edges);
deba@2467
   873
      } else {
deba@2467
   874
	typedef IdMap<Graph, Edge> Map;
deba@2467
   875
	Map map(graph);
deba@2467
   876
	_writer_bits::ComposeLess<Map> less(map);
deba@2467
   877
	std::sort(edges.begin(), edges.end(), less);
deba@2467
   878
      }
deba@2467
   879
    }
deba@2467
   880
deba@1409
   881
  private:
deba@1409
   882
deba@1845
   883
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@1845
   884
				  MapWriterBase<Edge>*> > MapWriters;
deba@1409
   885
    MapWriters writers;
deba@1409
   886
deba@1901
   887
    _writer_bits::MapWriterBase<Edge>* labelMap;
deba@1901
   888
    bool forceLabelMap;
deba@2467
   889
    bool forceSort;
deba@1409
   890
   
deba@1705
   891
    const Graph& graph;   
deba@1901
   892
    std::string name;
deba@1421
   893
deba@1901
   894
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter;
deba@1421
   895
  };
deba@1421
   896
deba@2016
   897
  /// \ingroup section_io
deba@1421
   898
  /// \brief SectionWriter for writing a undirected edgeset.
deba@1421
   899
  ///
deba@1421
   900
  /// The lemon format can store multiple undirected edgesets with several 
klao@1909
   901
  /// maps. The undirected edgeset section's header line is \c \@uedgeset 
klao@1909
   902
  /// \c uedgeset_name, but the \c uedgeset_name may be empty.
deba@1421
   903
  ///
deba@1421
   904
  /// The first line of the section contains the names of the maps separated
deba@1421
   905
  /// with white spaces. Each next lines describes an undirected edge in the 
deba@1901
   906
  /// edgeset. The line contains the two connected nodes' label and the mapped 
deba@1421
   907
  /// values for each undirected map.
deba@1421
   908
  ///
deba@1421
   909
  /// The section can handle the directed as a syntactical sugar. Two
deba@1421
   910
  /// undirected edge map describes one directed edge map. This two maps
deba@1421
   911
  /// are the forward map and the backward map and the names of this map
deba@1421
   912
  /// is near the same just with a prefix \c '+' or \c '-' character 
deba@1421
   913
  /// difference.
deba@1421
   914
  ///
deba@2101
   915
  /// If the edgeset contains an \c "label" named map then it will be
deba@2101
   916
  /// regarded as label map. This map should contain only unique
deba@2101
   917
  /// values and when the \c writeLabel() member will be called with
deba@2101
   918
  /// an undirected edge it will write it's label. Otherwise if the \c
deba@2101
   919
  /// _forceLabelMap constructor parameter is true then the label map
deba@2101
   920
  /// will be the id in the graph.  In addition if the the \c
deba@2467
   921
  /// _forceSort is true then the writer will write the edges sorted
deba@2101
   922
  /// by the labels.
deba@1421
   923
  ///
deba@1901
   924
  /// The undirected edgeset writer needs a node label writer to identify 
deba@1421
   925
  /// which nodes have to be connected. If a NodeSetWriter can write the 
deba@1901
   926
  /// nodes' label, it will be able to use with this class.
deba@1421
   927
  ///
deba@1421
   928
  /// \relates LemonWriter
deba@1421
   929
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
klao@1909
   930
  class UEdgeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   931
    typedef LemonWriter::SectionWriter Parent;
deba@1421
   932
  public:
deba@1421
   933
deba@1421
   934
    typedef _Graph Graph;
deba@1421
   935
    typedef _Traits Traits;
deba@1429
   936
    typedef typename Graph::Node Node;
deba@1429
   937
    typedef typename Graph::Edge Edge;
klao@1909
   938
    typedef typename Graph::UEdge UEdge;
deba@1421
   939
deba@1421
   940
    /// \brief Constructor.
deba@1421
   941
    ///
klao@1909
   942
    /// Constructor for UEdgeSetWriter. It creates the UEdgeSetWriter
deba@2101
   943
    /// and attach it into the given LemonWriter. It will write node
deba@2101
   944
    /// labels by the \c _nodeLabelWriter. If the \c _forceLabelMap
deba@2101
   945
    /// parameter is true then the writer will write own label map if
deba@2101
   946
    /// the user does not give "label" named map. In addition if the
deba@2467
   947
    /// the \c _forceSort is true then the writer will write the
deba@2101
   948
    /// edges sorted by the labels.
deba@1901
   949
    template <typename NodeLabelWriter>
klao@1909
   950
    UEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   951
		       const NodeLabelWriter& _nodeLabelWriter, 
deba@1901
   952
		       const std::string& _name = std::string(),
deba@2467
   953
		       bool _forceLabelMap = true, bool _forceSort = true)
deba@1901
   954
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
deba@2467
   955
	forceSort(_forceSort), graph(_graph), name(_name) {
deba@1901
   956
      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
deba@1901
   957
      nodeLabelWriter.reset(new _writer_bits::
deba@2467
   958
	LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
deba@1476
   959
    } 
deba@1421
   960
deba@1421
   961
    /// \brief Destructor.
deba@1421
   962
    ///
klao@1909
   963
    /// Destructor for UEdgeSetWriter.
klao@1909
   964
    virtual ~UEdgeSetWriter() {
deba@1421
   965
      typename MapWriters::iterator it;
deba@1421
   966
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1421
   967
	delete it->second;
deba@1421
   968
      }
deba@1421
   969
    }
deba@1421
   970
deba@1421
   971
  private:
klao@1909
   972
    UEdgeSetWriter(const UEdgeSetWriter&);
klao@1909
   973
    void operator=(const UEdgeSetWriter&);
deba@1421
   974
deba@1421
   975
  public:
deba@1421
   976
deba@1421
   977
    /// \brief Add a new undirected edge map writer command for the writer.
deba@1421
   978
    ///
deba@1421
   979
    /// Add a new undirected map writer command for the writer.
deba@1421
   980
    template <typename Map>
deba@2386
   981
    UEdgeSetWriter& writeUEdgeMap(std::string label, const Map& map) {
klao@1909
   982
      return writeUEdgeMap<typename Traits::
deba@2386
   983
	template Writer<typename Map::Value>, Map>(label, map);
deba@1421
   984
    }
deba@1421
   985
deba@1421
   986
    /// \brief Add a new undirected map writer command for the writer.
deba@1421
   987
    ///
deba@1421
   988
    /// Add a new undirected map writer command for the writer.
deba@2386
   989
    template <typename ItemWriter, typename Map>
deba@2386
   990
    UEdgeSetWriter& writeUEdgeMap(std::string label, const Map& map, 
deba@2386
   991
                                  const ItemWriter& iw = ItemWriter()) {
alpar@2260
   992
      checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>();
deba@2386
   993
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
deba@1421
   994
      writers.push_back(
deba@2386
   995
	make_pair(label, new _writer_bits::
deba@2467
   996
		  UEdgeMapWriter<Graph, Map, ItemWriter>(map, iw)));
deba@1421
   997
      return *this;
deba@1421
   998
    }
deba@1421
   999
deba@1421
  1000
    /// \brief Add a new directed edge map writer command for the writer.
deba@1421
  1001
    ///
deba@1421
  1002
    /// Add a new directed map writer command for the writer.
deba@1421
  1003
    template <typename Map>
deba@2386
  1004
    UEdgeSetWriter& writeEdgeMap(std::string label, const Map& map) {
deba@1492
  1005
      return writeEdgeMap<typename Traits::
deba@2386
  1006
	template Writer<typename Map::Value>, Map>(label, map);
deba@1421
  1007
    }
deba@1421
  1008
deba@1421
  1009
    /// \brief Add a new directed map writer command for the writer.
deba@1421
  1010
    ///
deba@1421
  1011
    /// Add a new directed map writer command for the writer.
deba@2386
  1012
    template <typename ItemWriter, typename Map>
deba@2386
  1013
    UEdgeSetWriter& writeEdgeMap(std::string label, const Map& map, 
deba@2386
  1014
                                 const ItemWriter& iw = ItemWriter()) {
alpar@2260
  1015
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@2386
  1016
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
deba@2467
  1017
      writeUEdgeMap("+" + label, 
deba@2386
  1018
                    _writer_bits::forwardComposeMap(graph, map), iw);
deba@2467
  1019
      writeUEdgeMap("-" + label, 
deba@2386
  1020
                    _writer_bits::backwardComposeMap(graph, map), iw);
deba@1421
  1021
      return *this;
deba@1421
  1022
    }
deba@1421
  1023
deba@1421
  1024
  protected:
deba@1421
  1025
deba@1421
  1026
    /// \brief The header of the section.
deba@1421
  1027
    ///
deba@1421
  1028
    /// It gives back the header of the section.
deba@1421
  1029
    virtual std::string header() {
klao@1909
  1030
      return "@uedgeset " + name;
deba@1421
  1031
    }
deba@1421
  1032
deba@1421
  1033
    /// \brief  Writer function of the section.
deba@1421
  1034
    ///
deba@1421
  1035
    /// Write the content of the section.
deba@1421
  1036
    virtual void write(std::ostream& os) {
deba@1901
  1037
      if (!nodeLabelWriter->isLabelWriter()) {
deba@1901
  1038
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1039
      }
deba@2386
  1040
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1901
  1041
	if (writers[i].first == "label") {
deba@1901
  1042
	  labelMap = writers[i].second;
deba@1901
  1043
	  forceLabelMap = false;
deba@1421
  1044
	  break;
deba@1421
  1045
	}
deba@1421
  1046
      }
deba@2101
  1047
      std::vector<UEdge> items;
deba@2101
  1048
      for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2101
  1049
        items.push_back(it);
deba@2101
  1050
      }
deba@2467
  1051
      if (forceSort) {
deba@2101
  1052
        if (labelMap) {
deba@2467
  1053
          labelMap->sort(items);
deba@2101
  1054
        } else {
deba@2101
  1055
          typedef IdMap<Graph, UEdge> Map;
deba@2101
  1056
          Map map(graph);
deba@2101
  1057
          _writer_bits::ComposeLess<Map> less(map);
deba@2101
  1058
          std::sort(items.begin(), items.end(), less);
deba@2101
  1059
        }
deba@2101
  1060
      }
deba@1421
  1061
      os << "\t\t";
deba@1901
  1062
      if (forceLabelMap) {
deba@1901
  1063
	os << "label\t";
deba@1421
  1064
      }
deba@2386
  1065
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1421
  1066
	os << writers[i].first << '\t';
deba@1421
  1067
      }
deba@1421
  1068
      os << std::endl;
deba@2282
  1069
      for (typename std::vector<UEdge>::iterator it = items.begin();
deba@2101
  1070
           it != items.end(); ++it) {
deba@2101
  1071
	nodeLabelWriter->write(os, graph.source(*it));
deba@1421
  1072
	os << '\t';
deba@2101
  1073
	nodeLabelWriter->write(os, graph.target(*it));
deba@1421
  1074
	os << '\t';
deba@1901
  1075
	if (forceLabelMap) {
deba@2101
  1076
	  os << graph.id(*it) << '\t';
deba@1421
  1077
	}
deba@2386
  1078
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2101
  1079
	  writers[i].second->write(os, *it);
deba@1421
  1080
	  os << '\t';
deba@1421
  1081
	}
deba@1421
  1082
	os << std::endl;
deba@1421
  1083
      }
deba@1421
  1084
    }
deba@1421
  1085
deba@1421
  1086
  public:
deba@1421
  1087
deba@1901
  1088
    /// \brief Returns true if the undirected edgeset can write the labels of 
deba@1421
  1089
    /// the edges.
deba@1421
  1090
    ///
deba@1901
  1091
    /// Returns true if the undirected edgeset can write the labels of the 
deba@2467
  1092
    /// undirected edges. It is possible only if a "label" named map was 
deba@1901
  1093
    /// written or the \c _forceLabelMap constructor parameter was true.
deba@1901
  1094
    bool isLabelWriter() const {
deba@1901
  1095
      return forceLabelMap || labelMap != 0;
deba@1421
  1096
    }
deba@1421
  1097
deba@1901
  1098
    /// \brief Write the label of the given undirected edge.
deba@1421
  1099
    ///
deba@1901
  1100
    /// It writes the label of the given undirected edge. If there was written 
deba@2467
  1101
    /// a "label" named map then it will write the map value belongs to the 
deba@1901
  1102
    /// undirected edge. Otherwise if the \c forceLabel parameter was true it 
deba@1421
  1103
    /// will write its id in the graph. 
klao@1909
  1104
    void writeLabel(std::ostream& os, const UEdge& item) const {
deba@1901
  1105
      if (forceLabelMap) {
deba@1429
  1106
	os << graph.id(item);
deba@1429
  1107
      } else {
deba@1901
  1108
	labelMap->write(os, item);
deba@1429
  1109
      }
deba@1429
  1110
    } 
deba@1429
  1111
deba@1901
  1112
    /// \brief Write the label of the given edge.
deba@1429
  1113
    ///
deba@1901
  1114
    /// It writes the label of the given edge. If there was written 
deba@2467
  1115
    /// a "label" named map then it will write the map value belongs to the 
deba@1901
  1116
    /// edge. Otherwise if the \c forceLabel parameter was true it 
deba@1429
  1117
    /// will write its id in the graph. If the edge is forward map
deba@1429
  1118
    /// then its prefix character is \c '+' elsewhere \c '-'.
deba@1901
  1119
    void writeLabel(std::ostream& os, const Edge& item) const {
deba@1690
  1120
      if (graph.direction(item)) {
deba@2467
  1121
	os << "+";
deba@1429
  1122
      } else {
deba@2467
  1123
	os << "-";
deba@1429
  1124
      }
deba@1901
  1125
      if (forceLabelMap) {
deba@2467
  1126
	os << graph.id(static_cast<const UEdge&>(item));
deba@1421
  1127
      } else {
deba@1901
  1128
	labelMap->write(os, item);
deba@1421
  1129
      }
deba@1421
  1130
    } 
deba@1421
  1131
deba@2467
  1132
    /// \brief Sorts the given undirected edge vector by label.
deba@2467
  1133
    ///
deba@2467
  1134
    /// Sorts the given undirected edge vector by label. If there was
deba@2467
  1135
    /// written a "label" named map then the vector will be sorted by
deba@2467
  1136
    /// the values of this map. Otherwise if the \c forceLabel
deba@2467
  1137
    /// parameter was true it will be sorted by its id in the graph.
deba@2467
  1138
    void sortByLabel(std::vector<UEdge>& uedges) const {
deba@2467
  1139
      if (labelMap) {
deba@2467
  1140
	labelMap->sort(uedges);
deba@2467
  1141
      } else {
deba@2467
  1142
	typedef IdMap<Graph, UEdge> Map;
deba@2467
  1143
	Map map(graph);
deba@2467
  1144
	_writer_bits::ComposeLess<Map> less(map);
deba@2467
  1145
	std::sort(uedges.begin(), uedges.end(), less);
deba@2467
  1146
      }
deba@2467
  1147
    }
deba@2467
  1148
deba@2467
  1149
    /// \brief Sorts the given edge vector by label.
deba@2467
  1150
    ///
deba@2467
  1151
    /// Sorts the given edge vector by label. If there was written a
deba@2467
  1152
    /// "label" named map then the vector will be sorted by the values
deba@2467
  1153
    /// of this map. Otherwise if the \c forceLabel parameter was true
deba@2467
  1154
    /// it will be sorted by its id in the graph.
deba@2467
  1155
    void sortByLabel(std::vector<Edge>& edges) const {
deba@2467
  1156
      if (labelMap) {
deba@2467
  1157
	labelMap->sort(graph, edges);
deba@2467
  1158
      } else {
deba@2467
  1159
	typedef IdMap<Graph, Edge> Map;
deba@2467
  1160
	Map map(graph);
deba@2467
  1161
	_writer_bits::ComposeLess<Map> less(map);
deba@2467
  1162
	std::sort(edges.begin(), edges.end(), less);
deba@2467
  1163
      }
deba@2467
  1164
    }
deba@2467
  1165
deba@1421
  1166
  private:
deba@1421
  1167
deba@1845
  1168
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@2467
  1169
				  UEdgeMapWriterBase<Graph>*> > MapWriters;
deba@1421
  1170
    MapWriters writers;
deba@1421
  1171
deba@2467
  1172
    _writer_bits::UEdgeMapWriterBase<Graph>* labelMap;
deba@1901
  1173
    bool forceLabelMap;
deba@2467
  1174
    bool forceSort;
deba@1421
  1175
   
deba@1705
  1176
    const Graph& graph;   
deba@1901
  1177
    std::string name;
deba@1409
  1178
deba@1901
  1179
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter;
deba@1409
  1180
  };
deba@1409
  1181
deba@2016
  1182
  /// \ingroup section_io
deba@1901
  1183
  /// \brief SectionWriter for writing named nodes.
deba@1409
  1184
  ///
deba@1901
  1185
  /// The nodes section's header line is \c \@nodes \c nodes_name, but the
deba@1901
  1186
  /// \c nodes_name may be empty.
deba@1409
  1187
  ///
deba@1901
  1188
  /// Each line in the section contains the name of the node and 
deba@1901
  1189
  /// then the node label. 
deba@1409
  1190
  ///
deba@1409
  1191
  /// \relates LemonWriter
deba@1409
  1192
  template <typename _Graph>
deba@1845
  1193
  class NodeWriter : public LemonWriter::SectionWriter {
deba@1845
  1194
    typedef LemonWriter::SectionWriter Parent;
deba@1409
  1195
    typedef _Graph Graph;
deba@1429
  1196
    typedef typename Graph::Node Node;
deba@1409
  1197
  public:
deba@1409
  1198
    
deba@1409
  1199
    /// \brief Constructor.
deba@1409
  1200
    ///
deba@1409
  1201
    /// Constructor for NodeWriter. It creates the NodeWriter and
deba@1901
  1202
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
  1203
    /// will write the nodes' label what can be a nodeset writer.
deba@1901
  1204
    template <typename _LabelWriter>
deba@1901
  1205
    NodeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
  1206
	       const std::string& _name = std::string()) 
deba@1901
  1207
      : Parent(_writer), name(_name) {
deba@1901
  1208
      checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>();
deba@1901
  1209
      labelWriter.reset(new _writer_bits::LabelWriter<Node, _LabelWriter>
deba@1901
  1210
                        (_labelWriter));
deba@1476
  1211
    }
deba@1476
  1212
deba@1409
  1213
deba@1409
  1214
    /// \brief Destructor.
deba@1409
  1215
    ///
deba@1409
  1216
    /// Destructor for NodeWriter.
deba@1409
  1217
    virtual ~NodeWriter() {}
deba@1409
  1218
deba@1409
  1219
  private:
deba@1409
  1220
    NodeWriter(const NodeWriter&);
deba@1409
  1221
    void operator=(const NodeWriter&);
deba@1409
  1222
deba@1409
  1223
  public:
deba@1409
  1224
deba@1409
  1225
    /// \brief Add a node writer command for the NodeWriter.
deba@1409
  1226
    ///
deba@1409
  1227
    /// Add a node writer command for the NodeWriter.
deba@2386
  1228
    void writeNode(std::string label, const Node& item) {
deba@2386
  1229
      writers.push_back(make_pair(label, &item));
deba@1409
  1230
    }
deba@1409
  1231
deba@1409
  1232
  protected:
deba@1409
  1233
deba@1901
  1234
    /// \brief The header of the section.
deba@1409
  1235
    ///
deba@1901
  1236
    /// It gives back the header of the section.
deba@1409
  1237
    virtual std::string header() {
deba@1901
  1238
      return "@nodes " + name;
deba@1409
  1239
    }
deba@1409
  1240
deba@1409
  1241
    /// \brief  Writer function of the section.
deba@1409
  1242
    ///
deba@1409
  1243
    /// Write the content of the section.
deba@1409
  1244
    virtual void write(std::ostream& os) {
deba@1901
  1245
      if (!labelWriter->isLabelWriter()) {
deba@1901
  1246
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1247
      }
deba@2386
  1248
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1409
  1249
	os << writers[i].first << ' ';
deba@1901
  1250
	labelWriter->write(os, *(writers[i].second));
deba@1409
  1251
	os << std::endl;
deba@1409
  1252
      }
deba@1409
  1253
    }
deba@2084
  1254
deba@2084
  1255
    /// \brief Gives back true when the section should be written.
deba@2084
  1256
    ///
deba@2084
  1257
    /// Gives back true when the section should be written.
deba@2084
  1258
    virtual bool valid() { return !writers.empty(); }
deba@1409
  1259
    
deba@1409
  1260
  private:
deba@1409
  1261
deba@1901
  1262
    std::string name;
deba@1409
  1263
deba@1429
  1264
    typedef std::vector<std::pair<std::string, const Node*> > NodeWriters;
deba@1429
  1265
    NodeWriters writers;
deba@1901
  1266
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter;
deba@1409
  1267
  };
deba@1409
  1268
deba@2016
  1269
  /// \ingroup section_io
deba@1901
  1270
  /// \brief SectionWriter for writing named edges.
deba@1409
  1271
  ///
deba@1901
  1272
  /// The edges section's header line is \c \@edges \c edges_name, but the
deba@1901
  1273
  /// \c edges_name may be empty.
deba@1409
  1274
  ///
deba@1901
  1275
  /// Each line in the section contains the name of the edge and 
deba@1901
  1276
  /// then the edge label. 
deba@1409
  1277
  ///
deba@1409
  1278
  /// \relates LemonWriter
deba@1409
  1279
  template <typename _Graph>
deba@1845
  1280
  class EdgeWriter : public LemonWriter::SectionWriter {
deba@1845
  1281
    typedef LemonWriter::SectionWriter Parent;
deba@1409
  1282
    typedef _Graph Graph;
deba@1429
  1283
    typedef typename Graph::Edge Edge;
deba@1409
  1284
  public:
deba@1409
  1285
    
deba@1409
  1286
    /// \brief Constructor.
deba@1409
  1287
    ///
deba@1409
  1288
    /// Constructor for EdgeWriter. It creates the EdgeWriter and
deba@1901
  1289
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
  1290
    /// will write the edges' label what can be a edgeset writer.
deba@1901
  1291
    template <typename _LabelWriter>
deba@1901
  1292
    EdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
  1293
	       const std::string& _name = std::string()) 
deba@1901
  1294
      : Parent(_writer), name(_name) {
deba@1901
  1295
      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
deba@1901
  1296
      labelWriter.reset(new _writer_bits::LabelWriter<Edge, _LabelWriter>(_labelWriter));
deba@1476
  1297
    }
deba@1409
  1298
deba@1409
  1299
    /// \brief Destructor.
deba@1409
  1300
    ///
deba@1409
  1301
    /// Destructor for EdgeWriter.
deba@1409
  1302
    virtual ~EdgeWriter() {}
deba@1409
  1303
  private:
deba@1409
  1304
    EdgeWriter(const EdgeWriter&);
deba@1409
  1305
    void operator=(const EdgeWriter&);
deba@1409
  1306
deba@1409
  1307
  public:
deba@1409
  1308
deba@1421
  1309
    /// \brief Add an edge writer command for the EdgeWriter.
deba@1409
  1310
    ///
deba@1421
  1311
    /// Add an edge writer command for the EdgeWriter.
deba@2386
  1312
    void writeEdge(std::string label, const Edge& item) {
deba@2386
  1313
      writers.push_back(make_pair(label, &item));
deba@1409
  1314
    }
deba@1409
  1315
deba@1409
  1316
  protected:
deba@1409
  1317
deba@1901
  1318
    /// \brief The header of the section.
deba@1409
  1319
    ///
deba@1901
  1320
    /// It gives back the header of the section.
deba@1421
  1321
    virtual std::string header() {
deba@1901
  1322
      return "@edges " + name;
deba@1421
  1323
    }
deba@1421
  1324
deba@1421
  1325
    /// \brief  Writer function of the section.
deba@1421
  1326
    ///
deba@1421
  1327
    /// Write the content of the section.
deba@1421
  1328
    virtual void write(std::ostream& os) {
deba@1901
  1329
      if (!labelWriter->isLabelWriter()) {
deba@1901
  1330
	throw DataFormatError("Cannot find edgeset or label map");
deba@1476
  1331
      }
deba@2386
  1332
      for (int i = 0; i < int(writers.size()); ++i) {
deba@1421
  1333
	os << writers[i].first << ' ';
deba@1901
  1334
	labelWriter->write(os, *(writers[i].second));
deba@1421
  1335
	os << std::endl;
deba@1421
  1336
      }
deba@1421
  1337
    }
deba@2084
  1338
deba@2084
  1339
    /// \brief Gives back true when the section should be written.
deba@2084
  1340
    ///
deba@2084
  1341
    /// Gives back true when the section should be written.
deba@2084
  1342
    virtual bool valid() { return !writers.empty(); }
deba@1421
  1343
    
deba@1421
  1344
  private:
deba@1421
  1345
deba@1901
  1346
    std::string name;
deba@1421
  1347
deba@1429
  1348
    typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
deba@1429
  1349
    EdgeWriters writers;
deba@1421
  1350
deba@1901
  1351
    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter;
deba@1421
  1352
  };
deba@1421
  1353
deba@2467
  1354
deba@2016
  1355
  /// \ingroup section_io
deba@1901
  1356
  /// \brief SectionWriter for writing named undirected edges.
deba@1421
  1357
  ///
klao@1909
  1358
  /// The undirected edges section's header line is \c \@uedges 
klao@1909
  1359
  /// \c uedges_name, but the \c uedges_name may be empty.
deba@1421
  1360
  ///
deba@1901
  1361
  /// Each line in the section contains the name of the undirected edge and 
deba@1901
  1362
  /// then the undirected edge label. 
deba@1421
  1363
  ///
deba@1421
  1364
  /// \relates LemonWriter
deba@1421
  1365
  template <typename _Graph>
klao@1909
  1366
  class UEdgeWriter : public LemonWriter::SectionWriter {
deba@1845
  1367
    typedef LemonWriter::SectionWriter Parent;
deba@1421
  1368
    typedef _Graph Graph;
deba@1429
  1369
    typedef typename Graph::Node Node;
deba@1429
  1370
    typedef typename Graph::Edge Edge;
klao@1909
  1371
    typedef typename Graph::UEdge UEdge;
deba@1421
  1372
  public:
deba@1421
  1373
    
deba@1421
  1374
    /// \brief Constructor.
deba@1421
  1375
    ///
klao@1909
  1376
    /// Constructor for UEdgeWriter. It creates the UEdgeWriter and
deba@1901
  1377
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
  1378
    /// will write the undirected edges' label what can be an undirected 
deba@1421
  1379
    /// edgeset writer.
deba@1901
  1380
    template <typename _LabelWriter>
klao@1909
  1381
    UEdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
  1382
	       const std::string& _name = std::string()) 
deba@1901
  1383
      : Parent(_writer), name(_name) {
deba@1901
  1384
      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
klao@1909
  1385
      checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>();
klao@1909
  1386
      uEdgeLabelWriter.reset(new _writer_bits::
klao@1909
  1387
			      LabelWriter<UEdge, _LabelWriter>(_labelWriter));
deba@1901
  1388
      edgeLabelWriter.reset(new _writer_bits::
deba@1901
  1389
			 LabelWriter<Edge, _LabelWriter>(_labelWriter));
deba@1476
  1390
    }
deba@1421
  1391
deba@1421
  1392
    /// \brief Destructor.
deba@1421
  1393
    ///
klao@1909
  1394
    /// Destructor for UEdgeWriter.
klao@1909
  1395
    virtual ~UEdgeWriter() {}
deba@1421
  1396
  private:
klao@1909
  1397
    UEdgeWriter(const UEdgeWriter&);
klao@1909
  1398
    void operator=(const UEdgeWriter&);
deba@1421
  1399
deba@1421
  1400
  public:
deba@1421
  1401
klao@1909
  1402
    /// \brief Add an edge writer command for the UEdgeWriter.
deba@1429
  1403
    ///
klao@1909
  1404
    /// Add an edge writer command for the UEdgeWriter.
deba@2386
  1405
    void writeEdge(std::string label, const Edge& item) {
deba@2386
  1406
      edgeWriters.push_back(make_pair(label, &item));
deba@1429
  1407
    }
deba@1429
  1408
klao@1909
  1409
    /// \brief Add an undirected edge writer command for the UEdgeWriter.
deba@1421
  1410
    ///
klao@1909
  1411
    /// Add an undirected edge writer command for the UEdgeWriter.
deba@2386
  1412
    void writeUEdge(std::string label, const UEdge& item) {
deba@2386
  1413
      uEdgeWriters.push_back(make_pair(label, &item));
deba@1421
  1414
    }
deba@1421
  1415
deba@1421
  1416
  protected:
deba@1421
  1417
deba@1901
  1418
    /// \brief The header of the section.
deba@1421
  1419
    ///
deba@1901
  1420
    /// It gives back the header of the section.
deba@1409
  1421
    virtual std::string header() {
klao@1909
  1422
      return "@uedges " + name;
deba@1409
  1423
    }
deba@1409
  1424
deba@1409
  1425
    /// \brief  Writer function of the section.
deba@1409
  1426
    ///
deba@1409
  1427
    /// Write the content of the section.
deba@1409
  1428
    virtual void write(std::ostream& os) {
deba@1901
  1429
      if (!edgeLabelWriter->isLabelWriter()) {
deba@1901
  1430
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1431
      }
klao@1909
  1432
      if (!uEdgeLabelWriter->isLabelWriter()) {
deba@1901
  1433
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1434
      }
deba@2386
  1435
      for (int i = 0; i < int(uEdgeWriters.size()); ++i) {
klao@1909
  1436
	os << uEdgeWriters[i].first << ' ';
klao@1909
  1437
	uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second));
deba@1429
  1438
	os << std::endl;
deba@1429
  1439
      }
deba@2386
  1440
      for (int i = 0; i < int(edgeWriters.size()); ++i) {
deba@1429
  1441
	os << edgeWriters[i].first << ' ';
deba@1901
  1442
	edgeLabelWriter->write(os, *(edgeWriters[i].second));
deba@1409
  1443
	os << std::endl;
deba@1409
  1444
      }
deba@1409
  1445
    }
deba@2084
  1446
deba@2084
  1447
    /// \brief Gives back true when the section should be written.
deba@2084
  1448
    ///
deba@2084
  1449
    /// Gives back true when the section should be written.
deba@2084
  1450
    virtual bool valid() { 
deba@2084
  1451
      return !uEdgeWriters.empty() || !edgeWriters.empty(); 
deba@2084
  1452
    }
deba@1409
  1453
    
deba@1409
  1454
  private:
deba@1409
  1455
deba@1901
  1456
    std::string name;
deba@1409
  1457
deba@1429
  1458
    typedef std::vector<std::pair<std::string, 
klao@1909
  1459
				  const UEdge*> > UEdgeWriters;
klao@1909
  1460
    UEdgeWriters uEdgeWriters;
klao@1909
  1461
    std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > uEdgeLabelWriter;
deba@1409
  1462
deba@1429
  1463
    typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
deba@1429
  1464
    EdgeWriters edgeWriters;
deba@1901
  1465
    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter;
deba@1429
  1466
deba@1409
  1467
  };
deba@1409
  1468
deba@2016
  1469
  /// \ingroup section_io
deba@2467
  1470
  /// \brief SectionWriter for writing extra node maps.
deba@2467
  1471
  ///
deba@2467
  1472
  /// The lemon format can store maps in the nodeset. This class let
deba@2467
  1473
  /// you make distinict section to store maps. The main purpose of
deba@2467
  1474
  /// this class is a logical separation of some maps. The other
deba@2467
  1475
  /// useful application could be to store paths in node maps.
deba@2467
  1476
  ///
deba@2467
  1477
  /// The first line of the section contains the names of the maps
deba@2467
  1478
  /// separated with white spaces. Each next line describes an item
deba@2467
  1479
  /// in the itemset, and contains in the first column the label of
deba@2467
  1480
  /// the item and then the mapped values for each map.
deba@2467
  1481
  ///
deba@2467
  1482
  /// \relates LemonWriter
deba@2467
  1483
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@2467
  1484
  class NodeMapWriter : public LemonWriter::SectionWriter {
deba@2467
  1485
    typedef LemonWriter::SectionWriter Parent;
deba@2467
  1486
  public:
deba@2467
  1487
deba@2467
  1488
    typedef _Graph Graph;
deba@2467
  1489
    typedef _Traits Traits;
deba@2467
  1490
    typedef typename Graph::Node Node;
deba@2467
  1491
deba@2467
  1492
    /// \brief Constructor.
deba@2467
  1493
    ///
deba@2467
  1494
    /// Constructor for NodeMapWriter. It creates the NodeMapWriter and
deba@2467
  1495
    /// attach it into the given LemonWriter. If the the
deba@2467
  1496
    /// \c _forceSort is true then the writer will write the edges
deba@2467
  1497
    /// sorted by the labels.
deba@2467
  1498
    template <typename _LabelWriter>
deba@2467
  1499
    NodeMapWriter(LemonWriter& _writer, const Graph& _graph,
deba@2467
  1500
		 const _LabelWriter& _labelWriter,
deba@2467
  1501
		 const std::string& _name = std::string(),
deba@2467
  1502
		 bool _forceSort = true) 
deba@2467
  1503
      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
deba@2467
  1504
      checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>();
deba@2467
  1505
      labelWriter.reset(new _writer_bits::LabelWriter<Node, 
deba@2467
  1506
			_LabelWriter>(_labelWriter));
deba@2467
  1507
    }
deba@2467
  1508
deba@2467
  1509
    /// \brief Destructor.
deba@2467
  1510
    ///
deba@2467
  1511
    /// Destructor for NodeMapWriter.
deba@2467
  1512
    virtual ~NodeMapWriter() {
deba@2467
  1513
      typename MapWriters::iterator it;
deba@2467
  1514
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@2467
  1515
	delete it->second;
deba@2467
  1516
      }
deba@2467
  1517
    }
deba@2467
  1518
deba@2467
  1519
  private:
deba@2467
  1520
    NodeMapWriter(const NodeMapWriter&);
deba@2467
  1521
    void operator=(const NodeMapWriter&);
deba@2467
  1522
  
deba@2467
  1523
  public:
deba@2467
  1524
deba@2467
  1525
    /// \brief Add a new node map writer command for the writer.
deba@2467
  1526
    ///
deba@2467
  1527
    /// Add a new node map writer command for the writer.
deba@2467
  1528
    template <typename Map>
deba@2467
  1529
    NodeMapWriter& writeNodeMap(std::string label, const Map& map) {
deba@2467
  1530
      return writeNodeMap<typename Traits::
deba@2467
  1531
	template Writer<typename Map::Value>, Map>(label, map);
deba@2467
  1532
    }
deba@2467
  1533
deba@2467
  1534
    /// \brief Add a new node map writer command for the writer.
deba@2467
  1535
    ///
deba@2467
  1536
    /// Add a new node map writer command for the writer.
deba@2467
  1537
    template <typename ItemWriter, typename Map>
deba@2467
  1538
    NodeMapWriter& writeNodeMap(std::string label, const Map& map, 
deba@2467
  1539
			   const ItemWriter& iw = ItemWriter()) {
deba@2467
  1540
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@2467
  1541
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
deba@2467
  1542
      writers.push_back(
deba@2467
  1543
	make_pair(label, new _writer_bits::
deba@2467
  1544
		  MapWriter<Node, Map, ItemWriter>(map, iw)));
deba@2467
  1545
      return *this;
deba@2467
  1546
    }
deba@2467
  1547
deba@2467
  1548
  protected:
deba@2467
  1549
deba@2467
  1550
    /// \brief The header of the section.
deba@2467
  1551
    ///
deba@2467
  1552
    /// It gives back the header of the section.
deba@2467
  1553
    virtual std::string header() {
deba@2467
  1554
      return "@nodemaps " + name;
deba@2467
  1555
    }
deba@2467
  1556
deba@2467
  1557
    /// \brief  Writer function of the section.
deba@2467
  1558
    ///
deba@2467
  1559
    /// Write the content of the section.
deba@2467
  1560
    virtual void write(std::ostream& os) {
deba@2467
  1561
      std::vector<Node> nodes;
deba@2467
  1562
      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@2467
  1563
        nodes.push_back(it);
deba@2467
  1564
      }
deba@2467
  1565
      if (forceSort) {
deba@2467
  1566
	labelWriter->sort(nodes);
deba@2467
  1567
      }
deba@2467
  1568
      os << '\t';
deba@2467
  1569
      for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1570
	os << writers[i].first << '\t';
deba@2467
  1571
      }
deba@2467
  1572
      os << std::endl;
deba@2467
  1573
      for (typename std::vector<Node>::iterator it = nodes.begin();
deba@2467
  1574
           it != nodes.end(); ++it) {
deba@2467
  1575
deba@2467
  1576
	labelWriter->write(os, *it); os << '\t';
deba@2467
  1577
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1578
	  writers[i].second->write(os, *it);
deba@2467
  1579
	  os << '\t';
deba@2467
  1580
	}
deba@2467
  1581
	os << std::endl;
deba@2467
  1582
      }
deba@2467
  1583
    }
deba@2467
  1584
deba@2467
  1585
deba@2467
  1586
  private:
deba@2467
  1587
deba@2467
  1588
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@2467
  1589
				  MapWriterBase<Node>*> > MapWriters;
deba@2467
  1590
    MapWriters writers;
deba@2467
  1591
deba@2467
  1592
    _writer_bits::MapWriterBase<Node>* labelMap;
deba@2467
  1593
deba@2467
  1594
    const Graph& graph;   
deba@2467
  1595
    std::string name;
deba@2467
  1596
    bool forceSort;
deba@2467
  1597
deba@2467
  1598
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter;
deba@2467
  1599
  };
deba@2467
  1600
deba@2467
  1601
  /// \ingroup section_io
deba@2467
  1602
  /// \brief SectionWriter for writing extra edge maps.
deba@2467
  1603
  ///
deba@2467
  1604
  /// The lemon format can store maps in the edgeset. This class let
deba@2467
  1605
  /// you make distinict section to store maps. The main purpose of
deba@2467
  1606
  /// this class is a logical separation of some maps. The other
deba@2467
  1607
  /// useful application could be to store paths in edge maps.
deba@2467
  1608
  ///
deba@2467
  1609
  /// The first line of the section contains the names of the maps
deba@2467
  1610
  /// separated with white spaces. Each next line describes an item
deba@2467
  1611
  /// in the itemset, and contains in the first column the label of
deba@2467
  1612
  /// the item and then the mapped values for each map.
deba@2467
  1613
  ///
deba@2467
  1614
  /// \relates LemonWriter
deba@2467
  1615
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@2467
  1616
  class EdgeMapWriter : public LemonWriter::SectionWriter {
deba@2467
  1617
    typedef LemonWriter::SectionWriter Parent;
deba@2467
  1618
  public:
deba@2467
  1619
deba@2467
  1620
    typedef _Graph Graph;
deba@2467
  1621
    typedef _Traits Traits;
deba@2467
  1622
    typedef typename Graph::Edge Edge;
deba@2467
  1623
deba@2467
  1624
    /// \brief Constructor.
deba@2467
  1625
    ///
deba@2467
  1626
    /// Constructor for EdgeMapWriter. It creates the EdgeMapWriter and
deba@2467
  1627
    /// attach it into the given LemonWriter. If the the
deba@2467
  1628
    /// \c _forceSort is true then the writer will write the edges
deba@2467
  1629
    /// sorted by the labels.
deba@2467
  1630
    template <typename _LabelWriter>
deba@2467
  1631
    EdgeMapWriter(LemonWriter& _writer, const Graph& _graph,
deba@2467
  1632
		 const _LabelWriter& _labelWriter,
deba@2467
  1633
		 const std::string& _name = std::string(),
deba@2467
  1634
		 bool _forceSort = true) 
deba@2467
  1635
      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
deba@2467
  1636
      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
deba@2467
  1637
      labelWriter.reset(new _writer_bits::LabelWriter<Edge, 
deba@2467
  1638
			_LabelWriter>(_labelWriter));
deba@2467
  1639
    }
deba@2467
  1640
deba@2467
  1641
    /// \brief Destructor.
deba@2467
  1642
    ///
deba@2467
  1643
    /// Destructor for EdgeMapWriter.
deba@2467
  1644
    virtual ~EdgeMapWriter() {
deba@2467
  1645
      typename MapWriters::iterator it;
deba@2467
  1646
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@2467
  1647
	delete it->second;
deba@2467
  1648
      }
deba@2467
  1649
    }
deba@2467
  1650
deba@2467
  1651
  private:
deba@2467
  1652
    EdgeMapWriter(const EdgeMapWriter&);
deba@2467
  1653
    void operator=(const EdgeMapWriter&);
deba@2467
  1654
  
deba@2467
  1655
  public:
deba@2467
  1656
deba@2467
  1657
    /// \brief Add a new edge map writer command for the writer.
deba@2467
  1658
    ///
deba@2467
  1659
    /// Add a new edge map writer command for the writer.
deba@2467
  1660
    template <typename Map>
deba@2467
  1661
    EdgeMapWriter& writeEdgeMap(std::string label, const Map& map) {
deba@2467
  1662
      return writeEdgeMap<typename Traits::
deba@2467
  1663
	template Writer<typename Map::Value>, Map>(label, map);
deba@2467
  1664
    }
deba@2467
  1665
deba@2467
  1666
    /// \brief Add a new edge map writer command for the writer.
deba@2467
  1667
    ///
deba@2467
  1668
    /// Add a new edge map writer command for the writer.
deba@2467
  1669
    template <typename ItemWriter, typename Map>
deba@2467
  1670
    EdgeMapWriter& writeEdgeMap(std::string label, const Map& map, 
deba@2467
  1671
				const ItemWriter& iw = ItemWriter()) {
deba@2467
  1672
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@2467
  1673
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
deba@2467
  1674
      writers.push_back(
deba@2467
  1675
	make_pair(label, new _writer_bits::
deba@2467
  1676
		  MapWriter<Edge, Map, ItemWriter>(map, iw)));
deba@2467
  1677
      return *this;
deba@2467
  1678
    }
deba@2467
  1679
deba@2467
  1680
  protected:
deba@2467
  1681
deba@2467
  1682
    /// \brief The header of the section.
deba@2467
  1683
    ///
deba@2467
  1684
    /// It gives back the header of the section.
deba@2467
  1685
    virtual std::string header() {
deba@2467
  1686
      return "@edgemaps " + name;
deba@2467
  1687
    }
deba@2467
  1688
deba@2467
  1689
    /// \brief  Writer function of the section.
deba@2467
  1690
    ///
deba@2467
  1691
    /// Write the content of the section.
deba@2467
  1692
    virtual void write(std::ostream& os) {
deba@2467
  1693
      std::vector<Edge> edges;
deba@2467
  1694
      for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) {
deba@2467
  1695
        edges.push_back(it);
deba@2467
  1696
      }
deba@2467
  1697
      if (forceSort) {
deba@2467
  1698
	labelWriter->sort(edges);
deba@2467
  1699
      }
deba@2467
  1700
      os << '\t';
deba@2467
  1701
      for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1702
	os << writers[i].first << '\t';
deba@2467
  1703
      }
deba@2467
  1704
      os << std::endl;
deba@2467
  1705
      for (typename std::vector<Edge>::iterator it = edges.begin();
deba@2467
  1706
           it != edges.end(); ++it) {
deba@2467
  1707
deba@2467
  1708
	labelWriter->write(os, *it); os << '\t';
deba@2467
  1709
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1710
	  writers[i].second->write(os, *it);
deba@2467
  1711
	  os << '\t';
deba@2467
  1712
	}
deba@2467
  1713
	os << std::endl;
deba@2467
  1714
      }
deba@2467
  1715
    }
deba@2467
  1716
deba@2467
  1717
deba@2467
  1718
  private:
deba@2467
  1719
deba@2467
  1720
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@2467
  1721
				  MapWriterBase<Edge>*> > MapWriters;
deba@2467
  1722
    MapWriters writers;
deba@2467
  1723
deba@2467
  1724
    _writer_bits::MapWriterBase<Edge>* labelMap;
deba@2467
  1725
deba@2467
  1726
    const Graph& graph;   
deba@2467
  1727
    std::string name;
deba@2467
  1728
    bool forceSort;
deba@2467
  1729
deba@2467
  1730
    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter;
deba@2467
  1731
  };
deba@2467
  1732
deba@2467
  1733
  /// \ingroup section_io
deba@2467
  1734
  /// \brief SectionWriter for writing extra undirected edge maps.
deba@2467
  1735
  ///
deba@2467
  1736
  /// The lemon format can store maps in the uedgeset. This class let
deba@2467
  1737
  /// you make distinict section to store maps. The main purpose of
deba@2467
  1738
  /// this class is a logical separation of some maps. The other
deba@2467
  1739
  /// useful application could be to store paths in undirected edge
deba@2467
  1740
  /// maps.
deba@2467
  1741
  ///
deba@2467
  1742
  /// The first line of the section contains the names of the maps
deba@2467
  1743
  /// separated with white spaces. Each next line describes an item
deba@2467
  1744
  /// in the itemset, and contains in the first column the label of
deba@2467
  1745
  /// the item and then the mapped values for each map.
deba@2467
  1746
  ///
deba@2467
  1747
  /// \relates LemonWriter
deba@2467
  1748
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@2467
  1749
  class UEdgeMapWriter : public LemonWriter::SectionWriter {
deba@2467
  1750
    typedef LemonWriter::SectionWriter Parent;
deba@2467
  1751
  public:
deba@2467
  1752
deba@2467
  1753
    typedef _Graph Graph;
deba@2467
  1754
    typedef _Traits Traits;
deba@2467
  1755
    typedef typename Graph::UEdge UEdge;
deba@2467
  1756
    typedef typename Graph::Edge Edge;
deba@2467
  1757
deba@2467
  1758
    /// \brief Constructor.
deba@2467
  1759
    ///
deba@2467
  1760
    /// Constructor for UEdgeMapWriter. It creates the UEdgeMapWriter and
deba@2467
  1761
    /// attach it into the given LemonWriter. If the the
deba@2467
  1762
    /// \c _forceSort is true then the writer will write the uedges
deba@2467
  1763
    /// sorted by the labels.
deba@2467
  1764
    template <typename _LabelWriter>
deba@2467
  1765
    UEdgeMapWriter(LemonWriter& _writer, const Graph& _graph,
deba@2467
  1766
		 const _LabelWriter& _labelWriter,
deba@2467
  1767
		 const std::string& _name = std::string(),
deba@2467
  1768
		 bool _forceSort = true) 
deba@2467
  1769
      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
deba@2467
  1770
      checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>();
deba@2467
  1771
      labelWriter.reset(new _writer_bits::LabelWriter<UEdge, 
deba@2467
  1772
			    _LabelWriter>(_labelWriter));
deba@2467
  1773
    }
deba@2467
  1774
deba@2467
  1775
    /// \brief Destructor.
deba@2467
  1776
    ///
deba@2467
  1777
    /// Destructor for UEdgeMapWriter.
deba@2467
  1778
    virtual ~UEdgeMapWriter() {
deba@2467
  1779
      typename MapWriters::iterator it;
deba@2467
  1780
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@2467
  1781
	delete it->second;
deba@2467
  1782
      }
deba@2467
  1783
    }
deba@2467
  1784
deba@2467
  1785
  private:
deba@2467
  1786
    UEdgeMapWriter(const UEdgeMapWriter&);
deba@2467
  1787
    void operator=(const UEdgeMapWriter&);
deba@2467
  1788
  
deba@2467
  1789
  public:
deba@2467
  1790
deba@2467
  1791
    /// \brief Add a new undirected edge map writer command for the writer.
deba@2467
  1792
    ///
deba@2467
  1793
    /// Add a new undirected edge map writer command for the writer.
deba@2467
  1794
    template <typename Map>
deba@2467
  1795
    UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map) {
deba@2467
  1796
      return writeUEdgeMap<typename Traits::
deba@2467
  1797
	template Writer<typename Map::Value>, Map>(label, map);
deba@2467
  1798
    }
deba@2467
  1799
deba@2467
  1800
    /// \brief Add a new undirected edge map writer command for the writer.
deba@2467
  1801
    ///
deba@2467
  1802
    /// Add a new undirected edge map writer command for the writer.
deba@2467
  1803
    template <typename ItemWriter, typename Map>
deba@2467
  1804
    UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map, 
deba@2467
  1805
			   const ItemWriter& iw = ItemWriter()) {
deba@2467
  1806
      checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>();
deba@2467
  1807
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
deba@2467
  1808
      writers.push_back(
deba@2467
  1809
	make_pair(label, new _writer_bits::
deba@2467
  1810
		  MapWriter<UEdge, Map, ItemWriter>(map, iw)));
deba@2467
  1811
      return *this;
deba@2467
  1812
    }
deba@2467
  1813
deba@2467
  1814
    /// \brief Add a new directed edge map writer command for the writer.
deba@2467
  1815
    ///
deba@2467
  1816
    /// Add a new directed map writer command for the writer.
deba@2467
  1817
    template <typename Map>
deba@2467
  1818
    UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map) {
deba@2467
  1819
      return writeEdgeMap<typename Traits::
deba@2467
  1820
	template Writer<typename Map::Value>, Map>(label, map);
deba@2467
  1821
    }
deba@2467
  1822
deba@2467
  1823
    /// \brief Add a new directed map writer command for the writer.
deba@2467
  1824
    ///
deba@2467
  1825
    /// Add a new directed map writer command for the writer.
deba@2467
  1826
    template <typename ItemWriter, typename Map>
deba@2467
  1827
    UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map, 
deba@2467
  1828
                                 const ItemWriter& iw = ItemWriter()) {
deba@2467
  1829
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@2467
  1830
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
deba@2467
  1831
      writeUEdgeMap("+" + label, 
deba@2467
  1832
                    _writer_bits::forwardComposeMap(graph, map), iw);
deba@2467
  1833
      writeUEdgeMap("-" + label, 
deba@2467
  1834
                    _writer_bits::backwardComposeMap(graph, map), iw);
deba@2467
  1835
      return *this;
deba@2467
  1836
    }
deba@2467
  1837
deba@2467
  1838
  protected:
deba@2467
  1839
deba@2467
  1840
    /// \brief The header of the section.
deba@2467
  1841
    ///
deba@2467
  1842
    /// It gives back the header of the section.
deba@2467
  1843
    virtual std::string header() {
deba@2467
  1844
      return "@uedgemaps " + name;
deba@2467
  1845
    }
deba@2467
  1846
deba@2467
  1847
    /// \brief  Writer function of the section.
deba@2467
  1848
    ///
deba@2467
  1849
    /// Write the content of the section.
deba@2467
  1850
    virtual void write(std::ostream& os) {
deba@2467
  1851
      std::vector<UEdge> uedges;
deba@2467
  1852
      for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2467
  1853
        uedges.push_back(it);
deba@2467
  1854
      }
deba@2467
  1855
      if (forceSort) {
deba@2467
  1856
	labelWriter->sort(uedges);
deba@2467
  1857
      }
deba@2467
  1858
      os << '\t';
deba@2467
  1859
      for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1860
	os << writers[i].first << '\t';
deba@2467
  1861
      }
deba@2467
  1862
      os << std::endl;
deba@2467
  1863
      for (typename std::vector<UEdge>::iterator it = uedges.begin();
deba@2467
  1864
           it != uedges.end(); ++it) {
deba@2467
  1865
deba@2467
  1866
	labelWriter->write(os, *it); os << '\t';
deba@2467
  1867
	for (int i = 0; i < int(writers.size()); ++i) {
deba@2467
  1868
	  writers[i].second->write(os, *it);
deba@2467
  1869
	  os << '\t';
deba@2467
  1870
	}
deba@2467
  1871
	os << std::endl;
deba@2467
  1872
      }
deba@2467
  1873
    }
deba@2467
  1874
deba@2467
  1875
deba@2467
  1876
  private:
deba@2467
  1877
deba@2467
  1878
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@2467
  1879
				  MapWriterBase<UEdge>*> > MapWriters;
deba@2467
  1880
    MapWriters writers;
deba@2467
  1881
deba@2467
  1882
    _writer_bits::MapWriterBase<UEdge>* labelMap;
deba@2467
  1883
deba@2467
  1884
    const Graph& graph;   
deba@2467
  1885
    std::string name;
deba@2467
  1886
    bool forceSort;
deba@2467
  1887
deba@2467
  1888
    std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > labelWriter;
deba@2467
  1889
  };
deba@2467
  1890
deba@2467
  1891
deba@2467
  1892
  /// \ingroup section_io
deba@1409
  1893
  /// \brief SectionWriter for attributes.
deba@1409
  1894
  ///
deba@1409
  1895
  /// The lemon format can store multiple attribute set. Each set has
deba@1901
  1896
  /// the header line \c \@attributes \c attributes_name, but the 
deba@1901
  1897
  /// attributeset_name may be empty.
deba@1409
  1898
  ///
deba@1409
  1899
  /// The attributeset section contains several lines. Each of them starts
deba@1409
  1900
  /// with the name of attribute and then the value.
deba@1409
  1901
  ///
deba@1409
  1902
  /// \relates LemonWriter
deba@1409
  1903
  template <typename _Traits = DefaultWriterTraits>
deba@1845
  1904
  class AttributeWriter : public LemonWriter::SectionWriter {
deba@1845
  1905
    typedef LemonWriter::SectionWriter Parent;
deba@1409
  1906
    typedef _Traits Traits; 
deba@1409
  1907
  public:
deba@1409
  1908
    /// \brief Constructor.
deba@1409
  1909
    ///
deba@1409
  1910
    /// Constructor for AttributeWriter. It creates the AttributeWriter and
deba@1409
  1911
    /// attach it into the given LemonWriter.
deba@1409
  1912
    AttributeWriter(LemonWriter& _writer, 
deba@1901
  1913
		    const std::string& _name = std::string()) 
deba@1901
  1914
      : Parent(_writer), name(_name) {}
deba@1409
  1915
deba@1409
  1916
    /// \brief Destructor.
deba@1409
  1917
    ///
deba@1409
  1918
    /// Destructor for AttributeWriter.
deba@1409
  1919
    virtual ~AttributeWriter() {
deba@1409
  1920
      typename Writers::iterator it;
deba@1409
  1921
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
  1922
	delete it->second;
deba@1409
  1923
      }
deba@1409
  1924
    }
deba@1409
  1925
deba@1409
  1926
  private:
deba@1409
  1927
    AttributeWriter(const AttributeWriter&);
deba@1409
  1928
    void operator=(AttributeWriter&);
deba@1409
  1929
deba@1409
  1930
  public:
deba@1409
  1931
    /// \brief Add an attribute writer command for the writer.
deba@1409
  1932
    ///
deba@1409
  1933
    /// Add an attribute writer command for the writer.
deba@1409
  1934
    template <typename Value>
deba@2386
  1935
    AttributeWriter& writeAttribute(std::string label, 
deba@1409
  1936
				    const Value& value) {
deba@1409
  1937
      return 
deba@2414
  1938
	writeAttribute<typename Traits::template Writer<Value> >(label, value);
deba@1409
  1939
    }
deba@1409
  1940
deba@1409
  1941
    /// \brief Add an attribute writer command for the writer.
deba@1409
  1942
    ///
deba@1409
  1943
    /// Add an attribute writer command for the writer.
deba@2386
  1944
    template <typename ItemWriter, typename Value>
deba@2386
  1945
    AttributeWriter& writeAttribute(std::string label, const Value& value,
deba@2386
  1946
				    const ItemWriter& iw = ItemWriter()) {
deba@2386
  1947
      checkConcept<_writer_bits::ItemWriter<Value>, ItemWriter>();
deba@2386
  1948
      writers.push_back(make_pair(label, new _writer_bits::
deba@2386
  1949
				  ValueWriter<Value, ItemWriter>(value, iw)));
deba@1409
  1950
      return *this;
deba@1409
  1951
    }
deba@1409
  1952
deba@1409
  1953
  protected:
deba@1409
  1954
deba@1409
  1955
    /// \brief The header of section.
deba@1409
  1956
    ///
deba@1409
  1957
    /// It gives back the header of the section.
deba@1409
  1958
    std::string header() {
deba@1901
  1959
      return "@attributes " + name;
deba@1409
  1960
    }
deba@1409
  1961
deba@1409
  1962
    /// \brief  Writer function of the section.
deba@1409
  1963
    ///
deba@1409
  1964
    /// Write the content of the section.
deba@1409
  1965
    void write(std::ostream& os) {
deba@1409
  1966
      typename Writers::iterator it;
deba@1409
  1967
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
  1968
	os << it->first << ' ';
deba@1409
  1969
	it->second->write(os);
deba@1409
  1970
	os << std::endl;
deba@1409
  1971
      }
deba@1409
  1972
    }    
deba@1409
  1973
deba@2084
  1974
    /// \brief Gives back true when the section should be written.
deba@2084
  1975
    ///
deba@2084
  1976
    /// Gives back true when the section should be written.
deba@2084
  1977
    virtual bool valid() { return !writers.empty(); }
deba@2084
  1978
deba@1409
  1979
  private:
deba@1901
  1980
    std::string name;
deba@1409
  1981
deba@1845
  1982
    typedef std::vector<std::pair<std::string, 
deba@1845
  1983
				  _writer_bits::ValueWriterBase*> > Writers;
deba@1409
  1984
    Writers writers;  
deba@1409
  1985
  };
deba@1409
  1986
deba@1409
  1987
deba@1409
  1988
}
deba@1409
  1989
#endif