[1137] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1137] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1287] | 19 | ///\ingroup io_group |
---|
[1137] | 20 | ///\file |
---|
[1287] | 21 | ///\brief Lemon Graph Format writer. |
---|
[1534] | 22 | /// |
---|
[1137] | 23 | |
---|
[1214] | 24 | #ifndef LEMON_GRAPH_WRITER_H |
---|
| 25 | #define LEMON_GRAPH_WRITER_H |
---|
[1137] | 26 | |
---|
| 27 | #include <iostream> |
---|
| 28 | |
---|
| 29 | #include <lemon/error.h> |
---|
[1409] | 30 | #include <lemon/lemon_writer.h> |
---|
[1137] | 31 | |
---|
| 32 | namespace lemon { |
---|
| 33 | |
---|
[1333] | 34 | /// \addtogroup io_group |
---|
| 35 | /// @{ |
---|
| 36 | |
---|
[1137] | 37 | /// \brief The graph writer class. |
---|
| 38 | /// |
---|
[1526] | 39 | /// The \c GraphWriter class provides the graph output. |
---|
| 40 | /// Before you read this documentation it might be useful to read the general |
---|
| 41 | /// description of \ref graph-io-page "Graph Input-Output". |
---|
[1540] | 42 | /// |
---|
[1526] | 43 | /// To write a graph |
---|
| 44 | /// you should first give writing commands to the writer. You can declare |
---|
| 45 | /// write commands as \c NodeMap or \c EdgeMap writing and labeled Node and |
---|
[1333] | 46 | /// Edge writing. |
---|
| 47 | /// |
---|
[1946] | 48 | ///\code |
---|
[1333] | 49 | /// GraphWriter<ListGraph> writer(std::cout, graph); |
---|
[1946] | 50 | ///\endcode |
---|
[1333] | 51 | /// |
---|
[1394] | 52 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
| 53 | /// command in the \c GraphWriter. You should give as parameter |
---|
| 54 | /// the name of the map and the map object. The NodeMap writing |
---|
[1901] | 55 | /// command with name "label" should write a unique map because it |
---|
| 56 | /// is regarded as label map (such a map is essential if the graph has edges). |
---|
[1333] | 57 | /// |
---|
[1946] | 58 | ///\code |
---|
[1901] | 59 | /// IdMap<ListGraph, Node> nodeLabelMap; |
---|
| 60 | /// writer.writeNodeMap("label", nodeLabelMap); |
---|
[1333] | 61 | /// |
---|
[1421] | 62 | /// writer.writeNodeMap("coords", coords); |
---|
[1394] | 63 | /// writer.writeNodeMap("color", colorMap); |
---|
[1946] | 64 | ///\endcode |
---|
[1333] | 65 | /// |
---|
[1394] | 66 | /// With the \c writeEdgeMap() member function you can give an edge map |
---|
[1333] | 67 | /// writing command similar to the NodeMaps. |
---|
| 68 | /// |
---|
[1946] | 69 | ///\code |
---|
[1333] | 70 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 71 | /// edgeDescMap(graph); |
---|
[1394] | 72 | /// writer.writeEdgeMap("descriptor", edgeDescMap); |
---|
[1333] | 73 | /// |
---|
[1394] | 74 | /// writer.writeEdgeMap("weight", weightMap); |
---|
| 75 | /// writer.writeEdgeMap("label", labelMap); |
---|
[1946] | 76 | ///\endcode |
---|
[1333] | 77 | /// |
---|
[1394] | 78 | /// With \c writeNode() and \c writeEdge() functions you can |
---|
[1526] | 79 | /// point out Nodes and Edges in the graph. For example, you can |
---|
| 80 | /// write out the source and target of a maximum flow instance. |
---|
[1333] | 81 | /// |
---|
[1946] | 82 | ///\code |
---|
[1394] | 83 | /// writer.writeNode("source", sourceNode); |
---|
| 84 | /// writer.writeNode("target", targetNode); |
---|
[1333] | 85 | /// |
---|
[1394] | 86 | /// writer.writeEdge("observed", edge); |
---|
[1946] | 87 | ///\endcode |
---|
[1333] | 88 | /// |
---|
| 89 | /// After you give all write commands you must call the \c run() member |
---|
[1526] | 90 | /// function, which executes all the writing commands. |
---|
[1333] | 91 | /// |
---|
[1946] | 92 | ///\code |
---|
[1333] | 93 | /// writer.run(); |
---|
[1946] | 94 | ///\endcode |
---|
[1333] | 95 | /// |
---|
[1287] | 96 | /// \see DefaultWriterTraits |
---|
| 97 | /// \see QuotedStringWriter |
---|
[1333] | 98 | /// \see IdMap |
---|
| 99 | /// \see DescriptorMap |
---|
[1421] | 100 | /// \see \ref GraphReader |
---|
[1138] | 101 | /// \see \ref graph-io-page |
---|
[1333] | 102 | /// \author Balazs Dezso |
---|
[1137] | 103 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
| 104 | class GraphWriter { |
---|
| 105 | public: |
---|
| 106 | |
---|
| 107 | typedef _Graph Graph; |
---|
| 108 | typedef typename Graph::Node Node; |
---|
| 109 | typedef typename Graph::Edge Edge; |
---|
| 110 | |
---|
| 111 | typedef _WriterTraits WriterTraits; |
---|
[1409] | 112 | |
---|
[1137] | 113 | /// \brief Construct a new GraphWriter. |
---|
| 114 | /// |
---|
[1526] | 115 | /// This function constructs a new GraphWriter to write the given graph |
---|
[1409] | 116 | /// to the given stream. |
---|
[1208] | 117 | GraphWriter(std::ostream& _os, const Graph& _graph) |
---|
[1409] | 118 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
[1421] | 119 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 120 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 121 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 122 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 123 | attribute_writer(*writer, std::string()) {} |
---|
[1137] | 124 | |
---|
[1409] | 125 | /// \brief Construct a new GraphWriter. |
---|
| 126 | /// |
---|
[1526] | 127 | /// This function constructs a new GraphWriter to write the given graph |
---|
[1409] | 128 | /// to the given file. |
---|
| 129 | GraphWriter(const std::string& _filename, const Graph& _graph) |
---|
| 130 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
[1421] | 131 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 132 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 133 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 134 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 135 | attribute_writer(*writer, std::string()) {} |
---|
| 136 | |
---|
| 137 | /// \brief Construct a new GraphWriter. |
---|
| 138 | /// |
---|
[1526] | 139 | /// This function constructs a new GraphWriter to write the given graph |
---|
| 140 | /// to the given LemonReader. |
---|
[1409] | 141 | GraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
| 142 | : writer(_writer), own_writer(false), |
---|
[1421] | 143 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 144 | edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1409] | 145 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 146 | edge_writer(*writer, edgeset_writer, std::string()), |
---|
| 147 | attribute_writer(*writer, std::string()) {} |
---|
[1137] | 148 | |
---|
| 149 | /// \brief Destruct the graph writer. |
---|
| 150 | /// |
---|
[1526] | 151 | /// This function destructs the graph writer. |
---|
[1137] | 152 | ~GraphWriter() { |
---|
[1409] | 153 | if (own_writer) |
---|
| 154 | delete writer; |
---|
[1137] | 155 | } |
---|
| 156 | |
---|
[1526] | 157 | /// \brief Issue a new node map writing command for the writer. |
---|
[1137] | 158 | /// |
---|
[1526] | 159 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1137] | 160 | template <typename Map> |
---|
[1394] | 161 | GraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
[1421] | 162 | nodeset_writer.writeNodeMap(name, map); |
---|
[1409] | 163 | return *this; |
---|
[1137] | 164 | } |
---|
| 165 | |
---|
[1540] | 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 |
---|
[1540] | 241 | /// \e LemonWriter::SectionWriter. For more details see |
---|
| 242 | /// the \ref rwbackground "Background of Reading and Writing". |
---|
[1409] | 243 | operator LemonWriter&() { |
---|
| 244 | return *writer; |
---|
[1396] | 245 | } |
---|
| 246 | |
---|
[1526] | 247 | /// \brief Executes the writing commands. |
---|
[1137] | 248 | /// |
---|
[1526] | 249 | /// Executes the writing commands. |
---|
[1409] | 250 | void run() { |
---|
| 251 | writer->run(); |
---|
[1137] | 252 | } |
---|
| 253 | |
---|
[1901] | 254 | /// \brief Write the label of the given node. |
---|
[1429] | 255 | /// |
---|
[1901] | 256 | /// It writes the label of the given node. If there was written an "label" |
---|
[1526] | 257 | /// named node map then it will write the map value belonging to the node. |
---|
[1901] | 258 | void writeLabel(std::ostream& os, const Node& item) const { |
---|
| 259 | nodeset_writer.writeLabel(os, item); |
---|
[1429] | 260 | } |
---|
| 261 | |
---|
[1901] | 262 | /// \brief Write the label of the given edge. |
---|
[1429] | 263 | /// |
---|
[1901] | 264 | /// It writes the label of the given edge. If there was written an "label" |
---|
[1526] | 265 | /// named edge map then it will write the map value belonging to the edge. |
---|
[1901] | 266 | void writeLabel(std::ostream& os, const Edge& item) const { |
---|
| 267 | edgeset_writer.writeLabel(os, item); |
---|
[1429] | 268 | } |
---|
| 269 | |
---|
[1137] | 270 | private: |
---|
| 271 | |
---|
[1409] | 272 | LemonWriter* writer; |
---|
| 273 | bool own_writer; |
---|
[1137] | 274 | |
---|
[1409] | 275 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
| 276 | EdgeSetWriter<Graph, WriterTraits> edgeset_writer; |
---|
| 277 | |
---|
| 278 | NodeWriter<Graph> node_writer; |
---|
| 279 | EdgeWriter<Graph> edge_writer; |
---|
| 280 | |
---|
| 281 | AttributeWriter<WriterTraits> attribute_writer; |
---|
[1137] | 282 | }; |
---|
| 283 | |
---|
[1409] | 284 | |
---|
[1744] | 285 | |
---|
[1333] | 286 | /// \brief Write a graph to the output. |
---|
| 287 | /// |
---|
[1744] | 288 | /// It is a helper function to write a graph to the given output |
---|
| 289 | /// stream. It gives back a GraphWriter object and this object |
---|
| 290 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 291 | /// \warning Do not forget to call the \c run() function. |
---|
[1534] | 292 | /// |
---|
[1333] | 293 | /// \param os The output stream. |
---|
| 294 | /// \param g The graph. |
---|
[1744] | 295 | template <typename Graph> |
---|
| 296 | GraphWriter<Graph> graphWriter(std::ostream& os, const Graph &g) { |
---|
| 297 | return GraphWriter<Graph>(os, g); |
---|
[1208] | 298 | } |
---|
| 299 | |
---|
[1744] | 300 | /// \brief Write a graph to the output. |
---|
[1333] | 301 | /// |
---|
[1744] | 302 | /// It is a helper function to write a graph to the given output |
---|
| 303 | /// file. It gives back a GraphWriter object and this object |
---|
| 304 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 305 | /// \warning Do not forget to call the \c run() function. |
---|
[1534] | 306 | /// |
---|
[1744] | 307 | /// \param fn The filename. |
---|
[1333] | 308 | /// \param g The graph. |
---|
[1744] | 309 | template <typename Graph> |
---|
| 310 | GraphWriter<Graph> graphWriter(const std::string& fn, const Graph &g) { |
---|
| 311 | return GraphWriter<Graph>(fn, g); |
---|
[1208] | 312 | } |
---|
| 313 | |
---|
[1421] | 314 | /// \brief The undirected graph writer class. |
---|
| 315 | /// |
---|
[1909] | 316 | /// The \c UGraphWriter class provides the ugraph output. To write |
---|
[1526] | 317 | /// a graph you should first give writing commands to the writer. You can |
---|
[1909] | 318 | /// declare write command as \c NodeMap, \c EdgeMap or \c UEdgeMap |
---|
| 319 | /// writing and labeled Node, Edge or UEdge writing. |
---|
[1421] | 320 | /// |
---|
[1946] | 321 | ///\code |
---|
[1909] | 322 | /// UGraphWriter<ListUGraph> writer(std::cout, graph); |
---|
[1946] | 323 | ///\endcode |
---|
[1421] | 324 | /// |
---|
| 325 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
[1909] | 326 | /// command in the \c UGraphWriter. You should give as parameter |
---|
[1421] | 327 | /// the name of the map and the map object. The NodeMap writing |
---|
[1901] | 328 | /// command with name "label" should write a unique map because it |
---|
| 329 | /// is regarded as label map. |
---|
[1421] | 330 | /// |
---|
[1946] | 331 | ///\code |
---|
[1909] | 332 | /// IdMap<ListUGraph, Node> nodeLabelMap; |
---|
[1901] | 333 | /// writer.writeNodeMap("label", nodeLabelMap); |
---|
[1421] | 334 | /// |
---|
| 335 | /// writer.writeNodeMap("coords", coords); |
---|
| 336 | /// writer.writeNodeMap("color", colorMap); |
---|
[1946] | 337 | ///\endcode |
---|
[1421] | 338 | /// |
---|
[1909] | 339 | /// With the \c writeUEdgeMap() member function you can give an |
---|
[1421] | 340 | /// undirected edge map writing command similar to the NodeMaps. |
---|
| 341 | /// |
---|
[1946] | 342 | ///\code |
---|
[1421] | 343 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 344 | /// edgeDescMap(graph); |
---|
[1909] | 345 | /// writer.writeUEdgeMap("descriptor", edgeDescMap); |
---|
[1421] | 346 | /// |
---|
[1909] | 347 | /// writer.writeUEdgeMap("weight", weightMap); |
---|
| 348 | /// writer.writeUEdgeMap("label", labelMap); |
---|
[1946] | 349 | ///\endcode |
---|
[1421] | 350 | /// |
---|
| 351 | /// The EdgeMap handling is just a syntactical sugar. It writes |
---|
| 352 | /// two undirected edge map with '+' and '-' prefix in the name. |
---|
| 353 | /// |
---|
[1946] | 354 | ///\code |
---|
[1421] | 355 | /// writer.writeEdgeMap("capacity", capacityMap); |
---|
[1946] | 356 | ///\endcode |
---|
[1421] | 357 | /// |
---|
| 358 | /// |
---|
[1909] | 359 | /// With \c writeNode() and \c writeUEdge() functions you can |
---|
[1526] | 360 | /// designate nodes and undirected edges in the graph. For example, you can |
---|
[1421] | 361 | /// write out the source and target of the graph. |
---|
| 362 | /// |
---|
[1946] | 363 | ///\code |
---|
[1421] | 364 | /// writer.writeNode("source", sourceNode); |
---|
| 365 | /// writer.writeNode("target", targetNode); |
---|
| 366 | /// |
---|
[1909] | 367 | /// writer.writeUEdge("observed", uEdge); |
---|
[1946] | 368 | ///\endcode |
---|
[1421] | 369 | /// |
---|
| 370 | /// After you give all write commands you must call the \c run() member |
---|
[1526] | 371 | /// function, which executes all the writing commands. |
---|
[1421] | 372 | /// |
---|
[1946] | 373 | ///\code |
---|
[1421] | 374 | /// writer.run(); |
---|
[1946] | 375 | ///\endcode |
---|
[1421] | 376 | /// |
---|
| 377 | /// \see DefaultWriterTraits |
---|
| 378 | /// \see QuotedStringWriter |
---|
| 379 | /// \see IdMap |
---|
| 380 | /// \see DescriptorMap |
---|
| 381 | /// \see \ref GraphWriter |
---|
| 382 | /// \see \ref graph-io-page |
---|
| 383 | /// \author Balazs Dezso |
---|
| 384 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
[1909] | 385 | class UGraphWriter { |
---|
[1421] | 386 | public: |
---|
| 387 | |
---|
| 388 | typedef _Graph Graph; |
---|
| 389 | typedef typename Graph::Node Node; |
---|
| 390 | typedef typename Graph::Edge Edge; |
---|
[1909] | 391 | typedef typename Graph::UEdge UEdge; |
---|
[1421] | 392 | |
---|
| 393 | typedef _WriterTraits WriterTraits; |
---|
| 394 | |
---|
[1909] | 395 | /// \brief Construct a new UGraphWriter. |
---|
[1421] | 396 | /// |
---|
[1909] | 397 | /// Construct a new UGraphWriter. It writes the given graph |
---|
[1421] | 398 | /// to the given stream. |
---|
[1909] | 399 | UGraphWriter(std::ostream& _os, const Graph& _graph) |
---|
[1421] | 400 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
| 401 | nodeset_writer(*writer, _graph, std::string()), |
---|
[1909] | 402 | u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1421] | 403 | node_writer(*writer, nodeset_writer, std::string()), |
---|
[1909] | 404 | u_edge_writer(*writer, u_edgeset_writer, std::string()), |
---|
[1421] | 405 | attribute_writer(*writer, std::string()) {} |
---|
| 406 | |
---|
[1909] | 407 | /// \brief Construct a new UGraphWriter. |
---|
[1421] | 408 | /// |
---|
[1909] | 409 | /// Construct a new UGraphWriter. It writes the given graph |
---|
[1421] | 410 | /// to the given file. |
---|
[1909] | 411 | UGraphWriter(const std::string& _filename, const Graph& _graph) |
---|
[1421] | 412 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
| 413 | nodeset_writer(*writer, _graph, std::string()), |
---|
[1909] | 414 | u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1421] | 415 | node_writer(*writer, nodeset_writer, std::string()), |
---|
[1909] | 416 | u_edge_writer(*writer, u_edgeset_writer, std::string()), |
---|
[1421] | 417 | attribute_writer(*writer, std::string()) {} |
---|
| 418 | |
---|
[1909] | 419 | /// \brief Construct a new UGraphWriter. |
---|
[1421] | 420 | /// |
---|
[1909] | 421 | /// Construct a new UGraphWriter. It writes the given graph |
---|
[1421] | 422 | /// to given LemonReader. |
---|
[1909] | 423 | UGraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
[1421] | 424 | : writer(_writer), own_writer(false), |
---|
| 425 | nodeset_writer(*writer, _graph, std::string()), |
---|
[1909] | 426 | u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
[1421] | 427 | node_writer(*writer, nodeset_writer, std::string()), |
---|
[1909] | 428 | u_edge_writer(*writer, u_edgeset_writer, std::string()), |
---|
[1421] | 429 | attribute_writer(*writer, std::string()) {} |
---|
| 430 | |
---|
| 431 | /// \brief Destruct the graph writer. |
---|
| 432 | /// |
---|
| 433 | /// Destruct the graph writer. |
---|
[1909] | 434 | ~UGraphWriter() { |
---|
[1421] | 435 | if (own_writer) |
---|
| 436 | delete writer; |
---|
| 437 | } |
---|
| 438 | |
---|
[1526] | 439 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 440 | /// |
---|
[1526] | 441 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 442 | template <typename Map> |
---|
[1909] | 443 | UGraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
[1421] | 444 | nodeset_writer.writeNodeMap(name, map); |
---|
| 445 | return *this; |
---|
| 446 | } |
---|
| 447 | |
---|
[1526] | 448 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 449 | /// |
---|
[1526] | 450 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 451 | template <typename Writer, typename Map> |
---|
[1909] | 452 | UGraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
[1421] | 453 | const Writer& writer = Writer()) { |
---|
| 454 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
| 455 | return *this; |
---|
| 456 | } |
---|
| 457 | |
---|
[1526] | 458 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 459 | /// |
---|
[1526] | 460 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 461 | template <typename Map> |
---|
[1909] | 462 | UGraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
| 463 | u_edgeset_writer.writeEdgeMap(name, map); |
---|
[1421] | 464 | return *this; |
---|
| 465 | } |
---|
| 466 | |
---|
[1526] | 467 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 468 | /// |
---|
[1526] | 469 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 470 | template <typename Writer, typename Map> |
---|
[1909] | 471 | UGraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
[1421] | 472 | const Writer& writer = Writer()) { |
---|
[1909] | 473 | u_edgeset_writer.writeEdgeMap(name, map, writer); |
---|
[1421] | 474 | return *this; |
---|
| 475 | } |
---|
| 476 | |
---|
[1526] | 477 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 478 | /// |
---|
[1526] | 479 | /// This function issues a new <i> undirected edge map writing |
---|
| 480 | /// command</i> to the writer. |
---|
[1421] | 481 | template <typename Map> |
---|
[1909] | 482 | UGraphWriter& writeUEdgeMap(std::string name, const Map& map) { |
---|
| 483 | u_edgeset_writer.writeUEdgeMap(name, map); |
---|
[1421] | 484 | return *this; |
---|
| 485 | } |
---|
| 486 | |
---|
[1526] | 487 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 488 | /// |
---|
[1526] | 489 | /// This function issues a new <i> undirected edge map writing |
---|
| 490 | /// command</i> to the writer. |
---|
[1540] | 491 | template <typename Writer, typename Map> |
---|
[1909] | 492 | UGraphWriter& writeUEdgeMap(std::string name, const Map& map, |
---|
[1421] | 493 | const Writer& writer = Writer()) { |
---|
[1909] | 494 | u_edgeset_writer.writeUEdgeMap(name, map, writer); |
---|
[1421] | 495 | return *this; |
---|
| 496 | } |
---|
| 497 | |
---|
[1526] | 498 | /// \brief Issue a new labeled node writer to the writer. |
---|
[1421] | 499 | /// |
---|
[1526] | 500 | /// This function issues a new <i> labeled node writing |
---|
| 501 | /// command</i> to the writer. |
---|
[1909] | 502 | UGraphWriter& writeNode(std::string name, const Node& node) { |
---|
[1421] | 503 | node_writer.writeNode(name, node); |
---|
| 504 | return *this; |
---|
| 505 | } |
---|
| 506 | |
---|
[1526] | 507 | /// \brief Issue a new labeled edge writer to the writer. |
---|
[1421] | 508 | /// |
---|
[1526] | 509 | /// This function issues a new <i> labeled edge writing |
---|
| 510 | /// command</i> to the writer. |
---|
[1909] | 511 | UGraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
| 512 | u_edge_writer.writeEdge(name, edge); |
---|
[1429] | 513 | } |
---|
| 514 | |
---|
[1526] | 515 | /// \brief Issue a new labeled undirected edge writing command to |
---|
| 516 | /// the writer. |
---|
[1429] | 517 | /// |
---|
[1526] | 518 | /// Issue a new <i>labeled undirected edge writing command</i> to |
---|
| 519 | /// the writer. |
---|
[1909] | 520 | UGraphWriter& writeUEdge(std::string name, const UEdge& edge) { |
---|
| 521 | u_edge_writer.writeUEdge(name, edge); |
---|
[1421] | 522 | } |
---|
| 523 | |
---|
[1526] | 524 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 525 | /// |
---|
[1526] | 526 | /// This function issues a new <i> attribute writing |
---|
| 527 | /// command</i> to the writer. |
---|
[1421] | 528 | template <typename Value> |
---|
[1909] | 529 | UGraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
[1421] | 530 | attribute_writer.writeAttribute(name, value); |
---|
| 531 | return *this; |
---|
| 532 | } |
---|
| 533 | |
---|
[1526] | 534 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 535 | /// |
---|
[1526] | 536 | /// This function issues a new <i> attribute writing |
---|
| 537 | /// command</i> to the writer. |
---|
[1421] | 538 | template <typename Writer, typename Value> |
---|
[1909] | 539 | UGraphWriter& writeAttribute(std::string name, const Value& value, |
---|
[1421] | 540 | const Writer& writer) { |
---|
| 541 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
| 542 | return *this; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | /// \brief Conversion operator to LemonWriter. |
---|
| 546 | /// |
---|
[1526] | 547 | /// Conversion operator to LemonWriter. It makes possible |
---|
[1421] | 548 | /// to access the encapsulated \e LemonWriter, this way |
---|
| 549 | /// you can attach to this writer new instances of |
---|
| 550 | /// \e LemonWriter::SectionWriter. |
---|
| 551 | operator LemonWriter&() { |
---|
| 552 | return *writer; |
---|
| 553 | } |
---|
| 554 | |
---|
[1526] | 555 | /// \brief Executes the writing commands. |
---|
[1421] | 556 | /// |
---|
[1526] | 557 | /// Executes the writing commands. |
---|
[1421] | 558 | void run() { |
---|
| 559 | writer->run(); |
---|
| 560 | } |
---|
| 561 | |
---|
[1901] | 562 | /// \brief Write the label of the given node. |
---|
[1429] | 563 | /// |
---|
[1901] | 564 | /// It writes the label of the given node. If there was written an "label" |
---|
[1526] | 565 | /// named node map then it will write the map value belonging to the node. |
---|
[1901] | 566 | void writeLabel(std::ostream& os, const Node& item) const { |
---|
| 567 | nodeset_writer.writeLabel(os, item); |
---|
[1429] | 568 | } |
---|
| 569 | |
---|
[1901] | 570 | /// \brief Write the label of the given edge. |
---|
[1429] | 571 | /// |
---|
[1901] | 572 | /// It writes the label of the given edge. If there was written an "label" |
---|
[1526] | 573 | /// named edge map then it will write the map value belonging to the edge. |
---|
[1901] | 574 | void writeLabel(std::ostream& os, const Edge& item) const { |
---|
[1909] | 575 | u_edgeset_writer.writeLabel(os, item); |
---|
[1429] | 576 | } |
---|
| 577 | |
---|
[1901] | 578 | /// \brief Write the label of the given undirected edge. |
---|
[1429] | 579 | /// |
---|
[1901] | 580 | /// It writes the label of the given undirected edge. If there was written |
---|
| 581 | /// an "label" named edge map then it will write the map value belonging to |
---|
[1429] | 582 | /// the edge. |
---|
[1909] | 583 | void writeLabel(std::ostream& os, const UEdge& item) const { |
---|
| 584 | u_edgeset_writer.writeLabel(os, item); |
---|
[1429] | 585 | } |
---|
| 586 | |
---|
| 587 | |
---|
[1421] | 588 | private: |
---|
| 589 | |
---|
| 590 | LemonWriter* writer; |
---|
| 591 | bool own_writer; |
---|
| 592 | |
---|
| 593 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
[1909] | 594 | UEdgeSetWriter<Graph, WriterTraits> u_edgeset_writer; |
---|
[1421] | 595 | |
---|
| 596 | NodeWriter<Graph> node_writer; |
---|
[1909] | 597 | UEdgeWriter<Graph> u_edge_writer; |
---|
[1421] | 598 | |
---|
| 599 | AttributeWriter<WriterTraits> attribute_writer; |
---|
| 600 | }; |
---|
| 601 | |
---|
[1534] | 602 | /// \brief Write an undirected graph to the output. |
---|
| 603 | /// |
---|
[1744] | 604 | /// It is a helper function to write an undirected graph to the given output |
---|
[1909] | 605 | /// stream. It gives back an UGraphWriter object and this object |
---|
[1744] | 606 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 607 | /// \warning Do not forget to call the \c run() function. |
---|
| 608 | /// |
---|
[1534] | 609 | /// \param os The output stream. |
---|
| 610 | /// \param g The graph. |
---|
[1744] | 611 | template <typename Graph> |
---|
[1909] | 612 | UGraphWriter<Graph> uGraphWriter(std::ostream& os, const Graph &g) { |
---|
| 613 | return UGraphWriter<Graph>(os, g); |
---|
[1534] | 614 | } |
---|
[1421] | 615 | |
---|
[1744] | 616 | /// \brief Write an undirected graph to the output. |
---|
[1421] | 617 | /// |
---|
[1744] | 618 | /// It is a helper function to write an undirected graph to the given output |
---|
[1909] | 619 | /// file. It gives back an UGraphWriter object and this object |
---|
[1744] | 620 | /// can write more maps, labeled nodes, edges, undirected edges and |
---|
| 621 | /// attributes. |
---|
| 622 | /// |
---|
| 623 | /// \warning Do not forget to call the \c run() function. |
---|
| 624 | /// |
---|
| 625 | /// \param fn The output file. |
---|
[1421] | 626 | /// \param g The graph. |
---|
[1744] | 627 | template <typename Graph> |
---|
[1909] | 628 | UGraphWriter<Graph> uGraphWriter(const std::string& fn, |
---|
[1744] | 629 | const Graph &g) { |
---|
[1909] | 630 | return UGraphWriter<Graph>(fn, g); |
---|
[1421] | 631 | } |
---|
| 632 | |
---|
[1333] | 633 | /// @} |
---|
[1137] | 634 | |
---|
| 635 | } |
---|
[1214] | 636 | |
---|
| 637 | #endif |
---|