[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 | |
---|
[1744] | 287 | |
---|
[1333] | 288 | /// \brief Write a graph to the output. |
---|
| 289 | /// |
---|
[1744] | 290 | /// It is a helper function to write a graph to the given output |
---|
| 291 | /// stream. It gives back a GraphWriter object and this object |
---|
| 292 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 293 | /// \warning Do not forget to call the \c run() function. |
---|
[1534] | 294 | /// |
---|
[1333] | 295 | /// \param os The output stream. |
---|
| 296 | /// \param g The graph. |
---|
[1744] | 297 | template <typename Graph> |
---|
| 298 | GraphWriter<Graph> graphWriter(std::ostream& os, const Graph &g) { |
---|
| 299 | return GraphWriter<Graph>(os, g); |
---|
[1208] | 300 | } |
---|
| 301 | |
---|
[1744] | 302 | /// \brief Write a graph to the output. |
---|
[1333] | 303 | /// |
---|
[1744] | 304 | /// It is a helper function to write a graph to the given output |
---|
| 305 | /// file. It gives back a GraphWriter object and this object |
---|
| 306 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 307 | /// \warning Do not forget to call the \c run() function. |
---|
[1534] | 308 | /// |
---|
[1744] | 309 | /// \param fn The filename. |
---|
[1333] | 310 | /// \param g The graph. |
---|
[1744] | 311 | template <typename Graph> |
---|
| 312 | GraphWriter<Graph> graphWriter(const std::string& fn, const Graph &g) { |
---|
| 313 | return GraphWriter<Graph>(fn, g); |
---|
[1208] | 314 | } |
---|
| 315 | |
---|
[1421] | 316 | /// \brief The undirected graph writer class. |
---|
| 317 | /// |
---|
| 318 | /// The \c UndirGraphWriter class provides the undir graph output. To write |
---|
[1526] | 319 | /// a graph you should first give writing commands to the writer. You can |
---|
[1421] | 320 | /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap |
---|
| 321 | /// writing and labeled Node, Edge or UndirEdge writing. |
---|
| 322 | /// |
---|
| 323 | /// \code |
---|
| 324 | /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph); |
---|
| 325 | /// \endcode |
---|
| 326 | /// |
---|
| 327 | /// The \c writeNodeMap() function declares a \c NodeMap writing |
---|
| 328 | /// command in the \c UndirGraphWriter. You should give as parameter |
---|
| 329 | /// the name of the map and the map object. The NodeMap writing |
---|
| 330 | /// command with name "id" should write a unique map because it |
---|
| 331 | /// is regarded as ID map. |
---|
| 332 | /// |
---|
| 333 | /// \code |
---|
| 334 | /// IdMap<UndirListGraph, Node> nodeIdMap; |
---|
| 335 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
| 336 | /// |
---|
| 337 | /// writer.writeNodeMap("coords", coords); |
---|
| 338 | /// writer.writeNodeMap("color", colorMap); |
---|
| 339 | /// \endcode |
---|
| 340 | /// |
---|
| 341 | /// With the \c writeUndirEdgeMap() member function you can give an |
---|
| 342 | /// undirected edge map writing command similar to the NodeMaps. |
---|
| 343 | /// |
---|
| 344 | /// \code |
---|
| 345 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 346 | /// edgeDescMap(graph); |
---|
| 347 | /// writer.writeUndirEdgeMap("descriptor", edgeDescMap); |
---|
| 348 | /// |
---|
| 349 | /// writer.writeUndirEdgeMap("weight", weightMap); |
---|
| 350 | /// writer.writeUndirEdgeMap("label", labelMap); |
---|
| 351 | /// \endcode |
---|
| 352 | /// |
---|
| 353 | /// The EdgeMap handling is just a syntactical sugar. It writes |
---|
| 354 | /// two undirected edge map with '+' and '-' prefix in the name. |
---|
| 355 | /// |
---|
| 356 | /// \code |
---|
| 357 | /// writer.writeEdgeMap("capacity", capacityMap); |
---|
| 358 | /// \endcode |
---|
| 359 | /// |
---|
| 360 | /// |
---|
| 361 | /// With \c writeNode() and \c writeUndirEdge() functions you can |
---|
[1526] | 362 | /// designate nodes and undirected edges in the graph. For example, you can |
---|
[1421] | 363 | /// write out the source and target of the graph. |
---|
| 364 | /// |
---|
| 365 | /// \code |
---|
| 366 | /// writer.writeNode("source", sourceNode); |
---|
| 367 | /// writer.writeNode("target", targetNode); |
---|
| 368 | /// |
---|
| 369 | /// writer.writeUndirEdge("observed", undirEdge); |
---|
| 370 | /// \endcode |
---|
| 371 | /// |
---|
| 372 | /// After you give all write commands you must call the \c run() member |
---|
[1526] | 373 | /// function, which executes all the writing commands. |
---|
[1421] | 374 | /// |
---|
| 375 | /// \code |
---|
| 376 | /// writer.run(); |
---|
| 377 | /// \endcode |
---|
| 378 | /// |
---|
| 379 | /// \see DefaultWriterTraits |
---|
| 380 | /// \see QuotedStringWriter |
---|
| 381 | /// \see IdMap |
---|
| 382 | /// \see DescriptorMap |
---|
| 383 | /// \see \ref GraphWriter |
---|
| 384 | /// \see \ref graph-io-page |
---|
| 385 | /// \author Balazs Dezso |
---|
| 386 | template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> |
---|
| 387 | class UndirGraphWriter { |
---|
| 388 | public: |
---|
| 389 | |
---|
| 390 | typedef _Graph Graph; |
---|
| 391 | typedef typename Graph::Node Node; |
---|
| 392 | typedef typename Graph::Edge Edge; |
---|
| 393 | typedef typename Graph::UndirEdge UndirEdge; |
---|
| 394 | |
---|
| 395 | typedef _WriterTraits WriterTraits; |
---|
| 396 | |
---|
| 397 | /// \brief Construct a new UndirGraphWriter. |
---|
| 398 | /// |
---|
| 399 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
| 400 | /// to the given stream. |
---|
| 401 | UndirGraphWriter(std::ostream& _os, const Graph& _graph) |
---|
| 402 | : writer(new LemonWriter(_os)), own_writer(true), |
---|
| 403 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 404 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 405 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 406 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 407 | attribute_writer(*writer, std::string()) {} |
---|
| 408 | |
---|
| 409 | /// \brief Construct a new UndirGraphWriter. |
---|
| 410 | /// |
---|
[1526] | 411 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
[1421] | 412 | /// to the given file. |
---|
| 413 | UndirGraphWriter(const std::string& _filename, const Graph& _graph) |
---|
| 414 | : writer(new LemonWriter(_filename)), own_writer(true), |
---|
| 415 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 416 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 417 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 418 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 419 | attribute_writer(*writer, std::string()) {} |
---|
| 420 | |
---|
| 421 | /// \brief Construct a new UndirGraphWriter. |
---|
| 422 | /// |
---|
[1526] | 423 | /// Construct a new UndirGraphWriter. It writes the given graph |
---|
[1421] | 424 | /// to given LemonReader. |
---|
| 425 | UndirGraphWriter(LemonWriter& _writer, const Graph& _graph) |
---|
| 426 | : writer(_writer), own_writer(false), |
---|
| 427 | nodeset_writer(*writer, _graph, std::string()), |
---|
| 428 | undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()), |
---|
| 429 | node_writer(*writer, nodeset_writer, std::string()), |
---|
| 430 | undir_edge_writer(*writer, undir_edgeset_writer, std::string()), |
---|
| 431 | attribute_writer(*writer, std::string()) {} |
---|
| 432 | |
---|
| 433 | /// \brief Destruct the graph writer. |
---|
| 434 | /// |
---|
| 435 | /// Destruct the graph writer. |
---|
| 436 | ~UndirGraphWriter() { |
---|
| 437 | if (own_writer) |
---|
| 438 | delete writer; |
---|
| 439 | } |
---|
| 440 | |
---|
[1526] | 441 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 442 | /// |
---|
[1526] | 443 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 444 | template <typename Map> |
---|
| 445 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
| 446 | nodeset_writer.writeNodeMap(name, map); |
---|
| 447 | return *this; |
---|
| 448 | } |
---|
| 449 | |
---|
[1526] | 450 | /// \brief Issue a new node map writing command to the writer. |
---|
[1421] | 451 | /// |
---|
[1526] | 452 | /// This function issues a new <i> node map writing command</i> to the writer. |
---|
[1421] | 453 | template <typename Writer, typename Map> |
---|
| 454 | UndirGraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
| 455 | const Writer& writer = Writer()) { |
---|
| 456 | nodeset_writer.writeNodeMap(name, map, writer); |
---|
| 457 | return *this; |
---|
| 458 | } |
---|
| 459 | |
---|
[1526] | 460 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 461 | /// |
---|
[1526] | 462 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 463 | template <typename Map> |
---|
| 464 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
| 465 | undir_edgeset_writer.writeEdgeMap(name, map); |
---|
| 466 | return *this; |
---|
| 467 | } |
---|
| 468 | |
---|
[1526] | 469 | /// \brief Issue a new edge map writing command to the writer. |
---|
[1421] | 470 | /// |
---|
[1526] | 471 | /// This function issues a new <i> edge map writing command</i> to the writer. |
---|
[1421] | 472 | template <typename Writer, typename Map> |
---|
| 473 | UndirGraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
| 474 | const Writer& writer = Writer()) { |
---|
| 475 | undir_edgeset_writer.writeEdgeMap(name, map, writer); |
---|
| 476 | return *this; |
---|
| 477 | } |
---|
| 478 | |
---|
[1526] | 479 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 480 | /// |
---|
[1526] | 481 | /// This function issues a new <i> undirected edge map writing |
---|
| 482 | /// command</i> to the writer. |
---|
[1421] | 483 | template <typename Map> |
---|
| 484 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) { |
---|
| 485 | undir_edgeset_writer.writeUndirEdgeMap(name, map); |
---|
| 486 | return *this; |
---|
| 487 | } |
---|
| 488 | |
---|
[1526] | 489 | /// \brief Issue a new undirected edge map writing command to the writer. |
---|
[1421] | 490 | /// |
---|
[1526] | 491 | /// This function issues a new <i> undirected edge map writing |
---|
| 492 | /// command</i> to the writer. |
---|
[1540] | 493 | template <typename Writer, typename Map> |
---|
[1421] | 494 | UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map, |
---|
| 495 | const Writer& writer = Writer()) { |
---|
| 496 | undir_edgeset_writer.writeUndirEdgeMap(name, map, writer); |
---|
| 497 | return *this; |
---|
| 498 | } |
---|
| 499 | |
---|
[1526] | 500 | /// \brief Issue a new labeled node writer to the writer. |
---|
[1421] | 501 | /// |
---|
[1526] | 502 | /// This function issues a new <i> labeled node writing |
---|
| 503 | /// command</i> to the writer. |
---|
[1421] | 504 | UndirGraphWriter& writeNode(std::string name, const Node& node) { |
---|
| 505 | node_writer.writeNode(name, node); |
---|
| 506 | return *this; |
---|
| 507 | } |
---|
| 508 | |
---|
[1526] | 509 | /// \brief Issue a new labeled edge writer to the writer. |
---|
[1421] | 510 | /// |
---|
[1526] | 511 | /// This function issues a new <i> labeled edge writing |
---|
| 512 | /// command</i> to the writer. |
---|
[1429] | 513 | UndirGraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
| 514 | undir_edge_writer.writeEdge(name, edge); |
---|
| 515 | } |
---|
| 516 | |
---|
[1526] | 517 | /// \brief Issue a new labeled undirected edge writing command to |
---|
| 518 | /// the writer. |
---|
[1429] | 519 | /// |
---|
[1526] | 520 | /// Issue a new <i>labeled undirected edge writing command</i> to |
---|
| 521 | /// the writer. |
---|
[1421] | 522 | UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) { |
---|
| 523 | undir_edge_writer.writeUndirEdge(name, edge); |
---|
| 524 | } |
---|
| 525 | |
---|
[1526] | 526 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 527 | /// |
---|
[1526] | 528 | /// This function issues a new <i> attribute writing |
---|
| 529 | /// command</i> to the writer. |
---|
[1421] | 530 | template <typename Value> |
---|
| 531 | UndirGraphWriter& writeAttribute(std::string name, const Value& value) { |
---|
| 532 | attribute_writer.writeAttribute(name, value); |
---|
| 533 | return *this; |
---|
| 534 | } |
---|
| 535 | |
---|
[1526] | 536 | /// \brief Issue a new attribute writing command. |
---|
[1421] | 537 | /// |
---|
[1526] | 538 | /// This function issues a new <i> attribute writing |
---|
| 539 | /// command</i> to the writer. |
---|
[1421] | 540 | template <typename Writer, typename Value> |
---|
| 541 | UndirGraphWriter& writeAttribute(std::string name, const Value& value, |
---|
| 542 | const Writer& writer) { |
---|
| 543 | attribute_writer.writeAttribute<Writer>(name, value, writer); |
---|
| 544 | return *this; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | /// \brief Conversion operator to LemonWriter. |
---|
| 548 | /// |
---|
[1526] | 549 | /// Conversion operator to LemonWriter. It makes possible |
---|
[1421] | 550 | /// to access the encapsulated \e LemonWriter, this way |
---|
| 551 | /// you can attach to this writer new instances of |
---|
| 552 | /// \e LemonWriter::SectionWriter. |
---|
| 553 | operator LemonWriter&() { |
---|
| 554 | return *writer; |
---|
| 555 | } |
---|
| 556 | |
---|
[1526] | 557 | /// \brief Executes the writing commands. |
---|
[1421] | 558 | /// |
---|
[1526] | 559 | /// Executes the writing commands. |
---|
[1421] | 560 | void run() { |
---|
| 561 | writer->run(); |
---|
| 562 | } |
---|
| 563 | |
---|
[1429] | 564 | /// \brief Write the id of the given node. |
---|
| 565 | /// |
---|
| 566 | /// It writes the id of the given node. If there was written an "id" |
---|
[1526] | 567 | /// named node map then it will write the map value belonging to the node. |
---|
[1429] | 568 | void writeId(std::ostream& os, const Node& item) const { |
---|
| 569 | nodeset_writer.writeId(os, item); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /// \brief Write the id of the given edge. |
---|
| 573 | /// |
---|
| 574 | /// It writes the id of the given edge. If there was written an "id" |
---|
[1526] | 575 | /// named edge map then it will write the map value belonging to the edge. |
---|
[1429] | 576 | void writeId(std::ostream& os, const Edge& item) const { |
---|
| 577 | undir_edgeset_writer.writeId(os, item); |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | /// \brief Write the id of the given undirected edge. |
---|
| 581 | /// |
---|
| 582 | /// It writes the id of the given undirected edge. If there was written |
---|
[1526] | 583 | /// an "id" named edge map then it will write the map value belonging to |
---|
[1429] | 584 | /// the edge. |
---|
| 585 | void writeId(std::ostream& os, const UndirEdge& item) const { |
---|
| 586 | undir_edgeset_writer.writeId(os, item); |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | |
---|
[1421] | 590 | private: |
---|
| 591 | |
---|
| 592 | LemonWriter* writer; |
---|
| 593 | bool own_writer; |
---|
| 594 | |
---|
| 595 | NodeSetWriter<Graph, WriterTraits> nodeset_writer; |
---|
| 596 | UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer; |
---|
| 597 | |
---|
| 598 | NodeWriter<Graph> node_writer; |
---|
| 599 | UndirEdgeWriter<Graph> undir_edge_writer; |
---|
| 600 | |
---|
| 601 | AttributeWriter<WriterTraits> attribute_writer; |
---|
| 602 | }; |
---|
| 603 | |
---|
[1534] | 604 | /// \brief Write an undirected graph to the output. |
---|
| 605 | /// |
---|
[1744] | 606 | /// It is a helper function to write an undirected graph to the given output |
---|
| 607 | /// stream. It gives back an UndirGraphWriter object and this object |
---|
| 608 | /// can write more maps, labeled nodes and edges and attributes. |
---|
| 609 | /// \warning Do not forget to call the \c run() function. |
---|
| 610 | /// |
---|
[1534] | 611 | /// \param os The output stream. |
---|
| 612 | /// \param g The graph. |
---|
[1744] | 613 | template <typename Graph> |
---|
| 614 | UndirGraphWriter<Graph> undirGraphWriter(std::ostream& os, const Graph &g) { |
---|
| 615 | return UndirGraphWriter<Graph>(os, g); |
---|
[1534] | 616 | } |
---|
[1421] | 617 | |
---|
[1744] | 618 | /// \brief Write an undirected graph to the output. |
---|
[1421] | 619 | /// |
---|
[1744] | 620 | /// It is a helper function to write an undirected graph to the given output |
---|
| 621 | /// file. It gives back an UndirGraphWriter object and this object |
---|
| 622 | /// can write more maps, labeled nodes, edges, undirected edges and |
---|
| 623 | /// attributes. |
---|
| 624 | /// |
---|
| 625 | /// \warning Do not forget to call the \c run() function. |
---|
| 626 | /// |
---|
| 627 | /// \param fn The output file. |
---|
[1421] | 628 | /// \param g The graph. |
---|
[1744] | 629 | template <typename Graph> |
---|
| 630 | UndirGraphWriter<Graph> undirGraphWriter(const std::string& fn, |
---|
| 631 | const Graph &g) { |
---|
| 632 | return UndirGraphWriter<Graph>(fn, g); |
---|
[1421] | 633 | } |
---|
| 634 | |
---|
[1333] | 635 | /// @} |
---|
[1137] | 636 | |
---|
| 637 | } |
---|
[1214] | 638 | |
---|
| 639 | #endif |
---|