COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/graph_writer.h @ 1526:8c14aa8f27a2

Last change on this file since 1526:8c14aa8f27a2 was 1526:8c14aa8f27a2, checked in by athos, 15 years ago

Mainly doc review.

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