[Lemon-commits] Balazs Dezso: Undirected LGF IO
Lemon HG
hg at lemon.cs.elte.hu
Wed Jun 4 12:25:35 CEST 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/b4c336c27a03
changeset: 165:b4c336c27a03
user: Balazs Dezso <deba [at] inf.elte.hu>
date: Sat May 31 12:49:18 2008 +0200
description:
Undirected LGF IO
diffstat:
2 files changed, 1473 insertions(+)
lemon/lgf_reader.h | 891 ++++++++++++++++++++++++++++++++++++++++++++++++++++
lemon/lgf_writer.h | 582 +++++++++++++++++++++++++++++++++
diffs (truncated from 1528 to 300 lines):
diff -r 00d297da491e -r b4c336c27a03 lemon/lgf_reader.h
--- a/lemon/lgf_reader.h Sat May 31 12:34:44 2008 +0200
+++ b/lemon/lgf_reader.h Sat May 31 12:49:18 2008 +0200
@@ -100,6 +100,32 @@
}
};
+ template <typename _Graph, bool _dir, typename _Map,
+ typename _Converter = DefaultConverter<typename _Map::Value> >
+ class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
+ public:
+ typedef _Map Map;
+ typedef _Converter Converter;
+ typedef _Graph Graph;
+ typedef typename Graph::Edge Item;
+ static const bool dir = _dir;
+
+ private:
+ const Graph& _graph;
+ Map& _map;
+ Converter _converter;
+
+ public:
+ GraphArcMapStorage(const Graph& graph, Map& map,
+ const Converter& converter = Converter())
+ : _graph(graph), _map(map), _converter(converter) {}
+ virtual ~GraphArcMapStorage() {}
+
+ virtual void set(const Item& item ,const std::string& value) {
+ _map.set(_graph.direct(item, dir), _converter(value));
+ }
+ };
+
class ValueStorageBase {
public:
ValueStorageBase() {}
@@ -143,6 +169,29 @@
throw DataFormatError(msg.str().c_str());
}
return it->second;
+ }
+ };
+
+ template <typename Graph>
+ struct GraphArcLookUpConverter {
+ const Graph& _graph;
+ const std::map<std::string, typename Graph::Edge>& _map;
+
+ GraphArcLookUpConverter(const Graph& graph,
+ const std::map<std::string,
+ typename Graph::Edge>& map)
+ : _graph(graph), _map(map) {}
+
+ typename Graph::Arc operator()(const std::string& str) {
+ if (str.empty() || (str[0] != '+' && str[0] != '-')) {
+ throw DataFormatError("Item must start with '+' or '-'");
+ }
+ typename std::map<std::string, typename Graph::Edge>
+ ::const_iterator it = _map.find(str.substr(1));
+ if (it == _map.end()) {
+ throw DataFormatError("Item not found");
+ }
+ return _graph.direct(it->second, str[0] == '+');
}
};
@@ -1180,6 +1229,848 @@
DigraphReader<Digraph> tmp(fn, digraph);
return tmp;
}
+
+ /// \ingroup lemon_io
+ ///
+ /// \brief LGF reader for undirected graphs
+ ///
+ /// This utility reads an \ref lgf-format "LGF" file.
+ template <typename _Graph>
+ class GraphReader {
+ public:
+
+ typedef _Graph Graph;
+ TEMPLATE_GRAPH_TYPEDEFS(Graph);
+
+ private:
+
+
+ std::istream* _is;
+ bool local_is;
+
+ Graph& _graph;
+
+ std::string _nodes_caption;
+ std::string _edges_caption;
+ std::string _attributes_caption;
+
+ typedef std::map<std::string, Node> NodeIndex;
+ NodeIndex _node_index;
+ typedef std::map<std::string, Edge> EdgeIndex;
+ EdgeIndex _edge_index;
+
+ typedef std::vector<std::pair<std::string,
+ _reader_bits::MapStorageBase<Node>*> > NodeMaps;
+ NodeMaps _node_maps;
+
+ typedef std::vector<std::pair<std::string,
+ _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
+ EdgeMaps _edge_maps;
+
+ typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
+ Attributes;
+ Attributes _attributes;
+
+ typedef std::map<std::string, _reader_bits::Section*> Sections;
+ Sections _sections;
+
+ bool _use_nodes;
+ bool _use_edges;
+
+ int line_num;
+ std::istringstream line;
+
+ public:
+
+ /// \brief Constructor
+ ///
+ /// Construct a undirected graph reader, which reads from the given
+ /// input stream.
+ GraphReader(std::istream& is, Graph& graph)
+ : _is(&is), local_is(false), _graph(graph),
+ _use_nodes(false), _use_edges(false) {}
+
+ /// \brief Constructor
+ ///
+ /// Construct a undirected graph reader, which reads from the given
+ /// file.
+ GraphReader(const std::string& fn, Graph& graph)
+ : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
+ _use_nodes(false), _use_edges(false) {}
+
+ /// \brief Constructor
+ ///
+ /// Construct a undirected graph reader, which reads from the given
+ /// file.
+ GraphReader(const char* fn, Graph& graph)
+ : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
+ _use_nodes(false), _use_edges(false) {}
+
+ /// \brief Copy constructor
+ ///
+ /// The copy constructor transfers all data from the other reader,
+ /// therefore the copied reader will not be usable more.
+ GraphReader(GraphReader& other)
+ : _is(other._is), local_is(other.local_is), _graph(other._graph),
+ _use_nodes(other._use_nodes), _use_edges(other._use_edges) {
+
+ other._is = 0;
+ other.local_is = false;
+
+ _node_index.swap(other._node_index);
+ _edge_index.swap(other._edge_index);
+
+ _node_maps.swap(other._node_maps);
+ _edge_maps.swap(other._edge_maps);
+ _attributes.swap(other._attributes);
+
+ _nodes_caption = other._nodes_caption;
+ _edges_caption = other._edges_caption;
+ _attributes_caption = other._attributes_caption;
+
+ _sections.swap(other._sections);
+ }
+
+ /// \brief Destructor
+ ~GraphReader() {
+ for (typename NodeMaps::iterator it = _node_maps.begin();
+ it != _node_maps.end(); ++it) {
+ delete it->second;
+ }
+
+ for (typename EdgeMaps::iterator it = _edge_maps.begin();
+ it != _edge_maps.end(); ++it) {
+ delete it->second;
+ }
+
+ for (typename Attributes::iterator it = _attributes.begin();
+ it != _attributes.end(); ++it) {
+ delete it->second;
+ }
+
+ for (typename Sections::iterator it = _sections.begin();
+ it != _sections.end(); ++it) {
+ delete it->second;
+ }
+
+ if (local_is) {
+ delete _is;
+ }
+
+ }
+
+ private:
+
+ GraphReader& operator=(const GraphReader&);
+
+ public:
+
+ /// \name Reading rules
+ /// @{
+
+ /// \brief Node map reading rule
+ ///
+ /// Add a node map reading rule to the reader.
+ template <typename Map>
+ GraphReader& nodeMap(const std::string& caption, Map& map) {
+ checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Node>* storage =
+ new _reader_bits::MapStorage<Node, Map>(map);
+ _node_maps.push_back(std::make_pair(caption, storage));
+ return *this;
+ }
+
+ /// \brief Node map reading rule
+ ///
+ /// Add a node map reading rule with specialized converter to the
+ /// reader.
+ template <typename Map, typename Converter>
+ GraphReader& nodeMap(const std::string& caption, Map& map,
+ const Converter& converter = Converter()) {
+ checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Node>* storage =
+ new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
+ _node_maps.push_back(std::make_pair(caption, storage));
+ return *this;
+ }
+
+ /// \brief Edge map reading rule
+ ///
+ /// Add an edge map reading rule to the reader.
+ template <typename Map>
+ GraphReader& edgeMap(const std::string& caption, Map& map) {
+ checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Edge>* storage =
+ new _reader_bits::MapStorage<Edge, Map>(map);
+ _edge_maps.push_back(std::make_pair(caption, storage));
+ return *this;
+ }
+
+ /// \brief Edge map reading rule
+ ///
+ /// Add an edge map reading rule with specialized converter to the
+ /// reader.
+ template <typename Map, typename Converter>
+ GraphReader& edgeMap(const std::string& caption, Map& map,
+ const Converter& converter = Converter()) {
+ checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Edge>* storage =
+ new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
+ _edge_maps.push_back(std::make_pair(caption, storage));
+ return *this;
+ }
+
+ /// \brief Arc map reading rule
+ ///
+ /// Add an arc map reading rule to the reader.
+ template <typename Map>
+ GraphReader& arcMap(const std::string& caption, Map& map) {
+ checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Edge>* forward_storage =
+ new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
+ _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
+ _reader_bits::MapStorageBase<Edge>* backward_storage =
+ new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
+ _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
+ return *this;
+ }
+
+ /// \brief Arc map reading rule
+ ///
+ /// Add an arc map reading rule with specialized converter to the
+ /// reader.
+ template <typename Map, typename Converter>
+ GraphReader& arcMap(const std::string& caption, Map& map,
+ const Converter& converter = Converter()) {
+ checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
+ _reader_bits::MapStorageBase<Edge>* forward_storage =
+ new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
+ (_graph, map, converter);
+ _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
+ _reader_bits::MapStorageBase<Edge>* backward_storage =
+ new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
+ (_graph, map, converter);
+ _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
+ return *this;
+ }
+
+ /// \brief Attribute reading rule
+ ///
+ /// Add an attribute reading rule to the reader.
+ template <typename Value>
+ GraphReader& attribute(const std::string& caption, Value& value) {
More information about the Lemon-commits
mailing list