COIN-OR::LEMON - Graph Library

Changeset 2467:2025a571895e in lemon-0.x


Ignore:
Timestamp:
08/28/07 16:00:42 (12 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3305
Message:

PathNodeIt?

PathWriter/Reader? structures
Distinict MapSet? readers and writers

Location:
lemon
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • lemon/bits/path_dump.h

    r2391 r2467  
    5555      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
    5656      RevEdgeIt(const PredMapPath& _path)
    57         : path(&_path), current(_path.target) {}
     57        : path(&_path), current(_path.target) {
     58        if (path->predMap[current] == INVALID) current = INVALID;
     59      }
    5860
    5961      operator const typename Graph::Edge() const {
     
    127129      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
    128130      RevEdgeIt(const PredMatrixMapPath& _path)
    129         : path(&_path), current(_path.target) {}
     131        : path(&_path), current(_path.target) {
     132        if (path->predMatrixMap(path->source, current) == INVALID)
     133          current = INVALID;
     134      }
    130135
    131136      operator const typename Graph::Edge() const {
  • lemon/graph_reader.h

    r2391 r2467  
    325325    /// it. It is possible only if there was read a "label" named edge map.
    326326    void readLabel(std::istream& is, Edge& edge) const {
    327       return edgeset_reader.readLabel(is, edge);
     327      edgeset_reader.readLabel(is, edge);
    328328    }
    329329
     
    444444      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
    445445        nodeset_reader(*reader, _graph, std::string(), skipper),
    446         u_edgeset_reader(*reader, _graph, nodeset_reader,
     446        uedgeset_reader(*reader, _graph, nodeset_reader,
    447447                             std::string(), skipper),
    448448        node_reader(*reader, nodeset_reader, std::string()),
    449         u_edge_reader(*reader, u_edgeset_reader, std::string()),
     449        uedge_reader(*reader, uedgeset_reader, std::string()),
    450450        attribute_reader(*reader, std::string()) {}
    451451
     
    459459        skipper(_skipper),
    460460        nodeset_reader(*reader, _graph, std::string(), skipper),
    461         u_edgeset_reader(*reader, _graph, nodeset_reader,
     461        uedgeset_reader(*reader, _graph, nodeset_reader,
    462462                             std::string(), skipper),
    463463        node_reader(*reader, nodeset_reader, std::string()),
    464         u_edge_reader(*reader, u_edgeset_reader, std::string()),
     464        uedge_reader(*reader, uedgeset_reader, std::string()),
    465465        attribute_reader(*reader, std::string()) {}
    466466
     
    473473      : reader(_reader), own_reader(false), skipper(_skipper),
    474474        nodeset_reader(*reader, _graph, std::string(), skipper),
    475         u_edgeset_reader(*reader, _graph, nodeset_reader,
     475        uedgeset_reader(*reader, _graph, nodeset_reader,
    476476                             std::string(), skipper),
    477477        node_reader(*reader, nodeset_reader, std::string()),
    478         u_edge_reader(*reader, u_edgeset_reader, std::string()),
     478        uedge_reader(*reader, uedgeset_reader, std::string()),
    479479        attribute_reader(*reader, std::string()) {}
    480480
     
    534534    template <typename Map>
    535535    UGraphReader& readUEdgeMap(std::string name, Map& map) {
    536       u_edgeset_reader.readUEdgeMap(name, map);
     536      uedgeset_reader.readUEdgeMap(name, map);
    537537      return *this;
    538538    }
     
    540540    template <typename Map>
    541541    UGraphReader& readUEdgeMap(std::string name, const Map& map) {
    542       u_edgeset_reader.readUEdgeMap(name, map);
     542      uedgeset_reader.readUEdgeMap(name, map);
    543543      return *this;
    544544    }
     
    551551    UGraphReader& readUEdgeMap(std::string name, Map& map,
    552552                               const ItemReader& ir = ItemReader()) {
    553       u_edgeset_reader.readUEdgeMap(name, map, ir);
     553      uedgeset_reader.readUEdgeMap(name, map, ir);
    554554      return *this;
    555555    }
     
    558558    UGraphReader& readUEdgeMap(std::string name, const Map& map,
    559559                               const ItemReader& ir = ItemReader()) {
    560       u_edgeset_reader.readUEdgeMap(name, map, ir);
     560      uedgeset_reader.readUEdgeMap(name, map, ir);
    561561      return *this;
    562562    }
     
    568568    UGraphReader& skipUEdgeMap(std::string name,
    569569                                       const ItemReader& ir = ItemReader()) {
    570       u_edgeset_reader.skipUMap(name, ir);
     570      uedgeset_reader.skipUMap(name, ir);
    571571      return *this;
    572572    }
     
    578578    template <typename Map>
    579579    UGraphReader& readEdgeMap(std::string name, Map& map) {
    580       u_edgeset_reader.readEdgeMap(name, map);
     580      uedgeset_reader.readEdgeMap(name, map);
    581581      return *this;
    582582    }
     
    584584    template <typename Map>
    585585    UGraphReader& readEdgeMap(std::string name, const Map& map) {
    586       u_edgeset_reader.readEdgeMap(name, map);
     586      uedgeset_reader.readEdgeMap(name, map);
    587587      return *this;
    588588    }
     
    595595    UGraphReader& readEdgeMap(std::string name, Map& map,
    596596                              const ItemReader& ir = ItemReader()) {
    597       u_edgeset_reader.readEdgeMap(name, map, ir);
     597      uedgeset_reader.readEdgeMap(name, map, ir);
    598598      return *this;
    599599    }
     
    602602    UGraphReader& readEdgeMap(std::string name, const Map& map,
    603603                              const ItemReader& ir = ItemReader()) {
    604       u_edgeset_reader.readEdgeMap(name, map, ir);
     604      uedgeset_reader.readEdgeMap(name, map, ir);
    605605      return *this;
    606606    }
     
    612612    UGraphReader& skipEdgeMap(std::string name,
    613613                              const ItemReader& ir = ItemReader()) {
    614       u_edgeset_reader.skipEdgeMap(name, ir);
     614      uedgeset_reader.skipEdgeMap(name, ir);
    615615      return *this;
    616616    }
     
    628628    /// Give a new labeled edge reading command to the reader.
    629629    UGraphReader& readEdge(std::string name, Edge& edge) {
    630       u_edge_reader.readEdge(name, edge);
     630      uedge_reader.readEdge(name, edge);
    631631    }
    632632
     
    636636    /// Give a new labeled undirected edge reading command to the reader.
    637637    UGraphReader& readUEdge(std::string name, UEdge& edge) {
    638       u_edge_reader.readUEdge(name, edge);
     638      uedge_reader.readUEdge(name, edge);
    639639    }
    640640
     
    678678    /// \brief Returns true if the reader can give back the items by its label.
    679679    ///
    680     /// \brief Returns true if the reader can give back the items by its label.
     680    /// Returns true if the reader can give back the items by its label.
    681681    bool isLabelReader() const {
    682682      return nodeset_reader.isLabelReader() &&
    683         u_edgeset_reader.isLabelReader();
     683        uedgeset_reader.isLabelReader();
    684684    }
    685685
     
    697697    /// it. It is possible only if there was read a "label" named edge map.
    698698    void readLabel(std::istream& is, Edge& edge) const {
    699       return u_edgeset_reader.readLabel(is, edge);
     699      return uedgeset_reader.readLabel(is, edge);
    700700    }
    701701
     
    706706    /// edge map.
    707707    void readLabel(std::istream& is, UEdge& uedge) const {
    708       return u_edgeset_reader.readLabel(is, uedge);
     708      return uedgeset_reader.readLabel(is, uedge);
    709709    }
    710710   
     
    718718
    719719    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
    720     UEdgeSetReader<Graph, ReaderTraits> u_edgeset_reader;
     720    UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
    721721
    722722    NodeReader<Graph> node_reader;
    723     UEdgeReader<Graph> u_edge_reader;
     723    UEdgeReader<Graph> uedge_reader;
    724724   
    725725    AttributeReader<ReaderTraits> attribute_reader;
  • lemon/graph_writer.h

    r2391 r2467  
    252252    }
    253253
     254    /// \brief Returns true if the writer can give back the labels by the items.
     255    ///
     256    /// Returns true if the writer can give back the the labels by the items.
     257    bool isLabelWriter() const {
     258      return nodeset_writer.isLabelWriter() &&
     259        edgeset_writer.isLabelWriter();
     260    }
     261
    254262    /// \brief Write the label of the given node.
    255263    ///
    256     /// It writes the label of the given node. If there was written an "label"
     264    /// It writes the label of the given node. If there was written a "label"
    257265    /// named node map then it will write the map value belonging to the node.
    258266    void writeLabel(std::ostream& os, const Node& item) const {
     
    262270    /// \brief Write the label of the given edge.
    263271    ///
    264     /// It writes the label of the given edge. If there was written an "label"
     272    /// It writes the label of the given edge. If there was written a "label"
    265273    /// named edge map then it will write the map value belonging to the edge.
    266274    void writeLabel(std::ostream& os, const Edge& item) const {
    267275      edgeset_writer.writeLabel(os, item);
    268276    }
     277
     278    /// \brief Sorts the given node vector by label.
     279    ///
     280    /// Sorts the given node vector by label. If there was written an
     281    /// "label" named map then the vector will be sorted by the values
     282    /// of this map. Otherwise if the \c forceLabel parameter was true
     283    /// it will be sorted by its id in the graph.
     284    void sortByLabel(std::vector<Node>& nodes) const {
     285      nodeset_writer.sortByLabel(nodes);
     286    }
     287
     288    /// \brief Sorts the given edge vector by label.
     289    ///
     290    /// Sorts the given edge vector by label. If there was written an
     291    /// "label" named map then the vector will be sorted by the values
     292    /// of this map. Otherwise if the \c forceLabel parameter was true
     293    /// it will be sorted by its id in the graph.
     294    void sortByLabel(std::vector<Edge>& edges) const {
     295      edgeset_writer.sortByLabel(edges);
     296    }
    269297
    270298  private:
     
    371399      : writer(new LemonWriter(_os)), own_writer(true),
    372400        nodeset_writer(*writer, _graph, std::string()),
    373         u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
     401        uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
    374402        node_writer(*writer, nodeset_writer, std::string()),
    375         u_edge_writer(*writer, u_edgeset_writer, std::string()),
     403        uedge_writer(*writer, uedgeset_writer, std::string()),
    376404        attribute_writer(*writer, std::string()) {}
    377405
     
    383411      : writer(new LemonWriter(_filename)), own_writer(true),
    384412        nodeset_writer(*writer, _graph, std::string()),
    385         u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
     413        uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
    386414        node_writer(*writer, nodeset_writer, std::string()),
    387         u_edge_writer(*writer, u_edgeset_writer, std::string()),
     415        uedge_writer(*writer, uedgeset_writer, std::string()),
    388416        attribute_writer(*writer, std::string()) {}
    389417
     
    391419    ///
    392420    /// Construct a new UGraphWriter. It writes the given graph
    393     /// to given LemonReader.
     421    /// to given LemonWriter.
    394422    UGraphWriter(LemonWriter& _writer, const Graph& _graph)
    395423      : writer(_writer), own_writer(false),
    396424        nodeset_writer(*writer, _graph, std::string()),
    397         u_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
     425        uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
    398426        node_writer(*writer, nodeset_writer, std::string()),
    399         u_edge_writer(*writer, u_edgeset_writer, std::string()),
     427        uedge_writer(*writer, uedgeset_writer, std::string()),
    400428        attribute_writer(*writer, std::string()) {}
    401429
     
    435463    template <typename Map>
    436464    UGraphWriter& writeEdgeMap(std::string label, const Map& map) {
    437       u_edgeset_writer.writeEdgeMap(label, map);
     465      uedgeset_writer.writeEdgeMap(label, map);
    438466      return *this;
    439467    }
     
    446474    UGraphWriter& writeEdgeMap(std::string label, const Map& map,
    447475                                   const ItemWriter& iw = ItemWriter()) {
    448       u_edgeset_writer.writeEdgeMap(label, map, iw);
     476      uedgeset_writer.writeEdgeMap(label, map, iw);
    449477      return *this;
    450478    }
     
    456484    template <typename Map>
    457485    UGraphWriter& writeUEdgeMap(std::string label, const Map& map) {
    458       u_edgeset_writer.writeUEdgeMap(label, map);
     486      uedgeset_writer.writeUEdgeMap(label, map);
    459487      return *this;
    460488    }
     
    467495    UGraphWriter& writeUEdgeMap(std::string label, const Map& map,
    468496                                        const ItemWriter& iw = ItemWriter()) {
    469       u_edgeset_writer.writeUEdgeMap(label, map, iw);
     497      uedgeset_writer.writeUEdgeMap(label, map, iw);
    470498      return *this;
    471499    }
     
    485513    /// command</i> to the writer.
    486514    UGraphWriter& writeEdge(std::string label, const Edge& edge) {
    487       u_edge_writer.writeEdge(label, edge);
     515      uedge_writer.writeEdge(label, edge);
    488516    }
    489517
     
    494522    /// the writer.
    495523    UGraphWriter& writeUEdge(std::string label, const UEdge& edge) {
    496       u_edge_writer.writeUEdge(label, edge);
     524      uedge_writer.writeUEdge(label, edge);
    497525    }
    498526
     
    535563    }
    536564
     565    /// \brief Returns true if the writer can give back the labels by the items.
     566    ///
     567    /// Returns true if the writer can give back the the labels by the items.
     568    bool isLabelWriter() const {
     569      return nodeset_writer.isLabelWriter() &&
     570        uedgeset_writer.isLabelWriter();
     571    }
     572
    537573    /// \brief Write the label of the given node.
    538574    ///
    539     /// It writes the label of the given node. If there was written an "label"
     575    /// It writes the label of the given node. If there was written a "label"
    540576    /// named node map then it will write the map value belonging to the node.
    541577    void writeLabel(std::ostream& os, const Node& item) const {
     
    545581    /// \brief Write the label of the given edge.
    546582    ///
    547     /// It writes the label of the given edge. If there was written an "label"
     583    /// It writes the label of the given edge. If there was written a "label"
    548584    /// named edge map then it will write the map value belonging to the edge.
    549585    void writeLabel(std::ostream& os, const Edge& item) const {
    550       u_edgeset_writer.writeLabel(os, item);
     586      uedgeset_writer.writeLabel(os, item);
    551587    }
    552588
     
    554590    ///
    555591    /// It writes the label of the given undirected edge. If there was
    556     /// written an "label" named edge map then it will write the map
     592    /// written a "label" named edge map then it will write the map
    557593    /// value belonging to the edge.
    558594    void writeLabel(std::ostream& os, const UEdge& item) const {
    559       u_edgeset_writer.writeLabel(os, item);
     595      uedgeset_writer.writeLabel(os, item);
    560596    }
    561597
     598    /// \brief Sorts the given node vector by label.
     599    ///
     600    /// Sorts the given node vector by label. If there was written an
     601    /// "label" named map then the vector will be sorted by the values
     602    /// of this map. Otherwise if the \c forceLabel parameter was true
     603    /// it will be sorted by its id in the graph.
     604    void sortByLabel(std::vector<Node>& nodes) const {
     605      nodeset_writer.sortByLabel(nodes);
     606    }
     607
     608    /// \brief Sorts the given edge vector by label.
     609    ///
     610    /// Sorts the given edge vector by label. If there was written an
     611    /// "label" named map then the vector will be sorted by the values
     612    /// of this map. Otherwise if the \c forceLabel parameter was true
     613    /// it will be sorted by its id in the graph.
     614    void sortByLabel(std::vector<Edge>& edges) const {
     615      uedgeset_writer.sortByLabel(edges);
     616    }
     617
     618    /// \brief Sorts the given undirected edge vector by label.
     619    ///
     620    /// Sorts the given undirected edge vector by label. If there was
     621    /// written an "label" named map then the vector will be sorted by
     622    /// the values of this map. Otherwise if the \c forceLabel
     623    /// parameter was true it will be sorted by its id in the graph.
     624    void sortByLabel(std::vector<UEdge>& uedges) const {
     625      uedgeset_writer.sortByLabel(uedges);
     626    }
    562627
    563628  private:
     
    567632
    568633    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
    569     UEdgeSetWriter<Graph, WriterTraits> u_edgeset_writer;
     634    UEdgeSetWriter<Graph, WriterTraits> uedgeset_writer;
    570635
    571636    NodeWriter<Graph> node_writer;
    572     UEdgeWriter<Graph> u_edge_writer;
     637    UEdgeWriter<Graph> uedge_writer;
    573638   
    574639    AttributeWriter<WriterTraits> attribute_writer;
  • lemon/lemon_reader.h

    r2464 r2467  
    5959      }
    6060    };
    61 
     61   
    6262    template <typename Item>
    6363    class ItemLabelReader {
     
    256256        } else {
    257257          ErrorMessage msg;
    258           msg << "Invalid label error: " << value;
     258          msg << "Invalid label error";
    259259          throw DataFormatError(msg.message());
    260260        }
     
    290290      virtual Item read(std::istream& is) const {
    291291        Value value;
    292         reader.read(is, value); 
     292        reader.read(is, value);
    293293        typename Inverse::const_iterator it = inverse.find(value);
    294294        if (it != inverse.end()) {
     
    383383      virtual Item read(std::istream& is) const = 0;
    384384      virtual bool isLabelReader() const = 0;
     385      virtual LabelReaderBase<_Item>* clone() const = 0;
    385386    };
    386387
     
    391392      typedef _BoxedLabelReader BoxedLabelReader;
    392393     
    393       const BoxedLabelReader& boxedLabelReader;
    394 
    395       LabelReader(const BoxedLabelReader& _boxedLabelReader)
    396         : boxedLabelReader(_boxedLabelReader) {}
     394      const BoxedLabelReader& labelReader;
     395
     396      LabelReader(const BoxedLabelReader& _labelReader)
     397        : labelReader(_labelReader) {}
    397398
    398399      virtual Item read(std::istream& is) const {
    399400        Item item;
    400         boxedLabelReader.readLabel(is, item);
     401        labelReader.readLabel(is, item);
    401402        return item;
    402403      }
    403404
    404405      virtual bool isLabelReader() const {
    405         return boxedLabelReader.isLabelReader();
     406        return labelReader.isLabelReader();
     407      }
     408     
     409      LabelReader<Item, BoxedLabelReader>* clone() const {
     410        return new LabelReader<Item, BoxedLabelReader>(labelReader);
    406411      }
    407412    };
     
    724729            char buf[2048];
    725730            FilterStreamBuf buffer(*is, line_num);
    726 
    727731            try {
    728732              buffer.pubsetbuf(buf, sizeof(buf));
     
    15121516    /// It reads an id from the stream and gives back which undirected edge
    15131517    /// belongs to it. It is possible only if there was read an "label" named map.
    1514     void readLabel(std::istream& is, UEdge& uEdge) const {
    1515       uEdge = inverter->read(is);
     1518    void readLabel(std::istream& is, UEdge& uedge) const {
     1519      uedge = inverter->read(is);
    15161520    }
    15171521
     
    15251529      char c;
    15261530      is >> c;
    1527       UEdge uEdge = inverter->read(is);
     1531      UEdge uedge = inverter->read(is);
    15281532      if (c == '+') {
    1529         edge = graph.direct(uEdge, true);
     1533        edge = graph.direct(uedge, true);
    15301534      } else if (c == '-') {
    1531         edge = graph.direct(uEdge, false);
     1535        edge = graph.direct(uedge, false);
    15321536      } else {
    15331537        throw DataFormatError("Wrong id format for edge "
     
    18081812      checkConcept<_reader_bits::ItemLabelReader<UEdge>, _LabelReader>();
    18091813      checkConcept<_reader_bits::ItemLabelReader<Edge>, _LabelReader>();
    1810       uEdgeLabelReader.reset(new _reader_bits::
    1811                               LabelReader<UEdge, _LabelReader>(_labelReader));
     1814      uedgeLabelReader.reset(new _reader_bits::
     1815                             LabelReader<UEdge, _LabelReader>(_labelReader));
    18121816      edgeLabelReader.reset(new _reader_bits::
    1813                          LabelReader<Edge, _LabelReader>(_labelReader));
     1817                            LabelReader<Edge, _LabelReader>(_labelReader));
    18141818    }
    18151819
     
    18281832    /// Add an undirected edge reader command for the UEdgeReader.
    18291833    void readUEdge(std::string label, UEdge& item) {
    1830       if (uEdgeReaders.find(label) != uEdgeReaders.end()) {
     1834      if (uedgeReaders.find(label) != uedgeReaders.end()) {
    18311835        ErrorMessage msg;
    18321836        msg << "Multiple read rule for undirected edge: " << label;
    18331837        throw IoParameterError(msg.message());
    18341838      }
    1835       uEdgeReaders.insert(make_pair(label, _reader_bits::
     1839      uedgeReaders.insert(make_pair(label, _reader_bits::
    18361840                                        ItemStore<UEdge>(item)));
    18371841    }
     
    18711875        throw DataFormatError("Cannot find undirected edgeset or label map");
    18721876      }
    1873       if (!uEdgeLabelReader->isLabelReader()) {
     1877      if (!uedgeLabelReader->isLabelReader()) {
    18741878        throw DataFormatError("Cannot find undirected edgeset or label map");
    18751879      }
     
    18801884        ls >> id;
    18811885        {
    1882           typename UEdgeReaders::iterator it = uEdgeReaders.find(id);
    1883           if (it != uEdgeReaders.end()) {
    1884             it->second.read(uEdgeLabelReader->read(ls));
     1886          typename UEdgeReaders::iterator it = uedgeReaders.find(id);
     1887          if (it != uedgeReaders.end()) {
     1888            it->second.read(uedgeLabelReader->read(ls));
    18851889            it->second.touch();
    18861890            continue;
     
    19031907        }
    19041908      }
    1905       for (typename UEdgeReaders::iterator it = uEdgeReaders.begin();
    1906            it != uEdgeReaders.end(); ++it) {
     1909      for (typename UEdgeReaders::iterator it = uedgeReaders.begin();
     1910           it != uedgeReaders.end(); ++it) {
    19071911        if (!it->second.touched()) {
    19081912          ErrorMessage msg;
     
    19141918
    19151919    virtual void missing() {
    1916       if (edgeReaders.empty() && uEdgeReaders.empty()) return;
     1920      if (edgeReaders.empty() && uedgeReaders.empty()) return;
    19171921      ErrorMessage msg;
    19181922      msg << "UEdges section not found in file: @uedges " << name;
     
    19261930    typedef std::map<std::string,
    19271931                     _reader_bits::ItemStore<UEdge> > UEdgeReaders;
    1928     UEdgeReaders uEdgeReaders;
    1929     std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > uEdgeLabelReader;
     1932    UEdgeReaders uedgeReaders;
     1933    std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > uedgeLabelReader;
    19301934
    19311935    typedef std::map<std::string, _reader_bits::ItemStore<Edge> > EdgeReaders;
     
    20522056    typedef std::map<std::string, _reader_bits::ValueReaderBase*> Readers;
    20532057    Readers readers; 
     2058  };
     2059
     2060  /// \ingroup section_io
     2061  /// \brief SectionReader for reading extra node maps.
     2062  ///
     2063  /// The lemon format can store maps in the nodeset sections. This
     2064  /// class let you make distinict section to store maps.  The main
     2065  /// purpose of this class is a logical separation of some maps. The
     2066  /// other useful application could be to store paths in node maps.
     2067  ///
     2068  /// The first line of the section contains the names of the maps
     2069  /// separated with white spaces. Each next line describes an item
     2070  /// in the itemset, and contains in the first column the label of
     2071  /// the item and then the mapped values for each map.
     2072  ///
     2073  /// \relates LemonReader
     2074  template <typename _Graph, typename _Traits = DefaultReaderTraits>
     2075  class NodeMapReader : public LemonReader::SectionReader {
     2076    typedef LemonReader::SectionReader Parent;
     2077  public:
     2078
     2079    typedef _Graph Graph;
     2080    typedef typename Graph::Node Node;
     2081    typedef _Traits Traits;
     2082    typedef typename Traits::Skipper DefaultSkipper;
     2083
     2084    /// \brief Constructor.
     2085    ///
     2086    /// Constructor for NodeMapReader. It creates the NodeMapReader and
     2087    /// attach it into the given LemonReader. The reader will read
     2088    /// the section when the \c section_name and the \c _name are the same.
     2089    template <typename _LabelReader>
     2090    NodeMapReader(LemonReader& _reader,
     2091                  const Graph& _graph,
     2092                  const _LabelReader& _labelReader,
     2093                  const std::string& _name = std::string(),
     2094                  const DefaultSkipper& _skipper = DefaultSkipper())
     2095      : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
     2096      labelReader.reset(new _reader_bits::
     2097                        LabelReader<Node, _LabelReader>(_labelReader));
     2098    }
     2099
     2100
     2101    /// \brief Destructor.
     2102    ///
     2103    /// Destructor for NodeMapReader.
     2104    virtual ~NodeMapReader() {
     2105      for (typename MapReaders::iterator it = readers.begin();
     2106           it != readers.end(); ++it) {
     2107        delete it->second;
     2108      }
     2109    }
     2110
     2111  private:
     2112    NodeMapReader(const NodeMapReader&);
     2113    void operator=(const NodeMapReader&);
     2114 
     2115  public:
     2116
     2117    /// \brief Add a new node map reader command for the reader.
     2118    ///
     2119    /// Add a new node map reader command for the reader.
     2120    template <typename Map>
     2121    NodeMapReader& readNodeMap(std::string label, Map& map) {
     2122      return _readMap<
     2123        typename Traits::template Reader<typename Map::Value>, Map,
     2124        typename _reader_bits::Arg<Map>::Type>(label, map);
     2125    }
     2126
     2127    template <typename Map>
     2128    NodeMapReader& readNodeMap(std::string label, const Map& map) {
     2129      return _readMap<
     2130        typename Traits::template Reader<typename Map::Value>, Map,
     2131        typename _reader_bits::Arg<Map>::Type>(label, map);
     2132    }
     2133
     2134    /// \brief Add a new node map reader command for the reader.
     2135    ///
     2136    /// Add a new node map reader command for the reader.
     2137    template <typename ItemReader, typename Map>
     2138    NodeMapReader& readNodeMap(std::string label, Map& map,
     2139                               const ItemReader& ir = ItemReader()) {
     2140      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2141        (label, map, ir);
     2142    }
     2143
     2144    template <typename ItemReader, typename Map>
     2145    NodeMapReader& readNodeMap(std::string label, const Map& map,
     2146                               const ItemReader& ir = ItemReader()) {
     2147      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2148        (label, map, ir);
     2149    }
     2150
     2151  private:
     2152
     2153    template <typename ItemReader, typename Map, typename MapParameter>
     2154    NodeMapReader& _readMap(std::string label, MapParameter map,
     2155                           const ItemReader& ir = ItemReader()) {
     2156      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
     2157      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
     2158      if (readers.find(label) != readers.end()) {
     2159        ErrorMessage msg;
     2160        msg << "Multiple read rule for map: " << label;
     2161        throw IoParameterError(msg.message());
     2162      }     
     2163      readers.insert(
     2164        make_pair(label, new _reader_bits::
     2165                  MapReader<Node, Map, ItemReader>(map, ir)));
     2166      return *this;
     2167    }
     2168
     2169  public:
     2170
     2171    /// \brief Add a new node map skipper command for the reader.
     2172    ///
     2173    /// Add a new node map skipper command for the reader.
     2174    template <typename ItemReader>
     2175    NodeMapReader& skipNodeMap(std::string label,
     2176                               const ItemReader& ir = ItemReader()) {
     2177      if (readers.find(label) != readers.end()) {
     2178        ErrorMessage msg;
     2179        msg << "Multiple read rule for map: " << label;
     2180        throw IoParameterError(msg.message());
     2181      }
     2182      readers.insert(make_pair(label, new _reader_bits::
     2183                               SkipReader<Node, ItemReader>(ir)));
     2184      return *this;
     2185    }
     2186
     2187  protected:
     2188
     2189    /// \brief Gives back true when the SectionReader can process
     2190    /// the section with the given header line.
     2191    ///
     2192    /// It gives back true when the header line starts with \c \@mapset,
     2193    /// and the header line's name and the mapset's name are the same.
     2194    virtual bool header(const std::string& line) {
     2195      std::istringstream ls(line);
     2196      std::string command;
     2197      std::string id;
     2198      ls >> command >> id;
     2199      return command == "@nodemaps" && name == id;
     2200    }
     2201
     2202    /// \brief Reader function of the section.
     2203    ///
     2204    /// It reads the content of the section.
     2205    virtual void read(std::istream& is) {
     2206      std::vector<_reader_bits::MapReaderBase<Node>* > index;
     2207      std::string line;
     2208
     2209      {
     2210        getline(is, line);
     2211        std::istringstream ls(line);
     2212        std::string id;
     2213        while (ls >> id) {
     2214          typename MapReaders::iterator it = readers.find(id);
     2215          if (it != readers.end()) {
     2216            it->second->touch();
     2217            index.push_back(it->second);
     2218          } else {
     2219            index.push_back(&skipper);
     2220          }
     2221        }
     2222      }
     2223      for (typename MapReaders::iterator it = readers.begin();
     2224           it != readers.end(); ++it) {
     2225        if (!it->second->touched()) {
     2226          ErrorMessage msg;
     2227          msg << "Map not found in file: " << it->first;
     2228          throw IoParameterError(msg.message());
     2229        }
     2230      }
     2231      while (getline(is, line)) {       
     2232        std::istringstream ls(line);
     2233        Node node = labelReader->read(ls);
     2234        for (int i = 0; i < int(index.size()); ++i) {
     2235          index[i]->read(ls, node);
     2236        }
     2237      }
     2238    }
     2239
     2240    virtual void missing() {
     2241      if (readers.empty()) return;
     2242      ErrorMessage msg;
     2243      msg << "NodeMap section not found in file: @nodemaps " << name;
     2244      throw IoParameterError(msg.message());
     2245    }
     2246
     2247  private:
     2248
     2249    typedef std::map<std::string, _reader_bits::MapReaderBase<Node>*> MapReaders;
     2250    MapReaders readers;
     2251   
     2252    const Graph& graph;   
     2253    std::string name;
     2254    _reader_bits::SkipReader<Node, DefaultSkipper> skipper;
     2255    std::auto_ptr<_reader_bits::LabelReaderBase<Node> > labelReader;
     2256
     2257  };
     2258
     2259  /// \ingroup section_io
     2260  /// \brief SectionReader for reading extra edge maps.
     2261  ///
     2262  /// The lemon format can store maps in the edgeset sections. This
     2263  /// class let you make distinict section to store maps.  The main
     2264  /// purpose of this class is a logical separation of some maps. The
     2265  /// other useful application could be to store paths in edge maps.
     2266  ///
     2267  /// The first line of the section contains the names of the maps
     2268  /// separated with white spaces. Each next line describes an item
     2269  /// in the itemset, and contains in the first column the label of
     2270  /// the item and then the mapped values for each map.
     2271  ///
     2272  /// \relates LemonReader
     2273  template <typename _Graph, typename _Traits = DefaultReaderTraits>
     2274  class EdgeMapReader : public LemonReader::SectionReader {
     2275    typedef LemonReader::SectionReader Parent;
     2276  public:
     2277
     2278    typedef _Graph Graph;
     2279    typedef typename Graph::Edge Edge;
     2280    typedef _Traits Traits;
     2281    typedef typename Traits::Skipper DefaultSkipper;
     2282
     2283    /// \brief Constructor.
     2284    ///
     2285    /// Constructor for EdgeMapReader. It creates the EdgeMapReader and
     2286    /// attach it into the given LemonReader. The reader will read
     2287    /// the section when the \c section_name and the \c _name are the same.
     2288    template <typename _LabelReader>
     2289    EdgeMapReader(LemonReader& _reader,
     2290                   const Graph& _graph,
     2291                   const _LabelReader& _labelReader,
     2292                   const std::string& _name = std::string(),
     2293                   const DefaultSkipper& _skipper = DefaultSkipper())
     2294      : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
     2295      labelReader.reset(new _reader_bits::
     2296                        LabelReader<Edge, _LabelReader>(_labelReader));
     2297    }
     2298
     2299
     2300    /// \brief Destructor.
     2301    ///
     2302    /// Destructor for EdgeMapReader.
     2303    virtual ~EdgeMapReader() {
     2304      for (typename MapReaders::iterator it = readers.begin();
     2305           it != readers.end(); ++it) {
     2306        delete it->second;
     2307      }
     2308    }
     2309
     2310  private:
     2311    EdgeMapReader(const EdgeMapReader&);
     2312    void operator=(const EdgeMapReader&);
     2313 
     2314  public:
     2315
     2316    /// \brief Add a new edge map reader command for the reader.
     2317    ///
     2318    /// Add a new edge map reader command for the reader.
     2319    template <typename Map>
     2320    EdgeMapReader& readEdgeMap(std::string label, Map& map) {
     2321      return _readMap<
     2322        typename Traits::template Reader<typename Map::Value>, Map,
     2323        typename _reader_bits::Arg<Map>::Type>(label, map);
     2324    }
     2325
     2326    template <typename Map>
     2327    EdgeMapReader& readEdgeMap(std::string label, const Map& map) {
     2328      return _readMap<
     2329        typename Traits::template Reader<typename Map::Value>, Map,
     2330        typename _reader_bits::Arg<Map>::Type>(label, map);
     2331    }
     2332
     2333    /// \brief Add a new edge map reader command for the reader.
     2334    ///
     2335    /// Add a new edge map reader command for the reader.
     2336    template <typename ItemReader, typename Map>
     2337    EdgeMapReader& readEdgeMap(std::string label, Map& map,
     2338                          const ItemReader& ir = ItemReader()) {
     2339      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2340        (label, map, ir);
     2341    }
     2342
     2343    template <typename ItemReader, typename Map>
     2344    EdgeMapReader& readEdgeMap(std::string label, const Map& map,
     2345                          const ItemReader& ir = ItemReader()) {
     2346      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2347        (label, map, ir);
     2348    }
     2349
     2350  private:
     2351
     2352    template <typename ItemReader, typename Map, typename MapParameter>
     2353    EdgeMapReader& _readMap(std::string label, MapParameter map,
     2354                                const ItemReader& ir = ItemReader()) {
     2355      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
     2356      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
     2357      if (readers.find(label) != readers.end()) {
     2358        ErrorMessage msg;
     2359        msg << "Multiple read rule for map: " << label;
     2360        throw IoParameterError(msg.message());
     2361      }     
     2362      readers.insert(
     2363        make_pair(label, new _reader_bits::
     2364                  MapReader<Edge, Map, ItemReader>(map, ir)));
     2365      return *this;
     2366    }
     2367
     2368  public:
     2369
     2370    /// \brief Add a new edge map skipper command for the reader.
     2371    ///
     2372    /// Add a new edge map skipper command for the reader.
     2373    template <typename ItemReader>
     2374    EdgeMapReader& skipEdgeMap(std::string label,
     2375                          const ItemReader& ir = ItemReader()) {
     2376      if (readers.find(label) != readers.end()) {
     2377        ErrorMessage msg;
     2378        msg << "Multiple read rule for map: " << label;
     2379        throw IoParameterError(msg.message());
     2380      }
     2381      readers.insert(make_pair(label, new _reader_bits::
     2382                               SkipReader<Edge, ItemReader>(ir)));
     2383      return *this;
     2384    }
     2385
     2386  protected:
     2387
     2388    /// \brief Gives back true when the SectionReader can process
     2389    /// the section with the given header line.
     2390    ///
     2391    /// It gives back true when the header line starts with \c \@mapset,
     2392    /// and the header line's name and the mapset's name are the same.
     2393    virtual bool header(const std::string& line) {
     2394      std::istringstream ls(line);
     2395      std::string command;
     2396      std::string id;
     2397      ls >> command >> id;
     2398      return (command == "@edgemaps" || command == "@uedgemaps") && name == id;
     2399    }
     2400
     2401    /// \brief Reader function of the section.
     2402    ///
     2403    /// It reads the content of the section.
     2404    virtual void read(std::istream& is) {
     2405      std::vector<_reader_bits::MapReaderBase<Edge>* > index;
     2406      std::string line;
     2407
     2408      {
     2409        getline(is, line);
     2410        std::istringstream ls(line);
     2411        std::string id;
     2412        while (ls >> id) {
     2413          typename MapReaders::iterator it = readers.find(id);
     2414          if (it != readers.end()) {
     2415            it->second->touch();
     2416            index.push_back(it->second);
     2417          } else {
     2418            index.push_back(&skipper);
     2419          }
     2420        }
     2421      }
     2422      for (typename MapReaders::iterator it = readers.begin();
     2423           it != readers.end(); ++it) {
     2424        if (!it->second->touched()) {
     2425          ErrorMessage msg;
     2426          msg << "Map not found in file: " << it->first;
     2427          throw IoParameterError(msg.message());
     2428        }
     2429      }
     2430      while (getline(is, line)) {       
     2431        std::istringstream ls(line);
     2432        Edge edge = labelReader->read(ls);
     2433        for (int i = 0; i < int(index.size()); ++i) {
     2434          index[i]->read(ls, edge);
     2435        }
     2436      }
     2437    }
     2438
     2439    virtual void missing() {
     2440      if (readers.empty()) return;
     2441      ErrorMessage msg;
     2442      msg << "EdgeMap section not found in file: @edgemaps " << name;
     2443      throw IoParameterError(msg.message());
     2444    }
     2445
     2446  private:
     2447
     2448    typedef std::map<std::string, _reader_bits::MapReaderBase<Edge>*> MapReaders;
     2449    MapReaders readers;
     2450   
     2451    const Graph& graph;   
     2452    std::string name;
     2453    _reader_bits::SkipReader<Edge, DefaultSkipper> skipper;
     2454    std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > labelReader;
     2455
     2456  };
     2457
     2458  /// \ingroup section_io
     2459  /// \brief SectionReader for reading extra undirected edge maps.
     2460  ///
     2461  /// The lemon format can store maps in the uedgeset sections. This
     2462  /// class let you make distinict section to store maps.  The main
     2463  /// purpose of this class is a logical separation of some maps. The
     2464  /// other useful application could be to store paths in undirected
     2465  /// edge maps.
     2466  ///
     2467  /// The first line of the section contains the names of the maps
     2468  /// separated with white spaces. Each next line describes an item
     2469  /// in the itemset, and contains in the first column the label of
     2470  /// the item and then the mapped values for each map.
     2471  ///
     2472  /// \relates LemonReader
     2473  template <typename _Graph, typename _Traits = DefaultReaderTraits>
     2474  class UEdgeMapReader : public LemonReader::SectionReader {
     2475    typedef LemonReader::SectionReader Parent;
     2476  public:
     2477
     2478    typedef _Graph Graph;
     2479    typedef typename Graph::Edge Edge;
     2480    typedef typename Graph::UEdge UEdge;
     2481    typedef _Traits Traits;
     2482    typedef typename Traits::Skipper DefaultSkipper;
     2483
     2484    /// \brief Constructor.
     2485    ///
     2486    /// Constructor for UEdgeMapReader. It creates the UEdgeMapReader and
     2487    /// attach it into the given LemonReader. The reader will read
     2488    /// the section when the \c section_name and the \c _name are the same.
     2489    template <typename _LabelReader>
     2490    UEdgeMapReader(LemonReader& _reader, const Graph& _graph,
     2491                   const _LabelReader& _labelReader,
     2492                   const std::string& _name = std::string(),
     2493                   const DefaultSkipper& _skipper = DefaultSkipper())
     2494      : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) {
     2495      labelReader.reset(new _reader_bits::
     2496                        LabelReader<UEdge, _LabelReader>(_labelReader));
     2497    }
     2498
     2499
     2500    /// \brief Destructor.
     2501    ///
     2502    /// Destructor for UEdgeMapReader.
     2503    virtual ~UEdgeMapReader() {
     2504      for (typename MapReaders::iterator it = readers.begin();
     2505           it != readers.end(); ++it) {
     2506        delete it->second;
     2507      }
     2508    }
     2509
     2510  private:
     2511    UEdgeMapReader(const UEdgeMapReader&);
     2512    void operator=(const UEdgeMapReader&);
     2513 
     2514  public:
     2515
     2516    /// \brief Add a new undirected edge map reader command for the
     2517    /// reader.
     2518    ///
     2519    /// Add a new undirected edge map reader command for the reader.
     2520    template <typename Map>
     2521    UEdgeMapReader& readUEdgeMap(std::string label, Map& map) {
     2522      return _readMap<
     2523        typename Traits::template Reader<typename Map::Value>, Map,
     2524        typename _reader_bits::Arg<Map>::Type>(label, map);
     2525    }
     2526
     2527    template <typename Map>
     2528    UEdgeMapReader& readUEdgeMap(std::string label, const Map& map) {
     2529      return _readMap<
     2530        typename Traits::template Reader<typename Map::Value>, Map,
     2531        typename _reader_bits::Arg<Map>::Type>(label, map);
     2532    }
     2533
     2534    /// \brief Add a new undirected edge map reader command for the
     2535    /// reader.
     2536    ///
     2537    /// Add a new undirected edge map reader command for the reader.
     2538    template <typename ItemReader, typename Map>
     2539    UEdgeMapReader& readUEdgeMap(std::string label, Map& map,
     2540                          const ItemReader& ir = ItemReader()) {
     2541      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2542        (label, map, ir);
     2543    }
     2544
     2545    template <typename ItemReader, typename Map>
     2546    UEdgeMapReader& readUEdgeMap(std::string label, const Map& map,
     2547                          const ItemReader& ir = ItemReader()) {
     2548      return _readMap<ItemReader, Map, typename _reader_bits::Arg<Map>::Type>
     2549        (label, map, ir);
     2550    }
     2551
     2552  private:
     2553
     2554    template <typename ItemReader, typename Map, typename MapParameter>
     2555    UEdgeMapReader& _readMap(std::string label, MapParameter map,
     2556                                const ItemReader& ir = ItemReader()) {
     2557      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
     2558      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
     2559      if (readers.find(label) != readers.end()) {
     2560        ErrorMessage msg;
     2561        msg << "Multiple read rule for map: " << label;
     2562        throw IoParameterError(msg.message());
     2563      }     
     2564      readers.insert(
     2565        make_pair(label, new _reader_bits::
     2566                  MapReader<UEdge, Map, ItemReader>(map, ir)));
     2567      return *this;
     2568    }
     2569
     2570  public:
     2571
     2572    /// \brief Add a new undirected edge map skipper command for the
     2573    /// reader.
     2574    ///
     2575    /// Add a new undirected edge map skipper command for the reader.
     2576    template <typename ItemReader>
     2577    UEdgeMapReader& skipUEdgeMap(std::string label,
     2578                          const ItemReader& ir = ItemReader()) {
     2579      if (readers.find(label) != readers.end()) {
     2580        ErrorMessage msg;
     2581        msg << "Multiple read rule for map: " << label;
     2582        throw IoParameterError(msg.message());
     2583      }
     2584      readers.insert(make_pair(label, new _reader_bits::
     2585                               SkipReader<Edge, ItemReader>(ir)));
     2586      return *this;
     2587    }
     2588
     2589    /// \brief Add a new directed edge map reader command for the reader.
     2590    ///
     2591    /// Add a new directed edge map reader command for the reader.
     2592    template <typename Map>
     2593    UEdgeMapReader& readEdgeMap(std::string label, Map& map) {
     2594      return _readDirMap<
     2595        typename Traits::template Reader<typename Map::Value>, Map,
     2596        typename _reader_bits::Arg<Map>::Type>(label, map);
     2597    }
     2598
     2599    template <typename Map>
     2600    UEdgeMapReader& readEdgeMap(std::string label, const Map& map) {
     2601      return _readDirMap<
     2602        typename Traits::template Reader<typename Map::Value>, Map,
     2603        typename _reader_bits::Arg<Map>::Type>(label, map);
     2604    }
     2605
     2606    /// \brief Add a new directed edge map reader command for the reader.
     2607    ///
     2608    /// Add a new directed edge map reader command for the reader.
     2609    template <typename ItemReader, typename Map>
     2610    UEdgeMapReader& readEdgeMap(std::string label, Map& map,
     2611                                    const ItemReader& ir = ItemReader()) {
     2612      return _readDirMap<ItemReader, Map,
     2613        typename _reader_bits::Arg<Map>::Type>(label, map, ir);
     2614    }
     2615
     2616    template <typename ItemReader, typename Map>
     2617    UEdgeMapReader& readEdgeMap(std::string label, const Map& map,
     2618                                    const ItemReader& ir = ItemReader()) {
     2619      return _readDirMap<ItemReader, Map,
     2620        typename _reader_bits::Arg<Map>::Type>(label, map, ir);
     2621    }
     2622
     2623  private:
     2624
     2625    template <typename ItemReader, typename Map, typename MapParameter>
     2626    UEdgeMapReader& _readDirMap(std::string label, MapParameter map,
     2627                                    const ItemReader& ir = ItemReader()) {
     2628      checkConcept<_reader_bits::ItemReader<typename Map::Value>, ItemReader>();
     2629      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
     2630      readUEdgeMap("+" + label,
     2631                   _reader_bits::forwardComposeMap(graph, map), ir);
     2632      readUEdgeMap("-" + label,
     2633                   _reader_bits::backwardComposeMap(graph, map), ir);
     2634      return *this;     
     2635    }
     2636
     2637  public:
     2638
     2639    /// \brief Add a new directed edge map skipper command for the reader.
     2640    ///
     2641    /// Add a new directed edge map skipper command for the reader.
     2642    template <typename ItemReader>
     2643    UEdgeMapReader& skipEdgeMap(std::string label,
     2644                                const ItemReader& ir = ItemReader()) {
     2645      skipUEdgeMap("+" + label, ir);
     2646      skipUEdgeMap("-" + label, ir);
     2647      return *this;
     2648    }
     2649
     2650  protected:
     2651
     2652    /// \brief Gives back true when the SectionReader can process
     2653    /// the section with the given header line.
     2654    ///
     2655    /// It gives back true when the header line starts with \c \@mapset,
     2656    /// and the header line's name and the mapset's name are the same.
     2657    virtual bool header(const std::string& line) {
     2658      std::istringstream ls(line);
     2659      std::string command;
     2660      std::string id;
     2661      ls >> command >> id;
     2662      return (command == "@edgemaps" || command == "@uedgemaps") && name == id;
     2663    }
     2664
     2665    /// \brief Reader function of the section.
     2666    ///
     2667    /// It reads the content of the section.
     2668    virtual void read(std::istream& is) {
     2669      std::vector<_reader_bits::MapReaderBase<UEdge>* > index;
     2670      std::string line;
     2671
     2672      {
     2673        getline(is, line);
     2674        std::istringstream ls(line);
     2675        std::string id;
     2676        while (ls >> id) {
     2677          typename MapReaders::iterator it = readers.find(id);
     2678          if (it != readers.end()) {
     2679            it->second->touch();
     2680            index.push_back(it->second);
     2681          } else {
     2682            index.push_back(&skipper);
     2683          }
     2684        }
     2685      }
     2686      for (typename MapReaders::iterator it = readers.begin();
     2687           it != readers.end(); ++it) {
     2688        if (!it->second->touched()) {
     2689          ErrorMessage msg;
     2690          msg << "Map not found in file: " << it->first;
     2691          throw IoParameterError(msg.message());
     2692        }
     2693      }
     2694      while (getline(is, line)) {       
     2695        std::istringstream ls(line);
     2696        UEdge uedge = labelReader->read(ls);
     2697        for (int i = 0; i < int(index.size()); ++i) {
     2698          index[i]->read(ls, uedge);
     2699        }
     2700      }
     2701    }
     2702
     2703    virtual void missing() {
     2704      if (readers.empty()) return;
     2705      ErrorMessage msg;
     2706      msg << "UEdgeMap section not found in file: @uedgemaps " << name;
     2707      throw IoParameterError(msg.message());
     2708    }
     2709
     2710  private:
     2711
     2712    const Graph& graph;   
     2713    std::string name;
     2714
     2715    typedef std::map<std::string,
     2716                     _reader_bits::MapReaderBase<UEdge>*> MapReaders;
     2717   
     2718    MapReaders readers;
     2719    _reader_bits::SkipReader<UEdge, DefaultSkipper> skipper;
     2720
     2721    std::auto_ptr<_reader_bits::LabelReaderBase<UEdge> > labelReader;
     2722
    20542723  };
    20552724
  • lemon/lemon_writer.h

    r2414 r2467  
    7272    };
    7373
     74    template <typename UGraph, typename Map>
     75    struct UEdgeComposeLess {
     76      UEdgeComposeLess(const UGraph& _ugraph, const Map& _map)
     77        : ugraph(_ugraph), map(_map), less() {}
     78
     79      bool operator()(const typename UGraph::Edge& p,
     80                      const typename UGraph::Edge& q) const {
     81        return p != q ? less(map[p], map[q]) :
     82          (!ugraph.direction(p) && ugraph.direction(q));
     83      }
     84
     85      const UGraph& ugraph;
     86      const Map& map;
     87      Less<typename Map::Value> less;
     88    };
     89
    7490    template <typename Item>
    7591    class ItemLabelWriter {
     
    124140        : graph(_graph), map(_map) {}
    125141     
    126       Value operator[](const Key& key) {
    127         return map[graph.direct(key, false)];
     142      Value operator[](const Key& key) const {
     143        return map[graph.direct(key, true)];
    128144      }
    129145
     
    148164        : graph(_graph), map(_map) {}
    149165     
    150       Value operator[](const Key& key) {
     166      Value operator[](const Key& key) const {
    151167        return map[graph.direct(key, false)];
    152168      }
     
    200216
    201217      virtual void write(std::ostream& os, const Item& item) const = 0;
    202       virtual void sortByMap(std::vector<Item>&) const = 0;
     218      virtual void sort(std::vector<Item>&) const = 0;
    203219    };
    204220
     
    225241      }
    226242
    227       virtual void sortByMap(std::vector<Item>& items) const {
     243      virtual void sort(std::vector<Item>& items) const {
     244        ComposeLess<Map> less(map);
     245        std::sort(items.begin(), items.end(), less);
     246      }
     247
     248    };
     249
     250    template <typename _UGraph>   
     251    class UEdgeMapWriterBase {
     252    public:
     253      typedef typename _UGraph::Edge Edge;
     254      typedef typename _UGraph::UEdge UEdge;
     255
     256      typedef UEdge Item;
     257
     258      virtual ~UEdgeMapWriterBase() {}
     259
     260      virtual void write(std::ostream& os, const Item& item) const = 0;
     261      virtual void sort(const _UGraph&, std::vector<Edge>&) const = 0;
     262      virtual void sort(std::vector<UEdge>&) const = 0;
     263    };
     264
     265
     266    template <typename _UGraph, typename _Map, typename _Writer>
     267    class UEdgeMapWriter : public UEdgeMapWriterBase<_UGraph> {
     268    public:
     269      typedef _Map Map;
     270      typedef _Writer Writer;
     271      typedef typename Writer::Value Value;
     272
     273      typedef typename _UGraph::Edge Edge;
     274      typedef typename _UGraph::UEdge UEdge;
     275      typedef UEdge Item;
     276     
     277      typename _writer_bits::Ref<Map>::Type map;
     278      Writer writer;
     279
     280      UEdgeMapWriter(const Map& _map, const Writer& _writer)
     281        : map(_map), writer(_writer) {}
     282
     283      virtual ~UEdgeMapWriter() {}
     284
     285      virtual void write(std::ostream& os, const Item& item) const {
     286        Value value = map[item];
     287        writer.write(os, value);
     288      }
     289
     290      virtual void sort(const _UGraph& ugraph, std::vector<Edge>& items) const {
     291        UEdgeComposeLess<_UGraph, Map> less(ugraph, map);
     292        std::sort(items.begin(), items.end(), less);
     293      }
     294
     295      virtual void sort(std::vector<UEdge>& items) const {
    228296        ComposeLess<Map> less(map);
    229297        std::sort(items.begin(), items.end(), less);
     
    263331      virtual ~LabelWriterBase() {}
    264332      virtual void write(std::ostream&, const Item&) const = 0;
     333      virtual void sort(std::vector<Item>&) const = 0;
    265334      virtual bool isLabelWriter() const = 0;
     335      virtual LabelWriterBase* clone() const = 0;
    266336    };
    267337
     
    280350        labelWriter.writeLabel(os, item);
    281351      }
     352      virtual void sort(std::vector<Item>& items) const {
     353        labelWriter.sortByLabel(items);
     354      }
    282355
    283356      virtual bool isLabelWriter() const {
    284357        return labelWriter.isLabelWriter();
     358      }
     359
     360      virtual LabelWriter* clone() const {
     361        return new LabelWriter(labelWriter);
    285362      }
    286363    };
     
    426503  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true
    427504  /// then the label map will be the id in the graph. In addition if the
    428   /// the \c _sortByLabel is true then the writer will write the edges
     505  /// the \c _forceSort is true then the writer will write the edges
    429506  /// sorted by the labels.
    430507  ///
     
    445522    /// parameter is true then the writer will write own label map when
    446523    /// the user does not give "label" named map. In addition if the
    447     /// the \c _sortByLabel is true then the writer will write the edges
     524    /// the \c _forceSort is true then the writer will write the edges
    448525    /// sorted by the labels.
    449526    NodeSetWriter(LemonWriter& _writer, const Graph& _graph,
    450527                  const std::string& _name = std::string(),
    451                   bool _forceLabelMap = true, bool _sortByLabel = true)
     528                  bool _forceLabelMap = true, bool _forceSort = true)
    452529      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
    453         sortByLabel(_sortByLabel), graph(_graph), name(_name) {}
     530        forceSort(_forceSort), graph(_graph), name(_name) {}
    454531
    455532    /// \brief Destructor.
     
    516593        items.push_back(it);
    517594      }
    518       if (sortByLabel) {
     595      if (forceSort) {
    519596        if (labelMap) {
    520           labelMap->sortByMap(items);
     597          labelMap->sort(items);
    521598        } else {
    522599          typedef IdMap<Graph, Node> Map;
     
    551628    ///
    552629    /// Returns true if the nodeset can write the labels of the nodes.
    553     /// It is possible only if an "label" named map was written or the
     630    /// It is possible only if a "label" named map was written or the
    554631    /// \c _forceLabelMap constructor parameter was true.
    555632    bool isLabelWriter() const {
     
    559636    /// \brief Write the label of the given node.
    560637    ///
    561     /// It writes the label of the given node. If there was written an "label"
     638    /// It writes the label of the given node. If there was written a "label"
    562639    /// named map then it will write the map value belongs to the node.
    563640    /// Otherwise if the \c forceLabel parameter was true it will write
     
    571648    }
    572649
     650    /// \brief Sorts the given node vector by label.
     651    ///
     652    /// Sorts the given node vector by label. If there was written an
     653    /// "label" named map then the vector will be sorted by the values
     654    /// of this map. Otherwise if the \c forceLabel parameter was true
     655    /// it will be sorted by its id in the graph.
     656    void sortByLabel(std::vector<Node>& nodes) const {
     657      if (labelMap) {
     658        labelMap->sort(nodes);
     659      } else {
     660        typedef IdMap<Graph, Node> Map;
     661        Map map(graph);
     662        _writer_bits::ComposeLess<Map> less(map);
     663        std::sort(nodes.begin(), nodes.end(), less);
     664      }
     665    }
     666
    573667  private:
    574668
     
    579673    _writer_bits::MapWriterBase<Node>* labelMap;
    580674    bool forceLabelMap;
    581     bool sortByLabel;
     675    bool forceSort;
    582676   
    583677    const Graph& graph;   
     
    603697  /// label. Otherwise if the \c _forceLabelMap constructor parameter is true
    604698  /// then the label map will be the id in the graph. In addition if the
    605   /// the \c _sortByLabel is true then the writer will write the edges
     699  /// the \c _forceSort is true then the writer will write the edges
    606700  /// sorted by the labels.
    607701  ///
     
    628722    /// parameter is true then the writer will write own label map if
    629723    /// the user does not give "label" named map. In addition if the
    630     /// the \c _sortByLabel is true then the writer will write the
     724    /// the \c _forceSort is true then the writer will write the
    631725    /// edges sorted by the labels.
    632726    template <typename NodeLabelWriter>
     
    634728                  const NodeLabelWriter& _nodeLabelWriter,
    635729                  const std::string& _name = std::string(),
    636                   bool _forceLabelMap = true, bool _sortByLabel = true)
     730                  bool _forceLabelMap = true, bool _forceSort = true)
    637731      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
    638         sortByLabel(_sortByLabel), graph(_graph), name(_name) {
     732        forceSort(_forceSort), graph(_graph), name(_name) {
    639733      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
    640734      nodeLabelWriter.reset(new _writer_bits::
     
    708802        items.push_back(it);
    709803      }
    710       if (sortByLabel) {
     804      if (forceSort) {
    711805        if (labelMap) {
    712           labelMap->sortByMap(items);
     806          labelMap->sort(items);
    713807        } else {
    714808          typedef IdMap<Graph, Edge> Map;
     
    748842    ///
    749843    /// Returns true if the edgeset can write the labels of the edges.
    750     /// It is possible only if an "label" named map was written or the
     844    /// It is possible only if a "label" named map was written or the
    751845    /// \c _forceLabelMap constructor parameter was true.
    752846    bool isLabelWriter() const {
     
    756850    /// \brief Write the label of the given edge.
    757851    ///
    758     /// It writes the label of the given edge. If there was written an "label"
     852    /// It writes the label of the given edge. If there was written a "label"
    759853    /// named map then it will write the map value belongs to the edge.
    760854    /// Otherwise if the \c forceLabel parameter was true it will write
     
    768862    }
    769863
     864    /// \brief Sorts the given edge vector by label.
     865    ///
     866    /// Sorts the given edge vector by label. If there was written an
     867    /// "label" named map then the vector will be sorted by the values
     868    /// of this map. Otherwise if the \c forceLabel parameter was true
     869    /// it will be sorted by its id in the graph.
     870    void sortByLabel(std::vector<Edge>& edges) const {
     871      if (labelMap) {
     872        labelMap->sort(edges);
     873      } else {
     874        typedef IdMap<Graph, Edge> Map;
     875        Map map(graph);
     876        _writer_bits::ComposeLess<Map> less(map);
     877        std::sort(edges.begin(), edges.end(), less);
     878      }
     879    }
     880
    770881  private:
    771882
     
    776887    _writer_bits::MapWriterBase<Edge>* labelMap;
    777888    bool forceLabelMap;
    778     bool sortByLabel;
     889    bool forceSort;
    779890   
    780891    const Graph& graph;   
     
    808919  /// _forceLabelMap constructor parameter is true then the label map
    809920  /// will be the id in the graph.  In addition if the the \c
    810   /// _sortByLabel is true then the writer will write the edges sorted
     921  /// _forceSort is true then the writer will write the edges sorted
    811922  /// by the labels.
    812923  ///
     
    834945    /// parameter is true then the writer will write own label map if
    835946    /// the user does not give "label" named map. In addition if the
    836     /// the \c _sortByLabel is true then the writer will write the
     947    /// the \c _forceSort is true then the writer will write the
    837948    /// edges sorted by the labels.
    838949    template <typename NodeLabelWriter>
     
    840951                       const NodeLabelWriter& _nodeLabelWriter,
    841952                       const std::string& _name = std::string(),
    842                        bool _forceLabelMap = true, bool _sortByLabel = true)
     953                       bool _forceLabelMap = true, bool _forceSort = true)
    843954      : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
    844         sortByLabel(_sortByLabel), graph(_graph), name(_name) {
     955        forceSort(_forceSort), graph(_graph), name(_name) {
    845956      checkConcept<_writer_bits::ItemLabelWriter<Node>, NodeLabelWriter>();
    846957      nodeLabelWriter.reset(new _writer_bits::
    847                          LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
     958        LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
    848959    }
    849960
     
    883994      writers.push_back(
    884995        make_pair(label, new _writer_bits::
    885                   MapWriter<UEdge, Map, ItemWriter>(map, iw)));
     996                  UEdgeMapWriter<Graph, Map, ItemWriter>(map, iw)));
    886997      return *this;
    887998    }
     
    9041015      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
    9051016      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
    906       writeUEdgeMap("+" + name,
     1017      writeUEdgeMap("+" + label,
    9071018                    _writer_bits::forwardComposeMap(graph, map), iw);
    908       writeUEdgeMap("-" + name,
     1019      writeUEdgeMap("-" + label,
    9091020                    _writer_bits::backwardComposeMap(graph, map), iw);
    9101021      return *this;
     
    9381049        items.push_back(it);
    9391050      }
    940       if (sortByLabel) {
     1051      if (forceSort) {
    9411052        if (labelMap) {
    942           labelMap->sortByMap(items);
     1053          labelMap->sort(items);
    9431054        } else {
    9441055          typedef IdMap<Graph, UEdge> Map;
     
    9791090    ///
    9801091    /// Returns true if the undirected edgeset can write the labels of the
    981     /// undirected edges. It is possible only if an "label" named map was
     1092    /// undirected edges. It is possible only if a "label" named map was
    9821093    /// written or the \c _forceLabelMap constructor parameter was true.
    9831094    bool isLabelWriter() const {
     
    9881099    ///
    9891100    /// It writes the label of the given undirected edge. If there was written
    990     /// an "label" named map then it will write the map value belongs to the
     1101    /// a "label" named map then it will write the map value belongs to the
    9911102    /// undirected edge. Otherwise if the \c forceLabel parameter was true it
    9921103    /// will write its id in the graph.
     
    10021113    ///
    10031114    /// It writes the label of the given edge. If there was written
    1004     /// an "label" named map then it will write the map value belongs to the
     1115    /// a "label" named map then it will write the map value belongs to the
    10051116    /// edge. Otherwise if the \c forceLabel parameter was true it
    10061117    /// will write its id in the graph. If the edge is forward map
     
    10081119    void writeLabel(std::ostream& os, const Edge& item) const {
    10091120      if (graph.direction(item)) {
    1010         os << "+ ";
     1121        os << "+";
    10111122      } else {
    1012         os << "- ";
     1123        os << "-";
    10131124      }
    10141125      if (forceLabelMap) {
    1015         os << graph.id(item);
     1126        os << graph.id(static_cast<const UEdge&>(item));
    10161127      } else {
    10171128        labelMap->write(os, item);
     
    10191130    }
    10201131
     1132    /// \brief Sorts the given undirected edge vector by label.
     1133    ///
     1134    /// Sorts the given undirected edge vector by label. If there was
     1135    /// written a "label" named map then the vector will be sorted by
     1136    /// the values of this map. Otherwise if the \c forceLabel
     1137    /// parameter was true it will be sorted by its id in the graph.
     1138    void sortByLabel(std::vector<UEdge>& uedges) const {
     1139      if (labelMap) {
     1140        labelMap->sort(uedges);
     1141      } else {
     1142        typedef IdMap<Graph, UEdge> Map;
     1143        Map map(graph);
     1144        _writer_bits::ComposeLess<Map> less(map);
     1145        std::sort(uedges.begin(), uedges.end(), less);
     1146      }
     1147    }
     1148
     1149    /// \brief Sorts the given edge vector by label.
     1150    ///
     1151    /// Sorts the given edge vector by label. If there was written a
     1152    /// "label" named map then the vector will be sorted by the values
     1153    /// of this map. Otherwise if the \c forceLabel parameter was true
     1154    /// it will be sorted by its id in the graph.
     1155    void sortByLabel(std::vector<Edge>& edges) const {
     1156      if (labelMap) {
     1157        labelMap->sort(graph, edges);
     1158      } else {
     1159        typedef IdMap<Graph, Edge> Map;
     1160        Map map(graph);
     1161        _writer_bits::ComposeLess<Map> less(map);
     1162        std::sort(edges.begin(), edges.end(), less);
     1163      }
     1164    }
     1165
    10211166  private:
    10221167
    10231168    typedef std::vector<std::pair<std::string, _writer_bits::
    1024                                   MapWriterBase<UEdge>*> > MapWriters;
     1169                                  UEdgeMapWriterBase<Graph>*> > MapWriters;
    10251170    MapWriters writers;
    10261171
    1027     _writer_bits::MapWriterBase<UEdge>* labelMap;
     1172    _writer_bits::UEdgeMapWriterBase<Graph>* labelMap;
    10281173    bool forceLabelMap;
    1029     bool sortByLabel;
     1174    bool forceSort;
    10301175   
    10311176    const Graph& graph;   
     
    12061351    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter;
    12071352  };
     1353
    12081354
    12091355  /// \ingroup section_io
     
    13221468
    13231469  /// \ingroup section_io
     1470  /// \brief SectionWriter for writing extra node maps.
     1471  ///
     1472  /// The lemon format can store maps in the nodeset. This class let
     1473  /// you make distinict section to store maps. The main purpose of
     1474  /// this class is a logical separation of some maps. The other
     1475  /// useful application could be to store paths in node maps.
     1476  ///
     1477  /// The first line of the section contains the names of the maps
     1478  /// separated with white spaces. Each next line describes an item
     1479  /// in the itemset, and contains in the first column the label of
     1480  /// the item and then the mapped values for each map.
     1481  ///
     1482  /// \relates LemonWriter
     1483  template <typename _Graph, typename _Traits = DefaultWriterTraits>
     1484  class NodeMapWriter : public LemonWriter::SectionWriter {
     1485    typedef LemonWriter::SectionWriter Parent;
     1486  public:
     1487
     1488    typedef _Graph Graph;
     1489    typedef _Traits Traits;
     1490    typedef typename Graph::Node Node;
     1491
     1492    /// \brief Constructor.
     1493    ///
     1494    /// Constructor for NodeMapWriter. It creates the NodeMapWriter and
     1495    /// attach it into the given LemonWriter. If the the
     1496    /// \c _forceSort is true then the writer will write the edges
     1497    /// sorted by the labels.
     1498    template <typename _LabelWriter>
     1499    NodeMapWriter(LemonWriter& _writer, const Graph& _graph,
     1500                 const _LabelWriter& _labelWriter,
     1501                 const std::string& _name = std::string(),
     1502                 bool _forceSort = true)
     1503      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
     1504      checkConcept<_writer_bits::ItemLabelWriter<Node>, _LabelWriter>();
     1505      labelWriter.reset(new _writer_bits::LabelWriter<Node,
     1506                        _LabelWriter>(_labelWriter));
     1507    }
     1508
     1509    /// \brief Destructor.
     1510    ///
     1511    /// Destructor for NodeMapWriter.
     1512    virtual ~NodeMapWriter() {
     1513      typename MapWriters::iterator it;
     1514      for (it = writers.begin(); it != writers.end(); ++it) {
     1515        delete it->second;
     1516      }
     1517    }
     1518
     1519  private:
     1520    NodeMapWriter(const NodeMapWriter&);
     1521    void operator=(const NodeMapWriter&);
     1522 
     1523  public:
     1524
     1525    /// \brief Add a new node map writer command for the writer.
     1526    ///
     1527    /// Add a new node map writer command for the writer.
     1528    template <typename Map>
     1529    NodeMapWriter& writeNodeMap(std::string label, const Map& map) {
     1530      return writeNodeMap<typename Traits::
     1531        template Writer<typename Map::Value>, Map>(label, map);
     1532    }
     1533
     1534    /// \brief Add a new node map writer command for the writer.
     1535    ///
     1536    /// Add a new node map writer command for the writer.
     1537    template <typename ItemWriter, typename Map>
     1538    NodeMapWriter& writeNodeMap(std::string label, const Map& map,
     1539                           const ItemWriter& iw = ItemWriter()) {
     1540      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
     1541      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
     1542      writers.push_back(
     1543        make_pair(label, new _writer_bits::
     1544                  MapWriter<Node, Map, ItemWriter>(map, iw)));
     1545      return *this;
     1546    }
     1547
     1548  protected:
     1549
     1550    /// \brief The header of the section.
     1551    ///
     1552    /// It gives back the header of the section.
     1553    virtual std::string header() {
     1554      return "@nodemaps " + name;
     1555    }
     1556
     1557    /// \brief  Writer function of the section.
     1558    ///
     1559    /// Write the content of the section.
     1560    virtual void write(std::ostream& os) {
     1561      std::vector<Node> nodes;
     1562      for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
     1563        nodes.push_back(it);
     1564      }
     1565      if (forceSort) {
     1566        labelWriter->sort(nodes);
     1567      }
     1568      os << '\t';
     1569      for (int i = 0; i < int(writers.size()); ++i) {
     1570        os << writers[i].first << '\t';
     1571      }
     1572      os << std::endl;
     1573      for (typename std::vector<Node>::iterator it = nodes.begin();
     1574           it != nodes.end(); ++it) {
     1575
     1576        labelWriter->write(os, *it); os << '\t';
     1577        for (int i = 0; i < int(writers.size()); ++i) {
     1578          writers[i].second->write(os, *it);
     1579          os << '\t';
     1580        }
     1581        os << std::endl;
     1582      }
     1583    }
     1584
     1585
     1586  private:
     1587
     1588    typedef std::vector<std::pair<std::string, _writer_bits::
     1589                                  MapWriterBase<Node>*> > MapWriters;
     1590    MapWriters writers;
     1591
     1592    _writer_bits::MapWriterBase<Node>* labelMap;
     1593
     1594    const Graph& graph;   
     1595    std::string name;
     1596    bool forceSort;
     1597
     1598    std::auto_ptr<_writer_bits::LabelWriterBase<Node> > labelWriter;
     1599  };
     1600
     1601  /// \ingroup section_io
     1602  /// \brief SectionWriter for writing extra edge maps.
     1603  ///
     1604  /// The lemon format can store maps in the edgeset. This class let
     1605  /// you make distinict section to store maps. The main purpose of
     1606  /// this class is a logical separation of some maps. The other
     1607  /// useful application could be to store paths in edge maps.
     1608  ///
     1609  /// The first line of the section contains the names of the maps
     1610  /// separated with white spaces. Each next line describes an item
     1611  /// in the itemset, and contains in the first column the label of
     1612  /// the item and then the mapped values for each map.
     1613  ///
     1614  /// \relates LemonWriter
     1615  template <typename _Graph, typename _Traits = DefaultWriterTraits>
     1616  class EdgeMapWriter : public LemonWriter::SectionWriter {
     1617    typedef LemonWriter::SectionWriter Parent;
     1618  public:
     1619
     1620    typedef _Graph Graph;
     1621    typedef _Traits Traits;
     1622    typedef typename Graph::Edge Edge;
     1623
     1624    /// \brief Constructor.
     1625    ///
     1626    /// Constructor for EdgeMapWriter. It creates the EdgeMapWriter and
     1627    /// attach it into the given LemonWriter. If the the
     1628    /// \c _forceSort is true then the writer will write the edges
     1629    /// sorted by the labels.
     1630    template <typename _LabelWriter>
     1631    EdgeMapWriter(LemonWriter& _writer, const Graph& _graph,
     1632                 const _LabelWriter& _labelWriter,
     1633                 const std::string& _name = std::string(),
     1634                 bool _forceSort = true)
     1635      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
     1636      checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
     1637      labelWriter.reset(new _writer_bits::LabelWriter<Edge,
     1638                        _LabelWriter>(_labelWriter));
     1639    }
     1640
     1641    /// \brief Destructor.
     1642    ///
     1643    /// Destructor for EdgeMapWriter.
     1644    virtual ~EdgeMapWriter() {
     1645      typename MapWriters::iterator it;
     1646      for (it = writers.begin(); it != writers.end(); ++it) {
     1647        delete it->second;
     1648      }
     1649    }
     1650
     1651  private:
     1652    EdgeMapWriter(const EdgeMapWriter&);
     1653    void operator=(const EdgeMapWriter&);
     1654 
     1655  public:
     1656
     1657    /// \brief Add a new edge map writer command for the writer.
     1658    ///
     1659    /// Add a new edge map writer command for the writer.
     1660    template <typename Map>
     1661    EdgeMapWriter& writeEdgeMap(std::string label, const Map& map) {
     1662      return writeEdgeMap<typename Traits::
     1663        template Writer<typename Map::Value>, Map>(label, map);
     1664    }
     1665
     1666    /// \brief Add a new edge map writer command for the writer.
     1667    ///
     1668    /// Add a new edge map writer command for the writer.
     1669    template <typename ItemWriter, typename Map>
     1670    EdgeMapWriter& writeEdgeMap(std::string label, const Map& map,
     1671                                const ItemWriter& iw = ItemWriter()) {
     1672      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
     1673      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
     1674      writers.push_back(
     1675        make_pair(label, new _writer_bits::
     1676                  MapWriter<Edge, Map, ItemWriter>(map, iw)));
     1677      return *this;
     1678    }
     1679
     1680  protected:
     1681
     1682    /// \brief The header of the section.
     1683    ///
     1684    /// It gives back the header of the section.
     1685    virtual std::string header() {
     1686      return "@edgemaps " + name;
     1687    }
     1688
     1689    /// \brief  Writer function of the section.
     1690    ///
     1691    /// Write the content of the section.
     1692    virtual void write(std::ostream& os) {
     1693      std::vector<Edge> edges;
     1694      for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) {
     1695        edges.push_back(it);
     1696      }
     1697      if (forceSort) {
     1698        labelWriter->sort(edges);
     1699      }
     1700      os << '\t';
     1701      for (int i = 0; i < int(writers.size()); ++i) {
     1702        os << writers[i].first << '\t';
     1703      }
     1704      os << std::endl;
     1705      for (typename std::vector<Edge>::iterator it = edges.begin();
     1706           it != edges.end(); ++it) {
     1707
     1708        labelWriter->write(os, *it); os << '\t';
     1709        for (int i = 0; i < int(writers.size()); ++i) {
     1710          writers[i].second->write(os, *it);
     1711          os << '\t';
     1712        }
     1713        os << std::endl;
     1714      }
     1715    }
     1716
     1717
     1718  private:
     1719
     1720    typedef std::vector<std::pair<std::string, _writer_bits::
     1721                                  MapWriterBase<Edge>*> > MapWriters;
     1722    MapWriters writers;
     1723
     1724    _writer_bits::MapWriterBase<Edge>* labelMap;
     1725
     1726    const Graph& graph;   
     1727    std::string name;
     1728    bool forceSort;
     1729
     1730    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > labelWriter;
     1731  };
     1732
     1733  /// \ingroup section_io
     1734  /// \brief SectionWriter for writing extra undirected edge maps.
     1735  ///
     1736  /// The lemon format can store maps in the uedgeset. This class let
     1737  /// you make distinict section to store maps. The main purpose of
     1738  /// this class is a logical separation of some maps. The other
     1739  /// useful application could be to store paths in undirected edge
     1740  /// maps.
     1741  ///
     1742  /// The first line of the section contains the names of the maps
     1743  /// separated with white spaces. Each next line describes an item
     1744  /// in the itemset, and contains in the first column the label of
     1745  /// the item and then the mapped values for each map.
     1746  ///
     1747  /// \relates LemonWriter
     1748  template <typename _Graph, typename _Traits = DefaultWriterTraits>
     1749  class UEdgeMapWriter : public LemonWriter::SectionWriter {
     1750    typedef LemonWriter::SectionWriter Parent;
     1751  public:
     1752
     1753    typedef _Graph Graph;
     1754    typedef _Traits Traits;
     1755    typedef typename Graph::UEdge UEdge;
     1756    typedef typename Graph::Edge Edge;
     1757
     1758    /// \brief Constructor.
     1759    ///
     1760    /// Constructor for UEdgeMapWriter. It creates the UEdgeMapWriter and
     1761    /// attach it into the given LemonWriter. If the the
     1762    /// \c _forceSort is true then the writer will write the uedges
     1763    /// sorted by the labels.
     1764    template <typename _LabelWriter>
     1765    UEdgeMapWriter(LemonWriter& _writer, const Graph& _graph,
     1766                 const _LabelWriter& _labelWriter,
     1767                 const std::string& _name = std::string(),
     1768                 bool _forceSort = true)
     1769      : Parent(_writer), graph(_graph), name(_name), forceSort(_forceSort) {
     1770      checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>();
     1771      labelWriter.reset(new _writer_bits::LabelWriter<UEdge,
     1772                            _LabelWriter>(_labelWriter));
     1773    }
     1774
     1775    /// \brief Destructor.
     1776    ///
     1777    /// Destructor for UEdgeMapWriter.
     1778    virtual ~UEdgeMapWriter() {
     1779      typename MapWriters::iterator it;
     1780      for (it = writers.begin(); it != writers.end(); ++it) {
     1781        delete it->second;
     1782      }
     1783    }
     1784
     1785  private:
     1786    UEdgeMapWriter(const UEdgeMapWriter&);
     1787    void operator=(const UEdgeMapWriter&);
     1788 
     1789  public:
     1790
     1791    /// \brief Add a new undirected edge map writer command for the writer.
     1792    ///
     1793    /// Add a new undirected edge map writer command for the writer.
     1794    template <typename Map>
     1795    UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map) {
     1796      return writeUEdgeMap<typename Traits::
     1797        template Writer<typename Map::Value>, Map>(label, map);
     1798    }
     1799
     1800    /// \brief Add a new undirected edge map writer command for the writer.
     1801    ///
     1802    /// Add a new undirected edge map writer command for the writer.
     1803    template <typename ItemWriter, typename Map>
     1804    UEdgeMapWriter& writeUEdgeMap(std::string label, const Map& map,
     1805                           const ItemWriter& iw = ItemWriter()) {
     1806      checkConcept<concepts::ReadMap<UEdge, typename Map::Value>, Map>();
     1807      checkConcept<_writer_bits::ItemWriter<typename Map::Value>,ItemWriter>();
     1808      writers.push_back(
     1809        make_pair(label, new _writer_bits::
     1810                  MapWriter<UEdge, Map, ItemWriter>(map, iw)));
     1811      return *this;
     1812    }
     1813
     1814    /// \brief Add a new directed edge map writer command for the writer.
     1815    ///
     1816    /// Add a new directed map writer command for the writer.
     1817    template <typename Map>
     1818    UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map) {
     1819      return writeEdgeMap<typename Traits::
     1820        template Writer<typename Map::Value>, Map>(label, map);
     1821    }
     1822
     1823    /// \brief Add a new directed map writer command for the writer.
     1824    ///
     1825    /// Add a new directed map writer command for the writer.
     1826    template <typename ItemWriter, typename Map>
     1827    UEdgeMapWriter& writeEdgeMap(std::string label, const Map& map,
     1828                                 const ItemWriter& iw = ItemWriter()) {
     1829      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
     1830      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, ItemWriter>();
     1831      writeUEdgeMap("+" + label,
     1832                    _writer_bits::forwardComposeMap(graph, map), iw);
     1833      writeUEdgeMap("-" + label,
     1834                    _writer_bits::backwardComposeMap(graph, map), iw);
     1835      return *this;
     1836    }
     1837
     1838  protected:
     1839
     1840    /// \brief The header of the section.
     1841    ///
     1842    /// It gives back the header of the section.
     1843    virtual std::string header() {
     1844      return "@uedgemaps " + name;
     1845    }
     1846
     1847    /// \brief  Writer function of the section.
     1848    ///
     1849    /// Write the content of the section.
     1850    virtual void write(std::ostream& os) {
     1851      std::vector<UEdge> uedges;
     1852      for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) {
     1853        uedges.push_back(it);
     1854      }
     1855      if (forceSort) {
     1856        labelWriter->sort(uedges);
     1857      }
     1858      os << '\t';
     1859      for (int i = 0; i < int(writers.size()); ++i) {
     1860        os << writers[i].first << '\t';
     1861      }
     1862      os << std::endl;
     1863      for (typename std::vector<UEdge>::iterator it = uedges.begin();
     1864           it != uedges.end(); ++it) {
     1865
     1866        labelWriter->write(os, *it); os << '\t';
     1867        for (int i = 0; i < int(writers.size()); ++i) {
     1868          writers[i].second->write(os, *it);
     1869          os << '\t';
     1870        }
     1871        os << std::endl;
     1872      }
     1873    }
     1874
     1875
     1876  private:
     1877
     1878    typedef std::vector<std::pair<std::string, _writer_bits::
     1879                                  MapWriterBase<UEdge>*> > MapWriters;
     1880    MapWriters writers;
     1881
     1882    _writer_bits::MapWriterBase<UEdge>* labelMap;
     1883
     1884    const Graph& graph;   
     1885    std::string name;
     1886    bool forceSort;
     1887
     1888    std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > labelWriter;
     1889  };
     1890
     1891
     1892  /// \ingroup section_io
    13241893  /// \brief SectionWriter for attributes.
    13251894  ///
  • lemon/path_utils.h

    r2391 r2467  
    2626
    2727#include <lemon/concepts/path.h>
     28#include <lemon/lemon_reader.h>
     29#include <lemon/lemon_writer.h>
    2830
    2931namespace lemon {
     
    133135    return graph.target(path.back());
    134136  }
     137
     138  /// \brief Class which helps to iterate the nodes of a path
     139  ///
     140  /// In a sense, the path can be treated as a list of edges. The
     141  /// lemon path type stores just this list. As a consequence it
     142  /// cannot enumerate the nodes in the path and the zero length paths
     143  /// cannot store the node.
     144  ///
     145  /// This class implements the node iterator of a path structure. To
     146  /// provide this feature, the underlying graph should be given to
     147  /// the constructor of the iterator.
     148  template <typename Path>
     149  class PathNodeIt {
     150  private:
     151    const typename Path::Graph *_graph;
     152    typename Path::EdgeIt _it;
     153    typename Path::Graph::Node _nd;
     154
     155  public:
     156
     157    typedef typename Path::Graph Graph;
     158    typedef typename Graph::Node Node;
     159   
     160    /// Default constructor
     161    PathNodeIt() {}
     162    /// Invalid constructor
     163    PathNodeIt(Invalid)
     164      : _graph(0), _it(INVALID), _nd(INVALID) {}
     165    /// Constructor
     166    PathNodeIt(const Graph& graph, const Path& path)
     167      : _graph(&graph), _it(path) {
     168      _nd = (_it != INVALID ? _graph->source(_it) : INVALID);
     169    }
     170    /// Constructor
     171    PathNodeIt(const Graph& graph, const Path& path, const Node& src)
     172      : _graph(&graph), _it(path), _nd(src) {}
     173
     174    ///Conversion to Graph::Node
     175    operator Node() const {
     176      return _nd;
     177    }
     178
     179    /// Next node
     180    PathNodeIt& operator++() {
     181      if (_it == INVALID) _nd = INVALID;
     182      else {
     183        _nd = _graph->target(_it);
     184        ++_it;
     185      }
     186      return *this;
     187    }
     188
     189    /// Comparison operator
     190    bool operator==(const PathNodeIt& n) const {
     191      return _it == n._it && _nd == n._nd;
     192    }
     193    /// Comparison operator
     194    bool operator!=(const PathNodeIt& n) const {
     195      return _it != n._it || _nd != n._nd;
     196    }
     197    /// Comparison operator
     198    bool operator<(const PathNodeIt& n) const {
     199      return (_it < n._it && _nd != INVALID);
     200    }
     201   
     202  };
     203
     204  /// \brief Item writer for paths
     205  ///
     206  /// This class can write paths into files. You can store paths in
     207  /// distinict mapset or in attributes section.
     208  ///
     209  ///\code
     210  /// GraphWriter<SmartGraph> gw(std::cout, g);
     211  /// NodeMapWriter<SmartGraph> nmw(gw, g, gw);
     212  ///
     213  /// SmartGraph::NodeMap<Path<SmartGraph> > pnm(g);
     214  /// for (SmartGraph::NodeIt n(g); n != INVALID; ++n) {
     215  ///   pnm[n] = bfs.path(n);
     216  /// }
     217  /// nmw.writeNodeMap("pnm", pnm, PathWriter<Path<SmartGraph> >(gw));
     218  ///
     219  /// gw.run();
     220  ///\endcode
     221  ///
     222  /// \warning Do not use this class to write node or edge map values
     223  /// into usual nodesets or edgesets. You will not be able to read
     224  /// back your paths. Rather use NodeMapWriter, EdgeSetWriter or
     225  /// UEdgeSetWriter to dump paths from maps to lemon file.
     226  template <typename Path>
     227  class PathWriter {
     228  private:
     229
     230    typedef typename Path::Edge Edge;
     231    std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter;
     232
     233  public:
     234
     235    typedef Path Value;
     236
     237    PathWriter(const PathWriter& pw) {
     238      edgeLabelWriter.reset(pw.edgeLabelWriter->clone());
     239    }
     240
     241    /// \brief Constructor
     242    ///
     243    /// The paramter shold be an edge label writer which could
     244    /// be a GraphWriter or an EdgeSetWriter.
     245    template <typename EdgeLabelWriter>
     246    explicit PathWriter(const EdgeLabelWriter& _edgeLabelWriter) {
     247      edgeLabelWriter.reset(new _writer_bits::
     248        LabelWriter<Edge, EdgeLabelWriter>(_edgeLabelWriter));
     249    }
     250
     251    /// \brief Writer function
     252    ///
     253    /// Writes the path to the current stream. The representation
     254    /// is the edge labels beetween parentheses.
     255    void write(std::ostream& os, const Value& value) const {
     256      if (!edgeLabelWriter->isLabelWriter()) {
     257        throw DataFormatError("Cannot find edgeset or label map");
     258      }
     259      os << '(' << ' ';
     260      for (typename Path::EdgeIt e(value); e != INVALID; ++e) {
     261        edgeLabelWriter->write(os, e);
     262        os << ' ';
     263      }
     264      os << ')';
     265    }
     266   
     267  };
     268
     269  namespace _path_bits {
     270
     271    template <typename _Graph>
     272    class PathProxy {
     273    public:
     274      typedef False RevPathTag;
     275
     276      typedef _Graph Graph;
     277      typedef typename Graph::Edge Edge;
     278
     279      PathProxy(const std::vector<Edge>& edges)
     280        : _edges(edges) {}
     281
     282      int length() const {
     283        return _edges.size();
     284      }
     285
     286      bool empty() const {
     287        return _edges.size() == 0;
     288      }
     289
     290      class EdgeIt {
     291      public:
     292        EdgeIt() {}
     293        EdgeIt(const PathProxy& path)
     294          : _path(&path), _index(0) {}
     295       
     296        operator const Edge() const {
     297          return _path->_edges[_index];
     298        }
     299
     300        EdgeIt& operator++() {
     301          ++_index;
     302          return *this;
     303        }
     304
     305        bool operator==(Invalid) const {
     306          return int(_path->_edges.size()) == _index;
     307        }
     308        bool operator!=(Invalid) const {
     309          return int(_path->_edges.size()) != _index;
     310        }
     311
     312      private:
     313        const PathProxy* _path;
     314        int _index;
     315      };
     316     
     317    private:
     318      const std::vector<Edge>& _edges;
     319     
     320    };
     321
     322  }
     323
     324  /// \brief Item reader for paths
     325  ///
     326  /// This class can read paths from files. You can store paths in
     327  /// distinict mapset or in attributes section.
     328  ///
     329  ///\code
     330  /// GraphReader<SmartGraph> gr(std::cout, g);
     331  /// NodeMapReader<SmartGraph> nmr(gr, g, gr);
     332  ///
     333  /// SmartGraph::NodeMap<Path<SmartGraph> > pnm(g);
     334  /// nmr.readNodeMap("pnm", pnm, PathReader<Path<SmartGraph> >(gr));
     335  ///
     336  /// gr.run();
     337  ///\endcode
     338  ///
     339  /// \warning Do not use this class to read node or edge map values
     340  /// from nodesets or edgesets. The edges are not surely constructed
     341  /// when the edge list should be read. Rather use NodeMapReader,
     342  /// EdgeSetReader or UEdgeSetReader to read distinict map sets from file.
     343  template <typename Path>
     344  class PathReader {
     345  private:
     346
     347    typedef typename Path::Edge Edge;
     348    std::auto_ptr<_reader_bits::LabelReaderBase<Edge> > edgeLabelReader;
     349
     350  public:
     351
     352    typedef Path Value;
     353
     354    PathReader(const PathReader& pw) {
     355      edgeLabelReader.reset(pw.edgeLabelReader->clone());
     356    }
     357
     358    /// \brief Constructor
     359    ///
     360    /// The paramter shold be an edge label reader which could
     361    /// be a GraphReader or an EdgeSetReader.
     362    template <typename EdgeLabelReader>
     363    explicit PathReader(const EdgeLabelReader& _edgeLabelReader) {
     364      edgeLabelReader.reset(new _reader_bits::
     365        LabelReader<Edge, EdgeLabelReader>(_edgeLabelReader));
     366    }
     367
     368
     369    /// \brief Reader function
     370    ///
     371    /// Reads the path from the current stream. The representation
     372    /// is the edge labels beetween parentheses.
     373    void read(std::istream& is, Value& value) const {
     374      if (!edgeLabelReader->isLabelReader()) {
     375        throw DataFormatError("Cannot find edgeset or label map");
     376      }
     377      char c;
     378      if (!(is >> c) || c != '(')
     379        throw DataFormatError("PathReader format error");
     380      std::vector<typename Path::Edge> v;
     381      while (is >> c && c != ')') {
     382        is.putback(c);
     383        Edge edge = edgeLabelReader->read(is);
     384        v.push_back(edge);
     385      }
     386      if (!is) throw DataFormatError("PathReader format error");
     387      copyPath(value, _path_bits::PathProxy<typename Path::Edge>(v));
     388    }
     389   
     390  };
     391 
    135392}
    136393
Note: See TracChangeset for help on using the changeset viewer.