diff -r e37cca875667 -r 7a21e1414c38 src/lemon/lemon_reader.h --- a/src/lemon/lemon_reader.h Sat May 14 17:37:33 2005 +0000 +++ b/src/lemon/lemon_reader.h Sat May 14 17:39:37 2005 +0000 @@ -18,9 +18,11 @@ ///\file ///\brief Lemon Format reader. + #ifndef LEMON_LEMON_READER_H #define LEMON_LEMON_READER_H + #include #include #include @@ -30,11 +32,61 @@ #include #include +#include +#include #include namespace lemon { + namespace _reader_bits { + + template + bool operator<(T, T) { + throw DataFormatError("Id is not comparable"); + } + + template + struct Less { + bool operator()(const T& p, const T& q) const { + return p < q; + } + }; + + template + class WriteComposeMap { + public: + typedef True NeedCopy; + + typedef typename M2::Key Key; + typedef typename M1::Value Value; + + WriteComposeMap(typename SmartParameter::Type _m1, const M2& _m2) + : m1(_m1), m2(_m2) {} + + void set(const Key& key, const Value& value) { + m1.set(m2[key], value); + } + + private: + + typename SmartReference::Type m1; + typename SmartConstReference::Type m2; + + }; + + template + WriteComposeMap writeComposeMap(M1& m1, const M2& m2) { + return WriteComposeMap(m1, m2); + } + + template + WriteComposeMap writeComposeMap(const M1& m1, const M2& m2) { + return WriteComposeMap(m1, m2); + } + + } + /// \ingroup io_group /// \brief Lemon Format reader class. /// @@ -338,8 +390,6 @@ virtual InverterBase<_Item>* getInverter() { return this; } - - }; template @@ -349,13 +399,15 @@ typedef _Reader Reader; typedef typename Reader::Value Value; typedef _Map Map; - typedef std::map Inverse; + typedef std::map > Inverse; - Map& map; + typename SmartReference::Type map; Reader reader; Inverse inverse; - MapReaderInverter(Map& _map, const Reader& _reader) + MapReaderInverter(typename SmartParameter::Type _map, + const Reader& _reader) : map(_map), reader(_reader) {} virtual ~MapReaderInverter() {} @@ -382,7 +434,6 @@ throw DataFormatError("Invalid ID error"); } } - }; template @@ -391,7 +442,8 @@ typedef _Item Item; typedef _Reader Reader; typedef typename Reader::Value Value; - typedef std::map Inverse; + typedef std::map > Inverse; Reader reader; @@ -426,8 +478,6 @@ Inverse inverse; }; - // Readers - template class ReaderBase { public: @@ -447,10 +497,11 @@ typedef typename Reader::Value Value; typedef _Item Item; - Map& map; + typename SmartReference::Type map; Reader reader; - MapReader(Map& _map, const Reader& _reader) + MapReader(typename SmartParameter::Type _map, + const Reader& _reader) : map(_map), reader(_reader) {} virtual ~MapReader() {} @@ -569,7 +620,8 @@ /// attach it into the given LemonReader. The nodeset reader will /// add the readed nodes to the given Graph. The reader will read /// the section when the \c section_id and the \c _id are the same. - NodeSetReader(LemonReader& _reader, Graph& _graph, + NodeSetReader(LemonReader& _reader, + typename SmartParameter::Type _graph, const std::string& _id = std::string(), const DefaultSkipper& _skipper = DefaultSkipper()) : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {} @@ -595,17 +647,43 @@ /// /// Add a new node map reader command for the reader. template - NodeSetReader& readMap(std::string name, Map& map) { - return readMap, Map>(name, map); + NodeSetReader& readNodeMap(std::string name, Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + template + NodeSetReader& readNodeMap(std::string name, const Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); } /// \brief Add a new node map reader command for the reader. /// /// Add a new node map reader command for the reader. template - NodeSetReader& readMap(std::string name, Map& map, - const Reader& reader = Reader()) { + NodeSetReader& readNodeMap(std::string name, Map& map, + const Reader& reader = Reader()) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map, reader); + } + + template + NodeSetReader& readNodeMap(std::string name, const Map& map, + const Reader& reader = Reader()) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map, reader); + } + + private: + + template + NodeSetReader& _readMap(std::string name, MapParameter map, + const Reader& reader = Reader()) { if (readers.find(name) != readers.end()) { ErrorMessage msg; msg << "Multiple read rule for node map: " << name; @@ -616,11 +694,13 @@ return *this; } + public: + /// \brief Add a new node map skipper command for the reader. /// /// Add a new node map skipper command for the reader. template - NodeSetReader& skipMap(std::string name, + NodeSetReader& skipNodeMap(std::string name, const Reader& reader = Reader()) { if (readers.find(name) != readers.end()) { ErrorMessage msg; @@ -636,7 +716,7 @@ /// \brief Gives back true when the SectionReader can process /// the section with the given header line. /// - /// It gives back true when the header line starts with \c @nodeset, + /// It gives back true when the header line starts with \c \@nodeset, /// and the header line's id and the nodeset's id are the same. virtual bool header(const std::string& line) { std::istringstream ls(line); @@ -690,7 +770,7 @@ /// /// It reads an id from the stream and gives back which node belongs to /// it. It is possible only if there was read an "id" named map. - typename Graph::Node readId(std::istream& is) const { + Item readId(std::istream& is) const { return inverter->read(is); } @@ -699,7 +779,7 @@ typedef std::map*> MapReaders; MapReaders readers; - Graph& graph; + typename SmartReference::Type graph; std::string id; SkipReader skipper; @@ -714,8 +794,9 @@ /// \c edgeset_id may be empty. /// /// The first line of the section contains the names of the maps separated - /// with white spaces. Each next lines describes a node in the nodeset. The - /// line contains the two nodes' id and the mapped values for each map. + /// with white spaces. Each next lines describes an edge in the edgeset. The + /// line contains the source and the target nodes' id and the mapped + /// values for each map. /// /// If the edgeset contains an \c "id" named map then it will be regarded /// as id map. This map should contain only unique values and when the @@ -746,7 +827,8 @@ /// The reader will read the section only if the \c _id and the /// \c edgset_id are the same. template - EdgeSetReader(LemonReader& _reader, Graph& _graph, + EdgeSetReader(LemonReader& _reader, + typename SmartParameter::Type _graph, const NodeIdReader& _nodeIdReader, const std::string& _id = std::string(), const DefaultSkipper& _skipper = DefaultSkipper()) @@ -774,17 +856,43 @@ /// /// Add a new edge map reader command for the reader. template - EdgeSetReader& readMap(std::string name, Map& map) { - return readMap, Map>(name, map); + EdgeSetReader& readEdgeMap(std::string name, Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + template + EdgeSetReader& readEdgeMap(std::string name, const Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); } /// \brief Add a new edge map reader command for the reader. /// /// Add a new edge map reader command for the reader. template - EdgeSetReader& readMap(std::string name, Map& map, - const Reader& reader = Reader()) { + EdgeSetReader& readEdgeMap(std::string name, Map& map, + const Reader& reader = Reader()) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map, reader); + } + + template + EdgeSetReader& readEdgeMap(std::string name, const Map& map, + const Reader& reader = Reader()) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map, reader); + } + + private: + + template + EdgeSetReader& _readMap(std::string name, MapParameter map, + const Reader& reader = Reader()) { if (readers.find(name) != readers.end()) { ErrorMessage msg; msg << "Multiple read rule for edge map: " << name; @@ -795,15 +903,17 @@ return *this; } + public: + /// \brief Add a new edge map skipper command for the reader. /// /// Add a new edge map skipper command for the reader. template - EdgeSetReader& skipMap(std::string name, - const Reader& reader = Reader()) { + EdgeSetReader& skipEdgeMap(std::string name, + const Reader& reader = Reader()) { if (readers.find(name) != readers.end()) { ErrorMessage msg; - msg << "Multiple read rule for node map: " << name; + msg << "Multiple read rule for edge map: " << name; throw IOParameterError(msg.message()); } readers.insert(make_pair(name, new SkipReader(reader))); @@ -815,7 +925,7 @@ /// \brief Gives back true when the SectionReader can process /// the section with the given header line. /// - /// It gives back true when the header line starts with \c @edgeset, + /// It gives back true when the header line starts with \c \@edgeset, /// and the header line's id and the edgeset's id are the same. virtual bool header(const std::string& line) { std::istringstream ls(line); @@ -871,7 +981,7 @@ /// /// It reads an id from the stream and gives back which edge belongs to /// it. It is possible only if there was read an "id" named map. - typename Graph::Edge readId(std::istream& is) const { + Item readId(std::istream& is) const { return inverter->read(is); } @@ -880,7 +990,281 @@ typedef std::map*> MapReaders; MapReaders readers; - Graph& graph; + typename SmartReference::Type graph; + std::string id; + SkipReader skipper; + + std::auto_ptr > inverter; + std::auto_ptr > nodeIdReader; + }; + + /// \ingroup io_group + /// \brief SectionReader for reading a undirected graph's edgeset. + /// + /// The lemon format can store multiple undirected edgesets with several + /// maps. The undirected edgeset section's header line is \c \@undiredgeset + /// \c undiredgeset_id, but the \c undiredgeset_id may be empty. + /// + /// The first line of the section contains the names of the maps separated + /// with white spaces. Each next lines describes an edge in the edgeset. The + /// line contains the connected nodes' id and the mapped values for each map. + /// + /// The section can handle the directed as a syntactical sugar. Two + /// undirected edge map describes one directed edge map. This two maps + /// are the forward map and the backward map and the names of this map + /// is near the same just with a prefix \c '+' or \c '-' character + /// difference. + /// + /// If the edgeset contains an \c "id" named map then it will be regarded + /// as id map. This map should contain only unique values and when the + /// \c readId() member will read a value from the given stream it will + /// give back that undiricted edge which is mapped to this value. + /// + /// The undirected edgeset reader needs a node id reader to identify which + /// nodes have to be connected. If a NodeSetReader reads an "id" named map, + /// it will be able to resolve the nodes by ids. + /// + /// \relates LemonReader + template + class UndirEdgeSetReader : public CommonSectionReaderBase { + typedef CommonSectionReaderBase Parent; + public: + + typedef _Graph Graph; + typedef _Traits Traits; + typedef typename Graph::UndirEdge Item; + typedef typename Traits::Skipper DefaultSkipper; + + /// \brief Constructor. + /// + /// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader + /// and attach it into the given LemonReader. The undirected edgeset + /// reader will add the readed undirected edges to the given Graph. It + /// will use the given node id reader to read the source and target + /// nodes of the edges. The reader will read the section only if the + /// \c _id and the \c undiredgset_id are the same. + template + UndirEdgeSetReader(LemonReader& _reader, + typename SmartParameter::Type _graph, + const NodeIdReader& _nodeIdReader, + const std::string& _id = std::string(), + const DefaultSkipper& _skipper = DefaultSkipper()) + : Parent(_reader), graph(_graph), id(_id), skipper(_skipper), + nodeIdReader(new IdReader + (_nodeIdReader)) {} + + /// \brief Destructor. + /// + /// Destructor for UndirEdgeSetReader. + virtual ~UndirEdgeSetReader() { + for (typename MapReaders::iterator it = readers.begin(); + it != readers.end(); ++it) { + delete it->second; + } + } + + private: + UndirEdgeSetReader(const UndirEdgeSetReader&); + void operator=(const UndirEdgeSetReader&); + + public: + + /// \brief Add a new undirected edge map reader command for the reader. + /// + /// Add a new edge undirected map reader command for the reader. + template + UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + template + UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) { + return _readMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + /// \brief Add a new undirected edge map reader command for the reader. + /// + /// Add a new edge undirected map reader command for the reader. + template + UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map, + const Reader& reader = Reader()) { + return _readMap::Type> + (name, map, reader); + } + + template + UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map, + const Reader& reader = Reader()) { + return _readMap::Type > + (name, map, reader); + } + + private: + + template + UndirEdgeSetReader& _readMap(std::string name, MapParameter map, + const Reader& reader = Reader()) { + if (readers.find(name) != readers.end()) { + ErrorMessage msg; + msg << "Multiple read rule for edge map: " << name; + throw IOParameterError(msg.message()); + } + readers.insert( + make_pair(name, new MapReader(map, reader))); + return *this; + } + + public: + + /// \brief Add a new undirected edge map skipper command for the reader. + /// + /// Add a new undirected edge map skipper command for the reader. + template + UndirEdgeSetReader& skipUndirEdgeMap(std::string name, + const Reader& reader = Reader()) { + if (readers.find(name) != readers.end()) { + ErrorMessage msg; + msg << "Multiple read rule for node map: " << name; + throw IOParameterError(msg.message()); + } + readers.insert(make_pair(name, new SkipReader(reader))); + return *this; + } + + /// \brief Add a new directed edge map reader command for the reader. + /// + /// Add a new directed edge map reader command for the reader. + template + UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) { + return _readDirMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + template + UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) { + return _readDirMap< + typename Traits::template Reader, Map, + typename SmartParameter::Type>(name, map); + } + + /// \brief Add a new directed edge map reader command for the reader. + /// + /// Add a new directed edge map reader command for the reader. + template + UndirEdgeSetReader& readEdgeMap(std::string name, Map& map, + const Reader& reader = Reader()) { + return _readDirMap::Type> + (name, map, reader); + } + + template + UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map, + const Reader& reader = Reader()) { + return _readDirMap::Type> + (name, map, reader); + } + + private: + + template + UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map, + const Reader& reader = Reader()) { + readMap("+" + name, + _reader_bits::writeComposeMap(map, forwardMap(graph)), reader); + readMap("-" + name, + _reader_bits::writeComposeMap(map, backwardMap(graph)), reader); + return *this; + } + + public: + + /// \brief Add a new directed edge map skipper command for the reader. + /// + /// Add a new directed edge map skipper command for the reader. + template + UndirEdgeSetReader& skipEdgeMap(std::string name, + const Reader& reader = Reader()) { + skipMap("+" + name, reader); + skipMap("-" + name, reader); + return *this; + } + + protected: + + /// \brief Gives back true when the SectionReader can process + /// the section with the given header line. + /// + /// It gives back true when the header line starts with \c \@undiredgeset, + /// and the header line's id and the edgeset's id are the same. + virtual bool header(const std::string& line) { + std::istringstream ls(line); + std::string command; + std::string name; + ls >> command >> name; + return command == "@undiredgeset" && name == id; + } + + /// \brief Reader function of the section. + /// + /// It reads the content of the section. + virtual void read(std::istream& is) { + std::vector* > index; + std::string line; + + getline(is, line); + std::istringstream ls(line); + while (ls >> id) { + typename MapReaders::iterator it = readers.find(id); + if (it != readers.end()) { + index.push_back(it->second); + } else { + index.push_back(&skipper); + } + if (id == "id" && inverter.get() == 0) { + inverter.reset(index.back()->getInverter()); + index.back() = inverter.get(); + } + } + while (getline(is, line)) { + std::istringstream ls(line); + typename Graph::Node from = nodeIdReader->read(ls); + typename Graph::Node to = nodeIdReader->read(ls); + typename Graph::UndirEdge edge = graph.addEdge(from, to); + for (int i = 0; i < (int)index.size(); ++i) { + index[i]->read(ls, edge); + } + } + } + + public: + + /// \brief Returns true if the edgeset can give back the edge by its id. + /// + /// Returns true if the edgeset can give back the undirected edge by its + /// id. It is possible only if an "id" named map was read. + bool isIdReader() const { + return inverter.get() != 0; + } + + /// \brief Gives back the undirected edge by its id. + /// + /// It reads an id from the stream and gives back which undirected edge + /// belongs to it. It is possible only if there was read an "id" named map. + Item readId(std::istream& is) const { + return inverter->read(is); + } + + private: + + typedef std::map*> MapReaders; + MapReaders readers; + + typename SmartReference::Type graph; std::string id; SkipReader skipper; @@ -945,7 +1329,7 @@ /// \brief Gives back true when the SectionReader can process /// the section with the given header line. /// - /// It gives back true when the header line start with \c @nodes, + /// It gives back true when the header line start with \c \@nodes, /// and the header line's id and the reader's id are the same. virtual bool header(const std::string& line) { std::istringstream ls(line); @@ -1002,7 +1386,7 @@ /// Constructor for EdgeReader. It creates the EdgeReader and /// attach it into the given LemonReader. It will use the given /// edge id reader to give back the edges. The reader will read the - /// section only if the \c _id and the \c nodes_id are the same. + /// section only if the \c _id and the \c edges_id are the same. template EdgeReader(LemonReader& _reader, const _IdReader& _idReader, const std::string& _id = std::string()) @@ -1036,7 +1420,100 @@ /// \brief Gives back true when the SectionReader can process /// the section with the given header line. /// - /// It gives back true when the header line start with \c @edges, + /// It gives back true when the header line start with \c \@edges, + /// and the header line's id and the reader's id are the same. + virtual bool header(const std::string& line) { + std::istringstream ls(line); + std::string command; + std::string name; + ls >> command >> name; + return command == "@edges" && name == id; + } + + /// \brief Reader function of the section. + /// + /// It reads the content of the section. + virtual void read(std::istream& is) { + std::string line; + while (getline(is, line)) { + std::istringstream ls(line); + std::string id; + ls >> id; + typename ItemReaders::iterator it = readers.find(id); + if (it != readers.end()) { + *(it->second) = idReader->read(ls); + } + } + } + + private: + + std::string id; + + typedef std::map ItemReaders; + ItemReaders readers; + std::auto_ptr > idReader; + }; + + /// \ingroup io_group + /// \brief SectionReader for reading labeled undirected edges. + /// + /// The undirected edges section's header line is \c \@undiredges + /// \c undiredges_id, but the \c undiredges_id may be empty. + /// + /// Each line in the section contains the name of the undirected edge + /// and then the undirected edge id. + /// + /// \relates LemonReader + template + class UndirEdgeReader : public CommonSectionReaderBase { + typedef CommonSectionReaderBase Parent; + typedef _Graph Graph; + typedef typename Graph::UndirEdge Item; + public: + + /// \brief Constructor. + /// + /// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and + /// attach it into the given LemonReader. It will use the given + /// undirected edge id reader to give back the edges. The reader will + /// read the section only if the \c _id and the \c undiredges_id are + /// the same. + template + UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, + const std::string& _id = std::string()) + : Parent(_reader), id(_id), + idReader(new IdReader(_idReader)) + {} + + /// \brief Destructor. + /// + /// Destructor for UndirEdgeReader. + virtual ~UndirEdgeReader() {} + private: + UndirEdgeReader(const UndirEdgeReader&); + void operator=(const UndirEdgeReader&); + + public: + + /// \brief Add an undirected edge reader command for the UndirEdgeReader. + /// + /// Add an undirected edge reader command for the UndirEdgeReader. + void readUndirEdge(const std::string& name, Item& item) { + if (readers.find(name) != readers.end()) { + ErrorMessage msg; + msg << "Multiple read rule for edge: " << name; + throw IOParameterError(msg.message()); + } + readers.insert(make_pair(name, &item)); + } + + protected: + + /// \brief Gives back true when the SectionReader can process + /// the section with the given header line. + /// + /// It gives back true when the header line start with \c \@edges, /// and the header line's id and the reader's id are the same. virtual bool header(const std::string& line) { std::istringstream ls(line); @@ -1141,7 +1618,7 @@ /// \brief Gives back true when the SectionReader can process /// the section with the given header line. /// - /// It gives back true when the header line start with \c @attributes, + /// It gives back true when the header line start with \c \@attributes, /// and the header line's id and the attributeset's id are the same. bool header(const std::string& line) { std::istringstream ls(line); @@ -1174,6 +1651,5 @@ Readers readers; }; - } #endif