lemon/graph_writer.h
changeset 1913 49fe71fce7fb
parent 1901 723b2b81d900
child 1946 17eb3eaad9f8
equal deleted inserted replaced
6:2e2fe4aa6fb4 7:a9c4cee68f43
   313     return GraphWriter<Graph>(fn, g);
   313     return GraphWriter<Graph>(fn, g);
   314   }
   314   }
   315 
   315 
   316   /// \brief The undirected graph writer class.
   316   /// \brief The undirected graph writer class.
   317   ///
   317   ///
   318   /// The \c UndirGraphWriter class provides the undir graph output. To write 
   318   /// The \c UGraphWriter class provides the ugraph output. To write 
   319   /// a graph you should first give writing commands to the writer. You can 
   319   /// a graph you should first give writing commands to the writer. You can 
   320   /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap 
   320   /// declare write command as \c NodeMap, \c EdgeMap or \c UEdgeMap 
   321   /// writing and labeled Node, Edge or UndirEdge writing.
   321   /// writing and labeled Node, Edge or UEdge writing.
   322   ///
   322   ///
   323   /// \code
   323   /// \code
   324   /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph);
   324   /// UGraphWriter<ListUGraph> writer(std::cout, graph);
   325   /// \endcode
   325   /// \endcode
   326   ///
   326   ///
   327   /// The \c writeNodeMap() function declares a \c NodeMap writing 
   327   /// The \c writeNodeMap() function declares a \c NodeMap writing 
   328   /// command in the \c UndirGraphWriter. You should give as parameter 
   328   /// command in the \c UGraphWriter. You should give as parameter 
   329   /// the name of the map and the map object. The NodeMap writing 
   329   /// the name of the map and the map object. The NodeMap writing 
   330   /// command with name "label" should write a unique map because it 
   330   /// command with name "label" should write a unique map because it 
   331   /// is regarded as label map.
   331   /// is regarded as label map.
   332   ///
   332   ///
   333   /// \code
   333   /// \code
   334   /// IdMap<UndirListGraph, Node> nodeLabelMap;
   334   /// IdMap<ListUGraph, Node> nodeLabelMap;
   335   /// writer.writeNodeMap("label", nodeLabelMap);
   335   /// writer.writeNodeMap("label", nodeLabelMap);
   336   ///
   336   ///
   337   /// writer.writeNodeMap("coords", coords);
   337   /// writer.writeNodeMap("coords", coords);
   338   /// writer.writeNodeMap("color", colorMap);
   338   /// writer.writeNodeMap("color", colorMap);
   339   /// \endcode
   339   /// \endcode
   340   ///
   340   ///
   341   /// With the \c writeUndirEdgeMap() member function you can give an 
   341   /// With the \c writeUEdgeMap() member function you can give an 
   342   /// undirected edge map writing command similar to the NodeMaps.
   342   /// undirected edge map writing command similar to the NodeMaps.
   343   ///
   343   ///
   344   /// \code
   344   /// \code
   345   /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
   345   /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
   346   ///   edgeDescMap(graph);
   346   ///   edgeDescMap(graph);
   347   /// writer.writeUndirEdgeMap("descriptor", edgeDescMap);
   347   /// writer.writeUEdgeMap("descriptor", edgeDescMap);
   348   ///
   348   ///
   349   /// writer.writeUndirEdgeMap("weight", weightMap);
   349   /// writer.writeUEdgeMap("weight", weightMap);
   350   /// writer.writeUndirEdgeMap("label", labelMap);
   350   /// writer.writeUEdgeMap("label", labelMap);
   351   /// \endcode
   351   /// \endcode
   352   /// 
   352   /// 
   353   /// The EdgeMap handling is just a syntactical sugar. It writes
   353   /// The EdgeMap handling is just a syntactical sugar. It writes
   354   /// two undirected edge map with '+' and '-' prefix in the name.
   354   /// two undirected edge map with '+' and '-' prefix in the name.
   355   ///
   355   ///
   356   /// \code
   356   /// \code
   357   /// writer.writeEdgeMap("capacity", capacityMap);
   357   /// writer.writeEdgeMap("capacity", capacityMap);
   358   /// \endcode
   358   /// \endcode
   359   ///
   359   ///
   360   ///
   360   ///
   361   /// With \c writeNode() and \c writeUndirEdge() functions you can 
   361   /// With \c writeNode() and \c writeUEdge() functions you can 
   362   /// designate nodes and undirected edges in the graph. For example, you can 
   362   /// designate nodes and undirected edges in the graph. For example, you can 
   363   /// write out the source and target of the graph.
   363   /// write out the source and target of the graph.
   364   ///
   364   ///
   365   /// \code
   365   /// \code
   366   /// writer.writeNode("source", sourceNode);
   366   /// writer.writeNode("source", sourceNode);
   367   /// writer.writeNode("target", targetNode);
   367   /// writer.writeNode("target", targetNode);
   368   ///
   368   ///
   369   /// writer.writeUndirEdge("observed", undirEdge);
   369   /// writer.writeUEdge("observed", uEdge);
   370   /// \endcode
   370   /// \endcode
   371   ///
   371   ///
   372   /// After you give all write commands you must call the \c run() member
   372   /// After you give all write commands you must call the \c run() member
   373   /// function, which executes all the writing commands.
   373   /// function, which executes all the writing commands.
   374   ///
   374   ///
   382   /// \see DescriptorMap
   382   /// \see DescriptorMap
   383   /// \see \ref GraphWriter
   383   /// \see \ref GraphWriter
   384   /// \see \ref graph-io-page
   384   /// \see \ref graph-io-page
   385   /// \author Balazs Dezso
   385   /// \author Balazs Dezso
   386   template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
   386   template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
   387   class UndirGraphWriter {
   387   class UGraphWriter {
   388   public:
   388   public:
   389     
   389     
   390     typedef _Graph Graph;
   390     typedef _Graph Graph;
   391     typedef typename Graph::Node Node;
   391     typedef typename Graph::Node Node;
   392     typedef typename Graph::Edge Edge;
   392     typedef typename Graph::Edge Edge;
   393     typedef typename Graph::UndirEdge UndirEdge;
   393     typedef typename Graph::UEdge UEdge;
   394 
   394 
   395     typedef _WriterTraits WriterTraits;
   395     typedef _WriterTraits WriterTraits;
   396 
   396 
   397     /// \brief Construct a new UndirGraphWriter.
   397     /// \brief Construct a new UGraphWriter.
   398     ///
   398     ///
   399     /// Construct a new UndirGraphWriter. It writes the given graph
   399     /// Construct a new UGraphWriter. It writes the given graph
   400     /// to the given stream.
   400     /// to the given stream.
   401     UndirGraphWriter(std::ostream& _os, const Graph& _graph) 
   401     UGraphWriter(std::ostream& _os, const Graph& _graph) 
   402       : writer(new LemonWriter(_os)), own_writer(true), 
   402       : writer(new LemonWriter(_os)), own_writer(true), 
   403 	nodeset_writer(*writer, _graph, std::string()),
   403 	nodeset_writer(*writer, _graph, std::string()),
   404 	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   404 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   405 	node_writer(*writer, nodeset_writer, std::string()),
   405 	node_writer(*writer, nodeset_writer, std::string()),
   406 	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
   406 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   407 	attribute_writer(*writer, std::string()) {}
   407 	attribute_writer(*writer, std::string()) {}
   408 
   408 
   409     /// \brief Construct a new UndirGraphWriter.
   409     /// \brief Construct a new UGraphWriter.
   410     ///
   410     ///
   411     /// Construct a new UndirGraphWriter. It writes the given graph
   411     /// Construct a new UGraphWriter. It writes the given graph
   412     /// to the given file.
   412     /// to the given file.
   413     UndirGraphWriter(const std::string& _filename, const Graph& _graph) 
   413     UGraphWriter(const std::string& _filename, const Graph& _graph) 
   414       : writer(new LemonWriter(_filename)), own_writer(true), 
   414       : writer(new LemonWriter(_filename)), own_writer(true), 
   415 	nodeset_writer(*writer, _graph, std::string()),
   415 	nodeset_writer(*writer, _graph, std::string()),
   416 	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   416 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   417 	node_writer(*writer, nodeset_writer, std::string()),
   417 	node_writer(*writer, nodeset_writer, std::string()),
   418 	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
   418 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   419 	attribute_writer(*writer, std::string()) {}
   419 	attribute_writer(*writer, std::string()) {}
   420 
   420 
   421     /// \brief Construct a new UndirGraphWriter.
   421     /// \brief Construct a new UGraphWriter.
   422     ///
   422     ///
   423     /// Construct a new UndirGraphWriter. It writes the given graph
   423     /// Construct a new UGraphWriter. It writes the given graph
   424     /// to given LemonReader.
   424     /// to given LemonReader.
   425     UndirGraphWriter(LemonWriter& _writer, const Graph& _graph)
   425     UGraphWriter(LemonWriter& _writer, const Graph& _graph)
   426       : writer(_writer), own_writer(false), 
   426       : writer(_writer), own_writer(false), 
   427 	nodeset_writer(*writer, _graph, std::string()),
   427 	nodeset_writer(*writer, _graph, std::string()),
   428 	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   428 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   429 	node_writer(*writer, nodeset_writer, std::string()),
   429 	node_writer(*writer, nodeset_writer, std::string()),
   430 	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
   430 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   431 	attribute_writer(*writer, std::string()) {}
   431 	attribute_writer(*writer, std::string()) {}
   432 
   432 
   433     /// \brief Destruct the graph writer.
   433     /// \brief Destruct the graph writer.
   434     ///
   434     ///
   435     /// Destruct the graph writer.
   435     /// Destruct the graph writer.
   436     ~UndirGraphWriter() {
   436     ~UGraphWriter() {
   437       if (own_writer) 
   437       if (own_writer) 
   438 	delete writer;
   438 	delete writer;
   439     }
   439     }
   440 
   440 
   441     /// \brief Issue a new node map writing command to the writer.
   441     /// \brief Issue a new node map writing command to the writer.
   442     ///
   442     ///
   443    /// This function issues a new <i> node map writing command</i> to the writer.
   443    /// This function issues a new <i> node map writing command</i> to the writer.
   444     template <typename Map>
   444     template <typename Map>
   445     UndirGraphWriter& writeNodeMap(std::string name, const Map& map) {
   445     UGraphWriter& writeNodeMap(std::string name, const Map& map) {
   446       nodeset_writer.writeNodeMap(name, map);
   446       nodeset_writer.writeNodeMap(name, map);
   447       return *this;
   447       return *this;
   448     }
   448     }
   449 
   449 
   450     /// \brief Issue a new node map writing command to the writer.
   450     /// \brief Issue a new node map writing command to the writer.
   451     ///
   451     ///
   452    /// This function issues a new <i> node map writing command</i> to the writer.
   452    /// This function issues a new <i> node map writing command</i> to the writer.
   453     template <typename Writer, typename Map>
   453     template <typename Writer, typename Map>
   454     UndirGraphWriter& writeNodeMap(std::string name, const Map& map, 
   454     UGraphWriter& writeNodeMap(std::string name, const Map& map, 
   455 			      const Writer& writer = Writer()) {
   455 			      const Writer& writer = Writer()) {
   456       nodeset_writer.writeNodeMap(name, map, writer);
   456       nodeset_writer.writeNodeMap(name, map, writer);
   457       return *this;
   457       return *this;
   458     }
   458     }
   459 
   459 
   460     /// \brief Issue a new edge map writing command to the writer.
   460     /// \brief Issue a new edge map writing command to the writer.
   461     ///
   461     ///
   462    /// This function issues a new <i> edge map writing command</i> to the writer.
   462    /// This function issues a new <i> edge map writing command</i> to the writer.
   463     template <typename Map>
   463     template <typename Map>
   464     UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) { 
   464     UGraphWriter& writeEdgeMap(std::string name, const Map& map) { 
   465       undir_edgeset_writer.writeEdgeMap(name, map);
   465       u_edgeset_writer.writeEdgeMap(name, map);
   466       return *this;
   466       return *this;
   467     }
   467     }
   468 
   468 
   469     /// \brief Issue a new edge map writing command to the writer.
   469     /// \brief Issue a new edge map writing command to the writer.
   470     ///
   470     ///
   471    /// This function issues a new <i> edge map writing command</i> to the writer.
   471    /// This function issues a new <i> edge map writing command</i> to the writer.
   472     template <typename Writer, typename Map>
   472     template <typename Writer, typename Map>
   473     UndirGraphWriter& writeEdgeMap(std::string name, const Map& map,
   473     UGraphWriter& writeEdgeMap(std::string name, const Map& map,
   474 				   const Writer& writer = Writer()) {
   474 				   const Writer& writer = Writer()) {
   475       undir_edgeset_writer.writeEdgeMap(name, map, writer);
   475       u_edgeset_writer.writeEdgeMap(name, map, writer);
   476       return *this;
   476       return *this;
   477     }
   477     }
   478 
   478 
   479     /// \brief Issue a new undirected edge map writing command to the writer.
   479     /// \brief Issue a new undirected edge map writing command to the writer.
   480     ///
   480     ///
   481     /// This function issues a new <i> undirected edge map writing
   481     /// This function issues a new <i> undirected edge map writing
   482     /// command</i> to the writer.
   482     /// command</i> to the writer.
   483     template <typename Map>
   483     template <typename Map>
   484     UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) { 
   484     UGraphWriter& writeUEdgeMap(std::string name, const Map& map) { 
   485       undir_edgeset_writer.writeUndirEdgeMap(name, map);
   485       u_edgeset_writer.writeUEdgeMap(name, map);
   486       return *this;
   486       return *this;
   487     }
   487     }
   488 
   488 
   489     /// \brief Issue a new undirected edge map writing command to the writer.
   489     /// \brief Issue a new undirected edge map writing command to the writer.
   490     ///
   490     ///
   491     /// This function issues a new <i> undirected edge map writing
   491     /// This function issues a new <i> undirected edge map writing
   492     /// command</i> to the writer.
   492     /// command</i> to the writer.
   493    template <typename Writer, typename Map>
   493    template <typename Writer, typename Map>
   494     UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map,
   494     UGraphWriter& writeUEdgeMap(std::string name, const Map& map,
   495 					const Writer& writer = Writer()) {
   495 					const Writer& writer = Writer()) {
   496       undir_edgeset_writer.writeUndirEdgeMap(name, map, writer);
   496       u_edgeset_writer.writeUEdgeMap(name, map, writer);
   497       return *this;
   497       return *this;
   498     }
   498     }
   499 
   499 
   500     /// \brief Issue a new labeled node writer to the writer.
   500     /// \brief Issue a new labeled node writer to the writer.
   501     ///
   501     ///
   502     /// This function issues a new <i> labeled node writing
   502     /// This function issues a new <i> labeled node writing
   503     /// command</i> to the writer.
   503     /// command</i> to the writer.
   504     UndirGraphWriter& writeNode(std::string name, const Node& node) {
   504     UGraphWriter& writeNode(std::string name, const Node& node) {
   505       node_writer.writeNode(name, node);
   505       node_writer.writeNode(name, node);
   506       return *this;
   506       return *this;
   507     }
   507     }
   508 
   508 
   509     /// \brief Issue a new labeled edge writer to the writer.
   509     /// \brief Issue a new labeled edge writer to the writer.
   510     ///
   510     ///
   511     /// This function issues a new <i> labeled edge writing
   511     /// This function issues a new <i> labeled edge writing
   512     /// command</i> to the writer.
   512     /// command</i> to the writer.
   513     UndirGraphWriter& writeEdge(std::string name, const Edge& edge) {
   513     UGraphWriter& writeEdge(std::string name, const Edge& edge) {
   514       undir_edge_writer.writeEdge(name, edge);
   514       u_edge_writer.writeEdge(name, edge);
   515     }
   515     }
   516 
   516 
   517     /// \brief Issue a new labeled undirected edge writing command to
   517     /// \brief Issue a new labeled undirected edge writing command to
   518     /// the writer.
   518     /// the writer.
   519     ///
   519     ///
   520     /// Issue a new <i>labeled undirected edge writing command</i> to
   520     /// Issue a new <i>labeled undirected edge writing command</i> to
   521     /// the writer.
   521     /// the writer.
   522     UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) {
   522     UGraphWriter& writeUEdge(std::string name, const UEdge& edge) {
   523       undir_edge_writer.writeUndirEdge(name, edge);
   523       u_edge_writer.writeUEdge(name, edge);
   524     }
   524     }
   525 
   525 
   526     /// \brief Issue a new attribute writing command.
   526     /// \brief Issue a new attribute writing command.
   527     ///
   527     ///
   528     /// This function issues a new <i> attribute writing
   528     /// This function issues a new <i> attribute writing
   529     /// command</i> to the writer.
   529     /// command</i> to the writer.
   530     template <typename Value>
   530     template <typename Value>
   531     UndirGraphWriter& writeAttribute(std::string name, const Value& value) {
   531     UGraphWriter& writeAttribute(std::string name, const Value& value) {
   532       attribute_writer.writeAttribute(name, value);
   532       attribute_writer.writeAttribute(name, value);
   533       return *this;
   533       return *this;
   534     }
   534     }
   535     
   535     
   536     /// \brief Issue a new attribute writing command.
   536     /// \brief Issue a new attribute writing command.
   537     ///
   537     ///
   538     /// This function issues a new <i> attribute writing
   538     /// This function issues a new <i> attribute writing
   539     /// command</i> to the writer.
   539     /// command</i> to the writer.
   540     template <typename Writer, typename Value>
   540     template <typename Writer, typename Value>
   541     UndirGraphWriter& writeAttribute(std::string name, const Value& value, 
   541     UGraphWriter& writeAttribute(std::string name, const Value& value, 
   542 			       const Writer& writer) {
   542 			       const Writer& writer) {
   543       attribute_writer.writeAttribute<Writer>(name, value, writer);
   543       attribute_writer.writeAttribute<Writer>(name, value, writer);
   544       return *this;
   544       return *this;
   545     }
   545     }
   546 
   546 
   572     /// \brief Write the label of the given edge.
   572     /// \brief Write the label of the given edge.
   573     ///
   573     ///
   574     /// It writes the label of the given edge. If there was written an "label"
   574     /// It writes the label of the given edge. If there was written an "label"
   575     /// named edge map then it will write the map value belonging to the edge.
   575     /// named edge map then it will write the map value belonging to the edge.
   576     void writeLabel(std::ostream& os, const Edge& item) const {
   576     void writeLabel(std::ostream& os, const Edge& item) const {
   577       undir_edgeset_writer.writeLabel(os, item);
   577       u_edgeset_writer.writeLabel(os, item);
   578     } 
   578     } 
   579 
   579 
   580     /// \brief Write the label of the given undirected edge.
   580     /// \brief Write the label of the given undirected edge.
   581     ///
   581     ///
   582     /// It writes the label of the given undirected edge. If there was written 
   582     /// It writes the label of the given undirected edge. If there was written 
   583     /// an "label" named edge map then it will write the map value belonging to 
   583     /// an "label" named edge map then it will write the map value belonging to 
   584     /// the edge.
   584     /// the edge.
   585     void writeLabel(std::ostream& os, const UndirEdge& item) const {
   585     void writeLabel(std::ostream& os, const UEdge& item) const {
   586       undir_edgeset_writer.writeLabel(os, item);
   586       u_edgeset_writer.writeLabel(os, item);
   587     } 
   587     } 
   588 
   588 
   589 
   589 
   590   private:
   590   private:
   591 
   591 
   592     LemonWriter* writer;
   592     LemonWriter* writer;
   593     bool own_writer;
   593     bool own_writer;
   594 
   594 
   595     NodeSetWriter<Graph, WriterTraits> nodeset_writer;
   595     NodeSetWriter<Graph, WriterTraits> nodeset_writer;
   596     UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer;
   596     UEdgeSetWriter<Graph, WriterTraits> u_edgeset_writer;
   597 
   597 
   598     NodeWriter<Graph> node_writer;
   598     NodeWriter<Graph> node_writer;
   599     UndirEdgeWriter<Graph> undir_edge_writer;
   599     UEdgeWriter<Graph> u_edge_writer;
   600     
   600     
   601     AttributeWriter<WriterTraits> attribute_writer;
   601     AttributeWriter<WriterTraits> attribute_writer;
   602   };
   602   };
   603 
   603 
   604   /// \brief Write an undirected graph to the output.
   604   /// \brief Write an undirected graph to the output.
   605   ///
   605   ///
   606   /// It is a helper function to write an undirected graph to the given output
   606   /// It is a helper function to write an undirected graph to the given output
   607   /// stream. It gives back an UndirGraphWriter object and this object
   607   /// stream. It gives back an UGraphWriter object and this object
   608   /// can write more maps, labeled nodes and edges and attributes.
   608   /// can write more maps, labeled nodes and edges and attributes.
   609   /// \warning Do not forget to call the \c run() function.
   609   /// \warning Do not forget to call the \c run() function.
   610   ///
   610   ///
   611   /// \param os The output stream.
   611   /// \param os The output stream.
   612   /// \param g The graph.
   612   /// \param g The graph.
   613   template <typename Graph>
   613   template <typename Graph>
   614   UndirGraphWriter<Graph> undirGraphWriter(std::ostream& os, const Graph &g) {
   614   UGraphWriter<Graph> uGraphWriter(std::ostream& os, const Graph &g) {
   615     return UndirGraphWriter<Graph>(os, g);
   615     return UGraphWriter<Graph>(os, g);
   616   }
   616   }
   617 
   617 
   618   /// \brief Write an undirected graph to the output.
   618   /// \brief Write an undirected graph to the output.
   619   ///
   619   ///
   620   /// It is a helper function to write an undirected graph to the given output
   620   /// It is a helper function to write an undirected graph to the given output
   621   /// file. It gives back an UndirGraphWriter object and this object
   621   /// file. It gives back an UGraphWriter object and this object
   622   /// can write more maps, labeled nodes, edges, undirected edges and 
   622   /// can write more maps, labeled nodes, edges, undirected edges and 
   623   /// attributes.
   623   /// attributes.
   624   ///
   624   ///
   625   /// \warning Do not forget to call the \c run() function.
   625   /// \warning Do not forget to call the \c run() function.
   626   ///
   626   ///
   627   /// \param fn The output file.
   627   /// \param fn The output file.
   628   /// \param g The graph.
   628   /// \param g The graph.
   629   template <typename Graph>
   629   template <typename Graph>
   630   UndirGraphWriter<Graph> undirGraphWriter(const std::string& fn, 
   630   UGraphWriter<Graph> uGraphWriter(const std::string& fn, 
   631 					   const Graph &g) {
   631 					   const Graph &g) {
   632     return UndirGraphWriter<Graph>(fn, g);
   632     return UGraphWriter<Graph>(fn, g);
   633   }
   633   }
   634 
   634 
   635   /// @}
   635   /// @}
   636 
   636 
   637 }
   637 }