[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[127] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[127] | 4 | * |
---|
[440] | 5 | * Copyright (C) 2003-2009 |
---|
[127] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 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 | |
---|
| 19 | ///\ingroup lemon_io |
---|
| 20 | ///\file |
---|
[236] | 21 | ///\brief \ref lgf-format "LEMON Graph Format" writer. |
---|
[127] | 22 | |
---|
| 23 | |
---|
| 24 | #ifndef LEMON_LGF_WRITER_H |
---|
| 25 | #define LEMON_LGF_WRITER_H |
---|
| 26 | |
---|
| 27 | #include <iostream> |
---|
| 28 | #include <fstream> |
---|
| 29 | #include <sstream> |
---|
| 30 | |
---|
| 31 | #include <algorithm> |
---|
| 32 | |
---|
| 33 | #include <vector> |
---|
| 34 | #include <functional> |
---|
| 35 | |
---|
[220] | 36 | #include <lemon/core.h> |
---|
| 37 | #include <lemon/maps.h> |
---|
[127] | 38 | |
---|
[248] | 39 | #include <lemon/concept_check.h> |
---|
| 40 | #include <lemon/concepts/maps.h> |
---|
| 41 | |
---|
[127] | 42 | namespace lemon { |
---|
| 43 | |
---|
| 44 | namespace _writer_bits { |
---|
| 45 | |
---|
| 46 | template <typename Value> |
---|
| 47 | struct DefaultConverter { |
---|
| 48 | std::string operator()(const Value& value) { |
---|
[209] | 49 | std::ostringstream os; |
---|
| 50 | os << value; |
---|
| 51 | return os.str(); |
---|
[127] | 52 | } |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | template <typename T> |
---|
| 56 | bool operator<(const T&, const T&) { |
---|
[290] | 57 | throw FormatError("Label map is not comparable"); |
---|
[127] | 58 | } |
---|
| 59 | |
---|
| 60 | template <typename _Map> |
---|
| 61 | class MapLess { |
---|
| 62 | public: |
---|
| 63 | typedef _Map Map; |
---|
| 64 | typedef typename Map::Key Item; |
---|
| 65 | |
---|
| 66 | private: |
---|
| 67 | const Map& _map; |
---|
[209] | 68 | |
---|
[127] | 69 | public: |
---|
| 70 | MapLess(const Map& map) : _map(map) {} |
---|
| 71 | |
---|
| 72 | bool operator()(const Item& left, const Item& right) { |
---|
[209] | 73 | return _map[left] < _map[right]; |
---|
[127] | 74 | } |
---|
| 75 | }; |
---|
| 76 | |
---|
[165] | 77 | template <typename _Graph, bool _dir, typename _Map> |
---|
| 78 | class GraphArcMapLess { |
---|
| 79 | public: |
---|
| 80 | typedef _Map Map; |
---|
| 81 | typedef _Graph Graph; |
---|
| 82 | typedef typename Graph::Edge Item; |
---|
| 83 | |
---|
| 84 | private: |
---|
| 85 | const Graph& _graph; |
---|
| 86 | const Map& _map; |
---|
[209] | 87 | |
---|
[165] | 88 | public: |
---|
[209] | 89 | GraphArcMapLess(const Graph& graph, const Map& map) |
---|
| 90 | : _graph(graph), _map(map) {} |
---|
[165] | 91 | |
---|
| 92 | bool operator()(const Item& left, const Item& right) { |
---|
[209] | 93 | return _map[_graph.direct(left, _dir)] < |
---|
| 94 | _map[_graph.direct(right, _dir)]; |
---|
[165] | 95 | } |
---|
| 96 | }; |
---|
| 97 | |
---|
[209] | 98 | template <typename _Item> |
---|
[127] | 99 | class MapStorageBase { |
---|
| 100 | public: |
---|
| 101 | typedef _Item Item; |
---|
| 102 | |
---|
| 103 | public: |
---|
| 104 | MapStorageBase() {} |
---|
| 105 | virtual ~MapStorageBase() {} |
---|
| 106 | |
---|
| 107 | virtual std::string get(const Item& item) = 0; |
---|
| 108 | virtual void sort(std::vector<Item>&) = 0; |
---|
| 109 | }; |
---|
| 110 | |
---|
[209] | 111 | template <typename _Item, typename _Map, |
---|
| 112 | typename _Converter = DefaultConverter<typename _Map::Value> > |
---|
[127] | 113 | class MapStorage : public MapStorageBase<_Item> { |
---|
| 114 | public: |
---|
| 115 | typedef _Map Map; |
---|
| 116 | typedef _Converter Converter; |
---|
| 117 | typedef _Item Item; |
---|
[209] | 118 | |
---|
[127] | 119 | private: |
---|
| 120 | const Map& _map; |
---|
| 121 | Converter _converter; |
---|
| 122 | |
---|
| 123 | public: |
---|
[209] | 124 | MapStorage(const Map& map, const Converter& converter = Converter()) |
---|
| 125 | : _map(map), _converter(converter) {} |
---|
[127] | 126 | virtual ~MapStorage() {} |
---|
| 127 | |
---|
| 128 | virtual std::string get(const Item& item) { |
---|
[209] | 129 | return _converter(_map[item]); |
---|
[127] | 130 | } |
---|
| 131 | virtual void sort(std::vector<Item>& items) { |
---|
[209] | 132 | MapLess<Map> less(_map); |
---|
| 133 | std::sort(items.begin(), items.end(), less); |
---|
[127] | 134 | } |
---|
| 135 | }; |
---|
| 136 | |
---|
[209] | 137 | template <typename _Graph, bool _dir, typename _Map, |
---|
| 138 | typename _Converter = DefaultConverter<typename _Map::Value> > |
---|
[165] | 139 | class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
---|
| 140 | public: |
---|
| 141 | typedef _Map Map; |
---|
| 142 | typedef _Converter Converter; |
---|
| 143 | typedef _Graph Graph; |
---|
| 144 | typedef typename Graph::Edge Item; |
---|
| 145 | static const bool dir = _dir; |
---|
[209] | 146 | |
---|
[165] | 147 | private: |
---|
| 148 | const Graph& _graph; |
---|
| 149 | const Map& _map; |
---|
| 150 | Converter _converter; |
---|
| 151 | |
---|
| 152 | public: |
---|
[209] | 153 | GraphArcMapStorage(const Graph& graph, const Map& map, |
---|
| 154 | const Converter& converter = Converter()) |
---|
| 155 | : _graph(graph), _map(map), _converter(converter) {} |
---|
[165] | 156 | virtual ~GraphArcMapStorage() {} |
---|
| 157 | |
---|
| 158 | virtual std::string get(const Item& item) { |
---|
[209] | 159 | return _converter(_map[_graph.direct(item, dir)]); |
---|
[165] | 160 | } |
---|
| 161 | virtual void sort(std::vector<Item>& items) { |
---|
[209] | 162 | GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
---|
| 163 | std::sort(items.begin(), items.end(), less); |
---|
[165] | 164 | } |
---|
| 165 | }; |
---|
| 166 | |
---|
[127] | 167 | class ValueStorageBase { |
---|
| 168 | public: |
---|
| 169 | ValueStorageBase() {} |
---|
| 170 | virtual ~ValueStorageBase() {} |
---|
| 171 | |
---|
[209] | 172 | virtual std::string get() = 0; |
---|
[127] | 173 | }; |
---|
| 174 | |
---|
| 175 | template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
---|
| 176 | class ValueStorage : public ValueStorageBase { |
---|
| 177 | public: |
---|
| 178 | typedef _Value Value; |
---|
| 179 | typedef _Converter Converter; |
---|
| 180 | |
---|
| 181 | private: |
---|
| 182 | const Value& _value; |
---|
| 183 | Converter _converter; |
---|
| 184 | |
---|
| 185 | public: |
---|
| 186 | ValueStorage(const Value& value, const Converter& converter = Converter()) |
---|
[212] | 187 | : _value(value), _converter(converter) {} |
---|
[127] | 188 | |
---|
| 189 | virtual std::string get() { |
---|
[209] | 190 | return _converter(_value); |
---|
[127] | 191 | } |
---|
| 192 | }; |
---|
| 193 | |
---|
| 194 | template <typename Value> |
---|
| 195 | struct MapLookUpConverter { |
---|
| 196 | const std::map<Value, std::string>& _map; |
---|
[209] | 197 | |
---|
| 198 | MapLookUpConverter(const std::map<Value, std::string>& map) |
---|
| 199 | : _map(map) {} |
---|
| 200 | |
---|
[127] | 201 | std::string operator()(const Value& str) { |
---|
[209] | 202 | typename std::map<Value, std::string>::const_iterator it = |
---|
| 203 | _map.find(str); |
---|
| 204 | if (it == _map.end()) { |
---|
[290] | 205 | throw FormatError("Item not found"); |
---|
[209] | 206 | } |
---|
| 207 | return it->second; |
---|
[127] | 208 | } |
---|
| 209 | }; |
---|
| 210 | |
---|
[165] | 211 | template <typename Graph> |
---|
| 212 | struct GraphArcLookUpConverter { |
---|
| 213 | const Graph& _graph; |
---|
| 214 | const std::map<typename Graph::Edge, std::string>& _map; |
---|
[209] | 215 | |
---|
| 216 | GraphArcLookUpConverter(const Graph& graph, |
---|
| 217 | const std::map<typename Graph::Edge, |
---|
| 218 | std::string>& map) |
---|
| 219 | : _graph(graph), _map(map) {} |
---|
| 220 | |
---|
[165] | 221 | std::string operator()(const typename Graph::Arc& val) { |
---|
[209] | 222 | typename std::map<typename Graph::Edge, std::string> |
---|
| 223 | ::const_iterator it = _map.find(val); |
---|
| 224 | if (it == _map.end()) { |
---|
[290] | 225 | throw FormatError("Item not found"); |
---|
[209] | 226 | } |
---|
| 227 | return (_graph.direction(val) ? '+' : '-') + it->second; |
---|
[165] | 228 | } |
---|
| 229 | }; |
---|
| 230 | |
---|
[197] | 231 | inline bool isWhiteSpace(char c) { |
---|
[209] | 232 | return c == ' ' || c == '\t' || c == '\v' || |
---|
| 233 | c == '\n' || c == '\r' || c == '\f'; |
---|
[127] | 234 | } |
---|
| 235 | |
---|
[197] | 236 | inline bool isEscaped(char c) { |
---|
[209] | 237 | return c == '\\' || c == '\"' || c == '\'' || |
---|
| 238 | c == '\a' || c == '\b'; |
---|
[127] | 239 | } |
---|
| 240 | |
---|
[197] | 241 | inline static void writeEscape(std::ostream& os, char c) { |
---|
[127] | 242 | switch (c) { |
---|
| 243 | case '\\': |
---|
[209] | 244 | os << "\\\\"; |
---|
| 245 | return; |
---|
[127] | 246 | case '\"': |
---|
[209] | 247 | os << "\\\""; |
---|
| 248 | return; |
---|
[127] | 249 | case '\a': |
---|
[209] | 250 | os << "\\a"; |
---|
| 251 | return; |
---|
[127] | 252 | case '\b': |
---|
[209] | 253 | os << "\\b"; |
---|
| 254 | return; |
---|
[127] | 255 | case '\f': |
---|
[209] | 256 | os << "\\f"; |
---|
| 257 | return; |
---|
[127] | 258 | case '\r': |
---|
[209] | 259 | os << "\\r"; |
---|
| 260 | return; |
---|
[127] | 261 | case '\n': |
---|
[209] | 262 | os << "\\n"; |
---|
| 263 | return; |
---|
[127] | 264 | case '\t': |
---|
[209] | 265 | os << "\\t"; |
---|
| 266 | return; |
---|
[127] | 267 | case '\v': |
---|
[209] | 268 | os << "\\v"; |
---|
| 269 | return; |
---|
[127] | 270 | default: |
---|
[209] | 271 | if (c < 0x20) { |
---|
| 272 | std::ios::fmtflags flags = os.flags(); |
---|
| 273 | os << '\\' << std::oct << static_cast<int>(c); |
---|
| 274 | os.flags(flags); |
---|
| 275 | } else { |
---|
| 276 | os << c; |
---|
| 277 | } |
---|
| 278 | return; |
---|
| 279 | } |
---|
[127] | 280 | } |
---|
| 281 | |
---|
[197] | 282 | inline bool requireEscape(const std::string& str) { |
---|
[156] | 283 | if (str.empty() || str[0] == '@') return true; |
---|
[127] | 284 | std::istringstream is(str); |
---|
| 285 | char c; |
---|
| 286 | while (is.get(c)) { |
---|
[209] | 287 | if (isWhiteSpace(c) || isEscaped(c)) { |
---|
| 288 | return true; |
---|
| 289 | } |
---|
[127] | 290 | } |
---|
| 291 | return false; |
---|
| 292 | } |
---|
[209] | 293 | |
---|
[197] | 294 | inline std::ostream& writeToken(std::ostream& os, const std::string& str) { |
---|
[127] | 295 | |
---|
| 296 | if (requireEscape(str)) { |
---|
[209] | 297 | os << '\"'; |
---|
| 298 | for (std::string::const_iterator it = str.begin(); |
---|
| 299 | it != str.end(); ++it) { |
---|
| 300 | writeEscape(os, *it); |
---|
| 301 | } |
---|
| 302 | os << '\"'; |
---|
[127] | 303 | } else { |
---|
[209] | 304 | os << str; |
---|
[127] | 305 | } |
---|
| 306 | return os; |
---|
| 307 | } |
---|
| 308 | |
---|
[248] | 309 | class Section { |
---|
| 310 | public: |
---|
| 311 | virtual ~Section() {} |
---|
| 312 | virtual void process(std::ostream& os) = 0; |
---|
| 313 | }; |
---|
| 314 | |
---|
| 315 | template <typename Functor> |
---|
| 316 | class LineSection : public Section { |
---|
| 317 | private: |
---|
| 318 | |
---|
| 319 | Functor _functor; |
---|
| 320 | |
---|
| 321 | public: |
---|
| 322 | |
---|
| 323 | LineSection(const Functor& functor) : _functor(functor) {} |
---|
| 324 | virtual ~LineSection() {} |
---|
| 325 | |
---|
| 326 | virtual void process(std::ostream& os) { |
---|
| 327 | std::string line; |
---|
| 328 | while (!(line = _functor()).empty()) os << line << std::endl; |
---|
| 329 | } |
---|
| 330 | }; |
---|
| 331 | |
---|
| 332 | template <typename Functor> |
---|
| 333 | class StreamSection : public Section { |
---|
| 334 | private: |
---|
| 335 | |
---|
| 336 | Functor _functor; |
---|
| 337 | |
---|
| 338 | public: |
---|
| 339 | |
---|
| 340 | StreamSection(const Functor& functor) : _functor(functor) {} |
---|
| 341 | virtual ~StreamSection() {} |
---|
| 342 | |
---|
| 343 | virtual void process(std::ostream& os) { |
---|
| 344 | _functor(os); |
---|
| 345 | } |
---|
| 346 | }; |
---|
| 347 | |
---|
[127] | 348 | } |
---|
[190] | 349 | |
---|
[598] | 350 | template <typename DGR> |
---|
[190] | 351 | class DigraphWriter; |
---|
| 352 | |
---|
[598] | 353 | template <typename TDGR> |
---|
| 354 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
---|
| 355 | std::ostream& os = std::cout); |
---|
| 356 | template <typename TDGR> |
---|
| 357 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const std::string& fn); |
---|
[190] | 358 | |
---|
[598] | 359 | template <typename TDGR> |
---|
| 360 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn); |
---|
[498] | 361 | |
---|
[209] | 362 | |
---|
[156] | 363 | /// \ingroup lemon_io |
---|
[209] | 364 | /// |
---|
[192] | 365 | /// \brief \ref lgf-format "LGF" writer for directed graphs |
---|
[156] | 366 | /// |
---|
| 367 | /// This utility writes an \ref lgf-format "LGF" file. |
---|
| 368 | /// |
---|
| 369 | /// The writing method does a batch processing. The user creates a |
---|
| 370 | /// writer object, then various writing rules can be added to the |
---|
| 371 | /// writer, and eventually the writing is executed with the \c run() |
---|
| 372 | /// member function. A map writing rule can be added to the writer |
---|
| 373 | /// with the \c nodeMap() or \c arcMap() members. An optional |
---|
[163] | 374 | /// converter parameter can also be added as a standard functor |
---|
[192] | 375 | /// converting from the value type of the map to \c std::string. If it |
---|
| 376 | /// is set, it will determine how the value type of the map is written to |
---|
[163] | 377 | /// the output stream. If the functor is not set, then a default |
---|
| 378 | /// conversion will be used. The \c attribute(), \c node() and \c |
---|
| 379 | /// arc() functions are used to add attribute writing rules. |
---|
[156] | 380 | /// |
---|
| 381 | ///\code |
---|
[598] | 382 | /// DigraphWriter<DGR>(digraph, std::cout). |
---|
[192] | 383 | /// nodeMap("coordinates", coord_map). |
---|
| 384 | /// nodeMap("size", size). |
---|
| 385 | /// nodeMap("title", title). |
---|
| 386 | /// arcMap("capacity", cap_map). |
---|
| 387 | /// node("source", src). |
---|
| 388 | /// node("target", trg). |
---|
| 389 | /// attribute("caption", caption). |
---|
| 390 | /// run(); |
---|
[156] | 391 | ///\endcode |
---|
| 392 | /// |
---|
| 393 | /// |
---|
| 394 | /// By default, the writer does not write additional captions to the |
---|
| 395 | /// sections, but they can be give as an optional parameter of |
---|
| 396 | /// the \c nodes(), \c arcs() or \c |
---|
| 397 | /// attributes() functions. |
---|
| 398 | /// |
---|
| 399 | /// The \c skipNodes() and \c skipArcs() functions forbid the |
---|
[163] | 400 | /// writing of the sections. If two arc sections should be written |
---|
| 401 | /// to the output, it can be done in two passes, the first pass |
---|
| 402 | /// writes the node section and the first arc section, then the |
---|
| 403 | /// second pass skips the node section and writes just the arc |
---|
| 404 | /// section to the stream. The output stream can be retrieved with |
---|
| 405 | /// the \c ostream() function, hence the second pass can append its |
---|
| 406 | /// output to the output of the first pass. |
---|
[598] | 407 | template <typename DGR> |
---|
[127] | 408 | class DigraphWriter { |
---|
| 409 | public: |
---|
| 410 | |
---|
[598] | 411 | typedef DGR Digraph; |
---|
| 412 | TEMPLATE_DIGRAPH_TYPEDEFS(DGR); |
---|
[209] | 413 | |
---|
[127] | 414 | private: |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | std::ostream* _os; |
---|
| 418 | bool local_os; |
---|
| 419 | |
---|
[598] | 420 | const DGR& _digraph; |
---|
[127] | 421 | |
---|
| 422 | std::string _nodes_caption; |
---|
| 423 | std::string _arcs_caption; |
---|
| 424 | std::string _attributes_caption; |
---|
[209] | 425 | |
---|
[127] | 426 | typedef std::map<Node, std::string> NodeIndex; |
---|
| 427 | NodeIndex _node_index; |
---|
| 428 | typedef std::map<Arc, std::string> ArcIndex; |
---|
| 429 | ArcIndex _arc_index; |
---|
| 430 | |
---|
[209] | 431 | typedef std::vector<std::pair<std::string, |
---|
| 432 | _writer_bits::MapStorageBase<Node>* > > NodeMaps; |
---|
| 433 | NodeMaps _node_maps; |
---|
[127] | 434 | |
---|
[209] | 435 | typedef std::vector<std::pair<std::string, |
---|
[127] | 436 | _writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
---|
| 437 | ArcMaps _arc_maps; |
---|
| 438 | |
---|
[209] | 439 | typedef std::vector<std::pair<std::string, |
---|
[127] | 440 | _writer_bits::ValueStorageBase*> > Attributes; |
---|
| 441 | Attributes _attributes; |
---|
| 442 | |
---|
| 443 | bool _skip_nodes; |
---|
| 444 | bool _skip_arcs; |
---|
| 445 | |
---|
| 446 | public: |
---|
| 447 | |
---|
[156] | 448 | /// \brief Constructor |
---|
| 449 | /// |
---|
| 450 | /// Construct a directed graph writer, which writes to the given |
---|
| 451 | /// output stream. |
---|
[598] | 452 | DigraphWriter(const DGR& digraph, std::ostream& os = std::cout) |
---|
[293] | 453 | : _os(&os), local_os(false), _digraph(digraph), |
---|
[209] | 454 | _skip_nodes(false), _skip_arcs(false) {} |
---|
[127] | 455 | |
---|
[156] | 456 | /// \brief Constructor |
---|
| 457 | /// |
---|
| 458 | /// Construct a directed graph writer, which writes to the given |
---|
| 459 | /// output file. |
---|
[598] | 460 | DigraphWriter(const DGR& digraph, const std::string& fn) |
---|
[127] | 461 | : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), |
---|
[290] | 462 | _skip_nodes(false), _skip_arcs(false) { |
---|
[295] | 463 | if (!(*_os)) { |
---|
| 464 | delete _os; |
---|
| 465 | throw IoError("Cannot write file", fn); |
---|
| 466 | } |
---|
[290] | 467 | } |
---|
[127] | 468 | |
---|
[156] | 469 | /// \brief Constructor |
---|
| 470 | /// |
---|
| 471 | /// Construct a directed graph writer, which writes to the given |
---|
| 472 | /// output file. |
---|
[598] | 473 | DigraphWriter(const DGR& digraph, const char* fn) |
---|
[127] | 474 | : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), |
---|
[290] | 475 | _skip_nodes(false), _skip_arcs(false) { |
---|
[295] | 476 | if (!(*_os)) { |
---|
| 477 | delete _os; |
---|
| 478 | throw IoError("Cannot write file", fn); |
---|
| 479 | } |
---|
[290] | 480 | } |
---|
[127] | 481 | |
---|
[156] | 482 | /// \brief Destructor |
---|
[127] | 483 | ~DigraphWriter() { |
---|
[209] | 484 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 485 | it != _node_maps.end(); ++it) { |
---|
| 486 | delete it->second; |
---|
[127] | 487 | } |
---|
| 488 | |
---|
[209] | 489 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
| 490 | it != _arc_maps.end(); ++it) { |
---|
| 491 | delete it->second; |
---|
[127] | 492 | } |
---|
| 493 | |
---|
[209] | 494 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 495 | it != _attributes.end(); ++it) { |
---|
| 496 | delete it->second; |
---|
[127] | 497 | } |
---|
| 498 | |
---|
| 499 | if (local_os) { |
---|
[209] | 500 | delete _os; |
---|
[127] | 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | private: |
---|
[190] | 505 | |
---|
[598] | 506 | template <typename TDGR> |
---|
| 507 | friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
---|
| 508 | std::ostream& os); |
---|
| 509 | template <typename TDGR> |
---|
| 510 | friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
---|
| 511 | const std::string& fn); |
---|
| 512 | template <typename TDGR> |
---|
| 513 | friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
---|
| 514 | const char *fn); |
---|
[190] | 515 | |
---|
[209] | 516 | DigraphWriter(DigraphWriter& other) |
---|
[190] | 517 | : _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
---|
[209] | 518 | _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
---|
[190] | 519 | |
---|
| 520 | other._os = 0; |
---|
| 521 | other.local_os = false; |
---|
| 522 | |
---|
| 523 | _node_index.swap(other._node_index); |
---|
| 524 | _arc_index.swap(other._arc_index); |
---|
| 525 | |
---|
| 526 | _node_maps.swap(other._node_maps); |
---|
| 527 | _arc_maps.swap(other._arc_maps); |
---|
| 528 | _attributes.swap(other._attributes); |
---|
| 529 | |
---|
| 530 | _nodes_caption = other._nodes_caption; |
---|
| 531 | _arcs_caption = other._arcs_caption; |
---|
| 532 | _attributes_caption = other._attributes_caption; |
---|
| 533 | } |
---|
[209] | 534 | |
---|
[127] | 535 | DigraphWriter& operator=(const DigraphWriter&); |
---|
| 536 | |
---|
| 537 | public: |
---|
| 538 | |
---|
[584] | 539 | /// \name Writing Rules |
---|
[156] | 540 | /// @{ |
---|
[209] | 541 | |
---|
[192] | 542 | /// \brief Node map writing rule |
---|
[156] | 543 | /// |
---|
[192] | 544 | /// Add a node map writing rule to the writer. |
---|
[127] | 545 | template <typename Map> |
---|
| 546 | DigraphWriter& nodeMap(const std::string& caption, const Map& map) { |
---|
| 547 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 548 | _writer_bits::MapStorageBase<Node>* storage = |
---|
| 549 | new _writer_bits::MapStorage<Node, Map>(map); |
---|
[127] | 550 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 551 | return *this; |
---|
| 552 | } |
---|
| 553 | |
---|
[156] | 554 | /// \brief Node map writing rule |
---|
| 555 | /// |
---|
| 556 | /// Add a node map writing rule with specialized converter to the |
---|
| 557 | /// writer. |
---|
[127] | 558 | template <typename Map, typename Converter> |
---|
[209] | 559 | DigraphWriter& nodeMap(const std::string& caption, const Map& map, |
---|
| 560 | const Converter& converter = Converter()) { |
---|
[127] | 561 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 562 | _writer_bits::MapStorageBase<Node>* storage = |
---|
| 563 | new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[127] | 564 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 565 | return *this; |
---|
| 566 | } |
---|
| 567 | |
---|
[156] | 568 | /// \brief Arc map writing rule |
---|
| 569 | /// |
---|
| 570 | /// Add an arc map writing rule to the writer. |
---|
[127] | 571 | template <typename Map> |
---|
| 572 | DigraphWriter& arcMap(const std::string& caption, const Map& map) { |
---|
| 573 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 574 | _writer_bits::MapStorageBase<Arc>* storage = |
---|
| 575 | new _writer_bits::MapStorage<Arc, Map>(map); |
---|
[127] | 576 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 577 | return *this; |
---|
| 578 | } |
---|
| 579 | |
---|
[156] | 580 | /// \brief Arc map writing rule |
---|
| 581 | /// |
---|
| 582 | /// Add an arc map writing rule with specialized converter to the |
---|
| 583 | /// writer. |
---|
[127] | 584 | template <typename Map, typename Converter> |
---|
[209] | 585 | DigraphWriter& arcMap(const std::string& caption, const Map& map, |
---|
| 586 | const Converter& converter = Converter()) { |
---|
[127] | 587 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 588 | _writer_bits::MapStorageBase<Arc>* storage = |
---|
| 589 | new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter); |
---|
[127] | 590 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 591 | return *this; |
---|
| 592 | } |
---|
| 593 | |
---|
[156] | 594 | /// \brief Attribute writing rule |
---|
| 595 | /// |
---|
| 596 | /// Add an attribute writing rule to the writer. |
---|
[127] | 597 | template <typename Value> |
---|
| 598 | DigraphWriter& attribute(const std::string& caption, const Value& value) { |
---|
[209] | 599 | _writer_bits::ValueStorageBase* storage = |
---|
| 600 | new _writer_bits::ValueStorage<Value>(value); |
---|
[127] | 601 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 602 | return *this; |
---|
| 603 | } |
---|
| 604 | |
---|
[156] | 605 | /// \brief Attribute writing rule |
---|
| 606 | /// |
---|
| 607 | /// Add an attribute writing rule with specialized converter to the |
---|
| 608 | /// writer. |
---|
[127] | 609 | template <typename Value, typename Converter> |
---|
[209] | 610 | DigraphWriter& attribute(const std::string& caption, const Value& value, |
---|
| 611 | const Converter& converter = Converter()) { |
---|
| 612 | _writer_bits::ValueStorageBase* storage = |
---|
| 613 | new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[127] | 614 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 615 | return *this; |
---|
| 616 | } |
---|
| 617 | |
---|
[156] | 618 | /// \brief Node writing rule |
---|
| 619 | /// |
---|
| 620 | /// Add a node writing rule to the writer. |
---|
[127] | 621 | DigraphWriter& node(const std::string& caption, const Node& node) { |
---|
| 622 | typedef _writer_bits::MapLookUpConverter<Node> Converter; |
---|
| 623 | Converter converter(_node_index); |
---|
[209] | 624 | _writer_bits::ValueStorageBase* storage = |
---|
| 625 | new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[127] | 626 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 627 | return *this; |
---|
| 628 | } |
---|
| 629 | |
---|
[156] | 630 | /// \brief Arc writing rule |
---|
| 631 | /// |
---|
| 632 | /// Add an arc writing rule to writer. |
---|
[127] | 633 | DigraphWriter& arc(const std::string& caption, const Arc& arc) { |
---|
| 634 | typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
---|
| 635 | Converter converter(_arc_index); |
---|
[209] | 636 | _writer_bits::ValueStorageBase* storage = |
---|
| 637 | new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[127] | 638 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 639 | return *this; |
---|
| 640 | } |
---|
| 641 | |
---|
[584] | 642 | /// \name Section Captions |
---|
[156] | 643 | /// @{ |
---|
| 644 | |
---|
[192] | 645 | /// \brief Add an additional caption to the \c \@nodes section |
---|
[156] | 646 | /// |
---|
[192] | 647 | /// Add an additional caption to the \c \@nodes section. |
---|
[127] | 648 | DigraphWriter& nodes(const std::string& caption) { |
---|
| 649 | _nodes_caption = caption; |
---|
| 650 | return *this; |
---|
| 651 | } |
---|
| 652 | |
---|
[192] | 653 | /// \brief Add an additional caption to the \c \@arcs section |
---|
[156] | 654 | /// |
---|
[192] | 655 | /// Add an additional caption to the \c \@arcs section. |
---|
[127] | 656 | DigraphWriter& arcs(const std::string& caption) { |
---|
| 657 | _arcs_caption = caption; |
---|
| 658 | return *this; |
---|
| 659 | } |
---|
| 660 | |
---|
[192] | 661 | /// \brief Add an additional caption to the \c \@attributes section |
---|
[156] | 662 | /// |
---|
[192] | 663 | /// Add an additional caption to the \c \@attributes section. |
---|
[127] | 664 | DigraphWriter& attributes(const std::string& caption) { |
---|
| 665 | _attributes_caption = caption; |
---|
| 666 | return *this; |
---|
| 667 | } |
---|
| 668 | |
---|
[584] | 669 | /// \name Skipping Section |
---|
[156] | 670 | /// @{ |
---|
| 671 | |
---|
| 672 | /// \brief Skip writing the node set |
---|
| 673 | /// |
---|
[192] | 674 | /// The \c \@nodes section will not be written to the stream. |
---|
[127] | 675 | DigraphWriter& skipNodes() { |
---|
| 676 | LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
---|
[185] | 677 | _skip_nodes = true; |
---|
[127] | 678 | return *this; |
---|
| 679 | } |
---|
| 680 | |
---|
[156] | 681 | /// \brief Skip writing arc set |
---|
| 682 | /// |
---|
[192] | 683 | /// The \c \@arcs section will not be written to the stream. |
---|
[127] | 684 | DigraphWriter& skipArcs() { |
---|
| 685 | LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
---|
[185] | 686 | _skip_arcs = true; |
---|
[127] | 687 | return *this; |
---|
| 688 | } |
---|
| 689 | |
---|
[156] | 690 | /// @} |
---|
| 691 | |
---|
[127] | 692 | private: |
---|
| 693 | |
---|
| 694 | void writeNodes() { |
---|
| 695 | _writer_bits::MapStorageBase<Node>* label = 0; |
---|
| 696 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 697 | it != _node_maps.end(); ++it) { |
---|
[127] | 698 | if (it->first == "label") { |
---|
[209] | 699 | label = it->second; |
---|
| 700 | break; |
---|
| 701 | } |
---|
[127] | 702 | } |
---|
| 703 | |
---|
| 704 | *_os << "@nodes"; |
---|
| 705 | if (!_nodes_caption.empty()) { |
---|
[209] | 706 | _writer_bits::writeToken(*_os << ' ', _nodes_caption); |
---|
[127] | 707 | } |
---|
| 708 | *_os << std::endl; |
---|
| 709 | |
---|
| 710 | if (label == 0) { |
---|
[209] | 711 | *_os << "label" << '\t'; |
---|
[127] | 712 | } |
---|
| 713 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 714 | it != _node_maps.end(); ++it) { |
---|
| 715 | _writer_bits::writeToken(*_os, it->first) << '\t'; |
---|
[127] | 716 | } |
---|
| 717 | *_os << std::endl; |
---|
| 718 | |
---|
| 719 | std::vector<Node> nodes; |
---|
| 720 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 721 | nodes.push_back(n); |
---|
[127] | 722 | } |
---|
[209] | 723 | |
---|
[127] | 724 | if (label == 0) { |
---|
[598] | 725 | IdMap<DGR, Node> id_map(_digraph); |
---|
| 726 | _writer_bits::MapLess<IdMap<DGR, Node> > id_less(id_map); |
---|
[209] | 727 | std::sort(nodes.begin(), nodes.end(), id_less); |
---|
[127] | 728 | } else { |
---|
[209] | 729 | label->sort(nodes); |
---|
[127] | 730 | } |
---|
| 731 | |
---|
| 732 | for (int i = 0; i < static_cast<int>(nodes.size()); ++i) { |
---|
[209] | 733 | Node n = nodes[i]; |
---|
| 734 | if (label == 0) { |
---|
| 735 | std::ostringstream os; |
---|
| 736 | os << _digraph.id(n); |
---|
| 737 | _writer_bits::writeToken(*_os, os.str()); |
---|
| 738 | *_os << '\t'; |
---|
| 739 | _node_index.insert(std::make_pair(n, os.str())); |
---|
| 740 | } |
---|
| 741 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 742 | it != _node_maps.end(); ++it) { |
---|
| 743 | std::string value = it->second->get(n); |
---|
| 744 | _writer_bits::writeToken(*_os, value); |
---|
| 745 | if (it->first == "label") { |
---|
| 746 | _node_index.insert(std::make_pair(n, value)); |
---|
| 747 | } |
---|
| 748 | *_os << '\t'; |
---|
| 749 | } |
---|
| 750 | *_os << std::endl; |
---|
[127] | 751 | } |
---|
| 752 | } |
---|
| 753 | |
---|
[185] | 754 | void createNodeIndex() { |
---|
| 755 | _writer_bits::MapStorageBase<Node>* label = 0; |
---|
| 756 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 757 | it != _node_maps.end(); ++it) { |
---|
[185] | 758 | if (it->first == "label") { |
---|
[209] | 759 | label = it->second; |
---|
| 760 | break; |
---|
| 761 | } |
---|
[185] | 762 | } |
---|
| 763 | |
---|
| 764 | if (label == 0) { |
---|
[209] | 765 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
| 766 | std::ostringstream os; |
---|
| 767 | os << _digraph.id(n); |
---|
| 768 | _node_index.insert(std::make_pair(n, os.str())); |
---|
| 769 | } |
---|
[185] | 770 | } else { |
---|
[209] | 771 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
| 772 | std::string value = label->get(n); |
---|
| 773 | _node_index.insert(std::make_pair(n, value)); |
---|
| 774 | } |
---|
[185] | 775 | } |
---|
| 776 | } |
---|
| 777 | |
---|
[127] | 778 | void writeArcs() { |
---|
| 779 | _writer_bits::MapStorageBase<Arc>* label = 0; |
---|
| 780 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
[209] | 781 | it != _arc_maps.end(); ++it) { |
---|
[127] | 782 | if (it->first == "label") { |
---|
[209] | 783 | label = it->second; |
---|
| 784 | break; |
---|
| 785 | } |
---|
[127] | 786 | } |
---|
| 787 | |
---|
| 788 | *_os << "@arcs"; |
---|
| 789 | if (!_arcs_caption.empty()) { |
---|
[209] | 790 | _writer_bits::writeToken(*_os << ' ', _arcs_caption); |
---|
[127] | 791 | } |
---|
| 792 | *_os << std::endl; |
---|
| 793 | |
---|
| 794 | *_os << '\t' << '\t'; |
---|
| 795 | if (label == 0) { |
---|
[209] | 796 | *_os << "label" << '\t'; |
---|
[127] | 797 | } |
---|
| 798 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
[209] | 799 | it != _arc_maps.end(); ++it) { |
---|
| 800 | _writer_bits::writeToken(*_os, it->first) << '\t'; |
---|
[127] | 801 | } |
---|
| 802 | *_os << std::endl; |
---|
| 803 | |
---|
| 804 | std::vector<Arc> arcs; |
---|
| 805 | for (ArcIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 806 | arcs.push_back(n); |
---|
[127] | 807 | } |
---|
[209] | 808 | |
---|
[127] | 809 | if (label == 0) { |
---|
[598] | 810 | IdMap<DGR, Arc> id_map(_digraph); |
---|
| 811 | _writer_bits::MapLess<IdMap<DGR, Arc> > id_less(id_map); |
---|
[209] | 812 | std::sort(arcs.begin(), arcs.end(), id_less); |
---|
[127] | 813 | } else { |
---|
[209] | 814 | label->sort(arcs); |
---|
[127] | 815 | } |
---|
| 816 | |
---|
| 817 | for (int i = 0; i < static_cast<int>(arcs.size()); ++i) { |
---|
[209] | 818 | Arc a = arcs[i]; |
---|
| 819 | _writer_bits::writeToken(*_os, _node_index. |
---|
| 820 | find(_digraph.source(a))->second); |
---|
| 821 | *_os << '\t'; |
---|
| 822 | _writer_bits::writeToken(*_os, _node_index. |
---|
| 823 | find(_digraph.target(a))->second); |
---|
| 824 | *_os << '\t'; |
---|
| 825 | if (label == 0) { |
---|
| 826 | std::ostringstream os; |
---|
| 827 | os << _digraph.id(a); |
---|
| 828 | _writer_bits::writeToken(*_os, os.str()); |
---|
| 829 | *_os << '\t'; |
---|
| 830 | _arc_index.insert(std::make_pair(a, os.str())); |
---|
| 831 | } |
---|
| 832 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
| 833 | it != _arc_maps.end(); ++it) { |
---|
| 834 | std::string value = it->second->get(a); |
---|
| 835 | _writer_bits::writeToken(*_os, value); |
---|
| 836 | if (it->first == "label") { |
---|
| 837 | _arc_index.insert(std::make_pair(a, value)); |
---|
| 838 | } |
---|
| 839 | *_os << '\t'; |
---|
| 840 | } |
---|
| 841 | *_os << std::endl; |
---|
[127] | 842 | } |
---|
| 843 | } |
---|
| 844 | |
---|
[185] | 845 | void createArcIndex() { |
---|
| 846 | _writer_bits::MapStorageBase<Arc>* label = 0; |
---|
| 847 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
[209] | 848 | it != _arc_maps.end(); ++it) { |
---|
[185] | 849 | if (it->first == "label") { |
---|
[209] | 850 | label = it->second; |
---|
| 851 | break; |
---|
| 852 | } |
---|
[185] | 853 | } |
---|
| 854 | |
---|
| 855 | if (label == 0) { |
---|
[209] | 856 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
| 857 | std::ostringstream os; |
---|
| 858 | os << _digraph.id(a); |
---|
| 859 | _arc_index.insert(std::make_pair(a, os.str())); |
---|
| 860 | } |
---|
[185] | 861 | } else { |
---|
[209] | 862 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
| 863 | std::string value = label->get(a); |
---|
| 864 | _arc_index.insert(std::make_pair(a, value)); |
---|
| 865 | } |
---|
[185] | 866 | } |
---|
| 867 | } |
---|
| 868 | |
---|
[127] | 869 | void writeAttributes() { |
---|
| 870 | if (_attributes.empty()) return; |
---|
| 871 | *_os << "@attributes"; |
---|
| 872 | if (!_attributes_caption.empty()) { |
---|
[209] | 873 | _writer_bits::writeToken(*_os << ' ', _attributes_caption); |
---|
[127] | 874 | } |
---|
| 875 | *_os << std::endl; |
---|
| 876 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 877 | it != _attributes.end(); ++it) { |
---|
| 878 | _writer_bits::writeToken(*_os, it->first) << ' '; |
---|
| 879 | _writer_bits::writeToken(*_os, it->second->get()); |
---|
| 880 | *_os << std::endl; |
---|
[127] | 881 | } |
---|
| 882 | } |
---|
[209] | 883 | |
---|
[127] | 884 | public: |
---|
[209] | 885 | |
---|
[584] | 886 | /// \name Execution of the Writer |
---|
[156] | 887 | /// @{ |
---|
| 888 | |
---|
| 889 | /// \brief Start the batch processing |
---|
| 890 | /// |
---|
[192] | 891 | /// This function starts the batch processing. |
---|
[127] | 892 | void run() { |
---|
| 893 | if (!_skip_nodes) { |
---|
[209] | 894 | writeNodes(); |
---|
[185] | 895 | } else { |
---|
[209] | 896 | createNodeIndex(); |
---|
[127] | 897 | } |
---|
[209] | 898 | if (!_skip_arcs) { |
---|
| 899 | writeArcs(); |
---|
[185] | 900 | } else { |
---|
[209] | 901 | createArcIndex(); |
---|
[127] | 902 | } |
---|
| 903 | writeAttributes(); |
---|
| 904 | } |
---|
| 905 | |
---|
[192] | 906 | /// \brief Give back the stream of the writer |
---|
[156] | 907 | /// |
---|
[192] | 908 | /// Give back the stream of the writer. |
---|
[156] | 909 | std::ostream& ostream() { |
---|
[127] | 910 | return *_os; |
---|
| 911 | } |
---|
[156] | 912 | |
---|
| 913 | /// @} |
---|
[127] | 914 | }; |
---|
| 915 | |
---|
[598] | 916 | /// \ingroup lemon_io |
---|
| 917 | /// |
---|
[498] | 918 | /// \brief Return a \ref DigraphWriter class |
---|
| 919 | /// |
---|
[598] | 920 | /// This function just returns a \ref DigraphWriter class. |
---|
| 921 | /// |
---|
| 922 | /// With this function a digraph can be write to a file or output |
---|
| 923 | /// stream in \ref lgf-format "LGF" format with several maps and |
---|
| 924 | /// attributes. For example, with the following code a network flow |
---|
| 925 | /// problem can be written to the standard output, i.e. a digraph |
---|
| 926 | /// with a \e capacity map on the arcs and \e source and \e target |
---|
| 927 | /// nodes: |
---|
| 928 | /// |
---|
| 929 | ///\code |
---|
| 930 | ///ListDigraph digraph; |
---|
| 931 | ///ListDigraph::ArcMap<int> cap(digraph); |
---|
| 932 | ///ListDigraph::Node src, trg; |
---|
| 933 | /// // Setting the capacity map and source and target nodes |
---|
| 934 | ///digraphWriter(digraph, std::cout). |
---|
| 935 | /// arcMap("capacity", cap). |
---|
| 936 | /// node("source", src). |
---|
| 937 | /// node("target", trg). |
---|
| 938 | /// run(); |
---|
| 939 | ///\endcode |
---|
| 940 | /// |
---|
| 941 | /// For a complete documentation, please see the \ref DigraphWriter |
---|
| 942 | /// class documentation. |
---|
| 943 | /// \warning Don't forget to put the \ref DigraphWriter::run() "run()" |
---|
| 944 | /// to the end of the parameter list. |
---|
[498] | 945 | /// \relates DigraphWriter |
---|
[598] | 946 | /// \sa digraphWriter(const TDGR& digraph, const std::string& fn) |
---|
| 947 | /// \sa digraphWriter(const TDGR& digraph, const char* fn) |
---|
| 948 | template <typename TDGR> |
---|
| 949 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, std::ostream& os) { |
---|
| 950 | DigraphWriter<TDGR> tmp(digraph, os); |
---|
[498] | 951 | return tmp; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | /// \brief Return a \ref DigraphWriter class |
---|
| 955 | /// |
---|
| 956 | /// This function just returns a \ref DigraphWriter class. |
---|
| 957 | /// \relates DigraphWriter |
---|
[598] | 958 | /// \sa digraphWriter(const TDGR& digraph, std::ostream& os) |
---|
| 959 | template <typename TDGR> |
---|
| 960 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
---|
| 961 | const std::string& fn) { |
---|
| 962 | DigraphWriter<TDGR> tmp(digraph, fn); |
---|
[498] | 963 | return tmp; |
---|
| 964 | } |
---|
| 965 | |
---|
| 966 | /// \brief Return a \ref DigraphWriter class |
---|
| 967 | /// |
---|
| 968 | /// This function just returns a \ref DigraphWriter class. |
---|
| 969 | /// \relates DigraphWriter |
---|
[598] | 970 | /// \sa digraphWriter(const TDGR& digraph, std::ostream& os) |
---|
| 971 | template <typename TDGR> |
---|
| 972 | DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn) { |
---|
| 973 | DigraphWriter<TDGR> tmp(digraph, fn); |
---|
[498] | 974 | return tmp; |
---|
| 975 | } |
---|
| 976 | |
---|
[598] | 977 | template <typename GR> |
---|
[303] | 978 | class GraphWriter; |
---|
| 979 | |
---|
[598] | 980 | template <typename TGR> |
---|
| 981 | GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os = std::cout); |
---|
| 982 | template <typename TGR> |
---|
| 983 | GraphWriter<TGR> graphWriter(const TGR& graph, const std::string& fn); |
---|
| 984 | template <typename TGR> |
---|
| 985 | GraphWriter<TGR> graphWriter(const TGR& graph, const char* fn); |
---|
[165] | 986 | |
---|
| 987 | /// \ingroup lemon_io |
---|
[209] | 988 | /// |
---|
[192] | 989 | /// \brief \ref lgf-format "LGF" writer for directed graphs |
---|
[165] | 990 | /// |
---|
| 991 | /// This utility writes an \ref lgf-format "LGF" file. |
---|
[192] | 992 | /// |
---|
| 993 | /// It can be used almost the same way as \c DigraphWriter. |
---|
| 994 | /// The only difference is that this class can handle edges and |
---|
| 995 | /// edge maps as well as arcs and arc maps. |
---|
[201] | 996 | /// |
---|
| 997 | /// The arc maps are written into the file as two columns, the |
---|
| 998 | /// caption of the columns are the name of the map prefixed with \c |
---|
| 999 | /// '+' and \c '-'. The arcs are written into the \c \@attributes |
---|
| 1000 | /// section as a \c '+' or a \c '-' prefix (depends on the direction |
---|
| 1001 | /// of the arc) and the label of corresponding edge. |
---|
[559] | 1002 | template <typename GR> |
---|
[165] | 1003 | class GraphWriter { |
---|
| 1004 | public: |
---|
| 1005 | |
---|
[559] | 1006 | typedef GR Graph; |
---|
[598] | 1007 | TEMPLATE_GRAPH_TYPEDEFS(GR); |
---|
[209] | 1008 | |
---|
[165] | 1009 | private: |
---|
| 1010 | |
---|
| 1011 | |
---|
| 1012 | std::ostream* _os; |
---|
| 1013 | bool local_os; |
---|
| 1014 | |
---|
[598] | 1015 | const GR& _graph; |
---|
[165] | 1016 | |
---|
| 1017 | std::string _nodes_caption; |
---|
| 1018 | std::string _edges_caption; |
---|
| 1019 | std::string _attributes_caption; |
---|
[209] | 1020 | |
---|
[165] | 1021 | typedef std::map<Node, std::string> NodeIndex; |
---|
| 1022 | NodeIndex _node_index; |
---|
| 1023 | typedef std::map<Edge, std::string> EdgeIndex; |
---|
| 1024 | EdgeIndex _edge_index; |
---|
| 1025 | |
---|
[209] | 1026 | typedef std::vector<std::pair<std::string, |
---|
| 1027 | _writer_bits::MapStorageBase<Node>* > > NodeMaps; |
---|
| 1028 | NodeMaps _node_maps; |
---|
[165] | 1029 | |
---|
[209] | 1030 | typedef std::vector<std::pair<std::string, |
---|
[165] | 1031 | _writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
---|
| 1032 | EdgeMaps _edge_maps; |
---|
| 1033 | |
---|
[209] | 1034 | typedef std::vector<std::pair<std::string, |
---|
[165] | 1035 | _writer_bits::ValueStorageBase*> > Attributes; |
---|
| 1036 | Attributes _attributes; |
---|
| 1037 | |
---|
| 1038 | bool _skip_nodes; |
---|
| 1039 | bool _skip_edges; |
---|
| 1040 | |
---|
| 1041 | public: |
---|
| 1042 | |
---|
| 1043 | /// \brief Constructor |
---|
| 1044 | /// |
---|
| 1045 | /// Construct a directed graph writer, which writes to the given |
---|
| 1046 | /// output stream. |
---|
[598] | 1047 | GraphWriter(const GR& graph, std::ostream& os = std::cout) |
---|
[293] | 1048 | : _os(&os), local_os(false), _graph(graph), |
---|
[209] | 1049 | _skip_nodes(false), _skip_edges(false) {} |
---|
[165] | 1050 | |
---|
| 1051 | /// \brief Constructor |
---|
| 1052 | /// |
---|
| 1053 | /// Construct a directed graph writer, which writes to the given |
---|
| 1054 | /// output file. |
---|
[598] | 1055 | GraphWriter(const GR& graph, const std::string& fn) |
---|
[165] | 1056 | : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
---|
[290] | 1057 | _skip_nodes(false), _skip_edges(false) { |
---|
[295] | 1058 | if (!(*_os)) { |
---|
| 1059 | delete _os; |
---|
| 1060 | throw IoError("Cannot write file", fn); |
---|
| 1061 | } |
---|
[290] | 1062 | } |
---|
[165] | 1063 | |
---|
| 1064 | /// \brief Constructor |
---|
| 1065 | /// |
---|
| 1066 | /// Construct a directed graph writer, which writes to the given |
---|
| 1067 | /// output file. |
---|
[598] | 1068 | GraphWriter(const GR& graph, const char* fn) |
---|
[165] | 1069 | : _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
---|
[290] | 1070 | _skip_nodes(false), _skip_edges(false) { |
---|
[295] | 1071 | if (!(*_os)) { |
---|
| 1072 | delete _os; |
---|
| 1073 | throw IoError("Cannot write file", fn); |
---|
| 1074 | } |
---|
[290] | 1075 | } |
---|
[165] | 1076 | |
---|
| 1077 | /// \brief Destructor |
---|
| 1078 | ~GraphWriter() { |
---|
[209] | 1079 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 1080 | it != _node_maps.end(); ++it) { |
---|
| 1081 | delete it->second; |
---|
[165] | 1082 | } |
---|
| 1083 | |
---|
[209] | 1084 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
| 1085 | it != _edge_maps.end(); ++it) { |
---|
| 1086 | delete it->second; |
---|
[165] | 1087 | } |
---|
| 1088 | |
---|
[209] | 1089 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 1090 | it != _attributes.end(); ++it) { |
---|
| 1091 | delete it->second; |
---|
[165] | 1092 | } |
---|
| 1093 | |
---|
| 1094 | if (local_os) { |
---|
[209] | 1095 | delete _os; |
---|
[165] | 1096 | } |
---|
| 1097 | } |
---|
[209] | 1098 | |
---|
[190] | 1099 | private: |
---|
[165] | 1100 | |
---|
[598] | 1101 | template <typename TGR> |
---|
| 1102 | friend GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os); |
---|
| 1103 | template <typename TGR> |
---|
| 1104 | friend GraphWriter<TGR> graphWriter(const TGR& graph, |
---|
| 1105 | const std::string& fn); |
---|
| 1106 | template <typename TGR> |
---|
| 1107 | friend GraphWriter<TGR> graphWriter(const TGR& graph, const char *fn); |
---|
[498] | 1108 | |
---|
[209] | 1109 | GraphWriter(GraphWriter& other) |
---|
[190] | 1110 | : _os(other._os), local_os(other.local_os), _graph(other._graph), |
---|
[209] | 1111 | _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
---|
[190] | 1112 | |
---|
| 1113 | other._os = 0; |
---|
| 1114 | other.local_os = false; |
---|
| 1115 | |
---|
| 1116 | _node_index.swap(other._node_index); |
---|
| 1117 | _edge_index.swap(other._edge_index); |
---|
| 1118 | |
---|
| 1119 | _node_maps.swap(other._node_maps); |
---|
| 1120 | _edge_maps.swap(other._edge_maps); |
---|
| 1121 | _attributes.swap(other._attributes); |
---|
| 1122 | |
---|
| 1123 | _nodes_caption = other._nodes_caption; |
---|
| 1124 | _edges_caption = other._edges_caption; |
---|
| 1125 | _attributes_caption = other._attributes_caption; |
---|
| 1126 | } |
---|
| 1127 | |
---|
[165] | 1128 | GraphWriter& operator=(const GraphWriter&); |
---|
| 1129 | |
---|
| 1130 | public: |
---|
| 1131 | |
---|
[584] | 1132 | /// \name Writing Rules |
---|
[165] | 1133 | /// @{ |
---|
[209] | 1134 | |
---|
[192] | 1135 | /// \brief Node map writing rule |
---|
[165] | 1136 | /// |
---|
[192] | 1137 | /// Add a node map writing rule to the writer. |
---|
[165] | 1138 | template <typename Map> |
---|
| 1139 | GraphWriter& nodeMap(const std::string& caption, const Map& map) { |
---|
| 1140 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1141 | _writer_bits::MapStorageBase<Node>* storage = |
---|
| 1142 | new _writer_bits::MapStorage<Node, Map>(map); |
---|
[165] | 1143 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1144 | return *this; |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | /// \brief Node map writing rule |
---|
| 1148 | /// |
---|
| 1149 | /// Add a node map writing rule with specialized converter to the |
---|
| 1150 | /// writer. |
---|
| 1151 | template <typename Map, typename Converter> |
---|
[209] | 1152 | GraphWriter& nodeMap(const std::string& caption, const Map& map, |
---|
| 1153 | const Converter& converter = Converter()) { |
---|
[165] | 1154 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1155 | _writer_bits::MapStorageBase<Node>* storage = |
---|
| 1156 | new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[165] | 1157 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1158 | return *this; |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | /// \brief Edge map writing rule |
---|
| 1162 | /// |
---|
| 1163 | /// Add an edge map writing rule to the writer. |
---|
| 1164 | template <typename Map> |
---|
| 1165 | GraphWriter& edgeMap(const std::string& caption, const Map& map) { |
---|
| 1166 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1167 | _writer_bits::MapStorageBase<Edge>* storage = |
---|
| 1168 | new _writer_bits::MapStorage<Edge, Map>(map); |
---|
[165] | 1169 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1170 | return *this; |
---|
| 1171 | } |
---|
| 1172 | |
---|
| 1173 | /// \brief Edge map writing rule |
---|
| 1174 | /// |
---|
| 1175 | /// Add an edge map writing rule with specialized converter to the |
---|
| 1176 | /// writer. |
---|
| 1177 | template <typename Map, typename Converter> |
---|
[209] | 1178 | GraphWriter& edgeMap(const std::string& caption, const Map& map, |
---|
| 1179 | const Converter& converter = Converter()) { |
---|
[165] | 1180 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1181 | _writer_bits::MapStorageBase<Edge>* storage = |
---|
| 1182 | new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter); |
---|
[165] | 1183 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1184 | return *this; |
---|
| 1185 | } |
---|
| 1186 | |
---|
| 1187 | /// \brief Arc map writing rule |
---|
| 1188 | /// |
---|
| 1189 | /// Add an arc map writing rule to the writer. |
---|
| 1190 | template <typename Map> |
---|
| 1191 | GraphWriter& arcMap(const std::string& caption, const Map& map) { |
---|
| 1192 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1193 | _writer_bits::MapStorageBase<Edge>* forward_storage = |
---|
[598] | 1194 | new _writer_bits::GraphArcMapStorage<GR, true, Map>(_graph, map); |
---|
[165] | 1195 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1196 | _writer_bits::MapStorageBase<Edge>* backward_storage = |
---|
[598] | 1197 | new _writer_bits::GraphArcMapStorage<GR, false, Map>(_graph, map); |
---|
[165] | 1198 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1199 | return *this; |
---|
| 1200 | } |
---|
| 1201 | |
---|
| 1202 | /// \brief Arc map writing rule |
---|
| 1203 | /// |
---|
| 1204 | /// Add an arc map writing rule with specialized converter to the |
---|
| 1205 | /// writer. |
---|
| 1206 | template <typename Map, typename Converter> |
---|
[209] | 1207 | GraphWriter& arcMap(const std::string& caption, const Map& map, |
---|
| 1208 | const Converter& converter = Converter()) { |
---|
[165] | 1209 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1210 | _writer_bits::MapStorageBase<Edge>* forward_storage = |
---|
[598] | 1211 | new _writer_bits::GraphArcMapStorage<GR, true, Map, Converter> |
---|
[209] | 1212 | (_graph, map, converter); |
---|
[165] | 1213 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1214 | _writer_bits::MapStorageBase<Edge>* backward_storage = |
---|
[598] | 1215 | new _writer_bits::GraphArcMapStorage<GR, false, Map, Converter> |
---|
[209] | 1216 | (_graph, map, converter); |
---|
[165] | 1217 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1218 | return *this; |
---|
| 1219 | } |
---|
| 1220 | |
---|
| 1221 | /// \brief Attribute writing rule |
---|
| 1222 | /// |
---|
| 1223 | /// Add an attribute writing rule to the writer. |
---|
| 1224 | template <typename Value> |
---|
| 1225 | GraphWriter& attribute(const std::string& caption, const Value& value) { |
---|
[209] | 1226 | _writer_bits::ValueStorageBase* storage = |
---|
| 1227 | new _writer_bits::ValueStorage<Value>(value); |
---|
[165] | 1228 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 1229 | return *this; |
---|
| 1230 | } |
---|
| 1231 | |
---|
| 1232 | /// \brief Attribute writing rule |
---|
| 1233 | /// |
---|
| 1234 | /// Add an attribute writing rule with specialized converter to the |
---|
| 1235 | /// writer. |
---|
| 1236 | template <typename Value, typename Converter> |
---|
[209] | 1237 | GraphWriter& attribute(const std::string& caption, const Value& value, |
---|
| 1238 | const Converter& converter = Converter()) { |
---|
| 1239 | _writer_bits::ValueStorageBase* storage = |
---|
| 1240 | new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[165] | 1241 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 1242 | return *this; |
---|
| 1243 | } |
---|
| 1244 | |
---|
| 1245 | /// \brief Node writing rule |
---|
| 1246 | /// |
---|
| 1247 | /// Add a node writing rule to the writer. |
---|
| 1248 | GraphWriter& node(const std::string& caption, const Node& node) { |
---|
| 1249 | typedef _writer_bits::MapLookUpConverter<Node> Converter; |
---|
| 1250 | Converter converter(_node_index); |
---|
[209] | 1251 | _writer_bits::ValueStorageBase* storage = |
---|
| 1252 | new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[165] | 1253 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 1254 | return *this; |
---|
| 1255 | } |
---|
| 1256 | |
---|
| 1257 | /// \brief Edge writing rule |
---|
| 1258 | /// |
---|
| 1259 | /// Add an edge writing rule to writer. |
---|
| 1260 | GraphWriter& edge(const std::string& caption, const Edge& edge) { |
---|
| 1261 | typedef _writer_bits::MapLookUpConverter<Edge> Converter; |
---|
| 1262 | Converter converter(_edge_index); |
---|
[209] | 1263 | _writer_bits::ValueStorageBase* storage = |
---|
| 1264 | new _writer_bits::ValueStorage<Edge, Converter>(edge, converter); |
---|
[165] | 1265 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 1266 | return *this; |
---|
| 1267 | } |
---|
| 1268 | |
---|
| 1269 | /// \brief Arc writing rule |
---|
| 1270 | /// |
---|
| 1271 | /// Add an arc writing rule to writer. |
---|
| 1272 | GraphWriter& arc(const std::string& caption, const Arc& arc) { |
---|
[598] | 1273 | typedef _writer_bits::GraphArcLookUpConverter<GR> Converter; |
---|
[165] | 1274 | Converter converter(_graph, _edge_index); |
---|
[209] | 1275 | _writer_bits::ValueStorageBase* storage = |
---|
| 1276 | new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[165] | 1277 | _attributes.push_back(std::make_pair(caption, storage)); |
---|
| 1278 | return *this; |
---|
| 1279 | } |
---|
| 1280 | |
---|
[584] | 1281 | /// \name Section Captions |
---|
[165] | 1282 | /// @{ |
---|
| 1283 | |
---|
[192] | 1284 | /// \brief Add an additional caption to the \c \@nodes section |
---|
[165] | 1285 | /// |
---|
[192] | 1286 | /// Add an additional caption to the \c \@nodes section. |
---|
[165] | 1287 | GraphWriter& nodes(const std::string& caption) { |
---|
| 1288 | _nodes_caption = caption; |
---|
| 1289 | return *this; |
---|
| 1290 | } |
---|
| 1291 | |
---|
[192] | 1292 | /// \brief Add an additional caption to the \c \@arcs section |
---|
[165] | 1293 | /// |
---|
[192] | 1294 | /// Add an additional caption to the \c \@arcs section. |
---|
[165] | 1295 | GraphWriter& edges(const std::string& caption) { |
---|
| 1296 | _edges_caption = caption; |
---|
| 1297 | return *this; |
---|
| 1298 | } |
---|
| 1299 | |
---|
[192] | 1300 | /// \brief Add an additional caption to the \c \@attributes section |
---|
[165] | 1301 | /// |
---|
[192] | 1302 | /// Add an additional caption to the \c \@attributes section. |
---|
[165] | 1303 | GraphWriter& attributes(const std::string& caption) { |
---|
| 1304 | _attributes_caption = caption; |
---|
| 1305 | return *this; |
---|
| 1306 | } |
---|
| 1307 | |
---|
[584] | 1308 | /// \name Skipping Section |
---|
[165] | 1309 | /// @{ |
---|
| 1310 | |
---|
| 1311 | /// \brief Skip writing the node set |
---|
| 1312 | /// |
---|
[192] | 1313 | /// The \c \@nodes section will not be written to the stream. |
---|
[165] | 1314 | GraphWriter& skipNodes() { |
---|
| 1315 | LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
---|
[185] | 1316 | _skip_nodes = true; |
---|
[165] | 1317 | return *this; |
---|
| 1318 | } |
---|
| 1319 | |
---|
| 1320 | /// \brief Skip writing edge set |
---|
| 1321 | /// |
---|
[192] | 1322 | /// The \c \@edges section will not be written to the stream. |
---|
[165] | 1323 | GraphWriter& skipEdges() { |
---|
| 1324 | LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member"); |
---|
[185] | 1325 | _skip_edges = true; |
---|
[165] | 1326 | return *this; |
---|
| 1327 | } |
---|
| 1328 | |
---|
| 1329 | /// @} |
---|
| 1330 | |
---|
| 1331 | private: |
---|
| 1332 | |
---|
| 1333 | void writeNodes() { |
---|
| 1334 | _writer_bits::MapStorageBase<Node>* label = 0; |
---|
| 1335 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 1336 | it != _node_maps.end(); ++it) { |
---|
[165] | 1337 | if (it->first == "label") { |
---|
[209] | 1338 | label = it->second; |
---|
| 1339 | break; |
---|
| 1340 | } |
---|
[165] | 1341 | } |
---|
| 1342 | |
---|
| 1343 | *_os << "@nodes"; |
---|
| 1344 | if (!_nodes_caption.empty()) { |
---|
[209] | 1345 | _writer_bits::writeToken(*_os << ' ', _nodes_caption); |
---|
[165] | 1346 | } |
---|
| 1347 | *_os << std::endl; |
---|
| 1348 | |
---|
| 1349 | if (label == 0) { |
---|
[209] | 1350 | *_os << "label" << '\t'; |
---|
[165] | 1351 | } |
---|
| 1352 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 1353 | it != _node_maps.end(); ++it) { |
---|
| 1354 | _writer_bits::writeToken(*_os, it->first) << '\t'; |
---|
[165] | 1355 | } |
---|
| 1356 | *_os << std::endl; |
---|
| 1357 | |
---|
| 1358 | std::vector<Node> nodes; |
---|
| 1359 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1360 | nodes.push_back(n); |
---|
[165] | 1361 | } |
---|
[209] | 1362 | |
---|
[165] | 1363 | if (label == 0) { |
---|
[598] | 1364 | IdMap<GR, Node> id_map(_graph); |
---|
| 1365 | _writer_bits::MapLess<IdMap<GR, Node> > id_less(id_map); |
---|
[209] | 1366 | std::sort(nodes.begin(), nodes.end(), id_less); |
---|
[165] | 1367 | } else { |
---|
[209] | 1368 | label->sort(nodes); |
---|
[165] | 1369 | } |
---|
| 1370 | |
---|
| 1371 | for (int i = 0; i < static_cast<int>(nodes.size()); ++i) { |
---|
[209] | 1372 | Node n = nodes[i]; |
---|
| 1373 | if (label == 0) { |
---|
| 1374 | std::ostringstream os; |
---|
| 1375 | os << _graph.id(n); |
---|
| 1376 | _writer_bits::writeToken(*_os, os.str()); |
---|
| 1377 | *_os << '\t'; |
---|
| 1378 | _node_index.insert(std::make_pair(n, os.str())); |
---|
| 1379 | } |
---|
| 1380 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 1381 | it != _node_maps.end(); ++it) { |
---|
| 1382 | std::string value = it->second->get(n); |
---|
| 1383 | _writer_bits::writeToken(*_os, value); |
---|
| 1384 | if (it->first == "label") { |
---|
| 1385 | _node_index.insert(std::make_pair(n, value)); |
---|
| 1386 | } |
---|
| 1387 | *_os << '\t'; |
---|
| 1388 | } |
---|
| 1389 | *_os << std::endl; |
---|
[165] | 1390 | } |
---|
| 1391 | } |
---|
| 1392 | |
---|
[185] | 1393 | void createNodeIndex() { |
---|
| 1394 | _writer_bits::MapStorageBase<Node>* label = 0; |
---|
| 1395 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
[209] | 1396 | it != _node_maps.end(); ++it) { |
---|
[185] | 1397 | if (it->first == "label") { |
---|
[209] | 1398 | label = it->second; |
---|
| 1399 | break; |
---|
| 1400 | } |
---|
[185] | 1401 | } |
---|
| 1402 | |
---|
| 1403 | if (label == 0) { |
---|
[209] | 1404 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 1405 | std::ostringstream os; |
---|
| 1406 | os << _graph.id(n); |
---|
| 1407 | _node_index.insert(std::make_pair(n, os.str())); |
---|
| 1408 | } |
---|
[185] | 1409 | } else { |
---|
[209] | 1410 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 1411 | std::string value = label->get(n); |
---|
| 1412 | _node_index.insert(std::make_pair(n, value)); |
---|
| 1413 | } |
---|
[185] | 1414 | } |
---|
| 1415 | } |
---|
| 1416 | |
---|
[165] | 1417 | void writeEdges() { |
---|
| 1418 | _writer_bits::MapStorageBase<Edge>* label = 0; |
---|
| 1419 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
[209] | 1420 | it != _edge_maps.end(); ++it) { |
---|
[165] | 1421 | if (it->first == "label") { |
---|
[209] | 1422 | label = it->second; |
---|
| 1423 | break; |
---|
| 1424 | } |
---|
[165] | 1425 | } |
---|
| 1426 | |
---|
| 1427 | *_os << "@edges"; |
---|
| 1428 | if (!_edges_caption.empty()) { |
---|
[209] | 1429 | _writer_bits::writeToken(*_os << ' ', _edges_caption); |
---|
[165] | 1430 | } |
---|
| 1431 | *_os << std::endl; |
---|
| 1432 | |
---|
| 1433 | *_os << '\t' << '\t'; |
---|
| 1434 | if (label == 0) { |
---|
[209] | 1435 | *_os << "label" << '\t'; |
---|
[165] | 1436 | } |
---|
| 1437 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
[209] | 1438 | it != _edge_maps.end(); ++it) { |
---|
| 1439 | _writer_bits::writeToken(*_os, it->first) << '\t'; |
---|
[165] | 1440 | } |
---|
| 1441 | *_os << std::endl; |
---|
| 1442 | |
---|
| 1443 | std::vector<Edge> edges; |
---|
| 1444 | for (EdgeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1445 | edges.push_back(n); |
---|
[165] | 1446 | } |
---|
[209] | 1447 | |
---|
[165] | 1448 | if (label == 0) { |
---|
[598] | 1449 | IdMap<GR, Edge> id_map(_graph); |
---|
| 1450 | _writer_bits::MapLess<IdMap<GR, Edge> > id_less(id_map); |
---|
[209] | 1451 | std::sort(edges.begin(), edges.end(), id_less); |
---|
[165] | 1452 | } else { |
---|
[209] | 1453 | label->sort(edges); |
---|
[165] | 1454 | } |
---|
| 1455 | |
---|
| 1456 | for (int i = 0; i < static_cast<int>(edges.size()); ++i) { |
---|
[209] | 1457 | Edge e = edges[i]; |
---|
| 1458 | _writer_bits::writeToken(*_os, _node_index. |
---|
| 1459 | find(_graph.u(e))->second); |
---|
| 1460 | *_os << '\t'; |
---|
| 1461 | _writer_bits::writeToken(*_os, _node_index. |
---|
| 1462 | find(_graph.v(e))->second); |
---|
| 1463 | *_os << '\t'; |
---|
| 1464 | if (label == 0) { |
---|
| 1465 | std::ostringstream os; |
---|
| 1466 | os << _graph.id(e); |
---|
| 1467 | _writer_bits::writeToken(*_os, os.str()); |
---|
| 1468 | *_os << '\t'; |
---|
| 1469 | _edge_index.insert(std::make_pair(e, os.str())); |
---|
| 1470 | } |
---|
| 1471 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
| 1472 | it != _edge_maps.end(); ++it) { |
---|
| 1473 | std::string value = it->second->get(e); |
---|
| 1474 | _writer_bits::writeToken(*_os, value); |
---|
| 1475 | if (it->first == "label") { |
---|
| 1476 | _edge_index.insert(std::make_pair(e, value)); |
---|
| 1477 | } |
---|
| 1478 | *_os << '\t'; |
---|
| 1479 | } |
---|
| 1480 | *_os << std::endl; |
---|
[165] | 1481 | } |
---|
| 1482 | } |
---|
| 1483 | |
---|
[185] | 1484 | void createEdgeIndex() { |
---|
| 1485 | _writer_bits::MapStorageBase<Edge>* label = 0; |
---|
| 1486 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
[209] | 1487 | it != _edge_maps.end(); ++it) { |
---|
[185] | 1488 | if (it->first == "label") { |
---|
[209] | 1489 | label = it->second; |
---|
| 1490 | break; |
---|
| 1491 | } |
---|
[185] | 1492 | } |
---|
| 1493 | |
---|
| 1494 | if (label == 0) { |
---|
[209] | 1495 | for (EdgeIt e(_graph); e != INVALID; ++e) { |
---|
| 1496 | std::ostringstream os; |
---|
| 1497 | os << _graph.id(e); |
---|
| 1498 | _edge_index.insert(std::make_pair(e, os.str())); |
---|
| 1499 | } |
---|
[185] | 1500 | } else { |
---|
[209] | 1501 | for (EdgeIt e(_graph); e != INVALID; ++e) { |
---|
| 1502 | std::string value = label->get(e); |
---|
| 1503 | _edge_index.insert(std::make_pair(e, value)); |
---|
| 1504 | } |
---|
[185] | 1505 | } |
---|
| 1506 | } |
---|
| 1507 | |
---|
[165] | 1508 | void writeAttributes() { |
---|
| 1509 | if (_attributes.empty()) return; |
---|
| 1510 | *_os << "@attributes"; |
---|
| 1511 | if (!_attributes_caption.empty()) { |
---|
[209] | 1512 | _writer_bits::writeToken(*_os << ' ', _attributes_caption); |
---|
[165] | 1513 | } |
---|
| 1514 | *_os << std::endl; |
---|
| 1515 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 1516 | it != _attributes.end(); ++it) { |
---|
| 1517 | _writer_bits::writeToken(*_os, it->first) << ' '; |
---|
| 1518 | _writer_bits::writeToken(*_os, it->second->get()); |
---|
| 1519 | *_os << std::endl; |
---|
[165] | 1520 | } |
---|
| 1521 | } |
---|
[209] | 1522 | |
---|
[165] | 1523 | public: |
---|
[209] | 1524 | |
---|
[584] | 1525 | /// \name Execution of the Writer |
---|
[165] | 1526 | /// @{ |
---|
| 1527 | |
---|
| 1528 | /// \brief Start the batch processing |
---|
| 1529 | /// |
---|
[192] | 1530 | /// This function starts the batch processing. |
---|
[165] | 1531 | void run() { |
---|
| 1532 | if (!_skip_nodes) { |
---|
[209] | 1533 | writeNodes(); |
---|
[185] | 1534 | } else { |
---|
[209] | 1535 | createNodeIndex(); |
---|
[165] | 1536 | } |
---|
[209] | 1537 | if (!_skip_edges) { |
---|
| 1538 | writeEdges(); |
---|
[185] | 1539 | } else { |
---|
[209] | 1540 | createEdgeIndex(); |
---|
[165] | 1541 | } |
---|
| 1542 | writeAttributes(); |
---|
| 1543 | } |
---|
| 1544 | |
---|
[192] | 1545 | /// \brief Give back the stream of the writer |
---|
[165] | 1546 | /// |
---|
[192] | 1547 | /// Give back the stream of the writer |
---|
[165] | 1548 | std::ostream& ostream() { |
---|
| 1549 | return *_os; |
---|
| 1550 | } |
---|
| 1551 | |
---|
| 1552 | /// @} |
---|
| 1553 | }; |
---|
| 1554 | |
---|
[598] | 1555 | /// \ingroup lemon_io |
---|
| 1556 | /// |
---|
[498] | 1557 | /// \brief Return a \ref GraphWriter class |
---|
| 1558 | /// |
---|
[598] | 1559 | /// This function just returns a \ref GraphWriter class. |
---|
| 1560 | /// |
---|
| 1561 | /// With this function a graph can be write to a file or output |
---|
| 1562 | /// stream in \ref lgf-format "LGF" format with several maps and |
---|
| 1563 | /// attributes. For example, with the following code a weighted |
---|
| 1564 | /// matching problem can be written to the standard output, i.e. a |
---|
| 1565 | /// graph with a \e weight map on the edges: |
---|
| 1566 | /// |
---|
| 1567 | ///\code |
---|
| 1568 | ///ListGraph graph; |
---|
| 1569 | ///ListGraph::EdgeMap<int> weight(graph); |
---|
| 1570 | /// // Setting the weight map |
---|
| 1571 | ///graphWriter(graph, std::cout). |
---|
| 1572 | /// edgeMap("weight", weight). |
---|
| 1573 | /// run(); |
---|
| 1574 | ///\endcode |
---|
| 1575 | /// |
---|
| 1576 | /// For a complete documentation, please see the \ref GraphWriter |
---|
| 1577 | /// class documentation. |
---|
| 1578 | /// \warning Don't forget to put the \ref GraphWriter::run() "run()" |
---|
| 1579 | /// to the end of the parameter list. |
---|
[498] | 1580 | /// \relates GraphWriter |
---|
[598] | 1581 | /// \sa graphWriter(const TGR& graph, const std::string& fn) |
---|
| 1582 | /// \sa graphWriter(const TGR& graph, const char* fn) |
---|
| 1583 | template <typename TGR> |
---|
| 1584 | GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os) { |
---|
| 1585 | GraphWriter<TGR> tmp(graph, os); |
---|
[498] | 1586 | return tmp; |
---|
| 1587 | } |
---|
| 1588 | |
---|
| 1589 | /// \brief Return a \ref GraphWriter class |
---|
| 1590 | /// |
---|
| 1591 | /// This function just returns a \ref GraphWriter class. |
---|
| 1592 | /// \relates GraphWriter |
---|
[598] | 1593 | /// \sa graphWriter(const TGR& graph, std::ostream& os) |
---|
| 1594 | template <typename TGR> |
---|
| 1595 | GraphWriter<TGR> graphWriter(const TGR& graph, const std::string& fn) { |
---|
| 1596 | GraphWriter<TGR> tmp(graph, fn); |
---|
[498] | 1597 | return tmp; |
---|
| 1598 | } |
---|
| 1599 | |
---|
| 1600 | /// \brief Return a \ref GraphWriter class |
---|
| 1601 | /// |
---|
| 1602 | /// This function just returns a \ref GraphWriter class. |
---|
| 1603 | /// \relates GraphWriter |
---|
[598] | 1604 | /// \sa graphWriter(const TGR& graph, std::ostream& os) |
---|
| 1605 | template <typename TGR> |
---|
| 1606 | GraphWriter<TGR> graphWriter(const TGR& graph, const char* fn) { |
---|
| 1607 | GraphWriter<TGR> tmp(graph, fn); |
---|
[498] | 1608 | return tmp; |
---|
| 1609 | } |
---|
| 1610 | |
---|
[248] | 1611 | class SectionWriter; |
---|
| 1612 | |
---|
| 1613 | SectionWriter sectionWriter(std::istream& is); |
---|
| 1614 | SectionWriter sectionWriter(const std::string& fn); |
---|
| 1615 | SectionWriter sectionWriter(const char* fn); |
---|
| 1616 | |
---|
| 1617 | /// \ingroup lemon_io |
---|
| 1618 | /// |
---|
| 1619 | /// \brief Section writer class |
---|
| 1620 | /// |
---|
| 1621 | /// In the \ref lgf-format "LGF" file extra sections can be placed, |
---|
| 1622 | /// which contain any data in arbitrary format. Such sections can be |
---|
| 1623 | /// written with this class. A writing rule can be added to the |
---|
| 1624 | /// class with two different functions. With the \c sectionLines() |
---|
| 1625 | /// function a generator can write the section line-by-line, while |
---|
| 1626 | /// with the \c sectionStream() member the section can be written to |
---|
| 1627 | /// an output stream. |
---|
| 1628 | class SectionWriter { |
---|
| 1629 | private: |
---|
| 1630 | |
---|
| 1631 | std::ostream* _os; |
---|
| 1632 | bool local_os; |
---|
| 1633 | |
---|
| 1634 | typedef std::vector<std::pair<std::string, _writer_bits::Section*> > |
---|
| 1635 | Sections; |
---|
| 1636 | |
---|
| 1637 | Sections _sections; |
---|
| 1638 | |
---|
| 1639 | public: |
---|
| 1640 | |
---|
| 1641 | /// \brief Constructor |
---|
| 1642 | /// |
---|
| 1643 | /// Construct a section writer, which writes to the given output |
---|
| 1644 | /// stream. |
---|
| 1645 | SectionWriter(std::ostream& os) |
---|
| 1646 | : _os(&os), local_os(false) {} |
---|
| 1647 | |
---|
| 1648 | /// \brief Constructor |
---|
| 1649 | /// |
---|
| 1650 | /// Construct a section writer, which writes into the given file. |
---|
| 1651 | SectionWriter(const std::string& fn) |
---|
[290] | 1652 | : _os(new std::ofstream(fn.c_str())), local_os(true) { |
---|
[295] | 1653 | if (!(*_os)) { |
---|
| 1654 | delete _os; |
---|
| 1655 | throw IoError("Cannot write file", fn); |
---|
| 1656 | } |
---|
[290] | 1657 | } |
---|
[248] | 1658 | |
---|
| 1659 | /// \brief Constructor |
---|
| 1660 | /// |
---|
| 1661 | /// Construct a section writer, which writes into the given file. |
---|
| 1662 | SectionWriter(const char* fn) |
---|
[290] | 1663 | : _os(new std::ofstream(fn)), local_os(true) { |
---|
[295] | 1664 | if (!(*_os)) { |
---|
| 1665 | delete _os; |
---|
| 1666 | throw IoError("Cannot write file", fn); |
---|
| 1667 | } |
---|
[290] | 1668 | } |
---|
[248] | 1669 | |
---|
| 1670 | /// \brief Destructor |
---|
| 1671 | ~SectionWriter() { |
---|
| 1672 | for (Sections::iterator it = _sections.begin(); |
---|
| 1673 | it != _sections.end(); ++it) { |
---|
| 1674 | delete it->second; |
---|
| 1675 | } |
---|
| 1676 | |
---|
| 1677 | if (local_os) { |
---|
| 1678 | delete _os; |
---|
| 1679 | } |
---|
| 1680 | |
---|
| 1681 | } |
---|
| 1682 | |
---|
| 1683 | private: |
---|
| 1684 | |
---|
| 1685 | friend SectionWriter sectionWriter(std::ostream& os); |
---|
| 1686 | friend SectionWriter sectionWriter(const std::string& fn); |
---|
| 1687 | friend SectionWriter sectionWriter(const char* fn); |
---|
| 1688 | |
---|
| 1689 | SectionWriter(SectionWriter& other) |
---|
| 1690 | : _os(other._os), local_os(other.local_os) { |
---|
| 1691 | |
---|
| 1692 | other._os = 0; |
---|
| 1693 | other.local_os = false; |
---|
| 1694 | |
---|
| 1695 | _sections.swap(other._sections); |
---|
| 1696 | } |
---|
| 1697 | |
---|
| 1698 | SectionWriter& operator=(const SectionWriter&); |
---|
| 1699 | |
---|
| 1700 | public: |
---|
| 1701 | |
---|
[584] | 1702 | /// \name Section Writers |
---|
[248] | 1703 | /// @{ |
---|
| 1704 | |
---|
| 1705 | /// \brief Add a section writer with line oriented writing |
---|
| 1706 | /// |
---|
| 1707 | /// The first parameter is the type descriptor of the section, the |
---|
| 1708 | /// second is a generator with std::string values. At the writing |
---|
| 1709 | /// process, the returned \c std::string will be written into the |
---|
| 1710 | /// output file until it is an empty string. |
---|
| 1711 | /// |
---|
| 1712 | /// For example, an integer vector is written into a section. |
---|
| 1713 | ///\code |
---|
| 1714 | /// @numbers |
---|
| 1715 | /// 12 45 23 78 |
---|
| 1716 | /// 4 28 38 28 |
---|
| 1717 | /// 23 6 16 |
---|
| 1718 | ///\endcode |
---|
| 1719 | /// |
---|
| 1720 | /// The generator is implemented as a struct. |
---|
| 1721 | ///\code |
---|
| 1722 | /// struct NumberSection { |
---|
| 1723 | /// std::vector<int>::const_iterator _it, _end; |
---|
| 1724 | /// NumberSection(const std::vector<int>& data) |
---|
| 1725 | /// : _it(data.begin()), _end(data.end()) {} |
---|
| 1726 | /// std::string operator()() { |
---|
| 1727 | /// int rem_in_line = 4; |
---|
| 1728 | /// std::ostringstream ls; |
---|
| 1729 | /// while (rem_in_line > 0 && _it != _end) { |
---|
| 1730 | /// ls << *(_it++) << ' '; |
---|
| 1731 | /// --rem_in_line; |
---|
| 1732 | /// } |
---|
| 1733 | /// return ls.str(); |
---|
| 1734 | /// } |
---|
| 1735 | /// }; |
---|
| 1736 | /// |
---|
| 1737 | /// // ... |
---|
| 1738 | /// |
---|
| 1739 | /// writer.sectionLines("numbers", NumberSection(vec)); |
---|
| 1740 | ///\endcode |
---|
| 1741 | template <typename Functor> |
---|
| 1742 | SectionWriter& sectionLines(const std::string& type, Functor functor) { |
---|
| 1743 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
| 1744 | _sections.push_back(std::make_pair(type, |
---|
| 1745 | new _writer_bits::LineSection<Functor>(functor))); |
---|
| 1746 | return *this; |
---|
| 1747 | } |
---|
| 1748 | |
---|
| 1749 | |
---|
| 1750 | /// \brief Add a section writer with stream oriented writing |
---|
| 1751 | /// |
---|
| 1752 | /// The first parameter is the type of the section, the second is |
---|
| 1753 | /// a functor, which takes a \c std::ostream& parameter. The |
---|
| 1754 | /// functor writes the section to the output stream. |
---|
| 1755 | /// \warning The last line must be closed with end-line character. |
---|
| 1756 | template <typename Functor> |
---|
| 1757 | SectionWriter& sectionStream(const std::string& type, Functor functor) { |
---|
| 1758 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
| 1759 | _sections.push_back(std::make_pair(type, |
---|
| 1760 | new _writer_bits::StreamSection<Functor>(functor))); |
---|
| 1761 | return *this; |
---|
| 1762 | } |
---|
| 1763 | |
---|
| 1764 | /// @} |
---|
| 1765 | |
---|
| 1766 | public: |
---|
| 1767 | |
---|
| 1768 | |
---|
[584] | 1769 | /// \name Execution of the Writer |
---|
[248] | 1770 | /// @{ |
---|
| 1771 | |
---|
| 1772 | /// \brief Start the batch processing |
---|
| 1773 | /// |
---|
| 1774 | /// This function starts the batch processing. |
---|
| 1775 | void run() { |
---|
| 1776 | |
---|
| 1777 | LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer"); |
---|
| 1778 | |
---|
| 1779 | for (Sections::iterator it = _sections.begin(); |
---|
| 1780 | it != _sections.end(); ++it) { |
---|
| 1781 | (*_os) << '@' << it->first << std::endl; |
---|
| 1782 | it->second->process(*_os); |
---|
| 1783 | } |
---|
| 1784 | } |
---|
| 1785 | |
---|
| 1786 | /// \brief Give back the stream of the writer |
---|
| 1787 | /// |
---|
| 1788 | /// Returns the stream of the writer |
---|
| 1789 | std::ostream& ostream() { |
---|
| 1790 | return *_os; |
---|
| 1791 | } |
---|
| 1792 | |
---|
| 1793 | /// @} |
---|
| 1794 | |
---|
| 1795 | }; |
---|
| 1796 | |
---|
[598] | 1797 | /// \ingroup lemon_io |
---|
| 1798 | /// |
---|
[248] | 1799 | /// \brief Return a \ref SectionWriter class |
---|
| 1800 | /// |
---|
| 1801 | /// This function just returns a \ref SectionWriter class. |
---|
[598] | 1802 | /// |
---|
| 1803 | /// Please see SectionWriter documentation about the custom section |
---|
| 1804 | /// output. |
---|
| 1805 | /// |
---|
[248] | 1806 | /// \relates SectionWriter |
---|
[598] | 1807 | /// \sa sectionWriter(const std::string& fn) |
---|
| 1808 | /// \sa sectionWriter(const char *fn) |
---|
[248] | 1809 | inline SectionWriter sectionWriter(std::ostream& os) { |
---|
| 1810 | SectionWriter tmp(os); |
---|
| 1811 | return tmp; |
---|
| 1812 | } |
---|
| 1813 | |
---|
| 1814 | /// \brief Return a \ref SectionWriter class |
---|
| 1815 | /// |
---|
| 1816 | /// This function just returns a \ref SectionWriter class. |
---|
| 1817 | /// \relates SectionWriter |
---|
[598] | 1818 | /// \sa sectionWriter(std::ostream& os) |
---|
[248] | 1819 | inline SectionWriter sectionWriter(const std::string& fn) { |
---|
| 1820 | SectionWriter tmp(fn); |
---|
| 1821 | return tmp; |
---|
| 1822 | } |
---|
| 1823 | |
---|
| 1824 | /// \brief Return a \ref SectionWriter class |
---|
| 1825 | /// |
---|
| 1826 | /// This function just returns a \ref SectionWriter class. |
---|
| 1827 | /// \relates SectionWriter |
---|
[598] | 1828 | /// \sa sectionWriter(std::ostream& os) |
---|
[248] | 1829 | inline SectionWriter sectionWriter(const char* fn) { |
---|
| 1830 | SectionWriter tmp(fn); |
---|
| 1831 | return tmp; |
---|
| 1832 | } |
---|
[127] | 1833 | } |
---|
| 1834 | |
---|
| 1835 | #endif |
---|