COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/graph_reader.h

Last change on this file was 2553:bfced05fa852, checked in by Alpar Juttner, 16 years ago

Happy New Year to LEMON (+ better update-copyright-header script)

File size: 40.4 KB
RevLine 
[1137]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
[2553]5 * Copyright (C) 2003-2008
[1956]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[1137]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
[2084]19///\ingroup lemon_io
[1137]20///\file
[1287]21///\brief Lemon Graph Format reader.
[1137]22
[1214]23#ifndef LEMON_GRAPH_READER_H
24#define LEMON_GRAPH_READER_H
25
[1137]26#include <iostream>
27
28#include <lemon/error.h>
[1408]29#include <lemon/lemon_reader.h>
[1137]30
31namespace lemon {
32
[2084]33  /// \addtogroup lemon_io
[1333]34  /// @{
[1137]35
36  /// \brief The graph reader class.
37  ///
[1534]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".
[1540]41  ///
[2334]42  /// The file to be read may contain several maps and labeled
43  /// (designated) nodes or edges.
[1333]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
[1540]48  /// given commands (i.e. you don't have to insist on the order in which the
49  /// maps are given in the file).
[1333]50  ///
[2100]51  /// The reader object assumes that not read values do not contain
[1333]52  /// whitespaces, therefore it has some extra possibilities to control how
53  /// it should skip the values when the string representation contains spaces.
54  ///
[1946]55  ///\code
[1333]56  /// GraphReader<ListGraph> reader(std::cin, graph);
[1946]57  ///\endcode
[1333]58  ///
[1394]59  /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
[1333]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  ///
[1946]65  ///\code
[1421]66  /// reader.readNodeMap("coords", coords);
[1333]67  ///
[1901]68  /// reader.skipNodeMap("description", desc);
[1333]69  ///
[1394]70  /// reader.readNodeMap("color", colorMap);
[1946]71  ///\endcode
[1333]72  ///
[1394]73  /// With the \c readEdgeMap() member function you can give an edge map
[1333]74  /// reading command similar to the NodeMaps.
75  ///
[1946]76  ///\code
[1394]77  /// reader.readEdgeMap("weight", weightMap);
78  /// reader.readEdgeMap("label", labelMap);
[1946]79  ///\endcode
[1333]80  ///
[1408]81  /// With \c readNode() and \c readEdge() functions you can read
82  /// labeled Nodes and Edges.
[1333]83  ///
[1946]84  ///\code
[1394]85  /// reader.readNode("source", sourceNode);
86  /// reader.readNode("target", targetNode);
[1333]87  ///
[1394]88  /// reader.readEdge("observed", edge);
[1946]89  ///\endcode
[1333]90  ///
[1408]91  /// With the \c readAttribute() functions you can read an attribute
[1540]92  /// into a variable. You can specify the reader for the attribute as
[1408]93  /// the nodemaps.
94  ///
[1333]95  /// After you give all read commands you must call the \c run() member
[1540]96  /// function, which executes all the commands.
[1333]97  ///
[1946]98  ///\code
[1333]99  /// reader.run();
[1946]100  ///\endcode
[1333]101  ///
[1287]102  /// \see DefaultReaderTraits
103  /// \see QuotedStringReader
[1138]104  /// \see \ref GraphWriter
105  /// \see \ref graph-io-page
[1333]106  /// \author Balazs Dezso
[1137]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;
[1408]116    typedef typename ReaderTraits::Skipper DefaultSkipper;
[1137]117
118    /// \brief Construct a new GraphReader.
119    ///
[1208]120    /// Construct a new GraphReader. It reads into the given graph
[1540]121    /// and it uses the given reader as the default skipper.
[1705]122    GraphReader(std::istream& _is, Graph& _graph,
[1408]123                const DefaultSkipper& _skipper = DefaultSkipper())
[1421]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),
[1408]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
[1540]135    /// and it uses the given reader as the default skipper.
[1705]136    GraphReader(const std::string& _filename, Graph& _graph,
[1408]137                const DefaultSkipper& _skipper = DefaultSkipper())
138      : reader(new LemonReader(_filename)), own_reader(true),
[1421]139        skipper(_skipper),
140        nodeset_reader(*reader, _graph, std::string(), skipper),
141        edgeset_reader(*reader, _graph, nodeset_reader,
142                       std::string(), skipper),
[1408]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
[1540]150    /// and it uses the given reader as the default skipper.
[1705]151    GraphReader(LemonReader& _reader, Graph& _graph,
[1408]152                const DefaultSkipper& _skipper = DefaultSkipper())
[1421]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),
[1408]157        node_reader(*reader, nodeset_reader, std::string()),
158        edge_reader(*reader, edgeset_reader, std::string()),
159        attribute_reader(*reader, std::string()) {}
[1137]160
161    /// \brief Destruct the graph reader.
162    ///
163    /// Destruct the graph reader.
164    ~GraphReader() {
[1408]165      if (own_reader)
166        delete reader;
[1137]167    }
168
[1540]169    /// \brief Give a new node map reading command to the reader.
[1137]170    ///
[1540]171    /// Give a new node map reading command to the reader.
[1137]172    template <typename Map>
[1394]173    GraphReader& readNodeMap(std::string name, Map& map) {
[1421]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);
[1408]181      return *this;
[1137]182    }
183
[1540]184    /// \brief Give a new node map reading command to the reader.
[1137]185    ///
[1540]186    /// Give a new node map reading command to the reader.
[2386]187    template <typename ItemReader, typename Map>
[1394]188    GraphReader& readNodeMap(std::string name, Map& map,
[2386]189                             const ItemReader& ir = ItemReader()) {
190      nodeset_reader.readNodeMap(name, map, ir);
[1421]191      return *this;
192    }
193
[2386]194    template <typename ItemReader, typename Map>
[1421]195    GraphReader& readNodeMap(std::string name, const Map& map,
[2386]196                             const ItemReader& ir = ItemReader()) {
197      nodeset_reader.readNodeMap(name, map, ir);
[1137]198      return *this;
199    }
200
[1540]201    /// \brief Give a new node map skipping command to the reader.
[1137]202    ///
[1540]203    /// Give a new node map skipping command to the reader.
[2386]204    template <typename ItemReader>
[1137]205    GraphReader& skipNodeMap(std::string name,
[2386]206                             const ItemReader& ir = ItemReader()) {
207      nodeset_reader.skipNodeMap(name, ir);
[1137]208      return *this;
209    }
210
[1540]211    /// \brief Give a new edge map reading command to the reader.
[1137]212    ///
[1540]213    /// Give a new edge map reading command to the reader.
[1137]214    template <typename Map>
[1394]215    GraphReader& readEdgeMap(std::string name, Map& map) {
[1421]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);
[1408]223      return *this;
[1137]224    }
225
226
[1540]227    /// \brief Give a new edge map reading command to the reader.
[1137]228    ///
[1540]229    /// Give a new edge map reading command to the reader.
[2386]230    template <typename ItemReader, typename Map>
[1394]231    GraphReader& readEdgeMap(std::string name, Map& map,
[2386]232                             const ItemReader& ir = ItemReader()) {
233      edgeset_reader.readEdgeMap(name, map, ir);
[1421]234      return *this;
235    }
236
[2386]237    template <typename ItemReader, typename Map>
[1421]238    GraphReader& readEdgeMap(std::string name, const Map& map,
[2386]239                             const ItemReader& ir = ItemReader()) {
240      edgeset_reader.readEdgeMap(name, map, ir);
[1137]241      return *this;
242    }
243
[1540]244    /// \brief Give a new edge map skipping command to the reader.
[1137]245    ///
[1540]246    /// Give a new edge map skipping command to the reader.
[2386]247    template <typename ItemReader>
[1421]248    GraphReader& skipEdgeMap(std::string name,
[2386]249                             const ItemReader& ir = ItemReader()) {
250      edgeset_reader.skipEdgeMap(name, ir);
[1137]251      return *this;
252    }
253
[1540]254    /// \brief Give a new labeled node reading command to the reader.
[1137]255    ///
[1540]256    /// Give a new labeled node reading command to the reader.
[1394]257    GraphReader& readNode(std::string name, Node& node) {
[1408]258      node_reader.readNode(name, node);
[1137]259      return *this;
260    }
261
[1540]262    /// \brief Give a new labeled edge reading command to the reader.
[1137]263    ///
[1540]264    /// Give a new labeled edge reading command to the reader.
[1394]265    GraphReader& readEdge(std::string name, Edge& edge) {
[1408]266      edge_reader.readEdge(name, edge);
[1476]267      return *this;
[1408]268    }
269
[1540]270    /// \brief Give a new attribute reading command.
[1408]271    ///
[1540]272    ///  Give a new attribute reading command.
[1408]273    template <typename Value>
274    GraphReader& readAttribute(std::string name, Value& value) {
275      attribute_reader.readAttribute(name, value);
[1137]276      return *this;
277    }
[1408]278   
[1540]279    /// \brief Give a new attribute reading command.
[1408]280    ///
[1540]281    ///  Give a new attribute reading command.
[2386]282    template <typename ItemReader, typename Value>
[1408]283    GraphReader& readAttribute(std::string name, Value& value,
[2386]284                               const ItemReader& ir = ItemReader()) {
285      attribute_reader.readAttribute(name, value, ir);
[1408]286      return *this;
287    }
288
289    /// \brief Conversion operator to LemonReader.
290    ///
[1540]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".
[1408]295    operator LemonReader&() {
296      return *reader;
297    }
[1137]298
[1540]299    /// \brief Executes the reading commands.
[1137]300    ///
[1540]301    /// Executes the reading commands.
[1137]302    void run() {
[1408]303      reader->run();
[1396]304    }
305
[1901]306
307    /// \brief Returns true if the reader can give back the items by its label.
[1429]308    ///
[1901]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
[1935]317    /// it. It is possible only if there was read a "label" named node map.
[1901]318    void readLabel(std::istream& is, Node& node) const {
319      nodeset_reader.readLabel(is, node);
[1429]320    }
321
[1901]322    /// \brief Gives back the edge by its label.
[1429]323    ///
[1901]324    /// It reads an label from the stream and gives back which edge belongs to
[1935]325    /// it. It is possible only if there was read a "label" named edge map.
[1901]326    void readLabel(std::istream& is, Edge& edge) const {
[2467]327      edgeset_reader.readLabel(is, edge);
[1429]328    }
329
[1137]330  private:
331
[1408]332    LemonReader* reader;
333    bool own_reader;
[1137]334
[1408]335    DefaultSkipper skipper;
[1137]336
[1408]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;
[1137]344  };
345
[1534]346
[1744]347  /// \brief The undirected graph reader class.
[1421]348  ///
[1909]349  /// The \c UGraphReader class provides the graph input.
[1540]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  ///
[1421]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
[1909]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
[1540]359  /// order of the given commands.
[1421]360  ///
[2100]361  /// The reader object suppose that each not read value does not contain
[1421]362  /// whitespaces, therefore it has some extra possibilities to control how
363  /// it should skip the values when the string representation contains spaces.
364  ///
[1946]365  ///\code
[1909]366  /// UGraphReader<ListUGraph> reader(std::cin, graph);
[1946]367  ///\endcode
[1421]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  ///
[1946]375  ///\code
[1421]376  /// reader.readNodeMap("coords", coords);
377  ///
[1901]378  /// reader.skipNodeMap("description", desc);
[1421]379  ///
380  /// reader.readNodeMap("color", colorMap);
[1946]381  ///\endcode
[1421]382  ///
[1909]383  /// With the \c readUEdgeMap() member function you can give an
384  /// uedge map reading command similar to the NodeMaps.
[1421]385  ///
[1946]386  ///\code
[1909]387  /// reader.readUEdgeMap("capacity", capacityMap);
[1946]388  ///\endcode
[1421]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  ///
[1946]395  ///\code
[1421]396  /// reader.readEdgeMap("flow", flowMap);
[1946]397  ///\endcode
[1421]398  ///
[1909]399  /// With \c readNode() and \c readUEdge() functions you can read
400  /// labeled Nodes and UEdges.
[1421]401  ///
[1946]402  ///\code
[1421]403  /// reader.readNode("source", sourceNode);
404  /// reader.readNode("target", targetNode);
405  ///
[1909]406  /// reader.readUEdge("observed", uEdge);
[1946]407  ///\endcode
[1421]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  ///
[1946]416  ///\code
[1421]417  /// reader.run();
[1946]418  ///\endcode
[1421]419  ///
420  /// \see GraphReader
421  /// \see DefaultReaderTraits
[1909]422  /// \see \ref UGraphWriter
[1421]423  /// \see \ref graph-io-page
424  ///
425  /// \author Balazs Dezso
426  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits>
[1909]427  class UGraphReader {
[1421]428  public:
429   
430    typedef _Graph Graph;
431    typedef typename Graph::Node Node;
432    typedef typename Graph::Edge Edge;
[1909]433    typedef typename Graph::UEdge UEdge;
[1421]434
435    typedef _ReaderTraits ReaderTraits;
436    typedef typename ReaderTraits::Skipper DefaultSkipper;
437
[1909]438    /// \brief Construct a new UGraphReader.
[1421]439    ///
[1909]440    /// Construct a new UGraphReader. It reads into the given graph
[1421]441    /// and it use the given reader as the default skipper.
[1909]442    UGraphReader(std::istream& _is, Graph& _graph,
[1421]443                     const DefaultSkipper& _skipper = DefaultSkipper())
444      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
445        nodeset_reader(*reader, _graph, std::string(), skipper),
[2467]446        uedgeset_reader(*reader, _graph, nodeset_reader,
[1421]447                             std::string(), skipper),
448        node_reader(*reader, nodeset_reader, std::string()),
[2467]449        uedge_reader(*reader, uedgeset_reader, std::string()),
[1421]450        attribute_reader(*reader, std::string()) {}
451
[1909]452    /// \brief Construct a new UGraphReader.
[1421]453    ///
[1909]454    /// Construct a new UGraphReader. It reads into the given graph
[1421]455    /// and it use the given reader as the default skipper.
[1909]456    UGraphReader(const std::string& _filename, Graph& _graph,
[1421]457                     const DefaultSkipper& _skipper = DefaultSkipper())
458      : reader(new LemonReader(_filename)), own_reader(true),
459        skipper(_skipper),
460        nodeset_reader(*reader, _graph, std::string(), skipper),
[2467]461        uedgeset_reader(*reader, _graph, nodeset_reader,
[1421]462                             std::string(), skipper),
463        node_reader(*reader, nodeset_reader, std::string()),
[2467]464        uedge_reader(*reader, uedgeset_reader, std::string()),
[1421]465        attribute_reader(*reader, std::string()) {}
466
[1909]467    /// \brief Construct a new UGraphReader.
[1421]468    ///
[1909]469    /// Construct a new UGraphReader. It reads into the given graph
[1421]470    /// and it use the given reader as the default skipper.
[1909]471    UGraphReader(LemonReader& _reader, Graph& _graph,
[1421]472                     const DefaultSkipper& _skipper = DefaultSkipper())
473      : reader(_reader), own_reader(false), skipper(_skipper),
474        nodeset_reader(*reader, _graph, std::string(), skipper),
[2467]475        uedgeset_reader(*reader, _graph, nodeset_reader,
[1421]476                             std::string(), skipper),
477        node_reader(*reader, nodeset_reader, std::string()),
[2467]478        uedge_reader(*reader, uedgeset_reader, std::string()),
[1421]479        attribute_reader(*reader, std::string()) {}
480
481    /// \brief Destruct the graph reader.
482    ///
483    /// Destruct the graph reader.
[1909]484    ~UGraphReader() {
[1421]485      if (own_reader)
486        delete reader;
487    }
488
[1540]489    /// \brief Give a new node map reading command to the reader.
[1421]490    ///
[1540]491    /// Give a new node map reading command to the reader.
[1421]492    template <typename Map>
[1909]493    UGraphReader& readNodeMap(std::string name, Map& map) {
[1421]494      nodeset_reader.readNodeMap(name, map);
495      return *this;
496    }
497
498    template <typename Map>
[1909]499    UGraphReader& readNodeMap(std::string name, const Map& map) {
[1421]500      nodeset_reader.readNodeMap(name, map);
501      return *this;
502    }
503
[1540]504    /// \brief Give a new node map reading command to the reader.
[1421]505    ///
[1540]506    /// Give a new node map reading command to the reader.
[2386]507    template <typename ItemReader, typename Map>
[1909]508    UGraphReader& readNodeMap(std::string name, Map& map,
[2386]509                              const ItemReader& ir = ItemReader()) {
510      nodeset_reader.readNodeMap(name, map, ir);
[1421]511      return *this;
512    }
513
[2386]514    template <typename ItemReader, typename Map>
[1909]515    UGraphReader& readNodeMap(std::string name, const Map& map,
[2386]516                              const ItemReader& ir = ItemReader()) {
517      nodeset_reader.readNodeMap(name, map, ir);
[1421]518      return *this;
519    }
520
[1540]521    /// \brief Give a new node map skipping command to the reader.
[1421]522    ///
[1540]523    /// Give a new node map skipping command to the reader.
[2386]524    template <typename ItemReader>
[1909]525    UGraphReader& skipNodeMap(std::string name,
[2386]526                              const ItemReader& ir = ItemReader()) {
527      nodeset_reader.skipNodeMap(name, ir);
[1421]528      return *this;
529    }
530
[1540]531    /// \brief Give a new undirected edge map reading command to the reader.
[1421]532    ///
[1540]533    /// Give a new undirected edge map reading command to the reader.
[1421]534    template <typename Map>
[1909]535    UGraphReader& readUEdgeMap(std::string name, Map& map) {
[2467]536      uedgeset_reader.readUEdgeMap(name, map);
[1421]537      return *this;
538    }
539
540    template <typename Map>
[1909]541    UGraphReader& readUEdgeMap(std::string name, const Map& map) {
[2467]542      uedgeset_reader.readUEdgeMap(name, map);
[1421]543      return *this;
544    }
545
546
[1540]547    /// \brief Give a new undirected edge map reading command to the reader.
[1421]548    ///
[1540]549    /// Give a new undirected edge map reading command to the reader.
[2386]550    template <typename ItemReader, typename Map>
[1909]551    UGraphReader& readUEdgeMap(std::string name, Map& map,
[2386]552                               const ItemReader& ir = ItemReader()) {
[2467]553      uedgeset_reader.readUEdgeMap(name, map, ir);
[1421]554      return *this;
555    }
556
[2386]557    template <typename ItemReader, typename Map>
[1909]558    UGraphReader& readUEdgeMap(std::string name, const Map& map,
[2386]559                               const ItemReader& ir = ItemReader()) {
[2467]560      uedgeset_reader.readUEdgeMap(name, map, ir);
[1421]561      return *this;
562    }
563
[1540]564    /// \brief Give a new undirected edge map skipping command to the reader.
[1421]565    ///
[1540]566    /// Give a new undirected edge map skipping command to the reader.
[2386]567    template <typename ItemReader>
[1909]568    UGraphReader& skipUEdgeMap(std::string name,
[2386]569                                       const ItemReader& ir = ItemReader()) {
[2467]570      uedgeset_reader.skipUMap(name, ir);
[1421]571      return *this;
572    }
573
574
[1540]575    /// \brief Give a new edge map reading command to the reader.
[1421]576    ///
[1540]577    /// Give a new edge map reading command to the reader.
[1421]578    template <typename Map>
[1909]579    UGraphReader& readEdgeMap(std::string name, Map& map) {
[2467]580      uedgeset_reader.readEdgeMap(name, map);
[1421]581      return *this;
582    }
583
584    template <typename Map>
[1909]585    UGraphReader& readEdgeMap(std::string name, const Map& map) {
[2467]586      uedgeset_reader.readEdgeMap(name, map);
[1421]587      return *this;
588    }
589
590
[1540]591    /// \brief Give a new edge map reading command to the reader.
[1421]592    ///
[1540]593    /// Give a new edge map reading command to the reader.
[2386]594    template <typename ItemReader, typename Map>
[1909]595    UGraphReader& readEdgeMap(std::string name, Map& map,
[2386]596                              const ItemReader& ir = ItemReader()) {
[2467]597      uedgeset_reader.readEdgeMap(name, map, ir);
[1421]598      return *this;
599    }
600
[2386]601    template <typename ItemReader, typename Map>
[1909]602    UGraphReader& readEdgeMap(std::string name, const Map& map,
[2386]603                              const ItemReader& ir = ItemReader()) {
[2467]604      uedgeset_reader.readEdgeMap(name, map, ir);
[1421]605      return *this;
606    }
607
[1540]608    /// \brief Give a new edge map skipping command to the reader.
[1421]609    ///
[1540]610    /// Give a new edge map skipping command to the reader.
[2386]611    template <typename ItemReader>
[1909]612    UGraphReader& skipEdgeMap(std::string name,
[2386]613                              const ItemReader& ir = ItemReader()) {
[2467]614      uedgeset_reader.skipEdgeMap(name, ir);
[1421]615      return *this;
616    }
617
[1540]618    /// \brief Give a new labeled node reading command to the reader.
[1421]619    ///
[1540]620    /// Give a new labeled node reading command to the reader.
[1909]621    UGraphReader& readNode(std::string name, Node& node) {
[1421]622      node_reader.readNode(name, node);
623      return *this;
624    }
625
[1540]626    /// \brief Give a new labeled edge reading command to the reader.
[1421]627    ///
[1540]628    /// Give a new labeled edge reading command to the reader.
[1909]629    UGraphReader& readEdge(std::string name, Edge& edge) {
[2467]630      uedge_reader.readEdge(name, edge);
[1429]631    }
632
[1540]633    /// \brief Give a new labeled undirected edge reading command to the
634    /// reader.
[1429]635    ///
[1540]636    /// Give a new labeled undirected edge reading command to the reader.
[1909]637    UGraphReader& readUEdge(std::string name, UEdge& edge) {
[2467]638      uedge_reader.readUEdge(name, edge);
[1421]639    }
640
[1540]641    /// \brief Give a new attribute reading command.
[1421]642    ///
[1540]643    ///  Give a new attribute reading command.
[1421]644    template <typename Value>
[1909]645    UGraphReader& readAttribute(std::string name, Value& value) {
[1421]646      attribute_reader.readAttribute(name, value);
647      return *this;
648    }
649   
[1540]650    /// \brief Give a new attribute reading command.
[1421]651    ///
[1540]652    ///  Give a new attribute reading command.
[2386]653    template <typename ItemReader, typename Value>
[1909]654    UGraphReader& readAttribute(std::string name, Value& value,
[2386]655                               const ItemReader& ir = ItemReader()) {
656      attribute_reader.readAttribute(name, value, ir);
[1421]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
[1540]670    /// \brief Executes the reading commands.
[1421]671    ///
[1540]672    /// Executes the reading commands.
[1421]673    void run() {
674      reader->run();
675    }
676
[1901]677
678    /// \brief Returns true if the reader can give back the items by its label.
[1429]679    ///
[2467]680    /// Returns true if the reader can give back the items by its label.
[1901]681    bool isLabelReader() const {
682      return nodeset_reader.isLabelReader() &&
[2467]683        uedgeset_reader.isLabelReader();
[1901]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
[1935]689    /// it. It is possible only if there was read a "label" named node map.
[1901]690    void readLabel(std::istream& is, Node& node) const {
691      return nodeset_reader.readLabel(is, node);
[1429]692    }
693
[1935]694    /// \brief Gives back the edge by its label
[1429]695    ///
[1901]696    /// It reads an label from the stream and gives back which edge belongs to
[1935]697    /// it. It is possible only if there was read a "label" named edge map.
[1901]698    void readLabel(std::istream& is, Edge& edge) const {
[2467]699      return uedgeset_reader.readLabel(is, edge);
[1429]700    }
701
[1901]702    /// \brief Gives back the undirected edge by its label.
[1429]703    ///
[1901]704    /// It reads an label from the stream and gives back which undirected edge
[1935]705    /// belongs to it. It is possible only if there was read a "label" named
[1429]706    /// edge map.
[1909]707    void readLabel(std::istream& is, UEdge& uedge) const {
[2467]708      return uedgeset_reader.readLabel(is, uedge);
[1429]709    }
710   
711
[1421]712  private:
713
714    LemonReader* reader;
715    bool own_reader;
716
717    DefaultSkipper skipper;
718
719    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
[2467]720    UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
[1421]721
722    NodeReader<Graph> node_reader;
[2467]723    UEdgeReader<Graph> uedge_reader;
[1421]724   
725    AttributeReader<ReaderTraits> attribute_reader;
726  };
727
[2502]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
[1421]1191
[1333]1192  /// @}
[1137]1193}
[1214]1194
1195#endif
Note: See TracBrowser for help on using the repository browser.