lemon/lemon_writer.h
author alpar
Tue, 21 Feb 2006 12:37:00 +0000
changeset 1977 8ef02f0c4245
parent 1909 2d806130e700
child 1993 2115143eceea
permissions -rw-r--r--
RefPtr: a reference counted pointer class
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@1956
     5
 * Copyright (C) 2003-2006
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@1409
    19
///\ingroup io_group
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@1409
    35
#include <lemon/invalid.h>
deba@1421
    36
#include <lemon/graph_utils.h>
deba@1409
    37
#include <lemon/bits/item_writer.h>
deba@1421
    38
#include <lemon/utility.h>
deba@1421
    39
#include <lemon/maps.h>
deba@1705
    40
#include <lemon/xy.h>
deba@1409
    41
deba@1476
    42
#include <lemon/concept_check.h>
deba@1476
    43
#include <lemon/concept/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@1476
    50
    template <typename Item>
deba@1901
    51
    class ItemLabelWriter {
deba@1476
    52
    public:
deba@1476
    53
deba@1901
    54
      bool isLabelWriter() { return true; }
deba@1476
    55
deba@1901
    56
      void writeLabel(std::ostream&, const Item&) {}
deba@1476
    57
      
deba@1901
    58
      template <class _ItemLabelWriter>
deba@1476
    59
      struct Constraints {
deba@1476
    60
	void constraints() {
deba@1901
    61
	  bool b = writer.isLabelWriter();
deba@1476
    62
	  ignore_unused_variable_warning(b);
deba@1901
    63
	  writer.writeLabel(os, item);
deba@1476
    64
	}
deba@1901
    65
	_ItemLabelWriter& writer;
deba@1476
    66
	std::ostream& os;
deba@1492
    67
	const Item& item;
deba@1492
    68
      };
deba@1492
    69
deba@1492
    70
    };
deba@1492
    71
deba@1492
    72
    template <typename Item>
deba@1492
    73
    class ItemWriter {
deba@1492
    74
    public:
deba@1492
    75
deba@1492
    76
      void write(std::ostream&, const Item&) {}
deba@1492
    77
      
deba@1492
    78
      template <class _ItemWriter>
deba@1492
    79
      struct Constraints {
deba@1492
    80
	void constraints() {
deba@1492
    81
	  writer.write(os, item);
deba@1492
    82
	}
deba@1492
    83
	_ItemWriter& writer;
deba@1492
    84
	std::ostream& os;
deba@1492
    85
	const Item& item;
deba@1476
    86
      };
deba@1476
    87
deba@1476
    88
    };
deba@1476
    89
deba@1705
    90
    template <typename Map>
deba@1705
    91
    struct Ref { typedef const Map& Type; };
deba@1705
    92
deba@1705
    93
    template <typename Graph, typename Map>
deba@1705
    94
    class ForwardComposeMap {
deba@1705
    95
    public:
klao@1909
    96
      typedef typename Graph::UEdge Key;
deba@1705
    97
      typedef typename Map::Value Value;
deba@1705
    98
deba@1705
    99
      ForwardComposeMap(const Graph& _graph, const Map& _map) 
deba@1705
   100
	: graph(_graph), map(_map) {}
deba@1705
   101
      
deba@1705
   102
      Value operator[](const Key& key) {
deba@1705
   103
	return map[graph.direct(key, false)];
deba@1705
   104
      }
deba@1705
   105
deba@1705
   106
    private:
deba@1705
   107
      typename Ref<Map>::Type map;
deba@1705
   108
      const Graph& graph;
deba@1705
   109
    };
deba@1705
   110
deba@1705
   111
    template <typename Graph, typename Map>
deba@1705
   112
    ForwardComposeMap<Graph, Map>
deba@1705
   113
    forwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   114
      return ForwardComposeMap<Graph, Map>(graph, map);
deba@1705
   115
    }
deba@1705
   116
deba@1705
   117
    template <typename Graph, typename Map>
deba@1705
   118
    class BackwardComposeMap {
deba@1705
   119
    public:
klao@1909
   120
      typedef typename Graph::UEdge Key;
deba@1705
   121
      typedef typename Map::Value Value;
deba@1705
   122
deba@1705
   123
      BackwardComposeMap(const Graph& _graph, const Map& _map) 
deba@1705
   124
	: graph(_graph), map(_map) {}
deba@1705
   125
      
deba@1705
   126
      Value operator[](const Key& key) {
deba@1705
   127
	return map[graph.direct(key, false)];
deba@1705
   128
      }
deba@1705
   129
deba@1705
   130
    private:
deba@1705
   131
      typename Ref<Map>::Type map;
deba@1705
   132
      const Graph& graph;
deba@1705
   133
    };
deba@1705
   134
deba@1705
   135
    template <typename Graph, typename Map>
deba@1705
   136
    BackwardComposeMap<Graph, Map>
deba@1705
   137
    backwardComposeMap(const Graph& graph, const Map& map) {
deba@1705
   138
      return BackwardComposeMap<Graph, Map>(graph, map);
deba@1705
   139
    }
deba@1705
   140
deba@1705
   141
    template <typename Graph, typename Map>
deba@1705
   142
    struct Ref<ForwardComposeMap<Graph, Map> > { 
deba@1705
   143
      typedef ForwardComposeMap<Graph, Map> Type;
deba@1705
   144
    };
deba@1705
   145
deba@1705
   146
    template <typename Graph, typename Map>
deba@1705
   147
    struct Ref<BackwardComposeMap<Graph, Map> > { 
deba@1705
   148
      typedef BackwardComposeMap<Graph, Map> Type; 
deba@1705
   149
    };
deba@1705
   150
deba@1705
   151
    template <typename Map>
deba@1705
   152
    struct Ref<XMap<Map> > { 
deba@1705
   153
      typedef XMap<Map> Type;
deba@1705
   154
    };
deba@1705
   155
    template <typename Map>
deba@1705
   156
    struct Ref<ConstXMap<Map> > { 
deba@1705
   157
      typedef ConstXMap<Map> Type;
deba@1705
   158
    };
deba@1705
   159
deba@1705
   160
    template <typename Map>
deba@1705
   161
    struct Ref<YMap<Map> > { 
deba@1705
   162
      typedef YMap<Map> Type;
deba@1705
   163
    };
deba@1705
   164
    template <typename Map>
deba@1705
   165
    struct Ref<ConstYMap<Map> > { 
deba@1705
   166
      typedef ConstYMap<Map> Type;
deba@1705
   167
    };
deba@1705
   168
deba@1845
   169
deba@1845
   170
    template <typename _Item>    
deba@1845
   171
    class MapWriterBase {
deba@1845
   172
    public:
deba@1845
   173
      typedef _Item Item;
deba@1845
   174
deba@1845
   175
      virtual ~MapWriterBase() {}
deba@1845
   176
deba@1852
   177
      virtual void write(std::ostream& os, const Item& item) const = 0;
deba@1845
   178
    };
deba@1845
   179
deba@1845
   180
deba@1845
   181
    template <typename _Item, typename _Map, typename _Writer>
deba@1845
   182
    class MapWriter : public MapWriterBase<_Item> {
deba@1845
   183
    public:
deba@1845
   184
      typedef _Map Map;
deba@1845
   185
      typedef _Writer Writer;
deba@1845
   186
      typedef typename Writer::Value Value;
deba@1845
   187
      typedef _Item Item;
deba@1845
   188
      
deba@1845
   189
      typename _writer_bits::Ref<Map>::Type map;
deba@1845
   190
      Writer writer;
deba@1845
   191
deba@1845
   192
      MapWriter(const Map& _map, const Writer& _writer) 
deba@1845
   193
	: map(_map), writer(_writer) {}
deba@1845
   194
deba@1845
   195
      virtual ~MapWriter() {}
deba@1845
   196
deba@1852
   197
      virtual void write(std::ostream& os, const Item& item) const {
deba@1845
   198
	Value value = map[item];
deba@1845
   199
	writer.write(os, value);
deba@1845
   200
      }
deba@1845
   201
deba@1845
   202
    };
deba@1845
   203
deba@1845
   204
deba@1845
   205
    class ValueWriterBase {
deba@1845
   206
    public:
deba@1845
   207
      virtual ~ValueWriterBase() {}
deba@1845
   208
      virtual void write(std::ostream&) = 0;
deba@1845
   209
    };
deba@1845
   210
deba@1845
   211
    template <typename _Value, typename _Writer>
deba@1845
   212
    class ValueWriter : public ValueWriterBase {
deba@1845
   213
    public:
deba@1845
   214
      typedef _Value Value;
deba@1845
   215
      typedef _Writer Writer;
deba@1845
   216
deba@1845
   217
      ValueWriter(const Value& _value, const Writer& _writer)
deba@1845
   218
 	: value(_value), writer(_writer) {}
deba@1845
   219
deba@1845
   220
      virtual void write(std::ostream& os) {
deba@1845
   221
	writer.write(os, value);
deba@1845
   222
      }
deba@1845
   223
    private:
deba@1845
   224
      const Value& value;
deba@1845
   225
      Writer writer;
deba@1845
   226
    };
deba@1845
   227
    
deba@1845
   228
deba@1845
   229
    template <typename _Item>
deba@1901
   230
    class LabelWriterBase {
deba@1845
   231
    public:
deba@1845
   232
      typedef _Item Item;
deba@1901
   233
      virtual ~LabelWriterBase() {}
deba@1845
   234
      virtual void write(std::ostream&, const Item&) const = 0;
deba@1901
   235
      virtual bool isLabelWriter() const = 0;
deba@1845
   236
    };
deba@1845
   237
deba@1901
   238
    template <typename _Item, typename _BoxedLabelWriter>
deba@1901
   239
    class LabelWriter : public LabelWriterBase<_Item> {
deba@1845
   240
    public:
deba@1845
   241
      typedef _Item Item;
deba@1901
   242
      typedef _BoxedLabelWriter BoxedLabelWriter;
deba@1845
   243
deba@1901
   244
      const BoxedLabelWriter& labelWriter;
deba@1845
   245
deba@1901
   246
      LabelWriter(const BoxedLabelWriter& _labelWriter) 
deba@1901
   247
	: labelWriter(_labelWriter) {}
deba@1845
   248
deba@1845
   249
      virtual void write(std::ostream& os, const Item& item) const {
deba@1901
   250
	labelWriter.writeLabel(os, item);
deba@1845
   251
      }
deba@1845
   252
deba@1901
   253
      virtual bool isLabelWriter() const {
deba@1901
   254
	return labelWriter.isLabelWriter();
deba@1845
   255
      }
deba@1845
   256
    };
deba@1845
   257
deba@1476
   258
  }
deba@1476
   259
deba@1409
   260
  /// \ingroup io_group
deba@1409
   261
  /// \brief Lemon Format writer class.
deba@1409
   262
  /// 
deba@1409
   263
  /// The Lemon Format contains several sections. We do not want to
deba@1409
   264
  /// determine what sections are in a lemon file we give only a framework
deba@1409
   265
  /// to write a section oriented format.
deba@1409
   266
  ///
deba@1409
   267
  /// In the Lemon Format each section starts with a line contains a \c \@
deba@1409
   268
  /// character on the first not white space position. This line is the
deba@1409
   269
  /// header line of the section. Each next lines belong to this section
deba@1409
   270
  /// while it does not starts with \c \@ character. This line can start a 
deba@1409
   271
  /// new section or if it can close the file with the \c \@end line.
deba@1409
   272
  /// The file format ignore the empty lines and it may contain comments
deba@1409
   273
  /// started with a \c # character to the end of the line. 
deba@1409
   274
  ///
deba@1409
   275
  /// The framework provides an abstract LemonWriter::SectionWriter class
deba@1409
   276
  /// what defines the interface of a SectionWriter. The SectionWriter
deba@1409
   277
  /// has the \c header() member function what gives back the header of the
deba@1409
   278
  /// section. After that it will be called the \c write() member which
deba@1409
   279
  /// should write the content of the section.
deba@1409
   280
  ///
deba@1409
   281
  /// \relates GraphWriter
deba@1409
   282
  /// \relates NodeSetWriter
deba@1409
   283
  /// \relates EdgeSetWriter
deba@1409
   284
  /// \relates NodesWriter
deba@1409
   285
  /// \relates EdgesWriter
deba@1409
   286
  /// \relates AttributeWriter
deba@1409
   287
  class LemonWriter {
deba@1409
   288
  public:
deba@1409
   289
deba@1409
   290
    /// \brief Abstract base class for writing a section.
deba@1409
   291
    ///
deba@1409
   292
    /// This class has an \c header() member function what gives back
deba@1409
   293
    /// the header line of the section. The \c write() member should
deba@1409
   294
    /// write the content of the section to the stream.
deba@1409
   295
    class SectionWriter {
deba@1409
   296
      friend class LemonWriter;
deba@1409
   297
    protected:
deba@1409
   298
      /// \brief Constructor for SectionWriter.
deba@1409
   299
      ///
deba@1409
   300
      /// Constructor for SectionWriter. It attach this writer to
deba@1409
   301
      /// the given LemonWriter.
deba@1409
   302
      SectionWriter(LemonWriter& writer) {
deba@1409
   303
	writer.attach(*this);
deba@1409
   304
      }
alpar@1494
   305
      
alpar@1494
   306
      virtual ~SectionWriter() {}
deba@1409
   307
deba@1409
   308
      /// \brief The header of section.
deba@1409
   309
      ///
deba@1409
   310
      /// It gives back the header of the section.
deba@1409
   311
      virtual std::string header() = 0;
deba@1409
   312
deba@1409
   313
      /// \brief  Writer function of the section.
deba@1409
   314
      ///
deba@1409
   315
      /// Write the content of the section.
deba@1409
   316
      virtual void write(std::ostream& os) = 0;
deba@1409
   317
    };
deba@1409
   318
deba@1409
   319
    /// \brief Constructor for LemonWriter.
deba@1409
   320
    ///
deba@1409
   321
    /// Constructor for LemonWriter which writes to the given stream.
deba@1409
   322
    LemonWriter(std::ostream& _os) 
deba@1409
   323
      : os(&_os), own_os(false) {}
deba@1409
   324
deba@1409
   325
    /// \brief Constructor for LemonWriter.
deba@1409
   326
    ///
deba@1409
   327
    /// Constructor for LemonWriter which writes to the given file.
deba@1409
   328
    LemonWriter(const std::string& filename) 
deba@1409
   329
      : os(0), own_os(true) {
deba@1409
   330
      os = new std::ofstream(filename.c_str());
deba@1409
   331
    }
deba@1409
   332
deba@1409
   333
    /// \brief Desctructor for LemonWriter.
deba@1409
   334
    ///
deba@1409
   335
    /// Desctructor for LemonWriter.
deba@1409
   336
    ~LemonWriter() {
deba@1409
   337
      if (own_os) {
deba@1409
   338
	delete os;
deba@1409
   339
      }
deba@1409
   340
    }
deba@1409
   341
deba@1409
   342
  private:
deba@1409
   343
    LemonWriter(const LemonWriter&);
deba@1409
   344
    void operator=(const LemonWriter&);
deba@1409
   345
deba@1409
   346
    void attach(SectionWriter& writer) {
deba@1409
   347
      writers.push_back(&writer);
deba@1409
   348
    }
deba@1409
   349
deba@1409
   350
  public:
deba@1409
   351
deba@1409
   352
    /// \brief Executes the LemonWriter.
deba@1409
   353
    /// 
deba@1409
   354
    /// It executes the LemonWriter.
deba@1409
   355
    void run() {
deba@1409
   356
      SectionWriters::iterator it;
deba@1409
   357
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
   358
	*os << (*it)->header() << std::endl;
deba@1409
   359
	(*it)->write(*os);
deba@1409
   360
      }
deba@1409
   361
      *os << "@end" << std::endl;
deba@1409
   362
    }
deba@1409
   363
deba@1409
   364
deba@1409
   365
  private:
deba@1409
   366
deba@1409
   367
    std::ostream* os;
deba@1409
   368
    bool own_os;
deba@1409
   369
deba@1409
   370
    typedef std::vector<SectionWriter*> SectionWriters;
deba@1409
   371
    SectionWriters writers;
deba@1409
   372
deba@1409
   373
  };
deba@1409
   374
deba@1409
   375
  /// \ingroup io_group
deba@1409
   376
  /// \brief SectionWriter for writing a graph's nodeset.
deba@1409
   377
  ///
deba@1409
   378
  /// The lemon format can store multiple graph nodesets with several maps.
deba@1901
   379
  /// The nodeset section's header line is \c \@nodeset \c nodeset_name, but 
deba@1901
   380
  /// the \c nodeset_name may be empty.
deba@1409
   381
  ///
deba@1409
   382
  /// The first line of the section contains the names of the maps separated
deba@1409
   383
  /// with white spaces. Each next lines describes a node in the nodeset, and
deba@1409
   384
  /// contains the mapped values for each map.
deba@1409
   385
  ///
deba@1901
   386
  /// If the nodeset contains an \c "label" named map then it will be regarded
deba@1901
   387
  /// as label map. This map should contain only unique values and when the 
deba@1901
   388
  /// \c writeLabel() member will be called with a node it will write it's 
deba@1901
   389
  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 
deba@1901
   390
  /// then the label map will be the id in the graph.
deba@1409
   391
  ///
deba@1409
   392
  /// \relates LemonWriter
deba@1409
   393
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@1845
   394
  class NodeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   395
    typedef LemonWriter::SectionWriter Parent;
deba@1409
   396
  public:
deba@1409
   397
deba@1409
   398
    typedef _Graph Graph;
deba@1409
   399
    typedef _Traits Traits;
deba@1429
   400
    typedef typename Graph::Node Node;
deba@1409
   401
deba@1409
   402
    /// \brief Constructor.
deba@1409
   403
    ///
deba@1409
   404
    /// Constructor for NodeSetWriter. It creates the NodeSetWriter and
deba@1901
   405
    /// attach it into the given LemonWriter. If the \c _forceLabelMap
deba@1901
   406
    /// parameter is true then the writer will write own label map when
deba@1901
   407
    /// the user does not give "label" named map.
deba@1409
   408
    NodeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   409
		  const std::string& _name = std::string(), 
deba@1901
   410
		  bool _forceLabelMap = true) 
deba@1901
   411
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 
deba@1901
   412
	graph(_graph), name(_name) {}
deba@1409
   413
deba@1409
   414
    /// \brief Destructor.
deba@1409
   415
    ///
deba@1409
   416
    /// Destructor for NodeSetWriter.
deba@1409
   417
    virtual ~NodeSetWriter() {
deba@1409
   418
      typename MapWriters::iterator it;
deba@1409
   419
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
   420
	delete it->second;
deba@1409
   421
      }
deba@1409
   422
    }
deba@1409
   423
deba@1409
   424
  private:
deba@1409
   425
    NodeSetWriter(const NodeSetWriter&);
deba@1409
   426
    void operator=(const NodeSetWriter&);
deba@1409
   427
  
deba@1409
   428
  public:
deba@1409
   429
deba@1409
   430
    /// \brief Add a new node map writer command for the writer.
deba@1409
   431
    ///
deba@1409
   432
    /// Add a new node map writer command for the writer.
deba@1409
   433
    template <typename Map>
deba@1421
   434
    NodeSetWriter& writeNodeMap(std::string name, const Map& map) {
deba@1421
   435
      return writeNodeMap<typename Traits::
deba@1409
   436
	template Writer<typename Map::Value>, Map>(name, map);
deba@1409
   437
    }
deba@1409
   438
deba@1409
   439
    /// \brief Add a new node map writer command for the writer.
deba@1409
   440
    ///
deba@1409
   441
    /// Add a new node map writer command for the writer.
deba@1409
   442
    template <typename Writer, typename Map>
deba@1421
   443
    NodeSetWriter& writeNodeMap(std::string name, const Map& map, 
deba@1421
   444
			    const Writer& writer = Writer()) {
deba@1492
   445
      checkConcept<concept::ReadMap<Node, typename Map::Value>, Map>();
deba@1492
   446
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
deba@1409
   447
      writers.push_back(
deba@1845
   448
	make_pair(name, new _writer_bits::
deba@1845
   449
		  MapWriter<Node, Map, Writer>(map, writer)));
deba@1409
   450
      return *this;
deba@1409
   451
    }
deba@1409
   452
deba@1409
   453
  protected:
deba@1409
   454
deba@1409
   455
    /// \brief The header of the section.
deba@1409
   456
    ///
deba@1409
   457
    /// It gives back the header of the section.
deba@1409
   458
    virtual std::string header() {
deba@1901
   459
      return "@nodeset " + name;
deba@1409
   460
    }
deba@1409
   461
deba@1409
   462
    /// \brief  Writer function of the section.
deba@1409
   463
    ///
deba@1409
   464
    /// Write the content of the section.
deba@1409
   465
    virtual void write(std::ostream& os) {
deba@1409
   466
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1901
   467
	if (writers[i].first == "label" || (writers[i].first == "id" && labelMap == 0)) {
deba@1901
   468
	  labelMap = writers[i].second;
deba@1901
   469
	  forceLabelMap = false;
deba@1409
   470
	  break;
deba@1409
   471
	}
deba@1409
   472
      }
deba@1901
   473
      if (forceLabelMap) {
deba@1901
   474
	os << "label\t";
deba@1409
   475
      }
deba@1409
   476
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1409
   477
	os << writers[i].first << '\t';
deba@1409
   478
      }
deba@1409
   479
      os << std::endl;
deba@1409
   480
      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1901
   481
	if (forceLabelMap) {
deba@1409
   482
	  os << graph.id(it) << '\t';
deba@1409
   483
	}
deba@1409
   484
	for (int i = 0; i < (int)writers.size(); ++i) {
deba@1409
   485
	  writers[i].second->write(os, it);
deba@1409
   486
	  os << '\t';
deba@1409
   487
	}
deba@1409
   488
	os << std::endl;
deba@1409
   489
      }
deba@1409
   490
    }
deba@1409
   491
deba@1409
   492
  public:
deba@1409
   493
deba@1901
   494
    /// \brief Returns true if the nodeset can write the labels of the nodes.
deba@1409
   495
    ///
deba@1901
   496
    /// Returns true if the nodeset can write the labels of the nodes.
deba@1901
   497
    /// It is possible only if an "label" named map was written or the 
deba@1901
   498
    /// \c _forceLabelMap constructor parameter was true.
deba@1901
   499
    bool isLabelWriter() const {
deba@1901
   500
      return labelMap != 0 || forceLabelMap;
deba@1409
   501
    }
deba@1409
   502
deba@1901
   503
    /// \brief Write the label of the given node.
deba@1409
   504
    ///
deba@1901
   505
    /// It writes the label of the given node. If there was written an "label"
deba@1409
   506
    /// named map then it will write the map value belongs to the node.
deba@1901
   507
    /// Otherwise if the \c forceLabel parameter was true it will write
deba@1901
   508
    /// its label in the graph. 
deba@1901
   509
    void writeLabel(std::ostream& os, const Node& item) const {
deba@1901
   510
      if (forceLabelMap) {
deba@1409
   511
	os << graph.id(item);
deba@1409
   512
      } else {
deba@1901
   513
	labelMap->write(os, item);
deba@1409
   514
      }
deba@1409
   515
    }
deba@1409
   516
deba@1409
   517
  private:
deba@1409
   518
deba@1845
   519
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@1845
   520
				  MapWriterBase<Node>*> > MapWriters;
deba@1409
   521
    MapWriters writers;
deba@1409
   522
deba@1901
   523
    _writer_bits::MapWriterBase<Node>* labelMap;
deba@1901
   524
    bool forceLabelMap;
deba@1409
   525
   
deba@1705
   526
    const Graph& graph;   
deba@1901
   527
    std::string name;
deba@1409
   528
deba@1409
   529
  };
deba@1409
   530
deba@1409
   531
  /// \ingroup io_group
deba@1421
   532
  /// \brief SectionWriter for writing a graph's edgesets.
deba@1409
   533
  ///
deba@1421
   534
  /// The lemon format can store multiple graph edgesets with several maps. 
deba@1901
   535
  /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but 
deba@1901
   536
  /// the \c edgeset_name may be empty.
deba@1409
   537
  ///
deba@1409
   538
  /// The first line of the section contains the names of the maps separated
deba@1409
   539
  /// with white spaces. Each next lines describes a edge in the edgeset. The
deba@1901
   540
  /// line contains the source and the target nodes' label and the mapped 
deba@1409
   541
  /// values for each map.
deba@1409
   542
  ///
deba@1901
   543
  /// If the edgeset contains an \c "label" named map then it will be regarded
deba@1901
   544
  /// as label map. This map should contain only unique values and when the 
deba@1901
   545
  /// \c writeLabel() member will be called with an edge it will write it's 
deba@1901
   546
  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 
deba@1901
   547
  /// then the label map will be the id in the graph.
deba@1409
   548
  ///
deba@1901
   549
  /// The edgeset writer needs a node label writer to identify which nodes
deba@1901
   550
  /// have to be connected. If a NodeSetWriter can write the nodes' label,
deba@1409
   551
  /// it will be able to use with this class.
deba@1409
   552
  ///
deba@1409
   553
  /// \relates LemonWriter
deba@1409
   554
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
deba@1845
   555
  class EdgeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   556
    typedef LemonWriter::SectionWriter Parent;
deba@1409
   557
  public:
deba@1409
   558
deba@1409
   559
    typedef _Graph Graph;
deba@1409
   560
    typedef _Traits Traits;
deba@1429
   561
    typedef typename Graph::Node Node;
deba@1429
   562
    typedef typename Graph::Edge Edge;
deba@1409
   563
deba@1409
   564
    /// \brief Constructor.
deba@1409
   565
    ///
deba@1409
   566
    /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter and
deba@1901
   567
    /// attach it into the given LemonWriter. It will write node labels by
deba@1901
   568
    /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 
deba@1901
   569
    /// then the writer will write own label map if the user does not give 
deba@1901
   570
    /// "label" named map.
deba@1901
   571
    template <typename NodeLabelWriter>
deba@1409
   572
    EdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   573
		  const NodeLabelWriter& _nodeLabelWriter, 
deba@1901
   574
		  const std::string& _name = std::string(),
deba@1901
   575
		  bool _forceLabelMap = true)
deba@1901
   576
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
deba@1901
   577
	graph(_graph), name(_name) {
deba@1901
   578
      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
deba@1901
   579
      nodeLabelWriter.reset(new _writer_bits::
deba@1901
   580
			 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
deba@1476
   581
    } 
deba@1409
   582
deba@1409
   583
    /// \brief Destructor.
deba@1409
   584
    ///
deba@1409
   585
    /// Destructor for EdgeSetWriter.
deba@1409
   586
    virtual ~EdgeSetWriter() {
deba@1409
   587
      typename MapWriters::iterator it;
deba@1409
   588
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
   589
	delete it->second;
deba@1409
   590
      }
deba@1409
   591
    }
deba@1409
   592
deba@1409
   593
  private:
deba@1409
   594
    EdgeSetWriter(const EdgeSetWriter&);
deba@1409
   595
    void operator=(const EdgeSetWriter&);
deba@1409
   596
deba@1409
   597
  public:
deba@1409
   598
deba@1421
   599
    /// \brief Add a new edge map writer command for the writer.
deba@1409
   600
    ///
deba@1421
   601
    /// Add a new edge map writer command for the writer.
deba@1409
   602
    template <typename Map>
deba@1421
   603
    EdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
deba@1421
   604
      return writeEdgeMap<typename Traits::
deba@1409
   605
	template Writer<typename Map::Value>, Map>(name, map);
deba@1409
   606
    }
deba@1409
   607
deba@1421
   608
    /// \brief Add a new edge map writer command for the writer.
deba@1409
   609
    ///
deba@1421
   610
    /// Add a new edge map writer command for the writer.
deba@1409
   611
    template <typename Writer, typename Map>
deba@1421
   612
    EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
deba@1421
   613
			    const Writer& writer = Writer()) {
deba@1492
   614
      checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
deba@1492
   615
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
deba@1409
   616
      writers.push_back(
deba@1845
   617
	make_pair(name, new _writer_bits::
deba@1845
   618
		  MapWriter<Edge, Map, Writer>(map, writer)));
deba@1409
   619
      return *this;
deba@1409
   620
    }
deba@1409
   621
deba@1409
   622
  protected:
deba@1409
   623
deba@1409
   624
    /// \brief The header of the section.
deba@1409
   625
    ///
deba@1409
   626
    /// It gives back the header of the section.
deba@1409
   627
    virtual std::string header() {
deba@1901
   628
      return "@edgeset " + name;
deba@1409
   629
    }
deba@1409
   630
deba@1409
   631
    /// \brief  Writer function of the section.
deba@1409
   632
    ///
deba@1409
   633
    /// Write the content of the section.
deba@1409
   634
    virtual void write(std::ostream& os) {
deba@1901
   635
      if (!nodeLabelWriter->isLabelWriter()) {
deba@1901
   636
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
   637
      }
deba@1409
   638
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1901
   639
	if (writers[i].first == "label" || (writers[i].first == "id" && labelMap == 0)) {
deba@1901
   640
	  labelMap = writers[i].second;
deba@1901
   641
	  forceLabelMap = false;
deba@1409
   642
	  break;
deba@1409
   643
	}
deba@1409
   644
      }
deba@1409
   645
      os << "\t\t";
deba@1901
   646
      if (forceLabelMap) {
deba@1901
   647
	os << "label\t";
deba@1409
   648
      }
deba@1409
   649
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1409
   650
	os << writers[i].first << '\t';
deba@1409
   651
      }
deba@1409
   652
      os << std::endl;
deba@1409
   653
      for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) {
deba@1901
   654
	nodeLabelWriter->write(os, graph.source(it));
deba@1409
   655
	os << '\t';
deba@1901
   656
	nodeLabelWriter->write(os, graph.target(it));
deba@1409
   657
	os << '\t';
deba@1901
   658
	if (forceLabelMap) {
deba@1409
   659
	  os << graph.id(it) << '\t';
deba@1409
   660
	}
deba@1409
   661
	for (int i = 0; i < (int)writers.size(); ++i) {
deba@1409
   662
	  writers[i].second->write(os, it);
deba@1409
   663
	  os << '\t';
deba@1409
   664
	}
deba@1409
   665
	os << std::endl;
deba@1409
   666
      }
deba@1409
   667
    }
deba@1409
   668
deba@1409
   669
  public:
deba@1409
   670
deba@1901
   671
    /// \brief Returns true if the edgeset can write the labels of the edges.
deba@1409
   672
    ///
deba@1901
   673
    /// Returns true if the edgeset can write the labels of the edges.
deba@1901
   674
    /// It is possible only if an "label" named map was written or the 
deba@1901
   675
    /// \c _forceLabelMap constructor parameter was true.
deba@1901
   676
    bool isLabelWriter() const {
deba@1901
   677
      return forceLabelMap || labelMap != 0;
deba@1409
   678
    }
deba@1409
   679
deba@1901
   680
    /// \brief Write the label of the given edge.
deba@1409
   681
    ///
deba@1901
   682
    /// It writes the label of the given edge. If there was written an "label"
deba@1409
   683
    /// named map then it will write the map value belongs to the edge.
deba@1901
   684
    /// Otherwise if the \c forceLabel parameter was true it will write
deba@1901
   685
    /// its label in the graph. 
deba@1901
   686
    void writeLabel(std::ostream& os, const Edge& item) const {
deba@1901
   687
      if (forceLabelMap) {
deba@1409
   688
	os << graph.id(item);
deba@1409
   689
      } else {
deba@1901
   690
	labelMap->write(os, item);
deba@1409
   691
      }
deba@1409
   692
    } 
deba@1409
   693
deba@1409
   694
  private:
deba@1409
   695
deba@1845
   696
    typedef std::vector<std::pair<std::string, _writer_bits::
deba@1845
   697
				  MapWriterBase<Edge>*> > MapWriters;
deba@1409
   698
    MapWriters writers;
deba@1409
   699
deba@1901
   700
    _writer_bits::MapWriterBase<Edge>* labelMap;
deba@1901
   701
    bool forceLabelMap;
deba@1409
   702
   
deba@1705
   703
    const Graph& graph;   
deba@1901
   704
    std::string name;
deba@1421
   705
deba@1901
   706
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter;
deba@1421
   707
  };
deba@1421
   708
deba@1421
   709
  /// \ingroup io_group
deba@1421
   710
  /// \brief SectionWriter for writing a undirected edgeset.
deba@1421
   711
  ///
deba@1421
   712
  /// The lemon format can store multiple undirected edgesets with several 
klao@1909
   713
  /// maps. The undirected edgeset section's header line is \c \@uedgeset 
klao@1909
   714
  /// \c uedgeset_name, but the \c uedgeset_name may be empty.
deba@1421
   715
  ///
deba@1421
   716
  /// The first line of the section contains the names of the maps separated
deba@1421
   717
  /// with white spaces. Each next lines describes an undirected edge in the 
deba@1901
   718
  /// edgeset. The line contains the two connected nodes' label and the mapped 
deba@1421
   719
  /// values for each undirected map.
deba@1421
   720
  ///
deba@1421
   721
  /// The section can handle the directed as a syntactical sugar. Two
deba@1421
   722
  /// undirected edge map describes one directed edge map. This two maps
deba@1421
   723
  /// are the forward map and the backward map and the names of this map
deba@1421
   724
  /// is near the same just with a prefix \c '+' or \c '-' character 
deba@1421
   725
  /// difference.
deba@1421
   726
  ///
deba@1901
   727
  /// If the edgeset contains an \c "label" named map then it will be regarded
deba@1901
   728
  /// as label map. This map should contain only unique values and when the 
deba@1901
   729
  /// \c writeLabel() member will be called with an undirected edge it will 
deba@1901
   730
  /// write it's label. Otherwise if the \c _forceLabelMap constructor 
deba@1901
   731
  /// parameter is true then the label map will be the id in the graph.
deba@1421
   732
  ///
deba@1901
   733
  /// The undirected edgeset writer needs a node label writer to identify 
deba@1421
   734
  /// which nodes have to be connected. If a NodeSetWriter can write the 
deba@1901
   735
  /// nodes' label, it will be able to use with this class.
deba@1421
   736
  ///
deba@1421
   737
  /// \relates LemonWriter
deba@1421
   738
  template <typename _Graph, typename _Traits = DefaultWriterTraits>
klao@1909
   739
  class UEdgeSetWriter : public LemonWriter::SectionWriter {
deba@1845
   740
    typedef LemonWriter::SectionWriter Parent;
deba@1421
   741
  public:
deba@1421
   742
deba@1421
   743
    typedef _Graph Graph;
deba@1421
   744
    typedef _Traits Traits;
deba@1429
   745
    typedef typename Graph::Node Node;
deba@1429
   746
    typedef typename Graph::Edge Edge;
klao@1909
   747
    typedef typename Graph::UEdge UEdge;
deba@1421
   748
deba@1421
   749
    /// \brief Constructor.
deba@1421
   750
    ///
klao@1909
   751
    /// Constructor for UEdgeSetWriter. It creates the UEdgeSetWriter
deba@1901
   752
    /// and attach it into the given LemonWriter. It will write node labels by
deba@1901
   753
    /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 
deba@1901
   754
    /// then the writer will write own label map if the user does not give 
deba@1901
   755
    /// "label" named map.
deba@1901
   756
    template <typename NodeLabelWriter>
klao@1909
   757
    UEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
deba@1901
   758
		       const NodeLabelWriter& _nodeLabelWriter, 
deba@1901
   759
		       const std::string& _name = std::string(),
deba@1901
   760
		       bool _forceLabelMap = true)
deba@1901
   761
      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
deba@1901
   762
	graph(_graph), name(_name) {
deba@1901
   763
      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
deba@1901
   764
      nodeLabelWriter.reset(new _writer_bits::
deba@1901
   765
			 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
deba@1476
   766
    } 
deba@1421
   767
deba@1421
   768
    /// \brief Destructor.
deba@1421
   769
    ///
klao@1909
   770
    /// Destructor for UEdgeSetWriter.
klao@1909
   771
    virtual ~UEdgeSetWriter() {
deba@1421
   772
      typename MapWriters::iterator it;
deba@1421
   773
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1421
   774
	delete it->second;
deba@1421
   775
      }
deba@1421
   776
    }
deba@1421
   777
deba@1421
   778
  private:
klao@1909
   779
    UEdgeSetWriter(const UEdgeSetWriter&);
klao@1909
   780
    void operator=(const UEdgeSetWriter&);
deba@1421
   781
deba@1421
   782
  public:
deba@1421
   783
deba@1421
   784
    /// \brief Add a new undirected edge map writer command for the writer.
deba@1421
   785
    ///
deba@1421
   786
    /// Add a new undirected map writer command for the writer.
deba@1421
   787
    template <typename Map>
klao@1909
   788
    UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map) {
klao@1909
   789
      return writeUEdgeMap<typename Traits::
deba@1421
   790
	template Writer<typename Map::Value>, Map>(name, map);
deba@1421
   791
    }
deba@1421
   792
deba@1421
   793
    /// \brief Add a new undirected map writer command for the writer.
deba@1421
   794
    ///
deba@1421
   795
    /// Add a new undirected map writer command for the writer.
deba@1421
   796
    template <typename Writer, typename Map>
klao@1909
   797
    UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map, 
deba@1421
   798
					  const Writer& writer = Writer()) {
klao@1909
   799
      checkConcept<concept::ReadMap<UEdge, typename Map::Value>, Map>();
deba@1492
   800
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
deba@1421
   801
      writers.push_back(
deba@1845
   802
	make_pair(name, new _writer_bits::
klao@1909
   803
		  MapWriter<UEdge, Map, Writer>(map, writer)));
deba@1421
   804
      return *this;
deba@1421
   805
    }
deba@1421
   806
deba@1421
   807
    /// \brief Add a new directed edge map writer command for the writer.
deba@1421
   808
    ///
deba@1421
   809
    /// Add a new directed map writer command for the writer.
deba@1421
   810
    template <typename Map>
klao@1909
   811
    UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
deba@1492
   812
      return writeEdgeMap<typename Traits::
deba@1492
   813
	template Writer<typename Map::Value>, Map>(name, map);
deba@1421
   814
    }
deba@1421
   815
deba@1421
   816
    /// \brief Add a new directed map writer command for the writer.
deba@1421
   817
    ///
deba@1421
   818
    /// Add a new directed map writer command for the writer.
deba@1421
   819
    template <typename Writer, typename Map>
klao@1909
   820
    UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
deba@1421
   821
				     const Writer& writer = Writer()) {
deba@1492
   822
      checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
deba@1492
   823
      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
klao@1909
   824
      writeUEdge("+" + name, 
deba@1705
   825
		     _writer_bits::forwardComposeMap(graph, map), writer);
klao@1909
   826
      writeUEdge("-" + name, 
deba@1705
   827
		     _writer_bits::backwardComposeMap(graph, map), writer);
deba@1421
   828
      return *this;
deba@1421
   829
    }
deba@1421
   830
deba@1421
   831
  protected:
deba@1421
   832
deba@1421
   833
    /// \brief The header of the section.
deba@1421
   834
    ///
deba@1421
   835
    /// It gives back the header of the section.
deba@1421
   836
    virtual std::string header() {
klao@1909
   837
      return "@uedgeset " + name;
deba@1421
   838
    }
deba@1421
   839
deba@1421
   840
    /// \brief  Writer function of the section.
deba@1421
   841
    ///
deba@1421
   842
    /// Write the content of the section.
deba@1421
   843
    virtual void write(std::ostream& os) {
deba@1901
   844
      if (!nodeLabelWriter->isLabelWriter()) {
deba@1901
   845
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
   846
      }
deba@1421
   847
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1901
   848
	if (writers[i].first == "label") {
deba@1901
   849
	  labelMap = writers[i].second;
deba@1901
   850
	  forceLabelMap = false;
deba@1421
   851
	  break;
deba@1421
   852
	}
deba@1421
   853
      }
deba@1421
   854
      os << "\t\t";
deba@1901
   855
      if (forceLabelMap) {
deba@1901
   856
	os << "label\t";
deba@1421
   857
      }
deba@1421
   858
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1421
   859
	os << writers[i].first << '\t';
deba@1421
   860
      }
deba@1421
   861
      os << std::endl;
klao@1909
   862
      for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@1901
   863
	nodeLabelWriter->write(os, graph.source(it));
deba@1421
   864
	os << '\t';
deba@1901
   865
	nodeLabelWriter->write(os, graph.target(it));
deba@1421
   866
	os << '\t';
deba@1901
   867
	if (forceLabelMap) {
deba@1421
   868
	  os << graph.id(it) << '\t';
deba@1421
   869
	}
deba@1421
   870
	for (int i = 0; i < (int)writers.size(); ++i) {
deba@1421
   871
	  writers[i].second->write(os, it);
deba@1421
   872
	  os << '\t';
deba@1421
   873
	}
deba@1421
   874
	os << std::endl;
deba@1421
   875
      }
deba@1421
   876
    }
deba@1421
   877
deba@1421
   878
  public:
deba@1421
   879
deba@1901
   880
    /// \brief Returns true if the undirected edgeset can write the labels of 
deba@1421
   881
    /// the edges.
deba@1421
   882
    ///
deba@1901
   883
    /// Returns true if the undirected edgeset can write the labels of the 
deba@1901
   884
    /// undirected edges. It is possible only if an "label" named map was 
deba@1901
   885
    /// written or the \c _forceLabelMap constructor parameter was true.
deba@1901
   886
    bool isLabelWriter() const {
deba@1901
   887
      return forceLabelMap || labelMap != 0;
deba@1421
   888
    }
deba@1421
   889
deba@1901
   890
    /// \brief Write the label of the given undirected edge.
deba@1421
   891
    ///
deba@1901
   892
    /// It writes the label of the given undirected edge. If there was written 
deba@1901
   893
    /// an "label" named map then it will write the map value belongs to the 
deba@1901
   894
    /// undirected edge. Otherwise if the \c forceLabel parameter was true it 
deba@1421
   895
    /// will write its id in the graph. 
klao@1909
   896
    void writeLabel(std::ostream& os, const UEdge& item) const {
deba@1901
   897
      if (forceLabelMap) {
deba@1429
   898
	os << graph.id(item);
deba@1429
   899
      } else {
deba@1901
   900
	labelMap->write(os, item);
deba@1429
   901
      }
deba@1429
   902
    } 
deba@1429
   903
deba@1901
   904
    /// \brief Write the label of the given edge.
deba@1429
   905
    ///
deba@1901
   906
    /// It writes the label of the given edge. If there was written 
deba@1901
   907
    /// an "label" named map then it will write the map value belongs to the 
deba@1901
   908
    /// edge. Otherwise if the \c forceLabel parameter was true it 
deba@1429
   909
    /// will write its id in the graph. If the edge is forward map
deba@1429
   910
    /// then its prefix character is \c '+' elsewhere \c '-'.
deba@1901
   911
    void writeLabel(std::ostream& os, const Edge& item) const {
deba@1690
   912
      if (graph.direction(item)) {
deba@1429
   913
	os << "+ ";
deba@1429
   914
      } else {
deba@1429
   915
	os << "- ";
deba@1429
   916
      }
deba@1901
   917
      if (forceLabelMap) {
deba@1421
   918
	os << graph.id(item);
deba@1421
   919
      } else {
deba@1901
   920
	labelMap->write(os, item);
deba@1421
   921
      }
deba@1421
   922
    } 
deba@1421
   923
deba@1421
   924
  private:
deba@1421
   925
deba@1845
   926
    typedef std::vector<std::pair<std::string, _writer_bits::
klao@1909
   927
				  MapWriterBase<UEdge>*> > MapWriters;
deba@1421
   928
    MapWriters writers;
deba@1421
   929
klao@1909
   930
    _writer_bits::MapWriterBase<UEdge>* labelMap;
deba@1901
   931
    bool forceLabelMap;
deba@1421
   932
   
deba@1705
   933
    const Graph& graph;   
deba@1901
   934
    std::string name;
deba@1409
   935
deba@1901
   936
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter;
deba@1409
   937
  };
deba@1409
   938
deba@1409
   939
  /// \ingroup io_group
deba@1901
   940
  /// \brief SectionWriter for writing named nodes.
deba@1409
   941
  ///
deba@1901
   942
  /// The nodes section's header line is \c \@nodes \c nodes_name, but the
deba@1901
   943
  /// \c nodes_name may be empty.
deba@1409
   944
  ///
deba@1901
   945
  /// Each line in the section contains the name of the node and 
deba@1901
   946
  /// then the node label. 
deba@1409
   947
  ///
deba@1409
   948
  /// \relates LemonWriter
deba@1409
   949
  template <typename _Graph>
deba@1845
   950
  class NodeWriter : public LemonWriter::SectionWriter {
deba@1845
   951
    typedef LemonWriter::SectionWriter Parent;
deba@1409
   952
    typedef _Graph Graph;
deba@1429
   953
    typedef typename Graph::Node Node;
deba@1409
   954
  public:
deba@1409
   955
    
deba@1409
   956
    /// \brief Constructor.
deba@1409
   957
    ///
deba@1409
   958
    /// Constructor for NodeWriter. It creates the NodeWriter and
deba@1901
   959
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
   960
    /// will write the nodes' label what can be a nodeset writer.
deba@1901
   961
    template <typename _LabelWriter>
deba@1901
   962
    NodeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
   963
	       const std::string& _name = std::string()) 
deba@1901
   964
      : Parent(_writer), name(_name) {
deba@1901
   965
      checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>();
deba@1901
   966
      labelWriter.reset(new _writer_bits::LabelWriter<Node, _LabelWriter>
deba@1901
   967
                        (_labelWriter));
deba@1476
   968
    }
deba@1476
   969
deba@1409
   970
deba@1409
   971
    /// \brief Destructor.
deba@1409
   972
    ///
deba@1409
   973
    /// Destructor for NodeWriter.
deba@1409
   974
    virtual ~NodeWriter() {}
deba@1409
   975
deba@1409
   976
  private:
deba@1409
   977
    NodeWriter(const NodeWriter&);
deba@1409
   978
    void operator=(const NodeWriter&);
deba@1409
   979
deba@1409
   980
  public:
deba@1409
   981
deba@1409
   982
    /// \brief Add a node writer command for the NodeWriter.
deba@1409
   983
    ///
deba@1409
   984
    /// Add a node writer command for the NodeWriter.
deba@1429
   985
    void writeNode(const std::string& name, const Node& item) {
deba@1409
   986
      writers.push_back(make_pair(name, &item));
deba@1409
   987
    }
deba@1409
   988
deba@1409
   989
  protected:
deba@1409
   990
deba@1901
   991
    /// \brief The header of the section.
deba@1409
   992
    ///
deba@1901
   993
    /// It gives back the header of the section.
deba@1409
   994
    virtual std::string header() {
deba@1901
   995
      return "@nodes " + name;
deba@1409
   996
    }
deba@1409
   997
deba@1409
   998
    /// \brief  Writer function of the section.
deba@1409
   999
    ///
deba@1409
  1000
    /// Write the content of the section.
deba@1409
  1001
    virtual void write(std::ostream& os) {
deba@1901
  1002
      if (!labelWriter->isLabelWriter()) {
deba@1901
  1003
	throw DataFormatError("Cannot find nodeset or label map");
deba@1476
  1004
      }
deba@1409
  1005
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1409
  1006
	os << writers[i].first << ' ';
deba@1901
  1007
	labelWriter->write(os, *(writers[i].second));
deba@1409
  1008
	os << std::endl;
deba@1409
  1009
      }
deba@1409
  1010
    }
deba@1409
  1011
    
deba@1409
  1012
  private:
deba@1409
  1013
deba@1901
  1014
    std::string name;
deba@1409
  1015
deba@1429
  1016
    typedef std::vector<std::pair<std::string, const Node*> > NodeWriters;
deba@1429
  1017
    NodeWriters writers;
deba@1901
  1018
    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter;
deba@1409
  1019
  };
deba@1409
  1020
deba@1409
  1021
  /// \ingroup io_group
deba@1901
  1022
  /// \brief SectionWriter for writing named edges.
deba@1409
  1023
  ///
deba@1901
  1024
  /// The edges section's header line is \c \@edges \c edges_name, but the
deba@1901
  1025
  /// \c edges_name may be empty.
deba@1409
  1026
  ///
deba@1901
  1027
  /// Each line in the section contains the name of the edge and 
deba@1901
  1028
  /// then the edge label. 
deba@1409
  1029
  ///
deba@1409
  1030
  /// \relates LemonWriter
deba@1409
  1031
  template <typename _Graph>
deba@1845
  1032
  class EdgeWriter : public LemonWriter::SectionWriter {
deba@1845
  1033
    typedef LemonWriter::SectionWriter Parent;
deba@1409
  1034
    typedef _Graph Graph;
deba@1429
  1035
    typedef typename Graph::Edge Edge;
deba@1409
  1036
  public:
deba@1409
  1037
    
deba@1409
  1038
    /// \brief Constructor.
deba@1409
  1039
    ///
deba@1409
  1040
    /// Constructor for EdgeWriter. It creates the EdgeWriter and
deba@1901
  1041
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
  1042
    /// will write the edges' label what can be a edgeset writer.
deba@1901
  1043
    template <typename _LabelWriter>
deba@1901
  1044
    EdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
  1045
	       const std::string& _name = std::string()) 
deba@1901
  1046
      : Parent(_writer), name(_name) {
deba@1901
  1047
      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
deba@1901
  1048
      labelWriter.reset(new _writer_bits::LabelWriter<Edge, _LabelWriter>(_labelWriter));
deba@1476
  1049
    }
deba@1409
  1050
deba@1409
  1051
    /// \brief Destructor.
deba@1409
  1052
    ///
deba@1409
  1053
    /// Destructor for EdgeWriter.
deba@1409
  1054
    virtual ~EdgeWriter() {}
deba@1409
  1055
  private:
deba@1409
  1056
    EdgeWriter(const EdgeWriter&);
deba@1409
  1057
    void operator=(const EdgeWriter&);
deba@1409
  1058
deba@1409
  1059
  public:
deba@1409
  1060
deba@1421
  1061
    /// \brief Add an edge writer command for the EdgeWriter.
deba@1409
  1062
    ///
deba@1421
  1063
    /// Add an edge writer command for the EdgeWriter.
deba@1429
  1064
    void writeEdge(const std::string& name, const Edge& item) {
deba@1409
  1065
      writers.push_back(make_pair(name, &item));
deba@1409
  1066
    }
deba@1409
  1067
deba@1409
  1068
  protected:
deba@1409
  1069
deba@1901
  1070
    /// \brief The header of the section.
deba@1409
  1071
    ///
deba@1901
  1072
    /// It gives back the header of the section.
deba@1421
  1073
    virtual std::string header() {
deba@1901
  1074
      return "@edges " + name;
deba@1421
  1075
    }
deba@1421
  1076
deba@1421
  1077
    /// \brief  Writer function of the section.
deba@1421
  1078
    ///
deba@1421
  1079
    /// Write the content of the section.
deba@1421
  1080
    virtual void write(std::ostream& os) {
deba@1901
  1081
      if (!labelWriter->isLabelWriter()) {
deba@1901
  1082
	throw DataFormatError("Cannot find edgeset or label map");
deba@1476
  1083
      }
deba@1421
  1084
      for (int i = 0; i < (int)writers.size(); ++i) {
deba@1421
  1085
	os << writers[i].first << ' ';
deba@1901
  1086
	labelWriter->write(os, *(writers[i].second));
deba@1421
  1087
	os << std::endl;
deba@1421
  1088
      }
deba@1421
  1089
    }
deba@1421
  1090
    
deba@1421
  1091
  private:
deba@1421
  1092
deba@1901
  1093
    std::string name;
deba@1421
  1094
deba@1429
  1095
    typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
deba@1429
  1096
    EdgeWriters writers;
deba@1421
  1097
deba@1901
  1098
    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter;
deba@1421
  1099
  };
deba@1421
  1100
deba@1421
  1101
  /// \ingroup io_group
deba@1901
  1102
  /// \brief SectionWriter for writing named undirected edges.
deba@1421
  1103
  ///
klao@1909
  1104
  /// The undirected edges section's header line is \c \@uedges 
klao@1909
  1105
  /// \c uedges_name, but the \c uedges_name may be empty.
deba@1421
  1106
  ///
deba@1901
  1107
  /// Each line in the section contains the name of the undirected edge and 
deba@1901
  1108
  /// then the undirected edge label. 
deba@1421
  1109
  ///
deba@1421
  1110
  /// \relates LemonWriter
deba@1421
  1111
  template <typename _Graph>
klao@1909
  1112
  class UEdgeWriter : public LemonWriter::SectionWriter {
deba@1845
  1113
    typedef LemonWriter::SectionWriter Parent;
deba@1421
  1114
    typedef _Graph Graph;
deba@1429
  1115
    typedef typename Graph::Node Node;
deba@1429
  1116
    typedef typename Graph::Edge Edge;
klao@1909
  1117
    typedef typename Graph::UEdge UEdge;
deba@1421
  1118
  public:
deba@1421
  1119
    
deba@1421
  1120
    /// \brief Constructor.
deba@1421
  1121
    ///
klao@1909
  1122
    /// Constructor for UEdgeWriter. It creates the UEdgeWriter and
deba@1901
  1123
    /// attach it into the given LemonWriter. The given \c _LabelWriter
deba@1901
  1124
    /// will write the undirected edges' label what can be an undirected 
deba@1421
  1125
    /// edgeset writer.
deba@1901
  1126
    template <typename _LabelWriter>
klao@1909
  1127
    UEdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
deba@1901
  1128
	       const std::string& _name = std::string()) 
deba@1901
  1129
      : Parent(_writer), name(_name) {
deba@1901
  1130
      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
klao@1909
  1131
      checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>();
klao@1909
  1132
      uEdgeLabelWriter.reset(new _writer_bits::
klao@1909
  1133
			      LabelWriter<UEdge, _LabelWriter>(_labelWriter));
deba@1901
  1134
      edgeLabelWriter.reset(new _writer_bits::
deba@1901
  1135
			 LabelWriter<Edge, _LabelWriter>(_labelWriter));
deba@1476
  1136
    }
deba@1421
  1137
deba@1421
  1138
    /// \brief Destructor.
deba@1421
  1139
    ///
klao@1909
  1140
    /// Destructor for UEdgeWriter.
klao@1909
  1141
    virtual ~UEdgeWriter() {}
deba@1421
  1142
  private:
klao@1909
  1143
    UEdgeWriter(const UEdgeWriter&);
klao@1909
  1144
    void operator=(const UEdgeWriter&);
deba@1421
  1145
deba@1421
  1146
  public:
deba@1421
  1147
klao@1909
  1148
    /// \brief Add an edge writer command for the UEdgeWriter.
deba@1429
  1149
    ///
klao@1909
  1150
    /// Add an edge writer command for the UEdgeWriter.
deba@1429
  1151
    void writeEdge(const std::string& name, const Edge& item) {
deba@1429
  1152
      edgeWriters.push_back(make_pair(name, &item));
deba@1429
  1153
    }
deba@1429
  1154
klao@1909
  1155
    /// \brief Add an undirected edge writer command for the UEdgeWriter.
deba@1421
  1156
    ///
klao@1909
  1157
    /// Add an undirected edge writer command for the UEdgeWriter.
klao@1909
  1158
    void writeUEdge(const std::string& name, const UEdge& item) {
klao@1909
  1159
      uEdgeWriters.push_back(make_pair(name, &item));
deba@1421
  1160
    }
deba@1421
  1161
deba@1421
  1162
  protected:
deba@1421
  1163
deba@1901
  1164
    /// \brief The header of the section.
deba@1421
  1165
    ///
deba@1901
  1166
    /// It gives back the header of the section.
deba@1409
  1167
    virtual std::string header() {
klao@1909
  1168
      return "@uedges " + name;
deba@1409
  1169
    }
deba@1409
  1170
deba@1409
  1171
    /// \brief  Writer function of the section.
deba@1409
  1172
    ///
deba@1409
  1173
    /// Write the content of the section.
deba@1409
  1174
    virtual void write(std::ostream& os) {
deba@1901
  1175
      if (!edgeLabelWriter->isLabelWriter()) {
deba@1901
  1176
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1177
      }
klao@1909
  1178
      if (!uEdgeLabelWriter->isLabelWriter()) {
deba@1901
  1179
	throw DataFormatError("Cannot find undirected edgeset or label map");
deba@1476
  1180
      }
klao@1909
  1181
      for (int i = 0; i < (int)uEdgeWriters.size(); ++i) {
klao@1909
  1182
	os << uEdgeWriters[i].first << ' ';
klao@1909
  1183
	uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second));
deba@1429
  1184
	os << std::endl;
deba@1429
  1185
      }
deba@1429
  1186
      for (int i = 0; i < (int)edgeWriters.size(); ++i) {
deba@1429
  1187
	os << edgeWriters[i].first << ' ';
deba@1901
  1188
	edgeLabelWriter->write(os, *(edgeWriters[i].second));
deba@1409
  1189
	os << std::endl;
deba@1409
  1190
      }
deba@1409
  1191
    }
deba@1409
  1192
    
deba@1409
  1193
  private:
deba@1409
  1194
deba@1901
  1195
    std::string name;
deba@1409
  1196
deba@1429
  1197
    typedef std::vector<std::pair<std::string, 
klao@1909
  1198
				  const UEdge*> > UEdgeWriters;
klao@1909
  1199
    UEdgeWriters uEdgeWriters;
klao@1909
  1200
    std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > uEdgeLabelWriter;
deba@1409
  1201
deba@1429
  1202
    typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
deba@1429
  1203
    EdgeWriters edgeWriters;
deba@1901
  1204
    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter;
deba@1429
  1205
deba@1409
  1206
  };
deba@1409
  1207
deba@1409
  1208
  /// \ingroup io_group
deba@1409
  1209
  /// \brief SectionWriter for attributes.
deba@1409
  1210
  ///
deba@1409
  1211
  /// The lemon format can store multiple attribute set. Each set has
deba@1901
  1212
  /// the header line \c \@attributes \c attributes_name, but the 
deba@1901
  1213
  /// attributeset_name may be empty.
deba@1409
  1214
  ///
deba@1409
  1215
  /// The attributeset section contains several lines. Each of them starts
deba@1409
  1216
  /// with the name of attribute and then the value.
deba@1409
  1217
  ///
deba@1409
  1218
  /// \relates LemonWriter
deba@1409
  1219
  template <typename _Traits = DefaultWriterTraits>
deba@1845
  1220
  class AttributeWriter : public LemonWriter::SectionWriter {
deba@1845
  1221
    typedef LemonWriter::SectionWriter Parent;
deba@1409
  1222
    typedef _Traits Traits; 
deba@1409
  1223
  public:
deba@1409
  1224
    /// \brief Constructor.
deba@1409
  1225
    ///
deba@1409
  1226
    /// Constructor for AttributeWriter. It creates the AttributeWriter and
deba@1409
  1227
    /// attach it into the given LemonWriter.
deba@1409
  1228
    AttributeWriter(LemonWriter& _writer, 
deba@1901
  1229
		    const std::string& _name = std::string()) 
deba@1901
  1230
      : Parent(_writer), name(_name) {}
deba@1409
  1231
deba@1409
  1232
    /// \brief Destructor.
deba@1409
  1233
    ///
deba@1409
  1234
    /// Destructor for AttributeWriter.
deba@1409
  1235
    virtual ~AttributeWriter() {
deba@1409
  1236
      typename Writers::iterator it;
deba@1409
  1237
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
  1238
	delete it->second;
deba@1409
  1239
      }
deba@1409
  1240
    }
deba@1409
  1241
deba@1409
  1242
  private:
deba@1409
  1243
    AttributeWriter(const AttributeWriter&);
deba@1409
  1244
    void operator=(AttributeWriter&);
deba@1409
  1245
deba@1409
  1246
  public:
deba@1409
  1247
    /// \brief Add an attribute writer command for the writer.
deba@1409
  1248
    ///
deba@1409
  1249
    /// Add an attribute writer command for the writer.
deba@1409
  1250
    template <typename Value>
deba@1901
  1251
    AttributeWriter& writeAttribute(const std::string& name, 
deba@1409
  1252
				    const Value& value) {
deba@1409
  1253
      return 
deba@1901
  1254
	writeAttribute<typename Traits::template Writer<Value> >(name, value);
deba@1409
  1255
    }
deba@1409
  1256
deba@1409
  1257
    /// \brief Add an attribute writer command for the writer.
deba@1409
  1258
    ///
deba@1409
  1259
    /// Add an attribute writer command for the writer.
deba@1409
  1260
    template <typename Writer, typename Value>
deba@1409
  1261
    AttributeWriter& writeAttribute(const std::string& name, 
deba@1409
  1262
				    const Value& value,
deba@1409
  1263
				    const Writer& writer = Writer()) {
deba@1492
  1264
      checkConcept<_writer_bits::ItemWriter<Value>, Writer>();
deba@1845
  1265
      writers.push_back(make_pair(name, new _writer_bits::
deba@1845
  1266
				  ValueWriter<Value, Writer>(value, writer)));
deba@1409
  1267
      return *this;
deba@1409
  1268
    }
deba@1409
  1269
deba@1409
  1270
  protected:
deba@1409
  1271
deba@1409
  1272
    /// \brief The header of section.
deba@1409
  1273
    ///
deba@1409
  1274
    /// It gives back the header of the section.
deba@1409
  1275
    std::string header() {
deba@1901
  1276
      return "@attributes " + name;
deba@1409
  1277
    }
deba@1409
  1278
deba@1409
  1279
    /// \brief  Writer function of the section.
deba@1409
  1280
    ///
deba@1409
  1281
    /// Write the content of the section.
deba@1409
  1282
    void write(std::ostream& os) {
deba@1409
  1283
      typename Writers::iterator it;
deba@1409
  1284
      for (it = writers.begin(); it != writers.end(); ++it) {
deba@1409
  1285
	os << it->first << ' ';
deba@1409
  1286
	it->second->write(os);
deba@1409
  1287
	os << std::endl;
deba@1409
  1288
      }
deba@1409
  1289
    }    
deba@1409
  1290
deba@1409
  1291
  private:
deba@1901
  1292
    std::string name;
deba@1409
  1293
deba@1845
  1294
    typedef std::vector<std::pair<std::string, 
deba@1845
  1295
				  _writer_bits::ValueWriterBase*> > Writers;
deba@1409
  1296
    Writers writers;  
deba@1409
  1297
  };
deba@1409
  1298
deba@1409
  1299
deba@1409
  1300
}
deba@1409
  1301
#endif