# HG changeset patch # User deba # Date 1133633849 0 # Node ID f8bbfed860369b3a9f6d0185fca9baf9a201931b # Parent eaa5f5b855f75adec3f5e65599d6249eeee6ae8b Implementation redesign Throws exception diff -r eaa5f5b855f7 -r f8bbfed86036 lemon/error.h --- a/lemon/error.h Sat Dec 03 18:15:43 2005 +0000 +++ b/lemon/error.h Sat Dec 03 18:17:29 2005 +0000 @@ -376,7 +376,7 @@ virtual ~FileOpenError() throw() {} }; - class IOParameterError : public LogicError { + class IOParameterError : public IOError { protected: ExceptionMember _message; ExceptionMember _file; @@ -385,7 +385,7 @@ public: IOParameterError(const IOParameterError &ile) : - LogicError(ile), _message(ile._message), _file(ile._file) {} + IOError(ile), _message(ile._message), _file(ile._file) {} ///\e explicit IOParameterError(const char *the_message) diff -r eaa5f5b855f7 -r f8bbfed86036 lemon/lemon_reader.h --- a/lemon/lemon_reader.h Sat Dec 03 18:15:43 2005 +0000 +++ b/lemon/lemon_reader.h Sat Dec 03 18:17:29 2005 +0000 @@ -45,6 +45,18 @@ namespace _reader_bits { + template + bool operator<(T, T) { + throw DataFormatError("Id is not comparable"); + } + + template + struct Less { + bool operator()(const T& p, const T& q) const { + return p < q; + } + }; + template class ItemIdReader { public: @@ -83,18 +95,6 @@ }; }; - - template - bool operator<(T, T) { - throw DataFormatError("Id is not comparable"); - } - - template - struct Less { - bool operator()(const T& p, const T& q) const { - return p < q; - } - }; template struct Ref { typedef Map& Type; }; @@ -198,6 +198,253 @@ typedef const YMap& Type; }; + + template + class MapReaderBase; + + template + class MapInverterBase : public MapReaderBase<_Item> { + public: + typedef _Item Item; + virtual void read(std::istream&, const Item&) = 0; + virtual Item read(std::istream&) const = 0; + + virtual MapInverterBase<_Item>* getInverter() { + return this; + } + }; + + template + class MapReaderInverter : public MapInverterBase<_Item> { + public: + typedef _Item Item; + typedef _Reader Reader; + typedef typename Reader::Value Value; + typedef _Map Map; + typedef std::map > Inverse; + + typename _reader_bits::Ref::Type map; + Reader reader; + Inverse inverse; + + MapReaderInverter(typename _reader_bits::Arg::Type _map, + const Reader& _reader) + : map(_map), reader(_reader) {} + + virtual ~MapReaderInverter() {} + + virtual void read(std::istream& is, const Item& item) { + Value value; + reader.read(is, value); + map.set(item, value); + typename Inverse::iterator it = inverse.find(value); + if (it == inverse.end()) { + inverse.insert(std::make_pair(value, item)); + } else { + throw DataFormatError("Multiple ID occurence"); + } + } + + virtual Item read(std::istream& is) const { + Value value; + reader.read(is, value); + typename Inverse::const_iterator it = inverse.find(value); + if (it != inverse.end()) { + return it->second; + } else { + throw DataFormatError("Invalid ID error"); + } + } + }; + + template + class SkipReaderInverter : public MapInverterBase<_Item> { + public: + typedef _Item Item; + typedef _Reader Reader; + typedef typename Reader::Value Value; + typedef std::map > Inverse; + + Reader reader; + + SkipReaderInverter(const Reader& _reader) + : reader(_reader) {} + + virtual ~SkipReaderInverter() {} + + virtual void read(std::istream& is, const Item& item) { + Value value; + reader.read(is, value); + typename Inverse::iterator it = inverse.find(value); + if (it == inverse.end()) { + inverse.insert(std::make_pair(value, item)); + } else { + throw DataFormatError("Multiple ID occurence error"); + } + } + + virtual Item read(std::istream& is) const { + Value value; + reader.read(is, value); + typename Inverse::const_iterator it = inverse.find(value); + if (it != inverse.end()) { + return it->second; + } else { + throw DataFormatError("Invalid ID error"); + } + } + + private: + Inverse inverse; + }; + + template + class MapReaderBase { + public: + typedef _Item Item; + + MapReaderBase() { _touched = false; } + + void touch() { _touched = true; } + bool touched() const { return _touched; } + + virtual ~MapReaderBase() {} + + virtual void read(std::istream& is, const Item& item) = 0; + virtual MapInverterBase<_Item>* getInverter() = 0; + + private: + bool _touched; + + }; + + template + class MapReader : public MapReaderBase<_Item> { + public: + typedef _Map Map; + typedef _Reader Reader; + typedef typename Reader::Value Value; + typedef _Item Item; + + typename _reader_bits::Ref::Type map; + Reader reader; + + MapReader(typename _reader_bits::Arg::Type _map, + const Reader& _reader) + : map(_map), reader(_reader) {} + + virtual ~MapReader() {} + + virtual void read(std::istream& is, const Item& item) { + Value value; + reader.read(is, value); + map.set(item, value); + } + + virtual MapInverterBase<_Item>* getInverter() { + return new MapReaderInverter(map, reader); + } + }; + + + template + class SkipReader : public MapReaderBase<_Item> { + public: + typedef _Reader Reader; + typedef typename Reader::Value Value; + typedef _Item Item; + + Reader reader; + SkipReader(const Reader& _reader) : reader(_reader) {} + + virtual ~SkipReader() {} + + virtual void read(std::istream& is, const Item&) { + Value value; + reader.read(is, value); + } + + virtual MapInverterBase* getInverter() { + return new SkipReaderInverter(reader); + } + }; + + template + class IdReaderBase { + public: + typedef _Item Item; + virtual ~IdReaderBase() {} + virtual Item read(std::istream& is) const = 0; + virtual bool isIdReader() const = 0; + }; + + template + class IdReader : public IdReaderBase<_Item> { + public: + typedef _Item Item; + typedef _BoxedIdReader BoxedIdReader; + + const BoxedIdReader& boxedIdReader; + + IdReader(const BoxedIdReader& _boxedIdReader) + : boxedIdReader(_boxedIdReader) {} + + virtual Item read(std::istream& is) const { + Item item; + boxedIdReader.readId(is, item); + return item; + } + + virtual bool isIdReader() const { + return boxedIdReader.isIdReader(); + } + }; + + template + class ItemStore { + public: + + typedef _Item Item; + + ItemStore(Item& _item) : item(&_item) { + _touched = false; + } + + void touch() { _touched = true; } + bool touched() const { return _touched; } + + void read(const Item& _item) { + *item = _item; + } + + private: + Item* item; + bool _touched; + }; + + class ValueReaderBase { + public: + virtual void read(std::istream&) {}; + virtual ~ValueReaderBase() {} + }; + + template + class ValueReader : public ValueReaderBase { + public: + typedef _Value Value; + typedef _Reader Reader; + + ValueReader(Value& _value, const Reader& _reader) + : value(_value), reader(_reader) {} + + virtual void read(std::istream& is) { + reader.read(is, value); + } + private: + Value& value; + Reader reader; + }; + } /// \ingroup io_group @@ -469,237 +716,6 @@ }; - /// \brief Helper class for implementing the common SectionReaders. - /// - /// Helper class for implementing the common SectionReaders. - class CommonSectionReaderBase : public LemonReader::SectionReader { - typedef LemonReader::SectionReader Parent; - protected: - - /// \brief Constructor for CommonSectionReaderBase. - /// - /// Constructor for CommonSectionReaderBase. It attach this reader to - /// the given LemonReader. - CommonSectionReaderBase(LemonReader& _reader) - : Parent(_reader) {} - - template - class ReaderBase; - - template - class InverterBase : public ReaderBase<_Item> { - public: - typedef _Item Item; - virtual void read(std::istream&, const Item&) = 0; - virtual Item read(std::istream&) const = 0; - - virtual InverterBase<_Item>* getInverter() { - return this; - } - }; - - template - class MapReaderInverter : public InverterBase<_Item> { - public: - typedef _Item Item; - typedef _Reader Reader; - typedef typename Reader::Value Value; - typedef _Map Map; - typedef std::map > Inverse; - - typename _reader_bits::Ref::Type map; - Reader reader; - Inverse inverse; - - MapReaderInverter(typename _reader_bits::Arg::Type _map, - const Reader& _reader) - : map(_map), reader(_reader) {} - - virtual ~MapReaderInverter() {} - - virtual void read(std::istream& is, const Item& item) { - Value value; - reader.read(is, value); - map.set(item, value); - typename Inverse::iterator it = inverse.find(value); - if (it == inverse.end()) { - inverse.insert(std::make_pair(value, item)); - } else { - throw DataFormatError("Multiple ID occurence"); - } - } - - virtual Item read(std::istream& is) const { - Value value; - reader.read(is, value); - typename Inverse::const_iterator it = inverse.find(value); - if (it != inverse.end()) { - return it->second; - } else { - throw DataFormatError("Invalid ID error"); - } - } - }; - - template - class SkipReaderInverter : public InverterBase<_Item> { - public: - typedef _Item Item; - typedef _Reader Reader; - typedef typename Reader::Value Value; - typedef std::map > Inverse; - - Reader reader; - - SkipReaderInverter(const Reader& _reader) - : reader(_reader) {} - - virtual ~SkipReaderInverter() {} - - virtual void read(std::istream& is, const Item& item) { - Value value; - reader.read(is, value); - typename Inverse::iterator it = inverse.find(value); - if (it == inverse.end()) { - inverse.insert(std::make_pair(value, item)); - } else { - throw DataFormatError("Multiple ID occurence error"); - } - } - - virtual Item read(std::istream& is) const { - Value value; - reader.read(is, value); - typename Inverse::const_iterator it = inverse.find(value); - if (it != inverse.end()) { - return it->second; - } else { - throw DataFormatError("Invalid ID error"); - } - } - - private: - Inverse inverse; - }; - - template - class ReaderBase { - public: - typedef _Item Item; - - virtual ~ReaderBase() {} - - virtual void read(std::istream& is, const Item& item) = 0; - virtual InverterBase<_Item>* getInverter() = 0; - }; - - template - class MapReader : public ReaderBase<_Item> { - public: - typedef _Map Map; - typedef _Reader Reader; - typedef typename Reader::Value Value; - typedef _Item Item; - - typename _reader_bits::Ref::Type map; - Reader reader; - - MapReader(typename _reader_bits::Arg::Type _map, - const Reader& _reader) - : map(_map), reader(_reader) {} - - virtual ~MapReader() {} - - virtual void read(std::istream& is, const Item& item) { - Value value; - reader.read(is, value); - map.set(item, value); - } - - virtual InverterBase<_Item>* getInverter() { - return new MapReaderInverter(map, reader); - } - }; - - - template - class SkipReader : public ReaderBase<_Item> { - public: - typedef _Reader Reader; - typedef typename Reader::Value Value; - typedef _Item Item; - - Reader reader; - SkipReader(const Reader& _reader) : reader(_reader) {} - - virtual ~SkipReader() {} - - virtual void read(std::istream& is, const Item&) { - Value value; - reader.read(is, value); - } - - virtual InverterBase* getInverter() { - return new SkipReaderInverter(reader); - } - }; - - template - class IdReaderBase { - public: - typedef _Item Item; - virtual ~IdReaderBase() {} - virtual Item read(std::istream& is) const = 0; - virtual bool isIdReader() const = 0; - }; - - template - class IdReader : public IdReaderBase<_Item> { - public: - typedef _Item Item; - typedef _BoxedIdReader BoxedIdReader; - - const BoxedIdReader& boxedIdReader; - - IdReader(const BoxedIdReader& _boxedIdReader) - : boxedIdReader(_boxedIdReader) {} - - virtual Item read(std::istream& is) const { - Item item; - boxedIdReader.readId(is, item); - return item; - } - - virtual bool isIdReader() const { - return boxedIdReader.isIdReader(); - } - }; - - class ValueReaderBase { - public: - virtual void read(std::istream&) {}; - virtual ~ValueReaderBase() {} - }; - - template - class ValueReader : public ValueReaderBase { - public: - typedef _Value Value; - typedef _Reader Reader; - - ValueReader(Value& _value, const Reader& _reader) - : value(_value), reader(_reader) {} - - virtual void read(std::istream& is) { - reader.read(is, value); - } - private: - Value& value; - Reader reader; - }; - - }; - /// \ingroup io_group /// \brief SectionReader for reading a graph's nodeset. /// @@ -718,8 +734,8 @@ /// /// \relates LemonReader template - class NodeSetReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class NodeSetReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; public: typedef _Graph Graph; @@ -801,9 +817,10 @@ ErrorMessage msg; msg << "Multiple read rule for node map: " << name; throw IOParameterError(msg.message()); - } + } readers.insert( - make_pair(name, new MapReader(map, reader))); + make_pair(name, new _reader_bits:: + MapReader(map, reader))); return *this; } @@ -820,7 +837,8 @@ msg << "Multiple read rule for node map: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, new SkipReader(reader))); + readers.insert(make_pair(name, new _reader_bits:: + SkipReader(reader))); return *this; } @@ -843,7 +861,7 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { - std::vector* > index; + std::vector<_reader_bits::MapReaderBase* > index; std::string line; getline(is, line); @@ -851,6 +869,7 @@ while (ls >> id) { typename MapReaders::iterator it = readers.find(id); if (it != readers.end()) { + it->second->touch(); index.push_back(it->second); } else { index.push_back(&skipper); @@ -860,6 +879,14 @@ index.back() = inverter.get(); } } + for (typename MapReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + if (!it->second->touched()) { + ErrorMessage msg; + msg << "Map not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } while (getline(is, line)) { Node node = graph.addNode(); std::istringstream ls(line); @@ -889,14 +916,14 @@ private: - typedef std::map*> MapReaders; + typedef std::map*> MapReaders; MapReaders readers; Graph& graph; std::string id; - SkipReader skipper; + _reader_bits::SkipReader skipper; - std::auto_ptr > inverter; + std::auto_ptr<_reader_bits::MapInverterBase > inverter; }; /// \ingroup io_group @@ -922,8 +949,8 @@ /// /// \relates LemonReader template - class EdgeSetReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class EdgeSetReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; public: typedef _Graph Graph; @@ -948,7 +975,8 @@ const DefaultSkipper& _skipper = DefaultSkipper()) : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); - nodeIdReader.reset(new IdReader(_nodeIdReader)); + nodeIdReader.reset(new _reader_bits:: + IdReader(_nodeIdReader)); } /// \brief Destructor. /// @@ -1013,7 +1041,8 @@ throw IOParameterError(msg.message()); } readers.insert( - make_pair(name, new MapReader(map, reader))); + make_pair(name, new _reader_bits:: + MapReader(map, reader))); return *this; } @@ -1030,7 +1059,8 @@ msg << "Multiple read rule for edge map: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, new SkipReader(reader))); + readers.insert(make_pair(name, new _reader_bits:: + SkipReader(reader))); return *this; } @@ -1056,7 +1086,7 @@ if (!nodeIdReader->isIdReader()) { throw DataFormatError("Cannot find nodeset or ID map"); } - std::vector* > index; + std::vector<_reader_bits::MapReaderBase* > index; std::string line; getline(is, line); @@ -1065,6 +1095,7 @@ typename MapReaders::iterator it = readers.find(id); if (it != readers.end()) { index.push_back(it->second); + it->second->touch(); } else { index.push_back(&skipper); } @@ -1073,6 +1104,14 @@ index.back() = inverter.get(); } } + for (typename MapReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + if (!it->second->touched()) { + ErrorMessage msg; + msg << "Map not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } while (getline(is, line)) { std::istringstream ls(line); Node from = nodeIdReader->read(ls); @@ -1104,15 +1143,15 @@ private: - typedef std::map*> MapReaders; + typedef std::map*> MapReaders; MapReaders readers; Graph& graph; std::string id; - SkipReader skipper; + _reader_bits::SkipReader skipper; - std::auto_ptr > inverter; - std::auto_ptr > nodeIdReader; + std::auto_ptr<_reader_bits::MapInverterBase > inverter; + std::auto_ptr<_reader_bits::IdReaderBase > nodeIdReader; }; /// \ingroup io_group @@ -1143,8 +1182,8 @@ /// /// \relates LemonReader template - class UndirEdgeSetReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class UndirEdgeSetReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; public: typedef _Graph Graph; @@ -1170,7 +1209,8 @@ const DefaultSkipper& _skipper = DefaultSkipper()) : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); - nodeIdReader.reset(new IdReader(_nodeIdReader)); + nodeIdReader.reset(new _reader_bits:: + IdReader(_nodeIdReader)); } /// \brief Destructor. /// @@ -1235,7 +1275,8 @@ throw IOParameterError(msg.message()); } readers.insert( - make_pair(name, new MapReader(map, reader))); + make_pair(name, new _reader_bits:: + MapReader(map, reader))); return *this; } @@ -1252,8 +1293,8 @@ msg << "Multiple read rule for node map: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, - new SkipReader(reader))); + readers.insert(make_pair(name, new _reader_bits:: + SkipReader(reader))); return *this; } @@ -1340,7 +1381,7 @@ if (!nodeIdReader->isIdReader()) { throw DataFormatError("Cannot find nodeset or ID map"); } - std::vector* > index; + std::vector<_reader_bits::MapReaderBase* > index; std::string line; getline(is, line); @@ -1349,6 +1390,7 @@ typename MapReaders::iterator it = readers.find(id); if (it != readers.end()) { index.push_back(it->second); + it->second->touch(); } else { index.push_back(&skipper); } @@ -1357,6 +1399,14 @@ index.back() = inverter.get(); } } + for (typename MapReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + if (!it->second->touched()) { + ErrorMessage msg; + msg << "Map not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } while (getline(is, line)) { std::istringstream ls(line); Node from = nodeIdReader->read(ls); @@ -1408,15 +1458,16 @@ private: - typedef std::map*> MapReaders; + typedef std::map*> MapReaders; MapReaders readers; Graph& graph; std::string id; - SkipReader skipper; + _reader_bits::SkipReader skipper; - std::auto_ptr > inverter; - std::auto_ptr > nodeIdReader; + std::auto_ptr<_reader_bits::MapInverterBase > inverter; + std::auto_ptr<_reader_bits::IdReaderBase > nodeIdReader; }; /// \ingroup io_group @@ -1430,8 +1481,8 @@ /// /// \relates LemonReader template - class NodeReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class NodeReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; typedef _Graph Graph; typedef typename Graph::Node Node; public: @@ -1447,7 +1498,8 @@ const std::string& _id = std::string()) : Parent(_reader), id(_id) { checkConcept<_reader_bits::ItemIdReader, _IdReader>(); - nodeIdReader.reset(new IdReader(_idReader)); + nodeIdReader.reset(new _reader_bits:: + IdReader(_idReader)); } /// \brief Destructor. @@ -1470,7 +1522,7 @@ msg << "Multiple read rule for node: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, &item)); + readers.insert(make_pair(name, _reader_bits::ItemStore(item))); } protected: @@ -1502,18 +1554,27 @@ ls >> id; typename NodeReaders::iterator it = readers.find(id); if (it != readers.end()) { - *(it->second) = nodeIdReader->read(ls); + it->second.read(nodeIdReader->read(ls)); + it->second.touch(); } } + for (typename NodeReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + if (!it->second.touched()) { + ErrorMessage msg; + msg << "Node not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } } private: std::string id; - typedef std::map NodeReaders; + typedef std::map > NodeReaders; NodeReaders readers; - std::auto_ptr > nodeIdReader; + std::auto_ptr<_reader_bits::IdReaderBase > nodeIdReader; }; /// \ingroup io_group @@ -1527,8 +1588,8 @@ /// /// \relates LemonReader template - class EdgeReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class EdgeReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; typedef _Graph Graph; typedef typename Graph::Edge Edge; public: @@ -1544,7 +1605,8 @@ const std::string& _id = std::string()) : Parent(_reader), id(_id) { checkConcept<_reader_bits::ItemIdReader, _IdReader>(); - edgeIdReader.reset(new IdReader(_idReader)); + edgeIdReader.reset(new _reader_bits:: + IdReader(_idReader)); } /// \brief Destructor. @@ -1566,7 +1628,7 @@ msg << "Multiple read rule for edge: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, &item)); + readers.insert(make_pair(name, _reader_bits::ItemStore(item))); } protected: @@ -1598,18 +1660,27 @@ ls >> id; typename EdgeReaders::iterator it = readers.find(id); if (it != readers.end()) { - *(it->second) = edgeIdReader->read(ls); + it->second.read(edgeIdReader->read(ls)); + it->second.touch(); } } + for (typename EdgeReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + if (!it->second.touched()) { + ErrorMessage msg; + msg << "Edge not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } } private: std::string id; - typedef std::map EdgeReaders; + typedef std::map > EdgeReaders; EdgeReaders readers; - std::auto_ptr > edgeIdReader; + std::auto_ptr<_reader_bits::IdReaderBase > edgeIdReader; }; /// \ingroup io_group @@ -1623,8 +1694,8 @@ /// /// \relates LemonReader template - class UndirEdgeReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class UndirEdgeReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; typedef _Graph Graph; typedef typename Graph::Edge Edge; typedef typename Graph::UndirEdge UndirEdge; @@ -1643,8 +1714,10 @@ : Parent(_reader), id(_id) { checkConcept<_reader_bits::ItemIdReader, _IdReader>(); checkConcept<_reader_bits::ItemIdReader, _IdReader>(); - undirEdgeIdReader.reset(new IdReader(_idReader)); - edgeIdReader.reset(new IdReader(_idReader)); + undirEdgeIdReader.reset(new _reader_bits:: + IdReader(_idReader)); + edgeIdReader.reset(new _reader_bits:: + IdReader(_idReader)); } /// \brief Destructor. @@ -1666,7 +1739,8 @@ msg << "Multiple read rule for undirected edge: " << name; throw IOParameterError(msg.message()); } - undirEdgeReaders.insert(make_pair(name, &item)); + undirEdgeReaders.insert(make_pair(name, _reader_bits:: + ItemStore(item))); } /// \brief Add an edge reader command for the UndirEdgeReader. @@ -1678,7 +1752,7 @@ msg << "Multiple read rule for edge: " << name; throw IOParameterError(msg.message()); } - edgeReaders.insert(make_pair(name, &item)); + edgeReaders.insert(make_pair(name, _reader_bits::ItemStore(item))); } protected: @@ -1714,30 +1788,49 @@ { typename UndirEdgeReaders::iterator it = undirEdgeReaders.find(id); if (it != undirEdgeReaders.end()) { - *(it->second) = undirEdgeIdReader->read(ls); - break; + it->second.read(undirEdgeIdReader->read(ls)); + it->second.touch(); + continue; } } { typename EdgeReaders::iterator it = edgeReaders.find(id); if (it != edgeReaders.end()) { - *(it->second) = edgeIdReader->read(ls); - break; + it->second.read(edgeIdReader->read(ls)); + it->second.touch(); + continue; } } } + for (typename EdgeReaders::iterator it = edgeReaders.begin(); + it != edgeReaders.end(); ++it) { + if (!it->second.touched()) { + ErrorMessage msg; + msg << "Edge not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } + for (typename UndirEdgeReaders::iterator it = undirEdgeReaders.begin(); + it != undirEdgeReaders.end(); ++it) { + if (!it->second.touched()) { + ErrorMessage msg; + msg << "UndirEdge not found in file: " << it->first; + throw IOParameterError(msg.message()); + } + } } private: std::string id; - typedef std::map UndirEdgeReaders; + typedef std::map > UndirEdgeReaders; UndirEdgeReaders undirEdgeReaders; - std::auto_ptr > undirEdgeIdReader; + std::auto_ptr<_reader_bits::IdReaderBase > undirEdgeIdReader; - typedef std::map EdgeReaders; + typedef std::map > EdgeReaders; EdgeReaders edgeReaders; - std::auto_ptr > edgeIdReader; + std::auto_ptr<_reader_bits::IdReaderBase > edgeIdReader; }; /// \ingroup io_group @@ -1752,8 +1845,8 @@ /// /// \relates LemonReader template - class AttributeReader : public CommonSectionReaderBase { - typedef CommonSectionReaderBase Parent; + class AttributeReader : public LemonReader::SectionReader { + typedef LemonReader::SectionReader Parent; typedef _Traits Traits; public: /// \brief Constructor. @@ -1801,8 +1894,8 @@ msg << "Multiple read rule for attribute: " << name; throw IOParameterError(msg.message()); } - readers.insert(make_pair(name, new ValueReader - (value, reader))); + readers.insert(make_pair(name, new _reader_bits:: + ValueReader(value, reader))); return *this; } @@ -1840,7 +1933,7 @@ private: std::string id; - typedef std::map Readers; + typedef std::map Readers; Readers readers; }; diff -r eaa5f5b855f7 -r f8bbfed86036 lemon/lemon_writer.h --- a/lemon/lemon_writer.h Sat Dec 03 18:15:43 2005 +0000 +++ b/lemon/lemon_writer.h Sat Dec 03 18:17:29 2005 +0000 @@ -164,6 +164,95 @@ typedef ConstYMap Type; }; + + template + class MapWriterBase { + public: + typedef _Item Item; + + virtual ~MapWriterBase() {} + + virtual void write(std::ostream& os, const Item& item) = 0; + }; + + + template + class MapWriter : public MapWriterBase<_Item> { + public: + typedef _Map Map; + typedef _Writer Writer; + typedef typename Writer::Value Value; + typedef _Item Item; + + typename _writer_bits::Ref::Type map; + Writer writer; + + MapWriter(const Map& _map, const Writer& _writer) + : map(_map), writer(_writer) {} + + virtual ~MapWriter() {} + + virtual void write(std::ostream& os, const Item& item) { + Value value = map[item]; + writer.write(os, value); + } + + }; + + + class ValueWriterBase { + public: + virtual ~ValueWriterBase() {} + virtual void write(std::ostream&) = 0; + }; + + template + class ValueWriter : public ValueWriterBase { + public: + typedef _Value Value; + typedef _Writer Writer; + + ValueWriter(const Value& _value, const Writer& _writer) + : value(_value), writer(_writer) {} + + virtual void write(std::ostream& os) { + writer.write(os, value); + } + private: + const Value& value; + Writer writer; + }; + + + template + class IdWriterBase { + public: + typedef _Item Item; + virtual ~IdWriterBase() {} + virtual void write(std::ostream&, const Item&) const = 0; + virtual bool isIdWriter() const = 0; + }; + + template + class IdWriter : public IdWriterBase<_Item> { + public: + typedef _Item Item; + typedef _BoxedIdWriter BoxedIdWriter; + + const BoxedIdWriter& idWriter; + + IdWriter(const BoxedIdWriter& _idWriter) + : idWriter(_idWriter) {} + + virtual void write(std::ostream& os, const Item& item) const { + idWriter.writeId(os, item); + } + + virtual bool isIdWriter() const { + return idWriter.isIdWriter(); + } + }; + } /// \ingroup io_group @@ -281,109 +370,6 @@ }; - /// \brief Helper class for implementing the common SectionWriters. - /// - /// Helper class for implementing the common SectionWriters. - class CommonSectionWriterBase : public LemonWriter::SectionWriter { - typedef LemonWriter::SectionWriter Parent; - protected: - - /// \brief Constructor for CommonSectionWriterBase. - /// - /// Constructor for CommonSectionWriterBase. It attach this writer to - /// the given LemonWriter. - CommonSectionWriterBase(LemonWriter& _writer) - : Parent(_writer) {} - - template - class WriterBase { - public: - typedef _Item Item; - - virtual ~WriterBase() {} - - virtual void write(std::ostream& os, const Item& item) = 0; - }; - - - template - class MapWriter : public WriterBase<_Item> { - public: - typedef _Map Map; - typedef _Writer Writer; - typedef typename Writer::Value Value; - typedef _Item Item; - - typename _writer_bits::Ref::Type map; - Writer writer; - - MapWriter(const Map& _map, const Writer& _writer) - : map(_map), writer(_writer) {} - - virtual ~MapWriter() {} - - virtual void write(std::ostream& os, const Item& item) { - Value value = map[item]; - writer.write(os, value); - } - - }; - - - class ValueWriterBase { - public: - virtual ~ValueWriterBase() {} - virtual void write(std::ostream&) = 0; - }; - - template - class ValueWriter : public ValueWriterBase { - public: - typedef _Value Value; - typedef _Writer Writer; - - ValueWriter(const Value& _value, const Writer& _writer) - : value(_value), writer(_writer) {} - - virtual void write(std::ostream& os) { - writer.write(os, value); - } - private: - const Value& value; - Writer writer; - }; - - - template - class IdWriterBase { - public: - typedef _Item Item; - virtual ~IdWriterBase() {} - virtual void write(std::ostream&, const Item&) const = 0; - virtual bool isIdWriter() const = 0; - }; - - template - class IdWriter : public IdWriterBase<_Item> { - public: - typedef _Item Item; - typedef _BoxedIdWriter BoxedIdWriter; - - const BoxedIdWriter& idWriter; - - IdWriter(const BoxedIdWriter& _idWriter) - : idWriter(_idWriter) {} - - virtual void write(std::ostream& os, const Item& item) const { - idWriter.writeId(os, item); - } - - virtual bool isIdWriter() const { - return idWriter.isIdWriter(); - } - }; - }; - /// \ingroup io_group /// \brief SectionWriter for writing a graph's nodeset. /// @@ -403,8 +389,8 @@ /// /// \relates LemonWriter template - class NodeSetWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class NodeSetWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; public: typedef _Graph Graph; @@ -457,7 +443,8 @@ checkConcept, Map>(); checkConcept<_writer_bits::ItemWriter, Writer>(); writers.push_back( - make_pair(name, new MapWriter(map, writer))); + make_pair(name, new _writer_bits:: + MapWriter(map, writer))); return *this; } @@ -527,10 +514,11 @@ private: - typedef std::vector*> > MapWriters; + typedef std::vector*> > MapWriters; MapWriters writers; - WriterBase* idMap; + _writer_bits::MapWriterBase* idMap; bool forceIdMap; const Graph& graph; @@ -562,8 +550,8 @@ /// /// \relates LemonWriter template - class EdgeSetWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class EdgeSetWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; public: typedef _Graph Graph; @@ -586,7 +574,8 @@ : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), graph(_graph), id(_id) { checkConcept<_writer_bits::ItemIdWriter, NodeIdWriter>(); - nodeIdWriter.reset(new IdWriter(_nodeIdWriter)); + nodeIdWriter.reset(new _writer_bits:: + IdWriter(_nodeIdWriter)); } /// \brief Destructor. @@ -623,7 +612,8 @@ checkConcept, Map>(); checkConcept<_writer_bits::ItemWriter, Writer>(); writers.push_back( - make_pair(name, new MapWriter(map, writer))); + make_pair(name, new _writer_bits:: + MapWriter(map, writer))); return *this; } @@ -701,16 +691,17 @@ private: - typedef std::vector*> > MapWriters; + typedef std::vector*> > MapWriters; MapWriters writers; - WriterBase* idMap; + _writer_bits::MapWriterBase* idMap; bool forceIdMap; const Graph& graph; std::string id; - std::auto_ptr > nodeIdWriter; + std::auto_ptr<_writer_bits::IdWriterBase > nodeIdWriter; }; /// \ingroup io_group @@ -743,8 +734,8 @@ /// /// \relates LemonWriter template - class UndirEdgeSetWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class UndirEdgeSetWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; public: typedef _Graph Graph; @@ -768,7 +759,8 @@ : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), graph(_graph), id(_id) { checkConcept<_writer_bits::ItemIdWriter, NodeIdWriter>(); - nodeIdWriter.reset(new IdWriter(_nodeIdWriter)); + nodeIdWriter.reset(new _writer_bits:: + IdWriter(_nodeIdWriter)); } /// \brief Destructor. @@ -805,7 +797,8 @@ checkConcept, Map>(); checkConcept<_writer_bits::ItemWriter, Writer>(); writers.push_back( - make_pair(name, new MapWriter(map, writer))); + make_pair(name, new _writer_bits:: + MapWriter(map, writer))); return *this; } @@ -928,17 +921,17 @@ private: - typedef std::vector*> > MapWriters; + typedef std::vector*> > MapWriters; MapWriters writers; - WriterBase* idMap; + _writer_bits::MapWriterBase* idMap; bool forceIdMap; const Graph& graph; std::string id; - std::auto_ptr > nodeIdWriter; + std::auto_ptr<_writer_bits::IdWriterBase > nodeIdWriter; }; /// \ingroup io_group @@ -952,8 +945,8 @@ /// /// \relates LemonWriter template - class NodeWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class NodeWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; typedef _Graph Graph; typedef typename Graph::Node Node; public: @@ -968,7 +961,7 @@ const std::string& _id = std::string()) : Parent(_writer), id(_id) { checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); - idWriter.reset(new IdWriter(_idWriter)); + idWriter.reset(new _writer_bits::IdWriter(_idWriter)); } @@ -1020,7 +1013,7 @@ typedef std::vector > NodeWriters; NodeWriters writers; - std::auto_ptr > idWriter; + std::auto_ptr<_writer_bits::IdWriterBase > idWriter; }; /// \ingroup io_group @@ -1034,8 +1027,8 @@ /// /// \relates LemonWriter template - class EdgeWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class EdgeWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; typedef _Graph Graph; typedef typename Graph::Edge Edge; public: @@ -1050,7 +1043,7 @@ const std::string& _id = std::string()) : Parent(_writer), id(_id) { checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); - idWriter.reset(new IdWriter(_idWriter)); + idWriter.reset(new _writer_bits::IdWriter(_idWriter)); } /// \brief Destructor. @@ -1101,7 +1094,7 @@ typedef std::vector > EdgeWriters; EdgeWriters writers; - std::auto_ptr > idWriter; + std::auto_ptr<_writer_bits::IdWriterBase > idWriter; }; /// \ingroup io_group @@ -1115,8 +1108,8 @@ /// /// \relates LemonWriter template - class UndirEdgeWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class UndirEdgeWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; typedef _Graph Graph; typedef typename Graph::Node Node; typedef typename Graph::Edge Edge; @@ -1135,8 +1128,10 @@ : Parent(_writer), id(_id) { checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); - undirEdgeIdWriter.reset(new IdWriter(_idWriter)); - edgeIdWriter.reset(new IdWriter(_idWriter)); + undirEdgeIdWriter.reset(new _writer_bits:: + IdWriter(_idWriter)); + edgeIdWriter.reset(new _writer_bits:: + IdWriter(_idWriter)); } /// \brief Destructor. @@ -1202,11 +1197,11 @@ typedef std::vector > UndirEdgeWriters; UndirEdgeWriters undirEdgeWriters; - std::auto_ptr > undirEdgeIdWriter; + std::auto_ptr<_writer_bits::IdWriterBase > undirEdgeIdWriter; typedef std::vector > EdgeWriters; EdgeWriters edgeWriters; - std::auto_ptr > edgeIdWriter; + std::auto_ptr<_writer_bits::IdWriterBase > edgeIdWriter; }; @@ -1222,8 +1217,8 @@ /// /// \relates LemonWriter template - class AttributeWriter : public CommonSectionWriterBase { - typedef CommonSectionWriterBase Parent; + class AttributeWriter : public LemonWriter::SectionWriter { + typedef LemonWriter::SectionWriter Parent; typedef _Traits Traits; public: /// \brief Constructor. @@ -1267,8 +1262,8 @@ const Value& value, const Writer& writer = Writer()) { checkConcept<_writer_bits::ItemWriter, Writer>(); - writers.push_back(make_pair(name, new ValueWriter - (value, writer))); + writers.push_back(make_pair(name, new _writer_bits:: + ValueWriter(value, writer))); return *this; } @@ -1296,7 +1291,8 @@ private: std::string id; - typedef std::vector > Writers; + typedef std::vector > Writers; Writers writers; }; diff -r eaa5f5b855f7 -r f8bbfed86036 test/heap_test.cc --- a/test/heap_test.cc Sat Dec 03 18:15:43 2005 +0000 +++ b/test/heap_test.cc Sat Dec 03 18:17:29 2005 +0000 @@ -56,7 +56,7 @@ std::ifstream input(f_name.c_str()); check(input, "Input file '" << f_name << "' not found."); GraphReader(input, graph). - readEdgeMap("length", length). + readEdgeMap("capacity", length). readNode("source", start). run();