1.1 --- a/src/lemon/graph_writer.h Sat May 14 17:37:33 2005 +0000
1.2 +++ b/src/lemon/graph_writer.h Sat May 14 17:39:37 2005 +0000
1.3 @@ -52,8 +52,7 @@
1.4 /// IdMap<ListGraph, Node> nodeIdMap;
1.5 /// writer.writeNodeMap("id", nodeIdMap);
1.6 ///
1.7 - /// writer.writeNodeMap("x-coord", xCoordMap);
1.8 - /// writer.writeNodeMap("y-coord", yCoordMap);
1.9 + /// writer.writeNodeMap("coords", coords);
1.10 /// writer.writeNodeMap("color", colorMap);
1.11 /// \endcode
1.12 ///
1.13 @@ -91,7 +90,7 @@
1.14 /// \see QuotedStringWriter
1.15 /// \see IdMap
1.16 /// \see DescriptorMap
1.17 - /// \see \ref GraphWriter
1.18 + /// \see \ref GraphReader
1.19 /// \see \ref graph-io-page
1.20 /// \author Balazs Dezso
1.21 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
1.22 @@ -110,9 +109,8 @@
1.23 /// to the given stream.
1.24 GraphWriter(std::ostream& _os, const Graph& _graph)
1.25 : writer(new LemonWriter(_os)), own_writer(true),
1.26 - graph(_graph),
1.27 - nodeset_writer(*writer, graph, std::string()),
1.28 - edgeset_writer(*writer, graph, nodeset_writer, std::string()),
1.29 + nodeset_writer(*writer, _graph, std::string()),
1.30 + edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.31 node_writer(*writer, nodeset_writer, std::string()),
1.32 edge_writer(*writer, edgeset_writer, std::string()),
1.33 attribute_writer(*writer, std::string()) {}
1.34 @@ -123,9 +121,8 @@
1.35 /// to the given file.
1.36 GraphWriter(const std::string& _filename, const Graph& _graph)
1.37 : writer(new LemonWriter(_filename)), own_writer(true),
1.38 - graph(_graph),
1.39 - nodeset_writer(*writer, graph, std::string()),
1.40 - edgeset_writer(*writer, graph, nodeset_writer, std::string()),
1.41 + nodeset_writer(*writer, _graph, std::string()),
1.42 + edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.43 node_writer(*writer, nodeset_writer, std::string()),
1.44 edge_writer(*writer, edgeset_writer, std::string()),
1.45 attribute_writer(*writer, std::string()) {}
1.46 @@ -136,9 +133,8 @@
1.47 /// to given LemonReader.
1.48 GraphWriter(LemonWriter& _writer, const Graph& _graph)
1.49 : writer(_writer), own_writer(false),
1.50 - graph(_graph),
1.51 - nodeset_writer(*writer, graph, std::string()),
1.52 - edgeset_writer(*writer, graph, nodeset_writer, std::string()),
1.53 + nodeset_writer(*writer, _graph, std::string()),
1.54 + edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.55 node_writer(*writer, nodeset_writer, std::string()),
1.56 edge_writer(*writer, edgeset_writer, std::string()),
1.57 attribute_writer(*writer, std::string()) {}
1.58 @@ -156,7 +152,7 @@
1.59 /// Add a new node map writer command for the writer.
1.60 template <typename Map>
1.61 GraphWriter& writeNodeMap(std::string name, const Map& map) {
1.62 - nodeset_writer.writeMap(name, map);
1.63 + nodeset_writer.writeNodeMap(name, map);
1.64 return *this;
1.65 }
1.66
1.67 @@ -165,8 +161,8 @@
1.68 /// Add a new node map writer command for the writer.
1.69 template <typename Writer, typename Map>
1.70 GraphWriter& writeNodeMap(std::string name, const Map& map,
1.71 - const Writer& writer = Writer()) {
1.72 - nodeset_writer.writeMap(name, map, writer);
1.73 + const Writer& writer = Writer()) {
1.74 + nodeset_writer.writeNodeMap(name, map, writer);
1.75 return *this;
1.76 }
1.77
1.78 @@ -176,7 +172,7 @@
1.79 /// Add a new edge map writer command for the writer.
1.80 template <typename Map>
1.81 GraphWriter& writeEdgeMap(std::string name, const Map& map) {
1.82 - edgeset_writer.writeMap(name, map);
1.83 + edgeset_writer.writeEdgeMap(name, map);
1.84 return *this;
1.85 }
1.86
1.87 @@ -186,8 +182,8 @@
1.88 /// Add a new edge map writer command for the writer.
1.89 template <typename Writer, typename Map>
1.90 GraphWriter& writeEdgeMap(std::string name, const Map& map,
1.91 - const Writer& writer = Writer()) {
1.92 - edgeset_writer.writeMap(name, map, writer);
1.93 + const Writer& writer = Writer()) {
1.94 + edgeset_writer.writeEdgeMap(name, map, writer);
1.95 return *this;
1.96 }
1.97
1.98 @@ -247,8 +243,6 @@
1.99 LemonWriter* writer;
1.100 bool own_writer;
1.101
1.102 - const Graph& graph;
1.103 -
1.104 NodeSetWriter<Graph, WriterTraits> nodeset_writer;
1.105 EdgeSetWriter<Graph, WriterTraits> edgeset_writer;
1.106
1.107 @@ -360,6 +354,279 @@
1.108 writer.run();
1.109 }
1.110
1.111 + /// \brief The undirected graph writer class.
1.112 + ///
1.113 + /// The \c UndirGraphWriter class provides the undir graph output. To write
1.114 + /// a graph you should first give writing commands for the writer. You can
1.115 + /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap
1.116 + /// writing and labeled Node, Edge or UndirEdge writing.
1.117 + ///
1.118 + /// \code
1.119 + /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph);
1.120 + /// \endcode
1.121 + ///
1.122 + /// The \c writeNodeMap() function declares a \c NodeMap writing
1.123 + /// command in the \c UndirGraphWriter. You should give as parameter
1.124 + /// the name of the map and the map object. The NodeMap writing
1.125 + /// command with name "id" should write a unique map because it
1.126 + /// is regarded as ID map.
1.127 + ///
1.128 + /// \code
1.129 + /// IdMap<UndirListGraph, Node> nodeIdMap;
1.130 + /// writer.writeNodeMap("id", nodeIdMap);
1.131 + ///
1.132 + /// writer.writeNodeMap("coords", coords);
1.133 + /// writer.writeNodeMap("color", colorMap);
1.134 + /// \endcode
1.135 + ///
1.136 + /// With the \c writeUndirEdgeMap() member function you can give an
1.137 + /// undirected edge map writing command similar to the NodeMaps.
1.138 + ///
1.139 + /// \code
1.140 + /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
1.141 + /// edgeDescMap(graph);
1.142 + /// writer.writeUndirEdgeMap("descriptor", edgeDescMap);
1.143 + ///
1.144 + /// writer.writeUndirEdgeMap("weight", weightMap);
1.145 + /// writer.writeUndirEdgeMap("label", labelMap);
1.146 + /// \endcode
1.147 + ///
1.148 + /// The EdgeMap handling is just a syntactical sugar. It writes
1.149 + /// two undirected edge map with '+' and '-' prefix in the name.
1.150 + ///
1.151 + /// \code
1.152 + /// writer.writeEdgeMap("capacity", capacityMap);
1.153 + /// \endcode
1.154 + ///
1.155 + ///
1.156 + /// With \c writeNode() and \c writeUndirEdge() functions you can
1.157 + /// point out nodes and undirected edges in the graph. By example, you can
1.158 + /// write out the source and target of the graph.
1.159 + ///
1.160 + /// \code
1.161 + /// writer.writeNode("source", sourceNode);
1.162 + /// writer.writeNode("target", targetNode);
1.163 + ///
1.164 + /// writer.writeUndirEdge("observed", undirEdge);
1.165 + /// \endcode
1.166 + ///
1.167 + /// After you give all write commands you must call the \c run() member
1.168 + /// function, which execute all the writer commands.
1.169 + ///
1.170 + /// \code
1.171 + /// writer.run();
1.172 + /// \endcode
1.173 + ///
1.174 + /// \see DefaultWriterTraits
1.175 + /// \see QuotedStringWriter
1.176 + /// \see IdMap
1.177 + /// \see DescriptorMap
1.178 + /// \see \ref GraphWriter
1.179 + /// \see \ref graph-io-page
1.180 + /// \author Balazs Dezso
1.181 + template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
1.182 + class UndirGraphWriter {
1.183 + public:
1.184 +
1.185 + typedef _Graph Graph;
1.186 + typedef typename Graph::Node Node;
1.187 + typedef typename Graph::Edge Edge;
1.188 + typedef typename Graph::UndirEdge UndirEdge;
1.189 +
1.190 + typedef _WriterTraits WriterTraits;
1.191 +
1.192 + /// \brief Construct a new UndirGraphWriter.
1.193 + ///
1.194 + /// Construct a new UndirGraphWriter. It writes the given graph
1.195 + /// to the given stream.
1.196 + UndirGraphWriter(std::ostream& _os, const Graph& _graph)
1.197 + : writer(new LemonWriter(_os)), own_writer(true),
1.198 + nodeset_writer(*writer, _graph, std::string()),
1.199 + undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.200 + node_writer(*writer, nodeset_writer, std::string()),
1.201 + undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
1.202 + attribute_writer(*writer, std::string()) {}
1.203 +
1.204 + /// \brief Construct a new UndirGraphWriter.
1.205 + ///
1.206 + /// Construct a new UndirGraphWriter. It writes into the given graph
1.207 + /// to the given file.
1.208 + UndirGraphWriter(const std::string& _filename, const Graph& _graph)
1.209 + : writer(new LemonWriter(_filename)), own_writer(true),
1.210 + nodeset_writer(*writer, _graph, std::string()),
1.211 + undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.212 + node_writer(*writer, nodeset_writer, std::string()),
1.213 + undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
1.214 + attribute_writer(*writer, std::string()) {}
1.215 +
1.216 + /// \brief Construct a new UndirGraphWriter.
1.217 + ///
1.218 + /// Construct a new UndirGraphWriter. It writes into the given graph
1.219 + /// to given LemonReader.
1.220 + UndirGraphWriter(LemonWriter& _writer, const Graph& _graph)
1.221 + : writer(_writer), own_writer(false),
1.222 + nodeset_writer(*writer, _graph, std::string()),
1.223 + undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
1.224 + node_writer(*writer, nodeset_writer, std::string()),
1.225 + undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
1.226 + attribute_writer(*writer, std::string()) {}
1.227 +
1.228 + /// \brief Destruct the graph writer.
1.229 + ///
1.230 + /// Destruct the graph writer.
1.231 + ~UndirGraphWriter() {
1.232 + if (own_writer)
1.233 + delete writer;
1.234 + }
1.235 +
1.236 + /// \brief Add a new node map writer command for the writer.
1.237 + ///
1.238 + /// Add a new node map writer command for the writer.
1.239 + template <typename Map>
1.240 + UndirGraphWriter& writeNodeMap(std::string name, const Map& map) {
1.241 + nodeset_writer.writeNodeMap(name, map);
1.242 + return *this;
1.243 + }
1.244 +
1.245 + /// \brief Add a new node map writer command for the writer.
1.246 + ///
1.247 + /// Add a new node map writer command for the writer.
1.248 + template <typename Writer, typename Map>
1.249 + UndirGraphWriter& writeNodeMap(std::string name, const Map& map,
1.250 + const Writer& writer = Writer()) {
1.251 + nodeset_writer.writeNodeMap(name, map, writer);
1.252 + return *this;
1.253 + }
1.254 +
1.255 + /// \brief Add a new edge map writer command for the writer.
1.256 + ///
1.257 + /// Add a new edge map writer command for the writer.
1.258 + template <typename Map>
1.259 + UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) {
1.260 + undir_edgeset_writer.writeEdgeMap(name, map);
1.261 + return *this;
1.262 + }
1.263 +
1.264 + /// \brief Add a new edge map writer command for the writer.
1.265 + ///
1.266 + /// Add a new edge map writer command for the writer.
1.267 + template <typename Writer, typename Map>
1.268 + UndirGraphWriter& writeEdgeMap(std::string name, const Map& map,
1.269 + const Writer& writer = Writer()) {
1.270 + undir_edgeset_writer.writeEdgeMap(name, map, writer);
1.271 + return *this;
1.272 + }
1.273 +
1.274 + /// \brief Add a new undirected edge map writer command for the writer.
1.275 + ///
1.276 + /// Add a new undirected edge map writer command for the writer.
1.277 + template <typename Map>
1.278 + UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) {
1.279 + undir_edgeset_writer.writeUndirEdgeMap(name, map);
1.280 + return *this;
1.281 + }
1.282 +
1.283 + /// \brief Add a new undirected edge map writer command for the writer.
1.284 + ///
1.285 + /// Add a new edge undirected map writer command for the writer.
1.286 + template <typename Writer, typename Map>
1.287 + UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map,
1.288 + const Writer& writer = Writer()) {
1.289 + undir_edgeset_writer.writeUndirEdgeMap(name, map, writer);
1.290 + return *this;
1.291 + }
1.292 +
1.293 + /// \brief Add a new labeled node writer for the writer.
1.294 + ///
1.295 + /// Add a new labeled node writer for the writer.
1.296 + UndirGraphWriter& writeNode(std::string name, const Node& node) {
1.297 + node_writer.writeNode(name, node);
1.298 + return *this;
1.299 + }
1.300 +
1.301 + /// \brief Add a new labeled edge writer for the writer.
1.302 + ///
1.303 + /// Add a new labeled edge writer for the writer.
1.304 + UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) {
1.305 + undir_edge_writer.writeUndirEdge(name, edge);
1.306 + }
1.307 +
1.308 + /// \brief Add a new attribute writer command.
1.309 + ///
1.310 + /// Add a new attribute writer command.
1.311 + template <typename Value>
1.312 + UndirGraphWriter& writeAttribute(std::string name, const Value& value) {
1.313 + attribute_writer.writeAttribute(name, value);
1.314 + return *this;
1.315 + }
1.316 +
1.317 + /// \brief Add a new attribute writer command.
1.318 + ///
1.319 + /// Add a new attribute writer command.
1.320 + template <typename Writer, typename Value>
1.321 + UndirGraphWriter& writeAttribute(std::string name, const Value& value,
1.322 + const Writer& writer) {
1.323 + attribute_writer.writeAttribute<Writer>(name, value, writer);
1.324 + return *this;
1.325 + }
1.326 +
1.327 + /// \brief Conversion operator to LemonWriter.
1.328 + ///
1.329 + /// Conversion operator to LemonWriter. It make possible
1.330 + /// to access the encapsulated \e LemonWriter, this way
1.331 + /// you can attach to this writer new instances of
1.332 + /// \e LemonWriter::SectionWriter.
1.333 + operator LemonWriter&() {
1.334 + return *writer;
1.335 + }
1.336 +
1.337 + /// \brief Executes the writer commands.
1.338 + ///
1.339 + /// Executes the writer commands.
1.340 + void run() {
1.341 + writer->run();
1.342 + }
1.343 +
1.344 + private:
1.345 +
1.346 + LemonWriter* writer;
1.347 + bool own_writer;
1.348 +
1.349 + NodeSetWriter<Graph, WriterTraits> nodeset_writer;
1.350 + UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer;
1.351 +
1.352 + NodeWriter<Graph> node_writer;
1.353 + UndirEdgeWriter<Graph> undir_edge_writer;
1.354 +
1.355 + AttributeWriter<WriterTraits> attribute_writer;
1.356 + };
1.357 +
1.358 +
1.359 + /// \brief Write an undirected graph to the output.
1.360 + ///
1.361 + /// Write an undirected graph to the output.
1.362 + /// \param os The output stream.
1.363 + /// \param g The graph.
1.364 + /// \param capacity The capacity undirected map.
1.365 + template<typename Graph, typename CapacityMap>
1.366 + void writeUndirGraph(std::ostream& os, const Graph &g,
1.367 + const CapacityMap& capacity) {
1.368 + UndirGraphWriter<Graph> writer(os, g);
1.369 + writer.writeUndirEdgeMap("capacity", capacity);
1.370 + writer.run();
1.371 + }
1.372 +
1.373 + /// \brief Write an undirected graph to the output.
1.374 + ///
1.375 + /// Write an undirected graph to the output.
1.376 + /// \param os The output stream.
1.377 + /// \param g The graph.
1.378 + template<typename Graph>
1.379 + void writeUndirGraph(std::ostream& os, const Graph &g) {
1.380 + UndirGraphWriter<Graph> writer(os, g);
1.381 + writer.run();
1.382 + }
1.383 +
1.384 /// @}
1.385
1.386 }