Changes in doc.
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 struct DefaultWriterTraits {
38 template <typename _Value>
42 void write(std::ostream& os, const Value& value) {
50 class QuotedStringWriter {
52 typedef std::string Value;
54 QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
56 void write(std::ostream& os, const std::string& value) {
61 for (int i = 0; i < value.size(); ++i) {
62 writeEscape(ls, value[i]);
73 static void writeEscape(std::ostream& os, char c) {
110 os << '\\' << oct << (int)c;
123 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
127 typedef _Graph Graph;
128 typedef typename Graph::Node Node;
129 typedef typename Graph::NodeIt NodeIt;
130 typedef typename Graph::Edge Edge;
131 typedef typename Graph::EdgeIt EdgeIt;
133 typedef _WriterTraits WriterTraits;
135 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
140 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); it != node_map_writers.end(); ++it) {
144 for (typename EdgeMapWriters::iterator it = edge_map_writers.begin(); it != edge_map_writers.end(); ++it) {
152 template <typename Map>
153 GraphWriter& writeNodeMap(std::string name, const Map& map) {
154 return writeNodeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map);
157 template <typename Writer, typename Map>
158 GraphWriter& writeNodeMap(std::string name, const Map& map, const Writer& writer = Writer()) {
159 // if (node_map_writers.find(name) != node_map_writers.end()) {
160 // throw Exception() << "Multiple write rule for node map: " << name;
162 node_map_writers.push_back(make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
168 template <typename Map>
169 GraphWriter& writeEdgeMap(std::string name, const Map& map) {
170 return writeEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map);
174 template <typename Writer, typename Map>
175 GraphWriter& writeEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) {
176 // if (edge_map_writers.find(name) != edge_map_writers.end()) {
177 // throw Exception() << "Multiple write rule for edge map: " << name;
179 edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer)));
184 GraphWriter& writeNode(std::string name, const Node& node) {
185 // if (node_writers.find(name) != node_writers.end()) {
186 // throw Exception() << "Multiple write rule for node";
188 node_writers.push_back(make_pair(name, node));
193 GraphWriter& writeEdge(std::string name, const Edge& edge) {
194 // if (edge_writers.find(name) != edge_writers.end()) {
195 // throw Exception() << "Multiple write rule for edge";
197 edge_writers.push_back(make_pair(name, edge));
205 os << "@end" << std::endl;
210 void writeNodeSet() {
211 if (node_map_writers.size() == 0) return;
212 os << "@nodeset" << std::endl;
213 for (int i = 0; i < node_map_writers.size(); ++i) {
214 os << node_map_writers[i].first << '\t';
217 for (NodeIt it(graph); it != INVALID; ++it) {
218 for (int i = 0; i < node_map_writers.size(); ++i) {
219 node_map_writers[i].second->write(os, it);
226 void writeEdgeSet() {
227 if (edge_map_writers.size() == 0) return;
228 if (node_map_writers.size() == 0) {
229 throw Exception() << "Missing node id map";
231 os << "@edgeset" << std::endl;
233 for (int i = 0; i < edge_map_writers.size(); ++i) {
234 os << edge_map_writers[i].first << '\t';
237 for (EdgeIt it(graph); it != INVALID; ++it) {
238 node_map_writers[0].second->write(os, graph.source(it));
239 node_map_writers[0].second->write(os, graph.target(it));
240 for (int i = 0; i < edge_map_writers.size(); ++i) {
241 edge_map_writers[i].second->write(os, it);
248 if (node_writers.size() == 0) return;
249 if (node_map_writers.size() == 0) {
250 throw Exception() << "Missing node id map";
252 os << "@nodes" << std::endl;
253 for (int i = 0; i < node_writers.size(); ++i) {
254 os << node_writers[i].first << '\t';
255 node_map_writers[0].second->write(os, node_writers[i].second);
261 if (edge_writers.size() == 0) return;
262 if (edge_map_writers.size() == 0) {
263 throw Exception() << "Missing edge id map";
265 os << "@edges" << std::endl;
266 for (int i = 0; i < edge_writers.size(); ++i) {
267 os << edge_writers[i].first << '\t';
268 edge_map_writers[0].second->write(os, edge_writers[i].second);
275 template <class _Item>
279 virtual void write(std::ostream&, const Item&) = 0;
282 template <class _Item, typename _Map, typename _Writer>
283 class MapWriter : public WriterBase<_Item> {
286 typedef _Writer Writer;
287 typedef typename Writer::Value Value;
293 MapWriter(const Map& _map, const Writer& _writer)
294 : map(_map), writer(_writer) {}
297 virtual void write(std::ostream& os, const Item& item) {
299 writer.write(os, map[item]);
306 typedef std::vector< std::pair<std::string, WriterBase<Node>*> > NodeMapWriters;
307 NodeMapWriters node_map_writers;
309 typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > EdgeMapWriters;
310 EdgeMapWriters edge_map_writers;
312 typedef std::vector<std::pair<std::string, Node> > NodeWriters;
313 NodeWriters node_writers;
315 typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
316 EdgeWriters edge_writers;