... | ... |
@@ -836,265 +836,285 @@ |
836 | 836 |
/// label map and a functor which converts the label map values to |
837 | 837 |
/// std::string. |
838 | 838 |
template <typename Map, typename Converter> |
839 | 839 |
DigraphReader& useArcs(const Map& map, |
840 | 840 |
const Converter& converter = Converter()) { |
841 | 841 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
842 | 842 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
843 | 843 |
_use_arcs = true; |
844 | 844 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
845 | 845 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
846 | 846 |
} |
847 | 847 |
return *this; |
848 | 848 |
} |
849 | 849 |
|
850 | 850 |
/// @} |
851 | 851 |
|
852 | 852 |
private: |
853 | 853 |
|
854 | 854 |
bool readLine() { |
855 | 855 |
std::string str; |
856 | 856 |
while(++line_num, std::getline(*_is, str)) { |
857 | 857 |
line.clear(); line.str(str); |
858 | 858 |
char c; |
859 | 859 |
if (line >> std::ws >> c && c != '#') { |
860 | 860 |
line.putback(c); |
861 | 861 |
return true; |
862 | 862 |
} |
863 | 863 |
} |
864 | 864 |
return false; |
865 | 865 |
} |
866 | 866 |
|
867 | 867 |
bool readSuccess() { |
868 | 868 |
return static_cast<bool>(*_is); |
869 | 869 |
} |
870 | 870 |
|
871 | 871 |
void skipSection() { |
872 | 872 |
char c; |
873 | 873 |
while (readSuccess() && line >> c && c != '@') { |
874 | 874 |
readLine(); |
875 | 875 |
} |
876 | 876 |
line.putback(c); |
877 | 877 |
} |
878 | 878 |
|
879 | 879 |
void readNodes() { |
880 | 880 |
|
881 | 881 |
std::vector<int> map_index(_node_maps.size()); |
882 | 882 |
int map_num, label_index; |
883 | 883 |
|
884 |
if (!readLine()) |
|
885 |
throw DataFormatError("Cannot find map captions"); |
|
886 |
|
|
884 |
char c; |
|
885 |
if (!readLine() || !(line >> c) || c == '@') { |
|
886 |
if (readSuccess() && line) line.putback(c); |
|
887 |
if (!_node_maps.empty()) |
|
888 |
throw DataFormatError("Cannot find map names"); |
|
889 |
return; |
|
890 |
} |
|
891 |
line.putback(c); |
|
892 |
|
|
887 | 893 |
{ |
888 | 894 |
std::map<std::string, int> maps; |
889 | 895 |
|
890 | 896 |
std::string map; |
891 | 897 |
int index = 0; |
892 | 898 |
while (_reader_bits::readToken(line, map)) { |
893 | 899 |
if (maps.find(map) != maps.end()) { |
894 | 900 |
std::ostringstream msg; |
895 | 901 |
msg << "Multiple occurence of node map: " << map; |
896 | 902 |
throw DataFormatError(msg.str().c_str()); |
897 | 903 |
} |
898 | 904 |
maps.insert(std::make_pair(map, index)); |
899 | 905 |
++index; |
900 | 906 |
} |
901 | 907 |
|
902 | 908 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
903 | 909 |
std::map<std::string, int>::iterator jt = |
904 | 910 |
maps.find(_node_maps[i].first); |
905 | 911 |
if (jt == maps.end()) { |
906 | 912 |
std::ostringstream msg; |
907 | 913 |
msg << "Map not found in file: " << _node_maps[i].first; |
908 | 914 |
throw DataFormatError(msg.str().c_str()); |
909 | 915 |
} |
910 | 916 |
map_index[i] = jt->second; |
911 | 917 |
} |
912 | 918 |
|
913 | 919 |
{ |
914 | 920 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
915 |
if (jt == maps.end()) |
|
916 |
throw DataFormatError("Label map not found in file"); |
|
917 |
|
|
921 |
if (jt != maps.end()) { |
|
922 |
label_index = jt->second; |
|
923 |
} else { |
|
924 |
label_index = -1; |
|
925 |
} |
|
918 | 926 |
} |
919 | 927 |
map_num = maps.size(); |
920 | 928 |
} |
921 | 929 |
|
922 |
char c; |
|
923 | 930 |
while (readLine() && line >> c && c != '@') { |
924 | 931 |
line.putback(c); |
925 | 932 |
|
926 | 933 |
std::vector<std::string> tokens(map_num); |
927 | 934 |
for (int i = 0; i < map_num; ++i) { |
928 | 935 |
if (!_reader_bits::readToken(line, tokens[i])) { |
929 | 936 |
std::ostringstream msg; |
930 | 937 |
msg << "Column not found (" << i + 1 << ")"; |
931 | 938 |
throw DataFormatError(msg.str().c_str()); |
932 | 939 |
} |
933 | 940 |
} |
934 | 941 |
if (line >> std::ws >> c) |
935 | 942 |
throw DataFormatError("Extra character on the end of line"); |
936 | 943 |
|
937 | 944 |
Node n; |
938 | 945 |
if (!_use_nodes) { |
939 | 946 |
n = _digraph.addNode(); |
940 |
|
|
947 |
if (label_index != -1) |
|
948 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
|
941 | 949 |
} else { |
950 |
if (label_index == -1) |
|
951 |
throw DataFormatError("Label map not found in file"); |
|
942 | 952 |
typename std::map<std::string, Node>::iterator it = |
943 | 953 |
_node_index.find(tokens[label_index]); |
944 | 954 |
if (it == _node_index.end()) { |
945 | 955 |
std::ostringstream msg; |
946 | 956 |
msg << "Node with label not found: " << tokens[label_index]; |
947 | 957 |
throw DataFormatError(msg.str().c_str()); |
948 | 958 |
} |
949 | 959 |
n = it->second; |
950 | 960 |
} |
951 | 961 |
|
952 | 962 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
953 | 963 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
954 | 964 |
} |
955 | 965 |
|
956 | 966 |
} |
957 | 967 |
if (readSuccess()) { |
958 | 968 |
line.putback(c); |
959 | 969 |
} |
960 | 970 |
} |
961 | 971 |
|
962 | 972 |
void readArcs() { |
963 | 973 |
|
964 | 974 |
std::vector<int> map_index(_arc_maps.size()); |
965 | 975 |
int map_num, label_index; |
966 | 976 |
|
967 |
if (!readLine()) |
|
968 |
throw DataFormatError("Cannot find map captions"); |
|
977 |
char c; |
|
978 |
if (!readLine() || !(line >> c) || c == '@') { |
|
979 |
if (readSuccess() && line) line.putback(c); |
|
980 |
if (!_arc_maps.empty()) |
|
981 |
throw DataFormatError("Cannot find map names"); |
|
982 |
return; |
|
983 |
} |
|
984 |
line.putback(c); |
|
969 | 985 |
|
970 | 986 |
{ |
971 | 987 |
std::map<std::string, int> maps; |
972 | 988 |
|
973 | 989 |
std::string map; |
974 | 990 |
int index = 0; |
975 | 991 |
while (_reader_bits::readToken(line, map)) { |
976 | 992 |
if (maps.find(map) != maps.end()) { |
977 | 993 |
std::ostringstream msg; |
978 | 994 |
msg << "Multiple occurence of arc map: " << map; |
979 | 995 |
throw DataFormatError(msg.str().c_str()); |
980 | 996 |
} |
981 | 997 |
maps.insert(std::make_pair(map, index)); |
982 | 998 |
++index; |
983 | 999 |
} |
984 | 1000 |
|
985 | 1001 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
986 | 1002 |
std::map<std::string, int>::iterator jt = |
987 | 1003 |
maps.find(_arc_maps[i].first); |
988 | 1004 |
if (jt == maps.end()) { |
989 | 1005 |
std::ostringstream msg; |
990 | 1006 |
msg << "Map not found in file: " << _arc_maps[i].first; |
991 | 1007 |
throw DataFormatError(msg.str().c_str()); |
992 | 1008 |
} |
993 | 1009 |
map_index[i] = jt->second; |
994 | 1010 |
} |
995 | 1011 |
|
996 | 1012 |
{ |
997 | 1013 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
998 |
if (jt == maps.end()) |
|
999 |
throw DataFormatError("Label map not found in file"); |
|
1000 |
|
|
1014 |
if (jt != maps.end()) { |
|
1015 |
label_index = jt->second; |
|
1016 |
} else { |
|
1017 |
label_index = -1; |
|
1018 |
} |
|
1001 | 1019 |
} |
1002 | 1020 |
map_num = maps.size(); |
1003 | 1021 |
} |
1004 | 1022 |
|
1005 |
char c; |
|
1006 | 1023 |
while (readLine() && line >> c && c != '@') { |
1007 | 1024 |
line.putback(c); |
1008 | 1025 |
|
1009 | 1026 |
std::string source_token; |
1010 | 1027 |
std::string target_token; |
1011 | 1028 |
|
1012 | 1029 |
if (!_reader_bits::readToken(line, source_token)) |
1013 | 1030 |
throw DataFormatError("Source not found"); |
1014 | 1031 |
|
1015 | 1032 |
if (!_reader_bits::readToken(line, target_token)) |
1016 |
throw DataFormatError(" |
|
1033 |
throw DataFormatError("Target not found"); |
|
1017 | 1034 |
|
1018 | 1035 |
std::vector<std::string> tokens(map_num); |
1019 | 1036 |
for (int i = 0; i < map_num; ++i) { |
1020 | 1037 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1021 | 1038 |
std::ostringstream msg; |
1022 | 1039 |
msg << "Column not found (" << i + 1 << ")"; |
1023 | 1040 |
throw DataFormatError(msg.str().c_str()); |
1024 | 1041 |
} |
1025 | 1042 |
} |
1026 | 1043 |
if (line >> std::ws >> c) |
1027 | 1044 |
throw DataFormatError("Extra character on the end of line"); |
1028 | 1045 |
|
1029 | 1046 |
Arc a; |
1030 | 1047 |
if (!_use_arcs) { |
1031 | 1048 |
|
1032 | 1049 |
typename NodeIndex::iterator it; |
1033 | 1050 |
|
1034 | 1051 |
it = _node_index.find(source_token); |
1035 | 1052 |
if (it == _node_index.end()) { |
1036 | 1053 |
std::ostringstream msg; |
1037 | 1054 |
msg << "Item not found: " << source_token; |
1038 | 1055 |
throw DataFormatError(msg.str().c_str()); |
1039 | 1056 |
} |
1040 | 1057 |
Node source = it->second; |
1041 | 1058 |
|
1042 | 1059 |
it = _node_index.find(target_token); |
1043 | 1060 |
if (it == _node_index.end()) { |
1044 | 1061 |
std::ostringstream msg; |
1045 | 1062 |
msg << "Item not found: " << target_token; |
1046 | 1063 |
throw DataFormatError(msg.str().c_str()); |
1047 | 1064 |
} |
1048 | 1065 |
Node target = it->second; |
1049 | 1066 |
|
1050 | 1067 |
a = _digraph.addArc(source, target); |
1051 |
|
|
1068 |
if (label_index != -1) |
|
1069 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
|
1052 | 1070 |
} else { |
1071 |
if (label_index == -1) |
|
1072 |
throw DataFormatError("Label map not found in file"); |
|
1053 | 1073 |
typename std::map<std::string, Arc>::iterator it = |
1054 | 1074 |
_arc_index.find(tokens[label_index]); |
1055 | 1075 |
if (it == _arc_index.end()) { |
1056 | 1076 |
std::ostringstream msg; |
1057 | 1077 |
msg << "Arc with label not found: " << tokens[label_index]; |
1058 | 1078 |
throw DataFormatError(msg.str().c_str()); |
1059 | 1079 |
} |
1060 | 1080 |
a = it->second; |
1061 | 1081 |
} |
1062 | 1082 |
|
1063 | 1083 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
1064 | 1084 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
1065 | 1085 |
} |
1066 | 1086 |
|
1067 | 1087 |
} |
1068 | 1088 |
if (readSuccess()) { |
1069 | 1089 |
line.putback(c); |
1070 | 1090 |
} |
1071 | 1091 |
} |
1072 | 1092 |
|
1073 | 1093 |
void readAttributes() { |
1074 | 1094 |
|
1075 | 1095 |
std::set<std::string> read_attr; |
1076 | 1096 |
|
1077 | 1097 |
char c; |
1078 | 1098 |
while (readLine() && line >> c && c != '@') { |
1079 | 1099 |
line.putback(c); |
1080 | 1100 |
|
1081 | 1101 |
std::string attr, token; |
1082 | 1102 |
if (!_reader_bits::readToken(line, attr)) |
1083 | 1103 |
throw DataFormatError("Attribute name not found"); |
1084 | 1104 |
if (!_reader_bits::readToken(line, token)) |
1085 | 1105 |
throw DataFormatError("Attribute value not found"); |
1086 | 1106 |
if (line >> c) |
1087 | 1107 |
throw DataFormatError("Extra character on the end of line"); |
1088 | 1108 |
|
1089 | 1109 |
{ |
1090 | 1110 |
std::set<std::string>::iterator it = read_attr.find(attr); |
1091 | 1111 |
if (it != read_attr.end()) { |
1092 | 1112 |
std::ostringstream msg; |
1093 | 1113 |
msg << "Multiple occurence of attribute " << attr; |
1094 | 1114 |
throw DataFormatError(msg.str().c_str()); |
1095 | 1115 |
} |
1096 | 1116 |
read_attr.insert(attr); |
1097 | 1117 |
} |
1098 | 1118 |
|
1099 | 1119 |
{ |
1100 | 1120 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
... | ... |
@@ -1678,265 +1698,285 @@ |
1678 | 1698 |
/// label map and a functor which converts the label map values to |
1679 | 1699 |
/// std::string. |
1680 | 1700 |
template <typename Map, typename Converter> |
1681 | 1701 |
GraphReader& useEdges(const Map& map, |
1682 | 1702 |
const Converter& converter = Converter()) { |
1683 | 1703 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
1684 | 1704 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
1685 | 1705 |
_use_edges = true; |
1686 | 1706 |
for (EdgeIt a(_graph); a != INVALID; ++a) { |
1687 | 1707 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
1688 | 1708 |
} |
1689 | 1709 |
return *this; |
1690 | 1710 |
} |
1691 | 1711 |
|
1692 | 1712 |
/// @} |
1693 | 1713 |
|
1694 | 1714 |
private: |
1695 | 1715 |
|
1696 | 1716 |
bool readLine() { |
1697 | 1717 |
std::string str; |
1698 | 1718 |
while(++line_num, std::getline(*_is, str)) { |
1699 | 1719 |
line.clear(); line.str(str); |
1700 | 1720 |
char c; |
1701 | 1721 |
if (line >> std::ws >> c && c != '#') { |
1702 | 1722 |
line.putback(c); |
1703 | 1723 |
return true; |
1704 | 1724 |
} |
1705 | 1725 |
} |
1706 | 1726 |
return false; |
1707 | 1727 |
} |
1708 | 1728 |
|
1709 | 1729 |
bool readSuccess() { |
1710 | 1730 |
return static_cast<bool>(*_is); |
1711 | 1731 |
} |
1712 | 1732 |
|
1713 | 1733 |
void skipSection() { |
1714 | 1734 |
char c; |
1715 | 1735 |
while (readSuccess() && line >> c && c != '@') { |
1716 | 1736 |
readLine(); |
1717 | 1737 |
} |
1718 | 1738 |
line.putback(c); |
1719 | 1739 |
} |
1720 | 1740 |
|
1721 | 1741 |
void readNodes() { |
1722 | 1742 |
|
1723 | 1743 |
std::vector<int> map_index(_node_maps.size()); |
1724 | 1744 |
int map_num, label_index; |
1725 | 1745 |
|
1726 |
if (!readLine()) |
|
1727 |
throw DataFormatError("Cannot find map captions"); |
|
1746 |
char c; |
|
1747 |
if (!readLine() || !(line >> c) || c == '@') { |
|
1748 |
if (readSuccess() && line) line.putback(c); |
|
1749 |
if (!_node_maps.empty()) |
|
1750 |
throw DataFormatError("Cannot find map names"); |
|
1751 |
return; |
|
1752 |
} |
|
1753 |
line.putback(c); |
|
1728 | 1754 |
|
1729 | 1755 |
{ |
1730 | 1756 |
std::map<std::string, int> maps; |
1731 | 1757 |
|
1732 | 1758 |
std::string map; |
1733 | 1759 |
int index = 0; |
1734 | 1760 |
while (_reader_bits::readToken(line, map)) { |
1735 | 1761 |
if (maps.find(map) != maps.end()) { |
1736 | 1762 |
std::ostringstream msg; |
1737 | 1763 |
msg << "Multiple occurence of node map: " << map; |
1738 | 1764 |
throw DataFormatError(msg.str().c_str()); |
1739 | 1765 |
} |
1740 | 1766 |
maps.insert(std::make_pair(map, index)); |
1741 | 1767 |
++index; |
1742 | 1768 |
} |
1743 | 1769 |
|
1744 | 1770 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
1745 | 1771 |
std::map<std::string, int>::iterator jt = |
1746 | 1772 |
maps.find(_node_maps[i].first); |
1747 | 1773 |
if (jt == maps.end()) { |
1748 | 1774 |
std::ostringstream msg; |
1749 | 1775 |
msg << "Map not found in file: " << _node_maps[i].first; |
1750 | 1776 |
throw DataFormatError(msg.str().c_str()); |
1751 | 1777 |
} |
1752 | 1778 |
map_index[i] = jt->second; |
1753 | 1779 |
} |
1754 | 1780 |
|
1755 | 1781 |
{ |
1756 | 1782 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
1757 |
if (jt == maps.end()) |
|
1758 |
throw DataFormatError("Label map not found in file"); |
|
1759 |
|
|
1783 |
if (jt != maps.end()) { |
|
1784 |
label_index = jt->second; |
|
1785 |
} else { |
|
1786 |
label_index = -1; |
|
1787 |
} |
|
1760 | 1788 |
} |
1761 | 1789 |
map_num = maps.size(); |
1762 | 1790 |
} |
1763 | 1791 |
|
1764 |
char c; |
|
1765 | 1792 |
while (readLine() && line >> c && c != '@') { |
1766 | 1793 |
line.putback(c); |
1767 | 1794 |
|
1768 | 1795 |
std::vector<std::string> tokens(map_num); |
1769 | 1796 |
for (int i = 0; i < map_num; ++i) { |
1770 | 1797 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1771 | 1798 |
std::ostringstream msg; |
1772 | 1799 |
msg << "Column not found (" << i + 1 << ")"; |
1773 | 1800 |
throw DataFormatError(msg.str().c_str()); |
1774 | 1801 |
} |
1775 | 1802 |
} |
1776 | 1803 |
if (line >> std::ws >> c) |
1777 | 1804 |
throw DataFormatError("Extra character on the end of line"); |
1778 | 1805 |
|
1779 | 1806 |
Node n; |
1780 | 1807 |
if (!_use_nodes) { |
1781 | 1808 |
n = _graph.addNode(); |
1782 |
|
|
1809 |
if (label_index != -1) |
|
1810 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
|
1783 | 1811 |
} else { |
1812 |
if (label_index == -1) |
|
1813 |
throw DataFormatError("Label map not found in file"); |
|
1784 | 1814 |
typename std::map<std::string, Node>::iterator it = |
1785 | 1815 |
_node_index.find(tokens[label_index]); |
1786 | 1816 |
if (it == _node_index.end()) { |
1787 | 1817 |
std::ostringstream msg; |
1788 | 1818 |
msg << "Node with label not found: " << tokens[label_index]; |
1789 | 1819 |
throw DataFormatError(msg.str().c_str()); |
1790 | 1820 |
} |
1791 | 1821 |
n = it->second; |
1792 | 1822 |
} |
1793 | 1823 |
|
1794 | 1824 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
1795 | 1825 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
1796 | 1826 |
} |
1797 | 1827 |
|
1798 | 1828 |
} |
1799 | 1829 |
if (readSuccess()) { |
1800 | 1830 |
line.putback(c); |
1801 | 1831 |
} |
1802 | 1832 |
} |
1803 | 1833 |
|
1804 | 1834 |
void readEdges() { |
1805 | 1835 |
|
1806 | 1836 |
std::vector<int> map_index(_edge_maps.size()); |
1807 | 1837 |
int map_num, label_index; |
1808 | 1838 |
|
1809 |
if (!readLine()) |
|
1810 |
throw DataFormatError("Cannot find map captions"); |
|
1839 |
char c; |
|
1840 |
if (!readLine() || !(line >> c) || c == '@') { |
|
1841 |
if (readSuccess() && line) line.putback(c); |
|
1842 |
if (!_edge_maps.empty()) |
|
1843 |
throw DataFormatError("Cannot find map names"); |
|
1844 |
return; |
|
1845 |
} |
|
1846 |
line.putback(c); |
|
1811 | 1847 |
|
1812 | 1848 |
{ |
1813 | 1849 |
std::map<std::string, int> maps; |
1814 | 1850 |
|
1815 | 1851 |
std::string map; |
1816 | 1852 |
int index = 0; |
1817 | 1853 |
while (_reader_bits::readToken(line, map)) { |
1818 | 1854 |
if (maps.find(map) != maps.end()) { |
1819 | 1855 |
std::ostringstream msg; |
1820 | 1856 |
msg << "Multiple occurence of edge map: " << map; |
1821 | 1857 |
throw DataFormatError(msg.str().c_str()); |
1822 | 1858 |
} |
1823 | 1859 |
maps.insert(std::make_pair(map, index)); |
1824 | 1860 |
++index; |
1825 | 1861 |
} |
1826 | 1862 |
|
1827 | 1863 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
1828 | 1864 |
std::map<std::string, int>::iterator jt = |
1829 | 1865 |
maps.find(_edge_maps[i].first); |
1830 | 1866 |
if (jt == maps.end()) { |
1831 | 1867 |
std::ostringstream msg; |
1832 | 1868 |
msg << "Map not found in file: " << _edge_maps[i].first; |
1833 | 1869 |
throw DataFormatError(msg.str().c_str()); |
1834 | 1870 |
} |
1835 | 1871 |
map_index[i] = jt->second; |
1836 | 1872 |
} |
1837 | 1873 |
|
1838 | 1874 |
{ |
1839 | 1875 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
1840 |
if (jt == maps.end()) |
|
1841 |
throw DataFormatError("Label map not found in file"); |
|
1842 |
|
|
1876 |
if (jt != maps.end()) { |
|
1877 |
label_index = jt->second; |
|
1878 |
} else { |
|
1879 |
label_index = -1; |
|
1880 |
} |
|
1843 | 1881 |
} |
1844 | 1882 |
map_num = maps.size(); |
1845 | 1883 |
} |
1846 | 1884 |
|
1847 |
char c; |
|
1848 | 1885 |
while (readLine() && line >> c && c != '@') { |
1849 | 1886 |
line.putback(c); |
1850 | 1887 |
|
1851 | 1888 |
std::string source_token; |
1852 | 1889 |
std::string target_token; |
1853 | 1890 |
|
1854 | 1891 |
if (!_reader_bits::readToken(line, source_token)) |
1855 |
throw DataFormatError(" |
|
1892 |
throw DataFormatError("Node u not found"); |
|
1856 | 1893 |
|
1857 | 1894 |
if (!_reader_bits::readToken(line, target_token)) |
1858 |
throw DataFormatError(" |
|
1895 |
throw DataFormatError("Node v not found"); |
|
1859 | 1896 |
|
1860 | 1897 |
std::vector<std::string> tokens(map_num); |
1861 | 1898 |
for (int i = 0; i < map_num; ++i) { |
1862 | 1899 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1863 | 1900 |
std::ostringstream msg; |
1864 | 1901 |
msg << "Column not found (" << i + 1 << ")"; |
1865 | 1902 |
throw DataFormatError(msg.str().c_str()); |
1866 | 1903 |
} |
1867 | 1904 |
} |
1868 | 1905 |
if (line >> std::ws >> c) |
1869 | 1906 |
throw DataFormatError("Extra character on the end of line"); |
1870 | 1907 |
|
1871 | 1908 |
Edge e; |
1872 | 1909 |
if (!_use_edges) { |
1873 | 1910 |
|
1874 | 1911 |
typename NodeIndex::iterator it; |
1875 | 1912 |
|
1876 | 1913 |
it = _node_index.find(source_token); |
1877 | 1914 |
if (it == _node_index.end()) { |
1878 | 1915 |
std::ostringstream msg; |
1879 | 1916 |
msg << "Item not found: " << source_token; |
1880 | 1917 |
throw DataFormatError(msg.str().c_str()); |
1881 | 1918 |
} |
1882 | 1919 |
Node source = it->second; |
1883 | 1920 |
|
1884 | 1921 |
it = _node_index.find(target_token); |
1885 | 1922 |
if (it == _node_index.end()) { |
1886 | 1923 |
std::ostringstream msg; |
1887 | 1924 |
msg << "Item not found: " << target_token; |
1888 | 1925 |
throw DataFormatError(msg.str().c_str()); |
1889 | 1926 |
} |
1890 | 1927 |
Node target = it->second; |
1891 | 1928 |
|
1892 | 1929 |
e = _graph.addEdge(source, target); |
1893 |
|
|
1930 |
if (label_index != -1) |
|
1931 |
_edge_index.insert(std::make_pair(tokens[label_index], e)); |
|
1894 | 1932 |
} else { |
1933 |
if (label_index == -1) |
|
1934 |
throw DataFormatError("Label map not found in file"); |
|
1895 | 1935 |
typename std::map<std::string, Edge>::iterator it = |
1896 | 1936 |
_edge_index.find(tokens[label_index]); |
1897 | 1937 |
if (it == _edge_index.end()) { |
1898 | 1938 |
std::ostringstream msg; |
1899 | 1939 |
msg << "Edge with label not found: " << tokens[label_index]; |
1900 | 1940 |
throw DataFormatError(msg.str().c_str()); |
1901 | 1941 |
} |
1902 | 1942 |
e = it->second; |
1903 | 1943 |
} |
1904 | 1944 |
|
1905 | 1945 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
1906 | 1946 |
_edge_maps[i].second->set(e, tokens[map_index[i]]); |
1907 | 1947 |
} |
1908 | 1948 |
|
1909 | 1949 |
} |
1910 | 1950 |
if (readSuccess()) { |
1911 | 1951 |
line.putback(c); |
1912 | 1952 |
} |
1913 | 1953 |
} |
1914 | 1954 |
|
1915 | 1955 |
void readAttributes() { |
1916 | 1956 |
|
1917 | 1957 |
std::set<std::string> read_attr; |
1918 | 1958 |
|
1919 | 1959 |
char c; |
1920 | 1960 |
while (readLine() && line >> c && c != '@') { |
1921 | 1961 |
line.putback(c); |
1922 | 1962 |
|
1923 | 1963 |
std::string attr, token; |
1924 | 1964 |
if (!_reader_bits::readToken(line, attr)) |
1925 | 1965 |
throw DataFormatError("Attribute name not found"); |
1926 | 1966 |
if (!_reader_bits::readToken(line, token)) |
1927 | 1967 |
throw DataFormatError("Attribute value not found"); |
1928 | 1968 |
if (line >> c) |
1929 | 1969 |
throw DataFormatError("Extra character on the end of line"); |
1930 | 1970 |
|
1931 | 1971 |
{ |
1932 | 1972 |
std::set<std::string>::iterator it = read_attr.find(attr); |
1933 | 1973 |
if (it != read_attr.end()) { |
1934 | 1974 |
std::ostringstream msg; |
1935 | 1975 |
msg << "Multiple occurence of attribute " << attr; |
1936 | 1976 |
throw DataFormatError(msg.str().c_str()); |
1937 | 1977 |
} |
1938 | 1978 |
read_attr.insert(attr); |
1939 | 1979 |
} |
1940 | 1980 |
|
1941 | 1981 |
{ |
1942 | 1982 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
... | ... |
@@ -2280,98 +2320,102 @@ |
2280 | 2320 |
|
2281 | 2321 |
/// \name Extra sections |
2282 | 2322 |
/// @{ |
2283 | 2323 |
|
2284 | 2324 |
/// \brief Gives back the number of extra sections in the file. |
2285 | 2325 |
/// |
2286 | 2326 |
/// Gives back the number of extra sections in the file. |
2287 | 2327 |
int extraSectionNum() const { |
2288 | 2328 |
return _extra_sections.size(); |
2289 | 2329 |
} |
2290 | 2330 |
|
2291 | 2331 |
/// \brief Returns the extra section type at the given position. |
2292 | 2332 |
/// |
2293 | 2333 |
/// Returns the section type at the given position. |
2294 | 2334 |
const std::string& extraSection(int i) const { |
2295 | 2335 |
return _extra_sections[i]; |
2296 | 2336 |
} |
2297 | 2337 |
|
2298 | 2338 |
/// @} |
2299 | 2339 |
|
2300 | 2340 |
private: |
2301 | 2341 |
|
2302 | 2342 |
bool readLine() { |
2303 | 2343 |
std::string str; |
2304 | 2344 |
while(++line_num, std::getline(*_is, str)) { |
2305 | 2345 |
line.clear(); line.str(str); |
2306 | 2346 |
char c; |
2307 | 2347 |
if (line >> std::ws >> c && c != '#') { |
2308 | 2348 |
line.putback(c); |
2309 | 2349 |
return true; |
2310 | 2350 |
} |
2311 | 2351 |
} |
2312 | 2352 |
return false; |
2313 | 2353 |
} |
2314 | 2354 |
|
2315 | 2355 |
bool readSuccess() { |
2316 | 2356 |
return static_cast<bool>(*_is); |
2317 | 2357 |
} |
2318 | 2358 |
|
2319 | 2359 |
void skipSection() { |
2320 | 2360 |
char c; |
2321 | 2361 |
while (readSuccess() && line >> c && c != '@') { |
2322 | 2362 |
readLine(); |
2323 | 2363 |
} |
2324 | 2364 |
line.putback(c); |
2325 | 2365 |
} |
2326 | 2366 |
|
2327 | 2367 |
void readMaps(std::vector<std::string>& maps) { |
2328 |
if (!readLine()) |
|
2329 |
throw DataFormatError("Cannot find map captions"); |
|
2368 |
char c; |
|
2369 |
if (!readLine() || !(line >> c) || c == '@') { |
|
2370 |
if (readSuccess() && line) line.putback(c); |
|
2371 |
return; |
|
2372 |
} |
|
2373 |
line.putback(c); |
|
2330 | 2374 |
std::string map; |
2331 | 2375 |
while (_reader_bits::readToken(line, map)) { |
2332 | 2376 |
maps.push_back(map); |
2333 | 2377 |
} |
2334 | 2378 |
} |
2335 | 2379 |
|
2336 | 2380 |
void readAttributes(std::vector<std::string>& attrs) { |
2337 | 2381 |
readLine(); |
2338 | 2382 |
char c; |
2339 | 2383 |
while (readSuccess() && line >> c && c != '@') { |
2340 | 2384 |
line.putback(c); |
2341 | 2385 |
std::string attr; |
2342 | 2386 |
_reader_bits::readToken(line, attr); |
2343 | 2387 |
attrs.push_back(attr); |
2344 | 2388 |
readLine(); |
2345 | 2389 |
} |
2346 | 2390 |
line.putback(c); |
2347 | 2391 |
} |
2348 | 2392 |
|
2349 | 2393 |
public: |
2350 | 2394 |
|
2351 | 2395 |
/// \name Execution of the contents reader |
2352 | 2396 |
/// @{ |
2353 | 2397 |
|
2354 | 2398 |
/// \brief Start the reading |
2355 | 2399 |
/// |
2356 | 2400 |
/// This function starts the reading |
2357 | 2401 |
void run() { |
2358 | 2402 |
|
2359 | 2403 |
readLine(); |
2360 | 2404 |
skipSection(); |
2361 | 2405 |
|
2362 | 2406 |
while (readSuccess()) { |
2363 | 2407 |
|
2364 | 2408 |
char c; |
2365 | 2409 |
line >> c; |
2366 | 2410 |
|
2367 | 2411 |
std::string section, caption; |
2368 | 2412 |
_reader_bits::readToken(line, section); |
2369 | 2413 |
_reader_bits::readToken(line, caption); |
2370 | 2414 |
|
2371 | 2415 |
if (section == "nodes") { |
2372 | 2416 |
_node_sections.push_back(caption); |
2373 | 2417 |
_node_maps.push_back(std::vector<std::string>()); |
2374 | 2418 |
readMaps(_node_maps.back()); |
2375 | 2419 |
readLine(); skipSection(); |
2376 | 2420 |
} else if (section == "arcs" || section == "edges") { |
2377 | 2421 |
_edge_sections.push_back(caption); |
0 comments (0 inline)