deba@1408
|
1 |
/* -*- C++ -*-
|
deba@1408
|
2 |
* src/lemon/lemon_reader.h - Part of LEMON, a generic C++ optimization library
|
deba@1408
|
3 |
*
|
deba@1408
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1408
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@1408
|
6 |
*
|
deba@1408
|
7 |
* Permission to use, modify and distribute this software is granted
|
deba@1408
|
8 |
* provided that this copyright notice appears in all copies. For
|
deba@1408
|
9 |
* precise terms see the accompanying LICENSE file.
|
deba@1408
|
10 |
*
|
deba@1408
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1408
|
12 |
* express or implied, and with no claim as to its suitability for any
|
deba@1408
|
13 |
* purpose.
|
deba@1408
|
14 |
*
|
deba@1408
|
15 |
*/
|
deba@1408
|
16 |
|
deba@1408
|
17 |
///\ingroup io_group
|
deba@1408
|
18 |
///\file
|
deba@1408
|
19 |
///\brief Lemon Format reader.
|
deba@1408
|
20 |
|
deba@1421
|
21 |
|
deba@1408
|
22 |
#ifndef LEMON_LEMON_READER_H
|
deba@1408
|
23 |
#define LEMON_LEMON_READER_H
|
deba@1408
|
24 |
|
deba@1421
|
25 |
|
deba@1408
|
26 |
#include <iostream>
|
deba@1408
|
27 |
#include <fstream>
|
deba@1408
|
28 |
#include <string>
|
deba@1408
|
29 |
#include <vector>
|
deba@1408
|
30 |
#include <algorithm>
|
deba@1408
|
31 |
#include <map>
|
deba@1408
|
32 |
#include <memory>
|
deba@1408
|
33 |
|
deba@1408
|
34 |
#include <lemon/error.h>
|
deba@1421
|
35 |
#include <lemon/graph_utils.h>
|
deba@1421
|
36 |
#include <lemon/utility.h>
|
deba@1409
|
37 |
#include <lemon/bits/item_reader.h>
|
deba@1408
|
38 |
|
deba@1408
|
39 |
|
deba@1408
|
40 |
namespace lemon {
|
deba@1408
|
41 |
|
deba@1421
|
42 |
namespace _reader_bits {
|
deba@1421
|
43 |
|
deba@1421
|
44 |
template <typename T>
|
deba@1421
|
45 |
bool operator<(T, T) {
|
deba@1421
|
46 |
throw DataFormatError("Id is not comparable");
|
deba@1421
|
47 |
}
|
deba@1421
|
48 |
|
deba@1421
|
49 |
template <typename T>
|
deba@1421
|
50 |
struct Less {
|
deba@1421
|
51 |
bool operator()(const T& p, const T& q) const {
|
deba@1421
|
52 |
return p < q;
|
deba@1421
|
53 |
}
|
deba@1421
|
54 |
};
|
deba@1421
|
55 |
|
deba@1421
|
56 |
template <typename M1, typename M2>
|
deba@1421
|
57 |
class WriteComposeMap {
|
deba@1421
|
58 |
public:
|
deba@1421
|
59 |
typedef True NeedCopy;
|
deba@1421
|
60 |
|
deba@1421
|
61 |
typedef typename M2::Key Key;
|
deba@1421
|
62 |
typedef typename M1::Value Value;
|
deba@1421
|
63 |
|
deba@1421
|
64 |
WriteComposeMap(typename SmartParameter<M1>::Type _m1, const M2& _m2)
|
deba@1421
|
65 |
: m1(_m1), m2(_m2) {}
|
deba@1421
|
66 |
|
deba@1421
|
67 |
void set(const Key& key, const Value& value) {
|
deba@1421
|
68 |
m1.set(m2[key], value);
|
deba@1421
|
69 |
}
|
deba@1421
|
70 |
|
deba@1421
|
71 |
private:
|
deba@1421
|
72 |
|
deba@1421
|
73 |
typename SmartReference<M1>::Type m1;
|
deba@1421
|
74 |
typename SmartConstReference<M2>::Type m2;
|
deba@1421
|
75 |
|
deba@1421
|
76 |
};
|
deba@1421
|
77 |
|
deba@1421
|
78 |
template <typename M1, typename M2>
|
deba@1421
|
79 |
WriteComposeMap<M1, M2> writeComposeMap(M1& m1, const M2& m2) {
|
deba@1421
|
80 |
return WriteComposeMap<M1, M2>(m1, m2);
|
deba@1421
|
81 |
}
|
deba@1421
|
82 |
|
deba@1421
|
83 |
template <typename M1, typename M2>
|
deba@1421
|
84 |
WriteComposeMap<M1, M2> writeComposeMap(const M1& m1, const M2& m2) {
|
deba@1421
|
85 |
return WriteComposeMap<M1, M2>(m1, m2);
|
deba@1421
|
86 |
}
|
deba@1421
|
87 |
|
deba@1421
|
88 |
}
|
deba@1421
|
89 |
|
deba@1409
|
90 |
/// \ingroup io_group
|
deba@1408
|
91 |
/// \brief Lemon Format reader class.
|
deba@1408
|
92 |
///
|
deba@1409
|
93 |
/// The Lemon Format contains several sections. We do not want to
|
deba@1409
|
94 |
/// determine what sections are in a lemon file we give only a framework
|
deba@1409
|
95 |
/// to read a section oriented format.
|
deba@1409
|
96 |
///
|
deba@1409
|
97 |
/// In the Lemon Format each section starts with a line contains a \c \@
|
deba@1409
|
98 |
/// character on the first not white space position. This line is the
|
deba@1409
|
99 |
/// header line of the section. Each next lines belong to this section
|
deba@1409
|
100 |
/// while it does not starts with \c \@ character. This line can start a
|
deba@1409
|
101 |
/// new section or if it can close the file with the \c \@end line.
|
deba@1427
|
102 |
/// The file format ignore the empty and comment lines. The line is
|
deba@1427
|
103 |
/// comment line if it starts with a \c # character.
|
deba@1409
|
104 |
///
|
deba@1409
|
105 |
/// The framework provides an abstract LemonReader::SectionReader class
|
deba@1409
|
106 |
/// what defines the interface of a SectionReader. The SectionReader
|
deba@1409
|
107 |
/// has the \c header() member function what get a header line string and
|
deba@1409
|
108 |
/// decides if it want to process the next section. Several SectionReaders
|
deba@1409
|
109 |
/// can be attached to an LemonReader and the first attached what can
|
deba@1409
|
110 |
/// process the section will be used. Its \c read() member will called
|
deba@1427
|
111 |
/// with a stream contains the section. From this stream the empty and
|
deba@1427
|
112 |
/// comment lines are filtered out.
|
deba@1409
|
113 |
///
|
deba@1409
|
114 |
/// \relates GraphReader
|
deba@1409
|
115 |
/// \relates NodeSetReader
|
deba@1409
|
116 |
/// \relates EdgeSetReader
|
deba@1409
|
117 |
/// \relates NodesReader
|
deba@1409
|
118 |
/// \relates EdgesReader
|
deba@1409
|
119 |
/// \relates AttributeReader
|
deba@1408
|
120 |
class LemonReader {
|
deba@1408
|
121 |
private:
|
deba@1408
|
122 |
|
deba@1408
|
123 |
class FilterStreamBuf : public std::streambuf {
|
deba@1408
|
124 |
public:
|
deba@1408
|
125 |
|
deba@1408
|
126 |
typedef std::streambuf Parent;
|
deba@1408
|
127 |
typedef Parent::char_type char_type;
|
deba@1408
|
128 |
FilterStreamBuf(std::istream& is, int& num)
|
deba@1408
|
129 |
: _is(is), _base(0), _eptr(0),
|
deba@1408
|
130 |
_num(num), skip_state(after_endl) {}
|
deba@1408
|
131 |
|
deba@1408
|
132 |
protected:
|
deba@1408
|
133 |
|
deba@1408
|
134 |
enum skip_state_type {
|
deba@1408
|
135 |
no_skip,
|
deba@1408
|
136 |
after_endl,
|
deba@1427
|
137 |
comment_line
|
deba@1408
|
138 |
};
|
deba@1408
|
139 |
|
deba@1408
|
140 |
char_type small_buf[1];
|
deba@1408
|
141 |
|
deba@1408
|
142 |
|
deba@1408
|
143 |
std::istream& _is;
|
deba@1408
|
144 |
|
deba@1408
|
145 |
char_type* _base;
|
deba@1408
|
146 |
char_type* _eptr;
|
deba@1408
|
147 |
|
deba@1408
|
148 |
int& _num;
|
deba@1408
|
149 |
|
deba@1408
|
150 |
skip_state_type skip_state;
|
deba@1408
|
151 |
|
deba@1408
|
152 |
|
deba@1408
|
153 |
char_type* base() { return _base; }
|
deba@1408
|
154 |
|
deba@1408
|
155 |
char_type* eptr() { return _eptr; }
|
deba@1408
|
156 |
|
deba@1408
|
157 |
int blen() { return _eptr - _base; }
|
deba@1408
|
158 |
|
deba@1408
|
159 |
void setb(char_type* buf, int len) {
|
deba@1408
|
160 |
_base = buf;
|
deba@1408
|
161 |
_eptr = buf + len;
|
deba@1408
|
162 |
}
|
deba@1408
|
163 |
|
deba@1408
|
164 |
virtual std::streambuf* setbuf(char *buf, int len) {
|
deba@1408
|
165 |
if (base()) return 0;
|
deba@1408
|
166 |
if (buf != 0 && len >= (int)sizeof(small_buf)) {
|
deba@1408
|
167 |
setb(buf, len);
|
deba@1408
|
168 |
} else {
|
deba@1408
|
169 |
setb(small_buf, sizeof(small_buf));
|
deba@1408
|
170 |
}
|
deba@1408
|
171 |
setg(0, 0, 0);
|
deba@1408
|
172 |
return this;
|
deba@1408
|
173 |
}
|
deba@1408
|
174 |
|
deba@1408
|
175 |
bool put_char(char c) {
|
deba@1408
|
176 |
switch (skip_state) {
|
deba@1408
|
177 |
case no_skip:
|
deba@1408
|
178 |
switch (c) {
|
deba@1408
|
179 |
case '\n':
|
deba@1408
|
180 |
skip_state = after_endl;
|
deba@1408
|
181 |
return true;
|
deba@1408
|
182 |
default:
|
deba@1408
|
183 |
return true;
|
deba@1408
|
184 |
}
|
deba@1408
|
185 |
case after_endl:
|
deba@1408
|
186 |
switch (c) {
|
deba@1408
|
187 |
case '@':
|
deba@1408
|
188 |
return false;
|
deba@1408
|
189 |
case '\n':
|
deba@1408
|
190 |
return false;
|
deba@1408
|
191 |
case '#':
|
deba@1427
|
192 |
skip_state = comment_line;
|
deba@1408
|
193 |
return false;
|
deba@1408
|
194 |
default:
|
deba@1408
|
195 |
if (!isspace(c)) {
|
deba@1408
|
196 |
skip_state = no_skip;
|
deba@1408
|
197 |
return true;
|
deba@1408
|
198 |
} else {
|
deba@1408
|
199 |
return false;
|
deba@1408
|
200 |
}
|
deba@1408
|
201 |
}
|
deba@1408
|
202 |
break;
|
deba@1427
|
203 |
case comment_line:
|
deba@1408
|
204 |
switch (c) {
|
deba@1408
|
205 |
case '\n':
|
deba@1408
|
206 |
skip_state = after_endl;
|
deba@1408
|
207 |
return false;
|
deba@1408
|
208 |
default:
|
deba@1408
|
209 |
return false;
|
deba@1408
|
210 |
}
|
deba@1408
|
211 |
}
|
deba@1408
|
212 |
return false;
|
deba@1408
|
213 |
}
|
deba@1408
|
214 |
|
deba@1408
|
215 |
virtual int underflow() {
|
deba@1408
|
216 |
char c;
|
deba@1408
|
217 |
if (_is.read(&c, 1)) {
|
deba@1408
|
218 |
_is.putback(c);
|
deba@1408
|
219 |
if (c == '@') {
|
deba@1408
|
220 |
return EOF;
|
deba@1408
|
221 |
}
|
deba@1408
|
222 |
} else {
|
deba@1408
|
223 |
return EOF;
|
deba@1408
|
224 |
}
|
deba@1408
|
225 |
char_type *ptr;
|
deba@1408
|
226 |
for (ptr = base(); ptr != eptr(); ++ptr) {
|
deba@1408
|
227 |
if (_is.read(&c, 1)) {
|
deba@1408
|
228 |
if (c == '\n') ++_num;
|
deba@1408
|
229 |
if (put_char(c)) {
|
deba@1408
|
230 |
*ptr = c;
|
deba@1408
|
231 |
} else {
|
deba@1408
|
232 |
if (skip_state == after_endl && c == '@') {
|
deba@1408
|
233 |
_is.putback('@');
|
deba@1408
|
234 |
break;
|
deba@1408
|
235 |
}
|
deba@1408
|
236 |
--ptr;
|
deba@1408
|
237 |
}
|
deba@1408
|
238 |
} else {
|
deba@1408
|
239 |
break;
|
deba@1408
|
240 |
}
|
deba@1408
|
241 |
}
|
deba@1408
|
242 |
setg(base(), base(), ptr);
|
deba@1408
|
243 |
return *base();
|
deba@1408
|
244 |
}
|
deba@1408
|
245 |
|
deba@1408
|
246 |
virtual int sync() {
|
deba@1408
|
247 |
return EOF;
|
deba@1408
|
248 |
}
|
deba@1408
|
249 |
};
|
deba@1408
|
250 |
|
deba@1408
|
251 |
public:
|
deba@1408
|
252 |
|
deba@1409
|
253 |
/// \brief Abstract base class for reading a section.
|
deba@1409
|
254 |
///
|
deba@1409
|
255 |
/// This class has an \c header() member function what get a
|
deba@1409
|
256 |
/// header line string and decides if it want to process the next
|
deba@1409
|
257 |
/// section. Several SectionReaders can be attached to an LemonReader
|
deba@1409
|
258 |
/// and the first attached what can process the section will be used.
|
deba@1409
|
259 |
/// Its \c read() member will called with a stream contains the section.
|
deba@1409
|
260 |
/// From this stream the empty lines and comments are filtered out.
|
deba@1408
|
261 |
class SectionReader {
|
deba@1409
|
262 |
friend class LemonReader;
|
deba@1409
|
263 |
protected:
|
deba@1409
|
264 |
/// \brief Constructor for SectionReader.
|
deba@1409
|
265 |
///
|
deba@1409
|
266 |
/// Constructor for SectionReader. It attach this reader to
|
deba@1409
|
267 |
/// the given LemonReader.
|
deba@1409
|
268 |
SectionReader(LemonReader& reader) {
|
deba@1409
|
269 |
reader.attach(*this);
|
deba@1409
|
270 |
}
|
deba@1409
|
271 |
|
deba@1409
|
272 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
273 |
/// the section with the given header line.
|
deba@1409
|
274 |
///
|
deba@1409
|
275 |
/// It gives back true when the SectionReader can process
|
deba@1409
|
276 |
/// the section with the given header line.
|
deba@1408
|
277 |
virtual bool header(const std::string& line) = 0;
|
deba@1409
|
278 |
|
deba@1409
|
279 |
/// \brief Reader function of the section.
|
deba@1409
|
280 |
///
|
deba@1409
|
281 |
/// It reads the content of the section.
|
deba@1408
|
282 |
virtual void read(std::istream& is) = 0;
|
deba@1408
|
283 |
};
|
deba@1408
|
284 |
|
deba@1409
|
285 |
/// \brief Constructor for LemonReader.
|
deba@1409
|
286 |
///
|
deba@1409
|
287 |
/// Constructor for LemonReader which reads from the given stream.
|
deba@1408
|
288 |
LemonReader(std::istream& _is)
|
deba@1408
|
289 |
: is(&_is), own_is(false) {}
|
deba@1408
|
290 |
|
deba@1409
|
291 |
/// \brief Constructor for LemonReader.
|
deba@1409
|
292 |
///
|
deba@1409
|
293 |
/// Constructor for LemonReader which reads from the given file.
|
deba@1408
|
294 |
LemonReader(const std::string& filename)
|
deba@1408
|
295 |
: is(0), own_is(true) {
|
deba@1408
|
296 |
is = new std::ifstream(filename.c_str());
|
deba@1408
|
297 |
}
|
deba@1408
|
298 |
|
deba@1409
|
299 |
/// \brief Desctructor for LemonReader.
|
deba@1409
|
300 |
///
|
deba@1409
|
301 |
/// Desctructor for LemonReader.
|
deba@1408
|
302 |
~LemonReader() {
|
deba@1408
|
303 |
if (own_is) {
|
deba@1408
|
304 |
delete is;
|
deba@1408
|
305 |
}
|
deba@1408
|
306 |
}
|
deba@1408
|
307 |
|
deba@1408
|
308 |
private:
|
deba@1408
|
309 |
LemonReader(const LemonReader&);
|
deba@1408
|
310 |
void operator=(const LemonReader&);
|
deba@1408
|
311 |
|
deba@1408
|
312 |
void attach(SectionReader& reader) {
|
deba@1408
|
313 |
readers.push_back(&reader);
|
deba@1408
|
314 |
}
|
deba@1408
|
315 |
|
deba@1409
|
316 |
public:
|
deba@1409
|
317 |
/// \brief Executes the LemonReader.
|
deba@1409
|
318 |
///
|
deba@1409
|
319 |
/// It executes the LemonReader.
|
deba@1408
|
320 |
void run() {
|
deba@1408
|
321 |
int line_num = 0;
|
deba@1408
|
322 |
std::string line;
|
deba@1408
|
323 |
try {
|
deba@1408
|
324 |
while ((++line_num, getline(*is, line)) && line.find("@end") != 0) {
|
deba@1408
|
325 |
SectionReaders::iterator it;
|
deba@1408
|
326 |
for (it = readers.begin(); it != readers.end(); ++it) {
|
deba@1408
|
327 |
if ((*it)->header(line)) {
|
deba@1408
|
328 |
char buf[2048];
|
deba@1408
|
329 |
FilterStreamBuf buffer(*is, line_num);
|
deba@1408
|
330 |
buffer.pubsetbuf(buf, sizeof(buf));
|
deba@1408
|
331 |
std::istream is(&buffer);
|
deba@1408
|
332 |
(*it)->read(is);
|
deba@1408
|
333 |
break;
|
deba@1408
|
334 |
}
|
deba@1408
|
335 |
}
|
deba@1408
|
336 |
}
|
deba@1408
|
337 |
} catch (DataFormatError& error) {
|
deba@1408
|
338 |
error.line(line_num);
|
deba@1408
|
339 |
throw error;
|
deba@1408
|
340 |
}
|
deba@1408
|
341 |
}
|
deba@1408
|
342 |
|
deba@1408
|
343 |
|
deba@1408
|
344 |
private:
|
deba@1408
|
345 |
|
deba@1408
|
346 |
std::istream* is;
|
deba@1408
|
347 |
bool own_is;
|
deba@1408
|
348 |
|
deba@1408
|
349 |
typedef std::vector<SectionReader*> SectionReaders;
|
deba@1408
|
350 |
SectionReaders readers;
|
deba@1408
|
351 |
|
deba@1408
|
352 |
};
|
deba@1408
|
353 |
|
deba@1409
|
354 |
/// \brief Helper class for implementing the common SectionReaders.
|
deba@1409
|
355 |
///
|
deba@1409
|
356 |
/// Helper class for implementing the common SectionReaders.
|
deba@1409
|
357 |
class CommonSectionReaderBase : public LemonReader::SectionReader {
|
deba@1409
|
358 |
typedef LemonReader::SectionReader Parent;
|
deba@1409
|
359 |
protected:
|
deba@1409
|
360 |
|
deba@1409
|
361 |
/// \brief Constructor for CommonSectionReaderBase.
|
deba@1409
|
362 |
///
|
deba@1409
|
363 |
/// Constructor for CommonSectionReaderBase. It attach this reader to
|
deba@1409
|
364 |
/// the given LemonReader.
|
deba@1409
|
365 |
CommonSectionReaderBase(LemonReader& _reader)
|
deba@1409
|
366 |
: Parent(_reader) {}
|
deba@1408
|
367 |
|
deba@1408
|
368 |
template <typename _Item>
|
deba@1408
|
369 |
class ReaderBase;
|
deba@1408
|
370 |
|
deba@1408
|
371 |
template <typename _Item>
|
deba@1408
|
372 |
class InverterBase : public ReaderBase<_Item> {
|
deba@1408
|
373 |
public:
|
deba@1408
|
374 |
typedef _Item Item;
|
deba@1408
|
375 |
virtual void read(std::istream&, const Item&) = 0;
|
deba@1408
|
376 |
virtual Item read(std::istream&) const = 0;
|
deba@1408
|
377 |
|
deba@1408
|
378 |
virtual InverterBase<_Item>* getInverter() {
|
deba@1408
|
379 |
return this;
|
deba@1408
|
380 |
}
|
deba@1408
|
381 |
};
|
deba@1408
|
382 |
|
deba@1408
|
383 |
template <typename _Item, typename _Map, typename _Reader>
|
deba@1408
|
384 |
class MapReaderInverter : public InverterBase<_Item> {
|
deba@1408
|
385 |
public:
|
deba@1408
|
386 |
typedef _Item Item;
|
deba@1408
|
387 |
typedef _Reader Reader;
|
deba@1408
|
388 |
typedef typename Reader::Value Value;
|
deba@1408
|
389 |
typedef _Map Map;
|
deba@1424
|
390 |
typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
|
deba@1408
|
391 |
|
deba@1421
|
392 |
typename SmartReference<Map>::Type map;
|
deba@1408
|
393 |
Reader reader;
|
deba@1408
|
394 |
Inverse inverse;
|
deba@1408
|
395 |
|
deba@1421
|
396 |
MapReaderInverter(typename SmartParameter<Map>::Type _map,
|
deba@1421
|
397 |
const Reader& _reader)
|
deba@1408
|
398 |
: map(_map), reader(_reader) {}
|
deba@1408
|
399 |
|
deba@1408
|
400 |
virtual ~MapReaderInverter() {}
|
deba@1408
|
401 |
|
deba@1408
|
402 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1408
|
403 |
Value value;
|
deba@1408
|
404 |
reader.read(is, value);
|
deba@1408
|
405 |
map.set(item, value);
|
deba@1408
|
406 |
typename Inverse::iterator it = inverse.find(value);
|
deba@1408
|
407 |
if (it == inverse.end()) {
|
deba@1408
|
408 |
inverse.insert(std::make_pair(value, item));
|
deba@1408
|
409 |
} else {
|
deba@1408
|
410 |
throw DataFormatError("Multiple ID occurence");
|
deba@1408
|
411 |
}
|
deba@1408
|
412 |
}
|
deba@1408
|
413 |
|
deba@1408
|
414 |
virtual Item read(std::istream& is) const {
|
deba@1408
|
415 |
Value value;
|
deba@1408
|
416 |
reader.read(is, value);
|
deba@1408
|
417 |
typename Inverse::const_iterator it = inverse.find(value);
|
deba@1408
|
418 |
if (it != inverse.end()) {
|
deba@1408
|
419 |
return it->second;
|
deba@1408
|
420 |
} else {
|
deba@1408
|
421 |
throw DataFormatError("Invalid ID error");
|
deba@1408
|
422 |
}
|
deba@1408
|
423 |
}
|
deba@1408
|
424 |
};
|
deba@1408
|
425 |
|
deba@1408
|
426 |
template <typename _Item, typename _Reader>
|
deba@1408
|
427 |
class SkipReaderInverter : public InverterBase<_Item> {
|
deba@1408
|
428 |
public:
|
deba@1408
|
429 |
typedef _Item Item;
|
deba@1408
|
430 |
typedef _Reader Reader;
|
deba@1408
|
431 |
typedef typename Reader::Value Value;
|
deba@1424
|
432 |
typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
|
deba@1408
|
433 |
|
deba@1408
|
434 |
Reader reader;
|
deba@1408
|
435 |
|
deba@1408
|
436 |
SkipReaderInverter(const Reader& _reader)
|
deba@1408
|
437 |
: reader(_reader) {}
|
deba@1408
|
438 |
|
deba@1408
|
439 |
virtual ~SkipReaderInverter() {}
|
deba@1408
|
440 |
|
deba@1408
|
441 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1408
|
442 |
Value value;
|
deba@1408
|
443 |
reader.read(is, value);
|
deba@1408
|
444 |
typename Inverse::iterator it = inverse.find(value);
|
deba@1408
|
445 |
if (it == inverse.end()) {
|
deba@1408
|
446 |
inverse.insert(std::make_pair(value, item));
|
deba@1408
|
447 |
} else {
|
deba@1408
|
448 |
throw DataFormatError("Multiple ID occurence error");
|
deba@1408
|
449 |
}
|
deba@1408
|
450 |
}
|
deba@1408
|
451 |
|
deba@1408
|
452 |
virtual Item read(std::istream& is) const {
|
deba@1408
|
453 |
Value value;
|
deba@1408
|
454 |
reader.read(is, value);
|
deba@1408
|
455 |
typename Inverse::const_iterator it = inverse.find(value);
|
deba@1408
|
456 |
if (it != inverse.end()) {
|
deba@1408
|
457 |
return it->second;
|
deba@1408
|
458 |
} else {
|
deba@1408
|
459 |
throw DataFormatError("Invalid ID error");
|
deba@1408
|
460 |
}
|
deba@1408
|
461 |
}
|
deba@1408
|
462 |
|
deba@1408
|
463 |
private:
|
deba@1408
|
464 |
Inverse inverse;
|
deba@1408
|
465 |
};
|
deba@1408
|
466 |
|
deba@1408
|
467 |
template <typename _Item>
|
deba@1408
|
468 |
class ReaderBase {
|
deba@1408
|
469 |
public:
|
deba@1408
|
470 |
typedef _Item Item;
|
deba@1408
|
471 |
|
deba@1408
|
472 |
virtual ~ReaderBase() {}
|
deba@1408
|
473 |
|
deba@1408
|
474 |
virtual void read(std::istream& is, const Item& item) = 0;
|
deba@1408
|
475 |
virtual InverterBase<_Item>* getInverter() = 0;
|
deba@1408
|
476 |
};
|
deba@1408
|
477 |
|
deba@1408
|
478 |
template <typename _Item, typename _Map, typename _Reader>
|
deba@1408
|
479 |
class MapReader : public ReaderBase<_Item> {
|
deba@1408
|
480 |
public:
|
deba@1408
|
481 |
typedef _Map Map;
|
deba@1408
|
482 |
typedef _Reader Reader;
|
deba@1408
|
483 |
typedef typename Reader::Value Value;
|
deba@1408
|
484 |
typedef _Item Item;
|
deba@1408
|
485 |
|
deba@1421
|
486 |
typename SmartReference<Map>::Type map;
|
deba@1408
|
487 |
Reader reader;
|
deba@1408
|
488 |
|
deba@1421
|
489 |
MapReader(typename SmartParameter<Map>::Type _map,
|
deba@1421
|
490 |
const Reader& _reader)
|
deba@1408
|
491 |
: map(_map), reader(_reader) {}
|
deba@1408
|
492 |
|
deba@1408
|
493 |
virtual ~MapReader() {}
|
deba@1408
|
494 |
|
deba@1408
|
495 |
virtual void read(std::istream& is, const Item& item) {
|
deba@1408
|
496 |
Value value;
|
deba@1408
|
497 |
reader.read(is, value);
|
deba@1408
|
498 |
map.set(item, value);
|
deba@1408
|
499 |
}
|
deba@1408
|
500 |
|
deba@1408
|
501 |
virtual InverterBase<_Item>* getInverter() {
|
deba@1408
|
502 |
return new MapReaderInverter<Item, Map, Reader>(map, reader);
|
deba@1408
|
503 |
}
|
deba@1408
|
504 |
};
|
deba@1408
|
505 |
|
deba@1408
|
506 |
|
deba@1408
|
507 |
template <typename _Item, typename _Reader>
|
deba@1408
|
508 |
class SkipReader : public ReaderBase<_Item> {
|
deba@1408
|
509 |
public:
|
deba@1408
|
510 |
typedef _Reader Reader;
|
deba@1408
|
511 |
typedef typename Reader::Value Value;
|
deba@1408
|
512 |
typedef _Item Item;
|
deba@1408
|
513 |
|
deba@1408
|
514 |
Reader reader;
|
deba@1408
|
515 |
SkipReader(const Reader& _reader) : reader(_reader) {}
|
deba@1408
|
516 |
|
deba@1408
|
517 |
virtual ~SkipReader() {}
|
deba@1408
|
518 |
|
deba@1408
|
519 |
virtual void read(std::istream& is, const Item&) {
|
deba@1408
|
520 |
Value value;
|
deba@1408
|
521 |
reader.read(is, value);
|
deba@1408
|
522 |
}
|
deba@1408
|
523 |
|
deba@1408
|
524 |
virtual InverterBase<Item>* getInverter() {
|
deba@1408
|
525 |
return new SkipReaderInverter<Item, Reader>(reader);
|
deba@1408
|
526 |
}
|
deba@1408
|
527 |
};
|
deba@1408
|
528 |
|
deba@1408
|
529 |
template <typename _Item>
|
deba@1409
|
530 |
class IdReaderBase {
|
deba@1408
|
531 |
public:
|
deba@1408
|
532 |
typedef _Item Item;
|
deba@1409
|
533 |
virtual Item read(std::istream& is) const = 0;
|
deba@1408
|
534 |
};
|
deba@1408
|
535 |
|
deba@1409
|
536 |
template <typename _Item, typename _BoxedIdReader>
|
deba@1409
|
537 |
class IdReader : public IdReaderBase<_Item> {
|
deba@1408
|
538 |
public:
|
deba@1408
|
539 |
typedef _Item Item;
|
deba@1409
|
540 |
typedef _BoxedIdReader BoxedIdReader;
|
deba@1409
|
541 |
|
deba@1409
|
542 |
const BoxedIdReader& boxedIdReader;
|
deba@1408
|
543 |
|
deba@1409
|
544 |
IdReader(const BoxedIdReader& _boxedIdReader)
|
deba@1409
|
545 |
: boxedIdReader(_boxedIdReader) {}
|
deba@1408
|
546 |
|
deba@1409
|
547 |
virtual Item read(std::istream& is) const {
|
deba@1409
|
548 |
return boxedIdReader.readId(is);
|
deba@1408
|
549 |
}
|
deba@1408
|
550 |
};
|
deba@1408
|
551 |
|
deba@1408
|
552 |
class ValueReaderBase {
|
deba@1408
|
553 |
public:
|
deba@1408
|
554 |
virtual void read(std::istream&) {};
|
deba@1408
|
555 |
};
|
deba@1408
|
556 |
|
deba@1408
|
557 |
template <typename _Value, typename _Reader>
|
deba@1408
|
558 |
class ValueReader : public ValueReaderBase {
|
deba@1408
|
559 |
public:
|
deba@1408
|
560 |
typedef _Value Value;
|
deba@1408
|
561 |
typedef _Reader Reader;
|
deba@1408
|
562 |
|
deba@1408
|
563 |
ValueReader(Value& _value, const Reader& _reader)
|
deba@1408
|
564 |
: value(_value), reader(_reader) {}
|
deba@1408
|
565 |
|
deba@1408
|
566 |
virtual void read(std::istream& is) {
|
deba@1408
|
567 |
reader.read(is, value);
|
deba@1408
|
568 |
}
|
deba@1408
|
569 |
private:
|
deba@1408
|
570 |
Value& value;
|
deba@1408
|
571 |
Reader reader;
|
deba@1408
|
572 |
};
|
deba@1408
|
573 |
|
deba@1408
|
574 |
};
|
deba@1408
|
575 |
|
deba@1409
|
576 |
/// \ingroup io_group
|
deba@1409
|
577 |
/// \brief SectionReader for reading a graph's nodeset.
|
deba@1409
|
578 |
///
|
deba@1409
|
579 |
/// The lemon format can store multiple graph nodesets with several maps.
|
deba@1409
|
580 |
/// The nodeset section's header line is \c \@nodeset \c nodeset_id, but the
|
deba@1409
|
581 |
/// \c nodeset_id may be empty.
|
deba@1409
|
582 |
///
|
deba@1409
|
583 |
/// The first line of the section contains the names of the maps separated
|
deba@1409
|
584 |
/// with white spaces. Each next lines describes a node in the nodeset, and
|
deba@1409
|
585 |
/// contains the mapped values for each map.
|
deba@1409
|
586 |
///
|
deba@1409
|
587 |
/// If the nodeset contains an \c "id" named map then it will be regarded
|
deba@1409
|
588 |
/// as id map. This map should contain only unique values and when the
|
deba@1409
|
589 |
/// \c readId() member will read a value from the given stream it will
|
deba@1409
|
590 |
/// give back that node which is mapped to this value.
|
deba@1409
|
591 |
///
|
deba@1409
|
592 |
/// \relates LemonReader
|
deba@1408
|
593 |
template <typename _Graph, typename _Traits = DefaultReaderTraits>
|
deba@1408
|
594 |
class NodeSetReader : public CommonSectionReaderBase {
|
deba@1408
|
595 |
typedef CommonSectionReaderBase Parent;
|
deba@1408
|
596 |
public:
|
deba@1408
|
597 |
|
deba@1408
|
598 |
typedef _Graph Graph;
|
deba@1408
|
599 |
typedef _Traits Traits;
|
deba@1408
|
600 |
typedef typename Graph::Node Item;
|
deba@1408
|
601 |
typedef typename Traits::Skipper DefaultSkipper;
|
deba@1408
|
602 |
|
deba@1409
|
603 |
/// \brief Constructor.
|
deba@1409
|
604 |
///
|
deba@1409
|
605 |
/// Constructor for NodeSetReader. It creates the NodeSetReader and
|
deba@1409
|
606 |
/// attach it into the given LemonReader. The nodeset reader will
|
deba@1409
|
607 |
/// add the readed nodes to the given Graph. The reader will read
|
deba@1409
|
608 |
/// the section when the \c section_id and the \c _id are the same.
|
deba@1421
|
609 |
NodeSetReader(LemonReader& _reader,
|
deba@1421
|
610 |
typename SmartParameter<Graph>::Type _graph,
|
deba@1408
|
611 |
const std::string& _id = std::string(),
|
deba@1409
|
612 |
const DefaultSkipper& _skipper = DefaultSkipper())
|
deba@1409
|
613 |
: Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {}
|
deba@1408
|
614 |
|
deba@1409
|
615 |
|
deba@1409
|
616 |
/// \brief Destructor.
|
deba@1409
|
617 |
///
|
deba@1409
|
618 |
/// Destructor for NodeSetReader.
|
deba@1408
|
619 |
virtual ~NodeSetReader() {
|
deba@1408
|
620 |
for (typename MapReaders::iterator it = readers.begin();
|
deba@1408
|
621 |
it != readers.end(); ++it) {
|
deba@1408
|
622 |
delete it->second;
|
deba@1408
|
623 |
}
|
deba@1408
|
624 |
}
|
deba@1408
|
625 |
|
deba@1408
|
626 |
private:
|
deba@1408
|
627 |
NodeSetReader(const NodeSetReader&);
|
deba@1408
|
628 |
void operator=(const NodeSetReader&);
|
deba@1408
|
629 |
|
deba@1408
|
630 |
public:
|
deba@1408
|
631 |
|
deba@1408
|
632 |
/// \brief Add a new node map reader command for the reader.
|
deba@1408
|
633 |
///
|
deba@1408
|
634 |
/// Add a new node map reader command for the reader.
|
deba@1408
|
635 |
template <typename Map>
|
deba@1421
|
636 |
NodeSetReader& readNodeMap(std::string name, Map& map) {
|
deba@1421
|
637 |
return _readMap<
|
deba@1421
|
638 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
639 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
640 |
}
|
deba@1421
|
641 |
|
deba@1421
|
642 |
template <typename Map>
|
deba@1421
|
643 |
NodeSetReader& readNodeMap(std::string name, const Map& map) {
|
deba@1421
|
644 |
return _readMap<
|
deba@1421
|
645 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
646 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1408
|
647 |
}
|
deba@1408
|
648 |
|
deba@1408
|
649 |
/// \brief Add a new node map reader command for the reader.
|
deba@1408
|
650 |
///
|
deba@1408
|
651 |
/// Add a new node map reader command for the reader.
|
deba@1408
|
652 |
template <typename Reader, typename Map>
|
deba@1421
|
653 |
NodeSetReader& readNodeMap(std::string name, Map& map,
|
deba@1421
|
654 |
const Reader& reader = Reader()) {
|
deba@1421
|
655 |
return _readMap<
|
deba@1421
|
656 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
657 |
typename SmartParameter<Map>::Type>(name, map, reader);
|
deba@1421
|
658 |
}
|
deba@1421
|
659 |
|
deba@1421
|
660 |
template <typename Reader, typename Map>
|
deba@1421
|
661 |
NodeSetReader& readNodeMap(std::string name, const Map& map,
|
deba@1421
|
662 |
const Reader& reader = Reader()) {
|
deba@1421
|
663 |
return _readMap<
|
deba@1421
|
664 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
665 |
typename SmartParameter<Map>::Type>(name, map, reader);
|
deba@1421
|
666 |
}
|
deba@1421
|
667 |
|
deba@1421
|
668 |
private:
|
deba@1421
|
669 |
|
deba@1421
|
670 |
template <typename Reader, typename Map, typename MapParameter>
|
deba@1421
|
671 |
NodeSetReader& _readMap(std::string name, MapParameter map,
|
deba@1421
|
672 |
const Reader& reader = Reader()) {
|
deba@1408
|
673 |
if (readers.find(name) != readers.end()) {
|
deba@1408
|
674 |
ErrorMessage msg;
|
deba@1408
|
675 |
msg << "Multiple read rule for node map: " << name;
|
deba@1408
|
676 |
throw IOParameterError(msg.message());
|
deba@1408
|
677 |
}
|
deba@1408
|
678 |
readers.insert(
|
deba@1408
|
679 |
make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
|
deba@1408
|
680 |
return *this;
|
deba@1408
|
681 |
}
|
deba@1408
|
682 |
|
deba@1421
|
683 |
public:
|
deba@1421
|
684 |
|
deba@1408
|
685 |
/// \brief Add a new node map skipper command for the reader.
|
deba@1408
|
686 |
///
|
deba@1408
|
687 |
/// Add a new node map skipper command for the reader.
|
deba@1408
|
688 |
template <typename Reader>
|
deba@1421
|
689 |
NodeSetReader& skipNodeMap(std::string name,
|
deba@1408
|
690 |
const Reader& reader = Reader()) {
|
deba@1408
|
691 |
if (readers.find(name) != readers.end()) {
|
deba@1408
|
692 |
ErrorMessage msg;
|
deba@1408
|
693 |
msg << "Multiple read rule for node map: " << name;
|
deba@1408
|
694 |
throw IOParameterError(msg.message());
|
deba@1408
|
695 |
}
|
deba@1408
|
696 |
readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
|
deba@1408
|
697 |
return *this;
|
deba@1408
|
698 |
}
|
deba@1408
|
699 |
|
deba@1409
|
700 |
protected:
|
deba@1409
|
701 |
|
deba@1409
|
702 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
703 |
/// the section with the given header line.
|
deba@1409
|
704 |
///
|
deba@1421
|
705 |
/// It gives back true when the header line starts with \c \@nodeset,
|
deba@1409
|
706 |
/// and the header line's id and the nodeset's id are the same.
|
deba@1408
|
707 |
virtual bool header(const std::string& line) {
|
deba@1408
|
708 |
std::istringstream ls(line);
|
deba@1408
|
709 |
std::string command;
|
deba@1408
|
710 |
std::string name;
|
deba@1408
|
711 |
ls >> command >> name;
|
deba@1408
|
712 |
return command == "@nodeset" && name == id;
|
deba@1408
|
713 |
}
|
deba@1408
|
714 |
|
deba@1409
|
715 |
/// \brief Reader function of the section.
|
deba@1409
|
716 |
///
|
deba@1409
|
717 |
/// It reads the content of the section.
|
deba@1408
|
718 |
virtual void read(std::istream& is) {
|
deba@1408
|
719 |
std::vector<ReaderBase<Item>* > index;
|
deba@1408
|
720 |
std::string line;
|
deba@1408
|
721 |
|
deba@1408
|
722 |
getline(is, line);
|
deba@1408
|
723 |
std::istringstream ls(line);
|
deba@1408
|
724 |
while (ls >> id) {
|
deba@1408
|
725 |
typename MapReaders::iterator it = readers.find(id);
|
deba@1408
|
726 |
if (it != readers.end()) {
|
deba@1408
|
727 |
index.push_back(it->second);
|
deba@1408
|
728 |
} else {
|
deba@1408
|
729 |
index.push_back(&skipper);
|
deba@1408
|
730 |
}
|
deba@1408
|
731 |
if (id == "id" && inverter.get() == 0) {
|
deba@1408
|
732 |
inverter.reset(index.back()->getInverter());
|
deba@1408
|
733 |
index.back() = inverter.get();
|
deba@1408
|
734 |
}
|
deba@1408
|
735 |
}
|
deba@1408
|
736 |
while (getline(is, line)) {
|
deba@1408
|
737 |
typename Graph::Node node = graph.addNode();
|
deba@1408
|
738 |
std::istringstream ls(line);
|
deba@1408
|
739 |
for (int i = 0; i < (int)index.size(); ++i) {
|
deba@1408
|
740 |
index[i]->read(ls, node);
|
deba@1408
|
741 |
}
|
deba@1408
|
742 |
}
|
deba@1408
|
743 |
}
|
deba@1408
|
744 |
|
deba@1409
|
745 |
public:
|
deba@1409
|
746 |
|
deba@1409
|
747 |
/// \brief Returns true if the nodeset can give back the node by its id.
|
deba@1409
|
748 |
///
|
deba@1409
|
749 |
/// Returns true if the nodeset can give back the node by its id.
|
deba@1409
|
750 |
/// It is possible only if an "id" named map was read.
|
deba@1409
|
751 |
bool isIdReader() const {
|
deba@1408
|
752 |
return inverter.get() != 0;
|
deba@1408
|
753 |
}
|
deba@1408
|
754 |
|
deba@1409
|
755 |
/// \brief Gives back the node by its id.
|
deba@1409
|
756 |
///
|
deba@1409
|
757 |
/// It reads an id from the stream and gives back which node belongs to
|
deba@1409
|
758 |
/// it. It is possible only if there was read an "id" named map.
|
deba@1421
|
759 |
Item readId(std::istream& is) const {
|
deba@1408
|
760 |
return inverter->read(is);
|
deba@1408
|
761 |
}
|
deba@1408
|
762 |
|
deba@1408
|
763 |
private:
|
deba@1408
|
764 |
|
deba@1408
|
765 |
typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
|
deba@1408
|
766 |
MapReaders readers;
|
deba@1408
|
767 |
|
deba@1421
|
768 |
typename SmartReference<Graph>::Type graph;
|
deba@1408
|
769 |
std::string id;
|
deba@1408
|
770 |
SkipReader<Item, DefaultSkipper> skipper;
|
deba@1408
|
771 |
|
deba@1408
|
772 |
std::auto_ptr<InverterBase<Item> > inverter;
|
deba@1408
|
773 |
};
|
deba@1408
|
774 |
|
deba@1409
|
775 |
/// \ingroup io_group
|
deba@1409
|
776 |
/// \brief SectionReader for reading a graph's edgeset.
|
deba@1409
|
777 |
///
|
deba@1409
|
778 |
/// The lemon format can store multiple graph edgesets with several maps.
|
deba@1409
|
779 |
/// The edgeset section's header line is \c \@edgeset \c edgeset_id, but the
|
deba@1409
|
780 |
/// \c edgeset_id may be empty.
|
deba@1409
|
781 |
///
|
deba@1409
|
782 |
/// The first line of the section contains the names of the maps separated
|
deba@1421
|
783 |
/// with white spaces. Each next lines describes an edge in the edgeset. The
|
deba@1421
|
784 |
/// line contains the source and the target nodes' id and the mapped
|
deba@1421
|
785 |
/// values for each map.
|
deba@1409
|
786 |
///
|
deba@1409
|
787 |
/// If the edgeset contains an \c "id" named map then it will be regarded
|
deba@1409
|
788 |
/// as id map. This map should contain only unique values and when the
|
deba@1409
|
789 |
/// \c readId() member will read a value from the given stream it will
|
deba@1409
|
790 |
/// give back that edge which is mapped to this value.
|
deba@1409
|
791 |
///
|
deba@1409
|
792 |
/// The edgeset reader needs a node id reader to identify which nodes
|
deba@1409
|
793 |
/// have to be connected. If a NodeSetReader reads an "id" named map,
|
deba@1409
|
794 |
/// it will be able to resolve the nodes by ids.
|
deba@1409
|
795 |
///
|
deba@1409
|
796 |
/// \relates LemonReader
|
deba@1408
|
797 |
template <typename _Graph, typename _Traits = DefaultReaderTraits>
|
deba@1408
|
798 |
class EdgeSetReader : public CommonSectionReaderBase {
|
deba@1408
|
799 |
typedef CommonSectionReaderBase Parent;
|
deba@1408
|
800 |
public:
|
deba@1408
|
801 |
|
deba@1408
|
802 |
typedef _Graph Graph;
|
deba@1408
|
803 |
typedef _Traits Traits;
|
deba@1408
|
804 |
typedef typename Graph::Edge Item;
|
deba@1408
|
805 |
typedef typename Traits::Skipper DefaultSkipper;
|
deba@1408
|
806 |
|
deba@1409
|
807 |
/// \brief Constructor.
|
deba@1409
|
808 |
///
|
deba@1409
|
809 |
/// Constructor for EdgeSetReader. It creates the EdgeSetReader and
|
deba@1409
|
810 |
/// attach it into the given LemonReader. The edgeset reader will
|
deba@1409
|
811 |
/// add the readed edges to the given Graph. It will use the given
|
deba@1409
|
812 |
/// node id reader to read the source and target nodes of the edges.
|
deba@1409
|
813 |
/// The reader will read the section only if the \c _id and the
|
deba@1409
|
814 |
/// \c edgset_id are the same.
|
deba@1409
|
815 |
template <typename NodeIdReader>
|
deba@1421
|
816 |
EdgeSetReader(LemonReader& _reader,
|
deba@1421
|
817 |
typename SmartParameter<Graph>::Type _graph,
|
deba@1409
|
818 |
const NodeIdReader& _nodeIdReader,
|
deba@1408
|
819 |
const std::string& _id = std::string(),
|
deba@1409
|
820 |
const DefaultSkipper& _skipper = DefaultSkipper())
|
deba@1409
|
821 |
: Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
|
deba@1409
|
822 |
nodeIdReader(new IdReader<typename Graph::Node, NodeIdReader>
|
deba@1409
|
823 |
(_nodeIdReader)) {}
|
deba@1408
|
824 |
|
deba@1409
|
825 |
/// \brief Destructor.
|
deba@1409
|
826 |
///
|
deba@1409
|
827 |
/// Destructor for EdgeSetReader.
|
deba@1408
|
828 |
virtual ~EdgeSetReader() {
|
deba@1408
|
829 |
for (typename MapReaders::iterator it = readers.begin();
|
deba@1408
|
830 |
it != readers.end(); ++it) {
|
deba@1408
|
831 |
delete it->second;
|
deba@1408
|
832 |
}
|
deba@1408
|
833 |
}
|
deba@1408
|
834 |
|
deba@1408
|
835 |
private:
|
deba@1408
|
836 |
EdgeSetReader(const EdgeSetReader&);
|
deba@1408
|
837 |
void operator=(const EdgeSetReader&);
|
deba@1408
|
838 |
|
deba@1408
|
839 |
public:
|
deba@1408
|
840 |
|
deba@1409
|
841 |
/// \brief Add a new edge map reader command for the reader.
|
deba@1408
|
842 |
///
|
deba@1409
|
843 |
/// Add a new edge map reader command for the reader.
|
deba@1408
|
844 |
template <typename Map>
|
deba@1421
|
845 |
EdgeSetReader& readEdgeMap(std::string name, Map& map) {
|
deba@1421
|
846 |
return _readMap<
|
deba@1421
|
847 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
848 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
849 |
}
|
deba@1421
|
850 |
|
deba@1421
|
851 |
template <typename Map>
|
deba@1421
|
852 |
EdgeSetReader& readEdgeMap(std::string name, const Map& map) {
|
deba@1421
|
853 |
return _readMap<
|
deba@1421
|
854 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
855 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1408
|
856 |
}
|
deba@1408
|
857 |
|
deba@1409
|
858 |
/// \brief Add a new edge map reader command for the reader.
|
deba@1408
|
859 |
///
|
deba@1409
|
860 |
/// Add a new edge map reader command for the reader.
|
deba@1408
|
861 |
template <typename Reader, typename Map>
|
deba@1421
|
862 |
EdgeSetReader& readEdgeMap(std::string name, Map& map,
|
deba@1421
|
863 |
const Reader& reader = Reader()) {
|
deba@1421
|
864 |
return _readMap<
|
deba@1421
|
865 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
866 |
typename SmartParameter<Map>::Type>(name, map, reader);
|
deba@1421
|
867 |
}
|
deba@1421
|
868 |
|
deba@1421
|
869 |
template <typename Reader, typename Map>
|
deba@1421
|
870 |
EdgeSetReader& readEdgeMap(std::string name, const Map& map,
|
deba@1421
|
871 |
const Reader& reader = Reader()) {
|
deba@1421
|
872 |
return _readMap<
|
deba@1421
|
873 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
874 |
typename SmartParameter<Map>::Type>(name, map, reader);
|
deba@1421
|
875 |
}
|
deba@1421
|
876 |
|
deba@1421
|
877 |
private:
|
deba@1421
|
878 |
|
deba@1421
|
879 |
template <typename Reader, typename Map, typename MapParameter>
|
deba@1421
|
880 |
EdgeSetReader& _readMap(std::string name, MapParameter map,
|
deba@1421
|
881 |
const Reader& reader = Reader()) {
|
deba@1408
|
882 |
if (readers.find(name) != readers.end()) {
|
deba@1408
|
883 |
ErrorMessage msg;
|
deba@1408
|
884 |
msg << "Multiple read rule for edge map: " << name;
|
deba@1408
|
885 |
throw IOParameterError(msg.message());
|
deba@1408
|
886 |
}
|
deba@1408
|
887 |
readers.insert(
|
deba@1408
|
888 |
make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
|
deba@1408
|
889 |
return *this;
|
deba@1408
|
890 |
}
|
deba@1408
|
891 |
|
deba@1421
|
892 |
public:
|
deba@1421
|
893 |
|
deba@1409
|
894 |
/// \brief Add a new edge map skipper command for the reader.
|
deba@1408
|
895 |
///
|
deba@1409
|
896 |
/// Add a new edge map skipper command for the reader.
|
deba@1408
|
897 |
template <typename Reader>
|
deba@1421
|
898 |
EdgeSetReader& skipEdgeMap(std::string name,
|
deba@1421
|
899 |
const Reader& reader = Reader()) {
|
deba@1408
|
900 |
if (readers.find(name) != readers.end()) {
|
deba@1408
|
901 |
ErrorMessage msg;
|
deba@1421
|
902 |
msg << "Multiple read rule for edge map: " << name;
|
deba@1408
|
903 |
throw IOParameterError(msg.message());
|
deba@1408
|
904 |
}
|
deba@1408
|
905 |
readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
|
deba@1408
|
906 |
return *this;
|
deba@1408
|
907 |
}
|
deba@1408
|
908 |
|
deba@1409
|
909 |
protected:
|
deba@1409
|
910 |
|
deba@1409
|
911 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
912 |
/// the section with the given header line.
|
deba@1409
|
913 |
///
|
deba@1421
|
914 |
/// It gives back true when the header line starts with \c \@edgeset,
|
deba@1409
|
915 |
/// and the header line's id and the edgeset's id are the same.
|
deba@1408
|
916 |
virtual bool header(const std::string& line) {
|
deba@1408
|
917 |
std::istringstream ls(line);
|
deba@1408
|
918 |
std::string command;
|
deba@1408
|
919 |
std::string name;
|
deba@1408
|
920 |
ls >> command >> name;
|
deba@1408
|
921 |
return command == "@edgeset" && name == id;
|
deba@1408
|
922 |
}
|
deba@1408
|
923 |
|
deba@1409
|
924 |
/// \brief Reader function of the section.
|
deba@1409
|
925 |
///
|
deba@1409
|
926 |
/// It reads the content of the section.
|
deba@1408
|
927 |
virtual void read(std::istream& is) {
|
deba@1408
|
928 |
std::vector<ReaderBase<Item>* > index;
|
deba@1408
|
929 |
std::string line;
|
deba@1408
|
930 |
|
deba@1408
|
931 |
getline(is, line);
|
deba@1408
|
932 |
std::istringstream ls(line);
|
deba@1408
|
933 |
while (ls >> id) {
|
deba@1408
|
934 |
typename MapReaders::iterator it = readers.find(id);
|
deba@1408
|
935 |
if (it != readers.end()) {
|
deba@1408
|
936 |
index.push_back(it->second);
|
deba@1408
|
937 |
} else {
|
deba@1408
|
938 |
index.push_back(&skipper);
|
deba@1408
|
939 |
}
|
deba@1408
|
940 |
if (id == "id" && inverter.get() == 0) {
|
deba@1408
|
941 |
inverter.reset(index.back()->getInverter());
|
deba@1408
|
942 |
index.back() = inverter.get();
|
deba@1408
|
943 |
}
|
deba@1408
|
944 |
}
|
deba@1408
|
945 |
while (getline(is, line)) {
|
deba@1408
|
946 |
std::istringstream ls(line);
|
deba@1409
|
947 |
typename Graph::Node from = nodeIdReader->read(ls);
|
deba@1409
|
948 |
typename Graph::Node to = nodeIdReader->read(ls);
|
deba@1408
|
949 |
typename Graph::Edge edge = graph.addEdge(from, to);
|
deba@1408
|
950 |
for (int i = 0; i < (int)index.size(); ++i) {
|
deba@1408
|
951 |
index[i]->read(ls, edge);
|
deba@1408
|
952 |
}
|
deba@1408
|
953 |
}
|
deba@1408
|
954 |
}
|
deba@1408
|
955 |
|
deba@1409
|
956 |
public:
|
deba@1409
|
957 |
|
deba@1409
|
958 |
/// \brief Returns true if the edgeset can give back the edge by its id.
|
deba@1409
|
959 |
///
|
deba@1409
|
960 |
/// Returns true if the edgeset can give back the edge by its id.
|
deba@1409
|
961 |
/// It is possible only if an "id" named map was read.
|
deba@1409
|
962 |
bool isIdReader() const {
|
deba@1408
|
963 |
return inverter.get() != 0;
|
deba@1408
|
964 |
}
|
deba@1408
|
965 |
|
deba@1409
|
966 |
/// \brief Gives back the edge by its id.
|
deba@1409
|
967 |
///
|
deba@1409
|
968 |
/// It reads an id from the stream and gives back which edge belongs to
|
deba@1409
|
969 |
/// it. It is possible only if there was read an "id" named map.
|
deba@1421
|
970 |
Item readId(std::istream& is) const {
|
deba@1408
|
971 |
return inverter->read(is);
|
deba@1408
|
972 |
}
|
deba@1408
|
973 |
|
deba@1408
|
974 |
private:
|
deba@1408
|
975 |
|
deba@1408
|
976 |
typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
|
deba@1408
|
977 |
MapReaders readers;
|
deba@1408
|
978 |
|
deba@1421
|
979 |
typename SmartReference<Graph>::Type graph;
|
deba@1421
|
980 |
std::string id;
|
deba@1421
|
981 |
SkipReader<Item, DefaultSkipper> skipper;
|
deba@1421
|
982 |
|
deba@1421
|
983 |
std::auto_ptr<InverterBase<Item> > inverter;
|
deba@1421
|
984 |
std::auto_ptr<IdReaderBase<typename Graph::Node> > nodeIdReader;
|
deba@1421
|
985 |
};
|
deba@1421
|
986 |
|
deba@1421
|
987 |
/// \ingroup io_group
|
deba@1421
|
988 |
/// \brief SectionReader for reading a undirected graph's edgeset.
|
deba@1421
|
989 |
///
|
deba@1421
|
990 |
/// The lemon format can store multiple undirected edgesets with several
|
deba@1421
|
991 |
/// maps. The undirected edgeset section's header line is \c \@undiredgeset
|
deba@1421
|
992 |
/// \c undiredgeset_id, but the \c undiredgeset_id may be empty.
|
deba@1421
|
993 |
///
|
deba@1421
|
994 |
/// The first line of the section contains the names of the maps separated
|
deba@1421
|
995 |
/// with white spaces. Each next lines describes an edge in the edgeset. The
|
deba@1421
|
996 |
/// line contains the connected nodes' id and the mapped values for each map.
|
deba@1421
|
997 |
///
|
deba@1421
|
998 |
/// The section can handle the directed as a syntactical sugar. Two
|
deba@1421
|
999 |
/// undirected edge map describes one directed edge map. This two maps
|
deba@1421
|
1000 |
/// are the forward map and the backward map and the names of this map
|
deba@1421
|
1001 |
/// is near the same just with a prefix \c '+' or \c '-' character
|
deba@1421
|
1002 |
/// difference.
|
deba@1421
|
1003 |
///
|
deba@1421
|
1004 |
/// If the edgeset contains an \c "id" named map then it will be regarded
|
deba@1421
|
1005 |
/// as id map. This map should contain only unique values and when the
|
deba@1421
|
1006 |
/// \c readId() member will read a value from the given stream it will
|
deba@1421
|
1007 |
/// give back that undiricted edge which is mapped to this value.
|
deba@1421
|
1008 |
///
|
deba@1421
|
1009 |
/// The undirected edgeset reader needs a node id reader to identify which
|
deba@1421
|
1010 |
/// nodes have to be connected. If a NodeSetReader reads an "id" named map,
|
deba@1421
|
1011 |
/// it will be able to resolve the nodes by ids.
|
deba@1421
|
1012 |
///
|
deba@1421
|
1013 |
/// \relates LemonReader
|
deba@1421
|
1014 |
template <typename _Graph, typename _Traits = DefaultReaderTraits>
|
deba@1421
|
1015 |
class UndirEdgeSetReader : public CommonSectionReaderBase {
|
deba@1421
|
1016 |
typedef CommonSectionReaderBase Parent;
|
deba@1421
|
1017 |
public:
|
deba@1421
|
1018 |
|
deba@1421
|
1019 |
typedef _Graph Graph;
|
deba@1421
|
1020 |
typedef _Traits Traits;
|
deba@1421
|
1021 |
typedef typename Graph::UndirEdge Item;
|
deba@1421
|
1022 |
typedef typename Traits::Skipper DefaultSkipper;
|
deba@1421
|
1023 |
|
deba@1421
|
1024 |
/// \brief Constructor.
|
deba@1421
|
1025 |
///
|
deba@1421
|
1026 |
/// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader
|
deba@1421
|
1027 |
/// and attach it into the given LemonReader. The undirected edgeset
|
deba@1421
|
1028 |
/// reader will add the readed undirected edges to the given Graph. It
|
deba@1421
|
1029 |
/// will use the given node id reader to read the source and target
|
deba@1421
|
1030 |
/// nodes of the edges. The reader will read the section only if the
|
deba@1421
|
1031 |
/// \c _id and the \c undiredgset_id are the same.
|
deba@1421
|
1032 |
template <typename NodeIdReader>
|
deba@1421
|
1033 |
UndirEdgeSetReader(LemonReader& _reader,
|
deba@1421
|
1034 |
typename SmartParameter<Graph>::Type _graph,
|
deba@1421
|
1035 |
const NodeIdReader& _nodeIdReader,
|
deba@1421
|
1036 |
const std::string& _id = std::string(),
|
deba@1421
|
1037 |
const DefaultSkipper& _skipper = DefaultSkipper())
|
deba@1421
|
1038 |
: Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
|
deba@1421
|
1039 |
nodeIdReader(new IdReader<typename Graph::Node, NodeIdReader>
|
deba@1421
|
1040 |
(_nodeIdReader)) {}
|
deba@1421
|
1041 |
|
deba@1421
|
1042 |
/// \brief Destructor.
|
deba@1421
|
1043 |
///
|
deba@1421
|
1044 |
/// Destructor for UndirEdgeSetReader.
|
deba@1421
|
1045 |
virtual ~UndirEdgeSetReader() {
|
deba@1421
|
1046 |
for (typename MapReaders::iterator it = readers.begin();
|
deba@1421
|
1047 |
it != readers.end(); ++it) {
|
deba@1421
|
1048 |
delete it->second;
|
deba@1421
|
1049 |
}
|
deba@1421
|
1050 |
}
|
deba@1421
|
1051 |
|
deba@1421
|
1052 |
private:
|
deba@1421
|
1053 |
UndirEdgeSetReader(const UndirEdgeSetReader&);
|
deba@1421
|
1054 |
void operator=(const UndirEdgeSetReader&);
|
deba@1421
|
1055 |
|
deba@1421
|
1056 |
public:
|
deba@1421
|
1057 |
|
deba@1421
|
1058 |
/// \brief Add a new undirected edge map reader command for the reader.
|
deba@1421
|
1059 |
///
|
deba@1421
|
1060 |
/// Add a new edge undirected map reader command for the reader.
|
deba@1421
|
1061 |
template <typename Map>
|
deba@1421
|
1062 |
UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) {
|
deba@1421
|
1063 |
return _readMap<
|
deba@1421
|
1064 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
1065 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
1066 |
}
|
deba@1421
|
1067 |
|
deba@1421
|
1068 |
template <typename Map>
|
deba@1421
|
1069 |
UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) {
|
deba@1421
|
1070 |
return _readMap<
|
deba@1421
|
1071 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
1072 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
1073 |
}
|
deba@1421
|
1074 |
|
deba@1421
|
1075 |
/// \brief Add a new undirected edge map reader command for the reader.
|
deba@1421
|
1076 |
///
|
deba@1421
|
1077 |
/// Add a new edge undirected map reader command for the reader.
|
deba@1421
|
1078 |
template <typename Reader, typename Map>
|
deba@1421
|
1079 |
UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map,
|
deba@1421
|
1080 |
const Reader& reader = Reader()) {
|
deba@1421
|
1081 |
return _readMap<Reader, Map, typename SmartParameter<Map>::Type>
|
deba@1421
|
1082 |
(name, map, reader);
|
deba@1421
|
1083 |
}
|
deba@1421
|
1084 |
|
deba@1421
|
1085 |
template <typename Reader, typename Map>
|
deba@1421
|
1086 |
UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map,
|
deba@1421
|
1087 |
const Reader& reader = Reader()) {
|
deba@1421
|
1088 |
return _readMap<Reader, Map, typename SmartParameter<Map>::Type >
|
deba@1421
|
1089 |
(name, map, reader);
|
deba@1421
|
1090 |
}
|
deba@1421
|
1091 |
|
deba@1421
|
1092 |
private:
|
deba@1421
|
1093 |
|
deba@1421
|
1094 |
template <typename Reader, typename Map, typename MapParameter>
|
deba@1421
|
1095 |
UndirEdgeSetReader& _readMap(std::string name, MapParameter map,
|
deba@1421
|
1096 |
const Reader& reader = Reader()) {
|
deba@1421
|
1097 |
if (readers.find(name) != readers.end()) {
|
deba@1421
|
1098 |
ErrorMessage msg;
|
deba@1421
|
1099 |
msg << "Multiple read rule for edge map: " << name;
|
deba@1421
|
1100 |
throw IOParameterError(msg.message());
|
deba@1421
|
1101 |
}
|
deba@1421
|
1102 |
readers.insert(
|
deba@1421
|
1103 |
make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
|
deba@1421
|
1104 |
return *this;
|
deba@1421
|
1105 |
}
|
deba@1421
|
1106 |
|
deba@1421
|
1107 |
public:
|
deba@1421
|
1108 |
|
deba@1421
|
1109 |
/// \brief Add a new undirected edge map skipper command for the reader.
|
deba@1421
|
1110 |
///
|
deba@1421
|
1111 |
/// Add a new undirected edge map skipper command for the reader.
|
deba@1421
|
1112 |
template <typename Reader>
|
deba@1421
|
1113 |
UndirEdgeSetReader& skipUndirEdgeMap(std::string name,
|
deba@1421
|
1114 |
const Reader& reader = Reader()) {
|
deba@1421
|
1115 |
if (readers.find(name) != readers.end()) {
|
deba@1421
|
1116 |
ErrorMessage msg;
|
deba@1421
|
1117 |
msg << "Multiple read rule for node map: " << name;
|
deba@1421
|
1118 |
throw IOParameterError(msg.message());
|
deba@1421
|
1119 |
}
|
deba@1421
|
1120 |
readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
|
deba@1421
|
1121 |
return *this;
|
deba@1421
|
1122 |
}
|
deba@1421
|
1123 |
|
deba@1421
|
1124 |
/// \brief Add a new directed edge map reader command for the reader.
|
deba@1421
|
1125 |
///
|
deba@1421
|
1126 |
/// Add a new directed edge map reader command for the reader.
|
deba@1421
|
1127 |
template <typename Map>
|
deba@1421
|
1128 |
UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) {
|
deba@1421
|
1129 |
return _readDirMap<
|
deba@1421
|
1130 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
1131 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
1132 |
}
|
deba@1421
|
1133 |
|
deba@1421
|
1134 |
template <typename Map>
|
deba@1421
|
1135 |
UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) {
|
deba@1421
|
1136 |
return _readDirMap<
|
deba@1421
|
1137 |
typename Traits::template Reader<typename Map::Value>, Map,
|
deba@1421
|
1138 |
typename SmartParameter<Map>::Type>(name, map);
|
deba@1421
|
1139 |
}
|
deba@1421
|
1140 |
|
deba@1421
|
1141 |
/// \brief Add a new directed edge map reader command for the reader.
|
deba@1421
|
1142 |
///
|
deba@1421
|
1143 |
/// Add a new directed edge map reader command for the reader.
|
deba@1421
|
1144 |
template <typename Reader, typename Map>
|
deba@1421
|
1145 |
UndirEdgeSetReader& readEdgeMap(std::string name, Map& map,
|
deba@1421
|
1146 |
const Reader& reader = Reader()) {
|
deba@1421
|
1147 |
return _readDirMap<Reader, Map, typename SmartParameter<Map>::Type>
|
deba@1421
|
1148 |
(name, map, reader);
|
deba@1421
|
1149 |
}
|
deba@1421
|
1150 |
|
deba@1421
|
1151 |
template <typename Reader, typename Map>
|
deba@1421
|
1152 |
UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map,
|
deba@1421
|
1153 |
const Reader& reader = Reader()) {
|
deba@1421
|
1154 |
return _readDirMap<Reader, Map, typename SmartParameter<Map>::Type>
|
deba@1421
|
1155 |
(name, map, reader);
|
deba@1421
|
1156 |
}
|
deba@1421
|
1157 |
|
deba@1421
|
1158 |
private:
|
deba@1421
|
1159 |
|
deba@1421
|
1160 |
template <typename Reader, typename Map, typename MapParameter>
|
deba@1421
|
1161 |
UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map,
|
deba@1421
|
1162 |
const Reader& reader = Reader()) {
|
deba@1421
|
1163 |
readMap("+" + name,
|
deba@1421
|
1164 |
_reader_bits::writeComposeMap(map, forwardMap(graph)), reader);
|
deba@1421
|
1165 |
readMap("-" + name,
|
deba@1421
|
1166 |
_reader_bits::writeComposeMap(map, backwardMap(graph)), reader);
|
deba@1421
|
1167 |
return *this;
|
deba@1421
|
1168 |
}
|
deba@1421
|
1169 |
|
deba@1421
|
1170 |
public:
|
deba@1421
|
1171 |
|
deba@1421
|
1172 |
/// \brief Add a new directed edge map skipper command for the reader.
|
deba@1421
|
1173 |
///
|
deba@1421
|
1174 |
/// Add a new directed edge map skipper command for the reader.
|
deba@1421
|
1175 |
template <typename Reader>
|
deba@1421
|
1176 |
UndirEdgeSetReader& skipEdgeMap(std::string name,
|
deba@1421
|
1177 |
const Reader& reader = Reader()) {
|
deba@1421
|
1178 |
skipMap("+" + name, reader);
|
deba@1421
|
1179 |
skipMap("-" + name, reader);
|
deba@1421
|
1180 |
return *this;
|
deba@1421
|
1181 |
}
|
deba@1421
|
1182 |
|
deba@1421
|
1183 |
protected:
|
deba@1421
|
1184 |
|
deba@1421
|
1185 |
/// \brief Gives back true when the SectionReader can process
|
deba@1421
|
1186 |
/// the section with the given header line.
|
deba@1421
|
1187 |
///
|
deba@1421
|
1188 |
/// It gives back true when the header line starts with \c \@undiredgeset,
|
deba@1421
|
1189 |
/// and the header line's id and the edgeset's id are the same.
|
deba@1421
|
1190 |
virtual bool header(const std::string& line) {
|
deba@1421
|
1191 |
std::istringstream ls(line);
|
deba@1421
|
1192 |
std::string command;
|
deba@1421
|
1193 |
std::string name;
|
deba@1421
|
1194 |
ls >> command >> name;
|
deba@1421
|
1195 |
return command == "@undiredgeset" && name == id;
|
deba@1421
|
1196 |
}
|
deba@1421
|
1197 |
|
deba@1421
|
1198 |
/// \brief Reader function of the section.
|
deba@1421
|
1199 |
///
|
deba@1421
|
1200 |
/// It reads the content of the section.
|
deba@1421
|
1201 |
virtual void read(std::istream& is) {
|
deba@1421
|
1202 |
std::vector<ReaderBase<Item>* > index;
|
deba@1421
|
1203 |
std::string line;
|
deba@1421
|
1204 |
|
deba@1421
|
1205 |
getline(is, line);
|
deba@1421
|
1206 |
std::istringstream ls(line);
|
deba@1421
|
1207 |
while (ls >> id) {
|
deba@1421
|
1208 |
typename MapReaders::iterator it = readers.find(id);
|
deba@1421
|
1209 |
if (it != readers.end()) {
|
deba@1421
|
1210 |
index.push_back(it->second);
|
deba@1421
|
1211 |
} else {
|
deba@1421
|
1212 |
index.push_back(&skipper);
|
deba@1421
|
1213 |
}
|
deba@1421
|
1214 |
if (id == "id" && inverter.get() == 0) {
|
deba@1421
|
1215 |
inverter.reset(index.back()->getInverter());
|
deba@1421
|
1216 |
index.back() = inverter.get();
|
deba@1421
|
1217 |
}
|
deba@1421
|
1218 |
}
|
deba@1421
|
1219 |
while (getline(is, line)) {
|
deba@1421
|
1220 |
std::istringstream ls(line);
|
deba@1421
|
1221 |
typename Graph::Node from = nodeIdReader->read(ls);
|
deba@1421
|
1222 |
typename Graph::Node to = nodeIdReader->read(ls);
|
deba@1421
|
1223 |
typename Graph::UndirEdge edge = graph.addEdge(from, to);
|
deba@1421
|
1224 |
for (int i = 0; i < (int)index.size(); ++i) {
|
deba@1421
|
1225 |
index[i]->read(ls, edge);
|
deba@1421
|
1226 |
}
|
deba@1421
|
1227 |
}
|
deba@1421
|
1228 |
}
|
deba@1421
|
1229 |
|
deba@1421
|
1230 |
public:
|
deba@1421
|
1231 |
|
deba@1421
|
1232 |
/// \brief Returns true if the edgeset can give back the edge by its id.
|
deba@1421
|
1233 |
///
|
deba@1421
|
1234 |
/// Returns true if the edgeset can give back the undirected edge by its
|
deba@1421
|
1235 |
/// id. It is possible only if an "id" named map was read.
|
deba@1421
|
1236 |
bool isIdReader() const {
|
deba@1421
|
1237 |
return inverter.get() != 0;
|
deba@1421
|
1238 |
}
|
deba@1421
|
1239 |
|
deba@1421
|
1240 |
/// \brief Gives back the undirected edge by its id.
|
deba@1421
|
1241 |
///
|
deba@1421
|
1242 |
/// It reads an id from the stream and gives back which undirected edge
|
deba@1421
|
1243 |
/// belongs to it. It is possible only if there was read an "id" named map.
|
deba@1421
|
1244 |
Item readId(std::istream& is) const {
|
deba@1421
|
1245 |
return inverter->read(is);
|
deba@1421
|
1246 |
}
|
deba@1421
|
1247 |
|
deba@1421
|
1248 |
private:
|
deba@1421
|
1249 |
|
deba@1421
|
1250 |
typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
|
deba@1421
|
1251 |
MapReaders readers;
|
deba@1421
|
1252 |
|
deba@1421
|
1253 |
typename SmartReference<Graph>::Type graph;
|
deba@1408
|
1254 |
std::string id;
|
deba@1408
|
1255 |
SkipReader<Item, DefaultSkipper> skipper;
|
deba@1408
|
1256 |
|
deba@1408
|
1257 |
std::auto_ptr<InverterBase<Item> > inverter;
|
deba@1409
|
1258 |
std::auto_ptr<IdReaderBase<typename Graph::Node> > nodeIdReader;
|
deba@1408
|
1259 |
};
|
deba@1408
|
1260 |
|
deba@1409
|
1261 |
/// \ingroup io_group
|
deba@1409
|
1262 |
/// \brief SectionReader for reading labeled nodes.
|
deba@1409
|
1263 |
///
|
deba@1409
|
1264 |
/// The nodes section's header line is \c \@nodes \c nodes_id, but the
|
deba@1409
|
1265 |
/// \c nodes_id may be empty.
|
deba@1409
|
1266 |
///
|
deba@1409
|
1267 |
/// Each line in the section contains the name of the node
|
deba@1409
|
1268 |
/// and then the node id.
|
deba@1409
|
1269 |
///
|
deba@1409
|
1270 |
/// \relates LemonReader
|
deba@1409
|
1271 |
template <typename _Graph>
|
deba@1409
|
1272 |
class NodeReader : public CommonSectionReaderBase {
|
deba@1409
|
1273 |
typedef CommonSectionReaderBase Parent;
|
deba@1409
|
1274 |
typedef _Graph Graph;
|
deba@1409
|
1275 |
typedef typename Graph::Node Item;
|
deba@1409
|
1276 |
public:
|
deba@1409
|
1277 |
|
deba@1409
|
1278 |
/// \brief Constructor.
|
deba@1409
|
1279 |
///
|
deba@1409
|
1280 |
/// Constructor for NodeReader. It creates the NodeReader and
|
deba@1409
|
1281 |
/// attach it into the given LemonReader. It will use the given
|
deba@1409
|
1282 |
/// node id reader to give back the nodes. The reader will read the
|
deba@1409
|
1283 |
/// section only if the \c _id and the \c nodes_id are the same.
|
deba@1409
|
1284 |
template <typename _IdReader>
|
deba@1409
|
1285 |
NodeReader(LemonReader& _reader, const _IdReader& _idReader,
|
deba@1409
|
1286 |
const std::string& _id = std::string())
|
deba@1409
|
1287 |
: Parent(_reader), id(_id),
|
deba@1409
|
1288 |
idReader(new IdReader<typename Graph::Node, _IdReader>(_idReader)) {}
|
deba@1408
|
1289 |
|
deba@1409
|
1290 |
/// \brief Destructor.
|
deba@1409
|
1291 |
///
|
deba@1409
|
1292 |
/// Destructor for NodeReader.
|
deba@1409
|
1293 |
virtual ~NodeReader() {}
|
deba@1409
|
1294 |
|
deba@1409
|
1295 |
private:
|
deba@1409
|
1296 |
NodeReader(const NodeReader&);
|
deba@1409
|
1297 |
void operator=(const NodeReader&);
|
deba@1409
|
1298 |
|
deba@1409
|
1299 |
public:
|
deba@1409
|
1300 |
|
deba@1409
|
1301 |
/// \brief Add a node reader command for the NodeReader.
|
deba@1409
|
1302 |
///
|
deba@1409
|
1303 |
/// Add a node reader command for the NodeReader.
|
deba@1409
|
1304 |
void readNode(const std::string& name, Item& item) {
|
deba@1409
|
1305 |
if (readers.find(name) != readers.end()) {
|
deba@1409
|
1306 |
ErrorMessage msg;
|
deba@1409
|
1307 |
msg << "Multiple read rule for node: " << name;
|
deba@1409
|
1308 |
throw IOParameterError(msg.message());
|
deba@1409
|
1309 |
}
|
deba@1409
|
1310 |
readers.insert(make_pair(name, &item));
|
deba@1409
|
1311 |
}
|
deba@1409
|
1312 |
|
deba@1409
|
1313 |
protected:
|
deba@1409
|
1314 |
|
deba@1409
|
1315 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
1316 |
/// the section with the given header line.
|
deba@1409
|
1317 |
///
|
deba@1421
|
1318 |
/// It gives back true when the header line start with \c \@nodes,
|
deba@1409
|
1319 |
/// and the header line's id and the reader's id are the same.
|
deba@1409
|
1320 |
virtual bool header(const std::string& line) {
|
deba@1409
|
1321 |
std::istringstream ls(line);
|
deba@1409
|
1322 |
std::string command;
|
deba@1409
|
1323 |
std::string name;
|
deba@1409
|
1324 |
ls >> command >> name;
|
deba@1409
|
1325 |
return command == "@nodes" && name == id;
|
deba@1409
|
1326 |
}
|
deba@1409
|
1327 |
|
deba@1409
|
1328 |
/// \brief Reader function of the section.
|
deba@1409
|
1329 |
///
|
deba@1409
|
1330 |
/// It reads the content of the section.
|
deba@1409
|
1331 |
virtual void read(std::istream& is) {
|
deba@1409
|
1332 |
std::string line;
|
deba@1409
|
1333 |
while (getline(is, line)) {
|
deba@1409
|
1334 |
std::istringstream ls(line);
|
deba@1409
|
1335 |
std::string id;
|
deba@1409
|
1336 |
ls >> id;
|
deba@1409
|
1337 |
typename ItemReaders::iterator it = readers.find(id);
|
deba@1409
|
1338 |
if (it != readers.end()) {
|
deba@1409
|
1339 |
*(it->second) = idReader->read(ls);
|
deba@1409
|
1340 |
}
|
deba@1409
|
1341 |
}
|
deba@1409
|
1342 |
}
|
deba@1409
|
1343 |
|
deba@1409
|
1344 |
private:
|
deba@1409
|
1345 |
|
deba@1409
|
1346 |
std::string id;
|
deba@1409
|
1347 |
|
deba@1409
|
1348 |
typedef std::map<std::string, Item*> ItemReaders;
|
deba@1409
|
1349 |
ItemReaders readers;
|
deba@1409
|
1350 |
std::auto_ptr<IdReaderBase<Item> > idReader;
|
deba@1409
|
1351 |
};
|
deba@1409
|
1352 |
|
deba@1409
|
1353 |
/// \ingroup io_group
|
deba@1409
|
1354 |
/// \brief SectionReader for reading labeled edges.
|
deba@1409
|
1355 |
///
|
deba@1409
|
1356 |
/// The edges section's header line is \c \@edges \c edges_id, but the
|
deba@1409
|
1357 |
/// \c edges_id may be empty.
|
deba@1409
|
1358 |
///
|
deba@1409
|
1359 |
/// Each line in the section contains the name of the edge
|
deba@1409
|
1360 |
/// and then the edge id.
|
deba@1409
|
1361 |
///
|
deba@1409
|
1362 |
/// \relates LemonReader
|
deba@1409
|
1363 |
template <typename _Graph>
|
deba@1409
|
1364 |
class EdgeReader : public CommonSectionReaderBase {
|
deba@1409
|
1365 |
typedef CommonSectionReaderBase Parent;
|
deba@1409
|
1366 |
typedef _Graph Graph;
|
deba@1409
|
1367 |
typedef typename Graph::Edge Item;
|
deba@1409
|
1368 |
public:
|
deba@1409
|
1369 |
|
deba@1409
|
1370 |
/// \brief Constructor.
|
deba@1409
|
1371 |
///
|
deba@1409
|
1372 |
/// Constructor for EdgeReader. It creates the EdgeReader and
|
deba@1409
|
1373 |
/// attach it into the given LemonReader. It will use the given
|
deba@1409
|
1374 |
/// edge id reader to give back the edges. The reader will read the
|
deba@1421
|
1375 |
/// section only if the \c _id and the \c edges_id are the same.
|
deba@1409
|
1376 |
template <typename _IdReader>
|
deba@1409
|
1377 |
EdgeReader(LemonReader& _reader, const _IdReader& _idReader,
|
deba@1409
|
1378 |
const std::string& _id = std::string())
|
deba@1409
|
1379 |
: Parent(_reader), id(_id),
|
deba@1409
|
1380 |
idReader(new IdReader<typename Graph::Edge, _IdReader>(_idReader)) {}
|
deba@1409
|
1381 |
|
deba@1409
|
1382 |
/// \brief Destructor.
|
deba@1409
|
1383 |
///
|
deba@1409
|
1384 |
/// Destructor for EdgeReader.
|
deba@1409
|
1385 |
virtual ~EdgeReader() {}
|
deba@1409
|
1386 |
private:
|
deba@1409
|
1387 |
EdgeReader(const EdgeReader&);
|
deba@1409
|
1388 |
void operator=(const EdgeReader&);
|
deba@1409
|
1389 |
|
deba@1409
|
1390 |
public:
|
deba@1409
|
1391 |
|
deba@1409
|
1392 |
/// \brief Add an edge reader command for the EdgeReader.
|
deba@1409
|
1393 |
///
|
deba@1409
|
1394 |
/// Add an edge reader command for the EdgeReader.
|
deba@1409
|
1395 |
void readEdge(const std::string& name, Item& item) {
|
deba@1409
|
1396 |
if (readers.find(name) != readers.end()) {
|
deba@1409
|
1397 |
ErrorMessage msg;
|
deba@1409
|
1398 |
msg << "Multiple read rule for edge: " << name;
|
deba@1409
|
1399 |
throw IOParameterError(msg.message());
|
deba@1409
|
1400 |
}
|
deba@1409
|
1401 |
readers.insert(make_pair(name, &item));
|
deba@1409
|
1402 |
}
|
deba@1409
|
1403 |
|
deba@1409
|
1404 |
protected:
|
deba@1409
|
1405 |
|
deba@1409
|
1406 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
1407 |
/// the section with the given header line.
|
deba@1409
|
1408 |
///
|
deba@1421
|
1409 |
/// It gives back true when the header line start with \c \@edges,
|
deba@1421
|
1410 |
/// and the header line's id and the reader's id are the same.
|
deba@1421
|
1411 |
virtual bool header(const std::string& line) {
|
deba@1421
|
1412 |
std::istringstream ls(line);
|
deba@1421
|
1413 |
std::string command;
|
deba@1421
|
1414 |
std::string name;
|
deba@1421
|
1415 |
ls >> command >> name;
|
deba@1421
|
1416 |
return command == "@edges" && name == id;
|
deba@1421
|
1417 |
}
|
deba@1421
|
1418 |
|
deba@1421
|
1419 |
/// \brief Reader function of the section.
|
deba@1421
|
1420 |
///
|
deba@1421
|
1421 |
/// It reads the content of the section.
|
deba@1421
|
1422 |
virtual void read(std::istream& is) {
|
deba@1421
|
1423 |
std::string line;
|
deba@1421
|
1424 |
while (getline(is, line)) {
|
deba@1421
|
1425 |
std::istringstream ls(line);
|
deba@1421
|
1426 |
std::string id;
|
deba@1421
|
1427 |
ls >> id;
|
deba@1421
|
1428 |
typename ItemReaders::iterator it = readers.find(id);
|
deba@1421
|
1429 |
if (it != readers.end()) {
|
deba@1421
|
1430 |
*(it->second) = idReader->read(ls);
|
deba@1421
|
1431 |
}
|
deba@1421
|
1432 |
}
|
deba@1421
|
1433 |
}
|
deba@1421
|
1434 |
|
deba@1421
|
1435 |
private:
|
deba@1421
|
1436 |
|
deba@1421
|
1437 |
std::string id;
|
deba@1421
|
1438 |
|
deba@1421
|
1439 |
typedef std::map<std::string, Item*> ItemReaders;
|
deba@1421
|
1440 |
ItemReaders readers;
|
deba@1421
|
1441 |
std::auto_ptr<IdReaderBase<Item> > idReader;
|
deba@1421
|
1442 |
};
|
deba@1421
|
1443 |
|
deba@1421
|
1444 |
/// \ingroup io_group
|
deba@1421
|
1445 |
/// \brief SectionReader for reading labeled undirected edges.
|
deba@1421
|
1446 |
///
|
deba@1421
|
1447 |
/// The undirected edges section's header line is \c \@undiredges
|
deba@1421
|
1448 |
/// \c undiredges_id, but the \c undiredges_id may be empty.
|
deba@1421
|
1449 |
///
|
deba@1421
|
1450 |
/// Each line in the section contains the name of the undirected edge
|
deba@1421
|
1451 |
/// and then the undirected edge id.
|
deba@1421
|
1452 |
///
|
deba@1421
|
1453 |
/// \relates LemonReader
|
deba@1421
|
1454 |
template <typename _Graph>
|
deba@1421
|
1455 |
class UndirEdgeReader : public CommonSectionReaderBase {
|
deba@1421
|
1456 |
typedef CommonSectionReaderBase Parent;
|
deba@1421
|
1457 |
typedef _Graph Graph;
|
deba@1421
|
1458 |
typedef typename Graph::UndirEdge Item;
|
deba@1421
|
1459 |
public:
|
deba@1421
|
1460 |
|
deba@1421
|
1461 |
/// \brief Constructor.
|
deba@1421
|
1462 |
///
|
deba@1421
|
1463 |
/// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and
|
deba@1421
|
1464 |
/// attach it into the given LemonReader. It will use the given
|
deba@1421
|
1465 |
/// undirected edge id reader to give back the edges. The reader will
|
deba@1421
|
1466 |
/// read the section only if the \c _id and the \c undiredges_id are
|
deba@1421
|
1467 |
/// the same.
|
deba@1421
|
1468 |
template <typename _IdReader>
|
deba@1421
|
1469 |
UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader,
|
deba@1421
|
1470 |
const std::string& _id = std::string())
|
deba@1421
|
1471 |
: Parent(_reader), id(_id),
|
deba@1421
|
1472 |
idReader(new IdReader<typename Graph::UndirEdge, _IdReader>(_idReader))
|
deba@1421
|
1473 |
{}
|
deba@1421
|
1474 |
|
deba@1421
|
1475 |
/// \brief Destructor.
|
deba@1421
|
1476 |
///
|
deba@1421
|
1477 |
/// Destructor for UndirEdgeReader.
|
deba@1421
|
1478 |
virtual ~UndirEdgeReader() {}
|
deba@1421
|
1479 |
private:
|
deba@1421
|
1480 |
UndirEdgeReader(const UndirEdgeReader&);
|
deba@1421
|
1481 |
void operator=(const UndirEdgeReader&);
|
deba@1421
|
1482 |
|
deba@1421
|
1483 |
public:
|
deba@1421
|
1484 |
|
deba@1421
|
1485 |
/// \brief Add an undirected edge reader command for the UndirEdgeReader.
|
deba@1421
|
1486 |
///
|
deba@1421
|
1487 |
/// Add an undirected edge reader command for the UndirEdgeReader.
|
deba@1421
|
1488 |
void readUndirEdge(const std::string& name, Item& item) {
|
deba@1421
|
1489 |
if (readers.find(name) != readers.end()) {
|
deba@1421
|
1490 |
ErrorMessage msg;
|
deba@1421
|
1491 |
msg << "Multiple read rule for edge: " << name;
|
deba@1421
|
1492 |
throw IOParameterError(msg.message());
|
deba@1421
|
1493 |
}
|
deba@1421
|
1494 |
readers.insert(make_pair(name, &item));
|
deba@1421
|
1495 |
}
|
deba@1421
|
1496 |
|
deba@1421
|
1497 |
protected:
|
deba@1421
|
1498 |
|
deba@1421
|
1499 |
/// \brief Gives back true when the SectionReader can process
|
deba@1421
|
1500 |
/// the section with the given header line.
|
deba@1421
|
1501 |
///
|
deba@1421
|
1502 |
/// It gives back true when the header line start with \c \@edges,
|
deba@1409
|
1503 |
/// and the header line's id and the reader's id are the same.
|
deba@1409
|
1504 |
virtual bool header(const std::string& line) {
|
deba@1409
|
1505 |
std::istringstream ls(line);
|
deba@1409
|
1506 |
std::string command;
|
deba@1409
|
1507 |
std::string name;
|
deba@1409
|
1508 |
ls >> command >> name;
|
deba@1409
|
1509 |
return command == "@edges" && name == id;
|
deba@1409
|
1510 |
}
|
deba@1409
|
1511 |
|
deba@1409
|
1512 |
/// \brief Reader function of the section.
|
deba@1409
|
1513 |
///
|
deba@1409
|
1514 |
/// It reads the content of the section.
|
deba@1409
|
1515 |
virtual void read(std::istream& is) {
|
deba@1409
|
1516 |
std::string line;
|
deba@1409
|
1517 |
while (getline(is, line)) {
|
deba@1409
|
1518 |
std::istringstream ls(line);
|
deba@1409
|
1519 |
std::string id;
|
deba@1409
|
1520 |
ls >> id;
|
deba@1409
|
1521 |
typename ItemReaders::iterator it = readers.find(id);
|
deba@1409
|
1522 |
if (it != readers.end()) {
|
deba@1409
|
1523 |
*(it->second) = idReader->read(ls);
|
deba@1409
|
1524 |
}
|
deba@1409
|
1525 |
}
|
deba@1409
|
1526 |
}
|
deba@1409
|
1527 |
|
deba@1409
|
1528 |
private:
|
deba@1409
|
1529 |
|
deba@1409
|
1530 |
std::string id;
|
deba@1409
|
1531 |
|
deba@1409
|
1532 |
typedef std::map<std::string, Item*> ItemReaders;
|
deba@1409
|
1533 |
ItemReaders readers;
|
deba@1409
|
1534 |
std::auto_ptr<IdReaderBase<Item> > idReader;
|
deba@1409
|
1535 |
};
|
deba@1409
|
1536 |
|
deba@1409
|
1537 |
/// \ingroup io_group
|
deba@1409
|
1538 |
/// \brief SectionReader for attributes.
|
deba@1409
|
1539 |
///
|
deba@1409
|
1540 |
/// The lemon format can store multiple attribute set. Each set has
|
deba@1409
|
1541 |
/// the header line \c \@attributes \c attributeset_id, but the
|
deba@1409
|
1542 |
/// attributeset_id may be empty.
|
deba@1409
|
1543 |
///
|
deba@1409
|
1544 |
/// The attributeset section contains several lines. Each of them starts
|
deba@1409
|
1545 |
/// with an attribute and then a the value for the id.
|
deba@1409
|
1546 |
///
|
deba@1409
|
1547 |
/// \relates LemonReader
|
deba@1408
|
1548 |
template <typename _Traits = DefaultReaderTraits>
|
deba@1408
|
1549 |
class AttributeReader : public CommonSectionReaderBase {
|
deba@1408
|
1550 |
typedef CommonSectionReaderBase Parent;
|
deba@1408
|
1551 |
typedef _Traits Traits;
|
deba@1408
|
1552 |
public:
|
deba@1409
|
1553 |
/// \brief Constructor.
|
deba@1409
|
1554 |
///
|
deba@1409
|
1555 |
/// Constructor for AttributeReader. It creates the AttributeReader and
|
deba@1409
|
1556 |
/// attach it into the given LemonReader. The reader process a section
|
deba@1409
|
1557 |
/// only if the \c section_id and the \c _id are the same.
|
deba@1408
|
1558 |
AttributeReader(LemonReader& _reader,
|
deba@1409
|
1559 |
const std::string& _id = std::string())
|
deba@1409
|
1560 |
: Parent(_reader), id(_id) {}
|
deba@1408
|
1561 |
|
deba@1409
|
1562 |
/// \brief Destructor.
|
deba@1409
|
1563 |
///
|
deba@1409
|
1564 |
/// Destructor for AttributeReader.
|
deba@1408
|
1565 |
virtual ~AttributeReader() {
|
deba@1408
|
1566 |
for (typename Readers::iterator it = readers.begin();
|
deba@1408
|
1567 |
it != readers.end(); ++it) {
|
deba@1408
|
1568 |
delete it->second;
|
deba@1408
|
1569 |
}
|
deba@1408
|
1570 |
}
|
deba@1408
|
1571 |
|
deba@1408
|
1572 |
private:
|
deba@1408
|
1573 |
AttributeReader(const AttributeReader&);
|
deba@1408
|
1574 |
void operator=(AttributeReader&);
|
deba@1408
|
1575 |
|
deba@1408
|
1576 |
public:
|
deba@1409
|
1577 |
/// \brief Add an attribute reader command for the reader.
|
deba@1409
|
1578 |
///
|
deba@1409
|
1579 |
/// Add an attribute reader command for the reader.
|
deba@1408
|
1580 |
template <typename Value>
|
deba@1408
|
1581 |
AttributeReader& readAttribute(const std::string& id, Value& value) {
|
deba@1408
|
1582 |
return readAttribute<typename Traits::template Reader<Value> >
|
deba@1408
|
1583 |
(id, value);
|
deba@1408
|
1584 |
}
|
deba@1408
|
1585 |
|
deba@1409
|
1586 |
/// \brief Add an attribute reader command for the reader.
|
deba@1409
|
1587 |
///
|
deba@1409
|
1588 |
/// Add an attribute reader command for the reader.
|
deba@1408
|
1589 |
template <typename Reader, typename Value>
|
deba@1408
|
1590 |
AttributeReader& readAttribute(const std::string& name, Value& value,
|
deba@1408
|
1591 |
const Reader& reader = Reader()) {
|
deba@1408
|
1592 |
if (readers.find(name) != readers.end()) {
|
deba@1408
|
1593 |
ErrorMessage msg;
|
deba@1408
|
1594 |
msg << "Multiple read rule for attribute: " << name;
|
deba@1408
|
1595 |
throw IOParameterError(msg.message());
|
deba@1408
|
1596 |
}
|
deba@1408
|
1597 |
readers.insert(make_pair(name, new ValueReader<Value, Reader>
|
deba@1408
|
1598 |
(value, reader)));
|
deba@1408
|
1599 |
return *this;
|
deba@1408
|
1600 |
}
|
deba@1408
|
1601 |
|
deba@1409
|
1602 |
protected:
|
deba@1409
|
1603 |
|
deba@1409
|
1604 |
/// \brief Gives back true when the SectionReader can process
|
deba@1409
|
1605 |
/// the section with the given header line.
|
deba@1409
|
1606 |
///
|
deba@1421
|
1607 |
/// It gives back true when the header line start with \c \@attributes,
|
deba@1409
|
1608 |
/// and the header line's id and the attributeset's id are the same.
|
deba@1408
|
1609 |
bool header(const std::string& line) {
|
deba@1408
|
1610 |
std::istringstream ls(line);
|
deba@1408
|
1611 |
std::string command;
|
deba@1408
|
1612 |
std::string name;
|
deba@1408
|
1613 |
ls >> command >> name;
|
deba@1408
|
1614 |
return command == "@attributes" && name == id;
|
deba@1408
|
1615 |
}
|
deba@1408
|
1616 |
|
deba@1409
|
1617 |
/// \brief Reader function of the section.
|
deba@1409
|
1618 |
///
|
deba@1409
|
1619 |
/// It reads the content of the section.
|
deba@1408
|
1620 |
void read(std::istream& is) {
|
deba@1408
|
1621 |
std::string line;
|
deba@1408
|
1622 |
while (getline(is, line)) {
|
deba@1408
|
1623 |
std::istringstream ls(line);
|
deba@1408
|
1624 |
std::string id;
|
deba@1408
|
1625 |
ls >> id;
|
deba@1408
|
1626 |
typename Readers::iterator it = readers.find(id);
|
deba@1408
|
1627 |
if (it != readers.end()) {
|
deba@1408
|
1628 |
it->second->read(ls);
|
deba@1408
|
1629 |
}
|
deba@1408
|
1630 |
}
|
deba@1408
|
1631 |
}
|
deba@1408
|
1632 |
|
deba@1408
|
1633 |
private:
|
deba@1408
|
1634 |
std::string id;
|
deba@1408
|
1635 |
|
deba@1408
|
1636 |
typedef std::map<std::string, ValueReaderBase*> Readers;
|
deba@1409
|
1637 |
Readers readers;
|
deba@1408
|
1638 |
};
|
deba@1408
|
1639 |
|
deba@1423
|
1640 |
/// \ingroup io_group
|
deba@1423
|
1641 |
/// \brief SectionReader for retrieve what is in the file.
|
deba@1423
|
1642 |
///
|
deba@1423
|
1643 |
/// SectionReader for retrieve what is in the file. If you want
|
deba@1423
|
1644 |
/// to know which sections, maps and items are in the file
|
deba@1423
|
1645 |
/// use the next code:
|
deba@1423
|
1646 |
/// \code
|
deba@1423
|
1647 |
/// LemonReader reader("input.lgf");
|
deba@1423
|
1648 |
/// ContentReader content(reader);
|
deba@1423
|
1649 |
/// reader.run();
|
deba@1423
|
1650 |
/// \endcode
|
deba@1423
|
1651 |
class ContentReader : public LemonReader::SectionReader {
|
deba@1423
|
1652 |
typedef LemonReader::SectionReader Parent;
|
deba@1423
|
1653 |
public:
|
deba@1423
|
1654 |
/// \brief Constructor.
|
deba@1423
|
1655 |
///
|
deba@1423
|
1656 |
/// Constructor for
|
deba@1423
|
1657 |
ContentReader(LemonReader& _reader) : Parent(_reader) {}
|
deba@1423
|
1658 |
|
deba@1423
|
1659 |
/// \brief Desctructor.
|
deba@1423
|
1660 |
///
|
deba@1423
|
1661 |
/// Desctructor.
|
deba@1423
|
1662 |
virtual ~ContentReader() {}
|
deba@1423
|
1663 |
|
deba@1423
|
1664 |
/// \brief Gives back how many nodesets are in the file.
|
deba@1423
|
1665 |
///
|
deba@1423
|
1666 |
/// Gives back how many nodesets are in the file.
|
deba@1423
|
1667 |
int nodeSetNum() const {
|
deba@1423
|
1668 |
return nodesets.size();
|
deba@1423
|
1669 |
}
|
deba@1423
|
1670 |
|
deba@1423
|
1671 |
/// \brief Gives back the name of nodeset on the indiced position.
|
deba@1423
|
1672 |
///
|
deba@1423
|
1673 |
/// Gives back the name of nodeset on the indiced position.
|
deba@1423
|
1674 |
std::string nodeSetName(int index) const {
|
deba@1423
|
1675 |
return nodesets[index].name;
|
deba@1423
|
1676 |
}
|
deba@1423
|
1677 |
|
deba@1423
|
1678 |
/// \brief Gives back the map names of nodeset on the indiced position.
|
deba@1423
|
1679 |
///
|
deba@1423
|
1680 |
/// Gives back the map names of nodeset on the indiced position.
|
deba@1423
|
1681 |
const std::vector<std::string>& nodeSetMaps(int index) const {
|
deba@1423
|
1682 |
return nodesets[index].items;
|
deba@1423
|
1683 |
}
|
deba@1423
|
1684 |
|
deba@1423
|
1685 |
/// \brief Gives back how many edgesets are in the file.
|
deba@1423
|
1686 |
///
|
deba@1423
|
1687 |
/// Gives back how many edgesets are in the file.
|
deba@1423
|
1688 |
int edgeSetNum() const {
|
deba@1423
|
1689 |
return edgesets.size();
|
deba@1423
|
1690 |
}
|
deba@1423
|
1691 |
|
deba@1423
|
1692 |
/// \brief Gives back the name of edgeset on the indiced position.
|
deba@1423
|
1693 |
///
|
deba@1423
|
1694 |
/// Gives back the name of edgeset on the indiced position.
|
deba@1423
|
1695 |
std::string edgeSetName(int index) const {
|
deba@1423
|
1696 |
return edgesets[index].name;
|
deba@1423
|
1697 |
}
|
deba@1423
|
1698 |
|
deba@1423
|
1699 |
/// \brief Gives back the map names of edgeset on the indiced position.
|
deba@1423
|
1700 |
///
|
deba@1423
|
1701 |
/// Gives back the map names of edgeset on the indiced position.
|
deba@1423
|
1702 |
const std::vector<std::string>& edgeSetMaps(int index) const {
|
deba@1423
|
1703 |
return edgesets[index].items;
|
deba@1423
|
1704 |
}
|
deba@1423
|
1705 |
|
deba@1423
|
1706 |
/// \brief Gives back how many undirected edgesets are in the file.
|
deba@1423
|
1707 |
///
|
deba@1423
|
1708 |
/// Gives back how many undirected edgesets are in the file.
|
deba@1423
|
1709 |
int undirEdgeSetNum() const {
|
deba@1423
|
1710 |
return undiredgesets.size();
|
deba@1423
|
1711 |
}
|
deba@1423
|
1712 |
|
deba@1423
|
1713 |
/// \brief Gives back the name of undirected edgeset on the indiced
|
deba@1423
|
1714 |
/// position.
|
deba@1423
|
1715 |
///
|
deba@1423
|
1716 |
/// Gives back the name of undirected edgeset on the indiced position.
|
deba@1423
|
1717 |
std::string undirEdgeSetName(int index) const {
|
deba@1423
|
1718 |
return undiredgesets[index].name;
|
deba@1423
|
1719 |
}
|
deba@1423
|
1720 |
|
deba@1423
|
1721 |
/// \brief Gives back the map names of undirected edgeset on the indiced
|
deba@1423
|
1722 |
/// position.
|
deba@1423
|
1723 |
///
|
deba@1423
|
1724 |
/// Gives back the map names of undirected edgeset on the indiced position.
|
deba@1423
|
1725 |
const std::vector<std::string>& undirEdgeSetMaps(int index) const {
|
deba@1423
|
1726 |
return undiredgesets[index].items;
|
deba@1423
|
1727 |
}
|
deba@1423
|
1728 |
|
deba@1423
|
1729 |
/// \brief Gives back how many labeled nodes section are in the file.
|
deba@1423
|
1730 |
///
|
deba@1423
|
1731 |
/// Gives back how many labeled nodes section are in the file.
|
deba@1423
|
1732 |
int nodesNum() const {
|
deba@1423
|
1733 |
return nodes.size();
|
deba@1423
|
1734 |
}
|
deba@1423
|
1735 |
|
deba@1423
|
1736 |
/// \brief Gives back the name of labeled nodes section on the indiced
|
deba@1423
|
1737 |
/// position.
|
deba@1423
|
1738 |
///
|
deba@1423
|
1739 |
/// Gives back the name of labeled nodes section on the indiced position.
|
deba@1423
|
1740 |
std::string nodesName(int index) const {
|
deba@1423
|
1741 |
return nodes[index].name;
|
deba@1423
|
1742 |
}
|
deba@1423
|
1743 |
|
deba@1423
|
1744 |
/// \brief Gives back the names of the labeled nodes in the indiced
|
deba@1423
|
1745 |
/// section.
|
deba@1423
|
1746 |
///
|
deba@1423
|
1747 |
/// Gives back the names of the labeled nodes in the indiced section.
|
deba@1423
|
1748 |
const std::vector<std::string>& nodesItems(int index) const {
|
deba@1423
|
1749 |
return nodes[index].items;
|
deba@1423
|
1750 |
}
|
deba@1423
|
1751 |
|
deba@1423
|
1752 |
/// \brief Gives back how many labeled edges section are in the file.
|
deba@1423
|
1753 |
///
|
deba@1423
|
1754 |
/// Gives back how many labeled edges section are in the file.
|
deba@1423
|
1755 |
int edgesNum() const {
|
deba@1423
|
1756 |
return edges.size();
|
deba@1423
|
1757 |
}
|
deba@1423
|
1758 |
|
deba@1423
|
1759 |
/// \brief Gives back the name of labeled edges section on the indiced
|
deba@1423
|
1760 |
/// position.
|
deba@1423
|
1761 |
///
|
deba@1423
|
1762 |
/// Gives back the name of labeled edges section on the indiced position.
|
deba@1423
|
1763 |
std::string edgesName(int index) const {
|
deba@1423
|
1764 |
return edges[index].name;
|
deba@1423
|
1765 |
}
|
deba@1423
|
1766 |
|
deba@1423
|
1767 |
/// \brief Gives back the names of the labeled edges in the indiced
|
deba@1423
|
1768 |
/// section.
|
deba@1423
|
1769 |
///
|
deba@1423
|
1770 |
/// Gives back the names of the labeled edges in the indiced section.
|
deba@1423
|
1771 |
const std::vector<std::string>& edgesItems(int index) const {
|
deba@1423
|
1772 |
return edges[index].items;
|
deba@1423
|
1773 |
}
|
deba@1423
|
1774 |
|
deba@1423
|
1775 |
/// \brief Gives back how many labeled undirected edges section are
|
deba@1423
|
1776 |
/// in the file.
|
deba@1423
|
1777 |
///
|
deba@1423
|
1778 |
/// Gives back how many labeled undirected edges section are in the file.
|
deba@1423
|
1779 |
int undirEdgesNum() const {
|
deba@1423
|
1780 |
return undiredges.size();
|
deba@1423
|
1781 |
}
|
deba@1423
|
1782 |
|
deba@1423
|
1783 |
/// \brief Gives back the name of labeled undirected edges section
|
deba@1423
|
1784 |
/// on the indiced position.
|
deba@1423
|
1785 |
///
|
deba@1423
|
1786 |
/// Gives back the name of labeled undirected edges section on the
|
deba@1423
|
1787 |
/// indiced position.
|
deba@1423
|
1788 |
std::string undirEdgesName(int index) const {
|
deba@1423
|
1789 |
return undiredges[index].name;
|
deba@1423
|
1790 |
}
|
deba@1423
|
1791 |
|
deba@1423
|
1792 |
/// \brief Gives back the names of the labeled undirected edges in
|
deba@1423
|
1793 |
/// the indiced section.
|
deba@1423
|
1794 |
///
|
deba@1423
|
1795 |
/// Gives back the names of the labeled undirected edges in the
|
deba@1423
|
1796 |
/// indiced section.
|
deba@1423
|
1797 |
const std::vector<std::string>& undirEdgesItems(int index) const {
|
deba@1423
|
1798 |
return undiredges[index].items;
|
deba@1423
|
1799 |
}
|
deba@1423
|
1800 |
|
deba@1423
|
1801 |
|
deba@1423
|
1802 |
/// \brief Gives back how many attributes section are in the file.
|
deba@1423
|
1803 |
///
|
deba@1423
|
1804 |
/// Gives back how many attributes section are in the file.
|
deba@1423
|
1805 |
int attributesNum() const {
|
deba@1423
|
1806 |
return attributes.size();
|
deba@1423
|
1807 |
}
|
deba@1423
|
1808 |
|
deba@1423
|
1809 |
/// \brief Gives back the name of attributes section on the indiced
|
deba@1423
|
1810 |
/// position.
|
deba@1423
|
1811 |
///
|
deba@1423
|
1812 |
/// Gives back the name of attributes section on the indiced position.
|
deba@1423
|
1813 |
std::string attributesName(int index) const {
|
deba@1423
|
1814 |
return attributes[index].name;
|
deba@1423
|
1815 |
}
|
deba@1423
|
1816 |
|
deba@1423
|
1817 |
/// \brief Gives back the names of the attributes in the indiced section.
|
deba@1423
|
1818 |
///
|
deba@1423
|
1819 |
/// Gives back the names of the attributes in the indiced section.
|
deba@1423
|
1820 |
const std::vector<std::string>& attributesItems(int index) const {
|
deba@1423
|
1821 |
return attributes[index].items;
|
deba@1423
|
1822 |
}
|
deba@1423
|
1823 |
|
deba@1423
|
1824 |
const std::vector<std::string>& otherSections() const {
|
deba@1423
|
1825 |
return sections;
|
deba@1423
|
1826 |
}
|
deba@1423
|
1827 |
|
deba@1423
|
1828 |
protected:
|
deba@1423
|
1829 |
|
deba@1423
|
1830 |
/// \brief Gives back true when the SectionReader can process
|
deba@1423
|
1831 |
/// the section with the given header line.
|
deba@1423
|
1832 |
///
|
deba@1423
|
1833 |
/// It gives back true when the section is common section.
|
deba@1423
|
1834 |
bool header(const std::string& line) {
|
deba@1423
|
1835 |
std::istringstream ls(line);
|
deba@1423
|
1836 |
std::string command, name;
|
deba@1423
|
1837 |
ls >> command >> name;
|
deba@1423
|
1838 |
if (command == "@nodeset") {
|
deba@1423
|
1839 |
current = command;
|
deba@1423
|
1840 |
nodesets.push_back(SectionInfo(name));
|
deba@1423
|
1841 |
} else if (command == "@edgeset") {
|
deba@1423
|
1842 |
current = command;
|
deba@1423
|
1843 |
edgesets.push_back(SectionInfo(name));
|
deba@1423
|
1844 |
} else if (command == "@undiredgeset") {
|
deba@1423
|
1845 |
current = command;
|
deba@1423
|
1846 |
undiredgesets.push_back(SectionInfo(name));
|
deba@1423
|
1847 |
} else if (command == "@nodes") {
|
deba@1423
|
1848 |
current = command;
|
deba@1423
|
1849 |
nodes.push_back(SectionInfo(name));
|
deba@1423
|
1850 |
} else if (command == "@edges") {
|
deba@1423
|
1851 |
current = command;
|
deba@1423
|
1852 |
edges.push_back(SectionInfo(name));
|
deba@1423
|
1853 |
} else if (command == "@undiredges") {
|
deba@1423
|
1854 |
current = command;
|
deba@1423
|
1855 |
undiredges.push_back(SectionInfo(name));
|
deba@1423
|
1856 |
} else if (command == "@attributes") {
|
deba@1423
|
1857 |
current = command;
|
deba@1423
|
1858 |
attributes.push_back(SectionInfo(name));
|
deba@1423
|
1859 |
} else {
|
deba@1423
|
1860 |
sections.push_back(line);
|
deba@1423
|
1861 |
return false;
|
deba@1423
|
1862 |
}
|
deba@1423
|
1863 |
return true;
|
deba@1423
|
1864 |
}
|
deba@1423
|
1865 |
|
deba@1423
|
1866 |
/// \brief Retrieve the items from various sections.
|
deba@1423
|
1867 |
///
|
deba@1423
|
1868 |
/// Retrieve the items from various sections.
|
deba@1423
|
1869 |
void read(std::istream& is) {
|
deba@1423
|
1870 |
if (current == "@nodeset") {
|
deba@1423
|
1871 |
readMapNames(is, nodesets.back().items);
|
deba@1423
|
1872 |
} else if (current == "@edgeset") {
|
deba@1423
|
1873 |
readMapNames(is, edgesets.back().items);
|
deba@1423
|
1874 |
} else if (current == "@undiredgeset") {
|
deba@1423
|
1875 |
readMapNames(is, undiredgesets.back().items);
|
deba@1423
|
1876 |
} else if (current == "@nodes") {
|
deba@1423
|
1877 |
readItemNames(is, nodes.back().items);
|
deba@1423
|
1878 |
} else if (current == "@edges") {
|
deba@1423
|
1879 |
readItemNames(is, edges.back().items);
|
deba@1423
|
1880 |
} else if (current == "@undiredges") {
|
deba@1423
|
1881 |
readItemNames(is, undiredges.back().items);
|
deba@1423
|
1882 |
} else if (current == "@attributes") {
|
deba@1423
|
1883 |
readItemNames(is, attributes.back().items);
|
deba@1423
|
1884 |
}
|
deba@1423
|
1885 |
}
|
deba@1423
|
1886 |
|
deba@1423
|
1887 |
private:
|
deba@1423
|
1888 |
|
deba@1423
|
1889 |
void readMapNames(std::istream& is, std::vector<std::string>& maps) {
|
deba@1423
|
1890 |
std::string line, id;
|
deba@1423
|
1891 |
std::getline(is, line);
|
deba@1423
|
1892 |
std::istringstream ls(line);
|
deba@1423
|
1893 |
while (ls >> id) {
|
deba@1423
|
1894 |
maps.push_back(id);
|
deba@1423
|
1895 |
}
|
deba@1423
|
1896 |
while (getline(is, line));
|
deba@1423
|
1897 |
}
|
deba@1423
|
1898 |
|
deba@1423
|
1899 |
void readItemNames(std::istream& is, std::vector<std::string>& maps) {
|
deba@1423
|
1900 |
std::string line, id;
|
deba@1423
|
1901 |
while (std::getline(is, line)) {
|
deba@1423
|
1902 |
std::istringstream ls(line);
|
deba@1423
|
1903 |
ls >> id;
|
deba@1423
|
1904 |
maps.push_back(id);
|
deba@1423
|
1905 |
}
|
deba@1423
|
1906 |
}
|
deba@1423
|
1907 |
|
deba@1423
|
1908 |
struct SectionInfo {
|
deba@1423
|
1909 |
std::string name;
|
deba@1423
|
1910 |
std::vector<std::string> items;
|
deba@1423
|
1911 |
|
deba@1423
|
1912 |
SectionInfo(const std::string& _name) : name(_name) {}
|
deba@1423
|
1913 |
};
|
deba@1423
|
1914 |
|
deba@1423
|
1915 |
std::vector<SectionInfo> nodesets;
|
deba@1423
|
1916 |
std::vector<SectionInfo> edgesets;
|
deba@1423
|
1917 |
std::vector<SectionInfo> undiredgesets;
|
deba@1423
|
1918 |
|
deba@1423
|
1919 |
std::vector<SectionInfo> nodes;
|
deba@1423
|
1920 |
std::vector<SectionInfo> edges;
|
deba@1423
|
1921 |
std::vector<SectionInfo> undiredges;
|
deba@1423
|
1922 |
|
deba@1423
|
1923 |
std::vector<SectionInfo> attributes;
|
deba@1423
|
1924 |
|
deba@1423
|
1925 |
std::vector<std::string> sections;
|
deba@1423
|
1926 |
|
deba@1423
|
1927 |
std::string current;
|
deba@1423
|
1928 |
|
deba@1423
|
1929 |
};
|
deba@1423
|
1930 |
|
deba@1408
|
1931 |
}
|
deba@1408
|
1932 |
#endif
|