COIN-OR::LEMON - Graph Library

Changeset 1852:ffa7c6e96330 in lemon-0.x for lemon


Ignore:
Timestamp:
12/07/05 12:57:30 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2420
Message:

Some bug fixes and improvments in the io classes

Location:
lemon
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lemon/bits/item_reader.h

    r1744 r1852  
    181181  public:
    182182
     183    /// \brief Constructor for InsertReader
     184    ///
     185    /// Constructor for InsertReader
     186    PushBackReader(const ItemReader& _item_reader = ItemReader())
     187      : item_reader(_item_reader) {}
     188
    183189    /// \brief Reads the values into the container from the given stream.
    184190    ///
     
    195201      }
    196202      if (!is) throw DataFormatError("PushBackReader format error");
    197       is.putback(c);
    198203    }
    199204
     
    223228
    224229  public:
     230
     231    /// \brief Constructor for InsertReader
     232    ///
     233    /// Constructor for InsertReader
     234    InsertReader(const ItemReader& _item_reader = ItemReader())
     235      : item_reader(_item_reader) {}
    225236
    226237    /// \brief Reads the values into the container from the given stream.
     
    238249      }
    239250      if (!is) throw DataFormatError("PushBackReader format error");
    240       is.putback(c);
    241251    }
    242252
     
    311321    ///
    312322    /// Reads the line from the given stream.
    313     void read(std::istream& is, Value& value) {
     323    void read(std::istream& is, Value& value) const {
    314324      if (skipSpaces) is >> std::ws;
    315325      if (!getline(is, value)) {
    316         throw DataFormatError("LineReader forma error");
     326        throw DataFormatError("LineReader format error");
    317327      }
    318328    }
    319329  private:
    320330    bool skipSpaces;
     331  };
     332
     333  /// \ingroup item_io
     334  /// \brief Reader for std::pair.
     335  ///
     336  /// Reader for std::pair.
     337  ///
     338  /// \author Balazs Dezso
     339  template <typename _Pair,
     340            typename _FirstReader =
     341            DefaultReader<typename _Pair::first_type>,
     342            typename _SecondReader =
     343            DefaultReader<typename _Pair::second_type> >
     344  class PairReader {
     345  public:
     346    typedef _Pair Value;
     347
     348    typedef _FirstReader FirstReader;
     349    typedef _SecondReader SecondReader;
     350
     351  private:
     352
     353    FirstReader first_reader;
     354    SecondReader second_reader;
     355
     356  public:
     357   
     358    /// \brief Constructor.
     359    ///
     360    /// Constructor for the PairReader.
     361    PairReader(const FirstReader& _first_reader = FirstReader(),
     362               const SecondReader& _second_reader = SecondReader())
     363      : first_reader(_first_reader), second_reader(_second_reader) {}
     364   
     365    /// \brief Reads the pair from the given stream.
     366    ///
     367    /// Reads the pair from the given stream.
     368    void read(std::istream& is, Value& value) const {
     369      char c;
     370      if (!(is >> c) || c != '(') {
     371        throw DataFormatError("PairReader format error");
     372      }
     373      first_reader.read(is, value.first);
     374      if (!(is >> c) || c != '=') {
     375        throw DataFormatError("PairReader format error");
     376      }
     377      if (!(is >> c) || c != '>') {
     378        throw DataFormatError("PairReader format error");
     379      }
     380      second_reader.read(is, value.second);
     381      if (!(is >> c) || c != ')') {
     382        throw DataFormatError("PairReader format error");
     383      }
     384    }
    321385  };
    322386
     
    390454    : public InsertReader<std::set<Item> > {};
    391455
     456  template <typename Key, typename Value>
     457  class DefaultReader<std::map<Key, Value> >
     458    : public InsertReader<std::map<Key, Value>,
     459                          DefaultReader<std::pair<Key, Value> > > {};
     460
    392461  template <typename Item>
    393462  class DefaultReader<std::multiset<Item> >
    394463    : public InsertReader<std::multiset<Item> > {};
    395464
     465  template <typename Key, typename Value>
     466  class DefaultReader<std::multimap<Key, Value> >
     467    : public InsertReader<std::multimap<Key, Value>,
     468                          DefaultReader<std::pair<Key, Value> > > {};
     469
     470  template <typename First, typename Second>
     471  class DefaultReader<std::pair<First, Second> >
     472    : public PairReader<std::pair<First, Second> > {};
     473
    396474  /// \ingroup item_io
    397475  ///
  • lemon/bits/item_writer.h

    r1535 r1852  
    5454    ///
    5555    /// Writes a quoted string to the given stream.
    56     void write(std::ostream& os, const std::string& value) {
     56    void write(std::ostream& os, const std::string& value) const {
    5757      os << "\"";
    5858      if (escaped) {
     
    137137    ///
    138138    /// Writes a quoted char array to the given stream.
    139     void write(std::ostream& os, const char* value) {
     139    void write(std::ostream& os, const char* value) const {
    140140      QuotedStringWriter(escaped).write(os, std::string(value));
    141141    }
     
    169169
    170170  public:
     171
     172    IterableWriter(const ItemWriter& _item_writer = ItemWriter())
     173      : item_writer(_item_writer) {}
    171174
    172175    /// \brief Writes the values of the container to the given stream.
     
    186189
    187190  /// \ingroup item_io
     191  ///
     192  /// \brief Writer for standard pairs.
     193  ///
     194  /// Writer for standard pairs. The representation of a pair is
     195  /// \code ( first_value => second_value ) \endcode.
     196  /// \author Balazs Dezso
     197  template <typename _Pair,
     198            typename _FirstWriter =
     199            DefaultWriter<typename _Pair::first_type>,
     200            typename _SecondWriter =
     201            DefaultWriter<typename _Pair::second_type> >
     202  class PairWriter {
     203  public:
     204
     205    typedef _Pair Value;
     206
     207    typedef _FirstWriter FirstWriter;
     208    typedef _SecondWriter SecondWriter;
     209
     210  private:
     211
     212    FirstWriter first_writer;
     213    SecondWriter second_writer;
     214
     215  public:
     216   
     217    /// \brief Constructor.
     218    ///
     219    /// Constructor for the PairWriter.
     220    PairWriter(const FirstWriter& _first_writer = FirstWriter(),
     221               const SecondWriter& _second_writer = SecondWriter())
     222      : first_writer(_first_writer), second_writer(_second_writer) {}
     223   
     224    /// \brief Writes the pair from the given stream.
     225    ///
     226    /// Writes the pair from the given stream.
     227    void write(std::ostream& os, const Value& value) const {
     228      os << "( ";
     229      first_writer.write(os, value.first);
     230      os << " => ";
     231      second_writer.write(os, value.second);
     232      os << " )";
     233    }
     234
     235  };
     236
     237  /// \ingroup item_io
    188238  ///
    189239  /// \brief The default item writer template class.
     
    218268    : public QuotedCharArrayWriter {};
    219269
     270  template <>
     271  class DefaultWriter<char*>
     272    : public QuotedCharArrayWriter {};
     273
     274  template <>
     275  class DefaultWriter<const char*>
     276    : public QuotedCharArrayWriter {};
     277
    220278  template <typename Item>
    221279  class DefaultWriter<std::vector<Item> >
     
    234292    : public IterableWriter<std::set<Item> > {};
    235293
     294  template <typename Key, typename Value>
     295  class DefaultWriter<std::map<Key, Value> >
     296    : public IterableWriter<std::map<Key, Value> > {};
     297
    236298  template <typename Item>
    237299  class DefaultWriter<std::multiset<Item> >
    238300    : public IterableWriter<std::multiset<Item> > {};
     301
     302  template <typename Key, typename Value>
     303  class DefaultWriter<std::multimap<Key, Value> >
     304    : public IterableWriter<std::multimap<Key, Value> > {};
     305
     306  template <typename First, typename Second>
     307  class DefaultWriter<std::pair<First, Second> >
     308    : public PairWriter<std::pair<First, Second> > {};
    239309
    240310  /// \ingroup item_io
  • lemon/lemon_writer.h

    r1845 r1852  
    173173      virtual ~MapWriterBase() {}
    174174
    175       virtual void write(std::ostream& os, const Item& item) = 0;
     175      virtual void write(std::ostream& os, const Item& item) const = 0;
    176176    };
    177177
     
    193193      virtual ~MapWriter() {}
    194194
    195       virtual void write(std::ostream& os, const Item& item) {
     195      virtual void write(std::ostream& os, const Item& item) const {
    196196        Value value = map[item];
    197197        writer.write(os, value);
Note: See TracChangeset for help on using the changeset viewer.