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