Bug fixes.
2 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 ///\brief Lemon Graph Format writer.
21 #ifndef LEMON_GRAPH_WRITER_H
22 #define LEMON_GRAPH_WRITER_H
26 #include <lemon/error.h>
27 #include <lemon/lemon_writer.h>
31 /// \addtogroup io_group
34 /// \brief The graph writer class.
36 /// The \c GraphWriter class provides the graph output. To write a graph
37 /// you should first give writing commands for the writer. You can declare
38 /// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
42 /// GraphWriter<ListGraph> writer(std::cout, graph);
45 /// The \c writeNodeMap() function declares a \c NodeMap writing
46 /// command in the \c GraphWriter. You should give as parameter
47 /// the name of the map and the map object. The NodeMap writing
48 /// command with name "id" should write a unique map because it
49 /// is regarded as ID map.
52 /// IdMap<ListGraph, Node> nodeIdMap;
53 /// writer.writeNodeMap("id", nodeIdMap);
55 /// writer.writeNodeMap("x-coord", xCoordMap);
56 /// writer.writeNodeMap("y-coord", yCoordMap);
57 /// writer.writeNodeMap("color", colorMap);
60 /// With the \c writeEdgeMap() member function you can give an edge map
61 /// writing command similar to the NodeMaps.
64 /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
65 /// edgeDescMap(graph);
66 /// writer.writeEdgeMap("descriptor", edgeDescMap);
68 /// writer.writeEdgeMap("weight", weightMap);
69 /// writer.writeEdgeMap("label", labelMap);
72 /// With \c writeNode() and \c writeEdge() functions you can
73 /// point out Nodes and Edges in the graph. By example, you can
74 /// write out the source and target of the graph.
77 /// writer.writeNode("source", sourceNode);
78 /// writer.writeNode("target", targetNode);
80 /// writer.writeEdge("observed", edge);
83 /// After you give all write commands you must call the \c run() member
84 /// function, which execute all the writer commands.
90 /// \see DefaultWriterTraits
91 /// \see QuotedStringWriter
93 /// \see DescriptorMap
94 /// \see \ref GraphWriter
95 /// \see \ref graph-io-page
96 /// \author Balazs Dezso
97 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
101 typedef _Graph Graph;
102 typedef typename Graph::Node Node;
103 typedef typename Graph::Edge Edge;
105 typedef _WriterTraits WriterTraits;
107 /// \brief Construct a new GraphWriter.
109 /// Construct a new GraphWriter. It writes the given graph
110 /// to the given stream.
111 GraphWriter(std::ostream& _os, const Graph& _graph)
112 : writer(new LemonWriter(_os)), own_writer(true),
114 nodeset_writer(*writer, graph, std::string()),
115 edgeset_writer(*writer, graph, nodeset_writer, std::string()),
116 node_writer(*writer, nodeset_writer, std::string()),
117 edge_writer(*writer, edgeset_writer, std::string()),
118 attribute_writer(*writer, std::string()) {}
120 /// \brief Construct a new GraphWriter.
122 /// Construct a new GraphWriter. It writes into the given graph
123 /// to the given file.
124 GraphWriter(const std::string& _filename, const Graph& _graph)
125 : writer(new LemonWriter(_filename)), own_writer(true),
127 nodeset_writer(*writer, graph, std::string()),
128 edgeset_writer(*writer, graph, nodeset_writer, std::string()),
129 node_writer(*writer, nodeset_writer, std::string()),
130 edge_writer(*writer, edgeset_writer, std::string()),
131 attribute_writer(*writer, std::string()) {}
133 /// \brief Construct a new GraphWriter.
135 /// Construct a new GraphWriter. It writes into the given graph
136 /// to given LemonReader.
137 GraphWriter(LemonWriter& _writer, const Graph& _graph)
138 : writer(_writer), own_writer(false),
140 nodeset_writer(*writer, graph, std::string()),
141 edgeset_writer(*writer, graph, nodeset_writer, std::string()),
142 node_writer(*writer, nodeset_writer, std::string()),
143 edge_writer(*writer, edgeset_writer, std::string()),
144 attribute_writer(*writer, std::string()) {}
146 /// \brief Destruct the graph writer.
148 /// Destruct the graph writer.
154 /// \brief Add a new node map writer command for the writer.
156 /// Add a new node map writer command for the writer.
157 template <typename Map>
158 GraphWriter& writeNodeMap(std::string name, const Map& map) {
159 nodeset_writer.writeMap(name, map);
163 /// \brief Add a new node map writer command for the writer.
165 /// Add a new node map writer command for the writer.
166 template <typename Writer, typename Map>
167 GraphWriter& writeNodeMap(std::string name, const Map& map,
168 const Writer& writer = Writer()) {
169 nodeset_writer.writeMap(name, map, writer);
174 /// \brief Add a new edge map writer command for the writer.
176 /// Add a new edge map writer command for the writer.
177 template <typename Map>
178 GraphWriter& writeEdgeMap(std::string name, const Map& map) {
179 edgeset_writer.writeMap(name, map);
184 /// \brief Add a new edge map writer command for the writer.
186 /// Add a new edge map writer command for the writer.
187 template <typename Writer, typename Map>
188 GraphWriter& writeEdgeMap(std::string name, const Map& map,
189 const Writer& writer = Writer()) {
190 edgeset_writer.writeMap(name, map, writer);
194 /// \brief Add a new labeled node writer for the writer.
196 /// Add a new labeled node writer for the writer.
197 GraphWriter& writeNode(std::string name, const Node& node) {
198 node_writer.writeNode(name, node);
202 /// \brief Add a new labeled edge writer for the writer.
204 /// Add a new labeled edge writer for the writer.
205 GraphWriter& writeEdge(std::string name, const Edge& edge) {
206 edge_writer.writeEdge(name, edge);
209 /// \brief Add a new attribute writer command.
211 /// Add a new attribute writer command.
212 template <typename Value>
213 GraphWriter& writeAttribute(std::string name, const Value& value) {
214 attribute_writer.writeAttribute(name, value);
218 /// \brief Add a new attribute writer command.
220 /// Add a new attribute writer command.
221 template <typename Writer, typename Value>
222 GraphWriter& writeAttribute(std::string name, const Value& value,
223 const Writer& writer) {
224 attribute_writer.writeAttribute<Writer>(name, value, writer);
228 /// \brief Conversion operator to LemonWriter.
230 /// Conversion operator to LemonWriter. It make possible
231 /// to access the encapsulated \e LemonWriter, this way
232 /// you can attach to this writer new instances of
233 /// \e LemonWriter::SectionWriter.
234 operator LemonWriter&() {
238 /// \brief Executes the writer commands.
240 /// Executes the writer commands.
252 NodeSetWriter<Graph, WriterTraits> nodeset_writer;
253 EdgeSetWriter<Graph, WriterTraits> edgeset_writer;
255 NodeWriter<Graph> node_writer;
256 EdgeWriter<Graph> edge_writer;
258 AttributeWriter<WriterTraits> attribute_writer;
262 /// \brief Write a graph to the output.
264 /// Write a graph to the output.
265 /// \param os The output stream.
266 /// \param g The graph.
267 /// \param capacity The capacity map.
268 /// \param s The source node.
269 /// \param t The target node.
270 /// \param cost The cost map.
271 template<typename Graph, typename CapacityMap, typename CostMap>
272 void writeGraph(std::ostream& os, const Graph &g,
273 const CapacityMap& capacity, const typename Graph::Node &s,
274 const typename Graph::Node &t, const CostMap& cost) {
275 GraphWriter<Graph> writer(os, g);
276 IdMap<Graph, typename Graph::Node> nodeIdMap(g);
277 writer.writeNodeMap("id", nodeIdMap);
278 IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
279 writer.writeEdgeMap("id", edgeIdMap);
280 writer.writeEdgeMap("capacity", capacity);
281 writer.writeEdgeMap("cost", cost);
282 writer.writeNode("source", s);
283 writer.writeNode("target", t);
287 /// \brief Write a graph to the output.
289 /// Write a graph to the output.
290 /// \param os The output stream.
291 /// \param g The graph.
292 /// \param capacity The capacity map.
293 /// \param s The source node.
294 /// \param t The target node.
295 template<typename Graph, typename CapacityMap>
296 void writeGraph(std::ostream& os, const Graph &g,
297 const CapacityMap& capacity, const typename Graph::Node &s,
298 const typename Graph::Node &t) {
299 GraphWriter<Graph> writer(os, g);
300 IdMap<Graph, typename Graph::Node> nodeIdMap(g);
301 writer.writeNodeMap("id", nodeIdMap);
302 IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
303 writer.writeEdgeMap("id", edgeIdMap);
304 writer.writeEdgeMap("capacity", capacity);
305 writer.writeNode("source", s);
306 writer.writeNode("target", t);
310 /// \brief Write a graph to the output.
312 /// Write a graph to the output.
313 /// \param os The output stream.
314 /// \param g The graph.
315 /// \param capacity The capacity map.
316 /// \param s The source node.
317 template<typename Graph, typename CapacityMap>
318 void writeGraph(std::ostream& os, const Graph &g,
319 const CapacityMap& capacity, const typename Graph::Node &s) {
320 GraphWriter<Graph> writer(os, g);
321 IdMap<Graph, typename Graph::Node> nodeIdMap(g);
322 writer.writeNodeMap("id", nodeIdMap);
323 IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
324 writer.writeEdgeMap("id", edgeIdMap);
325 writer.writeEdgeMap("capacity", capacity);
326 writer.writeNode("source", s);
330 /// \brief Write a graph to the output.
332 /// Write a graph to the output.
333 /// \param os The output stream.
334 /// \param g The graph.
335 /// \param capacity The capacity map.
336 template<typename Graph, typename CapacityMap>
337 void writeGraph(std::ostream& os, const Graph &g,
338 const CapacityMap& capacity) {
339 GraphWriter<Graph> writer(os, g);
340 IdMap<Graph, typename Graph::Node> nodeIdMap(g);
341 writer.writeNodeMap("id", nodeIdMap);
342 IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
343 writer.writeEdgeMap("id", edgeIdMap);
344 writer.writeEdgeMap("capacity", capacity);
348 /// \brief Write a graph to the output.
350 /// Write a graph to the output.
351 /// \param os The output stream.
352 /// \param g The graph.
353 template<typename Graph>
354 void writeGraph(std::ostream& os, const Graph &g) {
355 GraphWriter<Graph> writer(os, g);
356 IdMap<Graph, typename Graph::Node> nodeIdMap(g);
357 writer.writeNodeMap("id", nodeIdMap);
358 IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
359 writer.writeEdgeMap("id", edgeIdMap);