deba@1408: /* -*- C++ -*- deba@1408: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * 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@2084: ///\ingroup lemon_io 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@1993: #include deba@1409: #include deba@1408: alpar@2207: #include deba@1705: deba@1476: #include alpar@2260: #include deba@1408: deba@1408: namespace lemon { deba@1408: deba@1421: namespace _reader_bits { deba@1476: deba@1845: template deba@1845: bool operator<(T, T) { deba@1901: throw DataFormatError("Label is not comparable"); deba@1845: } deba@1845: deba@1845: template deba@1845: struct Less { deba@1845: bool operator()(const T& p, const T& q) const { deba@1845: return p < q; deba@1845: } deba@1845: }; deba@1845: deba@1476: template deba@1901: class ItemLabelReader { deba@1476: public: deba@1476: deba@1901: bool isLabelReader() { return true; } deba@1476: deba@1901: void readLabel(std::istream&, Item&) {} deba@1476: deba@1901: template deba@1476: struct Constraints { deba@1476: void constraints() { deba@1901: bool b = reader.isLabelReader(); deba@1476: ignore_unused_variable_warning(b); deba@1492: Item item; deba@1901: reader.readLabel(is, item); deba@1476: } deba@1901: _ItemLabelReader& 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@1705: template deba@1705: struct Ref { typedef Map& Type; }; deba@1705: template deba@1705: struct Arg { typedef Map& Type; }; deba@1705: deba@1705: template deba@1705: class ForwardComposeMap { deba@1421: public: klao@1909: typedef typename Graph::UEdge Key; deba@1705: typedef typename Map::Value Value; deba@1705: deba@1705: ForwardComposeMap(const Graph& _graph, typename Arg::Type _map) deba@1705: : graph(_graph), map(_map) {} deba@1421: deba@1705: void set(const Key& key, const Value& val) { deba@1705: map.set(graph.direct(key, true), val); deba@1421: } deba@1421: deba@1421: private: deba@2282: const Graph& graph; deba@1705: typename Ref::Type map; deba@1421: }; deba@1421: deba@1705: template deba@1705: ForwardComposeMap deba@1705: forwardComposeMap(const Graph& graph, const Map& map) { deba@1705: return ForwardComposeMap(graph, map); deba@1421: } deba@1421: deba@1705: template deba@1705: ForwardComposeMap deba@1705: forwardComposeMap(const Graph& graph, Map& map) { deba@1705: return ForwardComposeMap(graph, map); deba@1421: } deba@1705: deba@1705: template deba@1705: class BackwardComposeMap { deba@1705: public: klao@1909: typedef typename Graph::UEdge Key; deba@1705: typedef typename Map::Value Value; deba@1705: deba@1705: BackwardComposeMap(const Graph& _graph, typename Arg::Type _map) deba@1705: : graph(_graph), map(_map) {} deba@1705: deba@1705: void set(const Key& key, const Value& val) { deba@1705: map.set(graph.direct(key, false), val); deba@1705: } deba@1705: deba@1705: private: deba@2282: const Graph& graph; deba@1705: typename Ref::Type map; deba@1705: }; deba@1705: deba@1705: deba@1705: template deba@1705: BackwardComposeMap deba@1705: backwardComposeMap(const Graph& graph, const Map& map) { deba@1705: return BackwardComposeMap(graph, map); deba@1705: } deba@1705: deba@1705: template deba@1705: BackwardComposeMap deba@1705: backwardComposeMap(const Graph& graph, Map& map) { deba@1705: return BackwardComposeMap(graph, map); deba@1705: } deba@1705: deba@1705: template deba@1705: struct Ref > { deba@1705: typedef ForwardComposeMap Type; deba@1705: }; deba@1705: template deba@1705: struct Arg > { deba@1705: typedef const ForwardComposeMap& Type; deba@1705: }; deba@1705: deba@1705: template deba@1705: struct Ref > { deba@1705: typedef BackwardComposeMap Type; deba@1705: }; deba@1705: template deba@1705: struct Arg > { deba@1705: typedef const BackwardComposeMap& Type; deba@1705: }; deba@1705: deba@1705: template alpar@2207: struct Ref > { alpar@2207: typedef dim2::XMap Type; deba@1705: }; deba@1705: template alpar@2207: struct Arg > { alpar@2207: typedef const dim2::XMap& Type; deba@1705: }; deba@1705: deba@1705: template alpar@2207: struct Ref > { alpar@2207: typedef dim2::YMap Type; deba@1705: }; deba@1705: template alpar@2207: struct Arg > { alpar@2207: typedef const dim2::YMap& Type; deba@1705: }; deba@1705: deba@1845: deba@1845: template deba@1845: class MapReaderBase; deba@1845: deba@1845: template deba@1845: class MapInverterBase : public MapReaderBase<_Item> { deba@1845: public: deba@1845: typedef _Item Item; deba@1845: virtual void read(std::istream&, const Item&) = 0; deba@1845: virtual Item read(std::istream&) const = 0; deba@1845: deba@1845: virtual MapInverterBase<_Item>* getInverter() { deba@1845: return this; deba@1845: } deba@1845: }; deba@1845: deba@1845: template deba@1845: class MapReaderInverter : public MapInverterBase<_Item> { deba@1845: public: deba@1845: typedef _Item Item; deba@1845: typedef _Reader Reader; deba@1845: typedef typename Reader::Value Value; deba@1845: typedef _Map Map; deba@1845: typedef std::map > Inverse; deba@1845: deba@1845: typename _reader_bits::Ref::Type map; deba@1845: Reader reader; deba@1845: Inverse inverse; deba@1845: deba@1845: MapReaderInverter(typename _reader_bits::Arg::Type _map, deba@1845: const Reader& _reader) deba@1845: : map(_map), reader(_reader) {} deba@1845: deba@1845: virtual ~MapReaderInverter() {} deba@1845: deba@1845: virtual void read(std::istream& is, const Item& item) { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: map.set(item, value); deba@1845: typename Inverse::iterator it = inverse.find(value); deba@1845: if (it == inverse.end()) { deba@1845: inverse.insert(std::make_pair(value, item)); deba@1845: } else { deba@1901: throw DataFormatError("Multiple label occurence"); deba@1845: } deba@1845: } deba@1845: deba@1845: virtual Item read(std::istream& is) const { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: typename Inverse::const_iterator it = inverse.find(value); deba@1845: if (it != inverse.end()) { deba@1845: return it->second; deba@1845: } else { deba@1901: throw DataFormatError("Invalid label error"); deba@1845: } deba@1845: } deba@1845: }; deba@1845: deba@1845: template deba@1845: class SkipReaderInverter : public MapInverterBase<_Item> { deba@1845: public: deba@1845: typedef _Item Item; deba@1845: typedef _Reader Reader; deba@1845: typedef typename Reader::Value Value; deba@1845: typedef std::map > Inverse; deba@1845: deba@1845: Reader reader; deba@1845: deba@1845: SkipReaderInverter(const Reader& _reader) deba@1845: : reader(_reader) {} deba@1845: deba@1845: virtual ~SkipReaderInverter() {} deba@1845: deba@1845: virtual void read(std::istream& is, const Item& item) { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: typename Inverse::iterator it = inverse.find(value); deba@1845: if (it == inverse.end()) { deba@1845: inverse.insert(std::make_pair(value, item)); deba@1845: } else { deba@1901: throw DataFormatError("Multiple label occurence error"); deba@1845: } deba@1845: } deba@1845: deba@1845: virtual Item read(std::istream& is) const { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: typename Inverse::const_iterator it = inverse.find(value); deba@1845: if (it != inverse.end()) { deba@1845: return it->second; deba@1845: } else { deba@1901: throw DataFormatError("Invalid label error"); deba@1845: } deba@1845: } deba@1845: deba@1845: private: deba@1845: Inverse inverse; deba@1845: }; deba@1845: deba@1845: template deba@1845: class MapReaderBase { deba@1845: public: deba@1845: typedef _Item Item; deba@1845: deba@1845: MapReaderBase() { _touched = false; } deba@1845: deba@1845: void touch() { _touched = true; } deba@1845: bool touched() const { return _touched; } deba@1845: deba@1845: virtual ~MapReaderBase() {} deba@1845: deba@1845: virtual void read(std::istream& is, const Item& item) = 0; deba@1845: virtual MapInverterBase<_Item>* getInverter() = 0; deba@1845: deba@1845: private: deba@1845: bool _touched; deba@1845: deba@1845: }; deba@1845: deba@1845: template deba@1845: class MapReader : public MapReaderBase<_Item> { deba@1845: public: deba@1845: typedef _Map Map; deba@1845: typedef _Reader Reader; deba@1845: typedef typename Reader::Value Value; deba@1845: typedef _Item Item; deba@1845: deba@1845: typename _reader_bits::Ref::Type map; deba@1845: Reader reader; deba@1845: deba@1845: MapReader(typename _reader_bits::Arg::Type _map, deba@1845: const Reader& _reader) deba@1845: : map(_map), reader(_reader) {} deba@1845: deba@1845: virtual ~MapReader() {} deba@1845: deba@1845: virtual void read(std::istream& is, const Item& item) { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: map.set(item, value); deba@1845: } deba@1845: deba@1845: virtual MapInverterBase<_Item>* getInverter() { deba@1845: return new MapReaderInverter(map, reader); deba@1845: } deba@1845: }; deba@1845: deba@1845: deba@1845: template deba@1845: class SkipReader : public MapReaderBase<_Item> { deba@1845: public: deba@1845: typedef _Reader Reader; deba@1845: typedef typename Reader::Value Value; deba@1845: typedef _Item Item; deba@1845: deba@1845: Reader reader; deba@1845: SkipReader(const Reader& _reader) : reader(_reader) {} deba@1845: deba@1845: virtual ~SkipReader() {} deba@1845: deba@1845: virtual void read(std::istream& is, const Item&) { deba@1845: Value value; deba@1845: reader.read(is, value); deba@1845: } deba@1845: deba@1845: virtual MapInverterBase* getInverter() { deba@1845: return new SkipReaderInverter(reader); deba@1845: } deba@1845: }; deba@1845: deba@1845: template deba@1901: class LabelReaderBase { deba@1845: public: deba@1845: typedef _Item Item; deba@1901: virtual ~LabelReaderBase() {} deba@1845: virtual Item read(std::istream& is) const = 0; deba@1901: virtual bool isLabelReader() const = 0; deba@1845: }; deba@1845: deba@1901: template deba@1901: class LabelReader : public LabelReaderBase<_Item> { deba@1845: public: deba@1845: typedef _Item Item; deba@1901: typedef _BoxedLabelReader BoxedLabelReader; deba@1845: deba@1901: const BoxedLabelReader& boxedLabelReader; deba@1845: deba@1901: LabelReader(const BoxedLabelReader& _boxedLabelReader) deba@1901: : boxedLabelReader(_boxedLabelReader) {} deba@1845: deba@1845: virtual Item read(std::istream& is) const { deba@1845: Item item; deba@1901: boxedLabelReader.readLabel(is, item); deba@1845: return item; deba@1845: } deba@1845: deba@1901: virtual bool isLabelReader() const { deba@1901: return boxedLabelReader.isLabelReader(); deba@1845: } deba@1845: }; deba@1845: deba@1845: template deba@1845: class ItemStore { deba@1845: public: deba@1845: deba@1845: typedef _Item Item; deba@1845: deba@1845: ItemStore(Item& _item) : item(&_item) { deba@1845: _touched = false; deba@1845: } deba@1845: deba@1845: void touch() { _touched = true; } deba@1845: bool touched() const { return _touched; } deba@1845: deba@1845: void read(const Item& _item) { deba@1845: *item = _item; deba@1845: } deba@1845: deba@1845: private: deba@1845: Item* item; deba@1845: bool _touched; deba@1845: }; deba@1845: deba@1845: class ValueReaderBase { deba@1845: public: deba@1845: virtual void read(std::istream&) {}; deba@1846: ValueReaderBase() { _touched = false; } deba@1846: deba@1846: void touch() { _touched = true; } deba@1846: bool touched() const { return _touched; } deba@1846: deba@1845: virtual ~ValueReaderBase() {} deba@1846: private: deba@1846: bool _touched; deba@1845: }; deba@1845: deba@1845: template deba@1845: class ValueReader : public ValueReaderBase { deba@1845: public: deba@1845: typedef _Value Value; deba@1845: typedef _Reader Reader; deba@1845: deba@1845: ValueReader(Value& _value, const Reader& _reader) deba@1845: : value(_value), reader(_reader) {} deba@1845: deba@1845: virtual void read(std::istream& is) { deba@1845: reader.read(is, value); deba@1845: } deba@1845: private: deba@1845: Value& value; deba@1845: Reader reader; deba@1845: }; deba@1845: deba@1421: } deba@1421: deba@2084: /// \ingroup lemon_io 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@2084: int_type blen() { return _eptr - _base; } deba@1408: deba@2084: void setb(char_type* buf, int_type len) { deba@1408: _base = buf; deba@1408: _eptr = buf + len; deba@1408: } deba@1408: ladanyi@1722: virtual std::streambuf* setbuf(char *buf, std::streamsize 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@2084: virtual int_type 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@2084: virtual int_type 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@2000: deba@2000: /// \brief The given section missing in the file. deba@2000: /// deba@2000: /// The given section missing in the file. deba@2000: virtual void missing() {}; 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@1746: if (is->fail()) { deba@1746: throw FileOpenError(filename); deba@1746: } 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@2000: readers.push_back(std::make_pair(&reader, false)); 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@2000: SectionReaders::iterator it; deba@1408: while ((++line_num, getline(*is, line)) && line.find("@end") != 0) { deba@1408: for (it = readers.begin(); it != readers.end(); ++it) { deba@2000: if (it->first->header(line)) { deba@2000: it->second = true; 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@2000: it->first->read(is); deba@1408: break; deba@1408: } deba@1408: } deba@1408: } deba@2000: for (it = readers.begin(); it != readers.end(); ++it) { deba@2000: if (!it->second) { deba@2000: it->first->missing(); deba@2000: } deba@2000: } deba@1408: } catch (DataFormatError& error) { deba@1408: error.line(line_num); klao@2009: throw; 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@2000: typedef std::vector > SectionReaders; deba@1408: SectionReaders readers; deba@1408: deba@1408: }; deba@1408: deba@2016: /// \ingroup section_io 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@1901: /// The nodeset section's header line is \c \@nodeset \c nodeset_name, but the deba@1901: /// \c nodeset_name 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@1901: /// If the nodeset contains an \c "label" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1901: /// \c readLabel() 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@1845: class NodeSetReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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@2100: /// add the read nodes to the given Graph. The reader will read deba@1901: /// the section when the \c section_name and the \c _name are the same. deba@1421: NodeSetReader(LemonReader& _reader, deba@1705: Graph& _graph, deba@1901: const std::string& _name = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1901: : Parent(_reader), graph(_graph), name(_name), 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@1705: typename _reader_bits::Arg::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@1705: typename _reader_bits::Arg::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@1705: 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@1705: 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()) { alpar@2260: 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; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1408: readers.insert( deba@1845: make_pair(name, new _reader_bits:: deba@1845: 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; alpar@2153: throw IoParameterError(msg.message()); deba@1408: } deba@1845: readers.insert(make_pair(name, new _reader_bits:: deba@1845: 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@1901: /// and the header line's name and the nodeset's name are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; 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@1845: std::vector<_reader_bits::MapReaderBase* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1901: std::istringstream ls(line); deba@1901: std::string id; deba@1408: while (ls >> id) { deba@1408: typename MapReaders::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1845: it->second->touch(); deba@1408: index.push_back(it->second); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@2001: if (id == "label") { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1845: for (typename MapReaders::iterator it = readers.begin(); deba@1845: it != readers.end(); ++it) { deba@1845: if (!it->second->touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Map not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } 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@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "NodeSet section not found in file: @nodeset " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@2000: deba@1409: public: deba@1409: deba@1901: /// \brief Returns true if the nodeset can give back the node by its label. deba@1409: /// deba@1901: /// Returns true if the nodeset can give back the node by its label. deba@1901: /// It is possible only if an "label" named map was read. deba@1901: bool isLabelReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1901: /// \brief Gives back the node by its label. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which node belongs to deba@1901: /// it. It is possible only if there was read an "label" named map. deba@1901: void readLabel(std::istream& is, Node& node) const { deba@1492: node = inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@1845: typedef std::map*> MapReaders; deba@1408: MapReaders readers; deba@1408: deba@1705: Graph& graph; deba@1901: std::string name; deba@1845: _reader_bits::SkipReader skipper; deba@1408: deba@1845: std::auto_ptr<_reader_bits::MapInverterBase > inverter; deba@1408: }; deba@1408: deba@2016: /// \ingroup section_io 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@1901: /// The edgeset section's header line is \c \@edgeset \c edgeset_name, but the deba@1901: /// \c edgeset_name 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@1901: /// If the edgeset contains an \c "label" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1901: /// \c readLabel() 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@1901: /// have to be connected. If a NodeSetReader reads an "label" named map, deba@1409: /// it will be able to resolve the nodes by ids. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1845: class EdgeSetReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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@2100: /// add the read 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@1901: /// The reader will read the section only if the \c _name and the deba@1901: /// \c edgset_name are the same. deba@1901: template deba@1421: EdgeSetReader(LemonReader& _reader, deba@1705: Graph& _graph, deba@1901: const NodeLabelReader& _nodeLabelReader, deba@1901: const std::string& _name = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1901: : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { deba@1901: checkConcept<_reader_bits::ItemLabelReader, NodeLabelReader>(); deba@1901: nodeLabelReader.reset(new _reader_bits:: deba@1901: LabelReader(_nodeLabelReader)); 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@1705: typename _reader_bits::Arg::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@1705: typename _reader_bits::Arg::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()) { alpar@2260: 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; alpar@2153: throw IoParameterError(msg.message()); deba@1408: } deba@1408: readers.insert( deba@1845: make_pair(name, new _reader_bits:: deba@1845: 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; alpar@2153: throw IoParameterError(msg.message()); deba@1408: } deba@1845: readers.insert(make_pair(name, new _reader_bits:: deba@1845: 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@1901: /// and the header line's name and the edgeset's name are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; 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@1901: if (!nodeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find nodeset or label map"); deba@1476: } deba@1845: std::vector<_reader_bits::MapReaderBase* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1408: std::istringstream ls(line); deba@1901: std::string id; 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@1845: it->second->touch(); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@2001: if (id == "label") { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1845: for (typename MapReaders::iterator it = readers.begin(); deba@1845: it != readers.end(); ++it) { deba@1845: if (!it->second->touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Map not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } deba@1408: while (getline(is, line)) { deba@1408: std::istringstream ls(line); deba@1901: Node from = nodeLabelReader->read(ls); deba@1901: Node to = nodeLabelReader->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@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "EdgeSet section not found in file: @edgeset " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@2000: deba@1409: public: deba@1409: deba@1901: /// \brief Returns true if the edgeset can give back the edge by its label. deba@1409: /// deba@1901: /// Returns true if the edgeset can give back the edge by its label. deba@1901: /// It is possible only if an "label" named map was read. deba@1901: bool isLabelReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1901: /// \brief Gives back the edge by its label. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which edge belongs to deba@1901: /// it. It is possible only if there was read an "label" named map. deba@1901: void readLabel(std::istream& is, Edge& edge) const { deba@1492: edge = inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@2001: typedef std::map*> deba@2001: MapReaders; deba@2001: deba@1408: MapReaders readers; deba@1408: deba@1705: Graph& graph; deba@1901: std::string name; deba@1845: _reader_bits::SkipReader skipper; deba@1421: deba@1845: std::auto_ptr<_reader_bits::MapInverterBase > inverter; deba@1901: std::auto_ptr<_reader_bits::LabelReaderBase > nodeLabelReader; deba@1421: }; deba@1421: deba@2016: /// \ingroup section_io 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 klao@1909: /// maps. The undirected edgeset section's header line is \c \@uedgeset klao@1909: /// \c uedgeset_name, but the \c uedgeset_name 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@1901: /// If the edgeset contains an \c "label" named map then it will be regarded deba@1421: /// as id map. This map should contain only unique values and when the deba@1901: /// \c readLabel() member will read a value from the given stream it will klao@1909: /// give back that uicted edge which is mapped to this value. deba@1421: /// deba@1421: /// The undirected edgeset reader needs a node id reader to identify which deba@2001: /// nodes have to be connected. If a NodeSetReader reads an "label" named deba@2001: /// map, it will be able to resolve the nodes by ids. deba@1421: /// deba@1421: /// \relates LemonReader deba@1421: template klao@1909: class UEdgeSetReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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; klao@1909: typedef typename Graph::UEdge UEdge; deba@1421: typedef typename Traits::Skipper DefaultSkipper; deba@1421: deba@1421: /// \brief Constructor. deba@1421: /// klao@1909: /// Constructor for UEdgeSetReader. It creates the UEdgeSetReader deba@1421: /// and attach it into the given LemonReader. The undirected edgeset deba@2100: /// reader will add the read 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 klao@1909: /// \c _name and the \c uedgset_name are the same. deba@1901: template klao@1909: UEdgeSetReader(LemonReader& _reader, deba@1705: Graph& _graph, deba@1901: const NodeLabelReader& _nodeLabelReader, deba@1901: const std::string& _name = std::string(), deba@1421: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1901: : Parent(_reader), graph(_graph), name(_name), skipper(_skipper) { deba@1901: checkConcept<_reader_bits::ItemLabelReader, NodeLabelReader>(); deba@1901: nodeLabelReader.reset(new _reader_bits:: deba@1901: LabelReader(_nodeLabelReader)); deba@1476: } deba@1421: /// \brief Destructor. deba@1421: /// klao@1909: /// Destructor for UEdgeSetReader. klao@1909: virtual ~UEdgeSetReader() { 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: klao@1909: UEdgeSetReader(const UEdgeSetReader&); klao@1909: void operator=(const UEdgeSetReader&); 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 klao@1909: UEdgeSetReader& readUEdgeMap(std::string name, Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1705: typename _reader_bits::Arg::Type>(name, map); deba@1421: } deba@1421: deba@1421: template klao@1909: UEdgeSetReader& readUEdgeMap(std::string name, const Map& map) { deba@1421: return _readMap< deba@1421: typename Traits::template Reader, Map, deba@1705: typename _reader_bits::Arg::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 klao@1909: UEdgeSetReader& readUEdgeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1705: return _readMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: template klao@1909: UEdgeSetReader& readUEdgeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1705: return _readMap::Type > deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template klao@1909: UEdgeSetReader& _readMap(std::string name, MapParameter map, deba@1421: const Reader& reader = Reader()) { alpar@2260: 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; alpar@2153: throw IoParameterError(msg.message()); deba@1421: } deba@1421: readers.insert( deba@1845: make_pair(name, new _reader_bits:: klao@1909: 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 klao@1909: UEdgeSetReader& skipUEdgeMap(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; alpar@2153: throw IoParameterError(msg.message()); deba@1421: } deba@1845: readers.insert(make_pair(name, new _reader_bits:: klao@1909: 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 klao@1909: UEdgeSetReader& readEdgeMap(std::string name, Map& map) { deba@1421: return _readDirMap< deba@1421: typename Traits::template Reader, Map, deba@1705: typename _reader_bits::Arg::Type>(name, map); deba@1421: } deba@1421: deba@1421: template klao@1909: UEdgeSetReader& readEdgeMap(std::string name, const Map& map) { deba@1421: return _readDirMap< deba@1421: typename Traits::template Reader, Map, deba@1705: typename _reader_bits::Arg::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 klao@1909: UEdgeSetReader& readEdgeMap(std::string name, Map& map, deba@1421: const Reader& reader = Reader()) { deba@1705: return _readDirMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: template klao@1909: UEdgeSetReader& readEdgeMap(std::string name, const Map& map, deba@1421: const Reader& reader = Reader()) { deba@1705: return _readDirMap::Type> deba@1421: (name, map, reader); deba@1421: } deba@1421: deba@1421: private: deba@1421: deba@1421: template klao@1909: UEdgeSetReader& _readDirMap(std::string name, MapParameter map, deba@1492: const Reader& reader = Reader()) { deba@1492: checkConcept<_reader_bits::ItemReader, Reader>(); alpar@2260: checkConcept, Map>(); deba@2282: readUEdgeMap("+" + name, deba@2282: _reader_bits::forwardComposeMap(graph, map), reader); deba@2282: readUEdgeMap("-" + name, deba@2282: _reader_bits::backwardComposeMap(graph, map), 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 klao@1909: UEdgeSetReader& skipEdgeMap(std::string name, deba@2282: const Reader& reader = Reader()) { deba@2282: skipUEdgeMap("+" + name, reader); deba@2282: skipUEdgeMap("-" + 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: /// klao@1909: /// It gives back true when the header line starts with \c \@uedgeset, deba@1901: /// and the header line's name and the edgeset's name are the same. deba@1421: virtual bool header(const std::string& line) { deba@1421: std::istringstream ls(line); deba@1421: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; klao@1909: return command == "@uedgeset" && 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@1901: if (!nodeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find nodeset or label map"); deba@1476: } klao@1909: std::vector<_reader_bits::MapReaderBase* > index; deba@1421: std::string line; deba@1421: deba@1421: getline(is, line); deba@1421: std::istringstream ls(line); deba@1901: std::string id; 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@1845: it->second->touch(); deba@1421: } else { deba@1421: index.push_back(&skipper); deba@1421: } deba@2001: if (id == "label") { deba@1421: inverter.reset(index.back()->getInverter()); deba@1421: index.back() = inverter.get(); deba@1421: } deba@1421: } deba@1845: for (typename MapReaders::iterator it = readers.begin(); deba@1845: it != readers.end(); ++it) { deba@1845: if (!it->second->touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Map not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } deba@1421: while (getline(is, line)) { deba@1421: std::istringstream ls(line); deba@1901: Node from = nodeLabelReader->read(ls); deba@1901: Node to = nodeLabelReader->read(ls); klao@1909: UEdge 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@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "UEdgeSet section not found in file: @uedgeset " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@2000: deba@1421: public: deba@1421: deba@1901: /// \brief Returns true if the edgeset can give back the edge by its label. deba@1421: /// deba@1421: /// Returns true if the edgeset can give back the undirected edge by its deba@1901: /// id. It is possible only if an "label" named map was read. deba@1901: bool isLabelReader() const { deba@1421: return inverter.get() != 0; deba@1421: } deba@1421: deba@1901: /// \brief Gives back the undirected edge by its label. deba@1421: /// deba@1421: /// It reads an id from the stream and gives back which undirected edge deba@1901: /// belongs to it. It is possible only if there was read an "label" named map. klao@1909: void readLabel(std::istream& is, UEdge& uEdge) const { klao@1909: uEdge = inverter->read(is); deba@1421: } deba@1421: deba@1901: /// \brief Gives back the directed edge by its label. 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@1901: /// an "label" named map. deba@1901: void readLabel(std::istream& is, Edge& edge) const { deba@1429: char c; deba@1429: is >> c; klao@1909: UEdge uEdge = inverter->read(is); deba@1429: if (c == '+') { klao@1909: edge = graph.direct(uEdge, true); deba@1429: } else if (c == '-') { klao@1909: edge = graph.direct(uEdge, 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@1845: typedef std::map*> MapReaders; deba@1421: MapReaders readers; deba@1421: deba@1705: Graph& graph; deba@1901: std::string name; klao@1909: _reader_bits::SkipReader skipper; deba@1408: klao@1909: std::auto_ptr<_reader_bits::MapInverterBase > inverter; deba@1901: std::auto_ptr<_reader_bits::LabelReaderBase > nodeLabelReader; deba@1408: }; deba@1408: deba@2016: /// \ingroup section_io deba@1409: /// \brief SectionReader for reading labeled nodes. deba@1409: /// deba@1901: /// The nodes section's header line is \c \@nodes \c nodes_name, but the deba@1901: /// \c nodes_name 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@1845: class NodeReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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@1901: /// section only if the \c _name and the \c nodes_name are the same. deba@1901: template deba@1901: NodeReader(LemonReader& _reader, const _LabelReader& _labelReader, deba@1901: const std::string& _name = std::string()) deba@1901: : Parent(_reader), name(_name) { deba@1901: checkConcept<_reader_bits::ItemLabelReader, _LabelReader>(); deba@1901: nodeLabelReader.reset(new _reader_bits:: deba@1901: LabelReader(_labelReader)); 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; alpar@2153: throw IoParameterError(msg.message()); deba@1409: } deba@1845: readers.insert(make_pair(name, _reader_bits::ItemStore(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@1901: /// and the header line's name and the reader's name are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; 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@1901: if (!nodeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find nodeset or label 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@1901: it->second.read(nodeLabelReader->read(ls)); deba@1845: it->second.touch(); deba@1409: } deba@1409: } deba@1845: for (typename NodeReaders::iterator it = readers.begin(); deba@1845: it != readers.end(); ++it) { deba@1845: if (!it->second.touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Node not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } deba@1409: } deba@2000: deba@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "Nodes section not found in file: @nodes " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@1409: deba@1409: private: deba@1409: deba@1901: std::string name; deba@1409: deba@1845: typedef std::map > NodeReaders; deba@1429: NodeReaders readers; deba@1901: std::auto_ptr<_reader_bits::LabelReaderBase > nodeLabelReader; deba@1409: }; deba@1409: deba@2016: /// \ingroup section_io deba@1409: /// \brief SectionReader for reading labeled edges. deba@1409: /// deba@1901: /// The edges section's header line is \c \@edges \c edges_name, but the deba@1901: /// \c edges_name 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@1845: class EdgeReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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@1901: /// section only if the \c _name and the \c edges_name are the same. deba@1901: template deba@1901: EdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, deba@1901: const std::string& _name = std::string()) deba@1901: : Parent(_reader), name(_name) { deba@1901: checkConcept<_reader_bits::ItemLabelReader, _LabelReader>(); deba@1901: edgeLabelReader.reset(new _reader_bits:: deba@1901: LabelReader(_labelReader)); 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; alpar@2153: throw IoParameterError(msg.message()); deba@1409: } deba@1845: readers.insert(make_pair(name, _reader_bits::ItemStore(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@1901: /// and the header line's name and the reader's name are the same. deba@1421: virtual bool header(const std::string& line) { deba@1421: std::istringstream ls(line); deba@1421: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; 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@1901: if (!edgeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find edgeset or label 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@1901: it->second.read(edgeLabelReader->read(ls)); deba@1845: it->second.touch(); deba@1421: } deba@1421: } deba@1845: for (typename EdgeReaders::iterator it = readers.begin(); deba@1845: it != readers.end(); ++it) { deba@1845: if (!it->second.touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Edge not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } deba@1421: } deba@2000: deba@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "Edges section not found in file: @edges " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@1421: deba@1421: private: deba@1421: deba@1901: std::string name; deba@1421: deba@1845: typedef std::map > EdgeReaders; deba@1429: EdgeReaders readers; deba@1901: std::auto_ptr<_reader_bits::LabelReaderBase > edgeLabelReader; deba@1421: }; deba@1421: deba@2016: /// \ingroup section_io deba@1421: /// \brief SectionReader for reading labeled undirected edges. deba@1421: /// klao@1909: /// The undirected edges section's header line is \c \@uedges klao@1909: /// \c uedges_name, but the \c uedges_name 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 klao@1909: class UEdgeReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader Parent; deba@1421: typedef _Graph Graph; deba@1429: typedef typename Graph::Edge Edge; klao@1909: typedef typename Graph::UEdge UEdge; deba@1421: public: deba@1421: deba@1421: /// \brief Constructor. deba@1421: /// klao@1909: /// Constructor for UEdgeReader. It creates the UEdgeReader 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 klao@1909: /// read the section only if the \c _name and the \c uedges_name are deba@1421: /// the same. deba@1901: template klao@1909: UEdgeReader(LemonReader& _reader, const _LabelReader& _labelReader, deba@1901: const std::string& _name = std::string()) deba@1901: : Parent(_reader), name(_name) { klao@1909: checkConcept<_reader_bits::ItemLabelReader, _LabelReader>(); deba@1901: checkConcept<_reader_bits::ItemLabelReader, _LabelReader>(); klao@1909: uEdgeLabelReader.reset(new _reader_bits:: klao@1909: LabelReader(_labelReader)); deba@1901: edgeLabelReader.reset(new _reader_bits:: deba@1901: LabelReader(_labelReader)); deba@1476: } deba@1421: deba@1421: /// \brief Destructor. deba@1421: /// klao@1909: /// Destructor for UEdgeReader. klao@1909: virtual ~UEdgeReader() {} deba@1421: private: klao@1909: UEdgeReader(const UEdgeReader&); klao@1909: void operator=(const UEdgeReader&); deba@1421: deba@1421: public: deba@1421: klao@1909: /// \brief Add an undirected edge reader command for the UEdgeReader. deba@1421: /// klao@1909: /// Add an undirected edge reader command for the UEdgeReader. klao@1909: void readUEdge(const std::string& name, UEdge& item) { klao@1909: if (uEdgeReaders.find(name) != uEdgeReaders.end()) { deba@1429: ErrorMessage msg; deba@1429: msg << "Multiple read rule for undirected edge: " << name; alpar@2153: throw IoParameterError(msg.message()); deba@1429: } klao@1909: uEdgeReaders.insert(make_pair(name, _reader_bits:: klao@1909: ItemStore(item))); deba@1429: } deba@1429: klao@1909: /// \brief Add an edge reader command for the UEdgeReader. deba@1429: /// klao@1909: /// Add an edge reader command for the UEdgeReader. 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; alpar@2153: throw IoParameterError(msg.message()); deba@1421: } deba@1845: edgeReaders.insert(make_pair(name, _reader_bits::ItemStore(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@1901: /// and the header line's name and the reader's name are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1901: std::string id; klao@1997: ls >> command >> id; klao@1909: return command == "@uedges" && 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@1901: if (!edgeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find undirected edgeset or label map"); deba@1476: } klao@1909: if (!uEdgeLabelReader->isLabelReader()) { deba@1901: throw DataFormatError("Cannot find undirected edgeset or label 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: { klao@1909: typename UEdgeReaders::iterator it = uEdgeReaders.find(id); klao@1909: if (it != uEdgeReaders.end()) { klao@1909: it->second.read(uEdgeLabelReader->read(ls)); deba@1845: it->second.touch(); deba@1845: continue; deba@1429: } deba@1429: } { deba@1429: typename EdgeReaders::iterator it = edgeReaders.find(id); deba@1429: if (it != edgeReaders.end()) { deba@1901: it->second.read(edgeLabelReader->read(ls)); deba@1845: it->second.touch(); deba@1845: continue; deba@1429: } deba@1429: } deba@1409: } deba@1845: for (typename EdgeReaders::iterator it = edgeReaders.begin(); deba@1845: it != edgeReaders.end(); ++it) { deba@1845: if (!it->second.touched()) { deba@1845: ErrorMessage msg; deba@1845: msg << "Edge not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } klao@1909: for (typename UEdgeReaders::iterator it = uEdgeReaders.begin(); klao@1909: it != uEdgeReaders.end(); ++it) { deba@1845: if (!it->second.touched()) { deba@1845: ErrorMessage msg; klao@1909: msg << "UEdge not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1845: } deba@1845: } deba@1409: } deba@2000: deba@2000: virtual void missing() { deba@2000: if (edgeReaders.empty() && uEdgeReaders.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "UEdges section not found in file: @uedges " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@1409: deba@1409: private: deba@1409: deba@1901: std::string name; deba@1409: deba@1845: typedef std::map > UEdgeReaders; klao@1909: UEdgeReaders uEdgeReaders; klao@1909: std::auto_ptr<_reader_bits::LabelReaderBase > uEdgeLabelReader; deba@1429: deba@1845: typedef std::map > EdgeReaders; deba@1429: EdgeReaders edgeReaders; deba@1901: std::auto_ptr<_reader_bits::LabelReaderBase > edgeLabelReader; deba@1409: }; deba@1409: deba@2016: /// \ingroup section_io deba@1409: /// \brief SectionReader for attributes. deba@1409: /// deba@1409: /// The lemon format can store multiple attribute set. Each set has deba@1901: /// the header line \c \@attributes \c attributeset_name, but the deba@1901: /// attributeset_name 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@1845: class AttributeReader : public LemonReader::SectionReader { deba@1845: typedef LemonReader::SectionReader 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@1901: /// only if the \c section_name and the \c _name are the same. deba@1408: AttributeReader(LemonReader& _reader, deba@1901: const std::string& _name = std::string()) deba@1901: : Parent(_reader), name(_name) {} 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; alpar@2153: throw IoParameterError(msg.message()); deba@1408: } deba@1845: readers.insert(make_pair(name, new _reader_bits:: deba@1845: ValueReader(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@1901: std::string id; klao@1997: ls >> command >> id; 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@1901: it->second->touch(); deba@1408: } deba@1408: } deba@1846: for (typename Readers::iterator it = readers.begin(); deba@1846: it != readers.end(); ++it) { deba@1846: if (!it->second->touched()) { deba@1846: ErrorMessage msg; deba@1846: msg << "Attribute not found in file: " << it->first; alpar@2153: throw IoParameterError(msg.message()); deba@1846: } deba@1846: } deba@1408: } deba@1408: deba@2000: virtual void missing() { deba@2000: if (readers.empty()) return; deba@2000: ErrorMessage msg; deba@2001: msg << "Attribute section not found in file: @attributes " << name; alpar@2153: throw IoParameterError(msg.message()); deba@2000: } deba@2000: deba@1408: private: deba@1901: std::string name; deba@1408: deba@1845: typedef std::map Readers; deba@1409: Readers readers; deba@1408: }; deba@1408: deba@2016: /// \ingroup section_io 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: alpar@1946: ///\code deba@1423: /// LemonReader reader("input.lgf"); deba@1423: /// ContentReader content(reader); deba@1423: /// reader.run(); alpar@1946: ///\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. klao@1909: int uEdgeSetNum() const { klao@1909: return uedgesets.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. klao@1909: std::string uEdgeSetName(int index) const { klao@1909: return uedgesets[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. klao@1909: const std::vector& uEdgeSetMaps(int index) const { klao@1909: return uedgesets[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. klao@1909: int uEdgesNum() const { klao@1909: return uedges.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. klao@1909: std::string uEdgesName(int index) const { klao@1909: return uedges[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. klao@1909: const std::vector& uEdgesItems(int index) const { klao@1909: return uedges[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)); klao@1909: } else if (command == "@uedgeset") { deba@1423: current = command; klao@1909: uedgesets.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)); klao@1909: } else if (command == "@uedges") { deba@1423: current = command; klao@1909: uedges.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); klao@1909: } else if (current == "@uedgeset") { klao@1909: readMapNames(is, uedgesets.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); klao@1909: } else if (current == "@uedges") { klao@1909: readItemNames(is, uedges.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@1901: std::string line, name; deba@1423: std::getline(is, line); deba@1423: std::istringstream ls(line); deba@1901: while (ls >> name) { deba@1901: maps.push_back(name); deba@1423: } deba@1423: while (getline(is, line)); deba@1423: } deba@1423: deba@1423: void readItemNames(std::istream& is, std::vector& maps) { deba@1901: std::string line, name; deba@1423: while (std::getline(is, line)) { deba@1423: std::istringstream ls(line); deba@1901: ls >> name; deba@1901: maps.push_back(name); 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; klao@1909: std::vector uedgesets; deba@1423: deba@1423: std::vector nodes; deba@1423: std::vector edges; klao@1909: std::vector uedges; 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