lemon/lemon_reader.h
changeset 1476 182da222fceb
parent 1435 8e85e6bbefdf
child 1477 0d32f7947a00
equal deleted inserted replaced
0:c3b6e0f2feab 1:107a4dd85529
    34 #include <lemon/error.h>
    34 #include <lemon/error.h>
    35 #include <lemon/graph_utils.h>
    35 #include <lemon/graph_utils.h>
    36 #include <lemon/utility.h>
    36 #include <lemon/utility.h>
    37 #include <lemon/bits/item_reader.h>
    37 #include <lemon/bits/item_reader.h>
    38 
    38 
       
    39 #include <lemon/concept_check.h>
       
    40 #include <lemon/concept/maps.h>
    39 
    41 
    40 namespace lemon {
    42 namespace lemon {
    41 
    43 
    42   namespace _reader_bits {
    44   namespace _reader_bits {
       
    45 
       
    46     template <typename Item>
       
    47     class ItemIdReader {
       
    48     public:
       
    49 
       
    50       bool isIdReader() { return true; }
       
    51 
       
    52       Item readId(std::istream&, Item) { return Item();}
       
    53       
       
    54       template <class _ItemIdReader>
       
    55       struct Constraints {
       
    56 	void constraints() {
       
    57 	  bool b = reader.isIdReader();
       
    58 	  ignore_unused_variable_warning(b);
       
    59 	  Item item = reader.readId(is, Item());
       
    60 	}
       
    61 	_ItemIdReader& reader;
       
    62 	std::istream& is;
       
    63       };
       
    64 
       
    65     };
    43   
    66   
    44     template <typename T>
    67     template <typename T>
    45     bool operator<(T, T) {
    68     bool operator<(T, T) {
    46       throw DataFormatError("Id is not comparable");
    69       throw DataFormatError("Id is not comparable");
    47     }
    70     }
   529     template <typename _Item>
   552     template <typename _Item>
   530     class IdReaderBase {
   553     class IdReaderBase {
   531     public:
   554     public:
   532       typedef _Item Item;
   555       typedef _Item Item;
   533       virtual Item read(std::istream& is) const = 0;
   556       virtual Item read(std::istream& is) const = 0;
       
   557       virtual bool isIdReader() const = 0;
   534     };
   558     };
   535 
   559 
   536     template <typename _Item, typename _BoxedIdReader>
   560     template <typename _Item, typename _BoxedIdReader>
   537     class IdReader : public IdReaderBase<_Item> {
   561     class IdReader : public IdReaderBase<_Item> {
   538     public:
   562     public:
   544       IdReader(const BoxedIdReader& _boxedIdReader) 
   568       IdReader(const BoxedIdReader& _boxedIdReader) 
   545 	: boxedIdReader(_boxedIdReader) {}
   569 	: boxedIdReader(_boxedIdReader) {}
   546 
   570 
   547       virtual Item read(std::istream& is) const {
   571       virtual Item read(std::istream& is) const {
   548 	return boxedIdReader.readId(is, Item());
   572 	return boxedIdReader.readId(is, Item());
       
   573       }
       
   574 
       
   575       virtual bool isIdReader() const {
       
   576 	return boxedIdReader.isIdReader();
   549       }
   577       }
   550     };
   578     };
   551 
   579 
   552     class ValueReaderBase {
   580     class ValueReaderBase {
   553     public:
   581     public:
   815     EdgeSetReader(LemonReader& _reader, 
   843     EdgeSetReader(LemonReader& _reader, 
   816 		  typename SmartParameter<Graph>::Type _graph, 
   844 		  typename SmartParameter<Graph>::Type _graph, 
   817 		  const NodeIdReader& _nodeIdReader, 
   845 		  const NodeIdReader& _nodeIdReader, 
   818 		  const std::string& _id = std::string(),
   846 		  const std::string& _id = std::string(),
   819 		  const DefaultSkipper& _skipper = DefaultSkipper()) 
   847 		  const DefaultSkipper& _skipper = DefaultSkipper()) 
   820       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
   848       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
   821 	nodeIdReader(new IdReader<Node, NodeIdReader>(_nodeIdReader)) {} 
   849       checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
   822 
   850       nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
       
   851     }
   823     /// \brief Destructor.
   852     /// \brief Destructor.
   824     ///
   853     ///
   825     /// Destructor for EdgeSetReader.
   854     /// Destructor for EdgeSetReader.
   826     virtual ~EdgeSetReader() {
   855     virtual ~EdgeSetReader() {
   827       for (typename MapReaders::iterator it = readers.begin(); 
   856       for (typename MapReaders::iterator it = readers.begin(); 
   919 
   948 
   920     /// \brief Reader function of the section.
   949     /// \brief Reader function of the section.
   921     ///
   950     ///
   922     /// It reads the content of the section.
   951     /// It reads the content of the section.
   923     virtual void read(std::istream& is) {
   952     virtual void read(std::istream& is) {
       
   953       if (!nodeIdReader->isIdReader()) {
       
   954 	throw DataFormatError("Cannot find nodeset or ID map");
       
   955       }
   924       std::vector<ReaderBase<Edge>* > index;
   956       std::vector<ReaderBase<Edge>* > index;
   925       std::string line;
   957       std::string line;
   926 
   958 
   927       getline(is, line);
   959       getline(is, line);
   928       std::istringstream ls(line);	
   960       std::istringstream ls(line);	
  1031     UndirEdgeSetReader(LemonReader& _reader, 
  1063     UndirEdgeSetReader(LemonReader& _reader, 
  1032 		       typename SmartParameter<Graph>::Type _graph, 
  1064 		       typename SmartParameter<Graph>::Type _graph, 
  1033 		       const NodeIdReader& _nodeIdReader, 
  1065 		       const NodeIdReader& _nodeIdReader, 
  1034 		       const std::string& _id = std::string(),
  1066 		       const std::string& _id = std::string(),
  1035 		       const DefaultSkipper& _skipper = DefaultSkipper()) 
  1067 		       const DefaultSkipper& _skipper = DefaultSkipper()) 
  1036       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
  1068       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
  1037 	nodeIdReader(new IdReader<Node, NodeIdReader>(_nodeIdReader)) {} 
  1069       checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
  1038 
  1070       nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
       
  1071     }
  1039     /// \brief Destructor.
  1072     /// \brief Destructor.
  1040     ///
  1073     ///
  1041     /// Destructor for UndirEdgeSetReader.
  1074     /// Destructor for UndirEdgeSetReader.
  1042     virtual ~UndirEdgeSetReader() {
  1075     virtual ~UndirEdgeSetReader() {
  1043       for (typename MapReaders::iterator it = readers.begin(); 
  1076       for (typename MapReaders::iterator it = readers.begin(); 
  1195 
  1228 
  1196     /// \brief Reader function of the section.
  1229     /// \brief Reader function of the section.
  1197     ///
  1230     ///
  1198     /// It reads the content of the section.
  1231     /// It reads the content of the section.
  1199     virtual void read(std::istream& is) {
  1232     virtual void read(std::istream& is) {
       
  1233       if (!nodeIdReader->isIdReader()) {
       
  1234 	throw DataFormatError("Cannot find nodeset or ID map");
       
  1235       }
  1200       std::vector<ReaderBase<UndirEdge>* > index;
  1236       std::vector<ReaderBase<UndirEdge>* > index;
  1201       std::string line;
  1237       std::string line;
  1202 
  1238 
  1203       getline(is, line);
  1239       getline(is, line);
  1204       std::istringstream ls(line);	
  1240       std::istringstream ls(line);	
  1300     /// node id reader to give back the nodes. The reader will read the 
  1336     /// node id reader to give back the nodes. The reader will read the 
  1301     /// section only if the \c _id and the \c nodes_id are the same. 
  1337     /// section only if the \c _id and the \c nodes_id are the same. 
  1302     template <typename _IdReader>
  1338     template <typename _IdReader>
  1303     NodeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1339     NodeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1304 	       const std::string& _id = std::string()) 
  1340 	       const std::string& _id = std::string()) 
  1305       : Parent(_reader), id(_id), 
  1341       : Parent(_reader), id(_id) {
  1306 	idReader(new IdReader<typename Graph::Node, _IdReader>(_idReader)) {} 
  1342       checkConcept<_reader_bits::ItemIdReader<Node>, _IdReader>();
       
  1343       idReader.reset(new IdReader<Node, _IdReader>(_idReader));
       
  1344     }
  1307 
  1345 
  1308     /// \brief Destructor.
  1346     /// \brief Destructor.
  1309     ///
  1347     ///
  1310     /// Destructor for NodeReader.
  1348     /// Destructor for NodeReader.
  1311     virtual ~NodeReader() {}
  1349     virtual ~NodeReader() {}
  1345 
  1383 
  1346     /// \brief Reader function of the section.
  1384     /// \brief Reader function of the section.
  1347     ///
  1385     ///
  1348     /// It reads the content of the section.
  1386     /// It reads the content of the section.
  1349     virtual void read(std::istream& is) {
  1387     virtual void read(std::istream& is) {
       
  1388       if (!idReader->isIdReader()) {
       
  1389 	throw DataFormatError("Cannot find nodeset or ID map");
       
  1390       }
  1350       std::string line;
  1391       std::string line;
  1351       while (getline(is, line)) {
  1392       while (getline(is, line)) {
  1352 	std::istringstream ls(line);
  1393 	std::istringstream ls(line);
  1353 	std::string id;
  1394 	std::string id;
  1354 	ls >> id;
  1395 	ls >> id;
  1392     /// edge id reader to give back the edges. The reader will read the 
  1433     /// edge id reader to give back the edges. The reader will read the 
  1393     /// section only if the \c _id and the \c edges_id are the same. 
  1434     /// section only if the \c _id and the \c edges_id are the same. 
  1394     template <typename _IdReader>
  1435     template <typename _IdReader>
  1395     EdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1436     EdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1396 	       const std::string& _id = std::string()) 
  1437 	       const std::string& _id = std::string()) 
  1397       : Parent(_reader), id(_id), 
  1438       : Parent(_reader), id(_id) {
  1398 	idReader(new IdReader<typename Graph::Edge, _IdReader>(_idReader)) {} 
  1439       checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
       
  1440       idReader.reset(new IdReader<Edge, _IdReader>(_idReader));
       
  1441     }
  1399 
  1442 
  1400     /// \brief Destructor.
  1443     /// \brief Destructor.
  1401     ///
  1444     ///
  1402     /// Destructor for EdgeReader.
  1445     /// Destructor for EdgeReader.
  1403     virtual ~EdgeReader() {}
  1446     virtual ~EdgeReader() {}
  1436 
  1479 
  1437     /// \brief Reader function of the section.
  1480     /// \brief Reader function of the section.
  1438     ///
  1481     ///
  1439     /// It reads the content of the section.
  1482     /// It reads the content of the section.
  1440     virtual void read(std::istream& is) {
  1483     virtual void read(std::istream& is) {
       
  1484       if (!idReader->isIdReader()) {
       
  1485 	throw DataFormatError("Cannot find edgeset or ID map");
       
  1486       }
  1441       std::string line;
  1487       std::string line;
  1442       while (getline(is, line)) {
  1488       while (getline(is, line)) {
  1443 	std::istringstream ls(line);
  1489 	std::istringstream ls(line);
  1444 	std::string id;
  1490 	std::string id;
  1445 	ls >> id;
  1491 	ls >> id;
  1485     /// read the section only if the \c _id and the \c undiredges_id are 
  1531     /// read the section only if the \c _id and the \c undiredges_id are 
  1486     /// the same. 
  1532     /// the same. 
  1487     template <typename _IdReader>
  1533     template <typename _IdReader>
  1488     UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1534     UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
  1489 	       const std::string& _id = std::string()) 
  1535 	       const std::string& _id = std::string()) 
  1490       : Parent(_reader), id(_id), 
  1536       : Parent(_reader), id(_id) {
  1491 	undirEdgeIdReader(new IdReader<UndirEdge, _IdReader>(_idReader)),
  1537       checkConcept<_reader_bits::ItemIdReader<UndirEdge>, _IdReader>();
  1492 	edgeIdReader(new IdReader<Edge, _IdReader>(_idReader))
  1538       checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
  1493     {} 
  1539       undirEdgeIdReader.reset(new IdReader<UndirEdge, _IdReader>(_idReader));
       
  1540       egdeIdReader.reset(new IdReader<Edge, _IdReader>(_idReader));
       
  1541     }
  1494 
  1542 
  1495     /// \brief Destructor.
  1543     /// \brief Destructor.
  1496     ///
  1544     ///
  1497     /// Destructor for UndirEdgeReader.
  1545     /// Destructor for UndirEdgeReader.
  1498     virtual ~UndirEdgeReader() {}
  1546     virtual ~UndirEdgeReader() {}
  1543 
  1591 
  1544     /// \brief Reader function of the section.
  1592     /// \brief Reader function of the section.
  1545     ///
  1593     ///
  1546     /// It reads the content of the section.
  1594     /// It reads the content of the section.
  1547     virtual void read(std::istream& is) {
  1595     virtual void read(std::istream& is) {
       
  1596       if (!edgeIdReader->isIdReader()) {
       
  1597 	throw DataFormatError("Cannot find undirected edgeset or ID map");
       
  1598       }
       
  1599       if (!undirEdgeIdReader->isIdReader()) {
       
  1600 	throw DataFormatError("Cannot find undirected edgeset or ID map");
       
  1601       }
  1548       std::string line;
  1602       std::string line;
  1549       while (getline(is, line)) {
  1603       while (getline(is, line)) {
  1550 	std::istringstream ls(line);
  1604 	std::istringstream ls(line);
  1551 	std::string id;
  1605 	std::string id;
  1552 	ls >> id;
  1606 	ls >> id;