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