1 | /* -*- C++ -*- |
---|
2 | * lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
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. |
---|
10 | * |
---|
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 |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | ///\ingroup io_group |
---|
18 | ///\file |
---|
19 | ///\brief Lemon Graph Format writer. |
---|
20 | /// |
---|
21 | |
---|
22 | #ifndef LEMON_GRAPH_WRITER_H |
---|
23 | #define LEMON_GRAPH_WRITER_H |
---|
24 | |
---|
25 | #include <iostream> |
---|
26 | |
---|
27 | #include <lemon/error.h> |
---|
28 | #include <lemon/lemon_writer.h> |
---|
29 | |
---|
30 | namespace lemon { |
---|
31 | |
---|
32 | /// \addtogroup io_group |
---|
33 | /// @{ |
---|
34 | |
---|
35 | /// \brief The graph writer class. |
---|
36 | /// |
---|
37 | /// The \c GraphWriter class provides the graph output. |
---|
38 | /// Before you read this documentation it might be useful to read the general |
---|
39 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
40 | /// |
---|
41 | /// If you don't need very sophisticated |
---|
42 | /// behaviour then you can use the versions of the public function |
---|
43 | /// \ref writeGraph() to output a graph (or a max flow instance etc). |
---|
44 | /// |
---|
45 | /// To write a graph |
---|
46 | /// you should first give writing commands to the writer. You can declare |
---|
47 | /// write commands as \c NodeMap or \c EdgeMap writing and labeled Node and |
---|
48 | /// Edge writing. |
---|
49 | /// |
---|
50 | /// \code |
---|
51 | /// GraphWriter<ListGraph> writer(std::cout, graph); |
---|
52 | /// \endcode |
---|
53 | /// |
---|
54 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
55 | /// command in the \c GraphWriter. You should give as parameter |
---|
56 | /// the name of the map and the map object. The NodeMap writing |
---|
57 | /// command with name "id" should write a unique map because it |
---|
58 | /// is regarded as ID map (such a map is essential if the graph has edges). |
---|
59 | /// |
---|
60 | /// \code |
---|
61 | /// IdMap<ListGraph, Node> nodeIdMap; |
---|
62 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
63 | /// |
---|
64 | /// writer.writeNodeMap("coords", coords); |
---|
65 | /// writer.writeNodeMap("color", colorMap); |
---|
66 | /// \endcode |
---|
67 | /// |
---|
68 | /// With the \c writeEdgeMap() member function you can give an edge map |
---|
69 | /// writing command similar to the NodeMaps. |
---|
70 | /// |
---|
71 | /// \code |
---|
72 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
73 | /// edgeDescMap(graph); |
---|
74 | /// writer.writeEdgeMap("descriptor", edgeDescMap); |
---|
75 | /// |
---|
76 | /// writer.writeEdgeMap("weight", weightMap); |
---|
77 | /// writer.writeEdgeMap("label", labelMap); |
---|
78 | /// \endcode |
---|
79 | /// |
---|
80 | /// With \c writeNode() and \c writeEdge() functions you can |
---|
81 | /// point out Nodes and Edges in the graph. For example, you can |
---|
82 | /// write out the source and target of a maximum flow instance. |
---|
83 | /// |
---|
84 | /// \code |
---|
85 | /// writer.writeNode("source", sourceNode); |
---|
86 | /// writer.writeNode("target", targetNode); |
---|
87 | /// |
---|
88 | /// writer.writeEdge("observed", edge); |
---|
89 | /// \endcode |
---|
90 | /// |
---|
91 | /// After you give all write commands you must call the \c run() member |
---|
92 | /// function, which executes all the writing commands. |
---|
93 | /// |
---|
94 | /// \code |
---|
95 | /// writer.run(); |
---|
96 | /// \endcode |
---|
97 | /// |
---|
98 | /// \see DefaultWriterTraits |
---|
99 | /// \see QuotedStringWriter |
---|
100 | /// \see IdMap |
---|
101 | /// \see DescriptorMap |
---|
102 | /// \see \ref GraphReader |
---|
103 | /// \see \ref graph-io-page |
---|
104 | /// \author Balazs Dezso |
---|
105 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
106 | class GraphWriter { |
---|
107 | public: |
---|
108 | |
---|
109 | typedef _Graph Graph; |
---|
110 | typedef typename Graph::Node Node; |
---|
111 | typedef typename Graph::Edge Edge; |
---|
112 | |
---|
113 | typedef _WriterTraits WriterTraits; |
---|
114 | |
---|
115 | /// \brief Construct a new GraphWriter. |
---|
116 | /// |
---|
117 | /// This function constructs a new GraphWriter to write the given graph |
---|
118 | /// to the given stream. |
---|
119 | GraphWriter(std::ostream& _os, const Graph& _graph) |
---|
120 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
121 | nodeset_writer(*writer, _graph, std::string()), |
---|
122 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
123 | node_writer(*writer, nodeset_writer, std::string()), |
---|
124 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
125 | attribute_writer(*writer, std::string()) {} |
---|
126 | |
---|
127 | /// \brief Construct a new GraphWriter. |
---|
128 | /// |
---|
129 | /// This function constructs a new GraphWriter to write the given graph |
---|
130 | /// to the given file. |
---|
131 | GraphWriter(const std::string& _filename, const Graph& _graph) |
---|
132 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
133 | nodeset_writer(*writer, _graph, std::string()), |
---|
134 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
135 | node_writer(*writer, nodeset_writer, std::string()), |
---|
136 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
137 | attribute_writer(*writer, std::string()) {} |
---|
138 | |
---|
139 | /// \brief Construct a new GraphWriter. |
---|
140 | /// |
---|
141 | /// This function constructs a new GraphWriter to write the given graph |
---|
142 | /// to the given LemonReader. |
---|
143 | GraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
144 | : writer(_writer), own_writer(false), |
---|
145 | nodeset_writer(*writer, _graph, std::string()), |
---|
146 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
147 | node_writer(*writer, nodeset_writer, std::string()), |
---|
148 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
149 | attribute_writer(*writer, std::string()) {} |
---|
150 | |
---|
151 | /// \brief Destruct the graph writer. |
---|
152 | /// |
---|
153 | /// This function destructs the graph writer. |
---|
154 | ~GraphWriter() { |
---|
155 | if (own_writer) |
---|
156 | delete writer; |
---|
157 | } |
---|
158 | |
---|
159 | /// \brief Issue a new node map writing command for the writer. |
---|
160 | /// |
---|
161 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
162 | template <typename Map> |
---|
163 | GraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
164 | nodeset_writer.writeNodeMap(name, map); |
---|
165 | return *this; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /// \brief Issue a new node map writing command for the writer. |
---|
170 | /// |
---|
171 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
172 | template <typename Writer, typename Map> |
---|
173 | GraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
174 | const Writer& writer = Writer()) { |
---|
175 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
176 | return *this; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | /// \brief Issue a new edge map writing command for the writer. |
---|
181 | /// |
---|
182 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
183 | template <typename Map> |
---|
184 | GraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
185 | edgeset_writer.writeEdgeMap(name, map); |
---|
186 | return *this; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /// \brief Issue a new edge map writing command for the writer. |
---|
191 | /// |
---|
192 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
193 | template <typename Writer, typename Map> |
---|
194 | GraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
195 | const Writer& writer = Writer()) { |
---|
196 | edgeset_writer.writeEdgeMap(name, map, writer); |
---|
197 | return *this; |
---|
198 | } |
---|
199 | |
---|
200 | /// \brief Issue a new labeled node writing command to the writer. |
---|
201 | /// |
---|
202 | /// This function issues a new <i> labeled node writing command</i> |
---|
203 | /// to the writer. |
---|
204 | GraphWriter& writeNode(std::string name, const Node& node) { |
---|
205 | node_writer.writeNode(name, node); |
---|
206 | return *this; |
---|
207 | } |
---|
208 | |
---|
209 | /// \brief Issue a new labeled edge writing command to the writer. |
---|
210 | /// |
---|
211 | /// This function issues a new <i> labeled edge writing command</i> |
---|
212 | /// to the writer. |
---|
213 | GraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
214 | edge_writer.writeEdge(name, edge); |
---|
215 | } |
---|
216 | |
---|
217 | /// \brief Issue a new attribute writing command. |
---|
218 | /// |
---|
219 | /// This function issues a new <i> attribute writing command</i> |
---|
220 | /// to the writer. |
---|
221 | template <typename Value> |
---|
222 | GraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
223 | attribute_writer.writeAttribute(name, value); |
---|
224 | return *this; |
---|
225 | } |
---|
226 | |
---|
227 | /// \brief Issue a new attribute writing command. |
---|
228 | /// |
---|
229 | /// This function issues a new <i> attribute writing command</i> |
---|
230 | /// to the writer. |
---|
231 | template <typename Writer, typename Value> |
---|
232 | GraphWriter& writeAttribute(std::string name, const Value& value, |
---|
233 | const Writer& writer) { |
---|
234 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
235 | return *this; |
---|
236 | } |
---|
237 | |
---|
238 | /// \brief Conversion operator to LemonWriter. |
---|
239 | /// |
---|
240 | /// Conversion operator to LemonWriter. It makes possible |
---|
241 | /// to access the encapsulated \e LemonWriter, this way |
---|
242 | /// you can attach to this writer new instances of |
---|
243 | /// \e LemonWriter::SectionWriter. For more details see |
---|
244 | /// the \ref rwbackground "Background of Reading and Writing". |
---|
245 | operator LemonWriter&() { |
---|
246 | return *writer; |
---|
247 | } |
---|
248 | |
---|
249 | /// \brief Executes the writing commands. |
---|
250 | /// |
---|
251 | /// Executes the writing commands. |
---|
252 | void run() { |
---|
253 | writer->run(); |
---|
254 | } |
---|
255 | |
---|
256 | /// \brief Write the id of the given node. |
---|
257 | /// |
---|
258 | /// It writes the id of the given node. If there was written an "id" |
---|
259 | /// named node map then it will write the map value belonging to the node. |
---|
260 | void writeId(std::ostream& os, const Node& item) const { |
---|
261 | nodeset_writer.writeId(os, item); |
---|
262 | } |
---|
263 | |
---|
264 | /// \brief Write the id of the given edge. |
---|
265 | /// |
---|
266 | /// It writes the id of the given edge. If there was written an "id" |
---|
267 | /// named edge map then it will write the map value belonging to the edge. |
---|
268 | void writeId(std::ostream& os, const Edge& item) const { |
---|
269 | edgeset_writer.writeId(os, item); |
---|
270 | } |
---|
271 | |
---|
272 | private: |
---|
273 | |
---|
274 | LemonWriter* writer; |
---|
275 | bool own_writer; |
---|
276 | |
---|
277 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
278 | EdgeSetWriter<Graph, WriterTraits> edgeset_writer; |
---|
279 | |
---|
280 | NodeWriter<Graph> node_writer; |
---|
281 | EdgeWriter<Graph> edge_writer; |
---|
282 | |
---|
283 | AttributeWriter<WriterTraits> attribute_writer; |
---|
284 | }; |
---|
285 | |
---|
286 | |
---|
287 | ///\anchor writeGraph() |
---|
288 | /// |
---|
289 | /// \brief Write a graph to the output. |
---|
290 | /// |
---|
291 | /// Write a graph to the output. |
---|
292 | /// \param os The output stream. |
---|
293 | /// \param g The graph. |
---|
294 | template<typename Graph> |
---|
295 | void writeGraph(std::ostream& os, const Graph &g) { |
---|
296 | GraphWriter<Graph> writer(os, g); |
---|
297 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
298 | writer.writeNodeMap("id", nodeIdMap); |
---|
299 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
300 | writer.writeEdgeMap("id", edgeIdMap); |
---|
301 | writer.run(); |
---|
302 | } |
---|
303 | |
---|
304 | /// \brief Write a capacitated graph instance to the output. |
---|
305 | /// |
---|
306 | /// Write a capacitated graph (graph+capacity on the |
---|
307 | /// edges) to the output. |
---|
308 | /// \param os The output stream. |
---|
309 | /// \param g The graph. |
---|
310 | /// \param capacity The capacity map. |
---|
311 | template<typename Graph, typename CapacityMap> |
---|
312 | void writeGraph(std::ostream& os, const Graph &g, |
---|
313 | const CapacityMap& capacity) { |
---|
314 | GraphWriter<Graph> writer(os, g); |
---|
315 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
316 | writer.writeNodeMap("id", nodeIdMap); |
---|
317 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
318 | writer.writeEdgeMap("id", edgeIdMap); |
---|
319 | writer.writeEdgeMap("capacity", capacity); |
---|
320 | writer.run(); |
---|
321 | } |
---|
322 | |
---|
323 | /// \brief Write a shortest path instance to the output. |
---|
324 | /// |
---|
325 | /// Write a shortest path instance (graph+capacity on the |
---|
326 | /// edges+designated source) to the output. |
---|
327 | /// \param os The output stream. |
---|
328 | /// \param g The graph. |
---|
329 | /// \param capacity The capacity map. |
---|
330 | /// \param s The source node. |
---|
331 | template<typename Graph, typename CapacityMap> |
---|
332 | void writeGraph(std::ostream& os, const Graph &g, |
---|
333 | const CapacityMap& capacity, const typename Graph::Node &s) { |
---|
334 | GraphWriter<Graph> writer(os, g); |
---|
335 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
336 | writer.writeNodeMap("id", nodeIdMap); |
---|
337 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
338 | writer.writeEdgeMap("id", edgeIdMap); |
---|
339 | writer.writeEdgeMap("capacity", capacity); |
---|
340 | writer.writeNode("source", s); |
---|
341 | writer.run(); |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | /// \brief Write a max flow instance to the output. |
---|
346 | /// |
---|
347 | /// Write a max flow instance (graph+capacity on the |
---|
348 | /// edges+designated source and target) to the output. |
---|
349 | /// |
---|
350 | /// \param os The output stream. |
---|
351 | /// \param g The graph. |
---|
352 | /// \param capacity The capacity map. |
---|
353 | /// \param s The source node. |
---|
354 | /// \param t The target node. |
---|
355 | template<typename Graph, typename CapacityMap> |
---|
356 | void writeGraph(std::ostream& os, const Graph &g, |
---|
357 | const CapacityMap& capacity, const typename Graph::Node &s, |
---|
358 | const typename Graph::Node &t) { |
---|
359 | GraphWriter<Graph> writer(os, g); |
---|
360 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
361 | writer.writeNodeMap("id", nodeIdMap); |
---|
362 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
363 | writer.writeEdgeMap("id", edgeIdMap); |
---|
364 | writer.writeEdgeMap("capacity", capacity); |
---|
365 | writer.writeNode("source", s); |
---|
366 | writer.writeNode("target", t); |
---|
367 | writer.run(); |
---|
368 | } |
---|
369 | |
---|
370 | /// \brief Write a min cost flow instance to the output. |
---|
371 | /// |
---|
372 | /// Write a min cost flow instance (graph+capacity on the edges+cost |
---|
373 | /// function on the edges+designated source and target) to the output. |
---|
374 | /// |
---|
375 | /// \param os The output stream. |
---|
376 | /// \param g The graph. |
---|
377 | /// \param capacity The capacity map. |
---|
378 | /// \param s The source node. |
---|
379 | /// \param t The target node. |
---|
380 | /// \param cost The cost map. |
---|
381 | template<typename Graph, typename CapacityMap, typename CostMap> |
---|
382 | void writeGraph(std::ostream& os, const Graph &g, |
---|
383 | const CapacityMap& capacity, const typename Graph::Node &s, |
---|
384 | const typename Graph::Node &t, const CostMap& cost) { |
---|
385 | GraphWriter<Graph> writer(os, g); |
---|
386 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
387 | writer.writeNodeMap("id", nodeIdMap); |
---|
388 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
389 | writer.writeEdgeMap("id", edgeIdMap); |
---|
390 | writer.writeEdgeMap("capacity", capacity); |
---|
391 | writer.writeEdgeMap("cost", cost); |
---|
392 | writer.writeNode("source", s); |
---|
393 | writer.writeNode("target", t); |
---|
394 | writer.run(); |
---|
395 | } |
---|
396 | |
---|
397 | /// \brief The undirected graph writer class. |
---|
398 | /// |
---|
399 | /// The \c UndirGraphWriter class provides the undir graph output. To write |
---|
400 | /// a graph you should first give writing commands to the writer. You can |
---|
401 | /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap |
---|
402 | /// writing and labeled Node, Edge or UndirEdge writing. |
---|
403 | /// |
---|
404 | /// \code |
---|
405 | /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph); |
---|
406 | /// \endcode |
---|
407 | /// |
---|
408 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
409 | /// command in the \c UndirGraphWriter. You should give as parameter |
---|
410 | /// the name of the map and the map object. The NodeMap writing |
---|
411 | /// command with name "id" should write a unique map because it |
---|
412 | /// is regarded as ID map. |
---|
413 | /// |
---|
414 | /// \code |
---|
415 | /// IdMap<UndirListGraph, Node> nodeIdMap; |
---|
416 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
417 | /// |
---|
418 | /// writer.writeNodeMap("coords", coords); |
---|
419 | /// writer.writeNodeMap("color", colorMap); |
---|
420 | /// \endcode |
---|
421 | /// |
---|
422 | /// With the \c writeUndirEdgeMap() member function you can give an |
---|
423 | /// undirected edge map writing command similar to the NodeMaps. |
---|
424 | /// |
---|
425 | /// \code |
---|
426 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
427 | /// edgeDescMap(graph); |
---|
428 | /// writer.writeUndirEdgeMap("descriptor", edgeDescMap); |
---|
429 | /// |
---|
430 | /// writer.writeUndirEdgeMap("weight", weightMap); |
---|
431 | /// writer.writeUndirEdgeMap("label", labelMap); |
---|
432 | /// \endcode |
---|
433 | /// |
---|
434 | /// The EdgeMap handling is just a syntactical sugar. It writes |
---|
435 | /// two undirected edge map with '+' and '-' prefix in the name. |
---|
436 | /// |
---|
437 | /// \code |
---|
438 | /// writer.writeEdgeMap("capacity", capacityMap); |
---|
439 | /// \endcode |
---|
440 | /// |
---|
441 | /// |
---|
442 | /// With \c writeNode() and \c writeUndirEdge() functions you can |
---|
443 | /// designate nodes and undirected edges in the graph. For example, you can |
---|
444 | /// write out the source and target of the graph. |
---|
445 | /// |
---|
446 | /// \code |
---|
447 | /// writer.writeNode("source", sourceNode); |
---|
448 | /// writer.writeNode("target", targetNode); |
---|
449 | /// |
---|
450 | /// writer.writeUndirEdge("observed", undirEdge); |
---|
451 | /// \endcode |
---|
452 | /// |
---|
453 | /// After you give all write commands you must call the \c run() member |
---|
454 | /// function, which executes all the writing commands. |
---|
455 | /// |
---|
456 | /// \code |
---|
457 | /// writer.run(); |
---|
458 | /// \endcode |
---|
459 | /// |
---|
460 | /// \see DefaultWriterTraits |
---|
461 | /// \see QuotedStringWriter |
---|
462 | /// \see IdMap |
---|
463 | /// \see DescriptorMap |
---|
464 | /// \see \ref GraphWriter |
---|
465 | /// \see \ref graph-io-page |
---|
466 | /// \author Balazs Dezso |
---|
467 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
468 | class UndirGraphWriter { |
---|
469 | public: |
---|
470 | |
---|
471 | typedef _Graph Graph; |
---|
472 | typedef typename Graph::Node Node; |
---|
473 | typedef typename Graph::Edge Edge; |
---|
474 | typedef typename Graph::UndirEdge UndirEdge; |
---|
475 | |
---|
476 | typedef _WriterTraits WriterTraits; |
---|
477 | |
---|
478 | /// \brief Construct a new UndirGraphWriter. |
---|
479 | /// |
---|
480 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
481 | /// to the given stream. |
---|
482 | UndirGraphWriter(std::ostream& _os, const Graph& _graph) |
---|
483 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
484 | nodeset_writer(*writer, _graph, std::string()), |
---|
485 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
486 | node_writer(*writer, nodeset_writer, std::string()), |
---|
487 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
488 | attribute_writer(*writer, std::string()) {} |
---|
489 | |
---|
490 | /// \brief Construct a new UndirGraphWriter. |
---|
491 | /// |
---|
492 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
493 | /// to the given file. |
---|
494 | UndirGraphWriter(const std::string& _filename, const Graph& _graph) |
---|
495 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
496 | nodeset_writer(*writer, _graph, std::string()), |
---|
497 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
498 | node_writer(*writer, nodeset_writer, std::string()), |
---|
499 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
500 | attribute_writer(*writer, std::string()) {} |
---|
501 | |
---|
502 | /// \brief Construct a new UndirGraphWriter. |
---|
503 | /// |
---|
504 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
505 | /// to given LemonReader. |
---|
506 | UndirGraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
507 | : writer(_writer), own_writer(false), |
---|
508 | nodeset_writer(*writer, _graph, std::string()), |
---|
509 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
510 | node_writer(*writer, nodeset_writer, std::string()), |
---|
511 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
512 | attribute_writer(*writer, std::string()) {} |
---|
513 | |
---|
514 | /// \brief Destruct the graph writer. |
---|
515 | /// |
---|
516 | /// Destruct the graph writer. |
---|
517 | ~UndirGraphWriter() { |
---|
518 | if (own_writer) |
---|
519 | delete writer; |
---|
520 | } |
---|
521 | |
---|
522 | /// \brief Issue a new node map writing command to the writer. |
---|
523 | /// |
---|
524 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
525 | template <typename Map> |
---|
526 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
527 | nodeset_writer.writeNodeMap(name, map); |
---|
528 | return *this; |
---|
529 | } |
---|
530 | |
---|
531 | /// \brief Issue a new node map writing command to the writer. |
---|
532 | /// |
---|
533 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
534 | template <typename Writer, typename Map> |
---|
535 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
536 | const Writer& writer = Writer()) { |
---|
537 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
538 | return *this; |
---|
539 | } |
---|
540 | |
---|
541 | /// \brief Issue a new edge map writing command to the writer. |
---|
542 | /// |
---|
543 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
544 | template <typename Map> |
---|
545 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
546 | undir_edgeset_writer.writeEdgeMap(name, map); |
---|
547 | return *this; |
---|
548 | } |
---|
549 | |
---|
550 | /// \brief Issue a new edge map writing command to the writer. |
---|
551 | /// |
---|
552 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
553 | template <typename Writer, typename Map> |
---|
554 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
555 | const Writer& writer = Writer()) { |
---|
556 | undir_edgeset_writer.writeEdgeMap(name, map, writer); |
---|
557 | return *this; |
---|
558 | } |
---|
559 | |
---|
560 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
561 | /// |
---|
562 | /// This function issues a new <i> undirected edge map writing |
---|
563 | /// command</i> to the writer. |
---|
564 | template <typename Map> |
---|
565 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) { |
---|
566 | undir_edgeset_writer.writeUndirEdgeMap(name, map); |
---|
567 | return *this; |
---|
568 | } |
---|
569 | |
---|
570 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
571 | /// |
---|
572 | /// This function issues a new <i> undirected edge map writing |
---|
573 | /// command</i> to the writer. |
---|
574 | template <typename Writer, typename Map> |
---|
575 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map, |
---|
576 | const Writer& writer = Writer()) { |
---|
577 | undir_edgeset_writer.writeUndirEdgeMap(name, map, writer); |
---|
578 | return *this; |
---|
579 | } |
---|
580 | |
---|
581 | /// \brief Issue a new labeled node writer to the writer. |
---|
582 | /// |
---|
583 | /// This function issues a new <i> labeled node writing |
---|
584 | /// command</i> to the writer. |
---|
585 | UndirGraphWriter& writeNode(std::string name, const Node& node) { |
---|
586 | node_writer.writeNode(name, node); |
---|
587 | return *this; |
---|
588 | } |
---|
589 | |
---|
590 | /// \brief Issue a new labeled edge writer to the writer. |
---|
591 | /// |
---|
592 | /// This function issues a new <i> labeled edge writing |
---|
593 | /// command</i> to the writer. |
---|
594 | UndirGraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
595 | undir_edge_writer.writeEdge(name, edge); |
---|
596 | } |
---|
597 | |
---|
598 | /// \brief Issue a new labeled undirected edge writing command to |
---|
599 | /// the writer. |
---|
600 | /// |
---|
601 | /// Issue a new <i>labeled undirected edge writing command</i> to |
---|
602 | /// the writer. |
---|
603 | UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) { |
---|
604 | undir_edge_writer.writeUndirEdge(name, edge); |
---|
605 | } |
---|
606 | |
---|
607 | /// \brief Issue a new attribute writing command. |
---|
608 | /// |
---|
609 | /// This function issues a new <i> attribute writing |
---|
610 | /// command</i> to the writer. |
---|
611 | template <typename Value> |
---|
612 | UndirGraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
613 | attribute_writer.writeAttribute(name, value); |
---|
614 | return *this; |
---|
615 | } |
---|
616 | |
---|
617 | /// \brief Issue a new attribute writing command. |
---|
618 | /// |
---|
619 | /// This function issues a new <i> attribute writing |
---|
620 | /// command</i> to the writer. |
---|
621 | template <typename Writer, typename Value> |
---|
622 | UndirGraphWriter& writeAttribute(std::string name, const Value& value, |
---|
623 | const Writer& writer) { |
---|
624 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
625 | return *this; |
---|
626 | } |
---|
627 | |
---|
628 | /// \brief Conversion operator to LemonWriter. |
---|
629 | /// |
---|
630 | /// Conversion operator to LemonWriter. It makes possible |
---|
631 | /// to access the encapsulated \e LemonWriter, this way |
---|
632 | /// you can attach to this writer new instances of |
---|
633 | /// \e LemonWriter::SectionWriter. |
---|
634 | operator LemonWriter&() { |
---|
635 | return *writer; |
---|
636 | } |
---|
637 | |
---|
638 | /// \brief Executes the writing commands. |
---|
639 | /// |
---|
640 | /// Executes the writing commands. |
---|
641 | void run() { |
---|
642 | writer->run(); |
---|
643 | } |
---|
644 | |
---|
645 | /// \brief Write the id of the given node. |
---|
646 | /// |
---|
647 | /// It writes the id of the given node. If there was written an "id" |
---|
648 | /// named node map then it will write the map value belonging to the node. |
---|
649 | void writeId(std::ostream& os, const Node& item) const { |
---|
650 | nodeset_writer.writeId(os, item); |
---|
651 | } |
---|
652 | |
---|
653 | /// \brief Write the id of the given edge. |
---|
654 | /// |
---|
655 | /// It writes the id of the given edge. If there was written an "id" |
---|
656 | /// named edge map then it will write the map value belonging to the edge. |
---|
657 | void writeId(std::ostream& os, const Edge& item) const { |
---|
658 | undir_edgeset_writer.writeId(os, item); |
---|
659 | } |
---|
660 | |
---|
661 | /// \brief Write the id of the given undirected edge. |
---|
662 | /// |
---|
663 | /// It writes the id of the given undirected edge. If there was written |
---|
664 | /// an "id" named edge map then it will write the map value belonging to |
---|
665 | /// the edge. |
---|
666 | void writeId(std::ostream& os, const UndirEdge& item) const { |
---|
667 | undir_edgeset_writer.writeId(os, item); |
---|
668 | } |
---|
669 | |
---|
670 | |
---|
671 | private: |
---|
672 | |
---|
673 | LemonWriter* writer; |
---|
674 | bool own_writer; |
---|
675 | |
---|
676 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
677 | UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer; |
---|
678 | |
---|
679 | NodeWriter<Graph> node_writer; |
---|
680 | UndirEdgeWriter<Graph> undir_edge_writer; |
---|
681 | |
---|
682 | AttributeWriter<WriterTraits> attribute_writer; |
---|
683 | }; |
---|
684 | |
---|
685 | /// \brief Write an undirected graph to the output. |
---|
686 | /// |
---|
687 | /// Write an undirected graph to the output. |
---|
688 | /// \param os The output stream. |
---|
689 | /// \param g The graph. |
---|
690 | template<typename Graph> |
---|
691 | void writeUndirGraph(std::ostream& os, const Graph &g) { |
---|
692 | UndirGraphWriter<Graph> writer(os, g); |
---|
693 | writer.run(); |
---|
694 | } |
---|
695 | |
---|
696 | /// \brief Write an undirected multigraph (undirected graph + capacity |
---|
697 | /// map on the edges) to the output. |
---|
698 | /// |
---|
699 | /// Write an undirected multigraph (undirected graph + capacity |
---|
700 | /// map on the edges) to the output. |
---|
701 | /// \param os The output stream. |
---|
702 | /// \param g The graph. |
---|
703 | /// \param capacity The capacity undirected map. |
---|
704 | template<typename Graph, typename CapacityMap> |
---|
705 | void writeUndirGraph(std::ostream& os, const Graph &g, |
---|
706 | const CapacityMap& capacity) { |
---|
707 | UndirGraphWriter<Graph> writer(os, g); |
---|
708 | writer.writeUndirEdgeMap("capacity", capacity); |
---|
709 | writer.run(); |
---|
710 | } |
---|
711 | |
---|
712 | |
---|
713 | /// @} |
---|
714 | |
---|
715 | } |
---|
716 | |
---|
717 | #endif |
---|