lemon/graph_reader.h
changeset 2502 9c23c3762bc5
parent 2467 2025a571895e
child 2553 bfced05fa852
     1.1 --- a/lemon/graph_reader.h	Sat Oct 20 14:29:12 2007 +0000
     1.2 +++ b/lemon/graph_reader.h	Wed Oct 24 16:31:49 2007 +0000
     1.3 @@ -725,6 +725,469 @@
     1.4      AttributeReader<ReaderTraits> attribute_reader;
     1.5    };
     1.6  
     1.7 +  /// \brief The bipartite graph reader class.
     1.8 +  ///
     1.9 +  /// The \c BpUGraphReader class provides the graph input. 
    1.10 +  /// Before you read this documentation it might be useful to read the general
    1.11 +  /// description of  \ref graph-io-page "Graph Input-Output".
    1.12 +  ///
    1.13 +  /// The given file format may contain several maps and labeled nodes or 
    1.14 +  /// edges.
    1.15 +  ///
    1.16 +  /// If you read a graph you need not read all the maps and items just those
    1.17 +  /// that you need. The interface of the \c BpUGraphReader is very similar
    1.18 +  /// to the BpUGraphWriter but the reading method does not depend on the
    1.19 +  /// order of the given commands.
    1.20 +  ///
    1.21 +  /// The reader object suppose that each not read value does not contain 
    1.22 +  /// whitespaces, therefore it has some extra possibilities to control how
    1.23 +  /// it should skip the values when the string representation contains spaces.
    1.24 +  ///
    1.25 +  ///\code
    1.26 +  /// BpUGraphReader<ListBpUGraph> reader(std::cin, graph);
    1.27 +  ///\endcode
    1.28 +  ///
    1.29 +  /// The \c readANodeMap() function reads a map from the A-part of
    1.30 +  /// the\c \@bpnodeset section, while the \c readBNodeMap() reads
    1.31 +  /// from the B-part of the section.  If you use the \c readNodeMap()
    1.32 +  /// function, then the given map should appear in both part of the
    1.33 +  /// section. If there is a map that you do not want to read from the
    1.34 +  /// file and there is whitespace in the string represenation of the
    1.35 +  /// values then you should call the \c skipANodeMap(), \c
    1.36 +  /// skipBNodeMap() or \c skipNodeMap() template member function with
    1.37 +  /// proper parameters.
    1.38 +  ///
    1.39 +  ///\code
    1.40 +  /// reader.readNodeMap("coords", coords);
    1.41 +  /// reader.readANodeMap("range", range);
    1.42 +  /// reader.readANodeMap("benefit", benefit);
    1.43 +  ///
    1.44 +  /// reader.skipNodeMap("description", desc);
    1.45 +  ///
    1.46 +  /// reader.readNodeMap("color", colorMap);
    1.47 +  ///\endcode
    1.48 +  ///
    1.49 +  /// With the \c readUEdgeMap() member function you can give an 
    1.50 +  /// uedge map reading command similar to the NodeMaps. 
    1.51 +  ///
    1.52 +  ///\code
    1.53 +  /// reader.readUEdgeMap("capacity", capacityMap);
    1.54 +  /// reader.readEdgeMap("flow", flowMap);
    1.55 +  ///\endcode 
    1.56 +  ///
    1.57 +  /// With \c readNode() and \c readUEdge() functions you can read 
    1.58 +  /// labeled Nodes and UEdges.
    1.59 +  ///
    1.60 +  ///\code
    1.61 +  /// reader.readNode("source", sourceNode);
    1.62 +  /// reader.readNode("target", targetNode);
    1.63 +  ///
    1.64 +  /// reader.readUEdge("observed", uEdge);
    1.65 +  ///\endcode
    1.66 +  ///
    1.67 +  /// With the \c readAttribute() functions you can read an attribute
    1.68 +  /// in a variable. You can specify the reader for the attribute as
    1.69 +  /// the nodemaps.
    1.70 +  ///
    1.71 +  /// After you give all read commands you must call the \c run() member
    1.72 +  /// function, which execute all the commands.
    1.73 +  ///
    1.74 +  ///\code
    1.75 +  /// reader.run();
    1.76 +  ///\endcode
    1.77 +  ///
    1.78 +  /// \see GraphReader
    1.79 +  /// \see DefaultReaderTraits
    1.80 +  /// \see \ref UGraphWriter
    1.81 +  /// \see \ref graph-io-page
    1.82 +  ///
    1.83 +  /// \author Balazs Dezso
    1.84 +  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
    1.85 +  class BpUGraphReader {
    1.86 +  public:
    1.87 +    
    1.88 +    typedef _Graph Graph;
    1.89 +    typedef typename Graph::Node Node;
    1.90 +    typedef typename Graph::Edge Edge;
    1.91 +    typedef typename Graph::UEdge UEdge;
    1.92 +
    1.93 +    typedef _ReaderTraits ReaderTraits;
    1.94 +    typedef typename ReaderTraits::Skipper DefaultSkipper;
    1.95 +
    1.96 +    /// \brief Construct a new BpUGraphReader.
    1.97 +    ///
    1.98 +    /// Construct a new BpUGraphReader. It reads into the given graph
    1.99 +    /// and it use the given reader as the default skipper.
   1.100 +    BpUGraphReader(std::istream& _is, Graph& _graph, 
   1.101 +		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   1.102 +      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
   1.103 +	nodeset_reader(*reader, _graph, std::string(), skipper),
   1.104 +	uedgeset_reader(*reader, _graph, nodeset_reader, 
   1.105 +			     std::string(), skipper),
   1.106 +	node_reader(*reader, nodeset_reader, std::string()),
   1.107 +	uedge_reader(*reader, uedgeset_reader, std::string()),
   1.108 +	attribute_reader(*reader, std::string()) {}
   1.109 +
   1.110 +    /// \brief Construct a new BpUGraphReader.
   1.111 +    ///
   1.112 +    /// Construct a new BpUGraphReader. It reads into the given graph
   1.113 +    /// and it use the given reader as the default skipper.
   1.114 +    BpUGraphReader(const std::string& _filename, Graph& _graph, 
   1.115 +		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   1.116 +      : reader(new LemonReader(_filename)), own_reader(true), 
   1.117 +	skipper(_skipper),
   1.118 +	nodeset_reader(*reader, _graph, std::string(), skipper),
   1.119 +	uedgeset_reader(*reader, _graph, nodeset_reader, 
   1.120 +			     std::string(), skipper),
   1.121 +	node_reader(*reader, nodeset_reader, std::string()),
   1.122 +	uedge_reader(*reader, uedgeset_reader, std::string()),
   1.123 +	attribute_reader(*reader, std::string()) {}
   1.124 +
   1.125 +    /// \brief Construct a new BpUGraphReader.
   1.126 +    ///
   1.127 +    /// Construct a new BpUGraphReader. It reads into the given graph
   1.128 +    /// and it use the given reader as the default skipper.
   1.129 +    BpUGraphReader(LemonReader& _reader, Graph& _graph, 
   1.130 +		     const DefaultSkipper& _skipper = DefaultSkipper()) 
   1.131 +      : reader(_reader), own_reader(false), skipper(_skipper),
   1.132 +	nodeset_reader(*reader, _graph, std::string(), skipper),
   1.133 +	uedgeset_reader(*reader, _graph, nodeset_reader, 
   1.134 +			     std::string(), skipper),
   1.135 +	node_reader(*reader, nodeset_reader, std::string()),
   1.136 +	uedge_reader(*reader, uedgeset_reader, std::string()),
   1.137 +	attribute_reader(*reader, std::string()) {}
   1.138 +
   1.139 +    /// \brief Destruct the graph reader.
   1.140 +    ///
   1.141 +    /// Destruct the graph reader.
   1.142 +    ~BpUGraphReader() {
   1.143 +      if (own_reader) 
   1.144 +	delete reader;
   1.145 +    }
   1.146 +
   1.147 +    /// \brief Give a new node map reading command to the reader.
   1.148 +    ///
   1.149 +    /// Give a new node map reading command to the reader.
   1.150 +    template <typename Map>
   1.151 +    BpUGraphReader& readNodeMap(std::string name, Map& map) {
   1.152 +      nodeset_reader.readNodeMap(name, map);
   1.153 +      return *this;
   1.154 +    }
   1.155 +
   1.156 +    template <typename Map>
   1.157 +    BpUGraphReader& readNodeMap(std::string name, const Map& map) {
   1.158 +      nodeset_reader.readNodeMap(name, map);
   1.159 +      return *this;
   1.160 +    }
   1.161 +
   1.162 +    /// \brief Give a new node map reading command to the reader.
   1.163 +    ///
   1.164 +    /// Give a new node map reading command to the reader.
   1.165 +    template <typename ItemReader, typename Map>
   1.166 +    BpUGraphReader& readNodeMap(std::string name, Map& map, 
   1.167 +                              const ItemReader& ir = ItemReader()) {
   1.168 +      nodeset_reader.readNodeMap(name, map, ir);
   1.169 +      return *this;
   1.170 +    }
   1.171 +
   1.172 +    template <typename ItemReader, typename Map>
   1.173 +    BpUGraphReader& readNodeMap(std::string name, const Map& map, 
   1.174 +                              const ItemReader& ir = ItemReader()) {
   1.175 +      nodeset_reader.readNodeMap(name, map, ir);
   1.176 +      return *this;
   1.177 +    }
   1.178 +
   1.179 +    /// \brief Give a new node map skipping command to the reader.
   1.180 +    ///
   1.181 +    /// Give a new node map skipping command to the reader.
   1.182 +    template <typename ItemReader>
   1.183 +    BpUGraphReader& skipNodeMap(std::string name, 
   1.184 +                              const ItemReader& ir = ItemReader()) {
   1.185 +      nodeset_reader.skipNodeMap(name, ir);
   1.186 +      return *this;
   1.187 +    }
   1.188 +
   1.189 +    /// \brief Give a new A-node map reading command to the reader.
   1.190 +    ///
   1.191 +    /// Give a new A-node map reading command to the reader.
   1.192 +    template <typename Map>
   1.193 +    BpUGraphReader& readANodeMap(std::string name, Map& map) {
   1.194 +      nodeset_reader.readANodeMap(name, map);
   1.195 +      return *this;
   1.196 +    }
   1.197 +
   1.198 +    template <typename Map>
   1.199 +    BpUGraphReader& readANodeMap(std::string name, const Map& map) {
   1.200 +      nodeset_reader.readANodeMap(name, map);
   1.201 +      return *this;
   1.202 +    }
   1.203 +
   1.204 +    /// \brief Give a new A-node map reading command to the reader.
   1.205 +    ///
   1.206 +    /// Give a new A-node map reading command to the reader.
   1.207 +    template <typename ItemReader, typename Map>
   1.208 +    BpUGraphReader& readANodeMap(std::string name, Map& map, 
   1.209 +                              const ItemReader& ir = ItemReader()) {
   1.210 +      nodeset_reader.readANodeMap(name, map, ir);
   1.211 +      return *this;
   1.212 +    }
   1.213 +
   1.214 +    template <typename ItemReader, typename Map>
   1.215 +    BpUGraphReader& readANodeMap(std::string name, const Map& map, 
   1.216 +                              const ItemReader& ir = ItemReader()) {
   1.217 +      nodeset_reader.readNodeMap(name, map, ir);
   1.218 +      return *this;
   1.219 +    }
   1.220 +
   1.221 +    /// \brief Give a new A-node map skipping command to the reader.
   1.222 +    ///
   1.223 +    /// Give a new A-node map skipping command to the reader.
   1.224 +    template <typename ItemReader>
   1.225 +    BpUGraphReader& skipANodeMap(std::string name, 
   1.226 +                              const ItemReader& ir = ItemReader()) {
   1.227 +      nodeset_reader.skipANodeMap(name, ir);
   1.228 +      return *this;
   1.229 +    }
   1.230 +
   1.231 +    /// \brief Give a new B-node map reading command to the reader.
   1.232 +    ///
   1.233 +    /// Give a new B-node map reading command to the reader.
   1.234 +    template <typename Map>
   1.235 +    BpUGraphReader& readBNodeMap(std::string name, Map& map) {
   1.236 +      nodeset_reader.readBNodeMap(name, map);
   1.237 +      return *this;
   1.238 +    }
   1.239 +
   1.240 +    template <typename Map>
   1.241 +    BpUGraphReader& readBNodeMap(std::string name, const Map& map) {
   1.242 +      nodeset_reader.readBNodeMap(name, map);
   1.243 +      return *this;
   1.244 +    }
   1.245 +
   1.246 +    /// \brief Give a new B-node map reading command to the reader.
   1.247 +    ///
   1.248 +    /// Give a new B-node map reading command to the reader.
   1.249 +    template <typename ItemReader, typename Map>
   1.250 +    BpUGraphReader& readBNodeMap(std::string name, Map& map, 
   1.251 +                              const ItemReader& ir = ItemReader()) {
   1.252 +      nodeset_reader.readBNodeMap(name, map, ir);
   1.253 +      return *this;
   1.254 +    }
   1.255 +
   1.256 +    template <typename ItemReader, typename Map>
   1.257 +    BpUGraphReader& readBNodeMap(std::string name, const Map& map, 
   1.258 +                              const ItemReader& ir = ItemReader()) {
   1.259 +      nodeset_reader.readNodeMap(name, map, ir);
   1.260 +      return *this;
   1.261 +    }
   1.262 +
   1.263 +    /// \brief Give a new B-node map skipping command to the reader.
   1.264 +    ///
   1.265 +    /// Give a new B-node map skipping command to the reader.
   1.266 +    template <typename ItemReader>
   1.267 +    BpUGraphReader& skipBNodeMap(std::string name, 
   1.268 +                              const ItemReader& ir = ItemReader()) {
   1.269 +      nodeset_reader.skipBNodeMap(name, ir);
   1.270 +      return *this;
   1.271 +    }
   1.272 +
   1.273 +    /// \brief Give a new undirected edge map reading command to the reader.
   1.274 +    ///
   1.275 +    /// Give a new undirected edge map reading command to the reader.
   1.276 +    template <typename Map>
   1.277 +    BpUGraphReader& readUEdgeMap(std::string name, Map& map) { 
   1.278 +      uedgeset_reader.readUEdgeMap(name, map);
   1.279 +      return *this;
   1.280 +    }
   1.281 +
   1.282 +    template <typename Map>
   1.283 +    BpUGraphReader& readUEdgeMap(std::string name, const Map& map) { 
   1.284 +      uedgeset_reader.readUEdgeMap(name, map);
   1.285 +      return *this;
   1.286 +    }
   1.287 +
   1.288 +
   1.289 +    /// \brief Give a new undirected edge map reading command to the reader.
   1.290 +    ///
   1.291 +    /// Give a new undirected edge map reading command to the reader.
   1.292 +    template <typename ItemReader, typename Map>
   1.293 +    BpUGraphReader& readUEdgeMap(std::string name, Map& map,
   1.294 +                               const ItemReader& ir = ItemReader()) {
   1.295 +      uedgeset_reader.readUEdgeMap(name, map, ir);
   1.296 +      return *this;
   1.297 +    }
   1.298 +
   1.299 +    template <typename ItemReader, typename Map>
   1.300 +    BpUGraphReader& readUEdgeMap(std::string name, const Map& map,
   1.301 +                               const ItemReader& ir = ItemReader()) {
   1.302 +      uedgeset_reader.readUEdgeMap(name, map, ir);
   1.303 +      return *this;
   1.304 +    }
   1.305 +
   1.306 +    /// \brief Give a new undirected edge map skipping command to the reader.
   1.307 +    ///
   1.308 +    /// Give a new undirected edge map skipping command to the reader.
   1.309 +    template <typename ItemReader>
   1.310 +    BpUGraphReader& skipUEdgeMap(std::string name,
   1.311 +				       const ItemReader& ir = ItemReader()) {
   1.312 +      uedgeset_reader.skipUMap(name, ir);
   1.313 +      return *this;
   1.314 +    }
   1.315 +
   1.316 +
   1.317 +    /// \brief Give a new edge map reading command to the reader.
   1.318 +    ///
   1.319 +    /// Give a new edge map reading command to the reader.
   1.320 +    template <typename Map>
   1.321 +    BpUGraphReader& readEdgeMap(std::string name, Map& map) { 
   1.322 +      uedgeset_reader.readEdgeMap(name, map);
   1.323 +      return *this;
   1.324 +    }
   1.325 +
   1.326 +    template <typename Map>
   1.327 +    BpUGraphReader& readEdgeMap(std::string name, const Map& map) { 
   1.328 +      uedgeset_reader.readEdgeMap(name, map);
   1.329 +      return *this;
   1.330 +    }
   1.331 +
   1.332 +
   1.333 +    /// \brief Give a new edge map reading command to the reader.
   1.334 +    ///
   1.335 +    /// Give a new edge map reading command to the reader.
   1.336 +    template <typename ItemReader, typename Map>
   1.337 +    BpUGraphReader& readEdgeMap(std::string name, Map& map,
   1.338 +                              const ItemReader& ir = ItemReader()) {
   1.339 +      uedgeset_reader.readEdgeMap(name, map, ir);
   1.340 +      return *this;
   1.341 +    }
   1.342 +
   1.343 +    template <typename ItemReader, typename Map>
   1.344 +    BpUGraphReader& readEdgeMap(std::string name, const Map& map,
   1.345 +                              const ItemReader& ir = ItemReader()) {
   1.346 +      uedgeset_reader.readEdgeMap(name, map, ir);
   1.347 +      return *this;
   1.348 +    }
   1.349 +
   1.350 +    /// \brief Give a new edge map skipping command to the reader.
   1.351 +    ///
   1.352 +    /// Give a new edge map skipping command to the reader.
   1.353 +    template <typename ItemReader>
   1.354 +    BpUGraphReader& skipEdgeMap(std::string name,
   1.355 +                              const ItemReader& ir = ItemReader()) {
   1.356 +      uedgeset_reader.skipEdgeMap(name, ir);
   1.357 +      return *this;
   1.358 +    }
   1.359 +
   1.360 +    /// \brief Give a new labeled node reading command to the reader.
   1.361 +    ///
   1.362 +    /// Give a new labeled node reading command to the reader.
   1.363 +    BpUGraphReader& readNode(std::string name, Node& node) {
   1.364 +      node_reader.readNode(name, node);
   1.365 +      return *this;
   1.366 +    }
   1.367 +
   1.368 +    /// \brief Give a new labeled edge reading command to the reader.
   1.369 +    ///
   1.370 +    /// Give a new labeled edge reading command to the reader.
   1.371 +    BpUGraphReader& readEdge(std::string name, Edge& edge) {
   1.372 +      uedge_reader.readEdge(name, edge);
   1.373 +    }
   1.374 +
   1.375 +    /// \brief Give a new labeled undirected edge reading command to the
   1.376 +    /// reader.
   1.377 +    ///
   1.378 +    /// Give a new labeled undirected edge reading command to the reader.
   1.379 +    BpUGraphReader& readUEdge(std::string name, UEdge& edge) {
   1.380 +      uedge_reader.readUEdge(name, edge);
   1.381 +    }
   1.382 +
   1.383 +    /// \brief Give a new attribute reading command.
   1.384 +    ///
   1.385 +    ///  Give a new attribute reading command.
   1.386 +    template <typename Value>
   1.387 +    BpUGraphReader& readAttribute(std::string name, Value& value) {
   1.388 +      attribute_reader.readAttribute(name, value);
   1.389 +      return *this;
   1.390 +    }
   1.391 +    
   1.392 +    /// \brief Give a new attribute reading command.
   1.393 +    ///
   1.394 +    ///  Give a new attribute reading command.
   1.395 +    template <typename ItemReader, typename Value>
   1.396 +    BpUGraphReader& readAttribute(std::string name, Value& value, 
   1.397 +			       const ItemReader& ir = ItemReader()) {
   1.398 +      attribute_reader.readAttribute(name, value, ir);
   1.399 +      return *this;
   1.400 +    }
   1.401 +
   1.402 +    /// \brief Conversion operator to LemonReader.
   1.403 +    ///
   1.404 +    /// Conversion operator to LemonReader. It make possible
   1.405 +    /// to access the encapsulated \e LemonReader, this way
   1.406 +    /// you can attach to this reader new instances of 
   1.407 +    /// \e LemonReader::SectionReader.
   1.408 +    operator LemonReader&() {
   1.409 +      return *reader;
   1.410 +    }
   1.411 +
   1.412 +    /// \brief Executes the reading commands.
   1.413 +    ///
   1.414 +    /// Executes the reading commands.
   1.415 +    void run() {
   1.416 +      reader->run();
   1.417 +    }
   1.418 +
   1.419 +
   1.420 +    /// \brief Returns true if the reader can give back the items by its label.
   1.421 +    ///
   1.422 +    /// Returns true if the reader can give back the items by its label.
   1.423 +    bool isLabelReader() const {
   1.424 +      return nodeset_reader.isLabelReader() && 
   1.425 +        uedgeset_reader.isLabelReader();
   1.426 +    }
   1.427 +
   1.428 +    /// \brief Gives back the node by its label.
   1.429 +    ///
   1.430 +    /// It reads an label from the stream and gives back which node belongs to
   1.431 +    /// it. It is possible only if there was read a "label" named node map.
   1.432 +    void readLabel(std::istream& is, Node& node) const {
   1.433 +      return nodeset_reader.readLabel(is, node);
   1.434 +    } 
   1.435 +
   1.436 +    /// \brief Gives back the edge by its label
   1.437 +    ///
   1.438 +    /// It reads an label from the stream and gives back which edge belongs to
   1.439 +    /// it. It is possible only if there was read a "label" named edge map.
   1.440 +    void readLabel(std::istream& is, Edge& edge) const {
   1.441 +      return uedgeset_reader.readLabel(is, edge);
   1.442 +    } 
   1.443 +
   1.444 +    /// \brief Gives back the undirected edge by its label.
   1.445 +    ///
   1.446 +    /// It reads an label from the stream and gives back which undirected edge 
   1.447 +    /// belongs to it. It is possible only if there was read a "label" named 
   1.448 +    /// edge map.
   1.449 +    void readLabel(std::istream& is, UEdge& uedge) const {
   1.450 +      return uedgeset_reader.readLabel(is, uedge);
   1.451 +    } 
   1.452 +    
   1.453 +
   1.454 +  private:
   1.455 +
   1.456 +    LemonReader* reader;
   1.457 +    bool own_reader;
   1.458 +
   1.459 +    DefaultSkipper skipper;
   1.460 +
   1.461 +    BpNodeSetReader<Graph, ReaderTraits> nodeset_reader;
   1.462 +    UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
   1.463 +
   1.464 +    NodeReader<Graph> node_reader;
   1.465 +    UEdgeReader<Graph> uedge_reader;
   1.466 +    
   1.467 +    AttributeReader<ReaderTraits> attribute_reader;
   1.468 +  };
   1.469 +
   1.470  
   1.471    /// @}
   1.472  }