Changeset 2467:2025a571895e in lemon-0.x for lemon
- Timestamp:
- 08/28/07 16:00:42 (17 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3305
- Location:
- lemon
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bits/path_dump.h
r2391 r2467 55 55 RevEdgeIt(Invalid) : path(0), current(INVALID) {} 56 56 RevEdgeIt(const PredMapPath& _path) 57 : path(&_path), current(_path.target) {} 57 : path(&_path), current(_path.target) { 58 if (path->predMap[current] == INVALID) current = INVALID; 59 } 58 60 59 61 operator const typename Graph::Edge() const { … … 127 129 RevEdgeIt(Invalid) : path(0), current(INVALID) {} 128 130 RevEdgeIt(const PredMatrixMapPath& _path) 129 : path(&_path), current(_path.target) {} 131 : path(&_path), current(_path.target) { 132 if (path->predMatrixMap(path->source, current) == INVALID) 133 current = INVALID; 134 } 130 135 131 136 operator const typename Graph::Edge() const { -
lemon/graph_reader.h
r2391 r2467 325 325 /// it. It is possible only if there was read a "label" named edge map. 326 326 void readLabel(std::istream& is, Edge& edge) const { 327 returnedgeset_reader.readLabel(is, edge);327 edgeset_reader.readLabel(is, edge); 328 328 } 329 329 … … 444 444 : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper), 445 445 nodeset_reader(*reader, _graph, std::string(), skipper), 446 u _edgeset_reader(*reader, _graph, nodeset_reader,446 uedgeset_reader(*reader, _graph, nodeset_reader, 447 447 std::string(), skipper), 448 448 node_reader(*reader, nodeset_reader, std::string()), 449 u _edge_reader(*reader, u_edgeset_reader, std::string()),449 uedge_reader(*reader, uedgeset_reader, std::string()), 450 450 attribute_reader(*reader, std::string()) {} 451 451 … … 459 459 skipper(_skipper), 460 460 nodeset_reader(*reader, _graph, std::string(), skipper), 461 u _edgeset_reader(*reader, _graph, nodeset_reader,461 uedgeset_reader(*reader, _graph, nodeset_reader, 462 462 std::string(), skipper), 463 463 node_reader(*reader, nodeset_reader, std::string()), 464 u _edge_reader(*reader, u_edgeset_reader, std::string()),464 uedge_reader(*reader, uedgeset_reader, std::string()), 465 465 attribute_reader(*reader, std::string()) {} 466 466 … … 473 473 : reader(_reader), own_reader(false), skipper(_skipper), 474 474 nodeset_reader(*reader, _graph, std::string(), skipper), 475 u _edgeset_reader(*reader, _graph, nodeset_reader,475 uedgeset_reader(*reader, _graph, nodeset_reader, 476 476 std::string(), skipper), 477 477 node_reader(*reader, nodeset_reader, std::string()), 478 u _edge_reader(*reader, u_edgeset_reader, std::string()),478 uedge_reader(*reader, uedgeset_reader, std::string()), 479 479 attribute_reader(*reader, std::string()) {} 480 480 … … 534 534 template <typename Map> 535 535 UGraphReader& readUEdgeMap(std::string name, Map& map) { 536 u _edgeset_reader.readUEdgeMap(name, map);536 uedgeset_reader.readUEdgeMap(name, map); 537 537 return *this; 538 538 } … … 540 540 template <typename Map> 541 541 UGraphReader& readUEdgeMap(std::string name, const Map& map) { 542 u _edgeset_reader.readUEdgeMap(name, map);542 uedgeset_reader.readUEdgeMap(name, map); 543 543 return *this; 544 544 } … … 551 551 UGraphReader& readUEdgeMap(std::string name, Map& map, 552 552 const ItemReader& ir = ItemReader()) { 553 u _edgeset_reader.readUEdgeMap(name, map, ir);553 uedgeset_reader.readUEdgeMap(name, map, ir); 554 554 return *this; 555 555 } … … 558 558 UGraphReader& readUEdgeMap(std::string name, const Map& map, 559 559 const ItemReader& ir = ItemReader()) { 560 u _edgeset_reader.readUEdgeMap(name, map, ir);560 uedgeset_reader.readUEdgeMap(name, map, ir); 561 561 return *this; 562 562 } … … 568 568 UGraphReader& skipUEdgeMap(std::string name, 569 569 const ItemReader& ir = ItemReader()) { 570 u _edgeset_reader.skipUMap(name, ir);570 uedgeset_reader.skipUMap(name, ir); 571 571 return *this; 572 572 } … … 578 578 template <typename Map> 579 579 UGraphReader& readEdgeMap(std::string name, Map& map) { 580 u _edgeset_reader.readEdgeMap(name, map);580 uedgeset_reader.readEdgeMap(name, map); 581 581 return *this; 582 582 } … … 584 584 template <typename Map> 585 585 UGraphReader& readEdgeMap(std::string name, const Map& map) { 586 u _edgeset_reader.readEdgeMap(name, map);586 uedgeset_reader.readEdgeMap(name, map); 587 587 return *this; 588 588 } … … 595 595 UGraphReader& readEdgeMap(std::string name, Map& map, 596 596 const ItemReader& ir = ItemReader()) { 597 u _edgeset_reader.readEdgeMap(name, map, ir);597 uedgeset_reader.readEdgeMap(name, map, ir); 598 598 return *this; 599 599 } … … 602 602 UGraphReader& readEdgeMap(std::string name, const Map& map, 603 603 const ItemReader& ir = ItemReader()) { 604 u _edgeset_reader.readEdgeMap(name, map, ir);604 uedgeset_reader.readEdgeMap(name, map, ir); 605 605 return *this; 606 606 } … … 612 612 UGraphReader& skipEdgeMap(std::string name, 613 613 const ItemReader& ir = ItemReader()) { 614 u _edgeset_reader.skipEdgeMap(name, ir);614 uedgeset_reader.skipEdgeMap(name, ir); 615 615 return *this; 616 616 } … … 628 628 /// Give a new labeled edge reading command to the reader. 629 629 UGraphReader& readEdge(std::string name, Edge& edge) { 630 u _edge_reader.readEdge(name, edge);630 uedge_reader.readEdge(name, edge); 631 631 } 632 632 … … 636 636 /// Give a new labeled undirected edge reading command to the reader. 637 637 UGraphReader& readUEdge(std::string name, UEdge& edge) { 638 u _edge_reader.readUEdge(name, edge);638 uedge_reader.readUEdge(name, edge); 639 639 } 640 640 … … 678 678 /// \brief Returns true if the reader can give back the items by its label. 679 679 /// 680 /// \briefReturns true if the reader can give back the items by its label.680 /// Returns true if the reader can give back the items by its label. 681 681 bool isLabelReader() const { 682 682 return nodeset_reader.isLabelReader() && 683 u _edgeset_reader.isLabelReader();683 uedgeset_reader.isLabelReader(); 684 684 } 685 685 … … 697 697 /// it. It is possible only if there was read a "label" named edge map. 698 698 void readLabel(std::istream& is, Edge& edge) const { 699 return u _edgeset_reader.readLabel(is, edge);699 return uedgeset_reader.readLabel(is, edge); 700 700 } 701 701 … … 706 706 /// edge map. 707 707 void readLabel(std::istream& is, UEdge& uedge) const { 708 return u _edgeset_reader.readLabel(is, uedge);708 return uedgeset_reader.readLabel(is, uedge); 709 709 } 710 710 … … 718 718 719 719 NodeSetReader<Graph, ReaderTraits> nodeset_reader; 720 UEdgeSetReader<Graph, ReaderTraits> u _edgeset_reader;720 UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader; 721 721 722 722 NodeReader<Graph> node_reader; 723 UEdgeReader<Graph> u _edge_reader;723 UEdgeReader<Graph> uedge_reader; 724 724 725 725 AttributeReader<ReaderTraits> attribute_reader; -
lemon/graph_writer.h
r2391 r2467 252 252 } 253 253 254 /// \brief Returns true if the writer can give back the labels by the items. 255 /// 256 /// Returns true if the writer can give back the the labels by the items. 257 bool isLabelWriter() const { 258 return nodeset_writer.isLabelWriter() && 259 edgeset_writer.isLabelWriter(); 260 } 261 254 262 /// \brief Write the label of the given node. 255 263 /// 256 /// It writes the label of the given node. If there was written a n"label"264 /// It writes the label of the given node. If there was written a "label" 257 265 /// named node map then it will write the map value belonging to the node. 258 266 void writeLabel(std::ostream& os, const Node& item) const { … … 262 270 /// \brief Write the label of the given edge. 263 271 /// 264 /// It writes the label of the given edge. If there was written a n"label"272 /// It writes the label of the given edge. If there was written a "label" 265 273 /// named edge map then it will write the map value belonging to the edge. 266 274 void writeLabel(std::ostream& os, const Edge& item) const { 267 275 edgeset_writer.writeLabel(os, item); 268 276 } 277 278 /// \brief Sorts the given node vector by label. 279 /// 280 /// Sorts the given node vector by label. If there was written an 281 /// "label" named map then the vector will be sorted by the values 282 /// of this map. Otherwise if the \c forceLabel parameter was true 283 /// it will be sorted by its id in the graph. 284 void sortByLabel(std::vector<Node>& nodes) const { 285 nodeset_writer.sortByLabel(nodes); 286 } 287 288 /// \brief Sorts the given edge vector by label. 289 /// 290 /// Sorts the given edge vector by label. If there was written an 291 /// "label" named map then the vector will be sorted by the values 292 /// of this map. Otherwise if the \c forceLabel parameter was true 293 /// it will be sorted by its id in the graph. 294 void sortByLabel(std::vector<Edge>& edges) const { 295 edgeset_writer.sortByLabel(edges); 296 } 269 297 270 298 private: … … 371 399 : writer(new LemonWriter(_os)), own_writer(true), 372 400 nodeset_writer(*writer, _graph, std::string()), 373 u _edgeset_writer(*writer, _graph, nodeset_writer, std::string()),401 uedgeset_writer(*writer, _graph, nodeset_writer, std::string()), 374 402 node_writer(*writer, nodeset_writer, std::string()), 375 u _edge_writer(*writer, u_edgeset_writer, std::string()),403 uedge_writer(*writer, uedgeset_writer, std::string()), 376 404 attribute_writer(*writer, std::string()) {} 377 405 … … 383 411 : writer(new LemonWriter(_filename)), own_writer(true), 384 412 nodeset_writer(*writer, _graph, std::string()), 385 u _edgeset_writer(*writer, _graph, nodeset_writer, std::string()),413 uedgeset_writer(*writer, _graph, nodeset_writer, std::string()), 386 414 node_writer(*writer, nodeset_writer, std::string()), 387 u _edge_writer(*writer, u_edgeset_writer, std::string()),415 uedge_writer(*writer, uedgeset_writer, std::string()), 388 416 attribute_writer(*writer, std::string()) {} 389 417 … … 391 419 /// 392 420 /// Construct a new UGraphWriter. It writes the given graph 393 /// to given Lemon Reader.421 /// to given LemonWriter. 394 422 UGraphWriter(LemonWriter& _writer, const Graph& _graph) 395 423 : writer(_writer), own_writer(false), 396 424 nodeset_writer(*writer, _graph, std::string()), 397 u _edgeset_writer(*writer, _graph, nodeset_writer, std::string()),425 uedgeset_writer(*writer, _graph, nodeset_writer, std::string()), 398 426 node_writer(*writer, nodeset_writer, std::string()), 399 u _edge_writer(*writer, u_edgeset_writer, std::string()),427 uedge_writer(*writer, uedgeset_writer, std::string()), 400 428 attribute_writer(*writer, std::string()) {} 401 429 … … 435 463 template <typename Map> 436 464 UGraphWriter& writeEdgeMap(std::string label, const Map& map) { 437 u _edgeset_writer.writeEdgeMap(label, map);465 uedgeset_writer.writeEdgeMap(label, map); 438 466 return *this; 439 467 } … … 446 474 UGraphWriter& writeEdgeMap(std::string label, const Map& map, 447 475 const ItemWriter& iw = ItemWriter()) { 448 u _edgeset_writer.writeEdgeMap(label, map, iw);476 uedgeset_writer.writeEdgeMap(label, map, iw); 449 477 return *this; 450 478 } … … 456 484 template <typename Map> 457 485 UGraphWriter& writeUEdgeMap(std::string label, const Map& map) { 458 u _edgeset_writer.writeUEdgeMap(label, map);486 uedgeset_writer.writeUEdgeMap(label, map); 459 487 return *this; 460 488 } … … 467 495 UGraphWriter& writeUEdgeMap(std::string label, const Map& map, 468 496 const ItemWriter& iw = ItemWriter()) { 469 u _edgeset_writer.writeUEdgeMap(label, map, iw);497 uedgeset_writer.writeUEdgeMap(label, map, iw); 470 498 return *this; 471 499 } … … 485 513 /// command</i> to the writer. 486 514 UGraphWriter& writeEdge(std::string label, const Edge& edge) { 487 u _edge_writer.writeEdge(label, edge);515 uedge_writer.writeEdge(label, edge); 488 516 } 489 517 … … 494 522 /// the writer. 495 523 UGraphWriter& writeUEdge(std::string label, const UEdge& edge) { 496 u _edge_writer.writeUEdge(label, edge);524 uedge_writer.writeUEdge(label, edge); 497 525 } 498 526 … … 535 563 } 536 564 565 /// \brief Returns true if the writer can give back the labels by the items. 566 /// 567 /// Returns true if the writer can give back the the labels by the items. 568 bool isLabelWriter() const { 569 return nodeset_writer.isLabelWriter() && 570 uedgeset_writer.isLabelWriter(); 571 } 572 537 573 /// \brief Write the label of the given node. 538 574 /// 539 /// It writes the label of the given node. If there was written a n"label"575 /// It writes the label of the given node. If there was written a "label" 540 576 /// named node map then it will write the map value belonging to the node. 541 577 void writeLabel(std::ostream& os, const Node& item) const { … … 545 581 /// \brief Write the label of the given edge. 546 582 /// 547 /// It writes the label of the given edge. If there was written a n"label"583 /// It writes the label of the given edge. If there was written a "label" 548 584 /// named edge map then it will write the map value belonging to the edge. 549 585 void writeLabel(std::ostream& os, const Edge& item) const { 550 u _edgeset_writer.writeLabel(os, item);586 uedgeset_writer.writeLabel(os, item); 551 587 } 552 588 … … 554 590 /// 555 591 /// It writes the label of the given undirected edge. If there was 556 /// written a n"label" named edge map then it will write the map592 /// written a "label" named edge map then it will write the map 557 593 /// value belonging to the edge. 558 594 void writeLabel(std::ostream& os, const UEdge& item) const { 559 u _edgeset_writer.writeLabel(os, item);595 uedgeset_writer.writeLabel(os, item); 560 596 } 561 597 598 /// \brief Sorts the given node vector by label. 599 /// 600 /// Sorts the given node vector by label. If there was written an 601 /// "label" named map then the vector will be sorted by the values 602 /// of this map. Otherwise if the \c forceLabel parameter was true 603 /// it will be sorted by its id in the graph. 604 void sortByLabel(std::vector<Node>& nodes) const { 605 nodeset_writer.sortByLabel(nodes); 606 } 607 608 /// \brief Sorts the given edge vector by label. 609 /// 610 /// Sorts the given edge vector by label. If there was written an 611 /// "label" named map then the vector will be sorted by the values 612 /// of this map. Otherwise if the \c forceLabel parameter was true 613 /// it will be sorted by its id in the graph. 614 void sortByLabel(std::vector<Edge>& edges) const { 615 uedgeset_writer.sortByLabel(edges); 616 } 617 618 /// \brief Sorts the given undirected edge vector by label. 619 /// 620 /// Sorts the given undirected edge vector by label. If there was 621 /// written an "label" named map then the vector will be sorted by 622 /// the values of this map. Otherwise if the \c forceLabel 623 /// parameter was true it will be sorted by its id in the graph. 624 void sortByLabel(std::vector<UEdge>& uedges) const { 625 uedgeset_writer.sortByLabel(uedges); 626 } 562 627 563 628 private: … … 567 632 568 633 NodeSetWriter<Graph, WriterTraits> nodeset_writer; 569 UEdgeSetWriter<Graph, WriterTraits> u _edgeset_writer;634 UEdgeSetWriter<Graph, WriterTraits> uedgeset_writer; 570 635 571 636 NodeWriter<Graph> node_writer; 572 UEdgeWriter<Graph> u _edge_writer;637 UEdgeWriter<Graph> uedge_writer; 573 638 574 639 AttributeWriter<WriterTraits> attribute_writer; -
lemon/lemon_reader.h
r2464 r2467 59 59 } 60 60 }; 61 61 62 62 template <typename Item> 63 63 class ItemLabelReader { … … 256 256 } else { 257 257 ErrorMessage msg; 258 msg << "Invalid label error : " << value;258 msg << "Invalid label error"; 259 259 throw DataFormatError(msg.message()); 260 260 } … … 290 290 virtual Item read(std::istream& is) const { 291 291 Value value; 292 reader.read(is, value); 292 reader.read(is, value); 293 293 typename Inverse::const_iterator it = inverse.find(value); 294 294 if (it != inverse.end()) { … … 383 383 virtual Item read(std::istream& is) const = 0; 384 384 virtual bool isLabelReader() const = 0; 385 virtual LabelReaderBase<_Item>* clone() const = 0; 385 386 }; 386 387 … … 391 392 typedef _BoxedLabelReader BoxedLabelReader; 392 393 393 const BoxedLabelReader& boxedLabelReader;394 395 LabelReader(const BoxedLabelReader& _ boxedLabelReader)396 : boxedLabelReader(_boxedLabelReader) {}394 const BoxedLabelReader& labelReader; 395 396 LabelReader(const BoxedLabelReader& _labelReader) 397 : labelReader(_labelReader) {} 397 398 398 399 virtual Item read(std::istream& is) const { 399 400 Item item; 400 boxedLabelReader.readLabel(is, item);401 labelReader.readLabel(is, item); 401 402 return item; 402 403 } 403 404 404 405 virtual bool isLabelReader() const { 405 return boxedLabelReader.isLabelReader(); 406 return labelReader.isLabelReader(); 407 } 408 409 LabelReader<Item, BoxedLabelReader>* clone() const { 410 return new LabelReader<Item, BoxedLabelReader>(labelReader); 406 411 } 407 412 }; … … 724 729 char buf[2048]; 725 730 FilterStreamBuf buffer(*is, line_num); 726 727 731 try { 728 732 buffer.pubsetbuf(buf, sizeof(buf)); … … 1512 1516 /// It reads an id from the stream and gives back which undirected edge 1513 1517 /// belongs to it. It is possible only if there was read an "label" named map. 1514 void readLabel(std::istream& is, UEdge& u Edge) const {1515 u Edge = inverter->read(is);1518 void readLabel(std::istream& is, UEdge& uedge) const { 1519 uedge = inverter->read(is); 1516 1520 } 1517 1521 … … 1525 1529 char c; 1526 1530 is >> c; 1527 UEdge u Edge = inverter->read(is);1531 UEdge uedge = inverter->read(is); 1528 1532 if (c == '+') { 1529 edge = graph.direct(u Edge, true);1533 edge = graph.direct(uedge, true); 1530 1534 } else if (c == '-') { 1531 edge = graph.direct(u Edge, false);1535 edge = graph.direct(uedge, false); 1532 1536 } else { 1533 1537 throw DataFormatError("Wrong id format for edge " … … 1808 1812 checkConcept<_reader_bits::ItemLabelReader<UEdge>, _LabelReader>(); 1809 1813 checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>(); 1810 u EdgeLabelReader.reset(new _reader_bits::1811 1814 uedgeLabelReader.reset(new _reader_bits:: 1815 LabelReader<UEdge, _LabelReader>(_labelReader)); 1812 1816 edgeLabelReader.reset(new _reader_bits:: 1813 LabelReader<Edge, _LabelReader>(_labelReader));1817 LabelReader<Edge, _LabelReader>(_labelReader)); 1814 1818 } 1815 1819 … … 1828 1832 /// Add an undirected edge reader command for the UEdgeReader. 1829 1833 void readUEdge(std::string label, UEdge& item) { 1830 if (u EdgeReaders.find(label) != uEdgeReaders.end()) {1834 if (uedgeReaders.find(label) != uedgeReaders.end()) { 1831 1835 ErrorMessage msg; 1832 1836 msg << "Multiple read rule for undirected edge: " << label; 1833 1837 throw IoParameterError(msg.message()); 1834 1838 } 1835 u EdgeReaders.insert(make_pair(label, _reader_bits::1839 uedgeReaders.insert(make_pair(label, _reader_bits:: 1836 1840 ItemStore<UEdge>(item))); 1837 1841 } … … 1871 1875 throw DataFormatError("Cannot find undirected edgeset or label map"); 1872 1876 } 1873 if (!u EdgeLabelReader->isLabelReader()) {1877 if (!uedgeLabelReader->isLabelReader()) { 1874 1878 throw DataFormatError("Cannot find undirected edgeset or label map"); 1875 1879 } … … 1880 1884 ls >> id; 1881 1885 { 1882 typename UEdgeReaders::iterator it = u EdgeReaders.find(id);1883 if (it != u EdgeReaders.end()) {1884 it->second.read(u EdgeLabelReader->read(ls));1886 typename UEdgeReaders::iterator it = uedgeReaders.find(id); 1887 if (it != uedgeReaders.end()) { 1888 it->second.read(uedgeLabelReader->read(ls)); 1885 1889 it->second.touch(); 1886 1890 continue; … … 1903 1907 } 1904 1908 } 1905 for (typename UEdgeReaders::iterator it = u EdgeReaders.begin();1906 it != u EdgeReaders.end(); ++it) {1909 for (typename UEdgeReaders::iterator it = uedgeReaders.begin(); 1910 it != uedgeReaders.end(); ++it) { 1907 1911 if (!it->second.touched()) { 1908 1912 ErrorMessage msg; … … 1914 1918 1915 1919 virtual void missing() { 1916 if (edgeReaders.empty() && u EdgeReaders.empty()) return;1920 if (edgeReaders.empty() && uedgeReaders.empty()) return; 1917 1921 ErrorMessage msg; 1918 1922 msg << "UEdges section not found in file: @uedges " << name; … … 1926 1930 typedef std::map<std::string, 1927 1931 _reader_bits::ItemStore<UEdge> > UEdgeReaders; 1928 UEdgeReaders u EdgeReaders;1929 std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > u EdgeLabelReader;1932 UEdgeReaders uedgeReaders; 1933 std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > uedgeLabelReader; 1930 1934 1931 1935 typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders; … … 2052 2056 typedef std::map<std::string, _reader_bits::ValueReaderBase*> Readers; 2053 2057 Readers readers; 2058 }; 2059 2060 /// \ingroup section_io 2061 /// \brief SectionReader for reading extra node maps. 2062 /// 2063 /// The lemon format can store maps in the nodeset sections. This 2064 /// class let you make distinict section to store maps. The main 2065 /// purpose of this class is a logical separation of some maps. The 2066 /// other useful application could be to store paths in node maps. 2067 /// 2068 /// The first line of the section contains the names of the maps 2069 /// separated with white spaces. Each next line describes an item 2070 /// in the itemset, and contains in the first column the label of 2071 /// the item and then the mapped values for each map. 2072 /// 2073 /// \relates LemonReader 2074 template <typename _Graph, typename _Traits = DefaultReaderTraits> 2075 class NodeMapReader : public LemonReader::SectionReader { 2076 typedef LemonReader::SectionReader Parent; 2077 public: 2078 2079 typedef _Graph Graph; 2080 typedef typename Graph::Node Node; 2081 typedef _Traits Traits; 2082 typedef typename Traits::Skipper DefaultSkipper; 2083 2084 /// \brief Constructor. 2085 /// 2086 /// Constructor for NodeMapReader. It creates the NodeMapReader and 2087 /// attach it into the given LemonReader. The reader will read 2088 /// the section when the \c section_name and the \c _name are the same. 2089 template <typename _LabelReader> 2090 NodeMapReader(LemonReader& _reader, 2091 const Graph& _graph, 2092 const _LabelReader& _labelReader, 2093 const std::string& _name = std::string(), 2094 const DefaultSkipper& _skipper = DefaultSkipper()) 2095 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { 2096 labelReader.reset(new _reader_bits:: 2097 LabelReader<Node, _LabelReader>(_labelReader)); 2098 } 2099 2100 2101 /// \brief Destructor. 2102 /// 2103 /// Destructor for NodeMapReader. 2104 virtual ~NodeMapReader() { 2105 for (typename MapReaders::iterator it = readers.begin(); 2106 it != readers.end(); ++it) { 2107 delete it->second; 2108 } 2109 } 2110 2111 private: 2112 NodeMapReader(const NodeMapReader&); 2113 void operator=(const NodeMapReader&); 2114 2115 public: 2116 2117 /// \brief Add a new node map reader command for the reader. 2118 /// 2119 /// Add a new node map reader command for the reader. 2120 template <typename Map> 2121 NodeMapReader& readNodeMap(std::string label, Map& map) { 2122 return _readMap< 2123 typename Traits::template Reader<typename Map::Value>, Map, 2124 typename _reader_bits::Arg<Map>::Type>(label, map); 2125 } 2126 2127 template <typename Map> 2128 NodeMapReader& readNodeMap(std::string label, const Map& map) { 2129 return _readMap< 2130 typename Traits::template Reader<typename Map::Value>, Map, 2131 typename _reader_bits::Arg<Map>::Type>(label, map); 2132 } 2133 2134 /// \brief Add a new node map reader command for the reader. 2135 /// 2136 /// Add a new node map reader command for the reader. 2137 template <typename ItemReader, typename Map> 2138 NodeMapReader& readNodeMap(std::string label, Map& map, 2139 const ItemReader& ir = ItemReader()) { 2140 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2141 (label, map, ir); 2142 } 2143 2144 template <typename ItemReader, typename Map> 2145 NodeMapReader& readNodeMap(std::string label, const Map& map, 2146 const ItemReader& ir = ItemReader()) { 2147 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2148 (label, map, ir); 2149 } 2150 2151 private: 2152 2153 template <typename ItemReader, typename Map, typename MapParameter> 2154 NodeMapReader& _readMap(std::string label, MapParameter map, 2155 const ItemReader& ir = ItemReader()) { 2156 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); 2157 checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>(); 2158 if (readers.find(label) != readers.end()) { 2159 ErrorMessage msg; 2160 msg << "Multiple read rule for map: " << label; 2161 throw IoParameterError(msg.message()); 2162 } 2163 readers.insert( 2164 make_pair(label, new _reader_bits:: 2165 MapReader<Node, Map, ItemReader>(map, ir))); 2166 return *this; 2167 } 2168 2169 public: 2170 2171 /// \brief Add a new node map skipper command for the reader. 2172 /// 2173 /// Add a new node map skipper command for the reader. 2174 template <typename ItemReader> 2175 NodeMapReader& skipNodeMap(std::string label, 2176 const ItemReader& ir = ItemReader()) { 2177 if (readers.find(label) != readers.end()) { 2178 ErrorMessage msg; 2179 msg << "Multiple read rule for map: " << label; 2180 throw IoParameterError(msg.message()); 2181 } 2182 readers.insert(make_pair(label, new _reader_bits:: 2183 SkipReader<Node, ItemReader>(ir))); 2184 return *this; 2185 } 2186 2187 protected: 2188 2189 /// \brief Gives back true when the SectionReader can process 2190 /// the section with the given header line. 2191 /// 2192 /// It gives back true when the header line starts with \c \@mapset, 2193 /// and the header line's name and the mapset's name are the same. 2194 virtual bool header(const std::string& line) { 2195 std::istringstream ls(line); 2196 std::string command; 2197 std::string id; 2198 ls >> command >> id; 2199 return command == "@nodemaps" && name == id; 2200 } 2201 2202 /// \brief Reader function of the section. 2203 /// 2204 /// It reads the content of the section. 2205 virtual void read(std::istream& is) { 2206 std::vector<_reader_bits::MapReaderBase<Node>* > index; 2207 std::string line; 2208 2209 { 2210 getline(is, line); 2211 std::istringstream ls(line); 2212 std::string id; 2213 while (ls >> id) { 2214 typename MapReaders::iterator it = readers.find(id); 2215 if (it != readers.end()) { 2216 it->second->touch(); 2217 index.push_back(it->second); 2218 } else { 2219 index.push_back(&skipper); 2220 } 2221 } 2222 } 2223 for (typename MapReaders::iterator it = readers.begin(); 2224 it != readers.end(); ++it) { 2225 if (!it->second->touched()) { 2226 ErrorMessage msg; 2227 msg << "Map not found in file: " << it->first; 2228 throw IoParameterError(msg.message()); 2229 } 2230 } 2231 while (getline(is, line)) { 2232 std::istringstream ls(line); 2233 Node node = labelReader->read(ls); 2234 for (int i = 0; i < int(index.size()); ++i) { 2235 index[i]->read(ls, node); 2236 } 2237 } 2238 } 2239 2240 virtual void missing() { 2241 if (readers.empty()) return; 2242 ErrorMessage msg; 2243 msg << "NodeMap section not found in file: @nodemaps " << name; 2244 throw IoParameterError(msg.message()); 2245 } 2246 2247 private: 2248 2249 typedef std::map<std::string, _reader_bits::MapReaderBase<Node>*> MapReaders; 2250 MapReaders readers; 2251 2252 const Graph& graph; 2253 std::string name; 2254 _reader_bits::SkipReader<Node, DefaultSkipper> skipper; 2255 std::auto_ptr<_reader_bits::LabelReaderBase<Node> > labelReader; 2256 2257 }; 2258 2259 /// \ingroup section_io 2260 /// \brief SectionReader for reading extra edge maps. 2261 /// 2262 /// The lemon format can store maps in the edgeset sections. This 2263 /// class let you make distinict section to store maps. The main 2264 /// purpose of this class is a logical separation of some maps. The 2265 /// other useful application could be to store paths in edge maps. 2266 /// 2267 /// The first line of the section contains the names of the maps 2268 /// separated with white spaces. Each next line describes an item 2269 /// in the itemset, and contains in the first column the label of 2270 /// the item and then the mapped values for each map. 2271 /// 2272 /// \relates LemonReader 2273 template <typename _Graph, typename _Traits = DefaultReaderTraits> 2274 class EdgeMapReader : public LemonReader::SectionReader { 2275 typedef LemonReader::SectionReader Parent; 2276 public: 2277 2278 typedef _Graph Graph; 2279 typedef typename Graph::Edge Edge; 2280 typedef _Traits Traits; 2281 typedef typename Traits::Skipper DefaultSkipper; 2282 2283 /// \brief Constructor. 2284 /// 2285 /// Constructor for EdgeMapReader. It creates the EdgeMapReader and 2286 /// attach it into the given LemonReader. The reader will read 2287 /// the section when the \c section_name and the \c _name are the same. 2288 template <typename _LabelReader> 2289 EdgeMapReader(LemonReader& _reader, 2290 const Graph& _graph, 2291 const _LabelReader& _labelReader, 2292 const std::string& _name = std::string(), 2293 const DefaultSkipper& _skipper = DefaultSkipper()) 2294 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { 2295 labelReader.reset(new _reader_bits:: 2296 LabelReader<Edge, _LabelReader>(_labelReader)); 2297 } 2298 2299 2300 /// \brief Destructor. 2301 /// 2302 /// Destructor for EdgeMapReader. 2303 virtual ~EdgeMapReader() { 2304 for (typename MapReaders::iterator it = readers.begin(); 2305 it != readers.end(); ++it) { 2306 delete it->second; 2307 } 2308 } 2309 2310 private: 2311 EdgeMapReader(const EdgeMapReader&); 2312 void operator=(const EdgeMapReader&); 2313 2314 public: 2315 2316 /// \brief Add a new edge map reader command for the reader. 2317 /// 2318 /// Add a new edge map reader command for the reader. 2319 template <typename Map> 2320 EdgeMapReader& readEdgeMap(std::string label, Map& map) { 2321 return _readMap< 2322 typename Traits::template Reader<typename Map::Value>, Map, 2323 typename _reader_bits::Arg<Map>::Type>(label, map); 2324 } 2325 2326 template <typename Map> 2327 EdgeMapReader& readEdgeMap(std::string label, const Map& map) { 2328 return _readMap< 2329 typename Traits::template Reader<typename Map::Value>, Map, 2330 typename _reader_bits::Arg<Map>::Type>(label, map); 2331 } 2332 2333 /// \brief Add a new edge map reader command for the reader. 2334 /// 2335 /// Add a new edge map reader command for the reader. 2336 template <typename ItemReader, typename Map> 2337 EdgeMapReader& readEdgeMap(std::string label, Map& map, 2338 const ItemReader& ir = ItemReader()) { 2339 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2340 (label, map, ir); 2341 } 2342 2343 template <typename ItemReader, typename Map> 2344 EdgeMapReader& readEdgeMap(std::string label, const Map& map, 2345 const ItemReader& ir = ItemReader()) { 2346 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2347 (label, map, ir); 2348 } 2349 2350 private: 2351 2352 template <typename ItemReader, typename Map, typename MapParameter> 2353 EdgeMapReader& _readMap(std::string label, MapParameter map, 2354 const ItemReader& ir = ItemReader()) { 2355 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); 2356 checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>(); 2357 if (readers.find(label) != readers.end()) { 2358 ErrorMessage msg; 2359 msg << "Multiple read rule for map: " << label; 2360 throw IoParameterError(msg.message()); 2361 } 2362 readers.insert( 2363 make_pair(label, new _reader_bits:: 2364 MapReader<Edge, Map, ItemReader>(map, ir))); 2365 return *this; 2366 } 2367 2368 public: 2369 2370 /// \brief Add a new edge map skipper command for the reader. 2371 /// 2372 /// Add a new edge map skipper command for the reader. 2373 template <typename ItemReader> 2374 EdgeMapReader& skipEdgeMap(std::string label, 2375 const ItemReader& ir = ItemReader()) { 2376 if (readers.find(label) != readers.end()) { 2377 ErrorMessage msg; 2378 msg << "Multiple read rule for map: " << label; 2379 throw IoParameterError(msg.message()); 2380 } 2381 readers.insert(make_pair(label, new _reader_bits:: 2382 SkipReader<Edge, ItemReader>(ir))); 2383 return *this; 2384 } 2385 2386 protected: 2387 2388 /// \brief Gives back true when the SectionReader can process 2389 /// the section with the given header line. 2390 /// 2391 /// It gives back true when the header line starts with \c \@mapset, 2392 /// and the header line's name and the mapset's name are the same. 2393 virtual bool header(const std::string& line) { 2394 std::istringstream ls(line); 2395 std::string command; 2396 std::string id; 2397 ls >> command >> id; 2398 return (command == "@edgemaps" || command == "@uedgemaps") && name == id; 2399 } 2400 2401 /// \brief Reader function of the section. 2402 /// 2403 /// It reads the content of the section. 2404 virtual void read(std::istream& is) { 2405 std::vector<_reader_bits::MapReaderBase<Edge>* > index; 2406 std::string line; 2407 2408 { 2409 getline(is, line); 2410 std::istringstream ls(line); 2411 std::string id; 2412 while (ls >> id) { 2413 typename MapReaders::iterator it = readers.find(id); 2414 if (it != readers.end()) { 2415 it->second->touch(); 2416 index.push_back(it->second); 2417 } else { 2418 index.push_back(&skipper); 2419 } 2420 } 2421 } 2422 for (typename MapReaders::iterator it = readers.begin(); 2423 it != readers.end(); ++it) { 2424 if (!it->second->touched()) { 2425 ErrorMessage msg; 2426 msg << "Map not found in file: " << it->first; 2427 throw IoParameterError(msg.message()); 2428 } 2429 } 2430 while (getline(is, line)) { 2431 std::istringstream ls(line); 2432 Edge edge = labelReader->read(ls); 2433 for (int i = 0; i < int(index.size()); ++i) { 2434 index[i]->read(ls, edge); 2435 } 2436 } 2437 } 2438 2439 virtual void missing() { 2440 if (readers.empty()) return; 2441 ErrorMessage msg; 2442 msg << "EdgeMap section not found in file: @edgemaps " << name; 2443 throw IoParameterError(msg.message()); 2444 } 2445 2446 private: 2447 2448 typedef std::map<std::string, _reader_bits::MapReaderBase<Edge>*> MapReaders; 2449 MapReaders readers; 2450 2451 const Graph& graph; 2452 std::string name; 2453 _reader_bits::SkipReader<Edge, DefaultSkipper> skipper; 2454 std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > labelReader; 2455 2456 }; 2457 2458 /// \ingroup section_io 2459 /// \brief SectionReader for reading extra undirected edge maps. 2460 /// 2461 /// The lemon format can store maps in the uedgeset sections. This 2462 /// class let you make distinict section to store maps. The main 2463 /// purpose of this class is a logical separation of some maps. The 2464 /// other useful application could be to store paths in undirected 2465 /// edge maps. 2466 /// 2467 /// The first line of the section contains the names of the maps 2468 /// separated with white spaces. Each next line describes an item 2469 /// in the itemset, and contains in the first column the label of 2470 /// the item and then the mapped values for each map. 2471 /// 2472 /// \relates LemonReader 2473 template <typename _Graph, typename _Traits = DefaultReaderTraits> 2474 class UEdgeMapReader : public LemonReader::SectionReader { 2475 typedef LemonReader::SectionReader Parent; 2476 public: 2477 2478 typedef _Graph Graph; 2479 typedef typename Graph::Edge Edge; 2480 typedef typename Graph::UEdge UEdge; 2481 typedef _Traits Traits; 2482 typedef typename Traits::Skipper DefaultSkipper; 2483 2484 /// \brief Constructor. 2485 /// 2486 /// Constructor for UEdgeMapReader. It creates the UEdgeMapReader and 2487 /// attach it into the given LemonReader. The reader will read 2488 /// the section when the \c section_name and the \c _name are the same. 2489 template <typename _LabelReader> 2490 UEdgeMapReader(LemonReader& _reader, const Graph& _graph, 2491 const _LabelReader& _labelReader, 2492 const std::string& _name = std::string(), 2493 const DefaultSkipper& _skipper = DefaultSkipper()) 2494 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { 2495 labelReader.reset(new _reader_bits:: 2496 LabelReader<UEdge, _LabelReader>(_labelReader)); 2497 } 2498 2499 2500 /// \brief Destructor. 2501 /// 2502 /// Destructor for UEdgeMapReader. 2503 virtual ~UEdgeMapReader() { 2504 for (typename MapReaders::iterator it = readers.begin(); 2505 it != readers.end(); ++it) { 2506 delete it->second; 2507 } 2508 } 2509 2510 private: 2511 UEdgeMapReader(const UEdgeMapReader&); 2512 void operator=(const UEdgeMapReader&); 2513 2514 public: 2515 2516 /// \brief Add a new undirected edge map reader command for the 2517 /// reader. 2518 /// 2519 /// Add a new undirected edge map reader command for the reader. 2520 template <typename Map> 2521 UEdgeMapReader& readUEdgeMap(std::string label, Map& map) { 2522 return _readMap< 2523 typename Traits::template Reader<typename Map::Value>, Map, 2524 typename _reader_bits::Arg<Map>::Type>(label, map); 2525 } 2526 2527 template <typename Map> 2528 UEdgeMapReader& readUEdgeMap(std::string label, const Map& map) { 2529 return _readMap< 2530 typename Traits::template Reader<typename Map::Value>, Map, 2531 typename _reader_bits::Arg<Map>::Type>(label, map); 2532 } 2533 2534 /// \brief Add a new undirected edge map reader command for the 2535 /// reader. 2536 /// 2537 /// Add a new undirected edge map reader command for the reader. 2538 template <typename ItemReader, typename Map> 2539 UEdgeMapReader& readUEdgeMap(std::string label, Map& map, 2540 const ItemReader& ir = ItemReader()) { 2541 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2542 (label, map, ir); 2543 } 2544 2545 template <typename ItemReader, typename Map> 2546 UEdgeMapReader& readUEdgeMap(std::string label, const Map& map, 2547 const ItemReader& ir = ItemReader()) { 2548 return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type> 2549 (label, map, ir); 2550 } 2551 2552 private: 2553 2554 template <typename ItemReader, typename Map, typename MapParameter> 2555 UEdgeMapReader& _readMap(std::string label, MapParameter map, 2556 const ItemReader& ir = ItemReader()) { 2557 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); 2558 checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>(); 2559 if (readers.find(label) != readers.end()) { 2560 ErrorMessage msg; 2561 msg << "Multiple read rule for map: " << label; 2562 throw IoParameterError(msg.message()); 2563 } 2564 readers.insert( 2565 make_pair(label, new _reader_bits:: 2566 MapReader<UEdge, Map, ItemReader>(map, ir))); 2567 return *this; 2568 } 2569 2570 public: 2571 2572 /// \brief Add a new undirected edge map skipper command for the 2573 /// reader. 2574 /// 2575 /// Add a new undirected edge map skipper command for the reader. 2576 template <typename ItemReader> 2577 UEdgeMapReader& skipUEdgeMap(std::string label, 2578 const ItemReader& ir = ItemReader()) { 2579 if (readers.find(label) != readers.end()) { 2580 ErrorMessage msg; 2581 msg << "Multiple read rule for map: " << label; 2582 throw IoParameterError(msg.message()); 2583 } 2584 readers.insert(make_pair(label, new _reader_bits:: 2585 SkipReader<Edge, ItemReader>(ir))); 2586 return *this; 2587 } 2588 2589 /// \brief Add a new directed edge map reader command for the reader. 2590 /// 2591 /// Add a new directed edge map reader command for the reader. 2592 template <typename Map> 2593 UEdgeMapReader& readEdgeMap(std::string label, Map& map) { 2594 return _readDirMap< 2595 typename Traits::template Reader<typename Map::Value>, Map, 2596 typename _reader_bits::Arg<Map>::Type>(label, map); 2597 } 2598 2599 template <typename Map> 2600 UEdgeMapReader& readEdgeMap(std::string label, const Map& map) { 2601 return _readDirMap< 2602 typename Traits::template Reader<typename Map::Value>, Map, 2603 typename _reader_bits::Arg<Map>::Type>(label, map); 2604 } 2605 2606 /// \brief Add a new directed edge map reader command for the reader. 2607 /// 2608 /// Add a new directed edge map reader command for the reader. 2609 template <typename ItemReader, typename Map> 2610 UEdgeMapReader& readEdgeMap(std::string label, Map& map, 2611 const ItemReader& ir = ItemReader()) { 2612 return _readDirMap<ItemReader, Map, 2613 typename _reader_bits::Arg<Map>::Type>(label, map, ir); 2614 } 2615 2616 template <typename ItemReader, typename Map> 2617 UEdgeMapReader& readEdgeMap(std::string label, const Map& map, 2618 const ItemReader& ir = ItemReader()) { 2619 return _readDirMap<ItemReader, Map, 2620 typename _reader_bits::Arg<Map>::Type>(label, map, ir); 2621 } 2622 2623 private: 2624 2625 template <typename ItemReader, typename Map, typename MapParameter> 2626 UEdgeMapReader& _readDirMap(std::string label, MapParameter map, 2627 const ItemReader& ir = ItemReader()) { 2628 checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>(); 2629 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); 2630 readUEdgeMap("+" + label, 2631 _reader_bits::forwardComposeMap(graph, map), ir); 2632 readUEdgeMap("-" + label, 2633 _reader_bits::backwardComposeMap(graph, map), ir); 2634 return *this; 2635 } 2636 2637 public: 2638 2639 /// \brief Add a new directed edge map skipper command for the reader. 2640 /// 2641 /// Add a new directed edge map skipper command for the reader. 2642 template <typename ItemReader> 2643 UEdgeMapReader& skipEdgeMap(std::string label, 2644 const ItemReader& ir = ItemReader()) { 2645 skipUEdgeMap("+" + label, ir); 2646 skipUEdgeMap("-" + label, ir); 2647 return *this; 2648 } 2649 2650 protected: 2651 2652 /// \brief Gives back true when the SectionReader can process 2653 /// the section with the given header line. 2654 /// 2655 /// It gives back true when the header line starts with \c \@mapset, 2656 /// and the header line's name and the mapset's name are the same. 2657 virtual bool header(const std::string& line) { 2658 std::istringstream ls(line); 2659 std::string command; 2660 std::string id; 2661 ls >> command >> id; 2662 return (command == "@edgemaps" || command == "@uedgemaps") && name == id; 2663 } 2664 2665 /// \brief Reader function of the section. 2666 /// 2667 /// It reads the content of the section. 2668 virtual void read(std::istream& is) { 2669 std::vector<_reader_bits::MapReaderBase<UEdge>* > index; 2670 std::string line; 2671 2672 { 2673 getline(is, line); 2674 std::istringstream ls(line); 2675 std::string id; 2676 while (ls >> id) { 2677 typename MapReaders::iterator it = readers.find(id); 2678 if (it != readers.end()) { 2679 it->second->touch(); 2680 index.push_back(it->second); 2681 } else { 2682 index.push_back(&skipper); 2683 } 2684 } 2685 } 2686 for (typename MapReaders::iterator it = readers.begin(); 2687 it != readers.end(); ++it) { 2688 if (!it->second->touched()) { 2689 ErrorMessage msg; 2690 msg << "Map not found in file: " << it->first; 2691 throw IoParameterError(msg.message()); 2692 } 2693 } 2694 while (getline(is, line)) { 2695 std::istringstream ls(line); 2696 UEdge uedge = labelReader->read(ls); 2697 for (int i = 0; i < int(index.size()); ++i) { 2698 index[i]->read(ls, uedge); 2699 } 2700 } 2701 } 2702 2703 virtual void missing() { 2704 if (readers.empty()) return; 2705 ErrorMessage msg; 2706 msg << "UEdgeMap section not found in file: @uedgemaps " << name; 2707 throw IoParameterError(msg.message()); 2708 } 2709 2710 private: 2711 2712 const Graph& graph; 2713 std::string name; 2714 2715 typedef std::map<std::string, 2716 _reader_bits::MapReaderBase<UEdge>*> MapReaders; 2717 2718 MapReaders readers; 2719 _reader_bits::SkipReader<UEdge, DefaultSkipper> skipper; 2720 2721 std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > labelReader; 2722 2054 2723 }; 2055 2724 -
lemon/lemon_writer.h
r2414 r2467 72 72 }; 73 73 74 template <typename UGraph, typename Map> 75 struct UEdgeComposeLess { 76 UEdgeComposeLess(const UGraph& _ugraph, const Map& _map) 77 : ugraph(_ugraph), map(_map), less() {} 78 79 bool operator()(const typename UGraph::Edge& p, 80 const typename UGraph::Edge& q) const { 81 return p != q ? less(map[p], map[q]) : 82 (!ugraph.direction(p) && ugraph.direction(q)); 83 } 84 85 const UGraph& ugraph; 86 const Map& map; 87 Less<typename Map::Value> less; 88 }; 89 74 90 template <typename Item> 75 91 class ItemLabelWriter { … … 124 140 : graph(_graph), map(_map) {} 125 141 126 Value operator[](const Key& key) {127 return map[graph.direct(key, false)];142 Value operator[](const Key& key) const { 143 return map[graph.direct(key, true)]; 128 144 } 129 145 … … 148 164 : graph(_graph), map(_map) {} 149 165 150 Value operator[](const Key& key) {166 Value operator[](const Key& key) const { 151 167 return map[graph.direct(key, false)]; 152 168 } … … 200 216 201 217 virtual void write(std::ostream& os, const Item& item) const = 0; 202 virtual void sort ByMap(std::vector<Item>&) const = 0;218 virtual void sort(std::vector<Item>&) const = 0; 203 219 }; 204 220 … … 225 241 } 226 242 227 virtual void sortByMap(std::vector<Item>& items) const { 243 virtual void sort(std::vector<Item>& items) const { 244 ComposeLess<Map> less(map); 245 std::sort(items.begin(), items.end(), less); 246 } 247 248 }; 249 250 template <typename _UGraph> 251 class UEdgeMapWriterBase { 252 public: 253 typedef typename _UGraph::Edge Edge; 254 typedef typename _UGraph::UEdge UEdge; 255 256 typedef UEdge Item; 257 258 virtual ~UEdgeMapWriterBase() {} 259 260 virtual void write(std::ostream& os, const Item& item) const = 0; 261 virtual void sort(const _UGraph&, std::vector<Edge>&) const = 0; 262 virtual void sort(std::vector<UEdge>&) const = 0; 263 }; 264 265 266 template <typename _UGraph, typename _Map, typename _Writer> 267 class UEdgeMapWriter : public UEdgeMapWriterBase<_UGraph> { 268 public: 269 typedef _Map Map; 270 typedef _Writer Writer; 271 typedef typename Writer::Value Value; 272 273 typedef typename _UGraph::Edge Edge; 274 typedef typename _UGraph::UEdge UEdge; 275 typedef UEdge Item; 276 277 typename _writer_bits::Ref<Map>::Type map; 278 Writer writer; 279 280 UEdgeMapWriter(const Map& _map, const Writer& _writer) 281 : map(_map), writer(_writer) {} 282 283 virtual ~UEdgeMapWriter() {} 284 285 virtual void write(std::ostream& os, const Item& item) const { 286 Value value = map[item]; 287 writer.write(os, value); 288 } 289 290 virtual void sort(const _UGraph& ugraph, std::vector<Edge>& items) const { 291 UEdgeComposeLess<_UGraph, Map> less(ugraph, map); 292 std::sort(items.begin(), items.end(), less); 293 } 294 295 virtual void sort(std::vector<UEdge>& items) const { 228 296 ComposeLess<Map> less(map); 229 297 std::sort(items.begin(), items.end(), less); … … 263 331 virtual ~LabelWriterBase() {} 264 332 virtual void write(std::ostream&, const Item&) const = 0; 333 virtual void sort(std::vector<Item>&) const = 0; 265 334 virtual bool isLabelWriter() const = 0; 335 virtual LabelWriterBase* clone() const = 0; 266 336 }; 267 337 … … 280 350 labelWriter.writeLabel(os, item); 281 351 } 352 virtual void sort(std::vector<Item>& items) const { 353 labelWriter.sortByLabel(items); 354 } 282 355 283 356 virtual bool isLabelWriter() const { 284 357 return labelWriter.isLabelWriter(); 358 } 359 360 virtual LabelWriter* clone() const { 361 return new LabelWriter(labelWriter); 285 362 } 286 363 }; … … 426 503 /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 427 504 /// then the label map will be the id in the graph. In addition if the 428 /// the \c _ sortByLabelis true then the writer will write the edges505 /// the \c _forceSort is true then the writer will write the edges 429 506 /// sorted by the labels. 430 507 /// … … 445 522 /// parameter is true then the writer will write own label map when 446 523 /// the user does not give "label" named map. In addition if the 447 /// the \c _ sortByLabelis true then the writer will write the edges524 /// the \c _forceSort is true then the writer will write the edges 448 525 /// sorted by the labels. 449 526 NodeSetWriter(LemonWriter& _writer, const Graph& _graph, 450 527 const std::string& _name = std::string(), 451 bool _forceLabelMap = true, bool _ sortByLabel= true)528 bool _forceLabelMap = true, bool _forceSort = true) 452 529 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 453 sortByLabel(_sortByLabel), graph(_graph), name(_name) {}530 forceSort(_forceSort), graph(_graph), name(_name) {} 454 531 455 532 /// \brief Destructor. … … 516 593 items.push_back(it); 517 594 } 518 if ( sortByLabel) {595 if (forceSort) { 519 596 if (labelMap) { 520 labelMap->sort ByMap(items);597 labelMap->sort(items); 521 598 } else { 522 599 typedef IdMap<Graph, Node> Map; … … 551 628 /// 552 629 /// Returns true if the nodeset can write the labels of the nodes. 553 /// It is possible only if a n"label" named map was written or the630 /// It is possible only if a "label" named map was written or the 554 631 /// \c _forceLabelMap constructor parameter was true. 555 632 bool isLabelWriter() const { … … 559 636 /// \brief Write the label of the given node. 560 637 /// 561 /// It writes the label of the given node. If there was written a n"label"638 /// It writes the label of the given node. If there was written a "label" 562 639 /// named map then it will write the map value belongs to the node. 563 640 /// Otherwise if the \c forceLabel parameter was true it will write … … 571 648 } 572 649 650 /// \brief Sorts the given node vector by label. 651 /// 652 /// Sorts the given node vector by label. If there was written an 653 /// "label" named map then the vector will be sorted by the values 654 /// of this map. Otherwise if the \c forceLabel parameter was true 655 /// it will be sorted by its id in the graph. 656 void sortByLabel(std::vector<Node>& nodes) const { 657 if (labelMap) { 658 labelMap->sort(nodes); 659 } else { 660 typedef IdMap<Graph, Node> Map; 661 Map map(graph); 662 _writer_bits::ComposeLess<Map> less(map); 663 std::sort(nodes.begin(), nodes.end(), less); 664 } 665 } 666 573 667 private: 574 668 … … 579 673 _writer_bits::MapWriterBase<Node>* labelMap; 580 674 bool forceLabelMap; 581 bool sortByLabel;675 bool forceSort; 582 676 583 677 const Graph& graph; … … 603 697 /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 604 698 /// then the label map will be the id in the graph. In addition if the 605 /// the \c _ sortByLabelis true then the writer will write the edges699 /// the \c _forceSort is true then the writer will write the edges 606 700 /// sorted by the labels. 607 701 /// … … 628 722 /// parameter is true then the writer will write own label map if 629 723 /// the user does not give "label" named map. In addition if the 630 /// the \c _ sortByLabelis true then the writer will write the724 /// the \c _forceSort is true then the writer will write the 631 725 /// edges sorted by the labels. 632 726 template <typename NodeLabelWriter> … … 634 728 const NodeLabelWriter& _nodeLabelWriter, 635 729 const std::string& _name = std::string(), 636 bool _forceLabelMap = true, bool _ sortByLabel= true)730 bool _forceLabelMap = true, bool _forceSort = true) 637 731 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 638 sortByLabel(_sortByLabel), graph(_graph), name(_name) {732 forceSort(_forceSort), graph(_graph), name(_name) { 639 733 checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); 640 734 nodeLabelWriter.reset(new _writer_bits:: … … 708 802 items.push_back(it); 709 803 } 710 if ( sortByLabel) {804 if (forceSort) { 711 805 if (labelMap) { 712 labelMap->sort ByMap(items);806 labelMap->sort(items); 713 807 } else { 714 808 typedef IdMap<Graph, Edge> Map; … … 748 842 /// 749 843 /// Returns true if the edgeset can write the labels of the edges. 750 /// It is possible only if a n"label" named map was written or the844 /// It is possible only if a "label" named map was written or the 751 845 /// \c _forceLabelMap constructor parameter was true. 752 846 bool isLabelWriter() const { … … 756 850 /// \brief Write the label of the given edge. 757 851 /// 758 /// It writes the label of the given edge. If there was written a n"label"852 /// It writes the label of the given edge. If there was written a "label" 759 853 /// named map then it will write the map value belongs to the edge. 760 854 /// Otherwise if the \c forceLabel parameter was true it will write … … 768 862 } 769 863 864 /// \brief Sorts the given edge vector by label. 865 /// 866 /// Sorts the given edge vector by label. If there was written an 867 /// "label" named map then the vector will be sorted by the values 868 /// of this map. Otherwise if the \c forceLabel parameter was true 869 /// it will be sorted by its id in the graph. 870 void sortByLabel(std::vector<Edge>& edges) const { 871 if (labelMap) { 872 labelMap->sort(edges); 873 } else { 874 typedef IdMap<Graph, Edge> Map; 875 Map map(graph); 876 _writer_bits::ComposeLess<Map> less(map); 877 std::sort(edges.begin(), edges.end(), less); 878 } 879 } 880 770 881 private: 771 882 … … 776 887 _writer_bits::MapWriterBase<Edge>* labelMap; 777 888 bool forceLabelMap; 778 bool sortByLabel;889 bool forceSort; 779 890 780 891 const Graph& graph; … … 808 919 /// _forceLabelMap constructor parameter is true then the label map 809 920 /// will be the id in the graph. In addition if the the \c 810 /// _ sortByLabelis true then the writer will write the edges sorted921 /// _forceSort is true then the writer will write the edges sorted 811 922 /// by the labels. 812 923 /// … … 834 945 /// parameter is true then the writer will write own label map if 835 946 /// the user does not give "label" named map. In addition if the 836 /// the \c _ sortByLabelis true then the writer will write the947 /// the \c _forceSort is true then the writer will write the 837 948 /// edges sorted by the labels. 838 949 template <typename NodeLabelWriter> … … 840 951 const NodeLabelWriter& _nodeLabelWriter, 841 952 const std::string& _name = std::string(), 842 bool _forceLabelMap = true, bool _ sortByLabel= true)953 bool _forceLabelMap = true, bool _forceSort = true) 843 954 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 844 sortByLabel(_sortByLabel), graph(_graph), name(_name) {955 forceSort(_forceSort), graph(_graph), name(_name) { 845 956 checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); 846 957 nodeLabelWriter.reset(new _writer_bits:: 847 958 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter)); 848 959 } 849 960 … … 883 994 writers.push_back( 884 995 make_pair(label, new _writer_bits:: 885 MapWriter<UEdge, Map, ItemWriter>(map, iw)));996 UEdgeMapWriter<Graph, Map, ItemWriter>(map, iw))); 886 997 return *this; 887 998 } … … 904 1015 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); 905 1016 checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>(); 906 writeUEdgeMap("+" + name,1017 writeUEdgeMap("+" + label, 907 1018 _writer_bits::forwardComposeMap(graph, map), iw); 908 writeUEdgeMap("-" + name,1019 writeUEdgeMap("-" + label, 909 1020 _writer_bits::backwardComposeMap(graph, map), iw); 910 1021 return *this; … … 938 1049 items.push_back(it); 939 1050 } 940 if ( sortByLabel) {1051 if (forceSort) { 941 1052 if (labelMap) { 942 labelMap->sort ByMap(items);1053 labelMap->sort(items); 943 1054 } else { 944 1055 typedef IdMap<Graph, UEdge> Map; … … 979 1090 /// 980 1091 /// Returns true if the undirected edgeset can write the labels of the 981 /// undirected edges. It is possible only if a n"label" named map was1092 /// undirected edges. It is possible only if a "label" named map was 982 1093 /// written or the \c _forceLabelMap constructor parameter was true. 983 1094 bool isLabelWriter() const { … … 988 1099 /// 989 1100 /// It writes the label of the given undirected edge. If there was written 990 /// a n"label" named map then it will write the map value belongs to the1101 /// a "label" named map then it will write the map value belongs to the 991 1102 /// undirected edge. Otherwise if the \c forceLabel parameter was true it 992 1103 /// will write its id in the graph. … … 1002 1113 /// 1003 1114 /// It writes the label of the given edge. If there was written 1004 /// a n"label" named map then it will write the map value belongs to the1115 /// a "label" named map then it will write the map value belongs to the 1005 1116 /// edge. Otherwise if the \c forceLabel parameter was true it 1006 1117 /// will write its id in the graph. If the edge is forward map … … 1008 1119 void writeLabel(std::ostream& os, const Edge& item) const { 1009 1120 if (graph.direction(item)) { 1010 os << "+ 1121 os << "+"; 1011 1122 } else { 1012 os << "- 1123 os << "-"; 1013 1124 } 1014 1125 if (forceLabelMap) { 1015 os << graph.id( item);1126 os << graph.id(static_cast<const UEdge&>(item)); 1016 1127 } else { 1017 1128 labelMap->write(os, item); … … 1019 1130 } 1020 1131 1132 /// \brief Sorts the given undirected edge vector by label. 1133 /// 1134 /// Sorts the given undirected edge vector by label. If there was 1135 /// written a "label" named map then the vector will be sorted by 1136 /// the values of this map. Otherwise if the \c forceLabel 1137 /// parameter was true it will be sorted by its id in the graph. 1138 void sortByLabel(std::vector<UEdge>& uedges) const { 1139 if (labelMap) { 1140 labelMap->sort(uedges); 1141 } else { 1142 typedef IdMap<Graph, UEdge> Map; 1143 Map map(graph); 1144 _writer_bits::ComposeLess<Map> less(map); 1145 std::sort(uedges.begin(), uedges.end(), less); 1146 } 1147 } 1148 1149 /// \brief Sorts the given edge vector by label. 1150 /// 1151 /// Sorts the given edge vector by label. If there was written a 1152 /// "label" named map then the vector will be sorted by the values 1153 /// of this map. Otherwise if the \c forceLabel parameter was true 1154 /// it will be sorted by its id in the graph. 1155 void sortByLabel(std::vector<Edge>& edges) const { 1156 if (labelMap) { 1157 labelMap->sort(graph, edges); 1158 } else { 1159 typedef IdMap<Graph, Edge> Map; 1160 Map map(graph); 1161 _writer_bits::ComposeLess<Map> less(map); 1162 std::sort(edges.begin(), edges.end(), less); 1163 } 1164 } 1165 1021 1166 private: 1022 1167 1023 1168 typedef std::vector<std::pair<std::string, _writer_bits:: 1024 MapWriterBase<UEdge>*> > MapWriters;1169 UEdgeMapWriterBase<Graph>*> > MapWriters; 1025 1170 MapWriters writers; 1026 1171 1027 _writer_bits:: MapWriterBase<UEdge>* labelMap;1172 _writer_bits::UEdgeMapWriterBase<Graph>* labelMap; 1028 1173 bool forceLabelMap; 1029 bool sortByLabel;1174 bool forceSort; 1030 1175 1031 1176 const Graph& graph; … … 1206 1351 std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter; 1207 1352 }; 1353 1208 1354 1209 1355 /// \ingroup section_io … … 1322 1468 1323 1469 /// \ingroup section_io 1470 /// \brief SectionWriter for writing extra node maps. 1471 /// 1472 /// The lemon format can store maps in the nodeset. This class let 1473 /// you make distinict section to store maps. The main purpose of 1474 /// this class is a logical separation of some maps. The other 1475 /// useful application could be to store paths in node maps. 1476 /// 1477 /// The first line of the section contains the names of the maps 1478 /// separated with white spaces. Each next line describes an item 1479 /// in the itemset, and contains in the first column the label of 1480 /// the item and then the mapped values for each map. 1481 /// 1482 /// \relates LemonWriter 1483 template <typename _Graph, typename _Traits = DefaultWriterTraits> 1484 class NodeMapWriter : public LemonWriter::SectionWriter { 1485 typedef LemonWriter::SectionWriter Parent; 1486 public: 1487 1488 typedef _Graph Graph; 1489 typedef _Traits Traits; 1490 typedef typename Graph::Node Node; 1491 1492 /// \brief Constructor. 1493 /// 1494 /// Constructor for NodeMapWriter. It creates the NodeMapWriter and 1495 /// attach it into the given LemonWriter. If the the 1496 /// \c _forceSort is true then the writer will write the edges 1497 /// sorted by the labels. 1498 template <typename _LabelWriter> 1499 NodeMapWriter(LemonWriter& _writer, const Graph& _graph, 1500 const _LabelWriter& _labelWriter, 1501 const std::string& _name = std::string(), 1502 bool _forceSort = true) 1503 : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) { 1504 checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>(); 1505 labelWriter.reset(new _writer_bits::LabelWriter<Node, 1506 _LabelWriter>(_labelWriter)); 1507 } 1508 1509 /// \brief Destructor. 1510 /// 1511 /// Destructor for NodeMapWriter. 1512 virtual ~NodeMapWriter() { 1513 typename MapWriters::iterator it; 1514 for (it = writers.begin(); it != writers.end(); ++it) { 1515 delete it->second; 1516 } 1517 } 1518 1519 private: 1520 NodeMapWriter(const NodeMapWriter&); 1521 void operator=(const NodeMapWriter&); 1522 1523 public: 1524 1525 /// \brief Add a new node map writer command for the writer. 1526 /// 1527 /// Add a new node map writer command for the writer. 1528 template <typename Map> 1529 NodeMapWriter& writeNodeMap(std::string label, const Map& map) { 1530 return writeNodeMap<typename Traits:: 1531 template Writer<typename Map::Value>, Map>(label, map); 1532 } 1533 1534 /// \brief Add a new node map writer command for the writer. 1535 /// 1536 /// Add a new node map writer command for the writer. 1537 template <typename ItemWriter, typename Map> 1538 NodeMapWriter& writeNodeMap(std::string label, const Map& map, 1539 const ItemWriter& iw = ItemWriter()) { 1540 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); 1541 checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>(); 1542 writers.push_back( 1543 make_pair(label, new _writer_bits:: 1544 MapWriter<Node, Map, ItemWriter>(map, iw))); 1545 return *this; 1546 } 1547 1548 protected: 1549 1550 /// \brief The header of the section. 1551 /// 1552 /// It gives back the header of the section. 1553 virtual std::string header() { 1554 return "@nodemaps " + name; 1555 } 1556 1557 /// \brief Writer function of the section. 1558 /// 1559 /// Write the content of the section. 1560 virtual void write(std::ostream& os) { 1561 std::vector<Node> nodes; 1562 for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { 1563 nodes.push_back(it); 1564 } 1565 if (forceSort) { 1566 labelWriter->sort(nodes); 1567 } 1568 os << '\t'; 1569 for (int i = 0; i < int(writers.size()); ++i) { 1570 os << writers[i].first << '\t'; 1571 } 1572 os << std::endl; 1573 for (typename std::vector<Node>::iterator it = nodes.begin(); 1574 it != nodes.end(); ++it) { 1575 1576 labelWriter->write(os, *it); os << '\t'; 1577 for (int i = 0; i < int(writers.size()); ++i) { 1578 writers[i].second->write(os, *it); 1579 os << '\t'; 1580 } 1581 os << std::endl; 1582 } 1583 } 1584 1585 1586 private: 1587 1588 typedef std::vector<std::pair<std::string, _writer_bits:: 1589 MapWriterBase<Node>*> > MapWriters; 1590 MapWriters writers; 1591 1592 _writer_bits::MapWriterBase<Node>* labelMap; 1593 1594 const Graph& graph; 1595 std::string name; 1596 bool forceSort; 1597 1598 std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter; 1599 }; 1600 1601 /// \ingroup section_io 1602 /// \brief SectionWriter for writing extra edge maps. 1603 /// 1604 /// The lemon format can store maps in the edgeset. This class let 1605 /// you make distinict section to store maps. The main purpose of 1606 /// this class is a logical separation of some maps. The other 1607 /// useful application could be to store paths in edge maps. 1608 /// 1609 /// The first line of the section contains the names of the maps 1610 /// separated with white spaces. Each next line describes an item 1611 /// in the itemset, and contains in the first column the label of 1612 /// the item and then the mapped values for each map. 1613 /// 1614 /// \relates LemonWriter 1615 template <typename _Graph, typename _Traits = DefaultWriterTraits> 1616 class EdgeMapWriter : public LemonWriter::SectionWriter { 1617 typedef LemonWriter::SectionWriter Parent; 1618 public: 1619 1620 typedef _Graph Graph; 1621 typedef _Traits Traits; 1622 typedef typename Graph::Edge Edge; 1623 1624 /// \brief Constructor. 1625 /// 1626 /// Constructor for EdgeMapWriter. It creates the EdgeMapWriter and 1627 /// attach it into the given LemonWriter. If the the 1628 /// \c _forceSort is true then the writer will write the edges 1629 /// sorted by the labels. 1630 template <typename _LabelWriter> 1631 EdgeMapWriter(LemonWriter& _writer, const Graph& _graph, 1632 const _LabelWriter& _labelWriter, 1633 const std::string& _name = std::string(), 1634 bool _forceSort = true) 1635 : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) { 1636 checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>(); 1637 labelWriter.reset(new _writer_bits::LabelWriter<Edge, 1638 _LabelWriter>(_labelWriter)); 1639 } 1640 1641 /// \brief Destructor. 1642 /// 1643 /// Destructor for EdgeMapWriter. 1644 virtual ~EdgeMapWriter() { 1645 typename MapWriters::iterator it; 1646 for (it = writers.begin(); it != writers.end(); ++it) { 1647 delete it->second; 1648 } 1649 } 1650 1651 private: 1652 EdgeMapWriter(const EdgeMapWriter&); 1653 void operator=(const EdgeMapWriter&); 1654 1655 public: 1656 1657 /// \brief Add a new edge map writer command for the writer. 1658 /// 1659 /// Add a new edge map writer command for the writer. 1660 template <typename Map> 1661 EdgeMapWriter& writeEdgeMap(std::string label, const Map& map) { 1662 return writeEdgeMap<typename Traits:: 1663 template Writer<typename Map::Value>, Map>(label, map); 1664 } 1665 1666 /// \brief Add a new edge map writer command for the writer. 1667 /// 1668 /// Add a new edge map writer command for the writer. 1669 template <typename ItemWriter, typename Map> 1670 EdgeMapWriter& writeEdgeMap(std::string label, const Map& map, 1671 const ItemWriter& iw = ItemWriter()) { 1672 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); 1673 checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>(); 1674 writers.push_back( 1675 make_pair(label, new _writer_bits:: 1676 MapWriter<Edge, Map, ItemWriter>(map, iw))); 1677 return *this; 1678 } 1679 1680 protected: 1681 1682 /// \brief The header of the section. 1683 /// 1684 /// It gives back the header of the section. 1685 virtual std::string header() { 1686 return "@edgemaps " + name; 1687 } 1688 1689 /// \brief Writer function of the section. 1690 /// 1691 /// Write the content of the section. 1692 virtual void write(std::ostream& os) { 1693 std::vector<Edge> edges; 1694 for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) { 1695 edges.push_back(it); 1696 } 1697 if (forceSort) { 1698 labelWriter->sort(edges); 1699 } 1700 os << '\t'; 1701 for (int i = 0; i < int(writers.size()); ++i) { 1702 os << writers[i].first << '\t'; 1703 } 1704 os << std::endl; 1705 for (typename std::vector<Edge>::iterator it = edges.begin(); 1706 it != edges.end(); ++it) { 1707 1708 labelWriter->write(os, *it); os << '\t'; 1709 for (int i = 0; i < int(writers.size()); ++i) { 1710 writers[i].second->write(os, *it); 1711 os << '\t'; 1712 } 1713 os << std::endl; 1714 } 1715 } 1716 1717 1718 private: 1719 1720 typedef std::vector<std::pair<std::string, _writer_bits:: 1721 MapWriterBase<Edge>*> > MapWriters; 1722 MapWriters writers; 1723 1724 _writer_bits::MapWriterBase<Edge>* labelMap; 1725 1726 const Graph& graph; 1727 std::string name; 1728 bool forceSort; 1729 1730 std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter; 1731 }; 1732 1733 /// \ingroup section_io 1734 /// \brief SectionWriter for writing extra undirected edge maps. 1735 /// 1736 /// The lemon format can store maps in the uedgeset. This class let 1737 /// you make distinict section to store maps. The main purpose of 1738 /// this class is a logical separation of some maps. The other 1739 /// useful application could be to store paths in undirected edge 1740 /// maps. 1741 /// 1742 /// The first line of the section contains the names of the maps 1743 /// separated with white spaces. Each next line describes an item 1744 /// in the itemset, and contains in the first column the label of 1745 /// the item and then the mapped values for each map. 1746 /// 1747 /// \relates LemonWriter 1748 template <typename _Graph, typename _Traits = DefaultWriterTraits> 1749 class UEdgeMapWriter : public LemonWriter::SectionWriter { 1750 typedef LemonWriter::SectionWriter Parent; 1751 public: 1752 1753 typedef _Graph Graph; 1754 typedef _Traits Traits; 1755 typedef typename Graph::UEdge UEdge; 1756 typedef typename Graph::Edge Edge; 1757 1758 /// \brief Constructor. 1759 /// 1760 /// Constructor for UEdgeMapWriter. It creates the UEdgeMapWriter and 1761 /// attach it into the given LemonWriter. If the the 1762 /// \c _forceSort is true then the writer will write the uedges 1763 /// sorted by the labels. 1764 template <typename _LabelWriter> 1765 UEdgeMapWriter(LemonWriter& _writer, const Graph& _graph, 1766 const _LabelWriter& _labelWriter, 1767 const std::string& _name = std::string(), 1768 bool _forceSort = true) 1769 : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) { 1770 checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>(); 1771 labelWriter.reset(new _writer_bits::LabelWriter<UEdge, 1772 _LabelWriter>(_labelWriter)); 1773 } 1774 1775 /// \brief Destructor. 1776 /// 1777 /// Destructor for UEdgeMapWriter. 1778 virtual ~UEdgeMapWriter() { 1779 typename MapWriters::iterator it; 1780 for (it = writers.begin(); it != writers.end(); ++it) { 1781 delete it->second; 1782 } 1783 } 1784 1785 private: 1786 UEdgeMapWriter(const UEdgeMapWriter&); 1787 void operator=(const UEdgeMapWriter&); 1788 1789 public: 1790 1791 /// \brief Add a new undirected edge map writer command for the writer. 1792 /// 1793 /// Add a new undirected edge map writer command for the writer. 1794 template <typename Map> 1795 UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map) { 1796 return writeUEdgeMap<typename Traits:: 1797 template Writer<typename Map::Value>, Map>(label, map); 1798 } 1799 1800 /// \brief Add a new undirected edge map writer command for the writer. 1801 /// 1802 /// Add a new undirected edge map writer command for the writer. 1803 template <typename ItemWriter, typename Map> 1804 UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map, 1805 const ItemWriter& iw = ItemWriter()) { 1806 checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>(); 1807 checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>(); 1808 writers.push_back( 1809 make_pair(label, new _writer_bits:: 1810 MapWriter<UEdge, Map, ItemWriter>(map, iw))); 1811 return *this; 1812 } 1813 1814 /// \brief Add a new directed edge map writer command for the writer. 1815 /// 1816 /// Add a new directed map writer command for the writer. 1817 template <typename Map> 1818 UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map) { 1819 return writeEdgeMap<typename Traits:: 1820 template Writer<typename Map::Value>, Map>(label, map); 1821 } 1822 1823 /// \brief Add a new directed map writer command for the writer. 1824 /// 1825 /// Add a new directed map writer command for the writer. 1826 template <typename ItemWriter, typename Map> 1827 UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map, 1828 const ItemWriter& iw = ItemWriter()) { 1829 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); 1830 checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>(); 1831 writeUEdgeMap("+" + label, 1832 _writer_bits::forwardComposeMap(graph, map), iw); 1833 writeUEdgeMap("-" + label, 1834 _writer_bits::backwardComposeMap(graph, map), iw); 1835 return *this; 1836 } 1837 1838 protected: 1839 1840 /// \brief The header of the section. 1841 /// 1842 /// It gives back the header of the section. 1843 virtual std::string header() { 1844 return "@uedgemaps " + name; 1845 } 1846 1847 /// \brief Writer function of the section. 1848 /// 1849 /// Write the content of the section. 1850 virtual void write(std::ostream& os) { 1851 std::vector<UEdge> uedges; 1852 for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) { 1853 uedges.push_back(it); 1854 } 1855 if (forceSort) { 1856 labelWriter->sort(uedges); 1857 } 1858 os << '\t'; 1859 for (int i = 0; i < int(writers.size()); ++i) { 1860 os << writers[i].first << '\t'; 1861 } 1862 os << std::endl; 1863 for (typename std::vector<UEdge>::iterator it = uedges.begin(); 1864 it != uedges.end(); ++it) { 1865 1866 labelWriter->write(os, *it); os << '\t'; 1867 for (int i = 0; i < int(writers.size()); ++i) { 1868 writers[i].second->write(os, *it); 1869 os << '\t'; 1870 } 1871 os << std::endl; 1872 } 1873 } 1874 1875 1876 private: 1877 1878 typedef std::vector<std::pair<std::string, _writer_bits:: 1879 MapWriterBase<UEdge>*> > MapWriters; 1880 MapWriters writers; 1881 1882 _writer_bits::MapWriterBase<UEdge>* labelMap; 1883 1884 const Graph& graph; 1885 std::string name; 1886 bool forceSort; 1887 1888 std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > labelWriter; 1889 }; 1890 1891 1892 /// \ingroup section_io 1324 1893 /// \brief SectionWriter for attributes. 1325 1894 /// -
lemon/path_utils.h
r2391 r2467 26 26 27 27 #include <lemon/concepts/path.h> 28 #include <lemon/lemon_reader.h> 29 #include <lemon/lemon_writer.h> 28 30 29 31 namespace lemon { … … 133 135 return graph.target(path.back()); 134 136 } 137 138 /// \brief Class which helps to iterate the nodes of a path 139 /// 140 /// In a sense, the path can be treated as a list of edges. The 141 /// lemon path type stores just this list. As a consequence it 142 /// cannot enumerate the nodes in the path and the zero length paths 143 /// cannot store the node. 144 /// 145 /// This class implements the node iterator of a path structure. To 146 /// provide this feature, the underlying graph should be given to 147 /// the constructor of the iterator. 148 template <typename Path> 149 class PathNodeIt { 150 private: 151 const typename Path::Graph *_graph; 152 typename Path::EdgeIt _it; 153 typename Path::Graph::Node _nd; 154 155 public: 156 157 typedef typename Path::Graph Graph; 158 typedef typename Graph::Node Node; 159 160 /// Default constructor 161 PathNodeIt() {} 162 /// Invalid constructor 163 PathNodeIt(Invalid) 164 : _graph(0), _it(INVALID), _nd(INVALID) {} 165 /// Constructor 166 PathNodeIt(const Graph& graph, const Path& path) 167 : _graph(&graph), _it(path) { 168 _nd = (_it != INVALID ? _graph->source(_it) : INVALID); 169 } 170 /// Constructor 171 PathNodeIt(const Graph& graph, const Path& path, const Node& src) 172 : _graph(&graph), _it(path), _nd(src) {} 173 174 ///Conversion to Graph::Node 175 operator Node() const { 176 return _nd; 177 } 178 179 /// Next node 180 PathNodeIt& operator++() { 181 if (_it == INVALID) _nd = INVALID; 182 else { 183 _nd = _graph->target(_it); 184 ++_it; 185 } 186 return *this; 187 } 188 189 /// Comparison operator 190 bool operator==(const PathNodeIt& n) const { 191 return _it == n._it && _nd == n._nd; 192 } 193 /// Comparison operator 194 bool operator!=(const PathNodeIt& n) const { 195 return _it != n._it || _nd != n._nd; 196 } 197 /// Comparison operator 198 bool operator<(const PathNodeIt& n) const { 199 return (_it < n._it && _nd != INVALID); 200 } 201 202 }; 203 204 /// \brief Item writer for paths 205 /// 206 /// This class can write paths into files. You can store paths in 207 /// distinict mapset or in attributes section. 208 /// 209 ///\code 210 /// GraphWriter<SmartGraph> gw(std::cout, g); 211 /// NodeMapWriter<SmartGraph> nmw(gw, g, gw); 212 /// 213 /// SmartGraph::NodeMap<Path<SmartGraph> > pnm(g); 214 /// for (SmartGraph::NodeIt n(g); n != INVALID; ++n) { 215 /// pnm[n] = bfs.path(n); 216 /// } 217 /// nmw.writeNodeMap("pnm", pnm, PathWriter<Path<SmartGraph> >(gw)); 218 /// 219 /// gw.run(); 220 ///\endcode 221 /// 222 /// \warning Do not use this class to write node or edge map values 223 /// into usual nodesets or edgesets. You will not be able to read 224 /// back your paths. Rather use NodeMapWriter, EdgeSetWriter or 225 /// UEdgeSetWriter to dump paths from maps to lemon file. 226 template <typename Path> 227 class PathWriter { 228 private: 229 230 typedef typename Path::Edge Edge; 231 std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter; 232 233 public: 234 235 typedef Path Value; 236 237 PathWriter(const PathWriter& pw) { 238 edgeLabelWriter.reset(pw.edgeLabelWriter->clone()); 239 } 240 241 /// \brief Constructor 242 /// 243 /// The paramter shold be an edge label writer which could 244 /// be a GraphWriter or an EdgeSetWriter. 245 template <typename EdgeLabelWriter> 246 explicit PathWriter(const EdgeLabelWriter& _edgeLabelWriter) { 247 edgeLabelWriter.reset(new _writer_bits:: 248 LabelWriter<Edge, EdgeLabelWriter>(_edgeLabelWriter)); 249 } 250 251 /// \brief Writer function 252 /// 253 /// Writes the path to the current stream. The representation 254 /// is the edge labels beetween parentheses. 255 void write(std::ostream& os, const Value& value) const { 256 if (!edgeLabelWriter->isLabelWriter()) { 257 throw DataFormatError("Cannot find edgeset or label map"); 258 } 259 os << '(' << ' '; 260 for (typename Path::EdgeIt e(value); e != INVALID; ++e) { 261 edgeLabelWriter->write(os, e); 262 os << ' '; 263 } 264 os << ')'; 265 } 266 267 }; 268 269 namespace _path_bits { 270 271 template <typename _Graph> 272 class PathProxy { 273 public: 274 typedef False RevPathTag; 275 276 typedef _Graph Graph; 277 typedef typename Graph::Edge Edge; 278 279 PathProxy(const std::vector<Edge>& edges) 280 : _edges(edges) {} 281 282 int length() const { 283 return _edges.size(); 284 } 285 286 bool empty() const { 287 return _edges.size() == 0; 288 } 289 290 class EdgeIt { 291 public: 292 EdgeIt() {} 293 EdgeIt(const PathProxy& path) 294 : _path(&path), _index(0) {} 295 296 operator const Edge() const { 297 return _path->_edges[_index]; 298 } 299 300 EdgeIt& operator++() { 301 ++_index; 302 return *this; 303 } 304 305 bool operator==(Invalid) const { 306 return int(_path->_edges.size()) == _index; 307 } 308 bool operator!=(Invalid) const { 309 return int(_path->_edges.size()) != _index; 310 } 311 312 private: 313 const PathProxy* _path; 314 int _index; 315 }; 316 317 private: 318 const std::vector<Edge>& _edges; 319 320 }; 321 322 } 323 324 /// \brief Item reader for paths 325 /// 326 /// This class can read paths from files. You can store paths in 327 /// distinict mapset or in attributes section. 328 /// 329 ///\code 330 /// GraphReader<SmartGraph> gr(std::cout, g); 331 /// NodeMapReader<SmartGraph> nmr(gr, g, gr); 332 /// 333 /// SmartGraph::NodeMap<Path<SmartGraph> > pnm(g); 334 /// nmr.readNodeMap("pnm", pnm, PathReader<Path<SmartGraph> >(gr)); 335 /// 336 /// gr.run(); 337 ///\endcode 338 /// 339 /// \warning Do not use this class to read node or edge map values 340 /// from nodesets or edgesets. The edges are not surely constructed 341 /// when the edge list should be read. Rather use NodeMapReader, 342 /// EdgeSetReader or UEdgeSetReader to read distinict map sets from file. 343 template <typename Path> 344 class PathReader { 345 private: 346 347 typedef typename Path::Edge Edge; 348 std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader; 349 350 public: 351 352 typedef Path Value; 353 354 PathReader(const PathReader& pw) { 355 edgeLabelReader.reset(pw.edgeLabelReader->clone()); 356 } 357 358 /// \brief Constructor 359 /// 360 /// The paramter shold be an edge label reader which could 361 /// be a GraphReader or an EdgeSetReader. 362 template <typename EdgeLabelReader> 363 explicit PathReader(const EdgeLabelReader& _edgeLabelReader) { 364 edgeLabelReader.reset(new _reader_bits:: 365 LabelReader<Edge, EdgeLabelReader>(_edgeLabelReader)); 366 } 367 368 369 /// \brief Reader function 370 /// 371 /// Reads the path from the current stream. The representation 372 /// is the edge labels beetween parentheses. 373 void read(std::istream& is, Value& value) const { 374 if (!edgeLabelReader->isLabelReader()) { 375 throw DataFormatError("Cannot find edgeset or label map"); 376 } 377 char c; 378 if (!(is >> c) || c != '(') 379 throw DataFormatError("PathReader format error"); 380 std::vector<typename Path::Edge> v; 381 while (is >> c && c != ')') { 382 is.putback(c); 383 Edge edge = edgeLabelReader->read(is); 384 v.push_back(edge); 385 } 386 if (!is) throw DataFormatError("PathReader format error"); 387 copyPath(value, _path_bits::PathProxy<typename Path::Edge>(v)); 388 } 389 390 }; 391 135 392 } 136 393
Note: See TracChangeset
for help on using the changeset viewer.