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
|
deba@1137
|
5 |
* (Egervary Combinatorial Optimization Research Group, 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@1137
|
159 |
|
deba@1137
|
160 |
/// \brief The graph writer class.
|
deba@1137
|
161 |
///
|
deba@1333
|
162 |
/// The \c GraphWriter class provides the graph output. To write a graph
|
deba@1333
|
163 |
/// you should first give writing commands for the writer. You can declare
|
deba@1333
|
164 |
/// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
|
deba@1333
|
165 |
/// Edge writing.
|
deba@1333
|
166 |
///
|
deba@1333
|
167 |
/// \code
|
deba@1333
|
168 |
/// GraphWriter<ListGraph> writer(std::cout, graph);
|
deba@1333
|
169 |
/// \endcode
|
deba@1333
|
170 |
///
|
deba@1333
|
171 |
/// The \c addNodeMap() function declares a \c NodeMap writing command in the
|
deba@1333
|
172 |
/// \c GraphWriter. You should give as parameter the name of the map and the
|
deba@1333
|
173 |
/// map object. The NodeMap writing command with name "id" should write a
|
deba@1333
|
174 |
/// unique map because it is regarded as ID map.
|
deba@1333
|
175 |
///
|
deba@1333
|
176 |
/// \code
|
deba@1333
|
177 |
/// IdMap<ListGraph, Node> nodeIdMap;
|
deba@1333
|
178 |
/// writer.addNodeMap("id", nodeIdMap);
|
deba@1333
|
179 |
///
|
deba@1333
|
180 |
/// writer.addNodeMap("x-coord", xCoordMap);
|
deba@1333
|
181 |
/// writer.addNodeMap("y-coord", yCoordMap);
|
deba@1333
|
182 |
/// writer.addNodeMap("color", colorMap);
|
deba@1333
|
183 |
/// \endcode
|
deba@1333
|
184 |
///
|
deba@1333
|
185 |
/// With the \c addEdgeMap() member function you can give an edge map
|
deba@1333
|
186 |
/// writing command similar to the NodeMaps.
|
deba@1333
|
187 |
///
|
deba@1333
|
188 |
/// \code
|
deba@1333
|
189 |
/// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
|
deba@1333
|
190 |
/// edgeDescMap(graph);
|
deba@1333
|
191 |
/// writer.addEdgeMap("descriptor", edgeDescMap);
|
deba@1333
|
192 |
///
|
deba@1333
|
193 |
/// writer.addEdgeMap("weight", weightMap);
|
deba@1333
|
194 |
/// writer.addEdgeMap("label", labelMap);
|
deba@1333
|
195 |
/// \endcode
|
deba@1333
|
196 |
///
|
deba@1333
|
197 |
/// With \c addNode() and \c addEdge() functions you can point out Nodes and
|
deba@1333
|
198 |
/// Edges in the graph. By example, you can write out the source and target
|
deba@1333
|
199 |
/// of the graph.
|
deba@1333
|
200 |
///
|
deba@1333
|
201 |
/// \code
|
deba@1333
|
202 |
/// writer.addNode("source", sourceNode);
|
deba@1333
|
203 |
/// writer.addNode("target", targetNode);
|
deba@1333
|
204 |
///
|
deba@1333
|
205 |
/// writer.addEdge("observed", edge);
|
deba@1333
|
206 |
/// \endcode
|
deba@1333
|
207 |
///
|
deba@1333
|
208 |
/// After you give all write commands you must call the \c run() member
|
deba@1333
|
209 |
/// function, which execute all the writer commands.
|
deba@1333
|
210 |
///
|
deba@1333
|
211 |
/// \code
|
deba@1333
|
212 |
/// writer.run();
|
deba@1333
|
213 |
/// \endcode
|
deba@1333
|
214 |
///
|
alpar@1287
|
215 |
/// \see DefaultWriterTraits
|
alpar@1287
|
216 |
/// \see QuotedStringWriter
|
deba@1333
|
217 |
/// \see IdMap
|
deba@1333
|
218 |
/// \see DescriptorMap
|
alpar@1138
|
219 |
/// \see \ref GraphReader
|
alpar@1138
|
220 |
/// \see \ref graph-io-page
|
deba@1333
|
221 |
/// \author Balazs Dezso
|
deba@1137
|
222 |
template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
|
deba@1137
|
223 |
class GraphWriter {
|
deba@1137
|
224 |
public:
|
deba@1137
|
225 |
|
deba@1137
|
226 |
typedef _Graph Graph;
|
deba@1137
|
227 |
typedef typename Graph::Node Node;
|
deba@1137
|
228 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1137
|
229 |
typedef typename Graph::Edge Edge;
|
deba@1137
|
230 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@1137
|
231 |
|
deba@1137
|
232 |
typedef _WriterTraits WriterTraits;
|
deba@1137
|
233 |
|
deba@1137
|
234 |
/// \brief Construct a new GraphWriter.
|
deba@1137
|
235 |
///
|
deba@1137
|
236 |
/// Construct a new GraphWriter. It writes from the given map,
|
deba@1137
|
237 |
/// it constructs the given map and it use the given writer as the
|
deba@1137
|
238 |
/// default skipper.
|
deba@1208
|
239 |
GraphWriter(std::ostream& _os, const Graph& _graph)
|
deba@1208
|
240 |
: os(_os), graph(_graph) {}
|
deba@1137
|
241 |
|
deba@1137
|
242 |
|
deba@1137
|
243 |
/// \brief Destruct the graph writer.
|
deba@1137
|
244 |
///
|
deba@1137
|
245 |
/// Destruct the graph writer.
|
deba@1137
|
246 |
~GraphWriter() {
|
deba@1137
|
247 |
for (typename NodeMapWriters::iterator it = node_map_writers.begin();
|
deba@1137
|
248 |
it != node_map_writers.end(); ++it) {
|
deba@1137
|
249 |
delete it->second;
|
deba@1137
|
250 |
}
|
deba@1137
|
251 |
|
deba@1137
|
252 |
for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
|
deba@1137
|
253 |
it != edge_map_writers.end(); ++it) {
|
deba@1137
|
254 |
delete it->second;
|
deba@1137
|
255 |
}
|
deba@1137
|
256 |
|
deba@1137
|
257 |
}
|
deba@1137
|
258 |
|
deba@1137
|
259 |
// Node map rules
|
deba@1137
|
260 |
|
deba@1137
|
261 |
/// \brief Add a new node map writer command for the writer.
|
deba@1137
|
262 |
///
|
deba@1137
|
263 |
/// Add a new node map writer command for the writer.
|
deba@1137
|
264 |
template <typename Map>
|
deba@1137
|
265 |
GraphWriter& addNodeMap(std::string name, const Map& map) {
|
deba@1137
|
266 |
return addNodeMap<typename WriterTraits::template Writer<
|
deba@1137
|
267 |
typename Map::Value>, Map>(name, map);
|
deba@1137
|
268 |
}
|
deba@1137
|
269 |
|
deba@1137
|
270 |
/// \brief Add a new node map writer command for the writer.
|
deba@1137
|
271 |
///
|
deba@1137
|
272 |
/// Add a new node map writer command for the writer.
|
deba@1137
|
273 |
template <typename Writer, typename Map>
|
deba@1137
|
274 |
GraphWriter& addNodeMap(std::string name, const Map& map,
|
deba@1137
|
275 |
const Writer& writer = Writer()) {
|
deba@1137
|
276 |
node_map_writers.push_back(
|
deba@1137
|
277 |
make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
|
deba@1137
|
278 |
return *this;
|
deba@1137
|
279 |
}
|
deba@1137
|
280 |
|
deba@1137
|
281 |
// Edge map rules
|
deba@1137
|
282 |
|
deba@1137
|
283 |
/// \brief Add a new edge map writer command for the writer.
|
deba@1137
|
284 |
///
|
deba@1137
|
285 |
/// Add a new edge map writer command for the writer.
|
deba@1137
|
286 |
template <typename Map>
|
deba@1137
|
287 |
GraphWriter& addEdgeMap(std::string name, const Map& map) {
|
deba@1137
|
288 |
return addEdgeMap<typename WriterTraits::template Writer<
|
deba@1137
|
289 |
typename Map::Value>, Map>(name, map);
|
deba@1137
|
290 |
}
|
deba@1137
|
291 |
|
deba@1137
|
292 |
|
deba@1137
|
293 |
/// \brief Add a new edge map writer command for the writer.
|
deba@1137
|
294 |
///
|
deba@1137
|
295 |
/// Add a new edge map writer command for the writer.
|
deba@1137
|
296 |
template <typename Writer, typename Map>
|
deba@1137
|
297 |
GraphWriter& addEdgeMap(std::string name,
|
deba@1137
|
298 |
const Map& map, const Writer& writer = Writer()) {
|
deba@1137
|
299 |
edge_map_writers.push_back(make_pair(name,
|
deba@1137
|
300 |
new MapWriter<Edge, Map, Writer>(map, writer)));
|
deba@1137
|
301 |
return *this;
|
deba@1137
|
302 |
}
|
deba@1137
|
303 |
|
deba@1137
|
304 |
/// \brief Add a new labeled node writer for the writer.
|
deba@1137
|
305 |
///
|
deba@1137
|
306 |
/// Add a new labeled node writer for the writer.
|
deba@1137
|
307 |
GraphWriter& addNode(std::string name, const Node& node) {
|
deba@1137
|
308 |
node_writers.push_back(make_pair(name, node));
|
deba@1137
|
309 |
return *this;
|
deba@1137
|
310 |
}
|
deba@1137
|
311 |
|
deba@1137
|
312 |
/// \brief Add a new labeled edge writer for the writer.
|
deba@1137
|
313 |
///
|
deba@1137
|
314 |
/// Add a new labeled edge writer for the writer.
|
deba@1137
|
315 |
GraphWriter& addEdge(std::string name, const Edge& edge) {
|
deba@1137
|
316 |
edge_writers.push_back(make_pair(name, edge));
|
deba@1137
|
317 |
return *this;
|
deba@1137
|
318 |
}
|
deba@1137
|
319 |
|
deba@1137
|
320 |
/// \brief Executes the writer commands.
|
deba@1137
|
321 |
///
|
deba@1137
|
322 |
/// Executes the writer commands.
|
deba@1137
|
323 |
void run() {
|
deba@1333
|
324 |
WriterBase<Node>* nodeWriter = 0;
|
deba@1333
|
325 |
WriterBase<Edge>* edgeWriter = 0;
|
deba@1333
|
326 |
writeNodeSet(nodeWriter);
|
deba@1333
|
327 |
writeEdgeSet(nodeWriter, edgeWriter);
|
deba@1333
|
328 |
writeNodes(nodeWriter);
|
deba@1333
|
329 |
writeEdges(edgeWriter);
|
deba@1137
|
330 |
os << "@end" << std::endl;
|
deba@1137
|
331 |
}
|
deba@1137
|
332 |
|
deba@1137
|
333 |
private:
|
deba@1137
|
334 |
|
deba@1137
|
335 |
template <class _Item>
|
deba@1137
|
336 |
class WriterBase {
|
deba@1137
|
337 |
public:
|
deba@1137
|
338 |
typedef _Item Item;
|
deba@1137
|
339 |
virtual void write(std::ostream&, const Item&) = 0;
|
deba@1137
|
340 |
};
|
deba@1137
|
341 |
|
deba@1137
|
342 |
template <class _Item, typename _Map, typename _Writer>
|
deba@1137
|
343 |
class MapWriter : public WriterBase<_Item> {
|
deba@1137
|
344 |
public:
|
deba@1137
|
345 |
typedef _Map Map;
|
deba@1137
|
346 |
typedef _Writer Writer;
|
deba@1137
|
347 |
typedef typename Writer::Value Value;
|
deba@1137
|
348 |
typedef _Item Item;
|
deba@1137
|
349 |
|
deba@1137
|
350 |
const Map& map;
|
deba@1137
|
351 |
Writer writer;
|
deba@1137
|
352 |
|
deba@1137
|
353 |
MapWriter(const Map& _map, const Writer& _writer)
|
deba@1137
|
354 |
: map(_map), writer(_writer) {}
|
deba@1137
|
355 |
|
deba@1137
|
356 |
|
deba@1137
|
357 |
virtual void write(std::ostream& os, const Item& item) {
|
deba@1137
|
358 |
writer.write(os, map[item]);
|
deba@1137
|
359 |
}
|
deba@1137
|
360 |
|
deba@1137
|
361 |
};
|
deba@1137
|
362 |
|
deba@1333
|
363 |
void writeNodeSet(WriterBase<Node>* & nodeWriter) {
|
deba@1333
|
364 |
if (node_map_writers.size() == 0) return;
|
deba@1333
|
365 |
os << "@nodeset" << std::endl;
|
deba@1333
|
366 |
for (int i = 0; i < (int)node_map_writers.size(); ++i) {
|
deba@1333
|
367 |
const std::string& id = node_map_writers[i].first;
|
deba@1333
|
368 |
os << id << '\t';
|
deba@1333
|
369 |
if (WriterTraits::idMapName(id) && nodeWriter == 0) {
|
deba@1333
|
370 |
nodeWriter = node_map_writers[i].second;
|
deba@1333
|
371 |
}
|
deba@1333
|
372 |
}
|
deba@1333
|
373 |
os << std::endl;
|
deba@1333
|
374 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
deba@1333
|
375 |
for (int i = 0; i < (int)node_map_writers.size(); ++i) {
|
deba@1333
|
376 |
node_map_writers[i].second->write(os, it);
|
deba@1333
|
377 |
}
|
deba@1333
|
378 |
os << std::endl;
|
deba@1333
|
379 |
}
|
deba@1333
|
380 |
|
deba@1333
|
381 |
}
|
deba@1333
|
382 |
|
deba@1333
|
383 |
void writeEdgeSet(WriterBase<Node>* nodeWriter,
|
deba@1333
|
384 |
WriterBase<Edge>* & edgeWriter) {
|
deba@1343
|
385 |
if (edge_map_writers.size() == 0) return;
|
deba@1333
|
386 |
if (nodeWriter == 0) {
|
deba@1333
|
387 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
388 |
}
|
deba@1333
|
389 |
os << "@edgeset" << std::endl;
|
deba@1333
|
390 |
os << "\t\t";
|
deba@1333
|
391 |
for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
|
deba@1333
|
392 |
const std::string& id = edge_map_writers[i].first;
|
deba@1333
|
393 |
os << id << '\t';
|
deba@1333
|
394 |
if (WriterTraits::idMapName(id) && edgeWriter == 0) {
|
deba@1333
|
395 |
edgeWriter = edge_map_writers[i].second;
|
deba@1333
|
396 |
}
|
deba@1333
|
397 |
}
|
deba@1333
|
398 |
os << std::endl;
|
deba@1333
|
399 |
for (EdgeIt it(graph); it != INVALID; ++it) {
|
deba@1333
|
400 |
nodeWriter->write(os, graph.source(it));
|
deba@1333
|
401 |
nodeWriter->write(os, graph.target(it));
|
deba@1333
|
402 |
for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
|
deba@1333
|
403 |
edge_map_writers[i].second->write(os, it);
|
deba@1333
|
404 |
}
|
deba@1333
|
405 |
os << std::endl;
|
deba@1333
|
406 |
}
|
deba@1333
|
407 |
}
|
deba@1333
|
408 |
|
deba@1333
|
409 |
void writeNodes(WriterBase<Node>* nodeWriter) {
|
deba@1343
|
410 |
if (node_writers.size() == 0) return;
|
deba@1333
|
411 |
if (nodeWriter == 0) {
|
deba@1333
|
412 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
413 |
}
|
deba@1333
|
414 |
os << "@nodes" << std::endl;
|
deba@1333
|
415 |
for (int i = 0; i < (int)node_writers.size(); ++i) {
|
deba@1333
|
416 |
os << node_writers[i].first << '\t';
|
deba@1333
|
417 |
nodeWriter->write(os, node_writers[i].second);
|
deba@1333
|
418 |
os << std::endl;
|
deba@1333
|
419 |
}
|
deba@1333
|
420 |
}
|
deba@1333
|
421 |
|
deba@1333
|
422 |
void writeEdges(WriterBase<Edge>* edgeWriter) {
|
deba@1343
|
423 |
if (edge_writers.size() == 0) return;
|
deba@1333
|
424 |
if (edgeWriter == 0) {
|
deba@1333
|
425 |
throw DataFormatError("Cannot find node id map");
|
deba@1333
|
426 |
}
|
deba@1333
|
427 |
os << "@edges" << std::endl;
|
deba@1333
|
428 |
for (int i = 0; i < (int)edge_writers.size(); ++i) {
|
deba@1333
|
429 |
os << edge_writers[i].first << '\t';
|
deba@1333
|
430 |
edgeWriter->write(os, edge_writers[i].second);
|
deba@1333
|
431 |
os << std::endl;
|
deba@1333
|
432 |
}
|
deba@1333
|
433 |
}
|
deba@1333
|
434 |
|
deba@1333
|
435 |
|
deba@1137
|
436 |
|
deba@1137
|
437 |
|
deba@1137
|
438 |
typedef std::vector< std::pair<std::string, WriterBase<Node>*> >
|
deba@1137
|
439 |
NodeMapWriters;
|
deba@1137
|
440 |
NodeMapWriters node_map_writers;
|
deba@1137
|
441 |
|
deba@1137
|
442 |
typedef std::vector< std::pair<std::string, WriterBase<Edge>*> >
|
deba@1137
|
443 |
EdgeMapWriters;
|
deba@1137
|
444 |
EdgeMapWriters edge_map_writers;
|
deba@1137
|
445 |
|
deba@1137
|
446 |
typedef std::vector<std::pair<std::string, Node> > NodeWriters;
|
deba@1137
|
447 |
NodeWriters node_writers;
|
deba@1137
|
448 |
|
deba@1137
|
449 |
typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
|
deba@1137
|
450 |
EdgeWriters edge_writers;
|
deba@1137
|
451 |
|
deba@1137
|
452 |
std::ostream& os;
|
deba@1208
|
453 |
const Graph& graph;
|
deba@1137
|
454 |
|
deba@1137
|
455 |
};
|
deba@1137
|
456 |
|
deba@1333
|
457 |
/// \brief Write a graph to the output.
|
deba@1333
|
458 |
///
|
deba@1333
|
459 |
/// Write a graph to the output.
|
deba@1333
|
460 |
/// \param os The output stream.
|
deba@1333
|
461 |
/// \param g The graph.
|
deba@1333
|
462 |
/// \param capacity The capacity map.
|
deba@1333
|
463 |
/// \param s The source node.
|
deba@1333
|
464 |
/// \param t The target node.
|
deba@1333
|
465 |
/// \param cost The cost map.
|
deba@1208
|
466 |
template<typename Graph, typename CapacityMap, typename CostMap>
|
deba@1208
|
467 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
468 |
const CapacityMap& capacity, const typename Graph::Node &s,
|
deba@1208
|
469 |
const typename Graph::Node &t, const CostMap& cost) {
|
deba@1208
|
470 |
GraphWriter<Graph> reader(os, g);
|
deba@1208
|
471 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1208
|
472 |
reader.addNodeMap("id", nodeIdMap);
|
deba@1208
|
473 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1208
|
474 |
reader.addEdgeMap("id", edgeIdMap);
|
deba@1208
|
475 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
476 |
reader.addEdgeMap("cost", cost);
|
deba@1208
|
477 |
reader.addNode("source", s);
|
deba@1208
|
478 |
reader.addNode("target", t);
|
deba@1208
|
479 |
reader.run();
|
deba@1208
|
480 |
}
|
deba@1208
|
481 |
|
deba@1333
|
482 |
/// \brief Write a graph to the output.
|
deba@1333
|
483 |
///
|
deba@1333
|
484 |
/// Write a graph to the output.
|
deba@1333
|
485 |
/// \param os The output stream.
|
deba@1333
|
486 |
/// \param g The graph.
|
deba@1333
|
487 |
/// \param capacity The capacity map.
|
deba@1333
|
488 |
/// \param s The source node.
|
deba@1333
|
489 |
/// \param t The target node.
|
deba@1297
|
490 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
491 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
492 |
const CapacityMap& capacity, const typename Graph::Node &s,
|
deba@1208
|
493 |
const typename Graph::Node &t) {
|
deba@1208
|
494 |
GraphWriter<Graph> reader(os, g);
|
deba@1208
|
495 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1208
|
496 |
reader.addNodeMap("id", nodeIdMap);
|
deba@1208
|
497 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1208
|
498 |
reader.addEdgeMap("id", edgeIdMap);
|
deba@1208
|
499 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
500 |
reader.addNode("source", s);
|
deba@1208
|
501 |
reader.addNode("target", t);
|
deba@1208
|
502 |
reader.run();
|
deba@1208
|
503 |
}
|
deba@1208
|
504 |
|
deba@1333
|
505 |
/// \brief Write a graph to the output.
|
deba@1333
|
506 |
///
|
deba@1333
|
507 |
/// Write a graph to the output.
|
deba@1333
|
508 |
/// \param os The output stream.
|
deba@1333
|
509 |
/// \param g The graph.
|
deba@1333
|
510 |
/// \param capacity The capacity map.
|
deba@1333
|
511 |
/// \param s The source node.
|
deba@1208
|
512 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
513 |
void writeGraph(std::ostream& os, const Graph &g,
|
deba@1208
|
514 |
const CapacityMap& capacity, const typename Graph::Node &s) {
|
deba@1208
|
515 |
GraphWriter<Graph> reader(os, g);
|
deba@1208
|
516 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1208
|
517 |
reader.addNodeMap("id", nodeIdMap);
|
deba@1208
|
518 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1208
|
519 |
reader.addEdgeMap("id", edgeIdMap);
|
deba@1208
|
520 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
521 |
reader.addNode("source", s);
|
deba@1208
|
522 |
reader.run();
|
deba@1208
|
523 |
}
|
deba@1333
|
524 |
|
deba@1333
|
525 |
/// \brief Write a graph to the output.
|
deba@1333
|
526 |
///
|
deba@1333
|
527 |
/// Write a graph to the output.
|
deba@1333
|
528 |
/// \param os The output stream.
|
deba@1333
|
529 |
/// \param g The graph.
|
deba@1333
|
530 |
/// \param capacity The capacity map.
|
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) {
|
deba@1208
|
534 |
GraphWriter<Graph> reader(os, g);
|
deba@1208
|
535 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1208
|
536 |
reader.addNodeMap("id", nodeIdMap);
|
deba@1208
|
537 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1208
|
538 |
reader.addEdgeMap("id", edgeIdMap);
|
deba@1208
|
539 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
540 |
reader.run();
|
deba@1208
|
541 |
}
|
deba@1333
|
542 |
|
deba@1333
|
543 |
/// \brief Write a graph to the output.
|
deba@1333
|
544 |
///
|
deba@1333
|
545 |
/// Write a graph to the output.
|
deba@1333
|
546 |
/// \param os The output stream.
|
deba@1333
|
547 |
/// \param g The graph.
|
deba@1208
|
548 |
template<typename Graph>
|
deba@1208
|
549 |
void writeGraph(std::ostream& os, const Graph &g) {
|
deba@1208
|
550 |
GraphWriter<Graph> reader(os, g);
|
deba@1208
|
551 |
IdMap<Graph, typename Graph::Node> nodeIdMap(g);
|
deba@1208
|
552 |
reader.addNodeMap("id", nodeIdMap);
|
deba@1208
|
553 |
IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
|
deba@1208
|
554 |
reader.addEdgeMap("id", edgeIdMap);
|
deba@1208
|
555 |
reader.run();
|
deba@1208
|
556 |
}
|
deba@1208
|
557 |
|
deba@1333
|
558 |
/// @}
|
deba@1137
|
559 |
|
deba@1137
|
560 |
}
|
deba@1214
|
561 |
|
deba@1214
|
562 |
#endif
|