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