lemon/lgf_reader.h
changeset 187 84c2a2e5cfee
parent 182 9c6dfb5141d3
child 188 70694e6bdcac
equal deleted inserted replaced
14:ba2505b47210 15:f55be02578b0
   879     void readNodes() {
   879     void readNodes() {
   880 
   880 
   881       std::vector<int> map_index(_node_maps.size());
   881       std::vector<int> map_index(_node_maps.size());
   882       int map_num, label_index;
   882       int map_num, label_index;
   883 
   883 
   884       if (!readLine()) 
   884       char c;
   885 	throw DataFormatError("Cannot find map captions");
   885       if (!readLine() || !(line >> c) || c == '@') {
   886       
   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 	std::map<std::string, int> maps;
   894 	std::map<std::string, int> maps;
   889 	
   895 	
   890 	std::string map;
   896 	std::string map;
   891 	int index = 0;
   897 	int index = 0;
   910 	  map_index[i] = jt->second;
   916 	  map_index[i] = jt->second;
   911 	}
   917 	}
   912 
   918 
   913 	{
   919 	{
   914 	  std::map<std::string, int>::iterator jt = maps.find("label");
   920 	  std::map<std::string, int>::iterator jt = maps.find("label");
   915 	  if (jt == maps.end())
   921 	  if (jt != maps.end()) {
   916 	    throw DataFormatError("Label map not found in file");
   922 	    label_index = jt->second;
   917 	  label_index = jt->second;
   923 	  } else {
       
   924 	    label_index = -1;
       
   925 	  }
   918 	}
   926 	}
   919 	map_num = maps.size();
   927 	map_num = maps.size();
   920       }
   928       }
   921 
   929 
   922       char c;
       
   923       while (readLine() && line >> c && c != '@') {
   930       while (readLine() && line >> c && c != '@') {
   924 	line.putback(c);
   931 	line.putback(c);
   925 
   932 
   926 	std::vector<std::string> tokens(map_num);
   933 	std::vector<std::string> tokens(map_num);
   927 	for (int i = 0; i < map_num; ++i) {
   934 	for (int i = 0; i < map_num; ++i) {
   935 	  throw DataFormatError("Extra character on the end of line");
   942 	  throw DataFormatError("Extra character on the end of line");
   936 	
   943 	
   937 	Node n;
   944 	Node n;
   938 	if (!_use_nodes) {
   945 	if (!_use_nodes) {
   939 	  n = _digraph.addNode();
   946 	  n = _digraph.addNode();
   940 	  _node_index.insert(std::make_pair(tokens[label_index], n));
   947 	  if (label_index != -1)
       
   948 	    _node_index.insert(std::make_pair(tokens[label_index], n));
   941 	} else {
   949 	} else {
       
   950 	  if (label_index == -1) 
       
   951 	    throw DataFormatError("Label map not found in file");
   942 	  typename std::map<std::string, Node>::iterator it =
   952 	  typename std::map<std::string, Node>::iterator it =
   943 	    _node_index.find(tokens[label_index]);
   953 	    _node_index.find(tokens[label_index]);
   944 	  if (it == _node_index.end()) {
   954 	  if (it == _node_index.end()) {
   945 	    std::ostringstream msg;
   955 	    std::ostringstream msg;
   946 	    msg << "Node with label not found: " << tokens[label_index];
   956 	    msg << "Node with label not found: " << tokens[label_index];
   962     void readArcs() {
   972     void readArcs() {
   963 
   973 
   964       std::vector<int> map_index(_arc_maps.size());
   974       std::vector<int> map_index(_arc_maps.size());
   965       int map_num, label_index;
   975       int map_num, label_index;
   966 
   976 
   967       if (!readLine()) 
   977       char c;
   968 	throw DataFormatError("Cannot find map captions");
   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 	std::map<std::string, int> maps;
   987 	std::map<std::string, int> maps;
   972 	
   988 	
   973 	std::string map;
   989 	std::string map;
   993 	  map_index[i] = jt->second;
  1009 	  map_index[i] = jt->second;
   994 	}
  1010 	}
   995 
  1011 
   996 	{
  1012 	{
   997 	  std::map<std::string, int>::iterator jt = maps.find("label");
  1013 	  std::map<std::string, int>::iterator jt = maps.find("label");
   998 	  if (jt == maps.end())
  1014 	  if (jt != maps.end()) {
   999 	    throw DataFormatError("Label map not found in file");
  1015 	    label_index = jt->second;
  1000 	  label_index = jt->second;
  1016 	  } else {
       
  1017 	    label_index = -1;
       
  1018 	  }
  1001 	}
  1019 	}
  1002 	map_num = maps.size();
  1020 	map_num = maps.size();
  1003       }
  1021       }
  1004 
  1022 
  1005       char c;
       
  1006       while (readLine() && line >> c && c != '@') {
  1023       while (readLine() && line >> c && c != '@') {
  1007 	line.putback(c);
  1024 	line.putback(c);
  1008 
  1025 
  1009 	std::string source_token;
  1026 	std::string source_token;
  1010 	std::string target_token;
  1027 	std::string target_token;
  1011 
  1028 
  1012 	if (!_reader_bits::readToken(line, source_token))
  1029 	if (!_reader_bits::readToken(line, source_token))
  1013 	  throw DataFormatError("Source not found");
  1030 	  throw DataFormatError("Source not found");
  1014 
  1031 
  1015 	if (!_reader_bits::readToken(line, target_token))
  1032 	if (!_reader_bits::readToken(line, target_token))
  1016 	  throw DataFormatError("Source not found");
  1033 	  throw DataFormatError("Target not found");
  1017 	
  1034 	
  1018 	std::vector<std::string> tokens(map_num);
  1035 	std::vector<std::string> tokens(map_num);
  1019 	for (int i = 0; i < map_num; ++i) {
  1036 	for (int i = 0; i < map_num; ++i) {
  1020 	  if (!_reader_bits::readToken(line, tokens[i])) {
  1037 	  if (!_reader_bits::readToken(line, tokens[i])) {
  1021 	    std::ostringstream msg;
  1038 	    std::ostringstream msg;
  1046             throw DataFormatError(msg.str().c_str());
  1063             throw DataFormatError(msg.str().c_str());
  1047           }                                          
  1064           }                                          
  1048           Node target = it->second;                            
  1065           Node target = it->second;                            
  1049 
  1066 
  1050 	  a = _digraph.addArc(source, target);
  1067 	  a = _digraph.addArc(source, target);
  1051 	  _arc_index.insert(std::make_pair(tokens[label_index], a));
  1068 	  if (label_index != -1) 
       
  1069 	    _arc_index.insert(std::make_pair(tokens[label_index], a));
  1052 	} else {
  1070 	} else {
       
  1071 	  if (label_index == -1) 
       
  1072 	    throw DataFormatError("Label map not found in file");
  1053 	  typename std::map<std::string, Arc>::iterator it =
  1073 	  typename std::map<std::string, Arc>::iterator it =
  1054 	    _arc_index.find(tokens[label_index]);
  1074 	    _arc_index.find(tokens[label_index]);
  1055 	  if (it == _arc_index.end()) {
  1075 	  if (it == _arc_index.end()) {
  1056 	    std::ostringstream msg;
  1076 	    std::ostringstream msg;
  1057 	    msg << "Arc with label not found: " << tokens[label_index];
  1077 	    msg << "Arc with label not found: " << tokens[label_index];
  1721     void readNodes() {
  1741     void readNodes() {
  1722 
  1742 
  1723       std::vector<int> map_index(_node_maps.size());
  1743       std::vector<int> map_index(_node_maps.size());
  1724       int map_num, label_index;
  1744       int map_num, label_index;
  1725 
  1745 
  1726       if (!readLine()) 
  1746       char c;
  1727 	throw DataFormatError("Cannot find map captions");
  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 	std::map<std::string, int> maps;
  1756 	std::map<std::string, int> maps;
  1731 	
  1757 	
  1732 	std::string map;
  1758 	std::string map;
  1752 	  map_index[i] = jt->second;
  1778 	  map_index[i] = jt->second;
  1753 	}
  1779 	}
  1754 
  1780 
  1755 	{
  1781 	{
  1756 	  std::map<std::string, int>::iterator jt = maps.find("label");
  1782 	  std::map<std::string, int>::iterator jt = maps.find("label");
  1757 	  if (jt == maps.end())
  1783 	  if (jt != maps.end()) {
  1758 	    throw DataFormatError("Label map not found in file");
  1784 	    label_index = jt->second;
  1759 	  label_index = jt->second;
  1785 	  } else {
       
  1786 	    label_index = -1;
       
  1787 	  }
  1760 	}
  1788 	}
  1761 	map_num = maps.size();
  1789 	map_num = maps.size();
  1762       }
  1790       }
  1763 
  1791 
  1764       char c;
       
  1765       while (readLine() && line >> c && c != '@') {
  1792       while (readLine() && line >> c && c != '@') {
  1766 	line.putback(c);
  1793 	line.putback(c);
  1767 
  1794 
  1768 	std::vector<std::string> tokens(map_num);
  1795 	std::vector<std::string> tokens(map_num);
  1769 	for (int i = 0; i < map_num; ++i) {
  1796 	for (int i = 0; i < map_num; ++i) {
  1777 	  throw DataFormatError("Extra character on the end of line");
  1804 	  throw DataFormatError("Extra character on the end of line");
  1778 	
  1805 	
  1779 	Node n;
  1806 	Node n;
  1780 	if (!_use_nodes) {
  1807 	if (!_use_nodes) {
  1781 	  n = _graph.addNode();
  1808 	  n = _graph.addNode();
  1782 	  _node_index.insert(std::make_pair(tokens[label_index], n));
  1809 	  if (label_index != -1) 
       
  1810 	    _node_index.insert(std::make_pair(tokens[label_index], n));
  1783 	} else {
  1811 	} else {
       
  1812 	  if (label_index == -1) 
       
  1813 	    throw DataFormatError("Label map not found in file");
  1784 	  typename std::map<std::string, Node>::iterator it =
  1814 	  typename std::map<std::string, Node>::iterator it =
  1785 	    _node_index.find(tokens[label_index]);
  1815 	    _node_index.find(tokens[label_index]);
  1786 	  if (it == _node_index.end()) {
  1816 	  if (it == _node_index.end()) {
  1787 	    std::ostringstream msg;
  1817 	    std::ostringstream msg;
  1788 	    msg << "Node with label not found: " << tokens[label_index];
  1818 	    msg << "Node with label not found: " << tokens[label_index];
  1804     void readEdges() {
  1834     void readEdges() {
  1805 
  1835 
  1806       std::vector<int> map_index(_edge_maps.size());
  1836       std::vector<int> map_index(_edge_maps.size());
  1807       int map_num, label_index;
  1837       int map_num, label_index;
  1808 
  1838 
  1809       if (!readLine()) 
  1839       char c;
  1810 	throw DataFormatError("Cannot find map captions");
  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 	std::map<std::string, int> maps;
  1849 	std::map<std::string, int> maps;
  1814 	
  1850 	
  1815 	std::string map;
  1851 	std::string map;
  1835 	  map_index[i] = jt->second;
  1871 	  map_index[i] = jt->second;
  1836 	}
  1872 	}
  1837 
  1873 
  1838 	{
  1874 	{
  1839 	  std::map<std::string, int>::iterator jt = maps.find("label");
  1875 	  std::map<std::string, int>::iterator jt = maps.find("label");
  1840 	  if (jt == maps.end())
  1876 	  if (jt != maps.end()) {
  1841 	    throw DataFormatError("Label map not found in file");
  1877 	    label_index = jt->second;
  1842 	  label_index = jt->second;
  1878 	  } else {
       
  1879 	    label_index = -1;
       
  1880 	  }
  1843 	}
  1881 	}
  1844 	map_num = maps.size();
  1882 	map_num = maps.size();
  1845       }
  1883       }
  1846 
  1884 
  1847       char c;
       
  1848       while (readLine() && line >> c && c != '@') {
  1885       while (readLine() && line >> c && c != '@') {
  1849 	line.putback(c);
  1886 	line.putback(c);
  1850 
  1887 
  1851 	std::string source_token;
  1888 	std::string source_token;
  1852 	std::string target_token;
  1889 	std::string target_token;
  1853 
  1890 
  1854 	if (!_reader_bits::readToken(line, source_token))
  1891 	if (!_reader_bits::readToken(line, source_token))
  1855 	  throw DataFormatError("Source not found");
  1892 	  throw DataFormatError("Node u not found");
  1856 
  1893 
  1857 	if (!_reader_bits::readToken(line, target_token))
  1894 	if (!_reader_bits::readToken(line, target_token))
  1858 	  throw DataFormatError("Source not found");
  1895 	  throw DataFormatError("Node v not found");
  1859 	
  1896 	
  1860 	std::vector<std::string> tokens(map_num);
  1897 	std::vector<std::string> tokens(map_num);
  1861 	for (int i = 0; i < map_num; ++i) {
  1898 	for (int i = 0; i < map_num; ++i) {
  1862 	  if (!_reader_bits::readToken(line, tokens[i])) {
  1899 	  if (!_reader_bits::readToken(line, tokens[i])) {
  1863 	    std::ostringstream msg;
  1900 	    std::ostringstream msg;
  1888             throw DataFormatError(msg.str().c_str());
  1925             throw DataFormatError(msg.str().c_str());
  1889           }                                          
  1926           }                                          
  1890           Node target = it->second;                            
  1927           Node target = it->second;                            
  1891 
  1928 
  1892 	  e = _graph.addEdge(source, target);
  1929 	  e = _graph.addEdge(source, target);
  1893 	  _edge_index.insert(std::make_pair(tokens[label_index], e));
  1930 	  if (label_index != -1) 
       
  1931 	    _edge_index.insert(std::make_pair(tokens[label_index], e));
  1894 	} else {
  1932 	} else {
       
  1933 	  if (label_index == -1) 
       
  1934 	    throw DataFormatError("Label map not found in file");
  1895 	  typename std::map<std::string, Edge>::iterator it =
  1935 	  typename std::map<std::string, Edge>::iterator it =
  1896 	    _edge_index.find(tokens[label_index]);
  1936 	    _edge_index.find(tokens[label_index]);
  1897 	  if (it == _edge_index.end()) {
  1937 	  if (it == _edge_index.end()) {
  1898 	    std::ostringstream msg;
  1938 	    std::ostringstream msg;
  1899 	    msg << "Edge with label not found: " << tokens[label_index];
  1939 	    msg << "Edge with label not found: " << tokens[label_index];
  2323       }
  2363       }
  2324       line.putback(c);
  2364       line.putback(c);
  2325     }
  2365     }
  2326 
  2366 
  2327     void readMaps(std::vector<std::string>& maps) {
  2367     void readMaps(std::vector<std::string>& maps) {
  2328       if (!readLine()) 
  2368       char c;
  2329 	throw DataFormatError("Cannot find map captions");
  2369       if (!readLine() || !(line >> c) || c == '@') {
       
  2370 	if (readSuccess() && line) line.putback(c);
       
  2371 	return;
       
  2372       }
       
  2373       line.putback(c);
  2330       std::string map;
  2374       std::string map;
  2331       while (_reader_bits::readToken(line, map)) {
  2375       while (_reader_bits::readToken(line, map)) {
  2332 	maps.push_back(map);
  2376 	maps.push_back(map);
  2333       }
  2377       }
  2334     }
  2378     }