lemon/lgf_reader.h
changeset 195 aa45ff44fcf3
parent 190 1e6af6f0843c
child 197 5893bacaa720
equal deleted inserted replaced
18:61133d65d026 19:a44ec635a2db
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 ///\ingroup lemon_io
    19 ///\ingroup lemon_io
    20 ///\file
    20 ///\file
    21 ///\brief Lemon Graph Format reader.
    21 ///\brief \ref lgf-format "Lemon Graph Format" reader.
    22 
    22 
    23 
    23 
    24 #ifndef LEMON_LGF_READER_H
    24 #ifndef LEMON_LGF_READER_H
    25 #define LEMON_LGF_READER_H
    25 #define LEMON_LGF_READER_H
    26 
    26 
   398   template <typename Digraph>
   398   template <typename Digraph>
   399   DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph);
   399   DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph);
   400 
   400 
   401   /// \ingroup lemon_io
   401   /// \ingroup lemon_io
   402   ///  
   402   ///  
   403   /// \brief LGF reader for directed graphs
   403   /// \brief \ref lgf-format "LGF" reader for directed graphs
   404   ///
   404   ///
   405   /// This utility reads an \ref lgf-format "LGF" file.
   405   /// This utility reads an \ref lgf-format "LGF" file.
   406   ///
   406   ///
   407   /// The reading method does a batch processing. The user creates a
   407   /// The reading method does a batch processing. The user creates a
   408   /// reader object, then various reading rules can be added to the
   408   /// reader object, then various reading rules can be added to the
   409   /// reader, and eventually the reading is executed with the \c run()
   409   /// reader, and eventually the reading is executed with the \c run()
   410   /// member function. A map reading rule can be added to the reader
   410   /// member function. A map reading rule can be added to the reader
   411   /// with the \c nodeMap() or \c arcMap() members. An optional
   411   /// with the \c nodeMap() or \c arcMap() members. An optional
   412   /// converter parameter can also be added as a standard functor
   412   /// converter parameter can also be added as a standard functor
   413   /// converting from std::string to the value type of the map. If it
   413   /// converting from \c std::string to the value type of the map. If it
   414   /// is set, it will determine how the tokens in the file should be
   414   /// is set, it will determine how the tokens in the file should be
   415   /// is converted to the map's value type. If the functor is not set,
   415   /// converted to the value type of the map. If the functor is not set,
   416   /// then a default conversion will be used. One map can be read into
   416   /// then a default conversion will be used. One map can be read into
   417   /// multiple map objects at the same time. The \c attribute(), \c
   417   /// multiple map objects at the same time. The \c attribute(), \c
   418   /// node() and \c arc() functions are used to add attribute reading
   418   /// node() and \c arc() functions are used to add attribute reading
   419   /// rules.
   419   /// rules.
   420   ///
   420   ///
   421   ///\code
   421   ///\code
   422   ///     DigraphReader<Digraph>(std::cin, digraph).
   422   /// DigraphReader<Digraph>(std::cin, digraph).
   423   ///       nodeMap("coordinates", coord_map).
   423   ///   nodeMap("coordinates", coord_map).
   424   ///       arcMap("capacity", cap_map).
   424   ///   arcMap("capacity", cap_map).
   425   ///       node("source", src).
   425   ///   node("source", src).
   426   ///       node("target", trg).
   426   ///   node("target", trg).
   427   ///       attribute("caption", caption).
   427   ///   attribute("caption", caption).
   428   ///       run();
   428   ///   run();
   429   ///\endcode
   429   ///\endcode
   430   ///
   430   ///
   431   /// By default the reader uses the first section in the file of the
   431   /// By default the reader uses the first section in the file of the
   432   /// proper type. If a section has an optional name, then it can be
   432   /// proper type. If a section has an optional name, then it can be
   433   /// selected for reading by giving an optional name parameter to the
   433   /// selected for reading by giving an optional name parameter to the
   435   ///
   435   ///
   436   /// The \c useNodes() and \c useArcs() functions are used to tell the reader
   436   /// The \c useNodes() and \c useArcs() functions are used to tell the reader
   437   /// that the nodes or arcs should not be constructed (added to the
   437   /// that the nodes or arcs should not be constructed (added to the
   438   /// graph) during the reading, but instead the label map of the items
   438   /// graph) during the reading, but instead the label map of the items
   439   /// are given as a parameter of these functions. An
   439   /// are given as a parameter of these functions. An
   440   /// application of these function is multipass reading, which is
   440   /// application of these functions is multipass reading, which is
   441   /// important if two \e \@arcs sections must be read from the
   441   /// important if two \c \@arcs sections must be read from the
   442   /// file. In this example the first phase would read the node set and one
   442   /// file. In this case the first phase would read the node set and one
   443   /// of the arc sets, while the second phase would read the second arc
   443   /// of the arc sets, while the second phase would read the second arc
   444   /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
   444   /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
   445   /// The previously read label node map should be passed to the \c
   445   /// The previously read label node map should be passed to the \c
   446   /// useNodes() functions. Another application of multipass reading when
   446   /// useNodes() functions. Another application of multipass reading when
   447   /// paths are given as a node map or an arc map. It is impossible read this in
   447   /// paths are given as a node map or an arc map. It is impossible to read this in
   448   /// a single pass, because the arcs are not constructed when the node
   448   /// a single pass, because the arcs are not constructed when the node
   449   /// maps are read.
   449   /// maps are read.
   450   template <typename _Digraph>
   450   template <typename _Digraph>
   451   class DigraphReader {
   451   class DigraphReader {
   452   public:
   452   public:
   733 
   733 
   734     /// \brief Use previously constructed node set
   734     /// \brief Use previously constructed node set
   735     ///
   735     ///
   736     /// Use previously constructed node set, and specify the node
   736     /// Use previously constructed node set, and specify the node
   737     /// label map and a functor which converts the label map values to
   737     /// label map and a functor which converts the label map values to
   738     /// std::string.
   738     /// \c std::string.
   739     template <typename Map, typename Converter>
   739     template <typename Map, typename Converter>
   740     DigraphReader& useNodes(const Map& map, 
   740     DigraphReader& useNodes(const Map& map, 
   741 			    const Converter& converter = Converter()) {
   741 			    const Converter& converter = Converter()) {
   742       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   742       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
   743       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
   743       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
   766 
   766 
   767     /// \brief Use previously constructed arc set
   767     /// \brief Use previously constructed arc set
   768     ///
   768     ///
   769     /// Use previously constructed arc set, and specify the arc
   769     /// Use previously constructed arc set, and specify the arc
   770     /// label map and a functor which converts the label map values to
   770     /// label map and a functor which converts the label map values to
   771     /// std::string.
   771     /// \c std::string.
   772     template <typename Map, typename Converter>
   772     template <typename Map, typename Converter>
   773     DigraphReader& useArcs(const Map& map, 
   773     DigraphReader& useArcs(const Map& map, 
   774 			   const Converter& converter = Converter()) {
   774 			   const Converter& converter = Converter()) {
   775       checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
   775       checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
   776       LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); 
   776       LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); 
   782     }
   782     }
   783 
   783 
   784     /// \brief Skips the reading of node section
   784     /// \brief Skips the reading of node section
   785     ///
   785     ///
   786     /// Omit the reading of the node section. This implies that each node
   786     /// Omit the reading of the node section. This implies that each node
   787     /// map reading rule will be abanoned, and the nodes of the graph
   787     /// map reading rule will be abandoned, and the nodes of the graph
   788     /// will not be constructed, which usually cause that the arc set
   788     /// will not be constructed, which usually cause that the arc set
   789     /// could not be read due to lack of node name
   789     /// could not be read due to lack of node name resolving.
   790     /// resolving. Therefore, the \c skipArcs() should be used too, or
   790     /// Therefore \c skipArcs() function should also be used, or
   791     /// the useNodes() member function should be used to specify the
   791     /// \c useNodes() should be used to specify the label of the nodes.
   792     /// label of the nodes.
       
   793     DigraphReader& skipNodes() {
   792     DigraphReader& skipNodes() {
   794       LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); 
   793       LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); 
   795       _skip_nodes = true;
   794       _skip_nodes = true;
   796       return *this;
   795       return *this;
   797     }
   796     }
   798 
   797 
   799     /// \brief Skips the reading of arc section
   798     /// \brief Skips the reading of arc section
   800     ///
   799     ///
   801     /// Omit the reading of the arc section. This implies that each arc
   800     /// Omit the reading of the arc section. This implies that each arc
   802     /// map reading rule will be abanoned, and the arcs of the graph
   801     /// map reading rule will be abandoned, and the arcs of the graph
   803     /// will not be constructed.
   802     /// will not be constructed.
   804     DigraphReader& skipArcs() {
   803     DigraphReader& skipArcs() {
   805       LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); 
   804       LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); 
   806       _skip_arcs = true;
   805       _skip_arcs = true;
   807       return *this;
   806       return *this;
  1173 
  1172 
  1174     /// @}
  1173     /// @}
  1175     
  1174     
  1176   };
  1175   };
  1177 
  1176 
       
  1177   /// \brief Return a \ref DigraphReader class
       
  1178   /// 
       
  1179   /// This function just returns a \ref DigraphReader class.
  1178   /// \relates DigraphReader
  1180   /// \relates DigraphReader
  1179   template <typename Digraph>
  1181   template <typename Digraph>
  1180   DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
  1182   DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
  1181     DigraphReader<Digraph> tmp(is, digraph);
  1183     DigraphReader<Digraph> tmp(is, digraph);
  1182     return tmp;
  1184     return tmp;
  1183   }
  1185   }
  1184 
  1186 
       
  1187   /// \brief Return a \ref DigraphReader class
       
  1188   /// 
       
  1189   /// This function just returns a \ref DigraphReader class.
  1185   /// \relates DigraphReader
  1190   /// \relates DigraphReader
  1186   template <typename Digraph>
  1191   template <typename Digraph>
  1187   DigraphReader<Digraph> digraphReader(const std::string& fn, 
  1192   DigraphReader<Digraph> digraphReader(const std::string& fn, 
  1188 				       Digraph& digraph) {
  1193 				       Digraph& digraph) {
  1189     DigraphReader<Digraph> tmp(fn, digraph);
  1194     DigraphReader<Digraph> tmp(fn, digraph);
  1190     return tmp;
  1195     return tmp;
  1191   }
  1196   }
  1192 
  1197 
       
  1198   /// \brief Return a \ref DigraphReader class
       
  1199   /// 
       
  1200   /// This function just returns a \ref DigraphReader class.
  1193   /// \relates DigraphReader
  1201   /// \relates DigraphReader
  1194   template <typename Digraph>
  1202   template <typename Digraph>
  1195   DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
  1203   DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
  1196     DigraphReader<Digraph> tmp(fn, digraph);
  1204     DigraphReader<Digraph> tmp(fn, digraph);
  1197     return tmp;
  1205     return tmp;
  1209   template <typename Graph>
  1217   template <typename Graph>
  1210   GraphReader<Graph> graphReader(const char *fn, Graph& graph);    
  1218   GraphReader<Graph> graphReader(const char *fn, Graph& graph);    
  1211 
  1219 
  1212   /// \ingroup lemon_io
  1220   /// \ingroup lemon_io
  1213   ///  
  1221   ///  
  1214   /// \brief LGF reader for undirected graphs
  1222   /// \brief \ref lgf-format "LGF" reader for undirected graphs
  1215   ///
  1223   ///
  1216   /// This utility reads an \ref lgf-format "LGF" file.
  1224   /// This utility reads an \ref lgf-format "LGF" file.
       
  1225   ///
       
  1226   /// It can be used almost the same way as \c DigraphReader.
       
  1227   /// The only difference is that this class can handle edges and
       
  1228   /// edge maps as well as arcs and arc maps.
  1217   template <typename _Graph>
  1229   template <typename _Graph>
  1218   class GraphReader {
  1230   class GraphReader {
  1219   public:
  1231   public:
  1220 
  1232 
  1221     typedef _Graph Graph;
  1233     typedef _Graph Graph;
  1260 
  1272 
  1261   public:
  1273   public:
  1262 
  1274 
  1263     /// \brief Constructor
  1275     /// \brief Constructor
  1264     ///
  1276     ///
  1265     /// Construct a undirected graph reader, which reads from the given
  1277     /// Construct an undirected graph reader, which reads from the given
  1266     /// input stream.
  1278     /// input stream.
  1267     GraphReader(std::istream& is, Graph& graph) 
  1279     GraphReader(std::istream& is, Graph& graph) 
  1268       : _is(&is), local_is(false), _graph(graph),
  1280       : _is(&is), local_is(false), _graph(graph),
  1269 	_use_nodes(false), _use_edges(false),
  1281 	_use_nodes(false), _use_edges(false),
  1270 	_skip_nodes(false), _skip_edges(false) {}
  1282 	_skip_nodes(false), _skip_edges(false) {}
  1271 
  1283 
  1272     /// \brief Constructor
  1284     /// \brief Constructor
  1273     ///
  1285     ///
  1274     /// Construct a undirected graph reader, which reads from the given
  1286     /// Construct an undirected graph reader, which reads from the given
  1275     /// file.
  1287     /// file.
  1276     GraphReader(const std::string& fn, Graph& graph) 
  1288     GraphReader(const std::string& fn, Graph& graph) 
  1277       : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
  1289       : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
  1278     	_use_nodes(false), _use_edges(false),
  1290     	_use_nodes(false), _use_edges(false),
  1279 	_skip_nodes(false), _skip_edges(false) {}
  1291 	_skip_nodes(false), _skip_edges(false) {}
  1280     
  1292     
  1281     /// \brief Constructor
  1293     /// \brief Constructor
  1282     ///
  1294     ///
  1283     /// Construct a undirected graph reader, which reads from the given
  1295     /// Construct an undirected graph reader, which reads from the given
  1284     /// file.
  1296     /// file.
  1285     GraphReader(const char* fn, Graph& graph) 
  1297     GraphReader(const char* fn, Graph& graph) 
  1286       : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
  1298       : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
  1287     	_use_nodes(false), _use_edges(false),
  1299     	_use_nodes(false), _use_edges(false),
  1288 	_skip_nodes(false), _skip_edges(false) {}
  1300 	_skip_nodes(false), _skip_edges(false) {}
  1495     /// \name Select section by name
  1507     /// \name Select section by name
  1496     /// @{
  1508     /// @{
  1497 
  1509 
  1498     /// \brief Set \c \@nodes section to be read
  1510     /// \brief Set \c \@nodes section to be read
  1499     ///
  1511     ///
  1500     /// Set \c \@nodes section to be read
  1512     /// Set \c \@nodes section to be read.
  1501     GraphReader& nodes(const std::string& caption) {
  1513     GraphReader& nodes(const std::string& caption) {
  1502       _nodes_caption = caption;
  1514       _nodes_caption = caption;
  1503       return *this;
  1515       return *this;
  1504     }
  1516     }
  1505 
  1517 
  1506     /// \brief Set \c \@edges section to be read
  1518     /// \brief Set \c \@edges section to be read
  1507     ///
  1519     ///
  1508     /// Set \c \@edges section to be read
  1520     /// Set \c \@edges section to be read.
  1509     GraphReader& edges(const std::string& caption) {
  1521     GraphReader& edges(const std::string& caption) {
  1510       _edges_caption = caption;
  1522       _edges_caption = caption;
  1511       return *this;
  1523       return *this;
  1512     }
  1524     }
  1513 
  1525 
  1514     /// \brief Set \c \@attributes section to be read
  1526     /// \brief Set \c \@attributes section to be read
  1515     ///
  1527     ///
  1516     /// Set \c \@attributes section to be read
  1528     /// Set \c \@attributes section to be read.
  1517     GraphReader& attributes(const std::string& caption) {
  1529     GraphReader& attributes(const std::string& caption) {
  1518       _attributes_caption = caption;
  1530       _attributes_caption = caption;
  1519       return *this;
  1531       return *this;
  1520     }
  1532     }
  1521 
  1533 
  1542 
  1554 
  1543     /// \brief Use previously constructed node set
  1555     /// \brief Use previously constructed node set
  1544     ///
  1556     ///
  1545     /// Use previously constructed node set, and specify the node
  1557     /// Use previously constructed node set, and specify the node
  1546     /// label map and a functor which converts the label map values to
  1558     /// label map and a functor which converts the label map values to
  1547     /// std::string.
  1559     /// \c std::string.
  1548     template <typename Map, typename Converter>
  1560     template <typename Map, typename Converter>
  1549     GraphReader& useNodes(const Map& map, 
  1561     GraphReader& useNodes(const Map& map, 
  1550 			    const Converter& converter = Converter()) {
  1562 			    const Converter& converter = Converter()) {
  1551       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
  1563       checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
  1552       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
  1564       LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
  1575 
  1587 
  1576     /// \brief Use previously constructed edge set
  1588     /// \brief Use previously constructed edge set
  1577     ///
  1589     ///
  1578     /// Use previously constructed edge set, and specify the edge
  1590     /// Use previously constructed edge set, and specify the edge
  1579     /// label map and a functor which converts the label map values to
  1591     /// label map and a functor which converts the label map values to
  1580     /// std::string.
  1592     /// \c std::string.
  1581     template <typename Map, typename Converter>
  1593     template <typename Map, typename Converter>
  1582     GraphReader& useEdges(const Map& map, 
  1594     GraphReader& useEdges(const Map& map, 
  1583 			    const Converter& converter = Converter()) {
  1595 			    const Converter& converter = Converter()) {
  1584       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
  1596       checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
  1585       LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); 
  1597       LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); 
  1588 	_edge_index.insert(std::make_pair(converter(map[a]), a));
  1600 	_edge_index.insert(std::make_pair(converter(map[a]), a));
  1589       }
  1601       }
  1590       return *this;
  1602       return *this;
  1591     }
  1603     }
  1592 
  1604 
  1593     /// \brief Skips the reading of node section
  1605     /// \brief Skip the reading of node section
  1594     ///
  1606     ///
  1595     /// Omit the reading of the node section. This implies that each node
  1607     /// Omit the reading of the node section. This implies that each node
  1596     /// map reading rule will be abanoned, and the nodes of the graph
  1608     /// map reading rule will be abandoned, and the nodes of the graph
  1597     /// will not be constructed, which usually cause that the edge set
  1609     /// will not be constructed, which usually cause that the edge set
  1598     /// could not be read due to lack of node name
  1610     /// could not be read due to lack of node name
  1599     /// resolving. Therefore, the \c skipEdges() should be used too, or
  1611     /// could not be read due to lack of node name resolving.
  1600     /// the useNodes() member function should be used to specify the
  1612     /// Therefore \c skipEdges() function should also be used, or
  1601     /// label of the nodes.
  1613     /// \c useNodes() should be used to specify the label of the nodes.
  1602     GraphReader& skipNodes() {
  1614     GraphReader& skipNodes() {
  1603       LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); 
  1615       LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); 
  1604       _skip_nodes = true;
  1616       _skip_nodes = true;
  1605       return *this;
  1617       return *this;
  1606     }
  1618     }
  1607 
  1619 
  1608     /// \brief Skips the reading of edge section
  1620     /// \brief Skip the reading of edge section
  1609     ///
  1621     ///
  1610     /// Omit the reading of the edge section. This implies that each edge
  1622     /// Omit the reading of the edge section. This implies that each edge
  1611     /// map reading rule will be abanoned, and the edges of the graph
  1623     /// map reading rule will be abandoned, and the edges of the graph
  1612     /// will not be constructed.
  1624     /// will not be constructed.
  1613     GraphReader& skipEdges() {
  1625     GraphReader& skipEdges() {
  1614       LEMON_ASSERT(!_skip_edges, "Skip edges already set"); 
  1626       LEMON_ASSERT(!_skip_edges, "Skip edges already set"); 
  1615       _skip_edges = true;
  1627       _skip_edges = true;
  1616       return *this;
  1628       return *this;
  1980 
  1992 
  1981     /// @}
  1993     /// @}
  1982     
  1994     
  1983   };
  1995   };
  1984 
  1996 
       
  1997   /// \brief Return a \ref GraphReader class
       
  1998   /// 
       
  1999   /// This function just returns a \ref GraphReader class.
  1985   /// \relates GraphReader
  2000   /// \relates GraphReader
  1986   template <typename Graph>
  2001   template <typename Graph>
  1987   GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
  2002   GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
  1988     GraphReader<Graph> tmp(is, graph);
  2003     GraphReader<Graph> tmp(is, graph);
  1989     return tmp;
  2004     return tmp;
  1990   }
  2005   }
  1991 
  2006 
       
  2007   /// \brief Return a \ref GraphReader class
       
  2008   /// 
       
  2009   /// This function just returns a \ref GraphReader class.
  1992   /// \relates GraphReader
  2010   /// \relates GraphReader
  1993   template <typename Graph>
  2011   template <typename Graph>
  1994   GraphReader<Graph> graphReader(const std::string& fn, 
  2012   GraphReader<Graph> graphReader(const std::string& fn, 
  1995 				       Graph& graph) {
  2013 				       Graph& graph) {
  1996     GraphReader<Graph> tmp(fn, graph);
  2014     GraphReader<Graph> tmp(fn, graph);
  1997     return tmp;
  2015     return tmp;
  1998   }
  2016   }
  1999 
  2017 
       
  2018   /// \brief Return a \ref GraphReader class
       
  2019   /// 
       
  2020   /// This function just returns a \ref GraphReader class.
  2000   /// \relates GraphReader
  2021   /// \relates GraphReader
  2001   template <typename Graph>
  2022   template <typename Graph>
  2002   GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
  2023   GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
  2003     GraphReader<Graph> tmp(fn, graph);
  2024     GraphReader<Graph> tmp(fn, graph);
  2004     return tmp;
  2025     return tmp;
  2008 
  2029 
  2009   SectionReader sectionReader(std::istream& is);
  2030   SectionReader sectionReader(std::istream& is);
  2010   SectionReader sectionReader(const std::string& fn);
  2031   SectionReader sectionReader(const std::string& fn);
  2011   SectionReader sectionReader(const char* fn);
  2032   SectionReader sectionReader(const char* fn);
  2012   
  2033   
       
  2034   /// \ingroup lemon_io
       
  2035   ///
  2013   /// \brief Section reader class
  2036   /// \brief Section reader class
  2014   ///
  2037   ///
  2015   /// In the \e LGF file extra sections can be placed, which contain
  2038   /// In the \ref lgf-format "LGF" file extra sections can be placed, 
  2016   /// any data in arbitrary format. Such sections can be read with
  2039   /// which contain any data in arbitrary format. Such sections can be
  2017   /// this class. A reading rule can be added with two different
  2040   /// read with this class. A reading rule can be added to the class 
  2018   /// functions, with the \c sectionLines() function a functor can
  2041   /// with two different functions. With the \c sectionLines() function a
  2019   /// process the section line-by-line. While with the \c
  2042   /// functor can process the section line-by-line, while with the \c
  2020   /// sectionStream() member the section can be read from an input
  2043   /// sectionStream() member the section can be read from an input
  2021   /// stream.
  2044   /// stream.
  2022   class SectionReader {
  2045   class SectionReader {
  2023   private:
  2046   private:
  2024     
  2047     
  2103     ///  12 45 23
  2126     ///  12 45 23
  2104     ///  4
  2127     ///  4
  2105     ///  23 6
  2128     ///  23 6
  2106     ///\endcode
  2129     ///\endcode
  2107     ///
  2130     ///
  2108     /// The functor is implemented as an struct:
  2131     /// The functor is implemented as a struct:
  2109     ///\code
  2132     ///\code
  2110     ///  struct NumberSection {
  2133     ///  struct NumberSection {
  2111     ///    std::vector<int>& _data;
  2134     ///    std::vector<int>& _data;
  2112     ///    NumberSection(std::vector<int>& data) : _data(data) {}
  2135     ///    NumberSection(std::vector<int>& data) : _data(data) {}
  2113     ///    void operator()(const std::string& line) {
  2136     ///    void operator()(const std::string& line) {
  2121     ///
  2144     ///
  2122     ///  reader.sectionLines("numbers", NumberSection(vec));  
  2145     ///  reader.sectionLines("numbers", NumberSection(vec));  
  2123     ///\endcode
  2146     ///\endcode
  2124     template <typename Functor>
  2147     template <typename Functor>
  2125     SectionReader& sectionLines(const std::string& type, Functor functor) {
  2148     SectionReader& sectionLines(const std::string& type, Functor functor) {
  2126       LEMON_ASSERT(!type.empty(), "Type is not empty.");
  2149       LEMON_ASSERT(!type.empty(), "Type is empty.");
  2127       LEMON_ASSERT(_sections.find(type) == _sections.end(), 
  2150       LEMON_ASSERT(_sections.find(type) == _sections.end(), 
  2128 		   "Multiple reading of section.");
  2151 		   "Multiple reading of section.");
  2129       _sections.insert(std::make_pair(type, 
  2152       _sections.insert(std::make_pair(type, 
  2130         new _reader_bits::LineSection<Functor>(functor)));
  2153         new _reader_bits::LineSection<Functor>(functor)));
  2131       return *this;
  2154       return *this;
  2133 
  2156 
  2134 
  2157 
  2135     /// \brief Add a section processor with stream oriented reading
  2158     /// \brief Add a section processor with stream oriented reading
  2136     ///
  2159     ///
  2137     /// The first parameter is the type of the section, the second is
  2160     /// The first parameter is the type of the section, the second is
  2138     /// a functor, which takes an \c std::istream& and an int&
  2161     /// a functor, which takes an \c std::istream& and an \c int&
  2139     /// parameter, the latter regard to the line number of stream. The
  2162     /// parameter, the latter regard to the line number of stream. The
  2140     /// functor can read the input while the section go on, and the
  2163     /// functor can read the input while the section go on, and the
  2141     /// line number should be modified accordingly.
  2164     /// line number should be modified accordingly.
  2142     template <typename Functor>
  2165     template <typename Functor>
  2143     SectionReader& sectionStream(const std::string& type, Functor functor) {
  2166     SectionReader& sectionStream(const std::string& type, Functor functor) {
  2144       LEMON_ASSERT(!type.empty(), "Type is not empty.");
  2167       LEMON_ASSERT(!type.empty(), "Type is empty.");
  2145       LEMON_ASSERT(_sections.find(type) == _sections.end(), 
  2168       LEMON_ASSERT(_sections.find(type) == _sections.end(), 
  2146 		   "Multiple reading of section.");
  2169 		   "Multiple reading of section.");
  2147       _sections.insert(std::make_pair(type, 
  2170       _sections.insert(std::make_pair(type, 
  2148 	 new _reader_bits::StreamSection<Functor>(functor)));
  2171 	 new _reader_bits::StreamSection<Functor>(functor)));
  2149       return *this;
  2172       return *this;
  2184     /// \name Execution of the reader    
  2207     /// \name Execution of the reader    
  2185     /// @{
  2208     /// @{
  2186 
  2209 
  2187     /// \brief Start the batch processing
  2210     /// \brief Start the batch processing
  2188     ///
  2211     ///
  2189     /// This function starts the batch processing
  2212     /// This function starts the batch processing.
  2190     void run() {
  2213     void run() {
  2191       
  2214       
  2192       LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
  2215       LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
  2193       
  2216       
  2194       std::set<std::string> extra_sections;
  2217       std::set<std::string> extra_sections;
  2237 
  2260 
  2238     /// @}
  2261     /// @}
  2239         
  2262         
  2240   };
  2263   };
  2241 
  2264 
       
  2265   /// \brief Return a \ref SectionReader class
       
  2266   /// 
       
  2267   /// This function just returns a \ref SectionReader class.
  2242   /// \relates SectionReader
  2268   /// \relates SectionReader
  2243   inline SectionReader sectionReader(std::istream& is) {
  2269   inline SectionReader sectionReader(std::istream& is) {
  2244     SectionReader tmp(is);
  2270     SectionReader tmp(is);
  2245     return tmp;
  2271     return tmp;
  2246   }
  2272   }
  2247 
  2273 
       
  2274   /// \brief Return a \ref SectionReader class
       
  2275   /// 
       
  2276   /// This function just returns a \ref SectionReader class.
  2248   /// \relates SectionReader
  2277   /// \relates SectionReader
  2249   inline SectionReader sectionReader(const std::string& fn) {
  2278   inline SectionReader sectionReader(const std::string& fn) {
  2250     SectionReader tmp(fn);
  2279     SectionReader tmp(fn);
  2251     return tmp;
  2280     return tmp;
  2252   }
  2281   }
  2253 
  2282 
       
  2283   /// \brief Return a \ref SectionReader class
       
  2284   /// 
       
  2285   /// This function just returns a \ref SectionReader class.
  2254   /// \relates SectionReader
  2286   /// \relates SectionReader
  2255   inline SectionReader sectionReader(const char* fn) {
  2287   inline SectionReader sectionReader(const char* fn) {
  2256     SectionReader tmp(fn);
  2288     SectionReader tmp(fn);
  2257     return tmp;
  2289     return tmp;
  2258   }
  2290   }
  2267   /// should be read from a file, but in general tools (like glemon)
  2299   /// should be read from a file, but in general tools (like glemon)
  2268   /// the contents of an LGF file should be guessed somehow. This class
  2300   /// the contents of an LGF file should be guessed somehow. This class
  2269   /// reads the graph and stores the appropriate information for
  2301   /// reads the graph and stores the appropriate information for
  2270   /// reading the graph.
  2302   /// reading the graph.
  2271   ///
  2303   ///
  2272   ///\code LgfContents contents("graph.lgf"); 
  2304   ///\code 
       
  2305   /// LgfContents contents("graph.lgf"); 
  2273   /// contents.run();
  2306   /// contents.run();
  2274   ///
  2307   ///
  2275   /// // does it contain any node section and arc section
  2308   /// // Does it contain any node section and arc section?
  2276   /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
  2309   /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
  2277   ///   std::cerr << "Failure, cannot find graph" << std::endl;
  2310   ///   std::cerr << "Failure, cannot find graph." << std::endl;
  2278   ///   return -1;
  2311   ///   return -1;
  2279   /// }
  2312   /// }
  2280   /// std::cout << "The name of the default node section : " 
  2313   /// std::cout << "The name of the default node section: " 
  2281   ///           << contents.nodeSection(0) << std::endl;
  2314   ///           << contents.nodeSection(0) << std::endl;
  2282   /// std::cout << "The number of the arc maps : " 
  2315   /// std::cout << "The number of the arc maps: " 
  2283   ///           << contents.arcMaps(0).size() << std::endl;
  2316   ///           << contents.arcMaps(0).size() << std::endl;
  2284   /// std::cout << "The name of second arc map : " 
  2317   /// std::cout << "The name of second arc map: " 
  2285   ///           << contents.arcMaps(0)[1] << std::endl;
  2318   ///           << contents.arcMaps(0)[1] << std::endl;
  2286   ///\endcode
  2319   ///\endcode
  2287   class LgfContents {    
  2320   class LgfContents {    
  2288   private:
  2321   private:
  2289 
  2322 
  2350     /// Gives back the number of node sections in the file.
  2383     /// Gives back the number of node sections in the file.
  2351     int nodeSectionNum() const {
  2384     int nodeSectionNum() const {
  2352       return _node_sections.size();
  2385       return _node_sections.size();
  2353     }
  2386     }
  2354 
  2387 
  2355     /// \brief Returns the section name at the given position. 
  2388     /// \brief Returns the node section name at the given position. 
  2356     ///
  2389     ///
  2357     /// Returns the section name at the given position. 
  2390     /// Returns the node section name at the given position. 
  2358     const std::string& nodeSection(int i) const {
  2391     const std::string& nodeSection(int i) const {
  2359       return _node_sections[i];
  2392       return _node_sections[i];
  2360     }
  2393     }
  2361 
  2394 
  2362     /// \brief Gives back the node maps for the given section.
  2395     /// \brief Gives back the node maps for the given section.
  2377     /// \note It is synonym of \c edgeSectionNum().
  2410     /// \note It is synonym of \c edgeSectionNum().
  2378     int arcSectionNum() const {
  2411     int arcSectionNum() const {
  2379       return _edge_sections.size();
  2412       return _edge_sections.size();
  2380     }
  2413     }
  2381 
  2414 
  2382     /// \brief Returns the section name at the given position. 
  2415     /// \brief Returns the arc/edge section name at the given position. 
  2383     ///
  2416     ///
  2384     /// Returns the section name at the given position. 
  2417     /// Returns the arc/edge section name at the given position. 
  2385     /// \note It is synonym of \c edgeSection().
  2418     /// \note It is synonym of \c edgeSection().
  2386     const std::string& arcSection(int i) const {
  2419     const std::string& arcSection(int i) const {
  2387       return _edge_sections[i];
  2420       return _edge_sections[i];
  2388     }
  2421     }
  2389 
  2422 
  2434     /// Gives back the number of attribute sections in the file.
  2467     /// Gives back the number of attribute sections in the file.
  2435     int attributeSectionNum() const {
  2468     int attributeSectionNum() const {
  2436       return _attribute_sections.size();
  2469       return _attribute_sections.size();
  2437     }
  2470     }
  2438 
  2471 
  2439     /// \brief Returns the section name at the given position. 
  2472     /// \brief Returns the attribute section name at the given position. 
  2440     ///
  2473     ///
  2441     /// Returns the section name at the given position. 
  2474     /// Returns the attribute section name at the given position. 
  2442     const std::string& attributeSectionNames(int i) const {
  2475     const std::string& attributeSectionNames(int i) const {
  2443       return _attribute_sections[i];
  2476       return _attribute_sections[i];
  2444     }
  2477     }
  2445 
  2478 
  2446     /// \brief Gives back the attributes for the given section.
  2479     /// \brief Gives back the attributes for the given section.
  2527   public:
  2560   public:
  2528 
  2561 
  2529     /// \name Execution of the contents reader    
  2562     /// \name Execution of the contents reader    
  2530     /// @{
  2563     /// @{
  2531 
  2564 
  2532     /// \brief Start the reading
  2565     /// \brief Starts the reading
  2533     ///
  2566     ///
  2534     /// This function starts the reading
  2567     /// This function starts the reading.
  2535     void run() {
  2568     void run() {
  2536 
  2569 
  2537       readLine();
  2570       readLine();
  2538       skipSection();
  2571       skipSection();
  2539 
  2572