[1137] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library |
---|
[1137] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1137] | 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[1287] | 17 | ///\ingroup io_group |
---|
[1137] | 18 | ///\file |
---|
[1287] | 19 | ///\brief Lemon Graph Format writer. |
---|
[1534] | 20 | /// |
---|
[1137] | 21 | |
---|
[1214] | 22 | #ifndef LEMON_GRAPH_WRITER_H |
---|
| 23 | #define LEMON_GRAPH_WRITER_H |
---|
[1137] | 24 | |
---|
| 25 | #include <iostream> |
---|
| 26 | |
---|
| 27 | #include <lemon/error.h> |
---|
[1409] | 28 | #include <lemon/lemon_writer.h> |
---|
[1137] | 29 | |
---|
| 30 | namespace lemon { |
---|
| 31 | |
---|
[1333] | 32 | /// \addtogroup io_group |
---|
| 33 | /// @{ |
---|
| 34 | |
---|
[1137] | 35 | /// \brief The graph writer class. |
---|
| 36 | /// |
---|
[1526] | 37 | /// The \c GraphWriter class provides the graph output. |
---|
| 38 | /// Before you read this documentation it might be useful to read the general |
---|
| 39 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
[1534] | 40 | /// If you don't need very sophisticated |
---|
| 41 | /// behaviour then you can use the versions of the public function |
---|
| 42 | /// \ref writeGraph() to output a graph (or a max flow instance etc). |
---|
| 43 | /// |
---|
[1526] | 44 | /// To write a graph |
---|
| 45 | /// you should first give writing commands to the writer. You can declare |
---|
| 46 | /// write commands as \c NodeMap or \c EdgeMap writing and labeled Node and |
---|
[1333] | 47 | /// Edge writing. |
---|
| 48 | /// |
---|
| 49 | /// \code |
---|
| 50 | /// GraphWriter<ListGraph> writer(std::cout, graph); |
---|
| 51 | /// \endcode |
---|
| 52 | /// |
---|
[1394] | 53 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
| 54 | /// command in the \c GraphWriter. You should give as parameter |
---|
| 55 | /// the name of the map and the map object. The NodeMap writing |
---|
| 56 | /// command with name "id" should write a unique map because it |
---|
[1526] | 57 | /// is regarded as ID map (such a map is essential if the graph has edges). |
---|
[1333] | 58 | /// |
---|
| 59 | /// \code |
---|
| 60 | /// IdMap<ListGraph, Node> nodeIdMap; |
---|
[1394] | 61 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
[1333] | 62 | /// |
---|
[1421] | 63 | /// writer.writeNodeMap("coords", coords); |
---|
[1394] | 64 | /// writer.writeNodeMap("color", colorMap); |
---|
[1333] | 65 | /// \endcode |
---|
| 66 | /// |
---|
[1394] | 67 | /// With the \c writeEdgeMap() member function you can give an edge map |
---|
[1333] | 68 | /// writing command similar to the NodeMaps. |
---|
| 69 | /// |
---|
| 70 | /// \code |
---|
| 71 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 72 | /// edgeDescMap(graph); |
---|
[1394] | 73 | /// writer.writeEdgeMap("descriptor", edgeDescMap); |
---|
[1333] | 74 | /// |
---|
[1394] | 75 | /// writer.writeEdgeMap("weight", weightMap); |
---|
| 76 | /// writer.writeEdgeMap("label", labelMap); |
---|
[1333] | 77 | /// \endcode |
---|
| 78 | /// |
---|
[1394] | 79 | /// With \c writeNode() and \c writeEdge() functions you can |
---|
[1526] | 80 | /// point out Nodes and Edges in the graph. For example, you can |
---|
| 81 | /// write out the source and target of a maximum flow instance. |
---|
[1333] | 82 | /// |
---|
| 83 | /// \code |
---|
[1394] | 84 | /// writer.writeNode("source", sourceNode); |
---|
| 85 | /// writer.writeNode("target", targetNode); |
---|
[1333] | 86 | /// |
---|
[1394] | 87 | /// writer.writeEdge("observed", edge); |
---|
[1333] | 88 | /// \endcode |
---|
| 89 | /// |
---|
| 90 | /// After you give all write commands you must call the \c run() member |
---|
[1526] | 91 | /// function, which executes all the writing commands. |
---|
[1333] | 92 | /// |
---|
| 93 | /// \code |
---|
| 94 | /// writer.run(); |
---|
| 95 | /// \endcode |
---|
| 96 | /// |
---|
[1287] | 97 | /// \see DefaultWriterTraits |
---|
| 98 | /// \see QuotedStringWriter |
---|
[1333] | 99 | /// \see IdMap |
---|
| 100 | /// \see DescriptorMap |
---|
[1421] | 101 | /// \see \ref GraphReader |
---|
[1138] | 102 | /// \see \ref graph-io-page |
---|
[1333] | 103 | /// \author Balazs Dezso |
---|
[1137] | 104 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
| 105 | class GraphWriter { |
---|
| 106 | public: |
---|
| 107 | |
---|
| 108 | typedef _Graph Graph; |
---|
| 109 | typedef typename Graph::Node Node; |
---|
| 110 | typedef typename Graph::Edge Edge; |
---|
| 111 | |
---|
| 112 | typedef _WriterTraits WriterTraits; |
---|
[1409] | 113 | |
---|
[1137] | 114 | /// \brief Construct a new GraphWriter. |
---|
| 115 | /// |
---|
[1526] | 116 | /// This function constructs a new GraphWriter to write the given graph |
---|
[1409] | 117 | /// to the given stream. |
---|
[1208] | 118 | GraphWriter(std::ostream& _os, const Graph& _graph) |
---|
[1409] | 119 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
[1421] | 120 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 121 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 122 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 123 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 124 | attribute_writer(*writer, std::string()) {} |
---|
[1137] | 125 | |
---|
[1409] | 126 | /// \brief Construct a new GraphWriter. |
---|
| 127 | /// |
---|
[1526] | 128 | /// This function constructs a new GraphWriter to write the given graph |
---|
[1409] | 129 | /// to the given file. |
---|
| 130 | GraphWriter(const std::string& _filename, const Graph& _graph) |
---|
| 131 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
[1421] | 132 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 133 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 134 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 135 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 136 | attribute_writer(*writer, std::string()) {} |
---|
| 137 | |
---|
| 138 | /// \brief Construct a new GraphWriter. |
---|
| 139 | /// |
---|
[1526] | 140 | /// This function constructs a new GraphWriter to write the given graph |
---|
| 141 | /// to the given LemonReader. |
---|
[1409] | 142 | GraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
| 143 | : writer(_writer), own_writer(false), |
---|
[1421] | 144 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 145 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 146 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 147 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 148 | attribute_writer(*writer, std::string()) {} |
---|
[1137] | 149 | |
---|
| 150 | /// \brief Destruct the graph writer. |
---|
| 151 | /// |
---|
[1526] | 152 | /// This function destructs the graph writer. |
---|
[1137] | 153 | ~GraphWriter() { |
---|
[1409] | 154 | if (own_writer) |
---|
| 155 | delete writer; |
---|
[1137] | 156 | } |
---|
| 157 | |
---|
[1526] | 158 | /// \brief Issue a new node map writing command for the writer. |
---|
[1137] | 159 | /// |
---|
[1526] | 160 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1137] | 161 | template <typename Map> |
---|
[1394] | 162 | GraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
[1421] | 163 | nodeset_writer.writeNodeMap(name, map); |
---|
[1409] | 164 | return *this; |
---|
[1137] | 165 | } |
---|
| 166 | |
---|
[1526] | 167 | /// \brief Issue a new node map writing command for the writer. |
---|
[1137] | 168 | /// |
---|
[1526] | 169 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1137] | 170 | template <typename Writer, typename Map> |
---|
[1394] | 171 | GraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
[1421] | 172 | const Writer& writer = Writer()) { |
---|
| 173 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
[1137] | 174 | return *this; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
[1526] | 178 | /// \brief Issue a new edge map writing command for the writer. |
---|
[1137] | 179 | /// |
---|
[1526] | 180 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1137] | 181 | template <typename Map> |
---|
[1394] | 182 | GraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
[1421] | 183 | edgeset_writer.writeEdgeMap(name, map); |
---|
[1409] | 184 | return *this; |
---|
[1137] | 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
[1526] | 188 | /// \brief Issue a new edge map writing command for the writer. |
---|
[1137] | 189 | /// |
---|
[1526] | 190 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1137] | 191 | template <typename Writer, typename Map> |
---|
[1409] | 192 | GraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
[1421] | 193 | const Writer& writer = Writer()) { |
---|
| 194 | edgeset_writer.writeEdgeMap(name, map, writer); |
---|
[1137] | 195 | return *this; |
---|
| 196 | } |
---|
| 197 | |
---|
[1526] | 198 | /// \brief Issue a new labeled node writing command to the writer. |
---|
[1137] | 199 | /// |
---|
[1526] | 200 | /// This function issues a new <i> labeled node writing command</i> |
---|
| 201 | /// to the writer. |
---|
[1394] | 202 | GraphWriter& writeNode(std::string name, const Node& node) { |
---|
[1409] | 203 | node_writer.writeNode(name, node); |
---|
[1137] | 204 | return *this; |
---|
| 205 | } |
---|
| 206 | |
---|
[1526] | 207 | /// \brief Issue a new labeled edge writing command to the writer. |
---|
[1137] | 208 | /// |
---|
[1526] | 209 | /// This function issues a new <i> labeled edge writing command</i> |
---|
| 210 | /// to the writer. |
---|
[1394] | 211 | GraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
[1409] | 212 | edge_writer.writeEdge(name, edge); |
---|
| 213 | } |
---|
| 214 | |
---|
[1526] | 215 | /// \brief Issue a new attribute writing command. |
---|
[1409] | 216 | /// |
---|
[1526] | 217 | /// This function issues a new <i> attribute writing command</i> |
---|
| 218 | /// to the writer. |
---|
[1409] | 219 | template <typename Value> |
---|
| 220 | GraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
| 221 | attribute_writer.writeAttribute(name, value); |
---|
| 222 | return *this; |
---|
| 223 | } |
---|
| 224 | |
---|
[1526] | 225 | /// \brief Issue a new attribute writing command. |
---|
[1409] | 226 | /// |
---|
[1526] | 227 | /// This function issues a new <i> attribute writing command</i> |
---|
| 228 | /// to the writer. |
---|
[1409] | 229 | template <typename Writer, typename Value> |
---|
| 230 | GraphWriter& writeAttribute(std::string name, const Value& value, |
---|
| 231 | const Writer& writer) { |
---|
| 232 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
[1137] | 233 | return *this; |
---|
| 234 | } |
---|
| 235 | |
---|
[1409] | 236 | /// \brief Conversion operator to LemonWriter. |
---|
| 237 | /// |
---|
[1526] | 238 | /// Conversion operator to LemonWriter. It makes possible |
---|
[1409] | 239 | /// to access the encapsulated \e LemonWriter, this way |
---|
| 240 | /// you can attach to this writer new instances of |
---|
| 241 | /// \e LemonWriter::SectionWriter. |
---|
| 242 | operator LemonWriter&() { |
---|
| 243 | return *writer; |
---|
[1396] | 244 | } |
---|
| 245 | |
---|
[1526] | 246 | /// \brief Executes the writing commands. |
---|
[1137] | 247 | /// |
---|
[1526] | 248 | /// Executes the writing commands. |
---|
[1409] | 249 | void run() { |
---|
| 250 | writer->run(); |
---|
[1137] | 251 | } |
---|
| 252 | |
---|
[1429] | 253 | /// \brief Write the id of the given node. |
---|
| 254 | /// |
---|
| 255 | /// It writes the id of the given node. If there was written an "id" |
---|
[1526] | 256 | /// named node map then it will write the map value belonging to the node. |
---|
[1429] | 257 | void writeId(std::ostream& os, const Node& item) const { |
---|
| 258 | nodeset_writer.writeId(os, item); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /// \brief Write the id of the given edge. |
---|
| 262 | /// |
---|
| 263 | /// It writes the id of the given edge. If there was written an "id" |
---|
[1526] | 264 | /// named edge map then it will write the map value belonging to the edge. |
---|
[1429] | 265 | void writeId(std::ostream& os, const Edge& item) const { |
---|
| 266 | edgeset_writer.writeId(os, item); |
---|
| 267 | } |
---|
| 268 | |
---|
[1137] | 269 | private: |
---|
| 270 | |
---|
[1409] | 271 | LemonWriter* writer; |
---|
| 272 | bool own_writer; |
---|
[1137] | 273 | |
---|
[1409] | 274 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
| 275 | EdgeSetWriter<Graph, WriterTraits> edgeset_writer; |
---|
| 276 | |
---|
| 277 | NodeWriter<Graph> node_writer; |
---|
| 278 | EdgeWriter<Graph> edge_writer; |
---|
| 279 | |
---|
| 280 | AttributeWriter<WriterTraits> attribute_writer; |
---|
[1137] | 281 | }; |
---|
| 282 | |
---|
[1409] | 283 | |
---|
[1534] | 284 | ///\anchor writeGraph() |
---|
| 285 | /// |
---|
[1333] | 286 | /// \brief Write a graph to the output. |
---|
| 287 | /// |
---|
| 288 | /// Write a graph to the output. |
---|
| 289 | /// \param os The output stream. |
---|
| 290 | /// \param g The graph. |
---|
[1534] | 291 | template<typename Graph> |
---|
| 292 | void writeGraph(std::ostream& os, const Graph &g) { |
---|
| 293 | GraphWriter<Graph> writer(os, g); |
---|
| 294 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
| 295 | writer.writeNodeMap("id", nodeIdMap); |
---|
| 296 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
| 297 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 298 | writer.run(); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /// \brief Write a capacitated graph instance to the output. |
---|
| 302 | /// |
---|
| 303 | /// Write a capacitated graph (graph+capacity on the |
---|
| 304 | /// edges) to the output. |
---|
| 305 | /// \param os The output stream. |
---|
| 306 | /// \param g The graph. |
---|
[1333] | 307 | /// \param capacity The capacity map. |
---|
[1534] | 308 | template<typename Graph, typename CapacityMap> |
---|
[1208] | 309 | void writeGraph(std::ostream& os, const Graph &g, |
---|
[1534] | 310 | const CapacityMap& capacity) { |
---|
[1394] | 311 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 312 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 313 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 314 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 315 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 316 | writer.writeEdgeMap("capacity", capacity); |
---|
| 317 | writer.run(); |
---|
[1208] | 318 | } |
---|
| 319 | |
---|
[1534] | 320 | /// \brief Write a shortest path instance to the output. |
---|
| 321 | /// |
---|
| 322 | /// Write a shortest path instance (graph+capacity on the |
---|
| 323 | /// edges+designated source) to the output. |
---|
| 324 | /// \param os The output stream. |
---|
| 325 | /// \param g The graph. |
---|
| 326 | /// \param capacity The capacity map. |
---|
| 327 | /// \param s The source node. |
---|
| 328 | template<typename Graph, typename CapacityMap> |
---|
| 329 | void writeGraph(std::ostream& os, const Graph &g, |
---|
| 330 | const CapacityMap& capacity, const typename Graph::Node &s) { |
---|
| 331 | GraphWriter<Graph> writer(os, g); |
---|
| 332 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
| 333 | writer.writeNodeMap("id", nodeIdMap); |
---|
| 334 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
| 335 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 336 | writer.writeEdgeMap("capacity", capacity); |
---|
| 337 | writer.writeNode("source", s); |
---|
| 338 | writer.run(); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | /// \brief Write a max flow instance to the output. |
---|
[1333] | 343 | /// |
---|
[1534] | 344 | /// Write a max flow instance (graph+capacity on the |
---|
| 345 | /// edges+designated source and target) to the output. |
---|
| 346 | /// |
---|
[1333] | 347 | /// \param os The output stream. |
---|
| 348 | /// \param g The graph. |
---|
| 349 | /// \param capacity The capacity map. |
---|
| 350 | /// \param s The source node. |
---|
| 351 | /// \param t The target node. |
---|
[1297] | 352 | template<typename Graph, typename CapacityMap> |
---|
[1208] | 353 | void writeGraph(std::ostream& os, const Graph &g, |
---|
| 354 | const CapacityMap& capacity, const typename Graph::Node &s, |
---|
| 355 | const typename Graph::Node &t) { |
---|
[1394] | 356 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 357 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 358 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 359 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 360 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 361 | writer.writeEdgeMap("capacity", capacity); |
---|
| 362 | writer.writeNode("source", s); |
---|
| 363 | writer.writeNode("target", t); |
---|
| 364 | writer.run(); |
---|
[1208] | 365 | } |
---|
| 366 | |
---|
[1534] | 367 | /// \brief Write a min cost flow instance to the output. |
---|
[1333] | 368 | /// |
---|
[1534] | 369 | /// Write a min cost flow instance (graph+capacity on the edges+cost |
---|
| 370 | /// function on the edges+designated source and target) to the output. |
---|
| 371 | /// |
---|
[1333] | 372 | /// \param os The output stream. |
---|
| 373 | /// \param g The graph. |
---|
| 374 | /// \param capacity The capacity map. |
---|
| 375 | /// \param s The source node. |
---|
[1534] | 376 | /// \param t The target node. |
---|
| 377 | /// \param cost The cost map. |
---|
| 378 | template<typename Graph, typename CapacityMap, typename CostMap> |
---|
[1208] | 379 | void writeGraph(std::ostream& os, const Graph &g, |
---|
[1534] | 380 | const CapacityMap& capacity, const typename Graph::Node &s, |
---|
| 381 | const typename Graph::Node &t, const CostMap& cost) { |
---|
[1394] | 382 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 383 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 384 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 385 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 386 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 387 | writer.writeEdgeMap("capacity", capacity); |
---|
[1534] | 388 | writer.writeEdgeMap("cost", cost); |
---|
[1394] | 389 | writer.writeNode("source", s); |
---|
[1534] | 390 | writer.writeNode("target", t); |
---|
[1394] | 391 | writer.run(); |
---|
[1208] | 392 | } |
---|
| 393 | |
---|
[1421] | 394 | /// \brief The undirected graph writer class. |
---|
| 395 | /// |
---|
| 396 | /// The \c UndirGraphWriter class provides the undir graph output. To write |
---|
[1526] | 397 | /// a graph you should first give writing commands to the writer. You can |
---|
[1421] | 398 | /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap |
---|
| 399 | /// writing and labeled Node, Edge or UndirEdge writing. |
---|
| 400 | /// |
---|
| 401 | /// \code |
---|
| 402 | /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph); |
---|
| 403 | /// \endcode |
---|
| 404 | /// |
---|
| 405 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
| 406 | /// command in the \c UndirGraphWriter. You should give as parameter |
---|
| 407 | /// the name of the map and the map object. The NodeMap writing |
---|
| 408 | /// command with name "id" should write a unique map because it |
---|
| 409 | /// is regarded as ID map. |
---|
| 410 | /// |
---|
| 411 | /// \code |
---|
| 412 | /// IdMap<UndirListGraph, Node> nodeIdMap; |
---|
| 413 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
| 414 | /// |
---|
| 415 | /// writer.writeNodeMap("coords", coords); |
---|
| 416 | /// writer.writeNodeMap("color", colorMap); |
---|
| 417 | /// \endcode |
---|
| 418 | /// |
---|
| 419 | /// With the \c writeUndirEdgeMap() member function you can give an |
---|
| 420 | /// undirected edge map writing command similar to the NodeMaps. |
---|
| 421 | /// |
---|
| 422 | /// \code |
---|
| 423 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 424 | /// edgeDescMap(graph); |
---|
| 425 | /// writer.writeUndirEdgeMap("descriptor", edgeDescMap); |
---|
| 426 | /// |
---|
| 427 | /// writer.writeUndirEdgeMap("weight", weightMap); |
---|
| 428 | /// writer.writeUndirEdgeMap("label", labelMap); |
---|
| 429 | /// \endcode |
---|
| 430 | /// |
---|
| 431 | /// The EdgeMap handling is just a syntactical sugar. It writes |
---|
| 432 | /// two undirected edge map with '+' and '-' prefix in the name. |
---|
| 433 | /// |
---|
| 434 | /// \code |
---|
| 435 | /// writer.writeEdgeMap("capacity", capacityMap); |
---|
| 436 | /// \endcode |
---|
| 437 | /// |
---|
| 438 | /// |
---|
| 439 | /// With \c writeNode() and \c writeUndirEdge() functions you can |
---|
[1526] | 440 | /// designate nodes and undirected edges in the graph. For example, you can |
---|
[1421] | 441 | /// write out the source and target of the graph. |
---|
| 442 | /// |
---|
| 443 | /// \code |
---|
| 444 | /// writer.writeNode("source", sourceNode); |
---|
| 445 | /// writer.writeNode("target", targetNode); |
---|
| 446 | /// |
---|
| 447 | /// writer.writeUndirEdge("observed", undirEdge); |
---|
| 448 | /// \endcode |
---|
| 449 | /// |
---|
| 450 | /// After you give all write commands you must call the \c run() member |
---|
[1526] | 451 | /// function, which executes all the writing commands. |
---|
[1421] | 452 | /// |
---|
| 453 | /// \code |
---|
| 454 | /// writer.run(); |
---|
| 455 | /// \endcode |
---|
| 456 | /// |
---|
| 457 | /// \see DefaultWriterTraits |
---|
| 458 | /// \see QuotedStringWriter |
---|
| 459 | /// \see IdMap |
---|
| 460 | /// \see DescriptorMap |
---|
| 461 | /// \see \ref GraphWriter |
---|
| 462 | /// \see \ref graph-io-page |
---|
| 463 | /// \author Balazs Dezso |
---|
| 464 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
| 465 | class UndirGraphWriter { |
---|
| 466 | public: |
---|
| 467 | |
---|
| 468 | typedef _Graph Graph; |
---|
| 469 | typedef typename Graph::Node Node; |
---|
| 470 | typedef typename Graph::Edge Edge; |
---|
| 471 | typedef typename Graph::UndirEdge UndirEdge; |
---|
| 472 | |
---|
| 473 | typedef _WriterTraits WriterTraits; |
---|
| 474 | |
---|
| 475 | /// \brief Construct a new UndirGraphWriter. |
---|
| 476 | /// |
---|
| 477 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
| 478 | /// to the given stream. |
---|
| 479 | UndirGraphWriter(std::ostream& _os, const Graph& _graph) |
---|
| 480 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
| 481 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 482 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 483 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 484 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 485 | attribute_writer(*writer, std::string()) {} |
---|
| 486 | |
---|
| 487 | /// \brief Construct a new UndirGraphWriter. |
---|
| 488 | /// |
---|
[1526] | 489 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
[1421] | 490 | /// to the given file. |
---|
| 491 | UndirGraphWriter(const std::string& _filename, const Graph& _graph) |
---|
| 492 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
| 493 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 494 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 495 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 496 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 497 | attribute_writer(*writer, std::string()) {} |
---|
| 498 | |
---|
| 499 | /// \brief Construct a new UndirGraphWriter. |
---|
| 500 | /// |
---|
[1526] | 501 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
[1421] | 502 | /// to given LemonReader. |
---|
| 503 | UndirGraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
| 504 | : writer(_writer), own_writer(false), |
---|
| 505 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 506 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 507 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 508 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 509 | attribute_writer(*writer, std::string()) {} |
---|
| 510 | |
---|
| 511 | /// \brief Destruct the graph writer. |
---|
| 512 | /// |
---|
| 513 | /// Destruct the graph writer. |
---|
| 514 | ~UndirGraphWriter() { |
---|
| 515 | if (own_writer) |
---|
| 516 | delete writer; |
---|
| 517 | } |
---|
| 518 | |
---|
[1526] | 519 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 520 | /// |
---|
[1526] | 521 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 522 | template <typename Map> |
---|
| 523 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
| 524 | nodeset_writer.writeNodeMap(name, map); |
---|
| 525 | return *this; |
---|
| 526 | } |
---|
| 527 | |
---|
[1526] | 528 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 529 | /// |
---|
[1526] | 530 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 531 | template <typename Writer, typename Map> |
---|
| 532 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
| 533 | const Writer& writer = Writer()) { |
---|
| 534 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
| 535 | return *this; |
---|
| 536 | } |
---|
| 537 | |
---|
[1526] | 538 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 539 | /// |
---|
[1526] | 540 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 541 | template <typename Map> |
---|
| 542 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
| 543 | undir_edgeset_writer.writeEdgeMap(name, map); |
---|
| 544 | return *this; |
---|
| 545 | } |
---|
| 546 | |
---|
[1526] | 547 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 548 | /// |
---|
[1526] | 549 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 550 | template <typename Writer, typename Map> |
---|
| 551 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
| 552 | const Writer& writer = Writer()) { |
---|
| 553 | undir_edgeset_writer.writeEdgeMap(name, map, writer); |
---|
| 554 | return *this; |
---|
| 555 | } |
---|
| 556 | |
---|
[1526] | 557 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 558 | /// |
---|
[1526] | 559 | /// This function issues a new <i> undirected edge map writing |
---|
| 560 | /// command</i> to the writer. |
---|
[1421] | 561 | template <typename Map> |
---|
| 562 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) { |
---|
| 563 | undir_edgeset_writer.writeUndirEdgeMap(name, map); |
---|
| 564 | return *this; |
---|
| 565 | } |
---|
| 566 | |
---|
[1526] | 567 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 568 | /// |
---|
[1526] | 569 | /// This function issues a new <i> undirected edge map writing |
---|
| 570 | /// command</i> to the writer. |
---|
[1421] | 571 | template <typename Writer, typename Map> |
---|
| 572 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map, |
---|
| 573 | const Writer& writer = Writer()) { |
---|
| 574 | undir_edgeset_writer.writeUndirEdgeMap(name, map, writer); |
---|
| 575 | return *this; |
---|
| 576 | } |
---|
| 577 | |
---|
[1526] | 578 | /// \brief Issue a new labeled node writer to the writer. |
---|
[1421] | 579 | /// |
---|
[1526] | 580 | /// This function issues a new <i> labeled node writing |
---|
| 581 | /// command</i> to the writer. |
---|
[1421] | 582 | UndirGraphWriter& writeNode(std::string name, const Node& node) { |
---|
| 583 | node_writer.writeNode(name, node); |
---|
| 584 | return *this; |
---|
| 585 | } |
---|
| 586 | |
---|
[1526] | 587 | /// \brief Issue a new labeled edge writer to the writer. |
---|
[1421] | 588 | /// |
---|
[1526] | 589 | /// This function issues a new <i> labeled edge writing |
---|
| 590 | /// command</i> to the writer. |
---|
[1429] | 591 | UndirGraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
| 592 | undir_edge_writer.writeEdge(name, edge); |
---|
| 593 | } |
---|
| 594 | |
---|
[1526] | 595 | /// \brief Issue a new labeled undirected edge writing command to |
---|
| 596 | /// the writer. |
---|
[1429] | 597 | /// |
---|
[1526] | 598 | /// Issue a new <i>labeled undirected edge writing command</i> to |
---|
| 599 | /// the writer. |
---|
[1421] | 600 | UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) { |
---|
| 601 | undir_edge_writer.writeUndirEdge(name, edge); |
---|
| 602 | } |
---|
| 603 | |
---|
[1526] | 604 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 605 | /// |
---|
[1526] | 606 | /// This function issues a new <i> attribute writing |
---|
| 607 | /// command</i> to the writer. |
---|
[1421] | 608 | template <typename Value> |
---|
| 609 | UndirGraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
| 610 | attribute_writer.writeAttribute(name, value); |
---|
| 611 | return *this; |
---|
| 612 | } |
---|
| 613 | |
---|
[1526] | 614 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 615 | /// |
---|
[1526] | 616 | /// This function issues a new <i> attribute writing |
---|
| 617 | /// command</i> to the writer. |
---|
[1421] | 618 | template <typename Writer, typename Value> |
---|
| 619 | UndirGraphWriter& writeAttribute(std::string name, const Value& value, |
---|
| 620 | const Writer& writer) { |
---|
| 621 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
| 622 | return *this; |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | /// \brief Conversion operator to LemonWriter. |
---|
| 626 | /// |
---|
[1526] | 627 | /// Conversion operator to LemonWriter. It makes possible |
---|
[1421] | 628 | /// to access the encapsulated \e LemonWriter, this way |
---|
| 629 | /// you can attach to this writer new instances of |
---|
| 630 | /// \e LemonWriter::SectionWriter. |
---|
| 631 | operator LemonWriter&() { |
---|
| 632 | return *writer; |
---|
| 633 | } |
---|
| 634 | |
---|
[1526] | 635 | /// \brief Executes the writing commands. |
---|
[1421] | 636 | /// |
---|
[1526] | 637 | /// Executes the writing commands. |
---|
[1421] | 638 | void run() { |
---|
| 639 | writer->run(); |
---|
| 640 | } |
---|
| 641 | |
---|
[1429] | 642 | /// \brief Write the id of the given node. |
---|
| 643 | /// |
---|
| 644 | /// It writes the id of the given node. If there was written an "id" |
---|
[1526] | 645 | /// named node map then it will write the map value belonging to the node. |
---|
[1429] | 646 | void writeId(std::ostream& os, const Node& item) const { |
---|
| 647 | nodeset_writer.writeId(os, item); |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | /// \brief Write the id of the given edge. |
---|
| 651 | /// |
---|
| 652 | /// It writes the id of the given edge. If there was written an "id" |
---|
[1526] | 653 | /// named edge map then it will write the map value belonging to the edge. |
---|
[1429] | 654 | void writeId(std::ostream& os, const Edge& item) const { |
---|
| 655 | undir_edgeset_writer.writeId(os, item); |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | /// \brief Write the id of the given undirected edge. |
---|
| 659 | /// |
---|
| 660 | /// It writes the id of the given undirected edge. If there was written |
---|
[1526] | 661 | /// an "id" named edge map then it will write the map value belonging to |
---|
[1429] | 662 | /// the edge. |
---|
| 663 | void writeId(std::ostream& os, const UndirEdge& item) const { |
---|
| 664 | undir_edgeset_writer.writeId(os, item); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | |
---|
[1421] | 668 | private: |
---|
| 669 | |
---|
| 670 | LemonWriter* writer; |
---|
| 671 | bool own_writer; |
---|
| 672 | |
---|
| 673 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
| 674 | UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer; |
---|
| 675 | |
---|
| 676 | NodeWriter<Graph> node_writer; |
---|
| 677 | UndirEdgeWriter<Graph> undir_edge_writer; |
---|
| 678 | |
---|
| 679 | AttributeWriter<WriterTraits> attribute_writer; |
---|
| 680 | }; |
---|
| 681 | |
---|
[1534] | 682 | /// \brief Write an undirected graph to the output. |
---|
| 683 | /// |
---|
| 684 | /// Write an undirected graph to the output. |
---|
| 685 | /// \param os The output stream. |
---|
| 686 | /// \param g The graph. |
---|
| 687 | template<typename Graph> |
---|
| 688 | void writeUndirGraph(std::ostream& os, const Graph &g) { |
---|
| 689 | UndirGraphWriter<Graph> writer(os, g); |
---|
| 690 | writer.run(); |
---|
| 691 | } |
---|
[1421] | 692 | |
---|
[1526] | 693 | /// \brief Write an undirected multigraph (undirected graph + capacity |
---|
| 694 | /// map on the edges) to the output. |
---|
[1421] | 695 | /// |
---|
[1526] | 696 | /// Write an undirected multigraph (undirected graph + capacity |
---|
| 697 | /// map on the edges) to the output. |
---|
[1421] | 698 | /// \param os The output stream. |
---|
| 699 | /// \param g The graph. |
---|
| 700 | /// \param capacity The capacity undirected map. |
---|
| 701 | template<typename Graph, typename CapacityMap> |
---|
| 702 | void writeUndirGraph(std::ostream& os, const Graph &g, |
---|
| 703 | const CapacityMap& capacity) { |
---|
| 704 | UndirGraphWriter<Graph> writer(os, g); |
---|
| 705 | writer.writeUndirEdgeMap("capacity", capacity); |
---|
| 706 | writer.run(); |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | |
---|
[1333] | 710 | /// @} |
---|
[1137] | 711 | |
---|
| 712 | } |
---|
[1214] | 713 | |
---|
| 714 | #endif |
---|