deba@1137
|
1 |
/* -*- C++ -*-
|
deba@1137
|
2 |
* src/lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library
|
deba@1137
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1137
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
deba@1137
|
6 |
*
|
deba@1137
|
7 |
* Permission to use, modify and distribute this software is granted
|
deba@1137
|
8 |
* provided that this copyright notice appears in all copies. For
|
deba@1137
|
9 |
* precise terms see the accompanying LICENSE file.
|
deba@1137
|
10 |
*
|
deba@1137
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1137
|
12 |
* express or implied, and with no claim as to its suitability for any
|
deba@1137
|
13 |
* purpose.
|
deba@1137
|
14 |
*
|
deba@1137
|
15 |
*/
|
deba@1137
|
16 |
|
deba@1137
|
17 |
///\ingroup gio
|
deba@1137
|
18 |
///\file
|
deba@1137
|
19 |
///\brief Graph reader.
|
deba@1137
|
20 |
|
deba@1137
|
21 |
#include <iostream>
|
deba@1137
|
22 |
#include <sstream>
|
deba@1137
|
23 |
|
deba@1137
|
24 |
#include <map>
|
deba@1137
|
25 |
#include <vector>
|
deba@1137
|
26 |
|
deba@1137
|
27 |
#include <memory>
|
deba@1137
|
28 |
|
deba@1137
|
29 |
#include <lemon/error.h>
|
deba@1137
|
30 |
|
deba@1137
|
31 |
/// \todo fix exceptions
|
deba@1137
|
32 |
|
deba@1137
|
33 |
|
deba@1137
|
34 |
namespace lemon {
|
deba@1137
|
35 |
|
deba@1137
|
36 |
// Exceptions
|
deba@1137
|
37 |
|
deba@1137
|
38 |
|
deba@1137
|
39 |
/// \brief Standard ReaderTraits for the GraphReader class.
|
deba@1137
|
40 |
///
|
deba@1137
|
41 |
/// Standard ReaderTraits for the GraphReader class.
|
deba@1137
|
42 |
/// It defines standard reading method for all type of value.
|
deba@1137
|
43 |
struct DefaultReaderTraits {
|
deba@1137
|
44 |
|
deba@1137
|
45 |
/// \brief Template class for reading an value.
|
deba@1137
|
46 |
///
|
deba@1137
|
47 |
/// Template class for reading an value.
|
deba@1137
|
48 |
template <typename _Value>
|
deba@1137
|
49 |
struct Reader {
|
deba@1137
|
50 |
/// The value type.
|
deba@1137
|
51 |
typedef _Value Value;
|
deba@1137
|
52 |
/// \brief Reads a value from the given stream.
|
deba@1137
|
53 |
///
|
deba@1137
|
54 |
/// Reads a value from the given stream.
|
deba@1137
|
55 |
void read(std::istream& is, Value& value) {
|
deba@1137
|
56 |
if (!(is >> value))
|
deba@1208
|
57 |
throw DataFormatError("Default reader format exception");
|
deba@1137
|
58 |
}
|
deba@1137
|
59 |
};
|
deba@1137
|
60 |
|
deba@1137
|
61 |
/// The reader class for the not needed maps.
|
deba@1137
|
62 |
typedef Reader<std::string> DefaultReader;
|
deba@1137
|
63 |
|
deba@1137
|
64 |
};
|
deba@1137
|
65 |
|
deba@1137
|
66 |
/// \brief Reader class for quoted strings.
|
deba@1137
|
67 |
///
|
deba@1137
|
68 |
/// Reader class for quoted strings. It can process the escape
|
deba@1137
|
69 |
/// sequences in the string.
|
deba@1137
|
70 |
class QuotedStringReader {
|
deba@1137
|
71 |
public:
|
deba@1137
|
72 |
typedef std::string Value;
|
deba@1137
|
73 |
|
deba@1137
|
74 |
/// \brief Constructor for the reader.
|
deba@1137
|
75 |
///
|
deba@1137
|
76 |
/// Constructor for the reader. If the given parameter is true
|
deba@1137
|
77 |
/// the reader processes the escape sequences.
|
deba@1137
|
78 |
QuotedStringReader(bool _escaped = true) : escaped(_escaped) {}
|
deba@1137
|
79 |
|
deba@1137
|
80 |
/// \brief Reads a quoted string from the given stream.
|
deba@1137
|
81 |
///
|
deba@1137
|
82 |
/// Reads a quoted string from the given stream.
|
deba@1137
|
83 |
void read(std::istream& is, std::string& value) {
|
deba@1137
|
84 |
char c;
|
deba@1137
|
85 |
value.clear();
|
deba@1137
|
86 |
is >> ws;
|
deba@1137
|
87 |
if (!is.get(c) || c != '\"')
|
deba@1208
|
88 |
throw DataFormatError("Quoted string format error");
|
deba@1137
|
89 |
while (is.get(c) && c != '\"') {
|
deba@1137
|
90 |
if (escaped && c == '\\') {
|
deba@1137
|
91 |
value += readEscape(is);
|
deba@1137
|
92 |
} else {
|
deba@1137
|
93 |
value += c;
|
deba@1137
|
94 |
}
|
deba@1137
|
95 |
}
|
deba@1208
|
96 |
if (!is) throw DataFormatError("Quoted string format error");
|
deba@1137
|
97 |
}
|
deba@1137
|
98 |
|
deba@1137
|
99 |
private:
|
deba@1137
|
100 |
|
deba@1137
|
101 |
static char readEscape(std::istream& is) {
|
deba@1137
|
102 |
char c;
|
deba@1137
|
103 |
switch (is.get(c), c) {
|
deba@1137
|
104 |
case '\\':
|
deba@1137
|
105 |
return '\\';
|
deba@1137
|
106 |
case '\"':
|
deba@1137
|
107 |
return '\"';
|
deba@1137
|
108 |
case '\'':
|
deba@1137
|
109 |
return '\'';
|
deba@1137
|
110 |
case '\?':
|
deba@1137
|
111 |
return '\?';
|
deba@1137
|
112 |
case 'a':
|
deba@1137
|
113 |
return '\a';
|
deba@1137
|
114 |
case 'b':
|
deba@1137
|
115 |
return '\b';
|
deba@1137
|
116 |
case 'f':
|
deba@1137
|
117 |
return '\f';
|
deba@1137
|
118 |
case 'n':
|
deba@1137
|
119 |
return '\n';
|
deba@1137
|
120 |
case 'r':
|
deba@1137
|
121 |
return '\r';
|
deba@1137
|
122 |
case 't':
|
deba@1137
|
123 |
return '\t';
|
deba@1137
|
124 |
case 'v':
|
deba@1137
|
125 |
return '\v';
|
deba@1137
|
126 |
case 'x':
|
deba@1137
|
127 |
{
|
deba@1137
|
128 |
int code;
|
deba@1137
|
129 |
if (!is.get(c) || !isHex(c))
|
deba@1208
|
130 |
throw DataFormatError("Escape format error");
|
deba@1137
|
131 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
|
deba@1137
|
132 |
else code = code * 16 + valueHex(c);
|
deba@1137
|
133 |
return code;
|
deba@1137
|
134 |
}
|
deba@1137
|
135 |
default:
|
deba@1137
|
136 |
{
|
deba@1137
|
137 |
int code;
|
deba@1137
|
138 |
if (!isOct(c))
|
deba@1208
|
139 |
throw DataFormatError("Escape format error");
|
deba@1137
|
140 |
else if (code = valueOct(c), !is.get(c) || !isOct(c))
|
deba@1137
|
141 |
is.putback(c);
|
deba@1137
|
142 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
|
deba@1137
|
143 |
is.putback(c);
|
deba@1137
|
144 |
else code = code * 8 + valueOct(c);
|
deba@1137
|
145 |
return code;
|
deba@1137
|
146 |
}
|
deba@1137
|
147 |
}
|
deba@1137
|
148 |
}
|
deba@1137
|
149 |
|
deba@1137
|
150 |
static bool isOct(char c) {
|
deba@1137
|
151 |
return '0' <= c && c <='7';
|
deba@1137
|
152 |
}
|
deba@1137
|
153 |
|
deba@1137
|
154 |
static int valueOct(char c) {
|
deba@1137
|
155 |
return c - '0';
|
deba@1137
|
156 |
}
|
deba@1137
|
157 |
|
deba@1137
|
158 |
static bool isHex(char c) {
|
deba@1137
|
159 |
return ('0' <= c && c <= '9') ||
|
deba@1137
|
160 |
('a' <= c && c <= 'z') ||
|
deba@1137
|
161 |
('A' <= c && c <= 'Z');
|
deba@1137
|
162 |
}
|
deba@1137
|
163 |
|
deba@1137
|
164 |
static int valueHex(char c) {
|
deba@1137
|
165 |
if ('0' <= c && c <= '9') return c - '0';
|
deba@1137
|
166 |
if ('a' <= c && c <= 'z') return c - 'a' + 10;
|
deba@1137
|
167 |
return c - 'A' + 10;
|
deba@1137
|
168 |
}
|
deba@1137
|
169 |
|
deba@1137
|
170 |
bool escaped;
|
deba@1137
|
171 |
};
|
deba@1137
|
172 |
|
deba@1137
|
173 |
/// \brief The graph reader class.
|
deba@1137
|
174 |
///
|
deba@1137
|
175 |
/// The reader class for the graph input.
|
alpar@1138
|
176 |
/// \see \ref GraphWriter
|
alpar@1138
|
177 |
/// \see \ref graph-io-page
|
deba@1137
|
178 |
template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits>
|
deba@1137
|
179 |
class GraphReader {
|
deba@1137
|
180 |
public:
|
deba@1137
|
181 |
|
deba@1137
|
182 |
typedef _Graph Graph;
|
deba@1137
|
183 |
typedef typename Graph::Node Node;
|
deba@1137
|
184 |
typedef typename Graph::Edge Edge;
|
deba@1137
|
185 |
|
deba@1137
|
186 |
typedef _ReaderTraits ReaderTraits;
|
deba@1137
|
187 |
typedef typename ReaderTraits::DefaultReader DefaultReader;
|
deba@1137
|
188 |
|
deba@1137
|
189 |
/// \brief Construct a new GraphReader.
|
deba@1137
|
190 |
///
|
deba@1208
|
191 |
/// Construct a new GraphReader. It reads into the given graph
|
deba@1208
|
192 |
/// and it use the given reader as the default skipper.
|
deba@1137
|
193 |
GraphReader(std::istream& _is, Graph& _graph,
|
deba@1137
|
194 |
const DefaultReader& _reader = DefaultReader())
|
deba@1137
|
195 |
: is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
|
deba@1137
|
196 |
|
deba@1137
|
197 |
/// \brief Destruct the graph reader.
|
deba@1137
|
198 |
///
|
deba@1137
|
199 |
/// Destruct the graph reader.
|
deba@1137
|
200 |
~GraphReader() {
|
deba@1137
|
201 |
|
deba@1137
|
202 |
for (typename NodeMapReaders::iterator it = node_map_readers.begin();
|
deba@1137
|
203 |
it != node_map_readers.end(); ++it) {
|
deba@1137
|
204 |
delete it->second;
|
deba@1137
|
205 |
}
|
deba@1137
|
206 |
|
deba@1137
|
207 |
for (typename EdgeMapReaders::iterator it = edge_map_readers.begin();
|
deba@1137
|
208 |
it != edge_map_readers.end(); ++it) {
|
deba@1137
|
209 |
delete it->second;
|
deba@1137
|
210 |
}
|
deba@1137
|
211 |
|
deba@1137
|
212 |
}
|
deba@1137
|
213 |
|
deba@1137
|
214 |
/// \brief Add a new node map reader command for the reader.
|
deba@1137
|
215 |
///
|
deba@1137
|
216 |
/// Add a new node map reader command for the reader.
|
deba@1137
|
217 |
template <typename Map>
|
deba@1137
|
218 |
GraphReader& addNodeMap(std::string name, Map& map) {
|
deba@1137
|
219 |
return addNodeMap<typename ReaderTraits::template
|
deba@1137
|
220 |
Reader<typename Map::Value>, Map>(name, map);
|
deba@1137
|
221 |
}
|
deba@1137
|
222 |
|
deba@1137
|
223 |
/// \brief Add a new node map reader command for the reader.
|
deba@1137
|
224 |
///
|
deba@1137
|
225 |
/// Add a new node map reader command for the reader.
|
deba@1137
|
226 |
template <typename Reader, typename Map>
|
deba@1137
|
227 |
GraphReader& addNodeMap(std::string name, Map& map,
|
deba@1137
|
228 |
const Reader& reader = Reader()) {
|
deba@1137
|
229 |
if (node_map_readers.find(name) != node_map_readers.end()) {
|
deba@1208
|
230 |
ErrorMessage msg;
|
deba@1208
|
231 |
msg << "Multiple read rule for node map: " << name;
|
deba@1208
|
232 |
throw IOLogicError(msg.message());
|
deba@1137
|
233 |
}
|
deba@1137
|
234 |
node_map_readers.insert(
|
deba@1137
|
235 |
make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
|
deba@1137
|
236 |
return *this;
|
deba@1137
|
237 |
}
|
deba@1137
|
238 |
|
deba@1137
|
239 |
/// \brief Add a new node map skipper command for the reader.
|
deba@1137
|
240 |
///
|
deba@1137
|
241 |
/// Add a new node map skipper command for the reader.
|
deba@1137
|
242 |
template <typename Reader>
|
deba@1137
|
243 |
GraphReader& skipNodeMap(std::string name,
|
deba@1137
|
244 |
const Reader& reader = Reader()) {
|
deba@1137
|
245 |
if (node_map_readers.find(name) != node_map_readers.end()) {
|
deba@1208
|
246 |
ErrorMessage msg;
|
deba@1208
|
247 |
msg << "Multiple read rule for node map: " << name;
|
deba@1208
|
248 |
throw IOLogicError(msg.message());
|
deba@1137
|
249 |
}
|
deba@1137
|
250 |
node_map_readers.insert(
|
deba@1137
|
251 |
make_pair(name, new SkipReader<Node, Reader>(reader)));
|
deba@1137
|
252 |
return *this;
|
deba@1137
|
253 |
}
|
deba@1137
|
254 |
|
deba@1137
|
255 |
/// \brief Add a new edge map reader command for the reader.
|
deba@1137
|
256 |
///
|
deba@1137
|
257 |
/// Add a new edge map reader command for the reader.
|
deba@1137
|
258 |
template <typename Map>
|
deba@1137
|
259 |
GraphReader& addEdgeMap(std::string name, Map& map) {
|
deba@1137
|
260 |
return addEdgeMap<typename ReaderTraits::template
|
deba@1137
|
261 |
Reader<typename Map::Value>, Map>(name, map);
|
deba@1137
|
262 |
}
|
deba@1137
|
263 |
|
deba@1137
|
264 |
|
deba@1137
|
265 |
/// \brief Add a new edge map reader command for the reader.
|
deba@1137
|
266 |
///
|
deba@1137
|
267 |
/// Add a new edge map reader command for the reader.
|
deba@1137
|
268 |
template <typename Reader, typename Map>
|
deba@1137
|
269 |
GraphReader& addEdgeMap(std::string name, Map& map,
|
deba@1137
|
270 |
const Reader& reader = Reader()) {
|
deba@1137
|
271 |
if (edge_map_readers.find(name) != edge_map_readers.end()) {
|
deba@1208
|
272 |
ErrorMessage msg;
|
deba@1208
|
273 |
msg << "Multiple read rule for edge map: " << name;
|
deba@1208
|
274 |
throw IOLogicError(msg.message());
|
deba@1137
|
275 |
}
|
deba@1137
|
276 |
edge_map_readers.insert(
|
deba@1137
|
277 |
make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
|
deba@1137
|
278 |
return *this;
|
deba@1137
|
279 |
}
|
deba@1137
|
280 |
|
deba@1137
|
281 |
/// \brief Add a new edge map skipper command for the reader.
|
deba@1137
|
282 |
///
|
deba@1137
|
283 |
/// Add a new edge map skipper command for the reader.
|
deba@1137
|
284 |
template <typename Reader>
|
deba@1137
|
285 |
GraphReader& skipEdgeMap(std::string name,
|
deba@1137
|
286 |
const Reader& reader = Reader()) {
|
deba@1137
|
287 |
if (edge_map_readers.find(name) != edge_map_readers.end()) {
|
deba@1208
|
288 |
ErrorMessage msg;
|
deba@1208
|
289 |
msg << "Multiple read rule for edge map: " << name;
|
deba@1208
|
290 |
throw IOLogicError(msg.message());
|
deba@1137
|
291 |
}
|
deba@1137
|
292 |
edge_map_readers.insert(
|
deba@1137
|
293 |
make_pair(name, new SkipReader<Edge, Reader>(reader)));
|
deba@1137
|
294 |
return *this;
|
deba@1137
|
295 |
}
|
deba@1137
|
296 |
|
deba@1137
|
297 |
/// \brief Add a new labeled node reader for the reader.
|
deba@1137
|
298 |
///
|
deba@1137
|
299 |
/// Add a new labeled node reader for the reader.
|
deba@1137
|
300 |
GraphReader& addNode(std::string name, Node& node) {
|
deba@1137
|
301 |
if (node_readers.find(name) != node_readers.end()) {
|
deba@1208
|
302 |
ErrorMessage msg;
|
deba@1208
|
303 |
msg << "Multiple read rule for node: " << name;
|
deba@1208
|
304 |
throw IOLogicError(msg.message());
|
deba@1137
|
305 |
}
|
deba@1137
|
306 |
node_readers.insert(make_pair(name, &node));
|
deba@1137
|
307 |
return *this;
|
deba@1137
|
308 |
}
|
deba@1137
|
309 |
|
deba@1137
|
310 |
/// \brief Add a new labeled edge reader for the reader.
|
deba@1137
|
311 |
///
|
deba@1137
|
312 |
/// Add a new labeled edge reader for the reader.
|
deba@1137
|
313 |
GraphReader& addEdge(std::string name, Edge& edge) {
|
deba@1137
|
314 |
if (edge_readers.find(name) != edge_readers.end()) {
|
deba@1208
|
315 |
ErrorMessage msg;
|
deba@1208
|
316 |
msg << "Multiple read rule for edge: " << name;
|
deba@1208
|
317 |
throw IOLogicError(msg.message());
|
deba@1137
|
318 |
}
|
deba@1137
|
319 |
edge_readers.insert(make_pair(name, &edge));
|
deba@1137
|
320 |
return *this;
|
deba@1137
|
321 |
}
|
deba@1137
|
322 |
|
deba@1137
|
323 |
/// \brief Executes the reader commands.
|
deba@1137
|
324 |
///
|
deba@1137
|
325 |
/// Executes the reader commands.
|
deba@1137
|
326 |
void run() {
|
deba@1137
|
327 |
int line_num = 0;
|
deba@1137
|
328 |
std::auto_ptr<InverterBase<Node> > nodeInverter;
|
deba@1137
|
329 |
std::auto_ptr<InverterBase<Edge> > edgeInverter;
|
deba@1137
|
330 |
try {
|
deba@1137
|
331 |
std::string line = readNotEmptyLine(is, line_num);
|
deba@1137
|
332 |
if (line.find("@nodeset") == 0) {
|
deba@1137
|
333 |
line = readNodeSet(line_num, nodeInverter);
|
deba@1137
|
334 |
}
|
deba@1137
|
335 |
if (line.find("@edgeset") == 0) {
|
deba@1137
|
336 |
line = readEdgeSet(line_num, edgeInverter, nodeInverter);
|
deba@1137
|
337 |
}
|
deba@1137
|
338 |
if (line.find("@nodes") == 0) {
|
deba@1137
|
339 |
line = readNodes(line_num, nodeInverter);
|
deba@1137
|
340 |
}
|
deba@1137
|
341 |
if (line.find("@edges") == 0) {
|
deba@1137
|
342 |
line = readEdges(line_num, edgeInverter);
|
deba@1137
|
343 |
}
|
deba@1137
|
344 |
if (line.find("@end") != 0) {
|
deba@1208
|
345 |
throw DataFormatError("Invalid control sequence error");
|
deba@1137
|
346 |
}
|
deba@1208
|
347 |
} catch (DataFormatError e) {
|
deba@1208
|
348 |
e.line(line_num);
|
deba@1208
|
349 |
throw e;
|
deba@1137
|
350 |
}
|
deba@1137
|
351 |
}
|
deba@1137
|
352 |
|
deba@1137
|
353 |
private:
|
deba@1137
|
354 |
|
deba@1137
|
355 |
template <typename Item> class InverterBase;
|
deba@1137
|
356 |
|
deba@1137
|
357 |
std::string readNodeSet(int& line_num,
|
deba@1137
|
358 |
auto_ptr<InverterBase<Node> > & nodeInverter) {
|
deba@1137
|
359 |
std::vector<ReaderBase<Node>* > index;
|
deba@1137
|
360 |
{
|
deba@1137
|
361 |
std::string line = readNotEmptyLine(is, line_num);
|
deba@1137
|
362 |
std::string id;
|
deba@1137
|
363 |
std::istringstream ls(line);
|
deba@1137
|
364 |
while (ls >> id) {
|
deba@1137
|
365 |
if (id[0] == '#') break;
|
deba@1137
|
366 |
typename NodeMapReaders::iterator it = node_map_readers.find(id);
|
deba@1137
|
367 |
if (it != node_map_readers.end()) {
|
deba@1137
|
368 |
index.push_back(it->second);
|
deba@1137
|
369 |
node_map_readers.erase(it);
|
deba@1137
|
370 |
} else {
|
deba@1137
|
371 |
index.push_back(&nodeSkipper);
|
deba@1137
|
372 |
}
|
deba@1137
|
373 |
}
|
deba@1137
|
374 |
}
|
deba@1137
|
375 |
|
deba@1137
|
376 |
if (index.size() == 0) {
|
deba@1208
|
377 |
throw DataFormatError("Cannot find node id map");
|
deba@1137
|
378 |
}
|
deba@1137
|
379 |
|
deba@1137
|
380 |
nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
|
deba@1137
|
381 |
std::string line;
|
deba@1137
|
382 |
while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
|
deba@1137
|
383 |
Node node = graph.addNode();
|
deba@1137
|
384 |
std::istringstream ls(line);
|
deba@1137
|
385 |
nodeInverter->read(ls, node);
|
deba@1137
|
386 |
for (int i = 1; i < (int)index.size(); ++i) {
|
deba@1137
|
387 |
index[i]->read(ls, node);
|
deba@1137
|
388 |
}
|
deba@1137
|
389 |
}
|
deba@1137
|
390 |
return line;
|
deba@1137
|
391 |
}
|
deba@1137
|
392 |
|
deba@1137
|
393 |
std::string readEdgeSet(int& line_num,
|
deba@1137
|
394 |
auto_ptr<InverterBase<Edge> > & edgeInverter,
|
deba@1137
|
395 |
auto_ptr<InverterBase<Node> > & nodeInverter) {
|
deba@1137
|
396 |
std::vector<ReaderBase<Edge>*> index;
|
deba@1137
|
397 |
{
|
deba@1137
|
398 |
std::string line = readNotEmptyLine(is, line_num);
|
deba@1137
|
399 |
std::string id;
|
deba@1137
|
400 |
std::istringstream ls(line);
|
deba@1137
|
401 |
while (ls >> id) {
|
deba@1137
|
402 |
if (id[0] == '#') break;
|
deba@1137
|
403 |
typename EdgeMapReaders::iterator it = edge_map_readers.find(id);
|
deba@1137
|
404 |
if (it != edge_map_readers.end()) {
|
deba@1137
|
405 |
index.push_back(it->second);
|
deba@1137
|
406 |
edge_map_readers.erase(it);
|
deba@1137
|
407 |
} else {
|
deba@1137
|
408 |
index.push_back(&edgeSkipper);
|
deba@1137
|
409 |
}
|
deba@1137
|
410 |
}
|
deba@1137
|
411 |
}
|
deba@1137
|
412 |
|
deba@1137
|
413 |
if (index.size() == 0) {
|
deba@1208
|
414 |
throw DataFormatError("Cannot find edge id map");
|
deba@1137
|
415 |
}
|
deba@1137
|
416 |
|
deba@1137
|
417 |
edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
|
deba@1137
|
418 |
std::string line;
|
deba@1137
|
419 |
while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
|
deba@1137
|
420 |
std::istringstream ls(line);
|
deba@1137
|
421 |
Node source = nodeInverter->read(ls);
|
deba@1137
|
422 |
Node target = nodeInverter->read(ls);
|
deba@1137
|
423 |
Edge edge = graph.addEdge(source, target);
|
deba@1137
|
424 |
edgeInverter->read(ls, edge);
|
deba@1137
|
425 |
for (int i = 1; i < (int)index.size(); ++i) {
|
deba@1137
|
426 |
index[i]->read(ls, edge);
|
deba@1137
|
427 |
}
|
deba@1137
|
428 |
}
|
deba@1137
|
429 |
return line;
|
deba@1137
|
430 |
}
|
deba@1137
|
431 |
|
deba@1137
|
432 |
std::string readNodes(int& line_num,
|
deba@1137
|
433 |
auto_ptr<InverterBase<Node> >& nodeInverter) {
|
deba@1137
|
434 |
std::string line;
|
deba@1137
|
435 |
while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
|
deba@1137
|
436 |
std::istringstream ls(line);
|
deba@1137
|
437 |
std::string name;
|
deba@1137
|
438 |
ls >> name;
|
deba@1137
|
439 |
typename NodeReaders::iterator it = node_readers.find(name);
|
deba@1137
|
440 |
if (it != node_readers.end()) {
|
deba@1137
|
441 |
*(it -> second) = nodeInverter->read(ls);
|
deba@1137
|
442 |
}
|
deba@1137
|
443 |
}
|
deba@1137
|
444 |
return line;
|
deba@1137
|
445 |
}
|
deba@1137
|
446 |
|
deba@1137
|
447 |
std::string readEdges(int& line_num,
|
deba@1137
|
448 |
auto_ptr<InverterBase<Edge> >& edgeInverter) {
|
deba@1137
|
449 |
std::string line;
|
deba@1137
|
450 |
while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
|
deba@1137
|
451 |
std::istringstream ls(line);
|
deba@1137
|
452 |
std::string name;
|
deba@1137
|
453 |
ls >> name;
|
deba@1137
|
454 |
typename EdgeReaders::iterator it = edge_readers.find(name);
|
deba@1137
|
455 |
if (it != edge_readers.end()) {
|
deba@1137
|
456 |
*(it -> second) = edgeInverter->read(ls);
|
deba@1137
|
457 |
}
|
deba@1137
|
458 |
}
|
deba@1137
|
459 |
return line;
|
deba@1137
|
460 |
}
|
deba@1137
|
461 |
|
deba@1137
|
462 |
std::string readNotEmptyLine(std::istream& is, int& line_num) {
|
deba@1137
|
463 |
std::string line;
|
deba@1137
|
464 |
while (++line_num, getline(is, line)) {
|
deba@1137
|
465 |
int vi = line.find_first_not_of(" \t");
|
deba@1137
|
466 |
if (vi != (int)string::npos && line[vi] != '#') {
|
deba@1137
|
467 |
return line.substr(vi);
|
deba@1137
|
468 |
}
|
deba@1137
|
469 |
}
|
deba@1208
|
470 |
throw DataFormatError("End of stream error");
|
deba@1137
|
471 |
}
|
deba@1137
|
472 |
|
deba@1137
|
473 |
// Inverters store and give back the Item from the id,
|
deba@1137
|
474 |
// and may put the ids into a map.
|
deba@1137
|
475 |
|
deba@1137
|
476 |
template <typename _Item>
|
deba@1137
|
477 |
class InverterBase {
|
deba@1137
|
478 |
public:
|
deba@1137
|
479 |
typedef _Item Item;
|
deba@1137
|
480 |
virtual void read(std::istream&, const Item&) = 0;
|
deba@1137
|
481 |
virtual Item read(std::istream&) = 0;
|
deba@1137
|
482 |
};
|
deba@1137
|
483 |
|
deba@1137
|
484 |
template <typename _Item, typename _Map, typename _Reader>
|
deba@1137
|
485 |
class MapReaderInverter : public InverterBase<_Item> {
|
deba@1137
|
486 |
public:
|
deba@1137
|
487 |
typedef _Item Item;
|
deba@1137
|
488 |
typedef _Reader Reader;
|
deba@1137
|
489 |
typedef typename Reader::Value Value;
|
deba@1137
|
490 |
typedef _Map Map;
|
deba@1137
|
491 |
typedef std::map<Value, Item> Inverse;
|
deba@1137
|
492 |
|
deba@1137
|
493 |
Map& map;
|
deba@1137
|
494 |
Reader reader;
|
deba@1137
|
495 |
Inverse inverse;
|
deba@1137
|
496 |
|
deba@1137
|
497 |
MapReaderInverter(Map& _map, const Reader& _reader)
|
deba@1137
|
498 |
: map(_map), reader(_reader) {}
|
deba@1137
|
499 |
|
deba@1137
|
500 |
virtual ~MapReaderInverter() {}
|
deba@1137
|
501 |
|
deba@1137
|
502 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1137
|
503 |
Value value;
|
deba@1137
|
504 |
reader.read(is, value);
|
deba@1137
|
505 |
map.set(item, value);
|
deba@1137
|
506 |
typename Inverse::iterator it = inverse.find(value);
|
deba@1137
|
507 |
if (it == inverse.end()) {
|
deba@1137
|
508 |
inverse.insert(make_pair(value, item));
|
deba@1137
|
509 |
} else {
|
deba@1208
|
510 |
throw DataFormatError("Multiple ID occurence");
|
deba@1137
|
511 |
}
|
deba@1137
|
512 |
}
|
deba@1137
|
513 |
|
deba@1137
|
514 |
virtual Item read(std::istream& is) {
|
deba@1137
|
515 |
Value value;
|
deba@1137
|
516 |
reader.read(is, value);
|
deba@1137
|
517 |
typename Inverse::const_iterator it = inverse.find(value);
|
deba@1137
|
518 |
if (it != inverse.end()) {
|
deba@1137
|
519 |
return it->second;
|
deba@1137
|
520 |
} else {
|
deba@1208
|
521 |
throw DataFormatError("Invalid ID error");
|
deba@1137
|
522 |
}
|
deba@1137
|
523 |
}
|
deba@1137
|
524 |
};
|
deba@1137
|
525 |
|
deba@1137
|
526 |
template <typename _Item, typename _Reader>
|
deba@1137
|
527 |
class SkipReaderInverter : public InverterBase<_Item> {
|
deba@1137
|
528 |
public:
|
deba@1137
|
529 |
typedef _Item Item;
|
deba@1137
|
530 |
typedef _Reader Reader;
|
deba@1137
|
531 |
typedef typename Reader::Value Value;
|
deba@1137
|
532 |
typedef std::map<Value, Item> Inverse;
|
deba@1137
|
533 |
|
deba@1137
|
534 |
Reader reader;
|
deba@1137
|
535 |
|
deba@1137
|
536 |
SkipReaderInverter(const Reader& _reader)
|
deba@1137
|
537 |
: reader(_reader) {}
|
deba@1137
|
538 |
|
deba@1137
|
539 |
virtual ~SkipReaderInverter() {}
|
deba@1137
|
540 |
|
deba@1137
|
541 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1137
|
542 |
Value value;
|
deba@1137
|
543 |
reader.read(is, value);
|
deba@1137
|
544 |
typename Inverse::iterator it = inverse.find(value);
|
deba@1137
|
545 |
if (it == inverse.end()) {
|
deba@1137
|
546 |
inverse.insert(make_pair(value, item));
|
deba@1137
|
547 |
} else {
|
deba@1208
|
548 |
throw DataFormatError("Multiple ID occurence error");
|
deba@1137
|
549 |
}
|
deba@1137
|
550 |
}
|
deba@1137
|
551 |
|
deba@1137
|
552 |
virtual Item read(std::istream& is) {
|
deba@1137
|
553 |
Value value;
|
deba@1137
|
554 |
reader.read(is, value);
|
deba@1137
|
555 |
typename Inverse::const_iterator it = inverse.find(value);
|
deba@1137
|
556 |
if (it != inverse.end()) {
|
deba@1137
|
557 |
return it->second;
|
deba@1137
|
558 |
} else {
|
deba@1208
|
559 |
throw DataFormatError("Invalid ID error");
|
deba@1137
|
560 |
}
|
deba@1137
|
561 |
}
|
deba@1137
|
562 |
private:
|
deba@1137
|
563 |
Inverse inverse;
|
deba@1137
|
564 |
};
|
deba@1137
|
565 |
|
deba@1137
|
566 |
// Readers
|
deba@1137
|
567 |
|
deba@1137
|
568 |
template <typename _Item>
|
deba@1137
|
569 |
class ReaderBase {
|
deba@1137
|
570 |
public:
|
deba@1137
|
571 |
typedef _Item Item;
|
deba@1137
|
572 |
|
deba@1137
|
573 |
// virtual ~ReaderBase() {}
|
deba@1137
|
574 |
|
deba@1137
|
575 |
virtual void read(std::istream& is, const Item& item) = 0;
|
deba@1137
|
576 |
virtual InverterBase<_Item>* getInverter() = 0;
|
deba@1137
|
577 |
};
|
deba@1137
|
578 |
|
deba@1137
|
579 |
template <typename _Item, typename _Map, typename _Reader>
|
deba@1137
|
580 |
class MapReader : public ReaderBase<_Item> {
|
deba@1137
|
581 |
public:
|
deba@1137
|
582 |
typedef _Map Map;
|
deba@1137
|
583 |
typedef _Reader Reader;
|
deba@1137
|
584 |
typedef typename Reader::Value Value;
|
deba@1137
|
585 |
typedef _Item Item;
|
deba@1137
|
586 |
|
deba@1137
|
587 |
Map& map;
|
deba@1137
|
588 |
Reader reader;
|
deba@1137
|
589 |
|
deba@1137
|
590 |
MapReader(Map& _map, const Reader& _reader)
|
deba@1137
|
591 |
: map(_map), reader(_reader) {}
|
deba@1137
|
592 |
|
deba@1137
|
593 |
virtual ~MapReader() {}
|
deba@1137
|
594 |
|
deba@1137
|
595 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1137
|
596 |
Value value;
|
deba@1137
|
597 |
reader.read(is, value);
|
deba@1137
|
598 |
map.set(item, value);
|
deba@1137
|
599 |
}
|
deba@1137
|
600 |
|
deba@1137
|
601 |
virtual InverterBase<_Item>* getInverter() {
|
deba@1137
|
602 |
return new MapReaderInverter<Item, Map, Reader>(map, reader);
|
deba@1137
|
603 |
}
|
deba@1137
|
604 |
};
|
deba@1137
|
605 |
|
deba@1137
|
606 |
|
deba@1137
|
607 |
template <typename _Item, typename _Reader>
|
deba@1137
|
608 |
class SkipReader : public ReaderBase<_Item> {
|
deba@1137
|
609 |
public:
|
deba@1137
|
610 |
typedef _Reader Reader;
|
deba@1137
|
611 |
typedef typename Reader::Value Value;
|
deba@1137
|
612 |
typedef _Item Item;
|
deba@1137
|
613 |
|
deba@1137
|
614 |
Reader reader;
|
deba@1137
|
615 |
SkipReader(const Reader& _reader) : reader(_reader) {}
|
deba@1137
|
616 |
|
deba@1137
|
617 |
virtual ~SkipReader() {}
|
deba@1137
|
618 |
|
deba@1137
|
619 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1137
|
620 |
Value value;
|
deba@1137
|
621 |
reader.read(is, value);
|
deba@1137
|
622 |
}
|
deba@1137
|
623 |
|
deba@1137
|
624 |
virtual InverterBase<Item>* getInverter() {
|
deba@1137
|
625 |
return new SkipReaderInverter<Item, Reader>(reader);
|
deba@1137
|
626 |
}
|
deba@1137
|
627 |
};
|
deba@1137
|
628 |
|
deba@1137
|
629 |
|
deba@1137
|
630 |
typedef std::map<std::string, ReaderBase<Node>*> NodeMapReaders;
|
deba@1137
|
631 |
NodeMapReaders node_map_readers;
|
deba@1137
|
632 |
|
deba@1137
|
633 |
typedef std::map<std::string, ReaderBase<Edge>*> EdgeMapReaders;
|
deba@1137
|
634 |
EdgeMapReaders edge_map_readers;
|
deba@1137
|
635 |
|
deba@1137
|
636 |
typedef std::map<std::string, Node*> NodeReaders;
|
deba@1137
|
637 |
NodeReaders node_readers;
|
deba@1137
|
638 |
|
deba@1137
|
639 |
typedef std::map<std::string, Edge*> EdgeReaders;
|
deba@1137
|
640 |
EdgeReaders edge_readers;
|
deba@1137
|
641 |
|
deba@1137
|
642 |
std::istream& is;
|
deba@1137
|
643 |
Graph& graph;
|
deba@1137
|
644 |
|
deba@1137
|
645 |
SkipReader<Node, DefaultReader> nodeSkipper;
|
deba@1137
|
646 |
SkipReader<Edge, DefaultReader> edgeSkipper;
|
deba@1137
|
647 |
|
deba@1137
|
648 |
};
|
deba@1137
|
649 |
|
deba@1208
|
650 |
/// Ready to use reader function.
|
deba@1208
|
651 |
template<typename Graph, typename CapacityMap, typename CostMap>
|
deba@1208
|
652 |
void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
|
deba@1208
|
653 |
typename Graph::Node &s, typename Graph::Node &t,
|
deba@1208
|
654 |
CostMap& cost) {
|
deba@1208
|
655 |
GraphReader<Graph> reader(is, g);
|
deba@1208
|
656 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
657 |
reader.addEdgeMap("cost", cost);
|
deba@1208
|
658 |
reader.addNode("source", s);
|
deba@1208
|
659 |
reader.addNode("target", t);
|
deba@1208
|
660 |
reader.run();
|
deba@1208
|
661 |
}
|
deba@1208
|
662 |
|
deba@1208
|
663 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
664 |
void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
|
deba@1208
|
665 |
typename Graph::Node &s, typename Graph::Node &t) {
|
deba@1208
|
666 |
GraphReader<Graph> reader(is, g);
|
deba@1208
|
667 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
668 |
reader.addNode("source", s);
|
deba@1208
|
669 |
reader.addNode("target", t);
|
deba@1208
|
670 |
reader.run();
|
deba@1208
|
671 |
}
|
deba@1208
|
672 |
|
deba@1208
|
673 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
674 |
void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
|
deba@1208
|
675 |
typename Graph::Node &s) {
|
deba@1208
|
676 |
GraphReader<Graph> reader(is, g);
|
deba@1208
|
677 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
678 |
reader.addNode("source", s);
|
deba@1208
|
679 |
reader.run();
|
deba@1208
|
680 |
}
|
deba@1208
|
681 |
|
deba@1208
|
682 |
template<typename Graph, typename CapacityMap>
|
deba@1208
|
683 |
void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
|
deba@1208
|
684 |
GraphReader<Graph> reader(is, g);
|
deba@1208
|
685 |
reader.addEdgeMap("capacity", capacity);
|
deba@1208
|
686 |
reader.run();
|
deba@1208
|
687 |
}
|
deba@1208
|
688 |
|
deba@1208
|
689 |
template<typename Graph>
|
deba@1208
|
690 |
void readGraph(std::istream& is, Graph &g) {
|
deba@1208
|
691 |
GraphReader<Graph> reader(is, g);
|
deba@1208
|
692 |
reader.run();
|
deba@1208
|
693 |
}
|
deba@1208
|
694 |
|
deba@1137
|
695 |
}
|