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