2 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
19 ///\brief Graph writer.
30 #include <lemon/invalid.h>
31 #include <lemon/error.h>
36 /// \brief Standard WriterTraits for the GraphWriter class.
38 /// Standard WriterTraits for the GraphWriter class.
39 /// It defines standard writing method for all type of value.
40 struct DefaultWriterTraits {
42 /// \brief Template class for writing an value.
44 /// Template class for writing an value.
45 template <typename _Value>
50 /// \brief Writes a value to the given stream.
52 /// Writes a value to the given stream.
53 void write(std::ostream& os, const Value& value) {
61 /// \brief Writer class for quoted strings.
63 /// Writer class for quoted strings. It can process the escape
64 /// sequences in the string.
65 class QuotedStringWriter {
67 typedef std::string Value;
69 /// \brief Constructor for the writer.
71 /// Constructor for the writer. If the given parameter is true
72 /// the writer creates escape sequences from special characters.
73 QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
75 /// \brief Writes a quoted string to the given stream.
77 /// Writes a quoted string to the given stream.
78 void write(std::ostream& os, const std::string& value) {
82 for (int i = 0; i < (int)value.size(); ++i) {
83 writeEscape(ls, value[i]);
94 static void writeEscape(std::ostream& os, char c) {
131 os << '\\' << oct << (int)c;
143 /// \brief The graph writer class.
145 /// The writer class for the graph output.
146 /// \see \ref GraphReader
147 /// \see \ref graph-io-page
148 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
152 typedef _Graph Graph;
153 typedef typename Graph::Node Node;
154 typedef typename Graph::NodeIt NodeIt;
155 typedef typename Graph::Edge Edge;
156 typedef typename Graph::EdgeIt EdgeIt;
158 typedef _WriterTraits WriterTraits;
160 /// \brief Construct a new GraphWriter.
162 /// Construct a new GraphWriter. It writes from the given map,
163 /// it constructs the given map and it use the given writer as the
165 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
168 /// \brief Destruct the graph writer.
170 /// Destruct the graph writer.
172 for (typename NodeMapWriters::iterator it = node_map_writers.begin();
173 it != node_map_writers.end(); ++it) {
177 for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
178 it != edge_map_writers.end(); ++it) {
186 /// \brief Add a new node map writer command for the writer.
188 /// Add a new node map writer command for the writer.
189 template <typename Map>
190 GraphWriter& addNodeMap(std::string name, const Map& map) {
191 return addNodeMap<typename WriterTraits::template Writer<
192 typename Map::Value>, Map>(name, map);
195 /// \brief Add a new node map writer command for the writer.
197 /// Add a new node map writer command for the writer.
198 template <typename Writer, typename Map>
199 GraphWriter& addNodeMap(std::string name, const Map& map,
200 const Writer& writer = Writer()) {
201 node_map_writers.push_back(
202 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
208 /// \brief Add a new edge map writer command for the writer.
210 /// Add a new edge map writer command for the writer.
211 template <typename Map>
212 GraphWriter& addEdgeMap(std::string name, const Map& map) {
213 return addEdgeMap<typename WriterTraits::template Writer<
214 typename Map::Value>, Map>(name, map);
218 /// \brief Add a new edge map writer command for the writer.
220 /// Add a new edge map writer command for the writer.
221 template <typename Writer, typename Map>
222 GraphWriter& addEdgeMap(std::string name,
223 const Map& map, const Writer& writer = Writer()) {
224 edge_map_writers.push_back(make_pair(name,
225 new MapWriter<Edge, Map, Writer>(map, writer)));
229 /// \brief Add a new labeled node writer for the writer.
231 /// Add a new labeled node writer for the writer.
232 GraphWriter& addNode(std::string name, const Node& node) {
233 node_writers.push_back(make_pair(name, node));
237 /// \brief Add a new labeled edge writer for the writer.
239 /// Add a new labeled edge writer for the writer.
240 GraphWriter& addEdge(std::string name, const Edge& edge) {
241 edge_writers.push_back(make_pair(name, edge));
245 /// \brief Executes the writer commands.
247 /// Executes the writer commands.
253 os << "@end" << std::endl;
258 void writeNodeSet() {
259 if (node_map_writers.size() == 0) return;
260 os << "@nodeset" << std::endl;
261 for (int i = 0; i < (int)node_map_writers.size(); ++i) {
262 os << node_map_writers[i].first << '\t';
265 for (NodeIt it(graph); it != INVALID; ++it) {
266 for (int i = 0; i < (int)node_map_writers.size(); ++i) {
267 node_map_writers[i].second->write(os, it);
274 void writeEdgeSet() {
275 if (edge_map_writers.size() == 0) return;
276 if (node_map_writers.size() == 0) {
277 // ErrorMessage message;
278 // message << "Missing node id map";
279 // throw IOLogicError(message);
281 os << "@edgeset" << std::endl;
283 for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
284 os << edge_map_writers[i].first << '\t';
287 for (EdgeIt it(graph); it != INVALID; ++it) {
288 node_map_writers[0].second->write(os, graph.source(it));
289 node_map_writers[0].second->write(os, graph.target(it));
290 for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
291 edge_map_writers[i].second->write(os, it);
298 if (node_writers.size() == 0) return;
299 if (node_map_writers.size() == 0) {
300 // throw Exception() << "Missing node id map";
302 os << "@nodes" << std::endl;
303 for (int i = 0; i < (int)node_writers.size(); ++i) {
304 os << node_writers[i].first << '\t';
305 node_map_writers[0].second->write(os, node_writers[i].second);
311 if (edge_writers.size() == 0) return;
312 if (edge_map_writers.size() == 0) {
313 // throw Exception() << "Missing edge id map";
315 os << "@edges" << std::endl;
316 for (int i = 0; i < (int)edge_writers.size(); ++i) {
317 os << edge_writers[i].first << '\t';
318 edge_map_writers[0].second->write(os, edge_writers[i].second);
325 template <class _Item>
329 virtual void write(std::ostream&, const Item&) = 0;
332 template <class _Item, typename _Map, typename _Writer>
333 class MapWriter : public WriterBase<_Item> {
336 typedef _Writer Writer;
337 typedef typename Writer::Value Value;
343 MapWriter(const Map& _map, const Writer& _writer)
344 : map(_map), writer(_writer) {}
347 virtual void write(std::ostream& os, const Item& item) {
348 writer.write(os, map[item]);
355 typedef std::vector< std::pair<std::string, WriterBase<Node>*> >
357 NodeMapWriters node_map_writers;
359 typedef std::vector< std::pair<std::string, WriterBase<Edge>*> >
361 EdgeMapWriters edge_map_writers;
363 typedef std::vector<std::pair<std::string, Node> > NodeWriters;
364 NodeWriters node_writers;
366 typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
367 EdgeWriters edge_writers;