... | ... |
@@ -558,267 +558,321 @@ |
558 | 558 |
|
559 | 559 |
/// \brief Arc writing rule |
560 | 560 |
/// |
561 | 561 |
/// Add an arc writing rule to writer. |
562 | 562 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) { |
563 | 563 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
564 | 564 |
Converter converter(_arc_index); |
565 | 565 |
_writer_bits::ValueStorageBase* storage = |
566 | 566 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
567 | 567 |
_attributes.push_back(std::make_pair(caption, storage)); |
568 | 568 |
return *this; |
569 | 569 |
} |
570 | 570 |
|
571 | 571 |
/// \name Select section by name |
572 | 572 |
/// @{ |
573 | 573 |
|
574 | 574 |
/// \brief Set \c \@nodes section to be read |
575 | 575 |
/// |
576 | 576 |
/// Set \c \@nodes section to be read |
577 | 577 |
DigraphWriter& nodes(const std::string& caption) { |
578 | 578 |
_nodes_caption = caption; |
579 | 579 |
return *this; |
580 | 580 |
} |
581 | 581 |
|
582 | 582 |
/// \brief Set \c \@arcs section to be read |
583 | 583 |
/// |
584 | 584 |
/// Set \c \@arcs section to be read |
585 | 585 |
DigraphWriter& arcs(const std::string& caption) { |
586 | 586 |
_arcs_caption = caption; |
587 | 587 |
return *this; |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
/// \brief Set \c \@attributes section to be read |
591 | 591 |
/// |
592 | 592 |
/// Set \c \@attributes section to be read |
593 | 593 |
DigraphWriter& attributes(const std::string& caption) { |
594 | 594 |
_attributes_caption = caption; |
595 | 595 |
return *this; |
596 | 596 |
} |
597 | 597 |
|
598 | 598 |
/// \name Skipping section |
599 | 599 |
/// @{ |
600 | 600 |
|
601 | 601 |
/// \brief Skip writing the node set |
602 | 602 |
/// |
603 | 603 |
/// The \c \@nodes section will be not written to the stream. |
604 | 604 |
DigraphWriter& skipNodes() { |
605 | 605 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
606 |
_skip_nodes = true; |
|
606 | 607 |
return *this; |
607 | 608 |
} |
608 | 609 |
|
609 | 610 |
/// \brief Skip writing arc set |
610 | 611 |
/// |
611 | 612 |
/// The \c \@arcs section will be not written to the stream. |
612 | 613 |
DigraphWriter& skipArcs() { |
613 | 614 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
615 |
_skip_arcs = true; |
|
614 | 616 |
return *this; |
615 | 617 |
} |
616 | 618 |
|
617 | 619 |
/// @} |
618 | 620 |
|
619 | 621 |
private: |
620 | 622 |
|
621 | 623 |
void writeNodes() { |
622 | 624 |
_writer_bits::MapStorageBase<Node>* label = 0; |
623 | 625 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
624 | 626 |
it != _node_maps.end(); ++it) { |
625 | 627 |
if (it->first == "label") { |
626 | 628 |
label = it->second; |
627 | 629 |
break; |
628 | 630 |
} |
629 | 631 |
} |
630 | 632 |
|
631 | 633 |
*_os << "@nodes"; |
632 | 634 |
if (!_nodes_caption.empty()) { |
633 | 635 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
634 | 636 |
} |
635 | 637 |
*_os << std::endl; |
636 | 638 |
|
637 | 639 |
if (label == 0) { |
638 | 640 |
*_os << "label" << '\t'; |
639 | 641 |
} |
640 | 642 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
641 | 643 |
it != _node_maps.end(); ++it) { |
642 | 644 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
643 | 645 |
} |
644 | 646 |
*_os << std::endl; |
645 | 647 |
|
646 | 648 |
std::vector<Node> nodes; |
647 | 649 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
648 | 650 |
nodes.push_back(n); |
649 | 651 |
} |
650 | 652 |
|
651 | 653 |
if (label == 0) { |
652 | 654 |
IdMap<Digraph, Node> id_map(_digraph); |
653 | 655 |
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map); |
654 | 656 |
std::sort(nodes.begin(), nodes.end(), id_less); |
655 | 657 |
} else { |
656 | 658 |
label->sort(nodes); |
657 | 659 |
} |
658 | 660 |
|
659 | 661 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) { |
660 | 662 |
Node n = nodes[i]; |
661 | 663 |
if (label == 0) { |
662 | 664 |
std::ostringstream os; |
663 | 665 |
os << _digraph.id(n); |
664 | 666 |
_writer_bits::writeToken(*_os, os.str()); |
665 | 667 |
*_os << '\t'; |
666 | 668 |
_node_index.insert(std::make_pair(n, os.str())); |
667 | 669 |
} |
668 | 670 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
669 | 671 |
it != _node_maps.end(); ++it) { |
670 | 672 |
std::string value = it->second->get(n); |
671 | 673 |
_writer_bits::writeToken(*_os, value); |
672 | 674 |
if (it->first == "label") { |
673 | 675 |
_node_index.insert(std::make_pair(n, value)); |
674 | 676 |
} |
675 | 677 |
*_os << '\t'; |
676 | 678 |
} |
677 | 679 |
*_os << std::endl; |
678 | 680 |
} |
679 | 681 |
} |
680 | 682 |
|
683 |
void createNodeIndex() { |
|
684 |
_writer_bits::MapStorageBase<Node>* label = 0; |
|
685 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
|
686 |
it != _node_maps.end(); ++it) { |
|
687 |
if (it->first == "label") { |
|
688 |
label = it->second; |
|
689 |
break; |
|
690 |
} |
|
691 |
} |
|
692 |
|
|
693 |
if (label == 0) { |
|
694 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
|
695 |
std::ostringstream os; |
|
696 |
os << _digraph.id(n); |
|
697 |
_node_index.insert(std::make_pair(n, os.str())); |
|
698 |
} |
|
699 |
} else { |
|
700 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
|
701 |
std::string value = label->get(n); |
|
702 |
_node_index.insert(std::make_pair(n, value)); |
|
703 |
} |
|
704 |
} |
|
705 |
} |
|
706 |
|
|
681 | 707 |
void writeArcs() { |
682 | 708 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
683 | 709 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
684 | 710 |
it != _arc_maps.end(); ++it) { |
685 | 711 |
if (it->first == "label") { |
686 | 712 |
label = it->second; |
687 | 713 |
break; |
688 | 714 |
} |
689 | 715 |
} |
690 | 716 |
|
691 | 717 |
*_os << "@arcs"; |
692 | 718 |
if (!_arcs_caption.empty()) { |
693 | 719 |
_writer_bits::writeToken(*_os << ' ', _arcs_caption); |
694 | 720 |
} |
695 | 721 |
*_os << std::endl; |
696 | 722 |
|
697 | 723 |
*_os << '\t' << '\t'; |
698 | 724 |
if (label == 0) { |
699 | 725 |
*_os << "label" << '\t'; |
700 | 726 |
} |
701 | 727 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
702 | 728 |
it != _arc_maps.end(); ++it) { |
703 | 729 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
704 | 730 |
} |
705 | 731 |
*_os << std::endl; |
706 | 732 |
|
707 | 733 |
std::vector<Arc> arcs; |
708 | 734 |
for (ArcIt n(_digraph); n != INVALID; ++n) { |
709 | 735 |
arcs.push_back(n); |
710 | 736 |
} |
711 | 737 |
|
712 | 738 |
if (label == 0) { |
713 | 739 |
IdMap<Digraph, Arc> id_map(_digraph); |
714 | 740 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map); |
715 | 741 |
std::sort(arcs.begin(), arcs.end(), id_less); |
716 | 742 |
} else { |
717 | 743 |
label->sort(arcs); |
718 | 744 |
} |
719 | 745 |
|
720 | 746 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) { |
721 | 747 |
Arc a = arcs[i]; |
722 | 748 |
_writer_bits::writeToken(*_os, _node_index. |
723 | 749 |
find(_digraph.source(a))->second); |
724 | 750 |
*_os << '\t'; |
725 | 751 |
_writer_bits::writeToken(*_os, _node_index. |
726 | 752 |
find(_digraph.target(a))->second); |
727 | 753 |
*_os << '\t'; |
728 | 754 |
if (label == 0) { |
729 | 755 |
std::ostringstream os; |
730 | 756 |
os << _digraph.id(a); |
731 | 757 |
_writer_bits::writeToken(*_os, os.str()); |
732 | 758 |
*_os << '\t'; |
733 | 759 |
_arc_index.insert(std::make_pair(a, os.str())); |
734 | 760 |
} |
735 | 761 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
736 | 762 |
it != _arc_maps.end(); ++it) { |
737 | 763 |
std::string value = it->second->get(a); |
738 | 764 |
_writer_bits::writeToken(*_os, value); |
739 | 765 |
if (it->first == "label") { |
740 | 766 |
_arc_index.insert(std::make_pair(a, value)); |
741 | 767 |
} |
742 | 768 |
*_os << '\t'; |
743 | 769 |
} |
744 | 770 |
*_os << std::endl; |
745 | 771 |
} |
746 | 772 |
} |
747 | 773 |
|
774 |
void createArcIndex() { |
|
775 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
|
776 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
|
777 |
it != _arc_maps.end(); ++it) { |
|
778 |
if (it->first == "label") { |
|
779 |
label = it->second; |
|
780 |
break; |
|
781 |
} |
|
782 |
} |
|
783 |
|
|
784 |
if (label == 0) { |
|
785 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
|
786 |
std::ostringstream os; |
|
787 |
os << _digraph.id(a); |
|
788 |
_arc_index.insert(std::make_pair(a, os.str())); |
|
789 |
} |
|
790 |
} else { |
|
791 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
|
792 |
std::string value = label->get(a); |
|
793 |
_arc_index.insert(std::make_pair(a, value)); |
|
794 |
} |
|
795 |
} |
|
796 |
} |
|
797 |
|
|
748 | 798 |
void writeAttributes() { |
749 | 799 |
if (_attributes.empty()) return; |
750 | 800 |
*_os << "@attributes"; |
751 | 801 |
if (!_attributes_caption.empty()) { |
752 | 802 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
753 | 803 |
} |
754 | 804 |
*_os << std::endl; |
755 | 805 |
for (typename Attributes::iterator it = _attributes.begin(); |
756 | 806 |
it != _attributes.end(); ++it) { |
757 | 807 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
758 | 808 |
_writer_bits::writeToken(*_os, it->second->get()); |
759 | 809 |
*_os << std::endl; |
760 | 810 |
} |
761 | 811 |
} |
762 | 812 |
|
763 | 813 |
public: |
764 | 814 |
|
765 | 815 |
/// \name Execution of the writer |
766 | 816 |
/// @{ |
767 | 817 |
|
768 | 818 |
/// \brief Start the batch processing |
769 | 819 |
/// |
770 | 820 |
/// This function starts the batch processing |
771 | 821 |
void run() { |
772 | 822 |
if (!_skip_nodes) { |
773 | 823 |
writeNodes(); |
824 |
} else { |
|
825 |
createNodeIndex(); |
|
774 | 826 |
} |
775 | 827 |
if (!_skip_arcs) { |
776 | 828 |
writeArcs(); |
829 |
} else { |
|
830 |
createArcIndex(); |
|
777 | 831 |
} |
778 | 832 |
writeAttributes(); |
779 | 833 |
} |
780 | 834 |
|
781 | 835 |
/// \brief Gives back the stream of the writer |
782 | 836 |
/// |
783 | 837 |
/// Gives back the stream of the writer |
784 | 838 |
std::ostream& ostream() { |
785 | 839 |
return *_os; |
786 | 840 |
} |
787 | 841 |
|
788 | 842 |
/// @} |
789 | 843 |
}; |
790 | 844 |
|
791 | 845 |
/// \relates DigraphWriter |
792 | 846 |
template <typename Digraph> |
793 | 847 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os, Digraph& digraph) { |
794 | 848 |
DigraphWriter<Digraph> tmp(os, digraph); |
795 | 849 |
return tmp; |
796 | 850 |
} |
797 | 851 |
|
798 | 852 |
/// \relates DigraphWriter |
799 | 853 |
template <typename Digraph> |
800 | 854 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
801 | 855 |
Digraph& digraph) { |
802 | 856 |
DigraphWriter<Digraph> tmp(fn, digraph); |
803 | 857 |
return tmp; |
804 | 858 |
} |
805 | 859 |
|
806 | 860 |
/// \relates DigraphWriter |
807 | 861 |
template <typename Digraph> |
808 | 862 |
DigraphWriter<Digraph> digraphWriter(const char* fn, Digraph& digraph) { |
809 | 863 |
DigraphWriter<Digraph> tmp(fn, digraph); |
810 | 864 |
return tmp; |
811 | 865 |
} |
812 | 866 |
|
813 | 867 |
/// \ingroup lemon_io |
814 | 868 |
/// |
815 | 869 |
/// \brief LGF writer for directed graphs |
816 | 870 |
/// |
817 | 871 |
/// This utility writes an \ref lgf-format "LGF" file. |
818 | 872 |
template <typename _Graph> |
819 | 873 |
class GraphWriter { |
820 | 874 |
public: |
821 | 875 |
|
822 | 876 |
typedef _Graph Graph; |
823 | 877 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
824 | 878 |
|
... | ... |
@@ -1070,256 +1124,310 @@ |
1070 | 1124 |
|
1071 | 1125 |
/// \brief Arc writing rule |
1072 | 1126 |
/// |
1073 | 1127 |
/// Add an arc writing rule to writer. |
1074 | 1128 |
GraphWriter& arc(const std::string& caption, const Arc& arc) { |
1075 | 1129 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter; |
1076 | 1130 |
Converter converter(_graph, _edge_index); |
1077 | 1131 |
_writer_bits::ValueStorageBase* storage = |
1078 | 1132 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
1079 | 1133 |
_attributes.push_back(std::make_pair(caption, storage)); |
1080 | 1134 |
return *this; |
1081 | 1135 |
} |
1082 | 1136 |
|
1083 | 1137 |
/// \name Select section by name |
1084 | 1138 |
/// @{ |
1085 | 1139 |
|
1086 | 1140 |
/// \brief Set \c \@nodes section to be read |
1087 | 1141 |
/// |
1088 | 1142 |
/// Set \c \@nodes section to be read |
1089 | 1143 |
GraphWriter& nodes(const std::string& caption) { |
1090 | 1144 |
_nodes_caption = caption; |
1091 | 1145 |
return *this; |
1092 | 1146 |
} |
1093 | 1147 |
|
1094 | 1148 |
/// \brief Set \c \@edges section to be read |
1095 | 1149 |
/// |
1096 | 1150 |
/// Set \c \@edges section to be read |
1097 | 1151 |
GraphWriter& edges(const std::string& caption) { |
1098 | 1152 |
_edges_caption = caption; |
1099 | 1153 |
return *this; |
1100 | 1154 |
} |
1101 | 1155 |
|
1102 | 1156 |
/// \brief Set \c \@attributes section to be read |
1103 | 1157 |
/// |
1104 | 1158 |
/// Set \c \@attributes section to be read |
1105 | 1159 |
GraphWriter& attributes(const std::string& caption) { |
1106 | 1160 |
_attributes_caption = caption; |
1107 | 1161 |
return *this; |
1108 | 1162 |
} |
1109 | 1163 |
|
1110 | 1164 |
/// \name Skipping section |
1111 | 1165 |
/// @{ |
1112 | 1166 |
|
1113 | 1167 |
/// \brief Skip writing the node set |
1114 | 1168 |
/// |
1115 | 1169 |
/// The \c \@nodes section will be not written to the stream. |
1116 | 1170 |
GraphWriter& skipNodes() { |
1117 | 1171 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
1172 |
_skip_nodes = true; |
|
1118 | 1173 |
return *this; |
1119 | 1174 |
} |
1120 | 1175 |
|
1121 | 1176 |
/// \brief Skip writing edge set |
1122 | 1177 |
/// |
1123 | 1178 |
/// The \c \@edges section will be not written to the stream. |
1124 | 1179 |
GraphWriter& skipEdges() { |
1125 | 1180 |
LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member"); |
1181 |
_skip_edges = true; |
|
1126 | 1182 |
return *this; |
1127 | 1183 |
} |
1128 | 1184 |
|
1129 | 1185 |
/// @} |
1130 | 1186 |
|
1131 | 1187 |
private: |
1132 | 1188 |
|
1133 | 1189 |
void writeNodes() { |
1134 | 1190 |
_writer_bits::MapStorageBase<Node>* label = 0; |
1135 | 1191 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1136 | 1192 |
it != _node_maps.end(); ++it) { |
1137 | 1193 |
if (it->first == "label") { |
1138 | 1194 |
label = it->second; |
1139 | 1195 |
break; |
1140 | 1196 |
} |
1141 | 1197 |
} |
1142 | 1198 |
|
1143 | 1199 |
*_os << "@nodes"; |
1144 | 1200 |
if (!_nodes_caption.empty()) { |
1145 | 1201 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
1146 | 1202 |
} |
1147 | 1203 |
*_os << std::endl; |
1148 | 1204 |
|
1149 | 1205 |
if (label == 0) { |
1150 | 1206 |
*_os << "label" << '\t'; |
1151 | 1207 |
} |
1152 | 1208 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1153 | 1209 |
it != _node_maps.end(); ++it) { |
1154 | 1210 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
1155 | 1211 |
} |
1156 | 1212 |
*_os << std::endl; |
1157 | 1213 |
|
1158 | 1214 |
std::vector<Node> nodes; |
1159 | 1215 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1160 | 1216 |
nodes.push_back(n); |
1161 | 1217 |
} |
1162 | 1218 |
|
1163 | 1219 |
if (label == 0) { |
1164 | 1220 |
IdMap<Graph, Node> id_map(_graph); |
1165 | 1221 |
_writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map); |
1166 | 1222 |
std::sort(nodes.begin(), nodes.end(), id_less); |
1167 | 1223 |
} else { |
1168 | 1224 |
label->sort(nodes); |
1169 | 1225 |
} |
1170 | 1226 |
|
1171 | 1227 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) { |
1172 | 1228 |
Node n = nodes[i]; |
1173 | 1229 |
if (label == 0) { |
1174 | 1230 |
std::ostringstream os; |
1175 | 1231 |
os << _graph.id(n); |
1176 | 1232 |
_writer_bits::writeToken(*_os, os.str()); |
1177 | 1233 |
*_os << '\t'; |
1178 | 1234 |
_node_index.insert(std::make_pair(n, os.str())); |
1179 | 1235 |
} |
1180 | 1236 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1181 | 1237 |
it != _node_maps.end(); ++it) { |
1182 | 1238 |
std::string value = it->second->get(n); |
1183 | 1239 |
_writer_bits::writeToken(*_os, value); |
1184 | 1240 |
if (it->first == "label") { |
1185 | 1241 |
_node_index.insert(std::make_pair(n, value)); |
1186 | 1242 |
} |
1187 | 1243 |
*_os << '\t'; |
1188 | 1244 |
} |
1189 | 1245 |
*_os << std::endl; |
1190 | 1246 |
} |
1191 | 1247 |
} |
1192 | 1248 |
|
1249 |
void createNodeIndex() { |
|
1250 |
_writer_bits::MapStorageBase<Node>* label = 0; |
|
1251 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
|
1252 |
it != _node_maps.end(); ++it) { |
|
1253 |
if (it->first == "label") { |
|
1254 |
label = it->second; |
|
1255 |
break; |
|
1256 |
} |
|
1257 |
} |
|
1258 |
|
|
1259 |
if (label == 0) { |
|
1260 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
1261 |
std::ostringstream os; |
|
1262 |
os << _graph.id(n); |
|
1263 |
_node_index.insert(std::make_pair(n, os.str())); |
|
1264 |
} |
|
1265 |
} else { |
|
1266 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
1267 |
std::string value = label->get(n); |
|
1268 |
_node_index.insert(std::make_pair(n, value)); |
|
1269 |
} |
|
1270 |
} |
|
1271 |
} |
|
1272 |
|
|
1193 | 1273 |
void writeEdges() { |
1194 | 1274 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
1195 | 1275 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1196 | 1276 |
it != _edge_maps.end(); ++it) { |
1197 | 1277 |
if (it->first == "label") { |
1198 | 1278 |
label = it->second; |
1199 | 1279 |
break; |
1200 | 1280 |
} |
1201 | 1281 |
} |
1202 | 1282 |
|
1203 | 1283 |
*_os << "@edges"; |
1204 | 1284 |
if (!_edges_caption.empty()) { |
1205 | 1285 |
_writer_bits::writeToken(*_os << ' ', _edges_caption); |
1206 | 1286 |
} |
1207 | 1287 |
*_os << std::endl; |
1208 | 1288 |
|
1209 | 1289 |
*_os << '\t' << '\t'; |
1210 | 1290 |
if (label == 0) { |
1211 | 1291 |
*_os << "label" << '\t'; |
1212 | 1292 |
} |
1213 | 1293 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1214 | 1294 |
it != _edge_maps.end(); ++it) { |
1215 | 1295 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
1216 | 1296 |
} |
1217 | 1297 |
*_os << std::endl; |
1218 | 1298 |
|
1219 | 1299 |
std::vector<Edge> edges; |
1220 | 1300 |
for (EdgeIt n(_graph); n != INVALID; ++n) { |
1221 | 1301 |
edges.push_back(n); |
1222 | 1302 |
} |
1223 | 1303 |
|
1224 | 1304 |
if (label == 0) { |
1225 | 1305 |
IdMap<Graph, Edge> id_map(_graph); |
1226 | 1306 |
_writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map); |
1227 | 1307 |
std::sort(edges.begin(), edges.end(), id_less); |
1228 | 1308 |
} else { |
1229 | 1309 |
label->sort(edges); |
1230 | 1310 |
} |
1231 | 1311 |
|
1232 | 1312 |
for (int i = 0; i < static_cast<int>(edges.size()); ++i) { |
1233 | 1313 |
Edge e = edges[i]; |
1234 | 1314 |
_writer_bits::writeToken(*_os, _node_index. |
1235 | 1315 |
find(_graph.u(e))->second); |
1236 | 1316 |
*_os << '\t'; |
1237 | 1317 |
_writer_bits::writeToken(*_os, _node_index. |
1238 | 1318 |
find(_graph.v(e))->second); |
1239 | 1319 |
*_os << '\t'; |
1240 | 1320 |
if (label == 0) { |
1241 | 1321 |
std::ostringstream os; |
1242 | 1322 |
os << _graph.id(e); |
1243 | 1323 |
_writer_bits::writeToken(*_os, os.str()); |
1244 | 1324 |
*_os << '\t'; |
1245 | 1325 |
_edge_index.insert(std::make_pair(e, os.str())); |
1246 | 1326 |
} |
1247 | 1327 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1248 | 1328 |
it != _edge_maps.end(); ++it) { |
1249 | 1329 |
std::string value = it->second->get(e); |
1250 | 1330 |
_writer_bits::writeToken(*_os, value); |
1251 | 1331 |
if (it->first == "label") { |
1252 | 1332 |
_edge_index.insert(std::make_pair(e, value)); |
1253 | 1333 |
} |
1254 | 1334 |
*_os << '\t'; |
1255 | 1335 |
} |
1256 | 1336 |
*_os << std::endl; |
1257 | 1337 |
} |
1258 | 1338 |
} |
1259 | 1339 |
|
1340 |
void createEdgeIndex() { |
|
1341 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
|
1342 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
|
1343 |
it != _edge_maps.end(); ++it) { |
|
1344 |
if (it->first == "label") { |
|
1345 |
label = it->second; |
|
1346 |
break; |
|
1347 |
} |
|
1348 |
} |
|
1349 |
|
|
1350 |
if (label == 0) { |
|
1351 |
for (EdgeIt e(_graph); e != INVALID; ++e) { |
|
1352 |
std::ostringstream os; |
|
1353 |
os << _graph.id(e); |
|
1354 |
_edge_index.insert(std::make_pair(e, os.str())); |
|
1355 |
} |
|
1356 |
} else { |
|
1357 |
for (EdgeIt e(_graph); e != INVALID; ++e) { |
|
1358 |
std::string value = label->get(e); |
|
1359 |
_edge_index.insert(std::make_pair(e, value)); |
|
1360 |
} |
|
1361 |
} |
|
1362 |
} |
|
1363 |
|
|
1260 | 1364 |
void writeAttributes() { |
1261 | 1365 |
if (_attributes.empty()) return; |
1262 | 1366 |
*_os << "@attributes"; |
1263 | 1367 |
if (!_attributes_caption.empty()) { |
1264 | 1368 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
1265 | 1369 |
} |
1266 | 1370 |
*_os << std::endl; |
1267 | 1371 |
for (typename Attributes::iterator it = _attributes.begin(); |
1268 | 1372 |
it != _attributes.end(); ++it) { |
1269 | 1373 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
1270 | 1374 |
_writer_bits::writeToken(*_os, it->second->get()); |
1271 | 1375 |
*_os << std::endl; |
1272 | 1376 |
} |
1273 | 1377 |
} |
1274 | 1378 |
|
1275 | 1379 |
public: |
1276 | 1380 |
|
1277 | 1381 |
/// \name Execution of the writer |
1278 | 1382 |
/// @{ |
1279 | 1383 |
|
1280 | 1384 |
/// \brief Start the batch processing |
1281 | 1385 |
/// |
1282 | 1386 |
/// This function starts the batch processing |
1283 | 1387 |
void run() { |
1284 | 1388 |
if (!_skip_nodes) { |
1285 | 1389 |
writeNodes(); |
1390 |
} else { |
|
1391 |
createNodeIndex(); |
|
1286 | 1392 |
} |
1287 | 1393 |
if (!_skip_edges) { |
1288 | 1394 |
writeEdges(); |
1395 |
} else { |
|
1396 |
createEdgeIndex(); |
|
1289 | 1397 |
} |
1290 | 1398 |
writeAttributes(); |
1291 | 1399 |
} |
1292 | 1400 |
|
1293 | 1401 |
/// \brief Gives back the stream of the writer |
1294 | 1402 |
/// |
1295 | 1403 |
/// Gives back the stream of the writer |
1296 | 1404 |
std::ostream& ostream() { |
1297 | 1405 |
return *_os; |
1298 | 1406 |
} |
1299 | 1407 |
|
1300 | 1408 |
/// @} |
1301 | 1409 |
}; |
1302 | 1410 |
|
1303 | 1411 |
/// \relates GraphWriter |
1304 | 1412 |
template <typename Graph> |
1305 | 1413 |
GraphWriter<Graph> graphWriter(std::ostream& os, Graph& graph) { |
1306 | 1414 |
GraphWriter<Graph> tmp(os, graph); |
1307 | 1415 |
return tmp; |
1308 | 1416 |
} |
1309 | 1417 |
|
1310 | 1418 |
/// \relates GraphWriter |
1311 | 1419 |
template <typename Graph> |
1312 | 1420 |
GraphWriter<Graph> graphWriter(const std::string& fn, Graph& graph) { |
1313 | 1421 |
GraphWriter<Graph> tmp(fn, graph); |
1314 | 1422 |
return tmp; |
1315 | 1423 |
} |
1316 | 1424 |
|
1317 | 1425 |
/// \relates GraphWriter |
1318 | 1426 |
template <typename Graph> |
1319 | 1427 |
GraphWriter<Graph> graphWriter(const char* fn, Graph& graph) { |
1320 | 1428 |
GraphWriter<Graph> tmp(fn, graph); |
1321 | 1429 |
return tmp; |
1322 | 1430 |
} |
1323 | 1431 |
} |
1324 | 1432 |
|
1325 | 1433 |
#endif |
0 comments (0 inline)