lemon/graph_writer.h
changeset 2483 bf6d7b624d5c
parent 2391 14a343be7a5a
child 2502 9c23c3762bc5
equal deleted inserted replaced
14:179dafd1a3a3 15:3760315da2a1
   249     /// Executes the writing commands.
   249     /// Executes the writing commands.
   250     void run() {
   250     void run() {
   251       writer->run();
   251       writer->run();
   252     }
   252     }
   253 
   253 
       
   254     /// \brief Returns true if the writer can give back the labels by the items.
       
   255     ///
       
   256     /// Returns true if the writer can give back the the labels by the items.
       
   257     bool isLabelWriter() const {
       
   258       return nodeset_writer.isLabelWriter() && 
       
   259         edgeset_writer.isLabelWriter();
       
   260     }
       
   261 
   254     /// \brief Write the label of the given node.
   262     /// \brief Write the label of the given node.
   255     ///
   263     ///
   256     /// It writes the label of the given node. If there was written an "label"
   264     /// It writes the label of the given node. If there was written a "label"
   257     /// named node map then it will write the map value belonging to the node.
   265     /// named node map then it will write the map value belonging to the node.
   258     void writeLabel(std::ostream& os, const Node& item) const {
   266     void writeLabel(std::ostream& os, const Node& item) const {
   259       nodeset_writer.writeLabel(os, item);
   267       nodeset_writer.writeLabel(os, item);
   260     } 
   268     } 
   261 
   269 
   262     /// \brief Write the label of the given edge.
   270     /// \brief Write the label of the given edge.
   263     ///
   271     ///
   264     /// It writes the label of the given edge. If there was written an "label"
   272     /// It writes the label of the given edge. If there was written a "label"
   265     /// named edge map then it will write the map value belonging to the edge.
   273     /// named edge map then it will write the map value belonging to the edge.
   266     void writeLabel(std::ostream& os, const Edge& item) const {
   274     void writeLabel(std::ostream& os, const Edge& item) const {
   267       edgeset_writer.writeLabel(os, item);
   275       edgeset_writer.writeLabel(os, item);
   268     } 
   276     } 
       
   277 
       
   278     /// \brief Sorts the given node vector by label.
       
   279     ///
       
   280     /// Sorts the given node vector by label. If there was written an
       
   281     /// "label" named map then the vector will be sorted by the values
       
   282     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   283     /// it will be sorted by its id in the graph.
       
   284     void sortByLabel(std::vector<Node>& nodes) const {
       
   285       nodeset_writer.sortByLabel(nodes);
       
   286     }
       
   287 
       
   288     /// \brief Sorts the given edge vector by label.
       
   289     ///
       
   290     /// Sorts the given edge vector by label. If there was written an
       
   291     /// "label" named map then the vector will be sorted by the values
       
   292     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   293     /// it will be sorted by its id in the graph.
       
   294     void sortByLabel(std::vector<Edge>& edges) const {
       
   295       edgeset_writer.sortByLabel(edges);
       
   296     }
   269 
   297 
   270   private:
   298   private:
   271 
   299 
   272     LemonWriter* writer;
   300     LemonWriter* writer;
   273     bool own_writer;
   301     bool own_writer;
   368     /// Construct a new UGraphWriter. It writes the given graph
   396     /// Construct a new UGraphWriter. It writes the given graph
   369     /// to the given stream.
   397     /// to the given stream.
   370     UGraphWriter(std::ostream& _os, const Graph& _graph) 
   398     UGraphWriter(std::ostream& _os, const Graph& _graph) 
   371       : writer(new LemonWriter(_os)), own_writer(true), 
   399       : writer(new LemonWriter(_os)), own_writer(true), 
   372 	nodeset_writer(*writer, _graph, std::string()),
   400 	nodeset_writer(*writer, _graph, std::string()),
   373 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   401 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   374 	node_writer(*writer, nodeset_writer, std::string()),
   402 	node_writer(*writer, nodeset_writer, std::string()),
   375 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   403 	uedge_writer(*writer, uedgeset_writer, std::string()),
   376 	attribute_writer(*writer, std::string()) {}
   404 	attribute_writer(*writer, std::string()) {}
   377 
   405 
   378     /// \brief Construct a new UGraphWriter.
   406     /// \brief Construct a new UGraphWriter.
   379     ///
   407     ///
   380     /// Construct a new UGraphWriter. It writes the given graph
   408     /// Construct a new UGraphWriter. It writes the given graph
   381     /// to the given file.
   409     /// to the given file.
   382     UGraphWriter(const std::string& _filename, const Graph& _graph) 
   410     UGraphWriter(const std::string& _filename, const Graph& _graph) 
   383       : writer(new LemonWriter(_filename)), own_writer(true), 
   411       : writer(new LemonWriter(_filename)), own_writer(true), 
   384 	nodeset_writer(*writer, _graph, std::string()),
   412 	nodeset_writer(*writer, _graph, std::string()),
   385 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   413 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   386 	node_writer(*writer, nodeset_writer, std::string()),
   414 	node_writer(*writer, nodeset_writer, std::string()),
   387 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   415 	uedge_writer(*writer, uedgeset_writer, std::string()),
   388 	attribute_writer(*writer, std::string()) {}
   416 	attribute_writer(*writer, std::string()) {}
   389 
   417 
   390     /// \brief Construct a new UGraphWriter.
   418     /// \brief Construct a new UGraphWriter.
   391     ///
   419     ///
   392     /// Construct a new UGraphWriter. It writes the given graph
   420     /// Construct a new UGraphWriter. It writes the given graph
   393     /// to given LemonReader.
   421     /// to given LemonWriter.
   394     UGraphWriter(LemonWriter& _writer, const Graph& _graph)
   422     UGraphWriter(LemonWriter& _writer, const Graph& _graph)
   395       : writer(_writer), own_writer(false), 
   423       : writer(_writer), own_writer(false), 
   396 	nodeset_writer(*writer, _graph, std::string()),
   424 	nodeset_writer(*writer, _graph, std::string()),
   397 	u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   425 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   398 	node_writer(*writer, nodeset_writer, std::string()),
   426 	node_writer(*writer, nodeset_writer, std::string()),
   399 	u_edge_writer(*writer, u_edgeset_writer, std::string()),
   427 	uedge_writer(*writer, uedgeset_writer, std::string()),
   400 	attribute_writer(*writer, std::string()) {}
   428 	attribute_writer(*writer, std::string()) {}
   401 
   429 
   402     /// \brief Destruct the graph writer.
   430     /// \brief Destruct the graph writer.
   403     ///
   431     ///
   404     /// Destruct the graph writer.
   432     /// Destruct the graph writer.
   432     ///
   460     ///
   433     /// This function issues a new <i> edge map writing command</i> to
   461     /// This function issues a new <i> edge map writing command</i> to
   434     /// the writer.
   462     /// the writer.
   435     template <typename Map>
   463     template <typename Map>
   436     UGraphWriter& writeEdgeMap(std::string label, const Map& map) { 
   464     UGraphWriter& writeEdgeMap(std::string label, const Map& map) { 
   437       u_edgeset_writer.writeEdgeMap(label, map);
   465       uedgeset_writer.writeEdgeMap(label, map);
   438       return *this;
   466       return *this;
   439     }
   467     }
   440 
   468 
   441     /// \brief Issue a new edge map writing command to the writer.
   469     /// \brief Issue a new edge map writing command to the writer.
   442     ///
   470     ///
   443     /// This function issues a new <i> edge map writing command</i> to
   471     /// This function issues a new <i> edge map writing command</i> to
   444     /// the writer.
   472     /// the writer.
   445     template <typename ItemWriter, typename Map>
   473     template <typename ItemWriter, typename Map>
   446     UGraphWriter& writeEdgeMap(std::string label, const Map& map,
   474     UGraphWriter& writeEdgeMap(std::string label, const Map& map,
   447 				   const ItemWriter& iw = ItemWriter()) {
   475 				   const ItemWriter& iw = ItemWriter()) {
   448       u_edgeset_writer.writeEdgeMap(label, map, iw);
   476       uedgeset_writer.writeEdgeMap(label, map, iw);
   449       return *this;
   477       return *this;
   450     }
   478     }
   451 
   479 
   452     /// \brief Issue a new undirected edge map writing command to the writer.
   480     /// \brief Issue a new undirected edge map writing command to the writer.
   453     ///
   481     ///
   454     /// This function issues a new <i> undirected edge map writing
   482     /// This function issues a new <i> undirected edge map writing
   455     /// command</i> to the writer.
   483     /// command</i> to the writer.
   456     template <typename Map>
   484     template <typename Map>
   457     UGraphWriter& writeUEdgeMap(std::string label, const Map& map) { 
   485     UGraphWriter& writeUEdgeMap(std::string label, const Map& map) { 
   458       u_edgeset_writer.writeUEdgeMap(label, map);
   486       uedgeset_writer.writeUEdgeMap(label, map);
   459       return *this;
   487       return *this;
   460     }
   488     }
   461 
   489 
   462     /// \brief Issue a new undirected edge map writing command to the writer.
   490     /// \brief Issue a new undirected edge map writing command to the writer.
   463     ///
   491     ///
   464     /// This function issues a new <i> undirected edge map writing
   492     /// This function issues a new <i> undirected edge map writing
   465     /// command</i> to the writer.
   493     /// command</i> to the writer.
   466    template <typename ItemWriter, typename Map>
   494    template <typename ItemWriter, typename Map>
   467     UGraphWriter& writeUEdgeMap(std::string label, const Map& map,
   495     UGraphWriter& writeUEdgeMap(std::string label, const Map& map,
   468 					const ItemWriter& iw = ItemWriter()) {
   496 					const ItemWriter& iw = ItemWriter()) {
   469       u_edgeset_writer.writeUEdgeMap(label, map, iw);
   497       uedgeset_writer.writeUEdgeMap(label, map, iw);
   470       return *this;
   498       return *this;
   471     }
   499     }
   472 
   500 
   473     /// \brief Issue a new labeled node writer to the writer.
   501     /// \brief Issue a new labeled node writer to the writer.
   474     ///
   502     ///
   482     /// \brief Issue a new labeled edge writer to the writer.
   510     /// \brief Issue a new labeled edge writer to the writer.
   483     ///
   511     ///
   484     /// This function issues a new <i> labeled edge writing
   512     /// This function issues a new <i> labeled edge writing
   485     /// command</i> to the writer.
   513     /// command</i> to the writer.
   486     UGraphWriter& writeEdge(std::string label, const Edge& edge) {
   514     UGraphWriter& writeEdge(std::string label, const Edge& edge) {
   487       u_edge_writer.writeEdge(label, edge);
   515       uedge_writer.writeEdge(label, edge);
   488     }
   516     }
   489 
   517 
   490     /// \brief Issue a new labeled undirected edge writing command to
   518     /// \brief Issue a new labeled undirected edge writing command to
   491     /// the writer.
   519     /// the writer.
   492     ///
   520     ///
   493     /// Issue a new <i>labeled undirected edge writing command</i> to
   521     /// Issue a new <i>labeled undirected edge writing command</i> to
   494     /// the writer.
   522     /// the writer.
   495     UGraphWriter& writeUEdge(std::string label, const UEdge& edge) {
   523     UGraphWriter& writeUEdge(std::string label, const UEdge& edge) {
   496       u_edge_writer.writeUEdge(label, edge);
   524       uedge_writer.writeUEdge(label, edge);
   497     }
   525     }
   498 
   526 
   499     /// \brief Issue a new attribute writing command.
   527     /// \brief Issue a new attribute writing command.
   500     ///
   528     ///
   501     /// This function issues a new <i> attribute writing
   529     /// This function issues a new <i> attribute writing
   532     /// Executes the writing commands.
   560     /// Executes the writing commands.
   533     void run() {
   561     void run() {
   534       writer->run();
   562       writer->run();
   535     }
   563     }
   536 
   564 
       
   565     /// \brief Returns true if the writer can give back the labels by the items.
       
   566     ///
       
   567     /// Returns true if the writer can give back the the labels by the items.
       
   568     bool isLabelWriter() const {
       
   569       return nodeset_writer.isLabelWriter() && 
       
   570         uedgeset_writer.isLabelWriter();
       
   571     }
       
   572 
   537     /// \brief Write the label of the given node.
   573     /// \brief Write the label of the given node.
   538     ///
   574     ///
   539     /// It writes the label of the given node. If there was written an "label"
   575     /// It writes the label of the given node. If there was written a "label"
   540     /// named node map then it will write the map value belonging to the node.
   576     /// named node map then it will write the map value belonging to the node.
   541     void writeLabel(std::ostream& os, const Node& item) const {
   577     void writeLabel(std::ostream& os, const Node& item) const {
   542       nodeset_writer.writeLabel(os, item);
   578       nodeset_writer.writeLabel(os, item);
   543     } 
   579     } 
   544 
   580 
   545     /// \brief Write the label of the given edge.
   581     /// \brief Write the label of the given edge.
   546     ///
   582     ///
   547     /// It writes the label of the given edge. If there was written an "label"
   583     /// It writes the label of the given edge. If there was written a "label"
   548     /// named edge map then it will write the map value belonging to the edge.
   584     /// named edge map then it will write the map value belonging to the edge.
   549     void writeLabel(std::ostream& os, const Edge& item) const {
   585     void writeLabel(std::ostream& os, const Edge& item) const {
   550       u_edgeset_writer.writeLabel(os, item);
   586       uedgeset_writer.writeLabel(os, item);
   551     } 
   587     } 
   552 
   588 
   553     /// \brief Write the label of the given undirected edge.
   589     /// \brief Write the label of the given undirected edge.
   554     ///
   590     ///
   555     /// It writes the label of the given undirected edge. If there was
   591     /// It writes the label of the given undirected edge. If there was
   556     /// written an "label" named edge map then it will write the map
   592     /// written a "label" named edge map then it will write the map
   557     /// value belonging to the edge.
   593     /// value belonging to the edge.
   558     void writeLabel(std::ostream& os, const UEdge& item) const {
   594     void writeLabel(std::ostream& os, const UEdge& item) const {
   559       u_edgeset_writer.writeLabel(os, item);
   595       uedgeset_writer.writeLabel(os, item);
   560     } 
   596     } 
   561 
   597 
       
   598     /// \brief Sorts the given node vector by label.
       
   599     ///
       
   600     /// Sorts the given node vector by label. If there was written an
       
   601     /// "label" named map then the vector will be sorted by the values
       
   602     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   603     /// it will be sorted by its id in the graph.
       
   604     void sortByLabel(std::vector<Node>& nodes) const {
       
   605       nodeset_writer.sortByLabel(nodes);
       
   606     }
       
   607 
       
   608     /// \brief Sorts the given edge vector by label.
       
   609     ///
       
   610     /// Sorts the given edge vector by label. If there was written an
       
   611     /// "label" named map then the vector will be sorted by the values
       
   612     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   613     /// it will be sorted by its id in the graph.
       
   614     void sortByLabel(std::vector<Edge>& edges) const {
       
   615       uedgeset_writer.sortByLabel(edges);
       
   616     }
       
   617 
       
   618     /// \brief Sorts the given undirected edge vector by label.
       
   619     ///
       
   620     /// Sorts the given undirected edge vector by label. If there was
       
   621     /// written an "label" named map then the vector will be sorted by
       
   622     /// the values of this map. Otherwise if the \c forceLabel
       
   623     /// parameter was true it will be sorted by its id in the graph.
       
   624     void sortByLabel(std::vector<UEdge>& uedges) const {
       
   625       uedgeset_writer.sortByLabel(uedges);
       
   626     }
   562 
   627 
   563   private:
   628   private:
   564 
   629 
   565     LemonWriter* writer;
   630     LemonWriter* writer;
   566     bool own_writer;
   631     bool own_writer;
   567 
   632 
   568     NodeSetWriter<Graph, WriterTraits> nodeset_writer;
   633     NodeSetWriter<Graph, WriterTraits> nodeset_writer;
   569     UEdgeSetWriter<Graph, WriterTraits> u_edgeset_writer;
   634     UEdgeSetWriter<Graph, WriterTraits> uedgeset_writer;
   570 
   635 
   571     NodeWriter<Graph> node_writer;
   636     NodeWriter<Graph> node_writer;
   572     UEdgeWriter<Graph> u_edge_writer;
   637     UEdgeWriter<Graph> uedge_writer;
   573     
   638     
   574     AttributeWriter<WriterTraits> attribute_writer;
   639     AttributeWriter<WriterTraits> attribute_writer;
   575   };
   640   };
   576 
   641 
   577   /// @}
   642   /// @}