1 |
1 |
/* -*- C++ -*-
|
2 |
2 |
*
|
3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
4 |
4 |
*
|
5 |
5 |
* Copyright (C) 2003-2008
|
6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
8 |
8 |
*
|
9 |
9 |
* Permission to use, modify and distribute this software is granted
|
10 |
10 |
* provided that this copyright notice appears in all copies. For
|
11 |
11 |
* precise terms see the accompanying LICENSE file.
|
12 |
12 |
*
|
13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
15 |
15 |
* purpose.
|
16 |
16 |
*
|
17 |
17 |
*/
|
18 |
18 |
|
19 |
19 |
///\ingroup lemon_io
|
20 |
20 |
///\file
|
21 |
21 |
///\brief \ref lgf-format "Lemon Graph Format" reader.
|
22 |
22 |
|
23 |
23 |
|
24 |
24 |
#ifndef LEMON_LGF_READER_H
|
25 |
25 |
#define LEMON_LGF_READER_H
|
26 |
26 |
|
27 |
27 |
#include <iostream>
|
28 |
28 |
#include <fstream>
|
29 |
29 |
#include <sstream>
|
30 |
30 |
|
31 |
31 |
#include <set>
|
32 |
32 |
#include <map>
|
33 |
33 |
|
34 |
34 |
#include <lemon/assert.h>
|
35 |
35 |
#include <lemon/graph_utils.h>
|
36 |
36 |
|
37 |
37 |
#include <lemon/lgf_writer.h>
|
38 |
38 |
|
39 |
39 |
#include <lemon/concept_check.h>
|
40 |
40 |
#include <lemon/concepts/maps.h>
|
41 |
41 |
|
42 |
42 |
namespace lemon {
|
43 |
43 |
|
44 |
44 |
namespace _reader_bits {
|
45 |
45 |
|
46 |
46 |
template <typename Value>
|
47 |
47 |
struct DefaultConverter {
|
48 |
48 |
Value operator()(const std::string& str) {
|
49 |
49 |
std::istringstream is(str);
|
50 |
50 |
Value value;
|
51 |
51 |
is >> value;
|
52 |
52 |
|
53 |
53 |
char c;
|
54 |
54 |
if (is >> std::ws >> c) {
|
55 |
55 |
throw DataFormatError("Remaining characters in token");
|
56 |
56 |
}
|
57 |
57 |
return value;
|
58 |
58 |
}
|
59 |
59 |
};
|
60 |
60 |
|
61 |
61 |
template <>
|
62 |
62 |
struct DefaultConverter<std::string> {
|
63 |
63 |
std::string operator()(const std::string& str) {
|
64 |
64 |
return str;
|
65 |
65 |
}
|
66 |
66 |
};
|
67 |
67 |
|
68 |
68 |
template <typename _Item>
|
69 |
69 |
class MapStorageBase {
|
70 |
70 |
public:
|
71 |
71 |
typedef _Item Item;
|
72 |
72 |
|
73 |
73 |
public:
|
74 |
74 |
MapStorageBase() {}
|
75 |
75 |
virtual ~MapStorageBase() {}
|
76 |
76 |
|
77 |
77 |
virtual void set(const Item& item, const std::string& value) = 0;
|
78 |
78 |
|
79 |
79 |
};
|
80 |
80 |
|
81 |
81 |
template <typename _Item, typename _Map,
|
82 |
82 |
typename _Converter = DefaultConverter<typename _Map::Value> >
|
83 |
83 |
class MapStorage : public MapStorageBase<_Item> {
|
84 |
84 |
public:
|
85 |
85 |
typedef _Map Map;
|
86 |
86 |
typedef _Converter Converter;
|
87 |
87 |
typedef _Item Item;
|
88 |
88 |
|
89 |
89 |
private:
|
90 |
90 |
Map& _map;
|
91 |
91 |
Converter _converter;
|
92 |
92 |
|
93 |
93 |
public:
|
94 |
94 |
MapStorage(Map& map, const Converter& converter = Converter())
|
95 |
95 |
: _map(map), _converter(converter) {}
|
96 |
96 |
virtual ~MapStorage() {}
|
97 |
97 |
|
98 |
98 |
virtual void set(const Item& item ,const std::string& value) {
|
99 |
99 |
_map.set(item, _converter(value));
|
100 |
100 |
}
|
101 |
101 |
};
|
102 |
102 |
|
103 |
103 |
template <typename _Graph, bool _dir, typename _Map,
|
104 |
104 |
typename _Converter = DefaultConverter<typename _Map::Value> >
|
105 |
105 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
106 |
106 |
public:
|
107 |
107 |
typedef _Map Map;
|
108 |
108 |
typedef _Converter Converter;
|
109 |
109 |
typedef _Graph Graph;
|
110 |
110 |
typedef typename Graph::Edge Item;
|
111 |
111 |
static const bool dir = _dir;
|
112 |
112 |
|
113 |
113 |
private:
|
114 |
114 |
const Graph& _graph;
|
115 |
115 |
Map& _map;
|
116 |
116 |
Converter _converter;
|
117 |
117 |
|
118 |
118 |
public:
|
119 |
119 |
GraphArcMapStorage(const Graph& graph, Map& map,
|
120 |
120 |
const Converter& converter = Converter())
|
121 |
121 |
: _graph(graph), _map(map), _converter(converter) {}
|
122 |
122 |
virtual ~GraphArcMapStorage() {}
|
123 |
123 |
|
124 |
124 |
virtual void set(const Item& item ,const std::string& value) {
|
125 |
125 |
_map.set(_graph.direct(item, dir), _converter(value));
|
126 |
126 |
}
|
127 |
127 |
};
|
128 |
128 |
|
129 |
129 |
class ValueStorageBase {
|
130 |
130 |
public:
|
131 |
131 |
ValueStorageBase() {}
|
132 |
132 |
virtual ~ValueStorageBase() {}
|
133 |
133 |
|
134 |
134 |
virtual void set(const std::string&) = 0;
|
135 |
135 |
};
|
136 |
136 |
|
137 |
137 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> >
|
138 |
138 |
class ValueStorage : public ValueStorageBase {
|
139 |
139 |
public:
|
140 |
140 |
typedef _Value Value;
|
141 |
141 |
typedef _Converter Converter;
|
142 |
142 |
|
143 |
143 |
private:
|
144 |
144 |
Value& _value;
|
145 |
145 |
Converter _converter;
|
146 |
146 |
|
147 |
147 |
public:
|
148 |
148 |
ValueStorage(Value& value, const Converter& converter = Converter())
|
149 |
149 |
: _value(value), _converter(converter) {}
|
150 |
150 |
|
151 |
151 |
virtual void set(const std::string& value) {
|
152 |
152 |
_value = _converter(value);
|
153 |
153 |
}
|
154 |
154 |
};
|
155 |
155 |
|
156 |
156 |
template <typename Value>
|
157 |
157 |
struct MapLookUpConverter {
|
158 |
158 |
const std::map<std::string, Value>& _map;
|
159 |
159 |
|
160 |
160 |
MapLookUpConverter(const std::map<std::string, Value>& map)
|
161 |
161 |
: _map(map) {}
|
162 |
162 |
|
163 |
163 |
Value operator()(const std::string& str) {
|
164 |
164 |
typename std::map<std::string, Value>::const_iterator it =
|
165 |
165 |
_map.find(str);
|
166 |
166 |
if (it == _map.end()) {
|
167 |
167 |
std::ostringstream msg;
|
168 |
168 |
msg << "Item not found: " << str;
|
169 |
169 |
throw DataFormatError(msg.str().c_str());
|
170 |
170 |
}
|
171 |
171 |
return it->second;
|
172 |
172 |
}
|
173 |
173 |
};
|
174 |
174 |
|
175 |
175 |
template <typename Graph>
|
176 |
176 |
struct GraphArcLookUpConverter {
|
177 |
177 |
const Graph& _graph;
|
178 |
178 |
const std::map<std::string, typename Graph::Edge>& _map;
|
179 |
179 |
|
180 |
180 |
GraphArcLookUpConverter(const Graph& graph,
|
181 |
181 |
const std::map<std::string,
|
182 |
182 |
typename Graph::Edge>& map)
|
183 |
183 |
: _graph(graph), _map(map) {}
|
184 |
184 |
|
185 |
185 |
typename Graph::Arc operator()(const std::string& str) {
|
186 |
186 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) {
|
187 |
187 |
throw DataFormatError("Item must start with '+' or '-'");
|
188 |
188 |
}
|
189 |
189 |
typename std::map<std::string, typename Graph::Edge>
|
190 |
190 |
::const_iterator it = _map.find(str.substr(1));
|
191 |
191 |
if (it == _map.end()) {
|
192 |
192 |
throw DataFormatError("Item not found");
|
193 |
193 |
}
|
194 |
194 |
return _graph.direct(it->second, str[0] == '+');
|
195 |
195 |
}
|
196 |
196 |
};
|
197 |
197 |
|
198 |
198 |
inline bool isWhiteSpace(char c) {
|
199 |
199 |
return c == ' ' || c == '\t' || c == '\v' ||
|
200 |
200 |
c == '\n' || c == '\r' || c == '\f';
|
201 |
201 |
}
|
202 |
202 |
|
203 |
203 |
inline bool isOct(char c) {
|
204 |
204 |
return '0' <= c && c <='7';
|
205 |
205 |
}
|
206 |
206 |
|
207 |
207 |
inline int valueOct(char c) {
|
208 |
208 |
LEMON_ASSERT(isOct(c), "The character is not octal.");
|
209 |
209 |
return c - '0';
|
210 |
210 |
}
|
211 |
211 |
|
212 |
212 |
inline bool isHex(char c) {
|
213 |
213 |
return ('0' <= c && c <= '9') ||
|
214 |
214 |
('a' <= c && c <= 'z') ||
|
215 |
215 |
('A' <= c && c <= 'Z');
|
216 |
216 |
}
|
217 |
217 |
|
218 |
218 |
inline int valueHex(char c) {
|
219 |
219 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
|
220 |
220 |
if ('0' <= c && c <= '9') return c - '0';
|
221 |
221 |
if ('a' <= c && c <= 'z') return c - 'a' + 10;
|
222 |
222 |
return c - 'A' + 10;
|
223 |
223 |
}
|
224 |
224 |
|
225 |
225 |
inline bool isIdentifierFirstChar(char c) {
|
226 |
226 |
return ('a' <= c && c <= 'z') ||
|
227 |
227 |
('A' <= c && c <= 'Z') || c == '_';
|
228 |
228 |
}
|
229 |
229 |
|
230 |
230 |
inline bool isIdentifierChar(char c) {
|
231 |
231 |
return isIdentifierFirstChar(c) ||
|
232 |
232 |
('0' <= c && c <= '9');
|
233 |
233 |
}
|
234 |
234 |
|
235 |
235 |
inline char readEscape(std::istream& is) {
|
236 |
236 |
char c;
|
237 |
237 |
if (!is.get(c))
|
238 |
238 |
throw DataFormatError("Escape format error");
|
239 |
239 |
|
240 |
240 |
switch (c) {
|
241 |
241 |
case '\\':
|
242 |
242 |
return '\\';
|
243 |
243 |
case '\"':
|
244 |
244 |
return '\"';
|
245 |
245 |
case '\'':
|
246 |
246 |
return '\'';
|
247 |
247 |
case '\?':
|
248 |
248 |
return '\?';
|
249 |
249 |
case 'a':
|
250 |
250 |
return '\a';
|
251 |
251 |
case 'b':
|
252 |
252 |
return '\b';
|
253 |
253 |
case 'f':
|
254 |
254 |
return '\f';
|
255 |
255 |
case 'n':
|
256 |
256 |
return '\n';
|
257 |
257 |
case 'r':
|
258 |
258 |
return '\r';
|
259 |
259 |
case 't':
|
260 |
260 |
return '\t';
|
261 |
261 |
case 'v':
|
262 |
262 |
return '\v';
|
263 |
263 |
case 'x':
|
264 |
264 |
{
|
265 |
265 |
int code;
|
266 |
266 |
if (!is.get(c) || !isHex(c))
|
267 |
267 |
throw DataFormatError("Escape format error");
|
268 |
268 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
|
269 |
269 |
else code = code * 16 + valueHex(c);
|
270 |
270 |
return code;
|
271 |
271 |
}
|
272 |
272 |
default:
|
273 |
273 |
{
|
274 |
274 |
int code;
|
275 |
275 |
if (!isOct(c))
|
276 |
276 |
throw DataFormatError("Escape format error");
|
277 |
277 |
else if (code = valueOct(c), !is.get(c) || !isOct(c))
|
278 |
278 |
is.putback(c);
|
279 |
279 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
|
280 |
280 |
is.putback(c);
|
281 |
281 |
else code = code * 8 + valueOct(c);
|
282 |
282 |
return code;
|
283 |
283 |
}
|
284 |
284 |
}
|
285 |
285 |
}
|
286 |
286 |
|
287 |
287 |
inline std::istream& readToken(std::istream& is, std::string& str) {
|
288 |
288 |
std::ostringstream os;
|
289 |
289 |
|
290 |
290 |
char c;
|
291 |
291 |
is >> std::ws;
|
292 |
292 |
|
293 |
293 |
if (!is.get(c))
|
294 |
294 |
return is;
|
295 |
295 |
|
296 |
296 |
if (c == '\"') {
|
297 |
297 |
while (is.get(c) && c != '\"') {
|
298 |
298 |
if (c == '\\')
|
299 |
299 |
c = readEscape(is);
|
300 |
300 |
os << c;
|
301 |
301 |
}
|
302 |
302 |
if (!is)
|
303 |
303 |
throw DataFormatError("Quoted format error");
|
304 |
304 |
} else {
|
305 |
305 |
is.putback(c);
|
306 |
306 |
while (is.get(c) && !isWhiteSpace(c)) {
|
307 |
307 |
if (c == '\\')
|
308 |
308 |
c = readEscape(is);
|
309 |
309 |
os << c;
|
310 |
310 |
}
|
311 |
311 |
if (!is) {
|
312 |
312 |
is.clear();
|
313 |
313 |
} else {
|
314 |
314 |
is.putback(c);
|
315 |
315 |
}
|
316 |
316 |
}
|
317 |
317 |
str = os.str();
|
318 |
318 |
return is;
|
319 |
319 |
}
|
320 |
320 |
|
321 |
321 |
class Section {
|
322 |
322 |
public:
|
323 |
323 |
virtual ~Section() {}
|
324 |
324 |
virtual void process(std::istream& is, int& line_num) = 0;
|
325 |
325 |
};
|
326 |
326 |
|
327 |
327 |
template <typename Functor>
|
328 |
328 |
class LineSection : public Section {
|
329 |
329 |
private:
|
330 |
330 |
|
331 |
331 |
Functor _functor;
|
332 |
332 |
|
333 |
333 |
public:
|
334 |
334 |
|
335 |
335 |
LineSection(const Functor& functor) : _functor(functor) {}
|
336 |
336 |
virtual ~LineSection() {}
|
337 |
337 |
|
338 |
338 |
virtual void process(std::istream& is, int& line_num) {
|
339 |
339 |
char c;
|
340 |
340 |
std::string line;
|
341 |
341 |
while (is.get(c) && c != '@') {
|
342 |
342 |
if (c == '\n') {
|
343 |
343 |
++line_num;
|
344 |
344 |
} else if (c == '#') {
|
345 |
345 |
getline(is, line);
|
346 |
346 |
++line_num;
|
347 |
347 |
} else if (!isWhiteSpace(c)) {
|
348 |
348 |
is.putback(c);
|
349 |
349 |
getline(is, line);
|
350 |
350 |
_functor(line);
|
351 |
351 |
++line_num;
|
352 |
352 |
}
|
353 |
353 |
}
|
354 |
354 |
if (is) is.putback(c);
|
355 |
355 |
else if (is.eof()) is.clear();
|
356 |
356 |
}
|
357 |
357 |
};
|
358 |
358 |
|
359 |
359 |
template <typename Functor>
|
360 |
360 |
class StreamSection : public Section {
|
361 |
361 |
private:
|
362 |
362 |
|
363 |
363 |
Functor _functor;
|
364 |
364 |
|
365 |
365 |
public:
|
366 |
366 |
|
367 |
367 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
368 |
368 |
virtual ~StreamSection() {}
|
369 |
369 |
|
370 |
370 |
virtual void process(std::istream& is, int& line_num) {
|
371 |
371 |
_functor(is, line_num);
|
372 |
372 |
char c;
|
373 |
373 |
std::string line;
|
374 |
374 |
while (is.get(c) && c != '@') {
|
375 |
375 |
if (c == '\n') {
|
376 |
376 |
++line_num;
|
377 |
377 |
} else if (!isWhiteSpace(c)) {
|
378 |
378 |
getline(is, line);
|
379 |
379 |
++line_num;
|
380 |
380 |
}
|
381 |
381 |
}
|
382 |
382 |
if (is) is.putback(c);
|
383 |
383 |
else if (is.eof()) is.clear();
|
384 |
384 |
}
|
385 |
385 |
};
|
386 |
386 |
|
387 |
387 |
}
|
388 |
388 |
|
389 |
389 |
template <typename Digraph>
|
390 |
390 |
class DigraphReader;
|
391 |
391 |
|
392 |
392 |
template <typename Digraph>
|
393 |
393 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph);
|
394 |
394 |
|
395 |
395 |
template <typename Digraph>
|
396 |
396 |
DigraphReader<Digraph> digraphReader(const std::string& fn, Digraph& digraph);
|
397 |
397 |
|
398 |
398 |
template <typename Digraph>
|
399 |
399 |
DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph);
|
400 |
400 |
|
401 |
401 |
/// \ingroup lemon_io
|
402 |
402 |
///
|
403 |
403 |
/// \brief \ref lgf-format "LGF" reader for directed graphs
|
404 |
404 |
///
|
405 |
405 |
/// This utility reads an \ref lgf-format "LGF" file.
|
406 |
406 |
///
|
407 |
407 |
/// The reading method does a batch processing. The user creates a
|
408 |
408 |
/// reader object, then various reading rules can be added to the
|
409 |
409 |
/// reader, and eventually the reading is executed with the \c run()
|
410 |
410 |
/// member function. A map reading rule can be added to the reader
|
411 |
411 |
/// with the \c nodeMap() or \c arcMap() members. An optional
|
412 |
412 |
/// converter parameter can also be added as a standard functor
|
413 |
413 |
/// converting from \c std::string to the value type of the map. If it
|
414 |
414 |
/// is set, it will determine how the tokens in the file should be
|
415 |
415 |
/// converted to the value type of the map. If the functor is not set,
|
416 |
416 |
/// then a default conversion will be used. One map can be read into
|
417 |
417 |
/// multiple map objects at the same time. The \c attribute(), \c
|
418 |
418 |
/// node() and \c arc() functions are used to add attribute reading
|
419 |
419 |
/// rules.
|
420 |
420 |
///
|
421 |
421 |
///\code
|
422 |
422 |
/// DigraphReader<Digraph>(std::cin, digraph).
|
423 |
423 |
/// nodeMap("coordinates", coord_map).
|
424 |
424 |
/// arcMap("capacity", cap_map).
|
425 |
425 |
/// node("source", src).
|
426 |
426 |
/// node("target", trg).
|
427 |
427 |
/// attribute("caption", caption).
|
428 |
428 |
/// run();
|
429 |
429 |
///\endcode
|
430 |
430 |
///
|
431 |
431 |
/// By default the reader uses the first section in the file of the
|
432 |
432 |
/// proper type. If a section has an optional name, then it can be
|
433 |
433 |
/// selected for reading by giving an optional name parameter to the
|
434 |
434 |
/// \c nodes(), \c arcs() or \c attributes() functions.
|
435 |
435 |
///
|
436 |
436 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader
|
437 |
437 |
/// that the nodes or arcs should not be constructed (added to the
|
438 |
438 |
/// graph) during the reading, but instead the label map of the items
|
439 |
439 |
/// are given as a parameter of these functions. An
|
440 |
440 |
/// application of these functions is multipass reading, which is
|
441 |
441 |
/// important if two \c \@arcs sections must be read from the
|
442 |
442 |
/// file. In this case the first phase would read the node set and one
|
443 |
443 |
/// of the arc sets, while the second phase would read the second arc
|
444 |
444 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
|
445 |
445 |
/// The previously read label node map should be passed to the \c
|
446 |
446 |
/// useNodes() functions. Another application of multipass reading when
|
447 |
447 |
/// paths are given as a node map or an arc map. It is impossible to read this in
|
448 |
448 |
/// a single pass, because the arcs are not constructed when the node
|
449 |
449 |
/// maps are read.
|
450 |
450 |
template <typename _Digraph>
|
451 |
451 |
class DigraphReader {
|
452 |
452 |
public:
|
453 |
453 |
|
454 |
454 |
typedef _Digraph Digraph;
|
455 |
455 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
456 |
456 |
|
457 |
457 |
private:
|
458 |
458 |
|
459 |
459 |
|
460 |
460 |
std::istream* _is;
|
461 |
461 |
bool local_is;
|
462 |
462 |
|
463 |
463 |
Digraph& _digraph;
|
464 |
464 |
|
465 |
465 |
std::string _nodes_caption;
|
466 |
466 |
std::string _arcs_caption;
|
467 |
467 |
std::string _attributes_caption;
|
468 |
468 |
|
469 |
469 |
typedef std::map<std::string, Node> NodeIndex;
|
470 |
470 |
NodeIndex _node_index;
|
471 |
471 |
typedef std::map<std::string, Arc> ArcIndex;
|
472 |
472 |
ArcIndex _arc_index;
|
473 |
473 |
|
474 |
474 |
typedef std::vector<std::pair<std::string,
|
475 |
475 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
|
476 |
476 |
NodeMaps _node_maps;
|
477 |
477 |
|
478 |
478 |
typedef std::vector<std::pair<std::string,
|
479 |
479 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps;
|
480 |
480 |
ArcMaps _arc_maps;
|
481 |
481 |
|
482 |
482 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
|
483 |
483 |
Attributes;
|
484 |
484 |
Attributes _attributes;
|
485 |
485 |
|
486 |
486 |
bool _use_nodes;
|
487 |
487 |
bool _use_arcs;
|
488 |
488 |
|
489 |
489 |
bool _skip_nodes;
|
490 |
490 |
bool _skip_arcs;
|
491 |
491 |
|
492 |
492 |
int line_num;
|
493 |
493 |
std::istringstream line;
|
494 |
494 |
|
495 |
495 |
public:
|
496 |
496 |
|
497 |
497 |
/// \brief Constructor
|
498 |
498 |
///
|
499 |
499 |
/// Construct a directed graph reader, which reads from the given
|
500 |
500 |
/// input stream.
|
501 |
501 |
DigraphReader(std::istream& is, Digraph& digraph)
|
502 |
502 |
: _is(&is), local_is(false), _digraph(digraph),
|
503 |
503 |
_use_nodes(false), _use_arcs(false),
|
504 |
504 |
_skip_nodes(false), _skip_arcs(false) {}
|
505 |
505 |
|
506 |
506 |
/// \brief Constructor
|
507 |
507 |
///
|
508 |
508 |
/// Construct a directed graph reader, which reads from the given
|
509 |
509 |
/// file.
|
510 |
510 |
DigraphReader(const std::string& fn, Digraph& digraph)
|
511 |
511 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
|
512 |
512 |
_use_nodes(false), _use_arcs(false),
|
513 |
513 |
_skip_nodes(false), _skip_arcs(false) {}
|
514 |
514 |
|
515 |
515 |
/// \brief Constructor
|
516 |
516 |
///
|
517 |
517 |
/// Construct a directed graph reader, which reads from the given
|
518 |
518 |
/// file.
|
519 |
519 |
DigraphReader(const char* fn, Digraph& digraph)
|
520 |
520 |
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
|
521 |
521 |
_use_nodes(false), _use_arcs(false),
|
522 |
522 |
_skip_nodes(false), _skip_arcs(false) {}
|
523 |
523 |
|
524 |
524 |
/// \brief Destructor
|
525 |
525 |
~DigraphReader() {
|
526 |
526 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
527 |
527 |
it != _node_maps.end(); ++it) {
|
528 |
528 |
delete it->second;
|
529 |
529 |
}
|
530 |
530 |
|
531 |
531 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
532 |
532 |
it != _arc_maps.end(); ++it) {
|
533 |
533 |
delete it->second;
|
534 |
534 |
}
|
535 |
535 |
|
536 |
536 |
for (typename Attributes::iterator it = _attributes.begin();
|
537 |
537 |
it != _attributes.end(); ++it) {
|
538 |
538 |
delete it->second;
|
539 |
539 |
}
|
540 |
540 |
|
541 |
541 |
if (local_is) {
|
542 |
542 |
delete _is;
|
543 |
543 |
}
|
544 |
544 |
|
545 |
545 |
}
|
546 |
546 |
|
547 |
547 |
private:
|
548 |
548 |
|
549 |
549 |
friend DigraphReader<Digraph> digraphReader<>(std::istream& is,
|
550 |
550 |
Digraph& digraph);
|
551 |
551 |
friend DigraphReader<Digraph> digraphReader<>(const std::string& fn,
|
552 |
552 |
Digraph& digraph);
|
553 |
553 |
friend DigraphReader<Digraph> digraphReader<>(const char *fn,
|
554 |
554 |
Digraph& digraph);
|
555 |
555 |
|
556 |
556 |
DigraphReader(DigraphReader& other)
|
557 |
557 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph),
|
558 |
558 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
|
559 |
559 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
560 |
560 |
|
561 |
561 |
other._is = 0;
|
562 |
562 |
other.local_is = false;
|
563 |
563 |
|
564 |
564 |
_node_index.swap(other._node_index);
|
565 |
565 |
_arc_index.swap(other._arc_index);
|
566 |
566 |
|
567 |
567 |
_node_maps.swap(other._node_maps);
|
568 |
568 |
_arc_maps.swap(other._arc_maps);
|
569 |
569 |
_attributes.swap(other._attributes);
|
570 |
570 |
|
571 |
571 |
_nodes_caption = other._nodes_caption;
|
572 |
572 |
_arcs_caption = other._arcs_caption;
|
573 |
573 |
_attributes_caption = other._attributes_caption;
|
574 |
574 |
|
575 |
575 |
}
|
576 |
576 |
|
577 |
577 |
DigraphReader& operator=(const DigraphReader&);
|
578 |
578 |
|
579 |
579 |
public:
|
580 |
580 |
|
581 |
581 |
/// \name Reading rules
|
582 |
582 |
/// @{
|
583 |
583 |
|
584 |
584 |
/// \brief Node map reading rule
|
585 |
585 |
///
|
586 |
586 |
/// Add a node map reading rule to the reader.
|
587 |
587 |
template <typename Map>
|
588 |
588 |
DigraphReader& nodeMap(const std::string& caption, Map& map) {
|
589 |
589 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
590 |
590 |
_reader_bits::MapStorageBase<Node>* storage =
|
591 |
591 |
new _reader_bits::MapStorage<Node, Map>(map);
|
592 |
592 |
_node_maps.push_back(std::make_pair(caption, storage));
|
593 |
593 |
return *this;
|
594 |
594 |
}
|
595 |
595 |
|
596 |
596 |
/// \brief Node map reading rule
|
597 |
597 |
///
|
598 |
598 |
/// Add a node map reading rule with specialized converter to the
|
599 |
599 |
/// reader.
|
600 |
600 |
template <typename Map, typename Converter>
|
601 |
601 |
DigraphReader& nodeMap(const std::string& caption, Map& map,
|
602 |
602 |
const Converter& converter = Converter()) {
|
603 |
603 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
604 |
604 |
_reader_bits::MapStorageBase<Node>* storage =
|
605 |
605 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
|
606 |
606 |
_node_maps.push_back(std::make_pair(caption, storage));
|
607 |
607 |
return *this;
|
608 |
608 |
}
|
609 |
609 |
|
610 |
610 |
/// \brief Arc map reading rule
|
611 |
611 |
///
|
612 |
612 |
/// Add an arc map reading rule to the reader.
|
613 |
613 |
template <typename Map>
|
614 |
614 |
DigraphReader& arcMap(const std::string& caption, Map& map) {
|
615 |
615 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
|
616 |
616 |
_reader_bits::MapStorageBase<Arc>* storage =
|
617 |
617 |
new _reader_bits::MapStorage<Arc, Map>(map);
|
618 |
618 |
_arc_maps.push_back(std::make_pair(caption, storage));
|
619 |
619 |
return *this;
|
620 |
620 |
}
|
621 |
621 |
|
622 |
622 |
/// \brief Arc map reading rule
|
623 |
623 |
///
|
624 |
624 |
/// Add an arc map reading rule with specialized converter to the
|
625 |
625 |
/// reader.
|
626 |
626 |
template <typename Map, typename Converter>
|
627 |
627 |
DigraphReader& arcMap(const std::string& caption, Map& map,
|
628 |
628 |
const Converter& converter = Converter()) {
|
629 |
629 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
|
630 |
630 |
_reader_bits::MapStorageBase<Arc>* storage =
|
631 |
631 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
|
632 |
632 |
_arc_maps.push_back(std::make_pair(caption, storage));
|
633 |
633 |
return *this;
|
634 |
634 |
}
|
635 |
635 |
|
636 |
636 |
/// \brief Attribute reading rule
|
637 |
637 |
///
|
638 |
638 |
/// Add an attribute reading rule to the reader.
|
639 |
639 |
template <typename Value>
|
640 |
640 |
DigraphReader& attribute(const std::string& caption, Value& value) {
|
641 |
641 |
_reader_bits::ValueStorageBase* storage =
|
642 |
642 |
new _reader_bits::ValueStorage<Value>(value);
|
643 |
643 |
_attributes.insert(std::make_pair(caption, storage));
|
644 |
644 |
return *this;
|
645 |
645 |
}
|
646 |
646 |
|
647 |
647 |
/// \brief Attribute reading rule
|
648 |
648 |
///
|
649 |
649 |
/// Add an attribute reading rule with specialized converter to the
|
650 |
650 |
/// reader.
|
651 |
651 |
template <typename Value, typename Converter>
|
652 |
652 |
DigraphReader& attribute(const std::string& caption, Value& value,
|
653 |
653 |
const Converter& converter = Converter()) {
|
654 |
654 |
_reader_bits::ValueStorageBase* storage =
|
655 |
655 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter);
|
656 |
656 |
_attributes.insert(std::make_pair(caption, storage));
|
657 |
657 |
return *this;
|
658 |
658 |
}
|
659 |
659 |
|
660 |
660 |
/// \brief Node reading rule
|
661 |
661 |
///
|
662 |
662 |
/// Add a node reading rule to reader.
|
663 |
663 |
DigraphReader& node(const std::string& caption, Node& node) {
|
664 |
664 |
typedef _reader_bits::MapLookUpConverter<Node> Converter;
|
665 |
665 |
Converter converter(_node_index);
|
666 |
666 |
_reader_bits::ValueStorageBase* storage =
|
667 |
667 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter);
|
668 |
668 |
_attributes.insert(std::make_pair(caption, storage));
|
669 |
669 |
return *this;
|
670 |
670 |
}
|
671 |
671 |
|
672 |
672 |
/// \brief Arc reading rule
|
673 |
673 |
///
|
674 |
674 |
/// Add an arc reading rule to reader.
|
675 |
675 |
DigraphReader& arc(const std::string& caption, Arc& arc) {
|
676 |
676 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter;
|
677 |
677 |
Converter converter(_arc_index);
|
678 |
678 |
_reader_bits::ValueStorageBase* storage =
|
679 |
679 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
|
680 |
680 |
_attributes.insert(std::make_pair(caption, storage));
|
681 |
681 |
return *this;
|
682 |
682 |
}
|
683 |
683 |
|
684 |
684 |
/// @}
|
685 |
685 |
|
686 |
686 |
/// \name Select section by name
|
687 |
687 |
/// @{
|
688 |
688 |
|
689 |
689 |
/// \brief Set \c \@nodes section to be read
|
690 |
690 |
///
|
691 |
691 |
/// Set \c \@nodes section to be read
|
692 |
692 |
DigraphReader& nodes(const std::string& caption) {
|
693 |
693 |
_nodes_caption = caption;
|
694 |
694 |
return *this;
|
695 |
695 |
}
|
696 |
696 |
|
697 |
697 |
/// \brief Set \c \@arcs section to be read
|
698 |
698 |
///
|
699 |
699 |
/// Set \c \@arcs section to be read
|
700 |
700 |
DigraphReader& arcs(const std::string& caption) {
|
701 |
701 |
_arcs_caption = caption;
|
702 |
702 |
return *this;
|
703 |
703 |
}
|
704 |
704 |
|
705 |
705 |
/// \brief Set \c \@attributes section to be read
|
706 |
706 |
///
|
707 |
707 |
/// Set \c \@attributes section to be read
|
708 |
708 |
DigraphReader& attributes(const std::string& caption) {
|
709 |
709 |
_attributes_caption = caption;
|
710 |
710 |
return *this;
|
711 |
711 |
}
|
712 |
712 |
|
713 |
713 |
/// @}
|
714 |
714 |
|
715 |
715 |
/// \name Using previously constructed node or arc set
|
716 |
716 |
/// @{
|
717 |
717 |
|
718 |
718 |
/// \brief Use previously constructed node set
|
719 |
719 |
///
|
720 |
720 |
/// Use previously constructed node set, and specify the node
|
721 |
721 |
/// label map.
|
722 |
722 |
template <typename Map>
|
723 |
723 |
DigraphReader& useNodes(const Map& map) {
|
724 |
724 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
725 |
725 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
|
726 |
726 |
_use_nodes = true;
|
727 |
727 |
_writer_bits::DefaultConverter<typename Map::Value> converter;
|
728 |
728 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
729 |
729 |
_node_index.insert(std::make_pair(converter(map[n]), n));
|
730 |
730 |
}
|
731 |
731 |
return *this;
|
732 |
732 |
}
|
733 |
733 |
|
734 |
734 |
/// \brief Use previously constructed node set
|
735 |
735 |
///
|
736 |
736 |
/// Use previously constructed node set, and specify the node
|
737 |
737 |
/// label map and a functor which converts the label map values to
|
738 |
738 |
/// \c std::string.
|
739 |
739 |
template <typename Map, typename Converter>
|
740 |
740 |
DigraphReader& useNodes(const Map& map,
|
741 |
741 |
const Converter& converter = Converter()) {
|
742 |
742 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
743 |
743 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
|
744 |
744 |
_use_nodes = true;
|
745 |
745 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
746 |
746 |
_node_index.insert(std::make_pair(converter(map[n]), n));
|
747 |
747 |
}
|
748 |
748 |
return *this;
|
749 |
749 |
}
|
750 |
750 |
|
751 |
751 |
/// \brief Use previously constructed arc set
|
752 |
752 |
///
|
753 |
753 |
/// Use previously constructed arc set, and specify the arc
|
754 |
754 |
/// label map.
|
755 |
755 |
template <typename Map>
|
756 |
756 |
DigraphReader& useArcs(const Map& map) {
|
757 |
757 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
758 |
758 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
|
759 |
759 |
_use_arcs = true;
|
760 |
760 |
_writer_bits::DefaultConverter<typename Map::Value> converter;
|
761 |
761 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
762 |
762 |
_arc_index.insert(std::make_pair(converter(map[a]), a));
|
763 |
763 |
}
|
764 |
764 |
return *this;
|
765 |
765 |
}
|
766 |
766 |
|
767 |
767 |
/// \brief Use previously constructed arc set
|
768 |
768 |
///
|
769 |
769 |
/// Use previously constructed arc set, and specify the arc
|
770 |
770 |
/// label map and a functor which converts the label map values to
|
771 |
771 |
/// \c std::string.
|
772 |
772 |
template <typename Map, typename Converter>
|
773 |
773 |
DigraphReader& useArcs(const Map& map,
|
774 |
774 |
const Converter& converter = Converter()) {
|
775 |
775 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
776 |
776 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
|
777 |
777 |
_use_arcs = true;
|
778 |
778 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
779 |
779 |
_arc_index.insert(std::make_pair(converter(map[a]), a));
|
780 |
780 |
}
|
781 |
781 |
return *this;
|
782 |
782 |
}
|
783 |
783 |
|
784 |
784 |
/// \brief Skips the reading of node section
|
785 |
785 |
///
|
786 |
786 |
/// Omit the reading of the node section. This implies that each node
|
787 |
787 |
/// map reading rule will be abandoned, and the nodes of the graph
|
788 |
788 |
/// will not be constructed, which usually cause that the arc set
|
789 |
789 |
/// could not be read due to lack of node name resolving.
|
790 |
790 |
/// Therefore \c skipArcs() function should also be used, or
|
791 |
791 |
/// \c useNodes() should be used to specify the label of the nodes.
|
792 |
792 |
DigraphReader& skipNodes() {
|
793 |
793 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
|
794 |
794 |
_skip_nodes = true;
|
795 |
795 |
return *this;
|
796 |
796 |
}
|
797 |
797 |
|
798 |
798 |
/// \brief Skips the reading of arc section
|
799 |
799 |
///
|
800 |
800 |
/// Omit the reading of the arc section. This implies that each arc
|
801 |
801 |
/// map reading rule will be abandoned, and the arcs of the graph
|
802 |
802 |
/// will not be constructed.
|
803 |
803 |
DigraphReader& skipArcs() {
|
804 |
804 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set");
|
805 |
805 |
_skip_arcs = true;
|
806 |
806 |
return *this;
|
807 |
807 |
}
|
808 |
808 |
|
809 |
809 |
/// @}
|
810 |
810 |
|
811 |
811 |
private:
|
812 |
812 |
|
813 |
813 |
bool readLine() {
|
814 |
814 |
std::string str;
|
815 |
815 |
while(++line_num, std::getline(*_is, str)) {
|
816 |
816 |
line.clear(); line.str(str);
|
817 |
817 |
char c;
|
818 |
818 |
if (line >> std::ws >> c && c != '#') {
|
819 |
819 |
line.putback(c);
|
820 |
820 |
return true;
|
821 |
821 |
}
|
822 |
822 |
}
|
823 |
823 |
return false;
|
824 |
824 |
}
|
825 |
825 |
|
826 |
826 |
bool readSuccess() {
|
827 |
827 |
return static_cast<bool>(*_is);
|
828 |
828 |
}
|
829 |
829 |
|
830 |
830 |
void skipSection() {
|
831 |
831 |
char c;
|
832 |
832 |
while (readSuccess() && line >> c && c != '@') {
|
833 |
833 |
readLine();
|
834 |
834 |
}
|
835 |
835 |
line.putback(c);
|
836 |
836 |
}
|
837 |
837 |
|
838 |
838 |
void readNodes() {
|
839 |
839 |
|
840 |
840 |
std::vector<int> map_index(_node_maps.size());
|
841 |
841 |
int map_num, label_index;
|
842 |
842 |
|
843 |
843 |
char c;
|
844 |
844 |
if (!readLine() || !(line >> c) || c == '@') {
|
845 |
845 |
if (readSuccess() && line) line.putback(c);
|
846 |
846 |
if (!_node_maps.empty())
|
847 |
847 |
throw DataFormatError("Cannot find map names");
|
848 |
848 |
return;
|
849 |
849 |
}
|
850 |
850 |
line.putback(c);
|
851 |
851 |
|
852 |
852 |
{
|
853 |
853 |
std::map<std::string, int> maps;
|
854 |
854 |
|
855 |
855 |
std::string map;
|
856 |
856 |
int index = 0;
|
857 |
857 |
while (_reader_bits::readToken(line, map)) {
|
858 |
858 |
if (maps.find(map) != maps.end()) {
|
859 |
859 |
std::ostringstream msg;
|
860 |
860 |
msg << "Multiple occurence of node map: " << map;
|
861 |
861 |
throw DataFormatError(msg.str().c_str());
|
862 |
862 |
}
|
863 |
863 |
maps.insert(std::make_pair(map, index));
|
864 |
864 |
++index;
|
865 |
865 |
}
|
866 |
866 |
|
867 |
867 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
868 |
868 |
std::map<std::string, int>::iterator jt =
|
869 |
869 |
maps.find(_node_maps[i].first);
|
870 |
870 |
if (jt == maps.end()) {
|
871 |
871 |
std::ostringstream msg;
|
872 |
872 |
msg << "Map not found in file: " << _node_maps[i].first;
|
873 |
873 |
throw DataFormatError(msg.str().c_str());
|
874 |
874 |
}
|
875 |
875 |
map_index[i] = jt->second;
|
876 |
876 |
}
|
877 |
877 |
|
878 |
878 |
{
|
879 |
879 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
880 |
880 |
if (jt != maps.end()) {
|
881 |
881 |
label_index = jt->second;
|
882 |
882 |
} else {
|
883 |
883 |
label_index = -1;
|
884 |
884 |
}
|
885 |
885 |
}
|
886 |
886 |
map_num = maps.size();
|
887 |
887 |
}
|
888 |
888 |
|
889 |
889 |
while (readLine() && line >> c && c != '@') {
|
890 |
890 |
line.putback(c);
|
891 |
891 |
|
892 |
892 |
std::vector<std::string> tokens(map_num);
|
893 |
893 |
for (int i = 0; i < map_num; ++i) {
|
894 |
894 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
895 |
895 |
std::ostringstream msg;
|
896 |
896 |
msg << "Column not found (" << i + 1 << ")";
|
897 |
897 |
throw DataFormatError(msg.str().c_str());
|
898 |
898 |
}
|
899 |
899 |
}
|
900 |
900 |
if (line >> std::ws >> c)
|
901 |
901 |
throw DataFormatError("Extra character on the end of line");
|
902 |
902 |
|
903 |
903 |
Node n;
|
904 |
904 |
if (!_use_nodes) {
|
905 |
905 |
n = _digraph.addNode();
|
906 |
906 |
if (label_index != -1)
|
907 |
907 |
_node_index.insert(std::make_pair(tokens[label_index], n));
|
908 |
908 |
} else {
|
909 |
909 |
if (label_index == -1)
|
910 |
910 |
throw DataFormatError("Label map not found in file");
|
911 |
911 |
typename std::map<std::string, Node>::iterator it =
|
912 |
912 |
_node_index.find(tokens[label_index]);
|
913 |
913 |
if (it == _node_index.end()) {
|
914 |
914 |
std::ostringstream msg;
|
915 |
915 |
msg << "Node with label not found: " << tokens[label_index];
|
916 |
916 |
throw DataFormatError(msg.str().c_str());
|
917 |
917 |
}
|
918 |
918 |
n = it->second;
|
919 |
919 |
}
|
920 |
920 |
|
921 |
921 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
922 |
922 |
_node_maps[i].second->set(n, tokens[map_index[i]]);
|
923 |
923 |
}
|
924 |
924 |
|
925 |
925 |
}
|
926 |
926 |
if (readSuccess()) {
|
927 |
927 |
line.putback(c);
|
928 |
928 |
}
|
929 |
929 |
}
|
930 |
930 |
|
931 |
931 |
void readArcs() {
|
932 |
932 |
|
933 |
933 |
std::vector<int> map_index(_arc_maps.size());
|
934 |
934 |
int map_num, label_index;
|
935 |
935 |
|
936 |
936 |
char c;
|
937 |
937 |
if (!readLine() || !(line >> c) || c == '@') {
|
938 |
938 |
if (readSuccess() && line) line.putback(c);
|
939 |
939 |
if (!_arc_maps.empty())
|
940 |
940 |
throw DataFormatError("Cannot find map names");
|
941 |
941 |
return;
|
942 |
942 |
}
|
943 |
943 |
line.putback(c);
|
944 |
944 |
|
945 |
945 |
{
|
946 |
946 |
std::map<std::string, int> maps;
|
947 |
947 |
|
948 |
948 |
std::string map;
|
949 |
949 |
int index = 0;
|
950 |
950 |
while (_reader_bits::readToken(line, map)) {
|
951 |
951 |
if (maps.find(map) != maps.end()) {
|
952 |
952 |
std::ostringstream msg;
|
953 |
953 |
msg << "Multiple occurence of arc map: " << map;
|
954 |
954 |
throw DataFormatError(msg.str().c_str());
|
955 |
955 |
}
|
956 |
956 |
maps.insert(std::make_pair(map, index));
|
957 |
957 |
++index;
|
958 |
958 |
}
|
959 |
959 |
|
960 |
960 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
961 |
961 |
std::map<std::string, int>::iterator jt =
|
962 |
962 |
maps.find(_arc_maps[i].first);
|
963 |
963 |
if (jt == maps.end()) {
|
964 |
964 |
std::ostringstream msg;
|
965 |
965 |
msg << "Map not found in file: " << _arc_maps[i].first;
|
966 |
966 |
throw DataFormatError(msg.str().c_str());
|
967 |
967 |
}
|
968 |
968 |
map_index[i] = jt->second;
|
969 |
969 |
}
|
970 |
970 |
|
971 |
971 |
{
|
972 |
972 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
973 |
973 |
if (jt != maps.end()) {
|
974 |
974 |
label_index = jt->second;
|
975 |
975 |
} else {
|
976 |
976 |
label_index = -1;
|
977 |
977 |
}
|
978 |
978 |
}
|
979 |
979 |
map_num = maps.size();
|
980 |
980 |
}
|
981 |
981 |
|
982 |
982 |
while (readLine() && line >> c && c != '@') {
|
983 |
983 |
line.putback(c);
|
984 |
984 |
|
985 |
985 |
std::string source_token;
|
986 |
986 |
std::string target_token;
|
987 |
987 |
|
988 |
988 |
if (!_reader_bits::readToken(line, source_token))
|
989 |
989 |
throw DataFormatError("Source not found");
|
990 |
990 |
|
991 |
991 |
if (!_reader_bits::readToken(line, target_token))
|
992 |
992 |
throw DataFormatError("Target not found");
|
993 |
993 |
|
994 |
994 |
std::vector<std::string> tokens(map_num);
|
995 |
995 |
for (int i = 0; i < map_num; ++i) {
|
996 |
996 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
997 |
997 |
std::ostringstream msg;
|
998 |
998 |
msg << "Column not found (" << i + 1 << ")";
|
999 |
999 |
throw DataFormatError(msg.str().c_str());
|
1000 |
1000 |
}
|
1001 |
1001 |
}
|
1002 |
1002 |
if (line >> std::ws >> c)
|
1003 |
1003 |
throw DataFormatError("Extra character on the end of line");
|
1004 |
1004 |
|
1005 |
1005 |
Arc a;
|
1006 |
1006 |
if (!_use_arcs) {
|
1007 |
1007 |
|
1008 |
1008 |
typename NodeIndex::iterator it;
|
1009 |
1009 |
|
1010 |
1010 |
it = _node_index.find(source_token);
|
1011 |
1011 |
if (it == _node_index.end()) {
|
1012 |
1012 |
std::ostringstream msg;
|
1013 |
1013 |
msg << "Item not found: " << source_token;
|
1014 |
1014 |
throw DataFormatError(msg.str().c_str());
|
1015 |
1015 |
}
|
1016 |
1016 |
Node source = it->second;
|
1017 |
1017 |
|
1018 |
1018 |
it = _node_index.find(target_token);
|
1019 |
1019 |
if (it == _node_index.end()) {
|
1020 |
1020 |
std::ostringstream msg;
|
1021 |
1021 |
msg << "Item not found: " << target_token;
|
1022 |
1022 |
throw DataFormatError(msg.str().c_str());
|
1023 |
1023 |
}
|
1024 |
1024 |
Node target = it->second;
|
1025 |
1025 |
|
1026 |
1026 |
a = _digraph.addArc(source, target);
|
1027 |
1027 |
if (label_index != -1)
|
1028 |
1028 |
_arc_index.insert(std::make_pair(tokens[label_index], a));
|
1029 |
1029 |
} else {
|
1030 |
1030 |
if (label_index == -1)
|
1031 |
1031 |
throw DataFormatError("Label map not found in file");
|
1032 |
1032 |
typename std::map<std::string, Arc>::iterator it =
|
1033 |
1033 |
_arc_index.find(tokens[label_index]);
|
1034 |
1034 |
if (it == _arc_index.end()) {
|
1035 |
1035 |
std::ostringstream msg;
|
1036 |
1036 |
msg << "Arc with label not found: " << tokens[label_index];
|
1037 |
1037 |
throw DataFormatError(msg.str().c_str());
|
1038 |
1038 |
}
|
1039 |
1039 |
a = it->second;
|
1040 |
1040 |
}
|
1041 |
1041 |
|
1042 |
1042 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
1043 |
1043 |
_arc_maps[i].second->set(a, tokens[map_index[i]]);
|
1044 |
1044 |
}
|
1045 |
1045 |
|
1046 |
1046 |
}
|
1047 |
1047 |
if (readSuccess()) {
|
1048 |
1048 |
line.putback(c);
|
1049 |
1049 |
}
|
1050 |
1050 |
}
|
1051 |
1051 |
|
1052 |
1052 |
void readAttributes() {
|
1053 |
1053 |
|
1054 |
1054 |
std::set<std::string> read_attr;
|
1055 |
1055 |
|
1056 |
1056 |
char c;
|
1057 |
1057 |
while (readLine() && line >> c && c != '@') {
|
1058 |
1058 |
line.putback(c);
|
1059 |
1059 |
|
1060 |
1060 |
std::string attr, token;
|
1061 |
1061 |
if (!_reader_bits::readToken(line, attr))
|
1062 |
1062 |
throw DataFormatError("Attribute name not found");
|
1063 |
1063 |
if (!_reader_bits::readToken(line, token))
|
1064 |
1064 |
throw DataFormatError("Attribute value not found");
|
1065 |
1065 |
if (line >> c)
|
1066 |
1066 |
throw DataFormatError("Extra character on the end of line");
|
1067 |
1067 |
|
1068 |
1068 |
{
|
1069 |
1069 |
std::set<std::string>::iterator it = read_attr.find(attr);
|
1070 |
1070 |
if (it != read_attr.end()) {
|
1071 |
1071 |
std::ostringstream msg;
|
1072 |
1072 |
msg << "Multiple occurence of attribute " << attr;
|
1073 |
1073 |
throw DataFormatError(msg.str().c_str());
|
1074 |
1074 |
}
|
1075 |
1075 |
read_attr.insert(attr);
|
1076 |
1076 |
}
|
1077 |
1077 |
|
1078 |
1078 |
{
|
1079 |
1079 |
typename Attributes::iterator it = _attributes.lower_bound(attr);
|
1080 |
1080 |
while (it != _attributes.end() && it->first == attr) {
|
1081 |
1081 |
it->second->set(token);
|
1082 |
1082 |
++it;
|
1083 |
1083 |
}
|
1084 |
1084 |
}
|
1085 |
1085 |
|
1086 |
1086 |
}
|
1087 |
1087 |
if (readSuccess()) {
|
1088 |
1088 |
line.putback(c);
|
1089 |
1089 |
}
|
1090 |
1090 |
for (typename Attributes::iterator it = _attributes.begin();
|
1091 |
1091 |
it != _attributes.end(); ++it) {
|
1092 |
1092 |
if (read_attr.find(it->first) == read_attr.end()) {
|
1093 |
1093 |
std::ostringstream msg;
|
1094 |
1094 |
msg << "Attribute not found in file: " << it->first;
|
1095 |
1095 |
throw DataFormatError(msg.str().c_str());
|
1096 |
1096 |
}
|
1097 |
1097 |
}
|
1098 |
1098 |
}
|
1099 |
1099 |
|
1100 |
1100 |
public:
|
1101 |
1101 |
|
1102 |
1102 |
/// \name Execution of the reader
|
1103 |
1103 |
/// @{
|
1104 |
1104 |
|
1105 |
1105 |
/// \brief Start the batch processing
|
1106 |
1106 |
///
|
1107 |
1107 |
/// This function starts the batch processing
|
1108 |
1108 |
void run() {
|
1109 |
1109 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
1110 |
1110 |
if (!*_is) {
|
1111 |
1111 |
throw DataFormatError("Cannot find file");
|
1112 |
1112 |
}
|
1113 |
1113 |
|
1114 |
1114 |
bool nodes_done = _skip_nodes;
|
1115 |
1115 |
bool arcs_done = _skip_arcs;
|
1116 |
1116 |
bool attributes_done = false;
|
1117 |
1117 |
|
1118 |
1118 |
line_num = 0;
|
1119 |
1119 |
readLine();
|
1120 |
1120 |
skipSection();
|
1121 |
1121 |
|
1122 |
1122 |
while (readSuccess()) {
|
1123 |
1123 |
try {
|
1124 |
1124 |
char c;
|
1125 |
1125 |
std::string section, caption;
|
1126 |
1126 |
line >> c;
|
1127 |
1127 |
_reader_bits::readToken(line, section);
|
1128 |
1128 |
_reader_bits::readToken(line, caption);
|
1129 |
1129 |
|
1130 |
1130 |
if (line >> c)
|
1131 |
1131 |
throw DataFormatError("Extra character on the end of line");
|
1132 |
1132 |
|
1133 |
1133 |
if (section == "nodes" && !nodes_done) {
|
1134 |
1134 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
1135 |
1135 |
readNodes();
|
1136 |
1136 |
nodes_done = true;
|
1137 |
1137 |
}
|
1138 |
1138 |
} else if ((section == "arcs" || section == "edges") &&
|
1139 |
1139 |
!arcs_done) {
|
1140 |
1140 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
1141 |
1141 |
readArcs();
|
1142 |
1142 |
arcs_done = true;
|
1143 |
1143 |
}
|
1144 |
1144 |
} else if (section == "attributes" && !attributes_done) {
|
1145 |
1145 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
1146 |
1146 |
readAttributes();
|
1147 |
1147 |
attributes_done = true;
|
1148 |
1148 |
}
|
1149 |
1149 |
} else {
|
1150 |
1150 |
readLine();
|
1151 |
1151 |
skipSection();
|
1152 |
1152 |
}
|
1153 |
1153 |
} catch (DataFormatError& error) {
|
1154 |
1154 |
error.line(line_num);
|
1155 |
1155 |
throw;
|
1156 |
1156 |
}
|
1157 |
1157 |
}
|
1158 |
1158 |
|
1159 |
1159 |
if (!nodes_done) {
|
1160 |
1160 |
throw DataFormatError("Section @nodes not found");
|
1161 |
1161 |
}
|
1162 |
1162 |
|
1163 |
1163 |
if (!arcs_done) {
|
1164 |
1164 |
throw DataFormatError("Section @arcs not found");
|
1165 |
1165 |
}
|
1166 |
1166 |
|
1167 |
1167 |
if (!attributes_done && !_attributes.empty()) {
|
1168 |
1168 |
throw DataFormatError("Section @attributes not found");
|
1169 |
1169 |
}
|
1170 |
1170 |
|
1171 |
1171 |
}
|
1172 |
1172 |
|
1173 |
1173 |
/// @}
|
1174 |
1174 |
|
1175 |
1175 |
};
|
1176 |
1176 |
|
1177 |
1177 |
/// \brief Return a \ref DigraphReader class
|
1178 |
1178 |
///
|
1179 |
1179 |
/// This function just returns a \ref DigraphReader class.
|
1180 |
1180 |
/// \relates DigraphReader
|
1181 |
1181 |
template <typename Digraph>
|
1182 |
1182 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
|
1183 |
1183 |
DigraphReader<Digraph> tmp(is, digraph);
|
1184 |
1184 |
return tmp;
|
1185 |
1185 |
}
|
1186 |
1186 |
|
1187 |
1187 |
/// \brief Return a \ref DigraphReader class
|
1188 |
1188 |
///
|
1189 |
1189 |
/// This function just returns a \ref DigraphReader class.
|
1190 |
1190 |
/// \relates DigraphReader
|
1191 |
1191 |
template <typename Digraph>
|
1192 |
1192 |
DigraphReader<Digraph> digraphReader(const std::string& fn,
|
1193 |
1193 |
Digraph& digraph) {
|
1194 |
1194 |
DigraphReader<Digraph> tmp(fn, digraph);
|
1195 |
1195 |
return tmp;
|
1196 |
1196 |
}
|
1197 |
1197 |
|
1198 |
1198 |
/// \brief Return a \ref DigraphReader class
|
1199 |
1199 |
///
|
1200 |
1200 |
/// This function just returns a \ref DigraphReader class.
|
1201 |
1201 |
/// \relates DigraphReader
|
1202 |
1202 |
template <typename Digraph>
|
1203 |
1203 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
|
1204 |
1204 |
DigraphReader<Digraph> tmp(fn, digraph);
|
1205 |
1205 |
return tmp;
|
1206 |
1206 |
}
|
1207 |
1207 |
|
1208 |
1208 |
template <typename Graph>
|
1209 |
1209 |
class GraphReader;
|
1210 |
1210 |
|
1211 |
1211 |
template <typename Graph>
|
1212 |
1212 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph);
|
1213 |
1213 |
|
1214 |
1214 |
template <typename Graph>
|
1215 |
1215 |
GraphReader<Graph> graphReader(const std::string& fn, Graph& graph);
|
1216 |
1216 |
|
1217 |
1217 |
template <typename Graph>
|
1218 |
1218 |
GraphReader<Graph> graphReader(const char *fn, Graph& graph);
|
1219 |
1219 |
|
1220 |
1220 |
/// \ingroup lemon_io
|
1221 |
1221 |
///
|
1222 |
1222 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs
|
1223 |
1223 |
///
|
1224 |
1224 |
/// This utility reads an \ref lgf-format "LGF" file.
|
1225 |
1225 |
///
|
1226 |
1226 |
/// It can be used almost the same way as \c DigraphReader.
|
1227 |
1227 |
/// The only difference is that this class can handle edges and
|
1228 |
1228 |
/// edge maps as well as arcs and arc maps.
|
|
1229 |
///
|
|
1230 |
/// The columns in the \c \@edges (or \c \@arcs) section are the
|
|
1231 |
/// edge maps. However, if there are two maps with the same name
|
|
1232 |
/// prefixed with \c '+' and \c '-', then these can be read into an
|
|
1233 |
/// arc map. Similarly, an attribute can be read into an arc, if
|
|
1234 |
/// it's value is an edge label prefixed with \c '+' or \c '-'.
|
1229 |
1235 |
template <typename _Graph>
|
1230 |
1236 |
class GraphReader {
|
1231 |
1237 |
public:
|
1232 |
1238 |
|
1233 |
1239 |
typedef _Graph Graph;
|
1234 |
1240 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
1235 |
1241 |
|
1236 |
1242 |
private:
|
1237 |
1243 |
|
1238 |
1244 |
std::istream* _is;
|
1239 |
1245 |
bool local_is;
|
1240 |
1246 |
|
1241 |
1247 |
Graph& _graph;
|
1242 |
1248 |
|
1243 |
1249 |
std::string _nodes_caption;
|
1244 |
1250 |
std::string _edges_caption;
|
1245 |
1251 |
std::string _attributes_caption;
|
1246 |
1252 |
|
1247 |
1253 |
typedef std::map<std::string, Node> NodeIndex;
|
1248 |
1254 |
NodeIndex _node_index;
|
1249 |
1255 |
typedef std::map<std::string, Edge> EdgeIndex;
|
1250 |
1256 |
EdgeIndex _edge_index;
|
1251 |
1257 |
|
1252 |
1258 |
typedef std::vector<std::pair<std::string,
|
1253 |
1259 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
|
1254 |
1260 |
NodeMaps _node_maps;
|
1255 |
1261 |
|
1256 |
1262 |
typedef std::vector<std::pair<std::string,
|
1257 |
1263 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
|
1258 |
1264 |
EdgeMaps _edge_maps;
|
1259 |
1265 |
|
1260 |
1266 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
|
1261 |
1267 |
Attributes;
|
1262 |
1268 |
Attributes _attributes;
|
1263 |
1269 |
|
1264 |
1270 |
bool _use_nodes;
|
1265 |
1271 |
bool _use_edges;
|
1266 |
1272 |
|
1267 |
1273 |
bool _skip_nodes;
|
1268 |
1274 |
bool _skip_edges;
|
1269 |
1275 |
|
1270 |
1276 |
int line_num;
|
1271 |
1277 |
std::istringstream line;
|
1272 |
1278 |
|
1273 |
1279 |
public:
|
1274 |
1280 |
|
1275 |
1281 |
/// \brief Constructor
|
1276 |
1282 |
///
|
1277 |
1283 |
/// Construct an undirected graph reader, which reads from the given
|
1278 |
1284 |
/// input stream.
|
1279 |
1285 |
GraphReader(std::istream& is, Graph& graph)
|
1280 |
1286 |
: _is(&is), local_is(false), _graph(graph),
|
1281 |
1287 |
_use_nodes(false), _use_edges(false),
|
1282 |
1288 |
_skip_nodes(false), _skip_edges(false) {}
|
1283 |
1289 |
|
1284 |
1290 |
/// \brief Constructor
|
1285 |
1291 |
///
|
1286 |
1292 |
/// Construct an undirected graph reader, which reads from the given
|
1287 |
1293 |
/// file.
|
1288 |
1294 |
GraphReader(const std::string& fn, Graph& graph)
|
1289 |
1295 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
|
1290 |
1296 |
_use_nodes(false), _use_edges(false),
|
1291 |
1297 |
_skip_nodes(false), _skip_edges(false) {}
|
1292 |
1298 |
|
1293 |
1299 |
/// \brief Constructor
|
1294 |
1300 |
///
|
1295 |
1301 |
/// Construct an undirected graph reader, which reads from the given
|
1296 |
1302 |
/// file.
|
1297 |
1303 |
GraphReader(const char* fn, Graph& graph)
|
1298 |
1304 |
: _is(new std::ifstream(fn)), local_is(true), _graph(graph),
|
1299 |
1305 |
_use_nodes(false), _use_edges(false),
|
1300 |
1306 |
_skip_nodes(false), _skip_edges(false) {}
|
1301 |
1307 |
|
1302 |
1308 |
/// \brief Destructor
|
1303 |
1309 |
~GraphReader() {
|
1304 |
1310 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
1305 |
1311 |
it != _node_maps.end(); ++it) {
|
1306 |
1312 |
delete it->second;
|
1307 |
1313 |
}
|
1308 |
1314 |
|
1309 |
1315 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
1310 |
1316 |
it != _edge_maps.end(); ++it) {
|
1311 |
1317 |
delete it->second;
|
1312 |
1318 |
}
|
1313 |
1319 |
|
1314 |
1320 |
for (typename Attributes::iterator it = _attributes.begin();
|
1315 |
1321 |
it != _attributes.end(); ++it) {
|
1316 |
1322 |
delete it->second;
|
1317 |
1323 |
}
|
1318 |
1324 |
|
1319 |
1325 |
if (local_is) {
|
1320 |
1326 |
delete _is;
|
1321 |
1327 |
}
|
1322 |
1328 |
|
1323 |
1329 |
}
|
1324 |
1330 |
|
1325 |
1331 |
private:
|
1326 |
1332 |
friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph);
|
1327 |
1333 |
friend GraphReader<Graph> graphReader<>(const std::string& fn,
|
1328 |
1334 |
Graph& graph);
|
1329 |
1335 |
friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph);
|
1330 |
1336 |
|
1331 |
1337 |
GraphReader(GraphReader& other)
|
1332 |
1338 |
: _is(other._is), local_is(other.local_is), _graph(other._graph),
|
1333 |
1339 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges),
|
1334 |
1340 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
1335 |
1341 |
|
1336 |
1342 |
other._is = 0;
|
1337 |
1343 |
other.local_is = false;
|
1338 |
1344 |
|
1339 |
1345 |
_node_index.swap(other._node_index);
|
1340 |
1346 |
_edge_index.swap(other._edge_index);
|
1341 |
1347 |
|
1342 |
1348 |
_node_maps.swap(other._node_maps);
|
1343 |
1349 |
_edge_maps.swap(other._edge_maps);
|
1344 |
1350 |
_attributes.swap(other._attributes);
|
1345 |
1351 |
|
1346 |
1352 |
_nodes_caption = other._nodes_caption;
|
1347 |
1353 |
_edges_caption = other._edges_caption;
|
1348 |
1354 |
_attributes_caption = other._attributes_caption;
|
1349 |
1355 |
|
1350 |
1356 |
}
|
1351 |
1357 |
|
1352 |
1358 |
GraphReader& operator=(const GraphReader&);
|
1353 |
1359 |
|
1354 |
1360 |
public:
|
1355 |
1361 |
|
1356 |
1362 |
/// \name Reading rules
|
1357 |
1363 |
/// @{
|
1358 |
1364 |
|
1359 |
1365 |
/// \brief Node map reading rule
|
1360 |
1366 |
///
|
1361 |
1367 |
/// Add a node map reading rule to the reader.
|
1362 |
1368 |
template <typename Map>
|
1363 |
1369 |
GraphReader& nodeMap(const std::string& caption, Map& map) {
|
1364 |
1370 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
1365 |
1371 |
_reader_bits::MapStorageBase<Node>* storage =
|
1366 |
1372 |
new _reader_bits::MapStorage<Node, Map>(map);
|
1367 |
1373 |
_node_maps.push_back(std::make_pair(caption, storage));
|
1368 |
1374 |
return *this;
|
1369 |
1375 |
}
|
1370 |
1376 |
|
1371 |
1377 |
/// \brief Node map reading rule
|
1372 |
1378 |
///
|
1373 |
1379 |
/// Add a node map reading rule with specialized converter to the
|
1374 |
1380 |
/// reader.
|
1375 |
1381 |
template <typename Map, typename Converter>
|
1376 |
1382 |
GraphReader& nodeMap(const std::string& caption, Map& map,
|
1377 |
1383 |
const Converter& converter = Converter()) {
|
1378 |
1384 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
1379 |
1385 |
_reader_bits::MapStorageBase<Node>* storage =
|
1380 |
1386 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
|
1381 |
1387 |
_node_maps.push_back(std::make_pair(caption, storage));
|
1382 |
1388 |
return *this;
|
1383 |
1389 |
}
|
1384 |
1390 |
|
1385 |
1391 |
/// \brief Edge map reading rule
|
1386 |
1392 |
///
|
1387 |
1393 |
/// Add an edge map reading rule to the reader.
|
1388 |
1394 |
template <typename Map>
|
1389 |
1395 |
GraphReader& edgeMap(const std::string& caption, Map& map) {
|
1390 |
1396 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
|
1391 |
1397 |
_reader_bits::MapStorageBase<Edge>* storage =
|
1392 |
1398 |
new _reader_bits::MapStorage<Edge, Map>(map);
|
1393 |
1399 |
_edge_maps.push_back(std::make_pair(caption, storage));
|
1394 |
1400 |
return *this;
|
1395 |
1401 |
}
|
1396 |
1402 |
|
1397 |
1403 |
/// \brief Edge map reading rule
|
1398 |
1404 |
///
|
1399 |
1405 |
/// Add an edge map reading rule with specialized converter to the
|
1400 |
1406 |
/// reader.
|
1401 |
1407 |
template <typename Map, typename Converter>
|
1402 |
1408 |
GraphReader& edgeMap(const std::string& caption, Map& map,
|
1403 |
1409 |
const Converter& converter = Converter()) {
|
1404 |
1410 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
|
1405 |
1411 |
_reader_bits::MapStorageBase<Edge>* storage =
|
1406 |
1412 |
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
|
1407 |
1413 |
_edge_maps.push_back(std::make_pair(caption, storage));
|
1408 |
1414 |
return *this;
|
1409 |
1415 |
}
|
1410 |
1416 |
|
1411 |
1417 |
/// \brief Arc map reading rule
|
1412 |
1418 |
///
|
1413 |
1419 |
/// Add an arc map reading rule to the reader.
|
1414 |
1420 |
template <typename Map>
|
1415 |
1421 |
GraphReader& arcMap(const std::string& caption, Map& map) {
|
1416 |
1422 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
|
1417 |
1423 |
_reader_bits::MapStorageBase<Edge>* forward_storage =
|
1418 |
1424 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
|
1419 |
1425 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
1420 |
1426 |
_reader_bits::MapStorageBase<Edge>* backward_storage =
|
1421 |
1427 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
|
1422 |
1428 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
1423 |
1429 |
return *this;
|
1424 |
1430 |
}
|
1425 |
1431 |
|
1426 |
1432 |
/// \brief Arc map reading rule
|
1427 |
1433 |
///
|
1428 |
1434 |
/// Add an arc map reading rule with specialized converter to the
|
1429 |
1435 |
/// reader.
|
1430 |
1436 |
template <typename Map, typename Converter>
|
1431 |
1437 |
GraphReader& arcMap(const std::string& caption, Map& map,
|
1432 |
1438 |
const Converter& converter = Converter()) {
|
1433 |
1439 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
|
1434 |
1440 |
_reader_bits::MapStorageBase<Edge>* forward_storage =
|
1435 |
1441 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
|
1436 |
1442 |
(_graph, map, converter);
|
1437 |
1443 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
1438 |
1444 |
_reader_bits::MapStorageBase<Edge>* backward_storage =
|
1439 |
1445 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
|
1440 |
1446 |
(_graph, map, converter);
|
1441 |
1447 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
1442 |
1448 |
return *this;
|
1443 |
1449 |
}
|
1444 |
1450 |
|
1445 |
1451 |
/// \brief Attribute reading rule
|
1446 |
1452 |
///
|
1447 |
1453 |
/// Add an attribute reading rule to the reader.
|
1448 |
1454 |
template <typename Value>
|
1449 |
1455 |
GraphReader& attribute(const std::string& caption, Value& value) {
|
1450 |
1456 |
_reader_bits::ValueStorageBase* storage =
|
1451 |
1457 |
new _reader_bits::ValueStorage<Value>(value);
|
1452 |
1458 |
_attributes.insert(std::make_pair(caption, storage));
|
1453 |
1459 |
return *this;
|
1454 |
1460 |
}
|
1455 |
1461 |
|
1456 |
1462 |
/// \brief Attribute reading rule
|
1457 |
1463 |
///
|
1458 |
1464 |
/// Add an attribute reading rule with specialized converter to the
|
1459 |
1465 |
/// reader.
|
1460 |
1466 |
template <typename Value, typename Converter>
|
1461 |
1467 |
GraphReader& attribute(const std::string& caption, Value& value,
|
1462 |
1468 |
const Converter& converter = Converter()) {
|
1463 |
1469 |
_reader_bits::ValueStorageBase* storage =
|
1464 |
1470 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter);
|
1465 |
1471 |
_attributes.insert(std::make_pair(caption, storage));
|
1466 |
1472 |
return *this;
|
1467 |
1473 |
}
|
1468 |
1474 |
|
1469 |
1475 |
/// \brief Node reading rule
|
1470 |
1476 |
///
|
1471 |
1477 |
/// Add a node reading rule to reader.
|
1472 |
1478 |
GraphReader& node(const std::string& caption, Node& node) {
|
1473 |
1479 |
typedef _reader_bits::MapLookUpConverter<Node> Converter;
|
1474 |
1480 |
Converter converter(_node_index);
|
1475 |
1481 |
_reader_bits::ValueStorageBase* storage =
|
1476 |
1482 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter);
|
1477 |
1483 |
_attributes.insert(std::make_pair(caption, storage));
|
1478 |
1484 |
return *this;
|
1479 |
1485 |
}
|
1480 |
1486 |
|
1481 |
1487 |
/// \brief Edge reading rule
|
1482 |
1488 |
///
|
1483 |
1489 |
/// Add an edge reading rule to reader.
|
1484 |
1490 |
GraphReader& edge(const std::string& caption, Edge& edge) {
|
1485 |
1491 |
typedef _reader_bits::MapLookUpConverter<Edge> Converter;
|
1486 |
1492 |
Converter converter(_edge_index);
|
1487 |
1493 |
_reader_bits::ValueStorageBase* storage =
|
1488 |
1494 |
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
|
1489 |
1495 |
_attributes.insert(std::make_pair(caption, storage));
|
1490 |
1496 |
return *this;
|
1491 |
1497 |
}
|
1492 |
1498 |
|
1493 |
1499 |
/// \brief Arc reading rule
|
1494 |
1500 |
///
|
1495 |
1501 |
/// Add an arc reading rule to reader.
|
1496 |
1502 |
GraphReader& arc(const std::string& caption, Arc& arc) {
|
1497 |
1503 |
typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
|
1498 |
1504 |
Converter converter(_graph, _edge_index);
|
1499 |
1505 |
_reader_bits::ValueStorageBase* storage =
|
1500 |
1506 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
|
1501 |
1507 |
_attributes.insert(std::make_pair(caption, storage));
|
1502 |
1508 |
return *this;
|
1503 |
1509 |
}
|
1504 |
1510 |
|
1505 |
1511 |
/// @}
|
1506 |
1512 |
|
1507 |
1513 |
/// \name Select section by name
|
1508 |
1514 |
/// @{
|
1509 |
1515 |
|
1510 |
1516 |
/// \brief Set \c \@nodes section to be read
|
1511 |
1517 |
///
|
1512 |
1518 |
/// Set \c \@nodes section to be read.
|
1513 |
1519 |
GraphReader& nodes(const std::string& caption) {
|
1514 |
1520 |
_nodes_caption = caption;
|
1515 |
1521 |
return *this;
|
1516 |
1522 |
}
|
1517 |
1523 |
|
1518 |
1524 |
/// \brief Set \c \@edges section to be read
|
1519 |
1525 |
///
|
1520 |
1526 |
/// Set \c \@edges section to be read.
|
1521 |
1527 |
GraphReader& edges(const std::string& caption) {
|
1522 |
1528 |
_edges_caption = caption;
|
1523 |
1529 |
return *this;
|
1524 |
1530 |
}
|
1525 |
1531 |
|
1526 |
1532 |
/// \brief Set \c \@attributes section to be read
|
1527 |
1533 |
///
|
1528 |
1534 |
/// Set \c \@attributes section to be read.
|
1529 |
1535 |
GraphReader& attributes(const std::string& caption) {
|
1530 |
1536 |
_attributes_caption = caption;
|
1531 |
1537 |
return *this;
|
1532 |
1538 |
}
|
1533 |
1539 |
|
1534 |
1540 |
/// @}
|
1535 |
1541 |
|
1536 |
1542 |
/// \name Using previously constructed node or edge set
|
1537 |
1543 |
/// @{
|
1538 |
1544 |
|
1539 |
1545 |
/// \brief Use previously constructed node set
|
1540 |
1546 |
///
|
1541 |
1547 |
/// Use previously constructed node set, and specify the node
|
1542 |
1548 |
/// label map.
|
1543 |
1549 |
template <typename Map>
|
1544 |
1550 |
GraphReader& useNodes(const Map& map) {
|
1545 |
1551 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
1546 |
1552 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
|
1547 |
1553 |
_use_nodes = true;
|
1548 |
1554 |
_writer_bits::DefaultConverter<typename Map::Value> converter;
|
1549 |
1555 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1550 |
1556 |
_node_index.insert(std::make_pair(converter(map[n]), n));
|
1551 |
1557 |
}
|
1552 |
1558 |
return *this;
|
1553 |
1559 |
}
|
1554 |
1560 |
|
1555 |
1561 |
/// \brief Use previously constructed node set
|
1556 |
1562 |
///
|
1557 |
1563 |
/// Use previously constructed node set, and specify the node
|
1558 |
1564 |
/// label map and a functor which converts the label map values to
|
1559 |
1565 |
/// \c std::string.
|
1560 |
1566 |
template <typename Map, typename Converter>
|
1561 |
1567 |
GraphReader& useNodes(const Map& map,
|
1562 |
1568 |
const Converter& converter = Converter()) {
|
1563 |
1569 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
1564 |
1570 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
|
1565 |
1571 |
_use_nodes = true;
|
1566 |
1572 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1567 |
1573 |
_node_index.insert(std::make_pair(converter(map[n]), n));
|
1568 |
1574 |
}
|
1569 |
1575 |
return *this;
|
1570 |
1576 |
}
|
1571 |
1577 |
|
1572 |
1578 |
/// \brief Use previously constructed edge set
|
1573 |
1579 |
///
|
1574 |
1580 |
/// Use previously constructed edge set, and specify the edge
|
1575 |
1581 |
/// label map.
|
1576 |
1582 |
template <typename Map>
|
1577 |
1583 |
GraphReader& useEdges(const Map& map) {
|
1578 |
1584 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
|
1579 |
1585 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
|
1580 |
1586 |
_use_edges = true;
|
1581 |
1587 |
_writer_bits::DefaultConverter<typename Map::Value> converter;
|
1582 |
1588 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
1583 |
1589 |
_edge_index.insert(std::make_pair(converter(map[a]), a));
|
1584 |
1590 |
}
|
1585 |
1591 |
return *this;
|
1586 |
1592 |
}
|
1587 |
1593 |
|
1588 |
1594 |
/// \brief Use previously constructed edge set
|
1589 |
1595 |
///
|
1590 |
1596 |
/// Use previously constructed edge set, and specify the edge
|
1591 |
1597 |
/// label map and a functor which converts the label map values to
|
1592 |
1598 |
/// \c std::string.
|
1593 |
1599 |
template <typename Map, typename Converter>
|
1594 |
1600 |
GraphReader& useEdges(const Map& map,
|
1595 |
1601 |
const Converter& converter = Converter()) {
|
1596 |
1602 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
|
1597 |
1603 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
|
1598 |
1604 |
_use_edges = true;
|
1599 |
1605 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
1600 |
1606 |
_edge_index.insert(std::make_pair(converter(map[a]), a));
|
1601 |
1607 |
}
|
1602 |
1608 |
return *this;
|
1603 |
1609 |
}
|
1604 |
1610 |
|
1605 |
1611 |
/// \brief Skip the reading of node section
|
1606 |
1612 |
///
|
1607 |
1613 |
/// Omit the reading of the node section. This implies that each node
|
1608 |
1614 |
/// map reading rule will be abandoned, and the nodes of the graph
|
1609 |
1615 |
/// will not be constructed, which usually cause that the edge set
|
1610 |
1616 |
/// could not be read due to lack of node name
|
1611 |
1617 |
/// could not be read due to lack of node name resolving.
|
1612 |
1618 |
/// Therefore \c skipEdges() function should also be used, or
|
1613 |
1619 |
/// \c useNodes() should be used to specify the label of the nodes.
|
1614 |
1620 |
GraphReader& skipNodes() {
|
1615 |
1621 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
|
1616 |
1622 |
_skip_nodes = true;
|
1617 |
1623 |
return *this;
|
1618 |
1624 |
}
|
1619 |
1625 |
|
1620 |
1626 |
/// \brief Skip the reading of edge section
|
1621 |
1627 |
///
|
1622 |
1628 |
/// Omit the reading of the edge section. This implies that each edge
|
1623 |
1629 |
/// map reading rule will be abandoned, and the edges of the graph
|
1624 |
1630 |
/// will not be constructed.
|
1625 |
1631 |
GraphReader& skipEdges() {
|
1626 |
1632 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set");
|
1627 |
1633 |
_skip_edges = true;
|
1628 |
1634 |
return *this;
|
1629 |
1635 |
}
|
1630 |
1636 |
|
1631 |
1637 |
/// @}
|
1632 |
1638 |
|
1633 |
1639 |
private:
|
1634 |
1640 |
|
1635 |
1641 |
bool readLine() {
|
1636 |
1642 |
std::string str;
|
1637 |
1643 |
while(++line_num, std::getline(*_is, str)) {
|
1638 |
1644 |
line.clear(); line.str(str);
|
1639 |
1645 |
char c;
|
1640 |
1646 |
if (line >> std::ws >> c && c != '#') {
|
1641 |
1647 |
line.putback(c);
|
1642 |
1648 |
return true;
|
1643 |
1649 |
}
|
1644 |
1650 |
}
|
1645 |
1651 |
return false;
|
1646 |
1652 |
}
|
1647 |
1653 |
|
1648 |
1654 |
bool readSuccess() {
|
1649 |
1655 |
return static_cast<bool>(*_is);
|
1650 |
1656 |
}
|
1651 |
1657 |
|
1652 |
1658 |
void skipSection() {
|
1653 |
1659 |
char c;
|
1654 |
1660 |
while (readSuccess() && line >> c && c != '@') {
|
1655 |
1661 |
readLine();
|
1656 |
1662 |
}
|
1657 |
1663 |
line.putback(c);
|
1658 |
1664 |
}
|
1659 |
1665 |
|
1660 |
1666 |
void readNodes() {
|
1661 |
1667 |
|
1662 |
1668 |
std::vector<int> map_index(_node_maps.size());
|
1663 |
1669 |
int map_num, label_index;
|
1664 |
1670 |
|
1665 |
1671 |
char c;
|
1666 |
1672 |
if (!readLine() || !(line >> c) || c == '@') {
|
1667 |
1673 |
if (readSuccess() && line) line.putback(c);
|
1668 |
1674 |
if (!_node_maps.empty())
|
1669 |
1675 |
throw DataFormatError("Cannot find map names");
|
1670 |
1676 |
return;
|
1671 |
1677 |
}
|
1672 |
1678 |
line.putback(c);
|
1673 |
1679 |
|
1674 |
1680 |
{
|
1675 |
1681 |
std::map<std::string, int> maps;
|
1676 |
1682 |
|
1677 |
1683 |
std::string map;
|
1678 |
1684 |
int index = 0;
|
1679 |
1685 |
while (_reader_bits::readToken(line, map)) {
|
1680 |
1686 |
if (maps.find(map) != maps.end()) {
|
1681 |
1687 |
std::ostringstream msg;
|
1682 |
1688 |
msg << "Multiple occurence of node map: " << map;
|
1683 |
1689 |
throw DataFormatError(msg.str().c_str());
|
1684 |
1690 |
}
|
1685 |
1691 |
maps.insert(std::make_pair(map, index));
|
1686 |
1692 |
++index;
|
1687 |
1693 |
}
|
1688 |
1694 |
|
1689 |
1695 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
1690 |
1696 |
std::map<std::string, int>::iterator jt =
|
1691 |
1697 |
maps.find(_node_maps[i].first);
|
1692 |
1698 |
if (jt == maps.end()) {
|
1693 |
1699 |
std::ostringstream msg;
|
1694 |
1700 |
msg << "Map not found in file: " << _node_maps[i].first;
|
1695 |
1701 |
throw DataFormatError(msg.str().c_str());
|
1696 |
1702 |
}
|
1697 |
1703 |
map_index[i] = jt->second;
|
1698 |
1704 |
}
|
1699 |
1705 |
|
1700 |
1706 |
{
|
1701 |
1707 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
1702 |
1708 |
if (jt != maps.end()) {
|
1703 |
1709 |
label_index = jt->second;
|
1704 |
1710 |
} else {
|
1705 |
1711 |
label_index = -1;
|
1706 |
1712 |
}
|
1707 |
1713 |
}
|
1708 |
1714 |
map_num = maps.size();
|
1709 |
1715 |
}
|
1710 |
1716 |
|
1711 |
1717 |
while (readLine() && line >> c && c != '@') {
|
1712 |
1718 |
line.putback(c);
|
1713 |
1719 |
|
1714 |
1720 |
std::vector<std::string> tokens(map_num);
|
1715 |
1721 |
for (int i = 0; i < map_num; ++i) {
|
1716 |
1722 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
1717 |
1723 |
std::ostringstream msg;
|
1718 |
1724 |
msg << "Column not found (" << i + 1 << ")";
|
1719 |
1725 |
throw DataFormatError(msg.str().c_str());
|
1720 |
1726 |
}
|
1721 |
1727 |
}
|
1722 |
1728 |
if (line >> std::ws >> c)
|
1723 |
1729 |
throw DataFormatError("Extra character on the end of line");
|
1724 |
1730 |
|
1725 |
1731 |
Node n;
|
1726 |
1732 |
if (!_use_nodes) {
|
1727 |
1733 |
n = _graph.addNode();
|
1728 |
1734 |
if (label_index != -1)
|
1729 |
1735 |
_node_index.insert(std::make_pair(tokens[label_index], n));
|
1730 |
1736 |
} else {
|
1731 |
1737 |
if (label_index == -1)
|
1732 |
1738 |
throw DataFormatError("Label map not found in file");
|
1733 |
1739 |
typename std::map<std::string, Node>::iterator it =
|
1734 |
1740 |
_node_index.find(tokens[label_index]);
|
1735 |
1741 |
if (it == _node_index.end()) {
|
1736 |
1742 |
std::ostringstream msg;
|
1737 |
1743 |
msg << "Node with label not found: " << tokens[label_index];
|
1738 |
1744 |
throw DataFormatError(msg.str().c_str());
|
1739 |
1745 |
}
|
1740 |
1746 |
n = it->second;
|
1741 |
1747 |
}
|
1742 |
1748 |
|
1743 |
1749 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
1744 |
1750 |
_node_maps[i].second->set(n, tokens[map_index[i]]);
|
1745 |
1751 |
}
|
1746 |
1752 |
|
1747 |
1753 |
}
|
1748 |
1754 |
if (readSuccess()) {
|
1749 |
1755 |
line.putback(c);
|
1750 |
1756 |
}
|
1751 |
1757 |
}
|
1752 |
1758 |
|
1753 |
1759 |
void readEdges() {
|
1754 |
1760 |
|
1755 |
1761 |
std::vector<int> map_index(_edge_maps.size());
|
1756 |
1762 |
int map_num, label_index;
|
1757 |
1763 |
|
1758 |
1764 |
char c;
|
1759 |
1765 |
if (!readLine() || !(line >> c) || c == '@') {
|
1760 |
1766 |
if (readSuccess() && line) line.putback(c);
|
1761 |
1767 |
if (!_edge_maps.empty())
|
1762 |
1768 |
throw DataFormatError("Cannot find map names");
|
1763 |
1769 |
return;
|
1764 |
1770 |
}
|
1765 |
1771 |
line.putback(c);
|
1766 |
1772 |
|
1767 |
1773 |
{
|
1768 |
1774 |
std::map<std::string, int> maps;
|
1769 |
1775 |
|
1770 |
1776 |
std::string map;
|
1771 |
1777 |
int index = 0;
|
1772 |
1778 |
while (_reader_bits::readToken(line, map)) {
|
1773 |
1779 |
if (maps.find(map) != maps.end()) {
|
1774 |
1780 |
std::ostringstream msg;
|
1775 |
1781 |
msg << "Multiple occurence of edge map: " << map;
|
1776 |
1782 |
throw DataFormatError(msg.str().c_str());
|
1777 |
1783 |
}
|
1778 |
1784 |
maps.insert(std::make_pair(map, index));
|
1779 |
1785 |
++index;
|
1780 |
1786 |
}
|
1781 |
1787 |
|
1782 |
1788 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
1783 |
1789 |
std::map<std::string, int>::iterator jt =
|
1784 |
1790 |
maps.find(_edge_maps[i].first);
|
1785 |
1791 |
if (jt == maps.end()) {
|
1786 |
1792 |
std::ostringstream msg;
|
1787 |
1793 |
msg << "Map not found in file: " << _edge_maps[i].first;
|
1788 |
1794 |
throw DataFormatError(msg.str().c_str());
|
1789 |
1795 |
}
|
1790 |
1796 |
map_index[i] = jt->second;
|
1791 |
1797 |
}
|
1792 |
1798 |
|
1793 |
1799 |
{
|
1794 |
1800 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
1795 |
1801 |
if (jt != maps.end()) {
|
1796 |
1802 |
label_index = jt->second;
|
1797 |
1803 |
} else {
|
1798 |
1804 |
label_index = -1;
|
1799 |
1805 |
}
|
1800 |
1806 |
}
|
1801 |
1807 |
map_num = maps.size();
|
1802 |
1808 |
}
|
1803 |
1809 |
|
1804 |
1810 |
while (readLine() && line >> c && c != '@') {
|
1805 |
1811 |
line.putback(c);
|
1806 |
1812 |
|
1807 |
1813 |
std::string source_token;
|
1808 |
1814 |
std::string target_token;
|
1809 |
1815 |
|
1810 |
1816 |
if (!_reader_bits::readToken(line, source_token))
|
1811 |
1817 |
throw DataFormatError("Node u not found");
|
1812 |
1818 |
|
1813 |
1819 |
if (!_reader_bits::readToken(line, target_token))
|
1814 |
1820 |
throw DataFormatError("Node v not found");
|
1815 |
1821 |
|
1816 |
1822 |
std::vector<std::string> tokens(map_num);
|
1817 |
1823 |
for (int i = 0; i < map_num; ++i) {
|
1818 |
1824 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
1819 |
1825 |
std::ostringstream msg;
|
1820 |
1826 |
msg << "Column not found (" << i + 1 << ")";
|
1821 |
1827 |
throw DataFormatError(msg.str().c_str());
|
1822 |
1828 |
}
|
1823 |
1829 |
}
|
1824 |
1830 |
if (line >> std::ws >> c)
|
1825 |
1831 |
throw DataFormatError("Extra character on the end of line");
|
1826 |
1832 |
|
1827 |
1833 |
Edge e;
|
1828 |
1834 |
if (!_use_edges) {
|
1829 |
1835 |
|
1830 |
1836 |
typename NodeIndex::iterator it;
|
1831 |
1837 |
|
1832 |
1838 |
it = _node_index.find(source_token);
|
1833 |
1839 |
if (it == _node_index.end()) {
|
1834 |
1840 |
std::ostringstream msg;
|
1835 |
1841 |
msg << "Item not found: " << source_token;
|
1836 |
1842 |
throw DataFormatError(msg.str().c_str());
|
1837 |
1843 |
}
|
1838 |
1844 |
Node source = it->second;
|
1839 |
1845 |
|
1840 |
1846 |
it = _node_index.find(target_token);
|
1841 |
1847 |
if (it == _node_index.end()) {
|
1842 |
1848 |
std::ostringstream msg;
|
1843 |
1849 |
msg << "Item not found: " << target_token;
|
1844 |
1850 |
throw DataFormatError(msg.str().c_str());
|
1845 |
1851 |
}
|
1846 |
1852 |
Node target = it->second;
|
1847 |
1853 |
|
1848 |
1854 |
e = _graph.addEdge(source, target);
|
1849 |
1855 |
if (label_index != -1)
|
1850 |
1856 |
_edge_index.insert(std::make_pair(tokens[label_index], e));
|
1851 |
1857 |
} else {
|
1852 |
1858 |
if (label_index == -1)
|
1853 |
1859 |
throw DataFormatError("Label map not found in file");
|
1854 |
1860 |
typename std::map<std::string, Edge>::iterator it =
|
1855 |
1861 |
_edge_index.find(tokens[label_index]);
|
1856 |
1862 |
if (it == _edge_index.end()) {
|
1857 |
1863 |
std::ostringstream msg;
|
1858 |
1864 |
msg << "Edge with label not found: " << tokens[label_index];
|
1859 |
1865 |
throw DataFormatError(msg.str().c_str());
|
1860 |
1866 |
}
|
1861 |
1867 |
e = it->second;
|
1862 |
1868 |
}
|
1863 |
1869 |
|
1864 |
1870 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
1865 |
1871 |
_edge_maps[i].second->set(e, tokens[map_index[i]]);
|
1866 |
1872 |
}
|
1867 |
1873 |
|
1868 |
1874 |
}
|
1869 |
1875 |
if (readSuccess()) {
|
1870 |
1876 |
line.putback(c);
|
1871 |
1877 |
}
|
1872 |
1878 |
}
|
1873 |
1879 |
|
1874 |
1880 |
void readAttributes() {
|
1875 |
1881 |
|
1876 |
1882 |
std::set<std::string> read_attr;
|
1877 |
1883 |
|
1878 |
1884 |
char c;
|
1879 |
1885 |
while (readLine() && line >> c && c != '@') {
|
1880 |
1886 |
line.putback(c);
|
1881 |
1887 |
|
1882 |
1888 |
std::string attr, token;
|
1883 |
1889 |
if (!_reader_bits::readToken(line, attr))
|
1884 |
1890 |
throw DataFormatError("Attribute name not found");
|
1885 |
1891 |
if (!_reader_bits::readToken(line, token))
|
1886 |
1892 |
throw DataFormatError("Attribute value not found");
|
1887 |
1893 |
if (line >> c)
|
1888 |
1894 |
throw DataFormatError("Extra character on the end of line");
|
1889 |
1895 |
|
1890 |
1896 |
{
|
1891 |
1897 |
std::set<std::string>::iterator it = read_attr.find(attr);
|
1892 |
1898 |
if (it != read_attr.end()) {
|
1893 |
1899 |
std::ostringstream msg;
|
1894 |
1900 |
msg << "Multiple occurence of attribute " << attr;
|
1895 |
1901 |
throw DataFormatError(msg.str().c_str());
|
1896 |
1902 |
}
|
1897 |
1903 |
read_attr.insert(attr);
|
1898 |
1904 |
}
|
1899 |
1905 |
|
1900 |
1906 |
{
|
1901 |
1907 |
typename Attributes::iterator it = _attributes.lower_bound(attr);
|
1902 |
1908 |
while (it != _attributes.end() && it->first == attr) {
|
1903 |
1909 |
it->second->set(token);
|
1904 |
1910 |
++it;
|
1905 |
1911 |
}
|
1906 |
1912 |
}
|
1907 |
1913 |
|
1908 |
1914 |
}
|
1909 |
1915 |
if (readSuccess()) {
|
1910 |
1916 |
line.putback(c);
|
1911 |
1917 |
}
|
1912 |
1918 |
for (typename Attributes::iterator it = _attributes.begin();
|
1913 |
1919 |
it != _attributes.end(); ++it) {
|
1914 |
1920 |
if (read_attr.find(it->first) == read_attr.end()) {
|
1915 |
1921 |
std::ostringstream msg;
|
1916 |
1922 |
msg << "Attribute not found in file: " << it->first;
|
1917 |
1923 |
throw DataFormatError(msg.str().c_str());
|
1918 |
1924 |
}
|
1919 |
1925 |
}
|
1920 |
1926 |
}
|
1921 |
1927 |
|
1922 |
1928 |
public:
|
1923 |
1929 |
|
1924 |
1930 |
/// \name Execution of the reader
|
1925 |
1931 |
/// @{
|
1926 |
1932 |
|
1927 |
1933 |
/// \brief Start the batch processing
|
1928 |
1934 |
///
|
1929 |
1935 |
/// This function starts the batch processing
|
1930 |
1936 |
void run() {
|
1931 |
1937 |
|
1932 |
1938 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
1933 |
1939 |
|
1934 |
1940 |
bool nodes_done = _skip_nodes;
|
1935 |
1941 |
bool edges_done = _skip_edges;
|
1936 |
1942 |
bool attributes_done = false;
|
1937 |
1943 |
|
1938 |
1944 |
line_num = 0;
|
1939 |
1945 |
readLine();
|
1940 |
1946 |
skipSection();
|
1941 |
1947 |
|
1942 |
1948 |
while (readSuccess()) {
|
1943 |
1949 |
try {
|
1944 |
1950 |
char c;
|
1945 |
1951 |
std::string section, caption;
|
1946 |
1952 |
line >> c;
|
1947 |
1953 |
_reader_bits::readToken(line, section);
|
1948 |
1954 |
_reader_bits::readToken(line, caption);
|
1949 |
1955 |
|
1950 |
1956 |
if (line >> c)
|
1951 |
1957 |
throw DataFormatError("Extra character on the end of line");
|
1952 |
1958 |
|
1953 |
1959 |
if (section == "nodes" && !nodes_done) {
|
1954 |
1960 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
1955 |
1961 |
readNodes();
|
1956 |
1962 |
nodes_done = true;
|
1957 |
1963 |
}
|
1958 |
1964 |
} else if ((section == "edges" || section == "arcs") &&
|
1959 |
1965 |
!edges_done) {
|
1960 |
1966 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
1961 |
1967 |
readEdges();
|
1962 |
1968 |
edges_done = true;
|
1963 |
1969 |
}
|
1964 |
1970 |
} else if (section == "attributes" && !attributes_done) {
|
1965 |
1971 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
1966 |
1972 |
readAttributes();
|
1967 |
1973 |
attributes_done = true;
|
1968 |
1974 |
}
|
1969 |
1975 |
} else {
|
1970 |
1976 |
readLine();
|
1971 |
1977 |
skipSection();
|
1972 |
1978 |
}
|
1973 |
1979 |
} catch (DataFormatError& error) {
|
1974 |
1980 |
error.line(line_num);
|
1975 |
1981 |
throw;
|
1976 |
1982 |
}
|
1977 |
1983 |
}
|
1978 |
1984 |
|
1979 |
1985 |
if (!nodes_done) {
|
1980 |
1986 |
throw DataFormatError("Section @nodes not found");
|
1981 |
1987 |
}
|
1982 |
1988 |
|
1983 |
1989 |
if (!edges_done) {
|
1984 |
1990 |
throw DataFormatError("Section @edges not found");
|
1985 |
1991 |
}
|
1986 |
1992 |
|
1987 |
1993 |
if (!attributes_done && !_attributes.empty()) {
|
1988 |
1994 |
throw DataFormatError("Section @attributes not found");
|
1989 |
1995 |
}
|
1990 |
1996 |
|
1991 |
1997 |
}
|
1992 |
1998 |
|
1993 |
1999 |
/// @}
|
1994 |
2000 |
|
1995 |
2001 |
};
|
1996 |
2002 |
|
1997 |
2003 |
/// \brief Return a \ref GraphReader class
|
1998 |
2004 |
///
|
1999 |
2005 |
/// This function just returns a \ref GraphReader class.
|
2000 |
2006 |
/// \relates GraphReader
|
2001 |
2007 |
template <typename Graph>
|
2002 |
2008 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
|
2003 |
2009 |
GraphReader<Graph> tmp(is, graph);
|
2004 |
2010 |
return tmp;
|
2005 |
2011 |
}
|
2006 |
2012 |
|
2007 |
2013 |
/// \brief Return a \ref GraphReader class
|
2008 |
2014 |
///
|
2009 |
2015 |
/// This function just returns a \ref GraphReader class.
|
2010 |
2016 |
/// \relates GraphReader
|
2011 |
2017 |
template <typename Graph>
|
2012 |
2018 |
GraphReader<Graph> graphReader(const std::string& fn,
|
2013 |
2019 |
Graph& graph) {
|
2014 |
2020 |
GraphReader<Graph> tmp(fn, graph);
|
2015 |
2021 |
return tmp;
|
2016 |
2022 |
}
|
2017 |
2023 |
|
2018 |
2024 |
/// \brief Return a \ref GraphReader class
|
2019 |
2025 |
///
|
2020 |
2026 |
/// This function just returns a \ref GraphReader class.
|
2021 |
2027 |
/// \relates GraphReader
|
2022 |
2028 |
template <typename Graph>
|
2023 |
2029 |
GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
|
2024 |
2030 |
GraphReader<Graph> tmp(fn, graph);
|
2025 |
2031 |
return tmp;
|
2026 |
2032 |
}
|
2027 |
2033 |
|
2028 |
2034 |
class SectionReader;
|
2029 |
2035 |
|
2030 |
2036 |
SectionReader sectionReader(std::istream& is);
|
2031 |
2037 |
SectionReader sectionReader(const std::string& fn);
|
2032 |
2038 |
SectionReader sectionReader(const char* fn);
|
2033 |
2039 |
|
2034 |
2040 |
/// \ingroup lemon_io
|
2035 |
2041 |
///
|
2036 |
2042 |
/// \brief Section reader class
|
2037 |
2043 |
///
|
2038 |
2044 |
/// In the \ref lgf-format "LGF" file extra sections can be placed,
|
2039 |
2045 |
/// which contain any data in arbitrary format. Such sections can be
|
2040 |
2046 |
/// read with this class. A reading rule can be added to the class
|
2041 |
2047 |
/// with two different functions. With the \c sectionLines() function a
|
2042 |
2048 |
/// functor can process the section line-by-line, while with the \c
|
2043 |
2049 |
/// sectionStream() member the section can be read from an input
|
2044 |
2050 |
/// stream.
|
2045 |
2051 |
class SectionReader {
|
2046 |
2052 |
private:
|
2047 |
2053 |
|
2048 |
2054 |
std::istream* _is;
|
2049 |
2055 |
bool local_is;
|
2050 |
2056 |
|
2051 |
2057 |
typedef std::map<std::string, _reader_bits::Section*> Sections;
|
2052 |
2058 |
Sections _sections;
|
2053 |
2059 |
|
2054 |
2060 |
int line_num;
|
2055 |
2061 |
std::istringstream line;
|
2056 |
2062 |
|
2057 |
2063 |
public:
|
2058 |
2064 |
|
2059 |
2065 |
/// \brief Constructor
|
2060 |
2066 |
///
|
2061 |
2067 |
/// Construct a section reader, which reads from the given input
|
2062 |
2068 |
/// stream.
|
2063 |
2069 |
SectionReader(std::istream& is)
|
2064 |
2070 |
: _is(&is), local_is(false) {}
|
2065 |
2071 |
|
2066 |
2072 |
/// \brief Constructor
|
2067 |
2073 |
///
|
2068 |
2074 |
/// Construct a section reader, which reads from the given file.
|
2069 |
2075 |
SectionReader(const std::string& fn)
|
2070 |
2076 |
: _is(new std::ifstream(fn.c_str())), local_is(true) {}
|
2071 |
2077 |
|
2072 |
2078 |
/// \brief Constructor
|
2073 |
2079 |
///
|
2074 |
2080 |
/// Construct a section reader, which reads from the given file.
|
2075 |
2081 |
SectionReader(const char* fn)
|
2076 |
2082 |
: _is(new std::ifstream(fn)), local_is(true) {}
|
2077 |
2083 |
|
2078 |
2084 |
/// \brief Destructor
|
2079 |
2085 |
~SectionReader() {
|
2080 |
2086 |
for (Sections::iterator it = _sections.begin();
|
2081 |
2087 |
it != _sections.end(); ++it) {
|
2082 |
2088 |
delete it->second;
|
2083 |
2089 |
}
|
2084 |
2090 |
|
2085 |
2091 |
if (local_is) {
|
2086 |
2092 |
delete _is;
|
2087 |
2093 |
}
|
2088 |
2094 |
|
2089 |
2095 |
}
|
2090 |
2096 |
|
2091 |
2097 |
private:
|
2092 |
2098 |
|
2093 |
2099 |
friend SectionReader sectionReader(std::istream& is);
|
2094 |
2100 |
friend SectionReader sectionReader(const std::string& fn);
|
2095 |
2101 |
friend SectionReader sectionReader(const char* fn);
|
2096 |
2102 |
|
2097 |
2103 |
SectionReader(SectionReader& other)
|
2098 |
2104 |
: _is(other._is), local_is(other.local_is) {
|
2099 |
2105 |
|
2100 |
2106 |
other._is = 0;
|
2101 |
2107 |
other.local_is = false;
|
2102 |
2108 |
|
2103 |
2109 |
_sections.swap(other._sections);
|
2104 |
2110 |
}
|
2105 |
2111 |
|
2106 |
2112 |
SectionReader& operator=(const SectionReader&);
|
2107 |
2113 |
|
2108 |
2114 |
public:
|
2109 |
2115 |
|
2110 |
2116 |
/// \name Section readers
|
2111 |
2117 |
/// @{
|
2112 |
2118 |
|
2113 |
2119 |
/// \brief Add a section processor with line oriented reading
|
2114 |
2120 |
///
|
2115 |
2121 |
/// The first parameter is the type descriptor of the section, the
|
2116 |
2122 |
/// second is a functor, which takes just one \c std::string
|
2117 |
2123 |
/// parameter. At the reading process, each line of the section
|
2118 |
2124 |
/// will be given to the functor object. However, the empty lines
|
2119 |
2125 |
/// and the comment lines are filtered out, and the leading
|
2120 |
2126 |
/// whitespaces are trimmed from each processed string.
|
2121 |
2127 |
///
|
2122 |
2128 |
/// For example let's see a section, which contain several
|
2123 |
2129 |
/// integers, which should be inserted into a vector.
|
2124 |
2130 |
///\code
|
2125 |
2131 |
/// @numbers
|
2126 |
2132 |
/// 12 45 23
|
2127 |
2133 |
/// 4
|
2128 |
2134 |
/// 23 6
|
2129 |
2135 |
///\endcode
|
2130 |
2136 |
///
|
2131 |
2137 |
/// The functor is implemented as a struct:
|
2132 |
2138 |
///\code
|
2133 |
2139 |
/// struct NumberSection {
|
2134 |
2140 |
/// std::vector<int>& _data;
|
2135 |
2141 |
/// NumberSection(std::vector<int>& data) : _data(data) {}
|
2136 |
2142 |
/// void operator()(const std::string& line) {
|
2137 |
2143 |
/// std::istringstream ls(line);
|
2138 |
2144 |
/// int value;
|
2139 |
2145 |
/// while (ls >> value) _data.push_back(value);
|
2140 |
2146 |
/// }
|
2141 |
2147 |
/// };
|
2142 |
2148 |
///
|
2143 |
2149 |
/// // ...
|
2144 |
2150 |
///
|
2145 |
2151 |
/// reader.sectionLines("numbers", NumberSection(vec));
|
2146 |
2152 |
///\endcode
|
2147 |
2153 |
template <typename Functor>
|
2148 |
2154 |
SectionReader& sectionLines(const std::string& type, Functor functor) {
|
2149 |
2155 |
LEMON_ASSERT(!type.empty(), "Type is empty.");
|
2150 |
2156 |
LEMON_ASSERT(_sections.find(type) == _sections.end(),
|
2151 |
2157 |
"Multiple reading of section.");
|
2152 |
2158 |
_sections.insert(std::make_pair(type,
|
2153 |
2159 |
new _reader_bits::LineSection<Functor>(functor)));
|
2154 |
2160 |
return *this;
|
2155 |
2161 |
}
|
2156 |
2162 |
|
2157 |
2163 |
|
2158 |
2164 |
/// \brief Add a section processor with stream oriented reading
|
2159 |
2165 |
///
|
2160 |
2166 |
/// The first parameter is the type of the section, the second is
|
2161 |
2167 |
/// a functor, which takes an \c std::istream& and an \c int&
|
2162 |
2168 |
/// parameter, the latter regard to the line number of stream. The
|
2163 |
2169 |
/// functor can read the input while the section go on, and the
|
2164 |
2170 |
/// line number should be modified accordingly.
|
2165 |
2171 |
template <typename Functor>
|
2166 |
2172 |
SectionReader& sectionStream(const std::string& type, Functor functor) {
|
2167 |
2173 |
LEMON_ASSERT(!type.empty(), "Type is empty.");
|
2168 |
2174 |
LEMON_ASSERT(_sections.find(type) == _sections.end(),
|
2169 |
2175 |
"Multiple reading of section.");
|
2170 |
2176 |
_sections.insert(std::make_pair(type,
|
2171 |
2177 |
new _reader_bits::StreamSection<Functor>(functor)));
|
2172 |
2178 |
return *this;
|
2173 |
2179 |
}
|
2174 |
2180 |
|
2175 |
2181 |
/// @}
|
2176 |
2182 |
|
2177 |
2183 |
private:
|
2178 |
2184 |
|
2179 |
2185 |
bool readLine() {
|
2180 |
2186 |
std::string str;
|
2181 |
2187 |
while(++line_num, std::getline(*_is, str)) {
|
2182 |
2188 |
line.clear(); line.str(str);
|
2183 |
2189 |
char c;
|
2184 |
2190 |
if (line >> std::ws >> c && c != '#') {
|
2185 |
2191 |
line.putback(c);
|
2186 |
2192 |
return true;
|
2187 |
2193 |
}
|
2188 |
2194 |
}
|
2189 |
2195 |
return false;
|
2190 |
2196 |
}
|
2191 |
2197 |
|
2192 |
2198 |
bool readSuccess() {
|
2193 |
2199 |
return static_cast<bool>(*_is);
|
2194 |
2200 |
}
|
2195 |
2201 |
|
2196 |
2202 |
void skipSection() {
|
2197 |
2203 |
char c;
|
2198 |
2204 |
while (readSuccess() && line >> c && c != '@') {
|
2199 |
2205 |
readLine();
|
2200 |
2206 |
}
|
2201 |
2207 |
line.putback(c);
|
2202 |
2208 |
}
|
2203 |
2209 |
|
2204 |
2210 |
public:
|
2205 |
2211 |
|
2206 |
2212 |
|
2207 |
2213 |
/// \name Execution of the reader
|
2208 |
2214 |
/// @{
|
2209 |
2215 |
|
2210 |
2216 |
/// \brief Start the batch processing
|
2211 |
2217 |
///
|
2212 |
2218 |
/// This function starts the batch processing.
|
2213 |
2219 |
void run() {
|
2214 |
2220 |
|
2215 |
2221 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
2216 |
2222 |
|
2217 |
2223 |
std::set<std::string> extra_sections;
|
2218 |
2224 |
|
2219 |
2225 |
line_num = 0;
|
2220 |
2226 |
readLine();
|
2221 |
2227 |
skipSection();
|
2222 |
2228 |
|
2223 |
2229 |
while (readSuccess()) {
|
2224 |
2230 |
try {
|
2225 |
2231 |
char c;
|
2226 |
2232 |
std::string section, caption;
|
2227 |
2233 |
line >> c;
|
2228 |
2234 |
_reader_bits::readToken(line, section);
|
2229 |
2235 |
_reader_bits::readToken(line, caption);
|
2230 |
2236 |
|
2231 |
2237 |
if (line >> c)
|
2232 |
2238 |
throw DataFormatError("Extra character on the end of line");
|
2233 |
2239 |
|
2234 |
2240 |
if (extra_sections.find(section) != extra_sections.end()) {
|
2235 |
2241 |
std::ostringstream msg;
|
2236 |
2242 |
msg << "Multiple occurence of section " << section;
|
2237 |
2243 |
throw DataFormatError(msg.str().c_str());
|
2238 |
2244 |
}
|
2239 |
2245 |
Sections::iterator it = _sections.find(section);
|
2240 |
2246 |
if (it != _sections.end()) {
|
2241 |
2247 |
extra_sections.insert(section);
|
2242 |
2248 |
it->second->process(*_is, line_num);
|
2243 |
2249 |
}
|
2244 |
2250 |
readLine();
|
2245 |
2251 |
skipSection();
|
2246 |
2252 |
} catch (DataFormatError& error) {
|
2247 |
2253 |
error.line(line_num);
|
2248 |
2254 |
throw;
|
2249 |
2255 |
}
|
2250 |
2256 |
}
|
2251 |
2257 |
for (Sections::iterator it = _sections.begin();
|
2252 |
2258 |
it != _sections.end(); ++it) {
|
2253 |
2259 |
if (extra_sections.find(it->first) == extra_sections.end()) {
|
2254 |
2260 |
std::ostringstream os;
|
2255 |
2261 |
os << "Cannot find section: " << it->first;
|
2256 |
2262 |
throw DataFormatError(os.str().c_str());
|
2257 |
2263 |
}
|
2258 |
2264 |
}
|
2259 |
2265 |
}
|
2260 |
2266 |
|
2261 |
2267 |
/// @}
|
2262 |
2268 |
|
2263 |
2269 |
};
|
2264 |
2270 |
|
2265 |
2271 |
/// \brief Return a \ref SectionReader class
|
2266 |
2272 |
///
|
2267 |
2273 |
/// This function just returns a \ref SectionReader class.
|
2268 |
2274 |
/// \relates SectionReader
|
2269 |
2275 |
inline SectionReader sectionReader(std::istream& is) {
|
2270 |
2276 |
SectionReader tmp(is);
|
2271 |
2277 |
return tmp;
|
2272 |
2278 |
}
|
2273 |
2279 |
|
2274 |
2280 |
/// \brief Return a \ref SectionReader class
|
2275 |
2281 |
///
|
2276 |
2282 |
/// This function just returns a \ref SectionReader class.
|
2277 |
2283 |
/// \relates SectionReader
|
2278 |
2284 |
inline SectionReader sectionReader(const std::string& fn) {
|
2279 |
2285 |
SectionReader tmp(fn);
|
2280 |
2286 |
return tmp;
|
2281 |
2287 |
}
|
2282 |
2288 |
|
2283 |
2289 |
/// \brief Return a \ref SectionReader class
|
2284 |
2290 |
///
|
2285 |
2291 |
/// This function just returns a \ref SectionReader class.
|
2286 |
2292 |
/// \relates SectionReader
|
2287 |
2293 |
inline SectionReader sectionReader(const char* fn) {
|
2288 |
2294 |
SectionReader tmp(fn);
|
2289 |
2295 |
return tmp;
|
2290 |
2296 |
}
|
2291 |
2297 |
|
2292 |
2298 |
/// \ingroup lemon_io
|
2293 |
2299 |
///
|
2294 |
2300 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file
|
2295 |
2301 |
///
|
2296 |
2302 |
/// This class can be used to read the sections, the map names and
|
2297 |
2303 |
/// the attributes from a file. Usually, the Lemon programs know
|
2298 |
2304 |
/// that, which type of graph, which maps and which attributes
|
2299 |
2305 |
/// should be read from a file, but in general tools (like glemon)
|
2300 |
2306 |
/// the contents of an LGF file should be guessed somehow. This class
|
2301 |
2307 |
/// reads the graph and stores the appropriate information for
|
2302 |
2308 |
/// reading the graph.
|
2303 |
2309 |
///
|
2304 |
2310 |
///\code
|
2305 |
2311 |
/// LgfContents contents("graph.lgf");
|
2306 |
2312 |
/// contents.run();
|
2307 |
2313 |
///
|
2308 |
2314 |
/// // Does it contain any node section and arc section?
|
2309 |
2315 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
|
2310 |
2316 |
/// std::cerr << "Failure, cannot find graph." << std::endl;
|
2311 |
2317 |
/// return -1;
|
2312 |
2318 |
/// }
|
2313 |
2319 |
/// std::cout << "The name of the default node section: "
|
2314 |
2320 |
/// << contents.nodeSection(0) << std::endl;
|
2315 |
2321 |
/// std::cout << "The number of the arc maps: "
|
2316 |
2322 |
/// << contents.arcMaps(0).size() << std::endl;
|
2317 |
2323 |
/// std::cout << "The name of second arc map: "
|
2318 |
2324 |
/// << contents.arcMaps(0)[1] << std::endl;
|
2319 |
2325 |
///\endcode
|
2320 |
2326 |
class LgfContents {
|
2321 |
2327 |
private:
|
2322 |
2328 |
|
2323 |
2329 |
std::istream* _is;
|
2324 |
2330 |
bool local_is;
|
2325 |
2331 |
|
2326 |
2332 |
std::vector<std::string> _node_sections;
|
2327 |
2333 |
std::vector<std::string> _edge_sections;
|
2328 |
2334 |
std::vector<std::string> _attribute_sections;
|
2329 |
2335 |
std::vector<std::string> _extra_sections;
|
2330 |
2336 |
|
2331 |
2337 |
std::vector<bool> _arc_sections;
|
2332 |
2338 |
|
2333 |
2339 |
std::vector<std::vector<std::string> > _node_maps;
|
2334 |
2340 |
std::vector<std::vector<std::string> > _edge_maps;
|
2335 |
2341 |
|
2336 |
2342 |
std::vector<std::vector<std::string> > _attributes;
|
2337 |
2343 |
|
2338 |
2344 |
|
2339 |
2345 |
int line_num;
|
2340 |
2346 |
std::istringstream line;
|
2341 |
2347 |
|
2342 |
2348 |
public:
|
2343 |
2349 |
|
2344 |
2350 |
/// \brief Constructor
|
2345 |
2351 |
///
|
2346 |
2352 |
/// Construct an \e LGF contents reader, which reads from the given
|
2347 |
2353 |
/// input stream.
|
2348 |
2354 |
LgfContents(std::istream& is)
|
2349 |
2355 |
: _is(&is), local_is(false) {}
|
2350 |
2356 |
|
2351 |
2357 |
/// \brief Constructor
|
2352 |
2358 |
///
|
2353 |
2359 |
/// Construct an \e LGF contents reader, which reads from the given
|
2354 |
2360 |
/// file.
|
2355 |
2361 |
LgfContents(const std::string& fn)
|
2356 |
2362 |
: _is(new std::ifstream(fn.c_str())), local_is(true) {}
|
2357 |
2363 |
|
2358 |
2364 |
/// \brief Constructor
|
2359 |
2365 |
///
|
2360 |
2366 |
/// Construct an \e LGF contents reader, which reads from the given
|
2361 |
2367 |
/// file.
|
2362 |
2368 |
LgfContents(const char* fn)
|
2363 |
2369 |
: _is(new std::ifstream(fn)), local_is(true) {}
|
2364 |
2370 |
|
2365 |
2371 |
/// \brief Destructor
|
2366 |
2372 |
~LgfContents() {
|
2367 |
2373 |
if (local_is) delete _is;
|
2368 |
2374 |
}
|
2369 |
2375 |
|
2370 |
2376 |
private:
|
2371 |
2377 |
|
2372 |
2378 |
LgfContents(const LgfContents&);
|
2373 |
2379 |
LgfContents& operator=(const LgfContents&);
|
2374 |
2380 |
|
2375 |
2381 |
public:
|
2376 |
2382 |
|
2377 |
2383 |
|
2378 |
2384 |
/// \name Node sections
|
2379 |
2385 |
/// @{
|
2380 |
2386 |
|
2381 |
2387 |
/// \brief Gives back the number of node sections in the file.
|
2382 |
2388 |
///
|
2383 |
2389 |
/// Gives back the number of node sections in the file.
|
2384 |
2390 |
int nodeSectionNum() const {
|
2385 |
2391 |
return _node_sections.size();
|
2386 |
2392 |
}
|
2387 |
2393 |
|
2388 |
2394 |
/// \brief Returns the node section name at the given position.
|
2389 |
2395 |
///
|
2390 |
2396 |
/// Returns the node section name at the given position.
|
2391 |
2397 |
const std::string& nodeSection(int i) const {
|
2392 |
2398 |
return _node_sections[i];
|
2393 |
2399 |
}
|
2394 |
2400 |
|
2395 |
2401 |
/// \brief Gives back the node maps for the given section.
|
2396 |
2402 |
///
|
2397 |
2403 |
/// Gives back the node maps for the given section.
|
2398 |
2404 |
const std::vector<std::string>& nodeMapNames(int i) const {
|
2399 |
2405 |
return _node_maps[i];
|
2400 |
2406 |
}
|
2401 |
2407 |
|
2402 |
2408 |
/// @}
|
2403 |
2409 |
|
2404 |
2410 |
/// \name Arc/Edge sections
|
2405 |
2411 |
/// @{
|
2406 |
2412 |
|
2407 |
2413 |
/// \brief Gives back the number of arc/edge sections in the file.
|
2408 |
2414 |
///
|
2409 |
2415 |
/// Gives back the number of arc/edge sections in the file.
|
2410 |
2416 |
/// \note It is synonym of \c edgeSectionNum().
|
2411 |
2417 |
int arcSectionNum() const {
|
2412 |
2418 |
return _edge_sections.size();
|
2413 |
2419 |
}
|
2414 |
2420 |
|
2415 |
2421 |
/// \brief Returns the arc/edge section name at the given position.
|
2416 |
2422 |
///
|
2417 |
2423 |
/// Returns the arc/edge section name at the given position.
|
2418 |
2424 |
/// \note It is synonym of \c edgeSection().
|
2419 |
2425 |
const std::string& arcSection(int i) const {
|
2420 |
2426 |
return _edge_sections[i];
|
2421 |
2427 |
}
|
2422 |
2428 |
|
2423 |
2429 |
/// \brief Gives back the arc/edge maps for the given section.
|
2424 |
2430 |
///
|
2425 |
2431 |
/// Gives back the arc/edge maps for the given section.
|
2426 |
2432 |
/// \note It is synonym of \c edgeMapNames().
|
2427 |
2433 |
const std::vector<std::string>& arcMapNames(int i) const {
|
2428 |
2434 |
return _edge_maps[i];
|
2429 |
2435 |
}
|
2430 |
2436 |
|
2431 |
2437 |
/// @}
|
2432 |
2438 |
|
2433 |
2439 |
/// \name Synonyms
|
2434 |
2440 |
/// @{
|
2435 |
2441 |
|
2436 |
2442 |
/// \brief Gives back the number of arc/edge sections in the file.
|
2437 |
2443 |
///
|
2438 |
2444 |
/// Gives back the number of arc/edge sections in the file.
|
2439 |
2445 |
/// \note It is synonym of \c arcSectionNum().
|
2440 |
2446 |
int edgeSectionNum() const {
|
2441 |
2447 |
return _edge_sections.size();
|
2442 |
2448 |
}
|
2443 |
2449 |
|
2444 |
2450 |
/// \brief Returns the section name at the given position.
|
2445 |
2451 |
///
|
2446 |
2452 |
/// Returns the section name at the given position.
|
2447 |
2453 |
/// \note It is synonym of \c arcSection().
|
2448 |
2454 |
const std::string& edgeSection(int i) const {
|
2449 |
2455 |
return _edge_sections[i];
|
2450 |
2456 |
}
|
2451 |
2457 |
|
2452 |
2458 |
/// \brief Gives back the edge maps for the given section.
|
2453 |
2459 |
///
|
2454 |
2460 |
/// Gives back the edge maps for the given section.
|
2455 |
2461 |
/// \note It is synonym of \c arcMapNames().
|
2456 |
2462 |
const std::vector<std::string>& edgeMapNames(int i) const {
|
2457 |
2463 |
return _edge_maps[i];
|
2458 |
2464 |
}
|
2459 |
2465 |
|
2460 |
2466 |
/// @}
|
2461 |
2467 |
|
2462 |
2468 |
/// \name Attribute sections
|
2463 |
2469 |
/// @{
|
2464 |
2470 |
|
2465 |
2471 |
/// \brief Gives back the number of attribute sections in the file.
|
2466 |
2472 |
///
|
2467 |
2473 |
/// Gives back the number of attribute sections in the file.
|
2468 |
2474 |
int attributeSectionNum() const {
|
2469 |
2475 |
return _attribute_sections.size();
|
2470 |
2476 |
}
|
2471 |
2477 |
|
2472 |
2478 |
/// \brief Returns the attribute section name at the given position.
|
2473 |
2479 |
///
|
2474 |
2480 |
/// Returns the attribute section name at the given position.
|
2475 |
2481 |
const std::string& attributeSectionNames(int i) const {
|
2476 |
2482 |
return _attribute_sections[i];
|
2477 |
2483 |
}
|
2478 |
2484 |
|
2479 |
2485 |
/// \brief Gives back the attributes for the given section.
|
2480 |
2486 |
///
|
2481 |
2487 |
/// Gives back the attributes for the given section.
|
2482 |
2488 |
const std::vector<std::string>& attributes(int i) const {
|
2483 |
2489 |
return _attributes[i];
|
2484 |
2490 |
}
|
2485 |
2491 |
|
2486 |
2492 |
/// @}
|
2487 |
2493 |
|
2488 |
2494 |
/// \name Extra sections
|
2489 |
2495 |
/// @{
|
2490 |
2496 |
|
2491 |
2497 |
/// \brief Gives back the number of extra sections in the file.
|
2492 |
2498 |
///
|
2493 |
2499 |
/// Gives back the number of extra sections in the file.
|
2494 |
2500 |
int extraSectionNum() const {
|
2495 |
2501 |
return _extra_sections.size();
|
2496 |
2502 |
}
|
2497 |
2503 |
|
2498 |
2504 |
/// \brief Returns the extra section type at the given position.
|
2499 |
2505 |
///
|
2500 |
2506 |
/// Returns the section type at the given position.
|
2501 |
2507 |
const std::string& extraSection(int i) const {
|
2502 |
2508 |
return _extra_sections[i];
|
2503 |
2509 |
}
|
2504 |
2510 |
|
2505 |
2511 |
/// @}
|
2506 |
2512 |
|
2507 |
2513 |
private:
|
2508 |
2514 |
|
2509 |
2515 |
bool readLine() {
|
2510 |
2516 |
std::string str;
|
2511 |
2517 |
while(++line_num, std::getline(*_is, str)) {
|
2512 |
2518 |
line.clear(); line.str(str);
|
2513 |
2519 |
char c;
|
2514 |
2520 |
if (line >> std::ws >> c && c != '#') {
|
2515 |
2521 |
line.putback(c);
|
2516 |
2522 |
return true;
|
2517 |
2523 |
}
|
2518 |
2524 |
}
|
2519 |
2525 |
return false;
|
2520 |
2526 |
}
|
2521 |
2527 |
|
2522 |
2528 |
bool readSuccess() {
|
2523 |
2529 |
return static_cast<bool>(*_is);
|
2524 |
2530 |
}
|
2525 |
2531 |
|
2526 |
2532 |
void skipSection() {
|
2527 |
2533 |
char c;
|
2528 |
2534 |
while (readSuccess() && line >> c && c != '@') {
|
2529 |
2535 |
readLine();
|
2530 |
2536 |
}
|
2531 |
2537 |
line.putback(c);
|
2532 |
2538 |
}
|
2533 |
2539 |
|
2534 |
2540 |
void readMaps(std::vector<std::string>& maps) {
|
2535 |
2541 |
char c;
|
2536 |
2542 |
if (!readLine() || !(line >> c) || c == '@') {
|
2537 |
2543 |
if (readSuccess() && line) line.putback(c);
|
2538 |
2544 |
return;
|
2539 |
2545 |
}
|
2540 |
2546 |
line.putback(c);
|
2541 |
2547 |
std::string map;
|
2542 |
2548 |
while (_reader_bits::readToken(line, map)) {
|
2543 |
2549 |
maps.push_back(map);
|
2544 |
2550 |
}
|
2545 |
2551 |
}
|
2546 |
2552 |
|
2547 |
2553 |
void readAttributes(std::vector<std::string>& attrs) {
|
2548 |
2554 |
readLine();
|
2549 |
2555 |
char c;
|
2550 |
2556 |
while (readSuccess() && line >> c && c != '@') {
|
2551 |
2557 |
line.putback(c);
|
2552 |
2558 |
std::string attr;
|
2553 |
2559 |
_reader_bits::readToken(line, attr);
|
2554 |
2560 |
attrs.push_back(attr);
|
2555 |
2561 |
readLine();
|
2556 |
2562 |
}
|
2557 |
2563 |
line.putback(c);
|
2558 |
2564 |
}
|
2559 |
2565 |
|
2560 |
2566 |
public:
|
2561 |
2567 |
|
2562 |
2568 |
/// \name Execution of the contents reader
|
2563 |
2569 |
/// @{
|
2564 |
2570 |
|
2565 |
2571 |
/// \brief Starts the reading
|
2566 |
2572 |
///
|
2567 |
2573 |
/// This function starts the reading.
|
2568 |
2574 |
void run() {
|
2569 |
2575 |
|
2570 |
2576 |
readLine();
|
2571 |
2577 |
skipSection();
|
2572 |
2578 |
|
2573 |
2579 |
while (readSuccess()) {
|
2574 |
2580 |
|
2575 |
2581 |
char c;
|
2576 |
2582 |
line >> c;
|
2577 |
2583 |
|
2578 |
2584 |
std::string section, caption;
|
2579 |
2585 |
_reader_bits::readToken(line, section);
|
2580 |
2586 |
_reader_bits::readToken(line, caption);
|
2581 |
2587 |
|
2582 |
2588 |
if (section == "nodes") {
|
2583 |
2589 |
_node_sections.push_back(caption);
|
2584 |
2590 |
_node_maps.push_back(std::vector<std::string>());
|
2585 |
2591 |
readMaps(_node_maps.back());
|
2586 |
2592 |
readLine(); skipSection();
|
2587 |
2593 |
} else if (section == "arcs" || section == "edges") {
|
2588 |
2594 |
_edge_sections.push_back(caption);
|
2589 |
2595 |
_arc_sections.push_back(section == "arcs");
|
2590 |
2596 |
_edge_maps.push_back(std::vector<std::string>());
|
2591 |
2597 |
readMaps(_edge_maps.back());
|
2592 |
2598 |
readLine(); skipSection();
|
2593 |
2599 |
} else if (section == "attributes") {
|
2594 |
2600 |
_attribute_sections.push_back(caption);
|
2595 |
2601 |
_attributes.push_back(std::vector<std::string>());
|
2596 |
2602 |
readAttributes(_attributes.back());
|
2597 |
2603 |
} else {
|
2598 |
2604 |
_extra_sections.push_back(section);
|
2599 |
2605 |
readLine(); skipSection();
|
2600 |
2606 |
}
|
2601 |
2607 |
}
|
2602 |
2608 |
}
|
2603 |
2609 |
|
2604 |
2610 |
/// @}
|
2605 |
2611 |
|
2606 |
2612 |
};
|
2607 |
2613 |
}
|
2608 |
2614 |
|
2609 |
2615 |
#endif
|