1.1 --- a/lemon/lgf_reader.h Tue Apr 07 14:50:20 2009 +0100
1.2 +++ b/lemon/lgf_reader.h Sat Apr 18 21:54:30 2009 +0200
1.3 @@ -101,23 +101,23 @@
1.4 }
1.5 };
1.6
1.7 - template <typename _Graph, bool _dir, typename _Map,
1.8 + template <typename _GR, bool _dir, typename _Map,
1.9 typename _Converter = DefaultConverter<typename _Map::Value> >
1.10 - class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
1.11 + class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> {
1.12 public:
1.13 typedef _Map Map;
1.14 typedef _Converter Converter;
1.15 - typedef _Graph Graph;
1.16 - typedef typename Graph::Edge Item;
1.17 + typedef _GR GR;
1.18 + typedef typename GR::Edge Item;
1.19 static const bool dir = _dir;
1.20
1.21 private:
1.22 - const Graph& _graph;
1.23 + const GR& _graph;
1.24 Map& _map;
1.25 Converter _converter;
1.26
1.27 public:
1.28 - GraphArcMapStorage(const Graph& graph, Map& map,
1.29 + GraphArcMapStorage(const GR& graph, Map& map,
1.30 const Converter& converter = Converter())
1.31 : _graph(graph), _map(map), _converter(converter) {}
1.32 virtual ~GraphArcMapStorage() {}
1.33 @@ -173,21 +173,21 @@
1.34 }
1.35 };
1.36
1.37 - template <typename Graph>
1.38 + template <typename GR>
1.39 struct GraphArcLookUpConverter {
1.40 - const Graph& _graph;
1.41 - const std::map<std::string, typename Graph::Edge>& _map;
1.42 -
1.43 - GraphArcLookUpConverter(const Graph& graph,
1.44 + const GR& _graph;
1.45 + const std::map<std::string, typename GR::Edge>& _map;
1.46 +
1.47 + GraphArcLookUpConverter(const GR& graph,
1.48 const std::map<std::string,
1.49 - typename Graph::Edge>& map)
1.50 + typename GR::Edge>& map)
1.51 : _graph(graph), _map(map) {}
1.52
1.53 - typename Graph::Arc operator()(const std::string& str) {
1.54 + typename GR::Arc operator()(const std::string& str) {
1.55 if (str.empty() || (str[0] != '+' && str[0] != '-')) {
1.56 throw FormatError("Item must start with '+' or '-'");
1.57 }
1.58 - typename std::map<std::string, typename Graph::Edge>
1.59 + typename std::map<std::string, typename GR::Edge>
1.60 ::const_iterator it = _map.find(str.substr(1));
1.61 if (it == _map.end()) {
1.62 throw FormatError("Item not found");
1.63 @@ -387,16 +387,15 @@
1.64
1.65 }
1.66
1.67 - template <typename Digraph>
1.68 + template <typename DGR>
1.69 class DigraphReader;
1.70
1.71 - template <typename Digraph>
1.72 - DigraphReader<Digraph> digraphReader(Digraph& digraph,
1.73 - std::istream& is = std::cin);
1.74 - template <typename Digraph>
1.75 - DigraphReader<Digraph> digraphReader(Digraph& digraph, const std::string& fn);
1.76 - template <typename Digraph>
1.77 - DigraphReader<Digraph> digraphReader(Digraph& digraph, const char *fn);
1.78 + template <typename TDGR>
1.79 + DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin);
1.80 + template <typename TDGR>
1.81 + DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn);
1.82 + template <typename TDGR>
1.83 + DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
1.84
1.85 /// \ingroup lemon_io
1.86 ///
1.87 @@ -419,7 +418,7 @@
1.88 /// rules.
1.89 ///
1.90 ///\code
1.91 - /// DigraphReader<Digraph>(digraph, std::cin).
1.92 + /// DigraphReader<DGR>(digraph, std::cin).
1.93 /// nodeMap("coordinates", coord_map).
1.94 /// arcMap("capacity", cap_map).
1.95 /// node("source", src).
1.96 @@ -448,21 +447,21 @@
1.97 /// It is impossible to read this in
1.98 /// a single pass, because the arcs are not constructed when the node
1.99 /// maps are read.
1.100 - template <typename GR>
1.101 + template <typename DGR>
1.102 class DigraphReader {
1.103 public:
1.104
1.105 - typedef GR Digraph;
1.106 + typedef DGR Digraph;
1.107
1.108 private:
1.109
1.110 - TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
1.111 + TEMPLATE_DIGRAPH_TYPEDEFS(DGR);
1.112
1.113 std::istream* _is;
1.114 bool local_is;
1.115 std::string _filename;
1.116
1.117 - Digraph& _digraph;
1.118 + DGR& _digraph;
1.119
1.120 std::string _nodes_caption;
1.121 std::string _arcs_caption;
1.122 @@ -500,7 +499,7 @@
1.123 ///
1.124 /// Construct a directed graph reader, which reads from the given
1.125 /// input stream.
1.126 - DigraphReader(Digraph& digraph, std::istream& is = std::cin)
1.127 + DigraphReader(DGR& digraph, std::istream& is = std::cin)
1.128 : _is(&is), local_is(false), _digraph(digraph),
1.129 _use_nodes(false), _use_arcs(false),
1.130 _skip_nodes(false), _skip_arcs(false) {}
1.131 @@ -509,7 +508,7 @@
1.132 ///
1.133 /// Construct a directed graph reader, which reads from the given
1.134 /// file.
1.135 - DigraphReader(Digraph& digraph, const std::string& fn)
1.136 + DigraphReader(DGR& digraph, const std::string& fn)
1.137 : _is(new std::ifstream(fn.c_str())), local_is(true),
1.138 _filename(fn), _digraph(digraph),
1.139 _use_nodes(false), _use_arcs(false),
1.140 @@ -524,7 +523,7 @@
1.141 ///
1.142 /// Construct a directed graph reader, which reads from the given
1.143 /// file.
1.144 - DigraphReader(Digraph& digraph, const char* fn)
1.145 + DigraphReader(DGR& digraph, const char* fn)
1.146 : _is(new std::ifstream(fn)), local_is(true),
1.147 _filename(fn), _digraph(digraph),
1.148 _use_nodes(false), _use_arcs(false),
1.149 @@ -560,13 +559,13 @@
1.150
1.151 private:
1.152
1.153 - template <typename DGR>
1.154 - friend DigraphReader<DGR> digraphReader(DGR& digraph, std::istream& is);
1.155 - template <typename DGR>
1.156 - friend DigraphReader<DGR> digraphReader(DGR& digraph,
1.157 - const std::string& fn);
1.158 - template <typename DGR>
1.159 - friend DigraphReader<DGR> digraphReader(DGR& digraph, const char *fn);
1.160 + template <typename TDGR>
1.161 + friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is);
1.162 + template <typename TDGR>
1.163 + friend DigraphReader<TDGR> digraphReader(TDGR& digraph,
1.164 + const std::string& fn);
1.165 + template <typename TDGR>
1.166 + friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
1.167
1.168 DigraphReader(DigraphReader& other)
1.169 : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
1.170 @@ -1188,14 +1187,52 @@
1.171 /// @}
1.172
1.173 };
1.174 +
1.175 + /// \ingroup lemon_io
1.176 + ///
1.177 + /// \brief Return a \ref DigraphReader class
1.178 + ///
1.179 + /// This function just returns a \ref DigraphReader class.
1.180 + ///
1.181 + /// With this function a digraph can be read from an
1.182 + /// \ref lgf-format "LGF" file or input stream with several maps and
1.183 + /// attributes. For example, there is network flow problem on a
1.184 + /// digraph, i.e. a digraph with a \e capacity map on the arcs and
1.185 + /// \e source and \e target nodes. This digraph can be read with the
1.186 + /// following code:
1.187 + ///
1.188 + ///\code
1.189 + ///ListDigraph digraph;
1.190 + ///ListDigraph::ArcMap<int> cm(digraph);
1.191 + ///ListDigraph::Node src, trg;
1.192 + ///digraphReader(digraph, std::cin).
1.193 + /// arcMap("capacity", cap).
1.194 + /// node("source", src).
1.195 + /// node("target", trg).
1.196 + /// run();
1.197 + ///\endcode
1.198 + ///
1.199 + /// For a complete documentation, please see the \ref DigraphReader
1.200 + /// class documentation.
1.201 + /// \warning Don't forget to put the \ref DigraphReader::run() "run()"
1.202 + /// to the end of the parameter list.
1.203 + /// \relates DigraphReader
1.204 + /// \sa digraphReader(TDGR& digraph, const std::string& fn)
1.205 + /// \sa digraphReader(TDGR& digraph, const char* fn)
1.206 + template <typename TDGR>
1.207 + DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) {
1.208 + DigraphReader<TDGR> tmp(digraph, is);
1.209 + return tmp;
1.210 + }
1.211
1.212 /// \brief Return a \ref DigraphReader class
1.213 ///
1.214 /// This function just returns a \ref DigraphReader class.
1.215 /// \relates DigraphReader
1.216 - template <typename Digraph>
1.217 - DigraphReader<Digraph> digraphReader(Digraph& digraph, std::istream& is) {
1.218 - DigraphReader<Digraph> tmp(digraph, is);
1.219 + /// \sa digraphReader(TDGR& digraph, std::istream& is)
1.220 + template <typename TDGR>
1.221 + DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) {
1.222 + DigraphReader<TDGR> tmp(digraph, fn);
1.223 return tmp;
1.224 }
1.225
1.226 @@ -1203,33 +1240,22 @@
1.227 ///
1.228 /// This function just returns a \ref DigraphReader class.
1.229 /// \relates DigraphReader
1.230 - template <typename Digraph>
1.231 - DigraphReader<Digraph> digraphReader(Digraph& digraph,
1.232 - const std::string& fn) {
1.233 - DigraphReader<Digraph> tmp(digraph, fn);
1.234 + /// \sa digraphReader(TDGR& digraph, std::istream& is)
1.235 + template <typename TDGR>
1.236 + DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) {
1.237 + DigraphReader<TDGR> tmp(digraph, fn);
1.238 return tmp;
1.239 }
1.240
1.241 - /// \brief Return a \ref DigraphReader class
1.242 - ///
1.243 - /// This function just returns a \ref DigraphReader class.
1.244 - /// \relates DigraphReader
1.245 - template <typename Digraph>
1.246 - DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
1.247 - DigraphReader<Digraph> tmp(digraph, fn);
1.248 - return tmp;
1.249 - }
1.250 -
1.251 - template <typename Graph>
1.252 + template <typename GR>
1.253 class GraphReader;
1.254
1.255 - template <typename Graph>
1.256 - GraphReader<Graph> graphReader(Graph& graph,
1.257 - std::istream& is = std::cin);
1.258 - template <typename Graph>
1.259 - GraphReader<Graph> graphReader(Graph& graph, const std::string& fn);
1.260 - template <typename Graph>
1.261 - GraphReader<Graph> graphReader(Graph& graph, const char *fn);
1.262 + template <typename TGR>
1.263 + GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin);
1.264 + template <typename TGR>
1.265 + GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
1.266 + template <typename TGR>
1.267 + GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1.268
1.269 /// \ingroup lemon_io
1.270 ///
1.271 @@ -1254,13 +1280,13 @@
1.272
1.273 private:
1.274
1.275 - TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.276 + TEMPLATE_GRAPH_TYPEDEFS(GR);
1.277
1.278 std::istream* _is;
1.279 bool local_is;
1.280 std::string _filename;
1.281
1.282 - Graph& _graph;
1.283 + GR& _graph;
1.284
1.285 std::string _nodes_caption;
1.286 std::string _edges_caption;
1.287 @@ -1298,7 +1324,7 @@
1.288 ///
1.289 /// Construct an undirected graph reader, which reads from the given
1.290 /// input stream.
1.291 - GraphReader(Graph& graph, std::istream& is = std::cin)
1.292 + GraphReader(GR& graph, std::istream& is = std::cin)
1.293 : _is(&is), local_is(false), _graph(graph),
1.294 _use_nodes(false), _use_edges(false),
1.295 _skip_nodes(false), _skip_edges(false) {}
1.296 @@ -1307,7 +1333,7 @@
1.297 ///
1.298 /// Construct an undirected graph reader, which reads from the given
1.299 /// file.
1.300 - GraphReader(Graph& graph, const std::string& fn)
1.301 + GraphReader(GR& graph, const std::string& fn)
1.302 : _is(new std::ifstream(fn.c_str())), local_is(true),
1.303 _filename(fn), _graph(graph),
1.304 _use_nodes(false), _use_edges(false),
1.305 @@ -1322,7 +1348,7 @@
1.306 ///
1.307 /// Construct an undirected graph reader, which reads from the given
1.308 /// file.
1.309 - GraphReader(Graph& graph, const char* fn)
1.310 + GraphReader(GR& graph, const char* fn)
1.311 : _is(new std::ifstream(fn)), local_is(true),
1.312 _filename(fn), _graph(graph),
1.313 _use_nodes(false), _use_edges(false),
1.314 @@ -1357,12 +1383,12 @@
1.315 }
1.316
1.317 private:
1.318 - template <typename Graph>
1.319 - friend GraphReader<Graph> graphReader(Graph& graph, std::istream& is);
1.320 - template <typename Graph>
1.321 - friend GraphReader<Graph> graphReader(Graph& graph, const std::string& fn);
1.322 - template <typename Graph>
1.323 - friend GraphReader<Graph> graphReader(Graph& graph, const char *fn);
1.324 + template <typename TGR>
1.325 + friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is);
1.326 + template <typename TGR>
1.327 + friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
1.328 + template <typename TGR>
1.329 + friend GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1.330
1.331 GraphReader(GraphReader& other)
1.332 : _is(other._is), local_is(other.local_is), _graph(other._graph),
1.333 @@ -1454,7 +1480,7 @@
1.334 new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
1.335 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1.336 _reader_bits::MapStorageBase<Edge>* backward_storage =
1.337 - new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
1.338 + new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map);
1.339 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1.340 return *this;
1.341 }
1.342 @@ -1468,11 +1494,11 @@
1.343 const Converter& converter = Converter()) {
1.344 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1.345 _reader_bits::MapStorageBase<Edge>* forward_storage =
1.346 - new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
1.347 + new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter>
1.348 (_graph, map, converter);
1.349 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1.350 _reader_bits::MapStorageBase<Edge>* backward_storage =
1.351 - new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
1.352 + new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter>
1.353 (_graph, map, converter);
1.354 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1.355 return *this;
1.356 @@ -1530,7 +1556,7 @@
1.357 ///
1.358 /// Add an arc reading rule to reader.
1.359 GraphReader& arc(const std::string& caption, Arc& arc) {
1.360 - typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
1.361 + typedef _reader_bits::GraphArcLookUpConverter<GR> Converter;
1.362 Converter converter(_graph, _edge_index);
1.363 _reader_bits::ValueStorageBase* storage =
1.364 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
1.365 @@ -2033,13 +2059,47 @@
1.366
1.367 };
1.368
1.369 + /// \ingroup lemon_io
1.370 + ///
1.371 + /// \brief Return a \ref GraphReader class
1.372 + ///
1.373 + /// This function just returns a \ref GraphReader class.
1.374 + ///
1.375 + /// With this function a graph can be read from an
1.376 + /// \ref lgf-format "LGF" file or input stream with several maps and
1.377 + /// attributes. For example, there is weighted matching problem on a
1.378 + /// graph, i.e. a graph with a \e weight map on the edges. This
1.379 + /// graph can be read with the following code:
1.380 + ///
1.381 + ///\code
1.382 + ///ListGraph graph;
1.383 + ///ListGraph::EdgeMap<int> weight(graph);
1.384 + ///graphReader(graph, std::cin).
1.385 + /// edgeMap("weight", weight).
1.386 + /// run();
1.387 + ///\endcode
1.388 + ///
1.389 + /// For a complete documentation, please see the \ref GraphReader
1.390 + /// class documentation.
1.391 + /// \warning Don't forget to put the \ref GraphReader::run() "run()"
1.392 + /// to the end of the parameter list.
1.393 + /// \relates GraphReader
1.394 + /// \sa graphReader(TGR& graph, const std::string& fn)
1.395 + /// \sa graphReader(TGR& graph, const char* fn)
1.396 + template <typename TGR>
1.397 + GraphReader<TGR> graphReader(TGR& graph, std::istream& is) {
1.398 + GraphReader<TGR> tmp(graph, is);
1.399 + return tmp;
1.400 + }
1.401 +
1.402 /// \brief Return a \ref GraphReader class
1.403 ///
1.404 /// This function just returns a \ref GraphReader class.
1.405 /// \relates GraphReader
1.406 - template <typename Graph>
1.407 - GraphReader<Graph> graphReader(Graph& graph, std::istream& is) {
1.408 - GraphReader<Graph> tmp(graph, is);
1.409 + /// \sa graphReader(TGR& graph, std::istream& is)
1.410 + template <typename TGR>
1.411 + GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) {
1.412 + GraphReader<TGR> tmp(graph, fn);
1.413 return tmp;
1.414 }
1.415
1.416 @@ -2047,19 +2107,10 @@
1.417 ///
1.418 /// This function just returns a \ref GraphReader class.
1.419 /// \relates GraphReader
1.420 - template <typename Graph>
1.421 - GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
1.422 - GraphReader<Graph> tmp(graph, fn);
1.423 - return tmp;
1.424 - }
1.425 -
1.426 - /// \brief Return a \ref GraphReader class
1.427 - ///
1.428 - /// This function just returns a \ref GraphReader class.
1.429 - /// \relates GraphReader
1.430 - template <typename Graph>
1.431 - GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
1.432 - GraphReader<Graph> tmp(graph, fn);
1.433 + /// \sa graphReader(TGR& graph, std::istream& is)
1.434 + template <typename TGR>
1.435 + GraphReader<TGR> graphReader(TGR& graph, const char* fn) {
1.436 + GraphReader<TGR> tmp(graph, fn);
1.437 return tmp;
1.438 }
1.439
1.440 @@ -2316,12 +2367,30 @@
1.441
1.442 };
1.443
1.444 + /// \ingroup lemon_io
1.445 + ///
1.446 + /// \brief Return a \ref SectionReader class
1.447 + ///
1.448 + /// This function just returns a \ref SectionReader class.
1.449 + ///
1.450 + /// Please see SectionReader documentation about the custom section
1.451 + /// input.
1.452 + ///
1.453 + /// \relates SectionReader
1.454 + /// \sa sectionReader(const std::string& fn)
1.455 + /// \sa sectionReader(const char *fn)
1.456 + inline SectionReader sectionReader(std::istream& is) {
1.457 + SectionReader tmp(is);
1.458 + return tmp;
1.459 + }
1.460 +
1.461 /// \brief Return a \ref SectionReader class
1.462 ///
1.463 /// This function just returns a \ref SectionReader class.
1.464 /// \relates SectionReader
1.465 - inline SectionReader sectionReader(std::istream& is) {
1.466 - SectionReader tmp(is);
1.467 + /// \sa sectionReader(std::istream& is)
1.468 + inline SectionReader sectionReader(const std::string& fn) {
1.469 + SectionReader tmp(fn);
1.470 return tmp;
1.471 }
1.472
1.473 @@ -2329,15 +2398,7 @@
1.474 ///
1.475 /// This function just returns a \ref SectionReader class.
1.476 /// \relates SectionReader
1.477 - inline SectionReader sectionReader(const std::string& fn) {
1.478 - SectionReader tmp(fn);
1.479 - return tmp;
1.480 - }
1.481 -
1.482 - /// \brief Return a \ref SectionReader class
1.483 - ///
1.484 - /// This function just returns a \ref SectionReader class.
1.485 - /// \relates SectionReader
1.486 + /// \sa sectionReader(std::istream& is)
1.487 inline SectionReader sectionReader(const char* fn) {
1.488 SectionReader tmp(fn);
1.489 return tmp;