Changing first to iterators.
2 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 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 from the given stream.
52 /// Writes a value from 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 from the given stream.
77 /// Writes a quoted string from 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 graph-io-page
147 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
151 typedef _Graph Graph;
152 typedef typename Graph::Node Node;
153 typedef typename Graph::NodeIt NodeIt;
154 typedef typename Graph::Edge Edge;
155 typedef typename Graph::EdgeIt EdgeIt;
157 typedef _WriterTraits WriterTraits;
159 /// \brief Construct a new GraphWriter.
161 /// Construct a new GraphWriter. It writes from the given map,
162 /// it constructs the given map and it use the given writer as the
164 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
167 /// \brief Destruct the graph writer.
169 /// Destruct the graph writer.
171 for (typename NodeMapWriters::iterator it = node_map_writers.begin();
172 it != node_map_writers.end(); ++it) {
176 for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
177 it != edge_map_writers.end(); ++it) {
185 /// \brief Add a new node map writer command for the writer.
187 /// Add a new node map writer command for the writer.
188 template <typename Map>
189 GraphWriter& addNodeMap(std::string name, const Map& map) {
190 return addNodeMap<typename WriterTraits::template Writer<
191 typename Map::Value>, Map>(name, map);
194 /// \brief Add a new node map writer command for the writer.
196 /// Add a new node map writer command for the writer.
197 template <typename Writer, typename Map>
198 GraphWriter& addNodeMap(std::string name, const Map& map,
199 const Writer& writer = Writer()) {
200 node_map_writers.push_back(
201 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
207 /// \brief Add a new edge map writer command for the writer.
209 /// Add a new edge map writer command for the writer.
210 template <typename Map>
211 GraphWriter& addEdgeMap(std::string name, const Map& map) {
212 return addEdgeMap<typename WriterTraits::template Writer<
213 typename Map::Value>, Map>(name, map);
217 /// \brief Add a new edge map writer command for the writer.
219 /// Add a new edge map writer command for the writer.
220 template <typename Writer, typename Map>
221 GraphWriter& addEdgeMap(std::string name,
222 const Map& map, const Writer& writer = Writer()) {
223 edge_map_writers.push_back(make_pair(name,
224 new MapWriter<Edge, Map, Writer>(map, writer)));
228 /// \brief Add a new labeled node writer for the writer.
230 /// Add a new labeled node writer for the writer.
231 GraphWriter& addNode(std::string name, const Node& node) {
232 node_writers.push_back(make_pair(name, node));
236 /// \brief Add a new labeled edge writer for the writer.
238 /// Add a new labeled edge writer for the writer.
239 GraphWriter& addEdge(std::string name, const Edge& edge) {
240 edge_writers.push_back(make_pair(name, edge));
244 /// \brief Executes the writer commands.
246 /// Executes the writer commands.
252 os << "@end" << std::endl;
257 void writeNodeSet() {
258 if (node_map_writers.size() == 0) return;
259 os << "@nodeset" << std::endl;
260 for (int i = 0; i < (int)node_map_writers.size(); ++i) {
261 os << node_map_writers[i].first << '\t';
264 for (NodeIt it(graph); it != INVALID; ++it) {
265 for (int i = 0; i < (int)node_map_writers.size(); ++i) {
266 node_map_writers[i].second->write(os, it);
273 void writeEdgeSet() {
274 if (edge_map_writers.size() == 0) return;
275 if (node_map_writers.size() == 0) {
276 throw Exception() << "Missing node id map";
278 os << "@edgeset" << std::endl;
280 for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
281 os << edge_map_writers[i].first << '\t';
284 for (EdgeIt it(graph); it != INVALID; ++it) {
285 node_map_writers[0].second->write(os, graph.source(it));
286 node_map_writers[0].second->write(os, graph.target(it));
287 for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
288 edge_map_writers[i].second->write(os, it);
295 if (node_writers.size() == 0) return;
296 if (node_map_writers.size() == 0) {
297 throw Exception() << "Missing node id map";
299 os << "@nodes" << std::endl;
300 for (int i = 0; i < (int)node_writers.size(); ++i) {
301 os << node_writers[i].first << '\t';
302 node_map_writers[0].second->write(os, node_writers[i].second);
308 if (edge_writers.size() == 0) return;
309 if (edge_map_writers.size() == 0) {
310 throw Exception() << "Missing edge id map";
312 os << "@edges" << std::endl;
313 for (int i = 0; i < (int)edge_writers.size(); ++i) {
314 os << edge_writers[i].first << '\t';
315 edge_map_writers[0].second->write(os, edge_writers[i].second);
322 template <class _Item>
326 virtual void write(std::ostream&, const Item&) = 0;
329 template <class _Item, typename _Map, typename _Writer>
330 class MapWriter : public WriterBase<_Item> {
333 typedef _Writer Writer;
334 typedef typename Writer::Value Value;
340 MapWriter(const Map& _map, const Writer& _writer)
341 : map(_map), writer(_writer) {}
344 virtual void write(std::ostream& os, const Item& item) {
345 writer.write(os, map[item]);
352 typedef std::vector< std::pair<std::string, WriterBase<Node>*> >
354 NodeMapWriters node_map_writers;
356 typedef std::vector< std::pair<std::string, WriterBase<Edge>*> >
358 EdgeMapWriters edge_map_writers;
360 typedef std::vector<std::pair<std::string, Node> > NodeWriters;
361 NodeWriters node_writers;
363 typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
364 EdgeWriters edge_writers;