lemon/lemon_reader.h
changeset 1917 87d3518d73d8
parent 1901 723b2b81d900
child 1946 17eb3eaad9f8
equal deleted inserted replaced
12:028ac22cb3c4 13:b435a12f8831
   102     struct Arg { typedef Map& Type; };
   102     struct Arg { typedef Map& Type; };
   103 
   103 
   104     template <typename Graph, typename Map>
   104     template <typename Graph, typename Map>
   105     class ForwardComposeMap {
   105     class ForwardComposeMap {
   106     public:
   106     public:
   107       typedef typename Graph::UndirEdge Key;
   107       typedef typename Graph::UEdge Key;
   108       typedef typename Map::Value Value;
   108       typedef typename Map::Value Value;
   109 
   109 
   110       ForwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
   110       ForwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
   111 	: graph(_graph), map(_map) {}
   111 	: graph(_graph), map(_map) {}
   112       
   112       
   132     }
   132     }
   133 
   133 
   134     template <typename Graph, typename Map>
   134     template <typename Graph, typename Map>
   135     class BackwardComposeMap {
   135     class BackwardComposeMap {
   136     public:
   136     public:
   137       typedef typename Graph::UndirEdge Key;
   137       typedef typename Graph::UEdge Key;
   138       typedef typename Map::Value Value;
   138       typedef typename Map::Value Value;
   139 
   139 
   140       BackwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
   140       BackwardComposeMap(const Graph& _graph, typename Arg<Map>::Type _map) 
   141 	: graph(_graph), map(_map) {}
   141 	: graph(_graph), map(_map) {}
   142       
   142       
  1165 
  1165 
  1166   /// \ingroup io_group
  1166   /// \ingroup io_group
  1167   /// \brief SectionReader for reading a undirected graph's edgeset.
  1167   /// \brief SectionReader for reading a undirected graph's edgeset.
  1168   ///
  1168   ///
  1169   /// The lemon format can store multiple undirected edgesets with several 
  1169   /// The lemon format can store multiple undirected edgesets with several 
  1170   /// maps. The undirected edgeset section's header line is \c \@undiredgeset 
  1170   /// maps. The undirected edgeset section's header line is \c \@uedgeset 
  1171   /// \c undiredgeset_name, but the \c undiredgeset_name may be empty.
  1171   /// \c uedgeset_name, but the \c uedgeset_name may be empty.
  1172   ///
  1172   ///
  1173   /// The first line of the section contains the names of the maps separated
  1173   /// The first line of the section contains the names of the maps separated
  1174   /// with white spaces. Each next lines describes an edge in the edgeset. The
  1174   /// with white spaces. Each next lines describes an edge in the edgeset. The
  1175   /// line contains the connected nodes' id and the mapped values for each map.
  1175   /// line contains the connected nodes' id and the mapped values for each map.
  1176   ///
  1176   ///
  1181   /// difference.
  1181   /// difference.
  1182   ///
  1182   ///
  1183   /// If the edgeset contains an \c "label" named map then it will be regarded
  1183   /// If the edgeset contains an \c "label" named map then it will be regarded
  1184   /// as id map. This map should contain only unique values and when the 
  1184   /// as id map. This map should contain only unique values and when the 
  1185   /// \c readLabel() member will read a value from the given stream it will
  1185   /// \c readLabel() member will read a value from the given stream it will
  1186   /// give back that undiricted edge which is mapped to this value.
  1186   /// give back that uicted edge which is mapped to this value.
  1187   ///
  1187   ///
  1188   /// The undirected edgeset reader needs a node id reader to identify which 
  1188   /// The undirected edgeset reader needs a node id reader to identify which 
  1189   /// nodes have to be connected. If a NodeSetReader reads an "label" named map,
  1189   /// nodes have to be connected. If a NodeSetReader reads an "label" named map,
  1190   /// it will be able to resolve the nodes by ids.
  1190   /// it will be able to resolve the nodes by ids.
  1191   ///
  1191   ///
  1192   /// \relates LemonReader
  1192   /// \relates LemonReader
  1193   template <typename _Graph, typename _Traits = DefaultReaderTraits>
  1193   template <typename _Graph, typename _Traits = DefaultReaderTraits>
  1194   class UndirEdgeSetReader : public LemonReader::SectionReader {
  1194   class UEdgeSetReader : public LemonReader::SectionReader {
  1195     typedef LemonReader::SectionReader Parent;
  1195     typedef LemonReader::SectionReader Parent;
  1196   public:
  1196   public:
  1197 
  1197 
  1198     typedef _Graph Graph;
  1198     typedef _Graph Graph;
  1199     typedef _Traits Traits;
  1199     typedef _Traits Traits;
  1200     typedef typename Graph::Node Node;
  1200     typedef typename Graph::Node Node;
  1201     typedef typename Graph::Edge Edge;
  1201     typedef typename Graph::Edge Edge;
  1202     typedef typename Graph::UndirEdge UndirEdge;
  1202     typedef typename Graph::UEdge UEdge;
  1203     typedef typename Traits::Skipper DefaultSkipper;
  1203     typedef typename Traits::Skipper DefaultSkipper;
  1204 
  1204 
  1205     /// \brief Constructor.
  1205     /// \brief Constructor.
  1206     ///
  1206     ///
  1207     /// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader 
  1207     /// Constructor for UEdgeSetReader. It creates the UEdgeSetReader 
  1208     /// and attach it into the given LemonReader. The undirected edgeset 
  1208     /// and attach it into the given LemonReader. The undirected edgeset 
  1209     /// reader will add the readed undirected edges to the given Graph. It 
  1209     /// reader will add the readed undirected edges to the given Graph. It 
  1210     /// will use the given node id reader to read the source and target 
  1210     /// will use the given node id reader to read the source and target 
  1211     /// nodes of the edges. The reader will read the section only if the 
  1211     /// nodes of the edges. The reader will read the section only if the 
  1212     /// \c _name and the \c undiredgset_name are the same. 
  1212     /// \c _name and the \c uedgset_name are the same. 
  1213     template <typename NodeLabelReader>
  1213     template <typename NodeLabelReader>
  1214     UndirEdgeSetReader(LemonReader& _reader, 
  1214     UEdgeSetReader(LemonReader& _reader, 
  1215 		       Graph& _graph, 
  1215 		       Graph& _graph, 
  1216 		       const NodeLabelReader& _nodeLabelReader, 
  1216 		       const NodeLabelReader& _nodeLabelReader, 
  1217 		       const std::string& _name = std::string(),
  1217 		       const std::string& _name = std::string(),
  1218 		       const DefaultSkipper& _skipper = DefaultSkipper()) 
  1218 		       const DefaultSkipper& _skipper = DefaultSkipper()) 
  1219       : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
  1219       : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
  1221       nodeLabelReader.reset(new _reader_bits::
  1221       nodeLabelReader.reset(new _reader_bits::
  1222 			 LabelReader<Node, NodeLabelReader>(_nodeLabelReader));
  1222 			 LabelReader<Node, NodeLabelReader>(_nodeLabelReader));
  1223     }
  1223     }
  1224     /// \brief Destructor.
  1224     /// \brief Destructor.
  1225     ///
  1225     ///
  1226     /// Destructor for UndirEdgeSetReader.
  1226     /// Destructor for UEdgeSetReader.
  1227     virtual ~UndirEdgeSetReader() {
  1227     virtual ~UEdgeSetReader() {
  1228       for (typename MapReaders::iterator it = readers.begin(); 
  1228       for (typename MapReaders::iterator it = readers.begin(); 
  1229 	   it != readers.end(); ++it) {
  1229 	   it != readers.end(); ++it) {
  1230 	delete it->second;
  1230 	delete it->second;
  1231       }
  1231       }
  1232     }
  1232     }
  1233 
  1233 
  1234   private:
  1234   private:
  1235     UndirEdgeSetReader(const UndirEdgeSetReader&);
  1235     UEdgeSetReader(const UEdgeSetReader&);
  1236     void operator=(const UndirEdgeSetReader&);
  1236     void operator=(const UEdgeSetReader&);
  1237 
  1237 
  1238   public:
  1238   public:
  1239 
  1239 
  1240     /// \brief Add a new undirected edge map reader command for the reader.
  1240     /// \brief Add a new undirected edge map reader command for the reader.
  1241     ///
  1241     ///
  1242     /// Add a new edge undirected map reader command for the reader.
  1242     /// Add a new edge undirected map reader command for the reader.
  1243     template <typename Map>
  1243     template <typename Map>
  1244     UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) {
  1244     UEdgeSetReader& readUEdgeMap(std::string name, Map& map) {
  1245       return _readMap<
  1245       return _readMap<
  1246 	typename Traits::template Reader<typename Map::Value>, Map, 
  1246 	typename Traits::template Reader<typename Map::Value>, Map, 
  1247 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1247 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1248     }
  1248     }
  1249 
  1249 
  1250     template <typename Map>
  1250     template <typename Map>
  1251     UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) {
  1251     UEdgeSetReader& readUEdgeMap(std::string name, const Map& map) {
  1252       return _readMap<
  1252       return _readMap<
  1253 	typename Traits::template Reader<typename Map::Value>, Map, 
  1253 	typename Traits::template Reader<typename Map::Value>, Map, 
  1254 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1254 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1255     }
  1255     }
  1256 
  1256 
  1257     /// \brief Add a new undirected edge map reader command for the reader.
  1257     /// \brief Add a new undirected edge map reader command for the reader.
  1258     ///
  1258     ///
  1259     /// Add a new edge undirected map reader command for the reader.
  1259     /// Add a new edge undirected map reader command for the reader.
  1260     template <typename Reader, typename Map>
  1260     template <typename Reader, typename Map>
  1261     UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map, 
  1261     UEdgeSetReader& readUEdgeMap(std::string name, Map& map, 
  1262 					 const Reader& reader = Reader()) {
  1262 					 const Reader& reader = Reader()) {
  1263       return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1263       return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1264 	(name, map, reader);
  1264 	(name, map, reader);
  1265     }
  1265     }
  1266 
  1266 
  1267     template <typename Reader, typename Map>
  1267     template <typename Reader, typename Map>
  1268     UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map, 
  1268     UEdgeSetReader& readUEdgeMap(std::string name, const Map& map, 
  1269 					 const Reader& reader = Reader()) {
  1269 					 const Reader& reader = Reader()) {
  1270       return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type >
  1270       return _readMap<Reader, Map, typename _reader_bits::Arg<Map>::Type >
  1271 	(name, map, reader);
  1271 	(name, map, reader);
  1272     }
  1272     }
  1273 
  1273 
  1274   private:
  1274   private:
  1275 
  1275 
  1276     template <typename Reader, typename Map, typename MapParameter>
  1276     template <typename Reader, typename Map, typename MapParameter>
  1277     UndirEdgeSetReader& _readMap(std::string name, MapParameter map,
  1277     UEdgeSetReader& _readMap(std::string name, MapParameter map,
  1278 				 const Reader& reader = Reader()) {
  1278 				 const Reader& reader = Reader()) {
  1279       checkConcept<concept::WriteMap<UndirEdge, typename Map::Value>, Map>();
  1279       checkConcept<concept::WriteMap<UEdge, typename Map::Value>, Map>();
  1280       checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
  1280       checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
  1281       if (readers.find(name) != readers.end()) {
  1281       if (readers.find(name) != readers.end()) {
  1282 	ErrorMessage msg;
  1282 	ErrorMessage msg;
  1283 	msg << "Multiple read rule for edge map: " << name;
  1283 	msg << "Multiple read rule for edge map: " << name;
  1284 	throw IOParameterError(msg.message());
  1284 	throw IOParameterError(msg.message());
  1285       }
  1285       }
  1286       readers.insert(
  1286       readers.insert(
  1287 	make_pair(name, new _reader_bits::
  1287 	make_pair(name, new _reader_bits::
  1288 		  MapReader<UndirEdge, Map, Reader>(map, reader)));
  1288 		  MapReader<UEdge, Map, Reader>(map, reader)));
  1289       return *this;
  1289       return *this;
  1290     }
  1290     }
  1291 
  1291 
  1292   public:
  1292   public:
  1293 
  1293 
  1294     /// \brief Add a new undirected edge map skipper command for the reader.
  1294     /// \brief Add a new undirected edge map skipper command for the reader.
  1295     ///
  1295     ///
  1296     /// Add a new undirected edge map skipper command for the reader.
  1296     /// Add a new undirected edge map skipper command for the reader.
  1297     template <typename Reader>
  1297     template <typename Reader>
  1298     UndirEdgeSetReader& skipUndirEdgeMap(std::string name, 
  1298     UEdgeSetReader& skipUEdgeMap(std::string name, 
  1299 					 const Reader& reader = Reader()) {
  1299 					 const Reader& reader = Reader()) {
  1300       if (readers.find(name) != readers.end()) {
  1300       if (readers.find(name) != readers.end()) {
  1301 	ErrorMessage msg;
  1301 	ErrorMessage msg;
  1302 	msg << "Multiple read rule for node map: " << name;
  1302 	msg << "Multiple read rule for node map: " << name;
  1303 	throw IOParameterError(msg.message());
  1303 	throw IOParameterError(msg.message());
  1304       }
  1304       }
  1305       readers.insert(make_pair(name, new _reader_bits::
  1305       readers.insert(make_pair(name, new _reader_bits::
  1306 			       SkipReader<UndirEdge, Reader>(reader)));
  1306 			       SkipReader<UEdge, Reader>(reader)));
  1307       return *this;
  1307       return *this;
  1308     }
  1308     }
  1309 
  1309 
  1310     /// \brief Add a new directed edge map reader command for the reader.
  1310     /// \brief Add a new directed edge map reader command for the reader.
  1311     ///
  1311     ///
  1312     /// Add a new directed edge map reader command for the reader.
  1312     /// Add a new directed edge map reader command for the reader.
  1313     template <typename Map>
  1313     template <typename Map>
  1314     UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) {
  1314     UEdgeSetReader& readEdgeMap(std::string name, Map& map) {
  1315       return _readDirMap<
  1315       return _readDirMap<
  1316 	typename Traits::template Reader<typename Map::Value>, Map,
  1316 	typename Traits::template Reader<typename Map::Value>, Map,
  1317 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1317 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1318     }
  1318     }
  1319 
  1319 
  1320     template <typename Map>
  1320     template <typename Map>
  1321     UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) {
  1321     UEdgeSetReader& readEdgeMap(std::string name, const Map& map) {
  1322       return _readDirMap<
  1322       return _readDirMap<
  1323 	typename Traits::template Reader<typename Map::Value>, Map,
  1323 	typename Traits::template Reader<typename Map::Value>, Map,
  1324 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1324 	typename _reader_bits::Arg<Map>::Type>(name, map);
  1325     }
  1325     }
  1326 
  1326 
  1327     /// \brief Add a new directed edge map reader command for the reader.
  1327     /// \brief Add a new directed edge map reader command for the reader.
  1328     ///
  1328     ///
  1329     /// Add a new directed edge map reader command for the reader.
  1329     /// Add a new directed edge map reader command for the reader.
  1330     template <typename Reader, typename Map>
  1330     template <typename Reader, typename Map>
  1331     UndirEdgeSetReader& readEdgeMap(std::string name, Map& map, 
  1331     UEdgeSetReader& readEdgeMap(std::string name, Map& map, 
  1332 				    const Reader& reader = Reader()) {
  1332 				    const Reader& reader = Reader()) {
  1333       return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1333       return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1334 	(name, map, reader);
  1334 	(name, map, reader);
  1335     }
  1335     }
  1336 
  1336 
  1337     template <typename Reader, typename Map>
  1337     template <typename Reader, typename Map>
  1338     UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map, 
  1338     UEdgeSetReader& readEdgeMap(std::string name, const Map& map, 
  1339 				    const Reader& reader = Reader()) {
  1339 				    const Reader& reader = Reader()) {
  1340       return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1340       return _readDirMap<Reader, Map, typename _reader_bits::Arg<Map>::Type>
  1341 	(name, map, reader);
  1341 	(name, map, reader);
  1342     }
  1342     }
  1343 
  1343 
  1344   private:
  1344   private:
  1345 
  1345 
  1346     template <typename Reader, typename Map, typename MapParameter>
  1346     template <typename Reader, typename Map, typename MapParameter>
  1347     UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map,
  1347     UEdgeSetReader& _readDirMap(std::string name, MapParameter map,
  1348 				    const Reader& reader = Reader()) { 
  1348 				    const Reader& reader = Reader()) { 
  1349       checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
  1349       checkConcept<_reader_bits::ItemReader<typename Map::Value>, Reader>();
  1350       checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
  1350       checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
  1351       readMap("+" + name, 
  1351       readMap("+" + name, 
  1352 	      _reader_bits::forwardComposeMap(graph, map), reader);
  1352 	      _reader_bits::forwardComposeMap(graph, map), reader);
  1359 
  1359 
  1360     /// \brief Add a new directed edge map skipper command for the reader.
  1360     /// \brief Add a new directed edge map skipper command for the reader.
  1361     ///
  1361     ///
  1362     /// Add a new directed edge map skipper command for the reader.
  1362     /// Add a new directed edge map skipper command for the reader.
  1363     template <typename Reader>
  1363     template <typename Reader>
  1364     UndirEdgeSetReader& skipEdgeMap(std::string name, 
  1364     UEdgeSetReader& skipEdgeMap(std::string name, 
  1365 				    const Reader& reader = Reader()) {
  1365 				    const Reader& reader = Reader()) {
  1366       skipMap("+" + name, reader);
  1366       skipMap("+" + name, reader);
  1367       skipMap("-" + name, reader);
  1367       skipMap("-" + name, reader);
  1368       return *this;
  1368       return *this;
  1369     }
  1369     }
  1371   protected:
  1371   protected:
  1372 
  1372 
  1373     /// \brief Gives back true when the SectionReader can process 
  1373     /// \brief Gives back true when the SectionReader can process 
  1374     /// the section with the given header line.
  1374     /// the section with the given header line.
  1375     ///
  1375     ///
  1376     /// It gives back true when the header line starts with \c \@undiredgeset,
  1376     /// It gives back true when the header line starts with \c \@uedgeset,
  1377     /// and the header line's name and the edgeset's name are the same.
  1377     /// and the header line's name and the edgeset's name are the same.
  1378     virtual bool header(const std::string& line) {
  1378     virtual bool header(const std::string& line) {
  1379       std::istringstream ls(line);
  1379       std::istringstream ls(line);
  1380       std::string command;
  1380       std::string command;
  1381       std::string id;
  1381       std::string id;
  1382       ls >> command >> name;
  1382       ls >> command >> name;
  1383       return command == "@undiredgeset" && name == id;
  1383       return command == "@uedgeset" && name == id;
  1384     }
  1384     }
  1385 
  1385 
  1386     /// \brief Reader function of the section.
  1386     /// \brief Reader function of the section.
  1387     ///
  1387     ///
  1388     /// It reads the content of the section.
  1388     /// It reads the content of the section.
  1389     virtual void read(std::istream& is) {
  1389     virtual void read(std::istream& is) {
  1390       if (!nodeLabelReader->isLabelReader()) {
  1390       if (!nodeLabelReader->isLabelReader()) {
  1391 	throw DataFormatError("Cannot find nodeset or label map");
  1391 	throw DataFormatError("Cannot find nodeset or label map");
  1392       }
  1392       }
  1393       std::vector<_reader_bits::MapReaderBase<UndirEdge>* > index;
  1393       std::vector<_reader_bits::MapReaderBase<UEdge>* > index;
  1394       std::string line;
  1394       std::string line;
  1395 
  1395 
  1396       getline(is, line);
  1396       getline(is, line);
  1397       std::istringstream ls(line);	
  1397       std::istringstream ls(line);	
  1398       std::string id;
  1398       std::string id;
  1419       }
  1419       }
  1420       while (getline(is, line)) {	
  1420       while (getline(is, line)) {	
  1421 	std::istringstream ls(line);
  1421 	std::istringstream ls(line);
  1422 	Node from = nodeLabelReader->read(ls);
  1422 	Node from = nodeLabelReader->read(ls);
  1423 	Node to = nodeLabelReader->read(ls);
  1423 	Node to = nodeLabelReader->read(ls);
  1424 	UndirEdge edge = graph.addEdge(from, to);
  1424 	UEdge edge = graph.addEdge(from, to);
  1425 	for (int i = 0; i < (int)index.size(); ++i) {
  1425 	for (int i = 0; i < (int)index.size(); ++i) {
  1426 	  index[i]->read(ls, edge);
  1426 	  index[i]->read(ls, edge);
  1427 	}
  1427 	}
  1428       }
  1428       }
  1429     }
  1429     }
  1440 
  1440 
  1441     /// \brief Gives back the undirected edge by its label.
  1441     /// \brief Gives back the undirected edge by its label.
  1442     ///
  1442     ///
  1443     /// It reads an id from the stream and gives back which undirected edge 
  1443     /// It reads an id from the stream and gives back which undirected edge 
  1444     /// belongs to it. It is possible only if there was read an "label" named map.
  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 {
  1445     void readLabel(std::istream& is, UEdge& uEdge) const {
  1446       undirEdge = inverter->read(is);
  1446       uEdge = inverter->read(is);
  1447     } 
  1447     } 
  1448 
  1448 
  1449     /// \brief Gives back the directed edge by its label.
  1449     /// \brief Gives back the directed edge by its label.
  1450     ///
  1450     ///
  1451     /// It reads an id from the stream and gives back which directed edge 
  1451     /// It reads an id from the stream and gives back which directed edge 
  1453     /// and the undirected edge id. It is possible only if there was read 
  1453     /// and the undirected edge id. It is possible only if there was read 
  1454     /// an "label" named map.
  1454     /// an "label" named map.
  1455     void readLabel(std::istream& is, Edge& edge) const {
  1455     void readLabel(std::istream& is, Edge& edge) const {
  1456       char c;
  1456       char c;
  1457       is >> c;
  1457       is >> c;
  1458       UndirEdge undirEdge = inverter->read(is);
  1458       UEdge uEdge = inverter->read(is);
  1459       if (c == '+') {
  1459       if (c == '+') {
  1460 	edge = graph.direct(undirEdge, true);
  1460 	edge = graph.direct(uEdge, true);
  1461       } else if (c == '-') {
  1461       } else if (c == '-') {
  1462         edge = graph.direct(undirEdge, false);
  1462         edge = graph.direct(uEdge, false);
  1463       } else {
  1463       } else {
  1464 	throw DataFormatError("Wrong id format for edge "
  1464 	throw DataFormatError("Wrong id format for edge "
  1465 			      "in undirected edgeset");
  1465 			      "in undirected edgeset");
  1466       }
  1466       }
  1467     } 
  1467     } 
  1468 
  1468 
  1469   private:
  1469   private:
  1470 
  1470 
  1471     typedef std::map<std::string, 
  1471     typedef std::map<std::string, 
  1472 		     _reader_bits::MapReaderBase<UndirEdge>*> MapReaders;
  1472 		     _reader_bits::MapReaderBase<UEdge>*> MapReaders;
  1473     MapReaders readers;
  1473     MapReaders readers;
  1474    
  1474    
  1475     Graph& graph;   
  1475     Graph& graph;   
  1476     std::string name;
  1476     std::string name;
  1477     _reader_bits::SkipReader<UndirEdge, DefaultSkipper> skipper;
  1477     _reader_bits::SkipReader<UEdge, DefaultSkipper> skipper;
  1478 
  1478 
  1479     std::auto_ptr<_reader_bits::MapInverterBase<UndirEdge> > inverter;
  1479     std::auto_ptr<_reader_bits::MapInverterBase<UEdge> > inverter;
  1480     std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader;
  1480     std::auto_ptr<_reader_bits::LabelReaderBase<Node> > nodeLabelReader;
  1481   };
  1481   };
  1482 
  1482 
  1483   /// \ingroup io_group
  1483   /// \ingroup io_group
  1484   /// \brief SectionReader for reading labeled nodes.
  1484   /// \brief SectionReader for reading labeled nodes.
  1694   };
  1694   };
  1695 
  1695 
  1696   /// \ingroup io_group
  1696   /// \ingroup io_group
  1697   /// \brief SectionReader for reading labeled undirected edges.
  1697   /// \brief SectionReader for reading labeled undirected edges.
  1698   ///
  1698   ///
  1699   /// The undirected edges section's header line is \c \@undiredges 
  1699   /// The undirected edges section's header line is \c \@uedges 
  1700   /// \c undiredges_name, but the \c undiredges_name may be empty.
  1700   /// \c uedges_name, but the \c uedges_name may be empty.
  1701   ///
  1701   ///
  1702   /// Each line in the section contains the name of the undirected edge 
  1702   /// Each line in the section contains the name of the undirected edge 
  1703   /// and then the undirected edge id. 
  1703   /// and then the undirected edge id. 
  1704   ///
  1704   ///
  1705   /// \relates LemonReader
  1705   /// \relates LemonReader
  1706   template <typename _Graph>
  1706   template <typename _Graph>
  1707   class UndirEdgeReader : public LemonReader::SectionReader {
  1707   class UEdgeReader : public LemonReader::SectionReader {
  1708     typedef LemonReader::SectionReader Parent;
  1708     typedef LemonReader::SectionReader Parent;
  1709     typedef _Graph Graph;
  1709     typedef _Graph Graph;
  1710     typedef typename Graph::Edge Edge;
  1710     typedef typename Graph::Edge Edge;
  1711     typedef typename Graph::UndirEdge UndirEdge;
  1711     typedef typename Graph::UEdge UEdge;
  1712   public:
  1712   public:
  1713     
  1713     
  1714     /// \brief Constructor.
  1714     /// \brief Constructor.
  1715     ///
  1715     ///
  1716     /// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and
  1716     /// Constructor for UEdgeReader. It creates the UEdgeReader and
  1717     /// attach it into the given LemonReader. It will use the given
  1717     /// attach it into the given LemonReader. It will use the given
  1718     /// undirected edge id reader to give back the edges. The reader will 
  1718     /// undirected edge id reader to give back the edges. The reader will 
  1719     /// read the section only if the \c _name and the \c undiredges_name are 
  1719     /// read the section only if the \c _name and the \c uedges_name are 
  1720     /// the same. 
  1720     /// the same. 
  1721     template <typename _LabelReader>
  1721     template <typename _LabelReader>
  1722     UndirEdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 
  1722     UEdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, 
  1723 	       const std::string& _name = std::string()) 
  1723 	       const std::string& _name = std::string()) 
  1724       : Parent(_reader), name(_name) {
  1724       : Parent(_reader), name(_name) {
  1725       checkConcept<_reader_bits::ItemLabelReader<UndirEdge>, _LabelReader>();
  1725       checkConcept<_reader_bits::ItemLabelReader<UEdge>, _LabelReader>();
  1726       checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>();
  1726       checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>();
  1727       undirEdgeLabelReader.reset(new _reader_bits::
  1727       uEdgeLabelReader.reset(new _reader_bits::
  1728 			      LabelReader<UndirEdge, _LabelReader>(_labelReader));
  1728 			      LabelReader<UEdge, _LabelReader>(_labelReader));
  1729       edgeLabelReader.reset(new _reader_bits::
  1729       edgeLabelReader.reset(new _reader_bits::
  1730 			 LabelReader<Edge, _LabelReader>(_labelReader));
  1730 			 LabelReader<Edge, _LabelReader>(_labelReader));
  1731     }
  1731     }
  1732 
  1732 
  1733     /// \brief Destructor.
  1733     /// \brief Destructor.
  1734     ///
  1734     ///
  1735     /// Destructor for UndirEdgeReader.
  1735     /// Destructor for UEdgeReader.
  1736     virtual ~UndirEdgeReader() {}
  1736     virtual ~UEdgeReader() {}
  1737   private:
  1737   private:
  1738     UndirEdgeReader(const UndirEdgeReader&);
  1738     UEdgeReader(const UEdgeReader&);
  1739     void operator=(const UndirEdgeReader&);
  1739     void operator=(const UEdgeReader&);
  1740 
  1740 
  1741   public:
  1741   public:
  1742 
  1742 
  1743     /// \brief Add an undirected edge reader command for the UndirEdgeReader.
  1743     /// \brief Add an undirected edge reader command for the UEdgeReader.
  1744     ///
  1744     ///
  1745     /// Add an undirected edge reader command for the UndirEdgeReader.
  1745     /// Add an undirected edge reader command for the UEdgeReader.
  1746     void readUndirEdge(const std::string& name, UndirEdge& item) {
  1746     void readUEdge(const std::string& name, UEdge& item) {
  1747       if (undirEdgeReaders.find(name) != undirEdgeReaders.end()) {
  1747       if (uEdgeReaders.find(name) != uEdgeReaders.end()) {
  1748 	ErrorMessage msg;
  1748 	ErrorMessage msg;
  1749 	msg << "Multiple read rule for undirected edge: " << name;
  1749 	msg << "Multiple read rule for undirected edge: " << name;
  1750 	throw IOParameterError(msg.message());
  1750 	throw IOParameterError(msg.message());
  1751       }
  1751       }
  1752       undirEdgeReaders.insert(make_pair(name, _reader_bits::
  1752       uEdgeReaders.insert(make_pair(name, _reader_bits::
  1753 					ItemStore<UndirEdge>(item)));
  1753 					ItemStore<UEdge>(item)));
  1754     }
  1754     }
  1755 
  1755 
  1756     /// \brief Add an edge reader command for the UndirEdgeReader.
  1756     /// \brief Add an edge reader command for the UEdgeReader.
  1757     ///
  1757     ///
  1758     /// Add an edge reader command for the UndirEdgeReader.
  1758     /// Add an edge reader command for the UEdgeReader.
  1759     void readEdge(const std::string& name, Edge& item) {
  1759     void readEdge(const std::string& name, Edge& item) {
  1760       if (edgeReaders.find(name) != edgeReaders.end()) {
  1760       if (edgeReaders.find(name) != edgeReaders.end()) {
  1761 	ErrorMessage msg;
  1761 	ErrorMessage msg;
  1762 	msg << "Multiple read rule for edge: " << name;
  1762 	msg << "Multiple read rule for edge: " << name;
  1763 	throw IOParameterError(msg.message());
  1763 	throw IOParameterError(msg.message());
  1775     virtual bool header(const std::string& line) {
  1775     virtual bool header(const std::string& line) {
  1776       std::istringstream ls(line);
  1776       std::istringstream ls(line);
  1777       std::string command;
  1777       std::string command;
  1778       std::string id;
  1778       std::string id;
  1779       ls >> command >> name;
  1779       ls >> command >> name;
  1780       return command == "@undiredges" && name == id;
  1780       return command == "@uedges" && name == id;
  1781     }
  1781     }
  1782 
  1782 
  1783     /// \brief Reader function of the section.
  1783     /// \brief Reader function of the section.
  1784     ///
  1784     ///
  1785     /// It reads the content of the section.
  1785     /// It reads the content of the section.
  1786     virtual void read(std::istream& is) {
  1786     virtual void read(std::istream& is) {
  1787       if (!edgeLabelReader->isLabelReader()) {
  1787       if (!edgeLabelReader->isLabelReader()) {
  1788 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1788 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1789       }
  1789       }
  1790       if (!undirEdgeLabelReader->isLabelReader()) {
  1790       if (!uEdgeLabelReader->isLabelReader()) {
  1791 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1791 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1792       }
  1792       }
  1793       std::string line;
  1793       std::string line;
  1794       while (getline(is, line)) {
  1794       while (getline(is, line)) {
  1795 	std::istringstream ls(line);
  1795 	std::istringstream ls(line);
  1796 	std::string id;
  1796 	std::string id;
  1797 	ls >> id;
  1797 	ls >> id;
  1798 	{
  1798 	{
  1799 	  typename UndirEdgeReaders::iterator it = undirEdgeReaders.find(id);
  1799 	  typename UEdgeReaders::iterator it = uEdgeReaders.find(id);
  1800 	  if (it != undirEdgeReaders.end()) {
  1800 	  if (it != uEdgeReaders.end()) {
  1801 	    it->second.read(undirEdgeLabelReader->read(ls));
  1801 	    it->second.read(uEdgeLabelReader->read(ls));
  1802 	    it->second.touch();
  1802 	    it->second.touch();
  1803 	    continue;
  1803 	    continue;
  1804 	  }	
  1804 	  }	
  1805 	} {
  1805 	} {
  1806 	  typename EdgeReaders::iterator it = edgeReaders.find(id);
  1806 	  typename EdgeReaders::iterator it = edgeReaders.find(id);
  1817 	  ErrorMessage msg;
  1817 	  ErrorMessage msg;
  1818 	  msg << "Edge not found in file: " << it->first;
  1818 	  msg << "Edge not found in file: " << it->first;
  1819 	  throw IOParameterError(msg.message());
  1819 	  throw IOParameterError(msg.message());
  1820 	}
  1820 	}
  1821       }
  1821       }
  1822       for (typename UndirEdgeReaders::iterator it = undirEdgeReaders.begin();
  1822       for (typename UEdgeReaders::iterator it = uEdgeReaders.begin();
  1823 	   it != undirEdgeReaders.end(); ++it) {
  1823 	   it != uEdgeReaders.end(); ++it) {
  1824 	if (!it->second.touched()) {
  1824 	if (!it->second.touched()) {
  1825 	  ErrorMessage msg;
  1825 	  ErrorMessage msg;
  1826 	  msg << "UndirEdge not found in file: " << it->first;
  1826 	  msg << "UEdge not found in file: " << it->first;
  1827 	  throw IOParameterError(msg.message());
  1827 	  throw IOParameterError(msg.message());
  1828 	}
  1828 	}
  1829       }
  1829       }
  1830     }
  1830     }
  1831     
  1831     
  1832   private:
  1832   private:
  1833 
  1833 
  1834     std::string name;
  1834     std::string name;
  1835 
  1835 
  1836     typedef std::map<std::string, 
  1836     typedef std::map<std::string, 
  1837 		     _reader_bits::ItemStore<UndirEdge> > UndirEdgeReaders;
  1837 		     _reader_bits::ItemStore<UEdge> > UEdgeReaders;
  1838     UndirEdgeReaders undirEdgeReaders;
  1838     UEdgeReaders uEdgeReaders;
  1839     std::auto_ptr<_reader_bits::LabelReaderBase<UndirEdge> > undirEdgeLabelReader;
  1839     std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > uEdgeLabelReader;
  1840 
  1840 
  1841     typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders;
  1841     typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders;
  1842     EdgeReaders edgeReaders;
  1842     EdgeReaders edgeReaders;
  1843     std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader;
  1843     std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader;
  1844   };
  1844   };
  2023     }
  2023     }
  2024 
  2024 
  2025     /// \brief Gives back how many undirected edgesets are in the file.
  2025     /// \brief Gives back how many undirected edgesets are in the file.
  2026     ///
  2026     ///
  2027     /// Gives back how many undirected edgesets are in the file.
  2027     /// Gives back how many undirected edgesets are in the file.
  2028     int undirEdgeSetNum() const {
  2028     int uEdgeSetNum() const {
  2029       return undiredgesets.size();
  2029       return uedgesets.size();
  2030     }
  2030     }
  2031 
  2031 
  2032     /// \brief Gives back the name of undirected edgeset on the indiced 
  2032     /// \brief Gives back the name of undirected edgeset on the indiced 
  2033     /// position.
  2033     /// position.
  2034     ///
  2034     ///
  2035     /// Gives back the name of undirected edgeset on the indiced position.
  2035     /// Gives back the name of undirected edgeset on the indiced position.
  2036     std::string undirEdgeSetName(int index) const {
  2036     std::string uEdgeSetName(int index) const {
  2037       return undiredgesets[index].name;
  2037       return uedgesets[index].name;
  2038     }
  2038     }
  2039 
  2039 
  2040     /// \brief Gives back the map names of undirected edgeset on the indiced 
  2040     /// \brief Gives back the map names of undirected edgeset on the indiced 
  2041     /// position.
  2041     /// position.
  2042     ///
  2042     ///
  2043     /// Gives back the map names of undirected edgeset on the indiced position.
  2043     /// Gives back the map names of undirected edgeset on the indiced position.
  2044     const std::vector<std::string>& undirEdgeSetMaps(int index) const {
  2044     const std::vector<std::string>& uEdgeSetMaps(int index) const {
  2045       return undiredgesets[index].items;
  2045       return uedgesets[index].items;
  2046     }
  2046     }
  2047 
  2047 
  2048     /// \brief Gives back how many labeled nodes section are in the file.
  2048     /// \brief Gives back how many labeled nodes section are in the file.
  2049     ///
  2049     ///
  2050     /// Gives back how many labeled nodes section are in the file.
  2050     /// Gives back how many labeled nodes section are in the file.
  2093  
  2093  
  2094     /// \brief Gives back how many labeled undirected edges section are 
  2094     /// \brief Gives back how many labeled undirected edges section are 
  2095     /// in the file.
  2095     /// in the file.
  2096     ///
  2096     ///
  2097     /// Gives back how many labeled undirected edges section are in the file.
  2097     /// Gives back how many labeled undirected edges section are in the file.
  2098     int undirEdgesNum() const {
  2098     int uEdgesNum() const {
  2099       return undiredges.size();
  2099       return uedges.size();
  2100     }
  2100     }
  2101 
  2101 
  2102     /// \brief Gives back the name of labeled undirected edges section 
  2102     /// \brief Gives back the name of labeled undirected edges section 
  2103     /// on the indiced position.
  2103     /// on the indiced position.
  2104     ///
  2104     ///
  2105     /// Gives back the name of labeled undirected edges section on the 
  2105     /// Gives back the name of labeled undirected edges section on the 
  2106     /// indiced position.
  2106     /// indiced position.
  2107     std::string undirEdgesName(int index) const {
  2107     std::string uEdgesName(int index) const {
  2108       return undiredges[index].name;
  2108       return uedges[index].name;
  2109     }
  2109     }
  2110 
  2110 
  2111     /// \brief Gives back the names of the labeled undirected edges in 
  2111     /// \brief Gives back the names of the labeled undirected edges in 
  2112     /// the indiced section.
  2112     /// the indiced section.
  2113     ///
  2113     ///
  2114     /// Gives back the names of the labeled undirected edges in the 
  2114     /// Gives back the names of the labeled undirected edges in the 
  2115     /// indiced section.
  2115     /// indiced section.
  2116     const std::vector<std::string>& undirEdgesItems(int index) const {
  2116     const std::vector<std::string>& uEdgesItems(int index) const {
  2117       return undiredges[index].items;
  2117       return uedges[index].items;
  2118     }
  2118     }
  2119 
  2119 
  2120  
  2120  
  2121     /// \brief Gives back how many attributes section are in the file.
  2121     /// \brief Gives back how many attributes section are in the file.
  2122     ///
  2122     ///
  2158 	current = command;
  2158 	current = command;
  2159 	nodesets.push_back(SectionInfo(name));
  2159 	nodesets.push_back(SectionInfo(name));
  2160       } else if (command == "@edgeset") {
  2160       } else if (command == "@edgeset") {
  2161 	current = command;
  2161 	current = command;
  2162 	edgesets.push_back(SectionInfo(name));
  2162 	edgesets.push_back(SectionInfo(name));
  2163       } else if (command == "@undiredgeset") {
  2163       } else if (command == "@uedgeset") {
  2164 	current = command;
  2164 	current = command;
  2165 	undiredgesets.push_back(SectionInfo(name));
  2165 	uedgesets.push_back(SectionInfo(name));
  2166       } else if (command == "@nodes") {
  2166       } else if (command == "@nodes") {
  2167 	current = command;
  2167 	current = command;
  2168 	nodes.push_back(SectionInfo(name));
  2168 	nodes.push_back(SectionInfo(name));
  2169       } else if (command == "@edges") {
  2169       } else if (command == "@edges") {
  2170 	current = command;
  2170 	current = command;
  2171 	edges.push_back(SectionInfo(name));
  2171 	edges.push_back(SectionInfo(name));
  2172       } else if (command == "@undiredges") {
  2172       } else if (command == "@uedges") {
  2173 	current = command;
  2173 	current = command;
  2174 	undiredges.push_back(SectionInfo(name));
  2174 	uedges.push_back(SectionInfo(name));
  2175       } else if (command == "@attributes") {
  2175       } else if (command == "@attributes") {
  2176 	current = command;
  2176 	current = command;
  2177 	attributes.push_back(SectionInfo(name));
  2177 	attributes.push_back(SectionInfo(name));
  2178       } else {
  2178       } else {
  2179 	sections.push_back(line);
  2179 	sections.push_back(line);
  2188     void read(std::istream& is) {
  2188     void read(std::istream& is) {
  2189       if (current == "@nodeset") {
  2189       if (current == "@nodeset") {
  2190 	readMapNames(is, nodesets.back().items);
  2190 	readMapNames(is, nodesets.back().items);
  2191       } else if (current == "@edgeset") {
  2191       } else if (current == "@edgeset") {
  2192 	readMapNames(is, edgesets.back().items);
  2192 	readMapNames(is, edgesets.back().items);
  2193       } else if (current == "@undiredgeset") {
  2193       } else if (current == "@uedgeset") {
  2194 	readMapNames(is, undiredgesets.back().items);
  2194 	readMapNames(is, uedgesets.back().items);
  2195       } else if (current == "@nodes") {
  2195       } else if (current == "@nodes") {
  2196 	readItemNames(is, nodes.back().items);
  2196 	readItemNames(is, nodes.back().items);
  2197       } else if (current == "@edges") {
  2197       } else if (current == "@edges") {
  2198 	readItemNames(is, edges.back().items);
  2198 	readItemNames(is, edges.back().items);
  2199       } else if (current == "@undiredges") {
  2199       } else if (current == "@uedges") {
  2200 	readItemNames(is, undiredges.back().items);
  2200 	readItemNames(is, uedges.back().items);
  2201       } else if (current == "@attributes") {
  2201       } else if (current == "@attributes") {
  2202 	readItemNames(is, attributes.back().items);
  2202 	readItemNames(is, attributes.back().items);
  2203       }
  2203       }
  2204     }    
  2204     }    
  2205 
  2205 
  2231       SectionInfo(const std::string& _name) : name(_name) {}
  2231       SectionInfo(const std::string& _name) : name(_name) {}
  2232     };
  2232     };
  2233 
  2233 
  2234     std::vector<SectionInfo> nodesets;
  2234     std::vector<SectionInfo> nodesets;
  2235     std::vector<SectionInfo> edgesets;
  2235     std::vector<SectionInfo> edgesets;
  2236     std::vector<SectionInfo> undiredgesets;
  2236     std::vector<SectionInfo> uedgesets;
  2237 
  2237 
  2238     std::vector<SectionInfo> nodes;
  2238     std::vector<SectionInfo> nodes;
  2239     std::vector<SectionInfo> edges;
  2239     std::vector<SectionInfo> edges;
  2240     std::vector<SectionInfo> undiredges;
  2240     std::vector<SectionInfo> uedges;
  2241 
  2241 
  2242     std::vector<SectionInfo> attributes;
  2242     std::vector<SectionInfo> attributes;
  2243 
  2243 
  2244     std::vector<std::string> sections;
  2244     std::vector<std::string> sections;
  2245 
  2245