lemon/lgf_writer.h
changeset 191 abc5b9d0c67e
parent 185 33e45a9b868c
child 192 7bf5f97d574f
equal deleted inserted replaced
7:1f72caef2a5a 8:524021b720a1
   302       }
   302       }
   303       return os;
   303       return os;
   304     }
   304     }
   305 
   305 
   306   }
   306   }
       
   307 
       
   308   template <typename Digraph>
       
   309   class DigraphWriter;
       
   310 
       
   311   template <typename Digraph>
       
   312   DigraphWriter<Digraph> digraphWriter(std::ostream& os, 
       
   313 				       const Digraph& digraph);
       
   314 
       
   315   template <typename Digraph>
       
   316   DigraphWriter<Digraph> digraphWriter(const std::string& fn, 
       
   317 				       const Digraph& digraph);
       
   318 
       
   319   template <typename Digraph>
       
   320   DigraphWriter<Digraph> digraphWriter(const char *fn, 
       
   321 				       const Digraph& digraph);
   307   
   322   
   308   /// \ingroup lemon_io
   323   /// \ingroup lemon_io
   309   ///  
   324   ///  
   310   /// \brief LGF writer for directed graphs
   325   /// \brief LGF writer for directed graphs
   311   ///
   326   ///
   360 
   375 
   361 
   376 
   362     std::ostream* _os;
   377     std::ostream* _os;
   363     bool local_os;
   378     bool local_os;
   364 
   379 
   365     Digraph& _digraph;
   380     const Digraph& _digraph;
   366 
   381 
   367     std::string _nodes_caption;
   382     std::string _nodes_caption;
   368     std::string _arcs_caption;
   383     std::string _arcs_caption;
   369     std::string _attributes_caption;
   384     std::string _attributes_caption;
   370     
   385     
   392 
   407 
   393     /// \brief Constructor
   408     /// \brief Constructor
   394     ///
   409     ///
   395     /// Construct a directed graph writer, which writes to the given
   410     /// Construct a directed graph writer, which writes to the given
   396     /// output stream.
   411     /// output stream.
   397     DigraphWriter(std::ostream& is, Digraph& digraph) 
   412     DigraphWriter(std::ostream& is, const Digraph& digraph) 
   398       : _os(&is), local_os(false), _digraph(digraph),
   413       : _os(&is), local_os(false), _digraph(digraph),
   399 	_skip_nodes(false), _skip_arcs(false) {}
   414 	_skip_nodes(false), _skip_arcs(false) {}
   400 
   415 
   401     /// \brief Constructor
   416     /// \brief Constructor
   402     ///
   417     ///
   403     /// Construct a directed graph writer, which writes to the given
   418     /// Construct a directed graph writer, which writes to the given
   404     /// output file.
   419     /// output file.
   405     DigraphWriter(const std::string& fn, Digraph& digraph) 
   420     DigraphWriter(const std::string& fn, const Digraph& digraph) 
   406       : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
   421       : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
   407 	_skip_nodes(false), _skip_arcs(false) {}
   422 	_skip_nodes(false), _skip_arcs(false) {}
   408 
   423 
   409     /// \brief Constructor
   424     /// \brief Constructor
   410     ///
   425     ///
   411     /// Construct a directed graph writer, which writes to the given
   426     /// Construct a directed graph writer, which writes to the given
   412     /// output file.
   427     /// output file.
   413     DigraphWriter(const char* fn, Digraph& digraph) 
   428     DigraphWriter(const char* fn, const Digraph& digraph) 
   414       : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
   429       : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
   415 	_skip_nodes(false), _skip_arcs(false) {}
   430 	_skip_nodes(false), _skip_arcs(false) {}
   416 
       
   417     /// \brief Copy constructor
       
   418     ///
       
   419     /// The copy constructor transfers all data from the other writer,
       
   420     /// therefore the copied writer will not be usable more. 
       
   421     DigraphWriter(DigraphWriter& other) 
       
   422       : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
       
   423 	_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
       
   424 
       
   425       other._os = 0;
       
   426       other.local_os = false;
       
   427 
       
   428       _node_index.swap(other._node_index);
       
   429       _arc_index.swap(other._arc_index);
       
   430 
       
   431       _node_maps.swap(other._node_maps);
       
   432       _arc_maps.swap(other._arc_maps);
       
   433       _attributes.swap(other._attributes);
       
   434 
       
   435       _nodes_caption = other._nodes_caption;
       
   436       _arcs_caption = other._arcs_caption;
       
   437       _attributes_caption = other._attributes_caption;
       
   438     }
       
   439 
   431 
   440     /// \brief Destructor
   432     /// \brief Destructor
   441     ~DigraphWriter() {
   433     ~DigraphWriter() {
   442       for (typename NodeMaps::iterator it = _node_maps.begin(); 
   434       for (typename NodeMaps::iterator it = _node_maps.begin(); 
   443 	   it != _node_maps.end(); ++it) {
   435 	   it != _node_maps.end(); ++it) {
   458 	delete _os;
   450 	delete _os;
   459       }
   451       }
   460     }
   452     }
   461 
   453 
   462   private:
   454   private:
       
   455 
       
   456     friend DigraphWriter<Digraph> digraphWriter<>(std::ostream& os, 
       
   457 						  const Digraph& digraph);
       
   458     friend DigraphWriter<Digraph> digraphWriter<>(const std::string& fn, 
       
   459 						  const Digraph& digraph);   
       
   460     friend DigraphWriter<Digraph> digraphWriter<>(const char *fn, 
       
   461 						  const Digraph& digraph);
       
   462 
       
   463     DigraphWriter(DigraphWriter& other) 
       
   464       : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
       
   465 	_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
       
   466 
       
   467       other._os = 0;
       
   468       other.local_os = false;
       
   469 
       
   470       _node_index.swap(other._node_index);
       
   471       _arc_index.swap(other._arc_index);
       
   472 
       
   473       _node_maps.swap(other._node_maps);
       
   474       _arc_maps.swap(other._arc_maps);
       
   475       _attributes.swap(other._attributes);
       
   476 
       
   477       _nodes_caption = other._nodes_caption;
       
   478       _arcs_caption = other._arcs_caption;
       
   479       _attributes_caption = other._attributes_caption;
       
   480     }
   463     
   481     
   464     DigraphWriter& operator=(const DigraphWriter&);
   482     DigraphWriter& operator=(const DigraphWriter&);
   465 
   483 
   466   public:
   484   public:
   467 
   485 
   842     /// @}
   860     /// @}
   843   };
   861   };
   844 
   862 
   845   /// \relates DigraphWriter
   863   /// \relates DigraphWriter
   846   template <typename Digraph>
   864   template <typename Digraph>
   847   DigraphWriter<Digraph> digraphWriter(std::ostream& os, Digraph& digraph) {
   865   DigraphWriter<Digraph> digraphWriter(std::ostream& os, 
       
   866 				       const Digraph& digraph) {
   848     DigraphWriter<Digraph> tmp(os, digraph);
   867     DigraphWriter<Digraph> tmp(os, digraph);
   849     return tmp;
   868     return tmp;
   850   }
   869   }
   851 
   870 
   852   /// \relates DigraphWriter
   871   /// \relates DigraphWriter
   853   template <typename Digraph>
   872   template <typename Digraph>
   854   DigraphWriter<Digraph> digraphWriter(const std::string& fn, 
   873   DigraphWriter<Digraph> digraphWriter(const std::string& fn, 
   855 				       Digraph& digraph) {
   874 				       const Digraph& digraph) {
   856     DigraphWriter<Digraph> tmp(fn, digraph);
   875     DigraphWriter<Digraph> tmp(fn, digraph);
   857     return tmp;
   876     return tmp;
   858   }
   877   }
   859 
   878 
   860   /// \relates DigraphWriter
   879   /// \relates DigraphWriter
   861   template <typename Digraph>
   880   template <typename Digraph>
   862   DigraphWriter<Digraph> digraphWriter(const char* fn, Digraph& digraph) {
   881   DigraphWriter<Digraph> digraphWriter(const char* fn, 
       
   882 				       const Digraph& digraph) {
   863     DigraphWriter<Digraph> tmp(fn, digraph);
   883     DigraphWriter<Digraph> tmp(fn, digraph);
   864     return tmp;
   884     return tmp;
   865   }
   885   }
       
   886 
       
   887   template <typename Graph>
       
   888   class GraphWriter;
       
   889 
       
   890   template <typename Graph>
       
   891   GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph);    
       
   892 
       
   893   template <typename Graph>
       
   894   GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph);   
       
   895 
       
   896   template <typename Graph>
       
   897   GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);    
   866 
   898 
   867   /// \ingroup lemon_io
   899   /// \ingroup lemon_io
   868   ///  
   900   ///  
   869   /// \brief LGF writer for directed graphs
   901   /// \brief LGF writer for directed graphs
   870   ///
   902   ///
   912 
   944 
   913     /// \brief Constructor
   945     /// \brief Constructor
   914     ///
   946     ///
   915     /// Construct a directed graph writer, which writes to the given
   947     /// Construct a directed graph writer, which writes to the given
   916     /// output stream.
   948     /// output stream.
   917     GraphWriter(std::ostream& is, Graph& graph) 
   949     GraphWriter(std::ostream& is, const Graph& graph) 
   918       : _os(&is), local_os(false), _graph(graph),
   950       : _os(&is), local_os(false), _graph(graph),
   919 	_skip_nodes(false), _skip_edges(false) {}
   951 	_skip_nodes(false), _skip_edges(false) {}
   920 
   952 
   921     /// \brief Constructor
   953     /// \brief Constructor
   922     ///
   954     ///
   923     /// Construct a directed graph writer, which writes to the given
   955     /// Construct a directed graph writer, which writes to the given
   924     /// output file.
   956     /// output file.
   925     GraphWriter(const std::string& fn, Graph& graph) 
   957     GraphWriter(const std::string& fn, const Graph& graph) 
   926       : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
   958       : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
   927 	_skip_nodes(false), _skip_edges(false) {}
   959 	_skip_nodes(false), _skip_edges(false) {}
   928 
   960 
   929     /// \brief Constructor
   961     /// \brief Constructor
   930     ///
   962     ///
   931     /// Construct a directed graph writer, which writes to the given
   963     /// Construct a directed graph writer, which writes to the given
   932     /// output file.
   964     /// output file.
   933     GraphWriter(const char* fn, Graph& graph) 
   965     GraphWriter(const char* fn, const Graph& graph) 
   934       : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
   966       : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
   935 	_skip_nodes(false), _skip_edges(false) {}
   967 	_skip_nodes(false), _skip_edges(false) {}
   936 
       
   937     /// \brief Copy constructor
       
   938     ///
       
   939     /// The copy constructor transfers all data from the other writer,
       
   940     /// therefore the copied writer will not be usable more. 
       
   941     GraphWriter(GraphWriter& other) 
       
   942       : _os(other._os), local_os(other.local_os), _graph(other._graph),
       
   943 	_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
       
   944 
       
   945       other._os = 0;
       
   946       other.local_os = false;
       
   947 
       
   948       _node_index.swap(other._node_index);
       
   949       _edge_index.swap(other._edge_index);
       
   950 
       
   951       _node_maps.swap(other._node_maps);
       
   952       _edge_maps.swap(other._edge_maps);
       
   953       _attributes.swap(other._attributes);
       
   954 
       
   955       _nodes_caption = other._nodes_caption;
       
   956       _edges_caption = other._edges_caption;
       
   957       _attributes_caption = other._attributes_caption;
       
   958     }
       
   959 
   968 
   960     /// \brief Destructor
   969     /// \brief Destructor
   961     ~GraphWriter() {
   970     ~GraphWriter() {
   962       for (typename NodeMaps::iterator it = _node_maps.begin(); 
   971       for (typename NodeMaps::iterator it = _node_maps.begin(); 
   963 	   it != _node_maps.end(); ++it) {
   972 	   it != _node_maps.end(); ++it) {
   976 
   985 
   977       if (local_os) {
   986       if (local_os) {
   978 	delete _os;
   987 	delete _os;
   979       }
   988       }
   980     }
   989     }
   981 
   990     
   982   private:
   991   private:
   983     
   992 
       
   993     friend GraphWriter<Graph> graphWriter<>(std::ostream& os, 
       
   994 					    const Graph& graph);    
       
   995     friend GraphWriter<Graph> graphWriter<>(const std::string& fn, 
       
   996 					    const Graph& graph);   
       
   997     friend GraphWriter<Graph> graphWriter<>(const char *fn, 
       
   998 					    const Graph& graph);    
       
   999 
       
  1000     GraphWriter(GraphWriter& other) 
       
  1001       : _os(other._os), local_os(other.local_os), _graph(other._graph),
       
  1002 	_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
       
  1003 
       
  1004       other._os = 0;
       
  1005       other.local_os = false;
       
  1006 
       
  1007       _node_index.swap(other._node_index);
       
  1008       _edge_index.swap(other._edge_index);
       
  1009 
       
  1010       _node_maps.swap(other._node_maps);
       
  1011       _edge_maps.swap(other._edge_maps);
       
  1012       _attributes.swap(other._attributes);
       
  1013 
       
  1014       _nodes_caption = other._nodes_caption;
       
  1015       _edges_caption = other._edges_caption;
       
  1016       _attributes_caption = other._attributes_caption;
       
  1017     }
       
  1018 
   984     GraphWriter& operator=(const GraphWriter&);
  1019     GraphWriter& operator=(const GraphWriter&);
   985 
  1020 
   986   public:
  1021   public:
   987 
  1022 
   988     /// \name Writing rules
  1023     /// \name Writing rules
  1408     /// @}
  1443     /// @}
  1409   };
  1444   };
  1410 
  1445 
  1411   /// \relates GraphWriter
  1446   /// \relates GraphWriter
  1412   template <typename Graph>
  1447   template <typename Graph>
  1413   GraphWriter<Graph> graphWriter(std::ostream& os, Graph& graph) {
  1448   GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
  1414     GraphWriter<Graph> tmp(os, graph);
  1449     GraphWriter<Graph> tmp(os, graph);
  1415     return tmp;
  1450     return tmp;
  1416   }
  1451   }
  1417 
  1452 
  1418   /// \relates GraphWriter
  1453   /// \relates GraphWriter
  1419   template <typename Graph>
  1454   template <typename Graph>
  1420   GraphWriter<Graph> graphWriter(const std::string& fn, Graph& graph) {
  1455   GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
  1421     GraphWriter<Graph> tmp(fn, graph);
  1456     GraphWriter<Graph> tmp(fn, graph);
  1422     return tmp;
  1457     return tmp;
  1423   }
  1458   }
  1424 
  1459 
  1425   /// \relates GraphWriter
  1460   /// \relates GraphWriter
  1426   template <typename Graph>
  1461   template <typename Graph>
  1427   GraphWriter<Graph> graphWriter(const char* fn, Graph& graph) {
  1462   GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
  1428     GraphWriter<Graph> tmp(fn, graph);
  1463     GraphWriter<Graph> tmp(fn, graph);
  1429     return tmp;
  1464     return tmp;
  1430   }
  1465   }
  1431 }
  1466 }
  1432 
  1467