[1409] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1409] | 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 | |
---|
[2084] | 19 | ///\ingroup lemon_io |
---|
[1409] | 20 | ///\file |
---|
| 21 | ///\brief Lemon Format writer. |
---|
| 22 | |
---|
| 23 | #ifndef LEMON_LEMON_WRITER_H |
---|
| 24 | #define LEMON_LEMON_WRITER_H |
---|
| 25 | |
---|
| 26 | #include <iostream> |
---|
| 27 | #include <fstream> |
---|
| 28 | #include <string> |
---|
| 29 | #include <vector> |
---|
| 30 | #include <algorithm> |
---|
| 31 | #include <map> |
---|
| 32 | #include <memory> |
---|
| 33 | |
---|
| 34 | #include <lemon/error.h> |
---|
[1993] | 35 | #include <lemon/bits/invalid.h> |
---|
[1421] | 36 | #include <lemon/graph_utils.h> |
---|
[1409] | 37 | #include <lemon/bits/item_writer.h> |
---|
[1993] | 38 | #include <lemon/bits/utility.h> |
---|
[1421] | 39 | #include <lemon/maps.h> |
---|
[1705] | 40 | #include <lemon/xy.h> |
---|
[1409] | 41 | |
---|
[1476] | 42 | #include <lemon/concept_check.h> |
---|
| 43 | #include <lemon/concept/maps.h> |
---|
| 44 | |
---|
[1409] | 45 | |
---|
| 46 | namespace lemon { |
---|
| 47 | |
---|
[1476] | 48 | namespace _writer_bits { |
---|
| 49 | |
---|
[2101] | 50 | template <typename T> |
---|
| 51 | bool operator<(T, T) { |
---|
| 52 | throw DataFormatError("Label is not comparable"); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | template <typename T> |
---|
| 56 | struct Less { |
---|
| 57 | bool operator()(const T& p, const T& q) const { |
---|
| 58 | return p < q; |
---|
| 59 | } |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | template <typename Map> |
---|
| 63 | struct ComposeLess { |
---|
| 64 | ComposeLess(const Map& _map) : map(_map), less() {} |
---|
| 65 | |
---|
| 66 | bool operator()(const typename Map::Key& p, |
---|
| 67 | const typename Map::Key& q) const { |
---|
| 68 | return less(map[p], map[q]); |
---|
| 69 | } |
---|
| 70 | const Map& map; |
---|
| 71 | Less<typename Map::Value> less; |
---|
| 72 | }; |
---|
| 73 | |
---|
[1476] | 74 | template <typename Item> |
---|
[1901] | 75 | class ItemLabelWriter { |
---|
[1476] | 76 | public: |
---|
| 77 | |
---|
[1901] | 78 | bool isLabelWriter() { return true; } |
---|
[1476] | 79 | |
---|
[1901] | 80 | void writeLabel(std::ostream&, const Item&) {} |
---|
[1476] | 81 | |
---|
[1901] | 82 | template <class _ItemLabelWriter> |
---|
[1476] | 83 | struct Constraints { |
---|
| 84 | void constraints() { |
---|
[1901] | 85 | bool b = writer.isLabelWriter(); |
---|
[1476] | 86 | ignore_unused_variable_warning(b); |
---|
[1901] | 87 | writer.writeLabel(os, item); |
---|
[1476] | 88 | } |
---|
[1901] | 89 | _ItemLabelWriter& writer; |
---|
[1476] | 90 | std::ostream& os; |
---|
[1492] | 91 | const Item& item; |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | template <typename Item> |
---|
| 97 | class ItemWriter { |
---|
| 98 | public: |
---|
| 99 | |
---|
| 100 | void write(std::ostream&, const Item&) {} |
---|
| 101 | |
---|
| 102 | template <class _ItemWriter> |
---|
| 103 | struct Constraints { |
---|
| 104 | void constraints() { |
---|
| 105 | writer.write(os, item); |
---|
| 106 | } |
---|
| 107 | _ItemWriter& writer; |
---|
| 108 | std::ostream& os; |
---|
| 109 | const Item& item; |
---|
[1476] | 110 | }; |
---|
| 111 | |
---|
| 112 | }; |
---|
| 113 | |
---|
[1705] | 114 | template <typename Map> |
---|
| 115 | struct Ref { typedef const Map& Type; }; |
---|
| 116 | |
---|
| 117 | template <typename Graph, typename Map> |
---|
| 118 | class ForwardComposeMap { |
---|
| 119 | public: |
---|
[1909] | 120 | typedef typename Graph::UEdge Key; |
---|
[1705] | 121 | typedef typename Map::Value Value; |
---|
| 122 | |
---|
| 123 | ForwardComposeMap(const Graph& _graph, const Map& _map) |
---|
| 124 | : graph(_graph), map(_map) {} |
---|
| 125 | |
---|
| 126 | Value operator[](const Key& key) { |
---|
| 127 | return map[graph.direct(key, false)]; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | private: |
---|
| 131 | typename Ref<Map>::Type map; |
---|
| 132 | const Graph& graph; |
---|
| 133 | }; |
---|
| 134 | |
---|
| 135 | template <typename Graph, typename Map> |
---|
| 136 | ForwardComposeMap<Graph, Map> |
---|
| 137 | forwardComposeMap(const Graph& graph, const Map& map) { |
---|
| 138 | return ForwardComposeMap<Graph, Map>(graph, map); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | template <typename Graph, typename Map> |
---|
| 142 | class BackwardComposeMap { |
---|
| 143 | public: |
---|
[1909] | 144 | typedef typename Graph::UEdge Key; |
---|
[1705] | 145 | typedef typename Map::Value Value; |
---|
| 146 | |
---|
| 147 | BackwardComposeMap(const Graph& _graph, const Map& _map) |
---|
| 148 | : graph(_graph), map(_map) {} |
---|
| 149 | |
---|
| 150 | Value operator[](const Key& key) { |
---|
| 151 | return map[graph.direct(key, false)]; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | private: |
---|
| 155 | typename Ref<Map>::Type map; |
---|
| 156 | const Graph& graph; |
---|
| 157 | }; |
---|
| 158 | |
---|
| 159 | template <typename Graph, typename Map> |
---|
| 160 | BackwardComposeMap<Graph, Map> |
---|
| 161 | backwardComposeMap(const Graph& graph, const Map& map) { |
---|
| 162 | return BackwardComposeMap<Graph, Map>(graph, map); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | template <typename Graph, typename Map> |
---|
| 166 | struct Ref<ForwardComposeMap<Graph, Map> > { |
---|
| 167 | typedef ForwardComposeMap<Graph, Map> Type; |
---|
| 168 | }; |
---|
| 169 | |
---|
| 170 | template <typename Graph, typename Map> |
---|
| 171 | struct Ref<BackwardComposeMap<Graph, Map> > { |
---|
| 172 | typedef BackwardComposeMap<Graph, Map> Type; |
---|
| 173 | }; |
---|
| 174 | |
---|
| 175 | template <typename Map> |
---|
| 176 | struct Ref<XMap<Map> > { |
---|
| 177 | typedef XMap<Map> Type; |
---|
| 178 | }; |
---|
| 179 | template <typename Map> |
---|
| 180 | struct Ref<ConstXMap<Map> > { |
---|
| 181 | typedef ConstXMap<Map> Type; |
---|
| 182 | }; |
---|
| 183 | |
---|
| 184 | template <typename Map> |
---|
| 185 | struct Ref<YMap<Map> > { |
---|
| 186 | typedef YMap<Map> Type; |
---|
| 187 | }; |
---|
| 188 | template <typename Map> |
---|
| 189 | struct Ref<ConstYMap<Map> > { |
---|
| 190 | typedef ConstYMap<Map> Type; |
---|
| 191 | }; |
---|
| 192 | |
---|
[1845] | 193 | |
---|
| 194 | template <typename _Item> |
---|
| 195 | class MapWriterBase { |
---|
| 196 | public: |
---|
| 197 | typedef _Item Item; |
---|
| 198 | |
---|
| 199 | virtual ~MapWriterBase() {} |
---|
| 200 | |
---|
[1852] | 201 | virtual void write(std::ostream& os, const Item& item) const = 0; |
---|
[2101] | 202 | virtual void sortByMap(std::vector<Item>&) const = 0; |
---|
[1845] | 203 | }; |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | template <typename _Item, typename _Map, typename _Writer> |
---|
| 207 | class MapWriter : public MapWriterBase<_Item> { |
---|
| 208 | public: |
---|
| 209 | typedef _Map Map; |
---|
| 210 | typedef _Writer Writer; |
---|
| 211 | typedef typename Writer::Value Value; |
---|
| 212 | typedef _Item Item; |
---|
| 213 | |
---|
| 214 | typename _writer_bits::Ref<Map>::Type map; |
---|
| 215 | Writer writer; |
---|
| 216 | |
---|
| 217 | MapWriter(const Map& _map, const Writer& _writer) |
---|
| 218 | : map(_map), writer(_writer) {} |
---|
| 219 | |
---|
| 220 | virtual ~MapWriter() {} |
---|
| 221 | |
---|
[1852] | 222 | virtual void write(std::ostream& os, const Item& item) const { |
---|
[1845] | 223 | Value value = map[item]; |
---|
| 224 | writer.write(os, value); |
---|
| 225 | } |
---|
| 226 | |
---|
[2101] | 227 | virtual void sortByMap(std::vector<Item>& items) const { |
---|
| 228 | ComposeLess<Map> less(map); |
---|
| 229 | std::sort(items.begin(), items.end(), less); |
---|
| 230 | } |
---|
| 231 | |
---|
[1845] | 232 | }; |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | class ValueWriterBase { |
---|
| 236 | public: |
---|
| 237 | virtual ~ValueWriterBase() {} |
---|
| 238 | virtual void write(std::ostream&) = 0; |
---|
| 239 | }; |
---|
| 240 | |
---|
| 241 | template <typename _Value, typename _Writer> |
---|
| 242 | class ValueWriter : public ValueWriterBase { |
---|
| 243 | public: |
---|
| 244 | typedef _Value Value; |
---|
| 245 | typedef _Writer Writer; |
---|
| 246 | |
---|
| 247 | ValueWriter(const Value& _value, const Writer& _writer) |
---|
| 248 | : value(_value), writer(_writer) {} |
---|
| 249 | |
---|
| 250 | virtual void write(std::ostream& os) { |
---|
| 251 | writer.write(os, value); |
---|
| 252 | } |
---|
| 253 | private: |
---|
| 254 | const Value& value; |
---|
| 255 | Writer writer; |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | template <typename _Item> |
---|
[1901] | 260 | class LabelWriterBase { |
---|
[1845] | 261 | public: |
---|
| 262 | typedef _Item Item; |
---|
[1901] | 263 | virtual ~LabelWriterBase() {} |
---|
[1845] | 264 | virtual void write(std::ostream&, const Item&) const = 0; |
---|
[1901] | 265 | virtual bool isLabelWriter() const = 0; |
---|
[1845] | 266 | }; |
---|
| 267 | |
---|
[1901] | 268 | template <typename _Item, typename _BoxedLabelWriter> |
---|
| 269 | class LabelWriter : public LabelWriterBase<_Item> { |
---|
[1845] | 270 | public: |
---|
| 271 | typedef _Item Item; |
---|
[1901] | 272 | typedef _BoxedLabelWriter BoxedLabelWriter; |
---|
[1845] | 273 | |
---|
[1901] | 274 | const BoxedLabelWriter& labelWriter; |
---|
[1845] | 275 | |
---|
[1901] | 276 | LabelWriter(const BoxedLabelWriter& _labelWriter) |
---|
| 277 | : labelWriter(_labelWriter) {} |
---|
[1845] | 278 | |
---|
| 279 | virtual void write(std::ostream& os, const Item& item) const { |
---|
[1901] | 280 | labelWriter.writeLabel(os, item); |
---|
[1845] | 281 | } |
---|
| 282 | |
---|
[1901] | 283 | virtual bool isLabelWriter() const { |
---|
| 284 | return labelWriter.isLabelWriter(); |
---|
[1845] | 285 | } |
---|
| 286 | }; |
---|
| 287 | |
---|
[1476] | 288 | } |
---|
| 289 | |
---|
[2084] | 290 | /// \ingroup lemon_io |
---|
[1409] | 291 | /// \brief Lemon Format writer class. |
---|
| 292 | /// |
---|
| 293 | /// The Lemon Format contains several sections. We do not want to |
---|
| 294 | /// determine what sections are in a lemon file we give only a framework |
---|
| 295 | /// to write a section oriented format. |
---|
| 296 | /// |
---|
| 297 | /// In the Lemon Format each section starts with a line contains a \c \@ |
---|
| 298 | /// character on the first not white space position. This line is the |
---|
| 299 | /// header line of the section. Each next lines belong to this section |
---|
| 300 | /// while it does not starts with \c \@ character. This line can start a |
---|
| 301 | /// new section or if it can close the file with the \c \@end line. |
---|
| 302 | /// The file format ignore the empty lines and it may contain comments |
---|
| 303 | /// started with a \c # character to the end of the line. |
---|
| 304 | /// |
---|
| 305 | /// The framework provides an abstract LemonWriter::SectionWriter class |
---|
| 306 | /// what defines the interface of a SectionWriter. The SectionWriter |
---|
| 307 | /// has the \c header() member function what gives back the header of the |
---|
| 308 | /// section. After that it will be called the \c write() member which |
---|
| 309 | /// should write the content of the section. |
---|
| 310 | /// |
---|
| 311 | /// \relates GraphWriter |
---|
| 312 | /// \relates NodeSetWriter |
---|
| 313 | /// \relates EdgeSetWriter |
---|
| 314 | /// \relates NodesWriter |
---|
| 315 | /// \relates EdgesWriter |
---|
| 316 | /// \relates AttributeWriter |
---|
| 317 | class LemonWriter { |
---|
| 318 | public: |
---|
| 319 | |
---|
| 320 | /// \brief Abstract base class for writing a section. |
---|
| 321 | /// |
---|
| 322 | /// This class has an \c header() member function what gives back |
---|
| 323 | /// the header line of the section. The \c write() member should |
---|
| 324 | /// write the content of the section to the stream. |
---|
| 325 | class SectionWriter { |
---|
| 326 | friend class LemonWriter; |
---|
| 327 | protected: |
---|
| 328 | /// \brief Constructor for SectionWriter. |
---|
| 329 | /// |
---|
| 330 | /// Constructor for SectionWriter. It attach this writer to |
---|
| 331 | /// the given LemonWriter. |
---|
| 332 | SectionWriter(LemonWriter& writer) { |
---|
| 333 | writer.attach(*this); |
---|
| 334 | } |
---|
[1494] | 335 | |
---|
| 336 | virtual ~SectionWriter() {} |
---|
[1409] | 337 | |
---|
| 338 | /// \brief The header of section. |
---|
| 339 | /// |
---|
| 340 | /// It gives back the header of the section. |
---|
| 341 | virtual std::string header() = 0; |
---|
| 342 | |
---|
[2084] | 343 | /// \brief Writer function of the section. |
---|
[1409] | 344 | /// |
---|
| 345 | /// Write the content of the section. |
---|
| 346 | virtual void write(std::ostream& os) = 0; |
---|
[2084] | 347 | |
---|
| 348 | /// \brief Gives back true when the section should be written. |
---|
| 349 | /// |
---|
| 350 | /// Gives back true when the section should be written. |
---|
| 351 | virtual bool valid() { return true; } |
---|
[1409] | 352 | }; |
---|
| 353 | |
---|
| 354 | /// \brief Constructor for LemonWriter. |
---|
| 355 | /// |
---|
| 356 | /// Constructor for LemonWriter which writes to the given stream. |
---|
| 357 | LemonWriter(std::ostream& _os) |
---|
| 358 | : os(&_os), own_os(false) {} |
---|
| 359 | |
---|
| 360 | /// \brief Constructor for LemonWriter. |
---|
| 361 | /// |
---|
| 362 | /// Constructor for LemonWriter which writes to the given file. |
---|
| 363 | LemonWriter(const std::string& filename) |
---|
| 364 | : os(0), own_os(true) { |
---|
| 365 | os = new std::ofstream(filename.c_str()); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /// \brief Desctructor for LemonWriter. |
---|
| 369 | /// |
---|
| 370 | /// Desctructor for LemonWriter. |
---|
| 371 | ~LemonWriter() { |
---|
| 372 | if (own_os) { |
---|
| 373 | delete os; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | private: |
---|
| 378 | LemonWriter(const LemonWriter&); |
---|
| 379 | void operator=(const LemonWriter&); |
---|
| 380 | |
---|
| 381 | void attach(SectionWriter& writer) { |
---|
| 382 | writers.push_back(&writer); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | public: |
---|
| 386 | |
---|
| 387 | /// \brief Executes the LemonWriter. |
---|
| 388 | /// |
---|
| 389 | /// It executes the LemonWriter. |
---|
| 390 | void run() { |
---|
| 391 | SectionWriters::iterator it; |
---|
| 392 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
[2084] | 393 | if ((*it)->valid()) { |
---|
| 394 | *os << (*it)->header() << std::endl; |
---|
| 395 | (*it)->write(*os); |
---|
| 396 | } |
---|
[1409] | 397 | } |
---|
| 398 | *os << "@end" << std::endl; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | |
---|
| 402 | private: |
---|
| 403 | |
---|
| 404 | std::ostream* os; |
---|
| 405 | bool own_os; |
---|
| 406 | |
---|
| 407 | typedef std::vector<SectionWriter*> SectionWriters; |
---|
| 408 | SectionWriters writers; |
---|
| 409 | |
---|
| 410 | }; |
---|
| 411 | |
---|
[2016] | 412 | /// \ingroup section_io |
---|
[1409] | 413 | /// \brief SectionWriter for writing a graph's nodeset. |
---|
| 414 | /// |
---|
| 415 | /// The lemon format can store multiple graph nodesets with several maps. |
---|
[1901] | 416 | /// The nodeset section's header line is \c \@nodeset \c nodeset_name, but |
---|
| 417 | /// the \c nodeset_name may be empty. |
---|
[1409] | 418 | /// |
---|
| 419 | /// The first line of the section contains the names of the maps separated |
---|
| 420 | /// with white spaces. Each next lines describes a node in the nodeset, and |
---|
| 421 | /// contains the mapped values for each map. |
---|
| 422 | /// |
---|
[1901] | 423 | /// If the nodeset contains an \c "label" named map then it will be regarded |
---|
| 424 | /// as label map. This map should contain only unique values and when the |
---|
| 425 | /// \c writeLabel() member will be called with a node it will write it's |
---|
| 426 | /// label. Otherwise if the \c _forceLabelMap constructor parameter is true |
---|
[2101] | 427 | /// then the label map will be the id in the graph. In addition if the |
---|
| 428 | /// the \c _sortByLabel is true then the writer will write the edges |
---|
| 429 | /// sorted by the labels. |
---|
[1409] | 430 | /// |
---|
| 431 | /// \relates LemonWriter |
---|
| 432 | template <typename _Graph, typename _Traits = DefaultWriterTraits> |
---|
[1845] | 433 | class NodeSetWriter : public LemonWriter::SectionWriter { |
---|
| 434 | typedef LemonWriter::SectionWriter Parent; |
---|
[1409] | 435 | public: |
---|
| 436 | |
---|
| 437 | typedef _Graph Graph; |
---|
| 438 | typedef _Traits Traits; |
---|
[1429] | 439 | typedef typename Graph::Node Node; |
---|
[1409] | 440 | |
---|
| 441 | /// \brief Constructor. |
---|
| 442 | /// |
---|
| 443 | /// Constructor for NodeSetWriter. It creates the NodeSetWriter and |
---|
[1901] | 444 | /// attach it into the given LemonWriter. If the \c _forceLabelMap |
---|
| 445 | /// parameter is true then the writer will write own label map when |
---|
[2101] | 446 | /// the user does not give "label" named map. In addition if the |
---|
| 447 | /// the \c _sortByLabel is true then the writer will write the edges |
---|
| 448 | /// sorted by the labels. |
---|
[1409] | 449 | NodeSetWriter(LemonWriter& _writer, const Graph& _graph, |
---|
[1901] | 450 | const std::string& _name = std::string(), |
---|
[2101] | 451 | bool _forceLabelMap = true, bool _sortByLabel = true) |
---|
[1901] | 452 | : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), |
---|
[2101] | 453 | sortByLabel(_sortByLabel), graph(_graph), name(_name) {} |
---|
[1409] | 454 | |
---|
| 455 | /// \brief Destructor. |
---|
| 456 | /// |
---|
| 457 | /// Destructor for NodeSetWriter. |
---|
| 458 | virtual ~NodeSetWriter() { |
---|
| 459 | typename MapWriters::iterator it; |
---|
| 460 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
| 461 | delete it->second; |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | private: |
---|
| 466 | NodeSetWriter(const NodeSetWriter&); |
---|
| 467 | void operator=(const NodeSetWriter&); |
---|
| 468 | |
---|
| 469 | public: |
---|
| 470 | |
---|
| 471 | /// \brief Add a new node map writer command for the writer. |
---|
| 472 | /// |
---|
| 473 | /// Add a new node map writer command for the writer. |
---|
| 474 | template <typename Map> |
---|
[1421] | 475 | NodeSetWriter& writeNodeMap(std::string name, const Map& map) { |
---|
| 476 | return writeNodeMap<typename Traits:: |
---|
[1409] | 477 | template Writer<typename Map::Value>, Map>(name, map); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | /// \brief Add a new node map writer command for the writer. |
---|
| 481 | /// |
---|
| 482 | /// Add a new node map writer command for the writer. |
---|
| 483 | template <typename Writer, typename Map> |
---|
[1421] | 484 | NodeSetWriter& writeNodeMap(std::string name, const Map& map, |
---|
| 485 | const Writer& writer = Writer()) { |
---|
[1492] | 486 | checkConcept<concept::ReadMap<Node, typename Map::Value>, Map>(); |
---|
| 487 | checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); |
---|
[1409] | 488 | writers.push_back( |
---|
[1845] | 489 | make_pair(name, new _writer_bits:: |
---|
| 490 | MapWriter<Node, Map, Writer>(map, writer))); |
---|
[1409] | 491 | return *this; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | protected: |
---|
| 495 | |
---|
| 496 | /// \brief The header of the section. |
---|
| 497 | /// |
---|
| 498 | /// It gives back the header of the section. |
---|
| 499 | virtual std::string header() { |
---|
[1901] | 500 | return "@nodeset " + name; |
---|
[1409] | 501 | } |
---|
| 502 | |
---|
| 503 | /// \brief Writer function of the section. |
---|
| 504 | /// |
---|
| 505 | /// Write the content of the section. |
---|
| 506 | virtual void write(std::ostream& os) { |
---|
| 507 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[2084] | 508 | if (writers[i].first == "label") { |
---|
[1901] | 509 | labelMap = writers[i].second; |
---|
| 510 | forceLabelMap = false; |
---|
[1409] | 511 | break; |
---|
| 512 | } |
---|
| 513 | } |
---|
[2101] | 514 | std::vector<Node> items; |
---|
| 515 | for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 516 | items.push_back(it); |
---|
| 517 | } |
---|
| 518 | if (sortByLabel) { |
---|
| 519 | if (labelMap) { |
---|
| 520 | labelMap->sortByMap(items); |
---|
| 521 | } else { |
---|
| 522 | typedef IdMap<Graph, Node> Map; |
---|
| 523 | Map map(graph); |
---|
| 524 | _writer_bits::ComposeLess<Map> less(map); |
---|
| 525 | std::sort(items.begin(), items.end(), less); |
---|
| 526 | } |
---|
| 527 | } |
---|
[1901] | 528 | if (forceLabelMap) { |
---|
| 529 | os << "label\t"; |
---|
[1409] | 530 | } |
---|
| 531 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
| 532 | os << writers[i].first << '\t'; |
---|
| 533 | } |
---|
| 534 | os << std::endl; |
---|
[2101] | 535 | for (typename std::vector<Node>::iterator it = items.begin(); |
---|
| 536 | it != items.end(); ++it) { |
---|
[1901] | 537 | if (forceLabelMap) { |
---|
[2101] | 538 | os << graph.id(*it) << '\t'; |
---|
[1409] | 539 | } |
---|
| 540 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[2101] | 541 | writers[i].second->write(os, *it); |
---|
[1409] | 542 | os << '\t'; |
---|
| 543 | } |
---|
| 544 | os << std::endl; |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | public: |
---|
| 549 | |
---|
[1901] | 550 | /// \brief Returns true if the nodeset can write the labels of the nodes. |
---|
[1409] | 551 | /// |
---|
[1901] | 552 | /// Returns true if the nodeset can write the labels of the nodes. |
---|
| 553 | /// It is possible only if an "label" named map was written or the |
---|
| 554 | /// \c _forceLabelMap constructor parameter was true. |
---|
| 555 | bool isLabelWriter() const { |
---|
| 556 | return labelMap != 0 || forceLabelMap; |
---|
[1409] | 557 | } |
---|
| 558 | |
---|
[1901] | 559 | /// \brief Write the label of the given node. |
---|
[1409] | 560 | /// |
---|
[1901] | 561 | /// It writes the label of the given node. If there was written an "label" |
---|
[1409] | 562 | /// named map then it will write the map value belongs to the node. |
---|
[1901] | 563 | /// Otherwise if the \c forceLabel parameter was true it will write |
---|
| 564 | /// its label in the graph. |
---|
| 565 | void writeLabel(std::ostream& os, const Node& item) const { |
---|
| 566 | if (forceLabelMap) { |
---|
[1409] | 567 | os << graph.id(item); |
---|
| 568 | } else { |
---|
[1901] | 569 | labelMap->write(os, item); |
---|
[1409] | 570 | } |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | private: |
---|
| 574 | |
---|
[1845] | 575 | typedef std::vector<std::pair<std::string, _writer_bits:: |
---|
| 576 | MapWriterBase<Node>*> > MapWriters; |
---|
[1409] | 577 | MapWriters writers; |
---|
| 578 | |
---|
[1901] | 579 | _writer_bits::MapWriterBase<Node>* labelMap; |
---|
| 580 | bool forceLabelMap; |
---|
[2101] | 581 | bool sortByLabel; |
---|
[1409] | 582 | |
---|
[1705] | 583 | const Graph& graph; |
---|
[1901] | 584 | std::string name; |
---|
[1409] | 585 | |
---|
| 586 | }; |
---|
| 587 | |
---|
[2016] | 588 | /// \ingroup section_io |
---|
[1421] | 589 | /// \brief SectionWriter for writing a graph's edgesets. |
---|
[1409] | 590 | /// |
---|
[1421] | 591 | /// The lemon format can store multiple graph edgesets with several maps. |
---|
[1901] | 592 | /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but |
---|
| 593 | /// the \c edgeset_name may be empty. |
---|
[1409] | 594 | /// |
---|
| 595 | /// The first line of the section contains the names of the maps separated |
---|
| 596 | /// with white spaces. Each next lines describes a edge in the edgeset. The |
---|
[1901] | 597 | /// line contains the source and the target nodes' label and the mapped |
---|
[1409] | 598 | /// values for each map. |
---|
| 599 | /// |
---|
[1901] | 600 | /// If the edgeset contains an \c "label" named map then it will be regarded |
---|
| 601 | /// as label map. This map should contain only unique values and when the |
---|
| 602 | /// \c writeLabel() member will be called with an edge it will write it's |
---|
| 603 | /// label. Otherwise if the \c _forceLabelMap constructor parameter is true |
---|
[2101] | 604 | /// then the label map will be the id in the graph. In addition if the |
---|
| 605 | /// the \c _sortByLabel is true then the writer will write the edges |
---|
| 606 | /// sorted by the labels. |
---|
[1409] | 607 | /// |
---|
[1901] | 608 | /// The edgeset writer needs a node label writer to identify which nodes |
---|
| 609 | /// have to be connected. If a NodeSetWriter can write the nodes' label, |
---|
[1409] | 610 | /// it will be able to use with this class. |
---|
| 611 | /// |
---|
| 612 | /// \relates LemonWriter |
---|
| 613 | template <typename _Graph, typename _Traits = DefaultWriterTraits> |
---|
[1845] | 614 | class EdgeSetWriter : public LemonWriter::SectionWriter { |
---|
| 615 | typedef LemonWriter::SectionWriter Parent; |
---|
[1409] | 616 | public: |
---|
| 617 | |
---|
| 618 | typedef _Graph Graph; |
---|
| 619 | typedef _Traits Traits; |
---|
[1429] | 620 | typedef typename Graph::Node Node; |
---|
| 621 | typedef typename Graph::Edge Edge; |
---|
[1409] | 622 | |
---|
| 623 | /// \brief Constructor. |
---|
| 624 | /// |
---|
[2101] | 625 | /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter |
---|
| 626 | /// and attach it into the given LemonWriter. It will write node |
---|
| 627 | /// labels by the \c _nodeLabelWriter. If the \c _forceLabelMap |
---|
| 628 | /// parameter is true then the writer will write own label map if |
---|
| 629 | /// the user does not give "label" named map. In addition if the |
---|
| 630 | /// the \c _sortByLabel is true then the writer will write the |
---|
| 631 | /// edges sorted by the labels. |
---|
[1901] | 632 | template <typename NodeLabelWriter> |
---|
[1409] | 633 | EdgeSetWriter(LemonWriter& _writer, const Graph& _graph, |
---|
[1901] | 634 | const NodeLabelWriter& _nodeLabelWriter, |
---|
| 635 | const std::string& _name = std::string(), |
---|
[2101] | 636 | bool _forceLabelMap = true, bool _sortByLabel = true) |
---|
[1901] | 637 | : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), |
---|
[2101] | 638 | sortByLabel(_sortByLabel), graph(_graph), name(_name) { |
---|
[1901] | 639 | checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); |
---|
| 640 | nodeLabelWriter.reset(new _writer_bits:: |
---|
| 641 | LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter)); |
---|
[1476] | 642 | } |
---|
[1409] | 643 | |
---|
| 644 | /// \brief Destructor. |
---|
| 645 | /// |
---|
| 646 | /// Destructor for EdgeSetWriter. |
---|
| 647 | virtual ~EdgeSetWriter() { |
---|
| 648 | typename MapWriters::iterator it; |
---|
| 649 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
| 650 | delete it->second; |
---|
| 651 | } |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | private: |
---|
| 655 | EdgeSetWriter(const EdgeSetWriter&); |
---|
| 656 | void operator=(const EdgeSetWriter&); |
---|
| 657 | |
---|
| 658 | public: |
---|
| 659 | |
---|
[1421] | 660 | /// \brief Add a new edge map writer command for the writer. |
---|
[1409] | 661 | /// |
---|
[1421] | 662 | /// Add a new edge map writer command for the writer. |
---|
[1409] | 663 | template <typename Map> |
---|
[1421] | 664 | EdgeSetWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
| 665 | return writeEdgeMap<typename Traits:: |
---|
[1409] | 666 | template Writer<typename Map::Value>, Map>(name, map); |
---|
| 667 | } |
---|
| 668 | |
---|
[1421] | 669 | /// \brief Add a new edge map writer command for the writer. |
---|
[1409] | 670 | /// |
---|
[1421] | 671 | /// Add a new edge map writer command for the writer. |
---|
[1409] | 672 | template <typename Writer, typename Map> |
---|
[1421] | 673 | EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, |
---|
| 674 | const Writer& writer = Writer()) { |
---|
[1492] | 675 | checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 676 | checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); |
---|
[1409] | 677 | writers.push_back( |
---|
[1845] | 678 | make_pair(name, new _writer_bits:: |
---|
| 679 | MapWriter<Edge, Map, Writer>(map, writer))); |
---|
[1409] | 680 | return *this; |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | protected: |
---|
| 684 | |
---|
| 685 | /// \brief The header of the section. |
---|
| 686 | /// |
---|
| 687 | /// It gives back the header of the section. |
---|
| 688 | virtual std::string header() { |
---|
[1901] | 689 | return "@edgeset " + name; |
---|
[1409] | 690 | } |
---|
| 691 | |
---|
| 692 | /// \brief Writer function of the section. |
---|
| 693 | /// |
---|
| 694 | /// Write the content of the section. |
---|
| 695 | virtual void write(std::ostream& os) { |
---|
[1901] | 696 | if (!nodeLabelWriter->isLabelWriter()) { |
---|
| 697 | throw DataFormatError("Cannot find nodeset or label map"); |
---|
[1476] | 698 | } |
---|
[1409] | 699 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[2084] | 700 | if (writers[i].first == "label") { |
---|
[1901] | 701 | labelMap = writers[i].second; |
---|
| 702 | forceLabelMap = false; |
---|
[1409] | 703 | break; |
---|
| 704 | } |
---|
| 705 | } |
---|
[2101] | 706 | std::vector<Edge> items; |
---|
| 707 | for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) { |
---|
| 708 | items.push_back(it); |
---|
| 709 | } |
---|
| 710 | if (sortByLabel) { |
---|
| 711 | if (labelMap) { |
---|
| 712 | labelMap->sortByMap(items); |
---|
| 713 | } else { |
---|
| 714 | typedef IdMap<Graph, Edge> Map; |
---|
| 715 | Map map(graph); |
---|
| 716 | _writer_bits::ComposeLess<Map> less(map); |
---|
| 717 | std::sort(items.begin(), items.end(), less); |
---|
| 718 | } |
---|
| 719 | } |
---|
[1409] | 720 | os << "\t\t"; |
---|
[1901] | 721 | if (forceLabelMap) { |
---|
| 722 | os << "label\t"; |
---|
[1409] | 723 | } |
---|
| 724 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
| 725 | os << writers[i].first << '\t'; |
---|
| 726 | } |
---|
| 727 | os << std::endl; |
---|
[2101] | 728 | for (typename std::vector<Edge>::iterator it = items.begin(); |
---|
| 729 | it != items.end(); ++it) { |
---|
| 730 | nodeLabelWriter->write(os, graph.source(*it)); |
---|
[1409] | 731 | os << '\t'; |
---|
[2101] | 732 | nodeLabelWriter->write(os, graph.target(*it)); |
---|
[1409] | 733 | os << '\t'; |
---|
[1901] | 734 | if (forceLabelMap) { |
---|
[2101] | 735 | os << graph.id(*it) << '\t'; |
---|
[1409] | 736 | } |
---|
| 737 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[2101] | 738 | writers[i].second->write(os, *it); |
---|
[1409] | 739 | os << '\t'; |
---|
| 740 | } |
---|
| 741 | os << std::endl; |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | public: |
---|
| 746 | |
---|
[1901] | 747 | /// \brief Returns true if the edgeset can write the labels of the edges. |
---|
[1409] | 748 | /// |
---|
[1901] | 749 | /// Returns true if the edgeset can write the labels of the edges. |
---|
| 750 | /// It is possible only if an "label" named map was written or the |
---|
| 751 | /// \c _forceLabelMap constructor parameter was true. |
---|
| 752 | bool isLabelWriter() const { |
---|
| 753 | return forceLabelMap || labelMap != 0; |
---|
[1409] | 754 | } |
---|
| 755 | |
---|
[1901] | 756 | /// \brief Write the label of the given edge. |
---|
[1409] | 757 | /// |
---|
[1901] | 758 | /// It writes the label of the given edge. If there was written an "label" |
---|
[1409] | 759 | /// named map then it will write the map value belongs to the edge. |
---|
[1901] | 760 | /// Otherwise if the \c forceLabel parameter was true it will write |
---|
| 761 | /// its label in the graph. |
---|
| 762 | void writeLabel(std::ostream& os, const Edge& item) const { |
---|
| 763 | if (forceLabelMap) { |
---|
[1409] | 764 | os << graph.id(item); |
---|
| 765 | } else { |
---|
[1901] | 766 | labelMap->write(os, item); |
---|
[1409] | 767 | } |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | private: |
---|
| 771 | |
---|
[1845] | 772 | typedef std::vector<std::pair<std::string, _writer_bits:: |
---|
| 773 | MapWriterBase<Edge>*> > MapWriters; |
---|
[1409] | 774 | MapWriters writers; |
---|
| 775 | |
---|
[1901] | 776 | _writer_bits::MapWriterBase<Edge>* labelMap; |
---|
| 777 | bool forceLabelMap; |
---|
[2101] | 778 | bool sortByLabel; |
---|
[1409] | 779 | |
---|
[1705] | 780 | const Graph& graph; |
---|
[1901] | 781 | std::string name; |
---|
[1421] | 782 | |
---|
[1901] | 783 | std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter; |
---|
[1421] | 784 | }; |
---|
| 785 | |
---|
[2016] | 786 | /// \ingroup section_io |
---|
[1421] | 787 | /// \brief SectionWriter for writing a undirected edgeset. |
---|
| 788 | /// |
---|
| 789 | /// The lemon format can store multiple undirected edgesets with several |
---|
[1909] | 790 | /// maps. The undirected edgeset section's header line is \c \@uedgeset |
---|
| 791 | /// \c uedgeset_name, but the \c uedgeset_name may be empty. |
---|
[1421] | 792 | /// |
---|
| 793 | /// The first line of the section contains the names of the maps separated |
---|
| 794 | /// with white spaces. Each next lines describes an undirected edge in the |
---|
[1901] | 795 | /// edgeset. The line contains the two connected nodes' label and the mapped |
---|
[1421] | 796 | /// values for each undirected map. |
---|
| 797 | /// |
---|
| 798 | /// The section can handle the directed as a syntactical sugar. Two |
---|
| 799 | /// undirected edge map describes one directed edge map. This two maps |
---|
| 800 | /// are the forward map and the backward map and the names of this map |
---|
| 801 | /// is near the same just with a prefix \c '+' or \c '-' character |
---|
| 802 | /// difference. |
---|
| 803 | /// |
---|
[2101] | 804 | /// If the edgeset contains an \c "label" named map then it will be |
---|
| 805 | /// regarded as label map. This map should contain only unique |
---|
| 806 | /// values and when the \c writeLabel() member will be called with |
---|
| 807 | /// an undirected edge it will write it's label. Otherwise if the \c |
---|
| 808 | /// _forceLabelMap constructor parameter is true then the label map |
---|
| 809 | /// will be the id in the graph. In addition if the the \c |
---|
| 810 | /// _sortByLabel is true then the writer will write the edges sorted |
---|
| 811 | /// by the labels. |
---|
[1421] | 812 | /// |
---|
[1901] | 813 | /// The undirected edgeset writer needs a node label writer to identify |
---|
[1421] | 814 | /// which nodes have to be connected. If a NodeSetWriter can write the |
---|
[1901] | 815 | /// nodes' label, it will be able to use with this class. |
---|
[1421] | 816 | /// |
---|
| 817 | /// \relates LemonWriter |
---|
| 818 | template <typename _Graph, typename _Traits = DefaultWriterTraits> |
---|
[1909] | 819 | class UEdgeSetWriter : public LemonWriter::SectionWriter { |
---|
[1845] | 820 | typedef LemonWriter::SectionWriter Parent; |
---|
[1421] | 821 | public: |
---|
| 822 | |
---|
| 823 | typedef _Graph Graph; |
---|
| 824 | typedef _Traits Traits; |
---|
[1429] | 825 | typedef typename Graph::Node Node; |
---|
| 826 | typedef typename Graph::Edge Edge; |
---|
[1909] | 827 | typedef typename Graph::UEdge UEdge; |
---|
[1421] | 828 | |
---|
| 829 | /// \brief Constructor. |
---|
| 830 | /// |
---|
[1909] | 831 | /// Constructor for UEdgeSetWriter. It creates the UEdgeSetWriter |
---|
[2101] | 832 | /// and attach it into the given LemonWriter. It will write node |
---|
| 833 | /// labels by the \c _nodeLabelWriter. If the \c _forceLabelMap |
---|
| 834 | /// parameter is true then the writer will write own label map if |
---|
| 835 | /// the user does not give "label" named map. In addition if the |
---|
| 836 | /// the \c _sortByLabel is true then the writer will write the |
---|
| 837 | /// edges sorted by the labels. |
---|
[1901] | 838 | template <typename NodeLabelWriter> |
---|
[1909] | 839 | UEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, |
---|
[1901] | 840 | const NodeLabelWriter& _nodeLabelWriter, |
---|
| 841 | const std::string& _name = std::string(), |
---|
[2101] | 842 | bool _forceLabelMap = true, bool _sortByLabel = true) |
---|
[1901] | 843 | : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), |
---|
[2101] | 844 | sortByLabel(_sortByLabel), graph(_graph), name(_name) { |
---|
[1901] | 845 | checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); |
---|
| 846 | nodeLabelWriter.reset(new _writer_bits:: |
---|
| 847 | LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter)); |
---|
[1476] | 848 | } |
---|
[1421] | 849 | |
---|
| 850 | /// \brief Destructor. |
---|
| 851 | /// |
---|
[1909] | 852 | /// Destructor for UEdgeSetWriter. |
---|
| 853 | virtual ~UEdgeSetWriter() { |
---|
[1421] | 854 | typename MapWriters::iterator it; |
---|
| 855 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
| 856 | delete it->second; |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | |
---|
| 860 | private: |
---|
[1909] | 861 | UEdgeSetWriter(const UEdgeSetWriter&); |
---|
| 862 | void operator=(const UEdgeSetWriter&); |
---|
[1421] | 863 | |
---|
| 864 | public: |
---|
| 865 | |
---|
| 866 | /// \brief Add a new undirected edge map writer command for the writer. |
---|
| 867 | /// |
---|
| 868 | /// Add a new undirected map writer command for the writer. |
---|
| 869 | template <typename Map> |
---|
[1909] | 870 | UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map) { |
---|
| 871 | return writeUEdgeMap<typename Traits:: |
---|
[1421] | 872 | template Writer<typename Map::Value>, Map>(name, map); |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | /// \brief Add a new undirected map writer command for the writer. |
---|
| 876 | /// |
---|
| 877 | /// Add a new undirected map writer command for the writer. |
---|
| 878 | template <typename Writer, typename Map> |
---|
[1909] | 879 | UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map, |
---|
[1421] | 880 | const Writer& writer = Writer()) { |
---|
[1909] | 881 | checkConcept<concept::ReadMap<UEdge, typename Map::Value>, Map>(); |
---|
[1492] | 882 | checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); |
---|
[1421] | 883 | writers.push_back( |
---|
[1845] | 884 | make_pair(name, new _writer_bits:: |
---|
[1909] | 885 | MapWriter<UEdge, Map, Writer>(map, writer))); |
---|
[1421] | 886 | return *this; |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | /// \brief Add a new directed edge map writer command for the writer. |
---|
| 890 | /// |
---|
| 891 | /// Add a new directed map writer command for the writer. |
---|
| 892 | template <typename Map> |
---|
[1909] | 893 | UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
[1492] | 894 | return writeEdgeMap<typename Traits:: |
---|
| 895 | template Writer<typename Map::Value>, Map>(name, map); |
---|
[1421] | 896 | } |
---|
| 897 | |
---|
| 898 | /// \brief Add a new directed map writer command for the writer. |
---|
| 899 | /// |
---|
| 900 | /// Add a new directed map writer command for the writer. |
---|
| 901 | template <typename Writer, typename Map> |
---|
[1909] | 902 | UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, |
---|
[1421] | 903 | const Writer& writer = Writer()) { |
---|
[1492] | 904 | checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 905 | checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); |
---|
[1909] | 906 | writeUEdge("+" + name, |
---|
[1705] | 907 | _writer_bits::forwardComposeMap(graph, map), writer); |
---|
[1909] | 908 | writeUEdge("-" + name, |
---|
[1705] | 909 | _writer_bits::backwardComposeMap(graph, map), writer); |
---|
[1421] | 910 | return *this; |
---|
| 911 | } |
---|
| 912 | |
---|
| 913 | protected: |
---|
| 914 | |
---|
| 915 | /// \brief The header of the section. |
---|
| 916 | /// |
---|
| 917 | /// It gives back the header of the section. |
---|
| 918 | virtual std::string header() { |
---|
[1909] | 919 | return "@uedgeset " + name; |
---|
[1421] | 920 | } |
---|
| 921 | |
---|
| 922 | /// \brief Writer function of the section. |
---|
| 923 | /// |
---|
| 924 | /// Write the content of the section. |
---|
| 925 | virtual void write(std::ostream& os) { |
---|
[1901] | 926 | if (!nodeLabelWriter->isLabelWriter()) { |
---|
| 927 | throw DataFormatError("Cannot find nodeset or label map"); |
---|
[1476] | 928 | } |
---|
[1421] | 929 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[1901] | 930 | if (writers[i].first == "label") { |
---|
| 931 | labelMap = writers[i].second; |
---|
| 932 | forceLabelMap = false; |
---|
[1421] | 933 | break; |
---|
| 934 | } |
---|
| 935 | } |
---|
[2101] | 936 | std::vector<UEdge> items; |
---|
| 937 | for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) { |
---|
| 938 | items.push_back(it); |
---|
| 939 | } |
---|
| 940 | if (sortByLabel) { |
---|
| 941 | if (labelMap) { |
---|
| 942 | labelMap->sortByMap(items); |
---|
| 943 | } else { |
---|
| 944 | typedef IdMap<Graph, UEdge> Map; |
---|
| 945 | Map map(graph); |
---|
| 946 | _writer_bits::ComposeLess<Map> less(map); |
---|
| 947 | std::sort(items.begin(), items.end(), less); |
---|
| 948 | } |
---|
| 949 | } |
---|
[1421] | 950 | os << "\t\t"; |
---|
[1901] | 951 | if (forceLabelMap) { |
---|
| 952 | os << "label\t"; |
---|
[1421] | 953 | } |
---|
| 954 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
| 955 | os << writers[i].first << '\t'; |
---|
| 956 | } |
---|
| 957 | os << std::endl; |
---|
[2101] | 958 | for (typename std::vector<Edge>::iterator it = items.begin(); |
---|
| 959 | it != items.end(); ++it) { |
---|
| 960 | nodeLabelWriter->write(os, graph.source(*it)); |
---|
[1421] | 961 | os << '\t'; |
---|
[2101] | 962 | nodeLabelWriter->write(os, graph.target(*it)); |
---|
[1421] | 963 | os << '\t'; |
---|
[1901] | 964 | if (forceLabelMap) { |
---|
[2101] | 965 | os << graph.id(*it) << '\t'; |
---|
[1421] | 966 | } |
---|
| 967 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
[2101] | 968 | writers[i].second->write(os, *it); |
---|
[1421] | 969 | os << '\t'; |
---|
| 970 | } |
---|
| 971 | os << std::endl; |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | public: |
---|
| 976 | |
---|
[1901] | 977 | /// \brief Returns true if the undirected edgeset can write the labels of |
---|
[1421] | 978 | /// the edges. |
---|
| 979 | /// |
---|
[1901] | 980 | /// Returns true if the undirected edgeset can write the labels of the |
---|
| 981 | /// undirected edges. It is possible only if an "label" named map was |
---|
| 982 | /// written or the \c _forceLabelMap constructor parameter was true. |
---|
| 983 | bool isLabelWriter() const { |
---|
| 984 | return forceLabelMap || labelMap != 0; |
---|
[1421] | 985 | } |
---|
| 986 | |
---|
[1901] | 987 | /// \brief Write the label of the given undirected edge. |
---|
[1421] | 988 | /// |
---|
[1901] | 989 | /// It writes the label of the given undirected edge. If there was written |
---|
| 990 | /// an "label" named map then it will write the map value belongs to the |
---|
| 991 | /// undirected edge. Otherwise if the \c forceLabel parameter was true it |
---|
[1421] | 992 | /// will write its id in the graph. |
---|
[1909] | 993 | void writeLabel(std::ostream& os, const UEdge& item) const { |
---|
[1901] | 994 | if (forceLabelMap) { |
---|
[1429] | 995 | os << graph.id(item); |
---|
| 996 | } else { |
---|
[1901] | 997 | labelMap->write(os, item); |
---|
[1429] | 998 | } |
---|
| 999 | } |
---|
| 1000 | |
---|
[1901] | 1001 | /// \brief Write the label of the given edge. |
---|
[1429] | 1002 | /// |
---|
[1901] | 1003 | /// It writes the label of the given edge. If there was written |
---|
| 1004 | /// an "label" named map then it will write the map value belongs to the |
---|
| 1005 | /// edge. Otherwise if the \c forceLabel parameter was true it |
---|
[1429] | 1006 | /// will write its id in the graph. If the edge is forward map |
---|
| 1007 | /// then its prefix character is \c '+' elsewhere \c '-'. |
---|
[1901] | 1008 | void writeLabel(std::ostream& os, const Edge& item) const { |
---|
[1690] | 1009 | if (graph.direction(item)) { |
---|
[1429] | 1010 | os << "+ "; |
---|
| 1011 | } else { |
---|
| 1012 | os << "- "; |
---|
| 1013 | } |
---|
[1901] | 1014 | if (forceLabelMap) { |
---|
[1421] | 1015 | os << graph.id(item); |
---|
| 1016 | } else { |
---|
[1901] | 1017 | labelMap->write(os, item); |
---|
[1421] | 1018 | } |
---|
| 1019 | } |
---|
| 1020 | |
---|
| 1021 | private: |
---|
| 1022 | |
---|
[1845] | 1023 | typedef std::vector<std::pair<std::string, _writer_bits:: |
---|
[1909] | 1024 | MapWriterBase<UEdge>*> > MapWriters; |
---|
[1421] | 1025 | MapWriters writers; |
---|
| 1026 | |
---|
[1909] | 1027 | _writer_bits::MapWriterBase<UEdge>* labelMap; |
---|
[1901] | 1028 | bool forceLabelMap; |
---|
[2101] | 1029 | bool sortByLabel; |
---|
[1421] | 1030 | |
---|
[1705] | 1031 | const Graph& graph; |
---|
[1901] | 1032 | std::string name; |
---|
[1409] | 1033 | |
---|
[1901] | 1034 | std::auto_ptr<_writer_bits::LabelWriterBase<Node> > nodeLabelWriter; |
---|
[1409] | 1035 | }; |
---|
| 1036 | |
---|
[2016] | 1037 | /// \ingroup section_io |
---|
[1901] | 1038 | /// \brief SectionWriter for writing named nodes. |
---|
[1409] | 1039 | /// |
---|
[1901] | 1040 | /// The nodes section's header line is \c \@nodes \c nodes_name, but the |
---|
| 1041 | /// \c nodes_name may be empty. |
---|
[1409] | 1042 | /// |
---|
[1901] | 1043 | /// Each line in the section contains the name of the node and |
---|
| 1044 | /// then the node label. |
---|
[1409] | 1045 | /// |
---|
| 1046 | /// \relates LemonWriter |
---|
| 1047 | template <typename _Graph> |
---|
[1845] | 1048 | class NodeWriter : public LemonWriter::SectionWriter { |
---|
| 1049 | typedef LemonWriter::SectionWriter Parent; |
---|
[1409] | 1050 | typedef _Graph Graph; |
---|
[1429] | 1051 | typedef typename Graph::Node Node; |
---|
[1409] | 1052 | public: |
---|
| 1053 | |
---|
| 1054 | /// \brief Constructor. |
---|
| 1055 | /// |
---|
| 1056 | /// Constructor for NodeWriter. It creates the NodeWriter and |
---|
[1901] | 1057 | /// attach it into the given LemonWriter. The given \c _LabelWriter |
---|
| 1058 | /// will write the nodes' label what can be a nodeset writer. |
---|
| 1059 | template <typename _LabelWriter> |
---|
| 1060 | NodeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, |
---|
| 1061 | const std::string& _name = std::string()) |
---|
| 1062 | : Parent(_writer), name(_name) { |
---|
| 1063 | checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>(); |
---|
| 1064 | labelWriter.reset(new _writer_bits::LabelWriter<Node, _LabelWriter> |
---|
| 1065 | (_labelWriter)); |
---|
[1476] | 1066 | } |
---|
| 1067 | |
---|
[1409] | 1068 | |
---|
| 1069 | /// \brief Destructor. |
---|
| 1070 | /// |
---|
| 1071 | /// Destructor for NodeWriter. |
---|
| 1072 | virtual ~NodeWriter() {} |
---|
| 1073 | |
---|
| 1074 | private: |
---|
| 1075 | NodeWriter(const NodeWriter&); |
---|
| 1076 | void operator=(const NodeWriter&); |
---|
| 1077 | |
---|
| 1078 | public: |
---|
| 1079 | |
---|
| 1080 | /// \brief Add a node writer command for the NodeWriter. |
---|
| 1081 | /// |
---|
| 1082 | /// Add a node writer command for the NodeWriter. |
---|
[1429] | 1083 | void writeNode(const std::string& name, const Node& item) { |
---|
[1409] | 1084 | writers.push_back(make_pair(name, &item)); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | protected: |
---|
| 1088 | |
---|
[1901] | 1089 | /// \brief The header of the section. |
---|
[1409] | 1090 | /// |
---|
[1901] | 1091 | /// It gives back the header of the section. |
---|
[1409] | 1092 | virtual std::string header() { |
---|
[1901] | 1093 | return "@nodes " + name; |
---|
[1409] | 1094 | } |
---|
| 1095 | |
---|
| 1096 | /// \brief Writer function of the section. |
---|
| 1097 | /// |
---|
| 1098 | /// Write the content of the section. |
---|
| 1099 | virtual void write(std::ostream& os) { |
---|
[1901] | 1100 | if (!labelWriter->isLabelWriter()) { |
---|
| 1101 | throw DataFormatError("Cannot find nodeset or label map"); |
---|
[1476] | 1102 | } |
---|
[1409] | 1103 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
| 1104 | os << writers[i].first << ' '; |
---|
[1901] | 1105 | labelWriter->write(os, *(writers[i].second)); |
---|
[1409] | 1106 | os << std::endl; |
---|
| 1107 | } |
---|
| 1108 | } |
---|
[2084] | 1109 | |
---|
| 1110 | /// \brief Gives back true when the section should be written. |
---|
| 1111 | /// |
---|
| 1112 | /// Gives back true when the section should be written. |
---|
| 1113 | virtual bool valid() { return !writers.empty(); } |
---|
[1409] | 1114 | |
---|
| 1115 | private: |
---|
| 1116 | |
---|
[1901] | 1117 | std::string name; |
---|
[1409] | 1118 | |
---|
[1429] | 1119 | typedef std::vector<std::pair<std::string, const Node*> > NodeWriters; |
---|
| 1120 | NodeWriters writers; |
---|
[1901] | 1121 | std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter; |
---|
[1409] | 1122 | }; |
---|
| 1123 | |
---|
[2016] | 1124 | /// \ingroup section_io |
---|
[1901] | 1125 | /// \brief SectionWriter for writing named edges. |
---|
[1409] | 1126 | /// |
---|
[1901] | 1127 | /// The edges section's header line is \c \@edges \c edges_name, but the |
---|
| 1128 | /// \c edges_name may be empty. |
---|
[1409] | 1129 | /// |
---|
[1901] | 1130 | /// Each line in the section contains the name of the edge and |
---|
| 1131 | /// then the edge label. |
---|
[1409] | 1132 | /// |
---|
| 1133 | /// \relates LemonWriter |
---|
| 1134 | template <typename _Graph> |
---|
[1845] | 1135 | class EdgeWriter : public LemonWriter::SectionWriter { |
---|
| 1136 | typedef LemonWriter::SectionWriter Parent; |
---|
[1409] | 1137 | typedef _Graph Graph; |
---|
[1429] | 1138 | typedef typename Graph::Edge Edge; |
---|
[1409] | 1139 | public: |
---|
| 1140 | |
---|
| 1141 | /// \brief Constructor. |
---|
| 1142 | /// |
---|
| 1143 | /// Constructor for EdgeWriter. It creates the EdgeWriter and |
---|
[1901] | 1144 | /// attach it into the given LemonWriter. The given \c _LabelWriter |
---|
| 1145 | /// will write the edges' label what can be a edgeset writer. |
---|
| 1146 | template <typename _LabelWriter> |
---|
| 1147 | EdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, |
---|
| 1148 | const std::string& _name = std::string()) |
---|
| 1149 | : Parent(_writer), name(_name) { |
---|
| 1150 | checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>(); |
---|
| 1151 | labelWriter.reset(new _writer_bits::LabelWriter<Edge, _LabelWriter>(_labelWriter)); |
---|
[1476] | 1152 | } |
---|
[1409] | 1153 | |
---|
| 1154 | /// \brief Destructor. |
---|
| 1155 | /// |
---|
| 1156 | /// Destructor for EdgeWriter. |
---|
| 1157 | virtual ~EdgeWriter() {} |
---|
| 1158 | private: |
---|
| 1159 | EdgeWriter(const EdgeWriter&); |
---|
| 1160 | void operator=(const EdgeWriter&); |
---|
| 1161 | |
---|
| 1162 | public: |
---|
| 1163 | |
---|
[1421] | 1164 | /// \brief Add an edge writer command for the EdgeWriter. |
---|
[1409] | 1165 | /// |
---|
[1421] | 1166 | /// Add an edge writer command for the EdgeWriter. |
---|
[1429] | 1167 | void writeEdge(const std::string& name, const Edge& item) { |
---|
[1409] | 1168 | writers.push_back(make_pair(name, &item)); |
---|
| 1169 | } |
---|
| 1170 | |
---|
| 1171 | protected: |
---|
| 1172 | |
---|
[1901] | 1173 | /// \brief The header of the section. |
---|
[1409] | 1174 | /// |
---|
[1901] | 1175 | /// It gives back the header of the section. |
---|
[1421] | 1176 | virtual std::string header() { |
---|
[1901] | 1177 | return "@edges " + name; |
---|
[1421] | 1178 | } |
---|
| 1179 | |
---|
| 1180 | /// \brief Writer function of the section. |
---|
| 1181 | /// |
---|
| 1182 | /// Write the content of the section. |
---|
| 1183 | virtual void write(std::ostream& os) { |
---|
[1901] | 1184 | if (!labelWriter->isLabelWriter()) { |
---|
| 1185 | throw DataFormatError("Cannot find edgeset or label map"); |
---|
[1476] | 1186 | } |
---|
[1421] | 1187 | for (int i = 0; i < (int)writers.size(); ++i) { |
---|
| 1188 | os << writers[i].first << ' '; |
---|
[1901] | 1189 | labelWriter->write(os, *(writers[i].second)); |
---|
[1421] | 1190 | os << std::endl; |
---|
| 1191 | } |
---|
| 1192 | } |
---|
[2084] | 1193 | |
---|
| 1194 | /// \brief Gives back true when the section should be written. |
---|
| 1195 | /// |
---|
| 1196 | /// Gives back true when the section should be written. |
---|
| 1197 | virtual bool valid() { return !writers.empty(); } |
---|
[1421] | 1198 | |
---|
| 1199 | private: |
---|
| 1200 | |
---|
[1901] | 1201 | std::string name; |
---|
[1421] | 1202 | |
---|
[1429] | 1203 | typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters; |
---|
| 1204 | EdgeWriters writers; |
---|
[1421] | 1205 | |
---|
[1901] | 1206 | std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter; |
---|
[1421] | 1207 | }; |
---|
| 1208 | |
---|
[2016] | 1209 | /// \ingroup section_io |
---|
[1901] | 1210 | /// \brief SectionWriter for writing named undirected edges. |
---|
[1421] | 1211 | /// |
---|
[1909] | 1212 | /// The undirected edges section's header line is \c \@uedges |
---|
| 1213 | /// \c uedges_name, but the \c uedges_name may be empty. |
---|
[1421] | 1214 | /// |
---|
[1901] | 1215 | /// Each line in the section contains the name of the undirected edge and |
---|
| 1216 | /// then the undirected edge label. |
---|
[1421] | 1217 | /// |
---|
| 1218 | /// \relates LemonWriter |
---|
| 1219 | template <typename _Graph> |
---|
[1909] | 1220 | class UEdgeWriter : public LemonWriter::SectionWriter { |
---|
[1845] | 1221 | typedef LemonWriter::SectionWriter Parent; |
---|
[1421] | 1222 | typedef _Graph Graph; |
---|
[1429] | 1223 | typedef typename Graph::Node Node; |
---|
| 1224 | typedef typename Graph::Edge Edge; |
---|
[1909] | 1225 | typedef typename Graph::UEdge UEdge; |
---|
[1421] | 1226 | public: |
---|
| 1227 | |
---|
| 1228 | /// \brief Constructor. |
---|
| 1229 | /// |
---|
[1909] | 1230 | /// Constructor for UEdgeWriter. It creates the UEdgeWriter and |
---|
[1901] | 1231 | /// attach it into the given LemonWriter. The given \c _LabelWriter |
---|
| 1232 | /// will write the undirected edges' label what can be an undirected |
---|
[1421] | 1233 | /// edgeset writer. |
---|
[1901] | 1234 | template <typename _LabelWriter> |
---|
[1909] | 1235 | UEdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, |
---|
[1901] | 1236 | const std::string& _name = std::string()) |
---|
| 1237 | : Parent(_writer), name(_name) { |
---|
| 1238 | checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>(); |
---|
[1909] | 1239 | checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>(); |
---|
| 1240 | uEdgeLabelWriter.reset(new _writer_bits:: |
---|
| 1241 | LabelWriter<UEdge, _LabelWriter>(_labelWriter)); |
---|
[1901] | 1242 | edgeLabelWriter.reset(new _writer_bits:: |
---|
| 1243 | LabelWriter<Edge, _LabelWriter>(_labelWriter)); |
---|
[1476] | 1244 | } |
---|
[1421] | 1245 | |
---|
| 1246 | /// \brief Destructor. |
---|
| 1247 | /// |
---|
[1909] | 1248 | /// Destructor for UEdgeWriter. |
---|
| 1249 | virtual ~UEdgeWriter() {} |
---|
[1421] | 1250 | private: |
---|
[1909] | 1251 | UEdgeWriter(const UEdgeWriter&); |
---|
| 1252 | void operator=(const UEdgeWriter&); |
---|
[1421] | 1253 | |
---|
| 1254 | public: |
---|
| 1255 | |
---|
[1909] | 1256 | /// \brief Add an edge writer command for the UEdgeWriter. |
---|
[1429] | 1257 | /// |
---|
[1909] | 1258 | /// Add an edge writer command for the UEdgeWriter. |
---|
[1429] | 1259 | void writeEdge(const std::string& name, const Edge& item) { |
---|
| 1260 | edgeWriters.push_back(make_pair(name, &item)); |
---|
| 1261 | } |
---|
| 1262 | |
---|
[1909] | 1263 | /// \brief Add an undirected edge writer command for the UEdgeWriter. |
---|
[1421] | 1264 | /// |
---|
[1909] | 1265 | /// Add an undirected edge writer command for the UEdgeWriter. |
---|
| 1266 | void writeUEdge(const std::string& name, const UEdge& item) { |
---|
| 1267 | uEdgeWriters.push_back(make_pair(name, &item)); |
---|
[1421] | 1268 | } |
---|
| 1269 | |
---|
| 1270 | protected: |
---|
| 1271 | |
---|
[1901] | 1272 | /// \brief The header of the section. |
---|
[1421] | 1273 | /// |
---|
[1901] | 1274 | /// It gives back the header of the section. |
---|
[1409] | 1275 | virtual std::string header() { |
---|
[1909] | 1276 | return "@uedges " + name; |
---|
[1409] | 1277 | } |
---|
| 1278 | |
---|
| 1279 | /// \brief Writer function of the section. |
---|
| 1280 | /// |
---|
| 1281 | /// Write the content of the section. |
---|
| 1282 | virtual void write(std::ostream& os) { |
---|
[1901] | 1283 | if (!edgeLabelWriter->isLabelWriter()) { |
---|
| 1284 | throw DataFormatError("Cannot find undirected edgeset or label map"); |
---|
[1476] | 1285 | } |
---|
[1909] | 1286 | if (!uEdgeLabelWriter->isLabelWriter()) { |
---|
[1901] | 1287 | throw DataFormatError("Cannot find undirected edgeset or label map"); |
---|
[1476] | 1288 | } |
---|
[1909] | 1289 | for (int i = 0; i < (int)uEdgeWriters.size(); ++i) { |
---|
| 1290 | os << uEdgeWriters[i].first << ' '; |
---|
| 1291 | uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second)); |
---|
[1429] | 1292 | os << std::endl; |
---|
| 1293 | } |
---|
| 1294 | for (int i = 0; i < (int)edgeWriters.size(); ++i) { |
---|
| 1295 | os << edgeWriters[i].first << ' '; |
---|
[1901] | 1296 | edgeLabelWriter->write(os, *(edgeWriters[i].second)); |
---|
[1409] | 1297 | os << std::endl; |
---|
| 1298 | } |
---|
| 1299 | } |
---|
[2084] | 1300 | |
---|
| 1301 | /// \brief Gives back true when the section should be written. |
---|
| 1302 | /// |
---|
| 1303 | /// Gives back true when the section should be written. |
---|
| 1304 | virtual bool valid() { |
---|
| 1305 | return !uEdgeWriters.empty() || !edgeWriters.empty(); |
---|
| 1306 | } |
---|
[1409] | 1307 | |
---|
| 1308 | private: |
---|
| 1309 | |
---|
[1901] | 1310 | std::string name; |
---|
[1409] | 1311 | |
---|
[1429] | 1312 | typedef std::vector<std::pair<std::string, |
---|
[1909] | 1313 | const UEdge*> > UEdgeWriters; |
---|
| 1314 | UEdgeWriters uEdgeWriters; |
---|
| 1315 | std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > uEdgeLabelWriter; |
---|
[1409] | 1316 | |
---|
[1429] | 1317 | typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters; |
---|
| 1318 | EdgeWriters edgeWriters; |
---|
[1901] | 1319 | std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter; |
---|
[1429] | 1320 | |
---|
[1409] | 1321 | }; |
---|
| 1322 | |
---|
[2016] | 1323 | /// \ingroup section_io |
---|
[1409] | 1324 | /// \brief SectionWriter for attributes. |
---|
| 1325 | /// |
---|
| 1326 | /// The lemon format can store multiple attribute set. Each set has |
---|
[1901] | 1327 | /// the header line \c \@attributes \c attributes_name, but the |
---|
| 1328 | /// attributeset_name may be empty. |
---|
[1409] | 1329 | /// |
---|
| 1330 | /// The attributeset section contains several lines. Each of them starts |
---|
| 1331 | /// with the name of attribute and then the value. |
---|
| 1332 | /// |
---|
| 1333 | /// \relates LemonWriter |
---|
| 1334 | template <typename _Traits = DefaultWriterTraits> |
---|
[1845] | 1335 | class AttributeWriter : public LemonWriter::SectionWriter { |
---|
| 1336 | typedef LemonWriter::SectionWriter Parent; |
---|
[1409] | 1337 | typedef _Traits Traits; |
---|
| 1338 | public: |
---|
| 1339 | /// \brief Constructor. |
---|
| 1340 | /// |
---|
| 1341 | /// Constructor for AttributeWriter. It creates the AttributeWriter and |
---|
| 1342 | /// attach it into the given LemonWriter. |
---|
| 1343 | AttributeWriter(LemonWriter& _writer, |
---|
[1901] | 1344 | const std::string& _name = std::string()) |
---|
| 1345 | : Parent(_writer), name(_name) {} |
---|
[1409] | 1346 | |
---|
| 1347 | /// \brief Destructor. |
---|
| 1348 | /// |
---|
| 1349 | /// Destructor for AttributeWriter. |
---|
| 1350 | virtual ~AttributeWriter() { |
---|
| 1351 | typename Writers::iterator it; |
---|
| 1352 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
| 1353 | delete it->second; |
---|
| 1354 | } |
---|
| 1355 | } |
---|
| 1356 | |
---|
| 1357 | private: |
---|
| 1358 | AttributeWriter(const AttributeWriter&); |
---|
| 1359 | void operator=(AttributeWriter&); |
---|
| 1360 | |
---|
| 1361 | public: |
---|
| 1362 | /// \brief Add an attribute writer command for the writer. |
---|
| 1363 | /// |
---|
| 1364 | /// Add an attribute writer command for the writer. |
---|
| 1365 | template <typename Value> |
---|
[1901] | 1366 | AttributeWriter& writeAttribute(const std::string& name, |
---|
[1409] | 1367 | const Value& value) { |
---|
| 1368 | return |
---|
[1901] | 1369 | writeAttribute<typename Traits::template Writer<Value> >(name, value); |
---|
[1409] | 1370 | } |
---|
| 1371 | |
---|
| 1372 | /// \brief Add an attribute writer command for the writer. |
---|
| 1373 | /// |
---|
| 1374 | /// Add an attribute writer command for the writer. |
---|
| 1375 | template <typename Writer, typename Value> |
---|
| 1376 | AttributeWriter& writeAttribute(const std::string& name, |
---|
| 1377 | const Value& value, |
---|
| 1378 | const Writer& writer = Writer()) { |
---|
[1492] | 1379 | checkConcept<_writer_bits::ItemWriter<Value>, Writer>(); |
---|
[1845] | 1380 | writers.push_back(make_pair(name, new _writer_bits:: |
---|
| 1381 | ValueWriter<Value, Writer>(value, writer))); |
---|
[1409] | 1382 | return *this; |
---|
| 1383 | } |
---|
| 1384 | |
---|
| 1385 | protected: |
---|
| 1386 | |
---|
| 1387 | /// \brief The header of section. |
---|
| 1388 | /// |
---|
| 1389 | /// It gives back the header of the section. |
---|
| 1390 | std::string header() { |
---|
[1901] | 1391 | return "@attributes " + name; |
---|
[1409] | 1392 | } |
---|
| 1393 | |
---|
| 1394 | /// \brief Writer function of the section. |
---|
| 1395 | /// |
---|
| 1396 | /// Write the content of the section. |
---|
| 1397 | void write(std::ostream& os) { |
---|
| 1398 | typename Writers::iterator it; |
---|
| 1399 | for (it = writers.begin(); it != writers.end(); ++it) { |
---|
| 1400 | os << it->first << ' '; |
---|
| 1401 | it->second->write(os); |
---|
| 1402 | os << std::endl; |
---|
| 1403 | } |
---|
| 1404 | } |
---|
| 1405 | |
---|
[2084] | 1406 | /// \brief Gives back true when the section should be written. |
---|
| 1407 | /// |
---|
| 1408 | /// Gives back true when the section should be written. |
---|
| 1409 | virtual bool valid() { return !writers.empty(); } |
---|
| 1410 | |
---|
[1409] | 1411 | private: |
---|
[1901] | 1412 | std::string name; |
---|
[1409] | 1413 | |
---|
[1845] | 1414 | typedef std::vector<std::pair<std::string, |
---|
| 1415 | _writer_bits::ValueWriterBase*> > Writers; |
---|
[1409] | 1416 | Writers writers; |
---|
| 1417 | }; |
---|
| 1418 | |
---|
| 1419 | |
---|
| 1420 | } |
---|
| 1421 | #endif |
---|