deba@1408: /* -*- C++ -*- ladanyi@1435: * lemon/lemon_reader.h - Part of LEMON, a generic C++ optimization library deba@1408: * deba@1408: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1408: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1408: * deba@1408: * Permission to use, modify and distribute this software is granted deba@1408: * provided that this copyright notice appears in all copies. For deba@1408: * precise terms see the accompanying LICENSE file. deba@1408: * deba@1408: * This software is provided "AS IS" with no warranty of any kind, deba@1408: * express or implied, and with no claim as to its suitability for any deba@1408: * purpose. deba@1408: * deba@1408: */ deba@1408: deba@1408: ///\ingroup io_group deba@1408: ///\file deba@1408: ///\brief Lemon Format reader. deba@1408: deba@1421: deba@1408: #ifndef LEMON_LEMON_READER_H deba@1408: #define LEMON_LEMON_READER_H deba@1408: deba@1421: deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: deba@1408: #include deba@1421: #include deba@1421: #include deba@1409: #include deba@1408: deba@1476: #include deba@1476: #include deba@1408: deba@1408: namespace lemon { deba@1408: deba@1421: namespace _reader_bits { deba@1476: deba@1476: template deba@1476: class ItemIdReader { deba@1476: public: deba@1476: deba@1476: bool isIdReader() { return true; } deba@1476: deba@1492: void readId(std::istream&, Item&) {} deba@1476: deba@1476: template deba@1476: struct Constraints { deba@1476: void constraints() { deba@1476: bool b = reader.isIdReader(); deba@1476: ignore_unused_variable_warning(b); deba@1492: Item item; deba@1492: reader.readId(is, item); deba@1476: } deba@1476: _ItemIdReader& reader; deba@1476: std::istream& is; deba@1476: }; deba@1476: deba@1476: }; deba@1492: deba@1492: template deba@1492: class ItemReader { deba@1492: public: deba@1492: void read(std::istream&, Item&) {} deba@1492: deba@1492: template deba@1492: struct Constraints { deba@1492: void constraints() { deba@1492: Item item; deba@1492: reader.read(is, item); deba@1492: } deba@1492: _ItemReader& reader; deba@1492: std::istream& is; deba@1492: }; deba@1492: deba@1492: }; deba@1421: deba@1421: template deba@1421: bool operator<(T, T) { deba@1421: throw DataFormatError("Id is not comparable"); deba@1421: } deba@1421: deba@1421: template deba@1421: struct Less { deba@1421: bool operator()(const T& p, const T& q) const { deba@1421: return p < q; deba@1421: } deba@1421: }; deba@1421: deba@1421: template deba@1421: class WriteComposeMap { deba@1421: public: deba@1421: typedef True NeedCopy; deba@1421: deba@1421: typedef typename M2::Key Key; deba@1421: typedef typename M1::Value Value; deba@1421: deba@1421: WriteComposeMap(typename SmartParameter::Type _m1, const M2& _m2) deba@1421: : m1(_m1), m2(_m2) {} deba@1421: deba@1421: void set(const Key& key, const Value& value) { deba@1421: m1.set(m2[key], value); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: typename SmartReference::Type m1; deba@1421: typename SmartConstReference::Type m2; deba@1421: deba@1421: }; deba@1421: deba@1421: template deba@1421: WriteComposeMap writeComposeMap(M1& m1, const M2& m2) { deba@1421: return WriteComposeMap(m1, m2); deba@1421: } deba@1421: deba@1421: template deba@1421: WriteComposeMap writeComposeMap(const M1& m1, const M2& m2) { deba@1421: return WriteComposeMap(m1, m2); deba@1421: } deba@1421: deba@1421: } deba@1421: deba@1409: /// \ingroup io_group deba@1408: /// \brief Lemon Format reader class. deba@1408: /// deba@1409: /// The Lemon Format contains several sections. We do not want to deba@1409: /// determine what sections are in a lemon file we give only a framework deba@1409: /// to read a section oriented format. deba@1409: /// deba@1409: /// In the Lemon Format each section starts with a line contains a \c \@ deba@1409: /// character on the first not white space position. This line is the deba@1409: /// header line of the section. Each next lines belong to this section deba@1409: /// while it does not starts with \c \@ character. This line can start a deba@1409: /// new section or if it can close the file with the \c \@end line. deba@1427: /// The file format ignore the empty and comment lines. The line is deba@1427: /// comment line if it starts with a \c # character. deba@1409: /// deba@1409: /// The framework provides an abstract LemonReader::SectionReader class deba@1409: /// what defines the interface of a SectionReader. The SectionReader deba@1409: /// has the \c header() member function what get a header line string and deba@1409: /// decides if it want to process the next section. Several SectionReaders deba@1409: /// can be attached to an LemonReader and the first attached what can deba@1409: /// process the section will be used. Its \c read() member will called deba@1427: /// with a stream contains the section. From this stream the empty and deba@1427: /// comment lines are filtered out. deba@1409: /// deba@1409: /// \relates GraphReader deba@1409: /// \relates NodeSetReader deba@1409: /// \relates EdgeSetReader deba@1409: /// \relates NodesReader deba@1409: /// \relates EdgesReader deba@1409: /// \relates AttributeReader deba@1408: class LemonReader { deba@1408: private: deba@1408: deba@1408: class FilterStreamBuf : public std::streambuf { deba@1408: public: deba@1408: deba@1408: typedef std::streambuf Parent; deba@1408: typedef Parent::char_type char_type; deba@1408: FilterStreamBuf(std::istream& is, int& num) deba@1408: : _is(is), _base(0), _eptr(0), deba@1408: _num(num), skip_state(after_endl) {} deba@1408: deba@1408: protected: deba@1408: deba@1408: enum skip_state_type { deba@1408: no_skip, deba@1408: after_endl, deba@1427: comment_line deba@1408: }; deba@1408: deba@1408: char_type small_buf[1]; deba@1408: deba@1408: deba@1408: std::istream& _is; deba@1408: deba@1408: char_type* _base; deba@1408: char_type* _eptr; deba@1408: deba@1408: int& _num; deba@1408: deba@1408: skip_state_type skip_state; deba@1408: deba@1408: deba@1408: char_type* base() { return _base; } deba@1408: deba@1408: char_type* eptr() { return _eptr; } deba@1408: deba@1408: int blen() { return _eptr - _base; } deba@1408: deba@1408: void setb(char_type* buf, int len) { deba@1408: _base = buf; deba@1408: _eptr = buf + len; deba@1408: } deba@1408: deba@1408: virtual std::streambuf* setbuf(char *buf, int len) { deba@1408: if (base()) return 0; deba@1408: if (buf != 0 && len >= (int)sizeof(small_buf)) { deba@1408: setb(buf, len); deba@1408: } else { deba@1408: setb(small_buf, sizeof(small_buf)); deba@1408: } deba@1408: setg(0, 0, 0); deba@1408: return this; deba@1408: } deba@1408: deba@1408: bool put_char(char c) { deba@1408: switch (skip_state) { deba@1408: case no_skip: deba@1408: switch (c) { deba@1408: case '\n': deba@1408: skip_state = after_endl; deba@1408: return true; deba@1408: default: deba@1408: return true; deba@1408: } deba@1408: case after_endl: deba@1408: switch (c) { deba@1408: case '@': deba@1408: return false; deba@1408: case '\n': deba@1408: return false; deba@1408: case '#': deba@1427: skip_state = comment_line; deba@1408: return false; deba@1408: default: deba@1408: if (!isspace(c)) { deba@1408: skip_state = no_skip; deba@1408: return true; deba@1408: } else { deba@1408: return false; deba@1408: } deba@1408: } deba@1408: break; deba@1427: case comment_line: deba@1408: switch (c) { deba@1408: case '\n': deba@1408: skip_state = after_endl; deba@1408: return false; deba@1408: default: deba@1408: return false; deba@1408: } deba@1408: } deba@1408: return false; deba@1408: } deba@1408: deba@1408: virtual int underflow() { deba@1408: char c; deba@1408: if (_is.read(&c, 1)) { deba@1408: _is.putback(c); deba@1408: if (c == '@') { deba@1408: return EOF; deba@1408: } deba@1408: } else { deba@1408: return EOF; deba@1408: } deba@1408: char_type *ptr; deba@1408: for (ptr = base(); ptr != eptr(); ++ptr) { deba@1408: if (_is.read(&c, 1)) { deba@1408: if (c == '\n') ++_num; deba@1408: if (put_char(c)) { deba@1408: *ptr = c; deba@1408: } else { deba@1408: if (skip_state == after_endl && c == '@') { deba@1408: _is.putback('@'); deba@1408: break; deba@1408: } deba@1408: --ptr; deba@1408: } deba@1408: } else { deba@1408: break; deba@1408: } deba@1408: } deba@1408: setg(base(), base(), ptr); deba@1408: return *base(); deba@1408: } deba@1408: deba@1408: virtual int sync() { deba@1408: return EOF; deba@1408: } deba@1408: }; deba@1408: deba@1408: public: deba@1408: deba@1409: /// \brief Abstract base class for reading a section. deba@1409: /// deba@1409: /// This class has an \c header() member function what get a deba@1409: /// header line string and decides if it want to process the next deba@1409: /// section. Several SectionReaders can be attached to an LemonReader deba@1409: /// and the first attached what can process the section will be used. deba@1409: /// Its \c read() member will called with a stream contains the section. deba@1409: /// From this stream the empty lines and comments are filtered out. deba@1408: class SectionReader { deba@1409: friend class LemonReader; deba@1409: protected: deba@1409: /// \brief Constructor for SectionReader. deba@1409: /// deba@1409: /// Constructor for SectionReader. It attach this reader to deba@1409: /// the given LemonReader. deba@1409: SectionReader(LemonReader& reader) { deba@1409: reader.attach(*this); deba@1409: } deba@1409: alpar@1494: virtual ~SectionReader() {} alpar@1494: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1408: virtual bool header(const std::string& line) = 0; deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) = 0; deba@1408: }; deba@1408: deba@1409: /// \brief Constructor for LemonReader. deba@1409: /// deba@1409: /// Constructor for LemonReader which reads from the given stream. deba@1408: LemonReader(std::istream& _is) deba@1408: : is(&_is), own_is(false) {} deba@1408: deba@1409: /// \brief Constructor for LemonReader. deba@1409: /// deba@1409: /// Constructor for LemonReader which reads from the given file. deba@1408: LemonReader(const std::string& filename) deba@1408: : is(0), own_is(true) { deba@1408: is = new std::ifstream(filename.c_str()); deba@1408: } deba@1408: deba@1409: /// \brief Desctructor for LemonReader. deba@1409: /// deba@1409: /// Desctructor for LemonReader. deba@1408: ~LemonReader() { deba@1408: if (own_is) { deba@1408: delete is; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: LemonReader(const LemonReader&); deba@1408: void operator=(const LemonReader&); deba@1408: deba@1408: void attach(SectionReader& reader) { deba@1408: readers.push_back(&reader); deba@1408: } deba@1408: deba@1409: public: deba@1409: /// \brief Executes the LemonReader. deba@1409: /// deba@1409: /// It executes the LemonReader. deba@1408: void run() { deba@1408: int line_num = 0; deba@1408: std::string line; deba@1408: try { deba@1408: while ((++line_num, getline(*is, line)) && line.find("@end") != 0) { deba@1408: SectionReaders::iterator it; deba@1408: for (it = readers.begin(); it != readers.end(); ++it) { deba@1408: if ((*it)->header(line)) { deba@1408: char buf[2048]; deba@1408: FilterStreamBuf buffer(*is, line_num); deba@1408: buffer.pubsetbuf(buf, sizeof(buf)); deba@1408: std::istream is(&buffer); deba@1408: (*it)->read(is); deba@1408: break; deba@1408: } deba@1408: } deba@1408: } deba@1408: } catch (DataFormatError& error) { deba@1408: error.line(line_num); deba@1408: throw error; deba@1408: } deba@1408: } deba@1408: deba@1408: deba@1408: private: deba@1408: deba@1408: std::istream* is; deba@1408: bool own_is; deba@1408: deba@1408: typedef std::vector SectionReaders; deba@1408: SectionReaders readers; deba@1408: deba@1408: }; deba@1408: deba@1409: /// \brief Helper class for implementing the common SectionReaders. deba@1409: /// deba@1409: /// Helper class for implementing the common SectionReaders. deba@1409: class CommonSectionReaderBase : public LemonReader::SectionReader { deba@1409: typedef LemonReader::SectionReader Parent; deba@1409: protected: deba@1409: deba@1409: /// \brief Constructor for CommonSectionReaderBase. deba@1409: /// deba@1409: /// Constructor for CommonSectionReaderBase. It attach this reader to deba@1409: /// the given LemonReader. deba@1409: CommonSectionReaderBase(LemonReader& _reader) deba@1409: : Parent(_reader) {} deba@1408: deba@1408: template deba@1408: class ReaderBase; deba@1408: deba@1408: template deba@1408: class InverterBase : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: virtual void read(std::istream&, const Item&) = 0; deba@1408: virtual Item read(std::istream&) const = 0; deba@1408: deba@1408: virtual InverterBase<_Item>* getInverter() { deba@1408: return this; deba@1408: } deba@1408: }; deba@1408: deba@1408: template deba@1408: class MapReaderInverter : public InverterBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Map Map; deba@1424: typedef std::map > Inverse; deba@1408: deba@1421: typename SmartReference::Type map; deba@1408: Reader reader; deba@1408: Inverse inverse; deba@1408: deba@1421: MapReaderInverter(typename SmartParameter::Type _map, deba@1421: const Reader& _reader) deba@1408: : map(_map), reader(_reader) {} deba@1408: deba@1408: virtual ~MapReaderInverter() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: map.set(item, value); deba@1408: typename Inverse::iterator it = inverse.find(value); deba@1408: if (it == inverse.end()) { deba@1408: inverse.insert(std::make_pair(value, item)); deba@1408: } else { deba@1408: throw DataFormatError("Multiple ID occurence"); deba@1408: } deba@1408: } deba@1408: deba@1408: virtual Item read(std::istream& is) const { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::const_iterator it = inverse.find(value); deba@1408: if (it != inverse.end()) { deba@1408: return it->second; deba@1408: } else { deba@1408: throw DataFormatError("Invalid ID error"); deba@1408: } deba@1408: } deba@1408: }; deba@1408: deba@1408: template deba@1408: class SkipReaderInverter : public InverterBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1424: typedef std::map > Inverse; deba@1408: deba@1408: Reader reader; deba@1408: deba@1408: SkipReaderInverter(const Reader& _reader) deba@1408: : reader(_reader) {} deba@1408: deba@1408: virtual ~SkipReaderInverter() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::iterator it = inverse.find(value); deba@1408: if (it == inverse.end()) { deba@1408: inverse.insert(std::make_pair(value, item)); deba@1408: } else { deba@1408: throw DataFormatError("Multiple ID occurence error"); deba@1408: } deba@1408: } deba@1408: deba@1408: virtual Item read(std::istream& is) const { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::const_iterator it = inverse.find(value); deba@1408: if (it != inverse.end()) { deba@1408: return it->second; deba@1408: } else { deba@1408: throw DataFormatError("Invalid ID error"); deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: Inverse inverse; deba@1408: }; deba@1408: deba@1408: template deba@1408: class ReaderBase { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: deba@1408: virtual ~ReaderBase() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) = 0; deba@1408: virtual InverterBase<_Item>* getInverter() = 0; deba@1408: }; deba@1408: deba@1408: template deba@1408: class MapReader : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Map Map; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Item Item; deba@1408: deba@1421: typename SmartReference::Type map; deba@1408: Reader reader; deba@1408: deba@1421: MapReader(typename SmartParameter::Type _map, deba@1421: const Reader& _reader) deba@1408: : map(_map), reader(_reader) {} deba@1408: deba@1408: virtual ~MapReader() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: map.set(item, value); deba@1408: } deba@1408: deba@1408: virtual InverterBase<_Item>* getInverter() { deba@1408: return new MapReaderInverter(map, reader); deba@1408: } deba@1408: }; deba@1408: deba@1408: deba@1408: template deba@1408: class SkipReader : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Item Item; deba@1408: deba@1408: Reader reader; deba@1408: SkipReader(const Reader& _reader) : reader(_reader) {} deba@1408: deba@1408: virtual ~SkipReader() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item&) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: } deba@1408: deba@1408: virtual InverterBase* getInverter() { deba@1408: return new SkipReaderInverter(reader); deba@1408: } deba@1408: }; deba@1408: deba@1408: template deba@1409: class IdReaderBase { deba@1408: public: deba@1408: typedef _Item Item; alpar@1494: virtual ~IdReaderBase() {} deba@1409: virtual Item read(std::istream& is) const = 0; deba@1476: virtual bool isIdReader() const = 0; deba@1408: }; deba@1408: deba@1409: template deba@1409: class IdReader : public IdReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1409: typedef _BoxedIdReader BoxedIdReader; deba@1409: deba@1409: const BoxedIdReader& boxedIdReader; deba@1408: deba@1409: IdReader(const BoxedIdReader& _boxedIdReader) deba@1409: : boxedIdReader(_boxedIdReader) {} deba@1408: deba@1409: virtual Item read(std::istream& is) const { deba@1492: Item item; deba@1492: boxedIdReader.readId(is, item); deba@1492: return item; deba@1408: } deba@1476: deba@1476: virtual bool isIdReader() const { deba@1476: return boxedIdReader.isIdReader(); deba@1476: } deba@1408: }; deba@1408: deba@1408: class ValueReaderBase { deba@1408: public: deba@1408: virtual void read(std::istream&) {}; alpar@1494: virtual ~ValueReaderBase() {} deba@1408: }; deba@1408: deba@1408: template deba@1408: class ValueReader : public ValueReaderBase { deba@1408: public: deba@1408: typedef _Value Value; deba@1408: typedef _Reader Reader; deba@1408: deba@1408: ValueReader(Value& _value, const Reader& _reader) deba@1408: : value(_value), reader(_reader) {} deba@1408: deba@1408: virtual void read(std::istream& is) { deba@1408: reader.read(is, value); deba@1408: } deba@1408: private: deba@1408: Value& value; deba@1408: Reader reader; deba@1408: }; deba@1408: deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading a graph's nodeset. deba@1409: /// deba@1409: /// The lemon format can store multiple graph nodesets with several maps. deba@1409: /// The nodeset section's header line is \c \@nodeset \c nodeset_id, but the deba@1409: /// \c nodeset_id may be empty. deba@1409: /// deba@1409: /// The first line of the section contains the names of the maps separated deba@1409: /// with white spaces. Each next lines describes a node in the nodeset, and deba@1409: /// contains the mapped values for each map. deba@1409: /// deba@1409: /// If the nodeset contains an \c "id" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1409: /// \c readId() member will read a value from the given stream it will deba@1409: /// give back that node which is mapped to this value. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class NodeSetReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: public: deba@1408: deba@1408: typedef _Graph Graph; deba@1408: typedef _Traits Traits; deba@1429: typedef typename Graph::Node Node; deba@1408: typedef typename Traits::Skipper DefaultSkipper; deba@1408: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for NodeSetReader. It creates the NodeSetReader and deba@1409: /// attach it into the given LemonReader. The nodeset reader will deba@1409: /// add the readed nodes to the given Graph. The reader will read deba@1409: /// the section when the \c section_id and the \c _id are the same. deba@1421: NodeSetReader(LemonReader& _reader, deba@1421: typename SmartParameter::Type _graph, deba@1408: const std::string& _id = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1409: : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {} deba@1408: deba@1409: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for NodeSetReader. deba@1408: virtual ~NodeSetReader() { deba@1408: for (typename MapReaders::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: NodeSetReader(const NodeSetReader&); deba@1408: void operator=(const NodeSetReader&); deba@1408: deba@1408: public: deba@1408: deba@1408: /// \brief Add a new node map reader command for the reader. deba@1408: /// deba@1408: /// Add a new node map reader command for the reader. deba@1408: template deba@1421: NodeSetReader& readNodeMap(std::string name, Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: template deba@1421: NodeSetReader& readNodeMap(std::string name, const Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1408: } deba@1408: deba@1408: /// \brief Add a new node map reader command for the reader. deba@1408: /// deba@1408: /// Add a new node map reader command for the reader. deba@1408: template deba@1421: NodeSetReader& readNodeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1429: return _readMap::Type> deba@1429: (name, map, reader); deba@1421: } deba@1421: deba@1421: template deba@1421: NodeSetReader& readNodeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1429: return _readMap::Type> deba@1429: (name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template deba@1421: NodeSetReader& _readMap(std::string name, MapParameter map, deba@1421: const Reader& reader = Reader()) { deba@1492: checkConcept, Map>(); deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for node map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert( deba@1429: make_pair(name, new MapReader(map, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1421: public: deba@1421: deba@1408: /// \brief Add a new node map skipper command for the reader. deba@1408: /// deba@1408: /// Add a new node map skipper command for the reader. deba@1408: template deba@1421: NodeSetReader& skipNodeMap(std::string name, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for node map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1429: readers.insert(make_pair(name, new SkipReader(reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1421: /// It gives back true when the header line starts with \c \@nodeset, deba@1409: /// and the header line's id and the nodeset's id are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@nodeset" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) { deba@1429: std::vector* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1408: std::istringstream ls(line); deba@1408: while (ls >> id) { deba@1408: typename MapReaders::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: index.push_back(it->second); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@1408: if (id == "id" && inverter.get() == 0) { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1408: while (getline(is, line)) { deba@1429: Node node = graph.addNode(); deba@1408: std::istringstream ls(line); deba@1408: for (int i = 0; i < (int)index.size(); ++i) { deba@1408: index[i]->read(ls, node); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1409: public: deba@1409: deba@1409: /// \brief Returns true if the nodeset can give back the node by its id. deba@1409: /// deba@1409: /// Returns true if the nodeset can give back the node by its id. deba@1409: /// It is possible only if an "id" named map was read. deba@1409: bool isIdReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1409: /// \brief Gives back the node by its id. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which node belongs to deba@1409: /// it. It is possible only if there was read an "id" named map. deba@1492: void readId(std::istream& is, Node& node) const { deba@1492: node = inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@1429: typedef std::map*> MapReaders; deba@1408: MapReaders readers; deba@1408: deba@1421: typename SmartReference::Type graph; deba@1408: std::string id; deba@1429: SkipReader skipper; deba@1408: deba@1429: std::auto_ptr > inverter; deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading a graph's edgeset. deba@1409: /// deba@1409: /// The lemon format can store multiple graph edgesets with several maps. deba@1409: /// The edgeset section's header line is \c \@edgeset \c edgeset_id, but the deba@1409: /// \c edgeset_id may be empty. deba@1409: /// deba@1409: /// The first line of the section contains the names of the maps separated deba@1421: /// with white spaces. Each next lines describes an edge in the edgeset. The deba@1421: /// line contains the source and the target nodes' id and the mapped deba@1421: /// values for each map. deba@1409: /// deba@1409: /// If the edgeset contains an \c "id" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1409: /// \c readId() member will read a value from the given stream it will deba@1409: /// give back that edge which is mapped to this value. deba@1409: /// deba@1409: /// The edgeset reader needs a node id reader to identify which nodes deba@1409: /// have to be connected. If a NodeSetReader reads an "id" named map, deba@1409: /// it will be able to resolve the nodes by ids. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class EdgeSetReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: public: deba@1408: deba@1408: typedef _Graph Graph; deba@1408: typedef _Traits Traits; deba@1429: typedef typename Graph::Node Node; deba@1429: typedef typename Graph::Edge Edge; deba@1408: typedef typename Traits::Skipper DefaultSkipper; deba@1408: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for EdgeSetReader. It creates the EdgeSetReader and deba@1409: /// attach it into the given LemonReader. The edgeset reader will deba@1409: /// add the readed edges to the given Graph. It will use the given deba@1409: /// node id reader to read the source and target nodes of the edges. deba@1409: /// The reader will read the section only if the \c _id and the deba@1409: /// \c edgset_id are the same. deba@1409: template deba@1421: EdgeSetReader(LemonReader& _reader, deba@1421: typename SmartParameter::Type _graph, deba@1409: const NodeIdReader& _nodeIdReader, deba@1408: const std::string& _id = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1476: : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { deba@1476: checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); deba@1476: nodeIdReader.reset(new IdReader(_nodeIdReader)); deba@1476: } deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for EdgeSetReader. deba@1408: virtual ~EdgeSetReader() { deba@1408: for (typename MapReaders::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: EdgeSetReader(const EdgeSetReader&); deba@1408: void operator=(const EdgeSetReader&); deba@1408: deba@1408: public: deba@1408: deba@1409: /// \brief Add a new edge map reader command for the reader. deba@1408: /// deba@1409: /// Add a new edge map reader command for the reader. deba@1408: template deba@1421: EdgeSetReader& readEdgeMap(std::string name, Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: template deba@1421: EdgeSetReader& readEdgeMap(std::string name, const Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1408: } deba@1408: deba@1409: /// \brief Add a new edge map reader command for the reader. deba@1408: /// deba@1409: /// Add a new edge map reader command for the reader. deba@1408: template deba@1421: EdgeSetReader& readEdgeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1429: return _readMap::Type>(name, map, reader); deba@1421: } deba@1421: deba@1421: template deba@1421: EdgeSetReader& readEdgeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1429: return _readMap::Type>(name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template deba@1421: EdgeSetReader& _readMap(std::string name, MapParameter map, deba@1421: const Reader& reader = Reader()) { deba@1492: checkConcept, Map>(); deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for edge map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert( deba@1429: make_pair(name, new MapReader(map, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1421: public: deba@1421: deba@1409: /// \brief Add a new edge map skipper command for the reader. deba@1408: /// deba@1409: /// Add a new edge map skipper command for the reader. deba@1408: template deba@1421: EdgeSetReader& skipEdgeMap(std::string name, deba@1421: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1421: msg << "Multiple read rule for edge map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1429: readers.insert(make_pair(name, new SkipReader(reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1421: /// It gives back true when the header line starts with \c \@edgeset, deba@1409: /// and the header line's id and the edgeset's id are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@edgeset" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) { deba@1476: if (!nodeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find nodeset or ID map"); deba@1476: } deba@1429: std::vector* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1408: std::istringstream ls(line); deba@1408: while (ls >> id) { deba@1408: typename MapReaders::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: index.push_back(it->second); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@1408: if (id == "id" && inverter.get() == 0) { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1408: while (getline(is, line)) { deba@1408: std::istringstream ls(line); deba@1429: Node from = nodeIdReader->read(ls); deba@1429: Node to = nodeIdReader->read(ls); deba@1429: Edge edge = graph.addEdge(from, to); deba@1408: for (int i = 0; i < (int)index.size(); ++i) { deba@1408: index[i]->read(ls, edge); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1409: public: deba@1409: deba@1409: /// \brief Returns true if the edgeset can give back the edge by its id. deba@1409: /// deba@1409: /// Returns true if the edgeset can give back the edge by its id. deba@1409: /// It is possible only if an "id" named map was read. deba@1409: bool isIdReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1409: /// \brief Gives back the edge by its id. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which edge belongs to deba@1409: /// it. It is possible only if there was read an "id" named map. deba@1492: void readId(std::istream& is, Edge& edge) const { deba@1492: edge = inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@1429: typedef std::map*> MapReaders; deba@1408: MapReaders readers; deba@1408: deba@1421: typename SmartReference::Type graph; deba@1421: std::string id; deba@1429: SkipReader skipper; deba@1421: deba@1429: std::auto_ptr > inverter; deba@1429: std::auto_ptr > nodeIdReader; deba@1421: }; deba@1421: deba@1421: /// \ingroup io_group deba@1421: /// \brief SectionReader for reading a undirected graph's edgeset. deba@1421: /// deba@1421: /// The lemon format can store multiple undirected edgesets with several deba@1421: /// maps. The undirected edgeset section's header line is \c \@undiredgeset deba@1421: /// \c undiredgeset_id, but the \c undiredgeset_id may be empty. deba@1421: /// deba@1421: /// The first line of the section contains the names of the maps separated deba@1421: /// with white spaces. Each next lines describes an edge in the edgeset. The deba@1421: /// line contains the connected nodes' id and the mapped values for each map. deba@1421: /// deba@1421: /// The section can handle the directed as a syntactical sugar. Two deba@1421: /// undirected edge map describes one directed edge map. This two maps deba@1421: /// are the forward map and the backward map and the names of this map deba@1421: /// is near the same just with a prefix \c '+' or \c '-' character deba@1421: /// difference. deba@1421: /// deba@1421: /// If the edgeset contains an \c "id" named map then it will be regarded deba@1421: /// as id map. This map should contain only unique values and when the deba@1421: /// \c readId() member will read a value from the given stream it will deba@1421: /// give back that undiricted edge which is mapped to this value. deba@1421: /// deba@1421: /// The undirected edgeset reader needs a node id reader to identify which deba@1421: /// nodes have to be connected. If a NodeSetReader reads an "id" named map, deba@1421: /// it will be able to resolve the nodes by ids. deba@1421: /// deba@1421: /// \relates LemonReader deba@1421: template deba@1421: class UndirEdgeSetReader : public CommonSectionReaderBase { deba@1421: typedef CommonSectionReaderBase Parent; deba@1421: public: deba@1421: deba@1421: typedef _Graph Graph; deba@1421: typedef _Traits Traits; deba@1429: typedef typename Graph::Node Node; deba@1429: typedef typename Graph::Edge Edge; deba@1429: typedef typename Graph::UndirEdge UndirEdge; deba@1421: typedef typename Traits::Skipper DefaultSkipper; deba@1421: deba@1421: /// \brief Constructor. deba@1421: /// deba@1421: /// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader deba@1421: /// and attach it into the given LemonReader. The undirected edgeset deba@1421: /// reader will add the readed undirected edges to the given Graph. It deba@1421: /// will use the given node id reader to read the source and target deba@1421: /// nodes of the edges. The reader will read the section only if the deba@1421: /// \c _id and the \c undiredgset_id are the same. deba@1421: template deba@1421: UndirEdgeSetReader(LemonReader& _reader, deba@1421: typename SmartParameter::Type _graph, deba@1421: const NodeIdReader& _nodeIdReader, deba@1421: const std::string& _id = std::string(), deba@1421: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1476: : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { deba@1476: checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); deba@1476: nodeIdReader.reset(new IdReader(_nodeIdReader)); deba@1476: } deba@1421: /// \brief Destructor. deba@1421: /// deba@1421: /// Destructor for UndirEdgeSetReader. deba@1421: virtual ~UndirEdgeSetReader() { deba@1421: for (typename MapReaders::iterator it = readers.begin(); deba@1421: it != readers.end(); ++it) { deba@1421: delete it->second; deba@1421: } deba@1421: } deba@1421: deba@1421: private: deba@1421: UndirEdgeSetReader(const UndirEdgeSetReader&); deba@1421: void operator=(const UndirEdgeSetReader&); deba@1421: deba@1421: public: deba@1421: deba@1421: /// \brief Add a new undirected edge map reader command for the reader. deba@1421: /// deba@1421: /// Add a new edge undirected map reader command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: /// \brief Add a new undirected edge map reader command for the reader. deba@1421: /// deba@1421: /// Add a new edge undirected map reader command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1421: return _readMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1421: return _readMap::Type > deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& _readMap(std::string name, MapParameter map, deba@1421: const Reader& reader = Reader()) { deba@1492: checkConcept, Map>(); deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); deba@1421: if (readers.find(name) != readers.end()) { deba@1421: ErrorMessage msg; deba@1421: msg << "Multiple read rule for edge map: " << name; deba@1421: throw IOParameterError(msg.message()); deba@1421: } deba@1421: readers.insert( deba@1429: make_pair(name, new MapReader(map, reader))); deba@1421: return *this; deba@1421: } deba@1421: deba@1421: public: deba@1421: deba@1421: /// \brief Add a new undirected edge map skipper command for the reader. deba@1421: /// deba@1421: /// Add a new undirected edge map skipper command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& skipUndirEdgeMap(std::string name, deba@1421: const Reader& reader = Reader()) { deba@1421: if (readers.find(name) != readers.end()) { deba@1421: ErrorMessage msg; deba@1421: msg << "Multiple read rule for node map: " << name; deba@1421: throw IOParameterError(msg.message()); deba@1421: } deba@1429: readers.insert(make_pair(name, deba@1429: new SkipReader(reader))); deba@1421: return *this; deba@1421: } deba@1421: deba@1421: /// \brief Add a new directed edge map reader command for the reader. deba@1421: /// deba@1421: /// Add a new directed edge map reader command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) { deba@1421: return _readDirMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) { deba@1421: return _readDirMap< deba@1421: typename Traits::template Reader, Map, deba@1421: typename SmartParameter::Type>(name, map); deba@1421: } deba@1421: deba@1421: /// \brief Add a new directed edge map reader command for the reader. deba@1421: /// deba@1421: /// Add a new directed edge map reader command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& readEdgeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1421: return _readDirMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1421: return _readDirMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template deba@1421: UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map, deba@1492: const Reader& reader = Reader()) { deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); deba@1492: checkConcept, Map>(); deba@1421: readMap("+" + name, deba@1421: _reader_bits::writeComposeMap(map, forwardMap(graph)), reader); deba@1421: readMap("-" + name, deba@1421: _reader_bits::writeComposeMap(map, backwardMap(graph)), reader); deba@1421: return *this; deba@1421: } deba@1421: deba@1421: public: deba@1421: deba@1421: /// \brief Add a new directed edge map skipper command for the reader. deba@1421: /// deba@1421: /// Add a new directed edge map skipper command for the reader. deba@1421: template deba@1421: UndirEdgeSetReader& skipEdgeMap(std::string name, deba@1421: const Reader& reader = Reader()) { deba@1421: skipMap("+" + name, reader); deba@1421: skipMap("-" + name, reader); deba@1421: return *this; deba@1421: } deba@1421: deba@1421: protected: deba@1421: deba@1421: /// \brief Gives back true when the SectionReader can process deba@1421: /// the section with the given header line. deba@1421: /// deba@1421: /// It gives back true when the header line starts with \c \@undiredgeset, deba@1421: /// and the header line's id and the edgeset's id are the same. deba@1421: virtual bool header(const std::string& line) { deba@1421: std::istringstream ls(line); deba@1421: std::string command; deba@1421: std::string name; deba@1421: ls >> command >> name; deba@1421: return command == "@undiredgeset" && name == id; deba@1421: } deba@1421: deba@1421: /// \brief Reader function of the section. deba@1421: /// deba@1421: /// It reads the content of the section. deba@1421: virtual void read(std::istream& is) { deba@1476: if (!nodeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find nodeset or ID map"); deba@1476: } deba@1429: std::vector* > index; deba@1421: std::string line; deba@1421: deba@1421: getline(is, line); deba@1421: std::istringstream ls(line); deba@1421: while (ls >> id) { deba@1421: typename MapReaders::iterator it = readers.find(id); deba@1421: if (it != readers.end()) { deba@1421: index.push_back(it->second); deba@1421: } else { deba@1421: index.push_back(&skipper); deba@1421: } deba@1421: if (id == "id" && inverter.get() == 0) { deba@1421: inverter.reset(index.back()->getInverter()); deba@1421: index.back() = inverter.get(); deba@1421: } deba@1421: } deba@1421: while (getline(is, line)) { deba@1421: std::istringstream ls(line); deba@1429: Node from = nodeIdReader->read(ls); deba@1429: Node to = nodeIdReader->read(ls); deba@1429: UndirEdge edge = graph.addEdge(from, to); deba@1421: for (int i = 0; i < (int)index.size(); ++i) { deba@1421: index[i]->read(ls, edge); deba@1421: } deba@1421: } deba@1421: } deba@1421: deba@1421: public: deba@1421: deba@1421: /// \brief Returns true if the edgeset can give back the edge by its id. deba@1421: /// deba@1421: /// Returns true if the edgeset can give back the undirected edge by its deba@1421: /// id. It is possible only if an "id" named map was read. deba@1421: bool isIdReader() const { deba@1421: return inverter.get() != 0; deba@1421: } deba@1421: deba@1421: /// \brief Gives back the undirected edge by its id. deba@1421: /// deba@1421: /// It reads an id from the stream and gives back which undirected edge deba@1421: /// belongs to it. It is possible only if there was read an "id" named map. deba@1492: void readId(std::istream& is, UndirEdge& undirEdge) const { deba@1492: undirEdge = inverter->read(is); deba@1421: } deba@1421: deba@1429: /// \brief Gives back the directed edge by its id. deba@1429: /// deba@1429: /// It reads an id from the stream and gives back which directed edge deba@1429: /// belongs to it. The directed edge id is the \c '+' or \c '-' character deba@1429: /// and the undirected edge id. It is possible only if there was read deba@1429: /// an "id" named map. deba@1492: void readId(std::istream& is, Edge& edge) const { deba@1429: char c; deba@1429: is >> c; deba@1429: UndirEdge undirEdge = inverter->read(is); deba@1429: if (c == '+') { deba@1627: edge = graph.direct(undirEdge, true); deba@1429: } else if (c == '-') { deba@1627: edge = graph.direct(undirEdge, false); deba@1429: } else { deba@1429: throw DataFormatError("Wrong id format for edge " deba@1429: "in undirected edgeset"); deba@1429: } deba@1429: } deba@1429: deba@1421: private: deba@1421: deba@1429: typedef std::map*> MapReaders; deba@1421: MapReaders readers; deba@1421: deba@1421: typename SmartReference::Type graph; deba@1408: std::string id; deba@1429: SkipReader skipper; deba@1408: deba@1429: std::auto_ptr > inverter; deba@1429: std::auto_ptr > nodeIdReader; deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading labeled nodes. deba@1409: /// deba@1409: /// The nodes section's header line is \c \@nodes \c nodes_id, but the deba@1409: /// \c nodes_id may be empty. deba@1409: /// deba@1409: /// Each line in the section contains the name of the node deba@1409: /// and then the node id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1409: template deba@1409: class NodeReader : public CommonSectionReaderBase { deba@1409: typedef CommonSectionReaderBase Parent; deba@1409: typedef _Graph Graph; deba@1429: typedef typename Graph::Node Node; deba@1409: public: deba@1409: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for NodeReader. It creates the NodeReader and deba@1409: /// attach it into the given LemonReader. It will use the given deba@1409: /// node id reader to give back the nodes. The reader will read the deba@1409: /// section only if the \c _id and the \c nodes_id are the same. deba@1409: template deba@1409: NodeReader(LemonReader& _reader, const _IdReader& _idReader, deba@1409: const std::string& _id = std::string()) deba@1476: : Parent(_reader), id(_id) { deba@1476: checkConcept<_reader_bits::ItemIdReader, _IdReader>(); deba@1492: nodeIdReader.reset(new IdReader(_idReader)); deba@1476: } deba@1408: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for NodeReader. deba@1409: virtual ~NodeReader() {} deba@1409: deba@1409: private: deba@1409: NodeReader(const NodeReader&); deba@1409: void operator=(const NodeReader&); deba@1409: deba@1409: public: deba@1409: deba@1409: /// \brief Add a node reader command for the NodeReader. deba@1409: /// deba@1409: /// Add a node reader command for the NodeReader. deba@1429: void readNode(const std::string& name, Node& item) { deba@1409: if (readers.find(name) != readers.end()) { deba@1409: ErrorMessage msg; deba@1409: msg << "Multiple read rule for node: " << name; deba@1409: throw IOParameterError(msg.message()); deba@1409: } deba@1409: readers.insert(make_pair(name, &item)); deba@1409: } deba@1409: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1421: /// It gives back true when the header line start with \c \@nodes, deba@1409: /// and the header line's id and the reader's id are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1409: std::string name; deba@1409: ls >> command >> name; deba@1409: return command == "@nodes" && name == id; deba@1409: } deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1409: virtual void read(std::istream& is) { deba@1492: if (!nodeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find nodeset or ID map"); deba@1476: } deba@1409: std::string line; deba@1409: while (getline(is, line)) { deba@1409: std::istringstream ls(line); deba@1409: std::string id; deba@1409: ls >> id; deba@1429: typename NodeReaders::iterator it = readers.find(id); deba@1409: if (it != readers.end()) { deba@1492: *(it->second) = nodeIdReader->read(ls); deba@1409: } deba@1409: } deba@1409: } deba@1409: deba@1409: private: deba@1409: deba@1409: std::string id; deba@1409: deba@1429: typedef std::map NodeReaders; deba@1429: NodeReaders readers; deba@1492: std::auto_ptr > nodeIdReader; deba@1409: }; deba@1409: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading labeled edges. deba@1409: /// deba@1409: /// The edges section's header line is \c \@edges \c edges_id, but the deba@1409: /// \c edges_id may be empty. deba@1409: /// deba@1409: /// Each line in the section contains the name of the edge deba@1409: /// and then the edge id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1409: template deba@1409: class EdgeReader : public CommonSectionReaderBase { deba@1409: typedef CommonSectionReaderBase Parent; deba@1409: typedef _Graph Graph; deba@1429: typedef typename Graph::Edge Edge; deba@1409: public: deba@1409: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for EdgeReader. It creates the EdgeReader and deba@1409: /// attach it into the given LemonReader. It will use the given deba@1409: /// edge id reader to give back the edges. The reader will read the deba@1421: /// section only if the \c _id and the \c edges_id are the same. deba@1409: template deba@1409: EdgeReader(LemonReader& _reader, const _IdReader& _idReader, deba@1409: const std::string& _id = std::string()) deba@1476: : Parent(_reader), id(_id) { deba@1476: checkConcept<_reader_bits::ItemIdReader, _IdReader>(); deba@1492: edgeIdReader.reset(new IdReader(_idReader)); deba@1476: } deba@1409: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for EdgeReader. deba@1409: virtual ~EdgeReader() {} deba@1409: private: deba@1409: EdgeReader(const EdgeReader&); deba@1409: void operator=(const EdgeReader&); deba@1409: deba@1409: public: deba@1409: deba@1409: /// \brief Add an edge reader command for the EdgeReader. deba@1409: /// deba@1409: /// Add an edge reader command for the EdgeReader. deba@1429: void readEdge(const std::string& name, Edge& item) { deba@1409: if (readers.find(name) != readers.end()) { deba@1409: ErrorMessage msg; deba@1409: msg << "Multiple read rule for edge: " << name; deba@1409: throw IOParameterError(msg.message()); deba@1409: } deba@1409: readers.insert(make_pair(name, &item)); deba@1409: } deba@1409: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1421: /// It gives back true when the header line start with \c \@edges, deba@1421: /// and the header line's id and the reader's id are the same. deba@1421: virtual bool header(const std::string& line) { deba@1421: std::istringstream ls(line); deba@1421: std::string command; deba@1421: std::string name; deba@1421: ls >> command >> name; deba@1421: return command == "@edges" && name == id; deba@1421: } deba@1421: deba@1421: /// \brief Reader function of the section. deba@1421: /// deba@1421: /// It reads the content of the section. deba@1421: virtual void read(std::istream& is) { deba@1492: if (!edgeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find edgeset or ID map"); deba@1476: } deba@1421: std::string line; deba@1421: while (getline(is, line)) { deba@1421: std::istringstream ls(line); deba@1421: std::string id; deba@1421: ls >> id; deba@1429: typename EdgeReaders::iterator it = readers.find(id); deba@1421: if (it != readers.end()) { deba@1492: *(it->second) = edgeIdReader->read(ls); deba@1421: } deba@1421: } deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: std::string id; deba@1421: deba@1429: typedef std::map EdgeReaders; deba@1429: EdgeReaders readers; deba@1492: std::auto_ptr > edgeIdReader; deba@1421: }; deba@1421: deba@1421: /// \ingroup io_group deba@1421: /// \brief SectionReader for reading labeled undirected edges. deba@1421: /// deba@1421: /// The undirected edges section's header line is \c \@undiredges deba@1421: /// \c undiredges_id, but the \c undiredges_id may be empty. deba@1421: /// deba@1421: /// Each line in the section contains the name of the undirected edge deba@1421: /// and then the undirected edge id. deba@1421: /// deba@1421: /// \relates LemonReader deba@1421: template deba@1421: class UndirEdgeReader : public CommonSectionReaderBase { deba@1421: typedef CommonSectionReaderBase Parent; deba@1421: typedef _Graph Graph; deba@1429: typedef typename Graph::Edge Edge; deba@1429: typedef typename Graph::UndirEdge UndirEdge; deba@1421: public: deba@1421: deba@1421: /// \brief Constructor. deba@1421: /// deba@1421: /// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and deba@1421: /// attach it into the given LemonReader. It will use the given deba@1421: /// undirected edge id reader to give back the edges. The reader will deba@1421: /// read the section only if the \c _id and the \c undiredges_id are deba@1421: /// the same. deba@1421: template deba@1421: UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, deba@1421: const std::string& _id = std::string()) deba@1476: : Parent(_reader), id(_id) { deba@1476: checkConcept<_reader_bits::ItemIdReader, _IdReader>(); deba@1476: checkConcept<_reader_bits::ItemIdReader, _IdReader>(); deba@1476: undirEdgeIdReader.reset(new IdReader(_idReader)); deba@1477: edgeIdReader.reset(new IdReader(_idReader)); deba@1476: } deba@1421: deba@1421: /// \brief Destructor. deba@1421: /// deba@1421: /// Destructor for UndirEdgeReader. deba@1421: virtual ~UndirEdgeReader() {} deba@1421: private: deba@1421: UndirEdgeReader(const UndirEdgeReader&); deba@1421: void operator=(const UndirEdgeReader&); deba@1421: deba@1421: public: deba@1421: deba@1421: /// \brief Add an undirected edge reader command for the UndirEdgeReader. deba@1421: /// deba@1421: /// Add an undirected edge reader command for the UndirEdgeReader. deba@1429: void readUndirEdge(const std::string& name, UndirEdge& item) { deba@1429: if (undirEdgeReaders.find(name) != undirEdgeReaders.end()) { deba@1429: ErrorMessage msg; deba@1429: msg << "Multiple read rule for undirected edge: " << name; deba@1429: throw IOParameterError(msg.message()); deba@1429: } deba@1429: undirEdgeReaders.insert(make_pair(name, &item)); deba@1429: } deba@1429: deba@1429: /// \brief Add an edge reader command for the UndirEdgeReader. deba@1429: /// deba@1429: /// Add an edge reader command for the UndirEdgeReader. deba@1429: void readEdge(const std::string& name, Edge& item) { deba@1429: if (edgeReaders.find(name) != edgeReaders.end()) { deba@1421: ErrorMessage msg; deba@1421: msg << "Multiple read rule for edge: " << name; deba@1421: throw IOParameterError(msg.message()); deba@1421: } deba@1429: edgeReaders.insert(make_pair(name, &item)); deba@1421: } deba@1421: deba@1421: protected: deba@1421: deba@1421: /// \brief Gives back true when the SectionReader can process deba@1421: /// the section with the given header line. deba@1421: /// deba@1421: /// It gives back true when the header line start with \c \@edges, deba@1409: /// and the header line's id and the reader's id are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1409: std::string name; deba@1409: ls >> command >> name; deba@1429: return command == "@undiredges" && name == id; deba@1409: } deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1409: virtual void read(std::istream& is) { deba@1476: if (!edgeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find undirected edgeset or ID map"); deba@1476: } deba@1476: if (!undirEdgeIdReader->isIdReader()) { deba@1476: throw DataFormatError("Cannot find undirected edgeset or ID map"); deba@1476: } deba@1409: std::string line; deba@1409: while (getline(is, line)) { deba@1409: std::istringstream ls(line); deba@1409: std::string id; deba@1409: ls >> id; deba@1429: { deba@1429: typename UndirEdgeReaders::iterator it = undirEdgeReaders.find(id); deba@1429: if (it != undirEdgeReaders.end()) { deba@1429: *(it->second) = undirEdgeIdReader->read(ls); deba@1429: break; deba@1429: } deba@1429: } { deba@1429: typename EdgeReaders::iterator it = edgeReaders.find(id); deba@1429: if (it != edgeReaders.end()) { deba@1429: *(it->second) = edgeIdReader->read(ls); deba@1429: break; deba@1429: } deba@1429: } deba@1409: } deba@1409: } deba@1409: deba@1409: private: deba@1409: deba@1409: std::string id; deba@1409: deba@1429: typedef std::map UndirEdgeReaders; deba@1429: UndirEdgeReaders undirEdgeReaders; deba@1429: std::auto_ptr > undirEdgeIdReader; deba@1429: deba@1429: typedef std::map EdgeReaders; deba@1429: EdgeReaders edgeReaders; deba@1429: std::auto_ptr > edgeIdReader; deba@1409: }; deba@1409: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for attributes. deba@1409: /// deba@1409: /// The lemon format can store multiple attribute set. Each set has deba@1409: /// the header line \c \@attributes \c attributeset_id, but the deba@1409: /// attributeset_id may be empty. deba@1409: /// deba@1409: /// The attributeset section contains several lines. Each of them starts deba@1409: /// with an attribute and then a the value for the id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class AttributeReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: typedef _Traits Traits; deba@1408: public: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for AttributeReader. It creates the AttributeReader and deba@1409: /// attach it into the given LemonReader. The reader process a section deba@1409: /// only if the \c section_id and the \c _id are the same. deba@1408: AttributeReader(LemonReader& _reader, deba@1409: const std::string& _id = std::string()) deba@1409: : Parent(_reader), id(_id) {} deba@1408: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for AttributeReader. deba@1408: virtual ~AttributeReader() { deba@1408: for (typename Readers::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: AttributeReader(const AttributeReader&); deba@1408: void operator=(AttributeReader&); deba@1408: deba@1408: public: deba@1409: /// \brief Add an attribute reader command for the reader. deba@1409: /// deba@1409: /// Add an attribute reader command for the reader. deba@1408: template deba@1408: AttributeReader& readAttribute(const std::string& id, Value& value) { deba@1408: return readAttribute > deba@1408: (id, value); deba@1408: } deba@1408: deba@1409: /// \brief Add an attribute reader command for the reader. deba@1409: /// deba@1409: /// Add an attribute reader command for the reader. deba@1408: template deba@1408: AttributeReader& readAttribute(const std::string& name, Value& value, deba@1408: const Reader& reader = Reader()) { deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for attribute: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert(make_pair(name, new ValueReader deba@1408: (value, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1421: /// It gives back true when the header line start with \c \@attributes, deba@1409: /// and the header line's id and the attributeset's id are the same. deba@1408: bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@attributes" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: void read(std::istream& is) { deba@1408: std::string line; deba@1408: while (getline(is, line)) { deba@1408: std::istringstream ls(line); deba@1408: std::string id; deba@1408: ls >> id; deba@1408: typename Readers::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: it->second->read(ls); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: std::string id; deba@1408: deba@1408: typedef std::map Readers; deba@1409: Readers readers; deba@1408: }; deba@1408: deba@1423: /// \ingroup io_group deba@1423: /// \brief SectionReader for retrieve what is in the file. deba@1423: /// deba@1423: /// SectionReader for retrieve what is in the file. If you want deba@1423: /// to know which sections, maps and items are in the file deba@1423: /// use the next code: deba@1423: /// \code deba@1423: /// LemonReader reader("input.lgf"); deba@1423: /// ContentReader content(reader); deba@1423: /// reader.run(); deba@1423: /// \endcode deba@1423: class ContentReader : public LemonReader::SectionReader { deba@1423: typedef LemonReader::SectionReader Parent; deba@1423: public: deba@1423: /// \brief Constructor. deba@1423: /// deba@1423: /// Constructor for deba@1423: ContentReader(LemonReader& _reader) : Parent(_reader) {} deba@1423: deba@1423: /// \brief Desctructor. deba@1423: /// deba@1423: /// Desctructor. deba@1423: virtual ~ContentReader() {} deba@1423: deba@1423: /// \brief Gives back how many nodesets are in the file. deba@1423: /// deba@1423: /// Gives back how many nodesets are in the file. deba@1423: int nodeSetNum() const { deba@1423: return nodesets.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of nodeset on the indiced position. deba@1423: /// deba@1423: /// Gives back the name of nodeset on the indiced position. deba@1423: std::string nodeSetName(int index) const { deba@1423: return nodesets[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the map names of nodeset on the indiced position. deba@1423: /// deba@1423: /// Gives back the map names of nodeset on the indiced position. deba@1423: const std::vector& nodeSetMaps(int index) const { deba@1423: return nodesets[index].items; deba@1423: } deba@1423: deba@1423: /// \brief Gives back how many edgesets are in the file. deba@1423: /// deba@1423: /// Gives back how many edgesets are in the file. deba@1423: int edgeSetNum() const { deba@1423: return edgesets.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of edgeset on the indiced position. deba@1423: /// deba@1423: /// Gives back the name of edgeset on the indiced position. deba@1423: std::string edgeSetName(int index) const { deba@1423: return edgesets[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the map names of edgeset on the indiced position. deba@1423: /// deba@1423: /// Gives back the map names of edgeset on the indiced position. deba@1423: const std::vector& edgeSetMaps(int index) const { deba@1423: return edgesets[index].items; deba@1423: } deba@1423: deba@1423: /// \brief Gives back how many undirected edgesets are in the file. deba@1423: /// deba@1423: /// Gives back how many undirected edgesets are in the file. deba@1423: int undirEdgeSetNum() const { deba@1423: return undiredgesets.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of undirected edgeset on the indiced deba@1423: /// position. deba@1423: /// deba@1423: /// Gives back the name of undirected edgeset on the indiced position. deba@1423: std::string undirEdgeSetName(int index) const { deba@1423: return undiredgesets[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the map names of undirected edgeset on the indiced deba@1423: /// position. deba@1423: /// deba@1423: /// Gives back the map names of undirected edgeset on the indiced position. deba@1423: const std::vector& undirEdgeSetMaps(int index) const { deba@1423: return undiredgesets[index].items; deba@1423: } deba@1423: deba@1423: /// \brief Gives back how many labeled nodes section are in the file. deba@1423: /// deba@1423: /// Gives back how many labeled nodes section are in the file. deba@1423: int nodesNum() const { deba@1423: return nodes.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of labeled nodes section on the indiced deba@1423: /// position. deba@1423: /// deba@1423: /// Gives back the name of labeled nodes section on the indiced position. deba@1423: std::string nodesName(int index) const { deba@1423: return nodes[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the names of the labeled nodes in the indiced deba@1423: /// section. deba@1423: /// deba@1423: /// Gives back the names of the labeled nodes in the indiced section. deba@1423: const std::vector& nodesItems(int index) const { deba@1423: return nodes[index].items; deba@1423: } deba@1423: deba@1423: /// \brief Gives back how many labeled edges section are in the file. deba@1423: /// deba@1423: /// Gives back how many labeled edges section are in the file. deba@1423: int edgesNum() const { deba@1423: return edges.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of labeled edges section on the indiced deba@1423: /// position. deba@1423: /// deba@1423: /// Gives back the name of labeled edges section on the indiced position. deba@1423: std::string edgesName(int index) const { deba@1423: return edges[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the names of the labeled edges in the indiced deba@1423: /// section. deba@1423: /// deba@1423: /// Gives back the names of the labeled edges in the indiced section. deba@1423: const std::vector& edgesItems(int index) const { deba@1423: return edges[index].items; deba@1423: } deba@1423: deba@1423: /// \brief Gives back how many labeled undirected edges section are deba@1423: /// in the file. deba@1423: /// deba@1423: /// Gives back how many labeled undirected edges section are in the file. deba@1423: int undirEdgesNum() const { deba@1423: return undiredges.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of labeled undirected edges section deba@1423: /// on the indiced position. deba@1423: /// deba@1423: /// Gives back the name of labeled undirected edges section on the deba@1423: /// indiced position. deba@1423: std::string undirEdgesName(int index) const { deba@1423: return undiredges[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the names of the labeled undirected edges in deba@1423: /// the indiced section. deba@1423: /// deba@1423: /// Gives back the names of the labeled undirected edges in the deba@1423: /// indiced section. deba@1423: const std::vector& undirEdgesItems(int index) const { deba@1423: return undiredges[index].items; deba@1423: } deba@1423: deba@1423: deba@1423: /// \brief Gives back how many attributes section are in the file. deba@1423: /// deba@1423: /// Gives back how many attributes section are in the file. deba@1423: int attributesNum() const { deba@1423: return attributes.size(); deba@1423: } deba@1423: deba@1423: /// \brief Gives back the name of attributes section on the indiced deba@1423: /// position. deba@1423: /// deba@1423: /// Gives back the name of attributes section on the indiced position. deba@1423: std::string attributesName(int index) const { deba@1423: return attributes[index].name; deba@1423: } deba@1423: deba@1423: /// \brief Gives back the names of the attributes in the indiced section. deba@1423: /// deba@1423: /// Gives back the names of the attributes in the indiced section. deba@1423: const std::vector& attributesItems(int index) const { deba@1423: return attributes[index].items; deba@1423: } deba@1423: deba@1423: const std::vector& otherSections() const { deba@1423: return sections; deba@1423: } deba@1423: deba@1423: protected: deba@1423: deba@1423: /// \brief Gives back true when the SectionReader can process deba@1423: /// the section with the given header line. deba@1423: /// deba@1423: /// It gives back true when the section is common section. deba@1423: bool header(const std::string& line) { deba@1423: std::istringstream ls(line); deba@1423: std::string command, name; deba@1423: ls >> command >> name; deba@1423: if (command == "@nodeset") { deba@1423: current = command; deba@1423: nodesets.push_back(SectionInfo(name)); deba@1423: } else if (command == "@edgeset") { deba@1423: current = command; deba@1423: edgesets.push_back(SectionInfo(name)); deba@1423: } else if (command == "@undiredgeset") { deba@1423: current = command; deba@1423: undiredgesets.push_back(SectionInfo(name)); deba@1423: } else if (command == "@nodes") { deba@1423: current = command; deba@1423: nodes.push_back(SectionInfo(name)); deba@1423: } else if (command == "@edges") { deba@1423: current = command; deba@1423: edges.push_back(SectionInfo(name)); deba@1423: } else if (command == "@undiredges") { deba@1423: current = command; deba@1423: undiredges.push_back(SectionInfo(name)); deba@1423: } else if (command == "@attributes") { deba@1423: current = command; deba@1423: attributes.push_back(SectionInfo(name)); deba@1423: } else { deba@1423: sections.push_back(line); deba@1423: return false; deba@1423: } deba@1423: return true; deba@1423: } deba@1423: deba@1423: /// \brief Retrieve the items from various sections. deba@1423: /// deba@1423: /// Retrieve the items from various sections. deba@1423: void read(std::istream& is) { deba@1423: if (current == "@nodeset") { deba@1423: readMapNames(is, nodesets.back().items); deba@1423: } else if (current == "@edgeset") { deba@1423: readMapNames(is, edgesets.back().items); deba@1423: } else if (current == "@undiredgeset") { deba@1423: readMapNames(is, undiredgesets.back().items); deba@1423: } else if (current == "@nodes") { deba@1423: readItemNames(is, nodes.back().items); deba@1423: } else if (current == "@edges") { deba@1423: readItemNames(is, edges.back().items); deba@1423: } else if (current == "@undiredges") { deba@1423: readItemNames(is, undiredges.back().items); deba@1423: } else if (current == "@attributes") { deba@1423: readItemNames(is, attributes.back().items); deba@1423: } deba@1423: } deba@1423: deba@1423: private: deba@1423: deba@1423: void readMapNames(std::istream& is, std::vector& maps) { deba@1423: std::string line, id; deba@1423: std::getline(is, line); deba@1423: std::istringstream ls(line); deba@1423: while (ls >> id) { deba@1423: maps.push_back(id); deba@1423: } deba@1423: while (getline(is, line)); deba@1423: } deba@1423: deba@1423: void readItemNames(std::istream& is, std::vector& maps) { deba@1423: std::string line, id; deba@1423: while (std::getline(is, line)) { deba@1423: std::istringstream ls(line); deba@1423: ls >> id; deba@1423: maps.push_back(id); deba@1423: } deba@1423: } deba@1423: deba@1423: struct SectionInfo { deba@1423: std::string name; deba@1423: std::vector items; deba@1423: deba@1423: SectionInfo(const std::string& _name) : name(_name) {} deba@1423: }; deba@1423: deba@1423: std::vector nodesets; deba@1423: std::vector edgesets; deba@1423: std::vector undiredgesets; deba@1423: deba@1423: std::vector nodes; deba@1423: std::vector edges; deba@1423: std::vector undiredges; deba@1423: deba@1423: std::vector attributes; deba@1423: deba@1423: std::vector sections; deba@1423: deba@1423: std::string current; deba@1423: deba@1423: }; deba@1423: deba@1408: } deba@1408: #endif