lemon/lemon_writer.h
changeset 2386 81b47fc5c444
parent 2282 9d7b12f83daa
child 2391 14a343be7a5a
equal deleted inserted replaced
18:44a8d3238562 19:f4c4800df71a
   470 
   470 
   471     /// \brief Add a new node map writer command for the writer.
   471     /// \brief Add a new node map writer command for the writer.
   472     ///
   472     ///
   473     /// Add a new node map writer command for the writer.
   473     /// Add a new node map writer command for the writer.
   474     template <typename Map>
   474     template <typename Map>
   475     NodeSetWriter& writeNodeMap(std::string name, const Map& map) {
   475     NodeSetWriter& writeNodeMap(std::string label, const Map& map) {
   476       return writeNodeMap<typename Traits::
   476       return writeNodeMap<typename Traits::
   477 	template Writer<typename Map::Value>, Map>(name, map);
   477 	template Writer<typename Map::Value>, Map>(label, map);
   478     }
   478     }
   479 
   479 
   480     /// \brief Add a new node map writer command for the writer.
   480     /// \brief Add a new node map writer command for the writer.
   481     ///
   481     ///
   482     /// Add a new node map writer command for the writer.
   482     /// Add a new node map writer command for the writer.
   483     template <typename Writer, typename Map>
   483     template <typename ItemWriter, typename Map>
   484     NodeSetWriter& writeNodeMap(std::string name, const Map& map, 
   484     NodeSetWriter& writeNodeMap(std::string label, const Map& map, 
   485 			    const Writer& writer = Writer()) {
   485 			    const ItemWriter& iw = ItemWriter()) {
   486       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   486       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   487       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   487       checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
   488       writers.push_back(
   488       writers.push_back(
   489 	make_pair(name, new _writer_bits::
   489 	make_pair(label, new _writer_bits::
   490 		  MapWriter<Node, Map, Writer>(map, writer)));
   490 		  MapWriter<Node, Map, ItemWriter>(map, iw)));
   491       return *this;
   491       return *this;
   492     }
   492     }
   493 
   493 
   494   protected:
   494   protected:
   495 
   495 
   502 
   502 
   503     /// \brief  Writer function of the section.
   503     /// \brief  Writer function of the section.
   504     ///
   504     ///
   505     /// Write the content of the section.
   505     /// Write the content of the section.
   506     virtual void write(std::ostream& os) {
   506     virtual void write(std::ostream& os) {
   507       for (int i = 0; i < (int)writers.size(); ++i) {
   507       for (int i = 0; i < int(writers.size()); ++i) {
   508 	if (writers[i].first == "label") {
   508 	if (writers[i].first == "label") {
   509 	  labelMap = writers[i].second;
   509 	  labelMap = writers[i].second;
   510 	  forceLabelMap = false;
   510 	  forceLabelMap = false;
   511 	  break;
   511 	  break;
   512 	}
   512 	}
   526         }
   526         }
   527       }
   527       }
   528       if (forceLabelMap) {
   528       if (forceLabelMap) {
   529 	os << "label\t";
   529 	os << "label\t";
   530       }
   530       }
   531       for (int i = 0; i < (int)writers.size(); ++i) {
   531       for (int i = 0; i < int(writers.size()); ++i) {
   532 	os << writers[i].first << '\t';
   532 	os << writers[i].first << '\t';
   533       }
   533       }
   534       os << std::endl;
   534       os << std::endl;
   535       for (typename std::vector<Node>::iterator it = items.begin();
   535       for (typename std::vector<Node>::iterator it = items.begin();
   536            it != items.end(); ++it) {
   536            it != items.end(); ++it) {
   537 	if (forceLabelMap) {
   537 	if (forceLabelMap) {
   538 	  os << graph.id(*it) << '\t';
   538 	  os << graph.id(*it) << '\t';
   539 	}
   539 	}
   540 	for (int i = 0; i < (int)writers.size(); ++i) {
   540 	for (int i = 0; i < int(writers.size()); ++i) {
   541 	  writers[i].second->write(os, *it);
   541 	  writers[i].second->write(os, *it);
   542 	  os << '\t';
   542 	  os << '\t';
   543 	}
   543 	}
   544 	os << std::endl;
   544 	os << std::endl;
   545       }
   545       }
   659 
   659 
   660     /// \brief Add a new edge map writer command for the writer.
   660     /// \brief Add a new edge map writer command for the writer.
   661     ///
   661     ///
   662     /// Add a new edge map writer command for the writer.
   662     /// Add a new edge map writer command for the writer.
   663     template <typename Map>
   663     template <typename Map>
   664     EdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
   664     EdgeSetWriter& writeEdgeMap(std::string label, const Map& map) {
   665       return writeEdgeMap<typename Traits::
   665       return writeEdgeMap<typename Traits::
   666 	template Writer<typename Map::Value>, Map>(name, map);
   666 	template Writer<typename Map::Value>, Map>(label, map);
   667     }
   667     }
   668 
   668 
   669     /// \brief Add a new edge map writer command for the writer.
   669     /// \brief Add a new edge map writer command for the writer.
   670     ///
   670     ///
   671     /// Add a new edge map writer command for the writer.
   671     /// Add a new edge map writer command for the writer.
   672     template <typename Writer, typename Map>
   672     template <typename ItemWriter, typename Map>
   673     EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   673     EdgeSetWriter& writeEdgeMap(std::string label, const Map& map, 
   674 			    const Writer& writer = Writer()) {
   674 			    const ItemWriter& iw = ItemWriter()) {
   675       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
   675       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
   676       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   676       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
   677       writers.push_back(
   677       writers.push_back(
   678 	make_pair(name, new _writer_bits::
   678 	make_pair(label, new _writer_bits::
   679 		  MapWriter<Edge, Map, Writer>(map, writer)));
   679 		  MapWriter<Edge, Map, ItemWriter>(map, iw)));
   680       return *this;
   680       return *this;
   681     }
   681     }
   682 
   682 
   683   protected:
   683   protected:
   684 
   684 
   694     /// Write the content of the section.
   694     /// Write the content of the section.
   695     virtual void write(std::ostream& os) {
   695     virtual void write(std::ostream& os) {
   696       if (!nodeLabelWriter->isLabelWriter()) {
   696       if (!nodeLabelWriter->isLabelWriter()) {
   697 	throw DataFormatError("Cannot find nodeset or label map");
   697 	throw DataFormatError("Cannot find nodeset or label map");
   698       }
   698       }
   699       for (int i = 0; i < (int)writers.size(); ++i) {
   699       for (int i = 0; i < int(writers.size()); ++i) {
   700 	if (writers[i].first == "label") {
   700 	if (writers[i].first == "label") {
   701 	  labelMap = writers[i].second;
   701 	  labelMap = writers[i].second;
   702 	  forceLabelMap = false;
   702 	  forceLabelMap = false;
   703 	  break;
   703 	  break;
   704 	}
   704 	}
   719       }
   719       }
   720       os << "\t\t";
   720       os << "\t\t";
   721       if (forceLabelMap) {
   721       if (forceLabelMap) {
   722 	os << "label\t";
   722 	os << "label\t";
   723       }
   723       }
   724       for (int i = 0; i < (int)writers.size(); ++i) {
   724       for (int i = 0; i < int(writers.size()); ++i) {
   725 	os << writers[i].first << '\t';
   725 	os << writers[i].first << '\t';
   726       }
   726       }
   727       os << std::endl;
   727       os << std::endl;
   728       for (typename std::vector<Edge>::iterator it = items.begin();
   728       for (typename std::vector<Edge>::iterator it = items.begin();
   729            it != items.end(); ++it) {
   729            it != items.end(); ++it) {
   732 	nodeLabelWriter->write(os, graph.target(*it));
   732 	nodeLabelWriter->write(os, graph.target(*it));
   733 	os << '\t';
   733 	os << '\t';
   734 	if (forceLabelMap) {
   734 	if (forceLabelMap) {
   735 	  os << graph.id(*it) << '\t';
   735 	  os << graph.id(*it) << '\t';
   736 	}
   736 	}
   737 	for (int i = 0; i < (int)writers.size(); ++i) {
   737 	for (int i = 0; i < int(writers.size()); ++i) {
   738 	  writers[i].second->write(os, *it);
   738 	  writers[i].second->write(os, *it);
   739 	  os << '\t';
   739 	  os << '\t';
   740 	}
   740 	}
   741 	os << std::endl;
   741 	os << std::endl;
   742       }
   742       }
   865 
   865 
   866     /// \brief Add a new undirected edge map writer command for the writer.
   866     /// \brief Add a new undirected edge map writer command for the writer.
   867     ///
   867     ///
   868     /// Add a new undirected map writer command for the writer.
   868     /// Add a new undirected map writer command for the writer.
   869     template <typename Map>
   869     template <typename Map>
   870     UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map) {
   870     UEdgeSetWriter& writeUEdgeMap(std::string label, const Map& map) {
   871       return writeUEdgeMap<typename Traits::
   871       return writeUEdgeMap<typename Traits::
   872 	template Writer<typename Map::Value>, Map>(name, map);
   872 	template Writer<typename Map::Value>, Map>(label, map);
   873     }
   873     }
   874 
   874 
   875     /// \brief Add a new undirected map writer command for the writer.
   875     /// \brief Add a new undirected map writer command for the writer.
   876     ///
   876     ///
   877     /// Add a new undirected map writer command for the writer.
   877     /// Add a new undirected map writer command for the writer.
   878     template <typename Writer, typename Map>
   878     template <typename ItemWriter, typename Map>
   879     UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map, 
   879     UEdgeSetWriter& writeUEdgeMap(std::string label, const Map& map, 
   880                                   const Writer& writer = Writer()) {
   880                                   const ItemWriter& iw = ItemWriter()) {
   881       checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>();
   881       checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>();
   882       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   882       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
   883       writers.push_back(
   883       writers.push_back(
   884 	make_pair(name, new _writer_bits::
   884 	make_pair(label, new _writer_bits::
   885 		  MapWriter<UEdge, Map, Writer>(map, writer)));
   885 		  MapWriter<UEdge, Map, ItemWriter>(map, iw)));
   886       return *this;
   886       return *this;
   887     }
   887     }
   888 
   888 
   889     /// \brief Add a new directed edge map writer command for the writer.
   889     /// \brief Add a new directed edge map writer command for the writer.
   890     ///
   890     ///
   891     /// Add a new directed map writer command for the writer.
   891     /// Add a new directed map writer command for the writer.
   892     template <typename Map>
   892     template <typename Map>
   893     UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
   893     UEdgeSetWriter& writeEdgeMap(std::string label, const Map& map) {
   894       return writeEdgeMap<typename Traits::
   894       return writeEdgeMap<typename Traits::
   895 	template Writer<typename Map::Value>, Map>(name, map);
   895 	template Writer<typename Map::Value>, Map>(label, map);
   896     }
   896     }
   897 
   897 
   898     /// \brief Add a new directed map writer command for the writer.
   898     /// \brief Add a new directed map writer command for the writer.
   899     ///
   899     ///
   900     /// Add a new directed map writer command for the writer.
   900     /// Add a new directed map writer command for the writer.
   901     template <typename Writer, typename Map>
   901     template <typename ItemWriter, typename Map>
   902     UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   902     UEdgeSetWriter& writeEdgeMap(std::string label, const Map& map, 
   903                                  const Writer& writer = Writer()) {
   903                                  const ItemWriter& iw = ItemWriter()) {
   904       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
   904       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
   905       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   905       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
   906       writeUEdgeMap("+" + name, 
   906       writeUEdgeMap("+" + name, 
   907                     _writer_bits::forwardComposeMap(graph, map), writer);
   907                     _writer_bits::forwardComposeMap(graph, map), iw);
   908       writeUEdgeMap("-" + name, 
   908       writeUEdgeMap("-" + name, 
   909                     _writer_bits::backwardComposeMap(graph, map), writer);
   909                     _writer_bits::backwardComposeMap(graph, map), iw);
   910       return *this;
   910       return *this;
   911     }
   911     }
   912 
   912 
   913   protected:
   913   protected:
   914 
   914 
   924     /// Write the content of the section.
   924     /// Write the content of the section.
   925     virtual void write(std::ostream& os) {
   925     virtual void write(std::ostream& os) {
   926       if (!nodeLabelWriter->isLabelWriter()) {
   926       if (!nodeLabelWriter->isLabelWriter()) {
   927 	throw DataFormatError("Cannot find nodeset or label map");
   927 	throw DataFormatError("Cannot find nodeset or label map");
   928       }
   928       }
   929       for (int i = 0; i < (int)writers.size(); ++i) {
   929       for (int i = 0; i < int(writers.size()); ++i) {
   930 	if (writers[i].first == "label") {
   930 	if (writers[i].first == "label") {
   931 	  labelMap = writers[i].second;
   931 	  labelMap = writers[i].second;
   932 	  forceLabelMap = false;
   932 	  forceLabelMap = false;
   933 	  break;
   933 	  break;
   934 	}
   934 	}
   949       }
   949       }
   950       os << "\t\t";
   950       os << "\t\t";
   951       if (forceLabelMap) {
   951       if (forceLabelMap) {
   952 	os << "label\t";
   952 	os << "label\t";
   953       }
   953       }
   954       for (int i = 0; i < (int)writers.size(); ++i) {
   954       for (int i = 0; i < int(writers.size()); ++i) {
   955 	os << writers[i].first << '\t';
   955 	os << writers[i].first << '\t';
   956       }
   956       }
   957       os << std::endl;
   957       os << std::endl;
   958       for (typename std::vector<UEdge>::iterator it = items.begin();
   958       for (typename std::vector<UEdge>::iterator it = items.begin();
   959            it != items.end(); ++it) {
   959            it != items.end(); ++it) {
   962 	nodeLabelWriter->write(os, graph.target(*it));
   962 	nodeLabelWriter->write(os, graph.target(*it));
   963 	os << '\t';
   963 	os << '\t';
   964 	if (forceLabelMap) {
   964 	if (forceLabelMap) {
   965 	  os << graph.id(*it) << '\t';
   965 	  os << graph.id(*it) << '\t';
   966 	}
   966 	}
   967 	for (int i = 0; i < (int)writers.size(); ++i) {
   967 	for (int i = 0; i < int(writers.size()); ++i) {
   968 	  writers[i].second->write(os, *it);
   968 	  writers[i].second->write(os, *it);
   969 	  os << '\t';
   969 	  os << '\t';
   970 	}
   970 	}
   971 	os << std::endl;
   971 	os << std::endl;
   972       }
   972       }
  1078   public:
  1078   public:
  1079 
  1079 
  1080     /// \brief Add a node writer command for the NodeWriter.
  1080     /// \brief Add a node writer command for the NodeWriter.
  1081     ///
  1081     ///
  1082     /// Add a node writer command for the NodeWriter.
  1082     /// Add a node writer command for the NodeWriter.
  1083     void writeNode(const std::string& name, const Node& item) {
  1083     void writeNode(std::string label, const Node& item) {
  1084       writers.push_back(make_pair(name, &item));
  1084       writers.push_back(make_pair(label, &item));
  1085     }
  1085     }
  1086 
  1086 
  1087   protected:
  1087   protected:
  1088 
  1088 
  1089     /// \brief The header of the section.
  1089     /// \brief The header of the section.
  1098     /// Write the content of the section.
  1098     /// Write the content of the section.
  1099     virtual void write(std::ostream& os) {
  1099     virtual void write(std::ostream& os) {
  1100       if (!labelWriter->isLabelWriter()) {
  1100       if (!labelWriter->isLabelWriter()) {
  1101 	throw DataFormatError("Cannot find nodeset or label map");
  1101 	throw DataFormatError("Cannot find nodeset or label map");
  1102       }
  1102       }
  1103       for (int i = 0; i < (int)writers.size(); ++i) {
  1103       for (int i = 0; i < int(writers.size()); ++i) {
  1104 	os << writers[i].first << ' ';
  1104 	os << writers[i].first << ' ';
  1105 	labelWriter->write(os, *(writers[i].second));
  1105 	labelWriter->write(os, *(writers[i].second));
  1106 	os << std::endl;
  1106 	os << std::endl;
  1107       }
  1107       }
  1108     }
  1108     }
  1162   public:
  1162   public:
  1163 
  1163 
  1164     /// \brief Add an edge writer command for the EdgeWriter.
  1164     /// \brief Add an edge writer command for the EdgeWriter.
  1165     ///
  1165     ///
  1166     /// Add an edge writer command for the EdgeWriter.
  1166     /// Add an edge writer command for the EdgeWriter.
  1167     void writeEdge(const std::string& name, const Edge& item) {
  1167     void writeEdge(std::string label, const Edge& item) {
  1168       writers.push_back(make_pair(name, &item));
  1168       writers.push_back(make_pair(label, &item));
  1169     }
  1169     }
  1170 
  1170 
  1171   protected:
  1171   protected:
  1172 
  1172 
  1173     /// \brief The header of the section.
  1173     /// \brief The header of the section.
  1182     /// Write the content of the section.
  1182     /// Write the content of the section.
  1183     virtual void write(std::ostream& os) {
  1183     virtual void write(std::ostream& os) {
  1184       if (!labelWriter->isLabelWriter()) {
  1184       if (!labelWriter->isLabelWriter()) {
  1185 	throw DataFormatError("Cannot find edgeset or label map");
  1185 	throw DataFormatError("Cannot find edgeset or label map");
  1186       }
  1186       }
  1187       for (int i = 0; i < (int)writers.size(); ++i) {
  1187       for (int i = 0; i < int(writers.size()); ++i) {
  1188 	os << writers[i].first << ' ';
  1188 	os << writers[i].first << ' ';
  1189 	labelWriter->write(os, *(writers[i].second));
  1189 	labelWriter->write(os, *(writers[i].second));
  1190 	os << std::endl;
  1190 	os << std::endl;
  1191       }
  1191       }
  1192     }
  1192     }
  1254   public:
  1254   public:
  1255 
  1255 
  1256     /// \brief Add an edge writer command for the UEdgeWriter.
  1256     /// \brief Add an edge writer command for the UEdgeWriter.
  1257     ///
  1257     ///
  1258     /// Add an edge writer command for the UEdgeWriter.
  1258     /// Add an edge writer command for the UEdgeWriter.
  1259     void writeEdge(const std::string& name, const Edge& item) {
  1259     void writeEdge(std::string label, const Edge& item) {
  1260       edgeWriters.push_back(make_pair(name, &item));
  1260       edgeWriters.push_back(make_pair(label, &item));
  1261     }
  1261     }
  1262 
  1262 
  1263     /// \brief Add an undirected edge writer command for the UEdgeWriter.
  1263     /// \brief Add an undirected edge writer command for the UEdgeWriter.
  1264     ///
  1264     ///
  1265     /// Add an undirected edge writer command for the UEdgeWriter.
  1265     /// Add an undirected edge writer command for the UEdgeWriter.
  1266     void writeUEdge(const std::string& name, const UEdge& item) {
  1266     void writeUEdge(std::string label, const UEdge& item) {
  1267       uEdgeWriters.push_back(make_pair(name, &item));
  1267       uEdgeWriters.push_back(make_pair(label, &item));
  1268     }
  1268     }
  1269 
  1269 
  1270   protected:
  1270   protected:
  1271 
  1271 
  1272     /// \brief The header of the section.
  1272     /// \brief The header of the section.
  1284 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1284 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1285       }
  1285       }
  1286       if (!uEdgeLabelWriter->isLabelWriter()) {
  1286       if (!uEdgeLabelWriter->isLabelWriter()) {
  1287 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1287 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1288       }
  1288       }
  1289       for (int i = 0; i < (int)uEdgeWriters.size(); ++i) {
  1289       for (int i = 0; i < int(uEdgeWriters.size()); ++i) {
  1290 	os << uEdgeWriters[i].first << ' ';
  1290 	os << uEdgeWriters[i].first << ' ';
  1291 	uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second));
  1291 	uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second));
  1292 	os << std::endl;
  1292 	os << std::endl;
  1293       }
  1293       }
  1294       for (int i = 0; i < (int)edgeWriters.size(); ++i) {
  1294       for (int i = 0; i < int(edgeWriters.size()); ++i) {
  1295 	os << edgeWriters[i].first << ' ';
  1295 	os << edgeWriters[i].first << ' ';
  1296 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1296 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1297 	os << std::endl;
  1297 	os << std::endl;
  1298       }
  1298       }
  1299     }
  1299     }
  1361   public:
  1361   public:
  1362     /// \brief Add an attribute writer command for the writer.
  1362     /// \brief Add an attribute writer command for the writer.
  1363     ///
  1363     ///
  1364     /// Add an attribute writer command for the writer.
  1364     /// Add an attribute writer command for the writer.
  1365     template <typename Value>
  1365     template <typename Value>
  1366     AttributeWriter& writeAttribute(const std::string& name, 
  1366     AttributeWriter& writeAttribute(std::string label, 
  1367 				    const Value& value) {
  1367 				    const Value& value) {
  1368       return 
  1368       return 
  1369 	writeAttribute<typename Traits::template Writer<Value> >(name, value);
  1369 	writeAttribute<typename Traits::template Writer<Value> >(name, value);
  1370     }
  1370     }
  1371 
  1371 
  1372     /// \brief Add an attribute writer command for the writer.
  1372     /// \brief Add an attribute writer command for the writer.
  1373     ///
  1373     ///
  1374     /// Add an attribute writer command for the writer.
  1374     /// Add an attribute writer command for the writer.
  1375     template <typename Writer, typename Value>
  1375     template <typename ItemWriter, typename Value>
  1376     AttributeWriter& writeAttribute(const std::string& name, 
  1376     AttributeWriter& writeAttribute(std::string label, const Value& value,
  1377 				    const Value& value,
  1377 				    const ItemWriter& iw = ItemWriter()) {
  1378 				    const Writer& writer = Writer()) {
  1378       checkConcept<_writer_bits::ItemWriter<Value>, ItemWriter>();
  1379       checkConcept<_writer_bits::ItemWriter<Value>, Writer>();
  1379       writers.push_back(make_pair(label, new _writer_bits::
  1380       writers.push_back(make_pair(name, new _writer_bits::
  1380 				  ValueWriter<Value, ItemWriter>(value, iw)));
  1381 				  ValueWriter<Value, Writer>(value, writer)));
       
  1382       return *this;
  1381       return *this;
  1383     }
  1382     }
  1384 
  1383 
  1385   protected:
  1384   protected:
  1386 
  1385