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