deba@1137
|
1 |
/* -*- C++ -*-
|
deba@1137
|
2 |
* src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
|
deba@1137
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 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.
|
deba@1137
|
20 |
|
deba@1214
|
21 |
#ifndef LEMON_GRAPH_WRITER_H
|
deba@1214
|
22 |
#define LEMON_GRAPH_WRITER_H
|
deba@1137
|
23 |
|
deba@1137
|
24 |
#include <iostream>
|
deba@1137
|
25 |
#include <sstream>
|
deba@1137
|
26 |
|
deba@1137
|
27 |
#include <map>
|
deba@1137
|
28 |
#include <vector>
|
deba@1137
|
29 |
|
deba@1137
|
30 |
#include <memory>
|
deba@1137
|
31 |
|
deba@1208
|
32 |
#include <lemon/map_utils.h>
|
deba@1214
|
33 |
|
deba@1137
|
34 |
#include <lemon/invalid.h>
|
deba@1137
|
35 |
#include <lemon/error.h>
|
deba@1137
|
36 |
|
deba@1137
|
37 |
|
deba@1137
|
38 |
namespace lemon {
|
deba@1137
|
39 |
|
deba@1333
|
40 |
/// \addtogroup io_group
|
deba@1333
|
41 |
/// @{
|
deba@1333
|
42 |
|
deba@1137
|
43 |
/// \brief Standard WriterTraits for the GraphWriter class.
|
deba@1137
|
44 |
///
|
deba@1137
|
45 |
/// Standard WriterTraits for the GraphWriter class.
|
deba@1137
|
46 |
/// It defines standard writing method for all type of value.
|
deba@1333
|
47 |
/// \author Balazs Dezso
|
deba@1137
|
48 |
struct DefaultWriterTraits {
|
deba@1137
|
49 |
|
deba@1137
|
50 |
/// \brief Template class for writing an value.
|
deba@1137
|
51 |
///
|
deba@1137
|
52 |
/// Template class for writing an value.
|
deba@1333
|
53 |
/// \author Balazs Dezso
|
deba@1137
|
54 |
template <typename _Value>
|
deba@1137
|
55 |
struct Writer {
|
deba@1137
|
56 |
/// The value type.
|
deba@1137
|
57 |
typedef _Value Value;
|
deba@1137
|
58 |
|
deba@1188
|
59 |
/// \brief Writes a value to the given stream.
|
deba@1137
|
60 |
///
|
deba@1188
|
61 |
/// Writes a value to the given stream.
|
deba@1137
|
62 |
void write(std::ostream& os, const Value& value) {
|
deba@1137
|
63 |
os << value << '\t';
|
deba@1137
|
64 |
}
|
deba@1137
|
65 |
};
|
deba@1137
|
66 |
|
deba@1333
|
67 |
/// \brief Returns wheter this name is an ID map name.
|
deba@1333
|
68 |
///
|
deba@1333
|
69 |
/// Returns wheter this name is an ID map name.
|
deba@1333
|
70 |
static bool idMapName(const std::string& name) {
|
deba@1333
|
71 |
return name == "id";
|
deba@1333
|
72 |
}
|
deba@1333
|
73 |
|
deba@1137
|
74 |
};
|
deba@1137
|
75 |
|
deba@1137
|
76 |
|
deba@1137
|
77 |
/// \brief Writer class for quoted strings.
|
deba@1137
|
78 |
///
|
deba@1137
|
79 |
/// Writer class for quoted strings. It can process the escape
|
deba@1137
|
80 |
/// sequences in the string.
|
deba@1333
|
81 |
/// \author Balazs Dezso
|
deba@1137
|
82 |
class QuotedStringWriter {
|
deba@1137
|
83 |
public:
|
deba@1137
|
84 |
typedef std::string Value;
|
deba@1137
|
85 |
|
deba@1137
|
86 |
/// \brief Constructor for the writer.
|
deba@1137
|
87 |
///
|
deba@1137
|
88 |
/// Constructor for the writer. If the given parameter is true
|
deba@1137
|
89 |
/// the writer creates escape sequences from special characters.
|
deba@1137
|
90 |
QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
|
deba@1137
|
91 |
|
deba@1188
|
92 |
/// \brief Writes a quoted string to the given stream.
|
deba@1137
|
93 |
///
|
deba@1188
|
94 |
/// Writes a quoted string to the given stream.
|
deba@1137
|
95 |
void write(std::ostream& os, const std::string& value) {
|
deba@1137
|
96 |
os << "\"";
|
deba@1137
|
97 |
if (escaped) {
|
deba@1311
|
98 |
std::ostringstream ls;
|
deba@1137
|
99 |
for (int i = 0; i < (int)value.size(); ++i) {
|
deba@1137
|
100 |
writeEscape(ls, value[i]);
|
deba@1137
|
101 |
}
|
deba@1137
|
102 |
os << ls.str();
|
deba@1137
|
103 |
} else {
|
deba@1137
|
104 |
os << value;
|
deba@1137
|
105 |
}
|
deba@1137
|
106 |
os << "\"";
|
deba@1137
|
107 |
}
|
deba@1137
|
108 |
|
deba@1137
|
109 |
private:
|
deba@1137
|
110 |
|
deba@1137
|
111 |
static void writeEscape(std::ostream& os, char c) {
|
deba@1137
|
112 |
switch (c) {
|
deba@1137
|
113 |
case '\\':
|
deba@1137
|
114 |
os << "\\\\";
|
deba@1137
|
115 |
return;
|
deba@1137
|
116 |
case '\"':
|
deba@1137
|
117 |
os << "\\\"";
|
deba@1137
|
118 |
return;
|
deba@1137
|
119 |
case '\'':
|
deba@1137
|
120 |
os << "\\\'";
|
deba@1137
|
121 |
return;
|
deba@1137
|
122 |
case '\?':
|
deba@1137
|
123 |
os << "\\\?";
|
deba@1137
|
124 |
return;
|
deba@1137
|
125 |
case '\a':
|
deba@1137
|
126 |
os << "\\a";
|
deba@1137
|
127 |
return;
|
deba@1137
|
128 |
case '\b':
|
deba@1137
|
129 |
os << "\\b";
|
deba@1137
|
130 |
return;
|
deba@1137
|
131 |
case '\f':
|
deba@1137
|
132 |
os << "\\f";
|
deba@1137
|
133 |
return;
|
deba@1137
|
134 |
case '\r':
|
deba@1137
|
135 |
os << "\\r";
|
deba@1137
|
136 |
return;
|
deba@1137
|
137 |
case '\n':
|
deba@1137
|
138 |
os << "\\n";
|
deba@1137
|
139 |
return;
|
deba@1137
|
140 |
case '\t':
|
deba@1137
|
141 |
os << "\\t";
|
deba@1137
|
142 |
return;
|
deba@1137
|
143 |
case '\v':
|
deba@1137
|
144 |
os << "\\v";
|
deba@1137
|
145 |
return;
|
deba@1137
|
146 |
default:
|
deba@1137
|
147 |
if (c < 0x20) {
|
deba@1311
|
148 |
os << '\\' << std::oct << (int)c;
|
deba@1137
|
149 |
} else {
|
deba@1137
|
150 |
os << c;
|
deba@1137
|
151 |
}
|
deba@1137
|
152 |
return;
|
deba@1137
|
153 |
}
|
deba@1137
|
154 |
}
|
deba@1137
|
155 |
private:
|
deba@1137
|
156 |
bool escaped;
|
deba@1137
|
157 |
};
|
deba@1137
|
158 |
|
deba@1396
|
159 |
class GUIWriter {
|
deba@1396
|
160 |
public:
|
deba@1396
|
161 |
virtual void write(std::ostream& os) = 0;
|
deba@1396
|
162 |
};
|
deba@1396
|
163 |
|
deba@1137
|
164 |
|
deba@1137
|
165 |
/// \brief The graph writer class.
|
deba@1137
|
166 |
///
|
deba@1333
|
167 |
/// The \c GraphWriter class provides the graph output. To write a graph
|
deba@1333
|
168 |
/// you should first give writing commands for the writer. You can declare
|
deba@1333
|
169 |
/// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
|
deba@1333
|
170 |
/// Edge writing.
|
deba@1333
|
171 |
///
|
deba@1333
|
172 |
/// \code
|
deba@1333
|
173 |
/// GraphWriter<ListGraph> writer(std::cout, graph);
|
deba@1333
|
174 |
/// \endcode
|
deba@1333
|
175 |
///
|
deba@1394
|
176 |
/// The \c writeNodeMap() function declares a \c NodeMap writing
|
deba@1394
|
177 |
/// command in the \c GraphWriter. You should give as parameter
|
deba@1394
|
178 |
/// the name of the map and the map object. The NodeMap writing
|
deba@1394
|
179 |
/// command with name "id" should write a unique map because it
|
deba@1394
|
180 |
/// is regarded as ID map.
|
deba@1333
|
181 |
///
|
deba@1333
|
182 |
/// \code
|
deba@1333
|
183 |
/// IdMap<ListGraph, Node> nodeIdMap;
|
deba@1394
|
184 |
/// writer.writeNodeMap("id", nodeIdMap);
|
deba@1333
|
185 |
///
|
deba@1394
|
186 |
/// writer.writeNodeMap("x-coord", xCoordMap);
|
deba@1394
|
187 |
/// writer.writeNodeMap("y-coord", yCoordMap);
|
deba@1394
|
188 |
/// writer.writeNodeMap("color", colorMap);
|
deba@1333
|
189 |
/// \endcode
|
deba@1333
|
190 |
///
|
deba@1394
|
191 |
/// With the \c writeEdgeMap() member function you can give an edge map
|
deba@1333
|
192 |
/// writing command similar to the NodeMaps.
|
deba@1333
|
193 |
///
|
deba@1333
|
194 |
/// \code
|
deba@1333
|
195 |
/// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
|
deba@1333
|
196 |
/// edgeDescMap(graph);
|
deba@1394
|
197 |
/// writer.writeEdgeMap("descriptor", edgeDescMap);
|
deba@1333
|
198 |
///
|
deba@1394
|
199 |
/// writer.writeEdgeMap("weight", weightMap);
|
deba@1394
|
200 |
/// writer.writeEdgeMap("label", labelMap);
|
deba@1333
|
201 |
/// \endcode
|
deba@1333
|
202 |
///
|
deba@1394
|
203 |
/// With \c writeNode() and \c writeEdge() functions you can
|
deba@1394
|
204 |
/// point out Nodes and Edges in the graph. By example, you can
|
deba@1394
|
205 |
/// write out the source and target of the graph.
|
deba@1333
|
206 |
///
|
deba@1333
|
207 |
/// \code
|
deba@1394
|
208 |
/// writer.writeNode("source", sourceNode);
|
deba@1394
|
209 |
/// writer.writeNode("target", targetNode);
|
deba@1333
|
210 |
///
|
deba@1394
|
211 |
/// writer.writeEdge("observed", edge);
|
deba@1333
|
212 |
/// \endcode
|
deba@1333
|
213 |
///
|
deba@1333
|
214 |
/// After you give all write commands you must call the \c run() member
|
deba@1333
|
215 |
/// function, which execute all the writer commands.
|
deba@1333
|
216 |
///
|
deba@1333
|
217 |
/// \code
|
deba@1333
|
218 |
/// writer.run();
|
deba@1333
|
219 |
/// \endcode
|
deba@1333
|
220 |
///
|
alpar@1287
|
221 |
/// \see DefaultWriterTraits
|
alpar@1287
|
222 |
/// \see QuotedStringWriter
|
deba@1333
|
223 |
/// \see IdMap
|
deba@1333
|
224 |
/// \see DescriptorMap
|
deba@1394
|
225 |
/// \see \ref GraphWriter
|
alpar@1138
|
226 |
/// \see \ref graph-io-page
|
deba@1333
|
227 |
/// \author Balazs Dezso
|
deba@1137
|
228 |
template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
|
deba@1137
|
229 |
class GraphWriter {
|
deba@1137
|
230 |
public:
|
deba@1137
|
231 |
|
deba@1137
|
232 |
typedef _Graph Graph;
|
deba@1137
|
233 |
typedef typename Graph::Node Node;
|
deba@1137
|
234 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1137
|
235 |
typedef typename Graph::Edge Edge;
|
deba@1137
|
236 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@1137
|
237 |
|
deba@1137
|
238 |
typedef _WriterTraits WriterTraits;
|
deba@1137
|
239 |
|
deba@1137
|
240 |
/// \brief Construct a new GraphWriter.
|
deba@1137
|
241 |
///
|
deba@1137
|
242 |
/// Construct a new GraphWriter. It writes from the given map,
|
deba@1137
|
243 |
/// it constructs the given map and it use the given writer as the
|
deba@1137
|
244 |
/// default skipper.
|
deba@1208
|
245 |
GraphWriter(std::ostream& _os, const Graph& _graph)
|
deba@1396
|
246 |
: gui_writer(0), os(_os), graph(_graph){}
|
deba@1137
|
247 |
|
deba@1137
|
248 |
|
deba@1137
|
249 |
/// \brief Destruct the graph writer.
|
deba@1137
|
250 |
///
|
deba@1137
|
251 |
/// Destruct the graph writer.
|
deba@1137
|
252 |
~GraphWriter() {
|
deba@1137
|
253 |
for (typename NodeMapWriters::iterator it = node_map_writers.begin();
|
deba@1137
|
254 |
it != node_map_writers.end(); ++it) {
|
deba@1137
|
255 |
delete it->second;
|
deba@1137
|
256 |
}
|
deba@1137
|
257 |
|
deba@1137
|
258 |
for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
|
deba@1137
|
259 |
it != edge_map_writers.end(); ++it) {
|
deba@1137
|
260 |
delete it->second;
|
deba@1137
|
261 |
}
|
deba@1137
|
262 |
|
deba@1137
|
263 |
}
|
deba@1137
|
264 |
|
deba@1137
|
265 |
// Node map rules
|
deba@1137
|
266 |
|
deba@1137
|
267 |
/// \brief Add a new node map writer command for the writer.
|
deba@1137
|
268 |
///
|
deba@1137
|
269 |
/// Add a new node map writer command for the writer.
|
deba@1137
|
270 |
template <typename Map>
|
deba@1394
|
271 |
GraphWriter& writeNodeMap(std::string name, const Map& map) {
|
deba@1394
|
272 |
return writeNodeMap<typename WriterTraits::template Writer<
|
deba@1137
|
273 |
typename Map::Value>, Map>(name, map);
|
deba@1137
|
274 |
}
|
deba@1137
|
275 |
|
deba@1137
|
276 |
/// \brief Add a new node map writer command for the writer.
|
deba@1137
|
277 |
///
|
deba@1137
|
278 |
/// Add a new node map writer command for the writer.
|
deba@1137
|
279 |
template <typename Writer, typename Map>
|
deba@1394
|
280 |
GraphWriter& writeNodeMap(std::string name, const Map& map,
|
deba@1137
|
281 |
const Writer& writer = Writer()) {
|
deba@1137
|
282 |
node_map_writers.push_back(
|
deba@1137
|
283 |
make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
|
deba@1137
|
284 |
return *this;
|
deba@1137
|
285 |
}
|
deba@1137
|
286 |
|
deba@1137
|
287 |
// Edge map rules
|
deba@1137
|
288 |
|
deba@1137
|
289 |
/// \brief Add a new edge map writer command for the writer.
|
deba@1137
|
290 |
///
|
deba@1137
|
291 |
/// Add a new edge map writer command for the writer.
|
deba@1137
|
292 |
template <typename Map>
|
deba@1394
|
293 |
GraphWriter& writeEdgeMap(std::string name, const Map& map) {
|
deba@1394
|
294 |
return writeEdgeMap<typename WriterTraits::template Writer<
|
deba@1137
|
295 |
typename Map::Value>, Map>(name, map);
|
deba@1137
|
296 |
}
|
deba@1137
|
297 |
|
deba@1137
|
298 |
|
deba@1137
|
299 |
/// \brief Add a new edge map writer command for the writer.
|
deba@1137
|
300 |
///
|
deba@1137
|
301 |
/// Add a new edge map writer command for the writer.
|
deba@1137
|
302 |
template <typename Writer, typename Map>
|
deba@1394
|
303 |
GraphWriter& writeEdgeMap(std::string name,
|
deba@1137
|
304 |
const Map& map, const Writer& writer = Writer()) {
|
deba@1137
|
305 |
edge_map_writers.push_back(make_pair(name,
|
deba@1137
|
306 |
new MapWriter<Edge, Map, Writer>(map, writer)));
|
deba@1137
|
307 |
return *this;
|
deba@1137
|
308 |
}
|
deba@1137
|
309 |
|
deba@1137
|
310 |
/// \brief Add a new labeled node writer for the writer.
|
deba@1137
|
311 |
///
|
deba@1137
|
312 |
/// Add a new labeled node writer for the writer.
|
deba@1394
|
313 |
GraphWriter& writeNode(std::string name, const Node& node) {
|
deba@1137
|
314 |
node_writers.push_back(make_pair(name, node));
|
deba@1137
|
315 |
return *this;
|
deba@1137
|
316 |
}
|
deba@1137
|
317 |
|
deba@1137
|
318 |
/// \brief Add a new labeled edge writer for the writer.
|
deba@1137
|
319 |
///
|
deba@1137
|
320 |
/// Add a new labeled edge writer for the writer.
|
deba@1394
|
321 |
GraphWriter& writeEdge(std::string name, const Edge& edge) {
|
deba@1137
|
322 |
edge_writers.push_back(make_pair(name, edge));
|
deba@1137
|
323 |
return *this;
|
deba@1137
|
324 |
}
|
deba@1137
|
325 |
|
deba@1396
|
326 |
GraphWriter& writeGUI(const GUIWriter& writer) {
|
deba@1396
|
327 |
gui_writer = &writer;
|
deba@1396
|
328 |
}
|
deba@1396
|
329 |
|
deba@1137
|
330 |
/// \brief Executes the writer commands.
|
deba@1137
|
331 |
///
|
deba@1137
|
332 |
/// Executes the writer commands.
|
deba@1137
|
333 |
void run() {
|
deba@1333
|
334 |
WriterBase<Node>* nodeWriter = 0;
|
deba@1333
|
335 |
WriterBase<Edge>* edgeWriter = 0;
|
deba@1333
|
336 |
writeNodeSet(nodeWriter);
|
deba@1333
|
337 |
writeEdgeSet(nodeWriter, edgeWriter);
|
deba@1333
|
338 |
writeNodes(nodeWriter);
|
deba@1333
|
339 |
writeEdges(edgeWriter);
|
deba@1396
|
340 |
writeGUI();
|
deba@1137
|
341 |
os << "@end" << std::endl;
|
deba@1137
|
342 |
}
|
deba@1137
|
343 |
|
deba@1137
|
344 |
private:
|
deba@1137
|
345 |
|
deba@1137
|
346 |
template <class _Item>
|
deba@1137
|
347 |
class WriterBase {
|
deba@1137
|
348 |
public:
|
deba@1137
|
349 |
typedef _Item Item;
|
deba@1137
|
350 |
virtual void write(std::ostream&, const Item&) = 0;
|
deba@1137
|
351 |
};
|
deba@1137
|
352 |
|
deba@1137
|
353 |
template <class _Item, typename _Map, typename _Writer>
|
deba@1137
|
354 |
class MapWriter : public WriterBase<_Item> {
|
deba@1137
|
355 |
public:
|
deba@1137
|
356 |
typedef _Map Map;
|
deba@1137
|
357 |
typedef _Writer Writer;
|
deba@1137
|
358 |
typedef typename Writer::Value Value;
|
deba@1137
|
359 |
typedef _Item Item;
|
deba@1137
|
360 |
|
deba@1137
|
361 |
const Map& map;
|
deba@1137
|
362 |
Writer writer;
|
deba@1137
|
363 |
|
deba@1137
|
364 |
MapWriter(const Map& _map, const Writer& _writer)
|
deba@1137
|
365 |
: map(_map), writer(_writer) {}
|
deba@1137
|
366 |
|
deba@1137
|
367 |
|
deba@1137
|
368 |
virtual void write(std::ostream& os, const Item& item) {
|
deba@1137
|
369 |
writer.write(os, map[item]);
|
deba@1137
|
370 |
}
|
deba@1137
|
371 |
|
deba@1137
|
372 |
};
|
deba@1137
|
373 |
|
deba@1333
|
374 |
void writeNodeSet(WriterBase<Node>* & nodeWriter) {
|
deba@1333
|
375 |
if (node_map_writers.size() == 0) return;
|
deba@1333
|
376 |
os << "@nodeset" << std::endl;
|
deba@1333
|
377 |
for (int i = 0; i < (int)node_map_writers.size(); ++i) {
|
deba@1333
|
378 |
const std::string& id = node_map_writers[i].first;
|
deba@1333
|
379 |
os << id << '\t';
|
deba@1333
|
380 |
if (WriterTraits::idMapName(id) && nodeWriter == 0) {
|
deba@1333
|
381 |
nodeWriter = node_map_writers[i].second;
|
deba@1333
|
382 |
}
|
deba@1333
|
383 |
}
|
deba@1333
|
384 |
os << std::endl;
|
deba@1333
|
385 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
deba@1333
|
386 |
for (int i = 0; i < (int)node_map_writers.size(); ++i) {
|
deba@1333
|
387 |
node_map_writers[i].second->write(os, it);
|
deba@1333
|
388 |
}
|
deba@1333
|
389 |
os << std::endl;
|
deba@1333
|
390 |
}
|
deba@1333
|
391 |
|
deba@1333
|
392 |
}
|
deba@1333
|
393 |
|
deba@1333
|
394 |
void writeEdgeSet(WriterBase<Node>* nodeWriter,
|
deba@1333
|
395 |
WriterBase<Edge>* & edgeWriter) {
|
deba@1343
|
396 |
if (edge_map_writers.size() == 0) return;
|
deba@1333
|
397 |
if (nodeWriter == 0) {
|
deba@1333
|
398 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
399 |
}
|
deba@1333
|
400 |
os << "@edgeset" << std::endl;
|
deba@1333
|
401 |
os << "\t\t";
|
deba@1333
|
402 |
for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
|
deba@1333
|
403 |
const std::string& id = edge_map_writers[i].first;
|
deba@1333
|
404 |
os << id << '\t';
|
deba@1333
|
405 |
if (WriterTraits::idMapName(id) && edgeWriter == 0) {
|
deba@1333
|
406 |
edgeWriter = edge_map_writers[i].second;
|
deba@1333
|
407 |
}
|
deba@1333
|
408 |
}
|
deba@1333
|
409 |
os << std::endl;
|
deba@1333
|
410 |
for (EdgeIt it(graph); it != INVALID; ++it) {
|
deba@1333
|
411 |
nodeWriter->write(os, graph.source(it));
|
deba@1333
|
412 |
nodeWriter->write(os, graph.target(it));
|
deba@1333
|
413 |
for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
|
deba@1333
|
414 |
edge_map_writers[i].second->write(os, it);
|
deba@1333
|
415 |
}
|
deba@1333
|
416 |
os << std::endl;
|
deba@1333
|
417 |
}
|
deba@1333
|
418 |
}
|
deba@1333
|
419 |
|
deba@1333
|
420 |
void writeNodes(WriterBase<Node>* nodeWriter) {
|
deba@1343
|
421 |
if (node_writers.size() == 0) return;
|
deba@1333
|
422 |
if (nodeWriter == 0) {
|
deba@1333
|
423 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
424 |
}
|
deba@1333
|
425 |
os << "@nodes" << std::endl;
|
deba@1333
|
426 |
for (int i = 0; i < (int)node_writers.size(); ++i) {
|
deba@1333
|
427 |
os << node_writers[i].first << '\t';
|
deba@1333
|
428 |
nodeWriter->write(os, node_writers[i].second);
|
deba@1333
|
429 |
os << std::endl;
|
deba@1333
|
430 |
}
|
deba@1333
|
431 |
}
|
deba@1333
|
432 |
|
deba@1333
|
433 |
void writeEdges(WriterBase<Edge>* edgeWriter) {
|
deba@1343
|
434 |
if (edge_writers.size() == 0) return;
|
deba@1333
|
435 |
if (edgeWriter == 0) {
|
deba@1333
|
436 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
437 |
}
|
deba@1333
|
438 |
os << "@edges" << std::endl;
|
deba@1333
|
439 |
for (int i = 0; i < (int)edge_writers.size(); ++i) {
|
deba@1333
|
440 |
os << edge_writers[i].first << '\t';
|
deba@1333
|
441 |
edgeWriter->write(os, edge_writers[i].second);
|
deba@1333
|
442 |
os << std::endl;
|
deba@1333
|
443 |
}
|
deba@1333
|
444 |
}
|
deba@1333
|
445 |
|
deba@1396
|
446 |
void writeGUI() {
|
deba@1396
|
447 |
if (gui_writer) {
|
deba@1396
|
448 |
os << "@gui" << std::endl;
|
deba@1396
|
449 |
gui_writer->write(os);
|
deba@1396
|
450 |
}
|
deba@1396
|
451 |
}
|
deba@1333
|
452 |
|
deba@1137
|
453 |
|
deba@1137
|
454 |
|
deba@1137
|
455 |
typedef std::vector< std::pair<std::string, WriterBase<Node>*> >
|
deba@1137
|
456 |
NodeMapWriters;
|
deba@1137
|
457 |
NodeMapWriters node_map_writers;
|
deba@1137
|
458 |
|
deba@1137
|
459 |
typedef std::vector< std::pair<std::string, WriterBase<Edge>*> >
|
deba@1137
|
460 |
EdgeMapWriters;
|
deba@1137
|
461 |
EdgeMapWriters edge_map_writers;
|
deba@1137
|
462 |
|
deba@1137
|
463 |
typedef std::vector<std::pair<std::string, Node> > NodeWriters;
|
deba@1137
|
464 |
NodeWriters node_writers;
|
deba@1137
|
465 |
|
deba@1137
|
466 |
typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
|
deba@1137
|
467 |
EdgeWriters edge_writers;
|
deba@1137
|
468 |
|
deba@1396
|
469 |
GUIWriter* gui_writer;
|
deba@1396
|
470 |
|
deba@1137
|
471 |
std::ostream& os;
|
deba@1208
|
472 |
const Graph& graph;
|
deba@1137
|
473 |
|
deba@1137
|
474 |
};
|
deba@1137
|
475 |
|
deba@1333
|
476 |
/// \brief Write a graph to the output.
|
deba@1333
|
477 |
///
|
deba@1333
|
478 |
/// Write a graph to the output.
|
deba@1333
|
479 |
/// \param os The output stream.
|
deba@1333
|
480 |
/// \param g The graph.
|
deba@1333
|
481 |
/// \param capacity The capacity map.
|
deba@1333
|
482 |
/// \param s The source node.
|
deba@1333
|
483 |
/// \param t The target node.
|
deba@1333
|
484 |
/// \param cost The cost map.
|
deba@1208
|
485 |
template<typename Graph, typename CapacityMap, typename CostMap>
|
deba@1208
|
486 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
487 |
const CapacityMap& capacity, const typename Graph::Node &s,
|
deba@1208
|
488 |
const typename Graph::Node &t, const CostMap& cost) {
|
deba@1394
|
489 |
GraphWriter<Graph> writer(os, g);
|
deba@1208
|
490 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1394
|
491 |
writer.writeNodeMap("id", nodeIdMap);
|
deba@1208
|
492 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1394
|
493 |
writer.writeEdgeMap("id", edgeIdMap);
|
deba@1394
|
494 |
writer.writeEdgeMap("capacity", capacity);
|
deba@1394
|
495 |
writer.writeEdgeMap("cost", cost);
|
deba@1394
|
496 |
writer.writeNode("source", s);
|
deba@1394
|
497 |
writer.writeNode("target", t);
|
deba@1394
|
498 |
writer.run();
|
deba@1208
|
499 |
}
|
deba@1208
|
500 |
|
deba@1333
|
501 |
/// \brief Write a graph to the output.
|
deba@1333
|
502 |
///
|
deba@1333
|
503 |
/// Write a graph to the output.
|
deba@1333
|
504 |
/// \param os The output stream.
|
deba@1333
|
505 |
/// \param g The graph.
|
deba@1333
|
506 |
/// \param capacity The capacity map.
|
deba@1333
|
507 |
/// \param s The source node.
|
deba@1333
|
508 |
/// \param t The target node.
|
deba@1297
|
509 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
510 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
511 |
const CapacityMap& capacity, const typename Graph::Node &s,
|
deba@1208
|
512 |
const typename Graph::Node &t) {
|
deba@1394
|
513 |
GraphWriter<Graph> writer(os, g);
|
deba@1208
|
514 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1394
|
515 |
writer.writeNodeMap("id", nodeIdMap);
|
deba@1208
|
516 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1394
|
517 |
writer.writeEdgeMap("id", edgeIdMap);
|
deba@1394
|
518 |
writer.writeEdgeMap("capacity", capacity);
|
deba@1394
|
519 |
writer.writeNode("source", s);
|
deba@1394
|
520 |
writer.writeNode("target", t);
|
deba@1394
|
521 |
writer.run();
|
deba@1208
|
522 |
}
|
deba@1208
|
523 |
|
deba@1333
|
524 |
/// \brief Write a graph to the output.
|
deba@1333
|
525 |
///
|
deba@1333
|
526 |
/// Write a graph to the output.
|
deba@1333
|
527 |
/// \param os The output stream.
|
deba@1333
|
528 |
/// \param g The graph.
|
deba@1333
|
529 |
/// \param capacity The capacity map.
|
deba@1333
|
530 |
/// \param s The source node.
|
deba@1208
|
531 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
532 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
533 |
const CapacityMap& capacity, const typename Graph::Node &s) {
|
deba@1394
|
534 |
GraphWriter<Graph> writer(os, g);
|
deba@1208
|
535 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1394
|
536 |
writer.writeNodeMap("id", nodeIdMap);
|
deba@1208
|
537 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1394
|
538 |
writer.writeEdgeMap("id", edgeIdMap);
|
deba@1394
|
539 |
writer.writeEdgeMap("capacity", capacity);
|
deba@1394
|
540 |
writer.writeNode("source", s);
|
deba@1394
|
541 |
writer.run();
|
deba@1208
|
542 |
}
|
deba@1333
|
543 |
|
deba@1333
|
544 |
/// \brief Write a graph to the output.
|
deba@1333
|
545 |
///
|
deba@1333
|
546 |
/// Write a graph to the output.
|
deba@1333
|
547 |
/// \param os The output stream.
|
deba@1333
|
548 |
/// \param g The graph.
|
deba@1333
|
549 |
/// \param capacity The capacity map.
|
deba@1208
|
550 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
551 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
552 |
const CapacityMap& capacity) {
|
deba@1394
|
553 |
GraphWriter<Graph> writer(os, g);
|
deba@1208
|
554 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1394
|
555 |
writer.writeNodeMap("id", nodeIdMap);
|
deba@1208
|
556 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1394
|
557 |
writer.writeEdgeMap("id", edgeIdMap);
|
deba@1394
|
558 |
writer.writeEdgeMap("capacity", capacity);
|
deba@1394
|
559 |
writer.run();
|
deba@1208
|
560 |
}
|
deba@1333
|
561 |
|
deba@1333
|
562 |
/// \brief Write a graph to the output.
|
deba@1333
|
563 |
///
|
deba@1333
|
564 |
/// Write a graph to the output.
|
deba@1333
|
565 |
/// \param os The output stream.
|
deba@1333
|
566 |
/// \param g The graph.
|
deba@1208
|
567 |
template<typename Graph>
|
deba@1208
|
568 |
void writeGraph(std::ostream& os, const Graph &g) {
|
deba@1394
|
569 |
GraphWriter<Graph> writer(os, g);
|
deba@1208
|
570 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1394
|
571 |
writer.writeNodeMap("id", nodeIdMap);
|
deba@1208
|
572 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1394
|
573 |
writer.writeEdgeMap("id", edgeIdMap);
|
deba@1394
|
574 |
writer.run();
|
deba@1208
|
575 |
}
|
deba@1208
|
576 |
|
deba@1333
|
577 |
/// @}
|
deba@1137
|
578 |
|
deba@1137
|
579 |
}
|
deba@1214
|
580 |
|
deba@1214
|
581 |
#endif
|