1.1 --- a/src/lemon/graph_writer.h Sat Apr 09 19:32:06 2005 +0000
1.2 +++ b/src/lemon/graph_writer.h Sat Apr 09 19:35:33 2005 +0000
1.3 @@ -37,15 +37,20 @@
1.4
1.5 namespace lemon {
1.6
1.7 + /// \addtogroup io_group
1.8 + /// @{
1.9 +
1.10 /// \brief Standard WriterTraits for the GraphWriter class.
1.11 ///
1.12 /// Standard WriterTraits for the GraphWriter class.
1.13 /// It defines standard writing method for all type of value.
1.14 + /// \author Balazs Dezso
1.15 struct DefaultWriterTraits {
1.16
1.17 /// \brief Template class for writing an value.
1.18 ///
1.19 /// Template class for writing an value.
1.20 + /// \author Balazs Dezso
1.21 template <typename _Value>
1.22 struct Writer {
1.23 /// The value type.
1.24 @@ -59,6 +64,13 @@
1.25 }
1.26 };
1.27
1.28 + /// \brief Returns wheter this name is an ID map name.
1.29 + ///
1.30 + /// Returns wheter this name is an ID map name.
1.31 + static bool idMapName(const std::string& name) {
1.32 + return name == "id";
1.33 + }
1.34 +
1.35 };
1.36
1.37
1.38 @@ -66,6 +78,7 @@
1.39 ///
1.40 /// Writer class for quoted strings. It can process the escape
1.41 /// sequences in the string.
1.42 + /// \author Balazs Dezso
1.43 class QuotedStringWriter {
1.44 public:
1.45 typedef std::string Value;
1.46 @@ -146,12 +159,66 @@
1.47
1.48 /// \brief The graph writer class.
1.49 ///
1.50 - ///\ingroup io_group
1.51 - /// The writer class for the graph output.
1.52 + /// The \c GraphWriter class provides the graph output. To write a graph
1.53 + /// you should first give writing commands for the writer. You can declare
1.54 + /// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
1.55 + /// Edge writing.
1.56 + ///
1.57 + /// \code
1.58 + /// GraphWriter<ListGraph> writer(std::cout, graph);
1.59 + /// \endcode
1.60 + ///
1.61 + /// The \c addNodeMap() function declares a \c NodeMap writing command in the
1.62 + /// \c GraphWriter. You should give as parameter the name of the map and the
1.63 + /// map object. The NodeMap writing command with name "id" should write a
1.64 + /// unique map because it is regarded as ID map.
1.65 + ///
1.66 + /// \code
1.67 + /// IdMap<ListGraph, Node> nodeIdMap;
1.68 + /// writer.addNodeMap("id", nodeIdMap);
1.69 + ///
1.70 + /// writer.addNodeMap("x-coord", xCoordMap);
1.71 + /// writer.addNodeMap("y-coord", yCoordMap);
1.72 + /// writer.addNodeMap("color", colorMap);
1.73 + /// \endcode
1.74 + ///
1.75 + /// With the \c addEdgeMap() member function you can give an edge map
1.76 + /// writing command similar to the NodeMaps.
1.77 + ///
1.78 + /// \code
1.79 + /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
1.80 + /// edgeDescMap(graph);
1.81 + /// writer.addEdgeMap("descriptor", edgeDescMap);
1.82 + ///
1.83 + /// writer.addEdgeMap("weight", weightMap);
1.84 + /// writer.addEdgeMap("label", labelMap);
1.85 + /// \endcode
1.86 + ///
1.87 + /// With \c addNode() and \c addEdge() functions you can point out Nodes and
1.88 + /// Edges in the graph. By example, you can write out the source and target
1.89 + /// of the graph.
1.90 + ///
1.91 + /// \code
1.92 + /// writer.addNode("source", sourceNode);
1.93 + /// writer.addNode("target", targetNode);
1.94 + ///
1.95 + /// writer.addEdge("observed", edge);
1.96 + /// \endcode
1.97 + ///
1.98 + /// After you give all write commands you must call the \c run() member
1.99 + /// function, which execute all the writer commands.
1.100 + ///
1.101 + /// \code
1.102 + /// writer.run();
1.103 + /// \endcode
1.104 + ///
1.105 /// \see DefaultWriterTraits
1.106 /// \see QuotedStringWriter
1.107 + /// \see IdMap
1.108 + /// \see DescriptorMap
1.109 /// \see \ref GraphReader
1.110 /// \see \ref graph-io-page
1.111 + /// \author Balazs Dezso
1.112 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
1.113 class GraphWriter {
1.114 public:
1.115 @@ -254,82 +321,17 @@
1.116 ///
1.117 /// Executes the writer commands.
1.118 void run() {
1.119 - writeNodeSet();
1.120 - writeEdgeSet();
1.121 - writeNodes();
1.122 - writeEdges();
1.123 + WriterBase<Node>* nodeWriter = 0;
1.124 + WriterBase<Edge>* edgeWriter = 0;
1.125 + writeNodeSet(nodeWriter);
1.126 + writeEdgeSet(nodeWriter, edgeWriter);
1.127 + writeNodes(nodeWriter);
1.128 + writeEdges(edgeWriter);
1.129 os << "@end" << std::endl;
1.130 }
1.131
1.132 private:
1.133
1.134 - void writeNodeSet() {
1.135 - if (node_map_writers.size() == 0) return;
1.136 - os << "@nodeset" << std::endl;
1.137 - for (int i = 0; i < (int)node_map_writers.size(); ++i) {
1.138 - os << node_map_writers[i].first << '\t';
1.139 - }
1.140 - os << std::endl;
1.141 - for (NodeIt it(graph); it != INVALID; ++it) {
1.142 - for (int i = 0; i < (int)node_map_writers.size(); ++i) {
1.143 - node_map_writers[i].second->write(os, it);
1.144 - }
1.145 - os << std::endl;
1.146 - }
1.147 -
1.148 - }
1.149 -
1.150 - void writeEdgeSet() {
1.151 - if (edge_map_writers.size() == 0) return;
1.152 - if (node_map_writers.size() == 0) {
1.153 - // ErrorMessage message;
1.154 - // message << "Missing node id map";
1.155 - // throw IOLogicError(message);
1.156 - }
1.157 - os << "@edgeset" << std::endl;
1.158 - os << "\t\t";
1.159 - for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
1.160 - os << edge_map_writers[i].first << '\t';
1.161 - }
1.162 - os << std::endl;
1.163 - for (EdgeIt it(graph); it != INVALID; ++it) {
1.164 - node_map_writers[0].second->write(os, graph.source(it));
1.165 - node_map_writers[0].second->write(os, graph.target(it));
1.166 - for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
1.167 - edge_map_writers[i].second->write(os, it);
1.168 - }
1.169 - os << std::endl;
1.170 - }
1.171 - }
1.172 -
1.173 - void writeNodes() {
1.174 - if (node_writers.size() == 0) return;
1.175 - if (node_map_writers.size() == 0) {
1.176 - // throw Exception() << "Missing node id map";
1.177 - }
1.178 - os << "@nodes" << std::endl;
1.179 - for (int i = 0; i < (int)node_writers.size(); ++i) {
1.180 - os << node_writers[i].first << '\t';
1.181 - node_map_writers[0].second->write(os, node_writers[i].second);
1.182 - os << std::endl;
1.183 - }
1.184 - }
1.185 -
1.186 - void writeEdges() {
1.187 - if (edge_writers.size() == 0) return;
1.188 - if (edge_map_writers.size() == 0) {
1.189 - // throw Exception() << "Missing edge id map";
1.190 - }
1.191 - os << "@edges" << std::endl;
1.192 - for (int i = 0; i < (int)edge_writers.size(); ++i) {
1.193 - os << edge_writers[i].first << '\t';
1.194 - edge_map_writers[0].second->write(os, edge_writers[i].second);
1.195 - os << std::endl;
1.196 - }
1.197 - }
1.198 -
1.199 - // Writers
1.200 -
1.201 template <class _Item>
1.202 class WriterBase {
1.203 public:
1.204 @@ -358,6 +360,76 @@
1.205
1.206 };
1.207
1.208 + void writeNodeSet(WriterBase<Node>* & nodeWriter) {
1.209 + if (node_map_writers.size() == 0) return;
1.210 + os << "@nodeset" << std::endl;
1.211 + for (int i = 0; i < (int)node_map_writers.size(); ++i) {
1.212 + const std::string& id = node_map_writers[i].first;
1.213 + os << id << '\t';
1.214 + if (WriterTraits::idMapName(id) && nodeWriter == 0) {
1.215 + nodeWriter = node_map_writers[i].second;
1.216 + }
1.217 + }
1.218 + os << std::endl;
1.219 + for (NodeIt it(graph); it != INVALID; ++it) {
1.220 + for (int i = 0; i < (int)node_map_writers.size(); ++i) {
1.221 + node_map_writers[i].second->write(os, it);
1.222 + }
1.223 + os << std::endl;
1.224 + }
1.225 +
1.226 + }
1.227 +
1.228 + void writeEdgeSet(WriterBase<Node>* nodeWriter,
1.229 + WriterBase<Edge>* & edgeWriter) {
1.230 + if (nodeWriter == 0) {
1.231 + throw DataFormatError("Cannot find node id map");
1.232 + }
1.233 + os << "@edgeset" << std::endl;
1.234 + os << "\t\t";
1.235 + for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
1.236 + const std::string& id = edge_map_writers[i].first;
1.237 + os << id << '\t';
1.238 + if (WriterTraits::idMapName(id) && edgeWriter == 0) {
1.239 + edgeWriter = edge_map_writers[i].second;
1.240 + }
1.241 + }
1.242 + os << std::endl;
1.243 + for (EdgeIt it(graph); it != INVALID; ++it) {
1.244 + nodeWriter->write(os, graph.source(it));
1.245 + nodeWriter->write(os, graph.target(it));
1.246 + for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
1.247 + edge_map_writers[i].second->write(os, it);
1.248 + }
1.249 + os << std::endl;
1.250 + }
1.251 + }
1.252 +
1.253 + void writeNodes(WriterBase<Node>* nodeWriter) {
1.254 + if (nodeWriter == 0) {
1.255 + throw DataFormatError("Cannot find node id map");
1.256 + }
1.257 + os << "@nodes" << std::endl;
1.258 + for (int i = 0; i < (int)node_writers.size(); ++i) {
1.259 + os << node_writers[i].first << '\t';
1.260 + nodeWriter->write(os, node_writers[i].second);
1.261 + os << std::endl;
1.262 + }
1.263 + }
1.264 +
1.265 + void writeEdges(WriterBase<Edge>* edgeWriter) {
1.266 + if (edgeWriter == 0) {
1.267 + throw DataFormatError("Cannot find node id map");
1.268 + }
1.269 + os << "@edges" << std::endl;
1.270 + for (int i = 0; i < (int)edge_writers.size(); ++i) {
1.271 + os << edge_writers[i].first << '\t';
1.272 + edgeWriter->write(os, edge_writers[i].second);
1.273 + os << std::endl;
1.274 + }
1.275 + }
1.276 +
1.277 +
1.278
1.279
1.280 typedef std::vector< std::pair<std::string, WriterBase<Node>*> >
1.281 @@ -379,7 +451,15 @@
1.282
1.283 };
1.284
1.285 - /// Ready to use writer function.
1.286 + /// \brief Write a graph to the output.
1.287 + ///
1.288 + /// Write a graph to the output.
1.289 + /// \param os The output stream.
1.290 + /// \param g The graph.
1.291 + /// \param capacity The capacity map.
1.292 + /// \param s The source node.
1.293 + /// \param t The target node.
1.294 + /// \param cost The cost map.
1.295 template<typename Graph, typename CapacityMap, typename CostMap>
1.296 void writeGraph(std::ostream& os, const Graph &g,
1.297 const CapacityMap& capacity, const typename Graph::Node &s,
1.298 @@ -396,7 +476,14 @@
1.299 reader.run();
1.300 }
1.301
1.302 - /// Ready to use writer function.
1.303 + /// \brief Write a graph to the output.
1.304 + ///
1.305 + /// Write a graph to the output.
1.306 + /// \param os The output stream.
1.307 + /// \param g The graph.
1.308 + /// \param capacity The capacity map.
1.309 + /// \param s The source node.
1.310 + /// \param t The target node.
1.311 template<typename Graph, typename CapacityMap>
1.312 void writeGraph(std::ostream& os, const Graph &g,
1.313 const CapacityMap& capacity, const typename Graph::Node &s,
1.314 @@ -412,7 +499,13 @@
1.315 reader.run();
1.316 }
1.317
1.318 - /// Ready to use writer function.
1.319 + /// \brief Write a graph to the output.
1.320 + ///
1.321 + /// Write a graph to the output.
1.322 + /// \param os The output stream.
1.323 + /// \param g The graph.
1.324 + /// \param capacity The capacity map.
1.325 + /// \param s The source node.
1.326 template<typename Graph, typename CapacityMap>
1.327 void writeGraph(std::ostream& os, const Graph &g,
1.328 const CapacityMap& capacity, const typename Graph::Node &s) {
1.329 @@ -425,7 +518,13 @@
1.330 reader.addNode("source", s);
1.331 reader.run();
1.332 }
1.333 - /// Ready to use writer function.
1.334 +
1.335 + /// \brief Write a graph to the output.
1.336 + ///
1.337 + /// Write a graph to the output.
1.338 + /// \param os The output stream.
1.339 + /// \param g The graph.
1.340 + /// \param capacity The capacity map.
1.341 template<typename Graph, typename CapacityMap>
1.342 void writeGraph(std::ostream& os, const Graph &g,
1.343 const CapacityMap& capacity) {
1.344 @@ -437,7 +536,12 @@
1.345 reader.addEdgeMap("capacity", capacity);
1.346 reader.run();
1.347 }
1.348 - /// Ready to use writer function.
1.349 +
1.350 + /// \brief Write a graph to the output.
1.351 + ///
1.352 + /// Write a graph to the output.
1.353 + /// \param os The output stream.
1.354 + /// \param g The graph.
1.355 template<typename Graph>
1.356 void writeGraph(std::ostream& os, const Graph &g) {
1.357 GraphWriter<Graph> reader(os, g);
1.358 @@ -448,6 +552,7 @@
1.359 reader.run();
1.360 }
1.361
1.362 + /// @}
1.363
1.364 }
1.365