lemon/graph_writer.h
author ladanyi
Wed, 21 Jun 2006 08:35:23 +0000
changeset 2103 a979fcdda073
parent 2037 32e4bebee616
child 2200 2f2ac1b1ca1e
permissions -rw-r--r--
Exclude the gui from the build.
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1137
     8
 *
deba@1137
     9
 * Permission to use, modify and distribute this software is granted
deba@1137
    10
 * provided that this copyright notice appears in all copies. For
deba@1137
    11
 * precise terms see the accompanying LICENSE file.
deba@1137
    12
 *
deba@1137
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1137
    14
 * express or implied, and with no claim as to its suitability for any
deba@1137
    15
 * purpose.
deba@1137
    16
 *
deba@1137
    17
 */
deba@1137
    18
deba@2083
    19
///\ingroup lemon_io
deba@1137
    20
///\file
alpar@1287
    21
///\brief Lemon Graph Format writer.
athos@1534
    22
///
deba@1137
    23
deba@1214
    24
#ifndef LEMON_GRAPH_WRITER_H
deba@1214
    25
#define LEMON_GRAPH_WRITER_H
deba@1137
    26
deba@1137
    27
#include <iostream>
deba@1137
    28
deba@1137
    29
#include <lemon/error.h>
deba@1409
    30
#include <lemon/lemon_writer.h>
deba@1137
    31
deba@1137
    32
namespace lemon {
deba@1137
    33
deba@2083
    34
  /// \addtogroup lemon_io
deba@1333
    35
  /// @{
deba@1333
    36
deba@1137
    37
  /// \brief The graph writer class.
deba@1137
    38
  ///
athos@1526
    39
  /// The \c GraphWriter class provides the graph output. 
athos@1526
    40
  /// Before you read this documentation it might be useful to read the general
athos@1526
    41
  /// description of  \ref graph-io-page "Graph Input-Output".
athos@1540
    42
  ///
athos@1526
    43
  /// To write a graph
athos@1526
    44
  /// you should first give writing commands to the writer. You can declare
athos@1526
    45
  /// write commands as \c NodeMap or \c EdgeMap writing and labeled Node and
deba@1333
    46
  /// Edge writing.
deba@1333
    47
  ///
alpar@1946
    48
  ///\code
deba@1333
    49
  /// GraphWriter<ListGraph> writer(std::cout, graph);
alpar@1946
    50
  ///\endcode
deba@1333
    51
  ///
deba@1394
    52
  /// The \c writeNodeMap() function declares a \c NodeMap writing 
deba@1394
    53
  /// command in the \c GraphWriter. You should give as parameter 
deba@1394
    54
  /// the name of the map and the map object. The NodeMap writing 
deba@1901
    55
  /// command with name "label" should write a unique map because it 
deba@1901
    56
  /// is regarded as label map (such a map is essential if the graph has edges).
deba@1333
    57
  ///
alpar@1946
    58
  ///\code
deba@1901
    59
  /// IdMap<ListGraph, Node> nodeLabelMap;
deba@1901
    60
  /// writer.writeNodeMap("label", nodeLabelMap);
deba@1333
    61
  ///
deba@1421
    62
  /// writer.writeNodeMap("coords", coords);
deba@1394
    63
  /// writer.writeNodeMap("color", colorMap);
alpar@1946
    64
  ///\endcode
deba@1333
    65
  ///
deba@1394
    66
  /// With the \c writeEdgeMap() member function you can give an edge map
deba@1333
    67
  /// writing command similar to the NodeMaps.
deba@1333
    68
  ///
alpar@1946
    69
  ///\code
deba@1333
    70
  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
deba@1333
    71
  ///   edgeDescMap(graph);
deba@1394
    72
  /// writer.writeEdgeMap("descriptor", edgeDescMap);
deba@1333
    73
  ///
deba@1394
    74
  /// writer.writeEdgeMap("weight", weightMap);
deba@1394
    75
  /// writer.writeEdgeMap("label", labelMap);
alpar@1946
    76
  ///\endcode
deba@1333
    77
  ///
deba@1394
    78
  /// With \c writeNode() and \c writeEdge() functions you can 
athos@1526
    79
  /// point out Nodes and Edges in the graph. For example, you can 
athos@1526
    80
  /// write out the source and target of a maximum flow instance.
deba@1333
    81
  ///
alpar@1946
    82
  ///\code
deba@1394
    83
  /// writer.writeNode("source", sourceNode);
deba@1394
    84
  /// writer.writeNode("target", targetNode);
deba@1333
    85
  ///
deba@1394
    86
  /// writer.writeEdge("observed", edge);
alpar@1946
    87
  ///\endcode
deba@1333
    88
  ///
deba@1333
    89
  /// After you give all write commands you must call the \c run() member
athos@1526
    90
  /// function, which executes all the writing commands.
deba@1333
    91
  ///
alpar@1946
    92
  ///\code
deba@1333
    93
  /// writer.run();
alpar@1946
    94
  ///\endcode
deba@1333
    95
  ///
alpar@1287
    96
  /// \see DefaultWriterTraits
alpar@1287
    97
  /// \see QuotedStringWriter
deba@1333
    98
  /// \see IdMap
deba@1333
    99
  /// \see DescriptorMap
deba@1421
   100
  /// \see \ref GraphReader
alpar@1138
   101
  /// \see \ref graph-io-page
deba@1333
   102
  /// \author Balazs Dezso
deba@1137
   103
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
deba@1137
   104
  class GraphWriter {
deba@1137
   105
  public:
deba@1137
   106
    
deba@1137
   107
    typedef _Graph Graph;
deba@1137
   108
    typedef typename Graph::Node Node;
deba@1137
   109
    typedef typename Graph::Edge Edge;
deba@1137
   110
deba@1137
   111
    typedef _WriterTraits WriterTraits;
deba@1409
   112
deba@1137
   113
    /// \brief Construct a new GraphWriter.
deba@1137
   114
    ///
athos@1526
   115
    /// This function constructs a new GraphWriter to write the given graph
deba@1409
   116
    /// to the given stream.
deba@1208
   117
    GraphWriter(std::ostream& _os, const Graph& _graph) 
deba@1409
   118
      : writer(new LemonWriter(_os)), own_writer(true), 
deba@1421
   119
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   120
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   121
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   122
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   123
	attribute_writer(*writer, std::string()) {}
deba@1137
   124
deba@1409
   125
    /// \brief Construct a new GraphWriter.
deba@1409
   126
    ///
athos@1526
   127
    /// This function constructs a new GraphWriter to write the given graph
deba@1409
   128
    /// to the given file.
deba@1409
   129
    GraphWriter(const std::string& _filename, const Graph& _graph) 
deba@1409
   130
      : writer(new LemonWriter(_filename)), own_writer(true), 
deba@1421
   131
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   132
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   133
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   134
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   135
	attribute_writer(*writer, std::string()) {}
deba@1409
   136
deba@1409
   137
    /// \brief Construct a new GraphWriter.
deba@1409
   138
    ///
athos@1526
   139
    /// This function constructs a new GraphWriter to write the given graph
athos@1526
   140
    /// to the given LemonReader.
deba@1409
   141
    GraphWriter(LemonWriter& _writer, const Graph& _graph)
deba@1409
   142
      : writer(_writer), own_writer(false), 
deba@1421
   143
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   144
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   145
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   146
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   147
	attribute_writer(*writer, std::string()) {}
deba@1137
   148
deba@1137
   149
    /// \brief Destruct the graph writer.
deba@1137
   150
    ///
athos@1526
   151
    /// This function destructs the graph writer.
deba@1137
   152
    ~GraphWriter() {
deba@1409
   153
      if (own_writer) 
deba@1409
   154
	delete writer;
deba@1137
   155
    }
deba@1137
   156
athos@1526
   157
    /// \brief Issue a new node map writing command for the writer.
deba@1137
   158
    ///
athos@1526
   159
   /// This function issues a new <i> node map writing command</i> to the writer.
deba@1137
   160
    template <typename Map>
deba@1394
   161
    GraphWriter& writeNodeMap(std::string name, const Map& map) {
deba@1421
   162
      nodeset_writer.writeNodeMap(name, map);
deba@1409
   163
      return *this;
deba@1137
   164
    }
deba@1137
   165
athos@1540
   166
athos@1526
   167
    /// \brief Issue a new node map writing command for the writer.
deba@1137
   168
    ///
athos@1526
   169
   /// This function issues a new <i> node map writing command</i> to the writer.
deba@1137
   170
    template <typename Writer, typename Map>
deba@1394
   171
    GraphWriter& writeNodeMap(std::string name, const Map& map, 
deba@1421
   172
			      const Writer& writer = Writer()) {
deba@1421
   173
      nodeset_writer.writeNodeMap(name, map, writer);
deba@1137
   174
      return *this;
deba@1137
   175
    }
deba@1137
   176
deba@1137
   177
athos@1526
   178
    /// \brief Issue a new edge map writing command for the writer.
deba@1137
   179
    ///
athos@1526
   180
   /// This function issues a new <i> edge map writing command</i> to the writer.
deba@1137
   181
    template <typename Map>
deba@1394
   182
    GraphWriter& writeEdgeMap(std::string name, const Map& map) { 
deba@1421
   183
      edgeset_writer.writeEdgeMap(name, map);
deba@1409
   184
      return *this;
deba@1137
   185
    }
deba@1137
   186
deba@1137
   187
athos@1526
   188
    /// \brief Issue a new edge map writing command for the writer.
deba@1137
   189
    ///
athos@1526
   190
   /// This function issues a new <i> edge map writing command</i> to the writer.
deba@1137
   191
    template <typename Writer, typename Map>
deba@1409
   192
    GraphWriter& writeEdgeMap(std::string name, const Map& map,
deba@1421
   193
			      const Writer& writer = Writer()) {
deba@1421
   194
      edgeset_writer.writeEdgeMap(name, map, writer);
deba@1137
   195
      return *this;
deba@1137
   196
    }
deba@1137
   197
athos@1526
   198
    /// \brief Issue a new labeled node writing command to the writer.
deba@1137
   199
    ///
athos@1526
   200
    /// This function issues a new <i> labeled node writing command</i> 
athos@1526
   201
    /// to the writer.
deba@1394
   202
    GraphWriter& writeNode(std::string name, const Node& node) {
deba@1409
   203
      node_writer.writeNode(name, node);
deba@1137
   204
      return *this;
deba@1137
   205
    }
deba@1137
   206
athos@1526
   207
    /// \brief Issue a new labeled edge writing command to the writer.
deba@1137
   208
    ///
athos@1526
   209
    /// This function issues a new <i> labeled edge writing command</i> 
athos@1526
   210
    /// to the writer.
deba@1394
   211
    GraphWriter& writeEdge(std::string name, const Edge& edge) {
deba@1409
   212
      edge_writer.writeEdge(name, edge);
deba@1409
   213
    }
deba@1409
   214
athos@1526
   215
    /// \brief Issue a new attribute writing command.
deba@1409
   216
    ///
athos@1526
   217
    /// This function issues a new <i> attribute writing command</i> 
athos@1526
   218
    /// to the writer.
deba@1409
   219
    template <typename Value>
deba@1409
   220
    GraphWriter& writeAttribute(std::string name, const Value& value) {
deba@1409
   221
      attribute_writer.writeAttribute(name, value);
deba@1409
   222
      return *this;
deba@1409
   223
    }
deba@1409
   224
    
athos@1526
   225
    /// \brief Issue a new attribute writing command.
deba@1409
   226
    ///
athos@1526
   227
    /// This function issues a new <i> attribute writing command</i> 
athos@1526
   228
    /// to the writer.
deba@1409
   229
    template <typename Writer, typename Value>
deba@1409
   230
    GraphWriter& writeAttribute(std::string name, const Value& value, 
deba@1409
   231
			       const Writer& writer) {
deba@1409
   232
      attribute_writer.writeAttribute<Writer>(name, value, writer);
deba@1137
   233
      return *this;
deba@1137
   234
    }
deba@1137
   235
deba@1409
   236
    /// \brief Conversion operator to LemonWriter.
deba@1409
   237
    ///
athos@1526
   238
    /// Conversion operator to LemonWriter. It makes possible
deba@1409
   239
    /// to access the encapsulated \e LemonWriter, this way
deba@1409
   240
    /// you can attach to this writer new instances of 
athos@1540
   241
    /// \e LemonWriter::SectionWriter. For more details see
athos@1540
   242
    /// the \ref rwbackground "Background of Reading and Writing".
deba@1409
   243
    operator LemonWriter&() {
deba@1409
   244
      return *writer;
deba@1396
   245
    }
deba@1396
   246
athos@1526
   247
    /// \brief Executes the writing commands.
deba@1137
   248
    ///
athos@1526
   249
    /// Executes the writing commands.
deba@1409
   250
    void run() {
deba@1409
   251
      writer->run();
deba@1137
   252
    }
deba@1137
   253
deba@1901
   254
    /// \brief Write the label of the given node.
deba@1429
   255
    ///
deba@1901
   256
    /// It writes the label of the given node. If there was written an "label"
athos@1526
   257
    /// named node map then it will write the map value belonging to the node.
deba@1901
   258
    void writeLabel(std::ostream& os, const Node& item) const {
deba@1901
   259
      nodeset_writer.writeLabel(os, item);
deba@1429
   260
    } 
deba@1429
   261
deba@1901
   262
    /// \brief Write the label of the given edge.
deba@1429
   263
    ///
deba@1901
   264
    /// It writes the label of the given edge. If there was written an "label"
athos@1526
   265
    /// named edge map then it will write the map value belonging to the edge.
deba@1901
   266
    void writeLabel(std::ostream& os, const Edge& item) const {
deba@1901
   267
      edgeset_writer.writeLabel(os, item);
deba@1429
   268
    } 
deba@1429
   269
deba@1137
   270
  private:
deba@1137
   271
deba@1409
   272
    LemonWriter* writer;
deba@1409
   273
    bool own_writer;
deba@1137
   274
deba@1409
   275
    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
deba@1409
   276
    EdgeSetWriter<Graph, WriterTraits> edgeset_writer;
deba@1409
   277
deba@1409
   278
    NodeWriter<Graph> node_writer;
deba@1409
   279
    EdgeWriter<Graph> edge_writer;
deba@1409
   280
    
deba@1409
   281
    AttributeWriter<WriterTraits> attribute_writer;
deba@1137
   282
  };
deba@1137
   283
deba@1409
   284
deba@1744
   285
deba@1333
   286
  /// \brief Write a graph to the output.
deba@1333
   287
  ///
deba@1744
   288
  /// It is a helper function to write a graph to the given output
deba@1744
   289
  /// stream. It gives back a GraphWriter object and this object
deba@1744
   290
  /// can write more maps, labeled nodes and edges and attributes.
deba@1744
   291
  /// \warning Do not forget to call the \c run() function.
athos@1534
   292
  ///
deba@1333
   293
  /// \param os The output stream.
deba@1333
   294
  /// \param g The graph.
deba@1744
   295
  template <typename Graph>
deba@1744
   296
  GraphWriter<Graph> graphWriter(std::ostream& os, const Graph &g) {
deba@1744
   297
    return GraphWriter<Graph>(os, g);
deba@1208
   298
  }
deba@1208
   299
deba@1744
   300
  /// \brief Write a graph to the output.
deba@1333
   301
  ///
deba@1744
   302
  /// It is a helper function to write a graph to the given output
deba@1744
   303
  /// file. It gives back a GraphWriter object and this object
deba@1744
   304
  /// can write more maps, labeled nodes and edges and attributes.
deba@1744
   305
  /// \warning Do not forget to call the \c run() function.
athos@1534
   306
  ///
deba@1744
   307
  /// \param fn The filename.
deba@1333
   308
  /// \param g The graph.
deba@1744
   309
  template <typename Graph>
deba@1744
   310
  GraphWriter<Graph> graphWriter(const std::string& fn, const Graph &g) {
deba@1744
   311
    return GraphWriter<Graph>(fn, g);
deba@1208
   312
  }
deba@1208
   313
deba@1421
   314
  /// \brief The undirected graph writer class.
deba@1421
   315
  ///
klao@1909
   316
  /// The \c UGraphWriter class provides the ugraph output. To write 
athos@1526
   317
  /// a graph you should first give writing commands to the writer. You can 
klao@1909
   318
  /// declare write command as \c NodeMap, \c EdgeMap or \c UEdgeMap 
klao@1909
   319
  /// writing and labeled Node, Edge or UEdge writing.
deba@1421
   320
  ///
alpar@1946
   321
  ///\code
klao@1909
   322
  /// UGraphWriter<ListUGraph> writer(std::cout, graph);
alpar@1946
   323
  ///\endcode
deba@1421
   324
  ///
deba@1421
   325
  /// The \c writeNodeMap() function declares a \c NodeMap writing 
klao@1909
   326
  /// command in the \c UGraphWriter. You should give as parameter 
deba@1421
   327
  /// the name of the map and the map object. The NodeMap writing 
deba@1901
   328
  /// command with name "label" should write a unique map because it 
deba@1901
   329
  /// is regarded as label map.
deba@1421
   330
  ///
alpar@1946
   331
  ///\code
klao@1909
   332
  /// IdMap<ListUGraph, Node> nodeLabelMap;
deba@1901
   333
  /// writer.writeNodeMap("label", nodeLabelMap);
deba@1421
   334
  ///
deba@1421
   335
  /// writer.writeNodeMap("coords", coords);
deba@1421
   336
  /// writer.writeNodeMap("color", colorMap);
alpar@1946
   337
  ///\endcode
deba@1421
   338
  ///
klao@1909
   339
  /// With the \c writeUEdgeMap() member function you can give an 
deba@1421
   340
  /// undirected edge map writing command similar to the NodeMaps.
deba@1421
   341
  ///
alpar@1946
   342
  ///\code
deba@1421
   343
  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
deba@1421
   344
  ///   edgeDescMap(graph);
klao@1909
   345
  /// writer.writeUEdgeMap("descriptor", edgeDescMap);
deba@1421
   346
  ///
klao@1909
   347
  /// writer.writeUEdgeMap("weight", weightMap);
klao@1909
   348
  /// writer.writeUEdgeMap("label", labelMap);
alpar@1946
   349
  ///\endcode
deba@1421
   350
  /// 
deba@1421
   351
  /// The EdgeMap handling is just a syntactical sugar. It writes
deba@1421
   352
  /// two undirected edge map with '+' and '-' prefix in the name.
deba@1421
   353
  ///
alpar@1946
   354
  ///\code
deba@1421
   355
  /// writer.writeEdgeMap("capacity", capacityMap);
alpar@1946
   356
  ///\endcode
deba@1421
   357
  ///
deba@1421
   358
  ///
klao@1909
   359
  /// With \c writeNode() and \c writeUEdge() functions you can 
athos@1526
   360
  /// designate nodes and undirected edges in the graph. For example, you can 
deba@1421
   361
  /// write out the source and target of the graph.
deba@1421
   362
  ///
alpar@1946
   363
  ///\code
deba@1421
   364
  /// writer.writeNode("source", sourceNode);
deba@1421
   365
  /// writer.writeNode("target", targetNode);
deba@1421
   366
  ///
klao@1909
   367
  /// writer.writeUEdge("observed", uEdge);
alpar@1946
   368
  ///\endcode
deba@1421
   369
  ///
deba@1421
   370
  /// After you give all write commands you must call the \c run() member
athos@1526
   371
  /// function, which executes all the writing commands.
deba@1421
   372
  ///
alpar@1946
   373
  ///\code
deba@1421
   374
  /// writer.run();
alpar@1946
   375
  ///\endcode
deba@1421
   376
  ///
deba@1421
   377
  /// \see DefaultWriterTraits
deba@1421
   378
  /// \see QuotedStringWriter
deba@1421
   379
  /// \see IdMap
deba@1421
   380
  /// \see DescriptorMap
deba@1421
   381
  /// \see \ref GraphWriter
deba@1421
   382
  /// \see \ref graph-io-page
deba@1421
   383
  /// \author Balazs Dezso
deba@1421
   384
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
klao@1909
   385
  class UGraphWriter {
deba@1421
   386
  public:
deba@1421
   387
    
deba@1421
   388
    typedef _Graph Graph;
deba@1421
   389
    typedef typename Graph::Node Node;
deba@1421
   390
    typedef typename Graph::Edge Edge;
klao@1909
   391
    typedef typename Graph::UEdge UEdge;
deba@1421
   392
deba@1421
   393
    typedef _WriterTraits WriterTraits;
deba@1421
   394
klao@1909
   395
    /// \brief Construct a new UGraphWriter.
deba@1421
   396
    ///
klao@1909
   397
    /// Construct a new UGraphWriter. It writes the given graph
deba@1421
   398
    /// to the given stream.
klao@1909
   399
    UGraphWriter(std::ostream& _os, const Graph& _graph) 
deba@1421
   400
      : writer(new LemonWriter(_os)), own_writer(true), 
deba@1421
   401
	nodeset_writer(*writer, _graph, std::string()),
klao@1909
   402
	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   403
	node_writer(*writer, nodeset_writer, std::string()),
klao@1909
   404
	u_edge_writer(*writer, u_edgeset_writer, std::string()),
deba@1421
   405
	attribute_writer(*writer, std::string()) {}
deba@1421
   406
klao@1909
   407
    /// \brief Construct a new UGraphWriter.
deba@1421
   408
    ///
klao@1909
   409
    /// Construct a new UGraphWriter. It writes the given graph
deba@1421
   410
    /// to the given file.
klao@1909
   411
    UGraphWriter(const std::string& _filename, const Graph& _graph) 
deba@1421
   412
      : writer(new LemonWriter(_filename)), own_writer(true), 
deba@1421
   413
	nodeset_writer(*writer, _graph, std::string()),
klao@1909
   414
	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   415
	node_writer(*writer, nodeset_writer, std::string()),
klao@1909
   416
	u_edge_writer(*writer, u_edgeset_writer, std::string()),
deba@1421
   417
	attribute_writer(*writer, std::string()) {}
deba@1421
   418
klao@1909
   419
    /// \brief Construct a new UGraphWriter.
deba@1421
   420
    ///
klao@1909
   421
    /// Construct a new UGraphWriter. It writes the given graph
deba@1421
   422
    /// to given LemonReader.
klao@1909
   423
    UGraphWriter(LemonWriter& _writer, const Graph& _graph)
deba@1421
   424
      : writer(_writer), own_writer(false), 
deba@1421
   425
	nodeset_writer(*writer, _graph, std::string()),
klao@1909
   426
	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   427
	node_writer(*writer, nodeset_writer, std::string()),
klao@1909
   428
	u_edge_writer(*writer, u_edgeset_writer, std::string()),
deba@1421
   429
	attribute_writer(*writer, std::string()) {}
deba@1421
   430
deba@1421
   431
    /// \brief Destruct the graph writer.
deba@1421
   432
    ///
deba@1421
   433
    /// Destruct the graph writer.
klao@1909
   434
    ~UGraphWriter() {
deba@1421
   435
      if (own_writer) 
deba@1421
   436
	delete writer;
deba@1421
   437
    }
deba@1421
   438
athos@1526
   439
    /// \brief Issue a new node map writing command to the writer.
deba@1421
   440
    ///
athos@1526
   441
   /// This function issues a new <i> node map writing command</i> to the writer.
deba@1421
   442
    template <typename Map>
klao@1909
   443
    UGraphWriter& writeNodeMap(std::string name, const Map& map) {
deba@1421
   444
      nodeset_writer.writeNodeMap(name, map);
deba@1421
   445
      return *this;
deba@1421
   446
    }
deba@1421
   447
athos@1526
   448
    /// \brief Issue a new node map writing command to the writer.
deba@1421
   449
    ///
athos@1526
   450
   /// This function issues a new <i> node map writing command</i> to the writer.
deba@1421
   451
    template <typename Writer, typename Map>
klao@1909
   452
    UGraphWriter& writeNodeMap(std::string name, const Map& map, 
deba@1421
   453
			      const Writer& writer = Writer()) {
deba@1421
   454
      nodeset_writer.writeNodeMap(name, map, writer);
deba@1421
   455
      return *this;
deba@1421
   456
    }
deba@1421
   457
athos@1526
   458
    /// \brief Issue a new edge map writing command to the writer.
deba@1421
   459
    ///
athos@1526
   460
   /// This function issues a new <i> edge map writing command</i> to the writer.
deba@1421
   461
    template <typename Map>
klao@1909
   462
    UGraphWriter& writeEdgeMap(std::string name, const Map& map) { 
klao@1909
   463
      u_edgeset_writer.writeEdgeMap(name, map);
deba@1421
   464
      return *this;
deba@1421
   465
    }
deba@1421
   466
athos@1526
   467
    /// \brief Issue a new edge map writing command to the writer.
deba@1421
   468
    ///
athos@1526
   469
   /// This function issues a new <i> edge map writing command</i> to the writer.
deba@1421
   470
    template <typename Writer, typename Map>
klao@1909
   471
    UGraphWriter& writeEdgeMap(std::string name, const Map& map,
deba@1421
   472
				   const Writer& writer = Writer()) {
klao@1909
   473
      u_edgeset_writer.writeEdgeMap(name, map, writer);
deba@1421
   474
      return *this;
deba@1421
   475
    }
deba@1421
   476
athos@1526
   477
    /// \brief Issue a new undirected edge map writing command to the writer.
deba@1421
   478
    ///
athos@1526
   479
    /// This function issues a new <i> undirected edge map writing
athos@1526
   480
    /// command</i> to the writer.
deba@1421
   481
    template <typename Map>
klao@1909
   482
    UGraphWriter& writeUEdgeMap(std::string name, const Map& map) { 
klao@1909
   483
      u_edgeset_writer.writeUEdgeMap(name, map);
deba@1421
   484
      return *this;
deba@1421
   485
    }
deba@1421
   486
athos@1526
   487
    /// \brief Issue a new undirected edge map writing command to the writer.
deba@1421
   488
    ///
athos@1526
   489
    /// This function issues a new <i> undirected edge map writing
athos@1526
   490
    /// command</i> to the writer.
athos@1540
   491
   template <typename Writer, typename Map>
klao@1909
   492
    UGraphWriter& writeUEdgeMap(std::string name, const Map& map,
deba@1421
   493
					const Writer& writer = Writer()) {
klao@1909
   494
      u_edgeset_writer.writeUEdgeMap(name, map, writer);
deba@1421
   495
      return *this;
deba@1421
   496
    }
deba@1421
   497
athos@1526
   498
    /// \brief Issue a new labeled node writer to the writer.
deba@1421
   499
    ///
athos@1526
   500
    /// This function issues a new <i> labeled node writing
athos@1526
   501
    /// command</i> to the writer.
klao@1909
   502
    UGraphWriter& writeNode(std::string name, const Node& node) {
deba@1421
   503
      node_writer.writeNode(name, node);
deba@1421
   504
      return *this;
deba@1421
   505
    }
deba@1421
   506
athos@1526
   507
    /// \brief Issue a new labeled edge writer to the writer.
deba@1421
   508
    ///
athos@1526
   509
    /// This function issues a new <i> labeled edge writing
athos@1526
   510
    /// command</i> to the writer.
klao@1909
   511
    UGraphWriter& writeEdge(std::string name, const Edge& edge) {
klao@1909
   512
      u_edge_writer.writeEdge(name, edge);
deba@1429
   513
    }
deba@1429
   514
athos@1526
   515
    /// \brief Issue a new labeled undirected edge writing command to
athos@1526
   516
    /// the writer.
deba@1429
   517
    ///
athos@1526
   518
    /// Issue a new <i>labeled undirected edge writing command</i> to
athos@1526
   519
    /// the writer.
klao@1909
   520
    UGraphWriter& writeUEdge(std::string name, const UEdge& edge) {
klao@1909
   521
      u_edge_writer.writeUEdge(name, edge);
deba@1421
   522
    }
deba@1421
   523
athos@1526
   524
    /// \brief Issue a new attribute writing command.
deba@1421
   525
    ///
athos@1526
   526
    /// This function issues a new <i> attribute writing
athos@1526
   527
    /// command</i> to the writer.
deba@1421
   528
    template <typename Value>
klao@1909
   529
    UGraphWriter& writeAttribute(std::string name, const Value& value) {
deba@1421
   530
      attribute_writer.writeAttribute(name, value);
deba@1421
   531
      return *this;
deba@1421
   532
    }
deba@1421
   533
    
athos@1526
   534
    /// \brief Issue a new attribute writing command.
deba@1421
   535
    ///
athos@1526
   536
    /// This function issues a new <i> attribute writing
athos@1526
   537
    /// command</i> to the writer.
deba@1421
   538
    template <typename Writer, typename Value>
klao@1909
   539
    UGraphWriter& writeAttribute(std::string name, const Value& value, 
deba@1421
   540
			       const Writer& writer) {
deba@1421
   541
      attribute_writer.writeAttribute<Writer>(name, value, writer);
deba@1421
   542
      return *this;
deba@1421
   543
    }
deba@1421
   544
deba@1421
   545
    /// \brief Conversion operator to LemonWriter.
deba@1421
   546
    ///
athos@1526
   547
    /// Conversion operator to LemonWriter. It makes possible
deba@1421
   548
    /// to access the encapsulated \e LemonWriter, this way
deba@1421
   549
    /// you can attach to this writer new instances of 
deba@1421
   550
    /// \e LemonWriter::SectionWriter.
deba@1421
   551
    operator LemonWriter&() {
deba@1421
   552
      return *writer;
deba@1421
   553
    }
deba@1421
   554
athos@1526
   555
    /// \brief Executes the writing commands.
deba@1421
   556
    ///
athos@1526
   557
    /// Executes the writing commands.
deba@1421
   558
    void run() {
deba@1421
   559
      writer->run();
deba@1421
   560
    }
deba@1421
   561
deba@1901
   562
    /// \brief Write the label of the given node.
deba@1429
   563
    ///
deba@1901
   564
    /// It writes the label of the given node. If there was written an "label"
athos@1526
   565
    /// named node map then it will write the map value belonging to the node.
deba@1901
   566
    void writeLabel(std::ostream& os, const Node& item) const {
deba@1901
   567
      nodeset_writer.writeLabel(os, item);
deba@1429
   568
    } 
deba@1429
   569
deba@1901
   570
    /// \brief Write the label of the given edge.
deba@1429
   571
    ///
deba@1901
   572
    /// It writes the label of the given edge. If there was written an "label"
athos@1526
   573
    /// named edge map then it will write the map value belonging to the edge.
deba@1901
   574
    void writeLabel(std::ostream& os, const Edge& item) const {
klao@1909
   575
      u_edgeset_writer.writeLabel(os, item);
deba@1429
   576
    } 
deba@1429
   577
deba@1901
   578
    /// \brief Write the label of the given undirected edge.
deba@1429
   579
    ///
deba@1901
   580
    /// It writes the label of the given undirected edge. If there was written 
deba@1901
   581
    /// an "label" named edge map then it will write the map value belonging to 
deba@1429
   582
    /// the edge.
klao@1909
   583
    void writeLabel(std::ostream& os, const UEdge& item) const {
klao@1909
   584
      u_edgeset_writer.writeLabel(os, item);
deba@1429
   585
    } 
deba@1429
   586
deba@1429
   587
deba@1421
   588
  private:
deba@1421
   589
deba@1421
   590
    LemonWriter* writer;
deba@1421
   591
    bool own_writer;
deba@1421
   592
deba@1421
   593
    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
klao@1909
   594
    UEdgeSetWriter<Graph, WriterTraits> u_edgeset_writer;
deba@1421
   595
deba@1421
   596
    NodeWriter<Graph> node_writer;
klao@1909
   597
    UEdgeWriter<Graph> u_edge_writer;
deba@1421
   598
    
deba@1421
   599
    AttributeWriter<WriterTraits> attribute_writer;
deba@1421
   600
  };
deba@1421
   601
athos@1534
   602
  /// \brief Write an undirected graph to the output.
athos@1534
   603
  ///
deba@1744
   604
  /// It is a helper function to write an undirected graph to the given output
klao@1909
   605
  /// stream. It gives back an UGraphWriter object and this object
deba@1744
   606
  /// can write more maps, labeled nodes and edges and attributes.
deba@1744
   607
  /// \warning Do not forget to call the \c run() function.
deba@1744
   608
  ///
athos@1534
   609
  /// \param os The output stream.
athos@1534
   610
  /// \param g The graph.
deba@1744
   611
  template <typename Graph>
klao@1909
   612
  UGraphWriter<Graph> uGraphWriter(std::ostream& os, const Graph &g) {
klao@1909
   613
    return UGraphWriter<Graph>(os, g);
athos@1534
   614
  }
deba@1421
   615
deba@1744
   616
  /// \brief Write an undirected graph to the output.
deba@1421
   617
  ///
deba@1744
   618
  /// It is a helper function to write an undirected graph to the given output
klao@1909
   619
  /// file. It gives back an UGraphWriter object and this object
deba@1744
   620
  /// can write more maps, labeled nodes, edges, undirected edges and 
deba@1744
   621
  /// attributes.
deba@1744
   622
  ///
deba@1744
   623
  /// \warning Do not forget to call the \c run() function.
deba@1744
   624
  ///
deba@1744
   625
  /// \param fn The output file.
deba@1421
   626
  /// \param g The graph.
deba@1744
   627
  template <typename Graph>
klao@1909
   628
  UGraphWriter<Graph> uGraphWriter(const std::string& fn, 
deba@1744
   629
					   const Graph &g) {
klao@1909
   630
    return UGraphWriter<Graph>(fn, g);
deba@1421
   631
  }
deba@1421
   632
deba@1333
   633
  /// @}
deba@1137
   634
deba@1137
   635
}
deba@1214
   636
deba@1214
   637
#endif