lemon/lgf_writer.h
changeset 195 aa45ff44fcf3
parent 190 1e6af6f0843c
child 197 5893bacaa720
equal deleted inserted replaced
8:524021b720a1 9:3669c8d9bd87
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 ///\ingroup lemon_io
    19 ///\ingroup lemon_io
    20 ///\file
    20 ///\file
    21 ///\brief Lemon Graph Format writer.
    21 ///\brief \ref lgf-format "Lemon Graph Format" writer.
    22 
    22 
    23 
    23 
    24 #ifndef LEMON_LGF_WRITER_H
    24 #ifndef LEMON_LGF_WRITER_H
    25 #define LEMON_LGF_WRITER_H
    25 #define LEMON_LGF_WRITER_H
    26 
    26 
   320   DigraphWriter<Digraph> digraphWriter(const char *fn, 
   320   DigraphWriter<Digraph> digraphWriter(const char *fn, 
   321 				       const Digraph& digraph);
   321 				       const Digraph& digraph);
   322   
   322   
   323   /// \ingroup lemon_io
   323   /// \ingroup lemon_io
   324   ///  
   324   ///  
   325   /// \brief LGF writer for directed graphs
   325   /// \brief \ref lgf-format "LGF" writer for directed graphs
   326   ///
   326   ///
   327   /// This utility writes an \ref lgf-format "LGF" file.
   327   /// This utility writes an \ref lgf-format "LGF" file.
   328   ///
   328   ///
   329   /// The writing method does a batch processing. The user creates a
   329   /// The writing method does a batch processing. The user creates a
   330   /// writer object, then various writing rules can be added to the
   330   /// writer object, then various writing rules can be added to the
   331   /// writer, and eventually the writing is executed with the \c run()
   331   /// writer, and eventually the writing is executed with the \c run()
   332   /// member function. A map writing rule can be added to the writer
   332   /// member function. A map writing rule can be added to the writer
   333   /// with the \c nodeMap() or \c arcMap() members. An optional
   333   /// with the \c nodeMap() or \c arcMap() members. An optional
   334   /// converter parameter can also be added as a standard functor
   334   /// converter parameter can also be added as a standard functor
   335   /// converting from the value type of the map to std::string. If it
   335   /// converting from the value type of the map to \c std::string. If it
   336   /// is set, it will determine how the map's value type is written to
   336   /// is set, it will determine how the value type of the map is written to
   337   /// the output stream. If the functor is not set, then a default
   337   /// the output stream. If the functor is not set, then a default
   338   /// conversion will be used. The \c attribute(), \c node() and \c
   338   /// conversion will be used. The \c attribute(), \c node() and \c
   339   /// arc() functions are used to add attribute writing rules.
   339   /// arc() functions are used to add attribute writing rules.
   340   ///
   340   ///
   341   ///\code
   341   ///\code
   342   ///     DigraphWriter<Digraph>(std::cout, digraph).
   342   /// DigraphWriter<Digraph>(std::cout, digraph).
   343   ///       nodeMap("coordinates", coord_map).
   343   ///   nodeMap("coordinates", coord_map).
   344   ///       nodeMap("size", size).
   344   ///   nodeMap("size", size).
   345   ///       nodeMap("title", title).
   345   ///   nodeMap("title", title).
   346   ///       arcMap("capacity", cap_map).
   346   ///   arcMap("capacity", cap_map).
   347   ///       node("source", src).
   347   ///   node("source", src).
   348   ///       node("target", trg).
   348   ///   node("target", trg).
   349   ///       attribute("caption", caption).
   349   ///   attribute("caption", caption).
   350   ///       run();
   350   ///   run();
   351   ///\endcode
   351   ///\endcode
   352   ///
   352   ///
   353   ///
   353   ///
   354   /// By default, the writer does not write additional captions to the
   354   /// By default, the writer does not write additional captions to the
   355   /// sections, but they can be give as an optional parameter of
   355   /// sections, but they can be give as an optional parameter of
   484   public:
   484   public:
   485 
   485 
   486     /// \name Writing rules
   486     /// \name Writing rules
   487     /// @{
   487     /// @{
   488     
   488     
   489     /// \brief Node map reading rule
   489     /// \brief Node map writing rule
   490     ///
   490     ///
   491     /// Add a node map reading rule to the writer.
   491     /// Add a node map writing rule to the writer.
   492     template <typename Map>
   492     template <typename Map>
   493     DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
   493     DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
   494       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   494       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   495       _writer_bits::MapStorageBase<Node>* storage = 
   495       _writer_bits::MapStorageBase<Node>* storage = 
   496 	new _writer_bits::MapStorage<Node, Map>(map);
   496 	new _writer_bits::MapStorage<Node, Map>(map);
   584 	new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
   584 	new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
   585       _attributes.push_back(std::make_pair(caption, storage));
   585       _attributes.push_back(std::make_pair(caption, storage));
   586       return *this;
   586       return *this;
   587     }
   587     }
   588 
   588 
   589     /// \name Select section by name
   589     /// \name Section captions
   590     /// @{
   590     /// @{
   591 
   591 
   592     /// \brief Set \c \@nodes section to be read
   592     /// \brief Add an additional caption to the \c \@nodes section
   593     ///
   593     ///
   594     /// Set \c \@nodes section to be read
   594     /// Add an additional caption to the \c \@nodes section.
   595     DigraphWriter& nodes(const std::string& caption) {
   595     DigraphWriter& nodes(const std::string& caption) {
   596       _nodes_caption = caption;
   596       _nodes_caption = caption;
   597       return *this;
   597       return *this;
   598     }
   598     }
   599 
   599 
   600     /// \brief Set \c \@arcs section to be read
   600     /// \brief Add an additional caption to the \c \@arcs section
   601     ///
   601     ///
   602     /// Set \c \@arcs section to be read
   602     /// Add an additional caption to the \c \@arcs section.
   603     DigraphWriter& arcs(const std::string& caption) {
   603     DigraphWriter& arcs(const std::string& caption) {
   604       _arcs_caption = caption;
   604       _arcs_caption = caption;
   605       return *this;
   605       return *this;
   606     }
   606     }
   607 
   607 
   608     /// \brief Set \c \@attributes section to be read
   608     /// \brief Add an additional caption to the \c \@attributes section
   609     ///
   609     ///
   610     /// Set \c \@attributes section to be read
   610     /// Add an additional caption to the \c \@attributes section.
   611     DigraphWriter& attributes(const std::string& caption) {
   611     DigraphWriter& attributes(const std::string& caption) {
   612       _attributes_caption = caption;
   612       _attributes_caption = caption;
   613       return *this;
   613       return *this;
   614     }
   614     }
   615 
   615 
   616     /// \name Skipping section
   616     /// \name Skipping section
   617     /// @{
   617     /// @{
   618 
   618 
   619     /// \brief Skip writing the node set
   619     /// \brief Skip writing the node set
   620     ///
   620     ///
   621     /// The \c \@nodes section will be not written to the stream.
   621     /// The \c \@nodes section will not be written to the stream.
   622     DigraphWriter& skipNodes() {
   622     DigraphWriter& skipNodes() {
   623       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
   623       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
   624       _skip_nodes = true;
   624       _skip_nodes = true;
   625       return *this;
   625       return *this;
   626     }
   626     }
   627 
   627 
   628     /// \brief Skip writing arc set
   628     /// \brief Skip writing arc set
   629     ///
   629     ///
   630     /// The \c \@arcs section will be not written to the stream.
   630     /// The \c \@arcs section will not be written to the stream.
   631     DigraphWriter& skipArcs() {
   631     DigraphWriter& skipArcs() {
   632       LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
   632       LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
   633       _skip_arcs = true;
   633       _skip_arcs = true;
   634       return *this;
   634       return *this;
   635     }
   635     }
   833     /// \name Execution of the writer    
   833     /// \name Execution of the writer    
   834     /// @{
   834     /// @{
   835 
   835 
   836     /// \brief Start the batch processing
   836     /// \brief Start the batch processing
   837     ///
   837     ///
   838     /// This function starts the batch processing
   838     /// This function starts the batch processing.
   839     void run() {
   839     void run() {
   840       if (!_skip_nodes) {
   840       if (!_skip_nodes) {
   841 	writeNodes();
   841 	writeNodes();
   842       } else {
   842       } else {
   843 	createNodeIndex();
   843 	createNodeIndex();
   848 	createArcIndex();
   848 	createArcIndex();
   849       }
   849       }
   850       writeAttributes();
   850       writeAttributes();
   851     }
   851     }
   852 
   852 
   853     /// \brief Gives back the stream of the writer
   853     /// \brief Give back the stream of the writer
   854     ///
   854     ///
   855     /// Gives back the stream of the writer
   855     /// Give back the stream of the writer.
   856     std::ostream& ostream() {
   856     std::ostream& ostream() {
   857       return *_os;
   857       return *_os;
   858     }
   858     }
   859 
   859 
   860     /// @}
   860     /// @}
   861   };
   861   };
   862 
   862 
       
   863   /// \brief Return a \ref DigraphWriter class
       
   864   /// 
       
   865   /// This function just returns a \ref DigraphWriter class.
   863   /// \relates DigraphWriter
   866   /// \relates DigraphWriter
   864   template <typename Digraph>
   867   template <typename Digraph>
   865   DigraphWriter<Digraph> digraphWriter(std::ostream& os, 
   868   DigraphWriter<Digraph> digraphWriter(std::ostream& os, 
   866 				       const Digraph& digraph) {
   869 				       const Digraph& digraph) {
   867     DigraphWriter<Digraph> tmp(os, digraph);
   870     DigraphWriter<Digraph> tmp(os, digraph);
   868     return tmp;
   871     return tmp;
   869   }
   872   }
   870 
   873 
       
   874   /// \brief Return a \ref DigraphWriter class
       
   875   /// 
       
   876   /// This function just returns a \ref DigraphWriter class.
   871   /// \relates DigraphWriter
   877   /// \relates DigraphWriter
   872   template <typename Digraph>
   878   template <typename Digraph>
   873   DigraphWriter<Digraph> digraphWriter(const std::string& fn, 
   879   DigraphWriter<Digraph> digraphWriter(const std::string& fn, 
   874 				       const Digraph& digraph) {
   880 				       const Digraph& digraph) {
   875     DigraphWriter<Digraph> tmp(fn, digraph);
   881     DigraphWriter<Digraph> tmp(fn, digraph);
   876     return tmp;
   882     return tmp;
   877   }
   883   }
   878 
   884 
       
   885   /// \brief Return a \ref DigraphWriter class
       
   886   /// 
       
   887   /// This function just returns a \ref DigraphWriter class.
   879   /// \relates DigraphWriter
   888   /// \relates DigraphWriter
   880   template <typename Digraph>
   889   template <typename Digraph>
   881   DigraphWriter<Digraph> digraphWriter(const char* fn, 
   890   DigraphWriter<Digraph> digraphWriter(const char* fn, 
   882 				       const Digraph& digraph) {
   891 				       const Digraph& digraph) {
   883     DigraphWriter<Digraph> tmp(fn, digraph);
   892     DigraphWriter<Digraph> tmp(fn, digraph);
   896   template <typename Graph>
   905   template <typename Graph>
   897   GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);    
   906   GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);    
   898 
   907 
   899   /// \ingroup lemon_io
   908   /// \ingroup lemon_io
   900   ///  
   909   ///  
   901   /// \brief LGF writer for directed graphs
   910   /// \brief \ref lgf-format "LGF" writer for directed graphs
   902   ///
   911   ///
   903   /// This utility writes an \ref lgf-format "LGF" file.
   912   /// This utility writes an \ref lgf-format "LGF" file.
       
   913   ///
       
   914   /// It can be used almost the same way as \c DigraphWriter.
       
   915   /// The only difference is that this class can handle edges and
       
   916   /// edge maps as well as arcs and arc maps.
   904   template <typename _Graph>
   917   template <typename _Graph>
   905   class GraphWriter {
   918   class GraphWriter {
   906   public:
   919   public:
   907 
   920 
   908     typedef _Graph Graph;
   921     typedef _Graph Graph;
  1021   public:
  1034   public:
  1022 
  1035 
  1023     /// \name Writing rules
  1036     /// \name Writing rules
  1024     /// @{
  1037     /// @{
  1025     
  1038     
  1026     /// \brief Node map reading rule
  1039     /// \brief Node map writing rule
  1027     ///
  1040     ///
  1028     /// Add a node map reading rule to the writer.
  1041     /// Add a node map writing rule to the writer.
  1029     template <typename Map>
  1042     template <typename Map>
  1030     GraphWriter& nodeMap(const std::string& caption, const Map& map) {
  1043     GraphWriter& nodeMap(const std::string& caption, const Map& map) {
  1031       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
  1044       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
  1032       _writer_bits::MapStorageBase<Node>* storage = 
  1045       _writer_bits::MapStorageBase<Node>* storage = 
  1033 	new _writer_bits::MapStorage<Node, Map>(map);
  1046 	new _writer_bits::MapStorage<Node, Map>(map);
  1167 	new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
  1180 	new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
  1168       _attributes.push_back(std::make_pair(caption, storage));
  1181       _attributes.push_back(std::make_pair(caption, storage));
  1169       return *this;
  1182       return *this;
  1170     }
  1183     }
  1171 
  1184 
  1172     /// \name Select section by name
  1185     /// \name Section captions
  1173     /// @{
  1186     /// @{
  1174 
  1187 
  1175     /// \brief Set \c \@nodes section to be read
  1188     /// \brief Add an additional caption to the \c \@nodes section
  1176     ///
  1189     ///
  1177     /// Set \c \@nodes section to be read
  1190     /// Add an additional caption to the \c \@nodes section.
  1178     GraphWriter& nodes(const std::string& caption) {
  1191     GraphWriter& nodes(const std::string& caption) {
  1179       _nodes_caption = caption;
  1192       _nodes_caption = caption;
  1180       return *this;
  1193       return *this;
  1181     }
  1194     }
  1182 
  1195 
  1183     /// \brief Set \c \@edges section to be read
  1196     /// \brief Add an additional caption to the \c \@arcs section
  1184     ///
  1197     ///
  1185     /// Set \c \@edges section to be read
  1198     /// Add an additional caption to the \c \@arcs section.
  1186     GraphWriter& edges(const std::string& caption) {
  1199     GraphWriter& edges(const std::string& caption) {
  1187       _edges_caption = caption;
  1200       _edges_caption = caption;
  1188       return *this;
  1201       return *this;
  1189     }
  1202     }
  1190 
  1203 
  1191     /// \brief Set \c \@attributes section to be read
  1204     /// \brief Add an additional caption to the \c \@attributes section
  1192     ///
  1205     ///
  1193     /// Set \c \@attributes section to be read
  1206     /// Add an additional caption to the \c \@attributes section.
  1194     GraphWriter& attributes(const std::string& caption) {
  1207     GraphWriter& attributes(const std::string& caption) {
  1195       _attributes_caption = caption;
  1208       _attributes_caption = caption;
  1196       return *this;
  1209       return *this;
  1197     }
  1210     }
  1198 
  1211 
  1199     /// \name Skipping section
  1212     /// \name Skipping section
  1200     /// @{
  1213     /// @{
  1201 
  1214 
  1202     /// \brief Skip writing the node set
  1215     /// \brief Skip writing the node set
  1203     ///
  1216     ///
  1204     /// The \c \@nodes section will be not written to the stream.
  1217     /// The \c \@nodes section will not be written to the stream.
  1205     GraphWriter& skipNodes() {
  1218     GraphWriter& skipNodes() {
  1206       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
  1219       LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
  1207       _skip_nodes = true;
  1220       _skip_nodes = true;
  1208       return *this;
  1221       return *this;
  1209     }
  1222     }
  1210 
  1223 
  1211     /// \brief Skip writing edge set
  1224     /// \brief Skip writing edge set
  1212     ///
  1225     ///
  1213     /// The \c \@edges section will be not written to the stream.
  1226     /// The \c \@edges section will not be written to the stream.
  1214     GraphWriter& skipEdges() {
  1227     GraphWriter& skipEdges() {
  1215       LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
  1228       LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
  1216       _skip_edges = true;
  1229       _skip_edges = true;
  1217       return *this;
  1230       return *this;
  1218     }
  1231     }
  1416     /// \name Execution of the writer    
  1429     /// \name Execution of the writer    
  1417     /// @{
  1430     /// @{
  1418 
  1431 
  1419     /// \brief Start the batch processing
  1432     /// \brief Start the batch processing
  1420     ///
  1433     ///
  1421     /// This function starts the batch processing
  1434     /// This function starts the batch processing.
  1422     void run() {
  1435     void run() {
  1423       if (!_skip_nodes) {
  1436       if (!_skip_nodes) {
  1424 	writeNodes();
  1437 	writeNodes();
  1425       } else {
  1438       } else {
  1426 	createNodeIndex();
  1439 	createNodeIndex();
  1431 	createEdgeIndex();
  1444 	createEdgeIndex();
  1432       }
  1445       }
  1433       writeAttributes();
  1446       writeAttributes();
  1434     }
  1447     }
  1435 
  1448 
  1436     /// \brief Gives back the stream of the writer
  1449     /// \brief Give back the stream of the writer
  1437     ///
  1450     ///
  1438     /// Gives back the stream of the writer
  1451     /// Give back the stream of the writer
  1439     std::ostream& ostream() {
  1452     std::ostream& ostream() {
  1440       return *_os;
  1453       return *_os;
  1441     }
  1454     }
  1442 
  1455 
  1443     /// @}
  1456     /// @}
  1444   };
  1457   };
  1445 
  1458 
       
  1459   /// \brief Return a \ref GraphWriter class
       
  1460   /// 
       
  1461   /// This function just returns a \ref GraphWriter class.
  1446   /// \relates GraphWriter
  1462   /// \relates GraphWriter
  1447   template <typename Graph>
  1463   template <typename Graph>
  1448   GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
  1464   GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
  1449     GraphWriter<Graph> tmp(os, graph);
  1465     GraphWriter<Graph> tmp(os, graph);
  1450     return tmp;
  1466     return tmp;
  1451   }
  1467   }
  1452 
  1468 
       
  1469   /// \brief Return a \ref GraphWriter class
       
  1470   /// 
       
  1471   /// This function just returns a \ref GraphWriter class.
  1453   /// \relates GraphWriter
  1472   /// \relates GraphWriter
  1454   template <typename Graph>
  1473   template <typename Graph>
  1455   GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
  1474   GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
  1456     GraphWriter<Graph> tmp(fn, graph);
  1475     GraphWriter<Graph> tmp(fn, graph);
  1457     return tmp;
  1476     return tmp;
  1458   }
  1477   }
  1459 
  1478 
       
  1479   /// \brief Return a \ref GraphWriter class
       
  1480   /// 
       
  1481   /// This function just returns a \ref GraphWriter class.
  1460   /// \relates GraphWriter
  1482   /// \relates GraphWriter
  1461   template <typename Graph>
  1483   template <typename Graph>
  1462   GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
  1484   GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
  1463     GraphWriter<Graph> tmp(fn, graph);
  1485     GraphWriter<Graph> tmp(fn, graph);
  1464     return tmp;
  1486     return tmp;