LemonWriter and GraphWriter.
Little bit better documentation.
2 * src/lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
19 ///\brief Lemon Graph Format reader.
21 #ifndef LEMON_GRAPH_READER_H
22 #define LEMON_GRAPH_READER_H
26 #include <lemon/error.h>
27 #include <lemon/lemon_reader.h>
31 /// \addtogroup io_group
34 /// \brief The graph reader class.
36 /// The given file format may contain several maps and labeled nodes or
39 /// If you read a graph you need not read all the maps and items just those
40 /// that you need. The interface of the \c GraphReader is very similar to
41 /// the GraphWriter but the reading method does not depend on the order the
44 /// The reader object suppose that each not readed value does not contain
45 /// whitespaces, therefore it has some extra possibilities to control how
46 /// it should skip the values when the string representation contains spaces.
49 /// GraphReader<ListGraph> reader(std::cin, graph);
52 /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
53 /// If there is a map that you do not want to read from the file and there is
54 /// whitespace in the string represenation of the values then you should
55 /// call the \c skipNodeMap() template member function with proper
59 /// reader.readNodeMap("x-coord", xCoordMap);
60 /// reader.readNodeMap("y-coord", yCoordMap);
62 /// reader.readNodeMap<QuotedStringReader>("label", labelMap);
63 /// reader.skipNodeMap<QuotedStringReader>("description");
65 /// reader.readNodeMap("color", colorMap);
68 /// With the \c readEdgeMap() member function you can give an edge map
69 /// reading command similar to the NodeMaps.
72 /// reader.readEdgeMap("weight", weightMap);
73 /// reader.readEdgeMap("label", labelMap);
76 /// With \c readNode() and \c readEdge() functions you can read
77 /// labeled Nodes and Edges.
80 /// reader.readNode("source", sourceNode);
81 /// reader.readNode("target", targetNode);
83 /// reader.readEdge("observed", edge);
86 /// With the \c readAttribute() functions you can read an attribute
87 /// in a variable. You can specify the reader for the attribute as
90 /// After you give all read commands you must call the \c run() member
91 /// function, which execute all the commands.
97 /// \see DefaultReaderTraits
98 /// \see QuotedStringReader
99 /// \see \ref GraphWriter
100 /// \see \ref graph-io-page
101 /// \author Balazs Dezso
102 template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits>
106 typedef _Graph Graph;
107 typedef typename Graph::Node Node;
108 typedef typename Graph::Edge Edge;
110 typedef _ReaderTraits ReaderTraits;
111 typedef typename ReaderTraits::Skipper DefaultSkipper;
113 /// \brief Construct a new GraphReader.
115 /// Construct a new GraphReader. It reads into the given graph
116 /// and it use the given reader as the default skipper.
117 GraphReader(std::istream& _is, Graph& _graph,
118 const DefaultSkipper& _skipper = DefaultSkipper())
119 : reader(new LemonReader(_is)), own_reader(true),
120 graph(_graph), skipper(_skipper),
121 nodeset_reader(*reader, graph, std::string(), skipper),
122 edgeset_reader(*reader, graph, nodeset_reader, std::string(), skipper),
123 node_reader(*reader, nodeset_reader, std::string()),
124 edge_reader(*reader, edgeset_reader, std::string()),
125 attribute_reader(*reader, std::string()) {}
127 /// \brief Construct a new GraphReader.
129 /// Construct a new GraphReader. It reads into the given graph
130 /// and it use the given reader as the default skipper.
131 GraphReader(const std::string& _filename, Graph& _graph,
132 const DefaultSkipper& _skipper = DefaultSkipper())
133 : reader(new LemonReader(_filename)), own_reader(true),
134 graph(_graph), skipper(_skipper),
135 nodeset_reader(*reader, graph, std::string(), skipper),
136 edgeset_reader(*reader, graph, nodeset_reader, std::string(), skipper),
137 node_reader(*reader, nodeset_reader, std::string()),
138 edge_reader(*reader, edgeset_reader, std::string()),
139 attribute_reader(*reader, std::string()) {}
141 /// \brief Construct a new GraphReader.
143 /// Construct a new GraphReader. It reads into the given graph
144 /// and it use the given reader as the default skipper.
145 GraphReader(LemonReader& _reader, Graph& _graph,
146 const DefaultSkipper& _skipper = DefaultSkipper())
147 : reader(_reader), own_reader(false),
148 graph(_graph), skipper(_skipper),
149 nodeset_reader(*reader, graph, std::string(), skipper),
150 edgeset_reader(*reader, graph, nodeset_reader, std::string(), skipper),
151 node_reader(*reader, nodeset_reader, std::string()),
152 edge_reader(*reader, edgeset_reader, std::string()),
153 attribute_reader(*reader, std::string()) {}
155 /// \brief Destruct the graph reader.
157 /// Destruct the graph reader.
163 /// \brief Add a new node map reader command for the reader.
165 /// Add a new node map reader command for the reader.
166 template <typename Map>
167 GraphReader& readNodeMap(std::string name, Map& map) {
168 nodeset_reader.readMap(name, map);
172 /// \brief Add a new node map reader command for the reader.
174 /// Add a new node map reader command for the reader.
175 template <typename Reader, typename Map>
176 GraphReader& readNodeMap(std::string name, Map& map,
177 const Reader& reader = Reader()) {
178 nodeset_reader.readMap(name, map, reader);
182 /// \brief Add a new node map skipper command for the reader.
184 /// Add a new node map skipper command for the reader.
185 template <typename Reader>
186 GraphReader& skipNodeMap(std::string name,
187 const Reader& reader = Reader()) {
188 nodeset_reader.skipMap(name, reader);
192 /// \brief Add a new edge map reader command for the reader.
194 /// Add a new edge map reader command for the reader.
195 template <typename Map>
196 GraphReader& readEdgeMap(std::string name, Map& map) {
197 edgeset_reader.readMap(name, map);
202 /// \brief Add a new edge map reader command for the reader.
204 /// Add a new edge map reader command for the reader.
205 template <typename Reader, typename Map>
206 GraphReader& readEdgeMap(std::string name, Map& map,
207 const Reader& reader = Reader()) {
208 edgeset_reader.readMap(name, map, reader);
212 /// \brief Add a new edge map skipper command for the reader.
214 /// Add a new edge map skipper command for the reader.
215 template <typename Reader>
216 GraphReader& skipEdgeMap(std::string name,
217 const Reader& reader = Reader()) {
219 edgeset_reader.skipMap(name, reader);
223 /// \brief Add a new labeled node reader for the reader.
225 /// Add a new labeled node reader for the reader.
226 GraphReader& readNode(std::string name, Node& node) {
227 node_reader.readNode(name, node);
231 /// \brief Add a new labeled edge reader for the reader.
233 /// Add a new labeled edge reader for the reader.
234 GraphReader& readEdge(std::string name, Edge& edge) {
235 edge_reader.readEdge(name, edge);
238 /// \brief Add a new attribute reader command.
240 /// Add a new attribute reader command.
241 template <typename Value>
242 GraphReader& readAttribute(std::string name, Value& value) {
243 attribute_reader.readAttribute(name, value);
247 /// \brief Add a new attribute reader command.
249 /// Add a new attribute reader command.
250 template <typename Reader, typename Value>
251 GraphReader& readAttribute(std::string name, Value& value,
252 const Reader& reader) {
253 attribute_reader.readAttribute<Reader>(name, value, reader);
257 /// \brief Conversion operator to LemonReader.
259 /// Conversion operator to LemonReader. It make possible
260 /// to access the encapsulated \e LemonReader, this way
261 /// you can attach to this reader new instances of
262 /// \e LemonReader::SectionReader.
263 operator LemonReader&() {
267 /// \brief Executes the reader commands.
269 /// Executes the reader commands.
281 DefaultSkipper skipper;
283 NodeSetReader<Graph, ReaderTraits> nodeset_reader;
284 EdgeSetReader<Graph, ReaderTraits> edgeset_reader;
286 NodeReader<Graph> node_reader;
287 EdgeReader<Graph> edge_reader;
289 AttributeReader<ReaderTraits> attribute_reader;
292 /// \brief Read a graph from the input.
294 /// Read a graph from the input.
295 /// \param is The input stream.
296 /// \param g The graph.
297 /// \param capacity The capacity map.
298 /// \param s The source node.
299 /// \param t The target node.
300 /// \param cost The cost map.
301 template<typename Graph, typename CapacityMap, typename CostMap>
302 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
303 typename Graph::Node &s, typename Graph::Node &t,
305 GraphReader<Graph> reader(is, g);
306 reader.readEdgeMap("capacity", capacity);
307 reader.readEdgeMap("cost", cost);
308 reader.readNode("source", s);
309 reader.readNode("target", t);
313 /// \brief Read a graph from the input.
315 /// Read a graph from the input.
316 /// \param is The input stream.
317 /// \param g The graph.
318 /// \param capacity The capacity map.
319 /// \param s The source node.
320 /// \param t The target node.
321 template<typename Graph, typename CapacityMap>
322 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
323 typename Graph::Node &s, typename Graph::Node &t) {
324 GraphReader<Graph> reader(is, g);
325 reader.readEdgeMap("capacity", capacity);
326 reader.readNode("source", s);
327 reader.readNode("target", t);
331 /// \brief Read a graph from the input.
333 /// Read a graph from the input.
334 /// \param is The input stream.
335 /// \param g The graph.
336 /// \param capacity The capacity map.
337 /// \param s The source node.
338 template<typename Graph, typename CapacityMap>
339 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
340 typename Graph::Node &s) {
341 GraphReader<Graph> reader(is, g);
342 reader.readEdgeMap("capacity", capacity);
343 reader.readNode("source", s);
347 /// \brief Read a graph from the input.
349 /// Read a graph from the input.
350 /// \param is The input stream.
351 /// \param g The graph.
352 /// \param capacity The capacity map.
353 template<typename Graph, typename CapacityMap>
354 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
355 GraphReader<Graph> reader(is, g);
356 reader.readEdgeMap("capacity", capacity);
360 /// \brief Read a graph from the input.
362 /// Read a graph from the input.
363 /// \param is The input stream.
364 /// \param g The graph.
365 template<typename Graph>
366 void readGraph(std::istream& is, Graph &g) {
367 GraphReader<Graph> reader(is, g);