src/lemon/graph_writer.h
changeset 1394 f0c48d7fa73d
parent 1359 1581f961cfaa
child 1396 56f9a4ba9149
equal deleted inserted replaced
11:4f17c1c61444 12:06cb54690e9f
   166   ///
   166   ///
   167   /// \code
   167   /// \code
   168   /// GraphWriter<ListGraph> writer(std::cout, graph);
   168   /// GraphWriter<ListGraph> writer(std::cout, graph);
   169   /// \endcode
   169   /// \endcode
   170   ///
   170   ///
   171   /// The \c addNodeMap() function declares a \c NodeMap writing command in the
   171   /// The \c writeNodeMap() function declares a \c NodeMap writing 
   172   /// \c GraphWriter. You should give as parameter the name of the map and the
   172   /// command in the \c GraphWriter. You should give as parameter 
   173   /// map object. The NodeMap writing command with name "id" should write a 
   173   /// the name of the map and the map object. The NodeMap writing 
   174   /// unique map because it is regarded as ID map.
   174   /// command with name "id" should write a unique map because it 
       
   175   /// is regarded as ID map.
   175   ///
   176   ///
   176   /// \code
   177   /// \code
   177   /// IdMap<ListGraph, Node> nodeIdMap;
   178   /// IdMap<ListGraph, Node> nodeIdMap;
   178   /// writer.addNodeMap("id", nodeIdMap);
   179   /// writer.writeNodeMap("id", nodeIdMap);
   179   ///
   180   ///
   180   /// writer.addNodeMap("x-coord", xCoordMap);
   181   /// writer.writeNodeMap("x-coord", xCoordMap);
   181   /// writer.addNodeMap("y-coord", yCoordMap);
   182   /// writer.writeNodeMap("y-coord", yCoordMap);
   182   /// writer.addNodeMap("color", colorMap);
   183   /// writer.writeNodeMap("color", colorMap);
   183   /// \endcode
   184   /// \endcode
   184   ///
   185   ///
   185   /// With the \c addEdgeMap() member function you can give an edge map
   186   /// With the \c writeEdgeMap() member function you can give an edge map
   186   /// writing command similar to the NodeMaps.
   187   /// writing command similar to the NodeMaps.
   187   ///
   188   ///
   188   /// \code
   189   /// \code
   189   /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
   190   /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
   190   ///   edgeDescMap(graph);
   191   ///   edgeDescMap(graph);
   191   /// writer.addEdgeMap("descriptor", edgeDescMap);
   192   /// writer.writeEdgeMap("descriptor", edgeDescMap);
   192   ///
   193   ///
   193   /// writer.addEdgeMap("weight", weightMap);
   194   /// writer.writeEdgeMap("weight", weightMap);
   194   /// writer.addEdgeMap("label", labelMap);
   195   /// writer.writeEdgeMap("label", labelMap);
   195   /// \endcode
   196   /// \endcode
   196   ///
   197   ///
   197   /// With \c addNode() and \c addEdge() functions you can point out Nodes and
   198   /// With \c writeNode() and \c writeEdge() functions you can 
   198   /// Edges in the graph. By example, you can write out the source and target
   199   /// point out Nodes and Edges in the graph. By example, you can 
   199   /// of the graph.
   200   /// write out the source and target of the graph.
   200   ///
   201   ///
   201   /// \code
   202   /// \code
   202   /// writer.addNode("source", sourceNode);
   203   /// writer.writeNode("source", sourceNode);
   203   /// writer.addNode("target", targetNode);
   204   /// writer.writeNode("target", targetNode);
   204   ///
   205   ///
   205   /// writer.addEdge("observed", edge);
   206   /// writer.writeEdge("observed", edge);
   206   /// \endcode
   207   /// \endcode
   207   ///
   208   ///
   208   /// After you give all write commands you must call the \c run() member
   209   /// After you give all write commands you must call the \c run() member
   209   /// function, which execute all the writer commands.
   210   /// function, which execute all the writer commands.
   210   ///
   211   ///
   214   ///
   215   ///
   215   /// \see DefaultWriterTraits
   216   /// \see DefaultWriterTraits
   216   /// \see QuotedStringWriter
   217   /// \see QuotedStringWriter
   217   /// \see IdMap
   218   /// \see IdMap
   218   /// \see DescriptorMap
   219   /// \see DescriptorMap
   219   /// \see \ref GraphReader
   220   /// \see \ref GraphWriter
   220   /// \see \ref graph-io-page
   221   /// \see \ref graph-io-page
   221   /// \author Balazs Dezso
   222   /// \author Balazs Dezso
   222   template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
   223   template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
   223   class GraphWriter {
   224   class GraphWriter {
   224   public:
   225   public:
   260 
   261 
   261     /// \brief Add a new node map writer command for the writer.
   262     /// \brief Add a new node map writer command for the writer.
   262     ///
   263     ///
   263     /// Add a new node map writer command for the writer.
   264     /// Add a new node map writer command for the writer.
   264     template <typename Map>
   265     template <typename Map>
   265     GraphWriter& addNodeMap(std::string name, const Map& map) {
   266     GraphWriter& writeNodeMap(std::string name, const Map& map) {
   266       return addNodeMap<typename WriterTraits::template Writer<
   267       return writeNodeMap<typename WriterTraits::template Writer<
   267 	typename Map::Value>, Map>(name, map);
   268 	typename Map::Value>, Map>(name, map);
   268     }
   269     }
   269 
   270 
   270     /// \brief Add a new node map writer command for the writer.
   271     /// \brief Add a new node map writer command for the writer.
   271     ///
   272     ///
   272     /// Add a new node map writer command for the writer.
   273     /// Add a new node map writer command for the writer.
   273     template <typename Writer, typename Map>
   274     template <typename Writer, typename Map>
   274     GraphWriter& addNodeMap(std::string name, const Map& map, 
   275     GraphWriter& writeNodeMap(std::string name, const Map& map, 
   275 			      const Writer& writer = Writer()) {
   276 			      const Writer& writer = Writer()) {
   276       node_map_writers.push_back(
   277       node_map_writers.push_back(
   277         make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
   278         make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
   278       return *this;
   279       return *this;
   279     }
   280     }
   282 
   283 
   283     /// \brief Add a new edge map writer command for the writer.
   284     /// \brief Add a new edge map writer command for the writer.
   284     ///
   285     ///
   285     /// Add a new edge map writer command for the writer.
   286     /// Add a new edge map writer command for the writer.
   286     template <typename Map>
   287     template <typename Map>
   287     GraphWriter& addEdgeMap(std::string name, const Map& map) { 
   288     GraphWriter& writeEdgeMap(std::string name, const Map& map) { 
   288       return addEdgeMap<typename WriterTraits::template Writer<
   289       return writeEdgeMap<typename WriterTraits::template Writer<
   289         typename Map::Value>, Map>(name, map);
   290         typename Map::Value>, Map>(name, map);
   290     }
   291     }
   291 
   292 
   292 
   293 
   293     /// \brief Add a new edge map writer command for the writer.
   294     /// \brief Add a new edge map writer command for the writer.
   294     ///
   295     ///
   295     /// Add a new edge map writer command for the writer.
   296     /// Add a new edge map writer command for the writer.
   296     template <typename Writer, typename Map>
   297     template <typename Writer, typename Map>
   297     GraphWriter& addEdgeMap(std::string name, 
   298     GraphWriter& writeEdgeMap(std::string name, 
   298 			    const Map& map, const Writer& writer = Writer()) {
   299 			    const Map& map, const Writer& writer = Writer()) {
   299       edge_map_writers.push_back(make_pair(name, 
   300       edge_map_writers.push_back(make_pair(name, 
   300 	new MapWriter<Edge, Map, Writer>(map, writer)));
   301 	new MapWriter<Edge, Map, Writer>(map, writer)));
   301       return *this;
   302       return *this;
   302     }
   303     }
   303 
   304 
   304     /// \brief Add a new labeled node writer for the writer.
   305     /// \brief Add a new labeled node writer for the writer.
   305     ///
   306     ///
   306     /// Add a new labeled node writer for the writer.
   307     /// Add a new labeled node writer for the writer.
   307     GraphWriter& addNode(std::string name, const Node& node) {
   308     GraphWriter& writeNode(std::string name, const Node& node) {
   308       node_writers.push_back(make_pair(name, node));
   309       node_writers.push_back(make_pair(name, node));
   309       return *this;
   310       return *this;
   310     }
   311     }
   311 
   312 
   312     /// \brief Add a new labeled edge writer for the writer.
   313     /// \brief Add a new labeled edge writer for the writer.
   313     ///
   314     ///
   314     /// Add a new labeled edge writer for the writer.
   315     /// Add a new labeled edge writer for the writer.
   315     GraphWriter& addEdge(std::string name, const Edge& edge) {
   316     GraphWriter& writeEdge(std::string name, const Edge& edge) {
   316       edge_writers.push_back(make_pair(name, edge));
   317       edge_writers.push_back(make_pair(name, edge));
   317       return *this;
   318       return *this;
   318     }
   319     }
   319 
   320 
   320     /// \brief Executes the writer commands.
   321     /// \brief Executes the writer commands.
   465   /// \param cost The cost map.
   466   /// \param cost The cost map.
   466   template<typename Graph, typename CapacityMap, typename CostMap>
   467   template<typename Graph, typename CapacityMap, typename CostMap>
   467   void writeGraph(std::ostream& os, const Graph &g, 
   468   void writeGraph(std::ostream& os, const Graph &g, 
   468 		  const CapacityMap& capacity, const typename Graph::Node &s,
   469 		  const CapacityMap& capacity, const typename Graph::Node &s,
   469 		  const typename Graph::Node &t, const CostMap& cost) {
   470 		  const typename Graph::Node &t, const CostMap& cost) {
   470     GraphWriter<Graph> reader(os, g);
   471     GraphWriter<Graph> writer(os, g);
   471     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   472     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   472     reader.addNodeMap("id", nodeIdMap);
   473     writer.writeNodeMap("id", nodeIdMap);
   473     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   474     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   474     reader.addEdgeMap("id", edgeIdMap);
   475     writer.writeEdgeMap("id", edgeIdMap);
   475     reader.addEdgeMap("capacity", capacity);
   476     writer.writeEdgeMap("capacity", capacity);
   476     reader.addEdgeMap("cost", cost);
   477     writer.writeEdgeMap("cost", cost);
   477     reader.addNode("source", s);
   478     writer.writeNode("source", s);
   478     reader.addNode("target", t);
   479     writer.writeNode("target", t);
   479     reader.run();
   480     writer.run();
   480   }
   481   }
   481 
   482 
   482   /// \brief Write a graph to the output.
   483   /// \brief Write a graph to the output.
   483   ///
   484   ///
   484   /// Write a graph to the output.
   485   /// Write a graph to the output.
   489   /// \param t The target node.
   490   /// \param t The target node.
   490   template<typename Graph, typename CapacityMap>
   491   template<typename Graph, typename CapacityMap>
   491   void writeGraph(std::ostream& os, const Graph &g, 
   492   void writeGraph(std::ostream& os, const Graph &g, 
   492 		  const CapacityMap& capacity, const typename Graph::Node &s,
   493 		  const CapacityMap& capacity, const typename Graph::Node &s,
   493 		  const typename Graph::Node &t) {
   494 		  const typename Graph::Node &t) {
   494     GraphWriter<Graph> reader(os, g);
   495     GraphWriter<Graph> writer(os, g);
   495     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   496     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   496     reader.addNodeMap("id", nodeIdMap);
   497     writer.writeNodeMap("id", nodeIdMap);
   497     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   498     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   498     reader.addEdgeMap("id", edgeIdMap);
   499     writer.writeEdgeMap("id", edgeIdMap);
   499     reader.addEdgeMap("capacity", capacity);
   500     writer.writeEdgeMap("capacity", capacity);
   500     reader.addNode("source", s);
   501     writer.writeNode("source", s);
   501     reader.addNode("target", t);
   502     writer.writeNode("target", t);
   502     reader.run();
   503     writer.run();
   503   }
   504   }
   504 
   505 
   505   /// \brief Write a graph to the output.
   506   /// \brief Write a graph to the output.
   506   ///
   507   ///
   507   /// Write a graph to the output.
   508   /// Write a graph to the output.
   510   /// \param capacity The capacity map.
   511   /// \param capacity The capacity map.
   511   /// \param s The source node.
   512   /// \param s The source node.
   512   template<typename Graph, typename CapacityMap>
   513   template<typename Graph, typename CapacityMap>
   513   void writeGraph(std::ostream& os, const Graph &g, 
   514   void writeGraph(std::ostream& os, const Graph &g, 
   514 		  const CapacityMap& capacity, const typename Graph::Node &s) {
   515 		  const CapacityMap& capacity, const typename Graph::Node &s) {
   515     GraphWriter<Graph> reader(os, g);
   516     GraphWriter<Graph> writer(os, g);
   516     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   517     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   517     reader.addNodeMap("id", nodeIdMap);
   518     writer.writeNodeMap("id", nodeIdMap);
   518     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   519     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   519     reader.addEdgeMap("id", edgeIdMap);
   520     writer.writeEdgeMap("id", edgeIdMap);
   520     reader.addEdgeMap("capacity", capacity);
   521     writer.writeEdgeMap("capacity", capacity);
   521     reader.addNode("source", s);
   522     writer.writeNode("source", s);
   522     reader.run();
   523     writer.run();
   523   }
   524   }
   524 
   525 
   525   /// \brief Write a graph to the output.
   526   /// \brief Write a graph to the output.
   526   ///
   527   ///
   527   /// Write a graph to the output.
   528   /// Write a graph to the output.
   529   /// \param g The graph.
   530   /// \param g The graph.
   530   /// \param capacity The capacity map.
   531   /// \param capacity The capacity map.
   531   template<typename Graph, typename CapacityMap>
   532   template<typename Graph, typename CapacityMap>
   532   void writeGraph(std::ostream& os, const Graph &g, 
   533   void writeGraph(std::ostream& os, const Graph &g, 
   533 		  const CapacityMap& capacity) {
   534 		  const CapacityMap& capacity) {
   534     GraphWriter<Graph> reader(os, g);
   535     GraphWriter<Graph> writer(os, g);
   535     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   536     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   536     reader.addNodeMap("id", nodeIdMap);
   537     writer.writeNodeMap("id", nodeIdMap);
   537     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   538     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   538     reader.addEdgeMap("id", edgeIdMap);
   539     writer.writeEdgeMap("id", edgeIdMap);
   539     reader.addEdgeMap("capacity", capacity);
   540     writer.writeEdgeMap("capacity", capacity);
   540     reader.run();
   541     writer.run();
   541   }
   542   }
   542 
   543 
   543   /// \brief Write a graph to the output.
   544   /// \brief Write a graph to the output.
   544   ///
   545   ///
   545   /// Write a graph to the output.
   546   /// Write a graph to the output.
   546   /// \param os The output stream.
   547   /// \param os The output stream.
   547   /// \param g The graph.
   548   /// \param g The graph.
   548   template<typename Graph>
   549   template<typename Graph>
   549   void writeGraph(std::ostream& os, const Graph &g) {
   550   void writeGraph(std::ostream& os, const Graph &g) {
   550     GraphWriter<Graph> reader(os, g);
   551     GraphWriter<Graph> writer(os, g);
   551     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   552     IdMap<Graph, typename Graph::Node> nodeIdMap(g);
   552     reader.addNodeMap("id", nodeIdMap);
   553     writer.writeNodeMap("id", nodeIdMap);
   553     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   554     IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
   554     reader.addEdgeMap("id", edgeIdMap);
   555     writer.writeEdgeMap("id", edgeIdMap);
   555     reader.run();
   556     writer.run();
   556   }
   557   }
   557 
   558 
   558   /// @}
   559   /// @}
   559 
   560 
   560 }
   561 }