Changeset 2101:439b7f21ccc4 in lemon-0.x
- Timestamp:
- 06/19/06 15:44:06 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2787
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lemon_writer.h
r2084 r2101 48 48 namespace _writer_bits { 49 49 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 50 74 template <typename Item> 51 75 class ItemLabelWriter { … … 176 200 177 201 virtual void write(std::ostream& os, const Item& item) const = 0; 202 virtual void sortByMap(std::vector<Item>&) const = 0; 178 203 }; 179 204 … … 198 223 Value value = map[item]; 199 224 writer.write(os, value); 225 } 226 227 virtual void sortByMap(std::vector<Item>& items) const { 228 ComposeLess<Map> less(map); 229 std::sort(items.begin(), items.end(), less); 200 230 } 201 231 … … 395 425 /// \c writeLabel() member will be called with a node it will write it's 396 426 /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 397 /// then the label map will be the id in the graph. 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. 398 430 /// 399 431 /// \relates LemonWriter … … 412 444 /// attach it into the given LemonWriter. If the \c _forceLabelMap 413 445 /// parameter is true then the writer will write own label map when 414 /// the user does not give "label" named map. 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. 415 449 NodeSetWriter(LemonWriter& _writer, const Graph& _graph, 416 450 const std::string& _name = std::string(), 417 bool _forceLabelMap = true )451 bool _forceLabelMap = true, bool _sortByLabel = true) 418 452 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 419 graph(_graph), name(_name) {}453 sortByLabel(_sortByLabel), graph(_graph), name(_name) {} 420 454 421 455 /// \brief Destructor. … … 478 512 } 479 513 } 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 } 480 528 if (forceLabelMap) { 481 529 os << "label\t"; … … 485 533 } 486 534 os << std::endl; 487 for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { 535 for (typename std::vector<Node>::iterator it = items.begin(); 536 it != items.end(); ++it) { 488 537 if (forceLabelMap) { 489 os << graph.id( it) << '\t';538 os << graph.id(*it) << '\t'; 490 539 } 491 540 for (int i = 0; i < (int)writers.size(); ++i) { 492 writers[i].second->write(os, it);541 writers[i].second->write(os, *it); 493 542 os << '\t'; 494 543 } … … 530 579 _writer_bits::MapWriterBase<Node>* labelMap; 531 580 bool forceLabelMap; 581 bool sortByLabel; 532 582 533 583 const Graph& graph; … … 552 602 /// \c writeLabel() member will be called with an edge it will write it's 553 603 /// label. Otherwise if the \c _forceLabelMap constructor parameter is true 554 /// then the label map will be the id in the graph. 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. 555 607 /// 556 608 /// The edgeset writer needs a node label writer to identify which nodes … … 571 623 /// \brief Constructor. 572 624 /// 573 /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter and 574 /// attach it into the given LemonWriter. It will write node labels by 575 /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 576 /// then the writer will write own label map if the user does not give 577 /// "label" named map. 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. 578 632 template <typename NodeLabelWriter> 579 633 EdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 580 634 const NodeLabelWriter& _nodeLabelWriter, 581 635 const std::string& _name = std::string(), 582 bool _forceLabelMap = true )636 bool _forceLabelMap = true, bool _sortByLabel = true) 583 637 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 584 graph(_graph), name(_name) {638 sortByLabel(_sortByLabel), graph(_graph), name(_name) { 585 639 checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); 586 640 nodeLabelWriter.reset(new _writer_bits:: … … 650 704 } 651 705 } 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 } 652 720 os << "\t\t"; 653 721 if (forceLabelMap) { … … 658 726 } 659 727 os << std::endl; 660 for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) { 661 nodeLabelWriter->write(os, graph.source(it)); 728 for (typename std::vector<Edge>::iterator it = items.begin(); 729 it != items.end(); ++it) { 730 nodeLabelWriter->write(os, graph.source(*it)); 662 731 os << '\t'; 663 nodeLabelWriter->write(os, graph.target( it));732 nodeLabelWriter->write(os, graph.target(*it)); 664 733 os << '\t'; 665 734 if (forceLabelMap) { 666 os << graph.id( it) << '\t';735 os << graph.id(*it) << '\t'; 667 736 } 668 737 for (int i = 0; i < (int)writers.size(); ++i) { 669 writers[i].second->write(os, it);738 writers[i].second->write(os, *it); 670 739 os << '\t'; 671 740 } … … 707 776 _writer_bits::MapWriterBase<Edge>* labelMap; 708 777 bool forceLabelMap; 778 bool sortByLabel; 709 779 710 780 const Graph& graph; … … 732 802 /// difference. 733 803 /// 734 /// If the edgeset contains an \c "label" named map then it will be regarded 735 /// as label map. This map should contain only unique values and when the 736 /// \c writeLabel() member will be called with an undirected edge it will 737 /// write it's label. Otherwise if the \c _forceLabelMap constructor 738 /// parameter is true then the label map will be the id in the graph. 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. 739 812 /// 740 813 /// The undirected edgeset writer needs a node label writer to identify … … 757 830 /// 758 831 /// Constructor for UEdgeSetWriter. It creates the UEdgeSetWriter 759 /// and attach it into the given LemonWriter. It will write node labels by 760 /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 761 /// then the writer will write own label map if the user does not give 762 /// "label" named map. 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. 763 838 template <typename NodeLabelWriter> 764 839 UEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 765 840 const NodeLabelWriter& _nodeLabelWriter, 766 841 const std::string& _name = std::string(), 767 bool _forceLabelMap = true )842 bool _forceLabelMap = true, bool _sortByLabel = true) 768 843 : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap), 769 graph(_graph), name(_name) {844 sortByLabel(_sortByLabel), graph(_graph), name(_name) { 770 845 checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>(); 771 846 nodeLabelWriter.reset(new _writer_bits:: … … 859 934 } 860 935 } 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 } 861 950 os << "\t\t"; 862 951 if (forceLabelMap) { … … 867 956 } 868 957 os << std::endl; 869 for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) { 870 nodeLabelWriter->write(os, graph.source(it)); 958 for (typename std::vector<Edge>::iterator it = items.begin(); 959 it != items.end(); ++it) { 960 nodeLabelWriter->write(os, graph.source(*it)); 871 961 os << '\t'; 872 nodeLabelWriter->write(os, graph.target( it));962 nodeLabelWriter->write(os, graph.target(*it)); 873 963 os << '\t'; 874 964 if (forceLabelMap) { 875 os << graph.id( it) << '\t';965 os << graph.id(*it) << '\t'; 876 966 } 877 967 for (int i = 0; i < (int)writers.size(); ++i) { 878 writers[i].second->write(os, it);968 writers[i].second->write(os, *it); 879 969 os << '\t'; 880 970 } … … 937 1027 _writer_bits::MapWriterBase<UEdge>* labelMap; 938 1028 bool forceLabelMap; 1029 bool sortByLabel; 939 1030 940 1031 const Graph& graph;
Note: See TracChangeset
for help on using the changeset viewer.