| ... | ... |
@@ -434,132 +434,139 @@ |
| 434 | 434 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
| 435 | 435 |
/// The previously read label node map should be passed to the \c |
| 436 | 436 |
/// useNodes() functions. Another application of multipass reading when |
| 437 | 437 |
/// paths are given as a node map or an arc map. It is impossible read this in |
| 438 | 438 |
/// a single pass, because the arcs are not constructed when the node |
| 439 | 439 |
/// maps are read. |
| 440 | 440 |
template <typename _Digraph> |
| 441 | 441 |
class DigraphReader {
|
| 442 | 442 |
public: |
| 443 | 443 |
|
| 444 | 444 |
typedef _Digraph Digraph; |
| 445 | 445 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 446 | 446 |
|
| 447 | 447 |
private: |
| 448 | 448 |
|
| 449 | 449 |
|
| 450 | 450 |
std::istream* _is; |
| 451 | 451 |
bool local_is; |
| 452 | 452 |
|
| 453 | 453 |
Digraph& _digraph; |
| 454 | 454 |
|
| 455 | 455 |
std::string _nodes_caption; |
| 456 | 456 |
std::string _arcs_caption; |
| 457 | 457 |
std::string _attributes_caption; |
| 458 | 458 |
|
| 459 | 459 |
typedef std::map<std::string, Node> NodeIndex; |
| 460 | 460 |
NodeIndex _node_index; |
| 461 | 461 |
typedef std::map<std::string, Arc> ArcIndex; |
| 462 | 462 |
ArcIndex _arc_index; |
| 463 | 463 |
|
| 464 | 464 |
typedef std::vector<std::pair<std::string, |
| 465 | 465 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 466 | 466 |
NodeMaps _node_maps; |
| 467 | 467 |
|
| 468 | 468 |
typedef std::vector<std::pair<std::string, |
| 469 | 469 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
| 470 | 470 |
ArcMaps _arc_maps; |
| 471 | 471 |
|
| 472 | 472 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 473 | 473 |
Attributes; |
| 474 | 474 |
Attributes _attributes; |
| 475 | 475 |
|
| 476 | 476 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 477 | 477 |
Sections _sections; |
| 478 | 478 |
|
| 479 | 479 |
bool _use_nodes; |
| 480 | 480 |
bool _use_arcs; |
| 481 | 481 |
|
| 482 |
bool _skip_nodes; |
|
| 483 |
bool _skip_arcs; |
|
| 484 |
|
|
| 482 | 485 |
int line_num; |
| 483 | 486 |
std::istringstream line; |
| 484 | 487 |
|
| 485 | 488 |
public: |
| 486 | 489 |
|
| 487 | 490 |
/// \brief Constructor |
| 488 | 491 |
/// |
| 489 | 492 |
/// Construct a directed graph reader, which reads from the given |
| 490 | 493 |
/// input stream. |
| 491 | 494 |
DigraphReader(std::istream& is, Digraph& digraph) |
| 492 | 495 |
: _is(&is), local_is(false), _digraph(digraph), |
| 493 |
_use_nodes(false), _use_arcs(false) |
|
| 496 |
_use_nodes(false), _use_arcs(false), |
|
| 497 |
_skip_nodes(false), _skip_arcs(false) {}
|
|
| 494 | 498 |
|
| 495 | 499 |
/// \brief Constructor |
| 496 | 500 |
/// |
| 497 | 501 |
/// Construct a directed graph reader, which reads from the given |
| 498 | 502 |
/// file. |
| 499 | 503 |
DigraphReader(const std::string& fn, Digraph& digraph) |
| 500 | 504 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), |
| 501 |
_use_nodes(false), _use_arcs(false) |
|
| 505 |
_use_nodes(false), _use_arcs(false), |
|
| 506 |
_skip_nodes(false), _skip_arcs(false) {}
|
|
| 502 | 507 |
|
| 503 | 508 |
/// \brief Constructor |
| 504 | 509 |
/// |
| 505 | 510 |
/// Construct a directed graph reader, which reads from the given |
| 506 | 511 |
/// file. |
| 507 | 512 |
DigraphReader(const char* fn, Digraph& digraph) |
| 508 | 513 |
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), |
| 509 |
_use_nodes(false), _use_arcs(false) |
|
| 514 |
_use_nodes(false), _use_arcs(false), |
|
| 515 |
_skip_nodes(false), _skip_arcs(false) {}
|
|
| 510 | 516 |
|
| 511 | 517 |
/// \brief Copy constructor |
| 512 | 518 |
/// |
| 513 | 519 |
/// The copy constructor transfers all data from the other reader, |
| 514 | 520 |
/// therefore the copied reader will not be usable more. |
| 515 | 521 |
DigraphReader(DigraphReader& other) |
| 516 | 522 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
| 517 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) |
|
| 523 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
|
| 524 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
|
| 518 | 525 |
|
| 519 | 526 |
other._is = 0; |
| 520 | 527 |
other.local_is = false; |
| 521 | 528 |
|
| 522 | 529 |
_node_index.swap(other._node_index); |
| 523 | 530 |
_arc_index.swap(other._arc_index); |
| 524 | 531 |
|
| 525 | 532 |
_node_maps.swap(other._node_maps); |
| 526 | 533 |
_arc_maps.swap(other._arc_maps); |
| 527 | 534 |
_attributes.swap(other._attributes); |
| 528 | 535 |
|
| 529 | 536 |
_nodes_caption = other._nodes_caption; |
| 530 | 537 |
_arcs_caption = other._arcs_caption; |
| 531 | 538 |
_attributes_caption = other._attributes_caption; |
| 532 | 539 |
|
| 533 | 540 |
_sections.swap(other._sections); |
| 534 | 541 |
} |
| 535 | 542 |
|
| 536 | 543 |
/// \brief Destructor |
| 537 | 544 |
~DigraphReader() {
|
| 538 | 545 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 539 | 546 |
it != _node_maps.end(); ++it) {
|
| 540 | 547 |
delete it->second; |
| 541 | 548 |
} |
| 542 | 549 |
|
| 543 | 550 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 544 | 551 |
it != _arc_maps.end(); ++it) {
|
| 545 | 552 |
delete it->second; |
| 546 | 553 |
} |
| 547 | 554 |
|
| 548 | 555 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 549 | 556 |
it != _attributes.end(); ++it) {
|
| 550 | 557 |
delete it->second; |
| 551 | 558 |
} |
| 552 | 559 |
|
| 553 | 560 |
for (typename Sections::iterator it = _sections.begin(); |
| 554 | 561 |
it != _sections.end(); ++it) {
|
| 555 | 562 |
delete it->second; |
| 556 | 563 |
} |
| 557 | 564 |
|
| 558 | 565 |
if (local_is) {
|
| 559 | 566 |
delete _is; |
| 560 | 567 |
} |
| 561 | 568 |
|
| 562 | 569 |
} |
| 563 | 570 |
|
| 564 | 571 |
private: |
| 565 | 572 |
|
| ... | ... |
@@ -792,106 +799,132 @@ |
| 792 | 799 |
_use_nodes = true; |
| 793 | 800 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 794 | 801 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 795 | 802 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 796 | 803 |
} |
| 797 | 804 |
return *this; |
| 798 | 805 |
} |
| 799 | 806 |
|
| 800 | 807 |
/// \brief Use previously constructed node set |
| 801 | 808 |
/// |
| 802 | 809 |
/// Use previously constructed node set, and specify the node |
| 803 | 810 |
/// label map and a functor which converts the label map values to |
| 804 | 811 |
/// std::string. |
| 805 | 812 |
template <typename Map, typename Converter> |
| 806 | 813 |
DigraphReader& useNodes(const Map& map, |
| 807 | 814 |
const Converter& converter = Converter()) {
|
| 808 | 815 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 809 | 816 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 810 | 817 |
_use_nodes = true; |
| 811 | 818 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 812 | 819 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 813 | 820 |
} |
| 814 | 821 |
return *this; |
| 815 | 822 |
} |
| 816 | 823 |
|
| 817 | 824 |
/// \brief Use previously constructed arc set |
| 818 | 825 |
/// |
| 819 | 826 |
/// Use previously constructed arc set, and specify the arc |
| 820 | 827 |
/// label map. |
| 821 | 828 |
template <typename Map> |
| 822 | 829 |
DigraphReader& useArcs(const Map& map) {
|
| 823 | 830 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 824 | 831 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 825 | 832 |
_use_arcs = true; |
| 826 | 833 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 827 | 834 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 828 | 835 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 829 | 836 |
} |
| 830 | 837 |
return *this; |
| 831 | 838 |
} |
| 832 | 839 |
|
| 833 | 840 |
/// \brief Use previously constructed arc set |
| 834 | 841 |
/// |
| 835 | 842 |
/// Use previously constructed arc set, and specify the arc |
| 836 | 843 |
/// label map and a functor which converts the label map values to |
| 837 | 844 |
/// std::string. |
| 838 | 845 |
template <typename Map, typename Converter> |
| 839 | 846 |
DigraphReader& useArcs(const Map& map, |
| 840 |
|
|
| 847 |
const Converter& converter = Converter()) {
|
|
| 841 | 848 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 842 | 849 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 843 | 850 |
_use_arcs = true; |
| 844 | 851 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 845 | 852 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 846 | 853 |
} |
| 847 | 854 |
return *this; |
| 848 | 855 |
} |
| 849 | 856 |
|
| 857 |
/// \brief Skips the reading of node section |
|
| 858 |
/// |
|
| 859 |
/// Omit the reading of the node section. This implies that each node |
|
| 860 |
/// map reading rule will be abanoned, and the nodes of the graph |
|
| 861 |
/// will not be constructed, which usually cause that the arc set |
|
| 862 |
/// could not be read due to lack of node name |
|
| 863 |
/// resolving. Therefore, the \c skipArcs() should be used too, or |
|
| 864 |
/// the useNodes() member function should be used to specify the |
|
| 865 |
/// label of the nodes. |
|
| 866 |
DigraphReader& skipNodes() {
|
|
| 867 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
|
| 868 |
_skip_nodes = true; |
|
| 869 |
return *this; |
|
| 870 |
} |
|
| 871 |
|
|
| 872 |
/// \brief Skips the reading of arc section |
|
| 873 |
/// |
|
| 874 |
/// Omit the reading of the arc section. This implies that each arc |
|
| 875 |
/// map reading rule will be abanoned, and the arcs of the graph |
|
| 876 |
/// will not be constructed. |
|
| 877 |
DigraphReader& skipArcs() {
|
|
| 878 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
|
| 879 |
_skip_arcs = true; |
|
| 880 |
return *this; |
|
| 881 |
} |
|
| 882 |
|
|
| 850 | 883 |
/// @} |
| 851 | 884 |
|
| 852 | 885 |
private: |
| 853 | 886 |
|
| 854 | 887 |
bool readLine() {
|
| 855 | 888 |
std::string str; |
| 856 | 889 |
while(++line_num, std::getline(*_is, str)) {
|
| 857 | 890 |
line.clear(); line.str(str); |
| 858 | 891 |
char c; |
| 859 | 892 |
if (line >> std::ws >> c && c != '#') {
|
| 860 | 893 |
line.putback(c); |
| 861 | 894 |
return true; |
| 862 | 895 |
} |
| 863 | 896 |
} |
| 864 | 897 |
return false; |
| 865 | 898 |
} |
| 866 | 899 |
|
| 867 | 900 |
bool readSuccess() {
|
| 868 | 901 |
return static_cast<bool>(*_is); |
| 869 | 902 |
} |
| 870 | 903 |
|
| 871 | 904 |
void skipSection() {
|
| 872 | 905 |
char c; |
| 873 | 906 |
while (readSuccess() && line >> c && c != '@') {
|
| 874 | 907 |
readLine(); |
| 875 | 908 |
} |
| 876 | 909 |
line.putback(c); |
| 877 | 910 |
} |
| 878 | 911 |
|
| 879 | 912 |
void readNodes() {
|
| 880 | 913 |
|
| 881 | 914 |
std::vector<int> map_index(_node_maps.size()); |
| 882 | 915 |
int map_num, label_index; |
| 883 | 916 |
|
| 884 | 917 |
char c; |
| 885 | 918 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 886 | 919 |
if (readSuccess() && line) line.putback(c); |
| 887 | 920 |
if (!_node_maps.empty()) |
| 888 | 921 |
throw DataFormatError("Cannot find map names");
|
| 889 | 922 |
return; |
| 890 | 923 |
} |
| 891 | 924 |
line.putback(c); |
| 892 | 925 |
|
| 893 | 926 |
{
|
| 894 | 927 |
std::map<std::string, int> maps; |
| 895 | 928 |
|
| 896 | 929 |
std::string map; |
| 897 | 930 |
int index = 0; |
| ... | ... |
@@ -1107,98 +1140,98 @@ |
| 1107 | 1140 |
throw DataFormatError("Extra character on the end of line");
|
| 1108 | 1141 |
|
| 1109 | 1142 |
{
|
| 1110 | 1143 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1111 | 1144 |
if (it != read_attr.end()) {
|
| 1112 | 1145 |
std::ostringstream msg; |
| 1113 | 1146 |
msg << "Multiple occurence of attribute " << attr; |
| 1114 | 1147 |
throw DataFormatError(msg.str().c_str()); |
| 1115 | 1148 |
} |
| 1116 | 1149 |
read_attr.insert(attr); |
| 1117 | 1150 |
} |
| 1118 | 1151 |
|
| 1119 | 1152 |
{
|
| 1120 | 1153 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1121 | 1154 |
while (it != _attributes.end() && it->first == attr) {
|
| 1122 | 1155 |
it->second->set(token); |
| 1123 | 1156 |
++it; |
| 1124 | 1157 |
} |
| 1125 | 1158 |
} |
| 1126 | 1159 |
|
| 1127 | 1160 |
} |
| 1128 | 1161 |
if (readSuccess()) {
|
| 1129 | 1162 |
line.putback(c); |
| 1130 | 1163 |
} |
| 1131 | 1164 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1132 | 1165 |
it != _attributes.end(); ++it) {
|
| 1133 | 1166 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1134 | 1167 |
std::ostringstream msg; |
| 1135 | 1168 |
msg << "Attribute not found in file: " << it->first; |
| 1136 | 1169 |
throw DataFormatError(msg.str().c_str()); |
| 1137 | 1170 |
} |
| 1138 | 1171 |
} |
| 1139 | 1172 |
} |
| 1140 | 1173 |
|
| 1141 | 1174 |
public: |
| 1142 | 1175 |
|
| 1143 | 1176 |
/// \name Execution of the reader |
| 1144 | 1177 |
/// @{
|
| 1145 | 1178 |
|
| 1146 | 1179 |
/// \brief Start the batch processing |
| 1147 | 1180 |
/// |
| 1148 | 1181 |
/// This function starts the batch processing |
| 1149 | 1182 |
void run() {
|
| 1150 | 1183 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1151 | 1184 |
if (!*_is) {
|
| 1152 | 1185 |
throw DataFormatError("Cannot find file");
|
| 1153 | 1186 |
} |
| 1154 | 1187 |
|
| 1155 |
bool nodes_done = false; |
|
| 1156 |
bool arcs_done = false; |
|
| 1188 |
bool nodes_done = _skip_nodes; |
|
| 1189 |
bool arcs_done = _skip_arcs; |
|
| 1157 | 1190 |
bool attributes_done = false; |
| 1158 | 1191 |
std::set<std::string> extra_sections; |
| 1159 | 1192 |
|
| 1160 | 1193 |
line_num = 0; |
| 1161 | 1194 |
readLine(); |
| 1162 | 1195 |
skipSection(); |
| 1163 | 1196 |
|
| 1164 | 1197 |
while (readSuccess()) {
|
| 1165 | 1198 |
try {
|
| 1166 | 1199 |
char c; |
| 1167 | 1200 |
std::string section, caption; |
| 1168 | 1201 |
line >> c; |
| 1169 | 1202 |
_reader_bits::readToken(line, section); |
| 1170 | 1203 |
_reader_bits::readToken(line, caption); |
| 1171 | 1204 |
|
| 1172 | 1205 |
if (line >> c) |
| 1173 | 1206 |
throw DataFormatError("Extra character on the end of line");
|
| 1174 | 1207 |
|
| 1175 | 1208 |
if (section == "nodes" && !nodes_done) {
|
| 1176 | 1209 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1177 | 1210 |
readNodes(); |
| 1178 | 1211 |
nodes_done = true; |
| 1179 | 1212 |
} |
| 1180 | 1213 |
} else if ((section == "arcs" || section == "edges") && |
| 1181 | 1214 |
!arcs_done) {
|
| 1182 | 1215 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
| 1183 | 1216 |
readArcs(); |
| 1184 | 1217 |
arcs_done = true; |
| 1185 | 1218 |
} |
| 1186 | 1219 |
} else if (section == "attributes" && !attributes_done) {
|
| 1187 | 1220 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1188 | 1221 |
readAttributes(); |
| 1189 | 1222 |
attributes_done = true; |
| 1190 | 1223 |
} |
| 1191 | 1224 |
} else {
|
| 1192 | 1225 |
if (extra_sections.find(section) != extra_sections.end()) {
|
| 1193 | 1226 |
std::ostringstream msg; |
| 1194 | 1227 |
msg << "Multiple occurence of section " << section; |
| 1195 | 1228 |
throw DataFormatError(msg.str().c_str()); |
| 1196 | 1229 |
} |
| 1197 | 1230 |
Sections::iterator it = _sections.find(section); |
| 1198 | 1231 |
if (it != _sections.end()) {
|
| 1199 | 1232 |
extra_sections.insert(section); |
| 1200 | 1233 |
it->second->process(*_is, line_num); |
| 1201 | 1234 |
} |
| 1202 | 1235 |
readLine(); |
| 1203 | 1236 |
skipSection(); |
| 1204 | 1237 |
} |
| ... | ... |
@@ -1250,132 +1283,139 @@ |
| 1250 | 1283 |
|
| 1251 | 1284 |
/// \ingroup lemon_io |
| 1252 | 1285 |
/// |
| 1253 | 1286 |
/// \brief LGF reader for undirected graphs |
| 1254 | 1287 |
/// |
| 1255 | 1288 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1256 | 1289 |
template <typename _Graph> |
| 1257 | 1290 |
class GraphReader {
|
| 1258 | 1291 |
public: |
| 1259 | 1292 |
|
| 1260 | 1293 |
typedef _Graph Graph; |
| 1261 | 1294 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 1262 | 1295 |
|
| 1263 | 1296 |
private: |
| 1264 | 1297 |
|
| 1265 | 1298 |
|
| 1266 | 1299 |
std::istream* _is; |
| 1267 | 1300 |
bool local_is; |
| 1268 | 1301 |
|
| 1269 | 1302 |
Graph& _graph; |
| 1270 | 1303 |
|
| 1271 | 1304 |
std::string _nodes_caption; |
| 1272 | 1305 |
std::string _edges_caption; |
| 1273 | 1306 |
std::string _attributes_caption; |
| 1274 | 1307 |
|
| 1275 | 1308 |
typedef std::map<std::string, Node> NodeIndex; |
| 1276 | 1309 |
NodeIndex _node_index; |
| 1277 | 1310 |
typedef std::map<std::string, Edge> EdgeIndex; |
| 1278 | 1311 |
EdgeIndex _edge_index; |
| 1279 | 1312 |
|
| 1280 | 1313 |
typedef std::vector<std::pair<std::string, |
| 1281 | 1314 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 1282 | 1315 |
NodeMaps _node_maps; |
| 1283 | 1316 |
|
| 1284 | 1317 |
typedef std::vector<std::pair<std::string, |
| 1285 | 1318 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 1286 | 1319 |
EdgeMaps _edge_maps; |
| 1287 | 1320 |
|
| 1288 | 1321 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 1289 | 1322 |
Attributes; |
| 1290 | 1323 |
Attributes _attributes; |
| 1291 | 1324 |
|
| 1292 | 1325 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 1293 | 1326 |
Sections _sections; |
| 1294 | 1327 |
|
| 1295 | 1328 |
bool _use_nodes; |
| 1296 | 1329 |
bool _use_edges; |
| 1297 | 1330 |
|
| 1331 |
bool _skip_nodes; |
|
| 1332 |
bool _skip_edges; |
|
| 1333 |
|
|
| 1298 | 1334 |
int line_num; |
| 1299 | 1335 |
std::istringstream line; |
| 1300 | 1336 |
|
| 1301 | 1337 |
public: |
| 1302 | 1338 |
|
| 1303 | 1339 |
/// \brief Constructor |
| 1304 | 1340 |
/// |
| 1305 | 1341 |
/// Construct a undirected graph reader, which reads from the given |
| 1306 | 1342 |
/// input stream. |
| 1307 | 1343 |
GraphReader(std::istream& is, Graph& graph) |
| 1308 | 1344 |
: _is(&is), local_is(false), _graph(graph), |
| 1309 |
_use_nodes(false), _use_edges(false) |
|
| 1345 |
_use_nodes(false), _use_edges(false), |
|
| 1346 |
_skip_nodes(false), _skip_edges(false) {}
|
|
| 1310 | 1347 |
|
| 1311 | 1348 |
/// \brief Constructor |
| 1312 | 1349 |
/// |
| 1313 | 1350 |
/// Construct a undirected graph reader, which reads from the given |
| 1314 | 1351 |
/// file. |
| 1315 | 1352 |
GraphReader(const std::string& fn, Graph& graph) |
| 1316 | 1353 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), |
| 1317 |
_use_nodes(false), _use_edges(false) |
|
| 1354 |
_use_nodes(false), _use_edges(false), |
|
| 1355 |
_skip_nodes(false), _skip_edges(false) {}
|
|
| 1318 | 1356 |
|
| 1319 | 1357 |
/// \brief Constructor |
| 1320 | 1358 |
/// |
| 1321 | 1359 |
/// Construct a undirected graph reader, which reads from the given |
| 1322 | 1360 |
/// file. |
| 1323 | 1361 |
GraphReader(const char* fn, Graph& graph) |
| 1324 | 1362 |
: _is(new std::ifstream(fn)), local_is(true), _graph(graph), |
| 1325 |
_use_nodes(false), _use_edges(false) |
|
| 1363 |
_use_nodes(false), _use_edges(false), |
|
| 1364 |
_skip_nodes(false), _skip_edges(false) {}
|
|
| 1326 | 1365 |
|
| 1327 | 1366 |
/// \brief Copy constructor |
| 1328 | 1367 |
/// |
| 1329 | 1368 |
/// The copy constructor transfers all data from the other reader, |
| 1330 | 1369 |
/// therefore the copied reader will not be usable more. |
| 1331 | 1370 |
GraphReader(GraphReader& other) |
| 1332 | 1371 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 1333 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges) |
|
| 1372 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
|
| 1373 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
|
| 1334 | 1374 |
|
| 1335 | 1375 |
other._is = 0; |
| 1336 | 1376 |
other.local_is = false; |
| 1337 | 1377 |
|
| 1338 | 1378 |
_node_index.swap(other._node_index); |
| 1339 | 1379 |
_edge_index.swap(other._edge_index); |
| 1340 | 1380 |
|
| 1341 | 1381 |
_node_maps.swap(other._node_maps); |
| 1342 | 1382 |
_edge_maps.swap(other._edge_maps); |
| 1343 | 1383 |
_attributes.swap(other._attributes); |
| 1344 | 1384 |
|
| 1345 | 1385 |
_nodes_caption = other._nodes_caption; |
| 1346 | 1386 |
_edges_caption = other._edges_caption; |
| 1347 | 1387 |
_attributes_caption = other._attributes_caption; |
| 1348 | 1388 |
|
| 1349 | 1389 |
_sections.swap(other._sections); |
| 1350 | 1390 |
} |
| 1351 | 1391 |
|
| 1352 | 1392 |
/// \brief Destructor |
| 1353 | 1393 |
~GraphReader() {
|
| 1354 | 1394 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1355 | 1395 |
it != _node_maps.end(); ++it) {
|
| 1356 | 1396 |
delete it->second; |
| 1357 | 1397 |
} |
| 1358 | 1398 |
|
| 1359 | 1399 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1360 | 1400 |
it != _edge_maps.end(); ++it) {
|
| 1361 | 1401 |
delete it->second; |
| 1362 | 1402 |
} |
| 1363 | 1403 |
|
| 1364 | 1404 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1365 | 1405 |
it != _attributes.end(); ++it) {
|
| 1366 | 1406 |
delete it->second; |
| 1367 | 1407 |
} |
| 1368 | 1408 |
|
| 1369 | 1409 |
for (typename Sections::iterator it = _sections.begin(); |
| 1370 | 1410 |
it != _sections.end(); ++it) {
|
| 1371 | 1411 |
delete it->second; |
| 1372 | 1412 |
} |
| 1373 | 1413 |
|
| 1374 | 1414 |
if (local_is) {
|
| 1375 | 1415 |
delete _is; |
| 1376 | 1416 |
} |
| 1377 | 1417 |
|
| 1378 | 1418 |
} |
| 1379 | 1419 |
|
| 1380 | 1420 |
private: |
| 1381 | 1421 |
|
| ... | ... |
@@ -1664,96 +1704,122 @@ |
| 1664 | 1704 |
/// Use previously constructed node set, and specify the node |
| 1665 | 1705 |
/// label map and a functor which converts the label map values to |
| 1666 | 1706 |
/// std::string. |
| 1667 | 1707 |
template <typename Map, typename Converter> |
| 1668 | 1708 |
GraphReader& useNodes(const Map& map, |
| 1669 | 1709 |
const Converter& converter = Converter()) {
|
| 1670 | 1710 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1671 | 1711 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 1672 | 1712 |
_use_nodes = true; |
| 1673 | 1713 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1674 | 1714 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 1675 | 1715 |
} |
| 1676 | 1716 |
return *this; |
| 1677 | 1717 |
} |
| 1678 | 1718 |
|
| 1679 | 1719 |
/// \brief Use previously constructed edge set |
| 1680 | 1720 |
/// |
| 1681 | 1721 |
/// Use previously constructed edge set, and specify the edge |
| 1682 | 1722 |
/// label map. |
| 1683 | 1723 |
template <typename Map> |
| 1684 | 1724 |
GraphReader& useEdges(const Map& map) {
|
| 1685 | 1725 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1686 | 1726 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1687 | 1727 |
_use_edges = true; |
| 1688 | 1728 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 1689 | 1729 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1690 | 1730 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1691 | 1731 |
} |
| 1692 | 1732 |
return *this; |
| 1693 | 1733 |
} |
| 1694 | 1734 |
|
| 1695 | 1735 |
/// \brief Use previously constructed edge set |
| 1696 | 1736 |
/// |
| 1697 | 1737 |
/// Use previously constructed edge set, and specify the edge |
| 1698 | 1738 |
/// label map and a functor which converts the label map values to |
| 1699 | 1739 |
/// std::string. |
| 1700 | 1740 |
template <typename Map, typename Converter> |
| 1701 | 1741 |
GraphReader& useEdges(const Map& map, |
| 1702 | 1742 |
const Converter& converter = Converter()) {
|
| 1703 | 1743 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1704 | 1744 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1705 | 1745 |
_use_edges = true; |
| 1706 | 1746 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1707 | 1747 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1708 | 1748 |
} |
| 1709 | 1749 |
return *this; |
| 1710 | 1750 |
} |
| 1711 | 1751 |
|
| 1752 |
/// \brief Skips the reading of node section |
|
| 1753 |
/// |
|
| 1754 |
/// Omit the reading of the node section. This implies that each node |
|
| 1755 |
/// map reading rule will be abanoned, and the nodes of the graph |
|
| 1756 |
/// will not be constructed, which usually cause that the edge set |
|
| 1757 |
/// could not be read due to lack of node name |
|
| 1758 |
/// resolving. Therefore, the \c skipEdges() should be used too, or |
|
| 1759 |
/// the useNodes() member function should be used to specify the |
|
| 1760 |
/// label of the nodes. |
|
| 1761 |
GraphReader& skipNodes() {
|
|
| 1762 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
|
| 1763 |
_skip_nodes = true; |
|
| 1764 |
return *this; |
|
| 1765 |
} |
|
| 1766 |
|
|
| 1767 |
/// \brief Skips the reading of edge section |
|
| 1768 |
/// |
|
| 1769 |
/// Omit the reading of the edge section. This implies that each edge |
|
| 1770 |
/// map reading rule will be abanoned, and the edges of the graph |
|
| 1771 |
/// will not be constructed. |
|
| 1772 |
GraphReader& skipEdges() {
|
|
| 1773 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
|
| 1774 |
_skip_edges = true; |
|
| 1775 |
return *this; |
|
| 1776 |
} |
|
| 1777 |
|
|
| 1712 | 1778 |
/// @} |
| 1713 | 1779 |
|
| 1714 | 1780 |
private: |
| 1715 | 1781 |
|
| 1716 | 1782 |
bool readLine() {
|
| 1717 | 1783 |
std::string str; |
| 1718 | 1784 |
while(++line_num, std::getline(*_is, str)) {
|
| 1719 | 1785 |
line.clear(); line.str(str); |
| 1720 | 1786 |
char c; |
| 1721 | 1787 |
if (line >> std::ws >> c && c != '#') {
|
| 1722 | 1788 |
line.putback(c); |
| 1723 | 1789 |
return true; |
| 1724 | 1790 |
} |
| 1725 | 1791 |
} |
| 1726 | 1792 |
return false; |
| 1727 | 1793 |
} |
| 1728 | 1794 |
|
| 1729 | 1795 |
bool readSuccess() {
|
| 1730 | 1796 |
return static_cast<bool>(*_is); |
| 1731 | 1797 |
} |
| 1732 | 1798 |
|
| 1733 | 1799 |
void skipSection() {
|
| 1734 | 1800 |
char c; |
| 1735 | 1801 |
while (readSuccess() && line >> c && c != '@') {
|
| 1736 | 1802 |
readLine(); |
| 1737 | 1803 |
} |
| 1738 | 1804 |
line.putback(c); |
| 1739 | 1805 |
} |
| 1740 | 1806 |
|
| 1741 | 1807 |
void readNodes() {
|
| 1742 | 1808 |
|
| 1743 | 1809 |
std::vector<int> map_index(_node_maps.size()); |
| 1744 | 1810 |
int map_num, label_index; |
| 1745 | 1811 |
|
| 1746 | 1812 |
char c; |
| 1747 | 1813 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 1748 | 1814 |
if (readSuccess() && line) line.putback(c); |
| 1749 | 1815 |
if (!_node_maps.empty()) |
| 1750 | 1816 |
throw DataFormatError("Cannot find map names");
|
| 1751 | 1817 |
return; |
| 1752 | 1818 |
} |
| 1753 | 1819 |
line.putback(c); |
| 1754 | 1820 |
|
| 1755 | 1821 |
{
|
| 1756 | 1822 |
std::map<std::string, int> maps; |
| 1757 | 1823 |
|
| 1758 | 1824 |
std::string map; |
| 1759 | 1825 |
int index = 0; |
| ... | ... |
@@ -1967,98 +2033,98 @@ |
| 1967 | 2033 |
throw DataFormatError("Attribute value not found");
|
| 1968 | 2034 |
if (line >> c) |
| 1969 | 2035 |
throw DataFormatError("Extra character on the end of line");
|
| 1970 | 2036 |
|
| 1971 | 2037 |
{
|
| 1972 | 2038 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1973 | 2039 |
if (it != read_attr.end()) {
|
| 1974 | 2040 |
std::ostringstream msg; |
| 1975 | 2041 |
msg << "Multiple occurence of attribute " << attr; |
| 1976 | 2042 |
throw DataFormatError(msg.str().c_str()); |
| 1977 | 2043 |
} |
| 1978 | 2044 |
read_attr.insert(attr); |
| 1979 | 2045 |
} |
| 1980 | 2046 |
|
| 1981 | 2047 |
{
|
| 1982 | 2048 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1983 | 2049 |
while (it != _attributes.end() && it->first == attr) {
|
| 1984 | 2050 |
it->second->set(token); |
| 1985 | 2051 |
++it; |
| 1986 | 2052 |
} |
| 1987 | 2053 |
} |
| 1988 | 2054 |
|
| 1989 | 2055 |
} |
| 1990 | 2056 |
if (readSuccess()) {
|
| 1991 | 2057 |
line.putback(c); |
| 1992 | 2058 |
} |
| 1993 | 2059 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1994 | 2060 |
it != _attributes.end(); ++it) {
|
| 1995 | 2061 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1996 | 2062 |
std::ostringstream msg; |
| 1997 | 2063 |
msg << "Attribute not found in file: " << it->first; |
| 1998 | 2064 |
throw DataFormatError(msg.str().c_str()); |
| 1999 | 2065 |
} |
| 2000 | 2066 |
} |
| 2001 | 2067 |
} |
| 2002 | 2068 |
|
| 2003 | 2069 |
public: |
| 2004 | 2070 |
|
| 2005 | 2071 |
/// \name Execution of the reader |
| 2006 | 2072 |
/// @{
|
| 2007 | 2073 |
|
| 2008 | 2074 |
/// \brief Start the batch processing |
| 2009 | 2075 |
/// |
| 2010 | 2076 |
/// This function starts the batch processing |
| 2011 | 2077 |
void run() {
|
| 2012 | 2078 |
|
| 2013 | 2079 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 2014 | 2080 |
|
| 2015 |
bool nodes_done = false; |
|
| 2016 |
bool edges_done = false; |
|
| 2081 |
bool nodes_done = _skip_nodes; |
|
| 2082 |
bool edges_done = _skip_edges; |
|
| 2017 | 2083 |
bool attributes_done = false; |
| 2018 | 2084 |
std::set<std::string> extra_sections; |
| 2019 | 2085 |
|
| 2020 | 2086 |
line_num = 0; |
| 2021 | 2087 |
readLine(); |
| 2022 | 2088 |
skipSection(); |
| 2023 | 2089 |
|
| 2024 | 2090 |
while (readSuccess()) {
|
| 2025 | 2091 |
try {
|
| 2026 | 2092 |
char c; |
| 2027 | 2093 |
std::string section, caption; |
| 2028 | 2094 |
line >> c; |
| 2029 | 2095 |
_reader_bits::readToken(line, section); |
| 2030 | 2096 |
_reader_bits::readToken(line, caption); |
| 2031 | 2097 |
|
| 2032 | 2098 |
if (line >> c) |
| 2033 | 2099 |
throw DataFormatError("Extra character on the end of line");
|
| 2034 | 2100 |
|
| 2035 | 2101 |
if (section == "nodes" && !nodes_done) {
|
| 2036 | 2102 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 2037 | 2103 |
readNodes(); |
| 2038 | 2104 |
nodes_done = true; |
| 2039 | 2105 |
} |
| 2040 | 2106 |
} else if ((section == "edges" || section == "arcs") && |
| 2041 | 2107 |
!edges_done) {
|
| 2042 | 2108 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
| 2043 | 2109 |
readEdges(); |
| 2044 | 2110 |
edges_done = true; |
| 2045 | 2111 |
} |
| 2046 | 2112 |
} else if (section == "attributes" && !attributes_done) {
|
| 2047 | 2113 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 2048 | 2114 |
readAttributes(); |
| 2049 | 2115 |
attributes_done = true; |
| 2050 | 2116 |
} |
| 2051 | 2117 |
} else {
|
| 2052 | 2118 |
if (extra_sections.find(section) != extra_sections.end()) {
|
| 2053 | 2119 |
std::ostringstream msg; |
| 2054 | 2120 |
msg << "Multiple occurence of section " << section; |
| 2055 | 2121 |
throw DataFormatError(msg.str().c_str()); |
| 2056 | 2122 |
} |
| 2057 | 2123 |
Sections::iterator it = _sections.find(section); |
| 2058 | 2124 |
if (it != _sections.end()) {
|
| 2059 | 2125 |
extra_sections.insert(section); |
| 2060 | 2126 |
it->second->process(*_is, line_num); |
| 2061 | 2127 |
} |
| 2062 | 2128 |
readLine(); |
| 2063 | 2129 |
skipSection(); |
| 2064 | 2130 |
} |
0 comments (0 inline)