Changeset 1901:723b2b81d900 in lemon-0.x for lemon/lemon_reader.h
- Timestamp:
- 01/24/06 17:07:38 (18 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2476
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lemon_reader.h
r1875 r1901 48 48 template <typename T> 49 49 bool operator<(T, T) { 50 throw DataFormatError(" Idis not comparable");50 throw DataFormatError("Label is not comparable"); 51 51 } 52 52 … … 59 59 60 60 template <typename Item> 61 class Item IdReader {61 class ItemLabelReader { 62 62 public: 63 63 64 bool is IdReader() { return true; }65 66 void read Id(std::istream&, Item&) {}64 bool isLabelReader() { return true; } 65 66 void readLabel(std::istream&, Item&) {} 67 67 68 template <class _Item IdReader>68 template <class _ItemLabelReader> 69 69 struct Constraints { 70 70 void constraints() { 71 bool b = reader.is IdReader();71 bool b = reader.isLabelReader(); 72 72 ignore_unused_variable_warning(b); 73 73 Item item; 74 reader.read Id(is, item);75 } 76 _Item IdReader& reader;74 reader.readLabel(is, item); 75 } 76 _ItemLabelReader& reader; 77 77 std::istream& is; 78 78 }; … … 242 242 inverse.insert(std::make_pair(value, item)); 243 243 } else { 244 throw DataFormatError("Multiple IDoccurence");244 throw DataFormatError("Multiple label occurence"); 245 245 } 246 246 } … … 253 253 return it->second; 254 254 } else { 255 throw DataFormatError("Invalid IDerror");255 throw DataFormatError("Invalid label error"); 256 256 } 257 257 } … … 280 280 inverse.insert(std::make_pair(value, item)); 281 281 } else { 282 throw DataFormatError("Multiple IDoccurence error");282 throw DataFormatError("Multiple label occurence error"); 283 283 } 284 284 } … … 291 291 return it->second; 292 292 } else { 293 throw DataFormatError("Invalid IDerror");293 throw DataFormatError("Invalid label error"); 294 294 } 295 295 } … … 371 371 372 372 template <typename _Item> 373 class IdReaderBase {373 class LabelReaderBase { 374 374 public: 375 375 typedef _Item Item; 376 virtual ~ IdReaderBase() {}376 virtual ~LabelReaderBase() {} 377 377 virtual Item read(std::istream& is) const = 0; 378 virtual bool is IdReader() const = 0;379 }; 380 381 template <typename _Item, typename _Boxed IdReader>382 class IdReader : public IdReaderBase<_Item> {378 virtual bool isLabelReader() const = 0; 379 }; 380 381 template <typename _Item, typename _BoxedLabelReader> 382 class LabelReader : public LabelReaderBase<_Item> { 383 383 public: 384 384 typedef _Item Item; 385 typedef _Boxed IdReader BoxedIdReader;385 typedef _BoxedLabelReader BoxedLabelReader; 386 386 387 const Boxed IdReader& boxedIdReader;388 389 IdReader(const BoxedIdReader& _boxedIdReader)390 : boxed IdReader(_boxedIdReader) {}387 const BoxedLabelReader& boxedLabelReader; 388 389 LabelReader(const BoxedLabelReader& _boxedLabelReader) 390 : boxedLabelReader(_boxedLabelReader) {} 391 391 392 392 virtual Item read(std::istream& is) const { 393 393 Item item; 394 boxed IdReader.readId(is, item);394 boxedLabelReader.readLabel(is, item); 395 395 return item; 396 396 } 397 397 398 virtual bool is IdReader() const {399 return boxed IdReader.isIdReader();398 virtual bool isLabelReader() const { 399 return boxedLabelReader.isLabelReader(); 400 400 } 401 401 }; … … 728 728 /// 729 729 /// The lemon format can store multiple graph nodesets with several maps. 730 /// The nodeset section's header line is \c \@nodeset \c nodeset_ id, but the731 /// \c nodeset_ idmay be empty.730 /// The nodeset section's header line is \c \@nodeset \c nodeset_name, but the 731 /// \c nodeset_name may be empty. 732 732 /// 733 733 /// The first line of the section contains the names of the maps separated … … 735 735 /// contains the mapped values for each map. 736 736 /// 737 /// If the nodeset contains an \c " id" named map then it will be regarded737 /// If the nodeset contains an \c "label" named map then it will be regarded 738 738 /// as id map. This map should contain only unique values and when the 739 /// \c read Id() member will read a value from the given stream it will739 /// \c readLabel() member will read a value from the given stream it will 740 740 /// give back that node which is mapped to this value. 741 741 /// … … 756 756 /// attach it into the given LemonReader. The nodeset reader will 757 757 /// add the readed nodes to the given Graph. The reader will read 758 /// the section when the \c section_ id and the \c _idare the same.758 /// the section when the \c section_name and the \c _name are the same. 759 759 NodeSetReader(LemonReader& _reader, 760 760 Graph& _graph, 761 const std::string& _ id= std::string(),761 const std::string& _name = std::string(), 762 762 const DefaultSkipper& _skipper = DefaultSkipper()) 763 : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {}763 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {} 764 764 765 765 … … 856 856 /// 857 857 /// It gives back true when the header line starts with \c \@nodeset, 858 /// and the header line's id and the nodeset's idare the same.858 /// and the header line's name and the nodeset's name are the same. 859 859 virtual bool header(const std::string& line) { 860 860 std::istringstream ls(line); 861 861 std::string command; 862 std::string name;862 std::string id; 863 863 ls >> command >> name; 864 864 return command == "@nodeset" && name == id; … … 873 873 874 874 getline(is, line); 875 std::istringstream ls(line); 875 std::istringstream ls(line); 876 std::string id; 876 877 while (ls >> id) { 877 878 typename MapReaders::iterator it = readers.find(id); … … 882 883 index.push_back(&skipper); 883 884 } 884 if (id == " id" && inverter.get() == 0) {885 if (id == "label" || (id =="id" && inverter.get() == 0)) { 885 886 inverter.reset(index.back()->getInverter()); 886 887 index.back() = inverter.get(); … … 906 907 public: 907 908 908 /// \brief Returns true if the nodeset can give back the node by its id.909 /// 910 /// Returns true if the nodeset can give back the node by its id.911 /// It is possible only if an " id" named map was read.912 bool is IdReader() const {909 /// \brief Returns true if the nodeset can give back the node by its label. 910 /// 911 /// Returns true if the nodeset can give back the node by its label. 912 /// It is possible only if an "label" named map was read. 913 bool isLabelReader() const { 913 914 return inverter.get() != 0; 914 915 } 915 916 916 /// \brief Gives back the node by its id.917 /// \brief Gives back the node by its label. 917 918 /// 918 919 /// It reads an id from the stream and gives back which node belongs to 919 /// it. It is possible only if there was read an " id" named map.920 void read Id(std::istream& is, Node& node) const {920 /// it. It is possible only if there was read an "label" named map. 921 void readLabel(std::istream& is, Node& node) const { 921 922 node = inverter->read(is); 922 923 } … … 928 929 929 930 Graph& graph; 930 std::string id;931 std::string name; 931 932 _reader_bits::SkipReader<Node, DefaultSkipper> skipper; 932 933 … … 938 939 /// 939 940 /// The lemon format can store multiple graph edgesets with several maps. 940 /// The edgeset section's header line is \c \@edgeset \c edgeset_ id, but the941 /// \c edgeset_ idmay be empty.941 /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but the 942 /// \c edgeset_name may be empty. 942 943 /// 943 944 /// The first line of the section contains the names of the maps separated … … 946 947 /// values for each map. 947 948 /// 948 /// If the edgeset contains an \c " id" named map then it will be regarded949 /// If the edgeset contains an \c "label" named map then it will be regarded 949 950 /// as id map. This map should contain only unique values and when the 950 /// \c read Id() member will read a value from the given stream it will951 /// \c readLabel() member will read a value from the given stream it will 951 952 /// give back that edge which is mapped to this value. 952 953 /// 953 954 /// The edgeset reader needs a node id reader to identify which nodes 954 /// have to be connected. If a NodeSetReader reads an " id" named map,955 /// have to be connected. If a NodeSetReader reads an "label" named map, 955 956 /// it will be able to resolve the nodes by ids. 956 957 /// … … 973 974 /// add the readed edges to the given Graph. It will use the given 974 975 /// node id reader to read the source and target nodes of the edges. 975 /// The reader will read the section only if the \c _ idand the976 /// \c edgset_ idare the same.977 template <typename Node IdReader>976 /// The reader will read the section only if the \c _name and the 977 /// \c edgset_name are the same. 978 template <typename NodeLabelReader> 978 979 EdgeSetReader(LemonReader& _reader, 979 980 Graph& _graph, 980 const Node IdReader& _nodeIdReader,981 const std::string& _ id= std::string(),981 const NodeLabelReader& _nodeLabelReader, 982 const std::string& _name = std::string(), 982 983 const DefaultSkipper& _skipper = DefaultSkipper()) 983 : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {984 checkConcept<_reader_bits::Item IdReader<Node>, NodeIdReader>();985 node IdReader.reset(new _reader_bits::986 IdReader<Node, NodeIdReader>(_nodeIdReader));984 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { 985 checkConcept<_reader_bits::ItemLabelReader<Node>, NodeLabelReader>(); 986 nodeLabelReader.reset(new _reader_bits:: 987 LabelReader<Node, NodeLabelReader>(_nodeLabelReader)); 987 988 } 988 989 /// \brief Destructor. … … 1078 1079 /// 1079 1080 /// It gives back true when the header line starts with \c \@edgeset, 1080 /// and the header line's id and the edgeset's idare the same.1081 /// and the header line's name and the edgeset's name are the same. 1081 1082 virtual bool header(const std::string& line) { 1082 1083 std::istringstream ls(line); 1083 1084 std::string command; 1084 std::string name;1085 std::string id; 1085 1086 ls >> command >> name; 1086 1087 return command == "@edgeset" && name == id; … … 1091 1092 /// It reads the content of the section. 1092 1093 virtual void read(std::istream& is) { 1093 if (!node IdReader->isIdReader()) {1094 throw DataFormatError("Cannot find nodeset or IDmap");1094 if (!nodeLabelReader->isLabelReader()) { 1095 throw DataFormatError("Cannot find nodeset or label map"); 1095 1096 } 1096 1097 std::vector<_reader_bits::MapReaderBase<Edge>* > index; … … 1099 1100 getline(is, line); 1100 1101 std::istringstream ls(line); 1102 std::string id; 1101 1103 while (ls >> id) { 1102 1104 typename MapReaders::iterator it = readers.find(id); … … 1107 1109 index.push_back(&skipper); 1108 1110 } 1109 if (id == " id" && inverter.get() == 0) {1111 if (id == "label" || (id =="id" && inverter.get() == 0)) { 1110 1112 inverter.reset(index.back()->getInverter()); 1111 1113 index.back() = inverter.get(); … … 1122 1124 while (getline(is, line)) { 1123 1125 std::istringstream ls(line); 1124 Node from = node IdReader->read(ls);1125 Node to = node IdReader->read(ls);1126 Node from = nodeLabelReader->read(ls); 1127 Node to = nodeLabelReader->read(ls); 1126 1128 Edge edge = graph.addEdge(from, to); 1127 1129 for (int i = 0; i < (int)index.size(); ++i) { … … 1133 1135 public: 1134 1136 1135 /// \brief Returns true if the edgeset can give back the edge by its id.1136 /// 1137 /// Returns true if the edgeset can give back the edge by its id.1138 /// It is possible only if an " id" named map was read.1139 bool is IdReader() const {1137 /// \brief Returns true if the edgeset can give back the edge by its label. 1138 /// 1139 /// Returns true if the edgeset can give back the edge by its label. 1140 /// It is possible only if an "label" named map was read. 1141 bool isLabelReader() const { 1140 1142 return inverter.get() != 0; 1141 1143 } 1142 1144 1143 /// \brief Gives back the edge by its id.1145 /// \brief Gives back the edge by its label. 1144 1146 /// 1145 1147 /// It reads an id from the stream and gives back which edge belongs to 1146 /// it. It is possible only if there was read an " id" named map.1147 void read Id(std::istream& is, Edge& edge) const {1148 /// it. It is possible only if there was read an "label" named map. 1149 void readLabel(std::istream& is, Edge& edge) const { 1148 1150 edge = inverter->read(is); 1149 1151 } … … 1155 1157 1156 1158 Graph& graph; 1157 std::string id;1159 std::string name; 1158 1160 _reader_bits::SkipReader<Edge, DefaultSkipper> skipper; 1159 1161 1160 1162 std::auto_ptr<_reader_bits::MapInverterBase<Edge> > inverter; 1161 std::auto_ptr<_reader_bits:: IdReaderBase<Node> > nodeIdReader;1163 std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader; 1162 1164 }; 1163 1165 … … 1167 1169 /// The lemon format can store multiple undirected edgesets with several 1168 1170 /// maps. The undirected edgeset section's header line is \c \@undiredgeset 1169 /// \c undiredgeset_ id, but the \c undiredgeset_idmay be empty.1171 /// \c undiredgeset_name, but the \c undiredgeset_name may be empty. 1170 1172 /// 1171 1173 /// The first line of the section contains the names of the maps separated … … 1179 1181 /// difference. 1180 1182 /// 1181 /// If the edgeset contains an \c " id" named map then it will be regarded1183 /// If the edgeset contains an \c "label" named map then it will be regarded 1182 1184 /// as id map. This map should contain only unique values and when the 1183 /// \c read Id() member will read a value from the given stream it will1185 /// \c readLabel() member will read a value from the given stream it will 1184 1186 /// give back that undiricted edge which is mapped to this value. 1185 1187 /// 1186 1188 /// The undirected edgeset reader needs a node id reader to identify which 1187 /// nodes have to be connected. If a NodeSetReader reads an " id" named map,1189 /// nodes have to be connected. If a NodeSetReader reads an "label" named map, 1188 1190 /// it will be able to resolve the nodes by ids. 1189 1191 /// … … 1208 1210 /// will use the given node id reader to read the source and target 1209 1211 /// nodes of the edges. The reader will read the section only if the 1210 /// \c _ id and the \c undiredgset_idare the same.1211 template <typename Node IdReader>1212 /// \c _name and the \c undiredgset_name are the same. 1213 template <typename NodeLabelReader> 1212 1214 UndirEdgeSetReader(LemonReader& _reader, 1213 1215 Graph& _graph, 1214 const Node IdReader& _nodeIdReader,1215 const std::string& _ id= std::string(),1216 const NodeLabelReader& _nodeLabelReader, 1217 const std::string& _name = std::string(), 1216 1218 const DefaultSkipper& _skipper = DefaultSkipper()) 1217 : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {1218 checkConcept<_reader_bits::Item IdReader<Node>, NodeIdReader>();1219 node IdReader.reset(new _reader_bits::1220 IdReader<Node, NodeIdReader>(_nodeIdReader));1219 : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { 1220 checkConcept<_reader_bits::ItemLabelReader<Node>, NodeLabelReader>(); 1221 nodeLabelReader.reset(new _reader_bits:: 1222 LabelReader<Node, NodeLabelReader>(_nodeLabelReader)); 1221 1223 } 1222 1224 /// \brief Destructor. … … 1373 1375 /// 1374 1376 /// It gives back true when the header line starts with \c \@undiredgeset, 1375 /// and the header line's id and the edgeset's idare the same.1377 /// and the header line's name and the edgeset's name are the same. 1376 1378 virtual bool header(const std::string& line) { 1377 1379 std::istringstream ls(line); 1378 1380 std::string command; 1379 std::string name;1381 std::string id; 1380 1382 ls >> command >> name; 1381 1383 return command == "@undiredgeset" && name == id; … … 1386 1388 /// It reads the content of the section. 1387 1389 virtual void read(std::istream& is) { 1388 if (!node IdReader->isIdReader()) {1389 throw DataFormatError("Cannot find nodeset or IDmap");1390 if (!nodeLabelReader->isLabelReader()) { 1391 throw DataFormatError("Cannot find nodeset or label map"); 1390 1392 } 1391 1393 std::vector<_reader_bits::MapReaderBase<UndirEdge>* > index; … … 1394 1396 getline(is, line); 1395 1397 std::istringstream ls(line); 1398 std::string id; 1396 1399 while (ls >> id) { 1397 1400 typename MapReaders::iterator it = readers.find(id); … … 1402 1405 index.push_back(&skipper); 1403 1406 } 1404 if (id == " id" && inverter.get() == 0) {1407 if (id == "label" || (id =="id" && inverter.get() == 0)) { 1405 1408 inverter.reset(index.back()->getInverter()); 1406 1409 index.back() = inverter.get(); … … 1417 1420 while (getline(is, line)) { 1418 1421 std::istringstream ls(line); 1419 Node from = node IdReader->read(ls);1420 Node to = node IdReader->read(ls);1422 Node from = nodeLabelReader->read(ls); 1423 Node to = nodeLabelReader->read(ls); 1421 1424 UndirEdge edge = graph.addEdge(from, to); 1422 1425 for (int i = 0; i < (int)index.size(); ++i) { … … 1428 1431 public: 1429 1432 1430 /// \brief Returns true if the edgeset can give back the edge by its id.1433 /// \brief Returns true if the edgeset can give back the edge by its label. 1431 1434 /// 1432 1435 /// Returns true if the edgeset can give back the undirected edge by its 1433 /// id. It is possible only if an " id" named map was read.1434 bool is IdReader() const {1436 /// id. It is possible only if an "label" named map was read. 1437 bool isLabelReader() const { 1435 1438 return inverter.get() != 0; 1436 1439 } 1437 1440 1438 /// \brief Gives back the undirected edge by its id.1441 /// \brief Gives back the undirected edge by its label. 1439 1442 /// 1440 1443 /// It reads an id from the stream and gives back which undirected edge 1441 /// belongs to it. It is possible only if there was read an " id" named map.1442 void read Id(std::istream& is, UndirEdge& undirEdge) const {1444 /// belongs to it. It is possible only if there was read an "label" named map. 1445 void readLabel(std::istream& is, UndirEdge& undirEdge) const { 1443 1446 undirEdge = inverter->read(is); 1444 1447 } 1445 1448 1446 /// \brief Gives back the directed edge by its id.1449 /// \brief Gives back the directed edge by its label. 1447 1450 /// 1448 1451 /// It reads an id from the stream and gives back which directed edge 1449 1452 /// belongs to it. The directed edge id is the \c '+' or \c '-' character 1450 1453 /// and the undirected edge id. It is possible only if there was read 1451 /// an " id" named map.1452 void read Id(std::istream& is, Edge& edge) const {1454 /// an "label" named map. 1455 void readLabel(std::istream& is, Edge& edge) const { 1453 1456 char c; 1454 1457 is >> c; … … 1471 1474 1472 1475 Graph& graph; 1473 std::string id;1476 std::string name; 1474 1477 _reader_bits::SkipReader<UndirEdge, DefaultSkipper> skipper; 1475 1478 1476 1479 std::auto_ptr<_reader_bits::MapInverterBase<UndirEdge> > inverter; 1477 std::auto_ptr<_reader_bits:: IdReaderBase<Node> > nodeIdReader;1480 std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader; 1478 1481 }; 1479 1482 … … 1481 1484 /// \brief SectionReader for reading labeled nodes. 1482 1485 /// 1483 /// The nodes section's header line is \c \@nodes \c nodes_ id, but the1484 /// \c nodes_ idmay be empty.1486 /// The nodes section's header line is \c \@nodes \c nodes_name, but the 1487 /// \c nodes_name may be empty. 1485 1488 /// 1486 1489 /// Each line in the section contains the name of the node … … 1500 1503 /// attach it into the given LemonReader. It will use the given 1501 1504 /// node id reader to give back the nodes. The reader will read the 1502 /// section only if the \c _ id and the \c nodes_idare the same.1503 template <typename _ IdReader>1504 NodeReader(LemonReader& _reader, const _ IdReader& _idReader,1505 const std::string& _ id= std::string())1506 : Parent(_reader), id(_id) {1507 checkConcept<_reader_bits::Item IdReader<Node>, _IdReader>();1508 node IdReader.reset(new _reader_bits::1509 IdReader<Node, _IdReader>(_idReader));1505 /// section only if the \c _name and the \c nodes_name are the same. 1506 template <typename _LabelReader> 1507 NodeReader(LemonReader& _reader, const _LabelReader& _labelReader, 1508 const std::string& _name = std::string()) 1509 : Parent(_reader), name(_name) { 1510 checkConcept<_reader_bits::ItemLabelReader<Node>, _LabelReader>(); 1511 nodeLabelReader.reset(new _reader_bits:: 1512 LabelReader<Node, _LabelReader>(_labelReader)); 1510 1513 } 1511 1514 … … 1539 1542 /// 1540 1543 /// It gives back true when the header line start with \c \@nodes, 1541 /// and the header line's id and the reader's idare the same.1544 /// and the header line's name and the reader's name are the same. 1542 1545 virtual bool header(const std::string& line) { 1543 1546 std::istringstream ls(line); 1544 1547 std::string command; 1545 std::string name;1548 std::string id; 1546 1549 ls >> command >> name; 1547 1550 return command == "@nodes" && name == id; … … 1552 1555 /// It reads the content of the section. 1553 1556 virtual void read(std::istream& is) { 1554 if (!node IdReader->isIdReader()) {1555 throw DataFormatError("Cannot find nodeset or IDmap");1557 if (!nodeLabelReader->isLabelReader()) { 1558 throw DataFormatError("Cannot find nodeset or label map"); 1556 1559 } 1557 1560 std::string line; … … 1562 1565 typename NodeReaders::iterator it = readers.find(id); 1563 1566 if (it != readers.end()) { 1564 it->second.read(node IdReader->read(ls));1567 it->second.read(nodeLabelReader->read(ls)); 1565 1568 it->second.touch(); 1566 1569 } … … 1578 1581 private: 1579 1582 1580 std::string id;1583 std::string name; 1581 1584 1582 1585 typedef std::map<std::string, _reader_bits::ItemStore<Node> > NodeReaders; 1583 1586 NodeReaders readers; 1584 std::auto_ptr<_reader_bits:: IdReaderBase<Node> > nodeIdReader;1587 std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader; 1585 1588 }; 1586 1589 … … 1588 1591 /// \brief SectionReader for reading labeled edges. 1589 1592 /// 1590 /// The edges section's header line is \c \@edges \c edges_ id, but the1591 /// \c edges_ idmay be empty.1593 /// The edges section's header line is \c \@edges \c edges_name, but the 1594 /// \c edges_name may be empty. 1592 1595 /// 1593 1596 /// Each line in the section contains the name of the edge … … 1607 1610 /// attach it into the given LemonReader. It will use the given 1608 1611 /// edge id reader to give back the edges. The reader will read the 1609 /// section only if the \c _ id and the \c edges_idare the same.1610 template <typename _ IdReader>1611 EdgeReader(LemonReader& _reader, const _ IdReader& _idReader,1612 const std::string& _ id= std::string())1613 : Parent(_reader), id(_id) {1614 checkConcept<_reader_bits::Item IdReader<Edge>, _IdReader>();1615 edge IdReader.reset(new _reader_bits::1616 IdReader<Edge, _IdReader>(_idReader));1612 /// section only if the \c _name and the \c edges_name are the same. 1613 template <typename _LabelReader> 1614 EdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 1615 const std::string& _name = std::string()) 1616 : Parent(_reader), name(_name) { 1617 checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>(); 1618 edgeLabelReader.reset(new _reader_bits:: 1619 LabelReader<Edge, _LabelReader>(_labelReader)); 1617 1620 } 1618 1621 … … 1645 1648 /// 1646 1649 /// It gives back true when the header line start with \c \@edges, 1647 /// and the header line's id and the reader's idare the same.1650 /// and the header line's name and the reader's name are the same. 1648 1651 virtual bool header(const std::string& line) { 1649 1652 std::istringstream ls(line); 1650 1653 std::string command; 1651 std::string name;1654 std::string id; 1652 1655 ls >> command >> name; 1653 1656 return command == "@edges" && name == id; … … 1658 1661 /// It reads the content of the section. 1659 1662 virtual void read(std::istream& is) { 1660 if (!edge IdReader->isIdReader()) {1661 throw DataFormatError("Cannot find edgeset or IDmap");1663 if (!edgeLabelReader->isLabelReader()) { 1664 throw DataFormatError("Cannot find edgeset or label map"); 1662 1665 } 1663 1666 std::string line; … … 1668 1671 typename EdgeReaders::iterator it = readers.find(id); 1669 1672 if (it != readers.end()) { 1670 it->second.read(edge IdReader->read(ls));1673 it->second.read(edgeLabelReader->read(ls)); 1671 1674 it->second.touch(); 1672 1675 } … … 1684 1687 private: 1685 1688 1686 std::string id;1689 std::string name; 1687 1690 1688 1691 typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders; 1689 1692 EdgeReaders readers; 1690 std::auto_ptr<_reader_bits:: IdReaderBase<Edge> > edgeIdReader;1693 std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader; 1691 1694 }; 1692 1695 … … 1695 1698 /// 1696 1699 /// The undirected edges section's header line is \c \@undiredges 1697 /// \c undiredges_ id, but the \c undiredges_idmay be empty.1700 /// \c undiredges_name, but the \c undiredges_name may be empty. 1698 1701 /// 1699 1702 /// Each line in the section contains the name of the undirected edge … … 1714 1717 /// attach it into the given LemonReader. It will use the given 1715 1718 /// undirected edge id reader to give back the edges. The reader will 1716 /// read the section only if the \c _ id and the \c undiredges_idare1719 /// read the section only if the \c _name and the \c undiredges_name are 1717 1720 /// the same. 1718 template <typename _ IdReader>1719 UndirEdgeReader(LemonReader& _reader, const _ IdReader& _idReader,1720 const std::string& _ id= std::string())1721 : Parent(_reader), id(_id) {1722 checkConcept<_reader_bits::Item IdReader<UndirEdge>, _IdReader>();1723 checkConcept<_reader_bits::Item IdReader<Edge>, _IdReader>();1724 undirEdge IdReader.reset(new _reader_bits::1725 IdReader<UndirEdge, _IdReader>(_idReader));1726 edge IdReader.reset(new _reader_bits::1727 IdReader<Edge, _IdReader>(_idReader));1721 template <typename _LabelReader> 1722 UndirEdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 1723 const std::string& _name = std::string()) 1724 : Parent(_reader), name(_name) { 1725 checkConcept<_reader_bits::ItemLabelReader<UndirEdge>, _LabelReader>(); 1726 checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>(); 1727 undirEdgeLabelReader.reset(new _reader_bits:: 1728 LabelReader<UndirEdge, _LabelReader>(_labelReader)); 1729 edgeLabelReader.reset(new _reader_bits:: 1730 LabelReader<Edge, _LabelReader>(_labelReader)); 1728 1731 } 1729 1732 … … 1769 1772 /// 1770 1773 /// It gives back true when the header line start with \c \@edges, 1771 /// and the header line's id and the reader's idare the same.1774 /// and the header line's name and the reader's name are the same. 1772 1775 virtual bool header(const std::string& line) { 1773 1776 std::istringstream ls(line); 1774 1777 std::string command; 1775 std::string name;1778 std::string id; 1776 1779 ls >> command >> name; 1777 1780 return command == "@undiredges" && name == id; … … 1782 1785 /// It reads the content of the section. 1783 1786 virtual void read(std::istream& is) { 1784 if (!edge IdReader->isIdReader()) {1785 throw DataFormatError("Cannot find undirected edgeset or IDmap");1786 } 1787 if (!undirEdge IdReader->isIdReader()) {1788 throw DataFormatError("Cannot find undirected edgeset or IDmap");1787 if (!edgeLabelReader->isLabelReader()) { 1788 throw DataFormatError("Cannot find undirected edgeset or label map"); 1789 } 1790 if (!undirEdgeLabelReader->isLabelReader()) { 1791 throw DataFormatError("Cannot find undirected edgeset or label map"); 1789 1792 } 1790 1793 std::string line; … … 1796 1799 typename UndirEdgeReaders::iterator it = undirEdgeReaders.find(id); 1797 1800 if (it != undirEdgeReaders.end()) { 1798 it->second.read(undirEdge IdReader->read(ls));1801 it->second.read(undirEdgeLabelReader->read(ls)); 1799 1802 it->second.touch(); 1800 1803 continue; … … 1803 1806 typename EdgeReaders::iterator it = edgeReaders.find(id); 1804 1807 if (it != edgeReaders.end()) { 1805 it->second.read(edge IdReader->read(ls));1808 it->second.read(edgeLabelReader->read(ls)); 1806 1809 it->second.touch(); 1807 1810 continue; … … 1829 1832 private: 1830 1833 1831 std::string id;1834 std::string name; 1832 1835 1833 1836 typedef std::map<std::string, 1834 1837 _reader_bits::ItemStore<UndirEdge> > UndirEdgeReaders; 1835 1838 UndirEdgeReaders undirEdgeReaders; 1836 std::auto_ptr<_reader_bits:: IdReaderBase<UndirEdge> > undirEdgeIdReader;1839 std::auto_ptr<_reader_bits::LabelReaderBase<UndirEdge> > undirEdgeLabelReader; 1837 1840 1838 1841 typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders; 1839 1842 EdgeReaders edgeReaders; 1840 std::auto_ptr<_reader_bits:: IdReaderBase<Edge> > edgeIdReader;1843 std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader; 1841 1844 }; 1842 1845 … … 1845 1848 /// 1846 1849 /// The lemon format can store multiple attribute set. Each set has 1847 /// the header line \c \@attributes \c attributeset_ id, but the1848 /// attributeset_ idmay be empty.1850 /// the header line \c \@attributes \c attributeset_name, but the 1851 /// attributeset_name may be empty. 1849 1852 /// 1850 1853 /// The attributeset section contains several lines. Each of them starts … … 1861 1864 /// Constructor for AttributeReader. It creates the AttributeReader and 1862 1865 /// attach it into the given LemonReader. The reader process a section 1863 /// only if the \c section_ id and the \c _idare the same.1866 /// only if the \c section_name and the \c _name are the same. 1864 1867 AttributeReader(LemonReader& _reader, 1865 const std::string& _ id= std::string())1866 : Parent(_reader), id(_id) {}1868 const std::string& _name = std::string()) 1869 : Parent(_reader), name(_name) {} 1867 1870 1868 1871 /// \brief Destructor. … … 1917 1920 std::istringstream ls(line); 1918 1921 std::string command; 1919 std::string name;1922 std::string id; 1920 1923 ls >> command >> name; 1921 1924 return command == "@attributes" && name == id; … … 1934 1937 if (it != readers.end()) { 1935 1938 it->second->read(ls); 1936 it->second->touch();1939 it->second->touch(); 1937 1940 } 1938 1941 } … … 1948 1951 1949 1952 private: 1950 std::string id;1953 std::string name; 1951 1954 1952 1955 typedef std::map<std::string, _reader_bits::ValueReaderBase*> Readers; … … 2204 2207 2205 2208 void readMapNames(std::istream& is, std::vector<std::string>& maps) { 2206 std::string line, id;2209 std::string line, name; 2207 2210 std::getline(is, line); 2208 2211 std::istringstream ls(line); 2209 while (ls >> id) {2210 maps.push_back( id);2212 while (ls >> name) { 2213 maps.push_back(name); 2211 2214 } 2212 2215 while (getline(is, line)); … … 2214 2217 2215 2218 void readItemNames(std::istream& is, std::vector<std::string>& maps) { 2216 std::string line, id;2219 std::string line, name; 2217 2220 while (std::getline(is, line)) { 2218 2221 std::istringstream ls(line); 2219 ls >> id;2220 maps.push_back( id);2222 ls >> name; 2223 maps.push_back(name); 2221 2224 } 2222 2225 }
Note: See TracChangeset
for help on using the changeset viewer.