lemon/graph_writer.h
changeset 2504 46a82ce84cc6
parent 2467 2025a571895e
child 2553 bfced05fa852
equal deleted inserted replaced
15:3760315da2a1 16:bcea2890d400
   154 	delete writer;
   154 	delete writer;
   155     }
   155     }
   156 
   156 
   157     /// \brief Issue a new node map writing command for the writer.
   157     /// \brief Issue a new node map writing command for the writer.
   158     ///
   158     ///
   159    /// This function issues a new <i> node map writing command</i> to the writer.
   159     /// This function issues a new <i> node map writing command</i> to the writer.
   160     template <typename Map>
   160     template <typename Map>
   161     GraphWriter& writeNodeMap(std::string label, const Map& map) {
   161     GraphWriter& writeNodeMap(std::string label, const Map& map) {
   162       nodeset_writer.writeNodeMap(label, map);
   162       nodeset_writer.writeNodeMap(label, map);
   163       return *this;
   163       return *this;
   164     }
   164     }
   165 
   165 
   166 
   166 
   167     /// \brief Issue a new node map writing command for the writer.
   167     /// \brief Issue a new node map writing command for the writer.
   168     ///
   168     ///
   169    /// This function issues a new <i> node map writing command</i> to the writer.
   169     /// This function issues a new <i> node map writing command</i> to the writer.
   170     template <typename ItemWriter, typename Map>
   170     template <typename ItemWriter, typename Map>
   171     GraphWriter& writeNodeMap(std::string label, const Map& map, 
   171     GraphWriter& writeNodeMap(std::string label, const Map& map, 
   172 			      const ItemWriter& iw = ItemWriter()) {
   172 			      const ItemWriter& iw = ItemWriter()) {
   173       nodeset_writer.writeNodeMap(label, map, iw);
   173       nodeset_writer.writeNodeMap(label, map, iw);
   174       return *this;
   174       return *this;
   637     UEdgeWriter<Graph> uedge_writer;
   637     UEdgeWriter<Graph> uedge_writer;
   638     
   638     
   639     AttributeWriter<WriterTraits> attribute_writer;
   639     AttributeWriter<WriterTraits> attribute_writer;
   640   };
   640   };
   641 
   641 
       
   642   /// \brief The bipartite graph writer class.
       
   643   ///
       
   644   /// The \c BpUGraphWriter class provides the ugraph output. To write 
       
   645   /// a graph you should first give writing commands to the writer. You can 
       
   646   /// declare write command as \c NodeMap, \c EdgeMap or \c UEdgeMap 
       
   647   /// writing and labeled Node, Edge or UEdge writing.
       
   648   ///
       
   649   ///\code
       
   650   /// BpUGraphWriter<ListUGraph> writer(std::cout, graph);
       
   651   ///\endcode
       
   652   ///
       
   653   /// The \c writeNodeMap() function declares a \c NodeMap writing 
       
   654   /// command in the \c BpUGraphWriter. You should give as parameter 
       
   655   /// the name of the map and the map object. The NodeMap writing 
       
   656   /// command with name "label" should write a unique map because it 
       
   657   /// is regarded as label map.
       
   658   ///
       
   659   ///\code
       
   660   /// IdMap<ListUGraph, Node> nodeLabelMap;
       
   661   /// writer.writeNodeMap("label", nodeLabelMap);
       
   662   ///
       
   663   /// writer.writeNodeMap("coords", coords);
       
   664   /// writer.writeNodeMap("color", colorMap);
       
   665   ///\endcode
       
   666   ///
       
   667   /// With the \c writeUEdgeMap() member function you can give an 
       
   668   /// undirected edge map writing command similar to the NodeMaps.
       
   669   ///
       
   670   ///\code
       
   671   /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
       
   672   ///   edgeDescMap(graph);
       
   673   /// writer.writeUEdgeMap("descriptor", edgeDescMap);
       
   674   ///
       
   675   /// writer.writeUEdgeMap("weight", weightMap);
       
   676   /// writer.writeUEdgeMap("label", labelMap);
       
   677   ///\endcode
       
   678   /// 
       
   679   /// The EdgeMap handling is just a syntactical sugar. It writes
       
   680   /// two undirected edge map with '+' and '-' prefix in the name.
       
   681   ///
       
   682   ///\code
       
   683   /// writer.writeEdgeMap("capacity", capacityMap);
       
   684   ///\endcode
       
   685   ///
       
   686   ///
       
   687   /// With \c writeNode() and \c writeUEdge() functions you can 
       
   688   /// designate nodes and undirected edges in the graph. For example, you can 
       
   689   /// write out the source and target of the graph.
       
   690   ///
       
   691   ///\code
       
   692   /// writer.writeNode("source", sourceNode);
       
   693   /// writer.writeNode("target", targetNode);
       
   694   ///
       
   695   /// writer.writeUEdge("observed", uEdge);
       
   696   ///\endcode
       
   697   ///
       
   698   /// After you give all write commands you must call the \c run() member
       
   699   /// function, which executes all the writing commands.
       
   700   ///
       
   701   ///\code
       
   702   /// writer.run();
       
   703   ///\endcode
       
   704   ///
       
   705   /// \see DefaultWriterTraits
       
   706   /// \see QuotedStringWriter
       
   707   /// \see IdMap
       
   708   /// \see DescriptorMap
       
   709   /// \see \ref GraphWriter
       
   710   /// \see \ref graph-io-page
       
   711   /// \author Balazs Dezso
       
   712   template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
       
   713   class BpUGraphWriter {
       
   714   public:
       
   715     
       
   716     typedef _Graph Graph;
       
   717     typedef typename Graph::Node Node;
       
   718     typedef typename Graph::Edge Edge;
       
   719     typedef typename Graph::UEdge UEdge;
       
   720 
       
   721     typedef _WriterTraits WriterTraits;
       
   722 
       
   723     /// \brief Construct a new BpUGraphWriter.
       
   724     ///
       
   725     /// Construct a new BpUGraphWriter. It writes the given graph
       
   726     /// to the given stream.
       
   727     BpUGraphWriter(std::ostream& _os, const Graph& _graph) 
       
   728       : writer(new LemonWriter(_os)), own_writer(true), 
       
   729 	nodeset_writer(*writer, _graph, std::string()),
       
   730 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
       
   731 	node_writer(*writer, nodeset_writer, std::string()),
       
   732 	uedge_writer(*writer, uedgeset_writer, std::string()),
       
   733 	attribute_writer(*writer, std::string()) {}
       
   734 
       
   735     /// \brief Construct a new BpUGraphWriter.
       
   736     ///
       
   737     /// Construct a new BpUGraphWriter. It writes the given graph
       
   738     /// to the given file.
       
   739     BpUGraphWriter(const std::string& _filename, const Graph& _graph) 
       
   740       : writer(new LemonWriter(_filename)), own_writer(true), 
       
   741 	nodeset_writer(*writer, _graph, std::string()),
       
   742 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
       
   743 	node_writer(*writer, nodeset_writer, std::string()),
       
   744 	uedge_writer(*writer, uedgeset_writer, std::string()),
       
   745 	attribute_writer(*writer, std::string()) {}
       
   746 
       
   747     /// \brief Construct a new BpUGraphWriter.
       
   748     ///
       
   749     /// Construct a new BpUGraphWriter. It writes the given graph
       
   750     /// to given LemonWriter.
       
   751     BpUGraphWriter(LemonWriter& _writer, const Graph& _graph)
       
   752       : writer(_writer), own_writer(false), 
       
   753 	nodeset_writer(*writer, _graph, std::string()),
       
   754 	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
       
   755 	node_writer(*writer, nodeset_writer, std::string()),
       
   756 	uedge_writer(*writer, uedgeset_writer, std::string()),
       
   757 	attribute_writer(*writer, std::string()) {}
       
   758 
       
   759     /// \brief Destruct the graph writer.
       
   760     ///
       
   761     /// Destruct the graph writer.
       
   762     ~BpUGraphWriter() {
       
   763       if (own_writer) 
       
   764 	delete writer;
       
   765     }
       
   766 
       
   767     /// \brief Issue a new node map writing command to the writer.
       
   768     ///
       
   769     /// This function issues a new <i> node map writing command</i> to
       
   770     /// the writer.
       
   771     template <typename Map>
       
   772     BpUGraphWriter& writeNodeMap(std::string label, const Map& map) {
       
   773       nodeset_writer.writeNodeMap(label, map);
       
   774       return *this;
       
   775     }
       
   776 
       
   777     /// \brief Issue a new node map writing command to the writer.
       
   778     ///
       
   779     /// This function issues a new <i> node map writing command</i> to
       
   780     /// the writer.
       
   781     template <typename ItemWriter, typename Map>
       
   782     BpUGraphWriter& writeNodeMap(std::string label, const Map& map, 
       
   783 			      const ItemWriter& iw = ItemWriter()) {
       
   784       nodeset_writer.writeNodeMap(label, map, iw);
       
   785       return *this;
       
   786     }
       
   787 
       
   788     /// \brief Issue a new A-node map writing command to the writer.
       
   789     ///
       
   790     /// This function issues a new <i> A-node map writing command</i> to
       
   791     /// the writer.
       
   792     template <typename Map>
       
   793     BpUGraphWriter& writeANodeMap(std::string label, const Map& map) {
       
   794       nodeset_writer.writeANodeMap(label, map);
       
   795       return *this;
       
   796     }
       
   797 
       
   798     /// \brief Issue a new A-node map writing command to the writer.
       
   799     ///
       
   800     /// This function issues a new <i> A-node map writing command</i> to
       
   801     /// the writer.
       
   802     template <typename ItemWriter, typename Map>
       
   803     BpUGraphWriter& writeANodeMap(std::string label, const Map& map, 
       
   804 			      const ItemWriter& iw = ItemWriter()) {
       
   805       nodeset_writer.writeANodeMap(label, map, iw);
       
   806       return *this;
       
   807     }
       
   808     /// \brief Issue a new B-node map writing command to the writer.
       
   809     ///
       
   810     /// This function issues a new <i> B-node map writing command</i> to
       
   811     /// the writer.
       
   812     template <typename Map>
       
   813     BpUGraphWriter& writeBNodeMap(std::string label, const Map& map) {
       
   814       nodeset_writer.writeBNodeMap(label, map);
       
   815       return *this;
       
   816     }
       
   817 
       
   818     /// \brief Issue a new B-node map writing command to the writer.
       
   819     ///
       
   820     /// This function issues a new <i> B-node map writing command</i> to
       
   821     /// the writer.
       
   822     template <typename ItemWriter, typename Map>
       
   823     BpUGraphWriter& writeBNodeMap(std::string label, const Map& map, 
       
   824 			      const ItemWriter& iw = ItemWriter()) {
       
   825       nodeset_writer.writeBNodeMap(label, map, iw);
       
   826       return *this;
       
   827     }
       
   828 
       
   829     /// \brief Issue a new edge map writing command to the writer.
       
   830     ///
       
   831     /// This function issues a new <i> edge map writing command</i> to
       
   832     /// the writer.
       
   833     template <typename Map>
       
   834     BpUGraphWriter& writeEdgeMap(std::string label, const Map& map) { 
       
   835       uedgeset_writer.writeEdgeMap(label, map);
       
   836       return *this;
       
   837     }
       
   838 
       
   839     /// \brief Issue a new edge map writing command to the writer.
       
   840     ///
       
   841     /// This function issues a new <i> edge map writing command</i> to
       
   842     /// the writer.
       
   843     template <typename ItemWriter, typename Map>
       
   844     BpUGraphWriter& writeEdgeMap(std::string label, const Map& map,
       
   845 				   const ItemWriter& iw = ItemWriter()) {
       
   846       uedgeset_writer.writeEdgeMap(label, map, iw);
       
   847       return *this;
       
   848     }
       
   849 
       
   850     /// \brief Issue a new undirected edge map writing command to the writer.
       
   851     ///
       
   852     /// This function issues a new <i> undirected edge map writing
       
   853     /// command</i> to the writer.
       
   854     template <typename Map>
       
   855     BpUGraphWriter& writeUEdgeMap(std::string label, const Map& map) { 
       
   856       uedgeset_writer.writeUEdgeMap(label, map);
       
   857       return *this;
       
   858     }
       
   859 
       
   860     /// \brief Issue a new undirected edge map writing command to the writer.
       
   861     ///
       
   862     /// This function issues a new <i> undirected edge map writing
       
   863     /// command</i> to the writer.
       
   864    template <typename ItemWriter, typename Map>
       
   865     BpUGraphWriter& writeUEdgeMap(std::string label, const Map& map,
       
   866 					const ItemWriter& iw = ItemWriter()) {
       
   867       uedgeset_writer.writeUEdgeMap(label, map, iw);
       
   868       return *this;
       
   869     }
       
   870 
       
   871     /// \brief Issue a new labeled node writer to the writer.
       
   872     ///
       
   873     /// This function issues a new <i> labeled node writing
       
   874     /// command</i> to the writer.
       
   875     BpUGraphWriter& writeNode(std::string label, const Node& node) {
       
   876       node_writer.writeNode(label, node);
       
   877       return *this;
       
   878     }
       
   879 
       
   880     /// \brief Issue a new labeled edge writer to the writer.
       
   881     ///
       
   882     /// This function issues a new <i> labeled edge writing
       
   883     /// command</i> to the writer.
       
   884     BpUGraphWriter& writeEdge(std::string label, const Edge& edge) {
       
   885       uedge_writer.writeEdge(label, edge);
       
   886     }
       
   887 
       
   888     /// \brief Issue a new labeled undirected edge writing command to
       
   889     /// the writer.
       
   890     ///
       
   891     /// Issue a new <i>labeled undirected edge writing command</i> to
       
   892     /// the writer.
       
   893     BpUGraphWriter& writeUEdge(std::string label, const UEdge& edge) {
       
   894       uedge_writer.writeUEdge(label, edge);
       
   895     }
       
   896 
       
   897     /// \brief Issue a new attribute writing command.
       
   898     ///
       
   899     /// This function issues a new <i> attribute writing
       
   900     /// command</i> to the writer.
       
   901     template <typename Value>
       
   902     BpUGraphWriter& writeAttribute(std::string label, const Value& value) {
       
   903       attribute_writer.writeAttribute(label, value);
       
   904       return *this;
       
   905     }
       
   906     
       
   907     /// \brief Issue a new attribute writing command.
       
   908     ///
       
   909     /// This function issues a new <i> attribute writing
       
   910     /// command</i> to the writer.
       
   911     template <typename ItemWriter, typename Value>
       
   912     BpUGraphWriter& writeAttribute(std::string label, const Value& value, 
       
   913 			       const ItemWriter& iw = ItemWriter()) {
       
   914       attribute_writer.writeAttribute(label, value, iw);
       
   915       return *this;
       
   916     }
       
   917 
       
   918     /// \brief Conversion operator to LemonWriter.
       
   919     ///
       
   920     /// Conversion operator to LemonWriter. It makes possible
       
   921     /// to access the encapsulated \e LemonWriter, this way
       
   922     /// you can attach to this writer new instances of 
       
   923     /// \e LemonWriter::SectionWriter.
       
   924     operator LemonWriter&() {
       
   925       return *writer;
       
   926     }
       
   927 
       
   928     /// \brief Executes the writing commands.
       
   929     ///
       
   930     /// Executes the writing commands.
       
   931     void run() {
       
   932       writer->run();
       
   933     }
       
   934 
       
   935     /// \brief Returns true if the writer can give back the labels by the items.
       
   936     ///
       
   937     /// Returns true if the writer can give back the the labels by the items.
       
   938     bool isLabelWriter() const {
       
   939       return nodeset_writer.isLabelWriter() && 
       
   940         uedgeset_writer.isLabelWriter();
       
   941     }
       
   942 
       
   943     /// \brief Write the label of the given node.
       
   944     ///
       
   945     /// It writes the label of the given node. If there was written a "label"
       
   946     /// named node map then it will write the map value belonging to the node.
       
   947     void writeLabel(std::ostream& os, const Node& item) const {
       
   948       nodeset_writer.writeLabel(os, item);
       
   949     } 
       
   950 
       
   951     /// \brief Write the label of the given edge.
       
   952     ///
       
   953     /// It writes the label of the given edge. If there was written a "label"
       
   954     /// named edge map then it will write the map value belonging to the edge.
       
   955     void writeLabel(std::ostream& os, const Edge& item) const {
       
   956       uedgeset_writer.writeLabel(os, item);
       
   957     } 
       
   958 
       
   959     /// \brief Write the label of the given undirected edge.
       
   960     ///
       
   961     /// It writes the label of the given undirected edge. If there was
       
   962     /// written a "label" named edge map then it will write the map
       
   963     /// value belonging to the edge.
       
   964     void writeLabel(std::ostream& os, const UEdge& item) const {
       
   965       uedgeset_writer.writeLabel(os, item);
       
   966     } 
       
   967 
       
   968     /// \brief Sorts the given node vector by label.
       
   969     ///
       
   970     /// Sorts the given node vector by label. If there was written an
       
   971     /// "label" named map then the vector will be sorted by the values
       
   972     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   973     /// it will be sorted by its id in the graph.
       
   974     void sortByLabel(std::vector<Node>& nodes) const {
       
   975       nodeset_writer.sortByLabel(nodes);
       
   976     }
       
   977 
       
   978     /// \brief Sorts the given edge vector by label.
       
   979     ///
       
   980     /// Sorts the given edge vector by label. If there was written an
       
   981     /// "label" named map then the vector will be sorted by the values
       
   982     /// of this map. Otherwise if the \c forceLabel parameter was true
       
   983     /// it will be sorted by its id in the graph.
       
   984     void sortByLabel(std::vector<Edge>& edges) const {
       
   985       uedgeset_writer.sortByLabel(edges);
       
   986     }
       
   987 
       
   988     /// \brief Sorts the given undirected edge vector by label.
       
   989     ///
       
   990     /// Sorts the given undirected edge vector by label. If there was
       
   991     /// written an "label" named map then the vector will be sorted by
       
   992     /// the values of this map. Otherwise if the \c forceLabel
       
   993     /// parameter was true it will be sorted by its id in the graph.
       
   994     void sortByLabel(std::vector<UEdge>& uedges) const {
       
   995       uedgeset_writer.sortByLabel(uedges);
       
   996     }
       
   997 
       
   998   private:
       
   999 
       
  1000     LemonWriter* writer;
       
  1001     bool own_writer;
       
  1002 
       
  1003     BpNodeSetWriter<Graph, WriterTraits> nodeset_writer;
       
  1004     UEdgeSetWriter<Graph, WriterTraits> uedgeset_writer;
       
  1005 
       
  1006     NodeWriter<Graph> node_writer;
       
  1007     UEdgeWriter<Graph> uedge_writer;
       
  1008     
       
  1009     AttributeWriter<WriterTraits> attribute_writer;
       
  1010   };
       
  1011 
   642   /// @}
  1012   /// @}
   643 
  1013 
   644 }
  1014 }
   645 
  1015 
   646 #endif
  1016 #endif