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