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