lemon/graph_reader.h
author hegyi
Tue, 08 Apr 2008 11:39:40 +0000
changeset 2602 1c7790d9e025
parent 2502 9c23c3762bc5
permissions -rw-r--r--
Rel.07 NEWS - 3. round
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\ingroup lemon_io
    20 ///\file
    21 ///\brief Lemon Graph Format reader.
    22 
    23 #ifndef LEMON_GRAPH_READER_H
    24 #define LEMON_GRAPH_READER_H
    25 
    26 #include <iostream>
    27 
    28 #include <lemon/error.h>
    29 #include <lemon/lemon_reader.h>
    30 
    31 namespace lemon {
    32 
    33   /// \addtogroup lemon_io
    34   /// @{
    35 
    36   /// \brief The graph reader class.
    37   ///
    38   /// The \c GraphReader class provides the graph input. 
    39   /// Before you read this documentation it might be useful to read the general
    40   /// description of  \ref graph-io-page "Graph Input-Output".
    41   ///
    42   /// The file to be read may contain several maps and labeled
    43   /// (designated) nodes or edges.
    44   ///
    45   /// If you read a graph you need not read all the maps and items just those
    46   /// that you need. The interface of the \c GraphReader is very similar to
    47   /// the GraphWriter but the reading method does not depend on the order the
    48   /// given commands (i.e. you don't have to insist on the order in which the
    49   /// maps are given in the file).
    50   ///
    51   /// The reader object assumes that not read values do not contain 
    52   /// whitespaces, therefore it has some extra possibilities to control how
    53   /// it should skip the values when the string representation contains spaces.
    54   ///
    55   ///\code
    56   /// GraphReader<ListGraph> reader(std::cin, graph);
    57   ///\endcode
    58   ///
    59   /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
    60   /// If there is a map that you do not want to read from the file and there is
    61   /// whitespace in the string represenation of the values then you should
    62   /// call the \c skipNodeMap() template member function with proper 
    63   /// parameters.
    64   ///
    65   ///\code
    66   /// reader.readNodeMap("coords", coords);
    67   ///
    68   /// reader.skipNodeMap("description", desc);
    69   ///
    70   /// reader.readNodeMap("color", colorMap);
    71   ///\endcode
    72   ///
    73   /// With the \c readEdgeMap() member function you can give an edge map
    74   /// reading command similar to the NodeMaps. 
    75   ///
    76   ///\code
    77   /// reader.readEdgeMap("weight", weightMap);
    78   /// reader.readEdgeMap("label", labelMap);
    79   ///\endcode
    80   ///
    81   /// With \c readNode() and \c readEdge() functions you can read 
    82   /// labeled Nodes and Edges.
    83   ///
    84   ///\code
    85   /// reader.readNode("source", sourceNode);
    86   /// reader.readNode("target", targetNode);
    87   ///
    88   /// reader.readEdge("observed", edge);
    89   ///\endcode
    90   ///
    91   /// With the \c readAttribute() functions you can read an attribute
    92   /// into a variable. You can specify the reader for the attribute as
    93   /// the nodemaps.
    94   ///
    95   /// After you give all read commands you must call the \c run() member
    96   /// function, which executes all the commands.
    97   ///
    98   ///\code
    99   /// reader.run();
   100   ///\endcode
   101   ///
   102   /// \see DefaultReaderTraits
   103   /// \see QuotedStringReader
   104   /// \see \ref GraphWriter
   105   /// \see \ref graph-io-page
   106   /// \author Balazs Dezso
   107   template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
   108   class GraphReader {
   109   public:
   110     
   111     typedef _Graph Graph;
   112     typedef typename Graph::Node Node;
   113     typedef typename Graph::Edge Edge;
   114 
   115     typedef _ReaderTraits ReaderTraits;
   116     typedef typename ReaderTraits::Skipper DefaultSkipper;
   117 
   118     /// \brief Construct a new GraphReader.
   119     ///
   120     /// Construct a new GraphReader. It reads into the given graph
   121     /// and it uses the given reader as the default skipper.
   122     GraphReader(std::istream& _is, Graph& _graph, 
   123 		const DefaultSkipper& _skipper = DefaultSkipper()) 
   124       : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
   125 	nodeset_reader(*reader, _graph, std::string(), skipper),
   126 	edgeset_reader(*reader, _graph, nodeset_reader, 
   127 		       std::string(), skipper),
   128 	node_reader(*reader, nodeset_reader, std::string()),
   129 	edge_reader(*reader, edgeset_reader, std::string()),
   130 	attribute_reader(*reader, std::string()) {}
   131 
   132     /// \brief Construct a new GraphReader.
   133     ///
   134     /// Construct a new GraphReader. It reads into the given graph
   135     /// and it uses the given reader as the default skipper.
   136     GraphReader(const std::string& _filename, Graph& _graph, 
   137 		const DefaultSkipper& _skipper = DefaultSkipper()) 
   138       : reader(new LemonReader(_filename)), own_reader(true), 
   139 	skipper(_skipper),
   140 	nodeset_reader(*reader, _graph, std::string(), skipper),
   141 	edgeset_reader(*reader, _graph, nodeset_reader, 
   142 		       std::string(), skipper),
   143 	node_reader(*reader, nodeset_reader, std::string()),
   144 	edge_reader(*reader, edgeset_reader, std::string()),
   145 	attribute_reader(*reader, std::string()) {}
   146 
   147     /// \brief Construct a new GraphReader.
   148     ///
   149     /// Construct a new GraphReader. It reads into the given graph
   150     /// and it uses the given reader as the default skipper.
   151     GraphReader(LemonReader& _reader, Graph& _graph, 
   152 		const DefaultSkipper& _skipper = DefaultSkipper()) 
   153       : reader(_reader), own_reader(false), skipper(_skipper),
   154 	nodeset_reader(*reader, _graph, std::string(), skipper),
   155 	edgeset_reader(*reader, _graph, nodeset_reader, 
   156 		       std::string(), skipper),
   157 	node_reader(*reader, nodeset_reader, std::string()),
   158 	edge_reader(*reader, edgeset_reader, std::string()),
   159 	attribute_reader(*reader, std::string()) {}
   160 
   161     /// \brief Destruct the graph reader.
   162     ///
   163     /// Destruct the graph reader.
   164     ~GraphReader() {
   165       if (own_reader) 
   166 	delete reader;
   167     }
   168 
   169     /// \brief Give a new node map reading command to the reader.
   170     ///
   171     /// Give a new node map reading command to the reader.
   172     template <typename Map>
   173     GraphReader& readNodeMap(std::string name, Map& map) {
   174       nodeset_reader.readNodeMap(name, map);
   175       return *this;
   176     }
   177 
   178     template <typename Map>
   179     GraphReader& readNodeMap(std::string name, const Map& map) {
   180       nodeset_reader.readNodeMap(name, map);
   181       return *this;
   182     }
   183 
   184     /// \brief Give a new node map reading command to the reader.
   185     ///
   186     /// Give a new node map reading command to the reader.
   187     template <typename ItemReader, typename Map>
   188     GraphReader& readNodeMap(std::string name, Map& map, 
   189 			     const ItemReader& ir = ItemReader()) {
   190       nodeset_reader.readNodeMap(name, map, ir);
   191       return *this;
   192     }
   193 
   194     template <typename ItemReader, typename Map>
   195     GraphReader& readNodeMap(std::string name, const Map& map, 
   196 			     const ItemReader& ir = ItemReader()) {
   197       nodeset_reader.readNodeMap(name, map, ir);
   198       return *this;
   199     }
   200 
   201     /// \brief Give a new node map skipping command to the reader.
   202     ///
   203     /// Give a new node map skipping command to the reader.
   204     template <typename ItemReader>
   205     GraphReader& skipNodeMap(std::string name, 
   206 			     const ItemReader& ir = ItemReader()) {
   207       nodeset_reader.skipNodeMap(name, ir);
   208       return *this;
   209     }
   210 
   211     /// \brief Give a new edge map reading command to the reader.
   212     ///
   213     /// Give a new edge map reading command to the reader.
   214     template <typename Map>
   215     GraphReader& readEdgeMap(std::string name, Map& map) { 
   216       edgeset_reader.readEdgeMap(name, map);
   217       return *this;
   218     }
   219 
   220     template <typename Map>
   221     GraphReader& readEdgeMap(std::string name, const Map& map) { 
   222       edgeset_reader.readEdgeMap(name, map);
   223       return *this;
   224     }
   225 
   226 
   227     /// \brief Give a new edge map reading command to the reader.
   228     ///
   229     /// Give a new edge map reading command to the reader.
   230     template <typename ItemReader, typename Map>
   231     GraphReader& readEdgeMap(std::string name, Map& map,
   232 			     const ItemReader& ir = ItemReader()) {
   233       edgeset_reader.readEdgeMap(name, map, ir);
   234       return *this;
   235     }
   236 
   237     template <typename ItemReader, typename Map>
   238     GraphReader& readEdgeMap(std::string name, const Map& map,
   239 			     const ItemReader& ir = ItemReader()) {
   240       edgeset_reader.readEdgeMap(name, map, ir);
   241       return *this;
   242     }
   243 
   244     /// \brief Give a new edge map skipping command to the reader.
   245     ///
   246     /// Give a new edge map skipping command to the reader.
   247     template <typename ItemReader>
   248     GraphReader& skipEdgeMap(std::string name, 
   249 			     const ItemReader& ir = ItemReader()) {
   250       edgeset_reader.skipEdgeMap(name, ir);
   251       return *this;
   252     }
   253 
   254     /// \brief Give a new labeled node reading command to the reader.
   255     ///
   256     /// Give a new labeled node reading command to the reader.
   257     GraphReader& readNode(std::string name, Node& node) {
   258       node_reader.readNode(name, node);
   259       return *this;
   260     }
   261 
   262     /// \brief Give a new labeled edge reading command to the reader.
   263     ///
   264     /// Give a new labeled edge reading command to the reader.
   265     GraphReader& readEdge(std::string name, Edge& edge) {
   266       edge_reader.readEdge(name, edge);
   267       return *this;
   268     }
   269 
   270     /// \brief Give a new attribute reading command.
   271     ///
   272     ///  Give a new attribute reading command.
   273     template <typename Value>
   274     GraphReader& readAttribute(std::string name, Value& value) {
   275       attribute_reader.readAttribute(name, value);
   276       return *this;
   277     }
   278     
   279     /// \brief Give a new attribute reading command.
   280     ///
   281     ///  Give a new attribute reading command.
   282     template <typename ItemReader, typename Value>
   283     GraphReader& readAttribute(std::string name, Value& value, 
   284 			       const ItemReader& ir = ItemReader()) {
   285       attribute_reader.readAttribute(name, value, ir);
   286       return *this;
   287     }
   288 
   289     /// \brief Conversion operator to LemonReader.
   290     ///
   291     /// Conversion operator to LemonReader. It makes possible to access the
   292     /// encapsulated \e LemonReader, this way you can attach to this reader
   293     /// new instances of \e LemonReader::SectionReader. For more details see
   294     /// the \ref rwbackground "Background of Reading and Writing".
   295     operator LemonReader&() {
   296       return *reader;
   297     }
   298 
   299     /// \brief Executes the reading commands.
   300     ///
   301     /// Executes the reading commands.
   302     void run() {
   303       reader->run();
   304     }
   305 
   306 
   307     /// \brief Returns true if the reader can give back the items by its label.
   308     ///
   309     /// \brief Returns true if the reader can give back the items by its label.
   310     bool isLabelReader() const {
   311       return nodeset_reader.isLabelReader() && edgeset_reader.isLabelReader();
   312     }
   313 
   314     /// \brief Gives back the node by its label.
   315     ///
   316     /// It reads an label from the stream and gives back which node belongs to
   317     /// it. It is possible only if there was read a "label" named node map.
   318     void readLabel(std::istream& is, Node& node) const {
   319       nodeset_reader.readLabel(is, node);
   320     } 
   321 
   322     /// \brief Gives back the edge by its label.
   323     ///
   324     /// It reads an label from the stream and gives back which edge belongs to
   325     /// it. It is possible only if there was read a "label" named edge map.
   326     void readLabel(std::istream& is, Edge& edge) const {
   327       edgeset_reader.readLabel(is, edge);
   328     } 
   329 
   330   private:
   331 
   332     LemonReader* reader;
   333     bool own_reader;
   334 
   335     DefaultSkipper skipper;
   336 
   337     NodeSetReader<Graph, ReaderTraits> nodeset_reader;
   338     EdgeSetReader<Graph, ReaderTraits> edgeset_reader;
   339 
   340     NodeReader<Graph> node_reader;
   341     EdgeReader<Graph> edge_reader;
   342     
   343     AttributeReader<ReaderTraits> attribute_reader;
   344   };
   345 
   346 
   347   /// \brief The undirected graph reader class.
   348   ///
   349   /// The \c UGraphReader class provides the graph input. 
   350   /// Before you read this documentation it might be useful to read the general
   351   /// description of  \ref graph-io-page "Graph Input-Output".
   352   ///
   353   /// The given file format may contain several maps and labeled nodes or 
   354   /// edges.
   355   ///
   356   /// If you read a graph you need not read all the maps and items just those
   357   /// that you need. The interface of the \c UGraphReader is very similar
   358   /// to the UGraphWriter but the reading method does not depend on the
   359   /// order of the given commands.
   360   ///
   361   /// The reader object suppose that each not read value does not contain 
   362   /// whitespaces, therefore it has some extra possibilities to control how
   363   /// it should skip the values when the string representation contains spaces.
   364   ///
   365   ///\code
   366   /// UGraphReader<ListUGraph> reader(std::cin, graph);
   367   ///\endcode
   368   ///
   369   /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
   370   /// If there is a map that you do not want to read from the file and there is
   371   /// whitespace in the string represenation of the values then you should
   372   /// call the \c skipNodeMap() template member function with proper 
   373   /// parameters.
   374   ///
   375   ///\code
   376   /// reader.readNodeMap("coords", coords);
   377   ///
   378   /// reader.skipNodeMap("description", desc);
   379   ///
   380   /// reader.readNodeMap("color", colorMap);
   381   ///\endcode
   382   ///
   383   /// With the \c readUEdgeMap() member function you can give an 
   384   /// uedge map reading command similar to the NodeMaps. 
   385   ///
   386   ///\code
   387   /// reader.readUEdgeMap("capacity", capacityMap);
   388   ///\endcode
   389   ///
   390   /// The reading of the directed edge maps is just a syntactical sugar.
   391   /// It reads two undirected edgemaps into a directed edge map. The 
   392   /// undirected edge maps' name should be start with the \c '+' and the
   393   /// \c '-' character and the same.
   394   ///
   395   ///\code
   396   /// reader.readEdgeMap("flow", flowMap);
   397   ///\endcode 
   398   ///
   399   /// With \c readNode() and \c readUEdge() functions you can read 
   400   /// labeled Nodes and UEdges.
   401   ///
   402   ///\code
   403   /// reader.readNode("source", sourceNode);
   404   /// reader.readNode("target", targetNode);
   405   ///
   406   /// reader.readUEdge("observed", uEdge);
   407   ///\endcode
   408   ///
   409   /// With the \c readAttribute() functions you can read an attribute
   410   /// in a variable. You can specify the reader for the attribute as
   411   /// the nodemaps.
   412   ///
   413   /// After you give all read commands you must call the \c run() member
   414   /// function, which execute all the commands.
   415   ///
   416   ///\code
   417   /// reader.run();
   418   ///\endcode
   419   ///
   420   /// \see GraphReader
   421   /// \see DefaultReaderTraits
   422   /// \see \ref UGraphWriter
   423   /// \see \ref graph-io-page
   424   ///
   425   /// \author Balazs Dezso
   426   template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
   427   class UGraphReader {
   428   public:
   429     
   430     typedef _Graph Graph;
   431     typedef typename Graph::Node Node;
   432     typedef typename Graph::Edge Edge;
   433     typedef typename Graph::UEdge UEdge;
   434 
   435     typedef _ReaderTraits ReaderTraits;
   436     typedef typename ReaderTraits::Skipper DefaultSkipper;
   437 
   438     /// \brief Construct a new UGraphReader.
   439     ///
   440     /// Construct a new UGraphReader. It reads into the given graph
   441     /// and it use the given reader as the default skipper.
   442     UGraphReader(std::istream& _is, Graph& _graph, 
   443 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   444       : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
   445 	nodeset_reader(*reader, _graph, std::string(), skipper),
   446 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   447 			     std::string(), skipper),
   448 	node_reader(*reader, nodeset_reader, std::string()),
   449 	uedge_reader(*reader, uedgeset_reader, std::string()),
   450 	attribute_reader(*reader, std::string()) {}
   451 
   452     /// \brief Construct a new UGraphReader.
   453     ///
   454     /// Construct a new UGraphReader. It reads into the given graph
   455     /// and it use the given reader as the default skipper.
   456     UGraphReader(const std::string& _filename, Graph& _graph, 
   457 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   458       : reader(new LemonReader(_filename)), own_reader(true), 
   459 	skipper(_skipper),
   460 	nodeset_reader(*reader, _graph, std::string(), skipper),
   461 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   462 			     std::string(), skipper),
   463 	node_reader(*reader, nodeset_reader, std::string()),
   464 	uedge_reader(*reader, uedgeset_reader, std::string()),
   465 	attribute_reader(*reader, std::string()) {}
   466 
   467     /// \brief Construct a new UGraphReader.
   468     ///
   469     /// Construct a new UGraphReader. It reads into the given graph
   470     /// and it use the given reader as the default skipper.
   471     UGraphReader(LemonReader& _reader, Graph& _graph, 
   472 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   473       : reader(_reader), own_reader(false), skipper(_skipper),
   474 	nodeset_reader(*reader, _graph, std::string(), skipper),
   475 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   476 			     std::string(), skipper),
   477 	node_reader(*reader, nodeset_reader, std::string()),
   478 	uedge_reader(*reader, uedgeset_reader, std::string()),
   479 	attribute_reader(*reader, std::string()) {}
   480 
   481     /// \brief Destruct the graph reader.
   482     ///
   483     /// Destruct the graph reader.
   484     ~UGraphReader() {
   485       if (own_reader) 
   486 	delete reader;
   487     }
   488 
   489     /// \brief Give a new node map reading command to the reader.
   490     ///
   491     /// Give a new node map reading command to the reader.
   492     template <typename Map>
   493     UGraphReader& readNodeMap(std::string name, Map& map) {
   494       nodeset_reader.readNodeMap(name, map);
   495       return *this;
   496     }
   497 
   498     template <typename Map>
   499     UGraphReader& readNodeMap(std::string name, const Map& map) {
   500       nodeset_reader.readNodeMap(name, map);
   501       return *this;
   502     }
   503 
   504     /// \brief Give a new node map reading command to the reader.
   505     ///
   506     /// Give a new node map reading command to the reader.
   507     template <typename ItemReader, typename Map>
   508     UGraphReader& readNodeMap(std::string name, Map& map, 
   509                               const ItemReader& ir = ItemReader()) {
   510       nodeset_reader.readNodeMap(name, map, ir);
   511       return *this;
   512     }
   513 
   514     template <typename ItemReader, typename Map>
   515     UGraphReader& readNodeMap(std::string name, const Map& map, 
   516                               const ItemReader& ir = ItemReader()) {
   517       nodeset_reader.readNodeMap(name, map, ir);
   518       return *this;
   519     }
   520 
   521     /// \brief Give a new node map skipping command to the reader.
   522     ///
   523     /// Give a new node map skipping command to the reader.
   524     template <typename ItemReader>
   525     UGraphReader& skipNodeMap(std::string name, 
   526                               const ItemReader& ir = ItemReader()) {
   527       nodeset_reader.skipNodeMap(name, ir);
   528       return *this;
   529     }
   530 
   531     /// \brief Give a new undirected edge map reading command to the reader.
   532     ///
   533     /// Give a new undirected edge map reading command to the reader.
   534     template <typename Map>
   535     UGraphReader& readUEdgeMap(std::string name, Map& map) { 
   536       uedgeset_reader.readUEdgeMap(name, map);
   537       return *this;
   538     }
   539 
   540     template <typename Map>
   541     UGraphReader& readUEdgeMap(std::string name, const Map& map) { 
   542       uedgeset_reader.readUEdgeMap(name, map);
   543       return *this;
   544     }
   545 
   546 
   547     /// \brief Give a new undirected edge map reading command to the reader.
   548     ///
   549     /// Give a new undirected edge map reading command to the reader.
   550     template <typename ItemReader, typename Map>
   551     UGraphReader& readUEdgeMap(std::string name, Map& map,
   552                                const ItemReader& ir = ItemReader()) {
   553       uedgeset_reader.readUEdgeMap(name, map, ir);
   554       return *this;
   555     }
   556 
   557     template <typename ItemReader, typename Map>
   558     UGraphReader& readUEdgeMap(std::string name, const Map& map,
   559                                const ItemReader& ir = ItemReader()) {
   560       uedgeset_reader.readUEdgeMap(name, map, ir);
   561       return *this;
   562     }
   563 
   564     /// \brief Give a new undirected edge map skipping command to the reader.
   565     ///
   566     /// Give a new undirected edge map skipping command to the reader.
   567     template <typename ItemReader>
   568     UGraphReader& skipUEdgeMap(std::string name,
   569 				       const ItemReader& ir = ItemReader()) {
   570       uedgeset_reader.skipUMap(name, ir);
   571       return *this;
   572     }
   573 
   574 
   575     /// \brief Give a new edge map reading command to the reader.
   576     ///
   577     /// Give a new edge map reading command to the reader.
   578     template <typename Map>
   579     UGraphReader& readEdgeMap(std::string name, Map& map) { 
   580       uedgeset_reader.readEdgeMap(name, map);
   581       return *this;
   582     }
   583 
   584     template <typename Map>
   585     UGraphReader& readEdgeMap(std::string name, const Map& map) { 
   586       uedgeset_reader.readEdgeMap(name, map);
   587       return *this;
   588     }
   589 
   590 
   591     /// \brief Give a new edge map reading command to the reader.
   592     ///
   593     /// Give a new edge map reading command to the reader.
   594     template <typename ItemReader, typename Map>
   595     UGraphReader& readEdgeMap(std::string name, Map& map,
   596                               const ItemReader& ir = ItemReader()) {
   597       uedgeset_reader.readEdgeMap(name, map, ir);
   598       return *this;
   599     }
   600 
   601     template <typename ItemReader, typename Map>
   602     UGraphReader& readEdgeMap(std::string name, const Map& map,
   603                               const ItemReader& ir = ItemReader()) {
   604       uedgeset_reader.readEdgeMap(name, map, ir);
   605       return *this;
   606     }
   607 
   608     /// \brief Give a new edge map skipping command to the reader.
   609     ///
   610     /// Give a new edge map skipping command to the reader.
   611     template <typename ItemReader>
   612     UGraphReader& skipEdgeMap(std::string name,
   613                               const ItemReader& ir = ItemReader()) {
   614       uedgeset_reader.skipEdgeMap(name, ir);
   615       return *this;
   616     }
   617 
   618     /// \brief Give a new labeled node reading command to the reader.
   619     ///
   620     /// Give a new labeled node reading command to the reader.
   621     UGraphReader& readNode(std::string name, Node& node) {
   622       node_reader.readNode(name, node);
   623       return *this;
   624     }
   625 
   626     /// \brief Give a new labeled edge reading command to the reader.
   627     ///
   628     /// Give a new labeled edge reading command to the reader.
   629     UGraphReader& readEdge(std::string name, Edge& edge) {
   630       uedge_reader.readEdge(name, edge);
   631     }
   632 
   633     /// \brief Give a new labeled undirected edge reading command to the
   634     /// reader.
   635     ///
   636     /// Give a new labeled undirected edge reading command to the reader.
   637     UGraphReader& readUEdge(std::string name, UEdge& edge) {
   638       uedge_reader.readUEdge(name, edge);
   639     }
   640 
   641     /// \brief Give a new attribute reading command.
   642     ///
   643     ///  Give a new attribute reading command.
   644     template <typename Value>
   645     UGraphReader& readAttribute(std::string name, Value& value) {
   646       attribute_reader.readAttribute(name, value);
   647       return *this;
   648     }
   649     
   650     /// \brief Give a new attribute reading command.
   651     ///
   652     ///  Give a new attribute reading command.
   653     template <typename ItemReader, typename Value>
   654     UGraphReader& readAttribute(std::string name, Value& value, 
   655 			       const ItemReader& ir = ItemReader()) {
   656       attribute_reader.readAttribute(name, value, ir);
   657       return *this;
   658     }
   659 
   660     /// \brief Conversion operator to LemonReader.
   661     ///
   662     /// Conversion operator to LemonReader. It make possible
   663     /// to access the encapsulated \e LemonReader, this way
   664     /// you can attach to this reader new instances of 
   665     /// \e LemonReader::SectionReader.
   666     operator LemonReader&() {
   667       return *reader;
   668     }
   669 
   670     /// \brief Executes the reading commands.
   671     ///
   672     /// Executes the reading commands.
   673     void run() {
   674       reader->run();
   675     }
   676 
   677 
   678     /// \brief Returns true if the reader can give back the items by its label.
   679     ///
   680     /// Returns true if the reader can give back the items by its label.
   681     bool isLabelReader() const {
   682       return nodeset_reader.isLabelReader() && 
   683         uedgeset_reader.isLabelReader();
   684     }
   685 
   686     /// \brief Gives back the node by its label.
   687     ///
   688     /// It reads an label from the stream and gives back which node belongs to
   689     /// it. It is possible only if there was read a "label" named node map.
   690     void readLabel(std::istream& is, Node& node) const {
   691       return nodeset_reader.readLabel(is, node);
   692     } 
   693 
   694     /// \brief Gives back the edge by its label
   695     ///
   696     /// It reads an label from the stream and gives back which edge belongs to
   697     /// it. It is possible only if there was read a "label" named edge map.
   698     void readLabel(std::istream& is, Edge& edge) const {
   699       return uedgeset_reader.readLabel(is, edge);
   700     } 
   701 
   702     /// \brief Gives back the undirected edge by its label.
   703     ///
   704     /// It reads an label from the stream and gives back which undirected edge 
   705     /// belongs to it. It is possible only if there was read a "label" named 
   706     /// edge map.
   707     void readLabel(std::istream& is, UEdge& uedge) const {
   708       return uedgeset_reader.readLabel(is, uedge);
   709     } 
   710     
   711 
   712   private:
   713 
   714     LemonReader* reader;
   715     bool own_reader;
   716 
   717     DefaultSkipper skipper;
   718 
   719     NodeSetReader<Graph, ReaderTraits> nodeset_reader;
   720     UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
   721 
   722     NodeReader<Graph> node_reader;
   723     UEdgeReader<Graph> uedge_reader;
   724     
   725     AttributeReader<ReaderTraits> attribute_reader;
   726   };
   727 
   728   /// \brief The bipartite graph reader class.
   729   ///
   730   /// The \c BpUGraphReader class provides the graph input. 
   731   /// Before you read this documentation it might be useful to read the general
   732   /// description of  \ref graph-io-page "Graph Input-Output".
   733   ///
   734   /// The given file format may contain several maps and labeled nodes or 
   735   /// edges.
   736   ///
   737   /// If you read a graph you need not read all the maps and items just those
   738   /// that you need. The interface of the \c BpUGraphReader is very similar
   739   /// to the BpUGraphWriter but the reading method does not depend on the
   740   /// order of the given commands.
   741   ///
   742   /// The reader object suppose that each not read value does not contain 
   743   /// whitespaces, therefore it has some extra possibilities to control how
   744   /// it should skip the values when the string representation contains spaces.
   745   ///
   746   ///\code
   747   /// BpUGraphReader<ListBpUGraph> reader(std::cin, graph);
   748   ///\endcode
   749   ///
   750   /// The \c readANodeMap() function reads a map from the A-part of
   751   /// the\c \@bpnodeset section, while the \c readBNodeMap() reads
   752   /// from the B-part of the section.  If you use the \c readNodeMap()
   753   /// function, then the given map should appear in both part of the
   754   /// section. If there is a map that you do not want to read from the
   755   /// file and there is whitespace in the string represenation of the
   756   /// values then you should call the \c skipANodeMap(), \c
   757   /// skipBNodeMap() or \c skipNodeMap() template member function with
   758   /// proper parameters.
   759   ///
   760   ///\code
   761   /// reader.readNodeMap("coords", coords);
   762   /// reader.readANodeMap("range", range);
   763   /// reader.readANodeMap("benefit", benefit);
   764   ///
   765   /// reader.skipNodeMap("description", desc);
   766   ///
   767   /// reader.readNodeMap("color", colorMap);
   768   ///\endcode
   769   ///
   770   /// With the \c readUEdgeMap() member function you can give an 
   771   /// uedge map reading command similar to the NodeMaps. 
   772   ///
   773   ///\code
   774   /// reader.readUEdgeMap("capacity", capacityMap);
   775   /// reader.readEdgeMap("flow", flowMap);
   776   ///\endcode 
   777   ///
   778   /// With \c readNode() and \c readUEdge() functions you can read 
   779   /// labeled Nodes and UEdges.
   780   ///
   781   ///\code
   782   /// reader.readNode("source", sourceNode);
   783   /// reader.readNode("target", targetNode);
   784   ///
   785   /// reader.readUEdge("observed", uEdge);
   786   ///\endcode
   787   ///
   788   /// With the \c readAttribute() functions you can read an attribute
   789   /// in a variable. You can specify the reader for the attribute as
   790   /// the nodemaps.
   791   ///
   792   /// After you give all read commands you must call the \c run() member
   793   /// function, which execute all the commands.
   794   ///
   795   ///\code
   796   /// reader.run();
   797   ///\endcode
   798   ///
   799   /// \see GraphReader
   800   /// \see DefaultReaderTraits
   801   /// \see \ref UGraphWriter
   802   /// \see \ref graph-io-page
   803   ///
   804   /// \author Balazs Dezso
   805   template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
   806   class BpUGraphReader {
   807   public:
   808     
   809     typedef _Graph Graph;
   810     typedef typename Graph::Node Node;
   811     typedef typename Graph::Edge Edge;
   812     typedef typename Graph::UEdge UEdge;
   813 
   814     typedef _ReaderTraits ReaderTraits;
   815     typedef typename ReaderTraits::Skipper DefaultSkipper;
   816 
   817     /// \brief Construct a new BpUGraphReader.
   818     ///
   819     /// Construct a new BpUGraphReader. It reads into the given graph
   820     /// and it use the given reader as the default skipper.
   821     BpUGraphReader(std::istream& _is, Graph& _graph, 
   822 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   823       : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
   824 	nodeset_reader(*reader, _graph, std::string(), skipper),
   825 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   826 			     std::string(), skipper),
   827 	node_reader(*reader, nodeset_reader, std::string()),
   828 	uedge_reader(*reader, uedgeset_reader, std::string()),
   829 	attribute_reader(*reader, std::string()) {}
   830 
   831     /// \brief Construct a new BpUGraphReader.
   832     ///
   833     /// Construct a new BpUGraphReader. It reads into the given graph
   834     /// and it use the given reader as the default skipper.
   835     BpUGraphReader(const std::string& _filename, Graph& _graph, 
   836 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   837       : reader(new LemonReader(_filename)), own_reader(true), 
   838 	skipper(_skipper),
   839 	nodeset_reader(*reader, _graph, std::string(), skipper),
   840 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   841 			     std::string(), skipper),
   842 	node_reader(*reader, nodeset_reader, std::string()),
   843 	uedge_reader(*reader, uedgeset_reader, std::string()),
   844 	attribute_reader(*reader, std::string()) {}
   845 
   846     /// \brief Construct a new BpUGraphReader.
   847     ///
   848     /// Construct a new BpUGraphReader. It reads into the given graph
   849     /// and it use the given reader as the default skipper.
   850     BpUGraphReader(LemonReader& _reader, Graph& _graph, 
   851 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   852       : reader(_reader), own_reader(false), skipper(_skipper),
   853 	nodeset_reader(*reader, _graph, std::string(), skipper),
   854 	uedgeset_reader(*reader, _graph, nodeset_reader, 
   855 			     std::string(), skipper),
   856 	node_reader(*reader, nodeset_reader, std::string()),
   857 	uedge_reader(*reader, uedgeset_reader, std::string()),
   858 	attribute_reader(*reader, std::string()) {}
   859 
   860     /// \brief Destruct the graph reader.
   861     ///
   862     /// Destruct the graph reader.
   863     ~BpUGraphReader() {
   864       if (own_reader) 
   865 	delete reader;
   866     }
   867 
   868     /// \brief Give a new node map reading command to the reader.
   869     ///
   870     /// Give a new node map reading command to the reader.
   871     template <typename Map>
   872     BpUGraphReader& readNodeMap(std::string name, Map& map) {
   873       nodeset_reader.readNodeMap(name, map);
   874       return *this;
   875     }
   876 
   877     template <typename Map>
   878     BpUGraphReader& readNodeMap(std::string name, const Map& map) {
   879       nodeset_reader.readNodeMap(name, map);
   880       return *this;
   881     }
   882 
   883     /// \brief Give a new node map reading command to the reader.
   884     ///
   885     /// Give a new node map reading command to the reader.
   886     template <typename ItemReader, typename Map>
   887     BpUGraphReader& readNodeMap(std::string name, Map& map, 
   888                               const ItemReader& ir = ItemReader()) {
   889       nodeset_reader.readNodeMap(name, map, ir);
   890       return *this;
   891     }
   892 
   893     template <typename ItemReader, typename Map>
   894     BpUGraphReader& readNodeMap(std::string name, const Map& map, 
   895                               const ItemReader& ir = ItemReader()) {
   896       nodeset_reader.readNodeMap(name, map, ir);
   897       return *this;
   898     }
   899 
   900     /// \brief Give a new node map skipping command to the reader.
   901     ///
   902     /// Give a new node map skipping command to the reader.
   903     template <typename ItemReader>
   904     BpUGraphReader& skipNodeMap(std::string name, 
   905                               const ItemReader& ir = ItemReader()) {
   906       nodeset_reader.skipNodeMap(name, ir);
   907       return *this;
   908     }
   909 
   910     /// \brief Give a new A-node map reading command to the reader.
   911     ///
   912     /// Give a new A-node map reading command to the reader.
   913     template <typename Map>
   914     BpUGraphReader& readANodeMap(std::string name, Map& map) {
   915       nodeset_reader.readANodeMap(name, map);
   916       return *this;
   917     }
   918 
   919     template <typename Map>
   920     BpUGraphReader& readANodeMap(std::string name, const Map& map) {
   921       nodeset_reader.readANodeMap(name, map);
   922       return *this;
   923     }
   924 
   925     /// \brief Give a new A-node map reading command to the reader.
   926     ///
   927     /// Give a new A-node map reading command to the reader.
   928     template <typename ItemReader, typename Map>
   929     BpUGraphReader& readANodeMap(std::string name, Map& map, 
   930                               const ItemReader& ir = ItemReader()) {
   931       nodeset_reader.readANodeMap(name, map, ir);
   932       return *this;
   933     }
   934 
   935     template <typename ItemReader, typename Map>
   936     BpUGraphReader& readANodeMap(std::string name, const Map& map, 
   937                               const ItemReader& ir = ItemReader()) {
   938       nodeset_reader.readNodeMap(name, map, ir);
   939       return *this;
   940     }
   941 
   942     /// \brief Give a new A-node map skipping command to the reader.
   943     ///
   944     /// Give a new A-node map skipping command to the reader.
   945     template <typename ItemReader>
   946     BpUGraphReader& skipANodeMap(std::string name, 
   947                               const ItemReader& ir = ItemReader()) {
   948       nodeset_reader.skipANodeMap(name, ir);
   949       return *this;
   950     }
   951 
   952     /// \brief Give a new B-node map reading command to the reader.
   953     ///
   954     /// Give a new B-node map reading command to the reader.
   955     template <typename Map>
   956     BpUGraphReader& readBNodeMap(std::string name, Map& map) {
   957       nodeset_reader.readBNodeMap(name, map);
   958       return *this;
   959     }
   960 
   961     template <typename Map>
   962     BpUGraphReader& readBNodeMap(std::string name, const Map& map) {
   963       nodeset_reader.readBNodeMap(name, map);
   964       return *this;
   965     }
   966 
   967     /// \brief Give a new B-node map reading command to the reader.
   968     ///
   969     /// Give a new B-node map reading command to the reader.
   970     template <typename ItemReader, typename Map>
   971     BpUGraphReader& readBNodeMap(std::string name, Map& map, 
   972                               const ItemReader& ir = ItemReader()) {
   973       nodeset_reader.readBNodeMap(name, map, ir);
   974       return *this;
   975     }
   976 
   977     template <typename ItemReader, typename Map>
   978     BpUGraphReader& readBNodeMap(std::string name, const Map& map, 
   979                               const ItemReader& ir = ItemReader()) {
   980       nodeset_reader.readNodeMap(name, map, ir);
   981       return *this;
   982     }
   983 
   984     /// \brief Give a new B-node map skipping command to the reader.
   985     ///
   986     /// Give a new B-node map skipping command to the reader.
   987     template <typename ItemReader>
   988     BpUGraphReader& skipBNodeMap(std::string name, 
   989                               const ItemReader& ir = ItemReader()) {
   990       nodeset_reader.skipBNodeMap(name, ir);
   991       return *this;
   992     }
   993 
   994     /// \brief Give a new undirected edge map reading command to the reader.
   995     ///
   996     /// Give a new undirected edge map reading command to the reader.
   997     template <typename Map>
   998     BpUGraphReader& readUEdgeMap(std::string name, Map& map) { 
   999       uedgeset_reader.readUEdgeMap(name, map);
  1000       return *this;
  1001     }
  1002 
  1003     template <typename Map>
  1004     BpUGraphReader& readUEdgeMap(std::string name, const Map& map) { 
  1005       uedgeset_reader.readUEdgeMap(name, map);
  1006       return *this;
  1007     }
  1008 
  1009 
  1010     /// \brief Give a new undirected edge map reading command to the reader.
  1011     ///
  1012     /// Give a new undirected edge map reading command to the reader.
  1013     template <typename ItemReader, typename Map>
  1014     BpUGraphReader& readUEdgeMap(std::string name, Map& map,
  1015                                const ItemReader& ir = ItemReader()) {
  1016       uedgeset_reader.readUEdgeMap(name, map, ir);
  1017       return *this;
  1018     }
  1019 
  1020     template <typename ItemReader, typename Map>
  1021     BpUGraphReader& readUEdgeMap(std::string name, const Map& map,
  1022                                const ItemReader& ir = ItemReader()) {
  1023       uedgeset_reader.readUEdgeMap(name, map, ir);
  1024       return *this;
  1025     }
  1026 
  1027     /// \brief Give a new undirected edge map skipping command to the reader.
  1028     ///
  1029     /// Give a new undirected edge map skipping command to the reader.
  1030     template <typename ItemReader>
  1031     BpUGraphReader& skipUEdgeMap(std::string name,
  1032 				       const ItemReader& ir = ItemReader()) {
  1033       uedgeset_reader.skipUMap(name, ir);
  1034       return *this;
  1035     }
  1036 
  1037 
  1038     /// \brief Give a new edge map reading command to the reader.
  1039     ///
  1040     /// Give a new edge map reading command to the reader.
  1041     template <typename Map>
  1042     BpUGraphReader& readEdgeMap(std::string name, Map& map) { 
  1043       uedgeset_reader.readEdgeMap(name, map);
  1044       return *this;
  1045     }
  1046 
  1047     template <typename Map>
  1048     BpUGraphReader& readEdgeMap(std::string name, const Map& map) { 
  1049       uedgeset_reader.readEdgeMap(name, map);
  1050       return *this;
  1051     }
  1052 
  1053 
  1054     /// \brief Give a new edge map reading command to the reader.
  1055     ///
  1056     /// Give a new edge map reading command to the reader.
  1057     template <typename ItemReader, typename Map>
  1058     BpUGraphReader& readEdgeMap(std::string name, Map& map,
  1059                               const ItemReader& ir = ItemReader()) {
  1060       uedgeset_reader.readEdgeMap(name, map, ir);
  1061       return *this;
  1062     }
  1063 
  1064     template <typename ItemReader, typename Map>
  1065     BpUGraphReader& readEdgeMap(std::string name, const Map& map,
  1066                               const ItemReader& ir = ItemReader()) {
  1067       uedgeset_reader.readEdgeMap(name, map, ir);
  1068       return *this;
  1069     }
  1070 
  1071     /// \brief Give a new edge map skipping command to the reader.
  1072     ///
  1073     /// Give a new edge map skipping command to the reader.
  1074     template <typename ItemReader>
  1075     BpUGraphReader& skipEdgeMap(std::string name,
  1076                               const ItemReader& ir = ItemReader()) {
  1077       uedgeset_reader.skipEdgeMap(name, ir);
  1078       return *this;
  1079     }
  1080 
  1081     /// \brief Give a new labeled node reading command to the reader.
  1082     ///
  1083     /// Give a new labeled node reading command to the reader.
  1084     BpUGraphReader& readNode(std::string name, Node& node) {
  1085       node_reader.readNode(name, node);
  1086       return *this;
  1087     }
  1088 
  1089     /// \brief Give a new labeled edge reading command to the reader.
  1090     ///
  1091     /// Give a new labeled edge reading command to the reader.
  1092     BpUGraphReader& readEdge(std::string name, Edge& edge) {
  1093       uedge_reader.readEdge(name, edge);
  1094     }
  1095 
  1096     /// \brief Give a new labeled undirected edge reading command to the
  1097     /// reader.
  1098     ///
  1099     /// Give a new labeled undirected edge reading command to the reader.
  1100     BpUGraphReader& readUEdge(std::string name, UEdge& edge) {
  1101       uedge_reader.readUEdge(name, edge);
  1102     }
  1103 
  1104     /// \brief Give a new attribute reading command.
  1105     ///
  1106     ///  Give a new attribute reading command.
  1107     template <typename Value>
  1108     BpUGraphReader& readAttribute(std::string name, Value& value) {
  1109       attribute_reader.readAttribute(name, value);
  1110       return *this;
  1111     }
  1112     
  1113     /// \brief Give a new attribute reading command.
  1114     ///
  1115     ///  Give a new attribute reading command.
  1116     template <typename ItemReader, typename Value>
  1117     BpUGraphReader& readAttribute(std::string name, Value& value, 
  1118 			       const ItemReader& ir = ItemReader()) {
  1119       attribute_reader.readAttribute(name, value, ir);
  1120       return *this;
  1121     }
  1122 
  1123     /// \brief Conversion operator to LemonReader.
  1124     ///
  1125     /// Conversion operator to LemonReader. It make possible
  1126     /// to access the encapsulated \e LemonReader, this way
  1127     /// you can attach to this reader new instances of 
  1128     /// \e LemonReader::SectionReader.
  1129     operator LemonReader&() {
  1130       return *reader;
  1131     }
  1132 
  1133     /// \brief Executes the reading commands.
  1134     ///
  1135     /// Executes the reading commands.
  1136     void run() {
  1137       reader->run();
  1138     }
  1139 
  1140 
  1141     /// \brief Returns true if the reader can give back the items by its label.
  1142     ///
  1143     /// Returns true if the reader can give back the items by its label.
  1144     bool isLabelReader() const {
  1145       return nodeset_reader.isLabelReader() && 
  1146         uedgeset_reader.isLabelReader();
  1147     }
  1148 
  1149     /// \brief Gives back the node by its label.
  1150     ///
  1151     /// It reads an label from the stream and gives back which node belongs to
  1152     /// it. It is possible only if there was read a "label" named node map.
  1153     void readLabel(std::istream& is, Node& node) const {
  1154       return nodeset_reader.readLabel(is, node);
  1155     } 
  1156 
  1157     /// \brief Gives back the edge by its label
  1158     ///
  1159     /// It reads an label from the stream and gives back which edge belongs to
  1160     /// it. It is possible only if there was read a "label" named edge map.
  1161     void readLabel(std::istream& is, Edge& edge) const {
  1162       return uedgeset_reader.readLabel(is, edge);
  1163     } 
  1164 
  1165     /// \brief Gives back the undirected edge by its label.
  1166     ///
  1167     /// It reads an label from the stream and gives back which undirected edge 
  1168     /// belongs to it. It is possible only if there was read a "label" named 
  1169     /// edge map.
  1170     void readLabel(std::istream& is, UEdge& uedge) const {
  1171       return uedgeset_reader.readLabel(is, uedge);
  1172     } 
  1173     
  1174 
  1175   private:
  1176 
  1177     LemonReader* reader;
  1178     bool own_reader;
  1179 
  1180     DefaultSkipper skipper;
  1181 
  1182     BpNodeSetReader<Graph, ReaderTraits> nodeset_reader;
  1183     UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
  1184 
  1185     NodeReader<Graph> node_reader;
  1186     UEdgeReader<Graph> uedge_reader;
  1187     
  1188     AttributeReader<ReaderTraits> attribute_reader;
  1189   };
  1190 
  1191 
  1192   /// @}
  1193 }
  1194 
  1195 #endif