lemon/lgf_writer.h
changeset 187 84c2a2e5cfee
parent 165 b4c336c27a03
child 190 1e6af6f0843c
equal deleted inserted replaced
6:ed00337fdec4 7:1f72caef2a5a
   601     /// \brief Skip writing the node set
   601     /// \brief Skip writing the node set
   602     ///
   602     ///
   603     /// The \c \@nodes section will be not written to the stream.
   603     /// The \c \@nodes section will be not written to the stream.
   604     DigraphWriter& skipNodes() {
   604     DigraphWriter& skipNodes() {
   605       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
   605       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
       
   606       _skip_nodes = true;
   606       return *this;
   607       return *this;
   607     }
   608     }
   608 
   609 
   609     /// \brief Skip writing arc set
   610     /// \brief Skip writing arc set
   610     ///
   611     ///
   611     /// The \c \@arcs section will be not written to the stream.
   612     /// The \c \@arcs section will be not written to the stream.
   612     DigraphWriter& skipArcs() {
   613     DigraphWriter& skipArcs() {
   613       LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
   614       LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
       
   615       _skip_arcs = true;
   614       return *this;
   616       return *this;
   615     }
   617     }
   616 
   618 
   617     /// @}
   619     /// @}
   618 
   620 
   673 	    _node_index.insert(std::make_pair(n, value));
   675 	    _node_index.insert(std::make_pair(n, value));
   674 	  }
   676 	  }
   675 	  *_os << '\t';
   677 	  *_os << '\t';
   676 	}
   678 	}
   677 	*_os << std::endl;
   679 	*_os << std::endl;
       
   680       }
       
   681     }
       
   682 
       
   683     void createNodeIndex() {
       
   684       _writer_bits::MapStorageBase<Node>* label = 0;
       
   685       for (typename NodeMaps::iterator it = _node_maps.begin();
       
   686 	   it != _node_maps.end(); ++it) {
       
   687         if (it->first == "label") {
       
   688 	  label = it->second;
       
   689 	  break;
       
   690 	}
       
   691       }
       
   692 
       
   693       if (label == 0) {
       
   694 	for (NodeIt n(_digraph); n != INVALID; ++n) {
       
   695 	  std::ostringstream os;
       
   696 	  os << _digraph.id(n);
       
   697 	  _node_index.insert(std::make_pair(n, os.str()));	  
       
   698 	}	
       
   699       } else {
       
   700 	for (NodeIt n(_digraph); n != INVALID; ++n) {
       
   701 	  std::string value = label->get(n);	  
       
   702 	  _node_index.insert(std::make_pair(n, value));
       
   703 	}
   678       }
   704       }
   679     }
   705     }
   680 
   706 
   681     void writeArcs() {
   707     void writeArcs() {
   682       _writer_bits::MapStorageBase<Arc>* label = 0;
   708       _writer_bits::MapStorageBase<Arc>* label = 0;
   743 	}
   769 	}
   744 	*_os << std::endl;
   770 	*_os << std::endl;
   745       }
   771       }
   746     }
   772     }
   747 
   773 
       
   774     void createArcIndex() {
       
   775       _writer_bits::MapStorageBase<Arc>* label = 0;
       
   776       for (typename ArcMaps::iterator it = _arc_maps.begin();
       
   777 	   it != _arc_maps.end(); ++it) {
       
   778         if (it->first == "label") {
       
   779 	  label = it->second;
       
   780 	  break;
       
   781 	}
       
   782       }
       
   783 
       
   784       if (label == 0) {
       
   785 	for (ArcIt a(_digraph); a != INVALID; ++a) {
       
   786 	  std::ostringstream os;
       
   787 	  os << _digraph.id(a);
       
   788 	  _arc_index.insert(std::make_pair(a, os.str()));	  
       
   789 	}	
       
   790       } else {
       
   791 	for (ArcIt a(_digraph); a != INVALID; ++a) {
       
   792 	  std::string value = label->get(a);	  
       
   793 	  _arc_index.insert(std::make_pair(a, value));
       
   794 	}
       
   795       }
       
   796     }
       
   797 
   748     void writeAttributes() {
   798     void writeAttributes() {
   749       if (_attributes.empty()) return;
   799       if (_attributes.empty()) return;
   750       *_os << "@attributes";
   800       *_os << "@attributes";
   751       if (!_attributes_caption.empty()) {
   801       if (!_attributes_caption.empty()) {
   752 	_writer_bits::writeToken(*_os << ' ', _attributes_caption);
   802 	_writer_bits::writeToken(*_os << ' ', _attributes_caption);
   769     ///
   819     ///
   770     /// This function starts the batch processing
   820     /// This function starts the batch processing
   771     void run() {
   821     void run() {
   772       if (!_skip_nodes) {
   822       if (!_skip_nodes) {
   773 	writeNodes();
   823 	writeNodes();
       
   824       } else {
       
   825 	createNodeIndex();
   774       }
   826       }
   775       if (!_skip_arcs) {      
   827       if (!_skip_arcs) {      
   776 	writeArcs();
   828 	writeArcs();
       
   829       } else {
       
   830 	createArcIndex();
   777       }
   831       }
   778       writeAttributes();
   832       writeAttributes();
   779     }
   833     }
   780 
   834 
   781     /// \brief Gives back the stream of the writer
   835     /// \brief Gives back the stream of the writer
  1113     /// \brief Skip writing the node set
  1167     /// \brief Skip writing the node set
  1114     ///
  1168     ///
  1115     /// The \c \@nodes section will be not written to the stream.
  1169     /// The \c \@nodes section will be not written to the stream.
  1116     GraphWriter& skipNodes() {
  1170     GraphWriter& skipNodes() {
  1117       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
  1171       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
       
  1172       _skip_nodes = true;
  1118       return *this;
  1173       return *this;
  1119     }
  1174     }
  1120 
  1175 
  1121     /// \brief Skip writing edge set
  1176     /// \brief Skip writing edge set
  1122     ///
  1177     ///
  1123     /// The \c \@edges section will be not written to the stream.
  1178     /// The \c \@edges section will be not written to the stream.
  1124     GraphWriter& skipEdges() {
  1179     GraphWriter& skipEdges() {
  1125       LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
  1180       LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
       
  1181       _skip_edges = true;
  1126       return *this;
  1182       return *this;
  1127     }
  1183     }
  1128 
  1184 
  1129     /// @}
  1185     /// @}
  1130 
  1186 
  1185 	    _node_index.insert(std::make_pair(n, value));
  1241 	    _node_index.insert(std::make_pair(n, value));
  1186 	  }
  1242 	  }
  1187 	  *_os << '\t';
  1243 	  *_os << '\t';
  1188 	}
  1244 	}
  1189 	*_os << std::endl;
  1245 	*_os << std::endl;
       
  1246       }
       
  1247     }
       
  1248 
       
  1249     void createNodeIndex() {
       
  1250       _writer_bits::MapStorageBase<Node>* label = 0;
       
  1251       for (typename NodeMaps::iterator it = _node_maps.begin();
       
  1252 	   it != _node_maps.end(); ++it) {
       
  1253         if (it->first == "label") {
       
  1254 	  label = it->second;
       
  1255 	  break;
       
  1256 	}
       
  1257       }
       
  1258 
       
  1259       if (label == 0) {
       
  1260 	for (NodeIt n(_graph); n != INVALID; ++n) {
       
  1261 	  std::ostringstream os;
       
  1262 	  os << _graph.id(n);
       
  1263 	  _node_index.insert(std::make_pair(n, os.str()));	  
       
  1264 	}	
       
  1265       } else {
       
  1266 	for (NodeIt n(_graph); n != INVALID; ++n) {
       
  1267 	  std::string value = label->get(n);	  
       
  1268 	  _node_index.insert(std::make_pair(n, value));
       
  1269 	}
  1190       }
  1270       }
  1191     }
  1271     }
  1192 
  1272 
  1193     void writeEdges() {
  1273     void writeEdges() {
  1194       _writer_bits::MapStorageBase<Edge>* label = 0;
  1274       _writer_bits::MapStorageBase<Edge>* label = 0;
  1255 	}
  1335 	}
  1256 	*_os << std::endl;
  1336 	*_os << std::endl;
  1257       }
  1337       }
  1258     }
  1338     }
  1259 
  1339 
       
  1340     void createEdgeIndex() {
       
  1341       _writer_bits::MapStorageBase<Edge>* label = 0;
       
  1342       for (typename EdgeMaps::iterator it = _edge_maps.begin();
       
  1343 	   it != _edge_maps.end(); ++it) {
       
  1344         if (it->first == "label") {
       
  1345 	  label = it->second;
       
  1346 	  break;
       
  1347 	}
       
  1348       }
       
  1349 
       
  1350       if (label == 0) {
       
  1351 	for (EdgeIt e(_graph); e != INVALID; ++e) {
       
  1352 	  std::ostringstream os;
       
  1353 	  os << _graph.id(e);
       
  1354 	  _edge_index.insert(std::make_pair(e, os.str()));	  
       
  1355 	}	
       
  1356       } else {
       
  1357 	for (EdgeIt e(_graph); e != INVALID; ++e) {
       
  1358 	  std::string value = label->get(e);	  
       
  1359 	  _edge_index.insert(std::make_pair(e, value));
       
  1360 	}
       
  1361       }
       
  1362     }
       
  1363 
  1260     void writeAttributes() {
  1364     void writeAttributes() {
  1261       if (_attributes.empty()) return;
  1365       if (_attributes.empty()) return;
  1262       *_os << "@attributes";
  1366       *_os << "@attributes";
  1263       if (!_attributes_caption.empty()) {
  1367       if (!_attributes_caption.empty()) {
  1264 	_writer_bits::writeToken(*_os << ' ', _attributes_caption);
  1368 	_writer_bits::writeToken(*_os << ' ', _attributes_caption);
  1281     ///
  1385     ///
  1282     /// This function starts the batch processing
  1386     /// This function starts the batch processing
  1283     void run() {
  1387     void run() {
  1284       if (!_skip_nodes) {
  1388       if (!_skip_nodes) {
  1285 	writeNodes();
  1389 	writeNodes();
       
  1390       } else {
       
  1391 	createNodeIndex();
  1286       }
  1392       }
  1287       if (!_skip_edges) {      
  1393       if (!_skip_edges) {      
  1288 	writeEdges();
  1394 	writeEdges();
       
  1395       } else {
       
  1396 	createEdgeIndex();
  1289       }
  1397       }
  1290       writeAttributes();
  1398       writeAttributes();
  1291     }
  1399     }
  1292 
  1400 
  1293     /// \brief Gives back the stream of the writer
  1401     /// \brief Gives back the stream of the writer